spi-xcomm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. unsigned int speed;
  62. if ((t->bits_per_word && t->bits_per_word != 8) || t->len > 62)
  63. return -EINVAL;
  64. speed = t->speed_hz ? t->speed_hz : spi->max_speed_hz;
  65. if (speed != spi_xcomm->current_speed) {
  66. unsigned int divider = DIV_ROUND_UP(SPI_XCOMM_CLOCK, speed);
  67. if (divider >= 64)
  68. *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_64;
  69. else if (divider >= 16)
  70. *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_16;
  71. else
  72. *settings |= SPI_XCOMM_SETTINGS_CLOCK_DIV_4;
  73. spi_xcomm->current_speed = speed;
  74. }
  75. if (spi->mode & SPI_CPOL)
  76. *settings |= SPI_XCOMM_SETTINGS_CPOL;
  77. else
  78. *settings &= ~SPI_XCOMM_SETTINGS_CPOL;
  79. if (spi->mode & SPI_CPHA)
  80. *settings &= ~SPI_XCOMM_SETTINGS_CPHA;
  81. else
  82. *settings |= SPI_XCOMM_SETTINGS_CPHA;
  83. if (spi->mode & SPI_3WIRE)
  84. *settings |= SPI_XCOMM_SETTINGS_3WIRE;
  85. else
  86. *settings &= ~SPI_XCOMM_SETTINGS_3WIRE;
  87. return 0;
  88. }
  89. static int spi_xcomm_txrx_bufs(struct spi_xcomm *spi_xcomm,
  90. struct spi_device *spi, struct spi_transfer *t)
  91. {
  92. int ret;
  93. if (t->tx_buf) {
  94. spi_xcomm->buf[0] = SPI_XCOMM_CMD_WRITE;
  95. memcpy(spi_xcomm->buf + 1, t->tx_buf, t->len);
  96. ret = i2c_master_send(spi_xcomm->i2c, spi_xcomm->buf, t->len + 1);
  97. if (ret < 0)
  98. return ret;
  99. else if (ret != t->len + 1)
  100. return -EIO;
  101. } else if (t->rx_buf) {
  102. ret = i2c_master_recv(spi_xcomm->i2c, t->rx_buf, t->len);
  103. if (ret < 0)
  104. return ret;
  105. else if (ret != t->len)
  106. return -EIO;
  107. }
  108. return t->len;
  109. }
  110. static int spi_xcomm_transfer_one(struct spi_master *master,
  111. struct spi_message *msg)
  112. {
  113. struct spi_xcomm *spi_xcomm = spi_master_get_devdata(master);
  114. unsigned int settings = spi_xcomm->settings;
  115. struct spi_device *spi = msg->spi;
  116. unsigned cs_change = 0;
  117. struct spi_transfer *t;
  118. bool is_first = true;
  119. int status = 0;
  120. bool is_last;
  121. is_first = true;
  122. spi_xcomm_chipselect(spi_xcomm, spi, true);
  123. list_for_each_entry(t, &msg->transfers, transfer_list) {
  124. if (!t->tx_buf && !t->rx_buf && t->len) {
  125. status = -EINVAL;
  126. break;
  127. }
  128. status = spi_xcomm_setup_transfer(spi_xcomm, spi, t, &settings);
  129. if (status < 0)
  130. break;
  131. is_last = list_is_last(&t->transfer_list, &msg->transfers);
  132. cs_change = t->cs_change;
  133. if (cs_change ^ is_last)
  134. settings |= BIT(5);
  135. else
  136. settings &= ~BIT(5);
  137. if (t->rx_buf) {
  138. spi_xcomm->settings = settings;
  139. status = spi_xcomm_sync_config(spi_xcomm, t->len);
  140. if (status < 0)
  141. break;
  142. } else if (settings != spi_xcomm->settings || is_first) {
  143. spi_xcomm->settings = settings;
  144. status = spi_xcomm_sync_config(spi_xcomm, 0);
  145. if (status < 0)
  146. break;
  147. }
  148. if (t->len) {
  149. status = spi_xcomm_txrx_bufs(spi_xcomm, spi, t);
  150. if (status < 0)
  151. break;
  152. if (status > 0)
  153. msg->actual_length += status;
  154. }
  155. status = 0;
  156. if (t->delay_usecs)
  157. udelay(t->delay_usecs);
  158. is_first = false;
  159. }
  160. if (status != 0 || !cs_change)
  161. spi_xcomm_chipselect(spi_xcomm, spi, false);
  162. msg->status = status;
  163. spi_finalize_current_message(master);
  164. return status;
  165. }
  166. static int spi_xcomm_setup(struct spi_device *spi)
  167. {
  168. if (spi->bits_per_word != 8)
  169. return -EINVAL;
  170. return 0;
  171. }
  172. static int spi_xcomm_probe(struct i2c_client *i2c,
  173. const struct i2c_device_id *id)
  174. {
  175. struct spi_xcomm *spi_xcomm;
  176. struct spi_master *master;
  177. int ret;
  178. master = spi_alloc_master(&i2c->dev, sizeof(*spi_xcomm));
  179. if (!master)
  180. return -ENOMEM;
  181. spi_xcomm = spi_master_get_devdata(master);
  182. spi_xcomm->i2c = i2c;
  183. master->num_chipselect = 16;
  184. master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_3WIRE;
  185. master->flags = SPI_MASTER_HALF_DUPLEX;
  186. master->setup = spi_xcomm_setup;
  187. master->transfer_one_message = spi_xcomm_transfer_one;
  188. master->dev.of_node = i2c->dev.of_node;
  189. i2c_set_clientdata(i2c, master);
  190. ret = spi_register_master(master);
  191. if (ret < 0)
  192. spi_master_put(master);
  193. return ret;
  194. }
  195. static int spi_xcomm_remove(struct i2c_client *i2c)
  196. {
  197. struct spi_master *master = i2c_get_clientdata(i2c);
  198. spi_unregister_master(master);
  199. return 0;
  200. }
  201. static const struct i2c_device_id spi_xcomm_ids[] = {
  202. { "spi-xcomm" },
  203. { },
  204. };
  205. static struct i2c_driver spi_xcomm_driver = {
  206. .driver = {
  207. .name = "spi-xcomm",
  208. .owner = THIS_MODULE,
  209. },
  210. .id_table = spi_xcomm_ids,
  211. .probe = spi_xcomm_probe,
  212. .remove = spi_xcomm_remove,
  213. };
  214. module_i2c_driver(spi_xcomm_driver);
  215. MODULE_LICENSE("GPL");
  216. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  217. MODULE_DESCRIPTION("Analog Devices AD-FMCOMMS1-EBZ board I2C-SPI bridge driver");