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