rtc-mcp795.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * SPI Driver for Microchip MCP795 RTC
  3. *
  4. * Copyright (C) Josef Gajdusek <atx@atx.name>
  5. *
  6. * based on other Linux RTC drivers
  7. *
  8. * Device datasheet:
  9. * http://ww1.microchip.com/downloads/en/DeviceDoc/22280A.pdf
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/device.h>
  19. #include <linux/printk.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/rtc.h>
  22. #include <linux/of.h>
  23. /* MCP795 Instructions, see datasheet table 3-1 */
  24. #define MCP795_EEREAD 0x03
  25. #define MCP795_EEWRITE 0x02
  26. #define MCP795_EEWRDI 0x04
  27. #define MCP795_EEWREN 0x06
  28. #define MCP795_SRREAD 0x05
  29. #define MCP795_SRWRITE 0x01
  30. #define MCP795_READ 0x13
  31. #define MCP795_WRITE 0x12
  32. #define MCP795_UNLOCK 0x14
  33. #define MCP795_IDWRITE 0x32
  34. #define MCP795_IDREAD 0x33
  35. #define MCP795_CLRWDT 0x44
  36. #define MCP795_CLRRAM 0x54
  37. #define MCP795_ST_BIT 0x80
  38. #define MCP795_24_BIT 0x40
  39. static int mcp795_rtcc_read(struct device *dev, u8 addr, u8 *buf, u8 count)
  40. {
  41. struct spi_device *spi = to_spi_device(dev);
  42. int ret;
  43. u8 tx[2];
  44. tx[0] = MCP795_READ;
  45. tx[1] = addr;
  46. ret = spi_write_then_read(spi, tx, sizeof(tx), buf, count);
  47. if (ret)
  48. dev_err(dev, "Failed reading %d bytes from address %x.\n",
  49. count, addr);
  50. return ret;
  51. }
  52. static int mcp795_rtcc_write(struct device *dev, u8 addr, u8 *data, u8 count)
  53. {
  54. struct spi_device *spi = to_spi_device(dev);
  55. int ret;
  56. u8 tx[2 + count];
  57. tx[0] = MCP795_WRITE;
  58. tx[1] = addr;
  59. memcpy(&tx[2], data, count);
  60. ret = spi_write(spi, tx, 2 + count);
  61. if (ret)
  62. dev_err(dev, "Failed to write %d bytes to address %x.\n",
  63. count, addr);
  64. return ret;
  65. }
  66. static int mcp795_rtcc_set_bits(struct device *dev, u8 addr, u8 mask, u8 state)
  67. {
  68. int ret;
  69. u8 tmp;
  70. ret = mcp795_rtcc_read(dev, addr, &tmp, 1);
  71. if (ret)
  72. return ret;
  73. if ((tmp & mask) != state) {
  74. tmp = (tmp & ~mask) | state;
  75. ret = mcp795_rtcc_write(dev, addr, &tmp, 1);
  76. }
  77. return ret;
  78. }
  79. static int mcp795_set_time(struct device *dev, struct rtc_time *tim)
  80. {
  81. int ret;
  82. u8 data[7];
  83. /* Read first, so we can leave config bits untouched */
  84. ret = mcp795_rtcc_read(dev, 0x01, data, sizeof(data));
  85. if (ret)
  86. return ret;
  87. data[0] = (data[0] & 0x80) | ((tim->tm_sec / 10) << 4) | (tim->tm_sec % 10);
  88. data[1] = (data[1] & 0x80) | ((tim->tm_min / 10) << 4) | (tim->tm_min % 10);
  89. data[2] = ((tim->tm_hour / 10) << 4) | (tim->tm_hour % 10);
  90. data[4] = ((tim->tm_mday / 10) << 4) | ((tim->tm_mday) % 10);
  91. data[5] = (data[5] & 0x10) | (tim->tm_mon / 10) | (tim->tm_mon % 10);
  92. if (tim->tm_year > 100)
  93. tim->tm_year -= 100;
  94. data[6] = ((tim->tm_year / 10) << 4) | (tim->tm_year % 10);
  95. ret = mcp795_rtcc_write(dev, 0x01, data, sizeof(data));
  96. if (ret)
  97. return ret;
  98. dev_dbg(dev, "Set mcp795: %04d-%02d-%02d %02d:%02d:%02d\n",
  99. tim->tm_year + 1900, tim->tm_mon, tim->tm_mday,
  100. tim->tm_hour, tim->tm_min, tim->tm_sec);
  101. return 0;
  102. }
  103. static int mcp795_read_time(struct device *dev, struct rtc_time *tim)
  104. {
  105. int ret;
  106. u8 data[7];
  107. ret = mcp795_rtcc_read(dev, 0x01, data, sizeof(data));
  108. if (ret)
  109. return ret;
  110. tim->tm_sec = ((data[0] & 0x70) >> 4) * 10 + (data[0] & 0x0f);
  111. tim->tm_min = ((data[1] & 0x70) >> 4) * 10 + (data[1] & 0x0f);
  112. tim->tm_hour = ((data[2] & 0x30) >> 4) * 10 + (data[2] & 0x0f);
  113. tim->tm_mday = ((data[4] & 0x30) >> 4) * 10 + (data[4] & 0x0f);
  114. tim->tm_mon = ((data[5] & 0x10) >> 4) * 10 + (data[5] & 0x0f);
  115. tim->tm_year = ((data[6] & 0xf0) >> 4) * 10 + (data[6] & 0x0f) + 100; /* Assume we are in 20xx */
  116. dev_dbg(dev, "Read from mcp795: %04d-%02d-%02d %02d:%02d:%02d\n",
  117. tim->tm_year + 1900, tim->tm_mon, tim->tm_mday,
  118. tim->tm_hour, tim->tm_min, tim->tm_sec);
  119. return rtc_valid_tm(tim);
  120. }
  121. static const struct rtc_class_ops mcp795_rtc_ops = {
  122. .read_time = mcp795_read_time,
  123. .set_time = mcp795_set_time
  124. };
  125. static int mcp795_probe(struct spi_device *spi)
  126. {
  127. struct rtc_device *rtc;
  128. int ret;
  129. spi->mode = SPI_MODE_0;
  130. spi->bits_per_word = 8;
  131. ret = spi_setup(spi);
  132. if (ret) {
  133. dev_err(&spi->dev, "Unable to setup SPI\n");
  134. return ret;
  135. }
  136. /* Start the oscillator */
  137. mcp795_rtcc_set_bits(&spi->dev, 0x01, MCP795_ST_BIT, MCP795_ST_BIT);
  138. /* Clear the 12 hour mode flag*/
  139. mcp795_rtcc_set_bits(&spi->dev, 0x03, MCP795_24_BIT, 0);
  140. rtc = devm_rtc_device_register(&spi->dev, "rtc-mcp795",
  141. &mcp795_rtc_ops, THIS_MODULE);
  142. if (IS_ERR(rtc))
  143. return PTR_ERR(rtc);
  144. spi_set_drvdata(spi, rtc);
  145. return 0;
  146. }
  147. #ifdef CONFIG_OF
  148. static const struct of_device_id mcp795_of_match[] = {
  149. { .compatible = "maxim,mcp795" },
  150. { }
  151. };
  152. MODULE_DEVICE_TABLE(of, mcp795_of_match);
  153. #endif
  154. static struct spi_driver mcp795_driver = {
  155. .driver = {
  156. .name = "rtc-mcp795",
  157. .of_match_table = of_match_ptr(mcp795_of_match),
  158. },
  159. .probe = mcp795_probe,
  160. };
  161. module_spi_driver(mcp795_driver);
  162. MODULE_DESCRIPTION("MCP795 RTC SPI Driver");
  163. MODULE_AUTHOR("Josef Gajdusek <atx@atx.name>");
  164. MODULE_LICENSE("GPL");
  165. MODULE_ALIAS("spi:mcp795");