2 * An I2C driver for the PCF85063 RTC
3 * Copyright 2014 Rose Technology
5 * Author: Søren Andersen <san@rosetechnology.dk>
6 * Maintainers: http://www.nslu2-linux.org/
8 * based on the other drivers in this same directory.
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.
14 #include <linux/i2c.h>
15 #include <linux/bcd.h>
16 #include <linux/rtc.h>
17 #include <linux/module.h>
19 #define DRV_VERSION "0.0.1"
21 #define PCF85063_REG_CTRL1 0x00 /* status */
22 #define PCF85063_REG_CTRL1_STOP BIT(5)
23 #define PCF85063_REG_CTRL2 0x01
25 #define PCF85063_REG_SC 0x04 /* datetime */
26 #define PCF85063_REG_SC_OS 0x80
27 #define PCF85063_REG_MN 0x05
28 #define PCF85063_REG_HR 0x06
29 #define PCF85063_REG_DM 0x07
30 #define PCF85063_REG_DW 0x08
31 #define PCF85063_REG_MO 0x09
32 #define PCF85063_REG_YR 0x0A
34 #define PCF85063_MO_C 0x80 /* century */
36 static struct i2c_driver pcf85063_driver
;
39 struct rtc_device
*rtc
;
40 int c_polarity
; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */
41 int voltage_low
; /* indicates if a low_voltage was detected */
44 static int pcf85063_stop_clock(struct i2c_client
*client
, u8
*ctrl1
)
48 ret
= i2c_smbus_read_byte_data(client
, PCF85063_REG_CTRL1
);
50 dev_err(&client
->dev
, "Failing to stop the clock\n");
55 ret
|= PCF85063_REG_CTRL1_STOP
;
57 ret
= i2c_smbus_write_byte_data(client
, PCF85063_REG_CTRL1
, ret
);
59 dev_err(&client
->dev
, "Failing to stop the clock\n");
69 * In the routines that deal directly with the pcf85063 hardware, we use
70 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
72 static int pcf85063_get_datetime(struct i2c_client
*client
, struct rtc_time
*tm
)
75 struct pcf85063
*pcf85063
= i2c_get_clientdata(client
);
79 * while reading, the time/date registers are blocked and not updated
80 * anymore until the access is finished. To not lose a second
81 * event, the access must be finished within one second. So, read all
82 * time/date registers in one turn.
84 rc
= i2c_smbus_read_i2c_block_data(client
, PCF85063_REG_SC
,
86 if (rc
!= sizeof(regs
)) {
87 dev_err(&client
->dev
, "date/time register read error\n");
91 /* if the clock has lost its power it makes no sense to use its time */
92 if (regs
[0] & PCF85063_REG_SC_OS
) {
93 dev_warn(&client
->dev
, "Power loss detected, invalid time\n");
97 tm
->tm_sec
= bcd2bin(regs
[0] & 0x7F);
98 tm
->tm_min
= bcd2bin(regs
[1] & 0x7F);
99 tm
->tm_hour
= bcd2bin(regs
[2] & 0x3F); /* rtc hr 0-23 */
100 tm
->tm_mday
= bcd2bin(regs
[3] & 0x3F);
101 tm
->tm_wday
= regs
[4] & 0x07;
102 tm
->tm_mon
= bcd2bin(regs
[5] & 0x1F) - 1; /* rtc mn 1-12 */
103 tm
->tm_year
= bcd2bin(regs
[6]);
104 if (tm
->tm_year
< 70)
105 tm
->tm_year
+= 100; /* assume we are in 1970...2069 */
106 /* detect the polarity heuristically. see note above. */
107 pcf85063
->c_polarity
= (regs
[5] & PCF85063_MO_C
) ?
108 (tm
->tm_year
>= 100) : (tm
->tm_year
< 100);
110 return rtc_valid_tm(tm
);
113 static int pcf85063_set_datetime(struct i2c_client
*client
, struct rtc_time
*tm
)
119 * to accurately set the time, reset the divider chain and keep it in
120 * reset state until all time/date registers are written
122 rc
= pcf85063_stop_clock(client
, ®s
[7]);
126 /* hours, minutes and seconds */
127 regs
[0] = bin2bcd(tm
->tm_sec
) & 0x7F; /* clear OS flag */
129 regs
[1] = bin2bcd(tm
->tm_min
);
130 regs
[2] = bin2bcd(tm
->tm_hour
);
132 /* Day of month, 1 - 31 */
133 regs
[3] = bin2bcd(tm
->tm_mday
);
136 regs
[4] = tm
->tm_wday
& 0x07;
139 regs
[5] = bin2bcd(tm
->tm_mon
+ 1);
141 /* year and century */
142 regs
[6] = bin2bcd(tm
->tm_year
% 100);
145 * after all time/date registers are written, let the 'address auto
146 * increment' feature wrap around and write register CTRL1 to re-enable
147 * the clock divider chain again
149 regs
[7] &= ~PCF85063_REG_CTRL1_STOP
;
151 /* write all registers at once */
152 rc
= i2c_smbus_write_i2c_block_data(client
, PCF85063_REG_SC
,
155 dev_err(&client
->dev
, "date/time register write error\n");
162 static int pcf85063_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
164 return pcf85063_get_datetime(to_i2c_client(dev
), tm
);
167 static int pcf85063_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
169 return pcf85063_set_datetime(to_i2c_client(dev
), tm
);
172 static const struct rtc_class_ops pcf85063_rtc_ops
= {
173 .read_time
= pcf85063_rtc_read_time
,
174 .set_time
= pcf85063_rtc_set_time
177 static int pcf85063_probe(struct i2c_client
*client
,
178 const struct i2c_device_id
*id
)
180 struct pcf85063
*pcf85063
;
182 dev_dbg(&client
->dev
, "%s\n", __func__
);
184 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
187 pcf85063
= devm_kzalloc(&client
->dev
, sizeof(struct pcf85063
),
192 dev_info(&client
->dev
, "chip found, driver version " DRV_VERSION
"\n");
194 i2c_set_clientdata(client
, pcf85063
);
196 pcf85063
->rtc
= devm_rtc_device_register(&client
->dev
,
197 pcf85063_driver
.driver
.name
,
198 &pcf85063_rtc_ops
, THIS_MODULE
);
200 return PTR_ERR_OR_ZERO(pcf85063
->rtc
);
203 static const struct i2c_device_id pcf85063_id
[] = {
207 MODULE_DEVICE_TABLE(i2c
, pcf85063_id
);
210 static const struct of_device_id pcf85063_of_match
[] = {
211 { .compatible
= "nxp,pcf85063" },
214 MODULE_DEVICE_TABLE(of
, pcf85063_of_match
);
217 static struct i2c_driver pcf85063_driver
= {
219 .name
= "rtc-pcf85063",
220 .of_match_table
= of_match_ptr(pcf85063_of_match
),
222 .probe
= pcf85063_probe
,
223 .id_table
= pcf85063_id
,
226 module_i2c_driver(pcf85063_driver
);
228 MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
229 MODULE_DESCRIPTION("PCF85063 RTC driver");
230 MODULE_LICENSE("GPL");
231 MODULE_VERSION(DRV_VERSION
);