rtc-v3020.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* drivers/rtc/rtc-v3020.c
  2. *
  3. * Copyright (C) 2006 8D Technologies inc.
  4. * Copyright (C) 2004 Compulab Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Driver for the V3020 RTC
  11. *
  12. * Changelog:
  13. *
  14. * 10-May-2006: Raphael Assenat <raph@8d.com>
  15. * - Converted to platform driver
  16. * - Use the generic rtc class
  17. *
  18. * ??-???-2004: Someone at Compulab
  19. * - Initial driver creation.
  20. *
  21. */
  22. #include <linux/platform_device.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/rtc.h>
  26. #include <linux/types.h>
  27. #include <linux/bcd.h>
  28. #include <linux/rtc-v3020.h>
  29. #include <linux/delay.h>
  30. #include <asm/io.h>
  31. #undef DEBUG
  32. struct v3020 {
  33. void __iomem *ioaddress;
  34. int leftshift;
  35. struct rtc_device *rtc;
  36. };
  37. static void v3020_set_reg(struct v3020 *chip, unsigned char address,
  38. unsigned char data)
  39. {
  40. int i;
  41. unsigned char tmp;
  42. tmp = address;
  43. for (i = 0; i < 4; i++) {
  44. writel((tmp & 1) << chip->leftshift, chip->ioaddress);
  45. tmp >>= 1;
  46. udelay(1);
  47. }
  48. /* Commands dont have data */
  49. if (!V3020_IS_COMMAND(address)) {
  50. for (i = 0; i < 8; i++) {
  51. writel((data & 1) << chip->leftshift, chip->ioaddress);
  52. data >>= 1;
  53. udelay(1);
  54. }
  55. }
  56. }
  57. static unsigned char v3020_get_reg(struct v3020 *chip, unsigned char address)
  58. {
  59. unsigned int data=0;
  60. int i;
  61. for (i = 0; i < 4; i++) {
  62. writel((address & 1) << chip->leftshift, chip->ioaddress);
  63. address >>= 1;
  64. udelay(1);
  65. }
  66. for (i = 0; i < 8; i++) {
  67. data >>= 1;
  68. if (readl(chip->ioaddress) & (1 << chip->leftshift))
  69. data |= 0x80;
  70. udelay(1);
  71. }
  72. return data;
  73. }
  74. static int v3020_read_time(struct device *dev, struct rtc_time *dt)
  75. {
  76. struct v3020 *chip = dev_get_drvdata(dev);
  77. int tmp;
  78. /* Copy the current time to ram... */
  79. v3020_set_reg(chip, V3020_CMD_CLOCK2RAM, 0);
  80. /* ...and then read constant values. */
  81. tmp = v3020_get_reg(chip, V3020_SECONDS);
  82. dt->tm_sec = bcd2bin(tmp);
  83. tmp = v3020_get_reg(chip, V3020_MINUTES);
  84. dt->tm_min = bcd2bin(tmp);
  85. tmp = v3020_get_reg(chip, V3020_HOURS);
  86. dt->tm_hour = bcd2bin(tmp);
  87. tmp = v3020_get_reg(chip, V3020_MONTH_DAY);
  88. dt->tm_mday = bcd2bin(tmp);
  89. tmp = v3020_get_reg(chip, V3020_MONTH);
  90. dt->tm_mon = bcd2bin(tmp) - 1;
  91. tmp = v3020_get_reg(chip, V3020_WEEK_DAY);
  92. dt->tm_wday = bcd2bin(tmp);
  93. tmp = v3020_get_reg(chip, V3020_YEAR);
  94. dt->tm_year = bcd2bin(tmp)+100;
  95. #ifdef DEBUG
  96. printk("\n%s : Read RTC values\n",__func__);
  97. printk("tm_hour: %i\n",dt->tm_hour);
  98. printk("tm_min : %i\n",dt->tm_min);
  99. printk("tm_sec : %i\n",dt->tm_sec);
  100. printk("tm_year: %i\n",dt->tm_year);
  101. printk("tm_mon : %i\n",dt->tm_mon);
  102. printk("tm_mday: %i\n",dt->tm_mday);
  103. printk("tm_wday: %i\n",dt->tm_wday);
  104. #endif
  105. return 0;
  106. }
  107. static int v3020_set_time(struct device *dev, struct rtc_time *dt)
  108. {
  109. struct v3020 *chip = dev_get_drvdata(dev);
  110. #ifdef DEBUG
  111. printk("\n%s : Setting RTC values\n",__func__);
  112. printk("tm_sec : %i\n",dt->tm_sec);
  113. printk("tm_min : %i\n",dt->tm_min);
  114. printk("tm_hour: %i\n",dt->tm_hour);
  115. printk("tm_mday: %i\n",dt->tm_mday);
  116. printk("tm_wday: %i\n",dt->tm_wday);
  117. printk("tm_year: %i\n",dt->tm_year);
  118. #endif
  119. /* Write all the values to ram... */
  120. v3020_set_reg(chip, V3020_SECONDS, bin2bcd(dt->tm_sec));
  121. v3020_set_reg(chip, V3020_MINUTES, bin2bcd(dt->tm_min));
  122. v3020_set_reg(chip, V3020_HOURS, bin2bcd(dt->tm_hour));
  123. v3020_set_reg(chip, V3020_MONTH_DAY, bin2bcd(dt->tm_mday));
  124. v3020_set_reg(chip, V3020_MONTH, bin2bcd(dt->tm_mon + 1));
  125. v3020_set_reg(chip, V3020_WEEK_DAY, bin2bcd(dt->tm_wday));
  126. v3020_set_reg(chip, V3020_YEAR, bin2bcd(dt->tm_year % 100));
  127. /* ...and set the clock. */
  128. v3020_set_reg(chip, V3020_CMD_RAM2CLOCK, 0);
  129. /* Compulab used this delay here. I dont know why,
  130. * the datasheet does not specify a delay. */
  131. /*mdelay(5);*/
  132. return 0;
  133. }
  134. static const struct rtc_class_ops v3020_rtc_ops = {
  135. .read_time = v3020_read_time,
  136. .set_time = v3020_set_time,
  137. };
  138. static int rtc_probe(struct platform_device *pdev)
  139. {
  140. struct v3020_platform_data *pdata = pdev->dev.platform_data;
  141. struct v3020 *chip;
  142. struct rtc_device *rtc;
  143. int retval = -EBUSY;
  144. int i;
  145. int temp;
  146. if (pdev->num_resources != 1)
  147. return -EBUSY;
  148. if (pdev->resource[0].flags != IORESOURCE_MEM)
  149. return -EBUSY;
  150. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  151. if (!chip)
  152. return -ENOMEM;
  153. chip->leftshift = pdata->leftshift;
  154. chip->ioaddress = ioremap(pdev->resource[0].start, 1);
  155. if (chip->ioaddress == NULL)
  156. goto err_chip;
  157. /* Make sure the v3020 expects a communication cycle
  158. * by reading 8 times */
  159. for (i = 0; i < 8; i++)
  160. temp = readl(chip->ioaddress);
  161. /* Test chip by doing a write/read sequence
  162. * to the chip ram */
  163. v3020_set_reg(chip, V3020_SECONDS, 0x33);
  164. if(v3020_get_reg(chip, V3020_SECONDS) != 0x33) {
  165. retval = -ENODEV;
  166. goto err_io;
  167. }
  168. /* Make sure frequency measurment mode, test modes, and lock
  169. * are all disabled */
  170. v3020_set_reg(chip, V3020_STATUS_0, 0x0);
  171. dev_info(&pdev->dev, "Chip available at physical address 0x%llx,"
  172. "data connected to D%d\n",
  173. (unsigned long long)pdev->resource[0].start,
  174. chip->leftshift);
  175. platform_set_drvdata(pdev, chip);
  176. rtc = rtc_device_register("v3020",
  177. &pdev->dev, &v3020_rtc_ops, THIS_MODULE);
  178. if (IS_ERR(rtc)) {
  179. retval = PTR_ERR(rtc);
  180. goto err_io;
  181. }
  182. chip->rtc = rtc;
  183. return 0;
  184. err_io:
  185. iounmap(chip->ioaddress);
  186. err_chip:
  187. kfree(chip);
  188. return retval;
  189. }
  190. static int rtc_remove(struct platform_device *dev)
  191. {
  192. struct v3020 *chip = platform_get_drvdata(dev);
  193. struct rtc_device *rtc = chip->rtc;
  194. if (rtc)
  195. rtc_device_unregister(rtc);
  196. iounmap(chip->ioaddress);
  197. kfree(chip);
  198. return 0;
  199. }
  200. static struct platform_driver rtc_device_driver = {
  201. .probe = rtc_probe,
  202. .remove = rtc_remove,
  203. .driver = {
  204. .name = "v3020",
  205. .owner = THIS_MODULE,
  206. },
  207. };
  208. static __init int v3020_init(void)
  209. {
  210. return platform_driver_register(&rtc_device_driver);
  211. }
  212. static __exit void v3020_exit(void)
  213. {
  214. platform_driver_unregister(&rtc_device_driver);
  215. }
  216. module_init(v3020_init);
  217. module_exit(v3020_exit);
  218. MODULE_DESCRIPTION("V3020 RTC");
  219. MODULE_AUTHOR("Raphael Assenat");
  220. MODULE_LICENSE("GPL");
  221. MODULE_ALIAS("platform:v3020");