rtc-moxart.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * MOXA ART RTC driver.
  3. *
  4. * Copyright (C) 2013 Jonas Jensen
  5. *
  6. * Jonas Jensen <jonas.jensen@gmail.com>
  7. *
  8. * Based on code from
  9. * Moxa Technology Co., Ltd. <www.moxa.com>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/delay.h>
  18. #include <linux/rtc.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/module.h>
  21. #include <linux/gpio.h>
  22. #include <linux/of_gpio.h>
  23. #define GPIO_RTC_RESERVED 0x0C
  24. #define GPIO_RTC_DATA_SET 0x10
  25. #define GPIO_RTC_DATA_CLEAR 0x14
  26. #define GPIO_RTC_PIN_PULL_ENABLE 0x18
  27. #define GPIO_RTC_PIN_PULL_TYPE 0x1C
  28. #define GPIO_RTC_INT_ENABLE 0x20
  29. #define GPIO_RTC_INT_RAW_STATE 0x24
  30. #define GPIO_RTC_INT_MASKED_STATE 0x28
  31. #define GPIO_RTC_INT_MASK 0x2C
  32. #define GPIO_RTC_INT_CLEAR 0x30
  33. #define GPIO_RTC_INT_TRIGGER 0x34
  34. #define GPIO_RTC_INT_BOTH 0x38
  35. #define GPIO_RTC_INT_RISE_NEG 0x3C
  36. #define GPIO_RTC_BOUNCE_ENABLE 0x40
  37. #define GPIO_RTC_BOUNCE_PRE_SCALE 0x44
  38. #define GPIO_RTC_PROTECT_W 0x8E
  39. #define GPIO_RTC_PROTECT_R 0x8F
  40. #define GPIO_RTC_YEAR_W 0x8C
  41. #define GPIO_RTC_YEAR_R 0x8D
  42. #define GPIO_RTC_DAY_W 0x8A
  43. #define GPIO_RTC_DAY_R 0x8B
  44. #define GPIO_RTC_MONTH_W 0x88
  45. #define GPIO_RTC_MONTH_R 0x89
  46. #define GPIO_RTC_DATE_W 0x86
  47. #define GPIO_RTC_DATE_R 0x87
  48. #define GPIO_RTC_HOURS_W 0x84
  49. #define GPIO_RTC_HOURS_R 0x85
  50. #define GPIO_RTC_MINUTES_W 0x82
  51. #define GPIO_RTC_MINUTES_R 0x83
  52. #define GPIO_RTC_SECONDS_W 0x80
  53. #define GPIO_RTC_SECONDS_R 0x81
  54. #define GPIO_RTC_DELAY_TIME 8
  55. struct moxart_rtc {
  56. struct rtc_device *rtc;
  57. spinlock_t rtc_lock;
  58. int gpio_data, gpio_sclk, gpio_reset;
  59. };
  60. static int day_of_year[12] = { 0, 31, 59, 90, 120, 151, 181,
  61. 212, 243, 273, 304, 334 };
  62. static void moxart_rtc_write_byte(struct device *dev, u8 data)
  63. {
  64. struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
  65. int i;
  66. for (i = 0; i < 8; i++, data >>= 1) {
  67. gpio_set_value(moxart_rtc->gpio_sclk, 0);
  68. gpio_set_value(moxart_rtc->gpio_data, ((data & 1) == 1));
  69. udelay(GPIO_RTC_DELAY_TIME);
  70. gpio_set_value(moxart_rtc->gpio_sclk, 1);
  71. udelay(GPIO_RTC_DELAY_TIME);
  72. }
  73. }
  74. static u8 moxart_rtc_read_byte(struct device *dev)
  75. {
  76. struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
  77. int i;
  78. u8 data = 0;
  79. for (i = 0; i < 8; i++) {
  80. gpio_set_value(moxart_rtc->gpio_sclk, 0);
  81. udelay(GPIO_RTC_DELAY_TIME);
  82. gpio_set_value(moxart_rtc->gpio_sclk, 1);
  83. udelay(GPIO_RTC_DELAY_TIME);
  84. if (gpio_get_value(moxart_rtc->gpio_data))
  85. data |= (1 << i);
  86. udelay(GPIO_RTC_DELAY_TIME);
  87. }
  88. return data;
  89. }
  90. static u8 moxart_rtc_read_register(struct device *dev, u8 cmd)
  91. {
  92. struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
  93. u8 data;
  94. unsigned long flags;
  95. local_irq_save(flags);
  96. gpio_direction_output(moxart_rtc->gpio_data, 0);
  97. gpio_set_value(moxart_rtc->gpio_reset, 1);
  98. udelay(GPIO_RTC_DELAY_TIME);
  99. moxart_rtc_write_byte(dev, cmd);
  100. gpio_direction_input(moxart_rtc->gpio_data);
  101. udelay(GPIO_RTC_DELAY_TIME);
  102. data = moxart_rtc_read_byte(dev);
  103. gpio_set_value(moxart_rtc->gpio_sclk, 0);
  104. gpio_set_value(moxart_rtc->gpio_reset, 0);
  105. udelay(GPIO_RTC_DELAY_TIME);
  106. local_irq_restore(flags);
  107. return data;
  108. }
  109. static void moxart_rtc_write_register(struct device *dev, u8 cmd, u8 data)
  110. {
  111. struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
  112. unsigned long flags;
  113. local_irq_save(flags);
  114. gpio_direction_output(moxart_rtc->gpio_data, 0);
  115. gpio_set_value(moxart_rtc->gpio_reset, 1);
  116. udelay(GPIO_RTC_DELAY_TIME);
  117. moxart_rtc_write_byte(dev, cmd);
  118. moxart_rtc_write_byte(dev, data);
  119. gpio_set_value(moxart_rtc->gpio_sclk, 0);
  120. gpio_set_value(moxart_rtc->gpio_reset, 0);
  121. udelay(GPIO_RTC_DELAY_TIME);
  122. local_irq_restore(flags);
  123. }
  124. static int moxart_rtc_set_time(struct device *dev, struct rtc_time *tm)
  125. {
  126. struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
  127. spin_lock_irq(&moxart_rtc->rtc_lock);
  128. moxart_rtc_write_register(dev, GPIO_RTC_PROTECT_W, 0);
  129. moxart_rtc_write_register(dev, GPIO_RTC_YEAR_W,
  130. (((tm->tm_year - 100) / 10) << 4) |
  131. ((tm->tm_year - 100) % 10));
  132. moxart_rtc_write_register(dev, GPIO_RTC_MONTH_W,
  133. (((tm->tm_mon + 1) / 10) << 4) |
  134. ((tm->tm_mon + 1) % 10));
  135. moxart_rtc_write_register(dev, GPIO_RTC_DATE_W,
  136. ((tm->tm_mday / 10) << 4) |
  137. (tm->tm_mday % 10));
  138. moxart_rtc_write_register(dev, GPIO_RTC_HOURS_W,
  139. ((tm->tm_hour / 10) << 4) |
  140. (tm->tm_hour % 10));
  141. moxart_rtc_write_register(dev, GPIO_RTC_MINUTES_W,
  142. ((tm->tm_min / 10) << 4) |
  143. (tm->tm_min % 10));
  144. moxart_rtc_write_register(dev, GPIO_RTC_SECONDS_W,
  145. ((tm->tm_sec / 10) << 4) |
  146. (tm->tm_sec % 10));
  147. moxart_rtc_write_register(dev, GPIO_RTC_PROTECT_W, 0x80);
  148. spin_unlock_irq(&moxart_rtc->rtc_lock);
  149. dev_dbg(dev, "%s: success tm_year=%d tm_mon=%d\n"
  150. "tm_mday=%d tm_hour=%d tm_min=%d tm_sec=%d\n",
  151. __func__, tm->tm_year, tm->tm_mon, tm->tm_mday,
  152. tm->tm_hour, tm->tm_min, tm->tm_sec);
  153. return 0;
  154. }
  155. static int moxart_rtc_read_time(struct device *dev, struct rtc_time *tm)
  156. {
  157. struct moxart_rtc *moxart_rtc = dev_get_drvdata(dev);
  158. unsigned char v;
  159. spin_lock_irq(&moxart_rtc->rtc_lock);
  160. v = moxart_rtc_read_register(dev, GPIO_RTC_SECONDS_R);
  161. tm->tm_sec = (((v & 0x70) >> 4) * 10) + (v & 0x0F);
  162. v = moxart_rtc_read_register(dev, GPIO_RTC_MINUTES_R);
  163. tm->tm_min = (((v & 0x70) >> 4) * 10) + (v & 0x0F);
  164. v = moxart_rtc_read_register(dev, GPIO_RTC_HOURS_R);
  165. if (v & 0x80) { /* 12-hour mode */
  166. tm->tm_hour = (((v & 0x10) >> 4) * 10) + (v & 0x0F);
  167. if (v & 0x20) { /* PM mode */
  168. tm->tm_hour += 12;
  169. if (tm->tm_hour >= 24)
  170. tm->tm_hour = 0;
  171. }
  172. } else { /* 24-hour mode */
  173. tm->tm_hour = (((v & 0x30) >> 4) * 10) + (v & 0x0F);
  174. }
  175. v = moxart_rtc_read_register(dev, GPIO_RTC_DATE_R);
  176. tm->tm_mday = (((v & 0x30) >> 4) * 10) + (v & 0x0F);
  177. v = moxart_rtc_read_register(dev, GPIO_RTC_MONTH_R);
  178. tm->tm_mon = (((v & 0x10) >> 4) * 10) + (v & 0x0F);
  179. tm->tm_mon--;
  180. v = moxart_rtc_read_register(dev, GPIO_RTC_YEAR_R);
  181. tm->tm_year = (((v & 0xF0) >> 4) * 10) + (v & 0x0F);
  182. tm->tm_year += 100;
  183. if (tm->tm_year <= 69)
  184. tm->tm_year += 100;
  185. v = moxart_rtc_read_register(dev, GPIO_RTC_DAY_R);
  186. tm->tm_wday = (v & 0x0f) - 1;
  187. tm->tm_yday = day_of_year[tm->tm_mon];
  188. tm->tm_yday += (tm->tm_mday - 1);
  189. if (tm->tm_mon >= 2) {
  190. if (!(tm->tm_year % 4) && (tm->tm_year % 100))
  191. tm->tm_yday++;
  192. }
  193. tm->tm_isdst = 0;
  194. spin_unlock_irq(&moxart_rtc->rtc_lock);
  195. return 0;
  196. }
  197. static const struct rtc_class_ops moxart_rtc_ops = {
  198. .read_time = moxart_rtc_read_time,
  199. .set_time = moxart_rtc_set_time,
  200. };
  201. static int moxart_rtc_probe(struct platform_device *pdev)
  202. {
  203. struct moxart_rtc *moxart_rtc;
  204. int ret = 0;
  205. moxart_rtc = devm_kzalloc(&pdev->dev, sizeof(*moxart_rtc), GFP_KERNEL);
  206. if (!moxart_rtc)
  207. return -ENOMEM;
  208. moxart_rtc->gpio_data = of_get_named_gpio(pdev->dev.of_node,
  209. "gpio-rtc-data", 0);
  210. if (!gpio_is_valid(moxart_rtc->gpio_data)) {
  211. dev_err(&pdev->dev, "invalid gpio (data): %d\n",
  212. moxart_rtc->gpio_data);
  213. return moxart_rtc->gpio_data;
  214. }
  215. moxart_rtc->gpio_sclk = of_get_named_gpio(pdev->dev.of_node,
  216. "gpio-rtc-sclk", 0);
  217. if (!gpio_is_valid(moxart_rtc->gpio_sclk)) {
  218. dev_err(&pdev->dev, "invalid gpio (sclk): %d\n",
  219. moxart_rtc->gpio_sclk);
  220. return moxart_rtc->gpio_sclk;
  221. }
  222. moxart_rtc->gpio_reset = of_get_named_gpio(pdev->dev.of_node,
  223. "gpio-rtc-reset", 0);
  224. if (!gpio_is_valid(moxart_rtc->gpio_reset)) {
  225. dev_err(&pdev->dev, "invalid gpio (reset): %d\n",
  226. moxart_rtc->gpio_reset);
  227. return moxart_rtc->gpio_reset;
  228. }
  229. spin_lock_init(&moxart_rtc->rtc_lock);
  230. platform_set_drvdata(pdev, moxart_rtc);
  231. ret = devm_gpio_request(&pdev->dev, moxart_rtc->gpio_data, "rtc_data");
  232. if (ret) {
  233. dev_err(&pdev->dev, "can't get rtc_data gpio\n");
  234. return ret;
  235. }
  236. ret = devm_gpio_request_one(&pdev->dev, moxart_rtc->gpio_sclk,
  237. GPIOF_DIR_OUT, "rtc_sclk");
  238. if (ret) {
  239. dev_err(&pdev->dev, "can't get rtc_sclk gpio\n");
  240. return ret;
  241. }
  242. ret = devm_gpio_request_one(&pdev->dev, moxart_rtc->gpio_reset,
  243. GPIOF_DIR_OUT, "rtc_reset");
  244. if (ret) {
  245. dev_err(&pdev->dev, "can't get rtc_reset gpio\n");
  246. return ret;
  247. }
  248. moxart_rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  249. &moxart_rtc_ops,
  250. THIS_MODULE);
  251. if (IS_ERR(moxart_rtc->rtc)) {
  252. dev_err(&pdev->dev, "devm_rtc_device_register failed\n");
  253. return PTR_ERR(moxart_rtc->rtc);
  254. }
  255. return 0;
  256. }
  257. static const struct of_device_id moxart_rtc_match[] = {
  258. { .compatible = "moxa,moxart-rtc" },
  259. { },
  260. };
  261. MODULE_DEVICE_TABLE(of, moxart_rtc_match);
  262. static struct platform_driver moxart_rtc_driver = {
  263. .probe = moxart_rtc_probe,
  264. .driver = {
  265. .name = "moxart-rtc",
  266. .of_match_table = moxart_rtc_match,
  267. },
  268. };
  269. module_platform_driver(moxart_rtc_driver);
  270. MODULE_DESCRIPTION("MOXART RTC driver");
  271. MODULE_LICENSE("GPL");
  272. MODULE_AUTHOR("Jonas Jensen <jonas.jensen@gmail.com>");