xref: /linux/drivers/iio/orientation/hid-sensor-rotation.c (revision cbae17630954cb89f2bdf5bbadfd3aa81ea67283)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HID Sensors Driver
4  * Copyright (c) 2014, Intel Corporation.
5  */
6 
7 #include <linux/device.h>
8 #include <linux/platform_device.h>
9 #include <linux/module.h>
10 #include <linux/hid-sensor-hub.h>
11 #include <linux/iio/iio.h>
12 #include <linux/iio/sysfs.h>
13 #include <linux/iio/buffer.h>
14 #include "../common/hid-sensors/hid-sensor-trigger.h"
15 
16 struct dev_rot_state {
17 	struct hid_sensor_hub_callbacks callbacks;
18 	struct hid_sensor_common common_attributes;
19 	struct hid_sensor_hub_attribute_info quaternion;
20 	struct {
21 		IIO_DECLARE_QUATERNION(s32, sampled_vals);
22 		/*
23 		 * ABI regression avoidance: There are two copies of the same
24 		 * timestamp in case of userspace depending on broken alignment
25 		 * from older kernels.
26 		 */
27 		aligned_s64 timestamp[2];
28 	} scan;
29 	int scale_pre_decml;
30 	int scale_post_decml;
31 	int scale_precision;
32 	int value_offset;
33 	s64 timestamp;
34 };
35 
36 static const u32 rotation_sensitivity_addresses[] = {
37 	HID_USAGE_SENSOR_DATA_ORIENTATION,
38 	HID_USAGE_SENSOR_ORIENT_QUATERNION,
39 };
40 
41 enum {
42 	DEV_ROT_SCAN_TYPE_16BIT,
43 	DEV_ROT_SCAN_TYPE_32BIT,
44 };
45 
46 static const struct iio_scan_type dev_rot_scan_types[] = {
47 	[DEV_ROT_SCAN_TYPE_16BIT] = {
48 		.sign = 's',
49 		.realbits = 16,
50 		/* Storage bits has to stay 32 to not break userspace. */
51 		.storagebits = 32,
52 		.repeat = 4,
53 	},
54 	[DEV_ROT_SCAN_TYPE_32BIT] = {
55 		.sign = 's',
56 		.realbits = 32,
57 		.storagebits = 32,
58 		.repeat = 4,
59 	},
60 };
61 
62 /* Channel definitions */
63 static const struct iio_chan_spec dev_rot_channels[] = {
64 	{
65 		.type = IIO_ROT,
66 		.modified = 1,
67 		.channel2 = IIO_MOD_QUATERNION,
68 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
69 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
70 					BIT(IIO_CHAN_INFO_OFFSET) |
71 					BIT(IIO_CHAN_INFO_SCALE) |
72 					BIT(IIO_CHAN_INFO_HYSTERESIS),
73 		.scan_index = 0,
74 		.has_ext_scan_type = 1,
75 		.ext_scan_type = dev_rot_scan_types,
76 		.num_ext_scan_type = ARRAY_SIZE(dev_rot_scan_types),
77 	},
78 	IIO_CHAN_SOFT_TIMESTAMP(1)
79 };
80 
81 /* Channel read_raw handler */
82 static int dev_rot_read_raw(struct iio_dev *indio_dev,
83 				struct iio_chan_spec const *chan,
84 				int size, int *vals, int *val_len,
85 				long mask)
86 {
87 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
88 	struct hid_sensor_hub_device *hsdev = rot_state->common_attributes.hsdev;
89 	struct hid_sensor_hub_attribute_info *info = &rot_state->quaternion;
90 	u32 usage_id = HID_USAGE_SENSOR_ORIENT_QUATERNION;
91 	union {
92 		s16 val16[4];
93 		s32 val32[4];
94 	} raw_buf;
95 	int ret_type;
96 	int i;
97 
98 	vals[0] = 0;
99 	vals[1] = 0;
100 
101 	switch (mask) {
102 	case IIO_CHAN_INFO_RAW:
103 		if (size >= 4) {
104 			if (info->size <= 0 || info->size > sizeof(raw_buf))
105 				return -EINVAL;
106 
107 			hid_sensor_power_state(&rot_state->common_attributes, true);
108 
109 			ret_type = sensor_hub_input_attr_read_values(hsdev,
110 								     hsdev->usage,
111 								     usage_id,
112 								     info->report_id,
113 								     SENSOR_HUB_SYNC,
114 								     info->size,
115 								     (u8 *)&raw_buf);
116 
117 			hid_sensor_power_state(&rot_state->common_attributes, false);
118 
119 			if (ret_type < 0)
120 				return ret_type;
121 
122 			switch (info->size) {
123 			case sizeof(raw_buf.val16):
124 				for (i = 0; i < ARRAY_SIZE(raw_buf.val16); i++)
125 					vals[i] = raw_buf.val16[i];
126 				break;
127 			case sizeof(raw_buf.val32):
128 				for (i = 0; i < ARRAY_SIZE(raw_buf.val32); i++)
129 					vals[i] = raw_buf.val32[i];
130 				break;
131 			default:
132 				return -EINVAL;
133 			}
134 
135 			ret_type = IIO_VAL_INT_MULTIPLE;
136 			*val_len =  4;
137 		} else
138 			ret_type = -EINVAL;
139 		break;
140 	case IIO_CHAN_INFO_SCALE:
141 		vals[0] = rot_state->scale_pre_decml;
142 		vals[1] = rot_state->scale_post_decml;
143 		return rot_state->scale_precision;
144 
145 	case IIO_CHAN_INFO_OFFSET:
146 		*vals = rot_state->value_offset;
147 		return IIO_VAL_INT;
148 
149 	case IIO_CHAN_INFO_SAMP_FREQ:
150 		ret_type = hid_sensor_read_samp_freq_value(
151 			&rot_state->common_attributes, &vals[0], &vals[1]);
152 		break;
153 	case IIO_CHAN_INFO_HYSTERESIS:
154 		ret_type = hid_sensor_read_raw_hyst_value(
155 			&rot_state->common_attributes, &vals[0], &vals[1]);
156 		break;
157 	default:
158 		ret_type = -EINVAL;
159 		break;
160 	}
161 
162 	return ret_type;
163 }
164 
165 /* Channel write_raw handler */
166 static int dev_rot_write_raw(struct iio_dev *indio_dev,
167 			       struct iio_chan_spec const *chan,
168 			       int val,
169 			       int val2,
170 			       long mask)
171 {
172 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
173 	int ret;
174 
175 	switch (mask) {
176 	case IIO_CHAN_INFO_SAMP_FREQ:
177 		ret = hid_sensor_write_samp_freq_value(
178 				&rot_state->common_attributes, val, val2);
179 		break;
180 	case IIO_CHAN_INFO_HYSTERESIS:
181 		ret = hid_sensor_write_raw_hyst_value(
182 				&rot_state->common_attributes, val, val2);
183 		break;
184 	default:
185 		ret = -EINVAL;
186 	}
187 
188 	return ret;
189 }
190 
191 static int dev_rot_get_current_scan_type(const struct iio_dev *indio_dev,
192 					 const struct iio_chan_spec *chan)
193 {
194 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
195 
196 	switch (rot_state->quaternion.size / 4) {
197 	case sizeof(s16):
198 		return DEV_ROT_SCAN_TYPE_16BIT;
199 	case sizeof(s32):
200 		return DEV_ROT_SCAN_TYPE_32BIT;
201 	default:
202 		return -EINVAL;
203 	}
204 }
205 
206 static const struct iio_info dev_rot_info = {
207 	.read_raw_multi = &dev_rot_read_raw,
208 	.write_raw = &dev_rot_write_raw,
209 	.get_current_scan_type = &dev_rot_get_current_scan_type,
210 };
211 
212 /* Callback handler to send event after all samples are received and captured */
213 static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
214 				unsigned usage_id,
215 				void *priv)
216 {
217 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
218 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
219 
220 	dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
221 	if (atomic_read(&rot_state->common_attributes.data_ready)) {
222 		if (!rot_state->timestamp)
223 			rot_state->timestamp = iio_get_time_ns(indio_dev);
224 
225 		/*
226 		 * ABI regression avoidance: IIO previously had an incorrect
227 		 * implementation of iio_push_to_buffers_with_timestamp() that
228 		 * put the timestamp in the last 8 bytes of the buffer, which
229 		 * was incorrect according to the IIO ABI. To avoid breaking
230 		 * userspace that may be depending on this broken behavior, we
231 		 * put the timestamp in both the correct place [0] and the old
232 		 * incorrect place [1].
233 		 */
234 		rot_state->scan.timestamp[0] = rot_state->timestamp;
235 		rot_state->scan.timestamp[1] = rot_state->timestamp;
236 
237 		iio_push_to_buffers(indio_dev, &rot_state->scan);
238 
239 		rot_state->timestamp = 0;
240 	}
241 
242 	return 0;
243 }
244 
245 /* Capture samples in local storage */
246 static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
247 				unsigned usage_id,
248 				size_t raw_len, char *raw_data,
249 				void *priv)
250 {
251 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
252 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
253 
254 	if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
255 		if (raw_len / 4 == sizeof(s16)) {
256 			rot_state->scan.sampled_vals[0] = ((s16 *)raw_data)[0];
257 			rot_state->scan.sampled_vals[1] = ((s16 *)raw_data)[1];
258 			rot_state->scan.sampled_vals[2] = ((s16 *)raw_data)[2];
259 			rot_state->scan.sampled_vals[3] = ((s16 *)raw_data)[3];
260 		} else {
261 			memcpy(&rot_state->scan.sampled_vals, raw_data,
262 			       sizeof(rot_state->scan.sampled_vals));
263 		}
264 
265 		dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
266 			sizeof(rot_state->scan.sampled_vals));
267 	} else if (usage_id == HID_USAGE_SENSOR_TIME_TIMESTAMP) {
268 		rot_state->timestamp = hid_sensor_convert_timestamp(&rot_state->common_attributes,
269 								    *(s64 *)raw_data);
270 	}
271 
272 	return 0;
273 }
274 
275 /* Parse report which is specific to an usage id*/
276 static int dev_rot_parse_report(struct platform_device *pdev,
277 				struct hid_sensor_hub_device *hsdev,
278 				unsigned usage_id,
279 				struct dev_rot_state *st)
280 {
281 	int ret;
282 
283 	ret = sensor_hub_input_get_attribute_info(hsdev,
284 				HID_INPUT_REPORT,
285 				usage_id,
286 				HID_USAGE_SENSOR_ORIENT_QUATERNION,
287 				&st->quaternion);
288 	if (ret)
289 		return ret;
290 
291 	dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
292 		st->quaternion.report_id);
293 
294 	dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
295 				st->quaternion.size);
296 
297 	st->scale_precision = hid_sensor_format_scale(
298 				hsdev->usage,
299 				&st->quaternion,
300 				&st->scale_pre_decml, &st->scale_post_decml);
301 
302 	return 0;
303 }
304 
305 /* Function to initialize the processing for usage id */
306 static int hid_dev_rot_probe(struct platform_device *pdev)
307 {
308 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
309 	int ret;
310 	char *name;
311 	struct iio_dev *indio_dev;
312 	struct dev_rot_state *rot_state;
313 
314 	indio_dev = devm_iio_device_alloc(&pdev->dev,
315 					  sizeof(struct dev_rot_state));
316 	if (indio_dev == NULL)
317 		return -ENOMEM;
318 
319 	platform_set_drvdata(pdev, indio_dev);
320 
321 	rot_state = iio_priv(indio_dev);
322 	rot_state->common_attributes.hsdev = hsdev;
323 	rot_state->common_attributes.pdev = pdev;
324 
325 	switch (hsdev->usage) {
326 	case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
327 		name = "dev_rotation";
328 		break;
329 	case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
330 		name = "relative_orientation";
331 		break;
332 	case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
333 		name = "geomagnetic_orientation";
334 		break;
335 	default:
336 		return -EINVAL;
337 	}
338 
339 	ret = hid_sensor_parse_common_attributes(hsdev,
340 						 hsdev->usage,
341 						 &rot_state->common_attributes,
342 						 rotation_sensitivity_addresses,
343 						 ARRAY_SIZE(rotation_sensitivity_addresses));
344 	if (ret) {
345 		dev_err(&pdev->dev, "failed to setup common attributes\n");
346 		return ret;
347 	}
348 
349 	ret = dev_rot_parse_report(pdev, hsdev, hsdev->usage, rot_state);
350 	if (ret) {
351 		dev_err(&pdev->dev, "failed to setup attributes\n");
352 		return ret;
353 	}
354 
355 	indio_dev->channels = dev_rot_channels;
356 	indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
357 	indio_dev->info = &dev_rot_info;
358 	indio_dev->name = name;
359 	indio_dev->modes = INDIO_DIRECT_MODE;
360 
361 	atomic_set(&rot_state->common_attributes.data_ready, 0);
362 
363 	ret = hid_sensor_setup_trigger(indio_dev, name,
364 					&rot_state->common_attributes);
365 	if (ret) {
366 		dev_err(&pdev->dev, "trigger setup failed\n");
367 		return ret;
368 	}
369 
370 	ret = iio_device_register(indio_dev);
371 	if (ret) {
372 		dev_err(&pdev->dev, "device register failed\n");
373 		goto error_remove_trigger;
374 	}
375 
376 	rot_state->callbacks.send_event = dev_rot_proc_event;
377 	rot_state->callbacks.capture_sample = dev_rot_capture_sample;
378 	rot_state->callbacks.pdev = pdev;
379 	ret = sensor_hub_register_callback(hsdev, hsdev->usage,
380 					&rot_state->callbacks);
381 	if (ret) {
382 		dev_err(&pdev->dev, "callback reg failed\n");
383 		goto error_iio_unreg;
384 	}
385 
386 	return 0;
387 
388 error_iio_unreg:
389 	iio_device_unregister(indio_dev);
390 error_remove_trigger:
391 	hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
392 	return ret;
393 }
394 
395 /* Function to deinitialize the processing for usage id */
396 static void hid_dev_rot_remove(struct platform_device *pdev)
397 {
398 	struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
399 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
400 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
401 
402 	sensor_hub_remove_callback(hsdev, hsdev->usage);
403 	iio_device_unregister(indio_dev);
404 	hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
405 }
406 
407 static const struct platform_device_id hid_dev_rot_ids[] = {
408 	{
409 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
410 		.name = "HID-SENSOR-20008a",
411 	},
412 	{
413 		/* Relative orientation(AG) sensor */
414 		.name = "HID-SENSOR-20008e",
415 	},
416 	{
417 		/* Geomagnetic orientation(AM) sensor */
418 		.name = "HID-SENSOR-2000c1",
419 	},
420 	{ }
421 };
422 MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
423 
424 static struct platform_driver hid_dev_rot_platform_driver = {
425 	.id_table = hid_dev_rot_ids,
426 	.driver = {
427 		.name	= KBUILD_MODNAME,
428 		.pm     = &hid_sensor_pm_ops,
429 	},
430 	.probe		= hid_dev_rot_probe,
431 	.remove		= hid_dev_rot_remove,
432 };
433 module_platform_driver(hid_dev_rot_platform_driver);
434 
435 MODULE_DESCRIPTION("HID Sensor Device Rotation");
436 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
437 MODULE_LICENSE("GPL");
438 MODULE_IMPORT_NS("IIO_HID");
439