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