xref: /openbmc/linux/drivers/iio/accel/hid-sensor-accel-3d.c (revision 67464a54e5156ed2e5a72ffb516308b58f25e6ca)
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>
25c6399453SSrinivas Pandruvada #include <linux/delay.h>
2645fe6f7dSsrinivas pandruvada #include <linux/hid-sensor-hub.h>
2745fe6f7dSsrinivas pandruvada #include <linux/iio/iio.h>
2845fe6f7dSsrinivas pandruvada #include <linux/iio/sysfs.h>
2945fe6f7dSsrinivas pandruvada #include <linux/iio/buffer.h>
3045fe6f7dSsrinivas pandruvada #include <linux/iio/trigger_consumer.h>
3145fe6f7dSsrinivas pandruvada #include <linux/iio/triggered_buffer.h>
3245fe6f7dSsrinivas pandruvada #include "../common/hid-sensors/hid-sensor-trigger.h"
3345fe6f7dSsrinivas pandruvada 
3445fe6f7dSsrinivas pandruvada enum accel_3d_channel {
3545fe6f7dSsrinivas pandruvada 	CHANNEL_SCAN_INDEX_X,
3645fe6f7dSsrinivas pandruvada 	CHANNEL_SCAN_INDEX_Y,
3745fe6f7dSsrinivas pandruvada 	CHANNEL_SCAN_INDEX_Z,
3845fe6f7dSsrinivas pandruvada 	ACCEL_3D_CHANNEL_MAX,
3945fe6f7dSsrinivas pandruvada };
4045fe6f7dSsrinivas pandruvada 
4145fe6f7dSsrinivas pandruvada struct accel_3d_state {
4245fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_callbacks callbacks;
43e07c6d17SAlexander Holler 	struct hid_sensor_common common_attributes;
4445fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
45a96cd0f9SSrinivas Pandruvada 	/* Reserve for 3 channels + padding + timestamp */
46a96cd0f9SSrinivas Pandruvada 	u32 accel_val[ACCEL_3D_CHANNEL_MAX + 3];
4773a24988SSrinivas Pandruvada 	int scale_pre_decml;
4873a24988SSrinivas Pandruvada 	int scale_post_decml;
4973a24988SSrinivas Pandruvada 	int scale_precision;
5073a24988SSrinivas Pandruvada 	int value_offset;
51a96cd0f9SSrinivas Pandruvada 	int64_t timestamp;
5245fe6f7dSsrinivas pandruvada };
5345fe6f7dSsrinivas pandruvada 
5445fe6f7dSsrinivas pandruvada static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
5545fe6f7dSsrinivas pandruvada 	HID_USAGE_SENSOR_ACCEL_X_AXIS,
5645fe6f7dSsrinivas pandruvada 	HID_USAGE_SENSOR_ACCEL_Y_AXIS,
5745fe6f7dSsrinivas pandruvada 	HID_USAGE_SENSOR_ACCEL_Z_AXIS
5845fe6f7dSsrinivas pandruvada };
5945fe6f7dSsrinivas pandruvada 
6045fe6f7dSsrinivas pandruvada /* Channel definitions */
6145fe6f7dSsrinivas pandruvada static const struct iio_chan_spec accel_3d_channels[] = {
6245fe6f7dSsrinivas pandruvada 	{
6345fe6f7dSsrinivas pandruvada 		.type = IIO_ACCEL,
6445fe6f7dSsrinivas pandruvada 		.modified = 1,
6545fe6f7dSsrinivas pandruvada 		.channel2 = IIO_MOD_X,
66c6399453SSrinivas Pandruvada 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
67ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
68ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
69ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
70ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
7145fe6f7dSsrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_X,
7245fe6f7dSsrinivas pandruvada 	}, {
7345fe6f7dSsrinivas pandruvada 		.type = IIO_ACCEL,
7445fe6f7dSsrinivas pandruvada 		.modified = 1,
7545fe6f7dSsrinivas pandruvada 		.channel2 = IIO_MOD_Y,
76c6399453SSrinivas Pandruvada 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
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_Y,
8245fe6f7dSsrinivas pandruvada 	}, {
8345fe6f7dSsrinivas pandruvada 		.type = IIO_ACCEL,
8445fe6f7dSsrinivas pandruvada 		.modified = 1,
8545fe6f7dSsrinivas pandruvada 		.channel2 = IIO_MOD_Z,
86c6399453SSrinivas Pandruvada 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
87ecbe18f2SJonathan Cameron 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
88ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SCALE) |
89ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
90ecbe18f2SJonathan Cameron 		BIT(IIO_CHAN_INFO_HYSTERESIS),
9145fe6f7dSsrinivas pandruvada 		.scan_index = CHANNEL_SCAN_INDEX_Z,
92a96cd0f9SSrinivas Pandruvada 	},
93a96cd0f9SSrinivas Pandruvada 	IIO_CHAN_SOFT_TIMESTAMP(3)
9445fe6f7dSsrinivas pandruvada };
9545fe6f7dSsrinivas pandruvada 
960e377f3bSSong Hongyan /* Channel definitions */
970e377f3bSSong Hongyan static const struct iio_chan_spec gravity_channels[] = {
980e377f3bSSong Hongyan 	{
990e377f3bSSong Hongyan 		.type = IIO_GRAVITY,
1000e377f3bSSong Hongyan 		.modified = 1,
1010e377f3bSSong Hongyan 		.channel2 = IIO_MOD_X,
1020e377f3bSSong Hongyan 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1030e377f3bSSong Hongyan 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
1040e377f3bSSong Hongyan 		BIT(IIO_CHAN_INFO_SCALE) |
1050e377f3bSSong Hongyan 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1060e377f3bSSong Hongyan 		BIT(IIO_CHAN_INFO_HYSTERESIS),
1070e377f3bSSong Hongyan 		.scan_index = CHANNEL_SCAN_INDEX_X,
1080e377f3bSSong Hongyan 	}, {
1090e377f3bSSong Hongyan 		.type = IIO_GRAVITY,
1100e377f3bSSong Hongyan 		.modified = 1,
1110e377f3bSSong Hongyan 		.channel2 = IIO_MOD_Y,
1120e377f3bSSong Hongyan 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1130e377f3bSSong Hongyan 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
1140e377f3bSSong Hongyan 		BIT(IIO_CHAN_INFO_SCALE) |
1150e377f3bSSong Hongyan 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1160e377f3bSSong Hongyan 		BIT(IIO_CHAN_INFO_HYSTERESIS),
1170e377f3bSSong Hongyan 		.scan_index = CHANNEL_SCAN_INDEX_Y,
1180e377f3bSSong Hongyan 	}, {
1190e377f3bSSong Hongyan 		.type = IIO_GRAVITY,
1200e377f3bSSong Hongyan 		.modified = 1,
1210e377f3bSSong Hongyan 		.channel2 = IIO_MOD_Z,
1220e377f3bSSong Hongyan 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
1230e377f3bSSong Hongyan 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
1240e377f3bSSong Hongyan 		BIT(IIO_CHAN_INFO_SCALE) |
1250e377f3bSSong Hongyan 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
1260e377f3bSSong Hongyan 		BIT(IIO_CHAN_INFO_HYSTERESIS),
1270e377f3bSSong Hongyan 		.scan_index = CHANNEL_SCAN_INDEX_Z,
1280e377f3bSSong Hongyan 	}
1290e377f3bSSong Hongyan };
1300e377f3bSSong Hongyan 
13145fe6f7dSsrinivas pandruvada /* Adjust channel real bits based on report descriptor */
13245fe6f7dSsrinivas pandruvada static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
13345fe6f7dSsrinivas pandruvada 						int channel, int size)
13445fe6f7dSsrinivas pandruvada {
13545fe6f7dSsrinivas pandruvada 	channels[channel].scan_type.sign = 's';
13645fe6f7dSsrinivas pandruvada 	/* Real storage bits will change based on the report desc. */
13745fe6f7dSsrinivas pandruvada 	channels[channel].scan_type.realbits = size * 8;
13845fe6f7dSsrinivas pandruvada 	/* Maximum size of a sample to capture is u32 */
13945fe6f7dSsrinivas pandruvada 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
14045fe6f7dSsrinivas pandruvada }
14145fe6f7dSsrinivas pandruvada 
14245fe6f7dSsrinivas pandruvada /* Channel read_raw handler */
14345fe6f7dSsrinivas pandruvada static int accel_3d_read_raw(struct iio_dev *indio_dev,
14445fe6f7dSsrinivas pandruvada 			      struct iio_chan_spec const *chan,
14545fe6f7dSsrinivas pandruvada 			      int *val, int *val2,
14645fe6f7dSsrinivas pandruvada 			      long mask)
14745fe6f7dSsrinivas pandruvada {
14845fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
14945fe6f7dSsrinivas pandruvada 	int report_id = -1;
15045fe6f7dSsrinivas pandruvada 	u32 address;
15145fe6f7dSsrinivas pandruvada 	int ret_type;
1520e377f3bSSong Hongyan 	struct hid_sensor_hub_device *hsdev =
1530e377f3bSSong Hongyan 					accel_state->common_attributes.hsdev;
15445fe6f7dSsrinivas pandruvada 
15545fe6f7dSsrinivas pandruvada 	*val = 0;
15645fe6f7dSsrinivas pandruvada 	*val2 = 0;
15745fe6f7dSsrinivas pandruvada 	switch (mask) {
158*67464a54SRodrigo Siqueira 	case IIO_CHAN_INFO_RAW:
159c6399453SSrinivas Pandruvada 		hid_sensor_power_state(&accel_state->common_attributes, true);
16045fe6f7dSsrinivas pandruvada 		report_id = accel_state->accel[chan->scan_index].report_id;
16145fe6f7dSsrinivas pandruvada 		address = accel_3d_addresses[chan->scan_index];
16245fe6f7dSsrinivas pandruvada 		if (report_id >= 0)
16345fe6f7dSsrinivas pandruvada 			*val = sensor_hub_input_attr_get_raw_value(
16445fe6f7dSsrinivas pandruvada 					accel_state->common_attributes.hsdev,
1650e377f3bSSong Hongyan 					hsdev->usage, address, report_id,
166b3f4737dSSrinivas Pandruvada 					SENSOR_HUB_SYNC);
16745fe6f7dSsrinivas pandruvada 		else {
16845fe6f7dSsrinivas pandruvada 			*val = 0;
169c6399453SSrinivas Pandruvada 			hid_sensor_power_state(&accel_state->common_attributes,
170c6399453SSrinivas Pandruvada 						 false);
17145fe6f7dSsrinivas pandruvada 			return -EINVAL;
17245fe6f7dSsrinivas pandruvada 		}
173c6399453SSrinivas Pandruvada 		hid_sensor_power_state(&accel_state->common_attributes, false);
17445fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT;
17545fe6f7dSsrinivas pandruvada 		break;
17645fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_SCALE:
17773a24988SSrinivas Pandruvada 		*val = accel_state->scale_pre_decml;
17873a24988SSrinivas Pandruvada 		*val2 = accel_state->scale_post_decml;
17973a24988SSrinivas Pandruvada 		ret_type = accel_state->scale_precision;
18045fe6f7dSsrinivas pandruvada 		break;
18145fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_OFFSET:
18273a24988SSrinivas Pandruvada 		*val = accel_state->value_offset;
18345fe6f7dSsrinivas pandruvada 		ret_type = IIO_VAL_INT;
18445fe6f7dSsrinivas pandruvada 		break;
18545fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
1862f084c17SSachin Kamat 		ret_type = hid_sensor_read_samp_freq_value(
18745fe6f7dSsrinivas pandruvada 			&accel_state->common_attributes, val, val2);
18845fe6f7dSsrinivas pandruvada 		break;
18945fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
1902f084c17SSachin Kamat 		ret_type = hid_sensor_read_raw_hyst_value(
19145fe6f7dSsrinivas pandruvada 			&accel_state->common_attributes, val, val2);
19245fe6f7dSsrinivas pandruvada 		break;
19345fe6f7dSsrinivas pandruvada 	default:
19445fe6f7dSsrinivas pandruvada 		ret_type = -EINVAL;
19545fe6f7dSsrinivas pandruvada 		break;
19645fe6f7dSsrinivas pandruvada 	}
19745fe6f7dSsrinivas pandruvada 
19845fe6f7dSsrinivas pandruvada 	return ret_type;
19945fe6f7dSsrinivas pandruvada }
20045fe6f7dSsrinivas pandruvada 
20145fe6f7dSsrinivas pandruvada /* Channel write_raw handler */
20245fe6f7dSsrinivas pandruvada static int accel_3d_write_raw(struct iio_dev *indio_dev,
20345fe6f7dSsrinivas pandruvada 			       struct iio_chan_spec const *chan,
20445fe6f7dSsrinivas pandruvada 			       int val,
20545fe6f7dSsrinivas pandruvada 			       int val2,
20645fe6f7dSsrinivas pandruvada 			       long mask)
20745fe6f7dSsrinivas pandruvada {
20845fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
20945fe6f7dSsrinivas pandruvada 	int ret = 0;
21045fe6f7dSsrinivas pandruvada 
21145fe6f7dSsrinivas pandruvada 	switch (mask) {
21245fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_SAMP_FREQ:
21345fe6f7dSsrinivas pandruvada 		ret = hid_sensor_write_samp_freq_value(
21445fe6f7dSsrinivas pandruvada 				&accel_state->common_attributes, val, val2);
21545fe6f7dSsrinivas pandruvada 		break;
21645fe6f7dSsrinivas pandruvada 	case IIO_CHAN_INFO_HYSTERESIS:
21745fe6f7dSsrinivas pandruvada 		ret = hid_sensor_write_raw_hyst_value(
21845fe6f7dSsrinivas pandruvada 				&accel_state->common_attributes, val, val2);
21945fe6f7dSsrinivas pandruvada 		break;
22045fe6f7dSsrinivas pandruvada 	default:
22145fe6f7dSsrinivas pandruvada 		ret = -EINVAL;
22245fe6f7dSsrinivas pandruvada 	}
22345fe6f7dSsrinivas pandruvada 
22445fe6f7dSsrinivas pandruvada 	return ret;
22545fe6f7dSsrinivas pandruvada }
22645fe6f7dSsrinivas pandruvada 
22745fe6f7dSsrinivas pandruvada static const struct iio_info accel_3d_info = {
22845fe6f7dSsrinivas pandruvada 	.read_raw = &accel_3d_read_raw,
22945fe6f7dSsrinivas pandruvada 	.write_raw = &accel_3d_write_raw,
23045fe6f7dSsrinivas pandruvada };
23145fe6f7dSsrinivas pandruvada 
23245fe6f7dSsrinivas pandruvada /* Function to push data to buffer */
233a96cd0f9SSrinivas Pandruvada static void hid_sensor_push_data(struct iio_dev *indio_dev, void *data,
234a96cd0f9SSrinivas Pandruvada 				 int len, int64_t timestamp)
23545fe6f7dSsrinivas pandruvada {
23645fe6f7dSsrinivas pandruvada 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
237a96cd0f9SSrinivas Pandruvada 	iio_push_to_buffers_with_timestamp(indio_dev, data, timestamp);
23845fe6f7dSsrinivas pandruvada }
23945fe6f7dSsrinivas pandruvada 
24045fe6f7dSsrinivas pandruvada /* Callback handler to send event after all samples are received and captured */
24145fe6f7dSsrinivas pandruvada static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
24245fe6f7dSsrinivas pandruvada 				unsigned usage_id,
24345fe6f7dSsrinivas pandruvada 				void *priv)
24445fe6f7dSsrinivas pandruvada {
24545fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
24645fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
24745fe6f7dSsrinivas pandruvada 
24856ff6be6SSrinivas Pandruvada 	dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n");
249a96cd0f9SSrinivas Pandruvada 	if (atomic_read(&accel_state->common_attributes.data_ready)) {
250a96cd0f9SSrinivas Pandruvada 		if (!accel_state->timestamp)
251a96cd0f9SSrinivas Pandruvada 			accel_state->timestamp = iio_get_time_ns(indio_dev);
252a96cd0f9SSrinivas Pandruvada 
25345fe6f7dSsrinivas pandruvada 		hid_sensor_push_data(indio_dev,
2548c60c7e7SLars-Peter Clausen 				     accel_state->accel_val,
255a96cd0f9SSrinivas Pandruvada 				     sizeof(accel_state->accel_val),
256a96cd0f9SSrinivas Pandruvada 				     accel_state->timestamp);
257a96cd0f9SSrinivas Pandruvada 
258a96cd0f9SSrinivas Pandruvada 		accel_state->timestamp = 0;
259a96cd0f9SSrinivas Pandruvada 	}
26045fe6f7dSsrinivas pandruvada 
26145fe6f7dSsrinivas pandruvada 	return 0;
26245fe6f7dSsrinivas pandruvada }
26345fe6f7dSsrinivas pandruvada 
26445fe6f7dSsrinivas pandruvada /* Capture samples in local storage */
26545fe6f7dSsrinivas pandruvada static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
26645fe6f7dSsrinivas pandruvada 				unsigned usage_id,
26745fe6f7dSsrinivas pandruvada 				size_t raw_len, char *raw_data,
26845fe6f7dSsrinivas pandruvada 				void *priv)
26945fe6f7dSsrinivas pandruvada {
27045fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
27145fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
27245fe6f7dSsrinivas pandruvada 	int offset;
27345fe6f7dSsrinivas pandruvada 	int ret = -EINVAL;
27445fe6f7dSsrinivas pandruvada 
27545fe6f7dSsrinivas pandruvada 	switch (usage_id) {
27645fe6f7dSsrinivas pandruvada 	case HID_USAGE_SENSOR_ACCEL_X_AXIS:
27745fe6f7dSsrinivas pandruvada 	case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
27845fe6f7dSsrinivas pandruvada 	case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
27945fe6f7dSsrinivas pandruvada 		offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
28045fe6f7dSsrinivas pandruvada 		accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
28145fe6f7dSsrinivas pandruvada 						*(u32 *)raw_data;
28245fe6f7dSsrinivas pandruvada 		ret = 0;
28345fe6f7dSsrinivas pandruvada 	break;
284a96cd0f9SSrinivas Pandruvada 	case HID_USAGE_SENSOR_TIME_TIMESTAMP:
285a96cd0f9SSrinivas Pandruvada 		accel_state->timestamp =
286a96cd0f9SSrinivas Pandruvada 			hid_sensor_convert_timestamp(
287a96cd0f9SSrinivas Pandruvada 					&accel_state->common_attributes,
288a96cd0f9SSrinivas Pandruvada 					*(int64_t *)raw_data);
289a96cd0f9SSrinivas Pandruvada 	break;
29045fe6f7dSsrinivas pandruvada 	default:
29145fe6f7dSsrinivas pandruvada 		break;
29245fe6f7dSsrinivas pandruvada 	}
29345fe6f7dSsrinivas pandruvada 
29445fe6f7dSsrinivas pandruvada 	return ret;
29545fe6f7dSsrinivas pandruvada }
29645fe6f7dSsrinivas pandruvada 
29745fe6f7dSsrinivas pandruvada /* Parse report which is specific to an usage id*/
29845fe6f7dSsrinivas pandruvada static int accel_3d_parse_report(struct platform_device *pdev,
29945fe6f7dSsrinivas pandruvada 				struct hid_sensor_hub_device *hsdev,
30045fe6f7dSsrinivas pandruvada 				struct iio_chan_spec *channels,
30145fe6f7dSsrinivas pandruvada 				unsigned usage_id,
30245fe6f7dSsrinivas pandruvada 				struct accel_3d_state *st)
30345fe6f7dSsrinivas pandruvada {
30445fe6f7dSsrinivas pandruvada 	int ret;
30545fe6f7dSsrinivas pandruvada 	int i;
30645fe6f7dSsrinivas pandruvada 
30745fe6f7dSsrinivas pandruvada 	for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
30845fe6f7dSsrinivas pandruvada 		ret = sensor_hub_input_get_attribute_info(hsdev,
30945fe6f7dSsrinivas pandruvada 				HID_INPUT_REPORT,
31045fe6f7dSsrinivas pandruvada 				usage_id,
31145fe6f7dSsrinivas pandruvada 				HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
31245fe6f7dSsrinivas pandruvada 				&st->accel[CHANNEL_SCAN_INDEX_X + i]);
31345fe6f7dSsrinivas pandruvada 		if (ret < 0)
31445fe6f7dSsrinivas pandruvada 			break;
31545fe6f7dSsrinivas pandruvada 		accel_3d_adjust_channel_bit_mask(channels,
31645fe6f7dSsrinivas pandruvada 				CHANNEL_SCAN_INDEX_X + i,
31745fe6f7dSsrinivas pandruvada 				st->accel[CHANNEL_SCAN_INDEX_X + i].size);
31845fe6f7dSsrinivas pandruvada 	}
31945fe6f7dSsrinivas pandruvada 	dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
32045fe6f7dSsrinivas pandruvada 			st->accel[0].index,
32145fe6f7dSsrinivas pandruvada 			st->accel[0].report_id,
32245fe6f7dSsrinivas pandruvada 			st->accel[1].index, st->accel[1].report_id,
32345fe6f7dSsrinivas pandruvada 			st->accel[2].index, st->accel[2].report_id);
32445fe6f7dSsrinivas pandruvada 
32573a24988SSrinivas Pandruvada 	st->scale_precision = hid_sensor_format_scale(
3260e377f3bSSong Hongyan 				hsdev->usage,
32773a24988SSrinivas Pandruvada 				&st->accel[CHANNEL_SCAN_INDEX_X],
32873a24988SSrinivas Pandruvada 				&st->scale_pre_decml, &st->scale_post_decml);
32973a24988SSrinivas Pandruvada 
33064528d03SSrinivas Pandruvada 	/* Set Sensitivity field ids, when there is no individual modifier */
33164528d03SSrinivas Pandruvada 	if (st->common_attributes.sensitivity.index < 0) {
33264528d03SSrinivas Pandruvada 		sensor_hub_input_get_attribute_info(hsdev,
33364528d03SSrinivas Pandruvada 			HID_FEATURE_REPORT, usage_id,
33464528d03SSrinivas Pandruvada 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
33564528d03SSrinivas Pandruvada 			HID_USAGE_SENSOR_DATA_ACCELERATION,
33664528d03SSrinivas Pandruvada 			&st->common_attributes.sensitivity);
33764528d03SSrinivas Pandruvada 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
33864528d03SSrinivas Pandruvada 			st->common_attributes.sensitivity.index,
33964528d03SSrinivas Pandruvada 			st->common_attributes.sensitivity.report_id);
34064528d03SSrinivas Pandruvada 	}
34164528d03SSrinivas Pandruvada 
34245fe6f7dSsrinivas pandruvada 	return ret;
34345fe6f7dSsrinivas pandruvada }
34445fe6f7dSsrinivas pandruvada 
34545fe6f7dSsrinivas pandruvada /* Function to initialize the processing for usage id */
346fc52692cSGreg Kroah-Hartman static int hid_accel_3d_probe(struct platform_device *pdev)
34745fe6f7dSsrinivas pandruvada {
34845fe6f7dSsrinivas pandruvada 	int ret = 0;
34940f4b1f0SJulia Lawall 	const char *name;
35045fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev;
35145fe6f7dSsrinivas pandruvada 	struct accel_3d_state *accel_state;
3520e377f3bSSong Hongyan 	const struct iio_chan_spec *channel_spec;
3530e377f3bSSong Hongyan 	int channel_size;
3540e377f3bSSong Hongyan 
35545fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
35645fe6f7dSsrinivas pandruvada 
3576b545eedSSachin Kamat 	indio_dev = devm_iio_device_alloc(&pdev->dev,
3586b545eedSSachin Kamat 					  sizeof(struct accel_3d_state));
3596b545eedSSachin Kamat 	if (indio_dev == NULL)
3606b545eedSSachin Kamat 		return -ENOMEM;
3616b545eedSSachin Kamat 
36245fe6f7dSsrinivas pandruvada 	platform_set_drvdata(pdev, indio_dev);
36345fe6f7dSsrinivas pandruvada 
36445fe6f7dSsrinivas pandruvada 	accel_state = iio_priv(indio_dev);
36545fe6f7dSsrinivas pandruvada 	accel_state->common_attributes.hsdev = hsdev;
36645fe6f7dSsrinivas pandruvada 	accel_state->common_attributes.pdev = pdev;
36745fe6f7dSsrinivas pandruvada 
3680e377f3bSSong Hongyan 	if (hsdev->usage == HID_USAGE_SENSOR_ACCEL_3D) {
3690e377f3bSSong Hongyan 		name = "accel_3d";
3700e377f3bSSong Hongyan 		channel_spec = accel_3d_channels;
3710e377f3bSSong Hongyan 		channel_size = sizeof(accel_3d_channels);
372862d1d89SSrinivas Pandruvada 		indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels);
3730e377f3bSSong Hongyan 	} else {
3740e377f3bSSong Hongyan 		name = "gravity";
3750e377f3bSSong Hongyan 		channel_spec = gravity_channels;
3760e377f3bSSong Hongyan 		channel_size = sizeof(gravity_channels);
377862d1d89SSrinivas Pandruvada 		indio_dev->num_channels = ARRAY_SIZE(gravity_channels);
3780e377f3bSSong Hongyan 	}
3790e377f3bSSong Hongyan 	ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage,
38045fe6f7dSsrinivas pandruvada 					&accel_state->common_attributes);
38145fe6f7dSsrinivas pandruvada 	if (ret) {
38245fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to setup common attributes\n");
3836b545eedSSachin Kamat 		return ret;
38445fe6f7dSsrinivas pandruvada 	}
3850e377f3bSSong Hongyan 	indio_dev->channels = kmemdup(channel_spec, channel_size, GFP_KERNEL);
38645fe6f7dSsrinivas pandruvada 
387b136faffSFabio Estevam 	if (!indio_dev->channels) {
38845fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to duplicate channels\n");
3896b545eedSSachin Kamat 		return -ENOMEM;
39045fe6f7dSsrinivas pandruvada 	}
391b136faffSFabio Estevam 	ret = accel_3d_parse_report(pdev, hsdev,
392b136faffSFabio Estevam 				(struct iio_chan_spec *)indio_dev->channels,
3930e377f3bSSong Hongyan 				hsdev->usage, accel_state);
39445fe6f7dSsrinivas pandruvada 	if (ret) {
39545fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to setup attributes\n");
39645fe6f7dSsrinivas pandruvada 		goto error_free_dev_mem;
39745fe6f7dSsrinivas pandruvada 	}
39845fe6f7dSsrinivas pandruvada 
39945fe6f7dSsrinivas pandruvada 	indio_dev->dev.parent = &pdev->dev;
40045fe6f7dSsrinivas pandruvada 	indio_dev->info = &accel_3d_info;
40145fe6f7dSsrinivas pandruvada 	indio_dev->name = name;
40245fe6f7dSsrinivas pandruvada 	indio_dev->modes = INDIO_DIRECT_MODE;
40345fe6f7dSsrinivas pandruvada 
40445fe6f7dSsrinivas pandruvada 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
40545fe6f7dSsrinivas pandruvada 		NULL, NULL);
40645fe6f7dSsrinivas pandruvada 	if (ret) {
40745fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
40845fe6f7dSsrinivas pandruvada 		goto error_free_dev_mem;
40945fe6f7dSsrinivas pandruvada 	}
41056ff6be6SSrinivas Pandruvada 	atomic_set(&accel_state->common_attributes.data_ready, 0);
41145fe6f7dSsrinivas pandruvada 	ret = hid_sensor_setup_trigger(indio_dev, name,
41245fe6f7dSsrinivas pandruvada 					&accel_state->common_attributes);
41345fe6f7dSsrinivas pandruvada 	if (ret < 0) {
41445fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "trigger setup failed\n");
41545fe6f7dSsrinivas pandruvada 		goto error_unreg_buffer_funcs;
41645fe6f7dSsrinivas pandruvada 	}
41745fe6f7dSsrinivas pandruvada 
41845fe6f7dSsrinivas pandruvada 	ret = iio_device_register(indio_dev);
41945fe6f7dSsrinivas pandruvada 	if (ret) {
42045fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "device register failed\n");
42145fe6f7dSsrinivas pandruvada 		goto error_remove_trigger;
42245fe6f7dSsrinivas pandruvada 	}
42345fe6f7dSsrinivas pandruvada 
42445fe6f7dSsrinivas pandruvada 	accel_state->callbacks.send_event = accel_3d_proc_event;
42545fe6f7dSsrinivas pandruvada 	accel_state->callbacks.capture_sample = accel_3d_capture_sample;
42645fe6f7dSsrinivas pandruvada 	accel_state->callbacks.pdev = pdev;
4270e377f3bSSong Hongyan 	ret = sensor_hub_register_callback(hsdev, hsdev->usage,
42845fe6f7dSsrinivas pandruvada 					&accel_state->callbacks);
42945fe6f7dSsrinivas pandruvada 	if (ret < 0) {
43045fe6f7dSsrinivas pandruvada 		dev_err(&pdev->dev, "callback reg failed\n");
43145fe6f7dSsrinivas pandruvada 		goto error_iio_unreg;
43245fe6f7dSsrinivas pandruvada 	}
43345fe6f7dSsrinivas pandruvada 
43445fe6f7dSsrinivas pandruvada 	return ret;
43545fe6f7dSsrinivas pandruvada 
43645fe6f7dSsrinivas pandruvada error_iio_unreg:
43745fe6f7dSsrinivas pandruvada 	iio_device_unregister(indio_dev);
43845fe6f7dSsrinivas pandruvada error_remove_trigger:
439ec7f68e0SSrinivas Pandruvada 	hid_sensor_remove_trigger(&accel_state->common_attributes);
44045fe6f7dSsrinivas pandruvada error_unreg_buffer_funcs:
44145fe6f7dSsrinivas pandruvada 	iio_triggered_buffer_cleanup(indio_dev);
44245fe6f7dSsrinivas pandruvada error_free_dev_mem:
44345fe6f7dSsrinivas pandruvada 	kfree(indio_dev->channels);
44445fe6f7dSsrinivas pandruvada 	return ret;
44545fe6f7dSsrinivas pandruvada }
44645fe6f7dSsrinivas pandruvada 
44745fe6f7dSsrinivas pandruvada /* Function to deinitialize the processing for usage id */
448fc52692cSGreg Kroah-Hartman static int hid_accel_3d_remove(struct platform_device *pdev)
44945fe6f7dSsrinivas pandruvada {
45045fe6f7dSsrinivas pandruvada 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
45145fe6f7dSsrinivas pandruvada 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
452ec7f68e0SSrinivas Pandruvada 	struct accel_3d_state *accel_state = iio_priv(indio_dev);
45345fe6f7dSsrinivas pandruvada 
4540e377f3bSSong Hongyan 	sensor_hub_remove_callback(hsdev, hsdev->usage);
45545fe6f7dSsrinivas pandruvada 	iio_device_unregister(indio_dev);
456ec7f68e0SSrinivas Pandruvada 	hid_sensor_remove_trigger(&accel_state->common_attributes);
45745fe6f7dSsrinivas pandruvada 	iio_triggered_buffer_cleanup(indio_dev);
45845fe6f7dSsrinivas pandruvada 	kfree(indio_dev->channels);
45945fe6f7dSsrinivas pandruvada 
46045fe6f7dSsrinivas pandruvada 	return 0;
46145fe6f7dSsrinivas pandruvada }
46245fe6f7dSsrinivas pandruvada 
46376ec50a1SKrzysztof Kozlowski static const struct platform_device_id hid_accel_3d_ids[] = {
46466c721f7SAlexander Holler 	{
46566c721f7SAlexander Holler 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
46666c721f7SAlexander Holler 		.name = "HID-SENSOR-200073",
46766c721f7SAlexander Holler 	},
4680e377f3bSSong Hongyan 	{	/* gravity sensor */
4690e377f3bSSong Hongyan 		.name = "HID-SENSOR-20007b",
4700e377f3bSSong Hongyan 	},
47166c721f7SAlexander Holler 	{ /* sentinel */ }
47266c721f7SAlexander Holler };
47366c721f7SAlexander Holler MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
47466c721f7SAlexander Holler 
47545fe6f7dSsrinivas pandruvada static struct platform_driver hid_accel_3d_platform_driver = {
47666c721f7SAlexander Holler 	.id_table = hid_accel_3d_ids,
47745fe6f7dSsrinivas pandruvada 	.driver = {
47866c721f7SAlexander Holler 		.name	= KBUILD_MODNAME,
479cbbcc937SSrinivas Pandruvada 		.pm	= &hid_sensor_pm_ops,
48045fe6f7dSsrinivas pandruvada 	},
48145fe6f7dSsrinivas pandruvada 	.probe		= hid_accel_3d_probe,
48245fe6f7dSsrinivas pandruvada 	.remove		= hid_accel_3d_remove,
48345fe6f7dSsrinivas pandruvada };
48445fe6f7dSsrinivas pandruvada module_platform_driver(hid_accel_3d_platform_driver);
48545fe6f7dSsrinivas pandruvada 
48645fe6f7dSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Accel 3D");
48745fe6f7dSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
48845fe6f7dSsrinivas pandruvada MODULE_LICENSE("GPL");
489