diff options
Diffstat (limited to 'drivers/iio/orientation/hid-sensor-rotation.c')
-rw-r--r-- | drivers/iio/orientation/hid-sensor-rotation.c | 346 |
1 files changed, 346 insertions, 0 deletions
diff --git a/drivers/iio/orientation/hid-sensor-rotation.c b/drivers/iio/orientation/hid-sensor-rotation.c new file mode 100644 index 000000000000..dccf848e8b0f --- /dev/null +++ b/drivers/iio/orientation/hid-sensor-rotation.c | |||
@@ -0,0 +1,346 @@ | |||
1 | /* | ||
2 | * HID Sensors Driver | ||
3 | * Copyright (c) 2014, 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 | |||
15 | #include <linux/device.h> | ||
16 | #include <linux/platform_device.h> | ||
17 | #include <linux/module.h> | ||
18 | #include <linux/interrupt.h> | ||
19 | #include <linux/irq.h> | ||
20 | #include <linux/slab.h> | ||
21 | #include <linux/hid-sensor-hub.h> | ||
22 | #include <linux/iio/iio.h> | ||
23 | #include <linux/iio/sysfs.h> | ||
24 | #include <linux/iio/buffer.h> | ||
25 | #include <linux/iio/trigger_consumer.h> | ||
26 | #include <linux/iio/triggered_buffer.h> | ||
27 | #include "../common/hid-sensors/hid-sensor-trigger.h" | ||
28 | |||
29 | struct dev_rot_state { | ||
30 | struct hid_sensor_hub_callbacks callbacks; | ||
31 | struct hid_sensor_common common_attributes; | ||
32 | struct hid_sensor_hub_attribute_info quaternion; | ||
33 | u32 sampled_vals[4]; | ||
34 | }; | ||
35 | |||
36 | /* Channel definitions */ | ||
37 | static const struct iio_chan_spec dev_rot_channels[] = { | ||
38 | { | ||
39 | .type = IIO_ROT, | ||
40 | .modified = 1, | ||
41 | .channel2 = IIO_MOD_QUATERNION, | ||
42 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | ||
43 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | | ||
44 | BIT(IIO_CHAN_INFO_HYSTERESIS) | ||
45 | } | ||
46 | }; | ||
47 | |||
48 | /* Adjust channel real bits based on report descriptor */ | ||
49 | static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan, | ||
50 | int size) | ||
51 | { | ||
52 | chan->scan_type.sign = 's'; | ||
53 | /* Real storage bits will change based on the report desc. */ | ||
54 | chan->scan_type.realbits = size * 8; | ||
55 | /* Maximum size of a sample to capture is u32 */ | ||
56 | chan->scan_type.storagebits = sizeof(u32) * 8; | ||
57 | chan->scan_type.repeat = 4; | ||
58 | } | ||
59 | |||
60 | /* Channel read_raw handler */ | ||
61 | static int dev_rot_read_raw(struct iio_dev *indio_dev, | ||
62 | struct iio_chan_spec const *chan, | ||
63 | int size, int *vals, int *val_len, | ||
64 | long mask) | ||
65 | { | ||
66 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | ||
67 | int ret_type; | ||
68 | int i; | ||
69 | |||
70 | vals[0] = 0; | ||
71 | vals[1] = 0; | ||
72 | |||
73 | switch (mask) { | ||
74 | case IIO_CHAN_INFO_RAW: | ||
75 | if (size >= 4) { | ||
76 | for (i = 0; i < 4; ++i) | ||
77 | vals[i] = rot_state->sampled_vals[i]; | ||
78 | ret_type = IIO_VAL_INT_MULTIPLE; | ||
79 | *val_len = 4; | ||
80 | } else | ||
81 | ret_type = -EINVAL; | ||
82 | break; | ||
83 | case IIO_CHAN_INFO_SAMP_FREQ: | ||
84 | ret_type = hid_sensor_read_samp_freq_value( | ||
85 | &rot_state->common_attributes, &vals[0], &vals[1]); | ||
86 | break; | ||
87 | case IIO_CHAN_INFO_HYSTERESIS: | ||
88 | ret_type = hid_sensor_read_raw_hyst_value( | ||
89 | &rot_state->common_attributes, &vals[0], &vals[1]); | ||
90 | break; | ||
91 | default: | ||
92 | ret_type = -EINVAL; | ||
93 | break; | ||
94 | } | ||
95 | |||
96 | return ret_type; | ||
97 | } | ||
98 | |||
99 | /* Channel write_raw handler */ | ||
100 | static int dev_rot_write_raw(struct iio_dev *indio_dev, | ||
101 | struct iio_chan_spec const *chan, | ||
102 | int val, | ||
103 | int val2, | ||
104 | long mask) | ||
105 | { | ||
106 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | ||
107 | int ret; | ||
108 | |||
109 | switch (mask) { | ||
110 | case IIO_CHAN_INFO_SAMP_FREQ: | ||
111 | ret = hid_sensor_write_samp_freq_value( | ||
112 | &rot_state->common_attributes, val, val2); | ||
113 | break; | ||
114 | case IIO_CHAN_INFO_HYSTERESIS: | ||
115 | ret = hid_sensor_write_raw_hyst_value( | ||
116 | &rot_state->common_attributes, val, val2); | ||
117 | break; | ||
118 | default: | ||
119 | ret = -EINVAL; | ||
120 | } | ||
121 | |||
122 | return ret; | ||
123 | } | ||
124 | |||
125 | static const struct iio_info dev_rot_info = { | ||
126 | .driver_module = THIS_MODULE, | ||
127 | .read_raw_multi = &dev_rot_read_raw, | ||
128 | .write_raw = &dev_rot_write_raw, | ||
129 | }; | ||
130 | |||
131 | /* Function to push data to buffer */ | ||
132 | static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len) | ||
133 | { | ||
134 | dev_dbg(&indio_dev->dev, "hid_sensor_push_data >>\n"); | ||
135 | iio_push_to_buffers(indio_dev, (u8 *)data); | ||
136 | dev_dbg(&indio_dev->dev, "hid_sensor_push_data <<\n"); | ||
137 | |||
138 | } | ||
139 | |||
140 | /* Callback handler to send event after all samples are received and captured */ | ||
141 | static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev, | ||
142 | unsigned usage_id, | ||
143 | void *priv) | ||
144 | { | ||
145 | struct iio_dev *indio_dev = platform_get_drvdata(priv); | ||
146 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | ||
147 | |||
148 | dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n"); | ||
149 | if (atomic_read(&rot_state->common_attributes.data_ready)) | ||
150 | hid_sensor_push_data(indio_dev, | ||
151 | (u8 *)rot_state->sampled_vals, | ||
152 | sizeof(rot_state->sampled_vals)); | ||
153 | |||
154 | return 0; | ||
155 | } | ||
156 | |||
157 | /* Capture samples in local storage */ | ||
158 | static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev, | ||
159 | unsigned usage_id, | ||
160 | size_t raw_len, char *raw_data, | ||
161 | void *priv) | ||
162 | { | ||
163 | struct iio_dev *indio_dev = platform_get_drvdata(priv); | ||
164 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | ||
165 | |||
166 | if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) { | ||
167 | memcpy(rot_state->sampled_vals, raw_data, | ||
168 | sizeof(rot_state->sampled_vals)); | ||
169 | dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len, | ||
170 | sizeof(rot_state->sampled_vals)); | ||
171 | } | ||
172 | |||
173 | return 0; | ||
174 | } | ||
175 | |||
176 | /* Parse report which is specific to an usage id*/ | ||
177 | static int dev_rot_parse_report(struct platform_device *pdev, | ||
178 | struct hid_sensor_hub_device *hsdev, | ||
179 | struct iio_chan_spec *channels, | ||
180 | unsigned usage_id, | ||
181 | struct dev_rot_state *st) | ||
182 | { | ||
183 | int ret; | ||
184 | |||
185 | ret = sensor_hub_input_get_attribute_info(hsdev, | ||
186 | HID_INPUT_REPORT, | ||
187 | usage_id, | ||
188 | HID_USAGE_SENSOR_ORIENT_QUATERNION, | ||
189 | &st->quaternion); | ||
190 | if (ret) | ||
191 | return ret; | ||
192 | |||
193 | dev_rot_adjust_channel_bit_mask(&channels[0], | ||
194 | st->quaternion.size / 4); | ||
195 | |||
196 | dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index, | ||
197 | st->quaternion.report_id); | ||
198 | |||
199 | dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n", | ||
200 | st->quaternion.size); | ||
201 | |||
202 | /* Set Sensitivity field ids, when there is no individual modifier */ | ||
203 | if (st->common_attributes.sensitivity.index < 0) { | ||
204 | sensor_hub_input_get_attribute_info(hsdev, | ||
205 | HID_FEATURE_REPORT, usage_id, | ||
206 | HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | | ||
207 | HID_USAGE_SENSOR_DATA_ORIENTATION, | ||
208 | &st->common_attributes.sensitivity); | ||
209 | dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", | ||
210 | st->common_attributes.sensitivity.index, | ||
211 | st->common_attributes.sensitivity.report_id); | ||
212 | } | ||
213 | |||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | /* Function to initialize the processing for usage id */ | ||
218 | static int hid_dev_rot_probe(struct platform_device *pdev) | ||
219 | { | ||
220 | int ret; | ||
221 | static char *name = "dev_rotation"; | ||
222 | struct iio_dev *indio_dev; | ||
223 | struct dev_rot_state *rot_state; | ||
224 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; | ||
225 | struct iio_chan_spec *channels; | ||
226 | |||
227 | indio_dev = devm_iio_device_alloc(&pdev->dev, | ||
228 | sizeof(struct dev_rot_state)); | ||
229 | if (indio_dev == NULL) | ||
230 | return -ENOMEM; | ||
231 | |||
232 | platform_set_drvdata(pdev, indio_dev); | ||
233 | |||
234 | rot_state = iio_priv(indio_dev); | ||
235 | rot_state->common_attributes.hsdev = hsdev; | ||
236 | rot_state->common_attributes.pdev = pdev; | ||
237 | |||
238 | ret = hid_sensor_parse_common_attributes(hsdev, | ||
239 | HID_USAGE_SENSOR_DEVICE_ORIENTATION, | ||
240 | &rot_state->common_attributes); | ||
241 | if (ret) { | ||
242 | dev_err(&pdev->dev, "failed to setup common attributes\n"); | ||
243 | return ret; | ||
244 | } | ||
245 | |||
246 | channels = devm_kmemdup(&pdev->dev, dev_rot_channels, | ||
247 | sizeof(dev_rot_channels), GFP_KERNEL); | ||
248 | if (!channels) { | ||
249 | dev_err(&pdev->dev, "failed to duplicate channels\n"); | ||
250 | return -ENOMEM; | ||
251 | } | ||
252 | |||
253 | ret = dev_rot_parse_report(pdev, hsdev, channels, | ||
254 | HID_USAGE_SENSOR_DEVICE_ORIENTATION, rot_state); | ||
255 | if (ret) { | ||
256 | dev_err(&pdev->dev, "failed to setup attributes\n"); | ||
257 | return ret; | ||
258 | } | ||
259 | |||
260 | indio_dev->channels = channels; | ||
261 | indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels); | ||
262 | indio_dev->dev.parent = &pdev->dev; | ||
263 | indio_dev->info = &dev_rot_info; | ||
264 | indio_dev->name = name; | ||
265 | indio_dev->modes = INDIO_DIRECT_MODE; | ||
266 | |||
267 | ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, | ||
268 | NULL, NULL); | ||
269 | if (ret) { | ||
270 | dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); | ||
271 | return ret; | ||
272 | } | ||
273 | atomic_set(&rot_state->common_attributes.data_ready, 0); | ||
274 | ret = hid_sensor_setup_trigger(indio_dev, name, | ||
275 | &rot_state->common_attributes); | ||
276 | if (ret) { | ||
277 | dev_err(&pdev->dev, "trigger setup failed\n"); | ||
278 | goto error_unreg_buffer_funcs; | ||
279 | } | ||
280 | |||
281 | ret = iio_device_register(indio_dev); | ||
282 | if (ret) { | ||
283 | dev_err(&pdev->dev, "device register failed\n"); | ||
284 | goto error_remove_trigger; | ||
285 | } | ||
286 | |||
287 | rot_state->callbacks.send_event = dev_rot_proc_event; | ||
288 | rot_state->callbacks.capture_sample = dev_rot_capture_sample; | ||
289 | rot_state->callbacks.pdev = pdev; | ||
290 | ret = sensor_hub_register_callback(hsdev, | ||
291 | HID_USAGE_SENSOR_DEVICE_ORIENTATION, | ||
292 | &rot_state->callbacks); | ||
293 | if (ret) { | ||
294 | dev_err(&pdev->dev, "callback reg failed\n"); | ||
295 | goto error_iio_unreg; | ||
296 | } | ||
297 | |||
298 | return 0; | ||
299 | |||
300 | error_iio_unreg: | ||
301 | iio_device_unregister(indio_dev); | ||
302 | error_remove_trigger: | ||
303 | hid_sensor_remove_trigger(&rot_state->common_attributes); | ||
304 | error_unreg_buffer_funcs: | ||
305 | iio_triggered_buffer_cleanup(indio_dev); | ||
306 | return ret; | ||
307 | } | ||
308 | |||
309 | /* Function to deinitialize the processing for usage id */ | ||
310 | static int hid_dev_rot_remove(struct platform_device *pdev) | ||
311 | { | ||
312 | struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; | ||
313 | struct iio_dev *indio_dev = platform_get_drvdata(pdev); | ||
314 | struct dev_rot_state *rot_state = iio_priv(indio_dev); | ||
315 | |||
316 | sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_DEVICE_ORIENTATION); | ||
317 | iio_device_unregister(indio_dev); | ||
318 | hid_sensor_remove_trigger(&rot_state->common_attributes); | ||
319 | iio_triggered_buffer_cleanup(indio_dev); | ||
320 | |||
321 | return 0; | ||
322 | } | ||
323 | |||
324 | static struct platform_device_id hid_dev_rot_ids[] = { | ||
325 | { | ||
326 | /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ | ||
327 | .name = "HID-SENSOR-20008a", | ||
328 | }, | ||
329 | { /* sentinel */ } | ||
330 | }; | ||
331 | MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids); | ||
332 | |||
333 | static struct platform_driver hid_dev_rot_platform_driver = { | ||
334 | .id_table = hid_dev_rot_ids, | ||
335 | .driver = { | ||
336 | .name = KBUILD_MODNAME, | ||
337 | .owner = THIS_MODULE, | ||
338 | }, | ||
339 | .probe = hid_dev_rot_probe, | ||
340 | .remove = hid_dev_rot_remove, | ||
341 | }; | ||
342 | module_platform_driver(hid_dev_rot_platform_driver); | ||
343 | |||
344 | MODULE_DESCRIPTION("HID Sensor Device Rotation"); | ||
345 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); | ||
346 | MODULE_LICENSE("GPL"); | ||