treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / rtc / rtc-rx8010.c
blob8102469e27c05060f9e2dcfc2f3a03e5bc434474
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver for the Epson RTC module RX-8010 SJ
5 * Copyright(C) Timesys Corporation 2015
6 * Copyright(C) General Electric Company 2015
7 */
9 #include <linux/bcd.h>
10 #include <linux/bitops.h>
11 #include <linux/i2c.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/rtc.h>
16 #define RX8010_SEC 0x10
17 #define RX8010_MIN 0x11
18 #define RX8010_HOUR 0x12
19 #define RX8010_WDAY 0x13
20 #define RX8010_MDAY 0x14
21 #define RX8010_MONTH 0x15
22 #define RX8010_YEAR 0x16
23 #define RX8010_RESV17 0x17
24 #define RX8010_ALMIN 0x18
25 #define RX8010_ALHOUR 0x19
26 #define RX8010_ALWDAY 0x1A
27 #define RX8010_TCOUNT0 0x1B
28 #define RX8010_TCOUNT1 0x1C
29 #define RX8010_EXT 0x1D
30 #define RX8010_FLAG 0x1E
31 #define RX8010_CTRL 0x1F
32 /* 0x20 to 0x2F are user registers */
33 #define RX8010_RESV30 0x30
34 #define RX8010_RESV31 0x31
35 #define RX8010_IRQ 0x32
37 #define RX8010_EXT_WADA BIT(3)
39 #define RX8010_FLAG_VLF BIT(1)
40 #define RX8010_FLAG_AF BIT(3)
41 #define RX8010_FLAG_TF BIT(4)
42 #define RX8010_FLAG_UF BIT(5)
44 #define RX8010_CTRL_AIE BIT(3)
45 #define RX8010_CTRL_UIE BIT(5)
46 #define RX8010_CTRL_STOP BIT(6)
47 #define RX8010_CTRL_TEST BIT(7)
49 #define RX8010_ALARM_AE BIT(7)
51 static const struct i2c_device_id rx8010_id[] = {
52 { "rx8010", 0 },
53 { }
55 MODULE_DEVICE_TABLE(i2c, rx8010_id);
57 static const struct of_device_id rx8010_of_match[] = {
58 { .compatible = "epson,rx8010" },
59 { }
61 MODULE_DEVICE_TABLE(of, rx8010_of_match);
63 struct rx8010_data {
64 struct i2c_client *client;
65 struct rtc_device *rtc;
66 u8 ctrlreg;
69 static irqreturn_t rx8010_irq_1_handler(int irq, void *dev_id)
71 struct i2c_client *client = dev_id;
72 struct rx8010_data *rx8010 = i2c_get_clientdata(client);
73 int flagreg;
75 mutex_lock(&rx8010->rtc->ops_lock);
77 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
79 if (flagreg <= 0) {
80 mutex_unlock(&rx8010->rtc->ops_lock);
81 return IRQ_NONE;
84 if (flagreg & RX8010_FLAG_VLF)
85 dev_warn(&client->dev, "Frequency stop detected\n");
87 if (flagreg & RX8010_FLAG_TF) {
88 flagreg &= ~RX8010_FLAG_TF;
89 rtc_update_irq(rx8010->rtc, 1, RTC_PF | RTC_IRQF);
92 if (flagreg & RX8010_FLAG_AF) {
93 flagreg &= ~RX8010_FLAG_AF;
94 rtc_update_irq(rx8010->rtc, 1, RTC_AF | RTC_IRQF);
97 if (flagreg & RX8010_FLAG_UF) {
98 flagreg &= ~RX8010_FLAG_UF;
99 rtc_update_irq(rx8010->rtc, 1, RTC_UF | RTC_IRQF);
102 i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
104 mutex_unlock(&rx8010->rtc->ops_lock);
105 return IRQ_HANDLED;
108 static int rx8010_get_time(struct device *dev, struct rtc_time *dt)
110 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
111 u8 date[7];
112 int flagreg;
113 int err;
115 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
116 if (flagreg < 0)
117 return flagreg;
119 if (flagreg & RX8010_FLAG_VLF) {
120 dev_warn(dev, "Frequency stop detected\n");
121 return -EINVAL;
124 err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_SEC,
125 7, date);
126 if (err != 7)
127 return err < 0 ? err : -EIO;
129 dt->tm_sec = bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
130 dt->tm_min = bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
131 dt->tm_hour = bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
132 dt->tm_mday = bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
133 dt->tm_mon = bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1;
134 dt->tm_year = bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100;
135 dt->tm_wday = ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f);
137 return 0;
140 static int rx8010_set_time(struct device *dev, struct rtc_time *dt)
142 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
143 u8 date[7];
144 int ctrl, flagreg;
145 int ret;
147 if ((dt->tm_year < 100) || (dt->tm_year > 199))
148 return -EINVAL;
150 /* set STOP bit before changing clock/calendar */
151 ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
152 if (ctrl < 0)
153 return ctrl;
154 rx8010->ctrlreg = ctrl | RX8010_CTRL_STOP;
155 ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
156 rx8010->ctrlreg);
157 if (ret < 0)
158 return ret;
160 date[RX8010_SEC - RX8010_SEC] = bin2bcd(dt->tm_sec);
161 date[RX8010_MIN - RX8010_SEC] = bin2bcd(dt->tm_min);
162 date[RX8010_HOUR - RX8010_SEC] = bin2bcd(dt->tm_hour);
163 date[RX8010_MDAY - RX8010_SEC] = bin2bcd(dt->tm_mday);
164 date[RX8010_MONTH - RX8010_SEC] = bin2bcd(dt->tm_mon + 1);
165 date[RX8010_YEAR - RX8010_SEC] = bin2bcd(dt->tm_year - 100);
166 date[RX8010_WDAY - RX8010_SEC] = bin2bcd(1 << dt->tm_wday);
168 ret = i2c_smbus_write_i2c_block_data(rx8010->client,
169 RX8010_SEC, 7, date);
170 if (ret < 0)
171 return ret;
173 /* clear STOP bit after changing clock/calendar */
174 ctrl = i2c_smbus_read_byte_data(rx8010->client, RX8010_CTRL);
175 if (ctrl < 0)
176 return ctrl;
177 rx8010->ctrlreg = ctrl & ~RX8010_CTRL_STOP;
178 ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
179 rx8010->ctrlreg);
180 if (ret < 0)
181 return ret;
183 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
184 if (flagreg < 0) {
185 return flagreg;
188 if (flagreg & RX8010_FLAG_VLF)
189 ret = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG,
190 flagreg & ~RX8010_FLAG_VLF);
192 return 0;
195 static int rx8010_init_client(struct i2c_client *client)
197 struct rx8010_data *rx8010 = i2c_get_clientdata(client);
198 u8 ctrl[2];
199 int need_clear = 0, err = 0;
201 /* Initialize reserved registers as specified in datasheet */
202 err = i2c_smbus_write_byte_data(client, RX8010_RESV17, 0xD8);
203 if (err < 0)
204 return err;
206 err = i2c_smbus_write_byte_data(client, RX8010_RESV30, 0x00);
207 if (err < 0)
208 return err;
210 err = i2c_smbus_write_byte_data(client, RX8010_RESV31, 0x08);
211 if (err < 0)
212 return err;
214 err = i2c_smbus_write_byte_data(client, RX8010_IRQ, 0x00);
215 if (err < 0)
216 return err;
218 err = i2c_smbus_read_i2c_block_data(rx8010->client, RX8010_FLAG,
219 2, ctrl);
220 if (err != 2)
221 return err < 0 ? err : -EIO;
223 if (ctrl[0] & RX8010_FLAG_VLF)
224 dev_warn(&client->dev, "Frequency stop was detected\n");
226 if (ctrl[0] & RX8010_FLAG_AF) {
227 dev_warn(&client->dev, "Alarm was detected\n");
228 need_clear = 1;
231 if (ctrl[0] & RX8010_FLAG_TF)
232 need_clear = 1;
234 if (ctrl[0] & RX8010_FLAG_UF)
235 need_clear = 1;
237 if (need_clear) {
238 ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);
239 err = i2c_smbus_write_byte_data(client, RX8010_FLAG, ctrl[0]);
240 if (err < 0)
241 return err;
244 rx8010->ctrlreg = (ctrl[1] & ~RX8010_CTRL_TEST);
246 return 0;
249 static int rx8010_read_alarm(struct device *dev, struct rtc_wkalrm *t)
251 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
252 struct i2c_client *client = rx8010->client;
253 u8 alarmvals[3];
254 int flagreg;
255 int err;
257 err = i2c_smbus_read_i2c_block_data(client, RX8010_ALMIN, 3, alarmvals);
258 if (err != 3)
259 return err < 0 ? err : -EIO;
261 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
262 if (flagreg < 0)
263 return flagreg;
265 t->time.tm_sec = 0;
266 t->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
267 t->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
269 if (!(alarmvals[2] & RX8010_ALARM_AE))
270 t->time.tm_mday = bcd2bin(alarmvals[2] & 0x7f);
272 t->enabled = !!(rx8010->ctrlreg & RX8010_CTRL_AIE);
273 t->pending = (flagreg & RX8010_FLAG_AF) && t->enabled;
275 return 0;
278 static int rx8010_set_alarm(struct device *dev, struct rtc_wkalrm *t)
280 struct i2c_client *client = to_i2c_client(dev);
281 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
282 u8 alarmvals[3];
283 int extreg, flagreg;
284 int err;
286 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
287 if (flagreg < 0) {
288 return flagreg;
291 if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE)) {
292 rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);
293 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
294 rx8010->ctrlreg);
295 if (err < 0) {
296 return err;
300 flagreg &= ~RX8010_FLAG_AF;
301 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
302 if (err < 0)
303 return err;
305 alarmvals[0] = bin2bcd(t->time.tm_min);
306 alarmvals[1] = bin2bcd(t->time.tm_hour);
307 alarmvals[2] = bin2bcd(t->time.tm_mday);
309 err = i2c_smbus_write_i2c_block_data(rx8010->client, RX8010_ALMIN,
310 2, alarmvals);
311 if (err < 0)
312 return err;
314 extreg = i2c_smbus_read_byte_data(client, RX8010_EXT);
315 if (extreg < 0)
316 return extreg;
318 extreg |= RX8010_EXT_WADA;
319 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_EXT, extreg);
320 if (err < 0)
321 return err;
323 if (alarmvals[2] == 0)
324 alarmvals[2] |= RX8010_ALARM_AE;
326 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_ALWDAY,
327 alarmvals[2]);
328 if (err < 0)
329 return err;
331 if (t->enabled) {
332 if (rx8010->rtc->uie_rtctimer.enabled)
333 rx8010->ctrlreg |= RX8010_CTRL_UIE;
334 if (rx8010->rtc->aie_timer.enabled)
335 rx8010->ctrlreg |=
336 (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
338 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
339 rx8010->ctrlreg);
340 if (err < 0)
341 return err;
344 return 0;
347 static int rx8010_alarm_irq_enable(struct device *dev,
348 unsigned int enabled)
350 struct i2c_client *client = to_i2c_client(dev);
351 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
352 int flagreg;
353 u8 ctrl;
354 int err;
356 ctrl = rx8010->ctrlreg;
358 if (enabled) {
359 if (rx8010->rtc->uie_rtctimer.enabled)
360 ctrl |= RX8010_CTRL_UIE;
361 if (rx8010->rtc->aie_timer.enabled)
362 ctrl |= (RX8010_CTRL_AIE | RX8010_CTRL_UIE);
363 } else {
364 if (!rx8010->rtc->uie_rtctimer.enabled)
365 ctrl &= ~RX8010_CTRL_UIE;
366 if (!rx8010->rtc->aie_timer.enabled)
367 ctrl &= ~RX8010_CTRL_AIE;
370 flagreg = i2c_smbus_read_byte_data(client, RX8010_FLAG);
371 if (flagreg < 0)
372 return flagreg;
374 flagreg &= ~RX8010_FLAG_AF;
375 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_FLAG, flagreg);
376 if (err < 0)
377 return err;
379 if (ctrl != rx8010->ctrlreg) {
380 rx8010->ctrlreg = ctrl;
381 err = i2c_smbus_write_byte_data(rx8010->client, RX8010_CTRL,
382 rx8010->ctrlreg);
383 if (err < 0)
384 return err;
387 return 0;
390 static int rx8010_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
392 struct i2c_client *client = to_i2c_client(dev);
393 struct rx8010_data *rx8010 = dev_get_drvdata(dev);
394 int ret, tmp;
395 int flagreg;
397 switch (cmd) {
398 case RTC_VL_READ:
399 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
400 if (flagreg < 0)
401 return flagreg;
403 tmp = !!(flagreg & RX8010_FLAG_VLF);
404 if (copy_to_user((void __user *)arg, &tmp, sizeof(int)))
405 return -EFAULT;
407 return 0;
409 case RTC_VL_CLR:
410 flagreg = i2c_smbus_read_byte_data(rx8010->client, RX8010_FLAG);
411 if (flagreg < 0) {
412 return flagreg;
415 flagreg &= ~RX8010_FLAG_VLF;
416 ret = i2c_smbus_write_byte_data(client, RX8010_FLAG, flagreg);
417 if (ret < 0)
418 return ret;
420 return 0;
422 default:
423 return -ENOIOCTLCMD;
427 static struct rtc_class_ops rx8010_rtc_ops = {
428 .read_time = rx8010_get_time,
429 .set_time = rx8010_set_time,
430 .ioctl = rx8010_ioctl,
433 static int rx8010_probe(struct i2c_client *client,
434 const struct i2c_device_id *id)
436 struct i2c_adapter *adapter = client->adapter;
437 struct rx8010_data *rx8010;
438 int err = 0;
440 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA
441 | I2C_FUNC_SMBUS_I2C_BLOCK)) {
442 dev_err(&adapter->dev, "doesn't support required functionality\n");
443 return -EIO;
446 rx8010 = devm_kzalloc(&client->dev, sizeof(struct rx8010_data),
447 GFP_KERNEL);
448 if (!rx8010)
449 return -ENOMEM;
451 rx8010->client = client;
452 i2c_set_clientdata(client, rx8010);
454 err = rx8010_init_client(client);
455 if (err)
456 return err;
458 if (client->irq > 0) {
459 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
460 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
461 rx8010_irq_1_handler,
462 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
463 "rx8010", client);
465 if (err) {
466 dev_err(&client->dev, "unable to request IRQ\n");
467 client->irq = 0;
468 } else {
469 rx8010_rtc_ops.read_alarm = rx8010_read_alarm;
470 rx8010_rtc_ops.set_alarm = rx8010_set_alarm;
471 rx8010_rtc_ops.alarm_irq_enable = rx8010_alarm_irq_enable;
475 rx8010->rtc = devm_rtc_device_register(&client->dev, client->name,
476 &rx8010_rtc_ops, THIS_MODULE);
478 if (IS_ERR(rx8010->rtc)) {
479 dev_err(&client->dev, "unable to register the class device\n");
480 return PTR_ERR(rx8010->rtc);
483 rx8010->rtc->max_user_freq = 1;
485 return err;
488 static struct i2c_driver rx8010_driver = {
489 .driver = {
490 .name = "rtc-rx8010",
491 .of_match_table = of_match_ptr(rx8010_of_match),
493 .probe = rx8010_probe,
494 .id_table = rx8010_id,
497 module_i2c_driver(rx8010_driver);
499 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
500 MODULE_DESCRIPTION("Epson RX8010SJ RTC driver");
501 MODULE_LICENSE("GPL v2");