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 accel_3d_channel { 20 CHANNEL_SCAN_INDEX_X, 21 CHANNEL_SCAN_INDEX_Y, 22 CHANNEL_SCAN_INDEX_Z, 23 ACCEL_3D_CHANNEL_MAX, 24 }; 25 26 struct accel_3d_state { 27 struct hid_sensor_hub_callbacks callbacks; 28 struct hid_sensor_common common_attributes; 29 struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX]; 30 /* Reserve for 3 channels + padding + timestamp */ 31 u32 accel_val[ACCEL_3D_CHANNEL_MAX + 3]; 32 int scale_pre_decml; 33 int scale_post_decml; 34 int scale_precision; 35 int value_offset; 36 int64_t timestamp; 37 }; 38 39 static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = { 40 HID_USAGE_SENSOR_ACCEL_X_AXIS, 41 HID_USAGE_SENSOR_ACCEL_Y_AXIS, 42 HID_USAGE_SENSOR_ACCEL_Z_AXIS 43 }; 44 45 /* Channel definitions */ 46 static const struct iio_chan_spec accel_3d_channels[] = { 47 { 48 .type = IIO_ACCEL, 49 .modified = 1, 50 .channel2 = IIO_MOD_X, 51 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 52 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 53 BIT(IIO_CHAN_INFO_SCALE) | 54 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 55 BIT(IIO_CHAN_INFO_HYSTERESIS), 56 .scan_index = CHANNEL_SCAN_INDEX_X, 57 }, { 58 .type = IIO_ACCEL, 59 .modified = 1, 60 .channel2 = IIO_MOD_Y, 61 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 62 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 63 BIT(IIO_CHAN_INFO_SCALE) | 64 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 65 BIT(IIO_CHAN_INFO_HYSTERESIS), 66 .scan_index = CHANNEL_SCAN_INDEX_Y, 67 }, { 68 .type = IIO_ACCEL, 69 .modified = 1, 70 .channel2 = IIO_MOD_Z, 71 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 72 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 73 BIT(IIO_CHAN_INFO_SCALE) | 74 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 75 BIT(IIO_CHAN_INFO_HYSTERESIS), 76 .scan_index = CHANNEL_SCAN_INDEX_Z, 77 }, 78 IIO_CHAN_SOFT_TIMESTAMP(3) 79 }; 80 81 /* Channel definitions */ 82 static const struct iio_chan_spec gravity_channels[] = { 83 { 84 .type = IIO_GRAVITY, 85 .modified = 1, 86 .channel2 = IIO_MOD_X, 87 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 88 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 89 BIT(IIO_CHAN_INFO_SCALE) | 90 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 91 BIT(IIO_CHAN_INFO_HYSTERESIS), 92 .scan_index = CHANNEL_SCAN_INDEX_X, 93 }, { 94 .type = IIO_GRAVITY, 95 .modified = 1, 96 .channel2 = IIO_MOD_Y, 97 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 98 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 99 BIT(IIO_CHAN_INFO_SCALE) | 100 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 101 BIT(IIO_CHAN_INFO_HYSTERESIS), 102 .scan_index = CHANNEL_SCAN_INDEX_Y, 103 }, { 104 .type = IIO_GRAVITY, 105 .modified = 1, 106 .channel2 = IIO_MOD_Z, 107 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 108 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 109 BIT(IIO_CHAN_INFO_SCALE) | 110 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 111 BIT(IIO_CHAN_INFO_HYSTERESIS), 112 .scan_index = CHANNEL_SCAN_INDEX_Z, 113 } 114 }; 115 116 /* Adjust channel real bits based on report descriptor */ 117 static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels, 118 int channel, int size) 119 { 120 channels[channel].scan_type.sign = 's'; 121 /* Real storage bits will change based on the report desc. */ 122 channels[channel].scan_type.realbits = size * 8; 123 /* Maximum size of a sample to capture is u32 */ 124 channels[channel].scan_type.storagebits = sizeof(u32) * 8; 125 } 126 127 /* Channel read_raw handler */ 128 static int accel_3d_read_raw(struct iio_dev *indio_dev, 129 struct iio_chan_spec const *chan, 130 int *val, int *val2, 131 long mask) 132 { 133 struct accel_3d_state *accel_state = iio_priv(indio_dev); 134 int report_id = -1; 135 u32 address; 136 int ret_type; 137 s32 min; 138 struct hid_sensor_hub_device *hsdev = 139 accel_state->common_attributes.hsdev; 140 141 *val = 0; 142 *val2 = 0; 143 switch (mask) { 144 case IIO_CHAN_INFO_RAW: 145 hid_sensor_power_state(&accel_state->common_attributes, true); 146 report_id = accel_state->accel[chan->scan_index].report_id; 147 min = accel_state->accel[chan->scan_index].logical_minimum; 148 address = accel_3d_addresses[chan->scan_index]; 149 if (report_id >= 0) 150 *val = sensor_hub_input_attr_get_raw_value( 151 accel_state->common_attributes.hsdev, 152 hsdev->usage, address, report_id, 153 SENSOR_HUB_SYNC, 154 min < 0); 155 else { 156 *val = 0; 157 hid_sensor_power_state(&accel_state->common_attributes, 158 false); 159 return -EINVAL; 160 } 161 hid_sensor_power_state(&accel_state->common_attributes, false); 162 ret_type = IIO_VAL_INT; 163 break; 164 case IIO_CHAN_INFO_SCALE: 165 *val = accel_state->scale_pre_decml; 166 *val2 = accel_state->scale_post_decml; 167 ret_type = accel_state->scale_precision; 168 break; 169 case IIO_CHAN_INFO_OFFSET: 170 *val = accel_state->value_offset; 171 ret_type = IIO_VAL_INT; 172 break; 173 case IIO_CHAN_INFO_SAMP_FREQ: 174 ret_type = hid_sensor_read_samp_freq_value( 175 &accel_state->common_attributes, val, val2); 176 break; 177 case IIO_CHAN_INFO_HYSTERESIS: 178 ret_type = hid_sensor_read_raw_hyst_value( 179 &accel_state->common_attributes, val, val2); 180 break; 181 default: 182 ret_type = -EINVAL; 183 break; 184 } 185 186 return ret_type; 187 } 188 189 /* Channel write_raw handler */ 190 static int accel_3d_write_raw(struct iio_dev *indio_dev, 191 struct iio_chan_spec const *chan, 192 int val, 193 int val2, 194 long mask) 195 { 196 struct accel_3d_state *accel_state = iio_priv(indio_dev); 197 int ret = 0; 198 199 switch (mask) { 200 case IIO_CHAN_INFO_SAMP_FREQ: 201 ret = hid_sensor_write_samp_freq_value( 202 &accel_state->common_attributes, val, val2); 203 break; 204 case IIO_CHAN_INFO_HYSTERESIS: 205 ret = hid_sensor_write_raw_hyst_value( 206 &accel_state->common_attributes, val, val2); 207 break; 208 default: 209 ret = -EINVAL; 210 } 211 212 return ret; 213 } 214 215 static const struct iio_info accel_3d_info = { 216 .read_raw = &accel_3d_read_raw, 217 .write_raw = &accel_3d_write_raw, 218 }; 219 220 /* Function to push data to buffer */ 221 static void hid_sensor_push_data(struct iio_dev *indio_dev, void *data, 222 int len, int64_t timestamp) 223 { 224 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); 225 iio_push_to_buffers_with_timestamp(indio_dev, data, timestamp); 226 } 227 228 /* Callback handler to send event after all samples are received and captured */ 229 static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev, 230 unsigned usage_id, 231 void *priv) 232 { 233 struct iio_dev *indio_dev = platform_get_drvdata(priv); 234 struct accel_3d_state *accel_state = iio_priv(indio_dev); 235 236 dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n"); 237 if (atomic_read(&accel_state->common_attributes.data_ready)) { 238 if (!accel_state->timestamp) 239 accel_state->timestamp = iio_get_time_ns(indio_dev); 240 241 hid_sensor_push_data(indio_dev, 242 accel_state->accel_val, 243 sizeof(accel_state->accel_val), 244 accel_state->timestamp); 245 246 accel_state->timestamp = 0; 247 } 248 249 return 0; 250 } 251 252 /* Capture samples in local storage */ 253 static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev, 254 unsigned usage_id, 255 size_t raw_len, char *raw_data, 256 void *priv) 257 { 258 struct iio_dev *indio_dev = platform_get_drvdata(priv); 259 struct accel_3d_state *accel_state = iio_priv(indio_dev); 260 int offset; 261 int ret = -EINVAL; 262 263 switch (usage_id) { 264 case HID_USAGE_SENSOR_ACCEL_X_AXIS: 265 case HID_USAGE_SENSOR_ACCEL_Y_AXIS: 266 case HID_USAGE_SENSOR_ACCEL_Z_AXIS: 267 offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS; 268 accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] = 269 *(u32 *)raw_data; 270 ret = 0; 271 break; 272 case HID_USAGE_SENSOR_TIME_TIMESTAMP: 273 accel_state->timestamp = 274 hid_sensor_convert_timestamp( 275 &accel_state->common_attributes, 276 *(int64_t *)raw_data); 277 break; 278 default: 279 break; 280 } 281 282 return ret; 283 } 284 285 /* Parse report which is specific to an usage id*/ 286 static int accel_3d_parse_report(struct platform_device *pdev, 287 struct hid_sensor_hub_device *hsdev, 288 struct iio_chan_spec *channels, 289 unsigned usage_id, 290 struct accel_3d_state *st) 291 { 292 int ret; 293 int i; 294 295 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) { 296 ret = sensor_hub_input_get_attribute_info(hsdev, 297 HID_INPUT_REPORT, 298 usage_id, 299 HID_USAGE_SENSOR_ACCEL_X_AXIS + i, 300 &st->accel[CHANNEL_SCAN_INDEX_X + i]); 301 if (ret < 0) 302 break; 303 accel_3d_adjust_channel_bit_mask(channels, 304 CHANNEL_SCAN_INDEX_X + i, 305 st->accel[CHANNEL_SCAN_INDEX_X + i].size); 306 } 307 dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n", 308 st->accel[0].index, 309 st->accel[0].report_id, 310 st->accel[1].index, st->accel[1].report_id, 311 st->accel[2].index, st->accel[2].report_id); 312 313 st->scale_precision = hid_sensor_format_scale( 314 hsdev->usage, 315 &st->accel[CHANNEL_SCAN_INDEX_X], 316 &st->scale_pre_decml, &st->scale_post_decml); 317 318 /* Set Sensitivity field ids, when there is no individual modifier */ 319 if (st->common_attributes.sensitivity.index < 0) { 320 sensor_hub_input_get_attribute_info(hsdev, 321 HID_FEATURE_REPORT, usage_id, 322 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | 323 HID_USAGE_SENSOR_DATA_ACCELERATION, 324 &st->common_attributes.sensitivity); 325 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", 326 st->common_attributes.sensitivity.index, 327 st->common_attributes.sensitivity.report_id); 328 } 329 330 return ret; 331 } 332 333 /* Function to initialize the processing for usage id */ 334 static int hid_accel_3d_probe(struct platform_device *pdev) 335 { 336 int ret = 0; 337 const char *name; 338 struct iio_dev *indio_dev; 339 struct accel_3d_state *accel_state; 340 const struct iio_chan_spec *channel_spec; 341 int channel_size; 342 343 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 344 345 indio_dev = devm_iio_device_alloc(&pdev->dev, 346 sizeof(struct accel_3d_state)); 347 if (indio_dev == NULL) 348 return -ENOMEM; 349 350 platform_set_drvdata(pdev, indio_dev); 351 352 accel_state = iio_priv(indio_dev); 353 accel_state->common_attributes.hsdev = hsdev; 354 accel_state->common_attributes.pdev = pdev; 355 356 if (hsdev->usage == HID_USAGE_SENSOR_ACCEL_3D) { 357 name = "accel_3d"; 358 channel_spec = accel_3d_channels; 359 channel_size = sizeof(accel_3d_channels); 360 indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels); 361 } else { 362 name = "gravity"; 363 channel_spec = gravity_channels; 364 channel_size = sizeof(gravity_channels); 365 indio_dev->num_channels = ARRAY_SIZE(gravity_channels); 366 } 367 ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage, 368 &accel_state->common_attributes); 369 if (ret) { 370 dev_err(&pdev->dev, "failed to setup common attributes\n"); 371 return ret; 372 } 373 indio_dev->channels = kmemdup(channel_spec, channel_size, GFP_KERNEL); 374 375 if (!indio_dev->channels) { 376 dev_err(&pdev->dev, "failed to duplicate channels\n"); 377 return -ENOMEM; 378 } 379 ret = accel_3d_parse_report(pdev, hsdev, 380 (struct iio_chan_spec *)indio_dev->channels, 381 hsdev->usage, accel_state); 382 if (ret) { 383 dev_err(&pdev->dev, "failed to setup attributes\n"); 384 goto error_free_dev_mem; 385 } 386 387 indio_dev->dev.parent = &pdev->dev; 388 indio_dev->info = &accel_3d_info; 389 indio_dev->name = name; 390 indio_dev->modes = INDIO_DIRECT_MODE; 391 392 atomic_set(&accel_state->common_attributes.data_ready, 0); 393 394 ret = hid_sensor_setup_trigger(indio_dev, name, 395 &accel_state->common_attributes); 396 if (ret < 0) { 397 dev_err(&pdev->dev, "trigger setup failed\n"); 398 goto error_free_dev_mem; 399 } 400 401 ret = iio_device_register(indio_dev); 402 if (ret) { 403 dev_err(&pdev->dev, "device register failed\n"); 404 goto error_remove_trigger; 405 } 406 407 accel_state->callbacks.send_event = accel_3d_proc_event; 408 accel_state->callbacks.capture_sample = accel_3d_capture_sample; 409 accel_state->callbacks.pdev = pdev; 410 ret = sensor_hub_register_callback(hsdev, hsdev->usage, 411 &accel_state->callbacks); 412 if (ret < 0) { 413 dev_err(&pdev->dev, "callback reg failed\n"); 414 goto error_iio_unreg; 415 } 416 417 return ret; 418 419 error_iio_unreg: 420 iio_device_unregister(indio_dev); 421 error_remove_trigger: 422 hid_sensor_remove_trigger(indio_dev, &accel_state->common_attributes); 423 error_free_dev_mem: 424 kfree(indio_dev->channels); 425 return ret; 426 } 427 428 /* Function to deinitialize the processing for usage id */ 429 static int hid_accel_3d_remove(struct platform_device *pdev) 430 { 431 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 432 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 433 struct accel_3d_state *accel_state = iio_priv(indio_dev); 434 435 sensor_hub_remove_callback(hsdev, hsdev->usage); 436 iio_device_unregister(indio_dev); 437 hid_sensor_remove_trigger(indio_dev, &accel_state->common_attributes); 438 kfree(indio_dev->channels); 439 440 return 0; 441 } 442 443 static const struct platform_device_id hid_accel_3d_ids[] = { 444 { 445 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 446 .name = "HID-SENSOR-200073", 447 }, 448 { /* gravity sensor */ 449 .name = "HID-SENSOR-20007b", 450 }, 451 { /* sentinel */ } 452 }; 453 MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids); 454 455 static struct platform_driver hid_accel_3d_platform_driver = { 456 .id_table = hid_accel_3d_ids, 457 .driver = { 458 .name = KBUILD_MODNAME, 459 .pm = &hid_sensor_pm_ops, 460 }, 461 .probe = hid_accel_3d_probe, 462 .remove = hid_accel_3d_remove, 463 }; 464 module_platform_driver(hid_accel_3d_platform_driver); 465 466 MODULE_DESCRIPTION("HID Sensor Accel 3D"); 467 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 468 MODULE_LICENSE("GPL"); 469