spi-lp8841-rtc.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * SPI master driver for ICP DAS LP-8841 RTC
  3. *
  4. * Copyright (C) 2016 Sergei Ianovich
  5. *
  6. * based on
  7. *
  8. * Dallas DS1302 RTC Support
  9. * Copyright (C) 2002 David McCullough
  10. * Copyright (C) 2003 - 2007 Paul Mundt
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/spi/spi.h>
  29. #define DRIVER_NAME "spi_lp8841_rtc"
  30. #define SPI_LP8841_RTC_CE 0x01
  31. #define SPI_LP8841_RTC_CLK 0x02
  32. #define SPI_LP8841_RTC_nWE 0x04
  33. #define SPI_LP8841_RTC_MOSI 0x08
  34. #define SPI_LP8841_RTC_MISO 0x01
  35. /*
  36. * REVISIT If there is support for SPI_3WIRE and SPI_LSB_FIRST in SPI
  37. * GPIO driver, this SPI driver can be replaced by a simple GPIO driver
  38. * providing 3 GPIO pins.
  39. */
  40. struct spi_lp8841_rtc {
  41. void *iomem;
  42. unsigned long state;
  43. };
  44. static inline void
  45. setsck(struct spi_lp8841_rtc *data, int is_on)
  46. {
  47. if (is_on)
  48. data->state |= SPI_LP8841_RTC_CLK;
  49. else
  50. data->state &= ~SPI_LP8841_RTC_CLK;
  51. writeb(data->state, data->iomem);
  52. }
  53. static inline void
  54. setmosi(struct spi_lp8841_rtc *data, int is_on)
  55. {
  56. if (is_on)
  57. data->state |= SPI_LP8841_RTC_MOSI;
  58. else
  59. data->state &= ~SPI_LP8841_RTC_MOSI;
  60. writeb(data->state, data->iomem);
  61. }
  62. static inline int
  63. getmiso(struct spi_lp8841_rtc *data)
  64. {
  65. return ioread8(data->iomem) & SPI_LP8841_RTC_MISO;
  66. }
  67. static inline u32
  68. bitbang_txrx_be_cpha0_lsb(struct spi_lp8841_rtc *data,
  69. unsigned usecs, unsigned cpol, unsigned flags,
  70. u32 word, u8 bits)
  71. {
  72. /* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */
  73. u32 shift = 32 - bits;
  74. /* clock starts at inactive polarity */
  75. for (; likely(bits); bits--) {
  76. /* setup LSB (to slave) on leading edge */
  77. if ((flags & SPI_MASTER_NO_TX) == 0)
  78. setmosi(data, (word & 1));
  79. usleep_range(usecs, usecs + 1); /* T(setup) */
  80. /* sample LSB (from slave) on trailing edge */
  81. word >>= 1;
  82. if ((flags & SPI_MASTER_NO_RX) == 0)
  83. word |= (getmiso(data) << 31);
  84. setsck(data, !cpol);
  85. usleep_range(usecs, usecs + 1);
  86. setsck(data, cpol);
  87. }
  88. word >>= shift;
  89. return word;
  90. }
  91. static int
  92. spi_lp8841_rtc_transfer_one(struct spi_master *master,
  93. struct spi_device *spi,
  94. struct spi_transfer *t)
  95. {
  96. struct spi_lp8841_rtc *data = spi_master_get_devdata(master);
  97. unsigned count = t->len;
  98. const u8 *tx = t->tx_buf;
  99. u8 *rx = t->rx_buf;
  100. u8 word = 0;
  101. int ret = 0;
  102. if (tx) {
  103. data->state &= ~SPI_LP8841_RTC_nWE;
  104. writeb(data->state, data->iomem);
  105. while (likely(count > 0)) {
  106. word = *tx++;
  107. bitbang_txrx_be_cpha0_lsb(data, 1, 0,
  108. SPI_MASTER_NO_RX, word, 8);
  109. count--;
  110. }
  111. } else if (rx) {
  112. data->state |= SPI_LP8841_RTC_nWE;
  113. writeb(data->state, data->iomem);
  114. while (likely(count > 0)) {
  115. word = bitbang_txrx_be_cpha0_lsb(data, 1, 0,
  116. SPI_MASTER_NO_TX, word, 8);
  117. *rx++ = word;
  118. count--;
  119. }
  120. } else {
  121. ret = -EINVAL;
  122. }
  123. spi_finalize_current_transfer(master);
  124. return ret;
  125. }
  126. static void
  127. spi_lp8841_rtc_set_cs(struct spi_device *spi, bool enable)
  128. {
  129. struct spi_lp8841_rtc *data = spi_master_get_devdata(spi->master);
  130. data->state = 0;
  131. writeb(data->state, data->iomem);
  132. if (enable) {
  133. usleep_range(4, 5);
  134. data->state |= SPI_LP8841_RTC_CE;
  135. writeb(data->state, data->iomem);
  136. usleep_range(4, 5);
  137. }
  138. }
  139. static int
  140. spi_lp8841_rtc_setup(struct spi_device *spi)
  141. {
  142. if ((spi->mode & SPI_CS_HIGH) == 0) {
  143. dev_err(&spi->dev, "unsupported active low chip select\n");
  144. return -EINVAL;
  145. }
  146. if ((spi->mode & SPI_LSB_FIRST) == 0) {
  147. dev_err(&spi->dev, "unsupported MSB first mode\n");
  148. return -EINVAL;
  149. }
  150. if ((spi->mode & SPI_3WIRE) == 0) {
  151. dev_err(&spi->dev, "unsupported wiring. 3 wires required\n");
  152. return -EINVAL;
  153. }
  154. return 0;
  155. }
  156. #ifdef CONFIG_OF
  157. static const struct of_device_id spi_lp8841_rtc_dt_ids[] = {
  158. { .compatible = "icpdas,lp8841-spi-rtc" },
  159. { }
  160. };
  161. MODULE_DEVICE_TABLE(of, spi_lp8841_rtc_dt_ids);
  162. #endif
  163. static int
  164. spi_lp8841_rtc_probe(struct platform_device *pdev)
  165. {
  166. int ret;
  167. struct spi_master *master;
  168. struct spi_lp8841_rtc *data;
  169. void *iomem;
  170. master = spi_alloc_master(&pdev->dev, sizeof(*data));
  171. if (!master)
  172. return -ENOMEM;
  173. platform_set_drvdata(pdev, master);
  174. master->flags = SPI_MASTER_HALF_DUPLEX;
  175. master->mode_bits = SPI_CS_HIGH | SPI_3WIRE | SPI_LSB_FIRST;
  176. master->bus_num = pdev->id;
  177. master->num_chipselect = 1;
  178. master->setup = spi_lp8841_rtc_setup;
  179. master->set_cs = spi_lp8841_rtc_set_cs;
  180. master->transfer_one = spi_lp8841_rtc_transfer_one;
  181. master->bits_per_word_mask = SPI_BPW_MASK(8);
  182. #ifdef CONFIG_OF
  183. master->dev.of_node = pdev->dev.of_node;
  184. #endif
  185. data = spi_master_get_devdata(master);
  186. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  187. data->iomem = devm_ioremap_resource(&pdev->dev, iomem);
  188. ret = PTR_ERR_OR_ZERO(data->iomem);
  189. if (ret) {
  190. dev_err(&pdev->dev, "failed to get IO address\n");
  191. goto err_put_master;
  192. }
  193. /* register with the SPI framework */
  194. ret = devm_spi_register_master(&pdev->dev, master);
  195. if (ret) {
  196. dev_err(&pdev->dev, "cannot register spi master\n");
  197. goto err_put_master;
  198. }
  199. return ret;
  200. err_put_master:
  201. spi_master_put(master);
  202. return ret;
  203. }
  204. MODULE_ALIAS("platform:" DRIVER_NAME);
  205. static struct platform_driver spi_lp8841_rtc_driver = {
  206. .driver = {
  207. .name = DRIVER_NAME,
  208. .of_match_table = of_match_ptr(spi_lp8841_rtc_dt_ids),
  209. },
  210. .probe = spi_lp8841_rtc_probe,
  211. };
  212. module_platform_driver(spi_lp8841_rtc_driver);
  213. MODULE_DESCRIPTION("SPI master driver for ICP DAS LP-8841 RTC");
  214. MODULE_AUTHOR("Sergei Ianovich");
  215. MODULE_LICENSE("GPL");