blob: bbd6426c9726d8f4e0bf36f1e138901c19ff99e8 [file] [log] [blame]
srinivas pandruvada73c67682012-09-05 13:56:00 +01001/*
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/hid-sensor-hub.h>
26#include <linux/iio/iio.h>
27#include <linux/iio/trigger.h>
28#include <linux/iio/sysfs.h>
srinivas pandruvada73c67682012-09-05 13:56:00 +010029#include "hid-sensor-trigger.h"
30
31static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
32 bool state)
33{
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +000034 struct hid_sensor_common *st = iio_trigger_get_drvdata(trig);
srinivas pandruvada73c67682012-09-05 13:56:00 +010035 int state_val;
36
Srinivas Pandruvada7d3c1922013-09-18 18:13:00 +010037 if (state) {
38 if (sensor_hub_device_open(st->hsdev))
39 return -EIO;
40 } else
41 sensor_hub_device_close(st->hsdev);
42
srinivas pandruvada73c67682012-09-05 13:56:00 +010043 state_val = state ? 1 : 0;
Kirill A. Shutemov69bcd3b2012-10-22 08:57:00 +010044 if (IS_ENABLED(CONFIG_HID_SENSOR_ENUM_BASE_QUIRKS))
45 ++state_val;
srinivas pandruvada73c67682012-09-05 13:56:00 +010046 st->data_ready = state;
47 sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
48 st->power_state.index,
49 (s32)state_val);
50
51 sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
52 st->report_state.index,
53 (s32)state_val);
54
55 return 0;
56}
57
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +000058void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
srinivas pandruvada73c67682012-09-05 13:56:00 +010059{
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +000060 iio_trigger_unregister(attrb->trigger);
61 iio_trigger_free(attrb->trigger);
srinivas pandruvada73c67682012-09-05 13:56:00 +010062}
63EXPORT_SYMBOL(hid_sensor_remove_trigger);
64
65static const struct iio_trigger_ops hid_sensor_trigger_ops = {
66 .owner = THIS_MODULE,
67 .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
68};
69
70int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
Alexander Hollere07c6d12012-12-15 12:45:00 +000071 struct hid_sensor_common *attrb)
srinivas pandruvada73c67682012-09-05 13:56:00 +010072{
73 int ret;
74 struct iio_trigger *trig;
75
76 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
77 if (trig == NULL) {
78 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
79 ret = -ENOMEM;
80 goto error_ret;
81 }
82
83 trig->dev.parent = indio_dev->dev.parent;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +000084 iio_trigger_set_drvdata(trig, attrb);
srinivas pandruvada73c67682012-09-05 13:56:00 +010085 trig->ops = &hid_sensor_trigger_ops;
86 ret = iio_trigger_register(trig);
87
88 if (ret) {
89 dev_err(&indio_dev->dev, "Trigger Register Failed\n");
90 goto error_free_trig;
91 }
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +000092 indio_dev->trig = attrb->trigger = trig;
srinivas pandruvada73c67682012-09-05 13:56:00 +010093
94 return ret;
95
96error_free_trig:
97 iio_trigger_free(trig);
98error_ret:
99 return ret;
100}
101EXPORT_SYMBOL(hid_sensor_setup_trigger);
102
103MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
104MODULE_DESCRIPTION("HID Sensor trigger processing");
105MODULE_LICENSE("GPL");