1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 245fe6f7dSsrinivas pandruvada /* 345fe6f7dSsrinivas pandruvada * HID Sensors Driver 445fe6f7dSsrinivas pandruvada * Copyright (c) 2012, Intel Corporation. 545fe6f7dSsrinivas pandruvada */ 645fe6f7dSsrinivas pandruvada #include <linux/device.h> 745fe6f7dSsrinivas pandruvada #include <linux/platform_device.h> 845fe6f7dSsrinivas pandruvada #include <linux/module.h> 9fb226ae7SJonathan Cameron #include <linux/mod_devicetable.h> 1045fe6f7dSsrinivas pandruvada #include <linux/slab.h> 1145fe6f7dSsrinivas pandruvada #include <linux/hid-sensor-hub.h> 1245fe6f7dSsrinivas pandruvada #include <linux/iio/iio.h> 1345fe6f7dSsrinivas pandruvada #include <linux/iio/buffer.h> 1445fe6f7dSsrinivas pandruvada #include "../common/hid-sensors/hid-sensor-trigger.h" 1545fe6f7dSsrinivas pandruvada 1645fe6f7dSsrinivas pandruvada enum accel_3d_channel { 1745fe6f7dSsrinivas pandruvada CHANNEL_SCAN_INDEX_X, 1845fe6f7dSsrinivas pandruvada CHANNEL_SCAN_INDEX_Y, 1945fe6f7dSsrinivas pandruvada CHANNEL_SCAN_INDEX_Z, 2045fe6f7dSsrinivas pandruvada ACCEL_3D_CHANNEL_MAX, 2145fe6f7dSsrinivas pandruvada }; 2245fe6f7dSsrinivas pandruvada 234c261720SYe Xiang #define CHANNEL_SCAN_INDEX_TIMESTAMP ACCEL_3D_CHANNEL_MAX 2445fe6f7dSsrinivas pandruvada struct accel_3d_state { 2545fe6f7dSsrinivas pandruvada struct hid_sensor_hub_callbacks callbacks; 26e07c6d17SAlexander Holler struct hid_sensor_common common_attributes; 2745fe6f7dSsrinivas pandruvada struct hid_sensor_hub_attribute_info accel[ACCEL_3D_CHANNEL_MAX]; 28c6559bf7SJonathan Cameron /* Ensure timestamp is naturally aligned */ 29c6559bf7SJonathan Cameron struct { 30c6559bf7SJonathan Cameron u32 accel_val[3]; 31c6559bf7SJonathan Cameron s64 timestamp __aligned(8); 32c6559bf7SJonathan Cameron } scan; 3373a24988SSrinivas Pandruvada int scale_pre_decml; 3473a24988SSrinivas Pandruvada int scale_post_decml; 3573a24988SSrinivas Pandruvada int scale_precision; 3673a24988SSrinivas Pandruvada int value_offset; 37a96cd0f9SSrinivas Pandruvada int64_t timestamp; 3845fe6f7dSsrinivas pandruvada }; 3945fe6f7dSsrinivas pandruvada 4045fe6f7dSsrinivas pandruvada static const u32 accel_3d_addresses[ACCEL_3D_CHANNEL_MAX] = { 4145fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_X_AXIS, 4245fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_Y_AXIS, 4345fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_Z_AXIS 4445fe6f7dSsrinivas pandruvada }; 4545fe6f7dSsrinivas pandruvada 460e41fd51SYe Xiang static const u32 accel_3d_sensitivity_addresses[] = { 470e41fd51SYe Xiang HID_USAGE_SENSOR_DATA_ACCELERATION, 480e41fd51SYe Xiang }; 490e41fd51SYe Xiang 5045fe6f7dSsrinivas pandruvada /* Channel definitions */ 5145fe6f7dSsrinivas pandruvada static const struct iio_chan_spec accel_3d_channels[] = { 5245fe6f7dSsrinivas pandruvada { 5345fe6f7dSsrinivas pandruvada .type = IIO_ACCEL, 5445fe6f7dSsrinivas pandruvada .modified = 1, 5545fe6f7dSsrinivas pandruvada .channel2 = IIO_MOD_X, 56c6399453SSrinivas Pandruvada .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 57ecbe18f2SJonathan Cameron .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 58ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SCALE) | 59ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ) | 60ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_HYSTERESIS), 6145fe6f7dSsrinivas pandruvada .scan_index = CHANNEL_SCAN_INDEX_X, 6245fe6f7dSsrinivas pandruvada }, { 6345fe6f7dSsrinivas pandruvada .type = IIO_ACCEL, 6445fe6f7dSsrinivas pandruvada .modified = 1, 6545fe6f7dSsrinivas pandruvada .channel2 = IIO_MOD_Y, 66c6399453SSrinivas Pandruvada .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 67ecbe18f2SJonathan Cameron .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 68ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SCALE) | 69ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ) | 70ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_HYSTERESIS), 7145fe6f7dSsrinivas pandruvada .scan_index = CHANNEL_SCAN_INDEX_Y, 7245fe6f7dSsrinivas pandruvada }, { 7345fe6f7dSsrinivas pandruvada .type = IIO_ACCEL, 7445fe6f7dSsrinivas pandruvada .modified = 1, 7545fe6f7dSsrinivas pandruvada .channel2 = IIO_MOD_Z, 76c6399453SSrinivas Pandruvada .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 77ecbe18f2SJonathan Cameron .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 78ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SCALE) | 79ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ) | 80ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_HYSTERESIS), 8145fe6f7dSsrinivas pandruvada .scan_index = CHANNEL_SCAN_INDEX_Z, 82a96cd0f9SSrinivas Pandruvada }, 834c261720SYe Xiang IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP) 8445fe6f7dSsrinivas pandruvada }; 8545fe6f7dSsrinivas pandruvada 860e377f3bSSong Hongyan /* Channel definitions */ 870e377f3bSSong Hongyan static const struct iio_chan_spec gravity_channels[] = { 880e377f3bSSong Hongyan { 890e377f3bSSong Hongyan .type = IIO_GRAVITY, 900e377f3bSSong Hongyan .modified = 1, 910e377f3bSSong Hongyan .channel2 = IIO_MOD_X, 920e377f3bSSong Hongyan .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 930e377f3bSSong Hongyan .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 940e377f3bSSong Hongyan BIT(IIO_CHAN_INFO_SCALE) | 950e377f3bSSong Hongyan BIT(IIO_CHAN_INFO_SAMP_FREQ) | 960e377f3bSSong Hongyan BIT(IIO_CHAN_INFO_HYSTERESIS), 970e377f3bSSong Hongyan .scan_index = CHANNEL_SCAN_INDEX_X, 980e377f3bSSong Hongyan }, { 990e377f3bSSong Hongyan .type = IIO_GRAVITY, 1000e377f3bSSong Hongyan .modified = 1, 1010e377f3bSSong Hongyan .channel2 = IIO_MOD_Y, 1020e377f3bSSong Hongyan .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1030e377f3bSSong Hongyan .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 1040e377f3bSSong Hongyan BIT(IIO_CHAN_INFO_SCALE) | 1050e377f3bSSong Hongyan BIT(IIO_CHAN_INFO_SAMP_FREQ) | 1060e377f3bSSong Hongyan BIT(IIO_CHAN_INFO_HYSTERESIS), 1070e377f3bSSong Hongyan .scan_index = CHANNEL_SCAN_INDEX_Y, 1080e377f3bSSong Hongyan }, { 1090e377f3bSSong Hongyan .type = IIO_GRAVITY, 1100e377f3bSSong Hongyan .modified = 1, 1110e377f3bSSong Hongyan .channel2 = IIO_MOD_Z, 1120e377f3bSSong Hongyan .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1130e377f3bSSong Hongyan .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 1140e377f3bSSong Hongyan BIT(IIO_CHAN_INFO_SCALE) | 1150e377f3bSSong Hongyan BIT(IIO_CHAN_INFO_SAMP_FREQ) | 1160e377f3bSSong Hongyan BIT(IIO_CHAN_INFO_HYSTERESIS), 1170e377f3bSSong Hongyan .scan_index = CHANNEL_SCAN_INDEX_Z, 1184c261720SYe Xiang }, 1194c261720SYe Xiang IIO_CHAN_SOFT_TIMESTAMP(CHANNEL_SCAN_INDEX_TIMESTAMP), 1200e377f3bSSong Hongyan }; 1210e377f3bSSong Hongyan 12245fe6f7dSsrinivas pandruvada /* Adjust channel real bits based on report descriptor */ 12345fe6f7dSsrinivas pandruvada static void accel_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels, 12445fe6f7dSsrinivas pandruvada int channel, int size) 12545fe6f7dSsrinivas pandruvada { 12645fe6f7dSsrinivas pandruvada channels[channel].scan_type.sign = 's'; 12745fe6f7dSsrinivas pandruvada /* Real storage bits will change based on the report desc. */ 12845fe6f7dSsrinivas pandruvada channels[channel].scan_type.realbits = size * 8; 12945fe6f7dSsrinivas pandruvada /* Maximum size of a sample to capture is u32 */ 13045fe6f7dSsrinivas pandruvada channels[channel].scan_type.storagebits = sizeof(u32) * 8; 13145fe6f7dSsrinivas pandruvada } 13245fe6f7dSsrinivas pandruvada 13345fe6f7dSsrinivas pandruvada /* Channel read_raw handler */ 13445fe6f7dSsrinivas pandruvada static int accel_3d_read_raw(struct iio_dev *indio_dev, 13545fe6f7dSsrinivas pandruvada struct iio_chan_spec const *chan, 13645fe6f7dSsrinivas pandruvada int *val, int *val2, 13745fe6f7dSsrinivas pandruvada long mask) 13845fe6f7dSsrinivas pandruvada { 13945fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state = iio_priv(indio_dev); 14045fe6f7dSsrinivas pandruvada int report_id = -1; 14145fe6f7dSsrinivas pandruvada u32 address; 14245fe6f7dSsrinivas pandruvada int ret_type; 1430145b505SHans de Goede s32 min; 1440e377f3bSSong Hongyan struct hid_sensor_hub_device *hsdev = 1450e377f3bSSong Hongyan accel_state->common_attributes.hsdev; 14645fe6f7dSsrinivas pandruvada 14745fe6f7dSsrinivas pandruvada *val = 0; 14845fe6f7dSsrinivas pandruvada *val2 = 0; 14945fe6f7dSsrinivas pandruvada switch (mask) { 15067464a54SRodrigo Siqueira case IIO_CHAN_INFO_RAW: 151c6399453SSrinivas Pandruvada hid_sensor_power_state(&accel_state->common_attributes, true); 15245fe6f7dSsrinivas pandruvada report_id = accel_state->accel[chan->scan_index].report_id; 1530145b505SHans de Goede min = accel_state->accel[chan->scan_index].logical_minimum; 15445fe6f7dSsrinivas pandruvada address = accel_3d_addresses[chan->scan_index]; 15545fe6f7dSsrinivas pandruvada if (report_id >= 0) 15645fe6f7dSsrinivas pandruvada *val = sensor_hub_input_attr_get_raw_value( 15745fe6f7dSsrinivas pandruvada accel_state->common_attributes.hsdev, 1580e377f3bSSong Hongyan hsdev->usage, address, report_id, 1590145b505SHans de Goede SENSOR_HUB_SYNC, 1600145b505SHans de Goede min < 0); 16145fe6f7dSsrinivas pandruvada else { 16245fe6f7dSsrinivas pandruvada *val = 0; 163c6399453SSrinivas Pandruvada hid_sensor_power_state(&accel_state->common_attributes, 164c6399453SSrinivas Pandruvada false); 16545fe6f7dSsrinivas pandruvada return -EINVAL; 16645fe6f7dSsrinivas pandruvada } 167c6399453SSrinivas Pandruvada hid_sensor_power_state(&accel_state->common_attributes, false); 16845fe6f7dSsrinivas pandruvada ret_type = IIO_VAL_INT; 16945fe6f7dSsrinivas pandruvada break; 17045fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_SCALE: 17173a24988SSrinivas Pandruvada *val = accel_state->scale_pre_decml; 17273a24988SSrinivas Pandruvada *val2 = accel_state->scale_post_decml; 17373a24988SSrinivas Pandruvada ret_type = accel_state->scale_precision; 17445fe6f7dSsrinivas pandruvada break; 17545fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_OFFSET: 17673a24988SSrinivas Pandruvada *val = accel_state->value_offset; 17745fe6f7dSsrinivas pandruvada ret_type = IIO_VAL_INT; 17845fe6f7dSsrinivas pandruvada break; 17945fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_SAMP_FREQ: 1802f084c17SSachin Kamat ret_type = hid_sensor_read_samp_freq_value( 18145fe6f7dSsrinivas pandruvada &accel_state->common_attributes, val, val2); 18245fe6f7dSsrinivas pandruvada break; 18345fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_HYSTERESIS: 1842f084c17SSachin Kamat ret_type = hid_sensor_read_raw_hyst_value( 18545fe6f7dSsrinivas pandruvada &accel_state->common_attributes, val, val2); 18645fe6f7dSsrinivas pandruvada break; 18745fe6f7dSsrinivas pandruvada default: 18845fe6f7dSsrinivas pandruvada ret_type = -EINVAL; 18945fe6f7dSsrinivas pandruvada break; 19045fe6f7dSsrinivas pandruvada } 19145fe6f7dSsrinivas pandruvada 19245fe6f7dSsrinivas pandruvada return ret_type; 19345fe6f7dSsrinivas pandruvada } 19445fe6f7dSsrinivas pandruvada 19545fe6f7dSsrinivas pandruvada /* Channel write_raw handler */ 19645fe6f7dSsrinivas pandruvada static int accel_3d_write_raw(struct iio_dev *indio_dev, 19745fe6f7dSsrinivas pandruvada struct iio_chan_spec const *chan, 19845fe6f7dSsrinivas pandruvada int val, 19945fe6f7dSsrinivas pandruvada int val2, 20045fe6f7dSsrinivas pandruvada long mask) 20145fe6f7dSsrinivas pandruvada { 20245fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state = iio_priv(indio_dev); 20345fe6f7dSsrinivas pandruvada int ret = 0; 20445fe6f7dSsrinivas pandruvada 20545fe6f7dSsrinivas pandruvada switch (mask) { 20645fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_SAMP_FREQ: 20745fe6f7dSsrinivas pandruvada ret = hid_sensor_write_samp_freq_value( 20845fe6f7dSsrinivas pandruvada &accel_state->common_attributes, val, val2); 20945fe6f7dSsrinivas pandruvada break; 21045fe6f7dSsrinivas pandruvada case IIO_CHAN_INFO_HYSTERESIS: 21145fe6f7dSsrinivas pandruvada ret = hid_sensor_write_raw_hyst_value( 21245fe6f7dSsrinivas pandruvada &accel_state->common_attributes, val, val2); 21345fe6f7dSsrinivas pandruvada break; 21445fe6f7dSsrinivas pandruvada default: 21545fe6f7dSsrinivas pandruvada ret = -EINVAL; 21645fe6f7dSsrinivas pandruvada } 21745fe6f7dSsrinivas pandruvada 21845fe6f7dSsrinivas pandruvada return ret; 21945fe6f7dSsrinivas pandruvada } 22045fe6f7dSsrinivas pandruvada 22145fe6f7dSsrinivas pandruvada static const struct iio_info accel_3d_info = { 22245fe6f7dSsrinivas pandruvada .read_raw = &accel_3d_read_raw, 22345fe6f7dSsrinivas pandruvada .write_raw = &accel_3d_write_raw, 22445fe6f7dSsrinivas pandruvada }; 22545fe6f7dSsrinivas pandruvada 22645fe6f7dSsrinivas pandruvada /* Function to push data to buffer */ 227a96cd0f9SSrinivas Pandruvada static void hid_sensor_push_data(struct iio_dev *indio_dev, void *data, 228a96cd0f9SSrinivas Pandruvada int len, int64_t timestamp) 22945fe6f7dSsrinivas pandruvada { 23045fe6f7dSsrinivas pandruvada dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); 231a96cd0f9SSrinivas Pandruvada iio_push_to_buffers_with_timestamp(indio_dev, data, timestamp); 23245fe6f7dSsrinivas pandruvada } 23345fe6f7dSsrinivas pandruvada 23445fe6f7dSsrinivas pandruvada /* Callback handler to send event after all samples are received and captured */ 23545fe6f7dSsrinivas pandruvada static int accel_3d_proc_event(struct hid_sensor_hub_device *hsdev, 23645fe6f7dSsrinivas pandruvada unsigned usage_id, 23745fe6f7dSsrinivas pandruvada void *priv) 23845fe6f7dSsrinivas pandruvada { 23945fe6f7dSsrinivas pandruvada struct iio_dev *indio_dev = platform_get_drvdata(priv); 24045fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state = iio_priv(indio_dev); 24145fe6f7dSsrinivas pandruvada 24256ff6be6SSrinivas Pandruvada dev_dbg(&indio_dev->dev, "accel_3d_proc_event\n"); 243a96cd0f9SSrinivas Pandruvada if (atomic_read(&accel_state->common_attributes.data_ready)) { 244a96cd0f9SSrinivas Pandruvada if (!accel_state->timestamp) 245a96cd0f9SSrinivas Pandruvada accel_state->timestamp = iio_get_time_ns(indio_dev); 246a96cd0f9SSrinivas Pandruvada 24745fe6f7dSsrinivas pandruvada hid_sensor_push_data(indio_dev, 248c6559bf7SJonathan Cameron &accel_state->scan, 249c6559bf7SJonathan Cameron sizeof(accel_state->scan), 250a96cd0f9SSrinivas Pandruvada accel_state->timestamp); 251a96cd0f9SSrinivas Pandruvada 252a96cd0f9SSrinivas Pandruvada accel_state->timestamp = 0; 253a96cd0f9SSrinivas Pandruvada } 25445fe6f7dSsrinivas pandruvada 25545fe6f7dSsrinivas pandruvada return 0; 25645fe6f7dSsrinivas pandruvada } 25745fe6f7dSsrinivas pandruvada 25845fe6f7dSsrinivas pandruvada /* Capture samples in local storage */ 25945fe6f7dSsrinivas pandruvada static int accel_3d_capture_sample(struct hid_sensor_hub_device *hsdev, 26045fe6f7dSsrinivas pandruvada unsigned usage_id, 26145fe6f7dSsrinivas pandruvada size_t raw_len, char *raw_data, 26245fe6f7dSsrinivas pandruvada void *priv) 26345fe6f7dSsrinivas pandruvada { 26445fe6f7dSsrinivas pandruvada struct iio_dev *indio_dev = platform_get_drvdata(priv); 26545fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state = iio_priv(indio_dev); 26645fe6f7dSsrinivas pandruvada int offset; 26745fe6f7dSsrinivas pandruvada int ret = -EINVAL; 26845fe6f7dSsrinivas pandruvada 26945fe6f7dSsrinivas pandruvada switch (usage_id) { 27045fe6f7dSsrinivas pandruvada case HID_USAGE_SENSOR_ACCEL_X_AXIS: 27145fe6f7dSsrinivas pandruvada case HID_USAGE_SENSOR_ACCEL_Y_AXIS: 27245fe6f7dSsrinivas pandruvada case HID_USAGE_SENSOR_ACCEL_Z_AXIS: 27345fe6f7dSsrinivas pandruvada offset = usage_id - HID_USAGE_SENSOR_ACCEL_X_AXIS; 274c6559bf7SJonathan Cameron accel_state->scan.accel_val[CHANNEL_SCAN_INDEX_X + offset] = 27545fe6f7dSsrinivas pandruvada *(u32 *)raw_data; 27645fe6f7dSsrinivas pandruvada ret = 0; 27745fe6f7dSsrinivas pandruvada break; 278a96cd0f9SSrinivas Pandruvada case HID_USAGE_SENSOR_TIME_TIMESTAMP: 279a96cd0f9SSrinivas Pandruvada accel_state->timestamp = 280a96cd0f9SSrinivas Pandruvada hid_sensor_convert_timestamp( 281a96cd0f9SSrinivas Pandruvada &accel_state->common_attributes, 282a96cd0f9SSrinivas Pandruvada *(int64_t *)raw_data); 283*f7b23d1cSDmitry Perchanov ret = 0; 284a96cd0f9SSrinivas Pandruvada break; 28545fe6f7dSsrinivas pandruvada default: 28645fe6f7dSsrinivas pandruvada break; 28745fe6f7dSsrinivas pandruvada } 28845fe6f7dSsrinivas pandruvada 28945fe6f7dSsrinivas pandruvada return ret; 29045fe6f7dSsrinivas pandruvada } 29145fe6f7dSsrinivas pandruvada 29245fe6f7dSsrinivas pandruvada /* Parse report which is specific to an usage id*/ 29345fe6f7dSsrinivas pandruvada static int accel_3d_parse_report(struct platform_device *pdev, 29445fe6f7dSsrinivas pandruvada struct hid_sensor_hub_device *hsdev, 29545fe6f7dSsrinivas pandruvada struct iio_chan_spec *channels, 29645fe6f7dSsrinivas pandruvada unsigned usage_id, 29745fe6f7dSsrinivas pandruvada struct accel_3d_state *st) 29845fe6f7dSsrinivas pandruvada { 29945fe6f7dSsrinivas pandruvada int ret; 30045fe6f7dSsrinivas pandruvada int i; 30145fe6f7dSsrinivas pandruvada 30245fe6f7dSsrinivas pandruvada for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) { 30345fe6f7dSsrinivas pandruvada ret = sensor_hub_input_get_attribute_info(hsdev, 30445fe6f7dSsrinivas pandruvada HID_INPUT_REPORT, 30545fe6f7dSsrinivas pandruvada usage_id, 30645fe6f7dSsrinivas pandruvada HID_USAGE_SENSOR_ACCEL_X_AXIS + i, 30745fe6f7dSsrinivas pandruvada &st->accel[CHANNEL_SCAN_INDEX_X + i]); 30845fe6f7dSsrinivas pandruvada if (ret < 0) 30945fe6f7dSsrinivas pandruvada break; 31045fe6f7dSsrinivas pandruvada accel_3d_adjust_channel_bit_mask(channels, 31145fe6f7dSsrinivas pandruvada CHANNEL_SCAN_INDEX_X + i, 31245fe6f7dSsrinivas pandruvada st->accel[CHANNEL_SCAN_INDEX_X + i].size); 31345fe6f7dSsrinivas pandruvada } 31445fe6f7dSsrinivas pandruvada dev_dbg(&pdev->dev, "accel_3d %x:%x, %x:%x, %x:%x\n", 31545fe6f7dSsrinivas pandruvada st->accel[0].index, 31645fe6f7dSsrinivas pandruvada st->accel[0].report_id, 31745fe6f7dSsrinivas pandruvada st->accel[1].index, st->accel[1].report_id, 31845fe6f7dSsrinivas pandruvada st->accel[2].index, st->accel[2].report_id); 31945fe6f7dSsrinivas pandruvada 32073a24988SSrinivas Pandruvada st->scale_precision = hid_sensor_format_scale( 3210e377f3bSSong Hongyan hsdev->usage, 32273a24988SSrinivas Pandruvada &st->accel[CHANNEL_SCAN_INDEX_X], 32373a24988SSrinivas Pandruvada &st->scale_pre_decml, &st->scale_post_decml); 32473a24988SSrinivas Pandruvada 32545fe6f7dSsrinivas pandruvada return ret; 32645fe6f7dSsrinivas pandruvada } 32745fe6f7dSsrinivas pandruvada 32845fe6f7dSsrinivas pandruvada /* Function to initialize the processing for usage id */ 329fc52692cSGreg Kroah-Hartman static int hid_accel_3d_probe(struct platform_device *pdev) 33045fe6f7dSsrinivas pandruvada { 33145fe6f7dSsrinivas pandruvada int ret = 0; 33240f4b1f0SJulia Lawall const char *name; 33345fe6f7dSsrinivas pandruvada struct iio_dev *indio_dev; 33445fe6f7dSsrinivas pandruvada struct accel_3d_state *accel_state; 3350e377f3bSSong Hongyan const struct iio_chan_spec *channel_spec; 3360e377f3bSSong Hongyan int channel_size; 3370e377f3bSSong Hongyan 33845fe6f7dSsrinivas pandruvada struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 33945fe6f7dSsrinivas pandruvada 3406b545eedSSachin Kamat indio_dev = devm_iio_device_alloc(&pdev->dev, 3416b545eedSSachin Kamat sizeof(struct accel_3d_state)); 3426b545eedSSachin Kamat if (indio_dev == NULL) 3436b545eedSSachin Kamat return -ENOMEM; 3446b545eedSSachin Kamat 34545fe6f7dSsrinivas pandruvada platform_set_drvdata(pdev, indio_dev); 34645fe6f7dSsrinivas pandruvada 34745fe6f7dSsrinivas pandruvada accel_state = iio_priv(indio_dev); 34845fe6f7dSsrinivas pandruvada accel_state->common_attributes.hsdev = hsdev; 34945fe6f7dSsrinivas pandruvada accel_state->common_attributes.pdev = pdev; 35045fe6f7dSsrinivas pandruvada 3510e377f3bSSong Hongyan if (hsdev->usage == HID_USAGE_SENSOR_ACCEL_3D) { 3520e377f3bSSong Hongyan name = "accel_3d"; 3530e377f3bSSong Hongyan channel_spec = accel_3d_channels; 3540e377f3bSSong Hongyan channel_size = sizeof(accel_3d_channels); 355862d1d89SSrinivas Pandruvada indio_dev->num_channels = ARRAY_SIZE(accel_3d_channels); 3560e377f3bSSong Hongyan } else { 3570e377f3bSSong Hongyan name = "gravity"; 3580e377f3bSSong Hongyan channel_spec = gravity_channels; 3590e377f3bSSong Hongyan channel_size = sizeof(gravity_channels); 360862d1d89SSrinivas Pandruvada indio_dev->num_channels = ARRAY_SIZE(gravity_channels); 3610e377f3bSSong Hongyan } 3620e41fd51SYe Xiang ret = hid_sensor_parse_common_attributes(hsdev, 3630e41fd51SYe Xiang hsdev->usage, 3640e41fd51SYe Xiang &accel_state->common_attributes, 3650e41fd51SYe Xiang accel_3d_sensitivity_addresses, 3660e41fd51SYe Xiang ARRAY_SIZE(accel_3d_sensitivity_addresses)); 36745fe6f7dSsrinivas pandruvada if (ret) { 36845fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "failed to setup common attributes\n"); 3696b545eedSSachin Kamat return ret; 37045fe6f7dSsrinivas pandruvada } 3718b2ac516SAlexandru Ardelean indio_dev->channels = devm_kmemdup(&pdev->dev, channel_spec, 3728b2ac516SAlexandru Ardelean channel_size, GFP_KERNEL); 37345fe6f7dSsrinivas pandruvada 374b136faffSFabio Estevam if (!indio_dev->channels) { 37545fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "failed to duplicate channels\n"); 3766b545eedSSachin Kamat return -ENOMEM; 37745fe6f7dSsrinivas pandruvada } 378b136faffSFabio Estevam ret = accel_3d_parse_report(pdev, hsdev, 379b136faffSFabio Estevam (struct iio_chan_spec *)indio_dev->channels, 3800e377f3bSSong Hongyan hsdev->usage, accel_state); 38145fe6f7dSsrinivas pandruvada if (ret) { 38245fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "failed to setup attributes\n"); 3838b2ac516SAlexandru Ardelean return ret; 38445fe6f7dSsrinivas pandruvada } 38545fe6f7dSsrinivas pandruvada 38645fe6f7dSsrinivas pandruvada indio_dev->info = &accel_3d_info; 38745fe6f7dSsrinivas pandruvada indio_dev->name = name; 38845fe6f7dSsrinivas pandruvada indio_dev->modes = INDIO_DIRECT_MODE; 38945fe6f7dSsrinivas pandruvada 39056ff6be6SSrinivas Pandruvada atomic_set(&accel_state->common_attributes.data_ready, 0); 391067fda1cSAlexandru Ardelean 39245fe6f7dSsrinivas pandruvada ret = hid_sensor_setup_trigger(indio_dev, name, 39345fe6f7dSsrinivas pandruvada &accel_state->common_attributes); 39445fe6f7dSsrinivas pandruvada if (ret < 0) { 39545fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "trigger setup failed\n"); 3968b2ac516SAlexandru Ardelean return ret; 39745fe6f7dSsrinivas pandruvada } 39845fe6f7dSsrinivas pandruvada 39945fe6f7dSsrinivas pandruvada ret = iio_device_register(indio_dev); 40045fe6f7dSsrinivas pandruvada if (ret) { 40145fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "device register failed\n"); 40245fe6f7dSsrinivas pandruvada goto error_remove_trigger; 40345fe6f7dSsrinivas pandruvada } 40445fe6f7dSsrinivas pandruvada 40545fe6f7dSsrinivas pandruvada accel_state->callbacks.send_event = accel_3d_proc_event; 40645fe6f7dSsrinivas pandruvada accel_state->callbacks.capture_sample = accel_3d_capture_sample; 40745fe6f7dSsrinivas pandruvada accel_state->callbacks.pdev = pdev; 4080e377f3bSSong Hongyan ret = sensor_hub_register_callback(hsdev, hsdev->usage, 40945fe6f7dSsrinivas pandruvada &accel_state->callbacks); 41045fe6f7dSsrinivas pandruvada if (ret < 0) { 41145fe6f7dSsrinivas pandruvada dev_err(&pdev->dev, "callback reg failed\n"); 41245fe6f7dSsrinivas pandruvada goto error_iio_unreg; 41345fe6f7dSsrinivas pandruvada } 41445fe6f7dSsrinivas pandruvada 41545fe6f7dSsrinivas pandruvada return ret; 41645fe6f7dSsrinivas pandruvada 41745fe6f7dSsrinivas pandruvada error_iio_unreg: 41845fe6f7dSsrinivas pandruvada iio_device_unregister(indio_dev); 41945fe6f7dSsrinivas pandruvada error_remove_trigger: 420067fda1cSAlexandru Ardelean hid_sensor_remove_trigger(indio_dev, &accel_state->common_attributes); 42145fe6f7dSsrinivas pandruvada return ret; 42245fe6f7dSsrinivas pandruvada } 42345fe6f7dSsrinivas pandruvada 42445fe6f7dSsrinivas pandruvada /* Function to deinitialize the processing for usage id */ 425fc52692cSGreg Kroah-Hartman static int hid_accel_3d_remove(struct platform_device *pdev) 42645fe6f7dSsrinivas pandruvada { 42745fe6f7dSsrinivas pandruvada struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 42845fe6f7dSsrinivas pandruvada struct iio_dev *indio_dev = platform_get_drvdata(pdev); 429ec7f68e0SSrinivas Pandruvada struct accel_3d_state *accel_state = iio_priv(indio_dev); 43045fe6f7dSsrinivas pandruvada 4310e377f3bSSong Hongyan sensor_hub_remove_callback(hsdev, hsdev->usage); 43245fe6f7dSsrinivas pandruvada iio_device_unregister(indio_dev); 433067fda1cSAlexandru Ardelean hid_sensor_remove_trigger(indio_dev, &accel_state->common_attributes); 43445fe6f7dSsrinivas pandruvada 43545fe6f7dSsrinivas pandruvada return 0; 43645fe6f7dSsrinivas pandruvada } 43745fe6f7dSsrinivas pandruvada 43876ec50a1SKrzysztof Kozlowski static const struct platform_device_id hid_accel_3d_ids[] = { 43966c721f7SAlexander Holler { 44066c721f7SAlexander Holler /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 44166c721f7SAlexander Holler .name = "HID-SENSOR-200073", 44266c721f7SAlexander Holler }, 4430e377f3bSSong Hongyan { /* gravity sensor */ 4440e377f3bSSong Hongyan .name = "HID-SENSOR-20007b", 4450e377f3bSSong Hongyan }, 44666c721f7SAlexander Holler { /* sentinel */ } 44766c721f7SAlexander Holler }; 44866c721f7SAlexander Holler MODULE_DEVICE_TABLE(platform, hid_accel_3d_ids); 44966c721f7SAlexander Holler 45045fe6f7dSsrinivas pandruvada static struct platform_driver hid_accel_3d_platform_driver = { 45166c721f7SAlexander Holler .id_table = hid_accel_3d_ids, 45245fe6f7dSsrinivas pandruvada .driver = { 45366c721f7SAlexander Holler .name = KBUILD_MODNAME, 454cbbcc937SSrinivas Pandruvada .pm = &hid_sensor_pm_ops, 45545fe6f7dSsrinivas pandruvada }, 45645fe6f7dSsrinivas pandruvada .probe = hid_accel_3d_probe, 45745fe6f7dSsrinivas pandruvada .remove = hid_accel_3d_remove, 45845fe6f7dSsrinivas pandruvada }; 45945fe6f7dSsrinivas pandruvada module_platform_driver(hid_accel_3d_platform_driver); 46045fe6f7dSsrinivas pandruvada 46145fe6f7dSsrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Accel 3D"); 46245fe6f7dSsrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 46345fe6f7dSsrinivas pandruvada MODULE_LICENSE("GPL"); 46412f13d1fSAndy Shevchenko MODULE_IMPORT_NS(IIO_HID); 465