da280.c 4.4 KB

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