srinivas pandruvada | c5bdbef | 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 | 0964f70 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 25 | #include <linux/delay.h> |
srinivas pandruvada | c5bdbef | 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 | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 32 | #include "../common/hid-sensors/hid-sensor-trigger.h" |
| 33 | |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 34 | enum gyro_3d_channel { |
| 35 | CHANNEL_SCAN_INDEX_X, |
| 36 | CHANNEL_SCAN_INDEX_Y, |
| 37 | CHANNEL_SCAN_INDEX_Z, |
| 38 | GYRO_3D_CHANNEL_MAX, |
| 39 | }; |
| 40 | |
| 41 | struct gyro_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 | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 44 | struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX]; |
| 45 | u32 gyro_val[GYRO_3D_CHANNEL_MAX]; |
Srinivas Pandruvada | 6c7db77 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 46 | int scale_pre_decml; |
| 47 | int scale_post_decml; |
| 48 | int scale_precision; |
| 49 | int value_offset; |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = { |
| 53 | HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS, |
| 54 | HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS, |
| 55 | HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS |
| 56 | }; |
| 57 | |
| 58 | /* Channel definitions */ |
| 59 | static const struct iio_chan_spec gyro_3d_channels[] = { |
| 60 | { |
| 61 | .type = IIO_ANGL_VEL, |
| 62 | .modified = 1, |
| 63 | .channel2 = IIO_MOD_X, |
Srinivas Pandruvada | 0964f70 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 64 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Jonathan Cameron | ecbe18f | 2013-02-27 19:03:04 +0000 | [diff] [blame] | 65 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 66 | BIT(IIO_CHAN_INFO_SCALE) | |
| 67 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 68 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 69 | .scan_index = CHANNEL_SCAN_INDEX_X, |
| 70 | }, { |
| 71 | .type = IIO_ANGL_VEL, |
| 72 | .modified = 1, |
| 73 | .channel2 = IIO_MOD_Y, |
Srinivas Pandruvada | 0964f70 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 74 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Jonathan Cameron | ecbe18f | 2013-02-27 19:03:04 +0000 | [diff] [blame] | 75 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 76 | BIT(IIO_CHAN_INFO_SCALE) | |
| 77 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 78 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 79 | .scan_index = CHANNEL_SCAN_INDEX_Y, |
| 80 | }, { |
| 81 | .type = IIO_ANGL_VEL, |
| 82 | .modified = 1, |
| 83 | .channel2 = IIO_MOD_Z, |
Srinivas Pandruvada | 0964f70 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 84 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
Jonathan Cameron | ecbe18f | 2013-02-27 19:03:04 +0000 | [diff] [blame] | 85 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | |
| 86 | BIT(IIO_CHAN_INFO_SCALE) | |
| 87 | BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 88 | BIT(IIO_CHAN_INFO_HYSTERESIS), |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 89 | .scan_index = CHANNEL_SCAN_INDEX_Z, |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | /* Adjust channel real bits based on report descriptor */ |
| 94 | static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels, |
| 95 | int channel, int size) |
| 96 | { |
| 97 | channels[channel].scan_type.sign = 's'; |
| 98 | /* Real storage bits will change based on the report desc. */ |
| 99 | channels[channel].scan_type.realbits = size * 8; |
| 100 | /* Maximum size of a sample to capture is u32 */ |
| 101 | channels[channel].scan_type.storagebits = sizeof(u32) * 8; |
| 102 | } |
| 103 | |
| 104 | /* Channel read_raw handler */ |
| 105 | static int gyro_3d_read_raw(struct iio_dev *indio_dev, |
| 106 | struct iio_chan_spec const *chan, |
| 107 | int *val, int *val2, |
| 108 | long mask) |
| 109 | { |
| 110 | struct gyro_3d_state *gyro_state = iio_priv(indio_dev); |
| 111 | int report_id = -1; |
| 112 | u32 address; |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 113 | int ret_type; |
| 114 | |
| 115 | *val = 0; |
| 116 | *val2 = 0; |
| 117 | switch (mask) { |
| 118 | case 0: |
Srinivas Pandruvada | 0964f70 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 119 | hid_sensor_power_state(&gyro_state->common_attributes, true); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 120 | report_id = gyro_state->gyro[chan->scan_index].report_id; |
| 121 | address = gyro_3d_addresses[chan->scan_index]; |
| 122 | if (report_id >= 0) |
| 123 | *val = sensor_hub_input_attr_get_raw_value( |
Srinivas Pandruvada | 0964f70 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 124 | gyro_state->common_attributes.hsdev, |
| 125 | HID_USAGE_SENSOR_GYRO_3D, address, |
Srinivas Pandruvada | b3f4737 | 2015-02-19 15:33:56 -0800 | [diff] [blame] | 126 | report_id, |
| 127 | SENSOR_HUB_SYNC); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 128 | else { |
| 129 | *val = 0; |
Srinivas Pandruvada | 0964f70 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 130 | hid_sensor_power_state(&gyro_state->common_attributes, |
| 131 | false); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 132 | return -EINVAL; |
| 133 | } |
Srinivas Pandruvada | 0964f70 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 134 | hid_sensor_power_state(&gyro_state->common_attributes, false); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 135 | ret_type = IIO_VAL_INT; |
| 136 | break; |
| 137 | case IIO_CHAN_INFO_SCALE: |
Srinivas Pandruvada | 6c7db77 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 138 | *val = gyro_state->scale_pre_decml; |
| 139 | *val2 = gyro_state->scale_post_decml; |
| 140 | ret_type = gyro_state->scale_precision; |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 141 | break; |
| 142 | case IIO_CHAN_INFO_OFFSET: |
Srinivas Pandruvada | 6c7db77 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 143 | *val = gyro_state->value_offset; |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 144 | ret_type = IIO_VAL_INT; |
| 145 | break; |
| 146 | case IIO_CHAN_INFO_SAMP_FREQ: |
Sachin Kamat | 2779380 | 2014-03-07 07:44:00 +0000 | [diff] [blame] | 147 | ret_type = hid_sensor_read_samp_freq_value( |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 148 | &gyro_state->common_attributes, val, val2); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 149 | break; |
| 150 | case IIO_CHAN_INFO_HYSTERESIS: |
Sachin Kamat | 2779380 | 2014-03-07 07:44:00 +0000 | [diff] [blame] | 151 | ret_type = hid_sensor_read_raw_hyst_value( |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 152 | &gyro_state->common_attributes, val, val2); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 153 | break; |
| 154 | default: |
| 155 | ret_type = -EINVAL; |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | return ret_type; |
| 160 | } |
| 161 | |
| 162 | /* Channel write_raw handler */ |
| 163 | static int gyro_3d_write_raw(struct iio_dev *indio_dev, |
| 164 | struct iio_chan_spec const *chan, |
| 165 | int val, |
| 166 | int val2, |
| 167 | long mask) |
| 168 | { |
| 169 | struct gyro_3d_state *gyro_state = iio_priv(indio_dev); |
| 170 | int ret = 0; |
| 171 | |
| 172 | switch (mask) { |
| 173 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 174 | ret = hid_sensor_write_samp_freq_value( |
| 175 | &gyro_state->common_attributes, val, val2); |
| 176 | break; |
| 177 | case IIO_CHAN_INFO_HYSTERESIS: |
| 178 | ret = hid_sensor_write_raw_hyst_value( |
| 179 | &gyro_state->common_attributes, val, val2); |
| 180 | break; |
| 181 | default: |
| 182 | ret = -EINVAL; |
| 183 | } |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 188 | static const struct iio_info gyro_3d_info = { |
| 189 | .driver_module = THIS_MODULE, |
| 190 | .read_raw = &gyro_3d_read_raw, |
| 191 | .write_raw = &gyro_3d_write_raw, |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | /* Function to push data to buffer */ |
Lars-Peter Clausen | 8c60c7e | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 195 | static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data, |
| 196 | int len) |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 197 | { |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 198 | dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); |
Lars-Peter Clausen | 8c60c7e | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 199 | iio_push_to_buffers(indio_dev, data); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* Callback handler to send event after all samples are received and captured */ |
| 203 | static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev, |
| 204 | unsigned usage_id, |
| 205 | void *priv) |
| 206 | { |
| 207 | struct iio_dev *indio_dev = platform_get_drvdata(priv); |
| 208 | struct gyro_3d_state *gyro_state = iio_priv(indio_dev); |
| 209 | |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 210 | dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n"); |
| 211 | if (atomic_read(&gyro_state->common_attributes.data_ready)) |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 212 | hid_sensor_push_data(indio_dev, |
Lars-Peter Clausen | 8c60c7e | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 213 | gyro_state->gyro_val, |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 214 | sizeof(gyro_state->gyro_val)); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* Capture samples in local storage */ |
| 220 | static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev, |
| 221 | unsigned usage_id, |
| 222 | size_t raw_len, char *raw_data, |
| 223 | void *priv) |
| 224 | { |
| 225 | struct iio_dev *indio_dev = platform_get_drvdata(priv); |
| 226 | struct gyro_3d_state *gyro_state = iio_priv(indio_dev); |
| 227 | int offset; |
| 228 | int ret = -EINVAL; |
| 229 | |
| 230 | switch (usage_id) { |
| 231 | case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS: |
| 232 | case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS: |
| 233 | case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS: |
| 234 | offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS; |
| 235 | gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] = |
| 236 | *(u32 *)raw_data; |
| 237 | ret = 0; |
| 238 | break; |
| 239 | default: |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | /* Parse report which is specific to an usage id*/ |
| 247 | static int gyro_3d_parse_report(struct platform_device *pdev, |
| 248 | struct hid_sensor_hub_device *hsdev, |
| 249 | struct iio_chan_spec *channels, |
| 250 | unsigned usage_id, |
| 251 | struct gyro_3d_state *st) |
| 252 | { |
| 253 | int ret; |
| 254 | int i; |
| 255 | |
| 256 | for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) { |
| 257 | ret = sensor_hub_input_get_attribute_info(hsdev, |
| 258 | HID_INPUT_REPORT, |
| 259 | usage_id, |
| 260 | HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i, |
| 261 | &st->gyro[CHANNEL_SCAN_INDEX_X + i]); |
| 262 | if (ret < 0) |
| 263 | break; |
| 264 | gyro_3d_adjust_channel_bit_mask(channels, |
| 265 | CHANNEL_SCAN_INDEX_X + i, |
| 266 | st->gyro[CHANNEL_SCAN_INDEX_X + i].size); |
| 267 | } |
| 268 | dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n", |
| 269 | st->gyro[0].index, |
| 270 | st->gyro[0].report_id, |
| 271 | st->gyro[1].index, st->gyro[1].report_id, |
| 272 | st->gyro[2].index, st->gyro[2].report_id); |
| 273 | |
Srinivas Pandruvada | 6c7db77 | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 274 | st->scale_precision = hid_sensor_format_scale( |
| 275 | HID_USAGE_SENSOR_GYRO_3D, |
| 276 | &st->gyro[CHANNEL_SCAN_INDEX_X], |
| 277 | &st->scale_pre_decml, &st->scale_post_decml); |
| 278 | |
Srinivas Pandruvada | 2371aeb | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 279 | /* Set Sensitivity field ids, when there is no individual modifier */ |
| 280 | if (st->common_attributes.sensitivity.index < 0) { |
| 281 | sensor_hub_input_get_attribute_info(hsdev, |
| 282 | HID_FEATURE_REPORT, usage_id, |
| 283 | HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | |
| 284 | HID_USAGE_SENSOR_DATA_ANGL_VELOCITY, |
| 285 | &st->common_attributes.sensitivity); |
| 286 | dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", |
| 287 | st->common_attributes.sensitivity.index, |
| 288 | st->common_attributes.sensitivity.report_id); |
| 289 | } |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | /* Function to initialize the processing for usage id */ |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 294 | static int hid_gyro_3d_probe(struct platform_device *pdev) |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 295 | { |
| 296 | int ret = 0; |
| 297 | static const char *name = "gyro_3d"; |
| 298 | struct iio_dev *indio_dev; |
| 299 | struct gyro_3d_state *gyro_state; |
| 300 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 301 | |
Sachin Kamat | 1ba15cf | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 302 | indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state)); |
| 303 | if (!indio_dev) |
| 304 | return -ENOMEM; |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 305 | platform_set_drvdata(pdev, indio_dev); |
| 306 | |
| 307 | gyro_state = iio_priv(indio_dev); |
| 308 | gyro_state->common_attributes.hsdev = hsdev; |
| 309 | gyro_state->common_attributes.pdev = pdev; |
| 310 | |
| 311 | ret = hid_sensor_parse_common_attributes(hsdev, |
| 312 | HID_USAGE_SENSOR_GYRO_3D, |
| 313 | &gyro_state->common_attributes); |
| 314 | if (ret) { |
| 315 | dev_err(&pdev->dev, "failed to setup common attributes\n"); |
Sachin Kamat | 1ba15cf | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 316 | return ret; |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Fabio Estevam | d8c9d23 | 2015-05-13 15:06:10 -0300 | [diff] [blame^] | 319 | indio_dev->channels = kmemdup(gyro_3d_channels, |
| 320 | sizeof(gyro_3d_channels), GFP_KERNEL); |
| 321 | if (!indio_dev->channels) { |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 322 | dev_err(&pdev->dev, "failed to duplicate channels\n"); |
Sachin Kamat | 1ba15cf | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 323 | return -ENOMEM; |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Fabio Estevam | d8c9d23 | 2015-05-13 15:06:10 -0300 | [diff] [blame^] | 326 | ret = gyro_3d_parse_report(pdev, hsdev, |
| 327 | (struct iio_chan_spec *)indio_dev->channels, |
| 328 | HID_USAGE_SENSOR_GYRO_3D, gyro_state); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 329 | if (ret) { |
| 330 | dev_err(&pdev->dev, "failed to setup attributes\n"); |
| 331 | goto error_free_dev_mem; |
| 332 | } |
| 333 | |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 334 | indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels); |
| 335 | indio_dev->dev.parent = &pdev->dev; |
| 336 | indio_dev->info = &gyro_3d_info; |
| 337 | indio_dev->name = name; |
| 338 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 339 | |
| 340 | ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, |
| 341 | NULL, NULL); |
| 342 | if (ret) { |
| 343 | dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); |
| 344 | goto error_free_dev_mem; |
| 345 | } |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 346 | atomic_set(&gyro_state->common_attributes.data_ready, 0); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 347 | ret = hid_sensor_setup_trigger(indio_dev, name, |
| 348 | &gyro_state->common_attributes); |
| 349 | if (ret < 0) { |
| 350 | dev_err(&pdev->dev, "trigger setup failed\n"); |
| 351 | goto error_unreg_buffer_funcs; |
| 352 | } |
| 353 | |
| 354 | ret = iio_device_register(indio_dev); |
| 355 | if (ret) { |
| 356 | dev_err(&pdev->dev, "device register failed\n"); |
| 357 | goto error_remove_trigger; |
| 358 | } |
| 359 | |
| 360 | gyro_state->callbacks.send_event = gyro_3d_proc_event; |
| 361 | gyro_state->callbacks.capture_sample = gyro_3d_capture_sample; |
| 362 | gyro_state->callbacks.pdev = pdev; |
| 363 | ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D, |
| 364 | &gyro_state->callbacks); |
| 365 | if (ret < 0) { |
| 366 | dev_err(&pdev->dev, "callback reg failed\n"); |
| 367 | goto error_iio_unreg; |
| 368 | } |
| 369 | |
| 370 | return ret; |
| 371 | |
| 372 | error_iio_unreg: |
| 373 | iio_device_unregister(indio_dev); |
| 374 | error_remove_trigger: |
Srinivas Pandruvada | ec7f68e | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 375 | hid_sensor_remove_trigger(&gyro_state->common_attributes); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 376 | error_unreg_buffer_funcs: |
| 377 | iio_triggered_buffer_cleanup(indio_dev); |
| 378 | error_free_dev_mem: |
| 379 | kfree(indio_dev->channels); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | /* Function to deinitialize the processing for usage id */ |
Greg Kroah-Hartman | fc52692 | 2012-12-21 13:21:43 -0800 | [diff] [blame] | 384 | static int hid_gyro_3d_remove(struct platform_device *pdev) |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 385 | { |
| 386 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; |
| 387 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
Srinivas Pandruvada | ec7f68e | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 388 | struct gyro_3d_state *gyro_state = iio_priv(indio_dev); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 389 | |
| 390 | sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D); |
| 391 | iio_device_unregister(indio_dev); |
Srinivas Pandruvada | ec7f68e | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 392 | hid_sensor_remove_trigger(&gyro_state->common_attributes); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 393 | iio_triggered_buffer_cleanup(indio_dev); |
| 394 | kfree(indio_dev->channels); |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
Krzysztof Kozlowski | 322a3b1 | 2015-05-02 00:53:32 +0900 | [diff] [blame] | 399 | static const struct platform_device_id hid_gyro_3d_ids[] = { |
Alexander Holler | a411e73 | 2013-07-10 09:31:00 +0100 | [diff] [blame] | 400 | { |
| 401 | /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ |
| 402 | .name = "HID-SENSOR-200076", |
| 403 | }, |
| 404 | { /* sentinel */ } |
| 405 | }; |
| 406 | MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids); |
| 407 | |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 408 | static struct platform_driver hid_gyro_3d_platform_driver = { |
Alexander Holler | a411e73 | 2013-07-10 09:31:00 +0100 | [diff] [blame] | 409 | .id_table = hid_gyro_3d_ids, |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 410 | .driver = { |
Alexander Holler | a411e73 | 2013-07-10 09:31:00 +0100 | [diff] [blame] | 411 | .name = KBUILD_MODNAME, |
Srinivas Pandruvada | 27ce423 | 2015-01-07 10:55:55 -0800 | [diff] [blame] | 412 | .pm = &hid_sensor_pm_ops, |
srinivas pandruvada | c5bdbef | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 413 | }, |
| 414 | .probe = hid_gyro_3d_probe, |
| 415 | .remove = hid_gyro_3d_remove, |
| 416 | }; |
| 417 | module_platform_driver(hid_gyro_3d_platform_driver); |
| 418 | |
| 419 | MODULE_DESCRIPTION("HID Sensor Gyroscope 3D"); |
| 420 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); |
| 421 | MODULE_LICENSE("GPL"); |