1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2012 Sven Schnelle <svens@stackframe.org>
4 #include <linux/platform_device.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
8 #include <linux/types.h>
10 #include <linux/platform_data/rtc-ds2404.h>
11 #include <linux/delay.h>
12 #include <linux/gpio.h>
13 #include <linux/slab.h>
17 #define DS2404_STATUS_REG 0x200
18 #define DS2404_CONTROL_REG 0x201
19 #define DS2404_RTC_REG 0x202
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
36 struct ds2404_gpio *gpio;
37 struct rtc_device *rtc;
40 static struct ds2404_gpio ds2404_gpio[] = {
46 static int ds2404_gpio_map(struct ds2404 *chip, struct platform_device *pdev,
47 struct ds2404_platform_data *pdata)
51 ds2404_gpio[DS2404_RST].gpio = pdata->gpio_rst;
52 ds2404_gpio[DS2404_CLK].gpio = pdata->gpio_clk;
53 ds2404_gpio[DS2404_DQ].gpio = pdata->gpio_dq;
55 for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++) {
56 err = gpio_request(ds2404_gpio[i].gpio, ds2404_gpio[i].name);
58 dev_err(&pdev->dev, "error mapping gpio %s: %d\n",
59 ds2404_gpio[i].name, err);
63 gpio_direction_output(ds2404_gpio[i].gpio, 1);
66 chip->gpio = ds2404_gpio;
71 gpio_free(ds2404_gpio[i].gpio);
75 static void ds2404_gpio_unmap(void *data)
79 for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++)
80 gpio_free(ds2404_gpio[i].gpio);
83 static void ds2404_reset(struct device *dev)
85 gpio_set_value(ds2404_gpio[DS2404_RST].gpio, 0);
87 gpio_set_value(ds2404_gpio[DS2404_RST].gpio, 1);
88 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0);
89 gpio_direction_output(ds2404_gpio[DS2404_DQ].gpio, 0);
93 static void ds2404_write_byte(struct device *dev, u8 byte)
97 gpio_direction_output(ds2404_gpio[DS2404_DQ].gpio, 1);
98 for (i = 0; i < 8; i++) {
99 gpio_set_value(ds2404_gpio[DS2404_DQ].gpio, byte & (1 << i));
101 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1);
103 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0);
108 static u8 ds2404_read_byte(struct device *dev)
113 gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio);
115 for (i = 0; i < 8; i++) {
116 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0);
118 if (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio))
120 gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1);
126 static void ds2404_read_memory(struct device *dev, u16 offset,
130 ds2404_write_byte(dev, DS2404_READ_MEMORY_CMD);
131 ds2404_write_byte(dev, offset & 0xff);
132 ds2404_write_byte(dev, (offset >> 8) & 0xff);
134 *out++ = ds2404_read_byte(dev);
137 static void ds2404_write_memory(struct device *dev, u16 offset,
144 ds2404_write_byte(dev, DS2404_WRITE_SCRATCHPAD_CMD);
145 ds2404_write_byte(dev, offset & 0xff);
146 ds2404_write_byte(dev, (offset >> 8) & 0xff);
148 for (i = 0; i < length; i++)
149 ds2404_write_byte(dev, out[i]);
152 ds2404_write_byte(dev, DS2404_READ_SCRATCHPAD_CMD);
154 ta01 = ds2404_read_byte(dev);
155 ta02 = ds2404_read_byte(dev);
156 es = ds2404_read_byte(dev);
158 for (i = 0; i < length; i++) {
159 if (out[i] != ds2404_read_byte(dev)) {
160 dev_err(dev, "read invalid data\n");
166 ds2404_write_byte(dev, DS2404_COPY_SCRATCHPAD_CMD);
167 ds2404_write_byte(dev, ta01);
168 ds2404_write_byte(dev, ta02);
169 ds2404_write_byte(dev, es);
171 gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio);
172 while (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio))
176 static void ds2404_enable_osc(struct device *dev)
178 u8 in[1] = { 0x10 }; /* enable oscillator */
179 ds2404_write_memory(dev, 0x201, 1, in);
182 static int ds2404_read_time(struct device *dev, struct rtc_time *dt)
184 unsigned long time = 0;
187 ds2404_read_memory(dev, 0x203, 4, (u8 *)&hw_time);
188 time = le32_to_cpu(hw_time);
190 rtc_time64_to_tm(time, dt);
194 static int ds2404_set_time(struct device *dev, struct rtc_time *dt)
196 u32 time = cpu_to_le32(rtc_tm_to_time64(dt));
197 ds2404_write_memory(dev, 0x203, 4, (u8 *)&time);
201 static const struct rtc_class_ops ds2404_rtc_ops = {
202 .read_time = ds2404_read_time,
203 .set_time = ds2404_set_time,
206 static int rtc_probe(struct platform_device *pdev)
208 struct ds2404_platform_data *pdata = dev_get_platdata(&pdev->dev);
212 chip = devm_kzalloc(&pdev->dev, sizeof(struct ds2404), GFP_KERNEL);
216 chip->rtc = devm_rtc_allocate_device(&pdev->dev);
217 if (IS_ERR(chip->rtc))
218 return PTR_ERR(chip->rtc);
220 retval = ds2404_gpio_map(chip, pdev, pdata);
224 retval = devm_add_action_or_reset(&pdev->dev, ds2404_gpio_unmap, chip);
228 dev_info(&pdev->dev, "using GPIOs RST:%d, CLK:%d, DQ:%d\n",
229 chip->gpio[DS2404_RST].gpio, chip->gpio[DS2404_CLK].gpio,
230 chip->gpio[DS2404_DQ].gpio);
232 platform_set_drvdata(pdev, chip);
234 chip->rtc->ops = &ds2404_rtc_ops;
235 chip->rtc->range_max = U32_MAX;
237 retval = devm_rtc_register_device(chip->rtc);
241 ds2404_enable_osc(&pdev->dev);
245 static struct platform_driver rtc_device_driver = {
251 module_platform_driver(rtc_device_driver);
253 MODULE_DESCRIPTION("DS2404 RTC");
254 MODULE_AUTHOR("Sven Schnelle");
255 MODULE_LICENSE("GPL");
256 MODULE_ALIAS("platform:ds2404");