18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * vl6180.c - Support for STMicroelectronics VL6180 ALS, range and proximity 48c2ecf20Sopenharmony_ci * sensor 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net> 78c2ecf20Sopenharmony_ci * Copyright 2017 Manivannan Sadhasivam <manivannanece23@gmail.com> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * IIO driver for VL6180 (7-bit I2C slave address 0x29) 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Range: 0 to 100mm 128c2ecf20Sopenharmony_ci * ALS: < 1 Lux up to 100 kLux 138c2ecf20Sopenharmony_ci * IR: 850nm 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * TODO: irq, threshold events, continuous mode, hardware buffer 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <linux/module.h> 198c2ecf20Sopenharmony_ci#include <linux/mod_devicetable.h> 208c2ecf20Sopenharmony_ci#include <linux/i2c.h> 218c2ecf20Sopenharmony_ci#include <linux/mutex.h> 228c2ecf20Sopenharmony_ci#include <linux/err.h> 238c2ecf20Sopenharmony_ci#include <linux/of.h> 248c2ecf20Sopenharmony_ci#include <linux/delay.h> 258c2ecf20Sopenharmony_ci#include <linux/util_macros.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 288c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define VL6180_DRV_NAME "vl6180" 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci/* Device identification register and value */ 338c2ecf20Sopenharmony_ci#define VL6180_MODEL_ID 0x000 348c2ecf20Sopenharmony_ci#define VL6180_MODEL_ID_VAL 0xb4 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/* Configuration registers */ 378c2ecf20Sopenharmony_ci#define VL6180_INTR_CONFIG 0x014 388c2ecf20Sopenharmony_ci#define VL6180_INTR_CLEAR 0x015 398c2ecf20Sopenharmony_ci#define VL6180_OUT_OF_RESET 0x016 408c2ecf20Sopenharmony_ci#define VL6180_HOLD 0x017 418c2ecf20Sopenharmony_ci#define VL6180_RANGE_START 0x018 428c2ecf20Sopenharmony_ci#define VL6180_ALS_START 0x038 438c2ecf20Sopenharmony_ci#define VL6180_ALS_GAIN 0x03f 448c2ecf20Sopenharmony_ci#define VL6180_ALS_IT 0x040 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* Status registers */ 478c2ecf20Sopenharmony_ci#define VL6180_RANGE_STATUS 0x04d 488c2ecf20Sopenharmony_ci#define VL6180_ALS_STATUS 0x04e 498c2ecf20Sopenharmony_ci#define VL6180_INTR_STATUS 0x04f 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci/* Result value registers */ 528c2ecf20Sopenharmony_ci#define VL6180_ALS_VALUE 0x050 538c2ecf20Sopenharmony_ci#define VL6180_RANGE_VALUE 0x062 548c2ecf20Sopenharmony_ci#define VL6180_RANGE_RATE 0x066 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* bits of the RANGE_START and ALS_START register */ 578c2ecf20Sopenharmony_ci#define VL6180_MODE_CONT BIT(1) /* continuous mode */ 588c2ecf20Sopenharmony_ci#define VL6180_STARTSTOP BIT(0) /* start measurement, auto-reset */ 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/* bits of the INTR_STATUS and INTR_CONFIG register */ 618c2ecf20Sopenharmony_ci#define VL6180_ALS_READY BIT(5) 628c2ecf20Sopenharmony_ci#define VL6180_RANGE_READY BIT(2) 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci/* bits of the INTR_CLEAR register */ 658c2ecf20Sopenharmony_ci#define VL6180_CLEAR_ERROR BIT(2) 668c2ecf20Sopenharmony_ci#define VL6180_CLEAR_ALS BIT(1) 678c2ecf20Sopenharmony_ci#define VL6180_CLEAR_RANGE BIT(0) 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* bits of the HOLD register */ 708c2ecf20Sopenharmony_ci#define VL6180_HOLD_ON BIT(0) 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/* default value for the ALS_IT register */ 738c2ecf20Sopenharmony_ci#define VL6180_ALS_IT_100 0x63 /* 100 ms */ 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci/* values for the ALS_GAIN register */ 768c2ecf20Sopenharmony_ci#define VL6180_ALS_GAIN_1 0x46 778c2ecf20Sopenharmony_ci#define VL6180_ALS_GAIN_1_25 0x45 788c2ecf20Sopenharmony_ci#define VL6180_ALS_GAIN_1_67 0x44 798c2ecf20Sopenharmony_ci#define VL6180_ALS_GAIN_2_5 0x43 808c2ecf20Sopenharmony_ci#define VL6180_ALS_GAIN_5 0x42 818c2ecf20Sopenharmony_ci#define VL6180_ALS_GAIN_10 0x41 828c2ecf20Sopenharmony_ci#define VL6180_ALS_GAIN_20 0x40 838c2ecf20Sopenharmony_ci#define VL6180_ALS_GAIN_40 0x47 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistruct vl6180_data { 868c2ecf20Sopenharmony_ci struct i2c_client *client; 878c2ecf20Sopenharmony_ci struct mutex lock; 888c2ecf20Sopenharmony_ci unsigned int als_gain_milli; 898c2ecf20Sopenharmony_ci unsigned int als_it_ms; 908c2ecf20Sopenharmony_ci}; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cienum { VL6180_ALS, VL6180_RANGE, VL6180_PROX }; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci/** 958c2ecf20Sopenharmony_ci * struct vl6180_chan_regs - Registers for accessing channels 968c2ecf20Sopenharmony_ci * @drdy_mask: Data ready bit in status register 978c2ecf20Sopenharmony_ci * @start_reg: Conversion start register 988c2ecf20Sopenharmony_ci * @value_reg: Result value register 998c2ecf20Sopenharmony_ci * @word: Register word length 1008c2ecf20Sopenharmony_ci */ 1018c2ecf20Sopenharmony_cistruct vl6180_chan_regs { 1028c2ecf20Sopenharmony_ci u8 drdy_mask; 1038c2ecf20Sopenharmony_ci u16 start_reg, value_reg; 1048c2ecf20Sopenharmony_ci bool word; 1058c2ecf20Sopenharmony_ci}; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic const struct vl6180_chan_regs vl6180_chan_regs_table[] = { 1088c2ecf20Sopenharmony_ci [VL6180_ALS] = { 1098c2ecf20Sopenharmony_ci .drdy_mask = VL6180_ALS_READY, 1108c2ecf20Sopenharmony_ci .start_reg = VL6180_ALS_START, 1118c2ecf20Sopenharmony_ci .value_reg = VL6180_ALS_VALUE, 1128c2ecf20Sopenharmony_ci .word = true, 1138c2ecf20Sopenharmony_ci }, 1148c2ecf20Sopenharmony_ci [VL6180_RANGE] = { 1158c2ecf20Sopenharmony_ci .drdy_mask = VL6180_RANGE_READY, 1168c2ecf20Sopenharmony_ci .start_reg = VL6180_RANGE_START, 1178c2ecf20Sopenharmony_ci .value_reg = VL6180_RANGE_VALUE, 1188c2ecf20Sopenharmony_ci .word = false, 1198c2ecf20Sopenharmony_ci }, 1208c2ecf20Sopenharmony_ci [VL6180_PROX] = { 1218c2ecf20Sopenharmony_ci .drdy_mask = VL6180_RANGE_READY, 1228c2ecf20Sopenharmony_ci .start_reg = VL6180_RANGE_START, 1238c2ecf20Sopenharmony_ci .value_reg = VL6180_RANGE_RATE, 1248c2ecf20Sopenharmony_ci .word = true, 1258c2ecf20Sopenharmony_ci }, 1268c2ecf20Sopenharmony_ci}; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic int vl6180_read(struct i2c_client *client, u16 cmd, void *databuf, 1298c2ecf20Sopenharmony_ci u8 len) 1308c2ecf20Sopenharmony_ci{ 1318c2ecf20Sopenharmony_ci __be16 cmdbuf = cpu_to_be16(cmd); 1328c2ecf20Sopenharmony_ci struct i2c_msg msgs[2] = { 1338c2ecf20Sopenharmony_ci { .addr = client->addr, .len = sizeof(cmdbuf), .buf = (u8 *) &cmdbuf }, 1348c2ecf20Sopenharmony_ci { .addr = client->addr, .len = len, .buf = databuf, 1358c2ecf20Sopenharmony_ci .flags = I2C_M_RD } }; 1368c2ecf20Sopenharmony_ci int ret; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 1398c2ecf20Sopenharmony_ci if (ret < 0) 1408c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed reading register 0x%04x\n", cmd); 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci return ret; 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic int vl6180_read_byte(struct i2c_client *client, u16 cmd) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci u8 data; 1488c2ecf20Sopenharmony_ci int ret; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci ret = vl6180_read(client, cmd, &data, sizeof(data)); 1518c2ecf20Sopenharmony_ci if (ret < 0) 1528c2ecf20Sopenharmony_ci return ret; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci return data; 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic int vl6180_read_word(struct i2c_client *client, u16 cmd) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci __be16 data; 1608c2ecf20Sopenharmony_ci int ret; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci ret = vl6180_read(client, cmd, &data, sizeof(data)); 1638c2ecf20Sopenharmony_ci if (ret < 0) 1648c2ecf20Sopenharmony_ci return ret; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci return be16_to_cpu(data); 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_cistatic int vl6180_write_byte(struct i2c_client *client, u16 cmd, u8 val) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci u8 buf[3]; 1728c2ecf20Sopenharmony_ci struct i2c_msg msgs[1] = { 1738c2ecf20Sopenharmony_ci { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } }; 1748c2ecf20Sopenharmony_ci int ret; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci buf[0] = cmd >> 8; 1778c2ecf20Sopenharmony_ci buf[1] = cmd & 0xff; 1788c2ecf20Sopenharmony_ci buf[2] = val; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 1818c2ecf20Sopenharmony_ci if (ret < 0) { 1828c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed writing register 0x%04x\n", cmd); 1838c2ecf20Sopenharmony_ci return ret; 1848c2ecf20Sopenharmony_ci } 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci return 0; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cistatic int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci __be16 buf[2]; 1928c2ecf20Sopenharmony_ci struct i2c_msg msgs[1] = { 1938c2ecf20Sopenharmony_ci { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } }; 1948c2ecf20Sopenharmony_ci int ret; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci buf[0] = cpu_to_be16(cmd); 1978c2ecf20Sopenharmony_ci buf[1] = cpu_to_be16(val); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 2008c2ecf20Sopenharmony_ci if (ret < 0) { 2018c2ecf20Sopenharmony_ci dev_err(&client->dev, "failed writing register 0x%04x\n", cmd); 2028c2ecf20Sopenharmony_ci return ret; 2038c2ecf20Sopenharmony_ci } 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci return 0; 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic int vl6180_measure(struct vl6180_data *data, int addr) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 2118c2ecf20Sopenharmony_ci int tries = 20, ret; 2128c2ecf20Sopenharmony_ci u16 value; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 2158c2ecf20Sopenharmony_ci /* Start single shot measurement */ 2168c2ecf20Sopenharmony_ci ret = vl6180_write_byte(client, 2178c2ecf20Sopenharmony_ci vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP); 2188c2ecf20Sopenharmony_ci if (ret < 0) 2198c2ecf20Sopenharmony_ci goto fail; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci while (tries--) { 2228c2ecf20Sopenharmony_ci ret = vl6180_read_byte(client, VL6180_INTR_STATUS); 2238c2ecf20Sopenharmony_ci if (ret < 0) 2248c2ecf20Sopenharmony_ci goto fail; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci if (ret & vl6180_chan_regs_table[addr].drdy_mask) 2278c2ecf20Sopenharmony_ci break; 2288c2ecf20Sopenharmony_ci msleep(20); 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci if (tries < 0) { 2328c2ecf20Sopenharmony_ci ret = -EIO; 2338c2ecf20Sopenharmony_ci goto fail; 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci /* Read result value from appropriate registers */ 2378c2ecf20Sopenharmony_ci ret = vl6180_chan_regs_table[addr].word ? 2388c2ecf20Sopenharmony_ci vl6180_read_word(client, vl6180_chan_regs_table[addr].value_reg) : 2398c2ecf20Sopenharmony_ci vl6180_read_byte(client, vl6180_chan_regs_table[addr].value_reg); 2408c2ecf20Sopenharmony_ci if (ret < 0) 2418c2ecf20Sopenharmony_ci goto fail; 2428c2ecf20Sopenharmony_ci value = ret; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci /* Clear the interrupt flag after data read */ 2458c2ecf20Sopenharmony_ci ret = vl6180_write_byte(client, VL6180_INTR_CLEAR, 2468c2ecf20Sopenharmony_ci VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE); 2478c2ecf20Sopenharmony_ci if (ret < 0) 2488c2ecf20Sopenharmony_ci goto fail; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci ret = value; 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_cifail: 2538c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci return ret; 2568c2ecf20Sopenharmony_ci} 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_cistatic const struct iio_chan_spec vl6180_channels[] = { 2598c2ecf20Sopenharmony_ci { 2608c2ecf20Sopenharmony_ci .type = IIO_LIGHT, 2618c2ecf20Sopenharmony_ci .address = VL6180_ALS, 2628c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 2638c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_INT_TIME) | 2648c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE) | 2658c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_HARDWAREGAIN), 2668c2ecf20Sopenharmony_ci }, { 2678c2ecf20Sopenharmony_ci .type = IIO_DISTANCE, 2688c2ecf20Sopenharmony_ci .address = VL6180_RANGE, 2698c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 2708c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_SCALE), 2718c2ecf20Sopenharmony_ci }, { 2728c2ecf20Sopenharmony_ci .type = IIO_PROXIMITY, 2738c2ecf20Sopenharmony_ci .address = VL6180_PROX, 2748c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 2758c2ecf20Sopenharmony_ci } 2768c2ecf20Sopenharmony_ci}; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci/* 2798c2ecf20Sopenharmony_ci * Available Ambient Light Sensor gain settings, 1/1000th, and 2808c2ecf20Sopenharmony_ci * corresponding setting for the VL6180_ALS_GAIN register 2818c2ecf20Sopenharmony_ci */ 2828c2ecf20Sopenharmony_cistatic const int vl6180_als_gain_tab[8] = { 2838c2ecf20Sopenharmony_ci 1000, 1250, 1670, 2500, 5000, 10000, 20000, 40000 2848c2ecf20Sopenharmony_ci}; 2858c2ecf20Sopenharmony_cistatic const u8 vl6180_als_gain_tab_bits[8] = { 2868c2ecf20Sopenharmony_ci VL6180_ALS_GAIN_1, VL6180_ALS_GAIN_1_25, 2878c2ecf20Sopenharmony_ci VL6180_ALS_GAIN_1_67, VL6180_ALS_GAIN_2_5, 2888c2ecf20Sopenharmony_ci VL6180_ALS_GAIN_5, VL6180_ALS_GAIN_10, 2898c2ecf20Sopenharmony_ci VL6180_ALS_GAIN_20, VL6180_ALS_GAIN_40 2908c2ecf20Sopenharmony_ci}; 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cistatic int vl6180_read_raw(struct iio_dev *indio_dev, 2938c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 2948c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 2958c2ecf20Sopenharmony_ci{ 2968c2ecf20Sopenharmony_ci struct vl6180_data *data = iio_priv(indio_dev); 2978c2ecf20Sopenharmony_ci int ret; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci switch (mask) { 3008c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 3018c2ecf20Sopenharmony_ci ret = vl6180_measure(data, chan->address); 3028c2ecf20Sopenharmony_ci if (ret < 0) 3038c2ecf20Sopenharmony_ci return ret; 3048c2ecf20Sopenharmony_ci *val = ret; 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci return IIO_VAL_INT; 3078c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_INT_TIME: 3088c2ecf20Sopenharmony_ci *val = data->als_it_ms; 3098c2ecf20Sopenharmony_ci *val2 = 1000; 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci return IIO_VAL_FRACTIONAL; 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 3148c2ecf20Sopenharmony_ci switch (chan->type) { 3158c2ecf20Sopenharmony_ci case IIO_LIGHT: 3168c2ecf20Sopenharmony_ci /* one ALS count is 0.32 Lux @ gain 1, IT 100 ms */ 3178c2ecf20Sopenharmony_ci *val = 32000; /* 0.32 * 1000 * 100 */ 3188c2ecf20Sopenharmony_ci *val2 = data->als_gain_milli * data->als_it_ms; 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci return IIO_VAL_FRACTIONAL; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci case IIO_DISTANCE: 3238c2ecf20Sopenharmony_ci *val = 0; /* sensor reports mm, scale to meter */ 3248c2ecf20Sopenharmony_ci *val2 = 1000; 3258c2ecf20Sopenharmony_ci break; 3268c2ecf20Sopenharmony_ci default: 3278c2ecf20Sopenharmony_ci return -EINVAL; 3288c2ecf20Sopenharmony_ci } 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 3318c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_HARDWAREGAIN: 3328c2ecf20Sopenharmony_ci *val = data->als_gain_milli; 3338c2ecf20Sopenharmony_ci *val2 = 1000; 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci return IIO_VAL_FRACTIONAL; 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci default: 3388c2ecf20Sopenharmony_ci return -EINVAL; 3398c2ecf20Sopenharmony_ci } 3408c2ecf20Sopenharmony_ci} 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(als_gain_available, "1 1.25 1.67 2.5 5 10 20 40"); 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_cistatic struct attribute *vl6180_attributes[] = { 3458c2ecf20Sopenharmony_ci &iio_const_attr_als_gain_available.dev_attr.attr, 3468c2ecf20Sopenharmony_ci NULL 3478c2ecf20Sopenharmony_ci}; 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_cistatic const struct attribute_group vl6180_attribute_group = { 3508c2ecf20Sopenharmony_ci .attrs = vl6180_attributes, 3518c2ecf20Sopenharmony_ci}; 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci/* HOLD is needed before updating any config registers */ 3548c2ecf20Sopenharmony_cistatic int vl6180_hold(struct vl6180_data *data, bool hold) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci return vl6180_write_byte(data->client, VL6180_HOLD, 3578c2ecf20Sopenharmony_ci hold ? VL6180_HOLD_ON : 0); 3588c2ecf20Sopenharmony_ci} 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_cistatic int vl6180_set_als_gain(struct vl6180_data *data, int val, int val2) 3618c2ecf20Sopenharmony_ci{ 3628c2ecf20Sopenharmony_ci int i, ret, gain; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci if (val < 1 || val > 40) 3658c2ecf20Sopenharmony_ci return -EINVAL; 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci gain = (val * 1000000 + val2) / 1000; 3688c2ecf20Sopenharmony_ci if (gain < 1 || gain > 40000) 3698c2ecf20Sopenharmony_ci return -EINVAL; 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci i = find_closest(gain, vl6180_als_gain_tab, 3728c2ecf20Sopenharmony_ci ARRAY_SIZE(vl6180_als_gain_tab)); 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 3758c2ecf20Sopenharmony_ci ret = vl6180_hold(data, true); 3768c2ecf20Sopenharmony_ci if (ret < 0) 3778c2ecf20Sopenharmony_ci goto fail; 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_ci ret = vl6180_write_byte(data->client, VL6180_ALS_GAIN, 3808c2ecf20Sopenharmony_ci vl6180_als_gain_tab_bits[i]); 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_ci if (ret >= 0) 3838c2ecf20Sopenharmony_ci data->als_gain_milli = vl6180_als_gain_tab[i]; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_cifail: 3868c2ecf20Sopenharmony_ci vl6180_hold(data, false); 3878c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 3888c2ecf20Sopenharmony_ci return ret; 3898c2ecf20Sopenharmony_ci} 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_cistatic int vl6180_set_it(struct vl6180_data *data, int val, int val2) 3928c2ecf20Sopenharmony_ci{ 3938c2ecf20Sopenharmony_ci int ret, it_ms; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci it_ms = (val2 + 500) / 1000; /* round to ms */ 3968c2ecf20Sopenharmony_ci if (val != 0 || it_ms < 1 || it_ms > 512) 3978c2ecf20Sopenharmony_ci return -EINVAL; 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 4008c2ecf20Sopenharmony_ci ret = vl6180_hold(data, true); 4018c2ecf20Sopenharmony_ci if (ret < 0) 4028c2ecf20Sopenharmony_ci goto fail; 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci ret = vl6180_write_word(data->client, VL6180_ALS_IT, it_ms - 1); 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci if (ret >= 0) 4078c2ecf20Sopenharmony_ci data->als_it_ms = it_ms; 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_cifail: 4108c2ecf20Sopenharmony_ci vl6180_hold(data, false); 4118c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci return ret; 4148c2ecf20Sopenharmony_ci} 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_cistatic int vl6180_write_raw(struct iio_dev *indio_dev, 4178c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 4188c2ecf20Sopenharmony_ci int val, int val2, long mask) 4198c2ecf20Sopenharmony_ci{ 4208c2ecf20Sopenharmony_ci struct vl6180_data *data = iio_priv(indio_dev); 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci switch (mask) { 4238c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_INT_TIME: 4248c2ecf20Sopenharmony_ci return vl6180_set_it(data, val, val2); 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_HARDWAREGAIN: 4278c2ecf20Sopenharmony_ci if (chan->type != IIO_LIGHT) 4288c2ecf20Sopenharmony_ci return -EINVAL; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci return vl6180_set_als_gain(data, val, val2); 4318c2ecf20Sopenharmony_ci default: 4328c2ecf20Sopenharmony_ci return -EINVAL; 4338c2ecf20Sopenharmony_ci } 4348c2ecf20Sopenharmony_ci} 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_cistatic const struct iio_info vl6180_info = { 4378c2ecf20Sopenharmony_ci .read_raw = vl6180_read_raw, 4388c2ecf20Sopenharmony_ci .write_raw = vl6180_write_raw, 4398c2ecf20Sopenharmony_ci .attrs = &vl6180_attribute_group, 4408c2ecf20Sopenharmony_ci}; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_cistatic int vl6180_init(struct vl6180_data *data) 4438c2ecf20Sopenharmony_ci{ 4448c2ecf20Sopenharmony_ci struct i2c_client *client = data->client; 4458c2ecf20Sopenharmony_ci int ret; 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci ret = vl6180_read_byte(client, VL6180_MODEL_ID); 4488c2ecf20Sopenharmony_ci if (ret < 0) 4498c2ecf20Sopenharmony_ci return ret; 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci if (ret != VL6180_MODEL_ID_VAL) { 4528c2ecf20Sopenharmony_ci dev_err(&client->dev, "invalid model ID %02x\n", ret); 4538c2ecf20Sopenharmony_ci return -ENODEV; 4548c2ecf20Sopenharmony_ci } 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci ret = vl6180_hold(data, true); 4578c2ecf20Sopenharmony_ci if (ret < 0) 4588c2ecf20Sopenharmony_ci return ret; 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci ret = vl6180_read_byte(client, VL6180_OUT_OF_RESET); 4618c2ecf20Sopenharmony_ci if (ret < 0) 4628c2ecf20Sopenharmony_ci return ret; 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci /* 4658c2ecf20Sopenharmony_ci * Detect false reset condition here. This bit is always set when the 4668c2ecf20Sopenharmony_ci * system comes out of reset. 4678c2ecf20Sopenharmony_ci */ 4688c2ecf20Sopenharmony_ci if (ret != 0x01) 4698c2ecf20Sopenharmony_ci dev_info(&client->dev, "device is not fresh out of reset\n"); 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci /* Enable ALS and Range ready interrupts */ 4728c2ecf20Sopenharmony_ci ret = vl6180_write_byte(client, VL6180_INTR_CONFIG, 4738c2ecf20Sopenharmony_ci VL6180_ALS_READY | VL6180_RANGE_READY); 4748c2ecf20Sopenharmony_ci if (ret < 0) 4758c2ecf20Sopenharmony_ci return ret; 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci /* ALS integration time: 100ms */ 4788c2ecf20Sopenharmony_ci data->als_it_ms = 100; 4798c2ecf20Sopenharmony_ci ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100); 4808c2ecf20Sopenharmony_ci if (ret < 0) 4818c2ecf20Sopenharmony_ci return ret; 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci /* ALS gain: 1 */ 4848c2ecf20Sopenharmony_ci data->als_gain_milli = 1000; 4858c2ecf20Sopenharmony_ci ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1); 4868c2ecf20Sopenharmony_ci if (ret < 0) 4878c2ecf20Sopenharmony_ci return ret; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci ret = vl6180_write_byte(client, VL6180_OUT_OF_RESET, 0x00); 4908c2ecf20Sopenharmony_ci if (ret < 0) 4918c2ecf20Sopenharmony_ci return ret; 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci return vl6180_hold(data, false); 4948c2ecf20Sopenharmony_ci} 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_cistatic int vl6180_probe(struct i2c_client *client, 4978c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 4988c2ecf20Sopenharmony_ci{ 4998c2ecf20Sopenharmony_ci struct vl6180_data *data; 5008c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 5018c2ecf20Sopenharmony_ci int ret; 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 5048c2ecf20Sopenharmony_ci if (!indio_dev) 5058c2ecf20Sopenharmony_ci return -ENOMEM; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci data = iio_priv(indio_dev); 5088c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 5098c2ecf20Sopenharmony_ci data->client = client; 5108c2ecf20Sopenharmony_ci mutex_init(&data->lock); 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci indio_dev->info = &vl6180_info; 5138c2ecf20Sopenharmony_ci indio_dev->channels = vl6180_channels; 5148c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(vl6180_channels); 5158c2ecf20Sopenharmony_ci indio_dev->name = VL6180_DRV_NAME; 5168c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci ret = vl6180_init(data); 5198c2ecf20Sopenharmony_ci if (ret < 0) 5208c2ecf20Sopenharmony_ci return ret; 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci return devm_iio_device_register(&client->dev, indio_dev); 5238c2ecf20Sopenharmony_ci} 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_cistatic const struct of_device_id vl6180_of_match[] = { 5268c2ecf20Sopenharmony_ci { .compatible = "st,vl6180", }, 5278c2ecf20Sopenharmony_ci { }, 5288c2ecf20Sopenharmony_ci}; 5298c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, vl6180_of_match); 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_cistatic const struct i2c_device_id vl6180_id[] = { 5328c2ecf20Sopenharmony_ci { "vl6180", 0 }, 5338c2ecf20Sopenharmony_ci { } 5348c2ecf20Sopenharmony_ci}; 5358c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, vl6180_id); 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_cistatic struct i2c_driver vl6180_driver = { 5388c2ecf20Sopenharmony_ci .driver = { 5398c2ecf20Sopenharmony_ci .name = VL6180_DRV_NAME, 5408c2ecf20Sopenharmony_ci .of_match_table = vl6180_of_match, 5418c2ecf20Sopenharmony_ci }, 5428c2ecf20Sopenharmony_ci .probe = vl6180_probe, 5438c2ecf20Sopenharmony_ci .id_table = vl6180_id, 5448c2ecf20Sopenharmony_ci}; 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_cimodule_i2c_driver(vl6180_driver); 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ciMODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>"); 5498c2ecf20Sopenharmony_ciMODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>"); 5508c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("STMicro VL6180 ALS, range and proximity sensor driver"); 5518c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 552