3 * Copyright (c) 2012, Intel Corporation.
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.
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
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.
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/sysfs.h>
30 #include "hid-sensor-trigger.h"
32 static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state)
39 if (sensor_hub_device_open(st->hsdev))
42 atomic_inc(&st->data_ready);
44 state_val = hid_sensor_get_usage_index(st->hsdev,
45 st->power_state.report_id,
46 st->power_state.index,
47 HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM);
48 report_val = hid_sensor_get_usage_index(st->hsdev,
49 st->report_state.report_id,
50 st->report_state.index,
51 HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
53 poll_value = hid_sensor_read_poll_value(st);
57 val = atomic_dec_if_positive(&st->data_ready);
61 sensor_hub_device_close(st->hsdev);
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);
73 state_val += st->power_state.logical_minimum;
74 sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
75 st->power_state.index, sizeof(state_val),
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,
82 st->report_state.index,
87 pr_debug("HID_SENSOR %s set power_state %d report_state %d\n",
88 st->pdev->name, state_val, report_val);
90 sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
91 st->power_state.index,
92 sizeof(state_val), &state_val);
93 if (state && poll_value)
94 msleep_interruptible(poll_value * 2);
98 EXPORT_SYMBOL(hid_sensor_power_state);
100 int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
106 atomic_set(&st->user_requested_state, state);
108 ret = pm_runtime_get_sync(&st->pdev->dev);
110 pm_runtime_mark_last_busy(&st->pdev->dev);
111 pm_runtime_use_autosuspend(&st->pdev->dev);
112 ret = pm_runtime_put_autosuspend(&st->pdev->dev);
116 pm_runtime_put_noidle(&st->pdev->dev);
122 atomic_set(&st->user_requested_state, state);
123 return _hid_sensor_power_state(st, state);
127 static void hid_sensor_set_power_work(struct work_struct *work)
129 struct hid_sensor_common *attrb = container_of(work,
130 struct hid_sensor_common,
133 if (attrb->poll_interval >= 0)
134 sensor_hub_set_feature(attrb->hsdev, attrb->poll.report_id,
136 sizeof(attrb->poll_interval),
137 &attrb->poll_interval);
139 if (attrb->raw_hystersis >= 0)
140 sensor_hub_set_feature(attrb->hsdev,
141 attrb->sensitivity.report_id,
142 attrb->sensitivity.index,
143 sizeof(attrb->raw_hystersis),
144 &attrb->raw_hystersis);
146 _hid_sensor_power_state(attrb, true);
149 static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
152 return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
155 void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
157 cancel_work_sync(&attrb->work);
158 iio_trigger_unregister(attrb->trigger);
159 iio_trigger_free(attrb->trigger);
161 EXPORT_SYMBOL(hid_sensor_remove_trigger);
163 static const struct iio_trigger_ops hid_sensor_trigger_ops = {
164 .owner = THIS_MODULE,
165 .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
168 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
169 struct hid_sensor_common *attrb)
172 struct iio_trigger *trig;
174 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
176 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
181 trig->dev.parent = indio_dev->dev.parent;
182 iio_trigger_set_drvdata(trig, attrb);
183 trig->ops = &hid_sensor_trigger_ops;
184 ret = iio_trigger_register(trig);
187 dev_err(&indio_dev->dev, "Trigger Register Failed\n");
188 goto error_free_trig;
190 attrb->trigger = trig;
191 indio_dev->trig = iio_trigger_get(trig);
193 ret = pm_runtime_set_active(&indio_dev->dev);
195 goto error_unreg_trigger;
197 iio_device_set_drvdata(indio_dev, attrb);
199 INIT_WORK(&attrb->work, hid_sensor_set_power_work);
201 pm_suspend_ignore_children(&attrb->pdev->dev, true);
202 pm_runtime_enable(&attrb->pdev->dev);
203 /* Default to 3 seconds, but can be changed from sysfs */
204 pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
208 iio_trigger_unregister(trig);
210 iio_trigger_free(trig);
214 EXPORT_SYMBOL(hid_sensor_setup_trigger);
216 static int __maybe_unused hid_sensor_suspend(struct device *dev)
218 struct platform_device *pdev = to_platform_device(dev);
219 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
220 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
222 return _hid_sensor_power_state(attrb, false);
225 static int __maybe_unused hid_sensor_resume(struct device *dev)
227 struct platform_device *pdev = to_platform_device(dev);
228 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
229 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
230 schedule_work(&attrb->work);
234 static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
236 struct platform_device *pdev = to_platform_device(dev);
237 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
238 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
239 return _hid_sensor_power_state(attrb, true);
242 const struct dev_pm_ops hid_sensor_pm_ops = {
243 SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
244 SET_RUNTIME_PM_OPS(hid_sensor_suspend,
245 hid_sensor_runtime_resume, NULL)
247 EXPORT_SYMBOL(hid_sensor_pm_ops);
249 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
250 MODULE_DESCRIPTION("HID Sensor trigger processing");
251 MODULE_LICENSE("GPL");