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