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_CTRL2 0x01
24 #define PCF85063_REG_SC 0x04 /* datetime */
25 #define PCF85063_REG_SC_OS 0x80
26 #define PCF85063_REG_MN 0x05
27 #define PCF85063_REG_HR 0x06
28 #define PCF85063_REG_DM 0x07
29 #define PCF85063_REG_DW 0x08
30 #define PCF85063_REG_MO 0x09
31 #define PCF85063_REG_YR 0x0A
33 #define PCF85063_MO_C 0x80 /* century */
35 static struct i2c_driver pcf85063_driver
;
38 struct rtc_device
*rtc
;
39 int c_polarity
; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */
40 int voltage_low
; /* indicates if a low_voltage was detected */
44 * In the routines that deal directly with the pcf85063 hardware, we use
45 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
47 static int pcf85063_get_datetime(struct i2c_client
*client
, struct rtc_time
*tm
)
50 struct pcf85063
*pcf85063
= i2c_get_clientdata(client
);
54 * while reading, the time/date registers are blocked and not updated
55 * anymore until the access is finished. To not lose a second
56 * event, the access must be finished within one second. So, read all
57 * time/date registers in one turn.
59 rc
= i2c_smbus_read_i2c_block_data(client
, PCF85063_REG_SC
,
61 if (rc
!= sizeof(regs
)) {
62 dev_err(&client
->dev
, "date/time register read error\n");
66 /* if the clock has lost its power it makes no sense to use its time */
67 if (regs
[0] & PCF85063_REG_SC_OS
) {
68 dev_warn(&client
->dev
, "Power loss detected, invalid time\n");
72 tm
->tm_sec
= bcd2bin(regs
[0] & 0x7F);
73 tm
->tm_min
= bcd2bin(regs
[1] & 0x7F);
74 tm
->tm_hour
= bcd2bin(regs
[2] & 0x3F); /* rtc hr 0-23 */
75 tm
->tm_mday
= bcd2bin(regs
[3] & 0x3F);
76 tm
->tm_wday
= regs
[4] & 0x07;
77 tm
->tm_mon
= bcd2bin(regs
[5] & 0x1F) - 1; /* rtc mn 1-12 */
78 tm
->tm_year
= bcd2bin(regs
[6]);
80 tm
->tm_year
+= 100; /* assume we are in 1970...2069 */
81 /* detect the polarity heuristically. see note above. */
82 pcf85063
->c_polarity
= (regs
[5] & PCF85063_MO_C
) ?
83 (tm
->tm_year
>= 100) : (tm
->tm_year
< 100);
85 return rtc_valid_tm(tm
);
88 static int pcf85063_set_datetime(struct i2c_client
*client
, struct rtc_time
*tm
)
91 unsigned char buf
[11];
93 /* Control & status */
94 buf
[PCF85063_REG_CTRL1
] = 0;
95 buf
[PCF85063_REG_CTRL2
] = 5;
97 /* hours, minutes and seconds */
98 buf
[PCF85063_REG_SC
] = bin2bcd(tm
->tm_sec
) & 0x7F;
100 buf
[PCF85063_REG_MN
] = bin2bcd(tm
->tm_min
);
101 buf
[PCF85063_REG_HR
] = bin2bcd(tm
->tm_hour
);
103 /* Day of month, 1 - 31 */
104 buf
[PCF85063_REG_DM
] = bin2bcd(tm
->tm_mday
);
107 buf
[PCF85063_REG_DW
] = tm
->tm_wday
& 0x07;
110 buf
[PCF85063_REG_MO
] = bin2bcd(tm
->tm_mon
+ 1);
112 /* year and century */
113 buf
[PCF85063_REG_YR
] = bin2bcd(tm
->tm_year
% 100);
115 /* write register's data */
116 for (i
= 0; i
< sizeof(buf
); i
++) {
117 unsigned char data
[2] = { i
, buf
[i
] };
119 err
= i2c_master_send(client
, data
, sizeof(data
));
120 if (err
!= sizeof(data
)) {
121 dev_err(&client
->dev
, "%s: err=%d addr=%02x, data=%02x\n",
122 __func__
, err
, data
[0], data
[1]);
130 static int pcf85063_rtc_read_time(struct device
*dev
, struct rtc_time
*tm
)
132 return pcf85063_get_datetime(to_i2c_client(dev
), tm
);
135 static int pcf85063_rtc_set_time(struct device
*dev
, struct rtc_time
*tm
)
137 return pcf85063_set_datetime(to_i2c_client(dev
), tm
);
140 static const struct rtc_class_ops pcf85063_rtc_ops
= {
141 .read_time
= pcf85063_rtc_read_time
,
142 .set_time
= pcf85063_rtc_set_time
145 static int pcf85063_probe(struct i2c_client
*client
,
146 const struct i2c_device_id
*id
)
148 struct pcf85063
*pcf85063
;
150 dev_dbg(&client
->dev
, "%s\n", __func__
);
152 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
155 pcf85063
= devm_kzalloc(&client
->dev
, sizeof(struct pcf85063
),
160 dev_info(&client
->dev
, "chip found, driver version " DRV_VERSION
"\n");
162 i2c_set_clientdata(client
, pcf85063
);
164 pcf85063
->rtc
= devm_rtc_device_register(&client
->dev
,
165 pcf85063_driver
.driver
.name
,
166 &pcf85063_rtc_ops
, THIS_MODULE
);
168 return PTR_ERR_OR_ZERO(pcf85063
->rtc
);
171 static const struct i2c_device_id pcf85063_id
[] = {
175 MODULE_DEVICE_TABLE(i2c
, pcf85063_id
);
178 static const struct of_device_id pcf85063_of_match
[] = {
179 { .compatible
= "nxp,pcf85063" },
182 MODULE_DEVICE_TABLE(of
, pcf85063_of_match
);
185 static struct i2c_driver pcf85063_driver
= {
187 .name
= "rtc-pcf85063",
188 .of_match_table
= of_match_ptr(pcf85063_of_match
),
190 .probe
= pcf85063_probe
,
191 .id_table
= pcf85063_id
,
194 module_i2c_driver(pcf85063_driver
);
196 MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
197 MODULE_DESCRIPTION("PCF85063 RTC driver");
198 MODULE_LICENSE("GPL");
199 MODULE_VERSION(DRV_VERSION
);