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/buffer.h>
30 #include <linux/iio/sysfs.h>
31 #include "hid-sensor-trigger.h"
33 static ssize_t
_hid_sensor_set_report_latency(struct device
*dev
,
34 struct device_attribute
*attr
,
35 const char *buf
, size_t len
)
37 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
38 struct hid_sensor_common
*attrb
= iio_device_get_drvdata(indio_dev
);
39 int integer
, fract
, ret
;
42 ret
= iio_str_to_fixpoint(buf
, 100000, &integer
, &fract
);
46 latency
= integer
* 1000 + fract
/ 1000;
47 ret
= hid_sensor_set_report_latency(attrb
, latency
);
51 attrb
->latency_ms
= hid_sensor_get_report_latency(attrb
);
56 static ssize_t
_hid_sensor_get_report_latency(struct device
*dev
,
57 struct device_attribute
*attr
,
60 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
61 struct hid_sensor_common
*attrb
= iio_device_get_drvdata(indio_dev
);
64 latency
= hid_sensor_get_report_latency(attrb
);
68 return sprintf(buf
, "%d.%06u\n", latency
/ 1000, (latency
% 1000) * 1000);
71 static ssize_t
_hid_sensor_get_fifo_state(struct device
*dev
,
72 struct device_attribute
*attr
,
75 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
76 struct hid_sensor_common
*attrb
= iio_device_get_drvdata(indio_dev
);
79 latency
= hid_sensor_get_report_latency(attrb
);
83 return sprintf(buf
, "%d\n", !!latency
);
86 static IIO_DEVICE_ATTR(hwfifo_timeout
, 0644,
87 _hid_sensor_get_report_latency
,
88 _hid_sensor_set_report_latency
, 0);
89 static IIO_DEVICE_ATTR(hwfifo_enabled
, 0444,
90 _hid_sensor_get_fifo_state
, NULL
, 0);
92 static const struct attribute
*hid_sensor_fifo_attributes
[] = {
93 &iio_dev_attr_hwfifo_timeout
.dev_attr
.attr
,
94 &iio_dev_attr_hwfifo_enabled
.dev_attr
.attr
,
98 static void hid_sensor_setup_batch_mode(struct iio_dev
*indio_dev
,
99 struct hid_sensor_common
*st
)
101 if (!hid_sensor_batch_mode_supported(st
))
104 iio_buffer_set_attrs(indio_dev
->buffer
, hid_sensor_fifo_attributes
);
107 static int _hid_sensor_power_state(struct hid_sensor_common
*st
, bool state
)
114 if (sensor_hub_device_open(st
->hsdev
))
117 atomic_inc(&st
->data_ready
);
119 state_val
= hid_sensor_get_usage_index(st
->hsdev
,
120 st
->power_state
.report_id
,
121 st
->power_state
.index
,
122 HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM
);
123 report_val
= hid_sensor_get_usage_index(st
->hsdev
,
124 st
->report_state
.report_id
,
125 st
->report_state
.index
,
126 HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM
);
128 poll_value
= hid_sensor_read_poll_value(st
);
132 val
= atomic_dec_if_positive(&st
->data_ready
);
136 sensor_hub_device_close(st
->hsdev
);
137 state_val
= hid_sensor_get_usage_index(st
->hsdev
,
138 st
->power_state
.report_id
,
139 st
->power_state
.index
,
140 HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM
);
141 report_val
= hid_sensor_get_usage_index(st
->hsdev
,
142 st
->report_state
.report_id
,
143 st
->report_state
.index
,
144 HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM
);
147 if (state_val
>= 0) {
148 state_val
+= st
->power_state
.logical_minimum
;
149 sensor_hub_set_feature(st
->hsdev
, st
->power_state
.report_id
,
150 st
->power_state
.index
, sizeof(state_val
),
154 if (report_val
>= 0) {
155 report_val
+= st
->report_state
.logical_minimum
;
156 sensor_hub_set_feature(st
->hsdev
, st
->report_state
.report_id
,
157 st
->report_state
.index
,
162 pr_debug("HID_SENSOR %s set power_state %d report_state %d\n",
163 st
->pdev
->name
, state_val
, report_val
);
165 sensor_hub_get_feature(st
->hsdev
, st
->power_state
.report_id
,
166 st
->power_state
.index
,
167 sizeof(state_val
), &state_val
);
168 if (state
&& poll_value
)
169 msleep_interruptible(poll_value
* 2);
173 EXPORT_SYMBOL(hid_sensor_power_state
);
175 int hid_sensor_power_state(struct hid_sensor_common
*st
, bool state
)
181 if (atomic_add_unless(&st
->runtime_pm_enable
, 1, 1))
182 pm_runtime_enable(&st
->pdev
->dev
);
185 atomic_inc(&st
->user_requested_state
);
186 ret
= pm_runtime_get_sync(&st
->pdev
->dev
);
188 atomic_dec(&st
->user_requested_state
);
189 pm_runtime_mark_last_busy(&st
->pdev
->dev
);
190 pm_runtime_use_autosuspend(&st
->pdev
->dev
);
191 ret
= pm_runtime_put_autosuspend(&st
->pdev
->dev
);
195 pm_runtime_put_noidle(&st
->pdev
->dev
);
201 atomic_set(&st
->user_requested_state
, state
);
202 return _hid_sensor_power_state(st
, state
);
206 static void hid_sensor_set_power_work(struct work_struct
*work
)
208 struct hid_sensor_common
*attrb
= container_of(work
,
209 struct hid_sensor_common
,
212 if (attrb
->poll_interval
>= 0)
213 sensor_hub_set_feature(attrb
->hsdev
, attrb
->poll
.report_id
,
215 sizeof(attrb
->poll_interval
),
216 &attrb
->poll_interval
);
218 if (attrb
->raw_hystersis
>= 0)
219 sensor_hub_set_feature(attrb
->hsdev
,
220 attrb
->sensitivity
.report_id
,
221 attrb
->sensitivity
.index
,
222 sizeof(attrb
->raw_hystersis
),
223 &attrb
->raw_hystersis
);
225 if (attrb
->latency_ms
> 0)
226 hid_sensor_set_report_latency(attrb
, attrb
->latency_ms
);
228 if (atomic_read(&attrb
->user_requested_state
))
229 _hid_sensor_power_state(attrb
, true);
232 static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
235 return hid_sensor_power_state(iio_trigger_get_drvdata(trig
), state
);
238 void hid_sensor_remove_trigger(struct hid_sensor_common
*attrb
)
240 if (atomic_read(&attrb
->runtime_pm_enable
))
241 pm_runtime_disable(&attrb
->pdev
->dev
);
243 pm_runtime_set_suspended(&attrb
->pdev
->dev
);
244 pm_runtime_put_noidle(&attrb
->pdev
->dev
);
246 cancel_work_sync(&attrb
->work
);
247 iio_trigger_unregister(attrb
->trigger
);
248 iio_trigger_free(attrb
->trigger
);
250 EXPORT_SYMBOL(hid_sensor_remove_trigger
);
252 static const struct iio_trigger_ops hid_sensor_trigger_ops
= {
253 .set_trigger_state
= &hid_sensor_data_rdy_trigger_set_state
,
256 int hid_sensor_setup_trigger(struct iio_dev
*indio_dev
, const char *name
,
257 struct hid_sensor_common
*attrb
)
260 struct iio_trigger
*trig
;
262 trig
= iio_trigger_alloc("%s-dev%d", name
, indio_dev
->id
);
264 dev_err(&indio_dev
->dev
, "Trigger Allocate Failed\n");
269 trig
->dev
.parent
= indio_dev
->dev
.parent
;
270 iio_trigger_set_drvdata(trig
, attrb
);
271 trig
->ops
= &hid_sensor_trigger_ops
;
272 ret
= iio_trigger_register(trig
);
275 dev_err(&indio_dev
->dev
, "Trigger Register Failed\n");
276 goto error_free_trig
;
278 attrb
->trigger
= trig
;
279 indio_dev
->trig
= iio_trigger_get(trig
);
281 hid_sensor_setup_batch_mode(indio_dev
, attrb
);
283 ret
= pm_runtime_set_active(&indio_dev
->dev
);
285 goto error_unreg_trigger
;
287 iio_device_set_drvdata(indio_dev
, attrb
);
289 INIT_WORK(&attrb
->work
, hid_sensor_set_power_work
);
291 pm_suspend_ignore_children(&attrb
->pdev
->dev
, true);
292 /* Default to 3 seconds, but can be changed from sysfs */
293 pm_runtime_set_autosuspend_delay(&attrb
->pdev
->dev
,
297 iio_trigger_unregister(trig
);
299 iio_trigger_free(trig
);
303 EXPORT_SYMBOL(hid_sensor_setup_trigger
);
305 static int __maybe_unused
hid_sensor_suspend(struct device
*dev
)
307 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
308 struct hid_sensor_common
*attrb
= iio_device_get_drvdata(indio_dev
);
310 return _hid_sensor_power_state(attrb
, false);
313 static int __maybe_unused
hid_sensor_resume(struct device
*dev
)
315 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
316 struct hid_sensor_common
*attrb
= iio_device_get_drvdata(indio_dev
);
317 schedule_work(&attrb
->work
);
321 static int __maybe_unused
hid_sensor_runtime_resume(struct device
*dev
)
323 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
324 struct hid_sensor_common
*attrb
= iio_device_get_drvdata(indio_dev
);
325 return _hid_sensor_power_state(attrb
, true);
328 const struct dev_pm_ops hid_sensor_pm_ops
= {
329 SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend
, hid_sensor_resume
)
330 SET_RUNTIME_PM_OPS(hid_sensor_suspend
,
331 hid_sensor_runtime_resume
, NULL
)
333 EXPORT_SYMBOL(hid_sensor_pm_ops
);
335 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
336 MODULE_DESCRIPTION("HID Sensor trigger processing");
337 MODULE_LICENSE("GPL");