xref: /openbmc/linux/drivers/iio/accel/hid-sensor-accel-3d.c (revision 6b545eed655e846c16fdf611e686dca54db76766)
145fe6f7dSsrinivas pandruvada /*
245fe6f7dSsrinivas pandruvada  * HID Sensors Driver
345fe6f7dSsrinivas pandruvada  * Copyright (c) 2012, Intel Corporation.
445fe6f7dSsrinivas pandruvada  *
545fe6f7dSsrinivas pandruvada  * This program is free software; you can redistribute it and/or modify it
645fe6f7dSsrinivas pandruvada  * under the terms and conditions of the GNU General Public License,
745fe6f7dSsrinivas pandruvada  * version 2, as published by the Free Software Foundation.
845fe6f7dSsrinivas pandruvada  *
945fe6f7dSsrinivas pandruvada  * This program is distributed in the hope it will be useful, but WITHOUT
1045fe6f7dSsrinivas pandruvada  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1145fe6f7dSsrinivas pandruvada  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1245fe6f7dSsrinivas pandruvada  * more details.
1345fe6f7dSsrinivas pandruvada  *
1445fe6f7dSsrinivas pandruvada  * You should have received a copy of the GNU General Public License along with
1545fe6f7dSsrinivas pandruvada  * this program; if not, write to the Free Software Foundation, Inc.,
1645fe6f7dSsrinivas pandruvada  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
1745fe6f7dSsrinivas pandruvada  *
1845fe6f7dSsrinivas pandruvada  */
1945fe6f7dSsrinivas pandruvada #include <linux/device.h>
2045fe6f7dSsrinivas pandruvada #include <linux/platform_device.h>
2145fe6f7dSsrinivas pandruvada #include <linux/module.h>
2245fe6f7dSsrinivas pandruvada #include <linux/interrupt.h>
2345fe6f7dSsrinivas pandruvada #include <linux/irq.h>
2445fe6f7dSsrinivas pandruvada #include <linux/slab.h>
2545fe6f7dSsrinivas pandruvada #include <linux/hid-sensor-hub.h>
2645fe6f7dSsrinivas pandruvada #include <linux/iio/iio.h>
2745fe6f7dSsrinivas pandruvada #include <linux/iio/sysfs.h>
2845fe6f7dSsrinivas pandruvada #include <linux/iio/buffer.h>
2945fe6f7dSsrinivas pandruvada #include <linux/iio/trigger_consumer.h>
3045fe6f7dSsrinivas pandruvada #include <linux/iio/triggered_buffer.h>
3145fe6f7dSsrinivas pandruvada #include "../common/hid-sensors/hid-sensor-trigger.h"
3245fe6f7dSsrinivas pandruvada 
3345fe6f7dSsrinivas pandruvada enum accel_3d_channel {
3445fe6f7dSsrinivas pandruvada 	CHANNEL_SCAN_INDEX_X,
3545fe6f7dSsrinivas pandruvada 	CHANNEL_SCAN_INDEX_Y,
3645fe6f7dSsrinivas pandruvada 	CHANNEL_SCAN_INDEX_Z,
3745fe6f7dSsrinivas pandruvada 	ACCEL_3D_CHANNEL_MAX,
3845fe6f7dSsrinivas pandruvada };
3945fe6f7dSsrinivas pandruvada 
4045fe6f7dSsrinivas pandruvada struct accel_3d_state {
4145fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_callbacks callbacks;
42e07c6d17SAlexander Holler 	struct hid_sensor_common common_attributes;
4345fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
4445fe6f7dSsrinivas pandruvada 	u32 accel_val[ACCEL_3D_CHANNEL_MAX];
4545fe6f7dSsrinivas pandruvada };
4645fe6f7dSsrinivas pandruvada 
4745fe6f7dSsrinivas pandruvada static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
4845fe6f7dSsrinivas pandruvada 	HID_USAGE_SENSOR_ACCEL_X_AXIS,
4945fe6f7dSsrinivas pandruvada 	HID_USAGE_SENSOR_ACCEL_Y_AXIS,
5045fe6f7dSsrinivas pandruvada 	HID_USAGE_SENSOR_ACCEL_Z_AXIS
5145fe6f7dSsrinivas pandruvada };
5245fe6f7dSsrinivas pandruvada 
5345fe6f7dSsrinivas pandruvada /* Channel definitions */
5445fe6f7dSsrinivas pandruvada static const struct iio_chan_spec accel_3d_channels[] = {
5545fe6f7dSsrinivas pandruvada 	{
5645fe6f7dSsrinivas pandruvada 		.type = IIO_ACCEL,
5745fe6f7dSsrinivas pandruvada 		.modified = 1,
5845fe6f7dSsrinivas pandruvada 		.channel2 = IIO_MOD_X,
59ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
60ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
61ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
62ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
6345fe6f7dSsrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_X,
6445fe6f7dSsrinivas pandruvada 	}, {
6545fe6f7dSsrinivas pandruvada 		.type = IIO_ACCEL,
6645fe6f7dSsrinivas pandruvada 		.modified = 1,
6745fe6f7dSsrinivas pandruvada 		.channel2 = IIO_MOD_Y,
68ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
69ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
70ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
71ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
7245fe6f7dSsrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_Y,
7345fe6f7dSsrinivas pandruvada 	}, {
7445fe6f7dSsrinivas pandruvada 		.type = IIO_ACCEL,
7545fe6f7dSsrinivas pandruvada 		.modified = 1,
7645fe6f7dSsrinivas pandruvada 		.channel2 = IIO_MOD_Z,
77ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
78ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
79ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
80ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
8145fe6f7dSsrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_Z,
8245fe6f7dSsrinivas pandruvada 	}
8345fe6f7dSsrinivas pandruvada };
8445fe6f7dSsrinivas pandruvada 
8545fe6f7dSsrinivas pandruvada /* Adjust channel real bits based on report descriptor */
8645fe6f7dSsrinivas pandruvada static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
8745fe6f7dSsrinivas pandruvada 						int channel, int size)
8845fe6f7dSsrinivas pandruvada {
8945fe6f7dSsrinivas pandruvada 	channels[channel].scan_type.sign = 's';
9045fe6f7dSsrinivas pandruvada 	/* Real storage bits will change based on the report desc. */
9145fe6f7dSsrinivas pandruvada 	channels[channel].scan_type.realbits = size * 8;
9245fe6f7dSsrinivas pandruvada 	/* Maximum size of a sample to capture is u32 */
9345fe6f7dSsrinivas pandruvada 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
9445fe6f7dSsrinivas pandruvada }
9545fe6f7dSsrinivas pandruvada 
9645fe6f7dSsrinivas pandruvada /* Channel read_raw handler */
9745fe6f7dSsrinivas pandruvada static int accel_3d_read_raw(struct iio_dev *indio_dev,
9845fe6f7dSsrinivas pandruvada 			      struct iio_chan_spec const *chan,
9945fe6f7dSsrinivas pandruvada 			      int *val, int *val2,
10045fe6f7dSsrinivas pandruvada 			      long mask)
10145fe6f7dSsrinivas pandruvada {
10245fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
10345fe6f7dSsrinivas pandruvada 	int report_id = -1;
10445fe6f7dSsrinivas pandruvada 	u32 address;
10545fe6f7dSsrinivas pandruvada 	int ret;
10645fe6f7dSsrinivas pandruvada 	int ret_type;
10745fe6f7dSsrinivas pandruvada 
10845fe6f7dSsrinivas pandruvada 	*val = 0;
10945fe6f7dSsrinivas pandruvada 	*val2 = 0;
11045fe6f7dSsrinivas pandruvada 	switch (mask) {
11145fe6f7dSsrinivas pandruvada 	case 0:
11245fe6f7dSsrinivas pandruvada 		report_id = accel_state->accel[chan->scan_index].report_id;
11345fe6f7dSsrinivas pandruvada 		address = accel_3d_addresses[chan->scan_index];
11445fe6f7dSsrinivas pandruvada 		if (report_id >= 0)
11545fe6f7dSsrinivas pandruvada 			*val = sensor_hub_input_attr_get_raw_value(
11645fe6f7dSsrinivas pandruvada 				accel_state->common_attributes.hsdev,
11745fe6f7dSsrinivas pandruvada 				HID_USAGE_SENSOR_ACCEL_3D, address,
11845fe6f7dSsrinivas pandruvada 				report_id);
11945fe6f7dSsrinivas pandruvada 		else {
12045fe6f7dSsrinivas pandruvada 			*val = 0;
12145fe6f7dSsrinivas pandruvada 			return -EINVAL;
12245fe6f7dSsrinivas pandruvada 		}
12345fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT;
12445fe6f7dSsrinivas pandruvada 		break;
12545fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_SCALE:
12645fe6f7dSsrinivas pandruvada 		*val = accel_state->accel[CHANNEL_SCAN_INDEX_X].units;
12745fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT;
12845fe6f7dSsrinivas pandruvada 		break;
12945fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_OFFSET:
13045fe6f7dSsrinivas pandruvada 		*val = hid_sensor_convert_exponent(
13145fe6f7dSsrinivas pandruvada 			accel_state->accel[CHANNEL_SCAN_INDEX_X].unit_expo);
13245fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT;
13345fe6f7dSsrinivas pandruvada 		break;
13445fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
13545fe6f7dSsrinivas pandruvada 		ret = hid_sensor_read_samp_freq_value(
13645fe6f7dSsrinivas pandruvada 			&accel_state->common_attributes, val, val2);
13745fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT_PLUS_MICRO;
13845fe6f7dSsrinivas pandruvada 		break;
13945fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
14045fe6f7dSsrinivas pandruvada 		ret = hid_sensor_read_raw_hyst_value(
14145fe6f7dSsrinivas pandruvada 			&accel_state->common_attributes, val, val2);
14245fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT_PLUS_MICRO;
14345fe6f7dSsrinivas pandruvada 		break;
14445fe6f7dSsrinivas pandruvada 	default:
14545fe6f7dSsrinivas pandruvada 		ret_type = -EINVAL;
14645fe6f7dSsrinivas pandruvada 		break;
14745fe6f7dSsrinivas pandruvada 	}
14845fe6f7dSsrinivas pandruvada 
14945fe6f7dSsrinivas pandruvada 	return ret_type;
15045fe6f7dSsrinivas pandruvada }
15145fe6f7dSsrinivas pandruvada 
15245fe6f7dSsrinivas pandruvada /* Channel write_raw handler */
15345fe6f7dSsrinivas pandruvada static int accel_3d_write_raw(struct iio_dev *indio_dev,
15445fe6f7dSsrinivas pandruvada 			       struct iio_chan_spec const *chan,
15545fe6f7dSsrinivas pandruvada 			       int val,
15645fe6f7dSsrinivas pandruvada 			       int val2,
15745fe6f7dSsrinivas pandruvada 			       long mask)
15845fe6f7dSsrinivas pandruvada {
15945fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
16045fe6f7dSsrinivas pandruvada 	int ret = 0;
16145fe6f7dSsrinivas pandruvada 
16245fe6f7dSsrinivas pandruvada 	switch (mask) {
16345fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
16445fe6f7dSsrinivas pandruvada 		ret = hid_sensor_write_samp_freq_value(
16545fe6f7dSsrinivas pandruvada 				&accel_state->common_attributes, val, val2);
16645fe6f7dSsrinivas pandruvada 		break;
16745fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
16845fe6f7dSsrinivas pandruvada 		ret = hid_sensor_write_raw_hyst_value(
16945fe6f7dSsrinivas pandruvada 				&accel_state->common_attributes, val, val2);
17045fe6f7dSsrinivas pandruvada 		break;
17145fe6f7dSsrinivas pandruvada 	default:
17245fe6f7dSsrinivas pandruvada 		ret = -EINVAL;
17345fe6f7dSsrinivas pandruvada 	}
17445fe6f7dSsrinivas pandruvada 
17545fe6f7dSsrinivas pandruvada 	return ret;
17645fe6f7dSsrinivas pandruvada }
17745fe6f7dSsrinivas pandruvada 
17845fe6f7dSsrinivas pandruvada static int accel_3d_write_raw_get_fmt(struct iio_dev *indio_dev,
17945fe6f7dSsrinivas pandruvada 			       struct iio_chan_spec const *chan,
18045fe6f7dSsrinivas pandruvada 			       long mask)
18145fe6f7dSsrinivas pandruvada {
18245fe6f7dSsrinivas pandruvada 	return IIO_VAL_INT_PLUS_MICRO;
18345fe6f7dSsrinivas pandruvada }
18445fe6f7dSsrinivas pandruvada 
18545fe6f7dSsrinivas pandruvada static const struct iio_info accel_3d_info = {
18645fe6f7dSsrinivas pandruvada 	.driver_module = THIS_MODULE,
18745fe6f7dSsrinivas pandruvada 	.read_raw = &accel_3d_read_raw,
18845fe6f7dSsrinivas pandruvada 	.write_raw = &accel_3d_write_raw,
18945fe6f7dSsrinivas pandruvada 	.write_raw_get_fmt = &accel_3d_write_raw_get_fmt,
19045fe6f7dSsrinivas pandruvada };
19145fe6f7dSsrinivas pandruvada 
19245fe6f7dSsrinivas pandruvada /* Function to push data to buffer */
19345fe6f7dSsrinivas pandruvada static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
19445fe6f7dSsrinivas pandruvada {
19545fe6f7dSsrinivas pandruvada 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
19684b36ce5SJonathan Cameron 	iio_push_to_buffers(indio_dev, (u8 *)data);
19745fe6f7dSsrinivas pandruvada }
19845fe6f7dSsrinivas pandruvada 
19945fe6f7dSsrinivas pandruvada /* Callback handler to send event after all samples are received and captured */
20045fe6f7dSsrinivas pandruvada static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
20145fe6f7dSsrinivas pandruvada 				unsigned usage_id,
20245fe6f7dSsrinivas pandruvada 				void *priv)
20345fe6f7dSsrinivas pandruvada {
20445fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
20545fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
20645fe6f7dSsrinivas pandruvada 
20745fe6f7dSsrinivas pandruvada 	dev_dbg(&indio_dev->dev, "accel_3d_proc_event [%d]\n",
20845fe6f7dSsrinivas pandruvada 				accel_state->common_attributes.data_ready);
20945fe6f7dSsrinivas pandruvada 	if (accel_state->common_attributes.data_ready)
21045fe6f7dSsrinivas pandruvada 		hid_sensor_push_data(indio_dev,
21145fe6f7dSsrinivas pandruvada 				(u8 *)accel_state->accel_val,
21245fe6f7dSsrinivas pandruvada 				sizeof(accel_state->accel_val));
21345fe6f7dSsrinivas pandruvada 
21445fe6f7dSsrinivas pandruvada 	return 0;
21545fe6f7dSsrinivas pandruvada }
21645fe6f7dSsrinivas pandruvada 
21745fe6f7dSsrinivas pandruvada /* Capture samples in local storage */
21845fe6f7dSsrinivas pandruvada static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
21945fe6f7dSsrinivas pandruvada 				unsigned usage_id,
22045fe6f7dSsrinivas pandruvada 				size_t raw_len, char *raw_data,
22145fe6f7dSsrinivas pandruvada 				void *priv)
22245fe6f7dSsrinivas pandruvada {
22345fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
22445fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
22545fe6f7dSsrinivas pandruvada 	int offset;
22645fe6f7dSsrinivas pandruvada 	int ret = -EINVAL;
22745fe6f7dSsrinivas pandruvada 
22845fe6f7dSsrinivas pandruvada 	switch (usage_id) {
22945fe6f7dSsrinivas pandruvada 	case HID_USAGE_SENSOR_ACCEL_X_AXIS:
23045fe6f7dSsrinivas pandruvada 	case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
23145fe6f7dSsrinivas pandruvada 	case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
23245fe6f7dSsrinivas pandruvada 		offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
23345fe6f7dSsrinivas pandruvada 		accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
23445fe6f7dSsrinivas pandruvada 						*(u32 *)raw_data;
23545fe6f7dSsrinivas pandruvada 		ret = 0;
23645fe6f7dSsrinivas pandruvada 	break;
23745fe6f7dSsrinivas pandruvada 	default:
23845fe6f7dSsrinivas pandruvada 		break;
23945fe6f7dSsrinivas pandruvada 	}
24045fe6f7dSsrinivas pandruvada 
24145fe6f7dSsrinivas pandruvada 	return ret;
24245fe6f7dSsrinivas pandruvada }
24345fe6f7dSsrinivas pandruvada 
24445fe6f7dSsrinivas pandruvada /* Parse report which is specific to an usage id*/
24545fe6f7dSsrinivas pandruvada static int accel_3d_parse_report(struct platform_device *pdev,
24645fe6f7dSsrinivas pandruvada 				struct hid_sensor_hub_device *hsdev,
24745fe6f7dSsrinivas pandruvada 				struct iio_chan_spec *channels,
24845fe6f7dSsrinivas pandruvada 				unsigned usage_id,
24945fe6f7dSsrinivas pandruvada 				struct accel_3d_state *st)
25045fe6f7dSsrinivas pandruvada {
25145fe6f7dSsrinivas pandruvada 	int ret;
25245fe6f7dSsrinivas pandruvada 	int i;
25345fe6f7dSsrinivas pandruvada 
25445fe6f7dSsrinivas pandruvada 	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
25545fe6f7dSsrinivas pandruvada 		ret = sensor_hub_input_get_attribute_info(hsdev,
25645fe6f7dSsrinivas pandruvada 				HID_INPUT_REPORT,
25745fe6f7dSsrinivas pandruvada 				usage_id,
25845fe6f7dSsrinivas pandruvada 				HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
25945fe6f7dSsrinivas pandruvada 				&st->accel[CHANNEL_SCAN_INDEX_X + i]);
26045fe6f7dSsrinivas pandruvada 		if (ret < 0)
26145fe6f7dSsrinivas pandruvada 			break;
26245fe6f7dSsrinivas pandruvada 		accel_3d_adjust_channel_bit_mask(channels,
26345fe6f7dSsrinivas pandruvada 				CHANNEL_SCAN_INDEX_X + i,
26445fe6f7dSsrinivas pandruvada 				st->accel[CHANNEL_SCAN_INDEX_X + i].size);
26545fe6f7dSsrinivas pandruvada 	}
26645fe6f7dSsrinivas pandruvada 	dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
26745fe6f7dSsrinivas pandruvada 			st->accel[0].index,
26845fe6f7dSsrinivas pandruvada 			st->accel[0].report_id,
26945fe6f7dSsrinivas pandruvada 			st->accel[1].index, st->accel[1].report_id,
27045fe6f7dSsrinivas pandruvada 			st->accel[2].index, st->accel[2].report_id);
27145fe6f7dSsrinivas pandruvada 
27245fe6f7dSsrinivas pandruvada 	return ret;
27345fe6f7dSsrinivas pandruvada }
27445fe6f7dSsrinivas pandruvada 
27545fe6f7dSsrinivas pandruvada /* Function to initialize the processing for usage id */
276fc52692cSGreg Kroah-Hartman static int hid_accel_3d_probe(struct platform_device *pdev)
27745fe6f7dSsrinivas pandruvada {
27845fe6f7dSsrinivas pandruvada 	int ret = 0;
27945fe6f7dSsrinivas pandruvada 	static const char *name = "accel_3d";
28045fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev;
28145fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state;
28245fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
28345fe6f7dSsrinivas pandruvada 	struct iio_chan_spec *channels;
28445fe6f7dSsrinivas pandruvada 
285*6b545eedSSachin Kamat 	indio_dev = devm_iio_device_alloc(&pdev->dev,
286*6b545eedSSachin Kamat 					  sizeof(struct accel_3d_state));
287*6b545eedSSachin Kamat 	if (indio_dev == NULL)
288*6b545eedSSachin Kamat 		return -ENOMEM;
289*6b545eedSSachin Kamat 
29045fe6f7dSsrinivas pandruvada 	platform_set_drvdata(pdev, indio_dev);
29145fe6f7dSsrinivas pandruvada 
29245fe6f7dSsrinivas pandruvada 	accel_state = iio_priv(indio_dev);
29345fe6f7dSsrinivas pandruvada 	accel_state->common_attributes.hsdev = hsdev;
29445fe6f7dSsrinivas pandruvada 	accel_state->common_attributes.pdev = pdev;
29545fe6f7dSsrinivas pandruvada 
29645fe6f7dSsrinivas pandruvada 	ret = hid_sensor_parse_common_attributes(hsdev,
29745fe6f7dSsrinivas pandruvada 					HID_USAGE_SENSOR_ACCEL_3D,
29845fe6f7dSsrinivas pandruvada 					&accel_state->common_attributes);
29945fe6f7dSsrinivas pandruvada 	if (ret) {
30045fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to setup common attributes\n");
301*6b545eedSSachin Kamat 		return ret;
30245fe6f7dSsrinivas pandruvada 	}
30345fe6f7dSsrinivas pandruvada 
30464ebe955SAxel Lin 	channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels),
30545fe6f7dSsrinivas pandruvada 			   GFP_KERNEL);
30645fe6f7dSsrinivas pandruvada 	if (!channels) {
30745fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to duplicate channels\n");
308*6b545eedSSachin Kamat 		return -ENOMEM;
30945fe6f7dSsrinivas pandruvada 	}
31045fe6f7dSsrinivas pandruvada 
31145fe6f7dSsrinivas pandruvada 	ret = accel_3d_parse_report(pdev, hsdev, channels,
31245fe6f7dSsrinivas pandruvada 					HID_USAGE_SENSOR_ACCEL_3D, accel_state);
31345fe6f7dSsrinivas pandruvada 	if (ret) {
31445fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to setup attributes\n");
31545fe6f7dSsrinivas pandruvada 		goto error_free_dev_mem;
31645fe6f7dSsrinivas pandruvada 	}
31745fe6f7dSsrinivas pandruvada 
31845fe6f7dSsrinivas pandruvada 	indio_dev->channels = channels;
31945fe6f7dSsrinivas pandruvada 	indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels);
32045fe6f7dSsrinivas pandruvada 	indio_dev->dev.parent = &pdev->dev;
32145fe6f7dSsrinivas pandruvada 	indio_dev->info = &accel_3d_info;
32245fe6f7dSsrinivas pandruvada 	indio_dev->name = name;
32345fe6f7dSsrinivas pandruvada 	indio_dev->modes = INDIO_DIRECT_MODE;
32445fe6f7dSsrinivas pandruvada 
32545fe6f7dSsrinivas pandruvada 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
32645fe6f7dSsrinivas pandruvada 		NULL, NULL);
32745fe6f7dSsrinivas pandruvada 	if (ret) {
32845fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
32945fe6f7dSsrinivas pandruvada 		goto error_free_dev_mem;
33045fe6f7dSsrinivas pandruvada 	}
33145fe6f7dSsrinivas pandruvada 	accel_state->common_attributes.data_ready = false;
33245fe6f7dSsrinivas pandruvada 	ret = hid_sensor_setup_trigger(indio_dev, name,
33345fe6f7dSsrinivas pandruvada 					&accel_state->common_attributes);
33445fe6f7dSsrinivas pandruvada 	if (ret < 0) {
33545fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "trigger setup failed\n");
33645fe6f7dSsrinivas pandruvada 		goto error_unreg_buffer_funcs;
33745fe6f7dSsrinivas pandruvada 	}
33845fe6f7dSsrinivas pandruvada 
33945fe6f7dSsrinivas pandruvada 	ret = iio_device_register(indio_dev);
34045fe6f7dSsrinivas pandruvada 	if (ret) {
34145fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "device register failed\n");
34245fe6f7dSsrinivas pandruvada 		goto error_remove_trigger;
34345fe6f7dSsrinivas pandruvada 	}
34445fe6f7dSsrinivas pandruvada 
34545fe6f7dSsrinivas pandruvada 	accel_state->callbacks.send_event = accel_3d_proc_event;
34645fe6f7dSsrinivas pandruvada 	accel_state->callbacks.capture_sample = accel_3d_capture_sample;
34745fe6f7dSsrinivas pandruvada 	accel_state->callbacks.pdev = pdev;
34845fe6f7dSsrinivas pandruvada 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D,
34945fe6f7dSsrinivas pandruvada 					&accel_state->callbacks);
35045fe6f7dSsrinivas pandruvada 	if (ret < 0) {
35145fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "callback reg failed\n");
35245fe6f7dSsrinivas pandruvada 		goto error_iio_unreg;
35345fe6f7dSsrinivas pandruvada 	}
35445fe6f7dSsrinivas pandruvada 
35545fe6f7dSsrinivas pandruvada 	return ret;
35645fe6f7dSsrinivas pandruvada 
35745fe6f7dSsrinivas pandruvada error_iio_unreg:
35845fe6f7dSsrinivas pandruvada 	iio_device_unregister(indio_dev);
35945fe6f7dSsrinivas pandruvada error_remove_trigger:
36045fe6f7dSsrinivas pandruvada 	hid_sensor_remove_trigger(indio_dev);
36145fe6f7dSsrinivas pandruvada error_unreg_buffer_funcs:
36245fe6f7dSsrinivas pandruvada 	iio_triggered_buffer_cleanup(indio_dev);
36345fe6f7dSsrinivas pandruvada error_free_dev_mem:
36445fe6f7dSsrinivas pandruvada 	kfree(indio_dev->channels);
36545fe6f7dSsrinivas pandruvada 	return ret;
36645fe6f7dSsrinivas pandruvada }
36745fe6f7dSsrinivas pandruvada 
36845fe6f7dSsrinivas pandruvada /* Function to deinitialize the processing for usage id */
369fc52692cSGreg Kroah-Hartman static int hid_accel_3d_remove(struct platform_device *pdev)
37045fe6f7dSsrinivas pandruvada {
37145fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
37245fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
37345fe6f7dSsrinivas pandruvada 
37445fe6f7dSsrinivas pandruvada 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D);
37545fe6f7dSsrinivas pandruvada 	iio_device_unregister(indio_dev);
37645fe6f7dSsrinivas pandruvada 	hid_sensor_remove_trigger(indio_dev);
37745fe6f7dSsrinivas pandruvada 	iio_triggered_buffer_cleanup(indio_dev);
37845fe6f7dSsrinivas pandruvada 	kfree(indio_dev->channels);
37945fe6f7dSsrinivas pandruvada 
38045fe6f7dSsrinivas pandruvada 	return 0;
38145fe6f7dSsrinivas pandruvada }
38245fe6f7dSsrinivas pandruvada 
38366c721f7SAlexander Holler static struct platform_device_id hid_accel_3d_ids[] = {
38466c721f7SAlexander Holler 	{
38566c721f7SAlexander Holler 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
38666c721f7SAlexander Holler 		.name = "HID-SENSOR-200073",
38766c721f7SAlexander Holler 	},
38866c721f7SAlexander Holler 	{ /* sentinel */ }
38966c721f7SAlexander Holler };
39066c721f7SAlexander Holler MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
39166c721f7SAlexander Holler 
39245fe6f7dSsrinivas pandruvada static struct platform_driver hid_accel_3d_platform_driver = {
39366c721f7SAlexander Holler 	.id_table = hid_accel_3d_ids,
39445fe6f7dSsrinivas pandruvada 	.driver = {
39566c721f7SAlexander Holler 		.name	= KBUILD_MODNAME,
39645fe6f7dSsrinivas pandruvada 		.owner	= THIS_MODULE,
39745fe6f7dSsrinivas pandruvada 	},
39845fe6f7dSsrinivas pandruvada 	.probe		= hid_accel_3d_probe,
39945fe6f7dSsrinivas pandruvada 	.remove		= hid_accel_3d_remove,
40045fe6f7dSsrinivas pandruvada };
40145fe6f7dSsrinivas pandruvada module_platform_driver(hid_accel_3d_platform_driver);
40245fe6f7dSsrinivas pandruvada 
40345fe6f7dSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Accel 3D");
40445fe6f7dSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
40545fe6f7dSsrinivas pandruvada MODULE_LICENSE("GPL");
406