static int bma220_probe(struct spi_device *spi) { int ret; struct iio_dev *indio_dev; struct bma220_data *data; indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data)); if (!indio_dev) { dev_err(&spi->dev, "iio allocation failed!\n"); return -ENOMEM; } data = iio_priv(indio_dev); data->spi_device = spi; spi_set_drvdata(spi, indio_dev); mutex_init(&data->lock); indio_dev->dev.parent = &spi->dev; indio_dev->info = &bma220_info; indio_dev->name = BMA220_DEVICE_NAME; indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->channels = bma220_channels; indio_dev->num_channels = ARRAY_SIZE(bma220_channels); indio_dev->available_scan_masks = bma220_accel_scan_masks; ret = bma220_init(data->spi_device); if (ret < 0) return ret; ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time, bma220_trigger_handler, NULL); if (ret < 0) { dev_err(&spi->dev, "iio triggered buffer setup failed\n"); goto err_suspend; } ret = iio_device_register(indio_dev); if (ret < 0) { dev_err(&spi->dev, "iio_device_register failed\n"); iio_triggered_buffer_cleanup(indio_dev); goto err_suspend; } return 0; err_suspend: return bma220_deinit(spi); }
static int bma_probe(struct i2c_client *client, const struct i2c_device_id *id) { int err = 0; int tempvalue; struct bma_data *data; #ifdef BMA_DEBUG printk(KERN_INFO "%s\n",__FUNCTION__); #endif if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { printk(KERN_INFO "i2c_check_functionality error\n"); goto exit; } data = kzalloc(sizeof(struct bma_data), GFP_KERNEL); if (!data) { err = -ENOMEM; goto exit; } printk("[%s] slave addr = %x\n", __func__, client->addr); /* read chip id */ tempvalue = 0; #if 1//BMA220_SMBUS tempvalue = i2c_smbus_read_word_data(client, 0x00); #else i2c_master_send(client, (char*)&tempvalue, 1); i2c_master_recv(client, (char*)&tempvalue, 1); #endif if((tempvalue&0x0007) == 0x0002) { printk(KERN_INFO "Bosch Sensortec Device detected!\nBMA023 registered I2C driver!\n"); bma_client = client; sensor_type = BMA023; } else { client->addr = BMA220_I2C_ADDR; tempvalue = 0; tempvalue = i2c_smbus_read_word_data(client, 0x00); if((tempvalue&0x00FF) == 0x00dd) { printk(KERN_INFO "Bosch Sensortec Device detected!\nBMA220 registered I2C driver!\n"); bma_client = client; sensor_type= BMA220; } else { printk(KERN_ERR "Bosch Sensortec Device not found, i2c error %d \n", tempvalue); // i2c_detach_client(client); bma_client = NULL; err = -1; goto kfree_exit; } } i2c_set_clientdata(bma_client, data); err = misc_register(&bma_device); if (err) { printk(KERN_ERR "bma accel device register failed\n"); goto kfree_exit; } printk(KERN_INFO "bma accel device create ok\n"); if(sensor_type == BMA220) { /* bma220 sensor initial */ data->bma220.bus_write = bma220_i2c_write; data->bma220.bus_read = bma220_i2c_read; data->bma220.delay_msec = bma220_i2c_delay; bma220_init(&(data->bma220)); bma220_set_bandwidth(2); //bandwidth 250Hz bma220_set_range(0); //range +/- 2G /* register interrupt */ bma220_set_en_tt_xyz(0); bma220_set_en_slope_xyz(0); bma220_set_en_high_xyz(0); bma220_set_en_low(0); bma220_set_en_orient(0); bma220_reset_int(); } else if(sensor_type == BMA023) { data->bma023.bma023_bus_write = i2c_acc_bma023_write; data->bma023.bma023_bus_read = i2c_acc_bma023_read; /*call init function to set read write functions, read registers */ bma023_init( &(data->bma023) ); /* from this point everything is prepared for sensor communication */ /* set range to 2G mode, other constants: * 4G: bma023_RANGE_4G, * 8G: bma023_RANGE_8G */ bma023_set_range(bma023_RANGE_2G); /* set bandwidth to 25 HZ */ bma023_set_bandwidth(bma023_BW_25HZ); } return 0; // misc_deregister(&bma_device); kfree_exit: kfree(data); exit: return err; }