2 * Copyright (C) 2012 Sven Schnelle <svens@stackframe.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/platform_device.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/rtc.h>
14 #include <linux/types.h>
15 #include <linux/bcd.h>
16 #include <linux/rtc-ds2404.h>
17 #include <linux/delay.h>
18 #include <linux/gpio.h>
19 #include <linux/slab.h>
23 #define DS2404_STATUS_REG 0x200
24 #define DS2404_CONTROL_REG 0x201
25 #define DS2404_RTC_REG 0x202
27 #define DS2404_WRITE_SCRATCHPAD_CMD 0x0f
28 #define DS2404_READ_SCRATCHPAD_CMD 0xaa
29 #define DS2404_COPY_SCRATCHPAD_CMD 0x55
30 #define DS2404_READ_MEMORY_CMD 0xf0
34 struct ds2404_chip_ops
{
35 int (*map_io
)(struct ds2404
*chip
, struct platform_device
*pdev
,
36 struct ds2404_platform_data
*pdata
);
37 void (*unmap_io
)(struct ds2404
*chip
);
50 struct ds2404_gpio
*gpio
;
51 struct ds2404_chip_ops
*ops
;
52 struct rtc_device
*rtc
;
55 static struct ds2404_gpio ds2404_gpio
[] = {
61 static int ds2404_gpio_map(struct ds2404
*chip
, struct platform_device
*pdev
,
62 struct ds2404_platform_data
*pdata
)
66 ds2404_gpio
[DS2404_RST
].gpio
= pdata
->gpio_rst
;
67 ds2404_gpio
[DS2404_CLK
].gpio
= pdata
->gpio_clk
;
68 ds2404_gpio
[DS2404_DQ
].gpio
= pdata
->gpio_dq
;
70 for (i
= 0; i
< ARRAY_SIZE(ds2404_gpio
); i
++) {
71 err
= gpio_request(ds2404_gpio
[i
].gpio
, ds2404_gpio
[i
].name
);
73 dev_err(&pdev
->dev
, "error mapping gpio %s: %d\n",
74 ds2404_gpio
[i
].name
, err
);
78 gpio_direction_output(ds2404_gpio
[i
].gpio
, 1);
81 chip
->gpio
= ds2404_gpio
;
86 gpio_free(ds2404_gpio
[i
].gpio
);
90 static void ds2404_gpio_unmap(struct ds2404
*chip
)
94 for (i
= 0; i
< ARRAY_SIZE(ds2404_gpio
); i
++)
95 gpio_free(ds2404_gpio
[i
].gpio
);
98 static struct ds2404_chip_ops ds2404_gpio_ops
= {
99 .map_io
= ds2404_gpio_map
,
100 .unmap_io
= ds2404_gpio_unmap
,
103 static void ds2404_reset(struct device
*dev
)
105 gpio_set_value(ds2404_gpio
[DS2404_RST
].gpio
, 0);
107 gpio_set_value(ds2404_gpio
[DS2404_RST
].gpio
, 1);
108 gpio_set_value(ds2404_gpio
[DS2404_CLK
].gpio
, 0);
109 gpio_direction_output(ds2404_gpio
[DS2404_DQ
].gpio
, 0);
113 static void ds2404_write_byte(struct device
*dev
, u8 byte
)
117 gpio_direction_output(ds2404_gpio
[DS2404_DQ
].gpio
, 1);
118 for (i
= 0; i
< 8; i
++) {
119 gpio_set_value(ds2404_gpio
[DS2404_DQ
].gpio
, byte
& (1 << i
));
121 gpio_set_value(ds2404_gpio
[DS2404_CLK
].gpio
, 1);
123 gpio_set_value(ds2404_gpio
[DS2404_CLK
].gpio
, 0);
128 static u8
ds2404_read_byte(struct device
*dev
)
133 gpio_direction_input(ds2404_gpio
[DS2404_DQ
].gpio
);
135 for (i
= 0; i
< 8; i
++) {
136 gpio_set_value(ds2404_gpio
[DS2404_CLK
].gpio
, 0);
138 if (gpio_get_value(ds2404_gpio
[DS2404_DQ
].gpio
))
140 gpio_set_value(ds2404_gpio
[DS2404_CLK
].gpio
, 1);
146 static void ds2404_read_memory(struct device
*dev
, u16 offset
,
150 ds2404_write_byte(dev
, DS2404_READ_MEMORY_CMD
);
151 ds2404_write_byte(dev
, offset
& 0xff);
152 ds2404_write_byte(dev
, (offset
>> 8) & 0xff);
154 *out
++ = ds2404_read_byte(dev
);
157 static void ds2404_write_memory(struct device
*dev
, u16 offset
,
164 ds2404_write_byte(dev
, DS2404_WRITE_SCRATCHPAD_CMD
);
165 ds2404_write_byte(dev
, offset
& 0xff);
166 ds2404_write_byte(dev
, (offset
>> 8) & 0xff);
168 for (i
= 0; i
< length
; i
++)
169 ds2404_write_byte(dev
, out
[i
]);
172 ds2404_write_byte(dev
, DS2404_READ_SCRATCHPAD_CMD
);
174 ta01
= ds2404_read_byte(dev
);
175 ta02
= ds2404_read_byte(dev
);
176 es
= ds2404_read_byte(dev
);
178 for (i
= 0; i
< length
; i
++) {
179 if (out
[i
] != ds2404_read_byte(dev
)) {
180 dev_err(dev
, "read invalid data\n");
186 ds2404_write_byte(dev
, DS2404_COPY_SCRATCHPAD_CMD
);
187 ds2404_write_byte(dev
, ta01
);
188 ds2404_write_byte(dev
, ta02
);
189 ds2404_write_byte(dev
, es
);
191 gpio_direction_input(ds2404_gpio
[DS2404_DQ
].gpio
);
192 while (gpio_get_value(ds2404_gpio
[DS2404_DQ
].gpio
))
196 static void ds2404_enable_osc(struct device
*dev
)
198 u8 in
[1] = { 0x10 }; /* enable oscillator */
199 ds2404_write_memory(dev
, 0x201, 1, in
);
202 static int ds2404_read_time(struct device
*dev
, struct rtc_time
*dt
)
204 unsigned long time
= 0;
206 ds2404_read_memory(dev
, 0x203, 4, (u8
*)&time
);
207 time
= le32_to_cpu(time
);
209 rtc_time_to_tm(time
, dt
);
210 return rtc_valid_tm(dt
);
213 static int ds2404_set_mmss(struct device
*dev
, unsigned long secs
)
215 u32 time
= cpu_to_le32(secs
);
216 ds2404_write_memory(dev
, 0x203, 4, (u8
*)&time
);
220 static const struct rtc_class_ops ds2404_rtc_ops
= {
221 .read_time
= ds2404_read_time
,
222 .set_mmss
= ds2404_set_mmss
,
225 static int rtc_probe(struct platform_device
*pdev
)
227 struct ds2404_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
231 chip
= devm_kzalloc(&pdev
->dev
, sizeof(struct ds2404
), GFP_KERNEL
);
235 chip
->ops
= &ds2404_gpio_ops
;
237 retval
= chip
->ops
->map_io(chip
, pdev
, pdata
);
241 dev_info(&pdev
->dev
, "using GPIOs RST:%d, CLK:%d, DQ:%d\n",
242 chip
->gpio
[DS2404_RST
].gpio
, chip
->gpio
[DS2404_CLK
].gpio
,
243 chip
->gpio
[DS2404_DQ
].gpio
);
245 platform_set_drvdata(pdev
, chip
);
247 chip
->rtc
= devm_rtc_device_register(&pdev
->dev
, "ds2404",
248 &ds2404_rtc_ops
, THIS_MODULE
);
249 if (IS_ERR(chip
->rtc
)) {
250 retval
= PTR_ERR(chip
->rtc
);
254 ds2404_enable_osc(&pdev
->dev
);
258 chip
->ops
->unmap_io(chip
);
263 static int rtc_remove(struct platform_device
*dev
)
265 struct ds2404
*chip
= platform_get_drvdata(dev
);
267 chip
->ops
->unmap_io(chip
);
272 static struct platform_driver rtc_device_driver
= {
274 .remove
= rtc_remove
,
277 .owner
= THIS_MODULE
,
280 module_platform_driver(rtc_device_driver
);
282 MODULE_DESCRIPTION("DS2404 RTC");
283 MODULE_AUTHOR("Sven Schnelle");
284 MODULE_LICENSE("GPL");
285 MODULE_ALIAS("platform:ds2404");