spi-sunplus-sp7021.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (c) 2021 Sunplus Inc.
  3. // Author: Li-hao Kuo <[email protected]>
  4. #include <linux/bitfield.h>
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/reset.h>
  14. #include <linux/spi/spi.h>
  15. #define SP7021_DATA_RDY_REG 0x0044
  16. #define SP7021_SLAVE_DMA_CTRL_REG 0x0048
  17. #define SP7021_SLAVE_DMA_LENGTH_REG 0x004c
  18. #define SP7021_SLAVE_DMA_ADDR_REG 0x004c
  19. #define SP7021_SLAVE_DATA_RDY BIT(0)
  20. #define SP7021_SLAVE_SW_RST BIT(1)
  21. #define SP7021_SLA_DMA_W_INT BIT(8)
  22. #define SP7021_SLAVE_CLR_INT BIT(8)
  23. #define SP7021_SLAVE_DMA_EN BIT(0)
  24. #define SP7021_SLAVE_DMA_RW BIT(6)
  25. #define SP7021_SLAVE_DMA_CMD GENMASK(3, 2)
  26. #define SP7021_FIFO_REG 0x0034
  27. #define SP7021_SPI_STATUS_REG 0x0038
  28. #define SP7021_SPI_CONFIG_REG 0x003c
  29. #define SP7021_INT_BUSY_REG 0x004c
  30. #define SP7021_DMA_CTRL_REG 0x0050
  31. #define SP7021_SPI_START_FD BIT(0)
  32. #define SP7021_FD_SW_RST BIT(1)
  33. #define SP7021_TX_EMP_FLAG BIT(2)
  34. #define SP7021_RX_EMP_FLAG BIT(4)
  35. #define SP7021_RX_FULL_FLAG BIT(5)
  36. #define SP7021_FINISH_FLAG BIT(6)
  37. #define SP7021_TX_CNT_MASK GENMASK(11, 8)
  38. #define SP7021_RX_CNT_MASK GENMASK(15, 12)
  39. #define SP7021_TX_LEN_MASK GENMASK(23, 16)
  40. #define SP7021_GET_LEN_MASK GENMASK(31, 24)
  41. #define SP7021_SET_TX_LEN GENMASK(23, 16)
  42. #define SP7021_SET_XFER_LEN GENMASK(31, 24)
  43. #define SP7021_CPOL_FD BIT(0)
  44. #define SP7021_CPHA_R BIT(1)
  45. #define SP7021_CPHA_W BIT(2)
  46. #define SP7021_LSB_SEL BIT(4)
  47. #define SP7021_CS_POR BIT(5)
  48. #define SP7021_FD_SEL BIT(6)
  49. #define SP7021_RX_UNIT GENMASK(8, 7)
  50. #define SP7021_TX_UNIT GENMASK(10, 9)
  51. #define SP7021_TX_EMP_FLAG_MASK BIT(11)
  52. #define SP7021_RX_FULL_FLAG_MASK BIT(14)
  53. #define SP7021_FINISH_FLAG_MASK BIT(15)
  54. #define SP7021_CLEAN_RW_BYTE GENMASK(10, 7)
  55. #define SP7021_CLEAN_FLUG_MASK GENMASK(15, 11)
  56. #define SP7021_CLK_MASK GENMASK(31, 16)
  57. #define SP7021_INT_BYPASS BIT(3)
  58. #define SP7021_CLR_MASTER_INT BIT(6)
  59. #define SP7021_SPI_DATA_SIZE (255)
  60. #define SP7021_FIFO_DATA_LEN (16)
  61. enum {
  62. SP7021_MASTER_MODE = 0,
  63. SP7021_SLAVE_MODE = 1,
  64. };
  65. struct sp7021_spi_ctlr {
  66. struct device *dev;
  67. struct spi_controller *ctlr;
  68. void __iomem *m_base;
  69. void __iomem *s_base;
  70. u32 xfer_conf;
  71. int mode;
  72. int m_irq;
  73. int s_irq;
  74. struct clk *spi_clk;
  75. struct reset_control *rstc;
  76. // data xfer lock
  77. struct mutex buf_lock;
  78. struct completion isr_done;
  79. struct completion slave_isr;
  80. unsigned int rx_cur_len;
  81. unsigned int tx_cur_len;
  82. unsigned int data_unit;
  83. const u8 *tx_buf;
  84. u8 *rx_buf;
  85. };
  86. static irqreturn_t sp7021_spi_slave_irq(int irq, void *dev)
  87. {
  88. struct sp7021_spi_ctlr *pspim = dev;
  89. unsigned int data_status;
  90. data_status = readl(pspim->s_base + SP7021_DATA_RDY_REG);
  91. data_status |= SP7021_SLAVE_CLR_INT;
  92. writel(data_status , pspim->s_base + SP7021_DATA_RDY_REG);
  93. complete(&pspim->slave_isr);
  94. return IRQ_HANDLED;
  95. }
  96. static int sp7021_spi_slave_abort(struct spi_controller *ctlr)
  97. {
  98. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  99. complete(&pspim->slave_isr);
  100. complete(&pspim->isr_done);
  101. return 0;
  102. }
  103. static int sp7021_spi_slave_tx(struct spi_device *spi, struct spi_transfer *xfer)
  104. {
  105. struct sp7021_spi_ctlr *pspim = spi_controller_get_devdata(spi->controller);
  106. u32 value;
  107. reinit_completion(&pspim->slave_isr);
  108. value = SP7021_SLAVE_DMA_EN | SP7021_SLAVE_DMA_RW | FIELD_PREP(SP7021_SLAVE_DMA_CMD, 3);
  109. writel(value, pspim->s_base + SP7021_SLAVE_DMA_CTRL_REG);
  110. writel(xfer->len, pspim->s_base + SP7021_SLAVE_DMA_LENGTH_REG);
  111. writel(xfer->tx_dma, pspim->s_base + SP7021_SLAVE_DMA_ADDR_REG);
  112. value = readl(pspim->s_base + SP7021_DATA_RDY_REG);
  113. value |= SP7021_SLAVE_DATA_RDY;
  114. writel(value, pspim->s_base + SP7021_DATA_RDY_REG);
  115. if (wait_for_completion_interruptible(&pspim->isr_done)) {
  116. dev_err(&spi->dev, "%s() wait_for_completion err\n", __func__);
  117. return -EINTR;
  118. }
  119. return 0;
  120. }
  121. static int sp7021_spi_slave_rx(struct spi_device *spi, struct spi_transfer *xfer)
  122. {
  123. struct sp7021_spi_ctlr *pspim = spi_controller_get_devdata(spi->controller);
  124. u32 value;
  125. reinit_completion(&pspim->isr_done);
  126. value = SP7021_SLAVE_DMA_EN | FIELD_PREP(SP7021_SLAVE_DMA_CMD, 3);
  127. writel(value, pspim->s_base + SP7021_SLAVE_DMA_CTRL_REG);
  128. writel(xfer->len, pspim->s_base + SP7021_SLAVE_DMA_LENGTH_REG);
  129. writel(xfer->rx_dma, pspim->s_base + SP7021_SLAVE_DMA_ADDR_REG);
  130. if (wait_for_completion_interruptible(&pspim->isr_done)) {
  131. dev_err(&spi->dev, "%s() wait_for_completion err\n", __func__);
  132. return -EINTR;
  133. }
  134. writel(SP7021_SLAVE_SW_RST, pspim->s_base + SP7021_SLAVE_DMA_CTRL_REG);
  135. return 0;
  136. }
  137. static void sp7021_spi_master_rb(struct sp7021_spi_ctlr *pspim, unsigned int len)
  138. {
  139. int i;
  140. for (i = 0; i < len; i++) {
  141. pspim->rx_buf[pspim->rx_cur_len] =
  142. readl(pspim->m_base + SP7021_FIFO_REG);
  143. pspim->rx_cur_len++;
  144. }
  145. }
  146. static void sp7021_spi_master_wb(struct sp7021_spi_ctlr *pspim, unsigned int len)
  147. {
  148. int i;
  149. for (i = 0; i < len; i++) {
  150. writel(pspim->tx_buf[pspim->tx_cur_len],
  151. pspim->m_base + SP7021_FIFO_REG);
  152. pspim->tx_cur_len++;
  153. }
  154. }
  155. static irqreturn_t sp7021_spi_master_irq(int irq, void *dev)
  156. {
  157. struct sp7021_spi_ctlr *pspim = dev;
  158. unsigned int tx_cnt, total_len;
  159. unsigned int tx_len, rx_cnt;
  160. unsigned int fd_status;
  161. bool isrdone = false;
  162. u32 value;
  163. fd_status = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
  164. tx_cnt = FIELD_GET(SP7021_TX_CNT_MASK, fd_status);
  165. tx_len = FIELD_GET(SP7021_TX_LEN_MASK, fd_status);
  166. total_len = FIELD_GET(SP7021_GET_LEN_MASK, fd_status);
  167. if ((fd_status & SP7021_TX_EMP_FLAG) && (fd_status & SP7021_RX_EMP_FLAG) && total_len == 0)
  168. return IRQ_NONE;
  169. if (tx_len == 0 && total_len == 0)
  170. return IRQ_NONE;
  171. rx_cnt = FIELD_GET(SP7021_RX_CNT_MASK, fd_status);
  172. if (fd_status & SP7021_RX_FULL_FLAG)
  173. rx_cnt = pspim->data_unit;
  174. tx_cnt = min(tx_len - pspim->tx_cur_len, pspim->data_unit - tx_cnt);
  175. dev_dbg(pspim->dev, "fd_st=0x%x rx_c:%d tx_c:%d tx_l:%d",
  176. fd_status, rx_cnt, tx_cnt, tx_len);
  177. if (rx_cnt > 0)
  178. sp7021_spi_master_rb(pspim, rx_cnt);
  179. if (tx_cnt > 0)
  180. sp7021_spi_master_wb(pspim, tx_cnt);
  181. fd_status = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
  182. tx_len = FIELD_GET(SP7021_TX_LEN_MASK, fd_status);
  183. total_len = FIELD_GET(SP7021_GET_LEN_MASK, fd_status);
  184. if (fd_status & SP7021_FINISH_FLAG || tx_len == pspim->tx_cur_len) {
  185. while (total_len != pspim->rx_cur_len) {
  186. fd_status = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
  187. total_len = FIELD_GET(SP7021_GET_LEN_MASK, fd_status);
  188. if (fd_status & SP7021_RX_FULL_FLAG)
  189. rx_cnt = pspim->data_unit;
  190. else
  191. rx_cnt = FIELD_GET(SP7021_RX_CNT_MASK, fd_status);
  192. if (rx_cnt > 0)
  193. sp7021_spi_master_rb(pspim, rx_cnt);
  194. }
  195. value = readl(pspim->m_base + SP7021_INT_BUSY_REG);
  196. value |= SP7021_CLR_MASTER_INT;
  197. writel(value, pspim->m_base + SP7021_INT_BUSY_REG);
  198. writel(SP7021_FINISH_FLAG, pspim->m_base + SP7021_SPI_STATUS_REG);
  199. isrdone = true;
  200. }
  201. if (isrdone)
  202. complete(&pspim->isr_done);
  203. return IRQ_HANDLED;
  204. }
  205. static void sp7021_prep_transfer(struct spi_controller *ctlr, struct spi_device *spi)
  206. {
  207. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  208. pspim->tx_cur_len = 0;
  209. pspim->rx_cur_len = 0;
  210. pspim->data_unit = SP7021_FIFO_DATA_LEN;
  211. }
  212. // preliminary set CS, CPOL, CPHA and LSB
  213. static int sp7021_spi_controller_prepare_message(struct spi_controller *ctlr,
  214. struct spi_message *msg)
  215. {
  216. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  217. struct spi_device *s = msg->spi;
  218. u32 valus, rs = 0;
  219. valus = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
  220. valus |= SP7021_FD_SW_RST;
  221. writel(valus, pspim->m_base + SP7021_SPI_STATUS_REG);
  222. rs |= SP7021_FD_SEL;
  223. if (s->mode & SPI_CPOL)
  224. rs |= SP7021_CPOL_FD;
  225. if (s->mode & SPI_LSB_FIRST)
  226. rs |= SP7021_LSB_SEL;
  227. if (s->mode & SPI_CS_HIGH)
  228. rs |= SP7021_CS_POR;
  229. if (s->mode & SPI_CPHA)
  230. rs |= SP7021_CPHA_R;
  231. else
  232. rs |= SP7021_CPHA_W;
  233. rs |= FIELD_PREP(SP7021_TX_UNIT, 0) | FIELD_PREP(SP7021_RX_UNIT, 0);
  234. pspim->xfer_conf = rs;
  235. if (pspim->xfer_conf & SP7021_CPOL_FD)
  236. writel(pspim->xfer_conf, pspim->m_base + SP7021_SPI_CONFIG_REG);
  237. return 0;
  238. }
  239. static void sp7021_spi_setup_clk(struct spi_controller *ctlr, struct spi_transfer *xfer)
  240. {
  241. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  242. u32 clk_rate, clk_sel, div;
  243. clk_rate = clk_get_rate(pspim->spi_clk);
  244. div = max(2U, clk_rate / xfer->speed_hz);
  245. clk_sel = (div / 2) - 1;
  246. pspim->xfer_conf &= ~SP7021_CLK_MASK;
  247. pspim->xfer_conf |= FIELD_PREP(SP7021_CLK_MASK, clk_sel);
  248. writel(pspim->xfer_conf, pspim->m_base + SP7021_SPI_CONFIG_REG);
  249. }
  250. static int sp7021_spi_master_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
  251. struct spi_transfer *xfer)
  252. {
  253. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  254. unsigned long timeout = msecs_to_jiffies(1000);
  255. unsigned int xfer_cnt, xfer_len, last_len;
  256. unsigned int i, len_temp;
  257. u32 reg_temp;
  258. xfer_cnt = xfer->len / SP7021_SPI_DATA_SIZE;
  259. last_len = xfer->len % SP7021_SPI_DATA_SIZE;
  260. for (i = 0; i <= xfer_cnt; i++) {
  261. mutex_lock(&pspim->buf_lock);
  262. sp7021_prep_transfer(ctlr, spi);
  263. sp7021_spi_setup_clk(ctlr, xfer);
  264. reinit_completion(&pspim->isr_done);
  265. if (i == xfer_cnt)
  266. xfer_len = last_len;
  267. else
  268. xfer_len = SP7021_SPI_DATA_SIZE;
  269. pspim->tx_buf = xfer->tx_buf + i * SP7021_SPI_DATA_SIZE;
  270. pspim->rx_buf = xfer->rx_buf + i * SP7021_SPI_DATA_SIZE;
  271. if (pspim->tx_cur_len < xfer_len) {
  272. len_temp = min(pspim->data_unit, xfer_len);
  273. sp7021_spi_master_wb(pspim, len_temp);
  274. }
  275. reg_temp = readl(pspim->m_base + SP7021_SPI_CONFIG_REG);
  276. reg_temp &= ~SP7021_CLEAN_RW_BYTE;
  277. reg_temp &= ~SP7021_CLEAN_FLUG_MASK;
  278. reg_temp |= SP7021_FD_SEL | SP7021_FINISH_FLAG_MASK |
  279. SP7021_TX_EMP_FLAG_MASK | SP7021_RX_FULL_FLAG_MASK |
  280. FIELD_PREP(SP7021_TX_UNIT, 0) | FIELD_PREP(SP7021_RX_UNIT, 0);
  281. writel(reg_temp, pspim->m_base + SP7021_SPI_CONFIG_REG);
  282. reg_temp = FIELD_PREP(SP7021_SET_TX_LEN, xfer_len) |
  283. FIELD_PREP(SP7021_SET_XFER_LEN, xfer_len) |
  284. SP7021_SPI_START_FD;
  285. writel(reg_temp, pspim->m_base + SP7021_SPI_STATUS_REG);
  286. if (!wait_for_completion_interruptible_timeout(&pspim->isr_done, timeout)) {
  287. dev_err(&spi->dev, "wait_for_completion err\n");
  288. mutex_unlock(&pspim->buf_lock);
  289. return -ETIMEDOUT;
  290. }
  291. reg_temp = readl(pspim->m_base + SP7021_SPI_STATUS_REG);
  292. if (reg_temp & SP7021_FINISH_FLAG) {
  293. writel(SP7021_FINISH_FLAG, pspim->m_base + SP7021_SPI_STATUS_REG);
  294. writel(readl(pspim->m_base + SP7021_SPI_CONFIG_REG) &
  295. SP7021_CLEAN_FLUG_MASK, pspim->m_base + SP7021_SPI_CONFIG_REG);
  296. }
  297. if (pspim->xfer_conf & SP7021_CPOL_FD)
  298. writel(pspim->xfer_conf, pspim->m_base + SP7021_SPI_CONFIG_REG);
  299. mutex_unlock(&pspim->buf_lock);
  300. }
  301. return 0;
  302. }
  303. static int sp7021_spi_slave_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
  304. struct spi_transfer *xfer)
  305. {
  306. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  307. struct device *dev = pspim->dev;
  308. int ret;
  309. if (xfer->tx_buf && !xfer->rx_buf) {
  310. xfer->tx_dma = dma_map_single(dev, (void *)xfer->tx_buf,
  311. xfer->len, DMA_TO_DEVICE);
  312. if (dma_mapping_error(dev, xfer->tx_dma))
  313. return -ENOMEM;
  314. ret = sp7021_spi_slave_tx(spi, xfer);
  315. dma_unmap_single(dev, xfer->tx_dma, xfer->len, DMA_TO_DEVICE);
  316. } else if (xfer->rx_buf && !xfer->tx_buf) {
  317. xfer->rx_dma = dma_map_single(dev, xfer->rx_buf, xfer->len,
  318. DMA_FROM_DEVICE);
  319. if (dma_mapping_error(dev, xfer->rx_dma))
  320. return -ENOMEM;
  321. ret = sp7021_spi_slave_rx(spi, xfer);
  322. dma_unmap_single(dev, xfer->rx_dma, xfer->len, DMA_FROM_DEVICE);
  323. } else {
  324. dev_dbg(&ctlr->dev, "%s() wrong command\n", __func__);
  325. return -EINVAL;
  326. }
  327. spi_finalize_current_transfer(ctlr);
  328. return ret;
  329. }
  330. static void sp7021_spi_disable_unprepare(void *data)
  331. {
  332. clk_disable_unprepare(data);
  333. }
  334. static void sp7021_spi_reset_control_assert(void *data)
  335. {
  336. reset_control_assert(data);
  337. }
  338. static int sp7021_spi_controller_probe(struct platform_device *pdev)
  339. {
  340. struct device *dev = &pdev->dev;
  341. struct sp7021_spi_ctlr *pspim;
  342. struct spi_controller *ctlr;
  343. int mode, ret;
  344. pdev->id = of_alias_get_id(pdev->dev.of_node, "sp_spi");
  345. if (device_property_read_bool(dev, "spi-slave"))
  346. mode = SP7021_SLAVE_MODE;
  347. else
  348. mode = SP7021_MASTER_MODE;
  349. if (mode == SP7021_SLAVE_MODE)
  350. ctlr = devm_spi_alloc_slave(dev, sizeof(*pspim));
  351. else
  352. ctlr = devm_spi_alloc_master(dev, sizeof(*pspim));
  353. if (!ctlr)
  354. return -ENOMEM;
  355. device_set_node(&ctlr->dev, dev_fwnode(dev));
  356. ctlr->bus_num = pdev->id;
  357. ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  358. ctlr->auto_runtime_pm = true;
  359. ctlr->prepare_message = sp7021_spi_controller_prepare_message;
  360. if (mode == SP7021_SLAVE_MODE) {
  361. ctlr->transfer_one = sp7021_spi_slave_transfer_one;
  362. ctlr->slave_abort = sp7021_spi_slave_abort;
  363. ctlr->flags = SPI_CONTROLLER_HALF_DUPLEX;
  364. } else {
  365. ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
  366. ctlr->min_speed_hz = 40000;
  367. ctlr->max_speed_hz = 25000000;
  368. ctlr->use_gpio_descriptors = true;
  369. ctlr->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
  370. ctlr->transfer_one = sp7021_spi_master_transfer_one;
  371. }
  372. platform_set_drvdata(pdev, ctlr);
  373. pspim = spi_controller_get_devdata(ctlr);
  374. pspim->mode = mode;
  375. pspim->ctlr = ctlr;
  376. pspim->dev = dev;
  377. mutex_init(&pspim->buf_lock);
  378. init_completion(&pspim->isr_done);
  379. init_completion(&pspim->slave_isr);
  380. pspim->m_base = devm_platform_ioremap_resource_byname(pdev, "master");
  381. if (IS_ERR(pspim->m_base))
  382. return dev_err_probe(dev, PTR_ERR(pspim->m_base), "m_base get fail\n");
  383. pspim->s_base = devm_platform_ioremap_resource_byname(pdev, "slave");
  384. if (IS_ERR(pspim->s_base))
  385. return dev_err_probe(dev, PTR_ERR(pspim->s_base), "s_base get fail\n");
  386. pspim->m_irq = platform_get_irq_byname(pdev, "master_risc");
  387. if (pspim->m_irq < 0)
  388. return pspim->m_irq;
  389. pspim->s_irq = platform_get_irq_byname(pdev, "slave_risc");
  390. if (pspim->s_irq < 0)
  391. return pspim->s_irq;
  392. pspim->spi_clk = devm_clk_get(dev, NULL);
  393. if (IS_ERR(pspim->spi_clk))
  394. return dev_err_probe(dev, PTR_ERR(pspim->spi_clk), "clk get fail\n");
  395. pspim->rstc = devm_reset_control_get_exclusive(dev, NULL);
  396. if (IS_ERR(pspim->rstc))
  397. return dev_err_probe(dev, PTR_ERR(pspim->rstc), "rst get fail\n");
  398. ret = clk_prepare_enable(pspim->spi_clk);
  399. if (ret)
  400. return dev_err_probe(dev, ret, "failed to enable clk\n");
  401. ret = devm_add_action_or_reset(dev, sp7021_spi_disable_unprepare, pspim->spi_clk);
  402. if (ret)
  403. return ret;
  404. ret = reset_control_deassert(pspim->rstc);
  405. if (ret)
  406. return dev_err_probe(dev, ret, "failed to deassert reset\n");
  407. ret = devm_add_action_or_reset(dev, sp7021_spi_reset_control_assert, pspim->rstc);
  408. if (ret)
  409. return ret;
  410. ret = devm_request_irq(dev, pspim->m_irq, sp7021_spi_master_irq,
  411. IRQF_TRIGGER_RISING, pdev->name, pspim);
  412. if (ret)
  413. return ret;
  414. ret = devm_request_irq(dev, pspim->s_irq, sp7021_spi_slave_irq,
  415. IRQF_TRIGGER_RISING, pdev->name, pspim);
  416. if (ret)
  417. return ret;
  418. pm_runtime_enable(dev);
  419. ret = spi_register_controller(ctlr);
  420. if (ret) {
  421. pm_runtime_disable(dev);
  422. return dev_err_probe(dev, ret, "spi_register_master fail\n");
  423. }
  424. return 0;
  425. }
  426. static int sp7021_spi_controller_remove(struct platform_device *pdev)
  427. {
  428. struct spi_controller *ctlr = dev_get_drvdata(&pdev->dev);
  429. spi_unregister_controller(ctlr);
  430. pm_runtime_disable(&pdev->dev);
  431. pm_runtime_set_suspended(&pdev->dev);
  432. return 0;
  433. }
  434. static int __maybe_unused sp7021_spi_controller_suspend(struct device *dev)
  435. {
  436. struct spi_controller *ctlr = dev_get_drvdata(dev);
  437. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  438. return reset_control_assert(pspim->rstc);
  439. }
  440. static int __maybe_unused sp7021_spi_controller_resume(struct device *dev)
  441. {
  442. struct spi_controller *ctlr = dev_get_drvdata(dev);
  443. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  444. reset_control_deassert(pspim->rstc);
  445. return clk_prepare_enable(pspim->spi_clk);
  446. }
  447. #ifdef CONFIG_PM
  448. static int sp7021_spi_runtime_suspend(struct device *dev)
  449. {
  450. struct spi_controller *ctlr = dev_get_drvdata(dev);
  451. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  452. return reset_control_assert(pspim->rstc);
  453. }
  454. static int sp7021_spi_runtime_resume(struct device *dev)
  455. {
  456. struct spi_controller *ctlr = dev_get_drvdata(dev);
  457. struct sp7021_spi_ctlr *pspim = spi_master_get_devdata(ctlr);
  458. return reset_control_deassert(pspim->rstc);
  459. }
  460. #endif
  461. static const struct dev_pm_ops sp7021_spi_pm_ops = {
  462. SET_RUNTIME_PM_OPS(sp7021_spi_runtime_suspend,
  463. sp7021_spi_runtime_resume, NULL)
  464. SET_SYSTEM_SLEEP_PM_OPS(sp7021_spi_controller_suspend,
  465. sp7021_spi_controller_resume)
  466. };
  467. static const struct of_device_id sp7021_spi_controller_ids[] = {
  468. { .compatible = "sunplus,sp7021-spi" },
  469. {}
  470. };
  471. MODULE_DEVICE_TABLE(of, sp7021_spi_controller_ids);
  472. static struct platform_driver sp7021_spi_controller_driver = {
  473. .probe = sp7021_spi_controller_probe,
  474. .remove = sp7021_spi_controller_remove,
  475. .driver = {
  476. .name = "sunplus,sp7021-spi-controller",
  477. .of_match_table = sp7021_spi_controller_ids,
  478. .pm = &sp7021_spi_pm_ops,
  479. },
  480. };
  481. module_platform_driver(sp7021_spi_controller_driver);
  482. MODULE_AUTHOR("Li-hao Kuo <[email protected]>");
  483. MODULE_DESCRIPTION("Sunplus SPI controller driver");
  484. MODULE_LICENSE("GPL");