cros_ec_light_prox.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cros_ec_light_prox - Driver for light and prox sensors behing CrosEC.
  4. *
  5. * Copyright (C) 2017 Google, Inc
  6. */
  7. #include <linux/device.h>
  8. #include <linux/iio/buffer.h>
  9. #include <linux/iio/common/cros_ec_sensors_core.h>
  10. #include <linux/iio/iio.h>
  11. #include <linux/iio/kfifo_buf.h>
  12. #include <linux/iio/trigger.h>
  13. #include <linux/iio/triggered_buffer.h>
  14. #include <linux/iio/trigger_consumer.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_data/cros_ec_commands.h>
  19. #include <linux/platform_data/cros_ec_proto.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. /*
  23. * We only represent one entry for light or proximity. EC is merging different
  24. * light sensors to return the what the eye would see. For proximity, we
  25. * currently support only one light source.
  26. */
  27. #define CROS_EC_LIGHT_PROX_MAX_CHANNELS (1 + 1)
  28. /* State data for ec_sensors iio driver. */
  29. struct cros_ec_light_prox_state {
  30. /* Shared by all sensors */
  31. struct cros_ec_sensors_core_state core;
  32. struct iio_chan_spec channels[CROS_EC_LIGHT_PROX_MAX_CHANNELS];
  33. };
  34. static int cros_ec_light_prox_read(struct iio_dev *indio_dev,
  35. struct iio_chan_spec const *chan,
  36. int *val, int *val2, long mask)
  37. {
  38. struct cros_ec_light_prox_state *st = iio_priv(indio_dev);
  39. u16 data = 0;
  40. s64 val64;
  41. int ret;
  42. int idx = chan->scan_index;
  43. mutex_lock(&st->core.cmd_lock);
  44. switch (mask) {
  45. case IIO_CHAN_INFO_RAW:
  46. if (chan->type == IIO_PROXIMITY) {
  47. ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
  48. (s16 *)&data);
  49. if (ret)
  50. break;
  51. *val = data;
  52. ret = IIO_VAL_INT;
  53. } else {
  54. ret = -EINVAL;
  55. }
  56. break;
  57. case IIO_CHAN_INFO_PROCESSED:
  58. if (chan->type == IIO_LIGHT) {
  59. ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
  60. (s16 *)&data);
  61. if (ret)
  62. break;
  63. /*
  64. * The data coming from the light sensor is
  65. * pre-processed and represents the ambient light
  66. * illuminance reading expressed in lux.
  67. */
  68. *val = data;
  69. ret = IIO_VAL_INT;
  70. } else {
  71. ret = -EINVAL;
  72. }
  73. break;
  74. case IIO_CHAN_INFO_CALIBBIAS:
  75. st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
  76. st->core.param.sensor_offset.flags = 0;
  77. ret = cros_ec_motion_send_host_cmd(&st->core, 0);
  78. if (ret)
  79. break;
  80. /* Save values */
  81. st->core.calib[0].offset =
  82. st->core.resp->sensor_offset.offset[0];
  83. *val = st->core.calib[idx].offset;
  84. ret = IIO_VAL_INT;
  85. break;
  86. case IIO_CHAN_INFO_CALIBSCALE:
  87. /*
  88. * RANGE is used for calibration
  89. * scale is a number x.y, where x is coded on 16 bits,
  90. * y coded on 16 bits, between 0 and 9999.
  91. */
  92. st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
  93. st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
  94. ret = cros_ec_motion_send_host_cmd(&st->core, 0);
  95. if (ret)
  96. break;
  97. val64 = st->core.resp->sensor_range.ret;
  98. *val = val64 >> 16;
  99. *val2 = (val64 & 0xffff) * 100;
  100. ret = IIO_VAL_INT_PLUS_MICRO;
  101. break;
  102. default:
  103. ret = cros_ec_sensors_core_read(&st->core, chan, val, val2,
  104. mask);
  105. break;
  106. }
  107. mutex_unlock(&st->core.cmd_lock);
  108. return ret;
  109. }
  110. static int cros_ec_light_prox_write(struct iio_dev *indio_dev,
  111. struct iio_chan_spec const *chan,
  112. int val, int val2, long mask)
  113. {
  114. struct cros_ec_light_prox_state *st = iio_priv(indio_dev);
  115. int ret;
  116. int idx = chan->scan_index;
  117. mutex_lock(&st->core.cmd_lock);
  118. switch (mask) {
  119. case IIO_CHAN_INFO_CALIBBIAS:
  120. st->core.calib[idx].offset = val;
  121. /* Send to EC for each axis, even if not complete */
  122. st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
  123. st->core.param.sensor_offset.flags = MOTION_SENSE_SET_OFFSET;
  124. st->core.param.sensor_offset.offset[0] =
  125. st->core.calib[0].offset;
  126. st->core.param.sensor_offset.temp =
  127. EC_MOTION_SENSE_INVALID_CALIB_TEMP;
  128. ret = cros_ec_motion_send_host_cmd(&st->core, 0);
  129. break;
  130. case IIO_CHAN_INFO_CALIBSCALE:
  131. st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
  132. st->core.curr_range = (val << 16) | (val2 / 100);
  133. st->core.param.sensor_range.data = st->core.curr_range;
  134. ret = cros_ec_motion_send_host_cmd(&st->core, 0);
  135. if (ret == 0)
  136. st->core.range_updated = true;
  137. break;
  138. default:
  139. ret = cros_ec_sensors_core_write(&st->core, chan, val, val2,
  140. mask);
  141. break;
  142. }
  143. mutex_unlock(&st->core.cmd_lock);
  144. return ret;
  145. }
  146. static const struct iio_info cros_ec_light_prox_info = {
  147. .read_raw = &cros_ec_light_prox_read,
  148. .write_raw = &cros_ec_light_prox_write,
  149. .read_avail = &cros_ec_sensors_core_read_avail,
  150. };
  151. static int cros_ec_light_prox_probe(struct platform_device *pdev)
  152. {
  153. struct device *dev = &pdev->dev;
  154. struct iio_dev *indio_dev;
  155. struct cros_ec_light_prox_state *state;
  156. struct iio_chan_spec *channel;
  157. int ret;
  158. indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
  159. if (!indio_dev)
  160. return -ENOMEM;
  161. ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
  162. cros_ec_sensors_capture);
  163. if (ret)
  164. return ret;
  165. indio_dev->info = &cros_ec_light_prox_info;
  166. state = iio_priv(indio_dev);
  167. channel = state->channels;
  168. /* Common part */
  169. channel->info_mask_shared_by_all =
  170. BIT(IIO_CHAN_INFO_SAMP_FREQ);
  171. channel->info_mask_shared_by_all_available =
  172. BIT(IIO_CHAN_INFO_SAMP_FREQ);
  173. channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
  174. channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
  175. channel->scan_type.shift = 0;
  176. channel->scan_index = 0;
  177. channel->ext_info = cros_ec_sensors_ext_info;
  178. channel->scan_type.sign = 'u';
  179. /* Sensor specific */
  180. switch (state->core.type) {
  181. case MOTIONSENSE_TYPE_LIGHT:
  182. channel->type = IIO_LIGHT;
  183. channel->info_mask_separate =
  184. BIT(IIO_CHAN_INFO_PROCESSED) |
  185. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  186. BIT(IIO_CHAN_INFO_CALIBSCALE);
  187. break;
  188. case MOTIONSENSE_TYPE_PROX:
  189. channel->type = IIO_PROXIMITY;
  190. channel->info_mask_separate =
  191. BIT(IIO_CHAN_INFO_RAW) |
  192. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  193. BIT(IIO_CHAN_INFO_CALIBSCALE);
  194. break;
  195. default:
  196. dev_warn(dev, "Unknown motion sensor\n");
  197. return -EINVAL;
  198. }
  199. /* Timestamp */
  200. channel++;
  201. channel->type = IIO_TIMESTAMP;
  202. channel->channel = -1;
  203. channel->scan_index = 1;
  204. channel->scan_type.sign = 's';
  205. channel->scan_type.realbits = 64;
  206. channel->scan_type.storagebits = 64;
  207. indio_dev->channels = state->channels;
  208. indio_dev->num_channels = CROS_EC_LIGHT_PROX_MAX_CHANNELS;
  209. state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
  210. return cros_ec_sensors_core_register(dev, indio_dev,
  211. cros_ec_sensors_push_data);
  212. }
  213. static const struct platform_device_id cros_ec_light_prox_ids[] = {
  214. {
  215. .name = "cros-ec-prox",
  216. },
  217. {
  218. .name = "cros-ec-light",
  219. },
  220. { /* sentinel */ }
  221. };
  222. MODULE_DEVICE_TABLE(platform, cros_ec_light_prox_ids);
  223. static struct platform_driver cros_ec_light_prox_platform_driver = {
  224. .driver = {
  225. .name = "cros-ec-light-prox",
  226. .pm = &cros_ec_sensors_pm_ops,
  227. },
  228. .probe = cros_ec_light_prox_probe,
  229. .id_table = cros_ec_light_prox_ids,
  230. };
  231. module_platform_driver(cros_ec_light_prox_platform_driver);
  232. MODULE_DESCRIPTION("ChromeOS EC light/proximity sensors driver");
  233. MODULE_LICENSE("GPL v2");