blob: 2f1d535b94c45d08a1824f17081466bace1ea14a [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>
Srinivas Pandruvada2b896352015-01-07 10:55:53 -080025#include <linux/delay.h>
srinivas pandruvada73c67682012-09-05 13:56:00 +010026#include <linux/hid-sensor-hub.h>
27#include <linux/iio/iio.h>
28#include <linux/iio/trigger.h>
29#include <linux/iio/sysfs.h>
srinivas pandruvada73c67682012-09-05 13:56:00 +010030#include "hid-sensor-trigger.h"
31
Srinivas Pandruvada2b896352015-01-07 10:55:53 -080032static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state)
srinivas pandruvada73c67682012-09-05 13:56:00 +010033{
srinivas pandruvada73c67682012-09-05 13:56:00 +010034 int state_val;
Srinivas Pandruvada751d17e2013-11-27 22:19:00 +000035 int report_val;
Srinivas Pandruvada2b896352015-01-07 10:55:53 -080036 s32 poll_value = 0;
srinivas pandruvada73c67682012-09-05 13:56:00 +010037
Srinivas Pandruvada7d3c1922013-09-18 18:13:00 +010038 if (state) {
39 if (sensor_hub_device_open(st->hsdev))
40 return -EIO;
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +010041
42 atomic_inc(&st->data_ready);
43
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -080044 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);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -080052
53 poll_value = hid_sensor_read_poll_value(st);
Srinivas Pandruvada751d17e2013-11-27 22:19:00 +000054 } else {
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +010055 if (!atomic_dec_and_test(&st->data_ready))
56 return 0;
Srinivas Pandruvada751d17e2013-11-27 22:19:00 +000057 sensor_hub_device_close(st->hsdev);
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -080058 state_val = hid_sensor_get_usage_index(st->hsdev,
59 st->power_state.report_id,
60 st->power_state.index,
61 HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
62 report_val = hid_sensor_get_usage_index(st->hsdev,
63 st->report_state.report_id,
64 st->report_state.index,
65 HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
Srinivas Pandruvada751d17e2013-11-27 22:19:00 +000066 }
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -080067
68 if (state_val >= 0) {
69 state_val += st->power_state.logical_minimum;
70 sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
srinivas pandruvada73c67682012-09-05 13:56:00 +010071 st->power_state.index,
72 (s32)state_val);
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -080073 }
srinivas pandruvada73c67682012-09-05 13:56:00 +010074
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -080075 if (report_val >= 0) {
76 report_val += st->report_state.logical_minimum;
77 sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
srinivas pandruvada73c67682012-09-05 13:56:00 +010078 st->report_state.index,
Srinivas Pandruvada751d17e2013-11-27 22:19:00 +000079 (s32)report_val);
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -080080 }
srinivas pandruvada73c67682012-09-05 13:56:00 +010081
Archana Patnic0a36f02014-05-19 12:19:00 +010082 sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
83 st->power_state.index,
84 &state_val);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -080085 if (state && poll_value)
86 msleep_interruptible(poll_value * 2);
87
srinivas pandruvada73c67682012-09-05 13:56:00 +010088 return 0;
89}
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +010090EXPORT_SYMBOL(hid_sensor_power_state);
91
Srinivas Pandruvada2b896352015-01-07 10:55:53 -080092int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
93{
94#ifdef CONFIG_PM
95 int ret;
96
97 if (state)
98 ret = pm_runtime_get_sync(&st->pdev->dev);
99 else {
100 pm_runtime_mark_last_busy(&st->pdev->dev);
101 ret = pm_runtime_put_autosuspend(&st->pdev->dev);
102 }
103 if (ret < 0) {
104 if (state)
105 pm_runtime_put_noidle(&st->pdev->dev);
106 return ret;
107 }
108
109 return 0;
110#else
111 return _hid_sensor_power_state(st, state);
112#endif
113}
114
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100115static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
116 bool state)
117{
118 return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
119}
srinivas pandruvada73c67682012-09-05 13:56:00 +0100120
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000121void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
srinivas pandruvada73c67682012-09-05 13:56:00 +0100122{
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000123 iio_trigger_unregister(attrb->trigger);
124 iio_trigger_free(attrb->trigger);
srinivas pandruvada73c67682012-09-05 13:56:00 +0100125}
126EXPORT_SYMBOL(hid_sensor_remove_trigger);
127
128static const struct iio_trigger_ops hid_sensor_trigger_ops = {
129 .owner = THIS_MODULE,
130 .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
131};
132
133int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
Alexander Hollere07c6d12012-12-15 12:45:00 +0000134 struct hid_sensor_common *attrb)
srinivas pandruvada73c67682012-09-05 13:56:00 +0100135{
136 int ret;
137 struct iio_trigger *trig;
138
139 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
140 if (trig == NULL) {
141 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
142 ret = -ENOMEM;
143 goto error_ret;
144 }
145
146 trig->dev.parent = indio_dev->dev.parent;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000147 iio_trigger_set_drvdata(trig, attrb);
srinivas pandruvada73c67682012-09-05 13:56:00 +0100148 trig->ops = &hid_sensor_trigger_ops;
149 ret = iio_trigger_register(trig);
150
151 if (ret) {
152 dev_err(&indio_dev->dev, "Trigger Register Failed\n");
153 goto error_free_trig;
154 }
Srinivas Pandruvada55a6f9d2014-08-22 21:48:00 +0100155 attrb->trigger = trig;
156 indio_dev->trig = iio_trigger_get(trig);
srinivas pandruvada73c67682012-09-05 13:56:00 +0100157
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800158 ret = pm_runtime_set_active(&indio_dev->dev);
159 if (ret)
160 goto error_unreg_trigger;
srinivas pandruvada73c67682012-09-05 13:56:00 +0100161
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800162 iio_device_set_drvdata(indio_dev, attrb);
163 pm_suspend_ignore_children(&attrb->pdev->dev, true);
164 pm_runtime_enable(&attrb->pdev->dev);
165 /* Default to 3 seconds, but can be changed from sysfs */
166 pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
167 3000);
168 pm_runtime_use_autosuspend(&attrb->pdev->dev);
169
170 return ret;
171error_unreg_trigger:
172 iio_trigger_unregister(trig);
srinivas pandruvada73c67682012-09-05 13:56:00 +0100173error_free_trig:
174 iio_trigger_free(trig);
175error_ret:
176 return ret;
177}
178EXPORT_SYMBOL(hid_sensor_setup_trigger);
179
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800180#ifdef CONFIG_PM
181static int hid_sensor_suspend(struct device *dev)
182{
183 struct platform_device *pdev = to_platform_device(dev);
184 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
185 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
186
187 return _hid_sensor_power_state(attrb, false);
188}
189
190static int hid_sensor_resume(struct device *dev)
191{
192 struct platform_device *pdev = to_platform_device(dev);
193 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
194 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
195
196 return _hid_sensor_power_state(attrb, true);
197}
198
199#endif
200
201const struct dev_pm_ops hid_sensor_pm_ops = {
202 SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
203 SET_RUNTIME_PM_OPS(hid_sensor_suspend,
204 hid_sensor_resume, NULL)
205};
206EXPORT_SYMBOL(hid_sensor_pm_ops);
207
srinivas pandruvada73c67682012-09-05 13:56:00 +0100208MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
209MODULE_DESCRIPTION("HID Sensor trigger processing");
210MODULE_LICENSE("GPL");