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