1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (c) 2014, Intel Corporation.
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 <linux/iio/trigger_consumer.h>
18 #include <linux/iio/triggered_buffer.h>
19 #include "../common/hid-sensors/hid-sensor-trigger.h"
21 #define CHANNEL_SCAN_INDEX_PRESSURE 0
24 struct hid_sensor_hub_callbacks callbacks
;
25 struct hid_sensor_common common_attributes
;
26 struct hid_sensor_hub_attribute_info press_attr
;
34 /* Channel definitions */
35 static const struct iio_chan_spec press_channels
[] = {
38 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
39 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
40 BIT(IIO_CHAN_INFO_SCALE
) |
41 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
42 BIT(IIO_CHAN_INFO_HYSTERESIS
),
43 .scan_index
= CHANNEL_SCAN_INDEX_PRESSURE
,
47 /* Adjust channel real bits based on report descriptor */
48 static void press_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
49 int channel
, int size
)
51 channels
[channel
].scan_type
.sign
= 's';
52 /* Real storage bits will change based on the report desc. */
53 channels
[channel
].scan_type
.realbits
= size
* 8;
54 /* Maximum size of a sample to capture is u32 */
55 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
58 /* Channel read_raw handler */
59 static int press_read_raw(struct iio_dev
*indio_dev
,
60 struct iio_chan_spec
const *chan
,
64 struct press_state
*press_state
= iio_priv(indio_dev
);
73 case IIO_CHAN_INFO_RAW
:
74 switch (chan
->scan_index
) {
75 case CHANNEL_SCAN_INDEX_PRESSURE
:
76 report_id
= press_state
->press_attr
.report_id
;
77 min
= press_state
->press_attr
.logical_minimum
;
78 address
= HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE
;
85 hid_sensor_power_state(&press_state
->common_attributes
,
87 *val
= sensor_hub_input_attr_get_raw_value(
88 press_state
->common_attributes
.hsdev
,
89 HID_USAGE_SENSOR_PRESSURE
, address
,
93 hid_sensor_power_state(&press_state
->common_attributes
,
99 ret_type
= IIO_VAL_INT
;
101 case IIO_CHAN_INFO_SCALE
:
102 *val
= press_state
->scale_pre_decml
;
103 *val2
= press_state
->scale_post_decml
;
104 ret_type
= press_state
->scale_precision
;
106 case IIO_CHAN_INFO_OFFSET
:
107 *val
= press_state
->value_offset
;
108 ret_type
= IIO_VAL_INT
;
110 case IIO_CHAN_INFO_SAMP_FREQ
:
111 ret_type
= hid_sensor_read_samp_freq_value(
112 &press_state
->common_attributes
, val
, val2
);
114 case IIO_CHAN_INFO_HYSTERESIS
:
115 ret_type
= hid_sensor_read_raw_hyst_value(
116 &press_state
->common_attributes
, val
, val2
);
126 /* Channel write_raw handler */
127 static int press_write_raw(struct iio_dev
*indio_dev
,
128 struct iio_chan_spec
const *chan
,
133 struct press_state
*press_state
= iio_priv(indio_dev
);
137 case IIO_CHAN_INFO_SAMP_FREQ
:
138 ret
= hid_sensor_write_samp_freq_value(
139 &press_state
->common_attributes
, val
, val2
);
141 case IIO_CHAN_INFO_HYSTERESIS
:
142 ret
= hid_sensor_write_raw_hyst_value(
143 &press_state
->common_attributes
, val
, val2
);
152 static const struct iio_info press_info
= {
153 .read_raw
= &press_read_raw
,
154 .write_raw
= &press_write_raw
,
157 /* Function to push data to buffer */
158 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, const void *data
,
161 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
162 iio_push_to_buffers(indio_dev
, data
);
165 /* Callback handler to send event after all samples are received and captured */
166 static int press_proc_event(struct hid_sensor_hub_device
*hsdev
,
170 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
171 struct press_state
*press_state
= iio_priv(indio_dev
);
173 dev_dbg(&indio_dev
->dev
, "press_proc_event\n");
174 if (atomic_read(&press_state
->common_attributes
.data_ready
))
175 hid_sensor_push_data(indio_dev
,
176 &press_state
->press_data
,
177 sizeof(press_state
->press_data
));
182 /* Capture samples in local storage */
183 static int press_capture_sample(struct hid_sensor_hub_device
*hsdev
,
185 size_t raw_len
, char *raw_data
,
188 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
189 struct press_state
*press_state
= iio_priv(indio_dev
);
193 case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE
:
194 press_state
->press_data
= *(u32
*)raw_data
;
204 /* Parse report which is specific to an usage id*/
205 static int press_parse_report(struct platform_device
*pdev
,
206 struct hid_sensor_hub_device
*hsdev
,
207 struct iio_chan_spec
*channels
,
209 struct press_state
*st
)
213 ret
= sensor_hub_input_get_attribute_info(hsdev
, HID_INPUT_REPORT
,
215 HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE
,
219 press_adjust_channel_bit_mask(channels
, CHANNEL_SCAN_INDEX_PRESSURE
,
220 st
->press_attr
.size
);
222 dev_dbg(&pdev
->dev
, "press %x:%x\n", st
->press_attr
.index
,
223 st
->press_attr
.report_id
);
225 st
->scale_precision
= hid_sensor_format_scale(
226 HID_USAGE_SENSOR_PRESSURE
,
228 &st
->scale_pre_decml
, &st
->scale_post_decml
);
230 /* Set Sensitivity field ids, when there is no individual modifier */
231 if (st
->common_attributes
.sensitivity
.index
< 0) {
232 sensor_hub_input_get_attribute_info(hsdev
,
233 HID_FEATURE_REPORT
, usage_id
,
234 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
235 HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE
,
236 &st
->common_attributes
.sensitivity
);
237 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
238 st
->common_attributes
.sensitivity
.index
,
239 st
->common_attributes
.sensitivity
.report_id
);
244 /* Function to initialize the processing for usage id */
245 static int hid_press_probe(struct platform_device
*pdev
)
248 static const char *name
= "press";
249 struct iio_dev
*indio_dev
;
250 struct press_state
*press_state
;
251 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
253 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
254 sizeof(struct press_state
));
257 platform_set_drvdata(pdev
, indio_dev
);
259 press_state
= iio_priv(indio_dev
);
260 press_state
->common_attributes
.hsdev
= hsdev
;
261 press_state
->common_attributes
.pdev
= pdev
;
263 ret
= hid_sensor_parse_common_attributes(hsdev
,
264 HID_USAGE_SENSOR_PRESSURE
,
265 &press_state
->common_attributes
);
267 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
271 indio_dev
->channels
= kmemdup(press_channels
, sizeof(press_channels
),
273 if (!indio_dev
->channels
) {
274 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
278 ret
= press_parse_report(pdev
, hsdev
,
279 (struct iio_chan_spec
*)indio_dev
->channels
,
280 HID_USAGE_SENSOR_PRESSURE
, press_state
);
282 dev_err(&pdev
->dev
, "failed to setup attributes\n");
283 goto error_free_dev_mem
;
286 indio_dev
->num_channels
=
287 ARRAY_SIZE(press_channels
);
288 indio_dev
->dev
.parent
= &pdev
->dev
;
289 indio_dev
->info
= &press_info
;
290 indio_dev
->name
= name
;
291 indio_dev
->modes
= INDIO_DIRECT_MODE
;
293 ret
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
296 dev_err(&pdev
->dev
, "failed to initialize trigger buffer\n");
297 goto error_free_dev_mem
;
299 atomic_set(&press_state
->common_attributes
.data_ready
, 0);
300 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
301 &press_state
->common_attributes
);
303 dev_err(&pdev
->dev
, "trigger setup failed\n");
304 goto error_unreg_buffer_funcs
;
307 ret
= iio_device_register(indio_dev
);
309 dev_err(&pdev
->dev
, "device register failed\n");
310 goto error_remove_trigger
;
313 press_state
->callbacks
.send_event
= press_proc_event
;
314 press_state
->callbacks
.capture_sample
= press_capture_sample
;
315 press_state
->callbacks
.pdev
= pdev
;
316 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_PRESSURE
,
317 &press_state
->callbacks
);
319 dev_err(&pdev
->dev
, "callback reg failed\n");
320 goto error_iio_unreg
;
326 iio_device_unregister(indio_dev
);
327 error_remove_trigger
:
328 hid_sensor_remove_trigger(&press_state
->common_attributes
);
329 error_unreg_buffer_funcs
:
330 iio_triggered_buffer_cleanup(indio_dev
);
332 kfree(indio_dev
->channels
);
336 /* Function to deinitialize the processing for usage id */
337 static int hid_press_remove(struct platform_device
*pdev
)
339 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
340 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
341 struct press_state
*press_state
= iio_priv(indio_dev
);
343 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_PRESSURE
);
344 iio_device_unregister(indio_dev
);
345 hid_sensor_remove_trigger(&press_state
->common_attributes
);
346 iio_triggered_buffer_cleanup(indio_dev
);
347 kfree(indio_dev
->channels
);
352 static const struct platform_device_id hid_press_ids
[] = {
354 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
355 .name
= "HID-SENSOR-200031",
359 MODULE_DEVICE_TABLE(platform
, hid_press_ids
);
361 static struct platform_driver hid_press_platform_driver
= {
362 .id_table
= hid_press_ids
,
364 .name
= KBUILD_MODNAME
,
365 .pm
= &hid_sensor_pm_ops
,
367 .probe
= hid_press_probe
,
368 .remove
= hid_press_remove
,
370 module_platform_driver(hid_press_platform_driver
);
372 MODULE_DESCRIPTION("HID Sensor Pressure");
373 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
374 MODULE_LICENSE("GPL");