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