rtc-ds2404.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) 2012 Sven Schnelle <svens@stackframe.org>
  3. *
  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.
  7. *
  8. */
  9. #include <linux/platform_device.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/rtc.h>
  13. #include <linux/types.h>
  14. #include <linux/bcd.h>
  15. #include <linux/rtc-ds2404.h>
  16. #include <linux/delay.h>
  17. #include <linux/gpio.h>
  18. #include <linux/slab.h>
  19. #include <linux/io.h>
  20. #define DS2404_STATUS_REG 0x200
  21. #define DS2404_CONTROL_REG 0x201
  22. #define DS2404_RTC_REG 0x202
  23. #define DS2404_WRITE_SCRATCHPAD_CMD 0x0f
  24. #define DS2404_READ_SCRATCHPAD_CMD 0xaa
  25. #define DS2404_COPY_SCRATCHPAD_CMD 0x55
  26. #define DS2404_READ_MEMORY_CMD 0xf0
  27. struct ds2404;
  28. struct 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. #define DS2404_RST 0
  34. #define DS2404_CLK 1
  35. #define DS2404_DQ 2
  36. struct ds2404_gpio {
  37. const char *name;
  38. unsigned int gpio;
  39. };
  40. struct ds2404 {
  41. struct ds2404_gpio *gpio;
  42. struct ds2404_chip_ops *ops;
  43. struct rtc_device *rtc;
  44. };
  45. static struct ds2404_gpio ds2404_gpio[] = {
  46. { "RTC RST", 0 },
  47. { "RTC CLK", 0 },
  48. { "RTC DQ", 0 },
  49. };
  50. static int ds2404_gpio_map(struct ds2404 *chip, struct platform_device *pdev,
  51. struct ds2404_platform_data *pdata)
  52. {
  53. int i, err;
  54. ds2404_gpio[DS2404_RST].gpio = pdata->gpio_rst;
  55. ds2404_gpio[DS2404_CLK].gpio = pdata->gpio_clk;
  56. ds2404_gpio[DS2404_DQ].gpio = pdata->gpio_dq;
  57. for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++) {
  58. err = gpio_request(ds2404_gpio[i].gpio, ds2404_gpio[i].name);
  59. if (err) {
  60. dev_err(&pdev->dev, "error mapping gpio %s: %d\n",
  61. ds2404_gpio[i].name, err);
  62. goto err_request;
  63. }
  64. if (i != DS2404_DQ)
  65. gpio_direction_output(ds2404_gpio[i].gpio, 1);
  66. }
  67. chip->gpio = ds2404_gpio;
  68. return 0;
  69. err_request:
  70. while (--i >= 0)
  71. gpio_free(ds2404_gpio[i].gpio);
  72. return err;
  73. }
  74. static void ds2404_gpio_unmap(struct ds2404 *chip)
  75. {
  76. int i;
  77. for (i = 0; i < ARRAY_SIZE(ds2404_gpio); i++)
  78. gpio_free(ds2404_gpio[i].gpio);
  79. }
  80. static struct ds2404_chip_ops ds2404_gpio_ops = {
  81. .map_io = ds2404_gpio_map,
  82. .unmap_io = ds2404_gpio_unmap,
  83. };
  84. static void ds2404_reset(struct device *dev)
  85. {
  86. gpio_set_value(ds2404_gpio[DS2404_RST].gpio, 0);
  87. udelay(1000);
  88. gpio_set_value(ds2404_gpio[DS2404_RST].gpio, 1);
  89. gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0);
  90. gpio_direction_output(ds2404_gpio[DS2404_DQ].gpio, 0);
  91. udelay(10);
  92. }
  93. static void ds2404_write_byte(struct device *dev, u8 byte)
  94. {
  95. int i;
  96. gpio_direction_output(ds2404_gpio[DS2404_DQ].gpio, 1);
  97. for (i = 0; i < 8; i++) {
  98. gpio_set_value(ds2404_gpio[DS2404_DQ].gpio, byte & (1 << i));
  99. udelay(10);
  100. gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1);
  101. udelay(10);
  102. gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0);
  103. udelay(10);
  104. }
  105. }
  106. static u8 ds2404_read_byte(struct device *dev)
  107. {
  108. int i;
  109. u8 ret = 0;
  110. gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio);
  111. for (i = 0; i < 8; i++) {
  112. gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 0);
  113. udelay(10);
  114. if (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio))
  115. ret |= 1 << i;
  116. gpio_set_value(ds2404_gpio[DS2404_CLK].gpio, 1);
  117. udelay(10);
  118. }
  119. return ret;
  120. }
  121. static void ds2404_read_memory(struct device *dev, u16 offset,
  122. int length, u8 *out)
  123. {
  124. ds2404_reset(dev);
  125. ds2404_write_byte(dev, DS2404_READ_MEMORY_CMD);
  126. ds2404_write_byte(dev, offset & 0xff);
  127. ds2404_write_byte(dev, (offset >> 8) & 0xff);
  128. while (length--)
  129. *out++ = ds2404_read_byte(dev);
  130. }
  131. static void ds2404_write_memory(struct device *dev, u16 offset,
  132. int length, u8 *out)
  133. {
  134. int i;
  135. u8 ta01, ta02, es;
  136. ds2404_reset(dev);
  137. ds2404_write_byte(dev, DS2404_WRITE_SCRATCHPAD_CMD);
  138. ds2404_write_byte(dev, offset & 0xff);
  139. ds2404_write_byte(dev, (offset >> 8) & 0xff);
  140. for (i = 0; i < length; i++)
  141. ds2404_write_byte(dev, out[i]);
  142. ds2404_reset(dev);
  143. ds2404_write_byte(dev, DS2404_READ_SCRATCHPAD_CMD);
  144. ta01 = ds2404_read_byte(dev);
  145. ta02 = ds2404_read_byte(dev);
  146. es = ds2404_read_byte(dev);
  147. for (i = 0; i < length; i++) {
  148. if (out[i] != ds2404_read_byte(dev)) {
  149. dev_err(dev, "read invalid data\n");
  150. return;
  151. }
  152. }
  153. ds2404_reset(dev);
  154. ds2404_write_byte(dev, DS2404_COPY_SCRATCHPAD_CMD);
  155. ds2404_write_byte(dev, ta01);
  156. ds2404_write_byte(dev, ta02);
  157. ds2404_write_byte(dev, es);
  158. gpio_direction_input(ds2404_gpio[DS2404_DQ].gpio);
  159. while (gpio_get_value(ds2404_gpio[DS2404_DQ].gpio))
  160. ;
  161. }
  162. static void ds2404_enable_osc(struct device *dev)
  163. {
  164. u8 in[1] = { 0x10 }; /* enable oscillator */
  165. ds2404_write_memory(dev, 0x201, 1, in);
  166. }
  167. static int ds2404_read_time(struct device *dev, struct rtc_time *dt)
  168. {
  169. unsigned long time = 0;
  170. ds2404_read_memory(dev, 0x203, 4, (u8 *)&time);
  171. time = le32_to_cpu(time);
  172. rtc_time_to_tm(time, dt);
  173. return rtc_valid_tm(dt);
  174. }
  175. static int ds2404_set_mmss(struct device *dev, unsigned long secs)
  176. {
  177. u32 time = cpu_to_le32(secs);
  178. ds2404_write_memory(dev, 0x203, 4, (u8 *)&time);
  179. return 0;
  180. }
  181. static const struct rtc_class_ops ds2404_rtc_ops = {
  182. .read_time = ds2404_read_time,
  183. .set_mmss = ds2404_set_mmss,
  184. };
  185. static int rtc_probe(struct platform_device *pdev)
  186. {
  187. struct ds2404_platform_data *pdata = dev_get_platdata(&pdev->dev);
  188. struct ds2404 *chip;
  189. int retval = -EBUSY;
  190. chip = devm_kzalloc(&pdev->dev, sizeof(struct ds2404), GFP_KERNEL);
  191. if (!chip)
  192. return -ENOMEM;
  193. chip->ops = &ds2404_gpio_ops;
  194. retval = chip->ops->map_io(chip, pdev, pdata);
  195. if (retval)
  196. goto err_chip;
  197. dev_info(&pdev->dev, "using GPIOs RST:%d, CLK:%d, DQ:%d\n",
  198. chip->gpio[DS2404_RST].gpio, chip->gpio[DS2404_CLK].gpio,
  199. chip->gpio[DS2404_DQ].gpio);
  200. platform_set_drvdata(pdev, chip);
  201. chip->rtc = devm_rtc_device_register(&pdev->dev, "ds2404",
  202. &ds2404_rtc_ops, THIS_MODULE);
  203. if (IS_ERR(chip->rtc)) {
  204. retval = PTR_ERR(chip->rtc);
  205. goto err_io;
  206. }
  207. ds2404_enable_osc(&pdev->dev);
  208. return 0;
  209. err_io:
  210. chip->ops->unmap_io(chip);
  211. err_chip:
  212. return retval;
  213. }
  214. static int rtc_remove(struct platform_device *dev)
  215. {
  216. struct ds2404 *chip = platform_get_drvdata(dev);
  217. chip->ops->unmap_io(chip);
  218. return 0;
  219. }
  220. static struct platform_driver rtc_device_driver = {
  221. .probe = rtc_probe,
  222. .remove = rtc_remove,
  223. .driver = {
  224. .name = "ds2404",
  225. },
  226. };
  227. module_platform_driver(rtc_device_driver);
  228. MODULE_DESCRIPTION("DS2404 RTC");
  229. MODULE_AUTHOR("Sven Schnelle");
  230. MODULE_LICENSE("GPL");
  231. MODULE_ALIAS("platform:ds2404");