rtc-max6902.c 6.5 KB

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