cros_ec_accel_legacy.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for older Chrome OS EC accelerometer
  4. *
  5. * Copyright 2017 Google, Inc
  6. *
  7. * This driver uses the memory mapper cros-ec interface to communicate
  8. * with the Chrome OS EC about accelerometer data or older commands.
  9. * Accelerometer access is presented through iio sysfs.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/iio/buffer.h>
  14. #include <linux/iio/common/cros_ec_sensors_core.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/kfifo_buf.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #include <linux/iio/triggered_buffer.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/platform_data/cros_ec_commands.h>
  23. #include <linux/platform_data/cros_ec_proto.h>
  24. #include <linux/platform_device.h>
  25. #define DRV_NAME "cros-ec-accel-legacy"
  26. #define CROS_EC_SENSOR_LEGACY_NUM 2
  27. /*
  28. * Sensor scale hard coded at 10 bits per g, computed as:
  29. * g / (2^10 - 1) = 0.009586168; with g = 9.80665 m.s^-2
  30. */
  31. #define ACCEL_LEGACY_NSCALE 9586168
  32. /*
  33. * Sensor frequency is hard-coded to 10Hz.
  34. */
  35. static const int cros_ec_legacy_sample_freq[] = { 10, 0 };
  36. static int cros_ec_accel_legacy_read_cmd(struct iio_dev *indio_dev,
  37. unsigned long scan_mask, s16 *data)
  38. {
  39. struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
  40. int ret;
  41. unsigned int i;
  42. u8 sensor_num;
  43. /*
  44. * Read all sensor data through a command.
  45. * Save sensor_num, it is assumed to stay.
  46. */
  47. sensor_num = st->param.info.sensor_num;
  48. st->param.cmd = MOTIONSENSE_CMD_DUMP;
  49. st->param.dump.max_sensor_count = CROS_EC_SENSOR_LEGACY_NUM;
  50. ret = cros_ec_motion_send_host_cmd(st,
  51. sizeof(st->resp->dump) + CROS_EC_SENSOR_LEGACY_NUM *
  52. sizeof(struct ec_response_motion_sensor_data));
  53. st->param.info.sensor_num = sensor_num;
  54. if (ret != 0) {
  55. dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
  56. return ret;
  57. }
  58. for_each_set_bit(i, &scan_mask, iio_get_masklength(indio_dev)) {
  59. *data = st->resp->dump.sensor[sensor_num].data[i] *
  60. st->sign[i];
  61. data++;
  62. }
  63. return 0;
  64. }
  65. static int cros_ec_accel_legacy_read(struct iio_dev *indio_dev,
  66. struct iio_chan_spec const *chan,
  67. int *val, int *val2, long mask)
  68. {
  69. struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
  70. s16 data = 0;
  71. int ret;
  72. int idx = chan->scan_index;
  73. mutex_lock(&st->cmd_lock);
  74. switch (mask) {
  75. case IIO_CHAN_INFO_RAW:
  76. ret = st->read_ec_sensors_data(indio_dev, 1 << idx, &data);
  77. if (ret < 0)
  78. break;
  79. ret = IIO_VAL_INT;
  80. *val = data;
  81. break;
  82. case IIO_CHAN_INFO_SCALE:
  83. WARN_ON(st->type != MOTIONSENSE_TYPE_ACCEL);
  84. *val = 0;
  85. *val2 = ACCEL_LEGACY_NSCALE;
  86. ret = IIO_VAL_INT_PLUS_NANO;
  87. break;
  88. case IIO_CHAN_INFO_CALIBBIAS:
  89. /* Calibration not supported. */
  90. *val = 0;
  91. ret = IIO_VAL_INT;
  92. break;
  93. case IIO_CHAN_INFO_SAMP_FREQ:
  94. *val = cros_ec_legacy_sample_freq[0];
  95. *val2 = cros_ec_legacy_sample_freq[1];
  96. ret = IIO_VAL_INT_PLUS_MICRO;
  97. break;
  98. default:
  99. ret = cros_ec_sensors_core_read(st, chan, val, val2,
  100. mask);
  101. break;
  102. }
  103. mutex_unlock(&st->cmd_lock);
  104. return ret;
  105. }
  106. static int cros_ec_accel_legacy_write(struct iio_dev *indio_dev,
  107. struct iio_chan_spec const *chan,
  108. int val, int val2, long mask)
  109. {
  110. /*
  111. * Do nothing but don't return an error code to allow calibration
  112. * script to work.
  113. */
  114. if (mask == IIO_CHAN_INFO_CALIBBIAS)
  115. return 0;
  116. return -EINVAL;
  117. }
  118. /**
  119. * cros_ec_accel_legacy_read_avail() - get available values
  120. * @indio_dev: pointer to state information for device
  121. * @chan: channel specification structure table
  122. * @vals: list of available values
  123. * @type: type of data returned
  124. * @length: number of data returned in the array
  125. * @mask: specifies which values to be requested
  126. *
  127. * Return: an error code or IIO_AVAIL_LIST
  128. */
  129. static int cros_ec_accel_legacy_read_avail(struct iio_dev *indio_dev,
  130. struct iio_chan_spec const *chan,
  131. const int **vals,
  132. int *type,
  133. int *length,
  134. long mask)
  135. {
  136. switch (mask) {
  137. case IIO_CHAN_INFO_SAMP_FREQ:
  138. *length = ARRAY_SIZE(cros_ec_legacy_sample_freq);
  139. *vals = cros_ec_legacy_sample_freq;
  140. *type = IIO_VAL_INT_PLUS_MICRO;
  141. return IIO_AVAIL_LIST;
  142. }
  143. return -EINVAL;
  144. }
  145. static const struct iio_info cros_ec_accel_legacy_info = {
  146. .read_raw = &cros_ec_accel_legacy_read,
  147. .write_raw = &cros_ec_accel_legacy_write,
  148. .read_avail = &cros_ec_accel_legacy_read_avail,
  149. };
  150. /*
  151. * Present the channel using HTML5 standard:
  152. * need to invert X and Y and invert some lid axis.
  153. */
  154. #define CROS_EC_ACCEL_ROTATE_AXIS(_axis) \
  155. ((_axis) == CROS_EC_SENSOR_Z ? CROS_EC_SENSOR_Z : \
  156. ((_axis) == CROS_EC_SENSOR_X ? CROS_EC_SENSOR_Y : \
  157. CROS_EC_SENSOR_X))
  158. #define CROS_EC_ACCEL_LEGACY_CHAN(_axis) \
  159. { \
  160. .type = IIO_ACCEL, \
  161. .channel2 = IIO_MOD_X + (_axis), \
  162. .modified = 1, \
  163. .info_mask_separate = \
  164. BIT(IIO_CHAN_INFO_RAW) | \
  165. BIT(IIO_CHAN_INFO_CALIBBIAS), \
  166. .info_mask_shared_by_all = \
  167. BIT(IIO_CHAN_INFO_SCALE) | \
  168. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  169. .info_mask_shared_by_all_available = \
  170. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  171. .ext_info = cros_ec_sensors_ext_info, \
  172. .scan_type = { \
  173. .sign = 's', \
  174. .realbits = CROS_EC_SENSOR_BITS, \
  175. .storagebits = CROS_EC_SENSOR_BITS, \
  176. }, \
  177. .scan_index = CROS_EC_ACCEL_ROTATE_AXIS(_axis), \
  178. } \
  179. static const struct iio_chan_spec cros_ec_accel_legacy_channels[] = {
  180. CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_X),
  181. CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Y),
  182. CROS_EC_ACCEL_LEGACY_CHAN(CROS_EC_SENSOR_Z),
  183. IIO_CHAN_SOFT_TIMESTAMP(CROS_EC_SENSOR_MAX_AXIS)
  184. };
  185. static int cros_ec_accel_legacy_probe(struct platform_device *pdev)
  186. {
  187. struct device *dev = &pdev->dev;
  188. struct iio_dev *indio_dev;
  189. struct cros_ec_sensors_core_state *state;
  190. int ret;
  191. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
  192. if (!indio_dev)
  193. return -ENOMEM;
  194. ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
  195. cros_ec_sensors_capture);
  196. if (ret)
  197. return ret;
  198. indio_dev->info = &cros_ec_accel_legacy_info;
  199. state = iio_priv(indio_dev);
  200. if (state->ec->cmd_readmem != NULL)
  201. state->read_ec_sensors_data = cros_ec_sensors_read_lpc;
  202. else
  203. state->read_ec_sensors_data = cros_ec_accel_legacy_read_cmd;
  204. indio_dev->channels = cros_ec_accel_legacy_channels;
  205. indio_dev->num_channels = ARRAY_SIZE(cros_ec_accel_legacy_channels);
  206. /* The lid sensor needs to be presented inverted. */
  207. if (!strcmp(indio_dev->label, "accel-display")) {
  208. state->sign[CROS_EC_SENSOR_X] = -1;
  209. state->sign[CROS_EC_SENSOR_Z] = -1;
  210. }
  211. return cros_ec_sensors_core_register(dev, indio_dev, NULL);
  212. }
  213. static struct platform_driver cros_ec_accel_platform_driver = {
  214. .driver = {
  215. .name = DRV_NAME,
  216. },
  217. .probe = cros_ec_accel_legacy_probe,
  218. };
  219. module_platform_driver(cros_ec_accel_platform_driver);
  220. MODULE_DESCRIPTION("ChromeOS EC legacy accelerometer driver");
  221. MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
  222. MODULE_LICENSE("GPL v2");
  223. MODULE_ALIAS("platform:" DRV_NAME);