hwmon: (adt7475) fan stall prevention
[linux/fpc-iii.git] / drivers / rtc / rtc-rx8010.c
blob1ed3403ff8ac23ae1c9c2923b76f3f8aff1c9c11
1 /*
2 * Driver for the Epson RTC module RX-8010 SJ
4 * Copyright(C) Timesys Corporation 2015
5 * Copyright(C) General Electric Company 2015
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #include <linux/bcd.h>
14 #include <linux/bitops.h>
15 #include <linux/i2c.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/rtc.h>
20 #define RX8010_SEC 0x10
21 #define RX8010_MIN 0x11
22 #define RX8010_HOUR 0x12
23 #define RX8010_WDAY 0x13
24 #define RX8010_MDAY 0x14
25 #define RX8010_MONTH 0x15
26 #define RX8010_YEAR 0x16
27 #define RX8010_YEAR 0x16
28 #define RX8010_RESV17 0x17
29 #define RX8010_ALMIN 0x18
30 #define RX8010_ALHOUR 0x19
31 #define RX8010_ALWDAY 0x1A
32 #define RX8010_TCOUNT0 0x1B
33 #define RX8010_TCOUNT1 0x1C
34 #define RX8010_EXT 0x1D
35 #define RX8010_FLAG 0x1E
36 #define RX8010_CTRL 0x1F
37 /* 0x20 to 0x2F are user registers */
38 #define RX8010_RESV30 0x30
39 #define RX8010_RESV31 0x32
40 #define RX8010_IRQ 0x32
42 #define RX8010_EXT_WADA BIT(3)
44 #define RX8010_FLAG_VLF BIT(1)
45 #define RX8010_FLAG_AF BIT(3)
46 #define RX8010_FLAG_TF BIT(4)
47 #define RX8010_FLAG_UF BIT(5)
49 #define RX8010_CTRL_AIE BIT(3)
50 #define RX8010_CTRL_UIE BIT(5)
51 #define RX8010_CTRL_STOP BIT(6)
52 #define RX8010_CTRL_TEST BIT(7)
54 #define RX8010_ALARM_AE BIT(7)
56 static const struct i2c_device_id rx8010_id[] = {
57 { "rx8010", 0 },
58 { }
60 MODULE_DEVICE_TABLE(i2c, rx8010_id);
62 static const struct of_device_id rx8010_of_match[] = {
63 { .compatible = "epson,rx8010" },
64 { }
66 MODULE_DEVICE_TABLE(of, rx8010_of_match);
68 struct rx8010_data {
69 struct i2c_client *client;
70 struct rtc_device *rtc;
71 u8 ctrlreg;
74 static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
76 struct i2c_client *client = dev_id;
77 struct rx8010_data *rx8010 = i2c_get_clientdata(client);
78 int flagreg;
80 mutex_lock(&rx8010->rtc->ops_lock);
82 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
84 if (flagreg <= 0) {
85 mutex_unlock(&rx8010->rtc->ops_lock);
86 return IRQ_NONE;
89 if (flagreg & RX8010_FLAG_VLF)
90 dev_warn(&client->dev, "Frequency stop detected\n");
92 if (flagreg & RX8010_FLAG_TF) {
93 flagreg &= ~RX8010_FLAG_TF;
94 rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF);
97 if (flagreg & RX8010_FLAG_AF) {
98 flagreg &= ~RX8010_FLAG_AF;
99 rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF);
102 if (flagreg & RX8010_FLAG_UF) {
103 flagreg &= ~RX8010_FLAG_UF;
104 rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF);
107 i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
109 mutex_unlock(&rx8010->rtc->ops_lock);
110 return IRQ_HANDLED;
113 static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
115 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
116 u8 date[7];
117 int flagreg;
118 int err;
120 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
121 if (flagreg < 0)
122 return flagreg;
124 if (flagreg & RX8010_FLAG_VLF) {
125 dev_warn(dev, "Frequency stop detected\n");
126 return -EINVAL;
129 err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_SEC,
130 7, date);
131 if (err != 7)
132 return err < 0 ? err : -EIO;
134 dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
135 dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
136 dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
137 dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
138 dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1;
139 dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100;
140 dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f);
142 return rtc_valid_tm(dt);
145 static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
147 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
148 u8 date[7];
149 int ctrl, flagreg;
150 int ret;
152 if ((dt->tm_year < 100) || (dt->tm_year > 199))
153 return -EINVAL;
155 /* set STOP bit before changing clock/calendar */
156 ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
157 if (ctrl < 0)
158 return ctrl;
159 rx8010->ctrlreg = ctrl | RX8010_CTRL_STOP;
160 ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
161 rx8010->ctrlreg);
162 if (ret < 0)
163 return ret;
165 date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec);
166 date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min);
167 date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour);
168 date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday);
169 date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1);
170 date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100);
171 date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday);
173 ret = i2c_smbus_write_i2c_block_data(rx8010->client,
174 RX8010_SEC, 7, date);
175 if (ret < 0)
176 return ret;
178 /* clear STOP bit after changing clock/calendar */
179 ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
180 if (ctrl < 0)
181 return ctrl;
182 rx8010->ctrlreg = ctrl & ~RX8010_CTRL_STOP;
183 ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
184 rx8010->ctrlreg);
185 if (ret < 0)
186 return ret;
188 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
189 if (flagreg < 0) {
190 return flagreg;
193 if (flagreg & RX8010_FLAG_VLF)
194 ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG,
195 flagreg & ~RX8010_FLAG_VLF);
197 return 0;
200 static int rx8010_init_client(struct i2c_client *client)
202 struct rx8010_data *rx8010 = i2c_get_clientdata(client);
203 u8 ctrl[2];
204 int need_clear = 0, err = 0;
206 /* Initialize reserved registers as specified in datasheet */
207 err = i2c_smbus_write_byte_data(client, RX8010_RESV17, 0xD8);
208 if (err < 0)
209 return err;
211 err = i2c_smbus_write_byte_data(client, RX8010_RESV30, 0x00);
212 if (err < 0)
213 return err;
215 err = i2c_smbus_write_byte_data(client, RX8010_RESV31, 0x08);
216 if (err < 0)
217 return err;
219 err = i2c_smbus_write_byte_data(client, RX8010_IRQ, 0x00);
220 if (err < 0)
221 return err;
223 err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_FLAG,
224 2, ctrl);
225 if (err != 2)
226 return err < 0 ? err : -EIO;
228 if (ctrl[0] & RX8010_FLAG_VLF)
229 dev_warn(&client->dev, "Frequency stop was detected\n");
231 if (ctrl[0] & RX8010_FLAG_AF) {
232 dev_warn(&client->dev, "Alarm was detected\n");
233 need_clear = 1;
236 if (ctrl[0] & RX8010_FLAG_TF)
237 need_clear = 1;
239 if (ctrl[0] & RX8010_FLAG_UF)
240 need_clear = 1;
242 if (need_clear) {
243 ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
244 err = i2c_smbus_write_byte_data(client, RX8010_FLAG, ctrl[0]);
245 if (err < 0)
246 return err;
249 rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST);
251 return err;
254 static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
256 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
257 struct i2c_client *client = rx8010->client;
258 u8 alarmvals[3];
259 int flagreg;
260 int err;
262 err = i2c_smbus_read_i2c_block_data(client, RX8010_ALMIN, 3, alarmvals);
263 if (err != 3)
264 return err < 0 ? err : -EIO;
266 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
267 if (flagreg < 0)
268 return flagreg;
270 t->time.tm_sec = 0;
271 t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
272 t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
274 if (!(alarmvals[2] & RX8010_ALARM_AE))
275 t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f);
277 t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE);
278 t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled;
280 return err;
283 static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
285 struct i2c_client *client = to_i2c_client(dev);
286 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
287 u8 alarmvals[3];
288 int extreg, flagreg;
289 int err;
291 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
292 if (flagreg < 0) {
293 return flagreg;
296 if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) {
297 rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
298 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
299 rx8010->ctrlreg);
300 if (err < 0) {
301 return err;
305 flagreg &= ~RX8010_FLAG_AF;
306 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
307 if (err < 0)
308 return err;
310 alarmvals[0] = bin2bcd(t->time.tm_min);
311 alarmvals[1] = bin2bcd(t->time.tm_hour);
312 alarmvals[2] = bin2bcd(t->time.tm_mday);
314 err = i2c_smbus_write_i2c_block_data(rx8010->client, RX8010_ALMIN,
315 2, alarmvals);
316 if (err < 0)
317 return err;
319 extreg = i2c_smbus_read_byte_data(client, RX8010_EXT);
320 if (extreg < 0)
321 return extreg;
323 extreg |= RX8010_EXT_WADA;
324 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_EXT, extreg);
325 if (err < 0)
326 return err;
328 if (alarmvals[2] == 0)
329 alarmvals[2] |= RX8010_ALARM_AE;
331 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_ALWDAY,
332 alarmvals[2]);
333 if (err < 0)
334 return err;
336 if (t->enabled) {
337 if (rx8010->rtc->uie_rtctimer.enabled)
338 rx8010->ctrlreg |= RX8010_CTRL_UIE;
339 if (rx8010->rtc->aie_timer.enabled)
340 rx8010->ctrlreg |=
341 (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
343 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
344 rx8010->ctrlreg);
345 if (err < 0)
346 return err;
349 return 0;
352 static int rx8010_alarm_irq_enable(struct device *dev,
353 unsigned int enabled)
355 struct i2c_client *client = to_i2c_client(dev);
356 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
357 int flagreg;
358 u8 ctrl;
359 int err;
361 ctrl = rx8010->ctrlreg;
363 if (enabled) {
364 if (rx8010->rtc->uie_rtctimer.enabled)
365 ctrl |= RX8010_CTRL_UIE;
366 if (rx8010->rtc->aie_timer.enabled)
367 ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
368 } else {
369 if (!rx8010->rtc->uie_rtctimer.enabled)
370 ctrl &= ~RX8010_CTRL_UIE;
371 if (!rx8010->rtc->aie_timer.enabled)
372 ctrl &= ~RX8010_CTRL_AIE;
375 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
376 if (flagreg < 0)
377 return flagreg;
379 flagreg &= ~RX8010_FLAG_AF;
380 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
381 if (err < 0)
382 return err;
384 if (ctrl != rx8010->ctrlreg) {
385 rx8010->ctrlreg = ctrl;
386 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
387 rx8010->ctrlreg);
388 if (err < 0)
389 return err;
392 return 0;
395 static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
397 struct i2c_client *client = to_i2c_client(dev);
398 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
399 int ret, tmp;
400 int flagreg;
402 switch (cmd) {
403 case RTC_VL_READ:
404 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
405 if (flagreg < 0)
406 return flagreg;
408 tmp = !!(flagreg & RX8010_FLAG_VLF);
409 if (copy_to_user((void __user *)arg, &tmp, sizeof(int)))
410 return -EFAULT;
412 return 0;
414 case RTC_VL_CLR:
415 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
416 if (flagreg < 0) {
417 return flagreg;
420 flagreg &= ~RX8010_FLAG_VLF;
421 ret = i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
422 if (ret < 0)
423 return ret;
425 return 0;
427 default:
428 return -ENOIOCTLCMD;
432 static struct rtc_class_ops rx8010_rtc_ops = {
433 .read_time = rx8010_get_time,
434 .set_time = rx8010_set_time,
435 .ioctl = rx8010_ioctl,
438 static int rx8010_probe(struct i2c_client *client,
439 const struct i2c_device_id *id)
441 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
442 struct rx8010_data *rx8010;
443 int err = 0;
445 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
446 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
447 dev_err(&adapter->dev, "doesn't support required functionality\n");
448 return -EIO;
451 rx8010 = devm_kzalloc(&client->dev, sizeof(struct rx8010_data),
452 GFP_KERNEL);
453 if (!rx8010)
454 return -ENOMEM;
456 rx8010->client = client;
457 i2c_set_clientdata(client, rx8010);
459 err = rx8010_init_client(client);
460 if (err)
461 return err;
463 if (client->irq > 0) {
464 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
465 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
466 rx8010_irq_1_handler,
467 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
468 "rx8010", client);
470 if (err) {
471 dev_err(&client->dev, "unable to request IRQ\n");
472 client->irq = 0;
473 } else {
474 rx8010_rtc_ops.read_alarm = rx8010_read_alarm;
475 rx8010_rtc_ops.set_alarm = rx8010_set_alarm;
476 rx8010_rtc_ops.alarm_irq_enable = rx8010_alarm_irq_enable;
480 rx8010->rtc = devm_rtc_device_register(&client->dev, client->name,
481 &rx8010_rtc_ops, THIS_MODULE);
483 if (IS_ERR(rx8010->rtc)) {
484 dev_err(&client->dev, "unable to register the class device\n");
485 return PTR_ERR(rx8010->rtc);
488 rx8010->rtc->max_user_freq = 1;
490 return err;
493 static struct i2c_driver rx8010_driver = {
494 .driver = {
495 .name = "rtc-rx8010",
496 .of_match_table = of_match_ptr(rx8010_of_match),
498 .probe = rx8010_probe,
499 .id_table = rx8010_id,
502 module_i2c_driver(rx8010_driver);
504 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
505 MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
506 MODULE_LICENSE("GPL v2");