1
0

rtc-ds2404.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2012 Sven Schnelle <svens@stackframe.org>
  3. #include <linux/platform_device.h>
  4. #include <linux/module.h>
  5. #include <linux/init.h>
  6. #include <linux/rtc.h>
  7. #include <linux/types.h>
  8. #include <linux/bcd.h>
  9. #include <linux/delay.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/slab.h>
  12. #include <linux/io.h>
  13. #define DS2404_STATUS_REG 0x200
  14. #define DS2404_CONTROL_REG 0x201
  15. #define DS2404_RTC_REG 0x202
  16. #define DS2404_WRITE_SCRATCHPAD_CMD 0x0f
  17. #define DS2404_READ_SCRATCHPAD_CMD 0xaa
  18. #define DS2404_COPY_SCRATCHPAD_CMD 0x55
  19. #define DS2404_READ_MEMORY_CMD 0xf0
  20. #define DS2404_RST 0
  21. #define DS2404_CLK 1
  22. #define DS2404_DQ 2
  23. struct ds2404 {
  24. struct device *dev;
  25. struct gpio_desc *rst_gpiod;
  26. struct gpio_desc *clk_gpiod;
  27. struct gpio_desc *dq_gpiod;
  28. };
  29. static int ds2404_gpio_map(struct ds2404 *chip, struct platform_device *pdev)
  30. {
  31. struct device *dev = &pdev->dev;
  32. /* This will de-assert RESET, declare this GPIO as GPIOD_ACTIVE_LOW */
  33. chip->rst_gpiod = devm_gpiod_get(dev, "rst", GPIOD_OUT_LOW);
  34. if (IS_ERR(chip->rst_gpiod))
  35. return PTR_ERR(chip->rst_gpiod);
  36. chip->clk_gpiod = devm_gpiod_get(dev, "clk", GPIOD_OUT_HIGH);
  37. if (IS_ERR(chip->clk_gpiod))
  38. return PTR_ERR(chip->clk_gpiod);
  39. chip->dq_gpiod = devm_gpiod_get(dev, "dq", GPIOD_ASIS);
  40. if (IS_ERR(chip->dq_gpiod))
  41. return PTR_ERR(chip->dq_gpiod);
  42. return 0;
  43. }
  44. static void ds2404_reset(struct ds2404 *chip)
  45. {
  46. gpiod_set_value(chip->rst_gpiod, 1);
  47. udelay(1000);
  48. gpiod_set_value(chip->rst_gpiod, 0);
  49. gpiod_set_value(chip->clk_gpiod, 0);
  50. gpiod_direction_output(chip->dq_gpiod, 0);
  51. udelay(10);
  52. }
  53. static void ds2404_write_byte(struct ds2404 *chip, u8 byte)
  54. {
  55. int i;
  56. gpiod_direction_output(chip->dq_gpiod, 1);
  57. for (i = 0; i < 8; i++) {
  58. gpiod_set_value(chip->dq_gpiod, byte & (1 << i));
  59. udelay(10);
  60. gpiod_set_value(chip->clk_gpiod, 1);
  61. udelay(10);
  62. gpiod_set_value(chip->clk_gpiod, 0);
  63. udelay(10);
  64. }
  65. }
  66. static u8 ds2404_read_byte(struct ds2404 *chip)
  67. {
  68. int i;
  69. u8 ret = 0;
  70. gpiod_direction_input(chip->dq_gpiod);
  71. for (i = 0; i < 8; i++) {
  72. gpiod_set_value(chip->clk_gpiod, 0);
  73. udelay(10);
  74. if (gpiod_get_value(chip->dq_gpiod))
  75. ret |= 1 << i;
  76. gpiod_set_value(chip->clk_gpiod, 1);
  77. udelay(10);
  78. }
  79. return ret;
  80. }
  81. static void ds2404_read_memory(struct ds2404 *chip, u16 offset,
  82. int length, u8 *out)
  83. {
  84. ds2404_reset(chip);
  85. ds2404_write_byte(chip, DS2404_READ_MEMORY_CMD);
  86. ds2404_write_byte(chip, offset & 0xff);
  87. ds2404_write_byte(chip, (offset >> 8) & 0xff);
  88. while (length--)
  89. *out++ = ds2404_read_byte(chip);
  90. }
  91. static void ds2404_write_memory(struct ds2404 *chip, u16 offset,
  92. int length, u8 *out)
  93. {
  94. int i;
  95. u8 ta01, ta02, es;
  96. ds2404_reset(chip);
  97. ds2404_write_byte(chip, DS2404_WRITE_SCRATCHPAD_CMD);
  98. ds2404_write_byte(chip, offset & 0xff);
  99. ds2404_write_byte(chip, (offset >> 8) & 0xff);
  100. for (i = 0; i < length; i++)
  101. ds2404_write_byte(chip, out[i]);
  102. ds2404_reset(chip);
  103. ds2404_write_byte(chip, DS2404_READ_SCRATCHPAD_CMD);
  104. ta01 = ds2404_read_byte(chip);
  105. ta02 = ds2404_read_byte(chip);
  106. es = ds2404_read_byte(chip);
  107. for (i = 0; i < length; i++) {
  108. if (out[i] != ds2404_read_byte(chip)) {
  109. dev_err(chip->dev, "read invalid data\n");
  110. return;
  111. }
  112. }
  113. ds2404_reset(chip);
  114. ds2404_write_byte(chip, DS2404_COPY_SCRATCHPAD_CMD);
  115. ds2404_write_byte(chip, ta01);
  116. ds2404_write_byte(chip, ta02);
  117. ds2404_write_byte(chip, es);
  118. while (gpiod_get_value(chip->dq_gpiod))
  119. ;
  120. }
  121. static void ds2404_enable_osc(struct ds2404 *chip)
  122. {
  123. u8 in[1] = { 0x10 }; /* enable oscillator */
  124. ds2404_write_memory(chip, 0x201, 1, in);
  125. }
  126. static int ds2404_read_time(struct device *dev, struct rtc_time *dt)
  127. {
  128. struct ds2404 *chip = dev_get_drvdata(dev);
  129. unsigned long time = 0;
  130. __le32 hw_time = 0;
  131. ds2404_read_memory(chip, 0x203, 4, (u8 *)&hw_time);
  132. time = le32_to_cpu(hw_time);
  133. rtc_time64_to_tm(time, dt);
  134. return 0;
  135. }
  136. static int ds2404_set_time(struct device *dev, struct rtc_time *dt)
  137. {
  138. struct ds2404 *chip = dev_get_drvdata(dev);
  139. u32 time = cpu_to_le32(rtc_tm_to_time64(dt));
  140. ds2404_write_memory(chip, 0x203, 4, (u8 *)&time);
  141. return 0;
  142. }
  143. static const struct rtc_class_ops ds2404_rtc_ops = {
  144. .read_time = ds2404_read_time,
  145. .set_time = ds2404_set_time,
  146. };
  147. static int rtc_probe(struct platform_device *pdev)
  148. {
  149. struct ds2404 *chip;
  150. struct rtc_device *rtc;
  151. int retval = -EBUSY;
  152. chip = devm_kzalloc(&pdev->dev, sizeof(struct ds2404), GFP_KERNEL);
  153. if (!chip)
  154. return -ENOMEM;
  155. chip->dev = &pdev->dev;
  156. rtc = devm_rtc_allocate_device(&pdev->dev);
  157. if (IS_ERR(rtc))
  158. return PTR_ERR(rtc);
  159. retval = ds2404_gpio_map(chip, pdev);
  160. if (retval)
  161. return retval;
  162. platform_set_drvdata(pdev, chip);
  163. rtc->ops = &ds2404_rtc_ops;
  164. rtc->range_max = U32_MAX;
  165. retval = devm_rtc_register_device(rtc);
  166. if (retval)
  167. return retval;
  168. ds2404_enable_osc(chip);
  169. return 0;
  170. }
  171. static struct platform_driver rtc_device_driver = {
  172. .probe = rtc_probe,
  173. .driver = {
  174. .name = "ds2404",
  175. },
  176. };
  177. module_platform_driver(rtc_device_driver);
  178. MODULE_DESCRIPTION("DS2404 RTC");
  179. MODULE_AUTHOR("Sven Schnelle");
  180. MODULE_LICENSE("GPL");
  181. MODULE_ALIAS("platform:ds2404");