Exemple #1
0
int BcmSpiReserveSlave2(int busNum, int slaveId, int maxFreq, int spiMode, int ctrlState)
{
    struct spi_master * pSpiMaster;
    struct spi_driver * pSpiDriver;

    if ( slaveId > 7 )
    {
        return SPI_STATUS_ERR;
    }

    if ( LEG_SPI_BUS_NUM == busNum )
    {
#ifndef SPI
        return( SPI_STATUS_ERR );
#else
        if ( NULL != bcmLegSpiDevices[slaveId] )
        {
            printk(KERN_ERR "BcmSpiReserveSlave - slaveId %d, already registerd\n", slaveId);
            return( SPI_STATUS_ERR );
        }

        bcmLegSpiDevInfo[slaveId].max_speed_hz    = maxFreq;
        bcmLegSpiDevInfo[slaveId].controller_data = (void *)ctrlState;
        bcmLegSpiDevInfo[slaveId].mode            = spiMode;
        
        pSpiMaster                = spi_busnum_to_master( busNum );
        bcmLegSpiDevices[slaveId] = spi_new_device(pSpiMaster, &bcmLegSpiDevInfo[slaveId]);
        pSpiDriver                = &bcmLegSpiDevDrv[slaveId];
#endif
    }
    else if ( HS_SPI_BUS_NUM == busNum )
    {
#ifndef HS_SPI
        return( SPI_STATUS_ERR );
#else
        if ( NULL != bcmHSSpiDevices[slaveId] )
        {
            printk(KERN_ERR "BcmSpiReserveSlave - slaveId %d, already registerd\n", slaveId);
            return( SPI_STATUS_ERR );
        }

        bcmHSSpiDevInfo[slaveId].max_speed_hz    = maxFreq;
        bcmHSSpiDevInfo[slaveId].controller_data = (void *)ctrlState;
        bcmHSSpiDevInfo[slaveId].mode            = spiMode;
        
        pSpiMaster               = spi_busnum_to_master( busNum );
        bcmHSSpiDevices[slaveId] = spi_new_device(pSpiMaster, &bcmHSSpiDevInfo[slaveId]);
        pSpiDriver               = &bcmHSSpiDevDrv[slaveId];
#endif        
    }
    else
        return( SPI_STATUS_ERR );

    /* register the SPI driver */
    spi_register_driver(pSpiDriver);

    return 0;
    
}
static int __init galileo_module_init(void)
{
	struct spi_master *master;
	int err;

	pr_info("module init\n");

	set_spi_quark_board_value(&spi_info, &spi_irq, &spi_master);

	err = -ENODEV;
	master = spi_busnum_to_master(spi_master);
	pr_info("master=%p\n", master);
	if (!master)
		goto out;

	dev = spi_new_device(master, &spi_info);
	pr_info("dev=%p\n", dev);
	if (!dev)
		goto out;

	if (spi_irq) {
		dev->irq = gpio_to_irq(spi_irq);
		irq_set_irq_type(dev->irq, IRQF_TRIGGER_RISING);
	}

	pr_info("802.15.4 chip registered\n");
	err = 0;

 out:
	if (err)
		pr_err("Failed to register SPI device\n");
	return err;
}
static int hotplug_spi_init(void)
{
  int bus_num;
  struct spi_master *slaves_spi_master;

  printk(KERN_ALERT "Adding SPI Device: %s, bus: %i, chip-sel: %i\n", 
	 slave_spi_board_info.modalias, slave_spi_board_info.bus_num, slave_spi_board_info.chip_select);
  
  /* Add the slave SPI device to the SPI bus
   *
   * These methods are used to hot-plug spi devices.
   * SPI devices are by nature NOT hot-pluggable, as
   * they cannot be probed for functionality etc. SPI
   * devices are normally cold-plugged during boot, that
   * is, they are added in the board description file:
   * /arch/arm/mach-omap2/board-devkit8000.c
   * Using this method we actually doing "hot" cold-plugging
   * adding devices using a kernel module.
   * Note that it is crusial that driver and device uses
   * the same name alias. If not, the device and driver
   * will not be paired and the probe method in the driver
   * not be called.
   */ 
  bus_num = slave_spi_board_info.bus_num;
  slaves_spi_master = spi_busnum_to_master(bus_num);
  slave_spi_device = spi_new_device(slaves_spi_master,
				    &slave_spi_board_info);
  if(slave_spi_device < 0) {
    printk(KERN_ALERT "Unsuccesful creating a new device\n");
    return -1;
  }
    
  return 0;
}
static int __init low_speed_spidev_module_init(void)
{
	struct spi_master *master;
	int err;

	pr_info("module init\n");

	err = -ENODEV;

	master = spi_busnum_to_master(LOW_SPEED_SPIDEV_SPI_BUS);
	pr_info("master=%p\n", master);
	if (!master)
		goto out;

	dev = spi_new_device(master, &cal_spi_board_info);
	pr_info("dev=%p\n", dev);
	if (!dev)
		goto out;
	pr_info("spidev registered\n");
	err = 0;

 out:
	if (err)
		pr_err("Failed to register SPI device\n");
	return err;
}
static int __init as_spi_gpio_dev_probe(void)
{
	struct platform_device *pdev;
	int err;
	struct spi_master *master;
	struct spi_device *slave;
	struct spi_board_info slave_info;

	pdev=platform_device_alloc("as_spi_gpio",DEVID);
	if(!pdev)
	{
		err=-ENOMEM;
		goto err;
	}

	err=platform_device_add(pdev);
	if(err)
	{
		printk(KERN_ERR PFX "platform_device_add failed with return code %d\n",err);
		platform_device_put(pdev);
		goto err;
	}

	memset(&slave_info,0,sizeof(slave_info));
	strcpy(slave_info.modalias,"spidev");
	slave_info.controller_data=(void*)SPI_GPIO_NO_CHIPSELECT;
	slave_info.max_speed_hz=MAX_FREQ;
	slave_info.chip_select=0;
	slave_info.mode=SPI_MODE;

	master=spi_busnum_to_master(DEVID);
	if(!master)
	{
		printk(KERN_ERR PFX "unable to get master for bus %d\n",DEVID);
		err=-EINVAL;
		goto err_unregister;
	}

	slave=spi_new_device(master,&slave_info);
	spi_master_put(master);
	if(!slave)
	{
		printk(KERN_ERR PFX "unable to create slave %d for bus %d\n",0,DEVID);
		/* Will most likely fail due to unsupported mode bits */
		err=-EINVAL;
		goto err_unregister;
	}

	device=pdev;

	return 0;

err_unregister:
	platform_device_unregister(pdev);
err:
	as_spi_gpio_dev_cleanup();
	return err;
}
Exemple #6
0
static int __init
_spi_init(void)
{
   int ret;
   unsigned char ch = 0x01;
   struct spi_master *master;
   struct spi_board_info spi_device_info = {
        .modalias = "ami-spi-device",
        .max_speed_hz = 12000000, //speed of your device splace can handle
        .bus_num = 0, //BUS number
        .chip_select = 0,
        .mode = 3,
   };

   printk(KERN_INFO "spi basic driver init");

   master = spi_busnum_to_master(spi_device_info.bus_num);
   if (!master)
     {
        printk(KERN_ALERT "Failed to create master device");
        return -ENODEV;
     }
   //create a slave new device, given the master and device info
   sdev = spi_new_device(master, &spi_device_info);
   if (!sdev)
     {
        printk(KERN_ALERT "Failed to create slave device");
        return -ENODEV;
     }

   sdev->bits_per_word = 8;


   ret = spi_setup(sdev);
   if (ret)
     {
        printk(KERN_ALERT "Failed to setup slave");
        spi_unregister_device(sdev);
        return -ENODEV;
     }

   printk(KERN_ALERT "Writing ch=0x01 to spi interface");
   spi_write(sdev, &ch, sizeof(ch));

   return 0;
}

static void __exit
_spi_exit(void)
{
   printk(KERN_INFO "spi basic driver exit");
   if (sdev)
     {
        unsigned char ch = 0xff;
        spi_write(sdev, &ch, sizeof(ch));
        spi_unregister_device(sdev);
     }
}
Exemple #7
0
static void __exit mt7697spi_exit(void)
{
	char str[32];
	struct spi_master *master = NULL;
	struct device *dev;
	struct spi_device *spi;
	struct mt7697q_info *qinfo;
	int bus_num = MT7697_SPI_BUS_NUM;

	while (!master && (bus_num >= 0)) {
		master = spi_busnum_to_master(bus_num);
		if (!master)
			bus_num--;
	}

	if (!master) {
		pr_err(DRVNAME" spi_busnum_to_master() failed\n");
		goto cleanup;
	}

	snprintf(str, sizeof(str), "%s.%u", dev_name(&master->dev),
	         MT7697_SPI_CS);
	dev_info(&master->dev, "%s(): find SPI device('%s')\n", __func__, str);
	dev = bus_find_device_by_name(&spi_bus_type, NULL, str);
	if (!dev) {
		dev_err(&master->dev,
		        "%s(): '%s' bus_find_device_by_name() failed\n",
		        __func__, str);
		goto cleanup;
	}

	spi = to_spi_device(dev);
	if (!spi) {
		dev_err(dev, "%s():  get SPI device failed\n",
		        __func__);
		goto cleanup;
	}

	qinfo = spi_get_drvdata(spi);
	if (!qinfo) {
		dev_err(dev, "%s():  SPI device no queue info\n",
		        __func__);
		goto cleanup;
	}

	dev_info(qinfo->dev, "%s(): remove '%s'\n", __func__, DRVNAME);
	cancel_delayed_work_sync(&qinfo->irq_delayed_work);
	cancel_work_sync(&qinfo->irq_work);
	flush_workqueue(qinfo->irq_workq);
	destroy_workqueue(qinfo->irq_workq);

	free_irq(qinfo->irq, qinfo);
	if (qinfo->gpio_pin > 0) gpio_free(qinfo->gpio_pin);
	kfree(qinfo);

cleanup:
	return;
}
Exemple #8
0
static __init int spisvc_init(void)
{
	struct spi_master *m = NULL;
	struct spi_board_info board = {
		.modalias = "spidev",
		.max_speed_hz = 15058800,
		.mode = SPI_MODE_3,
		.platform_data = NULL,
		.bus_num = 0,
		.chip_select = 0,
		.irq = 0,
	};

	if (SPI_INVALID_BUS == busnum)
		/* Bus not assigned: find SPI master with lowest bus number */
		for (busnum = 0; SPI_MAX_BUS > busnum && NULL == m; busnum++)
			m = spi_busnum_to_master(busnum);
	else
		m = spi_busnum_to_master(busnum);

	if (!m) {
		pr_err("SPI bus not available.\n");
		return -ENODEV;
	}

	board.bus_num = busnum = m->bus_num;
	board.chip_select = cs;
	spidev = spi_new_device(m, &board);
	if (!spidev) {
		dev_err(&m->dev, "Cannot add '%s' on bus %u, cs %u\n",
			board.modalias, board.bus_num, board.chip_select);
		return -ENODEV;
	}
	return 0;
}
module_init(spisvc_init);

static __exit void spisvc_exit(void)
{
	if (spidev)
		spi_unregister_device(spidev);
}
module_exit(spisvc_exit);
Exemple #9
0
static int __init mcp3901_hotplug(void)
{
	int ret;
	struct spi_master     *master;

	master = spi_busnum_to_master(SPI_BUS1);
	if(master == NULL)
		return -ENODEV;

	mcp3901_device = spi_new_device(master, &mcp3901_board_info);
	if(mcp3901_device == NULL)
		return -ENODEV;

	printk("Hotplugged MCP3901.");
	return 0;
}
Exemple #10
0
// -----------------------------------------------------------------------------------
static int __init init_maspi(void)
{
  struct spi_master *master;
  printk(KERN_INFO "registering maspi spi device\n");
  master = spi_busnum_to_master(0);
  if (!master)
  {
    printk(KERN_ALERT "could not find master spi driver");
    return -1;
  }
  maspi_device = spi_new_device(master, &pseudo_spi_device);
  if (!maspi_device)
  {
    printk(KERN_ALERT "could not add extra spi device [CS2]");
    return -1;
  }
  return 0;
}
static int __init spi_gpio_custom_check_params(unsigned int id, unsigned int *params)
{
	int i;
	struct spi_master *master;

	if (bus_nump[id] < BUS_PARAM_REQUIRED) {
		printk(KERN_ERR PFX "not enough values for parameter bus%d\n",
		       id);
		return -EINVAL;
	}

	if (bus_nump[id] > (1+BUS_PARAM_CS1)) {
		/* more than 1 device: check CS GPIOs */
		for (i = 0; i < BUS_SLAVE_COUNT_MAX; i++) {
			/* no more slaves? */
			if (spi_gpio_custom_get_slave_mode(id, params, i) < 0)
				break;

			if (spi_gpio_custom_get_slave_cs(id, params, i) < 0) {
				printk(KERN_ERR PFX "invalid/missing CS gpio for slave %d on bus %d\n",
				       i, params[BUS_PARAM_ID]);
				return -EINVAL;
			}
		}
	}

	if (!gpio_is_valid(params[BUS_PARAM_SCK])) {
		printk(KERN_ERR PFX "invalid SCK gpio for bus %d\n",
		       params[BUS_PARAM_ID]);
		return -EINVAL;
	}

	master = spi_busnum_to_master(params[BUS_PARAM_ID]);
	if (master) {
		spi_master_put(master);
		printk(KERN_ERR PFX "bus %d already exists\n",
		       params[BUS_PARAM_ID]);
		return -EEXIST;
	}

	return 0;
}
Exemple #12
0
static inline __init int spi_int(void)
{
    int ret;
    struct spi_board_info spi_device_info_1 = {
        .modalias = "spi_int",
        .max_speed_hz = speed_hz,
        .bus_num = spi_bus,
        .mode = 1,  // depends on the mode we have to use this
    };

    struct spi_master *master1;  // for specifying I am using this driver for spi_master
    master1 = spi_busnum_to_master( spi_device_info_1.bus_num ); //assigning bus_number to master
    if( !master1 )
    {
        printk( KERN_INFO"spi bus information for master  not found");
        return -ENODEV; // error to the kernel
    }
    spi_device_1 = spi_new_device( master1, &spi_device_info_1 ); // creation of spi device in kernel module
    if( !spi_device_1 )
    {
        printk(KERN_INFO"spi device not able to create "); // this error is generated if we are using the same channel which bydefault spi is using
        return -ENODEV;
    }

    spi_device_1->bits_per_word = 32;  // specifying the lenght of word that can spi transfer
    spi_device_1->cs_gpio =60;  // for external GPIO as chip select
    ret = spi_setup( spi_pot_device);
    if( ret )
        spi_unregister_device( spi_device_1 );
    else
        printk( KERN_INFO "%d bus no andcs no %d", spi_bus, gpio_pin_chipselect );
    return ret;
}

static inline void spi_release(void)
{
    spi_unregister_device( spi_pot_device );

}
Exemple #13
0
static int add_gpio_pmodoled_device_to_bus(struct gpio_pmodoled_device *dev)
{
	struct spi_master *spi_master;
	struct spi_device *spi_device;
	int status = 0;

	spi_master = spi_busnum_to_master(dev->spi_id);
	if (!spi_master) {
		dev_err(&dev->pdev->dev, "spi_busnum_to_master(%d) returned NULL\n", dev->spi_id);
		return -ENOSYS;
	}

	spi_device = spi_alloc_device(spi_master);
	if (!spi_device) {
		put_device(&spi_master->dev);
		dev_err(&dev->pdev->dev, "spi_alloc_device() failed\n");
		return -ENOMEM;
	}

	spi_device->chip_select = 0;
	spi_device->max_speed_hz = 4000000;
	spi_device->mode = SPI_MODE_0;
	spi_device->bits_per_word = 8;
	spi_device->controller_data = (void *) dev->iCS;
	spi_device->dev.platform_data = dev;
	strlcpy(spi_device->modalias, SPI_DRIVER_NAME, sizeof(SPI_DRIVER_NAME));

	status = spi_add_device(spi_device);
	if (status < 0) {
		spi_dev_put(spi_device);
		dev_err(&dev->pdev->dev, "spi_add_device() failed %d\n", status);
		return status;
	}
	dev->spi = spi_device;

	put_device(&spi_master->dev);

	return status;
}
Exemple #14
0
int qtft_spi_init(void)
{
	int err=0;
	struct spi_master *master=NULL;
	struct spi_device *new_device=NULL;
	func_in();

	err = spi_register_driver(&qtft_spi_dri_driver);
	if(err)
	{
		printk(KERN_ERR "Can't register spi driver \n");
		goto out;
	}

	master = spi_busnum_to_master(SPI_BUS_NUM);
	if(!master)
	{
		printk(KERN_ERR "Can't get SPI bus %d\n",SPI_BUS_NUM);
		err = -ENODEV;
		goto err0;
	}

	new_device = spi_new_device(master, qtft_spi_dev_board_info);
	if(!new_device)
	{
		printk(KERN_ERR "Can't register spi device \n");
		err = -ENODEV;
		goto err0;
	}

	goto out;

err0:
	spi_unregister_driver(&qtft_spi_dri_driver);
out:
	func_out();
	return err;
}
Exemple #15
0
static inline __init int spi_init(void) {
	struct spi_board_info spi_pot_device_info = {
		.modalias = "itrigue",
		.max_speed_hz = speed_hz,
		.bus_num = pot_spi_bus,
		.chip_select = pot_spi_cs,
		.mode = 0,
	};

	struct spi_master *master;

	int ret;

	master = spi_busnum_to_master( spi_pot_device_info.bus_num );
	if( !master )
		return -ENODEV;

	spi_pot_device = spi_new_device( master, &spi_pot_device_info );
	if( !spi_pot_device )
		return -ENODEV;

	spi_pot_device->bits_per_word = 16;

	ret = spi_setup( spi_pot_device );
	if( ret )
		spi_unregister_device( spi_pot_device );
	else
		printk( KERN_INFO "I-Trigue 3300 potentiometers registered to SPI bus %u, chipselect %u\n", 
			pot_spi_bus, pot_spi_cs );

	return ret;
}

static inline void spi_exit(void) {
	spi_unregister_device( spi_pot_device );
}
Exemple #16
0
/*====================================================================
FUNCTION       find_spi_device  
DESCRIPTION 
DEPENDENCIES
RETURN VALUE
SIDE EFFECTS
======================================================================*/
static struct spi_device* find_spi_device(int bus_num)
{
  struct spi_master *spi_master;
  struct spi_device *spi_device;
  struct device *pdev;
  char buff[64];

  spi_master = spi_busnum_to_master(bus_num);
  if (!spi_master) {
    TDMB_MSG_SPI("[%s] spi_busnum_to_master(%d) returned NULL\n", __func__, bus_num);
    return NULL;
  }

  spi_device = spi_alloc_device(spi_master);
  if (!spi_device) {
    put_device(&spi_master->dev);
    TDMB_MSG_SPI("[%s] spi_alloc_device() failed\n", __func__);
    return NULL;
  }

  /* specify a chip select line */
  spi_device->chip_select = 0;

  snprintf(buff, sizeof(buff), "%s.%u",
      dev_name(&spi_device->master->dev),
      spi_device->chip_select);

  pdev = bus_find_device_by_name(spi_device->dev.bus, NULL, buff);

  //if (pdev) {
  //  TCDBG("spi_device :0x%X\n", (unsigned int)spi_device);
  //}

  put_device(&spi_master->dev);
  return spi_device;
}
/******************************
 register spi device and attach it to Master driver
******************************/
static int reg_spi_device(void)
{
	int retval = 0;
	
	struct spi_board_info spi_device_info = {
		.modalias = "nrf24l01+",
		.max_speed_hz = 5000000,
		.bus_num = 0,
		.chip_select = 1,
		.mode = 0,
	};

	struct spi_master *master;
	
	master = spi_busnum_to_master(spi_device_info.bus_num);
	if(!master){
		printk(KERN_ALERT "getting master device is failed!!\n");	
		retval = -ENODEV;
		goto out;
	}
	
	spi_device = spi_new_device(master,&spi_device_info);
	if(!spi_device){
		printk(KERN_ALERT "registering spi device is failed!!\n");	
		retval = -ENODEV;
		goto out;
	}
	
	spi_device->bits_per_word = 8;

	retval = spi_setup(spi_device);
	if(retval){
		spi_unregister_device(spi_device);
		goto out;
	}

	return 0;

	out:
	return retval;
}

/******************************
 Module initialization function
******************************/
static int __init nrf_init(void)
{
	int retval = 0;

	printk(KERN_INFO "hello from module!!\n");
	
	retval = reg_dev();
	if(retval != 0)
		goto out;
	
	retval = reg_spi_device();
	if(retval != 0)
		goto out;


	radio_init();

	return 0;
	
	out:
	return retval;
}
Exemple #18
0
static int __init ads7846_device_init(void)
{
	struct spi_master *master;
	struct ads7846_platform_data *pdata = &pdata_ads7846_device;

	if (verbose)
		pr_info("\n\n"DRVNAME": %s()\n", __func__);

	if (gpio_pendown < 0) {
		pr_err(DRVNAME": Argument required: 'gpio_pendown'\n");
		return -EINVAL;
	}

	if (verbose > 1)
		pr_spi_devices(); /* print list of registered SPI devices */

	/* set SPI values */
	spi_ads7846_device.max_speed_hz = speed;
	spi_ads7846_device.bus_num = busnum;
	spi_ads7846_device.chip_select = cs;
	spi_ads7846_device.mode = mode;
	irq = irq ? : (GPIO_IRQ_START + gpio_pendown);
	spi_ads7846_device.irq = irq;

	/* set platform_data values */
	pdata->model = model;
	pdata->vref_delay_usecs = vref_delay_usecs;
	pdata->vref_mv = vref_mv;
	pdata->keep_vref_on = keep_vref_on;
	pdata->swap_xy = swap_xy;
	pdata->settle_delay_usecs = settle_delay_usecs;
	pdata->penirq_recheck_delay_usecs = penirq_recheck_delay_usecs;
	pdata->x_plate_ohms = x_plate_ohms;
	pdata->y_plate_ohms = y_plate_ohms;
	pdata->x_min = x_min;
	pdata->x_max = x_max;
	pdata->y_min = y_min;
	pdata->y_max = y_max;
	pdata->pressure_min = pressure_min;
	pdata->pressure_max = pressure_max;
	pdata->debounce_max = debounce_max;
	pdata->debounce_tol = debounce_tol;
	pdata->debounce_rep = debounce_rep;
	pdata->gpio_pendown = gpio_pendown;
	pdata->irq_flags = irq_flags;

	if (verbose) {
		pr_info(DRVNAME": Settings:\n");
		pr_pdata(model);
		pr_pdata(gpio_pendown);
		pr_pdata(swap_xy);
		pr_pdata(x_min);
		pr_pdata(x_max);
		pr_pdata(y_min);
		pr_pdata(y_max);
		pr_pdata(x_plate_ohms);
		pr_pdata(pressure_min);
		pr_pdata(pressure_max);
		pr_pdata(keep_vref_on);
		pr_pdata(vref_delay_usecs);
		pr_pdata(vref_mv);
		pr_pdata(settle_delay_usecs);
		pr_pdata(penirq_recheck_delay_usecs);
		pr_pdata(y_plate_ohms);
		pr_pdata(debounce_max);
		pr_pdata(debounce_tol);
		pr_pdata(debounce_rep);
	}

	master = spi_busnum_to_master(spi_ads7846_device.bus_num);
	if (!master) {
		pr_err(DRVNAME": spi_busnum_to_master(%d) returned NULL.\n", spi_ads7846_device.bus_num);
		return -EINVAL;
	}

	spidevices_delete(master, spi_ads7846_device.chip_select);      /* make sure it's available */

	ads7846_spi_device = spi_new_device(master, &spi_ads7846_device);
	put_device(&master->dev);
	if (!ads7846_spi_device) {
		pr_err(DRVNAME": spi_new_device() returned NULL\n");
		return -EPERM;
	}

	if (verbose)
		pr_spi_devices();

	return 0;
}
static int __init spi_gpio_custom_add_one(unsigned int id, unsigned int *params)
{
	struct platform_device *pdev;
	struct spi_gpio_platform_data pdata;
	int i;
	int num_cs;
	int err;
	struct spi_master *master;
	struct spi_device *slave;
	struct spi_board_info slave_info;
	int mode, maxfreq, cs;


	if (!bus_nump[id])
		return 0;

	err = spi_gpio_custom_check_params(id, params);
	if (err)
		goto err;

	/* Create BUS device node */

	pdev = platform_device_alloc("spi_gpio", params[BUS_PARAM_ID]);
	if (!pdev) {
		err = -ENOMEM;
		goto err;
	}

	num_cs = 0;
	for (i = 0; i < BUS_SLAVE_COUNT_MAX; i++) {
		/* no more slaves? */
		if (spi_gpio_custom_get_slave_mode(id, params, i) < 0)
			break;

		if (spi_gpio_custom_get_slave_cs(id, params, i) >= 0)
			num_cs++;
	}
	if (num_cs == 0) {
		/*
		 * Even if no CS is used, spi modules expect
		 * at least 1 (unused)
		 */
		num_cs = 1;
	}

	pdata.sck = params[BUS_PARAM_SCK];
	pdata.mosi = gpio_is_valid(params[BUS_PARAM_MOSI])
		? params[BUS_PARAM_MOSI]
		: SPI_GPIO_NO_MOSI;
	pdata.miso = gpio_is_valid(params[BUS_PARAM_MISO])
		? params[BUS_PARAM_MISO]
		: SPI_GPIO_NO_MISO;
	pdata.num_chipselect = num_cs;

	err = platform_device_add_data(pdev, &pdata, sizeof(pdata));
	if (err) {
		platform_device_put(pdev);
		goto err;
	}

	err = platform_device_add(pdev);
	if (err) {
		printk(KERN_ERR PFX "platform_device_add failed with return code %d\n",
		       err);
		platform_device_put(pdev);
		goto err;
	}

	/* Register SLAVE devices */

	for (i = 0; i < BUS_SLAVE_COUNT_MAX; i++) {
		mode = spi_gpio_custom_get_slave_mode(id, params, i);
		maxfreq = spi_gpio_custom_get_slave_maxfreq(id, params, i);
		cs = spi_gpio_custom_get_slave_cs(id, params, i);

		/* no more slaves? */
		if (mode < 0)
			break;

		memset(&slave_info, 0, sizeof(slave_info));
		strcpy(slave_info.modalias, "spidev");
		slave_info.controller_data = (void *)((cs >= 0)
			? cs
			: SPI_GPIO_NO_CHIPSELECT);
		slave_info.max_speed_hz = maxfreq;
		slave_info.bus_num = params[BUS_PARAM_ID];
		slave_info.chip_select = i;
		slave_info.mode = mode;

		master = spi_busnum_to_master(params[BUS_PARAM_ID]);
		if (!master) {
			printk(KERN_ERR PFX "unable to get master for bus %d\n",
			       params[BUS_PARAM_ID]);
			err = -EINVAL;
			goto err_unregister;
		}
		slave = spi_new_device(master, &slave_info);
		spi_master_put(master);
		if (!slave) {
			printk(KERN_ERR PFX "unable to create slave %d for bus %d\n",
			       i, params[BUS_PARAM_ID]);
			/* Will most likely fail due to unsupported mode bits */
			err = -EINVAL;
			goto err_unregister;
		}
	}

	devices[nr_devices++] = pdev;

	return 0;

err_unregister:
	platform_device_unregister(pdev);
err:
	return err;
}
Exemple #20
0
static int __init mt7697spi_init(void)
{
	char str[32];
	struct spi_master *master = NULL;
	struct device *dev;
	struct spi_device *spi;
	struct mt7697q_info *qinfo = NULL;
	int bus_num = MT7697_SPI_BUS_NUM;
	int ret = 0;

	pr_info(DRVNAME" %s(): '%s' initialize\n", __func__, DRVNAME);

	while (!master && (bus_num >= 0)) {
		master = spi_busnum_to_master(bus_num);
		if (!master)
			bus_num--;
	}

	if (!master) {
		pr_err(DRVNAME" spi_busnum_to_master() failed\n");
		ret = -EINVAL;
		goto cleanup;
	}

	ret = cp2130_update_ch_config(master, MT7697_SPI_CONFIG);
	if (ret < 0) {
		dev_err(&master->dev,
		        "%s(): cp2130_update_ch_config() failed(%d)\n",
		        __func__, ret);
		goto cleanup;
	}

	snprintf(str, sizeof(str), "%s.%u", dev_name(&master->dev),
	         MT7697_SPI_CS);
	dev_info(&master->dev, "%s(): find SPI device('%s')\n", __func__, str);
	dev = bus_find_device_by_name(&spi_bus_type, NULL, str);
	if (!dev) {
		dev_err(&master->dev,
		        "%s(): bus_find_device_by_name('%s') failed\n",
		        __func__, str);

		ret = -EINVAL;
		goto cleanup;
	}

	spi = to_spi_device(dev);
	if (!spi) {
		dev_err(&master->dev, "%s(): get SPI device failed\n",
		        __func__);
		ret = -EINVAL;
		goto cleanup;
	}

	dev_info(&master->dev, "%s(): dev('%s') mode(%d) max speed(%d) "
	         "CS(%d) bits/word(%d)\n",
	         __func__, spi->modalias, spi->mode, spi->max_speed_hz,
	         spi->chip_select, spi->bits_per_word);

	qinfo = kzalloc(sizeof(struct mt7697q_info), GFP_KERNEL);
	if (!qinfo) {
		dev_err(&master->dev, "%s(): create queue info failed\n",
		        __func__);
		ret = -ENOMEM;
		goto cleanup;
	}

	qinfo->dev = &spi->dev;

	qinfo->hw_priv = spi;
	qinfo->hw_ops = &hw_ops;

	mutex_init(&qinfo->mutex);
	INIT_DELAYED_WORK(&qinfo->irq_delayed_work, mt7697q_irq_delayed_work);
	INIT_WORK(&qinfo->irq_work, mt7697q_irq_work);

	qinfo->irq_workq = alloc_workqueue(DRVNAME"wq",
	                                   WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
	if (!qinfo->irq_workq) {
		dev_err(qinfo->dev, "%s(): alloc_workqueue() failed\n",
		        __func__);
		ret = -ENOMEM;
		goto cleanup;
	}

	qinfo->gpio_pin = MT7697_SPI_INTR_GPIO_PIN;
	ret = gpio_request(qinfo->gpio_pin, MT7697_SPI_GPIO_IRQ_NAME);
	if (ret < 0) {
		if (ret != -EBUSY) {
			dev_err(qinfo->dev, "%s(): gpio_request() failed(%d)",
			        __func__, ret);
			goto failed_workqueue;
		}

		qinfo->irq = gpio_to_irq(qinfo->gpio_pin);
		qinfo->gpio_pin = MT7697_SPI_INTR_GPIO_PIN_INVALID;
	} else {
		gpio_direction_input(qinfo->gpio_pin);
		qinfo->irq = gpio_to_irq(qinfo->gpio_pin);
	}

	dev_info(qinfo->dev, "%s(): request irq(%d)\n", __func__, qinfo->irq);
	ret = request_irq(qinfo->irq, mt7697q_isr, 0, DRVNAME, qinfo);
	if (ret < 0) {
		dev_err(qinfo->dev, "%s(): request_irq() failed(%d)",
		        __func__, ret);
		goto failed_gpio_req;
	}

	irq_set_irq_type(qinfo->irq, IRQ_TYPE_EDGE_BOTH);

	spi_set_drvdata(spi, qinfo);

	dev_info(qinfo->dev, "%s(): '%s' initialized\n", __func__, DRVNAME);
	return 0;

failed_gpio_req:
	if (qinfo->gpio_pin > 0) gpio_free(qinfo->gpio_pin);

failed_workqueue:
	destroy_workqueue(qinfo->irq_workq);

cleanup:
	if (qinfo) kfree(qinfo);
	return ret;
}
Exemple #21
0
static int __init fbtft_device_init(void)
{
	struct spi_master *master = NULL;
	struct spi_board_info *display = NULL;
	const struct fbtft_platform_data *pdata = NULL;
	const struct fbtft_gpio *gpio = NULL;
	char *p_name, *p_num;
	bool found = false;
	int i;
	long val;
	int ret = 0;

	pr_debug("\n\n"DRVNAME": init\n");

	/* parse module parameter: gpios */
	if (gpios_num > MAX_GPIOS) {
		pr_err(DRVNAME":  gpios parameter: exceeded max array size: %d\n", MAX_GPIOS);
		return -EINVAL;
	}
	if (gpios_num > 0) {
		for (i=0;i<gpios_num;i++) {
			if (strchr(gpios[i], ':') == NULL) {
				pr_err(DRVNAME":  error missing ':' in gpios parameter: %s\n", gpios[i]);
				return -EINVAL;
			}
			p_num = gpios[i];
			p_name = strsep(&p_num, ":");
			if (p_name == NULL || p_num == NULL) {
				pr_err(DRVNAME":  something bad happenned parsing gpios parameter: %s\n", gpios[i]);
				return -EINVAL;
			}
			ret = kstrtol(p_num, 10, &val);
			if (ret) {
				pr_err(DRVNAME":  could not parse number in gpios parameter: %s:%s\n", p_name, p_num);
				return -EINVAL;
			}
			strcpy(fbtft_device_param_gpios[i].name, p_name);
			fbtft_device_param_gpios[i].gpio = (int) val;
			pdata = &fbtft_device_param_pdata;
		}
	}

	if (verbose > 2)
		pr_spi_devices(); /* print list of registered SPI devices */

	if (verbose > 2)
		pr_p_devices(); /* print list of 'fb' platform devices */

	if (name == NULL) {
		pr_err(DRVNAME":  missing module parameter: 'name'\n");
		pr_err(DRVNAME":  Use 'modinfo -p "DRVNAME"' to get all parameters\n");
		return -EINVAL;
	}

	pr_debug(DRVNAME":  name='%s', busnum=%d, cs=%d\n", name, busnum, cs);

	if (rotate > 3) {
		pr_warning("argument 'rotate' illegal value: %d (0-3). Setting it to 0.\n", rotate);
		rotate = 0;
	}

	/* name=list lists all supported drivers */
	if (strncmp(name, "list", 32) == 0) {
		pr_info(DRVNAME":  Supported drivers:\n");
		for (i=0; i < ARRAY_SIZE(fbtft_device_spi_displays); i++) {
			pr_info(DRVNAME":      %s\n", fbtft_device_spi_displays[i].modalias);
		}
		for (i=0; i < ARRAY_SIZE(fbtft_device_pdev_displays); i++) {
			pr_info(DRVNAME":      %s\n", fbtft_device_pdev_displays[i].name);
		}
		return -ECANCELED;
	}

	/* see if it is a SPI device */
	for (i=0; i < ARRAY_SIZE(fbtft_device_spi_displays); i++) {
		if (strncmp(name, fbtft_device_spi_displays[i].modalias, 32) == 0) {
			master = spi_busnum_to_master(busnum);
			if (!master) {
				pr_err(DRVNAME":  spi_busnum_to_master(%d) returned NULL\n", busnum);
				return -EINVAL;
			}
			fbtft_device_delete(master, cs);      /* make sure it's available */
			display = &fbtft_device_spi_displays[i];
			display->chip_select = cs;
			display->bus_num = busnum;
			if (speed)
				display->max_speed_hz = speed;
			if (mode != -1)
				display->mode = mode;
			if (pdata)
				display->platform_data = pdata;
			pdata = display->platform_data;
			((struct fbtft_platform_data *)pdata)->rotate = rotate;
			((struct fbtft_platform_data *)pdata)->bgr = bgr;
			if (fps)
				((struct fbtft_platform_data *)pdata)->fps = fps;
			if (txbuflen)
				((struct fbtft_platform_data *)pdata)->txbuflen = txbuflen;
			spi_device = spi_new_device(master, display);
			put_device(&master->dev);
			if (!spi_device) {
				pr_err(DRVNAME":    spi_new_device() returned NULL\n");
				return -EPERM;
			}
			found = true;
			break;
		}
	}

	if (!found) {
		/* see if it is a platform_device */
		for (i=0; i < ARRAY_SIZE(fbtft_device_pdev_displays); i++) {
			if (strncmp(name, fbtft_device_pdev_displays[i].name, 32) == 0) {
				p_device = &fbtft_device_pdev_displays[i];
				ret = platform_device_register(p_device);
				if (ret < 0) {
					pr_err(DRVNAME":    platform_device_register() returned %d\n", ret);
					return ret;
				}
				if (pdata)
					p_device->dev.platform_data = (void *)pdata;
				pdata = p_device->dev.platform_data;
				((struct fbtft_platform_data *)pdata)->rotate = rotate;
				((struct fbtft_platform_data *)pdata)->bgr = bgr;
				if (fps)
					((struct fbtft_platform_data *)pdata)->fps = fps;
				if (txbuflen)
					((struct fbtft_platform_data *)pdata)->txbuflen = txbuflen;
				found = true;
				break;
			}
		}
	}

	if (!found) {
		pr_err(DRVNAME":  device not supported: '%s'\n", name);
		return -EINVAL;
	}

	if (verbose)
		pr_info(DRVNAME":  GPIOS used by '%s':\n", name);
	gpio = pdata->gpios;
	if (!gpio) {
		pr_err(DRVNAME":  gpio is unexspectedly empty\n");
		return -EINVAL;
	}
	while (verbose && gpio->name[0]) {
		pr_info(DRVNAME":    '%s' = GPIO%d\n", gpio->name, gpio->gpio);
		gpio++;
	}

	if (spi_device && (verbose > 1))
		pr_spi_devices();
	if (p_device && (verbose > 1))
		pr_p_devices();

	return 0;
}
Exemple #22
0
int __init bcm_mpi_init(bcm_mpi_t *t, const bcm_mpi_params_t *params)
{
   int ret = -1;

#ifdef BCMPH_USE_SPI_DRIVER
# ifndef BCMPH_NOHW
   struct spi_master *master;
   struct spi_board_info board_info;
# endif // !BCMPH_NOHW
#endif // BCMPH_USE_SPI_DRIVER

   bcm_pr_debug("%s()\n", __func__);
   bcm_assert(NULL != params);

#ifndef BCMPH_NOHW
   t->trx_opts.fill_byte = params->fill_byte;
   t->trx_opts.wait_completion_with_irq = params->wait_completion_with_irq;
   t->trx_opts.drop_cs_after_each_byte = params->drop_cs_after_each_byte;
   t->trx_opts.cs_off_clk_cycles = params->cs_off_clk_cycles;
#endif // !BCMPH_NOHW

#ifdef BCMPH_USE_SPI_DRIVER
   t->mpi_clk = params->clk;
# ifndef BCMPH_NOHW
   master = spi_busnum_to_master(params->bus_num);
   if (NULL == master) {
      bcm_pr_err("No SPI master found for bus num %d. Module bcm63xx-spi not loaded ?\n",
         (int)(params->bus_num));
      ret = -EINVAL;
      goto fail_master;
   }

   memset(&(board_info), 0, sizeof(board_info));
   strcpy(board_info.modalias, driver_name);
   board_info.max_speed_hz = params->clk;
   board_info.bus_num = params->bus_num;
   board_info.chip_select = params->cs;
   board_info.mode = SPI_MODE_3;
   t->dev = spi_new_device(master, &(board_info));
   if (NULL == t->dev) {
      bcm_pr_err("Failed to add SPI device (busnum = %d, chip select = %d, clock = %lu)\n",
         (int)(params->bus_num), (int)(params->cs), (unsigned long)(params->clk));
      ret = -ENOMEM;
      goto fail_new_dev;
   }
   put_device(&(master->dev));
   /* Lock the bus */
   if ((params->has_exclusive_bus_access) && (!bcm_drv_param_mpi_no_exclusive_bus_access)) {
      spi_bus_lock(t->dev->master);
      t->bus_is_locked = true;
   }

   bcm_mpi_enable_extra_CSs(params->cs);
#  ifdef BCMPH_DEBUG_MPI
   t->trace_len = 0;
#  endif // BCMPH_DEBUG_MPI

   return (0);

   spi_unregister_device(t->dev);
fail_new_dev:
   put_device(&(master->dev));
fail_master:
# else // BCMPH_NOHW
   ret = 0;
# endif // BCMPH_NOHW
#else // !BCMPH_USE_SPI_DRIVER
# ifndef BCMPH_NOHW
   t->mpi_cs = params->cs;
   t->clk_cfg = bcm_mpi_get_clk_cfg(params->clk, params->cs_off_clk_cycles);

   bcm_mpi_enable_extra_CSs(params->cs);

   if (bcm_mpi_dev_data.ref_count <= 0) {
      struct device_driver *spi_driver = driver_find(bcm63xx_spi_driver.driver.name, &platform_bus_type);
      if (NULL != spi_driver) {
         bcm_pr_err("Error: Driver '%s' is already registered, aborting...\n",
            bcm63xx_spi_driver.driver.name);
         ret = -EBUSY;
      }
      else {
         ret = platform_driver_register(&(bcm63xx_spi_driver));
      }
      bcm_assert(((ret) && (0 == bcm_mpi_dev_data.ref_count))
         || ((!ret) && (1 == bcm_mpi_dev_data.ref_count)));
   }
   else {
      bcm_mpi_dev_data.ref_count += 1;
      ret = 0;
   }
   if (!ret) {
      bcm_assert(bcm_mpi_dev_data.ref_count > 0);
      if (params->cs > bcm_mpi_dev_data.num_chipselect) {
         dev_err(&(bcm_mpi_dev_data.pdev->dev), "%s, unsupported slave %d\n",
            __func__, params->cs);
         if (1 == bcm_mpi_dev_data.ref_count) {
            platform_driver_unregister(&(bcm63xx_spi_driver));
         }
         bcm_mpi_dev_data.ref_count -= 1;
         ret = -EINVAL;
      }
      else {
         t->dev_data = &(bcm_mpi_dev_data);
      }
   }
# else // BCMPH_NOHW
   ret = 0;
# endif // BCMPH_NOHW

#endif // !BCMPH_USE_SPI_DRIVER

   return (ret);
}
Exemple #23
0
static int __init add_pcd8544_device_to_bus(void)
{
	struct spi_master *spi_master;
	struct spi_device *spi_device;
	struct device *pdev;
	char buff[64];
	int status = 0;

	spi_master = spi_busnum_to_master(SPI_BUS);
	if (!spi_master) {
		printk(KERN_ALERT "spi_busnum_to_master(%d) returned NULL\n",
			SPI_BUS);
		printk(KERN_ALERT "Missing modprobe omap2_mcspi?\n");
		return -1;
	}

	spi_device = spi_alloc_device(spi_master);
	if (!spi_device) {
		put_device(&spi_master->dev);
		printk(KERN_ALERT "spi_alloc_device() failed\n");
		return -1;
	}

	spi_device->chip_select = SPI_BUS_CS;

	/* Check whether this SPI bus.cs is already claimed */
	snprintf(buff, sizeof(buff), "%s.%u", 
			dev_name(&spi_device->master->dev),
			spi_device->chip_select);

	pdev = bus_find_device_by_name(spi_device->dev.bus, NULL, buff);
 	if (pdev) {
		/* We are not going to use this spi_device, so free it */ 
		spi_dev_put(spi_device);
		
		/* 
		 * There is already a device configured for this bus.cs  
		 * It is okay if it us, otherwise complain and fail.
		 */
		if (pdev->driver && pdev->driver->name && 
				strcmp(this_driver_name, pdev->driver->name)) {
			printk(KERN_ALERT 
				"Driver [%s] already registered for %s\n",
				pdev->driver->name, buff);
			status = -1;
		} 
	} else {
		spi_device->max_speed_hz = SPI_BUS_SPEED;
		spi_device->mode = SPI_MODE_0;
		spi_device->bits_per_word = 8;
		spi_device->irq = -1;
		spi_device->controller_state = NULL;
		spi_device->controller_data = NULL;
		strlcpy(spi_device->modalias, this_driver_name, SPI_NAME_SIZE);
		
		status = spi_add_device(spi_device);		
		if (status < 0) {	
			spi_dev_put(spi_device);
			printk(KERN_ALERT "spi_add_device() failed: %d\n", 
				status);		
		}				
	}

	put_device(&spi_master->dev);

	return status;
}
static int cc2520_spi_add_to_bus(void)
{
    struct spi_master *spi_master;
    struct spi_device *spi_device;
    struct device *pdev;
    char buff[64];
    int status = 0;

    spi_master = spi_busnum_to_master(SPI_BUS);
    if (!spi_master) {
        printk(KERN_ALERT "[cc2520] - spi_busnum_to_master(%d) returned NULL\n",
            SPI_BUS);
        printk(KERN_ALERT "[cc2520] - Missing modprobe spi-bcm2708?\n");
        return -1;
    }

    spi_device = spi_alloc_device(spi_master);
    if (!spi_device) {
        put_device(&spi_master->dev);
        printk(KERN_ALERT "[cc2520] - spi_alloc_device() failed\n");
        return -1;
    }

    spi_device->chip_select = SPI_BUS_CS0;

    /* Check whether this SPI bus.cs is already claimed */
    snprintf(buff, sizeof(buff), "%s.%u", 
            dev_name(&spi_device->master->dev),
            spi_device->chip_select);

    pdev = bus_find_device_by_name(spi_device->dev.bus, NULL, buff);

    if (pdev) {
        if (pdev->driver != NULL) {
            printk(KERN_INFO
                "[cc2520] - Driver [%s] already registered for %s. Nuking from orbit.\n",
                pdev->driver->name, buff);            
        }
        else {
            printk(KERN_INFO
                "[cc2520] - Previous driver registered with no loaded module. Nuking from orbit.\n");
        }

        device_unregister(pdev);
    }

    spi_device->max_speed_hz = SPI_BUS_SPEED;
    spi_device->mode = SPI_MODE_0;
    spi_device->bits_per_word = 8;
    spi_device->irq = -1;

    spi_device->controller_state = NULL;
    spi_device->controller_data = NULL;
    strlcpy(spi_device->modalias, cc2520_name, SPI_NAME_SIZE);

    status = spi_add_device(spi_device);        
    if (status < 0) {   
        spi_dev_put(spi_device);
        printk(KERN_ALERT "[cc2520] - spi_add_device() failed: %d\n", 
            status);        
    }               

    put_device(&spi_master->dev);
    return status;
}
Exemple #25
0
static int __init add_nxtts_device_to_bus(void)
{
	struct spi_master *spi_master;
	struct spi_device *spi_device;
	struct device *pdev;
	char buff[64];
	int status = 0;

	/* This call returns a refcounted pointer to the relevant spi_master - the caller must release this pointer(device_put()) */	
	spi_master = spi_busnum_to_master(SPI_BUS);
	if (!spi_master) {
		printk(KERN_ALERT "spi_busnum_to_master(%d) returned NULL\n", SPI_BUS);
		printk(KERN_ALERT "Missing modprobe omap2_mcspi?\n");
		return -1;
	}

	spi_device = spi_alloc_device(spi_master);
	if (!spi_device) {
		printk(KERN_ALERT "spi_alloc_device() failed\n");
		return -1;
	}

	spi_device->chip_select = SPI_BUS_CS0;

	/* Check whether this SPI bus.cs is already claimed */
	/* snprintf the c-way of formatting a string */
	snprintf(buff, sizeof(buff), "%s.%u", dev_name(&spi_device->master->dev), spi_device->chip_select);

	pdev = bus_find_device_by_name(spi_device->dev.bus, NULL, buff);
 	if (pdev) {
		/* We are not going to use this spi_device, so free it. Since spi_device is not added then decrement the refcount */ 
		spi_dev_put(spi_device);

		/* 
		 * There is already a device configured for this bus.cs  
		 * It is okay if it us, otherwise complain and fail.
		 */
		if (pdev->driver && pdev->driver->name && strcmp(DEVICE_NAME, pdev->driver->name)) {
			printk(KERN_ALERT "Driver [%s] already registered for %s\n", pdev->driver->name, buff);
			status = -1;
		} 
	} else {
		spi_device->max_speed_hz = SPI_BUS_SPEED;
		spi_device->mode = SPI_MODE_0;
		spi_device->bits_per_word = SPI_BITS_PER_WORD;
		spi_device->irq = -1;
		spi_device->controller_state = NULL;
		spi_device->controller_data = NULL;
		strlcpy(spi_device->modalias, DEVICE_NAME, SPI_NAME_SIZE);

		status = spi_add_device(spi_device);		
		if (status < 0) {
			/* If spi_device is not added then decrement the refcount */	
			spi_dev_put(spi_device);
			printk(KERN_ALERT "spi_add_device() failed: %d\n", status);		
		}				
	}
	/* See comment for spi_busnum_to_master */
	put_device(&spi_master->dev);

	return status;
}