blob: b416ed026168a60205d59451af86f6f1aae1eca6 [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>
21
Heiko Schocher52365232011-05-26 16:25:05 -070022
23/* Register map */
24/* control section */
Michael Büschaba39d22016-03-04 22:38:45 +010025#define RV3029_ONOFF_CTRL 0x00
Michael Büsch7697de32016-03-04 22:39:49 +010026#define RV3029_ONOFF_CTRL_WE BIT(0)
27#define RV3029_ONOFF_CTRL_TE BIT(1)
28#define RV3029_ONOFF_CTRL_TAR BIT(2)
29#define RV3029_ONOFF_CTRL_EERE BIT(3)
30#define RV3029_ONOFF_CTRL_SRON BIT(4)
31#define RV3029_ONOFF_CTRL_TD0 BIT(5)
32#define RV3029_ONOFF_CTRL_TD1 BIT(6)
33#define RV3029_ONOFF_CTRL_CLKINT BIT(7)
Michael Büschaba39d22016-03-04 22:38:45 +010034#define RV3029_IRQ_CTRL 0x01
Michael Büsch7697de32016-03-04 22:39:49 +010035#define RV3029_IRQ_CTRL_AIE BIT(0)
36#define RV3029_IRQ_CTRL_TIE BIT(1)
37#define RV3029_IRQ_CTRL_V1IE BIT(2)
38#define RV3029_IRQ_CTRL_V2IE BIT(3)
39#define RV3029_IRQ_CTRL_SRIE BIT(4)
Michael Büschaba39d22016-03-04 22:38:45 +010040#define RV3029_IRQ_FLAGS 0x02
Michael Büsch7697de32016-03-04 22:39:49 +010041#define RV3029_IRQ_FLAGS_AF BIT(0)
42#define RV3029_IRQ_FLAGS_TF BIT(1)
43#define RV3029_IRQ_FLAGS_V1IF BIT(2)
44#define RV3029_IRQ_FLAGS_V2IF BIT(3)
45#define RV3029_IRQ_FLAGS_SRF BIT(4)
Michael Büschaba39d22016-03-04 22:38:45 +010046#define RV3029_STATUS 0x03
Michael Büsch7697de32016-03-04 22:39:49 +010047#define RV3029_STATUS_VLOW1 BIT(2)
48#define RV3029_STATUS_VLOW2 BIT(3)
49#define RV3029_STATUS_SR BIT(4)
50#define RV3029_STATUS_PON BIT(5)
51#define RV3029_STATUS_EEBUSY BIT(7)
Michael Büschaba39d22016-03-04 22:38:45 +010052#define RV3029_RST_CTRL 0x04
Michael Büsch7697de32016-03-04 22:39:49 +010053#define RV3029_RST_CTRL_SYSR BIT(4)
Michael Büschaba39d22016-03-04 22:38:45 +010054#define RV3029_CONTROL_SECTION_LEN 0x05
Heiko Schocher52365232011-05-26 16:25:05 -070055
56/* watch section */
Michael Büschaba39d22016-03-04 22:38:45 +010057#define RV3029_W_SEC 0x08
58#define RV3029_W_MINUTES 0x09
59#define RV3029_W_HOURS 0x0A
Michael Büsch7697de32016-03-04 22:39:49 +010060#define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */
61#define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */
Michael Büschaba39d22016-03-04 22:38:45 +010062#define RV3029_W_DATE 0x0B
63#define RV3029_W_DAYS 0x0C
64#define RV3029_W_MONTHS 0x0D
65#define RV3029_W_YEARS 0x0E
66#define RV3029_WATCH_SECTION_LEN 0x07
Heiko Schocher52365232011-05-26 16:25:05 -070067
68/* alarm section */
Michael Büschaba39d22016-03-04 22:38:45 +010069#define RV3029_A_SC 0x10
70#define RV3029_A_MN 0x11
71#define RV3029_A_HR 0x12
72#define RV3029_A_DT 0x13
73#define RV3029_A_DW 0x14
74#define RV3029_A_MO 0x15
75#define RV3029_A_YR 0x16
76#define RV3029_ALARM_SECTION_LEN 0x07
Heiko Schocher52365232011-05-26 16:25:05 -070077
78/* timer section */
Michael Büschaba39d22016-03-04 22:38:45 +010079#define RV3029_TIMER_LOW 0x18
80#define RV3029_TIMER_HIGH 0x19
Heiko Schocher52365232011-05-26 16:25:05 -070081
82/* temperature section */
Michael Büschaba39d22016-03-04 22:38:45 +010083#define RV3029_TEMP_PAGE 0x20
Heiko Schocher52365232011-05-26 16:25:05 -070084
85/* eeprom data section */
Michael Büschaba39d22016-03-04 22:38:45 +010086#define RV3029_E2P_EEDATA1 0x28
87#define RV3029_E2P_EEDATA2 0x29
Michael Büsch7697de32016-03-04 22:39:49 +010088#define RV3029_E2PDATA_SECTION_LEN 0x02
Heiko Schocher52365232011-05-26 16:25:05 -070089
90/* eeprom control section */
Michael Büschaba39d22016-03-04 22:38:45 +010091#define RV3029_CONTROL_E2P_EECTRL 0x30
Michael Büsch7697de32016-03-04 22:39:49 +010092#define RV3029_EECTRL_THP BIT(0) /* temp scan interval */
93#define RV3029_EECTRL_THE BIT(1) /* thermometer enable */
94#define RV3029_EECTRL_FD0 BIT(2) /* CLKOUT */
95#define RV3029_EECTRL_FD1 BIT(3) /* CLKOUT */
96#define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */
97#define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */
98#define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */
99#define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */
100#define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\
101 RV3029_TRICKLE_5K |\
102 RV3029_TRICKLE_20K |\
103 RV3029_TRICKLE_80K)
104#define RV3029_TRICKLE_SHIFT 4
105#define RV3029_CONTROL_E2P_XOFFS 0x31 /* XTAL offset */
106#define RV3029_CONTROL_E2P_XOFFS_SIGN BIT(7) /* Sign: 1->pos, 0->neg */
107#define RV3029_CONTROL_E2P_QCOEF 0x32 /* XTAL temp drift coef */
108#define RV3029_CONTROL_E2P_TURNOVER 0x33 /* XTAL turnover temp (in *C) */
109#define RV3029_CONTROL_E2P_TOV_MASK 0x3F /* XTAL turnover temp mask */
Heiko Schocher52365232011-05-26 16:25:05 -0700110
111/* user ram section */
Michael Büschaba39d22016-03-04 22:38:45 +0100112#define RV3029_USR1_RAM_PAGE 0x38
113#define RV3029_USR1_SECTION_LEN 0x04
114#define RV3029_USR2_RAM_PAGE 0x3C
115#define RV3029_USR2_SECTION_LEN 0x04
Heiko Schocher52365232011-05-26 16:25:05 -0700116
117static int
Michael Büschaba39d22016-03-04 22:38:45 +0100118rv3029_i2c_read_regs(struct i2c_client *client, u8 reg, u8 *buf,
119 unsigned len)
Heiko Schocher52365232011-05-26 16:25:05 -0700120{
121 int ret;
122
Michael Büschaba39d22016-03-04 22:38:45 +0100123 if ((reg > RV3029_USR1_RAM_PAGE + 7) ||
124 (reg + len > RV3029_USR1_RAM_PAGE + 8))
Heiko Schocher52365232011-05-26 16:25:05 -0700125 return -EINVAL;
126
127 ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
128 if (ret < 0)
129 return ret;
130 if (ret < len)
131 return -EIO;
132 return 0;
133}
134
135static int
Michael Büschaba39d22016-03-04 22:38:45 +0100136rv3029_i2c_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
146static int
Michael Büsch2dca3d92016-03-04 22:40:30 +0100147rv3029_i2c_update_bits(struct i2c_client *client, u8 reg, u8 mask, u8 set)
148{
149 u8 buf;
150 int ret;
151
152 ret = rv3029_i2c_read_regs(client, reg, &buf, 1);
153 if (ret < 0)
154 return ret;
155 buf &= ~mask;
156 buf |= set & mask;
157 ret = rv3029_i2c_write_regs(client, reg, &buf, 1);
158 if (ret < 0)
159 return ret;
160
161 return 0;
162}
163
164static int
Michael Büschaba39d22016-03-04 22:38:45 +0100165rv3029_i2c_get_sr(struct i2c_client *client, u8 *buf)
Heiko Schocher52365232011-05-26 16:25:05 -0700166{
Michael Büschaba39d22016-03-04 22:38:45 +0100167 int ret = rv3029_i2c_read_regs(client, RV3029_STATUS, buf, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700168
169 if (ret < 0)
170 return -EIO;
171 dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
172 return 0;
173}
174
175static int
Michael Büschaba39d22016-03-04 22:38:45 +0100176rv3029_i2c_set_sr(struct i2c_client *client, u8 val)
Heiko Schocher52365232011-05-26 16:25:05 -0700177{
178 u8 buf[1];
179 int sr;
180
181 buf[0] = val;
Michael Büschaba39d22016-03-04 22:38:45 +0100182 sr = rv3029_i2c_write_regs(client, RV3029_STATUS, buf, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700183 dev_dbg(&client->dev, "status = 0x%.2x (%d)\n", buf[0], buf[0]);
184 if (sr < 0)
185 return -EIO;
186 return 0;
187}
188
Michael Büscha7f6e282016-03-04 22:40:55 +0100189static int rv3029_eeprom_busywait(struct i2c_client *client)
190{
191 int i, ret;
192 u8 sr;
193
194 for (i = 100; i > 0; i--) {
195 ret = rv3029_i2c_get_sr(client, &sr);
196 if (ret < 0)
197 break;
198 if (!(sr & RV3029_STATUS_EEBUSY))
199 break;
200 usleep_range(1000, 10000);
201 }
202 if (i <= 0) {
203 dev_err(&client->dev, "EEPROM busy wait timeout.\n");
204 return -ETIMEDOUT;
205 }
206
207 return ret;
208}
209
210static int rv3029_eeprom_exit(struct i2c_client *client)
211{
212 /* Re-enable eeprom refresh */
213 return rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL,
214 RV3029_ONOFF_CTRL_EERE,
215 RV3029_ONOFF_CTRL_EERE);
216}
217
218static int rv3029_eeprom_enter(struct i2c_client *client)
219{
220 int ret;
221 u8 sr;
222
223 /* Check whether we are in the allowed voltage range. */
224 ret = rv3029_i2c_get_sr(client, &sr);
225 if (ret < 0)
226 return ret;
227 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
228 /* We clear the bits and retry once just in case
229 * we had a brown out in early startup.
230 */
231 sr &= ~RV3029_STATUS_VLOW1;
232 sr &= ~RV3029_STATUS_VLOW2;
233 ret = rv3029_i2c_set_sr(client, sr);
234 if (ret < 0)
235 return ret;
236 usleep_range(1000, 10000);
237 ret = rv3029_i2c_get_sr(client, &sr);
238 if (ret < 0)
239 return ret;
240 if (sr & (RV3029_STATUS_VLOW1 | RV3029_STATUS_VLOW2)) {
241 dev_err(&client->dev,
242 "Supply voltage is too low to safely access the EEPROM.\n");
243 return -ENODEV;
244 }
245 }
246
247 /* Disable eeprom refresh. */
248 ret = rv3029_i2c_update_bits(client, RV3029_ONOFF_CTRL,
249 RV3029_ONOFF_CTRL_EERE, 0);
250 if (ret < 0)
251 return ret;
252
253 /* Wait for any previous eeprom accesses to finish. */
254 ret = rv3029_eeprom_busywait(client);
255 if (ret < 0)
256 rv3029_eeprom_exit(client);
257
258 return ret;
259}
260
261static int rv3029_eeprom_read(struct i2c_client *client, u8 reg,
262 u8 buf[], size_t len)
263{
264 int ret, err;
265
266 err = rv3029_eeprom_enter(client);
267 if (err < 0)
268 return err;
269
270 ret = rv3029_i2c_read_regs(client, reg, buf, len);
271
272 err = rv3029_eeprom_exit(client);
273 if (err < 0)
274 return err;
275
276 return ret;
277}
278
279static int rv3029_eeprom_write(struct i2c_client *client, u8 reg,
280 u8 const buf[], size_t len)
281{
282 int ret, err;
283 size_t i;
284 u8 tmp;
285
286 err = rv3029_eeprom_enter(client);
287 if (err < 0)
288 return err;
289
290 for (i = 0; i < len; i++, reg++) {
291 ret = rv3029_i2c_read_regs(client, reg, &tmp, 1);
292 if (ret < 0)
293 break;
294 if (tmp != buf[i]) {
295 ret = rv3029_i2c_write_regs(client, reg, &buf[i], 1);
296 if (ret < 0)
297 break;
298 }
299 ret = rv3029_eeprom_busywait(client);
300 if (ret < 0)
301 break;
302 }
303
304 err = rv3029_eeprom_exit(client);
305 if (err < 0)
306 return err;
307
308 return ret;
309}
310
Heiko Schocher52365232011-05-26 16:25:05 -0700311static int
Michael Büschaba39d22016-03-04 22:38:45 +0100312rv3029_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700313{
314 u8 buf[1];
315 int ret;
Michael Büschaba39d22016-03-04 22:38:45 +0100316 u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
Heiko Schocher52365232011-05-26 16:25:05 -0700317
Michael Büschaba39d22016-03-04 22:38:45 +0100318 ret = rv3029_i2c_get_sr(client, buf);
Heiko Schocher52365232011-05-26 16:25:05 -0700319 if (ret < 0) {
320 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
321 return -EIO;
322 }
323
Michael Büschaba39d22016-03-04 22:38:45 +0100324 ret = rv3029_i2c_read_regs(client, RV3029_W_SEC, regs,
325 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700326 if (ret < 0) {
327 dev_err(&client->dev, "%s: reading RTC section failed\n",
328 __func__);
329 return ret;
330 }
331
Michael Büschaba39d22016-03-04 22:38:45 +0100332 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC-RV3029_W_SEC]);
333 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES-RV3029_W_SEC]);
Heiko Schocher52365232011-05-26 16:25:05 -0700334
335 /* HR field has a more complex interpretation */
336 {
Michael Büschaba39d22016-03-04 22:38:45 +0100337 const u8 _hr = regs[RV3029_W_HOURS-RV3029_W_SEC];
338
339 if (_hr & RV3029_REG_HR_12_24) {
Heiko Schocher52365232011-05-26 16:25:05 -0700340 /* 12h format */
341 tm->tm_hour = bcd2bin(_hr & 0x1f);
Michael Büschaba39d22016-03-04 22:38:45 +0100342 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
Heiko Schocher52365232011-05-26 16:25:05 -0700343 tm->tm_hour += 12;
344 } else /* 24h format */
345 tm->tm_hour = bcd2bin(_hr & 0x3f);
346 }
347
Michael Büschaba39d22016-03-04 22:38:45 +0100348 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE-RV3029_W_SEC]);
349 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS-RV3029_W_SEC]) - 1;
350 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS-RV3029_W_SEC]) + 100;
351 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS-RV3029_W_SEC]) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700352
353 return 0;
354}
355
Michael Büschaba39d22016-03-04 22:38:45 +0100356static int rv3029_rtc_read_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700357{
Michael Büschaba39d22016-03-04 22:38:45 +0100358 return rv3029_i2c_read_time(to_i2c_client(dev), tm);
Heiko Schocher52365232011-05-26 16:25:05 -0700359}
360
361static int
Michael Büschaba39d22016-03-04 22:38:45 +0100362rv3029_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700363{
364 struct rtc_time *const tm = &alarm->time;
365 int ret;
366 u8 regs[8];
367
Michael Büschaba39d22016-03-04 22:38:45 +0100368 ret = rv3029_i2c_get_sr(client, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700369 if (ret < 0) {
370 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
371 return -EIO;
372 }
373
Michael Büschaba39d22016-03-04 22:38:45 +0100374 ret = rv3029_i2c_read_regs(client, RV3029_A_SC, regs,
375 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700376
377 if (ret < 0) {
378 dev_err(&client->dev, "%s: reading alarm section failed\n",
379 __func__);
380 return ret;
381 }
382
Michael Büschaba39d22016-03-04 22:38:45 +0100383 tm->tm_sec = bcd2bin(regs[RV3029_A_SC-RV3029_A_SC] & 0x7f);
384 tm->tm_min = bcd2bin(regs[RV3029_A_MN-RV3029_A_SC] & 0x7f);
385 tm->tm_hour = bcd2bin(regs[RV3029_A_HR-RV3029_A_SC] & 0x3f);
386 tm->tm_mday = bcd2bin(regs[RV3029_A_DT-RV3029_A_SC] & 0x3f);
387 tm->tm_mon = bcd2bin(regs[RV3029_A_MO-RV3029_A_SC] & 0x1f) - 1;
388 tm->tm_year = bcd2bin(regs[RV3029_A_YR-RV3029_A_SC] & 0x7f) + 100;
389 tm->tm_wday = bcd2bin(regs[RV3029_A_DW-RV3029_A_SC] & 0x07) - 1;
Heiko Schocher52365232011-05-26 16:25:05 -0700390
391 return 0;
392}
393
394static int
Michael Büschaba39d22016-03-04 22:38:45 +0100395rv3029_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700396{
Michael Büschaba39d22016-03-04 22:38:45 +0100397 return rv3029_i2c_read_alarm(to_i2c_client(dev), alarm);
Heiko Schocher52365232011-05-26 16:25:05 -0700398}
399
Michael Büschaba39d22016-03-04 22:38:45 +0100400static int rv3029_rtc_i2c_alarm_set_irq(struct i2c_client *client,
Heiko Schocher52365232011-05-26 16:25:05 -0700401 int enable)
402{
403 int ret;
Heiko Schocher52365232011-05-26 16:25:05 -0700404
Michael Büsch2dca3d92016-03-04 22:40:30 +0100405 /* enable/disable AIE irq */
406 ret = rv3029_i2c_update_bits(client, RV3029_IRQ_CTRL,
407 RV3029_IRQ_CTRL_AIE,
408 (enable ? RV3029_IRQ_CTRL_AIE : 0));
Heiko Schocher52365232011-05-26 16:25:05 -0700409 if (ret < 0) {
Michael Büsch2dca3d92016-03-04 22:40:30 +0100410 dev_err(&client->dev, "can't update INT reg\n");
Heiko Schocher52365232011-05-26 16:25:05 -0700411 return ret;
412 }
413
414 return 0;
415}
416
Michael Büschaba39d22016-03-04 22:38:45 +0100417static int rv3029_rtc_i2c_set_alarm(struct i2c_client *client,
418 struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700419{
420 struct rtc_time *const tm = &alarm->time;
421 int ret;
422 u8 regs[8];
423
424 /*
425 * The clock has an 8 bit wide bcd-coded register (they never learn)
426 * for the year. tm_year is an offset from 1900 and we are interested
427 * in the 2000-2099 range, so any value less than 100 is invalid.
428 */
429 if (tm->tm_year < 100)
430 return -EINVAL;
431
Michael Büschaba39d22016-03-04 22:38:45 +0100432 ret = rv3029_i2c_get_sr(client, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700433 if (ret < 0) {
434 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
435 return -EIO;
436 }
Michael Büschaba39d22016-03-04 22:38:45 +0100437 regs[RV3029_A_SC-RV3029_A_SC] = bin2bcd(tm->tm_sec & 0x7f);
438 regs[RV3029_A_MN-RV3029_A_SC] = bin2bcd(tm->tm_min & 0x7f);
439 regs[RV3029_A_HR-RV3029_A_SC] = bin2bcd(tm->tm_hour & 0x3f);
440 regs[RV3029_A_DT-RV3029_A_SC] = bin2bcd(tm->tm_mday & 0x3f);
441 regs[RV3029_A_MO-RV3029_A_SC] = bin2bcd((tm->tm_mon & 0x1f) - 1);
442 regs[RV3029_A_DW-RV3029_A_SC] = bin2bcd((tm->tm_wday & 7) - 1);
443 regs[RV3029_A_YR-RV3029_A_SC] = bin2bcd((tm->tm_year & 0x7f) - 100);
Heiko Schocher52365232011-05-26 16:25:05 -0700444
Michael Büschaba39d22016-03-04 22:38:45 +0100445 ret = rv3029_i2c_write_regs(client, RV3029_A_SC, regs,
446 RV3029_ALARM_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700447 if (ret < 0)
448 return ret;
449
450 if (alarm->enabled) {
Heiko Schocher52365232011-05-26 16:25:05 -0700451 /* clear AF flag */
Michael Büsch2dca3d92016-03-04 22:40:30 +0100452 ret = rv3029_i2c_update_bits(client, RV3029_IRQ_FLAGS,
453 RV3029_IRQ_FLAGS_AF, 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700454 if (ret < 0) {
Michael Büsch2dca3d92016-03-04 22:40:30 +0100455 dev_err(&client->dev, "can't clear alarm flag\n");
Heiko Schocher52365232011-05-26 16:25:05 -0700456 return ret;
457 }
458 /* enable AIE irq */
Michael Büschaba39d22016-03-04 22:38:45 +0100459 ret = rv3029_rtc_i2c_alarm_set_irq(client, 1);
Heiko Schocher52365232011-05-26 16:25:05 -0700460 if (ret)
461 return ret;
462
463 dev_dbg(&client->dev, "alarm IRQ armed\n");
464 } else {
465 /* disable AIE irq */
Michael Büschaba39d22016-03-04 22:38:45 +0100466 ret = rv3029_rtc_i2c_alarm_set_irq(client, 0);
Heiko Schocher52365232011-05-26 16:25:05 -0700467 if (ret)
468 return ret;
469
470 dev_dbg(&client->dev, "alarm IRQ disabled\n");
471 }
472
473 return 0;
474}
475
Michael Büschaba39d22016-03-04 22:38:45 +0100476static int rv3029_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Heiko Schocher52365232011-05-26 16:25:05 -0700477{
Michael Büschaba39d22016-03-04 22:38:45 +0100478 return rv3029_rtc_i2c_set_alarm(to_i2c_client(dev), alarm);
Heiko Schocher52365232011-05-26 16:25:05 -0700479}
480
481static int
Michael Büschaba39d22016-03-04 22:38:45 +0100482rv3029_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700483{
484 u8 regs[8];
485 int ret;
486
487 /*
488 * The clock has an 8 bit wide bcd-coded register (they never learn)
489 * for the year. tm_year is an offset from 1900 and we are interested
490 * in the 2000-2099 range, so any value less than 100 is invalid.
491 */
492 if (tm->tm_year < 100)
493 return -EINVAL;
494
Michael Büschaba39d22016-03-04 22:38:45 +0100495 regs[RV3029_W_SEC-RV3029_W_SEC] = bin2bcd(tm->tm_sec);
496 regs[RV3029_W_MINUTES-RV3029_W_SEC] = bin2bcd(tm->tm_min);
497 regs[RV3029_W_HOURS-RV3029_W_SEC] = bin2bcd(tm->tm_hour);
498 regs[RV3029_W_DATE-RV3029_W_SEC] = bin2bcd(tm->tm_mday);
499 regs[RV3029_W_MONTHS-RV3029_W_SEC] = bin2bcd(tm->tm_mon+1);
500 regs[RV3029_W_DAYS-RV3029_W_SEC] = bin2bcd((tm->tm_wday & 7)+1);
501 regs[RV3029_W_YEARS-RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
Heiko Schocher52365232011-05-26 16:25:05 -0700502
Michael Büschaba39d22016-03-04 22:38:45 +0100503 ret = rv3029_i2c_write_regs(client, RV3029_W_SEC, regs,
504 RV3029_WATCH_SECTION_LEN);
Heiko Schocher52365232011-05-26 16:25:05 -0700505 if (ret < 0)
506 return ret;
507
Michael Büschaba39d22016-03-04 22:38:45 +0100508 ret = rv3029_i2c_get_sr(client, regs);
Heiko Schocher52365232011-05-26 16:25:05 -0700509 if (ret < 0) {
510 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
511 return ret;
512 }
513 /* clear PON bit */
Michael Büschaba39d22016-03-04 22:38:45 +0100514 ret = rv3029_i2c_set_sr(client, (regs[0] & ~RV3029_STATUS_PON));
Heiko Schocher52365232011-05-26 16:25:05 -0700515 if (ret < 0) {
516 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
517 return ret;
518 }
519
520 return 0;
521}
522
Michael Büschaba39d22016-03-04 22:38:45 +0100523static int rv3029_rtc_set_time(struct device *dev, struct rtc_time *tm)
Heiko Schocher52365232011-05-26 16:25:05 -0700524{
Michael Büschaba39d22016-03-04 22:38:45 +0100525 return rv3029_i2c_set_time(to_i2c_client(dev), tm);
Heiko Schocher52365232011-05-26 16:25:05 -0700526}
527
Michael Büsche27e2162016-03-04 22:41:19 +0100528static const struct rv3029_trickle_tab_elem {
529 u32 r; /* resistance in ohms */
530 u8 conf; /* trickle config bits */
531} rv3029_trickle_tab[] = {
532 {
533 .r = 1076,
534 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
535 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
536 }, {
537 .r = 1091,
538 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
539 RV3029_TRICKLE_20K,
540 }, {
541 .r = 1137,
542 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
543 RV3029_TRICKLE_80K,
544 }, {
545 .r = 1154,
546 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
547 }, {
548 .r = 1371,
549 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
550 RV3029_TRICKLE_80K,
551 }, {
552 .r = 1395,
553 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
554 }, {
555 .r = 1472,
556 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
557 }, {
558 .r = 1500,
559 .conf = RV3029_TRICKLE_1K,
560 }, {
561 .r = 3810,
562 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
563 RV3029_TRICKLE_80K,
564 }, {
565 .r = 4000,
566 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
567 }, {
568 .r = 4706,
569 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
570 }, {
571 .r = 5000,
572 .conf = RV3029_TRICKLE_5K,
573 }, {
574 .r = 16000,
575 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
576 }, {
577 .r = 20000,
578 .conf = RV3029_TRICKLE_20K,
579 }, {
580 .r = 80000,
581 .conf = RV3029_TRICKLE_80K,
582 },
583};
584
585static void rv3029_trickle_config(struct i2c_client *client)
586{
587 struct device_node *of_node = client->dev.of_node;
588 const struct rv3029_trickle_tab_elem *elem;
589 int i, err;
590 u32 ohms;
591 u8 eectrl;
592
593 if (!of_node)
594 return;
595
596 /* Configure the trickle charger. */
597 err = rv3029_eeprom_read(client, RV3029_CONTROL_E2P_EECTRL,
598 &eectrl, 1);
599 if (err < 0) {
600 dev_err(&client->dev,
601 "Failed to read trickle charger config\n");
602 return;
603 }
604 err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
605 if (err) {
606 /* Disable trickle charger. */
607 eectrl &= ~RV3029_TRICKLE_MASK;
608 } else {
609 /* Enable trickle charger. */
610 for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) {
611 elem = &rv3029_trickle_tab[i];
612 if (elem->r >= ohms)
613 break;
614 }
615 eectrl &= ~RV3029_TRICKLE_MASK;
616 eectrl |= elem->conf;
617 dev_info(&client->dev,
618 "Trickle charger enabled at %d ohms resistance.\n",
619 elem->r);
620 }
621 err = rv3029_eeprom_write(client, RV3029_CONTROL_E2P_EECTRL,
622 &eectrl, 1);
623 if (err < 0) {
624 dev_err(&client->dev,
625 "Failed to write trickle charger config\n");
626 }
627}
628
Michael Büschaba39d22016-03-04 22:38:45 +0100629static const struct rtc_class_ops rv3029_rtc_ops = {
630 .read_time = rv3029_rtc_read_time,
631 .set_time = rv3029_rtc_set_time,
632 .read_alarm = rv3029_rtc_read_alarm,
633 .set_alarm = rv3029_rtc_set_alarm,
Heiko Schocher52365232011-05-26 16:25:05 -0700634};
635
Michael Büschaba39d22016-03-04 22:38:45 +0100636static struct i2c_device_id rv3029_id[] = {
Michael Büschbaba6232016-03-04 22:39:17 +0100637 { "rv3029", 0 },
Heiko Schocher52365232011-05-26 16:25:05 -0700638 { "rv3029c2", 0 },
639 { }
640};
Michael Büschaba39d22016-03-04 22:38:45 +0100641MODULE_DEVICE_TABLE(i2c, rv3029_id);
Heiko Schocher52365232011-05-26 16:25:05 -0700642
Michael Büschaba39d22016-03-04 22:38:45 +0100643static int rv3029_probe(struct i2c_client *client,
644 const struct i2c_device_id *id)
Heiko Schocher52365232011-05-26 16:25:05 -0700645{
646 struct rtc_device *rtc;
647 int rc = 0;
648 u8 buf[1];
649
650 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_EMUL))
651 return -ENODEV;
652
Michael Büschaba39d22016-03-04 22:38:45 +0100653 rc = rv3029_i2c_get_sr(client, buf);
Gregory Hermant67ab2442014-04-03 14:50:17 -0700654 if (rc < 0) {
655 dev_err(&client->dev, "reading status failed\n");
656 return rc;
657 }
658
Michael Büsche27e2162016-03-04 22:41:19 +0100659 rv3029_trickle_config(client);
660
Jingoo Han3d7068c2013-04-29 16:19:48 -0700661 rtc = devm_rtc_device_register(&client->dev, client->name,
Michael Büschaba39d22016-03-04 22:38:45 +0100662 &rv3029_rtc_ops, THIS_MODULE);
Heiko Schocher52365232011-05-26 16:25:05 -0700663
664 if (IS_ERR(rtc))
665 return PTR_ERR(rtc);
666
667 i2c_set_clientdata(client, rtc);
668
Heiko Schocher52365232011-05-26 16:25:05 -0700669 return 0;
Heiko Schocher52365232011-05-26 16:25:05 -0700670}
671
Michael Büschaba39d22016-03-04 22:38:45 +0100672static struct i2c_driver rv3029_driver = {
Heiko Schocher52365232011-05-26 16:25:05 -0700673 .driver = {
674 .name = "rtc-rv3029c2",
675 },
Michael Büschaba39d22016-03-04 22:38:45 +0100676 .probe = rv3029_probe,
677 .id_table = rv3029_id,
Heiko Schocher52365232011-05-26 16:25:05 -0700678};
679
Michael Büschaba39d22016-03-04 22:38:45 +0100680module_i2c_driver(rv3029_driver);
Heiko Schocher52365232011-05-26 16:25:05 -0700681
682MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
Michael Büsch2dca3d92016-03-04 22:40:30 +0100683MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
Michael Büschaba39d22016-03-04 22:38:45 +0100684MODULE_DESCRIPTION("Micro Crystal RV3029 RTC driver");
Heiko Schocher52365232011-05-26 16:25:05 -0700685MODULE_LICENSE("GPL");