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