18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * HID Sensors Driver
48c2ecf20Sopenharmony_ci * Copyright (c) 2013, Intel Corporation.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/device.h>
88c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/irq.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/hid-sensor-hub.h>
158c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
168c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h>
178c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h>
188c2ecf20Sopenharmony_ci#include "../common/hid-sensors/hid-sensor-trigger.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cienum incl_3d_channel {
218c2ecf20Sopenharmony_ci	CHANNEL_SCAN_INDEX_X,
228c2ecf20Sopenharmony_ci	CHANNEL_SCAN_INDEX_Y,
238c2ecf20Sopenharmony_ci	CHANNEL_SCAN_INDEX_Z,
248c2ecf20Sopenharmony_ci	INCLI_3D_CHANNEL_MAX,
258c2ecf20Sopenharmony_ci};
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct incl_3d_state {
288c2ecf20Sopenharmony_ci	struct hid_sensor_hub_callbacks callbacks;
298c2ecf20Sopenharmony_ci	struct hid_sensor_common common_attributes;
308c2ecf20Sopenharmony_ci	struct hid_sensor_hub_attribute_info incl[INCLI_3D_CHANNEL_MAX];
318c2ecf20Sopenharmony_ci	u32 incl_val[INCLI_3D_CHANNEL_MAX];
328c2ecf20Sopenharmony_ci	int scale_pre_decml;
338c2ecf20Sopenharmony_ci	int scale_post_decml;
348c2ecf20Sopenharmony_ci	int scale_precision;
358c2ecf20Sopenharmony_ci	int value_offset;
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const u32 incl_3d_addresses[INCLI_3D_CHANNEL_MAX] = {
398c2ecf20Sopenharmony_ci	HID_USAGE_SENSOR_ORIENT_TILT_X,
408c2ecf20Sopenharmony_ci	HID_USAGE_SENSOR_ORIENT_TILT_Y,
418c2ecf20Sopenharmony_ci	HID_USAGE_SENSOR_ORIENT_TILT_Z
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* Channel definitions */
458c2ecf20Sopenharmony_cistatic const struct iio_chan_spec incl_3d_channels[] = {
468c2ecf20Sopenharmony_ci	{
478c2ecf20Sopenharmony_ci		.type = IIO_INCLI,
488c2ecf20Sopenharmony_ci		.modified = 1,
498c2ecf20Sopenharmony_ci		.channel2 = IIO_MOD_X,
508c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
518c2ecf20Sopenharmony_ci		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
528c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SCALE) |
538c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
548c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_HYSTERESIS),
558c2ecf20Sopenharmony_ci		.scan_index = CHANNEL_SCAN_INDEX_X,
568c2ecf20Sopenharmony_ci	}, {
578c2ecf20Sopenharmony_ci		.type = IIO_INCLI,
588c2ecf20Sopenharmony_ci		.modified = 1,
598c2ecf20Sopenharmony_ci		.channel2 = IIO_MOD_Y,
608c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
618c2ecf20Sopenharmony_ci		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
628c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SCALE) |
638c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
648c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_HYSTERESIS),
658c2ecf20Sopenharmony_ci		.scan_index = CHANNEL_SCAN_INDEX_Y,
668c2ecf20Sopenharmony_ci	}, {
678c2ecf20Sopenharmony_ci		.type = IIO_INCLI,
688c2ecf20Sopenharmony_ci		.modified = 1,
698c2ecf20Sopenharmony_ci		.channel2 = IIO_MOD_Z,
708c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
718c2ecf20Sopenharmony_ci		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
728c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SCALE) |
738c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
748c2ecf20Sopenharmony_ci		BIT(IIO_CHAN_INFO_HYSTERESIS),
758c2ecf20Sopenharmony_ci		.scan_index = CHANNEL_SCAN_INDEX_Z,
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* Adjust channel real bits based on report descriptor */
808c2ecf20Sopenharmony_cistatic void incl_3d_adjust_channel_bit_mask(struct iio_chan_spec *chan,
818c2ecf20Sopenharmony_ci						int size)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	chan->scan_type.sign = 's';
848c2ecf20Sopenharmony_ci	/* Real storage bits will change based on the report desc. */
858c2ecf20Sopenharmony_ci	chan->scan_type.realbits = size * 8;
868c2ecf20Sopenharmony_ci	/* Maximum size of a sample to capture is u32 */
878c2ecf20Sopenharmony_ci	chan->scan_type.storagebits = sizeof(u32) * 8;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* Channel read_raw handler */
918c2ecf20Sopenharmony_cistatic int incl_3d_read_raw(struct iio_dev *indio_dev,
928c2ecf20Sopenharmony_ci			      struct iio_chan_spec const *chan,
938c2ecf20Sopenharmony_ci			      int *val, int *val2,
948c2ecf20Sopenharmony_ci			      long mask)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct incl_3d_state *incl_state = iio_priv(indio_dev);
978c2ecf20Sopenharmony_ci	int report_id = -1;
988c2ecf20Sopenharmony_ci	u32 address;
998c2ecf20Sopenharmony_ci	int ret_type;
1008c2ecf20Sopenharmony_ci	s32 min;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	*val = 0;
1038c2ecf20Sopenharmony_ci	*val2 = 0;
1048c2ecf20Sopenharmony_ci	switch (mask) {
1058c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW:
1068c2ecf20Sopenharmony_ci		hid_sensor_power_state(&incl_state->common_attributes, true);
1078c2ecf20Sopenharmony_ci		report_id = incl_state->incl[chan->scan_index].report_id;
1088c2ecf20Sopenharmony_ci		min = incl_state->incl[chan->scan_index].logical_minimum;
1098c2ecf20Sopenharmony_ci		address = incl_3d_addresses[chan->scan_index];
1108c2ecf20Sopenharmony_ci		if (report_id >= 0)
1118c2ecf20Sopenharmony_ci			*val = sensor_hub_input_attr_get_raw_value(
1128c2ecf20Sopenharmony_ci				incl_state->common_attributes.hsdev,
1138c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_INCLINOMETER_3D, address,
1148c2ecf20Sopenharmony_ci				report_id,
1158c2ecf20Sopenharmony_ci				SENSOR_HUB_SYNC,
1168c2ecf20Sopenharmony_ci				min < 0);
1178c2ecf20Sopenharmony_ci		else {
1188c2ecf20Sopenharmony_ci			hid_sensor_power_state(&incl_state->common_attributes,
1198c2ecf20Sopenharmony_ci						false);
1208c2ecf20Sopenharmony_ci			return -EINVAL;
1218c2ecf20Sopenharmony_ci		}
1228c2ecf20Sopenharmony_ci		hid_sensor_power_state(&incl_state->common_attributes, false);
1238c2ecf20Sopenharmony_ci		ret_type = IIO_VAL_INT;
1248c2ecf20Sopenharmony_ci		break;
1258c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SCALE:
1268c2ecf20Sopenharmony_ci		*val = incl_state->scale_pre_decml;
1278c2ecf20Sopenharmony_ci		*val2 = incl_state->scale_post_decml;
1288c2ecf20Sopenharmony_ci		ret_type = incl_state->scale_precision;
1298c2ecf20Sopenharmony_ci		break;
1308c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_OFFSET:
1318c2ecf20Sopenharmony_ci		*val = incl_state->value_offset;
1328c2ecf20Sopenharmony_ci		ret_type = IIO_VAL_INT;
1338c2ecf20Sopenharmony_ci		break;
1348c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SAMP_FREQ:
1358c2ecf20Sopenharmony_ci		ret_type = hid_sensor_read_samp_freq_value(
1368c2ecf20Sopenharmony_ci			&incl_state->common_attributes, val, val2);
1378c2ecf20Sopenharmony_ci		break;
1388c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_HYSTERESIS:
1398c2ecf20Sopenharmony_ci		ret_type = hid_sensor_read_raw_hyst_value(
1408c2ecf20Sopenharmony_ci			&incl_state->common_attributes, val, val2);
1418c2ecf20Sopenharmony_ci		break;
1428c2ecf20Sopenharmony_ci	default:
1438c2ecf20Sopenharmony_ci		ret_type = -EINVAL;
1448c2ecf20Sopenharmony_ci		break;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	return ret_type;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/* Channel write_raw handler */
1518c2ecf20Sopenharmony_cistatic int incl_3d_write_raw(struct iio_dev *indio_dev,
1528c2ecf20Sopenharmony_ci			       struct iio_chan_spec const *chan,
1538c2ecf20Sopenharmony_ci			       int val,
1548c2ecf20Sopenharmony_ci			       int val2,
1558c2ecf20Sopenharmony_ci			       long mask)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct incl_3d_state *incl_state = iio_priv(indio_dev);
1588c2ecf20Sopenharmony_ci	int ret;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	switch (mask) {
1618c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_SAMP_FREQ:
1628c2ecf20Sopenharmony_ci		ret = hid_sensor_write_samp_freq_value(
1638c2ecf20Sopenharmony_ci				&incl_state->common_attributes, val, val2);
1648c2ecf20Sopenharmony_ci		break;
1658c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_HYSTERESIS:
1668c2ecf20Sopenharmony_ci		ret = hid_sensor_write_raw_hyst_value(
1678c2ecf20Sopenharmony_ci				&incl_state->common_attributes, val, val2);
1688c2ecf20Sopenharmony_ci		break;
1698c2ecf20Sopenharmony_ci	default:
1708c2ecf20Sopenharmony_ci		ret = -EINVAL;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	return ret;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic const struct iio_info incl_3d_info = {
1778c2ecf20Sopenharmony_ci	.read_raw = &incl_3d_read_raw,
1788c2ecf20Sopenharmony_ci	.write_raw = &incl_3d_write_raw,
1798c2ecf20Sopenharmony_ci};
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/* Function to push data to buffer */
1828c2ecf20Sopenharmony_cistatic void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
1858c2ecf20Sopenharmony_ci	iio_push_to_buffers(indio_dev, (u8 *)data);
1868c2ecf20Sopenharmony_ci}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci/* Callback handler to send event after all samples are received and captured */
1898c2ecf20Sopenharmony_cistatic int incl_3d_proc_event(struct hid_sensor_hub_device *hsdev,
1908c2ecf20Sopenharmony_ci				unsigned usage_id,
1918c2ecf20Sopenharmony_ci				void *priv)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = platform_get_drvdata(priv);
1948c2ecf20Sopenharmony_ci	struct incl_3d_state *incl_state = iio_priv(indio_dev);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	dev_dbg(&indio_dev->dev, "incl_3d_proc_event\n");
1978c2ecf20Sopenharmony_ci	if (atomic_read(&incl_state->common_attributes.data_ready))
1988c2ecf20Sopenharmony_ci		hid_sensor_push_data(indio_dev,
1998c2ecf20Sopenharmony_ci				(u8 *)incl_state->incl_val,
2008c2ecf20Sopenharmony_ci				sizeof(incl_state->incl_val));
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	return 0;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/* Capture samples in local storage */
2068c2ecf20Sopenharmony_cistatic int incl_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
2078c2ecf20Sopenharmony_ci				unsigned usage_id,
2088c2ecf20Sopenharmony_ci				size_t raw_len, char *raw_data,
2098c2ecf20Sopenharmony_ci				void *priv)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = platform_get_drvdata(priv);
2128c2ecf20Sopenharmony_ci	struct incl_3d_state *incl_state = iio_priv(indio_dev);
2138c2ecf20Sopenharmony_ci	int ret = 0;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	switch (usage_id) {
2168c2ecf20Sopenharmony_ci	case HID_USAGE_SENSOR_ORIENT_TILT_X:
2178c2ecf20Sopenharmony_ci		incl_state->incl_val[CHANNEL_SCAN_INDEX_X] = *(u32 *)raw_data;
2188c2ecf20Sopenharmony_ci	break;
2198c2ecf20Sopenharmony_ci	case HID_USAGE_SENSOR_ORIENT_TILT_Y:
2208c2ecf20Sopenharmony_ci		incl_state->incl_val[CHANNEL_SCAN_INDEX_Y] = *(u32 *)raw_data;
2218c2ecf20Sopenharmony_ci	break;
2228c2ecf20Sopenharmony_ci	case HID_USAGE_SENSOR_ORIENT_TILT_Z:
2238c2ecf20Sopenharmony_ci		incl_state->incl_val[CHANNEL_SCAN_INDEX_Z] = *(u32 *)raw_data;
2248c2ecf20Sopenharmony_ci	break;
2258c2ecf20Sopenharmony_ci	default:
2268c2ecf20Sopenharmony_ci		ret = -EINVAL;
2278c2ecf20Sopenharmony_ci		break;
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	return ret;
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci/* Parse report which is specific to an usage id*/
2348c2ecf20Sopenharmony_cistatic int incl_3d_parse_report(struct platform_device *pdev,
2358c2ecf20Sopenharmony_ci				struct hid_sensor_hub_device *hsdev,
2368c2ecf20Sopenharmony_ci				struct iio_chan_spec *channels,
2378c2ecf20Sopenharmony_ci				unsigned usage_id,
2388c2ecf20Sopenharmony_ci				struct incl_3d_state *st)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	int ret;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	ret = sensor_hub_input_get_attribute_info(hsdev,
2438c2ecf20Sopenharmony_ci				HID_INPUT_REPORT,
2448c2ecf20Sopenharmony_ci				usage_id,
2458c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_ORIENT_TILT_X,
2468c2ecf20Sopenharmony_ci				&st->incl[CHANNEL_SCAN_INDEX_X]);
2478c2ecf20Sopenharmony_ci	if (ret)
2488c2ecf20Sopenharmony_ci		return ret;
2498c2ecf20Sopenharmony_ci	incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_X],
2508c2ecf20Sopenharmony_ci				st->incl[CHANNEL_SCAN_INDEX_X].size);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	ret = sensor_hub_input_get_attribute_info(hsdev,
2538c2ecf20Sopenharmony_ci				HID_INPUT_REPORT,
2548c2ecf20Sopenharmony_ci				usage_id,
2558c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_ORIENT_TILT_Y,
2568c2ecf20Sopenharmony_ci				&st->incl[CHANNEL_SCAN_INDEX_Y]);
2578c2ecf20Sopenharmony_ci	if (ret)
2588c2ecf20Sopenharmony_ci		return ret;
2598c2ecf20Sopenharmony_ci	incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Y],
2608c2ecf20Sopenharmony_ci				st->incl[CHANNEL_SCAN_INDEX_Y].size);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	ret = sensor_hub_input_get_attribute_info(hsdev,
2638c2ecf20Sopenharmony_ci				HID_INPUT_REPORT,
2648c2ecf20Sopenharmony_ci				usage_id,
2658c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_ORIENT_TILT_Z,
2668c2ecf20Sopenharmony_ci				&st->incl[CHANNEL_SCAN_INDEX_Z]);
2678c2ecf20Sopenharmony_ci	if (ret)
2688c2ecf20Sopenharmony_ci		return ret;
2698c2ecf20Sopenharmony_ci	incl_3d_adjust_channel_bit_mask(&channels[CHANNEL_SCAN_INDEX_Z],
2708c2ecf20Sopenharmony_ci				st->incl[CHANNEL_SCAN_INDEX_Z].size);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	dev_dbg(&pdev->dev, "incl_3d %x:%x, %x:%x, %x:%x\n",
2738c2ecf20Sopenharmony_ci			st->incl[0].index,
2748c2ecf20Sopenharmony_ci			st->incl[0].report_id,
2758c2ecf20Sopenharmony_ci			st->incl[1].index, st->incl[1].report_id,
2768c2ecf20Sopenharmony_ci			st->incl[2].index, st->incl[2].report_id);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	st->scale_precision = hid_sensor_format_scale(
2798c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_INCLINOMETER_3D,
2808c2ecf20Sopenharmony_ci				&st->incl[CHANNEL_SCAN_INDEX_X],
2818c2ecf20Sopenharmony_ci				&st->scale_pre_decml, &st->scale_post_decml);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/* Set Sensitivity field ids, when there is no individual modifier */
2848c2ecf20Sopenharmony_ci	if (st->common_attributes.sensitivity.index < 0) {
2858c2ecf20Sopenharmony_ci		sensor_hub_input_get_attribute_info(hsdev,
2868c2ecf20Sopenharmony_ci			HID_FEATURE_REPORT, usage_id,
2878c2ecf20Sopenharmony_ci			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
2888c2ecf20Sopenharmony_ci			HID_USAGE_SENSOR_DATA_ORIENTATION,
2898c2ecf20Sopenharmony_ci			&st->common_attributes.sensitivity);
2908c2ecf20Sopenharmony_ci		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
2918c2ecf20Sopenharmony_ci			st->common_attributes.sensitivity.index,
2928c2ecf20Sopenharmony_ci			st->common_attributes.sensitivity.report_id);
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci	return ret;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/* Function to initialize the processing for usage id */
2988c2ecf20Sopenharmony_cistatic int hid_incl_3d_probe(struct platform_device *pdev)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	int ret;
3018c2ecf20Sopenharmony_ci	static char *name = "incli_3d";
3028c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
3038c2ecf20Sopenharmony_ci	struct incl_3d_state *incl_state;
3048c2ecf20Sopenharmony_ci	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&pdev->dev,
3078c2ecf20Sopenharmony_ci					  sizeof(struct incl_3d_state));
3088c2ecf20Sopenharmony_ci	if (indio_dev == NULL)
3098c2ecf20Sopenharmony_ci		return -ENOMEM;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, indio_dev);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	incl_state = iio_priv(indio_dev);
3148c2ecf20Sopenharmony_ci	incl_state->common_attributes.hsdev = hsdev;
3158c2ecf20Sopenharmony_ci	incl_state->common_attributes.pdev = pdev;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	ret = hid_sensor_parse_common_attributes(hsdev,
3188c2ecf20Sopenharmony_ci				HID_USAGE_SENSOR_INCLINOMETER_3D,
3198c2ecf20Sopenharmony_ci				&incl_state->common_attributes);
3208c2ecf20Sopenharmony_ci	if (ret) {
3218c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to setup common attributes\n");
3228c2ecf20Sopenharmony_ci		return ret;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	indio_dev->channels = kmemdup(incl_3d_channels,
3268c2ecf20Sopenharmony_ci				      sizeof(incl_3d_channels), GFP_KERNEL);
3278c2ecf20Sopenharmony_ci	if (!indio_dev->channels) {
3288c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to duplicate channels\n");
3298c2ecf20Sopenharmony_ci		return -ENOMEM;
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	ret = incl_3d_parse_report(pdev, hsdev,
3338c2ecf20Sopenharmony_ci				   (struct iio_chan_spec *)indio_dev->channels,
3348c2ecf20Sopenharmony_ci				   HID_USAGE_SENSOR_INCLINOMETER_3D,
3358c2ecf20Sopenharmony_ci				   incl_state);
3368c2ecf20Sopenharmony_ci	if (ret) {
3378c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to setup attributes\n");
3388c2ecf20Sopenharmony_ci		goto error_free_dev_mem;
3398c2ecf20Sopenharmony_ci	}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	indio_dev->num_channels = ARRAY_SIZE(incl_3d_channels);
3428c2ecf20Sopenharmony_ci	indio_dev->info = &incl_3d_info;
3438c2ecf20Sopenharmony_ci	indio_dev->name = name;
3448c2ecf20Sopenharmony_ci	indio_dev->modes = INDIO_DIRECT_MODE;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	atomic_set(&incl_state->common_attributes.data_ready, 0);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	ret = hid_sensor_setup_trigger(indio_dev, name,
3498c2ecf20Sopenharmony_ci					&incl_state->common_attributes);
3508c2ecf20Sopenharmony_ci	if (ret) {
3518c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "trigger setup failed\n");
3528c2ecf20Sopenharmony_ci		goto error_free_dev_mem;
3538c2ecf20Sopenharmony_ci	}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	ret = iio_device_register(indio_dev);
3568c2ecf20Sopenharmony_ci	if (ret) {
3578c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "device register failed\n");
3588c2ecf20Sopenharmony_ci		goto error_remove_trigger;
3598c2ecf20Sopenharmony_ci	}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	incl_state->callbacks.send_event = incl_3d_proc_event;
3628c2ecf20Sopenharmony_ci	incl_state->callbacks.capture_sample = incl_3d_capture_sample;
3638c2ecf20Sopenharmony_ci	incl_state->callbacks.pdev = pdev;
3648c2ecf20Sopenharmony_ci	ret = sensor_hub_register_callback(hsdev,
3658c2ecf20Sopenharmony_ci					HID_USAGE_SENSOR_INCLINOMETER_3D,
3668c2ecf20Sopenharmony_ci					&incl_state->callbacks);
3678c2ecf20Sopenharmony_ci	if (ret) {
3688c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "callback reg failed\n");
3698c2ecf20Sopenharmony_ci		goto error_iio_unreg;
3708c2ecf20Sopenharmony_ci	}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	return 0;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cierror_iio_unreg:
3758c2ecf20Sopenharmony_ci	iio_device_unregister(indio_dev);
3768c2ecf20Sopenharmony_cierror_remove_trigger:
3778c2ecf20Sopenharmony_ci	hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes);
3788c2ecf20Sopenharmony_cierror_free_dev_mem:
3798c2ecf20Sopenharmony_ci	kfree(indio_dev->channels);
3808c2ecf20Sopenharmony_ci	return ret;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci/* Function to deinitialize the processing for usage id */
3848c2ecf20Sopenharmony_cistatic int hid_incl_3d_remove(struct platform_device *pdev)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
3878c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
3888c2ecf20Sopenharmony_ci	struct incl_3d_state *incl_state = iio_priv(indio_dev);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_INCLINOMETER_3D);
3918c2ecf20Sopenharmony_ci	iio_device_unregister(indio_dev);
3928c2ecf20Sopenharmony_ci	hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes);
3938c2ecf20Sopenharmony_ci	kfree(indio_dev->channels);
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	return 0;
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic const struct platform_device_id hid_incl_3d_ids[] = {
3998c2ecf20Sopenharmony_ci	{
4008c2ecf20Sopenharmony_ci		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
4018c2ecf20Sopenharmony_ci		.name = "HID-SENSOR-200086",
4028c2ecf20Sopenharmony_ci	},
4038c2ecf20Sopenharmony_ci	{ /* sentinel */ }
4048c2ecf20Sopenharmony_ci};
4058c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, hid_incl_3d_ids);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic struct platform_driver hid_incl_3d_platform_driver = {
4088c2ecf20Sopenharmony_ci	.id_table = hid_incl_3d_ids,
4098c2ecf20Sopenharmony_ci	.driver = {
4108c2ecf20Sopenharmony_ci		.name	= KBUILD_MODNAME,
4118c2ecf20Sopenharmony_ci		.pm	= &hid_sensor_pm_ops,
4128c2ecf20Sopenharmony_ci	},
4138c2ecf20Sopenharmony_ci	.probe		= hid_incl_3d_probe,
4148c2ecf20Sopenharmony_ci	.remove		= hid_incl_3d_remove,
4158c2ecf20Sopenharmony_ci};
4168c2ecf20Sopenharmony_cimodule_platform_driver(hid_incl_3d_platform_driver);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HID Sensor Inclinometer 3D");
4198c2ecf20Sopenharmony_ciMODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
4208c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
421