Exemplo n.º 1
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);
     }
}
Exemplo n.º 2
0
void cc2520_plat_spi_free()
{
    if (state.spi_device)
        spi_unregister_device(state.spi_device);

    spi_unregister_driver(&cc2520_spi_driver);
}
struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
		struct spi_master *master, struct spi_board_info *info)
{
	struct v4l2_subdev *sd = NULL;
	struct spi_device *spi = NULL;

	BUG_ON(!v4l2_dev);

	if (info->modalias)
		request_module(info->modalias);

	spi = spi_new_device(master, info);

	if (spi == NULL || spi->dev.driver == NULL)
		goto error;

	if (!try_module_get(spi->dev.driver->owner))
		goto error;

	sd = spi_get_drvdata(spi);

	if (v4l2_device_register_subdev(v4l2_dev, sd))
		sd = NULL;

	
	module_put(spi->dev.driver->owner);

error:
	if (spi && sd == NULL)
		spi_unregister_device(spi);

	return sd;
}
Exemplo n.º 4
0
int BcmSpiReleaseSlave(int busNum, int slaveId)
{
    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 "BcmSpiReleaseSlave - slaveId %d, already released\n", slaveId);
            return( SPI_STATUS_ERR );
        }

        bcmLegSpiDevInfo[slaveId].max_speed_hz = 781000;
        spi_unregister_driver(&bcmLegSpiDevDrv[slaveId]);
        spi_unregister_device(bcmLegSpiDevices[slaveId]);
        bcmLegSpiDevices[slaveId] = 0;
#endif
    }
    else if ( HS_SPI_BUS_NUM == busNum )
    {
#ifndef HS_SPI
        return( SPI_STATUS_ERR );
#else
        if ( NULL == bcmHSSpiDevices[slaveId] )
        {
            printk(KERN_ERR "BcmSpiReleaseSlave - slaveId %d, already released\n", slaveId);
            return( SPI_STATUS_ERR );
        }

        bcmHSSpiDevInfo[slaveId].max_speed_hz = 781000;
        spi_unregister_driver(&bcmHSSpiDevDrv[slaveId]);
        spi_unregister_device(bcmHSSpiDevices[slaveId]);
        bcmHSSpiDevices[slaveId] = 0;
#endif        
    }
    else
        return( SPI_STATUS_ERR );

    return 0;
    
}
Exemplo n.º 5
0
// -----------------------------------------------------------------------------------
static void __exit exit_maspi(void)
{
  if (maspi_device)
  {
    printk(KERN_INFO "unregistering maspi spi device\n");
    spi_unregister_device(maspi_device);
  }
}
Exemplo n.º 6
0
/******************************
 Module exit function
******************************/
static void __exit nrf_exit(void)
{
	printk(KERN_ALERT "Bye from module!!\n");
	nrf_power_down();
	spi_unregister_device(spi_device);
	device_destroy(nrf_class,MKDEV(MAJOR(dev),0));
	class_unregister(nrf_class);
	class_destroy(nrf_class);
	cdev_del(nrf_cdev);
	unregister_chrdev_region(MKDEV(MAJOR(dev),0),1);	
}
Exemplo n.º 7
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 );

}
Exemplo n.º 8
0
static void butterfly_detach(struct parport *p)
{
	struct butterfly	*pp;
	struct platform_device	*pdev;
	int			status;

	/* FIXME this global is ugly ... but, how to quickly get from
	 * the parport to the "struct butterfly" associated with it?
	 * "old school" driver-internal device lists?
	 */
	if (!butterfly || butterfly->port != p)
		return;
	pp = butterfly;
	butterfly = NULL;

#ifdef	HAVE_USI
	spi_unregister_device(pp->butterfly);
	pp->butterfly = NULL;
#endif
	spi_unregister_device(pp->dataflash);
	pp->dataflash = NULL;

	status = spi_bitbang_stop(&pp->bitbang);

	/* turn off VCC */
	parport_write_data(pp->port, 0);
	msleep(10);

	parport_release(pp->pd);
	parport_unregister_device(pp->pd);

	pdev = to_platform_device(pp->bitbang.master->cdev.dev);

	(void) spi_master_put(pp->bitbang.master);

	platform_device_unregister(pdev);
}
Exemplo n.º 9
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 );
}
Exemplo n.º 10
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);
Exemplo n.º 11
0
void bcm_mpi_deinit(bcm_mpi_t *t)
{
   bcm_pr_debug("%s()\n", __func__);

#ifdef BCMPH_DEBUG_MPI
   bcm_mpi_dump_and_reset_trace(t);
#endif


#ifdef BCMPH_USE_SPI_DRIVER

#ifndef BCMPH_NOHW
   if (t->bus_is_locked) {
      spi_bus_unlock(t->dev->master);
      t->bus_is_locked = false;
   }
   spi_unregister_device(t->dev);
#endif // !BCMPH_NOHW
   t->dev = NULL;

#else // !BCMPH_USE_SPI_DRIVER

#ifndef BCMPH_NOHW
   if (bcm_mpi_dev_data.ref_count > 0) {
      if (1 == bcm_mpi_dev_data.ref_count) {
         platform_driver_unregister(&(bcm63xx_spi_driver));
      }
      else {
         bcm_mpi_dev_data.ref_count -= 1;
      }
   }
   else {
      bcm_pr_err("Illegal call of %s()\n", __func__);
   }
#endif // !BCMPH_NOHW

#endif // !BCMPH_USE_SPI_DRIVER
}
Exemplo n.º 12
0
struct v4l2_subdev *v4l2_spi_new_subdev(struct v4l2_device *v4l2_dev,
		struct spi_master *master, struct spi_board_info *info)
{
	struct v4l2_subdev *sd = NULL;
	struct spi_device *spi = NULL;

	BUG_ON(!v4l2_dev);

	if (info->modalias)
		request_module(info->modalias);

	spi = spi_new_device(master, info);

	if (spi == NULL || spi->dev.driver == NULL)
		goto error;

	if (!try_module_get(spi->dev.driver->owner))
		goto error;

	sd = spi_get_drvdata(spi);

	/* Register with the v4l2_device which increases the module's
	   use count as well. */
	if (v4l2_device_register_subdev(v4l2_dev, sd))
		sd = NULL;

	/* Decrease the module use count to match the first try_module_get. */
	module_put(spi->dev.driver->owner);

error:
	/* If we have a client but no subdev, then something went wrong and
	   we must unregister the client. */
	if (spi && sd == NULL)
		spi_unregister_device(spi);

	return sd;
}
Exemplo n.º 13
0
static void __exit low_speed_spidev_module_exit(void)
{
	pr_info("module exit");
	if (dev)
		spi_unregister_device(dev);
}
Exemplo n.º 14
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);
}
Exemplo n.º 15
0
static void __exit mcp3901_exit(void)
{
	spi_unregister_device(mcp3901_device);
	spi_unregister_driver(&mcp3901_driver);
}
Exemplo n.º 16
0
static void __exit galileo_module_exit(void)
{
	pr_info("module exit");
	if (dev)
		spi_unregister_device(dev);
}
static void hotplug_spi_exit(void)
{
  printk(KERN_ALERT "Removing 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);
  spi_unregister_device(slave_spi_device);
}
Exemplo n.º 18
0
static void qtft_spi_device_unregister(struct spi_device *device)
{
	spi_unregister_device(device);
}
Exemplo n.º 19
0
/** 
 * pmodad1_of_probe - Probe method for PmodAD1 device (over GPIO).
 * @pdev: pointer to platform devices 
 *
 * This function probes the PmodAD1 device in the device tree. It initializes the
 * PmodAD1 driver data structure. It returns 0, if the driver is bound to the PmodAD1
 * device, or a negative value if there is an error.
 */
static int __devinit pmodad1_of_probe(struct platform_device *pdev)
{
	struct pmodad1_device *pmodad1_dev;
	struct platform_device *pmodad1_pdev;
	struct spi_gpio_platform_data *pmodad1_pdata;

	struct device_node *np = pdev->dev.of_node;

	const u32* tree_info;
	const u32* spi_speed;
	int status = 0;

	/* Alloc Space for platform device structure */
	pmodad1_dev = (struct pmodad1_device*) kzalloc(sizeof(*pmodad1_dev), GFP_KERNEL);
	if(!pmodad1_dev) {
		status = -ENOMEM;
		dev_err(&pdev->dev, "Platform device structure allocation "
                                        "failed: %d\n", status);
		goto dev_alloc_err;
	}
	

	pmodad1_dev->val_buf = (unsigned short *)kmalloc(read_buf_size, GFP_KERNEL);
	if (!pmodad1_dev->val_buf) {
		status = -ENOMEM;
		dev_err(&pdev->dev, "Device value buffer allocation "
                                        "failed: %d\n", status);
		goto buf_alloc_err;
	}

	/* Get the GPIO Pins */

	pmodad1_dev->iSCLK = of_get_named_gpio(np, "spi-sclk-gpio", 0); 
	pmodad1_dev->iSDOUT = of_get_named_gpio(np, "spi-sdout-gpio", 0); 
	status = of_get_named_gpio(np, "spi-cs-gpio", 0);
	pmodad1_dev->iCS = (status < 0) ? SPI_GPIO_NO_CHIPSELECT : status;

#ifdef CONFIG_PMODS_DEBUG	
	printk(KERN_INFO DRIVER_NAME " %s: iSCLK: 0x%lx\n", np->name, pmodad1_dev->iSCLK);
	printk(KERN_INFO DRIVER_NAME " %s: iSDOUT: 0x%lx\n", np->name, pmodad1_dev->iSDOUT);
	printk(KERN_INFO DRIVER_NAME " %s: iCS  : 0x%lx\n", np->name, pmodad1_dev->iCS);
#endif
	
	/* Get SPI Related Params */
	tree_info = of_get_property(np, "spi-bus-num", NULL);
	if(tree_info) {
		pmodad1_dev->spi_id = be32_to_cpup((tree_info));
#ifdef CONFIG_PMODS_DEBUG
		printk(KERN_INFO DRIVER_NAME " %s: BUS_ID\t%x\n", np->name, pmodad1_dev->spi_id);
#endif
	}

	spi_speed = of_get_property(np, "spi-speed-hz", NULL);
	if(spi_speed) {
		pmodad1_dev->spi_speed = be32_to_cpup((spi_speed));
#ifdef CONFIG_PMODS_DEBUG
		printk(KERN_INFO DRIVER_NAME " %s: SPI_SPEED\t%x\n", np->name, pmodad1_dev->spi_speed);
#endif
	}
	else
	{
		pmodad1_dev->spi_speed = DEFAULT_SPI_SPEED;
	}

	/* Alloc Space for platform data structure */
	pmodad1_pdata = (struct spi_gpio_platform_data*) kzalloc(sizeof(*pmodad1_pdata), GFP_KERNEL);
	if(!pmodad1_pdata) {
		status = -ENOMEM;
		goto pdata_alloc_err;
	}

	/* Fill up Platform Data Structure */
	pmodad1_pdata->sck = pmodad1_dev->iSCLK;
	pmodad1_pdata->miso = pmodad1_dev->iSDOUT;
	pmodad1_pdata->mosi = SPI_GPIO_NO_MOSI;
	pmodad1_pdata->num_chipselect = 1;
	
	/* Alloc Space for platform data structure */
	pmodad1_pdev = (struct platform_device*) kzalloc(sizeof(*pmodad1_pdev), GFP_KERNEL);
	if(!pmodad1_pdev) {
		status = -ENOMEM;
		goto pdev_alloc_err;
	}
	
	/* Fill up Platform Device Structure */	
	pmodad1_pdev->name = "spi_gpio";
	pmodad1_pdev->id = pmodad1_dev->spi_id;
	pmodad1_pdev->dev.platform_data = pmodad1_pdata;
	pmodad1_dev->pdev = pmodad1_pdev;

	/* Register spi_gpio master */
	status = platform_device_register(pmodad1_dev->pdev);
	if(status < 0) {
		dev_err(&pdev->dev, "platform_device_register failed: %d\n", status);
		goto pdev_reg_err;
	}

#ifdef CONFIG_PMODS_DEBUG
	printk(KERN_INFO DRIVER_NAME " %s: spi_gpio platform device registered.\n", np->name);
#endif
	pmodad1_dev->name = (char *)np->name;	
	
	/* Fill up Board Info for SPI device */
	status = add_pmodad1_device_to_bus(pmodad1_dev);
	if(status < 0) {
		dev_err(&pdev->dev, "add_pmodad1_device_to_bus failed: %d\n", status);
		goto spi_add_err;
	}

#ifdef CONFIG_PMODS_DEBUG
	printk(KERN_INFO DRIVER_NAME " %s: spi device registered.\n", np->name);
#endif

	/* Point device node data to pmodad1_device structure */
	if(np->data == NULL)
		np->data = pmodad1_dev;
	
	if(pmodad1_dev_id == 0) {
		/* Alloc Major & Minor number for char device */
		status = alloc_chrdev_region(&pmodad1_dev_id, 0, MAX_PMODAD1_DEV_NUM, DRIVER_NAME);
		if(status) {
			dev_err(&pdev->dev, "Character device region not allocated correctly: %d\n", status);
			goto err_alloc_chrdev_region;
		}
#ifdef CONFIG_PMODS_DEBUG
		printk(KERN_INFO DRIVER_NAME " : Char Device Region Registered, with Major: %d.\n", 
						MAJOR(pmodad1_dev_id));
#endif
	}
	
	if(pmodad1_class == NULL) {
		/* Create Pmodad1 Device Class */
		pmodad1_class = class_create(THIS_MODULE, DRIVER_NAME);
		if (IS_ERR(pmodad1_class)) {
			status = PTR_ERR(pmodad1_class);
			goto err_create_class;
		}
#ifdef CONFIG_PMODS_DEBUG
		printk(KERN_INFO DRIVER_NAME " : pmodad1 device class registered.\n");
#endif
	}
	
	if(spi_drv_registered == 0) {
		/* Register SPI Driver for Pmodad1 Device */
		status = spi_register_driver(&pmodad1_spi_driver);
		if (status < 0) {
			dev_err(&pdev->dev, "pmodad1_spi_driver register failed: %d\n", status);
			goto err_spi_register;
		}
		spi_drv_registered = 1;
	}
	
	device_num ++;

	return status;	

err_spi_register:
	class_destroy(pmodad1_class);
	pmodad1_class = NULL;
err_create_class:
	unregister_chrdev_region(pmodad1_dev_id, MAX_PMODAD1_DEV_NUM);
	pmodad1_dev_id = 0;
err_alloc_chrdev_region:
	spi_unregister_device(pmodad1_dev->spi);
spi_add_err:
	platform_device_unregister(pmodad1_dev->pdev);
pdev_reg_err:
	kfree(pmodad1_pdev);
pdev_alloc_err:
	kfree(pmodad1_pdata);
pdata_alloc_err:
	kfree(pmodad1_dev->val_buf);
buf_alloc_err:
	kfree(pmodad1_dev);
dev_alloc_err:
	return status;
}
Exemplo n.º 20
0
/**
 * gpio_pmodoled_of_probe - Probe method for PmodOLED device (over GPIO).
 * @pdev: pointer to platform devices
 *
 * This function probes the OLED device in the device tree. It initializes the
 * OLED driver data structure. It returns 0, if the driver is bound to the OLED
 * device, or a negative value if there is an error.
 */
static int gpio_pmodoled_of_probe(struct platform_device *pdev)
{
	struct gpio_pmodoled_device *gpio_pmodoled_dev;
	struct platform_device *gpio_pmodoled_pdev;
	struct spi_gpio_platform_data *gpio_pmodoled_pdata;

	struct device_node *np = pdev->dev.of_node;

	const u32 *tree_info;
	int status = 0;

	/* Alloc Space for platform device structure */
	gpio_pmodoled_dev = (struct gpio_pmodoled_device *) kzalloc(sizeof(*gpio_pmodoled_dev), GFP_KERNEL);
	if (!gpio_pmodoled_dev) {
		status = -ENOMEM;
		goto dev_alloc_err;
	}

	/* Alloc Graphic Buffer for device */
	gpio_pmodoled_dev->disp_buf = (uint8_t *) kmalloc(DISPLAY_BUF_SZ, GFP_KERNEL);
	if (!gpio_pmodoled_dev->disp_buf) {
		status = -ENOMEM;
		dev_err(&pdev->dev, "Device Display data buffer allocation failed: %d\n", status);
		goto disp_buf_alloc_err;
	}

	/* Get the GPIO Pins */
	gpio_pmodoled_dev->iVBAT = of_get_named_gpio(np, "vbat-gpio", 0);
	gpio_pmodoled_dev->iVDD = of_get_named_gpio(np, "vdd-gpio", 0);
	gpio_pmodoled_dev->iRES = of_get_named_gpio(np, "res-gpio", 0);
	gpio_pmodoled_dev->iDC = of_get_named_gpio(np, "dc-gpio", 0);
	gpio_pmodoled_dev->iSCLK = of_get_named_gpio(np, "spi-sclk-gpio", 0);
	gpio_pmodoled_dev->iSDIN = of_get_named_gpio(np, "spi-sdin-gpio", 0);
	status = of_get_named_gpio(np, "spi-cs-gpio", 0);
	gpio_pmodoled_dev->iCS = (status < 0) ? SPI_GPIO_NO_CHIPSELECT : status;
#ifdef CONFIG_PMODS_DEBUG
	printk(KERN_INFO DRIVER_NAME " %s: iVBAT: 0x%lx\n", np->name, gpio_pmodoled_dev->iVBAT);
	printk(KERN_INFO DRIVER_NAME " %s: iVDD : 0x%lx\n", np->name, gpio_pmodoled_dev->iVDD);
	printk(KERN_INFO DRIVER_NAME " %s: iRES : 0x%lx\n", np->name, gpio_pmodoled_dev->iRES);
	printk(KERN_INFO DRIVER_NAME " %s: iDC  : 0x%lx\n", np->name, gpio_pmodoled_dev->iDC);
	printk(KERN_INFO DRIVER_NAME " %s: iSCLK: 0x%lx\n", np->name, gpio_pmodoled_dev->iSCLK);
	printk(KERN_INFO DRIVER_NAME " %s: iSDIN: 0x%lx\n", np->name, gpio_pmodoled_dev->iSDIN);
	printk(KERN_INFO DRIVER_NAME " %s: iCS  : 0x%lx\n", np->name, gpio_pmodoled_dev->iCS);
#endif

	/* Get SPI Related Params */
	tree_info = of_get_property(np, "spi-bus-num", NULL);
	if (tree_info) {
		gpio_pmodoled_dev->spi_id = be32_to_cpup((tree_info));
#ifdef CONFIG_PMODS_DEBUG
		printk(KERN_INFO DRIVER_NAME " %s: BUS_ID\t%x\n", np->name, gpio_pmodoled_dev->spi_id);
#endif
	}

	/* Alloc Space for platform data structure */
	gpio_pmodoled_pdata = (struct spi_gpio_platform_data *) kzalloc(sizeof(*gpio_pmodoled_pdata), GFP_KERNEL);
	if (!gpio_pmodoled_pdata) {
		status = -ENOMEM;
		goto pdata_alloc_err;
	}

	/* Fill up Platform Data Structure */
	gpio_pmodoled_pdata->sck = gpio_pmodoled_dev->iSCLK;
	gpio_pmodoled_pdata->miso = SPI_GPIO_NO_MISO;
	gpio_pmodoled_pdata->mosi = gpio_pmodoled_dev->iSDIN;
	gpio_pmodoled_pdata->num_chipselect = 1;

	/* Alloc Space for platform data structure */
	gpio_pmodoled_pdev = (struct platform_device *) kzalloc(sizeof(*gpio_pmodoled_pdev), GFP_KERNEL);
	if (!gpio_pmodoled_pdev) {
		status = -ENOMEM;
		goto pdev_alloc_err;
	}

	/* Fill up Platform Device Structure */
	gpio_pmodoled_pdev->name = "spi_gpio";
	gpio_pmodoled_pdev->id = gpio_pmodoled_dev->spi_id;
	gpio_pmodoled_pdev->dev.platform_data = gpio_pmodoled_pdata;
	gpio_pmodoled_dev->pdev = gpio_pmodoled_pdev;

	/* Register spi_gpio master */
	status = platform_device_register(gpio_pmodoled_dev->pdev);
	if (status < 0) {
		dev_err(&pdev->dev, "platform_device_register failed: %d\n", status);
		goto pdev_reg_err;
	}

#ifdef CONFIG_PMODS_DEBUG
	printk(KERN_INFO DRIVER_NAME " %s: spi_gpio platform device registered.\n", np->name);
#endif
	gpio_pmodoled_dev->name = np->name;

	/* Fill up Board Info for SPI device */
	status = add_gpio_pmodoled_device_to_bus(gpio_pmodoled_dev);
	if (status < 0) {
		dev_err(&pdev->dev, "add_gpio_pmodoled_device_to_bus failed: %d\n", status);
		goto spi_add_err;
	}

#ifdef CONFIG_PMODS_DEBUG
	printk(KERN_INFO DRIVER_NAME " %s: spi device registered.\n", np->name);
#endif

	/* Point device node data to gpio_pmodoled_device structure */
	if (np->data == NULL)
		np->data = gpio_pmodoled_dev;

	if (gpio_pmodoled_dev_id == 0) {
		/* Alloc Major & Minor number for char device */
		status = alloc_chrdev_region(&gpio_pmodoled_dev_id, 0, MAX_PMODOLED_GPIO_DEV_NUM, DRIVER_NAME);
		if (status) {
			dev_err(&pdev->dev, "Character device region not allocated correctly: %d\n", status);
			goto err_alloc_chrdev_region;
		}
#ifdef CONFIG_PMODS_DEBUG
		printk(KERN_INFO DRIVER_NAME " : Char Device Region Registered, with Major: %d.\n",
						MAJOR(gpio_pmodoled_dev_id));
#endif
	}

	if (gpio_pmodoled_class == NULL) {
		/* Create Pmodoled-gpio Device Class */
		gpio_pmodoled_class = class_create(THIS_MODULE, DRIVER_NAME);
		if (IS_ERR(gpio_pmodoled_class)) {
			status = PTR_ERR(gpio_pmodoled_class);
			goto err_create_class;
		}
#ifdef CONFIG_PMODS_DEBUG
		printk(KERN_INFO DRIVER_NAME " : pmodoled_gpio device class registered.\n");
#endif
	}

	if (spi_drv_registered == 0) {
		/* Register SPI Driver for Pmodoled Device */
		status = spi_register_driver(&gpio_pmodoled_spi_driver);
		if (status < 0) {
			dev_err(&pdev->dev, "gpio_pmodoled_spi_driver register failed: %d\n", status);
			goto err_spi_register;
		}
		spi_drv_registered = 1;
	}

	device_num++;

	return status;

err_spi_register:
	class_destroy(gpio_pmodoled_class);
	gpio_pmodoled_class = NULL;
err_create_class:
	unregister_chrdev_region(gpio_pmodoled_dev_id, MAX_PMODOLED_GPIO_DEV_NUM);
	gpio_pmodoled_dev_id = 0;
err_alloc_chrdev_region:
	spi_unregister_device(gpio_pmodoled_dev->spi);
spi_add_err:
	platform_device_unregister(gpio_pmodoled_dev->pdev);
pdev_reg_err:
	kfree(gpio_pmodoled_pdev);
pdev_alloc_err:
	kfree(gpio_pmodoled_pdata);
pdata_alloc_err:
	kfree(gpio_pmodoled_dev->disp_buf);
disp_buf_alloc_err:
	kfree(gpio_pmodoled_dev);
dev_alloc_err:
	return status;
}
Exemplo n.º 21
0
/******************************
 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;
}