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[] = {
50 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
51 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
52 BIT(IIO_CHAN_INFO_SCALE) |
53 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
54 BIT(IIO_CHAN_INFO_HYSTERESIS),
55 .scan_index = CHANNEL_SCAN_INDEX_PRESSURE,
59 /* Adjust channel real bits based on report descriptor */
60 static void press_adjust_channel_bit_mask(struct iio_chan_spec *channels,
61 int channel, int size)
63 channels[channel].scan_type.sign = 's';
64 /* Real storage bits will change based on the report desc. */
65 channels[channel].scan_type.realbits = size * 8;
66 /* Maximum size of a sample to capture is u32 */
67 channels[channel].scan_type.storagebits = sizeof(u32) * 8;
70 /* Channel read_raw handler */
71 static int press_read_raw(struct iio_dev *indio_dev,
72 struct iio_chan_spec const *chan,
76 struct press_state *press_state = iio_priv(indio_dev);
84 case IIO_CHAN_INFO_RAW:
85 switch (chan->scan_index) {
86 case CHANNEL_SCAN_INDEX_PRESSURE:
87 report_id = press_state->press_attr.report_id;
89 HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE;
96 hid_sensor_power_state(&press_state->common_attributes,
98 *val = sensor_hub_input_attr_get_raw_value(
99 press_state->common_attributes.hsdev,
100 HID_USAGE_SENSOR_PRESSURE, address,
103 hid_sensor_power_state(&press_state->common_attributes,
109 ret_type = IIO_VAL_INT;
111 case IIO_CHAN_INFO_SCALE:
112 *val = press_state->scale_pre_decml;
113 *val2 = press_state->scale_post_decml;
114 ret_type = press_state->scale_precision;
116 case IIO_CHAN_INFO_OFFSET:
117 *val = press_state->value_offset;
118 ret_type = IIO_VAL_INT;
120 case IIO_CHAN_INFO_SAMP_FREQ:
121 ret_type = hid_sensor_read_samp_freq_value(
122 &press_state->common_attributes, val, val2);
124 case IIO_CHAN_INFO_HYSTERESIS:
125 ret_type = hid_sensor_read_raw_hyst_value(
126 &press_state->common_attributes, val, val2);
136 /* Channel write_raw handler */
137 static int press_write_raw(struct iio_dev *indio_dev,
138 struct iio_chan_spec const *chan,
143 struct press_state *press_state = iio_priv(indio_dev);
147 case IIO_CHAN_INFO_SAMP_FREQ:
148 ret = hid_sensor_write_samp_freq_value(
149 &press_state->common_attributes, val, val2);
151 case IIO_CHAN_INFO_HYSTERESIS:
152 ret = hid_sensor_write_raw_hyst_value(
153 &press_state->common_attributes, val, val2);
162 static const struct iio_info press_info = {
163 .read_raw = &press_read_raw,
164 .write_raw = &press_write_raw,
167 /* Function to push data to buffer */
168 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
171 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
172 iio_push_to_buffers(indio_dev, data);
175 /* Callback handler to send event after all samples are received and captured */
176 static int press_proc_event(struct hid_sensor_hub_device *hsdev,
180 struct iio_dev *indio_dev = platform_get_drvdata(priv);
181 struct press_state *press_state = iio_priv(indio_dev);
183 dev_dbg(&indio_dev->dev, "press_proc_event\n");
184 if (atomic_read(&press_state->common_attributes.data_ready))
185 hid_sensor_push_data(indio_dev,
186 &press_state->press_data,
187 sizeof(press_state->press_data));
192 /* Capture samples in local storage */
193 static int press_capture_sample(struct hid_sensor_hub_device *hsdev,
195 size_t raw_len, char *raw_data,
198 struct iio_dev *indio_dev = platform_get_drvdata(priv);
199 struct press_state *press_state = iio_priv(indio_dev);
203 case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE:
204 press_state->press_data = *(u32 *)raw_data;
214 /* Parse report which is specific to an usage id*/
215 static int press_parse_report(struct platform_device *pdev,
216 struct hid_sensor_hub_device *hsdev,
217 struct iio_chan_spec *channels,
219 struct press_state *st)
223 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
225 HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE,
229 press_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESSURE,
230 st->press_attr.size);
232 dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
233 st->press_attr.report_id);
235 st->scale_precision = hid_sensor_format_scale(
236 HID_USAGE_SENSOR_PRESSURE,
238 &st->scale_pre_decml, &st->scale_post_decml);
240 /* Set Sensitivity field ids, when there is no individual modifier */
241 if (st->common_attributes.sensitivity.index < 0) {
242 sensor_hub_input_get_attribute_info(hsdev,
243 HID_FEATURE_REPORT, usage_id,
244 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
245 HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE,
246 &st->common_attributes.sensitivity);
247 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
248 st->common_attributes.sensitivity.index,
249 st->common_attributes.sensitivity.report_id);
254 /* Function to initialize the processing for usage id */
255 static int hid_press_probe(struct platform_device *pdev)
258 static const char *name = "press";
259 struct iio_dev *indio_dev;
260 struct press_state *press_state;
261 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
263 indio_dev = devm_iio_device_alloc(&pdev->dev,
264 sizeof(struct press_state));
267 platform_set_drvdata(pdev, indio_dev);
269 press_state = iio_priv(indio_dev);
270 press_state->common_attributes.hsdev = hsdev;
271 press_state->common_attributes.pdev = pdev;
273 ret = hid_sensor_parse_common_attributes(hsdev,
274 HID_USAGE_SENSOR_PRESSURE,
275 &press_state->common_attributes);
277 dev_err(&pdev->dev, "failed to setup common attributes\n");
281 indio_dev->channels = kmemdup(press_channels, sizeof(press_channels),
283 if (!indio_dev->channels) {
284 dev_err(&pdev->dev, "failed to duplicate channels\n");
288 ret = press_parse_report(pdev, hsdev,
289 (struct iio_chan_spec *)indio_dev->channels,
290 HID_USAGE_SENSOR_PRESSURE, press_state);
292 dev_err(&pdev->dev, "failed to setup attributes\n");
293 goto error_free_dev_mem;
296 indio_dev->num_channels =
297 ARRAY_SIZE(press_channels);
298 indio_dev->dev.parent = &pdev->dev;
299 indio_dev->info = &press_info;
300 indio_dev->name = name;
301 indio_dev->modes = INDIO_DIRECT_MODE;
303 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
306 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
307 goto error_free_dev_mem;
309 atomic_set(&press_state->common_attributes.data_ready, 0);
310 ret = hid_sensor_setup_trigger(indio_dev, name,
311 &press_state->common_attributes);
313 dev_err(&pdev->dev, "trigger setup failed\n");
314 goto error_unreg_buffer_funcs;
317 ret = iio_device_register(indio_dev);
319 dev_err(&pdev->dev, "device register failed\n");
320 goto error_remove_trigger;
323 press_state->callbacks.send_event = press_proc_event;
324 press_state->callbacks.capture_sample = press_capture_sample;
325 press_state->callbacks.pdev = pdev;
326 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PRESSURE,
327 &press_state->callbacks);
329 dev_err(&pdev->dev, "callback reg failed\n");
330 goto error_iio_unreg;
336 iio_device_unregister(indio_dev);
337 error_remove_trigger:
338 hid_sensor_remove_trigger(&press_state->common_attributes);
339 error_unreg_buffer_funcs:
340 iio_triggered_buffer_cleanup(indio_dev);
342 kfree(indio_dev->channels);
346 /* Function to deinitialize the processing for usage id */
347 static int hid_press_remove(struct platform_device *pdev)
349 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
350 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
351 struct press_state *press_state = iio_priv(indio_dev);
353 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PRESSURE);
354 iio_device_unregister(indio_dev);
355 hid_sensor_remove_trigger(&press_state->common_attributes);
356 iio_triggered_buffer_cleanup(indio_dev);
357 kfree(indio_dev->channels);
362 static const struct platform_device_id hid_press_ids[] = {
364 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
365 .name = "HID-SENSOR-200031",
369 MODULE_DEVICE_TABLE(platform, hid_press_ids);
371 static struct platform_driver hid_press_platform_driver = {
372 .id_table = hid_press_ids,
374 .name = KBUILD_MODNAME,
375 .pm = &hid_sensor_pm_ops,
377 .probe = hid_press_probe,
378 .remove = hid_press_remove,
380 module_platform_driver(hid_press_platform_driver);
382 MODULE_DESCRIPTION("HID Sensor Pressure");
383 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
384 MODULE_LICENSE("GPL");