Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * RTC client/driver for the Maxim/Dallas DS1374 Real-Time Clock over I2C
0004  *
0005  * Based on code by Randy Vinson <rvinson@mvista.com>,
0006  * which was based on the m41t00.c by Mark Greer <mgreer@mvista.com>.
0007  *
0008  * Copyright (C) 2014 Rose Technology
0009  * Copyright (C) 2006-2007 Freescale Semiconductor
0010  * Copyright (c) 2005 MontaVista Software, Inc.
0011  */
0012 /*
0013  * It would be more efficient to use i2c msgs/i2c_transfer directly but, as
0014  * recommended in .../Documentation/i2c/writing-clients.rst section
0015  * "Sending and receiving", using SMBus level communication is preferred.
0016  */
0017 
0018 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0019 
0020 #include <linux/kernel.h>
0021 #include <linux/module.h>
0022 #include <linux/interrupt.h>
0023 #include <linux/i2c.h>
0024 #include <linux/rtc.h>
0025 #include <linux/bcd.h>
0026 #include <linux/workqueue.h>
0027 #include <linux/slab.h>
0028 #include <linux/pm.h>
0029 #ifdef CONFIG_RTC_DRV_DS1374_WDT
0030 #include <linux/fs.h>
0031 #include <linux/ioctl.h>
0032 #include <linux/miscdevice.h>
0033 #include <linux/reboot.h>
0034 #include <linux/watchdog.h>
0035 #endif
0036 
0037 #define DS1374_REG_TOD0     0x00 /* Time of Day */
0038 #define DS1374_REG_TOD1     0x01
0039 #define DS1374_REG_TOD2     0x02
0040 #define DS1374_REG_TOD3     0x03
0041 #define DS1374_REG_WDALM0   0x04 /* Watchdog/Alarm */
0042 #define DS1374_REG_WDALM1   0x05
0043 #define DS1374_REG_WDALM2   0x06
0044 #define DS1374_REG_CR       0x07 /* Control */
0045 #define DS1374_REG_CR_AIE   0x01 /* Alarm Int. Enable */
0046 #define DS1374_REG_CR_WDSTR 0x08 /* 1=INT, 0=RST */
0047 #define DS1374_REG_CR_WDALM 0x20 /* 1=Watchdog, 0=Alarm */
0048 #define DS1374_REG_CR_WACE  0x40 /* WD/Alarm counter enable */
0049 #define DS1374_REG_SR       0x08 /* Status */
0050 #define DS1374_REG_SR_OSF   0x80 /* Oscillator Stop Flag */
0051 #define DS1374_REG_SR_AF    0x01 /* Alarm Flag */
0052 #define DS1374_REG_TCR      0x09 /* Trickle Charge */
0053 
0054 static const struct i2c_device_id ds1374_id[] = {
0055     { "ds1374", 0 },
0056     { }
0057 };
0058 MODULE_DEVICE_TABLE(i2c, ds1374_id);
0059 
0060 #ifdef CONFIG_OF
0061 static const struct of_device_id ds1374_of_match[] = {
0062     { .compatible = "dallas,ds1374" },
0063     { }
0064 };
0065 MODULE_DEVICE_TABLE(of, ds1374_of_match);
0066 #endif
0067 
0068 struct ds1374 {
0069     struct i2c_client *client;
0070     struct rtc_device *rtc;
0071     struct work_struct work;
0072 #ifdef CONFIG_RTC_DRV_DS1374_WDT
0073     struct watchdog_device wdt;
0074 #endif
0075     /* The mutex protects alarm operations, and prevents a race
0076      * between the enable_irq() in the workqueue and the free_irq()
0077      * in the remove function.
0078      */
0079     struct mutex mutex;
0080     int exiting;
0081 };
0082 
0083 static struct i2c_driver ds1374_driver;
0084 
0085 static int ds1374_read_rtc(struct i2c_client *client, u32 *time,
0086                int reg, int nbytes)
0087 {
0088     u8 buf[4];
0089     int ret;
0090     int i;
0091 
0092     if (WARN_ON(nbytes > 4))
0093         return -EINVAL;
0094 
0095     ret = i2c_smbus_read_i2c_block_data(client, reg, nbytes, buf);
0096 
0097     if (ret < 0)
0098         return ret;
0099     if (ret < nbytes)
0100         return -EIO;
0101 
0102     for (i = nbytes - 1, *time = 0; i >= 0; i--)
0103         *time = (*time << 8) | buf[i];
0104 
0105     return 0;
0106 }
0107 
0108 static int ds1374_write_rtc(struct i2c_client *client, u32 time,
0109                 int reg, int nbytes)
0110 {
0111     u8 buf[4];
0112     int i;
0113 
0114     if (nbytes > 4) {
0115         WARN_ON(1);
0116         return -EINVAL;
0117     }
0118 
0119     for (i = 0; i < nbytes; i++) {
0120         buf[i] = time & 0xff;
0121         time >>= 8;
0122     }
0123 
0124     return i2c_smbus_write_i2c_block_data(client, reg, nbytes, buf);
0125 }
0126 
0127 static int ds1374_check_rtc_status(struct i2c_client *client)
0128 {
0129     int ret = 0;
0130     int control, stat;
0131 
0132     stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
0133     if (stat < 0)
0134         return stat;
0135 
0136     if (stat & DS1374_REG_SR_OSF)
0137         dev_warn(&client->dev,
0138              "oscillator discontinuity flagged, time unreliable\n");
0139 
0140     stat &= ~(DS1374_REG_SR_OSF | DS1374_REG_SR_AF);
0141 
0142     ret = i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
0143     if (ret < 0)
0144         return ret;
0145 
0146     /* If the alarm is pending, clear it before requesting
0147      * the interrupt, so an interrupt event isn't reported
0148      * before everything is initialized.
0149      */
0150 
0151     control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
0152     if (control < 0)
0153         return control;
0154 
0155     control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
0156     return i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
0157 }
0158 
0159 static int ds1374_read_time(struct device *dev, struct rtc_time *time)
0160 {
0161     struct i2c_client *client = to_i2c_client(dev);
0162     u32 itime;
0163     int ret;
0164 
0165     ret = ds1374_read_rtc(client, &itime, DS1374_REG_TOD0, 4);
0166     if (!ret)
0167         rtc_time64_to_tm(itime, time);
0168 
0169     return ret;
0170 }
0171 
0172 static int ds1374_set_time(struct device *dev, struct rtc_time *time)
0173 {
0174     struct i2c_client *client = to_i2c_client(dev);
0175     unsigned long itime = rtc_tm_to_time64(time);
0176 
0177     return ds1374_write_rtc(client, itime, DS1374_REG_TOD0, 4);
0178 }
0179 
0180 #ifndef CONFIG_RTC_DRV_DS1374_WDT
0181 /* The ds1374 has a decrementer for an alarm, rather than a comparator.
0182  * If the time of day is changed, then the alarm will need to be
0183  * reset.
0184  */
0185 static int ds1374_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
0186 {
0187     struct i2c_client *client = to_i2c_client(dev);
0188     struct ds1374 *ds1374 = i2c_get_clientdata(client);
0189     u32 now, cur_alarm;
0190     int cr, sr;
0191     int ret = 0;
0192 
0193     if (client->irq <= 0)
0194         return -EINVAL;
0195 
0196     mutex_lock(&ds1374->mutex);
0197 
0198     cr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
0199     if (ret < 0)
0200         goto out;
0201 
0202     sr = ret = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
0203     if (ret < 0)
0204         goto out;
0205 
0206     ret = ds1374_read_rtc(client, &now, DS1374_REG_TOD0, 4);
0207     if (ret)
0208         goto out;
0209 
0210     ret = ds1374_read_rtc(client, &cur_alarm, DS1374_REG_WDALM0, 3);
0211     if (ret)
0212         goto out;
0213 
0214     rtc_time64_to_tm(now + cur_alarm, &alarm->time);
0215     alarm->enabled = !!(cr & DS1374_REG_CR_WACE);
0216     alarm->pending = !!(sr & DS1374_REG_SR_AF);
0217 
0218 out:
0219     mutex_unlock(&ds1374->mutex);
0220     return ret;
0221 }
0222 
0223 static int ds1374_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
0224 {
0225     struct i2c_client *client = to_i2c_client(dev);
0226     struct ds1374 *ds1374 = i2c_get_clientdata(client);
0227     struct rtc_time now;
0228     unsigned long new_alarm, itime;
0229     int cr;
0230     int ret = 0;
0231 
0232     if (client->irq <= 0)
0233         return -EINVAL;
0234 
0235     ret = ds1374_read_time(dev, &now);
0236     if (ret < 0)
0237         return ret;
0238 
0239     new_alarm = rtc_tm_to_time64(&alarm->time);
0240     itime = rtc_tm_to_time64(&now);
0241 
0242     /* This can happen due to races, in addition to dates that are
0243      * truly in the past.  To avoid requiring the caller to check for
0244      * races, dates in the past are assumed to be in the recent past
0245      * (i.e. not something that we'd rather the caller know about via
0246      * an error), and the alarm is set to go off as soon as possible.
0247      */
0248     if (time_before_eq(new_alarm, itime))
0249         new_alarm = 1;
0250     else
0251         new_alarm -= itime;
0252 
0253     mutex_lock(&ds1374->mutex);
0254 
0255     ret = cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
0256     if (ret < 0)
0257         goto out;
0258 
0259     /* Disable any existing alarm before setting the new one
0260      * (or lack thereof). */
0261     cr &= ~DS1374_REG_CR_WACE;
0262 
0263     ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
0264     if (ret < 0)
0265         goto out;
0266 
0267     ret = ds1374_write_rtc(client, new_alarm, DS1374_REG_WDALM0, 3);
0268     if (ret)
0269         goto out;
0270 
0271     if (alarm->enabled) {
0272         cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
0273         cr &= ~DS1374_REG_CR_WDALM;
0274 
0275         ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
0276     }
0277 
0278 out:
0279     mutex_unlock(&ds1374->mutex);
0280     return ret;
0281 }
0282 #endif
0283 
0284 static irqreturn_t ds1374_irq(int irq, void *dev_id)
0285 {
0286     struct i2c_client *client = dev_id;
0287     struct ds1374 *ds1374 = i2c_get_clientdata(client);
0288 
0289     disable_irq_nosync(irq);
0290     schedule_work(&ds1374->work);
0291     return IRQ_HANDLED;
0292 }
0293 
0294 static void ds1374_work(struct work_struct *work)
0295 {
0296     struct ds1374 *ds1374 = container_of(work, struct ds1374, work);
0297     struct i2c_client *client = ds1374->client;
0298     int stat, control;
0299 
0300     mutex_lock(&ds1374->mutex);
0301 
0302     stat = i2c_smbus_read_byte_data(client, DS1374_REG_SR);
0303     if (stat < 0)
0304         goto unlock;
0305 
0306     if (stat & DS1374_REG_SR_AF) {
0307         stat &= ~DS1374_REG_SR_AF;
0308         i2c_smbus_write_byte_data(client, DS1374_REG_SR, stat);
0309 
0310         control = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
0311         if (control < 0)
0312             goto out;
0313 
0314         control &= ~(DS1374_REG_CR_WACE | DS1374_REG_CR_AIE);
0315         i2c_smbus_write_byte_data(client, DS1374_REG_CR, control);
0316 
0317         rtc_update_irq(ds1374->rtc, 1, RTC_AF | RTC_IRQF);
0318     }
0319 
0320 out:
0321     if (!ds1374->exiting)
0322         enable_irq(client->irq);
0323 unlock:
0324     mutex_unlock(&ds1374->mutex);
0325 }
0326 
0327 #ifndef CONFIG_RTC_DRV_DS1374_WDT
0328 static int ds1374_alarm_irq_enable(struct device *dev, unsigned int enabled)
0329 {
0330     struct i2c_client *client = to_i2c_client(dev);
0331     struct ds1374 *ds1374 = i2c_get_clientdata(client);
0332     int ret;
0333 
0334     mutex_lock(&ds1374->mutex);
0335 
0336     ret = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
0337     if (ret < 0)
0338         goto out;
0339 
0340     if (enabled) {
0341         ret |= DS1374_REG_CR_WACE | DS1374_REG_CR_AIE;
0342         ret &= ~DS1374_REG_CR_WDALM;
0343     } else {
0344         ret &= ~DS1374_REG_CR_WACE;
0345     }
0346     ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, ret);
0347 
0348 out:
0349     mutex_unlock(&ds1374->mutex);
0350     return ret;
0351 }
0352 #endif
0353 
0354 static const struct rtc_class_ops ds1374_rtc_ops = {
0355     .read_time = ds1374_read_time,
0356     .set_time = ds1374_set_time,
0357 #ifndef CONFIG_RTC_DRV_DS1374_WDT
0358     .read_alarm = ds1374_read_alarm,
0359     .set_alarm = ds1374_set_alarm,
0360     .alarm_irq_enable = ds1374_alarm_irq_enable,
0361 #endif
0362 };
0363 
0364 #ifdef CONFIG_RTC_DRV_DS1374_WDT
0365 /*
0366  *****************************************************************************
0367  *
0368  * Watchdog Driver
0369  *
0370  *****************************************************************************
0371  */
0372 /* Default margin */
0373 #define TIMER_MARGIN_DEFAULT    32
0374 #define TIMER_MARGIN_MIN    1
0375 #define TIMER_MARGIN_MAX    4095 /* 24-bit value */
0376 
0377 static int wdt_margin;
0378 module_param(wdt_margin, int, 0);
0379 MODULE_PARM_DESC(wdt_margin, "Watchdog timeout in seconds (default 32s)");
0380 
0381 static bool nowayout = WATCHDOG_NOWAYOUT;
0382 module_param(nowayout, bool, 0);
0383 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default ="
0384         __MODULE_STRING(WATCHDOG_NOWAYOUT)")");
0385 
0386 static const struct watchdog_info ds1374_wdt_info = {
0387     .identity       = "DS1374 Watchdog",
0388     .options        = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
0389                         WDIOF_MAGICCLOSE,
0390 };
0391 
0392 static int ds1374_wdt_settimeout(struct watchdog_device *wdt, unsigned int timeout)
0393 {
0394     struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
0395     struct i2c_client *client = ds1374->client;
0396     int ret, cr;
0397 
0398     wdt->timeout = timeout;
0399 
0400     cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
0401     if (cr < 0)
0402         return cr;
0403 
0404     /* Disable any existing watchdog/alarm before setting the new one */
0405     cr &= ~DS1374_REG_CR_WACE;
0406 
0407     ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
0408     if (ret < 0)
0409         return ret;
0410 
0411     /* Set new watchdog time */
0412     timeout = timeout * 4096;
0413     ret = ds1374_write_rtc(client, timeout, DS1374_REG_WDALM0, 3);
0414     if (ret)
0415         return ret;
0416 
0417     /* Enable watchdog timer */
0418     cr |= DS1374_REG_CR_WACE | DS1374_REG_CR_WDALM;
0419     cr &= ~DS1374_REG_CR_WDSTR;/* for RST PIN */
0420     cr &= ~DS1374_REG_CR_AIE;
0421 
0422     ret = i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
0423     if (ret < 0)
0424         return ret;
0425 
0426     return 0;
0427 }
0428 
0429 /*
0430  * Reload the watchdog timer.  (ie, pat the watchdog)
0431  */
0432 static int ds1374_wdt_start(struct watchdog_device *wdt)
0433 {
0434     struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
0435     u32 val;
0436 
0437     return ds1374_read_rtc(ds1374->client, &val, DS1374_REG_WDALM0, 3);
0438 }
0439 
0440 static int ds1374_wdt_stop(struct watchdog_device *wdt)
0441 {
0442     struct ds1374 *ds1374 = watchdog_get_drvdata(wdt);
0443     struct i2c_client *client = ds1374->client;
0444     int cr;
0445 
0446     cr = i2c_smbus_read_byte_data(client, DS1374_REG_CR);
0447     if (cr < 0)
0448         return cr;
0449 
0450     /* Disable watchdog timer */
0451     cr &= ~DS1374_REG_CR_WACE;
0452 
0453     return i2c_smbus_write_byte_data(client, DS1374_REG_CR, cr);
0454 }
0455 
0456 static const struct watchdog_ops ds1374_wdt_ops = {
0457     .owner          = THIS_MODULE,
0458     .start          = ds1374_wdt_start,
0459     .stop           = ds1374_wdt_stop,
0460     .set_timeout    = ds1374_wdt_settimeout,
0461 };
0462 #endif /*CONFIG_RTC_DRV_DS1374_WDT*/
0463 /*
0464  *****************************************************************************
0465  *
0466  *  Driver Interface
0467  *
0468  *****************************************************************************
0469  */
0470 static int ds1374_probe(struct i2c_client *client)
0471 {
0472     struct ds1374 *ds1374;
0473     int ret;
0474 
0475     ds1374 = devm_kzalloc(&client->dev, sizeof(struct ds1374), GFP_KERNEL);
0476     if (!ds1374)
0477         return -ENOMEM;
0478 
0479     ds1374->rtc = devm_rtc_allocate_device(&client->dev);
0480     if (IS_ERR(ds1374->rtc))
0481         return PTR_ERR(ds1374->rtc);
0482 
0483     ds1374->client = client;
0484     i2c_set_clientdata(client, ds1374);
0485 
0486     INIT_WORK(&ds1374->work, ds1374_work);
0487     mutex_init(&ds1374->mutex);
0488 
0489     ret = ds1374_check_rtc_status(client);
0490     if (ret)
0491         return ret;
0492 
0493     if (client->irq > 0) {
0494         ret = devm_request_irq(&client->dev, client->irq, ds1374_irq, 0,
0495                     "ds1374", client);
0496         if (ret) {
0497             dev_err(&client->dev, "unable to request IRQ\n");
0498             return ret;
0499         }
0500 
0501         device_set_wakeup_capable(&client->dev, 1);
0502     }
0503 
0504     ds1374->rtc->ops = &ds1374_rtc_ops;
0505     ds1374->rtc->range_max = U32_MAX;
0506 
0507     ret = devm_rtc_register_device(ds1374->rtc);
0508     if (ret)
0509         return ret;
0510 
0511 #ifdef CONFIG_RTC_DRV_DS1374_WDT
0512     ds1374->wdt.info = &ds1374_wdt_info;
0513     ds1374->wdt.ops = &ds1374_wdt_ops;
0514     ds1374->wdt.timeout = TIMER_MARGIN_DEFAULT;
0515     ds1374->wdt.min_timeout = TIMER_MARGIN_MIN;
0516     ds1374->wdt.max_timeout = TIMER_MARGIN_MAX;
0517 
0518     watchdog_init_timeout(&ds1374->wdt, wdt_margin, &client->dev);
0519     watchdog_set_nowayout(&ds1374->wdt, nowayout);
0520     watchdog_stop_on_reboot(&ds1374->wdt);
0521     watchdog_stop_on_unregister(&ds1374->wdt);
0522     watchdog_set_drvdata(&ds1374->wdt, ds1374);
0523     ds1374_wdt_settimeout(&ds1374->wdt, ds1374->wdt.timeout);
0524 
0525     ret = devm_watchdog_register_device(&client->dev, &ds1374->wdt);
0526     if (ret)
0527         return ret;
0528 #endif
0529 
0530     return 0;
0531 }
0532 
0533 static int ds1374_remove(struct i2c_client *client)
0534 {
0535     struct ds1374 *ds1374 = i2c_get_clientdata(client);
0536 
0537     if (client->irq > 0) {
0538         mutex_lock(&ds1374->mutex);
0539         ds1374->exiting = 1;
0540         mutex_unlock(&ds1374->mutex);
0541 
0542         devm_free_irq(&client->dev, client->irq, client);
0543         cancel_work_sync(&ds1374->work);
0544     }
0545 
0546     return 0;
0547 }
0548 
0549 #ifdef CONFIG_PM_SLEEP
0550 static int ds1374_suspend(struct device *dev)
0551 {
0552     struct i2c_client *client = to_i2c_client(dev);
0553 
0554     if (client->irq > 0 && device_may_wakeup(&client->dev))
0555         enable_irq_wake(client->irq);
0556     return 0;
0557 }
0558 
0559 static int ds1374_resume(struct device *dev)
0560 {
0561     struct i2c_client *client = to_i2c_client(dev);
0562 
0563     if (client->irq > 0 && device_may_wakeup(&client->dev))
0564         disable_irq_wake(client->irq);
0565     return 0;
0566 }
0567 #endif
0568 
0569 static SIMPLE_DEV_PM_OPS(ds1374_pm, ds1374_suspend, ds1374_resume);
0570 
0571 static struct i2c_driver ds1374_driver = {
0572     .driver = {
0573         .name = "rtc-ds1374",
0574         .of_match_table = of_match_ptr(ds1374_of_match),
0575         .pm = &ds1374_pm,
0576     },
0577     .probe_new = ds1374_probe,
0578     .remove = ds1374_remove,
0579     .id_table = ds1374_id,
0580 };
0581 
0582 module_i2c_driver(ds1374_driver);
0583 
0584 MODULE_AUTHOR("Scott Wood <scottwood@freescale.com>");
0585 MODULE_DESCRIPTION("Maxim/Dallas DS1374 RTC Driver");
0586 MODULE_LICENSE("GPL");