linux/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
<<
>>
Prefs
   1/*
   2 * HID Sensors Driver
   3 * Copyright (c) 2012, 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 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  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/trigger.h>
  29#include <linux/iio/buffer.h>
  30#include <linux/iio/sysfs.h>
  31#include "hid-sensor-trigger.h"
  32
  33static ssize_t _hid_sensor_set_report_latency(struct device *dev,
  34                                              struct device_attribute *attr,
  35                                              const char *buf, size_t len)
  36{
  37        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  38        struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  39        int integer, fract, ret;
  40        int latency;
  41
  42        ret = iio_str_to_fixpoint(buf, 100000, &integer, &fract);
  43        if (ret)
  44                return ret;
  45
  46        latency = integer * 1000 + fract / 1000;
  47        ret = hid_sensor_set_report_latency(attrb, latency);
  48        if (ret < 0)
  49                return len;
  50
  51        attrb->latency_ms = hid_sensor_get_report_latency(attrb);
  52
  53        return len;
  54}
  55
  56static ssize_t _hid_sensor_get_report_latency(struct device *dev,
  57                                              struct device_attribute *attr,
  58                                              char *buf)
  59{
  60        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  61        struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  62        int latency;
  63
  64        latency = hid_sensor_get_report_latency(attrb);
  65        if (latency < 0)
  66                return latency;
  67
  68        return sprintf(buf, "%d.%06u\n", latency / 1000, (latency % 1000) * 1000);
  69}
  70
  71static ssize_t _hid_sensor_get_fifo_state(struct device *dev,
  72                                          struct device_attribute *attr,
  73                                          char *buf)
  74{
  75        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  76        struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
  77        int latency;
  78
  79        latency = hid_sensor_get_report_latency(attrb);
  80        if (latency < 0)
  81                return latency;
  82
  83        return sprintf(buf, "%d\n", !!latency);
  84}
  85
  86static IIO_DEVICE_ATTR(hwfifo_timeout, 0644,
  87                       _hid_sensor_get_report_latency,
  88                       _hid_sensor_set_report_latency, 0);
  89static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
  90                       _hid_sensor_get_fifo_state, NULL, 0);
  91
  92static const struct attribute *hid_sensor_fifo_attributes[] = {
  93        &iio_dev_attr_hwfifo_timeout.dev_attr.attr,
  94        &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
  95        NULL,
  96};
  97
  98static void hid_sensor_setup_batch_mode(struct iio_dev *indio_dev,
  99                                        struct hid_sensor_common *st)
 100{
 101        if (!hid_sensor_batch_mode_supported(st))
 102                return;
 103
 104        iio_buffer_set_attrs(indio_dev->buffer, hid_sensor_fifo_attributes);
 105}
 106
 107static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state)
 108{
 109        int state_val;
 110        int report_val;
 111        s32 poll_value = 0;
 112
 113        if (state) {
 114                if (sensor_hub_device_open(st->hsdev))
 115                        return -EIO;
 116
 117                atomic_inc(&st->data_ready);
 118
 119                state_val = hid_sensor_get_usage_index(st->hsdev,
 120                        st->power_state.report_id,
 121                        st->power_state.index,
 122                        HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM);
 123                report_val = hid_sensor_get_usage_index(st->hsdev,
 124                        st->report_state.report_id,
 125                        st->report_state.index,
 126                        HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
 127
 128                poll_value = hid_sensor_read_poll_value(st);
 129        } else {
 130                int val;
 131
 132                val = atomic_dec_if_positive(&st->data_ready);
 133                if (val < 0)
 134                        return 0;
 135
 136                sensor_hub_device_close(st->hsdev);
 137                state_val = hid_sensor_get_usage_index(st->hsdev,
 138                        st->power_state.report_id,
 139                        st->power_state.index,
 140                        HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
 141                report_val = hid_sensor_get_usage_index(st->hsdev,
 142                        st->report_state.report_id,
 143                        st->report_state.index,
 144                        HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
 145        }
 146
 147        if (state_val >= 0) {
 148                state_val += st->power_state.logical_minimum;
 149                sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
 150                                       st->power_state.index, sizeof(state_val),
 151                                       &state_val);
 152        }
 153
 154        if (report_val >= 0) {
 155                report_val += st->report_state.logical_minimum;
 156                sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
 157                                       st->report_state.index,
 158                                       sizeof(report_val),
 159                                       &report_val);
 160        }
 161
 162        pr_debug("HID_SENSOR %s set power_state %d report_state %d\n",
 163                 st->pdev->name, state_val, report_val);
 164
 165        sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
 166                               st->power_state.index,
 167                               sizeof(state_val), &state_val);
 168        if (state && poll_value)
 169                msleep_interruptible(poll_value * 2);
 170
 171        return 0;
 172}
 173EXPORT_SYMBOL(hid_sensor_power_state);
 174
 175int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
 176{
 177
 178#ifdef CONFIG_PM
 179        int ret;
 180
 181        if (atomic_add_unless(&st->runtime_pm_enable, 1, 1))
 182                pm_runtime_enable(&st->pdev->dev);
 183
 184        if (state) {
 185                atomic_inc(&st->user_requested_state);
 186                ret = pm_runtime_get_sync(&st->pdev->dev);
 187        } else {
 188                atomic_dec(&st->user_requested_state);
 189                pm_runtime_mark_last_busy(&st->pdev->dev);
 190                pm_runtime_use_autosuspend(&st->pdev->dev);
 191                ret = pm_runtime_put_autosuspend(&st->pdev->dev);
 192        }
 193        if (ret < 0) {
 194                if (state)
 195                        pm_runtime_put_noidle(&st->pdev->dev);
 196                return ret;
 197        }
 198
 199        return 0;
 200#else
 201        atomic_set(&st->user_requested_state, state);
 202        return _hid_sensor_power_state(st, state);
 203#endif
 204}
 205
 206static void hid_sensor_set_power_work(struct work_struct *work)
 207{
 208        struct hid_sensor_common *attrb = container_of(work,
 209                                                       struct hid_sensor_common,
 210                                                       work);
 211
 212        if (attrb->poll_interval >= 0)
 213                sensor_hub_set_feature(attrb->hsdev, attrb->poll.report_id,
 214                                       attrb->poll.index,
 215                                       sizeof(attrb->poll_interval),
 216                                       &attrb->poll_interval);
 217
 218        if (attrb->raw_hystersis >= 0)
 219                sensor_hub_set_feature(attrb->hsdev,
 220                                       attrb->sensitivity.report_id,
 221                                       attrb->sensitivity.index,
 222                                       sizeof(attrb->raw_hystersis),
 223                                       &attrb->raw_hystersis);
 224
 225        if (attrb->latency_ms > 0)
 226                hid_sensor_set_report_latency(attrb, attrb->latency_ms);
 227
 228        if (atomic_read(&attrb->user_requested_state))
 229                _hid_sensor_power_state(attrb, true);
 230}
 231
 232static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
 233                                                bool state)
 234{
 235        return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
 236}
 237
 238void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
 239{
 240        if (atomic_read(&attrb->runtime_pm_enable))
 241                pm_runtime_disable(&attrb->pdev->dev);
 242
 243        pm_runtime_set_suspended(&attrb->pdev->dev);
 244        pm_runtime_put_noidle(&attrb->pdev->dev);
 245
 246        cancel_work_sync(&attrb->work);
 247        iio_trigger_unregister(attrb->trigger);
 248        iio_trigger_free(attrb->trigger);
 249}
 250EXPORT_SYMBOL(hid_sensor_remove_trigger);
 251
 252static const struct iio_trigger_ops hid_sensor_trigger_ops = {
 253        .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
 254};
 255
 256int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
 257                                struct hid_sensor_common *attrb)
 258{
 259        int ret;
 260        struct iio_trigger *trig;
 261
 262        trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
 263        if (trig == NULL) {
 264                dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
 265                ret = -ENOMEM;
 266                goto error_ret;
 267        }
 268
 269        trig->dev.parent = indio_dev->dev.parent;
 270        iio_trigger_set_drvdata(trig, attrb);
 271        trig->ops = &hid_sensor_trigger_ops;
 272        ret = iio_trigger_register(trig);
 273
 274        if (ret) {
 275                dev_err(&indio_dev->dev, "Trigger Register Failed\n");
 276                goto error_free_trig;
 277        }
 278        attrb->trigger = trig;
 279        indio_dev->trig = iio_trigger_get(trig);
 280
 281        hid_sensor_setup_batch_mode(indio_dev, attrb);
 282
 283        ret = pm_runtime_set_active(&indio_dev->dev);
 284        if (ret)
 285                goto error_unreg_trigger;
 286
 287        iio_device_set_drvdata(indio_dev, attrb);
 288
 289        INIT_WORK(&attrb->work, hid_sensor_set_power_work);
 290
 291        pm_suspend_ignore_children(&attrb->pdev->dev, true);
 292        /* Default to 3 seconds, but can be changed from sysfs */
 293        pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
 294                                         3000);
 295        return ret;
 296error_unreg_trigger:
 297        iio_trigger_unregister(trig);
 298error_free_trig:
 299        iio_trigger_free(trig);
 300error_ret:
 301        return ret;
 302}
 303EXPORT_SYMBOL(hid_sensor_setup_trigger);
 304
 305static int __maybe_unused hid_sensor_suspend(struct device *dev)
 306{
 307        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 308        struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
 309
 310        return _hid_sensor_power_state(attrb, false);
 311}
 312
 313static int __maybe_unused hid_sensor_resume(struct device *dev)
 314{
 315        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 316        struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
 317        schedule_work(&attrb->work);
 318        return 0;
 319}
 320
 321static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
 322{
 323        struct iio_dev *indio_dev = dev_get_drvdata(dev);
 324        struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
 325        return _hid_sensor_power_state(attrb, true);
 326}
 327
 328const struct dev_pm_ops hid_sensor_pm_ops = {
 329        SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
 330        SET_RUNTIME_PM_OPS(hid_sensor_suspend,
 331                           hid_sensor_runtime_resume, NULL)
 332};
 333EXPORT_SYMBOL(hid_sensor_pm_ops);
 334
 335MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
 336MODULE_DESCRIPTION("HID Sensor trigger processing");
 337MODULE_LICENSE("GPL");
 338