spi-gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SPI master driver using generic bitbanged GPIO
  4. *
  5. * Copyright (C) 2006,2008 David Brownell
  6. * Copyright (C) 2017 Linus Walleij
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/of.h>
  13. #include <linux/of_device.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/spi/spi_bitbang.h>
  16. #include <linux/spi/spi_gpio.h>
  17. /*
  18. * This bitbanging SPI master driver should help make systems usable
  19. * when a native hardware SPI engine is not available, perhaps because
  20. * its driver isn't yet working or because the I/O pins it requires
  21. * are used for other purposes.
  22. *
  23. * platform_device->driver_data ... points to spi_gpio
  24. *
  25. * spi->controller_state ... reserved for bitbang framework code
  26. *
  27. * spi->master->dev.driver_data ... points to spi_gpio->bitbang
  28. */
  29. struct spi_gpio {
  30. struct spi_bitbang bitbang;
  31. struct gpio_desc *sck;
  32. struct gpio_desc *miso;
  33. struct gpio_desc *mosi;
  34. struct gpio_desc **cs_gpios;
  35. };
  36. /*----------------------------------------------------------------------*/
  37. /*
  38. * Because the overhead of going through four GPIO procedure calls
  39. * per transferred bit can make performance a problem, this code
  40. * is set up so that you can use it in either of two ways:
  41. *
  42. * - The slow generic way: set up platform_data to hold the GPIO
  43. * numbers used for MISO/MOSI/SCK, and issue procedure calls for
  44. * each of them. This driver can handle several such busses.
  45. *
  46. * - The quicker inlined way: only helps with platform GPIO code
  47. * that inlines operations for constant GPIOs. This can give
  48. * you tight (fast!) inner loops, but each such bus needs a
  49. * new driver. You'll define a new C file, with Makefile and
  50. * Kconfig support; the C code can be a total of six lines:
  51. *
  52. * #define DRIVER_NAME "myboard_spi2"
  53. * #define SPI_MISO_GPIO 119
  54. * #define SPI_MOSI_GPIO 120
  55. * #define SPI_SCK_GPIO 121
  56. * #define SPI_N_CHIPSEL 4
  57. * #include "spi-gpio.c"
  58. */
  59. #ifndef DRIVER_NAME
  60. #define DRIVER_NAME "spi_gpio"
  61. #define GENERIC_BITBANG /* vs tight inlines */
  62. #endif
  63. /*----------------------------------------------------------------------*/
  64. static inline struct spi_gpio *__pure
  65. spi_to_spi_gpio(const struct spi_device *spi)
  66. {
  67. const struct spi_bitbang *bang;
  68. struct spi_gpio *spi_gpio;
  69. bang = spi_master_get_devdata(spi->master);
  70. spi_gpio = container_of(bang, struct spi_gpio, bitbang);
  71. return spi_gpio;
  72. }
  73. /* These helpers are in turn called by the bitbang inlines */
  74. static inline void setsck(const struct spi_device *spi, int is_on)
  75. {
  76. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  77. gpiod_set_value_cansleep(spi_gpio->sck, is_on);
  78. }
  79. static inline void setmosi(const struct spi_device *spi, int is_on)
  80. {
  81. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  82. gpiod_set_value_cansleep(spi_gpio->mosi, is_on);
  83. }
  84. static inline int getmiso(const struct spi_device *spi)
  85. {
  86. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  87. if (spi->mode & SPI_3WIRE)
  88. return !!gpiod_get_value_cansleep(spi_gpio->mosi);
  89. else
  90. return !!gpiod_get_value_cansleep(spi_gpio->miso);
  91. }
  92. /*
  93. * NOTE: this clocks "as fast as we can". It "should" be a function of the
  94. * requested device clock. Software overhead means we usually have trouble
  95. * reaching even one Mbit/sec (except when we can inline bitops), so for now
  96. * we'll just assume we never need additional per-bit slowdowns.
  97. */
  98. #define spidelay(nsecs) do {} while (0)
  99. #include "spi-bitbang-txrx.h"
  100. /*
  101. * These functions can leverage inline expansion of GPIO calls to shrink
  102. * costs for a txrx bit, often by factors of around ten (by instruction
  103. * count). That is particularly visible for larger word sizes, but helps
  104. * even with default 8-bit words.
  105. *
  106. * REVISIT overheads calling these functions for each word also have
  107. * significant performance costs. Having txrx_bufs() calls that inline
  108. * the txrx_word() logic would help performance, e.g. on larger blocks
  109. * used with flash storage or MMC/SD. There should also be ways to make
  110. * GCC be less stupid about reloading registers inside the I/O loops,
  111. * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
  112. */
  113. static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
  114. unsigned nsecs, u32 word, u8 bits, unsigned flags)
  115. {
  116. if (unlikely(spi->mode & SPI_LSB_FIRST))
  117. return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
  118. else
  119. return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
  120. }
  121. static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
  122. unsigned nsecs, u32 word, u8 bits, unsigned flags)
  123. {
  124. if (unlikely(spi->mode & SPI_LSB_FIRST))
  125. return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
  126. else
  127. return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
  128. }
  129. static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
  130. unsigned nsecs, u32 word, u8 bits, unsigned flags)
  131. {
  132. if (unlikely(spi->mode & SPI_LSB_FIRST))
  133. return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
  134. else
  135. return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
  136. }
  137. static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
  138. unsigned nsecs, u32 word, u8 bits, unsigned flags)
  139. {
  140. if (unlikely(spi->mode & SPI_LSB_FIRST))
  141. return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
  142. else
  143. return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
  144. }
  145. /*
  146. * These functions do not call setmosi or getmiso if respective flag
  147. * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
  148. * call when such pin is not present or defined in the controller.
  149. * A separate set of callbacks is defined to get highest possible
  150. * speed in the generic case (when both MISO and MOSI lines are
  151. * available), as optimiser will remove the checks when argument is
  152. * constant.
  153. */
  154. static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
  155. unsigned nsecs, u32 word, u8 bits, unsigned flags)
  156. {
  157. flags = spi->master->flags;
  158. if (unlikely(spi->mode & SPI_LSB_FIRST))
  159. return bitbang_txrx_le_cpha0(spi, nsecs, 0, flags, word, bits);
  160. else
  161. return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
  162. }
  163. static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
  164. unsigned nsecs, u32 word, u8 bits, unsigned flags)
  165. {
  166. flags = spi->master->flags;
  167. if (unlikely(spi->mode & SPI_LSB_FIRST))
  168. return bitbang_txrx_le_cpha1(spi, nsecs, 0, flags, word, bits);
  169. else
  170. return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
  171. }
  172. static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
  173. unsigned nsecs, u32 word, u8 bits, unsigned flags)
  174. {
  175. flags = spi->master->flags;
  176. if (unlikely(spi->mode & SPI_LSB_FIRST))
  177. return bitbang_txrx_le_cpha0(spi, nsecs, 1, flags, word, bits);
  178. else
  179. return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
  180. }
  181. static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
  182. unsigned nsecs, u32 word, u8 bits, unsigned flags)
  183. {
  184. flags = spi->master->flags;
  185. if (unlikely(spi->mode & SPI_LSB_FIRST))
  186. return bitbang_txrx_le_cpha1(spi, nsecs, 1, flags, word, bits);
  187. else
  188. return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
  189. }
  190. /*----------------------------------------------------------------------*/
  191. static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
  192. {
  193. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  194. /* set initial clock line level */
  195. if (is_active)
  196. gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
  197. /* Drive chip select line, if we have one */
  198. if (spi_gpio->cs_gpios) {
  199. struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];
  200. /* SPI chip selects are normally active-low */
  201. gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  202. }
  203. }
  204. static int spi_gpio_setup(struct spi_device *spi)
  205. {
  206. struct gpio_desc *cs;
  207. int status = 0;
  208. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  209. /*
  210. * The CS GPIOs have already been
  211. * initialized from the descriptor lookup.
  212. */
  213. if (spi_gpio->cs_gpios) {
  214. cs = spi_gpio->cs_gpios[spi->chip_select];
  215. if (!spi->controller_state && cs)
  216. status = gpiod_direction_output(cs,
  217. !(spi->mode & SPI_CS_HIGH));
  218. }
  219. if (!status)
  220. status = spi_bitbang_setup(spi);
  221. return status;
  222. }
  223. static int spi_gpio_set_direction(struct spi_device *spi, bool output)
  224. {
  225. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  226. int ret;
  227. if (output)
  228. return gpiod_direction_output(spi_gpio->mosi, 1);
  229. /*
  230. * Only change MOSI to an input if using 3WIRE mode.
  231. * Otherwise, MOSI could be left floating if there is
  232. * no pull resistor connected to the I/O pin, or could
  233. * be left logic high if there is a pull-up. Transmitting
  234. * logic high when only clocking MISO data in can put some
  235. * SPI devices in to a bad state.
  236. */
  237. if (spi->mode & SPI_3WIRE) {
  238. ret = gpiod_direction_input(spi_gpio->mosi);
  239. if (ret)
  240. return ret;
  241. }
  242. /*
  243. * Send a turnaround high impedance cycle when switching
  244. * from output to input. Theoretically there should be
  245. * a clock delay here, but as has been noted above, the
  246. * nsec delay function for bit-banged GPIO is simply
  247. * {} because bit-banging just doesn't get fast enough
  248. * anyway.
  249. */
  250. if (spi->mode & SPI_3WIRE_HIZ) {
  251. gpiod_set_value_cansleep(spi_gpio->sck,
  252. !(spi->mode & SPI_CPOL));
  253. gpiod_set_value_cansleep(spi_gpio->sck,
  254. !!(spi->mode & SPI_CPOL));
  255. }
  256. return 0;
  257. }
  258. static void spi_gpio_cleanup(struct spi_device *spi)
  259. {
  260. spi_bitbang_cleanup(spi);
  261. }
  262. /*
  263. * It can be convenient to use this driver with pins that have alternate
  264. * functions associated with a "native" SPI controller if a driver for that
  265. * controller is not available, or is missing important functionality.
  266. *
  267. * On platforms which can do so, configure MISO with a weak pullup unless
  268. * there's an external pullup on that signal. That saves power by avoiding
  269. * floating signals. (A weak pulldown would save power too, but many
  270. * drivers expect to see all-ones data as the no slave "response".)
  271. */
  272. static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
  273. {
  274. spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
  275. if (IS_ERR(spi_gpio->mosi))
  276. return PTR_ERR(spi_gpio->mosi);
  277. spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
  278. if (IS_ERR(spi_gpio->miso))
  279. return PTR_ERR(spi_gpio->miso);
  280. spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
  281. return PTR_ERR_OR_ZERO(spi_gpio->sck);
  282. }
  283. #ifdef CONFIG_OF
  284. static const struct of_device_id spi_gpio_dt_ids[] = {
  285. { .compatible = "spi-gpio" },
  286. {}
  287. };
  288. MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
  289. static int spi_gpio_probe_dt(struct platform_device *pdev,
  290. struct spi_master *master)
  291. {
  292. master->dev.of_node = pdev->dev.of_node;
  293. master->use_gpio_descriptors = true;
  294. return 0;
  295. }
  296. #else
  297. static inline int spi_gpio_probe_dt(struct platform_device *pdev,
  298. struct spi_master *master)
  299. {
  300. return 0;
  301. }
  302. #endif
  303. static int spi_gpio_probe_pdata(struct platform_device *pdev,
  304. struct spi_master *master)
  305. {
  306. struct device *dev = &pdev->dev;
  307. struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
  308. struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
  309. int i;
  310. #ifdef GENERIC_BITBANG
  311. if (!pdata || !pdata->num_chipselect)
  312. return -ENODEV;
  313. #endif
  314. /*
  315. * The master needs to think there is a chipselect even if not
  316. * connected
  317. */
  318. master->num_chipselect = pdata->num_chipselect ?: 1;
  319. spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
  320. sizeof(*spi_gpio->cs_gpios),
  321. GFP_KERNEL);
  322. if (!spi_gpio->cs_gpios)
  323. return -ENOMEM;
  324. for (i = 0; i < master->num_chipselect; i++) {
  325. spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
  326. GPIOD_OUT_HIGH);
  327. if (IS_ERR(spi_gpio->cs_gpios[i]))
  328. return PTR_ERR(spi_gpio->cs_gpios[i]);
  329. }
  330. return 0;
  331. }
  332. static int spi_gpio_probe(struct platform_device *pdev)
  333. {
  334. int status;
  335. struct spi_master *master;
  336. struct spi_gpio *spi_gpio;
  337. struct device *dev = &pdev->dev;
  338. struct spi_bitbang *bb;
  339. master = devm_spi_alloc_master(dev, sizeof(*spi_gpio));
  340. if (!master)
  341. return -ENOMEM;
  342. if (pdev->dev.of_node)
  343. status = spi_gpio_probe_dt(pdev, master);
  344. else
  345. status = spi_gpio_probe_pdata(pdev, master);
  346. if (status)
  347. return status;
  348. spi_gpio = spi_master_get_devdata(master);
  349. status = spi_gpio_request(dev, spi_gpio);
  350. if (status)
  351. return status;
  352. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  353. master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
  354. SPI_CS_HIGH | SPI_LSB_FIRST;
  355. if (!spi_gpio->mosi) {
  356. /* HW configuration without MOSI pin
  357. *
  358. * No setting SPI_MASTER_NO_RX here - if there is only
  359. * a MOSI pin connected the host can still do RX by
  360. * changing the direction of the line.
  361. */
  362. master->flags = SPI_MASTER_NO_TX;
  363. }
  364. master->bus_num = pdev->id;
  365. master->setup = spi_gpio_setup;
  366. master->cleanup = spi_gpio_cleanup;
  367. bb = &spi_gpio->bitbang;
  368. bb->master = master;
  369. /*
  370. * There is some additional business, apart from driving the CS GPIO
  371. * line, that we need to do on selection. This makes the local
  372. * callback for chipselect always get called.
  373. */
  374. master->flags |= SPI_MASTER_GPIO_SS;
  375. bb->chipselect = spi_gpio_chipselect;
  376. bb->set_line_direction = spi_gpio_set_direction;
  377. if (master->flags & SPI_MASTER_NO_TX) {
  378. bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
  379. bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
  380. bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
  381. bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
  382. } else {
  383. bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  384. bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  385. bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  386. bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  387. }
  388. bb->setup_transfer = spi_bitbang_setup_transfer;
  389. status = spi_bitbang_init(&spi_gpio->bitbang);
  390. if (status)
  391. return status;
  392. return devm_spi_register_master(&pdev->dev, master);
  393. }
  394. MODULE_ALIAS("platform:" DRIVER_NAME);
  395. static struct platform_driver spi_gpio_driver = {
  396. .driver = {
  397. .name = DRIVER_NAME,
  398. .of_match_table = of_match_ptr(spi_gpio_dt_ids),
  399. },
  400. .probe = spi_gpio_probe,
  401. };
  402. module_platform_driver(spi_gpio_driver);
  403. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  404. MODULE_AUTHOR("David Brownell");
  405. MODULE_LICENSE("GPL");