blob: d0cbf08040cd1e193bfd458b7e673d8ff01c53de [file] [log] [blame]
Heiko Schocher52365232011-05-26 16:25:05 -07001/*
Michael Büschaba39d22016-03-04 22:38:45 +01002 * Micro Crystal RV-3029 rtc class driver
Heiko Schocher52365232011-05-26 16:25:05 -07003 *
4 * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
Michael Büsch2dca3d92016-03-04 22:40:30 +01005 * Michael Buesch <m@bues.ch>
Heiko Schocher52365232011-05-26 16:25:05 -07006 *
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 *
Heiko Schocher52365232011-05-26 16:25:05 -070013 */
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/bcd.h>
18#include <linux/rtc.h>
Michael Büscha7f6e282016-03-04 22:40:55 +010019#include <linux/delay.h>
20#include <linux/of.h>
Michael Büscha696b312016-03-10 18:34:46 +010021#include <linux/hwmon.h>
22#include <linux/hwmon-sysfs.h>
Michael Büscha7f6e282016-03-04 22:40:55 +010023
Heiko Schocher52365232011-05-26 16:25:05 -070024
25/* Register map */
26/* control section */
Michael Büschaba39d22016-03-04 22:38:45 +010027#define RV3029_ONOFF_CTRL 0x00
Michael Büsch7697de32016-03-04 22:39:49 +010028#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)
Michael Büschaba39d22016-03-04 22:38:45 +010036#define RV3029_IRQ_CTRL 0x01
Michael Büsch7697de32016-03-04 22:39:49 +010037#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)
Michael Büschaba39d22016-03-04 22:38:45 +010042#define RV3029_IRQ_FLAGS 0x02
Michael Büsch7697de32016-03-04 22:39:49 +010043#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)
Michael Büschaba39d22016-03-04 22:38:45 +010048#define RV3029_STATUS 0x03
Michael Büsch7697de32016-03-04 22:39:49 +010049#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)
Michael Büschaba39d22016-03-04 22:38:45 +010054#define RV3029_RST_CTRL 0x04
Michael Büsch7697de32016-03-04 22:39:49 +010055#define RV3029_RST_CTRL_SYSR BIT(4)
Michael Büschaba39d22016-03-04 22:38:45 +010056#define RV3029_CONTROL_SECTION_LEN 0x05
Heiko Schocher52365232011-05-26 16:25:05 -070057
58/* watch section */
Michael Büschaba39d22016-03-04 22:38:45 +010059#define RV3029_W_SEC 0x08
60#define RV3029_W_MINUTES 0x09
61#define RV3029_W_HOURS 0x0A
Michael Büsch7697de32016-03-04 22:39:49 +010062#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 */
Michael Büschaba39d22016-03-04 22:38:45 +010064#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
Heiko Schocher52365232011-05-26 16:25:05 -070069
70/* alarm section */
Michael Büschaba39d22016-03-04 22:38:45 +010071#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
Heiko Schocher52365232011-05-26 16:25:05 -070079
80/* timer section */
Michael Büschaba39d22016-03-04 22:38:45 +010081#define RV3029_TIMER_LOW 0x18
82#define RV3029_TIMER_HIGH 0x19
Heiko Schocher52365232011-05-26 16:25:05 -070083
84/* temperature section */
Michael Büschaba39d22016-03-04 22:38:45 +010085#define RV3029_TEMP_PAGE 0x20
Heiko Schocher52365232011-05-26 16:25:05 -070086
87/* eeprom data section */
Michael Büschaba39d22016-03-04 22:38:45 +010088#define RV3029_E2P_EEDATA1 0x28
89#define RV3029_E2P_EEDATA2 0x29
Michael Büsch7697de32016-03-04 22:39:49 +010090#define RV3029_E2PDATA_SECTION_LEN 0x02
Heiko Schocher52365232011-05-26 16:25:05 -070091
92/* eeprom control section */
Michael Büschaba39d22016-03-04 22:38:45 +010093#define RV3029_CONTROL_E2P_EECTRL 0x30
Michael Büsch7697de32016-03-04 22:39:49 +010094#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 */
Heiko Schocher52365232011-05-26 16:25:05 -0700112
113/* user ram section */
Michael Büschaba39d22016-03-04 22:38:45 +0100114#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
Heiko Schocher52365232011-05-26 16:25:05 -0700118
119static int
Michael Büschaba39d22016-03-04 22:38:45 +0100120rv3029_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf,
121 unsigned len)
Heiko Schocher52365232011-05-26 16:25:05 -0700122{
123 int ret;
124
Michael Büschaba39d22016-03-04 22:38:45 +0100125 if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
126 (reg + len > RV3029_USR1_RAM_PAGE + 8))
Heiko Schocher52365232011-05-26 16:25:05 -0700127 return -EINVAL;
128
129 ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
130 if (ret < 0)
131 return ret;
132 if (ret < len)
133 return -EIO;
134 return 0;
135}
136
137static int
Michael Büschaba39d22016-03-04 22:38:45 +0100138rv3029_i2c_write_regs(struct i2c_client *client, u8 reg, u8 const buf[],
139 unsigned len)
Heiko Schocher52365232011-05-26 16:25:05 -0700140{
Michael Büschaba39d22016-03-04 22:38:45 +0100141 if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
142 (reg + len > RV3029_USR1_RAM_PAGE + 8))
Heiko Schocher52365232011-05-26 16:25:05 -0700143 return -EINVAL;
144
145 return i2c_smbus_write_i2c_block_data(client, reg, len, buf);
146}
147
148static int
Michael Büsch2dca3d92016-03-04 22:40:30 +0100149rv3029_i2c_update_bits(struct i2c_client *client, u8 reg, u8 mask, u8 set)
150{
151 u8 buf;
152 int ret;
153
154 ret = rv3029_i2c_read_regs(client, reg, &buf, 1);
155 if (ret < 0)
156 return ret;
157 buf &= ~mask;
158 buf |= set & mask;
159 ret = rv3029_i2c_write_regs(client, reg, &buf, 1);
160 if (ret < 0)
161 return ret;
162
163 return 0;
164}
165
166static int
Michael Büschaba39d22016-03-04 22:38:45 +0100167rv3029_i2c_get_sr(struct i2c_client *client, u8 *buf)
Heiko Schocher52365232011-05-26 16:25:05 -0700168{
Michael Büschaba39d22016-03-04 22:38:45 +0100169 int ret = rv3029_i2c_read_regs(client, RV3029_STATUS, buf, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700170
171 if (ret < 0)
172 return -EIO;
173 dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
174 return 0;
175}
176
177static int
Michael Büschaba39d22016-03-04 22:38:45 +0100178rv3029_i2c_set_sr(struct i2c_client *client, u8 val)
Heiko Schocher52365232011-05-26 16:25:05 -0700179{
180 u8 buf[1];
181 int sr;
182
183 buf[0] = val;
Michael Büschaba39d22016-03-04 22:38:45 +0100184 sr = rv3029_i2c_write_regs(client, RV3029_STATUS, buf, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700185 dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
186 if (sr < 0)
187 return -EIO;
188 return 0;
189}
190
Michael Büscha7f6e282016-03-04 22:40:55 +0100191static int rv3029_eeprom_busywait(struct i2c_client *client)
192{
193 int i, ret;
194 u8 sr;
195
196 for (i = 100; i > 0; i--) {
197 ret = rv3029_i2c_get_sr(client, &sr);
198 if (ret < 0)
199 break;
200 if (!(sr & RV3029_STATUS_EEBUSY))
201 break;
202 usleep_range(1000, 10000);
203 }
204 if (i <= 0) {
205 dev_err(&client->dev, "EEPROM busy wait timeout.\n");
206 return -ETIMEDOUT;
207 }
208
209 return ret;
210}
211
212static int rv3029_eeprom_exit(struct i2c_client *client)
213{
214 /* Re-enable eeprom refresh */
215 return rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL,
216 RV3029_ONOFF_CTRL_EERE,
217 RV3029_ONOFF_CTRL_EERE);
218}
219
220static int rv3029_eeprom_enter(struct i2c_client *client)
221{
222 int ret;
223 u8 sr;
224
225 /* Check whether we are in the allowed voltage range. */
226 ret = rv3029_i2c_get_sr(client, &sr);
227 if (ret < 0)
228 return ret;
229 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
230 /* We clear the bits and retry once just in case
231 * we had a brown out in early startup.
232 */
233 sr &= ~RV3029_STATUS_VLOW1;
234 sr &= ~RV3029_STATUS_VLOW2;
235 ret = rv3029_i2c_set_sr(client, sr);
236 if (ret < 0)
237 return ret;
238 usleep_range(1000, 10000);
239 ret = rv3029_i2c_get_sr(client, &sr);
240 if (ret < 0)
241 return ret;
242 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
243 dev_err(&client->dev,
244 "Supply voltage is too low to safely access the EEPROM.\n");
245 return -ENODEV;
246 }
247 }
248
249 /* Disable eeprom refresh. */
250 ret = rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL,
251 RV3029_ONOFF_CTRL_EERE, 0);
252 if (ret < 0)
253 return ret;
254
255 /* Wait for any previous eeprom accesses to finish. */
256 ret = rv3029_eeprom_busywait(client);
257 if (ret < 0)
258 rv3029_eeprom_exit(client);
259
260 return ret;
261}
262
263static int rv3029_eeprom_read(struct i2c_client *client, u8 reg,
264 u8 buf[], size_t len)
265{
266 int ret, err;
267
268 err = rv3029_eeprom_enter(client);
269 if (err < 0)
270 return err;
271
272 ret = rv3029_i2c_read_regs(client, reg, buf, len);
273
274 err = rv3029_eeprom_exit(client);
275 if (err < 0)
276 return err;
277
278 return ret;
279}
280
281static int rv3029_eeprom_write(struct i2c_client *client, u8 reg,
282 u8 const buf[], size_t len)
283{
284 int ret, err;
285 size_t i;
286 u8 tmp;
287
288 err = rv3029_eeprom_enter(client);
289 if (err < 0)
290 return err;
291
292 for (i = 0; i < len; i++, reg++) {
293 ret = rv3029_i2c_read_regs(client, reg, &tmp, 1);
294 if (ret < 0)
295 break;
296 if (tmp != buf[i]) {
297 ret = rv3029_i2c_write_regs(client, reg, &buf[i], 1);
298 if (ret < 0)
299 break;
300 }
301 ret = rv3029_eeprom_busywait(client);
302 if (ret < 0)
303 break;
304 }
305
306 err = rv3029_eeprom_exit(client);
307 if (err < 0)
308 return err;
309
310 return ret;
311}
312
Michael Büsch39387dc22016-03-10 18:34:23 +0100313static int rv3029_eeprom_update_bits(struct i2c_client *client,
314 u8 reg, u8 mask, u8 set)
315{
316 u8 buf;
317 int ret;
318
319 ret = rv3029_eeprom_read(client, reg, &buf, 1);
320 if (ret < 0)
321 return ret;
322 buf &= ~mask;
323 buf |= set & mask;
324 ret = rv3029_eeprom_write(client, reg, &buf, 1);
325 if (ret < 0)
326 return ret;
327
328 return 0;
329}
330
Heiko Schocher52365232011-05-26 16:25:05 -0700331static int
Michael Büschaba39d22016-03-04 22:38:45 +0100332rv3029_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700333{
334 u8 buf[1];
335 int ret;
Michael Büschaba39d22016-03-04 22:38:45 +0100336 u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
Heiko Schocher52365232011-05-26 16:25:05 -0700337
Michael Büschaba39d22016-03-04 22:38:45 +0100338 ret = rv3029_i2c_get_sr(client, buf);
Heiko Schocher52365232011-05-26 16:25:05 -0700339 if (ret < 0) {
340 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
341 return -EIO;
342 }
343
Michael Büschaba39d22016-03-04 22:38:45 +0100344 ret = rv3029_i2c_read_regs(client, RV3029_W_SEC, regs,
345 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700346 if (ret < 0) {
347 dev_err(&client->dev, "%s: reading RTC section failed\n",
348 __func__);
349 return ret;
350 }
351
Michael Büschaba39d22016-03-04 22:38:45 +0100352 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC-RV3029_W_SEC]);
353 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES-RV3029_W_SEC]);
Heiko Schocher52365232011-05-26 16:25:05 -0700354
355 /* HR field has a more complex interpretation */
356 {
Michael Büschaba39d22016-03-04 22:38:45 +0100357 const u8 _hr = regs[RV3029_W_HOURS-RV3029_W_SEC];
358
359 if (_hr & RV3029_REG_HR_12_24) {
Heiko Schocher52365232011-05-26 16:25:05 -0700360 /* 12h format */
361 tm->tm_hour = bcd2bin(_hr & 0x1f);
Michael Büschaba39d22016-03-04 22:38:45 +0100362 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
Heiko Schocher52365232011-05-26 16:25:05 -0700363 tm->tm_hour += 12;
364 } else /* 24h format */
365 tm->tm_hour = bcd2bin(_hr & 0x3f);
366 }
367
Michael Büschaba39d22016-03-04 22:38:45 +0100368 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE-RV3029_W_SEC]);
369 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS-RV3029_W_SEC]) - 1;
370 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS-RV3029_W_SEC]) + 100;
371 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS-RV3029_W_SEC]) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700372
373 return 0;
374}
375
Michael Büschaba39d22016-03-04 22:38:45 +0100376static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700377{
Michael Büschaba39d22016-03-04 22:38:45 +0100378 return rv3029_i2c_read_time(to_i2c_client(dev), tm);
Heiko Schocher52365232011-05-26 16:25:05 -0700379}
380
381static int
Michael Büschaba39d22016-03-04 22:38:45 +0100382rv3029_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700383{
384 struct rtc_time *const tm = &alarm->time;
385 int ret;
386 u8 regs[8];
387
Michael Büschaba39d22016-03-04 22:38:45 +0100388 ret = rv3029_i2c_get_sr(client, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700389 if (ret < 0) {
390 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
391 return -EIO;
392 }
393
Michael Büschaba39d22016-03-04 22:38:45 +0100394 ret = rv3029_i2c_read_regs(client, RV3029_A_SC, regs,
395 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700396
397 if (ret < 0) {
398 dev_err(&client->dev, "%s: reading alarm section failed\n",
399 __func__);
400 return ret;
401 }
402
Michael Büschaba39d22016-03-04 22:38:45 +0100403 tm->tm_sec = bcd2bin(regs[RV3029_A_SC-RV3029_A_SC] & 0x7f);
404 tm->tm_min = bcd2bin(regs[RV3029_A_MN-RV3029_A_SC] & 0x7f);
405 tm->tm_hour = bcd2bin(regs[RV3029_A_HR-RV3029_A_SC] & 0x3f);
406 tm->tm_mday = bcd2bin(regs[RV3029_A_DT-RV3029_A_SC] & 0x3f);
407 tm->tm_mon = bcd2bin(regs[RV3029_A_MO-RV3029_A_SC] & 0x1f) - 1;
408 tm->tm_year = bcd2bin(regs[RV3029_A_YR-RV3029_A_SC] & 0x7f) + 100;
409 tm->tm_wday = bcd2bin(regs[RV3029_A_DW-RV3029_A_SC] & 0x07) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700410
411 return 0;
412}
413
414static int
Michael Büschaba39d22016-03-04 22:38:45 +0100415rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700416{
Michael Büschaba39d22016-03-04 22:38:45 +0100417 return rv3029_i2c_read_alarm(to_i2c_client(dev), alarm);
Heiko Schocher52365232011-05-26 16:25:05 -0700418}
419
Michael Büschaba39d22016-03-04 22:38:45 +0100420static int rv3029_rtc_i2c_alarm_set_irq(struct i2c_client *client,
Heiko Schocher52365232011-05-26 16:25:05 -0700421 int enable)
422{
423 int ret;
Heiko Schocher52365232011-05-26 16:25:05 -0700424
Michael Büsch2dca3d92016-03-04 22:40:30 +0100425 /* enable/disable AIE irq */
426 ret = rv3029_i2c_update_bits(client, RV3029_IRQ_CTRL,
427 RV3029_IRQ_CTRL_AIE,
428 (enable ? RV3029_IRQ_CTRL_AIE : 0));
Heiko Schocher52365232011-05-26 16:25:05 -0700429 if (ret < 0) {
Michael Büsch2dca3d92016-03-04 22:40:30 +0100430 dev_err(&client->dev, "can't update INT reg\n");
Heiko Schocher52365232011-05-26 16:25:05 -0700431 return ret;
432 }
433
434 return 0;
435}
436
Michael Büschaba39d22016-03-04 22:38:45 +0100437static int rv3029_rtc_i2c_set_alarm(struct i2c_client *client,
438 struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700439{
440 struct rtc_time *const tm = &alarm->time;
441 int ret;
442 u8 regs[8];
443
444 /*
445 * The clock has an 8 bit wide bcd-coded register (they never learn)
446 * for the year. tm_year is an offset from 1900 and we are interested
447 * in the 2000-2099 range, so any value less than 100 is invalid.
448 */
449 if (tm->tm_year < 100)
450 return -EINVAL;
451
Michael Büschaba39d22016-03-04 22:38:45 +0100452 ret = rv3029_i2c_get_sr(client, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700453 if (ret < 0) {
454 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
455 return -EIO;
456 }
Michael Büschaba39d22016-03-04 22:38:45 +0100457 regs[RV3029_A_SC-RV3029_A_SC] = bin2bcd(tm->tm_sec & 0x7f);
458 regs[RV3029_A_MN-RV3029_A_SC] = bin2bcd(tm->tm_min & 0x7f);
459 regs[RV3029_A_HR-RV3029_A_SC] = bin2bcd(tm->tm_hour & 0x3f);
460 regs[RV3029_A_DT-RV3029_A_SC] = bin2bcd(tm->tm_mday & 0x3f);
461 regs[RV3029_A_MO-RV3029_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1);
462 regs[RV3029_A_DW-RV3029_A_SC] = bin2bcd((tm->tm_wday & 7) - 1);
463 regs[RV3029_A_YR-RV3029_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100);
Heiko Schocher52365232011-05-26 16:25:05 -0700464
Michael Büschaba39d22016-03-04 22:38:45 +0100465 ret = rv3029_i2c_write_regs(client, RV3029_A_SC, regs,
466 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700467 if (ret < 0)
468 return ret;
469
470 if (alarm->enabled) {
Heiko Schocher52365232011-05-26 16:25:05 -0700471 /* clear AF flag */
Michael Büsch2dca3d92016-03-04 22:40:30 +0100472 ret = rv3029_i2c_update_bits(client, RV3029_IRQ_FLAGS,
473 RV3029_IRQ_FLAGS_AF, 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700474 if (ret < 0) {
Michael Büsch2dca3d92016-03-04 22:40:30 +0100475 dev_err(&client->dev, "can't clear alarm flag\n");
Heiko Schocher52365232011-05-26 16:25:05 -0700476 return ret;
477 }
478 /* enable AIE irq */
Michael Büschaba39d22016-03-04 22:38:45 +0100479 ret = rv3029_rtc_i2c_alarm_set_irq(client, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700480 if (ret)
481 return ret;
482
483 dev_dbg(&client->dev, "alarm IRQ armed\n");
484 } else {
485 /* disable AIE irq */
Michael Büschaba39d22016-03-04 22:38:45 +0100486 ret = rv3029_rtc_i2c_alarm_set_irq(client, 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700487 if (ret)
488 return ret;
489
490 dev_dbg(&client->dev, "alarm IRQ disabled\n");
491 }
492
493 return 0;
494}
495
Michael Büschaba39d22016-03-04 22:38:45 +0100496static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700497{
Michael Büschaba39d22016-03-04 22:38:45 +0100498 return rv3029_rtc_i2c_set_alarm(to_i2c_client(dev), alarm);
Heiko Schocher52365232011-05-26 16:25:05 -0700499}
500
501static int
Michael Büschaba39d22016-03-04 22:38:45 +0100502rv3029_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700503{
504 u8 regs[8];
505 int ret;
506
507 /*
508 * The clock has an 8 bit wide bcd-coded register (they never learn)
509 * for the year. tm_year is an offset from 1900 and we are interested
510 * in the 2000-2099 range, so any value less than 100 is invalid.
511 */
512 if (tm->tm_year < 100)
513 return -EINVAL;
514
Michael Büschaba39d22016-03-04 22:38:45 +0100515 regs[RV3029_W_SEC-RV3029_W_SEC] = bin2bcd(tm->tm_sec);
516 regs[RV3029_W_MINUTES-RV3029_W_SEC] = bin2bcd(tm->tm_min);
517 regs[RV3029_W_HOURS-RV3029_W_SEC] = bin2bcd(tm->tm_hour);
518 regs[RV3029_W_DATE-RV3029_W_SEC] = bin2bcd(tm->tm_mday);
519 regs[RV3029_W_MONTHS-RV3029_W_SEC] = bin2bcd(tm->tm_mon+1);
520 regs[RV3029_W_DAYS-RV3029_W_SEC] = bin2bcd((tm->tm_wday & 7)+1);
521 regs[RV3029_W_YEARS-RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
Heiko Schocher52365232011-05-26 16:25:05 -0700522
Michael Büschaba39d22016-03-04 22:38:45 +0100523 ret = rv3029_i2c_write_regs(client, RV3029_W_SEC, regs,
524 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700525 if (ret < 0)
526 return ret;
527
Michael Büschaba39d22016-03-04 22:38:45 +0100528 ret = rv3029_i2c_get_sr(client, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700529 if (ret < 0) {
530 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
531 return ret;
532 }
533 /* clear PON bit */
Michael Büschaba39d22016-03-04 22:38:45 +0100534 ret = rv3029_i2c_set_sr(client, (regs[0] & ~RV3029_STATUS_PON));
Heiko Schocher52365232011-05-26 16:25:05 -0700535 if (ret < 0) {
536 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
537 return ret;
538 }
539
540 return 0;
541}
542
Michael Büschaba39d22016-03-04 22:38:45 +0100543static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700544{
Michael Büschaba39d22016-03-04 22:38:45 +0100545 return rv3029_i2c_set_time(to_i2c_client(dev), tm);
Heiko Schocher52365232011-05-26 16:25:05 -0700546}
547
Michael Büsche27e2162016-03-04 22:41:19 +0100548static const struct rv3029_trickle_tab_elem {
549 u32 r; /* resistance in ohms */
550 u8 conf; /* trickle config bits */
551} rv3029_trickle_tab[] = {
552 {
553 .r = 1076,
554 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
555 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
556 }, {
557 .r = 1091,
558 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
559 RV3029_TRICKLE_20K,
560 }, {
561 .r = 1137,
562 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
563 RV3029_TRICKLE_80K,
564 }, {
565 .r = 1154,
566 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
567 }, {
568 .r = 1371,
569 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
570 RV3029_TRICKLE_80K,
571 }, {
572 .r = 1395,
573 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
574 }, {
575 .r = 1472,
576 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
577 }, {
578 .r = 1500,
579 .conf = RV3029_TRICKLE_1K,
580 }, {
581 .r = 3810,
582 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
583 RV3029_TRICKLE_80K,
584 }, {
585 .r = 4000,
586 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
587 }, {
588 .r = 4706,
589 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
590 }, {
591 .r = 5000,
592 .conf = RV3029_TRICKLE_5K,
593 }, {
594 .r = 16000,
595 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
596 }, {
597 .r = 20000,
598 .conf = RV3029_TRICKLE_20K,
599 }, {
600 .r = 80000,
601 .conf = RV3029_TRICKLE_80K,
602 },
603};
604
605static void rv3029_trickle_config(struct i2c_client *client)
606{
607 struct device_node *of_node = client->dev.of_node;
608 const struct rv3029_trickle_tab_elem *elem;
609 int i, err;
610 u32 ohms;
Michael Büsch39387dc22016-03-10 18:34:23 +0100611 u8 trickle_set_bits;
Michael Büsche27e2162016-03-04 22:41:19 +0100612
613 if (!of_node)
614 return;
615
616 /* Configure the trickle charger. */
Michael Büsche27e2162016-03-04 22:41:19 +0100617 err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
618 if (err) {
619 /* Disable trickle charger. */
Michael Büsch39387dc22016-03-10 18:34:23 +0100620 trickle_set_bits = 0;
Michael Büsche27e2162016-03-04 22:41:19 +0100621 } else {
622 /* Enable trickle charger. */
623 for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) {
624 elem = &rv3029_trickle_tab[i];
625 if (elem->r >= ohms)
626 break;
627 }
Michael Büsch39387dc22016-03-10 18:34:23 +0100628 trickle_set_bits = elem->conf;
Michael Büsche27e2162016-03-04 22:41:19 +0100629 dev_info(&client->dev,
630 "Trickle charger enabled at %d ohms resistance.\n",
631 elem->r);
632 }
Michael Büsch39387dc22016-03-10 18:34:23 +0100633 err = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL,
634 RV3029_TRICKLE_MASK,
635 trickle_set_bits);
Michael Büsche27e2162016-03-04 22:41:19 +0100636 if (err < 0) {
637 dev_err(&client->dev,
Michael Büsch39387dc22016-03-10 18:34:23 +0100638 "Failed to update trickle charger config\n");
Michael Büsche27e2162016-03-04 22:41:19 +0100639 }
640}
641
Michael Büscha696b312016-03-10 18:34:46 +0100642#ifdef CONFIG_RTC_DRV_RV3029_HWMON
643
644static int rv3029_read_temp(struct i2c_client *client, int *temp_mC)
645{
646 int ret;
647 u8 temp;
648
649 ret = rv3029_i2c_read_regs(client, RV3029_TEMP_PAGE, &temp, 1);
650 if (ret < 0)
651 return ret;
652
653 *temp_mC = ((int)temp - 60) * 1000;
654
655 return 0;
656}
657
658static ssize_t rv3029_hwmon_show_temp(struct device *dev,
659 struct device_attribute *attr,
660 char *buf)
661{
662 struct i2c_client *client = dev_get_drvdata(dev);
663 int ret, temp_mC;
664
665 ret = rv3029_read_temp(client, &temp_mC);
666 if (ret < 0)
667 return ret;
668
669 return sprintf(buf, "%d\n", temp_mC);
670}
671
672static ssize_t rv3029_hwmon_set_update_interval(struct device *dev,
673 struct device_attribute *attr,
674 const char *buf,
675 size_t count)
676{
677 struct i2c_client *client = dev_get_drvdata(dev);
678 unsigned long interval_ms;
679 int ret;
680 u8 th_set_bits = 0;
681
682 ret = kstrtoul(buf, 10, &interval_ms);
683 if (ret < 0)
684 return ret;
685
686 if (interval_ms != 0) {
687 th_set_bits |= RV3029_EECTRL_THE;
688 if (interval_ms >= 16000)
689 th_set_bits |= RV3029_EECTRL_THP;
690 }
691 ret = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL,
692 RV3029_EECTRL_THE | RV3029_EECTRL_THP,
693 th_set_bits);
694 if (ret < 0)
695 return ret;
696
697 return count;
698}
699
700static ssize_t rv3029_hwmon_show_update_interval(struct device *dev,
701 struct device_attribute *attr,
702 char *buf)
703{
704 struct i2c_client *client = dev_get_drvdata(dev);
705 int ret, interval_ms;
706 u8 eectrl;
707
708 ret = rv3029_eeprom_read(client, RV3029_CONTROL_E2P_EECTRL,
709 &eectrl, 1);
710 if (ret < 0)
711 return ret;
712
713 if (eectrl & RV3029_EECTRL_THE) {
714 if (eectrl & RV3029_EECTRL_THP)
715 interval_ms = 16000;
716 else
717 interval_ms = 1000;
718 } else {
719 interval_ms = 0;
720 }
721
722 return sprintf(buf, "%d\n", interval_ms);
723}
724
725static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp,
726 NULL, 0);
727static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
728 rv3029_hwmon_show_update_interval,
729 rv3029_hwmon_set_update_interval, 0);
730
731static struct attribute *rv3029_hwmon_attrs[] = {
732 &sensor_dev_attr_temp1_input.dev_attr.attr,
733 &sensor_dev_attr_update_interval.dev_attr.attr,
734 NULL,
735};
736ATTRIBUTE_GROUPS(rv3029_hwmon);
737
738static void rv3029_hwmon_register(struct i2c_client *client)
739{
740 struct device *hwmon_dev;
741
742 hwmon_dev = devm_hwmon_device_register_with_groups(
743 &client->dev, client->name, client, rv3029_hwmon_groups);
744 if (IS_ERR(hwmon_dev)) {
745 dev_warn(&client->dev,
746 "unable to register hwmon device %ld\n",
747 PTR_ERR(hwmon_dev));
748 }
749}
750
751#else /* CONFIG_RTC_DRV_RV3029_HWMON */
752
753static void rv3029_hwmon_register(struct i2c_client *client)
754{
755}
756
757#endif /* CONFIG_RTC_DRV_RV3029_HWMON */
758
Michael Büschaba39d22016-03-04 22:38:45 +0100759static const struct rtc_class_ops rv3029_rtc_ops = {
760 .read_time = rv3029_rtc_read_time,
761 .set_time = rv3029_rtc_set_time,
762 .read_alarm = rv3029_rtc_read_alarm,
763 .set_alarm = rv3029_rtc_set_alarm,
Heiko Schocher52365232011-05-26 16:25:05 -0700764};
765
Michael Büschaba39d22016-03-04 22:38:45 +0100766static struct i2c_device_id rv3029_id[] = {
Michael Büschbaba6232016-03-04 22:39:17 +0100767 { "rv3029", 0 },
Heiko Schocher52365232011-05-26 16:25:05 -0700768 { "rv3029c2", 0 },
769 { }
770};
Michael Büschaba39d22016-03-04 22:38:45 +0100771MODULE_DEVICE_TABLE(i2c, rv3029_id);
Heiko Schocher52365232011-05-26 16:25:05 -0700772
Michael Büschaba39d22016-03-04 22:38:45 +0100773static int rv3029_probe(struct i2c_client *client,
774 const struct i2c_device_id *id)
Heiko Schocher52365232011-05-26 16:25:05 -0700775{
776 struct rtc_device *rtc;
777 int rc = 0;
778 u8 buf[1];
779
780 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL))
781 return -ENODEV;
782
Michael Büschaba39d22016-03-04 22:38:45 +0100783 rc = rv3029_i2c_get_sr(client, buf);
Gregory Hermant67ab2442014-04-03 14:50:17 -0700784 if (rc < 0) {
785 dev_err(&client->dev, "reading status failed\n");
786 return rc;
787 }
788
Michael Büsche27e2162016-03-04 22:41:19 +0100789 rv3029_trickle_config(client);
Michael Büscha696b312016-03-10 18:34:46 +0100790 rv3029_hwmon_register(client);
Michael Büsche27e2162016-03-04 22:41:19 +0100791
Jingoo Han3d7068c2013-04-29 16:19:48 -0700792 rtc = devm_rtc_device_register(&client->dev, client->name,
Michael Büschaba39d22016-03-04 22:38:45 +0100793 &rv3029_rtc_ops, THIS_MODULE);
Heiko Schocher52365232011-05-26 16:25:05 -0700794
795 if (IS_ERR(rtc))
796 return PTR_ERR(rtc);
797
798 i2c_set_clientdata(client, rtc);
799
Heiko Schocher52365232011-05-26 16:25:05 -0700800 return 0;
Heiko Schocher52365232011-05-26 16:25:05 -0700801}
802
Michael Büschaba39d22016-03-04 22:38:45 +0100803static struct i2c_driver rv3029_driver = {
Heiko Schocher52365232011-05-26 16:25:05 -0700804 .driver = {
805 .name = "rtc-rv3029c2",
806 },
Michael Büschaba39d22016-03-04 22:38:45 +0100807 .probe = rv3029_probe,
808 .id_table = rv3029_id,
Heiko Schocher52365232011-05-26 16:25:05 -0700809};
810
Michael Büschaba39d22016-03-04 22:38:45 +0100811module_i2c_driver(rv3029_driver);
Heiko Schocher52365232011-05-26 16:25:05 -0700812
813MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
Michael Büsch2dca3d92016-03-04 22:40:30 +0100814MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
Michael Büschaba39d22016-03-04 22:38:45 +0100815MODULE_DESCRIPTION("Micro Crystal RV3029 RTC driver");
Heiko Schocher52365232011-05-26 16:25:05 -0700816MODULE_LICENSE("GPL");