Ejemplo n.º 1
0
static int mma8452_probe(struct i2c_client *client,
                         const struct i2c_device_id *id)
{
    struct mma8452_data *data;
    struct iio_dev *indio_dev;
    int ret;
    const struct of_device_id *match;

    match = of_match_device(mma8452_dt_ids, &client->dev);
    if (!match) {
        dev_err(&client->dev, "unknown device model\n");
        return -ENODEV;
    }

    indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
    if (!indio_dev)
        return -ENOMEM;

    data = iio_priv(indio_dev);
    data->client = client;
    mutex_init(&data->lock);
    data->chip_info = match->data;

    ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
    if (ret < 0)
        return ret;

    switch (ret) {
    case MMA8452_DEVICE_ID:
    case MMA8453_DEVICE_ID:
    case MMA8652_DEVICE_ID:
    case MMA8653_DEVICE_ID:
        if (ret == data->chip_info->chip_id)
            break;
    default:
        return -ENODEV;
    }

    dev_info(&client->dev, "registering %s accelerometer; ID 0x%x\n",
             match->compatible, data->chip_info->chip_id);

    i2c_set_clientdata(client, indio_dev);
    indio_dev->info = &mma8452_info;
    indio_dev->name = id->name;
    indio_dev->dev.parent = &client->dev;
    indio_dev->modes = INDIO_DIRECT_MODE;
    indio_dev->channels = data->chip_info->channels;
    indio_dev->num_channels = data->chip_info->num_channels;
    indio_dev->available_scan_masks = mma8452_scan_masks;

    ret = mma8452_reset(client);
    if (ret < 0)
        return ret;

    data->data_cfg = MMA8452_DATA_CFG_FS_2G;
    ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
                                    data->data_cfg);
    if (ret < 0)
        return ret;

    /*
     * By default set transient threshold to max to avoid events if
     * enabling without configuring threshold.
     */
    ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS,
                                    MMA8452_TRANSIENT_THS_MASK);
    if (ret < 0)
        return ret;

    if (client->irq) {
        /*
         * Although we enable the interrupt sources once and for
         * all here the event detection itself is not enabled until
         * userspace asks for it by mma8452_write_event_config()
         */
        int supported_interrupts = MMA8452_INT_DRDY |
                                   MMA8452_INT_TRANS |
                                   MMA8452_INT_FF_MT;
        int enabled_interrupts = MMA8452_INT_TRANS |
                                 MMA8452_INT_FF_MT;
        int irq2;

        irq2 = of_irq_get_byname(client->dev.of_node, "INT2");

        if (irq2 == client->irq) {
            dev_dbg(&client->dev, "using interrupt line INT2\n");
        } else {
            ret = i2c_smbus_write_byte_data(client,
                                            MMA8452_CTRL_REG5,
                                            supported_interrupts);
            if (ret < 0)
                return ret;

            dev_dbg(&client->dev, "using interrupt line INT1\n");
        }

        ret = i2c_smbus_write_byte_data(client,
                                        MMA8452_CTRL_REG4,
                                        enabled_interrupts);
        if (ret < 0)
            return ret;

        ret = mma8452_trigger_setup(indio_dev);
        if (ret < 0)
            return ret;
    }

    data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
                      (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
    ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
                                    data->ctrl_reg1);
    if (ret < 0)
        goto trigger_cleanup;

    ret = iio_triggered_buffer_setup(indio_dev, NULL,
                                     mma8452_trigger_handler, NULL);
    if (ret < 0)
        goto trigger_cleanup;

    if (client->irq) {
        ret = devm_request_threaded_irq(&client->dev,
                                        client->irq,
                                        NULL, mma8452_interrupt,
                                        IRQF_TRIGGER_LOW | IRQF_ONESHOT,
                                        client->name, indio_dev);
        if (ret)
            goto buffer_cleanup;
    }

    ret = iio_device_register(indio_dev);
    if (ret < 0)
        goto buffer_cleanup;

    return 0;

buffer_cleanup:
    iio_triggered_buffer_cleanup(indio_dev);

trigger_cleanup:
    mma8452_trigger_cleanup(indio_dev);

    return ret;
}
Ejemplo n.º 2
0
static int mma8452_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
{
	struct mma8452_data *data;
	struct iio_dev *indio_dev;
	int ret;

	ret = i2c_smbus_read_byte_data(client, MMA8452_WHO_AM_I);
	if (ret < 0)
		return ret;
	if (ret != MMA8452_DEVICE_ID)
		return -ENODEV;

	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
	if (!indio_dev)
		return -ENOMEM;

	data = iio_priv(indio_dev);
	data->client = client;
	mutex_init(&data->lock);

	i2c_set_clientdata(client, indio_dev);
	indio_dev->info = &mma8452_info;
	indio_dev->name = id->name;
	indio_dev->dev.parent = &client->dev;
	indio_dev->modes = INDIO_DIRECT_MODE;
	indio_dev->channels = mma8452_channels;
	indio_dev->num_channels = ARRAY_SIZE(mma8452_channels);
	indio_dev->available_scan_masks = mma8452_scan_masks;

	ret = mma8452_reset(client);
	if (ret < 0)
		return ret;

	data->data_cfg = MMA8452_DATA_CFG_FS_2G;
	ret = i2c_smbus_write_byte_data(client, MMA8452_DATA_CFG,
					data->data_cfg);
	if (ret < 0)
		return ret;

	/*
	 * By default set transient threshold to max to avoid events if
	 * enabling without configuring threshold.
	 */
	ret = i2c_smbus_write_byte_data(client, MMA8452_TRANSIENT_THS,
					MMA8452_TRANSIENT_THS_MASK);
	if (ret < 0)
		return ret;

	if (client->irq) {
		/*
		 * Although we enable the transient interrupt source once and
		 * for all here the transient event detection itself is not
		 * enabled until userspace asks for it by
		 * mma8452_write_event_config()
		 */
		int supported_interrupts = MMA8452_INT_DRDY | MMA8452_INT_TRANS;
		int enabled_interrupts = MMA8452_INT_TRANS;

		/* Assume wired to INT1 pin */
		ret = i2c_smbus_write_byte_data(client,
						MMA8452_CTRL_REG5,
						supported_interrupts);
		if (ret < 0)
			return ret;

		ret = i2c_smbus_write_byte_data(client,
						MMA8452_CTRL_REG4,
						enabled_interrupts);
		if (ret < 0)
			return ret;

		ret = mma8452_trigger_setup(indio_dev);
		if (ret < 0)
			return ret;
	}

	data->ctrl_reg1 = MMA8452_CTRL_ACTIVE |
			  (MMA8452_CTRL_DR_DEFAULT << MMA8452_CTRL_DR_SHIFT);
	ret = i2c_smbus_write_byte_data(client, MMA8452_CTRL_REG1,
					data->ctrl_reg1);
	if (ret < 0)
		goto trigger_cleanup;

	ret = iio_triggered_buffer_setup(indio_dev, NULL,
					 mma8452_trigger_handler, NULL);
	if (ret < 0)
		goto trigger_cleanup;

	if (client->irq) {
		ret = devm_request_threaded_irq(&client->dev,
						client->irq,
						NULL, mma8452_interrupt,
						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
						client->name, indio_dev);
		if (ret)
			goto buffer_cleanup;
	}

	ret = iio_device_register(indio_dev);
	if (ret < 0)
		goto buffer_cleanup;

	return 0;

buffer_cleanup:
	iio_triggered_buffer_cleanup(indio_dev);

trigger_cleanup:
	mma8452_trigger_cleanup(indio_dev);

	return ret;
}