blob: f4eb3e11de28b77464328d2b524927abd965ce71 [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 */
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{
87 u8 reg_addr[1] = { reg };
88 struct i2c_msg msgs[2] = {
Shubhrajyoti D885ccbb2012-10-04 17:14:27 -070089 {
90 .addr = client->addr,
91 .len = sizeof(reg_addr),
92 .buf = reg_addr
93 },
94 {
95 .addr = client->addr,
96 .flags = I2C_M_RD,
97 .len = len,
98 .buf = buf
99 }
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700100 };
101 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
106 ret = i2c_transfer(client->adapter, msgs, 2);
107 if (ret > 0)
108 ret = 0;
109 return ret;
110}
111
112/* block write */
113static int
114isl1208_i2c_set_regs(struct i2c_client *client, u8 reg, u8 const buf[],
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700115 unsigned len)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700116{
117 u8 i2c_buf[ISL1208_REG_USR2 + 2];
118 struct i2c_msg msgs[1] = {
Shubhrajyoti D885ccbb2012-10-04 17:14:27 -0700119 {
120 .addr = client->addr,
121 .len = len + 1,
122 .buf = i2c_buf
123 }
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700124 };
125 int ret;
126
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000127 WARN_ON(reg > ISL1219_REG_YRT);
128 WARN_ON(reg + len > ISL1219_REG_YRT + 1);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700129
130 i2c_buf[0] = reg;
131 memcpy(&i2c_buf[1], &buf[0], len);
132
133 ret = i2c_transfer(client->adapter, msgs, 1);
134 if (ret > 0)
135 ret = 0;
136 return ret;
137}
138
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400139/* simple check to see whether we have a isl1208 */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700140static int
141isl1208_i2c_validate_client(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700142{
143 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
144 u8 zero_mask[ISL1208_RTC_SECTION_LEN] = {
145 0x80, 0x80, 0x40, 0xc0, 0xe0, 0x00, 0xf8
146 };
147 int i;
148 int ret;
149
150 ret = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
151 if (ret < 0)
152 return ret;
153
154 for (i = 0; i < ISL1208_RTC_SECTION_LEN; ++i) {
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700155 if (regs[i] & zero_mask[i]) /* check if bits are cleared */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700156 return -ENODEV;
157 }
158
159 return 0;
160}
161
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700162static int
163isl1208_i2c_get_sr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700164{
Sachin Kamatc91fd912013-11-12 15:10:26 -0800165 return i2c_smbus_read_byte_data(client, ISL1208_REG_SR);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700166}
167
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700168static int
169isl1208_i2c_get_atr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700170{
171 int atr = i2c_smbus_read_byte_data(client, ISL1208_REG_ATR);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700172 if (atr < 0)
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700173 return atr;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700174
175 /* The 6bit value in the ATR register controls the load
176 * capacitance C_load * in steps of 0.25pF
177 *
178 * bit (1<<5) of the ATR register is inverted
179 *
180 * C_load(ATR=0x20) = 4.50pF
181 * C_load(ATR=0x00) = 12.50pF
182 * C_load(ATR=0x1f) = 20.25pF
183 *
184 */
185
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700186 atr &= 0x3f; /* mask out lsb */
187 atr ^= 1 << 5; /* invert 6th bit */
188 atr += 2 * 9; /* add offset of 4.5pF; unit[atr] = 0.25pF */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700189
190 return atr;
191}
192
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700193static int
194isl1208_i2c_get_dtr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700195{
196 int dtr = i2c_smbus_read_byte_data(client, ISL1208_REG_DTR);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700197 if (dtr < 0)
198 return -EIO;
199
200 /* dtr encodes adjustments of {-60,-40,-20,0,20,40,60} ppm */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700201 dtr = ((dtr & 0x3) * 20) * (dtr & (1 << 2) ? -1 : 1);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700202
203 return dtr;
204}
205
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700206static int
207isl1208_i2c_get_usr(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700208{
209 u8 buf[ISL1208_USR_SECTION_LEN] = { 0, };
210 int ret;
211
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700212 ret = isl1208_i2c_read_regs(client, ISL1208_REG_USR1, buf,
213 ISL1208_USR_SECTION_LEN);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700214 if (ret < 0)
215 return ret;
216
217 return (buf[1] << 8) | buf[0];
218}
219
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700220static int
221isl1208_i2c_set_usr(struct i2c_client *client, u16 usr)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700222{
223 u8 buf[ISL1208_USR_SECTION_LEN];
224
225 buf[0] = usr & 0xff;
226 buf[1] = (usr >> 8) & 0xff;
227
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700228 return isl1208_i2c_set_regs(client, ISL1208_REG_USR1, buf,
229 ISL1208_USR_SECTION_LEN);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700230}
231
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700232static int
Ryan Malloncf044f02011-03-22 16:34:53 -0700233isl1208_rtc_toggle_alarm(struct i2c_client *client, int enable)
234{
235 int icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
236
237 if (icr < 0) {
238 dev_err(&client->dev, "%s: reading INT failed\n", __func__);
239 return icr;
240 }
241
242 if (enable)
243 icr |= ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM;
244 else
245 icr &= ~(ISL1208_REG_INT_ALME | ISL1208_REG_INT_IM);
246
247 icr = i2c_smbus_write_byte_data(client, ISL1208_REG_INT, icr);
248 if (icr < 0) {
249 dev_err(&client->dev, "%s: writing INT failed\n", __func__);
250 return icr;
251 }
252
253 return 0;
254}
255
256static int
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700257isl1208_rtc_proc(struct device *dev, struct seq_file *seq)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700258{
259 struct i2c_client *const client = to_i2c_client(dev);
260 int sr, dtr, atr, usr;
261
262 sr = isl1208_i2c_get_sr(client);
263 if (sr < 0) {
264 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
265 return sr;
266 }
267
268 seq_printf(seq, "status_reg\t:%s%s%s%s%s%s (0x%.2x)\n",
269 (sr & ISL1208_REG_SR_RTCF) ? " RTCF" : "",
270 (sr & ISL1208_REG_SR_BAT) ? " BAT" : "",
271 (sr & ISL1208_REG_SR_ALM) ? " ALM" : "",
272 (sr & ISL1208_REG_SR_WRTC) ? " WRTC" : "",
273 (sr & ISL1208_REG_SR_XTOSCB) ? " XTOSCB" : "",
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700274 (sr & ISL1208_REG_SR_ARST) ? " ARST" : "", sr);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700275
276 seq_printf(seq, "batt_status\t: %s\n",
277 (sr & ISL1208_REG_SR_RTCF) ? "bad" : "okay");
278
279 dtr = isl1208_i2c_get_dtr(client);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700280 if (dtr >= 0 - 1)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700281 seq_printf(seq, "digital_trim\t: %d ppm\n", dtr);
282
283 atr = isl1208_i2c_get_atr(client);
284 if (atr >= 0)
285 seq_printf(seq, "analog_trim\t: %d.%.2d pF\n",
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700286 atr >> 2, (atr & 0x3) * 25);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700287
288 usr = isl1208_i2c_get_usr(client);
289 if (usr >= 0)
290 seq_printf(seq, "user_data\t: 0x%.4x\n", usr);
291
292 return 0;
293}
294
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700295static int
296isl1208_i2c_read_time(struct i2c_client *client, struct rtc_time *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700297{
298 int sr;
299 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
300
301 sr = isl1208_i2c_get_sr(client);
302 if (sr < 0) {
303 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
304 return -EIO;
305 }
306
307 sr = isl1208_i2c_read_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
308 if (sr < 0) {
309 dev_err(&client->dev, "%s: reading RTC section failed\n",
310 __func__);
311 return sr;
312 }
313
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700314 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SC]);
315 tm->tm_min = bcd2bin(regs[ISL1208_REG_MN]);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700316
317 /* HR field has a more complex interpretation */
318 {
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700319 const u8 _hr = regs[ISL1208_REG_HR];
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700320 if (_hr & ISL1208_REG_HR_MIL) /* 24h format */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700321 tm->tm_hour = bcd2bin(_hr & 0x3f);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700322 else {
323 /* 12h format */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700324 tm->tm_hour = bcd2bin(_hr & 0x1f);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700325 if (_hr & ISL1208_REG_HR_PM) /* PM flag set */
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700326 tm->tm_hour += 12;
327 }
328 }
329
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700330 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DT]);
331 tm->tm_mon = bcd2bin(regs[ISL1208_REG_MO]) - 1; /* rtc starts at 1 */
332 tm->tm_year = bcd2bin(regs[ISL1208_REG_YR]) + 100;
333 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DW]);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700334
335 return 0;
336}
337
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700338static int
339isl1208_i2c_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700340{
341 struct rtc_time *const tm = &alarm->time;
342 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
Ryan Malloncf044f02011-03-22 16:34:53 -0700343 int icr, yr, sr = isl1208_i2c_get_sr(client);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700344
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700345 if (sr < 0) {
346 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
347 return sr;
348 }
349
350 sr = isl1208_i2c_read_regs(client, ISL1208_REG_SCA, regs,
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700351 ISL1208_ALARM_SECTION_LEN);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700352 if (sr < 0) {
353 dev_err(&client->dev, "%s: reading alarm section failed\n",
354 __func__);
355 return sr;
356 }
357
358 /* MSB of each alarm register is an enable bit */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700359 tm->tm_sec = bcd2bin(regs[ISL1208_REG_SCA - ISL1208_REG_SCA] & 0x7f);
360 tm->tm_min = bcd2bin(regs[ISL1208_REG_MNA - ISL1208_REG_SCA] & 0x7f);
361 tm->tm_hour = bcd2bin(regs[ISL1208_REG_HRA - ISL1208_REG_SCA] & 0x3f);
362 tm->tm_mday = bcd2bin(regs[ISL1208_REG_DTA - ISL1208_REG_SCA] & 0x3f);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700363 tm->tm_mon =
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700364 bcd2bin(regs[ISL1208_REG_MOA - ISL1208_REG_SCA] & 0x1f) - 1;
365 tm->tm_wday = bcd2bin(regs[ISL1208_REG_DWA - ISL1208_REG_SCA] & 0x03);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700366
Ryan Malloncf044f02011-03-22 16:34:53 -0700367 /* The alarm doesn't store the year so get it from the rtc section */
368 yr = i2c_smbus_read_byte_data(client, ISL1208_REG_YR);
369 if (yr < 0) {
370 dev_err(&client->dev, "%s: reading RTC YR failed\n", __func__);
371 return yr;
372 }
373 tm->tm_year = bcd2bin(yr) + 100;
374
375 icr = i2c_smbus_read_byte_data(client, ISL1208_REG_INT);
376 if (icr < 0) {
377 dev_err(&client->dev, "%s: reading INT failed\n", __func__);
378 return icr;
379 }
380 alarm->enabled = !!(icr & ISL1208_REG_INT_ALME);
381
382 return 0;
383}
384
385static int
386isl1208_i2c_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alarm)
387{
388 struct rtc_time *alarm_tm = &alarm->time;
389 u8 regs[ISL1208_ALARM_SECTION_LEN] = { 0, };
390 const int offs = ISL1208_REG_SCA;
Ryan Malloncf044f02011-03-22 16:34:53 -0700391 struct rtc_time rtc_tm;
392 int err, enable;
393
394 err = isl1208_i2c_read_time(client, &rtc_tm);
395 if (err)
396 return err;
Ryan Malloncf044f02011-03-22 16:34:53 -0700397
398 /* If the alarm time is before the current time disable the alarm */
Xunlei Pangf118db12015-06-12 10:04:11 +0800399 if (!alarm->enabled || rtc_tm_sub(alarm_tm, &rtc_tm) <= 0)
Ryan Malloncf044f02011-03-22 16:34:53 -0700400 enable = 0x00;
401 else
402 enable = 0x80;
403
404 /* Program the alarm and enable it for each setting */
405 regs[ISL1208_REG_SCA - offs] = bin2bcd(alarm_tm->tm_sec) | enable;
406 regs[ISL1208_REG_MNA - offs] = bin2bcd(alarm_tm->tm_min) | enable;
407 regs[ISL1208_REG_HRA - offs] = bin2bcd(alarm_tm->tm_hour) |
408 ISL1208_REG_HR_MIL | enable;
409
410 regs[ISL1208_REG_DTA - offs] = bin2bcd(alarm_tm->tm_mday) | enable;
411 regs[ISL1208_REG_MOA - offs] = bin2bcd(alarm_tm->tm_mon + 1) | enable;
412 regs[ISL1208_REG_DWA - offs] = bin2bcd(alarm_tm->tm_wday & 7) | enable;
413
414 /* write ALARM registers */
415 err = isl1208_i2c_set_regs(client, offs, regs,
416 ISL1208_ALARM_SECTION_LEN);
417 if (err < 0) {
418 dev_err(&client->dev, "%s: writing ALARM section failed\n",
419 __func__);
420 return err;
421 }
422
423 err = isl1208_rtc_toggle_alarm(client, enable);
424 if (err)
425 return err;
426
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700427 return 0;
428}
429
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700430static int
431isl1208_rtc_read_time(struct device *dev, struct rtc_time *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700432{
433 return isl1208_i2c_read_time(to_i2c_client(dev), tm);
434}
435
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700436static int
437isl1208_i2c_set_time(struct i2c_client *client, struct rtc_time const *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700438{
439 int sr;
440 u8 regs[ISL1208_RTC_SECTION_LEN] = { 0, };
441
Chris Elstoncc6c2ca2008-12-23 13:57:10 -0800442 /* The clock has an 8 bit wide bcd-coded register (they never learn)
443 * for the year. tm_year is an offset from 1900 and we are interested
444 * in the 2000-2099 range, so any value less than 100 is invalid.
445 */
446 if (tm->tm_year < 100)
447 return -EINVAL;
448
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700449 regs[ISL1208_REG_SC] = bin2bcd(tm->tm_sec);
450 regs[ISL1208_REG_MN] = bin2bcd(tm->tm_min);
451 regs[ISL1208_REG_HR] = bin2bcd(tm->tm_hour) | ISL1208_REG_HR_MIL;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700452
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700453 regs[ISL1208_REG_DT] = bin2bcd(tm->tm_mday);
454 regs[ISL1208_REG_MO] = bin2bcd(tm->tm_mon + 1);
455 regs[ISL1208_REG_YR] = bin2bcd(tm->tm_year - 100);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700456
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700457 regs[ISL1208_REG_DW] = bin2bcd(tm->tm_wday & 7);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700458
459 sr = isl1208_i2c_get_sr(client);
460 if (sr < 0) {
461 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
462 return sr;
463 }
464
465 /* set WRTC */
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700466 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700467 sr | ISL1208_REG_SR_WRTC);
468 if (sr < 0) {
469 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
470 return sr;
471 }
472
473 /* write RTC registers */
474 sr = isl1208_i2c_set_regs(client, 0, regs, ISL1208_RTC_SECTION_LEN);
475 if (sr < 0) {
476 dev_err(&client->dev, "%s: writing RTC section failed\n",
477 __func__);
478 return sr;
479 }
480
481 /* clear WRTC again */
Denis Osterland5b9fc7952018-01-23 13:17:58 +0100482 sr = isl1208_i2c_get_sr(client);
483 if (sr < 0) {
484 dev_err(&client->dev, "%s: reading SR failed\n", __func__);
485 return sr;
486 }
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700487 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR,
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700488 sr & ~ISL1208_REG_SR_WRTC);
489 if (sr < 0) {
490 dev_err(&client->dev, "%s: writing SR failed\n", __func__);
491 return sr;
492 }
493
494 return 0;
495}
496
497
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700498static int
499isl1208_rtc_set_time(struct device *dev, struct rtc_time *tm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700500{
501 return isl1208_i2c_set_time(to_i2c_client(dev), tm);
502}
503
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700504static int
505isl1208_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700506{
507 return isl1208_i2c_read_alarm(to_i2c_client(dev), alarm);
508}
509
Ryan Malloncf044f02011-03-22 16:34:53 -0700510static int
511isl1208_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
512{
513 return isl1208_i2c_set_alarm(to_i2c_client(dev), alarm);
514}
515
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000516static ssize_t timestamp0_store(struct device *dev,
517 struct device_attribute *attr,
518 const char *buf, size_t count)
519{
520 struct i2c_client *client = dev_get_drvdata(dev);
521 int sr;
522
523 sr = isl1208_i2c_get_sr(client);
524 if (sr < 0) {
525 dev_err(dev, "%s: reading SR failed\n", __func__);
526 return sr;
527 }
528
529 sr &= ~ISL1208_REG_SR_EVT;
530
531 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
532 if (sr < 0)
533 dev_err(dev, "%s: writing SR failed\n",
534 __func__);
535
536 return count;
537};
538
539static ssize_t timestamp0_show(struct device *dev,
540 struct device_attribute *attr, char *buf)
541{
542 struct i2c_client *client = dev_get_drvdata(dev);
543 u8 regs[ISL1219_EVT_SECTION_LEN] = { 0, };
544 struct rtc_time tm;
545 int sr;
546
547 sr = isl1208_i2c_get_sr(client);
548 if (sr < 0) {
549 dev_err(dev, "%s: reading SR failed\n", __func__);
550 return sr;
551 }
552
553 if (!(sr & ISL1208_REG_SR_EVT))
554 return 0;
555
556 sr = isl1208_i2c_read_regs(client, ISL1219_REG_SCT, regs,
557 ISL1219_EVT_SECTION_LEN);
558 if (sr < 0) {
559 dev_err(dev, "%s: reading event section failed\n",
560 __func__);
561 return 0;
562 }
563
564 /* MSB of each alarm register is an enable bit */
565 tm.tm_sec = bcd2bin(regs[ISL1219_REG_SCT - ISL1219_REG_SCT] & 0x7f);
566 tm.tm_min = bcd2bin(regs[ISL1219_REG_MNT - ISL1219_REG_SCT] & 0x7f);
567 tm.tm_hour = bcd2bin(regs[ISL1219_REG_HRT - ISL1219_REG_SCT] & 0x3f);
568 tm.tm_mday = bcd2bin(regs[ISL1219_REG_DTT - ISL1219_REG_SCT] & 0x3f);
569 tm.tm_mon =
570 bcd2bin(regs[ISL1219_REG_MOT - ISL1219_REG_SCT] & 0x1f) - 1;
571 tm.tm_year = bcd2bin(regs[ISL1219_REG_YRT - ISL1219_REG_SCT]) + 100;
572
573 sr = rtc_valid_tm(&tm);
574 if (sr)
575 return sr;
576
577 return sprintf(buf, "%llu\n",
578 (unsigned long long)rtc_tm_to_time64(&tm));
579};
580
581static DEVICE_ATTR_RW(timestamp0);
582
Ryan Malloncf044f02011-03-22 16:34:53 -0700583static irqreturn_t
584isl1208_rtc_interrupt(int irq, void *data)
585{
586 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
587 struct i2c_client *client = data;
Jan Luebbe72fca4a2013-02-04 14:28:53 -0800588 struct rtc_device *rtc = i2c_get_clientdata(client);
Ryan Malloncf044f02011-03-22 16:34:53 -0700589 int handled = 0, sr, err;
590
591 /*
592 * I2C reads get NAK'ed if we read straight away after an interrupt?
593 * Using a mdelay/msleep didn't seem to help either, so we work around
594 * this by continually trying to read the register for a short time.
595 */
596 while (1) {
597 sr = isl1208_i2c_get_sr(client);
598 if (sr >= 0)
599 break;
600
601 if (time_after(jiffies, timeout)) {
602 dev_err(&client->dev, "%s: reading SR failed\n",
603 __func__);
604 return sr;
605 }
606 }
607
608 if (sr & ISL1208_REG_SR_ALM) {
609 dev_dbg(&client->dev, "alarm!\n");
610
Jan Luebbe72fca4a2013-02-04 14:28:53 -0800611 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
612
Ryan Malloncf044f02011-03-22 16:34:53 -0700613 /* Clear the alarm */
614 sr &= ~ISL1208_REG_SR_ALM;
615 sr = i2c_smbus_write_byte_data(client, ISL1208_REG_SR, sr);
616 if (sr < 0)
617 dev_err(&client->dev, "%s: writing SR failed\n",
618 __func__);
619 else
620 handled = 1;
621
622 /* Disable the alarm */
623 err = isl1208_rtc_toggle_alarm(client, 0);
624 if (err)
625 return err;
626 }
627
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000628 if (sr & ISL1208_REG_SR_EVT) {
629 sysfs_notify(&rtc->dev.kobj, NULL,
630 dev_attr_timestamp0.attr.name);
631 dev_warn(&client->dev, "event detected");
632 handled = 1;
633 }
634
Ryan Malloncf044f02011-03-22 16:34:53 -0700635 return handled ? IRQ_HANDLED : IRQ_NONE;
636}
637
David Brownellff8371a2006-09-30 23:28:17 -0700638static const struct rtc_class_ops isl1208_rtc_ops = {
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700639 .proc = isl1208_rtc_proc,
640 .read_time = isl1208_rtc_read_time,
641 .set_time = isl1208_rtc_set_time,
642 .read_alarm = isl1208_rtc_read_alarm,
Ryan Malloncf044f02011-03-22 16:34:53 -0700643 .set_alarm = isl1208_rtc_set_alarm,
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700644};
645
646/* sysfs interface */
647
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700648static ssize_t
649isl1208_sysfs_show_atrim(struct device *dev,
650 struct device_attribute *attr, char *buf)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700651{
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700652 int atr = isl1208_i2c_get_atr(to_i2c_client(dev));
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700653 if (atr < 0)
654 return atr;
655
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700656 return sprintf(buf, "%d.%.2d pF\n", atr >> 2, (atr & 0x3) * 25);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700657}
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700658
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700659static DEVICE_ATTR(atrim, S_IRUGO, isl1208_sysfs_show_atrim, NULL);
660
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700661static ssize_t
662isl1208_sysfs_show_dtrim(struct device *dev,
663 struct device_attribute *attr, char *buf)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700664{
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700665 int dtr = isl1208_i2c_get_dtr(to_i2c_client(dev));
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700666 if (dtr < 0)
667 return dtr;
668
669 return sprintf(buf, "%d ppm\n", dtr);
670}
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700671
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700672static DEVICE_ATTR(dtrim, S_IRUGO, isl1208_sysfs_show_dtrim, NULL);
673
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700674static ssize_t
675isl1208_sysfs_show_usr(struct device *dev,
676 struct device_attribute *attr, char *buf)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700677{
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700678 int usr = isl1208_i2c_get_usr(to_i2c_client(dev));
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700679 if (usr < 0)
680 return usr;
681
682 return sprintf(buf, "0x%.4x\n", usr);
683}
684
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700685static ssize_t
686isl1208_sysfs_store_usr(struct device *dev,
687 struct device_attribute *attr,
688 const char *buf, size_t count)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700689{
690 int usr = -1;
691
692 if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
693 if (sscanf(buf, "%x", &usr) != 1)
694 return -EINVAL;
695 } else {
696 if (sscanf(buf, "%d", &usr) != 1)
697 return -EINVAL;
698 }
699
700 if (usr < 0 || usr > 0xffff)
701 return -EINVAL;
702
703 return isl1208_i2c_set_usr(to_i2c_client(dev), usr) ? -EIO : count;
704}
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700705
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700706static DEVICE_ATTR(usr, S_IRUGO | S_IWUSR, isl1208_sysfs_show_usr,
707 isl1208_sysfs_store_usr);
708
H Hartley Sweetene17ab5cb2010-05-24 14:33:46 -0700709static struct attribute *isl1208_rtc_attrs[] = {
710 &dev_attr_atrim.attr,
711 &dev_attr_dtrim.attr,
712 &dev_attr_usr.attr,
713 NULL
714};
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700715
H Hartley Sweetene17ab5cb2010-05-24 14:33:46 -0700716static const struct attribute_group isl1208_rtc_sysfs_files = {
717 .attrs = isl1208_rtc_attrs,
718};
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700719
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000720static struct attribute *isl1219_rtc_attrs[] = {
721 &dev_attr_timestamp0.attr,
722 NULL
723};
724
725static const struct attribute_group isl1219_rtc_sysfs_files = {
726 .attrs = isl1219_rtc_attrs,
727};
728
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000729static int isl1208_setup_irq(struct i2c_client *client, int irq)
730{
731 int rc = devm_request_threaded_irq(&client->dev, irq, NULL,
732 isl1208_rtc_interrupt,
733 IRQF_SHARED | IRQF_ONESHOT,
734 isl1208_driver.driver.name,
735 client);
736 if (!rc) {
737 device_init_wakeup(&client->dev, 1);
738 enable_irq_wake(irq);
739 } else {
740 dev_err(&client->dev,
741 "Unable to request irq %d, no alarm support\n",
742 irq);
743 }
744 return rc;
745}
746
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700747static int
Jean Delvared2653e92008-04-29 23:11:39 +0200748isl1208_probe(struct i2c_client *client, const struct i2c_device_id *id)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700749{
750 int rc = 0;
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700751 struct rtc_device *rtc;
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000752 int evdet_irq = -1;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700753
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700754 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
755 return -ENODEV;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700756
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700757 if (isl1208_i2c_validate_client(client) < 0)
758 return -ENODEV;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700759
Denis Osterland236b7182018-03-05 10:43:53 +0000760 rtc = devm_rtc_allocate_device(&client->dev);
Sachin Kamatadce8c12013-11-12 15:10:30 -0800761 if (IS_ERR(rtc))
762 return PTR_ERR(rtc);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700763
Denis Osterland236b7182018-03-05 10:43:53 +0000764 rtc->ops = &isl1208_rtc_ops;
765
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700766 i2c_set_clientdata(client, rtc);
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000767 dev_set_drvdata(&rtc->dev, client);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700768
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700769 rc = isl1208_i2c_get_sr(client);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700770 if (rc < 0) {
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700771 dev_err(&client->dev, "reading status failed\n");
Sachin Kamatadce8c12013-11-12 15:10:30 -0800772 return rc;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700773 }
774
775 if (rc & ISL1208_REG_SR_RTCF)
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700776 dev_warn(&client->dev, "rtc power failure detected, "
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700777 "please set clock.\n");
778
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000779 if (id->driver_data == TYPE_ISL1219) {
780 rc = i2c_smbus_write_byte_data(client, ISL1219_REG_EV,
781 ISL1219_REG_EV_EVEN);
782 if (rc < 0) {
783 dev_err(&client->dev, "could not enable tamper detection\n");
784 return rc;
785 }
786 rc = rtc_add_group(rtc, &isl1219_rtc_sysfs_files);
787 if (rc)
788 return rc;
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000789 evdet_irq = of_irq_get_byname(client->dev.of_node, "evdet");
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000790 }
791
H Hartley Sweetene17ab5cb2010-05-24 14:33:46 -0700792 rc = sysfs_create_group(&client->dev.kobj, &isl1208_rtc_sysfs_files);
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700793 if (rc)
Sachin Kamatadce8c12013-11-12 15:10:30 -0800794 return rc;
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700795
Denis Osterland9ece7cd2018-07-24 11:31:21 +0000796 if (client->irq > 0)
797 rc = isl1208_setup_irq(client, client->irq);
798 if (rc)
799 return rc;
800
801 if (evdet_irq > 0 && evdet_irq != client->irq)
802 rc = isl1208_setup_irq(client, evdet_irq);
803 if (rc)
804 return rc;
Michael Grzeschik9d327c22018-03-05 10:43:53 +0000805
Denis Osterland236b7182018-03-05 10:43:53 +0000806 return rtc_register_device(rtc);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700807}
808
809static int
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700810isl1208_remove(struct i2c_client *client)
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700811{
H Hartley Sweetene17ab5cb2010-05-24 14:33:46 -0700812 sysfs_remove_group(&client->dev.kobj, &isl1208_rtc_sysfs_files);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700813
814 return 0;
815}
816
Jean Delvare3760f732008-04-29 23:11:40 +0200817static const struct i2c_device_id isl1208_id[] = {
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000818 { "isl1208", TYPE_ISL1208 },
819 { "isl1218", TYPE_ISL1218 },
820 { "isl1219", TYPE_ISL1219 },
Jean Delvare3760f732008-04-29 23:11:40 +0200821 { }
822};
823MODULE_DEVICE_TABLE(i2c, isl1208_id);
824
Javier Martinez Canillas03835502017-03-03 11:29:20 -0300825static const struct of_device_id isl1208_of_match[] = {
826 { .compatible = "isil,isl1208" },
827 { .compatible = "isil,isl1218" },
Michael Grzeschikdd35bdb2018-07-24 11:31:21 +0000828 { .compatible = "isil,isl1219" },
Javier Martinez Canillas03835502017-03-03 11:29:20 -0300829 { }
830};
831MODULE_DEVICE_TABLE(of, isl1208_of_match);
832
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700833static struct i2c_driver isl1208_driver = {
834 .driver = {
Javier Martinez Canillas03835502017-03-03 11:29:20 -0300835 .name = "rtc-isl1208",
836 .of_match_table = of_match_ptr(isl1208_of_match),
837 },
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700838 .probe = isl1208_probe,
839 .remove = isl1208_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200840 .id_table = isl1208_id,
Alessandro Zummo9edae7b2008-04-28 02:11:53 -0700841};
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700842
Axel Lin0abc9202012-03-23 15:02:31 -0700843module_i2c_driver(isl1208_driver);
Herbert Valerio Riedel7e56a7d2006-07-14 00:24:11 -0700844
845MODULE_AUTHOR("Herbert Valerio Riedel <hvr@gnu.org>");
846MODULE_DESCRIPTION("Intersil ISL1208 RTC driver");
847MODULE_LICENSE("GPL");