rtc-v3020.c 6.0 KB

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