550f67518c5af355e9ddbf7530a72cb902ef8416
[cascardo/linux.git] / drivers / rtc / rtc-pcf85063.c
1 /*
2  * An I2C driver for the PCF85063 RTC
3  * Copyright 2014 Rose Technology
4  *
5  * Author: Søren Andersen <san@rosetechnology.dk>
6  * Maintainers: http://www.nslu2-linux.org/
7  *
8  * based on the other drivers in this same directory.
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 #include <linux/i2c.h>
15 #include <linux/bcd.h>
16 #include <linux/rtc.h>
17 #include <linux/module.h>
18
19 #define PCF85063_REG_CTRL1              0x00 /* status */
20 #define PCF85063_REG_CTRL1_STOP         BIT(5)
21 #define PCF85063_REG_CTRL2              0x01
22
23 #define PCF85063_REG_SC                 0x04 /* datetime */
24 #define PCF85063_REG_SC_OS              0x80
25 #define PCF85063_REG_MN                 0x05
26 #define PCF85063_REG_HR                 0x06
27 #define PCF85063_REG_DM                 0x07
28 #define PCF85063_REG_DW                 0x08
29 #define PCF85063_REG_MO                 0x09
30 #define PCF85063_REG_YR                 0x0A
31
32 static struct i2c_driver pcf85063_driver;
33
34 struct pcf85063 {
35         struct rtc_device *rtc;
36         int voltage_low; /* indicates if a low_voltage was detected */
37 };
38
39 static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
40 {
41         s32 ret;
42
43         ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
44         if (ret < 0) {
45                 dev_err(&client->dev, "Failing to stop the clock\n");
46                 return -EIO;
47         }
48
49         /* stop the clock */
50         ret |= PCF85063_REG_CTRL1_STOP;
51
52         ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret);
53         if (ret < 0) {
54                 dev_err(&client->dev, "Failing to stop the clock\n");
55                 return -EIO;
56         }
57
58         *ctrl1 = ret;
59
60         return 0;
61 }
62
63 /*
64  * In the routines that deal directly with the pcf85063 hardware, we use
65  * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
66  */
67 static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
68 {
69         int rc;
70         u8 regs[7];
71
72         /*
73          * while reading, the time/date registers are blocked and not updated
74          * anymore until the access is finished. To not lose a second
75          * event, the access must be finished within one second. So, read all
76          * time/date registers in one turn.
77          */
78         rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
79                                            sizeof(regs), regs);
80         if (rc != sizeof(regs)) {
81                 dev_err(&client->dev, "date/time register read error\n");
82                 return -EIO;
83         }
84
85         /* if the clock has lost its power it makes no sense to use its time */
86         if (regs[0] & PCF85063_REG_SC_OS) {
87                 dev_warn(&client->dev, "Power loss detected, invalid time\n");
88                 return -EINVAL;
89         }
90
91         tm->tm_sec = bcd2bin(regs[0] & 0x7F);
92         tm->tm_min = bcd2bin(regs[1] & 0x7F);
93         tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
94         tm->tm_mday = bcd2bin(regs[3] & 0x3F);
95         tm->tm_wday = regs[4] & 0x07;
96         tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
97         tm->tm_year = bcd2bin(regs[6]);
98         if (tm->tm_year < 70)
99                 tm->tm_year += 100;     /* assume we are in 1970...2069 */
100
101         return rtc_valid_tm(tm);
102 }
103
104 static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
105 {
106         int rc;
107         u8 regs[8];
108
109         /*
110          * to accurately set the time, reset the divider chain and keep it in
111          * reset state until all time/date registers are written
112          */
113         rc = pcf85063_stop_clock(client, &regs[7]);
114         if (rc != 0)
115                 return rc;
116
117         /* hours, minutes and seconds */
118         regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
119
120         regs[1] = bin2bcd(tm->tm_min);
121         regs[2] = bin2bcd(tm->tm_hour);
122
123         /* Day of month, 1 - 31 */
124         regs[3] = bin2bcd(tm->tm_mday);
125
126         /* Day, 0 - 6 */
127         regs[4] = tm->tm_wday & 0x07;
128
129         /* month, 1 - 12 */
130         regs[5] = bin2bcd(tm->tm_mon + 1);
131
132         /* year and century */
133         regs[6] = bin2bcd(tm->tm_year % 100);
134
135         /*
136          * after all time/date registers are written, let the 'address auto
137          * increment' feature wrap around and write register CTRL1 to re-enable
138          * the clock divider chain again
139          */
140         regs[7] &= ~PCF85063_REG_CTRL1_STOP;
141
142         /* write all registers at once */
143         rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
144                                             sizeof(regs), regs);
145         if (rc < 0) {
146                 dev_err(&client->dev, "date/time register write error\n");
147                 return rc;
148         }
149
150         return 0;
151 }
152
153 static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
154 {
155         return pcf85063_get_datetime(to_i2c_client(dev), tm);
156 }
157
158 static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
159 {
160         return pcf85063_set_datetime(to_i2c_client(dev), tm);
161 }
162
163 static const struct rtc_class_ops pcf85063_rtc_ops = {
164         .read_time      = pcf85063_rtc_read_time,
165         .set_time       = pcf85063_rtc_set_time
166 };
167
168 static int pcf85063_probe(struct i2c_client *client,
169                                 const struct i2c_device_id *id)
170 {
171         struct pcf85063 *pcf85063;
172
173         dev_dbg(&client->dev, "%s\n", __func__);
174
175         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
176                 return -ENODEV;
177
178         pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
179                                 GFP_KERNEL);
180         if (!pcf85063)
181                 return -ENOMEM;
182
183         i2c_set_clientdata(client, pcf85063);
184
185         pcf85063->rtc = devm_rtc_device_register(&client->dev,
186                                 pcf85063_driver.driver.name,
187                                 &pcf85063_rtc_ops, THIS_MODULE);
188
189         return PTR_ERR_OR_ZERO(pcf85063->rtc);
190 }
191
192 static const struct i2c_device_id pcf85063_id[] = {
193         { "pcf85063", 0 },
194         { }
195 };
196 MODULE_DEVICE_TABLE(i2c, pcf85063_id);
197
198 #ifdef CONFIG_OF
199 static const struct of_device_id pcf85063_of_match[] = {
200         { .compatible = "nxp,pcf85063" },
201         {}
202 };
203 MODULE_DEVICE_TABLE(of, pcf85063_of_match);
204 #endif
205
206 static struct i2c_driver pcf85063_driver = {
207         .driver         = {
208                 .name   = "rtc-pcf85063",
209                 .of_match_table = of_match_ptr(pcf85063_of_match),
210         },
211         .probe          = pcf85063_probe,
212         .id_table       = pcf85063_id,
213 };
214
215 module_i2c_driver(pcf85063_driver);
216
217 MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
218 MODULE_DESCRIPTION("PCF85063 RTC driver");
219 MODULE_LICENSE("GPL");