Commit | Line | Data |
---|---|---|
18cb6368 RC |
1 | /* |
2 | * An I2C driver for the NXP PCF2127 RTC | |
3 | * Copyright 2013 Til-Technologies | |
4 | * | |
5 | * Author: Renaud Cerrato <r.cerrato@til-technologies.fr> | |
6 | * | |
7 | * based on the other drivers in this same directory. | |
8 | * | |
9 | * http://www.nxp.com/documents/data_sheet/PCF2127AT.pdf | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License version 2 as | |
13 | * published by the Free Software Foundation. | |
14 | */ | |
15 | ||
16 | #include <linux/i2c.h> | |
17 | #include <linux/bcd.h> | |
18 | #include <linux/rtc.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/of.h> | |
22 | ||
23 | #define DRV_VERSION "0.0.1" | |
24 | ||
25 | #define PCF2127_REG_CTRL1 (0x00) /* Control Register 1 */ | |
26 | #define PCF2127_REG_CTRL2 (0x01) /* Control Register 2 */ | |
27 | #define PCF2127_REG_CTRL3 (0x02) /* Control Register 3 */ | |
28 | #define PCF2127_REG_SC (0x03) /* datetime */ | |
29 | #define PCF2127_REG_MN (0x04) | |
30 | #define PCF2127_REG_HR (0x05) | |
31 | #define PCF2127_REG_DM (0x06) | |
32 | #define PCF2127_REG_DW (0x07) | |
33 | #define PCF2127_REG_MO (0x08) | |
34 | #define PCF2127_REG_YR (0x09) | |
35 | ||
653ebd75 AS |
36 | #define PCF2127_OSF BIT(7) /* Oscillator Fail flag */ |
37 | ||
18cb6368 RC |
38 | static struct i2c_driver pcf2127_driver; |
39 | ||
40 | struct pcf2127 { | |
41 | struct rtc_device *rtc; | |
42 | int voltage_low; /* indicates if a low_voltage was detected */ | |
653ebd75 | 43 | int oscillator_failed; /* OSF was detected and date is unreliable */ |
18cb6368 RC |
44 | }; |
45 | ||
46 | /* | |
47 | * In the routines that deal directly with the pcf2127 hardware, we use | |
48 | * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch. | |
49 | */ | |
50 | static int pcf2127_get_datetime(struct i2c_client *client, struct rtc_time *tm) | |
51 | { | |
52 | struct pcf2127 *pcf2127 = i2c_get_clientdata(client); | |
53 | unsigned char buf[10] = { PCF2127_REG_CTRL1 }; | |
54 | ||
55 | /* read registers */ | |
56 | if (i2c_master_send(client, buf, 1) != 1 || | |
57 | i2c_master_recv(client, buf, sizeof(buf)) != sizeof(buf)) { | |
58 | dev_err(&client->dev, "%s: read error\n", __func__); | |
59 | return -EIO; | |
60 | } | |
61 | ||
62 | if (buf[PCF2127_REG_CTRL3] & 0x04) { | |
63 | pcf2127->voltage_low = 1; | |
64 | dev_info(&client->dev, | |
653ebd75 AS |
65 | "low voltage detected, check/replace RTC battery.\n"); |
66 | } | |
67 | ||
68 | if (buf[PCF2127_REG_SC] & PCF2127_OSF) { | |
69 | /* | |
70 | * no need clear the flag here, | |
71 | * it will be cleared once the new date is saved | |
72 | */ | |
73 | pcf2127->oscillator_failed = 1; | |
74 | dev_warn(&client->dev, | |
75 | "oscillator stop detected, date/time is not reliable\n"); | |
76 | return -EINVAL; | |
18cb6368 RC |
77 | } |
78 | ||
79 | dev_dbg(&client->dev, | |
80 | "%s: raw data is cr1=%02x, cr2=%02x, cr3=%02x, " | |
81 | "sec=%02x, min=%02x, hr=%02x, " | |
82 | "mday=%02x, wday=%02x, mon=%02x, year=%02x\n", | |
83 | __func__, | |
84 | buf[0], buf[1], buf[2], | |
85 | buf[3], buf[4], buf[5], | |
86 | buf[6], buf[7], buf[8], buf[9]); | |
87 | ||
88 | ||
89 | tm->tm_sec = bcd2bin(buf[PCF2127_REG_SC] & 0x7F); | |
90 | tm->tm_min = bcd2bin(buf[PCF2127_REG_MN] & 0x7F); | |
91 | tm->tm_hour = bcd2bin(buf[PCF2127_REG_HR] & 0x3F); /* rtc hr 0-23 */ | |
92 | tm->tm_mday = bcd2bin(buf[PCF2127_REG_DM] & 0x3F); | |
93 | tm->tm_wday = buf[PCF2127_REG_DW] & 0x07; | |
94 | tm->tm_mon = bcd2bin(buf[PCF2127_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */ | |
95 | tm->tm_year = bcd2bin(buf[PCF2127_REG_YR]); | |
96 | if (tm->tm_year < 70) | |
97 | tm->tm_year += 100; /* assume we are in 1970...2069 */ | |
98 | ||
99 | dev_dbg(&client->dev, "%s: tm is secs=%d, mins=%d, hours=%d, " | |
100 | "mday=%d, mon=%d, year=%d, wday=%d\n", | |
101 | __func__, | |
102 | tm->tm_sec, tm->tm_min, tm->tm_hour, | |
103 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | |
104 | ||
821f51c4 | 105 | return rtc_valid_tm(tm); |
18cb6368 RC |
106 | } |
107 | ||
108 | static int pcf2127_set_datetime(struct i2c_client *client, struct rtc_time *tm) | |
109 | { | |
653ebd75 | 110 | struct pcf2127 *pcf2127 = i2c_get_clientdata(client); |
18cb6368 RC |
111 | unsigned char buf[8]; |
112 | int i = 0, err; | |
113 | ||
114 | dev_dbg(&client->dev, "%s: secs=%d, mins=%d, hours=%d, " | |
115 | "mday=%d, mon=%d, year=%d, wday=%d\n", | |
116 | __func__, | |
117 | tm->tm_sec, tm->tm_min, tm->tm_hour, | |
118 | tm->tm_mday, tm->tm_mon, tm->tm_year, tm->tm_wday); | |
119 | ||
120 | /* start register address */ | |
121 | buf[i++] = PCF2127_REG_SC; | |
122 | ||
123 | /* hours, minutes and seconds */ | |
653ebd75 | 124 | buf[i++] = bin2bcd(tm->tm_sec); /* this will also clear OSF flag */ |
18cb6368 RC |
125 | buf[i++] = bin2bcd(tm->tm_min); |
126 | buf[i++] = bin2bcd(tm->tm_hour); | |
127 | buf[i++] = bin2bcd(tm->tm_mday); | |
128 | buf[i++] = tm->tm_wday & 0x07; | |
129 | ||
130 | /* month, 1 - 12 */ | |
131 | buf[i++] = bin2bcd(tm->tm_mon + 1); | |
132 | ||
133 | /* year */ | |
134 | buf[i++] = bin2bcd(tm->tm_year % 100); | |
135 | ||
136 | /* write register's data */ | |
137 | err = i2c_master_send(client, buf, i); | |
138 | if (err != i) { | |
139 | dev_err(&client->dev, | |
140 | "%s: err=%d", __func__, err); | |
141 | return -EIO; | |
142 | } | |
143 | ||
653ebd75 AS |
144 | /* clear OSF flag in client data */ |
145 | pcf2127->oscillator_failed = 0; | |
146 | ||
18cb6368 RC |
147 | return 0; |
148 | } | |
149 | ||
150 | #ifdef CONFIG_RTC_INTF_DEV | |
151 | static int pcf2127_rtc_ioctl(struct device *dev, | |
152 | unsigned int cmd, unsigned long arg) | |
153 | { | |
154 | struct pcf2127 *pcf2127 = i2c_get_clientdata(to_i2c_client(dev)); | |
155 | ||
156 | switch (cmd) { | |
157 | case RTC_VL_READ: | |
158 | if (pcf2127->voltage_low) | |
653ebd75 AS |
159 | dev_info(dev, "low voltage detected, check/replace battery\n"); |
160 | if (pcf2127->oscillator_failed) | |
161 | dev_info(dev, "oscillator stop detected, date/time is not reliable\n"); | |
18cb6368 RC |
162 | |
163 | if (copy_to_user((void __user *)arg, &pcf2127->voltage_low, | |
164 | sizeof(int))) | |
165 | return -EFAULT; | |
166 | return 0; | |
167 | default: | |
168 | return -ENOIOCTLCMD; | |
169 | } | |
170 | } | |
171 | #else | |
172 | #define pcf2127_rtc_ioctl NULL | |
173 | #endif | |
174 | ||
175 | static int pcf2127_rtc_read_time(struct device *dev, struct rtc_time *tm) | |
176 | { | |
177 | return pcf2127_get_datetime(to_i2c_client(dev), tm); | |
178 | } | |
179 | ||
180 | static int pcf2127_rtc_set_time(struct device *dev, struct rtc_time *tm) | |
181 | { | |
182 | return pcf2127_set_datetime(to_i2c_client(dev), tm); | |
183 | } | |
184 | ||
185 | static const struct rtc_class_ops pcf2127_rtc_ops = { | |
186 | .ioctl = pcf2127_rtc_ioctl, | |
187 | .read_time = pcf2127_rtc_read_time, | |
188 | .set_time = pcf2127_rtc_set_time, | |
189 | }; | |
190 | ||
191 | static int pcf2127_probe(struct i2c_client *client, | |
192 | const struct i2c_device_id *id) | |
193 | { | |
194 | struct pcf2127 *pcf2127; | |
195 | ||
196 | dev_dbg(&client->dev, "%s\n", __func__); | |
197 | ||
198 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) | |
199 | return -ENODEV; | |
200 | ||
201 | pcf2127 = devm_kzalloc(&client->dev, sizeof(struct pcf2127), | |
202 | GFP_KERNEL); | |
203 | if (!pcf2127) | |
204 | return -ENOMEM; | |
205 | ||
206 | dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n"); | |
207 | ||
208 | i2c_set_clientdata(client, pcf2127); | |
209 | ||
210 | pcf2127->rtc = devm_rtc_device_register(&client->dev, | |
211 | pcf2127_driver.driver.name, | |
212 | &pcf2127_rtc_ops, THIS_MODULE); | |
213 | ||
7ab26cd1 | 214 | return PTR_ERR_OR_ZERO(pcf2127->rtc); |
18cb6368 RC |
215 | } |
216 | ||
18cb6368 RC |
217 | static const struct i2c_device_id pcf2127_id[] = { |
218 | { "pcf2127", 0 }, | |
219 | { } | |
220 | }; | |
221 | MODULE_DEVICE_TABLE(i2c, pcf2127_id); | |
222 | ||
223 | #ifdef CONFIG_OF | |
224 | static const struct of_device_id pcf2127_of_match[] = { | |
225 | { .compatible = "nxp,pcf2127" }, | |
226 | {} | |
227 | }; | |
228 | MODULE_DEVICE_TABLE(of, pcf2127_of_match); | |
229 | #endif | |
230 | ||
231 | static struct i2c_driver pcf2127_driver = { | |
232 | .driver = { | |
233 | .name = "rtc-pcf2127", | |
18cb6368 RC |
234 | .of_match_table = of_match_ptr(pcf2127_of_match), |
235 | }, | |
236 | .probe = pcf2127_probe, | |
18cb6368 RC |
237 | .id_table = pcf2127_id, |
238 | }; | |
239 | ||
240 | module_i2c_driver(pcf2127_driver); | |
241 | ||
242 | MODULE_AUTHOR("Renaud Cerrato <r.cerrato@til-technologies.fr>"); | |
243 | MODULE_DESCRIPTION("NXP PCF2127 RTC driver"); | |
244 | MODULE_LICENSE("GPL"); | |
245 | MODULE_VERSION(DRV_VERSION); |