rtc-mcp795.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /* MCP795 Instructions, see datasheet table 3-1 */
  23. #define MCP795_EEREAD 0x03
  24. #define MCP795_EEWRITE 0x02
  25. #define MCP795_EEWRDI 0x04
  26. #define MCP795_EEWREN 0x06
  27. #define MCP795_SRREAD 0x05
  28. #define MCP795_SRWRITE 0x01
  29. #define MCP795_READ 0x13
  30. #define MCP795_WRITE 0x12
  31. #define MCP795_UNLOCK 0x14
  32. #define MCP795_IDWRITE 0x32
  33. #define MCP795_IDREAD 0x33
  34. #define MCP795_CLRWDT 0x44
  35. #define MCP795_CLRRAM 0x54
  36. #define MCP795_ST_BIT 0x80
  37. #define MCP795_24_BIT 0x40
  38. static int mcp795_rtcc_read(struct device *dev, u8 addr, u8 *buf, u8 count)
  39. {
  40. struct spi_device *spi = to_spi_device(dev);
  41. int ret;
  42. u8 tx[2];
  43. tx[0] = MCP795_READ;
  44. tx[1] = addr;
  45. ret = spi_write_then_read(spi, tx, sizeof(tx), buf, count);
  46. if (ret)
  47. dev_err(dev, "Failed reading %d bytes from address %x.\n",
  48. count, addr);
  49. return ret;
  50. }
  51. static int mcp795_rtcc_write(struct device *dev, u8 addr, u8 *data, u8 count)
  52. {
  53. struct spi_device *spi = to_spi_device(dev);
  54. int ret;
  55. u8 tx[2 + count];
  56. tx[0] = MCP795_WRITE;
  57. tx[1] = addr;
  58. memcpy(&tx[2], data, count);
  59. ret = spi_write(spi, tx, 2 + count);
  60. if (ret)
  61. dev_err(dev, "Failed to write %d bytes to address %x.\n",
  62. count, addr);
  63. return ret;
  64. }
  65. static int mcp795_rtcc_set_bits(struct device *dev, u8 addr, u8 mask, u8 state)
  66. {
  67. int ret;
  68. u8 tmp;
  69. ret = mcp795_rtcc_read(dev, addr, &tmp, 1);
  70. if (ret)
  71. return ret;
  72. if ((tmp & mask) != state) {
  73. tmp = (tmp & ~mask) | state;
  74. ret = mcp795_rtcc_write(dev, addr, &tmp, 1);
  75. }
  76. return ret;
  77. }
  78. static int mcp795_set_time(struct device *dev, struct rtc_time *tim)
  79. {
  80. int ret;
  81. u8 data[7];
  82. /* Read first, so we can leave config bits untouched */
  83. ret = mcp795_rtcc_read(dev, 0x01, data, sizeof(data));
  84. if (ret)
  85. return ret;
  86. data[0] = (data[0] & 0x80) | ((tim->tm_sec / 10) << 4) | (tim->tm_sec % 10);
  87. data[1] = (data[1] & 0x80) | ((tim->tm_min / 10) << 4) | (tim->tm_min % 10);
  88. data[2] = ((tim->tm_hour / 10) << 4) | (tim->tm_hour % 10);
  89. data[4] = ((tim->tm_mday / 10) << 4) | ((tim->tm_mday) % 10);
  90. data[5] = (data[5] & 0x10) | (tim->tm_mon / 10) | (tim->tm_mon % 10);
  91. if (tim->tm_year > 100)
  92. tim->tm_year -= 100;
  93. data[6] = ((tim->tm_year / 10) << 4) | (tim->tm_year % 10);
  94. ret = mcp795_rtcc_write(dev, 0x01, data, sizeof(data));
  95. if (ret)
  96. return ret;
  97. dev_dbg(dev, "Set mcp795: %04d-%02d-%02d %02d:%02d:%02d\n",
  98. tim->tm_year + 1900, tim->tm_mon, tim->tm_mday,
  99. tim->tm_hour, tim->tm_min, tim->tm_sec);
  100. return 0;
  101. }
  102. static int mcp795_read_time(struct device *dev, struct rtc_time *tim)
  103. {
  104. int ret;
  105. u8 data[7];
  106. ret = mcp795_rtcc_read(dev, 0x01, data, sizeof(data));
  107. if (ret)
  108. return ret;
  109. tim->tm_sec = ((data[0] & 0x70) >> 4) * 10 + (data[0] & 0x0f);
  110. tim->tm_min = ((data[1] & 0x70) >> 4) * 10 + (data[1] & 0x0f);
  111. tim->tm_hour = ((data[2] & 0x30) >> 4) * 10 + (data[2] & 0x0f);
  112. tim->tm_mday = ((data[4] & 0x30) >> 4) * 10 + (data[4] & 0x0f);
  113. tim->tm_mon = ((data[5] & 0x10) >> 4) * 10 + (data[5] & 0x0f);
  114. tim->tm_year = ((data[6] & 0xf0) >> 4) * 10 + (data[6] & 0x0f) + 100; /* Assume we are in 20xx */
  115. dev_dbg(dev, "Read from mcp795: %04d-%02d-%02d %02d:%02d:%02d\n",
  116. tim->tm_year + 1900, tim->tm_mon, tim->tm_mday,
  117. tim->tm_hour, tim->tm_min, tim->tm_sec);
  118. return rtc_valid_tm(tim);
  119. }
  120. static struct rtc_class_ops mcp795_rtc_ops = {
  121. .read_time = mcp795_read_time,
  122. .set_time = mcp795_set_time
  123. };
  124. static int mcp795_probe(struct spi_device *spi)
  125. {
  126. struct rtc_device *rtc;
  127. int ret;
  128. spi->mode = SPI_MODE_0;
  129. spi->bits_per_word = 8;
  130. ret = spi_setup(spi);
  131. if (ret) {
  132. dev_err(&spi->dev, "Unable to setup SPI\n");
  133. return ret;
  134. }
  135. /* Start the oscillator */
  136. mcp795_rtcc_set_bits(&spi->dev, 0x01, MCP795_ST_BIT, MCP795_ST_BIT);
  137. /* Clear the 12 hour mode flag*/
  138. mcp795_rtcc_set_bits(&spi->dev, 0x03, MCP795_24_BIT, 0);
  139. rtc = devm_rtc_device_register(&spi->dev, "rtc-mcp795",
  140. &mcp795_rtc_ops, THIS_MODULE);
  141. if (IS_ERR(rtc))
  142. return PTR_ERR(rtc);
  143. spi_set_drvdata(spi, rtc);
  144. return 0;
  145. }
  146. static struct spi_driver mcp795_driver = {
  147. .driver = {
  148. .name = "rtc-mcp795",
  149. },
  150. .probe = mcp795_probe,
  151. };
  152. module_spi_driver(mcp795_driver);
  153. MODULE_DESCRIPTION("MCP795 RTC SPI Driver");
  154. MODULE_AUTHOR("Josef Gajdusek <atx@atx.name>");
  155. MODULE_LICENSE("GPL");
  156. MODULE_ALIAS("spi:mcp795");