hts221_i2c.c 2.6 KB

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