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