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