18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * MAX44000 Ambient and Infrared Proximity Sensor 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2016, Intel Corporation. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Data sheet: https://datasheets.maximintegrated.com/en/ds/MAX44000.pdf 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * 7-bit I2C slave address 0x4a 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/i2c.h> 158c2ecf20Sopenharmony_ci#include <linux/regmap.h> 168c2ecf20Sopenharmony_ci#include <linux/util_macros.h> 178c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 188c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 198c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h> 208c2ecf20Sopenharmony_ci#include <linux/iio/trigger_consumer.h> 218c2ecf20Sopenharmony_ci#include <linux/iio/triggered_buffer.h> 228c2ecf20Sopenharmony_ci#include <linux/acpi.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define MAX44000_DRV_NAME "max44000" 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/* Registers in datasheet order */ 278c2ecf20Sopenharmony_ci#define MAX44000_REG_STATUS 0x00 288c2ecf20Sopenharmony_ci#define MAX44000_REG_CFG_MAIN 0x01 298c2ecf20Sopenharmony_ci#define MAX44000_REG_CFG_RX 0x02 308c2ecf20Sopenharmony_ci#define MAX44000_REG_CFG_TX 0x03 318c2ecf20Sopenharmony_ci#define MAX44000_REG_ALS_DATA_HI 0x04 328c2ecf20Sopenharmony_ci#define MAX44000_REG_ALS_DATA_LO 0x05 338c2ecf20Sopenharmony_ci#define MAX44000_REG_PRX_DATA 0x16 348c2ecf20Sopenharmony_ci#define MAX44000_REG_ALS_UPTHR_HI 0x06 358c2ecf20Sopenharmony_ci#define MAX44000_REG_ALS_UPTHR_LO 0x07 368c2ecf20Sopenharmony_ci#define MAX44000_REG_ALS_LOTHR_HI 0x08 378c2ecf20Sopenharmony_ci#define MAX44000_REG_ALS_LOTHR_LO 0x09 388c2ecf20Sopenharmony_ci#define MAX44000_REG_PST 0x0a 398c2ecf20Sopenharmony_ci#define MAX44000_REG_PRX_IND 0x0b 408c2ecf20Sopenharmony_ci#define MAX44000_REG_PRX_THR 0x0c 418c2ecf20Sopenharmony_ci#define MAX44000_REG_TRIM_GAIN_GREEN 0x0f 428c2ecf20Sopenharmony_ci#define MAX44000_REG_TRIM_GAIN_IR 0x10 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* REG_CFG bits */ 458c2ecf20Sopenharmony_ci#define MAX44000_CFG_ALSINTE 0x01 468c2ecf20Sopenharmony_ci#define MAX44000_CFG_PRXINTE 0x02 478c2ecf20Sopenharmony_ci#define MAX44000_CFG_MASK 0x1c 488c2ecf20Sopenharmony_ci#define MAX44000_CFG_MODE_SHUTDOWN 0x00 498c2ecf20Sopenharmony_ci#define MAX44000_CFG_MODE_ALS_GIR 0x04 508c2ecf20Sopenharmony_ci#define MAX44000_CFG_MODE_ALS_G 0x08 518c2ecf20Sopenharmony_ci#define MAX44000_CFG_MODE_ALS_IR 0x0c 528c2ecf20Sopenharmony_ci#define MAX44000_CFG_MODE_ALS_PRX 0x10 538c2ecf20Sopenharmony_ci#define MAX44000_CFG_MODE_PRX 0x14 548c2ecf20Sopenharmony_ci#define MAX44000_CFG_TRIM 0x20 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* 578c2ecf20Sopenharmony_ci * Upper 4 bits are not documented but start as 1 on powerup 588c2ecf20Sopenharmony_ci * Setting them to 0 causes proximity to misbehave so set them to 1 598c2ecf20Sopenharmony_ci */ 608c2ecf20Sopenharmony_ci#define MAX44000_REG_CFG_RX_DEFAULT 0xf0 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci/* REG_RX bits */ 638c2ecf20Sopenharmony_ci#define MAX44000_CFG_RX_ALSTIM_MASK 0x0c 648c2ecf20Sopenharmony_ci#define MAX44000_CFG_RX_ALSTIM_SHIFT 2 658c2ecf20Sopenharmony_ci#define MAX44000_CFG_RX_ALSPGA_MASK 0x03 668c2ecf20Sopenharmony_ci#define MAX44000_CFG_RX_ALSPGA_SHIFT 0 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci/* REG_TX bits */ 698c2ecf20Sopenharmony_ci#define MAX44000_LED_CURRENT_MASK 0xf 708c2ecf20Sopenharmony_ci#define MAX44000_LED_CURRENT_MAX 11 718c2ecf20Sopenharmony_ci#define MAX44000_LED_CURRENT_DEFAULT 6 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci#define MAX44000_ALSDATA_OVERFLOW 0x4000 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistruct max44000_data { 768c2ecf20Sopenharmony_ci struct mutex lock; 778c2ecf20Sopenharmony_ci struct regmap *regmap; 788c2ecf20Sopenharmony_ci /* Ensure naturally aligned timestamp */ 798c2ecf20Sopenharmony_ci struct { 808c2ecf20Sopenharmony_ci u16 channels[2]; 818c2ecf20Sopenharmony_ci s64 ts __aligned(8); 828c2ecf20Sopenharmony_ci } scan; 838c2ecf20Sopenharmony_ci}; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci/* Default scale is set to the minimum of 0.03125 or 1 / (1 << 5) lux */ 868c2ecf20Sopenharmony_ci#define MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 5 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci/* Scale can be multiplied by up to 128x via ALSPGA for measurement gain */ 898c2ecf20Sopenharmony_cistatic const int max44000_alspga_shift[] = {0, 2, 4, 7}; 908c2ecf20Sopenharmony_ci#define MAX44000_ALSPGA_MAX_SHIFT 7 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci/* 938c2ecf20Sopenharmony_ci * Scale can be multiplied by up to 64x via ALSTIM because of lost resolution 948c2ecf20Sopenharmony_ci * 958c2ecf20Sopenharmony_ci * This scaling factor is hidden from userspace and instead accounted for when 968c2ecf20Sopenharmony_ci * reading raw values from the device. 978c2ecf20Sopenharmony_ci * 988c2ecf20Sopenharmony_ci * This makes it possible to cleanly expose ALSPGA as IIO_CHAN_INFO_SCALE and 998c2ecf20Sopenharmony_ci * ALSTIM as IIO_CHAN_INFO_INT_TIME without the values affecting each other. 1008c2ecf20Sopenharmony_ci * 1018c2ecf20Sopenharmony_ci * Handling this internally is also required for buffer support because the 1028c2ecf20Sopenharmony_ci * channel's scan_type can't be modified dynamically. 1038c2ecf20Sopenharmony_ci */ 1048c2ecf20Sopenharmony_ci#define MAX44000_ALSTIM_SHIFT(alstim) (2 * (alstim)) 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci/* Available integration times with pretty manual alignment: */ 1078c2ecf20Sopenharmony_cistatic const int max44000_int_time_avail_ns_array[] = { 1088c2ecf20Sopenharmony_ci 100000000, 1098c2ecf20Sopenharmony_ci 25000000, 1108c2ecf20Sopenharmony_ci 6250000, 1118c2ecf20Sopenharmony_ci 1562500, 1128c2ecf20Sopenharmony_ci}; 1138c2ecf20Sopenharmony_cistatic const char max44000_int_time_avail_str[] = 1148c2ecf20Sopenharmony_ci "0.100 " 1158c2ecf20Sopenharmony_ci "0.025 " 1168c2ecf20Sopenharmony_ci "0.00625 " 1178c2ecf20Sopenharmony_ci "0.0015625"; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci/* Available scales (internal to ulux) with pretty manual alignment: */ 1208c2ecf20Sopenharmony_cistatic const int max44000_scale_avail_ulux_array[] = { 1218c2ecf20Sopenharmony_ci 31250, 1228c2ecf20Sopenharmony_ci 125000, 1238c2ecf20Sopenharmony_ci 500000, 1248c2ecf20Sopenharmony_ci 4000000, 1258c2ecf20Sopenharmony_ci}; 1268c2ecf20Sopenharmony_cistatic const char max44000_scale_avail_str[] = 1278c2ecf20Sopenharmony_ci "0.03125 " 1288c2ecf20Sopenharmony_ci "0.125 " 1298c2ecf20Sopenharmony_ci "0.5 " 1308c2ecf20Sopenharmony_ci "4"; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci#define MAX44000_SCAN_INDEX_ALS 0 1338c2ecf20Sopenharmony_ci#define MAX44000_SCAN_INDEX_PRX 1 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_cistatic const struct iio_chan_spec max44000_channels[] = { 1368c2ecf20Sopenharmony_ci { 1378c2ecf20Sopenharmony_ci .type = IIO_LIGHT, 1388c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1398c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | 1408c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_INT_TIME), 1418c2ecf20Sopenharmony_ci .scan_index = MAX44000_SCAN_INDEX_ALS, 1428c2ecf20Sopenharmony_ci .scan_type = { 1438c2ecf20Sopenharmony_ci .sign = 'u', 1448c2ecf20Sopenharmony_ci .realbits = 14, 1458c2ecf20Sopenharmony_ci .storagebits = 16, 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci }, 1488c2ecf20Sopenharmony_ci { 1498c2ecf20Sopenharmony_ci .type = IIO_PROXIMITY, 1508c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 1518c2ecf20Sopenharmony_ci .scan_index = MAX44000_SCAN_INDEX_PRX, 1528c2ecf20Sopenharmony_ci .scan_type = { 1538c2ecf20Sopenharmony_ci .sign = 'u', 1548c2ecf20Sopenharmony_ci .realbits = 8, 1558c2ecf20Sopenharmony_ci .storagebits = 16, 1568c2ecf20Sopenharmony_ci } 1578c2ecf20Sopenharmony_ci }, 1588c2ecf20Sopenharmony_ci IIO_CHAN_SOFT_TIMESTAMP(2), 1598c2ecf20Sopenharmony_ci { 1608c2ecf20Sopenharmony_ci .type = IIO_CURRENT, 1618c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1628c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE), 1638c2ecf20Sopenharmony_ci .extend_name = "led", 1648c2ecf20Sopenharmony_ci .output = 1, 1658c2ecf20Sopenharmony_ci .scan_index = -1, 1668c2ecf20Sopenharmony_ci }, 1678c2ecf20Sopenharmony_ci}; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_cistatic int max44000_read_alstim(struct max44000_data *data) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci unsigned int val; 1728c2ecf20Sopenharmony_ci int ret; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val); 1758c2ecf20Sopenharmony_ci if (ret < 0) 1768c2ecf20Sopenharmony_ci return ret; 1778c2ecf20Sopenharmony_ci return (val & MAX44000_CFG_RX_ALSTIM_MASK) >> MAX44000_CFG_RX_ALSTIM_SHIFT; 1788c2ecf20Sopenharmony_ci} 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cistatic int max44000_write_alstim(struct max44000_data *data, int val) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX, 1838c2ecf20Sopenharmony_ci MAX44000_CFG_RX_ALSTIM_MASK, 1848c2ecf20Sopenharmony_ci val << MAX44000_CFG_RX_ALSTIM_SHIFT); 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cistatic int max44000_read_alspga(struct max44000_data *data) 1888c2ecf20Sopenharmony_ci{ 1898c2ecf20Sopenharmony_ci unsigned int val; 1908c2ecf20Sopenharmony_ci int ret; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val); 1938c2ecf20Sopenharmony_ci if (ret < 0) 1948c2ecf20Sopenharmony_ci return ret; 1958c2ecf20Sopenharmony_ci return (val & MAX44000_CFG_RX_ALSPGA_MASK) >> MAX44000_CFG_RX_ALSPGA_SHIFT; 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic int max44000_write_alspga(struct max44000_data *data, int val) 1998c2ecf20Sopenharmony_ci{ 2008c2ecf20Sopenharmony_ci return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX, 2018c2ecf20Sopenharmony_ci MAX44000_CFG_RX_ALSPGA_MASK, 2028c2ecf20Sopenharmony_ci val << MAX44000_CFG_RX_ALSPGA_SHIFT); 2038c2ecf20Sopenharmony_ci} 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_cistatic int max44000_read_alsval(struct max44000_data *data) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci u16 regval; 2088c2ecf20Sopenharmony_ci __be16 val; 2098c2ecf20Sopenharmony_ci int alstim, ret; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci ret = regmap_bulk_read(data->regmap, MAX44000_REG_ALS_DATA_HI, 2128c2ecf20Sopenharmony_ci &val, sizeof(val)); 2138c2ecf20Sopenharmony_ci if (ret < 0) 2148c2ecf20Sopenharmony_ci return ret; 2158c2ecf20Sopenharmony_ci alstim = ret = max44000_read_alstim(data); 2168c2ecf20Sopenharmony_ci if (ret < 0) 2178c2ecf20Sopenharmony_ci return ret; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci regval = be16_to_cpu(val); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* 2228c2ecf20Sopenharmony_ci * Overflow is explained on datasheet page 17. 2238c2ecf20Sopenharmony_ci * 2248c2ecf20Sopenharmony_ci * It's a warning that either the G or IR channel has become saturated 2258c2ecf20Sopenharmony_ci * and that the value in the register is likely incorrect. 2268c2ecf20Sopenharmony_ci * 2278c2ecf20Sopenharmony_ci * The recommendation is to change the scale (ALSPGA). 2288c2ecf20Sopenharmony_ci * The driver just returns the max representable value. 2298c2ecf20Sopenharmony_ci */ 2308c2ecf20Sopenharmony_ci if (regval & MAX44000_ALSDATA_OVERFLOW) 2318c2ecf20Sopenharmony_ci return 0x3FFF; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci return regval << MAX44000_ALSTIM_SHIFT(alstim); 2348c2ecf20Sopenharmony_ci} 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_cistatic int max44000_write_led_current_raw(struct max44000_data *data, int val) 2378c2ecf20Sopenharmony_ci{ 2388c2ecf20Sopenharmony_ci /* Maybe we should clamp the value instead? */ 2398c2ecf20Sopenharmony_ci if (val < 0 || val > MAX44000_LED_CURRENT_MAX) 2408c2ecf20Sopenharmony_ci return -ERANGE; 2418c2ecf20Sopenharmony_ci if (val >= 8) 2428c2ecf20Sopenharmony_ci val += 4; 2438c2ecf20Sopenharmony_ci return regmap_write_bits(data->regmap, MAX44000_REG_CFG_TX, 2448c2ecf20Sopenharmony_ci MAX44000_LED_CURRENT_MASK, val); 2458c2ecf20Sopenharmony_ci} 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_cistatic int max44000_read_led_current_raw(struct max44000_data *data) 2488c2ecf20Sopenharmony_ci{ 2498c2ecf20Sopenharmony_ci unsigned int regval; 2508c2ecf20Sopenharmony_ci int ret; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX44000_REG_CFG_TX, ®val); 2538c2ecf20Sopenharmony_ci if (ret < 0) 2548c2ecf20Sopenharmony_ci return ret; 2558c2ecf20Sopenharmony_ci regval &= MAX44000_LED_CURRENT_MASK; 2568c2ecf20Sopenharmony_ci if (regval >= 8) 2578c2ecf20Sopenharmony_ci regval -= 4; 2588c2ecf20Sopenharmony_ci return regval; 2598c2ecf20Sopenharmony_ci} 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistatic int max44000_read_raw(struct iio_dev *indio_dev, 2628c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 2638c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 2648c2ecf20Sopenharmony_ci{ 2658c2ecf20Sopenharmony_ci struct max44000_data *data = iio_priv(indio_dev); 2668c2ecf20Sopenharmony_ci int alstim, alspga; 2678c2ecf20Sopenharmony_ci unsigned int regval; 2688c2ecf20Sopenharmony_ci int ret; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci switch (mask) { 2718c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 2728c2ecf20Sopenharmony_ci switch (chan->type) { 2738c2ecf20Sopenharmony_ci case IIO_LIGHT: 2748c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 2758c2ecf20Sopenharmony_ci ret = max44000_read_alsval(data); 2768c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 2778c2ecf20Sopenharmony_ci if (ret < 0) 2788c2ecf20Sopenharmony_ci return ret; 2798c2ecf20Sopenharmony_ci *val = ret; 2808c2ecf20Sopenharmony_ci return IIO_VAL_INT; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 2838c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 2848c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, ®val); 2858c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 2868c2ecf20Sopenharmony_ci if (ret < 0) 2878c2ecf20Sopenharmony_ci return ret; 2888c2ecf20Sopenharmony_ci *val = regval; 2898c2ecf20Sopenharmony_ci return IIO_VAL_INT; 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci case IIO_CURRENT: 2928c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 2938c2ecf20Sopenharmony_ci ret = max44000_read_led_current_raw(data); 2948c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 2958c2ecf20Sopenharmony_ci if (ret < 0) 2968c2ecf20Sopenharmony_ci return ret; 2978c2ecf20Sopenharmony_ci *val = ret; 2988c2ecf20Sopenharmony_ci return IIO_VAL_INT; 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci default: 3018c2ecf20Sopenharmony_ci return -EINVAL; 3028c2ecf20Sopenharmony_ci } 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 3058c2ecf20Sopenharmony_ci switch (chan->type) { 3068c2ecf20Sopenharmony_ci case IIO_CURRENT: 3078c2ecf20Sopenharmony_ci /* Output register is in 10s of miliamps */ 3088c2ecf20Sopenharmony_ci *val = 10; 3098c2ecf20Sopenharmony_ci return IIO_VAL_INT; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci case IIO_LIGHT: 3128c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3138c2ecf20Sopenharmony_ci alspga = ret = max44000_read_alspga(data); 3148c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 3158c2ecf20Sopenharmony_ci if (ret < 0) 3168c2ecf20Sopenharmony_ci return ret; 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_ci /* Avoid negative shifts */ 3198c2ecf20Sopenharmony_ci *val = (1 << MAX44000_ALSPGA_MAX_SHIFT); 3208c2ecf20Sopenharmony_ci *val2 = MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 3218c2ecf20Sopenharmony_ci + MAX44000_ALSPGA_MAX_SHIFT 3228c2ecf20Sopenharmony_ci - max44000_alspga_shift[alspga]; 3238c2ecf20Sopenharmony_ci return IIO_VAL_FRACTIONAL_LOG2; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci default: 3268c2ecf20Sopenharmony_ci return -EINVAL; 3278c2ecf20Sopenharmony_ci } 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_INT_TIME: 3308c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3318c2ecf20Sopenharmony_ci alstim = ret = max44000_read_alstim(data); 3328c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci if (ret < 0) 3358c2ecf20Sopenharmony_ci return ret; 3368c2ecf20Sopenharmony_ci *val = 0; 3378c2ecf20Sopenharmony_ci *val2 = max44000_int_time_avail_ns_array[alstim]; 3388c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_NANO; 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci default: 3418c2ecf20Sopenharmony_ci return -EINVAL; 3428c2ecf20Sopenharmony_ci } 3438c2ecf20Sopenharmony_ci} 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_cistatic int max44000_write_raw(struct iio_dev *indio_dev, 3468c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 3478c2ecf20Sopenharmony_ci int val, int val2, long mask) 3488c2ecf20Sopenharmony_ci{ 3498c2ecf20Sopenharmony_ci struct max44000_data *data = iio_priv(indio_dev); 3508c2ecf20Sopenharmony_ci int ret; 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci if (mask == IIO_CHAN_INFO_RAW && chan->type == IIO_CURRENT) { 3538c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3548c2ecf20Sopenharmony_ci ret = max44000_write_led_current_raw(data, val); 3558c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 3568c2ecf20Sopenharmony_ci return ret; 3578c2ecf20Sopenharmony_ci } else if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) { 3588c2ecf20Sopenharmony_ci s64 valns = val * NSEC_PER_SEC + val2; 3598c2ecf20Sopenharmony_ci int alstim = find_closest_descending(valns, 3608c2ecf20Sopenharmony_ci max44000_int_time_avail_ns_array, 3618c2ecf20Sopenharmony_ci ARRAY_SIZE(max44000_int_time_avail_ns_array)); 3628c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3638c2ecf20Sopenharmony_ci ret = max44000_write_alstim(data, alstim); 3648c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 3658c2ecf20Sopenharmony_ci return ret; 3668c2ecf20Sopenharmony_ci } else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) { 3678c2ecf20Sopenharmony_ci s64 valus = val * USEC_PER_SEC + val2; 3688c2ecf20Sopenharmony_ci int alspga = find_closest(valus, 3698c2ecf20Sopenharmony_ci max44000_scale_avail_ulux_array, 3708c2ecf20Sopenharmony_ci ARRAY_SIZE(max44000_scale_avail_ulux_array)); 3718c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3728c2ecf20Sopenharmony_ci ret = max44000_write_alspga(data, alspga); 3738c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 3748c2ecf20Sopenharmony_ci return ret; 3758c2ecf20Sopenharmony_ci } 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci return -EINVAL; 3788c2ecf20Sopenharmony_ci} 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_cistatic int max44000_write_raw_get_fmt(struct iio_dev *indio_dev, 3818c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 3828c2ecf20Sopenharmony_ci long mask) 3838c2ecf20Sopenharmony_ci{ 3848c2ecf20Sopenharmony_ci if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) 3858c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_NANO; 3868c2ecf20Sopenharmony_ci else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) 3878c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 3888c2ecf20Sopenharmony_ci else 3898c2ecf20Sopenharmony_ci return IIO_VAL_INT; 3908c2ecf20Sopenharmony_ci} 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(illuminance_integration_time_available, max44000_int_time_avail_str); 3938c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(illuminance_scale_available, max44000_scale_avail_str); 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_cistatic struct attribute *max44000_attributes[] = { 3968c2ecf20Sopenharmony_ci &iio_const_attr_illuminance_integration_time_available.dev_attr.attr, 3978c2ecf20Sopenharmony_ci &iio_const_attr_illuminance_scale_available.dev_attr.attr, 3988c2ecf20Sopenharmony_ci NULL 3998c2ecf20Sopenharmony_ci}; 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_cistatic const struct attribute_group max44000_attribute_group = { 4028c2ecf20Sopenharmony_ci .attrs = max44000_attributes, 4038c2ecf20Sopenharmony_ci}; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_cistatic const struct iio_info max44000_info = { 4068c2ecf20Sopenharmony_ci .read_raw = max44000_read_raw, 4078c2ecf20Sopenharmony_ci .write_raw = max44000_write_raw, 4088c2ecf20Sopenharmony_ci .write_raw_get_fmt = max44000_write_raw_get_fmt, 4098c2ecf20Sopenharmony_ci .attrs = &max44000_attribute_group, 4108c2ecf20Sopenharmony_ci}; 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_cistatic bool max44000_readable_reg(struct device *dev, unsigned int reg) 4138c2ecf20Sopenharmony_ci{ 4148c2ecf20Sopenharmony_ci switch (reg) { 4158c2ecf20Sopenharmony_ci case MAX44000_REG_STATUS: 4168c2ecf20Sopenharmony_ci case MAX44000_REG_CFG_MAIN: 4178c2ecf20Sopenharmony_ci case MAX44000_REG_CFG_RX: 4188c2ecf20Sopenharmony_ci case MAX44000_REG_CFG_TX: 4198c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_DATA_HI: 4208c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_DATA_LO: 4218c2ecf20Sopenharmony_ci case MAX44000_REG_PRX_DATA: 4228c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_UPTHR_HI: 4238c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_UPTHR_LO: 4248c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_LOTHR_HI: 4258c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_LOTHR_LO: 4268c2ecf20Sopenharmony_ci case MAX44000_REG_PST: 4278c2ecf20Sopenharmony_ci case MAX44000_REG_PRX_IND: 4288c2ecf20Sopenharmony_ci case MAX44000_REG_PRX_THR: 4298c2ecf20Sopenharmony_ci case MAX44000_REG_TRIM_GAIN_GREEN: 4308c2ecf20Sopenharmony_ci case MAX44000_REG_TRIM_GAIN_IR: 4318c2ecf20Sopenharmony_ci return true; 4328c2ecf20Sopenharmony_ci default: 4338c2ecf20Sopenharmony_ci return false; 4348c2ecf20Sopenharmony_ci } 4358c2ecf20Sopenharmony_ci} 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_cistatic bool max44000_writeable_reg(struct device *dev, unsigned int reg) 4388c2ecf20Sopenharmony_ci{ 4398c2ecf20Sopenharmony_ci switch (reg) { 4408c2ecf20Sopenharmony_ci case MAX44000_REG_CFG_MAIN: 4418c2ecf20Sopenharmony_ci case MAX44000_REG_CFG_RX: 4428c2ecf20Sopenharmony_ci case MAX44000_REG_CFG_TX: 4438c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_UPTHR_HI: 4448c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_UPTHR_LO: 4458c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_LOTHR_HI: 4468c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_LOTHR_LO: 4478c2ecf20Sopenharmony_ci case MAX44000_REG_PST: 4488c2ecf20Sopenharmony_ci case MAX44000_REG_PRX_IND: 4498c2ecf20Sopenharmony_ci case MAX44000_REG_PRX_THR: 4508c2ecf20Sopenharmony_ci case MAX44000_REG_TRIM_GAIN_GREEN: 4518c2ecf20Sopenharmony_ci case MAX44000_REG_TRIM_GAIN_IR: 4528c2ecf20Sopenharmony_ci return true; 4538c2ecf20Sopenharmony_ci default: 4548c2ecf20Sopenharmony_ci return false; 4558c2ecf20Sopenharmony_ci } 4568c2ecf20Sopenharmony_ci} 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_cistatic bool max44000_volatile_reg(struct device *dev, unsigned int reg) 4598c2ecf20Sopenharmony_ci{ 4608c2ecf20Sopenharmony_ci switch (reg) { 4618c2ecf20Sopenharmony_ci case MAX44000_REG_STATUS: 4628c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_DATA_HI: 4638c2ecf20Sopenharmony_ci case MAX44000_REG_ALS_DATA_LO: 4648c2ecf20Sopenharmony_ci case MAX44000_REG_PRX_DATA: 4658c2ecf20Sopenharmony_ci return true; 4668c2ecf20Sopenharmony_ci default: 4678c2ecf20Sopenharmony_ci return false; 4688c2ecf20Sopenharmony_ci } 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_cistatic bool max44000_precious_reg(struct device *dev, unsigned int reg) 4728c2ecf20Sopenharmony_ci{ 4738c2ecf20Sopenharmony_ci return reg == MAX44000_REG_STATUS; 4748c2ecf20Sopenharmony_ci} 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_cistatic const struct regmap_config max44000_regmap_config = { 4778c2ecf20Sopenharmony_ci .reg_bits = 8, 4788c2ecf20Sopenharmony_ci .val_bits = 8, 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci .max_register = MAX44000_REG_PRX_DATA, 4818c2ecf20Sopenharmony_ci .readable_reg = max44000_readable_reg, 4828c2ecf20Sopenharmony_ci .writeable_reg = max44000_writeable_reg, 4838c2ecf20Sopenharmony_ci .volatile_reg = max44000_volatile_reg, 4848c2ecf20Sopenharmony_ci .precious_reg = max44000_precious_reg, 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci .use_single_read = true, 4878c2ecf20Sopenharmony_ci .use_single_write = true, 4888c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 4898c2ecf20Sopenharmony_ci}; 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_cistatic irqreturn_t max44000_trigger_handler(int irq, void *p) 4928c2ecf20Sopenharmony_ci{ 4938c2ecf20Sopenharmony_ci struct iio_poll_func *pf = p; 4948c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = pf->indio_dev; 4958c2ecf20Sopenharmony_ci struct max44000_data *data = iio_priv(indio_dev); 4968c2ecf20Sopenharmony_ci int index = 0; 4978c2ecf20Sopenharmony_ci unsigned int regval; 4988c2ecf20Sopenharmony_ci int ret; 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 5018c2ecf20Sopenharmony_ci if (test_bit(MAX44000_SCAN_INDEX_ALS, indio_dev->active_scan_mask)) { 5028c2ecf20Sopenharmony_ci ret = max44000_read_alsval(data); 5038c2ecf20Sopenharmony_ci if (ret < 0) 5048c2ecf20Sopenharmony_ci goto out_unlock; 5058c2ecf20Sopenharmony_ci data->scan.channels[index++] = ret; 5068c2ecf20Sopenharmony_ci } 5078c2ecf20Sopenharmony_ci if (test_bit(MAX44000_SCAN_INDEX_PRX, indio_dev->active_scan_mask)) { 5088c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, ®val); 5098c2ecf20Sopenharmony_ci if (ret < 0) 5108c2ecf20Sopenharmony_ci goto out_unlock; 5118c2ecf20Sopenharmony_ci data->scan.channels[index] = regval; 5128c2ecf20Sopenharmony_ci } 5138c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, 5168c2ecf20Sopenharmony_ci iio_get_time_ns(indio_dev)); 5178c2ecf20Sopenharmony_ci iio_trigger_notify_done(indio_dev->trig); 5188c2ecf20Sopenharmony_ci return IRQ_HANDLED; 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ciout_unlock: 5218c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 5228c2ecf20Sopenharmony_ci iio_trigger_notify_done(indio_dev->trig); 5238c2ecf20Sopenharmony_ci return IRQ_HANDLED; 5248c2ecf20Sopenharmony_ci} 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_cistatic int max44000_probe(struct i2c_client *client, 5278c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 5288c2ecf20Sopenharmony_ci{ 5298c2ecf20Sopenharmony_ci struct max44000_data *data; 5308c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 5318c2ecf20Sopenharmony_ci int ret, reg; 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 5348c2ecf20Sopenharmony_ci if (!indio_dev) 5358c2ecf20Sopenharmony_ci return -ENOMEM; 5368c2ecf20Sopenharmony_ci data = iio_priv(indio_dev); 5378c2ecf20Sopenharmony_ci data->regmap = devm_regmap_init_i2c(client, &max44000_regmap_config); 5388c2ecf20Sopenharmony_ci if (IS_ERR(data->regmap)) { 5398c2ecf20Sopenharmony_ci dev_err(&client->dev, "regmap_init failed!\n"); 5408c2ecf20Sopenharmony_ci return PTR_ERR(data->regmap); 5418c2ecf20Sopenharmony_ci } 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 5448c2ecf20Sopenharmony_ci mutex_init(&data->lock); 5458c2ecf20Sopenharmony_ci indio_dev->info = &max44000_info; 5468c2ecf20Sopenharmony_ci indio_dev->name = MAX44000_DRV_NAME; 5478c2ecf20Sopenharmony_ci indio_dev->channels = max44000_channels; 5488c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(max44000_channels); 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci /* 5518c2ecf20Sopenharmony_ci * The device doesn't have a reset function so we just clear some 5528c2ecf20Sopenharmony_ci * important bits at probe time to ensure sane operation. 5538c2ecf20Sopenharmony_ci * 5548c2ecf20Sopenharmony_ci * Since we don't support interrupts/events the threshold values are 5558c2ecf20Sopenharmony_ci * not important. We also don't touch trim values. 5568c2ecf20Sopenharmony_ci */ 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci /* Reset ALS scaling bits */ 5598c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, MAX44000_REG_CFG_RX, 5608c2ecf20Sopenharmony_ci MAX44000_REG_CFG_RX_DEFAULT); 5618c2ecf20Sopenharmony_ci if (ret < 0) { 5628c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to write default CFG_RX: %d\n", 5638c2ecf20Sopenharmony_ci ret); 5648c2ecf20Sopenharmony_ci return ret; 5658c2ecf20Sopenharmony_ci } 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci /* 5688c2ecf20Sopenharmony_ci * By default the LED pulse used for the proximity sensor is disabled. 5698c2ecf20Sopenharmony_ci * Set a middle value so that we get some sort of valid data by default. 5708c2ecf20Sopenharmony_ci */ 5718c2ecf20Sopenharmony_ci ret = max44000_write_led_current_raw(data, MAX44000_LED_CURRENT_DEFAULT); 5728c2ecf20Sopenharmony_ci if (ret < 0) { 5738c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to write init config: %d\n", ret); 5748c2ecf20Sopenharmony_ci return ret; 5758c2ecf20Sopenharmony_ci } 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_ci /* Reset CFG bits to ALS_PRX mode which allows easy reading of both values. */ 5788c2ecf20Sopenharmony_ci reg = MAX44000_CFG_TRIM | MAX44000_CFG_MODE_ALS_PRX; 5798c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, MAX44000_REG_CFG_MAIN, reg); 5808c2ecf20Sopenharmony_ci if (ret < 0) { 5818c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to write init config: %d\n", ret); 5828c2ecf20Sopenharmony_ci return ret; 5838c2ecf20Sopenharmony_ci } 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_ci /* Read status at least once to clear any stale interrupt bits. */ 5868c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, MAX44000_REG_STATUS, ®); 5878c2ecf20Sopenharmony_ci if (ret < 0) { 5888c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed to read init status: %d\n", ret); 5898c2ecf20Sopenharmony_ci return ret; 5908c2ecf20Sopenharmony_ci } 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci ret = iio_triggered_buffer_setup(indio_dev, NULL, max44000_trigger_handler, NULL); 5938c2ecf20Sopenharmony_ci if (ret < 0) { 5948c2ecf20Sopenharmony_ci dev_err(&client->dev, "iio triggered buffer setup failed\n"); 5958c2ecf20Sopenharmony_ci return ret; 5968c2ecf20Sopenharmony_ci } 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci return iio_device_register(indio_dev); 5998c2ecf20Sopenharmony_ci} 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_cistatic int max44000_remove(struct i2c_client *client) 6028c2ecf20Sopenharmony_ci{ 6038c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(client); 6048c2ecf20Sopenharmony_ci 6058c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 6068c2ecf20Sopenharmony_ci iio_triggered_buffer_cleanup(indio_dev); 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci return 0; 6098c2ecf20Sopenharmony_ci} 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_cistatic const struct i2c_device_id max44000_id[] = { 6128c2ecf20Sopenharmony_ci {"max44000", 0}, 6138c2ecf20Sopenharmony_ci { } 6148c2ecf20Sopenharmony_ci}; 6158c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max44000_id); 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci#ifdef CONFIG_ACPI 6188c2ecf20Sopenharmony_cistatic const struct acpi_device_id max44000_acpi_match[] = { 6198c2ecf20Sopenharmony_ci {"MAX44000", 0}, 6208c2ecf20Sopenharmony_ci { } 6218c2ecf20Sopenharmony_ci}; 6228c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, max44000_acpi_match); 6238c2ecf20Sopenharmony_ci#endif 6248c2ecf20Sopenharmony_ci 6258c2ecf20Sopenharmony_cistatic struct i2c_driver max44000_driver = { 6268c2ecf20Sopenharmony_ci .driver = { 6278c2ecf20Sopenharmony_ci .name = MAX44000_DRV_NAME, 6288c2ecf20Sopenharmony_ci .acpi_match_table = ACPI_PTR(max44000_acpi_match), 6298c2ecf20Sopenharmony_ci }, 6308c2ecf20Sopenharmony_ci .probe = max44000_probe, 6318c2ecf20Sopenharmony_ci .remove = max44000_remove, 6328c2ecf20Sopenharmony_ci .id_table = max44000_id, 6338c2ecf20Sopenharmony_ci}; 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_cimodule_i2c_driver(max44000_driver); 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ciMODULE_AUTHOR("Crestez Dan Leonard <leonard.crestez@intel.com>"); 6388c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MAX44000 Ambient and Infrared Proximity Sensor"); 6398c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 640