Version:  2.0.40 2.2.26 2.4.37 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1

Linux/drivers/iio/accel/hid-sensor-accel-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 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 
 41 struct accel_3d_state {
 42         struct hid_sensor_hub_callbacks callbacks;
 43         struct hid_sensor_common common_attributes;
 44         struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
 45         u32 accel_val[ACCEL_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 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 */
 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_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_ACCEL,
 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_ACCEL,
 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 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 */
105 static 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;
113         int ret_type;
114 
115         *val = 0;
116         *val2 = 0;
117         switch (mask) {
118         case 0:
119                 hid_sensor_power_state(&accel_state->common_attributes, true);
120                 report_id = accel_state->accel[chan->scan_index].report_id;
121                 address = accel_3d_addresses[chan->scan_index];
122                 if (report_id >= 0)
123                         *val = sensor_hub_input_attr_get_raw_value(
124                                         accel_state->common_attributes.hsdev,
125                                         HID_USAGE_SENSOR_ACCEL_3D, address,
126                                         report_id,
127                                         SENSOR_HUB_SYNC);
128                 else {
129                         *val = 0;
130                         hid_sensor_power_state(&accel_state->common_attributes,
131                                                  false);
132                         return -EINVAL;
133                 }
134                 hid_sensor_power_state(&accel_state->common_attributes, false);
135                 ret_type = IIO_VAL_INT;
136                 break;
137         case IIO_CHAN_INFO_SCALE:
138                 *val = accel_state->scale_pre_decml;
139                 *val2 = accel_state->scale_post_decml;
140                 ret_type = accel_state->scale_precision;
141                 break;
142         case IIO_CHAN_INFO_OFFSET:
143                 *val = accel_state->value_offset;
144                 ret_type = IIO_VAL_INT;
145                 break;
146         case IIO_CHAN_INFO_SAMP_FREQ:
147                 ret_type = hid_sensor_read_samp_freq_value(
148                         &accel_state->common_attributes, val, val2);
149                 break;
150         case IIO_CHAN_INFO_HYSTERESIS:
151                 ret_type = hid_sensor_read_raw_hyst_value(
152                         &accel_state->common_attributes, val, val2);
153                 break;
154         default:
155                 ret_type = -EINVAL;
156                 break;
157         }
158 
159         return ret_type;
160 }
161 
162 /* Channel write_raw handler */
163 static int accel_3d_write_raw(struct iio_dev *indio_dev,
164                                struct iio_chan_spec const *chan,
165                                int val,
166                                int val2,
167                                long mask)
168 {
169         struct accel_3d_state *accel_state = iio_priv(indio_dev);
170         int ret = 0;
171 
172         switch (mask) {
173         case IIO_CHAN_INFO_SAMP_FREQ:
174                 ret = hid_sensor_write_samp_freq_value(
175                                 &accel_state->common_attributes, val, val2);
176                 break;
177         case IIO_CHAN_INFO_HYSTERESIS:
178                 ret = hid_sensor_write_raw_hyst_value(
179                                 &accel_state->common_attributes, val, val2);
180                 break;
181         default:
182                 ret = -EINVAL;
183         }
184 
185         return ret;
186 }
187 
188 static const struct iio_info accel_3d_info = {
189         .driver_module = THIS_MODULE,
190         .read_raw = &accel_3d_read_raw,
191         .write_raw = &accel_3d_write_raw,
192 };
193 
194 /* Function to push data to buffer */
195 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
196         int len)
197 {
198         dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
199         iio_push_to_buffers(indio_dev, data);
200 }
201 
202 /* Callback handler to send event after all samples are received and captured */
203 static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
204                                 unsigned usage_id,
205                                 void *priv)
206 {
207         struct iio_dev *indio_dev = platform_get_drvdata(priv);
208         struct accel_3d_state *accel_state = iio_priv(indio_dev);
209 
210         dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n");
211         if (atomic_read(&accel_state->common_attributes.data_ready))
212                 hid_sensor_push_data(indio_dev,
213                                 accel_state->accel_val,
214                                 sizeof(accel_state->accel_val));
215 
216         return 0;
217 }
218 
219 /* Capture samples in local storage */
220 static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
221                                 unsigned usage_id,
222                                 size_t raw_len, char *raw_data,
223                                 void *priv)
224 {
225         struct iio_dev *indio_dev = platform_get_drvdata(priv);
226         struct accel_3d_state *accel_state = iio_priv(indio_dev);
227         int offset;
228         int ret = -EINVAL;
229 
230         switch (usage_id) {
231         case HID_USAGE_SENSOR_ACCEL_X_AXIS:
232         case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
233         case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
234                 offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
235                 accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
236                                                 *(u32 *)raw_data;
237                 ret = 0;
238         break;
239         default:
240                 break;
241         }
242 
243         return ret;
244 }
245 
246 /* Parse report which is specific to an usage id*/
247 static int accel_3d_parse_report(struct platform_device *pdev,
248                                 struct hid_sensor_hub_device *hsdev,
249                                 struct iio_chan_spec *channels,
250                                 unsigned usage_id,
251                                 struct accel_3d_state *st)
252 {
253         int ret;
254         int i;
255 
256         for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
257                 ret = sensor_hub_input_get_attribute_info(hsdev,
258                                 HID_INPUT_REPORT,
259                                 usage_id,
260                                 HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
261                                 &st->accel[CHANNEL_SCAN_INDEX_X + i]);
262                 if (ret < 0)
263                         break;
264                 accel_3d_adjust_channel_bit_mask(channels,
265                                 CHANNEL_SCAN_INDEX_X + i,
266                                 st->accel[CHANNEL_SCAN_INDEX_X + i].size);
267         }
268         dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
269                         st->accel[0].index,
270                         st->accel[0].report_id,
271                         st->accel[1].index, st->accel[1].report_id,
272                         st->accel[2].index, st->accel[2].report_id);
273 
274         st->scale_precision = hid_sensor_format_scale(
275                                 HID_USAGE_SENSOR_ACCEL_3D,
276                                 &st->accel[CHANNEL_SCAN_INDEX_X],
277                                 &st->scale_pre_decml, &st->scale_post_decml);
278 
279         /* Set Sensitivity field ids, when there is no individual modifier */
280         if (st->common_attributes.sensitivity.index < 0) {
281                 sensor_hub_input_get_attribute_info(hsdev,
282                         HID_FEATURE_REPORT, usage_id,
283                         HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
284                         HID_USAGE_SENSOR_DATA_ACCELERATION,
285                         &st->common_attributes.sensitivity);
286                 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
287                         st->common_attributes.sensitivity.index,
288                         st->common_attributes.sensitivity.report_id);
289         }
290 
291         return ret;
292 }
293 
294 /* Function to initialize the processing for usage id */
295 static int hid_accel_3d_probe(struct platform_device *pdev)
296 {
297         int ret = 0;
298         static const char *name = "accel_3d";
299         struct iio_dev *indio_dev;
300         struct accel_3d_state *accel_state;
301         struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
302         struct iio_chan_spec *channels;
303 
304         indio_dev = devm_iio_device_alloc(&pdev->dev,
305                                           sizeof(struct accel_3d_state));
306         if (indio_dev == NULL)
307                 return -ENOMEM;
308 
309         platform_set_drvdata(pdev, indio_dev);
310 
311         accel_state = iio_priv(indio_dev);
312         accel_state->common_attributes.hsdev = hsdev;
313         accel_state->common_attributes.pdev = pdev;
314 
315         ret = hid_sensor_parse_common_attributes(hsdev,
316                                         HID_USAGE_SENSOR_ACCEL_3D,
317                                         &accel_state->common_attributes);
318         if (ret) {
319                 dev_err(&pdev->dev, "failed to setup common attributes\n");
320                 return ret;
321         }
322 
323         channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels),
324                            GFP_KERNEL);
325         if (!channels) {
326                 dev_err(&pdev->dev, "failed to duplicate channels\n");
327                 return -ENOMEM;
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 
344         ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
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         atomic_set(&accel_state->common_attributes.data_ready, 0);
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;
367         ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D,
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(&accel_state->common_attributes);
380 error_unreg_buffer_funcs:
381         iio_triggered_buffer_cleanup(indio_dev);
382 error_free_dev_mem:
383         kfree(indio_dev->channels);
384         return ret;
385 }
386 
387 /* Function to deinitialize the processing for usage id */
388 static int hid_accel_3d_remove(struct platform_device *pdev)
389 {
390         struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
391         struct iio_dev *indio_dev = platform_get_drvdata(pdev);
392         struct accel_3d_state *accel_state = iio_priv(indio_dev);
393 
394         sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D);
395         iio_device_unregister(indio_dev);
396         hid_sensor_remove_trigger(&accel_state->common_attributes);
397         iio_triggered_buffer_cleanup(indio_dev);
398         kfree(indio_dev->channels);
399 
400         return 0;
401 }
402 
403 static struct platform_device_id hid_accel_3d_ids[] = {
404         {
405                 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
406                 .name = "HID-SENSOR-200073",
407         },
408         { /* sentinel */ }
409 };
410 MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
411 
412 static struct platform_driver hid_accel_3d_platform_driver = {
413         .id_table = hid_accel_3d_ids,
414         .driver = {
415                 .name   = KBUILD_MODNAME,
416                 .pm     = &hid_sensor_pm_ops,
417         },
418         .probe          = hid_accel_3d_probe,
419         .remove         = hid_accel_3d_remove,
420 };
421 module_platform_driver(hid_accel_3d_platform_driver);
422 
423 MODULE_DESCRIPTION("HID Sensor Accel 3D");
424 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
425 MODULE_LICENSE("GPL");
426 

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