xref: /linux/drivers/iio/gyro/hid-sensor-gyro-3d.c (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
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 static const u32 gryo_3d_sensitivity_addresses[] = {
49 	HID_USAGE_SENSOR_DATA_ANGL_VELOCITY,
50 };
51 
52 /* Channel definitions */
53 static const struct iio_chan_spec gyro_3d_channels[] = {
54 	{
55 		.type = IIO_ANGL_VEL,
56 		.modified = 1,
57 		.channel2 = IIO_MOD_X,
58 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
59 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
60 		BIT(IIO_CHAN_INFO_SCALE) |
61 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
62 		BIT(IIO_CHAN_INFO_HYSTERESIS),
63 		.scan_index = CHANNEL_SCAN_INDEX_X,
64 	}, {
65 		.type = IIO_ANGL_VEL,
66 		.modified = 1,
67 		.channel2 = IIO_MOD_Y,
68 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
69 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
70 		BIT(IIO_CHAN_INFO_SCALE) |
71 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
72 		BIT(IIO_CHAN_INFO_HYSTERESIS),
73 		.scan_index = CHANNEL_SCAN_INDEX_Y,
74 	}, {
75 		.type = IIO_ANGL_VEL,
76 		.modified = 1,
77 		.channel2 = IIO_MOD_Z,
78 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
79 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
80 		BIT(IIO_CHAN_INFO_SCALE) |
81 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
82 		BIT(IIO_CHAN_INFO_HYSTERESIS),
83 		.scan_index = CHANNEL_SCAN_INDEX_Z,
84 	},
85 	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
86 };
87 
88 /* Adjust channel real bits based on report descriptor */
89 static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
90 						int channel, int size)
91 {
92 	channels[channel].scan_type.sign = 's';
93 	/* Real storage bits will change based on the report desc. */
94 	channels[channel].scan_type.realbits = size * 8;
95 	/* Maximum size of a sample to capture is u32 */
96 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
97 }
98 
99 /* Channel read_raw handler */
100 static int gyro_3d_read_raw(struct iio_dev *indio_dev,
101 			      struct iio_chan_spec const *chan,
102 			      int *val, int *val2,
103 			      long mask)
104 {
105 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
106 	int report_id = -1;
107 	u32 address;
108 	int ret_type;
109 	s32 min;
110 
111 	*val = 0;
112 	*val2 = 0;
113 	switch (mask) {
114 	case IIO_CHAN_INFO_RAW:
115 		hid_sensor_power_state(&gyro_state->common_attributes, true);
116 		report_id = gyro_state->gyro[chan->scan_index].report_id;
117 		min = gyro_state->gyro[chan->scan_index].logical_minimum;
118 		address = gyro_3d_addresses[chan->scan_index];
119 		if (report_id >= 0)
120 			*val = sensor_hub_input_attr_get_raw_value(
121 					gyro_state->common_attributes.hsdev,
122 					HID_USAGE_SENSOR_GYRO_3D, address,
123 					report_id,
124 					SENSOR_HUB_SYNC,
125 					min < 0);
126 		else {
127 			*val = 0;
128 			hid_sensor_power_state(&gyro_state->common_attributes,
129 						false);
130 			return -EINVAL;
131 		}
132 		hid_sensor_power_state(&gyro_state->common_attributes, false);
133 		ret_type = IIO_VAL_INT;
134 		break;
135 	case IIO_CHAN_INFO_SCALE:
136 		*val = gyro_state->scale_pre_decml;
137 		*val2 = gyro_state->scale_post_decml;
138 		ret_type = gyro_state->scale_precision;
139 		break;
140 	case IIO_CHAN_INFO_OFFSET:
141 		*val = gyro_state->value_offset;
142 		ret_type = IIO_VAL_INT;
143 		break;
144 	case IIO_CHAN_INFO_SAMP_FREQ:
145 		ret_type = hid_sensor_read_samp_freq_value(
146 			&gyro_state->common_attributes, val, val2);
147 		break;
148 	case IIO_CHAN_INFO_HYSTERESIS:
149 		ret_type = hid_sensor_read_raw_hyst_value(
150 			&gyro_state->common_attributes, val, val2);
151 		break;
152 	default:
153 		ret_type = -EINVAL;
154 		break;
155 	}
156 
157 	return ret_type;
158 }
159 
160 /* Channel write_raw handler */
161 static int gyro_3d_write_raw(struct iio_dev *indio_dev,
162 			       struct iio_chan_spec const *chan,
163 			       int val,
164 			       int val2,
165 			       long mask)
166 {
167 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
168 	int ret = 0;
169 
170 	switch (mask) {
171 	case IIO_CHAN_INFO_SAMP_FREQ:
172 		ret = hid_sensor_write_samp_freq_value(
173 				&gyro_state->common_attributes, val, val2);
174 		break;
175 	case IIO_CHAN_INFO_HYSTERESIS:
176 		ret = hid_sensor_write_raw_hyst_value(
177 				&gyro_state->common_attributes, val, val2);
178 		break;
179 	default:
180 		ret = -EINVAL;
181 	}
182 
183 	return ret;
184 }
185 
186 static const struct iio_info gyro_3d_info = {
187 	.read_raw = &gyro_3d_read_raw,
188 	.write_raw = &gyro_3d_write_raw,
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 		if (!gyro_state->timestamp)
202 			gyro_state->timestamp = iio_get_time_ns(indio_dev);
203 
204 		iio_push_to_buffers_with_timestamp(indio_dev, &gyro_state->scan,
205 						   gyro_state->timestamp);
206 
207 		gyro_state->timestamp = 0;
208 	}
209 
210 	return 0;
211 }
212 
213 /* Capture samples in local storage */
214 static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
215 				unsigned usage_id,
216 				size_t raw_len, char *raw_data,
217 				void *priv)
218 {
219 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
220 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
221 	int offset;
222 	int ret = -EINVAL;
223 
224 	switch (usage_id) {
225 	case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS:
226 	case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS:
227 	case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
228 		offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
229 		gyro_state->scan.gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
230 				*(u32 *)raw_data;
231 		ret = 0;
232 	break;
233 	case HID_USAGE_SENSOR_TIME_TIMESTAMP:
234 		gyro_state->timestamp =
235 			hid_sensor_convert_timestamp(&gyro_state->common_attributes,
236 						     *(s64 *)raw_data);
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 	return ret;
279 }
280 
281 /* Function to initialize the processing for usage id */
282 static int hid_gyro_3d_probe(struct platform_device *pdev)
283 {
284 	int ret = 0;
285 	static const char *name = "gyro_3d";
286 	struct iio_dev *indio_dev;
287 	struct gyro_3d_state *gyro_state;
288 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
289 
290 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
291 	if (!indio_dev)
292 		return -ENOMEM;
293 	platform_set_drvdata(pdev, indio_dev);
294 
295 	gyro_state = iio_priv(indio_dev);
296 	gyro_state->common_attributes.hsdev = hsdev;
297 	gyro_state->common_attributes.pdev = pdev;
298 
299 	ret = hid_sensor_parse_common_attributes(hsdev,
300 						HID_USAGE_SENSOR_GYRO_3D,
301 						&gyro_state->common_attributes,
302 						gryo_3d_sensitivity_addresses,
303 						ARRAY_SIZE(gryo_3d_sensitivity_addresses));
304 	if (ret) {
305 		dev_err(&pdev->dev, "failed to setup common attributes\n");
306 		return ret;
307 	}
308 
309 	indio_dev->channels = kmemdup(gyro_3d_channels,
310 				      sizeof(gyro_3d_channels), GFP_KERNEL);
311 	if (!indio_dev->channels) {
312 		dev_err(&pdev->dev, "failed to duplicate channels\n");
313 		return -ENOMEM;
314 	}
315 
316 	ret = gyro_3d_parse_report(pdev, hsdev,
317 				   (struct iio_chan_spec *)indio_dev->channels,
318 				   HID_USAGE_SENSOR_GYRO_3D, gyro_state);
319 	if (ret) {
320 		dev_err(&pdev->dev, "failed to setup attributes\n");
321 		goto error_free_dev_mem;
322 	}
323 
324 	indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels);
325 	indio_dev->info = &gyro_3d_info;
326 	indio_dev->name = name;
327 	indio_dev->modes = INDIO_DIRECT_MODE;
328 
329 	atomic_set(&gyro_state->common_attributes.data_ready, 0);
330 
331 	ret = hid_sensor_setup_trigger(indio_dev, name,
332 					&gyro_state->common_attributes);
333 	if (ret < 0) {
334 		dev_err(&pdev->dev, "trigger setup failed\n");
335 		goto error_free_dev_mem;
336 	}
337 
338 	ret = iio_device_register(indio_dev);
339 	if (ret) {
340 		dev_err(&pdev->dev, "device register failed\n");
341 		goto error_remove_trigger;
342 	}
343 
344 	gyro_state->callbacks.send_event = gyro_3d_proc_event;
345 	gyro_state->callbacks.capture_sample = gyro_3d_capture_sample;
346 	gyro_state->callbacks.pdev = pdev;
347 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D,
348 					&gyro_state->callbacks);
349 	if (ret < 0) {
350 		dev_err(&pdev->dev, "callback reg failed\n");
351 		goto error_iio_unreg;
352 	}
353 
354 	return ret;
355 
356 error_iio_unreg:
357 	iio_device_unregister(indio_dev);
358 error_remove_trigger:
359 	hid_sensor_remove_trigger(indio_dev, &gyro_state->common_attributes);
360 error_free_dev_mem:
361 	kfree(indio_dev->channels);
362 	return ret;
363 }
364 
365 /* Function to deinitialize the processing for usage id */
366 static int hid_gyro_3d_remove(struct platform_device *pdev)
367 {
368 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
369 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
370 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
371 
372 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D);
373 	iio_device_unregister(indio_dev);
374 	hid_sensor_remove_trigger(indio_dev, &gyro_state->common_attributes);
375 	kfree(indio_dev->channels);
376 
377 	return 0;
378 }
379 
380 static const struct platform_device_id hid_gyro_3d_ids[] = {
381 	{
382 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
383 		.name = "HID-SENSOR-200076",
384 	},
385 	{ /* sentinel */ }
386 };
387 MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
388 
389 static struct platform_driver hid_gyro_3d_platform_driver = {
390 	.id_table = hid_gyro_3d_ids,
391 	.driver = {
392 		.name	= KBUILD_MODNAME,
393 		.pm	= &hid_sensor_pm_ops,
394 	},
395 	.probe		= hid_gyro_3d_probe,
396 	.remove		= hid_gyro_3d_remove,
397 };
398 module_platform_driver(hid_gyro_3d_platform_driver);
399 
400 MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
401 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
402 MODULE_LICENSE("GPL");
403