1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/i2c.h>
15#include <linux/bcd.h>
16#include <linux/rtc.h>
17#include <linux/module.h>
18
19
20
21
22
23
24
25
26
27
28
29#define PCF85063_REG_CTRL1 0x00
30#define PCF85063_REG_CTRL1_STOP BIT(5)
31#define PCF85063_REG_CTRL2 0x01
32
33#define PCF85063_REG_SC 0x04
34#define PCF85063_REG_SC_OS 0x80
35#define PCF85063_REG_MN 0x05
36#define PCF85063_REG_HR 0x06
37#define PCF85063_REG_DM 0x07
38#define PCF85063_REG_DW 0x08
39#define PCF85063_REG_MO 0x09
40#define PCF85063_REG_YR 0x0A
41
42static struct i2c_driver pcf85063_driver;
43
44static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
45{
46 s32 ret;
47
48 ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
49 if (ret < 0) {
50 dev_err(&client->dev, "Failing to stop the clock\n");
51 return -EIO;
52 }
53
54
55 ret |= PCF85063_REG_CTRL1_STOP;
56
57 ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret);
58 if (ret < 0) {
59 dev_err(&client->dev, "Failing to stop the clock\n");
60 return -EIO;
61 }
62
63 *ctrl1 = ret;
64
65 return 0;
66}
67
68static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1)
69{
70 s32 ret;
71
72
73 ctrl1 &= PCF85063_REG_CTRL1_STOP;
74
75 ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ctrl1);
76 if (ret < 0) {
77 dev_err(&client->dev, "Failing to start the clock\n");
78 return -EIO;
79 }
80
81 return 0;
82}
83
84static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
85{
86 int rc;
87 u8 regs[7];
88
89
90
91
92
93
94
95 rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
96 sizeof(regs), regs);
97 if (rc != sizeof(regs)) {
98 dev_err(&client->dev, "date/time register read error\n");
99 return -EIO;
100 }
101
102
103 if (regs[0] & PCF85063_REG_SC_OS) {
104 dev_warn(&client->dev, "Power loss detected, invalid time\n");
105 return -EINVAL;
106 }
107
108 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
109 tm->tm_min = bcd2bin(regs[1] & 0x7F);
110 tm->tm_hour = bcd2bin(regs[2] & 0x3F);
111 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
112 tm->tm_wday = regs[4] & 0x07;
113 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1;
114 tm->tm_year = bcd2bin(regs[6]);
115 tm->tm_year += 100;
116
117 return rtc_valid_tm(tm);
118}
119
120static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
121{
122 int rc;
123 u8 regs[7];
124 u8 ctrl1;
125
126 if ((tm->tm_year < 100) || (tm->tm_year > 199))
127 return -EINVAL;
128
129
130
131
132
133 rc = pcf85063_stop_clock(client, &ctrl1);
134 if (rc != 0)
135 return rc;
136
137
138 regs[0] = bin2bcd(tm->tm_sec) & 0x7F;
139
140 regs[1] = bin2bcd(tm->tm_min);
141 regs[2] = bin2bcd(tm->tm_hour);
142
143
144 regs[3] = bin2bcd(tm->tm_mday);
145
146
147 regs[4] = tm->tm_wday & 0x07;
148
149
150 regs[5] = bin2bcd(tm->tm_mon + 1);
151
152
153 regs[6] = bin2bcd(tm->tm_year - 100);
154
155
156 rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
157 sizeof(regs), regs);
158 if (rc < 0) {
159 dev_err(&client->dev, "date/time register write error\n");
160 return rc;
161 }
162
163
164
165
166
167
168 rc = pcf85063_start_clock(client, ctrl1);
169 if (rc != 0)
170 return rc;
171
172 return 0;
173}
174
175static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
176{
177 return pcf85063_get_datetime(to_i2c_client(dev), tm);
178}
179
180static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
181{
182 return pcf85063_set_datetime(to_i2c_client(dev), tm);
183}
184
185static const struct rtc_class_ops pcf85063_rtc_ops = {
186 .read_time = pcf85063_rtc_read_time,
187 .set_time = pcf85063_rtc_set_time
188};
189
190static int pcf85063_probe(struct i2c_client *client,
191 const struct i2c_device_id *id)
192{
193 struct rtc_device *rtc;
194
195 dev_dbg(&client->dev, "%s\n", __func__);
196
197 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
198 return -ENODEV;
199
200 rtc = devm_rtc_device_register(&client->dev,
201 pcf85063_driver.driver.name,
202 &pcf85063_rtc_ops, THIS_MODULE);
203
204 return PTR_ERR_OR_ZERO(rtc);
205}
206
207static const struct i2c_device_id pcf85063_id[] = {
208 { "pcf85063", 0 },
209 { }
210};
211MODULE_DEVICE_TABLE(i2c, pcf85063_id);
212
213#ifdef CONFIG_OF
214static const struct of_device_id pcf85063_of_match[] = {
215 { .compatible = "nxp,pcf85063" },
216 {}
217};
218MODULE_DEVICE_TABLE(of, pcf85063_of_match);
219#endif
220
221static struct i2c_driver pcf85063_driver = {
222 .driver = {
223 .name = "rtc-pcf85063",
224 .of_match_table = of_match_ptr(pcf85063_of_match),
225 },
226 .probe = pcf85063_probe,
227 .id_table = pcf85063_id,
228};
229
230module_i2c_driver(pcf85063_driver);
231
232MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
233MODULE_DESCRIPTION("PCF85063 RTC driver");
234MODULE_LICENSE("GPL");
235