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