spi-gpio.c 15 KB

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