linux/drivers/iio/common/st_sensors/st_sensors_i2c.c
<<
>>
Prefs
   1/*
   2 * STMicroelectronics sensors i2c library driver
   3 *
   4 * Copyright 2012-2013 STMicroelectronics Inc.
   5 *
   6 * Denis Ciocca <denis.ciocca@st.com>
   7 *
   8 * Licensed under the GPL-2.
   9 */
  10
  11#include <linux/kernel.h>
  12#include <linux/module.h>
  13#include <linux/slab.h>
  14#include <linux/iio/iio.h>
  15
  16#include <linux/iio/common/st_sensors_i2c.h>
  17
  18
  19#define ST_SENSORS_I2C_MULTIREAD        0x80
  20
  21static unsigned int st_sensors_i2c_get_irq(struct iio_dev *indio_dev)
  22{
  23        struct st_sensor_data *sdata = iio_priv(indio_dev);
  24
  25        return to_i2c_client(sdata->dev)->irq;
  26}
  27
  28static int st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer *tb,
  29                                struct device *dev, u8 reg_addr, u8 *res_byte)
  30{
  31        int err;
  32
  33        err = i2c_smbus_read_byte_data(to_i2c_client(dev), reg_addr);
  34        if (err < 0)
  35                goto st_accel_i2c_read_byte_error;
  36
  37        *res_byte = err & 0xff;
  38
  39st_accel_i2c_read_byte_error:
  40        return err < 0 ? err : 0;
  41}
  42
  43static int st_sensors_i2c_read_multiple_byte(
  44                struct st_sensor_transfer_buffer *tb, struct device *dev,
  45                        u8 reg_addr, int len, u8 *data, bool multiread_bit)
  46{
  47        if (multiread_bit)
  48                reg_addr |= ST_SENSORS_I2C_MULTIREAD;
  49
  50        return i2c_smbus_read_i2c_block_data(to_i2c_client(dev),
  51                                                        reg_addr, len, data);
  52}
  53
  54static int st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer *tb,
  55                                struct device *dev, u8 reg_addr, u8 data)
  56{
  57        return i2c_smbus_write_byte_data(to_i2c_client(dev), reg_addr, data);
  58}
  59
  60static const struct st_sensor_transfer_function st_sensors_tf_i2c = {
  61        .read_byte = st_sensors_i2c_read_byte,
  62        .write_byte = st_sensors_i2c_write_byte,
  63        .read_multiple_byte = st_sensors_i2c_read_multiple_byte,
  64};
  65
  66void st_sensors_i2c_configure(struct iio_dev *indio_dev,
  67                struct i2c_client *client, struct st_sensor_data *sdata)
  68{
  69        i2c_set_clientdata(client, indio_dev);
  70
  71        indio_dev->dev.parent = &client->dev;
  72        indio_dev->name = client->name;
  73
  74        sdata->tf = &st_sensors_tf_i2c;
  75        sdata->get_irq_data_ready = st_sensors_i2c_get_irq;
  76}
  77EXPORT_SYMBOL(st_sensors_i2c_configure);
  78
  79MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  80MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
  81MODULE_LICENSE("GPL v2");
  82