blob: 653abef2e5cd24a746d2ef5db3d1f68c01bf271f [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 Belloni7a5670c2019-04-07 23:05:37 +020087static int ds1672_set_mmss(struct device *dev, unsigned long secs)
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];
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080092
93 buf[0] = DS1672_REG_CNT_BASE;
94 buf[1] = secs & 0x000000FF;
95 buf[2] = (secs & 0x0000FF00) >> 8;
96 buf[3] = (secs & 0x00FF0000) >> 16;
97 buf[4] = (secs & 0xFF000000) >> 24;
Alessandro Zummo1716b0f2008-10-15 22:03:10 -070098 buf[5] = 0; /* set control reg to enable counting */
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -080099
Kumar Gala8a95b252006-04-10 22:54:39 -0700100 xfer = i2c_master_send(client, buf, 6);
101 if (xfer != 6) {
Harvey Harrison2a4e2b82008-04-28 02:12:00 -0700102 dev_err(&client->dev, "%s: send: %d\n", __func__, xfer);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800103 return -EIO;
104 }
105
106 return 0;
107}
108
David Brownellff8371a2006-09-30 23:28:17 -0700109static const struct rtc_class_ops ds1672_rtc_ops = {
Alexandre Belloni7a5670c2019-04-07 23:05:37 +0200110 .read_time = ds1672_read_time,
111 .set_mmss = ds1672_set_mmss,
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800112};
113
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700114static int ds1672_probe(struct i2c_client *client,
115 const struct i2c_device_id *id)
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800116{
117 int err = 0;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800118 struct rtc_device *rtc;
119
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700120 dev_dbg(&client->dev, "%s\n", __func__);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800121
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700122 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
123 return -ENODEV;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800124
Alexandre Bellonid1fbe692019-04-07 23:05:34 +0200125 rtc = devm_rtc_allocate_device(&client->dev);
126 if (IS_ERR(rtc))
127 return PTR_ERR(rtc);
128
129 rtc->ops = &ds1672_rtc_ops;
130 rtc->range_max = U32_MAX;
131
132 err = rtc_register_device(rtc);
133 if (err)
134 return err;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800135
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700136 if (IS_ERR(rtc))
137 return PTR_ERR(rtc);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800138
139 i2c_set_clientdata(client, rtc);
140
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800141 return 0;
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800142}
143
Arvind Yadav45a63512017-08-20 00:37:55 +0530144static const struct i2c_device_id ds1672_id[] = {
Alessandro Zummofe102c72008-12-09 13:14:11 -0800145 { "ds1672", 0 },
146 { }
147};
Axel Lin8ad0f5b2011-04-18 20:16:57 +0800148MODULE_DEVICE_TABLE(i2c, ds1672_id);
Alessandro Zummofe102c72008-12-09 13:14:11 -0800149
Javier Martinez Canillas23194ac2017-03-03 11:29:18 -0300150static const struct of_device_id ds1672_of_match[] = {
151 { .compatible = "dallas,ds1672" },
152 { }
153};
154MODULE_DEVICE_TABLE(of, ds1672_of_match);
155
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700156static struct i2c_driver ds1672_driver = {
157 .driver = {
158 .name = "rtc-ds1672",
Javier Martinez Canillas23194ac2017-03-03 11:29:18 -0300159 .of_match_table = of_match_ptr(ds1672_of_match),
160 },
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700161 .probe = &ds1672_probe,
Alessandro Zummofe102c72008-12-09 13:14:11 -0800162 .id_table = ds1672_id,
Alessandro Zummo1716b0f2008-10-15 22:03:10 -0700163};
164
Axel Lin0abc9202012-03-23 15:02:31 -0700165module_i2c_driver(ds1672_driver);
Alessandro Zummoedf1aaa2006-03-27 01:16:43 -0800166
167MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
168MODULE_DESCRIPTION("Dallas/Maxim DS1672 timekeeper driver");
169MODULE_LICENSE("GPL");