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/interrupt.h>
11 #include <linux/irq.h>
12 #include <linux/slab.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"
18 
19 struct dev_rot_state {
20 	struct hid_sensor_hub_callbacks callbacks;
21 	struct hid_sensor_common common_attributes;
22 	struct hid_sensor_hub_attribute_info quaternion;
23 	struct {
24 		u32 sampled_vals[4] __aligned(16);
25 		u64 timestamp __aligned(8);
26 	} scan;
27 	int scale_pre_decml;
28 	int scale_post_decml;
29 	int scale_precision;
30 	int value_offset;
31 	s64 timestamp;
32 };
33 
34 /* Channel definitions */
35 static const struct iio_chan_spec dev_rot_channels[] = {
36 	{
37 		.type = IIO_ROT,
38 		.modified = 1,
39 		.channel2 = IIO_MOD_QUATERNION,
40 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
41 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
42 					BIT(IIO_CHAN_INFO_OFFSET) |
43 					BIT(IIO_CHAN_INFO_SCALE) |
44 					BIT(IIO_CHAN_INFO_HYSTERESIS),
45 		.scan_index = 0
46 	},
47 	IIO_CHAN_SOFT_TIMESTAMP(1)
48 };
49 
50 /* Adjust channel real bits based on report descriptor */
51 static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
52 						int size)
53 {
54 	chan->scan_type.sign = 's';
55 	/* Real storage bits will change based on the report desc. */
56 	chan->scan_type.realbits = size * 8;
57 	/* Maximum size of a sample to capture is u32 */
58 	chan->scan_type.storagebits = sizeof(u32) * 8;
59 	chan->scan_type.repeat = 4;
60 }
61 
62 /* Channel read_raw handler */
63 static int dev_rot_read_raw(struct iio_dev *indio_dev,
64 				struct iio_chan_spec const *chan,
65 				int size, int *vals, int *val_len,
66 				long mask)
67 {
68 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
69 	int ret_type;
70 	int i;
71 
72 	vals[0] = 0;
73 	vals[1] = 0;
74 
75 	switch (mask) {
76 	case IIO_CHAN_INFO_RAW:
77 		if (size >= 4) {
78 			for (i = 0; i < 4; ++i)
79 				vals[i] = rot_state->scan.sampled_vals[i];
80 			ret_type = IIO_VAL_INT_MULTIPLE;
81 			*val_len =  4;
82 		} else
83 			ret_type = -EINVAL;
84 		break;
85 	case IIO_CHAN_INFO_SCALE:
86 		vals[0] = rot_state->scale_pre_decml;
87 		vals[1] = rot_state->scale_post_decml;
88 		return rot_state->scale_precision;
89 
90 	case IIO_CHAN_INFO_OFFSET:
91 		*vals = rot_state->value_offset;
92 		return IIO_VAL_INT;
93 
94 	case IIO_CHAN_INFO_SAMP_FREQ:
95 		ret_type = hid_sensor_read_samp_freq_value(
96 			&rot_state->common_attributes, &vals[0], &vals[1]);
97 		break;
98 	case IIO_CHAN_INFO_HYSTERESIS:
99 		ret_type = hid_sensor_read_raw_hyst_value(
100 			&rot_state->common_attributes, &vals[0], &vals[1]);
101 		break;
102 	default:
103 		ret_type = -EINVAL;
104 		break;
105 	}
106 
107 	return ret_type;
108 }
109 
110 /* Channel write_raw handler */
111 static int dev_rot_write_raw(struct iio_dev *indio_dev,
112 			       struct iio_chan_spec const *chan,
113 			       int val,
114 			       int val2,
115 			       long mask)
116 {
117 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
118 	int ret;
119 
120 	switch (mask) {
121 	case IIO_CHAN_INFO_SAMP_FREQ:
122 		ret = hid_sensor_write_samp_freq_value(
123 				&rot_state->common_attributes, val, val2);
124 		break;
125 	case IIO_CHAN_INFO_HYSTERESIS:
126 		ret = hid_sensor_write_raw_hyst_value(
127 				&rot_state->common_attributes, val, val2);
128 		break;
129 	default:
130 		ret = -EINVAL;
131 	}
132 
133 	return ret;
134 }
135 
136 static const struct iio_info dev_rot_info = {
137 	.read_raw_multi = &dev_rot_read_raw,
138 	.write_raw = &dev_rot_write_raw,
139 };
140 
141 /* Callback handler to send event after all samples are received and captured */
142 static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
143 				unsigned usage_id,
144 				void *priv)
145 {
146 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
147 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
148 
149 	dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
150 	if (atomic_read(&rot_state->common_attributes.data_ready)) {
151 		if (!rot_state->timestamp)
152 			rot_state->timestamp = iio_get_time_ns(indio_dev);
153 
154 		iio_push_to_buffers_with_timestamp(indio_dev, &rot_state->scan,
155 						   rot_state->timestamp);
156 
157 		rot_state->timestamp = 0;
158 	}
159 
160 	return 0;
161 }
162 
163 /* Capture samples in local storage */
164 static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
165 				unsigned usage_id,
166 				size_t raw_len, char *raw_data,
167 				void *priv)
168 {
169 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
170 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
171 
172 	if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
173 		memcpy(&rot_state->scan.sampled_vals, raw_data,
174 		       sizeof(rot_state->scan.sampled_vals));
175 
176 		dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
177 			sizeof(rot_state->scan.sampled_vals));
178 	} else if (usage_id == HID_USAGE_SENSOR_TIME_TIMESTAMP) {
179 		rot_state->timestamp = hid_sensor_convert_timestamp(&rot_state->common_attributes,
180 								    *(s64 *)raw_data);
181 	}
182 
183 	return 0;
184 }
185 
186 /* Parse report which is specific to an usage id*/
187 static int dev_rot_parse_report(struct platform_device *pdev,
188 				struct hid_sensor_hub_device *hsdev,
189 				struct iio_chan_spec *channels,
190 				unsigned usage_id,
191 				struct dev_rot_state *st)
192 {
193 	int ret;
194 
195 	ret = sensor_hub_input_get_attribute_info(hsdev,
196 				HID_INPUT_REPORT,
197 				usage_id,
198 				HID_USAGE_SENSOR_ORIENT_QUATERNION,
199 				&st->quaternion);
200 	if (ret)
201 		return ret;
202 
203 	dev_rot_adjust_channel_bit_mask(&channels[0],
204 		st->quaternion.size / 4);
205 
206 	dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
207 		st->quaternion.report_id);
208 
209 	dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
210 				st->quaternion.size);
211 
212 	st->scale_precision = hid_sensor_format_scale(
213 				hsdev->usage,
214 				&st->quaternion,
215 				&st->scale_pre_decml, &st->scale_post_decml);
216 
217 	/* Set Sensitivity field ids, when there is no individual modifier */
218 	if (st->common_attributes.sensitivity.index < 0) {
219 		sensor_hub_input_get_attribute_info(hsdev,
220 			HID_FEATURE_REPORT, usage_id,
221 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
222 			HID_USAGE_SENSOR_DATA_ORIENTATION,
223 			&st->common_attributes.sensitivity);
224 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
225 			st->common_attributes.sensitivity.index,
226 			st->common_attributes.sensitivity.report_id);
227 	}
228 
229 	return 0;
230 }
231 
232 /* Function to initialize the processing for usage id */
233 static int hid_dev_rot_probe(struct platform_device *pdev)
234 {
235 	int ret;
236 	char *name;
237 	struct iio_dev *indio_dev;
238 	struct dev_rot_state *rot_state;
239 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
240 
241 	indio_dev = devm_iio_device_alloc(&pdev->dev,
242 					  sizeof(struct dev_rot_state));
243 	if (indio_dev == NULL)
244 		return -ENOMEM;
245 
246 	platform_set_drvdata(pdev, indio_dev);
247 
248 	rot_state = iio_priv(indio_dev);
249 	rot_state->common_attributes.hsdev = hsdev;
250 	rot_state->common_attributes.pdev = pdev;
251 
252 	switch (hsdev->usage) {
253 	case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
254 		name = "dev_rotation";
255 		break;
256 	case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
257 		name = "relative_orientation";
258 		break;
259 	case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
260 		name = "geomagnetic_orientation";
261 		break;
262 	default:
263 		return -EINVAL;
264 	}
265 
266 	ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
267 				&rot_state->common_attributes);
268 	if (ret) {
269 		dev_err(&pdev->dev, "failed to setup common attributes\n");
270 		return ret;
271 	}
272 
273 	indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
274 					   sizeof(dev_rot_channels),
275 					   GFP_KERNEL);
276 	if (!indio_dev->channels) {
277 		dev_err(&pdev->dev, "failed to duplicate channels\n");
278 		return -ENOMEM;
279 	}
280 
281 	ret = dev_rot_parse_report(pdev, hsdev,
282 				   (struct iio_chan_spec *)indio_dev->channels,
283 					hsdev->usage, rot_state);
284 	if (ret) {
285 		dev_err(&pdev->dev, "failed to setup attributes\n");
286 		return ret;
287 	}
288 
289 	indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
290 	indio_dev->info = &dev_rot_info;
291 	indio_dev->name = name;
292 	indio_dev->modes = INDIO_DIRECT_MODE;
293 
294 	atomic_set(&rot_state->common_attributes.data_ready, 0);
295 
296 	ret = hid_sensor_setup_trigger(indio_dev, name,
297 					&rot_state->common_attributes);
298 	if (ret) {
299 		dev_err(&pdev->dev, "trigger setup failed\n");
300 		return ret;
301 	}
302 
303 	ret = iio_device_register(indio_dev);
304 	if (ret) {
305 		dev_err(&pdev->dev, "device register failed\n");
306 		goto error_remove_trigger;
307 	}
308 
309 	rot_state->callbacks.send_event = dev_rot_proc_event;
310 	rot_state->callbacks.capture_sample = dev_rot_capture_sample;
311 	rot_state->callbacks.pdev = pdev;
312 	ret = sensor_hub_register_callback(hsdev, hsdev->usage,
313 					&rot_state->callbacks);
314 	if (ret) {
315 		dev_err(&pdev->dev, "callback reg failed\n");
316 		goto error_iio_unreg;
317 	}
318 
319 	return 0;
320 
321 error_iio_unreg:
322 	iio_device_unregister(indio_dev);
323 error_remove_trigger:
324 	hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
325 	return ret;
326 }
327 
328 /* Function to deinitialize the processing for usage id */
329 static int hid_dev_rot_remove(struct platform_device *pdev)
330 {
331 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
332 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
333 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
334 
335 	sensor_hub_remove_callback(hsdev, hsdev->usage);
336 	iio_device_unregister(indio_dev);
337 	hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
338 
339 	return 0;
340 }
341 
342 static const struct platform_device_id hid_dev_rot_ids[] = {
343 	{
344 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
345 		.name = "HID-SENSOR-20008a",
346 	},
347 	{
348 		/* Relative orientation(AG) sensor */
349 		.name = "HID-SENSOR-20008e",
350 	},
351 	{
352 		/* Geomagnetic orientation(AM) sensor */
353 		.name = "HID-SENSOR-2000c1",
354 	},
355 	{ /* sentinel */ }
356 };
357 MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
358 
359 static struct platform_driver hid_dev_rot_platform_driver = {
360 	.id_table = hid_dev_rot_ids,
361 	.driver = {
362 		.name	= KBUILD_MODNAME,
363 		.pm     = &hid_sensor_pm_ops,
364 	},
365 	.probe		= hid_dev_rot_probe,
366 	.remove		= hid_dev_rot_remove,
367 };
368 module_platform_driver(hid_dev_rot_platform_driver);
369 
370 MODULE_DESCRIPTION("HID Sensor Device Rotation");
371 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
372 MODULE_LICENSE("GPL");
373