Thomas Gleixner | a61127c | 2019-05-29 16:57:49 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 2 | /* |
| 3 | * HID Sensors Driver |
| 4 | * Copyright (c) 2012, Intel Corporation. |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/device.h> |
| 7 | #include <linux/platform_device.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/irq.h> |
| 11 | #include <linux/slab.h> |
Srinivas Pandruvada | c639945 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 12 | #include <linux/delay.h> |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 13 | #include <linux/hid-sensor-hub.h> |
| 14 | #include <linux/iio/iio.h> |
| 15 | #include <linux/iio/sysfs.h> |
| 16 | #include <linux/iio/buffer.h> |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 17 | #include "../common/hid-sensors/hid-sensor-trigger.h" |
| 18 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 19 | enum accel_3d_channel { |
| 20 | CHANNEL_SCAN_INDEX_X, |
| 21 | CHANNEL_SCAN_INDEX_Y, |
| 22 | CHANNEL_SCAN_INDEX_Z, |
| 23 | ACCEL_3D_CHANNEL_MAX, |
| 24 | }; |
| 25 | |
| 26 | struct accel_3d_state { |
| 27 | struct hid_sensor_hub_callbacks callbacks; |
Alexander Holler | e07c6d1 | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 28 | struct hid_sensor_common common_attributes; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 29 | struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX]; |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 30 | /* Reserve for 3 channels + padding + timestamp */ |
| 31 | u32 accel_val[ACCEL_3D_CHANNEL_MAX + 3]; |
Srinivas Pandruvada | 73a2498 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 32 | int scale_pre_decml; |
| 33 | int scale_post_decml; |
| 34 | int scale_precision; |
| 35 | int value_offset; |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 36 | int64_t timestamp; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = { |
| 40 | HID_USAGE_SENSOR_ACCEL_X_AXIS, |
| 41 | HID_USAGE_SENSOR_ACCEL_Y_AXIS, |
| 42 | HID_USAGE_SENSOR_ACCEL_Z_AXIS |
| 43 | }; |
| 44 | |
| 45 | /* Channel definitions */ |
| 46 | static const struct iio_chan_spec accel_3d_channels[] = { |
| 47 | { |
| 48 | .type = IIO_ACCEL, |
| 49 | .modified = 1, |
| 50 | .channel2 = IIO_MOD_X, |
Srinivas Pandruvada | c639945 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 51 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Jonathan Cameron | ecbe18f | 2013-02-27 19:03:04 +0000 | [diff] [blame] | 52 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 53 | BIT(IIO_CHAN_INFO_SCALE) | |
| 54 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 55 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 56 | .scan_index = CHANNEL_SCAN_INDEX_X, |
| 57 | }, { |
| 58 | .type = IIO_ACCEL, |
| 59 | .modified = 1, |
| 60 | .channel2 = IIO_MOD_Y, |
Srinivas Pandruvada | c639945 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 61 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Jonathan Cameron | ecbe18f | 2013-02-27 19:03:04 +0000 | [diff] [blame] | 62 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 63 | BIT(IIO_CHAN_INFO_SCALE) | |
| 64 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 65 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 66 | .scan_index = CHANNEL_SCAN_INDEX_Y, |
| 67 | }, { |
| 68 | .type = IIO_ACCEL, |
| 69 | .modified = 1, |
| 70 | .channel2 = IIO_MOD_Z, |
Srinivas Pandruvada | c639945 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 71 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Jonathan Cameron | ecbe18f | 2013-02-27 19:03:04 +0000 | [diff] [blame] | 72 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 73 | BIT(IIO_CHAN_INFO_SCALE) | |
| 74 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 75 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 76 | .scan_index = CHANNEL_SCAN_INDEX_Z, |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 77 | }, |
| 78 | IIO_CHAN_SOFT_TIMESTAMP(3) |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 79 | }; |
| 80 | |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 81 | /* Channel definitions */ |
| 82 | static const struct iio_chan_spec gravity_channels[] = { |
| 83 | { |
| 84 | .type = IIO_GRAVITY, |
| 85 | .modified = 1, |
| 86 | .channel2 = IIO_MOD_X, |
| 87 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 88 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 89 | BIT(IIO_CHAN_INFO_SCALE) | |
| 90 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 91 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
| 92 | .scan_index = CHANNEL_SCAN_INDEX_X, |
| 93 | }, { |
| 94 | .type = IIO_GRAVITY, |
| 95 | .modified = 1, |
| 96 | .channel2 = IIO_MOD_Y, |
| 97 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 98 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 99 | BIT(IIO_CHAN_INFO_SCALE) | |
| 100 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 101 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
| 102 | .scan_index = CHANNEL_SCAN_INDEX_Y, |
| 103 | }, { |
| 104 | .type = IIO_GRAVITY, |
| 105 | .modified = 1, |
| 106 | .channel2 = IIO_MOD_Z, |
| 107 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 108 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 109 | BIT(IIO_CHAN_INFO_SCALE) | |
| 110 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 111 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
| 112 | .scan_index = CHANNEL_SCAN_INDEX_Z, |
| 113 | } |
| 114 | }; |
| 115 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 116 | /* Adjust channel real bits based on report descriptor */ |
| 117 | static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels, |
| 118 | int channel, int size) |
| 119 | { |
| 120 | channels[channel].scan_type.sign = 's'; |
| 121 | /* Real storage bits will change based on the report desc. */ |
| 122 | channels[channel].scan_type.realbits = size * 8; |
| 123 | /* Maximum size of a sample to capture is u32 */ |
| 124 | channels[channel].scan_type.storagebits = sizeof(u32) * 8; |
| 125 | } |
| 126 | |
| 127 | /* Channel read_raw handler */ |
| 128 | static int accel_3d_read_raw(struct iio_dev *indio_dev, |
| 129 | struct iio_chan_spec const *chan, |
| 130 | int *val, int *val2, |
| 131 | long mask) |
| 132 | { |
| 133 | struct accel_3d_state *accel_state = iio_priv(indio_dev); |
| 134 | int report_id = -1; |
| 135 | u32 address; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 136 | int ret_type; |
Hans de Goede | 0145b50 | 2018-10-31 15:20:05 +0100 | [diff] [blame] | 137 | s32 min; |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 138 | struct hid_sensor_hub_device *hsdev = |
| 139 | accel_state->common_attributes.hsdev; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 140 | |
| 141 | *val = 0; |
| 142 | *val2 = 0; |
| 143 | switch (mask) { |
Rodrigo Siqueira | 67464a5 | 2018-03-06 09:02:57 -0300 | [diff] [blame] | 144 | case IIO_CHAN_INFO_RAW: |
Srinivas Pandruvada | c639945 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 145 | hid_sensor_power_state(&accel_state->common_attributes, true); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 146 | report_id = accel_state->accel[chan->scan_index].report_id; |
Hans de Goede | 0145b50 | 2018-10-31 15:20:05 +0100 | [diff] [blame] | 147 | min = accel_state->accel[chan->scan_index].logical_minimum; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 148 | address = accel_3d_addresses[chan->scan_index]; |
| 149 | if (report_id >= 0) |
| 150 | *val = sensor_hub_input_attr_get_raw_value( |
Srinivas Pandruvada | c639945 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 151 | accel_state->common_attributes.hsdev, |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 152 | hsdev->usage, address, report_id, |
Hans de Goede | 0145b50 | 2018-10-31 15:20:05 +0100 | [diff] [blame] | 153 | SENSOR_HUB_SYNC, |
| 154 | min < 0); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 155 | else { |
| 156 | *val = 0; |
Srinivas Pandruvada | c639945 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 157 | hid_sensor_power_state(&accel_state->common_attributes, |
| 158 | false); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 159 | return -EINVAL; |
| 160 | } |
Srinivas Pandruvada | c639945 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 161 | hid_sensor_power_state(&accel_state->common_attributes, false); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 162 | ret_type = IIO_VAL_INT; |
| 163 | break; |
| 164 | case IIO_CHAN_INFO_SCALE: |
Srinivas Pandruvada | 73a2498 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 165 | *val = accel_state->scale_pre_decml; |
| 166 | *val2 = accel_state->scale_post_decml; |
| 167 | ret_type = accel_state->scale_precision; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 168 | break; |
| 169 | case IIO_CHAN_INFO_OFFSET: |
Srinivas Pandruvada | 73a2498 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 170 | *val = accel_state->value_offset; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 171 | ret_type = IIO_VAL_INT; |
| 172 | break; |
| 173 | case IIO_CHAN_INFO_SAMP_FREQ: |
Sachin Kamat | 2f084c1 | 2014-03-07 07:44:00 +0000 | [diff] [blame] | 174 | ret_type = hid_sensor_read_samp_freq_value( |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 175 | &accel_state->common_attributes, val, val2); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 176 | break; |
| 177 | case IIO_CHAN_INFO_HYSTERESIS: |
Sachin Kamat | 2f084c1 | 2014-03-07 07:44:00 +0000 | [diff] [blame] | 178 | ret_type = hid_sensor_read_raw_hyst_value( |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 179 | &accel_state->common_attributes, val, val2); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 180 | break; |
| 181 | default: |
| 182 | ret_type = -EINVAL; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | return ret_type; |
| 187 | } |
| 188 | |
| 189 | /* Channel write_raw handler */ |
| 190 | static int accel_3d_write_raw(struct iio_dev *indio_dev, |
| 191 | struct iio_chan_spec const *chan, |
| 192 | int val, |
| 193 | int val2, |
| 194 | long mask) |
| 195 | { |
| 196 | struct accel_3d_state *accel_state = iio_priv(indio_dev); |
| 197 | int ret = 0; |
| 198 | |
| 199 | switch (mask) { |
| 200 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 201 | ret = hid_sensor_write_samp_freq_value( |
| 202 | &accel_state->common_attributes, val, val2); |
| 203 | break; |
| 204 | case IIO_CHAN_INFO_HYSTERESIS: |
| 205 | ret = hid_sensor_write_raw_hyst_value( |
| 206 | &accel_state->common_attributes, val, val2); |
| 207 | break; |
| 208 | default: |
| 209 | ret = -EINVAL; |
| 210 | } |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 215 | static const struct iio_info accel_3d_info = { |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 216 | .read_raw = &accel_3d_read_raw, |
| 217 | .write_raw = &accel_3d_write_raw, |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | /* Function to push data to buffer */ |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 221 | static void hid_sensor_push_data(struct iio_dev *indio_dev, void *data, |
| 222 | int len, int64_t timestamp) |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 223 | { |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 224 | dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 225 | iio_push_to_buffers_with_timestamp(indio_dev, data, timestamp); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* Callback handler to send event after all samples are received and captured */ |
| 229 | static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev, |
| 230 | unsigned usage_id, |
| 231 | void *priv) |
| 232 | { |
| 233 | struct iio_dev *indio_dev = platform_get_drvdata(priv); |
| 234 | struct accel_3d_state *accel_state = iio_priv(indio_dev); |
| 235 | |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 236 | dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n"); |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 237 | if (atomic_read(&accel_state->common_attributes.data_ready)) { |
| 238 | if (!accel_state->timestamp) |
| 239 | accel_state->timestamp = iio_get_time_ns(indio_dev); |
| 240 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 241 | hid_sensor_push_data(indio_dev, |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 242 | accel_state->accel_val, |
| 243 | sizeof(accel_state->accel_val), |
| 244 | accel_state->timestamp); |
| 245 | |
| 246 | accel_state->timestamp = 0; |
| 247 | } |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /* Capture samples in local storage */ |
| 253 | static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev, |
| 254 | unsigned usage_id, |
| 255 | size_t raw_len, char *raw_data, |
| 256 | void *priv) |
| 257 | { |
| 258 | struct iio_dev *indio_dev = platform_get_drvdata(priv); |
| 259 | struct accel_3d_state *accel_state = iio_priv(indio_dev); |
| 260 | int offset; |
| 261 | int ret = -EINVAL; |
| 262 | |
| 263 | switch (usage_id) { |
| 264 | case HID_USAGE_SENSOR_ACCEL_X_AXIS: |
| 265 | case HID_USAGE_SENSOR_ACCEL_Y_AXIS: |
| 266 | case HID_USAGE_SENSOR_ACCEL_Z_AXIS: |
| 267 | offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS; |
| 268 | accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] = |
| 269 | *(u32 *)raw_data; |
| 270 | ret = 0; |
| 271 | break; |
Srinivas Pandruvada | a96cd0f | 2016-11-28 14:41:16 -0800 | [diff] [blame] | 272 | case HID_USAGE_SENSOR_TIME_TIMESTAMP: |
| 273 | accel_state->timestamp = |
| 274 | hid_sensor_convert_timestamp( |
| 275 | &accel_state->common_attributes, |
| 276 | *(int64_t *)raw_data); |
| 277 | break; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 278 | default: |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | /* Parse report which is specific to an usage id*/ |
| 286 | static int accel_3d_parse_report(struct platform_device *pdev, |
| 287 | struct hid_sensor_hub_device *hsdev, |
| 288 | struct iio_chan_spec *channels, |
| 289 | unsigned usage_id, |
| 290 | struct accel_3d_state *st) |
| 291 | { |
| 292 | int ret; |
| 293 | int i; |
| 294 | |
| 295 | for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) { |
| 296 | ret = sensor_hub_input_get_attribute_info(hsdev, |
| 297 | HID_INPUT_REPORT, |
| 298 | usage_id, |
| 299 | HID_USAGE_SENSOR_ACCEL_X_AXIS + i, |
| 300 | &st->accel[CHANNEL_SCAN_INDEX_X + i]); |
| 301 | if (ret < 0) |
| 302 | break; |
| 303 | accel_3d_adjust_channel_bit_mask(channels, |
| 304 | CHANNEL_SCAN_INDEX_X + i, |
| 305 | st->accel[CHANNEL_SCAN_INDEX_X + i].size); |
| 306 | } |
| 307 | dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n", |
| 308 | st->accel[0].index, |
| 309 | st->accel[0].report_id, |
| 310 | st->accel[1].index, st->accel[1].report_id, |
| 311 | st->accel[2].index, st->accel[2].report_id); |
| 312 | |
Srinivas Pandruvada | 73a2498 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 313 | st->scale_precision = hid_sensor_format_scale( |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 314 | hsdev->usage, |
Srinivas Pandruvada | 73a2498 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 315 | &st->accel[CHANNEL_SCAN_INDEX_X], |
| 316 | &st->scale_pre_decml, &st->scale_post_decml); |
| 317 | |
Srinivas Pandruvada | 64528d0 | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 318 | /* Set Sensitivity field ids, when there is no individual modifier */ |
| 319 | if (st->common_attributes.sensitivity.index < 0) { |
| 320 | sensor_hub_input_get_attribute_info(hsdev, |
| 321 | HID_FEATURE_REPORT, usage_id, |
| 322 | HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | |
| 323 | HID_USAGE_SENSOR_DATA_ACCELERATION, |
| 324 | &st->common_attributes.sensitivity); |
| 325 | dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", |
| 326 | st->common_attributes.sensitivity.index, |
| 327 | st->common_attributes.sensitivity.report_id); |
| 328 | } |
| 329 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | /* Function to initialize the processing for usage id */ |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 334 | static int hid_accel_3d_probe(struct platform_device *pdev) |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 335 | { |
| 336 | int ret = 0; |
Julia Lawall | 40f4b1f | 2017-05-04 22:10:52 +0200 | [diff] [blame] | 337 | const char *name; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 338 | struct iio_dev *indio_dev; |
| 339 | struct accel_3d_state *accel_state; |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 340 | const struct iio_chan_spec *channel_spec; |
| 341 | int channel_size; |
| 342 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 343 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 344 | |
Sachin Kamat | 6b545ee | 2013-07-23 07:47:00 +0100 | [diff] [blame] | 345 | indio_dev = devm_iio_device_alloc(&pdev->dev, |
| 346 | sizeof(struct accel_3d_state)); |
| 347 | if (indio_dev == NULL) |
| 348 | return -ENOMEM; |
| 349 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 350 | platform_set_drvdata(pdev, indio_dev); |
| 351 | |
| 352 | accel_state = iio_priv(indio_dev); |
| 353 | accel_state->common_attributes.hsdev = hsdev; |
| 354 | accel_state->common_attributes.pdev = pdev; |
| 355 | |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 356 | if (hsdev->usage == HID_USAGE_SENSOR_ACCEL_3D) { |
| 357 | name = "accel_3d"; |
| 358 | channel_spec = accel_3d_channels; |
| 359 | channel_size = sizeof(accel_3d_channels); |
Srinivas Pandruvada | 862d1d8 | 2017-03-29 16:23:35 -0700 | [diff] [blame] | 360 | indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels); |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 361 | } else { |
| 362 | name = "gravity"; |
| 363 | channel_spec = gravity_channels; |
| 364 | channel_size = sizeof(gravity_channels); |
Srinivas Pandruvada | 862d1d8 | 2017-03-29 16:23:35 -0700 | [diff] [blame] | 365 | indio_dev->num_channels = ARRAY_SIZE(gravity_channels); |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 366 | } |
| 367 | ret = hid_sensor_parse_common_attributes(hsdev, hsdev->usage, |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 368 | &accel_state->common_attributes); |
| 369 | if (ret) { |
| 370 | dev_err(&pdev->dev, "failed to setup common attributes\n"); |
Sachin Kamat | 6b545ee | 2013-07-23 07:47:00 +0100 | [diff] [blame] | 371 | return ret; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 372 | } |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 373 | indio_dev->channels = kmemdup(channel_spec, channel_size, GFP_KERNEL); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 374 | |
Fabio Estevam | b136faf | 2015-05-13 15:06:09 -0300 | [diff] [blame] | 375 | if (!indio_dev->channels) { |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 376 | dev_err(&pdev->dev, "failed to duplicate channels\n"); |
Sachin Kamat | 6b545ee | 2013-07-23 07:47:00 +0100 | [diff] [blame] | 377 | return -ENOMEM; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 378 | } |
Fabio Estevam | b136faf | 2015-05-13 15:06:09 -0300 | [diff] [blame] | 379 | ret = accel_3d_parse_report(pdev, hsdev, |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 380 | (struct iio_chan_spec *)indio_dev->channels, |
| 381 | hsdev->usage, accel_state); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 382 | if (ret) { |
| 383 | dev_err(&pdev->dev, "failed to setup attributes\n"); |
| 384 | goto error_free_dev_mem; |
| 385 | } |
| 386 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 387 | indio_dev->info = &accel_3d_info; |
| 388 | indio_dev->name = name; |
| 389 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 390 | |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 391 | atomic_set(&accel_state->common_attributes.data_ready, 0); |
Alexandru Ardelean | 067fda1 | 2020-04-24 07:34:18 +0300 | [diff] [blame] | 392 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 393 | ret = hid_sensor_setup_trigger(indio_dev, name, |
| 394 | &accel_state->common_attributes); |
| 395 | if (ret < 0) { |
| 396 | dev_err(&pdev->dev, "trigger setup failed\n"); |
Alexandru Ardelean | 067fda1 | 2020-04-24 07:34:18 +0300 | [diff] [blame] | 397 | goto error_free_dev_mem; |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | ret = iio_device_register(indio_dev); |
| 401 | if (ret) { |
| 402 | dev_err(&pdev->dev, "device register failed\n"); |
| 403 | goto error_remove_trigger; |
| 404 | } |
| 405 | |
| 406 | accel_state->callbacks.send_event = accel_3d_proc_event; |
| 407 | accel_state->callbacks.capture_sample = accel_3d_capture_sample; |
| 408 | accel_state->callbacks.pdev = pdev; |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 409 | ret = sensor_hub_register_callback(hsdev, hsdev->usage, |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 410 | &accel_state->callbacks); |
| 411 | if (ret < 0) { |
| 412 | dev_err(&pdev->dev, "callback reg failed\n"); |
| 413 | goto error_iio_unreg; |
| 414 | } |
| 415 | |
| 416 | return ret; |
| 417 | |
| 418 | error_iio_unreg: |
| 419 | iio_device_unregister(indio_dev); |
| 420 | error_remove_trigger: |
Alexandru Ardelean | 067fda1 | 2020-04-24 07:34:18 +0300 | [diff] [blame] | 421 | hid_sensor_remove_trigger(indio_dev, &accel_state->common_attributes); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 422 | error_free_dev_mem: |
| 423 | kfree(indio_dev->channels); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 424 | return ret; |
| 425 | } |
| 426 | |
| 427 | /* Function to deinitialize the processing for usage id */ |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 428 | static int hid_accel_3d_remove(struct platform_device *pdev) |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 429 | { |
| 430 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; |
| 431 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
Srinivas Pandruvada | ec7f68e | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 432 | struct accel_3d_state *accel_state = iio_priv(indio_dev); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 433 | |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 434 | sensor_hub_remove_callback(hsdev, hsdev->usage); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 435 | iio_device_unregister(indio_dev); |
Alexandru Ardelean | 067fda1 | 2020-04-24 07:34:18 +0300 | [diff] [blame] | 436 | hid_sensor_remove_trigger(indio_dev, &accel_state->common_attributes); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 437 | kfree(indio_dev->channels); |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
Krzysztof Kozlowski | 76ec50a | 2015-05-02 00:53:30 +0900 | [diff] [blame] | 442 | static const struct platform_device_id hid_accel_3d_ids[] = { |
Alexander Holler | 66c721f | 2013-07-10 09:31:00 +0100 | [diff] [blame] | 443 | { |
| 444 | /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ |
| 445 | .name = "HID-SENSOR-200073", |
| 446 | }, |
Song Hongyan | 0e377f3 | 2017-01-05 18:24:04 +0800 | [diff] [blame] | 447 | { /* gravity sensor */ |
| 448 | .name = "HID-SENSOR-20007b", |
| 449 | }, |
Alexander Holler | 66c721f | 2013-07-10 09:31:00 +0100 | [diff] [blame] | 450 | { /* sentinel */ } |
| 451 | }; |
| 452 | MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids); |
| 453 | |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 454 | static struct platform_driver hid_accel_3d_platform_driver = { |
Alexander Holler | 66c721f | 2013-07-10 09:31:00 +0100 | [diff] [blame] | 455 | .id_table = hid_accel_3d_ids, |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 456 | .driver = { |
Alexander Holler | 66c721f | 2013-07-10 09:31:00 +0100 | [diff] [blame] | 457 | .name = KBUILD_MODNAME, |
Srinivas Pandruvada | cbbcc93 | 2015-01-07 10:55:54 -0800 | [diff] [blame] | 458 | .pm = &hid_sensor_pm_ops, |
srinivas pandruvada | 45fe6f7 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 459 | }, |
| 460 | .probe = hid_accel_3d_probe, |
| 461 | .remove = hid_accel_3d_remove, |
| 462 | }; |
| 463 | module_platform_driver(hid_accel_3d_platform_driver); |
| 464 | |
| 465 | MODULE_DESCRIPTION("HID Sensor Accel 3D"); |
| 466 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); |
| 467 | MODULE_LICENSE("GPL"); |