blob: 131ced0dcb1cfdb5a15cb3df509ed56e354d6994 [file] [log] [blame]
srinivas pandruvadabc1d57ba2012-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 pandruvadabc1d57ba2012-09-05 13:56:00 +010031#include "../common/hid-sensors/hid-sensor-trigger.h"
32
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +010033enum magn_3d_channel {
34 CHANNEL_SCAN_INDEX_X,
35 CHANNEL_SCAN_INDEX_Y,
36 CHANNEL_SCAN_INDEX_Z,
37 MAGN_3D_CHANNEL_MAX,
38};
39
40struct magn_3d_state {
41 struct hid_sensor_hub_callbacks callbacks;
Alexander Hollere07c6d12012-12-15 12:45:00 +000042 struct hid_sensor_common common_attributes;
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +010043 struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
44 u32 magn_val[MAGN_3D_CHANNEL_MAX];
Srinivas Pandruvada92463fd2014-04-19 00:22:00 +010045 int scale_pre_decml;
46 int scale_post_decml;
47 int scale_precision;
48 int value_offset;
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +010049};
50
51static const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
52 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS,
53 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS,
54 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
55};
56
57/* Channel definitions */
58static const struct iio_chan_spec magn_3d_channels[] = {
59 {
60 .type = IIO_MAGN,
61 .modified = 1,
62 .channel2 = IIO_MOD_X,
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000063 .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),
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +010067 .scan_index = CHANNEL_SCAN_INDEX_X,
68 }, {
69 .type = IIO_MAGN,
70 .modified = 1,
71 .channel2 = IIO_MOD_Y,
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000072 .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),
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +010076 .scan_index = CHANNEL_SCAN_INDEX_Y,
77 }, {
78 .type = IIO_MAGN,
79 .modified = 1,
80 .channel2 = IIO_MOD_Z,
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000081 .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),
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +010085 .scan_index = CHANNEL_SCAN_INDEX_Z,
86 }
87};
88
89/* Adjust channel real bits based on report descriptor */
90static void magn_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 */
101static int magn_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 magn_3d_state *magn_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 =
117 magn_state->magn[chan->scan_index].report_id;
118 address = magn_3d_addresses[chan->scan_index];
119 if (report_id >= 0)
120 *val = sensor_hub_input_attr_get_raw_value(
121 magn_state->common_attributes.hsdev,
122 HID_USAGE_SENSOR_COMPASS_3D, address,
123 report_id);
124 else {
125 *val = 0;
126 return -EINVAL;
127 }
128 ret_type = IIO_VAL_INT;
129 break;
130 case IIO_CHAN_INFO_SCALE:
Srinivas Pandruvada92463fd2014-04-19 00:22:00 +0100131 *val = magn_state->scale_pre_decml;
132 *val2 = magn_state->scale_post_decml;
133 ret_type = magn_state->scale_precision;
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100134 break;
135 case IIO_CHAN_INFO_OFFSET:
Srinivas Pandruvada92463fd2014-04-19 00:22:00 +0100136 *val = magn_state->value_offset;
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100137 ret_type = IIO_VAL_INT;
138 break;
139 case IIO_CHAN_INFO_SAMP_FREQ:
140 ret = hid_sensor_read_samp_freq_value(
141 &magn_state->common_attributes, val, val2);
142 ret_type = IIO_VAL_INT_PLUS_MICRO;
143 break;
144 case IIO_CHAN_INFO_HYSTERESIS:
145 ret = hid_sensor_read_raw_hyst_value(
146 &magn_state->common_attributes, val, val2);
147 ret_type = IIO_VAL_INT_PLUS_MICRO;
148 break;
149 default:
150 ret_type = -EINVAL;
151 break;
152 }
153
154 return ret_type;
155}
156
157/* Channel write_raw handler */
158static int magn_3d_write_raw(struct iio_dev *indio_dev,
159 struct iio_chan_spec const *chan,
160 int val,
161 int val2,
162 long mask)
163{
164 struct magn_3d_state *magn_state = iio_priv(indio_dev);
165 int ret = 0;
166
167 switch (mask) {
168 case IIO_CHAN_INFO_SAMP_FREQ:
169 ret = hid_sensor_write_samp_freq_value(
170 &magn_state->common_attributes, val, val2);
171 break;
172 case IIO_CHAN_INFO_HYSTERESIS:
173 ret = hid_sensor_write_raw_hyst_value(
174 &magn_state->common_attributes, val, val2);
175 break;
176 default:
177 ret = -EINVAL;
178 }
179
180 return ret;
181}
182
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100183static const struct iio_info magn_3d_info = {
184 .driver_module = THIS_MODULE,
185 .read_raw = &magn_3d_read_raw,
186 .write_raw = &magn_3d_write_raw,
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100187};
188
189/* Function to push data to buffer */
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100190static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
191 int len)
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100192{
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100193 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100194 iio_push_to_buffers(indio_dev, data);
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100195}
196
197/* Callback handler to send event after all samples are received and captured */
198static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
199 unsigned usage_id,
200 void *priv)
201{
202 struct iio_dev *indio_dev = platform_get_drvdata(priv);
203 struct magn_3d_state *magn_state = iio_priv(indio_dev);
204
205 dev_dbg(&indio_dev->dev, "magn_3d_proc_event [%d]\n",
206 magn_state->common_attributes.data_ready);
207 if (magn_state->common_attributes.data_ready)
208 hid_sensor_push_data(indio_dev,
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100209 magn_state->magn_val,
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100210 sizeof(magn_state->magn_val));
211
212 return 0;
213}
214
215/* Capture samples in local storage */
216static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
217 unsigned usage_id,
218 size_t raw_len, char *raw_data,
219 void *priv)
220{
221 struct iio_dev *indio_dev = platform_get_drvdata(priv);
222 struct magn_3d_state *magn_state = iio_priv(indio_dev);
223 int offset;
224 int ret = -EINVAL;
225
226 switch (usage_id) {
227 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS:
228 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS:
229 case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS:
230 offset = usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS;
231 magn_state->magn_val[CHANNEL_SCAN_INDEX_X + offset] =
232 *(u32 *)raw_data;
233 ret = 0;
234 break;
235 default:
236 break;
237 }
238
239 return ret;
240}
241
242/* Parse report which is specific to an usage id*/
243static int magn_3d_parse_report(struct platform_device *pdev,
244 struct hid_sensor_hub_device *hsdev,
245 struct iio_chan_spec *channels,
246 unsigned usage_id,
247 struct magn_3d_state *st)
248{
249 int ret;
250 int i;
251
252 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
253 ret = sensor_hub_input_get_attribute_info(hsdev,
254 HID_INPUT_REPORT,
255 usage_id,
256 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS + i,
257 &st->magn[CHANNEL_SCAN_INDEX_X + i]);
258 if (ret < 0)
259 break;
260 magn_3d_adjust_channel_bit_mask(channels,
261 CHANNEL_SCAN_INDEX_X + i,
262 st->magn[CHANNEL_SCAN_INDEX_X + i].size);
263 }
264 dev_dbg(&pdev->dev, "magn_3d %x:%x, %x:%x, %x:%x\n",
265 st->magn[0].index,
266 st->magn[0].report_id,
267 st->magn[1].index, st->magn[1].report_id,
268 st->magn[2].index, st->magn[2].report_id);
269
Srinivas Pandruvada92463fd2014-04-19 00:22:00 +0100270 st->scale_precision = hid_sensor_format_scale(
271 HID_USAGE_SENSOR_COMPASS_3D,
272 &st->magn[CHANNEL_SCAN_INDEX_X],
273 &st->scale_pre_decml, &st->scale_post_decml);
274
Srinivas Pandruvada2461fc92013-11-06 00:11:00 +0000275 /* Set Sensitivity field ids, when there is no individual modifier */
276 if (st->common_attributes.sensitivity.index < 0) {
277 sensor_hub_input_get_attribute_info(hsdev,
278 HID_FEATURE_REPORT, usage_id,
279 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
280 HID_USAGE_SENSOR_DATA_ORIENTATION,
281 &st->common_attributes.sensitivity);
282 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
283 st->common_attributes.sensitivity.index,
284 st->common_attributes.sensitivity.report_id);
285 }
286
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100287 return ret;
288}
289
290/* Function to initialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800291static int hid_magn_3d_probe(struct platform_device *pdev)
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100292{
293 int ret = 0;
294 static char *name = "magn_3d";
295 struct iio_dev *indio_dev;
296 struct magn_3d_state *magn_state;
297 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
298 struct iio_chan_spec *channels;
299
Sachin Kamat907c0a02013-08-02 11:08:00 +0100300 indio_dev = devm_iio_device_alloc(&pdev->dev,
301 sizeof(struct magn_3d_state));
302 if (indio_dev == NULL)
303 return -ENOMEM;
304
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100305 platform_set_drvdata(pdev, indio_dev);
306
307 magn_state = iio_priv(indio_dev);
308 magn_state->common_attributes.hsdev = hsdev;
309 magn_state->common_attributes.pdev = pdev;
310
311 ret = hid_sensor_parse_common_attributes(hsdev,
312 HID_USAGE_SENSOR_COMPASS_3D,
313 &magn_state->common_attributes);
314 if (ret) {
315 dev_err(&pdev->dev, "failed to setup common attributes\n");
Sachin Kamat907c0a02013-08-02 11:08:00 +0100316 return ret;
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100317 }
318
Axel Lin64ebe952012-10-27 16:03:00 +0100319 channels = kmemdup(magn_3d_channels, sizeof(magn_3d_channels),
320 GFP_KERNEL);
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100321 if (!channels) {
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100322 dev_err(&pdev->dev, "failed to duplicate channels\n");
Sachin Kamat907c0a02013-08-02 11:08:00 +0100323 return -ENOMEM;
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100324 }
325
326 ret = magn_3d_parse_report(pdev, hsdev, channels,
327 HID_USAGE_SENSOR_COMPASS_3D, magn_state);
328 if (ret) {
329 dev_err(&pdev->dev, "failed to setup attributes\n");
330 goto error_free_dev_mem;
331 }
332
333 indio_dev->channels = channels;
334 indio_dev->num_channels = ARRAY_SIZE(magn_3d_channels);
335 indio_dev->dev.parent = &pdev->dev;
336 indio_dev->info = &magn_3d_info;
337 indio_dev->name = name;
338 indio_dev->modes = INDIO_DIRECT_MODE;
339
340 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
341 NULL, NULL);
342 if (ret) {
343 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
344 goto error_free_dev_mem;
345 }
346 magn_state->common_attributes.data_ready = false;
347 ret = hid_sensor_setup_trigger(indio_dev, name,
348 &magn_state->common_attributes);
349 if (ret < 0) {
350 dev_err(&pdev->dev, "trigger setup failed\n");
351 goto error_unreg_buffer_funcs;
352 }
353
354 ret = iio_device_register(indio_dev);
355 if (ret) {
356 dev_err(&pdev->dev, "device register failed\n");
357 goto error_remove_trigger;
358 }
359
360 magn_state->callbacks.send_event = magn_3d_proc_event;
361 magn_state->callbacks.capture_sample = magn_3d_capture_sample;
362 magn_state->callbacks.pdev = pdev;
363 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D,
364 &magn_state->callbacks);
365 if (ret < 0) {
366 dev_err(&pdev->dev, "callback reg failed\n");
367 goto error_iio_unreg;
368 }
369
370 return ret;
371
372error_iio_unreg:
373 iio_device_unregister(indio_dev);
374error_remove_trigger:
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000375 hid_sensor_remove_trigger(&magn_state->common_attributes);
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100376error_unreg_buffer_funcs:
377 iio_triggered_buffer_cleanup(indio_dev);
378error_free_dev_mem:
379 kfree(indio_dev->channels);
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100380 return ret;
381}
382
383/* Function to deinitialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800384static int hid_magn_3d_remove(struct platform_device *pdev)
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100385{
386 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
387 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000388 struct magn_3d_state *magn_state = iio_priv(indio_dev);
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100389
390 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D);
391 iio_device_unregister(indio_dev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000392 hid_sensor_remove_trigger(&magn_state->common_attributes);
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100393 iio_triggered_buffer_cleanup(indio_dev);
394 kfree(indio_dev->channels);
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100395
396 return 0;
397}
398
Alexander Hollera67c3852013-07-10 09:32:00 +0100399static struct platform_device_id hid_magn_3d_ids[] = {
400 {
401 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
402 .name = "HID-SENSOR-200083",
403 },
404 { /* sentinel */ }
405};
406MODULE_DEVICE_TABLE(platform, hid_magn_3d_ids);
407
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100408static struct platform_driver hid_magn_3d_platform_driver = {
Alexander Hollera67c3852013-07-10 09:32:00 +0100409 .id_table = hid_magn_3d_ids,
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100410 .driver = {
Alexander Hollera67c3852013-07-10 09:32:00 +0100411 .name = KBUILD_MODNAME,
srinivas pandruvadabc1d57ba2012-09-05 13:56:00 +0100412 .owner = THIS_MODULE,
413 },
414 .probe = hid_magn_3d_probe,
415 .remove = hid_magn_3d_remove,
416};
417module_platform_driver(hid_magn_3d_platform_driver);
418
419MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");
420MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
421MODULE_LICENSE("GPL");