linux/drivers/iio/pressure/hid-sensor-press.c
<<
>>
Prefs
   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 * You should have received a copy of the GNU General Public License along with
  15 * this program.
  16 *
  17 */
  18#include <linux/device.h>
  19#include <linux/platform_device.h>
  20#include <linux/module.h>
  21#include <linux/interrupt.h>
  22#include <linux/irq.h>
  23#include <linux/slab.h>
  24#include <linux/delay.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#define CHANNEL_SCAN_INDEX_PRESSURE 0
  34
  35struct press_state {
  36        struct hid_sensor_hub_callbacks callbacks;
  37        struct hid_sensor_common common_attributes;
  38        struct hid_sensor_hub_attribute_info press_attr;
  39        u32 press_data;
  40        int scale_pre_decml;
  41        int scale_post_decml;
  42        int scale_precision;
  43        int value_offset;
  44};
  45
  46/* Channel definitions */
  47static const struct iio_chan_spec press_channels[] = {
  48        {
  49                .type = IIO_PRESSURE,
  50                .modified = 1,
  51                .channel2 = IIO_NO_MOD,
  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_PRESSURE,
  58        }
  59};
  60
  61/* Adjust channel real bits based on report descriptor */
  62static void press_adjust_channel_bit_mask(struct iio_chan_spec *channels,
  63                                        int channel, int size)
  64{
  65        channels[channel].scan_type.sign = 's';
  66        /* Real storage bits will change based on the report desc. */
  67        channels[channel].scan_type.realbits = size * 8;
  68        /* Maximum size of a sample to capture is u32 */
  69        channels[channel].scan_type.storagebits = sizeof(u32) * 8;
  70}
  71
  72/* Channel read_raw handler */
  73static int press_read_raw(struct iio_dev *indio_dev,
  74                              struct iio_chan_spec const *chan,
  75                              int *val, int *val2,
  76                              long mask)
  77{
  78        struct press_state *press_state = iio_priv(indio_dev);
  79        int report_id = -1;
  80        u32 address;
  81        int ret_type;
  82        s32 poll_value;
  83
  84        *val = 0;
  85        *val2 = 0;
  86        switch (mask) {
  87        case IIO_CHAN_INFO_RAW:
  88                switch (chan->scan_index) {
  89                case  CHANNEL_SCAN_INDEX_PRESSURE:
  90                        report_id = press_state->press_attr.report_id;
  91                        address =
  92                        HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE;
  93                        break;
  94                default:
  95                        report_id = -1;
  96                        break;
  97                }
  98                if (report_id >= 0) {
  99                        poll_value = hid_sensor_read_poll_value(
 100                                        &press_state->common_attributes);
 101                        if (poll_value < 0)
 102                                return -EINVAL;
 103                        hid_sensor_power_state(&press_state->common_attributes,
 104                                                true);
 105
 106                        msleep_interruptible(poll_value * 2);
 107
 108                        *val = sensor_hub_input_attr_get_raw_value(
 109                                press_state->common_attributes.hsdev,
 110                                HID_USAGE_SENSOR_PRESSURE, address,
 111                                report_id);
 112                        hid_sensor_power_state(&press_state->common_attributes,
 113                                                false);
 114                } else {
 115                        *val = 0;
 116                        return -EINVAL;
 117                }
 118                ret_type = IIO_VAL_INT;
 119                break;
 120        case IIO_CHAN_INFO_SCALE:
 121                *val = press_state->scale_pre_decml;
 122                *val2 = press_state->scale_post_decml;
 123                ret_type = press_state->scale_precision;
 124                break;
 125        case IIO_CHAN_INFO_OFFSET:
 126                *val = press_state->value_offset;
 127                ret_type = IIO_VAL_INT;
 128                break;
 129        case IIO_CHAN_INFO_SAMP_FREQ:
 130                ret_type = hid_sensor_read_samp_freq_value(
 131                                &press_state->common_attributes, val, val2);
 132                break;
 133        case IIO_CHAN_INFO_HYSTERESIS:
 134                ret_type = hid_sensor_read_raw_hyst_value(
 135                                &press_state->common_attributes, val, val2);
 136                break;
 137        default:
 138                ret_type = -EINVAL;
 139                break;
 140        }
 141
 142        return ret_type;
 143}
 144
 145/* Channel write_raw handler */
 146static int press_write_raw(struct iio_dev *indio_dev,
 147                               struct iio_chan_spec const *chan,
 148                               int val,
 149                               int val2,
 150                               long mask)
 151{
 152        struct press_state *press_state = iio_priv(indio_dev);
 153        int ret = 0;
 154
 155        switch (mask) {
 156        case IIO_CHAN_INFO_SAMP_FREQ:
 157                ret = hid_sensor_write_samp_freq_value(
 158                                &press_state->common_attributes, val, val2);
 159                break;
 160        case IIO_CHAN_INFO_HYSTERESIS:
 161                ret = hid_sensor_write_raw_hyst_value(
 162                                &press_state->common_attributes, val, val2);
 163                break;
 164        default:
 165                ret = -EINVAL;
 166        }
 167
 168        return ret;
 169}
 170
 171static const struct iio_info press_info = {
 172        .driver_module = THIS_MODULE,
 173        .read_raw = &press_read_raw,
 174        .write_raw = &press_write_raw,
 175};
 176
 177/* Function to push data to buffer */
 178static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
 179                                        int len)
 180{
 181        dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
 182        iio_push_to_buffers(indio_dev, data);
 183}
 184
 185/* Callback handler to send event after all samples are received and captured */
 186static int press_proc_event(struct hid_sensor_hub_device *hsdev,
 187                                unsigned usage_id,
 188                                void *priv)
 189{
 190        struct iio_dev *indio_dev = platform_get_drvdata(priv);
 191        struct press_state *press_state = iio_priv(indio_dev);
 192
 193        dev_dbg(&indio_dev->dev, "press_proc_event\n");
 194        if (atomic_read(&press_state->common_attributes.data_ready))
 195                hid_sensor_push_data(indio_dev,
 196                                &press_state->press_data,
 197                                sizeof(press_state->press_data));
 198
 199        return 0;
 200}
 201
 202/* Capture samples in local storage */
 203static int press_capture_sample(struct hid_sensor_hub_device *hsdev,
 204                                unsigned usage_id,
 205                                size_t raw_len, char *raw_data,
 206                                void *priv)
 207{
 208        struct iio_dev *indio_dev = platform_get_drvdata(priv);
 209        struct press_state *press_state = iio_priv(indio_dev);
 210        int ret = -EINVAL;
 211
 212        switch (usage_id) {
 213        case HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE:
 214                press_state->press_data = *(u32 *)raw_data;
 215                ret = 0;
 216                break;
 217        default:
 218                break;
 219        }
 220
 221        return ret;
 222}
 223
 224/* Parse report which is specific to an usage id*/
 225static int press_parse_report(struct platform_device *pdev,
 226                                struct hid_sensor_hub_device *hsdev,
 227                                struct iio_chan_spec *channels,
 228                                unsigned usage_id,
 229                                struct press_state *st)
 230{
 231        int ret;
 232
 233        ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
 234                        usage_id,
 235                        HID_USAGE_SENSOR_ATMOSPHERIC_PRESSURE,
 236                        &st->press_attr);
 237        if (ret < 0)
 238                return ret;
 239        press_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_PRESSURE,
 240                                        st->press_attr.size);
 241
 242        dev_dbg(&pdev->dev, "press %x:%x\n", st->press_attr.index,
 243                        st->press_attr.report_id);
 244
 245        st->scale_precision = hid_sensor_format_scale(
 246                                HID_USAGE_SENSOR_PRESSURE,
 247                                &st->press_attr,
 248                                &st->scale_pre_decml, &st->scale_post_decml);
 249
 250        /* Set Sensitivity field ids, when there is no individual modifier */
 251        if (st->common_attributes.sensitivity.index < 0) {
 252                sensor_hub_input_get_attribute_info(hsdev,
 253                        HID_FEATURE_REPORT, usage_id,
 254                        HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
 255                        HID_USAGE_SENSOR_DATA_ATMOSPHERIC_PRESSURE,
 256                        &st->common_attributes.sensitivity);
 257                dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
 258                        st->common_attributes.sensitivity.index,
 259                        st->common_attributes.sensitivity.report_id);
 260        }
 261        return ret;
 262}
 263
 264/* Function to initialize the processing for usage id */
 265static int hid_press_probe(struct platform_device *pdev)
 266{
 267        int ret = 0;
 268        static const char *name = "press";
 269        struct iio_dev *indio_dev;
 270        struct press_state *press_state;
 271        struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
 272        struct iio_chan_spec *channels;
 273
 274        indio_dev = devm_iio_device_alloc(&pdev->dev,
 275                                sizeof(struct press_state));
 276        if (!indio_dev)
 277                return -ENOMEM;
 278        platform_set_drvdata(pdev, indio_dev);
 279
 280        press_state = iio_priv(indio_dev);
 281        press_state->common_attributes.hsdev = hsdev;
 282        press_state->common_attributes.pdev = pdev;
 283
 284        ret = hid_sensor_parse_common_attributes(hsdev,
 285                                        HID_USAGE_SENSOR_PRESSURE,
 286                                        &press_state->common_attributes);
 287        if (ret) {
 288                dev_err(&pdev->dev, "failed to setup common attributes\n");
 289                return ret;
 290        }
 291
 292        channels = kmemdup(press_channels, sizeof(press_channels), GFP_KERNEL);
 293        if (!channels) {
 294                dev_err(&pdev->dev, "failed to duplicate channels\n");
 295                return -ENOMEM;
 296        }
 297
 298        ret = press_parse_report(pdev, hsdev, channels,
 299                                HID_USAGE_SENSOR_PRESSURE, press_state);
 300        if (ret) {
 301                dev_err(&pdev->dev, "failed to setup attributes\n");
 302                goto error_free_dev_mem;
 303        }
 304
 305        indio_dev->channels = channels;
 306        indio_dev->num_channels =
 307                                ARRAY_SIZE(press_channels);
 308        indio_dev->dev.parent = &pdev->dev;
 309        indio_dev->info = &press_info;
 310        indio_dev->name = name;
 311        indio_dev->modes = INDIO_DIRECT_MODE;
 312
 313        ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
 314                NULL, NULL);
 315        if (ret) {
 316                dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
 317                goto error_free_dev_mem;
 318        }
 319        atomic_set(&press_state->common_attributes.data_ready, 0);
 320        ret = hid_sensor_setup_trigger(indio_dev, name,
 321                                &press_state->common_attributes);
 322        if (ret) {
 323                dev_err(&pdev->dev, "trigger setup failed\n");
 324                goto error_unreg_buffer_funcs;
 325        }
 326
 327        ret = iio_device_register(indio_dev);
 328        if (ret) {
 329                dev_err(&pdev->dev, "device register failed\n");
 330                goto error_remove_trigger;
 331        }
 332
 333        press_state->callbacks.send_event = press_proc_event;
 334        press_state->callbacks.capture_sample = press_capture_sample;
 335        press_state->callbacks.pdev = pdev;
 336        ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_PRESSURE,
 337                                        &press_state->callbacks);
 338        if (ret < 0) {
 339                dev_err(&pdev->dev, "callback reg failed\n");
 340                goto error_iio_unreg;
 341        }
 342
 343        return ret;
 344
 345error_iio_unreg:
 346        iio_device_unregister(indio_dev);
 347error_remove_trigger:
 348        hid_sensor_remove_trigger(&press_state->common_attributes);
 349error_unreg_buffer_funcs:
 350        iio_triggered_buffer_cleanup(indio_dev);
 351error_free_dev_mem:
 352        kfree(indio_dev->channels);
 353        return ret;
 354}
 355
 356/* Function to deinitialize the processing for usage id */
 357static int hid_press_remove(struct platform_device *pdev)
 358{
 359        struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
 360        struct iio_dev *indio_dev = platform_get_drvdata(pdev);
 361        struct press_state *press_state = iio_priv(indio_dev);
 362
 363        sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PRESSURE);
 364        iio_device_unregister(indio_dev);
 365        hid_sensor_remove_trigger(&press_state->common_attributes);
 366        iio_triggered_buffer_cleanup(indio_dev);
 367        kfree(indio_dev->channels);
 368
 369        return 0;
 370}
 371
 372static struct platform_device_id hid_press_ids[] = {
 373        {
 374                /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
 375                .name = "HID-SENSOR-200031",
 376        },
 377        { /* sentinel */ }
 378};
 379MODULE_DEVICE_TABLE(platform, hid_press_ids);
 380
 381static struct platform_driver hid_press_platform_driver = {
 382        .id_table = hid_press_ids,
 383        .driver = {
 384                .name   = KBUILD_MODNAME,
 385        },
 386        .probe          = hid_press_probe,
 387        .remove         = hid_press_remove,
 388};
 389module_platform_driver(hid_press_platform_driver);
 390
 391MODULE_DESCRIPTION("HID Sensor Pressure");
 392MODULE_AUTHOR("Archana Patni <archana.patni@intel.com>");
 393MODULE_LICENSE("GPL");
 394
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.