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