1c5bdbef7Ssrinivas pandruvada /* 2c5bdbef7Ssrinivas pandruvada * HID Sensors Driver 3c5bdbef7Ssrinivas pandruvada * Copyright (c) 2012, Intel Corporation. 4c5bdbef7Ssrinivas pandruvada * 5c5bdbef7Ssrinivas pandruvada * This program is free software; you can redistribute it and/or modify it 6c5bdbef7Ssrinivas pandruvada * under the terms and conditions of the GNU General Public License, 7c5bdbef7Ssrinivas pandruvada * version 2, as published by the Free Software Foundation. 8c5bdbef7Ssrinivas pandruvada * 9c5bdbef7Ssrinivas pandruvada * This program is distributed in the hope it will be useful, but WITHOUT 10c5bdbef7Ssrinivas pandruvada * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11c5bdbef7Ssrinivas pandruvada * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12c5bdbef7Ssrinivas pandruvada * more details. 13c5bdbef7Ssrinivas pandruvada * 14c5bdbef7Ssrinivas pandruvada * You should have received a copy of the GNU General Public License along with 15c5bdbef7Ssrinivas pandruvada * this program; if not, write to the Free Software Foundation, Inc., 16c5bdbef7Ssrinivas pandruvada * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 17c5bdbef7Ssrinivas pandruvada * 18c5bdbef7Ssrinivas pandruvada */ 19c5bdbef7Ssrinivas pandruvada #include <linux/device.h> 20c5bdbef7Ssrinivas pandruvada #include <linux/platform_device.h> 21c5bdbef7Ssrinivas pandruvada #include <linux/module.h> 22c5bdbef7Ssrinivas pandruvada #include <linux/interrupt.h> 23c5bdbef7Ssrinivas pandruvada #include <linux/irq.h> 24c5bdbef7Ssrinivas pandruvada #include <linux/slab.h> 250964f706SSrinivas Pandruvada #include <linux/delay.h> 26c5bdbef7Ssrinivas pandruvada #include <linux/hid-sensor-hub.h> 27c5bdbef7Ssrinivas pandruvada #include <linux/iio/iio.h> 28c5bdbef7Ssrinivas pandruvada #include <linux/iio/sysfs.h> 29c5bdbef7Ssrinivas pandruvada #include <linux/iio/buffer.h> 30c5bdbef7Ssrinivas pandruvada #include <linux/iio/trigger_consumer.h> 31c5bdbef7Ssrinivas pandruvada #include <linux/iio/triggered_buffer.h> 32c5bdbef7Ssrinivas pandruvada #include "../common/hid-sensors/hid-sensor-trigger.h" 33c5bdbef7Ssrinivas pandruvada 34c5bdbef7Ssrinivas pandruvada enum gyro_3d_channel { 35c5bdbef7Ssrinivas pandruvada CHANNEL_SCAN_INDEX_X, 36c5bdbef7Ssrinivas pandruvada CHANNEL_SCAN_INDEX_Y, 37c5bdbef7Ssrinivas pandruvada CHANNEL_SCAN_INDEX_Z, 38c5bdbef7Ssrinivas pandruvada GYRO_3D_CHANNEL_MAX, 39c5bdbef7Ssrinivas pandruvada }; 40c5bdbef7Ssrinivas pandruvada 41c5bdbef7Ssrinivas pandruvada struct gyro_3d_state { 42c5bdbef7Ssrinivas pandruvada struct hid_sensor_hub_callbacks callbacks; 43e07c6d17SAlexander Holler struct hid_sensor_common common_attributes; 44c5bdbef7Ssrinivas pandruvada struct hid_sensor_hub_attribute_info gyro[GYRO_3D_CHANNEL_MAX]; 45c5bdbef7Ssrinivas pandruvada u32 gyro_val[GYRO_3D_CHANNEL_MAX]; 466c7db77eSSrinivas Pandruvada int scale_pre_decml; 476c7db77eSSrinivas Pandruvada int scale_post_decml; 486c7db77eSSrinivas Pandruvada int scale_precision; 496c7db77eSSrinivas Pandruvada int value_offset; 50c5bdbef7Ssrinivas pandruvada }; 51c5bdbef7Ssrinivas pandruvada 52c5bdbef7Ssrinivas pandruvada static const u32 gyro_3d_addresses[GYRO_3D_CHANNEL_MAX] = { 53c5bdbef7Ssrinivas pandruvada HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS, 54c5bdbef7Ssrinivas pandruvada HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS, 55c5bdbef7Ssrinivas pandruvada HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS 56c5bdbef7Ssrinivas pandruvada }; 57c5bdbef7Ssrinivas pandruvada 58c5bdbef7Ssrinivas pandruvada /* Channel definitions */ 59c5bdbef7Ssrinivas pandruvada static const struct iio_chan_spec gyro_3d_channels[] = { 60c5bdbef7Ssrinivas pandruvada { 61c5bdbef7Ssrinivas pandruvada .type = IIO_ANGL_VEL, 62c5bdbef7Ssrinivas pandruvada .modified = 1, 63c5bdbef7Ssrinivas pandruvada .channel2 = IIO_MOD_X, 640964f706SSrinivas Pandruvada .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 65ecbe18f2SJonathan Cameron .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 66ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SCALE) | 67ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ) | 68ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_HYSTERESIS), 69c5bdbef7Ssrinivas pandruvada .scan_index = CHANNEL_SCAN_INDEX_X, 70c5bdbef7Ssrinivas pandruvada }, { 71c5bdbef7Ssrinivas pandruvada .type = IIO_ANGL_VEL, 72c5bdbef7Ssrinivas pandruvada .modified = 1, 73c5bdbef7Ssrinivas pandruvada .channel2 = IIO_MOD_Y, 740964f706SSrinivas Pandruvada .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 75ecbe18f2SJonathan Cameron .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 76ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SCALE) | 77ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ) | 78ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_HYSTERESIS), 79c5bdbef7Ssrinivas pandruvada .scan_index = CHANNEL_SCAN_INDEX_Y, 80c5bdbef7Ssrinivas pandruvada }, { 81c5bdbef7Ssrinivas pandruvada .type = IIO_ANGL_VEL, 82c5bdbef7Ssrinivas pandruvada .modified = 1, 83c5bdbef7Ssrinivas pandruvada .channel2 = IIO_MOD_Z, 840964f706SSrinivas Pandruvada .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 85ecbe18f2SJonathan Cameron .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 86ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SCALE) | 87ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_SAMP_FREQ) | 88ecbe18f2SJonathan Cameron BIT(IIO_CHAN_INFO_HYSTERESIS), 89c5bdbef7Ssrinivas pandruvada .scan_index = CHANNEL_SCAN_INDEX_Z, 90c5bdbef7Ssrinivas pandruvada } 91c5bdbef7Ssrinivas pandruvada }; 92c5bdbef7Ssrinivas pandruvada 93c5bdbef7Ssrinivas pandruvada /* Adjust channel real bits based on report descriptor */ 94c5bdbef7Ssrinivas pandruvada static void gyro_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels, 95c5bdbef7Ssrinivas pandruvada int channel, int size) 96c5bdbef7Ssrinivas pandruvada { 97c5bdbef7Ssrinivas pandruvada channels[channel].scan_type.sign = 's'; 98c5bdbef7Ssrinivas pandruvada /* Real storage bits will change based on the report desc. */ 99c5bdbef7Ssrinivas pandruvada channels[channel].scan_type.realbits = size * 8; 100c5bdbef7Ssrinivas pandruvada /* Maximum size of a sample to capture is u32 */ 101c5bdbef7Ssrinivas pandruvada channels[channel].scan_type.storagebits = sizeof(u32) * 8; 102c5bdbef7Ssrinivas pandruvada } 103c5bdbef7Ssrinivas pandruvada 104c5bdbef7Ssrinivas pandruvada /* Channel read_raw handler */ 105c5bdbef7Ssrinivas pandruvada static int gyro_3d_read_raw(struct iio_dev *indio_dev, 106c5bdbef7Ssrinivas pandruvada struct iio_chan_spec const *chan, 107c5bdbef7Ssrinivas pandruvada int *val, int *val2, 108c5bdbef7Ssrinivas pandruvada long mask) 109c5bdbef7Ssrinivas pandruvada { 110c5bdbef7Ssrinivas pandruvada struct gyro_3d_state *gyro_state = iio_priv(indio_dev); 111c5bdbef7Ssrinivas pandruvada int report_id = -1; 112c5bdbef7Ssrinivas pandruvada u32 address; 113c5bdbef7Ssrinivas pandruvada int ret_type; 114c5bdbef7Ssrinivas pandruvada 115c5bdbef7Ssrinivas pandruvada *val = 0; 116c5bdbef7Ssrinivas pandruvada *val2 = 0; 117c5bdbef7Ssrinivas pandruvada switch (mask) { 118*67464a54SRodrigo Siqueira case IIO_CHAN_INFO_RAW: 1190964f706SSrinivas Pandruvada hid_sensor_power_state(&gyro_state->common_attributes, true); 120c5bdbef7Ssrinivas pandruvada report_id = gyro_state->gyro[chan->scan_index].report_id; 121c5bdbef7Ssrinivas pandruvada address = gyro_3d_addresses[chan->scan_index]; 122c5bdbef7Ssrinivas pandruvada if (report_id >= 0) 123c5bdbef7Ssrinivas pandruvada *val = sensor_hub_input_attr_get_raw_value( 124c5bdbef7Ssrinivas pandruvada gyro_state->common_attributes.hsdev, 125c5bdbef7Ssrinivas pandruvada HID_USAGE_SENSOR_GYRO_3D, address, 126b3f4737dSSrinivas Pandruvada report_id, 127b3f4737dSSrinivas Pandruvada SENSOR_HUB_SYNC); 128c5bdbef7Ssrinivas pandruvada else { 129c5bdbef7Ssrinivas pandruvada *val = 0; 1300964f706SSrinivas Pandruvada hid_sensor_power_state(&gyro_state->common_attributes, 1310964f706SSrinivas Pandruvada false); 132c5bdbef7Ssrinivas pandruvada return -EINVAL; 133c5bdbef7Ssrinivas pandruvada } 1340964f706SSrinivas Pandruvada hid_sensor_power_state(&gyro_state->common_attributes, false); 135c5bdbef7Ssrinivas pandruvada ret_type = IIO_VAL_INT; 136c5bdbef7Ssrinivas pandruvada break; 137c5bdbef7Ssrinivas pandruvada case IIO_CHAN_INFO_SCALE: 1386c7db77eSSrinivas Pandruvada *val = gyro_state->scale_pre_decml; 1396c7db77eSSrinivas Pandruvada *val2 = gyro_state->scale_post_decml; 1406c7db77eSSrinivas Pandruvada ret_type = gyro_state->scale_precision; 141c5bdbef7Ssrinivas pandruvada break; 142c5bdbef7Ssrinivas pandruvada case IIO_CHAN_INFO_OFFSET: 1436c7db77eSSrinivas Pandruvada *val = gyro_state->value_offset; 144c5bdbef7Ssrinivas pandruvada ret_type = IIO_VAL_INT; 145c5bdbef7Ssrinivas pandruvada break; 146c5bdbef7Ssrinivas pandruvada case IIO_CHAN_INFO_SAMP_FREQ: 14727793803SSachin Kamat ret_type = hid_sensor_read_samp_freq_value( 148c5bdbef7Ssrinivas pandruvada &gyro_state->common_attributes, val, val2); 149c5bdbef7Ssrinivas pandruvada break; 150c5bdbef7Ssrinivas pandruvada case IIO_CHAN_INFO_HYSTERESIS: 15127793803SSachin Kamat ret_type = hid_sensor_read_raw_hyst_value( 152c5bdbef7Ssrinivas pandruvada &gyro_state->common_attributes, val, val2); 153c5bdbef7Ssrinivas pandruvada break; 154c5bdbef7Ssrinivas pandruvada default: 155c5bdbef7Ssrinivas pandruvada ret_type = -EINVAL; 156c5bdbef7Ssrinivas pandruvada break; 157c5bdbef7Ssrinivas pandruvada } 158c5bdbef7Ssrinivas pandruvada 159c5bdbef7Ssrinivas pandruvada return ret_type; 160c5bdbef7Ssrinivas pandruvada } 161c5bdbef7Ssrinivas pandruvada 162c5bdbef7Ssrinivas pandruvada /* Channel write_raw handler */ 163c5bdbef7Ssrinivas pandruvada static int gyro_3d_write_raw(struct iio_dev *indio_dev, 164c5bdbef7Ssrinivas pandruvada struct iio_chan_spec const *chan, 165c5bdbef7Ssrinivas pandruvada int val, 166c5bdbef7Ssrinivas pandruvada int val2, 167c5bdbef7Ssrinivas pandruvada long mask) 168c5bdbef7Ssrinivas pandruvada { 169c5bdbef7Ssrinivas pandruvada struct gyro_3d_state *gyro_state = iio_priv(indio_dev); 170c5bdbef7Ssrinivas pandruvada int ret = 0; 171c5bdbef7Ssrinivas pandruvada 172c5bdbef7Ssrinivas pandruvada switch (mask) { 173c5bdbef7Ssrinivas pandruvada case IIO_CHAN_INFO_SAMP_FREQ: 174c5bdbef7Ssrinivas pandruvada ret = hid_sensor_write_samp_freq_value( 175c5bdbef7Ssrinivas pandruvada &gyro_state->common_attributes, val, val2); 176c5bdbef7Ssrinivas pandruvada break; 177c5bdbef7Ssrinivas pandruvada case IIO_CHAN_INFO_HYSTERESIS: 178c5bdbef7Ssrinivas pandruvada ret = hid_sensor_write_raw_hyst_value( 179c5bdbef7Ssrinivas pandruvada &gyro_state->common_attributes, val, val2); 180c5bdbef7Ssrinivas pandruvada break; 181c5bdbef7Ssrinivas pandruvada default: 182c5bdbef7Ssrinivas pandruvada ret = -EINVAL; 183c5bdbef7Ssrinivas pandruvada } 184c5bdbef7Ssrinivas pandruvada 185c5bdbef7Ssrinivas pandruvada return ret; 186c5bdbef7Ssrinivas pandruvada } 187c5bdbef7Ssrinivas pandruvada 188c5bdbef7Ssrinivas pandruvada static const struct iio_info gyro_3d_info = { 189c5bdbef7Ssrinivas pandruvada .read_raw = &gyro_3d_read_raw, 190c5bdbef7Ssrinivas pandruvada .write_raw = &gyro_3d_write_raw, 191c5bdbef7Ssrinivas pandruvada }; 192c5bdbef7Ssrinivas pandruvada 193c5bdbef7Ssrinivas pandruvada /* Function to push data to buffer */ 1948c60c7e7SLars-Peter Clausen static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data, 1958c60c7e7SLars-Peter Clausen int len) 196c5bdbef7Ssrinivas pandruvada { 197c5bdbef7Ssrinivas pandruvada dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); 1988c60c7e7SLars-Peter Clausen iio_push_to_buffers(indio_dev, data); 199c5bdbef7Ssrinivas pandruvada } 200c5bdbef7Ssrinivas pandruvada 201c5bdbef7Ssrinivas pandruvada /* Callback handler to send event after all samples are received and captured */ 202c5bdbef7Ssrinivas pandruvada static int gyro_3d_proc_event(struct hid_sensor_hub_device *hsdev, 203c5bdbef7Ssrinivas pandruvada unsigned usage_id, 204c5bdbef7Ssrinivas pandruvada void *priv) 205c5bdbef7Ssrinivas pandruvada { 206c5bdbef7Ssrinivas pandruvada struct iio_dev *indio_dev = platform_get_drvdata(priv); 207c5bdbef7Ssrinivas pandruvada struct gyro_3d_state *gyro_state = iio_priv(indio_dev); 208c5bdbef7Ssrinivas pandruvada 20956ff6be6SSrinivas Pandruvada dev_dbg(&indio_dev->dev, "gyro_3d_proc_event\n"); 21056ff6be6SSrinivas Pandruvada if (atomic_read(&gyro_state->common_attributes.data_ready)) 211c5bdbef7Ssrinivas pandruvada hid_sensor_push_data(indio_dev, 2128c60c7e7SLars-Peter Clausen gyro_state->gyro_val, 213c5bdbef7Ssrinivas pandruvada sizeof(gyro_state->gyro_val)); 214c5bdbef7Ssrinivas pandruvada 215c5bdbef7Ssrinivas pandruvada return 0; 216c5bdbef7Ssrinivas pandruvada } 217c5bdbef7Ssrinivas pandruvada 218c5bdbef7Ssrinivas pandruvada /* Capture samples in local storage */ 219c5bdbef7Ssrinivas pandruvada static int gyro_3d_capture_sample(struct hid_sensor_hub_device *hsdev, 220c5bdbef7Ssrinivas pandruvada unsigned usage_id, 221c5bdbef7Ssrinivas pandruvada size_t raw_len, char *raw_data, 222c5bdbef7Ssrinivas pandruvada void *priv) 223c5bdbef7Ssrinivas pandruvada { 224c5bdbef7Ssrinivas pandruvada struct iio_dev *indio_dev = platform_get_drvdata(priv); 225c5bdbef7Ssrinivas pandruvada struct gyro_3d_state *gyro_state = iio_priv(indio_dev); 226c5bdbef7Ssrinivas pandruvada int offset; 227c5bdbef7Ssrinivas pandruvada int ret = -EINVAL; 228c5bdbef7Ssrinivas pandruvada 229c5bdbef7Ssrinivas pandruvada switch (usage_id) { 230c5bdbef7Ssrinivas pandruvada case HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS: 231c5bdbef7Ssrinivas pandruvada case HID_USAGE_SENSOR_ANGL_VELOCITY_Y_AXIS: 232c5bdbef7Ssrinivas pandruvada case HID_USAGE_SENSOR_ANGL_VELOCITY_Z_AXIS: 233c5bdbef7Ssrinivas pandruvada offset = usage_id - HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS; 234c5bdbef7Ssrinivas pandruvada gyro_state->gyro_val[CHANNEL_SCAN_INDEX_X + offset] = 235c5bdbef7Ssrinivas pandruvada *(u32 *)raw_data; 236c5bdbef7Ssrinivas pandruvada ret = 0; 237c5bdbef7Ssrinivas pandruvada break; 238c5bdbef7Ssrinivas pandruvada default: 239c5bdbef7Ssrinivas pandruvada break; 240c5bdbef7Ssrinivas pandruvada } 241c5bdbef7Ssrinivas pandruvada 242c5bdbef7Ssrinivas pandruvada return ret; 243c5bdbef7Ssrinivas pandruvada } 244c5bdbef7Ssrinivas pandruvada 245c5bdbef7Ssrinivas pandruvada /* Parse report which is specific to an usage id*/ 246c5bdbef7Ssrinivas pandruvada static int gyro_3d_parse_report(struct platform_device *pdev, 247c5bdbef7Ssrinivas pandruvada struct hid_sensor_hub_device *hsdev, 248c5bdbef7Ssrinivas pandruvada struct iio_chan_spec *channels, 249c5bdbef7Ssrinivas pandruvada unsigned usage_id, 250c5bdbef7Ssrinivas pandruvada struct gyro_3d_state *st) 251c5bdbef7Ssrinivas pandruvada { 252c5bdbef7Ssrinivas pandruvada int ret; 253c5bdbef7Ssrinivas pandruvada int i; 254c5bdbef7Ssrinivas pandruvada 255c5bdbef7Ssrinivas pandruvada for (i = 0; i <= CHANNEL_SCAN_INDEX_Z; ++i) { 256c5bdbef7Ssrinivas pandruvada ret = sensor_hub_input_get_attribute_info(hsdev, 257c5bdbef7Ssrinivas pandruvada HID_INPUT_REPORT, 258c5bdbef7Ssrinivas pandruvada usage_id, 259c5bdbef7Ssrinivas pandruvada HID_USAGE_SENSOR_ANGL_VELOCITY_X_AXIS + i, 260c5bdbef7Ssrinivas pandruvada &st->gyro[CHANNEL_SCAN_INDEX_X + i]); 261c5bdbef7Ssrinivas pandruvada if (ret < 0) 262c5bdbef7Ssrinivas pandruvada break; 263c5bdbef7Ssrinivas pandruvada gyro_3d_adjust_channel_bit_mask(channels, 264c5bdbef7Ssrinivas pandruvada CHANNEL_SCAN_INDEX_X + i, 265c5bdbef7Ssrinivas pandruvada st->gyro[CHANNEL_SCAN_INDEX_X + i].size); 266c5bdbef7Ssrinivas pandruvada } 267c5bdbef7Ssrinivas pandruvada dev_dbg(&pdev->dev, "gyro_3d %x:%x, %x:%x, %x:%x\n", 268c5bdbef7Ssrinivas pandruvada st->gyro[0].index, 269c5bdbef7Ssrinivas pandruvada st->gyro[0].report_id, 270c5bdbef7Ssrinivas pandruvada st->gyro[1].index, st->gyro[1].report_id, 271c5bdbef7Ssrinivas pandruvada st->gyro[2].index, st->gyro[2].report_id); 272c5bdbef7Ssrinivas pandruvada 2736c7db77eSSrinivas Pandruvada st->scale_precision = hid_sensor_format_scale( 2746c7db77eSSrinivas Pandruvada HID_USAGE_SENSOR_GYRO_3D, 2756c7db77eSSrinivas Pandruvada &st->gyro[CHANNEL_SCAN_INDEX_X], 2766c7db77eSSrinivas Pandruvada &st->scale_pre_decml, &st->scale_post_decml); 2776c7db77eSSrinivas Pandruvada 2782371aebfSSrinivas Pandruvada /* Set Sensitivity field ids, when there is no individual modifier */ 2792371aebfSSrinivas Pandruvada if (st->common_attributes.sensitivity.index < 0) { 2802371aebfSSrinivas Pandruvada sensor_hub_input_get_attribute_info(hsdev, 2812371aebfSSrinivas Pandruvada HID_FEATURE_REPORT, usage_id, 2822371aebfSSrinivas Pandruvada HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | 2832371aebfSSrinivas Pandruvada HID_USAGE_SENSOR_DATA_ANGL_VELOCITY, 2842371aebfSSrinivas Pandruvada &st->common_attributes.sensitivity); 2852371aebfSSrinivas Pandruvada dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", 2862371aebfSSrinivas Pandruvada st->common_attributes.sensitivity.index, 2872371aebfSSrinivas Pandruvada st->common_attributes.sensitivity.report_id); 2882371aebfSSrinivas Pandruvada } 289c5bdbef7Ssrinivas pandruvada return ret; 290c5bdbef7Ssrinivas pandruvada } 291c5bdbef7Ssrinivas pandruvada 292c5bdbef7Ssrinivas pandruvada /* Function to initialize the processing for usage id */ 293fc52692cSGreg Kroah-Hartman static int hid_gyro_3d_probe(struct platform_device *pdev) 294c5bdbef7Ssrinivas pandruvada { 295c5bdbef7Ssrinivas pandruvada int ret = 0; 296c5bdbef7Ssrinivas pandruvada static const char *name = "gyro_3d"; 297c5bdbef7Ssrinivas pandruvada struct iio_dev *indio_dev; 298c5bdbef7Ssrinivas pandruvada struct gyro_3d_state *gyro_state; 299c5bdbef7Ssrinivas pandruvada struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 300c5bdbef7Ssrinivas pandruvada 3011ba15cf2SSachin Kamat indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state)); 3021ba15cf2SSachin Kamat if (!indio_dev) 3031ba15cf2SSachin Kamat return -ENOMEM; 304c5bdbef7Ssrinivas pandruvada platform_set_drvdata(pdev, indio_dev); 305c5bdbef7Ssrinivas pandruvada 306c5bdbef7Ssrinivas pandruvada gyro_state = iio_priv(indio_dev); 307c5bdbef7Ssrinivas pandruvada gyro_state->common_attributes.hsdev = hsdev; 308c5bdbef7Ssrinivas pandruvada gyro_state->common_attributes.pdev = pdev; 309c5bdbef7Ssrinivas pandruvada 310c5bdbef7Ssrinivas pandruvada ret = hid_sensor_parse_common_attributes(hsdev, 311c5bdbef7Ssrinivas pandruvada HID_USAGE_SENSOR_GYRO_3D, 312c5bdbef7Ssrinivas pandruvada &gyro_state->common_attributes); 313c5bdbef7Ssrinivas pandruvada if (ret) { 314c5bdbef7Ssrinivas pandruvada dev_err(&pdev->dev, "failed to setup common attributes\n"); 3151ba15cf2SSachin Kamat return ret; 316c5bdbef7Ssrinivas pandruvada } 317c5bdbef7Ssrinivas pandruvada 318d8c9d23eSFabio Estevam indio_dev->channels = kmemdup(gyro_3d_channels, 319d8c9d23eSFabio Estevam sizeof(gyro_3d_channels), GFP_KERNEL); 320d8c9d23eSFabio Estevam if (!indio_dev->channels) { 321c5bdbef7Ssrinivas pandruvada dev_err(&pdev->dev, "failed to duplicate channels\n"); 3221ba15cf2SSachin Kamat return -ENOMEM; 323c5bdbef7Ssrinivas pandruvada } 324c5bdbef7Ssrinivas pandruvada 325d8c9d23eSFabio Estevam ret = gyro_3d_parse_report(pdev, hsdev, 326d8c9d23eSFabio Estevam (struct iio_chan_spec *)indio_dev->channels, 327c5bdbef7Ssrinivas pandruvada HID_USAGE_SENSOR_GYRO_3D, gyro_state); 328c5bdbef7Ssrinivas pandruvada if (ret) { 329c5bdbef7Ssrinivas pandruvada dev_err(&pdev->dev, "failed to setup attributes\n"); 330c5bdbef7Ssrinivas pandruvada goto error_free_dev_mem; 331c5bdbef7Ssrinivas pandruvada } 332c5bdbef7Ssrinivas pandruvada 333c5bdbef7Ssrinivas pandruvada indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels); 334c5bdbef7Ssrinivas pandruvada indio_dev->dev.parent = &pdev->dev; 335c5bdbef7Ssrinivas pandruvada indio_dev->info = &gyro_3d_info; 336c5bdbef7Ssrinivas pandruvada indio_dev->name = name; 337c5bdbef7Ssrinivas pandruvada indio_dev->modes = INDIO_DIRECT_MODE; 338c5bdbef7Ssrinivas pandruvada 339c5bdbef7Ssrinivas pandruvada ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 340c5bdbef7Ssrinivas pandruvada NULL, NULL); 341c5bdbef7Ssrinivas pandruvada if (ret) { 342c5bdbef7Ssrinivas pandruvada dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 343c5bdbef7Ssrinivas pandruvada goto error_free_dev_mem; 344c5bdbef7Ssrinivas pandruvada } 34556ff6be6SSrinivas Pandruvada atomic_set(&gyro_state->common_attributes.data_ready, 0); 346c5bdbef7Ssrinivas pandruvada ret = hid_sensor_setup_trigger(indio_dev, name, 347c5bdbef7Ssrinivas pandruvada &gyro_state->common_attributes); 348c5bdbef7Ssrinivas pandruvada if (ret < 0) { 349c5bdbef7Ssrinivas pandruvada dev_err(&pdev->dev, "trigger setup failed\n"); 350c5bdbef7Ssrinivas pandruvada goto error_unreg_buffer_funcs; 351c5bdbef7Ssrinivas pandruvada } 352c5bdbef7Ssrinivas pandruvada 353c5bdbef7Ssrinivas pandruvada ret = iio_device_register(indio_dev); 354c5bdbef7Ssrinivas pandruvada if (ret) { 355c5bdbef7Ssrinivas pandruvada dev_err(&pdev->dev, "device register failed\n"); 356c5bdbef7Ssrinivas pandruvada goto error_remove_trigger; 357c5bdbef7Ssrinivas pandruvada } 358c5bdbef7Ssrinivas pandruvada 359c5bdbef7Ssrinivas pandruvada gyro_state->callbacks.send_event = gyro_3d_proc_event; 360c5bdbef7Ssrinivas pandruvada gyro_state->callbacks.capture_sample = gyro_3d_capture_sample; 361c5bdbef7Ssrinivas pandruvada gyro_state->callbacks.pdev = pdev; 362c5bdbef7Ssrinivas pandruvada ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D, 363c5bdbef7Ssrinivas pandruvada &gyro_state->callbacks); 364c5bdbef7Ssrinivas pandruvada if (ret < 0) { 365c5bdbef7Ssrinivas pandruvada dev_err(&pdev->dev, "callback reg failed\n"); 366c5bdbef7Ssrinivas pandruvada goto error_iio_unreg; 367c5bdbef7Ssrinivas pandruvada } 368c5bdbef7Ssrinivas pandruvada 369c5bdbef7Ssrinivas pandruvada return ret; 370c5bdbef7Ssrinivas pandruvada 371c5bdbef7Ssrinivas pandruvada error_iio_unreg: 372c5bdbef7Ssrinivas pandruvada iio_device_unregister(indio_dev); 373c5bdbef7Ssrinivas pandruvada error_remove_trigger: 374ec7f68e0SSrinivas Pandruvada hid_sensor_remove_trigger(&gyro_state->common_attributes); 375c5bdbef7Ssrinivas pandruvada error_unreg_buffer_funcs: 376c5bdbef7Ssrinivas pandruvada iio_triggered_buffer_cleanup(indio_dev); 377c5bdbef7Ssrinivas pandruvada error_free_dev_mem: 378c5bdbef7Ssrinivas pandruvada kfree(indio_dev->channels); 379c5bdbef7Ssrinivas pandruvada return ret; 380c5bdbef7Ssrinivas pandruvada } 381c5bdbef7Ssrinivas pandruvada 382c5bdbef7Ssrinivas pandruvada /* Function to deinitialize the processing for usage id */ 383fc52692cSGreg Kroah-Hartman static int hid_gyro_3d_remove(struct platform_device *pdev) 384c5bdbef7Ssrinivas pandruvada { 385c5bdbef7Ssrinivas pandruvada struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 386c5bdbef7Ssrinivas pandruvada struct iio_dev *indio_dev = platform_get_drvdata(pdev); 387ec7f68e0SSrinivas Pandruvada struct gyro_3d_state *gyro_state = iio_priv(indio_dev); 388c5bdbef7Ssrinivas pandruvada 389c5bdbef7Ssrinivas pandruvada sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D); 390c5bdbef7Ssrinivas pandruvada iio_device_unregister(indio_dev); 391ec7f68e0SSrinivas Pandruvada hid_sensor_remove_trigger(&gyro_state->common_attributes); 392c5bdbef7Ssrinivas pandruvada iio_triggered_buffer_cleanup(indio_dev); 393c5bdbef7Ssrinivas pandruvada kfree(indio_dev->channels); 394c5bdbef7Ssrinivas pandruvada 395c5bdbef7Ssrinivas pandruvada return 0; 396c5bdbef7Ssrinivas pandruvada } 397c5bdbef7Ssrinivas pandruvada 398322a3b1eSKrzysztof Kozlowski static const struct platform_device_id hid_gyro_3d_ids[] = { 399a411e735SAlexander Holler { 400a411e735SAlexander Holler /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 401a411e735SAlexander Holler .name = "HID-SENSOR-200076", 402a411e735SAlexander Holler }, 403a411e735SAlexander Holler { /* sentinel */ } 404a411e735SAlexander Holler }; 405a411e735SAlexander Holler MODULE_DEVICE_TABLE(platform, hid_gyro_3d_ids); 406a411e735SAlexander Holler 407c5bdbef7Ssrinivas pandruvada static struct platform_driver hid_gyro_3d_platform_driver = { 408a411e735SAlexander Holler .id_table = hid_gyro_3d_ids, 409c5bdbef7Ssrinivas pandruvada .driver = { 410a411e735SAlexander Holler .name = KBUILD_MODNAME, 41127ce4239SSrinivas Pandruvada .pm = &hid_sensor_pm_ops, 412c5bdbef7Ssrinivas pandruvada }, 413c5bdbef7Ssrinivas pandruvada .probe = hid_gyro_3d_probe, 414c5bdbef7Ssrinivas pandruvada .remove = hid_gyro_3d_remove, 415c5bdbef7Ssrinivas pandruvada }; 416c5bdbef7Ssrinivas pandruvada module_platform_driver(hid_gyro_3d_platform_driver); 417c5bdbef7Ssrinivas pandruvada 418c5bdbef7Ssrinivas pandruvada MODULE_DESCRIPTION("HID Sensor Gyroscope 3D"); 419c5bdbef7Ssrinivas pandruvada MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 420c5bdbef7Ssrinivas pandruvada MODULE_LICENSE("GPL"); 421