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