0019-Add-Chris-Boot-s-i2c-and-spi-drivers.patch 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. From 66ea263246ca6dd9f63dce8fb22157fa83693300 Mon Sep 17 00:00:00 2001
  2. From: popcornmix <popcornmix@gmail.com>
  3. Date: Mon, 4 Nov 2013 18:56:10 +0000
  4. Subject: [PATCH 019/114] Add Chris Boot's i2c and spi drivers.
  5. i2c-bcm2708: fixed baudrate
  6. Fixed issue where the wrong CDIV value was set for baudrates below 3815 Hz (for 250MHz bus clock).
  7. In that case the computed CDIV value was more than 0xffff. However the CDIV register width is only 16 bits.
  8. This resulted in incorrect setting of CDIV and higher baudrate than intended.
  9. Example: 3500Hz -> CDIV=0x11704 -> CDIV(16bit)=0x1704 -> 42430Hz
  10. After correction: 3500Hz -> CDIV=0x11704 -> CDIV(16bit)=0xffff -> 3815Hz
  11. The correct baudrate is shown in the log after the cdiv > 0xffff correction.
  12. ---
  13. arch/arm/configs/bcmrpi_defconfig | 7 +
  14. arch/arm/mach-bcm2708/Kconfig | 7 +
  15. arch/arm/mach-bcm2708/bcm2708.c | 104 ++++++-
  16. drivers/i2c/busses/Kconfig | 19 ++
  17. drivers/i2c/busses/Makefile | 1 +
  18. drivers/i2c/busses/i2c-bcm2708.c | 420 +++++++++++++++++++++++++
  19. drivers/spi/Kconfig | 8 +
  20. drivers/spi/Makefile | 1 +
  21. drivers/spi/spi-bcm2708.c | 626 ++++++++++++++++++++++++++++++++++++++
  22. 9 files changed, 1191 insertions(+), 2 deletions(-)
  23. create mode 100644 drivers/i2c/busses/i2c-bcm2708.c
  24. create mode 100644 drivers/spi/spi-bcm2708.c
  25. --- a/arch/arm/configs/bcmrpi_defconfig
  26. +++ b/arch/arm/configs/bcmrpi_defconfig
  27. @@ -195,6 +195,13 @@ CONFIG_SERIAL_AMBA_PL011=y
  28. CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  29. # CONFIG_HW_RANDOM is not set
  30. CONFIG_RAW_DRIVER=y
  31. +CONFIG_I2C=y
  32. +CONFIG_I2C_CHARDEV=m
  33. +CONFIG_I2C_BCM2708=m
  34. +CONFIG_SPI=y
  35. +CONFIG_SPI_BCM2708=m
  36. +CONFIG_SPI_SPIDEV=m
  37. +CONFIG_GPIO_SYSFS=y
  38. # CONFIG_HWMON is not set
  39. CONFIG_WATCHDOG=y
  40. CONFIG_FB=y
  41. --- a/arch/arm/mach-bcm2708/Kconfig
  42. +++ b/arch/arm/mach-bcm2708/Kconfig
  43. @@ -31,4 +31,11 @@ config BCM2708_NOL2CACHE
  44. help
  45. Do not allow ARM to use GPU's L2 cache. Requires disable_l2cache in config.txt.
  46. +config BCM2708_SPIDEV
  47. + bool "Bind spidev to SPI0 master"
  48. + depends on MACH_BCM2708
  49. + depends on SPI
  50. + default y
  51. + help
  52. + Binds spidev driver to the SPI0 master
  53. endmenu
  54. --- a/arch/arm/mach-bcm2708/bcm2708.c
  55. +++ b/arch/arm/mach-bcm2708/bcm2708.c
  56. @@ -31,6 +31,7 @@
  57. #include <linux/cnt32_to_63.h>
  58. #include <linux/io.h>
  59. #include <linux/module.h>
  60. +#include <linux/spi/spi.h>
  61. #include <linux/version.h>
  62. #include <linux/clkdev.h>
  63. @@ -205,7 +206,6 @@ static struct clk osc_clk = {
  64. /* warning - the USB needs a clock > 34MHz */
  65. -#ifdef CONFIG_MMC_BCM2708
  66. static struct clk sdhost_clk = {
  67. #ifdef CONFIG_ARCH_BCM2708_CHIPIT
  68. .rate = 4000000, /* 4MHz */
  69. @@ -213,7 +213,6 @@ static struct clk sdhost_clk = {
  70. .rate = 250000000, /* 250MHz */
  71. #endif
  72. };
  73. -#endif
  74. static struct clk_lookup lookups[] = {
  75. { /* UART0 */
  76. @@ -223,6 +222,15 @@ static struct clk_lookup lookups[] = {
  77. { /* USB */
  78. .dev_id = "bcm2708_usb",
  79. .clk = &osc_clk,
  80. + }, { /* SPI */
  81. + .dev_id = "bcm2708_spi.0",
  82. + .clk = &sdhost_clk,
  83. + }, { /* BSC0 */
  84. + .dev_id = "bcm2708_i2c.0",
  85. + .clk = &sdhost_clk,
  86. + }, { /* BSC1 */
  87. + .dev_id = "bcm2708_i2c.1",
  88. + .clk = &sdhost_clk,
  89. }
  90. };
  91. @@ -455,6 +463,89 @@ static struct platform_device bcm2708_al
  92. },
  93. };
  94. +static struct resource bcm2708_spi_resources[] = {
  95. + {
  96. + .start = SPI0_BASE,
  97. + .end = SPI0_BASE + SZ_256 - 1,
  98. + .flags = IORESOURCE_MEM,
  99. + }, {
  100. + .start = IRQ_SPI,
  101. + .end = IRQ_SPI,
  102. + .flags = IORESOURCE_IRQ,
  103. + }
  104. +};
  105. +
  106. +
  107. +static u64 bcm2708_spi_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  108. +static struct platform_device bcm2708_spi_device = {
  109. + .name = "bcm2708_spi",
  110. + .id = 0,
  111. + .num_resources = ARRAY_SIZE(bcm2708_spi_resources),
  112. + .resource = bcm2708_spi_resources,
  113. + .dev = {
  114. + .dma_mask = &bcm2708_spi_dmamask,
  115. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON)},
  116. +};
  117. +
  118. +#ifdef CONFIG_BCM2708_SPIDEV
  119. +static struct spi_board_info bcm2708_spi_devices[] = {
  120. +#ifdef CONFIG_SPI_SPIDEV
  121. + {
  122. + .modalias = "spidev",
  123. + .max_speed_hz = 500000,
  124. + .bus_num = 0,
  125. + .chip_select = 0,
  126. + .mode = SPI_MODE_0,
  127. + }, {
  128. + .modalias = "spidev",
  129. + .max_speed_hz = 500000,
  130. + .bus_num = 0,
  131. + .chip_select = 1,
  132. + .mode = SPI_MODE_0,
  133. + }
  134. +#endif
  135. +};
  136. +#endif
  137. +
  138. +static struct resource bcm2708_bsc0_resources[] = {
  139. + {
  140. + .start = BSC0_BASE,
  141. + .end = BSC0_BASE + SZ_256 - 1,
  142. + .flags = IORESOURCE_MEM,
  143. + }, {
  144. + .start = INTERRUPT_I2C,
  145. + .end = INTERRUPT_I2C,
  146. + .flags = IORESOURCE_IRQ,
  147. + }
  148. +};
  149. +
  150. +static struct platform_device bcm2708_bsc0_device = {
  151. + .name = "bcm2708_i2c",
  152. + .id = 0,
  153. + .num_resources = ARRAY_SIZE(bcm2708_bsc0_resources),
  154. + .resource = bcm2708_bsc0_resources,
  155. +};
  156. +
  157. +
  158. +static struct resource bcm2708_bsc1_resources[] = {
  159. + {
  160. + .start = BSC1_BASE,
  161. + .end = BSC1_BASE + SZ_256 - 1,
  162. + .flags = IORESOURCE_MEM,
  163. + }, {
  164. + .start = INTERRUPT_I2C,
  165. + .end = INTERRUPT_I2C,
  166. + .flags = IORESOURCE_IRQ,
  167. + }
  168. +};
  169. +
  170. +static struct platform_device bcm2708_bsc1_device = {
  171. + .name = "bcm2708_i2c",
  172. + .id = 1,
  173. + .num_resources = ARRAY_SIZE(bcm2708_bsc1_resources),
  174. + .resource = bcm2708_bsc1_resources,
  175. +};
  176. +
  177. static struct platform_device bcm2835_hwmon_device = {
  178. .name = "bcm2835_hwmon",
  179. };
  180. @@ -571,6 +662,10 @@ void __init bcm2708_init(void)
  181. for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
  182. bcm_register_device(&bcm2708_alsa_devices[i]);
  183. + bcm_register_device(&bcm2708_spi_device);
  184. + bcm_register_device(&bcm2708_bsc0_device);
  185. + bcm_register_device(&bcm2708_bsc1_device);
  186. +
  187. bcm_register_device(&bcm2835_hwmon_device);
  188. bcm_register_device(&bcm2835_thermal_device);
  189. @@ -580,6 +675,11 @@ void __init bcm2708_init(void)
  190. }
  191. system_rev = boardrev;
  192. system_serial_low = serial;
  193. +
  194. +#ifdef CONFIG_BCM2708_SPIDEV
  195. + spi_register_board_info(bcm2708_spi_devices,
  196. + ARRAY_SIZE(bcm2708_spi_devices));
  197. +#endif
  198. }
  199. static void timer_set_mode(enum clock_event_mode mode,
  200. --- a/drivers/i2c/busses/Kconfig
  201. +++ b/drivers/i2c/busses/Kconfig
  202. @@ -372,6 +372,25 @@ config I2C_BCM2835
  203. This support is also available as a module. If so, the module
  204. will be called i2c-bcm2835.
  205. +config I2C_BCM2708
  206. + tristate "BCM2708 BSC"
  207. + depends on MACH_BCM2708
  208. + help
  209. + Enabling this option will add BSC (Broadcom Serial Controller)
  210. + support for the BCM2708. BSC is a Broadcom proprietary bus compatible
  211. + with I2C/TWI/SMBus.
  212. +
  213. +config I2C_BCM2708_BAUDRATE
  214. + prompt "BCM2708 I2C baudrate"
  215. + depends on I2C_BCM2708
  216. + int
  217. + default 100000
  218. + help
  219. + Set the I2C baudrate. This will alter the default value. A
  220. + different baudrate can be set by using a module parameter as well. If
  221. + no parameter is provided when loading, this is the value that will be
  222. + used.
  223. +
  224. config I2C_BCM_KONA
  225. tristate "BCM Kona I2C adapter"
  226. depends on ARCH_BCM_MOBILE
  227. --- a/drivers/i2c/busses/Makefile
  228. +++ b/drivers/i2c/busses/Makefile
  229. @@ -33,6 +33,7 @@ obj-$(CONFIG_I2C_AT91) += i2c-at91.o
  230. obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o
  231. obj-$(CONFIG_I2C_AXXIA) += i2c-axxia.o
  232. obj-$(CONFIG_I2C_BCM2835) += i2c-bcm2835.o
  233. +obj-$(CONFIG_I2C_BCM2708) += i2c-bcm2708.o
  234. obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
  235. obj-$(CONFIG_I2C_CADENCE) += i2c-cadence.o
  236. obj-$(CONFIG_I2C_CBUS_GPIO) += i2c-cbus-gpio.o
  237. --- /dev/null
  238. +++ b/drivers/i2c/busses/i2c-bcm2708.c
  239. @@ -0,0 +1,420 @@
  240. +/*
  241. + * Driver for Broadcom BCM2708 BSC Controllers
  242. + *
  243. + * Copyright (C) 2012 Chris Boot & Frank Buss
  244. + *
  245. + * This driver is inspired by:
  246. + * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
  247. + *
  248. + * This program is free software; you can redistribute it and/or modify
  249. + * it under the terms of the GNU General Public License as published by
  250. + * the Free Software Foundation; either version 2 of the License, or
  251. + * (at your option) any later version.
  252. + *
  253. + * This program is distributed in the hope that it will be useful,
  254. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  255. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  256. + * GNU General Public License for more details.
  257. + *
  258. + * You should have received a copy of the GNU General Public License
  259. + * along with this program; if not, write to the Free Software
  260. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  261. + */
  262. +
  263. +#include <linux/kernel.h>
  264. +#include <linux/module.h>
  265. +#include <linux/spinlock.h>
  266. +#include <linux/clk.h>
  267. +#include <linux/err.h>
  268. +#include <linux/platform_device.h>
  269. +#include <linux/io.h>
  270. +#include <linux/slab.h>
  271. +#include <linux/i2c.h>
  272. +#include <linux/interrupt.h>
  273. +#include <linux/sched.h>
  274. +#include <linux/wait.h>
  275. +
  276. +/* BSC register offsets */
  277. +#define BSC_C 0x00
  278. +#define BSC_S 0x04
  279. +#define BSC_DLEN 0x08
  280. +#define BSC_A 0x0c
  281. +#define BSC_FIFO 0x10
  282. +#define BSC_DIV 0x14
  283. +#define BSC_DEL 0x18
  284. +#define BSC_CLKT 0x1c
  285. +
  286. +/* Bitfields in BSC_C */
  287. +#define BSC_C_I2CEN 0x00008000
  288. +#define BSC_C_INTR 0x00000400
  289. +#define BSC_C_INTT 0x00000200
  290. +#define BSC_C_INTD 0x00000100
  291. +#define BSC_C_ST 0x00000080
  292. +#define BSC_C_CLEAR_1 0x00000020
  293. +#define BSC_C_CLEAR_2 0x00000010
  294. +#define BSC_C_READ 0x00000001
  295. +
  296. +/* Bitfields in BSC_S */
  297. +#define BSC_S_CLKT 0x00000200
  298. +#define BSC_S_ERR 0x00000100
  299. +#define BSC_S_RXF 0x00000080
  300. +#define BSC_S_TXE 0x00000040
  301. +#define BSC_S_RXD 0x00000020
  302. +#define BSC_S_TXD 0x00000010
  303. +#define BSC_S_RXR 0x00000008
  304. +#define BSC_S_TXW 0x00000004
  305. +#define BSC_S_DONE 0x00000002
  306. +#define BSC_S_TA 0x00000001
  307. +
  308. +#define I2C_TIMEOUT_MS 150
  309. +
  310. +#define DRV_NAME "bcm2708_i2c"
  311. +
  312. +static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
  313. +module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  314. +MODULE_PARM_DESC(baudrate, "The I2C baudrate");
  315. +
  316. +
  317. +struct bcm2708_i2c {
  318. + struct i2c_adapter adapter;
  319. +
  320. + spinlock_t lock;
  321. + void __iomem *base;
  322. + int irq;
  323. + struct clk *clk;
  324. +
  325. + struct completion done;
  326. +
  327. + struct i2c_msg *msg;
  328. + int pos;
  329. + int nmsgs;
  330. + bool error;
  331. +};
  332. +
  333. +/*
  334. + * This function sets the ALT mode on the I2C pins so that we can use them with
  335. + * the BSC hardware.
  336. + *
  337. + * FIXME: This is a hack. Use pinmux / pinctrl.
  338. + */
  339. +static void bcm2708_i2c_init_pinmode(int id)
  340. +{
  341. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  342. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  343. +
  344. + int pin;
  345. + u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
  346. +
  347. + BUG_ON(id != 0 && id != 1);
  348. + /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
  349. + for (pin = id*2+0; pin <= id*2+1; pin++) {
  350. +printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
  351. + INP_GPIO(pin); /* set mode to GPIO input first */
  352. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  353. + }
  354. +
  355. + iounmap(gpio);
  356. +
  357. +#undef INP_GPIO
  358. +#undef SET_GPIO_ALT
  359. +}
  360. +
  361. +static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
  362. +{
  363. + return readl(bi->base + reg);
  364. +}
  365. +
  366. +static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
  367. +{
  368. + writel(val, bi->base + reg);
  369. +}
  370. +
  371. +static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
  372. +{
  373. + bcm2708_wr(bi, BSC_C, 0);
  374. + bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
  375. +}
  376. +
  377. +static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
  378. +{
  379. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
  380. + bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
  381. +}
  382. +
  383. +static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
  384. +{
  385. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
  386. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  387. +}
  388. +
  389. +static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
  390. +{
  391. + unsigned long bus_hz;
  392. + u32 cdiv;
  393. + u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
  394. +
  395. + bus_hz = clk_get_rate(bi->clk);
  396. + cdiv = bus_hz / baudrate;
  397. + if (cdiv > 0xffff)
  398. + cdiv = 0xffff;
  399. +
  400. + if (bi->msg->flags & I2C_M_RD)
  401. + c |= BSC_C_INTR | BSC_C_READ;
  402. + else
  403. + c |= BSC_C_INTT;
  404. +
  405. + bcm2708_wr(bi, BSC_DIV, cdiv);
  406. + bcm2708_wr(bi, BSC_A, bi->msg->addr);
  407. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  408. + bcm2708_wr(bi, BSC_C, c);
  409. +}
  410. +
  411. +static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
  412. +{
  413. + struct bcm2708_i2c *bi = dev_id;
  414. + bool handled = true;
  415. + u32 s;
  416. +
  417. + spin_lock(&bi->lock);
  418. +
  419. + /* we may see camera interrupts on the "other" I2C channel
  420. + Just return if we've not sent anything */
  421. + if (!bi->nmsgs || !bi->msg )
  422. + goto early_exit;
  423. +
  424. + s = bcm2708_rd(bi, BSC_S);
  425. +
  426. + if (s & (BSC_S_CLKT | BSC_S_ERR)) {
  427. + bcm2708_bsc_reset(bi);
  428. + bi->error = true;
  429. +
  430. + /* wake up our bh */
  431. + complete(&bi->done);
  432. + } else if (s & BSC_S_DONE) {
  433. + bi->nmsgs--;
  434. +
  435. + if (bi->msg->flags & I2C_M_RD)
  436. + bcm2708_bsc_fifo_drain(bi);
  437. +
  438. + bcm2708_bsc_reset(bi);
  439. +
  440. + if (bi->nmsgs) {
  441. + /* advance to next message */
  442. + bi->msg++;
  443. + bi->pos = 0;
  444. + bcm2708_bsc_setup(bi);
  445. + } else {
  446. + /* wake up our bh */
  447. + complete(&bi->done);
  448. + }
  449. + } else if (s & BSC_S_TXW) {
  450. + bcm2708_bsc_fifo_fill(bi);
  451. + } else if (s & BSC_S_RXR) {
  452. + bcm2708_bsc_fifo_drain(bi);
  453. + } else {
  454. + handled = false;
  455. + }
  456. +
  457. +early_exit:
  458. + spin_unlock(&bi->lock);
  459. +
  460. + return handled ? IRQ_HANDLED : IRQ_NONE;
  461. +}
  462. +
  463. +static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
  464. + struct i2c_msg *msgs, int num)
  465. +{
  466. + struct bcm2708_i2c *bi = adap->algo_data;
  467. + unsigned long flags;
  468. + int ret;
  469. +
  470. + spin_lock_irqsave(&bi->lock, flags);
  471. +
  472. + reinit_completion(&bi->done);
  473. + bi->msg = msgs;
  474. + bi->pos = 0;
  475. + bi->nmsgs = num;
  476. + bi->error = false;
  477. +
  478. + bcm2708_bsc_setup(bi);
  479. +
  480. + /* unlockig _after_ the setup to avoid races with the interrupt routine */
  481. + spin_unlock_irqrestore(&bi->lock, flags);
  482. +
  483. + ret = wait_for_completion_timeout(&bi->done,
  484. + msecs_to_jiffies(I2C_TIMEOUT_MS));
  485. + if (ret == 0) {
  486. + dev_err(&adap->dev, "transfer timed out\n");
  487. + spin_lock_irqsave(&bi->lock, flags);
  488. + bcm2708_bsc_reset(bi);
  489. + spin_unlock_irqrestore(&bi->lock, flags);
  490. + return -ETIMEDOUT;
  491. + }
  492. +
  493. + return bi->error ? -EIO : num;
  494. +}
  495. +
  496. +static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
  497. +{
  498. + return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
  499. +}
  500. +
  501. +static struct i2c_algorithm bcm2708_i2c_algorithm = {
  502. + .master_xfer = bcm2708_i2c_master_xfer,
  503. + .functionality = bcm2708_i2c_functionality,
  504. +};
  505. +
  506. +static int bcm2708_i2c_probe(struct platform_device *pdev)
  507. +{
  508. + struct resource *regs;
  509. + int irq, err = -ENOMEM;
  510. + struct clk *clk;
  511. + struct bcm2708_i2c *bi;
  512. + struct i2c_adapter *adap;
  513. + unsigned long bus_hz;
  514. + u32 cdiv;
  515. +
  516. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  517. + if (!regs) {
  518. + dev_err(&pdev->dev, "could not get IO memory\n");
  519. + return -ENXIO;
  520. + }
  521. +
  522. + irq = platform_get_irq(pdev, 0);
  523. + if (irq < 0) {
  524. + dev_err(&pdev->dev, "could not get IRQ\n");
  525. + return irq;
  526. + }
  527. +
  528. + clk = clk_get(&pdev->dev, NULL);
  529. + if (IS_ERR(clk)) {
  530. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  531. + return PTR_ERR(clk);
  532. + }
  533. +
  534. + bcm2708_i2c_init_pinmode(pdev->id);
  535. +
  536. + bi = kzalloc(sizeof(*bi), GFP_KERNEL);
  537. + if (!bi)
  538. + goto out_clk_put;
  539. +
  540. + platform_set_drvdata(pdev, bi);
  541. +
  542. + adap = &bi->adapter;
  543. + adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
  544. + adap->algo = &bcm2708_i2c_algorithm;
  545. + adap->algo_data = bi;
  546. + adap->dev.parent = &pdev->dev;
  547. + adap->nr = pdev->id;
  548. + strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  549. +
  550. + switch (pdev->id) {
  551. + case 0:
  552. + adap->class = I2C_CLASS_HWMON;
  553. + break;
  554. + case 1:
  555. + adap->class = I2C_CLASS_DDC;
  556. + break;
  557. + default:
  558. + dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
  559. + err = -ENXIO;
  560. + goto out_free_bi;
  561. + }
  562. +
  563. + spin_lock_init(&bi->lock);
  564. + init_completion(&bi->done);
  565. +
  566. + bi->base = ioremap(regs->start, resource_size(regs));
  567. + if (!bi->base) {
  568. + dev_err(&pdev->dev, "could not remap memory\n");
  569. + goto out_free_bi;
  570. + }
  571. +
  572. + bi->irq = irq;
  573. + bi->clk = clk;
  574. +
  575. + err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
  576. + dev_name(&pdev->dev), bi);
  577. + if (err) {
  578. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  579. + goto out_iounmap;
  580. + }
  581. +
  582. + bcm2708_bsc_reset(bi);
  583. +
  584. + err = i2c_add_numbered_adapter(adap);
  585. + if (err < 0) {
  586. + dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
  587. + goto out_free_irq;
  588. + }
  589. +
  590. + bus_hz = clk_get_rate(bi->clk);
  591. + cdiv = bus_hz / baudrate;
  592. + if (cdiv > 0xffff) {
  593. + cdiv = 0xffff;
  594. + baudrate = bus_hz / cdiv;
  595. + }
  596. +
  597. + dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %d)\n",
  598. + pdev->id, (unsigned long)regs->start, irq, baudrate);
  599. +
  600. + return 0;
  601. +
  602. +out_free_irq:
  603. + free_irq(bi->irq, bi);
  604. +out_iounmap:
  605. + iounmap(bi->base);
  606. +out_free_bi:
  607. + kfree(bi);
  608. +out_clk_put:
  609. + clk_put(clk);
  610. + return err;
  611. +}
  612. +
  613. +static int bcm2708_i2c_remove(struct platform_device *pdev)
  614. +{
  615. + struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
  616. +
  617. + platform_set_drvdata(pdev, NULL);
  618. +
  619. + i2c_del_adapter(&bi->adapter);
  620. + free_irq(bi->irq, bi);
  621. + iounmap(bi->base);
  622. + clk_disable(bi->clk);
  623. + clk_put(bi->clk);
  624. + kfree(bi);
  625. +
  626. + return 0;
  627. +}
  628. +
  629. +static struct platform_driver bcm2708_i2c_driver = {
  630. + .driver = {
  631. + .name = DRV_NAME,
  632. + .owner = THIS_MODULE,
  633. + },
  634. + .probe = bcm2708_i2c_probe,
  635. + .remove = bcm2708_i2c_remove,
  636. +};
  637. +
  638. +// module_platform_driver(bcm2708_i2c_driver);
  639. +
  640. +
  641. +static int __init bcm2708_i2c_init(void)
  642. +{
  643. + return platform_driver_register(&bcm2708_i2c_driver);
  644. +}
  645. +
  646. +static void __exit bcm2708_i2c_exit(void)
  647. +{
  648. + platform_driver_unregister(&bcm2708_i2c_driver);
  649. +}
  650. +
  651. +module_init(bcm2708_i2c_init);
  652. +module_exit(bcm2708_i2c_exit);
  653. +
  654. +
  655. +
  656. +MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
  657. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  658. +MODULE_LICENSE("GPL v2");
  659. +MODULE_ALIAS("platform:" DRV_NAME);
  660. --- a/drivers/spi/Kconfig
  661. +++ b/drivers/spi/Kconfig
  662. @@ -86,6 +86,14 @@ config SPI_BCM2835
  663. is for the regular SPI controller. Slave mode operation is not also
  664. not supported.
  665. +config SPI_BCM2708
  666. + tristate "BCM2708 SPI controller driver (SPI0)"
  667. + depends on MACH_BCM2708
  668. + help
  669. + This selects a driver for the Broadcom BCM2708 SPI master (SPI0). This
  670. + driver is not compatible with the "Universal SPI Master" or the SPI slave
  671. + device.
  672. +
  673. config SPI_BFIN5XX
  674. tristate "SPI controller driver for ADI Blackfin5xx"
  675. depends on BLACKFIN && !BF60x
  676. --- a/drivers/spi/Makefile
  677. +++ b/drivers/spi/Makefile
  678. @@ -20,6 +20,7 @@ obj-$(CONFIG_SPI_BCM63XX) += spi-bcm63x
  679. obj-$(CONFIG_SPI_BCM63XX_HSSPI) += spi-bcm63xx-hsspi.o
  680. obj-$(CONFIG_SPI_BFIN5XX) += spi-bfin5xx.o
  681. obj-$(CONFIG_SPI_ADI_V3) += spi-adi-v3.o
  682. +obj-$(CONFIG_SPI_BCM2708) += spi-bcm2708.o
  683. obj-$(CONFIG_SPI_BFIN_SPORT) += spi-bfin-sport.o
  684. obj-$(CONFIG_SPI_BITBANG) += spi-bitbang.o
  685. obj-$(CONFIG_SPI_BUTTERFLY) += spi-butterfly.o
  686. --- /dev/null
  687. +++ b/drivers/spi/spi-bcm2708.c
  688. @@ -0,0 +1,626 @@
  689. +/*
  690. + * Driver for Broadcom BCM2708 SPI Controllers
  691. + *
  692. + * Copyright (C) 2012 Chris Boot
  693. + *
  694. + * This driver is inspired by:
  695. + * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  696. + * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
  697. + *
  698. + * This program is free software; you can redistribute it and/or modify
  699. + * it under the terms of the GNU General Public License as published by
  700. + * the Free Software Foundation; either version 2 of the License, or
  701. + * (at your option) any later version.
  702. + *
  703. + * This program is distributed in the hope that it will be useful,
  704. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  705. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  706. + * GNU General Public License for more details.
  707. + *
  708. + * You should have received a copy of the GNU General Public License
  709. + * along with this program; if not, write to the Free Software
  710. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  711. + */
  712. +
  713. +#include <linux/kernel.h>
  714. +#include <linux/module.h>
  715. +#include <linux/spinlock.h>
  716. +#include <linux/clk.h>
  717. +#include <linux/err.h>
  718. +#include <linux/platform_device.h>
  719. +#include <linux/io.h>
  720. +#include <linux/spi/spi.h>
  721. +#include <linux/interrupt.h>
  722. +#include <linux/delay.h>
  723. +#include <linux/log2.h>
  724. +#include <linux/sched.h>
  725. +#include <linux/wait.h>
  726. +
  727. +/* SPI register offsets */
  728. +#define SPI_CS 0x00
  729. +#define SPI_FIFO 0x04
  730. +#define SPI_CLK 0x08
  731. +#define SPI_DLEN 0x0c
  732. +#define SPI_LTOH 0x10
  733. +#define SPI_DC 0x14
  734. +
  735. +/* Bitfields in CS */
  736. +#define SPI_CS_LEN_LONG 0x02000000
  737. +#define SPI_CS_DMA_LEN 0x01000000
  738. +#define SPI_CS_CSPOL2 0x00800000
  739. +#define SPI_CS_CSPOL1 0x00400000
  740. +#define SPI_CS_CSPOL0 0x00200000
  741. +#define SPI_CS_RXF 0x00100000
  742. +#define SPI_CS_RXR 0x00080000
  743. +#define SPI_CS_TXD 0x00040000
  744. +#define SPI_CS_RXD 0x00020000
  745. +#define SPI_CS_DONE 0x00010000
  746. +#define SPI_CS_LEN 0x00002000
  747. +#define SPI_CS_REN 0x00001000
  748. +#define SPI_CS_ADCS 0x00000800
  749. +#define SPI_CS_INTR 0x00000400
  750. +#define SPI_CS_INTD 0x00000200
  751. +#define SPI_CS_DMAEN 0x00000100
  752. +#define SPI_CS_TA 0x00000080
  753. +#define SPI_CS_CSPOL 0x00000040
  754. +#define SPI_CS_CLEAR_RX 0x00000020
  755. +#define SPI_CS_CLEAR_TX 0x00000010
  756. +#define SPI_CS_CPOL 0x00000008
  757. +#define SPI_CS_CPHA 0x00000004
  758. +#define SPI_CS_CS_10 0x00000002
  759. +#define SPI_CS_CS_01 0x00000001
  760. +
  761. +#define SPI_TIMEOUT_MS 150
  762. +
  763. +#define DRV_NAME "bcm2708_spi"
  764. +
  765. +struct bcm2708_spi {
  766. + spinlock_t lock;
  767. + void __iomem *base;
  768. + int irq;
  769. + struct clk *clk;
  770. + bool stopping;
  771. +
  772. + struct list_head queue;
  773. + struct workqueue_struct *workq;
  774. + struct work_struct work;
  775. + struct completion done;
  776. +
  777. + const u8 *tx_buf;
  778. + u8 *rx_buf;
  779. + int len;
  780. +};
  781. +
  782. +struct bcm2708_spi_state {
  783. + u32 cs;
  784. + u16 cdiv;
  785. +};
  786. +
  787. +/*
  788. + * This function sets the ALT mode on the SPI pins so that we can use them with
  789. + * the SPI hardware.
  790. + *
  791. + * FIXME: This is a hack. Use pinmux / pinctrl.
  792. + */
  793. +static void bcm2708_init_pinmode(void)
  794. +{
  795. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  796. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  797. +
  798. + int pin;
  799. + u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
  800. +
  801. + /* SPI is on GPIO 7..11 */
  802. + for (pin = 7; pin <= 11; pin++) {
  803. + INP_GPIO(pin); /* set mode to GPIO input first */
  804. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  805. + }
  806. +
  807. + iounmap(gpio);
  808. +
  809. +#undef INP_GPIO
  810. +#undef SET_GPIO_ALT
  811. +}
  812. +
  813. +static inline u32 bcm2708_rd(struct bcm2708_spi *bs, unsigned reg)
  814. +{
  815. + return readl(bs->base + reg);
  816. +}
  817. +
  818. +static inline void bcm2708_wr(struct bcm2708_spi *bs, unsigned reg, u32 val)
  819. +{
  820. + writel(val, bs->base + reg);
  821. +}
  822. +
  823. +static inline void bcm2708_rd_fifo(struct bcm2708_spi *bs, int len)
  824. +{
  825. + u8 byte;
  826. +
  827. + while (len--) {
  828. + byte = bcm2708_rd(bs, SPI_FIFO);
  829. + if (bs->rx_buf)
  830. + *bs->rx_buf++ = byte;
  831. + }
  832. +}
  833. +
  834. +static inline void bcm2708_wr_fifo(struct bcm2708_spi *bs, int len)
  835. +{
  836. + u8 byte;
  837. + u16 val;
  838. +
  839. + if (len > bs->len)
  840. + len = bs->len;
  841. +
  842. + if (unlikely(bcm2708_rd(bs, SPI_CS) & SPI_CS_LEN)) {
  843. + /* LoSSI mode */
  844. + if (unlikely(len % 2)) {
  845. + printk(KERN_ERR"bcm2708_wr_fifo: length must be even, skipping.\n");
  846. + bs->len = 0;
  847. + return;
  848. + }
  849. + while (len) {
  850. + if (bs->tx_buf) {
  851. + val = *(const u16 *)bs->tx_buf;
  852. + bs->tx_buf += 2;
  853. + } else
  854. + val = 0;
  855. + bcm2708_wr(bs, SPI_FIFO, val);
  856. + bs->len -= 2;
  857. + len -= 2;
  858. + }
  859. + return;
  860. + }
  861. +
  862. + while (len--) {
  863. + byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  864. + bcm2708_wr(bs, SPI_FIFO, byte);
  865. + bs->len--;
  866. + }
  867. +}
  868. +
  869. +static irqreturn_t bcm2708_spi_interrupt(int irq, void *dev_id)
  870. +{
  871. + struct spi_master *master = dev_id;
  872. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  873. + u32 cs;
  874. +
  875. + spin_lock(&bs->lock);
  876. +
  877. + cs = bcm2708_rd(bs, SPI_CS);
  878. +
  879. + if (cs & SPI_CS_DONE) {
  880. + if (bs->len) { /* first interrupt in a transfer */
  881. + /* fill the TX fifo with up to 16 bytes */
  882. + bcm2708_wr_fifo(bs, 16);
  883. + } else { /* transfer complete */
  884. + /* disable interrupts */
  885. + cs &= ~(SPI_CS_INTR | SPI_CS_INTD);
  886. + bcm2708_wr(bs, SPI_CS, cs);
  887. +
  888. + /* drain RX FIFO */
  889. + while (cs & SPI_CS_RXD) {
  890. + bcm2708_rd_fifo(bs, 1);
  891. + cs = bcm2708_rd(bs, SPI_CS);
  892. + }
  893. +
  894. + /* wake up our bh */
  895. + complete(&bs->done);
  896. + }
  897. + } else if (cs & SPI_CS_RXR) {
  898. + /* read 12 bytes of data */
  899. + bcm2708_rd_fifo(bs, 12);
  900. +
  901. + /* write up to 12 bytes */
  902. + bcm2708_wr_fifo(bs, 12);
  903. + }
  904. +
  905. + spin_unlock(&bs->lock);
  906. +
  907. + return IRQ_HANDLED;
  908. +}
  909. +
  910. +static int bcm2708_setup_state(struct spi_master *master,
  911. + struct device *dev, struct bcm2708_spi_state *state,
  912. + u32 hz, u8 csel, u8 mode, u8 bpw)
  913. +{
  914. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  915. + int cdiv;
  916. + unsigned long bus_hz;
  917. + u32 cs = 0;
  918. +
  919. + bus_hz = clk_get_rate(bs->clk);
  920. +
  921. + if (hz >= bus_hz) {
  922. + cdiv = 2; /* bus_hz / 2 is as fast as we can go */
  923. + } else if (hz) {
  924. + cdiv = DIV_ROUND_UP(bus_hz, hz);
  925. +
  926. + /* CDIV must be a power of 2, so round up */
  927. + cdiv = roundup_pow_of_two(cdiv);
  928. +
  929. + if (cdiv > 65536) {
  930. + dev_dbg(dev,
  931. + "setup: %d Hz too slow, cdiv %u; min %ld Hz\n",
  932. + hz, cdiv, bus_hz / 65536);
  933. + return -EINVAL;
  934. + } else if (cdiv == 65536) {
  935. + cdiv = 0;
  936. + } else if (cdiv == 1) {
  937. + cdiv = 2; /* 1 gets rounded down to 0; == 65536 */
  938. + }
  939. + } else {
  940. + cdiv = 0;
  941. + }
  942. +
  943. + switch (bpw) {
  944. + case 8:
  945. + break;
  946. + case 9:
  947. + /* Reading in LoSSI mode is a special case. See 'BCM2835 ARM Peripherals' datasheet */
  948. + cs |= SPI_CS_LEN;
  949. + break;
  950. + default:
  951. + dev_dbg(dev, "setup: invalid bits_per_word %u (must be 8 or 9)\n",
  952. + bpw);
  953. + return -EINVAL;
  954. + }
  955. +
  956. + if (mode & SPI_CPOL)
  957. + cs |= SPI_CS_CPOL;
  958. + if (mode & SPI_CPHA)
  959. + cs |= SPI_CS_CPHA;
  960. +
  961. + if (!(mode & SPI_NO_CS)) {
  962. + if (mode & SPI_CS_HIGH) {
  963. + cs |= SPI_CS_CSPOL;
  964. + cs |= SPI_CS_CSPOL0 << csel;
  965. + }
  966. +
  967. + cs |= csel;
  968. + } else {
  969. + cs |= SPI_CS_CS_10 | SPI_CS_CS_01;
  970. + }
  971. +
  972. + if (state) {
  973. + state->cs = cs;
  974. + state->cdiv = cdiv;
  975. + dev_dbg(dev, "setup: want %d Hz; "
  976. + "bus_hz=%lu / cdiv=%u == %lu Hz; "
  977. + "mode %u: cs 0x%08X\n",
  978. + hz, bus_hz, cdiv, bus_hz/cdiv, mode, cs);
  979. + }
  980. +
  981. + return 0;
  982. +}
  983. +
  984. +static int bcm2708_process_transfer(struct bcm2708_spi *bs,
  985. + struct spi_message *msg, struct spi_transfer *xfer)
  986. +{
  987. + struct spi_device *spi = msg->spi;
  988. + struct bcm2708_spi_state state, *stp;
  989. + int ret;
  990. + u32 cs;
  991. +
  992. + if (bs->stopping)
  993. + return -ESHUTDOWN;
  994. +
  995. + if (xfer->bits_per_word || xfer->speed_hz) {
  996. + ret = bcm2708_setup_state(spi->master, &spi->dev, &state,
  997. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  998. + spi->chip_select, spi->mode,
  999. + xfer->bits_per_word ? xfer->bits_per_word :
  1000. + spi->bits_per_word);
  1001. + if (ret)
  1002. + return ret;
  1003. +
  1004. + stp = &state;
  1005. + } else {
  1006. + stp = spi->controller_state;
  1007. + }
  1008. +
  1009. + reinit_completion(&bs->done);
  1010. + bs->tx_buf = xfer->tx_buf;
  1011. + bs->rx_buf = xfer->rx_buf;
  1012. + bs->len = xfer->len;
  1013. +
  1014. + cs = stp->cs | SPI_CS_INTR | SPI_CS_INTD | SPI_CS_TA;
  1015. +
  1016. + bcm2708_wr(bs, SPI_CLK, stp->cdiv);
  1017. + bcm2708_wr(bs, SPI_CS, cs);
  1018. +
  1019. + ret = wait_for_completion_timeout(&bs->done,
  1020. + msecs_to_jiffies(SPI_TIMEOUT_MS));
  1021. + if (ret == 0) {
  1022. + dev_err(&spi->dev, "transfer timed out\n");
  1023. + return -ETIMEDOUT;
  1024. + }
  1025. +
  1026. + if (xfer->delay_usecs)
  1027. + udelay(xfer->delay_usecs);
  1028. +
  1029. + if (list_is_last(&xfer->transfer_list, &msg->transfers) ||
  1030. + xfer->cs_change) {
  1031. + /* clear TA and interrupt flags */
  1032. + bcm2708_wr(bs, SPI_CS, stp->cs);
  1033. + }
  1034. +
  1035. + msg->actual_length += (xfer->len - bs->len);
  1036. +
  1037. + return 0;
  1038. +}
  1039. +
  1040. +static void bcm2708_work(struct work_struct *work)
  1041. +{
  1042. + struct bcm2708_spi *bs = container_of(work, struct bcm2708_spi, work);
  1043. + unsigned long flags;
  1044. + struct spi_message *msg;
  1045. + struct spi_transfer *xfer;
  1046. + int status = 0;
  1047. +
  1048. + spin_lock_irqsave(&bs->lock, flags);
  1049. + while (!list_empty(&bs->queue)) {
  1050. + msg = list_first_entry(&bs->queue, struct spi_message, queue);
  1051. + list_del_init(&msg->queue);
  1052. + spin_unlock_irqrestore(&bs->lock, flags);
  1053. +
  1054. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1055. + status = bcm2708_process_transfer(bs, msg, xfer);
  1056. + if (status)
  1057. + break;
  1058. + }
  1059. +
  1060. + msg->status = status;
  1061. + msg->complete(msg->context);
  1062. +
  1063. + spin_lock_irqsave(&bs->lock, flags);
  1064. + }
  1065. + spin_unlock_irqrestore(&bs->lock, flags);
  1066. +}
  1067. +
  1068. +static int bcm2708_spi_setup(struct spi_device *spi)
  1069. +{
  1070. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  1071. + struct bcm2708_spi_state *state;
  1072. + int ret;
  1073. +
  1074. + if (bs->stopping)
  1075. + return -ESHUTDOWN;
  1076. +
  1077. + if (!(spi->mode & SPI_NO_CS) &&
  1078. + (spi->chip_select > spi->master->num_chipselect)) {
  1079. + dev_dbg(&spi->dev,
  1080. + "setup: invalid chipselect %u (%u defined)\n",
  1081. + spi->chip_select, spi->master->num_chipselect);
  1082. + return -EINVAL;
  1083. + }
  1084. +
  1085. + state = spi->controller_state;
  1086. + if (!state) {
  1087. + state = kzalloc(sizeof(*state), GFP_KERNEL);
  1088. + if (!state)
  1089. + return -ENOMEM;
  1090. +
  1091. + spi->controller_state = state;
  1092. + }
  1093. +
  1094. + ret = bcm2708_setup_state(spi->master, &spi->dev, state,
  1095. + spi->max_speed_hz, spi->chip_select, spi->mode,
  1096. + spi->bits_per_word);
  1097. + if (ret < 0) {
  1098. + kfree(state);
  1099. + spi->controller_state = NULL;
  1100. + return ret;
  1101. + }
  1102. +
  1103. + dev_dbg(&spi->dev,
  1104. + "setup: cd %d: %d Hz, bpw %u, mode 0x%x -> CS=%08x CDIV=%04x\n",
  1105. + spi->chip_select, spi->max_speed_hz, spi->bits_per_word,
  1106. + spi->mode, state->cs, state->cdiv);
  1107. +
  1108. + return 0;
  1109. +}
  1110. +
  1111. +static int bcm2708_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  1112. +{
  1113. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  1114. + struct spi_transfer *xfer;
  1115. + int ret;
  1116. + unsigned long flags;
  1117. +
  1118. + if (unlikely(list_empty(&msg->transfers)))
  1119. + return -EINVAL;
  1120. +
  1121. + if (bs->stopping)
  1122. + return -ESHUTDOWN;
  1123. +
  1124. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  1125. + if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  1126. + dev_dbg(&spi->dev, "missing rx or tx buf\n");
  1127. + return -EINVAL;
  1128. + }
  1129. +
  1130. + if (!xfer->bits_per_word || xfer->speed_hz)
  1131. + continue;
  1132. +
  1133. + ret = bcm2708_setup_state(spi->master, &spi->dev, NULL,
  1134. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  1135. + spi->chip_select, spi->mode,
  1136. + xfer->bits_per_word ? xfer->bits_per_word :
  1137. + spi->bits_per_word);
  1138. + if (ret)
  1139. + return ret;
  1140. + }
  1141. +
  1142. + msg->status = -EINPROGRESS;
  1143. + msg->actual_length = 0;
  1144. +
  1145. + spin_lock_irqsave(&bs->lock, flags);
  1146. + list_add_tail(&msg->queue, &bs->queue);
  1147. + queue_work(bs->workq, &bs->work);
  1148. + spin_unlock_irqrestore(&bs->lock, flags);
  1149. +
  1150. + return 0;
  1151. +}
  1152. +
  1153. +static void bcm2708_spi_cleanup(struct spi_device *spi)
  1154. +{
  1155. + if (spi->controller_state) {
  1156. + kfree(spi->controller_state);
  1157. + spi->controller_state = NULL;
  1158. + }
  1159. +}
  1160. +
  1161. +static int bcm2708_spi_probe(struct platform_device *pdev)
  1162. +{
  1163. + struct resource *regs;
  1164. + int irq, err = -ENOMEM;
  1165. + struct clk *clk;
  1166. + struct spi_master *master;
  1167. + struct bcm2708_spi *bs;
  1168. +
  1169. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1170. + if (!regs) {
  1171. + dev_err(&pdev->dev, "could not get IO memory\n");
  1172. + return -ENXIO;
  1173. + }
  1174. +
  1175. + irq = platform_get_irq(pdev, 0);
  1176. + if (irq < 0) {
  1177. + dev_err(&pdev->dev, "could not get IRQ\n");
  1178. + return irq;
  1179. + }
  1180. +
  1181. + clk = clk_get(&pdev->dev, NULL);
  1182. + if (IS_ERR(clk)) {
  1183. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  1184. + return PTR_ERR(clk);
  1185. + }
  1186. +
  1187. + bcm2708_init_pinmode();
  1188. +
  1189. + master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  1190. + if (!master) {
  1191. + dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  1192. + goto out_clk_put;
  1193. + }
  1194. +
  1195. + /* the spi->mode bits understood by this driver: */
  1196. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
  1197. +
  1198. + master->bus_num = pdev->id;
  1199. + master->num_chipselect = 3;
  1200. + master->setup = bcm2708_spi_setup;
  1201. + master->transfer = bcm2708_spi_transfer;
  1202. + master->cleanup = bcm2708_spi_cleanup;
  1203. + platform_set_drvdata(pdev, master);
  1204. +
  1205. + bs = spi_master_get_devdata(master);
  1206. +
  1207. + spin_lock_init(&bs->lock);
  1208. + INIT_LIST_HEAD(&bs->queue);
  1209. + init_completion(&bs->done);
  1210. + INIT_WORK(&bs->work, bcm2708_work);
  1211. +
  1212. + bs->base = ioremap(regs->start, resource_size(regs));
  1213. + if (!bs->base) {
  1214. + dev_err(&pdev->dev, "could not remap memory\n");
  1215. + goto out_master_put;
  1216. + }
  1217. +
  1218. + bs->workq = create_singlethread_workqueue(dev_name(&pdev->dev));
  1219. + if (!bs->workq) {
  1220. + dev_err(&pdev->dev, "could not create workqueue\n");
  1221. + goto out_iounmap;
  1222. + }
  1223. +
  1224. + bs->irq = irq;
  1225. + bs->clk = clk;
  1226. + bs->stopping = false;
  1227. +
  1228. + err = request_irq(irq, bcm2708_spi_interrupt, 0, dev_name(&pdev->dev),
  1229. + master);
  1230. + if (err) {
  1231. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  1232. + goto out_workqueue;
  1233. + }
  1234. +
  1235. + /* initialise the hardware */
  1236. + clk_enable(clk);
  1237. + bcm2708_wr(bs, SPI_CS, SPI_CS_REN | SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  1238. +
  1239. + err = spi_register_master(master);
  1240. + if (err) {
  1241. + dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  1242. + goto out_free_irq;
  1243. + }
  1244. +
  1245. + dev_info(&pdev->dev, "SPI Controller at 0x%08lx (irq %d)\n",
  1246. + (unsigned long)regs->start, irq);
  1247. +
  1248. + return 0;
  1249. +
  1250. +out_free_irq:
  1251. + free_irq(bs->irq, master);
  1252. +out_workqueue:
  1253. + destroy_workqueue(bs->workq);
  1254. +out_iounmap:
  1255. + iounmap(bs->base);
  1256. +out_master_put:
  1257. + spi_master_put(master);
  1258. +out_clk_put:
  1259. + clk_put(clk);
  1260. + return err;
  1261. +}
  1262. +
  1263. +static int bcm2708_spi_remove(struct platform_device *pdev)
  1264. +{
  1265. + struct spi_master *master = platform_get_drvdata(pdev);
  1266. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  1267. +
  1268. + /* reset the hardware and block queue progress */
  1269. + spin_lock_irq(&bs->lock);
  1270. + bs->stopping = true;
  1271. + bcm2708_wr(bs, SPI_CS, SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  1272. + spin_unlock_irq(&bs->lock);
  1273. +
  1274. + flush_work(&bs->work);
  1275. +
  1276. + clk_disable(bs->clk);
  1277. + clk_put(bs->clk);
  1278. + free_irq(bs->irq, master);
  1279. + iounmap(bs->base);
  1280. +
  1281. + spi_unregister_master(master);
  1282. +
  1283. + return 0;
  1284. +}
  1285. +
  1286. +static struct platform_driver bcm2708_spi_driver = {
  1287. + .driver = {
  1288. + .name = DRV_NAME,
  1289. + .owner = THIS_MODULE,
  1290. + },
  1291. + .probe = bcm2708_spi_probe,
  1292. + .remove = bcm2708_spi_remove,
  1293. +};
  1294. +
  1295. +
  1296. +static int __init bcm2708_spi_init(void)
  1297. +{
  1298. + return platform_driver_probe(&bcm2708_spi_driver, bcm2708_spi_probe);
  1299. +}
  1300. +module_init(bcm2708_spi_init);
  1301. +
  1302. +static void __exit bcm2708_spi_exit(void)
  1303. +{
  1304. + platform_driver_unregister(&bcm2708_spi_driver);
  1305. +}
  1306. +module_exit(bcm2708_spi_exit);
  1307. +
  1308. +
  1309. +//module_platform_driver(bcm2708_spi_driver);
  1310. +
  1311. +MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2708");
  1312. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  1313. +MODULE_LICENSE("GPL v2");
  1314. +MODULE_ALIAS("platform:" DRV_NAME);