18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2013 Samsung Electronics Co., Ltd. 48c2ecf20Sopenharmony_ci * Author: Beomho Seo <beomho.seo@samsung.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/delay.h> 88c2ecf20Sopenharmony_ci#include <linux/err.h> 98c2ecf20Sopenharmony_ci#include <linux/i2c.h> 108c2ecf20Sopenharmony_ci#include <linux/mutex.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 138c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h> 148c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 158c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 168c2ecf20Sopenharmony_ci#include <linux/iio/events.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* Slave address 0x19 for PS of 7 bit addressing protocol for I2C */ 198c2ecf20Sopenharmony_ci#define CM36651_I2C_ADDR_PS 0x19 208c2ecf20Sopenharmony_ci/* Alert Response Address */ 218c2ecf20Sopenharmony_ci#define CM36651_ARA 0x0C 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* Ambient light sensor */ 248c2ecf20Sopenharmony_ci#define CM36651_CS_CONF1 0x00 258c2ecf20Sopenharmony_ci#define CM36651_CS_CONF2 0x01 268c2ecf20Sopenharmony_ci#define CM36651_ALS_WH_M 0x02 278c2ecf20Sopenharmony_ci#define CM36651_ALS_WH_L 0x03 288c2ecf20Sopenharmony_ci#define CM36651_ALS_WL_M 0x04 298c2ecf20Sopenharmony_ci#define CM36651_ALS_WL_L 0x05 308c2ecf20Sopenharmony_ci#define CM36651_CS_CONF3 0x06 318c2ecf20Sopenharmony_ci#define CM36651_CS_CONF_REG_NUM 0x02 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci/* Proximity sensor */ 348c2ecf20Sopenharmony_ci#define CM36651_PS_CONF1 0x00 358c2ecf20Sopenharmony_ci#define CM36651_PS_THD 0x01 368c2ecf20Sopenharmony_ci#define CM36651_PS_CANC 0x02 378c2ecf20Sopenharmony_ci#define CM36651_PS_CONF2 0x03 388c2ecf20Sopenharmony_ci#define CM36651_PS_REG_NUM 0x04 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci/* CS_CONF1 command code */ 418c2ecf20Sopenharmony_ci#define CM36651_ALS_ENABLE 0x00 428c2ecf20Sopenharmony_ci#define CM36651_ALS_DISABLE 0x01 438c2ecf20Sopenharmony_ci#define CM36651_ALS_INT_EN 0x02 448c2ecf20Sopenharmony_ci#define CM36651_ALS_THRES 0x04 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci/* CS_CONF2 command code */ 478c2ecf20Sopenharmony_ci#define CM36651_CS_CONF2_DEFAULT_BIT 0x08 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci/* CS_CONF3 channel integration time */ 508c2ecf20Sopenharmony_ci#define CM36651_CS_IT1 0x00 /* Integration time 80 msec */ 518c2ecf20Sopenharmony_ci#define CM36651_CS_IT2 0x40 /* Integration time 160 msec */ 528c2ecf20Sopenharmony_ci#define CM36651_CS_IT3 0x80 /* Integration time 320 msec */ 538c2ecf20Sopenharmony_ci#define CM36651_CS_IT4 0xC0 /* Integration time 640 msec */ 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci/* PS_CONF1 command code */ 568c2ecf20Sopenharmony_ci#define CM36651_PS_ENABLE 0x00 578c2ecf20Sopenharmony_ci#define CM36651_PS_DISABLE 0x01 588c2ecf20Sopenharmony_ci#define CM36651_PS_INT_EN 0x02 598c2ecf20Sopenharmony_ci#define CM36651_PS_PERS2 0x04 608c2ecf20Sopenharmony_ci#define CM36651_PS_PERS3 0x08 618c2ecf20Sopenharmony_ci#define CM36651_PS_PERS4 0x0C 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci/* PS_CONF1 command code: integration time */ 648c2ecf20Sopenharmony_ci#define CM36651_PS_IT1 0x00 /* Integration time 0.32 msec */ 658c2ecf20Sopenharmony_ci#define CM36651_PS_IT2 0x10 /* Integration time 0.42 msec */ 668c2ecf20Sopenharmony_ci#define CM36651_PS_IT3 0x20 /* Integration time 0.52 msec */ 678c2ecf20Sopenharmony_ci#define CM36651_PS_IT4 0x30 /* Integration time 0.64 msec */ 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* PS_CONF1 command code: duty ratio */ 708c2ecf20Sopenharmony_ci#define CM36651_PS_DR1 0x00 /* Duty ratio 1/80 */ 718c2ecf20Sopenharmony_ci#define CM36651_PS_DR2 0x40 /* Duty ratio 1/160 */ 728c2ecf20Sopenharmony_ci#define CM36651_PS_DR3 0x80 /* Duty ratio 1/320 */ 738c2ecf20Sopenharmony_ci#define CM36651_PS_DR4 0xC0 /* Duty ratio 1/640 */ 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci/* PS_THD command code */ 768c2ecf20Sopenharmony_ci#define CM36651_PS_INITIAL_THD 0x05 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci/* PS_CANC command code */ 798c2ecf20Sopenharmony_ci#define CM36651_PS_CANC_DEFAULT 0x00 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci/* PS_CONF2 command code */ 828c2ecf20Sopenharmony_ci#define CM36651_PS_HYS1 0x00 838c2ecf20Sopenharmony_ci#define CM36651_PS_HYS2 0x01 848c2ecf20Sopenharmony_ci#define CM36651_PS_SMART_PERS_EN 0x02 858c2ecf20Sopenharmony_ci#define CM36651_PS_DIR_INT 0x04 868c2ecf20Sopenharmony_ci#define CM36651_PS_MS 0x10 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci#define CM36651_CS_COLOR_NUM 4 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#define CM36651_CLOSE_PROXIMITY 0x32 918c2ecf20Sopenharmony_ci#define CM36651_FAR_PROXIMITY 0x33 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci#define CM36651_CS_INT_TIME_AVAIL "0.08 0.16 0.32 0.64" 948c2ecf20Sopenharmony_ci#define CM36651_PS_INT_TIME_AVAIL "0.000320 0.000420 0.000520 0.000640" 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cienum cm36651_operation_mode { 978c2ecf20Sopenharmony_ci CM36651_LIGHT_EN, 988c2ecf20Sopenharmony_ci CM36651_PROXIMITY_EN, 998c2ecf20Sopenharmony_ci CM36651_PROXIMITY_EV_EN, 1008c2ecf20Sopenharmony_ci}; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cienum cm36651_light_channel_idx { 1038c2ecf20Sopenharmony_ci CM36651_LIGHT_CHANNEL_IDX_RED, 1048c2ecf20Sopenharmony_ci CM36651_LIGHT_CHANNEL_IDX_GREEN, 1058c2ecf20Sopenharmony_ci CM36651_LIGHT_CHANNEL_IDX_BLUE, 1068c2ecf20Sopenharmony_ci CM36651_LIGHT_CHANNEL_IDX_CLEAR, 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cienum cm36651_command { 1108c2ecf20Sopenharmony_ci CM36651_CMD_READ_RAW_LIGHT, 1118c2ecf20Sopenharmony_ci CM36651_CMD_READ_RAW_PROXIMITY, 1128c2ecf20Sopenharmony_ci CM36651_CMD_PROX_EV_EN, 1138c2ecf20Sopenharmony_ci CM36651_CMD_PROX_EV_DIS, 1148c2ecf20Sopenharmony_ci}; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic const u8 cm36651_cs_reg[CM36651_CS_CONF_REG_NUM] = { 1178c2ecf20Sopenharmony_ci CM36651_CS_CONF1, 1188c2ecf20Sopenharmony_ci CM36651_CS_CONF2, 1198c2ecf20Sopenharmony_ci}; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistatic const u8 cm36651_ps_reg[CM36651_PS_REG_NUM] = { 1228c2ecf20Sopenharmony_ci CM36651_PS_CONF1, 1238c2ecf20Sopenharmony_ci CM36651_PS_THD, 1248c2ecf20Sopenharmony_ci CM36651_PS_CANC, 1258c2ecf20Sopenharmony_ci CM36651_PS_CONF2, 1268c2ecf20Sopenharmony_ci}; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistruct cm36651_data { 1298c2ecf20Sopenharmony_ci const struct cm36651_platform_data *pdata; 1308c2ecf20Sopenharmony_ci struct i2c_client *client; 1318c2ecf20Sopenharmony_ci struct i2c_client *ps_client; 1328c2ecf20Sopenharmony_ci struct i2c_client *ara_client; 1338c2ecf20Sopenharmony_ci struct mutex lock; 1348c2ecf20Sopenharmony_ci struct regulator *vled_reg; 1358c2ecf20Sopenharmony_ci unsigned long flags; 1368c2ecf20Sopenharmony_ci int cs_int_time[CM36651_CS_COLOR_NUM]; 1378c2ecf20Sopenharmony_ci int ps_int_time; 1388c2ecf20Sopenharmony_ci u8 cs_ctrl_regs[CM36651_CS_CONF_REG_NUM]; 1398c2ecf20Sopenharmony_ci u8 ps_ctrl_regs[CM36651_PS_REG_NUM]; 1408c2ecf20Sopenharmony_ci u16 color[CM36651_CS_COLOR_NUM]; 1418c2ecf20Sopenharmony_ci}; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic int cm36651_setup_reg(struct cm36651_data *cm36651) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci struct i2c_client *client = cm36651->client; 1468c2ecf20Sopenharmony_ci struct i2c_client *ps_client = cm36651->ps_client; 1478c2ecf20Sopenharmony_ci int i, ret; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci /* CS initialization */ 1508c2ecf20Sopenharmony_ci cm36651->cs_ctrl_regs[CM36651_CS_CONF1] = CM36651_ALS_ENABLE | 1518c2ecf20Sopenharmony_ci CM36651_ALS_THRES; 1528c2ecf20Sopenharmony_ci cm36651->cs_ctrl_regs[CM36651_CS_CONF2] = CM36651_CS_CONF2_DEFAULT_BIT; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci for (i = 0; i < CM36651_CS_CONF_REG_NUM; i++) { 1558c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, cm36651_cs_reg[i], 1568c2ecf20Sopenharmony_ci cm36651->cs_ctrl_regs[i]); 1578c2ecf20Sopenharmony_ci if (ret < 0) 1588c2ecf20Sopenharmony_ci return ret; 1598c2ecf20Sopenharmony_ci } 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci /* PS initialization */ 1628c2ecf20Sopenharmony_ci cm36651->ps_ctrl_regs[CM36651_PS_CONF1] = CM36651_PS_ENABLE | 1638c2ecf20Sopenharmony_ci CM36651_PS_IT2; 1648c2ecf20Sopenharmony_ci cm36651->ps_ctrl_regs[CM36651_PS_THD] = CM36651_PS_INITIAL_THD; 1658c2ecf20Sopenharmony_ci cm36651->ps_ctrl_regs[CM36651_PS_CANC] = CM36651_PS_CANC_DEFAULT; 1668c2ecf20Sopenharmony_ci cm36651->ps_ctrl_regs[CM36651_PS_CONF2] = CM36651_PS_HYS2 | 1678c2ecf20Sopenharmony_ci CM36651_PS_DIR_INT | CM36651_PS_SMART_PERS_EN; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci for (i = 0; i < CM36651_PS_REG_NUM; i++) { 1708c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(ps_client, cm36651_ps_reg[i], 1718c2ecf20Sopenharmony_ci cm36651->ps_ctrl_regs[i]); 1728c2ecf20Sopenharmony_ci if (ret < 0) 1738c2ecf20Sopenharmony_ci return ret; 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci /* Set shutdown mode */ 1778c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1, 1788c2ecf20Sopenharmony_ci CM36651_ALS_DISABLE); 1798c2ecf20Sopenharmony_ci if (ret < 0) 1808c2ecf20Sopenharmony_ci return ret; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(cm36651->ps_client, 1838c2ecf20Sopenharmony_ci CM36651_PS_CONF1, CM36651_PS_DISABLE); 1848c2ecf20Sopenharmony_ci if (ret < 0) 1858c2ecf20Sopenharmony_ci return ret; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci return 0; 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic int cm36651_read_output(struct cm36651_data *cm36651, 1918c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, int *val) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci struct i2c_client *client = cm36651->client; 1948c2ecf20Sopenharmony_ci int ret = -EINVAL; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci switch (chan->type) { 1978c2ecf20Sopenharmony_ci case IIO_LIGHT: 1988c2ecf20Sopenharmony_ci *val = i2c_smbus_read_word_data(client, chan->address); 1998c2ecf20Sopenharmony_ci if (*val < 0) 2008c2ecf20Sopenharmony_ci return ret; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1, 2038c2ecf20Sopenharmony_ci CM36651_ALS_DISABLE); 2048c2ecf20Sopenharmony_ci if (ret < 0) 2058c2ecf20Sopenharmony_ci return ret; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci ret = IIO_VAL_INT; 2088c2ecf20Sopenharmony_ci break; 2098c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 2108c2ecf20Sopenharmony_ci *val = i2c_smbus_read_byte(cm36651->ps_client); 2118c2ecf20Sopenharmony_ci if (*val < 0) 2128c2ecf20Sopenharmony_ci return ret; 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) { 2158c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(cm36651->ps_client, 2168c2ecf20Sopenharmony_ci CM36651_PS_CONF1, CM36651_PS_DISABLE); 2178c2ecf20Sopenharmony_ci if (ret < 0) 2188c2ecf20Sopenharmony_ci return ret; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci ret = IIO_VAL_INT; 2228c2ecf20Sopenharmony_ci break; 2238c2ecf20Sopenharmony_ci default: 2248c2ecf20Sopenharmony_ci break; 2258c2ecf20Sopenharmony_ci } 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci return ret; 2288c2ecf20Sopenharmony_ci} 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cistatic irqreturn_t cm36651_irq_handler(int irq, void *data) 2318c2ecf20Sopenharmony_ci{ 2328c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = data; 2338c2ecf20Sopenharmony_ci struct cm36651_data *cm36651 = iio_priv(indio_dev); 2348c2ecf20Sopenharmony_ci struct i2c_client *client = cm36651->client; 2358c2ecf20Sopenharmony_ci int ev_dir, ret; 2368c2ecf20Sopenharmony_ci u64 ev_code; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci /* 2398c2ecf20Sopenharmony_ci * The PS INT pin is an active low signal that PS INT move logic low 2408c2ecf20Sopenharmony_ci * when the object is detect. Once the MCU host received the PS INT 2418c2ecf20Sopenharmony_ci * "LOW" signal, the Host needs to read the data at Alert Response 2428c2ecf20Sopenharmony_ci * Address(ARA) to clear the PS INT signal. After clearing the PS 2438c2ecf20Sopenharmony_ci * INT pin, the PS INT signal toggles from low to high. 2448c2ecf20Sopenharmony_ci */ 2458c2ecf20Sopenharmony_ci ret = i2c_smbus_read_byte(cm36651->ara_client); 2468c2ecf20Sopenharmony_ci if (ret < 0) { 2478c2ecf20Sopenharmony_ci dev_err(&client->dev, 2488c2ecf20Sopenharmony_ci "%s: Data read failed: %d\n", __func__, ret); 2498c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci switch (ret) { 2528c2ecf20Sopenharmony_ci case CM36651_CLOSE_PROXIMITY: 2538c2ecf20Sopenharmony_ci ev_dir = IIO_EV_DIR_RISING; 2548c2ecf20Sopenharmony_ci break; 2558c2ecf20Sopenharmony_ci case CM36651_FAR_PROXIMITY: 2568c2ecf20Sopenharmony_ci ev_dir = IIO_EV_DIR_FALLING; 2578c2ecf20Sopenharmony_ci break; 2588c2ecf20Sopenharmony_ci default: 2598c2ecf20Sopenharmony_ci dev_err(&client->dev, 2608c2ecf20Sopenharmony_ci "%s: Data read wrong: %d\n", __func__, ret); 2618c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci ev_code = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 2658c2ecf20Sopenharmony_ci CM36651_CMD_READ_RAW_PROXIMITY, 2668c2ecf20Sopenharmony_ci IIO_EV_TYPE_THRESH, ev_dir); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci iio_push_event(indio_dev, ev_code, iio_get_time_ns(indio_dev)); 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2718c2ecf20Sopenharmony_ci} 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_cistatic int cm36651_set_operation_mode(struct cm36651_data *cm36651, int cmd) 2748c2ecf20Sopenharmony_ci{ 2758c2ecf20Sopenharmony_ci struct i2c_client *client = cm36651->client; 2768c2ecf20Sopenharmony_ci struct i2c_client *ps_client = cm36651->ps_client; 2778c2ecf20Sopenharmony_ci int ret = -EINVAL; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci switch (cmd) { 2808c2ecf20Sopenharmony_ci case CM36651_CMD_READ_RAW_LIGHT: 2818c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1, 2828c2ecf20Sopenharmony_ci cm36651->cs_ctrl_regs[CM36651_CS_CONF1]); 2838c2ecf20Sopenharmony_ci break; 2848c2ecf20Sopenharmony_ci case CM36651_CMD_READ_RAW_PROXIMITY: 2858c2ecf20Sopenharmony_ci if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) 2868c2ecf20Sopenharmony_ci return CM36651_PROXIMITY_EV_EN; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(ps_client, CM36651_PS_CONF1, 2898c2ecf20Sopenharmony_ci cm36651->ps_ctrl_regs[CM36651_PS_CONF1]); 2908c2ecf20Sopenharmony_ci break; 2918c2ecf20Sopenharmony_ci case CM36651_CMD_PROX_EV_EN: 2928c2ecf20Sopenharmony_ci if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) { 2938c2ecf20Sopenharmony_ci dev_err(&client->dev, 2948c2ecf20Sopenharmony_ci "Already proximity event enable state\n"); 2958c2ecf20Sopenharmony_ci return ret; 2968c2ecf20Sopenharmony_ci } 2978c2ecf20Sopenharmony_ci set_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(ps_client, 3008c2ecf20Sopenharmony_ci cm36651_ps_reg[CM36651_PS_CONF1], 3018c2ecf20Sopenharmony_ci CM36651_PS_INT_EN | CM36651_PS_PERS2 | CM36651_PS_IT2); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci if (ret < 0) { 3048c2ecf20Sopenharmony_ci dev_err(&client->dev, "Proximity enable event failed\n"); 3058c2ecf20Sopenharmony_ci return ret; 3068c2ecf20Sopenharmony_ci } 3078c2ecf20Sopenharmony_ci break; 3088c2ecf20Sopenharmony_ci case CM36651_CMD_PROX_EV_DIS: 3098c2ecf20Sopenharmony_ci if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) { 3108c2ecf20Sopenharmony_ci dev_err(&client->dev, 3118c2ecf20Sopenharmony_ci "Already proximity event disable state\n"); 3128c2ecf20Sopenharmony_ci return ret; 3138c2ecf20Sopenharmony_ci } 3148c2ecf20Sopenharmony_ci clear_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags); 3158c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(ps_client, 3168c2ecf20Sopenharmony_ci CM36651_PS_CONF1, CM36651_PS_DISABLE); 3178c2ecf20Sopenharmony_ci break; 3188c2ecf20Sopenharmony_ci } 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci if (ret < 0) 3218c2ecf20Sopenharmony_ci dev_err(&client->dev, "Write register failed\n"); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci return ret; 3248c2ecf20Sopenharmony_ci} 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_cistatic int cm36651_read_channel(struct cm36651_data *cm36651, 3278c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, int *val) 3288c2ecf20Sopenharmony_ci{ 3298c2ecf20Sopenharmony_ci struct i2c_client *client = cm36651->client; 3308c2ecf20Sopenharmony_ci int cmd, ret; 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci if (chan->type == IIO_LIGHT) 3338c2ecf20Sopenharmony_ci cmd = CM36651_CMD_READ_RAW_LIGHT; 3348c2ecf20Sopenharmony_ci else if (chan->type == IIO_PROXIMITY) 3358c2ecf20Sopenharmony_ci cmd = CM36651_CMD_READ_RAW_PROXIMITY; 3368c2ecf20Sopenharmony_ci else 3378c2ecf20Sopenharmony_ci return -EINVAL; 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci ret = cm36651_set_operation_mode(cm36651, cmd); 3408c2ecf20Sopenharmony_ci if (ret < 0) { 3418c2ecf20Sopenharmony_ci dev_err(&client->dev, "CM36651 set operation mode failed\n"); 3428c2ecf20Sopenharmony_ci return ret; 3438c2ecf20Sopenharmony_ci } 3448c2ecf20Sopenharmony_ci /* Delay for work after enable operation */ 3458c2ecf20Sopenharmony_ci msleep(50); 3468c2ecf20Sopenharmony_ci ret = cm36651_read_output(cm36651, chan, val); 3478c2ecf20Sopenharmony_ci if (ret < 0) { 3488c2ecf20Sopenharmony_ci dev_err(&client->dev, "CM36651 read output failed\n"); 3498c2ecf20Sopenharmony_ci return ret; 3508c2ecf20Sopenharmony_ci } 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci return ret; 3538c2ecf20Sopenharmony_ci} 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_cistatic int cm36651_read_int_time(struct cm36651_data *cm36651, 3568c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, int *val2) 3578c2ecf20Sopenharmony_ci{ 3588c2ecf20Sopenharmony_ci switch (chan->type) { 3598c2ecf20Sopenharmony_ci case IIO_LIGHT: 3608c2ecf20Sopenharmony_ci if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT1) 3618c2ecf20Sopenharmony_ci *val2 = 80000; 3628c2ecf20Sopenharmony_ci else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT2) 3638c2ecf20Sopenharmony_ci *val2 = 160000; 3648c2ecf20Sopenharmony_ci else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT3) 3658c2ecf20Sopenharmony_ci *val2 = 320000; 3668c2ecf20Sopenharmony_ci else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT4) 3678c2ecf20Sopenharmony_ci *val2 = 640000; 3688c2ecf20Sopenharmony_ci else 3698c2ecf20Sopenharmony_ci return -EINVAL; 3708c2ecf20Sopenharmony_ci break; 3718c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 3728c2ecf20Sopenharmony_ci if (cm36651->ps_int_time == CM36651_PS_IT1) 3738c2ecf20Sopenharmony_ci *val2 = 320; 3748c2ecf20Sopenharmony_ci else if (cm36651->ps_int_time == CM36651_PS_IT2) 3758c2ecf20Sopenharmony_ci *val2 = 420; 3768c2ecf20Sopenharmony_ci else if (cm36651->ps_int_time == CM36651_PS_IT3) 3778c2ecf20Sopenharmony_ci *val2 = 520; 3788c2ecf20Sopenharmony_ci else if (cm36651->ps_int_time == CM36651_PS_IT4) 3798c2ecf20Sopenharmony_ci *val2 = 640; 3808c2ecf20Sopenharmony_ci else 3818c2ecf20Sopenharmony_ci return -EINVAL; 3828c2ecf20Sopenharmony_ci break; 3838c2ecf20Sopenharmony_ci default: 3848c2ecf20Sopenharmony_ci return -EINVAL; 3858c2ecf20Sopenharmony_ci } 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 3888c2ecf20Sopenharmony_ci} 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_cistatic int cm36651_write_int_time(struct cm36651_data *cm36651, 3918c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, int val) 3928c2ecf20Sopenharmony_ci{ 3938c2ecf20Sopenharmony_ci struct i2c_client *client = cm36651->client; 3948c2ecf20Sopenharmony_ci struct i2c_client *ps_client = cm36651->ps_client; 3958c2ecf20Sopenharmony_ci int int_time, ret; 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci switch (chan->type) { 3988c2ecf20Sopenharmony_ci case IIO_LIGHT: 3998c2ecf20Sopenharmony_ci if (val == 80000) 4008c2ecf20Sopenharmony_ci int_time = CM36651_CS_IT1; 4018c2ecf20Sopenharmony_ci else if (val == 160000) 4028c2ecf20Sopenharmony_ci int_time = CM36651_CS_IT2; 4038c2ecf20Sopenharmony_ci else if (val == 320000) 4048c2ecf20Sopenharmony_ci int_time = CM36651_CS_IT3; 4058c2ecf20Sopenharmony_ci else if (val == 640000) 4068c2ecf20Sopenharmony_ci int_time = CM36651_CS_IT4; 4078c2ecf20Sopenharmony_ci else 4088c2ecf20Sopenharmony_ci return -EINVAL; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF3, 4118c2ecf20Sopenharmony_ci int_time >> 2 * (chan->address)); 4128c2ecf20Sopenharmony_ci if (ret < 0) { 4138c2ecf20Sopenharmony_ci dev_err(&client->dev, "CS integration time write failed\n"); 4148c2ecf20Sopenharmony_ci return ret; 4158c2ecf20Sopenharmony_ci } 4168c2ecf20Sopenharmony_ci cm36651->cs_int_time[chan->address] = int_time; 4178c2ecf20Sopenharmony_ci break; 4188c2ecf20Sopenharmony_ci case IIO_PROXIMITY: 4198c2ecf20Sopenharmony_ci if (val == 320) 4208c2ecf20Sopenharmony_ci int_time = CM36651_PS_IT1; 4218c2ecf20Sopenharmony_ci else if (val == 420) 4228c2ecf20Sopenharmony_ci int_time = CM36651_PS_IT2; 4238c2ecf20Sopenharmony_ci else if (val == 520) 4248c2ecf20Sopenharmony_ci int_time = CM36651_PS_IT3; 4258c2ecf20Sopenharmony_ci else if (val == 640) 4268c2ecf20Sopenharmony_ci int_time = CM36651_PS_IT4; 4278c2ecf20Sopenharmony_ci else 4288c2ecf20Sopenharmony_ci return -EINVAL; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(ps_client, 4318c2ecf20Sopenharmony_ci CM36651_PS_CONF1, int_time); 4328c2ecf20Sopenharmony_ci if (ret < 0) { 4338c2ecf20Sopenharmony_ci dev_err(&client->dev, "PS integration time write failed\n"); 4348c2ecf20Sopenharmony_ci return ret; 4358c2ecf20Sopenharmony_ci } 4368c2ecf20Sopenharmony_ci cm36651->ps_int_time = int_time; 4378c2ecf20Sopenharmony_ci break; 4388c2ecf20Sopenharmony_ci default: 4398c2ecf20Sopenharmony_ci return -EINVAL; 4408c2ecf20Sopenharmony_ci } 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci return ret; 4438c2ecf20Sopenharmony_ci} 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_cistatic int cm36651_read_raw(struct iio_dev *indio_dev, 4468c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 4478c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci struct cm36651_data *cm36651 = iio_priv(indio_dev); 4508c2ecf20Sopenharmony_ci int ret; 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci mutex_lock(&cm36651->lock); 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci switch (mask) { 4558c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 4568c2ecf20Sopenharmony_ci ret = cm36651_read_channel(cm36651, chan, val); 4578c2ecf20Sopenharmony_ci break; 4588c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_INT_TIME: 4598c2ecf20Sopenharmony_ci *val = 0; 4608c2ecf20Sopenharmony_ci ret = cm36651_read_int_time(cm36651, chan, val2); 4618c2ecf20Sopenharmony_ci break; 4628c2ecf20Sopenharmony_ci default: 4638c2ecf20Sopenharmony_ci ret = -EINVAL; 4648c2ecf20Sopenharmony_ci } 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci mutex_unlock(&cm36651->lock); 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci return ret; 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_cistatic int cm36651_write_raw(struct iio_dev *indio_dev, 4728c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 4738c2ecf20Sopenharmony_ci int val, int val2, long mask) 4748c2ecf20Sopenharmony_ci{ 4758c2ecf20Sopenharmony_ci struct cm36651_data *cm36651 = iio_priv(indio_dev); 4768c2ecf20Sopenharmony_ci struct i2c_client *client = cm36651->client; 4778c2ecf20Sopenharmony_ci int ret = -EINVAL; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci if (mask == IIO_CHAN_INFO_INT_TIME) { 4808c2ecf20Sopenharmony_ci ret = cm36651_write_int_time(cm36651, chan, val2); 4818c2ecf20Sopenharmony_ci if (ret < 0) 4828c2ecf20Sopenharmony_ci dev_err(&client->dev, "Integration time write failed\n"); 4838c2ecf20Sopenharmony_ci } 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci return ret; 4868c2ecf20Sopenharmony_ci} 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_cistatic int cm36651_read_prox_thresh(struct iio_dev *indio_dev, 4898c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 4908c2ecf20Sopenharmony_ci enum iio_event_type type, 4918c2ecf20Sopenharmony_ci enum iio_event_direction dir, 4928c2ecf20Sopenharmony_ci enum iio_event_info info, 4938c2ecf20Sopenharmony_ci int *val, int *val2) 4948c2ecf20Sopenharmony_ci{ 4958c2ecf20Sopenharmony_ci struct cm36651_data *cm36651 = iio_priv(indio_dev); 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci *val = cm36651->ps_ctrl_regs[CM36651_PS_THD]; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci return 0; 5008c2ecf20Sopenharmony_ci} 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_cistatic int cm36651_write_prox_thresh(struct iio_dev *indio_dev, 5038c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 5048c2ecf20Sopenharmony_ci enum iio_event_type type, 5058c2ecf20Sopenharmony_ci enum iio_event_direction dir, 5068c2ecf20Sopenharmony_ci enum iio_event_info info, 5078c2ecf20Sopenharmony_ci int val, int val2) 5088c2ecf20Sopenharmony_ci{ 5098c2ecf20Sopenharmony_ci struct cm36651_data *cm36651 = iio_priv(indio_dev); 5108c2ecf20Sopenharmony_ci struct i2c_client *client = cm36651->client; 5118c2ecf20Sopenharmony_ci int ret; 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci if (val < 3 || val > 255) 5148c2ecf20Sopenharmony_ci return -EINVAL; 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_ci cm36651->ps_ctrl_regs[CM36651_PS_THD] = val; 5178c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte_data(cm36651->ps_client, CM36651_PS_THD, 5188c2ecf20Sopenharmony_ci cm36651->ps_ctrl_regs[CM36651_PS_THD]); 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci if (ret < 0) { 5218c2ecf20Sopenharmony_ci dev_err(&client->dev, "PS threshold write failed: %d\n", ret); 5228c2ecf20Sopenharmony_ci return ret; 5238c2ecf20Sopenharmony_ci } 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci return 0; 5268c2ecf20Sopenharmony_ci} 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_cistatic int cm36651_write_prox_event_config(struct iio_dev *indio_dev, 5298c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 5308c2ecf20Sopenharmony_ci enum iio_event_type type, 5318c2ecf20Sopenharmony_ci enum iio_event_direction dir, 5328c2ecf20Sopenharmony_ci int state) 5338c2ecf20Sopenharmony_ci{ 5348c2ecf20Sopenharmony_ci struct cm36651_data *cm36651 = iio_priv(indio_dev); 5358c2ecf20Sopenharmony_ci int cmd, ret; 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci mutex_lock(&cm36651->lock); 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci cmd = state ? CM36651_CMD_PROX_EV_EN : CM36651_CMD_PROX_EV_DIS; 5408c2ecf20Sopenharmony_ci ret = cm36651_set_operation_mode(cm36651, cmd); 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci mutex_unlock(&cm36651->lock); 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci return ret; 5458c2ecf20Sopenharmony_ci} 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_cistatic int cm36651_read_prox_event_config(struct iio_dev *indio_dev, 5488c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 5498c2ecf20Sopenharmony_ci enum iio_event_type type, 5508c2ecf20Sopenharmony_ci enum iio_event_direction dir) 5518c2ecf20Sopenharmony_ci{ 5528c2ecf20Sopenharmony_ci struct cm36651_data *cm36651 = iio_priv(indio_dev); 5538c2ecf20Sopenharmony_ci int event_en; 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci mutex_lock(&cm36651->lock); 5568c2ecf20Sopenharmony_ci 5578c2ecf20Sopenharmony_ci event_en = test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags); 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci mutex_unlock(&cm36651->lock); 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci return event_en; 5628c2ecf20Sopenharmony_ci} 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci#define CM36651_LIGHT_CHANNEL(_color, _idx) { \ 5658c2ecf20Sopenharmony_ci .type = IIO_LIGHT, \ 5668c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 5678c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_INT_TIME), \ 5688c2ecf20Sopenharmony_ci .address = _idx, \ 5698c2ecf20Sopenharmony_ci .modified = 1, \ 5708c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_LIGHT_##_color, \ 5718c2ecf20Sopenharmony_ci} \ 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_cistatic const struct iio_event_spec cm36651_event_spec[] = { 5748c2ecf20Sopenharmony_ci { 5758c2ecf20Sopenharmony_ci .type = IIO_EV_TYPE_THRESH, 5768c2ecf20Sopenharmony_ci .dir = IIO_EV_DIR_EITHER, 5778c2ecf20Sopenharmony_ci .mask_separate = BIT(IIO_EV_INFO_VALUE) | 5788c2ecf20Sopenharmony_ci BIT(IIO_EV_INFO_ENABLE), 5798c2ecf20Sopenharmony_ci } 5808c2ecf20Sopenharmony_ci}; 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_cistatic const struct iio_chan_spec cm36651_channels[] = { 5838c2ecf20Sopenharmony_ci { 5848c2ecf20Sopenharmony_ci .type = IIO_PROXIMITY, 5858c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 5868c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_INT_TIME), 5878c2ecf20Sopenharmony_ci .event_spec = cm36651_event_spec, 5888c2ecf20Sopenharmony_ci .num_event_specs = ARRAY_SIZE(cm36651_event_spec), 5898c2ecf20Sopenharmony_ci }, 5908c2ecf20Sopenharmony_ci CM36651_LIGHT_CHANNEL(RED, CM36651_LIGHT_CHANNEL_IDX_RED), 5918c2ecf20Sopenharmony_ci CM36651_LIGHT_CHANNEL(GREEN, CM36651_LIGHT_CHANNEL_IDX_GREEN), 5928c2ecf20Sopenharmony_ci CM36651_LIGHT_CHANNEL(BLUE, CM36651_LIGHT_CHANNEL_IDX_BLUE), 5938c2ecf20Sopenharmony_ci CM36651_LIGHT_CHANNEL(CLEAR, CM36651_LIGHT_CHANNEL_IDX_CLEAR), 5948c2ecf20Sopenharmony_ci}; 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(in_illuminance_integration_time_available, 5978c2ecf20Sopenharmony_ci CM36651_CS_INT_TIME_AVAIL); 5988c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(in_proximity_integration_time_available, 5998c2ecf20Sopenharmony_ci CM36651_PS_INT_TIME_AVAIL); 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_cistatic struct attribute *cm36651_attributes[] = { 6028c2ecf20Sopenharmony_ci &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr, 6038c2ecf20Sopenharmony_ci &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr, 6048c2ecf20Sopenharmony_ci NULL, 6058c2ecf20Sopenharmony_ci}; 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_cistatic const struct attribute_group cm36651_attribute_group = { 6088c2ecf20Sopenharmony_ci .attrs = cm36651_attributes 6098c2ecf20Sopenharmony_ci}; 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_cistatic const struct iio_info cm36651_info = { 6128c2ecf20Sopenharmony_ci .read_raw = &cm36651_read_raw, 6138c2ecf20Sopenharmony_ci .write_raw = &cm36651_write_raw, 6148c2ecf20Sopenharmony_ci .read_event_value = &cm36651_read_prox_thresh, 6158c2ecf20Sopenharmony_ci .write_event_value = &cm36651_write_prox_thresh, 6168c2ecf20Sopenharmony_ci .read_event_config = &cm36651_read_prox_event_config, 6178c2ecf20Sopenharmony_ci .write_event_config = &cm36651_write_prox_event_config, 6188c2ecf20Sopenharmony_ci .attrs = &cm36651_attribute_group, 6198c2ecf20Sopenharmony_ci}; 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_cistatic int cm36651_probe(struct i2c_client *client, 6228c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 6238c2ecf20Sopenharmony_ci{ 6248c2ecf20Sopenharmony_ci struct cm36651_data *cm36651; 6258c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 6268c2ecf20Sopenharmony_ci int ret; 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm36651)); 6298c2ecf20Sopenharmony_ci if (!indio_dev) 6308c2ecf20Sopenharmony_ci return -ENOMEM; 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci cm36651 = iio_priv(indio_dev); 6338c2ecf20Sopenharmony_ci 6348c2ecf20Sopenharmony_ci cm36651->vled_reg = devm_regulator_get(&client->dev, "vled"); 6358c2ecf20Sopenharmony_ci if (IS_ERR(cm36651->vled_reg)) { 6368c2ecf20Sopenharmony_ci dev_err(&client->dev, "get regulator vled failed\n"); 6378c2ecf20Sopenharmony_ci return PTR_ERR(cm36651->vled_reg); 6388c2ecf20Sopenharmony_ci } 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci ret = regulator_enable(cm36651->vled_reg); 6418c2ecf20Sopenharmony_ci if (ret) { 6428c2ecf20Sopenharmony_ci dev_err(&client->dev, "enable regulator vled failed\n"); 6438c2ecf20Sopenharmony_ci return ret; 6448c2ecf20Sopenharmony_ci } 6458c2ecf20Sopenharmony_ci 6468c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci cm36651->client = client; 6498c2ecf20Sopenharmony_ci cm36651->ps_client = i2c_new_dummy_device(client->adapter, 6508c2ecf20Sopenharmony_ci CM36651_I2C_ADDR_PS); 6518c2ecf20Sopenharmony_ci if (IS_ERR(cm36651->ps_client)) { 6528c2ecf20Sopenharmony_ci dev_err(&client->dev, "%s: new i2c device failed\n", __func__); 6538c2ecf20Sopenharmony_ci ret = PTR_ERR(cm36651->ps_client); 6548c2ecf20Sopenharmony_ci goto error_disable_reg; 6558c2ecf20Sopenharmony_ci } 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci cm36651->ara_client = i2c_new_dummy_device(client->adapter, CM36651_ARA); 6588c2ecf20Sopenharmony_ci if (IS_ERR(cm36651->ara_client)) { 6598c2ecf20Sopenharmony_ci dev_err(&client->dev, "%s: new i2c device failed\n", __func__); 6608c2ecf20Sopenharmony_ci ret = PTR_ERR(cm36651->ara_client); 6618c2ecf20Sopenharmony_ci goto error_i2c_unregister_ps; 6628c2ecf20Sopenharmony_ci } 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ci mutex_init(&cm36651->lock); 6658c2ecf20Sopenharmony_ci indio_dev->channels = cm36651_channels; 6668c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(cm36651_channels); 6678c2ecf20Sopenharmony_ci indio_dev->info = &cm36651_info; 6688c2ecf20Sopenharmony_ci indio_dev->name = id->name; 6698c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 6708c2ecf20Sopenharmony_ci 6718c2ecf20Sopenharmony_ci ret = cm36651_setup_reg(cm36651); 6728c2ecf20Sopenharmony_ci if (ret) { 6738c2ecf20Sopenharmony_ci dev_err(&client->dev, "%s: register setup failed\n", __func__); 6748c2ecf20Sopenharmony_ci goto error_i2c_unregister_ara; 6758c2ecf20Sopenharmony_ci } 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci ret = request_threaded_irq(client->irq, NULL, cm36651_irq_handler, 6788c2ecf20Sopenharmony_ci IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 6798c2ecf20Sopenharmony_ci "cm36651", indio_dev); 6808c2ecf20Sopenharmony_ci if (ret) { 6818c2ecf20Sopenharmony_ci dev_err(&client->dev, "%s: request irq failed\n", __func__); 6828c2ecf20Sopenharmony_ci goto error_i2c_unregister_ara; 6838c2ecf20Sopenharmony_ci } 6848c2ecf20Sopenharmony_ci 6858c2ecf20Sopenharmony_ci ret = iio_device_register(indio_dev); 6868c2ecf20Sopenharmony_ci if (ret) { 6878c2ecf20Sopenharmony_ci dev_err(&client->dev, "%s: regist device failed\n", __func__); 6888c2ecf20Sopenharmony_ci goto error_free_irq; 6898c2ecf20Sopenharmony_ci } 6908c2ecf20Sopenharmony_ci 6918c2ecf20Sopenharmony_ci return 0; 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_cierror_free_irq: 6948c2ecf20Sopenharmony_ci free_irq(client->irq, indio_dev); 6958c2ecf20Sopenharmony_cierror_i2c_unregister_ara: 6968c2ecf20Sopenharmony_ci i2c_unregister_device(cm36651->ara_client); 6978c2ecf20Sopenharmony_cierror_i2c_unregister_ps: 6988c2ecf20Sopenharmony_ci i2c_unregister_device(cm36651->ps_client); 6998c2ecf20Sopenharmony_cierror_disable_reg: 7008c2ecf20Sopenharmony_ci regulator_disable(cm36651->vled_reg); 7018c2ecf20Sopenharmony_ci return ret; 7028c2ecf20Sopenharmony_ci} 7038c2ecf20Sopenharmony_ci 7048c2ecf20Sopenharmony_cistatic int cm36651_remove(struct i2c_client *client) 7058c2ecf20Sopenharmony_ci{ 7068c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(client); 7078c2ecf20Sopenharmony_ci struct cm36651_data *cm36651 = iio_priv(indio_dev); 7088c2ecf20Sopenharmony_ci 7098c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 7108c2ecf20Sopenharmony_ci regulator_disable(cm36651->vled_reg); 7118c2ecf20Sopenharmony_ci free_irq(client->irq, indio_dev); 7128c2ecf20Sopenharmony_ci i2c_unregister_device(cm36651->ps_client); 7138c2ecf20Sopenharmony_ci i2c_unregister_device(cm36651->ara_client); 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci return 0; 7168c2ecf20Sopenharmony_ci} 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_cistatic const struct i2c_device_id cm36651_id[] = { 7198c2ecf20Sopenharmony_ci { "cm36651", 0 }, 7208c2ecf20Sopenharmony_ci { } 7218c2ecf20Sopenharmony_ci}; 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, cm36651_id); 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_cistatic const struct of_device_id cm36651_of_match[] = { 7268c2ecf20Sopenharmony_ci { .compatible = "capella,cm36651" }, 7278c2ecf20Sopenharmony_ci { } 7288c2ecf20Sopenharmony_ci}; 7298c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, cm36651_of_match); 7308c2ecf20Sopenharmony_ci 7318c2ecf20Sopenharmony_cistatic struct i2c_driver cm36651_driver = { 7328c2ecf20Sopenharmony_ci .driver = { 7338c2ecf20Sopenharmony_ci .name = "cm36651", 7348c2ecf20Sopenharmony_ci .of_match_table = cm36651_of_match, 7358c2ecf20Sopenharmony_ci }, 7368c2ecf20Sopenharmony_ci .probe = cm36651_probe, 7378c2ecf20Sopenharmony_ci .remove = cm36651_remove, 7388c2ecf20Sopenharmony_ci .id_table = cm36651_id, 7398c2ecf20Sopenharmony_ci}; 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_cimodule_i2c_driver(cm36651_driver); 7428c2ecf20Sopenharmony_ci 7438c2ecf20Sopenharmony_ciMODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>"); 7448c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CM36651 proximity/ambient light sensor driver"); 7458c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 746