spi-xcomm.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver
  4. *
  5. * Copyright 2012 Analog Devices Inc.
  6. * Author: Lars-Peter Clausen <lars@metafoo.de>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/delay.h>
  11. #include <linux/i2c.h>
  12. #include <linux/spi/spi.h>
  13. #include <asm/unaligned.h>
  14. #define SPI_XCOMM_SETTINGS_LEN_OFFSET 10
  15. #define SPI_XCOMM_SETTINGS_3WIRE BIT(6)
  16. #define SPI_XCOMM_SETTINGS_CS_HIGH BIT(5)
  17. #define SPI_XCOMM_SETTINGS_SAMPLE_END BIT(4)
  18. #define SPI_XCOMM_SETTINGS_CPHA BIT(3)
  19. #define SPI_XCOMM_SETTINGS_CPOL BIT(2)
  20. #define SPI_XCOMM_SETTINGS_CLOCK_DIV_MASK 0x3
  21. #define SPI_XCOMM_SETTINGS_CLOCK_DIV_64 0x2
  22. #define SPI_XCOMM_SETTINGS_CLOCK_DIV_16 0x1
  23. #define SPI_XCOMM_SETTINGS_CLOCK_DIV_4 0x0
  24. #define SPI_XCOMM_CMD_UPDATE_CONFIG 0x03
  25. #define SPI_XCOMM_CMD_WRITE 0x04
  26. #define SPI_XCOMM_CLOCK 48000000
  27. struct spi_xcomm {
  28. struct i2c_client *i2c;
  29. uint16_t settings;
  30. uint16_t chipselect;
  31. unsigned int current_speed;
  32. uint8_t buf[63];
  33. };
  34. static int spi_xcomm_sync_config(struct spi_xcomm *spi_xcomm, unsigned int len)
  35. {
  36. uint16_t settings;
  37. uint8_t *buf = spi_xcomm->buf;
  38. settings = spi_xcomm->settings;
  39. settings |= len << SPI_XCOMM_SETTINGS_LEN_OFFSET;
  40. buf[0] = SPI_XCOMM_CMD_UPDATE_CONFIG;
  41. put_unaligned_be16(settings, &buf[1]);
  42. put_unaligned_be16(spi_xcomm->chipselect, &buf[3]);
  43. return i2c_master_send(spi_xcomm->i2c, buf, 5);
  44. }
  45. static void spi_xcomm_chipselect(struct spi_xcomm *spi_xcomm,
  46. struct spi_device *spi, int is_active)
  47. {
  48. unsigned long cs = spi->chip_select;
  49. uint16_t chipselect = spi_xcomm->chipselect;
  50. if (is_active)
  51. chipselect |= BIT(cs);
  52. else
  53. chipselect &= ~BIT(cs);
  54. spi_xcomm->chipselect = chipselect;
  55. }
  56. static int spi_xcomm_setup_transfer(struct spi_xcomm *spi_xcomm,
  57. struct spi_device *spi, struct spi_transfer *t, unsigned int *settings)
  58. {
  59. if (t->len > 62)
  60. return -EINVAL;
  61. if (t->speed_hz != spi_xcomm->current_speed) {
  62. unsigned int divider;
  63. divider = DIV_ROUND_UP(SPI_XCOMM_CLOCK, t->speed_hz);
  64. if (divider >= 64)
  65. *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_64;
  66. else if (divider >= 16)
  67. *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_16;
  68. else
  69. *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_4;
  70. spi_xcomm->current_speed = t->speed_hz;
  71. }
  72. if (spi->mode & SPI_CPOL)
  73. *settings |= SPI_XCOMM_SETTINGS_CPOL;
  74. else
  75. *settings &= ~SPI_XCOMM_SETTINGS_CPOL;
  76. if (spi->mode & SPI_CPHA)
  77. *settings &= ~SPI_XCOMM_SETTINGS_CPHA;
  78. else
  79. *settings |= SPI_XCOMM_SETTINGS_CPHA;
  80. if (spi->mode & SPI_3WIRE)
  81. *settings |= SPI_XCOMM_SETTINGS_3WIRE;
  82. else
  83. *settings &= ~SPI_XCOMM_SETTINGS_3WIRE;
  84. return 0;
  85. }
  86. static int spi_xcomm_txrx_bufs(struct spi_xcomm *spi_xcomm,
  87. struct spi_device *spi, struct spi_transfer *t)
  88. {
  89. int ret;
  90. if (t->tx_buf) {
  91. spi_xcomm->buf[0] = SPI_XCOMM_CMD_WRITE;
  92. memcpy(spi_xcomm->buf + 1, t->tx_buf, t->len);
  93. ret = i2c_master_send(spi_xcomm->i2c, spi_xcomm->buf, t->len + 1);
  94. if (ret < 0)
  95. return ret;
  96. else if (ret != t->len + 1)
  97. return -EIO;
  98. } else if (t->rx_buf) {
  99. ret = i2c_master_recv(spi_xcomm->i2c, t->rx_buf, t->len);
  100. if (ret < 0)
  101. return ret;
  102. else if (ret != t->len)
  103. return -EIO;
  104. }
  105. return t->len;
  106. }
  107. static int spi_xcomm_transfer_one(struct spi_master *master,
  108. struct spi_message *msg)
  109. {
  110. struct spi_xcomm *spi_xcomm = spi_master_get_devdata(master);
  111. unsigned int settings = spi_xcomm->settings;
  112. struct spi_device *spi = msg->spi;
  113. unsigned cs_change = 0;
  114. struct spi_transfer *t;
  115. bool is_first = true;
  116. int status = 0;
  117. bool is_last;
  118. spi_xcomm_chipselect(spi_xcomm, spi, true);
  119. list_for_each_entry(t, &msg->transfers, transfer_list) {
  120. if (!t->tx_buf && !t->rx_buf && t->len) {
  121. status = -EINVAL;
  122. break;
  123. }
  124. status = spi_xcomm_setup_transfer(spi_xcomm, spi, t, &settings);
  125. if (status < 0)
  126. break;
  127. is_last = list_is_last(&t->transfer_list, &msg->transfers);
  128. cs_change = t->cs_change;
  129. if (cs_change ^ is_last)
  130. settings |= BIT(5);
  131. else
  132. settings &= ~BIT(5);
  133. if (t->rx_buf) {
  134. spi_xcomm->settings = settings;
  135. status = spi_xcomm_sync_config(spi_xcomm, t->len);
  136. if (status < 0)
  137. break;
  138. } else if (settings != spi_xcomm->settings || is_first) {
  139. spi_xcomm->settings = settings;
  140. status = spi_xcomm_sync_config(spi_xcomm, 0);
  141. if (status < 0)
  142. break;
  143. }
  144. if (t->len) {
  145. status = spi_xcomm_txrx_bufs(spi_xcomm, spi, t);
  146. if (status < 0)
  147. break;
  148. if (status > 0)
  149. msg->actual_length += status;
  150. }
  151. status = 0;
  152. spi_transfer_delay_exec(t);
  153. is_first = false;
  154. }
  155. if (status != 0 || !cs_change)
  156. spi_xcomm_chipselect(spi_xcomm, spi, false);
  157. msg->status = status;
  158. spi_finalize_current_message(master);
  159. return status;
  160. }
  161. static int spi_xcomm_probe(struct i2c_client *i2c,
  162. const struct i2c_device_id *id)
  163. {
  164. struct spi_xcomm *spi_xcomm;
  165. struct spi_master *master;
  166. int ret;
  167. master = spi_alloc_master(&i2c->dev, sizeof(*spi_xcomm));
  168. if (!master)
  169. return -ENOMEM;
  170. spi_xcomm = spi_master_get_devdata(master);
  171. spi_xcomm->i2c = i2c;
  172. master->num_chipselect = 16;
  173. master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_3WIRE;
  174. master->bits_per_word_mask = SPI_BPW_MASK(8);
  175. master->flags = SPI_MASTER_HALF_DUPLEX;
  176. master->transfer_one_message = spi_xcomm_transfer_one;
  177. master->dev.of_node = i2c->dev.of_node;
  178. i2c_set_clientdata(i2c, master);
  179. ret = devm_spi_register_master(&i2c->dev, master);
  180. if (ret < 0)
  181. spi_master_put(master);
  182. return ret;
  183. }
  184. static const struct i2c_device_id spi_xcomm_ids[] = {
  185. { "spi-xcomm" },
  186. { },
  187. };
  188. MODULE_DEVICE_TABLE(i2c, spi_xcomm_ids);
  189. static struct i2c_driver spi_xcomm_driver = {
  190. .driver = {
  191. .name = "spi-xcomm",
  192. },
  193. .id_table = spi_xcomm_ids,
  194. .probe = spi_xcomm_probe,
  195. };
  196. module_i2c_driver(spi_xcomm_driver);
  197. MODULE_LICENSE("GPL");
  198. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  199. MODULE_DESCRIPTION("Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver");