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 sensors 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 * @push_data: function to call when cros_ec_sensorhub receives 243 * a sample for that sensor. 244 * 245 * Return: 0 on success, -errno on failure. 246 */ 247 int cros_ec_sensors_core_init(struct platform_device *pdev, 248 struct iio_dev *indio_dev, 249 bool physical_device, 250 cros_ec_sensors_capture_t trigger_capture, 251 cros_ec_sensorhub_push_data_cb_t push_data) 252 { 253 struct device *dev = &pdev->dev; 254 struct cros_ec_sensors_core_state *state = iio_priv(indio_dev); 255 struct cros_ec_sensorhub *sensor_hub = dev_get_drvdata(dev->parent); 256 struct cros_ec_dev *ec = sensor_hub->ec; 257 struct cros_ec_sensor_platform *sensor_platform = dev_get_platdata(dev); 258 u32 ver_mask, temp; 259 int frequencies[ARRAY_SIZE(state->frequencies) / 2] = { 0 }; 260 int ret, i; 261 262 platform_set_drvdata(pdev, indio_dev); 263 264 state->ec = ec->ec_dev; 265 state->msg = devm_kzalloc(&pdev->dev, 266 max((u16)sizeof(struct ec_params_motion_sense), 267 state->ec->max_response), GFP_KERNEL); 268 if (!state->msg) 269 return -ENOMEM; 270 271 state->resp = (struct ec_response_motion_sense *)state->msg->data; 272 273 mutex_init(&state->cmd_lock); 274 275 ret = cros_ec_get_host_cmd_version_mask(state->ec, 276 ec->cmd_offset, 277 EC_CMD_MOTION_SENSE_CMD, 278 &ver_mask); 279 if (ret < 0) 280 return ret; 281 282 /* Set up the host command structure. */ 283 state->msg->version = fls(ver_mask) - 1; 284 state->msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; 285 state->msg->outsize = sizeof(struct ec_params_motion_sense); 286 287 indio_dev->name = pdev->name; 288 289 if (physical_device) { 290 state->param.cmd = MOTIONSENSE_CMD_INFO; 291 state->param.info.sensor_num = sensor_platform->sensor_num; 292 ret = cros_ec_motion_send_host_cmd(state, 0); 293 if (ret) { 294 dev_warn(dev, "Can not access sensor info\n"); 295 return ret; 296 } 297 state->type = state->resp->info.type; 298 state->loc = state->resp->info.location; 299 300 /* Set sign vector, only used for backward compatibility. */ 301 memset(state->sign, 1, CROS_EC_SENSOR_MAX_AXIS); 302 303 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++) 304 state->calib[i].scale = MOTION_SENSE_DEFAULT_SCALE; 305 306 /* 0 is a correct value used to stop the device */ 307 if (state->msg->version < 3) { 308 get_default_min_max_freq(state->resp->info.type, 309 &frequencies[1], 310 &frequencies[2], 311 &state->fifo_max_event_count); 312 } else { 313 if (state->resp->info_3.max_frequency == 0) { 314 get_default_min_max_freq(state->resp->info.type, 315 &frequencies[1], 316 &frequencies[2], 317 &temp); 318 } else { 319 frequencies[1] = state->resp->info_3.min_frequency; 320 frequencies[2] = state->resp->info_3.max_frequency; 321 } 322 state->fifo_max_event_count = state->resp->info_3.fifo_max_event_count; 323 } 324 for (i = 0; i < ARRAY_SIZE(frequencies); i++) { 325 state->frequencies[2 * i] = frequencies[i] / 1000; 326 state->frequencies[2 * i + 1] = 327 (frequencies[i] % 1000) * 1000; 328 } 329 330 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { 331 /* 332 * Create a software buffer, feed by the EC FIFO. 333 * We can not use trigger here, as events are generated 334 * as soon as sample_frequency is set. 335 */ 336 ret = devm_iio_kfifo_buffer_setup_ext(dev, indio_dev, NULL, 337 cros_ec_sensor_fifo_attributes); 338 if (ret) 339 return ret; 340 341 ret = cros_ec_sensorhub_register_push_data( 342 sensor_hub, sensor_platform->sensor_num, 343 indio_dev, push_data); 344 if (ret) 345 return ret; 346 347 ret = devm_add_action_or_reset( 348 dev, cros_ec_sensors_core_clean, pdev); 349 if (ret) 350 return ret; 351 352 /* Timestamp coming from FIFO are in ns since boot. */ 353 ret = iio_device_set_clock(indio_dev, CLOCK_BOOTTIME); 354 if (ret) 355 return ret; 356 357 } else { 358 /* 359 * The only way to get samples in buffer is to set a 360 * software trigger (systrig, hrtimer). 361 */ 362 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 363 NULL, trigger_capture, NULL); 364 if (ret) 365 return ret; 366 } 367 } 368 369 return 0; 370 } 371 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_init); 372 373 /** 374 * cros_ec_motion_send_host_cmd() - send motion sense host command 375 * @state: pointer to state information for device 376 * @opt_length: optional length to reduce the response size, useful on the data 377 * path. Otherwise, the maximal allowed response size is used 378 * 379 * When called, the sub-command is assumed to be set in param->cmd. 380 * 381 * Return: 0 on success, -errno on failure. 382 */ 383 int cros_ec_motion_send_host_cmd(struct cros_ec_sensors_core_state *state, 384 u16 opt_length) 385 { 386 int ret; 387 388 if (opt_length) 389 state->msg->insize = min(opt_length, state->ec->max_response); 390 else 391 state->msg->insize = state->ec->max_response; 392 393 memcpy(state->msg->data, &state->param, sizeof(state->param)); 394 395 ret = cros_ec_cmd_xfer_status(state->ec, state->msg); 396 if (ret < 0) 397 return ret; 398 399 if (ret && 400 state->resp != (struct ec_response_motion_sense *)state->msg->data) 401 memcpy(state->resp, state->msg->data, ret); 402 403 return 0; 404 } 405 EXPORT_SYMBOL_GPL(cros_ec_motion_send_host_cmd); 406 407 static ssize_t cros_ec_sensors_calibrate(struct iio_dev *indio_dev, 408 uintptr_t private, const struct iio_chan_spec *chan, 409 const char *buf, size_t len) 410 { 411 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 412 int ret, i; 413 bool calibrate; 414 415 ret = kstrtobool(buf, &calibrate); 416 if (ret < 0) 417 return ret; 418 if (!calibrate) 419 return -EINVAL; 420 421 mutex_lock(&st->cmd_lock); 422 st->param.cmd = MOTIONSENSE_CMD_PERFORM_CALIB; 423 ret = cros_ec_motion_send_host_cmd(st, 0); 424 if (ret != 0) { 425 dev_warn(&indio_dev->dev, "Unable to calibrate sensor\n"); 426 } else { 427 /* Save values */ 428 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++) 429 st->calib[i].offset = st->resp->perform_calib.offset[i]; 430 } 431 mutex_unlock(&st->cmd_lock); 432 433 return ret ? ret : len; 434 } 435 436 static ssize_t cros_ec_sensors_id(struct iio_dev *indio_dev, 437 uintptr_t private, 438 const struct iio_chan_spec *chan, char *buf) 439 { 440 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 441 442 return snprintf(buf, PAGE_SIZE, "%d\n", st->param.info.sensor_num); 443 } 444 445 static ssize_t cros_ec_sensors_loc(struct iio_dev *indio_dev, 446 uintptr_t private, const struct iio_chan_spec *chan, 447 char *buf) 448 { 449 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 450 451 return snprintf(buf, PAGE_SIZE, "%s\n", cros_ec_loc[st->loc]); 452 } 453 454 const struct iio_chan_spec_ext_info cros_ec_sensors_ext_info[] = { 455 { 456 .name = "calibrate", 457 .shared = IIO_SHARED_BY_ALL, 458 .write = cros_ec_sensors_calibrate 459 }, 460 { 461 .name = "id", 462 .shared = IIO_SHARED_BY_ALL, 463 .read = cros_ec_sensors_id 464 }, 465 { 466 .name = "location", 467 .shared = IIO_SHARED_BY_ALL, 468 .read = cros_ec_sensors_loc 469 }, 470 { }, 471 }; 472 EXPORT_SYMBOL_GPL(cros_ec_sensors_ext_info); 473 474 /** 475 * cros_ec_sensors_idx_to_reg - convert index into offset in shared memory 476 * @st: pointer to state information for device 477 * @idx: sensor index (should be element of enum sensor_index) 478 * 479 * Return: address to read at 480 */ 481 static unsigned int cros_ec_sensors_idx_to_reg( 482 struct cros_ec_sensors_core_state *st, 483 unsigned int idx) 484 { 485 /* 486 * When using LPC interface, only space for 2 Accel and one Gyro. 487 * First halfword of MOTIONSENSE_TYPE_ACCEL is used by angle. 488 */ 489 if (st->type == MOTIONSENSE_TYPE_ACCEL) 490 return EC_MEMMAP_ACC_DATA + sizeof(u16) * 491 (1 + idx + st->param.info.sensor_num * 492 CROS_EC_SENSOR_MAX_AXIS); 493 494 return EC_MEMMAP_GYRO_DATA + sizeof(u16) * idx; 495 } 496 497 static int cros_ec_sensors_cmd_read_u8(struct cros_ec_device *ec, 498 unsigned int offset, u8 *dest) 499 { 500 return ec->cmd_readmem(ec, offset, 1, dest); 501 } 502 503 static int cros_ec_sensors_cmd_read_u16(struct cros_ec_device *ec, 504 unsigned int offset, u16 *dest) 505 { 506 __le16 tmp; 507 int ret = ec->cmd_readmem(ec, offset, 2, &tmp); 508 509 if (ret >= 0) 510 *dest = le16_to_cpu(tmp); 511 512 return ret; 513 } 514 515 /** 516 * cros_ec_sensors_read_until_not_busy() - read until is not busy 517 * 518 * @st: pointer to state information for device 519 * 520 * Read from EC status byte until it reads not busy. 521 * Return: 8-bit status if ok, -errno on failure. 522 */ 523 static int cros_ec_sensors_read_until_not_busy( 524 struct cros_ec_sensors_core_state *st) 525 { 526 struct cros_ec_device *ec = st->ec; 527 u8 status; 528 int ret, attempts = 0; 529 530 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, &status); 531 if (ret < 0) 532 return ret; 533 534 while (status & EC_MEMMAP_ACC_STATUS_BUSY_BIT) { 535 /* Give up after enough attempts, return error. */ 536 if (attempts++ >= 50) 537 return -EIO; 538 539 /* Small delay every so often. */ 540 if (attempts % 5 == 0) 541 msleep(25); 542 543 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, 544 &status); 545 if (ret < 0) 546 return ret; 547 } 548 549 return status; 550 } 551 552 /** 553 * cros_ec_sensors_read_data_unsafe() - read acceleration data from EC shared memory 554 * @indio_dev: pointer to IIO device 555 * @scan_mask: bitmap of the sensor indices to scan 556 * @data: location to store data 557 * 558 * This is the unsafe function for reading the EC data. It does not guarantee 559 * that the EC will not modify the data as it is being read in. 560 * 561 * Return: 0 on success, -errno on failure. 562 */ 563 static int cros_ec_sensors_read_data_unsafe(struct iio_dev *indio_dev, 564 unsigned long scan_mask, s16 *data) 565 { 566 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 567 struct cros_ec_device *ec = st->ec; 568 unsigned int i; 569 int ret; 570 571 /* Read all sensors enabled in scan_mask. Each value is 2 bytes. */ 572 for_each_set_bit(i, &scan_mask, indio_dev->masklength) { 573 ret = cros_ec_sensors_cmd_read_u16(ec, 574 cros_ec_sensors_idx_to_reg(st, i), 575 data); 576 if (ret < 0) 577 return ret; 578 579 *data *= st->sign[i]; 580 data++; 581 } 582 583 return 0; 584 } 585 586 /** 587 * cros_ec_sensors_read_lpc() - read acceleration data from EC shared memory. 588 * @indio_dev: pointer to IIO device. 589 * @scan_mask: bitmap of the sensor indices to scan. 590 * @data: location to store data. 591 * 592 * Note: this is the safe function for reading the EC data. It guarantees 593 * that the data sampled was not modified by the EC while being read. 594 * 595 * Return: 0 on success, -errno on failure. 596 */ 597 int cros_ec_sensors_read_lpc(struct iio_dev *indio_dev, 598 unsigned long scan_mask, s16 *data) 599 { 600 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 601 struct cros_ec_device *ec = st->ec; 602 u8 samp_id = 0xff, status = 0; 603 int ret, attempts = 0; 604 605 /* 606 * Continually read all data from EC until the status byte after 607 * all reads reflects that the EC is not busy and the sample id 608 * matches the sample id from before all reads. This guarantees 609 * that data read in was not modified by the EC while reading. 610 */ 611 while ((status & (EC_MEMMAP_ACC_STATUS_BUSY_BIT | 612 EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK)) != samp_id) { 613 /* If we have tried to read too many times, return error. */ 614 if (attempts++ >= 5) 615 return -EIO; 616 617 /* Read status byte until EC is not busy. */ 618 ret = cros_ec_sensors_read_until_not_busy(st); 619 if (ret < 0) 620 return ret; 621 622 /* 623 * Store the current sample id so that we can compare to the 624 * sample id after reading the data. 625 */ 626 samp_id = ret & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK; 627 628 /* Read all EC data, format it, and store it into data. */ 629 ret = cros_ec_sensors_read_data_unsafe(indio_dev, scan_mask, 630 data); 631 if (ret < 0) 632 return ret; 633 634 /* Read status byte. */ 635 ret = cros_ec_sensors_cmd_read_u8(ec, EC_MEMMAP_ACC_STATUS, 636 &status); 637 if (ret < 0) 638 return ret; 639 } 640 641 return 0; 642 } 643 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_lpc); 644 645 /** 646 * cros_ec_sensors_read_cmd() - retrieve data using the EC command protocol 647 * @indio_dev: pointer to IIO device 648 * @scan_mask: bitmap of the sensor indices to scan 649 * @data: location to store data 650 * 651 * Return: 0 on success, -errno on failure. 652 */ 653 int cros_ec_sensors_read_cmd(struct iio_dev *indio_dev, 654 unsigned long scan_mask, s16 *data) 655 { 656 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 657 int ret; 658 unsigned int i; 659 660 /* Read all sensor data through a command. */ 661 st->param.cmd = MOTIONSENSE_CMD_DATA; 662 ret = cros_ec_motion_send_host_cmd(st, sizeof(st->resp->data)); 663 if (ret != 0) { 664 dev_warn(&indio_dev->dev, "Unable to read sensor data\n"); 665 return ret; 666 } 667 668 for_each_set_bit(i, &scan_mask, indio_dev->masklength) { 669 *data = st->resp->data.data[i]; 670 data++; 671 } 672 673 return 0; 674 } 675 EXPORT_SYMBOL_GPL(cros_ec_sensors_read_cmd); 676 677 /** 678 * cros_ec_sensors_capture() - the trigger handler function 679 * @irq: the interrupt number. 680 * @p: a pointer to the poll function. 681 * 682 * On a trigger event occurring, if the pollfunc is attached then this 683 * handler is called as a threaded interrupt (and hence may sleep). It 684 * is responsible for grabbing data from the device and pushing it into 685 * the associated buffer. 686 * 687 * Return: IRQ_HANDLED 688 */ 689 irqreturn_t cros_ec_sensors_capture(int irq, void *p) 690 { 691 struct iio_poll_func *pf = p; 692 struct iio_dev *indio_dev = pf->indio_dev; 693 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 694 int ret; 695 696 mutex_lock(&st->cmd_lock); 697 698 /* Clear capture data. */ 699 memset(st->samples, 0, indio_dev->scan_bytes); 700 701 /* Read data based on which channels are enabled in scan mask. */ 702 ret = st->read_ec_sensors_data(indio_dev, 703 *(indio_dev->active_scan_mask), 704 (s16 *)st->samples); 705 if (ret < 0) 706 goto done; 707 708 iio_push_to_buffers_with_timestamp(indio_dev, st->samples, 709 iio_get_time_ns(indio_dev)); 710 711 done: 712 /* 713 * Tell the core we are done with this trigger and ready for the 714 * next one. 715 */ 716 iio_trigger_notify_done(indio_dev->trig); 717 718 mutex_unlock(&st->cmd_lock); 719 720 return IRQ_HANDLED; 721 } 722 EXPORT_SYMBOL_GPL(cros_ec_sensors_capture); 723 724 /** 725 * cros_ec_sensors_core_read() - function to request a value from the sensor 726 * @st: pointer to state information for device 727 * @chan: channel specification structure table 728 * @val: will contain one element making up the returned value 729 * @val2: will contain another element making up the returned value 730 * @mask: specifies which values to be requested 731 * 732 * Return: the type of value returned by the device 733 */ 734 int cros_ec_sensors_core_read(struct cros_ec_sensors_core_state *st, 735 struct iio_chan_spec const *chan, 736 int *val, int *val2, long mask) 737 { 738 int ret, frequency; 739 740 switch (mask) { 741 case IIO_CHAN_INFO_SAMP_FREQ: 742 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR; 743 st->param.sensor_odr.data = 744 EC_MOTION_SENSE_NO_VALUE; 745 746 ret = cros_ec_motion_send_host_cmd(st, 0); 747 if (ret) 748 break; 749 750 frequency = st->resp->sensor_odr.ret; 751 *val = frequency / 1000; 752 *val2 = (frequency % 1000) * 1000; 753 ret = IIO_VAL_INT_PLUS_MICRO; 754 break; 755 default: 756 ret = -EINVAL; 757 break; 758 } 759 760 return ret; 761 } 762 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read); 763 764 /** 765 * cros_ec_sensors_core_read_avail() - get available values 766 * @indio_dev: pointer to state information for device 767 * @chan: channel specification structure table 768 * @vals: list of available values 769 * @type: type of data returned 770 * @length: number of data returned in the array 771 * @mask: specifies which values to be requested 772 * 773 * Return: an error code, IIO_AVAIL_RANGE or IIO_AVAIL_LIST 774 */ 775 int cros_ec_sensors_core_read_avail(struct iio_dev *indio_dev, 776 struct iio_chan_spec const *chan, 777 const int **vals, 778 int *type, 779 int *length, 780 long mask) 781 { 782 struct cros_ec_sensors_core_state *state = iio_priv(indio_dev); 783 784 switch (mask) { 785 case IIO_CHAN_INFO_SAMP_FREQ: 786 *length = ARRAY_SIZE(state->frequencies); 787 *vals = (const int *)&state->frequencies; 788 *type = IIO_VAL_INT_PLUS_MICRO; 789 return IIO_AVAIL_LIST; 790 } 791 792 return -EINVAL; 793 } 794 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_read_avail); 795 796 /** 797 * cros_ec_sensors_core_write() - function to write a value to the sensor 798 * @st: pointer to state information for device 799 * @chan: channel specification structure table 800 * @val: first part of value to write 801 * @val2: second part of value to write 802 * @mask: specifies which values to write 803 * 804 * Return: the type of value returned by the device 805 */ 806 int cros_ec_sensors_core_write(struct cros_ec_sensors_core_state *st, 807 struct iio_chan_spec const *chan, 808 int val, int val2, long mask) 809 { 810 int ret, frequency; 811 812 switch (mask) { 813 case IIO_CHAN_INFO_SAMP_FREQ: 814 frequency = val * 1000 + val2 / 1000; 815 st->param.cmd = MOTIONSENSE_CMD_SENSOR_ODR; 816 st->param.sensor_odr.data = frequency; 817 818 /* Always roundup, so caller gets at least what it asks for. */ 819 st->param.sensor_odr.roundup = 1; 820 821 ret = cros_ec_motion_send_host_cmd(st, 0); 822 break; 823 default: 824 ret = -EINVAL; 825 break; 826 } 827 return ret; 828 } 829 EXPORT_SYMBOL_GPL(cros_ec_sensors_core_write); 830 831 static int __maybe_unused cros_ec_sensors_resume(struct device *dev) 832 { 833 struct iio_dev *indio_dev = dev_get_drvdata(dev); 834 struct cros_ec_sensors_core_state *st = iio_priv(indio_dev); 835 int ret = 0; 836 837 if (st->range_updated) { 838 mutex_lock(&st->cmd_lock); 839 st->param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE; 840 st->param.sensor_range.data = st->curr_range; 841 st->param.sensor_range.roundup = 1; 842 ret = cros_ec_motion_send_host_cmd(st, 0); 843 mutex_unlock(&st->cmd_lock); 844 } 845 return ret; 846 } 847 848 SIMPLE_DEV_PM_OPS(cros_ec_sensors_pm_ops, NULL, cros_ec_sensors_resume); 849 EXPORT_SYMBOL_GPL(cros_ec_sensors_pm_ops); 850 851 MODULE_DESCRIPTION("ChromeOS EC sensor hub core functions"); 852 MODULE_LICENSE("GPL v2"); 853