spi-mt65xx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Author: Leilk Liu <leilk.liu@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  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. #include <linux/clk.h>
  15. #include <linux/device.h>
  16. #include <linux/err.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/ioport.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/platform_data/spi-mt65xx.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/spi/spi.h>
  27. #define SPI_CFG0_REG 0x0000
  28. #define SPI_CFG1_REG 0x0004
  29. #define SPI_TX_SRC_REG 0x0008
  30. #define SPI_RX_DST_REG 0x000c
  31. #define SPI_TX_DATA_REG 0x0010
  32. #define SPI_RX_DATA_REG 0x0014
  33. #define SPI_CMD_REG 0x0018
  34. #define SPI_STATUS0_REG 0x001c
  35. #define SPI_PAD_SEL_REG 0x0024
  36. #define SPI_CFG0_SCK_HIGH_OFFSET 0
  37. #define SPI_CFG0_SCK_LOW_OFFSET 8
  38. #define SPI_CFG0_CS_HOLD_OFFSET 16
  39. #define SPI_CFG0_CS_SETUP_OFFSET 24
  40. #define SPI_CFG1_CS_IDLE_OFFSET 0
  41. #define SPI_CFG1_PACKET_LOOP_OFFSET 8
  42. #define SPI_CFG1_PACKET_LENGTH_OFFSET 16
  43. #define SPI_CFG1_GET_TICK_DLY_OFFSET 30
  44. #define SPI_CFG1_CS_IDLE_MASK 0xff
  45. #define SPI_CFG1_PACKET_LOOP_MASK 0xff00
  46. #define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000
  47. #define SPI_CMD_ACT BIT(0)
  48. #define SPI_CMD_RESUME BIT(1)
  49. #define SPI_CMD_RST BIT(2)
  50. #define SPI_CMD_PAUSE_EN BIT(4)
  51. #define SPI_CMD_DEASSERT BIT(5)
  52. #define SPI_CMD_CPHA BIT(8)
  53. #define SPI_CMD_CPOL BIT(9)
  54. #define SPI_CMD_RX_DMA BIT(10)
  55. #define SPI_CMD_TX_DMA BIT(11)
  56. #define SPI_CMD_TXMSBF BIT(12)
  57. #define SPI_CMD_RXMSBF BIT(13)
  58. #define SPI_CMD_RX_ENDIAN BIT(14)
  59. #define SPI_CMD_TX_ENDIAN BIT(15)
  60. #define SPI_CMD_FINISH_IE BIT(16)
  61. #define SPI_CMD_PAUSE_IE BIT(17)
  62. #define MT8173_SPI_MAX_PAD_SEL 3
  63. #define MTK_SPI_PAUSE_INT_STATUS 0x2
  64. #define MTK_SPI_IDLE 0
  65. #define MTK_SPI_PAUSED 1
  66. #define MTK_SPI_MAX_FIFO_SIZE 32
  67. #define MTK_SPI_PACKET_SIZE 1024
  68. struct mtk_spi_compatible {
  69. bool need_pad_sel;
  70. /* Must explicitly send dummy Tx bytes to do Rx only transfer */
  71. bool must_tx;
  72. };
  73. struct mtk_spi {
  74. void __iomem *base;
  75. u32 state;
  76. int pad_num;
  77. u32 *pad_sel;
  78. struct clk *parent_clk, *sel_clk, *spi_clk;
  79. struct spi_transfer *cur_transfer;
  80. u32 xfer_len;
  81. struct scatterlist *tx_sgl, *rx_sgl;
  82. u32 tx_sgl_len, rx_sgl_len;
  83. const struct mtk_spi_compatible *dev_comp;
  84. };
  85. static const struct mtk_spi_compatible mtk_common_compat;
  86. static const struct mtk_spi_compatible mt8173_compat = {
  87. .need_pad_sel = true,
  88. .must_tx = true,
  89. };
  90. /*
  91. * A piece of default chip info unless the platform
  92. * supplies it.
  93. */
  94. static const struct mtk_chip_config mtk_default_chip_info = {
  95. .rx_mlsb = 1,
  96. .tx_mlsb = 1,
  97. };
  98. static const struct of_device_id mtk_spi_of_match[] = {
  99. { .compatible = "mediatek,mt2701-spi",
  100. .data = (void *)&mtk_common_compat,
  101. },
  102. { .compatible = "mediatek,mt6589-spi",
  103. .data = (void *)&mtk_common_compat,
  104. },
  105. { .compatible = "mediatek,mt8135-spi",
  106. .data = (void *)&mtk_common_compat,
  107. },
  108. { .compatible = "mediatek,mt8173-spi",
  109. .data = (void *)&mt8173_compat,
  110. },
  111. {}
  112. };
  113. MODULE_DEVICE_TABLE(of, mtk_spi_of_match);
  114. static void mtk_spi_reset(struct mtk_spi *mdata)
  115. {
  116. u32 reg_val;
  117. /* set the software reset bit in SPI_CMD_REG. */
  118. reg_val = readl(mdata->base + SPI_CMD_REG);
  119. reg_val |= SPI_CMD_RST;
  120. writel(reg_val, mdata->base + SPI_CMD_REG);
  121. reg_val = readl(mdata->base + SPI_CMD_REG);
  122. reg_val &= ~SPI_CMD_RST;
  123. writel(reg_val, mdata->base + SPI_CMD_REG);
  124. }
  125. static int mtk_spi_prepare_message(struct spi_master *master,
  126. struct spi_message *msg)
  127. {
  128. u16 cpha, cpol;
  129. u32 reg_val;
  130. struct spi_device *spi = msg->spi;
  131. struct mtk_chip_config *chip_config = spi->controller_data;
  132. struct mtk_spi *mdata = spi_master_get_devdata(master);
  133. cpha = spi->mode & SPI_CPHA ? 1 : 0;
  134. cpol = spi->mode & SPI_CPOL ? 1 : 0;
  135. reg_val = readl(mdata->base + SPI_CMD_REG);
  136. if (cpha)
  137. reg_val |= SPI_CMD_CPHA;
  138. else
  139. reg_val &= ~SPI_CMD_CPHA;
  140. if (cpol)
  141. reg_val |= SPI_CMD_CPOL;
  142. else
  143. reg_val &= ~SPI_CMD_CPOL;
  144. /* set the mlsbx and mlsbtx */
  145. if (chip_config->tx_mlsb)
  146. reg_val |= SPI_CMD_TXMSBF;
  147. else
  148. reg_val &= ~SPI_CMD_TXMSBF;
  149. if (chip_config->rx_mlsb)
  150. reg_val |= SPI_CMD_RXMSBF;
  151. else
  152. reg_val &= ~SPI_CMD_RXMSBF;
  153. /* set the tx/rx endian */
  154. #ifdef __LITTLE_ENDIAN
  155. reg_val &= ~SPI_CMD_TX_ENDIAN;
  156. reg_val &= ~SPI_CMD_RX_ENDIAN;
  157. #else
  158. reg_val |= SPI_CMD_TX_ENDIAN;
  159. reg_val |= SPI_CMD_RX_ENDIAN;
  160. #endif
  161. /* set finish and pause interrupt always enable */
  162. reg_val |= SPI_CMD_FINISH_IE | SPI_CMD_PAUSE_IE;
  163. /* disable dma mode */
  164. reg_val &= ~(SPI_CMD_TX_DMA | SPI_CMD_RX_DMA);
  165. /* disable deassert mode */
  166. reg_val &= ~SPI_CMD_DEASSERT;
  167. writel(reg_val, mdata->base + SPI_CMD_REG);
  168. /* pad select */
  169. if (mdata->dev_comp->need_pad_sel)
  170. writel(mdata->pad_sel[spi->chip_select],
  171. mdata->base + SPI_PAD_SEL_REG);
  172. return 0;
  173. }
  174. static void mtk_spi_set_cs(struct spi_device *spi, bool enable)
  175. {
  176. u32 reg_val;
  177. struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
  178. reg_val = readl(mdata->base + SPI_CMD_REG);
  179. if (!enable) {
  180. reg_val |= SPI_CMD_PAUSE_EN;
  181. writel(reg_val, mdata->base + SPI_CMD_REG);
  182. } else {
  183. reg_val &= ~SPI_CMD_PAUSE_EN;
  184. writel(reg_val, mdata->base + SPI_CMD_REG);
  185. mdata->state = MTK_SPI_IDLE;
  186. mtk_spi_reset(mdata);
  187. }
  188. }
  189. static void mtk_spi_prepare_transfer(struct spi_master *master,
  190. struct spi_transfer *xfer)
  191. {
  192. u32 spi_clk_hz, div, sck_time, cs_time, reg_val = 0;
  193. struct mtk_spi *mdata = spi_master_get_devdata(master);
  194. spi_clk_hz = clk_get_rate(mdata->spi_clk);
  195. if (xfer->speed_hz < spi_clk_hz / 2)
  196. div = DIV_ROUND_UP(spi_clk_hz, xfer->speed_hz);
  197. else
  198. div = 1;
  199. sck_time = (div + 1) / 2;
  200. cs_time = sck_time * 2;
  201. reg_val |= (((sck_time - 1) & 0xff) << SPI_CFG0_SCK_HIGH_OFFSET);
  202. reg_val |= (((sck_time - 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET);
  203. reg_val |= (((cs_time - 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET);
  204. reg_val |= (((cs_time - 1) & 0xff) << SPI_CFG0_CS_SETUP_OFFSET);
  205. writel(reg_val, mdata->base + SPI_CFG0_REG);
  206. reg_val = readl(mdata->base + SPI_CFG1_REG);
  207. reg_val &= ~SPI_CFG1_CS_IDLE_MASK;
  208. reg_val |= (((cs_time - 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET);
  209. writel(reg_val, mdata->base + SPI_CFG1_REG);
  210. }
  211. static void mtk_spi_setup_packet(struct spi_master *master)
  212. {
  213. u32 packet_size, packet_loop, reg_val;
  214. struct mtk_spi *mdata = spi_master_get_devdata(master);
  215. packet_size = min_t(u32, mdata->xfer_len, MTK_SPI_PACKET_SIZE);
  216. packet_loop = mdata->xfer_len / packet_size;
  217. reg_val = readl(mdata->base + SPI_CFG1_REG);
  218. reg_val &= ~(SPI_CFG1_PACKET_LENGTH_MASK | SPI_CFG1_PACKET_LOOP_MASK);
  219. reg_val |= (packet_size - 1) << SPI_CFG1_PACKET_LENGTH_OFFSET;
  220. reg_val |= (packet_loop - 1) << SPI_CFG1_PACKET_LOOP_OFFSET;
  221. writel(reg_val, mdata->base + SPI_CFG1_REG);
  222. }
  223. static void mtk_spi_enable_transfer(struct spi_master *master)
  224. {
  225. u32 cmd;
  226. struct mtk_spi *mdata = spi_master_get_devdata(master);
  227. cmd = readl(mdata->base + SPI_CMD_REG);
  228. if (mdata->state == MTK_SPI_IDLE)
  229. cmd |= SPI_CMD_ACT;
  230. else
  231. cmd |= SPI_CMD_RESUME;
  232. writel(cmd, mdata->base + SPI_CMD_REG);
  233. }
  234. static int mtk_spi_get_mult_delta(u32 xfer_len)
  235. {
  236. u32 mult_delta;
  237. if (xfer_len > MTK_SPI_PACKET_SIZE)
  238. mult_delta = xfer_len % MTK_SPI_PACKET_SIZE;
  239. else
  240. mult_delta = 0;
  241. return mult_delta;
  242. }
  243. static void mtk_spi_update_mdata_len(struct spi_master *master)
  244. {
  245. int mult_delta;
  246. struct mtk_spi *mdata = spi_master_get_devdata(master);
  247. if (mdata->tx_sgl_len && mdata->rx_sgl_len) {
  248. if (mdata->tx_sgl_len > mdata->rx_sgl_len) {
  249. mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
  250. mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
  251. mdata->rx_sgl_len = mult_delta;
  252. mdata->tx_sgl_len -= mdata->xfer_len;
  253. } else {
  254. mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
  255. mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
  256. mdata->tx_sgl_len = mult_delta;
  257. mdata->rx_sgl_len -= mdata->xfer_len;
  258. }
  259. } else if (mdata->tx_sgl_len) {
  260. mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
  261. mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
  262. mdata->tx_sgl_len = mult_delta;
  263. } else if (mdata->rx_sgl_len) {
  264. mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
  265. mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
  266. mdata->rx_sgl_len = mult_delta;
  267. }
  268. }
  269. static void mtk_spi_setup_dma_addr(struct spi_master *master,
  270. struct spi_transfer *xfer)
  271. {
  272. struct mtk_spi *mdata = spi_master_get_devdata(master);
  273. if (mdata->tx_sgl)
  274. writel(xfer->tx_dma, mdata->base + SPI_TX_SRC_REG);
  275. if (mdata->rx_sgl)
  276. writel(xfer->rx_dma, mdata->base + SPI_RX_DST_REG);
  277. }
  278. static int mtk_spi_fifo_transfer(struct spi_master *master,
  279. struct spi_device *spi,
  280. struct spi_transfer *xfer)
  281. {
  282. int cnt, remainder;
  283. u32 reg_val;
  284. struct mtk_spi *mdata = spi_master_get_devdata(master);
  285. mdata->cur_transfer = xfer;
  286. mdata->xfer_len = xfer->len;
  287. mtk_spi_prepare_transfer(master, xfer);
  288. mtk_spi_setup_packet(master);
  289. cnt = xfer->len / 4;
  290. iowrite32_rep(mdata->base + SPI_TX_DATA_REG, xfer->tx_buf, cnt);
  291. remainder = xfer->len % 4;
  292. if (remainder > 0) {
  293. reg_val = 0;
  294. memcpy(&reg_val, xfer->tx_buf + (cnt * 4), remainder);
  295. writel(reg_val, mdata->base + SPI_TX_DATA_REG);
  296. }
  297. mtk_spi_enable_transfer(master);
  298. return 1;
  299. }
  300. static int mtk_spi_dma_transfer(struct spi_master *master,
  301. struct spi_device *spi,
  302. struct spi_transfer *xfer)
  303. {
  304. int cmd;
  305. struct mtk_spi *mdata = spi_master_get_devdata(master);
  306. mdata->tx_sgl = NULL;
  307. mdata->rx_sgl = NULL;
  308. mdata->tx_sgl_len = 0;
  309. mdata->rx_sgl_len = 0;
  310. mdata->cur_transfer = xfer;
  311. mtk_spi_prepare_transfer(master, xfer);
  312. cmd = readl(mdata->base + SPI_CMD_REG);
  313. if (xfer->tx_buf)
  314. cmd |= SPI_CMD_TX_DMA;
  315. if (xfer->rx_buf)
  316. cmd |= SPI_CMD_RX_DMA;
  317. writel(cmd, mdata->base + SPI_CMD_REG);
  318. if (xfer->tx_buf)
  319. mdata->tx_sgl = xfer->tx_sg.sgl;
  320. if (xfer->rx_buf)
  321. mdata->rx_sgl = xfer->rx_sg.sgl;
  322. if (mdata->tx_sgl) {
  323. xfer->tx_dma = sg_dma_address(mdata->tx_sgl);
  324. mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
  325. }
  326. if (mdata->rx_sgl) {
  327. xfer->rx_dma = sg_dma_address(mdata->rx_sgl);
  328. mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
  329. }
  330. mtk_spi_update_mdata_len(master);
  331. mtk_spi_setup_packet(master);
  332. mtk_spi_setup_dma_addr(master, xfer);
  333. mtk_spi_enable_transfer(master);
  334. return 1;
  335. }
  336. static int mtk_spi_transfer_one(struct spi_master *master,
  337. struct spi_device *spi,
  338. struct spi_transfer *xfer)
  339. {
  340. if (master->can_dma(master, spi, xfer))
  341. return mtk_spi_dma_transfer(master, spi, xfer);
  342. else
  343. return mtk_spi_fifo_transfer(master, spi, xfer);
  344. }
  345. static bool mtk_spi_can_dma(struct spi_master *master,
  346. struct spi_device *spi,
  347. struct spi_transfer *xfer)
  348. {
  349. return xfer->len > MTK_SPI_MAX_FIFO_SIZE;
  350. }
  351. static int mtk_spi_setup(struct spi_device *spi)
  352. {
  353. struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
  354. if (!spi->controller_data)
  355. spi->controller_data = (void *)&mtk_default_chip_info;
  356. if (mdata->dev_comp->need_pad_sel && gpio_is_valid(spi->cs_gpio))
  357. gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  358. return 0;
  359. }
  360. static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id)
  361. {
  362. u32 cmd, reg_val, cnt, remainder;
  363. struct spi_master *master = dev_id;
  364. struct mtk_spi *mdata = spi_master_get_devdata(master);
  365. struct spi_transfer *trans = mdata->cur_transfer;
  366. reg_val = readl(mdata->base + SPI_STATUS0_REG);
  367. if (reg_val & MTK_SPI_PAUSE_INT_STATUS)
  368. mdata->state = MTK_SPI_PAUSED;
  369. else
  370. mdata->state = MTK_SPI_IDLE;
  371. if (!master->can_dma(master, master->cur_msg->spi, trans)) {
  372. if (trans->rx_buf) {
  373. cnt = mdata->xfer_len / 4;
  374. ioread32_rep(mdata->base + SPI_RX_DATA_REG,
  375. trans->rx_buf, cnt);
  376. remainder = mdata->xfer_len % 4;
  377. if (remainder > 0) {
  378. reg_val = readl(mdata->base + SPI_RX_DATA_REG);
  379. memcpy(trans->rx_buf + (cnt * 4),
  380. &reg_val, remainder);
  381. }
  382. }
  383. spi_finalize_current_transfer(master);
  384. return IRQ_HANDLED;
  385. }
  386. if (mdata->tx_sgl)
  387. trans->tx_dma += mdata->xfer_len;
  388. if (mdata->rx_sgl)
  389. trans->rx_dma += mdata->xfer_len;
  390. if (mdata->tx_sgl && (mdata->tx_sgl_len == 0)) {
  391. mdata->tx_sgl = sg_next(mdata->tx_sgl);
  392. if (mdata->tx_sgl) {
  393. trans->tx_dma = sg_dma_address(mdata->tx_sgl);
  394. mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
  395. }
  396. }
  397. if (mdata->rx_sgl && (mdata->rx_sgl_len == 0)) {
  398. mdata->rx_sgl = sg_next(mdata->rx_sgl);
  399. if (mdata->rx_sgl) {
  400. trans->rx_dma = sg_dma_address(mdata->rx_sgl);
  401. mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
  402. }
  403. }
  404. if (!mdata->tx_sgl && !mdata->rx_sgl) {
  405. /* spi disable dma */
  406. cmd = readl(mdata->base + SPI_CMD_REG);
  407. cmd &= ~SPI_CMD_TX_DMA;
  408. cmd &= ~SPI_CMD_RX_DMA;
  409. writel(cmd, mdata->base + SPI_CMD_REG);
  410. spi_finalize_current_transfer(master);
  411. return IRQ_HANDLED;
  412. }
  413. mtk_spi_update_mdata_len(master);
  414. mtk_spi_setup_packet(master);
  415. mtk_spi_setup_dma_addr(master, trans);
  416. mtk_spi_enable_transfer(master);
  417. return IRQ_HANDLED;
  418. }
  419. static int mtk_spi_probe(struct platform_device *pdev)
  420. {
  421. struct spi_master *master;
  422. struct mtk_spi *mdata;
  423. const struct of_device_id *of_id;
  424. struct resource *res;
  425. int i, irq, ret;
  426. master = spi_alloc_master(&pdev->dev, sizeof(*mdata));
  427. if (!master) {
  428. dev_err(&pdev->dev, "failed to alloc spi master\n");
  429. return -ENOMEM;
  430. }
  431. master->auto_runtime_pm = true;
  432. master->dev.of_node = pdev->dev.of_node;
  433. master->mode_bits = SPI_CPOL | SPI_CPHA;
  434. master->set_cs = mtk_spi_set_cs;
  435. master->prepare_message = mtk_spi_prepare_message;
  436. master->transfer_one = mtk_spi_transfer_one;
  437. master->can_dma = mtk_spi_can_dma;
  438. master->setup = mtk_spi_setup;
  439. of_id = of_match_node(mtk_spi_of_match, pdev->dev.of_node);
  440. if (!of_id) {
  441. dev_err(&pdev->dev, "failed to probe of_node\n");
  442. ret = -EINVAL;
  443. goto err_put_master;
  444. }
  445. mdata = spi_master_get_devdata(master);
  446. mdata->dev_comp = of_id->data;
  447. if (mdata->dev_comp->must_tx)
  448. master->flags = SPI_MASTER_MUST_TX;
  449. if (mdata->dev_comp->need_pad_sel) {
  450. mdata->pad_num = of_property_count_u32_elems(
  451. pdev->dev.of_node,
  452. "mediatek,pad-select");
  453. if (mdata->pad_num < 0) {
  454. dev_err(&pdev->dev,
  455. "No 'mediatek,pad-select' property\n");
  456. ret = -EINVAL;
  457. goto err_put_master;
  458. }
  459. mdata->pad_sel = devm_kmalloc_array(&pdev->dev, mdata->pad_num,
  460. sizeof(u32), GFP_KERNEL);
  461. if (!mdata->pad_sel) {
  462. ret = -ENOMEM;
  463. goto err_put_master;
  464. }
  465. for (i = 0; i < mdata->pad_num; i++) {
  466. of_property_read_u32_index(pdev->dev.of_node,
  467. "mediatek,pad-select",
  468. i, &mdata->pad_sel[i]);
  469. if (mdata->pad_sel[i] > MT8173_SPI_MAX_PAD_SEL) {
  470. dev_err(&pdev->dev, "wrong pad-sel[%d]: %u\n",
  471. i, mdata->pad_sel[i]);
  472. ret = -EINVAL;
  473. goto err_put_master;
  474. }
  475. }
  476. }
  477. platform_set_drvdata(pdev, master);
  478. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  479. if (!res) {
  480. ret = -ENODEV;
  481. dev_err(&pdev->dev, "failed to determine base address\n");
  482. goto err_put_master;
  483. }
  484. mdata->base = devm_ioremap_resource(&pdev->dev, res);
  485. if (IS_ERR(mdata->base)) {
  486. ret = PTR_ERR(mdata->base);
  487. goto err_put_master;
  488. }
  489. irq = platform_get_irq(pdev, 0);
  490. if (irq < 0) {
  491. dev_err(&pdev->dev, "failed to get irq (%d)\n", irq);
  492. ret = irq;
  493. goto err_put_master;
  494. }
  495. if (!pdev->dev.dma_mask)
  496. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  497. ret = devm_request_irq(&pdev->dev, irq, mtk_spi_interrupt,
  498. IRQF_TRIGGER_NONE, dev_name(&pdev->dev), master);
  499. if (ret) {
  500. dev_err(&pdev->dev, "failed to register irq (%d)\n", ret);
  501. goto err_put_master;
  502. }
  503. mdata->parent_clk = devm_clk_get(&pdev->dev, "parent-clk");
  504. if (IS_ERR(mdata->parent_clk)) {
  505. ret = PTR_ERR(mdata->parent_clk);
  506. dev_err(&pdev->dev, "failed to get parent-clk: %d\n", ret);
  507. goto err_put_master;
  508. }
  509. mdata->sel_clk = devm_clk_get(&pdev->dev, "sel-clk");
  510. if (IS_ERR(mdata->sel_clk)) {
  511. ret = PTR_ERR(mdata->sel_clk);
  512. dev_err(&pdev->dev, "failed to get sel-clk: %d\n", ret);
  513. goto err_put_master;
  514. }
  515. mdata->spi_clk = devm_clk_get(&pdev->dev, "spi-clk");
  516. if (IS_ERR(mdata->spi_clk)) {
  517. ret = PTR_ERR(mdata->spi_clk);
  518. dev_err(&pdev->dev, "failed to get spi-clk: %d\n", ret);
  519. goto err_put_master;
  520. }
  521. ret = clk_prepare_enable(mdata->spi_clk);
  522. if (ret < 0) {
  523. dev_err(&pdev->dev, "failed to enable spi_clk (%d)\n", ret);
  524. goto err_put_master;
  525. }
  526. ret = clk_set_parent(mdata->sel_clk, mdata->parent_clk);
  527. if (ret < 0) {
  528. dev_err(&pdev->dev, "failed to clk_set_parent (%d)\n", ret);
  529. clk_disable_unprepare(mdata->spi_clk);
  530. goto err_put_master;
  531. }
  532. clk_disable_unprepare(mdata->spi_clk);
  533. pm_runtime_enable(&pdev->dev);
  534. ret = devm_spi_register_master(&pdev->dev, master);
  535. if (ret) {
  536. dev_err(&pdev->dev, "failed to register master (%d)\n", ret);
  537. goto err_disable_runtime_pm;
  538. }
  539. if (mdata->dev_comp->need_pad_sel) {
  540. if (mdata->pad_num != master->num_chipselect) {
  541. dev_err(&pdev->dev,
  542. "pad_num does not match num_chipselect(%d != %d)\n",
  543. mdata->pad_num, master->num_chipselect);
  544. ret = -EINVAL;
  545. goto err_disable_runtime_pm;
  546. }
  547. if (!master->cs_gpios && master->num_chipselect > 1) {
  548. dev_err(&pdev->dev,
  549. "cs_gpios not specified and num_chipselect > 1\n");
  550. ret = -EINVAL;
  551. goto err_disable_runtime_pm;
  552. }
  553. if (master->cs_gpios) {
  554. for (i = 0; i < master->num_chipselect; i++) {
  555. ret = devm_gpio_request(&pdev->dev,
  556. master->cs_gpios[i],
  557. dev_name(&pdev->dev));
  558. if (ret) {
  559. dev_err(&pdev->dev,
  560. "can't get CS GPIO %i\n", i);
  561. goto err_disable_runtime_pm;
  562. }
  563. }
  564. }
  565. }
  566. return 0;
  567. err_disable_runtime_pm:
  568. pm_runtime_disable(&pdev->dev);
  569. err_put_master:
  570. spi_master_put(master);
  571. return ret;
  572. }
  573. static int mtk_spi_remove(struct platform_device *pdev)
  574. {
  575. struct spi_master *master = platform_get_drvdata(pdev);
  576. struct mtk_spi *mdata = spi_master_get_devdata(master);
  577. pm_runtime_disable(&pdev->dev);
  578. mtk_spi_reset(mdata);
  579. return 0;
  580. }
  581. #ifdef CONFIG_PM_SLEEP
  582. static int mtk_spi_suspend(struct device *dev)
  583. {
  584. int ret;
  585. struct spi_master *master = dev_get_drvdata(dev);
  586. struct mtk_spi *mdata = spi_master_get_devdata(master);
  587. ret = spi_master_suspend(master);
  588. if (ret)
  589. return ret;
  590. if (!pm_runtime_suspended(dev))
  591. clk_disable_unprepare(mdata->spi_clk);
  592. return ret;
  593. }
  594. static int mtk_spi_resume(struct device *dev)
  595. {
  596. int ret;
  597. struct spi_master *master = dev_get_drvdata(dev);
  598. struct mtk_spi *mdata = spi_master_get_devdata(master);
  599. if (!pm_runtime_suspended(dev)) {
  600. ret = clk_prepare_enable(mdata->spi_clk);
  601. if (ret < 0) {
  602. dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
  603. return ret;
  604. }
  605. }
  606. ret = spi_master_resume(master);
  607. if (ret < 0)
  608. clk_disable_unprepare(mdata->spi_clk);
  609. return ret;
  610. }
  611. #endif /* CONFIG_PM_SLEEP */
  612. #ifdef CONFIG_PM
  613. static int mtk_spi_runtime_suspend(struct device *dev)
  614. {
  615. struct spi_master *master = dev_get_drvdata(dev);
  616. struct mtk_spi *mdata = spi_master_get_devdata(master);
  617. clk_disable_unprepare(mdata->spi_clk);
  618. return 0;
  619. }
  620. static int mtk_spi_runtime_resume(struct device *dev)
  621. {
  622. struct spi_master *master = dev_get_drvdata(dev);
  623. struct mtk_spi *mdata = spi_master_get_devdata(master);
  624. int ret;
  625. ret = clk_prepare_enable(mdata->spi_clk);
  626. if (ret < 0) {
  627. dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
  628. return ret;
  629. }
  630. return 0;
  631. }
  632. #endif /* CONFIG_PM */
  633. static const struct dev_pm_ops mtk_spi_pm = {
  634. SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_suspend, mtk_spi_resume)
  635. SET_RUNTIME_PM_OPS(mtk_spi_runtime_suspend,
  636. mtk_spi_runtime_resume, NULL)
  637. };
  638. static struct platform_driver mtk_spi_driver = {
  639. .driver = {
  640. .name = "mtk-spi",
  641. .pm = &mtk_spi_pm,
  642. .of_match_table = mtk_spi_of_match,
  643. },
  644. .probe = mtk_spi_probe,
  645. .remove = mtk_spi_remove,
  646. };
  647. module_platform_driver(mtk_spi_driver);
  648. MODULE_DESCRIPTION("MTK SPI Controller driver");
  649. MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
  650. MODULE_LICENSE("GPL v2");
  651. MODULE_ALIAS("platform:mtk-spi");