spi-sun6i.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * Copyright (C) 2012 - 2014 Allwinner Tech
  3. * Pan Nan <pannan@allwinnertech.com>
  4. *
  5. * Copyright (C) 2014 Maxime Ripard
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/reset.h>
  23. #include <linux/spi/spi.h>
  24. #define SUN6I_FIFO_DEPTH 128
  25. #define SUN8I_FIFO_DEPTH 64
  26. #define SUN6I_GBL_CTL_REG 0x04
  27. #define SUN6I_GBL_CTL_BUS_ENABLE BIT(0)
  28. #define SUN6I_GBL_CTL_MASTER BIT(1)
  29. #define SUN6I_GBL_CTL_TP BIT(7)
  30. #define SUN6I_GBL_CTL_RST BIT(31)
  31. #define SUN6I_TFR_CTL_REG 0x08
  32. #define SUN6I_TFR_CTL_CPHA BIT(0)
  33. #define SUN6I_TFR_CTL_CPOL BIT(1)
  34. #define SUN6I_TFR_CTL_SPOL BIT(2)
  35. #define SUN6I_TFR_CTL_CS_MASK 0x30
  36. #define SUN6I_TFR_CTL_CS(cs) (((cs) << 4) & SUN6I_TFR_CTL_CS_MASK)
  37. #define SUN6I_TFR_CTL_CS_MANUAL BIT(6)
  38. #define SUN6I_TFR_CTL_CS_LEVEL BIT(7)
  39. #define SUN6I_TFR_CTL_DHB BIT(8)
  40. #define SUN6I_TFR_CTL_FBS BIT(12)
  41. #define SUN6I_TFR_CTL_XCH BIT(31)
  42. #define SUN6I_INT_CTL_REG 0x10
  43. #define SUN6I_INT_CTL_RF_RDY BIT(0)
  44. #define SUN6I_INT_CTL_TF_ERQ BIT(4)
  45. #define SUN6I_INT_CTL_RF_OVF BIT(8)
  46. #define SUN6I_INT_CTL_TC BIT(12)
  47. #define SUN6I_INT_STA_REG 0x14
  48. #define SUN6I_FIFO_CTL_REG 0x18
  49. #define SUN6I_FIFO_CTL_RF_RDY_TRIG_LEVEL_MASK 0xff
  50. #define SUN6I_FIFO_CTL_RF_RDY_TRIG_LEVEL_BITS 0
  51. #define SUN6I_FIFO_CTL_RF_RST BIT(15)
  52. #define SUN6I_FIFO_CTL_TF_ERQ_TRIG_LEVEL_MASK 0xff
  53. #define SUN6I_FIFO_CTL_TF_ERQ_TRIG_LEVEL_BITS 16
  54. #define SUN6I_FIFO_CTL_TF_RST BIT(31)
  55. #define SUN6I_FIFO_STA_REG 0x1c
  56. #define SUN6I_FIFO_STA_RF_CNT_MASK 0x7f
  57. #define SUN6I_FIFO_STA_RF_CNT_BITS 0
  58. #define SUN6I_FIFO_STA_TF_CNT_MASK 0x7f
  59. #define SUN6I_FIFO_STA_TF_CNT_BITS 16
  60. #define SUN6I_CLK_CTL_REG 0x24
  61. #define SUN6I_CLK_CTL_CDR2_MASK 0xff
  62. #define SUN6I_CLK_CTL_CDR2(div) (((div) & SUN6I_CLK_CTL_CDR2_MASK) << 0)
  63. #define SUN6I_CLK_CTL_CDR1_MASK 0xf
  64. #define SUN6I_CLK_CTL_CDR1(div) (((div) & SUN6I_CLK_CTL_CDR1_MASK) << 8)
  65. #define SUN6I_CLK_CTL_DRS BIT(12)
  66. #define SUN6I_MAX_XFER_SIZE 0xffffff
  67. #define SUN6I_BURST_CNT_REG 0x30
  68. #define SUN6I_BURST_CNT(cnt) ((cnt) & SUN6I_MAX_XFER_SIZE)
  69. #define SUN6I_XMIT_CNT_REG 0x34
  70. #define SUN6I_XMIT_CNT(cnt) ((cnt) & SUN6I_MAX_XFER_SIZE)
  71. #define SUN6I_BURST_CTL_CNT_REG 0x38
  72. #define SUN6I_BURST_CTL_CNT_STC(cnt) ((cnt) & SUN6I_MAX_XFER_SIZE)
  73. #define SUN6I_TXDATA_REG 0x200
  74. #define SUN6I_RXDATA_REG 0x300
  75. struct sun6i_spi {
  76. struct spi_master *master;
  77. void __iomem *base_addr;
  78. struct clk *hclk;
  79. struct clk *mclk;
  80. struct reset_control *rstc;
  81. struct completion done;
  82. const u8 *tx_buf;
  83. u8 *rx_buf;
  84. int len;
  85. unsigned long fifo_depth;
  86. };
  87. static inline u32 sun6i_spi_read(struct sun6i_spi *sspi, u32 reg)
  88. {
  89. return readl(sspi->base_addr + reg);
  90. }
  91. static inline void sun6i_spi_write(struct sun6i_spi *sspi, u32 reg, u32 value)
  92. {
  93. writel(value, sspi->base_addr + reg);
  94. }
  95. static inline u32 sun6i_spi_get_tx_fifo_count(struct sun6i_spi *sspi)
  96. {
  97. u32 reg = sun6i_spi_read(sspi, SUN6I_FIFO_STA_REG);
  98. reg >>= SUN6I_FIFO_STA_TF_CNT_BITS;
  99. return reg & SUN6I_FIFO_STA_TF_CNT_MASK;
  100. }
  101. static inline void sun6i_spi_enable_interrupt(struct sun6i_spi *sspi, u32 mask)
  102. {
  103. u32 reg = sun6i_spi_read(sspi, SUN6I_INT_CTL_REG);
  104. reg |= mask;
  105. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, reg);
  106. }
  107. static inline void sun6i_spi_disable_interrupt(struct sun6i_spi *sspi, u32 mask)
  108. {
  109. u32 reg = sun6i_spi_read(sspi, SUN6I_INT_CTL_REG);
  110. reg &= ~mask;
  111. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, reg);
  112. }
  113. static inline void sun6i_spi_drain_fifo(struct sun6i_spi *sspi, int len)
  114. {
  115. u32 reg, cnt;
  116. u8 byte;
  117. /* See how much data is available */
  118. reg = sun6i_spi_read(sspi, SUN6I_FIFO_STA_REG);
  119. reg &= SUN6I_FIFO_STA_RF_CNT_MASK;
  120. cnt = reg >> SUN6I_FIFO_STA_RF_CNT_BITS;
  121. if (len > cnt)
  122. len = cnt;
  123. while (len--) {
  124. byte = readb(sspi->base_addr + SUN6I_RXDATA_REG);
  125. if (sspi->rx_buf)
  126. *sspi->rx_buf++ = byte;
  127. }
  128. }
  129. static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi, int len)
  130. {
  131. u32 cnt;
  132. u8 byte;
  133. /* See how much data we can fit */
  134. cnt = sspi->fifo_depth - sun6i_spi_get_tx_fifo_count(sspi);
  135. len = min3(len, (int)cnt, sspi->len);
  136. while (len--) {
  137. byte = sspi->tx_buf ? *sspi->tx_buf++ : 0;
  138. writeb(byte, sspi->base_addr + SUN6I_TXDATA_REG);
  139. sspi->len--;
  140. }
  141. }
  142. static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
  143. {
  144. struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
  145. u32 reg;
  146. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  147. reg &= ~SUN6I_TFR_CTL_CS_MASK;
  148. reg |= SUN6I_TFR_CTL_CS(spi->chip_select);
  149. if (enable)
  150. reg |= SUN6I_TFR_CTL_CS_LEVEL;
  151. else
  152. reg &= ~SUN6I_TFR_CTL_CS_LEVEL;
  153. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
  154. }
  155. static size_t sun6i_spi_max_transfer_size(struct spi_device *spi)
  156. {
  157. return SUN6I_MAX_XFER_SIZE - 1;
  158. }
  159. static int sun6i_spi_transfer_one(struct spi_master *master,
  160. struct spi_device *spi,
  161. struct spi_transfer *tfr)
  162. {
  163. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  164. unsigned int mclk_rate, div, div_cdr1, div_cdr2, timeout;
  165. unsigned int start, end, tx_time;
  166. unsigned int trig_level;
  167. unsigned int tx_len = 0;
  168. int ret = 0;
  169. u32 reg;
  170. if (tfr->len > SUN6I_MAX_XFER_SIZE)
  171. return -EINVAL;
  172. reinit_completion(&sspi->done);
  173. sspi->tx_buf = tfr->tx_buf;
  174. sspi->rx_buf = tfr->rx_buf;
  175. sspi->len = tfr->len;
  176. /* Clear pending interrupts */
  177. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, ~0);
  178. /* Reset FIFO */
  179. sun6i_spi_write(sspi, SUN6I_FIFO_CTL_REG,
  180. SUN6I_FIFO_CTL_RF_RST | SUN6I_FIFO_CTL_TF_RST);
  181. /*
  182. * Setup FIFO interrupt trigger level
  183. * Here we choose 3/4 of the full fifo depth, as it's the hardcoded
  184. * value used in old generation of Allwinner SPI controller.
  185. * (See spi-sun4i.c)
  186. */
  187. trig_level = sspi->fifo_depth / 4 * 3;
  188. sun6i_spi_write(sspi, SUN6I_FIFO_CTL_REG,
  189. (trig_level << SUN6I_FIFO_CTL_RF_RDY_TRIG_LEVEL_BITS) |
  190. (trig_level << SUN6I_FIFO_CTL_TF_ERQ_TRIG_LEVEL_BITS));
  191. /*
  192. * Setup the transfer control register: Chip Select,
  193. * polarities, etc.
  194. */
  195. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  196. if (spi->mode & SPI_CPOL)
  197. reg |= SUN6I_TFR_CTL_CPOL;
  198. else
  199. reg &= ~SUN6I_TFR_CTL_CPOL;
  200. if (spi->mode & SPI_CPHA)
  201. reg |= SUN6I_TFR_CTL_CPHA;
  202. else
  203. reg &= ~SUN6I_TFR_CTL_CPHA;
  204. if (spi->mode & SPI_LSB_FIRST)
  205. reg |= SUN6I_TFR_CTL_FBS;
  206. else
  207. reg &= ~SUN6I_TFR_CTL_FBS;
  208. /*
  209. * If it's a TX only transfer, we don't want to fill the RX
  210. * FIFO with bogus data
  211. */
  212. if (sspi->rx_buf)
  213. reg &= ~SUN6I_TFR_CTL_DHB;
  214. else
  215. reg |= SUN6I_TFR_CTL_DHB;
  216. /* We want to control the chip select manually */
  217. reg |= SUN6I_TFR_CTL_CS_MANUAL;
  218. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
  219. /* Ensure that we have a parent clock fast enough */
  220. mclk_rate = clk_get_rate(sspi->mclk);
  221. if (mclk_rate < (2 * tfr->speed_hz)) {
  222. clk_set_rate(sspi->mclk, 2 * tfr->speed_hz);
  223. mclk_rate = clk_get_rate(sspi->mclk);
  224. }
  225. /*
  226. * Setup clock divider.
  227. *
  228. * We have two choices there. Either we can use the clock
  229. * divide rate 1, which is calculated thanks to this formula:
  230. * SPI_CLK = MOD_CLK / (2 ^ cdr)
  231. * Or we can use CDR2, which is calculated with the formula:
  232. * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
  233. * Wether we use the former or the latter is set through the
  234. * DRS bit.
  235. *
  236. * First try CDR2, and if we can't reach the expected
  237. * frequency, fall back to CDR1.
  238. */
  239. div_cdr1 = DIV_ROUND_UP(mclk_rate, tfr->speed_hz);
  240. div_cdr2 = DIV_ROUND_UP(div_cdr1, 2);
  241. if (div_cdr2 <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) {
  242. reg = SUN6I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN6I_CLK_CTL_DRS;
  243. } else {
  244. div = min(SUN6I_CLK_CTL_CDR1_MASK, order_base_2(div_cdr1));
  245. reg = SUN6I_CLK_CTL_CDR1(div);
  246. }
  247. sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
  248. /* Finally enable the bus - doing so before might raise SCK to HIGH */
  249. reg = sun6i_spi_read(sspi, SUN6I_GBL_CTL_REG);
  250. reg |= SUN6I_GBL_CTL_BUS_ENABLE;
  251. sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG, reg);
  252. /* Setup the transfer now... */
  253. if (sspi->tx_buf)
  254. tx_len = tfr->len;
  255. /* Setup the counters */
  256. sun6i_spi_write(sspi, SUN6I_BURST_CNT_REG, SUN6I_BURST_CNT(tfr->len));
  257. sun6i_spi_write(sspi, SUN6I_XMIT_CNT_REG, SUN6I_XMIT_CNT(tx_len));
  258. sun6i_spi_write(sspi, SUN6I_BURST_CTL_CNT_REG,
  259. SUN6I_BURST_CTL_CNT_STC(tx_len));
  260. /* Fill the TX FIFO */
  261. sun6i_spi_fill_fifo(sspi, sspi->fifo_depth);
  262. /* Enable the interrupts */
  263. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, SUN6I_INT_CTL_TC);
  264. sun6i_spi_enable_interrupt(sspi, SUN6I_INT_CTL_TC |
  265. SUN6I_INT_CTL_RF_RDY);
  266. if (tx_len > sspi->fifo_depth)
  267. sun6i_spi_enable_interrupt(sspi, SUN6I_INT_CTL_TF_ERQ);
  268. /* Start the transfer */
  269. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  270. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg | SUN6I_TFR_CTL_XCH);
  271. tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U);
  272. start = jiffies;
  273. timeout = wait_for_completion_timeout(&sspi->done,
  274. msecs_to_jiffies(tx_time));
  275. end = jiffies;
  276. if (!timeout) {
  277. dev_warn(&master->dev,
  278. "%s: timeout transferring %u bytes@%iHz for %i(%i)ms",
  279. dev_name(&spi->dev), tfr->len, tfr->speed_hz,
  280. jiffies_to_msecs(end - start), tx_time);
  281. ret = -ETIMEDOUT;
  282. goto out;
  283. }
  284. out:
  285. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, 0);
  286. return ret;
  287. }
  288. static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
  289. {
  290. struct sun6i_spi *sspi = dev_id;
  291. u32 status = sun6i_spi_read(sspi, SUN6I_INT_STA_REG);
  292. /* Transfer complete */
  293. if (status & SUN6I_INT_CTL_TC) {
  294. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC);
  295. sun6i_spi_drain_fifo(sspi, sspi->fifo_depth);
  296. complete(&sspi->done);
  297. return IRQ_HANDLED;
  298. }
  299. /* Receive FIFO 3/4 full */
  300. if (status & SUN6I_INT_CTL_RF_RDY) {
  301. sun6i_spi_drain_fifo(sspi, SUN6I_FIFO_DEPTH);
  302. /* Only clear the interrupt _after_ draining the FIFO */
  303. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_RF_RDY);
  304. return IRQ_HANDLED;
  305. }
  306. /* Transmit FIFO 3/4 empty */
  307. if (status & SUN6I_INT_CTL_TF_ERQ) {
  308. sun6i_spi_fill_fifo(sspi, SUN6I_FIFO_DEPTH);
  309. if (!sspi->len)
  310. /* nothing left to transmit */
  311. sun6i_spi_disable_interrupt(sspi, SUN6I_INT_CTL_TF_ERQ);
  312. /* Only clear the interrupt _after_ re-seeding the FIFO */
  313. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TF_ERQ);
  314. return IRQ_HANDLED;
  315. }
  316. return IRQ_NONE;
  317. }
  318. static int sun6i_spi_runtime_resume(struct device *dev)
  319. {
  320. struct spi_master *master = dev_get_drvdata(dev);
  321. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  322. int ret;
  323. ret = clk_prepare_enable(sspi->hclk);
  324. if (ret) {
  325. dev_err(dev, "Couldn't enable AHB clock\n");
  326. goto out;
  327. }
  328. ret = clk_prepare_enable(sspi->mclk);
  329. if (ret) {
  330. dev_err(dev, "Couldn't enable module clock\n");
  331. goto err;
  332. }
  333. ret = reset_control_deassert(sspi->rstc);
  334. if (ret) {
  335. dev_err(dev, "Couldn't deassert the device from reset\n");
  336. goto err2;
  337. }
  338. sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
  339. SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
  340. return 0;
  341. err2:
  342. clk_disable_unprepare(sspi->mclk);
  343. err:
  344. clk_disable_unprepare(sspi->hclk);
  345. out:
  346. return ret;
  347. }
  348. static int sun6i_spi_runtime_suspend(struct device *dev)
  349. {
  350. struct spi_master *master = dev_get_drvdata(dev);
  351. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  352. reset_control_assert(sspi->rstc);
  353. clk_disable_unprepare(sspi->mclk);
  354. clk_disable_unprepare(sspi->hclk);
  355. return 0;
  356. }
  357. static int sun6i_spi_probe(struct platform_device *pdev)
  358. {
  359. struct spi_master *master;
  360. struct sun6i_spi *sspi;
  361. struct resource *res;
  362. int ret = 0, irq;
  363. master = spi_alloc_master(&pdev->dev, sizeof(struct sun6i_spi));
  364. if (!master) {
  365. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  366. return -ENOMEM;
  367. }
  368. platform_set_drvdata(pdev, master);
  369. sspi = spi_master_get_devdata(master);
  370. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  371. sspi->base_addr = devm_ioremap_resource(&pdev->dev, res);
  372. if (IS_ERR(sspi->base_addr)) {
  373. ret = PTR_ERR(sspi->base_addr);
  374. goto err_free_master;
  375. }
  376. irq = platform_get_irq(pdev, 0);
  377. if (irq < 0) {
  378. dev_err(&pdev->dev, "No spi IRQ specified\n");
  379. ret = -ENXIO;
  380. goto err_free_master;
  381. }
  382. ret = devm_request_irq(&pdev->dev, irq, sun6i_spi_handler,
  383. 0, "sun6i-spi", sspi);
  384. if (ret) {
  385. dev_err(&pdev->dev, "Cannot request IRQ\n");
  386. goto err_free_master;
  387. }
  388. sspi->master = master;
  389. sspi->fifo_depth = (unsigned long)of_device_get_match_data(&pdev->dev);
  390. master->max_speed_hz = 100 * 1000 * 1000;
  391. master->min_speed_hz = 3 * 1000;
  392. master->set_cs = sun6i_spi_set_cs;
  393. master->transfer_one = sun6i_spi_transfer_one;
  394. master->num_chipselect = 4;
  395. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  396. master->bits_per_word_mask = SPI_BPW_MASK(8);
  397. master->dev.of_node = pdev->dev.of_node;
  398. master->auto_runtime_pm = true;
  399. master->max_transfer_size = sun6i_spi_max_transfer_size;
  400. sspi->hclk = devm_clk_get(&pdev->dev, "ahb");
  401. if (IS_ERR(sspi->hclk)) {
  402. dev_err(&pdev->dev, "Unable to acquire AHB clock\n");
  403. ret = PTR_ERR(sspi->hclk);
  404. goto err_free_master;
  405. }
  406. sspi->mclk = devm_clk_get(&pdev->dev, "mod");
  407. if (IS_ERR(sspi->mclk)) {
  408. dev_err(&pdev->dev, "Unable to acquire module clock\n");
  409. ret = PTR_ERR(sspi->mclk);
  410. goto err_free_master;
  411. }
  412. init_completion(&sspi->done);
  413. sspi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  414. if (IS_ERR(sspi->rstc)) {
  415. dev_err(&pdev->dev, "Couldn't get reset controller\n");
  416. ret = PTR_ERR(sspi->rstc);
  417. goto err_free_master;
  418. }
  419. /*
  420. * This wake-up/shutdown pattern is to be able to have the
  421. * device woken up, even if runtime_pm is disabled
  422. */
  423. ret = sun6i_spi_runtime_resume(&pdev->dev);
  424. if (ret) {
  425. dev_err(&pdev->dev, "Couldn't resume the device\n");
  426. goto err_free_master;
  427. }
  428. pm_runtime_set_active(&pdev->dev);
  429. pm_runtime_enable(&pdev->dev);
  430. pm_runtime_idle(&pdev->dev);
  431. ret = devm_spi_register_master(&pdev->dev, master);
  432. if (ret) {
  433. dev_err(&pdev->dev, "cannot register SPI master\n");
  434. goto err_pm_disable;
  435. }
  436. return 0;
  437. err_pm_disable:
  438. pm_runtime_disable(&pdev->dev);
  439. sun6i_spi_runtime_suspend(&pdev->dev);
  440. err_free_master:
  441. spi_master_put(master);
  442. return ret;
  443. }
  444. static int sun6i_spi_remove(struct platform_device *pdev)
  445. {
  446. pm_runtime_force_suspend(&pdev->dev);
  447. return 0;
  448. }
  449. static const struct of_device_id sun6i_spi_match[] = {
  450. { .compatible = "allwinner,sun6i-a31-spi", .data = (void *)SUN6I_FIFO_DEPTH },
  451. { .compatible = "allwinner,sun8i-h3-spi", .data = (void *)SUN8I_FIFO_DEPTH },
  452. {}
  453. };
  454. MODULE_DEVICE_TABLE(of, sun6i_spi_match);
  455. static const struct dev_pm_ops sun6i_spi_pm_ops = {
  456. .runtime_resume = sun6i_spi_runtime_resume,
  457. .runtime_suspend = sun6i_spi_runtime_suspend,
  458. };
  459. static struct platform_driver sun6i_spi_driver = {
  460. .probe = sun6i_spi_probe,
  461. .remove = sun6i_spi_remove,
  462. .driver = {
  463. .name = "sun6i-spi",
  464. .of_match_table = sun6i_spi_match,
  465. .pm = &sun6i_spi_pm_ops,
  466. },
  467. };
  468. module_platform_driver(sun6i_spi_driver);
  469. MODULE_AUTHOR("Pan Nan <pannan@allwinnertech.com>");
  470. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  471. MODULE_DESCRIPTION("Allwinner A31 SPI controller driver");
  472. MODULE_LICENSE("GPL");