mxc6255.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * MXC6255 - MEMSIC orientation sensing accelerometer
  3. *
  4. * Copyright (c) 2015, Intel Corporation.
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * IIO driver for MXC6255 (7-bit I2C slave address 0x15).
  11. */
  12. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/init.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/delay.h>
  17. #include <linux/acpi.h>
  18. #include <linux/regmap.h>
  19. #include <linux/iio/sysfs.h>
  20. #define MXC6255_DRV_NAME "mxc6255"
  21. #define MXC6255_REGMAP_NAME "mxc6255_regmap"
  22. #define MXC6255_REG_XOUT 0x00
  23. #define MXC6255_REG_YOUT 0x01
  24. #define MXC6255_REG_CHIP_ID 0x08
  25. #define MXC6255_CHIP_ID 0x05
  26. /*
  27. * MXC6255 has only one measurement range: +/- 2G.
  28. * The acceleration output is an 8-bit value.
  29. *
  30. * Scale is calculated as follows:
  31. * (2 + 2) * 9.80665 / (2^8 - 1) = 0.153829
  32. *
  33. * Scale value for +/- 2G measurement range
  34. */
  35. #define MXC6255_SCALE 153829
  36. enum mxc6255_axis {
  37. AXIS_X,
  38. AXIS_Y,
  39. };
  40. struct mxc6255_data {
  41. struct i2c_client *client;
  42. struct regmap *regmap;
  43. };
  44. static int mxc6255_read_raw(struct iio_dev *indio_dev,
  45. struct iio_chan_spec const *chan,
  46. int *val, int *val2, long mask)
  47. {
  48. struct mxc6255_data *data = iio_priv(indio_dev);
  49. unsigned int reg;
  50. int ret;
  51. switch (mask) {
  52. case IIO_CHAN_INFO_RAW:
  53. ret = regmap_read(data->regmap, chan->address, &reg);
  54. if (ret < 0) {
  55. dev_err(&data->client->dev,
  56. "Error reading reg %lu\n", chan->address);
  57. return ret;
  58. }
  59. *val = sign_extend32(reg, 7);
  60. return IIO_VAL_INT;
  61. case IIO_CHAN_INFO_SCALE:
  62. *val = 0;
  63. *val2 = MXC6255_SCALE;
  64. return IIO_VAL_INT_PLUS_MICRO;
  65. default:
  66. return -EINVAL;
  67. }
  68. }
  69. static const struct iio_info mxc6255_info = {
  70. .driver_module = THIS_MODULE,
  71. .read_raw = mxc6255_read_raw,
  72. };
  73. #define MXC6255_CHANNEL(_axis, reg) { \
  74. .type = IIO_ACCEL, \
  75. .modified = 1, \
  76. .channel2 = IIO_MOD_##_axis, \
  77. .address = reg, \
  78. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  79. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  80. }
  81. static const struct iio_chan_spec mxc6255_channels[] = {
  82. MXC6255_CHANNEL(X, MXC6255_REG_XOUT),
  83. MXC6255_CHANNEL(Y, MXC6255_REG_YOUT),
  84. };
  85. static bool mxc6255_is_readable_reg(struct device *dev, unsigned int reg)
  86. {
  87. switch (reg) {
  88. case MXC6255_REG_XOUT:
  89. case MXC6255_REG_YOUT:
  90. case MXC6255_REG_CHIP_ID:
  91. return true;
  92. default:
  93. return false;
  94. }
  95. }
  96. static const struct regmap_config mxc6255_regmap_config = {
  97. .name = MXC6255_REGMAP_NAME,
  98. .reg_bits = 8,
  99. .val_bits = 8,
  100. .readable_reg = mxc6255_is_readable_reg,
  101. };
  102. static int mxc6255_probe(struct i2c_client *client,
  103. const struct i2c_device_id *id)
  104. {
  105. struct mxc6255_data *data;
  106. struct iio_dev *indio_dev;
  107. struct regmap *regmap;
  108. unsigned int chip_id;
  109. int ret;
  110. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  111. if (!indio_dev)
  112. return -ENOMEM;
  113. regmap = devm_regmap_init_i2c(client, &mxc6255_regmap_config);
  114. if (IS_ERR(regmap)) {
  115. dev_err(&client->dev, "Error initializing regmap\n");
  116. return PTR_ERR(regmap);
  117. }
  118. data = iio_priv(indio_dev);
  119. i2c_set_clientdata(client, indio_dev);
  120. data->client = client;
  121. data->regmap = regmap;
  122. indio_dev->name = MXC6255_DRV_NAME;
  123. indio_dev->dev.parent = &client->dev;
  124. indio_dev->channels = mxc6255_channels;
  125. indio_dev->num_channels = ARRAY_SIZE(mxc6255_channels);
  126. indio_dev->modes = INDIO_DIRECT_MODE;
  127. indio_dev->info = &mxc6255_info;
  128. ret = regmap_read(data->regmap, MXC6255_REG_CHIP_ID, &chip_id);
  129. if (ret < 0) {
  130. dev_err(&client->dev, "Error reading chip id %d\n", ret);
  131. return ret;
  132. }
  133. if ((chip_id & 0x1f) != MXC6255_CHIP_ID) {
  134. dev_err(&client->dev, "Invalid chip id %x\n", chip_id);
  135. return -ENODEV;
  136. }
  137. dev_dbg(&client->dev, "Chip id %x\n", chip_id);
  138. ret = devm_iio_device_register(&client->dev, indio_dev);
  139. if (ret < 0) {
  140. dev_err(&client->dev, "Could not register IIO device\n");
  141. return ret;
  142. }
  143. return 0;
  144. }
  145. static const struct acpi_device_id mxc6255_acpi_match[] = {
  146. {"MXC6225", 0},
  147. {"MXC6255", 0},
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(acpi, mxc6255_acpi_match);
  151. static const struct i2c_device_id mxc6255_id[] = {
  152. {"mxc6225", 0},
  153. {"mxc6255", 0},
  154. { }
  155. };
  156. MODULE_DEVICE_TABLE(i2c, mxc6255_id);
  157. static struct i2c_driver mxc6255_driver = {
  158. .driver = {
  159. .name = MXC6255_DRV_NAME,
  160. .acpi_match_table = ACPI_PTR(mxc6255_acpi_match),
  161. },
  162. .probe = mxc6255_probe,
  163. .id_table = mxc6255_id,
  164. };
  165. module_i2c_driver(mxc6255_driver);
  166. MODULE_AUTHOR("Teodora Baluta <teodora.baluta@intel.com>");
  167. MODULE_DESCRIPTION("MEMSIC MXC6255 orientation sensing accelerometer driver");
  168. MODULE_LICENSE("GPL v2");