spi-gpio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * SPI master driver using generic bitbanged GPIO
  3. *
  4. * Copyright (C) 2006,2008 David Brownell
  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/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/gpio.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_gpio.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. unsigned long cs_gpios[0];
  44. };
  45. /*----------------------------------------------------------------------*/
  46. /*
  47. * Because the overhead of going through four GPIO procedure calls
  48. * per transferred bit can make performance a problem, this code
  49. * is set up so that you can use it in either of two ways:
  50. *
  51. * - The slow generic way: set up platform_data to hold the GPIO
  52. * numbers used for MISO/MOSI/SCK, and issue procedure calls for
  53. * each of them. This driver can handle several such busses.
  54. *
  55. * - The quicker inlined way: only helps with platform GPIO code
  56. * that inlines operations for constant GPIOs. This can give
  57. * you tight (fast!) inner loops, but each such bus needs a
  58. * new driver. You'll define a new C file, with Makefile and
  59. * Kconfig support; the C code can be a total of six lines:
  60. *
  61. * #define DRIVER_NAME "myboard_spi2"
  62. * #define SPI_MISO_GPIO 119
  63. * #define SPI_MOSI_GPIO 120
  64. * #define SPI_SCK_GPIO 121
  65. * #define SPI_N_CHIPSEL 4
  66. * #include "spi-gpio.c"
  67. */
  68. #ifndef DRIVER_NAME
  69. #define DRIVER_NAME "spi_gpio"
  70. #define GENERIC_BITBANG /* vs tight inlines */
  71. /* all functions referencing these symbols must define pdata */
  72. #define SPI_MISO_GPIO ((pdata)->miso)
  73. #define SPI_MOSI_GPIO ((pdata)->mosi)
  74. #define SPI_SCK_GPIO ((pdata)->sck)
  75. #define SPI_N_CHIPSEL ((pdata)->num_chipselect)
  76. #endif
  77. /*----------------------------------------------------------------------*/
  78. static inline struct spi_gpio *__pure
  79. spi_to_spi_gpio(const struct spi_device *spi)
  80. {
  81. const struct spi_bitbang *bang;
  82. struct spi_gpio *spi_gpio;
  83. bang = spi_master_get_devdata(spi->master);
  84. spi_gpio = container_of(bang, struct spi_gpio, bitbang);
  85. return spi_gpio;
  86. }
  87. static inline struct spi_gpio_platform_data *__pure
  88. spi_to_pdata(const struct spi_device *spi)
  89. {
  90. return &spi_to_spi_gpio(spi)->pdata;
  91. }
  92. /* this is #defined to avoid unused-variable warnings when inlining */
  93. #define pdata spi_to_pdata(spi)
  94. static inline void setsck(const struct spi_device *spi, int is_on)
  95. {
  96. gpio_set_value_cansleep(SPI_SCK_GPIO, is_on);
  97. }
  98. static inline void setmosi(const struct spi_device *spi, int is_on)
  99. {
  100. gpio_set_value_cansleep(SPI_MOSI_GPIO, is_on);
  101. }
  102. static inline int getmiso(const struct spi_device *spi)
  103. {
  104. return !!gpio_get_value_cansleep(SPI_MISO_GPIO);
  105. }
  106. #undef pdata
  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. unsigned long cs = spi_gpio->cs_gpios[spi->chip_select];
  186. /* set initial clock polarity */
  187. if (is_active)
  188. setsck(spi, spi->mode & SPI_CPOL);
  189. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  190. /* SPI is normally active-low */
  191. gpio_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  192. }
  193. }
  194. static int spi_gpio_setup(struct spi_device *spi)
  195. {
  196. unsigned long cs;
  197. int status = 0;
  198. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  199. struct device_node *np = spi->master->dev.of_node;
  200. if (np) {
  201. /*
  202. * In DT environments, the CS GPIOs have already been
  203. * initialized from the "cs-gpios" property of the node.
  204. */
  205. cs = spi_gpio->cs_gpios[spi->chip_select];
  206. } else {
  207. /*
  208. * ... otherwise, take it from spi->controller_data
  209. */
  210. cs = (uintptr_t) spi->controller_data;
  211. }
  212. if (!spi->controller_state) {
  213. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  214. status = gpio_request(cs, dev_name(&spi->dev));
  215. if (status)
  216. return status;
  217. status = gpio_direction_output(cs,
  218. !(spi->mode & SPI_CS_HIGH));
  219. }
  220. }
  221. if (!status) {
  222. /* in case it was initialized from static board data */
  223. spi_gpio->cs_gpios[spi->chip_select] = cs;
  224. status = spi_bitbang_setup(spi);
  225. }
  226. if (status) {
  227. if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
  228. gpio_free(cs);
  229. }
  230. return status;
  231. }
  232. static void spi_gpio_cleanup(struct spi_device *spi)
  233. {
  234. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  235. unsigned long cs = spi_gpio->cs_gpios[spi->chip_select];
  236. if (cs != SPI_GPIO_NO_CHIPSELECT)
  237. gpio_free(cs);
  238. spi_bitbang_cleanup(spi);
  239. }
  240. static int spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
  241. {
  242. int value;
  243. value = gpio_request(pin, label);
  244. if (value == 0) {
  245. if (is_in)
  246. value = gpio_direction_input(pin);
  247. else
  248. value = gpio_direction_output(pin, 0);
  249. }
  250. return value;
  251. }
  252. static int spi_gpio_request(struct spi_gpio_platform_data *pdata,
  253. const char *label, u16 *res_flags)
  254. {
  255. int value;
  256. /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
  257. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) {
  258. value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
  259. if (value)
  260. goto done;
  261. } else {
  262. /* HW configuration without MOSI pin */
  263. *res_flags |= SPI_MASTER_NO_TX;
  264. }
  265. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) {
  266. value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
  267. if (value)
  268. goto free_mosi;
  269. } else {
  270. /* HW configuration without MISO pin */
  271. *res_flags |= SPI_MASTER_NO_RX;
  272. }
  273. value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
  274. if (value)
  275. goto free_miso;
  276. goto done;
  277. free_miso:
  278. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  279. gpio_free(SPI_MISO_GPIO);
  280. free_mosi:
  281. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  282. gpio_free(SPI_MOSI_GPIO);
  283. done:
  284. return value;
  285. }
  286. #ifdef CONFIG_OF
  287. static const struct of_device_id spi_gpio_dt_ids[] = {
  288. { .compatible = "spi-gpio" },
  289. {}
  290. };
  291. MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
  292. static int spi_gpio_probe_dt(struct platform_device *pdev)
  293. {
  294. int ret;
  295. u32 tmp;
  296. struct spi_gpio_platform_data *pdata;
  297. struct device_node *np = pdev->dev.of_node;
  298. const struct of_device_id *of_id =
  299. of_match_device(spi_gpio_dt_ids, &pdev->dev);
  300. if (!of_id)
  301. return 0;
  302. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  303. if (!pdata)
  304. return -ENOMEM;
  305. ret = of_get_named_gpio(np, "gpio-sck", 0);
  306. if (ret < 0) {
  307. dev_err(&pdev->dev, "gpio-sck property not found\n");
  308. goto error_free;
  309. }
  310. pdata->sck = ret;
  311. ret = of_get_named_gpio(np, "gpio-miso", 0);
  312. if (ret < 0) {
  313. dev_info(&pdev->dev, "gpio-miso property not found, switching to no-rx mode\n");
  314. pdata->miso = SPI_GPIO_NO_MISO;
  315. } else
  316. pdata->miso = ret;
  317. ret = of_get_named_gpio(np, "gpio-mosi", 0);
  318. if (ret < 0) {
  319. dev_info(&pdev->dev, "gpio-mosi property not found, switching to no-tx mode\n");
  320. pdata->mosi = SPI_GPIO_NO_MOSI;
  321. } else
  322. pdata->mosi = ret;
  323. ret = of_property_read_u32(np, "num-chipselects", &tmp);
  324. if (ret < 0) {
  325. dev_err(&pdev->dev, "num-chipselects property not found\n");
  326. goto error_free;
  327. }
  328. pdata->num_chipselect = tmp;
  329. pdev->dev.platform_data = pdata;
  330. return 1;
  331. error_free:
  332. devm_kfree(&pdev->dev, pdata);
  333. return ret;
  334. }
  335. #else
  336. static inline int spi_gpio_probe_dt(struct platform_device *pdev)
  337. {
  338. return 0;
  339. }
  340. #endif
  341. static int spi_gpio_probe(struct platform_device *pdev)
  342. {
  343. int status;
  344. struct spi_master *master;
  345. struct spi_gpio *spi_gpio;
  346. struct spi_gpio_platform_data *pdata;
  347. u16 master_flags = 0;
  348. bool use_of = 0;
  349. int num_devices;
  350. status = spi_gpio_probe_dt(pdev);
  351. if (status < 0)
  352. return status;
  353. if (status > 0)
  354. use_of = 1;
  355. pdata = dev_get_platdata(&pdev->dev);
  356. #ifdef GENERIC_BITBANG
  357. if (!pdata || (!use_of && !pdata->num_chipselect))
  358. return -ENODEV;
  359. #endif
  360. if (use_of && !SPI_N_CHIPSEL)
  361. num_devices = 1;
  362. else
  363. num_devices = SPI_N_CHIPSEL;
  364. status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags);
  365. if (status < 0)
  366. return status;
  367. master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) +
  368. (sizeof(unsigned long) * num_devices));
  369. if (!master) {
  370. status = -ENOMEM;
  371. goto gpio_free;
  372. }
  373. spi_gpio = spi_master_get_devdata(master);
  374. platform_set_drvdata(pdev, spi_gpio);
  375. spi_gpio->pdev = pdev;
  376. if (pdata)
  377. spi_gpio->pdata = *pdata;
  378. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  379. master->flags = master_flags;
  380. master->bus_num = pdev->id;
  381. master->num_chipselect = num_devices;
  382. master->setup = spi_gpio_setup;
  383. master->cleanup = spi_gpio_cleanup;
  384. #ifdef CONFIG_OF
  385. master->dev.of_node = pdev->dev.of_node;
  386. if (use_of) {
  387. int i;
  388. struct device_node *np = pdev->dev.of_node;
  389. /*
  390. * In DT environments, take the CS GPIO from the "cs-gpios"
  391. * property of the node.
  392. */
  393. if (!SPI_N_CHIPSEL)
  394. spi_gpio->cs_gpios[0] = SPI_GPIO_NO_CHIPSELECT;
  395. else
  396. for (i = 0; i < SPI_N_CHIPSEL; i++) {
  397. status = of_get_named_gpio(np, "cs-gpios", i);
  398. if (status < 0) {
  399. dev_err(&pdev->dev,
  400. "invalid cs-gpios property\n");
  401. goto gpio_free;
  402. }
  403. spi_gpio->cs_gpios[i] = status;
  404. }
  405. }
  406. #endif
  407. spi_gpio->bitbang.master = master;
  408. spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
  409. if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
  410. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  411. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  412. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  413. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  414. } else {
  415. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
  416. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
  417. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
  418. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
  419. }
  420. spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  421. spi_gpio->bitbang.flags = SPI_CS_HIGH;
  422. status = spi_bitbang_start(&spi_gpio->bitbang);
  423. if (status < 0) {
  424. gpio_free:
  425. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  426. gpio_free(SPI_MISO_GPIO);
  427. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  428. gpio_free(SPI_MOSI_GPIO);
  429. gpio_free(SPI_SCK_GPIO);
  430. spi_master_put(master);
  431. }
  432. return status;
  433. }
  434. static int spi_gpio_remove(struct platform_device *pdev)
  435. {
  436. struct spi_gpio *spi_gpio;
  437. struct spi_gpio_platform_data *pdata;
  438. spi_gpio = platform_get_drvdata(pdev);
  439. pdata = dev_get_platdata(&pdev->dev);
  440. /* stop() unregisters child devices too */
  441. spi_bitbang_stop(&spi_gpio->bitbang);
  442. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  443. gpio_free(SPI_MISO_GPIO);
  444. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  445. gpio_free(SPI_MOSI_GPIO);
  446. gpio_free(SPI_SCK_GPIO);
  447. spi_master_put(spi_gpio->bitbang.master);
  448. return 0;
  449. }
  450. MODULE_ALIAS("platform:" DRIVER_NAME);
  451. static struct platform_driver spi_gpio_driver = {
  452. .driver = {
  453. .name = DRIVER_NAME,
  454. .of_match_table = of_match_ptr(spi_gpio_dt_ids),
  455. },
  456. .probe = spi_gpio_probe,
  457. .remove = spi_gpio_remove,
  458. };
  459. module_platform_driver(spi_gpio_driver);
  460. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  461. MODULE_AUTHOR("David Brownell");
  462. MODULE_LICENSE("GPL");