da280.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IIO driver for the MiraMEMS DA217 and DA280 3-axis accelerometer and
  4. * IIO driver for the MiraMEMS DA226 2-axis accelerometer
  5. *
  6. * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/i2c.h>
  10. #include <linux/acpi.h>
  11. #include <linux/iio/iio.h>
  12. #include <linux/iio/sysfs.h>
  13. #include <linux/byteorder/generic.h>
  14. #define DA280_REG_CHIP_ID 0x01
  15. #define DA280_REG_ACC_X_LSB 0x02
  16. #define DA280_REG_ACC_Y_LSB 0x04
  17. #define DA280_REG_ACC_Z_LSB 0x06
  18. #define DA280_REG_MODE_BW 0x11
  19. #define DA280_CHIP_ID 0x13
  20. #define DA280_MODE_ENABLE 0x1e
  21. #define DA280_MODE_DISABLE 0x9e
  22. /*
  23. * a value of + or -4096 corresponds to + or - 1G
  24. * scale = 9.81 / 4096 = 0.002395019
  25. */
  26. static const int da280_nscale = 2395019;
  27. #define DA280_CHANNEL(reg, axis) { \
  28. .type = IIO_ACCEL, \
  29. .address = reg, \
  30. .modified = 1, \
  31. .channel2 = IIO_MOD_##axis, \
  32. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  33. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  34. }
  35. static const struct iio_chan_spec da280_channels[] = {
  36. DA280_CHANNEL(DA280_REG_ACC_X_LSB, X),
  37. DA280_CHANNEL(DA280_REG_ACC_Y_LSB, Y),
  38. DA280_CHANNEL(DA280_REG_ACC_Z_LSB, Z),
  39. };
  40. struct da280_match_data {
  41. const char *name;
  42. int num_channels;
  43. };
  44. struct da280_data {
  45. struct i2c_client *client;
  46. };
  47. static int da280_enable(struct i2c_client *client, bool enable)
  48. {
  49. u8 data = enable ? DA280_MODE_ENABLE : DA280_MODE_DISABLE;
  50. return i2c_smbus_write_byte_data(client, DA280_REG_MODE_BW, data);
  51. }
  52. static int da280_read_raw(struct iio_dev *indio_dev,
  53. struct iio_chan_spec const *chan,
  54. int *val, int *val2, long mask)
  55. {
  56. struct da280_data *data = iio_priv(indio_dev);
  57. int ret;
  58. switch (mask) {
  59. case IIO_CHAN_INFO_RAW:
  60. ret = i2c_smbus_read_word_data(data->client, chan->address);
  61. if (ret < 0)
  62. return ret;
  63. /*
  64. * Values are 14 bits, stored as 16 bits with the 2
  65. * least significant bits always 0.
  66. */
  67. *val = (short)ret >> 2;
  68. return IIO_VAL_INT;
  69. case IIO_CHAN_INFO_SCALE:
  70. *val = 0;
  71. *val2 = da280_nscale;
  72. return IIO_VAL_INT_PLUS_NANO;
  73. default:
  74. return -EINVAL;
  75. }
  76. }
  77. static const struct iio_info da280_info = {
  78. .read_raw = da280_read_raw,
  79. };
  80. static void da280_disable(void *client)
  81. {
  82. da280_enable(client, false);
  83. }
  84. static int da280_probe(struct i2c_client *client)
  85. {
  86. const struct da280_match_data *match_data;
  87. struct iio_dev *indio_dev;
  88. struct da280_data *data;
  89. int ret;
  90. ret = i2c_smbus_read_byte_data(client, DA280_REG_CHIP_ID);
  91. if (ret != DA280_CHIP_ID)
  92. return (ret < 0) ? ret : -ENODEV;
  93. match_data = i2c_get_match_data(client);
  94. if (!match_data) {
  95. dev_err(&client->dev, "Error match-data not set\n");
  96. return -EINVAL;
  97. }
  98. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  99. if (!indio_dev)
  100. return -ENOMEM;
  101. data = iio_priv(indio_dev);
  102. data->client = client;
  103. indio_dev->info = &da280_info;
  104. indio_dev->modes = INDIO_DIRECT_MODE;
  105. indio_dev->channels = da280_channels;
  106. indio_dev->num_channels = match_data->num_channels;
  107. indio_dev->name = match_data->name;
  108. ret = da280_enable(client, true);
  109. if (ret < 0)
  110. return ret;
  111. ret = devm_add_action_or_reset(&client->dev, da280_disable, client);
  112. if (ret)
  113. return ret;
  114. return devm_iio_device_register(&client->dev, indio_dev);
  115. }
  116. static int da280_suspend(struct device *dev)
  117. {
  118. return da280_enable(to_i2c_client(dev), false);
  119. }
  120. static int da280_resume(struct device *dev)
  121. {
  122. return da280_enable(to_i2c_client(dev), true);
  123. }
  124. static DEFINE_SIMPLE_DEV_PM_OPS(da280_pm_ops, da280_suspend, da280_resume);
  125. static const struct da280_match_data da217_match_data = { "da217", 3 };
  126. static const struct da280_match_data da226_match_data = { "da226", 2 };
  127. static const struct da280_match_data da280_match_data = { "da280", 3 };
  128. static const struct acpi_device_id da280_acpi_match[] = {
  129. { "NSA2513", (kernel_ulong_t)&da217_match_data },
  130. { "MIRAACC", (kernel_ulong_t)&da280_match_data },
  131. {}
  132. };
  133. MODULE_DEVICE_TABLE(acpi, da280_acpi_match);
  134. static const struct i2c_device_id da280_i2c_id[] = {
  135. { "da217", (kernel_ulong_t)&da217_match_data },
  136. { "da226", (kernel_ulong_t)&da226_match_data },
  137. { "da280", (kernel_ulong_t)&da280_match_data },
  138. {}
  139. };
  140. MODULE_DEVICE_TABLE(i2c, da280_i2c_id);
  141. static struct i2c_driver da280_driver = {
  142. .driver = {
  143. .name = "da280",
  144. .acpi_match_table = da280_acpi_match,
  145. .pm = pm_sleep_ptr(&da280_pm_ops),
  146. },
  147. .probe = da280_probe,
  148. .id_table = da280_i2c_id,
  149. };
  150. module_i2c_driver(da280_driver);
  151. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  152. MODULE_DESCRIPTION("MiraMEMS DA280 3-Axis Accelerometer driver");
  153. MODULE_LICENSE("GPL v2");