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