]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1abb0dc9 DB |
2 | /* |
3 | * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips. | |
4 | * | |
5 | * Copyright (C) 2005 James Chapman (ds1337 core) | |
6 | * Copyright (C) 2006 David Brownell | |
a2166858 | 7 | * Copyright (C) 2009 Matthias Fuchs (rx8025 support) |
bc48b902 | 8 | * Copyright (C) 2012 Bertrand Achard (nvram access fixes) |
1abb0dc9 DB |
9 | */ |
10 | ||
9c19b893 | 11 | #include <linux/acpi.h> |
eac7237f NM |
12 | #include <linux/bcd.h> |
13 | #include <linux/i2c.h> | |
1abb0dc9 | 14 | #include <linux/init.h> |
eac7237f | 15 | #include <linux/module.h> |
7ef6d2c2 | 16 | #include <linux/of_device.h> |
eac7237f NM |
17 | #include <linux/rtc/ds1307.h> |
18 | #include <linux/rtc.h> | |
1abb0dc9 | 19 | #include <linux/slab.h> |
1abb0dc9 | 20 | #include <linux/string.h> |
445c0207 AM |
21 | #include <linux/hwmon.h> |
22 | #include <linux/hwmon-sysfs.h> | |
6c6ff145 | 23 | #include <linux/clk-provider.h> |
11e5890b | 24 | #include <linux/regmap.h> |
1abb0dc9 | 25 | |
40ce972d DA |
26 | /* |
27 | * We can't determine type by probing, but if we expect pre-Linux code | |
1abb0dc9 DB |
28 | * to have set the chip up as a clock (turning on the oscillator and |
29 | * setting the date and time), Linux can ignore the non-clock features. | |
30 | * That's a natural job for a factory or repair bench. | |
1abb0dc9 DB |
31 | */ |
32 | enum ds_type { | |
045e0e85 | 33 | ds_1307, |
300a7735 | 34 | ds_1308, |
045e0e85 DB |
35 | ds_1337, |
36 | ds_1338, | |
37 | ds_1339, | |
38 | ds_1340, | |
0759c886 | 39 | ds_1341, |
33df2ee1 | 40 | ds_1388, |
97f902b7 | 41 | ds_3231, |
8566f70c | 42 | m41t0, |
045e0e85 | 43 | m41t00, |
7e580769 | 44 | m41t11, |
f4199f85 | 45 | mcp794xx, |
a2166858 | 46 | rx_8025, |
ee0981be | 47 | rx_8130, |
32d322bc | 48 | last_ds_type /* always last */ |
40ce972d | 49 | /* rs5c372 too? different address... */ |
1abb0dc9 DB |
50 | }; |
51 | ||
1abb0dc9 DB |
52 | /* RTC registers don't differ much, except for the century flag */ |
53 | #define DS1307_REG_SECS 0x00 /* 00-59 */ | |
54 | # define DS1307_BIT_CH 0x80 | |
be5f59f4 | 55 | # define DS1340_BIT_nEOSC 0x80 |
f4199f85 | 56 | # define MCP794XX_BIT_ST 0x80 |
1abb0dc9 | 57 | #define DS1307_REG_MIN 0x01 /* 00-59 */ |
8566f70c | 58 | # define M41T0_BIT_OF 0x80 |
1abb0dc9 | 59 | #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */ |
c065f35c DB |
60 | # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */ |
61 | # define DS1307_BIT_PM 0x20 /* in REG_HOUR */ | |
1abb0dc9 DB |
62 | # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */ |
63 | # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */ | |
64 | #define DS1307_REG_WDAY 0x03 /* 01-07 */ | |
f4199f85 | 65 | # define MCP794XX_BIT_VBATEN 0x08 |
1abb0dc9 DB |
66 | #define DS1307_REG_MDAY 0x04 /* 01-31 */ |
67 | #define DS1307_REG_MONTH 0x05 /* 01-12 */ | |
68 | # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */ | |
69 | #define DS1307_REG_YEAR 0x06 /* 00-99 */ | |
70 | ||
40ce972d DA |
71 | /* |
72 | * Other registers (control, status, alarms, trickle charge, NVRAM, etc) | |
045e0e85 DB |
73 | * start at 7, and they differ a LOT. Only control and status matter for |
74 | * basic RTC date and time functionality; be careful using them. | |
1abb0dc9 | 75 | */ |
045e0e85 | 76 | #define DS1307_REG_CONTROL 0x07 /* or ds1338 */ |
1abb0dc9 | 77 | # define DS1307_BIT_OUT 0x80 |
be5f59f4 | 78 | # define DS1338_BIT_OSF 0x20 |
1abb0dc9 DB |
79 | # define DS1307_BIT_SQWE 0x10 |
80 | # define DS1307_BIT_RS1 0x02 | |
81 | # define DS1307_BIT_RS0 0x01 | |
82 | #define DS1337_REG_CONTROL 0x0e | |
83 | # define DS1337_BIT_nEOSC 0x80 | |
cb49a5e9 | 84 | # define DS1339_BIT_BBSQI 0x20 |
97f902b7 | 85 | # define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */ |
1abb0dc9 DB |
86 | # define DS1337_BIT_RS2 0x10 |
87 | # define DS1337_BIT_RS1 0x08 | |
88 | # define DS1337_BIT_INTCN 0x04 | |
89 | # define DS1337_BIT_A2IE 0x02 | |
90 | # define DS1337_BIT_A1IE 0x01 | |
045e0e85 DB |
91 | #define DS1340_REG_CONTROL 0x07 |
92 | # define DS1340_BIT_OUT 0x80 | |
93 | # define DS1340_BIT_FT 0x40 | |
94 | # define DS1340_BIT_CALIB_SIGN 0x20 | |
95 | # define DS1340_M_CALIBRATION 0x1f | |
be5f59f4 RG |
96 | #define DS1340_REG_FLAG 0x09 |
97 | # define DS1340_BIT_OSF 0x80 | |
1abb0dc9 DB |
98 | #define DS1337_REG_STATUS 0x0f |
99 | # define DS1337_BIT_OSF 0x80 | |
6c6ff145 | 100 | # define DS3231_BIT_EN32KHZ 0x08 |
1abb0dc9 DB |
101 | # define DS1337_BIT_A2I 0x02 |
102 | # define DS1337_BIT_A1I 0x01 | |
cb49a5e9 | 103 | #define DS1339_REG_ALARM1_SECS 0x07 |
eb86c306 WS |
104 | |
105 | #define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0 | |
1abb0dc9 | 106 | |
a2166858 MF |
107 | #define RX8025_REG_CTRL1 0x0e |
108 | # define RX8025_BIT_2412 0x20 | |
109 | #define RX8025_REG_CTRL2 0x0f | |
110 | # define RX8025_BIT_PON 0x10 | |
111 | # define RX8025_BIT_VDET 0x40 | |
112 | # define RX8025_BIT_XST 0x20 | |
1abb0dc9 | 113 | |
3ffd4a2f UKK |
114 | #define RX8130_REG_ALARM_MIN 0x17 |
115 | #define RX8130_REG_ALARM_HOUR 0x18 | |
116 | #define RX8130_REG_ALARM_WEEK_OR_DAY 0x19 | |
117 | #define RX8130_REG_EXTENSION 0x1c | |
92cbf12f | 118 | #define RX8130_REG_EXTENSION_WADA BIT(3) |
3ffd4a2f UKK |
119 | #define RX8130_REG_FLAG 0x1d |
120 | #define RX8130_REG_FLAG_VLF BIT(1) | |
92cbf12f | 121 | #define RX8130_REG_FLAG_AF BIT(3) |
3ffd4a2f | 122 | #define RX8130_REG_CONTROL0 0x1e |
92cbf12f UKK |
123 | #define RX8130_REG_CONTROL0_AIE BIT(3) |
124 | ||
125 | #define MCP794XX_REG_CONTROL 0x07 | |
126 | # define MCP794XX_BIT_ALM0_EN 0x10 | |
127 | # define MCP794XX_BIT_ALM1_EN 0x20 | |
128 | #define MCP794XX_REG_ALARM0_BASE 0x0a | |
129 | #define MCP794XX_REG_ALARM0_CTRL 0x0d | |
130 | #define MCP794XX_REG_ALARM1_BASE 0x11 | |
131 | #define MCP794XX_REG_ALARM1_CTRL 0x14 | |
132 | # define MCP794XX_BIT_ALMX_IF BIT(3) | |
133 | # define MCP794XX_BIT_ALMX_C0 BIT(4) | |
134 | # define MCP794XX_BIT_ALMX_C1 BIT(5) | |
135 | # define MCP794XX_BIT_ALMX_C2 BIT(6) | |
136 | # define MCP794XX_BIT_ALMX_POL BIT(7) | |
137 | # define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \ | |
138 | MCP794XX_BIT_ALMX_C1 | \ | |
139 | MCP794XX_BIT_ALMX_C2) | |
140 | ||
79230ff6 GB |
141 | #define M41TXX_REG_CONTROL 0x07 |
142 | # define M41TXX_BIT_OUT BIT(7) | |
143 | # define M41TXX_BIT_FT BIT(6) | |
144 | # define M41TXX_BIT_CALIB_SIGN BIT(5) | |
145 | # define M41TXX_M_CALIBRATION GENMASK(4, 0) | |
146 | ||
147 | /* negative offset step is -2.034ppm */ | |
148 | #define M41TXX_NEG_OFFSET_STEP_PPB 2034 | |
149 | /* positive offset step is +4.068ppm */ | |
150 | #define M41TXX_POS_OFFSET_STEP_PPB 4068 | |
151 | /* Min and max values supported with 'offset' interface by M41TXX */ | |
152 | #define M41TXX_MIN_OFFSET ((-31) * M41TXX_NEG_OFFSET_STEP_PPB) | |
153 | #define M41TXX_MAX_OFFSET ((31) * M41TXX_POS_OFFSET_STEP_PPB) | |
154 | ||
1abb0dc9 | 155 | struct ds1307 { |
1abb0dc9 | 156 | enum ds_type type; |
cb49a5e9 RG |
157 | unsigned long flags; |
158 | #define HAS_NVRAM 0 /* bit 0 == sysfs file active */ | |
159 | #define HAS_ALARM 1 /* bit 1 == irq claimed */ | |
11e5890b HK |
160 | struct device *dev; |
161 | struct regmap *regmap; | |
162 | const char *name; | |
1abb0dc9 | 163 | struct rtc_device *rtc; |
6c6ff145 AM |
164 | #ifdef CONFIG_COMMON_CLK |
165 | struct clk_hw clks[2]; | |
166 | #endif | |
1abb0dc9 DB |
167 | }; |
168 | ||
045e0e85 | 169 | struct chip_desc { |
045e0e85 | 170 | unsigned alarm:1; |
9eab0a78 AB |
171 | u16 nvram_offset; |
172 | u16 nvram_size; | |
e553170a | 173 | u8 offset; /* register's offset */ |
e48585de HK |
174 | u8 century_reg; |
175 | u8 century_enable_bit; | |
176 | u8 century_bit; | |
0b6ee805 | 177 | u8 bbsqi_bit; |
45947127 | 178 | irq_handler_t irq_handler; |
1efb98ba | 179 | const struct rtc_class_ops *rtc_ops; |
eb86c306 | 180 | u16 trickle_charger_reg; |
57ec2d95 | 181 | u8 (*do_trickle_setup)(struct ds1307 *, u32, |
11e5890b | 182 | bool); |
045e0e85 DB |
183 | }; |
184 | ||
d0e3f61b | 185 | static const struct chip_desc chips[last_ds_type]; |
cb49a5e9 | 186 | |
1abb0dc9 DB |
187 | static int ds1307_get_time(struct device *dev, struct rtc_time *t) |
188 | { | |
189 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
11e5890b | 190 | int tmp, ret; |
e48585de | 191 | const struct chip_desc *chip = &chips[ds1307->type]; |
042fa8c7 | 192 | u8 regs[7]; |
1abb0dc9 | 193 | |
501f9826 UKK |
194 | if (ds1307->type == rx_8130) { |
195 | unsigned int regflag; | |
196 | ret = regmap_read(ds1307->regmap, RX8130_REG_FLAG, ®flag); | |
197 | if (ret) { | |
198 | dev_err(dev, "%s error %d\n", "read", ret); | |
199 | return ret; | |
200 | } | |
201 | ||
202 | if (regflag & RX8130_REG_FLAG_VLF) { | |
203 | dev_warn_once(dev, "oscillator failed, set time!\n"); | |
204 | return -EINVAL; | |
205 | } | |
206 | } | |
207 | ||
045e0e85 | 208 | /* read the RTC date and time registers all at once */ |
042fa8c7 AB |
209 | ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs, |
210 | sizeof(regs)); | |
11e5890b HK |
211 | if (ret) { |
212 | dev_err(dev, "%s error %d\n", "read", ret); | |
213 | return ret; | |
1abb0dc9 DB |
214 | } |
215 | ||
042fa8c7 | 216 | dev_dbg(dev, "%s: %7ph\n", "read", regs); |
1abb0dc9 | 217 | |
8566f70c SA |
218 | /* if oscillator fail bit is set, no data can be trusted */ |
219 | if (ds1307->type == m41t0 && | |
042fa8c7 | 220 | regs[DS1307_REG_MIN] & M41T0_BIT_OF) { |
8566f70c SA |
221 | dev_warn_once(dev, "oscillator failed, set time!\n"); |
222 | return -EINVAL; | |
223 | } | |
224 | ||
042fa8c7 AB |
225 | t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f); |
226 | t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f); | |
227 | tmp = regs[DS1307_REG_HOUR] & 0x3f; | |
fe20ba70 | 228 | t->tm_hour = bcd2bin(tmp); |
042fa8c7 AB |
229 | t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1; |
230 | t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f); | |
231 | tmp = regs[DS1307_REG_MONTH] & 0x1f; | |
fe20ba70 | 232 | t->tm_mon = bcd2bin(tmp) - 1; |
042fa8c7 | 233 | t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100; |
1abb0dc9 | 234 | |
042fa8c7 | 235 | if (regs[chip->century_reg] & chip->century_bit && |
e48585de HK |
236 | IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY)) |
237 | t->tm_year += 100; | |
50d6c0ea | 238 | |
1abb0dc9 DB |
239 | dev_dbg(dev, "%s secs=%d, mins=%d, " |
240 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", | |
241 | "read", t->tm_sec, t->tm_min, | |
242 | t->tm_hour, t->tm_mday, | |
243 | t->tm_mon, t->tm_year, t->tm_wday); | |
244 | ||
22652ba7 | 245 | return 0; |
1abb0dc9 DB |
246 | } |
247 | ||
248 | static int ds1307_set_time(struct device *dev, struct rtc_time *t) | |
249 | { | |
250 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
e48585de | 251 | const struct chip_desc *chip = &chips[ds1307->type]; |
1abb0dc9 DB |
252 | int result; |
253 | int tmp; | |
042fa8c7 | 254 | u8 regs[7]; |
1abb0dc9 DB |
255 | |
256 | dev_dbg(dev, "%s secs=%d, mins=%d, " | |
257 | "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n", | |
11966adc JG |
258 | "write", t->tm_sec, t->tm_min, |
259 | t->tm_hour, t->tm_mday, | |
260 | t->tm_mon, t->tm_year, t->tm_wday); | |
1abb0dc9 | 261 | |
50d6c0ea AB |
262 | if (t->tm_year < 100) |
263 | return -EINVAL; | |
264 | ||
e48585de HK |
265 | #ifdef CONFIG_RTC_DRV_DS1307_CENTURY |
266 | if (t->tm_year > (chip->century_bit ? 299 : 199)) | |
267 | return -EINVAL; | |
50d6c0ea | 268 | #else |
e48585de | 269 | if (t->tm_year > 199) |
50d6c0ea AB |
270 | return -EINVAL; |
271 | #endif | |
272 | ||
042fa8c7 AB |
273 | regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec); |
274 | regs[DS1307_REG_MIN] = bin2bcd(t->tm_min); | |
275 | regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour); | |
276 | regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1); | |
277 | regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday); | |
278 | regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1); | |
1abb0dc9 DB |
279 | |
280 | /* assume 20YY not 19YY */ | |
281 | tmp = t->tm_year - 100; | |
042fa8c7 | 282 | regs[DS1307_REG_YEAR] = bin2bcd(tmp); |
1abb0dc9 | 283 | |
e48585de | 284 | if (chip->century_enable_bit) |
042fa8c7 | 285 | regs[chip->century_reg] |= chip->century_enable_bit; |
e48585de | 286 | if (t->tm_year > 199 && chip->century_bit) |
042fa8c7 | 287 | regs[chip->century_reg] |= chip->century_bit; |
e48585de HK |
288 | |
289 | if (ds1307->type == mcp794xx) { | |
40ce972d DA |
290 | /* |
291 | * these bits were cleared when preparing the date/time | |
292 | * values and need to be set again before writing the | |
042fa8c7 | 293 | * regsfer out to the device. |
40ce972d | 294 | */ |
042fa8c7 AB |
295 | regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST; |
296 | regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN; | |
be5f59f4 | 297 | } |
1abb0dc9 | 298 | |
042fa8c7 | 299 | dev_dbg(dev, "%s: %7ph\n", "write", regs); |
1abb0dc9 | 300 | |
042fa8c7 AB |
301 | result = regmap_bulk_write(ds1307->regmap, chip->offset, regs, |
302 | sizeof(regs)); | |
11e5890b | 303 | if (result) { |
fed40b73 BS |
304 | dev_err(dev, "%s error %d\n", "write", result); |
305 | return result; | |
1abb0dc9 | 306 | } |
501f9826 UKK |
307 | |
308 | if (ds1307->type == rx_8130) { | |
309 | /* clear Voltage Loss Flag as data is available now */ | |
310 | result = regmap_write(ds1307->regmap, RX8130_REG_FLAG, | |
311 | ~(u8)RX8130_REG_FLAG_VLF); | |
312 | if (result) { | |
313 | dev_err(dev, "%s error %d\n", "write", result); | |
314 | return result; | |
315 | } | |
316 | } | |
317 | ||
1abb0dc9 DB |
318 | return 0; |
319 | } | |
320 | ||
74d88eb2 | 321 | static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t) |
cb49a5e9 | 322 | { |
11e5890b | 323 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
cb49a5e9 | 324 | int ret; |
042fa8c7 | 325 | u8 regs[9]; |
cb49a5e9 RG |
326 | |
327 | if (!test_bit(HAS_ALARM, &ds1307->flags)) | |
328 | return -EINVAL; | |
329 | ||
330 | /* read all ALARM1, ALARM2, and status registers at once */ | |
11e5890b | 331 | ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, |
042fa8c7 | 332 | regs, sizeof(regs)); |
11e5890b | 333 | if (ret) { |
cb49a5e9 | 334 | dev_err(dev, "%s error %d\n", "alarm read", ret); |
11e5890b | 335 | return ret; |
cb49a5e9 RG |
336 | } |
337 | ||
ff67abd2 | 338 | dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read", |
042fa8c7 | 339 | ®s[0], ®s[4], ®s[7]); |
cb49a5e9 | 340 | |
40ce972d DA |
341 | /* |
342 | * report alarm time (ALARM1); assume 24 hour and day-of-month modes, | |
cb49a5e9 RG |
343 | * and that all four fields are checked matches |
344 | */ | |
042fa8c7 AB |
345 | t->time.tm_sec = bcd2bin(regs[0] & 0x7f); |
346 | t->time.tm_min = bcd2bin(regs[1] & 0x7f); | |
347 | t->time.tm_hour = bcd2bin(regs[2] & 0x3f); | |
348 | t->time.tm_mday = bcd2bin(regs[3] & 0x3f); | |
cb49a5e9 RG |
349 | |
350 | /* ... and status */ | |
042fa8c7 AB |
351 | t->enabled = !!(regs[7] & DS1337_BIT_A1IE); |
352 | t->pending = !!(regs[8] & DS1337_BIT_A1I); | |
cb49a5e9 RG |
353 | |
354 | dev_dbg(dev, "%s secs=%d, mins=%d, " | |
355 | "hours=%d, mday=%d, enabled=%d, pending=%d\n", | |
356 | "alarm read", t->time.tm_sec, t->time.tm_min, | |
357 | t->time.tm_hour, t->time.tm_mday, | |
358 | t->enabled, t->pending); | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
74d88eb2 | 363 | static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t) |
cb49a5e9 | 364 | { |
11e5890b | 365 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
042fa8c7 | 366 | unsigned char regs[9]; |
cb49a5e9 RG |
367 | u8 control, status; |
368 | int ret; | |
369 | ||
370 | if (!test_bit(HAS_ALARM, &ds1307->flags)) | |
371 | return -EINVAL; | |
372 | ||
373 | dev_dbg(dev, "%s secs=%d, mins=%d, " | |
374 | "hours=%d, mday=%d, enabled=%d, pending=%d\n", | |
375 | "alarm set", t->time.tm_sec, t->time.tm_min, | |
376 | t->time.tm_hour, t->time.tm_mday, | |
377 | t->enabled, t->pending); | |
378 | ||
379 | /* read current status of both alarms and the chip */ | |
042fa8c7 AB |
380 | ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs, |
381 | sizeof(regs)); | |
11e5890b | 382 | if (ret) { |
cb49a5e9 | 383 | dev_err(dev, "%s error %d\n", "alarm write", ret); |
11e5890b | 384 | return ret; |
cb49a5e9 | 385 | } |
042fa8c7 AB |
386 | control = regs[7]; |
387 | status = regs[8]; | |
cb49a5e9 | 388 | |
ff67abd2 | 389 | dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)", |
042fa8c7 | 390 | ®s[0], ®s[4], control, status); |
cb49a5e9 RG |
391 | |
392 | /* set ALARM1, using 24 hour and day-of-month modes */ | |
042fa8c7 AB |
393 | regs[0] = bin2bcd(t->time.tm_sec); |
394 | regs[1] = bin2bcd(t->time.tm_min); | |
395 | regs[2] = bin2bcd(t->time.tm_hour); | |
396 | regs[3] = bin2bcd(t->time.tm_mday); | |
cb49a5e9 RG |
397 | |
398 | /* set ALARM2 to non-garbage */ | |
042fa8c7 AB |
399 | regs[4] = 0; |
400 | regs[5] = 0; | |
401 | regs[6] = 0; | |
cb49a5e9 | 402 | |
5919fb97 | 403 | /* disable alarms */ |
042fa8c7 AB |
404 | regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE); |
405 | regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I); | |
cb49a5e9 | 406 | |
042fa8c7 AB |
407 | ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs, |
408 | sizeof(regs)); | |
11e5890b | 409 | if (ret) { |
cb49a5e9 | 410 | dev_err(dev, "can't set alarm time\n"); |
fed40b73 | 411 | return ret; |
cb49a5e9 RG |
412 | } |
413 | ||
5919fb97 NB |
414 | /* optionally enable ALARM1 */ |
415 | if (t->enabled) { | |
416 | dev_dbg(dev, "alarm IRQ armed\n"); | |
042fa8c7 AB |
417 | regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */ |
418 | regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]); | |
5919fb97 NB |
419 | } |
420 | ||
cb49a5e9 RG |
421 | return 0; |
422 | } | |
423 | ||
16380c15 | 424 | static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled) |
cb49a5e9 | 425 | { |
11e5890b | 426 | struct ds1307 *ds1307 = dev_get_drvdata(dev); |
cb49a5e9 | 427 | |
16380c15 JS |
428 | if (!test_bit(HAS_ALARM, &ds1307->flags)) |
429 | return -ENOTTY; | |
cb49a5e9 | 430 | |
078f3f64 HK |
431 | return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL, |
432 | DS1337_BIT_A1IE, | |
433 | enabled ? DS1337_BIT_A1IE : 0); | |
cb49a5e9 RG |
434 | } |
435 | ||
d0e3f61b UKK |
436 | static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307, u32 ohms, bool diode) |
437 | { | |
438 | u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE : | |
439 | DS1307_TRICKLE_CHARGER_NO_DIODE; | |
682d73f6 | 440 | |
d0e3f61b UKK |
441 | switch (ohms) { |
442 | case 250: | |
443 | setup |= DS1307_TRICKLE_CHARGER_250_OHM; | |
444 | break; | |
445 | case 2000: | |
446 | setup |= DS1307_TRICKLE_CHARGER_2K_OHM; | |
447 | break; | |
448 | case 4000: | |
449 | setup |= DS1307_TRICKLE_CHARGER_4K_OHM; | |
450 | break; | |
451 | default: | |
452 | dev_warn(ds1307->dev, | |
453 | "Unsupported ohm value %u in dt\n", ohms); | |
454 | return 0; | |
455 | } | |
456 | return setup; | |
457 | } | |
ee0981be | 458 | |
ee0981be MV |
459 | static irqreturn_t rx8130_irq(int irq, void *dev_id) |
460 | { | |
461 | struct ds1307 *ds1307 = dev_id; | |
462 | struct mutex *lock = &ds1307->rtc->ops_lock; | |
463 | u8 ctl[3]; | |
464 | int ret; | |
465 | ||
466 | mutex_lock(lock); | |
467 | ||
468 | /* Read control registers. */ | |
f2b48012 AB |
469 | ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
470 | sizeof(ctl)); | |
ee0981be MV |
471 | if (ret < 0) |
472 | goto out; | |
473 | if (!(ctl[1] & RX8130_REG_FLAG_AF)) | |
474 | goto out; | |
475 | ctl[1] &= ~RX8130_REG_FLAG_AF; | |
476 | ctl[2] &= ~RX8130_REG_CONTROL0_AIE; | |
477 | ||
f2b48012 AB |
478 | ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
479 | sizeof(ctl)); | |
ee0981be MV |
480 | if (ret < 0) |
481 | goto out; | |
482 | ||
483 | rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF); | |
484 | ||
485 | out: | |
486 | mutex_unlock(lock); | |
487 | ||
488 | return IRQ_HANDLED; | |
489 | } | |
490 | ||
491 | static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t) | |
492 | { | |
493 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
494 | u8 ald[3], ctl[3]; | |
495 | int ret; | |
496 | ||
497 | if (!test_bit(HAS_ALARM, &ds1307->flags)) | |
498 | return -EINVAL; | |
499 | ||
500 | /* Read alarm registers. */ | |
f2b48012 AB |
501 | ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, |
502 | sizeof(ald)); | |
ee0981be MV |
503 | if (ret < 0) |
504 | return ret; | |
505 | ||
506 | /* Read control registers. */ | |
f2b48012 AB |
507 | ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
508 | sizeof(ctl)); | |
ee0981be MV |
509 | if (ret < 0) |
510 | return ret; | |
511 | ||
512 | t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE); | |
513 | t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF); | |
514 | ||
515 | /* Report alarm 0 time assuming 24-hour and day-of-month modes. */ | |
516 | t->time.tm_sec = -1; | |
517 | t->time.tm_min = bcd2bin(ald[0] & 0x7f); | |
518 | t->time.tm_hour = bcd2bin(ald[1] & 0x7f); | |
519 | t->time.tm_wday = -1; | |
520 | t->time.tm_mday = bcd2bin(ald[2] & 0x7f); | |
521 | t->time.tm_mon = -1; | |
522 | t->time.tm_year = -1; | |
523 | t->time.tm_yday = -1; | |
524 | t->time.tm_isdst = -1; | |
525 | ||
526 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n", | |
527 | __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour, | |
528 | t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled); | |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
533 | static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t) | |
534 | { | |
535 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
536 | u8 ald[3], ctl[3]; | |
537 | int ret; | |
538 | ||
539 | if (!test_bit(HAS_ALARM, &ds1307->flags)) | |
540 | return -EINVAL; | |
541 | ||
542 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d " | |
543 | "enabled=%d pending=%d\n", __func__, | |
544 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, | |
545 | t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, | |
546 | t->enabled, t->pending); | |
547 | ||
548 | /* Read control registers. */ | |
f2b48012 AB |
549 | ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
550 | sizeof(ctl)); | |
ee0981be MV |
551 | if (ret < 0) |
552 | return ret; | |
553 | ||
3f929cad UKK |
554 | ctl[0] &= RX8130_REG_EXTENSION_WADA; |
555 | ctl[1] &= ~RX8130_REG_FLAG_AF; | |
ee0981be MV |
556 | ctl[2] &= ~RX8130_REG_CONTROL0_AIE; |
557 | ||
f2b48012 AB |
558 | ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl, |
559 | sizeof(ctl)); | |
ee0981be MV |
560 | if (ret < 0) |
561 | return ret; | |
562 | ||
563 | /* Hardware alarm precision is 1 minute! */ | |
564 | ald[0] = bin2bcd(t->time.tm_min); | |
565 | ald[1] = bin2bcd(t->time.tm_hour); | |
566 | ald[2] = bin2bcd(t->time.tm_mday); | |
567 | ||
f2b48012 AB |
568 | ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald, |
569 | sizeof(ald)); | |
ee0981be MV |
570 | if (ret < 0) |
571 | return ret; | |
572 | ||
573 | if (!t->enabled) | |
574 | return 0; | |
575 | ||
576 | ctl[2] |= RX8130_REG_CONTROL0_AIE; | |
577 | ||
3f929cad | 578 | return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, ctl[2]); |
ee0981be MV |
579 | } |
580 | ||
d0e3f61b UKK |
581 | static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled) |
582 | { | |
583 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
584 | int ret, reg; | |
585 | ||
586 | if (!test_bit(HAS_ALARM, &ds1307->flags)) | |
587 | return -EINVAL; | |
588 | ||
589 | ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, ®); | |
590 | if (ret < 0) | |
591 | return ret; | |
592 | ||
593 | if (enabled) | |
594 | reg |= RX8130_REG_CONTROL0_AIE; | |
595 | else | |
596 | reg &= ~RX8130_REG_CONTROL0_AIE; | |
597 | ||
598 | return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg); | |
599 | } | |
600 | ||
601 | static irqreturn_t mcp794xx_irq(int irq, void *dev_id) | |
602 | { | |
603 | struct ds1307 *ds1307 = dev_id; | |
604 | struct mutex *lock = &ds1307->rtc->ops_lock; | |
605 | int reg, ret; | |
606 | ||
607 | mutex_lock(lock); | |
608 | ||
609 | /* Check and clear alarm 0 interrupt flag. */ | |
610 | ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, ®); | |
611 | if (ret) | |
612 | goto out; | |
613 | if (!(reg & MCP794XX_BIT_ALMX_IF)) | |
614 | goto out; | |
615 | reg &= ~MCP794XX_BIT_ALMX_IF; | |
616 | ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg); | |
617 | if (ret) | |
618 | goto out; | |
619 | ||
620 | /* Disable alarm 0. */ | |
621 | ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL, | |
622 | MCP794XX_BIT_ALM0_EN, 0); | |
623 | if (ret) | |
624 | goto out; | |
625 | ||
626 | rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF); | |
627 | ||
628 | out: | |
629 | mutex_unlock(lock); | |
630 | ||
631 | return IRQ_HANDLED; | |
632 | } | |
633 | ||
634 | static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t) | |
635 | { | |
636 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
637 | u8 regs[10]; | |
638 | int ret; | |
639 | ||
640 | if (!test_bit(HAS_ALARM, &ds1307->flags)) | |
641 | return -EINVAL; | |
642 | ||
643 | /* Read control and alarm 0 registers. */ | |
644 | ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, | |
645 | sizeof(regs)); | |
646 | if (ret) | |
647 | return ret; | |
648 | ||
649 | t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN); | |
650 | ||
651 | /* Report alarm 0 time assuming 24-hour and day-of-month modes. */ | |
652 | t->time.tm_sec = bcd2bin(regs[3] & 0x7f); | |
653 | t->time.tm_min = bcd2bin(regs[4] & 0x7f); | |
654 | t->time.tm_hour = bcd2bin(regs[5] & 0x3f); | |
655 | t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1; | |
656 | t->time.tm_mday = bcd2bin(regs[7] & 0x3f); | |
657 | t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1; | |
658 | t->time.tm_year = -1; | |
659 | t->time.tm_yday = -1; | |
660 | t->time.tm_isdst = -1; | |
661 | ||
662 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d " | |
663 | "enabled=%d polarity=%d irq=%d match=%lu\n", __func__, | |
664 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, | |
665 | t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled, | |
666 | !!(regs[6] & MCP794XX_BIT_ALMX_POL), | |
667 | !!(regs[6] & MCP794XX_BIT_ALMX_IF), | |
668 | (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4); | |
669 | ||
670 | return 0; | |
671 | } | |
672 | ||
673 | /* | |
674 | * We may have a random RTC weekday, therefore calculate alarm weekday based | |
675 | * on current weekday we read from the RTC timekeeping regs | |
676 | */ | |
677 | static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm) | |
678 | { | |
679 | struct rtc_time tm_now; | |
680 | int days_now, days_alarm, ret; | |
681 | ||
682 | ret = ds1307_get_time(dev, &tm_now); | |
683 | if (ret) | |
684 | return ret; | |
685 | ||
686 | days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60); | |
687 | days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60); | |
688 | ||
689 | return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1; | |
690 | } | |
691 | ||
692 | static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t) | |
693 | { | |
694 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
695 | unsigned char regs[10]; | |
696 | int wday, ret; | |
697 | ||
698 | if (!test_bit(HAS_ALARM, &ds1307->flags)) | |
699 | return -EINVAL; | |
700 | ||
701 | wday = mcp794xx_alm_weekday(dev, &t->time); | |
702 | if (wday < 0) | |
703 | return wday; | |
704 | ||
705 | dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d " | |
706 | "enabled=%d pending=%d\n", __func__, | |
707 | t->time.tm_sec, t->time.tm_min, t->time.tm_hour, | |
708 | t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, | |
709 | t->enabled, t->pending); | |
710 | ||
711 | /* Read control and alarm 0 registers. */ | |
712 | ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs, | |
713 | sizeof(regs)); | |
714 | if (ret) | |
715 | return ret; | |
716 | ||
717 | /* Set alarm 0, using 24-hour and day-of-month modes. */ | |
718 | regs[3] = bin2bcd(t->time.tm_sec); | |
719 | regs[4] = bin2bcd(t->time.tm_min); | |
720 | regs[5] = bin2bcd(t->time.tm_hour); | |
721 | regs[6] = wday; | |
722 | regs[7] = bin2bcd(t->time.tm_mday); | |
723 | regs[8] = bin2bcd(t->time.tm_mon + 1); | |
724 | ||
725 | /* Clear the alarm 0 interrupt flag. */ | |
726 | regs[6] &= ~MCP794XX_BIT_ALMX_IF; | |
727 | /* Set alarm match: second, minute, hour, day, date, month. */ | |
728 | regs[6] |= MCP794XX_MSK_ALMX_MATCH; | |
729 | /* Disable interrupt. We will not enable until completely programmed */ | |
730 | regs[0] &= ~MCP794XX_BIT_ALM0_EN; | |
731 | ||
732 | ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs, | |
733 | sizeof(regs)); | |
734 | if (ret) | |
735 | return ret; | |
736 | ||
737 | if (!t->enabled) | |
738 | return 0; | |
739 | regs[0] |= MCP794XX_BIT_ALM0_EN; | |
740 | return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]); | |
741 | } | |
742 | ||
743 | static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled) | |
ee0981be MV |
744 | { |
745 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
ee0981be MV |
746 | |
747 | if (!test_bit(HAS_ALARM, &ds1307->flags)) | |
748 | return -EINVAL; | |
749 | ||
d0e3f61b UKK |
750 | return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL, |
751 | MCP794XX_BIT_ALM0_EN, | |
752 | enabled ? MCP794XX_BIT_ALM0_EN : 0); | |
753 | } | |
ee0981be | 754 | |
d0e3f61b UKK |
755 | static int m41txx_rtc_read_offset(struct device *dev, long *offset) |
756 | { | |
757 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
758 | unsigned int ctrl_reg; | |
759 | u8 val; | |
760 | ||
761 | regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg); | |
762 | ||
763 | val = ctrl_reg & M41TXX_M_CALIBRATION; | |
764 | ||
765 | /* check if positive */ | |
766 | if (ctrl_reg & M41TXX_BIT_CALIB_SIGN) | |
767 | *offset = (val * M41TXX_POS_OFFSET_STEP_PPB); | |
ee0981be | 768 | else |
d0e3f61b | 769 | *offset = -(val * M41TXX_NEG_OFFSET_STEP_PPB); |
ee0981be | 770 | |
d0e3f61b UKK |
771 | return 0; |
772 | } | |
773 | ||
774 | static int m41txx_rtc_set_offset(struct device *dev, long offset) | |
775 | { | |
776 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
777 | unsigned int ctrl_reg; | |
778 | ||
779 | if ((offset < M41TXX_MIN_OFFSET) || (offset > M41TXX_MAX_OFFSET)) | |
780 | return -ERANGE; | |
781 | ||
782 | if (offset >= 0) { | |
783 | ctrl_reg = DIV_ROUND_CLOSEST(offset, | |
784 | M41TXX_POS_OFFSET_STEP_PPB); | |
785 | ctrl_reg |= M41TXX_BIT_CALIB_SIGN; | |
786 | } else { | |
787 | ctrl_reg = DIV_ROUND_CLOSEST(abs(offset), | |
788 | M41TXX_NEG_OFFSET_STEP_PPB); | |
789 | } | |
790 | ||
791 | return regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, | |
792 | M41TXX_M_CALIBRATION | M41TXX_BIT_CALIB_SIGN, | |
793 | ctrl_reg); | |
ee0981be MV |
794 | } |
795 | ||
d0e3f61b UKK |
796 | static const struct rtc_class_ops rx8130_rtc_ops = { |
797 | .read_time = ds1307_get_time, | |
798 | .set_time = ds1307_set_time, | |
799 | .read_alarm = rx8130_read_alarm, | |
800 | .set_alarm = rx8130_set_alarm, | |
801 | .alarm_irq_enable = rx8130_alarm_irq_enable, | |
802 | }; | |
803 | ||
804 | static const struct rtc_class_ops mcp794xx_rtc_ops = { | |
805 | .read_time = ds1307_get_time, | |
806 | .set_time = ds1307_set_time, | |
807 | .read_alarm = mcp794xx_read_alarm, | |
808 | .set_alarm = mcp794xx_set_alarm, | |
809 | .alarm_irq_enable = mcp794xx_alarm_irq_enable, | |
810 | }; | |
811 | ||
812 | static const struct rtc_class_ops m41txx_rtc_ops = { | |
813 | .read_time = ds1307_get_time, | |
814 | .set_time = ds1307_set_time, | |
815 | .read_alarm = ds1337_read_alarm, | |
816 | .set_alarm = ds1337_set_alarm, | |
817 | .alarm_irq_enable = ds1307_alarm_irq_enable, | |
818 | .read_offset = m41txx_rtc_read_offset, | |
819 | .set_offset = m41txx_rtc_set_offset, | |
820 | }; | |
821 | ||
822 | static const struct chip_desc chips[last_ds_type] = { | |
823 | [ds_1307] = { | |
824 | .nvram_offset = 8, | |
825 | .nvram_size = 56, | |
826 | }, | |
827 | [ds_1308] = { | |
828 | .nvram_offset = 8, | |
829 | .nvram_size = 56, | |
830 | }, | |
831 | [ds_1337] = { | |
832 | .alarm = 1, | |
833 | .century_reg = DS1307_REG_MONTH, | |
834 | .century_bit = DS1337_BIT_CENTURY, | |
835 | }, | |
836 | [ds_1338] = { | |
837 | .nvram_offset = 8, | |
838 | .nvram_size = 56, | |
839 | }, | |
840 | [ds_1339] = { | |
841 | .alarm = 1, | |
842 | .century_reg = DS1307_REG_MONTH, | |
843 | .century_bit = DS1337_BIT_CENTURY, | |
844 | .bbsqi_bit = DS1339_BIT_BBSQI, | |
845 | .trickle_charger_reg = 0x10, | |
846 | .do_trickle_setup = &do_trickle_setup_ds1339, | |
847 | }, | |
848 | [ds_1340] = { | |
849 | .century_reg = DS1307_REG_HOUR, | |
850 | .century_enable_bit = DS1340_BIT_CENTURY_EN, | |
851 | .century_bit = DS1340_BIT_CENTURY, | |
852 | .do_trickle_setup = &do_trickle_setup_ds1339, | |
853 | .trickle_charger_reg = 0x08, | |
854 | }, | |
855 | [ds_1341] = { | |
856 | .century_reg = DS1307_REG_MONTH, | |
857 | .century_bit = DS1337_BIT_CENTURY, | |
858 | }, | |
859 | [ds_1388] = { | |
860 | .offset = 1, | |
861 | .trickle_charger_reg = 0x0a, | |
862 | }, | |
863 | [ds_3231] = { | |
864 | .alarm = 1, | |
865 | .century_reg = DS1307_REG_MONTH, | |
866 | .century_bit = DS1337_BIT_CENTURY, | |
867 | .bbsqi_bit = DS3231_BIT_BBSQW, | |
868 | }, | |
869 | [rx_8130] = { | |
870 | .alarm = 1, | |
871 | /* this is battery backed SRAM */ | |
872 | .nvram_offset = 0x20, | |
873 | .nvram_size = 4, /* 32bit (4 word x 8 bit) */ | |
874 | .offset = 0x10, | |
875 | .irq_handler = rx8130_irq, | |
876 | .rtc_ops = &rx8130_rtc_ops, | |
877 | }, | |
878 | [m41t0] = { | |
879 | .rtc_ops = &m41txx_rtc_ops, | |
880 | }, | |
881 | [m41t00] = { | |
882 | .rtc_ops = &m41txx_rtc_ops, | |
883 | }, | |
884 | [m41t11] = { | |
885 | /* this is battery backed SRAM */ | |
886 | .nvram_offset = 8, | |
887 | .nvram_size = 56, | |
888 | .rtc_ops = &m41txx_rtc_ops, | |
889 | }, | |
890 | [mcp794xx] = { | |
891 | .alarm = 1, | |
892 | /* this is battery backed SRAM */ | |
893 | .nvram_offset = 0x20, | |
894 | .nvram_size = 0x40, | |
895 | .irq_handler = mcp794xx_irq, | |
896 | .rtc_ops = &mcp794xx_rtc_ops, | |
897 | }, | |
898 | }; | |
899 | ||
900 | static const struct i2c_device_id ds1307_id[] = { | |
901 | { "ds1307", ds_1307 }, | |
902 | { "ds1308", ds_1308 }, | |
903 | { "ds1337", ds_1337 }, | |
904 | { "ds1338", ds_1338 }, | |
905 | { "ds1339", ds_1339 }, | |
906 | { "ds1388", ds_1388 }, | |
907 | { "ds1340", ds_1340 }, | |
908 | { "ds1341", ds_1341 }, | |
909 | { "ds3231", ds_3231 }, | |
910 | { "m41t0", m41t0 }, | |
911 | { "m41t00", m41t00 }, | |
912 | { "m41t11", m41t11 }, | |
913 | { "mcp7940x", mcp794xx }, | |
914 | { "mcp7941x", mcp794xx }, | |
915 | { "pt7c4338", ds_1307 }, | |
916 | { "rx8025", rx_8025 }, | |
917 | { "isl12057", ds_1337 }, | |
918 | { "rx8130", rx_8130 }, | |
919 | { } | |
920 | }; | |
921 | MODULE_DEVICE_TABLE(i2c, ds1307_id); | |
922 | ||
923 | #ifdef CONFIG_OF | |
924 | static const struct of_device_id ds1307_of_match[] = { | |
925 | { | |
926 | .compatible = "dallas,ds1307", | |
927 | .data = (void *)ds_1307 | |
928 | }, | |
929 | { | |
930 | .compatible = "dallas,ds1308", | |
931 | .data = (void *)ds_1308 | |
932 | }, | |
933 | { | |
934 | .compatible = "dallas,ds1337", | |
935 | .data = (void *)ds_1337 | |
936 | }, | |
937 | { | |
938 | .compatible = "dallas,ds1338", | |
939 | .data = (void *)ds_1338 | |
940 | }, | |
941 | { | |
942 | .compatible = "dallas,ds1339", | |
943 | .data = (void *)ds_1339 | |
944 | }, | |
945 | { | |
946 | .compatible = "dallas,ds1388", | |
947 | .data = (void *)ds_1388 | |
948 | }, | |
949 | { | |
950 | .compatible = "dallas,ds1340", | |
951 | .data = (void *)ds_1340 | |
952 | }, | |
953 | { | |
954 | .compatible = "dallas,ds1341", | |
955 | .data = (void *)ds_1341 | |
956 | }, | |
957 | { | |
958 | .compatible = "maxim,ds3231", | |
959 | .data = (void *)ds_3231 | |
960 | }, | |
961 | { | |
962 | .compatible = "st,m41t0", | |
963 | .data = (void *)m41t0 | |
964 | }, | |
965 | { | |
966 | .compatible = "st,m41t00", | |
967 | .data = (void *)m41t00 | |
968 | }, | |
969 | { | |
970 | .compatible = "st,m41t11", | |
971 | .data = (void *)m41t11 | |
972 | }, | |
973 | { | |
974 | .compatible = "microchip,mcp7940x", | |
975 | .data = (void *)mcp794xx | |
976 | }, | |
977 | { | |
978 | .compatible = "microchip,mcp7941x", | |
979 | .data = (void *)mcp794xx | |
980 | }, | |
981 | { | |
982 | .compatible = "pericom,pt7c4338", | |
983 | .data = (void *)ds_1307 | |
984 | }, | |
985 | { | |
986 | .compatible = "epson,rx8025", | |
987 | .data = (void *)rx_8025 | |
988 | }, | |
989 | { | |
990 | .compatible = "isil,isl12057", | |
991 | .data = (void *)ds_1337 | |
992 | }, | |
993 | { | |
994 | .compatible = "epson,rx8130", | |
995 | .data = (void *)rx_8130 | |
996 | }, | |
997 | { } | |
998 | }; | |
999 | MODULE_DEVICE_TABLE(of, ds1307_of_match); | |
1000 | #endif | |
1001 | ||
1002 | #ifdef CONFIG_ACPI | |
1003 | static const struct acpi_device_id ds1307_acpi_ids[] = { | |
1004 | { .id = "DS1307", .driver_data = ds_1307 }, | |
1005 | { .id = "DS1308", .driver_data = ds_1308 }, | |
1006 | { .id = "DS1337", .driver_data = ds_1337 }, | |
1007 | { .id = "DS1338", .driver_data = ds_1338 }, | |
1008 | { .id = "DS1339", .driver_data = ds_1339 }, | |
1009 | { .id = "DS1388", .driver_data = ds_1388 }, | |
1010 | { .id = "DS1340", .driver_data = ds_1340 }, | |
1011 | { .id = "DS1341", .driver_data = ds_1341 }, | |
1012 | { .id = "DS3231", .driver_data = ds_3231 }, | |
1013 | { .id = "M41T0", .driver_data = m41t0 }, | |
1014 | { .id = "M41T00", .driver_data = m41t00 }, | |
1015 | { .id = "M41T11", .driver_data = m41t11 }, | |
1016 | { .id = "MCP7940X", .driver_data = mcp794xx }, | |
1017 | { .id = "MCP7941X", .driver_data = mcp794xx }, | |
1018 | { .id = "PT7C4338", .driver_data = ds_1307 }, | |
1019 | { .id = "RX8025", .driver_data = rx_8025 }, | |
1020 | { .id = "ISL12057", .driver_data = ds_1337 }, | |
1021 | { .id = "RX8130", .driver_data = rx_8130 }, | |
1022 | { } | |
1023 | }; | |
1024 | MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids); | |
1025 | #endif | |
ee0981be | 1026 | |
1d1945d2 | 1027 | /* |
d0e3f61b UKK |
1028 | * The ds1337 and ds1339 both have two alarms, but we only use the first |
1029 | * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm | |
1030 | * signal; ds1339 chips have only one alarm signal. | |
1d1945d2 | 1031 | */ |
d0e3f61b | 1032 | static irqreturn_t ds1307_irq(int irq, void *dev_id) |
1d1945d2 | 1033 | { |
d0e3f61b UKK |
1034 | struct ds1307 *ds1307 = dev_id; |
1035 | struct mutex *lock = &ds1307->rtc->ops_lock; | |
1036 | int stat, ret; | |
1d1945d2 | 1037 | |
2fb07a10 | 1038 | mutex_lock(lock); |
d0e3f61b | 1039 | ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat); |
11e5890b | 1040 | if (ret) |
1d1945d2 SG |
1041 | goto out; |
1042 | ||
d0e3f61b UKK |
1043 | if (stat & DS1337_BIT_A1I) { |
1044 | stat &= ~DS1337_BIT_A1I; | |
1045 | regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat); | |
1d1945d2 | 1046 | |
d0e3f61b UKK |
1047 | ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL, |
1048 | DS1337_BIT_A1IE, 0); | |
1049 | if (ret) | |
1050 | goto out; | |
1051 | ||
1052 | rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF); | |
1053 | } | |
1d1945d2 SG |
1054 | |
1055 | out: | |
2fb07a10 FB |
1056 | mutex_unlock(lock); |
1057 | ||
1058 | return IRQ_HANDLED; | |
1d1945d2 SG |
1059 | } |
1060 | ||
d0e3f61b | 1061 | /*----------------------------------------------------------------------*/ |
79230ff6 | 1062 | |
d0e3f61b UKK |
1063 | static const struct rtc_class_ops ds13xx_rtc_ops = { |
1064 | .read_time = ds1307_get_time, | |
1065 | .set_time = ds1307_set_time, | |
1066 | .read_alarm = ds1337_read_alarm, | |
1067 | .set_alarm = ds1337_set_alarm, | |
1068 | .alarm_irq_enable = ds1307_alarm_irq_enable, | |
1069 | }; | |
79230ff6 | 1070 | |
6a5f2a1f AB |
1071 | static ssize_t frequency_test_store(struct device *dev, |
1072 | struct device_attribute *attr, | |
1073 | const char *buf, size_t count) | |
b41c23e1 | 1074 | { |
6a5f2a1f | 1075 | struct ds1307 *ds1307 = dev_get_drvdata(dev->parent); |
b41c23e1 GB |
1076 | bool freq_test_en; |
1077 | int ret; | |
1078 | ||
1079 | ret = kstrtobool(buf, &freq_test_en); | |
1080 | if (ret) { | |
1081 | dev_err(dev, "Failed to store RTC Frequency Test attribute\n"); | |
1082 | return ret; | |
1083 | } | |
1084 | ||
1085 | regmap_update_bits(ds1307->regmap, M41TXX_REG_CONTROL, M41TXX_BIT_FT, | |
1086 | freq_test_en ? M41TXX_BIT_FT : 0); | |
1087 | ||
1088 | return count; | |
1089 | } | |
1090 | ||
6a5f2a1f AB |
1091 | static ssize_t frequency_test_show(struct device *dev, |
1092 | struct device_attribute *attr, | |
1093 | char *buf) | |
b41c23e1 | 1094 | { |
6a5f2a1f | 1095 | struct ds1307 *ds1307 = dev_get_drvdata(dev->parent); |
b41c23e1 GB |
1096 | unsigned int ctrl_reg; |
1097 | ||
1098 | regmap_read(ds1307->regmap, M41TXX_REG_CONTROL, &ctrl_reg); | |
1099 | ||
1100 | return scnprintf(buf, PAGE_SIZE, (ctrl_reg & M41TXX_BIT_FT) ? "on\n" : | |
1101 | "off\n"); | |
1102 | } | |
1103 | ||
6a5f2a1f | 1104 | static DEVICE_ATTR_RW(frequency_test); |
b41c23e1 GB |
1105 | |
1106 | static struct attribute *rtc_freq_test_attrs[] = { | |
6a5f2a1f | 1107 | &dev_attr_frequency_test.attr, |
b41c23e1 GB |
1108 | NULL, |
1109 | }; | |
1110 | ||
1111 | static const struct attribute_group rtc_freq_test_attr_group = { | |
1112 | .attrs = rtc_freq_test_attrs, | |
1113 | }; | |
1114 | ||
b41c23e1 GB |
1115 | static int ds1307_add_frequency_test(struct ds1307 *ds1307) |
1116 | { | |
1117 | int err; | |
1118 | ||
1119 | switch (ds1307->type) { | |
1120 | case m41t0: | |
1121 | case m41t00: | |
1122 | case m41t11: | |
6a5f2a1f AB |
1123 | err = rtc_add_group(ds1307->rtc, &rtc_freq_test_attr_group); |
1124 | if (err) | |
b41c23e1 | 1125 | return err; |
b41c23e1 GB |
1126 | break; |
1127 | default: | |
1128 | break; | |
1129 | } | |
1130 | ||
1131 | return 0; | |
1132 | } | |
1133 | ||
1d1945d2 SG |
1134 | /*----------------------------------------------------------------------*/ |
1135 | ||
abc925f7 AB |
1136 | static int ds1307_nvram_read(void *priv, unsigned int offset, void *val, |
1137 | size_t bytes) | |
682d73f6 | 1138 | { |
abc925f7 | 1139 | struct ds1307 *ds1307 = priv; |
969fa07b | 1140 | const struct chip_desc *chip = &chips[ds1307->type]; |
682d73f6 | 1141 | |
969fa07b | 1142 | return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset, |
abc925f7 | 1143 | val, bytes); |
682d73f6 DB |
1144 | } |
1145 | ||
abc925f7 AB |
1146 | static int ds1307_nvram_write(void *priv, unsigned int offset, void *val, |
1147 | size_t bytes) | |
682d73f6 | 1148 | { |
abc925f7 | 1149 | struct ds1307 *ds1307 = priv; |
969fa07b | 1150 | const struct chip_desc *chip = &chips[ds1307->type]; |
682d73f6 | 1151 | |
969fa07b | 1152 | return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset, |
abc925f7 | 1153 | val, bytes); |
682d73f6 DB |
1154 | } |
1155 | ||
682d73f6 DB |
1156 | /*----------------------------------------------------------------------*/ |
1157 | ||
d8490fd5 | 1158 | static u8 ds1307_trickle_init(struct ds1307 *ds1307, |
7624df48 | 1159 | const struct chip_desc *chip) |
33b04b7b | 1160 | { |
57ec2d95 | 1161 | u32 ohms; |
33b04b7b MV |
1162 | bool diode = true; |
1163 | ||
1164 | if (!chip->do_trickle_setup) | |
d8490fd5 HK |
1165 | return 0; |
1166 | ||
11e5890b HK |
1167 | if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms", |
1168 | &ohms)) | |
d8490fd5 HK |
1169 | return 0; |
1170 | ||
11e5890b | 1171 | if (device_property_read_bool(ds1307->dev, "trickle-diode-disable")) |
33b04b7b | 1172 | diode = false; |
d8490fd5 HK |
1173 | |
1174 | return chip->do_trickle_setup(ds1307, ohms, diode); | |
33b04b7b MV |
1175 | } |
1176 | ||
445c0207 AM |
1177 | /*----------------------------------------------------------------------*/ |
1178 | ||
6b583a64 | 1179 | #if IS_REACHABLE(CONFIG_HWMON) |
445c0207 AM |
1180 | |
1181 | /* | |
1182 | * Temperature sensor support for ds3231 devices. | |
1183 | */ | |
1184 | ||
1185 | #define DS3231_REG_TEMPERATURE 0x11 | |
1186 | ||
1187 | /* | |
1188 | * A user-initiated temperature conversion is not started by this function, | |
1189 | * so the temperature is updated once every 64 seconds. | |
1190 | */ | |
9a3dce62 | 1191 | static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC) |
445c0207 AM |
1192 | { |
1193 | struct ds1307 *ds1307 = dev_get_drvdata(dev); | |
1194 | u8 temp_buf[2]; | |
1195 | s16 temp; | |
1196 | int ret; | |
1197 | ||
11e5890b HK |
1198 | ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE, |
1199 | temp_buf, sizeof(temp_buf)); | |
1200 | if (ret) | |
445c0207 | 1201 | return ret; |
445c0207 AM |
1202 | /* |
1203 | * Temperature is represented as a 10-bit code with a resolution of | |
1204 | * 0.25 degree celsius and encoded in two's complement format. | |
1205 | */ | |
1206 | temp = (temp_buf[0] << 8) | temp_buf[1]; | |
1207 | temp >>= 6; | |
1208 | *mC = temp * 250; | |
1209 | ||
1210 | return 0; | |
1211 | } | |
1212 | ||
1213 | static ssize_t ds3231_hwmon_show_temp(struct device *dev, | |
4057a66e | 1214 | struct device_attribute *attr, char *buf) |
445c0207 AM |
1215 | { |
1216 | int ret; | |
9a3dce62 | 1217 | s32 temp; |
445c0207 AM |
1218 | |
1219 | ret = ds3231_hwmon_read_temp(dev, &temp); | |
1220 | if (ret) | |
1221 | return ret; | |
1222 | ||
1223 | return sprintf(buf, "%d\n", temp); | |
1224 | } | |
b4be271c | 1225 | static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp, |
4057a66e | 1226 | NULL, 0); |
445c0207 AM |
1227 | |
1228 | static struct attribute *ds3231_hwmon_attrs[] = { | |
1229 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
1230 | NULL, | |
1231 | }; | |
1232 | ATTRIBUTE_GROUPS(ds3231_hwmon); | |
1233 | ||
1234 | static void ds1307_hwmon_register(struct ds1307 *ds1307) | |
1235 | { | |
1236 | struct device *dev; | |
1237 | ||
1238 | if (ds1307->type != ds_3231) | |
1239 | return; | |
1240 | ||
11e5890b | 1241 | dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name, |
4057a66e AB |
1242 | ds1307, |
1243 | ds3231_hwmon_groups); | |
445c0207 | 1244 | if (IS_ERR(dev)) { |
11e5890b HK |
1245 | dev_warn(ds1307->dev, "unable to register hwmon device %ld\n", |
1246 | PTR_ERR(dev)); | |
445c0207 AM |
1247 | } |
1248 | } | |
1249 | ||
1250 | #else | |
1251 | ||
1252 | static void ds1307_hwmon_register(struct ds1307 *ds1307) | |
1253 | { | |
1254 | } | |
1255 | ||
6c6ff145 AM |
1256 | #endif /* CONFIG_RTC_DRV_DS1307_HWMON */ |
1257 | ||
1258 | /*----------------------------------------------------------------------*/ | |
1259 | ||
1260 | /* | |
1261 | * Square-wave output support for DS3231 | |
1262 | * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf | |
1263 | */ | |
1264 | #ifdef CONFIG_COMMON_CLK | |
1265 | ||
1266 | enum { | |
1267 | DS3231_CLK_SQW = 0, | |
1268 | DS3231_CLK_32KHZ, | |
1269 | }; | |
1270 | ||
1271 | #define clk_sqw_to_ds1307(clk) \ | |
1272 | container_of(clk, struct ds1307, clks[DS3231_CLK_SQW]) | |
1273 | #define clk_32khz_to_ds1307(clk) \ | |
1274 | container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ]) | |
1275 | ||
1276 | static int ds3231_clk_sqw_rates[] = { | |
1277 | 1, | |
1278 | 1024, | |
1279 | 4096, | |
1280 | 8192, | |
1281 | }; | |
1282 | ||
1283 | static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value) | |
1284 | { | |
6c6ff145 | 1285 | struct mutex *lock = &ds1307->rtc->ops_lock; |
6c6ff145 AM |
1286 | int ret; |
1287 | ||
1288 | mutex_lock(lock); | |
078f3f64 HK |
1289 | ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL, |
1290 | mask, value); | |
6c6ff145 AM |
1291 | mutex_unlock(lock); |
1292 | ||
1293 | return ret; | |
1294 | } | |
1295 | ||
1296 | static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw, | |
1297 | unsigned long parent_rate) | |
1298 | { | |
1299 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); | |
11e5890b | 1300 | int control, ret; |
6c6ff145 AM |
1301 | int rate_sel = 0; |
1302 | ||
11e5890b HK |
1303 | ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control); |
1304 | if (ret) | |
1305 | return ret; | |
6c6ff145 AM |
1306 | if (control & DS1337_BIT_RS1) |
1307 | rate_sel += 1; | |
1308 | if (control & DS1337_BIT_RS2) | |
1309 | rate_sel += 2; | |
1310 | ||
1311 | return ds3231_clk_sqw_rates[rate_sel]; | |
1312 | } | |
1313 | ||
1314 | static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate, | |
4057a66e | 1315 | unsigned long *prate) |
6c6ff145 AM |
1316 | { |
1317 | int i; | |
1318 | ||
1319 | for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) { | |
1320 | if (ds3231_clk_sqw_rates[i] <= rate) | |
1321 | return ds3231_clk_sqw_rates[i]; | |
1322 | } | |
1323 | ||
1324 | return 0; | |
1325 | } | |
1326 | ||
1327 | static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate, | |
4057a66e | 1328 | unsigned long parent_rate) |
6c6ff145 AM |
1329 | { |
1330 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); | |
1331 | int control = 0; | |
1332 | int rate_sel; | |
1333 | ||
1334 | for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates); | |
1335 | rate_sel++) { | |
1336 | if (ds3231_clk_sqw_rates[rate_sel] == rate) | |
1337 | break; | |
1338 | } | |
1339 | ||
1340 | if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates)) | |
1341 | return -EINVAL; | |
1342 | ||
1343 | if (rate_sel & 1) | |
1344 | control |= DS1337_BIT_RS1; | |
1345 | if (rate_sel & 2) | |
1346 | control |= DS1337_BIT_RS2; | |
1347 | ||
1348 | return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2, | |
1349 | control); | |
1350 | } | |
1351 | ||
1352 | static int ds3231_clk_sqw_prepare(struct clk_hw *hw) | |
1353 | { | |
1354 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); | |
1355 | ||
1356 | return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0); | |
1357 | } | |
1358 | ||
1359 | static void ds3231_clk_sqw_unprepare(struct clk_hw *hw) | |
1360 | { | |
1361 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); | |
1362 | ||
1363 | ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN); | |
1364 | } | |
1365 | ||
1366 | static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw) | |
1367 | { | |
1368 | struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw); | |
11e5890b | 1369 | int control, ret; |
6c6ff145 | 1370 | |
11e5890b HK |
1371 | ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control); |
1372 | if (ret) | |
1373 | return ret; | |
6c6ff145 AM |
1374 | |
1375 | return !(control & DS1337_BIT_INTCN); | |
1376 | } | |
1377 | ||
1378 | static const struct clk_ops ds3231_clk_sqw_ops = { | |
1379 | .prepare = ds3231_clk_sqw_prepare, | |
1380 | .unprepare = ds3231_clk_sqw_unprepare, | |
1381 | .is_prepared = ds3231_clk_sqw_is_prepared, | |
1382 | .recalc_rate = ds3231_clk_sqw_recalc_rate, | |
1383 | .round_rate = ds3231_clk_sqw_round_rate, | |
1384 | .set_rate = ds3231_clk_sqw_set_rate, | |
1385 | }; | |
1386 | ||
1387 | static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw, | |
4057a66e | 1388 | unsigned long parent_rate) |
6c6ff145 AM |
1389 | { |
1390 | return 32768; | |
1391 | } | |
1392 | ||
1393 | static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable) | |
1394 | { | |
6c6ff145 | 1395 | struct mutex *lock = &ds1307->rtc->ops_lock; |
6c6ff145 AM |
1396 | int ret; |
1397 | ||
1398 | mutex_lock(lock); | |
078f3f64 HK |
1399 | ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS, |
1400 | DS3231_BIT_EN32KHZ, | |
1401 | enable ? DS3231_BIT_EN32KHZ : 0); | |
6c6ff145 AM |
1402 | mutex_unlock(lock); |
1403 | ||
1404 | return ret; | |
1405 | } | |
1406 | ||
1407 | static int ds3231_clk_32khz_prepare(struct clk_hw *hw) | |
1408 | { | |
1409 | struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw); | |
1410 | ||
1411 | return ds3231_clk_32khz_control(ds1307, true); | |
1412 | } | |
1413 | ||
1414 | static void ds3231_clk_32khz_unprepare(struct clk_hw *hw) | |
1415 | { | |
1416 | struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw); | |
1417 | ||
1418 | ds3231_clk_32khz_control(ds1307, false); | |
1419 | } | |
1420 | ||
1421 | static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw) | |
1422 | { | |
1423 | struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw); | |
11e5890b | 1424 | int status, ret; |
6c6ff145 | 1425 | |
11e5890b HK |
1426 | ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status); |
1427 | if (ret) | |
1428 | return ret; | |
6c6ff145 AM |
1429 | |
1430 | return !!(status & DS3231_BIT_EN32KHZ); | |
1431 | } | |
1432 | ||
1433 | static const struct clk_ops ds3231_clk_32khz_ops = { | |
1434 | .prepare = ds3231_clk_32khz_prepare, | |
1435 | .unprepare = ds3231_clk_32khz_unprepare, | |
1436 | .is_prepared = ds3231_clk_32khz_is_prepared, | |
1437 | .recalc_rate = ds3231_clk_32khz_recalc_rate, | |
1438 | }; | |
1439 | ||
1440 | static struct clk_init_data ds3231_clks_init[] = { | |
1441 | [DS3231_CLK_SQW] = { | |
1442 | .name = "ds3231_clk_sqw", | |
1443 | .ops = &ds3231_clk_sqw_ops, | |
6c6ff145 AM |
1444 | }, |
1445 | [DS3231_CLK_32KHZ] = { | |
1446 | .name = "ds3231_clk_32khz", | |
1447 | .ops = &ds3231_clk_32khz_ops, | |
6c6ff145 AM |
1448 | }, |
1449 | }; | |
1450 | ||
1451 | static int ds3231_clks_register(struct ds1307 *ds1307) | |
1452 | { | |
11e5890b | 1453 | struct device_node *node = ds1307->dev->of_node; |
6c6ff145 AM |
1454 | struct clk_onecell_data *onecell; |
1455 | int i; | |
1456 | ||
11e5890b | 1457 | onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL); |
6c6ff145 AM |
1458 | if (!onecell) |
1459 | return -ENOMEM; | |
1460 | ||
1461 | onecell->clk_num = ARRAY_SIZE(ds3231_clks_init); | |
11e5890b HK |
1462 | onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num, |
1463 | sizeof(onecell->clks[0]), GFP_KERNEL); | |
6c6ff145 AM |
1464 | if (!onecell->clks) |
1465 | return -ENOMEM; | |
1466 | ||
1467 | for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) { | |
1468 | struct clk_init_data init = ds3231_clks_init[i]; | |
1469 | ||
1470 | /* | |
1471 | * Interrupt signal due to alarm conditions and square-wave | |
1472 | * output share same pin, so don't initialize both. | |
1473 | */ | |
1474 | if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags)) | |
1475 | continue; | |
1476 | ||
1477 | /* optional override of the clockname */ | |
1478 | of_property_read_string_index(node, "clock-output-names", i, | |
4057a66e | 1479 | &init.name); |
6c6ff145 AM |
1480 | ds1307->clks[i].init = &init; |
1481 | ||
11e5890b HK |
1482 | onecell->clks[i] = devm_clk_register(ds1307->dev, |
1483 | &ds1307->clks[i]); | |
6c6ff145 AM |
1484 | if (IS_ERR(onecell->clks[i])) |
1485 | return PTR_ERR(onecell->clks[i]); | |
1486 | } | |
1487 | ||
1488 | if (!node) | |
1489 | return 0; | |
1490 | ||
1491 | of_clk_add_provider(node, of_clk_src_onecell_get, onecell); | |
1492 | ||
1493 | return 0; | |
1494 | } | |
1495 | ||
1496 | static void ds1307_clks_register(struct ds1307 *ds1307) | |
1497 | { | |
1498 | int ret; | |
1499 | ||
1500 | if (ds1307->type != ds_3231) | |
1501 | return; | |
1502 | ||
1503 | ret = ds3231_clks_register(ds1307); | |
1504 | if (ret) { | |
11e5890b HK |
1505 | dev_warn(ds1307->dev, "unable to register clock device %d\n", |
1506 | ret); | |
6c6ff145 AM |
1507 | } |
1508 | } | |
1509 | ||
1510 | #else | |
1511 | ||
1512 | static void ds1307_clks_register(struct ds1307 *ds1307) | |
1513 | { | |
1514 | } | |
1515 | ||
1516 | #endif /* CONFIG_COMMON_CLK */ | |
445c0207 | 1517 | |
11e5890b HK |
1518 | static const struct regmap_config regmap_config = { |
1519 | .reg_bits = 8, | |
1520 | .val_bits = 8, | |
11e5890b HK |
1521 | }; |
1522 | ||
5a167f45 GKH |
1523 | static int ds1307_probe(struct i2c_client *client, |
1524 | const struct i2c_device_id *id) | |
1abb0dc9 DB |
1525 | { |
1526 | struct ds1307 *ds1307; | |
1527 | int err = -ENODEV; | |
584ce30c | 1528 | int tmp; |
7624df48 | 1529 | const struct chip_desc *chip; |
82e2d43f | 1530 | bool want_irq; |
8bc2a407 | 1531 | bool ds1307_can_wakeup_device = false; |
042fa8c7 | 1532 | unsigned char regs[8]; |
01ce893d | 1533 | struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev); |
d8490fd5 | 1534 | u8 trickle_charger_setup = 0; |
1abb0dc9 | 1535 | |
edca66d2 | 1536 | ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL); |
40ce972d | 1537 | if (!ds1307) |
c065f35c | 1538 | return -ENOMEM; |
045e0e85 | 1539 | |
11e5890b HK |
1540 | dev_set_drvdata(&client->dev, ds1307); |
1541 | ds1307->dev = &client->dev; | |
1542 | ds1307->name = client->name; | |
11e5890b HK |
1543 | |
1544 | ds1307->regmap = devm_regmap_init_i2c(client, ®map_config); | |
1545 | if (IS_ERR(ds1307->regmap)) { | |
1546 | dev_err(ds1307->dev, "regmap allocation failed\n"); | |
1547 | return PTR_ERR(ds1307->regmap); | |
1548 | } | |
33df2ee1 | 1549 | |
11e5890b | 1550 | i2c_set_clientdata(client, ds1307); |
7ef6d2c2 JMC |
1551 | |
1552 | if (client->dev.of_node) { | |
1553 | ds1307->type = (enum ds_type) | |
1554 | of_device_get_match_data(&client->dev); | |
1555 | chip = &chips[ds1307->type]; | |
1556 | } else if (id) { | |
9c19b893 TH |
1557 | chip = &chips[id->driver_data]; |
1558 | ds1307->type = id->driver_data; | |
1559 | } else { | |
1560 | const struct acpi_device_id *acpi_id; | |
1561 | ||
1562 | acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids), | |
11e5890b | 1563 | ds1307->dev); |
9c19b893 TH |
1564 | if (!acpi_id) |
1565 | return -ENODEV; | |
1566 | chip = &chips[acpi_id->driver_data]; | |
1567 | ds1307->type = acpi_id->driver_data; | |
1568 | } | |
33df2ee1 | 1569 | |
82e2d43f HK |
1570 | want_irq = client->irq > 0 && chip->alarm; |
1571 | ||
9c19b893 | 1572 | if (!pdata) |
d8490fd5 | 1573 | trickle_charger_setup = ds1307_trickle_init(ds1307, chip); |
9c19b893 | 1574 | else if (pdata->trickle_charger_setup) |
d8490fd5 | 1575 | trickle_charger_setup = pdata->trickle_charger_setup; |
33b04b7b | 1576 | |
d8490fd5 HK |
1577 | if (trickle_charger_setup && chip->trickle_charger_reg) { |
1578 | trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC; | |
11e5890b HK |
1579 | dev_dbg(ds1307->dev, |
1580 | "writing trickle charger info 0x%x to 0x%x\n", | |
d8490fd5 | 1581 | trickle_charger_setup, chip->trickle_charger_reg); |
11e5890b | 1582 | regmap_write(ds1307->regmap, chip->trickle_charger_reg, |
d8490fd5 | 1583 | trickle_charger_setup); |
33b04b7b | 1584 | } |
eb86c306 | 1585 | |
8bc2a407 ML |
1586 | #ifdef CONFIG_OF |
1587 | /* | |
1588 | * For devices with no IRQ directly connected to the SoC, the RTC chip | |
1589 | * can be forced as a wakeup source by stating that explicitly in | |
1590 | * the device's .dts file using the "wakeup-source" boolean property. | |
1591 | * If the "wakeup-source" property is set, don't request an IRQ. | |
1592 | * This will guarantee the 'wakealarm' sysfs entry is available on the device, | |
1593 | * if supported by the RTC. | |
1594 | */ | |
82e2d43f HK |
1595 | if (chip->alarm && of_property_read_bool(client->dev.of_node, |
1596 | "wakeup-source")) | |
78aaa06d | 1597 | ds1307_can_wakeup_device = true; |
8bc2a407 ML |
1598 | #endif |
1599 | ||
045e0e85 DB |
1600 | switch (ds1307->type) { |
1601 | case ds_1337: | |
1602 | case ds_1339: | |
0759c886 | 1603 | case ds_1341: |
97f902b7 | 1604 | case ds_3231: |
be5f59f4 | 1605 | /* get registers that the "rtc" read below won't read... */ |
11e5890b | 1606 | err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL, |
042fa8c7 | 1607 | regs, 2); |
11e5890b HK |
1608 | if (err) { |
1609 | dev_dbg(ds1307->dev, "read error %d\n", err); | |
edca66d2 | 1610 | goto exit; |
1abb0dc9 DB |
1611 | } |
1612 | ||
be5f59f4 | 1613 | /* oscillator off? turn it on, so clock can tick. */ |
042fa8c7 AB |
1614 | if (regs[0] & DS1337_BIT_nEOSC) |
1615 | regs[0] &= ~DS1337_BIT_nEOSC; | |
cb49a5e9 | 1616 | |
40ce972d | 1617 | /* |
8bc2a407 ML |
1618 | * Using IRQ or defined as wakeup-source? |
1619 | * Disable the square wave and both alarms. | |
97f902b7 WS |
1620 | * For some variants, be sure alarms can trigger when we're |
1621 | * running on Vbackup (BBSQI/BBSQW) | |
cb49a5e9 | 1622 | */ |
82e2d43f | 1623 | if (want_irq || ds1307_can_wakeup_device) { |
042fa8c7 AB |
1624 | regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit; |
1625 | regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE); | |
cb49a5e9 RG |
1626 | } |
1627 | ||
11e5890b | 1628 | regmap_write(ds1307->regmap, DS1337_REG_CONTROL, |
042fa8c7 | 1629 | regs[0]); |
be5f59f4 RG |
1630 | |
1631 | /* oscillator fault? clear flag, and warn */ | |
042fa8c7 | 1632 | if (regs[1] & DS1337_BIT_OSF) { |
11e5890b | 1633 | regmap_write(ds1307->regmap, DS1337_REG_STATUS, |
042fa8c7 | 1634 | regs[1] & ~DS1337_BIT_OSF); |
11e5890b | 1635 | dev_warn(ds1307->dev, "SET TIME!\n"); |
1abb0dc9 | 1636 | } |
045e0e85 | 1637 | break; |
a2166858 MF |
1638 | |
1639 | case rx_8025: | |
11e5890b | 1640 | err = regmap_bulk_read(ds1307->regmap, |
042fa8c7 | 1641 | RX8025_REG_CTRL1 << 4 | 0x08, regs, 2); |
11e5890b HK |
1642 | if (err) { |
1643 | dev_dbg(ds1307->dev, "read error %d\n", err); | |
edca66d2 | 1644 | goto exit; |
a2166858 MF |
1645 | } |
1646 | ||
1647 | /* oscillator off? turn it on, so clock can tick. */ | |
042fa8c7 AB |
1648 | if (!(regs[1] & RX8025_BIT_XST)) { |
1649 | regs[1] |= RX8025_BIT_XST; | |
11e5890b HK |
1650 | regmap_write(ds1307->regmap, |
1651 | RX8025_REG_CTRL2 << 4 | 0x08, | |
042fa8c7 | 1652 | regs[1]); |
11e5890b | 1653 | dev_warn(ds1307->dev, |
a2166858 MF |
1654 | "oscillator stop detected - SET TIME!\n"); |
1655 | } | |
1656 | ||
042fa8c7 AB |
1657 | if (regs[1] & RX8025_BIT_PON) { |
1658 | regs[1] &= ~RX8025_BIT_PON; | |
11e5890b HK |
1659 | regmap_write(ds1307->regmap, |
1660 | RX8025_REG_CTRL2 << 4 | 0x08, | |
042fa8c7 | 1661 | regs[1]); |
11e5890b | 1662 | dev_warn(ds1307->dev, "power-on detected\n"); |
a2166858 MF |
1663 | } |
1664 | ||
042fa8c7 AB |
1665 | if (regs[1] & RX8025_BIT_VDET) { |
1666 | regs[1] &= ~RX8025_BIT_VDET; | |
11e5890b HK |
1667 | regmap_write(ds1307->regmap, |
1668 | RX8025_REG_CTRL2 << 4 | 0x08, | |
042fa8c7 | 1669 | regs[1]); |
11e5890b | 1670 | dev_warn(ds1307->dev, "voltage drop detected\n"); |
a2166858 MF |
1671 | } |
1672 | ||
1673 | /* make sure we are running in 24hour mode */ | |
042fa8c7 | 1674 | if (!(regs[0] & RX8025_BIT_2412)) { |
a2166858 MF |
1675 | u8 hour; |
1676 | ||
1677 | /* switch to 24 hour mode */ | |
11e5890b HK |
1678 | regmap_write(ds1307->regmap, |
1679 | RX8025_REG_CTRL1 << 4 | 0x08, | |
042fa8c7 | 1680 | regs[0] | RX8025_BIT_2412); |
11e5890b HK |
1681 | |
1682 | err = regmap_bulk_read(ds1307->regmap, | |
1683 | RX8025_REG_CTRL1 << 4 | 0x08, | |
042fa8c7 | 1684 | regs, 2); |
11e5890b HK |
1685 | if (err) { |
1686 | dev_dbg(ds1307->dev, "read error %d\n", err); | |
edca66d2 | 1687 | goto exit; |
a2166858 MF |
1688 | } |
1689 | ||
1690 | /* correct hour */ | |
042fa8c7 | 1691 | hour = bcd2bin(regs[DS1307_REG_HOUR]); |
a2166858 MF |
1692 | if (hour == 12) |
1693 | hour = 0; | |
042fa8c7 | 1694 | if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM) |
a2166858 MF |
1695 | hour += 12; |
1696 | ||
11e5890b HK |
1697 | regmap_write(ds1307->regmap, |
1698 | DS1307_REG_HOUR << 4 | 0x08, hour); | |
a2166858 MF |
1699 | } |
1700 | break; | |
045e0e85 DB |
1701 | default: |
1702 | break; | |
1703 | } | |
1abb0dc9 DB |
1704 | |
1705 | read_rtc: | |
1706 | /* read RTC registers */ | |
042fa8c7 AB |
1707 | err = regmap_bulk_read(ds1307->regmap, chip->offset, regs, |
1708 | sizeof(regs)); | |
11e5890b HK |
1709 | if (err) { |
1710 | dev_dbg(ds1307->dev, "read error %d\n", err); | |
edca66d2 | 1711 | goto exit; |
1abb0dc9 DB |
1712 | } |
1713 | ||
40ce972d DA |
1714 | /* |
1715 | * minimal sanity checking; some chips (like DS1340) don't | |
1abb0dc9 DB |
1716 | * specify the extra bits as must-be-zero, but there are |
1717 | * still a few values that are clearly out-of-range. | |
1718 | */ | |
042fa8c7 | 1719 | tmp = regs[DS1307_REG_SECS]; |
045e0e85 DB |
1720 | switch (ds1307->type) { |
1721 | case ds_1307: | |
8566f70c | 1722 | case m41t0: |
045e0e85 | 1723 | case m41t00: |
7e580769 | 1724 | case m41t11: |
be5f59f4 | 1725 | /* clock halted? turn it on, so clock can tick. */ |
045e0e85 | 1726 | if (tmp & DS1307_BIT_CH) { |
11e5890b HK |
1727 | regmap_write(ds1307->regmap, DS1307_REG_SECS, 0); |
1728 | dev_warn(ds1307->dev, "SET TIME!\n"); | |
045e0e85 | 1729 | goto read_rtc; |
1abb0dc9 | 1730 | } |
045e0e85 | 1731 | break; |
300a7735 | 1732 | case ds_1308: |
be5f59f4 RG |
1733 | case ds_1338: |
1734 | /* clock halted? turn it on, so clock can tick. */ | |
045e0e85 | 1735 | if (tmp & DS1307_BIT_CH) |
11e5890b | 1736 | regmap_write(ds1307->regmap, DS1307_REG_SECS, 0); |
be5f59f4 RG |
1737 | |
1738 | /* oscillator fault? clear flag, and warn */ | |
042fa8c7 | 1739 | if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) { |
11e5890b | 1740 | regmap_write(ds1307->regmap, DS1307_REG_CONTROL, |
4057a66e AB |
1741 | regs[DS1307_REG_CONTROL] & |
1742 | ~DS1338_BIT_OSF); | |
11e5890b | 1743 | dev_warn(ds1307->dev, "SET TIME!\n"); |
be5f59f4 RG |
1744 | goto read_rtc; |
1745 | } | |
045e0e85 | 1746 | break; |
fcd8db00 R |
1747 | case ds_1340: |
1748 | /* clock halted? turn it on, so clock can tick. */ | |
1749 | if (tmp & DS1340_BIT_nEOSC) | |
11e5890b | 1750 | regmap_write(ds1307->regmap, DS1307_REG_SECS, 0); |
fcd8db00 | 1751 | |
11e5890b HK |
1752 | err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp); |
1753 | if (err) { | |
1754 | dev_dbg(ds1307->dev, "read error %d\n", err); | |
edca66d2 | 1755 | goto exit; |
fcd8db00 R |
1756 | } |
1757 | ||
1758 | /* oscillator fault? clear flag, and warn */ | |
1759 | if (tmp & DS1340_BIT_OSF) { | |
11e5890b HK |
1760 | regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0); |
1761 | dev_warn(ds1307->dev, "SET TIME!\n"); | |
fcd8db00 | 1762 | } |
43fcb815 | 1763 | break; |
f4199f85 | 1764 | case mcp794xx: |
43fcb815 | 1765 | /* make sure that the backup battery is enabled */ |
042fa8c7 | 1766 | if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) { |
11e5890b | 1767 | regmap_write(ds1307->regmap, DS1307_REG_WDAY, |
042fa8c7 | 1768 | regs[DS1307_REG_WDAY] | |
11e5890b | 1769 | MCP794XX_BIT_VBATEN); |
43fcb815 DA |
1770 | } |
1771 | ||
1772 | /* clock halted? turn it on, so clock can tick. */ | |
f4199f85 | 1773 | if (!(tmp & MCP794XX_BIT_ST)) { |
11e5890b HK |
1774 | regmap_write(ds1307->regmap, DS1307_REG_SECS, |
1775 | MCP794XX_BIT_ST); | |
1776 | dev_warn(ds1307->dev, "SET TIME!\n"); | |
43fcb815 DA |
1777 | goto read_rtc; |
1778 | } | |
1779 | ||
fcd8db00 | 1780 | break; |
32d322bc | 1781 | default: |
045e0e85 | 1782 | break; |
1abb0dc9 | 1783 | } |
045e0e85 | 1784 | |
042fa8c7 | 1785 | tmp = regs[DS1307_REG_HOUR]; |
c065f35c DB |
1786 | switch (ds1307->type) { |
1787 | case ds_1340: | |
8566f70c | 1788 | case m41t0: |
c065f35c | 1789 | case m41t00: |
7e580769 | 1790 | case m41t11: |
40ce972d DA |
1791 | /* |
1792 | * NOTE: ignores century bits; fix before deploying | |
c065f35c DB |
1793 | * systems that will run through year 2100. |
1794 | */ | |
1795 | break; | |
a2166858 MF |
1796 | case rx_8025: |
1797 | break; | |
c065f35c DB |
1798 | default: |
1799 | if (!(tmp & DS1307_BIT_12HR)) | |
1800 | break; | |
1801 | ||
40ce972d DA |
1802 | /* |
1803 | * Be sure we're in 24 hour mode. Multi-master systems | |
c065f35c DB |
1804 | * take note... |
1805 | */ | |
fe20ba70 | 1806 | tmp = bcd2bin(tmp & 0x1f); |
c065f35c DB |
1807 | if (tmp == 12) |
1808 | tmp = 0; | |
042fa8c7 | 1809 | if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM) |
c065f35c | 1810 | tmp += 12; |
e553170a | 1811 | regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR, |
11e5890b | 1812 | bin2bcd(tmp)); |
1abb0dc9 DB |
1813 | } |
1814 | ||
82e2d43f | 1815 | if (want_irq || ds1307_can_wakeup_device) { |
11e5890b | 1816 | device_set_wakeup_capable(ds1307->dev, true); |
3abb1ada SG |
1817 | set_bit(HAS_ALARM, &ds1307->flags); |
1818 | } | |
69b119a6 AB |
1819 | |
1820 | ds1307->rtc = devm_rtc_allocate_device(ds1307->dev); | |
e69c0567 | 1821 | if (IS_ERR(ds1307->rtc)) |
4071ea25 | 1822 | return PTR_ERR(ds1307->rtc); |
1abb0dc9 | 1823 | |
82e2d43f | 1824 | if (ds1307_can_wakeup_device && !want_irq) { |
11e5890b HK |
1825 | dev_info(ds1307->dev, |
1826 | "'wakeup-source' is set, request for an IRQ is disabled!\n"); | |
8bc2a407 ML |
1827 | /* We cannot support UIE mode if we do not have an IRQ line */ |
1828 | ds1307->rtc->uie_unsupported = 1; | |
1829 | } | |
1830 | ||
cb49a5e9 | 1831 | if (want_irq) { |
45947127 HK |
1832 | err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL, |
1833 | chip->irq_handler ?: ds1307_irq, | |
c5983191 | 1834 | IRQF_SHARED | IRQF_ONESHOT, |
4b9e2a0c | 1835 | ds1307->name, ds1307); |
cb49a5e9 | 1836 | if (err) { |
4071ea25 | 1837 | client->irq = 0; |
11e5890b | 1838 | device_set_wakeup_capable(ds1307->dev, false); |
3abb1ada | 1839 | clear_bit(HAS_ALARM, &ds1307->flags); |
11e5890b | 1840 | dev_err(ds1307->dev, "unable to request IRQ!\n"); |
e69c0567 | 1841 | } else { |
11e5890b | 1842 | dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq); |
e69c0567 | 1843 | } |
cb49a5e9 RG |
1844 | } |
1845 | ||
e9fb7682 | 1846 | ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops; |
6a5f2a1f | 1847 | err = ds1307_add_frequency_test(ds1307); |
e9fb7682 AB |
1848 | if (err) |
1849 | return err; | |
1850 | ||
6a5f2a1f | 1851 | err = rtc_register_device(ds1307->rtc); |
b41c23e1 GB |
1852 | if (err) |
1853 | return err; | |
1854 | ||
9eab0a78 | 1855 | if (chip->nvram_size) { |
409baf17 AB |
1856 | struct nvmem_config nvmem_cfg = { |
1857 | .name = "ds1307_nvram", | |
1858 | .word_size = 1, | |
1859 | .stride = 1, | |
1860 | .size = chip->nvram_size, | |
1861 | .reg_read = ds1307_nvram_read, | |
1862 | .reg_write = ds1307_nvram_write, | |
1863 | .priv = ds1307, | |
1864 | }; | |
abc925f7 | 1865 | |
abc925f7 | 1866 | ds1307->rtc->nvram_old_abi = true; |
409baf17 | 1867 | rtc_nvmem_register(ds1307->rtc, &nvmem_cfg); |
682d73f6 DB |
1868 | } |
1869 | ||
445c0207 | 1870 | ds1307_hwmon_register(ds1307); |
6c6ff145 | 1871 | ds1307_clks_register(ds1307); |
445c0207 | 1872 | |
1abb0dc9 DB |
1873 | return 0; |
1874 | ||
edca66d2 | 1875 | exit: |
1abb0dc9 DB |
1876 | return err; |
1877 | } | |
1878 | ||
1abb0dc9 DB |
1879 | static struct i2c_driver ds1307_driver = { |
1880 | .driver = { | |
c065f35c | 1881 | .name = "rtc-ds1307", |
7ef6d2c2 | 1882 | .of_match_table = of_match_ptr(ds1307_of_match), |
9c19b893 | 1883 | .acpi_match_table = ACPI_PTR(ds1307_acpi_ids), |
1abb0dc9 | 1884 | }, |
c065f35c | 1885 | .probe = ds1307_probe, |
3760f736 | 1886 | .id_table = ds1307_id, |
1abb0dc9 DB |
1887 | }; |
1888 | ||
0abc9201 | 1889 | module_i2c_driver(ds1307_driver); |
1abb0dc9 DB |
1890 | |
1891 | MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips"); | |
1892 | MODULE_LICENSE("GPL"); |