spi-microchip-core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // SPDX-License-Identifier: (GPL-2.0)
  2. /*
  3. * Microchip CoreSPI SPI controller driver
  4. *
  5. * Copyright (c) 2018-2022 Microchip Technology Inc. and its subsidiaries
  6. *
  7. * Author: Daire McNamara <daire.mcnamara@microchip.com>
  8. * Author: Conor Dooley <conor.dooley@microchip.com>
  9. *
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/spi/spi.h>
  21. #define MAX_LEN (0xffff)
  22. #define MAX_CS (1)
  23. #define DEFAULT_FRAMESIZE (8)
  24. #define FIFO_DEPTH (32)
  25. #define CLK_GEN_MODE1_MAX (255)
  26. #define CLK_GEN_MODE0_MAX (15)
  27. #define CLK_GEN_MIN (0)
  28. #define MODE_X_MASK_SHIFT (24)
  29. #define CONTROL_ENABLE BIT(0)
  30. #define CONTROL_MASTER BIT(1)
  31. #define CONTROL_RX_DATA_INT BIT(4)
  32. #define CONTROL_TX_DATA_INT BIT(5)
  33. #define CONTROL_RX_OVER_INT BIT(6)
  34. #define CONTROL_TX_UNDER_INT BIT(7)
  35. #define CONTROL_SPO BIT(24)
  36. #define CONTROL_SPH BIT(25)
  37. #define CONTROL_SPS BIT(26)
  38. #define CONTROL_FRAMEURUN BIT(27)
  39. #define CONTROL_CLKMODE BIT(28)
  40. #define CONTROL_BIGFIFO BIT(29)
  41. #define CONTROL_OENOFF BIT(30)
  42. #define CONTROL_RESET BIT(31)
  43. #define CONTROL_MODE_MASK GENMASK(3, 2)
  44. #define MOTOROLA_MODE (0)
  45. #define CONTROL_FRAMECNT_MASK GENMASK(23, 8)
  46. #define CONTROL_FRAMECNT_SHIFT (8)
  47. #define STATUS_ACTIVE BIT(14)
  48. #define STATUS_SSEL BIT(13)
  49. #define STATUS_FRAMESTART BIT(12)
  50. #define STATUS_TXFIFO_EMPTY_NEXT_READ BIT(11)
  51. #define STATUS_TXFIFO_EMPTY BIT(10)
  52. #define STATUS_TXFIFO_FULL_NEXT_WRITE BIT(9)
  53. #define STATUS_TXFIFO_FULL BIT(8)
  54. #define STATUS_RXFIFO_EMPTY_NEXT_READ BIT(7)
  55. #define STATUS_RXFIFO_EMPTY BIT(6)
  56. #define STATUS_RXFIFO_FULL_NEXT_WRITE BIT(5)
  57. #define STATUS_RXFIFO_FULL BIT(4)
  58. #define STATUS_TX_UNDERRUN BIT(3)
  59. #define STATUS_RX_OVERFLOW BIT(2)
  60. #define STATUS_RXDAT_RXED BIT(1)
  61. #define STATUS_TXDAT_SENT BIT(0)
  62. #define INT_TXDONE BIT(0)
  63. #define INT_RXRDY BIT(1)
  64. #define INT_RX_CHANNEL_OVERFLOW BIT(2)
  65. #define INT_TX_CHANNEL_UNDERRUN BIT(3)
  66. #define INT_ENABLE_MASK (CONTROL_RX_OVER_INT | CONTROL_TX_UNDER_INT)
  67. #define REG_CONTROL (0x00)
  68. #define REG_FRAME_SIZE (0x04)
  69. #define FRAME_SIZE_MASK GENMASK(5, 0)
  70. #define REG_STATUS (0x08)
  71. #define REG_INT_CLEAR (0x0c)
  72. #define REG_RX_DATA (0x10)
  73. #define REG_TX_DATA (0x14)
  74. #define REG_CLK_GEN (0x18)
  75. #define REG_SLAVE_SELECT (0x1c)
  76. #define SSEL_MASK GENMASK(7, 0)
  77. #define SSEL_DIRECT BIT(8)
  78. #define SSELOUT_SHIFT 9
  79. #define SSELOUT BIT(SSELOUT_SHIFT)
  80. #define REG_MIS (0x20)
  81. #define REG_RIS (0x24)
  82. #define REG_CONTROL2 (0x28)
  83. #define REG_COMMAND (0x2c)
  84. #define COMMAND_CLRFRAMECNT BIT(4)
  85. #define COMMAND_TXFIFORST BIT(3)
  86. #define COMMAND_RXFIFORST BIT(2)
  87. #define REG_PKTSIZE (0x30)
  88. #define REG_CMD_SIZE (0x34)
  89. #define REG_HWSTATUS (0x38)
  90. #define REG_STAT8 (0x3c)
  91. #define REG_CTRL2 (0x48)
  92. #define REG_FRAMESUP (0x50)
  93. struct mchp_corespi {
  94. void __iomem *regs;
  95. struct clk *clk;
  96. const u8 *tx_buf;
  97. u8 *rx_buf;
  98. u32 clk_gen; /* divider for spi output clock generated by the controller */
  99. u32 clk_mode;
  100. u32 pending_slave_select;
  101. int irq;
  102. int tx_len;
  103. int rx_len;
  104. int n_bytes;
  105. };
  106. static inline u32 mchp_corespi_read(struct mchp_corespi *spi, unsigned int reg)
  107. {
  108. return readl(spi->regs + reg);
  109. }
  110. static inline void mchp_corespi_write(struct mchp_corespi *spi, unsigned int reg, u32 val)
  111. {
  112. writel(val, spi->regs + reg);
  113. }
  114. static inline void mchp_corespi_disable(struct mchp_corespi *spi)
  115. {
  116. u32 control = mchp_corespi_read(spi, REG_CONTROL);
  117. control &= ~CONTROL_ENABLE;
  118. mchp_corespi_write(spi, REG_CONTROL, control);
  119. }
  120. static inline void mchp_corespi_read_fifo(struct mchp_corespi *spi, int fifo_max)
  121. {
  122. for (int i = 0; i < fifo_max; i++) {
  123. u32 data;
  124. while (mchp_corespi_read(spi, REG_STATUS) & STATUS_RXFIFO_EMPTY)
  125. ;
  126. data = mchp_corespi_read(spi, REG_RX_DATA);
  127. spi->rx_len -= spi->n_bytes;
  128. if (!spi->rx_buf)
  129. continue;
  130. if (spi->n_bytes == 4)
  131. *((u32 *)spi->rx_buf) = data;
  132. else if (spi->n_bytes == 2)
  133. *((u16 *)spi->rx_buf) = data;
  134. else
  135. *spi->rx_buf = data;
  136. spi->rx_buf += spi->n_bytes;
  137. }
  138. }
  139. static void mchp_corespi_enable_ints(struct mchp_corespi *spi)
  140. {
  141. u32 control = mchp_corespi_read(spi, REG_CONTROL);
  142. control |= INT_ENABLE_MASK;
  143. mchp_corespi_write(spi, REG_CONTROL, control);
  144. }
  145. static void mchp_corespi_disable_ints(struct mchp_corespi *spi)
  146. {
  147. u32 control = mchp_corespi_read(spi, REG_CONTROL);
  148. control &= ~INT_ENABLE_MASK;
  149. mchp_corespi_write(spi, REG_CONTROL, control);
  150. }
  151. static inline void mchp_corespi_set_xfer_size(struct mchp_corespi *spi, int len)
  152. {
  153. u32 control;
  154. u32 lenpart;
  155. u32 frames = mchp_corespi_read(spi, REG_FRAMESUP);
  156. /*
  157. * Writing to FRAMECNT in REG_CONTROL will reset the frame count, taking
  158. * a shortcut requires an explicit clear.
  159. */
  160. if (frames == len) {
  161. mchp_corespi_write(spi, REG_COMMAND, COMMAND_CLRFRAMECNT);
  162. return;
  163. }
  164. /*
  165. * The lower 16 bits of the frame count are stored in the control reg
  166. * for legacy reasons, but the upper 16 written to a different register:
  167. * FRAMESUP. While both the upper and lower bits can be *READ* from the
  168. * FRAMESUP register, writing to the lower 16 bits is (supposedly) a NOP.
  169. *
  170. * The driver used to disable the controller while modifying the frame
  171. * count, and mask off the lower 16 bits of len while writing to
  172. * FRAMES_UP. When the driver was changed to disable the controller as
  173. * infrequently as possible, it was discovered that the logic of
  174. * lenpart = len & 0xffff_0000
  175. * write(REG_FRAMESUP, lenpart)
  176. * would actually write zeros into the lower 16 bits on an mpfs250t-es,
  177. * despite documentation stating these bits were read-only.
  178. * Writing len unmasked into FRAMES_UP ensures those bits aren't zeroed
  179. * on an mpfs250t-es and will be a NOP for the lower 16 bits on hardware
  180. * that matches the documentation.
  181. */
  182. lenpart = len & 0xffff;
  183. control = mchp_corespi_read(spi, REG_CONTROL);
  184. control &= ~CONTROL_FRAMECNT_MASK;
  185. control |= lenpart << CONTROL_FRAMECNT_SHIFT;
  186. mchp_corespi_write(spi, REG_CONTROL, control);
  187. mchp_corespi_write(spi, REG_FRAMESUP, len);
  188. }
  189. static inline void mchp_corespi_write_fifo(struct mchp_corespi *spi, int fifo_max)
  190. {
  191. int i = 0;
  192. mchp_corespi_set_xfer_size(spi, fifo_max);
  193. while ((i < fifo_max) && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_TXFIFO_FULL)) {
  194. u32 word;
  195. if (spi->n_bytes == 4)
  196. word = spi->tx_buf ? *((u32 *)spi->tx_buf) : 0xaa;
  197. else if (spi->n_bytes == 2)
  198. word = spi->tx_buf ? *((u16 *)spi->tx_buf) : 0xaa;
  199. else
  200. word = spi->tx_buf ? *spi->tx_buf : 0xaa;
  201. mchp_corespi_write(spi, REG_TX_DATA, word);
  202. if (spi->tx_buf)
  203. spi->tx_buf += spi->n_bytes;
  204. i++;
  205. }
  206. spi->tx_len -= i * spi->n_bytes;
  207. }
  208. static inline void mchp_corespi_set_framesize(struct mchp_corespi *spi, int bt)
  209. {
  210. u32 frame_size = mchp_corespi_read(spi, REG_FRAME_SIZE);
  211. u32 control;
  212. if ((frame_size & FRAME_SIZE_MASK) == bt)
  213. return;
  214. /*
  215. * Disable the SPI controller. Writes to the frame size have
  216. * no effect when the controller is enabled.
  217. */
  218. control = mchp_corespi_read(spi, REG_CONTROL);
  219. control &= ~CONTROL_ENABLE;
  220. mchp_corespi_write(spi, REG_CONTROL, control);
  221. mchp_corespi_write(spi, REG_FRAME_SIZE, bt);
  222. control |= CONTROL_ENABLE;
  223. mchp_corespi_write(spi, REG_CONTROL, control);
  224. }
  225. static void mchp_corespi_set_cs(struct spi_device *spi, bool disable)
  226. {
  227. u32 reg;
  228. struct mchp_corespi *corespi = spi_controller_get_devdata(spi->controller);
  229. reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT);
  230. reg &= ~BIT(spi_get_chipselect(spi, 0));
  231. reg |= !disable << spi_get_chipselect(spi, 0);
  232. corespi->pending_slave_select = reg;
  233. /*
  234. * Only deassert chip select immediately. Writing to some registers
  235. * requires the controller to be disabled, which results in the
  236. * output pins being tristated and can cause the SCLK and MOSI lines
  237. * to transition. Therefore asserting the chip select is deferred
  238. * until just before writing to the TX FIFO, to ensure the device
  239. * doesn't see any spurious clock transitions whilst CS is enabled.
  240. */
  241. if (((spi->mode & SPI_CS_HIGH) == 0) == disable)
  242. mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg);
  243. }
  244. static int mchp_corespi_setup(struct spi_device *spi)
  245. {
  246. struct mchp_corespi *corespi = spi_controller_get_devdata(spi->controller);
  247. u32 reg;
  248. if (spi_is_csgpiod(spi))
  249. return 0;
  250. /*
  251. * Active high targets need to be specifically set to their inactive
  252. * states during probe by adding them to the "control group" & thus
  253. * driving their select line low.
  254. */
  255. if (spi->mode & SPI_CS_HIGH) {
  256. reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT);
  257. reg |= BIT(spi_get_chipselect(spi, 0));
  258. corespi->pending_slave_select = reg;
  259. mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg);
  260. }
  261. return 0;
  262. }
  263. static void mchp_corespi_init(struct spi_controller *host, struct mchp_corespi *spi)
  264. {
  265. unsigned long clk_hz;
  266. u32 control = mchp_corespi_read(spi, REG_CONTROL);
  267. control &= ~CONTROL_ENABLE;
  268. mchp_corespi_write(spi, REG_CONTROL, control);
  269. control |= CONTROL_MASTER;
  270. control &= ~CONTROL_MODE_MASK;
  271. control |= MOTOROLA_MODE;
  272. /*
  273. * The controller must be configured so that it doesn't remove Chip
  274. * Select until the entire message has been transferred, even if at
  275. * some points TX FIFO becomes empty.
  276. *
  277. * BIGFIFO mode is also enabled, which sets the fifo depth to 32 frames
  278. * for the 8 bit transfers that this driver uses.
  279. */
  280. control |= CONTROL_SPS | CONTROL_BIGFIFO;
  281. mchp_corespi_write(spi, REG_CONTROL, control);
  282. mchp_corespi_set_framesize(spi, DEFAULT_FRAMESIZE);
  283. /* max. possible spi clock rate is the apb clock rate */
  284. clk_hz = clk_get_rate(spi->clk);
  285. host->max_speed_hz = clk_hz;
  286. mchp_corespi_enable_ints(spi);
  287. /*
  288. * It is required to enable direct mode, otherwise control over the chip
  289. * select is relinquished to the hardware. SSELOUT is enabled too so we
  290. * can deal with active high targets.
  291. */
  292. spi->pending_slave_select = SSELOUT | SSEL_DIRECT;
  293. mchp_corespi_write(spi, REG_SLAVE_SELECT, spi->pending_slave_select);
  294. control = mchp_corespi_read(spi, REG_CONTROL);
  295. control &= ~CONTROL_RESET;
  296. control |= CONTROL_ENABLE;
  297. mchp_corespi_write(spi, REG_CONTROL, control);
  298. }
  299. static inline void mchp_corespi_set_clk_gen(struct mchp_corespi *spi)
  300. {
  301. u32 control;
  302. control = mchp_corespi_read(spi, REG_CONTROL);
  303. if (spi->clk_mode)
  304. control |= CONTROL_CLKMODE;
  305. else
  306. control &= ~CONTROL_CLKMODE;
  307. mchp_corespi_write(spi, REG_CLK_GEN, spi->clk_gen);
  308. mchp_corespi_write(spi, REG_CONTROL, control);
  309. }
  310. static inline void mchp_corespi_set_mode(struct mchp_corespi *spi, unsigned int mode)
  311. {
  312. u32 mode_val;
  313. u32 control = mchp_corespi_read(spi, REG_CONTROL);
  314. switch (mode & SPI_MODE_X_MASK) {
  315. case SPI_MODE_0:
  316. mode_val = 0;
  317. break;
  318. case SPI_MODE_1:
  319. mode_val = CONTROL_SPH;
  320. break;
  321. case SPI_MODE_2:
  322. mode_val = CONTROL_SPO;
  323. break;
  324. case SPI_MODE_3:
  325. mode_val = CONTROL_SPH | CONTROL_SPO;
  326. break;
  327. }
  328. /*
  329. * Disable the SPI controller. Writes to the frame protocol have
  330. * no effect when the controller is enabled.
  331. */
  332. control &= ~CONTROL_ENABLE;
  333. mchp_corespi_write(spi, REG_CONTROL, control);
  334. control &= ~(SPI_MODE_X_MASK << MODE_X_MASK_SHIFT);
  335. control |= mode_val;
  336. mchp_corespi_write(spi, REG_CONTROL, control);
  337. control |= CONTROL_ENABLE;
  338. mchp_corespi_write(spi, REG_CONTROL, control);
  339. }
  340. static irqreturn_t mchp_corespi_interrupt(int irq, void *dev_id)
  341. {
  342. struct spi_controller *host = dev_id;
  343. struct mchp_corespi *spi = spi_controller_get_devdata(host);
  344. u32 intfield = mchp_corespi_read(spi, REG_MIS) & 0xf;
  345. bool finalise = false;
  346. /* Interrupt line may be shared and not for us at all */
  347. if (intfield == 0)
  348. return IRQ_NONE;
  349. if (intfield & INT_RX_CHANNEL_OVERFLOW) {
  350. mchp_corespi_write(spi, REG_INT_CLEAR, INT_RX_CHANNEL_OVERFLOW);
  351. finalise = true;
  352. dev_err(&host->dev,
  353. "%s: RX OVERFLOW: rxlen: %d, txlen: %d\n", __func__,
  354. spi->rx_len, spi->tx_len);
  355. }
  356. if (intfield & INT_TX_CHANNEL_UNDERRUN) {
  357. mchp_corespi_write(spi, REG_INT_CLEAR, INT_TX_CHANNEL_UNDERRUN);
  358. finalise = true;
  359. dev_err(&host->dev,
  360. "%s: TX UNDERFLOW: rxlen: %d, txlen: %d\n", __func__,
  361. spi->rx_len, spi->tx_len);
  362. }
  363. if (finalise)
  364. spi_finalize_current_transfer(host);
  365. return IRQ_HANDLED;
  366. }
  367. static int mchp_corespi_calculate_clkgen(struct mchp_corespi *spi,
  368. unsigned long target_hz)
  369. {
  370. unsigned long clk_hz, spi_hz, clk_gen;
  371. clk_hz = clk_get_rate(spi->clk);
  372. if (!clk_hz)
  373. return -EINVAL;
  374. spi_hz = min(target_hz, clk_hz);
  375. /*
  376. * There are two possible clock modes for the controller generated
  377. * clock's division ratio:
  378. * CLK_MODE = 0: 1 / (2^(CLK_GEN + 1)) where CLK_GEN = 0 to 15.
  379. * CLK_MODE = 1: 1 / (2 * CLK_GEN + 1) where CLK_GEN = 0 to 255.
  380. * First try mode 1, fall back to 0 and if we have tried both modes and
  381. * we /still/ can't get a good setting, we then throw the toys out of
  382. * the pram and give up
  383. * clk_gen is the register name for the clock divider on MPFS.
  384. */
  385. clk_gen = DIV_ROUND_UP(clk_hz, 2 * spi_hz) - 1;
  386. if (clk_gen > CLK_GEN_MODE1_MAX || clk_gen <= CLK_GEN_MIN) {
  387. clk_gen = DIV_ROUND_UP(clk_hz, spi_hz);
  388. clk_gen = fls(clk_gen) - 1;
  389. if (clk_gen > CLK_GEN_MODE0_MAX)
  390. return -EINVAL;
  391. spi->clk_mode = 0;
  392. } else {
  393. spi->clk_mode = 1;
  394. }
  395. spi->clk_gen = clk_gen;
  396. return 0;
  397. }
  398. static int mchp_corespi_transfer_one(struct spi_controller *host,
  399. struct spi_device *spi_dev,
  400. struct spi_transfer *xfer)
  401. {
  402. struct mchp_corespi *spi = spi_controller_get_devdata(host);
  403. int ret;
  404. ret = mchp_corespi_calculate_clkgen(spi, (unsigned long)xfer->speed_hz);
  405. if (ret) {
  406. dev_err(&host->dev, "failed to set clk_gen for target %u Hz\n", xfer->speed_hz);
  407. return ret;
  408. }
  409. mchp_corespi_set_clk_gen(spi);
  410. spi->tx_buf = xfer->tx_buf;
  411. spi->rx_buf = xfer->rx_buf;
  412. spi->tx_len = xfer->len;
  413. spi->rx_len = xfer->len;
  414. spi->n_bytes = roundup_pow_of_two(DIV_ROUND_UP(xfer->bits_per_word, BITS_PER_BYTE));
  415. mchp_corespi_set_framesize(spi, xfer->bits_per_word);
  416. mchp_corespi_write(spi, REG_COMMAND, COMMAND_RXFIFORST | COMMAND_TXFIFORST);
  417. mchp_corespi_write(spi, REG_SLAVE_SELECT, spi->pending_slave_select);
  418. while (spi->tx_len) {
  419. int fifo_max = DIV_ROUND_UP(min(spi->tx_len, FIFO_DEPTH), spi->n_bytes);
  420. mchp_corespi_write_fifo(spi, fifo_max);
  421. mchp_corespi_read_fifo(spi, fifo_max);
  422. }
  423. spi_finalize_current_transfer(host);
  424. return 1;
  425. }
  426. static int mchp_corespi_prepare_message(struct spi_controller *host,
  427. struct spi_message *msg)
  428. {
  429. struct spi_device *spi_dev = msg->spi;
  430. struct mchp_corespi *spi = spi_controller_get_devdata(host);
  431. mchp_corespi_set_mode(spi, spi_dev->mode);
  432. return 0;
  433. }
  434. static int mchp_corespi_probe(struct platform_device *pdev)
  435. {
  436. struct spi_controller *host;
  437. struct mchp_corespi *spi;
  438. struct resource *res;
  439. u32 num_cs;
  440. int ret = 0;
  441. host = devm_spi_alloc_host(&pdev->dev, sizeof(*spi));
  442. if (!host)
  443. return dev_err_probe(&pdev->dev, -ENOMEM,
  444. "unable to allocate host for SPI controller\n");
  445. platform_set_drvdata(pdev, host);
  446. if (of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs))
  447. num_cs = MAX_CS;
  448. host->num_chipselect = num_cs;
  449. host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  450. host->use_gpio_descriptors = true;
  451. host->setup = mchp_corespi_setup;
  452. host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  453. host->transfer_one = mchp_corespi_transfer_one;
  454. host->prepare_message = mchp_corespi_prepare_message;
  455. host->set_cs = mchp_corespi_set_cs;
  456. host->dev.of_node = pdev->dev.of_node;
  457. spi = spi_controller_get_devdata(host);
  458. spi->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  459. if (IS_ERR(spi->regs))
  460. return PTR_ERR(spi->regs);
  461. spi->irq = platform_get_irq(pdev, 0);
  462. if (spi->irq < 0)
  463. return spi->irq;
  464. ret = devm_request_irq(&pdev->dev, spi->irq, mchp_corespi_interrupt,
  465. IRQF_SHARED, dev_name(&pdev->dev), host);
  466. if (ret)
  467. return dev_err_probe(&pdev->dev, ret,
  468. "could not request irq\n");
  469. spi->clk = devm_clk_get_enabled(&pdev->dev, NULL);
  470. if (IS_ERR(spi->clk))
  471. return dev_err_probe(&pdev->dev, PTR_ERR(spi->clk),
  472. "could not get clk\n");
  473. mchp_corespi_init(host, spi);
  474. ret = devm_spi_register_controller(&pdev->dev, host);
  475. if (ret) {
  476. mchp_corespi_disable(spi);
  477. return dev_err_probe(&pdev->dev, ret,
  478. "unable to register host for SPI controller\n");
  479. }
  480. dev_info(&pdev->dev, "Registered SPI controller %d\n", host->bus_num);
  481. return 0;
  482. }
  483. static void mchp_corespi_remove(struct platform_device *pdev)
  484. {
  485. struct spi_controller *host = platform_get_drvdata(pdev);
  486. struct mchp_corespi *spi = spi_controller_get_devdata(host);
  487. mchp_corespi_disable_ints(spi);
  488. mchp_corespi_disable(spi);
  489. }
  490. #define MICROCHIP_SPI_PM_OPS (NULL)
  491. /*
  492. * Platform driver data structure
  493. */
  494. #if defined(CONFIG_OF)
  495. static const struct of_device_id mchp_corespi_dt_ids[] = {
  496. { .compatible = "microchip,mpfs-spi" },
  497. { /* sentinel */ }
  498. };
  499. MODULE_DEVICE_TABLE(of, mchp_corespi_dt_ids);
  500. #endif
  501. static struct platform_driver mchp_corespi_driver = {
  502. .probe = mchp_corespi_probe,
  503. .driver = {
  504. .name = "microchip-corespi",
  505. .pm = MICROCHIP_SPI_PM_OPS,
  506. .of_match_table = of_match_ptr(mchp_corespi_dt_ids),
  507. },
  508. .remove_new = mchp_corespi_remove,
  509. };
  510. module_platform_driver(mchp_corespi_driver);
  511. MODULE_DESCRIPTION("Microchip coreSPI SPI controller driver");
  512. MODULE_AUTHOR("Daire McNamara <daire.mcnamara@microchip.com>");
  513. MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
  514. MODULE_LICENSE("GPL");