18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * ltr501.c - Support for Lite-On LTR501 ambient light and proximity sensor 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2014 Peter Meerwald <pmeerw@pmeerw.net> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * 7-bit I2C slave address 0x23 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * TODO: IR LED characteristics 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/i2c.h> 148c2ecf20Sopenharmony_ci#include <linux/err.h> 158c2ecf20Sopenharmony_ci#include <linux/delay.h> 168c2ecf20Sopenharmony_ci#include <linux/regmap.h> 178c2ecf20Sopenharmony_ci#include <linux/acpi.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 208c2ecf20Sopenharmony_ci#include <linux/iio/events.h> 218c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 228c2ecf20Sopenharmony_ci#include <linux/iio/trigger_consumer.h> 238c2ecf20Sopenharmony_ci#include <linux/iio/buffer.h> 248c2ecf20Sopenharmony_ci#include <linux/iio/triggered_buffer.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define LTR501_DRV_NAME "ltr501" 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define LTR501_ALS_CONTR 0x80 /* ALS operation mode, SW reset */ 298c2ecf20Sopenharmony_ci#define LTR501_PS_CONTR 0x81 /* PS operation mode */ 308c2ecf20Sopenharmony_ci#define LTR501_PS_MEAS_RATE 0x84 /* measurement rate*/ 318c2ecf20Sopenharmony_ci#define LTR501_ALS_MEAS_RATE 0x85 /* ALS integ time, measurement rate*/ 328c2ecf20Sopenharmony_ci#define LTR501_PART_ID 0x86 338c2ecf20Sopenharmony_ci#define LTR501_MANUFAC_ID 0x87 348c2ecf20Sopenharmony_ci#define LTR501_ALS_DATA1 0x88 /* 16-bit, little endian */ 358c2ecf20Sopenharmony_ci#define LTR501_ALS_DATA1_UPPER 0x89 /* upper 8 bits of LTR501_ALS_DATA1 */ 368c2ecf20Sopenharmony_ci#define LTR501_ALS_DATA0 0x8a /* 16-bit, little endian */ 378c2ecf20Sopenharmony_ci#define LTR501_ALS_DATA0_UPPER 0x8b /* upper 8 bits of LTR501_ALS_DATA0 */ 388c2ecf20Sopenharmony_ci#define LTR501_ALS_PS_STATUS 0x8c 398c2ecf20Sopenharmony_ci#define LTR501_PS_DATA 0x8d /* 16-bit, little endian */ 408c2ecf20Sopenharmony_ci#define LTR501_PS_DATA_UPPER 0x8e /* upper 8 bits of LTR501_PS_DATA */ 418c2ecf20Sopenharmony_ci#define LTR501_INTR 0x8f /* output mode, polarity, mode */ 428c2ecf20Sopenharmony_ci#define LTR501_PS_THRESH_UP 0x90 /* 11 bit, ps upper threshold */ 438c2ecf20Sopenharmony_ci#define LTR501_PS_THRESH_LOW 0x92 /* 11 bit, ps lower threshold */ 448c2ecf20Sopenharmony_ci#define LTR501_ALS_THRESH_UP 0x97 /* 16 bit, ALS upper threshold */ 458c2ecf20Sopenharmony_ci#define LTR501_ALS_THRESH_LOW 0x99 /* 16 bit, ALS lower threshold */ 468c2ecf20Sopenharmony_ci#define LTR501_INTR_PRST 0x9e /* ps thresh, als thresh */ 478c2ecf20Sopenharmony_ci#define LTR501_MAX_REG 0x9f 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci#define LTR501_ALS_CONTR_SW_RESET BIT(2) 508c2ecf20Sopenharmony_ci#define LTR501_CONTR_PS_GAIN_MASK (BIT(3) | BIT(2)) 518c2ecf20Sopenharmony_ci#define LTR501_CONTR_PS_GAIN_SHIFT 2 528c2ecf20Sopenharmony_ci#define LTR501_CONTR_ALS_GAIN_MASK BIT(3) 538c2ecf20Sopenharmony_ci#define LTR501_CONTR_ACTIVE BIT(1) 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define LTR501_STATUS_ALS_INTR BIT(3) 568c2ecf20Sopenharmony_ci#define LTR501_STATUS_ALS_RDY BIT(2) 578c2ecf20Sopenharmony_ci#define LTR501_STATUS_PS_INTR BIT(1) 588c2ecf20Sopenharmony_ci#define LTR501_STATUS_PS_RDY BIT(0) 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#define LTR501_PS_DATA_MASK 0x7ff 618c2ecf20Sopenharmony_ci#define LTR501_PS_THRESH_MASK 0x7ff 628c2ecf20Sopenharmony_ci#define LTR501_ALS_THRESH_MASK 0xffff 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci#define LTR501_ALS_DEF_PERIOD 500000 658c2ecf20Sopenharmony_ci#define LTR501_PS_DEF_PERIOD 100000 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci#define LTR501_REGMAP_NAME "ltr501_regmap" 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci#define LTR501_LUX_CONV(vis_coeff, vis_data, ir_coeff, ir_data) \ 708c2ecf20Sopenharmony_ci ((vis_coeff * vis_data) - (ir_coeff * ir_data)) 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic const int int_time_mapping[] = {100000, 50000, 200000, 400000}; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic const struct reg_field reg_field_it = 758c2ecf20Sopenharmony_ci REG_FIELD(LTR501_ALS_MEAS_RATE, 3, 4); 768c2ecf20Sopenharmony_cistatic const struct reg_field reg_field_als_intr = 778c2ecf20Sopenharmony_ci REG_FIELD(LTR501_INTR, 1, 1); 788c2ecf20Sopenharmony_cistatic const struct reg_field reg_field_ps_intr = 798c2ecf20Sopenharmony_ci REG_FIELD(LTR501_INTR, 0, 0); 808c2ecf20Sopenharmony_cistatic const struct reg_field reg_field_als_rate = 818c2ecf20Sopenharmony_ci REG_FIELD(LTR501_ALS_MEAS_RATE, 0, 2); 828c2ecf20Sopenharmony_cistatic const struct reg_field reg_field_ps_rate = 838c2ecf20Sopenharmony_ci REG_FIELD(LTR501_PS_MEAS_RATE, 0, 3); 848c2ecf20Sopenharmony_cistatic const struct reg_field reg_field_als_prst = 858c2ecf20Sopenharmony_ci REG_FIELD(LTR501_INTR_PRST, 0, 3); 868c2ecf20Sopenharmony_cistatic const struct reg_field reg_field_ps_prst = 878c2ecf20Sopenharmony_ci REG_FIELD(LTR501_INTR_PRST, 4, 7); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistruct ltr501_samp_table { 908c2ecf20Sopenharmony_ci int freq_val; /* repetition frequency in micro HZ*/ 918c2ecf20Sopenharmony_ci int time_val; /* repetition rate in micro seconds */ 928c2ecf20Sopenharmony_ci}; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci#define LTR501_RESERVED_GAIN -1 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cienum { 978c2ecf20Sopenharmony_ci ltr501 = 0, 988c2ecf20Sopenharmony_ci ltr559, 998c2ecf20Sopenharmony_ci ltr301, 1008c2ecf20Sopenharmony_ci}; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistruct ltr501_gain { 1038c2ecf20Sopenharmony_ci int scale; 1048c2ecf20Sopenharmony_ci int uscale; 1058c2ecf20Sopenharmony_ci}; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic const struct ltr501_gain ltr501_als_gain_tbl[] = { 1088c2ecf20Sopenharmony_ci {1, 0}, 1098c2ecf20Sopenharmony_ci {0, 5000}, 1108c2ecf20Sopenharmony_ci}; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_cistatic const struct ltr501_gain ltr559_als_gain_tbl[] = { 1138c2ecf20Sopenharmony_ci {1, 0}, 1148c2ecf20Sopenharmony_ci {0, 500000}, 1158c2ecf20Sopenharmony_ci {0, 250000}, 1168c2ecf20Sopenharmony_ci {0, 125000}, 1178c2ecf20Sopenharmony_ci {LTR501_RESERVED_GAIN, LTR501_RESERVED_GAIN}, 1188c2ecf20Sopenharmony_ci {LTR501_RESERVED_GAIN, LTR501_RESERVED_GAIN}, 1198c2ecf20Sopenharmony_ci {0, 20000}, 1208c2ecf20Sopenharmony_ci {0, 10000}, 1218c2ecf20Sopenharmony_ci}; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic const struct ltr501_gain ltr501_ps_gain_tbl[] = { 1248c2ecf20Sopenharmony_ci {1, 0}, 1258c2ecf20Sopenharmony_ci {0, 250000}, 1268c2ecf20Sopenharmony_ci {0, 125000}, 1278c2ecf20Sopenharmony_ci {0, 62500}, 1288c2ecf20Sopenharmony_ci}; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic const struct ltr501_gain ltr559_ps_gain_tbl[] = { 1318c2ecf20Sopenharmony_ci {0, 62500}, /* x16 gain */ 1328c2ecf20Sopenharmony_ci {0, 31250}, /* x32 gain */ 1338c2ecf20Sopenharmony_ci {0, 15625}, /* bits X1 are for x64 gain */ 1348c2ecf20Sopenharmony_ci {0, 15624}, 1358c2ecf20Sopenharmony_ci}; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cistruct ltr501_chip_info { 1388c2ecf20Sopenharmony_ci u8 partid; 1398c2ecf20Sopenharmony_ci const struct ltr501_gain *als_gain; 1408c2ecf20Sopenharmony_ci int als_gain_tbl_size; 1418c2ecf20Sopenharmony_ci const struct ltr501_gain *ps_gain; 1428c2ecf20Sopenharmony_ci int ps_gain_tbl_size; 1438c2ecf20Sopenharmony_ci u8 als_mode_active; 1448c2ecf20Sopenharmony_ci u8 als_gain_mask; 1458c2ecf20Sopenharmony_ci u8 als_gain_shift; 1468c2ecf20Sopenharmony_ci struct iio_chan_spec const *channels; 1478c2ecf20Sopenharmony_ci const int no_channels; 1488c2ecf20Sopenharmony_ci const struct iio_info *info; 1498c2ecf20Sopenharmony_ci const struct iio_info *info_no_irq; 1508c2ecf20Sopenharmony_ci}; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistruct ltr501_data { 1538c2ecf20Sopenharmony_ci struct i2c_client *client; 1548c2ecf20Sopenharmony_ci struct mutex lock_als, lock_ps; 1558c2ecf20Sopenharmony_ci struct ltr501_chip_info *chip_info; 1568c2ecf20Sopenharmony_ci u8 als_contr, ps_contr; 1578c2ecf20Sopenharmony_ci int als_period, ps_period; /* period in micro seconds */ 1588c2ecf20Sopenharmony_ci struct regmap *regmap; 1598c2ecf20Sopenharmony_ci struct regmap_field *reg_it; 1608c2ecf20Sopenharmony_ci struct regmap_field *reg_als_intr; 1618c2ecf20Sopenharmony_ci struct regmap_field *reg_ps_intr; 1628c2ecf20Sopenharmony_ci struct regmap_field *reg_als_rate; 1638c2ecf20Sopenharmony_ci struct regmap_field *reg_ps_rate; 1648c2ecf20Sopenharmony_ci struct regmap_field *reg_als_prst; 1658c2ecf20Sopenharmony_ci struct regmap_field *reg_ps_prst; 1668c2ecf20Sopenharmony_ci}; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic const struct ltr501_samp_table ltr501_als_samp_table[] = { 1698c2ecf20Sopenharmony_ci {20000000, 50000}, {10000000, 100000}, 1708c2ecf20Sopenharmony_ci {5000000, 200000}, {2000000, 500000}, 1718c2ecf20Sopenharmony_ci {1000000, 1000000}, {500000, 2000000}, 1728c2ecf20Sopenharmony_ci {500000, 2000000}, {500000, 2000000} 1738c2ecf20Sopenharmony_ci}; 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic const struct ltr501_samp_table ltr501_ps_samp_table[] = { 1768c2ecf20Sopenharmony_ci {20000000, 50000}, {14285714, 70000}, 1778c2ecf20Sopenharmony_ci {10000000, 100000}, {5000000, 200000}, 1788c2ecf20Sopenharmony_ci {2000000, 500000}, {1000000, 1000000}, 1798c2ecf20Sopenharmony_ci {500000, 2000000}, {500000, 2000000}, 1808c2ecf20Sopenharmony_ci {500000, 2000000} 1818c2ecf20Sopenharmony_ci}; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cistatic int ltr501_match_samp_freq(const struct ltr501_samp_table *tab, 1848c2ecf20Sopenharmony_ci int len, int val, int val2) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci int i, freq; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci freq = val * 1000000 + val2; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci for (i = 0; i < len; i++) { 1918c2ecf20Sopenharmony_ci if (tab[i].freq_val == freq) 1928c2ecf20Sopenharmony_ci return i; 1938c2ecf20Sopenharmony_ci } 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci return -EINVAL; 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_cistatic int ltr501_als_read_samp_freq(const struct ltr501_data *data, 1998c2ecf20Sopenharmony_ci int *val, int *val2) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci int ret, i; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci ret = regmap_field_read(data->reg_als_rate, &i); 2048c2ecf20Sopenharmony_ci if (ret < 0) 2058c2ecf20Sopenharmony_ci return ret; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci if (i < 0 || i >= ARRAY_SIZE(ltr501_als_samp_table)) 2088c2ecf20Sopenharmony_ci return -EINVAL; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci *val = ltr501_als_samp_table[i].freq_val / 1000000; 2118c2ecf20Sopenharmony_ci *val2 = ltr501_als_samp_table[i].freq_val % 1000000; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 2148c2ecf20Sopenharmony_ci} 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_cistatic int ltr501_ps_read_samp_freq(const struct ltr501_data *data, 2178c2ecf20Sopenharmony_ci int *val, int *val2) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci int ret, i; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci ret = regmap_field_read(data->reg_ps_rate, &i); 2228c2ecf20Sopenharmony_ci if (ret < 0) 2238c2ecf20Sopenharmony_ci return ret; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci if (i < 0 || i >= ARRAY_SIZE(ltr501_ps_samp_table)) 2268c2ecf20Sopenharmony_ci return -EINVAL; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci *val = ltr501_ps_samp_table[i].freq_val / 1000000; 2298c2ecf20Sopenharmony_ci *val2 = ltr501_ps_samp_table[i].freq_val % 1000000; 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 2328c2ecf20Sopenharmony_ci} 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cistatic int ltr501_als_write_samp_freq(struct ltr501_data *data, 2358c2ecf20Sopenharmony_ci int val, int val2) 2368c2ecf20Sopenharmony_ci{ 2378c2ecf20Sopenharmony_ci int i, ret; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci i = ltr501_match_samp_freq(ltr501_als_samp_table, 2408c2ecf20Sopenharmony_ci ARRAY_SIZE(ltr501_als_samp_table), 2418c2ecf20Sopenharmony_ci val, val2); 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci if (i < 0) 2448c2ecf20Sopenharmony_ci return i; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci mutex_lock(&data->lock_als); 2478c2ecf20Sopenharmony_ci ret = regmap_field_write(data->reg_als_rate, i); 2488c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_als); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci return ret; 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic int ltr501_ps_write_samp_freq(struct ltr501_data *data, 2548c2ecf20Sopenharmony_ci int val, int val2) 2558c2ecf20Sopenharmony_ci{ 2568c2ecf20Sopenharmony_ci int i, ret; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci i = ltr501_match_samp_freq(ltr501_ps_samp_table, 2598c2ecf20Sopenharmony_ci ARRAY_SIZE(ltr501_ps_samp_table), 2608c2ecf20Sopenharmony_ci val, val2); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci if (i < 0) 2638c2ecf20Sopenharmony_ci return i; 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci mutex_lock(&data->lock_ps); 2668c2ecf20Sopenharmony_ci ret = regmap_field_write(data->reg_ps_rate, i); 2678c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_ps); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci return ret; 2708c2ecf20Sopenharmony_ci} 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_cistatic int ltr501_als_read_samp_period(const struct ltr501_data *data, int *val) 2738c2ecf20Sopenharmony_ci{ 2748c2ecf20Sopenharmony_ci int ret, i; 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci ret = regmap_field_read(data->reg_als_rate, &i); 2778c2ecf20Sopenharmony_ci if (ret < 0) 2788c2ecf20Sopenharmony_ci return ret; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci if (i < 0 || i >= ARRAY_SIZE(ltr501_als_samp_table)) 2818c2ecf20Sopenharmony_ci return -EINVAL; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci *val = ltr501_als_samp_table[i].time_val; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci return IIO_VAL_INT; 2868c2ecf20Sopenharmony_ci} 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_cistatic int ltr501_ps_read_samp_period(const struct ltr501_data *data, int *val) 2898c2ecf20Sopenharmony_ci{ 2908c2ecf20Sopenharmony_ci int ret, i; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci ret = regmap_field_read(data->reg_ps_rate, &i); 2938c2ecf20Sopenharmony_ci if (ret < 0) 2948c2ecf20Sopenharmony_ci return ret; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci if (i < 0 || i >= ARRAY_SIZE(ltr501_ps_samp_table)) 2978c2ecf20Sopenharmony_ci return -EINVAL; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci *val = ltr501_ps_samp_table[i].time_val; 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci return IIO_VAL_INT; 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci/* IR and visible spectrum coeff's are given in data sheet */ 3058c2ecf20Sopenharmony_cistatic unsigned long ltr501_calculate_lux(u16 vis_data, u16 ir_data) 3068c2ecf20Sopenharmony_ci{ 3078c2ecf20Sopenharmony_ci unsigned long ratio, lux; 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci if (vis_data == 0) 3108c2ecf20Sopenharmony_ci return 0; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci /* multiply numerator by 100 to avoid handling ratio < 1 */ 3138c2ecf20Sopenharmony_ci ratio = DIV_ROUND_UP(ir_data * 100, ir_data + vis_data); 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci if (ratio < 45) 3168c2ecf20Sopenharmony_ci lux = LTR501_LUX_CONV(1774, vis_data, -1105, ir_data); 3178c2ecf20Sopenharmony_ci else if (ratio >= 45 && ratio < 64) 3188c2ecf20Sopenharmony_ci lux = LTR501_LUX_CONV(3772, vis_data, 1336, ir_data); 3198c2ecf20Sopenharmony_ci else if (ratio >= 64 && ratio < 85) 3208c2ecf20Sopenharmony_ci lux = LTR501_LUX_CONV(1690, vis_data, 169, ir_data); 3218c2ecf20Sopenharmony_ci else 3228c2ecf20Sopenharmony_ci lux = 0; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci return lux / 1000; 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_cistatic int ltr501_drdy(const struct ltr501_data *data, u8 drdy_mask) 3288c2ecf20Sopenharmony_ci{ 3298c2ecf20Sopenharmony_ci int tries = 100; 3308c2ecf20Sopenharmony_ci int ret, status; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci while (tries--) { 3338c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, LTR501_ALS_PS_STATUS, &status); 3348c2ecf20Sopenharmony_ci if (ret < 0) 3358c2ecf20Sopenharmony_ci return ret; 3368c2ecf20Sopenharmony_ci if ((status & drdy_mask) == drdy_mask) 3378c2ecf20Sopenharmony_ci return 0; 3388c2ecf20Sopenharmony_ci msleep(25); 3398c2ecf20Sopenharmony_ci } 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci dev_err(&data->client->dev, "ltr501_drdy() failed, data not ready\n"); 3428c2ecf20Sopenharmony_ci return -EIO; 3438c2ecf20Sopenharmony_ci} 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_cistatic int ltr501_set_it_time(struct ltr501_data *data, int it) 3468c2ecf20Sopenharmony_ci{ 3478c2ecf20Sopenharmony_ci int ret, i, index = -1, status; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(int_time_mapping); i++) { 3508c2ecf20Sopenharmony_ci if (int_time_mapping[i] == it) { 3518c2ecf20Sopenharmony_ci index = i; 3528c2ecf20Sopenharmony_ci break; 3538c2ecf20Sopenharmony_ci } 3548c2ecf20Sopenharmony_ci } 3558c2ecf20Sopenharmony_ci /* Make sure integ time index is valid */ 3568c2ecf20Sopenharmony_ci if (index < 0) 3578c2ecf20Sopenharmony_ci return -EINVAL; 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, LTR501_ALS_CONTR, &status); 3608c2ecf20Sopenharmony_ci if (ret < 0) 3618c2ecf20Sopenharmony_ci return ret; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci if (status & LTR501_CONTR_ALS_GAIN_MASK) { 3648c2ecf20Sopenharmony_ci /* 3658c2ecf20Sopenharmony_ci * 200 ms and 400 ms integ time can only be 3668c2ecf20Sopenharmony_ci * used in dynamic range 1 3678c2ecf20Sopenharmony_ci */ 3688c2ecf20Sopenharmony_ci if (index > 1) 3698c2ecf20Sopenharmony_ci return -EINVAL; 3708c2ecf20Sopenharmony_ci } else 3718c2ecf20Sopenharmony_ci /* 50 ms integ time can only be used in dynamic range 2 */ 3728c2ecf20Sopenharmony_ci if (index == 1) 3738c2ecf20Sopenharmony_ci return -EINVAL; 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci return regmap_field_write(data->reg_it, index); 3768c2ecf20Sopenharmony_ci} 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci/* read int time in micro seconds */ 3798c2ecf20Sopenharmony_cistatic int ltr501_read_it_time(const struct ltr501_data *data, 3808c2ecf20Sopenharmony_ci int *val, int *val2) 3818c2ecf20Sopenharmony_ci{ 3828c2ecf20Sopenharmony_ci int ret, index; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci ret = regmap_field_read(data->reg_it, &index); 3858c2ecf20Sopenharmony_ci if (ret < 0) 3868c2ecf20Sopenharmony_ci return ret; 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci /* Make sure integ time index is valid */ 3898c2ecf20Sopenharmony_ci if (index < 0 || index >= ARRAY_SIZE(int_time_mapping)) 3908c2ecf20Sopenharmony_ci return -EINVAL; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci *val2 = int_time_mapping[index]; 3938c2ecf20Sopenharmony_ci *val = 0; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_cistatic int ltr501_read_als(const struct ltr501_data *data, __le16 buf[2]) 3998c2ecf20Sopenharmony_ci{ 4008c2ecf20Sopenharmony_ci int ret; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci ret = ltr501_drdy(data, LTR501_STATUS_ALS_RDY); 4038c2ecf20Sopenharmony_ci if (ret < 0) 4048c2ecf20Sopenharmony_ci return ret; 4058c2ecf20Sopenharmony_ci /* always read both ALS channels in given order */ 4068c2ecf20Sopenharmony_ci return regmap_bulk_read(data->regmap, LTR501_ALS_DATA1, 4078c2ecf20Sopenharmony_ci buf, 2 * sizeof(__le16)); 4088c2ecf20Sopenharmony_ci} 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_cistatic int ltr501_read_ps(const struct ltr501_data *data) 4118c2ecf20Sopenharmony_ci{ 4128c2ecf20Sopenharmony_ci __le16 status; 4138c2ecf20Sopenharmony_ci int ret; 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci ret = ltr501_drdy(data, LTR501_STATUS_PS_RDY); 4168c2ecf20Sopenharmony_ci if (ret < 0) 4178c2ecf20Sopenharmony_ci return ret; 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci ret = regmap_bulk_read(data->regmap, LTR501_PS_DATA, 4208c2ecf20Sopenharmony_ci &status, sizeof(status)); 4218c2ecf20Sopenharmony_ci if (ret < 0) 4228c2ecf20Sopenharmony_ci return ret; 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci return le16_to_cpu(status); 4258c2ecf20Sopenharmony_ci} 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_cistatic int ltr501_read_intr_prst(const struct ltr501_data *data, 4288c2ecf20Sopenharmony_ci enum iio_chan_type type, 4298c2ecf20Sopenharmony_ci int *val2) 4308c2ecf20Sopenharmony_ci{ 4318c2ecf20Sopenharmony_ci int ret, samp_period, prst; 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci switch (type) { 4348c2ecf20Sopenharmony_ci case IIO_INTENSITY: 4358c2ecf20Sopenharmony_ci ret = regmap_field_read(data->reg_als_prst, &prst); 4368c2ecf20Sopenharmony_ci if (ret < 0) 4378c2ecf20Sopenharmony_ci return ret; 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci ret = ltr501_als_read_samp_period(data, &samp_period); 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci if (ret < 0) 4428c2ecf20Sopenharmony_ci return ret; 4438c2ecf20Sopenharmony_ci *val2 = samp_period * prst; 4448c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 4458c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 4468c2ecf20Sopenharmony_ci ret = regmap_field_read(data->reg_ps_prst, &prst); 4478c2ecf20Sopenharmony_ci if (ret < 0) 4488c2ecf20Sopenharmony_ci return ret; 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci ret = ltr501_ps_read_samp_period(data, &samp_period); 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci if (ret < 0) 4538c2ecf20Sopenharmony_ci return ret; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci *val2 = samp_period * prst; 4568c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 4578c2ecf20Sopenharmony_ci default: 4588c2ecf20Sopenharmony_ci return -EINVAL; 4598c2ecf20Sopenharmony_ci } 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci return -EINVAL; 4628c2ecf20Sopenharmony_ci} 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_cistatic int ltr501_write_intr_prst(struct ltr501_data *data, 4658c2ecf20Sopenharmony_ci enum iio_chan_type type, 4668c2ecf20Sopenharmony_ci int val, int val2) 4678c2ecf20Sopenharmony_ci{ 4688c2ecf20Sopenharmony_ci int ret, samp_period, new_val; 4698c2ecf20Sopenharmony_ci unsigned long period; 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci if (val < 0 || val2 < 0) 4728c2ecf20Sopenharmony_ci return -EINVAL; 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci /* period in microseconds */ 4758c2ecf20Sopenharmony_ci period = ((val * 1000000) + val2); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci switch (type) { 4788c2ecf20Sopenharmony_ci case IIO_INTENSITY: 4798c2ecf20Sopenharmony_ci ret = ltr501_als_read_samp_period(data, &samp_period); 4808c2ecf20Sopenharmony_ci if (ret < 0) 4818c2ecf20Sopenharmony_ci return ret; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci /* period should be atleast equal to sampling period */ 4848c2ecf20Sopenharmony_ci if (period < samp_period) 4858c2ecf20Sopenharmony_ci return -EINVAL; 4868c2ecf20Sopenharmony_ci 4878c2ecf20Sopenharmony_ci new_val = DIV_ROUND_UP(period, samp_period); 4888c2ecf20Sopenharmony_ci if (new_val < 0 || new_val > 0x0f) 4898c2ecf20Sopenharmony_ci return -EINVAL; 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci mutex_lock(&data->lock_als); 4928c2ecf20Sopenharmony_ci ret = regmap_field_write(data->reg_als_prst, new_val); 4938c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_als); 4948c2ecf20Sopenharmony_ci if (ret >= 0) 4958c2ecf20Sopenharmony_ci data->als_period = period; 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci return ret; 4988c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 4998c2ecf20Sopenharmony_ci ret = ltr501_ps_read_samp_period(data, &samp_period); 5008c2ecf20Sopenharmony_ci if (ret < 0) 5018c2ecf20Sopenharmony_ci return ret; 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci /* period should be atleast equal to rate */ 5048c2ecf20Sopenharmony_ci if (period < samp_period) 5058c2ecf20Sopenharmony_ci return -EINVAL; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci new_val = DIV_ROUND_UP(period, samp_period); 5088c2ecf20Sopenharmony_ci if (new_val < 0 || new_val > 0x0f) 5098c2ecf20Sopenharmony_ci return -EINVAL; 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_ci mutex_lock(&data->lock_ps); 5128c2ecf20Sopenharmony_ci ret = regmap_field_write(data->reg_ps_prst, new_val); 5138c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_ps); 5148c2ecf20Sopenharmony_ci if (ret >= 0) 5158c2ecf20Sopenharmony_ci data->ps_period = period; 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ci return ret; 5188c2ecf20Sopenharmony_ci default: 5198c2ecf20Sopenharmony_ci return -EINVAL; 5208c2ecf20Sopenharmony_ci } 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci return -EINVAL; 5238c2ecf20Sopenharmony_ci} 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_cistatic const struct iio_event_spec ltr501_als_event_spec[] = { 5268c2ecf20Sopenharmony_ci { 5278c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 5288c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_RISING, 5298c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_VALUE), 5308c2ecf20Sopenharmony_ci }, { 5318c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 5328c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_FALLING, 5338c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_VALUE), 5348c2ecf20Sopenharmony_ci }, { 5358c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 5368c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_EITHER, 5378c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_ENABLE) | 5388c2ecf20Sopenharmony_ci BIT(IIO_EV_INFO_PERIOD), 5398c2ecf20Sopenharmony_ci }, 5408c2ecf20Sopenharmony_ci 5418c2ecf20Sopenharmony_ci}; 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_cistatic const struct iio_event_spec ltr501_pxs_event_spec[] = { 5448c2ecf20Sopenharmony_ci { 5458c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 5468c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_RISING, 5478c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_VALUE), 5488c2ecf20Sopenharmony_ci }, { 5498c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 5508c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_FALLING, 5518c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_VALUE), 5528c2ecf20Sopenharmony_ci }, { 5538c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 5548c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_EITHER, 5558c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_ENABLE) | 5568c2ecf20Sopenharmony_ci BIT(IIO_EV_INFO_PERIOD), 5578c2ecf20Sopenharmony_ci }, 5588c2ecf20Sopenharmony_ci}; 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci#define LTR501_INTENSITY_CHANNEL(_idx, _addr, _mod, _shared, \ 5618c2ecf20Sopenharmony_ci _evspec, _evsize) { \ 5628c2ecf20Sopenharmony_ci .type = IIO_INTENSITY, \ 5638c2ecf20Sopenharmony_ci .modified = 1, \ 5648c2ecf20Sopenharmony_ci .address = (_addr), \ 5658c2ecf20Sopenharmony_ci .channel2 = (_mod), \ 5668c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 5678c2ecf20Sopenharmony_ci .info_mask_shared_by_type = (_shared), \ 5688c2ecf20Sopenharmony_ci .scan_index = (_idx), \ 5698c2ecf20Sopenharmony_ci .scan_type = { \ 5708c2ecf20Sopenharmony_ci .sign = 'u', \ 5718c2ecf20Sopenharmony_ci .realbits = 16, \ 5728c2ecf20Sopenharmony_ci .storagebits = 16, \ 5738c2ecf20Sopenharmony_ci .endianness = IIO_CPU, \ 5748c2ecf20Sopenharmony_ci }, \ 5758c2ecf20Sopenharmony_ci .event_spec = _evspec,\ 5768c2ecf20Sopenharmony_ci .num_event_specs = _evsize,\ 5778c2ecf20Sopenharmony_ci} 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci#define LTR501_LIGHT_CHANNEL() { \ 5808c2ecf20Sopenharmony_ci .type = IIO_LIGHT, \ 5818c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ 5828c2ecf20Sopenharmony_ci .scan_index = -1, \ 5838c2ecf20Sopenharmony_ci} 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_cistatic const struct iio_chan_spec ltr501_channels[] = { 5868c2ecf20Sopenharmony_ci LTR501_LIGHT_CHANNEL(), 5878c2ecf20Sopenharmony_ci LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0, 5888c2ecf20Sopenharmony_ci ltr501_als_event_spec, 5898c2ecf20Sopenharmony_ci ARRAY_SIZE(ltr501_als_event_spec)), 5908c2ecf20Sopenharmony_ci LTR501_INTENSITY_CHANNEL(1, LTR501_ALS_DATA1, IIO_MOD_LIGHT_IR, 5918c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 5928c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_INT_TIME) | 5938c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ), 5948c2ecf20Sopenharmony_ci NULL, 0), 5958c2ecf20Sopenharmony_ci { 5968c2ecf20Sopenharmony_ci .type = IIO_PROXIMITY, 5978c2ecf20Sopenharmony_ci .address = LTR501_PS_DATA, 5988c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 5998c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE), 6008c2ecf20Sopenharmony_ci .scan_index = 2, 6018c2ecf20Sopenharmony_ci .scan_type = { 6028c2ecf20Sopenharmony_ci .sign = 'u', 6038c2ecf20Sopenharmony_ci .realbits = 11, 6048c2ecf20Sopenharmony_ci .storagebits = 16, 6058c2ecf20Sopenharmony_ci .endianness = IIO_CPU, 6068c2ecf20Sopenharmony_ci }, 6078c2ecf20Sopenharmony_ci .event_spec = ltr501_pxs_event_spec, 6088c2ecf20Sopenharmony_ci .num_event_specs = ARRAY_SIZE(ltr501_pxs_event_spec), 6098c2ecf20Sopenharmony_ci }, 6108c2ecf20Sopenharmony_ci IIO_CHAN_SOFT_TIMESTAMP(3), 6118c2ecf20Sopenharmony_ci}; 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_cistatic const struct iio_chan_spec ltr301_channels[] = { 6148c2ecf20Sopenharmony_ci LTR501_LIGHT_CHANNEL(), 6158c2ecf20Sopenharmony_ci LTR501_INTENSITY_CHANNEL(0, LTR501_ALS_DATA0, IIO_MOD_LIGHT_BOTH, 0, 6168c2ecf20Sopenharmony_ci ltr501_als_event_spec, 6178c2ecf20Sopenharmony_ci ARRAY_SIZE(ltr501_als_event_spec)), 6188c2ecf20Sopenharmony_ci LTR501_INTENSITY_CHANNEL(1, LTR501_ALS_DATA1, IIO_MOD_LIGHT_IR, 6198c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 6208c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_INT_TIME) | 6218c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SAMP_FREQ), 6228c2ecf20Sopenharmony_ci NULL, 0), 6238c2ecf20Sopenharmony_ci IIO_CHAN_SOFT_TIMESTAMP(2), 6248c2ecf20Sopenharmony_ci}; 6258c2ecf20Sopenharmony_ci 6268c2ecf20Sopenharmony_cistatic int ltr501_read_raw(struct iio_dev *indio_dev, 6278c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 6288c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 6298c2ecf20Sopenharmony_ci{ 6308c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(indio_dev); 6318c2ecf20Sopenharmony_ci __le16 buf[2]; 6328c2ecf20Sopenharmony_ci int ret, i; 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci switch (mask) { 6358c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_PROCESSED: 6368c2ecf20Sopenharmony_ci switch (chan->type) { 6378c2ecf20Sopenharmony_ci case IIO_LIGHT: 6388c2ecf20Sopenharmony_ci ret = iio_device_claim_direct_mode(indio_dev); 6398c2ecf20Sopenharmony_ci if (ret) 6408c2ecf20Sopenharmony_ci return ret; 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci mutex_lock(&data->lock_als); 6438c2ecf20Sopenharmony_ci ret = ltr501_read_als(data, buf); 6448c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_als); 6458c2ecf20Sopenharmony_ci iio_device_release_direct_mode(indio_dev); 6468c2ecf20Sopenharmony_ci if (ret < 0) 6478c2ecf20Sopenharmony_ci return ret; 6488c2ecf20Sopenharmony_ci *val = ltr501_calculate_lux(le16_to_cpu(buf[1]), 6498c2ecf20Sopenharmony_ci le16_to_cpu(buf[0])); 6508c2ecf20Sopenharmony_ci return IIO_VAL_INT; 6518c2ecf20Sopenharmony_ci default: 6528c2ecf20Sopenharmony_ci return -EINVAL; 6538c2ecf20Sopenharmony_ci } 6548c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 6558c2ecf20Sopenharmony_ci ret = iio_device_claim_direct_mode(indio_dev); 6568c2ecf20Sopenharmony_ci if (ret) 6578c2ecf20Sopenharmony_ci return ret; 6588c2ecf20Sopenharmony_ci 6598c2ecf20Sopenharmony_ci switch (chan->type) { 6608c2ecf20Sopenharmony_ci case IIO_INTENSITY: 6618c2ecf20Sopenharmony_ci mutex_lock(&data->lock_als); 6628c2ecf20Sopenharmony_ci ret = ltr501_read_als(data, buf); 6638c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_als); 6648c2ecf20Sopenharmony_ci if (ret < 0) 6658c2ecf20Sopenharmony_ci break; 6668c2ecf20Sopenharmony_ci *val = le16_to_cpu(chan->address == LTR501_ALS_DATA1 ? 6678c2ecf20Sopenharmony_ci buf[0] : buf[1]); 6688c2ecf20Sopenharmony_ci ret = IIO_VAL_INT; 6698c2ecf20Sopenharmony_ci break; 6708c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 6718c2ecf20Sopenharmony_ci mutex_lock(&data->lock_ps); 6728c2ecf20Sopenharmony_ci ret = ltr501_read_ps(data); 6738c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_ps); 6748c2ecf20Sopenharmony_ci if (ret < 0) 6758c2ecf20Sopenharmony_ci break; 6768c2ecf20Sopenharmony_ci *val = ret & LTR501_PS_DATA_MASK; 6778c2ecf20Sopenharmony_ci ret = IIO_VAL_INT; 6788c2ecf20Sopenharmony_ci break; 6798c2ecf20Sopenharmony_ci default: 6808c2ecf20Sopenharmony_ci ret = -EINVAL; 6818c2ecf20Sopenharmony_ci break; 6828c2ecf20Sopenharmony_ci } 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_ci iio_device_release_direct_mode(indio_dev); 6858c2ecf20Sopenharmony_ci return ret; 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 6888c2ecf20Sopenharmony_ci switch (chan->type) { 6898c2ecf20Sopenharmony_ci case IIO_INTENSITY: 6908c2ecf20Sopenharmony_ci i = (data->als_contr & data->chip_info->als_gain_mask) 6918c2ecf20Sopenharmony_ci >> data->chip_info->als_gain_shift; 6928c2ecf20Sopenharmony_ci *val = data->chip_info->als_gain[i].scale; 6938c2ecf20Sopenharmony_ci *val2 = data->chip_info->als_gain[i].uscale; 6948c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 6958c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 6968c2ecf20Sopenharmony_ci i = (data->ps_contr & LTR501_CONTR_PS_GAIN_MASK) >> 6978c2ecf20Sopenharmony_ci LTR501_CONTR_PS_GAIN_SHIFT; 6988c2ecf20Sopenharmony_ci *val = data->chip_info->ps_gain[i].scale; 6998c2ecf20Sopenharmony_ci *val2 = data->chip_info->ps_gain[i].uscale; 7008c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 7018c2ecf20Sopenharmony_ci default: 7028c2ecf20Sopenharmony_ci return -EINVAL; 7038c2ecf20Sopenharmony_ci } 7048c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_INT_TIME: 7058c2ecf20Sopenharmony_ci switch (chan->type) { 7068c2ecf20Sopenharmony_ci case IIO_INTENSITY: 7078c2ecf20Sopenharmony_ci return ltr501_read_it_time(data, val, val2); 7088c2ecf20Sopenharmony_ci default: 7098c2ecf20Sopenharmony_ci return -EINVAL; 7108c2ecf20Sopenharmony_ci } 7118c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 7128c2ecf20Sopenharmony_ci switch (chan->type) { 7138c2ecf20Sopenharmony_ci case IIO_INTENSITY: 7148c2ecf20Sopenharmony_ci return ltr501_als_read_samp_freq(data, val, val2); 7158c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 7168c2ecf20Sopenharmony_ci return ltr501_ps_read_samp_freq(data, val, val2); 7178c2ecf20Sopenharmony_ci default: 7188c2ecf20Sopenharmony_ci return -EINVAL; 7198c2ecf20Sopenharmony_ci } 7208c2ecf20Sopenharmony_ci } 7218c2ecf20Sopenharmony_ci return -EINVAL; 7228c2ecf20Sopenharmony_ci} 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_cistatic int ltr501_get_gain_index(const struct ltr501_gain *gain, int size, 7258c2ecf20Sopenharmony_ci int val, int val2) 7268c2ecf20Sopenharmony_ci{ 7278c2ecf20Sopenharmony_ci int i; 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci for (i = 0; i < size; i++) 7308c2ecf20Sopenharmony_ci if (val == gain[i].scale && val2 == gain[i].uscale) 7318c2ecf20Sopenharmony_ci return i; 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_ci return -1; 7348c2ecf20Sopenharmony_ci} 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_cistatic int ltr501_write_raw(struct iio_dev *indio_dev, 7378c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 7388c2ecf20Sopenharmony_ci int val, int val2, long mask) 7398c2ecf20Sopenharmony_ci{ 7408c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(indio_dev); 7418c2ecf20Sopenharmony_ci int i, ret, freq_val, freq_val2; 7428c2ecf20Sopenharmony_ci struct ltr501_chip_info *info = data->chip_info; 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci ret = iio_device_claim_direct_mode(indio_dev); 7458c2ecf20Sopenharmony_ci if (ret) 7468c2ecf20Sopenharmony_ci return ret; 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci switch (mask) { 7498c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 7508c2ecf20Sopenharmony_ci switch (chan->type) { 7518c2ecf20Sopenharmony_ci case IIO_INTENSITY: 7528c2ecf20Sopenharmony_ci i = ltr501_get_gain_index(info->als_gain, 7538c2ecf20Sopenharmony_ci info->als_gain_tbl_size, 7548c2ecf20Sopenharmony_ci val, val2); 7558c2ecf20Sopenharmony_ci if (i < 0) { 7568c2ecf20Sopenharmony_ci ret = -EINVAL; 7578c2ecf20Sopenharmony_ci break; 7588c2ecf20Sopenharmony_ci } 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ci data->als_contr &= ~info->als_gain_mask; 7618c2ecf20Sopenharmony_ci data->als_contr |= i << info->als_gain_shift; 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, LTR501_ALS_CONTR, 7648c2ecf20Sopenharmony_ci data->als_contr); 7658c2ecf20Sopenharmony_ci break; 7668c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 7678c2ecf20Sopenharmony_ci i = ltr501_get_gain_index(info->ps_gain, 7688c2ecf20Sopenharmony_ci info->ps_gain_tbl_size, 7698c2ecf20Sopenharmony_ci val, val2); 7708c2ecf20Sopenharmony_ci if (i < 0) { 7718c2ecf20Sopenharmony_ci ret = -EINVAL; 7728c2ecf20Sopenharmony_ci break; 7738c2ecf20Sopenharmony_ci } 7748c2ecf20Sopenharmony_ci data->ps_contr &= ~LTR501_CONTR_PS_GAIN_MASK; 7758c2ecf20Sopenharmony_ci data->ps_contr |= i << LTR501_CONTR_PS_GAIN_SHIFT; 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, LTR501_PS_CONTR, 7788c2ecf20Sopenharmony_ci data->ps_contr); 7798c2ecf20Sopenharmony_ci break; 7808c2ecf20Sopenharmony_ci default: 7818c2ecf20Sopenharmony_ci ret = -EINVAL; 7828c2ecf20Sopenharmony_ci break; 7838c2ecf20Sopenharmony_ci } 7848c2ecf20Sopenharmony_ci break; 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_INT_TIME: 7878c2ecf20Sopenharmony_ci switch (chan->type) { 7888c2ecf20Sopenharmony_ci case IIO_INTENSITY: 7898c2ecf20Sopenharmony_ci if (val != 0) { 7908c2ecf20Sopenharmony_ci ret = -EINVAL; 7918c2ecf20Sopenharmony_ci break; 7928c2ecf20Sopenharmony_ci } 7938c2ecf20Sopenharmony_ci mutex_lock(&data->lock_als); 7948c2ecf20Sopenharmony_ci ret = ltr501_set_it_time(data, val2); 7958c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_als); 7968c2ecf20Sopenharmony_ci break; 7978c2ecf20Sopenharmony_ci default: 7988c2ecf20Sopenharmony_ci ret = -EINVAL; 7998c2ecf20Sopenharmony_ci break; 8008c2ecf20Sopenharmony_ci } 8018c2ecf20Sopenharmony_ci break; 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SAMP_FREQ: 8048c2ecf20Sopenharmony_ci switch (chan->type) { 8058c2ecf20Sopenharmony_ci case IIO_INTENSITY: 8068c2ecf20Sopenharmony_ci ret = ltr501_als_read_samp_freq(data, &freq_val, 8078c2ecf20Sopenharmony_ci &freq_val2); 8088c2ecf20Sopenharmony_ci if (ret < 0) 8098c2ecf20Sopenharmony_ci break; 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci ret = ltr501_als_write_samp_freq(data, val, val2); 8128c2ecf20Sopenharmony_ci if (ret < 0) 8138c2ecf20Sopenharmony_ci break; 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci /* update persistence count when changing frequency */ 8168c2ecf20Sopenharmony_ci ret = ltr501_write_intr_prst(data, chan->type, 8178c2ecf20Sopenharmony_ci 0, data->als_period); 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci if (ret < 0) 8208c2ecf20Sopenharmony_ci ret = ltr501_als_write_samp_freq(data, freq_val, 8218c2ecf20Sopenharmony_ci freq_val2); 8228c2ecf20Sopenharmony_ci break; 8238c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 8248c2ecf20Sopenharmony_ci ret = ltr501_ps_read_samp_freq(data, &freq_val, 8258c2ecf20Sopenharmony_ci &freq_val2); 8268c2ecf20Sopenharmony_ci if (ret < 0) 8278c2ecf20Sopenharmony_ci break; 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_ci ret = ltr501_ps_write_samp_freq(data, val, val2); 8308c2ecf20Sopenharmony_ci if (ret < 0) 8318c2ecf20Sopenharmony_ci break; 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_ci /* update persistence count when changing frequency */ 8348c2ecf20Sopenharmony_ci ret = ltr501_write_intr_prst(data, chan->type, 8358c2ecf20Sopenharmony_ci 0, data->ps_period); 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci if (ret < 0) 8388c2ecf20Sopenharmony_ci ret = ltr501_ps_write_samp_freq(data, freq_val, 8398c2ecf20Sopenharmony_ci freq_val2); 8408c2ecf20Sopenharmony_ci break; 8418c2ecf20Sopenharmony_ci default: 8428c2ecf20Sopenharmony_ci ret = -EINVAL; 8438c2ecf20Sopenharmony_ci break; 8448c2ecf20Sopenharmony_ci } 8458c2ecf20Sopenharmony_ci break; 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci default: 8488c2ecf20Sopenharmony_ci ret = -EINVAL; 8498c2ecf20Sopenharmony_ci break; 8508c2ecf20Sopenharmony_ci } 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_ci iio_device_release_direct_mode(indio_dev); 8538c2ecf20Sopenharmony_ci return ret; 8548c2ecf20Sopenharmony_ci} 8558c2ecf20Sopenharmony_ci 8568c2ecf20Sopenharmony_cistatic int ltr501_read_thresh(const struct iio_dev *indio_dev, 8578c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 8588c2ecf20Sopenharmony_ci enum iio_event_type type, 8598c2ecf20Sopenharmony_ci enum iio_event_direction dir, 8608c2ecf20Sopenharmony_ci enum iio_event_info info, 8618c2ecf20Sopenharmony_ci int *val, int *val2) 8628c2ecf20Sopenharmony_ci{ 8638c2ecf20Sopenharmony_ci const struct ltr501_data *data = iio_priv(indio_dev); 8648c2ecf20Sopenharmony_ci int ret, thresh_data; 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci switch (chan->type) { 8678c2ecf20Sopenharmony_ci case IIO_INTENSITY: 8688c2ecf20Sopenharmony_ci switch (dir) { 8698c2ecf20Sopenharmony_ci case IIO_EV_DIR_RISING: 8708c2ecf20Sopenharmony_ci ret = regmap_bulk_read(data->regmap, 8718c2ecf20Sopenharmony_ci LTR501_ALS_THRESH_UP, 8728c2ecf20Sopenharmony_ci &thresh_data, 2); 8738c2ecf20Sopenharmony_ci if (ret < 0) 8748c2ecf20Sopenharmony_ci return ret; 8758c2ecf20Sopenharmony_ci *val = thresh_data & LTR501_ALS_THRESH_MASK; 8768c2ecf20Sopenharmony_ci return IIO_VAL_INT; 8778c2ecf20Sopenharmony_ci case IIO_EV_DIR_FALLING: 8788c2ecf20Sopenharmony_ci ret = regmap_bulk_read(data->regmap, 8798c2ecf20Sopenharmony_ci LTR501_ALS_THRESH_LOW, 8808c2ecf20Sopenharmony_ci &thresh_data, 2); 8818c2ecf20Sopenharmony_ci if (ret < 0) 8828c2ecf20Sopenharmony_ci return ret; 8838c2ecf20Sopenharmony_ci *val = thresh_data & LTR501_ALS_THRESH_MASK; 8848c2ecf20Sopenharmony_ci return IIO_VAL_INT; 8858c2ecf20Sopenharmony_ci default: 8868c2ecf20Sopenharmony_ci return -EINVAL; 8878c2ecf20Sopenharmony_ci } 8888c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 8898c2ecf20Sopenharmony_ci switch (dir) { 8908c2ecf20Sopenharmony_ci case IIO_EV_DIR_RISING: 8918c2ecf20Sopenharmony_ci ret = regmap_bulk_read(data->regmap, 8928c2ecf20Sopenharmony_ci LTR501_PS_THRESH_UP, 8938c2ecf20Sopenharmony_ci &thresh_data, 2); 8948c2ecf20Sopenharmony_ci if (ret < 0) 8958c2ecf20Sopenharmony_ci return ret; 8968c2ecf20Sopenharmony_ci *val = thresh_data & LTR501_PS_THRESH_MASK; 8978c2ecf20Sopenharmony_ci return IIO_VAL_INT; 8988c2ecf20Sopenharmony_ci case IIO_EV_DIR_FALLING: 8998c2ecf20Sopenharmony_ci ret = regmap_bulk_read(data->regmap, 9008c2ecf20Sopenharmony_ci LTR501_PS_THRESH_LOW, 9018c2ecf20Sopenharmony_ci &thresh_data, 2); 9028c2ecf20Sopenharmony_ci if (ret < 0) 9038c2ecf20Sopenharmony_ci return ret; 9048c2ecf20Sopenharmony_ci *val = thresh_data & LTR501_PS_THRESH_MASK; 9058c2ecf20Sopenharmony_ci return IIO_VAL_INT; 9068c2ecf20Sopenharmony_ci default: 9078c2ecf20Sopenharmony_ci return -EINVAL; 9088c2ecf20Sopenharmony_ci } 9098c2ecf20Sopenharmony_ci default: 9108c2ecf20Sopenharmony_ci return -EINVAL; 9118c2ecf20Sopenharmony_ci } 9128c2ecf20Sopenharmony_ci 9138c2ecf20Sopenharmony_ci return -EINVAL; 9148c2ecf20Sopenharmony_ci} 9158c2ecf20Sopenharmony_ci 9168c2ecf20Sopenharmony_cistatic int ltr501_write_thresh(struct iio_dev *indio_dev, 9178c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 9188c2ecf20Sopenharmony_ci enum iio_event_type type, 9198c2ecf20Sopenharmony_ci enum iio_event_direction dir, 9208c2ecf20Sopenharmony_ci enum iio_event_info info, 9218c2ecf20Sopenharmony_ci int val, int val2) 9228c2ecf20Sopenharmony_ci{ 9238c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(indio_dev); 9248c2ecf20Sopenharmony_ci int ret; 9258c2ecf20Sopenharmony_ci 9268c2ecf20Sopenharmony_ci if (val < 0) 9278c2ecf20Sopenharmony_ci return -EINVAL; 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_ci switch (chan->type) { 9308c2ecf20Sopenharmony_ci case IIO_INTENSITY: 9318c2ecf20Sopenharmony_ci if (val > LTR501_ALS_THRESH_MASK) 9328c2ecf20Sopenharmony_ci return -EINVAL; 9338c2ecf20Sopenharmony_ci switch (dir) { 9348c2ecf20Sopenharmony_ci case IIO_EV_DIR_RISING: 9358c2ecf20Sopenharmony_ci mutex_lock(&data->lock_als); 9368c2ecf20Sopenharmony_ci ret = regmap_bulk_write(data->regmap, 9378c2ecf20Sopenharmony_ci LTR501_ALS_THRESH_UP, 9388c2ecf20Sopenharmony_ci &val, 2); 9398c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_als); 9408c2ecf20Sopenharmony_ci return ret; 9418c2ecf20Sopenharmony_ci case IIO_EV_DIR_FALLING: 9428c2ecf20Sopenharmony_ci mutex_lock(&data->lock_als); 9438c2ecf20Sopenharmony_ci ret = regmap_bulk_write(data->regmap, 9448c2ecf20Sopenharmony_ci LTR501_ALS_THRESH_LOW, 9458c2ecf20Sopenharmony_ci &val, 2); 9468c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_als); 9478c2ecf20Sopenharmony_ci return ret; 9488c2ecf20Sopenharmony_ci default: 9498c2ecf20Sopenharmony_ci return -EINVAL; 9508c2ecf20Sopenharmony_ci } 9518c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 9528c2ecf20Sopenharmony_ci if (val > LTR501_PS_THRESH_MASK) 9538c2ecf20Sopenharmony_ci return -EINVAL; 9548c2ecf20Sopenharmony_ci switch (dir) { 9558c2ecf20Sopenharmony_ci case IIO_EV_DIR_RISING: 9568c2ecf20Sopenharmony_ci mutex_lock(&data->lock_ps); 9578c2ecf20Sopenharmony_ci ret = regmap_bulk_write(data->regmap, 9588c2ecf20Sopenharmony_ci LTR501_PS_THRESH_UP, 9598c2ecf20Sopenharmony_ci &val, 2); 9608c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_ps); 9618c2ecf20Sopenharmony_ci return ret; 9628c2ecf20Sopenharmony_ci case IIO_EV_DIR_FALLING: 9638c2ecf20Sopenharmony_ci mutex_lock(&data->lock_ps); 9648c2ecf20Sopenharmony_ci ret = regmap_bulk_write(data->regmap, 9658c2ecf20Sopenharmony_ci LTR501_PS_THRESH_LOW, 9668c2ecf20Sopenharmony_ci &val, 2); 9678c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_ps); 9688c2ecf20Sopenharmony_ci return ret; 9698c2ecf20Sopenharmony_ci default: 9708c2ecf20Sopenharmony_ci return -EINVAL; 9718c2ecf20Sopenharmony_ci } 9728c2ecf20Sopenharmony_ci default: 9738c2ecf20Sopenharmony_ci return -EINVAL; 9748c2ecf20Sopenharmony_ci } 9758c2ecf20Sopenharmony_ci 9768c2ecf20Sopenharmony_ci return -EINVAL; 9778c2ecf20Sopenharmony_ci} 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_cistatic int ltr501_read_event(struct iio_dev *indio_dev, 9808c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 9818c2ecf20Sopenharmony_ci enum iio_event_type type, 9828c2ecf20Sopenharmony_ci enum iio_event_direction dir, 9838c2ecf20Sopenharmony_ci enum iio_event_info info, 9848c2ecf20Sopenharmony_ci int *val, int *val2) 9858c2ecf20Sopenharmony_ci{ 9868c2ecf20Sopenharmony_ci int ret; 9878c2ecf20Sopenharmony_ci 9888c2ecf20Sopenharmony_ci switch (info) { 9898c2ecf20Sopenharmony_ci case IIO_EV_INFO_VALUE: 9908c2ecf20Sopenharmony_ci return ltr501_read_thresh(indio_dev, chan, type, dir, 9918c2ecf20Sopenharmony_ci info, val, val2); 9928c2ecf20Sopenharmony_ci case IIO_EV_INFO_PERIOD: 9938c2ecf20Sopenharmony_ci ret = ltr501_read_intr_prst(iio_priv(indio_dev), 9948c2ecf20Sopenharmony_ci chan->type, val2); 9958c2ecf20Sopenharmony_ci *val = *val2 / 1000000; 9968c2ecf20Sopenharmony_ci *val2 = *val2 % 1000000; 9978c2ecf20Sopenharmony_ci return ret; 9988c2ecf20Sopenharmony_ci default: 9998c2ecf20Sopenharmony_ci return -EINVAL; 10008c2ecf20Sopenharmony_ci } 10018c2ecf20Sopenharmony_ci 10028c2ecf20Sopenharmony_ci return -EINVAL; 10038c2ecf20Sopenharmony_ci} 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_cistatic int ltr501_write_event(struct iio_dev *indio_dev, 10068c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 10078c2ecf20Sopenharmony_ci enum iio_event_type type, 10088c2ecf20Sopenharmony_ci enum iio_event_direction dir, 10098c2ecf20Sopenharmony_ci enum iio_event_info info, 10108c2ecf20Sopenharmony_ci int val, int val2) 10118c2ecf20Sopenharmony_ci{ 10128c2ecf20Sopenharmony_ci switch (info) { 10138c2ecf20Sopenharmony_ci case IIO_EV_INFO_VALUE: 10148c2ecf20Sopenharmony_ci if (val2 != 0) 10158c2ecf20Sopenharmony_ci return -EINVAL; 10168c2ecf20Sopenharmony_ci return ltr501_write_thresh(indio_dev, chan, type, dir, 10178c2ecf20Sopenharmony_ci info, val, val2); 10188c2ecf20Sopenharmony_ci case IIO_EV_INFO_PERIOD: 10198c2ecf20Sopenharmony_ci return ltr501_write_intr_prst(iio_priv(indio_dev), chan->type, 10208c2ecf20Sopenharmony_ci val, val2); 10218c2ecf20Sopenharmony_ci default: 10228c2ecf20Sopenharmony_ci return -EINVAL; 10238c2ecf20Sopenharmony_ci } 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_ci return -EINVAL; 10268c2ecf20Sopenharmony_ci} 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_cistatic int ltr501_read_event_config(struct iio_dev *indio_dev, 10298c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 10308c2ecf20Sopenharmony_ci enum iio_event_type type, 10318c2ecf20Sopenharmony_ci enum iio_event_direction dir) 10328c2ecf20Sopenharmony_ci{ 10338c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(indio_dev); 10348c2ecf20Sopenharmony_ci int ret, status; 10358c2ecf20Sopenharmony_ci 10368c2ecf20Sopenharmony_ci switch (chan->type) { 10378c2ecf20Sopenharmony_ci case IIO_INTENSITY: 10388c2ecf20Sopenharmony_ci ret = regmap_field_read(data->reg_als_intr, &status); 10398c2ecf20Sopenharmony_ci if (ret < 0) 10408c2ecf20Sopenharmony_ci return ret; 10418c2ecf20Sopenharmony_ci return status; 10428c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 10438c2ecf20Sopenharmony_ci ret = regmap_field_read(data->reg_ps_intr, &status); 10448c2ecf20Sopenharmony_ci if (ret < 0) 10458c2ecf20Sopenharmony_ci return ret; 10468c2ecf20Sopenharmony_ci return status; 10478c2ecf20Sopenharmony_ci default: 10488c2ecf20Sopenharmony_ci return -EINVAL; 10498c2ecf20Sopenharmony_ci } 10508c2ecf20Sopenharmony_ci 10518c2ecf20Sopenharmony_ci return -EINVAL; 10528c2ecf20Sopenharmony_ci} 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_cistatic int ltr501_write_event_config(struct iio_dev *indio_dev, 10558c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 10568c2ecf20Sopenharmony_ci enum iio_event_type type, 10578c2ecf20Sopenharmony_ci enum iio_event_direction dir, int state) 10588c2ecf20Sopenharmony_ci{ 10598c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(indio_dev); 10608c2ecf20Sopenharmony_ci int ret; 10618c2ecf20Sopenharmony_ci 10628c2ecf20Sopenharmony_ci /* only 1 and 0 are valid inputs */ 10638c2ecf20Sopenharmony_ci if (state != 1 && state != 0) 10648c2ecf20Sopenharmony_ci return -EINVAL; 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci switch (chan->type) { 10678c2ecf20Sopenharmony_ci case IIO_INTENSITY: 10688c2ecf20Sopenharmony_ci mutex_lock(&data->lock_als); 10698c2ecf20Sopenharmony_ci ret = regmap_field_write(data->reg_als_intr, state); 10708c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_als); 10718c2ecf20Sopenharmony_ci return ret; 10728c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 10738c2ecf20Sopenharmony_ci mutex_lock(&data->lock_ps); 10748c2ecf20Sopenharmony_ci ret = regmap_field_write(data->reg_ps_intr, state); 10758c2ecf20Sopenharmony_ci mutex_unlock(&data->lock_ps); 10768c2ecf20Sopenharmony_ci return ret; 10778c2ecf20Sopenharmony_ci default: 10788c2ecf20Sopenharmony_ci return -EINVAL; 10798c2ecf20Sopenharmony_ci } 10808c2ecf20Sopenharmony_ci 10818c2ecf20Sopenharmony_ci return -EINVAL; 10828c2ecf20Sopenharmony_ci} 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_cistatic ssize_t ltr501_show_proximity_scale_avail(struct device *dev, 10858c2ecf20Sopenharmony_ci struct device_attribute *attr, 10868c2ecf20Sopenharmony_ci char *buf) 10878c2ecf20Sopenharmony_ci{ 10888c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(dev_to_iio_dev(dev)); 10898c2ecf20Sopenharmony_ci struct ltr501_chip_info *info = data->chip_info; 10908c2ecf20Sopenharmony_ci ssize_t len = 0; 10918c2ecf20Sopenharmony_ci int i; 10928c2ecf20Sopenharmony_ci 10938c2ecf20Sopenharmony_ci for (i = 0; i < info->ps_gain_tbl_size; i++) { 10948c2ecf20Sopenharmony_ci if (info->ps_gain[i].scale == LTR501_RESERVED_GAIN) 10958c2ecf20Sopenharmony_ci continue; 10968c2ecf20Sopenharmony_ci len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", 10978c2ecf20Sopenharmony_ci info->ps_gain[i].scale, 10988c2ecf20Sopenharmony_ci info->ps_gain[i].uscale); 10998c2ecf20Sopenharmony_ci } 11008c2ecf20Sopenharmony_ci 11018c2ecf20Sopenharmony_ci buf[len - 1] = '\n'; 11028c2ecf20Sopenharmony_ci 11038c2ecf20Sopenharmony_ci return len; 11048c2ecf20Sopenharmony_ci} 11058c2ecf20Sopenharmony_ci 11068c2ecf20Sopenharmony_cistatic ssize_t ltr501_show_intensity_scale_avail(struct device *dev, 11078c2ecf20Sopenharmony_ci struct device_attribute *attr, 11088c2ecf20Sopenharmony_ci char *buf) 11098c2ecf20Sopenharmony_ci{ 11108c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(dev_to_iio_dev(dev)); 11118c2ecf20Sopenharmony_ci struct ltr501_chip_info *info = data->chip_info; 11128c2ecf20Sopenharmony_ci ssize_t len = 0; 11138c2ecf20Sopenharmony_ci int i; 11148c2ecf20Sopenharmony_ci 11158c2ecf20Sopenharmony_ci for (i = 0; i < info->als_gain_tbl_size; i++) { 11168c2ecf20Sopenharmony_ci if (info->als_gain[i].scale == LTR501_RESERVED_GAIN) 11178c2ecf20Sopenharmony_ci continue; 11188c2ecf20Sopenharmony_ci len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06d ", 11198c2ecf20Sopenharmony_ci info->als_gain[i].scale, 11208c2ecf20Sopenharmony_ci info->als_gain[i].uscale); 11218c2ecf20Sopenharmony_ci } 11228c2ecf20Sopenharmony_ci 11238c2ecf20Sopenharmony_ci buf[len - 1] = '\n'; 11248c2ecf20Sopenharmony_ci 11258c2ecf20Sopenharmony_ci return len; 11268c2ecf20Sopenharmony_ci} 11278c2ecf20Sopenharmony_ci 11288c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR_INT_TIME_AVAIL("0.05 0.1 0.2 0.4"); 11298c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR_SAMP_FREQ_AVAIL("20 10 5 2 1 0.5"); 11308c2ecf20Sopenharmony_ci 11318c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(in_proximity_scale_available, S_IRUGO, 11328c2ecf20Sopenharmony_ci ltr501_show_proximity_scale_avail, NULL, 0); 11338c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(in_intensity_scale_available, S_IRUGO, 11348c2ecf20Sopenharmony_ci ltr501_show_intensity_scale_avail, NULL, 0); 11358c2ecf20Sopenharmony_ci 11368c2ecf20Sopenharmony_cistatic struct attribute *ltr501_attributes[] = { 11378c2ecf20Sopenharmony_ci &iio_dev_attr_in_proximity_scale_available.dev_attr.attr, 11388c2ecf20Sopenharmony_ci &iio_dev_attr_in_intensity_scale_available.dev_attr.attr, 11398c2ecf20Sopenharmony_ci &iio_const_attr_integration_time_available.dev_attr.attr, 11408c2ecf20Sopenharmony_ci &iio_const_attr_sampling_frequency_available.dev_attr.attr, 11418c2ecf20Sopenharmony_ci NULL 11428c2ecf20Sopenharmony_ci}; 11438c2ecf20Sopenharmony_ci 11448c2ecf20Sopenharmony_cistatic struct attribute *ltr301_attributes[] = { 11458c2ecf20Sopenharmony_ci &iio_dev_attr_in_intensity_scale_available.dev_attr.attr, 11468c2ecf20Sopenharmony_ci &iio_const_attr_integration_time_available.dev_attr.attr, 11478c2ecf20Sopenharmony_ci &iio_const_attr_sampling_frequency_available.dev_attr.attr, 11488c2ecf20Sopenharmony_ci NULL 11498c2ecf20Sopenharmony_ci}; 11508c2ecf20Sopenharmony_ci 11518c2ecf20Sopenharmony_cistatic const struct attribute_group ltr501_attribute_group = { 11528c2ecf20Sopenharmony_ci .attrs = ltr501_attributes, 11538c2ecf20Sopenharmony_ci}; 11548c2ecf20Sopenharmony_ci 11558c2ecf20Sopenharmony_cistatic const struct attribute_group ltr301_attribute_group = { 11568c2ecf20Sopenharmony_ci .attrs = ltr301_attributes, 11578c2ecf20Sopenharmony_ci}; 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_cistatic const struct iio_info ltr501_info_no_irq = { 11608c2ecf20Sopenharmony_ci .read_raw = ltr501_read_raw, 11618c2ecf20Sopenharmony_ci .write_raw = ltr501_write_raw, 11628c2ecf20Sopenharmony_ci .attrs = <r501_attribute_group, 11638c2ecf20Sopenharmony_ci}; 11648c2ecf20Sopenharmony_ci 11658c2ecf20Sopenharmony_cistatic const struct iio_info ltr501_info = { 11668c2ecf20Sopenharmony_ci .read_raw = ltr501_read_raw, 11678c2ecf20Sopenharmony_ci .write_raw = ltr501_write_raw, 11688c2ecf20Sopenharmony_ci .attrs = <r501_attribute_group, 11698c2ecf20Sopenharmony_ci .read_event_value = <r501_read_event, 11708c2ecf20Sopenharmony_ci .write_event_value = <r501_write_event, 11718c2ecf20Sopenharmony_ci .read_event_config = <r501_read_event_config, 11728c2ecf20Sopenharmony_ci .write_event_config = <r501_write_event_config, 11738c2ecf20Sopenharmony_ci}; 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_cistatic const struct iio_info ltr301_info_no_irq = { 11768c2ecf20Sopenharmony_ci .read_raw = ltr501_read_raw, 11778c2ecf20Sopenharmony_ci .write_raw = ltr501_write_raw, 11788c2ecf20Sopenharmony_ci .attrs = <r301_attribute_group, 11798c2ecf20Sopenharmony_ci}; 11808c2ecf20Sopenharmony_ci 11818c2ecf20Sopenharmony_cistatic const struct iio_info ltr301_info = { 11828c2ecf20Sopenharmony_ci .read_raw = ltr501_read_raw, 11838c2ecf20Sopenharmony_ci .write_raw = ltr501_write_raw, 11848c2ecf20Sopenharmony_ci .attrs = <r301_attribute_group, 11858c2ecf20Sopenharmony_ci .read_event_value = <r501_read_event, 11868c2ecf20Sopenharmony_ci .write_event_value = <r501_write_event, 11878c2ecf20Sopenharmony_ci .read_event_config = <r501_read_event_config, 11888c2ecf20Sopenharmony_ci .write_event_config = <r501_write_event_config, 11898c2ecf20Sopenharmony_ci}; 11908c2ecf20Sopenharmony_ci 11918c2ecf20Sopenharmony_cistatic struct ltr501_chip_info ltr501_chip_info_tbl[] = { 11928c2ecf20Sopenharmony_ci [ltr501] = { 11938c2ecf20Sopenharmony_ci .partid = 0x08, 11948c2ecf20Sopenharmony_ci .als_gain = ltr501_als_gain_tbl, 11958c2ecf20Sopenharmony_ci .als_gain_tbl_size = ARRAY_SIZE(ltr501_als_gain_tbl), 11968c2ecf20Sopenharmony_ci .ps_gain = ltr501_ps_gain_tbl, 11978c2ecf20Sopenharmony_ci .ps_gain_tbl_size = ARRAY_SIZE(ltr501_ps_gain_tbl), 11988c2ecf20Sopenharmony_ci .als_mode_active = BIT(0) | BIT(1), 11998c2ecf20Sopenharmony_ci .als_gain_mask = BIT(3), 12008c2ecf20Sopenharmony_ci .als_gain_shift = 3, 12018c2ecf20Sopenharmony_ci .info = <r501_info, 12028c2ecf20Sopenharmony_ci .info_no_irq = <r501_info_no_irq, 12038c2ecf20Sopenharmony_ci .channels = ltr501_channels, 12048c2ecf20Sopenharmony_ci .no_channels = ARRAY_SIZE(ltr501_channels), 12058c2ecf20Sopenharmony_ci }, 12068c2ecf20Sopenharmony_ci [ltr559] = { 12078c2ecf20Sopenharmony_ci .partid = 0x09, 12088c2ecf20Sopenharmony_ci .als_gain = ltr559_als_gain_tbl, 12098c2ecf20Sopenharmony_ci .als_gain_tbl_size = ARRAY_SIZE(ltr559_als_gain_tbl), 12108c2ecf20Sopenharmony_ci .ps_gain = ltr559_ps_gain_tbl, 12118c2ecf20Sopenharmony_ci .ps_gain_tbl_size = ARRAY_SIZE(ltr559_ps_gain_tbl), 12128c2ecf20Sopenharmony_ci .als_mode_active = BIT(0), 12138c2ecf20Sopenharmony_ci .als_gain_mask = BIT(2) | BIT(3) | BIT(4), 12148c2ecf20Sopenharmony_ci .als_gain_shift = 2, 12158c2ecf20Sopenharmony_ci .info = <r501_info, 12168c2ecf20Sopenharmony_ci .info_no_irq = <r501_info_no_irq, 12178c2ecf20Sopenharmony_ci .channels = ltr501_channels, 12188c2ecf20Sopenharmony_ci .no_channels = ARRAY_SIZE(ltr501_channels), 12198c2ecf20Sopenharmony_ci }, 12208c2ecf20Sopenharmony_ci [ltr301] = { 12218c2ecf20Sopenharmony_ci .partid = 0x08, 12228c2ecf20Sopenharmony_ci .als_gain = ltr501_als_gain_tbl, 12238c2ecf20Sopenharmony_ci .als_gain_tbl_size = ARRAY_SIZE(ltr501_als_gain_tbl), 12248c2ecf20Sopenharmony_ci .als_mode_active = BIT(0) | BIT(1), 12258c2ecf20Sopenharmony_ci .als_gain_mask = BIT(3), 12268c2ecf20Sopenharmony_ci .als_gain_shift = 3, 12278c2ecf20Sopenharmony_ci .info = <r301_info, 12288c2ecf20Sopenharmony_ci .info_no_irq = <r301_info_no_irq, 12298c2ecf20Sopenharmony_ci .channels = ltr301_channels, 12308c2ecf20Sopenharmony_ci .no_channels = ARRAY_SIZE(ltr301_channels), 12318c2ecf20Sopenharmony_ci }, 12328c2ecf20Sopenharmony_ci}; 12338c2ecf20Sopenharmony_ci 12348c2ecf20Sopenharmony_cistatic int ltr501_write_contr(struct ltr501_data *data, u8 als_val, u8 ps_val) 12358c2ecf20Sopenharmony_ci{ 12368c2ecf20Sopenharmony_ci int ret; 12378c2ecf20Sopenharmony_ci 12388c2ecf20Sopenharmony_ci ret = regmap_write(data->regmap, LTR501_ALS_CONTR, als_val); 12398c2ecf20Sopenharmony_ci if (ret < 0) 12408c2ecf20Sopenharmony_ci return ret; 12418c2ecf20Sopenharmony_ci 12428c2ecf20Sopenharmony_ci return regmap_write(data->regmap, LTR501_PS_CONTR, ps_val); 12438c2ecf20Sopenharmony_ci} 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_cistatic irqreturn_t ltr501_trigger_handler(int irq, void *p) 12468c2ecf20Sopenharmony_ci{ 12478c2ecf20Sopenharmony_ci struct iio_poll_func *pf = p; 12488c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = pf->indio_dev; 12498c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(indio_dev); 12508c2ecf20Sopenharmony_ci struct { 12518c2ecf20Sopenharmony_ci u16 channels[3]; 12528c2ecf20Sopenharmony_ci s64 ts __aligned(8); 12538c2ecf20Sopenharmony_ci } scan; 12548c2ecf20Sopenharmony_ci __le16 als_buf[2]; 12558c2ecf20Sopenharmony_ci u8 mask = 0; 12568c2ecf20Sopenharmony_ci int j = 0; 12578c2ecf20Sopenharmony_ci int ret, psdata; 12588c2ecf20Sopenharmony_ci 12598c2ecf20Sopenharmony_ci memset(&scan, 0, sizeof(scan)); 12608c2ecf20Sopenharmony_ci 12618c2ecf20Sopenharmony_ci /* figure out which data needs to be ready */ 12628c2ecf20Sopenharmony_ci if (test_bit(0, indio_dev->active_scan_mask) || 12638c2ecf20Sopenharmony_ci test_bit(1, indio_dev->active_scan_mask)) 12648c2ecf20Sopenharmony_ci mask |= LTR501_STATUS_ALS_RDY; 12658c2ecf20Sopenharmony_ci if (test_bit(2, indio_dev->active_scan_mask)) 12668c2ecf20Sopenharmony_ci mask |= LTR501_STATUS_PS_RDY; 12678c2ecf20Sopenharmony_ci 12688c2ecf20Sopenharmony_ci ret = ltr501_drdy(data, mask); 12698c2ecf20Sopenharmony_ci if (ret < 0) 12708c2ecf20Sopenharmony_ci goto done; 12718c2ecf20Sopenharmony_ci 12728c2ecf20Sopenharmony_ci if (mask & LTR501_STATUS_ALS_RDY) { 12738c2ecf20Sopenharmony_ci ret = regmap_bulk_read(data->regmap, LTR501_ALS_DATA1, 12748c2ecf20Sopenharmony_ci als_buf, sizeof(als_buf)); 12758c2ecf20Sopenharmony_ci if (ret < 0) 12768c2ecf20Sopenharmony_ci goto done; 12778c2ecf20Sopenharmony_ci if (test_bit(0, indio_dev->active_scan_mask)) 12788c2ecf20Sopenharmony_ci scan.channels[j++] = le16_to_cpu(als_buf[1]); 12798c2ecf20Sopenharmony_ci if (test_bit(1, indio_dev->active_scan_mask)) 12808c2ecf20Sopenharmony_ci scan.channels[j++] = le16_to_cpu(als_buf[0]); 12818c2ecf20Sopenharmony_ci } 12828c2ecf20Sopenharmony_ci 12838c2ecf20Sopenharmony_ci if (mask & LTR501_STATUS_PS_RDY) { 12848c2ecf20Sopenharmony_ci ret = regmap_bulk_read(data->regmap, LTR501_PS_DATA, 12858c2ecf20Sopenharmony_ci &psdata, 2); 12868c2ecf20Sopenharmony_ci if (ret < 0) 12878c2ecf20Sopenharmony_ci goto done; 12888c2ecf20Sopenharmony_ci scan.channels[j++] = psdata & LTR501_PS_DATA_MASK; 12898c2ecf20Sopenharmony_ci } 12908c2ecf20Sopenharmony_ci 12918c2ecf20Sopenharmony_ci iio_push_to_buffers_with_timestamp(indio_dev, &scan, 12928c2ecf20Sopenharmony_ci iio_get_time_ns(indio_dev)); 12938c2ecf20Sopenharmony_ci 12948c2ecf20Sopenharmony_cidone: 12958c2ecf20Sopenharmony_ci iio_trigger_notify_done(indio_dev->trig); 12968c2ecf20Sopenharmony_ci 12978c2ecf20Sopenharmony_ci return IRQ_HANDLED; 12988c2ecf20Sopenharmony_ci} 12998c2ecf20Sopenharmony_ci 13008c2ecf20Sopenharmony_cistatic irqreturn_t ltr501_interrupt_handler(int irq, void *private) 13018c2ecf20Sopenharmony_ci{ 13028c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = private; 13038c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(indio_dev); 13048c2ecf20Sopenharmony_ci int ret, status; 13058c2ecf20Sopenharmony_ci 13068c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, LTR501_ALS_PS_STATUS, &status); 13078c2ecf20Sopenharmony_ci if (ret < 0) { 13088c2ecf20Sopenharmony_ci dev_err(&data->client->dev, 13098c2ecf20Sopenharmony_ci "irq read int reg failed\n"); 13108c2ecf20Sopenharmony_ci return IRQ_HANDLED; 13118c2ecf20Sopenharmony_ci } 13128c2ecf20Sopenharmony_ci 13138c2ecf20Sopenharmony_ci if (status & LTR501_STATUS_ALS_INTR) 13148c2ecf20Sopenharmony_ci iio_push_event(indio_dev, 13158c2ecf20Sopenharmony_ci IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0, 13168c2ecf20Sopenharmony_ci IIO_EV_TYPE_THRESH, 13178c2ecf20Sopenharmony_ci IIO_EV_DIR_EITHER), 13188c2ecf20Sopenharmony_ci iio_get_time_ns(indio_dev)); 13198c2ecf20Sopenharmony_ci 13208c2ecf20Sopenharmony_ci if (status & LTR501_STATUS_PS_INTR) 13218c2ecf20Sopenharmony_ci iio_push_event(indio_dev, 13228c2ecf20Sopenharmony_ci IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0, 13238c2ecf20Sopenharmony_ci IIO_EV_TYPE_THRESH, 13248c2ecf20Sopenharmony_ci IIO_EV_DIR_EITHER), 13258c2ecf20Sopenharmony_ci iio_get_time_ns(indio_dev)); 13268c2ecf20Sopenharmony_ci 13278c2ecf20Sopenharmony_ci return IRQ_HANDLED; 13288c2ecf20Sopenharmony_ci} 13298c2ecf20Sopenharmony_ci 13308c2ecf20Sopenharmony_cistatic int ltr501_init(struct ltr501_data *data) 13318c2ecf20Sopenharmony_ci{ 13328c2ecf20Sopenharmony_ci int ret, status; 13338c2ecf20Sopenharmony_ci 13348c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, LTR501_ALS_CONTR, &status); 13358c2ecf20Sopenharmony_ci if (ret < 0) 13368c2ecf20Sopenharmony_ci return ret; 13378c2ecf20Sopenharmony_ci 13388c2ecf20Sopenharmony_ci data->als_contr = status | data->chip_info->als_mode_active; 13398c2ecf20Sopenharmony_ci 13408c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, LTR501_PS_CONTR, &status); 13418c2ecf20Sopenharmony_ci if (ret < 0) 13428c2ecf20Sopenharmony_ci return ret; 13438c2ecf20Sopenharmony_ci 13448c2ecf20Sopenharmony_ci data->ps_contr = status | LTR501_CONTR_ACTIVE; 13458c2ecf20Sopenharmony_ci 13468c2ecf20Sopenharmony_ci ret = ltr501_read_intr_prst(data, IIO_INTENSITY, &data->als_period); 13478c2ecf20Sopenharmony_ci if (ret < 0) 13488c2ecf20Sopenharmony_ci return ret; 13498c2ecf20Sopenharmony_ci 13508c2ecf20Sopenharmony_ci ret = ltr501_read_intr_prst(data, IIO_PROXIMITY, &data->ps_period); 13518c2ecf20Sopenharmony_ci if (ret < 0) 13528c2ecf20Sopenharmony_ci return ret; 13538c2ecf20Sopenharmony_ci 13548c2ecf20Sopenharmony_ci return ltr501_write_contr(data, data->als_contr, data->ps_contr); 13558c2ecf20Sopenharmony_ci} 13568c2ecf20Sopenharmony_ci 13578c2ecf20Sopenharmony_cistatic bool ltr501_is_volatile_reg(struct device *dev, unsigned int reg) 13588c2ecf20Sopenharmony_ci{ 13598c2ecf20Sopenharmony_ci switch (reg) { 13608c2ecf20Sopenharmony_ci case LTR501_ALS_DATA1: 13618c2ecf20Sopenharmony_ci case LTR501_ALS_DATA1_UPPER: 13628c2ecf20Sopenharmony_ci case LTR501_ALS_DATA0: 13638c2ecf20Sopenharmony_ci case LTR501_ALS_DATA0_UPPER: 13648c2ecf20Sopenharmony_ci case LTR501_ALS_PS_STATUS: 13658c2ecf20Sopenharmony_ci case LTR501_PS_DATA: 13668c2ecf20Sopenharmony_ci case LTR501_PS_DATA_UPPER: 13678c2ecf20Sopenharmony_ci return true; 13688c2ecf20Sopenharmony_ci default: 13698c2ecf20Sopenharmony_ci return false; 13708c2ecf20Sopenharmony_ci } 13718c2ecf20Sopenharmony_ci} 13728c2ecf20Sopenharmony_ci 13738c2ecf20Sopenharmony_cistatic const struct regmap_config ltr501_regmap_config = { 13748c2ecf20Sopenharmony_ci .name = LTR501_REGMAP_NAME, 13758c2ecf20Sopenharmony_ci .reg_bits = 8, 13768c2ecf20Sopenharmony_ci .val_bits = 8, 13778c2ecf20Sopenharmony_ci .max_register = LTR501_MAX_REG, 13788c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 13798c2ecf20Sopenharmony_ci .volatile_reg = ltr501_is_volatile_reg, 13808c2ecf20Sopenharmony_ci}; 13818c2ecf20Sopenharmony_ci 13828c2ecf20Sopenharmony_cistatic int ltr501_powerdown(struct ltr501_data *data) 13838c2ecf20Sopenharmony_ci{ 13848c2ecf20Sopenharmony_ci return ltr501_write_contr(data, data->als_contr & 13858c2ecf20Sopenharmony_ci ~data->chip_info->als_mode_active, 13868c2ecf20Sopenharmony_ci data->ps_contr & ~LTR501_CONTR_ACTIVE); 13878c2ecf20Sopenharmony_ci} 13888c2ecf20Sopenharmony_ci 13898c2ecf20Sopenharmony_cistatic const char *ltr501_match_acpi_device(struct device *dev, int *chip_idx) 13908c2ecf20Sopenharmony_ci{ 13918c2ecf20Sopenharmony_ci const struct acpi_device_id *id; 13928c2ecf20Sopenharmony_ci 13938c2ecf20Sopenharmony_ci id = acpi_match_device(dev->driver->acpi_match_table, dev); 13948c2ecf20Sopenharmony_ci if (!id) 13958c2ecf20Sopenharmony_ci return NULL; 13968c2ecf20Sopenharmony_ci *chip_idx = id->driver_data; 13978c2ecf20Sopenharmony_ci return dev_name(dev); 13988c2ecf20Sopenharmony_ci} 13998c2ecf20Sopenharmony_ci 14008c2ecf20Sopenharmony_cistatic int ltr501_probe(struct i2c_client *client, 14018c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 14028c2ecf20Sopenharmony_ci{ 14038c2ecf20Sopenharmony_ci struct ltr501_data *data; 14048c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 14058c2ecf20Sopenharmony_ci struct regmap *regmap; 14068c2ecf20Sopenharmony_ci int ret, partid, chip_idx = 0; 14078c2ecf20Sopenharmony_ci const char *name = NULL; 14088c2ecf20Sopenharmony_ci 14098c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 14108c2ecf20Sopenharmony_ci if (!indio_dev) 14118c2ecf20Sopenharmony_ci return -ENOMEM; 14128c2ecf20Sopenharmony_ci 14138c2ecf20Sopenharmony_ci regmap = devm_regmap_init_i2c(client, <r501_regmap_config); 14148c2ecf20Sopenharmony_ci if (IS_ERR(regmap)) { 14158c2ecf20Sopenharmony_ci dev_err(&client->dev, "Regmap initialization failed.\n"); 14168c2ecf20Sopenharmony_ci return PTR_ERR(regmap); 14178c2ecf20Sopenharmony_ci } 14188c2ecf20Sopenharmony_ci 14198c2ecf20Sopenharmony_ci data = iio_priv(indio_dev); 14208c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 14218c2ecf20Sopenharmony_ci data->client = client; 14228c2ecf20Sopenharmony_ci data->regmap = regmap; 14238c2ecf20Sopenharmony_ci mutex_init(&data->lock_als); 14248c2ecf20Sopenharmony_ci mutex_init(&data->lock_ps); 14258c2ecf20Sopenharmony_ci 14268c2ecf20Sopenharmony_ci data->reg_it = devm_regmap_field_alloc(&client->dev, regmap, 14278c2ecf20Sopenharmony_ci reg_field_it); 14288c2ecf20Sopenharmony_ci if (IS_ERR(data->reg_it)) { 14298c2ecf20Sopenharmony_ci dev_err(&client->dev, "Integ time reg field init failed.\n"); 14308c2ecf20Sopenharmony_ci return PTR_ERR(data->reg_it); 14318c2ecf20Sopenharmony_ci } 14328c2ecf20Sopenharmony_ci 14338c2ecf20Sopenharmony_ci data->reg_als_intr = devm_regmap_field_alloc(&client->dev, regmap, 14348c2ecf20Sopenharmony_ci reg_field_als_intr); 14358c2ecf20Sopenharmony_ci if (IS_ERR(data->reg_als_intr)) { 14368c2ecf20Sopenharmony_ci dev_err(&client->dev, "ALS intr mode reg field init failed\n"); 14378c2ecf20Sopenharmony_ci return PTR_ERR(data->reg_als_intr); 14388c2ecf20Sopenharmony_ci } 14398c2ecf20Sopenharmony_ci 14408c2ecf20Sopenharmony_ci data->reg_ps_intr = devm_regmap_field_alloc(&client->dev, regmap, 14418c2ecf20Sopenharmony_ci reg_field_ps_intr); 14428c2ecf20Sopenharmony_ci if (IS_ERR(data->reg_ps_intr)) { 14438c2ecf20Sopenharmony_ci dev_err(&client->dev, "PS intr mode reg field init failed.\n"); 14448c2ecf20Sopenharmony_ci return PTR_ERR(data->reg_ps_intr); 14458c2ecf20Sopenharmony_ci } 14468c2ecf20Sopenharmony_ci 14478c2ecf20Sopenharmony_ci data->reg_als_rate = devm_regmap_field_alloc(&client->dev, regmap, 14488c2ecf20Sopenharmony_ci reg_field_als_rate); 14498c2ecf20Sopenharmony_ci if (IS_ERR(data->reg_als_rate)) { 14508c2ecf20Sopenharmony_ci dev_err(&client->dev, "ALS samp rate field init failed.\n"); 14518c2ecf20Sopenharmony_ci return PTR_ERR(data->reg_als_rate); 14528c2ecf20Sopenharmony_ci } 14538c2ecf20Sopenharmony_ci 14548c2ecf20Sopenharmony_ci data->reg_ps_rate = devm_regmap_field_alloc(&client->dev, regmap, 14558c2ecf20Sopenharmony_ci reg_field_ps_rate); 14568c2ecf20Sopenharmony_ci if (IS_ERR(data->reg_ps_rate)) { 14578c2ecf20Sopenharmony_ci dev_err(&client->dev, "PS samp rate field init failed.\n"); 14588c2ecf20Sopenharmony_ci return PTR_ERR(data->reg_ps_rate); 14598c2ecf20Sopenharmony_ci } 14608c2ecf20Sopenharmony_ci 14618c2ecf20Sopenharmony_ci data->reg_als_prst = devm_regmap_field_alloc(&client->dev, regmap, 14628c2ecf20Sopenharmony_ci reg_field_als_prst); 14638c2ecf20Sopenharmony_ci if (IS_ERR(data->reg_als_prst)) { 14648c2ecf20Sopenharmony_ci dev_err(&client->dev, "ALS prst reg field init failed\n"); 14658c2ecf20Sopenharmony_ci return PTR_ERR(data->reg_als_prst); 14668c2ecf20Sopenharmony_ci } 14678c2ecf20Sopenharmony_ci 14688c2ecf20Sopenharmony_ci data->reg_ps_prst = devm_regmap_field_alloc(&client->dev, regmap, 14698c2ecf20Sopenharmony_ci reg_field_ps_prst); 14708c2ecf20Sopenharmony_ci if (IS_ERR(data->reg_ps_prst)) { 14718c2ecf20Sopenharmony_ci dev_err(&client->dev, "PS prst reg field init failed.\n"); 14728c2ecf20Sopenharmony_ci return PTR_ERR(data->reg_ps_prst); 14738c2ecf20Sopenharmony_ci } 14748c2ecf20Sopenharmony_ci 14758c2ecf20Sopenharmony_ci ret = regmap_read(data->regmap, LTR501_PART_ID, &partid); 14768c2ecf20Sopenharmony_ci if (ret < 0) 14778c2ecf20Sopenharmony_ci return ret; 14788c2ecf20Sopenharmony_ci 14798c2ecf20Sopenharmony_ci if (id) { 14808c2ecf20Sopenharmony_ci name = id->name; 14818c2ecf20Sopenharmony_ci chip_idx = id->driver_data; 14828c2ecf20Sopenharmony_ci } else if (ACPI_HANDLE(&client->dev)) { 14838c2ecf20Sopenharmony_ci name = ltr501_match_acpi_device(&client->dev, &chip_idx); 14848c2ecf20Sopenharmony_ci } else { 14858c2ecf20Sopenharmony_ci return -ENODEV; 14868c2ecf20Sopenharmony_ci } 14878c2ecf20Sopenharmony_ci 14888c2ecf20Sopenharmony_ci data->chip_info = <r501_chip_info_tbl[chip_idx]; 14898c2ecf20Sopenharmony_ci 14908c2ecf20Sopenharmony_ci if ((partid >> 4) != data->chip_info->partid) 14918c2ecf20Sopenharmony_ci return -ENODEV; 14928c2ecf20Sopenharmony_ci 14938c2ecf20Sopenharmony_ci indio_dev->info = data->chip_info->info; 14948c2ecf20Sopenharmony_ci indio_dev->channels = data->chip_info->channels; 14958c2ecf20Sopenharmony_ci indio_dev->num_channels = data->chip_info->no_channels; 14968c2ecf20Sopenharmony_ci indio_dev->name = name; 14978c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 14988c2ecf20Sopenharmony_ci 14998c2ecf20Sopenharmony_ci ret = ltr501_init(data); 15008c2ecf20Sopenharmony_ci if (ret < 0) 15018c2ecf20Sopenharmony_ci return ret; 15028c2ecf20Sopenharmony_ci 15038c2ecf20Sopenharmony_ci if (client->irq > 0) { 15048c2ecf20Sopenharmony_ci ret = devm_request_threaded_irq(&client->dev, client->irq, 15058c2ecf20Sopenharmony_ci NULL, ltr501_interrupt_handler, 15068c2ecf20Sopenharmony_ci IRQF_TRIGGER_FALLING | 15078c2ecf20Sopenharmony_ci IRQF_ONESHOT, 15088c2ecf20Sopenharmony_ci "ltr501_thresh_event", 15098c2ecf20Sopenharmony_ci indio_dev); 15108c2ecf20Sopenharmony_ci if (ret) { 15118c2ecf20Sopenharmony_ci dev_err(&client->dev, "request irq (%d) failed\n", 15128c2ecf20Sopenharmony_ci client->irq); 15138c2ecf20Sopenharmony_ci return ret; 15148c2ecf20Sopenharmony_ci } 15158c2ecf20Sopenharmony_ci } else { 15168c2ecf20Sopenharmony_ci indio_dev->info = data->chip_info->info_no_irq; 15178c2ecf20Sopenharmony_ci } 15188c2ecf20Sopenharmony_ci 15198c2ecf20Sopenharmony_ci ret = iio_triggered_buffer_setup(indio_dev, NULL, 15208c2ecf20Sopenharmony_ci ltr501_trigger_handler, NULL); 15218c2ecf20Sopenharmony_ci if (ret) 15228c2ecf20Sopenharmony_ci goto powerdown_on_error; 15238c2ecf20Sopenharmony_ci 15248c2ecf20Sopenharmony_ci ret = iio_device_register(indio_dev); 15258c2ecf20Sopenharmony_ci if (ret) 15268c2ecf20Sopenharmony_ci goto error_unreg_buffer; 15278c2ecf20Sopenharmony_ci 15288c2ecf20Sopenharmony_ci return 0; 15298c2ecf20Sopenharmony_ci 15308c2ecf20Sopenharmony_cierror_unreg_buffer: 15318c2ecf20Sopenharmony_ci iio_triggered_buffer_cleanup(indio_dev); 15328c2ecf20Sopenharmony_cipowerdown_on_error: 15338c2ecf20Sopenharmony_ci ltr501_powerdown(data); 15348c2ecf20Sopenharmony_ci return ret; 15358c2ecf20Sopenharmony_ci} 15368c2ecf20Sopenharmony_ci 15378c2ecf20Sopenharmony_cistatic int ltr501_remove(struct i2c_client *client) 15388c2ecf20Sopenharmony_ci{ 15398c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(client); 15408c2ecf20Sopenharmony_ci 15418c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 15428c2ecf20Sopenharmony_ci iio_triggered_buffer_cleanup(indio_dev); 15438c2ecf20Sopenharmony_ci ltr501_powerdown(iio_priv(indio_dev)); 15448c2ecf20Sopenharmony_ci 15458c2ecf20Sopenharmony_ci return 0; 15468c2ecf20Sopenharmony_ci} 15478c2ecf20Sopenharmony_ci 15488c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP 15498c2ecf20Sopenharmony_cistatic int ltr501_suspend(struct device *dev) 15508c2ecf20Sopenharmony_ci{ 15518c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(i2c_get_clientdata( 15528c2ecf20Sopenharmony_ci to_i2c_client(dev))); 15538c2ecf20Sopenharmony_ci return ltr501_powerdown(data); 15548c2ecf20Sopenharmony_ci} 15558c2ecf20Sopenharmony_ci 15568c2ecf20Sopenharmony_cistatic int ltr501_resume(struct device *dev) 15578c2ecf20Sopenharmony_ci{ 15588c2ecf20Sopenharmony_ci struct ltr501_data *data = iio_priv(i2c_get_clientdata( 15598c2ecf20Sopenharmony_ci to_i2c_client(dev))); 15608c2ecf20Sopenharmony_ci 15618c2ecf20Sopenharmony_ci return ltr501_write_contr(data, data->als_contr, 15628c2ecf20Sopenharmony_ci data->ps_contr); 15638c2ecf20Sopenharmony_ci} 15648c2ecf20Sopenharmony_ci#endif 15658c2ecf20Sopenharmony_ci 15668c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(ltr501_pm_ops, ltr501_suspend, ltr501_resume); 15678c2ecf20Sopenharmony_ci 15688c2ecf20Sopenharmony_cistatic const struct acpi_device_id ltr_acpi_match[] = { 15698c2ecf20Sopenharmony_ci {"LTER0501", ltr501}, 15708c2ecf20Sopenharmony_ci {"LTER0559", ltr559}, 15718c2ecf20Sopenharmony_ci {"LTER0301", ltr301}, 15728c2ecf20Sopenharmony_ci { }, 15738c2ecf20Sopenharmony_ci}; 15748c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, ltr_acpi_match); 15758c2ecf20Sopenharmony_ci 15768c2ecf20Sopenharmony_cistatic const struct i2c_device_id ltr501_id[] = { 15778c2ecf20Sopenharmony_ci { "ltr501", ltr501}, 15788c2ecf20Sopenharmony_ci { "ltr559", ltr559}, 15798c2ecf20Sopenharmony_ci { "ltr301", ltr301}, 15808c2ecf20Sopenharmony_ci { } 15818c2ecf20Sopenharmony_ci}; 15828c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, ltr501_id); 15838c2ecf20Sopenharmony_ci 15848c2ecf20Sopenharmony_cistatic struct i2c_driver ltr501_driver = { 15858c2ecf20Sopenharmony_ci .driver = { 15868c2ecf20Sopenharmony_ci .name = LTR501_DRV_NAME, 15878c2ecf20Sopenharmony_ci .pm = <r501_pm_ops, 15888c2ecf20Sopenharmony_ci .acpi_match_table = ACPI_PTR(ltr_acpi_match), 15898c2ecf20Sopenharmony_ci }, 15908c2ecf20Sopenharmony_ci .probe = ltr501_probe, 15918c2ecf20Sopenharmony_ci .remove = ltr501_remove, 15928c2ecf20Sopenharmony_ci .id_table = ltr501_id, 15938c2ecf20Sopenharmony_ci}; 15948c2ecf20Sopenharmony_ci 15958c2ecf20Sopenharmony_cimodule_i2c_driver(ltr501_driver); 15968c2ecf20Sopenharmony_ci 15978c2ecf20Sopenharmony_ciMODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 15988c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Lite-On LTR501 ambient light and proximity sensor driver"); 15998c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1600