blob: 263af3d8cd9f58eeb4d81db3bd25b032c907c93e [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
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070013#include <linux/bcd.h>
Alexandre Belloni4b3a6a32018-09-19 03:13:22 +020014#include <linux/i2c.h>
15#include <linux/module.h>
Denis Osterland9ece7cd2018-07-24 11:31:21 +000016#include <linux/of_irq.h>
Alexandre Belloni4b3a6a32018-09-19 03:13:22 +020017#include <linux/rtc.h>
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070018
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070019/* Register map */
20/* rtc section */
21#define ISL1208_REG_SC 0x00
22#define ISL1208_REG_MN 0x01
23#define ISL1208_REG_HR 0x02
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070024#define ISL1208_REG_HR_MIL (1<<7) /* 24h/12h mode */
25#define ISL1208_REG_HR_PM (1<<5) /* PM/AM bit in 12h mode */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070026#define ISL1208_REG_DT 0x03
27#define ISL1208_REG_MO 0x04
28#define ISL1208_REG_YR 0x05
29#define ISL1208_REG_DW 0x06
30#define ISL1208_RTC_SECTION_LEN 7
31
32/* control/status section */
33#define ISL1208_REG_SR 0x07
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070034#define ISL1208_REG_SR_ARST (1<<7) /* auto reset */
35#define ISL1208_REG_SR_XTOSCB (1<<6) /* crystal oscillator */
36#define ISL1208_REG_SR_WRTC (1<<4) /* write rtc */
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000037#define ISL1208_REG_SR_EVT (1<<3) /* event */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070038#define ISL1208_REG_SR_ALM (1<<2) /* alarm */
39#define ISL1208_REG_SR_BAT (1<<1) /* battery */
40#define ISL1208_REG_SR_RTCF (1<<0) /* rtc fail */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070041#define ISL1208_REG_INT 0x08
Ryan Malloncf044f02011-03-22 16:34:53 -070042#define ISL1208_REG_INT_ALME (1<<6) /* alarm enable */
43#define ISL1208_REG_INT_IM (1<<7) /* interrupt/alarm mode */
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000044#define ISL1219_REG_EV 0x09
45#define ISL1219_REG_EV_EVEN (1<<4) /* event detection enable */
Denis Osterlandcfa30622018-07-24 11:31:21 +000046#define ISL1219_REG_EV_EVIENB (1<<7) /* event in pull-up disable */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070047#define ISL1208_REG_ATR 0x0a
48#define ISL1208_REG_DTR 0x0b
49
50/* alarm section */
51#define ISL1208_REG_SCA 0x0c
52#define ISL1208_REG_MNA 0x0d
53#define ISL1208_REG_HRA 0x0e
54#define ISL1208_REG_DTA 0x0f
55#define ISL1208_REG_MOA 0x10
56#define ISL1208_REG_DWA 0x11
57#define ISL1208_ALARM_SECTION_LEN 6
58
59/* user section */
60#define ISL1208_REG_USR1 0x12
61#define ISL1208_REG_USR2 0x13
62#define ISL1208_USR_SECTION_LEN 2
63
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000064/* event section */
65#define ISL1219_REG_SCT 0x14
66#define ISL1219_REG_MNT 0x15
67#define ISL1219_REG_HRT 0x16
68#define ISL1219_REG_DTT 0x17
69#define ISL1219_REG_MOT 0x18
70#define ISL1219_REG_YRT 0x19
71#define ISL1219_EVT_SECTION_LEN 6
72
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070073static struct i2c_driver isl1208_driver;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070074
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000075/* ISL1208 various variants */
76enum {
77 TYPE_ISL1208 = 0,
78 TYPE_ISL1218,
79 TYPE_ISL1219,
80};
81
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070082/* block read */
83static int
84isl1208_i2c_read_regs(struct i2c_client *client, u8 reg, u8 buf[],
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070085 unsigned len)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070086{
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070087 int ret;
88
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +000089 WARN_ON(reg > ISL1219_REG_YRT);
90 WARN_ON(reg + len > ISL1219_REG_YRT + 1);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070091
Trent Piephofacc23b2018-11-15 18:51:18 +000092 ret = i2c_smbus_read_i2c_block_data(client, reg, len, buf);
93 return (ret < 0) ? ret : 0;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -070094}
95
96/* block write */
97static int
98isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
Alessandro Zummo9edae7b2008-04-28 02:11:53 -070099 unsigned len)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700100{
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700101 int ret;
102
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000103 WARN_ON(reg > ISL1219_REG_YRT);
104 WARN_ON(reg + len > ISL1219_REG_YRT + 1);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700105
Trent Piephofacc23b2018-11-15 18:51:18 +0000106 ret = i2c_smbus_write_i2c_block_data(client, reg, len, buf);
107 return (ret < 0) ? ret : 0;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700108}
109
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400110/* simple check to see whether we have a isl1208 */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700111static int
112isl1208_i2c_validate_client(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700113{
114 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
115 u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
116 0x80, 0x80, 0x40, 0xc0, 0xe0, 0x00, 0xf8
117 };
118 int i;
119 int ret;
120
121 ret = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
122 if (ret < 0)
123 return ret;
124
125 for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700126 if (regs[i] & zero_mask[i]) /* check if bits are cleared */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700127 return -ENODEV;
128 }
129
130 return 0;
131}
132
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700133static int
134isl1208_i2c_get_sr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700135{
Sachin Kamatc91fd912013-11-12 15:10:26 -0800136 return i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700137}
138
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700139static int
140isl1208_i2c_get_atr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700141{
142 int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700143 if (atr < 0)
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700144 return atr;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700145
146 /* The 6bit value in the ATR register controls the load
147 * capacitance C_load * in steps of 0.25pF
148 *
149 * bit (1<<5) of the ATR register is inverted
150 *
151 * C_load(ATR=0x20) = 4.50pF
152 * C_load(ATR=0x00) = 12.50pF
153 * C_load(ATR=0x1f) = 20.25pF
154 *
155 */
156
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700157 atr &= 0x3f; /* mask out lsb */
158 atr ^= 1 << 5; /* invert 6th bit */
159 atr += 2 * 9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700160
161 return atr;
162}
163
Trent Piephoc8c97a4f2019-01-02 16:00:17 +0000164/* returns adjustment value + 100 */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700165static int
166isl1208_i2c_get_dtr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700167{
168 int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700169 if (dtr < 0)
170 return -EIO;
171
172 /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700173 dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700174
Trent Piephoc8c97a4f2019-01-02 16:00:17 +0000175 return dtr + 100;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700176}
177
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700178static int
179isl1208_i2c_get_usr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700180{
181 u8 buf[ISL1208_USR_SECTION_LEN] = { 0, };
182 int ret;
183
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700184 ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf,
185 ISL1208_USR_SECTION_LEN);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700186 if (ret < 0)
187 return ret;
188
189 return (buf[1] << 8) | buf[0];
190}
191
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700192static int
193isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700194{
195 u8 buf[ISL1208_USR_SECTION_LEN];
196
197 buf[0] = usr & 0xff;
198 buf[1] = (usr >> 8) & 0xff;
199
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700200 return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
201 ISL1208_USR_SECTION_LEN);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700202}
203
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700204static int
Ryan Malloncf044f02011-03-22 16:34:53 -0700205isl1208_rtc_toggle_alarm(struct i2c_client *client, int enable)
206{
207 int icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
208
209 if (icr < 0) {
210 dev_err(&client->dev, "%s: reading INT failed\n", __func__);
211 return icr;
212 }
213
214 if (enable)
215 icr |= ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM;
216 else
217 icr &= ~(ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM);
218
219 icr = i2c_smbus_write_byte_data(client, ISL1208_REG_INT, icr);
220 if (icr < 0) {
221 dev_err(&client->dev, "%s: writing INT failed\n", __func__);
222 return icr;
223 }
224
225 return 0;
226}
227
228static int
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700229isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700230{
231 struct i2c_client *const client = to_i2c_client(dev);
232 int sr, dtr, atr, usr;
233
234 sr = isl1208_i2c_get_sr(client);
235 if (sr < 0) {
236 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
237 return sr;
238 }
239
240 seq_printf(seq, "status_reg\t:%s%s%s%s%s%s (0x%.2x)\n",
241 (sr & ISL1208_REG_SR_RTCF) ? " RTCF" : "",
242 (sr & ISL1208_REG_SR_BAT) ? " BAT" : "",
243 (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
244 (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
245 (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700246 (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700247
248 seq_printf(seq, "batt_status\t: %s\n",
249 (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");
250
251 dtr = isl1208_i2c_get_dtr(client);
Trent Piephoc8c97a4f2019-01-02 16:00:17 +0000252 if (dtr >= 0)
253 seq_printf(seq, "digital_trim\t: %d ppm\n", dtr - 100);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700254
255 atr = isl1208_i2c_get_atr(client);
256 if (atr >= 0)
257 seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700258 atr >> 2, (atr & 0x3) * 25);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700259
260 usr = isl1208_i2c_get_usr(client);
261 if (usr >= 0)
262 seq_printf(seq, "user_data\t: 0x%.4x\n", usr);
263
264 return 0;
265}
266
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700267static int
268isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700269{
270 int sr;
271 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
272
273 sr = isl1208_i2c_get_sr(client);
274 if (sr < 0) {
275 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
276 return -EIO;
277 }
278
279 sr = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
280 if (sr < 0) {
281 dev_err(&client->dev, "%s: reading RTC section failed\n",
282 __func__);
283 return sr;
284 }
285
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700286 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SC]);
287 tm->tm_min = bcd2bin(regs[ISL1208_REG_MN]);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700288
289 /* HR field has a more complex interpretation */
290 {
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700291 const u8 _hr = regs[ISL1208_REG_HR];
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700292 if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700293 tm->tm_hour = bcd2bin(_hr & 0x3f);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700294 else {
295 /* 12h format */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700296 tm->tm_hour = bcd2bin(_hr & 0x1f);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700297 if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700298 tm->tm_hour += 12;
299 }
300 }
301
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700302 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DT]);
303 tm->tm_mon = bcd2bin(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
304 tm->tm_year = bcd2bin(regs[ISL1208_REG_YR]) + 100;
305 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DW]);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700306
307 return 0;
308}
309
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700310static int
311isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700312{
313 struct rtc_time *const tm = &alarm->time;
314 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
Ryan Malloncf044f02011-03-22 16:34:53 -0700315 int icr, yr, sr = isl1208_i2c_get_sr(client);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700316
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700317 if (sr < 0) {
318 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
319 return sr;
320 }
321
322 sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700323 ISL1208_ALARM_SECTION_LEN);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700324 if (sr < 0) {
325 dev_err(&client->dev, "%s: reading alarm section failed\n",
326 __func__);
327 return sr;
328 }
329
330 /* MSB of each alarm register is an enable bit */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700331 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
332 tm->tm_min = bcd2bin(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
333 tm->tm_hour = bcd2bin(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
334 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700335 tm->tm_mon =
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700336 bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
337 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700338
Ryan Malloncf044f02011-03-22 16:34:53 -0700339 /* The alarm doesn't store the year so get it from the rtc section */
340 yr = i2c_smbus_read_byte_data(client, ISL1208_REG_YR);
341 if (yr < 0) {
342 dev_err(&client->dev, "%s: reading RTC YR failed\n", __func__);
343 return yr;
344 }
345 tm->tm_year = bcd2bin(yr) + 100;
346
347 icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
348 if (icr < 0) {
349 dev_err(&client->dev, "%s: reading INT failed\n", __func__);
350 return icr;
351 }
352 alarm->enabled = !!(icr & ISL1208_REG_INT_ALME);
353
354 return 0;
355}
356
357static int
358isl1208_i2c_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
359{
360 struct rtc_time *alarm_tm = &alarm->time;
361 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
362 const int offs = ISL1208_REG_SCA;
Ryan Malloncf044f02011-03-22 16:34:53 -0700363 struct rtc_time rtc_tm;
364 int err, enable;
365
366 err = isl1208_i2c_read_time(client, &rtc_tm);
367 if (err)
368 return err;
Ryan Malloncf044f02011-03-22 16:34:53 -0700369
370 /* If the alarm time is before the current time disable the alarm */
Xunlei Pangf118db12015-06-12 10:04:11 +0800371 if (!alarm->enabled || rtc_tm_sub(alarm_tm, &rtc_tm) <= 0)
Ryan Malloncf044f02011-03-22 16:34:53 -0700372 enable = 0x00;
373 else
374 enable = 0x80;
375
376 /* Program the alarm and enable it for each setting */
377 regs[ISL1208_REG_SCA - offs] = bin2bcd(alarm_tm->tm_sec) | enable;
378 regs[ISL1208_REG_MNA - offs] = bin2bcd(alarm_tm->tm_min) | enable;
379 regs[ISL1208_REG_HRA - offs] = bin2bcd(alarm_tm->tm_hour) |
380 ISL1208_REG_HR_MIL | enable;
381
382 regs[ISL1208_REG_DTA - offs] = bin2bcd(alarm_tm->tm_mday) | enable;
383 regs[ISL1208_REG_MOA - offs] = bin2bcd(alarm_tm->tm_mon + 1) | enable;
384 regs[ISL1208_REG_DWA - offs] = bin2bcd(alarm_tm->tm_wday & 7) | enable;
385
386 /* write ALARM registers */
387 err = isl1208_i2c_set_regs(client, offs, regs,
388 ISL1208_ALARM_SECTION_LEN);
389 if (err < 0) {
390 dev_err(&client->dev, "%s: writing ALARM section failed\n",
391 __func__);
392 return err;
393 }
394
395 err = isl1208_rtc_toggle_alarm(client, enable);
396 if (err)
397 return err;
398
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700399 return 0;
400}
401
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700402static int
403isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700404{
405 return isl1208_i2c_read_time(to_i2c_client(dev), tm);
406}
407
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700408static int
409isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700410{
411 int sr;
412 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
413
Chris Elstoncc6c2ca2008-12-23 13:57:10 -0800414 /* The clock has an 8 bit wide bcd-coded register (they never learn)
415 * for the year. tm_year is an offset from 1900 and we are interested
416 * in the 2000-2099 range, so any value less than 100 is invalid.
417 */
418 if (tm->tm_year < 100)
419 return -EINVAL;
420
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700421 regs[ISL1208_REG_SC] = bin2bcd(tm->tm_sec);
422 regs[ISL1208_REG_MN] = bin2bcd(tm->tm_min);
423 regs[ISL1208_REG_HR] = bin2bcd(tm->tm_hour) | ISL1208_REG_HR_MIL;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700424
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700425 regs[ISL1208_REG_DT] = bin2bcd(tm->tm_mday);
426 regs[ISL1208_REG_MO] = bin2bcd(tm->tm_mon + 1);
427 regs[ISL1208_REG_YR] = bin2bcd(tm->tm_year - 100);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700428
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700429 regs[ISL1208_REG_DW] = bin2bcd(tm->tm_wday & 7);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700430
431 sr = isl1208_i2c_get_sr(client);
432 if (sr < 0) {
433 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
434 return sr;
435 }
436
437 /* set WRTC */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700438 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700439 sr | ISL1208_REG_SR_WRTC);
440 if (sr < 0) {
441 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
442 return sr;
443 }
444
445 /* write RTC registers */
446 sr = isl1208_i2c_set_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
447 if (sr < 0) {
448 dev_err(&client->dev, "%s: writing RTC section failed\n",
449 __func__);
450 return sr;
451 }
452
453 /* clear WRTC again */
Denis Osterland5b9fc7952018-01-23 13:17:58 +0100454 sr = isl1208_i2c_get_sr(client);
455 if (sr < 0) {
456 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
457 return sr;
458 }
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700459 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700460 sr & ~ISL1208_REG_SR_WRTC);
461 if (sr < 0) {
462 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
463 return sr;
464 }
465
466 return 0;
467}
468
469
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700470static int
471isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700472{
473 return isl1208_i2c_set_time(to_i2c_client(dev), tm);
474}
475
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700476static int
477isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700478{
479 return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
480}
481
Ryan Malloncf044f02011-03-22 16:34:53 -0700482static int
483isl1208_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
484{
485 return isl1208_i2c_set_alarm(to_i2c_client(dev), alarm);
486}
487
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000488static ssize_t timestamp0_store(struct device *dev,
489 struct device_attribute *attr,
490 const char *buf, size_t count)
491{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200492 struct i2c_client *client = to_i2c_client(dev->parent);
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000493 int sr;
494
495 sr = isl1208_i2c_get_sr(client);
496 if (sr < 0) {
497 dev_err(dev, "%s: reading SR failed\n", __func__);
498 return sr;
499 }
500
501 sr &= ~ISL1208_REG_SR_EVT;
502
503 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
504 if (sr < 0)
505 dev_err(dev, "%s: writing SR failed\n",
506 __func__);
507
508 return count;
509};
510
511static ssize_t timestamp0_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200514 struct i2c_client *client = to_i2c_client(dev->parent);
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000515 u8 regs[ISL1219_EVT_SECTION_LEN] = { 0, };
516 struct rtc_time tm;
517 int sr;
518
519 sr = isl1208_i2c_get_sr(client);
520 if (sr < 0) {
521 dev_err(dev, "%s: reading SR failed\n", __func__);
522 return sr;
523 }
524
525 if (!(sr & ISL1208_REG_SR_EVT))
526 return 0;
527
528 sr = isl1208_i2c_read_regs(client, ISL1219_REG_SCT, regs,
529 ISL1219_EVT_SECTION_LEN);
530 if (sr < 0) {
531 dev_err(dev, "%s: reading event section failed\n",
532 __func__);
533 return 0;
534 }
535
536 /* MSB of each alarm register is an enable bit */
537 tm.tm_sec = bcd2bin(regs[ISL1219_REG_SCT - ISL1219_REG_SCT] & 0x7f);
538 tm.tm_min = bcd2bin(regs[ISL1219_REG_MNT - ISL1219_REG_SCT] & 0x7f);
539 tm.tm_hour = bcd2bin(regs[ISL1219_REG_HRT - ISL1219_REG_SCT] & 0x3f);
540 tm.tm_mday = bcd2bin(regs[ISL1219_REG_DTT - ISL1219_REG_SCT] & 0x3f);
541 tm.tm_mon =
542 bcd2bin(regs[ISL1219_REG_MOT - ISL1219_REG_SCT] & 0x1f) - 1;
543 tm.tm_year = bcd2bin(regs[ISL1219_REG_YRT - ISL1219_REG_SCT]) + 100;
544
545 sr = rtc_valid_tm(&tm);
546 if (sr)
547 return sr;
548
549 return sprintf(buf, "%llu\n",
550 (unsigned long long)rtc_tm_to_time64(&tm));
551};
552
553static DEVICE_ATTR_RW(timestamp0);
554
Ryan Malloncf044f02011-03-22 16:34:53 -0700555static irqreturn_t
556isl1208_rtc_interrupt(int irq, void *data)
557{
558 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
559 struct i2c_client *client = data;
Jan Luebbe72fca4a2013-02-04 14:28:53 -0800560 struct rtc_device *rtc = i2c_get_clientdata(client);
Ryan Malloncf044f02011-03-22 16:34:53 -0700561 int handled = 0, sr, err;
562
563 /*
564 * I2C reads get NAK'ed if we read straight away after an interrupt?
565 * Using a mdelay/msleep didn't seem to help either, so we work around
566 * this by continually trying to read the register for a short time.
567 */
568 while (1) {
569 sr = isl1208_i2c_get_sr(client);
570 if (sr >= 0)
571 break;
572
573 if (time_after(jiffies, timeout)) {
574 dev_err(&client->dev, "%s: reading SR failed\n",
575 __func__);
576 return sr;
577 }
578 }
579
580 if (sr & ISL1208_REG_SR_ALM) {
581 dev_dbg(&client->dev, "alarm!\n");
582
Jan Luebbe72fca4a2013-02-04 14:28:53 -0800583 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
584
Ryan Malloncf044f02011-03-22 16:34:53 -0700585 /* Clear the alarm */
586 sr &= ~ISL1208_REG_SR_ALM;
587 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
588 if (sr < 0)
589 dev_err(&client->dev, "%s: writing SR failed\n",
590 __func__);
591 else
592 handled = 1;
593
594 /* Disable the alarm */
595 err = isl1208_rtc_toggle_alarm(client, 0);
596 if (err)
597 return err;
598 }
599
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000600 if (sr & ISL1208_REG_SR_EVT) {
601 sysfs_notify(&rtc->dev.kobj, NULL,
602 dev_attr_timestamp0.attr.name);
603 dev_warn(&client->dev, "event detected");
604 handled = 1;
605 }
606
Ryan Malloncf044f02011-03-22 16:34:53 -0700607 return handled ? IRQ_HANDLED : IRQ_NONE;
608}
609
David Brownellff8371a2006-09-30 23:28:17 -0700610static const struct rtc_class_ops isl1208_rtc_ops = {
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700611 .proc = isl1208_rtc_proc,
612 .read_time = isl1208_rtc_read_time,
613 .set_time = isl1208_rtc_set_time,
614 .read_alarm = isl1208_rtc_read_alarm,
Ryan Malloncf044f02011-03-22 16:34:53 -0700615 .set_alarm = isl1208_rtc_set_alarm,
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700616};
617
618/* sysfs interface */
619
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700620static ssize_t
621isl1208_sysfs_show_atrim(struct device *dev,
622 struct device_attribute *attr, char *buf)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700623{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200624 int atr = isl1208_i2c_get_atr(to_i2c_client(dev->parent));
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700625 if (atr < 0)
626 return atr;
627
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700628 return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700629}
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700630
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700631static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL);
632
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700633static ssize_t
634isl1208_sysfs_show_dtrim(struct device *dev,
635 struct device_attribute *attr, char *buf)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700636{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200637 int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev->parent));
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700638 if (dtr < 0)
639 return dtr;
640
Trent Piephoc8c97a4f2019-01-02 16:00:17 +0000641 return sprintf(buf, "%d ppm\n", dtr - 100);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700642}
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700643
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700644static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);
645
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700646static ssize_t
647isl1208_sysfs_show_usr(struct device *dev,
648 struct device_attribute *attr, char *buf)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700649{
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200650 int usr = isl1208_i2c_get_usr(to_i2c_client(dev->parent));
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700651 if (usr < 0)
652 return usr;
653
654 return sprintf(buf, "0x%.4x\n", usr);
655}
656
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700657static ssize_t
658isl1208_sysfs_store_usr(struct device *dev,
659 struct device_attribute *attr,
660 const char *buf, size_t count)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700661{
662 int usr = -1;
663
664 if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
665 if (sscanf(buf, "%x", &usr) != 1)
666 return -EINVAL;
667 } else {
668 if (sscanf(buf, "%d", &usr) != 1)
669 return -EINVAL;
670 }
671
672 if (usr < 0 || usr > 0xffff)
673 return -EINVAL;
674
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200675 if (isl1208_i2c_set_usr(to_i2c_client(dev->parent), usr))
676 return -EIO;
677
678 return count;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700679}
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700680
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700681static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
682 isl1208_sysfs_store_usr);
683
H Hartley Sweetene17ab5cb2010-05-24 14:33:46 -0700684static struct attribute *isl1208_rtc_attrs[] = {
685 &dev_attr_atrim.attr,
686 &dev_attr_dtrim.attr,
687 &dev_attr_usr.attr,
688 NULL
689};
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700690
H Hartley Sweetene17ab5cb2010-05-24 14:33:46 -0700691static const struct attribute_group isl1208_rtc_sysfs_files = {
692 .attrs = isl1208_rtc_attrs,
693};
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700694
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000695static struct attribute *isl1219_rtc_attrs[] = {
696 &dev_attr_timestamp0.attr,
697 NULL
698};
699
700static const struct attribute_group isl1219_rtc_sysfs_files = {
701 .attrs = isl1219_rtc_attrs,
702};
703
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000704static int isl1208_setup_irq(struct i2c_client *client, int irq)
705{
706 int rc = devm_request_threaded_irq(&client->dev, irq, NULL,
707 isl1208_rtc_interrupt,
708 IRQF_SHARED | IRQF_ONESHOT,
709 isl1208_driver.driver.name,
710 client);
711 if (!rc) {
712 device_init_wakeup(&client->dev, 1);
713 enable_irq_wake(irq);
714 } else {
715 dev_err(&client->dev,
716 "Unable to request irq %d, no alarm support\n",
717 irq);
718 }
719 return rc;
720}
721
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700722static int
Jean Delvared2653e92008-04-29 23:11:39 +0200723isl1208_probe(struct i2c_client *client, const struct i2c_device_id *id)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700724{
725 int rc = 0;
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700726 struct rtc_device *rtc;
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000727 int evdet_irq = -1;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700728
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700729 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
730 return -ENODEV;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700731
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700732 if (isl1208_i2c_validate_client(client) < 0)
733 return -ENODEV;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700734
Denis Osterland236b7182018-03-05 10:43:53 +0000735 rtc = devm_rtc_allocate_device(&client->dev);
Sachin Kamatadce8c12013-11-12 15:10:30 -0800736 if (IS_ERR(rtc))
737 return PTR_ERR(rtc);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700738
Denis Osterland236b7182018-03-05 10:43:53 +0000739 rtc->ops = &isl1208_rtc_ops;
740
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700741 i2c_set_clientdata(client, rtc);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700742
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700743 rc = isl1208_i2c_get_sr(client);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700744 if (rc < 0) {
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700745 dev_err(&client->dev, "reading status failed\n");
Sachin Kamatadce8c12013-11-12 15:10:30 -0800746 return rc;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700747 }
748
749 if (rc & ISL1208_REG_SR_RTCF)
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700750 dev_warn(&client->dev, "rtc power failure detected, "
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700751 "please set clock.\n");
752
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000753 if (id->driver_data == TYPE_ISL1219) {
Denis Osterlandcfa30622018-07-24 11:31:21 +0000754 struct device_node *np = client->dev.of_node;
755 u32 evienb;
756
757 rc = i2c_smbus_read_byte_data(client, ISL1219_REG_EV);
758 if (rc < 0) {
759 dev_err(&client->dev, "failed to read EV reg\n");
760 return rc;
761 }
762 rc |= ISL1219_REG_EV_EVEN;
763 if (!of_property_read_u32(np, "isil,ev-evienb", &evienb)) {
764 if (evienb)
765 rc |= ISL1219_REG_EV_EVIENB;
766 else
767 rc &= ~ISL1219_REG_EV_EVIENB;
768 }
769 rc = i2c_smbus_write_byte_data(client, ISL1219_REG_EV, rc);
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000770 if (rc < 0) {
771 dev_err(&client->dev, "could not enable tamper detection\n");
772 return rc;
773 }
774 rc = rtc_add_group(rtc, &isl1219_rtc_sysfs_files);
775 if (rc)
776 return rc;
Denis Osterlandcfa30622018-07-24 11:31:21 +0000777 evdet_irq = of_irq_get_byname(np, "evdet");
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000778 }
779
Alexandre Belloni1b4c7942018-09-15 13:29:56 +0200780 rc = rtc_add_group(rtc, &isl1208_rtc_sysfs_files);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700781 if (rc)
Sachin Kamatadce8c12013-11-12 15:10:30 -0800782 return rc;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700783
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000784 if (client->irq > 0)
785 rc = isl1208_setup_irq(client, client->irq);
786 if (rc)
787 return rc;
788
789 if (evdet_irq > 0 && evdet_irq != client->irq)
790 rc = isl1208_setup_irq(client, evdet_irq);
791 if (rc)
792 return rc;
Michael Grzeschik9d327c22018-03-05 10:43:53 +0000793
Denis Osterland236b7182018-03-05 10:43:53 +0000794 return rtc_register_device(rtc);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700795}
796
Jean Delvare3760f732008-04-29 23:11:40 +0200797static const struct i2c_device_id isl1208_id[] = {
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000798 { "isl1208", TYPE_ISL1208 },
799 { "isl1218", TYPE_ISL1218 },
800 { "isl1219", TYPE_ISL1219 },
Jean Delvare3760f732008-04-29 23:11:40 +0200801 { }
802};
803MODULE_DEVICE_TABLE(i2c, isl1208_id);
804
Javier Martinez Canillas03835502017-03-03 11:29:20 -0300805static const struct of_device_id isl1208_of_match[] = {
806 { .compatible = "isil,isl1208" },
807 { .compatible = "isil,isl1218" },
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000808 { .compatible = "isil,isl1219" },
Javier Martinez Canillas03835502017-03-03 11:29:20 -0300809 { }
810};
811MODULE_DEVICE_TABLE(of, isl1208_of_match);
812
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700813static struct i2c_driver isl1208_driver = {
814 .driver = {
Javier Martinez Canillas03835502017-03-03 11:29:20 -0300815 .name = "rtc-isl1208",
816 .of_match_table = of_match_ptr(isl1208_of_match),
817 },
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700818 .probe = isl1208_probe,
Jean Delvare3760f732008-04-29 23:11:40 +0200819 .id_table = isl1208_id,
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700820};
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700821
Axel Lin0abc9202012-03-23 15:02:31 -0700822module_i2c_driver(isl1208_driver);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700823
824MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
825MODULE_DESCRIPTION("Intersil ISL1208 RTC driver");
826MODULE_LICENSE("GPL");