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/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"
34 enum accel_3d_channel {
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];
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
58 /* Channel definitions */
59 static const struct iio_chan_spec accel_3d_channels[] = {
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,
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,
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,
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)
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;
104 /* Channel read_raw handler */
105 static int accel_3d_read_raw(struct iio_dev *indio_dev,
106 struct iio_chan_spec const *chan,
110 struct accel_3d_state *accel_state = iio_priv(indio_dev);
121 poll_value = hid_sensor_read_poll_value(
122 &accel_state->common_attributes);
126 hid_sensor_power_state(&accel_state->common_attributes, true);
127 msleep_interruptible(poll_value * 2);
128 report_id = accel_state->accel[chan->scan_index].report_id;
129 address = accel_3d_addresses[chan->scan_index];
131 *val = sensor_hub_input_attr_get_raw_value(
132 accel_state->common_attributes.hsdev,
133 HID_USAGE_SENSOR_ACCEL_3D, address,
137 hid_sensor_power_state(&accel_state->common_attributes,
141 hid_sensor_power_state(&accel_state->common_attributes, false);
142 ret_type = IIO_VAL_INT;
144 case IIO_CHAN_INFO_SCALE:
145 *val = accel_state->scale_pre_decml;
146 *val2 = accel_state->scale_post_decml;
147 ret_type = accel_state->scale_precision;
149 case IIO_CHAN_INFO_OFFSET:
150 *val = accel_state->value_offset;
151 ret_type = IIO_VAL_INT;
153 case IIO_CHAN_INFO_SAMP_FREQ:
154 ret = hid_sensor_read_samp_freq_value(
155 &accel_state->common_attributes, val, val2);
156 ret_type = IIO_VAL_INT_PLUS_MICRO;
158 case IIO_CHAN_INFO_HYSTERESIS:
159 ret = hid_sensor_read_raw_hyst_value(
160 &accel_state->common_attributes, val, val2);
161 ret_type = IIO_VAL_INT_PLUS_MICRO;
171 /* Channel write_raw handler */
172 static int accel_3d_write_raw(struct iio_dev *indio_dev,
173 struct iio_chan_spec const *chan,
178 struct accel_3d_state *accel_state = iio_priv(indio_dev);
182 case IIO_CHAN_INFO_SAMP_FREQ:
183 ret = hid_sensor_write_samp_freq_value(
184 &accel_state->common_attributes, val, val2);
186 case IIO_CHAN_INFO_HYSTERESIS:
187 ret = hid_sensor_write_raw_hyst_value(
188 &accel_state->common_attributes, val, val2);
197 static const struct iio_info accel_3d_info = {
198 .driver_module = THIS_MODULE,
199 .read_raw = &accel_3d_read_raw,
200 .write_raw = &accel_3d_write_raw,
203 /* Function to push data to buffer */
204 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
207 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
208 iio_push_to_buffers(indio_dev, data);
211 /* Callback handler to send event after all samples are received and captured */
212 static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev,
216 struct iio_dev *indio_dev = platform_get_drvdata(priv);
217 struct accel_3d_state *accel_state = iio_priv(indio_dev);
219 dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n");
220 if (atomic_read(&accel_state->common_attributes.data_ready))
221 hid_sensor_push_data(indio_dev,
222 accel_state->accel_val,
223 sizeof(accel_state->accel_val));
228 /* Capture samples in local storage */
229 static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev,
231 size_t raw_len, char *raw_data,
234 struct iio_dev *indio_dev = platform_get_drvdata(priv);
235 struct accel_3d_state *accel_state = iio_priv(indio_dev);
240 case HID_USAGE_SENSOR_ACCEL_X_AXIS:
241 case HID_USAGE_SENSOR_ACCEL_Y_AXIS:
242 case HID_USAGE_SENSOR_ACCEL_Z_AXIS:
243 offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS;
244 accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] =
255 /* Parse report which is specific to an usage id*/
256 static int accel_3d_parse_report(struct platform_device *pdev,
257 struct hid_sensor_hub_device *hsdev,
258 struct iio_chan_spec *channels,
260 struct accel_3d_state *st)
265 for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) {
266 ret = sensor_hub_input_get_attribute_info(hsdev,
269 HID_USAGE_SENSOR_ACCEL_X_AXIS + i,
270 &st->accel[CHANNEL_SCAN_INDEX_X + i]);
273 accel_3d_adjust_channel_bit_mask(channels,
274 CHANNEL_SCAN_INDEX_X + i,
275 st->accel[CHANNEL_SCAN_INDEX_X + i].size);
277 dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n",
279 st->accel[0].report_id,
280 st->accel[1].index, st->accel[1].report_id,
281 st->accel[2].index, st->accel[2].report_id);
283 st->scale_precision = hid_sensor_format_scale(
284 HID_USAGE_SENSOR_ACCEL_3D,
285 &st->accel[CHANNEL_SCAN_INDEX_X],
286 &st->scale_pre_decml, &st->scale_post_decml);
288 /* Set Sensitivity field ids, when there is no individual modifier */
289 if (st->common_attributes.sensitivity.index < 0) {
290 sensor_hub_input_get_attribute_info(hsdev,
291 HID_FEATURE_REPORT, usage_id,
292 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
293 HID_USAGE_SENSOR_DATA_ACCELERATION,
294 &st->common_attributes.sensitivity);
295 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
296 st->common_attributes.sensitivity.index,
297 st->common_attributes.sensitivity.report_id);
303 /* Function to initialize the processing for usage id */
304 static int hid_accel_3d_probe(struct platform_device *pdev)
307 static const char *name = "accel_3d";
308 struct iio_dev *indio_dev;
309 struct accel_3d_state *accel_state;
310 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
311 struct iio_chan_spec *channels;
313 indio_dev = devm_iio_device_alloc(&pdev->dev,
314 sizeof(struct accel_3d_state));
315 if (indio_dev == NULL)
318 platform_set_drvdata(pdev, indio_dev);
320 accel_state = iio_priv(indio_dev);
321 accel_state->common_attributes.hsdev = hsdev;
322 accel_state->common_attributes.pdev = pdev;
324 ret = hid_sensor_parse_common_attributes(hsdev,
325 HID_USAGE_SENSOR_ACCEL_3D,
326 &accel_state->common_attributes);
328 dev_err(&pdev->dev, "failed to setup common attributes\n");
332 channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels),
335 dev_err(&pdev->dev, "failed to duplicate channels\n");
339 ret = accel_3d_parse_report(pdev, hsdev, channels,
340 HID_USAGE_SENSOR_ACCEL_3D, accel_state);
342 dev_err(&pdev->dev, "failed to setup attributes\n");
343 goto error_free_dev_mem;
346 indio_dev->channels = channels;
347 indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels);
348 indio_dev->dev.parent = &pdev->dev;
349 indio_dev->info = &accel_3d_info;
350 indio_dev->name = name;
351 indio_dev->modes = INDIO_DIRECT_MODE;
353 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
356 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
357 goto error_free_dev_mem;
359 atomic_set(&accel_state->common_attributes.data_ready, 0);
360 ret = hid_sensor_setup_trigger(indio_dev, name,
361 &accel_state->common_attributes);
363 dev_err(&pdev->dev, "trigger setup failed\n");
364 goto error_unreg_buffer_funcs;
367 ret = iio_device_register(indio_dev);
369 dev_err(&pdev->dev, "device register failed\n");
370 goto error_remove_trigger;
373 accel_state->callbacks.send_event = accel_3d_proc_event;
374 accel_state->callbacks.capture_sample = accel_3d_capture_sample;
375 accel_state->callbacks.pdev = pdev;
376 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D,
377 &accel_state->callbacks);
379 dev_err(&pdev->dev, "callback reg failed\n");
380 goto error_iio_unreg;
386 iio_device_unregister(indio_dev);
387 error_remove_trigger:
388 hid_sensor_remove_trigger(&accel_state->common_attributes);
389 error_unreg_buffer_funcs:
390 iio_triggered_buffer_cleanup(indio_dev);
392 kfree(indio_dev->channels);
396 /* Function to deinitialize the processing for usage id */
397 static int hid_accel_3d_remove(struct platform_device *pdev)
399 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
400 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
401 struct accel_3d_state *accel_state = iio_priv(indio_dev);
403 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D);
404 iio_device_unregister(indio_dev);
405 hid_sensor_remove_trigger(&accel_state->common_attributes);
406 iio_triggered_buffer_cleanup(indio_dev);
407 kfree(indio_dev->channels);
412 static struct platform_device_id hid_accel_3d_ids[] = {
414 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
415 .name = "HID-SENSOR-200073",
419 MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids);
421 static struct platform_driver hid_accel_3d_platform_driver = {
422 .id_table = hid_accel_3d_ids,
424 .name = KBUILD_MODNAME,
425 .owner = THIS_MODULE,
427 .probe = hid_accel_3d_probe,
428 .remove = hid_accel_3d_remove,
430 module_platform_driver(hid_accel_3d_platform_driver);
432 MODULE_DESCRIPTION("HID Sensor Accel 3D");
433 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
434 MODULE_LICENSE("GPL");