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