spi-sc18is602.c 8.0 KB

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