3 * Copyright (c) 2012, Intel Corporation.
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.
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
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.
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>
31 #include "../common/hid-sensors/hid-sensor-trigger.h"
33 enum accel_3d_channel {
40 struct accel_3d_state {
41 struct hid_sensor_hub_callbacks callbacks;
42 struct hid_sensor_common common_attributes;
43 struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX];
44 u32 accel_val[ACCEL_3D_CHANNEL_MAX];
47 static 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
53 /* Channel definitions */
54 static const struct iio_chan_spec accel_3d_channels[] = {
58 .channel2 = IIO_MOD_X,
59 .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),
63 .scan_index = CHANNEL_SCAN_INDEX_X,
67 .channel2 = IIO_MOD_Y,
68 .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),
72 .scan_index = CHANNEL_SCAN_INDEX_Y,
76 .channel2 = IIO_MOD_Z,
77 .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),
81 .scan_index = CHANNEL_SCAN_INDEX_Z,
85 /* Adjust channel real bits based on report descriptor */
86 static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels,
87 int channel, int size)
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;
96 /* Channel read_raw handler */
97 static int accel_3d_read_raw(struct iio_dev *indio_dev,
98 struct iio_chan_spec const *chan,
102 struct accel_3d_state *accel_state = iio_priv(indio_dev);
112 report_id = accel_state->accel[chan->scan_index].report_id;
113 address = accel_3d_addresses[chan->scan_index];
115 *val = sensor_hub_input_attr_get_raw_value(
116 accel_state->common_attributes.hsdev,
117 HID_USAGE_SENSOR_ACCEL_3D, address,
123 ret_type = IIO_VAL_INT;
125 case IIO_CHAN_INFO_SCALE:
126 *val = accel_state->accel[CHANNEL_SCAN_INDEX_X].units;
127 ret_type = IIO_VAL_INT;
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;
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;
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;
152 /* Channel write_raw handler */
153 static int accel_3d_write_raw(struct iio_dev *indio_dev,
154 struct iio_chan_spec const *chan,
159 struct accel_3d_state *accel_state = iio_priv(indio_dev);
163 case IIO_CHAN_INFO_SAMP_FREQ:
164 ret = hid_sensor_write_samp_freq_value(
165 &accel_state->common_attributes, val, val2);
167 case IIO_CHAN_INFO_HYSTERESIS:
168 ret = hid_sensor_write_raw_hyst_value(
169 &accel_state->common_attributes, val, val2);
178 static int accel_3d_write_raw_get_fmt(struct iio_dev *indio_dev,
179 struct iio_chan_spec const *chan,
182 return IIO_VAL_INT_PLUS_MICRO;
185 static const struct iio_info accel_3d_info = {
186 .driver_module = THIS_MODULE,
187 .read_raw = &accel_3d_read_raw,
188 .write_raw = &accel_3d_write_raw,
189 .write_raw_get_fmt = &accel_3d_write_raw_get_fmt,
192 /* Function to push data to buffer */
193 static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
195 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
196 iio_push_to_buffers(indio_dev, (u8 *)data);
199 /* Callback handler to send event after all samples are received and captured */
200 static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
204 struct iio_dev *indio_dev = platform_get_drvdata(priv);
205 struct accel_3d_state *accel_state = iio_priv(indio_dev);
207 dev_dbg(&indio_dev->dev, "accel_3d_proc_event [%d]\n",
208 accel_state->common_attributes.data_ready);
209 if (accel_state->common_attributes.data_ready)
210 hid_sensor_push_data(indio_dev,
211 (u8 *)accel_state->accel_val,
212 sizeof(accel_state->accel_val));
217 /* Capture samples in local storage */
218 static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
220 size_t raw_len, char *raw_data,
223 struct iio_dev *indio_dev = platform_get_drvdata(priv);
224 struct accel_3d_state *accel_state = iio_priv(indio_dev);
229 case HID_USAGE_SENSOR_ACCEL_X_AXIS:
230 case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
231 case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
232 offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
233 accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
244 /* Parse report which is specific to an usage id*/
245 static int accel_3d_parse_report(struct platform_device *pdev,
246 struct hid_sensor_hub_device *hsdev,
247 struct iio_chan_spec *channels,
249 struct accel_3d_state *st)
254 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
255 ret = sensor_hub_input_get_attribute_info(hsdev,
258 HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
259 &st->accel[CHANNEL_SCAN_INDEX_X + i]);
262 accel_3d_adjust_channel_bit_mask(channels,
263 CHANNEL_SCAN_INDEX_X + i,
264 st->accel[CHANNEL_SCAN_INDEX_X + i].size);
266 dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
268 st->accel[0].report_id,
269 st->accel[1].index, st->accel[1].report_id,
270 st->accel[2].index, st->accel[2].report_id);
275 /* Function to initialize the processing for usage id */
276 static int hid_accel_3d_probe(struct platform_device *pdev)
279 static const char *name = "accel_3d";
280 struct iio_dev *indio_dev;
281 struct accel_3d_state *accel_state;
282 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
283 struct iio_chan_spec *channels;
285 indio_dev = iio_device_alloc(sizeof(struct accel_3d_state));
286 if (indio_dev == NULL) {
290 platform_set_drvdata(pdev, indio_dev);
292 accel_state = iio_priv(indio_dev);
293 accel_state->common_attributes.hsdev = hsdev;
294 accel_state->common_attributes.pdev = pdev;
296 ret = hid_sensor_parse_common_attributes(hsdev,
297 HID_USAGE_SENSOR_ACCEL_3D,
298 &accel_state->common_attributes);
300 dev_err(&pdev->dev, "failed to setup common attributes\n");
304 channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels),
308 dev_err(&pdev->dev, "failed to duplicate channels\n");
312 ret = accel_3d_parse_report(pdev, hsdev, channels,
313 HID_USAGE_SENSOR_ACCEL_3D, accel_state);
315 dev_err(&pdev->dev, "failed to setup attributes\n");
316 goto error_free_dev_mem;
319 indio_dev->channels = channels;
320 indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels);
321 indio_dev->dev.parent = &pdev->dev;
322 indio_dev->info = &accel_3d_info;
323 indio_dev->name = name;
324 indio_dev->modes = INDIO_DIRECT_MODE;
326 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
329 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
330 goto error_free_dev_mem;
332 accel_state->common_attributes.data_ready = false;
333 ret = hid_sensor_setup_trigger(indio_dev, name,
334 &accel_state->common_attributes);
336 dev_err(&pdev->dev, "trigger setup failed\n");
337 goto error_unreg_buffer_funcs;
340 ret = iio_device_register(indio_dev);
342 dev_err(&pdev->dev, "device register failed\n");
343 goto error_remove_trigger;
346 accel_state->callbacks.send_event = accel_3d_proc_event;
347 accel_state->callbacks.capture_sample = accel_3d_capture_sample;
348 accel_state->callbacks.pdev = pdev;
349 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D,
350 &accel_state->callbacks);
352 dev_err(&pdev->dev, "callback reg failed\n");
353 goto error_iio_unreg;
359 iio_device_unregister(indio_dev);
360 error_remove_trigger:
361 hid_sensor_remove_trigger(indio_dev);
362 error_unreg_buffer_funcs:
363 iio_triggered_buffer_cleanup(indio_dev);
365 kfree(indio_dev->channels);
367 iio_device_free(indio_dev);
372 /* Function to deinitialize the processing for usage id */
373 static int hid_accel_3d_remove(struct platform_device *pdev)
375 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
376 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
378 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D);
379 iio_device_unregister(indio_dev);
380 hid_sensor_remove_trigger(indio_dev);
381 iio_triggered_buffer_cleanup(indio_dev);
382 kfree(indio_dev->channels);
383 iio_device_free(indio_dev);
388 static struct platform_device_id hid_accel_3d_ids[] = {
390 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
391 .name = "HID-SENSOR-200073",
395 MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
397 static struct platform_driver hid_accel_3d_platform_driver = {
398 .id_table = hid_accel_3d_ids,
400 .name = KBUILD_MODNAME,
401 .owner = THIS_MODULE,
403 .probe = hid_accel_3d_probe,
404 .remove = hid_accel_3d_remove,
406 module_platform_driver(hid_accel_3d_platform_driver);
408 MODULE_DESCRIPTION("HID Sensor Accel 3D");
409 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
410 MODULE_LICENSE("GPL");