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/regmap.h>
19#include <linux/rtc.h>
20#include <linux/log2.h>
21
22#define RX8581_REG_SC 0x00
23#define RX8581_REG_MN 0x01
24#define RX8581_REG_HR 0x02
25#define RX8581_REG_DW 0x03
26#define RX8581_REG_DM 0x04
27#define RX8581_REG_MO 0x05
28#define RX8581_REG_YR 0x06
29#define RX8581_REG_RAM 0x07
30#define RX8581_REG_AMN 0x08
31#define RX8581_REG_AHR 0x09
32#define RX8581_REG_ADM 0x0A
33#define RX8581_REG_ADW 0x0A
34#define RX8581_REG_TMR0 0x0B
35#define RX8581_REG_TMR1 0x0C
36#define RX8581_REG_EXT 0x0D
37#define RX8581_REG_FLAG 0x0E
38#define RX8581_REG_CTRL 0x0F
39
40
41
42#define RX8581_FLAG_UF 0x20
43#define RX8581_FLAG_TF 0x10
44#define RX8581_FLAG_AF 0x08
45#define RX8581_FLAG_VLF 0x02
46
47
48#define RX8581_CTRL_UIE 0x20
49#define RX8581_CTRL_TIE 0x10
50#define RX8581_CTRL_AIE 0x08
51#define RX8581_CTRL_STOP 0x02
52#define RX8581_CTRL_RESET 0x01
53
54struct rx8581 {
55 struct regmap *regmap;
56 struct rtc_device *rtc;
57};
58
59
60
61
62
63static int rx8581_rtc_read_time(struct device *dev, struct rtc_time *tm)
64{
65 struct i2c_client *client = to_i2c_client(dev);
66 unsigned char date[7];
67 unsigned int data;
68 int err;
69 struct rx8581 *rx8581 = i2c_get_clientdata(client);
70
71
72
73
74
75
76 err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
77 if (err < 0)
78 return err;
79
80 if (data & RX8581_FLAG_VLF) {
81 dev_warn(dev,
82 "low voltage detected, date/time is not reliable.\n");
83 return -EINVAL;
84 }
85
86 do {
87
88 if (data & RX8581_FLAG_UF) {
89 err = regmap_write(rx8581->regmap, RX8581_REG_FLAG,
90 data & ~RX8581_FLAG_UF);
91 if (err < 0)
92 return err;
93 }
94
95
96 err = regmap_bulk_read(rx8581->regmap, RX8581_REG_SC, date,
97 sizeof(date));
98 if (err < 0)
99 return err;
100
101
102 err = regmap_read(rx8581->regmap, RX8581_REG_FLAG, &data);
103 if (err < 0)
104 return err;
105 } while (data & RX8581_FLAG_UF);
106
107 dev_dbg(dev, "%s: raw data is sec=%02x, min=%02x, hr=%02x, "
108 "wday=%02x, mday=%02x, mon=%02x, year=%02x\n",
109 __func__,
110 date[0], date[1], date[2], date[3], date[4], date[5], date[6]);
111
112 tm->tm_sec = bcd2bin(date[RX8581_REG_SC] & 0x7F);
113 tm->tm_min = bcd2bin(date[RX8581_REG_MN] & 0x7F);
114 tm->tm_hour = bcd2bin(date[RX8581_REG_HR] & 0x3F);
115 tm->tm_wday = ilog2(date[RX8581_REG_DW] & 0x7F);
116 tm->tm_mday = bcd2bin(date[RX8581_REG_DM] & 0x3F);
117 tm->tm_mon = bcd2bin(date[RX8581_REG_MO] & 0x1F) - 1;
118 tm->tm_year = bcd2bin(date[RX8581_REG_YR]) + 100;
119
120 dev_dbg(dev, "%s: tm is secs=%d, mins=%d, hours=%d, "
121 "mday=%d, mon=%d, year=%d, wday=%d\n",
122 __func__,
123 tm->tm_sec, tm->tm_min, tm->tm_hour,
124 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
125
126 return 0;
127}
128
129static int rx8581_rtc_set_time(struct device *dev, struct rtc_time *tm)
130{
131 struct i2c_client *client = to_i2c_client(dev);
132 int err;
133 unsigned char buf[7];
134 struct rx8581 *rx8581 = i2c_get_clientdata(client);
135
136 dev_dbg(dev, "%s: secs=%d, mins=%d, hours=%d, "
137 "mday=%d, mon=%d, year=%d, wday=%d\n",
138 __func__,
139 tm->tm_sec, tm->tm_min, tm->tm_hour,
140 tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday);
141
142
143 buf[RX8581_REG_SC] = bin2bcd(tm->tm_sec);
144 buf[RX8581_REG_MN] = bin2bcd(tm->tm_min);
145 buf[RX8581_REG_HR] = bin2bcd(tm->tm_hour);
146
147 buf[RX8581_REG_DM] = bin2bcd(tm->tm_mday);
148
149
150 buf[RX8581_REG_MO] = bin2bcd(tm->tm_mon + 1);
151
152
153 buf[RX8581_REG_YR] = bin2bcd(tm->tm_year - 100);
154 buf[RX8581_REG_DW] = (0x1 << tm->tm_wday);
155
156
157 err = regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
158 RX8581_CTRL_STOP, RX8581_CTRL_STOP);
159 if (err < 0)
160 return err;
161
162
163 err = regmap_bulk_write(rx8581->regmap, RX8581_REG_SC,
164 buf, sizeof(buf));
165 if (err < 0)
166 return err;
167
168
169 err = regmap_update_bits(rx8581->regmap, RX8581_REG_FLAG,
170 RX8581_FLAG_VLF, 0);
171 if (err < 0)
172 return err;
173
174
175 return regmap_update_bits(rx8581->regmap, RX8581_REG_CTRL,
176 RX8581_CTRL_STOP, 0);
177}
178
179static const struct rtc_class_ops rx8581_rtc_ops = {
180 .read_time = rx8581_rtc_read_time,
181 .set_time = rx8581_rtc_set_time,
182};
183
184static int rx8581_probe(struct i2c_client *client,
185 const struct i2c_device_id *id)
186{
187 struct rx8581 *rx8581;
188 static const struct regmap_config config = {
189 .reg_bits = 8,
190 .val_bits = 8,
191 .max_register = 0xf,
192 };
193
194 dev_dbg(&client->dev, "%s\n", __func__);
195
196 rx8581 = devm_kzalloc(&client->dev, sizeof(struct rx8581), GFP_KERNEL);
197 if (!rx8581)
198 return -ENOMEM;
199
200 i2c_set_clientdata(client, rx8581);
201
202 rx8581->regmap = devm_regmap_init_i2c(client, &config);
203 if (IS_ERR(rx8581->regmap))
204 return PTR_ERR(rx8581->regmap);
205
206 rx8581->rtc = devm_rtc_allocate_device(&client->dev);
207 if (IS_ERR(rx8581->rtc))
208 return PTR_ERR(rx8581->rtc);
209
210 rx8581->rtc->ops = &rx8581_rtc_ops;
211 rx8581->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
212 rx8581->rtc->range_max = RTC_TIMESTAMP_END_2099;
213 rx8581->rtc->start_secs = 0;
214 rx8581->rtc->set_start_time = true;
215
216 return rtc_register_device(rx8581->rtc);
217}
218
219static const struct i2c_device_id rx8581_id[] = {
220 { "rx8581", 0 },
221 { }
222};
223MODULE_DEVICE_TABLE(i2c, rx8581_id);
224
225static const struct of_device_id rx8581_of_match[] = {
226 { .compatible = "epson,rx8581" },
227 { }
228};
229MODULE_DEVICE_TABLE(of, rx8581_of_match);
230
231static struct i2c_driver rx8581_driver = {
232 .driver = {
233 .name = "rtc-rx8581",
234 .of_match_table = of_match_ptr(rx8581_of_match),
235 },
236 .probe = rx8581_probe,
237 .id_table = rx8581_id,
238};
239
240module_i2c_driver(rx8581_driver);
241
242MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com>");
243MODULE_DESCRIPTION("Epson RX-8581 RTC driver");
244MODULE_LICENSE("GPL");
245