spi-gpio.c 14 KB

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