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