spi-sc18is602.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. enum chips { sc18is602, sc18is602b, sc18is603 };
  26. #define SC18IS602_BUFSIZ 200
  27. #define SC18IS602_CLOCK 7372000
  28. #define SC18IS602_MODE_CPHA BIT(2)
  29. #define SC18IS602_MODE_CPOL BIT(3)
  30. #define SC18IS602_MODE_LSB_FIRST BIT(5)
  31. #define SC18IS602_MODE_CLOCK_DIV_4 0x0
  32. #define SC18IS602_MODE_CLOCK_DIV_16 0x1
  33. #define SC18IS602_MODE_CLOCK_DIV_64 0x2
  34. #define SC18IS602_MODE_CLOCK_DIV_128 0x3
  35. struct sc18is602 {
  36. struct spi_master *master;
  37. struct device *dev;
  38. u8 ctrl;
  39. u32 freq;
  40. u32 speed;
  41. /* I2C data */
  42. struct i2c_client *client;
  43. enum chips id;
  44. u8 buffer[SC18IS602_BUFSIZ + 1];
  45. int tlen; /* Data queued for tx in buffer */
  46. int rindex; /* Receive data index in buffer */
  47. };
  48. static int sc18is602_wait_ready(struct sc18is602 *hw, int len)
  49. {
  50. int i, err;
  51. int usecs = 1000000 * len / hw->speed + 1;
  52. u8 dummy[1];
  53. for (i = 0; i < 10; i++) {
  54. err = i2c_master_recv(hw->client, dummy, 1);
  55. if (err >= 0)
  56. return 0;
  57. usleep_range(usecs, usecs * 2);
  58. }
  59. return -ETIMEDOUT;
  60. }
  61. static int sc18is602_txrx(struct sc18is602 *hw, struct spi_message *msg,
  62. struct spi_transfer *t, bool do_transfer)
  63. {
  64. unsigned int len = t->len;
  65. int ret;
  66. if (hw->tlen == 0) {
  67. /* First byte (I2C command) is chip select */
  68. hw->buffer[0] = 1 << msg->spi->chip_select;
  69. hw->tlen = 1;
  70. hw->rindex = 0;
  71. }
  72. /*
  73. * We can not immediately send data to the chip, since each I2C message
  74. * resembles a full SPI message (from CS active to CS inactive).
  75. * Enqueue messages up to the first read or until do_transfer is true.
  76. */
  77. if (t->tx_buf) {
  78. memcpy(&hw->buffer[hw->tlen], t->tx_buf, len);
  79. hw->tlen += len;
  80. if (t->rx_buf)
  81. do_transfer = true;
  82. else
  83. hw->rindex = hw->tlen - 1;
  84. } else if (t->rx_buf) {
  85. /*
  86. * For receive-only transfers we still need to perform a dummy
  87. * write to receive data from the SPI chip.
  88. * Read data starts at the end of transmit data (minus 1 to
  89. * account for CS).
  90. */
  91. hw->rindex = hw->tlen - 1;
  92. memset(&hw->buffer[hw->tlen], 0, len);
  93. hw->tlen += len;
  94. do_transfer = true;
  95. }
  96. if (do_transfer && hw->tlen > 1) {
  97. ret = sc18is602_wait_ready(hw, SC18IS602_BUFSIZ);
  98. if (ret < 0)
  99. return ret;
  100. ret = i2c_master_send(hw->client, hw->buffer, hw->tlen);
  101. if (ret < 0)
  102. return ret;
  103. if (ret != hw->tlen)
  104. return -EIO;
  105. if (t->rx_buf) {
  106. int rlen = hw->rindex + len;
  107. ret = sc18is602_wait_ready(hw, hw->tlen);
  108. if (ret < 0)
  109. return ret;
  110. ret = i2c_master_recv(hw->client, hw->buffer, rlen);
  111. if (ret < 0)
  112. return ret;
  113. if (ret != rlen)
  114. return -EIO;
  115. memcpy(t->rx_buf, &hw->buffer[hw->rindex], len);
  116. }
  117. hw->tlen = 0;
  118. }
  119. return len;
  120. }
  121. static int sc18is602_setup_transfer(struct sc18is602 *hw, u32 hz, u8 mode)
  122. {
  123. u8 ctrl = 0;
  124. int ret;
  125. if (mode & SPI_CPHA)
  126. ctrl |= SC18IS602_MODE_CPHA;
  127. if (mode & SPI_CPOL)
  128. ctrl |= SC18IS602_MODE_CPOL;
  129. if (mode & SPI_LSB_FIRST)
  130. ctrl |= SC18IS602_MODE_LSB_FIRST;
  131. /* Find the closest clock speed */
  132. if (hz >= hw->freq / 4) {
  133. ctrl |= SC18IS602_MODE_CLOCK_DIV_4;
  134. hw->speed = hw->freq / 4;
  135. } else if (hz >= hw->freq / 16) {
  136. ctrl |= SC18IS602_MODE_CLOCK_DIV_16;
  137. hw->speed = hw->freq / 16;
  138. } else if (hz >= hw->freq / 64) {
  139. ctrl |= SC18IS602_MODE_CLOCK_DIV_64;
  140. hw->speed = hw->freq / 64;
  141. } else {
  142. ctrl |= SC18IS602_MODE_CLOCK_DIV_128;
  143. hw->speed = hw->freq / 128;
  144. }
  145. /*
  146. * Don't do anything if the control value did not change. The initial
  147. * value of 0xff for hw->ctrl ensures that the correct mode will be set
  148. * with the first call to this function.
  149. */
  150. if (ctrl == hw->ctrl)
  151. return 0;
  152. ret = i2c_smbus_write_byte_data(hw->client, 0xf0, ctrl);
  153. if (ret < 0)
  154. return ret;
  155. hw->ctrl = ctrl;
  156. return 0;
  157. }
  158. static int sc18is602_check_transfer(struct spi_device *spi,
  159. struct spi_transfer *t, int tlen)
  160. {
  161. if (t && t->len + tlen > SC18IS602_BUFSIZ)
  162. return -EINVAL;
  163. return 0;
  164. }
  165. static int sc18is602_transfer_one(struct spi_master *master,
  166. struct spi_message *m)
  167. {
  168. struct sc18is602 *hw = spi_master_get_devdata(master);
  169. struct spi_device *spi = m->spi;
  170. struct spi_transfer *t;
  171. int status = 0;
  172. hw->tlen = 0;
  173. list_for_each_entry(t, &m->transfers, transfer_list) {
  174. bool do_transfer;
  175. status = sc18is602_check_transfer(spi, t, hw->tlen);
  176. if (status < 0)
  177. break;
  178. status = sc18is602_setup_transfer(hw, t->speed_hz, spi->mode);
  179. if (status < 0)
  180. break;
  181. do_transfer = t->cs_change || list_is_last(&t->transfer_list,
  182. &m->transfers);
  183. if (t->len) {
  184. status = sc18is602_txrx(hw, m, t, do_transfer);
  185. if (status < 0)
  186. break;
  187. m->actual_length += status;
  188. }
  189. status = 0;
  190. if (t->delay_usecs)
  191. udelay(t->delay_usecs);
  192. }
  193. m->status = status;
  194. spi_finalize_current_message(master);
  195. return status;
  196. }
  197. static int sc18is602_setup(struct spi_device *spi)
  198. {
  199. struct sc18is602 *hw = spi_master_get_devdata(spi->master);
  200. /* SC18IS602 does not support CS2 */
  201. if (hw->id == sc18is602 && spi->chip_select == 2)
  202. return -ENXIO;
  203. return 0;
  204. }
  205. static int sc18is602_probe(struct i2c_client *client,
  206. const struct i2c_device_id *id)
  207. {
  208. struct device *dev = &client->dev;
  209. struct device_node *np = dev->of_node;
  210. struct sc18is602_platform_data *pdata = dev_get_platdata(dev);
  211. struct sc18is602 *hw;
  212. struct spi_master *master;
  213. int error;
  214. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
  215. I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  216. return -EINVAL;
  217. master = spi_alloc_master(dev, sizeof(struct sc18is602));
  218. if (!master)
  219. return -ENOMEM;
  220. hw = spi_master_get_devdata(master);
  221. i2c_set_clientdata(client, hw);
  222. hw->master = master;
  223. hw->client = client;
  224. hw->dev = dev;
  225. hw->ctrl = 0xff;
  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. error = devm_spi_register_master(dev, master);
  257. if (error)
  258. goto error_reg;
  259. return 0;
  260. error_reg:
  261. spi_master_put(master);
  262. return error;
  263. }
  264. static const struct i2c_device_id sc18is602_id[] = {
  265. { "sc18is602", sc18is602 },
  266. { "sc18is602b", sc18is602b },
  267. { "sc18is603", sc18is603 },
  268. { }
  269. };
  270. MODULE_DEVICE_TABLE(i2c, sc18is602_id);
  271. static struct i2c_driver sc18is602_driver = {
  272. .driver = {
  273. .name = "sc18is602",
  274. },
  275. .probe = sc18is602_probe,
  276. .id_table = sc18is602_id,
  277. };
  278. module_i2c_driver(sc18is602_driver);
  279. MODULE_DESCRIPTION("SC18IC602/603 SPI Master Driver");
  280. MODULE_AUTHOR("Guenter Roeck");
  281. MODULE_LICENSE("GPL");