cros_ec_baro.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/iio.h>
  19. #include <linux/iio/kfifo_buf.h>
  20. #include <linux/iio/trigger.h>
  21. #include <linux/iio/triggered_buffer.h>
  22. #include <linux/iio/trigger_consumer.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mfd/cros_ec.h>
  25. #include <linux/mfd/cros_ec_commands.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/platform_device.h>
  29. #include "../common/cros_ec_sensors/cros_ec_sensors_core.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. .driver_module = THIS_MODULE,
  104. };
  105. static int cros_ec_baro_probe(struct platform_device *pdev)
  106. {
  107. struct device *dev = &pdev->dev;
  108. struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
  109. struct cros_ec_device *ec_device;
  110. struct iio_dev *indio_dev;
  111. struct cros_ec_baro_state *state;
  112. struct iio_chan_spec *channel;
  113. int ret;
  114. if (!ec_dev || !ec_dev->ec_dev) {
  115. dev_warn(dev, "No CROS EC device found.\n");
  116. return -EINVAL;
  117. }
  118. ec_device = ec_dev->ec_dev;
  119. indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
  120. if (!indio_dev)
  121. return -ENOMEM;
  122. ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
  123. if (ret)
  124. return ret;
  125. indio_dev->info = &cros_ec_baro_info;
  126. state = iio_priv(indio_dev);
  127. state->core.type = state->core.resp->info.type;
  128. state->core.loc = state->core.resp->info.location;
  129. channel = state->channels;
  130. /* Common part */
  131. channel->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
  132. channel->info_mask_shared_by_all =
  133. BIT(IIO_CHAN_INFO_SCALE) |
  134. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  135. BIT(IIO_CHAN_INFO_FREQUENCY);
  136. channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
  137. channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
  138. channel->scan_type.shift = 0;
  139. channel->scan_index = 0;
  140. channel->ext_info = cros_ec_sensors_ext_info;
  141. channel->scan_type.sign = 'u';
  142. state->core.calib[0] = 0;
  143. /* Sensor specific */
  144. switch (state->core.type) {
  145. case MOTIONSENSE_TYPE_BARO:
  146. channel->type = IIO_PRESSURE;
  147. break;
  148. default:
  149. dev_warn(dev, "Unknown motion sensor\n");
  150. return -EINVAL;
  151. }
  152. /* Timestamp */
  153. channel++;
  154. channel->type = IIO_TIMESTAMP;
  155. channel->channel = -1;
  156. channel->scan_index = 1;
  157. channel->scan_type.sign = 's';
  158. channel->scan_type.realbits = 64;
  159. channel->scan_type.storagebits = 64;
  160. indio_dev->channels = state->channels;
  161. indio_dev->num_channels = CROS_EC_BARO_MAX_CHANNELS;
  162. state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
  163. ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
  164. cros_ec_sensors_capture, NULL);
  165. if (ret)
  166. return ret;
  167. return devm_iio_device_register(dev, indio_dev);
  168. }
  169. static const struct platform_device_id cros_ec_baro_ids[] = {
  170. {
  171. .name = "cros-ec-baro",
  172. },
  173. { /* sentinel */ }
  174. };
  175. MODULE_DEVICE_TABLE(platform, cros_ec_baro_ids);
  176. static struct platform_driver cros_ec_baro_platform_driver = {
  177. .driver = {
  178. .name = "cros-ec-baro",
  179. },
  180. .probe = cros_ec_baro_probe,
  181. .id_table = cros_ec_baro_ids,
  182. };
  183. module_platform_driver(cros_ec_baro_platform_driver);
  184. MODULE_DESCRIPTION("ChromeOS EC barometer sensor driver");
  185. MODULE_LICENSE("GPL v2");