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