spi-xcomm.c 5.8 KB

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