xref: /linux/drivers/iio/orientation/hid-sensor-incl-3d.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
1 /*
2  * HID Sensors Driver
3  * Copyright (c) 2013, 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  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.
16  *
17  */
18 
19 #include <linux/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
25 #include <linux/delay.h>
26 #include <linux/hid-sensor-hub.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger_consumer.h>
31 #include <linux/iio/triggered_buffer.h>
32 #include "../common/hid-sensors/hid-sensor-trigger.h"
33 
34 enum incl_3d_channel {
35 	CHANNEL_SCAN_INDEX_X,
36 	CHANNEL_SCAN_INDEX_Y,
37 	CHANNEL_SCAN_INDEX_Z,
38 	INCLI_3D_CHANNEL_MAX,
39 };
40 
41 struct incl_3d_state {
42 	struct hid_sensor_hub_callbacks callbacks;
43 	struct hid_sensor_common common_attributes;
44 	struct hid_sensor_hub_attribute_info incl[INCLI_3D_CHANNEL_MAX];
45 	u32 incl_val[INCLI_3D_CHANNEL_MAX];
46 	int scale_pre_decml;
47 	int scale_post_decml;
48 	int scale_precision;
49 	int value_offset;
50 };
51 
52 static const u32 incl_3d_addresses[INCLI_3D_CHANNEL_MAX] = {
53 	HID_USAGE_SENSOR_ORIENT_TILT_X,
54 	HID_USAGE_SENSOR_ORIENT_TILT_Y,
55 	HID_USAGE_SENSOR_ORIENT_TILT_Z
56 };
57 
58 /* Channel definitions */
59 static const struct iio_chan_spec incl_3d_channels[] = {
60 	{
61 		.type = IIO_INCLI,
62 		.modified = 1,
63 		.channel2 = IIO_MOD_X,
64 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
65 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
66 		BIT(IIO_CHAN_INFO_SCALE) |
67 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
68 		BIT(IIO_CHAN_INFO_HYSTERESIS),
69 		.scan_index = CHANNEL_SCAN_INDEX_X,
70 	}, {
71 		.type = IIO_INCLI,
72 		.modified = 1,
73 		.channel2 = IIO_MOD_Y,
74 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
75 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
76 		BIT(IIO_CHAN_INFO_SCALE) |
77 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
78 		BIT(IIO_CHAN_INFO_HYSTERESIS),
79 		.scan_index = CHANNEL_SCAN_INDEX_Y,
80 	}, {
81 		.type = IIO_INCLI,
82 		.modified = 1,
83 		.channel2 = IIO_MOD_Z,
84 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
85 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
86 		BIT(IIO_CHAN_INFO_SCALE) |
87 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
88 		BIT(IIO_CHAN_INFO_HYSTERESIS),
89 		.scan_index = CHANNEL_SCAN_INDEX_Z,
90 	}
91 };
92 
93 /* Adjust channel real bits based on report descriptor */
94 static void incl_3d_adjust_channel_bit_mask(struct iio_chan_spec *chan,
95 						int size)
96 {
97 	chan->scan_type.sign = 's';
98 	/* Real storage bits will change based on the report desc. */
99 	chan->scan_type.realbits = size * 8;
100 	/* Maximum size of a sample to capture is u32 */
101 	chan->scan_type.storagebits = sizeof(u32) * 8;
102 }
103 
104 /* Channel read_raw handler */
105 static int incl_3d_read_raw(struct iio_dev *indio_dev,
106 			      struct iio_chan_spec const *chan,
107 			      int *val, int *val2,
108 			      long mask)
109 {
110 	struct incl_3d_state *incl_state = iio_priv(indio_dev);
111 	int report_id = -1;
112 	u32 address;
113 	int ret_type;
114 	s32 min;
115 
116 	*val = 0;
117 	*val2 = 0;
118 	switch (mask) {
119 	case IIO_CHAN_INFO_RAW:
120 		hid_sensor_power_state(&incl_state->common_attributes, true);
121 		report_id = incl_state->incl[chan->scan_index].report_id;
122 		min = incl_state->incl[chan->scan_index].logical_minimum;
123 		address = incl_3d_addresses[chan->scan_index];
124 		if (report_id >= 0)
125 			*val = sensor_hub_input_attr_get_raw_value(
126 				incl_state->common_attributes.hsdev,
127 				HID_USAGE_SENSOR_INCLINOMETER_3D, address,
128 				report_id,
129 				SENSOR_HUB_SYNC,
130 				min < 0);
131 		else {
132 			hid_sensor_power_state(&incl_state->common_attributes,
133 						false);
134 			return -EINVAL;
135 		}
136 		hid_sensor_power_state(&incl_state->common_attributes, false);
137 		ret_type = IIO_VAL_INT;
138 		break;
139 	case IIO_CHAN_INFO_SCALE:
140 		*val = incl_state->scale_pre_decml;
141 		*val2 = incl_state->scale_post_decml;
142 		ret_type = incl_state->scale_precision;
143 		break;
144 	case IIO_CHAN_INFO_OFFSET:
145 		*val = incl_state->value_offset;
146 		ret_type = IIO_VAL_INT;
147 		break;
148 	case IIO_CHAN_INFO_SAMP_FREQ:
149 		ret_type = hid_sensor_read_samp_freq_value(
150 			&incl_state->common_attributes, val, val2);
151 		break;
152 	case IIO_CHAN_INFO_HYSTERESIS:
153 		ret_type = hid_sensor_read_raw_hyst_value(
154 			&incl_state->common_attributes, val, val2);
155 		break;
156 	default:
157 		ret_type = -EINVAL;
158 		break;
159 	}
160 
161 	return ret_type;
162 }
163 
164 /* Channel write_raw handler */
165 static int incl_3d_write_raw(struct iio_dev *indio_dev,
166 			       struct iio_chan_spec const *chan,
167 			       int val,
168 			       int val2,
169 			       long mask)
170 {
171 	struct incl_3d_state *incl_state = iio_priv(indio_dev);
172 	int ret;
173 
174 	switch (mask) {
175 	case IIO_CHAN_INFO_SAMP_FREQ:
176 		ret = hid_sensor_write_samp_freq_value(
177 				&incl_state->common_attributes, val, val2);
178 		break;
179 	case IIO_CHAN_INFO_HYSTERESIS:
180 		ret = hid_sensor_write_raw_hyst_value(
181 				&incl_state->common_attributes, val, val2);
182 		break;
183 	default:
184 		ret = -EINVAL;
185 	}
186 
187 	return ret;
188 }
189 
190 static const struct iio_info incl_3d_info = {
191 	.read_raw = &incl_3d_read_raw,
192 	.write_raw = &incl_3d_write_raw,
193 };
194 
195 /* Function to push data to buffer */
196 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
197 {
198 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
199 	iio_push_to_buffers(indio_dev, (u8 *)data);
200 }
201 
202 /* Callback handler to send event after all samples are received and captured */
203 static int incl_3d_proc_event(struct hid_sensor_hub_device *hsdev,
204 				unsigned usage_id,
205 				void *priv)
206 {
207 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
208 	struct incl_3d_state *incl_state = iio_priv(indio_dev);
209 
210 	dev_dbg(&indio_dev->dev, "incl_3d_proc_event\n");
211 	if (atomic_read(&incl_state->common_attributes.data_ready))
212 		hid_sensor_push_data(indio_dev,
213 				(u8 *)incl_state->incl_val,
214 				sizeof(incl_state->incl_val));
215 
216 	return 0;
217 }
218 
219 /* Capture samples in local storage */
220 static int incl_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
221 				unsigned usage_id,
222 				size_t raw_len, char *raw_data,
223 				void *priv)
224 {
225 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
226 	struct incl_3d_state *incl_state = iio_priv(indio_dev);
227 	int ret = 0;
228 
229 	switch (usage_id) {
230 	case HID_USAGE_SENSOR_ORIENT_TILT_X:
231 		incl_state->incl_val[CHANNEL_SCAN_INDEX_X] = *(u32 *)raw_data;
232 	break;
233 	case HID_USAGE_SENSOR_ORIENT_TILT_Y:
234 		incl_state->incl_val[CHANNEL_SCAN_INDEX_Y] = *(u32 *)raw_data;
235 	break;
236 	case HID_USAGE_SENSOR_ORIENT_TILT_Z:
237 		incl_state->incl_val[CHANNEL_SCAN_INDEX_Z] = *(u32 *)raw_data;
238 	break;
239 	default:
240 		ret = -EINVAL;
241 		break;
242 	}
243 
244 	return ret;
245 }
246 
247 /* Parse report which is specific to an usage id*/
248 static int incl_3d_parse_report(struct platform_device *pdev,
249 				struct hid_sensor_hub_device *hsdev,
250 				struct iio_chan_spec *channels,
251 				unsigned usage_id,
252 				struct incl_3d_state *st)
253 {
254 	int ret;
255 
256 	ret = sensor_hub_input_get_attribute_info(hsdev,
257 				HID_INPUT_REPORT,
258 				usage_id,
259 				HID_USAGE_SENSOR_ORIENT_TILT_X,
260 				&st->incl[CHANNEL_SCAN_INDEX_X]);
261 	if (ret)
262 		return ret;
263 	incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_X],
264 				st->incl[CHANNEL_SCAN_INDEX_X].size);
265 
266 	ret = sensor_hub_input_get_attribute_info(hsdev,
267 				HID_INPUT_REPORT,
268 				usage_id,
269 				HID_USAGE_SENSOR_ORIENT_TILT_Y,
270 				&st->incl[CHANNEL_SCAN_INDEX_Y]);
271 	if (ret)
272 		return ret;
273 	incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Y],
274 				st->incl[CHANNEL_SCAN_INDEX_Y].size);
275 
276 	ret = sensor_hub_input_get_attribute_info(hsdev,
277 				HID_INPUT_REPORT,
278 				usage_id,
279 				HID_USAGE_SENSOR_ORIENT_TILT_Z,
280 				&st->incl[CHANNEL_SCAN_INDEX_Z]);
281 	if (ret)
282 		return ret;
283 	incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Z],
284 				st->incl[CHANNEL_SCAN_INDEX_Z].size);
285 
286 	dev_dbg(&pdev->dev, "incl_3d %x:%x, %x:%x, %x:%x\n",
287 			st->incl[0].index,
288 			st->incl[0].report_id,
289 			st->incl[1].index, st->incl[1].report_id,
290 			st->incl[2].index, st->incl[2].report_id);
291 
292 	st->scale_precision = hid_sensor_format_scale(
293 				HID_USAGE_SENSOR_INCLINOMETER_3D,
294 				&st->incl[CHANNEL_SCAN_INDEX_X],
295 				&st->scale_pre_decml, &st->scale_post_decml);
296 
297 	/* Set Sensitivity field ids, when there is no individual modifier */
298 	if (st->common_attributes.sensitivity.index < 0) {
299 		sensor_hub_input_get_attribute_info(hsdev,
300 			HID_FEATURE_REPORT, usage_id,
301 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
302 			HID_USAGE_SENSOR_DATA_ORIENTATION,
303 			&st->common_attributes.sensitivity);
304 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
305 			st->common_attributes.sensitivity.index,
306 			st->common_attributes.sensitivity.report_id);
307 	}
308 	return ret;
309 }
310 
311 /* Function to initialize the processing for usage id */
312 static int hid_incl_3d_probe(struct platform_device *pdev)
313 {
314 	int ret;
315 	static char *name = "incli_3d";
316 	struct iio_dev *indio_dev;
317 	struct incl_3d_state *incl_state;
318 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
319 
320 	indio_dev = devm_iio_device_alloc(&pdev->dev,
321 					  sizeof(struct incl_3d_state));
322 	if (indio_dev == NULL)
323 		return -ENOMEM;
324 
325 	platform_set_drvdata(pdev, indio_dev);
326 
327 	incl_state = iio_priv(indio_dev);
328 	incl_state->common_attributes.hsdev = hsdev;
329 	incl_state->common_attributes.pdev = pdev;
330 
331 	ret = hid_sensor_parse_common_attributes(hsdev,
332 				HID_USAGE_SENSOR_INCLINOMETER_3D,
333 				&incl_state->common_attributes);
334 	if (ret) {
335 		dev_err(&pdev->dev, "failed to setup common attributes\n");
336 		return ret;
337 	}
338 
339 	indio_dev->channels = kmemdup(incl_3d_channels,
340 				      sizeof(incl_3d_channels), GFP_KERNEL);
341 	if (!indio_dev->channels) {
342 		dev_err(&pdev->dev, "failed to duplicate channels\n");
343 		return -ENOMEM;
344 	}
345 
346 	ret = incl_3d_parse_report(pdev, hsdev,
347 				   (struct iio_chan_spec *)indio_dev->channels,
348 				   HID_USAGE_SENSOR_INCLINOMETER_3D,
349 				   incl_state);
350 	if (ret) {
351 		dev_err(&pdev->dev, "failed to setup attributes\n");
352 		goto error_free_dev_mem;
353 	}
354 
355 	indio_dev->num_channels = ARRAY_SIZE(incl_3d_channels);
356 	indio_dev->dev.parent = &pdev->dev;
357 	indio_dev->info = &incl_3d_info;
358 	indio_dev->name = name;
359 	indio_dev->modes = INDIO_DIRECT_MODE;
360 
361 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
362 		NULL, NULL);
363 	if (ret) {
364 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
365 		goto error_free_dev_mem;
366 	}
367 	atomic_set(&incl_state->common_attributes.data_ready, 0);
368 	ret = hid_sensor_setup_trigger(indio_dev, name,
369 					&incl_state->common_attributes);
370 	if (ret) {
371 		dev_err(&pdev->dev, "trigger setup failed\n");
372 		goto error_unreg_buffer_funcs;
373 	}
374 
375 	ret = iio_device_register(indio_dev);
376 	if (ret) {
377 		dev_err(&pdev->dev, "device register failed\n");
378 		goto error_remove_trigger;
379 	}
380 
381 	incl_state->callbacks.send_event = incl_3d_proc_event;
382 	incl_state->callbacks.capture_sample = incl_3d_capture_sample;
383 	incl_state->callbacks.pdev = pdev;
384 	ret = sensor_hub_register_callback(hsdev,
385 					HID_USAGE_SENSOR_INCLINOMETER_3D,
386 					&incl_state->callbacks);
387 	if (ret) {
388 		dev_err(&pdev->dev, "callback reg failed\n");
389 		goto error_iio_unreg;
390 	}
391 
392 	return 0;
393 
394 error_iio_unreg:
395 	iio_device_unregister(indio_dev);
396 error_remove_trigger:
397 	hid_sensor_remove_trigger(&incl_state->common_attributes);
398 error_unreg_buffer_funcs:
399 	iio_triggered_buffer_cleanup(indio_dev);
400 error_free_dev_mem:
401 	kfree(indio_dev->channels);
402 	return ret;
403 }
404 
405 /* Function to deinitialize the processing for usage id */
406 static int hid_incl_3d_remove(struct platform_device *pdev)
407 {
408 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
409 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
410 	struct incl_3d_state *incl_state = iio_priv(indio_dev);
411 
412 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_INCLINOMETER_3D);
413 	iio_device_unregister(indio_dev);
414 	hid_sensor_remove_trigger(&incl_state->common_attributes);
415 	iio_triggered_buffer_cleanup(indio_dev);
416 	kfree(indio_dev->channels);
417 
418 	return 0;
419 }
420 
421 static const struct platform_device_id hid_incl_3d_ids[] = {
422 	{
423 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
424 		.name = "HID-SENSOR-200086",
425 	},
426 	{ /* sentinel */ }
427 };
428 MODULE_DEVICE_TABLE(platform, hid_incl_3d_ids);
429 
430 static struct platform_driver hid_incl_3d_platform_driver = {
431 	.id_table = hid_incl_3d_ids,
432 	.driver = {
433 		.name	= KBUILD_MODNAME,
434 		.pm	= &hid_sensor_pm_ops,
435 	},
436 	.probe		= hid_incl_3d_probe,
437 	.remove		= hid_incl_3d_remove,
438 };
439 module_platform_driver(hid_incl_3d_platform_driver);
440 
441 MODULE_DESCRIPTION("HID Sensor Inclinometer 3D");
442 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
443 MODULE_LICENSE("GPL");
444