xref: /openbmc/linux/drivers/iio/gyro/hid-sensor-gyro-3d.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c5bdbef7Ssrinivas pandruvada /*
3c5bdbef7Ssrinivas pandruvada  * HID Sensors Driver
4c5bdbef7Ssrinivas pandruvada  * Copyright (c) 2012, Intel Corporation.
5c5bdbef7Ssrinivas pandruvada  */
6c5bdbef7Ssrinivas pandruvada #include <linux/device.h>
7c5bdbef7Ssrinivas pandruvada #include <linux/platform_device.h>
8c5bdbef7Ssrinivas pandruvada #include <linux/module.h>
9fb226ae7SJonathan Cameron #include <linux/mod_devicetable.h>
10c5bdbef7Ssrinivas pandruvada #include <linux/slab.h>
11c5bdbef7Ssrinivas pandruvada #include <linux/hid-sensor-hub.h>
12c5bdbef7Ssrinivas pandruvada #include <linux/iio/iio.h>
13c5bdbef7Ssrinivas pandruvada #include <linux/iio/buffer.h>
14c5bdbef7Ssrinivas pandruvada #include "../common/hid-sensors/hid-sensor-trigger.h"
15c5bdbef7Ssrinivas pandruvada 
16c5bdbef7Ssrinivas pandruvada enum gyro_3d_channel {
17c5bdbef7Ssrinivas pandruvada 	CHANNEL_SCAN_INDEX_X,
18c5bdbef7Ssrinivas pandruvada 	CHANNEL_SCAN_INDEX_Y,
19c5bdbef7Ssrinivas pandruvada 	CHANNEL_SCAN_INDEX_Z,
20c5bdbef7Ssrinivas pandruvada 	GYRO_3D_CHANNEL_MAX,
21c5bdbef7Ssrinivas pandruvada };
22c5bdbef7Ssrinivas pandruvada 
234648cbd8SYe Xiang #define CHANNEL_SCAN_INDEX_TIMESTAMP GYRO_3D_CHANNEL_MAX
24c5bdbef7Ssrinivas pandruvada struct gyro_3d_state {
25c5bdbef7Ssrinivas pandruvada 	struct hid_sensor_hub_callbacks callbacks;
26e07c6d17SAlexander Holler 	struct hid_sensor_common common_attributes;
27c5bdbef7Ssrinivas pandruvada 	struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
284648cbd8SYe Xiang 	struct {
29c5bdbef7Ssrinivas pandruvada 		u32 gyro_val[GYRO_3D_CHANNEL_MAX];
304648cbd8SYe Xiang 		u64 timestamp __aligned(8);
314648cbd8SYe Xiang 	} scan;
326c7db77eSSrinivas Pandruvada 	int scale_pre_decml;
336c7db77eSSrinivas Pandruvada 	int scale_post_decml;
346c7db77eSSrinivas Pandruvada 	int scale_precision;
356c7db77eSSrinivas Pandruvada 	int value_offset;
364648cbd8SYe Xiang 	s64 timestamp;
37c5bdbef7Ssrinivas pandruvada };
38c5bdbef7Ssrinivas pandruvada 
39c5bdbef7Ssrinivas pandruvada static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
40c5bdbef7Ssrinivas pandruvada 	HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS,
41c5bdbef7Ssrinivas pandruvada 	HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS,
42c5bdbef7Ssrinivas pandruvada 	HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS
43c5bdbef7Ssrinivas pandruvada };
44c5bdbef7Ssrinivas pandruvada 
450e41fd51SYe Xiang static const u32 gryo_3d_sensitivity_addresses[] = {
460e41fd51SYe Xiang 	HID_USAGE_SENSOR_DATA_ANGL_VELOCITY,
470e41fd51SYe Xiang };
480e41fd51SYe Xiang 
49c5bdbef7Ssrinivas pandruvada /* Channel definitions */
50c5bdbef7Ssrinivas pandruvada static const struct iio_chan_spec gyro_3d_channels[] = {
51c5bdbef7Ssrinivas pandruvada 	{
52c5bdbef7Ssrinivas pandruvada 		.type = IIO_ANGL_VEL,
53c5bdbef7Ssrinivas pandruvada 		.modified = 1,
54c5bdbef7Ssrinivas pandruvada 		.channel2 = IIO_MOD_X,
550964f706SSrinivas Pandruvada 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
56ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
57ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
58ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
59ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
60c5bdbef7Ssrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_X,
61c5bdbef7Ssrinivas pandruvada 	}, {
62c5bdbef7Ssrinivas pandruvada 		.type = IIO_ANGL_VEL,
63c5bdbef7Ssrinivas pandruvada 		.modified = 1,
64c5bdbef7Ssrinivas pandruvada 		.channel2 = IIO_MOD_Y,
650964f706SSrinivas Pandruvada 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
66ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
67ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
68ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
69ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
70c5bdbef7Ssrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_Y,
71c5bdbef7Ssrinivas pandruvada 	}, {
72c5bdbef7Ssrinivas pandruvada 		.type = IIO_ANGL_VEL,
73c5bdbef7Ssrinivas pandruvada 		.modified = 1,
74c5bdbef7Ssrinivas pandruvada 		.channel2 = IIO_MOD_Z,
750964f706SSrinivas Pandruvada 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
76ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
77ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
78ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
79ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
80c5bdbef7Ssrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_Z,
814648cbd8SYe Xiang 	},
824648cbd8SYe Xiang 	IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP)
83c5bdbef7Ssrinivas pandruvada };
84c5bdbef7Ssrinivas pandruvada 
85c5bdbef7Ssrinivas pandruvada /* Adjust channel real bits based on report descriptor */
gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec * channels,int channel,int size)86c5bdbef7Ssrinivas pandruvada static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
87c5bdbef7Ssrinivas pandruvada 						int channel, int size)
88c5bdbef7Ssrinivas pandruvada {
89c5bdbef7Ssrinivas pandruvada 	channels[channel].scan_type.sign = 's';
90c5bdbef7Ssrinivas pandruvada 	/* Real storage bits will change based on the report desc. */
91c5bdbef7Ssrinivas pandruvada 	channels[channel].scan_type.realbits = size * 8;
92c5bdbef7Ssrinivas pandruvada 	/* Maximum size of a sample to capture is u32 */
93c5bdbef7Ssrinivas pandruvada 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
94c5bdbef7Ssrinivas pandruvada }
95c5bdbef7Ssrinivas pandruvada 
96c5bdbef7Ssrinivas pandruvada /* Channel read_raw handler */
gyro_3d_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)97c5bdbef7Ssrinivas pandruvada static int gyro_3d_read_raw(struct iio_dev *indio_dev,
98c5bdbef7Ssrinivas pandruvada 			      struct iio_chan_spec const *chan,
99c5bdbef7Ssrinivas pandruvada 			      int *val, int *val2,
100c5bdbef7Ssrinivas pandruvada 			      long mask)
101c5bdbef7Ssrinivas pandruvada {
102c5bdbef7Ssrinivas pandruvada 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
103c5bdbef7Ssrinivas pandruvada 	int report_id = -1;
104c5bdbef7Ssrinivas pandruvada 	u32 address;
105c5bdbef7Ssrinivas pandruvada 	int ret_type;
1060145b505SHans de Goede 	s32 min;
107c5bdbef7Ssrinivas pandruvada 
108c5bdbef7Ssrinivas pandruvada 	*val = 0;
109c5bdbef7Ssrinivas pandruvada 	*val2 = 0;
110c5bdbef7Ssrinivas pandruvada 	switch (mask) {
11167464a54SRodrigo Siqueira 	case IIO_CHAN_INFO_RAW:
1120964f706SSrinivas Pandruvada 		hid_sensor_power_state(&gyro_state->common_attributes, true);
113c5bdbef7Ssrinivas pandruvada 		report_id = gyro_state->gyro[chan->scan_index].report_id;
1140145b505SHans de Goede 		min = gyro_state->gyro[chan->scan_index].logical_minimum;
115c5bdbef7Ssrinivas pandruvada 		address = gyro_3d_addresses[chan->scan_index];
116c5bdbef7Ssrinivas pandruvada 		if (report_id >= 0)
117c5bdbef7Ssrinivas pandruvada 			*val = sensor_hub_input_attr_get_raw_value(
118c5bdbef7Ssrinivas pandruvada 					gyro_state->common_attributes.hsdev,
119c5bdbef7Ssrinivas pandruvada 					HID_USAGE_SENSOR_GYRO_3D, address,
120b3f4737dSSrinivas Pandruvada 					report_id,
1210145b505SHans de Goede 					SENSOR_HUB_SYNC,
1220145b505SHans de Goede 					min < 0);
123c5bdbef7Ssrinivas pandruvada 		else {
124c5bdbef7Ssrinivas pandruvada 			*val = 0;
1250964f706SSrinivas Pandruvada 			hid_sensor_power_state(&gyro_state->common_attributes,
1260964f706SSrinivas Pandruvada 						false);
127c5bdbef7Ssrinivas pandruvada 			return -EINVAL;
128c5bdbef7Ssrinivas pandruvada 		}
1290964f706SSrinivas Pandruvada 		hid_sensor_power_state(&gyro_state->common_attributes, false);
130c5bdbef7Ssrinivas pandruvada 		ret_type = IIO_VAL_INT;
131c5bdbef7Ssrinivas pandruvada 		break;
132c5bdbef7Ssrinivas pandruvada 	case IIO_CHAN_INFO_SCALE:
1336c7db77eSSrinivas Pandruvada 		*val = gyro_state->scale_pre_decml;
1346c7db77eSSrinivas Pandruvada 		*val2 = gyro_state->scale_post_decml;
1356c7db77eSSrinivas Pandruvada 		ret_type = gyro_state->scale_precision;
136c5bdbef7Ssrinivas pandruvada 		break;
137c5bdbef7Ssrinivas pandruvada 	case IIO_CHAN_INFO_OFFSET:
1386c7db77eSSrinivas Pandruvada 		*val = gyro_state->value_offset;
139c5bdbef7Ssrinivas pandruvada 		ret_type = IIO_VAL_INT;
140c5bdbef7Ssrinivas pandruvada 		break;
141c5bdbef7Ssrinivas pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
14227793803SSachin Kamat 		ret_type = hid_sensor_read_samp_freq_value(
143c5bdbef7Ssrinivas pandruvada 			&gyro_state->common_attributes, val, val2);
144c5bdbef7Ssrinivas pandruvada 		break;
145c5bdbef7Ssrinivas pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
14627793803SSachin Kamat 		ret_type = hid_sensor_read_raw_hyst_value(
147c5bdbef7Ssrinivas pandruvada 			&gyro_state->common_attributes, val, val2);
148c5bdbef7Ssrinivas pandruvada 		break;
149c5bdbef7Ssrinivas pandruvada 	default:
150c5bdbef7Ssrinivas pandruvada 		ret_type = -EINVAL;
151c5bdbef7Ssrinivas pandruvada 		break;
152c5bdbef7Ssrinivas pandruvada 	}
153c5bdbef7Ssrinivas pandruvada 
154c5bdbef7Ssrinivas pandruvada 	return ret_type;
155c5bdbef7Ssrinivas pandruvada }
156c5bdbef7Ssrinivas pandruvada 
157c5bdbef7Ssrinivas pandruvada /* Channel write_raw handler */
gyro_3d_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)158c5bdbef7Ssrinivas pandruvada static int gyro_3d_write_raw(struct iio_dev *indio_dev,
159c5bdbef7Ssrinivas pandruvada 			       struct iio_chan_spec const *chan,
160c5bdbef7Ssrinivas pandruvada 			       int val,
161c5bdbef7Ssrinivas pandruvada 			       int val2,
162c5bdbef7Ssrinivas pandruvada 			       long mask)
163c5bdbef7Ssrinivas pandruvada {
164c5bdbef7Ssrinivas pandruvada 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
165c5bdbef7Ssrinivas pandruvada 	int ret = 0;
166c5bdbef7Ssrinivas pandruvada 
167c5bdbef7Ssrinivas pandruvada 	switch (mask) {
168c5bdbef7Ssrinivas pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
169c5bdbef7Ssrinivas pandruvada 		ret = hid_sensor_write_samp_freq_value(
170c5bdbef7Ssrinivas pandruvada 				&gyro_state->common_attributes, val, val2);
171c5bdbef7Ssrinivas pandruvada 		break;
172c5bdbef7Ssrinivas pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
173c5bdbef7Ssrinivas pandruvada 		ret = hid_sensor_write_raw_hyst_value(
174c5bdbef7Ssrinivas pandruvada 				&gyro_state->common_attributes, val, val2);
175c5bdbef7Ssrinivas pandruvada 		break;
176c5bdbef7Ssrinivas pandruvada 	default:
177c5bdbef7Ssrinivas pandruvada 		ret = -EINVAL;
178c5bdbef7Ssrinivas pandruvada 	}
179c5bdbef7Ssrinivas pandruvada 
180c5bdbef7Ssrinivas pandruvada 	return ret;
181c5bdbef7Ssrinivas pandruvada }
182c5bdbef7Ssrinivas pandruvada 
183c5bdbef7Ssrinivas pandruvada static const struct iio_info gyro_3d_info = {
184c5bdbef7Ssrinivas pandruvada 	.read_raw = &gyro_3d_read_raw,
185c5bdbef7Ssrinivas pandruvada 	.write_raw = &gyro_3d_write_raw,
186c5bdbef7Ssrinivas pandruvada };
187c5bdbef7Ssrinivas pandruvada 
188c5bdbef7Ssrinivas pandruvada /* Callback handler to send event after all samples are received and captured */
gyro_3d_proc_event(struct hid_sensor_hub_device * hsdev,unsigned usage_id,void * priv)189c5bdbef7Ssrinivas pandruvada static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
190c5bdbef7Ssrinivas pandruvada 				unsigned usage_id,
191c5bdbef7Ssrinivas pandruvada 				void *priv)
192c5bdbef7Ssrinivas pandruvada {
193c5bdbef7Ssrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
194c5bdbef7Ssrinivas pandruvada 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
195c5bdbef7Ssrinivas pandruvada 
19656ff6be6SSrinivas Pandruvada 	dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
1974648cbd8SYe Xiang 	if (atomic_read(&gyro_state->common_attributes.data_ready)) {
1984648cbd8SYe Xiang 		if (!gyro_state->timestamp)
1994648cbd8SYe Xiang 			gyro_state->timestamp = iio_get_time_ns(indio_dev);
2004648cbd8SYe Xiang 
2014648cbd8SYe Xiang 		iio_push_to_buffers_with_timestamp(indio_dev, &gyro_state->scan,
2024648cbd8SYe Xiang 						   gyro_state->timestamp);
2034648cbd8SYe Xiang 
2044648cbd8SYe Xiang 		gyro_state->timestamp = 0;
2054648cbd8SYe Xiang 	}
206c5bdbef7Ssrinivas pandruvada 
207c5bdbef7Ssrinivas pandruvada 	return 0;
208c5bdbef7Ssrinivas pandruvada }
209c5bdbef7Ssrinivas pandruvada 
210c5bdbef7Ssrinivas pandruvada /* Capture samples in local storage */
gyro_3d_capture_sample(struct hid_sensor_hub_device * hsdev,unsigned usage_id,size_t raw_len,char * raw_data,void * priv)211c5bdbef7Ssrinivas pandruvada static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
212c5bdbef7Ssrinivas pandruvada 				unsigned usage_id,
213c5bdbef7Ssrinivas pandruvada 				size_t raw_len, char *raw_data,
214c5bdbef7Ssrinivas pandruvada 				void *priv)
215c5bdbef7Ssrinivas pandruvada {
216c5bdbef7Ssrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
217c5bdbef7Ssrinivas pandruvada 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
218c5bdbef7Ssrinivas pandruvada 	int offset;
219c5bdbef7Ssrinivas pandruvada 	int ret = -EINVAL;
220c5bdbef7Ssrinivas pandruvada 
221c5bdbef7Ssrinivas pandruvada 	switch (usage_id) {
222c5bdbef7Ssrinivas pandruvada 	case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS:
223c5bdbef7Ssrinivas pandruvada 	case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS:
224c5bdbef7Ssrinivas pandruvada 	case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
225c5bdbef7Ssrinivas pandruvada 		offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
2264648cbd8SYe Xiang 		gyro_state->scan.gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
227c5bdbef7Ssrinivas pandruvada 				*(u32 *)raw_data;
228c5bdbef7Ssrinivas pandruvada 		ret = 0;
229c5bdbef7Ssrinivas pandruvada 	break;
2304648cbd8SYe Xiang 	case HID_USAGE_SENSOR_TIME_TIMESTAMP:
2314648cbd8SYe Xiang 		gyro_state->timestamp =
2324648cbd8SYe Xiang 			hid_sensor_convert_timestamp(&gyro_state->common_attributes,
2334648cbd8SYe Xiang 						     *(s64 *)raw_data);
234*eb50cd5bSDmitry Perchanov 		ret = 0;
2354648cbd8SYe Xiang 	break;
236c5bdbef7Ssrinivas pandruvada 	default:
237c5bdbef7Ssrinivas pandruvada 		break;
238c5bdbef7Ssrinivas pandruvada 	}
239c5bdbef7Ssrinivas pandruvada 
240c5bdbef7Ssrinivas pandruvada 	return ret;
241c5bdbef7Ssrinivas pandruvada }
242c5bdbef7Ssrinivas pandruvada 
243c5bdbef7Ssrinivas pandruvada /* Parse report which is specific to an usage id*/
gyro_3d_parse_report(struct platform_device * pdev,struct hid_sensor_hub_device * hsdev,struct iio_chan_spec * channels,unsigned usage_id,struct gyro_3d_state * st)244c5bdbef7Ssrinivas pandruvada static int gyro_3d_parse_report(struct platform_device *pdev,
245c5bdbef7Ssrinivas pandruvada 				struct hid_sensor_hub_device *hsdev,
246c5bdbef7Ssrinivas pandruvada 				struct iio_chan_spec *channels,
247c5bdbef7Ssrinivas pandruvada 				unsigned usage_id,
248c5bdbef7Ssrinivas pandruvada 				struct gyro_3d_state *st)
249c5bdbef7Ssrinivas pandruvada {
250c5bdbef7Ssrinivas pandruvada 	int ret;
251c5bdbef7Ssrinivas pandruvada 	int i;
252c5bdbef7Ssrinivas pandruvada 
253c5bdbef7Ssrinivas pandruvada 	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
254c5bdbef7Ssrinivas pandruvada 		ret = sensor_hub_input_get_attribute_info(hsdev,
255c5bdbef7Ssrinivas pandruvada 				HID_INPUT_REPORT,
256c5bdbef7Ssrinivas pandruvada 				usage_id,
257c5bdbef7Ssrinivas pandruvada 				HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,
258c5bdbef7Ssrinivas pandruvada 				&st->gyro[CHANNEL_SCAN_INDEX_X + i]);
259c5bdbef7Ssrinivas pandruvada 		if (ret < 0)
260c5bdbef7Ssrinivas pandruvada 			break;
261c5bdbef7Ssrinivas pandruvada 		gyro_3d_adjust_channel_bit_mask(channels,
262c5bdbef7Ssrinivas pandruvada 				CHANNEL_SCAN_INDEX_X + i,
263c5bdbef7Ssrinivas pandruvada 				st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
264c5bdbef7Ssrinivas pandruvada 	}
265c5bdbef7Ssrinivas pandruvada 	dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n",
266c5bdbef7Ssrinivas pandruvada 			st->gyro[0].index,
267c5bdbef7Ssrinivas pandruvada 			st->gyro[0].report_id,
268c5bdbef7Ssrinivas pandruvada 			st->gyro[1].index, st->gyro[1].report_id,
269c5bdbef7Ssrinivas pandruvada 			st->gyro[2].index, st->gyro[2].report_id);
270c5bdbef7Ssrinivas pandruvada 
2716c7db77eSSrinivas Pandruvada 	st->scale_precision = hid_sensor_format_scale(
2726c7db77eSSrinivas Pandruvada 				HID_USAGE_SENSOR_GYRO_3D,
2736c7db77eSSrinivas Pandruvada 				&st->gyro[CHANNEL_SCAN_INDEX_X],
2746c7db77eSSrinivas Pandruvada 				&st->scale_pre_decml, &st->scale_post_decml);
2756c7db77eSSrinivas Pandruvada 
276c5bdbef7Ssrinivas pandruvada 	return ret;
277c5bdbef7Ssrinivas pandruvada }
278c5bdbef7Ssrinivas pandruvada 
279c5bdbef7Ssrinivas pandruvada /* Function to initialize the processing for usage id */
hid_gyro_3d_probe(struct platform_device * pdev)280fc52692cSGreg Kroah-Hartman static int hid_gyro_3d_probe(struct platform_device *pdev)
281c5bdbef7Ssrinivas pandruvada {
282c5bdbef7Ssrinivas pandruvada 	int ret = 0;
283c5bdbef7Ssrinivas pandruvada 	static const char *name = "gyro_3d";
284c5bdbef7Ssrinivas pandruvada 	struct iio_dev *indio_dev;
285c5bdbef7Ssrinivas pandruvada 	struct gyro_3d_state *gyro_state;
286c5bdbef7Ssrinivas pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
287c5bdbef7Ssrinivas pandruvada 
2881ba15cf2SSachin Kamat 	indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
2891ba15cf2SSachin Kamat 	if (!indio_dev)
2901ba15cf2SSachin Kamat 		return -ENOMEM;
291c5bdbef7Ssrinivas pandruvada 	platform_set_drvdata(pdev, indio_dev);
292c5bdbef7Ssrinivas pandruvada 
293c5bdbef7Ssrinivas pandruvada 	gyro_state = iio_priv(indio_dev);
294c5bdbef7Ssrinivas pandruvada 	gyro_state->common_attributes.hsdev = hsdev;
295c5bdbef7Ssrinivas pandruvada 	gyro_state->common_attributes.pdev = pdev;
296c5bdbef7Ssrinivas pandruvada 
297c5bdbef7Ssrinivas pandruvada 	ret = hid_sensor_parse_common_attributes(hsdev,
298c5bdbef7Ssrinivas pandruvada 						HID_USAGE_SENSOR_GYRO_3D,
2990e41fd51SYe Xiang 						&gyro_state->common_attributes,
3000e41fd51SYe Xiang 						gryo_3d_sensitivity_addresses,
3010e41fd51SYe Xiang 						ARRAY_SIZE(gryo_3d_sensitivity_addresses));
302c5bdbef7Ssrinivas pandruvada 	if (ret) {
303c5bdbef7Ssrinivas pandruvada 		dev_err(&pdev->dev, "failed to setup common attributes\n");
3041ba15cf2SSachin Kamat 		return ret;
305c5bdbef7Ssrinivas pandruvada 	}
306c5bdbef7Ssrinivas pandruvada 
3078b2ac516SAlexandru Ardelean 	indio_dev->channels = devm_kmemdup(&pdev->dev, gyro_3d_channels,
308d8c9d23eSFabio Estevam 					   sizeof(gyro_3d_channels), GFP_KERNEL);
309d8c9d23eSFabio Estevam 	if (!indio_dev->channels) {
310c5bdbef7Ssrinivas pandruvada 		dev_err(&pdev->dev, "failed to duplicate channels\n");
3111ba15cf2SSachin Kamat 		return -ENOMEM;
312c5bdbef7Ssrinivas pandruvada 	}
313c5bdbef7Ssrinivas pandruvada 
314d8c9d23eSFabio Estevam 	ret = gyro_3d_parse_report(pdev, hsdev,
315d8c9d23eSFabio Estevam 				   (struct iio_chan_spec *)indio_dev->channels,
316c5bdbef7Ssrinivas pandruvada 				   HID_USAGE_SENSOR_GYRO_3D, gyro_state);
317c5bdbef7Ssrinivas pandruvada 	if (ret) {
318c5bdbef7Ssrinivas pandruvada 		dev_err(&pdev->dev, "failed to setup attributes\n");
3198b2ac516SAlexandru Ardelean 		return ret;
320c5bdbef7Ssrinivas pandruvada 	}
321c5bdbef7Ssrinivas pandruvada 
322c5bdbef7Ssrinivas pandruvada 	indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels);
323c5bdbef7Ssrinivas pandruvada 	indio_dev->info = &gyro_3d_info;
324c5bdbef7Ssrinivas pandruvada 	indio_dev->name = name;
325c5bdbef7Ssrinivas pandruvada 	indio_dev->modes = INDIO_DIRECT_MODE;
326c5bdbef7Ssrinivas pandruvada 
32756ff6be6SSrinivas Pandruvada 	atomic_set(&gyro_state->common_attributes.data_ready, 0);
328067fda1cSAlexandru Ardelean 
329c5bdbef7Ssrinivas pandruvada 	ret = hid_sensor_setup_trigger(indio_dev, name,
330c5bdbef7Ssrinivas pandruvada 					&gyro_state->common_attributes);
331c5bdbef7Ssrinivas pandruvada 	if (ret < 0) {
332c5bdbef7Ssrinivas pandruvada 		dev_err(&pdev->dev, "trigger setup failed\n");
3338b2ac516SAlexandru Ardelean 		return ret;
334c5bdbef7Ssrinivas pandruvada 	}
335c5bdbef7Ssrinivas pandruvada 
336c5bdbef7Ssrinivas pandruvada 	ret = iio_device_register(indio_dev);
337c5bdbef7Ssrinivas pandruvada 	if (ret) {
338c5bdbef7Ssrinivas pandruvada 		dev_err(&pdev->dev, "device register failed\n");
339c5bdbef7Ssrinivas pandruvada 		goto error_remove_trigger;
340c5bdbef7Ssrinivas pandruvada 	}
341c5bdbef7Ssrinivas pandruvada 
342c5bdbef7Ssrinivas pandruvada 	gyro_state->callbacks.send_event = gyro_3d_proc_event;
343c5bdbef7Ssrinivas pandruvada 	gyro_state->callbacks.capture_sample = gyro_3d_capture_sample;
344c5bdbef7Ssrinivas pandruvada 	gyro_state->callbacks.pdev = pdev;
345c5bdbef7Ssrinivas pandruvada 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D,
346c5bdbef7Ssrinivas pandruvada 					&gyro_state->callbacks);
347c5bdbef7Ssrinivas pandruvada 	if (ret < 0) {
348c5bdbef7Ssrinivas pandruvada 		dev_err(&pdev->dev, "callback reg failed\n");
349c5bdbef7Ssrinivas pandruvada 		goto error_iio_unreg;
350c5bdbef7Ssrinivas pandruvada 	}
351c5bdbef7Ssrinivas pandruvada 
352c5bdbef7Ssrinivas pandruvada 	return ret;
353c5bdbef7Ssrinivas pandruvada 
354c5bdbef7Ssrinivas pandruvada error_iio_unreg:
355c5bdbef7Ssrinivas pandruvada 	iio_device_unregister(indio_dev);
356c5bdbef7Ssrinivas pandruvada error_remove_trigger:
357067fda1cSAlexandru Ardelean 	hid_sensor_remove_trigger(indio_dev, &gyro_state->common_attributes);
358c5bdbef7Ssrinivas pandruvada 	return ret;
359c5bdbef7Ssrinivas pandruvada }
360c5bdbef7Ssrinivas pandruvada 
361c5bdbef7Ssrinivas pandruvada /* Function to deinitialize the processing for usage id */
hid_gyro_3d_remove(struct platform_device * pdev)362fc52692cSGreg Kroah-Hartman static int hid_gyro_3d_remove(struct platform_device *pdev)
363c5bdbef7Ssrinivas pandruvada {
364c5bdbef7Ssrinivas pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
365c5bdbef7Ssrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
366ec7f68e0SSrinivas Pandruvada 	struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
367c5bdbef7Ssrinivas pandruvada 
368c5bdbef7Ssrinivas pandruvada 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D);
369c5bdbef7Ssrinivas pandruvada 	iio_device_unregister(indio_dev);
370067fda1cSAlexandru Ardelean 	hid_sensor_remove_trigger(indio_dev, &gyro_state->common_attributes);
371c5bdbef7Ssrinivas pandruvada 
372c5bdbef7Ssrinivas pandruvada 	return 0;
373c5bdbef7Ssrinivas pandruvada }
374c5bdbef7Ssrinivas pandruvada 
375322a3b1eSKrzysztof Kozlowski static const struct platform_device_id hid_gyro_3d_ids[] = {
376a411e735SAlexander Holler 	{
377a411e735SAlexander Holler 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
378a411e735SAlexander Holler 		.name = "HID-SENSOR-200076",
379a411e735SAlexander Holler 	},
380a411e735SAlexander Holler 	{ /* sentinel */ }
381a411e735SAlexander Holler };
382a411e735SAlexander Holler MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
383a411e735SAlexander Holler 
384c5bdbef7Ssrinivas pandruvada static struct platform_driver hid_gyro_3d_platform_driver = {
385a411e735SAlexander Holler 	.id_table = hid_gyro_3d_ids,
386c5bdbef7Ssrinivas pandruvada 	.driver = {
387a411e735SAlexander Holler 		.name	= KBUILD_MODNAME,
38827ce4239SSrinivas Pandruvada 		.pm	= &hid_sensor_pm_ops,
389c5bdbef7Ssrinivas pandruvada 	},
390c5bdbef7Ssrinivas pandruvada 	.probe		= hid_gyro_3d_probe,
391c5bdbef7Ssrinivas pandruvada 	.remove		= hid_gyro_3d_remove,
392c5bdbef7Ssrinivas pandruvada };
393c5bdbef7Ssrinivas pandruvada module_platform_driver(hid_gyro_3d_platform_driver);
394c5bdbef7Ssrinivas pandruvada 
395c5bdbef7Ssrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
396c5bdbef7Ssrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
397c5bdbef7Ssrinivas pandruvada MODULE_LICENSE("GPL");
39812f13d1fSAndy Shevchenko MODULE_IMPORT_NS(IIO_HID);
399