blob: 95c23b31c952997787970797764f70bfddfc8cb6 [file] [log] [blame]
Alexandre Belloni69468322019-04-07 23:05:40 +02001// SPDX-License-Identifier: GPL-2.0
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -08002/*
3 * An rtc/i2c driver for the Dallas DS1672
Alessandro Zummo39035862006-04-10 22:54:41 -07004 * Copyright 2005-06 Tower Technologies
5 *
6 * Author: Alessandro Zummo <a.zummo@towertech.it>
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -08007 */
8
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -08009#include <linux/i2c.h>
10#include <linux/rtc.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040011#include <linux/module.h>
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080012
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080013/* Registers */
14
15#define DS1672_REG_CNT_BASE 0
16#define DS1672_REG_CONTROL 4
17#define DS1672_REG_TRICKLE 5
18
Alessandro Zummo39035862006-04-10 22:54:41 -070019#define DS1672_REG_CONTROL_EOSC 0x80
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080020
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080021/*
22 * In the routines that deal directly with the ds1672 hardware, we use
23 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
Alexandre Bellonid1fbe692019-04-07 23:05:34 +020024 * Time is set to UTC.
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080025 */
Alexandre Belloni7a5670c2019-04-07 23:05:37 +020026static int ds1672_read_time(struct device *dev, struct rtc_time *tm)
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080027{
Alexandre Belloni7a5670c2019-04-07 23:05:37 +020028 struct i2c_client *client = to_i2c_client(dev);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080029 unsigned long time;
Alexandre Belloni10e3efc2019-04-07 23:05:35 +020030 unsigned char addr = DS1672_REG_CONTROL;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080031 unsigned char buf[4];
32
33 struct i2c_msg msgs[] = {
Shubhrajyoti D2bfc37d2012-10-04 17:14:17 -070034 {/* setup read ptr */
35 .addr = client->addr,
36 .len = 1,
37 .buf = &addr
38 },
39 {/* read date */
40 .addr = client->addr,
41 .flags = I2C_M_RD,
Alexandre Belloni10e3efc2019-04-07 23:05:35 +020042 .len = 1,
Shubhrajyoti D2bfc37d2012-10-04 17:14:17 -070043 .buf = buf
44 },
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080045 };
46
Alexandre Belloni10e3efc2019-04-07 23:05:35 +020047 /* read control register */
48 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
49 dev_warn(&client->dev, "Unable to read the control register\n");
50 return -EIO;
51 }
52
53 if (buf[0] & DS1672_REG_CONTROL_EOSC) {
54 dev_warn(&client->dev, "Oscillator not enabled. Set time to enable.\n");
55 return -EINVAL;
56 }
57
58 addr = DS1672_REG_CNT_BASE;
59 msgs[1].len = 4;
60
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080061 /* read date registers */
62 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070063 dev_err(&client->dev, "%s: read error\n", __func__);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080064 return -EIO;
65 }
66
67 dev_dbg(&client->dev,
Jeff Garzik11966ad2006-10-04 04:41:53 -040068 "%s: raw read data - counters=%02x,%02x,%02x,%02x\n",
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070069 __func__, buf[0], buf[1], buf[2], buf[3]);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080070
Colin Ian Kingf0c04c22019-02-05 18:04:49 +000071 time = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
72 (buf[1] << 8) | buf[0];
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080073
Alexandre Belloni520d6512019-04-07 23:05:38 +020074 rtc_time64_to_tm(time, tm);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080075
76 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
77 "mday=%d, mon=%d, year=%d, wday=%d\n",
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070078 __func__, tm->tm_sec, tm->tm_min, tm->tm_hour,
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080079 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
80
81 return 0;
82}
83
Alexandre Belloni219219d2019-04-07 23:05:39 +020084static int ds1672_set_time(struct device *dev, struct rtc_time *tm)
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080085{
Alexandre Belloni7a5670c2019-04-07 23:05:37 +020086 struct i2c_client *client = to_i2c_client(dev);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080087 int xfer;
Kumar Gala8a95b252006-04-10 22:54:39 -070088 unsigned char buf[6];
Alexandre Belloni219219d2019-04-07 23:05:39 +020089 unsigned long secs = rtc_tm_to_time64(tm);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080090
91 buf[0] = DS1672_REG_CNT_BASE;
92 buf[1] = secs & 0x000000FF;
93 buf[2] = (secs & 0x0000FF00) >> 8;
94 buf[3] = (secs & 0x00FF0000) >> 16;
95 buf[4] = (secs & 0xFF000000) >> 24;
Alessandro Zummo1716b0f2008-10-15 22:03:10 -070096 buf[5] = 0; /* set control reg to enable counting */
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080097
Kumar Gala8a95b252006-04-10 22:54:39 -070098 xfer = i2c_master_send(client, buf, 6);
99 if (xfer != 6) {
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700100 dev_err(&client->dev, "%s: send: %d\n", __func__, xfer);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800101 return -EIO;
102 }
103
104 return 0;
105}
106
David Brownellff8371a2006-09-30 23:28:17 -0700107static const struct rtc_class_ops ds1672_rtc_ops = {
Alexandre Belloni7a5670c2019-04-07 23:05:37 +0200108 .read_time = ds1672_read_time,
Alexandre Belloni219219d2019-04-07 23:05:39 +0200109 .set_time = ds1672_set_time,
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800110};
111
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700112static int ds1672_probe(struct i2c_client *client,
113 const struct i2c_device_id *id)
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800114{
115 int err = 0;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800116 struct rtc_device *rtc;
117
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700118 dev_dbg(&client->dev, "%s\n", __func__);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800119
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700120 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
121 return -ENODEV;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800122
Alexandre Bellonid1fbe692019-04-07 23:05:34 +0200123 rtc = devm_rtc_allocate_device(&client->dev);
124 if (IS_ERR(rtc))
125 return PTR_ERR(rtc);
126
127 rtc->ops = &ds1672_rtc_ops;
128 rtc->range_max = U32_MAX;
129
130 err = rtc_register_device(rtc);
131 if (err)
132 return err;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800133
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700134 if (IS_ERR(rtc))
135 return PTR_ERR(rtc);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800136
137 i2c_set_clientdata(client, rtc);
138
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800139 return 0;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800140}
141
Arvind Yadav45a63512017-08-20 00:37:55 +0530142static const struct i2c_device_id ds1672_id[] = {
Alessandro Zummofe102c72008-12-09 13:14:11 -0800143 { "ds1672", 0 },
144 { }
145};
Axel Lin8ad0f5b2011-04-18 20:16:57 +0800146MODULE_DEVICE_TABLE(i2c, ds1672_id);
Alessandro Zummofe102c72008-12-09 13:14:11 -0800147
Javier Martinez Canillas23194ac2017-03-03 11:29:18 -0300148static const struct of_device_id ds1672_of_match[] = {
149 { .compatible = "dallas,ds1672" },
150 { }
151};
152MODULE_DEVICE_TABLE(of, ds1672_of_match);
153
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700154static struct i2c_driver ds1672_driver = {
155 .driver = {
156 .name = "rtc-ds1672",
Javier Martinez Canillas23194ac2017-03-03 11:29:18 -0300157 .of_match_table = of_match_ptr(ds1672_of_match),
158 },
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700159 .probe = &ds1672_probe,
Alessandro Zummofe102c72008-12-09 13:14:11 -0800160 .id_table = ds1672_id,
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700161};
162
Axel Lin0abc9202012-03-23 15:02:31 -0700163module_i2c_driver(ds1672_driver);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800164
165MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
166MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
167MODULE_LICENSE("GPL");