da280.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * IIO driver for the MiraMEMS DA280 3-axis accelerometer and
  3. * IIO driver for the MiraMEMS DA226 2-axis accelerometer
  4. *
  5. * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/acpi.h>
  14. #include <linux/iio/iio.h>
  15. #include <linux/iio/sysfs.h>
  16. #include <linux/byteorder/generic.h>
  17. #define DA280_REG_CHIP_ID 0x01
  18. #define DA280_REG_ACC_X_LSB 0x02
  19. #define DA280_REG_ACC_Y_LSB 0x04
  20. #define DA280_REG_ACC_Z_LSB 0x06
  21. #define DA280_REG_MODE_BW 0x11
  22. #define DA280_CHIP_ID 0x13
  23. #define DA280_MODE_ENABLE 0x1e
  24. #define DA280_MODE_DISABLE 0x9e
  25. enum da280_chipset { da226, da280 };
  26. /*
  27. * a value of + or -4096 corresponds to + or - 1G
  28. * scale = 9.81 / 4096 = 0.002395019
  29. */
  30. static const int da280_nscale = 2395019;
  31. #define DA280_CHANNEL(reg, axis) { \
  32. .type = IIO_ACCEL, \
  33. .address = reg, \
  34. .modified = 1, \
  35. .channel2 = IIO_MOD_##axis, \
  36. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  37. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  38. }
  39. static const struct iio_chan_spec da280_channels[] = {
  40. DA280_CHANNEL(DA280_REG_ACC_X_LSB, X),
  41. DA280_CHANNEL(DA280_REG_ACC_Y_LSB, Y),
  42. DA280_CHANNEL(DA280_REG_ACC_Z_LSB, Z),
  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 enum da280_chipset da280_match_acpi_device(struct device *dev)
  81. {
  82. const struct acpi_device_id *id;
  83. id = acpi_match_device(dev->driver->acpi_match_table, dev);
  84. if (!id)
  85. return -EINVAL;
  86. return (enum da280_chipset) id->driver_data;
  87. }
  88. static int da280_probe(struct i2c_client *client,
  89. const struct i2c_device_id *id)
  90. {
  91. int ret;
  92. struct iio_dev *indio_dev;
  93. struct da280_data *data;
  94. enum da280_chipset chip;
  95. ret = i2c_smbus_read_byte_data(client, DA280_REG_CHIP_ID);
  96. if (ret != DA280_CHIP_ID)
  97. return (ret < 0) ? ret : -ENODEV;
  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. i2c_set_clientdata(client, indio_dev);
  104. indio_dev->dev.parent = &client->dev;
  105. indio_dev->info = &da280_info;
  106. indio_dev->modes = INDIO_DIRECT_MODE;
  107. indio_dev->channels = da280_channels;
  108. if (ACPI_HANDLE(&client->dev)) {
  109. chip = da280_match_acpi_device(&client->dev);
  110. } else {
  111. chip = id->driver_data;
  112. }
  113. if (chip == da226) {
  114. indio_dev->name = "da226";
  115. indio_dev->num_channels = 2;
  116. } else {
  117. indio_dev->name = "da280";
  118. indio_dev->num_channels = 3;
  119. }
  120. ret = da280_enable(client, true);
  121. if (ret < 0)
  122. return ret;
  123. ret = iio_device_register(indio_dev);
  124. if (ret < 0) {
  125. dev_err(&client->dev, "device_register failed\n");
  126. da280_enable(client, false);
  127. }
  128. return ret;
  129. }
  130. static int da280_remove(struct i2c_client *client)
  131. {
  132. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  133. iio_device_unregister(indio_dev);
  134. return da280_enable(client, false);
  135. }
  136. #ifdef CONFIG_PM_SLEEP
  137. static int da280_suspend(struct device *dev)
  138. {
  139. return da280_enable(to_i2c_client(dev), false);
  140. }
  141. static int da280_resume(struct device *dev)
  142. {
  143. return da280_enable(to_i2c_client(dev), true);
  144. }
  145. #endif
  146. static SIMPLE_DEV_PM_OPS(da280_pm_ops, da280_suspend, da280_resume);
  147. static const struct acpi_device_id da280_acpi_match[] = {
  148. {"MIRAACC", da280},
  149. {},
  150. };
  151. MODULE_DEVICE_TABLE(acpi, da280_acpi_match);
  152. static const struct i2c_device_id da280_i2c_id[] = {
  153. { "da226", da226 },
  154. { "da280", da280 },
  155. {}
  156. };
  157. MODULE_DEVICE_TABLE(i2c, da280_i2c_id);
  158. static struct i2c_driver da280_driver = {
  159. .driver = {
  160. .name = "da280",
  161. .acpi_match_table = ACPI_PTR(da280_acpi_match),
  162. .pm = &da280_pm_ops,
  163. },
  164. .probe = da280_probe,
  165. .remove = da280_remove,
  166. .id_table = da280_i2c_id,
  167. };
  168. module_i2c_driver(da280_driver);
  169. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  170. MODULE_DESCRIPTION("MiraMEMS DA280 3-Axis Accelerometer driver");
  171. MODULE_LICENSE("GPL v2");