hts221_i2c.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * STMicroelectronics hts221 i2c driver
  3. *
  4. * Copyright 2016 STMicroelectronics Inc.
  5. *
  6. * Lorenzo Bianconi <lorenzo.bianconi@st.com>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/slab.h>
  14. #include "hts221.h"
  15. #define I2C_AUTO_INCREMENT 0x80
  16. static int hts221_i2c_read(struct device *dev, u8 addr, int len, u8 *data)
  17. {
  18. struct i2c_msg msg[2];
  19. struct i2c_client *client = to_i2c_client(dev);
  20. if (len > 1)
  21. addr |= I2C_AUTO_INCREMENT;
  22. msg[0].addr = client->addr;
  23. msg[0].flags = client->flags;
  24. msg[0].len = 1;
  25. msg[0].buf = &addr;
  26. msg[1].addr = client->addr;
  27. msg[1].flags = client->flags | I2C_M_RD;
  28. msg[1].len = len;
  29. msg[1].buf = data;
  30. return i2c_transfer(client->adapter, msg, 2);
  31. }
  32. static int hts221_i2c_write(struct device *dev, u8 addr, int len, u8 *data)
  33. {
  34. u8 send[len + 1];
  35. struct i2c_msg msg;
  36. struct i2c_client *client = to_i2c_client(dev);
  37. if (len > 1)
  38. addr |= I2C_AUTO_INCREMENT;
  39. send[0] = addr;
  40. memcpy(&send[1], data, len * sizeof(u8));
  41. msg.addr = client->addr;
  42. msg.flags = client->flags;
  43. msg.len = len + 1;
  44. msg.buf = send;
  45. return i2c_transfer(client->adapter, &msg, 1);
  46. }
  47. static const struct hts221_transfer_function hts221_transfer_fn = {
  48. .read = hts221_i2c_read,
  49. .write = hts221_i2c_write,
  50. };
  51. static int hts221_i2c_probe(struct i2c_client *client,
  52. const struct i2c_device_id *id)
  53. {
  54. struct hts221_hw *hw;
  55. struct iio_dev *iio_dev;
  56. iio_dev = devm_iio_device_alloc(&client->dev, sizeof(*hw));
  57. if (!iio_dev)
  58. return -ENOMEM;
  59. i2c_set_clientdata(client, iio_dev);
  60. hw = iio_priv(iio_dev);
  61. hw->name = client->name;
  62. hw->dev = &client->dev;
  63. hw->irq = client->irq;
  64. hw->tf = &hts221_transfer_fn;
  65. return hts221_probe(iio_dev);
  66. }
  67. static const struct of_device_id hts221_i2c_of_match[] = {
  68. { .compatible = "st,hts221", },
  69. {},
  70. };
  71. MODULE_DEVICE_TABLE(of, hts221_i2c_of_match);
  72. static const struct i2c_device_id hts221_i2c_id_table[] = {
  73. { HTS221_DEV_NAME },
  74. {},
  75. };
  76. MODULE_DEVICE_TABLE(i2c, hts221_i2c_id_table);
  77. static struct i2c_driver hts221_driver = {
  78. .driver = {
  79. .name = "hts221_i2c",
  80. .of_match_table = of_match_ptr(hts221_i2c_of_match),
  81. },
  82. .probe = hts221_i2c_probe,
  83. .id_table = hts221_i2c_id_table,
  84. };
  85. module_i2c_driver(hts221_driver);
  86. MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
  87. MODULE_DESCRIPTION("STMicroelectronics hts221 i2c driver");
  88. MODULE_LICENSE("GPL v2");