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