rtc-max6902.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. status = message.status;
  80. else
  81. return status;
  82. *data = chip->rx_buf[1];
  83. return status;
  84. }
  85. static int max6902_get_datetime(struct device *dev, struct rtc_time *dt)
  86. {
  87. unsigned char tmp;
  88. int century;
  89. int err;
  90. struct spi_device *spi = to_spi_device(dev);
  91. struct max6902 *chip = dev_get_drvdata(dev);
  92. struct spi_message message;
  93. struct spi_transfer xfer;
  94. int status;
  95. err = max6902_get_reg(dev, MAX6902_REG_CENTURY, &tmp);
  96. if (err)
  97. return err;
  98. /* build the message */
  99. spi_message_init(&message);
  100. memset(&xfer, 0, sizeof(xfer));
  101. xfer.len = 1 + 7; /* Burst read command + 7 registers */
  102. xfer.tx_buf = chip->buf;
  103. xfer.rx_buf = chip->buf;
  104. chip->buf[0] = 0xbf; /* Burst read */
  105. spi_message_add_tail(&xfer, &message);
  106. /* do the i/o */
  107. status = spi_sync(spi, &message);
  108. if (status == 0)
  109. status = message.status;
  110. else
  111. return status;
  112. /* The chip sends data in this order:
  113. * Seconds, Minutes, Hours, Date, Month, Day, Year */
  114. dt->tm_sec = BCD2BIN(chip->buf[1]);
  115. dt->tm_min = BCD2BIN(chip->buf[2]);
  116. dt->tm_hour = BCD2BIN(chip->buf[3]);
  117. dt->tm_mday = BCD2BIN(chip->buf[4]);
  118. dt->tm_mon = BCD2BIN(chip->buf[5]) - 1;
  119. dt->tm_wday = BCD2BIN(chip->buf[6]);
  120. dt->tm_year = BCD2BIN(chip->buf[7]);
  121. century = BCD2BIN(tmp) * 100;
  122. dt->tm_year += century;
  123. dt->tm_year -= 1900;
  124. #ifdef MAX6902_DEBUG
  125. printk("\n%s : Read RTC values\n",__FUNCTION__);
  126. printk("tm_hour: %i\n",dt->tm_hour);
  127. printk("tm_min : %i\n",dt->tm_min);
  128. printk("tm_sec : %i\n",dt->tm_sec);
  129. printk("tm_year: %i\n",dt->tm_year);
  130. printk("tm_mon : %i\n",dt->tm_mon);
  131. printk("tm_mday: %i\n",dt->tm_mday);
  132. printk("tm_wday: %i\n",dt->tm_wday);
  133. #endif
  134. return 0;
  135. }
  136. static int max6902_set_datetime(struct device *dev, struct rtc_time *dt)
  137. {
  138. dt->tm_year = dt->tm_year+1900;
  139. #ifdef MAX6902_DEBUG
  140. printk("\n%s : Setting RTC values\n",__FUNCTION__);
  141. printk("tm_sec : %i\n",dt->tm_sec);
  142. printk("tm_min : %i\n",dt->tm_min);
  143. printk("tm_hour: %i\n",dt->tm_hour);
  144. printk("tm_mday: %i\n",dt->tm_mday);
  145. printk("tm_wday: %i\n",dt->tm_wday);
  146. printk("tm_year: %i\n",dt->tm_year);
  147. #endif
  148. /* Remove write protection */
  149. max6902_set_reg(dev, 0xF, 0);
  150. max6902_set_reg(dev, 0x01, BIN2BCD(dt->tm_sec));
  151. max6902_set_reg(dev, 0x03, BIN2BCD(dt->tm_min));
  152. max6902_set_reg(dev, 0x05, BIN2BCD(dt->tm_hour));
  153. max6902_set_reg(dev, 0x07, BIN2BCD(dt->tm_mday));
  154. max6902_set_reg(dev, 0x09, BIN2BCD(dt->tm_mon+1));
  155. max6902_set_reg(dev, 0x0B, BIN2BCD(dt->tm_wday));
  156. max6902_set_reg(dev, 0x0D, BIN2BCD(dt->tm_year%100));
  157. max6902_set_reg(dev, 0x13, BIN2BCD(dt->tm_year/100));
  158. /* Compulab used a delay here. However, the datasheet
  159. * does not mention a delay being required anywhere... */
  160. /* delay(2000); */
  161. /* Write protect */
  162. max6902_set_reg(dev, 0xF, 0x80);
  163. return 0;
  164. }
  165. static int max6902_read_time(struct device *dev, struct rtc_time *tm)
  166. {
  167. return max6902_get_datetime(dev, tm);
  168. }
  169. static int max6902_set_time(struct device *dev, struct rtc_time *tm)
  170. {
  171. return max6902_set_datetime(dev, tm);
  172. }
  173. static const struct rtc_class_ops max6902_rtc_ops = {
  174. .read_time = max6902_read_time,
  175. .set_time = max6902_set_time,
  176. };
  177. static int __devinit max6902_probe(struct spi_device *spi)
  178. {
  179. struct rtc_device *rtc;
  180. unsigned char tmp;
  181. struct max6902 *chip;
  182. int res;
  183. rtc = rtc_device_register("max6902",
  184. &spi->dev, &max6902_rtc_ops, THIS_MODULE);
  185. if (IS_ERR(rtc))
  186. return PTR_ERR(rtc);
  187. spi->mode = SPI_MODE_3;
  188. spi->bits_per_word = 8;
  189. spi_setup(spi);
  190. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  191. if (!chip) {
  192. rtc_device_unregister(rtc);
  193. return -ENOMEM;
  194. }
  195. chip->rtc = rtc;
  196. dev_set_drvdata(&spi->dev, chip);
  197. res = max6902_get_reg(&spi->dev, MAX6902_REG_SECONDS, &tmp);
  198. if (res) {
  199. rtc_device_unregister(rtc);
  200. return res;
  201. }
  202. return 0;
  203. }
  204. static int __devexit max6902_remove(struct spi_device *spi)
  205. {
  206. struct max6902 *chip = platform_get_drvdata(spi);
  207. struct rtc_device *rtc = chip->rtc;
  208. if (rtc)
  209. rtc_device_unregister(rtc);
  210. kfree(chip);
  211. return 0;
  212. }
  213. static struct spi_driver max6902_driver = {
  214. .driver = {
  215. .name = "max6902",
  216. .bus = &spi_bus_type,
  217. .owner = THIS_MODULE,
  218. },
  219. .probe = max6902_probe,
  220. .remove = __devexit_p(max6902_remove),
  221. };
  222. static __init int max6902_init(void)
  223. {
  224. printk("max6902 spi driver\n");
  225. return spi_register_driver(&max6902_driver);
  226. }
  227. module_init(max6902_init);
  228. static __exit void max6902_exit(void)
  229. {
  230. spi_unregister_driver(&max6902_driver);
  231. }
  232. module_exit(max6902_exit);
  233. MODULE_DESCRIPTION ("max6902 spi RTC driver");
  234. MODULE_AUTHOR ("Raphael Assenat");
  235. MODULE_LICENSE ("GPL");