mxc_spi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  3. * Copyright (C) 2008 Juergen Beisert
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the
  16. * Free Software Foundation
  17. * 51 Franklin Street, Fifth Floor
  18. * Boston, MA 02110-1301, USA.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/completion.h>
  22. #include <linux/delay.h>
  23. #include <linux/err.h>
  24. #include <linux/gpio.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/irq.h>
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/spi/spi.h>
  33. #include <linux/spi/spi_bitbang.h>
  34. #include <linux/types.h>
  35. #include <mach/spi.h>
  36. #define DRIVER_NAME "spi_imx"
  37. #define MXC_CSPIRXDATA 0x00
  38. #define MXC_CSPITXDATA 0x04
  39. #define MXC_CSPICTRL 0x08
  40. #define MXC_CSPIINT 0x0c
  41. #define MXC_RESET 0x1c
  42. /* generic defines to abstract from the different register layouts */
  43. #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
  44. #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
  45. struct mxc_spi_config {
  46. unsigned int speed_hz;
  47. unsigned int bpw;
  48. unsigned int mode;
  49. int cs;
  50. };
  51. struct mxc_spi_data {
  52. struct spi_bitbang bitbang;
  53. struct completion xfer_done;
  54. void *base;
  55. int irq;
  56. struct clk *clk;
  57. unsigned long spi_clk;
  58. int *chipselect;
  59. unsigned int count;
  60. void (*tx)(struct mxc_spi_data *);
  61. void (*rx)(struct mxc_spi_data *);
  62. void *rx_buf;
  63. const void *tx_buf;
  64. unsigned int txfifo; /* number of words pushed in tx FIFO */
  65. /* SoC specific functions */
  66. void (*intctrl)(struct mxc_spi_data *, int);
  67. int (*config)(struct mxc_spi_data *, struct mxc_spi_config *);
  68. void (*trigger)(struct mxc_spi_data *);
  69. int (*rx_available)(struct mxc_spi_data *);
  70. };
  71. #define MXC_SPI_BUF_RX(type) \
  72. static void mxc_spi_buf_rx_##type(struct mxc_spi_data *mxc_spi) \
  73. { \
  74. unsigned int val = readl(mxc_spi->base + MXC_CSPIRXDATA); \
  75. \
  76. if (mxc_spi->rx_buf) { \
  77. *(type *)mxc_spi->rx_buf = val; \
  78. mxc_spi->rx_buf += sizeof(type); \
  79. } \
  80. }
  81. #define MXC_SPI_BUF_TX(type) \
  82. static void mxc_spi_buf_tx_##type(struct mxc_spi_data *mxc_spi) \
  83. { \
  84. type val = 0; \
  85. \
  86. if (mxc_spi->tx_buf) { \
  87. val = *(type *)mxc_spi->tx_buf; \
  88. mxc_spi->tx_buf += sizeof(type); \
  89. } \
  90. \
  91. mxc_spi->count -= sizeof(type); \
  92. \
  93. writel(val, mxc_spi->base + MXC_CSPITXDATA); \
  94. }
  95. MXC_SPI_BUF_RX(u8)
  96. MXC_SPI_BUF_TX(u8)
  97. MXC_SPI_BUF_RX(u16)
  98. MXC_SPI_BUF_TX(u16)
  99. MXC_SPI_BUF_RX(u32)
  100. MXC_SPI_BUF_TX(u32)
  101. /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
  102. * (which is currently not the case in this driver)
  103. */
  104. static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
  105. 256, 384, 512, 768, 1024};
  106. /* MX21, MX27 */
  107. static unsigned int mxc_spi_clkdiv_1(unsigned int fin,
  108. unsigned int fspi)
  109. {
  110. int i, max;
  111. if (cpu_is_mx21())
  112. max = 18;
  113. else
  114. max = 16;
  115. for (i = 2; i < max; i++)
  116. if (fspi * mxc_clkdivs[i] >= fin)
  117. return i;
  118. return max;
  119. }
  120. /* MX1, MX31, MX35 */
  121. static unsigned int mxc_spi_clkdiv_2(unsigned int fin,
  122. unsigned int fspi)
  123. {
  124. int i, div = 4;
  125. for (i = 0; i < 7; i++) {
  126. if (fspi * div >= fin)
  127. return i;
  128. div <<= 1;
  129. }
  130. return 7;
  131. }
  132. #define MX31_INTREG_TEEN (1 << 0)
  133. #define MX31_INTREG_RREN (1 << 3)
  134. #define MX31_CSPICTRL_ENABLE (1 << 0)
  135. #define MX31_CSPICTRL_MASTER (1 << 1)
  136. #define MX31_CSPICTRL_XCH (1 << 2)
  137. #define MX31_CSPICTRL_POL (1 << 4)
  138. #define MX31_CSPICTRL_PHA (1 << 5)
  139. #define MX31_CSPICTRL_SSCTL (1 << 6)
  140. #define MX31_CSPICTRL_SSPOL (1 << 7)
  141. #define MX31_CSPICTRL_BC_SHIFT 8
  142. #define MX35_CSPICTRL_BL_SHIFT 20
  143. #define MX31_CSPICTRL_CS_SHIFT 24
  144. #define MX35_CSPICTRL_CS_SHIFT 12
  145. #define MX31_CSPICTRL_DR_SHIFT 16
  146. #define MX31_CSPISTATUS 0x14
  147. #define MX31_STATUS_RR (1 << 3)
  148. /* These functions also work for the i.MX35, but be aware that
  149. * the i.MX35 has a slightly different register layout for bits
  150. * we do not use here.
  151. */
  152. static void mx31_intctrl(struct mxc_spi_data *mxc_spi, int enable)
  153. {
  154. unsigned int val = 0;
  155. if (enable & MXC_INT_TE)
  156. val |= MX31_INTREG_TEEN;
  157. if (enable & MXC_INT_RR)
  158. val |= MX31_INTREG_RREN;
  159. writel(val, mxc_spi->base + MXC_CSPIINT);
  160. }
  161. static void mx31_trigger(struct mxc_spi_data *mxc_spi)
  162. {
  163. unsigned int reg;
  164. reg = readl(mxc_spi->base + MXC_CSPICTRL);
  165. reg |= MX31_CSPICTRL_XCH;
  166. writel(reg, mxc_spi->base + MXC_CSPICTRL);
  167. }
  168. static int mx31_config(struct mxc_spi_data *mxc_spi,
  169. struct mxc_spi_config *config)
  170. {
  171. unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
  172. reg |= mxc_spi_clkdiv_2(mxc_spi->spi_clk, config->speed_hz) <<
  173. MX31_CSPICTRL_DR_SHIFT;
  174. if (cpu_is_mx31())
  175. reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT;
  176. else if (cpu_is_mx35()) {
  177. reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT;
  178. reg |= MX31_CSPICTRL_SSCTL;
  179. }
  180. if (config->mode & SPI_CPHA)
  181. reg |= MX31_CSPICTRL_PHA;
  182. if (config->mode & SPI_CPOL)
  183. reg |= MX31_CSPICTRL_POL;
  184. if (config->mode & SPI_CS_HIGH)
  185. reg |= MX31_CSPICTRL_SSPOL;
  186. if (config->cs < 0) {
  187. if (cpu_is_mx31())
  188. reg |= (config->cs + 32) << MX31_CSPICTRL_CS_SHIFT;
  189. else if (cpu_is_mx35())
  190. reg |= (config->cs + 32) << MX35_CSPICTRL_CS_SHIFT;
  191. }
  192. writel(reg, mxc_spi->base + MXC_CSPICTRL);
  193. return 0;
  194. }
  195. static int mx31_rx_available(struct mxc_spi_data *mxc_spi)
  196. {
  197. return readl(mxc_spi->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
  198. }
  199. #define MX27_INTREG_RR (1 << 4)
  200. #define MX27_INTREG_TEEN (1 << 9)
  201. #define MX27_INTREG_RREN (1 << 13)
  202. #define MX27_CSPICTRL_POL (1 << 5)
  203. #define MX27_CSPICTRL_PHA (1 << 6)
  204. #define MX27_CSPICTRL_SSPOL (1 << 8)
  205. #define MX27_CSPICTRL_XCH (1 << 9)
  206. #define MX27_CSPICTRL_ENABLE (1 << 10)
  207. #define MX27_CSPICTRL_MASTER (1 << 11)
  208. #define MX27_CSPICTRL_DR_SHIFT 14
  209. #define MX27_CSPICTRL_CS_SHIFT 19
  210. static void mx27_intctrl(struct mxc_spi_data *mxc_spi, int enable)
  211. {
  212. unsigned int val = 0;
  213. if (enable & MXC_INT_TE)
  214. val |= MX27_INTREG_TEEN;
  215. if (enable & MXC_INT_RR)
  216. val |= MX27_INTREG_RREN;
  217. writel(val, mxc_spi->base + MXC_CSPIINT);
  218. }
  219. static void mx27_trigger(struct mxc_spi_data *mxc_spi)
  220. {
  221. unsigned int reg;
  222. reg = readl(mxc_spi->base + MXC_CSPICTRL);
  223. reg |= MX27_CSPICTRL_XCH;
  224. writel(reg, mxc_spi->base + MXC_CSPICTRL);
  225. }
  226. static int mx27_config(struct mxc_spi_data *mxc_spi,
  227. struct mxc_spi_config *config)
  228. {
  229. unsigned int reg = MX27_CSPICTRL_ENABLE | MX27_CSPICTRL_MASTER;
  230. reg |= mxc_spi_clkdiv_1(mxc_spi->spi_clk, config->speed_hz) <<
  231. MX27_CSPICTRL_DR_SHIFT;
  232. reg |= config->bpw - 1;
  233. if (config->mode & SPI_CPHA)
  234. reg |= MX27_CSPICTRL_PHA;
  235. if (config->mode & SPI_CPOL)
  236. reg |= MX27_CSPICTRL_POL;
  237. if (config->mode & SPI_CS_HIGH)
  238. reg |= MX27_CSPICTRL_SSPOL;
  239. if (config->cs < 0)
  240. reg |= (config->cs + 32) << MX27_CSPICTRL_CS_SHIFT;
  241. writel(reg, mxc_spi->base + MXC_CSPICTRL);
  242. return 0;
  243. }
  244. static int mx27_rx_available(struct mxc_spi_data *mxc_spi)
  245. {
  246. return readl(mxc_spi->base + MXC_CSPIINT) & MX27_INTREG_RR;
  247. }
  248. #define MX1_INTREG_RR (1 << 3)
  249. #define MX1_INTREG_TEEN (1 << 8)
  250. #define MX1_INTREG_RREN (1 << 11)
  251. #define MX1_CSPICTRL_POL (1 << 4)
  252. #define MX1_CSPICTRL_PHA (1 << 5)
  253. #define MX1_CSPICTRL_XCH (1 << 8)
  254. #define MX1_CSPICTRL_ENABLE (1 << 9)
  255. #define MX1_CSPICTRL_MASTER (1 << 10)
  256. #define MX1_CSPICTRL_DR_SHIFT 13
  257. static void mx1_intctrl(struct mxc_spi_data *mxc_spi, int enable)
  258. {
  259. unsigned int val = 0;
  260. if (enable & MXC_INT_TE)
  261. val |= MX1_INTREG_TEEN;
  262. if (enable & MXC_INT_RR)
  263. val |= MX1_INTREG_RREN;
  264. writel(val, mxc_spi->base + MXC_CSPIINT);
  265. }
  266. static void mx1_trigger(struct mxc_spi_data *mxc_spi)
  267. {
  268. unsigned int reg;
  269. reg = readl(mxc_spi->base + MXC_CSPICTRL);
  270. reg |= MX1_CSPICTRL_XCH;
  271. writel(reg, mxc_spi->base + MXC_CSPICTRL);
  272. }
  273. static int mx1_config(struct mxc_spi_data *mxc_spi,
  274. struct mxc_spi_config *config)
  275. {
  276. unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
  277. reg |= mxc_spi_clkdiv_2(mxc_spi->spi_clk, config->speed_hz) <<
  278. MX1_CSPICTRL_DR_SHIFT;
  279. reg |= config->bpw - 1;
  280. if (config->mode & SPI_CPHA)
  281. reg |= MX1_CSPICTRL_PHA;
  282. if (config->mode & SPI_CPOL)
  283. reg |= MX1_CSPICTRL_POL;
  284. writel(reg, mxc_spi->base + MXC_CSPICTRL);
  285. return 0;
  286. }
  287. static int mx1_rx_available(struct mxc_spi_data *mxc_spi)
  288. {
  289. return readl(mxc_spi->base + MXC_CSPIINT) & MX1_INTREG_RR;
  290. }
  291. static void mxc_spi_chipselect(struct spi_device *spi, int is_active)
  292. {
  293. struct mxc_spi_data *mxc_spi = spi_master_get_devdata(spi->master);
  294. unsigned int cs = 0;
  295. int gpio = mxc_spi->chipselect[spi->chip_select];
  296. struct mxc_spi_config config;
  297. if (spi->mode & SPI_CS_HIGH)
  298. cs = 1;
  299. if (is_active == BITBANG_CS_INACTIVE) {
  300. if (gpio >= 0)
  301. gpio_set_value(gpio, !cs);
  302. return;
  303. }
  304. config.bpw = spi->bits_per_word;
  305. config.speed_hz = spi->max_speed_hz;
  306. config.mode = spi->mode;
  307. config.cs = mxc_spi->chipselect[spi->chip_select];
  308. mxc_spi->config(mxc_spi, &config);
  309. /* Initialize the functions for transfer */
  310. if (config.bpw <= 8) {
  311. mxc_spi->rx = mxc_spi_buf_rx_u8;
  312. mxc_spi->tx = mxc_spi_buf_tx_u8;
  313. } else if (config.bpw <= 16) {
  314. mxc_spi->rx = mxc_spi_buf_rx_u16;
  315. mxc_spi->tx = mxc_spi_buf_tx_u16;
  316. } else if (config.bpw <= 32) {
  317. mxc_spi->rx = mxc_spi_buf_rx_u32;
  318. mxc_spi->tx = mxc_spi_buf_tx_u32;
  319. } else
  320. BUG();
  321. if (gpio >= 0)
  322. gpio_set_value(gpio, cs);
  323. return;
  324. }
  325. static void mxc_spi_push(struct mxc_spi_data *mxc_spi)
  326. {
  327. while (mxc_spi->txfifo < 8) {
  328. if (!mxc_spi->count)
  329. break;
  330. mxc_spi->tx(mxc_spi);
  331. mxc_spi->txfifo++;
  332. }
  333. mxc_spi->trigger(mxc_spi);
  334. }
  335. static irqreturn_t mxc_spi_isr(int irq, void *dev_id)
  336. {
  337. struct mxc_spi_data *mxc_spi = dev_id;
  338. while (mxc_spi->rx_available(mxc_spi)) {
  339. mxc_spi->rx(mxc_spi);
  340. mxc_spi->txfifo--;
  341. }
  342. if (mxc_spi->count) {
  343. mxc_spi_push(mxc_spi);
  344. return IRQ_HANDLED;
  345. }
  346. if (mxc_spi->txfifo) {
  347. /* No data left to push, but still waiting for rx data,
  348. * enable receive data available interrupt.
  349. */
  350. mxc_spi->intctrl(mxc_spi, MXC_INT_RR);
  351. return IRQ_HANDLED;
  352. }
  353. mxc_spi->intctrl(mxc_spi, 0);
  354. complete(&mxc_spi->xfer_done);
  355. return IRQ_HANDLED;
  356. }
  357. static int mxc_spi_setupxfer(struct spi_device *spi,
  358. struct spi_transfer *t)
  359. {
  360. struct mxc_spi_data *mxc_spi = spi_master_get_devdata(spi->master);
  361. struct mxc_spi_config config;
  362. config.bpw = t ? t->bits_per_word : spi->bits_per_word;
  363. config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
  364. config.mode = spi->mode;
  365. mxc_spi->config(mxc_spi, &config);
  366. return 0;
  367. }
  368. static int mxc_spi_transfer(struct spi_device *spi,
  369. struct spi_transfer *transfer)
  370. {
  371. struct mxc_spi_data *mxc_spi = spi_master_get_devdata(spi->master);
  372. mxc_spi->tx_buf = transfer->tx_buf;
  373. mxc_spi->rx_buf = transfer->rx_buf;
  374. mxc_spi->count = transfer->len;
  375. mxc_spi->txfifo = 0;
  376. init_completion(&mxc_spi->xfer_done);
  377. mxc_spi_push(mxc_spi);
  378. mxc_spi->intctrl(mxc_spi, MXC_INT_TE);
  379. wait_for_completion(&mxc_spi->xfer_done);
  380. return transfer->len;
  381. }
  382. static int mxc_spi_setup(struct spi_device *spi)
  383. {
  384. if (!spi->bits_per_word)
  385. spi->bits_per_word = 8;
  386. pr_debug("%s: mode %d, %u bpw, %d hz\n", __func__,
  387. spi->mode, spi->bits_per_word, spi->max_speed_hz);
  388. mxc_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  389. return 0;
  390. }
  391. static void mxc_spi_cleanup(struct spi_device *spi)
  392. {
  393. }
  394. static int __init mxc_spi_probe(struct platform_device *pdev)
  395. {
  396. struct spi_imx_master *mxc_platform_info;
  397. struct spi_master *master;
  398. struct mxc_spi_data *mxc_spi;
  399. struct resource *res;
  400. int i, ret;
  401. mxc_platform_info = (struct spi_imx_master *)pdev->dev.platform_data;
  402. if (!mxc_platform_info) {
  403. dev_err(&pdev->dev, "can't get the platform data\n");
  404. return -EINVAL;
  405. }
  406. master = spi_alloc_master(&pdev->dev, sizeof(struct mxc_spi_data));
  407. if (!master)
  408. return -ENOMEM;
  409. platform_set_drvdata(pdev, master);
  410. master->bus_num = pdev->id;
  411. master->num_chipselect = mxc_platform_info->num_chipselect;
  412. mxc_spi = spi_master_get_devdata(master);
  413. mxc_spi->bitbang.master = spi_master_get(master);
  414. mxc_spi->chipselect = mxc_platform_info->chipselect;
  415. for (i = 0; i < master->num_chipselect; i++) {
  416. if (mxc_spi->chipselect[i] < 0)
  417. continue;
  418. ret = gpio_request(mxc_spi->chipselect[i], DRIVER_NAME);
  419. if (ret) {
  420. i--;
  421. while (i > 0)
  422. if (mxc_spi->chipselect[i] >= 0)
  423. gpio_free(mxc_spi->chipselect[i--]);
  424. dev_err(&pdev->dev, "can't get cs gpios");
  425. goto out_master_put;
  426. }
  427. gpio_direction_output(mxc_spi->chipselect[i], 1);
  428. }
  429. mxc_spi->bitbang.chipselect = mxc_spi_chipselect;
  430. mxc_spi->bitbang.setup_transfer = mxc_spi_setupxfer;
  431. mxc_spi->bitbang.txrx_bufs = mxc_spi_transfer;
  432. mxc_spi->bitbang.master->setup = mxc_spi_setup;
  433. mxc_spi->bitbang.master->cleanup = mxc_spi_cleanup;
  434. init_completion(&mxc_spi->xfer_done);
  435. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  436. if (!res) {
  437. dev_err(&pdev->dev, "can't get platform resource\n");
  438. ret = -ENOMEM;
  439. goto out_gpio_free;
  440. }
  441. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  442. dev_err(&pdev->dev, "request_mem_region failed\n");
  443. ret = -EBUSY;
  444. goto out_gpio_free;
  445. }
  446. mxc_spi->base = ioremap(res->start, resource_size(res));
  447. if (!mxc_spi->base) {
  448. ret = -EINVAL;
  449. goto out_release_mem;
  450. }
  451. mxc_spi->irq = platform_get_irq(pdev, 0);
  452. if (!mxc_spi->irq) {
  453. ret = -EINVAL;
  454. goto out_iounmap;
  455. }
  456. ret = request_irq(mxc_spi->irq, mxc_spi_isr, 0, DRIVER_NAME, mxc_spi);
  457. if (ret) {
  458. dev_err(&pdev->dev, "can't get irq%d: %d\n", mxc_spi->irq, ret);
  459. goto out_iounmap;
  460. }
  461. if (cpu_is_mx31() || cpu_is_mx35()) {
  462. mxc_spi->intctrl = mx31_intctrl;
  463. mxc_spi->config = mx31_config;
  464. mxc_spi->trigger = mx31_trigger;
  465. mxc_spi->rx_available = mx31_rx_available;
  466. } else if (cpu_is_mx27() || cpu_is_mx21()) {
  467. mxc_spi->intctrl = mx27_intctrl;
  468. mxc_spi->config = mx27_config;
  469. mxc_spi->trigger = mx27_trigger;
  470. mxc_spi->rx_available = mx27_rx_available;
  471. } else if (cpu_is_mx1()) {
  472. mxc_spi->intctrl = mx1_intctrl;
  473. mxc_spi->config = mx1_config;
  474. mxc_spi->trigger = mx1_trigger;
  475. mxc_spi->rx_available = mx1_rx_available;
  476. } else
  477. BUG();
  478. mxc_spi->clk = clk_get(&pdev->dev, NULL);
  479. if (IS_ERR(mxc_spi->clk)) {
  480. dev_err(&pdev->dev, "unable to get clock\n");
  481. ret = PTR_ERR(mxc_spi->clk);
  482. goto out_free_irq;
  483. }
  484. clk_enable(mxc_spi->clk);
  485. mxc_spi->spi_clk = clk_get_rate(mxc_spi->clk);
  486. if (!cpu_is_mx31() || !cpu_is_mx35())
  487. writel(1, mxc_spi->base + MXC_RESET);
  488. mxc_spi->intctrl(mxc_spi, 0);
  489. ret = spi_bitbang_start(&mxc_spi->bitbang);
  490. if (ret) {
  491. dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
  492. goto out_clk_put;
  493. }
  494. dev_info(&pdev->dev, "probed\n");
  495. return ret;
  496. out_clk_put:
  497. clk_disable(mxc_spi->clk);
  498. clk_put(mxc_spi->clk);
  499. out_free_irq:
  500. free_irq(mxc_spi->irq, mxc_spi);
  501. out_iounmap:
  502. iounmap(mxc_spi->base);
  503. out_release_mem:
  504. release_mem_region(res->start, resource_size(res));
  505. out_gpio_free:
  506. for (i = 0; i < master->num_chipselect; i++)
  507. if (mxc_spi->chipselect[i] >= 0)
  508. gpio_free(mxc_spi->chipselect[i]);
  509. out_master_put:
  510. spi_master_put(master);
  511. kfree(master);
  512. platform_set_drvdata(pdev, NULL);
  513. return ret;
  514. }
  515. static int __exit mxc_spi_remove(struct platform_device *pdev)
  516. {
  517. struct spi_master *master = platform_get_drvdata(pdev);
  518. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  519. struct mxc_spi_data *mxc_spi = spi_master_get_devdata(master);
  520. int i;
  521. spi_bitbang_stop(&mxc_spi->bitbang);
  522. writel(0, mxc_spi->base + MXC_CSPICTRL);
  523. clk_disable(mxc_spi->clk);
  524. clk_put(mxc_spi->clk);
  525. free_irq(mxc_spi->irq, mxc_spi);
  526. iounmap(mxc_spi->base);
  527. for (i = 0; i < master->num_chipselect; i++)
  528. if (mxc_spi->chipselect[i] >= 0)
  529. gpio_free(mxc_spi->chipselect[i]);
  530. spi_master_put(master);
  531. release_mem_region(res->start, resource_size(res));
  532. platform_set_drvdata(pdev, NULL);
  533. return 0;
  534. }
  535. static struct platform_driver mxc_spi_driver = {
  536. .driver = {
  537. .name = DRIVER_NAME,
  538. .owner = THIS_MODULE,
  539. },
  540. .probe = mxc_spi_probe,
  541. .remove = __exit_p(mxc_spi_remove),
  542. };
  543. static int __init mxc_spi_init(void)
  544. {
  545. return platform_driver_register(&mxc_spi_driver);
  546. }
  547. static void __exit mxc_spi_exit(void)
  548. {
  549. platform_driver_unregister(&mxc_spi_driver);
  550. }
  551. module_init(mxc_spi_init);
  552. module_exit(mxc_spi_exit);
  553. MODULE_DESCRIPTION("SPI Master Controller driver");
  554. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  555. MODULE_LICENSE("GPL");