blob: e8ddbb359d11951f0d9592895a23699941280412 [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
Søren Andersen796b7ab2014-08-08 14:20:22 -070019#define PCF85063_REG_CTRL1 0x00 /* status */
Juergen Borleis31d4d332016-02-09 11:57:27 +010020#define PCF85063_REG_CTRL1_STOP BIT(5)
Søren Andersen796b7ab2014-08-08 14:20:22 -070021#define PCF85063_REG_CTRL2 0x01
22
23#define PCF85063_REG_SC 0x04 /* datetime */
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010024#define PCF85063_REG_SC_OS 0x80
Søren Andersen796b7ab2014-08-08 14:20:22 -070025#define PCF85063_REG_MN 0x05
26#define PCF85063_REG_HR 0x06
27#define PCF85063_REG_DM 0x07
28#define PCF85063_REG_DW 0x08
29#define PCF85063_REG_MO 0x09
30#define PCF85063_REG_YR 0x0A
31
Søren Andersen796b7ab2014-08-08 14:20:22 -070032static struct i2c_driver pcf85063_driver;
33
Juergen Borleis31d4d332016-02-09 11:57:27 +010034static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
35{
36 s32 ret;
37
38 ret = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
39 if (ret < 0) {
40 dev_err(&client->dev, "Failing to stop the clock\n");
41 return -EIO;
42 }
43
44 /* stop the clock */
45 ret |= PCF85063_REG_CTRL1_STOP;
46
47 ret = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ret);
48 if (ret < 0) {
49 dev_err(&client->dev, "Failing to stop the clock\n");
50 return -EIO;
51 }
52
53 *ctrl1 = ret;
54
55 return 0;
56}
57
Søren Andersen796b7ab2014-08-08 14:20:22 -070058/*
59 * In the routines that deal directly with the pcf85063 hardware, we use
60 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
61 */
62static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
63{
Juergen Borleis7b576842016-02-09 11:57:25 +010064 int rc;
Juergen Borleis7b576842016-02-09 11:57:25 +010065 u8 regs[7];
Søren Andersen796b7ab2014-08-08 14:20:22 -070066
Juergen Borleis7b576842016-02-09 11:57:25 +010067 /*
68 * while reading, the time/date registers are blocked and not updated
69 * anymore until the access is finished. To not lose a second
70 * event, the access must be finished within one second. So, read all
71 * time/date registers in one turn.
72 */
73 rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
74 sizeof(regs), regs);
75 if (rc != sizeof(regs)) {
76 dev_err(&client->dev, "date/time register read error\n");
Søren Andersen796b7ab2014-08-08 14:20:22 -070077 return -EIO;
78 }
79
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010080 /* if the clock has lost its power it makes no sense to use its time */
81 if (regs[0] & PCF85063_REG_SC_OS) {
82 dev_warn(&client->dev, "Power loss detected, invalid time\n");
83 return -EINVAL;
84 }
85
Juergen Borleis7b576842016-02-09 11:57:25 +010086 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
87 tm->tm_min = bcd2bin(regs[1] & 0x7F);
88 tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
89 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
90 tm->tm_wday = regs[4] & 0x07;
91 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
92 tm->tm_year = bcd2bin(regs[6]);
Søren Andersen796b7ab2014-08-08 14:20:22 -070093 if (tm->tm_year < 70)
94 tm->tm_year += 100; /* assume we are in 1970...2069 */
Søren Andersen796b7ab2014-08-08 14:20:22 -070095
Alexandre Belloni5413eab2015-09-29 23:02:46 +020096 return rtc_valid_tm(tm);
Søren Andersen796b7ab2014-08-08 14:20:22 -070097}
98
99static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
100{
Juergen Borleis31d4d332016-02-09 11:57:27 +0100101 int rc;
102 u8 regs[8];
Søren Andersen796b7ab2014-08-08 14:20:22 -0700103
Juergen Borleis31d4d332016-02-09 11:57:27 +0100104 /*
105 * to accurately set the time, reset the divider chain and keep it in
106 * reset state until all time/date registers are written
107 */
108 rc = pcf85063_stop_clock(client, &regs[7]);
109 if (rc != 0)
110 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700111
112 /* hours, minutes and seconds */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100113 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
Søren Andersen796b7ab2014-08-08 14:20:22 -0700114
Juergen Borleis31d4d332016-02-09 11:57:27 +0100115 regs[1] = bin2bcd(tm->tm_min);
116 regs[2] = bin2bcd(tm->tm_hour);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700117
118 /* Day of month, 1 - 31 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100119 regs[3] = bin2bcd(tm->tm_mday);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700120
121 /* Day, 0 - 6 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100122 regs[4] = tm->tm_wday & 0x07;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700123
124 /* month, 1 - 12 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100125 regs[5] = bin2bcd(tm->tm_mon + 1);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700126
127 /* year and century */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100128 regs[6] = bin2bcd(tm->tm_year % 100);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700129
Juergen Borleis31d4d332016-02-09 11:57:27 +0100130 /*
131 * after all time/date registers are written, let the 'address auto
132 * increment' feature wrap around and write register CTRL1 to re-enable
133 * the clock divider chain again
134 */
135 regs[7] &= ~PCF85063_REG_CTRL1_STOP;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700136
Juergen Borleis31d4d332016-02-09 11:57:27 +0100137 /* write all registers at once */
138 rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
139 sizeof(regs), regs);
140 if (rc < 0) {
141 dev_err(&client->dev, "date/time register write error\n");
142 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700143 }
144
145 return 0;
146}
147
148static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
149{
150 return pcf85063_get_datetime(to_i2c_client(dev), tm);
151}
152
153static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
154{
155 return pcf85063_set_datetime(to_i2c_client(dev), tm);
156}
157
158static const struct rtc_class_ops pcf85063_rtc_ops = {
159 .read_time = pcf85063_rtc_read_time,
160 .set_time = pcf85063_rtc_set_time
161};
162
163static int pcf85063_probe(struct i2c_client *client,
164 const struct i2c_device_id *id)
165{
Alexandre Belloni2da424a2016-03-02 11:25:43 +0100166 struct rtc_device *rtc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700167
168 dev_dbg(&client->dev, "%s\n", __func__);
169
170 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
171 return -ENODEV;
172
Alexandre Belloni2da424a2016-03-02 11:25:43 +0100173 rtc = devm_rtc_device_register(&client->dev,
174 pcf85063_driver.driver.name,
175 &pcf85063_rtc_ops, THIS_MODULE);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700176
Alexandre Belloni2da424a2016-03-02 11:25:43 +0100177 return PTR_ERR_OR_ZERO(rtc);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700178}
179
180static const struct i2c_device_id pcf85063_id[] = {
181 { "pcf85063", 0 },
182 { }
183};
184MODULE_DEVICE_TABLE(i2c, pcf85063_id);
185
186#ifdef CONFIG_OF
187static const struct of_device_id pcf85063_of_match[] = {
188 { .compatible = "nxp,pcf85063" },
189 {}
190};
191MODULE_DEVICE_TABLE(of, pcf85063_of_match);
192#endif
193
194static struct i2c_driver pcf85063_driver = {
195 .driver = {
196 .name = "rtc-pcf85063",
Søren Andersen796b7ab2014-08-08 14:20:22 -0700197 .of_match_table = of_match_ptr(pcf85063_of_match),
198 },
199 .probe = pcf85063_probe,
200 .id_table = pcf85063_id,
201};
202
203module_i2c_driver(pcf85063_driver);
204
205MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
206MODULE_DESCRIPTION("PCF85063 RTC driver");
207MODULE_LICENSE("GPL");