rtc-max6902.c 6.6 KB

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