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/hid-sensor-hub.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/sysfs.h>
29 #include "hid-sensor-trigger.h"
30 
31 static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
32 						bool state)
33 {
34 	struct hid_sensor_common *st = iio_trigger_get_drvdata(trig);
35 	int state_val;
36 	int report_val;
37 
38 	if (state) {
39 		if (sensor_hub_device_open(st->hsdev))
40 			return -EIO;
41 		state_val = hid_sensor_get_usage_index(st->hsdev,
42 			st->power_state.report_id,
43 			st->power_state.index,
44 			HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM);
45 		report_val = hid_sensor_get_usage_index(st->hsdev,
46 			st->report_state.report_id,
47 			st->report_state.index,
48 			HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM);
49 	} else {
50 		sensor_hub_device_close(st->hsdev);
51 		state_val = hid_sensor_get_usage_index(st->hsdev,
52 			st->power_state.report_id,
53 			st->power_state.index,
54 			HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM);
55 		report_val = hid_sensor_get_usage_index(st->hsdev,
56 			st->report_state.report_id,
57 			st->report_state.index,
58 			HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM);
59 	}
60 	st->data_ready = state;
61 
62 	if (state_val >= 0) {
63 		state_val += st->power_state.logical_minimum;
64 		sensor_hub_set_feature(st->hsdev, st->power_state.report_id,
65 					st->power_state.index,
66 					(s32)state_val);
67 	}
68 
69 	if (report_val >= 0) {
70 		report_val += st->report_state.logical_minimum;
71 		sensor_hub_set_feature(st->hsdev, st->report_state.report_id,
72 					st->report_state.index,
73 					(s32)report_val);
74 	}
75 
76 	return 0;
77 }
78 
79 void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
80 {
81 	iio_trigger_unregister(attrb->trigger);
82 	iio_trigger_free(attrb->trigger);
83 }
84 EXPORT_SYMBOL(hid_sensor_remove_trigger);
85 
86 static const struct iio_trigger_ops hid_sensor_trigger_ops = {
87 	.owner = THIS_MODULE,
88 	.set_trigger_state = &hid_sensor_data_rdy_trigger_set_state,
89 };
90 
91 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
92 				struct hid_sensor_common *attrb)
93 {
94 	int ret;
95 	struct iio_trigger *trig;
96 
97 	trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id);
98 	if (trig == NULL) {
99 		dev_err(&indio_dev->dev, "Trigger Allocate Failed\n");
100 		ret = -ENOMEM;
101 		goto error_ret;
102 	}
103 
104 	trig->dev.parent = indio_dev->dev.parent;
105 	iio_trigger_set_drvdata(trig, attrb);
106 	trig->ops = &hid_sensor_trigger_ops;
107 	ret = iio_trigger_register(trig);
108 
109 	if (ret) {
110 		dev_err(&indio_dev->dev, "Trigger Register Failed\n");
111 		goto error_free_trig;
112 	}
113 	indio_dev->trig = attrb->trigger = trig;
114 
115 	return ret;
116 
117 error_free_trig:
118 	iio_trigger_free(trig);
119 error_ret:
120 	return ret;
121 }
122 EXPORT_SYMBOL(hid_sensor_setup_trigger);
123 
124 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
125 MODULE_DESCRIPTION("HID Sensor trigger processing");
126 MODULE_LICENSE("GPL");
127