blob: 54e464e4bb725a21d6687037f5b5489b15f07233 [file] [log] [blame]
srinivas pandruvada45fe6f72012-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>
Srinivas Pandruvadac6399452014-04-19 00:22:00 +010025#include <linux/delay.h>
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010026#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>
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010032#include "../common/hid-sensors/hid-sensor-trigger.h"
33
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010034enum accel_3d_channel {
35 CHANNEL_SCAN_INDEX_X,
36 CHANNEL_SCAN_INDEX_Y,
37 CHANNEL_SCAN_INDEX_Z,
38 ACCEL_3D_CHANNEL_MAX,
39};
40
41struct accel_3d_state {
42 struct hid_sensor_hub_callbacks callbacks;
Alexander Hollere07c6d12012-12-15 12:45:00 +000043 struct hid_sensor_common common_attributes;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010044 struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
45 u32 accel_val[ACCEL_3D_CHANNEL_MAX];
Srinivas Pandruvada73a24982014-04-19 00:22:00 +010046 int scale_pre_decml;
47 int scale_post_decml;
48 int scale_precision;
49 int value_offset;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010050};
51
52static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
53 HID_USAGE_SENSOR_ACCEL_X_AXIS,
54 HID_USAGE_SENSOR_ACCEL_Y_AXIS,
55 HID_USAGE_SENSOR_ACCEL_Z_AXIS
56};
57
58/* Channel definitions */
59static const struct iio_chan_spec accel_3d_channels[] = {
60 {
61 .type = IIO_ACCEL,
62 .modified = 1,
63 .channel2 = IIO_MOD_X,
Srinivas Pandruvadac6399452014-04-19 00:22:00 +010064 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000065 .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),
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010069 .scan_index = CHANNEL_SCAN_INDEX_X,
70 }, {
71 .type = IIO_ACCEL,
72 .modified = 1,
73 .channel2 = IIO_MOD_Y,
Srinivas Pandruvadac6399452014-04-19 00:22:00 +010074 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000075 .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),
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010079 .scan_index = CHANNEL_SCAN_INDEX_Y,
80 }, {
81 .type = IIO_ACCEL,
82 .modified = 1,
83 .channel2 = IIO_MOD_Z,
Srinivas Pandruvadac6399452014-04-19 00:22:00 +010084 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000085 .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),
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010089 .scan_index = CHANNEL_SCAN_INDEX_Z,
90 }
91};
92
93/* Adjust channel real bits based on report descriptor */
94static void accel_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 */
105static int accel_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 accel_3d_state *accel_state = iio_priv(indio_dev);
111 int report_id = -1;
112 u32 address;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100113 int ret_type;
Srinivas Pandruvadac6399452014-04-19 00:22:00 +0100114 s32 poll_value;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100115
116 *val = 0;
117 *val2 = 0;
118 switch (mask) {
119 case 0:
Srinivas Pandruvadac6399452014-04-19 00:22:00 +0100120 poll_value = hid_sensor_read_poll_value(
121 &accel_state->common_attributes);
122 if (poll_value < 0)
123 return -EINVAL;
124
125 hid_sensor_power_state(&accel_state->common_attributes, true);
126 msleep_interruptible(poll_value * 2);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100127 report_id = accel_state->accel[chan->scan_index].report_id;
128 address = accel_3d_addresses[chan->scan_index];
129 if (report_id >= 0)
130 *val = sensor_hub_input_attr_get_raw_value(
Srinivas Pandruvadac6399452014-04-19 00:22:00 +0100131 accel_state->common_attributes.hsdev,
132 HID_USAGE_SENSOR_ACCEL_3D, address,
133 report_id);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100134 else {
135 *val = 0;
Srinivas Pandruvadac6399452014-04-19 00:22:00 +0100136 hid_sensor_power_state(&accel_state->common_attributes,
137 false);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100138 return -EINVAL;
139 }
Srinivas Pandruvadac6399452014-04-19 00:22:00 +0100140 hid_sensor_power_state(&accel_state->common_attributes, false);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100141 ret_type = IIO_VAL_INT;
142 break;
143 case IIO_CHAN_INFO_SCALE:
Srinivas Pandruvada73a24982014-04-19 00:22:00 +0100144 *val = accel_state->scale_pre_decml;
145 *val2 = accel_state->scale_post_decml;
146 ret_type = accel_state->scale_precision;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100147 break;
148 case IIO_CHAN_INFO_OFFSET:
Srinivas Pandruvada73a24982014-04-19 00:22:00 +0100149 *val = accel_state->value_offset;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100150 ret_type = IIO_VAL_INT;
151 break;
152 case IIO_CHAN_INFO_SAMP_FREQ:
Sachin Kamat2f084c12014-03-07 07:44:00 +0000153 ret_type = hid_sensor_read_samp_freq_value(
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100154 &accel_state->common_attributes, val, val2);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100155 break;
156 case IIO_CHAN_INFO_HYSTERESIS:
Sachin Kamat2f084c12014-03-07 07:44:00 +0000157 ret_type = hid_sensor_read_raw_hyst_value(
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100158 &accel_state->common_attributes, val, val2);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100159 break;
160 default:
161 ret_type = -EINVAL;
162 break;
163 }
164
165 return ret_type;
166}
167
168/* Channel write_raw handler */
169static int accel_3d_write_raw(struct iio_dev *indio_dev,
170 struct iio_chan_spec const *chan,
171 int val,
172 int val2,
173 long mask)
174{
175 struct accel_3d_state *accel_state = iio_priv(indio_dev);
176 int ret = 0;
177
178 switch (mask) {
179 case IIO_CHAN_INFO_SAMP_FREQ:
180 ret = hid_sensor_write_samp_freq_value(
181 &accel_state->common_attributes, val, val2);
182 break;
183 case IIO_CHAN_INFO_HYSTERESIS:
184 ret = hid_sensor_write_raw_hyst_value(
185 &accel_state->common_attributes, val, val2);
186 break;
187 default:
188 ret = -EINVAL;
189 }
190
191 return ret;
192}
193
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100194static const struct iio_info accel_3d_info = {
195 .driver_module = THIS_MODULE,
196 .read_raw = &accel_3d_read_raw,
197 .write_raw = &accel_3d_write_raw,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100198};
199
200/* Function to push data to buffer */
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100201static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
202 int len)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100203{
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100204 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100205 iio_push_to_buffers(indio_dev, data);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100206}
207
208/* Callback handler to send event after all samples are received and captured */
209static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
210 unsigned usage_id,
211 void *priv)
212{
213 struct iio_dev *indio_dev = platform_get_drvdata(priv);
214 struct accel_3d_state *accel_state = iio_priv(indio_dev);
215
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100216 dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n");
217 if (atomic_read(&accel_state->common_attributes.data_ready))
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100218 hid_sensor_push_data(indio_dev,
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100219 accel_state->accel_val,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100220 sizeof(accel_state->accel_val));
221
222 return 0;
223}
224
225/* Capture samples in local storage */
226static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
227 unsigned usage_id,
228 size_t raw_len, char *raw_data,
229 void *priv)
230{
231 struct iio_dev *indio_dev = platform_get_drvdata(priv);
232 struct accel_3d_state *accel_state = iio_priv(indio_dev);
233 int offset;
234 int ret = -EINVAL;
235
236 switch (usage_id) {
237 case HID_USAGE_SENSOR_ACCEL_X_AXIS:
238 case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
239 case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
240 offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
241 accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
242 *(u32 *)raw_data;
243 ret = 0;
244 break;
245 default:
246 break;
247 }
248
249 return ret;
250}
251
252/* Parse report which is specific to an usage id*/
253static int accel_3d_parse_report(struct platform_device *pdev,
254 struct hid_sensor_hub_device *hsdev,
255 struct iio_chan_spec *channels,
256 unsigned usage_id,
257 struct accel_3d_state *st)
258{
259 int ret;
260 int i;
261
262 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
263 ret = sensor_hub_input_get_attribute_info(hsdev,
264 HID_INPUT_REPORT,
265 usage_id,
266 HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
267 &st->accel[CHANNEL_SCAN_INDEX_X + i]);
268 if (ret < 0)
269 break;
270 accel_3d_adjust_channel_bit_mask(channels,
271 CHANNEL_SCAN_INDEX_X + i,
272 st->accel[CHANNEL_SCAN_INDEX_X + i].size);
273 }
274 dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
275 st->accel[0].index,
276 st->accel[0].report_id,
277 st->accel[1].index, st->accel[1].report_id,
278 st->accel[2].index, st->accel[2].report_id);
279
Srinivas Pandruvada73a24982014-04-19 00:22:00 +0100280 st->scale_precision = hid_sensor_format_scale(
281 HID_USAGE_SENSOR_ACCEL_3D,
282 &st->accel[CHANNEL_SCAN_INDEX_X],
283 &st->scale_pre_decml, &st->scale_post_decml);
284
Srinivas Pandruvada64528d02013-10-30 22:48:00 +0000285 /* Set Sensitivity field ids, when there is no individual modifier */
286 if (st->common_attributes.sensitivity.index < 0) {
287 sensor_hub_input_get_attribute_info(hsdev,
288 HID_FEATURE_REPORT, usage_id,
289 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
290 HID_USAGE_SENSOR_DATA_ACCELERATION,
291 &st->common_attributes.sensitivity);
292 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
293 st->common_attributes.sensitivity.index,
294 st->common_attributes.sensitivity.report_id);
295 }
296
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100297 return ret;
298}
299
300/* Function to initialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800301static int hid_accel_3d_probe(struct platform_device *pdev)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100302{
303 int ret = 0;
304 static const char *name = "accel_3d";
305 struct iio_dev *indio_dev;
306 struct accel_3d_state *accel_state;
307 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
308 struct iio_chan_spec *channels;
309
Sachin Kamat6b545ee2013-07-23 07:47:00 +0100310 indio_dev = devm_iio_device_alloc(&pdev->dev,
311 sizeof(struct accel_3d_state));
312 if (indio_dev == NULL)
313 return -ENOMEM;
314
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100315 platform_set_drvdata(pdev, indio_dev);
316
317 accel_state = iio_priv(indio_dev);
318 accel_state->common_attributes.hsdev = hsdev;
319 accel_state->common_attributes.pdev = pdev;
320
321 ret = hid_sensor_parse_common_attributes(hsdev,
322 HID_USAGE_SENSOR_ACCEL_3D,
323 &accel_state->common_attributes);
324 if (ret) {
325 dev_err(&pdev->dev, "failed to setup common attributes\n");
Sachin Kamat6b545ee2013-07-23 07:47:00 +0100326 return ret;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100327 }
328
Axel Lin64ebe952012-10-27 16:03:00 +0100329 channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels),
330 GFP_KERNEL);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100331 if (!channels) {
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100332 dev_err(&pdev->dev, "failed to duplicate channels\n");
Sachin Kamat6b545ee2013-07-23 07:47:00 +0100333 return -ENOMEM;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100334 }
335
336 ret = accel_3d_parse_report(pdev, hsdev, channels,
337 HID_USAGE_SENSOR_ACCEL_3D, accel_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(accel_3d_channels);
345 indio_dev->dev.parent = &pdev->dev;
346 indio_dev->info = &accel_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 }
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100356 atomic_set(&accel_state->common_attributes.data_ready, 0);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100357 ret = hid_sensor_setup_trigger(indio_dev, name,
358 &accel_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 accel_state->callbacks.send_event = accel_3d_proc_event;
371 accel_state->callbacks.capture_sample = accel_3d_capture_sample;
372 accel_state->callbacks.pdev = pdev;
373 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D,
374 &accel_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
382error_iio_unreg:
383 iio_device_unregister(indio_dev);
384error_remove_trigger:
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000385 hid_sensor_remove_trigger(&accel_state->common_attributes);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100386error_unreg_buffer_funcs:
387 iio_triggered_buffer_cleanup(indio_dev);
388error_free_dev_mem:
389 kfree(indio_dev->channels);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100390 return ret;
391}
392
393/* Function to deinitialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800394static int hid_accel_3d_remove(struct platform_device *pdev)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100395{
396 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
397 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000398 struct accel_3d_state *accel_state = iio_priv(indio_dev);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100399
400 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D);
401 iio_device_unregister(indio_dev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000402 hid_sensor_remove_trigger(&accel_state->common_attributes);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100403 iio_triggered_buffer_cleanup(indio_dev);
404 kfree(indio_dev->channels);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100405
406 return 0;
407}
408
Alexander Holler66c721f2013-07-10 09:31:00 +0100409static struct platform_device_id hid_accel_3d_ids[] = {
410 {
411 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
412 .name = "HID-SENSOR-200073",
413 },
414 { /* sentinel */ }
415};
416MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
417
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100418static struct platform_driver hid_accel_3d_platform_driver = {
Alexander Holler66c721f2013-07-10 09:31:00 +0100419 .id_table = hid_accel_3d_ids,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100420 .driver = {
Alexander Holler66c721f2013-07-10 09:31:00 +0100421 .name = KBUILD_MODNAME,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100422 .owner = THIS_MODULE,
423 },
424 .probe = hid_accel_3d_probe,
425 .remove = hid_accel_3d_remove,
426};
427module_platform_driver(hid_accel_3d_platform_driver);
428
429MODULE_DESCRIPTION("HID Sensor Accel 3D");
430MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
431MODULE_LICENSE("GPL");