]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/iio/common/hid-sensors/hid-sensor-trigger.c
Merge remote-tracking branches 'spi/topic/devprop', 'spi/topic/fsl', 'spi/topic/fsl...
[mirror_ubuntu-bionic-kernel.git] / drivers / iio / common / hid-sensors / hid-sensor-trigger.c
CommitLineData
73c6768b 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>
2b89635e 25#include <linux/delay.h>
73c6768b 26#include <linux/hid-sensor-hub.h>
27#include <linux/iio/iio.h>
28#include <linux/iio/trigger.h>
29#include <linux/iio/sysfs.h>
73c6768b 30#include "hid-sensor-trigger.h"
31
2b89635e 32static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state)
73c6768b 33{
73c6768b 34 int state_val;
751d17e2 35 int report_val;
2b89635e 36 s32 poll_value = 0;
73c6768b 37
7d3c192d 38 if (state) {
1e25aa96
SP
39 if (!atomic_read(&st->user_requested_state))
40 return 0;
7d3c192d
SP
41 if (sensor_hub_device_open(st->hsdev))
42 return -EIO;
56ff6be6
SP
43
44 atomic_inc(&st->data_ready);
45
1a214ae5
SP
46 state_val = hid_sensor_get_usage_index(st->hsdev,
47 st->power_state.report_id,
48 st->power_state.index,
49 HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM);
50 report_val = hid_sensor_get_usage_index(st->hsdev,
51 st->report_state.report_id,
52 st->report_state.index,
53 HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
751d17e2 54 } else {
1e25aa96
SP
55 int val;
56
57 val = atomic_dec_if_positive(&st->data_ready);
58 if (val < 0)
56ff6be6 59 return 0;
1e25aa96 60
7d3c192d 61 sensor_hub_device_close(st->hsdev);
1a214ae5
SP
62 state_val = hid_sensor_get_usage_index(st->hsdev,
63 st->power_state.report_id,
64 st->power_state.index,
65 HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
66 report_val = hid_sensor_get_usage_index(st->hsdev,
67 st->report_state.report_id,
68 st->report_state.index,
69 HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
751d17e2 70 }
1a214ae5
SP
71
72 if (state_val >= 0) {
73 state_val += st->power_state.logical_minimum;
74 sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
3950e033
SP
75 st->power_state.index, sizeof(state_val),
76 &state_val);
1a214ae5 77 }
73c6768b 78
1a214ae5
SP
79 if (report_val >= 0) {
80 report_val += st->report_state.logical_minimum;
81 sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
3950e033
SP
82 st->report_state.index,
83 sizeof(report_val),
84 &report_val);
1a214ae5 85 }
73c6768b 86
c0a36f08 87 sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
6adc83fc
SP
88 st->power_state.index,
89 sizeof(state_val), &state_val);
3bec2474
SH
90 if (state)
91 poll_value = hid_sensor_read_poll_value(st);
92 if (poll_value > 0)
2b89635e
SP
93 msleep_interruptible(poll_value * 2);
94
73c6768b 95 return 0;
96}
56ff6be6
SP
97EXPORT_SYMBOL(hid_sensor_power_state);
98
2b89635e
SP
99int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
100{
1e25aa96 101
2b89635e
SP
102#ifdef CONFIG_PM
103 int ret;
104
1e25aa96 105 atomic_set(&st->user_requested_state, state);
2b89635e
SP
106 if (state)
107 ret = pm_runtime_get_sync(&st->pdev->dev);
108 else {
109 pm_runtime_mark_last_busy(&st->pdev->dev);
110 ret = pm_runtime_put_autosuspend(&st->pdev->dev);
111 }
112 if (ret < 0) {
113 if (state)
114 pm_runtime_put_noidle(&st->pdev->dev);
115 return ret;
116 }
117
d23057e0 118 return 0;
2b89635e 119#else
1e25aa96 120 atomic_set(&st->user_requested_state, state);
2b89635e
SP
121 return _hid_sensor_power_state(st, state);
122#endif
123}
124
7f6cf741
SP
125static void hid_sensor_set_power_work(struct work_struct *work)
126{
127 struct hid_sensor_common *attrb = container_of(work,
128 struct hid_sensor_common,
129 work);
130 _hid_sensor_power_state(attrb, true);
131}
132
56ff6be6
SP
133static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
134 bool state)
135{
136 return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
137}
73c6768b 138
ec7f68e0 139void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
73c6768b 140{
7f6cf741 141 cancel_work_sync(&attrb->work);
ec7f68e0
SP
142 iio_trigger_unregister(attrb->trigger);
143 iio_trigger_free(attrb->trigger);
73c6768b 144}
145EXPORT_SYMBOL(hid_sensor_remove_trigger);
146
147static const struct iio_trigger_ops hid_sensor_trigger_ops = {
148 .owner = THIS_MODULE,
149 .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
150};
151
152int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
e07c6d17 153 struct hid_sensor_common *attrb)
73c6768b 154{
155 int ret;
156 struct iio_trigger *trig;
157
158 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
159 if (trig == NULL) {
160 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
161 ret = -ENOMEM;
162 goto error_ret;
163 }
164
165 trig->dev.parent = indio_dev->dev.parent;
1e9663c6 166 iio_trigger_set_drvdata(trig, attrb);
73c6768b 167 trig->ops = &hid_sensor_trigger_ops;
168 ret = iio_trigger_register(trig);
169
170 if (ret) {
171 dev_err(&indio_dev->dev, "Trigger Register Failed\n");
172 goto error_free_trig;
173 }
55a6f9dd
SP
174 attrb->trigger = trig;
175 indio_dev->trig = iio_trigger_get(trig);
73c6768b 176
2b89635e
SP
177 ret = pm_runtime_set_active(&indio_dev->dev);
178 if (ret)
179 goto error_unreg_trigger;
73c6768b 180
2b89635e 181 iio_device_set_drvdata(indio_dev, attrb);
7f6cf741
SP
182
183 INIT_WORK(&attrb->work, hid_sensor_set_power_work);
184
2b89635e
SP
185 pm_suspend_ignore_children(&attrb->pdev->dev, true);
186 pm_runtime_enable(&attrb->pdev->dev);
187 /* Default to 3 seconds, but can be changed from sysfs */
188 pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
189 3000);
190 pm_runtime_use_autosuspend(&attrb->pdev->dev);
191
192 return ret;
193error_unreg_trigger:
194 iio_trigger_unregister(trig);
73c6768b 195error_free_trig:
196 iio_trigger_free(trig);
197error_ret:
198 return ret;
199}
200EXPORT_SYMBOL(hid_sensor_setup_trigger);
201
2e1f44d8 202static int __maybe_unused hid_sensor_suspend(struct device *dev)
2b89635e
SP
203{
204 struct platform_device *pdev = to_platform_device(dev);
205 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
206 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
207
208 return _hid_sensor_power_state(attrb, false);
209}
210
2e1f44d8 211static int __maybe_unused hid_sensor_resume(struct device *dev)
2b89635e
SP
212{
213 struct platform_device *pdev = to_platform_device(dev);
214 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
215 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
7f6cf741
SP
216 schedule_work(&attrb->work);
217 return 0;
218}
2b89635e 219
2e1f44d8 220static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
7f6cf741
SP
221{
222 struct platform_device *pdev = to_platform_device(dev);
223 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
224 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
2b89635e
SP
225 return _hid_sensor_power_state(attrb, true);
226}
227
2b89635e
SP
228const struct dev_pm_ops hid_sensor_pm_ops = {
229 SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
230 SET_RUNTIME_PM_OPS(hid_sensor_suspend,
7f6cf741 231 hid_sensor_runtime_resume, NULL)
2b89635e
SP
232};
233EXPORT_SYMBOL(hid_sensor_pm_ops);
234
73c6768b 235MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
236MODULE_DESCRIPTION("HID Sensor trigger processing");
237MODULE_LICENSE("GPL");