]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/iio/gyro/hid-sensor-gyro-3d.c
iio: hid-sensors: Add API to power on/off
[mirror_ubuntu-bionic-kernel.git] / drivers / iio / gyro / hid-sensor-gyro-3d.c
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/sysfs.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
31 #include "../common/hid-sensors/hid-sensor-trigger.h"
32
33 enum gyro_3d_channel {
34 CHANNEL_SCAN_INDEX_X,
35 CHANNEL_SCAN_INDEX_Y,
36 CHANNEL_SCAN_INDEX_Z,
37 GYRO_3D_CHANNEL_MAX,
38 };
39
40 struct gyro_3d_state {
41 struct hid_sensor_hub_callbacks callbacks;
42 struct hid_sensor_common common_attributes;
43 struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX];
44 u32 gyro_val[GYRO_3D_CHANNEL_MAX];
45 int scale_pre_decml;
46 int scale_post_decml;
47 int scale_precision;
48 int value_offset;
49 };
50
51 static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = {
52 HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS,
53 HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS,
54 HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS
55 };
56
57 /* Channel definitions */
58 static const struct iio_chan_spec gyro_3d_channels[] = {
59 {
60 .type = IIO_ANGL_VEL,
61 .modified = 1,
62 .channel2 = IIO_MOD_X,
63 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
64 BIT(IIO_CHAN_INFO_SCALE) |
65 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
66 BIT(IIO_CHAN_INFO_HYSTERESIS),
67 .scan_index = CHANNEL_SCAN_INDEX_X,
68 }, {
69 .type = IIO_ANGL_VEL,
70 .modified = 1,
71 .channel2 = IIO_MOD_Y,
72 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
73 BIT(IIO_CHAN_INFO_SCALE) |
74 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
75 BIT(IIO_CHAN_INFO_HYSTERESIS),
76 .scan_index = CHANNEL_SCAN_INDEX_Y,
77 }, {
78 .type = IIO_ANGL_VEL,
79 .modified = 1,
80 .channel2 = IIO_MOD_Z,
81 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) |
82 BIT(IIO_CHAN_INFO_SCALE) |
83 BIT(IIO_CHAN_INFO_SAMP_FREQ) |
84 BIT(IIO_CHAN_INFO_HYSTERESIS),
85 .scan_index = CHANNEL_SCAN_INDEX_Z,
86 }
87 };
88
89 /* Adjust channel real bits based on report descriptor */
90 static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
91 int channel, int size)
92 {
93 channels[channel].scan_type.sign = 's';
94 /* Real storage bits will change based on the report desc. */
95 channels[channel].scan_type.realbits = size * 8;
96 /* Maximum size of a sample to capture is u32 */
97 channels[channel].scan_type.storagebits = sizeof(u32) * 8;
98 }
99
100 /* Channel read_raw handler */
101 static int gyro_3d_read_raw(struct iio_dev *indio_dev,
102 struct iio_chan_spec const *chan,
103 int *val, int *val2,
104 long mask)
105 {
106 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
107 int report_id = -1;
108 u32 address;
109 int ret;
110 int ret_type;
111
112 *val = 0;
113 *val2 = 0;
114 switch (mask) {
115 case 0:
116 report_id = gyro_state->gyro[chan->scan_index].report_id;
117 address = gyro_3d_addresses[chan->scan_index];
118 if (report_id >= 0)
119 *val = sensor_hub_input_attr_get_raw_value(
120 gyro_state->common_attributes.hsdev,
121 HID_USAGE_SENSOR_GYRO_3D, address,
122 report_id);
123 else {
124 *val = 0;
125 return -EINVAL;
126 }
127 ret_type = IIO_VAL_INT;
128 break;
129 case IIO_CHAN_INFO_SCALE:
130 *val = gyro_state->scale_pre_decml;
131 *val2 = gyro_state->scale_post_decml;
132 ret_type = gyro_state->scale_precision;
133 break;
134 case IIO_CHAN_INFO_OFFSET:
135 *val = gyro_state->value_offset;
136 ret_type = IIO_VAL_INT;
137 break;
138 case IIO_CHAN_INFO_SAMP_FREQ:
139 ret = hid_sensor_read_samp_freq_value(
140 &gyro_state->common_attributes, val, val2);
141 ret_type = IIO_VAL_INT_PLUS_MICRO;
142 break;
143 case IIO_CHAN_INFO_HYSTERESIS:
144 ret = hid_sensor_read_raw_hyst_value(
145 &gyro_state->common_attributes, val, val2);
146 ret_type = IIO_VAL_INT_PLUS_MICRO;
147 break;
148 default:
149 ret_type = -EINVAL;
150 break;
151 }
152
153 return ret_type;
154 }
155
156 /* Channel write_raw handler */
157 static int gyro_3d_write_raw(struct iio_dev *indio_dev,
158 struct iio_chan_spec const *chan,
159 int val,
160 int val2,
161 long mask)
162 {
163 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
164 int ret = 0;
165
166 switch (mask) {
167 case IIO_CHAN_INFO_SAMP_FREQ:
168 ret = hid_sensor_write_samp_freq_value(
169 &gyro_state->common_attributes, val, val2);
170 break;
171 case IIO_CHAN_INFO_HYSTERESIS:
172 ret = hid_sensor_write_raw_hyst_value(
173 &gyro_state->common_attributes, val, val2);
174 break;
175 default:
176 ret = -EINVAL;
177 }
178
179 return ret;
180 }
181
182 static const struct iio_info gyro_3d_info = {
183 .driver_module = THIS_MODULE,
184 .read_raw = &gyro_3d_read_raw,
185 .write_raw = &gyro_3d_write_raw,
186 };
187
188 /* Function to push data to buffer */
189 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
190 int len)
191 {
192 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
193 iio_push_to_buffers(indio_dev, data);
194 }
195
196 /* Callback handler to send event after all samples are received and captured */
197 static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev,
198 unsigned usage_id,
199 void *priv)
200 {
201 struct iio_dev *indio_dev = platform_get_drvdata(priv);
202 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
203
204 dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n");
205 if (atomic_read(&gyro_state->common_attributes.data_ready))
206 hid_sensor_push_data(indio_dev,
207 gyro_state->gyro_val,
208 sizeof(gyro_state->gyro_val));
209
210 return 0;
211 }
212
213 /* Capture samples in local storage */
214 static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
215 unsigned usage_id,
216 size_t raw_len, char *raw_data,
217 void *priv)
218 {
219 struct iio_dev *indio_dev = platform_get_drvdata(priv);
220 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
221 int offset;
222 int ret = -EINVAL;
223
224 switch (usage_id) {
225 case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS:
226 case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS:
227 case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS:
228 offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS;
229 gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] =
230 *(u32 *)raw_data;
231 ret = 0;
232 break;
233 default:
234 break;
235 }
236
237 return ret;
238 }
239
240 /* Parse report which is specific to an usage id*/
241 static int gyro_3d_parse_report(struct platform_device *pdev,
242 struct hid_sensor_hub_device *hsdev,
243 struct iio_chan_spec *channels,
244 unsigned usage_id,
245 struct gyro_3d_state *st)
246 {
247 int ret;
248 int i;
249
250 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
251 ret = sensor_hub_input_get_attribute_info(hsdev,
252 HID_INPUT_REPORT,
253 usage_id,
254 HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i,
255 &st->gyro[CHANNEL_SCAN_INDEX_X + i]);
256 if (ret < 0)
257 break;
258 gyro_3d_adjust_channel_bit_mask(channels,
259 CHANNEL_SCAN_INDEX_X + i,
260 st->gyro[CHANNEL_SCAN_INDEX_X + i].size);
261 }
262 dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n",
263 st->gyro[0].index,
264 st->gyro[0].report_id,
265 st->gyro[1].index, st->gyro[1].report_id,
266 st->gyro[2].index, st->gyro[2].report_id);
267
268 st->scale_precision = hid_sensor_format_scale(
269 HID_USAGE_SENSOR_GYRO_3D,
270 &st->gyro[CHANNEL_SCAN_INDEX_X],
271 &st->scale_pre_decml, &st->scale_post_decml);
272
273 /* Set Sensitivity field ids, when there is no individual modifier */
274 if (st->common_attributes.sensitivity.index < 0) {
275 sensor_hub_input_get_attribute_info(hsdev,
276 HID_FEATURE_REPORT, usage_id,
277 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
278 HID_USAGE_SENSOR_DATA_ANGL_VELOCITY,
279 &st->common_attributes.sensitivity);
280 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
281 st->common_attributes.sensitivity.index,
282 st->common_attributes.sensitivity.report_id);
283 }
284 return ret;
285 }
286
287 /* Function to initialize the processing for usage id */
288 static int hid_gyro_3d_probe(struct platform_device *pdev)
289 {
290 int ret = 0;
291 static const char *name = "gyro_3d";
292 struct iio_dev *indio_dev;
293 struct gyro_3d_state *gyro_state;
294 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
295 struct iio_chan_spec *channels;
296
297 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state));
298 if (!indio_dev)
299 return -ENOMEM;
300 platform_set_drvdata(pdev, indio_dev);
301
302 gyro_state = iio_priv(indio_dev);
303 gyro_state->common_attributes.hsdev = hsdev;
304 gyro_state->common_attributes.pdev = pdev;
305
306 ret = hid_sensor_parse_common_attributes(hsdev,
307 HID_USAGE_SENSOR_GYRO_3D,
308 &gyro_state->common_attributes);
309 if (ret) {
310 dev_err(&pdev->dev, "failed to setup common attributes\n");
311 return ret;
312 }
313
314 channels = kmemdup(gyro_3d_channels, sizeof(gyro_3d_channels),
315 GFP_KERNEL);
316 if (!channels) {
317 dev_err(&pdev->dev, "failed to duplicate channels\n");
318 return -ENOMEM;
319 }
320
321 ret = gyro_3d_parse_report(pdev, hsdev, channels,
322 HID_USAGE_SENSOR_GYRO_3D, gyro_state);
323 if (ret) {
324 dev_err(&pdev->dev, "failed to setup attributes\n");
325 goto error_free_dev_mem;
326 }
327
328 indio_dev->channels = channels;
329 indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels);
330 indio_dev->dev.parent = &pdev->dev;
331 indio_dev->info = &gyro_3d_info;
332 indio_dev->name = name;
333 indio_dev->modes = INDIO_DIRECT_MODE;
334
335 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
336 NULL, NULL);
337 if (ret) {
338 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
339 goto error_free_dev_mem;
340 }
341 atomic_set(&gyro_state->common_attributes.data_ready, 0);
342 ret = hid_sensor_setup_trigger(indio_dev, name,
343 &gyro_state->common_attributes);
344 if (ret < 0) {
345 dev_err(&pdev->dev, "trigger setup failed\n");
346 goto error_unreg_buffer_funcs;
347 }
348
349 ret = iio_device_register(indio_dev);
350 if (ret) {
351 dev_err(&pdev->dev, "device register failed\n");
352 goto error_remove_trigger;
353 }
354
355 gyro_state->callbacks.send_event = gyro_3d_proc_event;
356 gyro_state->callbacks.capture_sample = gyro_3d_capture_sample;
357 gyro_state->callbacks.pdev = pdev;
358 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D,
359 &gyro_state->callbacks);
360 if (ret < 0) {
361 dev_err(&pdev->dev, "callback reg failed\n");
362 goto error_iio_unreg;
363 }
364
365 return ret;
366
367 error_iio_unreg:
368 iio_device_unregister(indio_dev);
369 error_remove_trigger:
370 hid_sensor_remove_trigger(&gyro_state->common_attributes);
371 error_unreg_buffer_funcs:
372 iio_triggered_buffer_cleanup(indio_dev);
373 error_free_dev_mem:
374 kfree(indio_dev->channels);
375 return ret;
376 }
377
378 /* Function to deinitialize the processing for usage id */
379 static int hid_gyro_3d_remove(struct platform_device *pdev)
380 {
381 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
382 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
383 struct gyro_3d_state *gyro_state = iio_priv(indio_dev);
384
385 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D);
386 iio_device_unregister(indio_dev);
387 hid_sensor_remove_trigger(&gyro_state->common_attributes);
388 iio_triggered_buffer_cleanup(indio_dev);
389 kfree(indio_dev->channels);
390
391 return 0;
392 }
393
394 static struct platform_device_id hid_gyro_3d_ids[] = {
395 {
396 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
397 .name = "HID-SENSOR-200076",
398 },
399 { /* sentinel */ }
400 };
401 MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids);
402
403 static struct platform_driver hid_gyro_3d_platform_driver = {
404 .id_table = hid_gyro_3d_ids,
405 .driver = {
406 .name = KBUILD_MODNAME,
407 .owner = THIS_MODULE,
408 },
409 .probe = hid_gyro_3d_probe,
410 .remove = hid_gyro_3d_remove,
411 };
412 module_platform_driver(hid_gyro_3d_platform_driver);
413
414 MODULE_DESCRIPTION("HID Sensor Gyroscope 3D");
415 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
416 MODULE_LICENSE("GPL");