Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / rtc / rtc-s35390a.c
CommitLineData
c46288b0
BB
1/*
2 * Seiko Instruments S-35390A RTC Driver
3 *
4 * Copyright (c) 2007 Byron Bradley
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/rtc.h>
14#include <linux/i2c.h>
15#include <linux/bitrev.h>
16#include <linux/bcd.h>
17#include <linux/slab.h>
df4be7e6 18#include <linux/delay.h>
c46288b0
BB
19
20#define S35390A_CMD_STATUS1 0
21#define S35390A_CMD_STATUS2 1
22#define S35390A_CMD_TIME1 2
542dd33a
ML
23#define S35390A_CMD_TIME2 3
24#define S35390A_CMD_INT2_REG1 5
c46288b0
BB
25
26#define S35390A_BYTE_YEAR 0
27#define S35390A_BYTE_MONTH 1
28#define S35390A_BYTE_DAY 2
29#define S35390A_BYTE_WDAY 3
30#define S35390A_BYTE_HOURS 4
31#define S35390A_BYTE_MINS 5
32#define S35390A_BYTE_SECS 6
33
542dd33a
ML
34#define S35390A_ALRM_BYTE_WDAY 0
35#define S35390A_ALRM_BYTE_HOURS 1
36#define S35390A_ALRM_BYTE_MINS 2
37
535c7d61 38/* flags for STATUS1 */
c46288b0
BB
39#define S35390A_FLAG_POC 0x01
40#define S35390A_FLAG_BLD 0x02
535c7d61 41#define S35390A_FLAG_INT2 0x04
c46288b0
BB
42#define S35390A_FLAG_24H 0x40
43#define S35390A_FLAG_RESET 0x80
535c7d61
UKK
44
45/* flag for STATUS2 */
c46288b0
BB
46#define S35390A_FLAG_TEST 0x01
47
542dd33a
ML
48#define S35390A_INT2_MODE_MASK 0xF0
49
50#define S35390A_INT2_MODE_NOINTR 0x00
51#define S35390A_INT2_MODE_FREQ 0x10
52#define S35390A_INT2_MODE_ALARM 0x40
53#define S35390A_INT2_MODE_PMIN_EDG 0x20
54
3760f736
JD
55static const struct i2c_device_id s35390a_id[] = {
56 { "s35390a", 0 },
57 { }
58};
59MODULE_DEVICE_TABLE(i2c, s35390a_id);
60
c46288b0
BB
61struct s35390a {
62 struct i2c_client *client[8];
63 struct rtc_device *rtc;
64 int twentyfourhour;
65};
66
67static int s35390a_set_reg(struct s35390a *s35390a, int reg, char *buf, int len)
68{
69 struct i2c_client *client = s35390a->client[reg];
70 struct i2c_msg msg[] = {
65659f63
S
71 {
72 .addr = client->addr,
73 .len = len,
74 .buf = buf
75 },
c46288b0
BB
76 };
77
78 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
79 return -EIO;
80
81 return 0;
82}
83
84static int s35390a_get_reg(struct s35390a *s35390a, int reg, char *buf, int len)
85{
86 struct i2c_client *client = s35390a->client[reg];
87 struct i2c_msg msg[] = {
65659f63
S
88 {
89 .addr = client->addr,
90 .flags = I2C_M_RD,
91 .len = len,
92 .buf = buf
93 },
c46288b0
BB
94 };
95
96 if ((i2c_transfer(client->adapter, msg, 1)) != 1)
97 return -EIO;
98
99 return 0;
100}
101
df4be7e6
UKK
102/*
103 * Returns <0 on error, 0 if rtc is setup fine and 1 if the chip was reset.
104 * To keep the information if an irq is pending, pass the value read from
105 * STATUS1 to the caller.
106 */
107static int s35390a_reset(struct s35390a *s35390a, char *status1)
c46288b0 108{
df4be7e6
UKK
109 char buf;
110 int ret;
111 unsigned initcount = 0;
112
113 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, status1, 1);
114 if (ret < 0)
115 return ret;
116
117 if (*status1 & S35390A_FLAG_POC)
118 /*
119 * Do not communicate for 0.5 seconds since the power-on
120 * detection circuit is in operation.
121 */
122 msleep(500);
123 else if (!(*status1 & S35390A_FLAG_BLD))
124 /*
125 * If both POC and BLD are unset everything is fine.
126 */
c46288b0
BB
127 return 0;
128
df4be7e6
UKK
129 /*
130 * At least one of POC and BLD are set, so reinitialise chip. Keeping
131 * this information in the hardware to know later that the time isn't
132 * valid is unfortunately not possible because POC and BLD are cleared
133 * on read. So the reset is best done now.
134 *
135 * The 24H bit is kept over reset, so set it already here.
136 */
137initialize:
138 *status1 = S35390A_FLAG_24H;
139 buf = S35390A_FLAG_RESET | S35390A_FLAG_24H;
140 ret = s35390a_set_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
141
142 if (ret < 0)
143 return ret;
144
145 ret = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &buf, 1);
146 if (ret < 0)
147 return ret;
148
149 if (buf & (S35390A_FLAG_POC | S35390A_FLAG_BLD)) {
150 /* Try up to five times to reset the chip */
151 if (initcount < 5) {
152 ++initcount;
153 goto initialize;
154 } else
155 return -EIO;
156 }
157
158 return 1;
c46288b0
BB
159}
160
161static int s35390a_disable_test_mode(struct s35390a *s35390a)
162{
163 char buf[1];
164
165 if (s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf)) < 0)
166 return -EIO;
167
168 if (!(buf[0] & S35390A_FLAG_TEST))
169 return 0;
170
171 buf[0] &= ~S35390A_FLAG_TEST;
172 return s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, buf, sizeof(buf));
173}
174
175static char s35390a_hr2reg(struct s35390a *s35390a, int hour)
176{
177 if (s35390a->twentyfourhour)
fe20ba70 178 return bin2bcd(hour);
c46288b0
BB
179
180 if (hour < 12)
fe20ba70 181 return bin2bcd(hour);
c46288b0 182
fe20ba70 183 return 0x40 | bin2bcd(hour - 12);
c46288b0
BB
184}
185
186static int s35390a_reg2hr(struct s35390a *s35390a, char reg)
187{
188 unsigned hour;
189
190 if (s35390a->twentyfourhour)
fe20ba70 191 return bcd2bin(reg & 0x3f);
c46288b0 192
fe20ba70 193 hour = bcd2bin(reg & 0x3f);
c46288b0
BB
194 if (reg & 0x40)
195 hour += 12;
196
197 return hour;
198}
199
200static int s35390a_set_datetime(struct i2c_client *client, struct rtc_time *tm)
201{
202 struct s35390a *s35390a = i2c_get_clientdata(client);
203 int i, err;
204 char buf[7];
205
206 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d mday=%d, "
207 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
208 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
209 tm->tm_wday);
210
fe20ba70
AB
211 buf[S35390A_BYTE_YEAR] = bin2bcd(tm->tm_year - 100);
212 buf[S35390A_BYTE_MONTH] = bin2bcd(tm->tm_mon + 1);
213 buf[S35390A_BYTE_DAY] = bin2bcd(tm->tm_mday);
214 buf[S35390A_BYTE_WDAY] = bin2bcd(tm->tm_wday);
c46288b0 215 buf[S35390A_BYTE_HOURS] = s35390a_hr2reg(s35390a, tm->tm_hour);
fe20ba70
AB
216 buf[S35390A_BYTE_MINS] = bin2bcd(tm->tm_min);
217 buf[S35390A_BYTE_SECS] = bin2bcd(tm->tm_sec);
c46288b0
BB
218
219 /* This chip expects the bits of each byte to be in reverse order */
220 for (i = 0; i < 7; ++i)
221 buf[i] = bitrev8(buf[i]);
222
223 err = s35390a_set_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
224
225 return err;
226}
227
228static int s35390a_get_datetime(struct i2c_client *client, struct rtc_time *tm)
229{
230 struct s35390a *s35390a = i2c_get_clientdata(client);
231 char buf[7];
232 int i, err;
233
234 err = s35390a_get_reg(s35390a, S35390A_CMD_TIME1, buf, sizeof(buf));
235 if (err < 0)
236 return err;
237
238 /* This chip returns the bits of each byte in reverse order */
239 for (i = 0; i < 7; ++i)
240 buf[i] = bitrev8(buf[i]);
241
fe20ba70
AB
242 tm->tm_sec = bcd2bin(buf[S35390A_BYTE_SECS]);
243 tm->tm_min = bcd2bin(buf[S35390A_BYTE_MINS]);
c46288b0 244 tm->tm_hour = s35390a_reg2hr(s35390a, buf[S35390A_BYTE_HOURS]);
fe20ba70
AB
245 tm->tm_wday = bcd2bin(buf[S35390A_BYTE_WDAY]);
246 tm->tm_mday = bcd2bin(buf[S35390A_BYTE_DAY]);
247 tm->tm_mon = bcd2bin(buf[S35390A_BYTE_MONTH]) - 1;
248 tm->tm_year = bcd2bin(buf[S35390A_BYTE_YEAR]) + 100;
c46288b0
BB
249
250 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, mday=%d, "
251 "mon=%d, year=%d, wday=%d\n", __func__, tm->tm_sec,
252 tm->tm_min, tm->tm_hour, tm->tm_mday, tm->tm_mon, tm->tm_year,
253 tm->tm_wday);
254
255 return rtc_valid_tm(tm);
256}
257
542dd33a
ML
258static int s35390a_set_alarm(struct i2c_client *client, struct rtc_wkalrm *alm)
259{
260 struct s35390a *s35390a = i2c_get_clientdata(client);
261 char buf[3], sts = 0;
262 int err, i;
263
264 dev_dbg(&client->dev, "%s: alm is secs=%d, mins=%d, hours=%d mday=%d, "\
265 "mon=%d, year=%d, wday=%d\n", __func__, alm->time.tm_sec,
266 alm->time.tm_min, alm->time.tm_hour, alm->time.tm_mday,
267 alm->time.tm_mon, alm->time.tm_year, alm->time.tm_wday);
268
269 /* disable interrupt */
270 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
271 if (err < 0)
272 return err;
273
274 /* clear pending interrupt, if any */
275 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS1, &sts, sizeof(sts));
276 if (err < 0)
277 return err;
278
279 if (alm->enabled)
280 sts = S35390A_INT2_MODE_ALARM;
281 else
282 sts = S35390A_INT2_MODE_NOINTR;
283
284 /* This chip expects the bits of each byte to be in reverse order */
285 sts = bitrev8(sts);
286
287 /* set interupt mode*/
288 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
289 if (err < 0)
290 return err;
291
292 if (alm->time.tm_wday != -1)
293 buf[S35390A_ALRM_BYTE_WDAY] = bin2bcd(alm->time.tm_wday) | 0x80;
294
295 buf[S35390A_ALRM_BYTE_HOURS] = s35390a_hr2reg(s35390a,
296 alm->time.tm_hour) | 0x80;
297 buf[S35390A_ALRM_BYTE_MINS] = bin2bcd(alm->time.tm_min) | 0x80;
298
299 if (alm->time.tm_hour >= 12)
300 buf[S35390A_ALRM_BYTE_HOURS] |= 0x40;
301
302 for (i = 0; i < 3; ++i)
303 buf[i] = bitrev8(buf[i]);
304
305 err = s35390a_set_reg(s35390a, S35390A_CMD_INT2_REG1, buf,
306 sizeof(buf));
307
308 return err;
309}
310
311static int s35390a_read_alarm(struct i2c_client *client, struct rtc_wkalrm *alm)
312{
313 struct s35390a *s35390a = i2c_get_clientdata(client);
314 char buf[3], sts;
315 int i, err;
316
231b0f80
UKK
317 /*
318 * initialize all members to -1 to signal the core that they are not
319 * defined by the hardware.
320 */
321 alm->time.tm_sec = -1;
322 alm->time.tm_min = -1;
323 alm->time.tm_hour = -1;
324 alm->time.tm_mday = -1;
325 alm->time.tm_mon = -1;
326 alm->time.tm_year = -1;
327 alm->time.tm_wday = -1;
328 alm->time.tm_yday = -1;
329 alm->time.tm_isdst = -1;
330
542dd33a
ML
331 err = s35390a_get_reg(s35390a, S35390A_CMD_STATUS2, &sts, sizeof(sts));
332 if (err < 0)
333 return err;
334
335 if (bitrev8(sts) != S35390A_INT2_MODE_ALARM)
336 return -EINVAL;
337
338 err = s35390a_get_reg(s35390a, S35390A_CMD_INT2_REG1, buf, sizeof(buf));
339 if (err < 0)
340 return err;
341
342 /* This chip returns the bits of each byte in reverse order */
343 for (i = 0; i < 3; ++i) {
344 buf[i] = bitrev8(buf[i]);
345 buf[i] &= ~0x80;
346 }
347
348 alm->time.tm_wday = bcd2bin(buf[S35390A_ALRM_BYTE_WDAY]);
349 alm->time.tm_hour = s35390a_reg2hr(s35390a,
350 buf[S35390A_ALRM_BYTE_HOURS]);
351 alm->time.tm_min = bcd2bin(buf[S35390A_ALRM_BYTE_MINS]);
352
353 dev_dbg(&client->dev, "%s: alm is mins=%d, hours=%d, wday=%d\n",
354 __func__, alm->time.tm_min, alm->time.tm_hour,
355 alm->time.tm_wday);
356
357 return 0;
358}
359
360static int s35390a_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
361{
362 return s35390a_read_alarm(to_i2c_client(dev), alm);
363}
364
365static int s35390a_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
366{
367 return s35390a_set_alarm(to_i2c_client(dev), alm);
368}
369
c46288b0
BB
370static int s35390a_rtc_read_time(struct device *dev, struct rtc_time *tm)
371{
372 return s35390a_get_datetime(to_i2c_client(dev), tm);
373}
374
375static int s35390a_rtc_set_time(struct device *dev, struct rtc_time *tm)
376{
377 return s35390a_set_datetime(to_i2c_client(dev), tm);
378}
379
380static const struct rtc_class_ops s35390a_rtc_ops = {
381 .read_time = s35390a_rtc_read_time,
382 .set_time = s35390a_rtc_set_time,
542dd33a
ML
383 .set_alarm = s35390a_rtc_set_alarm,
384 .read_alarm = s35390a_rtc_read_alarm,
385
c46288b0
BB
386};
387
388static struct i2c_driver s35390a_driver;
389
d2653e92
JD
390static int s35390a_probe(struct i2c_client *client,
391 const struct i2c_device_id *id)
c46288b0 392{
535c7d61 393 int err, err_reset;
c46288b0
BB
394 unsigned int i;
395 struct s35390a *s35390a;
396 struct rtc_time tm;
535c7d61 397 char buf, status1;
c46288b0
BB
398
399 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
400 err = -ENODEV;
401 goto exit;
402 }
403
b4cd3d6a
JH
404 s35390a = devm_kzalloc(&client->dev, sizeof(struct s35390a),
405 GFP_KERNEL);
c46288b0
BB
406 if (!s35390a) {
407 err = -ENOMEM;
408 goto exit;
409 }
410
411 s35390a->client[0] = client;
412 i2c_set_clientdata(client, s35390a);
413
414 /* This chip uses multiple addresses, use dummy devices for them */
415 for (i = 1; i < 8; ++i) {
416 s35390a->client[i] = i2c_new_dummy(client->adapter,
60b129d7 417 client->addr + i);
c46288b0
BB
418 if (!s35390a->client[i]) {
419 dev_err(&client->dev, "Address %02x unavailable\n",
420 client->addr + i);
421 err = -EBUSY;
422 goto exit_dummy;
423 }
424 }
425
535c7d61
UKK
426 err_reset = s35390a_reset(s35390a, &status1);
427 if (err_reset < 0) {
428 err = err_reset;
c46288b0
BB
429 dev_err(&client->dev, "error resetting chip\n");
430 goto exit_dummy;
431 }
432
535c7d61 433 if (status1 & S35390A_FLAG_24H)
c46288b0
BB
434 s35390a->twentyfourhour = 1;
435 else
436 s35390a->twentyfourhour = 0;
437
535c7d61
UKK
438 if (status1 & S35390A_FLAG_INT2) {
439 /* disable alarm (and maybe test mode) */
440 buf = 0;
441 err = s35390a_set_reg(s35390a, S35390A_CMD_STATUS2, &buf, 1);
442 if (err < 0) {
443 dev_err(&client->dev, "error disabling alarm");
444 goto exit_dummy;
445 }
446 } else {
447 err = s35390a_disable_test_mode(s35390a);
448 if (err < 0) {
449 dev_err(&client->dev, "error disabling test mode\n");
450 goto exit_dummy;
451 }
452 }
453
454 if (err_reset > 0 || s35390a_get_datetime(client, &tm) < 0)
c46288b0
BB
455 dev_warn(&client->dev, "clock needs to be set\n");
456
542dd33a
ML
457 device_set_wakeup_capable(&client->dev, 1);
458
b4cd3d6a
JH
459 s35390a->rtc = devm_rtc_device_register(&client->dev,
460 s35390a_driver.driver.name,
461 &s35390a_rtc_ops, THIS_MODULE);
c46288b0
BB
462
463 if (IS_ERR(s35390a->rtc)) {
464 err = PTR_ERR(s35390a->rtc);
465 goto exit_dummy;
466 }
535c7d61
UKK
467
468 if (status1 & S35390A_FLAG_INT2)
469 rtc_update_irq(s35390a->rtc, 1, RTC_AF);
470
c46288b0
BB
471 return 0;
472
473exit_dummy:
474 for (i = 1; i < 8; ++i)
475 if (s35390a->client[i])
476 i2c_unregister_device(s35390a->client[i]);
c46288b0
BB
477
478exit:
479 return err;
480}
481
482static int s35390a_remove(struct i2c_client *client)
483{
484 unsigned int i;
c46288b0 485 struct s35390a *s35390a = i2c_get_clientdata(client);
b4cd3d6a 486
c46288b0
BB
487 for (i = 1; i < 8; ++i)
488 if (s35390a->client[i])
489 i2c_unregister_device(s35390a->client[i]);
490
c46288b0
BB
491 return 0;
492}
493
494static struct i2c_driver s35390a_driver = {
495 .driver = {
496 .name = "rtc-s35390a",
497 },
498 .probe = s35390a_probe,
499 .remove = s35390a_remove,
3760f736 500 .id_table = s35390a_id,
c46288b0
BB
501};
502
0abc9201 503module_i2c_driver(s35390a_driver);
c46288b0
BB
504
505MODULE_AUTHOR("Byron Bradley <byron.bbradley@gmail.com>");
506MODULE_DESCRIPTION("S35390A RTC driver");
507MODULE_LICENSE("GPL");