cros_ec_light_prox.c 7.5 KB

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