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; }
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; }
void of_register_spi_devices(struct spi_master *master, struct device_node *np) { struct spi_device *spi; struct device_node *nc; const __be32 *prop; int rc; int len; for_each_child_of_node(np, nc) { /* Alloc an spi_device */ spi = spi_alloc_device(master); if (!spi) { dev_err(&master->dev, "spi_device alloc error for %s\n", nc->full_name); spi_dev_put(spi); continue; } /* Select device driver */ if (of_modalias_node(nc, spi->modalias, sizeof(spi->modalias)) < 0) { dev_err(&master->dev, "cannot find modalias for %s\n", nc->full_name); spi_dev_put(spi); continue; } /* Device address */ prop = of_get_property(nc, "reg", &len); if (!prop || len < sizeof(*prop)) { dev_err(&master->dev, "%s has no 'reg' property\n", nc->full_name); spi_dev_put(spi); continue; } spi->chip_select = be32_to_cpup(prop); /* Mode (clock phase/polarity/etc.) */ if (of_find_property(nc, "spi-cpha", NULL)) spi->mode |= SPI_CPHA; if (of_find_property(nc, "spi-cpol", NULL)) spi->mode |= SPI_CPOL; if (of_find_property(nc, "spi-cs-high", NULL)) spi->mode |= SPI_CS_HIGH; /* Device speed */ prop = of_get_property(nc, "spi-max-frequency", &len); if (!prop || len < sizeof(*prop)) { dev_err(&master->dev, "%s has no 'spi-max-frequency' property\n", nc->full_name); spi_dev_put(spi); continue; } spi->max_speed_hz = be32_to_cpup(prop); /* IRQ */ spi->irq = irq_of_parse_and_map(nc, 0); /* Store a pointer to the node in the device structure */ of_node_get(nc); spi->dev.of_node = nc; /* Register the new device */ request_module(spi->modalias); rc = spi_add_device(spi); if (rc) { dev_err(&master->dev, "spi_device register error %s\n", nc->full_name); spi_dev_put(spi); } }
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 __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; }