blob: 1e10c0af2f2c14f47e6a10fd0fcfef3ed19d80f7 [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>
Srinivas Pandruvada138bc792017-04-07 19:22:06 -070029#include <linux/iio/buffer.h>
srinivas pandruvada73c67682012-09-05 13:56:00 +010030#include <linux/iio/sysfs.h>
srinivas pandruvada73c67682012-09-05 13:56:00 +010031#include "hid-sensor-trigger.h"
32
Srinivas Pandruvada138bc792017-04-07 19:22:06 -070033static ssize_t _hid_sensor_set_report_latency(struct device *dev,
34 struct device_attribute *attr,
35 const char *buf, size_t len)
36{
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;
40 int latency;
41
42 ret = iio_str_to_fixpoint(buf, 100000, &integer, &fract);
43 if (ret)
44 return ret;
45
46 latency = integer * 1000 + fract / 1000;
47 ret = hid_sensor_set_report_latency(attrb, latency);
48 if (ret < 0)
49 return len;
50
51 attrb->latency_ms = hid_sensor_get_report_latency(attrb);
52
53 return len;
54}
55
56static ssize_t _hid_sensor_get_report_latency(struct device *dev,
57 struct device_attribute *attr,
58 char *buf)
59{
60 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
61 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
62 int latency;
63
64 latency = hid_sensor_get_report_latency(attrb);
65 if (latency < 0)
66 return latency;
67
68 return sprintf(buf, "%d.%06u\n", latency / 1000, (latency % 1000) * 1000);
69}
70
71static ssize_t _hid_sensor_get_fifo_state(struct device *dev,
72 struct device_attribute *attr,
73 char *buf)
74{
75 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
76 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
77 int latency;
78
79 latency = hid_sensor_get_report_latency(attrb);
80 if (latency < 0)
81 return latency;
82
83 return sprintf(buf, "%d\n", !!latency);
84}
85
86static IIO_DEVICE_ATTR(hwfifo_timeout, 0644,
87 _hid_sensor_get_report_latency,
88 _hid_sensor_set_report_latency, 0);
89static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
90 _hid_sensor_get_fifo_state, NULL, 0);
91
92static 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,
95 NULL,
96};
97
98static void hid_sensor_setup_batch_mode(struct iio_dev *indio_dev,
99 struct hid_sensor_common *st)
100{
101 if (!hid_sensor_batch_mode_supported(st))
102 return;
103
104 iio_buffer_set_attrs(indio_dev->buffer, hid_sensor_fifo_attributes);
105}
106
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800107static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state)
srinivas pandruvada73c67682012-09-05 13:56:00 +0100108{
srinivas pandruvada73c67682012-09-05 13:56:00 +0100109 int state_val;
Srinivas Pandruvada751d17e2013-11-27 22:19:00 +0000110 int report_val;
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800111 s32 poll_value = 0;
srinivas pandruvada73c67682012-09-05 13:56:00 +0100112
Srinivas Pandruvada7d3c1922013-09-18 18:13:00 +0100113 if (state) {
114 if (sensor_hub_device_open(st->hsdev))
115 return -EIO;
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100116
117 atomic_inc(&st->data_ready);
118
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -0800119 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);
Srinivas Pandruvada5d9854e2017-04-07 17:13:17 -0700127
128 poll_value = hid_sensor_read_poll_value(st);
Srinivas Pandruvada751d17e2013-11-27 22:19:00 +0000129 } else {
Srinivas Pandruvada1e25aa92015-06-01 16:36:27 -0700130 int val;
131
132 val = atomic_dec_if_positive(&st->data_ready);
133 if (val < 0)
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100134 return 0;
Srinivas Pandruvada1e25aa92015-06-01 16:36:27 -0700135
Srinivas Pandruvada751d17e2013-11-27 22:19:00 +0000136 sensor_hub_device_close(st->hsdev);
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -0800137 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);
Srinivas Pandruvada751d17e2013-11-27 22:19:00 +0000145 }
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -0800146
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,
Srinivas Pandruvada3950e032015-02-19 15:35:26 -0800150 st->power_state.index, sizeof(state_val),
151 &state_val);
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -0800152 }
srinivas pandruvada73c67682012-09-05 13:56:00 +0100153
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -0800154 if (report_val >= 0) {
155 report_val += st->report_state.logical_minimum;
156 sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
Srinivas Pandruvada3950e032015-02-19 15:35:26 -0800157 st->report_state.index,
158 sizeof(report_val),
159 &report_val);
Srinivas Pandruvada1a214ae2014-01-23 18:50:22 -0800160 }
srinivas pandruvada73c67682012-09-05 13:56:00 +0100161
Srinivas Pandruvadaf1664ea2017-08-12 09:09:21 -0700162 pr_debug("HID_SENSOR %s set power_state %d report_state %d\n",
163 st->pdev->name, state_val, report_val);
164
Archana Patnic0a36f02014-05-19 12:19:00 +0100165 sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
Srinivas Pandruvada6adc83f2015-02-19 15:35:25 -0800166 st->power_state.index,
167 sizeof(state_val), &state_val);
Srinivas Pandruvada5d9854e2017-04-07 17:13:17 -0700168 if (state && poll_value)
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800169 msleep_interruptible(poll_value * 2);
170
srinivas pandruvada73c67682012-09-05 13:56:00 +0100171 return 0;
172}
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100173EXPORT_SYMBOL(hid_sensor_power_state);
174
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800175int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
176{
Srinivas Pandruvada1e25aa92015-06-01 16:36:27 -0700177
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800178#ifdef CONFIG_PM
179 int ret;
180
Srinivas Pandruvadaad7532c2017-10-11 09:35:01 -0700181 if (atomic_add_unless(&st->runtime_pm_enable, 1, 1))
182 pm_runtime_enable(&st->pdev->dev);
183
Hans de Goede6f922532018-04-14 17:09:09 +0200184 if (state) {
185 atomic_inc(&st->user_requested_state);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800186 ret = pm_runtime_get_sync(&st->pdev->dev);
Hans de Goede6f922532018-04-14 17:09:09 +0200187 } else {
188 atomic_dec(&st->user_requested_state);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800189 pm_runtime_mark_last_busy(&st->pdev->dev);
Srinivas Pandruvadaf1664ea2017-08-12 09:09:21 -0700190 pm_runtime_use_autosuspend(&st->pdev->dev);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800191 ret = pm_runtime_put_autosuspend(&st->pdev->dev);
192 }
193 if (ret < 0) {
194 if (state)
195 pm_runtime_put_noidle(&st->pdev->dev);
196 return ret;
197 }
198
Slawomir Stepiend23057e2016-04-14 21:36:40 +0200199 return 0;
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800200#else
Srinivas Pandruvada1e25aa92015-06-01 16:36:27 -0700201 atomic_set(&st->user_requested_state, state);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800202 return _hid_sensor_power_state(st, state);
203#endif
204}
205
Srinivas Pandruvada7f6cf742016-08-15 12:12:47 -0700206static void hid_sensor_set_power_work(struct work_struct *work)
207{
208 struct hid_sensor_common *attrb = container_of(work,
209 struct hid_sensor_common,
210 work);
Srinivas Pandruvada5d9854e2017-04-07 17:13:17 -0700211
212 if (attrb->poll_interval >= 0)
213 sensor_hub_set_feature(attrb->hsdev, attrb->poll.report_id,
214 attrb->poll.index,
215 sizeof(attrb->poll_interval),
216 &attrb->poll_interval);
217
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);
224
Srinivas Pandruvada138bc792017-04-07 19:22:06 -0700225 if (attrb->latency_ms > 0)
226 hid_sensor_set_report_latency(attrb, attrb->latency_ms);
227
Srinivas Pandruvadaad7532c2017-10-11 09:35:01 -0700228 if (atomic_read(&attrb->user_requested_state))
229 _hid_sensor_power_state(attrb, true);
Srinivas Pandruvada7f6cf742016-08-15 12:12:47 -0700230}
231
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100232static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
233 bool state)
234{
235 return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
236}
srinivas pandruvada73c67682012-09-05 13:56:00 +0100237
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000238void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
srinivas pandruvada73c67682012-09-05 13:56:00 +0100239{
Srinivas Pandruvadaad7532c2017-10-11 09:35:01 -0700240 if (atomic_read(&attrb->runtime_pm_enable))
241 pm_runtime_disable(&attrb->pdev->dev);
242
Srinivas Pandruvadac94f5802017-04-07 17:16:34 -0700243 pm_runtime_set_suspended(&attrb->pdev->dev);
244 pm_runtime_put_noidle(&attrb->pdev->dev);
245
Srinivas Pandruvada7f6cf742016-08-15 12:12:47 -0700246 cancel_work_sync(&attrb->work);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000247 iio_trigger_unregister(attrb->trigger);
248 iio_trigger_free(attrb->trigger);
srinivas pandruvada73c67682012-09-05 13:56:00 +0100249}
250EXPORT_SYMBOL(hid_sensor_remove_trigger);
251
252static const struct iio_trigger_ops hid_sensor_trigger_ops = {
srinivas pandruvada73c67682012-09-05 13:56:00 +0100253 .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
254};
255
256int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
Alexander Hollere07c6d12012-12-15 12:45:00 +0000257 struct hid_sensor_common *attrb)
srinivas pandruvada73c67682012-09-05 13:56:00 +0100258{
259 int ret;
260 struct iio_trigger *trig;
261
262 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
263 if (trig == NULL) {
264 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
265 ret = -ENOMEM;
266 goto error_ret;
267 }
268
269 trig->dev.parent = indio_dev->dev.parent;
Lars-Peter Clausen1e9663c2013-03-25 08:58:00 +0000270 iio_trigger_set_drvdata(trig, attrb);
srinivas pandruvada73c67682012-09-05 13:56:00 +0100271 trig->ops = &hid_sensor_trigger_ops;
272 ret = iio_trigger_register(trig);
273
274 if (ret) {
275 dev_err(&indio_dev->dev, "Trigger Register Failed\n");
276 goto error_free_trig;
277 }
Srinivas Pandruvada55a6f9d2014-08-22 21:48:00 +0100278 attrb->trigger = trig;
279 indio_dev->trig = iio_trigger_get(trig);
srinivas pandruvada73c67682012-09-05 13:56:00 +0100280
Srinivas Pandruvada138bc792017-04-07 19:22:06 -0700281 hid_sensor_setup_batch_mode(indio_dev, attrb);
282
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800283 ret = pm_runtime_set_active(&indio_dev->dev);
284 if (ret)
285 goto error_unreg_trigger;
srinivas pandruvada73c67682012-09-05 13:56:00 +0100286
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800287 iio_device_set_drvdata(indio_dev, attrb);
Srinivas Pandruvada7f6cf742016-08-15 12:12:47 -0700288
289 INIT_WORK(&attrb->work, hid_sensor_set_power_work);
290
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800291 pm_suspend_ignore_children(&attrb->pdev->dev, true);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800292 /* Default to 3 seconds, but can be changed from sysfs */
293 pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
294 3000);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800295 return ret;
296error_unreg_trigger:
297 iio_trigger_unregister(trig);
srinivas pandruvada73c67682012-09-05 13:56:00 +0100298error_free_trig:
299 iio_trigger_free(trig);
300error_ret:
301 return ret;
302}
303EXPORT_SYMBOL(hid_sensor_setup_trigger);
304
Arnd Bergmann2e1f44d2016-08-26 17:29:35 +0200305static int __maybe_unused hid_sensor_suspend(struct device *dev)
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800306{
Wolfram Sangebbdcba2018-04-19 16:05:49 +0200307 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800308 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
309
310 return _hid_sensor_power_state(attrb, false);
311}
312
Arnd Bergmann2e1f44d2016-08-26 17:29:35 +0200313static int __maybe_unused hid_sensor_resume(struct device *dev)
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800314{
Wolfram Sangebbdcba2018-04-19 16:05:49 +0200315 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800316 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
Srinivas Pandruvada7f6cf742016-08-15 12:12:47 -0700317 schedule_work(&attrb->work);
318 return 0;
319}
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800320
Arnd Bergmann2e1f44d2016-08-26 17:29:35 +0200321static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
Srinivas Pandruvada7f6cf742016-08-15 12:12:47 -0700322{
Wolfram Sangebbdcba2018-04-19 16:05:49 +0200323 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Srinivas Pandruvada7f6cf742016-08-15 12:12:47 -0700324 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800325 return _hid_sensor_power_state(attrb, true);
326}
327
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800328const 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,
Srinivas Pandruvada7f6cf742016-08-15 12:12:47 -0700331 hid_sensor_runtime_resume, NULL)
Srinivas Pandruvada2b896352015-01-07 10:55:53 -0800332};
333EXPORT_SYMBOL(hid_sensor_pm_ops);
334
srinivas pandruvada73c67682012-09-05 13:56:00 +0100335MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
336MODULE_DESCRIPTION("HID Sensor trigger processing");
337MODULE_LICENSE("GPL");