blob: 7f9caee5f4abcb1936c2b7ee15232f75d93063df [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 */
22#define PCF85063_REG_CTRL2 0x01
23
24#define PCF85063_REG_SC 0x04 /* datetime */
25#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
32#define PCF85063_MO_C 0x80 /* century */
33
34static struct i2c_driver pcf85063_driver;
35
36struct pcf85063 {
37 struct rtc_device *rtc;
38 int c_polarity; /* 0: MO_C=1 means 19xx, otherwise MO_C=1 means 20xx */
39 int voltage_low; /* indicates if a low_voltage was detected */
40};
41
42/*
43 * In the routines that deal directly with the pcf85063 hardware, we use
44 * rtc_time -- month 0-11, hour 0-23, yr = calendar year-epoch.
45 */
46static int pcf85063_get_datetime(struct i2c_client *client, struct rtc_time *tm)
47{
Juergen Borleis7b576842016-02-09 11:57:25 +010048 int rc;
Søren Andersen796b7ab2014-08-08 14:20:22 -070049 struct pcf85063 *pcf85063 = i2c_get_clientdata(client);
Juergen Borleis7b576842016-02-09 11:57:25 +010050 u8 regs[7];
Søren Andersen796b7ab2014-08-08 14:20:22 -070051
Juergen Borleis7b576842016-02-09 11:57:25 +010052 /*
53 * while reading, the time/date registers are blocked and not updated
54 * anymore until the access is finished. To not lose a second
55 * event, the access must be finished within one second. So, read all
56 * time/date registers in one turn.
57 */
58 rc = i2c_smbus_read_i2c_block_data(client, PCF85063_REG_SC,
59 sizeof(regs), regs);
60 if (rc != sizeof(regs)) {
61 dev_err(&client->dev, "date/time register read error\n");
Søren Andersen796b7ab2014-08-08 14:20:22 -070062 return -EIO;
63 }
64
Juergen Borleis7b576842016-02-09 11:57:25 +010065 tm->tm_sec = bcd2bin(regs[0] & 0x7F);
66 tm->tm_min = bcd2bin(regs[1] & 0x7F);
67 tm->tm_hour = bcd2bin(regs[2] & 0x3F); /* rtc hr 0-23 */
68 tm->tm_mday = bcd2bin(regs[3] & 0x3F);
69 tm->tm_wday = regs[4] & 0x07;
70 tm->tm_mon = bcd2bin(regs[5] & 0x1F) - 1; /* rtc mn 1-12 */
71 tm->tm_year = bcd2bin(regs[6]);
Søren Andersen796b7ab2014-08-08 14:20:22 -070072 if (tm->tm_year < 70)
73 tm->tm_year += 100; /* assume we are in 1970...2069 */
74 /* detect the polarity heuristically. see note above. */
Juergen Borleis7b576842016-02-09 11:57:25 +010075 pcf85063->c_polarity = (regs[5] & PCF85063_MO_C) ?
Søren Andersen796b7ab2014-08-08 14:20:22 -070076 (tm->tm_year >= 100) : (tm->tm_year < 100);
77
Alexandre Belloni5413eab2015-09-29 23:02:46 +020078 return rtc_valid_tm(tm);
Søren Andersen796b7ab2014-08-08 14:20:22 -070079}
80
81static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
82{
83 int i = 0, err = 0;
84 unsigned char buf[11];
85
86 /* Control & status */
87 buf[PCF85063_REG_CTRL1] = 0;
88 buf[PCF85063_REG_CTRL2] = 5;
89
90 /* hours, minutes and seconds */
91 buf[PCF85063_REG_SC] = bin2bcd(tm->tm_sec) & 0x7F;
92
93 buf[PCF85063_REG_MN] = bin2bcd(tm->tm_min);
94 buf[PCF85063_REG_HR] = bin2bcd(tm->tm_hour);
95
96 /* Day of month, 1 - 31 */
97 buf[PCF85063_REG_DM] = bin2bcd(tm->tm_mday);
98
99 /* Day, 0 - 6 */
100 buf[PCF85063_REG_DW] = tm->tm_wday & 0x07;
101
102 /* month, 1 - 12 */
103 buf[PCF85063_REG_MO] = bin2bcd(tm->tm_mon + 1);
104
105 /* year and century */
106 buf[PCF85063_REG_YR] = bin2bcd(tm->tm_year % 100);
107
108 /* write register's data */
109 for (i = 0; i < sizeof(buf); i++) {
110 unsigned char data[2] = { i, buf[i] };
111
112 err = i2c_master_send(client, data, sizeof(data));
113 if (err != sizeof(data)) {
114 dev_err(&client->dev, "%s: err=%d addr=%02x, data=%02x\n",
115 __func__, err, data[0], data[1]);
116 return -EIO;
117 }
118 }
119
120 return 0;
121}
122
123static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
124{
125 return pcf85063_get_datetime(to_i2c_client(dev), tm);
126}
127
128static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
129{
130 return pcf85063_set_datetime(to_i2c_client(dev), tm);
131}
132
133static const struct rtc_class_ops pcf85063_rtc_ops = {
134 .read_time = pcf85063_rtc_read_time,
135 .set_time = pcf85063_rtc_set_time
136};
137
138static int pcf85063_probe(struct i2c_client *client,
139 const struct i2c_device_id *id)
140{
141 struct pcf85063 *pcf85063;
142
143 dev_dbg(&client->dev, "%s\n", __func__);
144
145 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
146 return -ENODEV;
147
148 pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
149 GFP_KERNEL);
150 if (!pcf85063)
151 return -ENOMEM;
152
153 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
154
155 i2c_set_clientdata(client, pcf85063);
156
157 pcf85063->rtc = devm_rtc_device_register(&client->dev,
158 pcf85063_driver.driver.name,
159 &pcf85063_rtc_ops, THIS_MODULE);
160
161 return PTR_ERR_OR_ZERO(pcf85063->rtc);
162}
163
164static const struct i2c_device_id pcf85063_id[] = {
165 { "pcf85063", 0 },
166 { }
167};
168MODULE_DEVICE_TABLE(i2c, pcf85063_id);
169
170#ifdef CONFIG_OF
171static const struct of_device_id pcf85063_of_match[] = {
172 { .compatible = "nxp,pcf85063" },
173 {}
174};
175MODULE_DEVICE_TABLE(of, pcf85063_of_match);
176#endif
177
178static struct i2c_driver pcf85063_driver = {
179 .driver = {
180 .name = "rtc-pcf85063",
Søren Andersen796b7ab2014-08-08 14:20:22 -0700181 .of_match_table = of_match_ptr(pcf85063_of_match),
182 },
183 .probe = pcf85063_probe,
184 .id_table = pcf85063_id,
185};
186
187module_i2c_driver(pcf85063_driver);
188
189MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
190MODULE_DESCRIPTION("PCF85063 RTC driver");
191MODULE_LICENSE("GPL");
192MODULE_VERSION(DRV_VERSION);