hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/rtc/rtc-ds1672.c
....@@ -1,12 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * An rtc/i2c driver for the Dallas DS1672
34 * Copyright 2005-06 Tower Technologies
45 *
56 * Author: Alessandro Zummo <a.zummo@towertech.it>
6
- *
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.
107 */
118
129 #include <linux/i2c.h>
....@@ -21,17 +18,16 @@
2118
2219 #define DS1672_REG_CONTROL_EOSC 0x80
2320
24
-static struct i2c_driver ds1672_driver;
25
-
2621 /*
2722 * In the routines that deal directly with the ds1672 hardware, we use
2823 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch
29
- * Epoch is initialized as 2000. Time is set to UTC.
24
+ * Time is set to UTC.
3025 */
31
-static int ds1672_get_datetime(struct i2c_client *client, struct rtc_time *tm)
26
+static int ds1672_read_time(struct device *dev, struct rtc_time *tm)
3227 {
28
+ struct i2c_client *client = to_i2c_client(dev);
3329 unsigned long time;
34
- unsigned char addr = DS1672_REG_CNT_BASE;
30
+ unsigned char addr = DS1672_REG_CONTROL;
3531 unsigned char buf[4];
3632
3733 struct i2c_msg msgs[] = {
....@@ -43,10 +39,24 @@
4339 {/* read date */
4440 .addr = client->addr,
4541 .flags = I2C_M_RD,
46
- .len = 4,
42
+ .len = 1,
4743 .buf = buf
4844 },
4945 };
46
+
47
+ /* 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;
5060
5161 /* read date registers */
5262 if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
....@@ -61,20 +71,19 @@
6171 time = ((unsigned long)buf[3] << 24) | (buf[2] << 16) |
6272 (buf[1] << 8) | buf[0];
6373
64
- rtc_time_to_tm(time, tm);
74
+ rtc_time64_to_tm(time, tm);
6575
66
- dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
67
- "mday=%d, mon=%d, year=%d, wday=%d\n",
68
- __func__, tm->tm_sec, tm->tm_min, tm->tm_hour,
69
- tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
76
+ dev_dbg(&client->dev, "%s: tm is %ptR\n", __func__, tm);
7077
7178 return 0;
7279 }
7380
74
-static int ds1672_set_mmss(struct i2c_client *client, unsigned long secs)
81
+static int ds1672_set_time(struct device *dev, struct rtc_time *tm)
7582 {
83
+ struct i2c_client *client = to_i2c_client(dev);
7684 int xfer;
7785 unsigned char buf[6];
86
+ unsigned long secs = rtc_tm_to_time64(tm);
7887
7988 buf[0] = DS1672_REG_CNT_BASE;
8089 buf[1] = secs & 0x000000FF;
....@@ -92,71 +101,15 @@
92101 return 0;
93102 }
94103
95
-static int ds1672_rtc_read_time(struct device *dev, struct rtc_time *tm)
96
-{
97
- return ds1672_get_datetime(to_i2c_client(dev), tm);
98
-}
99
-
100
-static int ds1672_rtc_set_mmss(struct device *dev, unsigned long secs)
101
-{
102
- return ds1672_set_mmss(to_i2c_client(dev), secs);
103
-}
104
-
105
-static int ds1672_get_control(struct i2c_client *client, u8 *status)
106
-{
107
- unsigned char addr = DS1672_REG_CONTROL;
108
-
109
- struct i2c_msg msgs[] = {
110
- {/* setup read ptr */
111
- .addr = client->addr,
112
- .len = 1,
113
- .buf = &addr
114
- },
115
- {/* read control */
116
- .addr = client->addr,
117
- .flags = I2C_M_RD,
118
- .len = 1,
119
- .buf = status
120
- },
121
- };
122
-
123
- /* read control register */
124
- if ((i2c_transfer(client->adapter, &msgs[0], 2)) != 2) {
125
- dev_err(&client->dev, "%s: read error\n", __func__);
126
- return -EIO;
127
- }
128
-
129
- return 0;
130
-}
131
-
132
-/* following are the sysfs callback functions */
133
-static ssize_t show_control(struct device *dev, struct device_attribute *attr,
134
- char *buf)
135
-{
136
- struct i2c_client *client = to_i2c_client(dev);
137
- u8 control;
138
- int err;
139
-
140
- err = ds1672_get_control(client, &control);
141
- if (err)
142
- return err;
143
-
144
- return sprintf(buf, "%s\n", (control & DS1672_REG_CONTROL_EOSC)
145
- ? "disabled" : "enabled");
146
-}
147
-
148
-static DEVICE_ATTR(control, S_IRUGO, show_control, NULL);
149
-
150104 static const struct rtc_class_ops ds1672_rtc_ops = {
151
- .read_time = ds1672_rtc_read_time,
152
- .set_mmss = ds1672_rtc_set_mmss,
105
+ .read_time = ds1672_read_time,
106
+ .set_time = ds1672_set_time,
153107 };
154108
155109 static int ds1672_probe(struct i2c_client *client,
156110 const struct i2c_device_id *id)
157111 {
158112 int err = 0;
159
- u8 control;
160113 struct rtc_device *rtc;
161114
162115 dev_dbg(&client->dev, "%s\n", __func__);
....@@ -164,29 +117,18 @@
164117 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
165118 return -ENODEV;
166119
167
- rtc = devm_rtc_device_register(&client->dev, ds1672_driver.driver.name,
168
- &ds1672_rtc_ops, THIS_MODULE);
169
-
120
+ rtc = devm_rtc_allocate_device(&client->dev);
170121 if (IS_ERR(rtc))
171122 return PTR_ERR(rtc);
172123
173
- i2c_set_clientdata(client, rtc);
124
+ rtc->ops = &ds1672_rtc_ops;
125
+ rtc->range_max = U32_MAX;
174126
175
- /* read control register */
176
- err = ds1672_get_control(client, &control);
177
- if (err) {
178
- dev_warn(&client->dev, "Unable to read the control register\n");
179
- }
180
-
181
- if (control & DS1672_REG_CONTROL_EOSC)
182
- dev_warn(&client->dev, "Oscillator not enabled. "
183
- "Set time to enable.\n");
184
-
185
- /* Register sysfs hooks */
186
- err = device_create_file(&client->dev, &dev_attr_control);
127
+ err = rtc_register_device(rtc);
187128 if (err)
188
- dev_err(&client->dev, "Unable to create sysfs entry: %s\n",
189
- dev_attr_control.attr.name);
129
+ return err;
130
+
131
+ i2c_set_clientdata(client, rtc);
190132
191133 return 0;
192134 }