da280.c 4.2 KB

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