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