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