spi-gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. return !!gpiod_get_value_cansleep(spi_gpio->miso);
  106. }
  107. /*
  108. * NOTE: this clocks "as fast as we can". It "should" be a function of the
  109. * requested device clock. Software overhead means we usually have trouble
  110. * reaching even one Mbit/sec (except when we can inline bitops), so for now
  111. * we'll just assume we never need additional per-bit slowdowns.
  112. */
  113. #define spidelay(nsecs) do {} while (0)
  114. #include "spi-bitbang-txrx.h"
  115. /*
  116. * These functions can leverage inline expansion of GPIO calls to shrink
  117. * costs for a txrx bit, often by factors of around ten (by instruction
  118. * count). That is particularly visible for larger word sizes, but helps
  119. * even with default 8-bit words.
  120. *
  121. * REVISIT overheads calling these functions for each word also have
  122. * significant performance costs. Having txrx_bufs() calls that inline
  123. * the txrx_word() logic would help performance, e.g. on larger blocks
  124. * used with flash storage or MMC/SD. There should also be ways to make
  125. * GCC be less stupid about reloading registers inside the I/O loops,
  126. * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
  127. */
  128. static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
  129. unsigned nsecs, u32 word, u8 bits)
  130. {
  131. return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
  132. }
  133. static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
  134. unsigned nsecs, u32 word, u8 bits)
  135. {
  136. return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits);
  137. }
  138. static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
  139. unsigned nsecs, u32 word, u8 bits)
  140. {
  141. return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits);
  142. }
  143. static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
  144. unsigned nsecs, u32 word, u8 bits)
  145. {
  146. return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits);
  147. }
  148. /*
  149. * These functions do not call setmosi or getmiso if respective flag
  150. * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
  151. * call when such pin is not present or defined in the controller.
  152. * A separate set of callbacks is defined to get highest possible
  153. * speed in the generic case (when both MISO and MOSI lines are
  154. * available), as optimiser will remove the checks when argument is
  155. * constant.
  156. */
  157. static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
  158. unsigned nsecs, u32 word, u8 bits)
  159. {
  160. unsigned flags = spi->master->flags;
  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)
  165. {
  166. unsigned flags = spi->master->flags;
  167. return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
  168. }
  169. static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
  170. unsigned nsecs, u32 word, u8 bits)
  171. {
  172. unsigned flags = spi->master->flags;
  173. return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
  174. }
  175. static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
  176. unsigned nsecs, u32 word, u8 bits)
  177. {
  178. unsigned flags = spi->master->flags;
  179. return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
  180. }
  181. /*----------------------------------------------------------------------*/
  182. static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
  183. {
  184. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  185. /* set initial clock line level */
  186. if (is_active)
  187. gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
  188. /* Drive chip select line, if we have one */
  189. if (spi_gpio->has_cs) {
  190. struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];
  191. /* SPI chip selects are normally active-low */
  192. gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  193. }
  194. }
  195. static int spi_gpio_setup(struct spi_device *spi)
  196. {
  197. struct gpio_desc *cs;
  198. int status = 0;
  199. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  200. /*
  201. * The CS GPIOs have already been
  202. * initialized from the descriptor lookup.
  203. */
  204. cs = spi_gpio->cs_gpios[spi->chip_select];
  205. if (!spi->controller_state && cs)
  206. status = gpiod_direction_output(cs,
  207. !(spi->mode & SPI_CS_HIGH));
  208. if (!status)
  209. status = spi_bitbang_setup(spi);
  210. return status;
  211. }
  212. static void spi_gpio_cleanup(struct spi_device *spi)
  213. {
  214. spi_bitbang_cleanup(spi);
  215. }
  216. /*
  217. * It can be convenient to use this driver with pins that have alternate
  218. * functions associated with a "native" SPI controller if a driver for that
  219. * controller is not available, or is missing important functionality.
  220. *
  221. * On platforms which can do so, configure MISO with a weak pullup unless
  222. * there's an external pullup on that signal. That saves power by avoiding
  223. * floating signals. (A weak pulldown would save power too, but many
  224. * drivers expect to see all-ones data as the no slave "response".)
  225. */
  226. static int spi_gpio_request(struct device *dev,
  227. struct spi_gpio *spi_gpio,
  228. unsigned int num_chipselects,
  229. u16 *mflags)
  230. {
  231. int i;
  232. spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
  233. if (IS_ERR(spi_gpio->mosi))
  234. return PTR_ERR(spi_gpio->mosi);
  235. if (!spi_gpio->mosi)
  236. /* HW configuration without MOSI pin */
  237. *mflags |= SPI_MASTER_NO_TX;
  238. spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
  239. if (IS_ERR(spi_gpio->miso))
  240. return PTR_ERR(spi_gpio->miso);
  241. if (!spi_gpio->miso)
  242. /* HW configuration without MISO pin */
  243. *mflags |= SPI_MASTER_NO_RX;
  244. spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
  245. if (IS_ERR(spi_gpio->mosi))
  246. return PTR_ERR(spi_gpio->mosi);
  247. for (i = 0; i < num_chipselects; i++) {
  248. spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs",
  249. i, GPIOD_OUT_HIGH);
  250. if (IS_ERR(spi_gpio->cs_gpios[i]))
  251. return PTR_ERR(spi_gpio->cs_gpios[i]);
  252. }
  253. return 0;
  254. }
  255. #ifdef CONFIG_OF
  256. static const struct of_device_id spi_gpio_dt_ids[] = {
  257. { .compatible = "spi-gpio" },
  258. {}
  259. };
  260. MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
  261. static int spi_gpio_probe_dt(struct platform_device *pdev)
  262. {
  263. int ret;
  264. u32 tmp;
  265. struct spi_gpio_platform_data *pdata;
  266. struct device_node *np = pdev->dev.of_node;
  267. const struct of_device_id *of_id =
  268. of_match_device(spi_gpio_dt_ids, &pdev->dev);
  269. if (!of_id)
  270. return 0;
  271. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  272. if (!pdata)
  273. return -ENOMEM;
  274. ret = of_property_read_u32(np, "num-chipselects", &tmp);
  275. if (ret < 0) {
  276. dev_err(&pdev->dev, "num-chipselects property not found\n");
  277. goto error_free;
  278. }
  279. pdata->num_chipselect = tmp;
  280. pdev->dev.platform_data = pdata;
  281. return 1;
  282. error_free:
  283. devm_kfree(&pdev->dev, pdata);
  284. return ret;
  285. }
  286. #else
  287. static inline int spi_gpio_probe_dt(struct platform_device *pdev)
  288. {
  289. return 0;
  290. }
  291. #endif
  292. static int spi_gpio_probe(struct platform_device *pdev)
  293. {
  294. int status;
  295. struct spi_master *master;
  296. struct spi_gpio *spi_gpio;
  297. struct spi_gpio_platform_data *pdata;
  298. u16 master_flags = 0;
  299. bool use_of = 0;
  300. status = spi_gpio_probe_dt(pdev);
  301. if (status < 0)
  302. return status;
  303. if (status > 0)
  304. use_of = 1;
  305. pdata = dev_get_platdata(&pdev->dev);
  306. #ifdef GENERIC_BITBANG
  307. if (!pdata || (!use_of && !pdata->num_chipselect))
  308. return -ENODEV;
  309. #endif
  310. master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio));
  311. if (!master)
  312. return -ENOMEM;
  313. spi_gpio = spi_master_get_devdata(master);
  314. spi_gpio->cs_gpios = devm_kzalloc(&pdev->dev,
  315. pdata->num_chipselect * sizeof(*spi_gpio->cs_gpios),
  316. GFP_KERNEL);
  317. if (!spi_gpio->cs_gpios)
  318. return -ENOMEM;
  319. platform_set_drvdata(pdev, spi_gpio);
  320. /* Determine if we have chip selects connected */
  321. spi_gpio->has_cs = !!pdata->num_chipselect;
  322. spi_gpio->pdev = pdev;
  323. if (pdata)
  324. spi_gpio->pdata = *pdata;
  325. status = spi_gpio_request(&pdev->dev, spi_gpio,
  326. pdata->num_chipselect, &master_flags);
  327. if (status)
  328. return status;
  329. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  330. master->flags = master_flags;
  331. master->bus_num = pdev->id;
  332. /* The master needs to think there is a chipselect even if not connected */
  333. master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1;
  334. master->setup = spi_gpio_setup;
  335. master->cleanup = spi_gpio_cleanup;
  336. #ifdef CONFIG_OF
  337. master->dev.of_node = pdev->dev.of_node;
  338. #endif
  339. spi_gpio->bitbang.master = master;
  340. spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
  341. if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
  342. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  343. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  344. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  345. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  346. } else {
  347. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
  348. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
  349. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
  350. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
  351. }
  352. spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  353. spi_gpio->bitbang.flags = SPI_CS_HIGH;
  354. status = spi_bitbang_start(&spi_gpio->bitbang);
  355. if (status)
  356. spi_master_put(master);
  357. return status;
  358. }
  359. static int spi_gpio_remove(struct platform_device *pdev)
  360. {
  361. struct spi_gpio *spi_gpio;
  362. struct spi_gpio_platform_data *pdata;
  363. spi_gpio = platform_get_drvdata(pdev);
  364. pdata = dev_get_platdata(&pdev->dev);
  365. /* stop() unregisters child devices too */
  366. spi_bitbang_stop(&spi_gpio->bitbang);
  367. spi_master_put(spi_gpio->bitbang.master);
  368. return 0;
  369. }
  370. MODULE_ALIAS("platform:" DRIVER_NAME);
  371. static struct platform_driver spi_gpio_driver = {
  372. .driver = {
  373. .name = DRIVER_NAME,
  374. .of_match_table = of_match_ptr(spi_gpio_dt_ids),
  375. },
  376. .probe = spi_gpio_probe,
  377. .remove = spi_gpio_remove,
  378. };
  379. module_platform_driver(spi_gpio_driver);
  380. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  381. MODULE_AUTHOR("David Brownell");
  382. MODULE_LICENSE("GPL");