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