From b3f70746c8b1d51c299ac745a742513bfcdd089c Mon Sep 17 00:00:00 2001 From: luowei Date: Wed, 28 Aug 2013 16:54:02 +0800 Subject: [PATCH] improve spi_test to support SPI0 and SPI1 test --- drivers/spi/spi_test.c | 133 +++++++++++++++++++++++++++++++---------- 1 file changed, 103 insertions(+), 30 deletions(-) diff --git a/drivers/spi/spi_test.c b/drivers/spi/spi_test.c index cfba557f7dd2..3b1fec729540 100755 --- a/drivers/spi/spi_test.c +++ b/drivers/spi/spi_test.c @@ -24,6 +24,8 @@ #include #include +#define MAX_SPI_BUS_NUM 2 + struct spi_test_data { struct device *dev; struct spi_device *spi; @@ -32,62 +34,117 @@ struct spi_test_data { char *tx_buf; int tx_len; }; -static struct spi_test_data *g_spi_test_data; +static struct spi_test_data *g_spi_test_data[MAX_SPI_BUS_NUM]; -static struct rk29xx_spi_chip spi_test_chip = { +static struct rk29xx_spi_chip spi_test_chip[] = { +{ + //.poll_mode = 1, + .enable_dma = 1, +}, +{ //.poll_mode = 1, .enable_dma = 1, +}, + }; static struct spi_board_info board_spi_test_devices[] = { +#if defined(CONFIG_SPIM0_RK29) { - .modalias = "spi_test", + .modalias = "spi_test_bus0", .bus_num = 0, //0 or 1 .max_speed_hz = 12*1000*1000, .chip_select = 0, - .controller_data = &spi_test_chip, - }, - + .mode = SPI_MODE_0, + .controller_data = &spi_test_chip[0], + }, +#endif +#if defined(CONFIG_SPIM1_RK29) + { + .modalias = "spi_test_bus1", + .bus_num = 1, //0 or 1 + .max_speed_hz = 12*1000*1000, + .chip_select = 0, + .mode = SPI_MODE_0, + .controller_data = &spi_test_chip[1], + } +#endif }; static ssize_t spi_test_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) { - char nr_buf[8]; - int nr = 0, ret; + char nr_buf[8]; + int nr = 0, ret; int i = 0; - struct spi_device *spi = g_spi_test_data->spi; + struct spi_device *spi = NULL; char txbuf[256],rxbuf[256]; - if(count > 3) - return -EFAULT; - ret = copy_from_user(nr_buf, buf, count); - if(ret < 0) - return -EFAULT; - - sscanf(nr_buf, "%d", &nr); - if(nr >= 2 || nr < 0) - { - printk("%s:data is error\n",__func__); - return -EFAULT; - } + printk("%s:0:bus=0,cs=0; 1:bus=0,cs=1; 2:bus=1,cs=0; 3:bus=1,cs=1\n",__func__); + + if(count > 3) + return -EFAULT; + ret = copy_from_user(nr_buf, buf, count); + if(ret < 0) + return -EFAULT; + + sscanf(nr_buf, "%d", &nr); + if(nr >= 4 || nr < 0) + { + printk("%s:cmd is error\n",__func__); + return -EFAULT; + } for(i=0; i<256; i++) txbuf[i] = i; + +#if !defined(CONFIG_SPIM0_RK29) + if((nr == 0) || (nr == 1)) + { + printk("%s:error SPIM0 need selected\n",__func__); + return -EFAULT; + } +#endif + +#if !defined(CONFIG_SPIM1_RK29) + if((nr == 2) || (nr == 3)) + { + printk("%s:error SPIM1 need selected\n",__func__); + return -EFAULT; + } +#endif + switch(nr) { - case 0: + case 0: + if(!g_spi_test_data[0]->spi) + return -EFAULT; + spi = g_spi_test_data[0]->spi; spi->chip_select = 0; break; - case 1: + case 1: + if(!g_spi_test_data[0]->spi) + return -EFAULT; + spi = g_spi_test_data[0]->spi; spi->chip_select = 1; break; - + case 2: + if(!g_spi_test_data[1]->spi) + return -EFAULT; + spi = g_spi_test_data[1]->spi; + spi->chip_select = 0; + break; + case 3: + if(!g_spi_test_data[1]->spi) + return -EFAULT; + spi = g_spi_test_data[1]->spi; + spi->chip_select = 1; + break; + default: break; - } for(i=0; i<10; i++) @@ -99,11 +156,11 @@ static ssize_t spi_test_write(struct file *file, } if(!ret) - printk("%s:ok\n",__func__); + printk("%s:bus_num=%d,chip_select=%d,ok\n",__func__,spi->master->bus_num, spi->chip_select); else - printk("%s:error\n",__func__); + printk("%s:bus_num=%d,chip_select=%d,error\n",__func__,spi->master->bus_num, spi->chip_select); - return count; + return count; } @@ -121,6 +178,15 @@ static int __devinit spi_test_probe(struct spi_device *spi) { struct spi_test_data *spi_test_data; int ret; + + if(!spi) + return -ENOMEM; + + if((spi->master->bus_num >= MAX_SPI_BUS_NUM) || (spi->master->bus_num < 0)) + { + printk("%s:error:bus_num=%d\n",__func__, spi->master->bus_num); + return -ENOMEM; + } spi_test_data = (struct spi_test_data *)kzalloc(sizeof(struct spi_test_data), GFP_KERNEL); if(!spi_test_data){ @@ -129,7 +195,6 @@ static int __devinit spi_test_probe(struct spi_device *spi) } spi->bits_per_word = 8; - spi->mode = SPI_MODE_0; spi_test_data->spi = spi; spi_test_data->dev = &spi->dev; @@ -139,7 +204,7 @@ static int __devinit spi_test_probe(struct spi_device *spi) return -1; } - g_spi_test_data = spi_test_data; + g_spi_test_data[spi->master->bus_num] = spi_test_data; printk("%s:bus_num=%d,ok\n",__func__,spi->master->bus_num); @@ -147,6 +212,12 @@ static int __devinit spi_test_probe(struct spi_device *spi) } +static const struct spi_device_id spi_test_id[] = { + {"spi_test_bus0", 0}, + {"spi_test_bus1", 1}, + {}, +}; + static struct spi_driver spi_test_driver = { .driver = { @@ -154,12 +225,14 @@ static struct spi_driver spi_test_driver = { .bus = &spi_bus_type, .owner = THIS_MODULE, }, + .id_table = spi_test_id, .probe = spi_test_probe, }; static int __init spi_test_init(void) { + printk("%s\n",__func__); spi_register_board_info(board_spi_test_devices, ARRAY_SIZE(board_spi_test_devices)); misc_register(&spi_test_misc); return spi_register_driver(&spi_test_driver); -- 2.34.1