blob: 6a12bf62c504bf736a7f1b371d8546a275b1dd03 [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{
48 struct pcf85063 *pcf85063 = i2c_get_clientdata(client);
49 unsigned char buf[13] = { PCF85063_REG_CTRL1 };
50 struct i2c_msg msgs[] = {
51 {/* setup read ptr */
52 .addr = client->addr,
53 .len = 1,
54 .buf = buf
55 },
56 {/* read status + date */
57 .addr = client->addr,
58 .flags = I2C_M_RD,
59 .len = 13,
60 .buf = buf
61 },
62 };
63
64 /* read registers */
65 if ((i2c_transfer(client->adapter, msgs, 2)) != 2) {
66 dev_err(&client->dev, "%s: read error\n", __func__);
67 return -EIO;
68 }
69
70 tm->tm_sec = bcd2bin(buf[PCF85063_REG_SC] & 0x7F);
71 tm->tm_min = bcd2bin(buf[PCF85063_REG_MN] & 0x7F);
72 tm->tm_hour = bcd2bin(buf[PCF85063_REG_HR] & 0x3F); /* rtc hr 0-23 */
73 tm->tm_mday = bcd2bin(buf[PCF85063_REG_DM] & 0x3F);
74 tm->tm_wday = buf[PCF85063_REG_DW] & 0x07;
75 tm->tm_mon = bcd2bin(buf[PCF85063_REG_MO] & 0x1F) - 1; /* rtc mn 1-12 */
76 tm->tm_year = bcd2bin(buf[PCF85063_REG_YR]);
77 if (tm->tm_year < 70)
78 tm->tm_year += 100; /* assume we are in 1970...2069 */
79 /* detect the polarity heuristically. see note above. */
80 pcf85063->c_polarity = (buf[PCF85063_REG_MO] & PCF85063_MO_C) ?
81 (tm->tm_year >= 100) : (tm->tm_year < 100);
82
83 /* the clock can give out invalid datetime, but we cannot return
84 * -EINVAL otherwise hwclock will refuse to set the time on bootup.
85 */
86 if (rtc_valid_tm(tm) < 0)
87 dev_err(&client->dev, "retrieved date/time is not valid.\n");
88
89 return 0;
90}
91
92static int pcf85063_set_datetime(struct i2c_client *client, struct rtc_time *tm)
93{
94 int i = 0, err = 0;
95 unsigned char buf[11];
96
97 /* Control & status */
98 buf[PCF85063_REG_CTRL1] = 0;
99 buf[PCF85063_REG_CTRL2] = 5;
100
101 /* hours, minutes and seconds */
102 buf[PCF85063_REG_SC] = bin2bcd(tm->tm_sec) & 0x7F;
103
104 buf[PCF85063_REG_MN] = bin2bcd(tm->tm_min);
105 buf[PCF85063_REG_HR] = bin2bcd(tm->tm_hour);
106
107 /* Day of month, 1 - 31 */
108 buf[PCF85063_REG_DM] = bin2bcd(tm->tm_mday);
109
110 /* Day, 0 - 6 */
111 buf[PCF85063_REG_DW] = tm->tm_wday & 0x07;
112
113 /* month, 1 - 12 */
114 buf[PCF85063_REG_MO] = bin2bcd(tm->tm_mon + 1);
115
116 /* year and century */
117 buf[PCF85063_REG_YR] = bin2bcd(tm->tm_year % 100);
118
119 /* write register's data */
120 for (i = 0; i < sizeof(buf); i++) {
121 unsigned char data[2] = { i, buf[i] };
122
123 err = i2c_master_send(client, data, sizeof(data));
124 if (err != sizeof(data)) {
125 dev_err(&client->dev, "%s: err=%d addr=%02x, data=%02x\n",
126 __func__, err, data[0], data[1]);
127 return -EIO;
128 }
129 }
130
131 return 0;
132}
133
134static int pcf85063_rtc_read_time(struct device *dev, struct rtc_time *tm)
135{
136 return pcf85063_get_datetime(to_i2c_client(dev), tm);
137}
138
139static int pcf85063_rtc_set_time(struct device *dev, struct rtc_time *tm)
140{
141 return pcf85063_set_datetime(to_i2c_client(dev), tm);
142}
143
144static const struct rtc_class_ops pcf85063_rtc_ops = {
145 .read_time = pcf85063_rtc_read_time,
146 .set_time = pcf85063_rtc_set_time
147};
148
149static int pcf85063_probe(struct i2c_client *client,
150 const struct i2c_device_id *id)
151{
152 struct pcf85063 *pcf85063;
153
154 dev_dbg(&client->dev, "%s\n", __func__);
155
156 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
157 return -ENODEV;
158
159 pcf85063 = devm_kzalloc(&client->dev, sizeof(struct pcf85063),
160 GFP_KERNEL);
161 if (!pcf85063)
162 return -ENOMEM;
163
164 dev_info(&client->dev, "chip found, driver version " DRV_VERSION "\n");
165
166 i2c_set_clientdata(client, pcf85063);
167
168 pcf85063->rtc = devm_rtc_device_register(&client->dev,
169 pcf85063_driver.driver.name,
170 &pcf85063_rtc_ops, THIS_MODULE);
171
172 return PTR_ERR_OR_ZERO(pcf85063->rtc);
173}
174
175static const struct i2c_device_id pcf85063_id[] = {
176 { "pcf85063", 0 },
177 { }
178};
179MODULE_DEVICE_TABLE(i2c, pcf85063_id);
180
181#ifdef CONFIG_OF
182static const struct of_device_id pcf85063_of_match[] = {
183 { .compatible = "nxp,pcf85063" },
184 {}
185};
186MODULE_DEVICE_TABLE(of, pcf85063_of_match);
187#endif
188
189static struct i2c_driver pcf85063_driver = {
190 .driver = {
191 .name = "rtc-pcf85063",
192 .owner = THIS_MODULE,
193 .of_match_table = of_match_ptr(pcf85063_of_match),
194 },
195 .probe = pcf85063_probe,
196 .id_table = pcf85063_id,
197};
198
199module_i2c_driver(pcf85063_driver);
200
201MODULE_AUTHOR("Søren Andersen <san@rosetechnology.dk>");
202MODULE_DESCRIPTION("PCF85063 RTC driver");
203MODULE_LICENSE("GPL");
204MODULE_VERSION(DRV_VERSION);