18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * HID Sensors Driver 48c2ecf20Sopenharmony_ci * Copyright (c) 2012, Intel Corporation. 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci#include <linux/device.h> 78c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 88c2ecf20Sopenharmony_ci#include <linux/module.h> 98c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 108c2ecf20Sopenharmony_ci#include <linux/irq.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci#include <linux/delay.h> 138c2ecf20Sopenharmony_ci#include <linux/hid-sensor-hub.h> 148c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 158c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 168c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h> 178c2ecf20Sopenharmony_ci#include "../common/hid-sensors/hid-sensor-trigger.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cienum magn_3d_channel { 208c2ecf20Sopenharmony_ci CHANNEL_SCAN_INDEX_X, 218c2ecf20Sopenharmony_ci CHANNEL_SCAN_INDEX_Y, 228c2ecf20Sopenharmony_ci CHANNEL_SCAN_INDEX_Z, 238c2ecf20Sopenharmony_ci CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP, 248c2ecf20Sopenharmony_ci CHANNEL_SCAN_INDEX_NORTH_TRUE_TILT_COMP, 258c2ecf20Sopenharmony_ci CHANNEL_SCAN_INDEX_NORTH_MAGN, 268c2ecf20Sopenharmony_ci CHANNEL_SCAN_INDEX_NORTH_TRUE, 278c2ecf20Sopenharmony_ci MAGN_3D_CHANNEL_MAX, 288c2ecf20Sopenharmony_ci}; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistruct common_attributes { 318c2ecf20Sopenharmony_ci int scale_pre_decml; 328c2ecf20Sopenharmony_ci int scale_post_decml; 338c2ecf20Sopenharmony_ci int scale_precision; 348c2ecf20Sopenharmony_ci int value_offset; 358c2ecf20Sopenharmony_ci}; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistruct magn_3d_state { 388c2ecf20Sopenharmony_ci struct hid_sensor_hub_callbacks callbacks; 398c2ecf20Sopenharmony_ci struct hid_sensor_common magn_flux_attributes; 408c2ecf20Sopenharmony_ci struct hid_sensor_common rot_attributes; 418c2ecf20Sopenharmony_ci struct hid_sensor_hub_attribute_info magn[MAGN_3D_CHANNEL_MAX]; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci /* dynamically sized array to hold sensor values */ 448c2ecf20Sopenharmony_ci u32 *iio_vals; 458c2ecf20Sopenharmony_ci /* array of pointers to sensor value */ 468c2ecf20Sopenharmony_ci u32 *magn_val_addr[MAGN_3D_CHANNEL_MAX]; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci struct common_attributes magn_flux_attr; 498c2ecf20Sopenharmony_ci struct common_attributes rot_attr; 508c2ecf20Sopenharmony_ci}; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic const u32 magn_3d_addresses[MAGN_3D_CHANNEL_MAX] = { 538c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS, 548c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS, 558c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS, 568c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH, 578c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH, 588c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_MAGN_NORTH, 598c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_TRUE_NORTH, 608c2ecf20Sopenharmony_ci}; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* Channel definitions */ 638c2ecf20Sopenharmony_cistatic const struct iio_chan_spec magn_3d_channels[] = { 648c2ecf20Sopenharmony_ci { 658c2ecf20Sopenharmony_ci .type = IIO_MAGN, 668c2ecf20Sopenharmony_ci .modified = 1, 678c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_X, 688c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 698c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 708c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 718c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ) | 728c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_HYSTERESIS), 738c2ecf20Sopenharmony_ci }, { 748c2ecf20Sopenharmony_ci .type = IIO_MAGN, 758c2ecf20Sopenharmony_ci .modified = 1, 768c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_Y, 778c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 788c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 798c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 808c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ) | 818c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_HYSTERESIS), 828c2ecf20Sopenharmony_ci }, { 838c2ecf20Sopenharmony_ci .type = IIO_MAGN, 848c2ecf20Sopenharmony_ci .modified = 1, 858c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_Z, 868c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 878c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 888c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 898c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ) | 908c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_HYSTERESIS), 918c2ecf20Sopenharmony_ci }, { 928c2ecf20Sopenharmony_ci .type = IIO_ROT, 938c2ecf20Sopenharmony_ci .modified = 1, 948c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_NORTH_MAGN_TILT_COMP, 958c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 968c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 978c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 988c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ) | 998c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_HYSTERESIS), 1008c2ecf20Sopenharmony_ci }, { 1018c2ecf20Sopenharmony_ci .type = IIO_ROT, 1028c2ecf20Sopenharmony_ci .modified = 1, 1038c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_NORTH_TRUE_TILT_COMP, 1048c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1058c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 1068c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 1078c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ) | 1088c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_HYSTERESIS), 1098c2ecf20Sopenharmony_ci }, { 1108c2ecf20Sopenharmony_ci .type = IIO_ROT, 1118c2ecf20Sopenharmony_ci .modified = 1, 1128c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_NORTH_MAGN, 1138c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1148c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 1158c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 1168c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ) | 1178c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_HYSTERESIS), 1188c2ecf20Sopenharmony_ci }, { 1198c2ecf20Sopenharmony_ci .type = IIO_ROT, 1208c2ecf20Sopenharmony_ci .modified = 1, 1218c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_NORTH_TRUE, 1228c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1238c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 1248c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 1258c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ) | 1268c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_HYSTERESIS), 1278c2ecf20Sopenharmony_ci } 1288c2ecf20Sopenharmony_ci}; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci/* Adjust channel real bits based on report descriptor */ 1318c2ecf20Sopenharmony_cistatic void magn_3d_adjust_channel_bit_mask(struct iio_chan_spec *channels, 1328c2ecf20Sopenharmony_ci int channel, int size) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci channels[channel].scan_type.sign = 's'; 1358c2ecf20Sopenharmony_ci /* Real storage bits will change based on the report desc. */ 1368c2ecf20Sopenharmony_ci channels[channel].scan_type.realbits = size * 8; 1378c2ecf20Sopenharmony_ci /* Maximum size of a sample to capture is u32 */ 1388c2ecf20Sopenharmony_ci channels[channel].scan_type.storagebits = sizeof(u32) * 8; 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci/* Channel read_raw handler */ 1428c2ecf20Sopenharmony_cistatic int magn_3d_read_raw(struct iio_dev *indio_dev, 1438c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 1448c2ecf20Sopenharmony_ci int *val, int *val2, 1458c2ecf20Sopenharmony_ci long mask) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci struct magn_3d_state *magn_state = iio_priv(indio_dev); 1488c2ecf20Sopenharmony_ci int report_id = -1; 1498c2ecf20Sopenharmony_ci u32 address; 1508c2ecf20Sopenharmony_ci int ret_type; 1518c2ecf20Sopenharmony_ci s32 min; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci *val = 0; 1548c2ecf20Sopenharmony_ci *val2 = 0; 1558c2ecf20Sopenharmony_ci switch (mask) { 1568c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 1578c2ecf20Sopenharmony_ci hid_sensor_power_state(&magn_state->magn_flux_attributes, true); 1588c2ecf20Sopenharmony_ci report_id = magn_state->magn[chan->address].report_id; 1598c2ecf20Sopenharmony_ci min = magn_state->magn[chan->address].logical_minimum; 1608c2ecf20Sopenharmony_ci address = magn_3d_addresses[chan->address]; 1618c2ecf20Sopenharmony_ci if (report_id >= 0) 1628c2ecf20Sopenharmony_ci *val = sensor_hub_input_attr_get_raw_value( 1638c2ecf20Sopenharmony_ci magn_state->magn_flux_attributes.hsdev, 1648c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_COMPASS_3D, address, 1658c2ecf20Sopenharmony_ci report_id, 1668c2ecf20Sopenharmony_ci SENSOR_HUB_SYNC, 1678c2ecf20Sopenharmony_ci min < 0); 1688c2ecf20Sopenharmony_ci else { 1698c2ecf20Sopenharmony_ci *val = 0; 1708c2ecf20Sopenharmony_ci hid_sensor_power_state( 1718c2ecf20Sopenharmony_ci &magn_state->magn_flux_attributes, 1728c2ecf20Sopenharmony_ci false); 1738c2ecf20Sopenharmony_ci return -EINVAL; 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci hid_sensor_power_state(&magn_state->magn_flux_attributes, 1768c2ecf20Sopenharmony_ci false); 1778c2ecf20Sopenharmony_ci ret_type = IIO_VAL_INT; 1788c2ecf20Sopenharmony_ci break; 1798c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 1808c2ecf20Sopenharmony_ci switch (chan->type) { 1818c2ecf20Sopenharmony_ci case IIO_MAGN: 1828c2ecf20Sopenharmony_ci *val = magn_state->magn_flux_attr.scale_pre_decml; 1838c2ecf20Sopenharmony_ci *val2 = magn_state->magn_flux_attr.scale_post_decml; 1848c2ecf20Sopenharmony_ci ret_type = magn_state->magn_flux_attr.scale_precision; 1858c2ecf20Sopenharmony_ci break; 1868c2ecf20Sopenharmony_ci case IIO_ROT: 1878c2ecf20Sopenharmony_ci *val = magn_state->rot_attr.scale_pre_decml; 1888c2ecf20Sopenharmony_ci *val2 = magn_state->rot_attr.scale_post_decml; 1898c2ecf20Sopenharmony_ci ret_type = magn_state->rot_attr.scale_precision; 1908c2ecf20Sopenharmony_ci break; 1918c2ecf20Sopenharmony_ci default: 1928c2ecf20Sopenharmony_ci ret_type = -EINVAL; 1938c2ecf20Sopenharmony_ci } 1948c2ecf20Sopenharmony_ci break; 1958c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_OFFSET: 1968c2ecf20Sopenharmony_ci switch (chan->type) { 1978c2ecf20Sopenharmony_ci case IIO_MAGN: 1988c2ecf20Sopenharmony_ci *val = magn_state->magn_flux_attr.value_offset; 1998c2ecf20Sopenharmony_ci ret_type = IIO_VAL_INT; 2008c2ecf20Sopenharmony_ci break; 2018c2ecf20Sopenharmony_ci case IIO_ROT: 2028c2ecf20Sopenharmony_ci *val = magn_state->rot_attr.value_offset; 2038c2ecf20Sopenharmony_ci ret_type = IIO_VAL_INT; 2048c2ecf20Sopenharmony_ci break; 2058c2ecf20Sopenharmony_ci default: 2068c2ecf20Sopenharmony_ci ret_type = -EINVAL; 2078c2ecf20Sopenharmony_ci } 2088c2ecf20Sopenharmony_ci break; 2098c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 2108c2ecf20Sopenharmony_ci ret_type = hid_sensor_read_samp_freq_value( 2118c2ecf20Sopenharmony_ci &magn_state->magn_flux_attributes, val, val2); 2128c2ecf20Sopenharmony_ci break; 2138c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_HYSTERESIS: 2148c2ecf20Sopenharmony_ci switch (chan->type) { 2158c2ecf20Sopenharmony_ci case IIO_MAGN: 2168c2ecf20Sopenharmony_ci ret_type = hid_sensor_read_raw_hyst_value( 2178c2ecf20Sopenharmony_ci &magn_state->magn_flux_attributes, val, val2); 2188c2ecf20Sopenharmony_ci break; 2198c2ecf20Sopenharmony_ci case IIO_ROT: 2208c2ecf20Sopenharmony_ci ret_type = hid_sensor_read_raw_hyst_value( 2218c2ecf20Sopenharmony_ci &magn_state->rot_attributes, val, val2); 2228c2ecf20Sopenharmony_ci break; 2238c2ecf20Sopenharmony_ci default: 2248c2ecf20Sopenharmony_ci ret_type = -EINVAL; 2258c2ecf20Sopenharmony_ci } 2268c2ecf20Sopenharmony_ci break; 2278c2ecf20Sopenharmony_ci default: 2288c2ecf20Sopenharmony_ci ret_type = -EINVAL; 2298c2ecf20Sopenharmony_ci break; 2308c2ecf20Sopenharmony_ci } 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci return ret_type; 2338c2ecf20Sopenharmony_ci} 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci/* Channel write_raw handler */ 2368c2ecf20Sopenharmony_cistatic int magn_3d_write_raw(struct iio_dev *indio_dev, 2378c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 2388c2ecf20Sopenharmony_ci int val, 2398c2ecf20Sopenharmony_ci int val2, 2408c2ecf20Sopenharmony_ci long mask) 2418c2ecf20Sopenharmony_ci{ 2428c2ecf20Sopenharmony_ci struct magn_3d_state *magn_state = iio_priv(indio_dev); 2438c2ecf20Sopenharmony_ci int ret = 0; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci switch (mask) { 2468c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 2478c2ecf20Sopenharmony_ci ret = hid_sensor_write_samp_freq_value( 2488c2ecf20Sopenharmony_ci &magn_state->magn_flux_attributes, val, val2); 2498c2ecf20Sopenharmony_ci break; 2508c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_HYSTERESIS: 2518c2ecf20Sopenharmony_ci switch (chan->type) { 2528c2ecf20Sopenharmony_ci case IIO_MAGN: 2538c2ecf20Sopenharmony_ci ret = hid_sensor_write_raw_hyst_value( 2548c2ecf20Sopenharmony_ci &magn_state->magn_flux_attributes, val, val2); 2558c2ecf20Sopenharmony_ci break; 2568c2ecf20Sopenharmony_ci case IIO_ROT: 2578c2ecf20Sopenharmony_ci ret = hid_sensor_write_raw_hyst_value( 2588c2ecf20Sopenharmony_ci &magn_state->rot_attributes, val, val2); 2598c2ecf20Sopenharmony_ci break; 2608c2ecf20Sopenharmony_ci default: 2618c2ecf20Sopenharmony_ci ret = -EINVAL; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci break; 2648c2ecf20Sopenharmony_ci default: 2658c2ecf20Sopenharmony_ci ret = -EINVAL; 2668c2ecf20Sopenharmony_ci } 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci return ret; 2698c2ecf20Sopenharmony_ci} 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_cistatic const struct iio_info magn_3d_info = { 2728c2ecf20Sopenharmony_ci .read_raw = &magn_3d_read_raw, 2738c2ecf20Sopenharmony_ci .write_raw = &magn_3d_write_raw, 2748c2ecf20Sopenharmony_ci}; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci/* Function to push data to buffer */ 2778c2ecf20Sopenharmony_cistatic void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); 2808c2ecf20Sopenharmony_ci iio_push_to_buffers(indio_dev, data); 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci/* Callback handler to send event after all samples are received and captured */ 2848c2ecf20Sopenharmony_cistatic int magn_3d_proc_event(struct hid_sensor_hub_device *hsdev, 2858c2ecf20Sopenharmony_ci unsigned usage_id, 2868c2ecf20Sopenharmony_ci void *priv) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = platform_get_drvdata(priv); 2898c2ecf20Sopenharmony_ci struct magn_3d_state *magn_state = iio_priv(indio_dev); 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci dev_dbg(&indio_dev->dev, "magn_3d_proc_event\n"); 2928c2ecf20Sopenharmony_ci if (atomic_read(&magn_state->magn_flux_attributes.data_ready)) 2938c2ecf20Sopenharmony_ci hid_sensor_push_data(indio_dev, magn_state->iio_vals); 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci return 0; 2968c2ecf20Sopenharmony_ci} 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci/* Capture samples in local storage */ 2998c2ecf20Sopenharmony_cistatic int magn_3d_capture_sample(struct hid_sensor_hub_device *hsdev, 3008c2ecf20Sopenharmony_ci unsigned usage_id, 3018c2ecf20Sopenharmony_ci size_t raw_len, char *raw_data, 3028c2ecf20Sopenharmony_ci void *priv) 3038c2ecf20Sopenharmony_ci{ 3048c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = platform_get_drvdata(priv); 3058c2ecf20Sopenharmony_ci struct magn_3d_state *magn_state = iio_priv(indio_dev); 3068c2ecf20Sopenharmony_ci int offset; 3078c2ecf20Sopenharmony_ci int ret = 0; 3088c2ecf20Sopenharmony_ci u32 *iio_val = NULL; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci switch (usage_id) { 3118c2ecf20Sopenharmony_ci case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS: 3128c2ecf20Sopenharmony_ci case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Y_AXIS: 3138c2ecf20Sopenharmony_ci case HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_Z_AXIS: 3148c2ecf20Sopenharmony_ci offset = (usage_id - HID_USAGE_SENSOR_ORIENT_MAGN_FLUX_X_AXIS) 3158c2ecf20Sopenharmony_ci + CHANNEL_SCAN_INDEX_X; 3168c2ecf20Sopenharmony_ci break; 3178c2ecf20Sopenharmony_ci case HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH: 3188c2ecf20Sopenharmony_ci case HID_USAGE_SENSOR_ORIENT_COMP_TRUE_NORTH: 3198c2ecf20Sopenharmony_ci case HID_USAGE_SENSOR_ORIENT_MAGN_NORTH: 3208c2ecf20Sopenharmony_ci case HID_USAGE_SENSOR_ORIENT_TRUE_NORTH: 3218c2ecf20Sopenharmony_ci offset = (usage_id - HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH) 3228c2ecf20Sopenharmony_ci + CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP; 3238c2ecf20Sopenharmony_ci break; 3248c2ecf20Sopenharmony_ci default: 3258c2ecf20Sopenharmony_ci return -EINVAL; 3268c2ecf20Sopenharmony_ci } 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci iio_val = magn_state->magn_val_addr[offset]; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci if (iio_val != NULL) 3318c2ecf20Sopenharmony_ci *iio_val = *((u32 *)raw_data); 3328c2ecf20Sopenharmony_ci else 3338c2ecf20Sopenharmony_ci ret = -EINVAL; 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci return ret; 3368c2ecf20Sopenharmony_ci} 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci/* Parse report which is specific to an usage id*/ 3398c2ecf20Sopenharmony_cistatic int magn_3d_parse_report(struct platform_device *pdev, 3408c2ecf20Sopenharmony_ci struct hid_sensor_hub_device *hsdev, 3418c2ecf20Sopenharmony_ci struct iio_chan_spec **channels, 3428c2ecf20Sopenharmony_ci int *chan_count, 3438c2ecf20Sopenharmony_ci unsigned usage_id, 3448c2ecf20Sopenharmony_ci struct magn_3d_state *st) 3458c2ecf20Sopenharmony_ci{ 3468c2ecf20Sopenharmony_ci int i; 3478c2ecf20Sopenharmony_ci int attr_count = 0; 3488c2ecf20Sopenharmony_ci struct iio_chan_spec *_channels; 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci /* Scan for each usage attribute supported */ 3518c2ecf20Sopenharmony_ci for (i = 0; i < MAGN_3D_CHANNEL_MAX; i++) { 3528c2ecf20Sopenharmony_ci int status; 3538c2ecf20Sopenharmony_ci u32 address = magn_3d_addresses[i]; 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci /* Check if usage attribute exists in the sensor hub device */ 3568c2ecf20Sopenharmony_ci status = sensor_hub_input_get_attribute_info(hsdev, 3578c2ecf20Sopenharmony_ci HID_INPUT_REPORT, 3588c2ecf20Sopenharmony_ci usage_id, 3598c2ecf20Sopenharmony_ci address, 3608c2ecf20Sopenharmony_ci &(st->magn[i])); 3618c2ecf20Sopenharmony_ci if (!status) 3628c2ecf20Sopenharmony_ci attr_count++; 3638c2ecf20Sopenharmony_ci } 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci if (attr_count <= 0) { 3668c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 3678c2ecf20Sopenharmony_ci "failed to find any supported usage attributes in report\n"); 3688c2ecf20Sopenharmony_ci return -EINVAL; 3698c2ecf20Sopenharmony_ci } 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "magn_3d Found %d usage attributes\n", 3728c2ecf20Sopenharmony_ci attr_count); 3738c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "magn_3d X: %x:%x Y: %x:%x Z: %x:%x\n", 3748c2ecf20Sopenharmony_ci st->magn[0].index, 3758c2ecf20Sopenharmony_ci st->magn[0].report_id, 3768c2ecf20Sopenharmony_ci st->magn[1].index, st->magn[1].report_id, 3778c2ecf20Sopenharmony_ci st->magn[2].index, st->magn[2].report_id); 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci /* Setup IIO channel array */ 3808c2ecf20Sopenharmony_ci _channels = devm_kcalloc(&pdev->dev, attr_count, 3818c2ecf20Sopenharmony_ci sizeof(struct iio_chan_spec), 3828c2ecf20Sopenharmony_ci GFP_KERNEL); 3838c2ecf20Sopenharmony_ci if (!_channels) { 3848c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 3858c2ecf20Sopenharmony_ci "failed to allocate space for iio channels\n"); 3868c2ecf20Sopenharmony_ci return -ENOMEM; 3878c2ecf20Sopenharmony_ci } 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci st->iio_vals = devm_kcalloc(&pdev->dev, attr_count, 3908c2ecf20Sopenharmony_ci sizeof(u32), 3918c2ecf20Sopenharmony_ci GFP_KERNEL); 3928c2ecf20Sopenharmony_ci if (!st->iio_vals) { 3938c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 3948c2ecf20Sopenharmony_ci "failed to allocate space for iio values array\n"); 3958c2ecf20Sopenharmony_ci return -ENOMEM; 3968c2ecf20Sopenharmony_ci } 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci for (i = 0, *chan_count = 0; 3998c2ecf20Sopenharmony_ci i < MAGN_3D_CHANNEL_MAX && *chan_count < attr_count; 4008c2ecf20Sopenharmony_ci i++){ 4018c2ecf20Sopenharmony_ci if (st->magn[i].index >= 0) { 4028c2ecf20Sopenharmony_ci /* Setup IIO channel struct */ 4038c2ecf20Sopenharmony_ci (_channels[*chan_count]) = magn_3d_channels[i]; 4048c2ecf20Sopenharmony_ci (_channels[*chan_count]).scan_index = *chan_count; 4058c2ecf20Sopenharmony_ci (_channels[*chan_count]).address = i; 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci /* Set magn_val_addr to iio value address */ 4088c2ecf20Sopenharmony_ci st->magn_val_addr[i] = &(st->iio_vals[*chan_count]); 4098c2ecf20Sopenharmony_ci magn_3d_adjust_channel_bit_mask(_channels, 4108c2ecf20Sopenharmony_ci *chan_count, 4118c2ecf20Sopenharmony_ci st->magn[i].size); 4128c2ecf20Sopenharmony_ci (*chan_count)++; 4138c2ecf20Sopenharmony_ci } 4148c2ecf20Sopenharmony_ci } 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci if (*chan_count <= 0) { 4178c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 4188c2ecf20Sopenharmony_ci "failed to find any magnetic channels setup\n"); 4198c2ecf20Sopenharmony_ci return -EINVAL; 4208c2ecf20Sopenharmony_ci } 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci *channels = _channels; 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "magn_3d Setup %d IIO channels\n", 4258c2ecf20Sopenharmony_ci *chan_count); 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci st->magn_flux_attr.scale_precision = hid_sensor_format_scale( 4288c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_COMPASS_3D, 4298c2ecf20Sopenharmony_ci &st->magn[CHANNEL_SCAN_INDEX_X], 4308c2ecf20Sopenharmony_ci &st->magn_flux_attr.scale_pre_decml, 4318c2ecf20Sopenharmony_ci &st->magn_flux_attr.scale_post_decml); 4328c2ecf20Sopenharmony_ci st->rot_attr.scale_precision 4338c2ecf20Sopenharmony_ci = hid_sensor_format_scale( 4348c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH, 4358c2ecf20Sopenharmony_ci &st->magn[CHANNEL_SCAN_INDEX_NORTH_MAGN_TILT_COMP], 4368c2ecf20Sopenharmony_ci &st->rot_attr.scale_pre_decml, 4378c2ecf20Sopenharmony_ci &st->rot_attr.scale_post_decml); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci /* Set Sensitivity field ids, when there is no individual modifier */ 4408c2ecf20Sopenharmony_ci if (st->magn_flux_attributes.sensitivity.index < 0) { 4418c2ecf20Sopenharmony_ci sensor_hub_input_get_attribute_info(hsdev, 4428c2ecf20Sopenharmony_ci HID_FEATURE_REPORT, usage_id, 4438c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | 4448c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_DATA_ORIENTATION, 4458c2ecf20Sopenharmony_ci &st->magn_flux_attributes.sensitivity); 4468c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", 4478c2ecf20Sopenharmony_ci st->magn_flux_attributes.sensitivity.index, 4488c2ecf20Sopenharmony_ci st->magn_flux_attributes.sensitivity.report_id); 4498c2ecf20Sopenharmony_ci } 4508c2ecf20Sopenharmony_ci if (st->magn_flux_attributes.sensitivity.index < 0) { 4518c2ecf20Sopenharmony_ci sensor_hub_input_get_attribute_info(hsdev, 4528c2ecf20Sopenharmony_ci HID_FEATURE_REPORT, usage_id, 4538c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | 4548c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_MAGN_FLUX, 4558c2ecf20Sopenharmony_ci &st->magn_flux_attributes.sensitivity); 4568c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", 4578c2ecf20Sopenharmony_ci st->magn_flux_attributes.sensitivity.index, 4588c2ecf20Sopenharmony_ci st->magn_flux_attributes.sensitivity.report_id); 4598c2ecf20Sopenharmony_ci } 4608c2ecf20Sopenharmony_ci if (st->rot_attributes.sensitivity.index < 0) { 4618c2ecf20Sopenharmony_ci sensor_hub_input_get_attribute_info(hsdev, 4628c2ecf20Sopenharmony_ci HID_FEATURE_REPORT, usage_id, 4638c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | 4648c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_ORIENT_COMP_MAGN_NORTH, 4658c2ecf20Sopenharmony_ci &st->rot_attributes.sensitivity); 4668c2ecf20Sopenharmony_ci dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", 4678c2ecf20Sopenharmony_ci st->rot_attributes.sensitivity.index, 4688c2ecf20Sopenharmony_ci st->rot_attributes.sensitivity.report_id); 4698c2ecf20Sopenharmony_ci } 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci return 0; 4728c2ecf20Sopenharmony_ci} 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci/* Function to initialize the processing for usage id */ 4758c2ecf20Sopenharmony_cistatic int hid_magn_3d_probe(struct platform_device *pdev) 4768c2ecf20Sopenharmony_ci{ 4778c2ecf20Sopenharmony_ci int ret = 0; 4788c2ecf20Sopenharmony_ci static char *name = "magn_3d"; 4798c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 4808c2ecf20Sopenharmony_ci struct magn_3d_state *magn_state; 4818c2ecf20Sopenharmony_ci struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 4828c2ecf20Sopenharmony_ci struct iio_chan_spec *channels; 4838c2ecf20Sopenharmony_ci int chan_count = 0; 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&pdev->dev, 4868c2ecf20Sopenharmony_ci sizeof(struct magn_3d_state)); 4878c2ecf20Sopenharmony_ci if (indio_dev == NULL) 4888c2ecf20Sopenharmony_ci return -ENOMEM; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, indio_dev); 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci magn_state = iio_priv(indio_dev); 4938c2ecf20Sopenharmony_ci magn_state->magn_flux_attributes.hsdev = hsdev; 4948c2ecf20Sopenharmony_ci magn_state->magn_flux_attributes.pdev = pdev; 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci ret = hid_sensor_parse_common_attributes(hsdev, 4978c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_COMPASS_3D, 4988c2ecf20Sopenharmony_ci &magn_state->magn_flux_attributes); 4998c2ecf20Sopenharmony_ci if (ret) { 5008c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to setup common attributes\n"); 5018c2ecf20Sopenharmony_ci return ret; 5028c2ecf20Sopenharmony_ci } 5038c2ecf20Sopenharmony_ci magn_state->rot_attributes = magn_state->magn_flux_attributes; 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci ret = magn_3d_parse_report(pdev, hsdev, 5068c2ecf20Sopenharmony_ci &channels, &chan_count, 5078c2ecf20Sopenharmony_ci HID_USAGE_SENSOR_COMPASS_3D, magn_state); 5088c2ecf20Sopenharmony_ci if (ret) { 5098c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "failed to parse report\n"); 5108c2ecf20Sopenharmony_ci return ret; 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci indio_dev->channels = channels; 5148c2ecf20Sopenharmony_ci indio_dev->num_channels = chan_count; 5158c2ecf20Sopenharmony_ci indio_dev->info = &magn_3d_info; 5168c2ecf20Sopenharmony_ci indio_dev->name = name; 5178c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci atomic_set(&magn_state->magn_flux_attributes.data_ready, 0); 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci ret = hid_sensor_setup_trigger(indio_dev, name, 5228c2ecf20Sopenharmony_ci &magn_state->magn_flux_attributes); 5238c2ecf20Sopenharmony_ci if (ret < 0) { 5248c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "trigger setup failed\n"); 5258c2ecf20Sopenharmony_ci return ret; 5268c2ecf20Sopenharmony_ci } 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci ret = iio_device_register(indio_dev); 5298c2ecf20Sopenharmony_ci if (ret) { 5308c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "device register failed\n"); 5318c2ecf20Sopenharmony_ci goto error_remove_trigger; 5328c2ecf20Sopenharmony_ci } 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci magn_state->callbacks.send_event = magn_3d_proc_event; 5358c2ecf20Sopenharmony_ci magn_state->callbacks.capture_sample = magn_3d_capture_sample; 5368c2ecf20Sopenharmony_ci magn_state->callbacks.pdev = pdev; 5378c2ecf20Sopenharmony_ci ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D, 5388c2ecf20Sopenharmony_ci &magn_state->callbacks); 5398c2ecf20Sopenharmony_ci if (ret < 0) { 5408c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "callback reg failed\n"); 5418c2ecf20Sopenharmony_ci goto error_iio_unreg; 5428c2ecf20Sopenharmony_ci } 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci return ret; 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_cierror_iio_unreg: 5478c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 5488c2ecf20Sopenharmony_cierror_remove_trigger: 5498c2ecf20Sopenharmony_ci hid_sensor_remove_trigger(indio_dev, &magn_state->magn_flux_attributes); 5508c2ecf20Sopenharmony_ci return ret; 5518c2ecf20Sopenharmony_ci} 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci/* Function to deinitialize the processing for usage id */ 5548c2ecf20Sopenharmony_cistatic int hid_magn_3d_remove(struct platform_device *pdev) 5558c2ecf20Sopenharmony_ci{ 5568c2ecf20Sopenharmony_ci struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 5578c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = platform_get_drvdata(pdev); 5588c2ecf20Sopenharmony_ci struct magn_3d_state *magn_state = iio_priv(indio_dev); 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D); 5618c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 5628c2ecf20Sopenharmony_ci hid_sensor_remove_trigger(indio_dev, &magn_state->magn_flux_attributes); 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci return 0; 5658c2ecf20Sopenharmony_ci} 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_cistatic const struct platform_device_id hid_magn_3d_ids[] = { 5688c2ecf20Sopenharmony_ci { 5698c2ecf20Sopenharmony_ci /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 5708c2ecf20Sopenharmony_ci .name = "HID-SENSOR-200083", 5718c2ecf20Sopenharmony_ci }, 5728c2ecf20Sopenharmony_ci { /* sentinel */ } 5738c2ecf20Sopenharmony_ci}; 5748c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, hid_magn_3d_ids); 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_cistatic struct platform_driver hid_magn_3d_platform_driver = { 5778c2ecf20Sopenharmony_ci .id_table = hid_magn_3d_ids, 5788c2ecf20Sopenharmony_ci .driver = { 5798c2ecf20Sopenharmony_ci .name = KBUILD_MODNAME, 5808c2ecf20Sopenharmony_ci .pm = &hid_sensor_pm_ops, 5818c2ecf20Sopenharmony_ci }, 5828c2ecf20Sopenharmony_ci .probe = hid_magn_3d_probe, 5838c2ecf20Sopenharmony_ci .remove = hid_magn_3d_remove, 5848c2ecf20Sopenharmony_ci}; 5858c2ecf20Sopenharmony_cimodule_platform_driver(hid_magn_3d_platform_driver); 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HID Sensor Magnetometer 3D"); 5888c2ecf20Sopenharmony_ciMODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 5898c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 590