spi-xcomm.c 5.8 KB

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