This source file includes following definitions.
- spi_controller_dma_map_mem_op_data
- spi_controller_dma_unmap_mem_op_data
- spi_check_buswidth_req
- spi_mem_default_supports_op
- spi_mem_buswidth_is_valid
- spi_mem_check_op
- spi_mem_internal_supports_op
- spi_mem_supports_op
- spi_mem_access_start
- spi_mem_access_end
- spi_mem_exec_op
- spi_mem_get_name
- spi_mem_adjust_op_size
- spi_mem_no_dirmap_read
- spi_mem_no_dirmap_write
- spi_mem_dirmap_create
- spi_mem_dirmap_destroy
- devm_spi_mem_dirmap_release
- devm_spi_mem_dirmap_create
- devm_spi_mem_dirmap_match
- devm_spi_mem_dirmap_destroy
- spi_mem_dirmap_read
- spi_mem_dirmap_write
- to_spi_mem_drv
- spi_mem_probe
- spi_mem_remove
- spi_mem_shutdown
- spi_mem_driver_register_with_owner
- spi_mem_driver_unregister
1
2
3
4
5
6
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
13 #include "internals.h"
14
15 #define SPI_MEM_MAX_BUSWIDTH 8
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
35 const struct spi_mem_op *op,
36 struct sg_table *sgt)
37 {
38 struct device *dmadev;
39
40 if (!op->data.nbytes)
41 return -EINVAL;
42
43 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
44 dmadev = ctlr->dma_tx->device->dev;
45 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
46 dmadev = ctlr->dma_rx->device->dev;
47 else
48 dmadev = ctlr->dev.parent;
49
50 if (!dmadev)
51 return -EINVAL;
52
53 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
54 op->data.dir == SPI_MEM_DATA_IN ?
55 DMA_FROM_DEVICE : DMA_TO_DEVICE);
56 }
57 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
81 const struct spi_mem_op *op,
82 struct sg_table *sgt)
83 {
84 struct device *dmadev;
85
86 if (!op->data.nbytes)
87 return;
88
89 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
90 dmadev = ctlr->dma_tx->device->dev;
91 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
92 dmadev = ctlr->dma_rx->device->dev;
93 else
94 dmadev = ctlr->dev.parent;
95
96 spi_unmap_buf(ctlr, dmadev, sgt,
97 op->data.dir == SPI_MEM_DATA_IN ?
98 DMA_FROM_DEVICE : DMA_TO_DEVICE);
99 }
100 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
101
102 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx)
103 {
104 u32 mode = mem->spi->mode;
105
106 switch (buswidth) {
107 case 1:
108 return 0;
109
110 case 2:
111 if ((tx && (mode & (SPI_TX_DUAL | SPI_TX_QUAD))) ||
112 (!tx && (mode & (SPI_RX_DUAL | SPI_RX_QUAD))))
113 return 0;
114
115 break;
116
117 case 4:
118 if ((tx && (mode & SPI_TX_QUAD)) ||
119 (!tx && (mode & SPI_RX_QUAD)))
120 return 0;
121
122 break;
123
124 case 8:
125 if ((tx && (mode & SPI_TX_OCTAL)) ||
126 (!tx && (mode & SPI_RX_OCTAL)))
127 return 0;
128
129 break;
130
131 default:
132 break;
133 }
134
135 return -ENOTSUPP;
136 }
137
138 bool spi_mem_default_supports_op(struct spi_mem *mem,
139 const struct spi_mem_op *op)
140 {
141 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))
142 return false;
143
144 if (op->addr.nbytes &&
145 spi_check_buswidth_req(mem, op->addr.buswidth, true))
146 return false;
147
148 if (op->dummy.nbytes &&
149 spi_check_buswidth_req(mem, op->dummy.buswidth, true))
150 return false;
151
152 if (op->data.dir != SPI_MEM_NO_DATA &&
153 spi_check_buswidth_req(mem, op->data.buswidth,
154 op->data.dir == SPI_MEM_DATA_OUT))
155 return false;
156
157 return true;
158 }
159 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
160
161 static bool spi_mem_buswidth_is_valid(u8 buswidth)
162 {
163 if (hweight8(buswidth) > 1 || buswidth > SPI_MEM_MAX_BUSWIDTH)
164 return false;
165
166 return true;
167 }
168
169 static int spi_mem_check_op(const struct spi_mem_op *op)
170 {
171 if (!op->cmd.buswidth)
172 return -EINVAL;
173
174 if ((op->addr.nbytes && !op->addr.buswidth) ||
175 (op->dummy.nbytes && !op->dummy.buswidth) ||
176 (op->data.nbytes && !op->data.buswidth))
177 return -EINVAL;
178
179 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) ||
180 !spi_mem_buswidth_is_valid(op->addr.buswidth) ||
181 !spi_mem_buswidth_is_valid(op->dummy.buswidth) ||
182 !spi_mem_buswidth_is_valid(op->data.buswidth))
183 return -EINVAL;
184
185 return 0;
186 }
187
188 static bool spi_mem_internal_supports_op(struct spi_mem *mem,
189 const struct spi_mem_op *op)
190 {
191 struct spi_controller *ctlr = mem->spi->controller;
192
193 if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
194 return ctlr->mem_ops->supports_op(mem, op);
195
196 return spi_mem_default_supports_op(mem, op);
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
215 {
216 if (spi_mem_check_op(op))
217 return false;
218
219 return spi_mem_internal_supports_op(mem, op);
220 }
221 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
222
223 static int spi_mem_access_start(struct spi_mem *mem)
224 {
225 struct spi_controller *ctlr = mem->spi->controller;
226
227
228
229
230
231 spi_flush_queue(ctlr);
232
233 if (ctlr->auto_runtime_pm) {
234 int ret;
235
236 ret = pm_runtime_get_sync(ctlr->dev.parent);
237 if (ret < 0) {
238 dev_err(&ctlr->dev, "Failed to power device: %d\n",
239 ret);
240 return ret;
241 }
242 }
243
244 mutex_lock(&ctlr->bus_lock_mutex);
245 mutex_lock(&ctlr->io_mutex);
246
247 return 0;
248 }
249
250 static void spi_mem_access_end(struct spi_mem *mem)
251 {
252 struct spi_controller *ctlr = mem->spi->controller;
253
254 mutex_unlock(&ctlr->io_mutex);
255 mutex_unlock(&ctlr->bus_lock_mutex);
256
257 if (ctlr->auto_runtime_pm)
258 pm_runtime_put(ctlr->dev.parent);
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
274 {
275 unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
276 struct spi_controller *ctlr = mem->spi->controller;
277 struct spi_transfer xfers[4] = { };
278 struct spi_message msg;
279 u8 *tmpbuf;
280 int ret;
281
282 ret = spi_mem_check_op(op);
283 if (ret)
284 return ret;
285
286 if (!spi_mem_internal_supports_op(mem, op))
287 return -ENOTSUPP;
288
289 if (ctlr->mem_ops) {
290 ret = spi_mem_access_start(mem);
291 if (ret)
292 return ret;
293
294 ret = ctlr->mem_ops->exec_op(mem, op);
295
296 spi_mem_access_end(mem);
297
298
299
300
301
302
303 if (!ret || ret != -ENOTSUPP)
304 return ret;
305 }
306
307 tmpbufsize = sizeof(op->cmd.opcode) + op->addr.nbytes +
308 op->dummy.nbytes;
309
310
311
312
313
314
315 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
316 if (!tmpbuf)
317 return -ENOMEM;
318
319 spi_message_init(&msg);
320
321 tmpbuf[0] = op->cmd.opcode;
322 xfers[xferpos].tx_buf = tmpbuf;
323 xfers[xferpos].len = sizeof(op->cmd.opcode);
324 xfers[xferpos].tx_nbits = op->cmd.buswidth;
325 spi_message_add_tail(&xfers[xferpos], &msg);
326 xferpos++;
327 totalxferlen++;
328
329 if (op->addr.nbytes) {
330 int i;
331
332 for (i = 0; i < op->addr.nbytes; i++)
333 tmpbuf[i + 1] = op->addr.val >>
334 (8 * (op->addr.nbytes - i - 1));
335
336 xfers[xferpos].tx_buf = tmpbuf + 1;
337 xfers[xferpos].len = op->addr.nbytes;
338 xfers[xferpos].tx_nbits = op->addr.buswidth;
339 spi_message_add_tail(&xfers[xferpos], &msg);
340 xferpos++;
341 totalxferlen += op->addr.nbytes;
342 }
343
344 if (op->dummy.nbytes) {
345 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
346 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
347 xfers[xferpos].len = op->dummy.nbytes;
348 xfers[xferpos].tx_nbits = op->dummy.buswidth;
349 spi_message_add_tail(&xfers[xferpos], &msg);
350 xferpos++;
351 totalxferlen += op->dummy.nbytes;
352 }
353
354 if (op->data.nbytes) {
355 if (op->data.dir == SPI_MEM_DATA_IN) {
356 xfers[xferpos].rx_buf = op->data.buf.in;
357 xfers[xferpos].rx_nbits = op->data.buswidth;
358 } else {
359 xfers[xferpos].tx_buf = op->data.buf.out;
360 xfers[xferpos].tx_nbits = op->data.buswidth;
361 }
362
363 xfers[xferpos].len = op->data.nbytes;
364 spi_message_add_tail(&xfers[xferpos], &msg);
365 xferpos++;
366 totalxferlen += op->data.nbytes;
367 }
368
369 ret = spi_sync(mem->spi, &msg);
370
371 kfree(tmpbuf);
372
373 if (ret)
374 return ret;
375
376 if (msg.actual_length != totalxferlen)
377 return -EIO;
378
379 return 0;
380 }
381 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
382
383
384
385
386
387
388
389
390
391
392
393
394
395 const char *spi_mem_get_name(struct spi_mem *mem)
396 {
397 return mem->name;
398 }
399 EXPORT_SYMBOL_GPL(spi_mem_get_name);
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
417 {
418 struct spi_controller *ctlr = mem->spi->controller;
419 size_t len;
420
421 len = sizeof(op->cmd.opcode) + op->addr.nbytes + op->dummy.nbytes;
422
423 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
424 return ctlr->mem_ops->adjust_op_size(mem, op);
425
426 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
427 if (len > spi_max_transfer_size(mem->spi))
428 return -EINVAL;
429
430 op->data.nbytes = min3((size_t)op->data.nbytes,
431 spi_max_transfer_size(mem->spi),
432 spi_max_message_size(mem->spi) -
433 len);
434 if (!op->data.nbytes)
435 return -EINVAL;
436 }
437
438 return 0;
439 }
440 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
441
442 static ssize_t spi_mem_no_dirmap_read(struct spi_mem_dirmap_desc *desc,
443 u64 offs, size_t len, void *buf)
444 {
445 struct spi_mem_op op = desc->info.op_tmpl;
446 int ret;
447
448 op.addr.val = desc->info.offset + offs;
449 op.data.buf.in = buf;
450 op.data.nbytes = len;
451 ret = spi_mem_adjust_op_size(desc->mem, &op);
452 if (ret)
453 return ret;
454
455 ret = spi_mem_exec_op(desc->mem, &op);
456 if (ret)
457 return ret;
458
459 return op.data.nbytes;
460 }
461
462 static ssize_t spi_mem_no_dirmap_write(struct spi_mem_dirmap_desc *desc,
463 u64 offs, size_t len, const void *buf)
464 {
465 struct spi_mem_op op = desc->info.op_tmpl;
466 int ret;
467
468 op.addr.val = desc->info.offset + offs;
469 op.data.buf.out = buf;
470 op.data.nbytes = len;
471 ret = spi_mem_adjust_op_size(desc->mem, &op);
472 if (ret)
473 return ret;
474
475 ret = spi_mem_exec_op(desc->mem, &op);
476 if (ret)
477 return ret;
478
479 return op.data.nbytes;
480 }
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495 struct spi_mem_dirmap_desc *
496 spi_mem_dirmap_create(struct spi_mem *mem,
497 const struct spi_mem_dirmap_info *info)
498 {
499 struct spi_controller *ctlr = mem->spi->controller;
500 struct spi_mem_dirmap_desc *desc;
501 int ret = -ENOTSUPP;
502
503
504 if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8)
505 return ERR_PTR(-EINVAL);
506
507
508 if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA)
509 return ERR_PTR(-EINVAL);
510
511 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
512 if (!desc)
513 return ERR_PTR(-ENOMEM);
514
515 desc->mem = mem;
516 desc->info = *info;
517 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create)
518 ret = ctlr->mem_ops->dirmap_create(desc);
519
520 if (ret) {
521 desc->nodirmap = true;
522 if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl))
523 ret = -ENOTSUPP;
524 else
525 ret = 0;
526 }
527
528 if (ret) {
529 kfree(desc);
530 return ERR_PTR(ret);
531 }
532
533 return desc;
534 }
535 EXPORT_SYMBOL_GPL(spi_mem_dirmap_create);
536
537
538
539
540
541
542
543
544 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc)
545 {
546 struct spi_controller *ctlr = desc->mem->spi->controller;
547
548 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy)
549 ctlr->mem_ops->dirmap_destroy(desc);
550
551 kfree(desc);
552 }
553 EXPORT_SYMBOL_GPL(spi_mem_dirmap_destroy);
554
555 static void devm_spi_mem_dirmap_release(struct device *dev, void *res)
556 {
557 struct spi_mem_dirmap_desc *desc = *(struct spi_mem_dirmap_desc **)res;
558
559 spi_mem_dirmap_destroy(desc);
560 }
561
562
563
564
565
566
567
568
569
570
571
572
573
574 struct spi_mem_dirmap_desc *
575 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
576 const struct spi_mem_dirmap_info *info)
577 {
578 struct spi_mem_dirmap_desc **ptr, *desc;
579
580 ptr = devres_alloc(devm_spi_mem_dirmap_release, sizeof(*ptr),
581 GFP_KERNEL);
582 if (!ptr)
583 return ERR_PTR(-ENOMEM);
584
585 desc = spi_mem_dirmap_create(mem, info);
586 if (IS_ERR(desc)) {
587 devres_free(ptr);
588 } else {
589 *ptr = desc;
590 devres_add(dev, ptr);
591 }
592
593 return desc;
594 }
595 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_create);
596
597 static int devm_spi_mem_dirmap_match(struct device *dev, void *res, void *data)
598 {
599 struct spi_mem_dirmap_desc **ptr = res;
600
601 if (WARN_ON(!ptr || !*ptr))
602 return 0;
603
604 return *ptr == data;
605 }
606
607
608
609
610
611
612
613
614
615
616 void devm_spi_mem_dirmap_destroy(struct device *dev,
617 struct spi_mem_dirmap_desc *desc)
618 {
619 devres_release(dev, devm_spi_mem_dirmap_release,
620 devm_spi_mem_dirmap_match, desc);
621 }
622 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy);
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
641 u64 offs, size_t len, void *buf)
642 {
643 struct spi_controller *ctlr = desc->mem->spi->controller;
644 ssize_t ret;
645
646 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN)
647 return -EINVAL;
648
649 if (!len)
650 return 0;
651
652 if (desc->nodirmap) {
653 ret = spi_mem_no_dirmap_read(desc, offs, len, buf);
654 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) {
655 ret = spi_mem_access_start(desc->mem);
656 if (ret)
657 return ret;
658
659 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf);
660
661 spi_mem_access_end(desc->mem);
662 } else {
663 ret = -ENOTSUPP;
664 }
665
666 return ret;
667 }
668 EXPORT_SYMBOL_GPL(spi_mem_dirmap_read);
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
687 u64 offs, size_t len, const void *buf)
688 {
689 struct spi_controller *ctlr = desc->mem->spi->controller;
690 ssize_t ret;
691
692 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT)
693 return -EINVAL;
694
695 if (!len)
696 return 0;
697
698 if (desc->nodirmap) {
699 ret = spi_mem_no_dirmap_write(desc, offs, len, buf);
700 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) {
701 ret = spi_mem_access_start(desc->mem);
702 if (ret)
703 return ret;
704
705 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf);
706
707 spi_mem_access_end(desc->mem);
708 } else {
709 ret = -ENOTSUPP;
710 }
711
712 return ret;
713 }
714 EXPORT_SYMBOL_GPL(spi_mem_dirmap_write);
715
716 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
717 {
718 return container_of(drv, struct spi_mem_driver, spidrv.driver);
719 }
720
721 static int spi_mem_probe(struct spi_device *spi)
722 {
723 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
724 struct spi_controller *ctlr = spi->controller;
725 struct spi_mem *mem;
726
727 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
728 if (!mem)
729 return -ENOMEM;
730
731 mem->spi = spi;
732
733 if (ctlr->mem_ops && ctlr->mem_ops->get_name)
734 mem->name = ctlr->mem_ops->get_name(mem);
735 else
736 mem->name = dev_name(&spi->dev);
737
738 if (IS_ERR_OR_NULL(mem->name))
739 return PTR_ERR(mem->name);
740
741 spi_set_drvdata(spi, mem);
742
743 return memdrv->probe(mem);
744 }
745
746 static int spi_mem_remove(struct spi_device *spi)
747 {
748 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
749 struct spi_mem *mem = spi_get_drvdata(spi);
750
751 if (memdrv->remove)
752 return memdrv->remove(mem);
753
754 return 0;
755 }
756
757 static void spi_mem_shutdown(struct spi_device *spi)
758 {
759 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
760 struct spi_mem *mem = spi_get_drvdata(spi);
761
762 if (memdrv->shutdown)
763 memdrv->shutdown(mem);
764 }
765
766
767
768
769
770
771
772
773
774
775
776 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
777 struct module *owner)
778 {
779 memdrv->spidrv.probe = spi_mem_probe;
780 memdrv->spidrv.remove = spi_mem_remove;
781 memdrv->spidrv.shutdown = spi_mem_shutdown;
782
783 return __spi_register_driver(owner, &memdrv->spidrv);
784 }
785 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
786
787
788
789
790
791
792
793 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
794 {
795 spi_unregister_driver(&memdrv->spidrv);
796 }
797 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);