2 * Copyright (C) 2012 Avionic Design GmbH
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/rtc.h>
15 #define DRIVER_NAME "rtc-pcf8523"
17 #define REG_CONTROL1 0x00
18 #define REG_CONTROL1_CAP_SEL (1 << 7)
19 #define REG_CONTROL1_STOP (1 << 5)
21 #define REG_CONTROL3 0x02
22 #define REG_CONTROL3_PM_BLD (1 << 7) /* battery low detection disabled */
23 #define REG_CONTROL3_PM_VDD (1 << 6) /* switch-over disabled */
24 #define REG_CONTROL3_PM_DSM (1 << 5) /* direct switching mode */
25 #define REG_CONTROL3_PM_MASK 0xe0
26 #define REG_CONTROL3_BLF (1 << 2) /* battery low bit, read-only */
28 #define REG_SECONDS 0x03
29 #define REG_SECONDS_OS (1 << 7)
31 #define REG_MINUTES 0x04
32 #define REG_HOURS 0x05
34 #define REG_WEEKDAYS 0x07
35 #define REG_MONTHS 0x08
36 #define REG_YEARS 0x09
38 #define REG_OFFSET 0x0e
39 #define REG_OFFSET_MODE BIT(7)
42 struct rtc_device
*rtc
;
45 static int pcf8523_read(struct i2c_client
*client
, u8 reg
, u8
*valuep
)
47 struct i2c_msg msgs
[2];
51 msgs
[0].addr
= client
->addr
;
53 msgs
[0].len
= sizeof(reg
);
56 msgs
[1].addr
= client
->addr
;
57 msgs
[1].flags
= I2C_M_RD
;
58 msgs
[1].len
= sizeof(value
);
61 err
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
70 static int pcf8523_write(struct i2c_client
*client
, u8 reg
, u8 value
)
72 u8 buffer
[2] = { reg
, value
};
76 msg
.addr
= client
->addr
;
78 msg
.len
= sizeof(buffer
);
81 err
= i2c_transfer(client
->adapter
, &msg
, 1);
88 static int pcf8523_voltage_low(struct i2c_client
*client
)
93 err
= pcf8523_read(client
, REG_CONTROL3
, &value
);
97 return !!(value
& REG_CONTROL3_BLF
);
100 static int pcf8523_select_capacitance(struct i2c_client
*client
, bool high
)
105 err
= pcf8523_read(client
, REG_CONTROL1
, &value
);
110 value
&= ~REG_CONTROL1_CAP_SEL
;
112 value
|= REG_CONTROL1_CAP_SEL
;
114 err
= pcf8523_write(client
, REG_CONTROL1
, value
);
121 static int pcf8523_set_pm(struct i2c_client
*client
, u8 pm
)
126 err
= pcf8523_read(client
, REG_CONTROL3
, &value
);
130 value
= (value
& ~REG_CONTROL3_PM_MASK
) | pm
;
132 err
= pcf8523_write(client
, REG_CONTROL3
, value
);
139 static int pcf8523_stop_rtc(struct i2c_client
*client
)
144 err
= pcf8523_read(client
, REG_CONTROL1
, &value
);
148 value
|= REG_CONTROL1_STOP
;
150 err
= pcf8523_write(client
, REG_CONTROL1
, value
);
157 static int pcf8523_start_rtc(struct i2c_client
*client
)
162 err
= pcf8523_read(client
, REG_CONTROL1
, &value
);
166 value
&= ~REG_CONTROL1_STOP
;
168 err
= pcf8523_write(client
, REG_CONTROL1
, value
);
175 static int pcf8523_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
177 struct i2c_client
*client
= to_i2c_client(dev
);
178 u8 start
= REG_SECONDS
, regs
[7];
179 struct i2c_msg msgs
[2];
182 err
= pcf8523_voltage_low(client
);
185 } else if (err
> 0) {
186 dev_err(dev
, "low voltage detected, time is unreliable\n");
190 msgs
[0].addr
= client
->addr
;
193 msgs
[0].buf
= &start
;
195 msgs
[1].addr
= client
->addr
;
196 msgs
[1].flags
= I2C_M_RD
;
197 msgs
[1].len
= sizeof(regs
);
200 err
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
204 if (regs
[0] & REG_SECONDS_OS
)
207 tm
->tm_sec
= bcd2bin(regs
[0] & 0x7f);
208 tm
->tm_min
= bcd2bin(regs
[1] & 0x7f);
209 tm
->tm_hour
= bcd2bin(regs
[2] & 0x3f);
210 tm
->tm_mday
= bcd2bin(regs
[3] & 0x3f);
211 tm
->tm_wday
= regs
[4] & 0x7;
212 tm
->tm_mon
= bcd2bin(regs
[5] & 0x1f) - 1;
213 tm
->tm_year
= bcd2bin(regs
[6]) + 100;
218 static int pcf8523_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
220 struct i2c_client
*client
= to_i2c_client(dev
);
226 * The hardware can only store values between 0 and 99 in it's YEAR
227 * register (with 99 overflowing to 0 on increment).
228 * After 2100-02-28 we could start interpreting the year to be in the
229 * interval [2100, 2199], but there is no path to switch in a smooth way
230 * because the chip handles YEAR=0x00 (and the out-of-spec
231 * YEAR=0xa0) as a leap year, but 2100 isn't.
233 if (tm
->tm_year
< 100 || tm
->tm_year
>= 200)
236 err
= pcf8523_stop_rtc(client
);
240 regs
[0] = REG_SECONDS
;
241 /* This will purposely overwrite REG_SECONDS_OS */
242 regs
[1] = bin2bcd(tm
->tm_sec
);
243 regs
[2] = bin2bcd(tm
->tm_min
);
244 regs
[3] = bin2bcd(tm
->tm_hour
);
245 regs
[4] = bin2bcd(tm
->tm_mday
);
246 regs
[5] = tm
->tm_wday
;
247 regs
[6] = bin2bcd(tm
->tm_mon
+ 1);
248 regs
[7] = bin2bcd(tm
->tm_year
- 100);
250 msg
.addr
= client
->addr
;
252 msg
.len
= sizeof(regs
);
255 err
= i2c_transfer(client
->adapter
, &msg
, 1);
258 * If the time cannot be set, restart the RTC anyway. Note
259 * that errors are ignored if the RTC cannot be started so
260 * that we have a chance to propagate the original error.
262 pcf8523_start_rtc(client
);
266 return pcf8523_start_rtc(client
);
269 #ifdef CONFIG_RTC_INTF_DEV
270 static int pcf8523_rtc_ioctl(struct device
*dev
, unsigned int cmd
,
273 struct i2c_client
*client
= to_i2c_client(dev
);
278 ret
= pcf8523_voltage_low(client
);
282 if (copy_to_user((void __user
*)arg
, &ret
, sizeof(int)))
291 #define pcf8523_rtc_ioctl NULL
294 static int pcf8523_rtc_read_offset(struct device
*dev
, long *offset
)
296 struct i2c_client
*client
= to_i2c_client(dev
);
301 err
= pcf8523_read(client
, REG_OFFSET
, &value
);
305 /* sign extend the 7-bit offset value */
307 *offset
= (value
& REG_OFFSET_MODE
? 4069 : 4340) * (val
>> 1);
312 static int pcf8523_rtc_set_offset(struct device
*dev
, long offset
)
314 struct i2c_client
*client
= to_i2c_client(dev
);
318 reg_m0
= clamp(DIV_ROUND_CLOSEST(offset
, 4340), -64L, 63L);
319 reg_m1
= clamp(DIV_ROUND_CLOSEST(offset
, 4069), -64L, 63L);
321 if (abs(reg_m0
* 4340 - offset
) < abs(reg_m1
* 4069 - offset
))
322 value
= reg_m0
& 0x7f;
324 value
= (reg_m1
& 0x7f) | REG_OFFSET_MODE
;
326 return pcf8523_write(client
, REG_OFFSET
, value
);
329 static const struct rtc_class_ops pcf8523_rtc_ops
= {
330 .read_time
= pcf8523_rtc_read_time
,
331 .set_time
= pcf8523_rtc_set_time
,
332 .ioctl
= pcf8523_rtc_ioctl
,
333 .read_offset
= pcf8523_rtc_read_offset
,
334 .set_offset
= pcf8523_rtc_set_offset
,
337 static int pcf8523_probe(struct i2c_client
*client
,
338 const struct i2c_device_id
*id
)
343 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
346 pcf
= devm_kzalloc(&client
->dev
, sizeof(*pcf
), GFP_KERNEL
);
350 err
= pcf8523_select_capacitance(client
, true);
354 err
= pcf8523_set_pm(client
, 0);
358 pcf
->rtc
= devm_rtc_device_register(&client
->dev
, DRIVER_NAME
,
359 &pcf8523_rtc_ops
, THIS_MODULE
);
360 if (IS_ERR(pcf
->rtc
))
361 return PTR_ERR(pcf
->rtc
);
363 i2c_set_clientdata(client
, pcf
);
368 static const struct i2c_device_id pcf8523_id
[] = {
372 MODULE_DEVICE_TABLE(i2c
, pcf8523_id
);
375 static const struct of_device_id pcf8523_of_match
[] = {
376 { .compatible
= "nxp,pcf8523" },
379 MODULE_DEVICE_TABLE(of
, pcf8523_of_match
);
382 static struct i2c_driver pcf8523_driver
= {
385 .of_match_table
= of_match_ptr(pcf8523_of_match
),
387 .probe
= pcf8523_probe
,
388 .id_table
= pcf8523_id
,
390 module_i2c_driver(pcf8523_driver
);
392 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
393 MODULE_DESCRIPTION("NXP PCF8523 RTC driver");
394 MODULE_LICENSE("GPL v2");