1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/bcd.h>
18#include <linux/rtc.h>
19#include <linux/log2.h>
20
21#define RX8581_REG_SC 0x00
22#define RX8581_REG_MN 0x01
23#define RX8581_REG_HR 0x02
24#define RX8581_REG_DW 0x03
25#define RX8581_REG_DM 0x04
26#define RX8581_REG_MO 0x05
27#define RX8581_REG_YR 0x06
28#define RX8581_REG_RAM 0x07
29#define RX8581_REG_AMN 0x08
30#define RX8581_REG_AHR 0x09
31#define RX8581_REG_ADM 0x0A
32#define RX8581_REG_ADW 0x0A
33#define RX8581_REG_TMR0 0x0B
34#define RX8581_REG_TMR1 0x0C
35#define RX8581_REG_EXT 0x0D
36#define RX8581_REG_FLAG 0x0E
37#define RX8581_REG_CTRL 0x0F
38
39
40
41#define RX8581_FLAG_UF 0x20
42#define RX8581_FLAG_TF 0x10
43#define RX8581_FLAG_AF 0x08
44#define RX8581_FLAG_VLF 0x02
45
46
47#define RX8581_CTRL_UIE 0x20
48#define RX8581_CTRL_TIE 0x10
49#define RX8581_CTRL_AIE 0x08
50#define RX8581_CTRL_STOP 0x02
51#define RX8581_CTRL_RESET 0x01
52
53struct rx8581 {
54 struct i2c_client *client;
55 struct rtc_device *rtc;
56 s32 (*read_block_data)(const struct i2c_client *client, u8 command,
57 u8 length, u8 *values);
58 s32 (*write_block_data)(const struct i2c_client *client, u8 command,
59 u8 length, const u8 *values);
60};
61
62static struct i2c_driver rx8581_driver;
63
64static int rx8581_read_block_data(const struct i2c_client *client, u8 command,
65 u8 length, u8 *values)
66{
67 s32 i, data;
68
69 for (i = 0; i < length; i++) {
70 data = i2c_smbus_read_byte_data(client, command + i);
71 if (data < 0)
72 return data;
73 values[i] = data;
74 }
75 return i;
76}
77
78static int rx8581_write_block_data(const struct i2c_client *client, u8 command,
79 u8 length, const u8 *values)
80{
81 s32 i, ret;
82
83 for (i = 0; i < length; i++) {
84 ret = i2c_smbus_write_byte_data(client, command + i,
85 values[i]);
86 if (ret < 0)
87 return ret;
88 }
89 return length;
90}
91
92
93
94
95
96static int rx8581_get_datetime(struct i2c_client *client, struct rtc_time *tm)
97{
98 unsigned char date[7];
99 int data, err;
100 struct rx8581 *rx8581 = i2c_get_clientdata(client);
101
102
103
104
105
106
107 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
108 if (data < 0) {
109 dev_err(&client->dev, "Unable to read device flags\n");
110 return -EIO;
111 }
112
113 do {
114
115 if (data & RX8581_FLAG_UF) {
116 err = i2c_smbus_write_byte_data(client,
117 RX8581_REG_FLAG, (data & ~RX8581_FLAG_UF));
118 if (err != 0) {
119 dev_err(&client->dev, "Unable to write device flags\n");
120 return -EIO;
121 }
122 }
123
124
125 err = rx8581->read_block_data(client, RX8581_REG_SC,
126 7, date);
127 if (err < 0) {
128 dev_err(&client->dev, "Unable to read date\n");
129 return -EIO;
130 }
131
132
133 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
134 if (data < 0) {
135 dev_err(&client->dev, "Unable to read device flags\n");
136 return -EIO;
137 }
138 } while (data & RX8581_FLAG_UF);
139
140 if (data & RX8581_FLAG_VLF)
141 dev_info(&client->dev,
142 "low voltage detected, date/time is not reliable.\n");
143
144 dev_dbg(&client->dev,
145 "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
146 "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
147 __func__,
148 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
149
150 tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
151 tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
152 tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F);
153 tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
154 tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
155 tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1;
156 tm->tm_year = bcd2bin(date[RX8581_REG_YR]);
157 if (tm->tm_year < 70)
158 tm->tm_year += 100;
159
160
161 dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
162 "mday=%d, mon=%d, year=%d, wday=%d\n",
163 __func__,
164 tm->tm_sec, tm->tm_min, tm->tm_hour,
165 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
166
167 err = rtc_valid_tm(tm);
168 if (err < 0)
169 dev_err(&client->dev, "retrieved date/time is not valid.\n");
170
171 return err;
172}
173
174static int rx8581_set_datetime(struct i2c_client *client, struct rtc_time *tm)
175{
176 int data, err;
177 unsigned char buf[7];
178 struct rx8581 *rx8581 = i2c_get_clientdata(client);
179
180 dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, "
181 "mday=%d, mon=%d, year=%d, wday=%d\n",
182 __func__,
183 tm->tm_sec, tm->tm_min, tm->tm_hour,
184 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
185
186
187 buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
188 buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
189 buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
190
191 buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
192
193
194 buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
195
196
197 buf[RX8581_REG_YR] = bin2bcd(tm->tm_year % 100);
198 buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
199
200
201 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
202 if (data < 0) {
203 dev_err(&client->dev, "Unable to read control register\n");
204 return -EIO;
205 }
206
207 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
208 (data | RX8581_CTRL_STOP));
209 if (err < 0) {
210 dev_err(&client->dev, "Unable to write control register\n");
211 return -EIO;
212 }
213
214
215 err = rx8581->write_block_data(client, RX8581_REG_SC, 7, buf);
216 if (err < 0) {
217 dev_err(&client->dev, "Unable to write to date registers\n");
218 return -EIO;
219 }
220
221
222 data = i2c_smbus_read_byte_data(client, RX8581_REG_FLAG);
223 if (data < 0) {
224 dev_err(&client->dev, "Unable to read flag register\n");
225 return -EIO;
226 }
227
228 err = i2c_smbus_write_byte_data(client, RX8581_REG_FLAG,
229 (data & ~(RX8581_FLAG_VLF)));
230 if (err != 0) {
231 dev_err(&client->dev, "Unable to write flag register\n");
232 return -EIO;
233 }
234
235
236 data = i2c_smbus_read_byte_data(client, RX8581_REG_CTRL);
237 if (data < 0) {
238 dev_err(&client->dev, "Unable to read control register\n");
239 return -EIO;
240 }
241
242 err = i2c_smbus_write_byte_data(client, RX8581_REG_CTRL,
243 (data & ~(RX8581_CTRL_STOP)));
244 if (err != 0) {
245 dev_err(&client->dev, "Unable to write control register\n");
246 return -EIO;
247 }
248
249 return 0;
250}
251
252static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
253{
254 return rx8581_get_datetime(to_i2c_client(dev), tm);
255}
256
257static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
258{
259 return rx8581_set_datetime(to_i2c_client(dev), tm);
260}
261
262static const struct rtc_class_ops rx8581_rtc_ops = {
263 .read_time = rx8581_rtc_read_time,
264 .set_time = rx8581_rtc_set_time,
265};
266
267static int rx8581_probe(struct i2c_client *client,
268 const struct i2c_device_id *id)
269{
270 struct rx8581 *rx8581;
271
272 dev_dbg(&client->dev, "%s\n", __func__);
273
274 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)
275 && !i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
276 return -EIO;
277
278 rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
279 if (!rx8581)
280 return -ENOMEM;
281
282 i2c_set_clientdata(client, rx8581);
283 rx8581->client = client;
284
285 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK)) {
286 rx8581->read_block_data = i2c_smbus_read_i2c_block_data;
287 rx8581->write_block_data = i2c_smbus_write_i2c_block_data;
288 } else {
289 rx8581->read_block_data = rx8581_read_block_data;
290 rx8581->write_block_data = rx8581_write_block_data;
291 }
292
293 rx8581->rtc = devm_rtc_device_register(&client->dev,
294 rx8581_driver.driver.name, &rx8581_rtc_ops, THIS_MODULE);
295
296 if (IS_ERR(rx8581->rtc)) {
297 dev_err(&client->dev,
298 "unable to register the class device\n");
299 return PTR_ERR(rx8581->rtc);
300 }
301
302 return 0;
303}
304
305static const struct i2c_device_id rx8581_id[] = {
306 { "rx8581", 0 },
307 { }
308};
309MODULE_DEVICE_TABLE(i2c, rx8581_id);
310
311static struct i2c_driver rx8581_driver = {
312 .driver = {
313 .name = "rtc-rx8581",
314 },
315 .probe = rx8581_probe,
316 .id_table = rx8581_id,
317};
318
319module_i2c_driver(rx8581_driver);
320
321MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
322MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
323MODULE_LICENSE("GPL");
324