Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hid-sensor-accel-3d.c
Go to the documentation of this file.
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/hid-sensor-hub.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/buffer.h>
31 #include "../common/hid-sensors/hid-sensor-attributes.h"
32 #include "../common/hid-sensors/hid-sensor-trigger.h"
33 
34 /*Format: HID-SENSOR-usage_id_in_hex*/
35 /*Usage ID from spec for Accelerometer-3D: 0x200073*/
36 #define DRIVER_NAME "HID-SENSOR-200073"
37 
43 };
44 
50 };
51 
52 static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = {
56 };
57 
58 /* Channel definitions */
59 static const struct iio_chan_spec accel_3d_channels[] = {
60  {
61  .type = IIO_ACCEL,
62  .modified = 1,
63  .channel2 = IIO_MOD_X,
64  .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
68  .scan_index = CHANNEL_SCAN_INDEX_X,
69  }, {
70  .type = IIO_ACCEL,
71  .modified = 1,
72  .channel2 = IIO_MOD_Y,
73  .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
77  .scan_index = CHANNEL_SCAN_INDEX_Y,
78  }, {
79  .type = IIO_ACCEL,
80  .modified = 1,
81  .channel2 = IIO_MOD_Z,
82  .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT |
86  .scan_index = CHANNEL_SCAN_INDEX_Z,
87  }
88 };
89 
90 /* Adjust channel real bits based on report descriptor */
91 static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
92  int channel, int size)
93 {
94  channels[channel].scan_type.sign = 's';
95  /* Real storage bits will change based on the report desc. */
96  channels[channel].scan_type.realbits = size * 8;
97  /* Maximum size of a sample to capture is u32 */
98  channels[channel].scan_type.storagebits = sizeof(u32) * 8;
99 }
100 
101 /* Channel read_raw handler */
102 static int accel_3d_read_raw(struct iio_dev *indio_dev,
103  struct iio_chan_spec const *chan,
104  int *val, int *val2,
105  long mask)
106 {
107  struct accel_3d_state *accel_state = iio_priv(indio_dev);
108  int report_id = -1;
109  u32 address;
110  int ret;
111  int ret_type;
112 
113  *val = 0;
114  *val2 = 0;
115  switch (mask) {
116  case 0:
117  report_id = accel_state->accel[chan->scan_index].report_id;
118  address = accel_3d_addresses[chan->scan_index];
119  if (report_id >= 0)
121  accel_state->common_attributes.hsdev,
122  HID_USAGE_SENSOR_ACCEL_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:
131  *val = accel_state->accel[CHANNEL_SCAN_INDEX_X].units;
132  ret_type = IIO_VAL_INT;
133  break;
135  *val = hid_sensor_convert_exponent(
136  accel_state->accel[CHANNEL_SCAN_INDEX_X].unit_expo);
137  ret_type = IIO_VAL_INT;
138  break;
141  &accel_state->common_attributes, val, val2);
142  ret_type = IIO_VAL_INT_PLUS_MICRO;
143  break;
146  &accel_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 */
158 static int accel_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 accel_3d_state *accel_state = iio_priv(indio_dev);
165  int ret = 0;
166 
167  switch (mask) {
170  &accel_state->common_attributes, val, val2);
171  break;
174  &accel_state->common_attributes, val, val2);
175  break;
176  default:
177  ret = -EINVAL;
178  }
179 
180  return ret;
181 }
182 
183 static int accel_3d_write_raw_get_fmt(struct iio_dev *indio_dev,
184  struct iio_chan_spec const *chan,
185  long mask)
186 {
187  return IIO_VAL_INT_PLUS_MICRO;
188 }
189 
190 static const struct iio_info accel_3d_info = {
191  .driver_module = THIS_MODULE,
192  .read_raw = &accel_3d_read_raw,
193  .write_raw = &accel_3d_write_raw,
194  .write_raw_get_fmt = &accel_3d_write_raw_get_fmt,
195 };
196 
197 /* Function to push data to buffer */
198 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
199 {
200  struct iio_buffer *buffer = indio_dev->buffer;
201  int datum_sz;
202 
203  dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
204  if (!buffer) {
205  dev_err(&indio_dev->dev, "Buffer == NULL\n");
206  return;
207  }
208  datum_sz = buffer->access->get_bytes_per_datum(buffer);
209  if (len > datum_sz) {
210  dev_err(&indio_dev->dev, "Datum size mismatch %d:%d\n", len,
211  datum_sz);
212  return;
213  }
214  iio_push_to_buffer(buffer, (u8 *)data);
215 }
216 
217 /* Callback handler to send event after all samples are received and captured */
218 static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
219  unsigned usage_id,
220  void *priv)
221 {
222  struct iio_dev *indio_dev = platform_get_drvdata(priv);
223  struct accel_3d_state *accel_state = iio_priv(indio_dev);
224 
225  dev_dbg(&indio_dev->dev, "accel_3d_proc_event [%d]\n",
226  accel_state->common_attributes.data_ready);
227  if (accel_state->common_attributes.data_ready)
228  hid_sensor_push_data(indio_dev,
229  (u8 *)accel_state->accel_val,
230  sizeof(accel_state->accel_val));
231 
232  return 0;
233 }
234 
235 /* Capture samples in local storage */
236 static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
237  unsigned usage_id,
238  size_t raw_len, char *raw_data,
239  void *priv)
240 {
241  struct iio_dev *indio_dev = platform_get_drvdata(priv);
242  struct accel_3d_state *accel_state = iio_priv(indio_dev);
243  int offset;
244  int ret = -EINVAL;
245 
246  switch (usage_id) {
250  offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
251  accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
252  *(u32 *)raw_data;
253  ret = 0;
254  break;
255  default:
256  break;
257  }
258 
259  return ret;
260 }
261 
262 /* Parse report which is specific to an usage id*/
263 static int accel_3d_parse_report(struct platform_device *pdev,
264  struct hid_sensor_hub_device *hsdev,
265  struct iio_chan_spec *channels,
266  unsigned usage_id,
267  struct accel_3d_state *st)
268 {
269  int ret;
270  int i;
271 
272  for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
275  usage_id,
277  &st->accel[CHANNEL_SCAN_INDEX_X + i]);
278  if (ret < 0)
279  break;
280  accel_3d_adjust_channel_bit_mask(channels,
282  st->accel[CHANNEL_SCAN_INDEX_X + i].size);
283  }
284  dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
285  st->accel[0].index,
286  st->accel[0].report_id,
287  st->accel[1].index, st->accel[1].report_id,
288  st->accel[2].index, st->accel[2].report_id);
289 
290  return ret;
291 }
292 
293 /* Function to initialize the processing for usage id */
294 static int __devinit hid_accel_3d_probe(struct platform_device *pdev)
295 {
296  int ret = 0;
297  static const char *name = "accel_3d";
298  struct iio_dev *indio_dev;
299  struct accel_3d_state *accel_state;
300  struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
301  struct iio_chan_spec *channels;
302 
303  indio_dev = iio_device_alloc(sizeof(struct accel_3d_state));
304  if (indio_dev == NULL) {
305  ret = -ENOMEM;
306  goto error_ret;
307  }
308  platform_set_drvdata(pdev, indio_dev);
309 
310  accel_state = iio_priv(indio_dev);
311  accel_state->common_attributes.hsdev = hsdev;
312  accel_state->common_attributes.pdev = pdev;
313 
316  &accel_state->common_attributes);
317  if (ret) {
318  dev_err(&pdev->dev, "failed to setup common attributes\n");
319  goto error_free_dev;
320  }
321 
322  channels = kmemdup(accel_3d_channels,
323  sizeof(accel_3d_channels),
324  GFP_KERNEL);
325  if (!channels) {
326  dev_err(&pdev->dev, "failed to duplicate channels\n");
327  goto error_free_dev;
328  }
329 
330  ret = accel_3d_parse_report(pdev, hsdev, channels,
331  HID_USAGE_SENSOR_ACCEL_3D, accel_state);
332  if (ret) {
333  dev_err(&pdev->dev, "failed to setup attributes\n");
334  goto error_free_dev_mem;
335  }
336 
337  indio_dev->channels = channels;
338  indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels);
339  indio_dev->dev.parent = &pdev->dev;
340  indio_dev->info = &accel_3d_info;
341  indio_dev->name = name;
342  indio_dev->modes = INDIO_DIRECT_MODE;
343 
345  NULL, NULL);
346  if (ret) {
347  dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
348  goto error_free_dev_mem;
349  }
350  accel_state->common_attributes.data_ready = false;
351  ret = hid_sensor_setup_trigger(indio_dev, name,
352  &accel_state->common_attributes);
353  if (ret < 0) {
354  dev_err(&pdev->dev, "trigger setup failed\n");
355  goto error_unreg_buffer_funcs;
356  }
357 
358  ret = iio_device_register(indio_dev);
359  if (ret) {
360  dev_err(&pdev->dev, "device register failed\n");
361  goto error_remove_trigger;
362  }
363 
364  accel_state->callbacks.send_event = accel_3d_proc_event;
365  accel_state->callbacks.capture_sample = accel_3d_capture_sample;
366  accel_state->callbacks.pdev = pdev;
368  &accel_state->callbacks);
369  if (ret < 0) {
370  dev_err(&pdev->dev, "callback reg failed\n");
371  goto error_iio_unreg;
372  }
373 
374  return ret;
375 
376 error_iio_unreg:
377  iio_device_unregister(indio_dev);
378 error_remove_trigger:
379  hid_sensor_remove_trigger(indio_dev);
380 error_unreg_buffer_funcs:
381  iio_triggered_buffer_cleanup(indio_dev);
382 error_free_dev_mem:
383  kfree(indio_dev->channels);
384 error_free_dev:
385  iio_device_free(indio_dev);
386 error_ret:
387  return ret;
388 }
389 
390 /* Function to deinitialize the processing for usage id */
391 static int __devinit hid_accel_3d_remove(struct platform_device *pdev)
392 {
393  struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
394  struct iio_dev *indio_dev = platform_get_drvdata(pdev);
395 
397  iio_device_unregister(indio_dev);
398  hid_sensor_remove_trigger(indio_dev);
399  iio_triggered_buffer_cleanup(indio_dev);
400  kfree(indio_dev->channels);
401  iio_device_free(indio_dev);
402 
403  return 0;
404 }
405 
406 static struct platform_driver hid_accel_3d_platform_driver = {
407  .driver = {
408  .name = DRIVER_NAME,
409  .owner = THIS_MODULE,
410  },
411  .probe = hid_accel_3d_probe,
412  .remove = hid_accel_3d_remove,
413 };
414 module_platform_driver(hid_accel_3d_platform_driver);
415 
416 MODULE_DESCRIPTION("HID Sensor Accel 3D");
417 MODULE_AUTHOR("Srinivas Pandruvada <[email protected]>");
418 MODULE_LICENSE("GPL");