]>
Commit | Line | Data |
---|---|---|
52365230 | 1 | /* |
aba39d27 | 2 | * Micro Crystal RV-3029 rtc class driver |
52365230 HS |
3 | * |
4 | * Author: Gregory Hermant <gregory.hermant@calao-systems.com> | |
2dca3d9e | 5 | * Michael Buesch <m@bues.ch> |
52365230 HS |
6 | * |
7 | * based on previously existing rtc class drivers | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
52365230 HS |
13 | */ |
14 | ||
15 | #include <linux/module.h> | |
16 | #include <linux/i2c.h> | |
17 | #include <linux/bcd.h> | |
18 | #include <linux/rtc.h> | |
a7f6e287 MB |
19 | #include <linux/delay.h> |
20 | #include <linux/of.h> | |
a696b31e MB |
21 | #include <linux/hwmon.h> |
22 | #include <linux/hwmon-sysfs.h> | |
a7f6e287 | 23 | |
52365230 HS |
24 | |
25 | /* Register map */ | |
26 | /* control section */ | |
aba39d27 | 27 | #define RV3029_ONOFF_CTRL 0x00 |
7697de35 MB |
28 | #define RV3029_ONOFF_CTRL_WE BIT(0) |
29 | #define RV3029_ONOFF_CTRL_TE BIT(1) | |
30 | #define RV3029_ONOFF_CTRL_TAR BIT(2) | |
31 | #define RV3029_ONOFF_CTRL_EERE BIT(3) | |
32 | #define RV3029_ONOFF_CTRL_SRON BIT(4) | |
33 | #define RV3029_ONOFF_CTRL_TD0 BIT(5) | |
34 | #define RV3029_ONOFF_CTRL_TD1 BIT(6) | |
35 | #define RV3029_ONOFF_CTRL_CLKINT BIT(7) | |
aba39d27 | 36 | #define RV3029_IRQ_CTRL 0x01 |
7697de35 MB |
37 | #define RV3029_IRQ_CTRL_AIE BIT(0) |
38 | #define RV3029_IRQ_CTRL_TIE BIT(1) | |
39 | #define RV3029_IRQ_CTRL_V1IE BIT(2) | |
40 | #define RV3029_IRQ_CTRL_V2IE BIT(3) | |
41 | #define RV3029_IRQ_CTRL_SRIE BIT(4) | |
aba39d27 | 42 | #define RV3029_IRQ_FLAGS 0x02 |
7697de35 MB |
43 | #define RV3029_IRQ_FLAGS_AF BIT(0) |
44 | #define RV3029_IRQ_FLAGS_TF BIT(1) | |
45 | #define RV3029_IRQ_FLAGS_V1IF BIT(2) | |
46 | #define RV3029_IRQ_FLAGS_V2IF BIT(3) | |
47 | #define RV3029_IRQ_FLAGS_SRF BIT(4) | |
aba39d27 | 48 | #define RV3029_STATUS 0x03 |
7697de35 MB |
49 | #define RV3029_STATUS_VLOW1 BIT(2) |
50 | #define RV3029_STATUS_VLOW2 BIT(3) | |
51 | #define RV3029_STATUS_SR BIT(4) | |
52 | #define RV3029_STATUS_PON BIT(5) | |
53 | #define RV3029_STATUS_EEBUSY BIT(7) | |
aba39d27 | 54 | #define RV3029_RST_CTRL 0x04 |
7697de35 | 55 | #define RV3029_RST_CTRL_SYSR BIT(4) |
aba39d27 | 56 | #define RV3029_CONTROL_SECTION_LEN 0x05 |
52365230 HS |
57 | |
58 | /* watch section */ | |
aba39d27 MB |
59 | #define RV3029_W_SEC 0x08 |
60 | #define RV3029_W_MINUTES 0x09 | |
61 | #define RV3029_W_HOURS 0x0A | |
7697de35 MB |
62 | #define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */ |
63 | #define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */ | |
aba39d27 MB |
64 | #define RV3029_W_DATE 0x0B |
65 | #define RV3029_W_DAYS 0x0C | |
66 | #define RV3029_W_MONTHS 0x0D | |
67 | #define RV3029_W_YEARS 0x0E | |
68 | #define RV3029_WATCH_SECTION_LEN 0x07 | |
52365230 HS |
69 | |
70 | /* alarm section */ | |
aba39d27 MB |
71 | #define RV3029_A_SC 0x10 |
72 | #define RV3029_A_MN 0x11 | |
73 | #define RV3029_A_HR 0x12 | |
74 | #define RV3029_A_DT 0x13 | |
75 | #define RV3029_A_DW 0x14 | |
76 | #define RV3029_A_MO 0x15 | |
77 | #define RV3029_A_YR 0x16 | |
78 | #define RV3029_ALARM_SECTION_LEN 0x07 | |
52365230 HS |
79 | |
80 | /* timer section */ | |
aba39d27 MB |
81 | #define RV3029_TIMER_LOW 0x18 |
82 | #define RV3029_TIMER_HIGH 0x19 | |
52365230 HS |
83 | |
84 | /* temperature section */ | |
aba39d27 | 85 | #define RV3029_TEMP_PAGE 0x20 |
52365230 HS |
86 | |
87 | /* eeprom data section */ | |
aba39d27 MB |
88 | #define RV3029_E2P_EEDATA1 0x28 |
89 | #define RV3029_E2P_EEDATA2 0x29 | |
7697de35 | 90 | #define RV3029_E2PDATA_SECTION_LEN 0x02 |
52365230 HS |
91 | |
92 | /* eeprom control section */ | |
aba39d27 | 93 | #define RV3029_CONTROL_E2P_EECTRL 0x30 |
7697de35 MB |
94 | #define RV3029_EECTRL_THP BIT(0) /* temp scan interval */ |
95 | #define RV3029_EECTRL_THE BIT(1) /* thermometer enable */ | |
96 | #define RV3029_EECTRL_FD0 BIT(2) /* CLKOUT */ | |
97 | #define RV3029_EECTRL_FD1 BIT(3) /* CLKOUT */ | |
98 | #define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */ | |
99 | #define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */ | |
100 | #define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */ | |
101 | #define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */ | |
102 | #define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\ | |
103 | RV3029_TRICKLE_5K |\ | |
104 | RV3029_TRICKLE_20K |\ | |
105 | RV3029_TRICKLE_80K) | |
106 | #define RV3029_TRICKLE_SHIFT 4 | |
107 | #define RV3029_CONTROL_E2P_XOFFS 0x31 /* XTAL offset */ | |
108 | #define RV3029_CONTROL_E2P_XOFFS_SIGN BIT(7) /* Sign: 1->pos, 0->neg */ | |
109 | #define RV3029_CONTROL_E2P_QCOEF 0x32 /* XTAL temp drift coef */ | |
110 | #define RV3029_CONTROL_E2P_TURNOVER 0x33 /* XTAL turnover temp (in *C) */ | |
111 | #define RV3029_CONTROL_E2P_TOV_MASK 0x3F /* XTAL turnover temp mask */ | |
52365230 HS |
112 | |
113 | /* user ram section */ | |
aba39d27 MB |
114 | #define RV3029_USR1_RAM_PAGE 0x38 |
115 | #define RV3029_USR1_SECTION_LEN 0x04 | |
116 | #define RV3029_USR2_RAM_PAGE 0x3C | |
117 | #define RV3029_USR2_SECTION_LEN 0x04 | |
52365230 | 118 | |
4e7f1a60 MJ |
119 | static int rv3029_read_regs(struct i2c_client *client, u8 reg, u8 *buf, |
120 | unsigned len) | |
52365230 HS |
121 | { |
122 | int ret; | |
123 | ||
aba39d27 MB |
124 | if ((reg > RV3029_USR1_RAM_PAGE + 7) || |
125 | (reg + len > RV3029_USR1_RAM_PAGE + 8)) | |
52365230 HS |
126 | return -EINVAL; |
127 | ||
128 | ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf); | |
129 | if (ret < 0) | |
130 | return ret; | |
131 | if (ret < len) | |
132 | return -EIO; | |
133 | return 0; | |
134 | } | |
135 | ||
4e7f1a60 MJ |
136 | static int rv3029_write_regs(struct i2c_client *client, u8 reg, u8 const buf[], |
137 | unsigned len) | |
52365230 | 138 | { |
aba39d27 MB |
139 | if ((reg > RV3029_USR1_RAM_PAGE + 7) || |
140 | (reg + len > RV3029_USR1_RAM_PAGE + 8)) | |
52365230 HS |
141 | return -EINVAL; |
142 | ||
143 | return i2c_smbus_write_i2c_block_data(client, reg, len, buf); | |
144 | } | |
145 | ||
4e7f1a60 MJ |
146 | static int rv3029_update_bits(struct i2c_client *client, u8 reg, u8 mask, |
147 | u8 set) | |
2dca3d9e MB |
148 | { |
149 | u8 buf; | |
150 | int ret; | |
151 | ||
4e7f1a60 | 152 | ret = rv3029_read_regs(client, reg, &buf, 1); |
2dca3d9e MB |
153 | if (ret < 0) |
154 | return ret; | |
155 | buf &= ~mask; | |
156 | buf |= set & mask; | |
4e7f1a60 | 157 | ret = rv3029_write_regs(client, reg, &buf, 1); |
2dca3d9e MB |
158 | if (ret < 0) |
159 | return ret; | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
4e7f1a60 | 164 | static int rv3029_get_sr(struct i2c_client *client, u8 *buf) |
52365230 | 165 | { |
4e7f1a60 | 166 | int ret = rv3029_read_regs(client, RV3029_STATUS, buf, 1); |
52365230 HS |
167 | |
168 | if (ret < 0) | |
169 | return -EIO; | |
170 | dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); | |
171 | return 0; | |
172 | } | |
173 | ||
4e7f1a60 | 174 | static int rv3029_set_sr(struct i2c_client *client, u8 val) |
52365230 HS |
175 | { |
176 | u8 buf[1]; | |
177 | int sr; | |
178 | ||
179 | buf[0] = val; | |
4e7f1a60 | 180 | sr = rv3029_write_regs(client, RV3029_STATUS, buf, 1); |
52365230 HS |
181 | dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]); |
182 | if (sr < 0) | |
183 | return -EIO; | |
184 | return 0; | |
185 | } | |
186 | ||
a7f6e287 MB |
187 | static int rv3029_eeprom_busywait(struct i2c_client *client) |
188 | { | |
189 | int i, ret; | |
190 | u8 sr; | |
191 | ||
192 | for (i = 100; i > 0; i--) { | |
4e7f1a60 | 193 | ret = rv3029_get_sr(client, &sr); |
a7f6e287 MB |
194 | if (ret < 0) |
195 | break; | |
196 | if (!(sr & RV3029_STATUS_EEBUSY)) | |
197 | break; | |
198 | usleep_range(1000, 10000); | |
199 | } | |
200 | if (i <= 0) { | |
201 | dev_err(&client->dev, "EEPROM busy wait timeout.\n"); | |
202 | return -ETIMEDOUT; | |
203 | } | |
204 | ||
205 | return ret; | |
206 | } | |
207 | ||
208 | static int rv3029_eeprom_exit(struct i2c_client *client) | |
209 | { | |
210 | /* Re-enable eeprom refresh */ | |
4e7f1a60 MJ |
211 | return rv3029_update_bits(client, RV3029_ONOFF_CTRL, |
212 | RV3029_ONOFF_CTRL_EERE, | |
213 | RV3029_ONOFF_CTRL_EERE); | |
a7f6e287 MB |
214 | } |
215 | ||
216 | static int rv3029_eeprom_enter(struct i2c_client *client) | |
217 | { | |
218 | int ret; | |
219 | u8 sr; | |
220 | ||
221 | /* Check whether we are in the allowed voltage range. */ | |
4e7f1a60 | 222 | ret = rv3029_get_sr(client, &sr); |
a7f6e287 MB |
223 | if (ret < 0) |
224 | return ret; | |
225 | if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { | |
226 | /* We clear the bits and retry once just in case | |
227 | * we had a brown out in early startup. | |
228 | */ | |
229 | sr &= ~RV3029_STATUS_VLOW1; | |
230 | sr &= ~RV3029_STATUS_VLOW2; | |
4e7f1a60 | 231 | ret = rv3029_set_sr(client, sr); |
a7f6e287 MB |
232 | if (ret < 0) |
233 | return ret; | |
234 | usleep_range(1000, 10000); | |
4e7f1a60 | 235 | ret = rv3029_get_sr(client, &sr); |
a7f6e287 MB |
236 | if (ret < 0) |
237 | return ret; | |
238 | if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) { | |
239 | dev_err(&client->dev, | |
240 | "Supply voltage is too low to safely access the EEPROM.\n"); | |
241 | return -ENODEV; | |
242 | } | |
243 | } | |
244 | ||
245 | /* Disable eeprom refresh. */ | |
4e7f1a60 MJ |
246 | ret = rv3029_update_bits(client, RV3029_ONOFF_CTRL, |
247 | RV3029_ONOFF_CTRL_EERE, 0); | |
a7f6e287 MB |
248 | if (ret < 0) |
249 | return ret; | |
250 | ||
251 | /* Wait for any previous eeprom accesses to finish. */ | |
252 | ret = rv3029_eeprom_busywait(client); | |
253 | if (ret < 0) | |
254 | rv3029_eeprom_exit(client); | |
255 | ||
256 | return ret; | |
257 | } | |
258 | ||
259 | static int rv3029_eeprom_read(struct i2c_client *client, u8 reg, | |
260 | u8 buf[], size_t len) | |
261 | { | |
262 | int ret, err; | |
263 | ||
264 | err = rv3029_eeprom_enter(client); | |
265 | if (err < 0) | |
266 | return err; | |
267 | ||
4e7f1a60 | 268 | ret = rv3029_read_regs(client, reg, buf, len); |
a7f6e287 MB |
269 | |
270 | err = rv3029_eeprom_exit(client); | |
271 | if (err < 0) | |
272 | return err; | |
273 | ||
274 | return ret; | |
275 | } | |
276 | ||
277 | static int rv3029_eeprom_write(struct i2c_client *client, u8 reg, | |
278 | u8 const buf[], size_t len) | |
279 | { | |
280 | int ret, err; | |
281 | size_t i; | |
282 | u8 tmp; | |
283 | ||
284 | err = rv3029_eeprom_enter(client); | |
285 | if (err < 0) | |
286 | return err; | |
287 | ||
288 | for (i = 0; i < len; i++, reg++) { | |
4e7f1a60 | 289 | ret = rv3029_read_regs(client, reg, &tmp, 1); |
a7f6e287 MB |
290 | if (ret < 0) |
291 | break; | |
292 | if (tmp != buf[i]) { | |
4e7f1a60 | 293 | ret = rv3029_write_regs(client, reg, &buf[i], 1); |
a7f6e287 MB |
294 | if (ret < 0) |
295 | break; | |
296 | } | |
297 | ret = rv3029_eeprom_busywait(client); | |
298 | if (ret < 0) | |
299 | break; | |
300 | } | |
301 | ||
302 | err = rv3029_eeprom_exit(client); | |
303 | if (err < 0) | |
304 | return err; | |
305 | ||
306 | return ret; | |
307 | } | |
308 | ||
39387dc2 MB |
309 | static int rv3029_eeprom_update_bits(struct i2c_client *client, |
310 | u8 reg, u8 mask, u8 set) | |
311 | { | |
312 | u8 buf; | |
313 | int ret; | |
314 | ||
315 | ret = rv3029_eeprom_read(client, reg, &buf, 1); | |
316 | if (ret < 0) | |
317 | return ret; | |
318 | buf &= ~mask; | |
319 | buf |= set & mask; | |
320 | ret = rv3029_eeprom_write(client, reg, &buf, 1); | |
321 | if (ret < 0) | |
322 | return ret; | |
323 | ||
324 | return 0; | |
325 | } | |
326 | ||
4e7f1a60 | 327 | static int rv3029_read_time(struct i2c_client *client, struct rtc_time *tm) |
52365230 HS |
328 | { |
329 | u8 buf[1]; | |
330 | int ret; | |
aba39d27 | 331 | u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, }; |
52365230 | 332 | |
4e7f1a60 | 333 | ret = rv3029_get_sr(client, buf); |
52365230 HS |
334 | if (ret < 0) { |
335 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); | |
336 | return -EIO; | |
337 | } | |
338 | ||
4e7f1a60 MJ |
339 | ret = rv3029_read_regs(client, RV3029_W_SEC, regs, |
340 | RV3029_WATCH_SECTION_LEN); | |
52365230 HS |
341 | if (ret < 0) { |
342 | dev_err(&client->dev, "%s: reading RTC section failed\n", | |
343 | __func__); | |
344 | return ret; | |
345 | } | |
346 | ||
aba39d27 MB |
347 | tm->tm_sec = bcd2bin(regs[RV3029_W_SEC-RV3029_W_SEC]); |
348 | tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES-RV3029_W_SEC]); | |
52365230 HS |
349 | |
350 | /* HR field has a more complex interpretation */ | |
351 | { | |
aba39d27 MB |
352 | const u8 _hr = regs[RV3029_W_HOURS-RV3029_W_SEC]; |
353 | ||
354 | if (_hr & RV3029_REG_HR_12_24) { | |
52365230 HS |
355 | /* 12h format */ |
356 | tm->tm_hour = bcd2bin(_hr & 0x1f); | |
aba39d27 | 357 | if (_hr & RV3029_REG_HR_PM) /* PM flag set */ |
52365230 HS |
358 | tm->tm_hour += 12; |
359 | } else /* 24h format */ | |
360 | tm->tm_hour = bcd2bin(_hr & 0x3f); | |
361 | } | |
362 | ||
aba39d27 MB |
363 | tm->tm_mday = bcd2bin(regs[RV3029_W_DATE-RV3029_W_SEC]); |
364 | tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS-RV3029_W_SEC]) - 1; | |
365 | tm->tm_year = bcd2bin(regs[RV3029_W_YEARS-RV3029_W_SEC]) + 100; | |
366 | tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS-RV3029_W_SEC]) - 1; | |
52365230 HS |
367 | |
368 | return 0; | |
369 | } | |
370 | ||
aba39d27 | 371 | static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm) |
52365230 | 372 | { |
4e7f1a60 | 373 | return rv3029_read_time(to_i2c_client(dev), tm); |
52365230 HS |
374 | } |
375 | ||
4e7f1a60 | 376 | static int rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
52365230 | 377 | { |
4e7f1a60 | 378 | struct i2c_client *client = to_i2c_client(dev); |
52365230 HS |
379 | struct rtc_time *const tm = &alarm->time; |
380 | int ret; | |
381 | u8 regs[8]; | |
382 | ||
4e7f1a60 | 383 | ret = rv3029_get_sr(client, regs); |
52365230 HS |
384 | if (ret < 0) { |
385 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); | |
386 | return -EIO; | |
387 | } | |
388 | ||
4e7f1a60 MJ |
389 | ret = rv3029_read_regs(client, RV3029_A_SC, regs, |
390 | RV3029_ALARM_SECTION_LEN); | |
52365230 HS |
391 | |
392 | if (ret < 0) { | |
393 | dev_err(&client->dev, "%s: reading alarm section failed\n", | |
394 | __func__); | |
395 | return ret; | |
396 | } | |
397 | ||
aba39d27 MB |
398 | tm->tm_sec = bcd2bin(regs[RV3029_A_SC-RV3029_A_SC] & 0x7f); |
399 | tm->tm_min = bcd2bin(regs[RV3029_A_MN-RV3029_A_SC] & 0x7f); | |
400 | tm->tm_hour = bcd2bin(regs[RV3029_A_HR-RV3029_A_SC] & 0x3f); | |
401 | tm->tm_mday = bcd2bin(regs[RV3029_A_DT-RV3029_A_SC] & 0x3f); | |
402 | tm->tm_mon = bcd2bin(regs[RV3029_A_MO-RV3029_A_SC] & 0x1f) - 1; | |
403 | tm->tm_year = bcd2bin(regs[RV3029_A_YR-RV3029_A_SC] & 0x7f) + 100; | |
404 | tm->tm_wday = bcd2bin(regs[RV3029_A_DW-RV3029_A_SC] & 0x07) - 1; | |
52365230 HS |
405 | |
406 | return 0; | |
407 | } | |
408 | ||
4e7f1a60 | 409 | static int rv3029_rtc_alarm_set_irq(struct i2c_client *client, int enable) |
52365230 HS |
410 | { |
411 | int ret; | |
52365230 | 412 | |
2dca3d9e | 413 | /* enable/disable AIE irq */ |
4e7f1a60 MJ |
414 | ret = rv3029_update_bits(client, RV3029_IRQ_CTRL, |
415 | RV3029_IRQ_CTRL_AIE, | |
416 | (enable ? RV3029_IRQ_CTRL_AIE : 0)); | |
52365230 | 417 | if (ret < 0) { |
2dca3d9e | 418 | dev_err(&client->dev, "can't update INT reg\n"); |
52365230 HS |
419 | return ret; |
420 | } | |
421 | ||
422 | return 0; | |
423 | } | |
424 | ||
4e7f1a60 | 425 | static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
52365230 | 426 | { |
4e7f1a60 | 427 | struct i2c_client *client = to_i2c_client(dev); |
52365230 HS |
428 | struct rtc_time *const tm = &alarm->time; |
429 | int ret; | |
430 | u8 regs[8]; | |
431 | ||
432 | /* | |
433 | * The clock has an 8 bit wide bcd-coded register (they never learn) | |
434 | * for the year. tm_year is an offset from 1900 and we are interested | |
435 | * in the 2000-2099 range, so any value less than 100 is invalid. | |
436 | */ | |
437 | if (tm->tm_year < 100) | |
438 | return -EINVAL; | |
439 | ||
4e7f1a60 | 440 | ret = rv3029_get_sr(client, regs); |
52365230 HS |
441 | if (ret < 0) { |
442 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); | |
443 | return -EIO; | |
444 | } | |
aba39d27 MB |
445 | regs[RV3029_A_SC-RV3029_A_SC] = bin2bcd(tm->tm_sec & 0x7f); |
446 | regs[RV3029_A_MN-RV3029_A_SC] = bin2bcd(tm->tm_min & 0x7f); | |
447 | regs[RV3029_A_HR-RV3029_A_SC] = bin2bcd(tm->tm_hour & 0x3f); | |
448 | regs[RV3029_A_DT-RV3029_A_SC] = bin2bcd(tm->tm_mday & 0x3f); | |
449 | regs[RV3029_A_MO-RV3029_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1); | |
450 | regs[RV3029_A_DW-RV3029_A_SC] = bin2bcd((tm->tm_wday & 7) - 1); | |
451 | regs[RV3029_A_YR-RV3029_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100); | |
452 | ||
4e7f1a60 MJ |
453 | ret = rv3029_write_regs(client, RV3029_A_SC, regs, |
454 | RV3029_ALARM_SECTION_LEN); | |
52365230 HS |
455 | if (ret < 0) |
456 | return ret; | |
457 | ||
458 | if (alarm->enabled) { | |
52365230 | 459 | /* clear AF flag */ |
4e7f1a60 MJ |
460 | ret = rv3029_update_bits(client, RV3029_IRQ_FLAGS, |
461 | RV3029_IRQ_FLAGS_AF, 0); | |
52365230 | 462 | if (ret < 0) { |
2dca3d9e | 463 | dev_err(&client->dev, "can't clear alarm flag\n"); |
52365230 HS |
464 | return ret; |
465 | } | |
466 | /* enable AIE irq */ | |
4e7f1a60 | 467 | ret = rv3029_rtc_alarm_set_irq(client, 1); |
52365230 HS |
468 | if (ret) |
469 | return ret; | |
470 | ||
471 | dev_dbg(&client->dev, "alarm IRQ armed\n"); | |
472 | } else { | |
473 | /* disable AIE irq */ | |
4e7f1a60 | 474 | ret = rv3029_rtc_alarm_set_irq(client, 0); |
52365230 HS |
475 | if (ret) |
476 | return ret; | |
477 | ||
478 | dev_dbg(&client->dev, "alarm IRQ disabled\n"); | |
479 | } | |
480 | ||
481 | return 0; | |
482 | } | |
483 | ||
4e7f1a60 | 484 | static int rv3029_set_time(struct i2c_client *client, struct rtc_time const *tm) |
52365230 HS |
485 | { |
486 | u8 regs[8]; | |
487 | int ret; | |
488 | ||
489 | /* | |
490 | * The clock has an 8 bit wide bcd-coded register (they never learn) | |
491 | * for the year. tm_year is an offset from 1900 and we are interested | |
492 | * in the 2000-2099 range, so any value less than 100 is invalid. | |
493 | */ | |
494 | if (tm->tm_year < 100) | |
495 | return -EINVAL; | |
496 | ||
aba39d27 MB |
497 | regs[RV3029_W_SEC-RV3029_W_SEC] = bin2bcd(tm->tm_sec); |
498 | regs[RV3029_W_MINUTES-RV3029_W_SEC] = bin2bcd(tm->tm_min); | |
499 | regs[RV3029_W_HOURS-RV3029_W_SEC] = bin2bcd(tm->tm_hour); | |
500 | regs[RV3029_W_DATE-RV3029_W_SEC] = bin2bcd(tm->tm_mday); | |
501 | regs[RV3029_W_MONTHS-RV3029_W_SEC] = bin2bcd(tm->tm_mon+1); | |
502 | regs[RV3029_W_DAYS-RV3029_W_SEC] = bin2bcd((tm->tm_wday & 7)+1); | |
503 | regs[RV3029_W_YEARS-RV3029_W_SEC] = bin2bcd(tm->tm_year - 100); | |
52365230 | 504 | |
4e7f1a60 MJ |
505 | ret = rv3029_write_regs(client, RV3029_W_SEC, regs, |
506 | RV3029_WATCH_SECTION_LEN); | |
52365230 HS |
507 | if (ret < 0) |
508 | return ret; | |
509 | ||
4e7f1a60 | 510 | ret = rv3029_get_sr(client, regs); |
52365230 HS |
511 | if (ret < 0) { |
512 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); | |
513 | return ret; | |
514 | } | |
515 | /* clear PON bit */ | |
4e7f1a60 | 516 | ret = rv3029_set_sr(client, (regs[0] & ~RV3029_STATUS_PON)); |
52365230 HS |
517 | if (ret < 0) { |
518 | dev_err(&client->dev, "%s: reading SR failed\n", __func__); | |
519 | return ret; | |
520 | } | |
521 | ||
522 | return 0; | |
523 | } | |
524 | ||
aba39d27 | 525 | static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm) |
52365230 | 526 | { |
4e7f1a60 | 527 | return rv3029_set_time(to_i2c_client(dev), tm); |
52365230 HS |
528 | } |
529 | ||
e27e2160 MB |
530 | static const struct rv3029_trickle_tab_elem { |
531 | u32 r; /* resistance in ohms */ | |
532 | u8 conf; /* trickle config bits */ | |
533 | } rv3029_trickle_tab[] = { | |
534 | { | |
535 | .r = 1076, | |
536 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | | |
537 | RV3029_TRICKLE_20K | RV3029_TRICKLE_80K, | |
538 | }, { | |
539 | .r = 1091, | |
540 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | | |
541 | RV3029_TRICKLE_20K, | |
542 | }, { | |
543 | .r = 1137, | |
544 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K | | |
545 | RV3029_TRICKLE_80K, | |
546 | }, { | |
547 | .r = 1154, | |
548 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K, | |
549 | }, { | |
550 | .r = 1371, | |
551 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K | | |
552 | RV3029_TRICKLE_80K, | |
553 | }, { | |
554 | .r = 1395, | |
555 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K, | |
556 | }, { | |
557 | .r = 1472, | |
558 | .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K, | |
559 | }, { | |
560 | .r = 1500, | |
561 | .conf = RV3029_TRICKLE_1K, | |
562 | }, { | |
563 | .r = 3810, | |
564 | .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K | | |
565 | RV3029_TRICKLE_80K, | |
566 | }, { | |
567 | .r = 4000, | |
568 | .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K, | |
569 | }, { | |
570 | .r = 4706, | |
571 | .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K, | |
572 | }, { | |
573 | .r = 5000, | |
574 | .conf = RV3029_TRICKLE_5K, | |
575 | }, { | |
576 | .r = 16000, | |
577 | .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K, | |
578 | }, { | |
579 | .r = 20000, | |
580 | .conf = RV3029_TRICKLE_20K, | |
581 | }, { | |
582 | .r = 80000, | |
583 | .conf = RV3029_TRICKLE_80K, | |
584 | }, | |
585 | }; | |
586 | ||
587 | static void rv3029_trickle_config(struct i2c_client *client) | |
588 | { | |
589 | struct device_node *of_node = client->dev.of_node; | |
590 | const struct rv3029_trickle_tab_elem *elem; | |
591 | int i, err; | |
592 | u32 ohms; | |
39387dc2 | 593 | u8 trickle_set_bits; |
e27e2160 MB |
594 | |
595 | if (!of_node) | |
596 | return; | |
597 | ||
598 | /* Configure the trickle charger. */ | |
e27e2160 MB |
599 | err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms); |
600 | if (err) { | |
601 | /* Disable trickle charger. */ | |
39387dc2 | 602 | trickle_set_bits = 0; |
e27e2160 MB |
603 | } else { |
604 | /* Enable trickle charger. */ | |
605 | for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) { | |
606 | elem = &rv3029_trickle_tab[i]; | |
607 | if (elem->r >= ohms) | |
608 | break; | |
609 | } | |
39387dc2 | 610 | trickle_set_bits = elem->conf; |
e27e2160 MB |
611 | dev_info(&client->dev, |
612 | "Trickle charger enabled at %d ohms resistance.\n", | |
613 | elem->r); | |
614 | } | |
39387dc2 MB |
615 | err = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL, |
616 | RV3029_TRICKLE_MASK, | |
617 | trickle_set_bits); | |
e27e2160 MB |
618 | if (err < 0) { |
619 | dev_err(&client->dev, | |
39387dc2 | 620 | "Failed to update trickle charger config\n"); |
e27e2160 MB |
621 | } |
622 | } | |
623 | ||
a696b31e MB |
624 | #ifdef CONFIG_RTC_DRV_RV3029_HWMON |
625 | ||
626 | static int rv3029_read_temp(struct i2c_client *client, int *temp_mC) | |
627 | { | |
628 | int ret; | |
629 | u8 temp; | |
630 | ||
4e7f1a60 | 631 | ret = rv3029_read_regs(client, RV3029_TEMP_PAGE, &temp, 1); |
a696b31e MB |
632 | if (ret < 0) |
633 | return ret; | |
634 | ||
635 | *temp_mC = ((int)temp - 60) * 1000; | |
636 | ||
637 | return 0; | |
638 | } | |
639 | ||
640 | static ssize_t rv3029_hwmon_show_temp(struct device *dev, | |
641 | struct device_attribute *attr, | |
642 | char *buf) | |
643 | { | |
644 | struct i2c_client *client = dev_get_drvdata(dev); | |
645 | int ret, temp_mC; | |
646 | ||
647 | ret = rv3029_read_temp(client, &temp_mC); | |
648 | if (ret < 0) | |
649 | return ret; | |
650 | ||
651 | return sprintf(buf, "%d\n", temp_mC); | |
652 | } | |
653 | ||
654 | static ssize_t rv3029_hwmon_set_update_interval(struct device *dev, | |
655 | struct device_attribute *attr, | |
656 | const char *buf, | |
657 | size_t count) | |
658 | { | |
659 | struct i2c_client *client = dev_get_drvdata(dev); | |
660 | unsigned long interval_ms; | |
661 | int ret; | |
662 | u8 th_set_bits = 0; | |
663 | ||
664 | ret = kstrtoul(buf, 10, &interval_ms); | |
665 | if (ret < 0) | |
666 | return ret; | |
667 | ||
668 | if (interval_ms != 0) { | |
669 | th_set_bits |= RV3029_EECTRL_THE; | |
670 | if (interval_ms >= 16000) | |
671 | th_set_bits |= RV3029_EECTRL_THP; | |
672 | } | |
673 | ret = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL, | |
674 | RV3029_EECTRL_THE | RV3029_EECTRL_THP, | |
675 | th_set_bits); | |
676 | if (ret < 0) | |
677 | return ret; | |
678 | ||
679 | return count; | |
680 | } | |
681 | ||
682 | static ssize_t rv3029_hwmon_show_update_interval(struct device *dev, | |
683 | struct device_attribute *attr, | |
684 | char *buf) | |
685 | { | |
686 | struct i2c_client *client = dev_get_drvdata(dev); | |
687 | int ret, interval_ms; | |
688 | u8 eectrl; | |
689 | ||
690 | ret = rv3029_eeprom_read(client, RV3029_CONTROL_E2P_EECTRL, | |
691 | &eectrl, 1); | |
692 | if (ret < 0) | |
693 | return ret; | |
694 | ||
695 | if (eectrl & RV3029_EECTRL_THE) { | |
696 | if (eectrl & RV3029_EECTRL_THP) | |
697 | interval_ms = 16000; | |
698 | else | |
699 | interval_ms = 1000; | |
700 | } else { | |
701 | interval_ms = 0; | |
702 | } | |
703 | ||
704 | return sprintf(buf, "%d\n", interval_ms); | |
705 | } | |
706 | ||
707 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp, | |
708 | NULL, 0); | |
709 | static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, | |
710 | rv3029_hwmon_show_update_interval, | |
711 | rv3029_hwmon_set_update_interval, 0); | |
712 | ||
713 | static struct attribute *rv3029_hwmon_attrs[] = { | |
714 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
715 | &sensor_dev_attr_update_interval.dev_attr.attr, | |
716 | NULL, | |
717 | }; | |
718 | ATTRIBUTE_GROUPS(rv3029_hwmon); | |
719 | ||
720 | static void rv3029_hwmon_register(struct i2c_client *client) | |
721 | { | |
722 | struct device *hwmon_dev; | |
723 | ||
724 | hwmon_dev = devm_hwmon_device_register_with_groups( | |
725 | &client->dev, client->name, client, rv3029_hwmon_groups); | |
726 | if (IS_ERR(hwmon_dev)) { | |
727 | dev_warn(&client->dev, | |
4e7f1a60 MJ |
728 | "unable to register hwmon device %ld\n", |
729 | PTR_ERR(hwmon_dev)); | |
a696b31e MB |
730 | } |
731 | } | |
732 | ||
733 | #else /* CONFIG_RTC_DRV_RV3029_HWMON */ | |
734 | ||
735 | static void rv3029_hwmon_register(struct i2c_client *client) | |
736 | { | |
737 | } | |
738 | ||
739 | #endif /* CONFIG_RTC_DRV_RV3029_HWMON */ | |
740 | ||
aba39d27 MB |
741 | static const struct rtc_class_ops rv3029_rtc_ops = { |
742 | .read_time = rv3029_rtc_read_time, | |
743 | .set_time = rv3029_rtc_set_time, | |
744 | .read_alarm = rv3029_rtc_read_alarm, | |
745 | .set_alarm = rv3029_rtc_set_alarm, | |
52365230 HS |
746 | }; |
747 | ||
aba39d27 | 748 | static struct i2c_device_id rv3029_id[] = { |
baba623f | 749 | { "rv3029", 0 }, |
52365230 HS |
750 | { "rv3029c2", 0 }, |
751 | { } | |
752 | }; | |
aba39d27 | 753 | MODULE_DEVICE_TABLE(i2c, rv3029_id); |
52365230 | 754 | |
aba39d27 MB |
755 | static int rv3029_probe(struct i2c_client *client, |
756 | const struct i2c_device_id *id) | |
52365230 HS |
757 | { |
758 | struct rtc_device *rtc; | |
759 | int rc = 0; | |
760 | u8 buf[1]; | |
761 | ||
762 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL)) | |
763 | return -ENODEV; | |
764 | ||
4e7f1a60 | 765 | rc = rv3029_get_sr(client, buf); |
67ab2440 GH |
766 | if (rc < 0) { |
767 | dev_err(&client->dev, "reading status failed\n"); | |
768 | return rc; | |
769 | } | |
770 | ||
e27e2160 | 771 | rv3029_trickle_config(client); |
a696b31e | 772 | rv3029_hwmon_register(client); |
e27e2160 | 773 | |
3d7068c9 | 774 | rtc = devm_rtc_device_register(&client->dev, client->name, |
aba39d27 | 775 | &rv3029_rtc_ops, THIS_MODULE); |
52365230 HS |
776 | |
777 | if (IS_ERR(rtc)) | |
778 | return PTR_ERR(rtc); | |
779 | ||
780 | i2c_set_clientdata(client, rtc); | |
781 | ||
52365230 | 782 | return 0; |
52365230 HS |
783 | } |
784 | ||
aba39d27 | 785 | static struct i2c_driver rv3029_driver = { |
52365230 HS |
786 | .driver = { |
787 | .name = "rtc-rv3029c2", | |
788 | }, | |
aba39d27 MB |
789 | .probe = rv3029_probe, |
790 | .id_table = rv3029_id, | |
52365230 HS |
791 | }; |
792 | ||
aba39d27 | 793 | module_i2c_driver(rv3029_driver); |
52365230 HS |
794 | |
795 | MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>"); | |
2dca3d9e | 796 | MODULE_AUTHOR("Michael Buesch <m@bues.ch>"); |
aba39d27 | 797 | MODULE_DESCRIPTION("Micro Crystal RV3029 RTC driver"); |
52365230 | 798 | MODULE_LICENSE("GPL"); |