spi-sc18is602.c 7.9 KB

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