blob: 283c2335b01b69b54a636680ec88d88852b11aa6 [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
Chris DeBruin0d981f82016-07-12 17:15:46 -040019/*
20 * Information for this driver was pulled from the following datasheets.
21 *
22 * http://www.nxp.com/documents/data_sheet/PCF85063A.pdf
23 * http://www.nxp.com/documents/data_sheet/PCF85063TP.pdf
24 *
25 * PCF85063A -- Rev. 6 — 18 November 2015
26 * PCF85063TP -- Rev. 4 — 6 May 2015
27*/
28
Søren Andersen796b7ab2014-08-08 14:20:22 -070029#define PCF85063_REG_CTRL1 0x00 /* status */
Juergen Borleis31d4d332016-02-09 11:57:27 +010030#define PCF85063_REG_CTRL1_STOP BIT(5)
Søren Andersen796b7ab2014-08-08 14:20:22 -070031#define PCF85063_REG_CTRL2 0x01
32
33#define PCF85063_REG_SC 0x04 /* datetime */
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +010034#define PCF85063_REG_SC_OS 0x80
Søren Andersen796b7ab2014-08-08 14:20:22 -070035#define PCF85063_REG_MN 0x05
36#define PCF85063_REG_HR 0x06
37#define PCF85063_REG_DM 0x07
38#define PCF85063_REG_DW 0x08
39#define PCF85063_REG_MO 0x09
40#define PCF85063_REG_YR 0x0A
41
Søren Andersen796b7ab2014-08-08 14:20:22 -070042static struct i2c_driver pcf85063_driver;
43
Juergen Borleis31d4d332016-02-09 11:57:27 +010044static int pcf85063_stop_clock(struct i2c_client *client, u8 *ctrl1)
45{
Alvin Šipragaec9cf1b2018-07-15 23:31:53 +020046 int rc;
47 u8 reg;
Juergen Borleis31d4d332016-02-09 11:57:27 +010048
Alvin Šipragaec9cf1b2018-07-15 23:31:53 +020049 rc = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
50 if (rc < 0) {
Juergen Borleis31d4d332016-02-09 11:57:27 +010051 dev_err(&client->dev, "Failing to stop the clock\n");
52 return -EIO;
53 }
54
55 /* stop the clock */
Alvin Šipragaec9cf1b2018-07-15 23:31:53 +020056 reg = rc | PCF85063_REG_CTRL1_STOP;
Juergen Borleis31d4d332016-02-09 11:57:27 +010057
Alvin Šipragaec9cf1b2018-07-15 23:31:53 +020058 rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, reg);
59 if (rc < 0) {
Juergen Borleis31d4d332016-02-09 11:57:27 +010060 dev_err(&client->dev, "Failing to stop the clock\n");
61 return -EIO;
62 }
63
Alvin Šipragaec9cf1b2018-07-15 23:31:53 +020064 *ctrl1 = reg;
Juergen Borleis31d4d332016-02-09 11:57:27 +010065
66 return 0;
67}
68
Chris DeBruin0d981f82016-07-12 17:15:46 -040069static int pcf85063_start_clock(struct i2c_client *client, u8 ctrl1)
70{
Alvin Šipragaec9cf1b2018-07-15 23:31:53 +020071 int rc;
Chris DeBruin0d981f82016-07-12 17:15:46 -040072
73 /* start the clock */
Michael McCormick051abf52018-02-22 11:29:24 +130074 ctrl1 &= ~PCF85063_REG_CTRL1_STOP;
Chris DeBruin0d981f82016-07-12 17:15:46 -040075
Alvin Šipragaec9cf1b2018-07-15 23:31:53 +020076 rc = i2c_smbus_write_byte_data(client, PCF85063_REG_CTRL1, ctrl1);
77 if (rc < 0) {
Chris DeBruin0d981f82016-07-12 17:15:46 -040078 dev_err(&client->dev, "Failing to start the clock\n");
79 return -EIO;
80 }
81
82 return 0;
83}
84
Alexandre Belloni965271d2018-02-21 16:09:27 +010085static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
Søren Andersen796b7ab2014-08-08 14:20:22 -070086{
Alexandre Belloni965271d2018-02-21 16:09:27 +010087 struct i2c_client *client = to_i2c_client(dev);
Juergen Borleis7b576842016-02-09 11:57:25 +010088 int rc;
Juergen Borleis7b576842016-02-09 11:57:25 +010089 u8 regs[7];
Søren Andersen796b7ab2014-08-08 14:20:22 -070090
Juergen Borleis7b576842016-02-09 11:57:25 +010091 /*
92 * while reading, the time/date registers are blocked and not updated
93 * anymore until the access is finished. To not lose a second
94 * event, the access must be finished within one second. So, read all
95 * time/date registers in one turn.
96 */
97 rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
98 sizeof(regs), regs);
99 if (rc != sizeof(regs)) {
100 dev_err(&client->dev, "date/time register read error\n");
Søren Andersen796b7ab2014-08-08 14:20:22 -0700101 return -EIO;
102 }
103
Juergen Borleis6cc4c8b2016-02-09 11:57:26 +0100104 /* if the clock has lost its power it makes no sense to use its time */
105 if (regs[0] & PCF85063_REG_SC_OS) {
106 dev_warn(&client->dev, "Power loss detected, invalid time\n");
107 return -EINVAL;
108 }
109
Juergen Borleis7b576842016-02-09 11:57:25 +0100110 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
111 tm->tm_min = bcd2bin(regs[1] & 0x7F);
112 tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
113 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
114 tm->tm_wday = regs[4] & 0x07;
115 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
116 tm->tm_year = bcd2bin(regs[6]);
Alexandre Bellonic421ce72016-07-18 11:08:59 +0200117 tm->tm_year += 100;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700118
Alexandre Belloni0a6b8882018-02-21 16:07:34 +0100119 return 0;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700120}
121
Alexandre Belloni965271d2018-02-21 16:09:27 +0100122static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
Søren Andersen796b7ab2014-08-08 14:20:22 -0700123{
Alexandre Belloni965271d2018-02-21 16:09:27 +0100124 struct i2c_client *client = to_i2c_client(dev);
Juergen Borleis31d4d332016-02-09 11:57:27 +0100125 int rc;
Chris DeBruin0d981f82016-07-12 17:15:46 -0400126 u8 regs[7];
127 u8 ctrl1;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700128
Alexandre Bellonic421ce72016-07-18 11:08:59 +0200129 if ((tm->tm_year < 100) || (tm->tm_year > 199))
130 return -EINVAL;
131
Juergen Borleis31d4d332016-02-09 11:57:27 +0100132 /*
133 * to accurately set the time, reset the divider chain and keep it in
134 * reset state until all time/date registers are written
135 */
Chris DeBruin0d981f82016-07-12 17:15:46 -0400136 rc = pcf85063_stop_clock(client, &ctrl1);
Juergen Borleis31d4d332016-02-09 11:57:27 +0100137 if (rc != 0)
138 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700139
140 /* hours, minutes and seconds */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100141 regs[0] = bin2bcd(tm->tm_sec) & 0x7F; /* clear OS flag */
Søren Andersen796b7ab2014-08-08 14:20:22 -0700142
Juergen Borleis31d4d332016-02-09 11:57:27 +0100143 regs[1] = bin2bcd(tm->tm_min);
144 regs[2] = bin2bcd(tm->tm_hour);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700145
146 /* Day of month, 1 - 31 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100147 regs[3] = bin2bcd(tm->tm_mday);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700148
149 /* Day, 0 - 6 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100150 regs[4] = tm->tm_wday & 0x07;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700151
152 /* month, 1 - 12 */
Juergen Borleis31d4d332016-02-09 11:57:27 +0100153 regs[5] = bin2bcd(tm->tm_mon + 1);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700154
155 /* year and century */
Alexandre Bellonic421ce72016-07-18 11:08:59 +0200156 regs[6] = bin2bcd(tm->tm_year - 100);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700157
Juergen Borleis31d4d332016-02-09 11:57:27 +0100158 /* write all registers at once */
159 rc = i2c_smbus_write_i2c_block_data(client, PCF85063_REG_SC,
160 sizeof(regs), regs);
161 if (rc < 0) {
162 dev_err(&client->dev, "date/time register write error\n");
163 return rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700164 }
165
Chris DeBruin0d981f82016-07-12 17:15:46 -0400166 /*
167 * Write the control register as a separate action since the size of
168 * the register space is different between the PCF85063TP and
169 * PCF85063A devices. The rollover point can not be used.
170 */
171 rc = pcf85063_start_clock(client, ctrl1);
172 if (rc != 0)
173 return rc;
174
Søren Andersen796b7ab2014-08-08 14:20:22 -0700175 return 0;
176}
177
Søren Andersen796b7ab2014-08-08 14:20:22 -0700178static const struct rtc_class_ops pcf85063_rtc_ops = {
179 .read_time = pcf85063_rtc_read_time,
180 .set_time = pcf85063_rtc_set_time
181};
182
183static int pcf85063_probe(struct i2c_client *client,
184 const struct i2c_device_id *id)
185{
Alexandre Belloni2da424a2016-03-02 11:25:43 +0100186 struct rtc_device *rtc;
Mirza Krakc18b4c52016-10-17 15:53:31 +0200187 int err;
Søren Andersen796b7ab2014-08-08 14:20:22 -0700188
189 dev_dbg(&client->dev, "%s\n", __func__);
190
191 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
192 return -ENODEV;
193
Mirza Krakc18b4c52016-10-17 15:53:31 +0200194 err = i2c_smbus_read_byte_data(client, PCF85063_REG_CTRL1);
195 if (err < 0) {
196 dev_err(&client->dev, "RTC chip is not present\n");
197 return err;
198 }
199
Alexandre Belloni2da424a2016-03-02 11:25:43 +0100200 rtc = devm_rtc_device_register(&client->dev,
201 pcf85063_driver.driver.name,
202 &pcf85063_rtc_ops, THIS_MODULE);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700203
Alexandre Belloni2da424a2016-03-02 11:25:43 +0100204 return PTR_ERR_OR_ZERO(rtc);
Søren Andersen796b7ab2014-08-08 14:20:22 -0700205}
206
207static const struct i2c_device_id pcf85063_id[] = {
208 { "pcf85063", 0 },
209 { }
210};
211MODULE_DEVICE_TABLE(i2c, pcf85063_id);
212
213#ifdef CONFIG_OF
214static const struct of_device_id pcf85063_of_match[] = {
215 { .compatible = "nxp,pcf85063" },
216 {}
217};
218MODULE_DEVICE_TABLE(of, pcf85063_of_match);
219#endif
220
221static struct i2c_driver pcf85063_driver = {
222 .driver = {
223 .name = "rtc-pcf85063",
Søren Andersen796b7ab2014-08-08 14:20:22 -0700224 .of_match_table = of_match_ptr(pcf85063_of_match),
225 },
226 .probe = pcf85063_probe,
227 .id_table = pcf85063_id,
228};
229
230module_i2c_driver(pcf85063_driver);
231
232MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
233MODULE_DESCRIPTION("PCF85063 RTC driver");
234MODULE_LICENSE("GPL");