18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * adux1020.c - Support for Analog Devices ADUX1020 photometric sensor 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2019 Linaro Ltd. 68c2ecf20Sopenharmony_ci * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * TODO: Triggered buffer support 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/bitfield.h> 128c2ecf20Sopenharmony_ci#include <linux/delay.h> 138c2ecf20Sopenharmony_ci#include <linux/err.h> 148c2ecf20Sopenharmony_ci#include <linux/i2c.h> 158c2ecf20Sopenharmony_ci#include <linux/init.h> 168c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 178c2ecf20Sopenharmony_ci#include <linux/irq.h> 188c2ecf20Sopenharmony_ci#include <linux/module.h> 198c2ecf20Sopenharmony_ci#include <linux/mutex.h> 208c2ecf20Sopenharmony_ci#include <linux/regmap.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 238c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 248c2ecf20Sopenharmony_ci#include <linux/iio/events.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define ADUX1020_REGMAP_NAME "adux1020_regmap" 278c2ecf20Sopenharmony_ci#define ADUX1020_DRV_NAME "adux1020" 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci/* System registers */ 308c2ecf20Sopenharmony_ci#define ADUX1020_REG_CHIP_ID 0x08 318c2ecf20Sopenharmony_ci#define ADUX1020_REG_SLAVE_ADDRESS 0x09 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define ADUX1020_REG_SW_RESET 0x0f 348c2ecf20Sopenharmony_ci#define ADUX1020_REG_INT_ENABLE 0x1c 358c2ecf20Sopenharmony_ci#define ADUX1020_REG_INT_POLARITY 0x1d 368c2ecf20Sopenharmony_ci#define ADUX1020_REG_PROX_TH_ON1 0x2a 378c2ecf20Sopenharmony_ci#define ADUX1020_REG_PROX_TH_OFF1 0x2b 388c2ecf20Sopenharmony_ci#define ADUX1020_REG_PROX_TYPE 0x2f 398c2ecf20Sopenharmony_ci#define ADUX1020_REG_TEST_MODES_3 0x32 408c2ecf20Sopenharmony_ci#define ADUX1020_REG_FORCE_MODE 0x33 418c2ecf20Sopenharmony_ci#define ADUX1020_REG_FREQUENCY 0x40 428c2ecf20Sopenharmony_ci#define ADUX1020_REG_LED_CURRENT 0x41 438c2ecf20Sopenharmony_ci#define ADUX1020_REG_OP_MODE 0x45 448c2ecf20Sopenharmony_ci#define ADUX1020_REG_INT_MASK 0x48 458c2ecf20Sopenharmony_ci#define ADUX1020_REG_INT_STATUS 0x49 468c2ecf20Sopenharmony_ci#define ADUX1020_REG_DATA_BUFFER 0x60 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci/* Chip ID bits */ 498c2ecf20Sopenharmony_ci#define ADUX1020_CHIP_ID_MASK GENMASK(11, 0) 508c2ecf20Sopenharmony_ci#define ADUX1020_CHIP_ID 0x03fc 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define ADUX1020_SW_RESET BIT(1) 538c2ecf20Sopenharmony_ci#define ADUX1020_FIFO_FLUSH BIT(15) 548c2ecf20Sopenharmony_ci#define ADUX1020_OP_MODE_MASK GENMASK(3, 0) 558c2ecf20Sopenharmony_ci#define ADUX1020_DATA_OUT_MODE_MASK GENMASK(7, 4) 568c2ecf20Sopenharmony_ci#define ADUX1020_DATA_OUT_PROX_I FIELD_PREP(ADUX1020_DATA_OUT_MODE_MASK, 1) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define ADUX1020_MODE_INT_MASK GENMASK(7, 0) 598c2ecf20Sopenharmony_ci#define ADUX1020_INT_ENABLE 0x2094 608c2ecf20Sopenharmony_ci#define ADUX1020_INT_DISABLE 0x2090 618c2ecf20Sopenharmony_ci#define ADUX1020_PROX_INT_ENABLE 0x00f0 628c2ecf20Sopenharmony_ci#define ADUX1020_PROX_ON1_INT BIT(0) 638c2ecf20Sopenharmony_ci#define ADUX1020_PROX_OFF1_INT BIT(1) 648c2ecf20Sopenharmony_ci#define ADUX1020_FIFO_INT_ENABLE 0x7f 658c2ecf20Sopenharmony_ci#define ADUX1020_MODE_INT_DISABLE 0xff 668c2ecf20Sopenharmony_ci#define ADUX1020_MODE_INT_STATUS_MASK GENMASK(7, 0) 678c2ecf20Sopenharmony_ci#define ADUX1020_FIFO_STATUS_MASK GENMASK(15, 8) 688c2ecf20Sopenharmony_ci#define ADUX1020_INT_CLEAR 0xff 698c2ecf20Sopenharmony_ci#define ADUX1020_PROX_TYPE BIT(15) 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci#define ADUX1020_INT_PROX_ON1 BIT(0) 728c2ecf20Sopenharmony_ci#define ADUX1020_INT_PROX_OFF1 BIT(1) 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci#define ADUX1020_FORCE_CLOCK_ON 0x0f4f 758c2ecf20Sopenharmony_ci#define ADUX1020_FORCE_CLOCK_RESET 0x0040 768c2ecf20Sopenharmony_ci#define ADUX1020_ACTIVE_4_STATE 0x0008 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci#define ADUX1020_PROX_FREQ_MASK GENMASK(7, 4) 798c2ecf20Sopenharmony_ci#define ADUX1020_PROX_FREQ(x) FIELD_PREP(ADUX1020_PROX_FREQ_MASK, x) 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci#define ADUX1020_LED_CURRENT_MASK GENMASK(3, 0) 828c2ecf20Sopenharmony_ci#define ADUX1020_LED_PIREF_EN BIT(12) 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci/* Operating modes */ 858c2ecf20Sopenharmony_cienum adux1020_op_modes { 868c2ecf20Sopenharmony_ci ADUX1020_MODE_STANDBY, 878c2ecf20Sopenharmony_ci ADUX1020_MODE_PROX_I, 888c2ecf20Sopenharmony_ci ADUX1020_MODE_PROX_XY, 898c2ecf20Sopenharmony_ci ADUX1020_MODE_GEST, 908c2ecf20Sopenharmony_ci ADUX1020_MODE_SAMPLE, 918c2ecf20Sopenharmony_ci ADUX1020_MODE_FORCE = 0x0e, 928c2ecf20Sopenharmony_ci ADUX1020_MODE_IDLE = 0x0f, 938c2ecf20Sopenharmony_ci}; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistruct adux1020_data { 968c2ecf20Sopenharmony_ci struct i2c_client *client; 978c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 988c2ecf20Sopenharmony_ci struct mutex lock; 998c2ecf20Sopenharmony_ci struct regmap *regmap; 1008c2ecf20Sopenharmony_ci}; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistruct adux1020_mode_data { 1038c2ecf20Sopenharmony_ci u8 bytes; 1048c2ecf20Sopenharmony_ci u8 buf_len; 1058c2ecf20Sopenharmony_ci u16 int_en; 1068c2ecf20Sopenharmony_ci}; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_cistatic const struct adux1020_mode_data adux1020_modes[] = { 1098c2ecf20Sopenharmony_ci [ADUX1020_MODE_PROX_I] = { 1108c2ecf20Sopenharmony_ci .bytes = 2, 1118c2ecf20Sopenharmony_ci .buf_len = 1, 1128c2ecf20Sopenharmony_ci .int_en = ADUX1020_PROX_INT_ENABLE, 1138c2ecf20Sopenharmony_ci }, 1148c2ecf20Sopenharmony_ci}; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic const struct regmap_config adux1020_regmap_config = { 1178c2ecf20Sopenharmony_ci .name = ADUX1020_REGMAP_NAME, 1188c2ecf20Sopenharmony_ci .reg_bits = 8, 1198c2ecf20Sopenharmony_ci .val_bits = 16, 1208c2ecf20Sopenharmony_ci .max_register = 0x6F, 1218c2ecf20Sopenharmony_ci .cache_type = REGCACHE_NONE, 1228c2ecf20Sopenharmony_ci}; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic const struct reg_sequence adux1020_def_conf[] = { 1258c2ecf20Sopenharmony_ci { 0x000c, 0x000f }, 1268c2ecf20Sopenharmony_ci { 0x0010, 0x1010 }, 1278c2ecf20Sopenharmony_ci { 0x0011, 0x004c }, 1288c2ecf20Sopenharmony_ci { 0x0012, 0x5f0c }, 1298c2ecf20Sopenharmony_ci { 0x0013, 0xada5 }, 1308c2ecf20Sopenharmony_ci { 0x0014, 0x0080 }, 1318c2ecf20Sopenharmony_ci { 0x0015, 0x0000 }, 1328c2ecf20Sopenharmony_ci { 0x0016, 0x0600 }, 1338c2ecf20Sopenharmony_ci { 0x0017, 0x0000 }, 1348c2ecf20Sopenharmony_ci { 0x0018, 0x2693 }, 1358c2ecf20Sopenharmony_ci { 0x0019, 0x0004 }, 1368c2ecf20Sopenharmony_ci { 0x001a, 0x4280 }, 1378c2ecf20Sopenharmony_ci { 0x001b, 0x0060 }, 1388c2ecf20Sopenharmony_ci { 0x001c, 0x2094 }, 1398c2ecf20Sopenharmony_ci { 0x001d, 0x0020 }, 1408c2ecf20Sopenharmony_ci { 0x001e, 0x0001 }, 1418c2ecf20Sopenharmony_ci { 0x001f, 0x0100 }, 1428c2ecf20Sopenharmony_ci { 0x0020, 0x0320 }, 1438c2ecf20Sopenharmony_ci { 0x0021, 0x0A13 }, 1448c2ecf20Sopenharmony_ci { 0x0022, 0x0320 }, 1458c2ecf20Sopenharmony_ci { 0x0023, 0x0113 }, 1468c2ecf20Sopenharmony_ci { 0x0024, 0x0000 }, 1478c2ecf20Sopenharmony_ci { 0x0025, 0x2412 }, 1488c2ecf20Sopenharmony_ci { 0x0026, 0x2412 }, 1498c2ecf20Sopenharmony_ci { 0x0027, 0x0022 }, 1508c2ecf20Sopenharmony_ci { 0x0028, 0x0000 }, 1518c2ecf20Sopenharmony_ci { 0x0029, 0x0300 }, 1528c2ecf20Sopenharmony_ci { 0x002a, 0x0700 }, 1538c2ecf20Sopenharmony_ci { 0x002b, 0x0600 }, 1548c2ecf20Sopenharmony_ci { 0x002c, 0x6000 }, 1558c2ecf20Sopenharmony_ci { 0x002d, 0x4000 }, 1568c2ecf20Sopenharmony_ci { 0x002e, 0x0000 }, 1578c2ecf20Sopenharmony_ci { 0x002f, 0x0000 }, 1588c2ecf20Sopenharmony_ci { 0x0030, 0x0000 }, 1598c2ecf20Sopenharmony_ci { 0x0031, 0x0000 }, 1608c2ecf20Sopenharmony_ci { 0x0032, 0x0040 }, 1618c2ecf20Sopenharmony_ci { 0x0033, 0x0008 }, 1628c2ecf20Sopenharmony_ci { 0x0034, 0xE400 }, 1638c2ecf20Sopenharmony_ci { 0x0038, 0x8080 }, 1648c2ecf20Sopenharmony_ci { 0x0039, 0x8080 }, 1658c2ecf20Sopenharmony_ci { 0x003a, 0x2000 }, 1668c2ecf20Sopenharmony_ci { 0x003b, 0x1f00 }, 1678c2ecf20Sopenharmony_ci { 0x003c, 0x2000 }, 1688c2ecf20Sopenharmony_ci { 0x003d, 0x2000 }, 1698c2ecf20Sopenharmony_ci { 0x003e, 0x0000 }, 1708c2ecf20Sopenharmony_ci { 0x0040, 0x8069 }, 1718c2ecf20Sopenharmony_ci { 0x0041, 0x1f2f }, 1728c2ecf20Sopenharmony_ci { 0x0042, 0x4000 }, 1738c2ecf20Sopenharmony_ci { 0x0043, 0x0000 }, 1748c2ecf20Sopenharmony_ci { 0x0044, 0x0008 }, 1758c2ecf20Sopenharmony_ci { 0x0046, 0x0000 }, 1768c2ecf20Sopenharmony_ci { 0x0048, 0x00ef }, 1778c2ecf20Sopenharmony_ci { 0x0049, 0x0000 }, 1788c2ecf20Sopenharmony_ci { 0x0045, 0x0000 }, 1798c2ecf20Sopenharmony_ci}; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_cistatic const int adux1020_rates[][2] = { 1828c2ecf20Sopenharmony_ci { 0, 100000 }, 1838c2ecf20Sopenharmony_ci { 0, 200000 }, 1848c2ecf20Sopenharmony_ci { 0, 500000 }, 1858c2ecf20Sopenharmony_ci { 1, 0 }, 1868c2ecf20Sopenharmony_ci { 2, 0 }, 1878c2ecf20Sopenharmony_ci { 5, 0 }, 1888c2ecf20Sopenharmony_ci { 10, 0 }, 1898c2ecf20Sopenharmony_ci { 20, 0 }, 1908c2ecf20Sopenharmony_ci { 50, 0 }, 1918c2ecf20Sopenharmony_ci { 100, 0 }, 1928c2ecf20Sopenharmony_ci { 190, 0 }, 1938c2ecf20Sopenharmony_ci { 450, 0 }, 1948c2ecf20Sopenharmony_ci { 820, 0 }, 1958c2ecf20Sopenharmony_ci { 1400, 0 }, 1968c2ecf20Sopenharmony_ci}; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic const int adux1020_led_currents[][2] = { 1998c2ecf20Sopenharmony_ci { 0, 25000 }, 2008c2ecf20Sopenharmony_ci { 0, 40000 }, 2018c2ecf20Sopenharmony_ci { 0, 55000 }, 2028c2ecf20Sopenharmony_ci { 0, 70000 }, 2038c2ecf20Sopenharmony_ci { 0, 85000 }, 2048c2ecf20Sopenharmony_ci { 0, 100000 }, 2058c2ecf20Sopenharmony_ci { 0, 115000 }, 2068c2ecf20Sopenharmony_ci { 0, 130000 }, 2078c2ecf20Sopenharmony_ci { 0, 145000 }, 2088c2ecf20Sopenharmony_ci { 0, 160000 }, 2098c2ecf20Sopenharmony_ci { 0, 175000 }, 2108c2ecf20Sopenharmony_ci { 0, 190000 }, 2118c2ecf20Sopenharmony_ci { 0, 205000 }, 2128c2ecf20Sopenharmony_ci { 0, 220000 }, 2138c2ecf20Sopenharmony_ci { 0, 235000 }, 2148c2ecf20Sopenharmony_ci { 0, 250000 }, 2158c2ecf20Sopenharmony_ci}; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_cistatic int adux1020_flush_fifo(struct adux1020_data *data) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci int ret; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* Force Idle mode */ 2228c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, ADUX1020_REG_FORCE_MODE, 2238c2ecf20Sopenharmony_ci ADUX1020_ACTIVE_4_STATE); 2248c2ecf20Sopenharmony_ci if (ret < 0) 2258c2ecf20Sopenharmony_ci return ret; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, ADUX1020_REG_OP_MODE, 2288c2ecf20Sopenharmony_ci ADUX1020_OP_MODE_MASK, ADUX1020_MODE_FORCE); 2298c2ecf20Sopenharmony_ci if (ret < 0) 2308c2ecf20Sopenharmony_ci return ret; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, ADUX1020_REG_OP_MODE, 2338c2ecf20Sopenharmony_ci ADUX1020_OP_MODE_MASK, ADUX1020_MODE_IDLE); 2348c2ecf20Sopenharmony_ci if (ret < 0) 2358c2ecf20Sopenharmony_ci return ret; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci /* Flush FIFO */ 2388c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, ADUX1020_REG_TEST_MODES_3, 2398c2ecf20Sopenharmony_ci ADUX1020_FORCE_CLOCK_ON); 2408c2ecf20Sopenharmony_ci if (ret < 0) 2418c2ecf20Sopenharmony_ci return ret; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, ADUX1020_REG_INT_STATUS, 2448c2ecf20Sopenharmony_ci ADUX1020_FIFO_FLUSH); 2458c2ecf20Sopenharmony_ci if (ret < 0) 2468c2ecf20Sopenharmony_ci return ret; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci return regmap_write(data->regmap, ADUX1020_REG_TEST_MODES_3, 2498c2ecf20Sopenharmony_ci ADUX1020_FORCE_CLOCK_RESET); 2508c2ecf20Sopenharmony_ci} 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_cistatic int adux1020_read_fifo(struct adux1020_data *data, u16 *buf, u8 buf_len) 2538c2ecf20Sopenharmony_ci{ 2548c2ecf20Sopenharmony_ci unsigned int regval; 2558c2ecf20Sopenharmony_ci int i, ret; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci /* Enable 32MHz clock */ 2588c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, ADUX1020_REG_TEST_MODES_3, 2598c2ecf20Sopenharmony_ci ADUX1020_FORCE_CLOCK_ON); 2608c2ecf20Sopenharmony_ci if (ret < 0) 2618c2ecf20Sopenharmony_ci return ret; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci for (i = 0; i < buf_len; i++) { 2648c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, ADUX1020_REG_DATA_BUFFER, 2658c2ecf20Sopenharmony_ci ®val); 2668c2ecf20Sopenharmony_ci if (ret < 0) 2678c2ecf20Sopenharmony_ci return ret; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci buf[i] = regval; 2708c2ecf20Sopenharmony_ci } 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci /* Set 32MHz clock to be controlled by internal state machine */ 2738c2ecf20Sopenharmony_ci return regmap_write(data->regmap, ADUX1020_REG_TEST_MODES_3, 2748c2ecf20Sopenharmony_ci ADUX1020_FORCE_CLOCK_RESET); 2758c2ecf20Sopenharmony_ci} 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_cistatic int adux1020_set_mode(struct adux1020_data *data, 2788c2ecf20Sopenharmony_ci enum adux1020_op_modes mode) 2798c2ecf20Sopenharmony_ci{ 2808c2ecf20Sopenharmony_ci int ret; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci /* Switch to standby mode before changing the mode */ 2838c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, ADUX1020_REG_OP_MODE, 2848c2ecf20Sopenharmony_ci ADUX1020_MODE_STANDBY); 2858c2ecf20Sopenharmony_ci if (ret < 0) 2868c2ecf20Sopenharmony_ci return ret; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci /* Set data out and switch to the desired mode */ 2898c2ecf20Sopenharmony_ci switch (mode) { 2908c2ecf20Sopenharmony_ci case ADUX1020_MODE_PROX_I: 2918c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, ADUX1020_REG_OP_MODE, 2928c2ecf20Sopenharmony_ci ADUX1020_DATA_OUT_MODE_MASK, 2938c2ecf20Sopenharmony_ci ADUX1020_DATA_OUT_PROX_I); 2948c2ecf20Sopenharmony_ci if (ret < 0) 2958c2ecf20Sopenharmony_ci return ret; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, ADUX1020_REG_OP_MODE, 2988c2ecf20Sopenharmony_ci ADUX1020_OP_MODE_MASK, 2998c2ecf20Sopenharmony_ci ADUX1020_MODE_PROX_I); 3008c2ecf20Sopenharmony_ci if (ret < 0) 3018c2ecf20Sopenharmony_ci return ret; 3028c2ecf20Sopenharmony_ci break; 3038c2ecf20Sopenharmony_ci default: 3048c2ecf20Sopenharmony_ci return -EINVAL; 3058c2ecf20Sopenharmony_ci } 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci return 0; 3088c2ecf20Sopenharmony_ci} 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_cistatic int adux1020_measure(struct adux1020_data *data, 3118c2ecf20Sopenharmony_ci enum adux1020_op_modes mode, 3128c2ecf20Sopenharmony_ci u16 *val) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci unsigned int status; 3158c2ecf20Sopenharmony_ci int ret, tries = 50; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci /* Disable INT pin as polling is going to be used */ 3188c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, ADUX1020_REG_INT_ENABLE, 3198c2ecf20Sopenharmony_ci ADUX1020_INT_DISABLE); 3208c2ecf20Sopenharmony_ci if (ret < 0) 3218c2ecf20Sopenharmony_ci return ret; 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci /* Enable mode interrupt */ 3248c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, ADUX1020_REG_INT_MASK, 3258c2ecf20Sopenharmony_ci ADUX1020_MODE_INT_MASK, 3268c2ecf20Sopenharmony_ci adux1020_modes[mode].int_en); 3278c2ecf20Sopenharmony_ci if (ret < 0) 3288c2ecf20Sopenharmony_ci return ret; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci while (tries--) { 3318c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, ADUX1020_REG_INT_STATUS, 3328c2ecf20Sopenharmony_ci &status); 3338c2ecf20Sopenharmony_ci if (ret < 0) 3348c2ecf20Sopenharmony_ci return ret; 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci status &= ADUX1020_FIFO_STATUS_MASK; 3378c2ecf20Sopenharmony_ci if (status >= adux1020_modes[mode].bytes) 3388c2ecf20Sopenharmony_ci break; 3398c2ecf20Sopenharmony_ci msleep(20); 3408c2ecf20Sopenharmony_ci } 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci if (tries < 0) 3438c2ecf20Sopenharmony_ci return -EIO; 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci ret = adux1020_read_fifo(data, val, adux1020_modes[mode].buf_len); 3468c2ecf20Sopenharmony_ci if (ret < 0) 3478c2ecf20Sopenharmony_ci return ret; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci /* Clear mode interrupt */ 3508c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, ADUX1020_REG_INT_STATUS, 3518c2ecf20Sopenharmony_ci (~adux1020_modes[mode].int_en)); 3528c2ecf20Sopenharmony_ci if (ret < 0) 3538c2ecf20Sopenharmony_ci return ret; 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci /* Disable mode interrupts */ 3568c2ecf20Sopenharmony_ci return regmap_update_bits(data->regmap, ADUX1020_REG_INT_MASK, 3578c2ecf20Sopenharmony_ci ADUX1020_MODE_INT_MASK, 3588c2ecf20Sopenharmony_ci ADUX1020_MODE_INT_DISABLE); 3598c2ecf20Sopenharmony_ci} 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_cistatic int adux1020_read_raw(struct iio_dev *indio_dev, 3628c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 3638c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 3648c2ecf20Sopenharmony_ci{ 3658c2ecf20Sopenharmony_ci struct adux1020_data *data = iio_priv(indio_dev); 3668c2ecf20Sopenharmony_ci u16 buf[3]; 3678c2ecf20Sopenharmony_ci int ret = -EINVAL; 3688c2ecf20Sopenharmony_ci unsigned int regval; 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3718c2ecf20Sopenharmony_ci 3728c2ecf20Sopenharmony_ci switch (mask) { 3738c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 3748c2ecf20Sopenharmony_ci switch (chan->type) { 3758c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 3768c2ecf20Sopenharmony_ci ret = adux1020_set_mode(data, ADUX1020_MODE_PROX_I); 3778c2ecf20Sopenharmony_ci if (ret < 0) 3788c2ecf20Sopenharmony_ci goto fail; 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci ret = adux1020_measure(data, ADUX1020_MODE_PROX_I, buf); 3818c2ecf20Sopenharmony_ci if (ret < 0) 3828c2ecf20Sopenharmony_ci goto fail; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci *val = buf[0]; 3858c2ecf20Sopenharmony_ci ret = IIO_VAL_INT; 3868c2ecf20Sopenharmony_ci break; 3878c2ecf20Sopenharmony_ci default: 3888c2ecf20Sopenharmony_ci break; 3898c2ecf20Sopenharmony_ci } 3908c2ecf20Sopenharmony_ci break; 3918c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_PROCESSED: 3928c2ecf20Sopenharmony_ci switch (chan->type) { 3938c2ecf20Sopenharmony_ci case IIO_CURRENT: 3948c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, 3958c2ecf20Sopenharmony_ci ADUX1020_REG_LED_CURRENT, ®val); 3968c2ecf20Sopenharmony_ci if (ret < 0) 3978c2ecf20Sopenharmony_ci goto fail; 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci regval = regval & ADUX1020_LED_CURRENT_MASK; 4008c2ecf20Sopenharmony_ci 4018c2ecf20Sopenharmony_ci *val = adux1020_led_currents[regval][0]; 4028c2ecf20Sopenharmony_ci *val2 = adux1020_led_currents[regval][1]; 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci ret = IIO_VAL_INT_PLUS_MICRO; 4058c2ecf20Sopenharmony_ci break; 4068c2ecf20Sopenharmony_ci default: 4078c2ecf20Sopenharmony_ci break; 4088c2ecf20Sopenharmony_ci } 4098c2ecf20Sopenharmony_ci break; 4108c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 4118c2ecf20Sopenharmony_ci switch (chan->type) { 4128c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 4138c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, ADUX1020_REG_FREQUENCY, 4148c2ecf20Sopenharmony_ci ®val); 4158c2ecf20Sopenharmony_ci if (ret < 0) 4168c2ecf20Sopenharmony_ci goto fail; 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci regval = FIELD_GET(ADUX1020_PROX_FREQ_MASK, regval); 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci *val = adux1020_rates[regval][0]; 4218c2ecf20Sopenharmony_ci *val2 = adux1020_rates[regval][1]; 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci ret = IIO_VAL_INT_PLUS_MICRO; 4248c2ecf20Sopenharmony_ci break; 4258c2ecf20Sopenharmony_ci default: 4268c2ecf20Sopenharmony_ci break; 4278c2ecf20Sopenharmony_ci } 4288c2ecf20Sopenharmony_ci break; 4298c2ecf20Sopenharmony_ci default: 4308c2ecf20Sopenharmony_ci break; 4318c2ecf20Sopenharmony_ci } 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_cifail: 4348c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci return ret; 4378c2ecf20Sopenharmony_ci}; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_cistatic inline int adux1020_find_index(const int array[][2], int count, int val, 4408c2ecf20Sopenharmony_ci int val2) 4418c2ecf20Sopenharmony_ci{ 4428c2ecf20Sopenharmony_ci int i; 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci for (i = 0; i < count; i++) 4458c2ecf20Sopenharmony_ci if (val == array[i][0] && val2 == array[i][1]) 4468c2ecf20Sopenharmony_ci return i; 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci return -EINVAL; 4498c2ecf20Sopenharmony_ci} 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_cistatic int adux1020_write_raw(struct iio_dev *indio_dev, 4528c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 4538c2ecf20Sopenharmony_ci int val, int val2, long mask) 4548c2ecf20Sopenharmony_ci{ 4558c2ecf20Sopenharmony_ci struct adux1020_data *data = iio_priv(indio_dev); 4568c2ecf20Sopenharmony_ci int i, ret = -EINVAL; 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci switch (mask) { 4618c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 4628c2ecf20Sopenharmony_ci if (chan->type == IIO_PROXIMITY) { 4638c2ecf20Sopenharmony_ci i = adux1020_find_index(adux1020_rates, 4648c2ecf20Sopenharmony_ci ARRAY_SIZE(adux1020_rates), 4658c2ecf20Sopenharmony_ci val, val2); 4668c2ecf20Sopenharmony_ci if (i < 0) { 4678c2ecf20Sopenharmony_ci ret = i; 4688c2ecf20Sopenharmony_ci goto fail; 4698c2ecf20Sopenharmony_ci } 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, 4728c2ecf20Sopenharmony_ci ADUX1020_REG_FREQUENCY, 4738c2ecf20Sopenharmony_ci ADUX1020_PROX_FREQ_MASK, 4748c2ecf20Sopenharmony_ci ADUX1020_PROX_FREQ(i)); 4758c2ecf20Sopenharmony_ci } 4768c2ecf20Sopenharmony_ci break; 4778c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_PROCESSED: 4788c2ecf20Sopenharmony_ci if (chan->type == IIO_CURRENT) { 4798c2ecf20Sopenharmony_ci i = adux1020_find_index(adux1020_led_currents, 4808c2ecf20Sopenharmony_ci ARRAY_SIZE(adux1020_led_currents), 4818c2ecf20Sopenharmony_ci val, val2); 4828c2ecf20Sopenharmony_ci if (i < 0) { 4838c2ecf20Sopenharmony_ci ret = i; 4848c2ecf20Sopenharmony_ci goto fail; 4858c2ecf20Sopenharmony_ci } 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, 4888c2ecf20Sopenharmony_ci ADUX1020_REG_LED_CURRENT, 4898c2ecf20Sopenharmony_ci ADUX1020_LED_CURRENT_MASK, i); 4908c2ecf20Sopenharmony_ci } 4918c2ecf20Sopenharmony_ci break; 4928c2ecf20Sopenharmony_ci default: 4938c2ecf20Sopenharmony_ci break; 4948c2ecf20Sopenharmony_ci } 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_cifail: 4978c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci return ret; 5008c2ecf20Sopenharmony_ci} 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_cistatic int adux1020_write_event_config(struct iio_dev *indio_dev, 5038c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 5048c2ecf20Sopenharmony_ci enum iio_event_type type, 5058c2ecf20Sopenharmony_ci enum iio_event_direction dir, int state) 5068c2ecf20Sopenharmony_ci{ 5078c2ecf20Sopenharmony_ci struct adux1020_data *data = iio_priv(indio_dev); 5088c2ecf20Sopenharmony_ci int ret, mask; 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, ADUX1020_REG_INT_ENABLE, 5138c2ecf20Sopenharmony_ci ADUX1020_INT_ENABLE); 5148c2ecf20Sopenharmony_ci if (ret < 0) 5158c2ecf20Sopenharmony_ci goto fail; 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, ADUX1020_REG_INT_POLARITY, 0); 5188c2ecf20Sopenharmony_ci if (ret < 0) 5198c2ecf20Sopenharmony_ci goto fail; 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci switch (chan->type) { 5228c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 5238c2ecf20Sopenharmony_ci if (dir == IIO_EV_DIR_RISING) 5248c2ecf20Sopenharmony_ci mask = ADUX1020_PROX_ON1_INT; 5258c2ecf20Sopenharmony_ci else 5268c2ecf20Sopenharmony_ci mask = ADUX1020_PROX_OFF1_INT; 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci if (state) 5298c2ecf20Sopenharmony_ci state = 0; 5308c2ecf20Sopenharmony_ci else 5318c2ecf20Sopenharmony_ci state = mask; 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, ADUX1020_REG_INT_MASK, 5348c2ecf20Sopenharmony_ci mask, state); 5358c2ecf20Sopenharmony_ci if (ret < 0) 5368c2ecf20Sopenharmony_ci goto fail; 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci /* 5398c2ecf20Sopenharmony_ci * Trigger proximity interrupt when the intensity is above 5408c2ecf20Sopenharmony_ci * or below threshold 5418c2ecf20Sopenharmony_ci */ 5428c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, ADUX1020_REG_PROX_TYPE, 5438c2ecf20Sopenharmony_ci ADUX1020_PROX_TYPE, 5448c2ecf20Sopenharmony_ci ADUX1020_PROX_TYPE); 5458c2ecf20Sopenharmony_ci if (ret < 0) 5468c2ecf20Sopenharmony_ci goto fail; 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci /* Set proximity mode */ 5498c2ecf20Sopenharmony_ci ret = adux1020_set_mode(data, ADUX1020_MODE_PROX_I); 5508c2ecf20Sopenharmony_ci break; 5518c2ecf20Sopenharmony_ci default: 5528c2ecf20Sopenharmony_ci ret = -EINVAL; 5538c2ecf20Sopenharmony_ci break; 5548c2ecf20Sopenharmony_ci } 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_cifail: 5578c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci return ret; 5608c2ecf20Sopenharmony_ci} 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_cistatic int adux1020_read_event_config(struct iio_dev *indio_dev, 5638c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 5648c2ecf20Sopenharmony_ci enum iio_event_type type, 5658c2ecf20Sopenharmony_ci enum iio_event_direction dir) 5668c2ecf20Sopenharmony_ci{ 5678c2ecf20Sopenharmony_ci struct adux1020_data *data = iio_priv(indio_dev); 5688c2ecf20Sopenharmony_ci int ret, mask; 5698c2ecf20Sopenharmony_ci unsigned int regval; 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci switch (chan->type) { 5728c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 5738c2ecf20Sopenharmony_ci if (dir == IIO_EV_DIR_RISING) 5748c2ecf20Sopenharmony_ci mask = ADUX1020_PROX_ON1_INT; 5758c2ecf20Sopenharmony_ci else 5768c2ecf20Sopenharmony_ci mask = ADUX1020_PROX_OFF1_INT; 5778c2ecf20Sopenharmony_ci break; 5788c2ecf20Sopenharmony_ci default: 5798c2ecf20Sopenharmony_ci return -EINVAL; 5808c2ecf20Sopenharmony_ci } 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, ADUX1020_REG_INT_MASK, ®val); 5838c2ecf20Sopenharmony_ci if (ret < 0) 5848c2ecf20Sopenharmony_ci return ret; 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci return !(regval & mask); 5878c2ecf20Sopenharmony_ci} 5888c2ecf20Sopenharmony_ci 5898c2ecf20Sopenharmony_cistatic int adux1020_read_thresh(struct iio_dev *indio_dev, 5908c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 5918c2ecf20Sopenharmony_ci enum iio_event_type type, 5928c2ecf20Sopenharmony_ci enum iio_event_direction dir, 5938c2ecf20Sopenharmony_ci enum iio_event_info info, int *val, int *val2) 5948c2ecf20Sopenharmony_ci{ 5958c2ecf20Sopenharmony_ci struct adux1020_data *data = iio_priv(indio_dev); 5968c2ecf20Sopenharmony_ci u8 reg; 5978c2ecf20Sopenharmony_ci int ret; 5988c2ecf20Sopenharmony_ci unsigned int regval; 5998c2ecf20Sopenharmony_ci 6008c2ecf20Sopenharmony_ci switch (chan->type) { 6018c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 6028c2ecf20Sopenharmony_ci if (dir == IIO_EV_DIR_RISING) 6038c2ecf20Sopenharmony_ci reg = ADUX1020_REG_PROX_TH_ON1; 6048c2ecf20Sopenharmony_ci else 6058c2ecf20Sopenharmony_ci reg = ADUX1020_REG_PROX_TH_OFF1; 6068c2ecf20Sopenharmony_ci break; 6078c2ecf20Sopenharmony_ci default: 6088c2ecf20Sopenharmony_ci return -EINVAL; 6098c2ecf20Sopenharmony_ci } 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, reg, ®val); 6128c2ecf20Sopenharmony_ci if (ret < 0) 6138c2ecf20Sopenharmony_ci return ret; 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci *val = regval; 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci return IIO_VAL_INT; 6188c2ecf20Sopenharmony_ci} 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_cistatic int adux1020_write_thresh(struct iio_dev *indio_dev, 6218c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 6228c2ecf20Sopenharmony_ci enum iio_event_type type, 6238c2ecf20Sopenharmony_ci enum iio_event_direction dir, 6248c2ecf20Sopenharmony_ci enum iio_event_info info, int val, int val2) 6258c2ecf20Sopenharmony_ci{ 6268c2ecf20Sopenharmony_ci struct adux1020_data *data = iio_priv(indio_dev); 6278c2ecf20Sopenharmony_ci u8 reg; 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci switch (chan->type) { 6308c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 6318c2ecf20Sopenharmony_ci if (dir == IIO_EV_DIR_RISING) 6328c2ecf20Sopenharmony_ci reg = ADUX1020_REG_PROX_TH_ON1; 6338c2ecf20Sopenharmony_ci else 6348c2ecf20Sopenharmony_ci reg = ADUX1020_REG_PROX_TH_OFF1; 6358c2ecf20Sopenharmony_ci break; 6368c2ecf20Sopenharmony_ci default: 6378c2ecf20Sopenharmony_ci return -EINVAL; 6388c2ecf20Sopenharmony_ci } 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci /* Full scale threshold value is 0-65535 */ 6418c2ecf20Sopenharmony_ci if (val < 0 || val > 65535) 6428c2ecf20Sopenharmony_ci return -EINVAL; 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci return regmap_write(data->regmap, reg, val); 6458c2ecf20Sopenharmony_ci} 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_cistatic const struct iio_event_spec adux1020_proximity_event[] = { 6488c2ecf20Sopenharmony_ci { 6498c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 6508c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_RISING, 6518c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_VALUE) | 6528c2ecf20Sopenharmony_ci BIT(IIO_EV_INFO_ENABLE), 6538c2ecf20Sopenharmony_ci }, 6548c2ecf20Sopenharmony_ci { 6558c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 6568c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_FALLING, 6578c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_VALUE) | 6588c2ecf20Sopenharmony_ci BIT(IIO_EV_INFO_ENABLE), 6598c2ecf20Sopenharmony_ci }, 6608c2ecf20Sopenharmony_ci}; 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_cistatic const struct iio_chan_spec adux1020_channels[] = { 6638c2ecf20Sopenharmony_ci { 6648c2ecf20Sopenharmony_ci .type = IIO_PROXIMITY, 6658c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 6668c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ), 6678c2ecf20Sopenharmony_ci .event_spec = adux1020_proximity_event, 6688c2ecf20Sopenharmony_ci .num_event_specs = ARRAY_SIZE(adux1020_proximity_event), 6698c2ecf20Sopenharmony_ci }, 6708c2ecf20Sopenharmony_ci { 6718c2ecf20Sopenharmony_ci .type = IIO_CURRENT, 6728c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 6738c2ecf20Sopenharmony_ci .extend_name = "led", 6748c2ecf20Sopenharmony_ci .output = 1, 6758c2ecf20Sopenharmony_ci }, 6768c2ecf20Sopenharmony_ci}; 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR_SAMP_FREQ_AVAIL( 6798c2ecf20Sopenharmony_ci "0.1 0.2 0.5 1 2 5 10 20 50 100 190 450 820 1400"); 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_cistatic struct attribute *adux1020_attributes[] = { 6828c2ecf20Sopenharmony_ci &iio_const_attr_sampling_frequency_available.dev_attr.attr, 6838c2ecf20Sopenharmony_ci NULL 6848c2ecf20Sopenharmony_ci}; 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_cistatic const struct attribute_group adux1020_attribute_group = { 6878c2ecf20Sopenharmony_ci .attrs = adux1020_attributes, 6888c2ecf20Sopenharmony_ci}; 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_cistatic const struct iio_info adux1020_info = { 6918c2ecf20Sopenharmony_ci .attrs = &adux1020_attribute_group, 6928c2ecf20Sopenharmony_ci .read_raw = adux1020_read_raw, 6938c2ecf20Sopenharmony_ci .write_raw = adux1020_write_raw, 6948c2ecf20Sopenharmony_ci .read_event_config = adux1020_read_event_config, 6958c2ecf20Sopenharmony_ci .write_event_config = adux1020_write_event_config, 6968c2ecf20Sopenharmony_ci .read_event_value = adux1020_read_thresh, 6978c2ecf20Sopenharmony_ci .write_event_value = adux1020_write_thresh, 6988c2ecf20Sopenharmony_ci}; 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_cistatic irqreturn_t adux1020_interrupt_handler(int irq, void *private) 7018c2ecf20Sopenharmony_ci{ 7028c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = private; 7038c2ecf20Sopenharmony_ci struct adux1020_data *data = iio_priv(indio_dev); 7048c2ecf20Sopenharmony_ci int ret, status; 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, ADUX1020_REG_INT_STATUS, &status); 7078c2ecf20Sopenharmony_ci if (ret < 0) 7088c2ecf20Sopenharmony_ci return IRQ_HANDLED; 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci status &= ADUX1020_MODE_INT_STATUS_MASK; 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci if (status & ADUX1020_INT_PROX_ON1) { 7138c2ecf20Sopenharmony_ci iio_push_event(indio_dev, 7148c2ecf20Sopenharmony_ci IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0, 7158c2ecf20Sopenharmony_ci IIO_EV_TYPE_THRESH, 7168c2ecf20Sopenharmony_ci IIO_EV_DIR_RISING), 7178c2ecf20Sopenharmony_ci iio_get_time_ns(indio_dev)); 7188c2ecf20Sopenharmony_ci } 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci if (status & ADUX1020_INT_PROX_OFF1) { 7218c2ecf20Sopenharmony_ci iio_push_event(indio_dev, 7228c2ecf20Sopenharmony_ci IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0, 7238c2ecf20Sopenharmony_ci IIO_EV_TYPE_THRESH, 7248c2ecf20Sopenharmony_ci IIO_EV_DIR_FALLING), 7258c2ecf20Sopenharmony_ci iio_get_time_ns(indio_dev)); 7268c2ecf20Sopenharmony_ci } 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci regmap_update_bits(data->regmap, ADUX1020_REG_INT_STATUS, 7298c2ecf20Sopenharmony_ci ADUX1020_MODE_INT_MASK, ADUX1020_INT_CLEAR); 7308c2ecf20Sopenharmony_ci 7318c2ecf20Sopenharmony_ci return IRQ_HANDLED; 7328c2ecf20Sopenharmony_ci} 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_cistatic int adux1020_chip_init(struct adux1020_data *data) 7358c2ecf20Sopenharmony_ci{ 7368c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 7378c2ecf20Sopenharmony_ci int ret; 7388c2ecf20Sopenharmony_ci unsigned int val; 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, ADUX1020_REG_CHIP_ID, &val); 7418c2ecf20Sopenharmony_ci if (ret < 0) 7428c2ecf20Sopenharmony_ci return ret; 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci if ((val & ADUX1020_CHIP_ID_MASK) != ADUX1020_CHIP_ID) { 7458c2ecf20Sopenharmony_ci dev_err(&client->dev, "invalid chip id 0x%04x\n", val); 7468c2ecf20Sopenharmony_ci return -ENODEV; 7478c2ecf20Sopenharmony_ci } 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci dev_dbg(&client->dev, "Detected ADUX1020 with chip id: 0x%04x\n", val); 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, ADUX1020_REG_SW_RESET, 7528c2ecf20Sopenharmony_ci ADUX1020_SW_RESET, ADUX1020_SW_RESET); 7538c2ecf20Sopenharmony_ci if (ret < 0) 7548c2ecf20Sopenharmony_ci return ret; 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci /* Load default configuration */ 7578c2ecf20Sopenharmony_ci ret = regmap_multi_reg_write(data->regmap, adux1020_def_conf, 7588c2ecf20Sopenharmony_ci ARRAY_SIZE(adux1020_def_conf)); 7598c2ecf20Sopenharmony_ci if (ret < 0) 7608c2ecf20Sopenharmony_ci return ret; 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_ci ret = adux1020_flush_fifo(data); 7638c2ecf20Sopenharmony_ci if (ret < 0) 7648c2ecf20Sopenharmony_ci return ret; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci /* Use LED_IREF for proximity mode */ 7678c2ecf20Sopenharmony_ci ret = regmap_update_bits(data->regmap, ADUX1020_REG_LED_CURRENT, 7688c2ecf20Sopenharmony_ci ADUX1020_LED_PIREF_EN, 0); 7698c2ecf20Sopenharmony_ci if (ret < 0) 7708c2ecf20Sopenharmony_ci return ret; 7718c2ecf20Sopenharmony_ci 7728c2ecf20Sopenharmony_ci /* Mask all interrupts */ 7738c2ecf20Sopenharmony_ci return regmap_update_bits(data->regmap, ADUX1020_REG_INT_MASK, 7748c2ecf20Sopenharmony_ci ADUX1020_MODE_INT_MASK, ADUX1020_MODE_INT_DISABLE); 7758c2ecf20Sopenharmony_ci} 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_cistatic int adux1020_probe(struct i2c_client *client, 7788c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 7798c2ecf20Sopenharmony_ci{ 7808c2ecf20Sopenharmony_ci struct adux1020_data *data; 7818c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 7828c2ecf20Sopenharmony_ci int ret; 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 7858c2ecf20Sopenharmony_ci if (!indio_dev) 7868c2ecf20Sopenharmony_ci return -ENOMEM; 7878c2ecf20Sopenharmony_ci 7888c2ecf20Sopenharmony_ci indio_dev->info = &adux1020_info; 7898c2ecf20Sopenharmony_ci indio_dev->name = ADUX1020_DRV_NAME; 7908c2ecf20Sopenharmony_ci indio_dev->channels = adux1020_channels; 7918c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(adux1020_channels); 7928c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ci data = iio_priv(indio_dev); 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_ci data->regmap = devm_regmap_init_i2c(client, &adux1020_regmap_config); 7978c2ecf20Sopenharmony_ci if (IS_ERR(data->regmap)) { 7988c2ecf20Sopenharmony_ci dev_err(&client->dev, "regmap initialization failed.\n"); 7998c2ecf20Sopenharmony_ci return PTR_ERR(data->regmap); 8008c2ecf20Sopenharmony_ci } 8018c2ecf20Sopenharmony_ci 8028c2ecf20Sopenharmony_ci data->client = client; 8038c2ecf20Sopenharmony_ci data->indio_dev = indio_dev; 8048c2ecf20Sopenharmony_ci mutex_init(&data->lock); 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_ci ret = adux1020_chip_init(data); 8078c2ecf20Sopenharmony_ci if (ret) 8088c2ecf20Sopenharmony_ci return ret; 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_ci if (client->irq) { 8118c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(&client->dev, client->irq, 8128c2ecf20Sopenharmony_ci NULL, adux1020_interrupt_handler, 8138c2ecf20Sopenharmony_ci IRQF_TRIGGER_HIGH | IRQF_ONESHOT, 8148c2ecf20Sopenharmony_ci ADUX1020_DRV_NAME, indio_dev); 8158c2ecf20Sopenharmony_ci if (ret) { 8168c2ecf20Sopenharmony_ci dev_err(&client->dev, "irq request error %d\n", -ret); 8178c2ecf20Sopenharmony_ci return ret; 8188c2ecf20Sopenharmony_ci } 8198c2ecf20Sopenharmony_ci } 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci return devm_iio_device_register(&client->dev, indio_dev); 8228c2ecf20Sopenharmony_ci} 8238c2ecf20Sopenharmony_ci 8248c2ecf20Sopenharmony_cistatic const struct i2c_device_id adux1020_id[] = { 8258c2ecf20Sopenharmony_ci { "adux1020", 0 }, 8268c2ecf20Sopenharmony_ci {} 8278c2ecf20Sopenharmony_ci}; 8288c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, adux1020_id); 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_cistatic const struct of_device_id adux1020_of_match[] = { 8318c2ecf20Sopenharmony_ci { .compatible = "adi,adux1020" }, 8328c2ecf20Sopenharmony_ci { } 8338c2ecf20Sopenharmony_ci}; 8348c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, adux1020_of_match); 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_cistatic struct i2c_driver adux1020_driver = { 8378c2ecf20Sopenharmony_ci .driver = { 8388c2ecf20Sopenharmony_ci .name = ADUX1020_DRV_NAME, 8398c2ecf20Sopenharmony_ci .of_match_table = adux1020_of_match, 8408c2ecf20Sopenharmony_ci }, 8418c2ecf20Sopenharmony_ci .probe = adux1020_probe, 8428c2ecf20Sopenharmony_ci .id_table = adux1020_id, 8438c2ecf20Sopenharmony_ci}; 8448c2ecf20Sopenharmony_cimodule_i2c_driver(adux1020_driver); 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ciMODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>"); 8478c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ADUX1020 photometric sensor"); 8488c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 849