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