dmard09.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * IIO driver for the 3-axis accelerometer Domintech DMARD09.
  3. *
  4. * Copyright (c) 2016, Jelle van der Waa <jelle@vdwaa.nl>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <asm/unaligned.h>
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/iio/iio.h>
  19. #define DMARD09_DRV_NAME "dmard09"
  20. #define DMARD09_REG_CHIPID 0x18
  21. #define DMARD09_REG_STAT 0x0A
  22. #define DMARD09_REG_X 0x0C
  23. #define DMARD09_REG_Y 0x0E
  24. #define DMARD09_REG_Z 0x10
  25. #define DMARD09_CHIPID 0x95
  26. #define DMARD09_BUF_LEN 8
  27. #define DMARD09_AXIS_X 0
  28. #define DMARD09_AXIS_Y 1
  29. #define DMARD09_AXIS_Z 2
  30. #define DMARD09_AXIS_X_OFFSET ((DMARD09_AXIS_X + 1) * 2)
  31. #define DMARD09_AXIS_Y_OFFSET ((DMARD09_AXIS_Y + 1 )* 2)
  32. #define DMARD09_AXIS_Z_OFFSET ((DMARD09_AXIS_Z + 1) * 2)
  33. struct dmard09_data {
  34. struct i2c_client *client;
  35. };
  36. #define DMARD09_CHANNEL(_axis, offset) { \
  37. .type = IIO_ACCEL, \
  38. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  39. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  40. .modified = 1, \
  41. .address = offset, \
  42. .channel2 = IIO_MOD_##_axis, \
  43. }
  44. static const struct iio_chan_spec dmard09_channels[] = {
  45. DMARD09_CHANNEL(X, DMARD09_AXIS_X_OFFSET),
  46. DMARD09_CHANNEL(Y, DMARD09_AXIS_Y_OFFSET),
  47. DMARD09_CHANNEL(Z, DMARD09_AXIS_Z_OFFSET),
  48. };
  49. static int dmard09_read_raw(struct iio_dev *indio_dev,
  50. struct iio_chan_spec const *chan,
  51. int *val, int *val2, long mask)
  52. {
  53. struct dmard09_data *data = iio_priv(indio_dev);
  54. u8 buf[DMARD09_BUF_LEN];
  55. int ret;
  56. s16 accel;
  57. switch (mask) {
  58. case IIO_CHAN_INFO_RAW:
  59. /*
  60. * Read from the DMAR09_REG_STAT register, since the chip
  61. * caches reads from the individual X, Y, Z registers.
  62. */
  63. ret = i2c_smbus_read_i2c_block_data(data->client,
  64. DMARD09_REG_STAT,
  65. DMARD09_BUF_LEN, buf);
  66. if (ret < 0) {
  67. dev_err(&data->client->dev, "Error reading reg %d\n",
  68. DMARD09_REG_STAT);
  69. return ret;
  70. }
  71. accel = get_unaligned_le16(&buf[chan->address]);
  72. /* Remove lower 3 bits and sign extend */
  73. accel <<= 4;
  74. accel >>= 7;
  75. *val = accel;
  76. return IIO_VAL_INT;
  77. default:
  78. return -EINVAL;
  79. }
  80. }
  81. static const struct iio_info dmard09_info = {
  82. .driver_module = THIS_MODULE,
  83. .read_raw = dmard09_read_raw,
  84. };
  85. static int dmard09_probe(struct i2c_client *client,
  86. const struct i2c_device_id *id)
  87. {
  88. int ret;
  89. struct iio_dev *indio_dev;
  90. struct dmard09_data *data;
  91. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  92. if (!indio_dev) {
  93. dev_err(&client->dev, "iio allocation failed\n");
  94. return -ENOMEM;
  95. }
  96. data = iio_priv(indio_dev);
  97. data->client = client;
  98. ret = i2c_smbus_read_byte_data(data->client, DMARD09_REG_CHIPID);
  99. if (ret < 0) {
  100. dev_err(&client->dev, "Error reading chip id %d\n", ret);
  101. return ret;
  102. }
  103. if (ret != DMARD09_CHIPID) {
  104. dev_err(&client->dev, "Invalid chip id %d\n", ret);
  105. return -ENODEV;
  106. }
  107. i2c_set_clientdata(client, indio_dev);
  108. indio_dev->dev.parent = &client->dev;
  109. indio_dev->name = DMARD09_DRV_NAME;
  110. indio_dev->modes = INDIO_DIRECT_MODE;
  111. indio_dev->channels = dmard09_channels;
  112. indio_dev->num_channels = ARRAY_SIZE(dmard09_channels);
  113. indio_dev->info = &dmard09_info;
  114. return devm_iio_device_register(&client->dev, indio_dev);
  115. }
  116. static const struct i2c_device_id dmard09_id[] = {
  117. { "dmard09", 0},
  118. { },
  119. };
  120. MODULE_DEVICE_TABLE(i2c, dmard09_id);
  121. static struct i2c_driver dmard09_driver = {
  122. .driver = {
  123. .name = DMARD09_DRV_NAME
  124. },
  125. .probe = dmard09_probe,
  126. .id_table = dmard09_id,
  127. };
  128. module_i2c_driver(dmard09_driver);
  129. MODULE_AUTHOR("Jelle van der Waa <jelle@vdwaa.nl>");
  130. MODULE_DESCRIPTION("DMARD09 3-axis accelerometer driver");
  131. MODULE_LICENSE("GPL");