146-1-spi-add-a31-spi.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. From 86cb7c7ab176112f8b0031dc7c8d19103ba52277 Mon Sep 17 00:00:00 2001
  2. From: Maxime Ripard <maxime.ripard@free-electrons.com>
  3. Date: Wed, 5 Feb 2014 14:05:05 +0100
  4. Subject: [PATCH] spi: sunxi: Add Allwinner A31 SPI controller driver
  5. The Allwinner A31 has a new SPI controller IP compared to the older Allwinner
  6. SoCs.
  7. It supports DMA, but the driver only does PIO for now, and DMA will be
  8. supported eventually.
  9. Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
  10. ---
  11. .../devicetree/bindings/spi/spi-sun6i.txt | 24 +
  12. drivers/spi/Kconfig | 6 +
  13. drivers/spi/Makefile | 1 +
  14. drivers/spi/spi-sun6i.c | 483 +++++++++++++++++++++
  15. 4 files changed, 514 insertions(+)
  16. create mode 100644 Documentation/devicetree/bindings/spi/spi-sun6i.txt
  17. create mode 100644 drivers/spi/spi-sun6i.c
  18. --- /dev/null
  19. +++ b/Documentation/devicetree/bindings/spi/spi-sun6i.txt
  20. @@ -0,0 +1,24 @@
  21. +Allwinner A31 SPI controller
  22. +
  23. +Required properties:
  24. +- compatible: Should be "allwinner,sun6i-a31-spi".
  25. +- reg: Should contain register location and length.
  26. +- interrupts: Should contain interrupt.
  27. +- clocks: phandle to the clocks feeding the SPI controller. Two are
  28. + needed:
  29. + - "ahb": the gated AHB parent clock
  30. + - "mod": the parent module clock
  31. +- clock-names: Must contain the clock names described just above
  32. +- resets: phandle to the reset controller asserting this device in
  33. + reset
  34. +
  35. +Example:
  36. +
  37. +spi1: spi@01c69000 {
  38. + compatible = "allwinner,sun6i-a31-spi";
  39. + reg = <0x01c69000 0x1000>;
  40. + interrupts = <0 66 4>;
  41. + clocks = <&ahb1_gates 21>, <&spi1_clk>;
  42. + clock-names = "ahb", "mod";
  43. + resets = <&ahb1_rst 21>;
  44. +};
  45. --- a/drivers/spi/Kconfig
  46. +++ b/drivers/spi/Kconfig
  47. @@ -455,6 +455,12 @@ config SPI_SIRF
  48. help
  49. SPI driver for CSR SiRFprimaII SoCs
  50. +config SPI_SUN6I
  51. + tristate "Allwinner A31 SPI controller"
  52. + depends on ARCH_SUNXI || COMPILE_TEST
  53. + help
  54. + This enables using the SPI controller on the Allwinner A31 SoCs.
  55. +
  56. config SPI_MXS
  57. tristate "Freescale MXS SPI controller"
  58. depends on ARCH_MXS
  59. --- a/drivers/spi/Makefile
  60. +++ b/drivers/spi/Makefile
  61. @@ -71,6 +71,7 @@ obj-$(CONFIG_SPI_SH_HSPI) += spi-sh-hsp
  62. obj-$(CONFIG_SPI_SH_MSIOF) += spi-sh-msiof.o
  63. obj-$(CONFIG_SPI_SH_SCI) += spi-sh-sci.o
  64. obj-$(CONFIG_SPI_SIRF) += spi-sirf.o
  65. +obj-$(CONFIG_SPI_SUN6I) += spi-sun6i.o
  66. obj-$(CONFIG_SPI_TEGRA114) += spi-tegra114.o
  67. obj-$(CONFIG_SPI_TEGRA20_SFLASH) += spi-tegra20-sflash.o
  68. obj-$(CONFIG_SPI_TEGRA20_SLINK) += spi-tegra20-slink.o
  69. --- /dev/null
  70. +++ b/drivers/spi/spi-sun6i.c
  71. @@ -0,0 +1,483 @@
  72. +/*
  73. + * Copyright (C) 2012 - 2014 Allwinner Tech
  74. + * Pan Nan <pannan@allwinnertech.com>
  75. + *
  76. + * Copyright (C) 2014 Maxime Ripard
  77. + * Maxime Ripard <maxime.ripard@free-electrons.com>
  78. + *
  79. + * This program is free software; you can redistribute it and/or
  80. + * modify it under the terms of the GNU General Public License as
  81. + * published by the Free Software Foundation; either version 2 of
  82. + * the License, or (at your option) any later version.
  83. + */
  84. +
  85. +#include <linux/clk.h>
  86. +#include <linux/delay.h>
  87. +#include <linux/device.h>
  88. +#include <linux/interrupt.h>
  89. +#include <linux/io.h>
  90. +#include <linux/module.h>
  91. +#include <linux/platform_device.h>
  92. +#include <linux/pm_runtime.h>
  93. +#include <linux/reset.h>
  94. +#include <linux/workqueue.h>
  95. +
  96. +#include <linux/spi/spi.h>
  97. +
  98. +#define SUN6I_FIFO_DEPTH 128
  99. +
  100. +#define SUN6I_GBL_CTL_REG 0x04
  101. +#define SUN6I_GBL_CTL_BUS_ENABLE BIT(0)
  102. +#define SUN6I_GBL_CTL_MASTER BIT(1)
  103. +#define SUN6I_GBL_CTL_TP BIT(7)
  104. +#define SUN6I_GBL_CTL_RST BIT(31)
  105. +
  106. +#define SUN6I_TFR_CTL_REG 0x08
  107. +#define SUN6I_TFR_CTL_CPHA BIT(0)
  108. +#define SUN6I_TFR_CTL_CPOL BIT(1)
  109. +#define SUN6I_TFR_CTL_SPOL BIT(2)
  110. +#define SUN6I_TFR_CTL_CS_MASK 0x3
  111. +#define SUN6I_TFR_CTL_CS(cs) (((cs) & SUN6I_TFR_CTL_CS_MASK) << 4)
  112. +#define SUN6I_TFR_CTL_CS_MANUAL BIT(6)
  113. +#define SUN6I_TFR_CTL_CS_LEVEL BIT(7)
  114. +#define SUN6I_TFR_CTL_DHB BIT(8)
  115. +#define SUN6I_TFR_CTL_FBS BIT(12)
  116. +#define SUN6I_TFR_CTL_XCH BIT(31)
  117. +
  118. +#define SUN6I_INT_CTL_REG 0x10
  119. +#define SUN6I_INT_CTL_RF_OVF BIT(8)
  120. +#define SUN6I_INT_CTL_TC BIT(12)
  121. +
  122. +#define SUN6I_INT_STA_REG 0x14
  123. +
  124. +#define SUN6I_FIFO_CTL_REG 0x18
  125. +#define SUN6I_FIFO_CTL_RF_RST BIT(15)
  126. +#define SUN6I_FIFO_CTL_TF_RST BIT(31)
  127. +
  128. +#define SUN6I_FIFO_STA_REG 0x1c
  129. +#define SUN6I_FIFO_STA_RF_CNT_MASK 0x7f
  130. +#define SUN6I_FIFO_STA_RF_CNT_BITS 0
  131. +#define SUN6I_FIFO_STA_TF_CNT_MASK 0x7f
  132. +#define SUN6I_FIFO_STA_TF_CNT_BITS 16
  133. +
  134. +#define SUN6I_CLK_CTL_REG 0x24
  135. +#define SUN6I_CLK_CTL_CDR2_MASK 0xff
  136. +#define SUN6I_CLK_CTL_CDR2(div) (((div) & SUN6I_CLK_CTL_CDR2_MASK) << 0)
  137. +#define SUN6I_CLK_CTL_CDR1_MASK 0xf
  138. +#define SUN6I_CLK_CTL_CDR1(div) (((div) & SUN6I_CLK_CTL_CDR1_MASK) << 8)
  139. +#define SUN6I_CLK_CTL_DRS BIT(12)
  140. +
  141. +#define SUN6I_BURST_CNT_REG 0x30
  142. +#define SUN6I_BURST_CNT(cnt) ((cnt) & 0xffffff)
  143. +
  144. +#define SUN6I_XMIT_CNT_REG 0x34
  145. +#define SUN6I_XMIT_CNT(cnt) ((cnt) & 0xffffff)
  146. +
  147. +#define SUN6I_BURST_CTL_CNT_REG 0x38
  148. +#define SUN6I_BURST_CTL_CNT_STC(cnt) ((cnt) & 0xffffff)
  149. +
  150. +#define SUN6I_TXDATA_REG 0x200
  151. +#define SUN6I_RXDATA_REG 0x300
  152. +
  153. +struct sun6i_spi {
  154. + struct spi_master *master;
  155. + void __iomem *base_addr;
  156. + struct clk *hclk;
  157. + struct clk *mclk;
  158. + struct reset_control *rstc;
  159. +
  160. + struct completion done;
  161. +
  162. + const u8 *tx_buf;
  163. + u8 *rx_buf;
  164. + int len;
  165. +};
  166. +
  167. +static inline u32 sun6i_spi_read(struct sun6i_spi *sspi, u32 reg)
  168. +{
  169. + return readl(sspi->base_addr + reg);
  170. +}
  171. +
  172. +static inline void sun6i_spi_write(struct sun6i_spi *sspi, u32 reg, u32 value)
  173. +{
  174. + writel(value, sspi->base_addr + reg);
  175. +}
  176. +
  177. +static inline void sun6i_spi_drain_fifo(struct sun6i_spi *sspi, int len)
  178. +{
  179. + u32 reg, cnt;
  180. + u8 byte;
  181. +
  182. + /* See how much data is available */
  183. + reg = sun6i_spi_read(sspi, SUN6I_FIFO_STA_REG);
  184. + reg &= SUN6I_FIFO_STA_RF_CNT_MASK;
  185. + cnt = reg >> SUN6I_FIFO_STA_RF_CNT_BITS;
  186. +
  187. + if (len > cnt)
  188. + len = cnt;
  189. +
  190. + while (len--) {
  191. + byte = readb(sspi->base_addr + SUN6I_RXDATA_REG);
  192. + if (sspi->rx_buf)
  193. + *sspi->rx_buf++ = byte;
  194. + }
  195. +}
  196. +
  197. +static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi, int len)
  198. +{
  199. + u8 byte;
  200. +
  201. + if (len > sspi->len)
  202. + len = sspi->len;
  203. +
  204. + while (len--) {
  205. + byte = sspi->tx_buf ? *sspi->tx_buf++ : 0;
  206. + writeb(byte, sspi->base_addr + SUN6I_TXDATA_REG);
  207. + sspi->len--;
  208. + }
  209. +}
  210. +
  211. +static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
  212. +{
  213. + struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
  214. + u32 reg;
  215. +
  216. + reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  217. + reg &= ~SUN6I_TFR_CTL_CS_MASK;
  218. + reg |= SUN6I_TFR_CTL_CS(spi->chip_select);
  219. +
  220. + if (enable)
  221. + reg |= SUN6I_TFR_CTL_CS_LEVEL;
  222. + else
  223. + reg &= ~SUN6I_TFR_CTL_CS_LEVEL;
  224. +
  225. + sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
  226. +}
  227. +
  228. +
  229. +static int sun6i_spi_transfer_one(struct spi_master *master,
  230. + struct spi_device *spi,
  231. + struct spi_transfer *tfr)
  232. +{
  233. + struct sun6i_spi *sspi = spi_master_get_devdata(master);
  234. + unsigned int mclk_rate, div, timeout;
  235. + unsigned int tx_len = 0;
  236. + int ret = 0;
  237. + u32 reg;
  238. +
  239. + /* We don't support transfer larger than the FIFO */
  240. + if (tfr->len > SUN6I_FIFO_DEPTH)
  241. + return -EINVAL;
  242. +
  243. + reinit_completion(&sspi->done);
  244. + sspi->tx_buf = tfr->tx_buf;
  245. + sspi->rx_buf = tfr->rx_buf;
  246. + sspi->len = tfr->len;
  247. +
  248. + /* Clear pending interrupts */
  249. + sun6i_spi_write(sspi, SUN6I_INT_STA_REG, ~0);
  250. +
  251. + /* Reset FIFO */
  252. + sun6i_spi_write(sspi, SUN6I_FIFO_CTL_REG,
  253. + SUN6I_FIFO_CTL_RF_RST | SUN6I_FIFO_CTL_TF_RST);
  254. +
  255. + /*
  256. + * Setup the transfer control register: Chip Select,
  257. + * polarities, etc.
  258. + */
  259. + reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  260. +
  261. + if (spi->mode & SPI_CPOL)
  262. + reg |= SUN6I_TFR_CTL_CPOL;
  263. + else
  264. + reg &= ~SUN6I_TFR_CTL_CPOL;
  265. +
  266. + if (spi->mode & SPI_CPHA)
  267. + reg |= SUN6I_TFR_CTL_CPHA;
  268. + else
  269. + reg &= ~SUN6I_TFR_CTL_CPHA;
  270. +
  271. + if (spi->mode & SPI_LSB_FIRST)
  272. + reg |= SUN6I_TFR_CTL_FBS;
  273. + else
  274. + reg &= ~SUN6I_TFR_CTL_FBS;
  275. +
  276. + /*
  277. + * If it's a TX only transfer, we don't want to fill the RX
  278. + * FIFO with bogus data
  279. + */
  280. + if (sspi->rx_buf)
  281. + reg &= ~SUN6I_TFR_CTL_DHB;
  282. + else
  283. + reg |= SUN6I_TFR_CTL_DHB;
  284. +
  285. + /* We want to control the chip select manually */
  286. + reg |= SUN6I_TFR_CTL_CS_MANUAL;
  287. +
  288. + sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
  289. +
  290. + /* Ensure that we have a parent clock fast enough */
  291. + mclk_rate = clk_get_rate(sspi->mclk);
  292. + if (mclk_rate < (2 * spi->max_speed_hz)) {
  293. + clk_set_rate(sspi->mclk, 2 * spi->max_speed_hz);
  294. + mclk_rate = clk_get_rate(sspi->mclk);
  295. + }
  296. +
  297. + /*
  298. + * Setup clock divider.
  299. + *
  300. + * We have two choices there. Either we can use the clock
  301. + * divide rate 1, which is calculated thanks to this formula:
  302. + * SPI_CLK = MOD_CLK / (2 ^ cdr)
  303. + * Or we can use CDR2, which is calculated with the formula:
  304. + * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
  305. + * Wether we use the former or the latter is set through the
  306. + * DRS bit.
  307. + *
  308. + * First try CDR2, and if we can't reach the expected
  309. + * frequency, fall back to CDR1.
  310. + */
  311. + div = mclk_rate / (2 * spi->max_speed_hz);
  312. + if (div <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) {
  313. + if (div > 0)
  314. + div--;
  315. +
  316. + reg = SUN6I_CLK_CTL_CDR2(div) | SUN6I_CLK_CTL_DRS;
  317. + } else {
  318. + div = ilog2(mclk_rate) - ilog2(spi->max_speed_hz);
  319. + reg = SUN6I_CLK_CTL_CDR1(div);
  320. + }
  321. +
  322. + sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
  323. +
  324. + /* Setup the transfer now... */
  325. + if (sspi->tx_buf)
  326. + tx_len = tfr->len;
  327. +
  328. + /* Setup the counters */
  329. + sun6i_spi_write(sspi, SUN6I_BURST_CNT_REG, SUN6I_BURST_CNT(tfr->len));
  330. + sun6i_spi_write(sspi, SUN6I_XMIT_CNT_REG, SUN6I_XMIT_CNT(tx_len));
  331. + sun6i_spi_write(sspi, SUN6I_BURST_CTL_CNT_REG,
  332. + SUN6I_BURST_CTL_CNT_STC(tx_len));
  333. +
  334. + /* Fill the TX FIFO */
  335. + sun6i_spi_fill_fifo(sspi, SUN6I_FIFO_DEPTH);
  336. +
  337. + /* Enable the interrupts */
  338. + sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, SUN6I_INT_CTL_TC);
  339. +
  340. + /* Start the transfer */
  341. + reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  342. + sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg | SUN6I_TFR_CTL_XCH);
  343. +
  344. + timeout = wait_for_completion_timeout(&sspi->done,
  345. + msecs_to_jiffies(1000));
  346. + if (!timeout) {
  347. + ret = -ETIMEDOUT;
  348. + goto out;
  349. + }
  350. +
  351. + sun6i_spi_drain_fifo(sspi, SUN6I_FIFO_DEPTH);
  352. +
  353. +out:
  354. + sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, 0);
  355. +
  356. + return ret;
  357. +}
  358. +
  359. +static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
  360. +{
  361. + struct sun6i_spi *sspi = dev_id;
  362. + u32 status = sun6i_spi_read(sspi, SUN6I_INT_STA_REG);
  363. +
  364. + /* Transfer complete */
  365. + if (status & SUN6I_INT_CTL_TC) {
  366. + sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC);
  367. + complete(&sspi->done);
  368. + return IRQ_HANDLED;
  369. + }
  370. +
  371. + return IRQ_NONE;
  372. +}
  373. +
  374. +static int sun6i_spi_runtime_resume(struct device *dev)
  375. +{
  376. + struct spi_master *master = dev_get_drvdata(dev);
  377. + struct sun6i_spi *sspi = spi_master_get_devdata(master);
  378. + int ret;
  379. +
  380. + ret = clk_prepare_enable(sspi->hclk);
  381. + if (ret) {
  382. + dev_err(dev, "Couldn't enable AHB clock\n");
  383. + goto out;
  384. + }
  385. +
  386. + ret = clk_prepare_enable(sspi->mclk);
  387. + if (ret) {
  388. + dev_err(dev, "Couldn't enable module clock\n");
  389. + goto err;
  390. + }
  391. +
  392. + ret = reset_control_deassert(sspi->rstc);
  393. + if (ret) {
  394. + dev_err(dev, "Couldn't deassert the device from reset\n");
  395. + goto err2;
  396. + }
  397. +
  398. + sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
  399. + SUN6I_GBL_CTL_BUS_ENABLE | SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
  400. +
  401. + return 0;
  402. +
  403. +err2:
  404. + clk_disable_unprepare(sspi->mclk);
  405. +err:
  406. + clk_disable_unprepare(sspi->hclk);
  407. +out:
  408. + return ret;
  409. +}
  410. +
  411. +static int sun6i_spi_runtime_suspend(struct device *dev)
  412. +{
  413. + struct spi_master *master = dev_get_drvdata(dev);
  414. + struct sun6i_spi *sspi = spi_master_get_devdata(master);
  415. +
  416. + reset_control_assert(sspi->rstc);
  417. + clk_disable_unprepare(sspi->mclk);
  418. + clk_disable_unprepare(sspi->hclk);
  419. +
  420. + return 0;
  421. +}
  422. +
  423. +static int sun6i_spi_probe(struct platform_device *pdev)
  424. +{
  425. + struct spi_master *master;
  426. + struct sun6i_spi *sspi;
  427. + struct resource *res;
  428. + int ret = 0, irq;
  429. +
  430. + master = spi_alloc_master(&pdev->dev, sizeof(struct sun6i_spi));
  431. + if (!master) {
  432. + dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  433. + return -ENOMEM;
  434. + }
  435. +
  436. + platform_set_drvdata(pdev, master);
  437. + sspi = spi_master_get_devdata(master);
  438. +
  439. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  440. + sspi->base_addr = devm_ioremap_resource(&pdev->dev, res);
  441. + if (IS_ERR(sspi->base_addr)) {
  442. + ret = PTR_ERR(sspi->base_addr);
  443. + goto err_free_master;
  444. + }
  445. +
  446. + irq = platform_get_irq(pdev, 0);
  447. + if (irq < 0) {
  448. + dev_err(&pdev->dev, "No spi IRQ specified\n");
  449. + ret = -ENXIO;
  450. + goto err_free_master;
  451. + }
  452. +
  453. + ret = devm_request_irq(&pdev->dev, irq, sun6i_spi_handler,
  454. + 0, "sun6i-spi", sspi);
  455. + if (ret) {
  456. + dev_err(&pdev->dev, "Cannot request IRQ\n");
  457. + goto err_free_master;
  458. + }
  459. +
  460. + sspi->master = master;
  461. + master->set_cs = sun6i_spi_set_cs;
  462. + master->transfer_one = sun6i_spi_transfer_one;
  463. + master->num_chipselect = 4;
  464. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  465. + master->dev.of_node = pdev->dev.of_node;
  466. + master->auto_runtime_pm = true;
  467. +
  468. + sspi->hclk = devm_clk_get(&pdev->dev, "ahb");
  469. + if (IS_ERR(sspi->hclk)) {
  470. + dev_err(&pdev->dev, "Unable to acquire AHB clock\n");
  471. + ret = PTR_ERR(sspi->hclk);
  472. + goto err_free_master;
  473. + }
  474. +
  475. + sspi->mclk = devm_clk_get(&pdev->dev, "mod");
  476. + if (IS_ERR(sspi->mclk)) {
  477. + dev_err(&pdev->dev, "Unable to acquire module clock\n");
  478. + ret = PTR_ERR(sspi->mclk);
  479. + goto err_free_master;
  480. + }
  481. +
  482. + init_completion(&sspi->done);
  483. +
  484. + sspi->rstc = devm_reset_control_get(&pdev->dev, NULL);
  485. + if (IS_ERR(sspi->rstc)) {
  486. + dev_err(&pdev->dev, "Couldn't get reset controller\n");
  487. + ret = PTR_ERR(sspi->rstc);
  488. + goto err_free_master;
  489. + }
  490. +
  491. + /*
  492. + * This wake-up/shutdown pattern is to be able to have the
  493. + * device woken up, even if runtime_pm is disabled
  494. + */
  495. + ret = sun6i_spi_runtime_resume(&pdev->dev);
  496. + if (ret) {
  497. + dev_err(&pdev->dev, "Couldn't resume the device\n");
  498. + goto err_free_master;
  499. + }
  500. +
  501. + pm_runtime_set_active(&pdev->dev);
  502. + pm_runtime_enable(&pdev->dev);
  503. + pm_runtime_idle(&pdev->dev);
  504. +
  505. + ret = devm_spi_register_master(&pdev->dev, master);
  506. + if (ret) {
  507. + dev_err(&pdev->dev, "cannot register SPI master\n");
  508. + goto err_pm_disable;
  509. + }
  510. +
  511. + return 0;
  512. +
  513. +err_pm_disable:
  514. + pm_runtime_disable(&pdev->dev);
  515. + sun6i_spi_runtime_suspend(&pdev->dev);
  516. +err_free_master:
  517. + spi_master_put(master);
  518. + return ret;
  519. +}
  520. +
  521. +static int sun6i_spi_remove(struct platform_device *pdev)
  522. +{
  523. + pm_runtime_disable(&pdev->dev);
  524. +
  525. + return 0;
  526. +}
  527. +
  528. +static const struct of_device_id sun6i_spi_match[] = {
  529. + { .compatible = "allwinner,sun6i-a31-spi", },
  530. + {}
  531. +};
  532. +MODULE_DEVICE_TABLE(of, sun6i_spi_match);
  533. +
  534. +static const struct dev_pm_ops sun6i_spi_pm_ops = {
  535. + .runtime_resume = sun6i_spi_runtime_resume,
  536. + .runtime_suspend = sun6i_spi_runtime_suspend,
  537. +};
  538. +
  539. +static struct platform_driver sun6i_spi_driver = {
  540. + .probe = sun6i_spi_probe,
  541. + .remove = sun6i_spi_remove,
  542. + .driver = {
  543. + .name = "sun6i-spi",
  544. + .owner = THIS_MODULE,
  545. + .of_match_table = sun6i_spi_match,
  546. + .pm = &sun6i_spi_pm_ops,
  547. + },
  548. +};
  549. +module_platform_driver(sun6i_spi_driver);
  550. +
  551. +MODULE_AUTHOR("Pan Nan <pannan@allwinnertech.com>");
  552. +MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  553. +MODULE_DESCRIPTION("Allwinner A31 SPI controller driver");
  554. +MODULE_LICENSE("GPL");