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