cros_ec_baro.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * cros_ec_baro - Driver for barometer sensor behind CrosEC.
  3. *
  4. * Copyright (C) 2017 Google, Inc
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/common/cros_ec_sensors_core.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/kfifo_buf.h>
  21. #include <linux/iio/trigger.h>
  22. #include <linux/iio/triggered_buffer.h>
  23. #include <linux/iio/trigger_consumer.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mfd/cros_ec.h>
  26. #include <linux/mfd/cros_ec_commands.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/platform_device.h>
  30. /*
  31. * One channel for pressure, the other for timestamp.
  32. */
  33. #define CROS_EC_BARO_MAX_CHANNELS (1 + 1)
  34. /* State data for ec_sensors iio driver. */
  35. struct cros_ec_baro_state {
  36. /* Shared by all sensors */
  37. struct cros_ec_sensors_core_state core;
  38. struct iio_chan_spec channels[CROS_EC_BARO_MAX_CHANNELS];
  39. };
  40. static int cros_ec_baro_read(struct iio_dev *indio_dev,
  41. struct iio_chan_spec const *chan,
  42. int *val, int *val2, long mask)
  43. {
  44. struct cros_ec_baro_state *st = iio_priv(indio_dev);
  45. u16 data = 0;
  46. int ret = IIO_VAL_INT;
  47. int idx = chan->scan_index;
  48. mutex_lock(&st->core.cmd_lock);
  49. switch (mask) {
  50. case IIO_CHAN_INFO_RAW:
  51. if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
  52. (s16 *)&data) < 0)
  53. ret = -EIO;
  54. *val = data;
  55. break;
  56. case IIO_CHAN_INFO_SCALE:
  57. st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
  58. st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
  59. if (cros_ec_motion_send_host_cmd(&st->core, 0)) {
  60. ret = -EIO;
  61. break;
  62. }
  63. *val = st->core.resp->sensor_range.ret;
  64. /* scale * in_pressure_raw --> kPa */
  65. *val2 = 10 << CROS_EC_SENSOR_BITS;
  66. ret = IIO_VAL_FRACTIONAL;
  67. break;
  68. default:
  69. ret = cros_ec_sensors_core_read(&st->core, chan, val, val2,
  70. mask);
  71. break;
  72. }
  73. mutex_unlock(&st->core.cmd_lock);
  74. return ret;
  75. }
  76. static int cros_ec_baro_write(struct iio_dev *indio_dev,
  77. struct iio_chan_spec const *chan,
  78. int val, int val2, long mask)
  79. {
  80. struct cros_ec_baro_state *st = iio_priv(indio_dev);
  81. int ret = 0;
  82. mutex_lock(&st->core.cmd_lock);
  83. switch (mask) {
  84. case IIO_CHAN_INFO_SCALE:
  85. st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
  86. st->core.param.sensor_range.data = val;
  87. /* Always roundup, so caller gets at least what it asks for. */
  88. st->core.param.sensor_range.roundup = 1;
  89. if (cros_ec_motion_send_host_cmd(&st->core, 0))
  90. ret = -EIO;
  91. break;
  92. default:
  93. ret = cros_ec_sensors_core_write(&st->core, chan, val, val2,
  94. mask);
  95. break;
  96. }
  97. mutex_unlock(&st->core.cmd_lock);
  98. return ret;
  99. }
  100. static const struct iio_info cros_ec_baro_info = {
  101. .read_raw = &cros_ec_baro_read,
  102. .write_raw = &cros_ec_baro_write,
  103. };
  104. static int cros_ec_baro_probe(struct platform_device *pdev)
  105. {
  106. struct device *dev = &pdev->dev;
  107. struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
  108. struct iio_dev *indio_dev;
  109. struct cros_ec_baro_state *state;
  110. struct iio_chan_spec *channel;
  111. int ret;
  112. if (!ec_dev || !ec_dev->ec_dev) {
  113. dev_warn(dev, "No CROS EC device found.\n");
  114. return -EINVAL;
  115. }
  116. indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
  117. if (!indio_dev)
  118. return -ENOMEM;
  119. ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
  120. if (ret)
  121. return ret;
  122. indio_dev->info = &cros_ec_baro_info;
  123. state = iio_priv(indio_dev);
  124. state->core.type = state->core.resp->info.type;
  125. state->core.loc = state->core.resp->info.location;
  126. channel = state->channels;
  127. /* Common part */
  128. channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  129. channel->info_mask_shared_by_all =
  130. BIT(IIO_CHAN_INFO_SCALE) |
  131. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  132. BIT(IIO_CHAN_INFO_FREQUENCY);
  133. channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
  134. channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
  135. channel->scan_type.shift = 0;
  136. channel->scan_index = 0;
  137. channel->ext_info = cros_ec_sensors_ext_info;
  138. channel->scan_type.sign = 'u';
  139. state->core.calib[0] = 0;
  140. /* Sensor specific */
  141. switch (state->core.type) {
  142. case MOTIONSENSE_TYPE_BARO:
  143. channel->type = IIO_PRESSURE;
  144. break;
  145. default:
  146. dev_warn(dev, "Unknown motion sensor\n");
  147. return -EINVAL;
  148. }
  149. /* Timestamp */
  150. channel++;
  151. channel->type = IIO_TIMESTAMP;
  152. channel->channel = -1;
  153. channel->scan_index = 1;
  154. channel->scan_type.sign = 's';
  155. channel->scan_type.realbits = 64;
  156. channel->scan_type.storagebits = 64;
  157. indio_dev->channels = state->channels;
  158. indio_dev->num_channels = CROS_EC_BARO_MAX_CHANNELS;
  159. state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
  160. ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
  161. cros_ec_sensors_capture, NULL);
  162. if (ret)
  163. return ret;
  164. return devm_iio_device_register(dev, indio_dev);
  165. }
  166. static const struct platform_device_id cros_ec_baro_ids[] = {
  167. {
  168. .name = "cros-ec-baro",
  169. },
  170. { /* sentinel */ }
  171. };
  172. MODULE_DEVICE_TABLE(platform, cros_ec_baro_ids);
  173. static struct platform_driver cros_ec_baro_platform_driver = {
  174. .driver = {
  175. .name = "cros-ec-baro",
  176. },
  177. .probe = cros_ec_baro_probe,
  178. .id_table = cros_ec_baro_ids,
  179. };
  180. module_platform_driver(cros_ec_baro_platform_driver);
  181. MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver");
  182. MODULE_LICENSE("GPL v2");