xref: /openbmc/linux/drivers/iio/accel/hid-sensor-accel-3d.c (revision 56ff6be608659ac06d4e3cc5827476efa29d610f)
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];
4573a24988SSrinivas Pandruvada 	int scale_pre_decml;
4673a24988SSrinivas Pandruvada 	int scale_post_decml;
4773a24988SSrinivas Pandruvada 	int scale_precision;
4873a24988SSrinivas Pandruvada 	int value_offset;
4945fe6f7dSsrinivas pandruvada };
5045fe6f7dSsrinivas pandruvada 
5145fe6f7dSsrinivas pandruvada static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
5245fe6f7dSsrinivas pandruvada 	HID_USAGE_SENSOR_ACCEL_X_AXIS,
5345fe6f7dSsrinivas pandruvada 	HID_USAGE_SENSOR_ACCEL_Y_AXIS,
5445fe6f7dSsrinivas pandruvada 	HID_USAGE_SENSOR_ACCEL_Z_AXIS
5545fe6f7dSsrinivas pandruvada };
5645fe6f7dSsrinivas pandruvada 
5745fe6f7dSsrinivas pandruvada /* Channel definitions */
5845fe6f7dSsrinivas pandruvada static const struct iio_chan_spec accel_3d_channels[] = {
5945fe6f7dSsrinivas pandruvada 	{
6045fe6f7dSsrinivas pandruvada 		.type = IIO_ACCEL,
6145fe6f7dSsrinivas pandruvada 		.modified = 1,
6245fe6f7dSsrinivas pandruvada 		.channel2 = IIO_MOD_X,
63ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
64ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
65ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
66ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
6745fe6f7dSsrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_X,
6845fe6f7dSsrinivas pandruvada 	}, {
6945fe6f7dSsrinivas pandruvada 		.type = IIO_ACCEL,
7045fe6f7dSsrinivas pandruvada 		.modified = 1,
7145fe6f7dSsrinivas pandruvada 		.channel2 = IIO_MOD_Y,
72ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
73ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
74ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
75ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
7645fe6f7dSsrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_Y,
7745fe6f7dSsrinivas pandruvada 	}, {
7845fe6f7dSsrinivas pandruvada 		.type = IIO_ACCEL,
7945fe6f7dSsrinivas pandruvada 		.modified = 1,
8045fe6f7dSsrinivas pandruvada 		.channel2 = IIO_MOD_Z,
81ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
82ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
83ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
84ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
8545fe6f7dSsrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_Z,
8645fe6f7dSsrinivas pandruvada 	}
8745fe6f7dSsrinivas pandruvada };
8845fe6f7dSsrinivas pandruvada 
8945fe6f7dSsrinivas pandruvada /* Adjust channel real bits based on report descriptor */
9045fe6f7dSsrinivas pandruvada static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
9145fe6f7dSsrinivas pandruvada 						int channel, int size)
9245fe6f7dSsrinivas pandruvada {
9345fe6f7dSsrinivas pandruvada 	channels[channel].scan_type.sign = 's';
9445fe6f7dSsrinivas pandruvada 	/* Real storage bits will change based on the report desc. */
9545fe6f7dSsrinivas pandruvada 	channels[channel].scan_type.realbits = size * 8;
9645fe6f7dSsrinivas pandruvada 	/* Maximum size of a sample to capture is u32 */
9745fe6f7dSsrinivas pandruvada 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
9845fe6f7dSsrinivas pandruvada }
9945fe6f7dSsrinivas pandruvada 
10045fe6f7dSsrinivas pandruvada /* Channel read_raw handler */
10145fe6f7dSsrinivas pandruvada static int accel_3d_read_raw(struct iio_dev *indio_dev,
10245fe6f7dSsrinivas pandruvada 			      struct iio_chan_spec const *chan,
10345fe6f7dSsrinivas pandruvada 			      int *val, int *val2,
10445fe6f7dSsrinivas pandruvada 			      long mask)
10545fe6f7dSsrinivas pandruvada {
10645fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
10745fe6f7dSsrinivas pandruvada 	int report_id = -1;
10845fe6f7dSsrinivas pandruvada 	u32 address;
10945fe6f7dSsrinivas pandruvada 	int ret;
11045fe6f7dSsrinivas pandruvada 	int ret_type;
11145fe6f7dSsrinivas pandruvada 
11245fe6f7dSsrinivas pandruvada 	*val = 0;
11345fe6f7dSsrinivas pandruvada 	*val2 = 0;
11445fe6f7dSsrinivas pandruvada 	switch (mask) {
11545fe6f7dSsrinivas pandruvada 	case 0:
11645fe6f7dSsrinivas pandruvada 		report_id = accel_state->accel[chan->scan_index].report_id;
11745fe6f7dSsrinivas pandruvada 		address = accel_3d_addresses[chan->scan_index];
11845fe6f7dSsrinivas pandruvada 		if (report_id >= 0)
11945fe6f7dSsrinivas pandruvada 			*val = sensor_hub_input_attr_get_raw_value(
12045fe6f7dSsrinivas pandruvada 				accel_state->common_attributes.hsdev,
12145fe6f7dSsrinivas pandruvada 				HID_USAGE_SENSOR_ACCEL_3D, address,
12245fe6f7dSsrinivas pandruvada 				report_id);
12345fe6f7dSsrinivas pandruvada 		else {
12445fe6f7dSsrinivas pandruvada 			*val = 0;
12545fe6f7dSsrinivas pandruvada 			return -EINVAL;
12645fe6f7dSsrinivas pandruvada 		}
12745fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT;
12845fe6f7dSsrinivas pandruvada 		break;
12945fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_SCALE:
13073a24988SSrinivas Pandruvada 		*val = accel_state->scale_pre_decml;
13173a24988SSrinivas Pandruvada 		*val2 = accel_state->scale_post_decml;
13273a24988SSrinivas Pandruvada 		ret_type = accel_state->scale_precision;
13345fe6f7dSsrinivas pandruvada 		break;
13445fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_OFFSET:
13573a24988SSrinivas Pandruvada 		*val = accel_state->value_offset;
13645fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT;
13745fe6f7dSsrinivas pandruvada 		break;
13845fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
13945fe6f7dSsrinivas pandruvada 		ret = hid_sensor_read_samp_freq_value(
14045fe6f7dSsrinivas pandruvada 			&accel_state->common_attributes, val, val2);
14145fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT_PLUS_MICRO;
14245fe6f7dSsrinivas pandruvada 		break;
14345fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
14445fe6f7dSsrinivas pandruvada 		ret = hid_sensor_read_raw_hyst_value(
14545fe6f7dSsrinivas pandruvada 			&accel_state->common_attributes, val, val2);
14645fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT_PLUS_MICRO;
14745fe6f7dSsrinivas pandruvada 		break;
14845fe6f7dSsrinivas pandruvada 	default:
14945fe6f7dSsrinivas pandruvada 		ret_type = -EINVAL;
15045fe6f7dSsrinivas pandruvada 		break;
15145fe6f7dSsrinivas pandruvada 	}
15245fe6f7dSsrinivas pandruvada 
15345fe6f7dSsrinivas pandruvada 	return ret_type;
15445fe6f7dSsrinivas pandruvada }
15545fe6f7dSsrinivas pandruvada 
15645fe6f7dSsrinivas pandruvada /* Channel write_raw handler */
15745fe6f7dSsrinivas pandruvada static int accel_3d_write_raw(struct iio_dev *indio_dev,
15845fe6f7dSsrinivas pandruvada 			       struct iio_chan_spec const *chan,
15945fe6f7dSsrinivas pandruvada 			       int val,
16045fe6f7dSsrinivas pandruvada 			       int val2,
16145fe6f7dSsrinivas pandruvada 			       long mask)
16245fe6f7dSsrinivas pandruvada {
16345fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
16445fe6f7dSsrinivas pandruvada 	int ret = 0;
16545fe6f7dSsrinivas pandruvada 
16645fe6f7dSsrinivas pandruvada 	switch (mask) {
16745fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
16845fe6f7dSsrinivas pandruvada 		ret = hid_sensor_write_samp_freq_value(
16945fe6f7dSsrinivas pandruvada 				&accel_state->common_attributes, val, val2);
17045fe6f7dSsrinivas pandruvada 		break;
17145fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
17245fe6f7dSsrinivas pandruvada 		ret = hid_sensor_write_raw_hyst_value(
17345fe6f7dSsrinivas pandruvada 				&accel_state->common_attributes, val, val2);
17445fe6f7dSsrinivas pandruvada 		break;
17545fe6f7dSsrinivas pandruvada 	default:
17645fe6f7dSsrinivas pandruvada 		ret = -EINVAL;
17745fe6f7dSsrinivas pandruvada 	}
17845fe6f7dSsrinivas pandruvada 
17945fe6f7dSsrinivas pandruvada 	return ret;
18045fe6f7dSsrinivas pandruvada }
18145fe6f7dSsrinivas pandruvada 
18245fe6f7dSsrinivas pandruvada static const struct iio_info accel_3d_info = {
18345fe6f7dSsrinivas pandruvada 	.driver_module = THIS_MODULE,
18445fe6f7dSsrinivas pandruvada 	.read_raw = &accel_3d_read_raw,
18545fe6f7dSsrinivas pandruvada 	.write_raw = &accel_3d_write_raw,
18645fe6f7dSsrinivas pandruvada };
18745fe6f7dSsrinivas pandruvada 
18845fe6f7dSsrinivas pandruvada /* Function to push data to buffer */
1898c60c7e7SLars-Peter Clausen static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
1908c60c7e7SLars-Peter Clausen 	int len)
19145fe6f7dSsrinivas pandruvada {
19245fe6f7dSsrinivas pandruvada 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
1938c60c7e7SLars-Peter Clausen 	iio_push_to_buffers(indio_dev, data);
19445fe6f7dSsrinivas pandruvada }
19545fe6f7dSsrinivas pandruvada 
19645fe6f7dSsrinivas pandruvada /* Callback handler to send event after all samples are received and captured */
19745fe6f7dSsrinivas pandruvada static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
19845fe6f7dSsrinivas pandruvada 				unsigned usage_id,
19945fe6f7dSsrinivas pandruvada 				void *priv)
20045fe6f7dSsrinivas pandruvada {
20145fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
20245fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
20345fe6f7dSsrinivas pandruvada 
204*56ff6be6SSrinivas Pandruvada 	dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n");
205*56ff6be6SSrinivas Pandruvada 	if (atomic_read(&accel_state->common_attributes.data_ready))
20645fe6f7dSsrinivas pandruvada 		hid_sensor_push_data(indio_dev,
2078c60c7e7SLars-Peter Clausen 				accel_state->accel_val,
20845fe6f7dSsrinivas pandruvada 				sizeof(accel_state->accel_val));
20945fe6f7dSsrinivas pandruvada 
21045fe6f7dSsrinivas pandruvada 	return 0;
21145fe6f7dSsrinivas pandruvada }
21245fe6f7dSsrinivas pandruvada 
21345fe6f7dSsrinivas pandruvada /* Capture samples in local storage */
21445fe6f7dSsrinivas pandruvada static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
21545fe6f7dSsrinivas pandruvada 				unsigned usage_id,
21645fe6f7dSsrinivas pandruvada 				size_t raw_len, char *raw_data,
21745fe6f7dSsrinivas pandruvada 				void *priv)
21845fe6f7dSsrinivas pandruvada {
21945fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
22045fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
22145fe6f7dSsrinivas pandruvada 	int offset;
22245fe6f7dSsrinivas pandruvada 	int ret = -EINVAL;
22345fe6f7dSsrinivas pandruvada 
22445fe6f7dSsrinivas pandruvada 	switch (usage_id) {
22545fe6f7dSsrinivas pandruvada 	case HID_USAGE_SENSOR_ACCEL_X_AXIS:
22645fe6f7dSsrinivas pandruvada 	case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
22745fe6f7dSsrinivas pandruvada 	case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
22845fe6f7dSsrinivas pandruvada 		offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
22945fe6f7dSsrinivas pandruvada 		accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
23045fe6f7dSsrinivas pandruvada 						*(u32 *)raw_data;
23145fe6f7dSsrinivas pandruvada 		ret = 0;
23245fe6f7dSsrinivas pandruvada 	break;
23345fe6f7dSsrinivas pandruvada 	default:
23445fe6f7dSsrinivas pandruvada 		break;
23545fe6f7dSsrinivas pandruvada 	}
23645fe6f7dSsrinivas pandruvada 
23745fe6f7dSsrinivas pandruvada 	return ret;
23845fe6f7dSsrinivas pandruvada }
23945fe6f7dSsrinivas pandruvada 
24045fe6f7dSsrinivas pandruvada /* Parse report which is specific to an usage id*/
24145fe6f7dSsrinivas pandruvada static int accel_3d_parse_report(struct platform_device *pdev,
24245fe6f7dSsrinivas pandruvada 				struct hid_sensor_hub_device *hsdev,
24345fe6f7dSsrinivas pandruvada 				struct iio_chan_spec *channels,
24445fe6f7dSsrinivas pandruvada 				unsigned usage_id,
24545fe6f7dSsrinivas pandruvada 				struct accel_3d_state *st)
24645fe6f7dSsrinivas pandruvada {
24745fe6f7dSsrinivas pandruvada 	int ret;
24845fe6f7dSsrinivas pandruvada 	int i;
24945fe6f7dSsrinivas pandruvada 
25045fe6f7dSsrinivas pandruvada 	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
25145fe6f7dSsrinivas pandruvada 		ret = sensor_hub_input_get_attribute_info(hsdev,
25245fe6f7dSsrinivas pandruvada 				HID_INPUT_REPORT,
25345fe6f7dSsrinivas pandruvada 				usage_id,
25445fe6f7dSsrinivas pandruvada 				HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
25545fe6f7dSsrinivas pandruvada 				&st->accel[CHANNEL_SCAN_INDEX_X + i]);
25645fe6f7dSsrinivas pandruvada 		if (ret < 0)
25745fe6f7dSsrinivas pandruvada 			break;
25845fe6f7dSsrinivas pandruvada 		accel_3d_adjust_channel_bit_mask(channels,
25945fe6f7dSsrinivas pandruvada 				CHANNEL_SCAN_INDEX_X + i,
26045fe6f7dSsrinivas pandruvada 				st->accel[CHANNEL_SCAN_INDEX_X + i].size);
26145fe6f7dSsrinivas pandruvada 	}
26245fe6f7dSsrinivas pandruvada 	dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
26345fe6f7dSsrinivas pandruvada 			st->accel[0].index,
26445fe6f7dSsrinivas pandruvada 			st->accel[0].report_id,
26545fe6f7dSsrinivas pandruvada 			st->accel[1].index, st->accel[1].report_id,
26645fe6f7dSsrinivas pandruvada 			st->accel[2].index, st->accel[2].report_id);
26745fe6f7dSsrinivas pandruvada 
26873a24988SSrinivas Pandruvada 	st->scale_precision = hid_sensor_format_scale(
26973a24988SSrinivas Pandruvada 				HID_USAGE_SENSOR_ACCEL_3D,
27073a24988SSrinivas Pandruvada 				&st->accel[CHANNEL_SCAN_INDEX_X],
27173a24988SSrinivas Pandruvada 				&st->scale_pre_decml, &st->scale_post_decml);
27273a24988SSrinivas Pandruvada 
27364528d03SSrinivas Pandruvada 	/* Set Sensitivity field ids, when there is no individual modifier */
27464528d03SSrinivas Pandruvada 	if (st->common_attributes.sensitivity.index < 0) {
27564528d03SSrinivas Pandruvada 		sensor_hub_input_get_attribute_info(hsdev,
27664528d03SSrinivas Pandruvada 			HID_FEATURE_REPORT, usage_id,
27764528d03SSrinivas Pandruvada 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
27864528d03SSrinivas Pandruvada 			HID_USAGE_SENSOR_DATA_ACCELERATION,
27964528d03SSrinivas Pandruvada 			&st->common_attributes.sensitivity);
28064528d03SSrinivas Pandruvada 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
28164528d03SSrinivas Pandruvada 			st->common_attributes.sensitivity.index,
28264528d03SSrinivas Pandruvada 			st->common_attributes.sensitivity.report_id);
28364528d03SSrinivas Pandruvada 	}
28464528d03SSrinivas Pandruvada 
28545fe6f7dSsrinivas pandruvada 	return ret;
28645fe6f7dSsrinivas pandruvada }
28745fe6f7dSsrinivas pandruvada 
28845fe6f7dSsrinivas pandruvada /* Function to initialize the processing for usage id */
289fc52692cSGreg Kroah-Hartman static int hid_accel_3d_probe(struct platform_device *pdev)
29045fe6f7dSsrinivas pandruvada {
29145fe6f7dSsrinivas pandruvada 	int ret = 0;
29245fe6f7dSsrinivas pandruvada 	static const char *name = "accel_3d";
29345fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev;
29445fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state;
29545fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
29645fe6f7dSsrinivas pandruvada 	struct iio_chan_spec *channels;
29745fe6f7dSsrinivas pandruvada 
2986b545eedSSachin Kamat 	indio_dev = devm_iio_device_alloc(&pdev->dev,
2996b545eedSSachin Kamat 					  sizeof(struct accel_3d_state));
3006b545eedSSachin Kamat 	if (indio_dev == NULL)
3016b545eedSSachin Kamat 		return -ENOMEM;
3026b545eedSSachin Kamat 
30345fe6f7dSsrinivas pandruvada 	platform_set_drvdata(pdev, indio_dev);
30445fe6f7dSsrinivas pandruvada 
30545fe6f7dSsrinivas pandruvada 	accel_state = iio_priv(indio_dev);
30645fe6f7dSsrinivas pandruvada 	accel_state->common_attributes.hsdev = hsdev;
30745fe6f7dSsrinivas pandruvada 	accel_state->common_attributes.pdev = pdev;
30845fe6f7dSsrinivas pandruvada 
30945fe6f7dSsrinivas pandruvada 	ret = hid_sensor_parse_common_attributes(hsdev,
31045fe6f7dSsrinivas pandruvada 					HID_USAGE_SENSOR_ACCEL_3D,
31145fe6f7dSsrinivas pandruvada 					&accel_state->common_attributes);
31245fe6f7dSsrinivas pandruvada 	if (ret) {
31345fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to setup common attributes\n");
3146b545eedSSachin Kamat 		return ret;
31545fe6f7dSsrinivas pandruvada 	}
31645fe6f7dSsrinivas pandruvada 
31764ebe955SAxel Lin 	channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels),
31845fe6f7dSsrinivas pandruvada 			   GFP_KERNEL);
31945fe6f7dSsrinivas pandruvada 	if (!channels) {
32045fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to duplicate channels\n");
3216b545eedSSachin Kamat 		return -ENOMEM;
32245fe6f7dSsrinivas pandruvada 	}
32345fe6f7dSsrinivas pandruvada 
32445fe6f7dSsrinivas pandruvada 	ret = accel_3d_parse_report(pdev, hsdev, channels,
32545fe6f7dSsrinivas pandruvada 					HID_USAGE_SENSOR_ACCEL_3D, accel_state);
32645fe6f7dSsrinivas pandruvada 	if (ret) {
32745fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to setup attributes\n");
32845fe6f7dSsrinivas pandruvada 		goto error_free_dev_mem;
32945fe6f7dSsrinivas pandruvada 	}
33045fe6f7dSsrinivas pandruvada 
33145fe6f7dSsrinivas pandruvada 	indio_dev->channels = channels;
33245fe6f7dSsrinivas pandruvada 	indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels);
33345fe6f7dSsrinivas pandruvada 	indio_dev->dev.parent = &pdev->dev;
33445fe6f7dSsrinivas pandruvada 	indio_dev->info = &accel_3d_info;
33545fe6f7dSsrinivas pandruvada 	indio_dev->name = name;
33645fe6f7dSsrinivas pandruvada 	indio_dev->modes = INDIO_DIRECT_MODE;
33745fe6f7dSsrinivas pandruvada 
33845fe6f7dSsrinivas pandruvada 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
33945fe6f7dSsrinivas pandruvada 		NULL, NULL);
34045fe6f7dSsrinivas pandruvada 	if (ret) {
34145fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
34245fe6f7dSsrinivas pandruvada 		goto error_free_dev_mem;
34345fe6f7dSsrinivas pandruvada 	}
344*56ff6be6SSrinivas Pandruvada 	atomic_set(&accel_state->common_attributes.data_ready, 0);
34545fe6f7dSsrinivas pandruvada 	ret = hid_sensor_setup_trigger(indio_dev, name,
34645fe6f7dSsrinivas pandruvada 					&accel_state->common_attributes);
34745fe6f7dSsrinivas pandruvada 	if (ret < 0) {
34845fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "trigger setup failed\n");
34945fe6f7dSsrinivas pandruvada 		goto error_unreg_buffer_funcs;
35045fe6f7dSsrinivas pandruvada 	}
35145fe6f7dSsrinivas pandruvada 
35245fe6f7dSsrinivas pandruvada 	ret = iio_device_register(indio_dev);
35345fe6f7dSsrinivas pandruvada 	if (ret) {
35445fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "device register failed\n");
35545fe6f7dSsrinivas pandruvada 		goto error_remove_trigger;
35645fe6f7dSsrinivas pandruvada 	}
35745fe6f7dSsrinivas pandruvada 
35845fe6f7dSsrinivas pandruvada 	accel_state->callbacks.send_event = accel_3d_proc_event;
35945fe6f7dSsrinivas pandruvada 	accel_state->callbacks.capture_sample = accel_3d_capture_sample;
36045fe6f7dSsrinivas pandruvada 	accel_state->callbacks.pdev = pdev;
36145fe6f7dSsrinivas pandruvada 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D,
36245fe6f7dSsrinivas pandruvada 					&accel_state->callbacks);
36345fe6f7dSsrinivas pandruvada 	if (ret < 0) {
36445fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "callback reg failed\n");
36545fe6f7dSsrinivas pandruvada 		goto error_iio_unreg;
36645fe6f7dSsrinivas pandruvada 	}
36745fe6f7dSsrinivas pandruvada 
36845fe6f7dSsrinivas pandruvada 	return ret;
36945fe6f7dSsrinivas pandruvada 
37045fe6f7dSsrinivas pandruvada error_iio_unreg:
37145fe6f7dSsrinivas pandruvada 	iio_device_unregister(indio_dev);
37245fe6f7dSsrinivas pandruvada error_remove_trigger:
373ec7f68e0SSrinivas Pandruvada 	hid_sensor_remove_trigger(&accel_state->common_attributes);
37445fe6f7dSsrinivas pandruvada error_unreg_buffer_funcs:
37545fe6f7dSsrinivas pandruvada 	iio_triggered_buffer_cleanup(indio_dev);
37645fe6f7dSsrinivas pandruvada error_free_dev_mem:
37745fe6f7dSsrinivas pandruvada 	kfree(indio_dev->channels);
37845fe6f7dSsrinivas pandruvada 	return ret;
37945fe6f7dSsrinivas pandruvada }
38045fe6f7dSsrinivas pandruvada 
38145fe6f7dSsrinivas pandruvada /* Function to deinitialize the processing for usage id */
382fc52692cSGreg Kroah-Hartman static int hid_accel_3d_remove(struct platform_device *pdev)
38345fe6f7dSsrinivas pandruvada {
38445fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
38545fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
386ec7f68e0SSrinivas Pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
38745fe6f7dSsrinivas pandruvada 
38845fe6f7dSsrinivas pandruvada 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D);
38945fe6f7dSsrinivas pandruvada 	iio_device_unregister(indio_dev);
390ec7f68e0SSrinivas Pandruvada 	hid_sensor_remove_trigger(&accel_state->common_attributes);
39145fe6f7dSsrinivas pandruvada 	iio_triggered_buffer_cleanup(indio_dev);
39245fe6f7dSsrinivas pandruvada 	kfree(indio_dev->channels);
39345fe6f7dSsrinivas pandruvada 
39445fe6f7dSsrinivas pandruvada 	return 0;
39545fe6f7dSsrinivas pandruvada }
39645fe6f7dSsrinivas pandruvada 
39766c721f7SAlexander Holler static struct platform_device_id hid_accel_3d_ids[] = {
39866c721f7SAlexander Holler 	{
39966c721f7SAlexander Holler 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
40066c721f7SAlexander Holler 		.name = "HID-SENSOR-200073",
40166c721f7SAlexander Holler 	},
40266c721f7SAlexander Holler 	{ /* sentinel */ }
40366c721f7SAlexander Holler };
40466c721f7SAlexander Holler MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
40566c721f7SAlexander Holler 
40645fe6f7dSsrinivas pandruvada static struct platform_driver hid_accel_3d_platform_driver = {
40766c721f7SAlexander Holler 	.id_table = hid_accel_3d_ids,
40845fe6f7dSsrinivas pandruvada 	.driver = {
40966c721f7SAlexander Holler 		.name	= KBUILD_MODNAME,
41045fe6f7dSsrinivas pandruvada 		.owner	= THIS_MODULE,
41145fe6f7dSsrinivas pandruvada 	},
41245fe6f7dSsrinivas pandruvada 	.probe		= hid_accel_3d_probe,
41345fe6f7dSsrinivas pandruvada 	.remove		= hid_accel_3d_remove,
41445fe6f7dSsrinivas pandruvada };
41545fe6f7dSsrinivas pandruvada module_platform_driver(hid_accel_3d_platform_driver);
41645fe6f7dSsrinivas pandruvada 
41745fe6f7dSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Accel 3D");
41845fe6f7dSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
41945fe6f7dSsrinivas pandruvada MODULE_LICENSE("GPL");
420