blob: 4997b4f3d66725d7169bc266e02d6e35be6fc666 [file] [log] [blame]
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -08001/*
2 * An rtc/i2c driver for the Dallas DS1672
Alessandro Zummo39035862006-04-10 22:54:41 -07003 * Copyright 2005-06 Tower Technologies
4 *
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080012#include <linux/i2c.h>
13#include <linux/rtc.h>
Paul Gortmaker21138522011-05-27 09:57:25 -040014#include <linux/module.h>
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080015
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080016/* Registers */
17
18#define DS1672_REG_CNT_BASE 0
19#define DS1672_REG_CONTROL 4
20#define DS1672_REG_TRICKLE 5
21
Alessandro Zummo39035862006-04-10 22:54:41 -070022#define DS1672_REG_CONTROL_EOSC 0x80
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080023
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080024/*
25 * In the routines that deal directly with the ds1672 hardware, we use
26 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
Alexandre Bellonid1fbe692019-04-07 23:05:34 +020027 * Time is set to UTC.
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080028 */
Alexandre Belloni7a5670c2019-04-07 23:05:37 +020029static int ds1672_read_time(struct device *dev, struct rtc_time *tm)
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080030{
Alexandre Belloni7a5670c2019-04-07 23:05:37 +020031 struct i2c_client *client = to_i2c_client(dev);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080032 unsigned long time;
Alexandre Belloni10e3efc2019-04-07 23:05:35 +020033 unsigned char addr = DS1672_REG_CONTROL;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080034 unsigned char buf[4];
35
36 struct i2c_msg msgs[] = {
Shubhrajyoti D2bfc37d2012-10-04 17:14:17 -070037 {/* setup read ptr */
38 .addr = client->addr,
39 .len = 1,
40 .buf = &addr
41 },
42 {/* read date */
43 .addr = client->addr,
44 .flags = I2C_M_RD,
Alexandre Belloni10e3efc2019-04-07 23:05:35 +020045 .len = 1,
Shubhrajyoti D2bfc37d2012-10-04 17:14:17 -070046 .buf = buf
47 },
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080048 };
49
Alexandre Belloni10e3efc2019-04-07 23:05:35 +020050 /* read control register */
51 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
52 dev_warn(&client->dev, "Unable to read the control register\n");
53 return -EIO;
54 }
55
56 if (buf[0] & DS1672_REG_CONTROL_EOSC) {
57 dev_warn(&client->dev, "Oscillator not enabled. Set time to enable.\n");
58 return -EINVAL;
59 }
60
61 addr = DS1672_REG_CNT_BASE;
62 msgs[1].len = 4;
63
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080064 /* read date registers */
65 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070066 dev_err(&client->dev, "%s: read error\n", __func__);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080067 return -EIO;
68 }
69
70 dev_dbg(&client->dev,
Jeff Garzik11966ad2006-10-04 04:41:53 -040071 "%s: raw read data - counters=%02x,%02x,%02x,%02x\n",
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070072 __func__, buf[0], buf[1], buf[2], buf[3]);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080073
Colin Ian Kingf0c04c22019-02-05 18:04:49 +000074 time = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
75 (buf[1] << 8) | buf[0];
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080076
Alexandre Belloni520d6512019-04-07 23:05:38 +020077 rtc_time64_to_tm(time, tm);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080078
79 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
80 "mday=%d, mon=%d, year=%d, wday=%d\n",
Harvey Harrison2a4e2b82008-04-28 02:12:00 -070081 __func__, tm->tm_sec, tm->tm_min, tm->tm_hour,
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080082 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
83
84 return 0;
85}
86
Alexandre Belloni219219d2019-04-07 23:05:39 +020087static int ds1672_set_time(struct device *dev, struct rtc_time *tm)
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080088{
Alexandre Belloni7a5670c2019-04-07 23:05:37 +020089 struct i2c_client *client = to_i2c_client(dev);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080090 int xfer;
Kumar Gala8a95b252006-04-10 22:54:39 -070091 unsigned char buf[6];
Alexandre Belloni219219d2019-04-07 23:05:39 +020092 unsigned long secs = rtc_tm_to_time64(tm);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080093
94 buf[0] = DS1672_REG_CNT_BASE;
95 buf[1] = secs & 0x000000FF;
96 buf[2] = (secs & 0x0000FF00) >> 8;
97 buf[3] = (secs & 0x00FF0000) >> 16;
98 buf[4] = (secs & 0xFF000000) >> 24;
Alessandro Zummo1716b0f2008-10-15 22:03:10 -070099 buf[5] = 0; /* set control reg to enable counting */
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800100
Kumar Gala8a95b252006-04-10 22:54:39 -0700101 xfer = i2c_master_send(client, buf, 6);
102 if (xfer != 6) {
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700103 dev_err(&client->dev, "%s: send: %d\n", __func__, xfer);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800104 return -EIO;
105 }
106
107 return 0;
108}
109
David Brownellff8371a2006-09-30 23:28:17 -0700110static const struct rtc_class_ops ds1672_rtc_ops = {
Alexandre Belloni7a5670c2019-04-07 23:05:37 +0200111 .read_time = ds1672_read_time,
Alexandre Belloni219219d2019-04-07 23:05:39 +0200112 .set_time = ds1672_set_time,
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800113};
114
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700115static int ds1672_probe(struct i2c_client *client,
116 const struct i2c_device_id *id)
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800117{
118 int err = 0;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800119 struct rtc_device *rtc;
120
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700121 dev_dbg(&client->dev, "%s\n", __func__);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800122
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700123 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
124 return -ENODEV;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800125
Alexandre Bellonid1fbe692019-04-07 23:05:34 +0200126 rtc = devm_rtc_allocate_device(&client->dev);
127 if (IS_ERR(rtc))
128 return PTR_ERR(rtc);
129
130 rtc->ops = &ds1672_rtc_ops;
131 rtc->range_max = U32_MAX;
132
133 err = rtc_register_device(rtc);
134 if (err)
135 return err;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800136
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700137 if (IS_ERR(rtc))
138 return PTR_ERR(rtc);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800139
140 i2c_set_clientdata(client, rtc);
141
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800142 return 0;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800143}
144
Arvind Yadav45a63512017-08-20 00:37:55 +0530145static const struct i2c_device_id ds1672_id[] = {
Alessandro Zummofe102c72008-12-09 13:14:11 -0800146 { "ds1672", 0 },
147 { }
148};
Axel Lin8ad0f5b2011-04-18 20:16:57 +0800149MODULE_DEVICE_TABLE(i2c, ds1672_id);
Alessandro Zummofe102c72008-12-09 13:14:11 -0800150
Javier Martinez Canillas23194ac2017-03-03 11:29:18 -0300151static const struct of_device_id ds1672_of_match[] = {
152 { .compatible = "dallas,ds1672" },
153 { }
154};
155MODULE_DEVICE_TABLE(of, ds1672_of_match);
156
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700157static struct i2c_driver ds1672_driver = {
158 .driver = {
159 .name = "rtc-ds1672",
Javier Martinez Canillas23194ac2017-03-03 11:29:18 -0300160 .of_match_table = of_match_ptr(ds1672_of_match),
161 },
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700162 .probe = &ds1672_probe,
Alessandro Zummofe102c72008-12-09 13:14:11 -0800163 .id_table = ds1672_id,
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700164};
165
Axel Lin0abc9202012-03-23 15:02:31 -0700166module_i2c_driver(ds1672_driver);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800167
168MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
169MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
170MODULE_LICENSE("GPL");