1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * HID Sensors Driver 4 * Copyright (c) 2012, Intel Corporation. 5 */ 6 #include <linux/device.h> 7 #include <linux/platform_device.h> 8 #include <linux/module.h> 9 #include <linux/interrupt.h> 10 #include <linux/irq.h> 11 #include <linux/slab.h> 12 #include <linux/delay.h> 13 #include <linux/hid-sensor-hub.h> 14 #include <linux/iio/iio.h> 15 #include <linux/iio/sysfs.h> 16 #include <linux/iio/buffer.h> 17 #include "../common/hid-sensors/hid-sensor-trigger.h" 18 19 enum magn_3d_channel { 20 CHANNEL_SCAN_INDEX_X, 21 CHANNEL_SCAN_INDEX_Y, 22 CHANNEL_SCAN_INDEX_Z, 23 CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP, 24 CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP, 25 CHANNEL_SCAN_INDEX_NORTH_MAGN, 26 CHANNEL_SCAN_INDEX_NORTH_TRUE, 27 CHANNEL_SCAN_INDEX_TIMESTAMP, 28 MAGN_3D_CHANNEL_MAX, 29 }; 30 31 struct common_attributes { 32 int scale_pre_decml; 33 int scale_post_decml; 34 int scale_precision; 35 int value_offset; 36 }; 37 38 struct magn_3d_state { 39 struct hid_sensor_hub_callbacks callbacks; 40 struct hid_sensor_common magn_flux_attributes; 41 struct hid_sensor_common rot_attributes; 42 struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX]; 43 44 /* dynamically sized array to hold sensor values */ 45 u32 *iio_vals; 46 /* array of pointers to sensor value */ 47 u32 *magn_val_addr[MAGN_3D_CHANNEL_MAX]; 48 49 struct common_attributes magn_flux_attr; 50 struct common_attributes rot_attr; 51 s64 timestamp; 52 }; 53 54 static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = { 55 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS, 56 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS, 57 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS, 58 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH, 59 HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH, 60 HID_USAGE_SENSOR_ORIENT_MAGN_NORTH, 61 HID_USAGE_SENSOR_ORIENT_TRUE_NORTH, 62 HID_USAGE_SENSOR_TIME_TIMESTAMP, 63 }; 64 65 static const u32 magn_3d_sensitivity_addresses[] = { 66 HID_USAGE_SENSOR_DATA_ORIENTATION, 67 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX, 68 }; 69 70 /* Channel definitions */ 71 static const struct iio_chan_spec magn_3d_channels[] = { 72 { 73 .type = IIO_MAGN, 74 .modified = 1, 75 .channel2 = IIO_MOD_X, 76 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 77 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 78 BIT(IIO_CHAN_INFO_SCALE) | 79 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 80 BIT(IIO_CHAN_INFO_HYSTERESIS), 81 }, { 82 .type = IIO_MAGN, 83 .modified = 1, 84 .channel2 = IIO_MOD_Y, 85 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 86 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 87 BIT(IIO_CHAN_INFO_SCALE) | 88 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 89 BIT(IIO_CHAN_INFO_HYSTERESIS), 90 }, { 91 .type = IIO_MAGN, 92 .modified = 1, 93 .channel2 = IIO_MOD_Z, 94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 95 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 96 BIT(IIO_CHAN_INFO_SCALE) | 97 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 98 BIT(IIO_CHAN_INFO_HYSTERESIS), 99 }, { 100 .type = IIO_ROT, 101 .modified = 1, 102 .channel2 = IIO_MOD_NORTH_MAGN_TILT_COMP, 103 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 104 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 105 BIT(IIO_CHAN_INFO_SCALE) | 106 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 107 BIT(IIO_CHAN_INFO_HYSTERESIS), 108 }, { 109 .type = IIO_ROT, 110 .modified = 1, 111 .channel2 = IIO_MOD_NORTH_TRUE_TILT_COMP, 112 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 113 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 114 BIT(IIO_CHAN_INFO_SCALE) | 115 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 116 BIT(IIO_CHAN_INFO_HYSTERESIS), 117 }, { 118 .type = IIO_ROT, 119 .modified = 1, 120 .channel2 = IIO_MOD_NORTH_MAGN, 121 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 122 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 123 BIT(IIO_CHAN_INFO_SCALE) | 124 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 125 BIT(IIO_CHAN_INFO_HYSTERESIS), 126 }, { 127 .type = IIO_ROT, 128 .modified = 1, 129 .channel2 = IIO_MOD_NORTH_TRUE, 130 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 131 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 132 BIT(IIO_CHAN_INFO_SCALE) | 133 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 134 BIT(IIO_CHAN_INFO_HYSTERESIS), 135 }, 136 IIO_CHAN_SOFT_TIMESTAMP(7) 137 }; 138 139 /* Adjust channel real bits based on report descriptor */ 140 static void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels, 141 int channel, int size) 142 { 143 channels[channel].scan_type.sign = 's'; 144 /* Real storage bits will change based on the report desc. */ 145 channels[channel].scan_type.realbits = size * 8; 146 /* Maximum size of a sample to capture is u32 */ 147 channels[channel].scan_type.storagebits = sizeof(u32) * 8; 148 } 149 150 /* Channel read_raw handler */ 151 static int magn_3d_read_raw(struct iio_dev *indio_dev, 152 struct iio_chan_spec const *chan, 153 int *val, int *val2, 154 long mask) 155 { 156 struct magn_3d_state *magn_state = iio_priv(indio_dev); 157 int report_id = -1; 158 u32 address; 159 int ret_type; 160 s32 min; 161 162 *val = 0; 163 *val2 = 0; 164 switch (mask) { 165 case IIO_CHAN_INFO_RAW: 166 hid_sensor_power_state(&magn_state->magn_flux_attributes, true); 167 report_id = magn_state->magn[chan->address].report_id; 168 min = magn_state->magn[chan->address].logical_minimum; 169 address = magn_3d_addresses[chan->address]; 170 if (report_id >= 0) 171 *val = sensor_hub_input_attr_get_raw_value( 172 magn_state->magn_flux_attributes.hsdev, 173 HID_USAGE_SENSOR_COMPASS_3D, address, 174 report_id, 175 SENSOR_HUB_SYNC, 176 min < 0); 177 else { 178 *val = 0; 179 hid_sensor_power_state( 180 &magn_state->magn_flux_attributes, 181 false); 182 return -EINVAL; 183 } 184 hid_sensor_power_state(&magn_state->magn_flux_attributes, 185 false); 186 ret_type = IIO_VAL_INT; 187 break; 188 case IIO_CHAN_INFO_SCALE: 189 switch (chan->type) { 190 case IIO_MAGN: 191 *val = magn_state->magn_flux_attr.scale_pre_decml; 192 *val2 = magn_state->magn_flux_attr.scale_post_decml; 193 ret_type = magn_state->magn_flux_attr.scale_precision; 194 break; 195 case IIO_ROT: 196 *val = magn_state->rot_attr.scale_pre_decml; 197 *val2 = magn_state->rot_attr.scale_post_decml; 198 ret_type = magn_state->rot_attr.scale_precision; 199 break; 200 default: 201 ret_type = -EINVAL; 202 } 203 break; 204 case IIO_CHAN_INFO_OFFSET: 205 switch (chan->type) { 206 case IIO_MAGN: 207 *val = magn_state->magn_flux_attr.value_offset; 208 ret_type = IIO_VAL_INT; 209 break; 210 case IIO_ROT: 211 *val = magn_state->rot_attr.value_offset; 212 ret_type = IIO_VAL_INT; 213 break; 214 default: 215 ret_type = -EINVAL; 216 } 217 break; 218 case IIO_CHAN_INFO_SAMP_FREQ: 219 ret_type = hid_sensor_read_samp_freq_value( 220 &magn_state->magn_flux_attributes, val, val2); 221 break; 222 case IIO_CHAN_INFO_HYSTERESIS: 223 switch (chan->type) { 224 case IIO_MAGN: 225 ret_type = hid_sensor_read_raw_hyst_value( 226 &magn_state->magn_flux_attributes, val, val2); 227 break; 228 case IIO_ROT: 229 ret_type = hid_sensor_read_raw_hyst_value( 230 &magn_state->rot_attributes, val, val2); 231 break; 232 default: 233 ret_type = -EINVAL; 234 } 235 break; 236 default: 237 ret_type = -EINVAL; 238 break; 239 } 240 241 return ret_type; 242 } 243 244 /* Channel write_raw handler */ 245 static int magn_3d_write_raw(struct iio_dev *indio_dev, 246 struct iio_chan_spec const *chan, 247 int val, 248 int val2, 249 long mask) 250 { 251 struct magn_3d_state *magn_state = iio_priv(indio_dev); 252 int ret = 0; 253 254 switch (mask) { 255 case IIO_CHAN_INFO_SAMP_FREQ: 256 ret = hid_sensor_write_samp_freq_value( 257 &magn_state->magn_flux_attributes, val, val2); 258 break; 259 case IIO_CHAN_INFO_HYSTERESIS: 260 switch (chan->type) { 261 case IIO_MAGN: 262 ret = hid_sensor_write_raw_hyst_value( 263 &magn_state->magn_flux_attributes, val, val2); 264 break; 265 case IIO_ROT: 266 ret = hid_sensor_write_raw_hyst_value( 267 &magn_state->rot_attributes, val, val2); 268 break; 269 default: 270 ret = -EINVAL; 271 } 272 break; 273 default: 274 ret = -EINVAL; 275 } 276 277 return ret; 278 } 279 280 static const struct iio_info magn_3d_info = { 281 .read_raw = &magn_3d_read_raw, 282 .write_raw = &magn_3d_write_raw, 283 }; 284 285 /* Callback handler to send event after all samples are received and captured */ 286 static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev, 287 unsigned usage_id, 288 void *priv) 289 { 290 struct iio_dev *indio_dev = platform_get_drvdata(priv); 291 struct magn_3d_state *magn_state = iio_priv(indio_dev); 292 293 dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n"); 294 if (atomic_read(&magn_state->magn_flux_attributes.data_ready)) { 295 if (!magn_state->timestamp) 296 magn_state->timestamp = iio_get_time_ns(indio_dev); 297 298 iio_push_to_buffers_with_timestamp(indio_dev, 299 magn_state->iio_vals, 300 magn_state->timestamp); 301 magn_state->timestamp = 0; 302 } 303 304 return 0; 305 } 306 307 /* Capture samples in local storage */ 308 static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev, 309 unsigned usage_id, 310 size_t raw_len, char *raw_data, 311 void *priv) 312 { 313 struct iio_dev *indio_dev = platform_get_drvdata(priv); 314 struct magn_3d_state *magn_state = iio_priv(indio_dev); 315 int offset; 316 int ret = 0; 317 u32 *iio_val = NULL; 318 319 switch (usage_id) { 320 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS: 321 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS: 322 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS: 323 offset = (usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS) 324 + CHANNEL_SCAN_INDEX_X; 325 break; 326 case HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH: 327 case HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH: 328 case HID_USAGE_SENSOR_ORIENT_MAGN_NORTH: 329 case HID_USAGE_SENSOR_ORIENT_TRUE_NORTH: 330 offset = (usage_id - HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH) 331 + CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP; 332 break; 333 case HID_USAGE_SENSOR_TIME_TIMESTAMP: 334 magn_state->timestamp = 335 hid_sensor_convert_timestamp(&magn_state->magn_flux_attributes, 336 *(s64 *)raw_data); 337 return ret; 338 default: 339 return -EINVAL; 340 } 341 342 iio_val = magn_state->magn_val_addr[offset]; 343 344 if (iio_val != NULL) 345 *iio_val = *((u32 *)raw_data); 346 else 347 ret = -EINVAL; 348 349 return ret; 350 } 351 352 /* Parse report which is specific to an usage id*/ 353 static int magn_3d_parse_report(struct platform_device *pdev, 354 struct hid_sensor_hub_device *hsdev, 355 struct iio_chan_spec **channels, 356 int *chan_count, 357 unsigned usage_id, 358 struct magn_3d_state *st) 359 { 360 int i; 361 int attr_count = 0; 362 struct iio_chan_spec *_channels; 363 364 /* Scan for each usage attribute supported */ 365 for (i = 0; i < MAGN_3D_CHANNEL_MAX; i++) { 366 int status; 367 u32 address = magn_3d_addresses[i]; 368 369 /* Check if usage attribute exists in the sensor hub device */ 370 status = sensor_hub_input_get_attribute_info(hsdev, 371 HID_INPUT_REPORT, 372 usage_id, 373 address, 374 &(st->magn[i])); 375 if (!status) 376 attr_count++; 377 } 378 379 if (attr_count <= 0) { 380 dev_err(&pdev->dev, 381 "failed to find any supported usage attributes in report\n"); 382 return -EINVAL; 383 } 384 385 dev_dbg(&pdev->dev, "magn_3d Found %d usage attributes\n", 386 attr_count); 387 dev_dbg(&pdev->dev, "magn_3d X: %x:%x Y: %x:%x Z: %x:%x\n", 388 st->magn[0].index, 389 st->magn[0].report_id, 390 st->magn[1].index, st->magn[1].report_id, 391 st->magn[2].index, st->magn[2].report_id); 392 393 /* Setup IIO channel array */ 394 _channels = devm_kcalloc(&pdev->dev, attr_count, 395 sizeof(struct iio_chan_spec), 396 GFP_KERNEL); 397 if (!_channels) { 398 dev_err(&pdev->dev, 399 "failed to allocate space for iio channels\n"); 400 return -ENOMEM; 401 } 402 403 /* attr_count include timestamp channel, and the iio_vals should be aligned to 8byte */ 404 st->iio_vals = devm_kcalloc(&pdev->dev, 405 ((attr_count + 1) % 2 + (attr_count + 1) / 2) * 2, 406 sizeof(u32), GFP_KERNEL); 407 if (!st->iio_vals) { 408 dev_err(&pdev->dev, 409 "failed to allocate space for iio values array\n"); 410 return -ENOMEM; 411 } 412 413 for (i = 0, *chan_count = 0; 414 i < MAGN_3D_CHANNEL_MAX && *chan_count < attr_count; 415 i++){ 416 if (st->magn[i].index >= 0) { 417 /* Setup IIO channel struct */ 418 (_channels[*chan_count]) = magn_3d_channels[i]; 419 (_channels[*chan_count]).scan_index = *chan_count; 420 (_channels[*chan_count]).address = i; 421 422 if (i != CHANNEL_SCAN_INDEX_TIMESTAMP) { 423 /* Set magn_val_addr to iio value address */ 424 st->magn_val_addr[i] = &st->iio_vals[*chan_count]; 425 magn_3d_adjust_channel_bit_mask(_channels, 426 *chan_count, 427 st->magn[i].size); 428 } 429 (*chan_count)++; 430 } 431 } 432 433 if (*chan_count <= 0) { 434 dev_err(&pdev->dev, 435 "failed to find any magnetic channels setup\n"); 436 return -EINVAL; 437 } 438 439 *channels = _channels; 440 441 dev_dbg(&pdev->dev, "magn_3d Setup %d IIO channels\n", 442 *chan_count); 443 444 st->magn_flux_attr.scale_precision = hid_sensor_format_scale( 445 HID_USAGE_SENSOR_COMPASS_3D, 446 &st->magn[CHANNEL_SCAN_INDEX_X], 447 &st->magn_flux_attr.scale_pre_decml, 448 &st->magn_flux_attr.scale_post_decml); 449 st->rot_attr.scale_precision 450 = hid_sensor_format_scale( 451 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH, 452 &st->magn[CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP], 453 &st->rot_attr.scale_pre_decml, 454 &st->rot_attr.scale_post_decml); 455 456 if (st->rot_attributes.sensitivity.index < 0) { 457 sensor_hub_input_get_attribute_info(hsdev, 458 HID_FEATURE_REPORT, usage_id, 459 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | 460 HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH, 461 &st->rot_attributes.sensitivity); 462 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", 463 st->rot_attributes.sensitivity.index, 464 st->rot_attributes.sensitivity.report_id); 465 } 466 467 return 0; 468 } 469 470 /* Function to initialize the processing for usage id */ 471 static int hid_magn_3d_probe(struct platform_device *pdev) 472 { 473 int ret = 0; 474 static char *name = "magn_3d"; 475 struct iio_dev *indio_dev; 476 struct magn_3d_state *magn_state; 477 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 478 struct iio_chan_spec *channels; 479 int chan_count = 0; 480 481 indio_dev = devm_iio_device_alloc(&pdev->dev, 482 sizeof(struct magn_3d_state)); 483 if (indio_dev == NULL) 484 return -ENOMEM; 485 486 platform_set_drvdata(pdev, indio_dev); 487 488 magn_state = iio_priv(indio_dev); 489 magn_state->magn_flux_attributes.hsdev = hsdev; 490 magn_state->magn_flux_attributes.pdev = pdev; 491 492 ret = hid_sensor_parse_common_attributes(hsdev, 493 HID_USAGE_SENSOR_COMPASS_3D, 494 &magn_state->magn_flux_attributes, 495 magn_3d_sensitivity_addresses, 496 ARRAY_SIZE(magn_3d_sensitivity_addresses)); 497 if (ret) { 498 dev_err(&pdev->dev, "failed to setup common attributes\n"); 499 return ret; 500 } 501 magn_state->rot_attributes = magn_state->magn_flux_attributes; 502 /* sensitivity of rot_attribute is not the same as magn_flux_attributes */ 503 magn_state->rot_attributes.sensitivity.index = -1; 504 505 ret = magn_3d_parse_report(pdev, hsdev, 506 &channels, &chan_count, 507 HID_USAGE_SENSOR_COMPASS_3D, magn_state); 508 if (ret) { 509 dev_err(&pdev->dev, "failed to parse report\n"); 510 return ret; 511 } 512 513 indio_dev->channels = channels; 514 indio_dev->num_channels = chan_count; 515 indio_dev->info = &magn_3d_info; 516 indio_dev->name = name; 517 indio_dev->modes = INDIO_DIRECT_MODE; 518 519 atomic_set(&magn_state->magn_flux_attributes.data_ready, 0); 520 521 ret = hid_sensor_setup_trigger(indio_dev, name, 522 &magn_state->magn_flux_attributes); 523 if (ret < 0) { 524 dev_err(&pdev->dev, "trigger setup failed\n"); 525 return ret; 526 } 527 528 ret = iio_device_register(indio_dev); 529 if (ret) { 530 dev_err(&pdev->dev, "device register failed\n"); 531 goto error_remove_trigger; 532 } 533 534 magn_state->callbacks.send_event = magn_3d_proc_event; 535 magn_state->callbacks.capture_sample = magn_3d_capture_sample; 536 magn_state->callbacks.pdev = pdev; 537 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D, 538 &magn_state->callbacks); 539 if (ret < 0) { 540 dev_err(&pdev->dev, "callback reg failed\n"); 541 goto error_iio_unreg; 542 } 543 544 return ret; 545 546 error_iio_unreg: 547 iio_device_unregister(indio_dev); 548 error_remove_trigger: 549 hid_sensor_remove_trigger(indio_dev, &magn_state->magn_flux_attributes); 550 return ret; 551 } 552 553 /* Function to deinitialize the processing for usage id */ 554 static int hid_magn_3d_remove(struct platform_device *pdev) 555 { 556 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 557 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 558 struct magn_3d_state *magn_state = iio_priv(indio_dev); 559 560 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D); 561 iio_device_unregister(indio_dev); 562 hid_sensor_remove_trigger(indio_dev, &magn_state->magn_flux_attributes); 563 564 return 0; 565 } 566 567 static const struct platform_device_id hid_magn_3d_ids[] = { 568 { 569 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 570 .name = "HID-SENSOR-200083", 571 }, 572 { /* sentinel */ } 573 }; 574 MODULE_DEVICE_TABLE(platform, hid_magn_3d_ids); 575 576 static struct platform_driver hid_magn_3d_platform_driver = { 577 .id_table = hid_magn_3d_ids, 578 .driver = { 579 .name = KBUILD_MODNAME, 580 .pm = &hid_sensor_pm_ops, 581 }, 582 .probe = hid_magn_3d_probe, 583 .remove = hid_magn_3d_remove, 584 }; 585 module_platform_driver(hid_magn_3d_platform_driver); 586 587 MODULE_DESCRIPTION("HID Sensor Magnetometer 3D"); 588 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 589 MODULE_LICENSE("GPL"); 590