blob: 89402ac11cfaba7136c259cd2e6e1b132e70d94c [file] [log] [blame]
Alexandre Belloni14556f02019-04-19 10:24:59 +02001// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2012 Sven Schnelle <svens@stackframe.org>
Sven Schnelle7418a112012-10-04 17:13:47 -07003
4#include <linux/platform_device.h>
5#include <linux/module.h>
6#include <linux/init.h>
7#include <linux/rtc.h>
8#include <linux/types.h>
9#include <linux/bcd.h>
Alexandre Belloni10c2a2e2016-06-26 23:38:44 +020010#include <linux/platform_data/rtc-ds2404.h>
Sven Schnelle7418a112012-10-04 17:13:47 -070011#include <linux/delay.h>
12#include <linux/gpio.h>
13#include <linux/slab.h>
14
15#include <linux/io.h>
16
17#define DS2404_STATUS_REG 0x200
18#define DS2404_CONTROL_REG 0x201
19#define DS2404_RTC_REG 0x202
20
21#define DS2404_WRITE_SCRATCHPAD_CMD 0x0f
22#define DS2404_READ_SCRATCHPAD_CMD 0xaa
23#define DS2404_COPY_SCRATCHPAD_CMD 0x55
24#define DS2404_READ_MEMORY_CMD 0xf0
25
26struct ds2404;
27
28struct ds2404_chip_ops {
29 int (*map_io)(struct ds2404 *chip, struct platform_device *pdev,
30 struct ds2404_platform_data *pdata);
31 void (*unmap_io)(struct ds2404 *chip);
32};
33
34#define DS2404_RST 0
35#define DS2404_CLK 1
36#define DS2404_DQ 2
37
38struct ds2404_gpio {
39 const char *name;
40 unsigned int gpio;
41};
42
43struct ds2404 {
44 struct ds2404_gpio *gpio;
Julia Lawallc4c23f52015-12-30 21:59:34 +010045 const struct ds2404_chip_ops *ops;
Sven Schnelle7418a112012-10-04 17:13:47 -070046 struct rtc_device *rtc;
47};
48
49static struct ds2404_gpio ds2404_gpio[] = {
50 { "RTC RST", 0 },
51 { "RTC CLK", 0 },
52 { "RTC DQ", 0 },
53};
54
55static int ds2404_gpio_map(struct ds2404 *chip, struct platform_device *pdev,
56 struct ds2404_platform_data *pdata)
57{
58 int i, err;
59
60 ds2404_gpio[DS2404_RST].gpio = pdata->gpio_rst;
61 ds2404_gpio[DS2404_CLK].gpio = pdata->gpio_clk;
62 ds2404_gpio[DS2404_DQ].gpio = pdata->gpio_dq;
63
64 for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++) {
65 err = gpio_request(ds2404_gpio[i].gpio, ds2404_gpio[i].name);
66 if (err) {
Jingoo Han0fae8232013-02-21 16:45:26 -080067 dev_err(&pdev->dev, "error mapping gpio %s: %d\n",
Sven Schnelle7418a112012-10-04 17:13:47 -070068 ds2404_gpio[i].name, err);
69 goto err_request;
70 }
71 if (i != DS2404_DQ)
72 gpio_direction_output(ds2404_gpio[i].gpio, 1);
73 }
74
75 chip->gpio = ds2404_gpio;
76 return 0;
77
78err_request:
79 while (--i >= 0)
80 gpio_free(ds2404_gpio[i].gpio);
81 return err;
82}
83
84static void ds2404_gpio_unmap(struct ds2404 *chip)
85{
86 int i;
87
88 for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++)
89 gpio_free(ds2404_gpio[i].gpio);
90}
91
Julia Lawallc4c23f52015-12-30 21:59:34 +010092static const struct ds2404_chip_ops ds2404_gpio_ops = {
Sven Schnelle7418a112012-10-04 17:13:47 -070093 .map_io = ds2404_gpio_map,
94 .unmap_io = ds2404_gpio_unmap,
95};
96
97static void ds2404_reset(struct device *dev)
98{
99 gpio_set_value(ds2404_gpio[DS2404_RST].gpio, 0);
100 udelay(1000);
101 gpio_set_value(ds2404_gpio[DS2404_RST].gpio, 1);
102 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0);
103 gpio_direction_output(ds2404_gpio[DS2404_DQ].gpio, 0);
104 udelay(10);
105}
106
107static void ds2404_write_byte(struct device *dev, u8 byte)
108{
109 int i;
110
111 gpio_direction_output(ds2404_gpio[DS2404_DQ].gpio, 1);
112 for (i = 0; i < 8; i++) {
113 gpio_set_value(ds2404_gpio[DS2404_DQ].gpio, byte & (1 << i));
114 udelay(10);
115 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1);
116 udelay(10);
117 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0);
118 udelay(10);
119 }
120}
121
122static u8 ds2404_read_byte(struct device *dev)
123{
124 int i;
125 u8 ret = 0;
126
127 gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio);
128
129 for (i = 0; i < 8; i++) {
130 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0);
131 udelay(10);
132 if (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio))
133 ret |= 1 << i;
134 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1);
135 udelay(10);
136 }
137 return ret;
138}
139
140static void ds2404_read_memory(struct device *dev, u16 offset,
141 int length, u8 *out)
142{
143 ds2404_reset(dev);
144 ds2404_write_byte(dev, DS2404_READ_MEMORY_CMD);
145 ds2404_write_byte(dev, offset & 0xff);
146 ds2404_write_byte(dev, (offset >> 8) & 0xff);
147 while (length--)
148 *out++ = ds2404_read_byte(dev);
149}
150
151static void ds2404_write_memory(struct device *dev, u16 offset,
152 int length, u8 *out)
153{
154 int i;
155 u8 ta01, ta02, es;
156
157 ds2404_reset(dev);
158 ds2404_write_byte(dev, DS2404_WRITE_SCRATCHPAD_CMD);
159 ds2404_write_byte(dev, offset & 0xff);
160 ds2404_write_byte(dev, (offset >> 8) & 0xff);
161
162 for (i = 0; i < length; i++)
163 ds2404_write_byte(dev, out[i]);
164
165 ds2404_reset(dev);
166 ds2404_write_byte(dev, DS2404_READ_SCRATCHPAD_CMD);
167
168 ta01 = ds2404_read_byte(dev);
169 ta02 = ds2404_read_byte(dev);
170 es = ds2404_read_byte(dev);
171
172 for (i = 0; i < length; i++) {
173 if (out[i] != ds2404_read_byte(dev)) {
Jingoo Han0fae8232013-02-21 16:45:26 -0800174 dev_err(dev, "read invalid data\n");
Sven Schnelle7418a112012-10-04 17:13:47 -0700175 return;
176 }
177 }
178
179 ds2404_reset(dev);
180 ds2404_write_byte(dev, DS2404_COPY_SCRATCHPAD_CMD);
181 ds2404_write_byte(dev, ta01);
182 ds2404_write_byte(dev, ta02);
183 ds2404_write_byte(dev, es);
184
185 gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio);
186 while (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio))
187 ;
188}
189
190static void ds2404_enable_osc(struct device *dev)
191{
192 u8 in[1] = { 0x10 }; /* enable oscillator */
193 ds2404_write_memory(dev, 0x201, 1, in);
194}
195
196static int ds2404_read_time(struct device *dev, struct rtc_time *dt)
197{
198 unsigned long time = 0;
199
200 ds2404_read_memory(dev, 0x203, 4, (u8 *)&time);
201 time = le32_to_cpu(time);
202
Alexandre Belloni53523212019-04-19 10:24:57 +0200203 rtc_time64_to_tm(time, dt);
Alexandre Belloni22652ba2018-02-19 16:23:56 +0100204 return 0;
Sven Schnelle7418a112012-10-04 17:13:47 -0700205}
206
Alexandre Bellonibe2b0432019-04-19 10:24:58 +0200207static int ds2404_set_time(struct device *dev, struct rtc_time *dt)
Sven Schnelle7418a112012-10-04 17:13:47 -0700208{
Alexandre Bellonibe2b0432019-04-19 10:24:58 +0200209 u32 time = cpu_to_le32(rtc_tm_to_time64(dt));
Sven Schnelle7418a112012-10-04 17:13:47 -0700210 ds2404_write_memory(dev, 0x203, 4, (u8 *)&time);
211 return 0;
212}
213
214static const struct rtc_class_ops ds2404_rtc_ops = {
215 .read_time = ds2404_read_time,
Alexandre Bellonibe2b0432019-04-19 10:24:58 +0200216 .set_time = ds2404_set_time,
Sven Schnelle7418a112012-10-04 17:13:47 -0700217};
218
219static int rtc_probe(struct platform_device *pdev)
220{
Jingoo Han77bf3822013-11-12 15:10:42 -0800221 struct ds2404_platform_data *pdata = dev_get_platdata(&pdev->dev);
Sven Schnelle7418a112012-10-04 17:13:47 -0700222 struct ds2404 *chip;
223 int retval = -EBUSY;
224
Jingoo Han2a444cf2013-04-29 16:20:40 -0700225 chip = devm_kzalloc(&pdev->dev, sizeof(struct ds2404), GFP_KERNEL);
Sven Schnelle7418a112012-10-04 17:13:47 -0700226 if (!chip)
227 return -ENOMEM;
228
229 chip->ops = &ds2404_gpio_ops;
230
Alexandre Belloni13bfa942019-04-19 10:24:56 +0200231 chip->rtc = devm_rtc_allocate_device(&pdev->dev);
232 if (IS_ERR(chip->rtc))
233 return PTR_ERR(chip->rtc);
234
Sven Schnelle7418a112012-10-04 17:13:47 -0700235 retval = chip->ops->map_io(chip, pdev, pdata);
236 if (retval)
237 goto err_chip;
238
239 dev_info(&pdev->dev, "using GPIOs RST:%d, CLK:%d, DQ:%d\n",
240 chip->gpio[DS2404_RST].gpio, chip->gpio[DS2404_CLK].gpio,
241 chip->gpio[DS2404_DQ].gpio);
242
243 platform_set_drvdata(pdev, chip);
244
Alexandre Belloni13bfa942019-04-19 10:24:56 +0200245 chip->rtc->ops = &ds2404_rtc_ops;
246 chip->rtc->range_max = U32_MAX;
247
248 retval = rtc_register_device(chip->rtc);
249 if (retval)
Sven Schnelle7418a112012-10-04 17:13:47 -0700250 goto err_io;
Sven Schnelle7418a112012-10-04 17:13:47 -0700251
252 ds2404_enable_osc(&pdev->dev);
253 return 0;
254
255err_io:
256 chip->ops->unmap_io(chip);
257err_chip:
Sven Schnelle7418a112012-10-04 17:13:47 -0700258 return retval;
259}
260
261static int rtc_remove(struct platform_device *dev)
262{
263 struct ds2404 *chip = platform_get_drvdata(dev);
Sven Schnelle7418a112012-10-04 17:13:47 -0700264
265 chip->ops->unmap_io(chip);
Sven Schnelle7418a112012-10-04 17:13:47 -0700266
267 return 0;
268}
269
270static struct platform_driver rtc_device_driver = {
271 .probe = rtc_probe,
272 .remove = rtc_remove,
273 .driver = {
274 .name = "ds2404",
Sven Schnelle7418a112012-10-04 17:13:47 -0700275 },
276};
Srinivas Kandagatla56ae1b82013-02-21 16:44:31 -0800277module_platform_driver(rtc_device_driver);
Sven Schnelle7418a112012-10-04 17:13:47 -0700278
279MODULE_DESCRIPTION("DS2404 RTC");
280MODULE_AUTHOR("Sven Schnelle");
281MODULE_LICENSE("GPL");
282MODULE_ALIAS("platform:ds2404");