xref: /linux/drivers/iio/magnetometer/hid-sensor-magn-3d.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
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 magn_3d_channel {
35 	CHANNEL_SCAN_INDEX_X,
36 	CHANNEL_SCAN_INDEX_Y,
37 	CHANNEL_SCAN_INDEX_Z,
38 	CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP,
39 	CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP,
40 	CHANNEL_SCAN_INDEX_NORTH_MAGN,
41 	CHANNEL_SCAN_INDEX_NORTH_TRUE,
42 	MAGN_3D_CHANNEL_MAX,
43 };
44 
45 struct magn_3d_state {
46 	struct hid_sensor_hub_callbacks callbacks;
47 	struct hid_sensor_common common_attributes;
48 	struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
49 
50 	/* dynamically sized array to hold sensor values */
51 	u32 *iio_vals;
52 	/* array of pointers to sensor value */
53 	u32 *magn_val_addr[MAGN_3D_CHANNEL_MAX];
54 
55 	int scale_pre_decml;
56 	int scale_post_decml;
57 	int scale_precision;
58 	int value_offset;
59 };
60 
61 static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
62 	HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS,
63 	HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS,
64 	HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS,
65 	HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH,
66 	HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH,
67 	HID_USAGE_SENSOR_ORIENT_MAGN_NORTH,
68 	HID_USAGE_SENSOR_ORIENT_TRUE_NORTH,
69 };
70 
71 /* Channel definitions */
72 static const struct iio_chan_spec magn_3d_channels[] = {
73 	{
74 		.type = IIO_MAGN,
75 		.modified = 1,
76 		.channel2 = IIO_MOD_X,
77 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
78 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
79 		BIT(IIO_CHAN_INFO_SCALE) |
80 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
81 		BIT(IIO_CHAN_INFO_HYSTERESIS),
82 	}, {
83 		.type = IIO_MAGN,
84 		.modified = 1,
85 		.channel2 = IIO_MOD_Y,
86 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
87 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
88 		BIT(IIO_CHAN_INFO_SCALE) |
89 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
90 		BIT(IIO_CHAN_INFO_HYSTERESIS),
91 	}, {
92 		.type = IIO_MAGN,
93 		.modified = 1,
94 		.channel2 = IIO_MOD_Z,
95 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
96 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
97 		BIT(IIO_CHAN_INFO_SCALE) |
98 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
99 		BIT(IIO_CHAN_INFO_HYSTERESIS),
100 	}, {
101 		.type = IIO_ROT,
102 		.modified = 1,
103 		.channel2 = IIO_MOD_NORTH_MAGN_TILT_COMP,
104 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
105 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
106 		BIT(IIO_CHAN_INFO_SCALE) |
107 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
108 		BIT(IIO_CHAN_INFO_HYSTERESIS),
109 	}, {
110 		.type = IIO_ROT,
111 		.modified = 1,
112 		.channel2 = IIO_MOD_NORTH_TRUE_TILT_COMP,
113 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
114 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
115 		BIT(IIO_CHAN_INFO_SCALE) |
116 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
117 		BIT(IIO_CHAN_INFO_HYSTERESIS),
118 	}, {
119 		.type = IIO_ROT,
120 		.modified = 1,
121 		.channel2 = IIO_MOD_NORTH_MAGN,
122 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
123 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
124 		BIT(IIO_CHAN_INFO_SCALE) |
125 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
126 		BIT(IIO_CHAN_INFO_HYSTERESIS),
127 	}, {
128 		.type = IIO_ROT,
129 		.modified = 1,
130 		.channel2 = IIO_MOD_NORTH_TRUE,
131 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
132 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
133 		BIT(IIO_CHAN_INFO_SCALE) |
134 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
135 		BIT(IIO_CHAN_INFO_HYSTERESIS),
136 	}
137 };
138 
139 /* Adjust channel real bits based on report descriptor */
140 static void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
141 						int channel, int size)
142 {
143 	channels[channel].scan_type.sign = 's';
144 	/* Real storage bits will change based on the report desc. */
145 	channels[channel].scan_type.realbits = size * 8;
146 	/* Maximum size of a sample to capture is u32 */
147 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
148 }
149 
150 /* Channel read_raw handler */
151 static int magn_3d_read_raw(struct iio_dev *indio_dev,
152 			      struct iio_chan_spec const *chan,
153 			      int *val, int *val2,
154 			      long mask)
155 {
156 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
157 	int report_id = -1;
158 	u32 address;
159 	int ret_type;
160 
161 	*val = 0;
162 	*val2 = 0;
163 	switch (mask) {
164 	case 0:
165 		hid_sensor_power_state(&magn_state->common_attributes, true);
166 		report_id =
167 			magn_state->magn[chan->address].report_id;
168 		address = magn_3d_addresses[chan->address];
169 		if (report_id >= 0)
170 			*val = sensor_hub_input_attr_get_raw_value(
171 				magn_state->common_attributes.hsdev,
172 				HID_USAGE_SENSOR_COMPASS_3D, address,
173 				report_id,
174 				SENSOR_HUB_SYNC);
175 		else {
176 			*val = 0;
177 			hid_sensor_power_state(&magn_state->common_attributes,
178 						false);
179 			return -EINVAL;
180 		}
181 		hid_sensor_power_state(&magn_state->common_attributes, false);
182 		ret_type = IIO_VAL_INT;
183 		break;
184 	case IIO_CHAN_INFO_SCALE:
185 		*val = magn_state->scale_pre_decml;
186 		*val2 = magn_state->scale_post_decml;
187 		ret_type = magn_state->scale_precision;
188 		break;
189 	case IIO_CHAN_INFO_OFFSET:
190 		*val = magn_state->value_offset;
191 		ret_type = IIO_VAL_INT;
192 		break;
193 	case IIO_CHAN_INFO_SAMP_FREQ:
194 		ret_type = hid_sensor_read_samp_freq_value(
195 			&magn_state->common_attributes, val, val2);
196 		break;
197 	case IIO_CHAN_INFO_HYSTERESIS:
198 		ret_type = hid_sensor_read_raw_hyst_value(
199 			&magn_state->common_attributes, val, val2);
200 		break;
201 	default:
202 		ret_type = -EINVAL;
203 		break;
204 	}
205 
206 	return ret_type;
207 }
208 
209 /* Channel write_raw handler */
210 static int magn_3d_write_raw(struct iio_dev *indio_dev,
211 			       struct iio_chan_spec const *chan,
212 			       int val,
213 			       int val2,
214 			       long mask)
215 {
216 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
217 	int ret = 0;
218 
219 	switch (mask) {
220 	case IIO_CHAN_INFO_SAMP_FREQ:
221 		ret = hid_sensor_write_samp_freq_value(
222 				&magn_state->common_attributes, val, val2);
223 		break;
224 	case IIO_CHAN_INFO_HYSTERESIS:
225 		ret = hid_sensor_write_raw_hyst_value(
226 				&magn_state->common_attributes, val, val2);
227 		break;
228 	default:
229 		ret = -EINVAL;
230 	}
231 
232 	return ret;
233 }
234 
235 static const struct iio_info magn_3d_info = {
236 	.driver_module = THIS_MODULE,
237 	.read_raw = &magn_3d_read_raw,
238 	.write_raw = &magn_3d_write_raw,
239 };
240 
241 /* Function to push data to buffer */
242 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data)
243 {
244 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
245 	iio_push_to_buffers(indio_dev, data);
246 }
247 
248 /* Callback handler to send event after all samples are received and captured */
249 static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
250 				unsigned usage_id,
251 				void *priv)
252 {
253 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
254 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
255 
256 	dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n");
257 	if (atomic_read(&magn_state->common_attributes.data_ready))
258 		hid_sensor_push_data(indio_dev, magn_state->iio_vals);
259 
260 	return 0;
261 }
262 
263 /* Capture samples in local storage */
264 static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
265 				unsigned usage_id,
266 				size_t raw_len, char *raw_data,
267 				void *priv)
268 {
269 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
270 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
271 	int offset;
272 	int ret = 0;
273 	u32 *iio_val = NULL;
274 
275 	switch (usage_id) {
276 	case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS:
277 	case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS:
278 	case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS:
279 		offset = (usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS)
280 				+ CHANNEL_SCAN_INDEX_X;
281 	break;
282 	case HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH:
283 	case HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH:
284 	case HID_USAGE_SENSOR_ORIENT_MAGN_NORTH:
285 	case HID_USAGE_SENSOR_ORIENT_TRUE_NORTH:
286 		offset = (usage_id - HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH)
287 				+ CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP;
288 	break;
289 	default:
290 		return -EINVAL;
291 	}
292 
293 	iio_val = magn_state->magn_val_addr[offset];
294 
295 	if (iio_val != NULL)
296 		*iio_val = *((u32 *)raw_data);
297 	else
298 		ret = -EINVAL;
299 
300 	return ret;
301 }
302 
303 /* Parse report which is specific to an usage id*/
304 static int magn_3d_parse_report(struct platform_device *pdev,
305 				struct hid_sensor_hub_device *hsdev,
306 				struct iio_chan_spec **channels,
307 				int *chan_count,
308 				unsigned usage_id,
309 				struct magn_3d_state *st)
310 {
311 	int i;
312 	int attr_count = 0;
313 	struct iio_chan_spec *_channels;
314 
315 	/* Scan for each usage attribute supported */
316 	for (i = 0; i < MAGN_3D_CHANNEL_MAX; i++) {
317 		int status;
318 		u32 address = magn_3d_addresses[i];
319 
320 		/* Check if usage attribute exists in the sensor hub device */
321 		status = sensor_hub_input_get_attribute_info(hsdev,
322 			HID_INPUT_REPORT,
323 			usage_id,
324 			address,
325 			&(st->magn[i]));
326 		if (!status)
327 			attr_count++;
328 	}
329 
330 	if (attr_count <= 0) {
331 		dev_err(&pdev->dev,
332 			"failed to find any supported usage attributes in report\n");
333 		return  -EINVAL;
334 	}
335 
336 	dev_dbg(&pdev->dev, "magn_3d Found %d usage attributes\n",
337 			attr_count);
338 	dev_dbg(&pdev->dev, "magn_3d X: %x:%x Y: %x:%x Z: %x:%x\n",
339 			st->magn[0].index,
340 			st->magn[0].report_id,
341 			st->magn[1].index, st->magn[1].report_id,
342 			st->magn[2].index, st->magn[2].report_id);
343 
344 	/* Setup IIO channel array */
345 	_channels = devm_kcalloc(&pdev->dev, attr_count,
346 				sizeof(struct iio_chan_spec),
347 				GFP_KERNEL);
348 	if (!_channels) {
349 		dev_err(&pdev->dev,
350 			"failed to allocate space for iio channels\n");
351 		return -ENOMEM;
352 	}
353 
354 	st->iio_vals = devm_kcalloc(&pdev->dev, attr_count,
355 				sizeof(u32),
356 				GFP_KERNEL);
357 	if (!st->iio_vals) {
358 		dev_err(&pdev->dev,
359 			"failed to allocate space for iio values array\n");
360 		return -ENOMEM;
361 	}
362 
363 	for (i = 0, *chan_count = 0;
364 	i < MAGN_3D_CHANNEL_MAX && *chan_count < attr_count;
365 	i++){
366 		if (st->magn[i].index >= 0) {
367 			/* Setup IIO channel struct */
368 			(_channels[*chan_count]) = magn_3d_channels[i];
369 			(_channels[*chan_count]).scan_index = *chan_count;
370 			(_channels[*chan_count]).address = i;
371 
372 			/* Set magn_val_addr to iio value address */
373 			st->magn_val_addr[i] = &(st->iio_vals[*chan_count]);
374 			magn_3d_adjust_channel_bit_mask(_channels,
375 							*chan_count,
376 							st->magn[i].size);
377 			(*chan_count)++;
378 		}
379 	}
380 
381 	if (*chan_count <= 0) {
382 		dev_err(&pdev->dev,
383 			"failed to find any magnetic channels setup\n");
384 		return -EINVAL;
385 	}
386 
387 	*channels = _channels;
388 
389 	dev_dbg(&pdev->dev, "magn_3d Setup %d IIO channels\n",
390 			*chan_count);
391 
392 	st->scale_precision = hid_sensor_format_scale(
393 				HID_USAGE_SENSOR_COMPASS_3D,
394 				&st->magn[CHANNEL_SCAN_INDEX_X],
395 				&st->scale_pre_decml, &st->scale_post_decml);
396 
397 	/* Set Sensitivity field ids, when there is no individual modifier */
398 	if (st->common_attributes.sensitivity.index < 0) {
399 		sensor_hub_input_get_attribute_info(hsdev,
400 			HID_FEATURE_REPORT, usage_id,
401 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
402 			HID_USAGE_SENSOR_DATA_ORIENTATION,
403 			&st->common_attributes.sensitivity);
404 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
405 			st->common_attributes.sensitivity.index,
406 			st->common_attributes.sensitivity.report_id);
407 	}
408 
409 	return 0;
410 }
411 
412 /* Function to initialize the processing for usage id */
413 static int hid_magn_3d_probe(struct platform_device *pdev)
414 {
415 	int ret = 0;
416 	static char *name = "magn_3d";
417 	struct iio_dev *indio_dev;
418 	struct magn_3d_state *magn_state;
419 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
420 	struct iio_chan_spec *channels;
421 	int chan_count = 0;
422 
423 	indio_dev = devm_iio_device_alloc(&pdev->dev,
424 					  sizeof(struct magn_3d_state));
425 	if (indio_dev == NULL)
426 		return -ENOMEM;
427 
428 	platform_set_drvdata(pdev, indio_dev);
429 
430 	magn_state = iio_priv(indio_dev);
431 	magn_state->common_attributes.hsdev = hsdev;
432 	magn_state->common_attributes.pdev = pdev;
433 
434 	ret = hid_sensor_parse_common_attributes(hsdev,
435 				HID_USAGE_SENSOR_COMPASS_3D,
436 				&magn_state->common_attributes);
437 	if (ret) {
438 		dev_err(&pdev->dev, "failed to setup common attributes\n");
439 		return ret;
440 	}
441 
442 	ret = magn_3d_parse_report(pdev, hsdev,
443 				&channels, &chan_count,
444 				HID_USAGE_SENSOR_COMPASS_3D, magn_state);
445 	if (ret) {
446 		dev_err(&pdev->dev, "failed to parse report\n");
447 		return ret;
448 	}
449 
450 	indio_dev->channels = channels;
451 	indio_dev->num_channels = chan_count;
452 	indio_dev->dev.parent = &pdev->dev;
453 	indio_dev->info = &magn_3d_info;
454 	indio_dev->name = name;
455 	indio_dev->modes = INDIO_DIRECT_MODE;
456 
457 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
458 		NULL, NULL);
459 	if (ret) {
460 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
461 		return ret;
462 	}
463 	atomic_set(&magn_state->common_attributes.data_ready, 0);
464 	ret = hid_sensor_setup_trigger(indio_dev, name,
465 					&magn_state->common_attributes);
466 	if (ret < 0) {
467 		dev_err(&pdev->dev, "trigger setup failed\n");
468 		goto error_unreg_buffer_funcs;
469 	}
470 
471 	ret = iio_device_register(indio_dev);
472 	if (ret) {
473 		dev_err(&pdev->dev, "device register failed\n");
474 		goto error_remove_trigger;
475 	}
476 
477 	magn_state->callbacks.send_event = magn_3d_proc_event;
478 	magn_state->callbacks.capture_sample = magn_3d_capture_sample;
479 	magn_state->callbacks.pdev = pdev;
480 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D,
481 					&magn_state->callbacks);
482 	if (ret < 0) {
483 		dev_err(&pdev->dev, "callback reg failed\n");
484 		goto error_iio_unreg;
485 	}
486 
487 	return ret;
488 
489 error_iio_unreg:
490 	iio_device_unregister(indio_dev);
491 error_remove_trigger:
492 	hid_sensor_remove_trigger(&magn_state->common_attributes);
493 error_unreg_buffer_funcs:
494 	iio_triggered_buffer_cleanup(indio_dev);
495 	return ret;
496 }
497 
498 /* Function to deinitialize the processing for usage id */
499 static int hid_magn_3d_remove(struct platform_device *pdev)
500 {
501 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
502 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
503 	struct magn_3d_state *magn_state = iio_priv(indio_dev);
504 
505 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D);
506 	iio_device_unregister(indio_dev);
507 	hid_sensor_remove_trigger(&magn_state->common_attributes);
508 	iio_triggered_buffer_cleanup(indio_dev);
509 
510 	return 0;
511 }
512 
513 static const struct platform_device_id hid_magn_3d_ids[] = {
514 	{
515 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
516 		.name = "HID-SENSOR-200083",
517 	},
518 	{ /* sentinel */ }
519 };
520 MODULE_DEVICE_TABLE(platform, hid_magn_3d_ids);
521 
522 static struct platform_driver hid_magn_3d_platform_driver = {
523 	.id_table = hid_magn_3d_ids,
524 	.driver = {
525 		.name	= KBUILD_MODNAME,
526 		.pm	= &hid_sensor_pm_ops,
527 	},
528 	.probe		= hid_magn_3d_probe,
529 	.remove		= hid_magn_3d_remove,
530 };
531 module_platform_driver(hid_magn_3d_platform_driver);
532 
533 MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");
534 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
535 MODULE_LICENSE("GPL");
536