1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/kernel.h>
22#include <linux/device.h>
23#include <linux/init.h>
24#include <linux/cache.h>
25#include <linux/mutex.h>
26#include <linux/slab.h>
27#include <linux/mod_devicetable.h>
28#include <linux/spi/spi.h>
29
30
31
32
33
34
35static void spidev_release(struct device *dev)
36{
37 struct spi_device *spi = to_spi_device(dev);
38
39
40 if (spi->master->cleanup)
41 spi->master->cleanup(spi);
42
43 spi_master_put(spi->master);
44 kfree(spi);
45}
46
47static ssize_t
48modalias_show(struct device *dev, struct device_attribute *a, char *buf)
49{
50 const struct spi_device *spi = to_spi_device(dev);
51
52 return sprintf(buf, "%s\n", spi->modalias);
53}
54
55static struct device_attribute spi_dev_attrs[] = {
56 __ATTR_RO(modalias),
57 __ATTR_NULL,
58};
59
60
61
62
63
64static const struct spi_device_id *spi_match_id(const struct spi_device_id *id,
65 const struct spi_device *sdev)
66{
67 while (id->name[0]) {
68 if (!strcmp(sdev->modalias, id->name))
69 return id;
70 id++;
71 }
72 return NULL;
73}
74
75const struct spi_device_id *spi_get_device_id(const struct spi_device *sdev)
76{
77 const struct spi_driver *sdrv = to_spi_driver(sdev->dev.driver);
78
79 return spi_match_id(sdrv->id_table, sdev);
80}
81EXPORT_SYMBOL_GPL(spi_get_device_id);
82
83static int spi_match_device(struct device *dev, struct device_driver *drv)
84{
85 const struct spi_device *spi = to_spi_device(dev);
86 const struct spi_driver *sdrv = to_spi_driver(drv);
87
88 if (sdrv->id_table)
89 return !!spi_match_id(sdrv->id_table, spi);
90
91 return strcmp(spi->modalias, drv->name) == 0;
92}
93
94static int spi_uevent(struct device *dev, struct kobj_uevent_env *env)
95{
96 const struct spi_device *spi = to_spi_device(dev);
97
98 add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias);
99 return 0;
100}
101
102#ifdef CONFIG_PM
103
104static int spi_suspend(struct device *dev, pm_message_t message)
105{
106 int value = 0;
107 struct spi_driver *drv = to_spi_driver(dev->driver);
108
109
110 if (drv) {
111 if (drv->suspend)
112 value = drv->suspend(to_spi_device(dev), message);
113 else
114 dev_dbg(dev, "... can't suspend\n");
115 }
116 return value;
117}
118
119static int spi_resume(struct device *dev)
120{
121 int value = 0;
122 struct spi_driver *drv = to_spi_driver(dev->driver);
123
124
125 if (drv) {
126 if (drv->resume)
127 value = drv->resume(to_spi_device(dev));
128 else
129 dev_dbg(dev, "... can't resume\n");
130 }
131 return value;
132}
133
134#else
135#define spi_suspend NULL
136#define spi_resume NULL
137#endif
138
139struct bus_type spi_bus_type = {
140 .name = "spi",
141 .dev_attrs = spi_dev_attrs,
142 .match = spi_match_device,
143 .uevent = spi_uevent,
144 .suspend = spi_suspend,
145 .resume = spi_resume,
146};
147EXPORT_SYMBOL_GPL(spi_bus_type);
148
149
150static int spi_drv_probe(struct device *dev)
151{
152 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
153
154 return sdrv->probe(to_spi_device(dev));
155}
156
157static int spi_drv_remove(struct device *dev)
158{
159 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
160
161 return sdrv->remove(to_spi_device(dev));
162}
163
164static void spi_drv_shutdown(struct device *dev)
165{
166 const struct spi_driver *sdrv = to_spi_driver(dev->driver);
167
168 sdrv->shutdown(to_spi_device(dev));
169}
170
171
172
173
174
175
176int spi_register_driver(struct spi_driver *sdrv)
177{
178 sdrv->driver.bus = &spi_bus_type;
179 if (sdrv->probe)
180 sdrv->driver.probe = spi_drv_probe;
181 if (sdrv->remove)
182 sdrv->driver.remove = spi_drv_remove;
183 if (sdrv->shutdown)
184 sdrv->driver.shutdown = spi_drv_shutdown;
185 return driver_register(&sdrv->driver);
186}
187EXPORT_SYMBOL_GPL(spi_register_driver);
188
189
190
191
192
193
194
195
196
197struct boardinfo {
198 struct list_head list;
199 unsigned n_board_info;
200 struct spi_board_info board_info[0];
201};
202
203static LIST_HEAD(board_list);
204static DEFINE_MUTEX(board_lock);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223struct spi_device *spi_alloc_device(struct spi_master *master)
224{
225 struct spi_device *spi;
226 struct device *dev = master->dev.parent;
227
228 if (!spi_master_get(master))
229 return NULL;
230
231 spi = kzalloc(sizeof *spi, GFP_KERNEL);
232 if (!spi) {
233 dev_err(dev, "cannot alloc spi_device\n");
234 spi_master_put(master);
235 return NULL;
236 }
237
238 spi->master = master;
239 spi->dev.parent = dev;
240 spi->dev.bus = &spi_bus_type;
241 spi->dev.release = spidev_release;
242 device_initialize(&spi->dev);
243 return spi;
244}
245EXPORT_SYMBOL_GPL(spi_alloc_device);
246
247
248
249
250
251
252
253
254
255
256int spi_add_device(struct spi_device *spi)
257{
258 static DEFINE_MUTEX(spi_add_lock);
259 struct device *dev = spi->master->dev.parent;
260 struct device *d;
261 int status;
262
263
264 if (spi->chip_select >= spi->master->num_chipselect) {
265 dev_err(dev, "cs%d >= max %d\n",
266 spi->chip_select,
267 spi->master->num_chipselect);
268 return -EINVAL;
269 }
270
271
272 dev_set_name(&spi->dev, "%s.%u", dev_name(&spi->master->dev),
273 spi->chip_select);
274
275
276
277
278
279
280 mutex_lock(&spi_add_lock);
281
282 d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev));
283 if (d != NULL) {
284 dev_err(dev, "chipselect %d already in use\n",
285 spi->chip_select);
286 put_device(d);
287 status = -EBUSY;
288 goto done;
289 }
290
291
292
293
294
295 status = spi_setup(spi);
296 if (status < 0) {
297 dev_err(dev, "can't %s %s, status %d\n",
298 "setup", dev_name(&spi->dev), status);
299 goto done;
300 }
301
302
303 status = device_add(&spi->dev);
304 if (status < 0)
305 dev_err(dev, "can't %s %s, status %d\n",
306 "add", dev_name(&spi->dev), status);
307 else
308 dev_dbg(dev, "registered child %s\n", dev_name(&spi->dev));
309
310done:
311 mutex_unlock(&spi_add_lock);
312 return status;
313}
314EXPORT_SYMBOL_GPL(spi_add_device);
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330struct spi_device *spi_new_device(struct spi_master *master,
331 struct spi_board_info *chip)
332{
333 struct spi_device *proxy;
334 int status;
335
336
337
338
339
340
341
342
343 proxy = spi_alloc_device(master);
344 if (!proxy)
345 return NULL;
346
347 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias));
348
349 proxy->chip_select = chip->chip_select;
350 proxy->max_speed_hz = chip->max_speed_hz;
351 proxy->mode = chip->mode;
352 proxy->irq = chip->irq;
353 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias));
354 proxy->dev.platform_data = (void *) chip->platform_data;
355 proxy->controller_data = chip->controller_data;
356 proxy->controller_state = NULL;
357
358 status = spi_add_device(proxy);
359 if (status < 0) {
360 spi_dev_put(proxy);
361 return NULL;
362 }
363
364 return proxy;
365}
366EXPORT_SYMBOL_GPL(spi_new_device);
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387int __init
388spi_register_board_info(struct spi_board_info const *info, unsigned n)
389{
390 struct boardinfo *bi;
391
392 bi = kmalloc(sizeof(*bi) + n * sizeof *info, GFP_KERNEL);
393 if (!bi)
394 return -ENOMEM;
395 bi->n_board_info = n;
396 memcpy(bi->board_info, info, n * sizeof *info);
397
398 mutex_lock(&board_lock);
399 list_add_tail(&bi->list, &board_list);
400 mutex_unlock(&board_lock);
401 return 0;
402}
403
404
405
406
407
408static void scan_boardinfo(struct spi_master *master)
409{
410 struct boardinfo *bi;
411
412 mutex_lock(&board_lock);
413 list_for_each_entry(bi, &board_list, list) {
414 struct spi_board_info *chip = bi->board_info;
415 unsigned n;
416
417 for (n = bi->n_board_info; n > 0; n--, chip++) {
418 if (chip->bus_num != master->bus_num)
419 continue;
420
421
422
423 (void) spi_new_device(master, chip);
424 }
425 }
426 mutex_unlock(&board_lock);
427}
428
429
430
431static void spi_master_release(struct device *dev)
432{
433 struct spi_master *master;
434
435 master = container_of(dev, struct spi_master, dev);
436 kfree(master);
437}
438
439static struct class spi_master_class = {
440 .name = "spi_master",
441 .owner = THIS_MODULE,
442 .dev_release = spi_master_release,
443};
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465struct spi_master *spi_alloc_master(struct device *dev, unsigned size)
466{
467 struct spi_master *master;
468
469 if (!dev)
470 return NULL;
471
472 master = kzalloc(size + sizeof *master, GFP_KERNEL);
473 if (!master)
474 return NULL;
475
476 device_initialize(&master->dev);
477 master->dev.class = &spi_master_class;
478 master->dev.parent = get_device(dev);
479 spi_master_set_devdata(master, &master[1]);
480
481 return master;
482}
483EXPORT_SYMBOL_GPL(spi_alloc_master);
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505int spi_register_master(struct spi_master *master)
506{
507 static atomic_t dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
508 struct device *dev = master->dev.parent;
509 int status = -ENODEV;
510 int dynamic = 0;
511
512 if (!dev)
513 return -ENODEV;
514
515
516
517
518 if (master->num_chipselect == 0)
519 return -EINVAL;
520
521
522 if (master->bus_num < 0) {
523
524
525
526 master->bus_num = atomic_dec_return(&dyn_bus_id);
527 dynamic = 1;
528 }
529
530
531
532
533 dev_set_name(&master->dev, "spi%u", master->bus_num);
534 status = device_add(&master->dev);
535 if (status < 0)
536 goto done;
537 dev_dbg(dev, "registered master %s%s\n", dev_name(&master->dev),
538 dynamic ? " (dynamic)" : "");
539
540
541 scan_boardinfo(master);
542 status = 0;
543done:
544 return status;
545}
546EXPORT_SYMBOL_GPL(spi_register_master);
547
548
549static int __unregister(struct device *dev, void *master_dev)
550{
551
552 if (dev != master_dev)
553 spi_unregister_device(to_spi_device(dev));
554 return 0;
555}
556
557
558
559
560
561
562
563
564
565
566
567void spi_unregister_master(struct spi_master *master)
568{
569 int dummy;
570
571 dummy = device_for_each_child(master->dev.parent, &master->dev,
572 __unregister);
573 device_unregister(&master->dev);
574}
575EXPORT_SYMBOL_GPL(spi_unregister_master);
576
577static int __spi_master_match(struct device *dev, void *data)
578{
579 struct spi_master *m;
580 u16 *bus_num = data;
581
582 m = container_of(dev, struct spi_master, dev);
583 return m->bus_num == *bus_num;
584}
585
586
587
588
589
590
591
592
593
594
595
596struct spi_master *spi_busnum_to_master(u16 bus_num)
597{
598 struct device *dev;
599 struct spi_master *master = NULL;
600
601 dev = class_find_device(&spi_master_class, NULL, &bus_num,
602 __spi_master_match);
603 if (dev)
604 master = container_of(dev, struct spi_master, dev);
605
606 return master;
607}
608EXPORT_SYMBOL_GPL(spi_busnum_to_master);
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635int spi_setup(struct spi_device *spi)
636{
637 unsigned bad_bits;
638 int status;
639
640
641
642
643 bad_bits = spi->mode & ~spi->master->mode_bits;
644 if (bad_bits) {
645 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
646 bad_bits);
647 return -EINVAL;
648 }
649
650 if (!spi->bits_per_word)
651 spi->bits_per_word = 8;
652
653 status = spi->master->setup(spi);
654
655 dev_dbg(&spi->dev, "setup mode %d, %s%s%s%s"
656 "%u bits/w, %u Hz max --> %d\n",
657 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
658 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
659 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
660 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
661 (spi->mode & SPI_LOOP) ? "loopback, " : "",
662 spi->bits_per_word, spi->max_speed_hz,
663 status);
664
665 return status;
666}
667EXPORT_SYMBOL_GPL(spi_setup);
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698int spi_async(struct spi_device *spi, struct spi_message *message)
699{
700 struct spi_master *master = spi->master;
701
702
703
704
705
706
707 if ((master->flags & SPI_MASTER_HALF_DUPLEX)
708 || (spi->mode & SPI_3WIRE)) {
709 struct spi_transfer *xfer;
710 unsigned flags = master->flags;
711
712 list_for_each_entry(xfer, &message->transfers, transfer_list) {
713 if (xfer->rx_buf && xfer->tx_buf)
714 return -EINVAL;
715 if ((flags & SPI_MASTER_NO_TX) && xfer->tx_buf)
716 return -EINVAL;
717 if ((flags & SPI_MASTER_NO_RX) && xfer->rx_buf)
718 return -EINVAL;
719 }
720 }
721
722 message->spi = spi;
723 message->status = -EINPROGRESS;
724 return master->transfer(spi, message);
725}
726EXPORT_SYMBOL_GPL(spi_async);
727
728
729
730
731
732
733
734
735
736static void spi_complete(void *arg)
737{
738 complete(arg);
739}
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762int spi_sync(struct spi_device *spi, struct spi_message *message)
763{
764 DECLARE_COMPLETION_ONSTACK(done);
765 int status;
766
767 message->complete = spi_complete;
768 message->context = &done;
769 status = spi_async(spi, message);
770 if (status == 0) {
771 wait_for_completion(&done);
772 status = message->status;
773 }
774 message->context = NULL;
775 return status;
776}
777EXPORT_SYMBOL_GPL(spi_sync);
778
779
780#define SPI_BUFSIZ max(32,SMP_CACHE_BYTES)
781
782static u8 *buf;
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803int spi_write_then_read(struct spi_device *spi,
804 const u8 *txbuf, unsigned n_tx,
805 u8 *rxbuf, unsigned n_rx)
806{
807 static DEFINE_MUTEX(lock);
808
809 int status;
810 struct spi_message message;
811 struct spi_transfer x[2];
812 u8 *local_buf;
813
814
815
816
817
818 if ((n_tx + n_rx) > SPI_BUFSIZ)
819 return -EINVAL;
820
821 spi_message_init(&message);
822 memset(x, 0, sizeof x);
823 if (n_tx) {
824 x[0].len = n_tx;
825 spi_message_add_tail(&x[0], &message);
826 }
827 if (n_rx) {
828 x[1].len = n_rx;
829 spi_message_add_tail(&x[1], &message);
830 }
831
832
833 if (!mutex_trylock(&lock)) {
834 local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
835 if (!local_buf)
836 return -ENOMEM;
837 } else
838 local_buf = buf;
839
840 memcpy(local_buf, txbuf, n_tx);
841 x[0].tx_buf = local_buf;
842 x[1].rx_buf = local_buf + n_tx;
843
844
845 status = spi_sync(spi, &message);
846 if (status == 0)
847 memcpy(rxbuf, x[1].rx_buf, n_rx);
848
849 if (x[0].tx_buf == buf)
850 mutex_unlock(&lock);
851 else
852 kfree(local_buf);
853
854 return status;
855}
856EXPORT_SYMBOL_GPL(spi_write_then_read);
857
858
859
860static int __init spi_init(void)
861{
862 int status;
863
864 buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
865 if (!buf) {
866 status = -ENOMEM;
867 goto err0;
868 }
869
870 status = bus_register(&spi_bus_type);
871 if (status < 0)
872 goto err1;
873
874 status = class_register(&spi_master_class);
875 if (status < 0)
876 goto err2;
877 return 0;
878
879err2:
880 bus_unregister(&spi_bus_type);
881err1:
882 kfree(buf);
883 buf = NULL;
884err0:
885 return status;
886}
887
888
889
890
891
892
893
894
895postcore_initcall(spi_init);
896
897