PM / AVS: rockchip-io: add io selectors and supplies for rk3399
[linux-2.6-block.git] / drivers / rtc / rtc-abx80x.c
CommitLineData
4d61ff6b
PDM
1/*
2 * A driver for the I2C members of the Abracon AB x8xx RTC family,
3 * and compatible: AB 1805 and AB 0805
4 *
5 * Copyright 2014-2015 Macq S.A.
6 *
7 * Author: Philippe De Muyter <phdm@macqel.be>
8 * Author: Alexandre Belloni <alexandre.belloni@free-electrons.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/bcd.h>
17#include <linux/i2c.h>
18#include <linux/module.h>
19#include <linux/rtc.h>
20
21#define ABX8XX_REG_HTH 0x00
22#define ABX8XX_REG_SC 0x01
23#define ABX8XX_REG_MN 0x02
24#define ABX8XX_REG_HR 0x03
25#define ABX8XX_REG_DA 0x04
26#define ABX8XX_REG_MO 0x05
27#define ABX8XX_REG_YR 0x06
28#define ABX8XX_REG_WD 0x07
29
718a820a
AB
30#define ABX8XX_REG_AHTH 0x08
31#define ABX8XX_REG_ASC 0x09
32#define ABX8XX_REG_AMN 0x0a
33#define ABX8XX_REG_AHR 0x0b
34#define ABX8XX_REG_ADA 0x0c
35#define ABX8XX_REG_AMO 0x0d
36#define ABX8XX_REG_AWD 0x0e
37
38#define ABX8XX_REG_STATUS 0x0f
39#define ABX8XX_STATUS_AF BIT(2)
40
4d61ff6b 41#define ABX8XX_REG_CTRL1 0x10
5f1b2f77 42#define ABX8XX_CTRL_WRITE BIT(0)
718a820a 43#define ABX8XX_CTRL_ARST BIT(2)
4d61ff6b
PDM
44#define ABX8XX_CTRL_12_24 BIT(6)
45
718a820a
AB
46#define ABX8XX_REG_IRQ 0x12
47#define ABX8XX_IRQ_AIE BIT(2)
48#define ABX8XX_IRQ_IM_1_4 (0x3 << 5)
49
50#define ABX8XX_REG_CD_TIMER_CTL 0x18
51
4d61ff6b
PDM
52#define ABX8XX_REG_CFG_KEY 0x1f
53#define ABX8XX_CFG_KEY_MISC 0x9d
54
55#define ABX8XX_REG_ID0 0x28
56
57#define ABX8XX_REG_TRICKLE 0x20
58#define ABX8XX_TRICKLE_CHARGE_ENABLE 0xa0
59#define ABX8XX_TRICKLE_STANDARD_DIODE 0x8
60#define ABX8XX_TRICKLE_SCHOTTKY_DIODE 0x4
61
62static u8 trickle_resistors[] = {0, 3, 6, 11};
63
64enum abx80x_chip {AB0801, AB0803, AB0804, AB0805,
65 AB1801, AB1803, AB1804, AB1805, ABX80X};
66
67struct abx80x_cap {
68 u16 pn;
69 bool has_tc;
70};
71
72static struct abx80x_cap abx80x_caps[] = {
73 [AB0801] = {.pn = 0x0801},
74 [AB0803] = {.pn = 0x0803},
75 [AB0804] = {.pn = 0x0804, .has_tc = true},
76 [AB0805] = {.pn = 0x0805, .has_tc = true},
77 [AB1801] = {.pn = 0x1801},
78 [AB1803] = {.pn = 0x1803},
79 [AB1804] = {.pn = 0x1804, .has_tc = true},
80 [AB1805] = {.pn = 0x1805, .has_tc = true},
81 [ABX80X] = {.pn = 0}
82};
83
4d61ff6b
PDM
84static int abx80x_enable_trickle_charger(struct i2c_client *client,
85 u8 trickle_cfg)
86{
87 int err;
88
89 /*
90 * Write the configuration key register to enable access to the Trickle
91 * register
92 */
93 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CFG_KEY,
94 ABX8XX_CFG_KEY_MISC);
95 if (err < 0) {
96 dev_err(&client->dev, "Unable to write configuration key\n");
97 return -EIO;
98 }
99
100 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_TRICKLE,
101 ABX8XX_TRICKLE_CHARGE_ENABLE |
102 trickle_cfg);
103 if (err < 0) {
104 dev_err(&client->dev, "Unable to write trickle register\n");
105 return -EIO;
106 }
107
108 return 0;
109}
110
111static int abx80x_rtc_read_time(struct device *dev, struct rtc_time *tm)
112{
113 struct i2c_client *client = to_i2c_client(dev);
114 unsigned char buf[8];
115 int err;
116
117 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_HTH,
118 sizeof(buf), buf);
119 if (err < 0) {
120 dev_err(&client->dev, "Unable to read date\n");
121 return -EIO;
122 }
123
124 tm->tm_sec = bcd2bin(buf[ABX8XX_REG_SC] & 0x7F);
125 tm->tm_min = bcd2bin(buf[ABX8XX_REG_MN] & 0x7F);
126 tm->tm_hour = bcd2bin(buf[ABX8XX_REG_HR] & 0x3F);
127 tm->tm_wday = buf[ABX8XX_REG_WD] & 0x7;
128 tm->tm_mday = bcd2bin(buf[ABX8XX_REG_DA] & 0x3F);
129 tm->tm_mon = bcd2bin(buf[ABX8XX_REG_MO] & 0x1F) - 1;
130 tm->tm_year = bcd2bin(buf[ABX8XX_REG_YR]) + 100;
131
132 err = rtc_valid_tm(tm);
133 if (err < 0)
134 dev_err(&client->dev, "retrieved date/time is not valid.\n");
135
136 return err;
137}
138
139static int abx80x_rtc_set_time(struct device *dev, struct rtc_time *tm)
140{
141 struct i2c_client *client = to_i2c_client(dev);
142 unsigned char buf[8];
143 int err;
144
145 if (tm->tm_year < 100)
146 return -EINVAL;
147
148 buf[ABX8XX_REG_HTH] = 0;
149 buf[ABX8XX_REG_SC] = bin2bcd(tm->tm_sec);
150 buf[ABX8XX_REG_MN] = bin2bcd(tm->tm_min);
151 buf[ABX8XX_REG_HR] = bin2bcd(tm->tm_hour);
152 buf[ABX8XX_REG_DA] = bin2bcd(tm->tm_mday);
153 buf[ABX8XX_REG_MO] = bin2bcd(tm->tm_mon + 1);
154 buf[ABX8XX_REG_YR] = bin2bcd(tm->tm_year - 100);
155 buf[ABX8XX_REG_WD] = tm->tm_wday;
156
157 err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_HTH,
158 sizeof(buf), buf);
159 if (err < 0) {
160 dev_err(&client->dev, "Unable to write to date registers\n");
161 return -EIO;
162 }
163
164 return 0;
165}
166
718a820a
AB
167static irqreturn_t abx80x_handle_irq(int irq, void *dev_id)
168{
169 struct i2c_client *client = dev_id;
170 struct rtc_device *rtc = i2c_get_clientdata(client);
171 int status;
172
173 status = i2c_smbus_read_byte_data(client, ABX8XX_REG_STATUS);
174 if (status < 0)
175 return IRQ_NONE;
176
177 if (status & ABX8XX_STATUS_AF)
178 rtc_update_irq(rtc, 1, RTC_AF | RTC_IRQF);
179
180 i2c_smbus_write_byte_data(client, ABX8XX_REG_STATUS, 0);
181
182 return IRQ_HANDLED;
183}
184
185static int abx80x_read_alarm(struct device *dev, struct rtc_wkalrm *t)
186{
187 struct i2c_client *client = to_i2c_client(dev);
188 unsigned char buf[7];
189
190 int irq_mask, err;
191
192 if (client->irq <= 0)
193 return -EINVAL;
194
195 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ASC,
196 sizeof(buf), buf);
197 if (err)
198 return err;
199
200 irq_mask = i2c_smbus_read_byte_data(client, ABX8XX_REG_IRQ);
201 if (irq_mask < 0)
202 return irq_mask;
203
204 t->time.tm_sec = bcd2bin(buf[0] & 0x7F);
205 t->time.tm_min = bcd2bin(buf[1] & 0x7F);
206 t->time.tm_hour = bcd2bin(buf[2] & 0x3F);
207 t->time.tm_mday = bcd2bin(buf[3] & 0x3F);
208 t->time.tm_mon = bcd2bin(buf[4] & 0x1F) - 1;
209 t->time.tm_wday = buf[5] & 0x7;
210
211 t->enabled = !!(irq_mask & ABX8XX_IRQ_AIE);
212 t->pending = (buf[6] & ABX8XX_STATUS_AF) && t->enabled;
213
214 return err;
215}
216
217static int abx80x_set_alarm(struct device *dev, struct rtc_wkalrm *t)
218{
219 struct i2c_client *client = to_i2c_client(dev);
220 u8 alarm[6];
221 int err;
222
223 if (client->irq <= 0)
224 return -EINVAL;
225
226 alarm[0] = 0x0;
227 alarm[1] = bin2bcd(t->time.tm_sec);
228 alarm[2] = bin2bcd(t->time.tm_min);
229 alarm[3] = bin2bcd(t->time.tm_hour);
230 alarm[4] = bin2bcd(t->time.tm_mday);
231 alarm[5] = bin2bcd(t->time.tm_mon + 1);
232
233 err = i2c_smbus_write_i2c_block_data(client, ABX8XX_REG_AHTH,
234 sizeof(alarm), alarm);
235 if (err < 0) {
236 dev_err(&client->dev, "Unable to write alarm registers\n");
237 return -EIO;
238 }
239
240 if (t->enabled) {
241 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
242 (ABX8XX_IRQ_IM_1_4 |
243 ABX8XX_IRQ_AIE));
244 if (err)
245 return err;
246 }
247
248 return 0;
249}
250
251static int abx80x_alarm_irq_enable(struct device *dev, unsigned int enabled)
252{
253 struct i2c_client *client = to_i2c_client(dev);
254 int err;
255
256 if (enabled)
257 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
258 (ABX8XX_IRQ_IM_1_4 |
259 ABX8XX_IRQ_AIE));
260 else
261 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_IRQ,
262 ABX8XX_IRQ_IM_1_4);
263 return err;
264}
265
4d61ff6b
PDM
266static const struct rtc_class_ops abx80x_rtc_ops = {
267 .read_time = abx80x_rtc_read_time,
268 .set_time = abx80x_rtc_set_time,
718a820a
AB
269 .read_alarm = abx80x_read_alarm,
270 .set_alarm = abx80x_set_alarm,
271 .alarm_irq_enable = abx80x_alarm_irq_enable,
4d61ff6b
PDM
272};
273
274static int abx80x_dt_trickle_cfg(struct device_node *np)
275{
276 const char *diode;
277 int trickle_cfg = 0;
278 int i, ret;
279 u32 tmp;
280
281 ret = of_property_read_string(np, "abracon,tc-diode", &diode);
282 if (ret)
283 return ret;
284
285 if (!strcmp(diode, "standard"))
286 trickle_cfg |= ABX8XX_TRICKLE_STANDARD_DIODE;
287 else if (!strcmp(diode, "schottky"))
288 trickle_cfg |= ABX8XX_TRICKLE_SCHOTTKY_DIODE;
289 else
290 return -EINVAL;
291
292 ret = of_property_read_u32(np, "abracon,tc-resistor", &tmp);
293 if (ret)
294 return ret;
295
296 for (i = 0; i < sizeof(trickle_resistors); i++)
297 if (trickle_resistors[i] == tmp)
298 break;
299
300 if (i == sizeof(trickle_resistors))
301 return -EINVAL;
302
303 return (trickle_cfg | i);
304}
305
306static int abx80x_probe(struct i2c_client *client,
307 const struct i2c_device_id *id)
308{
309 struct device_node *np = client->dev.of_node;
310 struct rtc_device *rtc;
311 int i, data, err, trickle_cfg = -EINVAL;
312 char buf[7];
313 unsigned int part = id->driver_data;
314 unsigned int partnumber;
315 unsigned int majrev, minrev;
316 unsigned int lot;
317 unsigned int wafer;
318 unsigned int uid;
319
320 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
321 return -ENODEV;
322
323 err = i2c_smbus_read_i2c_block_data(client, ABX8XX_REG_ID0,
324 sizeof(buf), buf);
325 if (err < 0) {
326 dev_err(&client->dev, "Unable to read partnumber\n");
327 return -EIO;
328 }
329
330 partnumber = (buf[0] << 8) | buf[1];
331 majrev = buf[2] >> 3;
332 minrev = buf[2] & 0x7;
333 lot = ((buf[4] & 0x80) << 2) | ((buf[6] & 0x80) << 1) | buf[3];
334 uid = ((buf[4] & 0x7f) << 8) | buf[5];
335 wafer = (buf[6] & 0x7c) >> 2;
336 dev_info(&client->dev, "model %04x, revision %u.%u, lot %x, wafer %x, uid %x\n",
337 partnumber, majrev, minrev, lot, wafer, uid);
338
339 data = i2c_smbus_read_byte_data(client, ABX8XX_REG_CTRL1);
340 if (data < 0) {
341 dev_err(&client->dev, "Unable to read control register\n");
342 return -EIO;
343 }
344
345 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CTRL1,
718a820a
AB
346 ((data & ~(ABX8XX_CTRL_12_24 |
347 ABX8XX_CTRL_ARST)) |
4d61ff6b
PDM
348 ABX8XX_CTRL_WRITE));
349 if (err < 0) {
350 dev_err(&client->dev, "Unable to write control register\n");
351 return -EIO;
352 }
353
354 /* part autodetection */
355 if (part == ABX80X) {
356 for (i = 0; abx80x_caps[i].pn; i++)
357 if (partnumber == abx80x_caps[i].pn)
358 break;
359 if (abx80x_caps[i].pn == 0) {
360 dev_err(&client->dev, "Unknown part: %04x\n",
361 partnumber);
362 return -EINVAL;
363 }
364 part = i;
365 }
366
367 if (partnumber != abx80x_caps[part].pn) {
368 dev_err(&client->dev, "partnumber mismatch %04x != %04x\n",
369 partnumber, abx80x_caps[part].pn);
370 return -EINVAL;
371 }
372
373 if (np && abx80x_caps[part].has_tc)
374 trickle_cfg = abx80x_dt_trickle_cfg(np);
375
376 if (trickle_cfg > 0) {
377 dev_info(&client->dev, "Enabling trickle charger: %02x\n",
378 trickle_cfg);
379 abx80x_enable_trickle_charger(client, trickle_cfg);
380 }
381
718a820a
AB
382 err = i2c_smbus_write_byte_data(client, ABX8XX_REG_CD_TIMER_CTL,
383 BIT(2));
384 if (err)
385 return err;
386
387 rtc = devm_rtc_device_register(&client->dev, "abx8xx",
4d61ff6b
PDM
388 &abx80x_rtc_ops, THIS_MODULE);
389
390 if (IS_ERR(rtc))
391 return PTR_ERR(rtc);
392
393 i2c_set_clientdata(client, rtc);
394
718a820a
AB
395 if (client->irq > 0) {
396 dev_info(&client->dev, "IRQ %d supplied\n", client->irq);
397 err = devm_request_threaded_irq(&client->dev, client->irq, NULL,
398 abx80x_handle_irq,
399 IRQF_SHARED | IRQF_ONESHOT,
400 "abx8xx",
401 client);
402 if (err) {
403 dev_err(&client->dev, "unable to request IRQ, alarms disabled\n");
404 client->irq = 0;
405 }
406 }
407
4d61ff6b
PDM
408 return 0;
409}
410
411static int abx80x_remove(struct i2c_client *client)
412{
413 return 0;
414}
415
416static const struct i2c_device_id abx80x_id[] = {
417 { "abx80x", ABX80X },
418 { "ab0801", AB0801 },
419 { "ab0803", AB0803 },
420 { "ab0804", AB0804 },
421 { "ab0805", AB0805 },
422 { "ab1801", AB1801 },
423 { "ab1803", AB1803 },
424 { "ab1804", AB1804 },
425 { "ab1805", AB1805 },
fca733a1 426 { "rv1805", AB1805 },
4d61ff6b
PDM
427 { }
428};
429MODULE_DEVICE_TABLE(i2c, abx80x_id);
430
431static struct i2c_driver abx80x_driver = {
432 .driver = {
433 .name = "rtc-abx80x",
434 },
435 .probe = abx80x_probe,
436 .remove = abx80x_remove,
437 .id_table = abx80x_id,
438};
439
440module_i2c_driver(abx80x_driver);
441
442MODULE_AUTHOR("Philippe De Muyter <phdm@macqel.be>");
443MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@free-electrons.com>");
444MODULE_DESCRIPTION("Abracon ABX80X RTC driver");
445MODULE_LICENSE("GPL v2");