spi-mt65xx.c 20 KB

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