3 * Copyright (c) 2014, Intel Corporation.
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.
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
14 * You should have received a copy of the GNU General Public License along with
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"
33 #define CHANNEL_SCAN_INDEX_PRESSURE 0
36 struct hid_sensor_hub_callbacks callbacks;
37 struct hid_sensor_common common_attributes;
38 struct hid_sensor_hub_attribute_info press_attr;
46 /* Channel definitions */
47 static const struct iio_chan_spec press_channels[] = {
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,
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)
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;
72 /* Channel read_raw handler */
73 static int press_read_raw(struct iio_dev *indio_dev,
74 struct iio_chan_spec const *chan,
78 struct press_state *press_state = iio_priv(indio_dev);
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;
93 HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE;
100 poll_value = hid_sensor_read_poll_value(
101 &press_state->common_attributes);
104 hid_sensor_power_state(&press_state->common_attributes,
107 msleep_interruptible(poll_value * 2);
109 *val = sensor_hub_input_attr_get_raw_value(
110 press_state->common_attributes.hsdev,
111 HID_USAGE_SENSOR_PRESSURE, address,
113 hid_sensor_power_state(&press_state->common_attributes,
119 ret_type = IIO_VAL_INT;
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;
126 case IIO_CHAN_INFO_OFFSET:
127 *val = press_state->value_offset;
128 ret_type = IIO_VAL_INT;
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;
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;
148 /* Channel write_raw handler */
149 static int press_write_raw(struct iio_dev *indio_dev,
150 struct iio_chan_spec const *chan,
155 struct press_state *press_state = iio_priv(indio_dev);
159 case IIO_CHAN_INFO_SAMP_FREQ:
160 ret = hid_sensor_write_samp_freq_value(
161 &press_state->common_attributes, val, val2);
163 case IIO_CHAN_INFO_HYSTERESIS:
164 ret = hid_sensor_write_raw_hyst_value(
165 &press_state->common_attributes, val, val2);
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,
180 /* Function to push data to buffer */
181 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
184 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
185 iio_push_to_buffers(indio_dev, data);
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,
193 struct iio_dev *indio_dev = platform_get_drvdata(priv);
194 struct press_state *press_state = iio_priv(indio_dev);
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));
205 /* Capture samples in local storage */
206 static int press_capture_sample(struct hid_sensor_hub_device *hsdev,
208 size_t raw_len, char *raw_data,
211 struct iio_dev *indio_dev = platform_get_drvdata(priv);
212 struct press_state *press_state = iio_priv(indio_dev);
216 case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE:
217 press_state->press_data = *(u32 *)raw_data;
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,
232 struct press_state *st)
236 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
238 HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE,
242 press_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESSURE,
243 st->press_attr.size);
245 dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
246 st->press_attr.report_id);
248 st->scale_precision = hid_sensor_format_scale(
249 HID_USAGE_SENSOR_PRESSURE,
251 &st->scale_pre_decml, &st->scale_post_decml);
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);
267 /* Function to initialize the processing for usage id */
268 static int hid_press_probe(struct platform_device *pdev)
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;
277 indio_dev = devm_iio_device_alloc(&pdev->dev,
278 sizeof(struct press_state));
281 platform_set_drvdata(pdev, indio_dev);
283 press_state = iio_priv(indio_dev);
284 press_state->common_attributes.hsdev = hsdev;
285 press_state->common_attributes.pdev = pdev;
287 ret = hid_sensor_parse_common_attributes(hsdev,
288 HID_USAGE_SENSOR_PRESSURE,
289 &press_state->common_attributes);
291 dev_err(&pdev->dev, "failed to setup common attributes\n");
295 channels = kmemdup(press_channels, sizeof(press_channels), GFP_KERNEL);
297 dev_err(&pdev->dev, "failed to duplicate channels\n");
301 ret = press_parse_report(pdev, hsdev, channels,
302 HID_USAGE_SENSOR_PRESSURE, press_state);
304 dev_err(&pdev->dev, "failed to setup attributes\n");
305 goto error_free_dev_mem;
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;
316 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
319 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
320 goto error_free_dev_mem;
322 atomic_set(&press_state->common_attributes.data_ready, 0);
323 ret = hid_sensor_setup_trigger(indio_dev, name,
324 &press_state->common_attributes);
326 dev_err(&pdev->dev, "trigger setup failed\n");
327 goto error_unreg_buffer_funcs;
330 ret = iio_device_register(indio_dev);
332 dev_err(&pdev->dev, "device register failed\n");
333 goto error_remove_trigger;
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);
342 dev_err(&pdev->dev, "callback reg failed\n");
343 goto 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);
355 kfree(indio_dev->channels);
359 /* Function to deinitialize the processing for usage id */
360 static int hid_press_remove(struct platform_device *pdev)
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);
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);
375 static struct platform_device_id hid_press_ids[] = {
377 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
378 .name = "HID-SENSOR-200031",
382 MODULE_DEVICE_TABLE(platform, hid_press_ids);
384 static struct platform_driver hid_press_platform_driver = {
385 .id_table = hid_press_ids,
387 .name = KBUILD_MODNAME,
388 .owner = THIS_MODULE,
390 .probe = hid_press_probe,
391 .remove = hid_press_remove,
393 module_platform_driver(hid_press_platform_driver);
395 MODULE_DESCRIPTION("HID Sensor Pressure");
396 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
397 MODULE_LICENSE("GPL");