862-gpio_spi_driver.patch 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. THIS CODE IS DEPRECATED.
  2. Please use the new mainline SPI-GPIO driver, as of 2.6.29.
  3. --mb
  4. ---
  5. drivers/spi/Kconfig | 9 +
  6. drivers/spi/Makefile | 1
  7. drivers/spi/spi_gpio_old.c | 251 +++++++++++++++++++++++++++++++++++++++
  8. include/linux/spi/spi_gpio_old.h | 73 +++++++++++
  9. 4 files changed, 334 insertions(+)
  10. --- /dev/null
  11. +++ b/include/linux/spi/spi_gpio_old.h
  12. @@ -0,0 +1,73 @@
  13. +/*
  14. + * spi_gpio interface to platform code
  15. + *
  16. + * Copyright (c) 2008 Piotr Skamruk
  17. + * Copyright (c) 2008 Michael Buesch
  18. + *
  19. + * This program is free software; you can redistribute it and/or modify
  20. + * it under the terms of the GNU General Public License version 2 as
  21. + * published by the Free Software Foundation.
  22. + */
  23. +#ifndef _LINUX_SPI_SPI_GPIO
  24. +#define _LINUX_SPI_SPI_GPIO
  25. +
  26. +#include <linux/types.h>
  27. +#include <linux/spi/spi.h>
  28. +
  29. +
  30. +/**
  31. + * struct spi_gpio_platform_data - Data definitions for a SPI-GPIO device.
  32. + *
  33. + * This structure holds information about a GPIO-based SPI device.
  34. + *
  35. + * @pin_clk: The GPIO pin number of the CLOCK pin.
  36. + *
  37. + * @pin_miso: The GPIO pin number of the MISO pin.
  38. + *
  39. + * @pin_mosi: The GPIO pin number of the MOSI pin.
  40. + *
  41. + * @pin_cs: The GPIO pin number of the CHIPSELECT pin.
  42. + *
  43. + * @cs_activelow: If true, the chip is selected when the CS line is low.
  44. + *
  45. + * @no_spi_delay: If true, no delay is done in the lowlevel bitbanging.
  46. + * Note that doing no delay is not standards compliant,
  47. + * but it might be needed to speed up transfers on some
  48. + * slow embedded machines.
  49. + *
  50. + * @boardinfo_setup: This callback is called after the
  51. + * SPI master device was registered, but before the
  52. + * device is registered.
  53. + * @boardinfo_setup_data: Data argument passed to boardinfo_setup().
  54. + */
  55. +struct spi_gpio_platform_data {
  56. + unsigned int pin_clk;
  57. + unsigned int pin_miso;
  58. + unsigned int pin_mosi;
  59. + unsigned int pin_cs;
  60. + bool cs_activelow;
  61. + bool no_spi_delay;
  62. + int (*boardinfo_setup)(struct spi_board_info *bi,
  63. + struct spi_master *master,
  64. + void *data);
  65. + void *boardinfo_setup_data;
  66. +};
  67. +
  68. +/**
  69. + * SPI_GPIO_PLATDEV_NAME - The platform device name string.
  70. + *
  71. + * The name string that has to be used for platform_device_alloc
  72. + * when allocating a spi-gpio device.
  73. + */
  74. +#define SPI_GPIO_PLATDEV_NAME "spi-gpio"
  75. +
  76. +/**
  77. + * spi_gpio_next_id - Get another platform device ID number.
  78. + *
  79. + * This returns the next platform device ID number that has to be used
  80. + * for platform_device_alloc. The ID is opaque and should not be used for
  81. + * anything else.
  82. + */
  83. +int spi_gpio_next_id(void);
  84. +
  85. +#endif /* _LINUX_SPI_SPI_GPIO */
  86. --- /dev/null
  87. +++ b/drivers/spi/spi_gpio_old.c
  88. @@ -0,0 +1,251 @@
  89. +/*
  90. + * Bitbanging SPI bus driver using GPIO API
  91. + *
  92. + * Copyright (c) 2008 Piotr Skamruk
  93. + * Copyright (c) 2008 Michael Buesch
  94. + *
  95. + * based on spi_s3c2410_gpio.c
  96. + * Copyright (c) 2006 Ben Dooks
  97. + * Copyright (c) 2006 Simtec Electronics
  98. + * and on i2c-gpio.c
  99. + * Copyright (C) 2007 Atmel Corporation
  100. + *
  101. + * This program is free software; you can redistribute it and/or modify
  102. + * it under the terms of the GNU General Public License version 2 as
  103. + * published by the Free Software Foundation.
  104. + */
  105. +
  106. +#include <linux/kernel.h>
  107. +#include <linux/init.h>
  108. +#include <linux/delay.h>
  109. +#include <linux/spinlock.h>
  110. +#include <linux/workqueue.h>
  111. +#include <linux/module.h>
  112. +#include <linux/platform_device.h>
  113. +#include <linux/spi/spi.h>
  114. +#include <linux/spi/spi_bitbang.h>
  115. +#include <linux/spi/spi_gpio_old.h>
  116. +#include <linux/gpio.h>
  117. +#include <asm/atomic.h>
  118. +
  119. +
  120. +struct spi_gpio {
  121. + struct spi_bitbang bitbang;
  122. + struct spi_gpio_platform_data *info;
  123. + struct platform_device *pdev;
  124. + struct spi_board_info bi;
  125. +};
  126. +
  127. +
  128. +static inline struct spi_gpio *spidev_to_sg(struct spi_device *dev)
  129. +{
  130. + return dev->controller_data;
  131. +}
  132. +
  133. +static inline void setsck(struct spi_device *dev, int val)
  134. +{
  135. + struct spi_gpio *sp = spidev_to_sg(dev);
  136. + gpio_set_value(sp->info->pin_clk, val ? 1 : 0);
  137. +}
  138. +
  139. +static inline void setmosi(struct spi_device *dev, int val)
  140. +{
  141. + struct spi_gpio *sp = spidev_to_sg(dev);
  142. + gpio_set_value(sp->info->pin_mosi, val ? 1 : 0);
  143. +}
  144. +
  145. +static inline u32 getmiso(struct spi_device *dev)
  146. +{
  147. + struct spi_gpio *sp = spidev_to_sg(dev);
  148. + return gpio_get_value(sp->info->pin_miso) ? 1 : 0;
  149. +}
  150. +
  151. +static inline void do_spidelay(struct spi_device *dev, unsigned nsecs)
  152. +{
  153. + struct spi_gpio *sp = spidev_to_sg(dev);
  154. +
  155. + if (!sp->info->no_spi_delay)
  156. + ndelay(nsecs);
  157. +}
  158. +
  159. +#define spidelay(nsecs) do { \
  160. + /* Steal the spi_device pointer from our caller. \
  161. + * The bitbang-API should probably get fixed here... */ \
  162. + do_spidelay(spi, nsecs); \
  163. + } while (0)
  164. +
  165. +#define EXPAND_BITBANG_TXRX
  166. +#include "spi-bitbang-txrx.h"
  167. +
  168. +static u32 spi_gpio_txrx_mode0(struct spi_device *spi,
  169. + unsigned nsecs, u32 word, u8 bits)
  170. +{
  171. + return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
  172. +}
  173. +
  174. +static u32 spi_gpio_txrx_mode1(struct spi_device *spi,
  175. + unsigned nsecs, u32 word, u8 bits)
  176. +{
  177. + return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits);
  178. +}
  179. +
  180. +static u32 spi_gpio_txrx_mode2(struct spi_device *spi,
  181. + unsigned nsecs, u32 word, u8 bits)
  182. +{
  183. + return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits);
  184. +}
  185. +
  186. +static u32 spi_gpio_txrx_mode3(struct spi_device *spi,
  187. + unsigned nsecs, u32 word, u8 bits)
  188. +{
  189. + return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits);
  190. +}
  191. +
  192. +static void spi_gpio_chipselect(struct spi_device *dev, int on)
  193. +{
  194. + struct spi_gpio *sp = spidev_to_sg(dev);
  195. +
  196. + if (sp->info->cs_activelow)
  197. + on = !on;
  198. + gpio_set_value(sp->info->pin_cs, on ? 1 : 0);
  199. +}
  200. +
  201. +static int spi_gpio_probe(struct platform_device *pdev)
  202. +{
  203. + struct spi_master *master;
  204. + struct spi_gpio_platform_data *pdata;
  205. + struct spi_gpio *sp;
  206. + struct spi_device *spidev;
  207. + int err;
  208. +
  209. + pdata = pdev->dev.platform_data;
  210. + if (!pdata)
  211. + return -ENXIO;
  212. +
  213. + err = -ENOMEM;
  214. + master = spi_alloc_master(&pdev->dev, sizeof(struct spi_gpio));
  215. + if (!master)
  216. + goto err_alloc_master;
  217. +
  218. + sp = spi_master_get_devdata(master);
  219. + platform_set_drvdata(pdev, sp);
  220. + sp->info = pdata;
  221. +
  222. + err = gpio_request(pdata->pin_clk, "spi_clock");
  223. + if (err)
  224. + goto err_request_clk;
  225. + err = gpio_request(pdata->pin_mosi, "spi_mosi");
  226. + if (err)
  227. + goto err_request_mosi;
  228. + err = gpio_request(pdata->pin_miso, "spi_miso");
  229. + if (err)
  230. + goto err_request_miso;
  231. + err = gpio_request(pdata->pin_cs, "spi_cs");
  232. + if (err)
  233. + goto err_request_cs;
  234. +
  235. + sp->bitbang.master = spi_master_get(master);
  236. + sp->bitbang.master->bus_num = -1;
  237. + sp->bitbang.master->num_chipselect = 1;
  238. + sp->bitbang.chipselect = spi_gpio_chipselect;
  239. + sp->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_mode0;
  240. + sp->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_mode1;
  241. + sp->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_mode2;
  242. + sp->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_mode3;
  243. +
  244. + gpio_direction_output(pdata->pin_clk, 0);
  245. + gpio_direction_output(pdata->pin_mosi, 0);
  246. + gpio_direction_output(pdata->pin_cs,
  247. + pdata->cs_activelow ? 1 : 0);
  248. + gpio_direction_input(pdata->pin_miso);
  249. +
  250. + err = spi_bitbang_start(&sp->bitbang);
  251. + if (err)
  252. + goto err_no_bitbang;
  253. + err = pdata->boardinfo_setup(&sp->bi, master,
  254. + pdata->boardinfo_setup_data);
  255. + if (err)
  256. + goto err_bi_setup;
  257. + sp->bi.controller_data = sp;
  258. + spidev = spi_new_device(master, &sp->bi);
  259. + if (!spidev)
  260. + goto err_new_dev;
  261. +
  262. + return 0;
  263. +
  264. +err_new_dev:
  265. +err_bi_setup:
  266. + spi_bitbang_stop(&sp->bitbang);
  267. +err_no_bitbang:
  268. + spi_master_put(sp->bitbang.master);
  269. + gpio_free(pdata->pin_cs);
  270. +err_request_cs:
  271. + gpio_free(pdata->pin_miso);
  272. +err_request_miso:
  273. + gpio_free(pdata->pin_mosi);
  274. +err_request_mosi:
  275. + gpio_free(pdata->pin_clk);
  276. +err_request_clk:
  277. + kfree(master);
  278. +
  279. +err_alloc_master:
  280. + return err;
  281. +}
  282. +
  283. +static int spi_gpio_remove(struct platform_device *pdev)
  284. +{
  285. + struct spi_gpio *sp;
  286. + struct spi_gpio_platform_data *pdata;
  287. +
  288. + pdata = pdev->dev.platform_data;
  289. + sp = platform_get_drvdata(pdev);
  290. +
  291. + gpio_free(pdata->pin_clk);
  292. + gpio_free(pdata->pin_mosi);
  293. + gpio_free(pdata->pin_miso);
  294. + gpio_free(pdata->pin_cs);
  295. + spi_bitbang_stop(&sp->bitbang);
  296. + spi_master_put(sp->bitbang.master);
  297. +
  298. + return 0;
  299. +}
  300. +
  301. +static struct platform_driver spi_gpio_driver = {
  302. + .driver = {
  303. + .name = SPI_GPIO_PLATDEV_NAME,
  304. + .owner = THIS_MODULE,
  305. + },
  306. + .probe = spi_gpio_probe,
  307. + .remove = spi_gpio_remove,
  308. +};
  309. +
  310. +int spi_gpio_next_id(void)
  311. +{
  312. + static atomic_t counter = ATOMIC_INIT(-1);
  313. +
  314. + return atomic_inc_return(&counter);
  315. +}
  316. +EXPORT_SYMBOL(spi_gpio_next_id);
  317. +
  318. +static int __init spi_gpio_init(void)
  319. +{
  320. + int err;
  321. +
  322. + err = platform_driver_register(&spi_gpio_driver);
  323. + if (err)
  324. + printk(KERN_ERR "spi-gpio: register failed: %d\n", err);
  325. +
  326. + return err;
  327. +}
  328. +module_init(spi_gpio_init);
  329. +
  330. +static void __exit spi_gpio_exit(void)
  331. +{
  332. + platform_driver_unregister(&spi_gpio_driver);
  333. +}
  334. +module_exit(spi_gpio_exit);
  335. +
  336. +MODULE_AUTHOR("Piot Skamruk <piotr.skamruk at gmail.com>");
  337. +MODULE_AUTHOR("Michael Buesch");
  338. +MODULE_DESCRIPTION("Platform independent GPIO bitbanging SPI driver");
  339. +MODULE_LICENSE("GPL v2");
  340. --- a/drivers/spi/Kconfig
  341. +++ b/drivers/spi/Kconfig
  342. @@ -242,6 +242,15 @@ config SPI_IMG_SPFI
  343. This enables support for the SPFI master controller found on
  344. IMG SoCs.
  345. +config SPI_GPIO_OLD
  346. + tristate "Old GPIO API based bitbanging SPI controller (DEPRECATED)"
  347. + depends on SPI_MASTER && GPIOLIB
  348. + select SPI_BITBANG
  349. + help
  350. + This code is deprecated. Please use the new mainline SPI-GPIO driver.
  351. +
  352. + If unsure, say N.
  353. +
  354. config SPI_IMX
  355. tristate "Freescale i.MX SPI controllers"
  356. depends on ARCH_MXC || COMPILE_TEST
  357. --- a/drivers/spi/Makefile
  358. +++ b/drivers/spi/Makefile
  359. @@ -41,6 +41,7 @@ obj-$(CONFIG_SPI_FSL_LIB) += spi-fsl-li
  360. obj-$(CONFIG_SPI_FSL_ESPI) += spi-fsl-espi.o
  361. obj-$(CONFIG_SPI_FSL_SPI) += spi-fsl-spi.o
  362. obj-$(CONFIG_SPI_GPIO) += spi-gpio.o
  363. +obj-$(CONFIG_SPI_GPIO_OLD) += spi_gpio_old.o
  364. obj-$(CONFIG_SPI_IMG_SPFI) += spi-img-spfi.o
  365. obj-$(CONFIG_SPI_IMX) += spi-imx.o
  366. obj-$(CONFIG_SPI_LM70_LLP) += spi-lm70llp.o