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