st_sensors_i2c.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/of_device.h>
  15. #include <linux/iio/common/st_sensors_i2c.h>
  16. #define ST_SENSORS_I2C_MULTIREAD 0x80
  17. static unsigned int st_sensors_i2c_get_irq(struct iio_dev *indio_dev)
  18. {
  19. struct st_sensor_data *sdata = iio_priv(indio_dev);
  20. return to_i2c_client(sdata->dev)->irq;
  21. }
  22. static int st_sensors_i2c_read_byte(struct st_sensor_transfer_buffer *tb,
  23. struct device *dev, u8 reg_addr, u8 *res_byte)
  24. {
  25. int err;
  26. err = i2c_smbus_read_byte_data(to_i2c_client(dev), reg_addr);
  27. if (err < 0)
  28. goto st_accel_i2c_read_byte_error;
  29. *res_byte = err & 0xff;
  30. st_accel_i2c_read_byte_error:
  31. return err < 0 ? err : 0;
  32. }
  33. static int st_sensors_i2c_read_multiple_byte(
  34. struct st_sensor_transfer_buffer *tb, struct device *dev,
  35. u8 reg_addr, int len, u8 *data, bool multiread_bit)
  36. {
  37. if (multiread_bit)
  38. reg_addr |= ST_SENSORS_I2C_MULTIREAD;
  39. return i2c_smbus_read_i2c_block_data_or_emulated(to_i2c_client(dev),
  40. reg_addr, len, data);
  41. }
  42. static int st_sensors_i2c_write_byte(struct st_sensor_transfer_buffer *tb,
  43. struct device *dev, u8 reg_addr, u8 data)
  44. {
  45. return i2c_smbus_write_byte_data(to_i2c_client(dev), reg_addr, data);
  46. }
  47. static const struct st_sensor_transfer_function st_sensors_tf_i2c = {
  48. .read_byte = st_sensors_i2c_read_byte,
  49. .write_byte = st_sensors_i2c_write_byte,
  50. .read_multiple_byte = st_sensors_i2c_read_multiple_byte,
  51. };
  52. void st_sensors_i2c_configure(struct iio_dev *indio_dev,
  53. struct i2c_client *client, struct st_sensor_data *sdata)
  54. {
  55. i2c_set_clientdata(client, indio_dev);
  56. indio_dev->dev.parent = &client->dev;
  57. indio_dev->name = client->name;
  58. sdata->dev = &client->dev;
  59. sdata->tf = &st_sensors_tf_i2c;
  60. sdata->get_irq_data_ready = st_sensors_i2c_get_irq;
  61. }
  62. EXPORT_SYMBOL(st_sensors_i2c_configure);
  63. #ifdef CONFIG_OF
  64. /**
  65. * st_sensors_of_i2c_probe() - device tree probe for ST I2C sensors
  66. * @client: the I2C client device for the sensor
  67. * @match: the OF match table for the device, containing compatible strings
  68. * but also a .data field with the corresponding internal kernel name
  69. * used by this sensor.
  70. *
  71. * In effect this function matches a compatible string to an internal kernel
  72. * name for a certain sensor device, so that the rest of the autodetection can
  73. * rely on that name from this point on. I2C client devices will be renamed
  74. * to match the internal kernel convention.
  75. */
  76. void st_sensors_of_i2c_probe(struct i2c_client *client,
  77. const struct of_device_id *match)
  78. {
  79. const struct of_device_id *of_id;
  80. of_id = of_match_device(match, &client->dev);
  81. if (!of_id)
  82. return;
  83. /* The name from the OF match takes precedence if present */
  84. strncpy(client->name, of_id->data, sizeof(client->name));
  85. client->name[sizeof(client->name) - 1] = '\0';
  86. }
  87. EXPORT_SYMBOL(st_sensors_of_i2c_probe);
  88. #endif
  89. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  90. MODULE_DESCRIPTION("STMicroelectronics ST-sensors i2c driver");
  91. MODULE_LICENSE("GPL v2");