1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * HID Sensors Driver 4 * Copyright (c) 2013, Intel Corporation. 5 */ 6 7 #include <linux/device.h> 8 #include <linux/platform_device.h> 9 #include <linux/module.h> 10 #include <linux/interrupt.h> 11 #include <linux/irq.h> 12 #include <linux/slab.h> 13 #include <linux/delay.h> 14 #include <linux/hid-sensor-hub.h> 15 #include <linux/iio/iio.h> 16 #include <linux/iio/sysfs.h> 17 #include <linux/iio/buffer.h> 18 #include "../common/hid-sensors/hid-sensor-trigger.h" 19 20 enum incl_3d_channel { 21 CHANNEL_SCAN_INDEX_X, 22 CHANNEL_SCAN_INDEX_Y, 23 CHANNEL_SCAN_INDEX_Z, 24 INCLI_3D_CHANNEL_MAX, 25 }; 26 27 #define CHANNEL_SCAN_INDEX_TIMESTAMP INCLI_3D_CHANNEL_MAX 28 29 struct incl_3d_state { 30 struct hid_sensor_hub_callbacks callbacks; 31 struct hid_sensor_common common_attributes; 32 struct hid_sensor_hub_attribute_info incl[INCLI_3D_CHANNEL_MAX]; 33 struct { 34 u32 incl_val[INCLI_3D_CHANNEL_MAX]; 35 u64 timestamp __aligned(8); 36 } scan; 37 int scale_pre_decml; 38 int scale_post_decml; 39 int scale_precision; 40 int value_offset; 41 s64 timestamp; 42 }; 43 44 static const u32 incl_3d_addresses[INCLI_3D_CHANNEL_MAX] = { 45 HID_USAGE_SENSOR_ORIENT_TILT_X, 46 HID_USAGE_SENSOR_ORIENT_TILT_Y, 47 HID_USAGE_SENSOR_ORIENT_TILT_Z 48 }; 49 50 static const u32 incl_3d_sensitivity_addresses[] = { 51 HID_USAGE_SENSOR_DATA_ORIENTATION, 52 HID_USAGE_SENSOR_ORIENT_TILT, 53 }; 54 55 /* Channel definitions */ 56 static const struct iio_chan_spec incl_3d_channels[] = { 57 { 58 .type = IIO_INCLI, 59 .modified = 1, 60 .channel2 = IIO_MOD_X, 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_X, 67 }, { 68 .type = IIO_INCLI, 69 .modified = 1, 70 .channel2 = IIO_MOD_Y, 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_Y, 77 }, { 78 .type = IIO_INCLI, 79 .modified = 1, 80 .channel2 = IIO_MOD_Z, 81 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 82 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 83 BIT(IIO_CHAN_INFO_SCALE) | 84 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 85 BIT(IIO_CHAN_INFO_HYSTERESIS), 86 .scan_index = CHANNEL_SCAN_INDEX_Z, 87 }, 88 IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP), 89 }; 90 91 /* Adjust channel real bits based on report descriptor */ 92 static void incl_3d_adjust_channel_bit_mask(struct iio_chan_spec *chan, 93 int size) 94 { 95 chan->scan_type.sign = 's'; 96 /* Real storage bits will change based on the report desc. */ 97 chan->scan_type.realbits = size * 8; 98 /* Maximum size of a sample to capture is u32 */ 99 chan->scan_type.storagebits = sizeof(u32) * 8; 100 } 101 102 /* Channel read_raw handler */ 103 static int incl_3d_read_raw(struct iio_dev *indio_dev, 104 struct iio_chan_spec const *chan, 105 int *val, int *val2, 106 long mask) 107 { 108 struct incl_3d_state *incl_state = iio_priv(indio_dev); 109 int report_id = -1; 110 u32 address; 111 int ret_type; 112 s32 min; 113 114 *val = 0; 115 *val2 = 0; 116 switch (mask) { 117 case IIO_CHAN_INFO_RAW: 118 hid_sensor_power_state(&incl_state->common_attributes, true); 119 report_id = incl_state->incl[chan->scan_index].report_id; 120 min = incl_state->incl[chan->scan_index].logical_minimum; 121 address = incl_3d_addresses[chan->scan_index]; 122 if (report_id >= 0) 123 *val = sensor_hub_input_attr_get_raw_value( 124 incl_state->common_attributes.hsdev, 125 HID_USAGE_SENSOR_INCLINOMETER_3D, address, 126 report_id, 127 SENSOR_HUB_SYNC, 128 min < 0); 129 else { 130 hid_sensor_power_state(&incl_state->common_attributes, 131 false); 132 return -EINVAL; 133 } 134 hid_sensor_power_state(&incl_state->common_attributes, false); 135 ret_type = IIO_VAL_INT; 136 break; 137 case IIO_CHAN_INFO_SCALE: 138 *val = incl_state->scale_pre_decml; 139 *val2 = incl_state->scale_post_decml; 140 ret_type = incl_state->scale_precision; 141 break; 142 case IIO_CHAN_INFO_OFFSET: 143 *val = incl_state->value_offset; 144 ret_type = IIO_VAL_INT; 145 break; 146 case IIO_CHAN_INFO_SAMP_FREQ: 147 ret_type = hid_sensor_read_samp_freq_value( 148 &incl_state->common_attributes, val, val2); 149 break; 150 case IIO_CHAN_INFO_HYSTERESIS: 151 ret_type = hid_sensor_read_raw_hyst_value( 152 &incl_state->common_attributes, val, val2); 153 break; 154 default: 155 ret_type = -EINVAL; 156 break; 157 } 158 159 return ret_type; 160 } 161 162 /* Channel write_raw handler */ 163 static int incl_3d_write_raw(struct iio_dev *indio_dev, 164 struct iio_chan_spec const *chan, 165 int val, 166 int val2, 167 long mask) 168 { 169 struct incl_3d_state *incl_state = iio_priv(indio_dev); 170 int ret; 171 172 switch (mask) { 173 case IIO_CHAN_INFO_SAMP_FREQ: 174 ret = hid_sensor_write_samp_freq_value( 175 &incl_state->common_attributes, val, val2); 176 break; 177 case IIO_CHAN_INFO_HYSTERESIS: 178 ret = hid_sensor_write_raw_hyst_value( 179 &incl_state->common_attributes, val, val2); 180 break; 181 default: 182 ret = -EINVAL; 183 } 184 185 return ret; 186 } 187 188 static const struct iio_info incl_3d_info = { 189 .read_raw = &incl_3d_read_raw, 190 .write_raw = &incl_3d_write_raw, 191 }; 192 193 /* Callback handler to send event after all samples are received and captured */ 194 static int incl_3d_proc_event(struct hid_sensor_hub_device *hsdev, 195 unsigned usage_id, 196 void *priv) 197 { 198 struct iio_dev *indio_dev = platform_get_drvdata(priv); 199 struct incl_3d_state *incl_state = iio_priv(indio_dev); 200 201 dev_dbg(&indio_dev->dev, "incl_3d_proc_event\n"); 202 if (atomic_read(&incl_state->common_attributes.data_ready)) { 203 if (!incl_state->timestamp) 204 incl_state->timestamp = iio_get_time_ns(indio_dev); 205 206 iio_push_to_buffers_with_timestamp(indio_dev, 207 &incl_state->scan, 208 incl_state->timestamp); 209 210 incl_state->timestamp = 0; 211 } 212 213 return 0; 214 } 215 216 /* Capture samples in local storage */ 217 static int incl_3d_capture_sample(struct hid_sensor_hub_device *hsdev, 218 unsigned usage_id, 219 size_t raw_len, char *raw_data, 220 void *priv) 221 { 222 struct iio_dev *indio_dev = platform_get_drvdata(priv); 223 struct incl_3d_state *incl_state = iio_priv(indio_dev); 224 int ret = 0; 225 226 switch (usage_id) { 227 case HID_USAGE_SENSOR_ORIENT_TILT_X: 228 incl_state->scan.incl_val[CHANNEL_SCAN_INDEX_X] = *(u32 *)raw_data; 229 break; 230 case HID_USAGE_SENSOR_ORIENT_TILT_Y: 231 incl_state->scan.incl_val[CHANNEL_SCAN_INDEX_Y] = *(u32 *)raw_data; 232 break; 233 case HID_USAGE_SENSOR_ORIENT_TILT_Z: 234 incl_state->scan.incl_val[CHANNEL_SCAN_INDEX_Z] = *(u32 *)raw_data; 235 break; 236 case HID_USAGE_SENSOR_TIME_TIMESTAMP: 237 incl_state->timestamp = 238 hid_sensor_convert_timestamp(&incl_state->common_attributes, 239 *(s64 *)raw_data); 240 break; 241 default: 242 ret = -EINVAL; 243 break; 244 } 245 246 return ret; 247 } 248 249 /* Parse report which is specific to an usage id*/ 250 static int incl_3d_parse_report(struct platform_device *pdev, 251 struct hid_sensor_hub_device *hsdev, 252 struct iio_chan_spec *channels, 253 unsigned usage_id, 254 struct incl_3d_state *st) 255 { 256 int ret; 257 258 ret = sensor_hub_input_get_attribute_info(hsdev, 259 HID_INPUT_REPORT, 260 usage_id, 261 HID_USAGE_SENSOR_ORIENT_TILT_X, 262 &st->incl[CHANNEL_SCAN_INDEX_X]); 263 if (ret) 264 return ret; 265 incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_X], 266 st->incl[CHANNEL_SCAN_INDEX_X].size); 267 268 ret = sensor_hub_input_get_attribute_info(hsdev, 269 HID_INPUT_REPORT, 270 usage_id, 271 HID_USAGE_SENSOR_ORIENT_TILT_Y, 272 &st->incl[CHANNEL_SCAN_INDEX_Y]); 273 if (ret) 274 return ret; 275 incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Y], 276 st->incl[CHANNEL_SCAN_INDEX_Y].size); 277 278 ret = sensor_hub_input_get_attribute_info(hsdev, 279 HID_INPUT_REPORT, 280 usage_id, 281 HID_USAGE_SENSOR_ORIENT_TILT_Z, 282 &st->incl[CHANNEL_SCAN_INDEX_Z]); 283 if (ret) 284 return ret; 285 incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Z], 286 st->incl[CHANNEL_SCAN_INDEX_Z].size); 287 288 dev_dbg(&pdev->dev, "incl_3d %x:%x, %x:%x, %x:%x\n", 289 st->incl[0].index, 290 st->incl[0].report_id, 291 st->incl[1].index, st->incl[1].report_id, 292 st->incl[2].index, st->incl[2].report_id); 293 294 st->scale_precision = hid_sensor_format_scale( 295 HID_USAGE_SENSOR_INCLINOMETER_3D, 296 &st->incl[CHANNEL_SCAN_INDEX_X], 297 &st->scale_pre_decml, &st->scale_post_decml); 298 299 return ret; 300 } 301 302 /* Function to initialize the processing for usage id */ 303 static int hid_incl_3d_probe(struct platform_device *pdev) 304 { 305 int ret; 306 static char *name = "incli_3d"; 307 struct iio_dev *indio_dev; 308 struct incl_3d_state *incl_state; 309 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 310 311 indio_dev = devm_iio_device_alloc(&pdev->dev, 312 sizeof(struct incl_3d_state)); 313 if (indio_dev == NULL) 314 return -ENOMEM; 315 316 platform_set_drvdata(pdev, indio_dev); 317 318 incl_state = iio_priv(indio_dev); 319 incl_state->common_attributes.hsdev = hsdev; 320 incl_state->common_attributes.pdev = pdev; 321 322 ret = hid_sensor_parse_common_attributes(hsdev, 323 HID_USAGE_SENSOR_INCLINOMETER_3D, 324 &incl_state->common_attributes, 325 incl_3d_sensitivity_addresses, 326 ARRAY_SIZE(incl_3d_sensitivity_addresses)); 327 if (ret) { 328 dev_err(&pdev->dev, "failed to setup common attributes\n"); 329 return ret; 330 } 331 332 indio_dev->channels = kmemdup(incl_3d_channels, 333 sizeof(incl_3d_channels), GFP_KERNEL); 334 if (!indio_dev->channels) { 335 dev_err(&pdev->dev, "failed to duplicate channels\n"); 336 return -ENOMEM; 337 } 338 339 ret = incl_3d_parse_report(pdev, hsdev, 340 (struct iio_chan_spec *)indio_dev->channels, 341 HID_USAGE_SENSOR_INCLINOMETER_3D, 342 incl_state); 343 if (ret) { 344 dev_err(&pdev->dev, "failed to setup attributes\n"); 345 goto error_free_dev_mem; 346 } 347 348 indio_dev->num_channels = ARRAY_SIZE(incl_3d_channels); 349 indio_dev->info = &incl_3d_info; 350 indio_dev->name = name; 351 indio_dev->modes = INDIO_DIRECT_MODE; 352 353 atomic_set(&incl_state->common_attributes.data_ready, 0); 354 355 ret = hid_sensor_setup_trigger(indio_dev, name, 356 &incl_state->common_attributes); 357 if (ret) { 358 dev_err(&pdev->dev, "trigger setup failed\n"); 359 goto error_free_dev_mem; 360 } 361 362 ret = iio_device_register(indio_dev); 363 if (ret) { 364 dev_err(&pdev->dev, "device register failed\n"); 365 goto error_remove_trigger; 366 } 367 368 incl_state->callbacks.send_event = incl_3d_proc_event; 369 incl_state->callbacks.capture_sample = incl_3d_capture_sample; 370 incl_state->callbacks.pdev = pdev; 371 ret = sensor_hub_register_callback(hsdev, 372 HID_USAGE_SENSOR_INCLINOMETER_3D, 373 &incl_state->callbacks); 374 if (ret) { 375 dev_err(&pdev->dev, "callback reg failed\n"); 376 goto error_iio_unreg; 377 } 378 379 return 0; 380 381 error_iio_unreg: 382 iio_device_unregister(indio_dev); 383 error_remove_trigger: 384 hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes); 385 error_free_dev_mem: 386 kfree(indio_dev->channels); 387 return ret; 388 } 389 390 /* Function to deinitialize the processing for usage id */ 391 static int hid_incl_3d_remove(struct platform_device *pdev) 392 { 393 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 394 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 395 struct incl_3d_state *incl_state = iio_priv(indio_dev); 396 397 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_INCLINOMETER_3D); 398 iio_device_unregister(indio_dev); 399 hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes); 400 kfree(indio_dev->channels); 401 402 return 0; 403 } 404 405 static const struct platform_device_id hid_incl_3d_ids[] = { 406 { 407 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 408 .name = "HID-SENSOR-200086", 409 }, 410 { /* sentinel */ } 411 }; 412 MODULE_DEVICE_TABLE(platform, hid_incl_3d_ids); 413 414 static struct platform_driver hid_incl_3d_platform_driver = { 415 .id_table = hid_incl_3d_ids, 416 .driver = { 417 .name = KBUILD_MODNAME, 418 .pm = &hid_sensor_pm_ops, 419 }, 420 .probe = hid_incl_3d_probe, 421 .remove = hid_incl_3d_remove, 422 }; 423 module_platform_driver(hid_incl_3d_platform_driver); 424 425 MODULE_DESCRIPTION("HID Sensor Inclinometer 3D"); 426 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); 427 MODULE_LICENSE("GPL"); 428