spi-sc18is602.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NXP SC18IS602/603 SPI driver
  4. *
  5. * Copyright (C) Guenter Roeck <linux@roeck-us.net>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/i2c.h>
  12. #include <linux/delay.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_data/sc18is602.h>
  17. #include <linux/gpio/consumer.h>
  18. enum chips { sc18is602, sc18is602b, sc18is603 };
  19. #define SC18IS602_BUFSIZ 200
  20. #define SC18IS602_CLOCK 7372000
  21. #define SC18IS602_MODE_CPHA BIT(2)
  22. #define SC18IS602_MODE_CPOL BIT(3)
  23. #define SC18IS602_MODE_LSB_FIRST BIT(5)
  24. #define SC18IS602_MODE_CLOCK_DIV_4 0x0
  25. #define SC18IS602_MODE_CLOCK_DIV_16 0x1
  26. #define SC18IS602_MODE_CLOCK_DIV_64 0x2
  27. #define SC18IS602_MODE_CLOCK_DIV_128 0x3
  28. struct sc18is602 {
  29. struct spi_master *master;
  30. struct device *dev;
  31. u8 ctrl;
  32. u32 freq;
  33. u32 speed;
  34. /* I2C data */
  35. struct i2c_client *client;
  36. enum chips id;
  37. u8 buffer[SC18IS602_BUFSIZ + 1];
  38. int tlen; /* Data queued for tx in buffer */
  39. int rindex; /* Receive data index in buffer */
  40. struct gpio_desc *reset;
  41. };
  42. static int sc18is602_wait_ready(struct sc18is602 *hw, int len)
  43. {
  44. int i, err;
  45. int usecs = 1000000 * len / hw->speed + 1;
  46. u8 dummy[1];
  47. for (i = 0; i < 10; i++) {
  48. err = i2c_master_recv(hw->client, dummy, 1);
  49. if (err >= 0)
  50. return 0;
  51. usleep_range(usecs, usecs * 2);
  52. }
  53. return -ETIMEDOUT;
  54. }
  55. static int sc18is602_txrx(struct sc18is602 *hw, struct spi_message *msg,
  56. struct spi_transfer *t, bool do_transfer)
  57. {
  58. unsigned int len = t->len;
  59. int ret;
  60. if (hw->tlen == 0) {
  61. /* First byte (I2C command) is chip select */
  62. hw->buffer[0] = 1 << msg->spi->chip_select;
  63. hw->tlen = 1;
  64. hw->rindex = 0;
  65. }
  66. /*
  67. * We can not immediately send data to the chip, since each I2C message
  68. * resembles a full SPI message (from CS active to CS inactive).
  69. * Enqueue messages up to the first read or until do_transfer is true.
  70. */
  71. if (t->tx_buf) {
  72. memcpy(&hw->buffer[hw->tlen], t->tx_buf, len);
  73. hw->tlen += len;
  74. if (t->rx_buf)
  75. do_transfer = true;
  76. else
  77. hw->rindex = hw->tlen - 1;
  78. } else if (t->rx_buf) {
  79. /*
  80. * For receive-only transfers we still need to perform a dummy
  81. * write to receive data from the SPI chip.
  82. * Read data starts at the end of transmit data (minus 1 to
  83. * account for CS).
  84. */
  85. hw->rindex = hw->tlen - 1;
  86. memset(&hw->buffer[hw->tlen], 0, len);
  87. hw->tlen += len;
  88. do_transfer = true;
  89. }
  90. if (do_transfer && hw->tlen > 1) {
  91. ret = sc18is602_wait_ready(hw, SC18IS602_BUFSIZ);
  92. if (ret < 0)
  93. return ret;
  94. ret = i2c_master_send(hw->client, hw->buffer, hw->tlen);
  95. if (ret < 0)
  96. return ret;
  97. if (ret != hw->tlen)
  98. return -EIO;
  99. if (t->rx_buf) {
  100. int rlen = hw->rindex + len;
  101. ret = sc18is602_wait_ready(hw, hw->tlen);
  102. if (ret < 0)
  103. return ret;
  104. ret = i2c_master_recv(hw->client, hw->buffer, rlen);
  105. if (ret < 0)
  106. return ret;
  107. if (ret != rlen)
  108. return -EIO;
  109. memcpy(t->rx_buf, &hw->buffer[hw->rindex], len);
  110. }
  111. hw->tlen = 0;
  112. }
  113. return len;
  114. }
  115. static int sc18is602_setup_transfer(struct sc18is602 *hw, u32 hz, u8 mode)
  116. {
  117. u8 ctrl = 0;
  118. int ret;
  119. if (mode & SPI_CPHA)
  120. ctrl |= SC18IS602_MODE_CPHA;
  121. if (mode & SPI_CPOL)
  122. ctrl |= SC18IS602_MODE_CPOL;
  123. if (mode & SPI_LSB_FIRST)
  124. ctrl |= SC18IS602_MODE_LSB_FIRST;
  125. /* Find the closest clock speed */
  126. if (hz >= hw->freq / 4) {
  127. ctrl |= SC18IS602_MODE_CLOCK_DIV_4;
  128. hw->speed = hw->freq / 4;
  129. } else if (hz >= hw->freq / 16) {
  130. ctrl |= SC18IS602_MODE_CLOCK_DIV_16;
  131. hw->speed = hw->freq / 16;
  132. } else if (hz >= hw->freq / 64) {
  133. ctrl |= SC18IS602_MODE_CLOCK_DIV_64;
  134. hw->speed = hw->freq / 64;
  135. } else {
  136. ctrl |= SC18IS602_MODE_CLOCK_DIV_128;
  137. hw->speed = hw->freq / 128;
  138. }
  139. /*
  140. * Don't do anything if the control value did not change. The initial
  141. * value of 0xff for hw->ctrl ensures that the correct mode will be set
  142. * with the first call to this function.
  143. */
  144. if (ctrl == hw->ctrl)
  145. return 0;
  146. ret = i2c_smbus_write_byte_data(hw->client, 0xf0, ctrl);
  147. if (ret < 0)
  148. return ret;
  149. hw->ctrl = ctrl;
  150. return 0;
  151. }
  152. static int sc18is602_check_transfer(struct spi_device *spi,
  153. struct spi_transfer *t, int tlen)
  154. {
  155. if (t && t->len + tlen > SC18IS602_BUFSIZ)
  156. return -EINVAL;
  157. return 0;
  158. }
  159. static int sc18is602_transfer_one(struct spi_master *master,
  160. struct spi_message *m)
  161. {
  162. struct sc18is602 *hw = spi_master_get_devdata(master);
  163. struct spi_device *spi = m->spi;
  164. struct spi_transfer *t;
  165. int status = 0;
  166. hw->tlen = 0;
  167. list_for_each_entry(t, &m->transfers, transfer_list) {
  168. bool do_transfer;
  169. status = sc18is602_check_transfer(spi, t, hw->tlen);
  170. if (status < 0)
  171. break;
  172. status = sc18is602_setup_transfer(hw, t->speed_hz, spi->mode);
  173. if (status < 0)
  174. break;
  175. do_transfer = t->cs_change || list_is_last(&t->transfer_list,
  176. &m->transfers);
  177. if (t->len) {
  178. status = sc18is602_txrx(hw, m, t, do_transfer);
  179. if (status < 0)
  180. break;
  181. m->actual_length += status;
  182. }
  183. status = 0;
  184. spi_transfer_delay_exec(t);
  185. }
  186. m->status = status;
  187. spi_finalize_current_message(master);
  188. return status;
  189. }
  190. static int sc18is602_setup(struct spi_device *spi)
  191. {
  192. struct sc18is602 *hw = spi_master_get_devdata(spi->master);
  193. /* SC18IS602 does not support CS2 */
  194. if (hw->id == sc18is602 && spi->chip_select == 2)
  195. return -ENXIO;
  196. return 0;
  197. }
  198. static int sc18is602_probe(struct i2c_client *client,
  199. const struct i2c_device_id *id)
  200. {
  201. struct device *dev = &client->dev;
  202. struct device_node *np = dev->of_node;
  203. struct sc18is602_platform_data *pdata = dev_get_platdata(dev);
  204. struct sc18is602 *hw;
  205. struct spi_master *master;
  206. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  207. I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  208. return -EINVAL;
  209. master = devm_spi_alloc_master(dev, sizeof(struct sc18is602));
  210. if (!master)
  211. return -ENOMEM;
  212. hw = spi_master_get_devdata(master);
  213. i2c_set_clientdata(client, hw);
  214. /* assert reset and then release */
  215. hw->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
  216. if (IS_ERR(hw->reset))
  217. return PTR_ERR(hw->reset);
  218. gpiod_set_value_cansleep(hw->reset, 0);
  219. hw->master = master;
  220. hw->client = client;
  221. hw->dev = dev;
  222. hw->ctrl = 0xff;
  223. if (client->dev.of_node)
  224. hw->id = (enum chips)of_device_get_match_data(&client->dev);
  225. else
  226. hw->id = id->driver_data;
  227. switch (hw->id) {
  228. case sc18is602:
  229. case sc18is602b:
  230. master->num_chipselect = 4;
  231. hw->freq = SC18IS602_CLOCK;
  232. break;
  233. case sc18is603:
  234. master->num_chipselect = 2;
  235. if (pdata) {
  236. hw->freq = pdata->clock_frequency;
  237. } else {
  238. const __be32 *val;
  239. int len;
  240. val = of_get_property(np, "clock-frequency", &len);
  241. if (val && len >= sizeof(__be32))
  242. hw->freq = be32_to_cpup(val);
  243. }
  244. if (!hw->freq)
  245. hw->freq = SC18IS602_CLOCK;
  246. break;
  247. }
  248. master->bus_num = np ? -1 : client->adapter->nr;
  249. master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_LSB_FIRST;
  250. master->bits_per_word_mask = SPI_BPW_MASK(8);
  251. master->setup = sc18is602_setup;
  252. master->transfer_one_message = sc18is602_transfer_one;
  253. master->dev.of_node = np;
  254. master->min_speed_hz = hw->freq / 128;
  255. master->max_speed_hz = hw->freq / 4;
  256. return devm_spi_register_master(dev, master);
  257. }
  258. static const struct i2c_device_id sc18is602_id[] = {
  259. { "sc18is602", sc18is602 },
  260. { "sc18is602b", sc18is602b },
  261. { "sc18is603", sc18is603 },
  262. { }
  263. };
  264. MODULE_DEVICE_TABLE(i2c, sc18is602_id);
  265. static const struct of_device_id sc18is602_of_match[] = {
  266. {
  267. .compatible = "nxp,sc18is602",
  268. .data = (void *)sc18is602
  269. },
  270. {
  271. .compatible = "nxp,sc18is602b",
  272. .data = (void *)sc18is602b
  273. },
  274. {
  275. .compatible = "nxp,sc18is603",
  276. .data = (void *)sc18is603
  277. },
  278. { },
  279. };
  280. MODULE_DEVICE_TABLE(of, sc18is602_of_match);
  281. static struct i2c_driver sc18is602_driver = {
  282. .driver = {
  283. .name = "sc18is602",
  284. .of_match_table = of_match_ptr(sc18is602_of_match),
  285. },
  286. .probe = sc18is602_probe,
  287. .id_table = sc18is602_id,
  288. };
  289. module_i2c_driver(sc18is602_driver);
  290. MODULE_DESCRIPTION("SC18IS602/603 SPI Master Driver");
  291. MODULE_AUTHOR("Guenter Roeck");
  292. MODULE_LICENSE("GPL");