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 "../common/hid-sensors/hid-sensor-trigger.h"
19 #define CHANNEL_SCAN_INDEX_PRESSURE 0
22 struct hid_sensor_hub_callbacks callbacks
;
23 struct hid_sensor_common common_attributes
;
24 struct hid_sensor_hub_attribute_info press_attr
;
32 /* Channel definitions */
33 static const struct iio_chan_spec press_channels
[] = {
36 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
37 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_OFFSET
) |
38 BIT(IIO_CHAN_INFO_SCALE
) |
39 BIT(IIO_CHAN_INFO_SAMP_FREQ
) |
40 BIT(IIO_CHAN_INFO_HYSTERESIS
),
41 .scan_index
= CHANNEL_SCAN_INDEX_PRESSURE
,
45 /* Adjust channel real bits based on report descriptor */
46 static void press_adjust_channel_bit_mask(struct iio_chan_spec
*channels
,
47 int channel
, int size
)
49 channels
[channel
].scan_type
.sign
= 's';
50 /* Real storage bits will change based on the report desc. */
51 channels
[channel
].scan_type
.realbits
= size
* 8;
52 /* Maximum size of a sample to capture is u32 */
53 channels
[channel
].scan_type
.storagebits
= sizeof(u32
) * 8;
56 /* Channel read_raw handler */
57 static int press_read_raw(struct iio_dev
*indio_dev
,
58 struct iio_chan_spec
const *chan
,
62 struct press_state
*press_state
= iio_priv(indio_dev
);
71 case IIO_CHAN_INFO_RAW
:
72 switch (chan
->scan_index
) {
73 case CHANNEL_SCAN_INDEX_PRESSURE
:
74 report_id
= press_state
->press_attr
.report_id
;
75 min
= press_state
->press_attr
.logical_minimum
;
76 address
= HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE
;
83 hid_sensor_power_state(&press_state
->common_attributes
,
85 *val
= sensor_hub_input_attr_get_raw_value(
86 press_state
->common_attributes
.hsdev
,
87 HID_USAGE_SENSOR_PRESSURE
, address
,
91 hid_sensor_power_state(&press_state
->common_attributes
,
97 ret_type
= IIO_VAL_INT
;
99 case IIO_CHAN_INFO_SCALE
:
100 *val
= press_state
->scale_pre_decml
;
101 *val2
= press_state
->scale_post_decml
;
102 ret_type
= press_state
->scale_precision
;
104 case IIO_CHAN_INFO_OFFSET
:
105 *val
= press_state
->value_offset
;
106 ret_type
= IIO_VAL_INT
;
108 case IIO_CHAN_INFO_SAMP_FREQ
:
109 ret_type
= hid_sensor_read_samp_freq_value(
110 &press_state
->common_attributes
, val
, val2
);
112 case IIO_CHAN_INFO_HYSTERESIS
:
113 ret_type
= hid_sensor_read_raw_hyst_value(
114 &press_state
->common_attributes
, val
, val2
);
124 /* Channel write_raw handler */
125 static int press_write_raw(struct iio_dev
*indio_dev
,
126 struct iio_chan_spec
const *chan
,
131 struct press_state
*press_state
= iio_priv(indio_dev
);
135 case IIO_CHAN_INFO_SAMP_FREQ
:
136 ret
= hid_sensor_write_samp_freq_value(
137 &press_state
->common_attributes
, val
, val2
);
139 case IIO_CHAN_INFO_HYSTERESIS
:
140 ret
= hid_sensor_write_raw_hyst_value(
141 &press_state
->common_attributes
, val
, val2
);
150 static const struct iio_info press_info
= {
151 .read_raw
= &press_read_raw
,
152 .write_raw
= &press_write_raw
,
155 /* Function to push data to buffer */
156 static void hid_sensor_push_data(struct iio_dev
*indio_dev
, const void *data
,
159 dev_dbg(&indio_dev
->dev
, "hid_sensor_push_data\n");
160 iio_push_to_buffers(indio_dev
, data
);
163 /* Callback handler to send event after all samples are received and captured */
164 static int press_proc_event(struct hid_sensor_hub_device
*hsdev
,
168 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
169 struct press_state
*press_state
= iio_priv(indio_dev
);
171 dev_dbg(&indio_dev
->dev
, "press_proc_event\n");
172 if (atomic_read(&press_state
->common_attributes
.data_ready
))
173 hid_sensor_push_data(indio_dev
,
174 &press_state
->press_data
,
175 sizeof(press_state
->press_data
));
180 /* Capture samples in local storage */
181 static int press_capture_sample(struct hid_sensor_hub_device
*hsdev
,
183 size_t raw_len
, char *raw_data
,
186 struct iio_dev
*indio_dev
= platform_get_drvdata(priv
);
187 struct press_state
*press_state
= iio_priv(indio_dev
);
191 case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE
:
192 press_state
->press_data
= *(u32
*)raw_data
;
202 /* Parse report which is specific to an usage id*/
203 static int press_parse_report(struct platform_device
*pdev
,
204 struct hid_sensor_hub_device
*hsdev
,
205 struct iio_chan_spec
*channels
,
207 struct press_state
*st
)
211 ret
= sensor_hub_input_get_attribute_info(hsdev
, HID_INPUT_REPORT
,
213 HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE
,
217 press_adjust_channel_bit_mask(channels
, CHANNEL_SCAN_INDEX_PRESSURE
,
218 st
->press_attr
.size
);
220 dev_dbg(&pdev
->dev
, "press %x:%x\n", st
->press_attr
.index
,
221 st
->press_attr
.report_id
);
223 st
->scale_precision
= hid_sensor_format_scale(
224 HID_USAGE_SENSOR_PRESSURE
,
226 &st
->scale_pre_decml
, &st
->scale_post_decml
);
228 /* Set Sensitivity field ids, when there is no individual modifier */
229 if (st
->common_attributes
.sensitivity
.index
< 0) {
230 sensor_hub_input_get_attribute_info(hsdev
,
231 HID_FEATURE_REPORT
, usage_id
,
232 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS
|
233 HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE
,
234 &st
->common_attributes
.sensitivity
);
235 dev_dbg(&pdev
->dev
, "Sensitivity index:report %d:%d\n",
236 st
->common_attributes
.sensitivity
.index
,
237 st
->common_attributes
.sensitivity
.report_id
);
242 /* Function to initialize the processing for usage id */
243 static int hid_press_probe(struct platform_device
*pdev
)
246 static const char *name
= "press";
247 struct iio_dev
*indio_dev
;
248 struct press_state
*press_state
;
249 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
251 indio_dev
= devm_iio_device_alloc(&pdev
->dev
,
252 sizeof(struct press_state
));
255 platform_set_drvdata(pdev
, indio_dev
);
257 press_state
= iio_priv(indio_dev
);
258 press_state
->common_attributes
.hsdev
= hsdev
;
259 press_state
->common_attributes
.pdev
= pdev
;
261 ret
= hid_sensor_parse_common_attributes(hsdev
,
262 HID_USAGE_SENSOR_PRESSURE
,
263 &press_state
->common_attributes
);
265 dev_err(&pdev
->dev
, "failed to setup common attributes\n");
269 indio_dev
->channels
= kmemdup(press_channels
, sizeof(press_channels
),
271 if (!indio_dev
->channels
) {
272 dev_err(&pdev
->dev
, "failed to duplicate channels\n");
276 ret
= press_parse_report(pdev
, hsdev
,
277 (struct iio_chan_spec
*)indio_dev
->channels
,
278 HID_USAGE_SENSOR_PRESSURE
, press_state
);
280 dev_err(&pdev
->dev
, "failed to setup attributes\n");
281 goto error_free_dev_mem
;
284 indio_dev
->num_channels
=
285 ARRAY_SIZE(press_channels
);
286 indio_dev
->info
= &press_info
;
287 indio_dev
->name
= name
;
288 indio_dev
->modes
= INDIO_DIRECT_MODE
;
290 atomic_set(&press_state
->common_attributes
.data_ready
, 0);
292 ret
= hid_sensor_setup_trigger(indio_dev
, name
,
293 &press_state
->common_attributes
);
295 dev_err(&pdev
->dev
, "trigger setup failed\n");
296 goto error_free_dev_mem
;
299 ret
= iio_device_register(indio_dev
);
301 dev_err(&pdev
->dev
, "device register failed\n");
302 goto error_remove_trigger
;
305 press_state
->callbacks
.send_event
= press_proc_event
;
306 press_state
->callbacks
.capture_sample
= press_capture_sample
;
307 press_state
->callbacks
.pdev
= pdev
;
308 ret
= sensor_hub_register_callback(hsdev
, HID_USAGE_SENSOR_PRESSURE
,
309 &press_state
->callbacks
);
311 dev_err(&pdev
->dev
, "callback reg failed\n");
312 goto error_iio_unreg
;
318 iio_device_unregister(indio_dev
);
319 error_remove_trigger
:
320 hid_sensor_remove_trigger(indio_dev
, &press_state
->common_attributes
);
322 kfree(indio_dev
->channels
);
326 /* Function to deinitialize the processing for usage id */
327 static int hid_press_remove(struct platform_device
*pdev
)
329 struct hid_sensor_hub_device
*hsdev
= pdev
->dev
.platform_data
;
330 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
331 struct press_state
*press_state
= iio_priv(indio_dev
);
333 sensor_hub_remove_callback(hsdev
, HID_USAGE_SENSOR_PRESSURE
);
334 iio_device_unregister(indio_dev
);
335 hid_sensor_remove_trigger(indio_dev
, &press_state
->common_attributes
);
336 kfree(indio_dev
->channels
);
341 static const struct platform_device_id hid_press_ids
[] = {
343 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
344 .name
= "HID-SENSOR-200031",
348 MODULE_DEVICE_TABLE(platform
, hid_press_ids
);
350 static struct platform_driver hid_press_platform_driver
= {
351 .id_table
= hid_press_ids
,
353 .name
= KBUILD_MODNAME
,
354 .pm
= &hid_sensor_pm_ops
,
356 .probe
= hid_press_probe
,
357 .remove
= hid_press_remove
,
359 module_platform_driver(hid_press_platform_driver
);
361 MODULE_DESCRIPTION("HID Sensor Pressure");
362 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
363 MODULE_LICENSE("GPL");