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-trigger.h"
32 
33 enum gyro_3d_channel {
34 	CHANNEL_SCAN_INDEX_X,
35 	CHANNEL_SCAN_INDEX_Y,
36 	CHANNEL_SCAN_INDEX_Z,
37 	GYRO_3D_CHANNEL_MAX,
38 };
39 
40 struct gyro_3d_state {
41 	struct hid_sensor_hub_callbacks callbacks;
42 	struct hid_sensor_common common_attributes;
43 	struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
44 	u32 gyro_val[GYRO_3D_CHANNEL_MAX];
45 };
46 
47 static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
48 	HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS,
49 	HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS,
50 	HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS
51 };
52 
53 /* Channel definitions */
54 static const struct iio_chan_spec gyro_3d_channels[] = {
55 	{
56 		.type = IIO_ANGL_VEL,
57 		.modified = 1,
58 		.channel2 = IIO_MOD_X,
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_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
69 		BIT(IIO_CHAN_INFO_SCALE) |
70 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
71 		BIT(IIO_CHAN_INFO_HYSTERESIS),
72 		.scan_index = CHANNEL_SCAN_INDEX_Y,
73 	}, {
74 		.type = IIO_ANGL_VEL,
75 		.modified = 1,
76 		.channel2 = IIO_MOD_Z,
77 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
78 		BIT(IIO_CHAN_INFO_SCALE) |
79 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
80 		BIT(IIO_CHAN_INFO_HYSTERESIS),
81 		.scan_index = CHANNEL_SCAN_INDEX_Z,
82 	}
83 };
84 
85 /* Adjust channel real bits based on report descriptor */
86 static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
87 						int channel, int size)
88 {
89 	channels[channel].scan_type.sign = 's';
90 	/* Real storage bits will change based on the report desc. */
91 	channels[channel].scan_type.realbits = size * 8;
92 	/* Maximum size of a sample to capture is u32 */
93 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
94 }
95 
96 /* Channel read_raw handler */
97 static int gyro_3d_read_raw(struct iio_dev *indio_dev,
98 			      struct iio_chan_spec const *chan,
99 			      int *val, int *val2,
100 			      long mask)
101 {
102 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
103 	int report_id = -1;
104 	u32 address;
105 	int ret;
106 	int ret_type;
107 
108 	*val = 0;
109 	*val2 = 0;
110 	switch (mask) {
111 	case 0:
112 		report_id = gyro_state->gyro[chan->scan_index].report_id;
113 		address = gyro_3d_addresses[chan->scan_index];
114 		if (report_id >= 0)
115 			*val = sensor_hub_input_attr_get_raw_value(
116 				gyro_state->common_attributes.hsdev,
117 				HID_USAGE_SENSOR_GYRO_3D, address,
118 				report_id);
119 		else {
120 			*val = 0;
121 			return -EINVAL;
122 		}
123 		ret_type = IIO_VAL_INT;
124 		break;
125 	case IIO_CHAN_INFO_SCALE:
126 		*val = gyro_state->gyro[CHANNEL_SCAN_INDEX_X].units;
127 		ret_type = IIO_VAL_INT;
128 		break;
129 	case IIO_CHAN_INFO_OFFSET:
130 		*val = hid_sensor_convert_exponent(
131 			gyro_state->gyro[CHANNEL_SCAN_INDEX_X].unit_expo);
132 		ret_type = IIO_VAL_INT;
133 		break;
134 	case IIO_CHAN_INFO_SAMP_FREQ:
135 		ret = hid_sensor_read_samp_freq_value(
136 			&gyro_state->common_attributes, val, val2);
137 			ret_type = IIO_VAL_INT_PLUS_MICRO;
138 		break;
139 	case IIO_CHAN_INFO_HYSTERESIS:
140 		ret = hid_sensor_read_raw_hyst_value(
141 			&gyro_state->common_attributes, val, val2);
142 		ret_type = IIO_VAL_INT_PLUS_MICRO;
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 	.driver_module = THIS_MODULE,
180 	.read_raw = &gyro_3d_read_raw,
181 	.write_raw = &gyro_3d_write_raw,
182 };
183 
184 /* Function to push data to buffer */
185 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
186 {
187 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
188 	iio_push_to_buffers(indio_dev, (u8 *)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 [%d]\n",
200 				gyro_state->common_attributes.data_ready);
201 	if (gyro_state->common_attributes.data_ready)
202 		hid_sensor_push_data(indio_dev,
203 				(u8 *)gyro_state->gyro_val,
204 				sizeof(gyro_state->gyro_val));
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->gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
226 						*(u32 *)raw_data;
227 		ret = 0;
228 	break;
229 	default:
230 		break;
231 	}
232 
233 	return ret;
234 }
235 
236 /* Parse report which is specific to an usage id*/
237 static int gyro_3d_parse_report(struct platform_device *pdev,
238 				struct hid_sensor_hub_device *hsdev,
239 				struct iio_chan_spec *channels,
240 				unsigned usage_id,
241 				struct gyro_3d_state *st)
242 {
243 	int ret;
244 	int i;
245 
246 	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
247 		ret = sensor_hub_input_get_attribute_info(hsdev,
248 				HID_INPUT_REPORT,
249 				usage_id,
250 				HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,
251 				&st->gyro[CHANNEL_SCAN_INDEX_X + i]);
252 		if (ret < 0)
253 			break;
254 		gyro_3d_adjust_channel_bit_mask(channels,
255 				CHANNEL_SCAN_INDEX_X + i,
256 				st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
257 	}
258 	dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n",
259 			st->gyro[0].index,
260 			st->gyro[0].report_id,
261 			st->gyro[1].index, st->gyro[1].report_id,
262 			st->gyro[2].index, st->gyro[2].report_id);
263 
264 	return ret;
265 }
266 
267 /* Function to initialize the processing for usage id */
268 static int hid_gyro_3d_probe(struct platform_device *pdev)
269 {
270 	int ret = 0;
271 	static const char *name = "gyro_3d";
272 	struct iio_dev *indio_dev;
273 	struct gyro_3d_state *gyro_state;
274 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
275 	struct iio_chan_spec *channels;
276 
277 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
278 	if (!indio_dev)
279 		return -ENOMEM;
280 	platform_set_drvdata(pdev, indio_dev);
281 
282 	gyro_state = iio_priv(indio_dev);
283 	gyro_state->common_attributes.hsdev = hsdev;
284 	gyro_state->common_attributes.pdev = pdev;
285 
286 	ret = hid_sensor_parse_common_attributes(hsdev,
287 						HID_USAGE_SENSOR_GYRO_3D,
288 						&gyro_state->common_attributes);
289 	if (ret) {
290 		dev_err(&pdev->dev, "failed to setup common attributes\n");
291 		return ret;
292 	}
293 
294 	channels = kmemdup(gyro_3d_channels, sizeof(gyro_3d_channels),
295 			   GFP_KERNEL);
296 	if (!channels) {
297 		dev_err(&pdev->dev, "failed to duplicate channels\n");
298 		return -ENOMEM;
299 	}
300 
301 	ret = gyro_3d_parse_report(pdev, hsdev, channels,
302 					HID_USAGE_SENSOR_GYRO_3D, gyro_state);
303 	if (ret) {
304 		dev_err(&pdev->dev, "failed to setup attributes\n");
305 		goto error_free_dev_mem;
306 	}
307 
308 	indio_dev->channels = channels;
309 	indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels);
310 	indio_dev->dev.parent = &pdev->dev;
311 	indio_dev->info = &gyro_3d_info;
312 	indio_dev->name = name;
313 	indio_dev->modes = INDIO_DIRECT_MODE;
314 
315 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
316 		NULL, NULL);
317 	if (ret) {
318 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
319 		goto error_free_dev_mem;
320 	}
321 	gyro_state->common_attributes.data_ready = false;
322 	ret = hid_sensor_setup_trigger(indio_dev, name,
323 					&gyro_state->common_attributes);
324 	if (ret < 0) {
325 		dev_err(&pdev->dev, "trigger setup failed\n");
326 		goto error_unreg_buffer_funcs;
327 	}
328 
329 	ret = iio_device_register(indio_dev);
330 	if (ret) {
331 		dev_err(&pdev->dev, "device register failed\n");
332 		goto error_remove_trigger;
333 	}
334 
335 	gyro_state->callbacks.send_event = gyro_3d_proc_event;
336 	gyro_state->callbacks.capture_sample = gyro_3d_capture_sample;
337 	gyro_state->callbacks.pdev = pdev;
338 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D,
339 					&gyro_state->callbacks);
340 	if (ret < 0) {
341 		dev_err(&pdev->dev, "callback reg failed\n");
342 		goto error_iio_unreg;
343 	}
344 
345 	return ret;
346 
347 error_iio_unreg:
348 	iio_device_unregister(indio_dev);
349 error_remove_trigger:
350 	hid_sensor_remove_trigger(indio_dev);
351 error_unreg_buffer_funcs:
352 	iio_triggered_buffer_cleanup(indio_dev);
353 error_free_dev_mem:
354 	kfree(indio_dev->channels);
355 	return ret;
356 }
357 
358 /* Function to deinitialize the processing for usage id */
359 static int hid_gyro_3d_remove(struct platform_device *pdev)
360 {
361 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
362 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
363 
364 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D);
365 	iio_device_unregister(indio_dev);
366 	hid_sensor_remove_trigger(indio_dev);
367 	iio_triggered_buffer_cleanup(indio_dev);
368 	kfree(indio_dev->channels);
369 
370 	return 0;
371 }
372 
373 static struct platform_device_id hid_gyro_3d_ids[] = {
374 	{
375 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
376 		.name = "HID-SENSOR-200076",
377 	},
378 	{ /* sentinel */ }
379 };
380 MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
381 
382 static struct platform_driver hid_gyro_3d_platform_driver = {
383 	.id_table = hid_gyro_3d_ids,
384 	.driver = {
385 		.name	= KBUILD_MODNAME,
386 		.owner	= THIS_MODULE,
387 	},
388 	.probe		= hid_gyro_3d_probe,
389 	.remove		= hid_gyro_3d_remove,
390 };
391 module_platform_driver(hid_gyro_3d_platform_driver);
392 
393 MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
394 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
395 MODULE_LICENSE("GPL");
396