Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/drivers/iio/magnetometer/hid-sensor-magn-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 magn_3d_channel {
 35         CHANNEL_SCAN_INDEX_X,
 36         CHANNEL_SCAN_INDEX_Y,
 37         CHANNEL_SCAN_INDEX_Z,
 38         MAGN_3D_CHANNEL_MAX,
 39 };
 40 
 41 struct magn_3d_state {
 42         struct hid_sensor_hub_callbacks callbacks;
 43         struct hid_sensor_common common_attributes;
 44         struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX];
 45         u32 magn_val[MAGN_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 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = {
 53         HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS,
 54         HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS,
 55         HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS
 56 };
 57 
 58 /* Channel definitions */
 59 static const struct iio_chan_spec magn_3d_channels[] = {
 60         {
 61                 .type = IIO_MAGN,
 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_MAGN,
 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_MAGN,
 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 magn_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 magn_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 magn_3d_state *magn_state = iio_priv(indio_dev);
111         int report_id = -1;
112         u32 address;
113         int ret_type;
114         s32 poll_value;
115 
116         *val = 0;
117         *val2 = 0;
118         switch (mask) {
119         case 0:
120                 poll_value = hid_sensor_read_poll_value(
121                                         &magn_state->common_attributes);
122                 if (poll_value < 0)
123                                 return -EINVAL;
124 
125                 hid_sensor_power_state(&magn_state->common_attributes, true);
126                 msleep_interruptible(poll_value * 2);
127 
128                 report_id =
129                         magn_state->magn[chan->scan_index].report_id;
130                 address = magn_3d_addresses[chan->scan_index];
131                 if (report_id >= 0)
132                         *val = sensor_hub_input_attr_get_raw_value(
133                                 magn_state->common_attributes.hsdev,
134                                 HID_USAGE_SENSOR_COMPASS_3D, address,
135                                 report_id);
136                 else {
137                         *val = 0;
138                         hid_sensor_power_state(&magn_state->common_attributes,
139                                                 false);
140                         return -EINVAL;
141                 }
142                 hid_sensor_power_state(&magn_state->common_attributes, false);
143                 ret_type = IIO_VAL_INT;
144                 break;
145         case IIO_CHAN_INFO_SCALE:
146                 *val = magn_state->scale_pre_decml;
147                 *val2 = magn_state->scale_post_decml;
148                 ret_type = magn_state->scale_precision;
149                 break;
150         case IIO_CHAN_INFO_OFFSET:
151                 *val = magn_state->value_offset;
152                 ret_type = IIO_VAL_INT;
153                 break;
154         case IIO_CHAN_INFO_SAMP_FREQ:
155                 ret_type = hid_sensor_read_samp_freq_value(
156                         &magn_state->common_attributes, val, val2);
157                 break;
158         case IIO_CHAN_INFO_HYSTERESIS:
159                 ret_type = hid_sensor_read_raw_hyst_value(
160                         &magn_state->common_attributes, val, val2);
161                 break;
162         default:
163                 ret_type = -EINVAL;
164                 break;
165         }
166 
167         return ret_type;
168 }
169 
170 /* Channel write_raw handler */
171 static int magn_3d_write_raw(struct iio_dev *indio_dev,
172                                struct iio_chan_spec const *chan,
173                                int val,
174                                int val2,
175                                long mask)
176 {
177         struct magn_3d_state *magn_state = iio_priv(indio_dev);
178         int ret = 0;
179 
180         switch (mask) {
181         case IIO_CHAN_INFO_SAMP_FREQ:
182                 ret = hid_sensor_write_samp_freq_value(
183                                 &magn_state->common_attributes, val, val2);
184                 break;
185         case IIO_CHAN_INFO_HYSTERESIS:
186                 ret = hid_sensor_write_raw_hyst_value(
187                                 &magn_state->common_attributes, val, val2);
188                 break;
189         default:
190                 ret = -EINVAL;
191         }
192 
193         return ret;
194 }
195 
196 static const struct iio_info magn_3d_info = {
197         .driver_module = THIS_MODULE,
198         .read_raw = &magn_3d_read_raw,
199         .write_raw = &magn_3d_write_raw,
200 };
201 
202 /* Function to push data to buffer */
203 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
204         int len)
205 {
206         dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
207         iio_push_to_buffers(indio_dev, data);
208 }
209 
210 /* Callback handler to send event after all samples are received and captured */
211 static int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev,
212                                 unsigned usage_id,
213                                 void *priv)
214 {
215         struct iio_dev *indio_dev = platform_get_drvdata(priv);
216         struct magn_3d_state *magn_state = iio_priv(indio_dev);
217 
218         dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n");
219         if (atomic_read(&magn_state->common_attributes.data_ready))
220                 hid_sensor_push_data(indio_dev,
221                                 magn_state->magn_val,
222                                 sizeof(magn_state->magn_val));
223 
224         return 0;
225 }
226 
227 /* Capture samples in local storage */
228 static int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
229                                 unsigned usage_id,
230                                 size_t raw_len, char *raw_data,
231                                 void *priv)
232 {
233         struct iio_dev *indio_dev = platform_get_drvdata(priv);
234         struct magn_3d_state *magn_state = iio_priv(indio_dev);
235         int offset;
236         int ret = -EINVAL;
237 
238         switch (usage_id) {
239         case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS:
240         case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS:
241         case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS:
242                 offset = usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS;
243                 magn_state->magn_val[CHANNEL_SCAN_INDEX_X + offset] =
244                                                 *(u32 *)raw_data;
245                 ret = 0;
246         break;
247         default:
248                 break;
249         }
250 
251         return ret;
252 }
253 
254 /* Parse report which is specific to an usage id*/
255 static int magn_3d_parse_report(struct platform_device *pdev,
256                                 struct hid_sensor_hub_device *hsdev,
257                                 struct iio_chan_spec *channels,
258                                 unsigned usage_id,
259                                 struct magn_3d_state *st)
260 {
261         int ret;
262         int i;
263 
264         for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
265                 ret = sensor_hub_input_get_attribute_info(hsdev,
266                                 HID_INPUT_REPORT,
267                                 usage_id,
268                                 HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS + i,
269                                 &st->magn[CHANNEL_SCAN_INDEX_X + i]);
270                 if (ret < 0)
271                         break;
272                 magn_3d_adjust_channel_bit_mask(channels,
273                                 CHANNEL_SCAN_INDEX_X + i,
274                                 st->magn[CHANNEL_SCAN_INDEX_X + i].size);
275         }
276         dev_dbg(&pdev->dev, "magn_3d %x:%x, %x:%x, %x:%x\n",
277                         st->magn[0].index,
278                         st->magn[0].report_id,
279                         st->magn[1].index, st->magn[1].report_id,
280                         st->magn[2].index, st->magn[2].report_id);
281 
282         st->scale_precision = hid_sensor_format_scale(
283                                 HID_USAGE_SENSOR_COMPASS_3D,
284                                 &st->magn[CHANNEL_SCAN_INDEX_X],
285                                 &st->scale_pre_decml, &st->scale_post_decml);
286 
287         /* Set Sensitivity field ids, when there is no individual modifier */
288         if (st->common_attributes.sensitivity.index < 0) {
289                 sensor_hub_input_get_attribute_info(hsdev,
290                         HID_FEATURE_REPORT, usage_id,
291                         HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
292                         HID_USAGE_SENSOR_DATA_ORIENTATION,
293                         &st->common_attributes.sensitivity);
294                 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
295                         st->common_attributes.sensitivity.index,
296                         st->common_attributes.sensitivity.report_id);
297         }
298 
299         return ret;
300 }
301 
302 /* Function to initialize the processing for usage id */
303 static int hid_magn_3d_probe(struct platform_device *pdev)
304 {
305         int ret = 0;
306         static char *name = "magn_3d";
307         struct iio_dev *indio_dev;
308         struct magn_3d_state *magn_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,
313                                           sizeof(struct magn_3d_state));
314         if (indio_dev == NULL)
315                 return -ENOMEM;
316 
317         platform_set_drvdata(pdev, indio_dev);
318 
319         magn_state = iio_priv(indio_dev);
320         magn_state->common_attributes.hsdev = hsdev;
321         magn_state->common_attributes.pdev = pdev;
322 
323         ret = hid_sensor_parse_common_attributes(hsdev,
324                                 HID_USAGE_SENSOR_COMPASS_3D,
325                                 &magn_state->common_attributes);
326         if (ret) {
327                 dev_err(&pdev->dev, "failed to setup common attributes\n");
328                 return ret;
329         }
330 
331         channels = kmemdup(magn_3d_channels, sizeof(magn_3d_channels),
332                            GFP_KERNEL);
333         if (!channels) {
334                 dev_err(&pdev->dev, "failed to duplicate channels\n");
335                 return -ENOMEM;
336         }
337 
338         ret = magn_3d_parse_report(pdev, hsdev, channels,
339                                 HID_USAGE_SENSOR_COMPASS_3D, magn_state);
340         if (ret) {
341                 dev_err(&pdev->dev, "failed to setup attributes\n");
342                 goto error_free_dev_mem;
343         }
344 
345         indio_dev->channels = channels;
346         indio_dev->num_channels = ARRAY_SIZE(magn_3d_channels);
347         indio_dev->dev.parent = &pdev->dev;
348         indio_dev->info = &magn_3d_info;
349         indio_dev->name = name;
350         indio_dev->modes = INDIO_DIRECT_MODE;
351 
352         ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
353                 NULL, NULL);
354         if (ret) {
355                 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
356                 goto error_free_dev_mem;
357         }
358         atomic_set(&magn_state->common_attributes.data_ready, 0);
359         ret = hid_sensor_setup_trigger(indio_dev, name,
360                                         &magn_state->common_attributes);
361         if (ret < 0) {
362                 dev_err(&pdev->dev, "trigger setup failed\n");
363                 goto error_unreg_buffer_funcs;
364         }
365 
366         ret = iio_device_register(indio_dev);
367         if (ret) {
368                 dev_err(&pdev->dev, "device register failed\n");
369                 goto error_remove_trigger;
370         }
371 
372         magn_state->callbacks.send_event = magn_3d_proc_event;
373         magn_state->callbacks.capture_sample = magn_3d_capture_sample;
374         magn_state->callbacks.pdev = pdev;
375         ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D,
376                                         &magn_state->callbacks);
377         if (ret < 0) {
378                 dev_err(&pdev->dev, "callback reg failed\n");
379                 goto error_iio_unreg;
380         }
381 
382         return ret;
383 
384 error_iio_unreg:
385         iio_device_unregister(indio_dev);
386 error_remove_trigger:
387         hid_sensor_remove_trigger(&magn_state->common_attributes);
388 error_unreg_buffer_funcs:
389         iio_triggered_buffer_cleanup(indio_dev);
390 error_free_dev_mem:
391         kfree(indio_dev->channels);
392         return ret;
393 }
394 
395 /* Function to deinitialize the processing for usage id */
396 static int hid_magn_3d_remove(struct platform_device *pdev)
397 {
398         struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
399         struct iio_dev *indio_dev = platform_get_drvdata(pdev);
400         struct magn_3d_state *magn_state = iio_priv(indio_dev);
401 
402         sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D);
403         iio_device_unregister(indio_dev);
404         hid_sensor_remove_trigger(&magn_state->common_attributes);
405         iio_triggered_buffer_cleanup(indio_dev);
406         kfree(indio_dev->channels);
407 
408         return 0;
409 }
410 
411 static struct platform_device_id hid_magn_3d_ids[] = {
412         {
413                 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
414                 .name = "HID-SENSOR-200083",
415         },
416         { /* sentinel */ }
417 };
418 MODULE_DEVICE_TABLE(platform, hid_magn_3d_ids);
419 
420 static struct platform_driver hid_magn_3d_platform_driver = {
421         .id_table = hid_magn_3d_ids,
422         .driver = {
423                 .name   = KBUILD_MODNAME,
424                 .owner  = THIS_MODULE,
425         },
426         .probe          = hid_magn_3d_probe,
427         .remove         = hid_magn_3d_remove,
428 };
429 module_platform_driver(hid_magn_3d_platform_driver);
430 
431 MODULE_DESCRIPTION("HID Sensor Magnetometer 3D");
432 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
433 MODULE_LICENSE("GPL");
434 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us