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