xref: /linux/drivers/iio/gyro/hid-sensor-gyro-3d.c (revision cf40a76e7d5874bb25f4404eecc58a2e033af885)
1 /*
2  * HID Sensors Driver
3  * Copyright (c) 2012, 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  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
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 gyro_3d_channel {
35 	CHANNEL_SCAN_INDEX_X,
36 	CHANNEL_SCAN_INDEX_Y,
37 	CHANNEL_SCAN_INDEX_Z,
38 	GYRO_3D_CHANNEL_MAX,
39 };
40 
41 struct gyro_3d_state {
42 	struct hid_sensor_hub_callbacks callbacks;
43 	struct hid_sensor_common common_attributes;
44 	struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
45 	u32 gyro_val[GYRO_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 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
53 	HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS,
54 	HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS,
55 	HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS
56 };
57 
58 /* Channel definitions */
59 static const struct iio_chan_spec gyro_3d_channels[] = {
60 	{
61 		.type = IIO_ANGL_VEL,
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_ANGL_VEL,
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_ANGL_VEL,
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 gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
95 						int channel, int size)
96 {
97 	channels[channel].scan_type.sign = 's';
98 	/* Real storage bits will change based on the report desc. */
99 	channels[channel].scan_type.realbits = size * 8;
100 	/* Maximum size of a sample to capture is u32 */
101 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
102 }
103 
104 /* Channel read_raw handler */
105 static int gyro_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 gyro_3d_state *gyro_state = iio_priv(indio_dev);
111 	int report_id = -1;
112 	u32 address;
113 	int ret_type;
114 
115 	*val = 0;
116 	*val2 = 0;
117 	switch (mask) {
118 	case 0:
119 		hid_sensor_power_state(&gyro_state->common_attributes, true);
120 		report_id = gyro_state->gyro[chan->scan_index].report_id;
121 		address = gyro_3d_addresses[chan->scan_index];
122 		if (report_id >= 0)
123 			*val = sensor_hub_input_attr_get_raw_value(
124 					gyro_state->common_attributes.hsdev,
125 					HID_USAGE_SENSOR_GYRO_3D, address,
126 					report_id,
127 					SENSOR_HUB_SYNC);
128 		else {
129 			*val = 0;
130 			hid_sensor_power_state(&gyro_state->common_attributes,
131 						false);
132 			return -EINVAL;
133 		}
134 		hid_sensor_power_state(&gyro_state->common_attributes, false);
135 		ret_type = IIO_VAL_INT;
136 		break;
137 	case IIO_CHAN_INFO_SCALE:
138 		*val = gyro_state->scale_pre_decml;
139 		*val2 = gyro_state->scale_post_decml;
140 		ret_type = gyro_state->scale_precision;
141 		break;
142 	case IIO_CHAN_INFO_OFFSET:
143 		*val = gyro_state->value_offset;
144 		ret_type = IIO_VAL_INT;
145 		break;
146 	case IIO_CHAN_INFO_SAMP_FREQ:
147 		ret_type = hid_sensor_read_samp_freq_value(
148 			&gyro_state->common_attributes, val, val2);
149 		break;
150 	case IIO_CHAN_INFO_HYSTERESIS:
151 		ret_type = hid_sensor_read_raw_hyst_value(
152 			&gyro_state->common_attributes, val, val2);
153 		break;
154 	default:
155 		ret_type = -EINVAL;
156 		break;
157 	}
158 
159 	return ret_type;
160 }
161 
162 /* Channel write_raw handler */
163 static int gyro_3d_write_raw(struct iio_dev *indio_dev,
164 			       struct iio_chan_spec const *chan,
165 			       int val,
166 			       int val2,
167 			       long mask)
168 {
169 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
170 	int ret = 0;
171 
172 	switch (mask) {
173 	case IIO_CHAN_INFO_SAMP_FREQ:
174 		ret = hid_sensor_write_samp_freq_value(
175 				&gyro_state->common_attributes, val, val2);
176 		break;
177 	case IIO_CHAN_INFO_HYSTERESIS:
178 		ret = hid_sensor_write_raw_hyst_value(
179 				&gyro_state->common_attributes, val, val2);
180 		break;
181 	default:
182 		ret = -EINVAL;
183 	}
184 
185 	return ret;
186 }
187 
188 static const struct iio_info gyro_3d_info = {
189 	.read_raw = &gyro_3d_read_raw,
190 	.write_raw = &gyro_3d_write_raw,
191 };
192 
193 /* Function to push data to buffer */
194 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
195 	int len)
196 {
197 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
198 	iio_push_to_buffers(indio_dev, data);
199 }
200 
201 /* Callback handler to send event after all samples are received and captured */
202 static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
203 				unsigned usage_id,
204 				void *priv)
205 {
206 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
207 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
208 
209 	dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
210 	if (atomic_read(&gyro_state->common_attributes.data_ready))
211 		hid_sensor_push_data(indio_dev,
212 				gyro_state->gyro_val,
213 				sizeof(gyro_state->gyro_val));
214 
215 	return 0;
216 }
217 
218 /* Capture samples in local storage */
219 static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
220 				unsigned usage_id,
221 				size_t raw_len, char *raw_data,
222 				void *priv)
223 {
224 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
225 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
226 	int offset;
227 	int ret = -EINVAL;
228 
229 	switch (usage_id) {
230 	case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS:
231 	case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS:
232 	case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
233 		offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
234 		gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
235 						*(u32 *)raw_data;
236 		ret = 0;
237 	break;
238 	default:
239 		break;
240 	}
241 
242 	return ret;
243 }
244 
245 /* Parse report which is specific to an usage id*/
246 static int gyro_3d_parse_report(struct platform_device *pdev,
247 				struct hid_sensor_hub_device *hsdev,
248 				struct iio_chan_spec *channels,
249 				unsigned usage_id,
250 				struct gyro_3d_state *st)
251 {
252 	int ret;
253 	int i;
254 
255 	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
256 		ret = sensor_hub_input_get_attribute_info(hsdev,
257 				HID_INPUT_REPORT,
258 				usage_id,
259 				HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,
260 				&st->gyro[CHANNEL_SCAN_INDEX_X + i]);
261 		if (ret < 0)
262 			break;
263 		gyro_3d_adjust_channel_bit_mask(channels,
264 				CHANNEL_SCAN_INDEX_X + i,
265 				st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
266 	}
267 	dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n",
268 			st->gyro[0].index,
269 			st->gyro[0].report_id,
270 			st->gyro[1].index, st->gyro[1].report_id,
271 			st->gyro[2].index, st->gyro[2].report_id);
272 
273 	st->scale_precision = hid_sensor_format_scale(
274 				HID_USAGE_SENSOR_GYRO_3D,
275 				&st->gyro[CHANNEL_SCAN_INDEX_X],
276 				&st->scale_pre_decml, &st->scale_post_decml);
277 
278 	/* Set Sensitivity field ids, when there is no individual modifier */
279 	if (st->common_attributes.sensitivity.index < 0) {
280 		sensor_hub_input_get_attribute_info(hsdev,
281 			HID_FEATURE_REPORT, usage_id,
282 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
283 			HID_USAGE_SENSOR_DATA_ANGL_VELOCITY,
284 			&st->common_attributes.sensitivity);
285 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
286 			st->common_attributes.sensitivity.index,
287 			st->common_attributes.sensitivity.report_id);
288 	}
289 	return ret;
290 }
291 
292 /* Function to initialize the processing for usage id */
293 static int hid_gyro_3d_probe(struct platform_device *pdev)
294 {
295 	int ret = 0;
296 	static const char *name = "gyro_3d";
297 	struct iio_dev *indio_dev;
298 	struct gyro_3d_state *gyro_state;
299 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
300 
301 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
302 	if (!indio_dev)
303 		return -ENOMEM;
304 	platform_set_drvdata(pdev, indio_dev);
305 
306 	gyro_state = iio_priv(indio_dev);
307 	gyro_state->common_attributes.hsdev = hsdev;
308 	gyro_state->common_attributes.pdev = pdev;
309 
310 	ret = hid_sensor_parse_common_attributes(hsdev,
311 						HID_USAGE_SENSOR_GYRO_3D,
312 						&gyro_state->common_attributes);
313 	if (ret) {
314 		dev_err(&pdev->dev, "failed to setup common attributes\n");
315 		return ret;
316 	}
317 
318 	indio_dev->channels = kmemdup(gyro_3d_channels,
319 				      sizeof(gyro_3d_channels), GFP_KERNEL);
320 	if (!indio_dev->channels) {
321 		dev_err(&pdev->dev, "failed to duplicate channels\n");
322 		return -ENOMEM;
323 	}
324 
325 	ret = gyro_3d_parse_report(pdev, hsdev,
326 				   (struct iio_chan_spec *)indio_dev->channels,
327 				   HID_USAGE_SENSOR_GYRO_3D, gyro_state);
328 	if (ret) {
329 		dev_err(&pdev->dev, "failed to setup attributes\n");
330 		goto error_free_dev_mem;
331 	}
332 
333 	indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels);
334 	indio_dev->dev.parent = &pdev->dev;
335 	indio_dev->info = &gyro_3d_info;
336 	indio_dev->name = name;
337 	indio_dev->modes = INDIO_DIRECT_MODE;
338 
339 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
340 		NULL, NULL);
341 	if (ret) {
342 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
343 		goto error_free_dev_mem;
344 	}
345 	atomic_set(&gyro_state->common_attributes.data_ready, 0);
346 	ret = hid_sensor_setup_trigger(indio_dev, name,
347 					&gyro_state->common_attributes);
348 	if (ret < 0) {
349 		dev_err(&pdev->dev, "trigger setup failed\n");
350 		goto error_unreg_buffer_funcs;
351 	}
352 
353 	ret = iio_device_register(indio_dev);
354 	if (ret) {
355 		dev_err(&pdev->dev, "device register failed\n");
356 		goto error_remove_trigger;
357 	}
358 
359 	gyro_state->callbacks.send_event = gyro_3d_proc_event;
360 	gyro_state->callbacks.capture_sample = gyro_3d_capture_sample;
361 	gyro_state->callbacks.pdev = pdev;
362 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D,
363 					&gyro_state->callbacks);
364 	if (ret < 0) {
365 		dev_err(&pdev->dev, "callback reg failed\n");
366 		goto error_iio_unreg;
367 	}
368 
369 	return ret;
370 
371 error_iio_unreg:
372 	iio_device_unregister(indio_dev);
373 error_remove_trigger:
374 	hid_sensor_remove_trigger(&gyro_state->common_attributes);
375 error_unreg_buffer_funcs:
376 	iio_triggered_buffer_cleanup(indio_dev);
377 error_free_dev_mem:
378 	kfree(indio_dev->channels);
379 	return ret;
380 }
381 
382 /* Function to deinitialize the processing for usage id */
383 static int hid_gyro_3d_remove(struct platform_device *pdev)
384 {
385 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
386 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
387 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
388 
389 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D);
390 	iio_device_unregister(indio_dev);
391 	hid_sensor_remove_trigger(&gyro_state->common_attributes);
392 	iio_triggered_buffer_cleanup(indio_dev);
393 	kfree(indio_dev->channels);
394 
395 	return 0;
396 }
397 
398 static const struct platform_device_id hid_gyro_3d_ids[] = {
399 	{
400 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
401 		.name = "HID-SENSOR-200076",
402 	},
403 	{ /* sentinel */ }
404 };
405 MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
406 
407 static struct platform_driver hid_gyro_3d_platform_driver = {
408 	.id_table = hid_gyro_3d_ids,
409 	.driver = {
410 		.name	= KBUILD_MODNAME,
411 		.pm	= &hid_sensor_pm_ops,
412 	},
413 	.probe		= hid_gyro_3d_probe,
414 	.remove		= hid_gyro_3d_remove,
415 };
416 module_platform_driver(hid_gyro_3d_platform_driver);
417 
418 MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
419 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
420 MODULE_LICENSE("GPL");
421