145fe6f7dSsrinivas pandruvada /* 245fe6f7dSsrinivas pandruvada * HID Sensors Driver 345fe6f7dSsrinivas pandruvada * Copyright (c) 2012, Intel Corporation. 445fe6f7dSsrinivas pandruvada * 545fe6f7dSsrinivas pandruvada * This program is free software; you can redistribute it and/or modify it 645fe6f7dSsrinivas pandruvada * under the terms and conditions of the GNU General Public License, 745fe6f7dSsrinivas pandruvada * version 2, as published by the Free Software Foundation. 845fe6f7dSsrinivas pandruvada * 945fe6f7dSsrinivas pandruvada * This program is distributed in the hope it will be useful, but WITHOUT 1045fe6f7dSsrinivas pandruvada * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1145fe6f7dSsrinivas pandruvada * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1245fe6f7dSsrinivas pandruvada * more details. 1345fe6f7dSsrinivas pandruvada * 1445fe6f7dSsrinivas pandruvada * You should have received a copy of the GNU General Public License along with 1545fe6f7dSsrinivas pandruvada * this program; if not, write to the Free Software Foundation, Inc., 1645fe6f7dSsrinivas pandruvada * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 1745fe6f7dSsrinivas pandruvada * 1845fe6f7dSsrinivas pandruvada */ 1945fe6f7dSsrinivas pandruvada #include <linux/device.h> 2045fe6f7dSsrinivas pandruvada #include <linux/platform_device.h> 2145fe6f7dSsrinivas pandruvada #include <linux/module.h> 2245fe6f7dSsrinivas pandruvada #include <linux/interrupt.h> 2345fe6f7dSsrinivas pandruvada #include <linux/irq.h> 2445fe6f7dSsrinivas pandruvada #include <linux/slab.h> 2545fe6f7dSsrinivas pandruvada #include <linux/hid-sensor-hub.h> 2645fe6f7dSsrinivas pandruvada #include <linux/iio/iio.h> 2745fe6f7dSsrinivas pandruvada #include <linux/iio/sysfs.h> 2845fe6f7dSsrinivas pandruvada #include <linux/iio/buffer.h> 2945fe6f7dSsrinivas pandruvada #include <linux/iio/trigger_consumer.h> 3045fe6f7dSsrinivas pandruvada #include <linux/iio/triggered_buffer.h> 3145fe6f7dSsrinivas pandruvada #include "../common/hid-sensors/hid-sensor-trigger.h" 3245fe6f7dSsrinivas pandruvada 3345fe6f7dSsrinivas pandruvada /*Format: HID-SENSOR-usage_id_in_hex*/ 3445fe6f7dSsrinivas pandruvada /*Usage ID from spec for Accelerometer-3D: 0x200073*/ 3545fe6f7dSsrinivas pandruvada #define DRIVER_NAME "HID-SENSOR-200073" 3645fe6f7dSsrinivas pandruvada 3745fe6f7dSsrinivas pandruvada enum accel_3d_channel { 3845fe6f7dSsrinivas pandruvada CHANNEL_SCAN_INDEX_X, 3945fe6f7dSsrinivas pandruvada CHANNEL_SCAN_INDEX_Y, 4045fe6f7dSsrinivas pandruvada CHANNEL_SCAN_INDEX_Z, 4145fe6f7dSsrinivas pandruvada ACCEL_3D_CHANNEL_MAX, 4245fe6f7dSsrinivas pandruvada }; 4345fe6f7dSsrinivas pandruvada 4445fe6f7dSsrinivas pandruvada struct accel_3d_state { 4545fe6f7dSsrinivas pandruvada struct hid_sensor_hub_callbacks callbacks; 46*e07c6d17SAlexander Holler struct hid_sensor_common common_attributes; 4745fe6f7dSsrinivas pandruvada struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX]; 4845fe6f7dSsrinivas pandruvada u32 accel_val[ACCEL_3D_CHANNEL_MAX]; 4945fe6f7dSsrinivas pandruvada }; 5045fe6f7dSsrinivas pandruvada 5145fe6f7dSsrinivas pandruvada static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = { 5245fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_X_AXIS, 5345fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_Y_AXIS, 5445fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_Z_AXIS 5545fe6f7dSsrinivas pandruvada }; 5645fe6f7dSsrinivas pandruvada 5745fe6f7dSsrinivas pandruvada /* Channel definitions */ 5845fe6f7dSsrinivas pandruvada static const struct iio_chan_spec accel_3d_channels[] = { 5945fe6f7dSsrinivas pandruvada { 6045fe6f7dSsrinivas pandruvada .type = IIO_ACCEL, 6145fe6f7dSsrinivas pandruvada .modified = 1, 6245fe6f7dSsrinivas pandruvada .channel2 = IIO_MOD_X, 6345fe6f7dSsrinivas pandruvada .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT | 6445fe6f7dSsrinivas pandruvada IIO_CHAN_INFO_SCALE_SHARED_BIT | 6545fe6f7dSsrinivas pandruvada IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT | 6645fe6f7dSsrinivas pandruvada IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT, 6745fe6f7dSsrinivas pandruvada .scan_index = CHANNEL_SCAN_INDEX_X, 6845fe6f7dSsrinivas pandruvada }, { 6945fe6f7dSsrinivas pandruvada .type = IIO_ACCEL, 7045fe6f7dSsrinivas pandruvada .modified = 1, 7145fe6f7dSsrinivas pandruvada .channel2 = IIO_MOD_Y, 7245fe6f7dSsrinivas pandruvada .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT | 7345fe6f7dSsrinivas pandruvada IIO_CHAN_INFO_SCALE_SHARED_BIT | 7445fe6f7dSsrinivas pandruvada IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT | 7545fe6f7dSsrinivas pandruvada IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT, 7645fe6f7dSsrinivas pandruvada .scan_index = CHANNEL_SCAN_INDEX_Y, 7745fe6f7dSsrinivas pandruvada }, { 7845fe6f7dSsrinivas pandruvada .type = IIO_ACCEL, 7945fe6f7dSsrinivas pandruvada .modified = 1, 8045fe6f7dSsrinivas pandruvada .channel2 = IIO_MOD_Z, 8145fe6f7dSsrinivas pandruvada .info_mask = IIO_CHAN_INFO_OFFSET_SHARED_BIT | 8245fe6f7dSsrinivas pandruvada IIO_CHAN_INFO_SCALE_SHARED_BIT | 8345fe6f7dSsrinivas pandruvada IIO_CHAN_INFO_SAMP_FREQ_SHARED_BIT | 8445fe6f7dSsrinivas pandruvada IIO_CHAN_INFO_HYSTERESIS_SHARED_BIT, 8545fe6f7dSsrinivas pandruvada .scan_index = CHANNEL_SCAN_INDEX_Z, 8645fe6f7dSsrinivas pandruvada } 8745fe6f7dSsrinivas pandruvada }; 8845fe6f7dSsrinivas pandruvada 8945fe6f7dSsrinivas pandruvada /* Adjust channel real bits based on report descriptor */ 9045fe6f7dSsrinivas pandruvada static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels, 9145fe6f7dSsrinivas pandruvada int channel, int size) 9245fe6f7dSsrinivas pandruvada { 9345fe6f7dSsrinivas pandruvada channels[channel].scan_type.sign = 's'; 9445fe6f7dSsrinivas pandruvada /* Real storage bits will change based on the report desc. */ 9545fe6f7dSsrinivas pandruvada channels[channel].scan_type.realbits = size * 8; 9645fe6f7dSsrinivas pandruvada /* Maximum size of a sample to capture is u32 */ 9745fe6f7dSsrinivas pandruvada channels[channel].scan_type.storagebits = sizeof(u32) * 8; 9845fe6f7dSsrinivas pandruvada } 9945fe6f7dSsrinivas pandruvada 10045fe6f7dSsrinivas pandruvada /* Channel read_raw handler */ 10145fe6f7dSsrinivas pandruvada static int accel_3d_read_raw(struct iio_dev *indio_dev, 10245fe6f7dSsrinivas pandruvada struct iio_chan_spec const *chan, 10345fe6f7dSsrinivas pandruvada int *val, int *val2, 10445fe6f7dSsrinivas pandruvada long mask) 10545fe6f7dSsrinivas pandruvada { 10645fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state = iio_priv(indio_dev); 10745fe6f7dSsrinivas pandruvada int report_id = -1; 10845fe6f7dSsrinivas pandruvada u32 address; 10945fe6f7dSsrinivas pandruvada int ret; 11045fe6f7dSsrinivas pandruvada int ret_type; 11145fe6f7dSsrinivas pandruvada 11245fe6f7dSsrinivas pandruvada *val = 0; 11345fe6f7dSsrinivas pandruvada *val2 = 0; 11445fe6f7dSsrinivas pandruvada switch (mask) { 11545fe6f7dSsrinivas pandruvada case 0: 11645fe6f7dSsrinivas pandruvada report_id = accel_state->accel[chan->scan_index].report_id; 11745fe6f7dSsrinivas pandruvada address = accel_3d_addresses[chan->scan_index]; 11845fe6f7dSsrinivas pandruvada if (report_id >= 0) 11945fe6f7dSsrinivas pandruvada *val = sensor_hub_input_attr_get_raw_value( 12045fe6f7dSsrinivas pandruvada accel_state->common_attributes.hsdev, 12145fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_3D, address, 12245fe6f7dSsrinivas pandruvada report_id); 12345fe6f7dSsrinivas pandruvada else { 12445fe6f7dSsrinivas pandruvada *val = 0; 12545fe6f7dSsrinivas pandruvada return -EINVAL; 12645fe6f7dSsrinivas pandruvada } 12745fe6f7dSsrinivas pandruvada ret_type = IIO_VAL_INT; 12845fe6f7dSsrinivas pandruvada break; 12945fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_SCALE: 13045fe6f7dSsrinivas pandruvada *val = accel_state->accel[CHANNEL_SCAN_INDEX_X].units; 13145fe6f7dSsrinivas pandruvada ret_type = IIO_VAL_INT; 13245fe6f7dSsrinivas pandruvada break; 13345fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_OFFSET: 13445fe6f7dSsrinivas pandruvada *val = hid_sensor_convert_exponent( 13545fe6f7dSsrinivas pandruvada accel_state->accel[CHANNEL_SCAN_INDEX_X].unit_expo); 13645fe6f7dSsrinivas pandruvada ret_type = IIO_VAL_INT; 13745fe6f7dSsrinivas pandruvada break; 13845fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_SAMP_FREQ: 13945fe6f7dSsrinivas pandruvada ret = hid_sensor_read_samp_freq_value( 14045fe6f7dSsrinivas pandruvada &accel_state->common_attributes, val, val2); 14145fe6f7dSsrinivas pandruvada ret_type = IIO_VAL_INT_PLUS_MICRO; 14245fe6f7dSsrinivas pandruvada break; 14345fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_HYSTERESIS: 14445fe6f7dSsrinivas pandruvada ret = hid_sensor_read_raw_hyst_value( 14545fe6f7dSsrinivas pandruvada &accel_state->common_attributes, val, val2); 14645fe6f7dSsrinivas pandruvada ret_type = IIO_VAL_INT_PLUS_MICRO; 14745fe6f7dSsrinivas pandruvada break; 14845fe6f7dSsrinivas pandruvada default: 14945fe6f7dSsrinivas pandruvada ret_type = -EINVAL; 15045fe6f7dSsrinivas pandruvada break; 15145fe6f7dSsrinivas pandruvada } 15245fe6f7dSsrinivas pandruvada 15345fe6f7dSsrinivas pandruvada return ret_type; 15445fe6f7dSsrinivas pandruvada } 15545fe6f7dSsrinivas pandruvada 15645fe6f7dSsrinivas pandruvada /* Channel write_raw handler */ 15745fe6f7dSsrinivas pandruvada static int accel_3d_write_raw(struct iio_dev *indio_dev, 15845fe6f7dSsrinivas pandruvada struct iio_chan_spec const *chan, 15945fe6f7dSsrinivas pandruvada int val, 16045fe6f7dSsrinivas pandruvada int val2, 16145fe6f7dSsrinivas pandruvada long mask) 16245fe6f7dSsrinivas pandruvada { 16345fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state = iio_priv(indio_dev); 16445fe6f7dSsrinivas pandruvada int ret = 0; 16545fe6f7dSsrinivas pandruvada 16645fe6f7dSsrinivas pandruvada switch (mask) { 16745fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_SAMP_FREQ: 16845fe6f7dSsrinivas pandruvada ret = hid_sensor_write_samp_freq_value( 16945fe6f7dSsrinivas pandruvada &accel_state->common_attributes, val, val2); 17045fe6f7dSsrinivas pandruvada break; 17145fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_HYSTERESIS: 17245fe6f7dSsrinivas pandruvada ret = hid_sensor_write_raw_hyst_value( 17345fe6f7dSsrinivas pandruvada &accel_state->common_attributes, val, val2); 17445fe6f7dSsrinivas pandruvada break; 17545fe6f7dSsrinivas pandruvada default: 17645fe6f7dSsrinivas pandruvada ret = -EINVAL; 17745fe6f7dSsrinivas pandruvada } 17845fe6f7dSsrinivas pandruvada 17945fe6f7dSsrinivas pandruvada return ret; 18045fe6f7dSsrinivas pandruvada } 18145fe6f7dSsrinivas pandruvada 18245fe6f7dSsrinivas pandruvada static int accel_3d_write_raw_get_fmt(struct iio_dev *indio_dev, 18345fe6f7dSsrinivas pandruvada struct iio_chan_spec const *chan, 18445fe6f7dSsrinivas pandruvada long mask) 18545fe6f7dSsrinivas pandruvada { 18645fe6f7dSsrinivas pandruvada return IIO_VAL_INT_PLUS_MICRO; 18745fe6f7dSsrinivas pandruvada } 18845fe6f7dSsrinivas pandruvada 18945fe6f7dSsrinivas pandruvada static const struct iio_info accel_3d_info = { 19045fe6f7dSsrinivas pandruvada .driver_module = THIS_MODULE, 19145fe6f7dSsrinivas pandruvada .read_raw = &accel_3d_read_raw, 19245fe6f7dSsrinivas pandruvada .write_raw = &accel_3d_write_raw, 19345fe6f7dSsrinivas pandruvada .write_raw_get_fmt = &accel_3d_write_raw_get_fmt, 19445fe6f7dSsrinivas pandruvada }; 19545fe6f7dSsrinivas pandruvada 19645fe6f7dSsrinivas pandruvada /* Function to push data to buffer */ 19745fe6f7dSsrinivas pandruvada static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len) 19845fe6f7dSsrinivas pandruvada { 19945fe6f7dSsrinivas pandruvada dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); 20084b36ce5SJonathan Cameron iio_push_to_buffers(indio_dev, (u8 *)data); 20145fe6f7dSsrinivas pandruvada } 20245fe6f7dSsrinivas pandruvada 20345fe6f7dSsrinivas pandruvada /* Callback handler to send event after all samples are received and captured */ 20445fe6f7dSsrinivas pandruvada static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev, 20545fe6f7dSsrinivas pandruvada unsigned usage_id, 20645fe6f7dSsrinivas pandruvada void *priv) 20745fe6f7dSsrinivas pandruvada { 20845fe6f7dSsrinivas pandruvada struct iio_dev *indio_dev = platform_get_drvdata(priv); 20945fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state = iio_priv(indio_dev); 21045fe6f7dSsrinivas pandruvada 21145fe6f7dSsrinivas pandruvada dev_dbg(&indio_dev->dev, "accel_3d_proc_event [%d]\n", 21245fe6f7dSsrinivas pandruvada accel_state->common_attributes.data_ready); 21345fe6f7dSsrinivas pandruvada if (accel_state->common_attributes.data_ready) 21445fe6f7dSsrinivas pandruvada hid_sensor_push_data(indio_dev, 21545fe6f7dSsrinivas pandruvada (u8 *)accel_state->accel_val, 21645fe6f7dSsrinivas pandruvada sizeof(accel_state->accel_val)); 21745fe6f7dSsrinivas pandruvada 21845fe6f7dSsrinivas pandruvada return 0; 21945fe6f7dSsrinivas pandruvada } 22045fe6f7dSsrinivas pandruvada 22145fe6f7dSsrinivas pandruvada /* Capture samples in local storage */ 22245fe6f7dSsrinivas pandruvada static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev, 22345fe6f7dSsrinivas pandruvada unsigned usage_id, 22445fe6f7dSsrinivas pandruvada size_t raw_len, char *raw_data, 22545fe6f7dSsrinivas pandruvada void *priv) 22645fe6f7dSsrinivas pandruvada { 22745fe6f7dSsrinivas pandruvada struct iio_dev *indio_dev = platform_get_drvdata(priv); 22845fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state = iio_priv(indio_dev); 22945fe6f7dSsrinivas pandruvada int offset; 23045fe6f7dSsrinivas pandruvada int ret = -EINVAL; 23145fe6f7dSsrinivas pandruvada 23245fe6f7dSsrinivas pandruvada switch (usage_id) { 23345fe6f7dSsrinivas pandruvada case HID_USAGE_SENSOR_ACCEL_X_AXIS: 23445fe6f7dSsrinivas pandruvada case HID_USAGE_SENSOR_ACCEL_Y_AXIS: 23545fe6f7dSsrinivas pandruvada case HID_USAGE_SENSOR_ACCEL_Z_AXIS: 23645fe6f7dSsrinivas pandruvada offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS; 23745fe6f7dSsrinivas pandruvada accel_state->accel_val[CHANNEL_SCAN_INDEX_X + offset] = 23845fe6f7dSsrinivas pandruvada *(u32 *)raw_data; 23945fe6f7dSsrinivas pandruvada ret = 0; 24045fe6f7dSsrinivas pandruvada break; 24145fe6f7dSsrinivas pandruvada default: 24245fe6f7dSsrinivas pandruvada break; 24345fe6f7dSsrinivas pandruvada } 24445fe6f7dSsrinivas pandruvada 24545fe6f7dSsrinivas pandruvada return ret; 24645fe6f7dSsrinivas pandruvada } 24745fe6f7dSsrinivas pandruvada 24845fe6f7dSsrinivas pandruvada /* Parse report which is specific to an usage id*/ 24945fe6f7dSsrinivas pandruvada static int accel_3d_parse_report(struct platform_device *pdev, 25045fe6f7dSsrinivas pandruvada struct hid_sensor_hub_device *hsdev, 25145fe6f7dSsrinivas pandruvada struct iio_chan_spec *channels, 25245fe6f7dSsrinivas pandruvada unsigned usage_id, 25345fe6f7dSsrinivas pandruvada struct accel_3d_state *st) 25445fe6f7dSsrinivas pandruvada { 25545fe6f7dSsrinivas pandruvada int ret; 25645fe6f7dSsrinivas pandruvada int i; 25745fe6f7dSsrinivas pandruvada 25845fe6f7dSsrinivas pandruvada for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) { 25945fe6f7dSsrinivas pandruvada ret = sensor_hub_input_get_attribute_info(hsdev, 26045fe6f7dSsrinivas pandruvada HID_INPUT_REPORT, 26145fe6f7dSsrinivas pandruvada usage_id, 26245fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_X_AXIS + i, 26345fe6f7dSsrinivas pandruvada &st->accel[CHANNEL_SCAN_INDEX_X + i]); 26445fe6f7dSsrinivas pandruvada if (ret < 0) 26545fe6f7dSsrinivas pandruvada break; 26645fe6f7dSsrinivas pandruvada accel_3d_adjust_channel_bit_mask(channels, 26745fe6f7dSsrinivas pandruvada CHANNEL_SCAN_INDEX_X + i, 26845fe6f7dSsrinivas pandruvada st->accel[CHANNEL_SCAN_INDEX_X + i].size); 26945fe6f7dSsrinivas pandruvada } 27045fe6f7dSsrinivas pandruvada dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n", 27145fe6f7dSsrinivas pandruvada st->accel[0].index, 27245fe6f7dSsrinivas pandruvada st->accel[0].report_id, 27345fe6f7dSsrinivas pandruvada st->accel[1].index, st->accel[1].report_id, 27445fe6f7dSsrinivas pandruvada st->accel[2].index, st->accel[2].report_id); 27545fe6f7dSsrinivas pandruvada 27645fe6f7dSsrinivas pandruvada return ret; 27745fe6f7dSsrinivas pandruvada } 27845fe6f7dSsrinivas pandruvada 27945fe6f7dSsrinivas pandruvada /* Function to initialize the processing for usage id */ 28045fe6f7dSsrinivas pandruvada static int __devinit hid_accel_3d_probe(struct platform_device *pdev) 28145fe6f7dSsrinivas pandruvada { 28245fe6f7dSsrinivas pandruvada int ret = 0; 28345fe6f7dSsrinivas pandruvada static const char *name = "accel_3d"; 28445fe6f7dSsrinivas pandruvada struct iio_dev *indio_dev; 28545fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state; 28645fe6f7dSsrinivas pandruvada struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 28745fe6f7dSsrinivas pandruvada struct iio_chan_spec *channels; 28845fe6f7dSsrinivas pandruvada 28945fe6f7dSsrinivas pandruvada indio_dev = iio_device_alloc(sizeof(struct accel_3d_state)); 29045fe6f7dSsrinivas pandruvada if (indio_dev == NULL) { 29145fe6f7dSsrinivas pandruvada ret = -ENOMEM; 29245fe6f7dSsrinivas pandruvada goto error_ret; 29345fe6f7dSsrinivas pandruvada } 29445fe6f7dSsrinivas pandruvada platform_set_drvdata(pdev, indio_dev); 29545fe6f7dSsrinivas pandruvada 29645fe6f7dSsrinivas pandruvada accel_state = iio_priv(indio_dev); 29745fe6f7dSsrinivas pandruvada accel_state->common_attributes.hsdev = hsdev; 29845fe6f7dSsrinivas pandruvada accel_state->common_attributes.pdev = pdev; 29945fe6f7dSsrinivas pandruvada 30045fe6f7dSsrinivas pandruvada ret = hid_sensor_parse_common_attributes(hsdev, 30145fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_3D, 30245fe6f7dSsrinivas pandruvada &accel_state->common_attributes); 30345fe6f7dSsrinivas pandruvada if (ret) { 30445fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "failed to setup common attributes\n"); 30545fe6f7dSsrinivas pandruvada goto error_free_dev; 30645fe6f7dSsrinivas pandruvada } 30745fe6f7dSsrinivas pandruvada 30864ebe955SAxel Lin channels = kmemdup(accel_3d_channels, sizeof(accel_3d_channels), 30945fe6f7dSsrinivas pandruvada GFP_KERNEL); 31045fe6f7dSsrinivas pandruvada if (!channels) { 31164ebe955SAxel Lin ret = -ENOMEM; 31245fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "failed to duplicate channels\n"); 31345fe6f7dSsrinivas pandruvada goto error_free_dev; 31445fe6f7dSsrinivas pandruvada } 31545fe6f7dSsrinivas pandruvada 31645fe6f7dSsrinivas pandruvada ret = accel_3d_parse_report(pdev, hsdev, channels, 31745fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_3D, accel_state); 31845fe6f7dSsrinivas pandruvada if (ret) { 31945fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "failed to setup attributes\n"); 32045fe6f7dSsrinivas pandruvada goto error_free_dev_mem; 32145fe6f7dSsrinivas pandruvada } 32245fe6f7dSsrinivas pandruvada 32345fe6f7dSsrinivas pandruvada indio_dev->channels = channels; 32445fe6f7dSsrinivas pandruvada indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels); 32545fe6f7dSsrinivas pandruvada indio_dev->dev.parent = &pdev->dev; 32645fe6f7dSsrinivas pandruvada indio_dev->info = &accel_3d_info; 32745fe6f7dSsrinivas pandruvada indio_dev->name = name; 32845fe6f7dSsrinivas pandruvada indio_dev->modes = INDIO_DIRECT_MODE; 32945fe6f7dSsrinivas pandruvada 33045fe6f7dSsrinivas pandruvada ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 33145fe6f7dSsrinivas pandruvada NULL, NULL); 33245fe6f7dSsrinivas pandruvada if (ret) { 33345fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 33445fe6f7dSsrinivas pandruvada goto error_free_dev_mem; 33545fe6f7dSsrinivas pandruvada } 33645fe6f7dSsrinivas pandruvada accel_state->common_attributes.data_ready = false; 33745fe6f7dSsrinivas pandruvada ret = hid_sensor_setup_trigger(indio_dev, name, 33845fe6f7dSsrinivas pandruvada &accel_state->common_attributes); 33945fe6f7dSsrinivas pandruvada if (ret < 0) { 34045fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "trigger setup failed\n"); 34145fe6f7dSsrinivas pandruvada goto error_unreg_buffer_funcs; 34245fe6f7dSsrinivas pandruvada } 34345fe6f7dSsrinivas pandruvada 34445fe6f7dSsrinivas pandruvada ret = iio_device_register(indio_dev); 34545fe6f7dSsrinivas pandruvada if (ret) { 34645fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "device register failed\n"); 34745fe6f7dSsrinivas pandruvada goto error_remove_trigger; 34845fe6f7dSsrinivas pandruvada } 34945fe6f7dSsrinivas pandruvada 35045fe6f7dSsrinivas pandruvada accel_state->callbacks.send_event = accel_3d_proc_event; 35145fe6f7dSsrinivas pandruvada accel_state->callbacks.capture_sample = accel_3d_capture_sample; 35245fe6f7dSsrinivas pandruvada accel_state->callbacks.pdev = pdev; 35345fe6f7dSsrinivas pandruvada ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D, 35445fe6f7dSsrinivas pandruvada &accel_state->callbacks); 35545fe6f7dSsrinivas pandruvada if (ret < 0) { 35645fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "callback reg failed\n"); 35745fe6f7dSsrinivas pandruvada goto error_iio_unreg; 35845fe6f7dSsrinivas pandruvada } 35945fe6f7dSsrinivas pandruvada 36045fe6f7dSsrinivas pandruvada return ret; 36145fe6f7dSsrinivas pandruvada 36245fe6f7dSsrinivas pandruvada error_iio_unreg: 36345fe6f7dSsrinivas pandruvada iio_device_unregister(indio_dev); 36445fe6f7dSsrinivas pandruvada error_remove_trigger: 36545fe6f7dSsrinivas pandruvada hid_sensor_remove_trigger(indio_dev); 36645fe6f7dSsrinivas pandruvada error_unreg_buffer_funcs: 36745fe6f7dSsrinivas pandruvada iio_triggered_buffer_cleanup(indio_dev); 36845fe6f7dSsrinivas pandruvada error_free_dev_mem: 36945fe6f7dSsrinivas pandruvada kfree(indio_dev->channels); 37045fe6f7dSsrinivas pandruvada error_free_dev: 37145fe6f7dSsrinivas pandruvada iio_device_free(indio_dev); 37245fe6f7dSsrinivas pandruvada error_ret: 37345fe6f7dSsrinivas pandruvada return ret; 37445fe6f7dSsrinivas pandruvada } 37545fe6f7dSsrinivas pandruvada 37645fe6f7dSsrinivas pandruvada /* Function to deinitialize the processing for usage id */ 37745fe6f7dSsrinivas pandruvada static int __devinit hid_accel_3d_remove(struct platform_device *pdev) 37845fe6f7dSsrinivas pandruvada { 37945fe6f7dSsrinivas pandruvada struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 38045fe6f7dSsrinivas pandruvada struct iio_dev *indio_dev = platform_get_drvdata(pdev); 38145fe6f7dSsrinivas pandruvada 38245fe6f7dSsrinivas pandruvada sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ACCEL_3D); 38345fe6f7dSsrinivas pandruvada iio_device_unregister(indio_dev); 38445fe6f7dSsrinivas pandruvada hid_sensor_remove_trigger(indio_dev); 38545fe6f7dSsrinivas pandruvada iio_triggered_buffer_cleanup(indio_dev); 38645fe6f7dSsrinivas pandruvada kfree(indio_dev->channels); 38745fe6f7dSsrinivas pandruvada iio_device_free(indio_dev); 38845fe6f7dSsrinivas pandruvada 38945fe6f7dSsrinivas pandruvada return 0; 39045fe6f7dSsrinivas pandruvada } 39145fe6f7dSsrinivas pandruvada 39245fe6f7dSsrinivas pandruvada static struct platform_driver hid_accel_3d_platform_driver = { 39345fe6f7dSsrinivas pandruvada .driver = { 39445fe6f7dSsrinivas pandruvada .name = DRIVER_NAME, 39545fe6f7dSsrinivas pandruvada .owner = THIS_MODULE, 39645fe6f7dSsrinivas pandruvada }, 39745fe6f7dSsrinivas pandruvada .probe = hid_accel_3d_probe, 39845fe6f7dSsrinivas pandruvada .remove = hid_accel_3d_remove, 39945fe6f7dSsrinivas pandruvada }; 40045fe6f7dSsrinivas pandruvada module_platform_driver(hid_accel_3d_platform_driver); 40145fe6f7dSsrinivas pandruvada 40245fe6f7dSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Accel 3D"); 40345fe6f7dSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 40445fe6f7dSsrinivas pandruvada MODULE_LICENSE("GPL"); 405