Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * An rtc/i2c driver for the Dallas DS1672
0004  * Copyright 2005-06 Tower Technologies
0005  *
0006  * Author: Alessandro Zummo <a.zummo@towertech.it>
0007  */
0008 
0009 #include <linux/i2c.h>
0010 #include <linux/rtc.h>
0011 #include <linux/module.h>
0012 
0013 /* Registers */
0014 
0015 #define DS1672_REG_CNT_BASE 0
0016 #define DS1672_REG_CONTROL  4
0017 #define DS1672_REG_TRICKLE  5
0018 
0019 #define DS1672_REG_CONTROL_EOSC 0x80
0020 
0021 /*
0022  * In the routines that deal directly with the ds1672 hardware, we use
0023  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
0024  * Time is set to UTC.
0025  */
0026 static int ds1672_read_time(struct device *dev, struct rtc_time *tm)
0027 {
0028     struct i2c_client *client = to_i2c_client(dev);
0029     unsigned long time;
0030     unsigned char addr = DS1672_REG_CONTROL;
0031     unsigned char buf[4];
0032 
0033     struct i2c_msg msgs[] = {
0034         {/* setup read ptr */
0035             .addr = client->addr,
0036             .len = 1,
0037             .buf = &addr
0038         },
0039         {/* read date */
0040             .addr = client->addr,
0041             .flags = I2C_M_RD,
0042             .len = 1,
0043             .buf = buf
0044         },
0045     };
0046 
0047     /* read control register */
0048     if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
0049         dev_warn(&client->dev, "Unable to read the control register\n");
0050         return -EIO;
0051     }
0052 
0053     if (buf[0] & DS1672_REG_CONTROL_EOSC) {
0054         dev_warn(&client->dev, "Oscillator not enabled. Set time to enable.\n");
0055         return -EINVAL;
0056     }
0057 
0058     addr = DS1672_REG_CNT_BASE;
0059     msgs[1].len = 4;
0060 
0061     /* read date registers */
0062     if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
0063         dev_err(&client->dev, "%s: read error\n", __func__);
0064         return -EIO;
0065     }
0066 
0067     dev_dbg(&client->dev,
0068         "%s: raw read data - counters=%02x,%02x,%02x,%02x\n",
0069         __func__, buf[0], buf[1], buf[2], buf[3]);
0070 
0071     time = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
0072            (buf[1] << 8) | buf[0];
0073 
0074     rtc_time64_to_tm(time, tm);
0075 
0076     dev_dbg(&client->dev, "%s: tm is %ptR\n", __func__, tm);
0077 
0078     return 0;
0079 }
0080 
0081 static int ds1672_set_time(struct device *dev, struct rtc_time *tm)
0082 {
0083     struct i2c_client *client = to_i2c_client(dev);
0084     int xfer;
0085     unsigned char buf[6];
0086     unsigned long secs = rtc_tm_to_time64(tm);
0087 
0088     buf[0] = DS1672_REG_CNT_BASE;
0089     buf[1] = secs & 0x000000FF;
0090     buf[2] = (secs & 0x0000FF00) >> 8;
0091     buf[3] = (secs & 0x00FF0000) >> 16;
0092     buf[4] = (secs & 0xFF000000) >> 24;
0093     buf[5] = 0;     /* set control reg to enable counting */
0094 
0095     xfer = i2c_master_send(client, buf, 6);
0096     if (xfer != 6) {
0097         dev_err(&client->dev, "%s: send: %d\n", __func__, xfer);
0098         return -EIO;
0099     }
0100 
0101     return 0;
0102 }
0103 
0104 static const struct rtc_class_ops ds1672_rtc_ops = {
0105     .read_time = ds1672_read_time,
0106     .set_time = ds1672_set_time,
0107 };
0108 
0109 static int ds1672_probe(struct i2c_client *client)
0110 {
0111     int err = 0;
0112     struct rtc_device *rtc;
0113 
0114     dev_dbg(&client->dev, "%s\n", __func__);
0115 
0116     if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0117         return -ENODEV;
0118 
0119     rtc = devm_rtc_allocate_device(&client->dev);
0120     if (IS_ERR(rtc))
0121         return PTR_ERR(rtc);
0122 
0123     rtc->ops = &ds1672_rtc_ops;
0124     rtc->range_max = U32_MAX;
0125 
0126     err = devm_rtc_register_device(rtc);
0127     if (err)
0128         return err;
0129 
0130     i2c_set_clientdata(client, rtc);
0131 
0132     return 0;
0133 }
0134 
0135 static const struct i2c_device_id ds1672_id[] = {
0136     { "ds1672", 0 },
0137     { }
0138 };
0139 MODULE_DEVICE_TABLE(i2c, ds1672_id);
0140 
0141 static const __maybe_unused struct of_device_id ds1672_of_match[] = {
0142     { .compatible = "dallas,ds1672" },
0143     { }
0144 };
0145 MODULE_DEVICE_TABLE(of, ds1672_of_match);
0146 
0147 static struct i2c_driver ds1672_driver = {
0148     .driver = {
0149            .name = "rtc-ds1672",
0150            .of_match_table = of_match_ptr(ds1672_of_match),
0151     },
0152     .probe_new = ds1672_probe,
0153     .id_table = ds1672_id,
0154 };
0155 
0156 module_i2c_driver(ds1672_driver);
0157 
0158 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
0159 MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
0160 MODULE_LICENSE("GPL");