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