1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * cros_ec_sensors_core - Common function for Chrome OS EC sensor driver.
4  *
5  * Copyright (C) 2016 Google, Inc
6  */
7 
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/iio/buffer.h>
11 #include <linux/iio/common/cros_ec_sensors_core.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/kfifo_buf.h>
14 #include <linux/iio/sysfs.h>
15 #include <linux/iio/trigger.h>
16 #include <linux/iio/trigger_consumer.h>
17 #include <linux/iio/triggered_buffer.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/platform_data/cros_ec_commands.h>
22 #include <linux/platform_data/cros_ec_proto.h>
23 #include <linux/platform_data/cros_ec_sensorhub.h>
24 #include <linux/platform_device.h>
25 
26 /*
27  * Hard coded to the first device to support sensor fifo.  The EC has a 2048
28  * byte fifo and will trigger an interrupt when fifo is 2/3 full.
29  */
30 #define CROS_EC_FIFO_SIZE (2048 * 2 / 3)
31 
32 static char *cros_ec_loc[] = {
33 	[MOTIONSENSE_LOC_BASE] = "base",
34 	[MOTIONSENSE_LOC_LID] = "lid",
35 	[MOTIONSENSE_LOC_MAX] = "unknown",
36 };
37 
38 static int cros_ec_get_host_cmd_version_mask(struct cros_ec_device *ec_dev,
39 					     u16 cmd_offset, u16 cmd, u32 *mask)
40 {
41 	int ret;
42 	struct {
43 		struct cros_ec_command msg;
44 		union {
45 			struct ec_params_get_cmd_versions params;
46 			struct ec_response_get_cmd_versions resp;
47 		};
48 	} __packed buf = {
49 		.msg = {
50 			.command = EC_CMD_GET_CMD_VERSIONS + cmd_offset,
51 			.insize = sizeof(struct ec_response_get_cmd_versions),
52 			.outsize = sizeof(struct ec_params_get_cmd_versions)
53 			},
54 		.params = {.cmd = cmd}
55 	};
56 
57 	ret = cros_ec_cmd_xfer_status(ec_dev, &buf.msg);
58 	if (ret >= 0)
59 		*mask = buf.resp.version_mask;
60 	return ret;
61 }
62 
63 static void get_default_min_max_freq(enum motionsensor_type type,
64 				     u32 *min_freq,
65 				     u32 *max_freq,
66 				     u32 *max_fifo_events)
67 {
68 	/*
69 	 * We don't know fifo size, set to size previously used by older
70 	 * hardware.
71 	 */
72 	*max_fifo_events = CROS_EC_FIFO_SIZE;
73 
74 	switch (type) {
75 	case MOTIONSENSE_TYPE_ACCEL:
76 		*min_freq = 12500;
77 		*max_freq = 100000;
78 		break;
79 	case MOTIONSENSE_TYPE_GYRO:
80 		*min_freq = 25000;
81 		*max_freq = 100000;
82 		break;
83 	case MOTIONSENSE_TYPE_MAG:
84 		*min_freq = 5000;
85 		*max_freq = 25000;
86 		break;
87 	case MOTIONSENSE_TYPE_PROX:
88 	case MOTIONSENSE_TYPE_LIGHT:
89 		*min_freq = 100;
90 		*max_freq = 50000;
91 		break;
92 	case MOTIONSENSE_TYPE_BARO:
93 		*min_freq = 250;
94 		*max_freq = 20000;
95 		break;
96 	case MOTIONSENSE_TYPE_ACTIVITY:
97 	default:
98 		*min_freq = 0;
99 		*max_freq = 0;
100 		break;
101 	}
102 }
103 
104 static int cros_ec_sensor_set_ec_rate(struct cros_ec_sensors_core_state *st,
105 				      int rate)
106 {
107 	int ret;
108 
109 	if (rate > U16_MAX)
110 		rate = U16_MAX;
111 
112 	mutex_lock(&st->cmd_lock);
113 	st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
114 	st->param.ec_rate.data = rate;
115 	ret = cros_ec_motion_send_host_cmd(st, 0);
116 	mutex_unlock(&st->cmd_lock);
117 	return ret;
118 }
119 
120 static ssize_t cros_ec_sensor_set_report_latency(struct device *dev,
121 						 struct device_attribute *attr,
122 						 const char *buf, size_t len)
123 {
124 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
125 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
126 	int integer, fract, ret;
127 	int latency;
128 
129 	ret = iio_str_to_fixpoint(buf, 100000, &integer, &fract);
130 	if (ret)
131 		return ret;
132 
133 	/* EC rate is in ms. */
134 	latency = integer * 1000 + fract / 1000;
135 	ret = cros_ec_sensor_set_ec_rate(st, latency);
136 	if (ret < 0)
137 		return ret;
138 
139 	return len;
140 }
141 
142 static ssize_t cros_ec_sensor_get_report_latency(struct device *dev,
143 						 struct device_attribute *attr,
144 						 char *buf)
145 {
146 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
147 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
148 	int latency, ret;
149 
150 	mutex_lock(&st->cmd_lock);
151 	st->param.cmd = MOTIONSENSE_CMD_EC_RATE;
152 	st->param.ec_rate.data = EC_MOTION_SENSE_NO_VALUE;
153 
154 	ret = cros_ec_motion_send_host_cmd(st, 0);
155 	latency = st->resp->ec_rate.ret;
156 	mutex_unlock(&st->cmd_lock);
157 	if (ret < 0)
158 		return ret;
159 
160 	return sprintf(buf, "%d.%06u\n",
161 		       latency / 1000,
162 		       (latency % 1000) * 1000);
163 }
164 
165 static IIO_DEVICE_ATTR(hwfifo_timeout, 0644,
166 		       cros_ec_sensor_get_report_latency,
167 		       cros_ec_sensor_set_report_latency, 0);
168 
169 static ssize_t hwfifo_watermark_max_show(struct device *dev,
170 					 struct device_attribute *attr,
171 					 char *buf)
172 {
173 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
174 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
175 
176 	return sprintf(buf, "%d\n", st->fifo_max_event_count);
177 }
178 
179 static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0);
180 
181 static const struct attribute *cros_ec_sensor_fifo_attributes[] = {
182 	&iio_dev_attr_hwfifo_timeout.dev_attr.attr,
183 	&iio_dev_attr_hwfifo_watermark_max.dev_attr.attr,
184 	NULL,
185 };
186 
187 int cros_ec_sensors_push_data(struct iio_dev *indio_dev,
188 			      s16 *data,
189 			      s64 timestamp)
190 {
191 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
192 	s16 *out;
193 	s64 delta;
194 	unsigned int i;
195 
196 	/*
197 	 * Ignore samples if the buffer is not set: it is needed if the ODR is
198 	 * set but the buffer is not enabled yet.
199 	 */
200 	if (!iio_buffer_enabled(indio_dev))
201 		return 0;
202 
203 	out = (s16 *)st->samples;
204 	for_each_set_bit(i,
205 			 indio_dev->active_scan_mask,
206 			 indio_dev->masklength) {
207 		*out = data[i];
208 		out++;
209 	}
210 
211 	if (iio_device_get_clock(indio_dev) != CLOCK_BOOTTIME)
212 		delta = iio_get_time_ns(indio_dev) - cros_ec_get_time_ns();
213 	else
214 		delta = 0;
215 
216 	iio_push_to_buffers_with_timestamp(indio_dev, st->samples,
217 					   timestamp + delta);
218 
219 	return 0;
220 }
221 EXPORT_SYMBOL_GPL(cros_ec_sensors_push_data);
222 
223 static void cros_ec_sensors_core_clean(void *arg)
224 {
225 	struct platform_device *pdev = (struct platform_device *)arg;
226 	struct cros_ec_sensorhub *sensor_hub =
227 		dev_get_drvdata(pdev->dev.parent);
228 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
229 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
230 	u8 sensor_num = st->param.info.sensor_num;
231 
232 	cros_ec_sensorhub_unregister_push_data(sensor_hub, sensor_num);
233 }
234 
235 /**
236  * cros_ec_sensors_core_init() - basic initialization of the core structure
237  * @pdev:		platform device created for the sensor
238  * @indio_dev:		iio device structure of the device
239  * @physical_device:	true if the device refers to a physical device
240  * @trigger_capture:    function pointer to call buffer is triggered,
241  *    for backward compatibility.
242  *
243  * Return: 0 on success, -errno on failure.
244  */
245 int cros_ec_sensors_core_init(struct platform_device *pdev,
246 			      struct iio_dev *indio_dev,
247 			      bool physical_device,
248 			      cros_ec_sensors_capture_t trigger_capture)
249 {
250 	struct device *dev = &pdev->dev;
251 	struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
252 	struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
253 	struct cros_ec_dev *ec = sensor_hub->ec;
254 	struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
255 	u32 ver_mask, temp;
256 	int frequencies[ARRAY_SIZE(state->frequencies) / 2] = { 0 };
257 	int ret, i;
258 
259 	platform_set_drvdata(pdev, indio_dev);
260 
261 	state->ec = ec->ec_dev;
262 	state->msg = devm_kzalloc(&pdev->dev,
263 				max((u16)sizeof(struct ec_params_motion_sense),
264 				state->ec->max_response), GFP_KERNEL);
265 	if (!state->msg)
266 		return -ENOMEM;
267 
268 	state->resp = (struct ec_response_motion_sense *)state->msg->data;
269 
270 	mutex_init(&state->cmd_lock);
271 
272 	ret = cros_ec_get_host_cmd_version_mask(state->ec,
273 						ec->cmd_offset,
274 						EC_CMD_MOTION_SENSE_CMD,
275 						&ver_mask);
276 	if (ret < 0)
277 		return ret;
278 
279 	/* Set up the host command structure. */
280 	state->msg->version = fls(ver_mask) - 1;
281 	state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
282 	state->msg->outsize = sizeof(struct ec_params_motion_sense);
283 
284 	indio_dev->name = pdev->name;
285 
286 	if (physical_device) {
287 		state->param.cmd = MOTIONSENSE_CMD_INFO;
288 		state->param.info.sensor_num = sensor_platform->sensor_num;
289 		ret = cros_ec_motion_send_host_cmd(state, 0);
290 		if (ret) {
291 			dev_warn(dev, "Can not access sensor info\n");
292 			return ret;
293 		}
294 		state->type = state->resp->info.type;
295 		state->loc = state->resp->info.location;
296 
297 		/* Set sign vector, only used for backward compatibility. */
298 		memset(state->sign, 1, CROS_EC_SENSOR_MAX_AXIS);
299 
300 		for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
301 			state->calib[i].scale = MOTION_SENSE_DEFAULT_SCALE;
302 
303 		/* 0 is a correct value used to stop the device */
304 		if (state->msg->version < 3) {
305 			get_default_min_max_freq(state->resp->info.type,
306 						 &frequencies[1],
307 						 &frequencies[2],
308 						 &state->fifo_max_event_count);
309 		} else {
310 			if (state->resp->info_3.max_frequency == 0) {
311 				get_default_min_max_freq(state->resp->info.type,
312 							 &frequencies[1],
313 							 &frequencies[2],
314 							 &temp);
315 			} else {
316 				frequencies[1] = state->resp->info_3.min_frequency;
317 				frequencies[2] = state->resp->info_3.max_frequency;
318 			}
319 			state->fifo_max_event_count = state->resp->info_3.fifo_max_event_count;
320 		}
321 		for (i = 0; i < ARRAY_SIZE(frequencies); i++) {
322 			state->frequencies[2 * i] = frequencies[i] / 1000;
323 			state->frequencies[2 * i + 1] =
324 				(frequencies[i] % 1000) * 1000;
325 		}
326 
327 		if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
328 			/*
329 			 * Create a software buffer, feed by the EC FIFO.
330 			 * We can not use trigger here, as events are generated
331 			 * as soon as sample_frequency is set.
332 			 */
333 			ret = devm_iio_kfifo_buffer_setup_ext(dev, indio_dev,
334 							      INDIO_BUFFER_SOFTWARE, NULL,
335 							      cros_ec_sensor_fifo_attributes);
336 			if (ret)
337 				return ret;
338 
339 			/* Timestamp coming from FIFO are in ns since boot. */
340 			ret = iio_device_set_clock(indio_dev, CLOCK_BOOTTIME);
341 			if (ret)
342 				return ret;
343 
344 		} else {
345 			/*
346 			 * The only way to get samples in buffer is to set a
347 			 * software trigger (systrig, hrtimer).
348 			 */
349 			ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
350 					NULL, trigger_capture, NULL);
351 			if (ret)
352 				return ret;
353 		}
354 	}
355 
356 	return 0;
357 }
358 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init);
359 
360 /**
361  * cros_ec_sensors_core_register() - Register callback to FIFO and IIO when
362  * sensor is ready.
363  * It must be called at the end of the sensor probe routine.
364  * @dev:		device created for the sensor
365  * @indio_dev:		iio device structure of the device
366  * @push_data:          function to call when cros_ec_sensorhub receives
367  *    a sample for that sensor.
368  *
369  * Return: 0 on success, -errno on failure.
370  */
371 int cros_ec_sensors_core_register(struct device *dev,
372 				  struct iio_dev *indio_dev,
373 				  cros_ec_sensorhub_push_data_cb_t push_data)
374 {
375 	struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev);
376 	struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent);
377 	struct platform_device *pdev = to_platform_device(dev);
378 	struct cros_ec_dev *ec = sensor_hub->ec;
379 	int ret;
380 
381 	ret = devm_iio_device_register(dev, indio_dev);
382 	if (ret)
383 		return ret;
384 
385 	if (!push_data ||
386 	    !cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO))
387 		return 0;
388 
389 	ret = cros_ec_sensorhub_register_push_data(
390 			sensor_hub, sensor_platform->sensor_num,
391 			indio_dev, push_data);
392 	if (ret)
393 		return ret;
394 
395 	return devm_add_action_or_reset(
396 			dev, cros_ec_sensors_core_clean, pdev);
397 }
398 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_register);
399 
400 /**
401  * cros_ec_motion_send_host_cmd() - send motion sense host command
402  * @state:		pointer to state information for device
403  * @opt_length:	optional length to reduce the response size, useful on the data
404  *		path. Otherwise, the maximal allowed response size is used
405  *
406  * When called, the sub-command is assumed to be set in param->cmd.
407  *
408  * Return: 0 on success, -errno on failure.
409  */
410 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state,
411 				 u16 opt_length)
412 {
413 	int ret;
414 
415 	if (opt_length)
416 		state->msg->insize = min(opt_length, state->ec->max_response);
417 	else
418 		state->msg->insize = state->ec->max_response;
419 
420 	memcpy(state->msg->data, &state->param, sizeof(state->param));
421 
422 	ret = cros_ec_cmd_xfer_status(state->ec, state->msg);
423 	if (ret < 0)
424 		return ret;
425 
426 	if (ret &&
427 	    state->resp != (struct ec_response_motion_sense *)state->msg->data)
428 		memcpy(state->resp, state->msg->data, ret);
429 
430 	return 0;
431 }
432 EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd);
433 
434 static ssize_t cros_ec_sensors_calibrate(struct iio_dev *indio_dev,
435 		uintptr_t private, const struct iio_chan_spec *chan,
436 		const char *buf, size_t len)
437 {
438 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
439 	int ret, i;
440 	bool calibrate;
441 
442 	ret = strtobool(buf, &calibrate);
443 	if (ret < 0)
444 		return ret;
445 	if (!calibrate)
446 		return -EINVAL;
447 
448 	mutex_lock(&st->cmd_lock);
449 	st->param.cmd = MOTIONSENSE_CMD_PERFORM_CALIB;
450 	ret = cros_ec_motion_send_host_cmd(st, 0);
451 	if (ret != 0) {
452 		dev_warn(&indio_dev->dev, "Unable to calibrate sensor\n");
453 	} else {
454 		/* Save values */
455 		for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
456 			st->calib[i].offset = st->resp->perform_calib.offset[i];
457 	}
458 	mutex_unlock(&st->cmd_lock);
459 
460 	return ret ? ret : len;
461 }
462 
463 static ssize_t cros_ec_sensors_id(struct iio_dev *indio_dev,
464 				  uintptr_t private,
465 				  const struct iio_chan_spec *chan, char *buf)
466 {
467 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
468 
469 	return snprintf(buf, PAGE_SIZE, "%d\n", st->param.info.sensor_num);
470 }
471 
472 static ssize_t cros_ec_sensors_loc(struct iio_dev *indio_dev,
473 		uintptr_t private, const struct iio_chan_spec *chan,
474 		char *buf)
475 {
476 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
477 
478 	return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->loc]);
479 }
480 
481 const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = {
482 	{
483 		.name = "calibrate",
484 		.shared = IIO_SHARED_BY_ALL,
485 		.write = cros_ec_sensors_calibrate
486 	},
487 	{
488 		.name = "id",
489 		.shared = IIO_SHARED_BY_ALL,
490 		.read = cros_ec_sensors_id
491 	},
492 	{
493 		.name = "location",
494 		.shared = IIO_SHARED_BY_ALL,
495 		.read = cros_ec_sensors_loc
496 	},
497 	{ },
498 };
499 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info);
500 
501 /**
502  * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory
503  * @st:		pointer to state information for device
504  * @idx:	sensor index (should be element of enum sensor_index)
505  *
506  * Return:	address to read at
507  */
508 static unsigned int cros_ec_sensors_idx_to_reg(
509 					struct cros_ec_sensors_core_state *st,
510 					unsigned int idx)
511 {
512 	/*
513 	 * When using LPC interface, only space for 2 Accel and one Gyro.
514 	 * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle.
515 	 */
516 	if (st->type == MOTIONSENSE_TYPE_ACCEL)
517 		return EC_MEMMAP_ACC_DATA + sizeof(u16) *
518 			(1 + idx + st->param.info.sensor_num *
519 			 CROS_EC_SENSOR_MAX_AXIS);
520 
521 	return EC_MEMMAP_GYRO_DATA + sizeof(u16) * idx;
522 }
523 
524 static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec,
525 				       unsigned int offset, u8 *dest)
526 {
527 	return ec->cmd_readmem(ec, offset, 1, dest);
528 }
529 
530 static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec,
531 					 unsigned int offset, u16 *dest)
532 {
533 	__le16 tmp;
534 	int ret = ec->cmd_readmem(ec, offset, 2, &tmp);
535 
536 	if (ret >= 0)
537 		*dest = le16_to_cpu(tmp);
538 
539 	return ret;
540 }
541 
542 /**
543  * cros_ec_sensors_read_until_not_busy() - read until is not busy
544  *
545  * @st:	pointer to state information for device
546  *
547  * Read from EC status byte until it reads not busy.
548  * Return: 8-bit status if ok, -errno on failure.
549  */
550 static int cros_ec_sensors_read_until_not_busy(
551 					struct cros_ec_sensors_core_state *st)
552 {
553 	struct cros_ec_device *ec = st->ec;
554 	u8 status;
555 	int ret, attempts = 0;
556 
557 	ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status);
558 	if (ret < 0)
559 		return ret;
560 
561 	while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) {
562 		/* Give up after enough attempts, return error. */
563 		if (attempts++ >= 50)
564 			return -EIO;
565 
566 		/* Small delay every so often. */
567 		if (attempts % 5 == 0)
568 			msleep(25);
569 
570 		ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
571 						  &status);
572 		if (ret < 0)
573 			return ret;
574 	}
575 
576 	return status;
577 }
578 
579 /**
580  * cros_ec_sensors_read_data_unsafe() - read acceleration data from EC shared memory
581  * @indio_dev:	pointer to IIO device
582  * @scan_mask:	bitmap of the sensor indices to scan
583  * @data:	location to store data
584  *
585  * This is the unsafe function for reading the EC data. It does not guarantee
586  * that the EC will not modify the data as it is being read in.
587  *
588  * Return: 0 on success, -errno on failure.
589  */
590 static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev,
591 			 unsigned long scan_mask, s16 *data)
592 {
593 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
594 	struct cros_ec_device *ec = st->ec;
595 	unsigned int i;
596 	int ret;
597 
598 	/* Read all sensors enabled in scan_mask. Each value is 2 bytes. */
599 	for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
600 		ret = cros_ec_sensors_cmd_read_u16(ec,
601 					     cros_ec_sensors_idx_to_reg(st, i),
602 					     data);
603 		if (ret < 0)
604 			return ret;
605 
606 		*data *= st->sign[i];
607 		data++;
608 	}
609 
610 	return 0;
611 }
612 
613 /**
614  * cros_ec_sensors_read_lpc() - read acceleration data from EC shared memory.
615  * @indio_dev: pointer to IIO device.
616  * @scan_mask: bitmap of the sensor indices to scan.
617  * @data: location to store data.
618  *
619  * Note: this is the safe function for reading the EC data. It guarantees
620  * that the data sampled was not modified by the EC while being read.
621  *
622  * Return: 0 on success, -errno on failure.
623  */
624 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev,
625 			     unsigned long scan_mask, s16 *data)
626 {
627 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
628 	struct cros_ec_device *ec = st->ec;
629 	u8 samp_id = 0xff, status = 0;
630 	int ret, attempts = 0;
631 
632 	/*
633 	 * Continually read all data from EC until the status byte after
634 	 * all reads reflects that the EC is not busy and the sample id
635 	 * matches the sample id from before all reads. This guarantees
636 	 * that data read in was not modified by the EC while reading.
637 	 */
638 	while ((status & (EC_MEMMAP_ACC_STATUS_BUSY_BIT |
639 			  EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK)) != samp_id) {
640 		/* If we have tried to read too many times, return error. */
641 		if (attempts++ >= 5)
642 			return -EIO;
643 
644 		/* Read status byte until EC is not busy. */
645 		ret = cros_ec_sensors_read_until_not_busy(st);
646 		if (ret < 0)
647 			return ret;
648 
649 		/*
650 		 * Store the current sample id so that we can compare to the
651 		 * sample id after reading the data.
652 		 */
653 		samp_id = ret & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK;
654 
655 		/* Read all EC data, format it, and store it into data. */
656 		ret = cros_ec_sensors_read_data_unsafe(indio_dev, scan_mask,
657 						       data);
658 		if (ret < 0)
659 			return ret;
660 
661 		/* Read status byte. */
662 		ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS,
663 						  &status);
664 		if (ret < 0)
665 			return ret;
666 	}
667 
668 	return 0;
669 }
670 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc);
671 
672 /**
673  * cros_ec_sensors_read_cmd() - retrieve data using the EC command protocol
674  * @indio_dev:	pointer to IIO device
675  * @scan_mask:	bitmap of the sensor indices to scan
676  * @data:	location to store data
677  *
678  * Return: 0 on success, -errno on failure.
679  */
680 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev,
681 			     unsigned long scan_mask, s16 *data)
682 {
683 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
684 	int ret;
685 	unsigned int i;
686 
687 	/* Read all sensor data through a command. */
688 	st->param.cmd = MOTIONSENSE_CMD_DATA;
689 	ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->data));
690 	if (ret != 0) {
691 		dev_warn(&indio_dev->dev, "Unable to read sensor data\n");
692 		return ret;
693 	}
694 
695 	for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
696 		*data = st->resp->data.data[i];
697 		data++;
698 	}
699 
700 	return 0;
701 }
702 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd);
703 
704 /**
705  * cros_ec_sensors_capture() - the trigger handler function
706  * @irq:	the interrupt number.
707  * @p:		a pointer to the poll function.
708  *
709  * On a trigger event occurring, if the pollfunc is attached then this
710  * handler is called as a threaded interrupt (and hence may sleep). It
711  * is responsible for grabbing data from the device and pushing it into
712  * the associated buffer.
713  *
714  * Return: IRQ_HANDLED
715  */
716 irqreturn_t cros_ec_sensors_capture(int irq, void *p)
717 {
718 	struct iio_poll_func *pf = p;
719 	struct iio_dev *indio_dev = pf->indio_dev;
720 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
721 	int ret;
722 
723 	mutex_lock(&st->cmd_lock);
724 
725 	/* Clear capture data. */
726 	memset(st->samples, 0, indio_dev->scan_bytes);
727 
728 	/* Read data based on which channels are enabled in scan mask. */
729 	ret = st->read_ec_sensors_data(indio_dev,
730 				       *(indio_dev->active_scan_mask),
731 				       (s16 *)st->samples);
732 	if (ret < 0)
733 		goto done;
734 
735 	iio_push_to_buffers_with_timestamp(indio_dev, st->samples,
736 					   iio_get_time_ns(indio_dev));
737 
738 done:
739 	/*
740 	 * Tell the core we are done with this trigger and ready for the
741 	 * next one.
742 	 */
743 	iio_trigger_notify_done(indio_dev->trig);
744 
745 	mutex_unlock(&st->cmd_lock);
746 
747 	return IRQ_HANDLED;
748 }
749 EXPORT_SYMBOL_GPL(cros_ec_sensors_capture);
750 
751 /**
752  * cros_ec_sensors_core_read() - function to request a value from the sensor
753  * @st:		pointer to state information for device
754  * @chan:	channel specification structure table
755  * @val:	will contain one element making up the returned value
756  * @val2:	will contain another element making up the returned value
757  * @mask:	specifies which values to be requested
758  *
759  * Return:	the type of value returned by the device
760  */
761 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st,
762 			  struct iio_chan_spec const *chan,
763 			  int *val, int *val2, long mask)
764 {
765 	int ret, frequency;
766 
767 	switch (mask) {
768 	case IIO_CHAN_INFO_SAMP_FREQ:
769 		st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
770 		st->param.sensor_odr.data =
771 			EC_MOTION_SENSE_NO_VALUE;
772 
773 		ret = cros_ec_motion_send_host_cmd(st, 0);
774 		if (ret)
775 			break;
776 
777 		frequency = st->resp->sensor_odr.ret;
778 		*val = frequency / 1000;
779 		*val2 = (frequency % 1000) * 1000;
780 		ret = IIO_VAL_INT_PLUS_MICRO;
781 		break;
782 	default:
783 		ret = -EINVAL;
784 		break;
785 	}
786 
787 	return ret;
788 }
789 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read);
790 
791 /**
792  * cros_ec_sensors_core_read_avail() - get available values
793  * @indio_dev:		pointer to state information for device
794  * @chan:	channel specification structure table
795  * @vals:	list of available values
796  * @type:	type of data returned
797  * @length:	number of data returned in the array
798  * @mask:	specifies which values to be requested
799  *
800  * Return:	an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST
801  */
802 int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev,
803 				    struct iio_chan_spec const *chan,
804 				    const int **vals,
805 				    int *type,
806 				    int *length,
807 				    long mask)
808 {
809 	struct cros_ec_sensors_core_state *state = iio_priv(indio_dev);
810 
811 	switch (mask) {
812 	case IIO_CHAN_INFO_SAMP_FREQ:
813 		*length = ARRAY_SIZE(state->frequencies);
814 		*vals = (const int *)&state->frequencies;
815 		*type = IIO_VAL_INT_PLUS_MICRO;
816 		return IIO_AVAIL_LIST;
817 	}
818 
819 	return -EINVAL;
820 }
821 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read_avail);
822 
823 /**
824  * cros_ec_sensors_core_write() - function to write a value to the sensor
825  * @st:		pointer to state information for device
826  * @chan:	channel specification structure table
827  * @val:	first part of value to write
828  * @val2:	second part of value to write
829  * @mask:	specifies which values to write
830  *
831  * Return:	the type of value returned by the device
832  */
833 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st,
834 			       struct iio_chan_spec const *chan,
835 			       int val, int val2, long mask)
836 {
837 	int ret, frequency;
838 
839 	switch (mask) {
840 	case IIO_CHAN_INFO_SAMP_FREQ:
841 		frequency = val * 1000 + val2 / 1000;
842 		st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR;
843 		st->param.sensor_odr.data = frequency;
844 
845 		/* Always roundup, so caller gets at least what it asks for. */
846 		st->param.sensor_odr.roundup = 1;
847 
848 		ret = cros_ec_motion_send_host_cmd(st, 0);
849 		break;
850 	default:
851 		ret = -EINVAL;
852 		break;
853 	}
854 	return ret;
855 }
856 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write);
857 
858 static int __maybe_unused cros_ec_sensors_resume(struct device *dev)
859 {
860 	struct platform_device *pdev = to_platform_device(dev);
861 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
862 	struct cros_ec_sensors_core_state *st = iio_priv(indio_dev);
863 	int ret = 0;
864 
865 	if (st->range_updated) {
866 		mutex_lock(&st->cmd_lock);
867 		st->param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
868 		st->param.sensor_range.data = st->curr_range;
869 		st->param.sensor_range.roundup = 1;
870 		ret = cros_ec_motion_send_host_cmd(st, 0);
871 		mutex_unlock(&st->cmd_lock);
872 	}
873 	return ret;
874 }
875 
876 SIMPLE_DEV_PM_OPS(cros_ec_sensors_pm_ops, NULL, cros_ec_sensors_resume);
877 EXPORT_SYMBOL_GPL(cros_ec_sensors_pm_ops);
878 
879 MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions");
880 MODULE_LICENSE("GPL v2");
881