xref: /openbmc/linux/drivers/iio/orientation/hid-sensor-rotation.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fc18dddcSSrinivas Pandruvada /*
3fc18dddcSSrinivas Pandruvada  * HID Sensors Driver
4fc18dddcSSrinivas Pandruvada  * Copyright (c) 2014, Intel Corporation.
5fc18dddcSSrinivas Pandruvada  */
6fc18dddcSSrinivas Pandruvada 
7fc18dddcSSrinivas Pandruvada #include <linux/device.h>
8fc18dddcSSrinivas Pandruvada #include <linux/platform_device.h>
9fc18dddcSSrinivas Pandruvada #include <linux/module.h>
10*fb226ae7SJonathan Cameron #include <linux/mod_devicetable.h>
11fc18dddcSSrinivas Pandruvada #include <linux/hid-sensor-hub.h>
12fc18dddcSSrinivas Pandruvada #include <linux/iio/iio.h>
13fc18dddcSSrinivas Pandruvada #include <linux/iio/sysfs.h>
14fc18dddcSSrinivas Pandruvada #include <linux/iio/buffer.h>
15fc18dddcSSrinivas Pandruvada #include "../common/hid-sensors/hid-sensor-trigger.h"
16fc18dddcSSrinivas Pandruvada 
17fc18dddcSSrinivas Pandruvada struct dev_rot_state {
18fc18dddcSSrinivas Pandruvada 	struct hid_sensor_hub_callbacks callbacks;
19fc18dddcSSrinivas Pandruvada 	struct hid_sensor_common common_attributes;
20fc18dddcSSrinivas Pandruvada 	struct hid_sensor_hub_attribute_info quaternion;
214a3582c8SYe Xiang 	struct {
226c3b6153SYe Xiang 		s32 sampled_vals[4] __aligned(16);
234a3582c8SYe Xiang 		u64 timestamp __aligned(8);
244a3582c8SYe Xiang 	} scan;
25a78587d3SSong Hongyan 	int scale_pre_decml;
26a78587d3SSong Hongyan 	int scale_post_decml;
27a78587d3SSong Hongyan 	int scale_precision;
28a78587d3SSong Hongyan 	int value_offset;
294a3582c8SYe Xiang 	s64 timestamp;
30fc18dddcSSrinivas Pandruvada };
31fc18dddcSSrinivas Pandruvada 
320e41fd51SYe Xiang static const u32 rotation_sensitivity_addresses[] = {
330e41fd51SYe Xiang 	HID_USAGE_SENSOR_DATA_ORIENTATION,
344efd13c3SYe Xiang 	HID_USAGE_SENSOR_ORIENT_QUATERNION,
350e41fd51SYe Xiang };
360e41fd51SYe Xiang 
37fc18dddcSSrinivas Pandruvada /* Channel definitions */
38fc18dddcSSrinivas Pandruvada static const struct iio_chan_spec dev_rot_channels[] = {
39fc18dddcSSrinivas Pandruvada 	{
40fc18dddcSSrinivas Pandruvada 		.type = IIO_ROT,
41fc18dddcSSrinivas Pandruvada 		.modified = 1,
42fc18dddcSSrinivas Pandruvada 		.channel2 = IIO_MOD_QUATERNION,
43fc18dddcSSrinivas Pandruvada 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
44fc18dddcSSrinivas Pandruvada 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
45a78587d3SSong Hongyan 					BIT(IIO_CHAN_INFO_OFFSET) |
46a78587d3SSong Hongyan 					BIT(IIO_CHAN_INFO_SCALE) |
474a3582c8SYe Xiang 					BIT(IIO_CHAN_INFO_HYSTERESIS),
484a3582c8SYe Xiang 		.scan_index = 0
494a3582c8SYe Xiang 	},
504a3582c8SYe Xiang 	IIO_CHAN_SOFT_TIMESTAMP(1)
51fc18dddcSSrinivas Pandruvada };
52fc18dddcSSrinivas Pandruvada 
53fc18dddcSSrinivas Pandruvada /* Adjust channel real bits based on report descriptor */
dev_rot_adjust_channel_bit_mask(struct iio_chan_spec * chan,int size)54fc18dddcSSrinivas Pandruvada static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
55fc18dddcSSrinivas Pandruvada 						int size)
56fc18dddcSSrinivas Pandruvada {
57fc18dddcSSrinivas Pandruvada 	chan->scan_type.sign = 's';
58fc18dddcSSrinivas Pandruvada 	/* Real storage bits will change based on the report desc. */
59fc18dddcSSrinivas Pandruvada 	chan->scan_type.realbits = size * 8;
60fc18dddcSSrinivas Pandruvada 	/* Maximum size of a sample to capture is u32 */
61fc18dddcSSrinivas Pandruvada 	chan->scan_type.storagebits = sizeof(u32) * 8;
62fc18dddcSSrinivas Pandruvada 	chan->scan_type.repeat = 4;
63fc18dddcSSrinivas Pandruvada }
64fc18dddcSSrinivas Pandruvada 
65fc18dddcSSrinivas Pandruvada /* Channel read_raw handler */
dev_rot_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int size,int * vals,int * val_len,long mask)66fc18dddcSSrinivas Pandruvada static int dev_rot_read_raw(struct iio_dev *indio_dev,
67fc18dddcSSrinivas Pandruvada 				struct iio_chan_spec const *chan,
68fc18dddcSSrinivas Pandruvada 				int size, int *vals, int *val_len,
69fc18dddcSSrinivas Pandruvada 				long mask)
70fc18dddcSSrinivas Pandruvada {
71fc18dddcSSrinivas Pandruvada 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
72fc18dddcSSrinivas Pandruvada 	int ret_type;
73fc18dddcSSrinivas Pandruvada 	int i;
74fc18dddcSSrinivas Pandruvada 
75fc18dddcSSrinivas Pandruvada 	vals[0] = 0;
76fc18dddcSSrinivas Pandruvada 	vals[1] = 0;
77fc18dddcSSrinivas Pandruvada 
78fc18dddcSSrinivas Pandruvada 	switch (mask) {
79fc18dddcSSrinivas Pandruvada 	case IIO_CHAN_INFO_RAW:
80fc18dddcSSrinivas Pandruvada 		if (size >= 4) {
81fc18dddcSSrinivas Pandruvada 			for (i = 0; i < 4; ++i)
824a3582c8SYe Xiang 				vals[i] = rot_state->scan.sampled_vals[i];
83fc18dddcSSrinivas Pandruvada 			ret_type = IIO_VAL_INT_MULTIPLE;
84fc18dddcSSrinivas Pandruvada 			*val_len =  4;
85fc18dddcSSrinivas Pandruvada 		} else
86fc18dddcSSrinivas Pandruvada 			ret_type = -EINVAL;
87fc18dddcSSrinivas Pandruvada 		break;
88a78587d3SSong Hongyan 	case IIO_CHAN_INFO_SCALE:
89a78587d3SSong Hongyan 		vals[0] = rot_state->scale_pre_decml;
90a78587d3SSong Hongyan 		vals[1] = rot_state->scale_post_decml;
91a78587d3SSong Hongyan 		return rot_state->scale_precision;
92a78587d3SSong Hongyan 
93a78587d3SSong Hongyan 	case IIO_CHAN_INFO_OFFSET:
94a78587d3SSong Hongyan 		*vals = rot_state->value_offset;
95a78587d3SSong Hongyan 		return IIO_VAL_INT;
96a78587d3SSong Hongyan 
97fc18dddcSSrinivas Pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
98fc18dddcSSrinivas Pandruvada 		ret_type = hid_sensor_read_samp_freq_value(
99fc18dddcSSrinivas Pandruvada 			&rot_state->common_attributes, &vals[0], &vals[1]);
100fc18dddcSSrinivas Pandruvada 		break;
101fc18dddcSSrinivas Pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
102fc18dddcSSrinivas Pandruvada 		ret_type = hid_sensor_read_raw_hyst_value(
103fc18dddcSSrinivas Pandruvada 			&rot_state->common_attributes, &vals[0], &vals[1]);
104fc18dddcSSrinivas Pandruvada 		break;
105fc18dddcSSrinivas Pandruvada 	default:
106fc18dddcSSrinivas Pandruvada 		ret_type = -EINVAL;
107fc18dddcSSrinivas Pandruvada 		break;
108fc18dddcSSrinivas Pandruvada 	}
109fc18dddcSSrinivas Pandruvada 
110fc18dddcSSrinivas Pandruvada 	return ret_type;
111fc18dddcSSrinivas Pandruvada }
112fc18dddcSSrinivas Pandruvada 
113fc18dddcSSrinivas Pandruvada /* Channel write_raw handler */
dev_rot_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)114fc18dddcSSrinivas Pandruvada static int dev_rot_write_raw(struct iio_dev *indio_dev,
115fc18dddcSSrinivas Pandruvada 			       struct iio_chan_spec const *chan,
116fc18dddcSSrinivas Pandruvada 			       int val,
117fc18dddcSSrinivas Pandruvada 			       int val2,
118fc18dddcSSrinivas Pandruvada 			       long mask)
119fc18dddcSSrinivas Pandruvada {
120fc18dddcSSrinivas Pandruvada 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
121fc18dddcSSrinivas Pandruvada 	int ret;
122fc18dddcSSrinivas Pandruvada 
123fc18dddcSSrinivas Pandruvada 	switch (mask) {
124fc18dddcSSrinivas Pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
125fc18dddcSSrinivas Pandruvada 		ret = hid_sensor_write_samp_freq_value(
126fc18dddcSSrinivas Pandruvada 				&rot_state->common_attributes, val, val2);
127fc18dddcSSrinivas Pandruvada 		break;
128fc18dddcSSrinivas Pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
129fc18dddcSSrinivas Pandruvada 		ret = hid_sensor_write_raw_hyst_value(
130fc18dddcSSrinivas Pandruvada 				&rot_state->common_attributes, val, val2);
131fc18dddcSSrinivas Pandruvada 		break;
132fc18dddcSSrinivas Pandruvada 	default:
133fc18dddcSSrinivas Pandruvada 		ret = -EINVAL;
134fc18dddcSSrinivas Pandruvada 	}
135fc18dddcSSrinivas Pandruvada 
136fc18dddcSSrinivas Pandruvada 	return ret;
137fc18dddcSSrinivas Pandruvada }
138fc18dddcSSrinivas Pandruvada 
139fc18dddcSSrinivas Pandruvada static const struct iio_info dev_rot_info = {
140fc18dddcSSrinivas Pandruvada 	.read_raw_multi = &dev_rot_read_raw,
141fc18dddcSSrinivas Pandruvada 	.write_raw = &dev_rot_write_raw,
142fc18dddcSSrinivas Pandruvada };
143fc18dddcSSrinivas Pandruvada 
144fc18dddcSSrinivas Pandruvada /* Callback handler to send event after all samples are received and captured */
dev_rot_proc_event(struct hid_sensor_hub_device * hsdev,unsigned usage_id,void * priv)145fc18dddcSSrinivas Pandruvada static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
146fc18dddcSSrinivas Pandruvada 				unsigned usage_id,
147fc18dddcSSrinivas Pandruvada 				void *priv)
148fc18dddcSSrinivas Pandruvada {
149fc18dddcSSrinivas Pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
150fc18dddcSSrinivas Pandruvada 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
151fc18dddcSSrinivas Pandruvada 
15256ff6be6SSrinivas Pandruvada 	dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
1534a3582c8SYe Xiang 	if (atomic_read(&rot_state->common_attributes.data_ready)) {
1544a3582c8SYe Xiang 		if (!rot_state->timestamp)
1554a3582c8SYe Xiang 			rot_state->timestamp = iio_get_time_ns(indio_dev);
1564a3582c8SYe Xiang 
1574a3582c8SYe Xiang 		iio_push_to_buffers_with_timestamp(indio_dev, &rot_state->scan,
1584a3582c8SYe Xiang 						   rot_state->timestamp);
1594a3582c8SYe Xiang 
1604a3582c8SYe Xiang 		rot_state->timestamp = 0;
1614a3582c8SYe Xiang 	}
162fc18dddcSSrinivas Pandruvada 
163fc18dddcSSrinivas Pandruvada 	return 0;
164fc18dddcSSrinivas Pandruvada }
165fc18dddcSSrinivas Pandruvada 
166fc18dddcSSrinivas Pandruvada /* Capture samples in local storage */
dev_rot_capture_sample(struct hid_sensor_hub_device * hsdev,unsigned usage_id,size_t raw_len,char * raw_data,void * priv)167fc18dddcSSrinivas Pandruvada static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
168fc18dddcSSrinivas Pandruvada 				unsigned usage_id,
169fc18dddcSSrinivas Pandruvada 				size_t raw_len, char *raw_data,
170fc18dddcSSrinivas Pandruvada 				void *priv)
171fc18dddcSSrinivas Pandruvada {
172fc18dddcSSrinivas Pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
173fc18dddcSSrinivas Pandruvada 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
174fc18dddcSSrinivas Pandruvada 
175fc18dddcSSrinivas Pandruvada 	if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
1766c3b6153SYe Xiang 		if (raw_len / 4 == sizeof(s16)) {
1776c3b6153SYe Xiang 			rot_state->scan.sampled_vals[0] = ((s16 *)raw_data)[0];
1786c3b6153SYe Xiang 			rot_state->scan.sampled_vals[1] = ((s16 *)raw_data)[1];
1796c3b6153SYe Xiang 			rot_state->scan.sampled_vals[2] = ((s16 *)raw_data)[2];
1806c3b6153SYe Xiang 			rot_state->scan.sampled_vals[3] = ((s16 *)raw_data)[3];
1816c3b6153SYe Xiang 		} else {
1824a3582c8SYe Xiang 			memcpy(&rot_state->scan.sampled_vals, raw_data,
1834a3582c8SYe Xiang 			       sizeof(rot_state->scan.sampled_vals));
1846c3b6153SYe Xiang 		}
1854a3582c8SYe Xiang 
186fc18dddcSSrinivas Pandruvada 		dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
1874a3582c8SYe Xiang 			sizeof(rot_state->scan.sampled_vals));
1884a3582c8SYe Xiang 	} else if (usage_id == HID_USAGE_SENSOR_TIME_TIMESTAMP) {
1894a3582c8SYe Xiang 		rot_state->timestamp = hid_sensor_convert_timestamp(&rot_state->common_attributes,
1904a3582c8SYe Xiang 								    *(s64 *)raw_data);
191fc18dddcSSrinivas Pandruvada 	}
192fc18dddcSSrinivas Pandruvada 
193fc18dddcSSrinivas Pandruvada 	return 0;
194fc18dddcSSrinivas Pandruvada }
195fc18dddcSSrinivas Pandruvada 
196fc18dddcSSrinivas Pandruvada /* Parse report which is specific to an usage id*/
dev_rot_parse_report(struct platform_device * pdev,struct hid_sensor_hub_device * hsdev,struct iio_chan_spec * channels,unsigned usage_id,struct dev_rot_state * st)197fc18dddcSSrinivas Pandruvada static int dev_rot_parse_report(struct platform_device *pdev,
198fc18dddcSSrinivas Pandruvada 				struct hid_sensor_hub_device *hsdev,
199fc18dddcSSrinivas Pandruvada 				struct iio_chan_spec *channels,
200fc18dddcSSrinivas Pandruvada 				unsigned usage_id,
201fc18dddcSSrinivas Pandruvada 				struct dev_rot_state *st)
202fc18dddcSSrinivas Pandruvada {
203fc18dddcSSrinivas Pandruvada 	int ret;
204fc18dddcSSrinivas Pandruvada 
205fc18dddcSSrinivas Pandruvada 	ret = sensor_hub_input_get_attribute_info(hsdev,
206fc18dddcSSrinivas Pandruvada 				HID_INPUT_REPORT,
207fc18dddcSSrinivas Pandruvada 				usage_id,
208fc18dddcSSrinivas Pandruvada 				HID_USAGE_SENSOR_ORIENT_QUATERNION,
209fc18dddcSSrinivas Pandruvada 				&st->quaternion);
210fc18dddcSSrinivas Pandruvada 	if (ret)
211fc18dddcSSrinivas Pandruvada 		return ret;
212fc18dddcSSrinivas Pandruvada 
213fc18dddcSSrinivas Pandruvada 	dev_rot_adjust_channel_bit_mask(&channels[0],
214fc18dddcSSrinivas Pandruvada 		st->quaternion.size / 4);
215fc18dddcSSrinivas Pandruvada 
216fc18dddcSSrinivas Pandruvada 	dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
217fc18dddcSSrinivas Pandruvada 		st->quaternion.report_id);
218fc18dddcSSrinivas Pandruvada 
219fc18dddcSSrinivas Pandruvada 	dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
220fc18dddcSSrinivas Pandruvada 				st->quaternion.size);
221fc18dddcSSrinivas Pandruvada 
222a78587d3SSong Hongyan 	st->scale_precision = hid_sensor_format_scale(
223a78587d3SSong Hongyan 				hsdev->usage,
224a78587d3SSong Hongyan 				&st->quaternion,
225a78587d3SSong Hongyan 				&st->scale_pre_decml, &st->scale_post_decml);
226a78587d3SSong Hongyan 
227fc18dddcSSrinivas Pandruvada 	return 0;
228fc18dddcSSrinivas Pandruvada }
229fc18dddcSSrinivas Pandruvada 
230fc18dddcSSrinivas Pandruvada /* Function to initialize the processing for usage id */
hid_dev_rot_probe(struct platform_device * pdev)231fc18dddcSSrinivas Pandruvada static int hid_dev_rot_probe(struct platform_device *pdev)
232fc18dddcSSrinivas Pandruvada {
233fc18dddcSSrinivas Pandruvada 	int ret;
23450db2cd4SJulia Lawall 	char *name;
235fc18dddcSSrinivas Pandruvada 	struct iio_dev *indio_dev;
236fc18dddcSSrinivas Pandruvada 	struct dev_rot_state *rot_state;
237fc18dddcSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
238fc18dddcSSrinivas Pandruvada 
239fc18dddcSSrinivas Pandruvada 	indio_dev = devm_iio_device_alloc(&pdev->dev,
240fc18dddcSSrinivas Pandruvada 					  sizeof(struct dev_rot_state));
241fc18dddcSSrinivas Pandruvada 	if (indio_dev == NULL)
242fc18dddcSSrinivas Pandruvada 		return -ENOMEM;
243fc18dddcSSrinivas Pandruvada 
244fc18dddcSSrinivas Pandruvada 	platform_set_drvdata(pdev, indio_dev);
245fc18dddcSSrinivas Pandruvada 
246fc18dddcSSrinivas Pandruvada 	rot_state = iio_priv(indio_dev);
247fc18dddcSSrinivas Pandruvada 	rot_state->common_attributes.hsdev = hsdev;
248fc18dddcSSrinivas Pandruvada 	rot_state->common_attributes.pdev = pdev;
249fc18dddcSSrinivas Pandruvada 
2509ff88edcSSong Hongyan 	switch (hsdev->usage) {
2519ff88edcSSong Hongyan 	case HID_USAGE_SENSOR_DEVICE_ORIENTATION:
2529ff88edcSSong Hongyan 		name = "dev_rotation";
2539ff88edcSSong Hongyan 		break;
2549ff88edcSSong Hongyan 	case HID_USAGE_SENSOR_RELATIVE_ORIENTATION:
2559ff88edcSSong Hongyan 		name = "relative_orientation";
2569ff88edcSSong Hongyan 		break;
25700907c7aSSong Hongyan 	case HID_USAGE_SENSOR_GEOMAGNETIC_ORIENTATION:
25800907c7aSSong Hongyan 		name = "geomagnetic_orientation";
25900907c7aSSong Hongyan 		break;
2609ff88edcSSong Hongyan 	default:
2619ff88edcSSong Hongyan 		return -EINVAL;
2629ff88edcSSong Hongyan 	}
2639ff88edcSSong Hongyan 
2640e41fd51SYe Xiang 	ret = hid_sensor_parse_common_attributes(hsdev,
2650e41fd51SYe Xiang 						 hsdev->usage,
2660e41fd51SYe Xiang 						 &rot_state->common_attributes,
2670e41fd51SYe Xiang 						 rotation_sensitivity_addresses,
2680e41fd51SYe Xiang 						 ARRAY_SIZE(rotation_sensitivity_addresses));
269fc18dddcSSrinivas Pandruvada 	if (ret) {
270fc18dddcSSrinivas Pandruvada 		dev_err(&pdev->dev, "failed to setup common attributes\n");
271fc18dddcSSrinivas Pandruvada 		return ret;
272fc18dddcSSrinivas Pandruvada 	}
273fc18dddcSSrinivas Pandruvada 
2742bd04628SFabio Estevam 	indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
2752bd04628SFabio Estevam 					   sizeof(dev_rot_channels),
2762bd04628SFabio Estevam 					   GFP_KERNEL);
2772bd04628SFabio Estevam 	if (!indio_dev->channels) {
278fc18dddcSSrinivas Pandruvada 		dev_err(&pdev->dev, "failed to duplicate channels\n");
279fc18dddcSSrinivas Pandruvada 		return -ENOMEM;
280fc18dddcSSrinivas Pandruvada 	}
281fc18dddcSSrinivas Pandruvada 
2822bd04628SFabio Estevam 	ret = dev_rot_parse_report(pdev, hsdev,
2832bd04628SFabio Estevam 				   (struct iio_chan_spec *)indio_dev->channels,
2849ff88edcSSong Hongyan 					hsdev->usage, rot_state);
285fc18dddcSSrinivas Pandruvada 	if (ret) {
286fc18dddcSSrinivas Pandruvada 		dev_err(&pdev->dev, "failed to setup attributes\n");
287fc18dddcSSrinivas Pandruvada 		return ret;
288fc18dddcSSrinivas Pandruvada 	}
289fc18dddcSSrinivas Pandruvada 
290fc18dddcSSrinivas Pandruvada 	indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
291fc18dddcSSrinivas Pandruvada 	indio_dev->info = &dev_rot_info;
292fc18dddcSSrinivas Pandruvada 	indio_dev->name = name;
293fc18dddcSSrinivas Pandruvada 	indio_dev->modes = INDIO_DIRECT_MODE;
294fc18dddcSSrinivas Pandruvada 
29556ff6be6SSrinivas Pandruvada 	atomic_set(&rot_state->common_attributes.data_ready, 0);
296067fda1cSAlexandru Ardelean 
297fc18dddcSSrinivas Pandruvada 	ret = hid_sensor_setup_trigger(indio_dev, name,
298fc18dddcSSrinivas Pandruvada 					&rot_state->common_attributes);
299fc18dddcSSrinivas Pandruvada 	if (ret) {
300fc18dddcSSrinivas Pandruvada 		dev_err(&pdev->dev, "trigger setup failed\n");
301067fda1cSAlexandru Ardelean 		return ret;
302fc18dddcSSrinivas Pandruvada 	}
303fc18dddcSSrinivas Pandruvada 
304fc18dddcSSrinivas Pandruvada 	ret = iio_device_register(indio_dev);
305fc18dddcSSrinivas Pandruvada 	if (ret) {
306fc18dddcSSrinivas Pandruvada 		dev_err(&pdev->dev, "device register failed\n");
307fc18dddcSSrinivas Pandruvada 		goto error_remove_trigger;
308fc18dddcSSrinivas Pandruvada 	}
309fc18dddcSSrinivas Pandruvada 
310fc18dddcSSrinivas Pandruvada 	rot_state->callbacks.send_event = dev_rot_proc_event;
311fc18dddcSSrinivas Pandruvada 	rot_state->callbacks.capture_sample = dev_rot_capture_sample;
312fc18dddcSSrinivas Pandruvada 	rot_state->callbacks.pdev = pdev;
3139ff88edcSSong Hongyan 	ret = sensor_hub_register_callback(hsdev, hsdev->usage,
314fc18dddcSSrinivas Pandruvada 					&rot_state->callbacks);
315fc18dddcSSrinivas Pandruvada 	if (ret) {
316fc18dddcSSrinivas Pandruvada 		dev_err(&pdev->dev, "callback reg failed\n");
317fc18dddcSSrinivas Pandruvada 		goto error_iio_unreg;
318fc18dddcSSrinivas Pandruvada 	}
319fc18dddcSSrinivas Pandruvada 
320fc18dddcSSrinivas Pandruvada 	return 0;
321fc18dddcSSrinivas Pandruvada 
322fc18dddcSSrinivas Pandruvada error_iio_unreg:
323fc18dddcSSrinivas Pandruvada 	iio_device_unregister(indio_dev);
324fc18dddcSSrinivas Pandruvada error_remove_trigger:
325067fda1cSAlexandru Ardelean 	hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
326fc18dddcSSrinivas Pandruvada 	return ret;
327fc18dddcSSrinivas Pandruvada }
328fc18dddcSSrinivas Pandruvada 
329fc18dddcSSrinivas Pandruvada /* Function to deinitialize the processing for usage id */
hid_dev_rot_remove(struct platform_device * pdev)330fc18dddcSSrinivas Pandruvada static int hid_dev_rot_remove(struct platform_device *pdev)
331fc18dddcSSrinivas Pandruvada {
332fc18dddcSSrinivas Pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
333fc18dddcSSrinivas Pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
334fc18dddcSSrinivas Pandruvada 	struct dev_rot_state *rot_state = iio_priv(indio_dev);
335fc18dddcSSrinivas Pandruvada 
3369ff88edcSSong Hongyan 	sensor_hub_remove_callback(hsdev, hsdev->usage);
337fc18dddcSSrinivas Pandruvada 	iio_device_unregister(indio_dev);
338067fda1cSAlexandru Ardelean 	hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes);
339fc18dddcSSrinivas Pandruvada 
340fc18dddcSSrinivas Pandruvada 	return 0;
341fc18dddcSSrinivas Pandruvada }
342fc18dddcSSrinivas Pandruvada 
3436b490c6cSKrzysztof Kozlowski static const struct platform_device_id hid_dev_rot_ids[] = {
344fc18dddcSSrinivas Pandruvada 	{
345fc18dddcSSrinivas Pandruvada 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
346fc18dddcSSrinivas Pandruvada 		.name = "HID-SENSOR-20008a",
347fc18dddcSSrinivas Pandruvada 	},
3489ff88edcSSong Hongyan 	{
3499ff88edcSSong Hongyan 		/* Relative orientation(AG) sensor */
3509ff88edcSSong Hongyan 		.name = "HID-SENSOR-20008e",
3519ff88edcSSong Hongyan 	},
35200907c7aSSong Hongyan 	{
35300907c7aSSong Hongyan 		/* Geomagnetic orientation(AM) sensor */
35400907c7aSSong Hongyan 		.name = "HID-SENSOR-2000c1",
35500907c7aSSong Hongyan 	},
356fc18dddcSSrinivas Pandruvada 	{ /* sentinel */ }
357fc18dddcSSrinivas Pandruvada };
358fc18dddcSSrinivas Pandruvada MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
359fc18dddcSSrinivas Pandruvada 
360fc18dddcSSrinivas Pandruvada static struct platform_driver hid_dev_rot_platform_driver = {
361fc18dddcSSrinivas Pandruvada 	.id_table = hid_dev_rot_ids,
362fc18dddcSSrinivas Pandruvada 	.driver = {
363fc18dddcSSrinivas Pandruvada 		.name	= KBUILD_MODNAME,
3648af644a7SSong Hongyan 		.pm     = &hid_sensor_pm_ops,
365fc18dddcSSrinivas Pandruvada 	},
366fc18dddcSSrinivas Pandruvada 	.probe		= hid_dev_rot_probe,
367fc18dddcSSrinivas Pandruvada 	.remove		= hid_dev_rot_remove,
368fc18dddcSSrinivas Pandruvada };
369fc18dddcSSrinivas Pandruvada module_platform_driver(hid_dev_rot_platform_driver);
370fc18dddcSSrinivas Pandruvada 
371fc18dddcSSrinivas Pandruvada MODULE_DESCRIPTION("HID Sensor Device Rotation");
372fc18dddcSSrinivas Pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
373fc18dddcSSrinivas Pandruvada MODULE_LICENSE("GPL");
37412f13d1fSAndy Shevchenko MODULE_IMPORT_NS(IIO_HID);
375