blob: c5db231f14de228ab9c61e8bdf9cbab444806899 [file] [log] [blame]
Søren Andersen796b7ab2014-08-08 14:20:22 -07001/*
2 * An I2C driver for the PCF85063 RTC
3 * Copyright 2014 Rose Technology
4 *
5 * Author: Søren Andersen <san@rosetechnology.dk>
6 * Maintainers: http://www.nslu2-linux.org/
7 *
8 * based on the other drivers in this same directory.
9 *
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.
13 */
14#include <linux/i2c.h>
15#include <linux/bcd.h>
16#include <linux/rtc.h>
17#include <linux/module.h>
18
19#define DRV_VERSION "0.0.1"
20
21#define PCF85063_REG_CTRL1 0x00 /* status */
Juergen Borleis31d4d332016-02-09 11:57:27 +010022#define PCF85063_REG_CTRL1_STOP BIT(5)
Søren Andersen796b7ab2014-08-08 14:20:22 -070023#define PCF85063_REG_CTRL2 0x01
24
25#define PCF85063_REG_SC 0x04 /* datetime */
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010026#define PCF85063_REG_SC_OS 0x80
Søren Andersen796b7ab2014-08-08 14:20:22 -070027#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
33
34#define PCF85063_MO_C 0x80 /* century */
35
36static struct i2c_driver pcf85063_driver;
37
38struct pcf85063 {
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 */
42};
43
Juergen Borleis31d4d332016-02-09 11:57:27 +010044static 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 /* stop the clock */
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
Søren Andersen796b7ab2014-08-08 14:20:22 -070068/*
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.
71 */
72static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
73{
Juergen Borleis7b576842016-02-09 11:57:25 +010074 int rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -070075 struct pcf85063 *pcf85063 = i2c_get_clientdata(client);
Juergen Borleis7b576842016-02-09 11:57:25 +010076 u8 regs[7];
Søren Andersen796b7ab2014-08-08 14:20:22 -070077
Juergen Borleis7b576842016-02-09 11:57:25 +010078 /*
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.
83 */
84 rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
85 sizeof(regs), regs);
86 if (rc != sizeof(regs)) {
87 dev_err(&client->dev, "date/time register read error\n");
Søren Andersen796b7ab2014-08-08 14:20:22 -070088 return -EIO;
89 }
90
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010091 /* 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");
94 return -EINVAL;
95 }
96
Juergen Borleis7b576842016-02-09 11:57:25 +010097 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]);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700104 if (tm->tm_year < 70)
105 tm->tm_year += 100; /* assume we are in 1970...2069 */
106 /* detect the polarity heuristically. see note above. */
Juergen Borleis7b576842016-02-09 11:57:25 +0100107 pcf85063->c_polarity = (regs[5] & PCF85063_MO_C) ?
Søren Andersen796b7ab2014-08-08 14:20:22 -0700108 (tm->tm_year >= 100) : (tm->tm_year < 100);
109
Alexandre Belloni5413eab2015-09-29 23:02:46 +0200110 return rtc_valid_tm(tm);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700111}
112
113static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
114{
Juergen Borleis31d4d332016-02-09 11:57:27 +0100115 int rc;
116 u8 regs[8];
Søren Andersen796b7ab2014-08-08 14:20:22 -0700117
Juergen Borleis31d4d332016-02-09 11:57:27 +0100118 /*
119 * to accurately set the time, reset the divider chain and keep it in
120 * reset state until all time/date registers are written
121 */
122 rc = pcf85063_stop_clock(client, &regs[7]);
123 if (rc != 0)
124 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700125
126 /* hours, minutes and seconds */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100127 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
Søren Andersen796b7ab2014-08-08 14:20:22 -0700128
Juergen Borleis31d4d332016-02-09 11:57:27 +0100129 regs[1] = bin2bcd(tm->tm_min);
130 regs[2] = bin2bcd(tm->tm_hour);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700131
132 /* Day of month, 1 - 31 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100133 regs[3] = bin2bcd(tm->tm_mday);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700134
135 /* Day, 0 - 6 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100136 regs[4] = tm->tm_wday & 0x07;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700137
138 /* month, 1 - 12 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100139 regs[5] = bin2bcd(tm->tm_mon + 1);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700140
141 /* year and century */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100142 regs[6] = bin2bcd(tm->tm_year % 100);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700143
Juergen Borleis31d4d332016-02-09 11:57:27 +0100144 /*
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
148 */
149 regs[7] &= ~PCF85063_REG_CTRL1_STOP;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700150
Juergen Borleis31d4d332016-02-09 11:57:27 +0100151 /* write all registers at once */
152 rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
153 sizeof(regs), regs);
154 if (rc < 0) {
155 dev_err(&client->dev, "date/time register write error\n");
156 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700157 }
158
159 return 0;
160}
161
162static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
163{
164 return pcf85063_get_datetime(to_i2c_client(dev), tm);
165}
166
167static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
168{
169 return pcf85063_set_datetime(to_i2c_client(dev), tm);
170}
171
172static const struct rtc_class_ops pcf85063_rtc_ops = {
173 .read_time = pcf85063_rtc_read_time,
174 .set_time = pcf85063_rtc_set_time
175};
176
177static int pcf85063_probe(struct i2c_client *client,
178 const struct i2c_device_id *id)
179{
180 struct pcf85063 *pcf85063;
181
182 dev_dbg(&client->dev, "%s\n", __func__);
183
184 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
185 return -ENODEV;
186
187 pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
188 GFP_KERNEL);
189 if (!pcf85063)
190 return -ENOMEM;
191
192 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
193
194 i2c_set_clientdata(client, pcf85063);
195
196 pcf85063->rtc = devm_rtc_device_register(&client->dev,
197 pcf85063_driver.driver.name,
198 &pcf85063_rtc_ops, THIS_MODULE);
199
200 return PTR_ERR_OR_ZERO(pcf85063->rtc);
201}
202
203static const struct i2c_device_id pcf85063_id[] = {
204 { "pcf85063", 0 },
205 { }
206};
207MODULE_DEVICE_TABLE(i2c, pcf85063_id);
208
209#ifdef CONFIG_OF
210static const struct of_device_id pcf85063_of_match[] = {
211 { .compatible = "nxp,pcf85063" },
212 {}
213};
214MODULE_DEVICE_TABLE(of, pcf85063_of_match);
215#endif
216
217static struct i2c_driver pcf85063_driver = {
218 .driver = {
219 .name = "rtc-pcf85063",
Søren Andersen796b7ab2014-08-08 14:20:22 -0700220 .of_match_table = of_match_ptr(pcf85063_of_match),
221 },
222 .probe = pcf85063_probe,
223 .id_table = pcf85063_id,
224};
225
226module_i2c_driver(pcf85063_driver);
227
228MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
229MODULE_DESCRIPTION("PCF85063 RTC driver");
230MODULE_LICENSE("GPL");
231MODULE_VERSION(DRV_VERSION);