cros_ec_light_prox.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/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/platform_device.h>
  29. #include <linux/slab.h>
  30. #include <linux/sysfs.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. };
  158. static int cros_ec_light_prox_probe(struct platform_device *pdev)
  159. {
  160. struct device *dev = &pdev->dev;
  161. struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
  162. struct iio_dev *indio_dev;
  163. struct cros_ec_light_prox_state *state;
  164. struct iio_chan_spec *channel;
  165. int ret;
  166. if (!ec_dev || !ec_dev->ec_dev) {
  167. dev_warn(dev, "No CROS EC device found.\n");
  168. return -EINVAL;
  169. }
  170. indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
  171. if (!indio_dev)
  172. return -ENOMEM;
  173. ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
  174. if (ret)
  175. return ret;
  176. indio_dev->info = &cros_ec_light_prox_info;
  177. state = iio_priv(indio_dev);
  178. state->core.type = state->core.resp->info.type;
  179. state->core.loc = state->core.resp->info.location;
  180. channel = state->channels;
  181. /* Common part */
  182. channel->info_mask_shared_by_all =
  183. BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  184. BIT(IIO_CHAN_INFO_FREQUENCY);
  185. channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
  186. channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
  187. channel->scan_type.shift = 0;
  188. channel->scan_index = 0;
  189. channel->ext_info = cros_ec_sensors_ext_info;
  190. channel->scan_type.sign = 'u';
  191. state->core.calib[0] = 0;
  192. /* Sensor specific */
  193. switch (state->core.type) {
  194. case MOTIONSENSE_TYPE_LIGHT:
  195. channel->type = IIO_LIGHT;
  196. channel->info_mask_separate =
  197. BIT(IIO_CHAN_INFO_PROCESSED) |
  198. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  199. BIT(IIO_CHAN_INFO_CALIBSCALE);
  200. break;
  201. case MOTIONSENSE_TYPE_PROX:
  202. channel->type = IIO_PROXIMITY;
  203. channel->info_mask_separate =
  204. BIT(IIO_CHAN_INFO_RAW) |
  205. BIT(IIO_CHAN_INFO_CALIBBIAS) |
  206. BIT(IIO_CHAN_INFO_CALIBSCALE);
  207. break;
  208. default:
  209. dev_warn(dev, "Unknown motion sensor\n");
  210. return -EINVAL;
  211. }
  212. /* Timestamp */
  213. channel++;
  214. channel->type = IIO_TIMESTAMP;
  215. channel->channel = -1;
  216. channel->scan_index = 1;
  217. channel->scan_type.sign = 's';
  218. channel->scan_type.realbits = 64;
  219. channel->scan_type.storagebits = 64;
  220. indio_dev->channels = state->channels;
  221. indio_dev->num_channels = CROS_EC_LIGHT_PROX_MAX_CHANNELS;
  222. state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
  223. ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
  224. cros_ec_sensors_capture, NULL);
  225. if (ret)
  226. return ret;
  227. return devm_iio_device_register(dev, indio_dev);
  228. }
  229. static const struct platform_device_id cros_ec_light_prox_ids[] = {
  230. {
  231. .name = "cros-ec-prox",
  232. },
  233. {
  234. .name = "cros-ec-light",
  235. },
  236. { /* sentinel */ }
  237. };
  238. MODULE_DEVICE_TABLE(platform, cros_ec_light_prox_ids);
  239. static struct platform_driver cros_ec_light_prox_platform_driver = {
  240. .driver = {
  241. .name = "cros-ec-light-prox",
  242. .pm = &cros_ec_sensors_pm_ops,
  243. },
  244. .probe = cros_ec_light_prox_probe,
  245. .id_table = cros_ec_light_prox_ids,
  246. };
  247. module_platform_driver(cros_ec_light_prox_platform_driver);
  248. MODULE_DESCRIPTION("ChromeOS EC light/proximity sensors driver");
  249. MODULE_LICENSE("GPL v2");