Srinivas Pandruvada | fc18ddd | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HID Sensors Driver |
| 3 | * Copyright (c) 2014, 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 | |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/irq.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/hid-sensor-hub.h> |
| 22 | #include <linux/iio/iio.h> |
| 23 | #include <linux/iio/sysfs.h> |
| 24 | #include <linux/iio/buffer.h> |
| 25 | #include <linux/iio/trigger_consumer.h> |
| 26 | #include <linux/iio/triggered_buffer.h> |
| 27 | #include "../common/hid-sensors/hid-sensor-trigger.h" |
| 28 | |
| 29 | struct dev_rot_state { |
| 30 | struct hid_sensor_hub_callbacks callbacks; |
| 31 | struct hid_sensor_common common_attributes; |
| 32 | struct hid_sensor_hub_attribute_info quaternion; |
| 33 | u32 sampled_vals[4]; |
| 34 | }; |
| 35 | |
| 36 | /* Channel definitions */ |
| 37 | static const struct iio_chan_spec dev_rot_channels[] = { |
| 38 | { |
| 39 | .type = IIO_ROT, |
| 40 | .modified = 1, |
| 41 | .channel2 = IIO_MOD_QUATERNION, |
| 42 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), |
| 43 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | |
| 44 | BIT(IIO_CHAN_INFO_HYSTERESIS) |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | /* Adjust channel real bits based on report descriptor */ |
| 49 | static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan, |
| 50 | int size) |
| 51 | { |
| 52 | chan->scan_type.sign = 's'; |
| 53 | /* Real storage bits will change based on the report desc. */ |
| 54 | chan->scan_type.realbits = size * 8; |
| 55 | /* Maximum size of a sample to capture is u32 */ |
| 56 | chan->scan_type.storagebits = sizeof(u32) * 8; |
| 57 | chan->scan_type.repeat = 4; |
| 58 | } |
| 59 | |
| 60 | /* Channel read_raw handler */ |
| 61 | static int dev_rot_read_raw(struct iio_dev *indio_dev, |
| 62 | struct iio_chan_spec const *chan, |
| 63 | int size, int *vals, int *val_len, |
| 64 | long mask) |
| 65 | { |
| 66 | struct dev_rot_state *rot_state = iio_priv(indio_dev); |
| 67 | int ret_type; |
| 68 | int i; |
| 69 | |
| 70 | vals[0] = 0; |
| 71 | vals[1] = 0; |
| 72 | |
| 73 | switch (mask) { |
| 74 | case IIO_CHAN_INFO_RAW: |
| 75 | if (size >= 4) { |
| 76 | for (i = 0; i < 4; ++i) |
| 77 | vals[i] = rot_state->sampled_vals[i]; |
| 78 | ret_type = IIO_VAL_INT_MULTIPLE; |
| 79 | *val_len = 4; |
| 80 | } else |
| 81 | ret_type = -EINVAL; |
| 82 | break; |
| 83 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 84 | ret_type = hid_sensor_read_samp_freq_value( |
| 85 | &rot_state->common_attributes, &vals[0], &vals[1]); |
| 86 | break; |
| 87 | case IIO_CHAN_INFO_HYSTERESIS: |
| 88 | ret_type = hid_sensor_read_raw_hyst_value( |
| 89 | &rot_state->common_attributes, &vals[0], &vals[1]); |
| 90 | break; |
| 91 | default: |
| 92 | ret_type = -EINVAL; |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | return ret_type; |
| 97 | } |
| 98 | |
| 99 | /* Channel write_raw handler */ |
| 100 | static int dev_rot_write_raw(struct iio_dev *indio_dev, |
| 101 | struct iio_chan_spec const *chan, |
| 102 | int val, |
| 103 | int val2, |
| 104 | long mask) |
| 105 | { |
| 106 | struct dev_rot_state *rot_state = iio_priv(indio_dev); |
| 107 | int ret; |
| 108 | |
| 109 | switch (mask) { |
| 110 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 111 | ret = hid_sensor_write_samp_freq_value( |
| 112 | &rot_state->common_attributes, val, val2); |
| 113 | break; |
| 114 | case IIO_CHAN_INFO_HYSTERESIS: |
| 115 | ret = hid_sensor_write_raw_hyst_value( |
| 116 | &rot_state->common_attributes, val, val2); |
| 117 | break; |
| 118 | default: |
| 119 | ret = -EINVAL; |
| 120 | } |
| 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static const struct iio_info dev_rot_info = { |
| 126 | .driver_module = THIS_MODULE, |
| 127 | .read_raw_multi = &dev_rot_read_raw, |
| 128 | .write_raw = &dev_rot_write_raw, |
| 129 | }; |
| 130 | |
| 131 | /* Function to push data to buffer */ |
| 132 | static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len) |
| 133 | { |
| 134 | dev_dbg(&indio_dev->dev, "hid_sensor_push_data >>\n"); |
| 135 | iio_push_to_buffers(indio_dev, (u8 *)data); |
| 136 | dev_dbg(&indio_dev->dev, "hid_sensor_push_data <<\n"); |
| 137 | |
| 138 | } |
| 139 | |
| 140 | /* Callback handler to send event after all samples are received and captured */ |
| 141 | static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev, |
| 142 | unsigned usage_id, |
| 143 | void *priv) |
| 144 | { |
| 145 | struct iio_dev *indio_dev = platform_get_drvdata(priv); |
| 146 | struct dev_rot_state *rot_state = iio_priv(indio_dev); |
| 147 | |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 148 | dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n"); |
| 149 | if (atomic_read(&rot_state->common_attributes.data_ready)) |
Srinivas Pandruvada | fc18ddd | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 150 | hid_sensor_push_data(indio_dev, |
| 151 | (u8 *)rot_state->sampled_vals, |
| 152 | sizeof(rot_state->sampled_vals)); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | /* Capture samples in local storage */ |
| 158 | static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev, |
| 159 | unsigned usage_id, |
| 160 | size_t raw_len, char *raw_data, |
| 161 | void *priv) |
| 162 | { |
| 163 | struct iio_dev *indio_dev = platform_get_drvdata(priv); |
| 164 | struct dev_rot_state *rot_state = iio_priv(indio_dev); |
| 165 | |
| 166 | if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) { |
| 167 | memcpy(rot_state->sampled_vals, raw_data, |
| 168 | sizeof(rot_state->sampled_vals)); |
| 169 | dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len, |
| 170 | sizeof(rot_state->sampled_vals)); |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* Parse report which is specific to an usage id*/ |
| 177 | static int dev_rot_parse_report(struct platform_device *pdev, |
| 178 | struct hid_sensor_hub_device *hsdev, |
| 179 | struct iio_chan_spec *channels, |
| 180 | unsigned usage_id, |
| 181 | struct dev_rot_state *st) |
| 182 | { |
| 183 | int ret; |
| 184 | |
| 185 | ret = sensor_hub_input_get_attribute_info(hsdev, |
| 186 | HID_INPUT_REPORT, |
| 187 | usage_id, |
| 188 | HID_USAGE_SENSOR_ORIENT_QUATERNION, |
| 189 | &st->quaternion); |
| 190 | if (ret) |
| 191 | return ret; |
| 192 | |
| 193 | dev_rot_adjust_channel_bit_mask(&channels[0], |
| 194 | st->quaternion.size / 4); |
| 195 | |
| 196 | dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index, |
| 197 | st->quaternion.report_id); |
| 198 | |
| 199 | dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n", |
| 200 | st->quaternion.size); |
| 201 | |
| 202 | /* Set Sensitivity field ids, when there is no individual modifier */ |
| 203 | if (st->common_attributes.sensitivity.index < 0) { |
| 204 | sensor_hub_input_get_attribute_info(hsdev, |
| 205 | HID_FEATURE_REPORT, usage_id, |
| 206 | HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | |
| 207 | HID_USAGE_SENSOR_DATA_ORIENTATION, |
| 208 | &st->common_attributes.sensitivity); |
| 209 | dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", |
| 210 | st->common_attributes.sensitivity.index, |
| 211 | st->common_attributes.sensitivity.report_id); |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | /* Function to initialize the processing for usage id */ |
| 218 | static int hid_dev_rot_probe(struct platform_device *pdev) |
| 219 | { |
| 220 | int ret; |
| 221 | static char *name = "dev_rotation"; |
| 222 | struct iio_dev *indio_dev; |
| 223 | struct dev_rot_state *rot_state; |
| 224 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; |
Srinivas Pandruvada | fc18ddd | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 225 | |
| 226 | indio_dev = devm_iio_device_alloc(&pdev->dev, |
| 227 | sizeof(struct dev_rot_state)); |
| 228 | if (indio_dev == NULL) |
| 229 | return -ENOMEM; |
| 230 | |
| 231 | platform_set_drvdata(pdev, indio_dev); |
| 232 | |
| 233 | rot_state = iio_priv(indio_dev); |
| 234 | rot_state->common_attributes.hsdev = hsdev; |
| 235 | rot_state->common_attributes.pdev = pdev; |
| 236 | |
| 237 | ret = hid_sensor_parse_common_attributes(hsdev, |
| 238 | HID_USAGE_SENSOR_DEVICE_ORIENTATION, |
| 239 | &rot_state->common_attributes); |
| 240 | if (ret) { |
| 241 | dev_err(&pdev->dev, "failed to setup common attributes\n"); |
| 242 | return ret; |
| 243 | } |
| 244 | |
Fabio Estevam | 2bd0462 | 2015-05-13 15:06:13 -0300 | [diff] [blame] | 245 | indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels, |
| 246 | sizeof(dev_rot_channels), |
| 247 | GFP_KERNEL); |
| 248 | if (!indio_dev->channels) { |
Srinivas Pandruvada | fc18ddd | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 249 | dev_err(&pdev->dev, "failed to duplicate channels\n"); |
| 250 | return -ENOMEM; |
| 251 | } |
| 252 | |
Fabio Estevam | 2bd0462 | 2015-05-13 15:06:13 -0300 | [diff] [blame] | 253 | ret = dev_rot_parse_report(pdev, hsdev, |
| 254 | (struct iio_chan_spec *)indio_dev->channels, |
| 255 | HID_USAGE_SENSOR_DEVICE_ORIENTATION, |
| 256 | rot_state); |
Srinivas Pandruvada | fc18ddd | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 257 | if (ret) { |
| 258 | dev_err(&pdev->dev, "failed to setup attributes\n"); |
| 259 | return ret; |
| 260 | } |
| 261 | |
Srinivas Pandruvada | fc18ddd | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 262 | indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels); |
| 263 | indio_dev->dev.parent = &pdev->dev; |
| 264 | indio_dev->info = &dev_rot_info; |
| 265 | indio_dev->name = name; |
| 266 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 267 | |
| 268 | ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, |
| 269 | NULL, NULL); |
| 270 | if (ret) { |
| 271 | dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); |
| 272 | return ret; |
| 273 | } |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 274 | atomic_set(&rot_state->common_attributes.data_ready, 0); |
Srinivas Pandruvada | fc18ddd | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 275 | ret = hid_sensor_setup_trigger(indio_dev, name, |
| 276 | &rot_state->common_attributes); |
| 277 | if (ret) { |
| 278 | dev_err(&pdev->dev, "trigger setup failed\n"); |
| 279 | goto error_unreg_buffer_funcs; |
| 280 | } |
| 281 | |
| 282 | ret = iio_device_register(indio_dev); |
| 283 | if (ret) { |
| 284 | dev_err(&pdev->dev, "device register failed\n"); |
| 285 | goto error_remove_trigger; |
| 286 | } |
| 287 | |
| 288 | rot_state->callbacks.send_event = dev_rot_proc_event; |
| 289 | rot_state->callbacks.capture_sample = dev_rot_capture_sample; |
| 290 | rot_state->callbacks.pdev = pdev; |
| 291 | ret = sensor_hub_register_callback(hsdev, |
| 292 | HID_USAGE_SENSOR_DEVICE_ORIENTATION, |
| 293 | &rot_state->callbacks); |
| 294 | if (ret) { |
| 295 | dev_err(&pdev->dev, "callback reg failed\n"); |
| 296 | goto error_iio_unreg; |
| 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | |
| 301 | error_iio_unreg: |
| 302 | iio_device_unregister(indio_dev); |
| 303 | error_remove_trigger: |
| 304 | hid_sensor_remove_trigger(&rot_state->common_attributes); |
| 305 | error_unreg_buffer_funcs: |
| 306 | iio_triggered_buffer_cleanup(indio_dev); |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | /* Function to deinitialize the processing for usage id */ |
| 311 | static int hid_dev_rot_remove(struct platform_device *pdev) |
| 312 | { |
| 313 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; |
| 314 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); |
| 315 | struct dev_rot_state *rot_state = iio_priv(indio_dev); |
| 316 | |
| 317 | sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_DEVICE_ORIENTATION); |
| 318 | iio_device_unregister(indio_dev); |
| 319 | hid_sensor_remove_trigger(&rot_state->common_attributes); |
| 320 | iio_triggered_buffer_cleanup(indio_dev); |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Krzysztof Kozlowski | 6b490c6 | 2015-05-02 00:53:37 +0900 | [diff] [blame] | 325 | static const struct platform_device_id hid_dev_rot_ids[] = { |
Srinivas Pandruvada | fc18ddd | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 326 | { |
| 327 | /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ |
| 328 | .name = "HID-SENSOR-20008a", |
| 329 | }, |
| 330 | { /* sentinel */ } |
| 331 | }; |
| 332 | MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids); |
| 333 | |
| 334 | static struct platform_driver hid_dev_rot_platform_driver = { |
| 335 | .id_table = hid_dev_rot_ids, |
| 336 | .driver = { |
| 337 | .name = KBUILD_MODNAME, |
Song Hongyan | 8af644a | 2016-10-25 01:06:03 +0000 | [diff] [blame] | 338 | .pm = &hid_sensor_pm_ops, |
Srinivas Pandruvada | fc18ddd | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 339 | }, |
| 340 | .probe = hid_dev_rot_probe, |
| 341 | .remove = hid_dev_rot_remove, |
| 342 | }; |
| 343 | module_platform_driver(hid_dev_rot_platform_driver); |
| 344 | |
| 345 | MODULE_DESCRIPTION("HID Sensor Device Rotation"); |
| 346 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); |
| 347 | MODULE_LICENSE("GPL"); |