libcxgb,iw_cxgb4,cxgbit: add cxgb_is_neg_adv()
[linux/fpc-iii.git] / drivers / rtc / rtc-isl12057.c
blob0e7f0f52bfe4e77a89600255997fb0d2aa7ff196
1 /*
2 * rtc-isl12057 - Driver for Intersil ISL12057 I2C Real Time Clock
4 * Copyright (C) 2013, Arnaud EBALARD <arno@natisbad.org>
6 * This work is largely based on Intersil ISL1208 driver developed by
7 * Hebert Valerio Riedel <hvr@gnu.org>.
9 * Detailed datasheet on which this development is based is available here:
11 * http://natisbad.org/NAS2/refs/ISL12057.pdf
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/rtc.h>
27 #include <linux/i2c.h>
28 #include <linux/bcd.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/regmap.h>
33 #define DRV_NAME "rtc-isl12057"
35 /* RTC section */
36 #define ISL12057_REG_RTC_SC 0x00 /* Seconds */
37 #define ISL12057_REG_RTC_MN 0x01 /* Minutes */
38 #define ISL12057_REG_RTC_HR 0x02 /* Hours */
39 #define ISL12057_REG_RTC_HR_PM BIT(5) /* AM/PM bit in 12h format */
40 #define ISL12057_REG_RTC_HR_MIL BIT(6) /* 24h/12h format */
41 #define ISL12057_REG_RTC_DW 0x03 /* Day of the Week */
42 #define ISL12057_REG_RTC_DT 0x04 /* Date */
43 #define ISL12057_REG_RTC_MO 0x05 /* Month */
44 #define ISL12057_REG_RTC_MO_CEN BIT(7) /* Century bit */
45 #define ISL12057_REG_RTC_YR 0x06 /* Year */
46 #define ISL12057_RTC_SEC_LEN 7
48 /* Alarm 1 section */
49 #define ISL12057_REG_A1_SC 0x07 /* Alarm 1 Seconds */
50 #define ISL12057_REG_A1_MN 0x08 /* Alarm 1 Minutes */
51 #define ISL12057_REG_A1_HR 0x09 /* Alarm 1 Hours */
52 #define ISL12057_REG_A1_HR_PM BIT(5) /* AM/PM bit in 12h format */
53 #define ISL12057_REG_A1_HR_MIL BIT(6) /* 24h/12h format */
54 #define ISL12057_REG_A1_DWDT 0x0A /* Alarm 1 Date / Day of the week */
55 #define ISL12057_REG_A1_DWDT_B BIT(6) /* DW / DT selection bit */
56 #define ISL12057_A1_SEC_LEN 4
58 /* Alarm 2 section */
59 #define ISL12057_REG_A2_MN 0x0B /* Alarm 2 Minutes */
60 #define ISL12057_REG_A2_HR 0x0C /* Alarm 2 Hours */
61 #define ISL12057_REG_A2_DWDT 0x0D /* Alarm 2 Date / Day of the week */
62 #define ISL12057_A2_SEC_LEN 3
64 /* Control/Status registers */
65 #define ISL12057_REG_INT 0x0E
66 #define ISL12057_REG_INT_A1IE BIT(0) /* Alarm 1 interrupt enable bit */
67 #define ISL12057_REG_INT_A2IE BIT(1) /* Alarm 2 interrupt enable bit */
68 #define ISL12057_REG_INT_INTCN BIT(2) /* Interrupt control enable bit */
69 #define ISL12057_REG_INT_RS1 BIT(3) /* Freq out control bit 1 */
70 #define ISL12057_REG_INT_RS2 BIT(4) /* Freq out control bit 2 */
71 #define ISL12057_REG_INT_EOSC BIT(7) /* Oscillator enable bit */
73 #define ISL12057_REG_SR 0x0F
74 #define ISL12057_REG_SR_A1F BIT(0) /* Alarm 1 interrupt bit */
75 #define ISL12057_REG_SR_A2F BIT(1) /* Alarm 2 interrupt bit */
76 #define ISL12057_REG_SR_OSF BIT(7) /* Oscillator failure bit */
78 /* Register memory map length */
79 #define ISL12057_MEM_MAP_LEN 0x10
81 struct isl12057_rtc_data {
82 struct rtc_device *rtc;
83 struct regmap *regmap;
84 struct mutex lock;
85 int irq;
88 static void isl12057_rtc_regs_to_tm(struct rtc_time *tm, u8 *regs)
90 tm->tm_sec = bcd2bin(regs[ISL12057_REG_RTC_SC]);
91 tm->tm_min = bcd2bin(regs[ISL12057_REG_RTC_MN]);
93 if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_MIL) { /* AM/PM */
94 tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x1f);
95 if (regs[ISL12057_REG_RTC_HR] & ISL12057_REG_RTC_HR_PM)
96 tm->tm_hour += 12;
97 } else { /* 24 hour mode */
98 tm->tm_hour = bcd2bin(regs[ISL12057_REG_RTC_HR] & 0x3f);
101 tm->tm_mday = bcd2bin(regs[ISL12057_REG_RTC_DT]);
102 tm->tm_wday = bcd2bin(regs[ISL12057_REG_RTC_DW]) - 1; /* starts at 1 */
103 tm->tm_mon = bcd2bin(regs[ISL12057_REG_RTC_MO] & 0x1f) - 1; /* ditto */
104 tm->tm_year = bcd2bin(regs[ISL12057_REG_RTC_YR]) + 100;
106 /* Check if years register has overflown from 99 to 00 */
107 if (regs[ISL12057_REG_RTC_MO] & ISL12057_REG_RTC_MO_CEN)
108 tm->tm_year += 100;
111 static int isl12057_rtc_tm_to_regs(u8 *regs, struct rtc_time *tm)
113 u8 century_bit;
116 * The clock has an 8 bit wide bcd-coded register for the year.
117 * It also has a century bit encoded in MO flag which provides
118 * information about overflow of year register from 99 to 00.
119 * tm_year is an offset from 1900 and we are interested in the
120 * 2000-2199 range, so any value less than 100 or larger than
121 * 299 is invalid.
123 if (tm->tm_year < 100 || tm->tm_year > 299)
124 return -EINVAL;
126 century_bit = (tm->tm_year > 199) ? ISL12057_REG_RTC_MO_CEN : 0;
128 regs[ISL12057_REG_RTC_SC] = bin2bcd(tm->tm_sec);
129 regs[ISL12057_REG_RTC_MN] = bin2bcd(tm->tm_min);
130 regs[ISL12057_REG_RTC_HR] = bin2bcd(tm->tm_hour); /* 24-hour format */
131 regs[ISL12057_REG_RTC_DT] = bin2bcd(tm->tm_mday);
132 regs[ISL12057_REG_RTC_MO] = bin2bcd(tm->tm_mon + 1) | century_bit;
133 regs[ISL12057_REG_RTC_YR] = bin2bcd(tm->tm_year % 100);
134 regs[ISL12057_REG_RTC_DW] = bin2bcd(tm->tm_wday + 1);
136 return 0;
140 * Try and match register bits w/ fixed null values to see whether we
141 * are dealing with an ISL12057. Note: this function is called early
142 * during init and hence does need mutex protection.
144 static int isl12057_i2c_validate_chip(struct regmap *regmap)
146 u8 regs[ISL12057_MEM_MAP_LEN];
147 static const u8 mask[ISL12057_MEM_MAP_LEN] = { 0x80, 0x80, 0x80, 0xf8,
148 0xc0, 0x60, 0x00, 0x00,
149 0x00, 0x00, 0x00, 0x00,
150 0x00, 0x00, 0x60, 0x7c };
151 int ret, i;
153 ret = regmap_bulk_read(regmap, 0, regs, ISL12057_MEM_MAP_LEN);
154 if (ret)
155 return ret;
157 for (i = 0; i < ISL12057_MEM_MAP_LEN; ++i) {
158 if (regs[i] & mask[i]) /* check if bits are cleared */
159 return -ENODEV;
162 return 0;
165 static int _isl12057_rtc_clear_alarm(struct device *dev)
167 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
168 int ret;
170 ret = regmap_update_bits(data->regmap, ISL12057_REG_SR,
171 ISL12057_REG_SR_A1F, 0);
172 if (ret)
173 dev_err(dev, "%s: clearing alarm failed (%d)\n", __func__, ret);
175 return ret;
178 static int _isl12057_rtc_update_alarm(struct device *dev, int enable)
180 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
181 int ret;
183 ret = regmap_update_bits(data->regmap, ISL12057_REG_INT,
184 ISL12057_REG_INT_A1IE,
185 enable ? ISL12057_REG_INT_A1IE : 0);
186 if (ret)
187 dev_err(dev, "%s: changing alarm interrupt flag failed (%d)\n",
188 __func__, ret);
190 return ret;
194 * Note: as we only read from device and do not perform any update, there is
195 * no need for an equivalent function which would try and get driver's main
196 * lock. Here, it is safe for everyone if we just use regmap internal lock
197 * on the device when reading.
199 static int _isl12057_rtc_read_time(struct device *dev, struct rtc_time *tm)
201 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
202 u8 regs[ISL12057_RTC_SEC_LEN];
203 unsigned int sr;
204 int ret;
206 ret = regmap_read(data->regmap, ISL12057_REG_SR, &sr);
207 if (ret) {
208 dev_err(dev, "%s: unable to read oscillator status flag (%d)\n",
209 __func__, ret);
210 goto out;
211 } else {
212 if (sr & ISL12057_REG_SR_OSF) {
213 ret = -ENODATA;
214 goto out;
218 ret = regmap_bulk_read(data->regmap, ISL12057_REG_RTC_SC, regs,
219 ISL12057_RTC_SEC_LEN);
220 if (ret)
221 dev_err(dev, "%s: unable to read RTC time section (%d)\n",
222 __func__, ret);
224 out:
225 if (ret)
226 return ret;
228 isl12057_rtc_regs_to_tm(tm, regs);
230 return rtc_valid_tm(tm);
233 static int isl12057_rtc_update_alarm(struct device *dev, int enable)
235 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
236 int ret;
238 mutex_lock(&data->lock);
239 ret = _isl12057_rtc_update_alarm(dev, enable);
240 mutex_unlock(&data->lock);
242 return ret;
245 static int isl12057_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
247 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
248 struct rtc_time *alarm_tm = &alarm->time;
249 u8 regs[ISL12057_A1_SEC_LEN];
250 unsigned int ir;
251 int ret;
253 mutex_lock(&data->lock);
254 ret = regmap_bulk_read(data->regmap, ISL12057_REG_A1_SC, regs,
255 ISL12057_A1_SEC_LEN);
256 if (ret) {
257 dev_err(dev, "%s: reading alarm section failed (%d)\n",
258 __func__, ret);
259 goto err_unlock;
262 alarm_tm->tm_sec = bcd2bin(regs[0] & 0x7f);
263 alarm_tm->tm_min = bcd2bin(regs[1] & 0x7f);
264 alarm_tm->tm_hour = bcd2bin(regs[2] & 0x3f);
265 alarm_tm->tm_mday = bcd2bin(regs[3] & 0x3f);
267 ret = regmap_read(data->regmap, ISL12057_REG_INT, &ir);
268 if (ret) {
269 dev_err(dev, "%s: reading alarm interrupt flag failed (%d)\n",
270 __func__, ret);
271 goto err_unlock;
274 alarm->enabled = !!(ir & ISL12057_REG_INT_A1IE);
276 err_unlock:
277 mutex_unlock(&data->lock);
279 return ret;
282 static int isl12057_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
284 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
285 struct rtc_time *alarm_tm = &alarm->time;
286 unsigned long rtc_secs, alarm_secs;
287 u8 regs[ISL12057_A1_SEC_LEN];
288 struct rtc_time rtc_tm;
289 int ret, enable = 1;
291 mutex_lock(&data->lock);
292 ret = _isl12057_rtc_read_time(dev, &rtc_tm);
293 if (ret)
294 goto err_unlock;
296 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
297 if (ret)
298 goto err_unlock;
300 ret = rtc_tm_to_time(alarm_tm, &alarm_secs);
301 if (ret)
302 goto err_unlock;
304 /* If alarm time is before current time, disable the alarm */
305 if (!alarm->enabled || alarm_secs <= rtc_secs) {
306 enable = 0;
307 } else {
309 * Chip only support alarms up to one month in the future. Let's
310 * return an error if we get something after that limit.
311 * Comparison is done by incrementing rtc_tm month field by one
312 * and checking alarm value is still below.
314 if (rtc_tm.tm_mon == 11) { /* handle year wrapping */
315 rtc_tm.tm_mon = 0;
316 rtc_tm.tm_year += 1;
317 } else {
318 rtc_tm.tm_mon += 1;
321 ret = rtc_tm_to_time(&rtc_tm, &rtc_secs);
322 if (ret)
323 goto err_unlock;
325 if (alarm_secs > rtc_secs) {
326 dev_err(dev, "%s: max for alarm is one month (%d)\n",
327 __func__, ret);
328 ret = -EINVAL;
329 goto err_unlock;
333 /* Disable the alarm before modifying it */
334 ret = _isl12057_rtc_update_alarm(dev, 0);
335 if (ret < 0) {
336 dev_err(dev, "%s: unable to disable the alarm (%d)\n",
337 __func__, ret);
338 goto err_unlock;
341 /* Program alarm registers */
342 regs[0] = bin2bcd(alarm_tm->tm_sec) & 0x7f;
343 regs[1] = bin2bcd(alarm_tm->tm_min) & 0x7f;
344 regs[2] = bin2bcd(alarm_tm->tm_hour) & 0x3f;
345 regs[3] = bin2bcd(alarm_tm->tm_mday) & 0x3f;
347 ret = regmap_bulk_write(data->regmap, ISL12057_REG_A1_SC, regs,
348 ISL12057_A1_SEC_LEN);
349 if (ret < 0) {
350 dev_err(dev, "%s: writing alarm section failed (%d)\n",
351 __func__, ret);
352 goto err_unlock;
355 /* Enable or disable alarm */
356 ret = _isl12057_rtc_update_alarm(dev, enable);
358 err_unlock:
359 mutex_unlock(&data->lock);
361 return ret;
364 static int isl12057_rtc_set_time(struct device *dev, struct rtc_time *tm)
366 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
367 u8 regs[ISL12057_RTC_SEC_LEN];
368 int ret;
370 ret = isl12057_rtc_tm_to_regs(regs, tm);
371 if (ret)
372 return ret;
374 mutex_lock(&data->lock);
375 ret = regmap_bulk_write(data->regmap, ISL12057_REG_RTC_SC, regs,
376 ISL12057_RTC_SEC_LEN);
377 if (ret) {
378 dev_err(dev, "%s: unable to write RTC time section (%d)\n",
379 __func__, ret);
380 goto out;
384 * Now that RTC time has been updated, let's clear oscillator
385 * failure flag, if needed.
387 ret = regmap_update_bits(data->regmap, ISL12057_REG_SR,
388 ISL12057_REG_SR_OSF, 0);
389 if (ret < 0)
390 dev_err(dev, "%s: unable to clear osc. failure bit (%d)\n",
391 __func__, ret);
393 out:
394 mutex_unlock(&data->lock);
396 return ret;
400 * Check current RTC status and enable/disable what needs to be. Return 0 if
401 * everything went ok and a negative value upon error. Note: this function
402 * is called early during init and hence does need mutex protection.
404 static int isl12057_check_rtc_status(struct device *dev, struct regmap *regmap)
406 int ret;
408 /* Enable oscillator if not already running */
409 ret = regmap_update_bits(regmap, ISL12057_REG_INT,
410 ISL12057_REG_INT_EOSC, 0);
411 if (ret < 0) {
412 dev_err(dev, "%s: unable to enable oscillator (%d)\n",
413 __func__, ret);
414 return ret;
417 /* Clear alarm bit if needed */
418 ret = regmap_update_bits(regmap, ISL12057_REG_SR,
419 ISL12057_REG_SR_A1F, 0);
420 if (ret < 0) {
421 dev_err(dev, "%s: unable to clear alarm bit (%d)\n",
422 __func__, ret);
423 return ret;
426 return 0;
429 #ifdef CONFIG_OF
431 * One would expect the device to be marked as a wakeup source only
432 * when an IRQ pin of the RTC is routed to an interrupt line of the
433 * CPU. In practice, such an IRQ pin can be connected to a PMIC and
434 * this allows the device to be powered up when RTC alarm rings. This
435 * is for instance the case on ReadyNAS 102, 104 and 2120. On those
436 * devices with no IRQ driectly connected to the SoC, the RTC chip
437 * can be forced as a wakeup source by stating that explicitly in
438 * the device's .dts file using the "wakeup-source" boolean property.
439 * This will guarantee 'wakealarm' sysfs entry is available on the device.
441 * The function below returns 1, i.e. the capability of the chip to
442 * wakeup the device, based on IRQ availability or if the boolean
443 * property has been set in the .dts file. Otherwise, it returns 0.
446 static bool isl12057_can_wakeup_machine(struct device *dev)
448 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
450 return data->irq || of_property_read_bool(dev->of_node, "wakeup-source")
451 || of_property_read_bool(dev->of_node, /* legacy */
452 "isil,irq2-can-wakeup-machine");
454 #else
455 static bool isl12057_can_wakeup_machine(struct device *dev)
457 struct isl12057_rtc_data *data = dev_get_drvdata(dev);
459 return !!data->irq;
461 #endif
463 static int isl12057_rtc_alarm_irq_enable(struct device *dev,
464 unsigned int enable)
466 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
467 int ret = -ENOTTY;
469 if (rtc_data->irq)
470 ret = isl12057_rtc_update_alarm(dev, enable);
472 return ret;
475 static irqreturn_t isl12057_rtc_interrupt(int irq, void *data)
477 struct i2c_client *client = data;
478 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(&client->dev);
479 struct rtc_device *rtc = rtc_data->rtc;
480 int ret, handled = IRQ_NONE;
481 unsigned int sr;
483 ret = regmap_read(rtc_data->regmap, ISL12057_REG_SR, &sr);
484 if (!ret && (sr & ISL12057_REG_SR_A1F)) {
485 dev_dbg(&client->dev, "RTC alarm!\n");
487 rtc_update_irq(rtc, 1, RTC_IRQF | RTC_AF);
489 /* Acknowledge and disable the alarm */
490 _isl12057_rtc_clear_alarm(&client->dev);
491 _isl12057_rtc_update_alarm(&client->dev, 0);
493 handled = IRQ_HANDLED;
496 return handled;
499 static const struct rtc_class_ops rtc_ops = {
500 .read_time = _isl12057_rtc_read_time,
501 .set_time = isl12057_rtc_set_time,
502 .read_alarm = isl12057_rtc_read_alarm,
503 .set_alarm = isl12057_rtc_set_alarm,
504 .alarm_irq_enable = isl12057_rtc_alarm_irq_enable,
507 static const struct regmap_config isl12057_rtc_regmap_config = {
508 .reg_bits = 8,
509 .val_bits = 8,
512 static int isl12057_probe(struct i2c_client *client,
513 const struct i2c_device_id *id)
515 struct device *dev = &client->dev;
516 struct isl12057_rtc_data *data;
517 struct regmap *regmap;
518 int ret;
520 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
521 I2C_FUNC_SMBUS_BYTE_DATA |
522 I2C_FUNC_SMBUS_I2C_BLOCK))
523 return -ENODEV;
525 regmap = devm_regmap_init_i2c(client, &isl12057_rtc_regmap_config);
526 if (IS_ERR(regmap)) {
527 ret = PTR_ERR(regmap);
528 dev_err(dev, "%s: regmap allocation failed (%d)\n",
529 __func__, ret);
530 return ret;
533 ret = isl12057_i2c_validate_chip(regmap);
534 if (ret)
535 return ret;
537 ret = isl12057_check_rtc_status(dev, regmap);
538 if (ret)
539 return ret;
541 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
542 if (!data)
543 return -ENOMEM;
545 mutex_init(&data->lock);
546 data->regmap = regmap;
547 dev_set_drvdata(dev, data);
549 if (client->irq > 0) {
550 ret = devm_request_threaded_irq(dev, client->irq, NULL,
551 isl12057_rtc_interrupt,
552 IRQF_SHARED|IRQF_ONESHOT,
553 DRV_NAME, client);
554 if (!ret)
555 data->irq = client->irq;
556 else
557 dev_err(dev, "%s: irq %d unavailable (%d)\n", __func__,
558 client->irq, ret);
561 if (isl12057_can_wakeup_machine(dev))
562 device_init_wakeup(dev, true);
564 data->rtc = devm_rtc_device_register(dev, DRV_NAME, &rtc_ops,
565 THIS_MODULE);
566 ret = PTR_ERR_OR_ZERO(data->rtc);
567 if (ret) {
568 dev_err(dev, "%s: unable to register RTC device (%d)\n",
569 __func__, ret);
570 goto err;
573 /* We cannot support UIE mode if we do not have an IRQ line */
574 if (!data->irq)
575 data->rtc->uie_unsupported = 1;
577 err:
578 return ret;
581 static int isl12057_remove(struct i2c_client *client)
583 if (isl12057_can_wakeup_machine(&client->dev))
584 device_init_wakeup(&client->dev, false);
586 return 0;
589 #ifdef CONFIG_PM_SLEEP
590 static int isl12057_rtc_suspend(struct device *dev)
592 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
594 if (rtc_data->irq && device_may_wakeup(dev))
595 return enable_irq_wake(rtc_data->irq);
597 return 0;
600 static int isl12057_rtc_resume(struct device *dev)
602 struct isl12057_rtc_data *rtc_data = dev_get_drvdata(dev);
604 if (rtc_data->irq && device_may_wakeup(dev))
605 return disable_irq_wake(rtc_data->irq);
607 return 0;
609 #endif
611 static SIMPLE_DEV_PM_OPS(isl12057_rtc_pm_ops, isl12057_rtc_suspend,
612 isl12057_rtc_resume);
614 #ifdef CONFIG_OF
615 static const struct of_device_id isl12057_dt_match[] = {
616 { .compatible = "isl,isl12057" }, /* for backward compat., don't use */
617 { .compatible = "isil,isl12057" },
618 { },
620 MODULE_DEVICE_TABLE(of, isl12057_dt_match);
621 #endif
623 static const struct i2c_device_id isl12057_id[] = {
624 { "isl12057", 0 },
627 MODULE_DEVICE_TABLE(i2c, isl12057_id);
629 static struct i2c_driver isl12057_driver = {
630 .driver = {
631 .name = DRV_NAME,
632 .pm = &isl12057_rtc_pm_ops,
633 .of_match_table = of_match_ptr(isl12057_dt_match),
635 .probe = isl12057_probe,
636 .remove = isl12057_remove,
637 .id_table = isl12057_id,
639 module_i2c_driver(isl12057_driver);
641 MODULE_AUTHOR("Arnaud EBALARD <arno@natisbad.org>");
642 MODULE_DESCRIPTION("Intersil ISL12057 RTC driver");
643 MODULE_LICENSE("GPL");