spi-mem.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Exceet Electronics GmbH
  4. * Copyright (C) 2018 Bootlin
  5. *
  6. * Author: Boris Brezillon <boris.brezillon@bootlin.com>
  7. */
  8. #include <linux/dmaengine.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/spi/spi.h>
  11. #include <linux/spi/spi-mem.h>
  12. #include "internals.h"
  13. #define SPI_MEM_MAX_BUSWIDTH 8
  14. /**
  15. * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
  16. * memory operation
  17. * @ctlr: the SPI controller requesting this dma_map()
  18. * @op: the memory operation containing the buffer to map
  19. * @sgt: a pointer to a non-initialized sg_table that will be filled by this
  20. * function
  21. *
  22. * Some controllers might want to do DMA on the data buffer embedded in @op.
  23. * This helper prepares everything for you and provides a ready-to-use
  24. * sg_table. This function is not intended to be called from spi drivers.
  25. * Only SPI controller drivers should use it.
  26. * Note that the caller must ensure the memory region pointed by
  27. * op->data.buf.{in,out} is DMA-able before calling this function.
  28. *
  29. * Return: 0 in case of success, a negative error code otherwise.
  30. */
  31. int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
  32. const struct spi_mem_op *op,
  33. struct sg_table *sgt)
  34. {
  35. struct device *dmadev;
  36. if (!op->data.nbytes)
  37. return -EINVAL;
  38. if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
  39. dmadev = ctlr->dma_tx->device->dev;
  40. else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
  41. dmadev = ctlr->dma_rx->device->dev;
  42. else
  43. dmadev = ctlr->dev.parent;
  44. if (!dmadev)
  45. return -EINVAL;
  46. return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
  47. op->data.dir == SPI_MEM_DATA_IN ?
  48. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  49. }
  50. EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
  51. /**
  52. * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
  53. * memory operation
  54. * @ctlr: the SPI controller requesting this dma_unmap()
  55. * @op: the memory operation containing the buffer to unmap
  56. * @sgt: a pointer to an sg_table previously initialized by
  57. * spi_controller_dma_map_mem_op_data()
  58. *
  59. * Some controllers might want to do DMA on the data buffer embedded in @op.
  60. * This helper prepares things so that the CPU can access the
  61. * op->data.buf.{in,out} buffer again.
  62. *
  63. * This function is not intended to be called from SPI drivers. Only SPI
  64. * controller drivers should use it.
  65. *
  66. * This function should be called after the DMA operation has finished and is
  67. * only valid if the previous spi_controller_dma_map_mem_op_data() call
  68. * returned 0.
  69. *
  70. * Return: 0 in case of success, a negative error code otherwise.
  71. */
  72. void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
  73. const struct spi_mem_op *op,
  74. struct sg_table *sgt)
  75. {
  76. struct device *dmadev;
  77. if (!op->data.nbytes)
  78. return;
  79. if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
  80. dmadev = ctlr->dma_tx->device->dev;
  81. else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
  82. dmadev = ctlr->dma_rx->device->dev;
  83. else
  84. dmadev = ctlr->dev.parent;
  85. spi_unmap_buf(ctlr, dmadev, sgt,
  86. op->data.dir == SPI_MEM_DATA_IN ?
  87. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  88. }
  89. EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
  90. static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx)
  91. {
  92. u32 mode = mem->spi->mode;
  93. switch (buswidth) {
  94. case 1:
  95. return 0;
  96. case 2:
  97. if ((tx &&
  98. (mode & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL))) ||
  99. (!tx &&
  100. (mode & (SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))))
  101. return 0;
  102. break;
  103. case 4:
  104. if ((tx && (mode & (SPI_TX_QUAD | SPI_TX_OCTAL))) ||
  105. (!tx && (mode & (SPI_RX_QUAD | SPI_RX_OCTAL))))
  106. return 0;
  107. break;
  108. case 8:
  109. if ((tx && (mode & SPI_TX_OCTAL)) ||
  110. (!tx && (mode & SPI_RX_OCTAL)))
  111. return 0;
  112. break;
  113. default:
  114. break;
  115. }
  116. return -ENOTSUPP;
  117. }
  118. static bool spi_mem_check_buswidth(struct spi_mem *mem,
  119. const struct spi_mem_op *op)
  120. {
  121. if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))
  122. return false;
  123. if (op->addr.nbytes &&
  124. spi_check_buswidth_req(mem, op->addr.buswidth, true))
  125. return false;
  126. if (op->dummy.nbytes &&
  127. spi_check_buswidth_req(mem, op->dummy.buswidth, true))
  128. return false;
  129. if (op->data.dir != SPI_MEM_NO_DATA &&
  130. spi_check_buswidth_req(mem, op->data.buswidth,
  131. op->data.dir == SPI_MEM_DATA_OUT))
  132. return false;
  133. return true;
  134. }
  135. bool spi_mem_dtr_supports_op(struct spi_mem *mem,
  136. const struct spi_mem_op *op)
  137. {
  138. if (op->cmd.nbytes != 2)
  139. return false;
  140. return spi_mem_check_buswidth(mem, op);
  141. }
  142. EXPORT_SYMBOL_GPL(spi_mem_dtr_supports_op);
  143. bool spi_mem_default_supports_op(struct spi_mem *mem,
  144. const struct spi_mem_op *op)
  145. {
  146. if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr)
  147. return false;
  148. if (op->cmd.nbytes != 1)
  149. return false;
  150. return spi_mem_check_buswidth(mem, op);
  151. }
  152. EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
  153. static bool spi_mem_buswidth_is_valid(u8 buswidth)
  154. {
  155. if (hweight8(buswidth) > 1 || buswidth > SPI_MEM_MAX_BUSWIDTH)
  156. return false;
  157. return true;
  158. }
  159. static int spi_mem_check_op(const struct spi_mem_op *op)
  160. {
  161. if (!op->cmd.buswidth || !op->cmd.nbytes)
  162. return -EINVAL;
  163. if ((op->addr.nbytes && !op->addr.buswidth) ||
  164. (op->dummy.nbytes && !op->dummy.buswidth) ||
  165. (op->data.nbytes && !op->data.buswidth))
  166. return -EINVAL;
  167. if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) ||
  168. !spi_mem_buswidth_is_valid(op->addr.buswidth) ||
  169. !spi_mem_buswidth_is_valid(op->dummy.buswidth) ||
  170. !spi_mem_buswidth_is_valid(op->data.buswidth))
  171. return -EINVAL;
  172. return 0;
  173. }
  174. static bool spi_mem_internal_supports_op(struct spi_mem *mem,
  175. const struct spi_mem_op *op)
  176. {
  177. struct spi_controller *ctlr = mem->spi->controller;
  178. if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
  179. return ctlr->mem_ops->supports_op(mem, op);
  180. return spi_mem_default_supports_op(mem, op);
  181. }
  182. /**
  183. * spi_mem_supports_op() - Check if a memory device and the controller it is
  184. * connected to support a specific memory operation
  185. * @mem: the SPI memory
  186. * @op: the memory operation to check
  187. *
  188. * Some controllers are only supporting Single or Dual IOs, others might only
  189. * support specific opcodes, or it can even be that the controller and device
  190. * both support Quad IOs but the hardware prevents you from using it because
  191. * only 2 IO lines are connected.
  192. *
  193. * This function checks whether a specific operation is supported.
  194. *
  195. * Return: true if @op is supported, false otherwise.
  196. */
  197. bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
  198. {
  199. if (spi_mem_check_op(op))
  200. return false;
  201. return spi_mem_internal_supports_op(mem, op);
  202. }
  203. EXPORT_SYMBOL_GPL(spi_mem_supports_op);
  204. static int spi_mem_access_start(struct spi_mem *mem)
  205. {
  206. struct spi_controller *ctlr = mem->spi->controller;
  207. /*
  208. * Flush the message queue before executing our SPI memory
  209. * operation to prevent preemption of regular SPI transfers.
  210. */
  211. spi_flush_queue(ctlr);
  212. if (ctlr->auto_runtime_pm) {
  213. int ret;
  214. ret = pm_runtime_get_sync(ctlr->dev.parent);
  215. if (ret < 0) {
  216. pm_runtime_put_noidle(ctlr->dev.parent);
  217. dev_err(&ctlr->dev, "Failed to power device: %d\n",
  218. ret);
  219. return ret;
  220. }
  221. }
  222. mutex_lock(&ctlr->bus_lock_mutex);
  223. mutex_lock(&ctlr->io_mutex);
  224. return 0;
  225. }
  226. static void spi_mem_access_end(struct spi_mem *mem)
  227. {
  228. struct spi_controller *ctlr = mem->spi->controller;
  229. mutex_unlock(&ctlr->io_mutex);
  230. mutex_unlock(&ctlr->bus_lock_mutex);
  231. if (ctlr->auto_runtime_pm)
  232. pm_runtime_put(ctlr->dev.parent);
  233. }
  234. /**
  235. * spi_mem_exec_op() - Execute a memory operation
  236. * @mem: the SPI memory
  237. * @op: the memory operation to execute
  238. *
  239. * Executes a memory operation.
  240. *
  241. * This function first checks that @op is supported and then tries to execute
  242. * it.
  243. *
  244. * Return: 0 in case of success, a negative error code otherwise.
  245. */
  246. int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
  247. {
  248. unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
  249. struct spi_controller *ctlr = mem->spi->controller;
  250. struct spi_transfer xfers[4] = { };
  251. struct spi_message msg;
  252. u8 *tmpbuf;
  253. int ret;
  254. ret = spi_mem_check_op(op);
  255. if (ret)
  256. return ret;
  257. if (!spi_mem_internal_supports_op(mem, op))
  258. return -ENOTSUPP;
  259. if (ctlr->mem_ops && !mem->spi->cs_gpiod) {
  260. ret = spi_mem_access_start(mem);
  261. if (ret)
  262. return ret;
  263. ret = ctlr->mem_ops->exec_op(mem, op);
  264. spi_mem_access_end(mem);
  265. /*
  266. * Some controllers only optimize specific paths (typically the
  267. * read path) and expect the core to use the regular SPI
  268. * interface in other cases.
  269. */
  270. if (!ret || ret != -ENOTSUPP)
  271. return ret;
  272. }
  273. tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
  274. /*
  275. * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
  276. * we're guaranteed that this buffer is DMA-able, as required by the
  277. * SPI layer.
  278. */
  279. tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
  280. if (!tmpbuf)
  281. return -ENOMEM;
  282. spi_message_init(&msg);
  283. tmpbuf[0] = op->cmd.opcode;
  284. xfers[xferpos].tx_buf = tmpbuf;
  285. xfers[xferpos].len = op->cmd.nbytes;
  286. xfers[xferpos].tx_nbits = op->cmd.buswidth;
  287. spi_message_add_tail(&xfers[xferpos], &msg);
  288. xferpos++;
  289. totalxferlen++;
  290. if (op->addr.nbytes) {
  291. int i;
  292. for (i = 0; i < op->addr.nbytes; i++)
  293. tmpbuf[i + 1] = op->addr.val >>
  294. (8 * (op->addr.nbytes - i - 1));
  295. xfers[xferpos].tx_buf = tmpbuf + 1;
  296. xfers[xferpos].len = op->addr.nbytes;
  297. xfers[xferpos].tx_nbits = op->addr.buswidth;
  298. spi_message_add_tail(&xfers[xferpos], &msg);
  299. xferpos++;
  300. totalxferlen += op->addr.nbytes;
  301. }
  302. if (op->dummy.nbytes) {
  303. memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
  304. xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
  305. xfers[xferpos].len = op->dummy.nbytes;
  306. xfers[xferpos].tx_nbits = op->dummy.buswidth;
  307. spi_message_add_tail(&xfers[xferpos], &msg);
  308. xferpos++;
  309. totalxferlen += op->dummy.nbytes;
  310. }
  311. if (op->data.nbytes) {
  312. if (op->data.dir == SPI_MEM_DATA_IN) {
  313. xfers[xferpos].rx_buf = op->data.buf.in;
  314. xfers[xferpos].rx_nbits = op->data.buswidth;
  315. } else {
  316. xfers[xferpos].tx_buf = op->data.buf.out;
  317. xfers[xferpos].tx_nbits = op->data.buswidth;
  318. }
  319. xfers[xferpos].len = op->data.nbytes;
  320. spi_message_add_tail(&xfers[xferpos], &msg);
  321. xferpos++;
  322. totalxferlen += op->data.nbytes;
  323. }
  324. ret = spi_sync(mem->spi, &msg);
  325. kfree(tmpbuf);
  326. if (ret)
  327. return ret;
  328. if (msg.actual_length != totalxferlen)
  329. return -EIO;
  330. return 0;
  331. }
  332. EXPORT_SYMBOL_GPL(spi_mem_exec_op);
  333. /**
  334. * spi_mem_get_name() - Return the SPI mem device name to be used by the
  335. * upper layer if necessary
  336. * @mem: the SPI memory
  337. *
  338. * This function allows SPI mem users to retrieve the SPI mem device name.
  339. * It is useful if the upper layer needs to expose a custom name for
  340. * compatibility reasons.
  341. *
  342. * Return: a string containing the name of the memory device to be used
  343. * by the SPI mem user
  344. */
  345. const char *spi_mem_get_name(struct spi_mem *mem)
  346. {
  347. return mem->name;
  348. }
  349. EXPORT_SYMBOL_GPL(spi_mem_get_name);
  350. /**
  351. * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
  352. * match controller limitations
  353. * @mem: the SPI memory
  354. * @op: the operation to adjust
  355. *
  356. * Some controllers have FIFO limitations and must split a data transfer
  357. * operation into multiple ones, others require a specific alignment for
  358. * optimized accesses. This function allows SPI mem drivers to split a single
  359. * operation into multiple sub-operations when required.
  360. *
  361. * Return: a negative error code if the controller can't properly adjust @op,
  362. * 0 otherwise. Note that @op->data.nbytes will be updated if @op
  363. * can't be handled in a single step.
  364. */
  365. int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
  366. {
  367. struct spi_controller *ctlr = mem->spi->controller;
  368. size_t len;
  369. if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
  370. return ctlr->mem_ops->adjust_op_size(mem, op);
  371. if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
  372. len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
  373. if (len > spi_max_transfer_size(mem->spi))
  374. return -EINVAL;
  375. op->data.nbytes = min3((size_t)op->data.nbytes,
  376. spi_max_transfer_size(mem->spi),
  377. spi_max_message_size(mem->spi) -
  378. len);
  379. if (!op->data.nbytes)
  380. return -EINVAL;
  381. }
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
  385. static ssize_t spi_mem_no_dirmap_read(struct spi_mem_dirmap_desc *desc,
  386. u64 offs, size_t len, void *buf)
  387. {
  388. struct spi_mem_op op = desc->info.op_tmpl;
  389. int ret;
  390. op.addr.val = desc->info.offset + offs;
  391. op.data.buf.in = buf;
  392. op.data.nbytes = len;
  393. ret = spi_mem_adjust_op_size(desc->mem, &op);
  394. if (ret)
  395. return ret;
  396. ret = spi_mem_exec_op(desc->mem, &op);
  397. if (ret)
  398. return ret;
  399. return op.data.nbytes;
  400. }
  401. static ssize_t spi_mem_no_dirmap_write(struct spi_mem_dirmap_desc *desc,
  402. u64 offs, size_t len, const void *buf)
  403. {
  404. struct spi_mem_op op = desc->info.op_tmpl;
  405. int ret;
  406. op.addr.val = desc->info.offset + offs;
  407. op.data.buf.out = buf;
  408. op.data.nbytes = len;
  409. ret = spi_mem_adjust_op_size(desc->mem, &op);
  410. if (ret)
  411. return ret;
  412. ret = spi_mem_exec_op(desc->mem, &op);
  413. if (ret)
  414. return ret;
  415. return op.data.nbytes;
  416. }
  417. /**
  418. * spi_mem_dirmap_create() - Create a direct mapping descriptor
  419. * @mem: SPI mem device this direct mapping should be created for
  420. * @info: direct mapping information
  421. *
  422. * This function is creating a direct mapping descriptor which can then be used
  423. * to access the memory using spi_mem_dirmap_read() or spi_mem_dirmap_write().
  424. * If the SPI controller driver does not support direct mapping, this function
  425. * falls back to an implementation using spi_mem_exec_op(), so that the caller
  426. * doesn't have to bother implementing a fallback on his own.
  427. *
  428. * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
  429. */
  430. struct spi_mem_dirmap_desc *
  431. spi_mem_dirmap_create(struct spi_mem *mem,
  432. const struct spi_mem_dirmap_info *info)
  433. {
  434. struct spi_controller *ctlr = mem->spi->controller;
  435. struct spi_mem_dirmap_desc *desc;
  436. int ret = -ENOTSUPP;
  437. /* Make sure the number of address cycles is between 1 and 8 bytes. */
  438. if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8)
  439. return ERR_PTR(-EINVAL);
  440. /* data.dir should either be SPI_MEM_DATA_IN or SPI_MEM_DATA_OUT. */
  441. if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA)
  442. return ERR_PTR(-EINVAL);
  443. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  444. if (!desc)
  445. return ERR_PTR(-ENOMEM);
  446. desc->mem = mem;
  447. desc->info = *info;
  448. if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create)
  449. ret = ctlr->mem_ops->dirmap_create(desc);
  450. if (ret) {
  451. desc->nodirmap = true;
  452. if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl))
  453. ret = -ENOTSUPP;
  454. else
  455. ret = 0;
  456. }
  457. if (ret) {
  458. kfree(desc);
  459. return ERR_PTR(ret);
  460. }
  461. return desc;
  462. }
  463. EXPORT_SYMBOL_GPL(spi_mem_dirmap_create);
  464. /**
  465. * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor
  466. * @desc: the direct mapping descriptor to destroy
  467. *
  468. * This function destroys a direct mapping descriptor previously created by
  469. * spi_mem_dirmap_create().
  470. */
  471. void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc)
  472. {
  473. struct spi_controller *ctlr = desc->mem->spi->controller;
  474. if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy)
  475. ctlr->mem_ops->dirmap_destroy(desc);
  476. kfree(desc);
  477. }
  478. EXPORT_SYMBOL_GPL(spi_mem_dirmap_destroy);
  479. static void devm_spi_mem_dirmap_release(struct device *dev, void *res)
  480. {
  481. struct spi_mem_dirmap_desc *desc = *(struct spi_mem_dirmap_desc **)res;
  482. spi_mem_dirmap_destroy(desc);
  483. }
  484. /**
  485. * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach
  486. * it to a device
  487. * @dev: device the dirmap desc will be attached to
  488. * @mem: SPI mem device this direct mapping should be created for
  489. * @info: direct mapping information
  490. *
  491. * devm_ variant of the spi_mem_dirmap_create() function. See
  492. * spi_mem_dirmap_create() for more details.
  493. *
  494. * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
  495. */
  496. struct spi_mem_dirmap_desc *
  497. devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
  498. const struct spi_mem_dirmap_info *info)
  499. {
  500. struct spi_mem_dirmap_desc **ptr, *desc;
  501. ptr = devres_alloc(devm_spi_mem_dirmap_release, sizeof(*ptr),
  502. GFP_KERNEL);
  503. if (!ptr)
  504. return ERR_PTR(-ENOMEM);
  505. desc = spi_mem_dirmap_create(mem, info);
  506. if (IS_ERR(desc)) {
  507. devres_free(ptr);
  508. } else {
  509. *ptr = desc;
  510. devres_add(dev, ptr);
  511. }
  512. return desc;
  513. }
  514. EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_create);
  515. static int devm_spi_mem_dirmap_match(struct device *dev, void *res, void *data)
  516. {
  517. struct spi_mem_dirmap_desc **ptr = res;
  518. if (WARN_ON(!ptr || !*ptr))
  519. return 0;
  520. return *ptr == data;
  521. }
  522. /**
  523. * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached
  524. * to a device
  525. * @dev: device the dirmap desc is attached to
  526. * @desc: the direct mapping descriptor to destroy
  527. *
  528. * devm_ variant of the spi_mem_dirmap_destroy() function. See
  529. * spi_mem_dirmap_destroy() for more details.
  530. */
  531. void devm_spi_mem_dirmap_destroy(struct device *dev,
  532. struct spi_mem_dirmap_desc *desc)
  533. {
  534. devres_release(dev, devm_spi_mem_dirmap_release,
  535. devm_spi_mem_dirmap_match, desc);
  536. }
  537. EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy);
  538. /**
  539. * spi_mem_dirmap_read() - Read data through a direct mapping
  540. * @desc: direct mapping descriptor
  541. * @offs: offset to start reading from. Note that this is not an absolute
  542. * offset, but the offset within the direct mapping which already has
  543. * its own offset
  544. * @len: length in bytes
  545. * @buf: destination buffer. This buffer must be DMA-able
  546. *
  547. * This function reads data from a memory device using a direct mapping
  548. * previously instantiated with spi_mem_dirmap_create().
  549. *
  550. * Return: the amount of data read from the memory device or a negative error
  551. * code. Note that the returned size might be smaller than @len, and the caller
  552. * is responsible for calling spi_mem_dirmap_read() again when that happens.
  553. */
  554. ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
  555. u64 offs, size_t len, void *buf)
  556. {
  557. struct spi_controller *ctlr = desc->mem->spi->controller;
  558. ssize_t ret;
  559. if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN)
  560. return -EINVAL;
  561. if (!len)
  562. return 0;
  563. if (desc->nodirmap) {
  564. ret = spi_mem_no_dirmap_read(desc, offs, len, buf);
  565. } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) {
  566. ret = spi_mem_access_start(desc->mem);
  567. if (ret)
  568. return ret;
  569. ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf);
  570. spi_mem_access_end(desc->mem);
  571. } else {
  572. ret = -ENOTSUPP;
  573. }
  574. return ret;
  575. }
  576. EXPORT_SYMBOL_GPL(spi_mem_dirmap_read);
  577. /**
  578. * spi_mem_dirmap_write() - Write data through a direct mapping
  579. * @desc: direct mapping descriptor
  580. * @offs: offset to start writing from. Note that this is not an absolute
  581. * offset, but the offset within the direct mapping which already has
  582. * its own offset
  583. * @len: length in bytes
  584. * @buf: source buffer. This buffer must be DMA-able
  585. *
  586. * This function writes data to a memory device using a direct mapping
  587. * previously instantiated with spi_mem_dirmap_create().
  588. *
  589. * Return: the amount of data written to the memory device or a negative error
  590. * code. Note that the returned size might be smaller than @len, and the caller
  591. * is responsible for calling spi_mem_dirmap_write() again when that happens.
  592. */
  593. ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
  594. u64 offs, size_t len, const void *buf)
  595. {
  596. struct spi_controller *ctlr = desc->mem->spi->controller;
  597. ssize_t ret;
  598. if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT)
  599. return -EINVAL;
  600. if (!len)
  601. return 0;
  602. if (desc->nodirmap) {
  603. ret = spi_mem_no_dirmap_write(desc, offs, len, buf);
  604. } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) {
  605. ret = spi_mem_access_start(desc->mem);
  606. if (ret)
  607. return ret;
  608. ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf);
  609. spi_mem_access_end(desc->mem);
  610. } else {
  611. ret = -ENOTSUPP;
  612. }
  613. return ret;
  614. }
  615. EXPORT_SYMBOL_GPL(spi_mem_dirmap_write);
  616. static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
  617. {
  618. return container_of(drv, struct spi_mem_driver, spidrv.driver);
  619. }
  620. static int spi_mem_probe(struct spi_device *spi)
  621. {
  622. struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
  623. struct spi_controller *ctlr = spi->controller;
  624. struct spi_mem *mem;
  625. mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
  626. if (!mem)
  627. return -ENOMEM;
  628. mem->spi = spi;
  629. if (ctlr->mem_ops && ctlr->mem_ops->get_name)
  630. mem->name = ctlr->mem_ops->get_name(mem);
  631. else
  632. mem->name = dev_name(&spi->dev);
  633. if (IS_ERR_OR_NULL(mem->name))
  634. return PTR_ERR(mem->name);
  635. spi_set_drvdata(spi, mem);
  636. return memdrv->probe(mem);
  637. }
  638. static int spi_mem_remove(struct spi_device *spi)
  639. {
  640. struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
  641. struct spi_mem *mem = spi_get_drvdata(spi);
  642. if (memdrv->remove)
  643. return memdrv->remove(mem);
  644. return 0;
  645. }
  646. static void spi_mem_shutdown(struct spi_device *spi)
  647. {
  648. struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
  649. struct spi_mem *mem = spi_get_drvdata(spi);
  650. if (memdrv->shutdown)
  651. memdrv->shutdown(mem);
  652. }
  653. /**
  654. * spi_mem_driver_register_with_owner() - Register a SPI memory driver
  655. * @memdrv: the SPI memory driver to register
  656. * @owner: the owner of this driver
  657. *
  658. * Registers a SPI memory driver.
  659. *
  660. * Return: 0 in case of success, a negative error core otherwise.
  661. */
  662. int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
  663. struct module *owner)
  664. {
  665. memdrv->spidrv.probe = spi_mem_probe;
  666. memdrv->spidrv.remove = spi_mem_remove;
  667. memdrv->spidrv.shutdown = spi_mem_shutdown;
  668. return __spi_register_driver(owner, &memdrv->spidrv);
  669. }
  670. EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
  671. /**
  672. * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
  673. * @memdrv: the SPI memory driver to unregister
  674. *
  675. * Unregisters a SPI memory driver.
  676. */
  677. void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
  678. {
  679. spi_unregister_driver(&memdrv->spidrv);
  680. }
  681. EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);