spi-gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. pdata->sck = of_get_named_gpio(np, "gpio-sck", 0);
  312. pdata->miso = of_get_named_gpio(np, "gpio-miso", 0);
  313. pdata->mosi = of_get_named_gpio(np, "gpio-mosi", 0);
  314. ret = of_property_read_u32(np, "num-chipselects", &tmp);
  315. if (ret < 0) {
  316. dev_err(&pdev->dev, "num-chipselects property not found\n");
  317. goto error_free;
  318. }
  319. pdata->num_chipselect = tmp;
  320. pdev->dev.platform_data = pdata;
  321. return 1;
  322. error_free:
  323. devm_kfree(&pdev->dev, pdata);
  324. return ret;
  325. }
  326. #else
  327. static inline int spi_gpio_probe_dt(struct platform_device *pdev)
  328. {
  329. return 0;
  330. }
  331. #endif
  332. static int spi_gpio_probe(struct platform_device *pdev)
  333. {
  334. int status;
  335. struct spi_master *master;
  336. struct spi_gpio *spi_gpio;
  337. struct spi_gpio_platform_data *pdata;
  338. u16 master_flags = 0;
  339. bool use_of = 0;
  340. status = spi_gpio_probe_dt(pdev);
  341. if (status < 0)
  342. return status;
  343. if (status > 0)
  344. use_of = 1;
  345. pdata = pdev->dev.platform_data;
  346. #ifdef GENERIC_BITBANG
  347. if (!pdata || !pdata->num_chipselect)
  348. return -ENODEV;
  349. #endif
  350. status = spi_gpio_request(pdata, dev_name(&pdev->dev), &master_flags);
  351. if (status < 0)
  352. return status;
  353. master = spi_alloc_master(&pdev->dev, sizeof(*spi_gpio) +
  354. (sizeof(int) * SPI_N_CHIPSEL));
  355. if (!master) {
  356. status = -ENOMEM;
  357. goto gpio_free;
  358. }
  359. spi_gpio = spi_master_get_devdata(master);
  360. platform_set_drvdata(pdev, spi_gpio);
  361. spi_gpio->pdev = pdev;
  362. if (pdata)
  363. spi_gpio->pdata = *pdata;
  364. master->flags = master_flags;
  365. master->bus_num = pdev->id;
  366. master->num_chipselect = SPI_N_CHIPSEL;
  367. master->setup = spi_gpio_setup;
  368. master->cleanup = spi_gpio_cleanup;
  369. #ifdef CONFIG_OF
  370. master->dev.of_node = pdev->dev.of_node;
  371. if (use_of) {
  372. int i;
  373. struct device_node *np = pdev->dev.of_node;
  374. /*
  375. * In DT environments, take the CS GPIO from the "cs-gpios"
  376. * property of the node.
  377. */
  378. for (i = 0; i < SPI_N_CHIPSEL; i++)
  379. spi_gpio->cs_gpios[i] =
  380. of_get_named_gpio(np, "cs-gpios", i);
  381. }
  382. #endif
  383. spi_gpio->bitbang.master = spi_master_get(master);
  384. spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
  385. if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) {
  386. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
  387. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
  388. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
  389. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
  390. } else {
  391. spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
  392. spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
  393. spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
  394. spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
  395. }
  396. spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  397. spi_gpio->bitbang.flags = SPI_CS_HIGH;
  398. status = spi_bitbang_start(&spi_gpio->bitbang);
  399. if (status < 0) {
  400. spi_master_put(spi_gpio->bitbang.master);
  401. gpio_free:
  402. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  403. gpio_free(SPI_MISO_GPIO);
  404. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  405. gpio_free(SPI_MOSI_GPIO);
  406. gpio_free(SPI_SCK_GPIO);
  407. spi_master_put(master);
  408. }
  409. return status;
  410. }
  411. static int spi_gpio_remove(struct platform_device *pdev)
  412. {
  413. struct spi_gpio *spi_gpio;
  414. struct spi_gpio_platform_data *pdata;
  415. int status;
  416. spi_gpio = platform_get_drvdata(pdev);
  417. pdata = pdev->dev.platform_data;
  418. /* stop() unregisters child devices too */
  419. status = spi_bitbang_stop(&spi_gpio->bitbang);
  420. spi_master_put(spi_gpio->bitbang.master);
  421. platform_set_drvdata(pdev, NULL);
  422. if (SPI_MISO_GPIO != SPI_GPIO_NO_MISO)
  423. gpio_free(SPI_MISO_GPIO);
  424. if (SPI_MOSI_GPIO != SPI_GPIO_NO_MOSI)
  425. gpio_free(SPI_MOSI_GPIO);
  426. gpio_free(SPI_SCK_GPIO);
  427. return status;
  428. }
  429. MODULE_ALIAS("platform:" DRIVER_NAME);
  430. static struct platform_driver spi_gpio_driver = {
  431. .driver = {
  432. .name = DRIVER_NAME,
  433. .owner = THIS_MODULE,
  434. .of_match_table = of_match_ptr(spi_gpio_dt_ids),
  435. },
  436. .probe = spi_gpio_probe,
  437. .remove = spi_gpio_remove,
  438. };
  439. module_platform_driver(spi_gpio_driver);
  440. MODULE_DESCRIPTION("SPI master driver using generic bitbanged GPIO ");
  441. MODULE_AUTHOR("David Brownell");
  442. MODULE_LICENSE("GPL");