• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HID Sensors Driver
4  * Copyright (c) 2012, Intel Corporation.
5  */
6 #include <linux/device.h>
7 #include <linux/platform_device.h>
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/slab.h>
12 #include <linux/delay.h>
13 #include <linux/hid-sensor-hub.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/trigger.h>
16 #include <linux/iio/triggered_buffer.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/sysfs.h>
20 #include "hid-sensor-trigger.h"
21 
_hid_sensor_set_report_latency(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)22 static ssize_t _hid_sensor_set_report_latency(struct device *dev,
23 					      struct device_attribute *attr,
24 					      const char *buf, size_t len)
25 {
26 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
27 	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
28 	int integer, fract, ret;
29 	int latency;
30 
31 	ret = iio_str_to_fixpoint(buf, 100000, &integer, &fract);
32 	if (ret)
33 		return ret;
34 
35 	latency = integer * 1000 + fract / 1000;
36 	ret = hid_sensor_set_report_latency(attrb, latency);
37 	if (ret < 0)
38 		return len;
39 
40 	attrb->latency_ms = hid_sensor_get_report_latency(attrb);
41 
42 	return len;
43 }
44 
_hid_sensor_get_report_latency(struct device * dev,struct device_attribute * attr,char * buf)45 static ssize_t _hid_sensor_get_report_latency(struct device *dev,
46 					      struct device_attribute *attr,
47 					      char *buf)
48 {
49 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
50 	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
51 	int latency;
52 
53 	latency = hid_sensor_get_report_latency(attrb);
54 	if (latency < 0)
55 		return latency;
56 
57 	return sprintf(buf, "%d.%06u\n", latency / 1000, (latency % 1000) * 1000);
58 }
59 
_hid_sensor_get_fifo_state(struct device * dev,struct device_attribute * attr,char * buf)60 static ssize_t _hid_sensor_get_fifo_state(struct device *dev,
61 					  struct device_attribute *attr,
62 					  char *buf)
63 {
64 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
65 	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
66 	int latency;
67 
68 	latency = hid_sensor_get_report_latency(attrb);
69 	if (latency < 0)
70 		return latency;
71 
72 	return sprintf(buf, "%d\n", !!latency);
73 }
74 
75 static IIO_DEVICE_ATTR(hwfifo_timeout, 0644,
76 		       _hid_sensor_get_report_latency,
77 		       _hid_sensor_set_report_latency, 0);
78 static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
79 		       _hid_sensor_get_fifo_state, NULL, 0);
80 
81 static const struct attribute *hid_sensor_fifo_attributes[] = {
82 	&iio_dev_attr_hwfifo_timeout.dev_attr.attr,
83 	&iio_dev_attr_hwfifo_enabled.dev_attr.attr,
84 	NULL,
85 };
86 
hid_sensor_setup_batch_mode(struct iio_dev * indio_dev,struct hid_sensor_common * st)87 static void hid_sensor_setup_batch_mode(struct iio_dev *indio_dev,
88 					struct hid_sensor_common *st)
89 {
90 	if (!hid_sensor_batch_mode_supported(st))
91 		return;
92 
93 	iio_buffer_set_attrs(indio_dev->buffer, hid_sensor_fifo_attributes);
94 }
95 
_hid_sensor_power_state(struct hid_sensor_common * st,bool state)96 static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state)
97 {
98 	int state_val;
99 	int report_val;
100 	s32 poll_value = 0;
101 
102 	if (state) {
103 		if (sensor_hub_device_open(st->hsdev))
104 			return -EIO;
105 
106 		atomic_inc(&st->data_ready);
107 
108 		state_val = hid_sensor_get_usage_index(st->hsdev,
109 			st->power_state.report_id,
110 			st->power_state.index,
111 			HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM);
112 		report_val = hid_sensor_get_usage_index(st->hsdev,
113 			st->report_state.report_id,
114 			st->report_state.index,
115 			HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
116 
117 		poll_value = hid_sensor_read_poll_value(st);
118 	} else {
119 		int val;
120 
121 		val = atomic_dec_if_positive(&st->data_ready);
122 		if (val < 0)
123 			return 0;
124 
125 		sensor_hub_device_close(st->hsdev);
126 		state_val = hid_sensor_get_usage_index(st->hsdev,
127 			st->power_state.report_id,
128 			st->power_state.index,
129 			HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
130 		report_val = hid_sensor_get_usage_index(st->hsdev,
131 			st->report_state.report_id,
132 			st->report_state.index,
133 			HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
134 	}
135 
136 	if (state_val >= 0) {
137 		state_val += st->power_state.logical_minimum;
138 		sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
139 				       st->power_state.index, sizeof(state_val),
140 				       &state_val);
141 	}
142 
143 	if (report_val >= 0) {
144 		report_val += st->report_state.logical_minimum;
145 		sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
146 				       st->report_state.index,
147 				       sizeof(report_val),
148 				       &report_val);
149 	}
150 
151 	pr_debug("HID_SENSOR %s set power_state %d report_state %d\n",
152 		 st->pdev->name, state_val, report_val);
153 
154 	sensor_hub_get_feature(st->hsdev, st->power_state.report_id,
155 			       st->power_state.index,
156 			       sizeof(state_val), &state_val);
157 	if (state && poll_value)
158 		msleep_interruptible(poll_value * 2);
159 
160 	return 0;
161 }
162 EXPORT_SYMBOL(hid_sensor_power_state);
163 
hid_sensor_power_state(struct hid_sensor_common * st,bool state)164 int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
165 {
166 
167 #ifdef CONFIG_PM
168 	int ret;
169 
170 	if (atomic_add_unless(&st->runtime_pm_enable, 1, 1))
171 		pm_runtime_enable(&st->pdev->dev);
172 
173 	if (state) {
174 		atomic_inc(&st->user_requested_state);
175 		ret = pm_runtime_get_sync(&st->pdev->dev);
176 	} else {
177 		atomic_dec(&st->user_requested_state);
178 		pm_runtime_mark_last_busy(&st->pdev->dev);
179 		pm_runtime_use_autosuspend(&st->pdev->dev);
180 		ret = pm_runtime_put_autosuspend(&st->pdev->dev);
181 	}
182 	if (ret < 0) {
183 		if (state)
184 			pm_runtime_put_noidle(&st->pdev->dev);
185 		return ret;
186 	}
187 
188 	return 0;
189 #else
190 	atomic_set(&st->user_requested_state, state);
191 	return _hid_sensor_power_state(st, state);
192 #endif
193 }
194 
hid_sensor_set_power_work(struct work_struct * work)195 static void hid_sensor_set_power_work(struct work_struct *work)
196 {
197 	struct hid_sensor_common *attrb = container_of(work,
198 						       struct hid_sensor_common,
199 						       work);
200 
201 	if (attrb->poll_interval >= 0)
202 		sensor_hub_set_feature(attrb->hsdev, attrb->poll.report_id,
203 				       attrb->poll.index,
204 				       sizeof(attrb->poll_interval),
205 				       &attrb->poll_interval);
206 
207 	if (attrb->raw_hystersis >= 0)
208 		sensor_hub_set_feature(attrb->hsdev,
209 				       attrb->sensitivity.report_id,
210 				       attrb->sensitivity.index,
211 				       sizeof(attrb->raw_hystersis),
212 				       &attrb->raw_hystersis);
213 
214 	if (attrb->latency_ms > 0)
215 		hid_sensor_set_report_latency(attrb, attrb->latency_ms);
216 
217 	if (atomic_read(&attrb->user_requested_state))
218 		_hid_sensor_power_state(attrb, true);
219 }
220 
hid_sensor_data_rdy_trigger_set_state(struct iio_trigger * trig,bool state)221 static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
222 						bool state)
223 {
224 	return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state);
225 }
226 
hid_sensor_remove_trigger(struct iio_dev * indio_dev,struct hid_sensor_common * attrb)227 void hid_sensor_remove_trigger(struct iio_dev *indio_dev,
228 			       struct hid_sensor_common *attrb)
229 {
230 	if (atomic_read(&attrb->runtime_pm_enable))
231 		pm_runtime_disable(&attrb->pdev->dev);
232 
233 	pm_runtime_set_suspended(&attrb->pdev->dev);
234 	pm_runtime_put_noidle(&attrb->pdev->dev);
235 
236 	cancel_work_sync(&attrb->work);
237 	iio_trigger_unregister(attrb->trigger);
238 	iio_trigger_free(attrb->trigger);
239 	iio_triggered_buffer_cleanup(indio_dev);
240 }
241 EXPORT_SYMBOL(hid_sensor_remove_trigger);
242 
243 static const struct iio_trigger_ops hid_sensor_trigger_ops = {
244 	.set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
245 };
246 
hid_sensor_setup_trigger(struct iio_dev * indio_dev,const char * name,struct hid_sensor_common * attrb)247 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
248 				struct hid_sensor_common *attrb)
249 {
250 	int ret;
251 	struct iio_trigger *trig;
252 
253 	ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
254 					 NULL, NULL);
255 	if (ret) {
256 		dev_err(&indio_dev->dev, "Triggered Buffer Setup Failed\n");
257 		return ret;
258 	}
259 
260 	trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
261 	if (trig == NULL) {
262 		dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
263 		ret = -ENOMEM;
264 		goto error_triggered_buffer_cleanup;
265 	}
266 
267 	trig->dev.parent = indio_dev->dev.parent;
268 	iio_trigger_set_drvdata(trig, attrb);
269 	trig->ops = &hid_sensor_trigger_ops;
270 	ret = iio_trigger_register(trig);
271 
272 	if (ret) {
273 		dev_err(&indio_dev->dev, "Trigger Register Failed\n");
274 		goto error_free_trig;
275 	}
276 	attrb->trigger = trig;
277 	indio_dev->trig = iio_trigger_get(trig);
278 
279 	hid_sensor_setup_batch_mode(indio_dev, attrb);
280 
281 	ret = pm_runtime_set_active(&indio_dev->dev);
282 	if (ret)
283 		goto error_unreg_trigger;
284 
285 	iio_device_set_drvdata(indio_dev, attrb);
286 
287 	INIT_WORK(&attrb->work, hid_sensor_set_power_work);
288 
289 	pm_suspend_ignore_children(&attrb->pdev->dev, true);
290 	/* Default to 3 seconds, but can be changed from sysfs */
291 	pm_runtime_set_autosuspend_delay(&attrb->pdev->dev,
292 					 3000);
293 	return ret;
294 error_unreg_trigger:
295 	iio_trigger_unregister(trig);
296 error_free_trig:
297 	iio_trigger_free(trig);
298 error_triggered_buffer_cleanup:
299 	iio_triggered_buffer_cleanup(indio_dev);
300 	return ret;
301 }
302 EXPORT_SYMBOL(hid_sensor_setup_trigger);
303 
hid_sensor_suspend(struct device * dev)304 static int __maybe_unused hid_sensor_suspend(struct device *dev)
305 {
306 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
307 	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
308 
309 	return _hid_sensor_power_state(attrb, false);
310 }
311 
hid_sensor_resume(struct device * dev)312 static int __maybe_unused hid_sensor_resume(struct device *dev)
313 {
314 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
315 	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
316 	schedule_work(&attrb->work);
317 	return 0;
318 }
319 
hid_sensor_runtime_resume(struct device * dev)320 static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
321 {
322 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
323 	struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
324 	return _hid_sensor_power_state(attrb, true);
325 }
326 
327 const struct dev_pm_ops hid_sensor_pm_ops = {
328 	SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
329 	SET_RUNTIME_PM_OPS(hid_sensor_suspend,
330 			   hid_sensor_runtime_resume, NULL)
331 };
332 EXPORT_SYMBOL(hid_sensor_pm_ops);
333 
334 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
335 MODULE_DESCRIPTION("HID Sensor trigger processing");
336 MODULE_LICENSE("GPL");
337