blob: 9da84df9f15209943948d41d6e9a4ef2a602d12a [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 Harrison2a4e2b8782008-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 Harrison2a4e2b8782008-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
Alexandre Bellonie3a76912019-04-07 23:05:41 +020076 dev_dbg(&client->dev, "%s: tm is %ptR\n", __func__, tm);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080077
78 return 0;
79}
80
Alexandre Belloni219219d2019-04-07 23:05:39 +020081static int ds1672_set_time(struct device *dev, struct rtc_time *tm)
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080082{
Alexandre Belloni7a5670c2019-04-07 23:05:37 +020083 struct i2c_client *client = to_i2c_client(dev);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080084 int xfer;
Kumar Gala8a95b252006-04-10 22:54:39 -070085 unsigned char buf[6];
Alexandre Belloni219219d2019-04-07 23:05:39 +020086 unsigned long secs = rtc_tm_to_time64(tm);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080087
88 buf[0] = DS1672_REG_CNT_BASE;
89 buf[1] = secs & 0x000000FF;
90 buf[2] = (secs & 0x0000FF00) >> 8;
91 buf[3] = (secs & 0x00FF0000) >> 16;
92 buf[4] = (secs & 0xFF000000) >> 24;
Alessandro Zummo1716b0f2008-10-15 22:03:10 -070093 buf[5] = 0; /* set control reg to enable counting */
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080094
Kumar Gala8a95b252006-04-10 22:54:39 -070095 xfer = i2c_master_send(client, buf, 6);
96 if (xfer != 6) {
Harvey Harrison2a4e2b8782008-04-28 02:12:00 -070097 dev_err(&client->dev, "%s: send: %d\n", __func__, xfer);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080098 return -EIO;
99 }
100
101 return 0;
102}
103
David Brownellff8371a2006-09-30 23:28:17 -0700104static const struct rtc_class_ops ds1672_rtc_ops = {
Alexandre Belloni7a5670c2019-04-07 23:05:37 +0200105 .read_time = ds1672_read_time,
Alexandre Belloni219219d2019-04-07 23:05:39 +0200106 .set_time = ds1672_set_time,
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800107};
108
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700109static int ds1672_probe(struct i2c_client *client,
110 const struct i2c_device_id *id)
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800111{
112 int err = 0;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800113 struct rtc_device *rtc;
114
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700115 dev_dbg(&client->dev, "%s\n", __func__);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800116
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700117 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
118 return -ENODEV;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800119
Alexandre Bellonid1fbe692019-04-07 23:05:34 +0200120 rtc = devm_rtc_allocate_device(&client->dev);
121 if (IS_ERR(rtc))
122 return PTR_ERR(rtc);
123
124 rtc->ops = &ds1672_rtc_ops;
125 rtc->range_max = U32_MAX;
126
127 err = rtc_register_device(rtc);
128 if (err)
129 return err;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800130
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800131 i2c_set_clientdata(client, rtc);
132
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800133 return 0;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800134}
135
Arvind Yadav45a63512017-08-20 00:37:55 +0530136static const struct i2c_device_id ds1672_id[] = {
Alessandro Zummofe102c72008-12-09 13:14:11 -0800137 { "ds1672", 0 },
138 { }
139};
Axel Lin8ad0f5b2011-04-18 20:16:57 +0800140MODULE_DEVICE_TABLE(i2c, ds1672_id);
Alessandro Zummofe102c72008-12-09 13:14:11 -0800141
Javier Martinez Canillas23194ac2017-03-03 11:29:18 -0300142static const struct of_device_id ds1672_of_match[] = {
143 { .compatible = "dallas,ds1672" },
144 { }
145};
146MODULE_DEVICE_TABLE(of, ds1672_of_match);
147
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700148static struct i2c_driver ds1672_driver = {
149 .driver = {
150 .name = "rtc-ds1672",
Javier Martinez Canillas23194ac2017-03-03 11:29:18 -0300151 .of_match_table = of_match_ptr(ds1672_of_match),
152 },
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700153 .probe = &ds1672_probe,
Alessandro Zummofe102c72008-12-09 13:14:11 -0800154 .id_table = ds1672_id,
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700155};
156
Axel Lin0abc9202012-03-23 15:02:31 -0700157module_i2c_driver(ds1672_driver);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800158
159MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
160MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
161MODULE_LICENSE("GPL");