Alexandre Belloni | 14556f0 | 2019-04-19 10:24:59 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright (C) 2012 Sven Schnelle <svens@stackframe.org> |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 3 | |
| 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 Belloni | 10c2a2e | 2016-06-26 23:38:44 +0200 | [diff] [blame] | 10 | #include <linux/platform_data/rtc-ds2404.h> |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 11 | #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 | |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 26 | #define DS2404_RST 0 |
| 27 | #define DS2404_CLK 1 |
| 28 | #define DS2404_DQ 2 |
| 29 | |
| 30 | struct ds2404_gpio { |
| 31 | const char *name; |
| 32 | unsigned int gpio; |
| 33 | }; |
| 34 | |
| 35 | struct ds2404 { |
| 36 | struct ds2404_gpio *gpio; |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 37 | struct rtc_device *rtc; |
| 38 | }; |
| 39 | |
| 40 | static struct ds2404_gpio ds2404_gpio[] = { |
| 41 | { "RTC RST", 0 }, |
| 42 | { "RTC CLK", 0 }, |
| 43 | { "RTC DQ", 0 }, |
| 44 | }; |
| 45 | |
| 46 | static int ds2404_gpio_map(struct ds2404 *chip, struct platform_device *pdev, |
| 47 | struct ds2404_platform_data *pdata) |
| 48 | { |
| 49 | int i, err; |
| 50 | |
| 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; |
| 54 | |
| 55 | for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++) { |
| 56 | err = gpio_request(ds2404_gpio[i].gpio, ds2404_gpio[i].name); |
| 57 | if (err) { |
Jingoo Han | 0fae823 | 2013-02-21 16:45:26 -0800 | [diff] [blame] | 58 | dev_err(&pdev->dev, "error mapping gpio %s: %d\n", |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 59 | ds2404_gpio[i].name, err); |
| 60 | goto err_request; |
| 61 | } |
| 62 | if (i != DS2404_DQ) |
| 63 | gpio_direction_output(ds2404_gpio[i].gpio, 1); |
| 64 | } |
| 65 | |
| 66 | chip->gpio = ds2404_gpio; |
| 67 | return 0; |
| 68 | |
| 69 | err_request: |
| 70 | while (--i >= 0) |
| 71 | gpio_free(ds2404_gpio[i].gpio); |
| 72 | return err; |
| 73 | } |
| 74 | |
Alexandre Belloni | d9aa5ca | 2019-04-19 10:25:01 +0200 | [diff] [blame] | 75 | static void ds2404_gpio_unmap(void *data) |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 76 | { |
| 77 | int i; |
| 78 | |
| 79 | for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++) |
| 80 | gpio_free(ds2404_gpio[i].gpio); |
| 81 | } |
| 82 | |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 83 | static void ds2404_reset(struct device *dev) |
| 84 | { |
| 85 | gpio_set_value(ds2404_gpio[DS2404_RST].gpio, 0); |
| 86 | udelay(1000); |
| 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); |
| 90 | udelay(10); |
| 91 | } |
| 92 | |
| 93 | static void ds2404_write_byte(struct device *dev, u8 byte) |
| 94 | { |
| 95 | int i; |
| 96 | |
| 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)); |
| 100 | udelay(10); |
| 101 | gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1); |
| 102 | udelay(10); |
| 103 | gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0); |
| 104 | udelay(10); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static u8 ds2404_read_byte(struct device *dev) |
| 109 | { |
| 110 | int i; |
| 111 | u8 ret = 0; |
| 112 | |
| 113 | gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio); |
| 114 | |
| 115 | for (i = 0; i < 8; i++) { |
| 116 | gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0); |
| 117 | udelay(10); |
| 118 | if (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio)) |
| 119 | ret |= 1 << i; |
| 120 | gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1); |
| 121 | udelay(10); |
| 122 | } |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | static void ds2404_read_memory(struct device *dev, u16 offset, |
| 127 | int length, u8 *out) |
| 128 | { |
| 129 | ds2404_reset(dev); |
| 130 | ds2404_write_byte(dev, DS2404_READ_MEMORY_CMD); |
| 131 | ds2404_write_byte(dev, offset & 0xff); |
| 132 | ds2404_write_byte(dev, (offset >> 8) & 0xff); |
| 133 | while (length--) |
| 134 | *out++ = ds2404_read_byte(dev); |
| 135 | } |
| 136 | |
| 137 | static void ds2404_write_memory(struct device *dev, u16 offset, |
| 138 | int length, u8 *out) |
| 139 | { |
| 140 | int i; |
| 141 | u8 ta01, ta02, es; |
| 142 | |
| 143 | ds2404_reset(dev); |
| 144 | ds2404_write_byte(dev, DS2404_WRITE_SCRATCHPAD_CMD); |
| 145 | ds2404_write_byte(dev, offset & 0xff); |
| 146 | ds2404_write_byte(dev, (offset >> 8) & 0xff); |
| 147 | |
| 148 | for (i = 0; i < length; i++) |
| 149 | ds2404_write_byte(dev, out[i]); |
| 150 | |
| 151 | ds2404_reset(dev); |
| 152 | ds2404_write_byte(dev, DS2404_READ_SCRATCHPAD_CMD); |
| 153 | |
| 154 | ta01 = ds2404_read_byte(dev); |
| 155 | ta02 = ds2404_read_byte(dev); |
| 156 | es = ds2404_read_byte(dev); |
| 157 | |
| 158 | for (i = 0; i < length; i++) { |
| 159 | if (out[i] != ds2404_read_byte(dev)) { |
Jingoo Han | 0fae823 | 2013-02-21 16:45:26 -0800 | [diff] [blame] | 160 | dev_err(dev, "read invalid data\n"); |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | ds2404_reset(dev); |
| 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); |
| 170 | |
| 171 | gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio); |
| 172 | while (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio)) |
| 173 | ; |
| 174 | } |
| 175 | |
| 176 | static void ds2404_enable_osc(struct device *dev) |
| 177 | { |
| 178 | u8 in[1] = { 0x10 }; /* enable oscillator */ |
| 179 | ds2404_write_memory(dev, 0x201, 1, in); |
| 180 | } |
| 181 | |
| 182 | static int ds2404_read_time(struct device *dev, struct rtc_time *dt) |
| 183 | { |
| 184 | unsigned long time = 0; |
Nicholas Mc Guire | 8aec4b8 | 2019-05-09 04:13:55 +0200 | [diff] [blame] | 185 | __le32 hw_time = 0; |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 186 | |
Nicholas Mc Guire | 8aec4b8 | 2019-05-09 04:13:55 +0200 | [diff] [blame] | 187 | ds2404_read_memory(dev, 0x203, 4, (u8 *)&hw_time); |
| 188 | time = le32_to_cpu(hw_time); |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 189 | |
Alexandre Belloni | 5352321 | 2019-04-19 10:24:57 +0200 | [diff] [blame] | 190 | rtc_time64_to_tm(time, dt); |
Alexandre Belloni | 22652ba | 2018-02-19 16:23:56 +0100 | [diff] [blame] | 191 | return 0; |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Alexandre Belloni | be2b043 | 2019-04-19 10:24:58 +0200 | [diff] [blame] | 194 | static int ds2404_set_time(struct device *dev, struct rtc_time *dt) |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 195 | { |
Alexandre Belloni | be2b043 | 2019-04-19 10:24:58 +0200 | [diff] [blame] | 196 | u32 time = cpu_to_le32(rtc_tm_to_time64(dt)); |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 197 | ds2404_write_memory(dev, 0x203, 4, (u8 *)&time); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static const struct rtc_class_ops ds2404_rtc_ops = { |
| 202 | .read_time = ds2404_read_time, |
Alexandre Belloni | be2b043 | 2019-04-19 10:24:58 +0200 | [diff] [blame] | 203 | .set_time = ds2404_set_time, |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | static int rtc_probe(struct platform_device *pdev) |
| 207 | { |
Jingoo Han | 77bf382 | 2013-11-12 15:10:42 -0800 | [diff] [blame] | 208 | struct ds2404_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 209 | struct ds2404 *chip; |
| 210 | int retval = -EBUSY; |
| 211 | |
Jingoo Han | 2a444cf | 2013-04-29 16:20:40 -0700 | [diff] [blame] | 212 | chip = devm_kzalloc(&pdev->dev, sizeof(struct ds2404), GFP_KERNEL); |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 213 | if (!chip) |
| 214 | return -ENOMEM; |
| 215 | |
Alexandre Belloni | 13bfa94 | 2019-04-19 10:24:56 +0200 | [diff] [blame] | 216 | chip->rtc = devm_rtc_allocate_device(&pdev->dev); |
| 217 | if (IS_ERR(chip->rtc)) |
| 218 | return PTR_ERR(chip->rtc); |
| 219 | |
Alexandre Belloni | c7ac260 | 2019-04-19 10:25:00 +0200 | [diff] [blame] | 220 | retval = ds2404_gpio_map(chip, pdev, pdata); |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 221 | if (retval) |
Alexandre Belloni | d9aa5ca | 2019-04-19 10:25:01 +0200 | [diff] [blame] | 222 | return retval; |
| 223 | |
| 224 | retval = devm_add_action_or_reset(&pdev->dev, ds2404_gpio_unmap, chip); |
| 225 | if (retval) |
| 226 | return retval; |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 227 | |
| 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); |
| 231 | |
| 232 | platform_set_drvdata(pdev, chip); |
| 233 | |
Alexandre Belloni | 13bfa94 | 2019-04-19 10:24:56 +0200 | [diff] [blame] | 234 | chip->rtc->ops = &ds2404_rtc_ops; |
| 235 | chip->rtc->range_max = U32_MAX; |
| 236 | |
Bartosz Golaszewski | fdcfd85 | 2020-11-09 17:34:08 +0100 | [diff] [blame] | 237 | retval = devm_rtc_register_device(chip->rtc); |
Alexandre Belloni | 13bfa94 | 2019-04-19 10:24:56 +0200 | [diff] [blame] | 238 | if (retval) |
Alexandre Belloni | d9aa5ca | 2019-04-19 10:25:01 +0200 | [diff] [blame] | 239 | return retval; |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 240 | |
| 241 | ds2404_enable_osc(&pdev->dev); |
| 242 | return 0; |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static struct platform_driver rtc_device_driver = { |
| 246 | .probe = rtc_probe, |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 247 | .driver = { |
| 248 | .name = "ds2404", |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 249 | }, |
| 250 | }; |
Srinivas Kandagatla | 56ae1b8 | 2013-02-21 16:44:31 -0800 | [diff] [blame] | 251 | module_platform_driver(rtc_device_driver); |
Sven Schnelle | 7418a11 | 2012-10-04 17:13:47 -0700 | [diff] [blame] | 252 | |
| 253 | MODULE_DESCRIPTION("DS2404 RTC"); |
| 254 | MODULE_AUTHOR("Sven Schnelle"); |
| 255 | MODULE_LICENSE("GPL"); |
| 256 | MODULE_ALIAS("platform:ds2404"); |