blob: 64573cccf7fe0f2e644cb8afe433cd4292b4f411 [file] [log] [blame]
Eric Nelsona9687aa2017-11-01 08:01:20 -07001/*
2 * drivers/rtc/rtc-pcf85363.c
3 *
4 * Driver for NXP PCF85363 real-time clock.
5 *
6 * Copyright (C) 2017 Eric Nelson
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Based loosely on rtc-8583 by Russell King, Wolfram Sang and Juergen Beisert
13 */
14#include <linux/module.h>
15#include <linux/i2c.h>
16#include <linux/slab.h>
17#include <linux/rtc.h>
18#include <linux/init.h>
19#include <linux/err.h>
20#include <linux/errno.h>
21#include <linux/bcd.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/regmap.h>
25
26/*
27 * Date/Time registers
28 */
29#define DT_100THS 0x00
30#define DT_SECS 0x01
31#define DT_MINUTES 0x02
32#define DT_HOURS 0x03
33#define DT_DAYS 0x04
34#define DT_WEEKDAYS 0x05
35#define DT_MONTHS 0x06
36#define DT_YEARS 0x07
37
38/*
39 * Alarm registers
40 */
41#define DT_SECOND_ALM1 0x08
42#define DT_MINUTE_ALM1 0x09
43#define DT_HOUR_ALM1 0x0a
44#define DT_DAY_ALM1 0x0b
45#define DT_MONTH_ALM1 0x0c
46#define DT_MINUTE_ALM2 0x0d
47#define DT_HOUR_ALM2 0x0e
48#define DT_WEEKDAY_ALM2 0x0f
49#define DT_ALARM_EN 0x10
50
51/*
52 * Time stamp registers
53 */
54#define DT_TIMESTAMP1 0x11
55#define DT_TIMESTAMP2 0x17
56#define DT_TIMESTAMP3 0x1d
57#define DT_TS_MODE 0x23
58
59/*
60 * control registers
61 */
62#define CTRL_OFFSET 0x24
63#define CTRL_OSCILLATOR 0x25
64#define CTRL_BATTERY 0x26
65#define CTRL_PIN_IO 0x27
66#define CTRL_FUNCTION 0x28
67#define CTRL_INTA_EN 0x29
68#define CTRL_INTB_EN 0x2a
69#define CTRL_FLAGS 0x2b
70#define CTRL_RAMBYTE 0x2c
71#define CTRL_WDOG 0x2d
72#define CTRL_STOP_EN 0x2e
73#define CTRL_RESETS 0x2f
74#define CTRL_RAM 0x40
75
76#define NVRAM_SIZE 0x40
77
78static struct i2c_driver pcf85363_driver;
79
80struct pcf85363 {
81 struct device *dev;
82 struct rtc_device *rtc;
Eric Nelsona9687aa2017-11-01 08:01:20 -070083 struct regmap *regmap;
84};
85
86static int pcf85363_rtc_read_time(struct device *dev, struct rtc_time *tm)
87{
88 struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
89 unsigned char buf[DT_YEARS + 1];
90 int ret, len = sizeof(buf);
91
92 /* read the RTC date and time registers all at once */
93 ret = regmap_bulk_read(pcf85363->regmap, DT_100THS, buf, len);
94 if (ret) {
95 dev_err(dev, "%s: error %d\n", __func__, ret);
96 return ret;
97 }
98
99 tm->tm_year = bcd2bin(buf[DT_YEARS]);
100 /* adjust for 1900 base of rtc_time */
101 tm->tm_year += 100;
102
103 tm->tm_wday = buf[DT_WEEKDAYS] & 7;
104 buf[DT_SECS] &= 0x7F;
105 tm->tm_sec = bcd2bin(buf[DT_SECS]);
106 buf[DT_MINUTES] &= 0x7F;
107 tm->tm_min = bcd2bin(buf[DT_MINUTES]);
108 tm->tm_hour = bcd2bin(buf[DT_HOURS]);
109 tm->tm_mday = bcd2bin(buf[DT_DAYS]);
110 tm->tm_mon = bcd2bin(buf[DT_MONTHS]) - 1;
111
112 return 0;
113}
114
115static int pcf85363_rtc_set_time(struct device *dev, struct rtc_time *tm)
116{
117 struct pcf85363 *pcf85363 = dev_get_drvdata(dev);
118 unsigned char buf[DT_YEARS + 1];
119 int len = sizeof(buf);
120
121 buf[DT_100THS] = 0;
122 buf[DT_SECS] = bin2bcd(tm->tm_sec);
123 buf[DT_MINUTES] = bin2bcd(tm->tm_min);
124 buf[DT_HOURS] = bin2bcd(tm->tm_hour);
125 buf[DT_DAYS] = bin2bcd(tm->tm_mday);
126 buf[DT_WEEKDAYS] = tm->tm_wday;
127 buf[DT_MONTHS] = bin2bcd(tm->tm_mon + 1);
128 buf[DT_YEARS] = bin2bcd(tm->tm_year % 100);
129
130 return regmap_bulk_write(pcf85363->regmap, DT_100THS,
131 buf, len);
132}
133
134static const struct rtc_class_ops rtc_ops = {
135 .read_time = pcf85363_rtc_read_time,
136 .set_time = pcf85363_rtc_set_time,
137};
138
139static int pcf85363_nvram_read(void *priv, unsigned int offset, void *val,
140 size_t bytes)
141{
142 struct pcf85363 *pcf85363 = priv;
143
144 return regmap_bulk_read(pcf85363->regmap, CTRL_RAM + offset,
145 val, bytes);
146}
147
148static int pcf85363_nvram_write(void *priv, unsigned int offset, void *val,
149 size_t bytes)
150{
151 struct pcf85363 *pcf85363 = priv;
152
153 return regmap_bulk_write(pcf85363->regmap, CTRL_RAM + offset,
154 val, bytes);
155}
156
157static const struct regmap_config regmap_config = {
158 .reg_bits = 8,
159 .val_bits = 8,
160};
161
162static int pcf85363_probe(struct i2c_client *client,
163 const struct i2c_device_id *id)
164{
165 struct pcf85363 *pcf85363;
Alexandre Belloni0e7a4122018-02-12 23:47:30 +0100166 struct nvmem_config nvmem_cfg = {
167 .name = "pcf85363-",
168 .word_size = 1,
169 .stride = 1,
170 .size = NVRAM_SIZE,
171 .reg_read = pcf85363_nvram_read,
172 .reg_write = pcf85363_nvram_write,
173 };
Alexandre Belloni24849d12018-02-12 23:47:29 +0100174 int ret;
Eric Nelsona9687aa2017-11-01 08:01:20 -0700175
176 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
177 return -ENODEV;
178
179 pcf85363 = devm_kzalloc(&client->dev, sizeof(struct pcf85363),
180 GFP_KERNEL);
181 if (!pcf85363)
182 return -ENOMEM;
183
184 pcf85363->regmap = devm_regmap_init_i2c(client, &regmap_config);
185 if (IS_ERR(pcf85363->regmap)) {
186 dev_err(&client->dev, "regmap allocation failed\n");
187 return PTR_ERR(pcf85363->regmap);
188 }
189
190 pcf85363->dev = &client->dev;
191 i2c_set_clientdata(client, pcf85363);
192
193 pcf85363->rtc = devm_rtc_allocate_device(pcf85363->dev);
194 if (IS_ERR(pcf85363->rtc))
195 return PTR_ERR(pcf85363->rtc);
196
Eric Nelsona9687aa2017-11-01 08:01:20 -0700197 pcf85363->rtc->ops = &rtc_ops;
198
Alexandre Belloni24849d12018-02-12 23:47:29 +0100199 ret = rtc_register_device(pcf85363->rtc);
200
Alexandre Belloni0e7a4122018-02-12 23:47:30 +0100201 nvmem_cfg.priv = pcf85363;
202 rtc_nvmem_register(pcf85363->rtc, &nvmem_cfg);
Alexandre Belloni24849d12018-02-12 23:47:29 +0100203
204 return ret;
Eric Nelsona9687aa2017-11-01 08:01:20 -0700205}
206
207static const struct of_device_id dev_ids[] = {
208 { .compatible = "nxp,pcf85363" },
209 {}
210};
211MODULE_DEVICE_TABLE(of, dev_ids);
212
213static struct i2c_driver pcf85363_driver = {
214 .driver = {
215 .name = "pcf85363",
216 .of_match_table = of_match_ptr(dev_ids),
217 },
218 .probe = pcf85363_probe,
219};
220
221module_i2c_driver(pcf85363_driver);
222
223MODULE_AUTHOR("Eric Nelson");
224MODULE_DESCRIPTION("pcf85363 I2C RTC driver");
225MODULE_LICENSE("GPL");