blob: 033f65aef5788f225fd6dffd4fa65ebe8b0f403a [file] [log] [blame]
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -07001/*
2 * Intersil ISL1208 rtc class driver
3 *
4 * Copyright 2005,2006 Hebert Valerio Riedel <hvr@gnu.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 */
12
13#include <linux/module.h>
14#include <linux/i2c.h>
15#include <linux/bcd.h>
16#include <linux/rtc.h>
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000017#include "rtc-core.h"
Denis Osterland9ece7cd2018-07-24 11:31:21 +000018#include <linux/of_irq.h>
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070019
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070020/* Register map */
21/* rtc section */
22#define ISL1208_REG_SC 0x00
23#define ISL1208_REG_MN 0x01
24#define ISL1208_REG_HR 0x02
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070025#define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */
26#define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070027#define ISL1208_REG_DT 0x03
28#define ISL1208_REG_MO 0x04
29#define ISL1208_REG_YR 0x05
30#define ISL1208_REG_DW 0x06
31#define ISL1208_RTC_SECTION_LEN 7
32
33/* control/status section */
34#define ISL1208_REG_SR 0x07
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070035#define ISL1208_REG_SR_ARST (1<<7) /* auto reset */
36#define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */
37#define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000038#define ISL1208_REG_SR_EVT (1<<3) /* event */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070039#define ISL1208_REG_SR_ALM (1<<2) /* alarm */
40#define ISL1208_REG_SR_BAT (1<<1) /* battery */
41#define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070042#define ISL1208_REG_INT 0x08
Ryan Malloncf044f02011-03-22 16:34:53 -070043#define ISL1208_REG_INT_ALME (1<<6) /* alarm enable */
44#define ISL1208_REG_INT_IM (1<<7) /* interrupt/alarm mode */
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000045#define ISL1219_REG_EV 0x09
46#define ISL1219_REG_EV_EVEN (1<<4) /* event detection enable */
Denis Osterlandcfa30622018-07-24 11:31:21 +000047#define ISL1219_REG_EV_EVIENB (1<<7) /* event in pull-up disable */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070048#define ISL1208_REG_ATR 0x0a
49#define ISL1208_REG_DTR 0x0b
50
51/* alarm section */
52#define ISL1208_REG_SCA 0x0c
53#define ISL1208_REG_MNA 0x0d
54#define ISL1208_REG_HRA 0x0e
55#define ISL1208_REG_DTA 0x0f
56#define ISL1208_REG_MOA 0x10
57#define ISL1208_REG_DWA 0x11
58#define ISL1208_ALARM_SECTION_LEN 6
59
60/* user section */
61#define ISL1208_REG_USR1 0x12
62#define ISL1208_REG_USR2 0x13
63#define ISL1208_USR_SECTION_LEN 2
64
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000065/* event section */
66#define ISL1219_REG_SCT 0x14
67#define ISL1219_REG_MNT 0x15
68#define ISL1219_REG_HRT 0x16
69#define ISL1219_REG_DTT 0x17
70#define ISL1219_REG_MOT 0x18
71#define ISL1219_REG_YRT 0x19
72#define ISL1219_EVT_SECTION_LEN 6
73
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070074static struct i2c_driver isl1208_driver;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070075
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000076/* ISL1208 various variants */
77enum {
78 TYPE_ISL1208 = 0,
79 TYPE_ISL1218,
80 TYPE_ISL1219,
81};
82
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070083/* block read */
84static int
85isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070086 unsigned len)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070087{
88 u8 reg_addr[1] = { reg };
89 struct i2c_msg msgs[2] = {
Shubhrajyoti D885ccbb2012-10-04 17:14:27 -070090 {
91 .addr = client->addr,
92 .len = sizeof(reg_addr),
93 .buf = reg_addr
94 },
95 {
96 .addr = client->addr,
97 .flags = I2C_M_RD,
98 .len = len,
99 .buf = buf
100 }
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700101 };
102 int ret;
103
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000104 WARN_ON(reg > ISL1219_REG_YRT);
105 WARN_ON(reg + len > ISL1219_REG_YRT + 1);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700106
107 ret = i2c_transfer(client->adapter, msgs, 2);
108 if (ret > 0)
109 ret = 0;
110 return ret;
111}
112
113/* block write */
114static int
115isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700116 unsigned len)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700117{
118 u8 i2c_buf[ISL1208_REG_USR2 + 2];
119 struct i2c_msg msgs[1] = {
Shubhrajyoti D885ccbb2012-10-04 17:14:27 -0700120 {
121 .addr = client->addr,
122 .len = len + 1,
123 .buf = i2c_buf
124 }
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700125 };
126 int ret;
127
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000128 WARN_ON(reg > ISL1219_REG_YRT);
129 WARN_ON(reg + len > ISL1219_REG_YRT + 1);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700130
131 i2c_buf[0] = reg;
132 memcpy(&i2c_buf[1], &buf[0], len);
133
134 ret = i2c_transfer(client->adapter, msgs, 1);
135 if (ret > 0)
136 ret = 0;
137 return ret;
138}
139
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400140/* simple check to see whether we have a isl1208 */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700141static int
142isl1208_i2c_validate_client(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700143{
144 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
145 u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
146 0x80, 0x80, 0x40, 0xc0, 0xe0, 0x00, 0xf8
147 };
148 int i;
149 int ret;
150
151 ret = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
152 if (ret < 0)
153 return ret;
154
155 for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700156 if (regs[i] & zero_mask[i]) /* check if bits are cleared */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700157 return -ENODEV;
158 }
159
160 return 0;
161}
162
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700163static int
164isl1208_i2c_get_sr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700165{
Sachin Kamatc91fd912013-11-12 15:10:26 -0800166 return i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700167}
168
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700169static int
170isl1208_i2c_get_atr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700171{
172 int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700173 if (atr < 0)
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700174 return atr;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700175
176 /* The 6bit value in the ATR register controls the load
177 * capacitance C_load * in steps of 0.25pF
178 *
179 * bit (1<<5) of the ATR register is inverted
180 *
181 * C_load(ATR=0x20) = 4.50pF
182 * C_load(ATR=0x00) = 12.50pF
183 * C_load(ATR=0x1f) = 20.25pF
184 *
185 */
186
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700187 atr &= 0x3f; /* mask out lsb */
188 atr ^= 1 << 5; /* invert 6th bit */
189 atr += 2 * 9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700190
191 return atr;
192}
193
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700194static int
195isl1208_i2c_get_dtr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700196{
197 int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700198 if (dtr < 0)
199 return -EIO;
200
201 /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700202 dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700203
204 return dtr;
205}
206
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700207static int
208isl1208_i2c_get_usr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700209{
210 u8 buf[ISL1208_USR_SECTION_LEN] = { 0, };
211 int ret;
212
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700213 ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf,
214 ISL1208_USR_SECTION_LEN);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700215 if (ret < 0)
216 return ret;
217
218 return (buf[1] << 8) | buf[0];
219}
220
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700221static int
222isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700223{
224 u8 buf[ISL1208_USR_SECTION_LEN];
225
226 buf[0] = usr & 0xff;
227 buf[1] = (usr >> 8) & 0xff;
228
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700229 return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
230 ISL1208_USR_SECTION_LEN);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700231}
232
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700233static int
Ryan Malloncf044f02011-03-22 16:34:53 -0700234isl1208_rtc_toggle_alarm(struct i2c_client *client, int enable)
235{
236 int icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
237
238 if (icr < 0) {
239 dev_err(&client->dev, "%s: reading INT failed\n", __func__);
240 return icr;
241 }
242
243 if (enable)
244 icr |= ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM;
245 else
246 icr &= ~(ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM);
247
248 icr = i2c_smbus_write_byte_data(client, ISL1208_REG_INT, icr);
249 if (icr < 0) {
250 dev_err(&client->dev, "%s: writing INT failed\n", __func__);
251 return icr;
252 }
253
254 return 0;
255}
256
257static int
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700258isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700259{
260 struct i2c_client *const client = to_i2c_client(dev);
261 int sr, dtr, atr, usr;
262
263 sr = isl1208_i2c_get_sr(client);
264 if (sr < 0) {
265 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
266 return sr;
267 }
268
269 seq_printf(seq, "status_reg\t:%s%s%s%s%s%s (0x%.2x)\n",
270 (sr & ISL1208_REG_SR_RTCF) ? " RTCF" : "",
271 (sr & ISL1208_REG_SR_BAT) ? " BAT" : "",
272 (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
273 (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
274 (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700275 (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700276
277 seq_printf(seq, "batt_status\t: %s\n",
278 (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");
279
280 dtr = isl1208_i2c_get_dtr(client);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700281 if (dtr >= 0 - 1)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700282 seq_printf(seq, "digital_trim\t: %d ppm\n", dtr);
283
284 atr = isl1208_i2c_get_atr(client);
285 if (atr >= 0)
286 seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700287 atr >> 2, (atr & 0x3) * 25);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700288
289 usr = isl1208_i2c_get_usr(client);
290 if (usr >= 0)
291 seq_printf(seq, "user_data\t: 0x%.4x\n", usr);
292
293 return 0;
294}
295
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700296static int
297isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700298{
299 int sr;
300 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
301
302 sr = isl1208_i2c_get_sr(client);
303 if (sr < 0) {
304 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
305 return -EIO;
306 }
307
308 sr = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
309 if (sr < 0) {
310 dev_err(&client->dev, "%s: reading RTC section failed\n",
311 __func__);
312 return sr;
313 }
314
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700315 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SC]);
316 tm->tm_min = bcd2bin(regs[ISL1208_REG_MN]);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700317
318 /* HR field has a more complex interpretation */
319 {
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700320 const u8 _hr = regs[ISL1208_REG_HR];
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700321 if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700322 tm->tm_hour = bcd2bin(_hr & 0x3f);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700323 else {
324 /* 12h format */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700325 tm->tm_hour = bcd2bin(_hr & 0x1f);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700326 if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700327 tm->tm_hour += 12;
328 }
329 }
330
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700331 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DT]);
332 tm->tm_mon = bcd2bin(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
333 tm->tm_year = bcd2bin(regs[ISL1208_REG_YR]) + 100;
334 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DW]);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700335
336 return 0;
337}
338
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700339static int
340isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700341{
342 struct rtc_time *const tm = &alarm->time;
343 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
Ryan Malloncf044f02011-03-22 16:34:53 -0700344 int icr, yr, sr = isl1208_i2c_get_sr(client);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700345
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700346 if (sr < 0) {
347 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
348 return sr;
349 }
350
351 sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700352 ISL1208_ALARM_SECTION_LEN);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700353 if (sr < 0) {
354 dev_err(&client->dev, "%s: reading alarm section failed\n",
355 __func__);
356 return sr;
357 }
358
359 /* MSB of each alarm register is an enable bit */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700360 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
361 tm->tm_min = bcd2bin(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
362 tm->tm_hour = bcd2bin(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
363 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700364 tm->tm_mon =
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700365 bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
366 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700367
Ryan Malloncf044f02011-03-22 16:34:53 -0700368 /* The alarm doesn't store the year so get it from the rtc section */
369 yr = i2c_smbus_read_byte_data(client, ISL1208_REG_YR);
370 if (yr < 0) {
371 dev_err(&client->dev, "%s: reading RTC YR failed\n", __func__);
372 return yr;
373 }
374 tm->tm_year = bcd2bin(yr) + 100;
375
376 icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
377 if (icr < 0) {
378 dev_err(&client->dev, "%s: reading INT failed\n", __func__);
379 return icr;
380 }
381 alarm->enabled = !!(icr & ISL1208_REG_INT_ALME);
382
383 return 0;
384}
385
386static int
387isl1208_i2c_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
388{
389 struct rtc_time *alarm_tm = &alarm->time;
390 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
391 const int offs = ISL1208_REG_SCA;
Ryan Malloncf044f02011-03-22 16:34:53 -0700392 struct rtc_time rtc_tm;
393 int err, enable;
394
395 err = isl1208_i2c_read_time(client, &rtc_tm);
396 if (err)
397 return err;
Ryan Malloncf044f02011-03-22 16:34:53 -0700398
399 /* If the alarm time is before the current time disable the alarm */
Xunlei Pangf118db12015-06-12 10:04:11 +0800400 if (!alarm->enabled || rtc_tm_sub(alarm_tm, &rtc_tm) <= 0)
Ryan Malloncf044f02011-03-22 16:34:53 -0700401 enable = 0x00;
402 else
403 enable = 0x80;
404
405 /* Program the alarm and enable it for each setting */
406 regs[ISL1208_REG_SCA - offs] = bin2bcd(alarm_tm->tm_sec) | enable;
407 regs[ISL1208_REG_MNA - offs] = bin2bcd(alarm_tm->tm_min) | enable;
408 regs[ISL1208_REG_HRA - offs] = bin2bcd(alarm_tm->tm_hour) |
409 ISL1208_REG_HR_MIL | enable;
410
411 regs[ISL1208_REG_DTA - offs] = bin2bcd(alarm_tm->tm_mday) | enable;
412 regs[ISL1208_REG_MOA - offs] = bin2bcd(alarm_tm->tm_mon + 1) | enable;
413 regs[ISL1208_REG_DWA - offs] = bin2bcd(alarm_tm->tm_wday & 7) | enable;
414
415 /* write ALARM registers */
416 err = isl1208_i2c_set_regs(client, offs, regs,
417 ISL1208_ALARM_SECTION_LEN);
418 if (err < 0) {
419 dev_err(&client->dev, "%s: writing ALARM section failed\n",
420 __func__);
421 return err;
422 }
423
424 err = isl1208_rtc_toggle_alarm(client, enable);
425 if (err)
426 return err;
427
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700428 return 0;
429}
430
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700431static int
432isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700433{
434 return isl1208_i2c_read_time(to_i2c_client(dev), tm);
435}
436
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700437static int
438isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700439{
440 int sr;
441 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
442
Chris Elstoncc6c2ca2008-12-23 13:57:10 -0800443 /* The clock has an 8 bit wide bcd-coded register (they never learn)
444 * for the year. tm_year is an offset from 1900 and we are interested
445 * in the 2000-2099 range, so any value less than 100 is invalid.
446 */
447 if (tm->tm_year < 100)
448 return -EINVAL;
449
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700450 regs[ISL1208_REG_SC] = bin2bcd(tm->tm_sec);
451 regs[ISL1208_REG_MN] = bin2bcd(tm->tm_min);
452 regs[ISL1208_REG_HR] = bin2bcd(tm->tm_hour) | ISL1208_REG_HR_MIL;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700453
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700454 regs[ISL1208_REG_DT] = bin2bcd(tm->tm_mday);
455 regs[ISL1208_REG_MO] = bin2bcd(tm->tm_mon + 1);
456 regs[ISL1208_REG_YR] = bin2bcd(tm->tm_year - 100);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700457
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700458 regs[ISL1208_REG_DW] = bin2bcd(tm->tm_wday & 7);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700459
460 sr = isl1208_i2c_get_sr(client);
461 if (sr < 0) {
462 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
463 return sr;
464 }
465
466 /* set WRTC */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700467 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700468 sr | ISL1208_REG_SR_WRTC);
469 if (sr < 0) {
470 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
471 return sr;
472 }
473
474 /* write RTC registers */
475 sr = isl1208_i2c_set_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
476 if (sr < 0) {
477 dev_err(&client->dev, "%s: writing RTC section failed\n",
478 __func__);
479 return sr;
480 }
481
482 /* clear WRTC again */
Denis Osterland5b9fc7952018-01-23 13:17:58 +0100483 sr = isl1208_i2c_get_sr(client);
484 if (sr < 0) {
485 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
486 return sr;
487 }
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700488 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700489 sr & ~ISL1208_REG_SR_WRTC);
490 if (sr < 0) {
491 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
492 return sr;
493 }
494
495 return 0;
496}
497
498
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700499static int
500isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700501{
502 return isl1208_i2c_set_time(to_i2c_client(dev), tm);
503}
504
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700505static int
506isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700507{
508 return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
509}
510
Ryan Malloncf044f02011-03-22 16:34:53 -0700511static int
512isl1208_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
513{
514 return isl1208_i2c_set_alarm(to_i2c_client(dev), alarm);
515}
516
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000517static ssize_t timestamp0_store(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf, size_t count)
520{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200521 struct i2c_client *client = to_i2c_client(dev->parent);
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000522 int sr;
523
524 sr = isl1208_i2c_get_sr(client);
525 if (sr < 0) {
526 dev_err(dev, "%s: reading SR failed\n", __func__);
527 return sr;
528 }
529
530 sr &= ~ISL1208_REG_SR_EVT;
531
532 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
533 if (sr < 0)
534 dev_err(dev, "%s: writing SR failed\n",
535 __func__);
536
537 return count;
538};
539
540static ssize_t timestamp0_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
542{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200543 struct i2c_client *client = to_i2c_client(dev->parent);
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000544 u8 regs[ISL1219_EVT_SECTION_LEN] = { 0, };
545 struct rtc_time tm;
546 int sr;
547
548 sr = isl1208_i2c_get_sr(client);
549 if (sr < 0) {
550 dev_err(dev, "%s: reading SR failed\n", __func__);
551 return sr;
552 }
553
554 if (!(sr & ISL1208_REG_SR_EVT))
555 return 0;
556
557 sr = isl1208_i2c_read_regs(client, ISL1219_REG_SCT, regs,
558 ISL1219_EVT_SECTION_LEN);
559 if (sr < 0) {
560 dev_err(dev, "%s: reading event section failed\n",
561 __func__);
562 return 0;
563 }
564
565 /* MSB of each alarm register is an enable bit */
566 tm.tm_sec = bcd2bin(regs[ISL1219_REG_SCT - ISL1219_REG_SCT] & 0x7f);
567 tm.tm_min = bcd2bin(regs[ISL1219_REG_MNT - ISL1219_REG_SCT] & 0x7f);
568 tm.tm_hour = bcd2bin(regs[ISL1219_REG_HRT - ISL1219_REG_SCT] & 0x3f);
569 tm.tm_mday = bcd2bin(regs[ISL1219_REG_DTT - ISL1219_REG_SCT] & 0x3f);
570 tm.tm_mon =
571 bcd2bin(regs[ISL1219_REG_MOT - ISL1219_REG_SCT] & 0x1f) - 1;
572 tm.tm_year = bcd2bin(regs[ISL1219_REG_YRT - ISL1219_REG_SCT]) + 100;
573
574 sr = rtc_valid_tm(&tm);
575 if (sr)
576 return sr;
577
578 return sprintf(buf, "%llu\n",
579 (unsigned long long)rtc_tm_to_time64(&tm));
580};
581
582static DEVICE_ATTR_RW(timestamp0);
583
Ryan Malloncf044f02011-03-22 16:34:53 -0700584static irqreturn_t
585isl1208_rtc_interrupt(int irq, void *data)
586{
587 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
588 struct i2c_client *client = data;
Jan Luebbe72fca4a2013-02-04 14:28:53 -0800589 struct rtc_device *rtc = i2c_get_clientdata(client);
Ryan Malloncf044f02011-03-22 16:34:53 -0700590 int handled = 0, sr, err;
591
592 /*
593 * I2C reads get NAK'ed if we read straight away after an interrupt?
594 * Using a mdelay/msleep didn't seem to help either, so we work around
595 * this by continually trying to read the register for a short time.
596 */
597 while (1) {
598 sr = isl1208_i2c_get_sr(client);
599 if (sr >= 0)
600 break;
601
602 if (time_after(jiffies, timeout)) {
603 dev_err(&client->dev, "%s: reading SR failed\n",
604 __func__);
605 return sr;
606 }
607 }
608
609 if (sr & ISL1208_REG_SR_ALM) {
610 dev_dbg(&client->dev, "alarm!\n");
611
Jan Luebbe72fca4a2013-02-04 14:28:53 -0800612 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
613
Ryan Malloncf044f02011-03-22 16:34:53 -0700614 /* Clear the alarm */
615 sr &= ~ISL1208_REG_SR_ALM;
616 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
617 if (sr < 0)
618 dev_err(&client->dev, "%s: writing SR failed\n",
619 __func__);
620 else
621 handled = 1;
622
623 /* Disable the alarm */
624 err = isl1208_rtc_toggle_alarm(client, 0);
625 if (err)
626 return err;
627 }
628
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000629 if (sr & ISL1208_REG_SR_EVT) {
630 sysfs_notify(&rtc->dev.kobj, NULL,
631 dev_attr_timestamp0.attr.name);
632 dev_warn(&client->dev, "event detected");
633 handled = 1;
634 }
635
Ryan Malloncf044f02011-03-22 16:34:53 -0700636 return handled ? IRQ_HANDLED : IRQ_NONE;
637}
638
David Brownellff8371a2006-09-30 23:28:17 -0700639static const struct rtc_class_ops isl1208_rtc_ops = {
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700640 .proc = isl1208_rtc_proc,
641 .read_time = isl1208_rtc_read_time,
642 .set_time = isl1208_rtc_set_time,
643 .read_alarm = isl1208_rtc_read_alarm,
Ryan Malloncf044f02011-03-22 16:34:53 -0700644 .set_alarm = isl1208_rtc_set_alarm,
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700645};
646
647/* sysfs interface */
648
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700649static ssize_t
650isl1208_sysfs_show_atrim(struct device *dev,
651 struct device_attribute *attr, char *buf)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700652{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200653 int atr = isl1208_i2c_get_atr(to_i2c_client(dev->parent));
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700654 if (atr < 0)
655 return atr;
656
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700657 return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700658}
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700659
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700660static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL);
661
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700662static ssize_t
663isl1208_sysfs_show_dtrim(struct device *dev,
664 struct device_attribute *attr, char *buf)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700665{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200666 int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev->parent));
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700667 if (dtr < 0)
668 return dtr;
669
670 return sprintf(buf, "%d ppm\n", dtr);
671}
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700672
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700673static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);
674
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700675static ssize_t
676isl1208_sysfs_show_usr(struct device *dev,
677 struct device_attribute *attr, char *buf)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700678{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200679 int usr = isl1208_i2c_get_usr(to_i2c_client(dev->parent));
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700680 if (usr < 0)
681 return usr;
682
683 return sprintf(buf, "0x%.4x\n", usr);
684}
685
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700686static ssize_t
687isl1208_sysfs_store_usr(struct device *dev,
688 struct device_attribute *attr,
689 const char *buf, size_t count)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700690{
691 int usr = -1;
692
693 if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
694 if (sscanf(buf, "%x", &usr) != 1)
695 return -EINVAL;
696 } else {
697 if (sscanf(buf, "%d", &usr) != 1)
698 return -EINVAL;
699 }
700
701 if (usr < 0 || usr > 0xffff)
702 return -EINVAL;
703
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200704 if (isl1208_i2c_set_usr(to_i2c_client(dev->parent), usr))
705 return -EIO;
706
707 return count;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700708}
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700709
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700710static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
711 isl1208_sysfs_store_usr);
712
H Hartley Sweetene17ab5cb2010-05-24 14:33:46 -0700713static struct attribute *isl1208_rtc_attrs[] = {
714 &dev_attr_atrim.attr,
715 &dev_attr_dtrim.attr,
716 &dev_attr_usr.attr,
717 NULL
718};
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700719
H Hartley Sweetene17ab5cb2010-05-24 14:33:46 -0700720static const struct attribute_group isl1208_rtc_sysfs_files = {
721 .attrs = isl1208_rtc_attrs,
722};
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700723
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000724static struct attribute *isl1219_rtc_attrs[] = {
725 &dev_attr_timestamp0.attr,
726 NULL
727};
728
729static const struct attribute_group isl1219_rtc_sysfs_files = {
730 .attrs = isl1219_rtc_attrs,
731};
732
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000733static int isl1208_setup_irq(struct i2c_client *client, int irq)
734{
735 int rc = devm_request_threaded_irq(&client->dev, irq, NULL,
736 isl1208_rtc_interrupt,
737 IRQF_SHARED | IRQF_ONESHOT,
738 isl1208_driver.driver.name,
739 client);
740 if (!rc) {
741 device_init_wakeup(&client->dev, 1);
742 enable_irq_wake(irq);
743 } else {
744 dev_err(&client->dev,
745 "Unable to request irq %d, no alarm support\n",
746 irq);
747 }
748 return rc;
749}
750
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700751static int
Jean Delvared2653e92008-04-29 23:11:39 +0200752isl1208_probe(struct i2c_client *client, const struct i2c_device_id *id)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700753{
754 int rc = 0;
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700755 struct rtc_device *rtc;
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000756 int evdet_irq = -1;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700757
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700758 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
759 return -ENODEV;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700760
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700761 if (isl1208_i2c_validate_client(client) < 0)
762 return -ENODEV;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700763
Denis Osterland236b7182018-03-05 10:43:53 +0000764 rtc = devm_rtc_allocate_device(&client->dev);
Sachin Kamatadce8c12013-11-12 15:10:30 -0800765 if (IS_ERR(rtc))
766 return PTR_ERR(rtc);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700767
Denis Osterland236b7182018-03-05 10:43:53 +0000768 rtc->ops = &isl1208_rtc_ops;
769
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700770 i2c_set_clientdata(client, rtc);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700771
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700772 rc = isl1208_i2c_get_sr(client);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700773 if (rc < 0) {
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700774 dev_err(&client->dev, "reading status failed\n");
Sachin Kamatadce8c12013-11-12 15:10:30 -0800775 return rc;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700776 }
777
778 if (rc & ISL1208_REG_SR_RTCF)
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700779 dev_warn(&client->dev, "rtc power failure detected, "
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700780 "please set clock.\n");
781
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000782 if (id->driver_data == TYPE_ISL1219) {
Denis Osterlandcfa30622018-07-24 11:31:21 +0000783 struct device_node *np = client->dev.of_node;
784 u32 evienb;
785
786 rc = i2c_smbus_read_byte_data(client, ISL1219_REG_EV);
787 if (rc < 0) {
788 dev_err(&client->dev, "failed to read EV reg\n");
789 return rc;
790 }
791 rc |= ISL1219_REG_EV_EVEN;
792 if (!of_property_read_u32(np, "isil,ev-evienb", &evienb)) {
793 if (evienb)
794 rc |= ISL1219_REG_EV_EVIENB;
795 else
796 rc &= ~ISL1219_REG_EV_EVIENB;
797 }
798 rc = i2c_smbus_write_byte_data(client, ISL1219_REG_EV, rc);
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000799 if (rc < 0) {
800 dev_err(&client->dev, "could not enable tamper detection\n");
801 return rc;
802 }
803 rc = rtc_add_group(rtc, &isl1219_rtc_sysfs_files);
804 if (rc)
805 return rc;
Denis Osterlandcfa30622018-07-24 11:31:21 +0000806 evdet_irq = of_irq_get_byname(np, "evdet");
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000807 }
808
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200809 rc = rtc_add_group(rtc, &isl1208_rtc_sysfs_files);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700810 if (rc)
Sachin Kamatadce8c12013-11-12 15:10:30 -0800811 return rc;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700812
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000813 if (client->irq > 0)
814 rc = isl1208_setup_irq(client, client->irq);
815 if (rc)
816 return rc;
817
818 if (evdet_irq > 0 && evdet_irq != client->irq)
819 rc = isl1208_setup_irq(client, evdet_irq);
820 if (rc)
821 return rc;
Michael Grzeschik9d327c22018-03-05 10:43:53 +0000822
Denis Osterland236b7182018-03-05 10:43:53 +0000823 return rtc_register_device(rtc);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700824}
825
Jean Delvare3760f732008-04-29 23:11:40 +0200826static const struct i2c_device_id isl1208_id[] = {
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000827 { "isl1208", TYPE_ISL1208 },
828 { "isl1218", TYPE_ISL1218 },
829 { "isl1219", TYPE_ISL1219 },
Jean Delvare3760f732008-04-29 23:11:40 +0200830 { }
831};
832MODULE_DEVICE_TABLE(i2c, isl1208_id);
833
Javier Martinez Canillas03835502017-03-03 11:29:20 -0300834static const struct of_device_id isl1208_of_match[] = {
835 { .compatible = "isil,isl1208" },
836 { .compatible = "isil,isl1218" },
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000837 { .compatible = "isil,isl1219" },
Javier Martinez Canillas03835502017-03-03 11:29:20 -0300838 { }
839};
840MODULE_DEVICE_TABLE(of, isl1208_of_match);
841
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700842static struct i2c_driver isl1208_driver = {
843 .driver = {
Javier Martinez Canillas03835502017-03-03 11:29:20 -0300844 .name = "rtc-isl1208",
845 .of_match_table = of_match_ptr(isl1208_of_match),
846 },
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700847 .probe = isl1208_probe,
Jean Delvare3760f732008-04-29 23:11:40 +0200848 .id_table = isl1208_id,
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700849};
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700850
Axel Lin0abc9202012-03-23 15:02:31 -0700851module_i2c_driver(isl1208_driver);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700852
853MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
854MODULE_DESCRIPTION("Intersil ISL1208 RTC driver");
855MODULE_LICENSE("GPL");