rtc-max6902.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* drivers/rtc/rtc-max6902.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 MAX6902 spi RTC
  11. *
  12. * Changelog:
  13. *
  14. * 24-May-2006: Raphael Assenat <raph@8d.com>
  15. * - Major rework
  16. * Converted to rtc_device and uses the SPI layer.
  17. *
  18. * ??-???-2005: Someone at Compulab
  19. * - Initial driver creation.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/init.h>
  25. #include <linux/rtc.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/bcd.h>
  28. #include <linux/delay.h>
  29. #define MAX6902_REG_SECONDS 0x01
  30. #define MAX6902_REG_MINUTES 0x03
  31. #define MAX6902_REG_HOURS 0x05
  32. #define MAX6902_REG_DATE 0x07
  33. #define MAX6902_REG_MONTH 0x09
  34. #define MAX6902_REG_DAY 0x0B
  35. #define MAX6902_REG_YEAR 0x0D
  36. #define MAX6902_REG_CONTROL 0x0F
  37. #define MAX6902_REG_CENTURY 0x13
  38. #undef MAX6902_DEBUG
  39. struct max6902 {
  40. struct rtc_device *rtc;
  41. u8 buf[9]; /* Burst read cmd + 8 registers */
  42. u8 tx_buf[2];
  43. u8 rx_buf[2];
  44. };
  45. static void max6902_set_reg(struct device *dev, unsigned char address,
  46. unsigned char data)
  47. {
  48. struct spi_device *spi = to_spi_device(dev);
  49. unsigned char buf[2];
  50. /* MSB must be '0' to write */
  51. buf[0] = address & 0x7f;
  52. buf[1] = data;
  53. spi_write(spi, buf, 2);
  54. }
  55. static int max6902_get_reg(struct device *dev, unsigned char address,
  56. unsigned char *data)
  57. {
  58. struct spi_device *spi = to_spi_device(dev);
  59. struct max6902 *chip = dev_get_drvdata(dev);
  60. struct spi_message message;
  61. struct spi_transfer xfer;
  62. int status;
  63. if (!data)
  64. return -EINVAL;
  65. /* Build our spi message */
  66. spi_message_init(&message);
  67. memset(&xfer, 0, sizeof(xfer));
  68. xfer.len = 2;
  69. /* Can tx_buf and rx_buf be equal? The doc in spi.h is not sure... */
  70. xfer.tx_buf = chip->tx_buf;
  71. xfer.rx_buf = chip->rx_buf;
  72. /* Set MSB to indicate read */
  73. chip->tx_buf[0] = address | 0x80;
  74. spi_message_add_tail(&xfer, &message);
  75. /* do the i/o */
  76. status = spi_sync(spi, &message);
  77. if (status == 0)
  78. *data = chip->rx_buf[1];
  79. return status;
  80. }
  81. static int max6902_get_datetime(struct device *dev, struct rtc_time *dt)
  82. {
  83. unsigned char tmp;
  84. int century;
  85. int err;
  86. struct spi_device *spi = to_spi_device(dev);
  87. struct max6902 *chip = dev_get_drvdata(dev);
  88. struct spi_message message;
  89. struct spi_transfer xfer;
  90. int status;
  91. err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &tmp);
  92. if (err)
  93. return err;
  94. /* build the message */
  95. spi_message_init(&message);
  96. memset(&xfer, 0, sizeof(xfer));
  97. xfer.len = 1 + 7; /* Burst read command + 7 registers */
  98. xfer.tx_buf = chip->buf;
  99. xfer.rx_buf = chip->buf;
  100. chip->buf[0] = 0xbf; /* Burst read */
  101. spi_message_add_tail(&xfer, &message);
  102. /* do the i/o */
  103. status = spi_sync(spi, &message);
  104. if (status)
  105. return status;
  106. /* The chip sends data in this order:
  107. * Seconds, Minutes, Hours, Date, Month, Day, Year */
  108. dt->tm_sec = BCD2BIN(chip->buf[1]);
  109. dt->tm_min = BCD2BIN(chip->buf[2]);
  110. dt->tm_hour = BCD2BIN(chip->buf[3]);
  111. dt->tm_mday = BCD2BIN(chip->buf[4]);
  112. dt->tm_mon = BCD2BIN(chip->buf[5]) - 1;
  113. dt->tm_wday = BCD2BIN(chip->buf[6]);
  114. dt->tm_year = BCD2BIN(chip->buf[7]);
  115. century = BCD2BIN(tmp) * 100;
  116. dt->tm_year += century;
  117. dt->tm_year -= 1900;
  118. #ifdef MAX6902_DEBUG
  119. printk("\n%s : Read RTC values\n",__func__);
  120. printk("tm_hour: %i\n",dt->tm_hour);
  121. printk("tm_min : %i\n",dt->tm_min);
  122. printk("tm_sec : %i\n",dt->tm_sec);
  123. printk("tm_year: %i\n",dt->tm_year);
  124. printk("tm_mon : %i\n",dt->tm_mon);
  125. printk("tm_mday: %i\n",dt->tm_mday);
  126. printk("tm_wday: %i\n",dt->tm_wday);
  127. #endif
  128. return 0;
  129. }
  130. static int max6902_set_datetime(struct device *dev, struct rtc_time *dt)
  131. {
  132. dt->tm_year = dt->tm_year+1900;
  133. #ifdef MAX6902_DEBUG
  134. printk("\n%s : Setting RTC values\n",__func__);
  135. printk("tm_sec : %i\n",dt->tm_sec);
  136. printk("tm_min : %i\n",dt->tm_min);
  137. printk("tm_hour: %i\n",dt->tm_hour);
  138. printk("tm_mday: %i\n",dt->tm_mday);
  139. printk("tm_wday: %i\n",dt->tm_wday);
  140. printk("tm_year: %i\n",dt->tm_year);
  141. #endif
  142. /* Remove write protection */
  143. max6902_set_reg(dev, 0xF, 0);
  144. max6902_set_reg(dev, 0x01, BIN2BCD(dt->tm_sec));
  145. max6902_set_reg(dev, 0x03, BIN2BCD(dt->tm_min));
  146. max6902_set_reg(dev, 0x05, BIN2BCD(dt->tm_hour));
  147. max6902_set_reg(dev, 0x07, BIN2BCD(dt->tm_mday));
  148. max6902_set_reg(dev, 0x09, BIN2BCD(dt->tm_mon+1));
  149. max6902_set_reg(dev, 0x0B, BIN2BCD(dt->tm_wday));
  150. max6902_set_reg(dev, 0x0D, BIN2BCD(dt->tm_year%100));
  151. max6902_set_reg(dev, 0x13, BIN2BCD(dt->tm_year/100));
  152. /* Compulab used a delay here. However, the datasheet
  153. * does not mention a delay being required anywhere... */
  154. /* delay(2000); */
  155. /* Write protect */
  156. max6902_set_reg(dev, 0xF, 0x80);
  157. return 0;
  158. }
  159. static int max6902_read_time(struct device *dev, struct rtc_time *tm)
  160. {
  161. return max6902_get_datetime(dev, tm);
  162. }
  163. static int max6902_set_time(struct device *dev, struct rtc_time *tm)
  164. {
  165. return max6902_set_datetime(dev, tm);
  166. }
  167. static const struct rtc_class_ops max6902_rtc_ops = {
  168. .read_time = max6902_read_time,
  169. .set_time = max6902_set_time,
  170. };
  171. static int __devinit max6902_probe(struct spi_device *spi)
  172. {
  173. struct rtc_device *rtc;
  174. unsigned char tmp;
  175. struct max6902 *chip;
  176. int res;
  177. rtc = rtc_device_register("max6902",
  178. &spi->dev, &max6902_rtc_ops, THIS_MODULE);
  179. if (IS_ERR(rtc))
  180. return PTR_ERR(rtc);
  181. spi->mode = SPI_MODE_3;
  182. spi->bits_per_word = 8;
  183. spi_setup(spi);
  184. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  185. if (!chip) {
  186. rtc_device_unregister(rtc);
  187. return -ENOMEM;
  188. }
  189. chip->rtc = rtc;
  190. dev_set_drvdata(&spi->dev, chip);
  191. res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
  192. if (res) {
  193. rtc_device_unregister(rtc);
  194. return res;
  195. }
  196. return 0;
  197. }
  198. static int __devexit max6902_remove(struct spi_device *spi)
  199. {
  200. struct max6902 *chip = platform_get_drvdata(spi);
  201. struct rtc_device *rtc = chip->rtc;
  202. if (rtc)
  203. rtc_device_unregister(rtc);
  204. kfree(chip);
  205. return 0;
  206. }
  207. static struct spi_driver max6902_driver = {
  208. .driver = {
  209. .name = "rtc-max6902",
  210. .bus = &spi_bus_type,
  211. .owner = THIS_MODULE,
  212. },
  213. .probe = max6902_probe,
  214. .remove = __devexit_p(max6902_remove),
  215. };
  216. static __init int max6902_init(void)
  217. {
  218. printk("max6902 spi driver\n");
  219. return spi_register_driver(&max6902_driver);
  220. }
  221. module_init(max6902_init);
  222. static __exit void max6902_exit(void)
  223. {
  224. spi_unregister_driver(&max6902_driver);
  225. }
  226. module_exit(max6902_exit);
  227. MODULE_DESCRIPTION ("max6902 spi RTC driver");
  228. MODULE_AUTHOR ("Raphael Assenat");
  229. MODULE_LICENSE ("GPL");