Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * HID Sensors Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2014, Intel Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/hid-sensor-hub.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/iio/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/iio/buffer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "../common/hid-sensors/hid-sensor-trigger.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define CHANNEL_SCAN_INDEX_PRESSURE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) struct press_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct hid_sensor_hub_callbacks callbacks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct hid_sensor_common common_attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct hid_sensor_hub_attribute_info press_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	u32 press_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	int scale_pre_decml;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	int scale_post_decml;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	int scale_precision;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	int value_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /* Channel definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static const struct iio_chan_spec press_channels[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		.type = IIO_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 		BIT(IIO_CHAN_INFO_SCALE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		BIT(IIO_CHAN_INFO_SAMP_FREQ) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		BIT(IIO_CHAN_INFO_HYSTERESIS),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		.scan_index = CHANNEL_SCAN_INDEX_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /* Adjust channel real bits based on report descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static void press_adjust_channel_bit_mask(struct iio_chan_spec *channels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 					int channel, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	channels[channel].scan_type.sign = 's';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	/* Real storage bits will change based on the report desc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	channels[channel].scan_type.realbits = size * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	/* Maximum size of a sample to capture is u32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	channels[channel].scan_type.storagebits = sizeof(u32) * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) /* Channel read_raw handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static int press_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 			      struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 			      int *val, int *val2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			      long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct press_state *press_state = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	int report_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	u32 address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	int ret_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	s32 min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	*val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	*val2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		switch (chan->scan_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		case  CHANNEL_SCAN_INDEX_PRESSURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			report_id = press_state->press_attr.report_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 			min = press_state->press_attr.logical_minimum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 			address = HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			report_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		if (report_id >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 			hid_sensor_power_state(&press_state->common_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 						true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			*val = sensor_hub_input_attr_get_raw_value(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 				press_state->common_attributes.hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 				HID_USAGE_SENSOR_PRESSURE, address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 				report_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 				SENSOR_HUB_SYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 				min < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			hid_sensor_power_state(&press_state->common_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 						false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			*val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		ret_type = IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	case IIO_CHAN_INFO_SCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		*val = press_state->scale_pre_decml;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		*val2 = press_state->scale_post_decml;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		ret_type = press_state->scale_precision;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	case IIO_CHAN_INFO_OFFSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		*val = press_state->value_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		ret_type = IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	case IIO_CHAN_INFO_SAMP_FREQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		ret_type = hid_sensor_read_samp_freq_value(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 				&press_state->common_attributes, val, val2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	case IIO_CHAN_INFO_HYSTERESIS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		ret_type = hid_sensor_read_raw_hyst_value(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 				&press_state->common_attributes, val, val2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		ret_type = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	return ret_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* Channel write_raw handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int press_write_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			       struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			       int val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			       int val2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			       long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct press_state *press_state = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	case IIO_CHAN_INFO_SAMP_FREQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		ret = hid_sensor_write_samp_freq_value(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 				&press_state->common_attributes, val, val2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	case IIO_CHAN_INFO_HYSTERESIS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		ret = hid_sensor_write_raw_hyst_value(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 				&press_state->common_attributes, val, val2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static const struct iio_info press_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	.read_raw = &press_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	.write_raw = &press_write_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /* Function to push data to buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 					int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	iio_push_to_buffers(indio_dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* Callback handler to send event after all samples are received and captured */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static int press_proc_event(struct hid_sensor_hub_device *hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 				unsigned usage_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	struct press_state *press_state = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	dev_dbg(&indio_dev->dev, "press_proc_event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (atomic_read(&press_state->common_attributes.data_ready))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		hid_sensor_push_data(indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				&press_state->press_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				sizeof(press_state->press_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* Capture samples in local storage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static int press_capture_sample(struct hid_sensor_hub_device *hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				unsigned usage_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				size_t raw_len, char *raw_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct iio_dev *indio_dev = platform_get_drvdata(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct press_state *press_state = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	switch (usage_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		press_state->press_data = *(u32 *)raw_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /* Parse report which is specific to an usage id*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int press_parse_report(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 				struct hid_sensor_hub_device *hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 				struct iio_chan_spec *channels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				unsigned usage_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 				struct press_state *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			usage_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			&st->press_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	press_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 					st->press_attr.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			st->press_attr.report_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	st->scale_precision = hid_sensor_format_scale(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 				HID_USAGE_SENSOR_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				&st->press_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 				&st->scale_pre_decml, &st->scale_post_decml);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	/* Set Sensitivity field ids, when there is no individual modifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (st->common_attributes.sensitivity.index < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		sensor_hub_input_get_attribute_info(hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			HID_FEATURE_REPORT, usage_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 			HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 			&st->common_attributes.sensitivity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			st->common_attributes.sensitivity.index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			st->common_attributes.sensitivity.report_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* Function to initialize the processing for usage id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static int hid_press_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	static const char *name = "press";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	struct press_state *press_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	indio_dev = devm_iio_device_alloc(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				sizeof(struct press_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	platform_set_drvdata(pdev, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	press_state = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	press_state->common_attributes.hsdev = hsdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	press_state->common_attributes.pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	ret = hid_sensor_parse_common_attributes(hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 					HID_USAGE_SENSOR_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 					&press_state->common_attributes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		dev_err(&pdev->dev, "failed to setup common attributes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	indio_dev->channels = kmemdup(press_channels, sizeof(press_channels),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 				      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (!indio_dev->channels) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		dev_err(&pdev->dev, "failed to duplicate channels\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	ret = press_parse_report(pdev, hsdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				 (struct iio_chan_spec *)indio_dev->channels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				 HID_USAGE_SENSOR_PRESSURE, press_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		dev_err(&pdev->dev, "failed to setup attributes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		goto error_free_dev_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	indio_dev->num_channels =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 				ARRAY_SIZE(press_channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	indio_dev->info = &press_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	indio_dev->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	indio_dev->modes = INDIO_DIRECT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	atomic_set(&press_state->common_attributes.data_ready, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	ret = hid_sensor_setup_trigger(indio_dev, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				&press_state->common_attributes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		dev_err(&pdev->dev, "trigger setup failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		goto error_free_dev_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	ret = iio_device_register(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		dev_err(&pdev->dev, "device register failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		goto error_remove_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	press_state->callbacks.send_event = press_proc_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	press_state->callbacks.capture_sample = press_capture_sample;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	press_state->callbacks.pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PRESSURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 					&press_state->callbacks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		dev_err(&pdev->dev, "callback reg failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		goto error_iio_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) error_iio_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	iio_device_unregister(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) error_remove_trigger:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	hid_sensor_remove_trigger(indio_dev, &press_state->common_attributes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) error_free_dev_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	kfree(indio_dev->channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* Function to deinitialize the processing for usage id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int hid_press_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	struct press_state *press_state = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PRESSURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	iio_device_unregister(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	hid_sensor_remove_trigger(indio_dev, &press_state->common_attributes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	kfree(indio_dev->channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static const struct platform_device_id hid_press_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		/* Format: HID-SENSOR-usage_id_in_hex_lowercase */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		.name = "HID-SENSOR-200031",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	{ /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) MODULE_DEVICE_TABLE(platform, hid_press_ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static struct platform_driver hid_press_platform_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	.id_table = hid_press_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		.name	= KBUILD_MODNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		.pm	= &hid_sensor_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	.probe		= hid_press_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	.remove		= hid_press_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) module_platform_driver(hid_press_platform_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) MODULE_DESCRIPTION("HID Sensor Pressure");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) MODULE_LICENSE("GPL");