blob: 091be483e3e9fe53a71423b4d469116ec0cc57d1 [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
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200119static int rv3029_read_regs(struct i2c_client *client, u8 reg, u8 *buf,
120 unsigned len)
Heiko Schocher52365232011-05-26 16:25:05 -0700121{
122 int ret;
123
Michael Büschaba39d22016-03-04 22:38:45 +0100124 if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
125 (reg + len > RV3029_USR1_RAM_PAGE + 8))
Heiko Schocher52365232011-05-26 16:25:05 -0700126 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
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200136static int rv3029_write_regs(struct i2c_client *client, u8 reg, u8 const buf[],
137 unsigned len)
Heiko Schocher52365232011-05-26 16:25:05 -0700138{
Michael Büschaba39d22016-03-04 22:38:45 +0100139 if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
140 (reg + len > RV3029_USR1_RAM_PAGE + 8))
Heiko Schocher52365232011-05-26 16:25:05 -0700141 return -EINVAL;
142
143 return i2c_smbus_write_i2c_block_data(client, reg, len, buf);
144}
145
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200146static int rv3029_update_bits(struct i2c_client *client, u8 reg, u8 mask,
147 u8 set)
Michael Büsch2dca3d92016-03-04 22:40:30 +0100148{
149 u8 buf;
150 int ret;
151
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200152 ret = rv3029_read_regs(client, reg, &buf, 1);
Michael Büsch2dca3d92016-03-04 22:40:30 +0100153 if (ret < 0)
154 return ret;
155 buf &= ~mask;
156 buf |= set & mask;
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200157 ret = rv3029_write_regs(client, reg, &buf, 1);
Michael Büsch2dca3d92016-03-04 22:40:30 +0100158 if (ret < 0)
159 return ret;
160
161 return 0;
162}
163
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200164static int rv3029_get_sr(struct i2c_client *client, u8 *buf)
Heiko Schocher52365232011-05-26 16:25:05 -0700165{
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200166 int ret = rv3029_read_regs(client, RV3029_STATUS, buf, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700167
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
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200174static int rv3029_set_sr(struct i2c_client *client, u8 val)
Heiko Schocher52365232011-05-26 16:25:05 -0700175{
176 u8 buf[1];
177 int sr;
178
179 buf[0] = val;
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200180 sr = rv3029_write_regs(client, RV3029_STATUS, buf, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700181 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
Michael Büscha7f6e282016-03-04 22:40:55 +0100187static int rv3029_eeprom_busywait(struct i2c_client *client)
188{
189 int i, ret;
190 u8 sr;
191
192 for (i = 100; i > 0; i--) {
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200193 ret = rv3029_get_sr(client, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100194 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
208static int rv3029_eeprom_exit(struct i2c_client *client)
209{
210 /* Re-enable eeprom refresh */
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200211 return rv3029_update_bits(client, RV3029_ONOFF_CTRL,
212 RV3029_ONOFF_CTRL_EERE,
213 RV3029_ONOFF_CTRL_EERE);
Michael Büscha7f6e282016-03-04 22:40:55 +0100214}
215
216static 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. */
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200222 ret = rv3029_get_sr(client, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100223 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;
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200231 ret = rv3029_set_sr(client, sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100232 if (ret < 0)
233 return ret;
234 usleep_range(1000, 10000);
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200235 ret = rv3029_get_sr(client, &sr);
Michael Büscha7f6e282016-03-04 22:40:55 +0100236 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. */
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200246 ret = rv3029_update_bits(client, RV3029_ONOFF_CTRL,
247 RV3029_ONOFF_CTRL_EERE, 0);
Michael Büscha7f6e282016-03-04 22:40:55 +0100248 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
259static 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
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200268 ret = rv3029_read_regs(client, reg, buf, len);
Michael Büscha7f6e282016-03-04 22:40:55 +0100269
270 err = rv3029_eeprom_exit(client);
271 if (err < 0)
272 return err;
273
274 return ret;
275}
276
277static 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++) {
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200289 ret = rv3029_read_regs(client, reg, &tmp, 1);
Michael Büscha7f6e282016-03-04 22:40:55 +0100290 if (ret < 0)
291 break;
292 if (tmp != buf[i]) {
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200293 ret = rv3029_write_regs(client, reg, &buf[i], 1);
Michael Büscha7f6e282016-03-04 22:40:55 +0100294 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
Michael Büsch39387dc22016-03-10 18:34:23 +0100309static 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
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200327static int rv3029_read_time(struct i2c_client *client, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700328{
329 u8 buf[1];
330 int ret;
Michael Büschaba39d22016-03-04 22:38:45 +0100331 u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
Heiko Schocher52365232011-05-26 16:25:05 -0700332
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200333 ret = rv3029_get_sr(client, buf);
Heiko Schocher52365232011-05-26 16:25:05 -0700334 if (ret < 0) {
335 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
336 return -EIO;
337 }
338
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200339 ret = rv3029_read_regs(client, RV3029_W_SEC, regs,
340 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700341 if (ret < 0) {
342 dev_err(&client->dev, "%s: reading RTC section failed\n",
343 __func__);
344 return ret;
345 }
346
Michael Büschaba39d22016-03-04 22:38:45 +0100347 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC-RV3029_W_SEC]);
348 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES-RV3029_W_SEC]);
Heiko Schocher52365232011-05-26 16:25:05 -0700349
350 /* HR field has a more complex interpretation */
351 {
Michael Büschaba39d22016-03-04 22:38:45 +0100352 const u8 _hr = regs[RV3029_W_HOURS-RV3029_W_SEC];
353
354 if (_hr & RV3029_REG_HR_12_24) {
Heiko Schocher52365232011-05-26 16:25:05 -0700355 /* 12h format */
356 tm->tm_hour = bcd2bin(_hr & 0x1f);
Michael Büschaba39d22016-03-04 22:38:45 +0100357 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
Heiko Schocher52365232011-05-26 16:25:05 -0700358 tm->tm_hour += 12;
359 } else /* 24h format */
360 tm->tm_hour = bcd2bin(_hr & 0x3f);
361 }
362
Michael Büschaba39d22016-03-04 22:38:45 +0100363 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;
Heiko Schocher52365232011-05-26 16:25:05 -0700367
368 return 0;
369}
370
Michael Büschaba39d22016-03-04 22:38:45 +0100371static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700372{
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200373 return rv3029_read_time(to_i2c_client(dev), tm);
Heiko Schocher52365232011-05-26 16:25:05 -0700374}
375
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200376static int rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700377{
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200378 struct i2c_client *client = to_i2c_client(dev);
Heiko Schocher52365232011-05-26 16:25:05 -0700379 struct rtc_time *const tm = &alarm->time;
380 int ret;
381 u8 regs[8];
382
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200383 ret = rv3029_get_sr(client, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700384 if (ret < 0) {
385 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
386 return -EIO;
387 }
388
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200389 ret = rv3029_read_regs(client, RV3029_A_SC, regs,
390 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700391
392 if (ret < 0) {
393 dev_err(&client->dev, "%s: reading alarm section failed\n",
394 __func__);
395 return ret;
396 }
397
Michael Büschaba39d22016-03-04 22:38:45 +0100398 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;
Heiko Schocher52365232011-05-26 16:25:05 -0700405
406 return 0;
407}
408
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200409static int rv3029_rtc_alarm_set_irq(struct i2c_client *client, int enable)
Heiko Schocher52365232011-05-26 16:25:05 -0700410{
411 int ret;
Heiko Schocher52365232011-05-26 16:25:05 -0700412
Michael Büsch2dca3d92016-03-04 22:40:30 +0100413 /* enable/disable AIE irq */
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200414 ret = rv3029_update_bits(client, RV3029_IRQ_CTRL,
415 RV3029_IRQ_CTRL_AIE,
416 (enable ? RV3029_IRQ_CTRL_AIE : 0));
Heiko Schocher52365232011-05-26 16:25:05 -0700417 if (ret < 0) {
Michael Büsch2dca3d92016-03-04 22:40:30 +0100418 dev_err(&client->dev, "can't update INT reg\n");
Heiko Schocher52365232011-05-26 16:25:05 -0700419 return ret;
420 }
421
422 return 0;
423}
424
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200425static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700426{
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200427 struct i2c_client *client = to_i2c_client(dev);
Heiko Schocher52365232011-05-26 16:25:05 -0700428 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
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200440 ret = rv3029_get_sr(client, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700441 if (ret < 0) {
442 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
443 return -EIO;
444 }
Michael Büschaba39d22016-03-04 22:38:45 +0100445 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);
Heiko Schocher52365232011-05-26 16:25:05 -0700452
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200453 ret = rv3029_write_regs(client, RV3029_A_SC, regs,
454 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700455 if (ret < 0)
456 return ret;
457
458 if (alarm->enabled) {
Heiko Schocher52365232011-05-26 16:25:05 -0700459 /* clear AF flag */
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200460 ret = rv3029_update_bits(client, RV3029_IRQ_FLAGS,
461 RV3029_IRQ_FLAGS_AF, 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700462 if (ret < 0) {
Michael Büsch2dca3d92016-03-04 22:40:30 +0100463 dev_err(&client->dev, "can't clear alarm flag\n");
Heiko Schocher52365232011-05-26 16:25:05 -0700464 return ret;
465 }
466 /* enable AIE irq */
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200467 ret = rv3029_rtc_alarm_set_irq(client, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700468 if (ret)
469 return ret;
470
471 dev_dbg(&client->dev, "alarm IRQ armed\n");
472 } else {
473 /* disable AIE irq */
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200474 ret = rv3029_rtc_alarm_set_irq(client, 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700475 if (ret)
476 return ret;
477
478 dev_dbg(&client->dev, "alarm IRQ disabled\n");
479 }
480
481 return 0;
482}
483
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200484static int rv3029_set_time(struct i2c_client *client, struct rtc_time const *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700485{
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
Michael Büschaba39d22016-03-04 22:38:45 +0100497 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);
Heiko Schocher52365232011-05-26 16:25:05 -0700504
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200505 ret = rv3029_write_regs(client, RV3029_W_SEC, regs,
506 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700507 if (ret < 0)
508 return ret;
509
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200510 ret = rv3029_get_sr(client, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700511 if (ret < 0) {
512 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
513 return ret;
514 }
515 /* clear PON bit */
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200516 ret = rv3029_set_sr(client, (regs[0] & ~RV3029_STATUS_PON));
Heiko Schocher52365232011-05-26 16:25:05 -0700517 if (ret < 0) {
518 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
519 return ret;
520 }
521
522 return 0;
523}
524
Michael Büschaba39d22016-03-04 22:38:45 +0100525static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700526{
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200527 return rv3029_set_time(to_i2c_client(dev), tm);
Heiko Schocher52365232011-05-26 16:25:05 -0700528}
529
Michael Büsche27e2162016-03-04 22:41:19 +0100530static 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
587static 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;
Michael Büsch39387dc22016-03-10 18:34:23 +0100593 u8 trickle_set_bits;
Michael Büsche27e2162016-03-04 22:41:19 +0100594
595 if (!of_node)
596 return;
597
598 /* Configure the trickle charger. */
Michael Büsche27e2162016-03-04 22:41:19 +0100599 err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
600 if (err) {
601 /* Disable trickle charger. */
Michael Büsch39387dc22016-03-10 18:34:23 +0100602 trickle_set_bits = 0;
Michael Büsche27e2162016-03-04 22:41:19 +0100603 } 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 }
Michael Büsch39387dc22016-03-10 18:34:23 +0100610 trickle_set_bits = elem->conf;
Michael Büsche27e2162016-03-04 22:41:19 +0100611 dev_info(&client->dev,
612 "Trickle charger enabled at %d ohms resistance.\n",
613 elem->r);
614 }
Michael Büsch39387dc22016-03-10 18:34:23 +0100615 err = rv3029_eeprom_update_bits(client, RV3029_CONTROL_E2P_EECTRL,
616 RV3029_TRICKLE_MASK,
617 trickle_set_bits);
Michael Büsche27e2162016-03-04 22:41:19 +0100618 if (err < 0) {
619 dev_err(&client->dev,
Michael Büsch39387dc22016-03-10 18:34:23 +0100620 "Failed to update trickle charger config\n");
Michael Büsche27e2162016-03-04 22:41:19 +0100621 }
622}
623
Michael Büscha696b312016-03-10 18:34:46 +0100624#ifdef CONFIG_RTC_DRV_RV3029_HWMON
625
626static int rv3029_read_temp(struct i2c_client *client, int *temp_mC)
627{
628 int ret;
629 u8 temp;
630
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200631 ret = rv3029_read_regs(client, RV3029_TEMP_PAGE, &temp, 1);
Michael Büscha696b312016-03-10 18:34:46 +0100632 if (ret < 0)
633 return ret;
634
635 *temp_mC = ((int)temp - 60) * 1000;
636
637 return 0;
638}
639
640static 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
654static 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
682static 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
707static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp,
708 NULL, 0);
709static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
710 rv3029_hwmon_show_update_interval,
711 rv3029_hwmon_set_update_interval, 0);
712
713static 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};
718ATTRIBUTE_GROUPS(rv3029_hwmon);
719
720static 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,
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200728 "unable to register hwmon device %ld\n",
729 PTR_ERR(hwmon_dev));
Michael Büscha696b312016-03-10 18:34:46 +0100730 }
731}
732
733#else /* CONFIG_RTC_DRV_RV3029_HWMON */
734
735static void rv3029_hwmon_register(struct i2c_client *client)
736{
737}
738
739#endif /* CONFIG_RTC_DRV_RV3029_HWMON */
740
Michael Büschaba39d22016-03-04 22:38:45 +0100741static 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,
Heiko Schocher52365232011-05-26 16:25:05 -0700746};
747
Michael Büschaba39d22016-03-04 22:38:45 +0100748static struct i2c_device_id rv3029_id[] = {
Michael Büschbaba6232016-03-04 22:39:17 +0100749 { "rv3029", 0 },
Heiko Schocher52365232011-05-26 16:25:05 -0700750 { "rv3029c2", 0 },
751 { }
752};
Michael Büschaba39d22016-03-04 22:38:45 +0100753MODULE_DEVICE_TABLE(i2c, rv3029_id);
Heiko Schocher52365232011-05-26 16:25:05 -0700754
Michael Büschaba39d22016-03-04 22:38:45 +0100755static int rv3029_probe(struct i2c_client *client,
756 const struct i2c_device_id *id)
Heiko Schocher52365232011-05-26 16:25:05 -0700757{
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
Mylène Josserand4e7f1a62016-05-03 11:54:32 +0200765 rc = rv3029_get_sr(client, buf);
Gregory Hermant67ab2442014-04-03 14:50:17 -0700766 if (rc < 0) {
767 dev_err(&client->dev, "reading status failed\n");
768 return rc;
769 }
770
Michael Büsche27e2162016-03-04 22:41:19 +0100771 rv3029_trickle_config(client);
Michael Büscha696b312016-03-10 18:34:46 +0100772 rv3029_hwmon_register(client);
Michael Büsche27e2162016-03-04 22:41:19 +0100773
Jingoo Han3d7068c2013-04-29 16:19:48 -0700774 rtc = devm_rtc_device_register(&client->dev, client->name,
Michael Büschaba39d22016-03-04 22:38:45 +0100775 &rv3029_rtc_ops, THIS_MODULE);
Heiko Schocher52365232011-05-26 16:25:05 -0700776
777 if (IS_ERR(rtc))
778 return PTR_ERR(rtc);
779
780 i2c_set_clientdata(client, rtc);
781
Heiko Schocher52365232011-05-26 16:25:05 -0700782 return 0;
Heiko Schocher52365232011-05-26 16:25:05 -0700783}
784
Michael Büschaba39d22016-03-04 22:38:45 +0100785static struct i2c_driver rv3029_driver = {
Heiko Schocher52365232011-05-26 16:25:05 -0700786 .driver = {
787 .name = "rtc-rv3029c2",
788 },
Michael Büschaba39d22016-03-04 22:38:45 +0100789 .probe = rv3029_probe,
790 .id_table = rv3029_id,
Heiko Schocher52365232011-05-26 16:25:05 -0700791};
792
Michael Büschaba39d22016-03-04 22:38:45 +0100793module_i2c_driver(rv3029_driver);
Heiko Schocher52365232011-05-26 16:25:05 -0700794
795MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
Michael Büsch2dca3d92016-03-04 22:40:30 +0100796MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
Michael Büschaba39d22016-03-04 22:38:45 +0100797MODULE_DESCRIPTION("Micro Crystal RV3029 RTC driver");
Heiko Schocher52365232011-05-26 16:25:05 -0700798MODULE_LICENSE("GPL");