spi-gpio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/gpio.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/spi_bitbang.h>
  29. #include <linux/spi/spi_gpio.h>
  30. /*
  31. * This bitbanging SPI master driver should help make systems usable
  32. * when a native hardware SPI engine is not available, perhaps because
  33. * its driver isn't yet working or because the I/O pins it requires
  34. * are used for other purposes.
  35. *
  36. * platform_device->driver_data ... points to spi_gpio
  37. *
  38. * spi->controller_state ... reserved for bitbang framework code
  39. * spi->controller_data ... holds chipselect GPIO
  40. *
  41. * spi->master->dev.driver_data ... points to spi_gpio->bitbang
  42. */
  43. struct spi_gpio {
  44. struct spi_bitbang bitbang;
  45. struct spi_gpio_platform_data pdata;
  46. struct platform_device *pdev;
  47. int cs_gpios[0];
  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. /* all functions referencing these symbols must define pdata */
  76. #define SPI_MISO_GPIO ((pdata)->miso)
  77. #define SPI_MOSI_GPIO ((pdata)->mosi)
  78. #define SPI_SCK_GPIO ((pdata)->sck)
  79. #define SPI_N_CHIPSEL ((pdata)->num_chipselect)
  80. #endif
  81. /*----------------------------------------------------------------------*/
  82. static inline struct spi_gpio * __pure
  83. spi_to_spi_gpio(const struct spi_device *spi)
  84. {
  85. const struct spi_bitbang *bang;
  86. struct spi_gpio *spi_gpio;
  87. bang = spi_master_get_devdata(spi->master);
  88. spi_gpio = container_of(bang, struct spi_gpio, bitbang);
  89. return spi_gpio;
  90. }
  91. static inline struct spi_gpio_platform_data * __pure
  92. spi_to_pdata(const struct spi_device *spi)
  93. {
  94. return &spi_to_spi_gpio(spi)->pdata;
  95. }
  96. /* this is #defined to avoid unused-variable warnings when inlining */
  97. #define pdata spi_to_pdata(spi)
  98. static inline void setsck(const struct spi_device *spi, int is_on)
  99. {
  100. gpio_set_value(SPI_SCK_GPIO, is_on);
  101. }
  102. static inline void setmosi(const struct spi_device *spi, int is_on)
  103. {
  104. gpio_set_value(SPI_MOSI_GPIO, is_on);
  105. }
  106. static inline int getmiso(const struct spi_device *spi)
  107. {
  108. return !!gpio_get_value(SPI_MISO_GPIO);
  109. }
  110. #undef pdata
  111. /*
  112. * NOTE: this clocks "as fast as we can". It "should" be a function of the
  113. * requested device clock. Software overhead means we usually have trouble
  114. * reaching even one Mbit/sec (except when we can inline bitops), so for now
  115. * we'll just assume we never need additional per-bit slowdowns.
  116. */
  117. #define spidelay(nsecs) do {} while (0)
  118. #include "spi-bitbang-txrx.h"
  119. /*
  120. * These functions can leverage inline expansion of GPIO calls to shrink
  121. * costs for a txrx bit, often by factors of around ten (by instruction
  122. * count). That is particularly visible for larger word sizes, but helps
  123. * even with default 8-bit words.
  124. *
  125. * REVISIT overheads calling these functions for each word also have
  126. * significant performance costs. Having txrx_bufs() calls that inline
  127. * the txrx_word() logic would help performance, e.g. on larger blocks
  128. * used with flash storage or MMC/SD. There should also be ways to make
  129. * GCC be less stupid about reloading registers inside the I/O loops,
  130. * even without inlined GPIO calls; __attribute__((hot)) on GCC 4.3?
  131. */
  132. static u32 spi_gpio_txrx_word_mode0(struct spi_device *spi,
  133. unsigned nsecs, u32 word, u8 bits)
  134. {
  135. return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
  136. }
  137. static u32 spi_gpio_txrx_word_mode1(struct spi_device *spi,
  138. unsigned nsecs, u32 word, u8 bits)
  139. {
  140. return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits);
  141. }
  142. static u32 spi_gpio_txrx_word_mode2(struct spi_device *spi,
  143. unsigned nsecs, u32 word, u8 bits)
  144. {
  145. return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits);
  146. }
  147. static u32 spi_gpio_txrx_word_mode3(struct spi_device *spi,
  148. unsigned nsecs, u32 word, u8 bits)
  149. {
  150. return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits);
  151. }
  152. /*
  153. * These functions do not call setmosi or getmiso if respective flag
  154. * (SPI_MASTER_NO_RX or SPI_MASTER_NO_TX) is set, so they are safe to
  155. * call when such pin is not present or defined in the controller.
  156. * A separate set of callbacks is defined to get highest possible
  157. * speed in the generic case (when both MISO and MOSI lines are
  158. * available), as optimiser will remove the checks when argument is
  159. * constant.
  160. */
  161. static u32 spi_gpio_spec_txrx_word_mode0(struct spi_device *spi,
  162. unsigned nsecs, u32 word, u8 bits)
  163. {
  164. unsigned flags = spi->master->flags;
  165. return bitbang_txrx_be_cpha0(spi, nsecs, 0, flags, word, bits);
  166. }
  167. static u32 spi_gpio_spec_txrx_word_mode1(struct spi_device *spi,
  168. unsigned nsecs, u32 word, u8 bits)
  169. {
  170. unsigned flags = spi->master->flags;
  171. return bitbang_txrx_be_cpha1(spi, nsecs, 0, flags, word, bits);
  172. }
  173. static u32 spi_gpio_spec_txrx_word_mode2(struct spi_device *spi,
  174. unsigned nsecs, u32 word, u8 bits)
  175. {
  176. unsigned flags = spi->master->flags;
  177. return bitbang_txrx_be_cpha0(spi, nsecs, 1, flags, word, bits);
  178. }
  179. static u32 spi_gpio_spec_txrx_word_mode3(struct spi_device *spi,
  180. unsigned nsecs, u32 word, u8 bits)
  181. {
  182. unsigned flags = spi->master->flags;
  183. return bitbang_txrx_be_cpha1(spi, nsecs, 1, flags, word, bits);
  184. }
  185. /*----------------------------------------------------------------------*/
  186. static void spi_gpio_chipselect(struct spi_device *spi, int is_active)
  187. {
  188. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  189. unsigned int cs = spi_gpio->cs_gpios[spi->chip_select];
  190. /* set initial clock polarity */
  191. if (is_active)
  192. setsck(spi, spi->mode & SPI_CPOL);
  193. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  194. /* SPI is normally active-low */
  195. gpio_set_value(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  196. }
  197. }
  198. static int spi_gpio_setup(struct spi_device *spi)
  199. {
  200. unsigned int cs;
  201. int status = 0;
  202. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  203. struct device_node *np = spi->master->dev.of_node;
  204. if (spi->bits_per_word > 32)
  205. return -EINVAL;
  206. if (np) {
  207. /*
  208. * In DT environments, the CS GPIOs have already been
  209. * initialized from the "cs-gpios" property of the node.
  210. */
  211. cs = spi_gpio->cs_gpios[spi->chip_select];
  212. } else {
  213. /*
  214. * ... otherwise, take it from spi->controller_data
  215. */
  216. cs = (unsigned int) spi->controller_data;
  217. }
  218. if (!spi->controller_state) {
  219. if (cs != SPI_GPIO_NO_CHIPSELECT) {
  220. status = gpio_request(cs, dev_name(&spi->dev));
  221. if (status)
  222. return status;
  223. status = gpio_direction_output(cs,
  224. !(spi->mode & SPI_CS_HIGH));
  225. }
  226. }
  227. if (!status) {
  228. status = spi_bitbang_setup(spi);
  229. /* in case it was initialized from static board data */
  230. spi_gpio->cs_gpios[spi->chip_select] = cs;
  231. }
  232. if (status) {
  233. if (!spi->controller_state && cs != SPI_GPIO_NO_CHIPSELECT)
  234. gpio_free(cs);
  235. }
  236. return status;
  237. }
  238. static void spi_gpio_cleanup(struct spi_device *spi)
  239. {
  240. struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
  241. unsigned int cs = spi_gpio->cs_gpios[spi->chip_select];
  242. if (cs != SPI_GPIO_NO_CHIPSELECT)
  243. gpio_free(cs);
  244. spi_bitbang_cleanup(spi);
  245. }
  246. static int spi_gpio_alloc(unsigned pin, const char *label, bool is_in)
  247. {
  248. int value;
  249. value = gpio_request(pin, label);
  250. if (value == 0) {
  251. if (is_in)
  252. value = gpio_direction_input(pin);
  253. else
  254. value = gpio_direction_output(pin, 0);
  255. }
  256. return value;
  257. }
  258. static int spi_gpio_request(struct spi_gpio_platform_data *pdata,
  259. const char *label, u16 *res_flags)
  260. {
  261. int value;
  262. /* NOTE: SPI_*_GPIO symbols may reference "pdata" */
  263. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI) {
  264. value = spi_gpio_alloc(SPI_MOSI_GPIO, label, false);
  265. if (value)
  266. goto done;
  267. } else {
  268. /* HW configuration without MOSI pin */
  269. *res_flags |= SPI_MASTER_NO_TX;
  270. }
  271. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO) {
  272. value = spi_gpio_alloc(SPI_MISO_GPIO, label, true);
  273. if (value)
  274. goto free_mosi;
  275. } else {
  276. /* HW configuration without MISO pin */
  277. *res_flags |= SPI_MASTER_NO_RX;
  278. }
  279. value = spi_gpio_alloc(SPI_SCK_GPIO, label, false);
  280. if (value)
  281. goto free_miso;
  282. goto done;
  283. free_miso:
  284. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  285. gpio_free(SPI_MISO_GPIO);
  286. free_mosi:
  287. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  288. gpio_free(SPI_MOSI_GPIO);
  289. done:
  290. return value;
  291. }
  292. #ifdef CONFIG_OF
  293. static struct of_device_id spi_gpio_dt_ids[] = {
  294. { .compatible = "spi-gpio" },
  295. {}
  296. };
  297. MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
  298. static int spi_gpio_probe_dt(struct platform_device *pdev)
  299. {
  300. int ret;
  301. u32 tmp;
  302. struct spi_gpio_platform_data *pdata;
  303. struct device_node *np = pdev->dev.of_node;
  304. const struct of_device_id *of_id =
  305. of_match_device(spi_gpio_dt_ids, &pdev->dev);
  306. if (!of_id)
  307. return 0;
  308. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  309. if (!pdata)
  310. return -ENOMEM;
  311. ret = of_get_named_gpio(np, "gpio-sck", 0);
  312. if (ret < 0) {
  313. dev_err(&pdev->dev, "gpio-sck property not found\n");
  314. goto error_free;
  315. }
  316. pdata->sck = ret;
  317. ret = of_get_named_gpio(np, "gpio-miso", 0);
  318. if (ret < 0) {
  319. dev_info(&pdev->dev, "gpio-miso property not found, switching to no-rx mode\n");
  320. pdata->miso = SPI_GPIO_NO_MISO;
  321. } else
  322. pdata->miso = ret;
  323. ret = of_get_named_gpio(np, "gpio-mosi", 0);
  324. if (ret < 0) {
  325. dev_info(&pdev->dev, "gpio-mosi property not found, switching to no-tx mode\n");
  326. pdata->mosi = SPI_GPIO_NO_MOSI;
  327. } else
  328. pdata->mosi = ret;
  329. ret = of_property_read_u32(np, "num-chipselects", &tmp);
  330. if (ret < 0) {
  331. dev_err(&pdev->dev, "num-chipselects property not found\n");
  332. goto error_free;
  333. }
  334. pdata->num_chipselect = tmp;
  335. pdev->dev.platform_data = pdata;
  336. return 1;
  337. error_free:
  338. devm_kfree(&pdev->dev, pdata);
  339. return ret;
  340. }
  341. #else
  342. static inline int spi_gpio_probe_dt(struct platform_device *pdev)
  343. {
  344. return 0;
  345. }
  346. #endif
  347. static int spi_gpio_probe(struct platform_device *pdev)
  348. {
  349. int status;
  350. struct spi_master *master;
  351. struct spi_gpio *spi_gpio;
  352. struct spi_gpio_platform_data *pdata;
  353. u16 master_flags = 0;
  354. bool use_of = 0;
  355. status = spi_gpio_probe_dt(pdev);
  356. if (status < 0)
  357. return status;
  358. if (status > 0)
  359. use_of = 1;
  360. pdata = pdev->dev.platform_data;
  361. #ifdef GENERIC_BITBANG
  362. if (!pdata || !pdata->num_chipselect)
  363. return -ENODEV;
  364. #endif
  365. status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags);
  366. if (status < 0)
  367. return status;
  368. master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) +
  369. (sizeof(int) * SPI_N_CHIPSEL));
  370. if (!master) {
  371. status = -ENOMEM;
  372. goto gpio_free;
  373. }
  374. spi_gpio = spi_master_get_devdata(master);
  375. platform_set_drvdata(pdev, spi_gpio);
  376. spi_gpio->pdev = pdev;
  377. if (pdata)
  378. spi_gpio->pdata = *pdata;
  379. master->flags = master_flags;
  380. master->bus_num = pdev->id;
  381. master->num_chipselect = SPI_N_CHIPSEL;
  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. for (i = 0; i < SPI_N_CHIPSEL; i++)
  394. spi_gpio->cs_gpios[i] =
  395. of_get_named_gpio(np, "cs-gpios", i);
  396. }
  397. #endif
  398. spi_gpio->bitbang.master = spi_master_get(master);
  399. spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
  400. if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
  401. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  402. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  403. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  404. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  405. } else {
  406. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
  407. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
  408. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
  409. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
  410. }
  411. spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  412. spi_gpio->bitbang.flags = SPI_CS_HIGH;
  413. status = spi_bitbang_start(&spi_gpio->bitbang);
  414. if (status < 0) {
  415. spi_master_put(spi_gpio->bitbang.master);
  416. gpio_free:
  417. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  418. gpio_free(SPI_MISO_GPIO);
  419. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  420. gpio_free(SPI_MOSI_GPIO);
  421. gpio_free(SPI_SCK_GPIO);
  422. spi_master_put(master);
  423. }
  424. return status;
  425. }
  426. static int spi_gpio_remove(struct platform_device *pdev)
  427. {
  428. struct spi_gpio *spi_gpio;
  429. struct spi_gpio_platform_data *pdata;
  430. int status;
  431. spi_gpio = platform_get_drvdata(pdev);
  432. pdata = pdev->dev.platform_data;
  433. /* stop() unregisters child devices too */
  434. status = spi_bitbang_stop(&spi_gpio->bitbang);
  435. spi_master_put(spi_gpio->bitbang.master);
  436. platform_set_drvdata(pdev, NULL);
  437. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  438. gpio_free(SPI_MISO_GPIO);
  439. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  440. gpio_free(SPI_MOSI_GPIO);
  441. gpio_free(SPI_SCK_GPIO);
  442. return status;
  443. }
  444. MODULE_ALIAS("platform:" DRIVER_NAME);
  445. static struct platform_driver spi_gpio_driver = {
  446. .driver = {
  447. .name = DRIVER_NAME,
  448. .owner = THIS_MODULE,
  449. .of_match_table = of_match_ptr(spi_gpio_dt_ids),
  450. },
  451. .probe = spi_gpio_probe,
  452. .remove = spi_gpio_remove,
  453. };
  454. module_platform_driver(spi_gpio_driver);
  455. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  456. MODULE_AUTHOR("David Brownell");
  457. MODULE_LICENSE("GPL");