Thomas Gleixner | a61127c | 2019-05-29 16:57:49 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 2 | /* |
| 3 | * HID Sensors Driver |
| 4 | * Copyright (c) 2012, Intel Corporation. |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 5 | */ |
| 6 | #ifndef _HID_SENSORS_HUB_H |
| 7 | #define _HID_SENSORS_HUB_H |
| 8 | |
| 9 | #include <linux/hid.h> |
| 10 | #include <linux/hid-sensor-ids.h> |
Srinivas Pandruvada | ec7f68e | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 11 | #include <linux/iio/iio.h> |
| 12 | #include <linux/iio/trigger.h> |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * struct hid_sensor_hub_attribute_info - Attribute info |
| 16 | * @usage_id: Parent usage id of a physical device. |
| 17 | * @attrib_id: Attribute id for this attribute. |
| 18 | * @report_id: Report id in which this information resides. |
| 19 | * @index: Field index in the report. |
| 20 | * @units: Measurment unit for this attribute. |
| 21 | * @unit_expo: Exponent used in the data. |
| 22 | * @size: Size in bytes for data size. |
Srinivas Pandruvada | f9d904a | 2015-01-07 10:14:43 -0800 | [diff] [blame] | 23 | * @logical_minimum: Logical minimum value for this attribute. |
| 24 | * @logical_maximum: Logical maximum value for this attribute. |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 25 | */ |
| 26 | struct hid_sensor_hub_attribute_info { |
| 27 | u32 usage_id; |
| 28 | u32 attrib_id; |
| 29 | s32 report_id; |
| 30 | s32 index; |
| 31 | s32 units; |
| 32 | s32 unit_expo; |
| 33 | s32 size; |
Srinivas Pandruvada | 9f740ff | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 34 | s32 logical_minimum; |
| 35 | s32 logical_maximum; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /** |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame] | 39 | * struct sensor_hub_pending - Synchronous read pending information |
| 40 | * @status: Pending status true/false. |
| 41 | * @ready: Completion synchronization data. |
| 42 | * @usage_id: Usage id for physical device, E.g. Gyro usage id. |
| 43 | * @attr_usage_id: Usage Id of a field, E.g. X-AXIS for a gyro. |
| 44 | * @raw_size: Response size for a read request. |
| 45 | * @raw_data: Place holder for received response. |
| 46 | */ |
| 47 | struct sensor_hub_pending { |
| 48 | bool status; |
| 49 | struct completion ready; |
| 50 | u32 usage_id; |
| 51 | u32 attr_usage_id; |
| 52 | int raw_size; |
| 53 | u8 *raw_data; |
| 54 | }; |
| 55 | |
| 56 | /** |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 57 | * struct hid_sensor_hub_device - Stores the hub instance data |
| 58 | * @hdev: Stores the hid instance. |
| 59 | * @vendor_id: Vendor id of hub device. |
| 60 | * @product_id: Product id of hub device. |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame] | 61 | * @usage: Usage id for this hub device instance. |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 62 | * @start_collection_index: Starting index for a phy type collection |
| 63 | * @end_collection_index: Last index for a phy type collection |
Srinivas Pandruvada | 2d94e52 | 2015-05-07 14:26:34 -0700 | [diff] [blame] | 64 | * @mutex_ptr: synchronizing mutex pointer. |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame] | 65 | * @pending: Holds information of pending sync read request. |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 66 | */ |
| 67 | struct hid_sensor_hub_device { |
| 68 | struct hid_device *hdev; |
| 69 | u32 vendor_id; |
| 70 | u32 product_id; |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame] | 71 | u32 usage; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 72 | int start_collection_index; |
| 73 | int end_collection_index; |
Srinivas Pandruvada | 2d94e52 | 2015-05-07 14:26:34 -0700 | [diff] [blame] | 74 | struct mutex *mutex_ptr; |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame] | 75 | struct sensor_hub_pending pending; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /** |
| 79 | * struct hid_sensor_hub_callbacks - Client callback functions |
| 80 | * @pdev: Platform device instance of the client driver. |
| 81 | * @suspend: Suspend callback. |
| 82 | * @resume: Resume callback. |
| 83 | * @capture_sample: Callback to get a sample. |
| 84 | * @send_event: Send notification to indicate all samples are |
| 85 | * captured, process and send event |
| 86 | */ |
| 87 | struct hid_sensor_hub_callbacks { |
| 88 | struct platform_device *pdev; |
| 89 | int (*suspend)(struct hid_sensor_hub_device *hsdev, void *priv); |
| 90 | int (*resume)(struct hid_sensor_hub_device *hsdev, void *priv); |
| 91 | int (*capture_sample)(struct hid_sensor_hub_device *hsdev, |
| 92 | u32 usage_id, size_t raw_len, char *raw_data, |
| 93 | void *priv); |
| 94 | int (*send_event)(struct hid_sensor_hub_device *hsdev, u32 usage_id, |
| 95 | void *priv); |
| 96 | }; |
| 97 | |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 98 | /** |
| 99 | * sensor_hub_device_open() - Open hub device |
| 100 | * @hsdev: Hub device instance. |
| 101 | * |
| 102 | * Used to open hid device for sensor hub. |
| 103 | */ |
| 104 | int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev); |
| 105 | |
| 106 | /** |
| 107 | * sensor_hub_device_clode() - Close hub device |
| 108 | * @hsdev: Hub device instance. |
| 109 | * |
| 110 | * Used to clode hid device for sensor hub. |
| 111 | */ |
| 112 | void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev); |
| 113 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 114 | /* Registration functions */ |
| 115 | |
| 116 | /** |
| 117 | * sensor_hub_register_callback() - Register client callbacks |
| 118 | * @hsdev: Hub device instance. |
| 119 | * @usage_id: Usage id of the client (E.g. 0x200076 for Gyro). |
| 120 | * @usage_callback: Callback function storage |
| 121 | * |
| 122 | * Used to register callbacks by client processing drivers. Sensor |
| 123 | * hub core driver will call these callbacks to offload processing |
| 124 | * of data streams and notifications. |
| 125 | */ |
| 126 | int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev, |
| 127 | u32 usage_id, |
| 128 | struct hid_sensor_hub_callbacks *usage_callback); |
| 129 | |
| 130 | /** |
| 131 | * sensor_hub_remove_callback() - Remove client callbacks |
| 132 | * @hsdev: Hub device instance. |
| 133 | * @usage_id: Usage id of the client (E.g. 0x200076 for Gyro). |
| 134 | * |
| 135 | * If there is a callback registred, this call will remove that |
| 136 | * callbacks, so that it will stop data and event notifications. |
| 137 | */ |
| 138 | int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev, |
| 139 | u32 usage_id); |
| 140 | |
| 141 | |
| 142 | /* Hid sensor hub core interfaces */ |
| 143 | |
| 144 | /** |
| 145 | * sensor_hub_input_get_attribute_info() - Get an attribute information |
| 146 | * @hsdev: Hub device instance. |
| 147 | * @type: Type of this attribute, input/output/feature |
| 148 | * @usage_id: Attribute usage id of parent physical device as per spec |
| 149 | * @attr_usage_id: Attribute usage id as per spec |
| 150 | * @info: return information about attribute after parsing report |
| 151 | * |
| 152 | * Parses report and returns the attribute information such as report id, |
| 153 | * field index, units and exponet etc. |
| 154 | */ |
| 155 | int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev, |
| 156 | u8 type, |
| 157 | u32 usage_id, u32 attr_usage_id, |
| 158 | struct hid_sensor_hub_attribute_info *info); |
| 159 | |
| 160 | /** |
| 161 | * sensor_hub_input_attr_get_raw_value() - Synchronous read request |
Srinivas Pandruvada | f9d904a | 2015-01-07 10:14:43 -0800 | [diff] [blame] | 162 | * @hsdev: Hub device instance. |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 163 | * @usage_id: Attribute usage id of parent physical device as per spec |
| 164 | * @attr_usage_id: Attribute usage id as per spec |
| 165 | * @report_id: Report id to look for |
Srinivas Pandruvada | b3f4737 | 2015-02-19 15:33:56 -0800 | [diff] [blame] | 166 | * @flag: Synchronous or asynchronous read |
Hans de Goede | 0145b50 | 2018-10-31 15:20:05 +0100 | [diff] [blame] | 167 | * @is_signed: If true then fields < 32 bits will be sign-extended |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 168 | * |
Srinivas Pandruvada | b3f4737 | 2015-02-19 15:33:56 -0800 | [diff] [blame] | 169 | * Issues a synchronous or asynchronous read request for an input attribute. |
| 170 | * Returns data upto 32 bits. |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 171 | */ |
| 172 | |
Srinivas Pandruvada | b3f4737 | 2015-02-19 15:33:56 -0800 | [diff] [blame] | 173 | enum sensor_hub_read_flags { |
| 174 | SENSOR_HUB_SYNC, |
| 175 | SENSOR_HUB_ASYNC, |
| 176 | }; |
| 177 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 178 | int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev, |
Srinivas Pandruvada | b3f4737 | 2015-02-19 15:33:56 -0800 | [diff] [blame] | 179 | u32 usage_id, |
| 180 | u32 attr_usage_id, u32 report_id, |
Hans de Goede | 0145b50 | 2018-10-31 15:20:05 +0100 | [diff] [blame] | 181 | enum sensor_hub_read_flags flag, |
| 182 | bool is_signed |
Srinivas Pandruvada | b3f4737 | 2015-02-19 15:33:56 -0800 | [diff] [blame] | 183 | ); |
| 184 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 185 | /** |
| 186 | * sensor_hub_set_feature() - Feature set request |
Srinivas Pandruvada | f9d904a | 2015-01-07 10:14:43 -0800 | [diff] [blame] | 187 | * @hsdev: Hub device instance. |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 188 | * @report_id: Report id to look for |
| 189 | * @field_index: Field index inside a report |
Srinivas Pandruvada | 3950e03 | 2015-02-19 15:35:26 -0800 | [diff] [blame] | 190 | * @buffer_size: size of the buffer |
| 191 | * @buffer: buffer to use in the feature set |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 192 | * |
| 193 | * Used to set a field in feature report. For example this can set polling |
| 194 | * interval, sensitivity, activate/deactivate state. |
| 195 | */ |
| 196 | int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, |
Srinivas Pandruvada | 3950e03 | 2015-02-19 15:35:26 -0800 | [diff] [blame] | 197 | u32 field_index, int buffer_size, void *buffer); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * sensor_hub_get_feature() - Feature get request |
Srinivas Pandruvada | f9d904a | 2015-01-07 10:14:43 -0800 | [diff] [blame] | 201 | * @hsdev: Hub device instance. |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 202 | * @report_id: Report id to look for |
| 203 | * @field_index: Field index inside a report |
Srinivas Pandruvada | 6adc83f | 2015-02-19 15:35:25 -0800 | [diff] [blame] | 204 | * @buffer_size: size of the buffer |
| 205 | * @buffer: buffer to copy output |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 206 | * |
| 207 | * Used to get a field in feature report. For example this can get polling |
Srinivas Pandruvada | 6adc83f | 2015-02-19 15:35:25 -0800 | [diff] [blame] | 208 | * interval, sensitivity, activate/deactivate state. On success it returns |
| 209 | * number of bytes copied to buffer. On failure, it returns value < 0. |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 210 | */ |
| 211 | int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, |
Srinivas Pandruvada | 6adc83f | 2015-02-19 15:35:25 -0800 | [diff] [blame] | 212 | u32 field_index, int buffer_size, void *buffer); |
Alexander Holler | 2974cdf | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 213 | |
| 214 | /* hid-sensor-attributes */ |
| 215 | |
| 216 | /* Common hid sensor iio structure */ |
Alexander Holler | e07c6d1 | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 217 | struct hid_sensor_common { |
Alexander Holler | 2974cdf | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 218 | struct hid_sensor_hub_device *hsdev; |
| 219 | struct platform_device *pdev; |
| 220 | unsigned usage_id; |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 221 | atomic_t data_ready; |
Srinivas Pandruvada | 1e25aa9 | 2015-06-01 16:36:27 -0700 | [diff] [blame] | 222 | atomic_t user_requested_state; |
Srinivas Pandruvada | ad7532c | 2017-10-11 09:35:01 -0700 | [diff] [blame] | 223 | atomic_t runtime_pm_enable; |
Srinivas Pandruvada | 5d9854e | 2017-04-07 17:13:17 -0700 | [diff] [blame] | 224 | int poll_interval; |
| 225 | int raw_hystersis; |
Srinivas Pandruvada | 138bc79 | 2017-04-07 19:22:06 -0700 | [diff] [blame] | 226 | int latency_ms; |
Srinivas Pandruvada | ec7f68e | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 227 | struct iio_trigger *trigger; |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 228 | int timestamp_ns_scale; |
Alexander Holler | 2974cdf | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 229 | struct hid_sensor_hub_attribute_info poll; |
| 230 | struct hid_sensor_hub_attribute_info report_state; |
| 231 | struct hid_sensor_hub_attribute_info power_state; |
| 232 | struct hid_sensor_hub_attribute_info sensitivity; |
Srinivas Pandruvada | 138bc79 | 2017-04-07 19:22:06 -0700 | [diff] [blame] | 233 | struct hid_sensor_hub_attribute_info report_latency; |
Srinivas Pandruvada | 7f6cf74 | 2016-08-15 12:12:47 -0700 | [diff] [blame] | 234 | struct work_struct work; |
Alexander Holler | 2974cdf | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
Andy Shevchenko | 15261f6 | 2013-08-14 11:07:08 +0300 | [diff] [blame] | 237 | /* Convert from hid unit expo to regular exponent */ |
Alexander Holler | 2974cdf | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 238 | static inline int hid_sensor_convert_exponent(int unit_expo) |
| 239 | { |
| 240 | if (unit_expo < 0x08) |
| 241 | return unit_expo; |
| 242 | else if (unit_expo <= 0x0f) |
| 243 | return -(0x0f-unit_expo+1); |
| 244 | else |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | int hid_sensor_parse_common_attributes(struct hid_sensor_hub_device *hsdev, |
| 249 | u32 usage_id, |
Alexander Holler | e07c6d1 | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 250 | struct hid_sensor_common *st); |
| 251 | int hid_sensor_write_raw_hyst_value(struct hid_sensor_common *st, |
Alexander Holler | 2974cdf | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 252 | int val1, int val2); |
Alexander Holler | e07c6d1 | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 253 | int hid_sensor_read_raw_hyst_value(struct hid_sensor_common *st, |
Alexander Holler | 2974cdf | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 254 | int *val1, int *val2); |
Alexander Holler | e07c6d1 | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 255 | int hid_sensor_write_samp_freq_value(struct hid_sensor_common *st, |
Alexander Holler | 2974cdf | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 256 | int val1, int val2); |
Alexander Holler | e07c6d1 | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 257 | int hid_sensor_read_samp_freq_value(struct hid_sensor_common *st, |
Alexander Holler | 2974cdf | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 258 | int *val1, int *val2); |
| 259 | |
Srinivas Pandruvada | e02cee4 | 2014-01-23 18:50:21 -0800 | [diff] [blame] | 260 | int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev, |
| 261 | u32 report_id, int field_index, u32 usage_id); |
| 262 | |
Srinivas Pandruvada | 5d02edfc | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 263 | int hid_sensor_format_scale(u32 usage_id, |
| 264 | struct hid_sensor_hub_attribute_info *attr_info, |
| 265 | int *val0, int *val1); |
| 266 | |
Srinivas Pandruvada | 9030924 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 267 | s32 hid_sensor_read_poll_value(struct hid_sensor_common *st); |
| 268 | |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 269 | int64_t hid_sensor_convert_timestamp(struct hid_sensor_common *st, |
| 270 | int64_t raw_value); |
Srinivas Pandruvada | 138bc79 | 2017-04-07 19:22:06 -0700 | [diff] [blame] | 271 | bool hid_sensor_batch_mode_supported(struct hid_sensor_common *st); |
| 272 | int hid_sensor_set_report_latency(struct hid_sensor_common *st, int latency); |
| 273 | int hid_sensor_get_report_latency(struct hid_sensor_common *st); |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 274 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 275 | #endif |