WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / pressure / hid-sensor-press.c
blob5c458788f346e72a47d56df9439b39b39ec7e161
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HID Sensors Driver
4 * Copyright (c) 2014, Intel Corporation.
5 */
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
21 struct press_state {
22 struct hid_sensor_hub_callbacks callbacks;
23 struct hid_sensor_common common_attributes;
24 struct hid_sensor_hub_attribute_info press_attr;
25 u32 press_data;
26 int scale_pre_decml;
27 int scale_post_decml;
28 int scale_precision;
29 int value_offset;
32 /* Channel definitions */
33 static const struct iio_chan_spec press_channels[] = {
35 .type = IIO_PRESSURE,
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,
59 int *val, int *val2,
60 long mask)
62 struct press_state *press_state = iio_priv(indio_dev);
63 int report_id = -1;
64 u32 address;
65 int ret_type;
66 s32 min;
68 *val = 0;
69 *val2 = 0;
70 switch (mask) {
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;
77 break;
78 default:
79 report_id = -1;
80 break;
82 if (report_id >= 0) {
83 hid_sensor_power_state(&press_state->common_attributes,
84 true);
85 *val = sensor_hub_input_attr_get_raw_value(
86 press_state->common_attributes.hsdev,
87 HID_USAGE_SENSOR_PRESSURE, address,
88 report_id,
89 SENSOR_HUB_SYNC,
90 min < 0);
91 hid_sensor_power_state(&press_state->common_attributes,
92 false);
93 } else {
94 *val = 0;
95 return -EINVAL;
97 ret_type = IIO_VAL_INT;
98 break;
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;
103 break;
104 case IIO_CHAN_INFO_OFFSET:
105 *val = press_state->value_offset;
106 ret_type = IIO_VAL_INT;
107 break;
108 case IIO_CHAN_INFO_SAMP_FREQ:
109 ret_type = hid_sensor_read_samp_freq_value(
110 &press_state->common_attributes, val, val2);
111 break;
112 case IIO_CHAN_INFO_HYSTERESIS:
113 ret_type = hid_sensor_read_raw_hyst_value(
114 &press_state->common_attributes, val, val2);
115 break;
116 default:
117 ret_type = -EINVAL;
118 break;
121 return ret_type;
124 /* Channel write_raw handler */
125 static int press_write_raw(struct iio_dev *indio_dev,
126 struct iio_chan_spec const *chan,
127 int val,
128 int val2,
129 long mask)
131 struct press_state *press_state = iio_priv(indio_dev);
132 int ret = 0;
134 switch (mask) {
135 case IIO_CHAN_INFO_SAMP_FREQ:
136 ret = hid_sensor_write_samp_freq_value(
137 &press_state->common_attributes, val, val2);
138 break;
139 case IIO_CHAN_INFO_HYSTERESIS:
140 ret = hid_sensor_write_raw_hyst_value(
141 &press_state->common_attributes, val, val2);
142 break;
143 default:
144 ret = -EINVAL;
147 return ret;
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,
157 int len)
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,
165 unsigned usage_id,
166 void *priv)
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));
177 return 0;
180 /* Capture samples in local storage */
181 static int press_capture_sample(struct hid_sensor_hub_device *hsdev,
182 unsigned usage_id,
183 size_t raw_len, char *raw_data,
184 void *priv)
186 struct iio_dev *indio_dev = platform_get_drvdata(priv);
187 struct press_state *press_state = iio_priv(indio_dev);
188 int ret = -EINVAL;
190 switch (usage_id) {
191 case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE:
192 press_state->press_data = *(u32 *)raw_data;
193 ret = 0;
194 break;
195 default:
196 break;
199 return ret;
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,
206 unsigned usage_id,
207 struct press_state *st)
209 int ret;
211 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
212 usage_id,
213 HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE,
214 &st->press_attr);
215 if (ret < 0)
216 return ret;
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,
225 &st->press_attr,
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);
239 return ret;
242 /* Function to initialize the processing for usage id */
243 static int hid_press_probe(struct platform_device *pdev)
245 int ret = 0;
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));
253 if (!indio_dev)
254 return -ENOMEM;
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);
264 if (ret) {
265 dev_err(&pdev->dev, "failed to setup common attributes\n");
266 return ret;
269 indio_dev->channels = kmemdup(press_channels, sizeof(press_channels),
270 GFP_KERNEL);
271 if (!indio_dev->channels) {
272 dev_err(&pdev->dev, "failed to duplicate channels\n");
273 return -ENOMEM;
276 ret = press_parse_report(pdev, hsdev,
277 (struct iio_chan_spec *)indio_dev->channels,
278 HID_USAGE_SENSOR_PRESSURE, press_state);
279 if (ret) {
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);
294 if (ret) {
295 dev_err(&pdev->dev, "trigger setup failed\n");
296 goto error_free_dev_mem;
299 ret = iio_device_register(indio_dev);
300 if (ret) {
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);
310 if (ret < 0) {
311 dev_err(&pdev->dev, "callback reg failed\n");
312 goto error_iio_unreg;
315 return ret;
317 error_iio_unreg:
318 iio_device_unregister(indio_dev);
319 error_remove_trigger:
320 hid_sensor_remove_trigger(indio_dev, &press_state->common_attributes);
321 error_free_dev_mem:
322 kfree(indio_dev->channels);
323 return ret;
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);
338 return 0;
341 static const struct platform_device_id hid_press_ids[] = {
343 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
344 .name = "HID-SENSOR-200031",
346 { /* sentinel */ }
348 MODULE_DEVICE_TABLE(platform, hid_press_ids);
350 static struct platform_driver hid_press_platform_driver = {
351 .id_table = hid_press_ids,
352 .driver = {
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");