add dts demo code for spi devices
authorluowei <lw@rock-chips.com>
Thu, 20 Mar 2014 03:27:53 +0000 (11:27 +0800)
committerluowei <lw@rock-chips.com>
Thu, 20 Mar 2014 03:28:20 +0000 (11:28 +0800)
arch/arm/boot/dts/rk3188-tb.dts
arch/arm/configs/rockchip_defconfig
drivers/spi/spi-rockchip-core.c
drivers/spi/spi-rockchip-test.c

index a9b5be87f7d3d5ed2f86eec05f2dff9eb62376e6..1f3b93a14ad8a29527548c702b293e7ad50aad1e 100755 (executable)
                compatible = "rockchip,vmac-phy";
                power-gpios = <&gpio0 GPIO_C0 GPIO_ACTIVE_HIGH>;
        };
+
 };
 
 
 
 &spi0 {
        status = "okay";
+       max-freq = <24000000>;  
+       /*
+       spi_test@00 {
+               compatible = "rockchip,spi_test_bus0_cs0";
+                reg = <0>;
+                spi-max-frequency = <12000000>;
+                poll_mode = <0>;
+               type = <0>;
+               enable_dma = <0>;
+
+       };
+
+       spi_test@01 {
+               compatible = "rockchip,spi_test_bus0_cs1";
+               reg = <1>;
+               spi-max-frequency = <12000000>;
+               poll_mode = <0>;
+               type = <0>;
+               enable_dma = <0>;               
+       };
+       */
 };
 
 &spi1 {
        status = "okay";
+       max-freq = <24000000>;
+       /*
+       spi_test@10 {
+               compatible = "rockchip,spi_test_bus1_cs0";
+               reg = <0>;
+               spi-max-frequency = <12000000>;
+               poll_mode = <0>;
+               type = <0>;
+               enable_dma = <0>;
+       };
+
+       spi_test@11 {
+               compatible = "rockchip,spi_test_bus1_cs1";
+               reg = <1>;
+               spi-max-frequency = <12000000>;
+               poll_mode = <0>;
+               type = <0>;
+               enable_dma = <0>;
+       };
+       */
 };
 
+
 &i2c0 {
        status = "okay";
        rt5631: rt5631@1a {
index 1abec66c0c9b343ac3d5d460ae4a80bccdfc058c..52a8d9e69d802e3ea2175b29f0401489683798e9 100755 (executable)
@@ -306,7 +306,6 @@ CONFIG_SPI=y
 CONFIG_SPI_ROCKCHIP_CORE=y
 CONFIG_SPI_ROCKCHIP=y
 CONFIG_SPI_ROCKCHIP_DMA=y
-CONFIG_SPI_ROCKCHIP_TEST=y
 CONFIG_DEBUG_GPIO=y
 CONFIG_GPIO_SYSFS=y
 CONFIG_THERMAL=y
index 71b34aeca78cc26276c21bbf87910ac80e13f233..b5a41a497ffbce70bdb4f973e422efa252c7079b 100755 (executable)
@@ -995,8 +995,10 @@ int dw_spi_add_host(struct dw_spi *dws)
                dev_err(&master->dev, "can not get IRQ\n");
                goto err_free_master;
        }
-
-       master->mode_bits = SPI_CPOL | SPI_CPHA;
+       
+       master->dev.parent = dws->parent_dev;
+       master->dev.of_node = dws->parent_dev->of_node; 
+       master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
        master->bus_num = dws->bus_num;
        master->num_chipselect = dws->num_cs;
        master->cleanup = dw_spi_cleanup;
index ba76112a8ad466f9ed96deac2510249b4a91057f..b4c5321ec5eecae15be9ddf129751e555096ece9 100755 (executable)
 \r
 #include "spi-rockchip-core.h"\r
 \r
+#define MAX_SPI_DEV_NUM 6\r
+#define SPI_MAX_SPEED_HZ       12000000\r
 \r
-#define MAX_SPI_BUS_NUM 2\r
 \r
 struct spi_test_data {\r
        struct device   *dev;\r
-       struct spi_device       *spi;   \r
+       struct spi_device       *spi;\r
        char *rx_buf;\r
        int rx_len; \r
        char *tx_buf;\r
        int tx_len; \r
 };\r
-static struct spi_test_data *g_spi_test_data[MAX_SPI_BUS_NUM];\r
 \r
+static struct spi_test_data *g_spi_test_data[MAX_SPI_DEV_NUM];\r
 \r
-static struct dw_spi_chip spi_test_chip[] = {\r
-{\r
-       //.poll_mode = 1,\r
-       //.enable_dma = 1,\r
-},\r
-{\r
-       //.poll_mode = 1,\r
-       //.enable_dma = 1,\r
-},\r
-\r
-};\r
-\r
-       \r
-static struct spi_board_info board_spi_test_devices[] = {      \r
-//#if defined(CONFIG_SPIM0_RK29)\r
-       {\r
-               .modalias  = "spi_test_bus0",\r
-               .bus_num = 0,   //0 or 1\r
-               .max_speed_hz  = 12*1000*1000,\r
-               .chip_select   = 0,             \r
-               .mode   = SPI_MODE_0,\r
-               .controller_data = &spi_test_chip[0],\r
-       },\r
-//#endif\r
-//#if defined(CONFIG_SPIM1_RK29)\r
-       {\r
-               .modalias  = "spi_test_bus1",\r
-               .bus_num = 1,   //0 or 1\r
-               .max_speed_hz  = 12*1000*1000,\r
-               .chip_select   = 0,             \r
-               .mode   = SPI_MODE_0,\r
-               .controller_data = &spi_test_chip[1],\r
-       }\r
-//#endif\r
-};\r
 \r
 static ssize_t spi_test_write(struct file *file, \r
                        const char __user *buf, size_t count, loff_t *offset)\r
@@ -90,7 +56,7 @@ static ssize_t spi_test_write(struct file *file,
 \r
        printk("%s:0:bus=0,cs=0; 1:bus=0,cs=1; 2:bus=1,cs=0; 3:bus=1,cs=1\n",__func__);\r
 \r
-       if(count > 3)\r
+       if(count > 5)\r
            return -EFAULT;\r
        \r
        ret = copy_from_user(nr_buf, buf, count);\r
@@ -98,7 +64,7 @@ static ssize_t spi_test_write(struct file *file,
            return -EFAULT;\r
 \r
        sscanf(nr_buf, "%d", &nr);\r
-       if(nr >= 4 || nr < 0)\r
+       if(nr >= 6 || nr < 0)\r
        {\r
                printk("%s:cmd is error\n",__func__);\r
            return -EFAULT;\r
@@ -107,51 +73,13 @@ static ssize_t spi_test_write(struct file *file,
        for(i=0; i<256; i++)\r
        txbuf[i] = i;\r
 \r
-\r
-/*\r
-       if((nr == 0) || (nr == 1))\r
+       if(!g_spi_test_data[nr] || !g_spi_test_data[nr]->spi)\r
        {\r
-               printk("%s:error SPIM0 need selected\n",__func__);      \r
+               printk("%s:error g_spi_test_data is null\n",__func__);          \r
                return -EFAULT;\r
        }\r
 \r
-       if((nr == 2) || (nr == 3))\r
-       {\r
-               printk("%s:error SPIM1 need selected\n",__func__);      \r
-               return -EFAULT;\r
-       }\r
-*/\r
-\r
-       switch(nr)\r
-       {\r
-               case 0: \r
-                       if(!g_spi_test_data[0]->spi)            \r
-                       return -EFAULT;\r
-                       spi = g_spi_test_data[0]->spi;\r
-                       spi->chip_select = 0;\r
-                       break;\r
-               case 1: \r
-                       if(!g_spi_test_data[0]->spi)            \r
-                       return -EFAULT;\r
-                       spi = g_spi_test_data[0]->spi;\r
-                       spi->chip_select = 1;\r
-                       break;\r
-               case 2: \r
-                       if(!g_spi_test_data[1]->spi)            \r
-                       return -EFAULT;\r
-                       spi = g_spi_test_data[1]->spi;\r
-                       spi->chip_select = 0;\r
-                       break;\r
-               case 3: \r
-                       if(!g_spi_test_data[1]->spi)            \r
-                       return -EFAULT;\r
-                       spi = g_spi_test_data[1]->spi;\r
-                       spi->chip_select = 1;\r
-                       break;\r
-               \r
-               default:\r
-                       break;\r
-       }\r
+       spi = g_spi_test_data[nr]->spi;\r
 \r
        for(i=0; i<100; i++)\r
        {\r
@@ -181,19 +109,65 @@ static struct miscdevice spi_test_misc = {
        .fops = &spi_test_fops,\r
 };\r
 \r
-static int spi_test_probe(struct spi_device *spi)\r
+#ifdef CONFIG_OF\r
+static struct dw_spi_chip *rockchip_spi_parse_dt(struct device *dev)\r
+{\r
+       u32 temp;\r
+       struct dw_spi_chip *spi_chip_data; \r
+\r
+       spi_chip_data = devm_kzalloc(dev, sizeof(*spi_chip_data), GFP_KERNEL);\r
+       if (!spi_chip_data) {\r
+               dev_err(dev, "memory allocation for spi_chip_data failed\n");\r
+               return ERR_PTR(-ENOMEM);\r
+       }\r
+       \r
+       if (of_property_read_u32(dev->of_node, "poll_mode", &temp)) {\r
+               dev_warn(dev, "fail to get poll_mode, default set 0\n");\r
+               spi_chip_data->poll_mode = 0;\r
+       } else {\r
+               spi_chip_data->poll_mode = temp;\r
+       }\r
+\r
+       if (of_property_read_u32(dev->of_node, "type", &temp)) {\r
+               dev_warn(dev, "fail to get type, default set 0\n");\r
+               spi_chip_data->type = 0;\r
+       } else {\r
+               spi_chip_data->type = temp;\r
+       }\r
+\r
+       if (of_property_read_u32(dev->of_node, "enable_dma", &temp)) {\r
+               dev_warn(dev, "fail to get enable_dma, default set 0\n");\r
+               spi_chip_data->enable_dma = 0;\r
+       } else {\r
+               spi_chip_data->enable_dma = temp;\r
+       }\r
+       \r
+\r
+       return spi_chip_data;\r
+}\r
+#else\r
+static struct spi_board_info *rockchip_spi_parse_dt(struct device *dev)\r
+{\r
+       return dev->platform_data;\r
+}\r
+#endif\r
+\r
+\r
+static int rockchip_spi_test_probe(struct spi_device *spi)\r
 {      \r
-       struct spi_test_data *spi_test_data;\r
        int ret;\r
        int i =0;\r
-       char txbuf[256],rxbuf[256];\r
+       char txbuf[256],rxbuf[256];     \r
+       int id = 0;\r
+       static struct dw_spi_chip *spi_chip_data;\r
+       struct spi_test_data *spi_test_data;\r
        \r
        if(!spi)        \r
        return -ENOMEM;\r
 \r
-       if((spi->master->bus_num >= MAX_SPI_BUS_NUM) || (spi->master->bus_num < 0))\r
-       {\r
-               printk("%s:error:bus_num=%d\n",__func__, spi->master->bus_num); \r
+       if (!spi_chip_data && spi->dev.of_node) {\r
+               spi_chip_data = rockchip_spi_parse_dt(&spi->dev);\r
+               if (IS_ERR(spi_chip_data))\r
                return -ENOMEM;\r
        }\r
        \r
@@ -203,54 +177,87 @@ static int spi_test_probe(struct spi_device *spi)
                return -ENOMEM;
        }\r
 \r
-       spi->bits_per_word = 8;\r
+       spi->bits_per_word = 8; \r
+       spi->controller_data = spi_chip_data;\r
        \r
        spi_test_data->spi = spi;\r
        spi_test_data->dev = &spi->dev;\r
+       \r
        ret = spi_setup(spi);\r
        if (ret < 0){\r
                dev_err(spi_test_data->dev, "ERR: fail to setup spi\n");\r
                return -1;\r
        }       \r
 \r
-       g_spi_test_data[spi->master->bus_num] = spi_test_data;\r
+       if((spi->master->bus_num == 0) && (spi->chip_select == 0))\r
+               id = 0;\r
+       else if((spi->master->bus_num == 0) && (spi->chip_select == 1))\r
+               id = 1;\r
+       else if ((spi->master->bus_num == 1) && (spi->chip_select == 0))\r
+               id = 2;\r
+       else if ((spi->master->bus_num == 1) && (spi->chip_select == 1))\r
+               id = 3;\r
+       else if ((spi->master->bus_num == 2) && (spi->chip_select == 0))\r
+               id = 4;\r
+       else if ((spi->master->bus_num == 2) && (spi->chip_select == 1))\r
+               id = 5;\r
+\r
+       g_spi_test_data[id] = spi_test_data;\r
+               \r
+       printk("%s:name=%s,bus_num=%d,cs=%d,mode=%d,speed=%d\n",__func__,spi->modalias, spi->master->bus_num, spi->chip_select, spi->mode, spi->max_speed_hz);\r
 \r
-       printk("%s:bus_num=%d,ok\n",__func__,spi->master->bus_num);\r
+       printk("%s:poll_mode=%d, type=%d, enable_dma=%d\n",__func__, spi_chip_data->poll_mode, spi_chip_data->type, spi_chip_data->enable_dma);\r
        return ret;\r
 \r
 }\r
 \r
-static const struct spi_device_id spi_test_id[] = {            \r
-       {"spi_test_bus0", 0},\r
-       {"spi_test_bus1", 1},\r
+static int rockchip_spi_test_remove(struct spi_device *spi)\r
+{\r
+       printk("%s\n",__func__);\r
+       return 0;\r
+}\r
+\r
+#ifdef CONFIG_OF\r
+static const struct of_device_id rockchip_spi_test_dt_match[] = {\r
+       { .compatible = "rockchip,spi_test_bus0_cs0", },\r
+       { .compatible = "rockchip,spi_test_bus0_cs1", },\r
+       { .compatible = "rockchip,spi_test_bus1_cs0", },\r
+       { .compatible = "rockchip,spi_test_bus1_cs1", },\r
        {},\r
 };\r
+MODULE_DEVICE_TABLE(of, rockchip_spi_test_dt_match);\r
 \r
+#endif /* CONFIG_OF */\r
 \r
-static struct spi_driver spi_test_driver = {\r
+static struct spi_driver spi_rockchip_test_driver = {\r
        .driver = {\r
-               .name           = "spi_test",\r
-               .bus            = &spi_bus_type,\r
-               .owner          = THIS_MODULE,\r
+               .name   = "spi_test",\r
+               .owner = THIS_MODULE,\r
+               .of_match_table = of_match_ptr(rockchip_spi_test_dt_match),\r
        },\r
-       .id_table = spi_test_id,\r
-\r
-       .probe          = spi_test_probe,\r
+       .probe = rockchip_spi_test_probe,\r
+       .remove = rockchip_spi_test_remove,\r
 };\r
 \r
-static int __init spi_test_init(void)\r
+static int __init spi_rockchip_test_init(void)\r
 {      \r
-       printk("%s\n",__func__);\r
-       spi_register_board_info(board_spi_test_devices, ARRAY_SIZE(board_spi_test_devices));\r
+       int ret= 0;\r
        misc_register(&spi_test_misc);\r
-       return spi_register_driver(&spi_test_driver);\r
+       ret = spi_register_driver(&spi_rockchip_test_driver);\r
+       \r
+       return ret;\r
 }\r
+module_init(spi_rockchip_test_init);\r
 \r
-static void __exit spi_test_exit(void)\r
+static void __exit spi_rockchip_test_exit(void)\r
 {\r
-        misc_deregister(&spi_test_misc);\r
-       return spi_unregister_driver(&spi_test_driver);\r
+       misc_deregister(&spi_test_misc);\r
+       return spi_unregister_driver(&spi_rockchip_test_driver);\r
 }\r
-module_init(spi_test_init);\r
-module_exit(spi_test_exit);\r
+module_exit(spi_rockchip_test_exit);\r
+\r
+MODULE_AUTHOR("Luo Wei <lw@rock-chips.com>");\r
+MODULE_DESCRIPTION("ROCKCHIP SPI TEST Driver");\r
+MODULE_LICENSE("GPL");\r
+MODULE_ALIAS("spi:spi_test");\r
 \r