aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi
diff options
context:
space:
mode:
authorAnton Vorontsov <avorontsov@ru.mvista.com>2009-03-31 18:24:37 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2009-04-01 11:59:22 -0400
commit35b4b3c0c1265f1a7342574be393c157601401f0 (patch)
treeca12b33dbe911f1ba02cff197fcfb5928ab5763b /drivers/spi
parent364fdbc00fbdd409ade63500710123fe323aa164 (diff)
spi_mpc83xx: add OF platform driver bindings
Implement full support for OF SPI bindings. Now the driver can manage its own chip selects without any help from the board files and/or fsl_soc constructors. The "legacy" code is well isolated and could be removed as time goes by. Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com> Cc: David Brownell <david-b@pacbell.net> Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org> Cc: Kumar Gala <galak@gate.crashing.org> Cc: Grant Likely <grant.likely@secretlab.ca> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/spi')
-rw-r--r--drivers/spi/spi_mpc83xx.c330
1 files changed, 294 insertions, 36 deletions
diff --git a/drivers/spi/spi_mpc83xx.c b/drivers/spi/spi_mpc83xx.c
index b95085a46f90..f4573a96af24 100644
--- a/drivers/spi/spi_mpc83xx.c
+++ b/drivers/spi/spi_mpc83xx.c
@@ -14,6 +14,8 @@
14#include <linux/init.h> 14#include <linux/init.h>
15#include <linux/types.h> 15#include <linux/types.h>
16#include <linux/kernel.h> 16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/err.h>
17#include <linux/completion.h> 19#include <linux/completion.h>
18#include <linux/interrupt.h> 20#include <linux/interrupt.h>
19#include <linux/delay.h> 21#include <linux/delay.h>
@@ -23,7 +25,13 @@
23#include <linux/spi/spi_bitbang.h> 25#include <linux/spi/spi_bitbang.h>
24#include <linux/platform_device.h> 26#include <linux/platform_device.h>
25#include <linux/fsl_devices.h> 27#include <linux/fsl_devices.h>
28#include <linux/of.h>
29#include <linux/of_platform.h>
30#include <linux/gpio.h>
31#include <linux/of_gpio.h>
32#include <linux/of_spi.h>
26 33
34#include <sysdev/fsl_soc.h>
27#include <asm/irq.h> 35#include <asm/irq.h>
28#include <asm/io.h> 36#include <asm/io.h>
29 37
@@ -79,7 +87,7 @@ struct mpc83xx_spi {
79 u32(*get_tx) (struct mpc83xx_spi *); 87 u32(*get_tx) (struct mpc83xx_spi *);
80 88
81 unsigned int count; 89 unsigned int count;
82 int irq; 90 unsigned int irq;
83 91
84 unsigned nsecs; /* (clock cycle time)/2 */ 92 unsigned nsecs; /* (clock cycle time)/2 */
85 93
@@ -543,36 +551,23 @@ static void mpc83xx_spi_cleanup(struct spi_device *spi)
543 kfree(spi->controller_state); 551 kfree(spi->controller_state);
544} 552}
545 553
546static int __init mpc83xx_spi_probe(struct platform_device *dev) 554static struct spi_master * __devinit
555mpc83xx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
547{ 556{
557 struct fsl_spi_platform_data *pdata = dev->platform_data;
548 struct spi_master *master; 558 struct spi_master *master;
549 struct mpc83xx_spi *mpc83xx_spi; 559 struct mpc83xx_spi *mpc83xx_spi;
550 struct fsl_spi_platform_data *pdata;
551 struct resource *r;
552 u32 regval; 560 u32 regval;
553 int ret = 0; 561 int ret = 0;
554 562
555 /* Get resources(memory, IRQ) associated with the device */ 563 master = spi_alloc_master(dev, sizeof(struct mpc83xx_spi));
556 master = spi_alloc_master(&dev->dev, sizeof(struct mpc83xx_spi));
557
558 if (master == NULL) { 564 if (master == NULL) {
559 ret = -ENOMEM; 565 ret = -ENOMEM;
560 goto err; 566 goto err;
561 } 567 }
562 568
563 platform_set_drvdata(dev, master); 569 dev_set_drvdata(dev, master);
564 pdata = dev->dev.platform_data;
565
566 if (pdata == NULL) {
567 ret = -ENODEV;
568 goto free_master;
569 }
570 570
571 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
572 if (r == NULL) {
573 ret = -ENODEV;
574 goto free_master;
575 }
576 master->setup = mpc83xx_spi_setup; 571 master->setup = mpc83xx_spi_setup;
577 master->transfer = mpc83xx_spi_transfer; 572 master->transfer = mpc83xx_spi_transfer;
578 master->cleanup = mpc83xx_spi_cleanup; 573 master->cleanup = mpc83xx_spi_cleanup;
@@ -592,18 +587,13 @@ static int __init mpc83xx_spi_probe(struct platform_device *dev)
592 587
593 init_completion(&mpc83xx_spi->done); 588 init_completion(&mpc83xx_spi->done);
594 589
595 mpc83xx_spi->base = ioremap(r->start, r->end - r->start + 1); 590 mpc83xx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
596 if (mpc83xx_spi->base == NULL) { 591 if (mpc83xx_spi->base == NULL) {
597 ret = -ENOMEM; 592 ret = -ENOMEM;
598 goto put_master; 593 goto put_master;
599 } 594 }
600 595
601 mpc83xx_spi->irq = platform_get_irq(dev, 0); 596 mpc83xx_spi->irq = irq;
602
603 if (mpc83xx_spi->irq < 0) {
604 ret = -ENXIO;
605 goto unmap_io;
606 }
607 597
608 /* Register for SPI Interrupt */ 598 /* Register for SPI Interrupt */
609 ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq, 599 ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
@@ -645,9 +635,9 @@ static int __init mpc83xx_spi_probe(struct platform_device *dev)
645 635
646 printk(KERN_INFO 636 printk(KERN_INFO
647 "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n", 637 "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
648 dev_name(&dev->dev), mpc83xx_spi->base, mpc83xx_spi->irq); 638 dev_name(dev), mpc83xx_spi->base, mpc83xx_spi->irq);
649 639
650 return ret; 640 return master;
651 641
652unreg_master: 642unreg_master:
653 destroy_workqueue(mpc83xx_spi->workqueue); 643 destroy_workqueue(mpc83xx_spi->workqueue);
@@ -657,18 +647,16 @@ unmap_io:
657 iounmap(mpc83xx_spi->base); 647 iounmap(mpc83xx_spi->base);
658put_master: 648put_master:
659 spi_master_put(master); 649 spi_master_put(master);
660free_master:
661 kfree(master);
662err: 650err:
663 return ret; 651 return ERR_PTR(ret);
664} 652}
665 653
666static int __exit mpc83xx_spi_remove(struct platform_device *dev) 654static int __devexit mpc83xx_spi_remove(struct device *dev)
667{ 655{
668 struct mpc83xx_spi *mpc83xx_spi; 656 struct mpc83xx_spi *mpc83xx_spi;
669 struct spi_master *master; 657 struct spi_master *master;
670 658
671 master = platform_get_drvdata(dev); 659 master = dev_get_drvdata(dev);
672 mpc83xx_spi = spi_master_get_devdata(master); 660 mpc83xx_spi = spi_master_get_devdata(master);
673 661
674 flush_workqueue(mpc83xx_spi->workqueue); 662 flush_workqueue(mpc83xx_spi->workqueue);
@@ -681,23 +669,293 @@ static int __exit mpc83xx_spi_remove(struct platform_device *dev)
681 return 0; 669 return 0;
682} 670}
683 671
672struct mpc83xx_spi_probe_info {
673 struct fsl_spi_platform_data pdata;
674 int *gpios;
675 bool *alow_flags;
676};
677
678static struct mpc83xx_spi_probe_info *
679to_of_pinfo(struct fsl_spi_platform_data *pdata)
680{
681 return container_of(pdata, struct mpc83xx_spi_probe_info, pdata);
682}
683
684static void mpc83xx_spi_cs_control(struct spi_device *spi, bool on)
685{
686 struct device *dev = spi->dev.parent;
687 struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
688 u16 cs = spi->chip_select;
689 int gpio = pinfo->gpios[cs];
690 bool alow = pinfo->alow_flags[cs];
691
692 gpio_set_value(gpio, on ^ alow);
693}
694
695static int of_mpc83xx_spi_get_chipselects(struct device *dev)
696{
697 struct device_node *np = dev_archdata_get_node(&dev->archdata);
698 struct fsl_spi_platform_data *pdata = dev->platform_data;
699 struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(pdata);
700 unsigned int ngpios;
701 int i = 0;
702 int ret;
703
704 ngpios = of_gpio_count(np);
705 if (!ngpios) {
706 /*
707 * SPI w/o chip-select line. One SPI device is still permitted
708 * though.
709 */
710 pdata->max_chipselect = 1;
711 return 0;
712 }
713
714 pinfo->gpios = kmalloc(ngpios * sizeof(pinfo->gpios), GFP_KERNEL);
715 if (!pinfo->gpios)
716 return -ENOMEM;
717 memset(pinfo->gpios, -1, ngpios * sizeof(pinfo->gpios));
718
719 pinfo->alow_flags = kzalloc(ngpios * sizeof(pinfo->alow_flags),
720 GFP_KERNEL);
721 if (!pinfo->alow_flags) {
722 ret = -ENOMEM;
723 goto err_alloc_flags;
724 }
725
726 for (; i < ngpios; i++) {
727 int gpio;
728 enum of_gpio_flags flags;
729
730 gpio = of_get_gpio_flags(np, i, &flags);
731 if (!gpio_is_valid(gpio)) {
732 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
733 goto err_loop;
734 }
735
736 ret = gpio_request(gpio, dev_name(dev));
737 if (ret) {
738 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
739 goto err_loop;
740 }
741
742 pinfo->gpios[i] = gpio;
743 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
744
745 ret = gpio_direction_output(pinfo->gpios[i],
746 pinfo->alow_flags[i]);
747 if (ret) {
748 dev_err(dev, "can't set output direction for gpio "
749 "#%d: %d\n", i, ret);
750 goto err_loop;
751 }
752 }
753
754 pdata->max_chipselect = ngpios;
755 pdata->cs_control = mpc83xx_spi_cs_control;
756
757 return 0;
758
759err_loop:
760 while (i >= 0) {
761 if (gpio_is_valid(pinfo->gpios[i]))
762 gpio_free(pinfo->gpios[i]);
763 i--;
764 }
765
766 kfree(pinfo->alow_flags);
767 pinfo->alow_flags = NULL;
768err_alloc_flags:
769 kfree(pinfo->gpios);
770 pinfo->gpios = NULL;
771 return ret;
772}
773
774static int of_mpc83xx_spi_free_chipselects(struct device *dev)
775{
776 struct fsl_spi_platform_data *pdata = dev->platform_data;
777 struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(pdata);
778 int i;
779
780 if (!pinfo->gpios)
781 return 0;
782
783 for (i = 0; i < pdata->max_chipselect; i++) {
784 if (gpio_is_valid(pinfo->gpios[i]))
785 gpio_free(pinfo->gpios[i]);
786 }
787
788 kfree(pinfo->gpios);
789 kfree(pinfo->alow_flags);
790 return 0;
791}
792
793static int __devinit of_mpc83xx_spi_probe(struct of_device *ofdev,
794 const struct of_device_id *ofid)
795{
796 struct device *dev = &ofdev->dev;
797 struct device_node *np = ofdev->node;
798 struct mpc83xx_spi_probe_info *pinfo;
799 struct fsl_spi_platform_data *pdata;
800 struct spi_master *master;
801 struct resource mem;
802 struct resource irq;
803 const void *prop;
804 int ret = -ENOMEM;
805
806 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
807 if (!pinfo)
808 return -ENOMEM;
809
810 pdata = &pinfo->pdata;
811 dev->platform_data = pdata;
812
813 /* Allocate bus num dynamically. */
814 pdata->bus_num = -1;
815
816 /* SPI controller is either clocked from QE or SoC clock. */
817 pdata->sysclk = get_brgfreq();
818 if (pdata->sysclk == -1) {
819 pdata->sysclk = fsl_get_sys_freq();
820 if (pdata->sysclk == -1) {
821 ret = -ENODEV;
822 goto err_clk;
823 }
824 }
825
826 prop = of_get_property(np, "mode", NULL);
827 if (prop && !strcmp(prop, "cpu-qe"))
828 pdata->qe_mode = 1;
829
830 ret = of_mpc83xx_spi_get_chipselects(dev);
831 if (ret)
832 goto err;
833
834 ret = of_address_to_resource(np, 0, &mem);
835 if (ret)
836 goto err;
837
838 ret = of_irq_to_resource(np, 0, &irq);
839 if (!ret) {
840 ret = -EINVAL;
841 goto err;
842 }
843
844 master = mpc83xx_spi_probe(dev, &mem, irq.start);
845 if (IS_ERR(master)) {
846 ret = PTR_ERR(master);
847 goto err;
848 }
849
850 of_register_spi_devices(master, np);
851
852 return 0;
853
854err:
855 of_mpc83xx_spi_free_chipselects(dev);
856err_clk:
857 kfree(pinfo);
858 return ret;
859}
860
861static int __devexit of_mpc83xx_spi_remove(struct of_device *ofdev)
862{
863 int ret;
864
865 ret = mpc83xx_spi_remove(&ofdev->dev);
866 if (ret)
867 return ret;
868 of_mpc83xx_spi_free_chipselects(&ofdev->dev);
869 return 0;
870}
871
872static const struct of_device_id of_mpc83xx_spi_match[] = {
873 { .compatible = "fsl,spi" },
874 {},
875};
876MODULE_DEVICE_TABLE(of, of_mpc83xx_spi_match);
877
878static struct of_platform_driver of_mpc83xx_spi_driver = {
879 .name = "mpc83xx_spi",
880 .match_table = of_mpc83xx_spi_match,
881 .probe = of_mpc83xx_spi_probe,
882 .remove = __devexit_p(of_mpc83xx_spi_remove),
883};
884
885#ifdef CONFIG_MPC832x_RDB
886/*
887 * XXX XXX XXX
888 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
889 * only. The driver should go away soon, since newer MPC8323E-RDB's device
890 * tree can work with OpenFirmware driver. But for now we support old trees
891 * as well.
892 */
893static int __devinit plat_mpc83xx_spi_probe(struct platform_device *pdev)
894{
895 struct resource *mem;
896 unsigned int irq;
897 struct spi_master *master;
898
899 if (!pdev->dev.platform_data)
900 return -EINVAL;
901
902 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
903 if (!mem)
904 return -EINVAL;
905
906 irq = platform_get_irq(pdev, 0);
907 if (!irq)
908 return -EINVAL;
909
910 master = mpc83xx_spi_probe(&pdev->dev, mem, irq);
911 if (IS_ERR(master))
912 return PTR_ERR(master);
913 return 0;
914}
915
916static int __devexit plat_mpc83xx_spi_remove(struct platform_device *pdev)
917{
918 return mpc83xx_spi_remove(&pdev->dev);
919}
920
684MODULE_ALIAS("platform:mpc83xx_spi"); 921MODULE_ALIAS("platform:mpc83xx_spi");
685static struct platform_driver mpc83xx_spi_driver = { 922static struct platform_driver mpc83xx_spi_driver = {
686 .remove = __exit_p(mpc83xx_spi_remove), 923 .probe = plat_mpc83xx_spi_probe,
924 .remove = __exit_p(plat_mpc83xx_spi_remove),
687 .driver = { 925 .driver = {
688 .name = "mpc83xx_spi", 926 .name = "mpc83xx_spi",
689 .owner = THIS_MODULE, 927 .owner = THIS_MODULE,
690 }, 928 },
691}; 929};
692 930
931static bool legacy_driver_failed;
932
933static void __init legacy_driver_register(void)
934{
935 legacy_driver_failed = platform_driver_register(&mpc83xx_spi_driver);
936}
937
938static void __exit legacy_driver_unregister(void)
939{
940 if (legacy_driver_failed)
941 return;
942 platform_driver_unregister(&mpc83xx_spi_driver);
943}
944#else
945static void __init legacy_driver_register(void) {}
946static void __exit legacy_driver_unregister(void) {}
947#endif /* CONFIG_MPC832x_RDB */
948
693static int __init mpc83xx_spi_init(void) 949static int __init mpc83xx_spi_init(void)
694{ 950{
695 return platform_driver_probe(&mpc83xx_spi_driver, mpc83xx_spi_probe); 951 legacy_driver_register();
952 return of_register_platform_driver(&of_mpc83xx_spi_driver);
696} 953}
697 954
698static void __exit mpc83xx_spi_exit(void) 955static void __exit mpc83xx_spi_exit(void)
699{ 956{
700 platform_driver_unregister(&mpc83xx_spi_driver); 957 of_unregister_platform_driver(&of_mpc83xx_spi_driver);
958 legacy_driver_unregister();
701} 959}
702 960
703module_init(mpc83xx_spi_init); 961module_init(mpc83xx_spi_init);