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);
87 case IIO_CHAN_INFO_RAW:
88 switch (chan->scan_index) {
89 case CHANNEL_SCAN_INDEX_PRESSURE:
90 report_id = press_state->press_attr.report_id;
92 HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE;
99 poll_value = hid_sensor_read_poll_value(
100 &press_state->common_attributes);
103 hid_sensor_power_state(&press_state->common_attributes,
106 msleep_interruptible(poll_value * 2);
108 *val = sensor_hub_input_attr_get_raw_value(
109 press_state->common_attributes.hsdev,
110 HID_USAGE_SENSOR_PRESSURE, address,
112 hid_sensor_power_state(&press_state->common_attributes,
118 ret_type = IIO_VAL_INT;
120 case IIO_CHAN_INFO_SCALE:
121 *val = press_state->scale_pre_decml;
122 *val2 = press_state->scale_post_decml;
123 ret_type = press_state->scale_precision;
125 case IIO_CHAN_INFO_OFFSET:
126 *val = press_state->value_offset;
127 ret_type = IIO_VAL_INT;
129 case IIO_CHAN_INFO_SAMP_FREQ:
130 ret_type = hid_sensor_read_samp_freq_value(
131 &press_state->common_attributes, val, val2);
133 case IIO_CHAN_INFO_HYSTERESIS:
134 ret_type = hid_sensor_read_raw_hyst_value(
135 &press_state->common_attributes, val, val2);
145 /* Channel write_raw handler */
146 static int press_write_raw(struct iio_dev *indio_dev,
147 struct iio_chan_spec const *chan,
152 struct press_state *press_state = iio_priv(indio_dev);
156 case IIO_CHAN_INFO_SAMP_FREQ:
157 ret = hid_sensor_write_samp_freq_value(
158 &press_state->common_attributes, val, val2);
160 case IIO_CHAN_INFO_HYSTERESIS:
161 ret = hid_sensor_write_raw_hyst_value(
162 &press_state->common_attributes, val, val2);
171 static const struct iio_info press_info = {
172 .driver_module = THIS_MODULE,
173 .read_raw = &press_read_raw,
174 .write_raw = &press_write_raw,
177 /* Function to push data to buffer */
178 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
181 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
182 iio_push_to_buffers(indio_dev, data);
185 /* Callback handler to send event after all samples are received and captured */
186 static int press_proc_event(struct hid_sensor_hub_device *hsdev,
190 struct iio_dev *indio_dev = platform_get_drvdata(priv);
191 struct press_state *press_state = iio_priv(indio_dev);
193 dev_dbg(&indio_dev->dev, "press_proc_event\n");
194 if (atomic_read(&press_state->common_attributes.data_ready))
195 hid_sensor_push_data(indio_dev,
196 &press_state->press_data,
197 sizeof(press_state->press_data));
202 /* Capture samples in local storage */
203 static int press_capture_sample(struct hid_sensor_hub_device *hsdev,
205 size_t raw_len, char *raw_data,
208 struct iio_dev *indio_dev = platform_get_drvdata(priv);
209 struct press_state *press_state = iio_priv(indio_dev);
213 case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE:
214 press_state->press_data = *(u32 *)raw_data;
224 /* Parse report which is specific to an usage id*/
225 static int press_parse_report(struct platform_device *pdev,
226 struct hid_sensor_hub_device *hsdev,
227 struct iio_chan_spec *channels,
229 struct press_state *st)
233 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
235 HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE,
239 press_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESSURE,
240 st->press_attr.size);
242 dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
243 st->press_attr.report_id);
245 st->scale_precision = hid_sensor_format_scale(
246 HID_USAGE_SENSOR_PRESSURE,
248 &st->scale_pre_decml, &st->scale_post_decml);
250 /* Set Sensitivity field ids, when there is no individual modifier */
251 if (st->common_attributes.sensitivity.index < 0) {
252 sensor_hub_input_get_attribute_info(hsdev,
253 HID_FEATURE_REPORT, usage_id,
254 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
255 HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE,
256 &st->common_attributes.sensitivity);
257 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
258 st->common_attributes.sensitivity.index,
259 st->common_attributes.sensitivity.report_id);
264 /* Function to initialize the processing for usage id */
265 static int hid_press_probe(struct platform_device *pdev)
268 static const char *name = "press";
269 struct iio_dev *indio_dev;
270 struct press_state *press_state;
271 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
272 struct iio_chan_spec *channels;
274 indio_dev = devm_iio_device_alloc(&pdev->dev,
275 sizeof(struct press_state));
278 platform_set_drvdata(pdev, indio_dev);
280 press_state = iio_priv(indio_dev);
281 press_state->common_attributes.hsdev = hsdev;
282 press_state->common_attributes.pdev = pdev;
284 ret = hid_sensor_parse_common_attributes(hsdev,
285 HID_USAGE_SENSOR_PRESSURE,
286 &press_state->common_attributes);
288 dev_err(&pdev->dev, "failed to setup common attributes\n");
292 channels = kmemdup(press_channels, sizeof(press_channels), GFP_KERNEL);
294 dev_err(&pdev->dev, "failed to duplicate channels\n");
298 ret = press_parse_report(pdev, hsdev, channels,
299 HID_USAGE_SENSOR_PRESSURE, press_state);
301 dev_err(&pdev->dev, "failed to setup attributes\n");
302 goto error_free_dev_mem;
305 indio_dev->channels = channels;
306 indio_dev->num_channels =
307 ARRAY_SIZE(press_channels);
308 indio_dev->dev.parent = &pdev->dev;
309 indio_dev->info = &press_info;
310 indio_dev->name = name;
311 indio_dev->modes = INDIO_DIRECT_MODE;
313 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
316 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
317 goto error_free_dev_mem;
319 atomic_set(&press_state->common_attributes.data_ready, 0);
320 ret = hid_sensor_setup_trigger(indio_dev, name,
321 &press_state->common_attributes);
323 dev_err(&pdev->dev, "trigger setup failed\n");
324 goto error_unreg_buffer_funcs;
327 ret = iio_device_register(indio_dev);
329 dev_err(&pdev->dev, "device register failed\n");
330 goto error_remove_trigger;
333 press_state->callbacks.send_event = press_proc_event;
334 press_state->callbacks.capture_sample = press_capture_sample;
335 press_state->callbacks.pdev = pdev;
336 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PRESSURE,
337 &press_state->callbacks);
339 dev_err(&pdev->dev, "callback reg failed\n");
340 goto error_iio_unreg;
346 iio_device_unregister(indio_dev);
347 error_remove_trigger:
348 hid_sensor_remove_trigger(&press_state->common_attributes);
349 error_unreg_buffer_funcs:
350 iio_triggered_buffer_cleanup(indio_dev);
352 kfree(indio_dev->channels);
356 /* Function to deinitialize the processing for usage id */
357 static int hid_press_remove(struct platform_device *pdev)
359 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
360 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
361 struct press_state *press_state = iio_priv(indio_dev);
363 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PRESSURE);
364 iio_device_unregister(indio_dev);
365 hid_sensor_remove_trigger(&press_state->common_attributes);
366 iio_triggered_buffer_cleanup(indio_dev);
367 kfree(indio_dev->channels);
372 static struct platform_device_id hid_press_ids[] = {
374 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
375 .name = "HID-SENSOR-200031",
379 MODULE_DEVICE_TABLE(platform, hid_press_ids);
381 static struct platform_driver hid_press_platform_driver = {
382 .id_table = hid_press_ids,
384 .name = KBUILD_MODNAME,
385 .owner = THIS_MODULE,
387 .probe = hid_press_probe,
388 .remove = hid_press_remove,
390 module_platform_driver(hid_press_platform_driver);
392 MODULE_DESCRIPTION("HID Sensor Pressure");
393 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
394 MODULE_LICENSE("GPL");