xref: /openbmc/linux/drivers/iio/pressure/hid-sensor-press.c (revision fb226ae750ea09020dbde0ac8769c86820bcb6c0)
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/mod_devicetable.h>
10 #include <linux/slab.h>
11 #include <linux/hid-sensor-hub.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/buffer.h>
14 #include "../common/hid-sensors/hid-sensor-trigger.h"
15 
16 #define CHANNEL_SCAN_INDEX_PRESSURE 0
17 
18 struct press_state {
19 	struct hid_sensor_hub_callbacks callbacks;
20 	struct hid_sensor_common common_attributes;
21 	struct hid_sensor_hub_attribute_info press_attr;
22 	u32 press_data;
23 	int scale_pre_decml;
24 	int scale_post_decml;
25 	int scale_precision;
26 	int value_offset;
27 };
28 
29 static const u32 press_sensitivity_addresses[] = {
30 	HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE,
31 	HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE
32 };
33 
34 /* Channel definitions */
35 static const struct iio_chan_spec press_channels[] = {
36 	{
37 		.type = IIO_PRESSURE,
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,
44 	}
45 };
46 
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)
50 {
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;
56 }
57 
58 /* Channel read_raw handler */
59 static int press_read_raw(struct iio_dev *indio_dev,
60 			      struct iio_chan_spec const *chan,
61 			      int *val, int *val2,
62 			      long mask)
63 {
64 	struct press_state *press_state = iio_priv(indio_dev);
65 	int report_id = -1;
66 	u32 address;
67 	int ret_type;
68 	s32 min;
69 
70 	*val = 0;
71 	*val2 = 0;
72 	switch (mask) {
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;
79 			break;
80 		default:
81 			report_id = -1;
82 			break;
83 		}
84 		if (report_id >= 0) {
85 			hid_sensor_power_state(&press_state->common_attributes,
86 						true);
87 			*val = sensor_hub_input_attr_get_raw_value(
88 				press_state->common_attributes.hsdev,
89 				HID_USAGE_SENSOR_PRESSURE, address,
90 				report_id,
91 				SENSOR_HUB_SYNC,
92 				min < 0);
93 			hid_sensor_power_state(&press_state->common_attributes,
94 						false);
95 		} else {
96 			*val = 0;
97 			return -EINVAL;
98 		}
99 		ret_type = IIO_VAL_INT;
100 		break;
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;
105 		break;
106 	case IIO_CHAN_INFO_OFFSET:
107 		*val = press_state->value_offset;
108 		ret_type = IIO_VAL_INT;
109 		break;
110 	case IIO_CHAN_INFO_SAMP_FREQ:
111 		ret_type = hid_sensor_read_samp_freq_value(
112 				&press_state->common_attributes, val, val2);
113 		break;
114 	case IIO_CHAN_INFO_HYSTERESIS:
115 		ret_type = hid_sensor_read_raw_hyst_value(
116 				&press_state->common_attributes, val, val2);
117 		break;
118 	default:
119 		ret_type = -EINVAL;
120 		break;
121 	}
122 
123 	return ret_type;
124 }
125 
126 /* Channel write_raw handler */
127 static int press_write_raw(struct iio_dev *indio_dev,
128 			       struct iio_chan_spec const *chan,
129 			       int val,
130 			       int val2,
131 			       long mask)
132 {
133 	struct press_state *press_state = iio_priv(indio_dev);
134 	int ret = 0;
135 
136 	switch (mask) {
137 	case IIO_CHAN_INFO_SAMP_FREQ:
138 		ret = hid_sensor_write_samp_freq_value(
139 				&press_state->common_attributes, val, val2);
140 		break;
141 	case IIO_CHAN_INFO_HYSTERESIS:
142 		ret = hid_sensor_write_raw_hyst_value(
143 				&press_state->common_attributes, val, val2);
144 		break;
145 	default:
146 		ret = -EINVAL;
147 	}
148 
149 	return ret;
150 }
151 
152 static const struct iio_info press_info = {
153 	.read_raw = &press_read_raw,
154 	.write_raw = &press_write_raw,
155 };
156 
157 /* Function to push data to buffer */
158 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
159 					int len)
160 {
161 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
162 	iio_push_to_buffers(indio_dev, data);
163 }
164 
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,
167 				unsigned usage_id,
168 				void *priv)
169 {
170 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
171 	struct press_state *press_state = iio_priv(indio_dev);
172 
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));
178 
179 	return 0;
180 }
181 
182 /* Capture samples in local storage */
183 static int press_capture_sample(struct hid_sensor_hub_device *hsdev,
184 				unsigned usage_id,
185 				size_t raw_len, char *raw_data,
186 				void *priv)
187 {
188 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
189 	struct press_state *press_state = iio_priv(indio_dev);
190 	int ret = -EINVAL;
191 
192 	switch (usage_id) {
193 	case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE:
194 		press_state->press_data = *(u32 *)raw_data;
195 		ret = 0;
196 		break;
197 	default:
198 		break;
199 	}
200 
201 	return ret;
202 }
203 
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,
208 				unsigned usage_id,
209 				struct press_state *st)
210 {
211 	int ret;
212 
213 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
214 			usage_id,
215 			HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE,
216 			&st->press_attr);
217 	if (ret < 0)
218 		return ret;
219 	press_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESSURE,
220 					st->press_attr.size);
221 
222 	dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
223 			st->press_attr.report_id);
224 
225 	st->scale_precision = hid_sensor_format_scale(
226 				HID_USAGE_SENSOR_PRESSURE,
227 				&st->press_attr,
228 				&st->scale_pre_decml, &st->scale_post_decml);
229 
230 	return ret;
231 }
232 
233 /* Function to initialize the processing for usage id */
234 static int hid_press_probe(struct platform_device *pdev)
235 {
236 	int ret = 0;
237 	static const char *name = "press";
238 	struct iio_dev *indio_dev;
239 	struct press_state *press_state;
240 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
241 
242 	indio_dev = devm_iio_device_alloc(&pdev->dev,
243 				sizeof(struct press_state));
244 	if (!indio_dev)
245 		return -ENOMEM;
246 	platform_set_drvdata(pdev, indio_dev);
247 
248 	press_state = iio_priv(indio_dev);
249 	press_state->common_attributes.hsdev = hsdev;
250 	press_state->common_attributes.pdev = pdev;
251 
252 	ret = hid_sensor_parse_common_attributes(hsdev,
253 					HID_USAGE_SENSOR_PRESSURE,
254 					&press_state->common_attributes,
255 					press_sensitivity_addresses,
256 					ARRAY_SIZE(press_sensitivity_addresses));
257 	if (ret) {
258 		dev_err(&pdev->dev, "failed to setup common attributes\n");
259 		return ret;
260 	}
261 
262 	indio_dev->channels = kmemdup(press_channels, sizeof(press_channels),
263 				      GFP_KERNEL);
264 	if (!indio_dev->channels) {
265 		dev_err(&pdev->dev, "failed to duplicate channels\n");
266 		return -ENOMEM;
267 	}
268 
269 	ret = press_parse_report(pdev, hsdev,
270 				 (struct iio_chan_spec *)indio_dev->channels,
271 				 HID_USAGE_SENSOR_PRESSURE, press_state);
272 	if (ret) {
273 		dev_err(&pdev->dev, "failed to setup attributes\n");
274 		goto error_free_dev_mem;
275 	}
276 
277 	indio_dev->num_channels =
278 				ARRAY_SIZE(press_channels);
279 	indio_dev->info = &press_info;
280 	indio_dev->name = name;
281 	indio_dev->modes = INDIO_DIRECT_MODE;
282 
283 	atomic_set(&press_state->common_attributes.data_ready, 0);
284 
285 	ret = hid_sensor_setup_trigger(indio_dev, name,
286 				&press_state->common_attributes);
287 	if (ret) {
288 		dev_err(&pdev->dev, "trigger setup failed\n");
289 		goto error_free_dev_mem;
290 	}
291 
292 	ret = iio_device_register(indio_dev);
293 	if (ret) {
294 		dev_err(&pdev->dev, "device register failed\n");
295 		goto error_remove_trigger;
296 	}
297 
298 	press_state->callbacks.send_event = press_proc_event;
299 	press_state->callbacks.capture_sample = press_capture_sample;
300 	press_state->callbacks.pdev = pdev;
301 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PRESSURE,
302 					&press_state->callbacks);
303 	if (ret < 0) {
304 		dev_err(&pdev->dev, "callback reg failed\n");
305 		goto error_iio_unreg;
306 	}
307 
308 	return ret;
309 
310 error_iio_unreg:
311 	iio_device_unregister(indio_dev);
312 error_remove_trigger:
313 	hid_sensor_remove_trigger(indio_dev, &press_state->common_attributes);
314 error_free_dev_mem:
315 	kfree(indio_dev->channels);
316 	return ret;
317 }
318 
319 /* Function to deinitialize the processing for usage id */
320 static int hid_press_remove(struct platform_device *pdev)
321 {
322 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
323 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
324 	struct press_state *press_state = iio_priv(indio_dev);
325 
326 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PRESSURE);
327 	iio_device_unregister(indio_dev);
328 	hid_sensor_remove_trigger(indio_dev, &press_state->common_attributes);
329 	kfree(indio_dev->channels);
330 
331 	return 0;
332 }
333 
334 static const struct platform_device_id hid_press_ids[] = {
335 	{
336 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
337 		.name = "HID-SENSOR-200031",
338 	},
339 	{ /* sentinel */ }
340 };
341 MODULE_DEVICE_TABLE(platform, hid_press_ids);
342 
343 static struct platform_driver hid_press_platform_driver = {
344 	.id_table = hid_press_ids,
345 	.driver = {
346 		.name	= KBUILD_MODNAME,
347 		.pm	= &hid_sensor_pm_ops,
348 	},
349 	.probe		= hid_press_probe,
350 	.remove		= hid_press_remove,
351 };
352 module_platform_driver(hid_press_platform_driver);
353 
354 MODULE_DESCRIPTION("HID Sensor Pressure");
355 MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
356 MODULE_LICENSE("GPL");
357 MODULE_IMPORT_NS(IIO_HID);
358