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/sysfs.h>
30 #include "hid-sensor-trigger.h"
31
_hid_sensor_power_state(struct hid_sensor_common * st,bool state)32 static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state)
33 {
34 int state_val;
35 int report_val;
36 s32 poll_value = 0;
37
38 if (state) {
39 if (sensor_hub_device_open(st->hsdev))
40 return -EIO;
41
42 atomic_inc(&st->data_ready);
43
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);
52
53 poll_value = hid_sensor_read_poll_value(st);
54 } else {
55 int val;
56
57 val = atomic_dec_if_positive(&st->data_ready);
58 if (val < 0)
59 return 0;
60
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);
70 }
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,
75 st->power_state.index, sizeof(state_val),
76 &state_val);
77 }
78
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,
83 sizeof(report_val),
84 &report_val);
85 }
86
87 pr_debug("HID_SENSOR %s set power_state %d report_state %d\n",
88 st->pdev->name, state_val, report_val);
89
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);
95
96 return 0;
97 }
98 EXPORT_SYMBOL(hid_sensor_power_state);
99
hid_sensor_power_state(struct hid_sensor_common * st,bool state)100 int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
101 {
102
103 #ifdef CONFIG_PM
104 int ret;
105
106 atomic_set(&st->user_requested_state, state);
107 if (state)
108 ret = pm_runtime_get_sync(&st->pdev->dev);
109 else {
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);
113 }
114 if (ret < 0) {
115 if (state)
116 pm_runtime_put_noidle(&st->pdev->dev);
117 return ret;
118 }
119
120 return 0;
121 #else
122 atomic_set(&st->user_requested_state, state);
123 return _hid_sensor_power_state(st, state);
124 #endif
125 }
126
hid_sensor_set_power_work(struct work_struct * work)127 static void hid_sensor_set_power_work(struct work_struct *work)
128 {
129 struct hid_sensor_common *attrb = container_of(work,
130 struct hid_sensor_common,
131 work);
132
133 if (attrb->poll_interval >= 0)
134 sensor_hub_set_feature(attrb->hsdev, attrb->poll.report_id,
135 attrb->poll.index,
136 sizeof(attrb->poll_interval),
137 &attrb->poll_interval);
138
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);
145
146 _hid_sensor_power_state(attrb, true);
147 }
148
hid_sensor_data_rdy_trigger_set_state(struct iio_trigger * trig,bool state)149 static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
150 bool state)
151 {
152 return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
153 }
154
hid_sensor_remove_trigger(struct hid_sensor_common * attrb)155 void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
156 {
157 cancel_work_sync(&attrb->work);
158 iio_trigger_unregister(attrb->trigger);
159 iio_trigger_free(attrb->trigger);
160 }
161 EXPORT_SYMBOL(hid_sensor_remove_trigger);
162
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,
166 };
167
hid_sensor_setup_trigger(struct iio_dev * indio_dev,const char * name,struct hid_sensor_common * attrb)168 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
169 struct hid_sensor_common *attrb)
170 {
171 int ret;
172 struct iio_trigger *trig;
173
174 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
175 if (trig == NULL) {
176 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
177 ret = -ENOMEM;
178 goto error_ret;
179 }
180
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);
185
186 if (ret) {
187 dev_err(&indio_dev->dev, "Trigger Register Failed\n");
188 goto error_free_trig;
189 }
190 attrb->trigger = trig;
191 indio_dev->trig = iio_trigger_get(trig);
192
193 ret = pm_runtime_set_active(&indio_dev->dev);
194 if (ret)
195 goto error_unreg_trigger;
196
197 iio_device_set_drvdata(indio_dev, attrb);
198
199 INIT_WORK(&attrb->work, hid_sensor_set_power_work);
200
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,
205 3000);
206 return ret;
207 error_unreg_trigger:
208 iio_trigger_unregister(trig);
209 error_free_trig:
210 iio_trigger_free(trig);
211 error_ret:
212 return ret;
213 }
214 EXPORT_SYMBOL(hid_sensor_setup_trigger);
215
hid_sensor_suspend(struct device * dev)216 static int __maybe_unused hid_sensor_suspend(struct device *dev)
217 {
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);
221
222 return _hid_sensor_power_state(attrb, false);
223 }
224
hid_sensor_resume(struct device * dev)225 static int __maybe_unused hid_sensor_resume(struct device *dev)
226 {
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);
231 return 0;
232 }
233
hid_sensor_runtime_resume(struct device * dev)234 static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
235 {
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);
240 }
241
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)
246 };
247 EXPORT_SYMBOL(hid_sensor_pm_ops);
248
249 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
250 MODULE_DESCRIPTION("HID Sensor trigger processing");
251 MODULE_LICENSE("GPL");
252