spi-fsl-dspi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * drivers/spi/spi-fsl-dspi.c
  3. *
  4. * Copyright 2013 Freescale Semiconductor, Inc.
  5. *
  6. * Freescale DSPI driver
  7. * This file contains a driver for the Freescale DSPI
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/err.h>
  18. #include <linux/errno.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/math64.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/regmap.h>
  29. #include <linux/sched.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/spi/spi_bitbang.h>
  32. #include <linux/time.h>
  33. #define DRIVER_NAME "fsl-dspi"
  34. #define TRAN_STATE_RX_VOID 0x01
  35. #define TRAN_STATE_TX_VOID 0x02
  36. #define TRAN_STATE_WORD_ODD_NUM 0x04
  37. #define DSPI_FIFO_SIZE 4
  38. #define SPI_MCR 0x00
  39. #define SPI_MCR_MASTER (1 << 31)
  40. #define SPI_MCR_PCSIS (0x3F << 16)
  41. #define SPI_MCR_CLR_TXF (1 << 11)
  42. #define SPI_MCR_CLR_RXF (1 << 10)
  43. #define SPI_TCR 0x08
  44. #define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
  45. #define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
  46. #define SPI_CTAR_CPOL(x) ((x) << 26)
  47. #define SPI_CTAR_CPHA(x) ((x) << 25)
  48. #define SPI_CTAR_LSBFE(x) ((x) << 24)
  49. #define SPI_CTAR_PCSSCK(x) (((x) & 0x00000003) << 22)
  50. #define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
  51. #define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
  52. #define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
  53. #define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
  54. #define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
  55. #define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
  56. #define SPI_CTAR_BR(x) ((x) & 0x0000000f)
  57. #define SPI_CTAR_SCALE_BITS 0xf
  58. #define SPI_CTAR0_SLAVE 0x0c
  59. #define SPI_SR 0x2c
  60. #define SPI_SR_EOQF 0x10000000
  61. #define SPI_SR_TCFQF 0x80000000
  62. #define SPI_RSER 0x30
  63. #define SPI_RSER_EOQFE 0x10000000
  64. #define SPI_RSER_TCFQE 0x80000000
  65. #define SPI_PUSHR 0x34
  66. #define SPI_PUSHR_CONT (1 << 31)
  67. #define SPI_PUSHR_CTAS(x) (((x) & 0x00000003) << 28)
  68. #define SPI_PUSHR_EOQ (1 << 27)
  69. #define SPI_PUSHR_CTCNT (1 << 26)
  70. #define SPI_PUSHR_PCS(x) (((1 << x) & 0x0000003f) << 16)
  71. #define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
  72. #define SPI_PUSHR_SLAVE 0x34
  73. #define SPI_POPR 0x38
  74. #define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
  75. #define SPI_TXFR0 0x3c
  76. #define SPI_TXFR1 0x40
  77. #define SPI_TXFR2 0x44
  78. #define SPI_TXFR3 0x48
  79. #define SPI_RXFR0 0x7c
  80. #define SPI_RXFR1 0x80
  81. #define SPI_RXFR2 0x84
  82. #define SPI_RXFR3 0x88
  83. #define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
  84. #define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
  85. #define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
  86. #define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
  87. #define SPI_CS_INIT 0x01
  88. #define SPI_CS_ASSERT 0x02
  89. #define SPI_CS_DROP 0x04
  90. struct chip_data {
  91. u32 mcr_val;
  92. u32 ctar_val;
  93. u16 void_write_data;
  94. };
  95. enum dspi_trans_mode {
  96. DSPI_EOQ_MODE = 0,
  97. DSPI_TCFQ_MODE,
  98. };
  99. struct fsl_dspi_devtype_data {
  100. enum dspi_trans_mode trans_mode;
  101. };
  102. static const struct fsl_dspi_devtype_data vf610_data = {
  103. .trans_mode = DSPI_EOQ_MODE,
  104. };
  105. static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
  106. .trans_mode = DSPI_TCFQ_MODE,
  107. };
  108. static const struct fsl_dspi_devtype_data ls2085a_data = {
  109. .trans_mode = DSPI_TCFQ_MODE,
  110. };
  111. struct fsl_dspi {
  112. struct spi_master *master;
  113. struct platform_device *pdev;
  114. struct regmap *regmap;
  115. int irq;
  116. struct clk *clk;
  117. struct spi_transfer *cur_transfer;
  118. struct spi_message *cur_msg;
  119. struct chip_data *cur_chip;
  120. size_t len;
  121. void *tx;
  122. void *tx_end;
  123. void *rx;
  124. void *rx_end;
  125. char dataflags;
  126. u8 cs;
  127. u16 void_write_data;
  128. u32 cs_change;
  129. struct fsl_dspi_devtype_data *devtype_data;
  130. wait_queue_head_t waitq;
  131. u32 waitflags;
  132. };
  133. static inline int is_double_byte_mode(struct fsl_dspi *dspi)
  134. {
  135. unsigned int val;
  136. regmap_read(dspi->regmap, SPI_CTAR(dspi->cs), &val);
  137. return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
  138. }
  139. static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
  140. unsigned long clkrate)
  141. {
  142. /* Valid baud rate pre-scaler values */
  143. int pbr_tbl[4] = {2, 3, 5, 7};
  144. int brs[16] = { 2, 4, 6, 8,
  145. 16, 32, 64, 128,
  146. 256, 512, 1024, 2048,
  147. 4096, 8192, 16384, 32768 };
  148. int scale_needed, scale, minscale = INT_MAX;
  149. int i, j;
  150. scale_needed = clkrate / speed_hz;
  151. if (clkrate % speed_hz)
  152. scale_needed++;
  153. for (i = 0; i < ARRAY_SIZE(brs); i++)
  154. for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
  155. scale = brs[i] * pbr_tbl[j];
  156. if (scale >= scale_needed) {
  157. if (scale < minscale) {
  158. minscale = scale;
  159. *br = i;
  160. *pbr = j;
  161. }
  162. break;
  163. }
  164. }
  165. if (minscale == INT_MAX) {
  166. pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
  167. speed_hz, clkrate);
  168. *pbr = ARRAY_SIZE(pbr_tbl) - 1;
  169. *br = ARRAY_SIZE(brs) - 1;
  170. }
  171. }
  172. static void ns_delay_scale(char *psc, char *sc, int delay_ns,
  173. unsigned long clkrate)
  174. {
  175. int pscale_tbl[4] = {1, 3, 5, 7};
  176. int scale_needed, scale, minscale = INT_MAX;
  177. int i, j;
  178. u32 remainder;
  179. scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
  180. &remainder);
  181. if (remainder)
  182. scale_needed++;
  183. for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
  184. for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
  185. scale = pscale_tbl[i] * (2 << j);
  186. if (scale >= scale_needed) {
  187. if (scale < minscale) {
  188. minscale = scale;
  189. *psc = i;
  190. *sc = j;
  191. }
  192. break;
  193. }
  194. }
  195. if (minscale == INT_MAX) {
  196. pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
  197. delay_ns, clkrate);
  198. *psc = ARRAY_SIZE(pscale_tbl) - 1;
  199. *sc = SPI_CTAR_SCALE_BITS;
  200. }
  201. }
  202. static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word)
  203. {
  204. u16 d16;
  205. if (!(dspi->dataflags & TRAN_STATE_TX_VOID))
  206. d16 = tx_word ? *(u16 *)dspi->tx : *(u8 *)dspi->tx;
  207. else
  208. d16 = dspi->void_write_data;
  209. dspi->tx += tx_word + 1;
  210. dspi->len -= tx_word + 1;
  211. return SPI_PUSHR_TXDATA(d16) |
  212. SPI_PUSHR_PCS(dspi->cs) |
  213. SPI_PUSHR_CTAS(dspi->cs) |
  214. SPI_PUSHR_CONT;
  215. }
  216. static void dspi_data_from_popr(struct fsl_dspi *dspi, int rx_word)
  217. {
  218. u16 d;
  219. unsigned int val;
  220. regmap_read(dspi->regmap, SPI_POPR, &val);
  221. d = SPI_POPR_RXDATA(val);
  222. if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
  223. rx_word ? (*(u16 *)dspi->rx = d) : (*(u8 *)dspi->rx = d);
  224. dspi->rx += rx_word + 1;
  225. }
  226. static int dspi_eoq_write(struct fsl_dspi *dspi)
  227. {
  228. int tx_count = 0;
  229. int tx_word;
  230. u32 dspi_pushr = 0;
  231. int first = 1;
  232. tx_word = is_double_byte_mode(dspi);
  233. while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) {
  234. /* If we are in word mode, only have a single byte to transfer
  235. * switch to byte mode temporarily. Will switch back at the
  236. * end of the transfer.
  237. */
  238. if (tx_word && (dspi->len == 1)) {
  239. dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
  240. regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
  241. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
  242. tx_word = 0;
  243. }
  244. dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
  245. if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
  246. /* last transfer in the transfer */
  247. dspi_pushr |= SPI_PUSHR_EOQ;
  248. if ((dspi->cs_change) && (!dspi->len))
  249. dspi_pushr &= ~SPI_PUSHR_CONT;
  250. } else if (tx_word && (dspi->len == 1))
  251. dspi_pushr |= SPI_PUSHR_EOQ;
  252. if (first) {
  253. first = 0;
  254. dspi_pushr |= SPI_PUSHR_CTCNT; /* clear counter */
  255. }
  256. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  257. tx_count++;
  258. }
  259. return tx_count * (tx_word + 1);
  260. }
  261. static int dspi_eoq_read(struct fsl_dspi *dspi)
  262. {
  263. int rx_count = 0;
  264. int rx_word = is_double_byte_mode(dspi);
  265. while ((dspi->rx < dspi->rx_end)
  266. && (rx_count < DSPI_FIFO_SIZE)) {
  267. if (rx_word && (dspi->rx_end - dspi->rx) == 1)
  268. rx_word = 0;
  269. dspi_data_from_popr(dspi, rx_word);
  270. rx_count++;
  271. }
  272. return rx_count;
  273. }
  274. static int dspi_tcfq_write(struct fsl_dspi *dspi)
  275. {
  276. int tx_word;
  277. u32 dspi_pushr = 0;
  278. tx_word = is_double_byte_mode(dspi);
  279. if (tx_word && (dspi->len == 1)) {
  280. dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
  281. regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
  282. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
  283. tx_word = 0;
  284. }
  285. dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
  286. if ((dspi->cs_change) && (!dspi->len))
  287. dspi_pushr &= ~SPI_PUSHR_CONT;
  288. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  289. return tx_word + 1;
  290. }
  291. static void dspi_tcfq_read(struct fsl_dspi *dspi)
  292. {
  293. int rx_word = is_double_byte_mode(dspi);
  294. if (rx_word && (dspi->rx_end - dspi->rx) == 1)
  295. rx_word = 0;
  296. dspi_data_from_popr(dspi, rx_word);
  297. }
  298. static int dspi_transfer_one_message(struct spi_master *master,
  299. struct spi_message *message)
  300. {
  301. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  302. struct spi_device *spi = message->spi;
  303. struct spi_transfer *transfer;
  304. int status = 0;
  305. enum dspi_trans_mode trans_mode;
  306. message->actual_length = 0;
  307. list_for_each_entry(transfer, &message->transfers, transfer_list) {
  308. dspi->cur_transfer = transfer;
  309. dspi->cur_msg = message;
  310. dspi->cur_chip = spi_get_ctldata(spi);
  311. dspi->cs = spi->chip_select;
  312. dspi->cs_change = 0;
  313. if (dspi->cur_transfer->transfer_list.next
  314. == &dspi->cur_msg->transfers)
  315. dspi->cs_change = 1;
  316. dspi->void_write_data = dspi->cur_chip->void_write_data;
  317. dspi->dataflags = 0;
  318. dspi->tx = (void *)transfer->tx_buf;
  319. dspi->tx_end = dspi->tx + transfer->len;
  320. dspi->rx = transfer->rx_buf;
  321. dspi->rx_end = dspi->rx + transfer->len;
  322. dspi->len = transfer->len;
  323. if (!dspi->rx)
  324. dspi->dataflags |= TRAN_STATE_RX_VOID;
  325. if (!dspi->tx)
  326. dspi->dataflags |= TRAN_STATE_TX_VOID;
  327. regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
  328. regmap_update_bits(dspi->regmap, SPI_MCR,
  329. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
  330. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
  331. regmap_write(dspi->regmap, SPI_CTAR(dspi->cs),
  332. dspi->cur_chip->ctar_val);
  333. if (transfer->speed_hz)
  334. regmap_write(dspi->regmap, SPI_CTAR(dspi->cs),
  335. dspi->cur_chip->ctar_val);
  336. trans_mode = dspi->devtype_data->trans_mode;
  337. switch (trans_mode) {
  338. case DSPI_EOQ_MODE:
  339. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
  340. message->actual_length += dspi_eoq_write(dspi);
  341. break;
  342. case DSPI_TCFQ_MODE:
  343. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
  344. message->actual_length += dspi_tcfq_write(dspi);
  345. break;
  346. default:
  347. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  348. trans_mode);
  349. status = -EINVAL;
  350. goto out;
  351. }
  352. if (wait_event_interruptible(dspi->waitq, dspi->waitflags))
  353. dev_err(&dspi->pdev->dev, "wait transfer complete fail!\n");
  354. dspi->waitflags = 0;
  355. if (transfer->delay_usecs)
  356. udelay(transfer->delay_usecs);
  357. }
  358. out:
  359. message->status = status;
  360. spi_finalize_current_message(master);
  361. return status;
  362. }
  363. static int dspi_setup(struct spi_device *spi)
  364. {
  365. struct chip_data *chip;
  366. struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
  367. u32 cs_sck_delay = 0, sck_cs_delay = 0;
  368. unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
  369. unsigned char pasc = 0, asc = 0, fmsz = 0;
  370. unsigned long clkrate;
  371. if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
  372. fmsz = spi->bits_per_word - 1;
  373. } else {
  374. pr_err("Invalid wordsize\n");
  375. return -ENODEV;
  376. }
  377. /* Only alloc on first setup */
  378. chip = spi_get_ctldata(spi);
  379. if (chip == NULL) {
  380. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  381. if (!chip)
  382. return -ENOMEM;
  383. }
  384. of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
  385. &cs_sck_delay);
  386. of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
  387. &sck_cs_delay);
  388. chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
  389. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
  390. chip->void_write_data = 0;
  391. clkrate = clk_get_rate(dspi->clk);
  392. hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
  393. /* Set PCS to SCK delay scale values */
  394. ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
  395. /* Set After SCK delay scale values */
  396. ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
  397. chip->ctar_val = SPI_CTAR_FMSZ(fmsz)
  398. | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
  399. | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
  400. | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
  401. | SPI_CTAR_PCSSCK(pcssck)
  402. | SPI_CTAR_CSSCK(cssck)
  403. | SPI_CTAR_PASC(pasc)
  404. | SPI_CTAR_ASC(asc)
  405. | SPI_CTAR_PBR(pbr)
  406. | SPI_CTAR_BR(br);
  407. spi_set_ctldata(spi, chip);
  408. return 0;
  409. }
  410. static void dspi_cleanup(struct spi_device *spi)
  411. {
  412. struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
  413. dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
  414. spi->master->bus_num, spi->chip_select);
  415. kfree(chip);
  416. }
  417. static irqreturn_t dspi_interrupt(int irq, void *dev_id)
  418. {
  419. struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
  420. struct spi_message *msg = dspi->cur_msg;
  421. enum dspi_trans_mode trans_mode;
  422. u32 spi_sr;
  423. regmap_read(dspi->regmap, SPI_SR, &spi_sr);
  424. regmap_write(dspi->regmap, SPI_SR, spi_sr);
  425. trans_mode = dspi->devtype_data->trans_mode;
  426. switch (trans_mode) {
  427. case DSPI_EOQ_MODE:
  428. dspi_eoq_read(dspi);
  429. break;
  430. case DSPI_TCFQ_MODE:
  431. dspi_tcfq_read(dspi);
  432. break;
  433. default:
  434. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  435. trans_mode);
  436. return IRQ_HANDLED;
  437. }
  438. if (!dspi->len) {
  439. if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) {
  440. regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
  441. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(16));
  442. dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM;
  443. }
  444. dspi->waitflags = 1;
  445. wake_up_interruptible(&dspi->waitq);
  446. } else {
  447. switch (trans_mode) {
  448. case DSPI_EOQ_MODE:
  449. msg->actual_length += dspi_eoq_write(dspi);
  450. break;
  451. case DSPI_TCFQ_MODE:
  452. msg->actual_length += dspi_tcfq_write(dspi);
  453. break;
  454. default:
  455. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  456. trans_mode);
  457. }
  458. }
  459. return IRQ_HANDLED;
  460. }
  461. static const struct of_device_id fsl_dspi_dt_ids[] = {
  462. { .compatible = "fsl,vf610-dspi", .data = (void *)&vf610_data, },
  463. { .compatible = "fsl,ls1021a-v1.0-dspi",
  464. .data = (void *)&ls1021a_v1_data, },
  465. { .compatible = "fsl,ls2085a-dspi", .data = (void *)&ls2085a_data, },
  466. { /* sentinel */ }
  467. };
  468. MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
  469. #ifdef CONFIG_PM_SLEEP
  470. static int dspi_suspend(struct device *dev)
  471. {
  472. struct spi_master *master = dev_get_drvdata(dev);
  473. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  474. spi_master_suspend(master);
  475. clk_disable_unprepare(dspi->clk);
  476. return 0;
  477. }
  478. static int dspi_resume(struct device *dev)
  479. {
  480. struct spi_master *master = dev_get_drvdata(dev);
  481. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  482. clk_prepare_enable(dspi->clk);
  483. spi_master_resume(master);
  484. return 0;
  485. }
  486. #endif /* CONFIG_PM_SLEEP */
  487. static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
  488. static const struct regmap_config dspi_regmap_config = {
  489. .reg_bits = 32,
  490. .val_bits = 32,
  491. .reg_stride = 4,
  492. .max_register = 0x88,
  493. };
  494. static int dspi_probe(struct platform_device *pdev)
  495. {
  496. struct device_node *np = pdev->dev.of_node;
  497. struct spi_master *master;
  498. struct fsl_dspi *dspi;
  499. struct resource *res;
  500. void __iomem *base;
  501. int ret = 0, cs_num, bus_num;
  502. const struct of_device_id *of_id =
  503. of_match_device(fsl_dspi_dt_ids, &pdev->dev);
  504. master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
  505. if (!master)
  506. return -ENOMEM;
  507. dspi = spi_master_get_devdata(master);
  508. dspi->pdev = pdev;
  509. dspi->master = master;
  510. master->transfer = NULL;
  511. master->setup = dspi_setup;
  512. master->transfer_one_message = dspi_transfer_one_message;
  513. master->dev.of_node = pdev->dev.of_node;
  514. master->cleanup = dspi_cleanup;
  515. master->mode_bits = SPI_CPOL | SPI_CPHA;
  516. master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
  517. SPI_BPW_MASK(16);
  518. ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
  519. if (ret < 0) {
  520. dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
  521. goto out_master_put;
  522. }
  523. master->num_chipselect = cs_num;
  524. ret = of_property_read_u32(np, "bus-num", &bus_num);
  525. if (ret < 0) {
  526. dev_err(&pdev->dev, "can't get bus-num\n");
  527. goto out_master_put;
  528. }
  529. master->bus_num = bus_num;
  530. dspi->devtype_data = (struct fsl_dspi_devtype_data *)of_id->data;
  531. if (!dspi->devtype_data) {
  532. dev_err(&pdev->dev, "can't get devtype_data\n");
  533. ret = -EFAULT;
  534. goto out_master_put;
  535. }
  536. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  537. base = devm_ioremap_resource(&pdev->dev, res);
  538. if (IS_ERR(base)) {
  539. ret = PTR_ERR(base);
  540. goto out_master_put;
  541. }
  542. dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  543. &dspi_regmap_config);
  544. if (IS_ERR(dspi->regmap)) {
  545. dev_err(&pdev->dev, "failed to init regmap: %ld\n",
  546. PTR_ERR(dspi->regmap));
  547. return PTR_ERR(dspi->regmap);
  548. }
  549. dspi->irq = platform_get_irq(pdev, 0);
  550. if (dspi->irq < 0) {
  551. dev_err(&pdev->dev, "can't get platform irq\n");
  552. ret = dspi->irq;
  553. goto out_master_put;
  554. }
  555. ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
  556. pdev->name, dspi);
  557. if (ret < 0) {
  558. dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
  559. goto out_master_put;
  560. }
  561. dspi->clk = devm_clk_get(&pdev->dev, "dspi");
  562. if (IS_ERR(dspi->clk)) {
  563. ret = PTR_ERR(dspi->clk);
  564. dev_err(&pdev->dev, "unable to get clock\n");
  565. goto out_master_put;
  566. }
  567. clk_prepare_enable(dspi->clk);
  568. init_waitqueue_head(&dspi->waitq);
  569. platform_set_drvdata(pdev, master);
  570. ret = spi_register_master(master);
  571. if (ret != 0) {
  572. dev_err(&pdev->dev, "Problem registering DSPI master\n");
  573. goto out_clk_put;
  574. }
  575. return ret;
  576. out_clk_put:
  577. clk_disable_unprepare(dspi->clk);
  578. out_master_put:
  579. spi_master_put(master);
  580. return ret;
  581. }
  582. static int dspi_remove(struct platform_device *pdev)
  583. {
  584. struct spi_master *master = platform_get_drvdata(pdev);
  585. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  586. /* Disconnect from the SPI framework */
  587. clk_disable_unprepare(dspi->clk);
  588. spi_unregister_master(dspi->master);
  589. spi_master_put(dspi->master);
  590. return 0;
  591. }
  592. static struct platform_driver fsl_dspi_driver = {
  593. .driver.name = DRIVER_NAME,
  594. .driver.of_match_table = fsl_dspi_dt_ids,
  595. .driver.owner = THIS_MODULE,
  596. .driver.pm = &dspi_pm,
  597. .probe = dspi_probe,
  598. .remove = dspi_remove,
  599. };
  600. module_platform_driver(fsl_dspi_driver);
  601. MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
  602. MODULE_LICENSE("GPL");
  603. MODULE_ALIAS("platform:" DRIVER_NAME);