blob: 3dcdbad654565f2c5d590c99b0c81800afd9702f [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>
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 pandruvada45fe6f72012-09-05 13:56:00 +010031#include "../common/hid-sensors/hid-sensor-trigger.h"
32
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010033enum accel_3d_channel {
34 CHANNEL_SCAN_INDEX_X,
35 CHANNEL_SCAN_INDEX_Y,
36 CHANNEL_SCAN_INDEX_Z,
37 ACCEL_3D_CHANNEL_MAX,
38};
39
40struct accel_3d_state {
41 struct hid_sensor_hub_callbacks callbacks;
Alexander Hollere07c6d12012-12-15 12:45:00 +000042 struct hid_sensor_common common_attributes;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +010043 struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
44 u32 accel_val[ACCEL_3D_CHANNEL_MAX];
45};
46
47static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
48 HID_USAGE_SENSOR_ACCEL_X_AXIS,
49 HID_USAGE_SENSOR_ACCEL_Y_AXIS,
50 HID_USAGE_SENSOR_ACCEL_Z_AXIS
51};
52
53/* Channel definitions */
54static const struct iio_chan_spec accel_3d_channels[] = {
55 {
56 .type = IIO_ACCEL,
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 pandruvada45fe6f72012-09-05 13:56:00 +010063 .scan_index = CHANNEL_SCAN_INDEX_X,
64 }, {
65 .type = IIO_ACCEL,
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 pandruvada45fe6f72012-09-05 13:56:00 +010072 .scan_index = CHANNEL_SCAN_INDEX_Y,
73 }, {
74 .type = IIO_ACCEL,
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 pandruvada45fe6f72012-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 accel_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 accel_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 accel_3d_state *accel_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 = accel_state->accel[chan->scan_index].report_id;
113 address = accel_3d_addresses[chan->scan_index];
114 if (report_id >= 0)
115 *val = sensor_hub_input_attr_get_raw_value(
116 accel_state->common_attributes.hsdev,
117 HID_USAGE_SENSOR_ACCEL_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 = accel_state->accel[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 accel_state->accel[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 &accel_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 &accel_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 accel_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 accel_3d_state *accel_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 &accel_state->common_attributes, val, val2);
166 break;
167 case IIO_CHAN_INFO_HYSTERESIS:
168 ret = hid_sensor_write_raw_hyst_value(
169 &accel_state->common_attributes, val, val2);
170 break;
171 default:
172 ret = -EINVAL;
173 }
174
175 return ret;
176}
177
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100178static const struct iio_info accel_3d_info = {
179 .driver_module = THIS_MODULE,
180 .read_raw = &accel_3d_read_raw,
181 .write_raw = &accel_3d_write_raw,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100182};
183
184/* Function to push data to buffer */
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100185static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
186 int len)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100187{
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100188 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100189 iio_push_to_buffers(indio_dev, data);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100190}
191
192/* Callback handler to send event after all samples are received and captured */
193static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
194 unsigned usage_id,
195 void *priv)
196{
197 struct iio_dev *indio_dev = platform_get_drvdata(priv);
198 struct accel_3d_state *accel_state = iio_priv(indio_dev);
199
200 dev_dbg(&indio_dev->dev, "accel_3d_proc_event [%d]\n",
201 accel_state->common_attributes.data_ready);
202 if (accel_state->common_attributes.data_ready)
203 hid_sensor_push_data(indio_dev,
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100204 accel_state->accel_val,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100205 sizeof(accel_state->accel_val));
206
207 return 0;
208}
209
210/* Capture samples in local storage */
211static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
212 unsigned usage_id,
213 size_t raw_len, char *raw_data,
214 void *priv)
215{
216 struct iio_dev *indio_dev = platform_get_drvdata(priv);
217 struct accel_3d_state *accel_state = iio_priv(indio_dev);
218 int offset;
219 int ret = -EINVAL;
220
221 switch (usage_id) {
222 case HID_USAGE_SENSOR_ACCEL_X_AXIS:
223 case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
224 case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
225 offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
226 accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
227 *(u32 *)raw_data;
228 ret = 0;
229 break;
230 default:
231 break;
232 }
233
234 return ret;
235}
236
237/* Parse report which is specific to an usage id*/
238static int accel_3d_parse_report(struct platform_device *pdev,
239 struct hid_sensor_hub_device *hsdev,
240 struct iio_chan_spec *channels,
241 unsigned usage_id,
242 struct accel_3d_state *st)
243{
244 int ret;
245 int i;
246
247 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
248 ret = sensor_hub_input_get_attribute_info(hsdev,
249 HID_INPUT_REPORT,
250 usage_id,
251 HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
252 &st->accel[CHANNEL_SCAN_INDEX_X + i]);
253 if (ret < 0)
254 break;
255 accel_3d_adjust_channel_bit_mask(channels,
256 CHANNEL_SCAN_INDEX_X + i,
257 st->accel[CHANNEL_SCAN_INDEX_X + i].size);
258 }
259 dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
260 st->accel[0].index,
261 st->accel[0].report_id,
262 st->accel[1].index, st->accel[1].report_id,
263 st->accel[2].index, st->accel[2].report_id);
264
Srinivas Pandruvada64528d02013-10-30 22:48:00 +0000265 /* Set Sensitivity field ids, when there is no individual modifier */
266 if (st->common_attributes.sensitivity.index < 0) {
267 sensor_hub_input_get_attribute_info(hsdev,
268 HID_FEATURE_REPORT, usage_id,
269 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
270 HID_USAGE_SENSOR_DATA_ACCELERATION,
271 &st->common_attributes.sensitivity);
272 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
273 st->common_attributes.sensitivity.index,
274 st->common_attributes.sensitivity.report_id);
275 }
276
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100277 return ret;
278}
279
280/* Function to initialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800281static int hid_accel_3d_probe(struct platform_device *pdev)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100282{
283 int ret = 0;
284 static const char *name = "accel_3d";
285 struct iio_dev *indio_dev;
286 struct accel_3d_state *accel_state;
287 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
288 struct iio_chan_spec *channels;
289
Sachin Kamat6b545ee2013-07-23 07:47:00 +0100290 indio_dev = devm_iio_device_alloc(&pdev->dev,
291 sizeof(struct accel_3d_state));
292 if (indio_dev == NULL)
293 return -ENOMEM;
294
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100295 platform_set_drvdata(pdev, indio_dev);
296
297 accel_state = iio_priv(indio_dev);
298 accel_state->common_attributes.hsdev = hsdev;
299 accel_state->common_attributes.pdev = pdev;
300
301 ret = hid_sensor_parse_common_attributes(hsdev,
302 HID_USAGE_SENSOR_ACCEL_3D,
303 &accel_state->common_attributes);
304 if (ret) {
305 dev_err(&pdev->dev, "failed to setup common attributes\n");
Sachin Kamat6b545ee2013-07-23 07:47:00 +0100306 return ret;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100307 }
308
Axel Lin64ebe952012-10-27 16:03:00 +0100309 channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels),
310 GFP_KERNEL);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100311 if (!channels) {
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100312 dev_err(&pdev->dev, "failed to duplicate channels\n");
Sachin Kamat6b545ee2013-07-23 07:47:00 +0100313 return -ENOMEM;
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100314 }
315
316 ret = accel_3d_parse_report(pdev, hsdev, channels,
317 HID_USAGE_SENSOR_ACCEL_3D, accel_state);
318 if (ret) {
319 dev_err(&pdev->dev, "failed to setup attributes\n");
320 goto error_free_dev_mem;
321 }
322
323 indio_dev->channels = channels;
324 indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels);
325 indio_dev->dev.parent = &pdev->dev;
326 indio_dev->info = &accel_3d_info;
327 indio_dev->name = name;
328 indio_dev->modes = INDIO_DIRECT_MODE;
329
330 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
331 NULL, NULL);
332 if (ret) {
333 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
334 goto error_free_dev_mem;
335 }
336 accel_state->common_attributes.data_ready = false;
337 ret = hid_sensor_setup_trigger(indio_dev, name,
338 &accel_state->common_attributes);
339 if (ret < 0) {
340 dev_err(&pdev->dev, "trigger setup failed\n");
341 goto error_unreg_buffer_funcs;
342 }
343
344 ret = iio_device_register(indio_dev);
345 if (ret) {
346 dev_err(&pdev->dev, "device register failed\n");
347 goto error_remove_trigger;
348 }
349
350 accel_state->callbacks.send_event = accel_3d_proc_event;
351 accel_state->callbacks.capture_sample = accel_3d_capture_sample;
352 accel_state->callbacks.pdev = pdev;
353 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D,
354 &accel_state->callbacks);
355 if (ret < 0) {
356 dev_err(&pdev->dev, "callback reg failed\n");
357 goto error_iio_unreg;
358 }
359
360 return ret;
361
362error_iio_unreg:
363 iio_device_unregister(indio_dev);
364error_remove_trigger:
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000365 hid_sensor_remove_trigger(&accel_state->common_attributes);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100366error_unreg_buffer_funcs:
367 iio_triggered_buffer_cleanup(indio_dev);
368error_free_dev_mem:
369 kfree(indio_dev->channels);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100370 return ret;
371}
372
373/* Function to deinitialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800374static int hid_accel_3d_remove(struct platform_device *pdev)
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100375{
376 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
377 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000378 struct accel_3d_state *accel_state = iio_priv(indio_dev);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100379
380 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D);
381 iio_device_unregister(indio_dev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000382 hid_sensor_remove_trigger(&accel_state->common_attributes);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100383 iio_triggered_buffer_cleanup(indio_dev);
384 kfree(indio_dev->channels);
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100385
386 return 0;
387}
388
Alexander Holler66c721f2013-07-10 09:31:00 +0100389static struct platform_device_id hid_accel_3d_ids[] = {
390 {
391 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
392 .name = "HID-SENSOR-200073",
393 },
394 { /* sentinel */ }
395};
396MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
397
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100398static struct platform_driver hid_accel_3d_platform_driver = {
Alexander Holler66c721f2013-07-10 09:31:00 +0100399 .id_table = hid_accel_3d_ids,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100400 .driver = {
Alexander Holler66c721f2013-07-10 09:31:00 +0100401 .name = KBUILD_MODNAME,
srinivas pandruvada45fe6f72012-09-05 13:56:00 +0100402 .owner = THIS_MODULE,
403 },
404 .probe = hid_accel_3d_probe,
405 .remove = hid_accel_3d_remove,
406};
407module_platform_driver(hid_accel_3d_platform_driver);
408
409MODULE_DESCRIPTION("HID Sensor Accel 3D");
410MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
411MODULE_LICENSE("GPL");