Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
0004  * Author: Beomho Seo <beomho.seo@samsung.com>
0005  */
0006 
0007 #include <linux/delay.h>
0008 #include <linux/err.h>
0009 #include <linux/i2c.h>
0010 #include <linux/mutex.h>
0011 #include <linux/module.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/regulator/consumer.h>
0014 #include <linux/iio/iio.h>
0015 #include <linux/iio/sysfs.h>
0016 #include <linux/iio/events.h>
0017 
0018 /* Slave address 0x19 for PS of 7 bit addressing protocol for I2C */
0019 #define CM36651_I2C_ADDR_PS     0x19
0020 /* Alert Response Address */
0021 #define CM36651_ARA         0x0C
0022 
0023 /* Ambient light sensor */
0024 #define CM36651_CS_CONF1        0x00
0025 #define CM36651_CS_CONF2        0x01
0026 #define CM36651_ALS_WH_M        0x02
0027 #define CM36651_ALS_WH_L        0x03
0028 #define CM36651_ALS_WL_M        0x04
0029 #define CM36651_ALS_WL_L        0x05
0030 #define CM36651_CS_CONF3        0x06
0031 #define CM36651_CS_CONF_REG_NUM     0x02
0032 
0033 /* Proximity sensor */
0034 #define CM36651_PS_CONF1        0x00
0035 #define CM36651_PS_THD          0x01
0036 #define CM36651_PS_CANC         0x02
0037 #define CM36651_PS_CONF2        0x03
0038 #define CM36651_PS_REG_NUM      0x04
0039 
0040 /* CS_CONF1 command code */
0041 #define CM36651_ALS_ENABLE      0x00
0042 #define CM36651_ALS_DISABLE     0x01
0043 #define CM36651_ALS_INT_EN      0x02
0044 #define CM36651_ALS_THRES       0x04
0045 
0046 /* CS_CONF2 command code */
0047 #define CM36651_CS_CONF2_DEFAULT_BIT    0x08
0048 
0049 /* CS_CONF3 channel integration time */
0050 #define CM36651_CS_IT1          0x00 /* Integration time 80 msec */
0051 #define CM36651_CS_IT2          0x40 /* Integration time 160 msec */
0052 #define CM36651_CS_IT3          0x80 /* Integration time 320 msec */
0053 #define CM36651_CS_IT4          0xC0 /* Integration time 640 msec */
0054 
0055 /* PS_CONF1 command code */
0056 #define CM36651_PS_ENABLE       0x00
0057 #define CM36651_PS_DISABLE      0x01
0058 #define CM36651_PS_INT_EN       0x02
0059 #define CM36651_PS_PERS2        0x04
0060 #define CM36651_PS_PERS3        0x08
0061 #define CM36651_PS_PERS4        0x0C
0062 
0063 /* PS_CONF1 command code: integration time */
0064 #define CM36651_PS_IT1          0x00 /* Integration time 0.32 msec */
0065 #define CM36651_PS_IT2          0x10 /* Integration time 0.42 msec */
0066 #define CM36651_PS_IT3          0x20 /* Integration time 0.52 msec */
0067 #define CM36651_PS_IT4          0x30 /* Integration time 0.64 msec */
0068 
0069 /* PS_CONF1 command code: duty ratio */
0070 #define CM36651_PS_DR1          0x00 /* Duty ratio 1/80 */
0071 #define CM36651_PS_DR2          0x40 /* Duty ratio 1/160 */
0072 #define CM36651_PS_DR3          0x80 /* Duty ratio 1/320 */
0073 #define CM36651_PS_DR4          0xC0 /* Duty ratio 1/640 */
0074 
0075 /* PS_THD command code */
0076 #define CM36651_PS_INITIAL_THD      0x05
0077 
0078 /* PS_CANC command code */
0079 #define CM36651_PS_CANC_DEFAULT     0x00
0080 
0081 /* PS_CONF2 command code */
0082 #define CM36651_PS_HYS1         0x00
0083 #define CM36651_PS_HYS2         0x01
0084 #define CM36651_PS_SMART_PERS_EN    0x02
0085 #define CM36651_PS_DIR_INT      0x04
0086 #define CM36651_PS_MS           0x10
0087 
0088 #define CM36651_CS_COLOR_NUM        4
0089 
0090 #define CM36651_CLOSE_PROXIMITY     0x32
0091 #define CM36651_FAR_PROXIMITY           0x33
0092 
0093 #define CM36651_CS_INT_TIME_AVAIL   "0.08 0.16 0.32 0.64"
0094 #define CM36651_PS_INT_TIME_AVAIL   "0.000320 0.000420 0.000520 0.000640"
0095 
0096 enum cm36651_operation_mode {
0097     CM36651_LIGHT_EN,
0098     CM36651_PROXIMITY_EN,
0099     CM36651_PROXIMITY_EV_EN,
0100 };
0101 
0102 enum cm36651_light_channel_idx {
0103     CM36651_LIGHT_CHANNEL_IDX_RED,
0104     CM36651_LIGHT_CHANNEL_IDX_GREEN,
0105     CM36651_LIGHT_CHANNEL_IDX_BLUE,
0106     CM36651_LIGHT_CHANNEL_IDX_CLEAR,
0107 };
0108 
0109 enum cm36651_command {
0110     CM36651_CMD_READ_RAW_LIGHT,
0111     CM36651_CMD_READ_RAW_PROXIMITY,
0112     CM36651_CMD_PROX_EV_EN,
0113     CM36651_CMD_PROX_EV_DIS,
0114 };
0115 
0116 static const u8 cm36651_cs_reg[CM36651_CS_CONF_REG_NUM] = {
0117     CM36651_CS_CONF1,
0118     CM36651_CS_CONF2,
0119 };
0120 
0121 static const u8 cm36651_ps_reg[CM36651_PS_REG_NUM] = {
0122     CM36651_PS_CONF1,
0123     CM36651_PS_THD,
0124     CM36651_PS_CANC,
0125     CM36651_PS_CONF2,
0126 };
0127 
0128 struct cm36651_data {
0129     const struct cm36651_platform_data *pdata;
0130     struct i2c_client *client;
0131     struct i2c_client *ps_client;
0132     struct i2c_client *ara_client;
0133     struct mutex lock;
0134     struct regulator *vled_reg;
0135     unsigned long flags;
0136     int cs_int_time[CM36651_CS_COLOR_NUM];
0137     int ps_int_time;
0138     u8 cs_ctrl_regs[CM36651_CS_CONF_REG_NUM];
0139     u8 ps_ctrl_regs[CM36651_PS_REG_NUM];
0140     u16 color[CM36651_CS_COLOR_NUM];
0141 };
0142 
0143 static int cm36651_setup_reg(struct cm36651_data *cm36651)
0144 {
0145     struct i2c_client *client = cm36651->client;
0146     struct i2c_client *ps_client = cm36651->ps_client;
0147     int i, ret;
0148 
0149     /* CS initialization */
0150     cm36651->cs_ctrl_regs[CM36651_CS_CONF1] = CM36651_ALS_ENABLE |
0151                                  CM36651_ALS_THRES;
0152     cm36651->cs_ctrl_regs[CM36651_CS_CONF2] = CM36651_CS_CONF2_DEFAULT_BIT;
0153 
0154     for (i = 0; i < CM36651_CS_CONF_REG_NUM; i++) {
0155         ret = i2c_smbus_write_byte_data(client, cm36651_cs_reg[i],
0156                              cm36651->cs_ctrl_regs[i]);
0157         if (ret < 0)
0158             return ret;
0159     }
0160 
0161     /* PS initialization */
0162     cm36651->ps_ctrl_regs[CM36651_PS_CONF1] = CM36651_PS_ENABLE |
0163                                 CM36651_PS_IT2;
0164     cm36651->ps_ctrl_regs[CM36651_PS_THD] = CM36651_PS_INITIAL_THD;
0165     cm36651->ps_ctrl_regs[CM36651_PS_CANC] = CM36651_PS_CANC_DEFAULT;
0166     cm36651->ps_ctrl_regs[CM36651_PS_CONF2] = CM36651_PS_HYS2 |
0167                 CM36651_PS_DIR_INT | CM36651_PS_SMART_PERS_EN;
0168 
0169     for (i = 0; i < CM36651_PS_REG_NUM; i++) {
0170         ret = i2c_smbus_write_byte_data(ps_client, cm36651_ps_reg[i],
0171                              cm36651->ps_ctrl_regs[i]);
0172         if (ret < 0)
0173             return ret;
0174     }
0175 
0176     /* Set shutdown mode */
0177     ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
0178                               CM36651_ALS_DISABLE);
0179     if (ret < 0)
0180         return ret;
0181 
0182     ret = i2c_smbus_write_byte_data(cm36651->ps_client,
0183                      CM36651_PS_CONF1, CM36651_PS_DISABLE);
0184     if (ret < 0)
0185         return ret;
0186 
0187     return 0;
0188 }
0189 
0190 static int cm36651_read_output(struct cm36651_data *cm36651,
0191                 struct iio_chan_spec const *chan, int *val)
0192 {
0193     struct i2c_client *client = cm36651->client;
0194     int ret = -EINVAL;
0195 
0196     switch (chan->type) {
0197     case IIO_LIGHT:
0198         *val = i2c_smbus_read_word_data(client, chan->address);
0199         if (*val < 0)
0200             return ret;
0201 
0202         ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
0203                             CM36651_ALS_DISABLE);
0204         if (ret < 0)
0205             return ret;
0206 
0207         ret = IIO_VAL_INT;
0208         break;
0209     case IIO_PROXIMITY:
0210         *val = i2c_smbus_read_byte(cm36651->ps_client);
0211         if (*val < 0)
0212             return ret;
0213 
0214         if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
0215             ret = i2c_smbus_write_byte_data(cm36651->ps_client,
0216                     CM36651_PS_CONF1, CM36651_PS_DISABLE);
0217             if (ret < 0)
0218                 return ret;
0219         }
0220 
0221         ret = IIO_VAL_INT;
0222         break;
0223     default:
0224         break;
0225     }
0226 
0227     return ret;
0228 }
0229 
0230 static irqreturn_t cm36651_irq_handler(int irq, void *data)
0231 {
0232     struct iio_dev *indio_dev = data;
0233     struct cm36651_data *cm36651 = iio_priv(indio_dev);
0234     struct i2c_client *client = cm36651->client;
0235     int ev_dir, ret;
0236     u64 ev_code;
0237 
0238     /*
0239      * The PS INT pin is an active low signal that PS INT move logic low
0240      * when the object is detect. Once the MCU host received the PS INT
0241      * "LOW" signal, the Host needs to read the data at Alert Response
0242      * Address(ARA) to clear the PS INT signal. After clearing the PS
0243      * INT pin, the PS INT signal toggles from low to high.
0244      */
0245     ret = i2c_smbus_read_byte(cm36651->ara_client);
0246     if (ret < 0) {
0247         dev_err(&client->dev,
0248                 "%s: Data read failed: %d\n", __func__, ret);
0249         return IRQ_HANDLED;
0250     }
0251     switch (ret) {
0252     case CM36651_CLOSE_PROXIMITY:
0253         ev_dir = IIO_EV_DIR_RISING;
0254         break;
0255     case CM36651_FAR_PROXIMITY:
0256         ev_dir = IIO_EV_DIR_FALLING;
0257         break;
0258     default:
0259         dev_err(&client->dev,
0260             "%s: Data read wrong: %d\n", __func__, ret);
0261         return IRQ_HANDLED;
0262     }
0263 
0264     ev_code = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
0265                 CM36651_CMD_READ_RAW_PROXIMITY,
0266                 IIO_EV_TYPE_THRESH, ev_dir);
0267 
0268     iio_push_event(indio_dev, ev_code, iio_get_time_ns(indio_dev));
0269 
0270     return IRQ_HANDLED;
0271 }
0272 
0273 static int cm36651_set_operation_mode(struct cm36651_data *cm36651, int cmd)
0274 {
0275     struct i2c_client *client = cm36651->client;
0276     struct i2c_client *ps_client = cm36651->ps_client;
0277     int ret = -EINVAL;
0278 
0279     switch (cmd) {
0280     case CM36651_CMD_READ_RAW_LIGHT:
0281         ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
0282                 cm36651->cs_ctrl_regs[CM36651_CS_CONF1]);
0283         break;
0284     case CM36651_CMD_READ_RAW_PROXIMITY:
0285         if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags))
0286             return CM36651_PROXIMITY_EV_EN;
0287 
0288         ret = i2c_smbus_write_byte_data(ps_client, CM36651_PS_CONF1,
0289                 cm36651->ps_ctrl_regs[CM36651_PS_CONF1]);
0290         break;
0291     case CM36651_CMD_PROX_EV_EN:
0292         if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
0293             dev_err(&client->dev,
0294                 "Already proximity event enable state\n");
0295             return ret;
0296         }
0297         set_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
0298 
0299         ret = i2c_smbus_write_byte_data(ps_client,
0300             cm36651_ps_reg[CM36651_PS_CONF1],
0301             CM36651_PS_INT_EN | CM36651_PS_PERS2 | CM36651_PS_IT2);
0302 
0303         if (ret < 0) {
0304             dev_err(&client->dev, "Proximity enable event failed\n");
0305             return ret;
0306         }
0307         break;
0308     case CM36651_CMD_PROX_EV_DIS:
0309         if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
0310             dev_err(&client->dev,
0311                 "Already proximity event disable state\n");
0312             return ret;
0313         }
0314         clear_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
0315         ret = i2c_smbus_write_byte_data(ps_client,
0316                     CM36651_PS_CONF1, CM36651_PS_DISABLE);
0317         break;
0318     }
0319 
0320     if (ret < 0)
0321         dev_err(&client->dev, "Write register failed\n");
0322 
0323     return ret;
0324 }
0325 
0326 static int cm36651_read_channel(struct cm36651_data *cm36651,
0327                 struct iio_chan_spec const *chan, int *val)
0328 {
0329     struct i2c_client *client = cm36651->client;
0330     int cmd, ret;
0331 
0332     if (chan->type == IIO_LIGHT)
0333         cmd = CM36651_CMD_READ_RAW_LIGHT;
0334     else if (chan->type == IIO_PROXIMITY)
0335         cmd = CM36651_CMD_READ_RAW_PROXIMITY;
0336     else
0337         return -EINVAL;
0338 
0339     ret = cm36651_set_operation_mode(cm36651, cmd);
0340     if (ret < 0) {
0341         dev_err(&client->dev, "CM36651 set operation mode failed\n");
0342         return ret;
0343     }
0344     /* Delay for work after enable operation */
0345     msleep(50);
0346     ret = cm36651_read_output(cm36651, chan, val);
0347     if (ret < 0) {
0348         dev_err(&client->dev, "CM36651 read output failed\n");
0349         return ret;
0350     }
0351 
0352     return ret;
0353 }
0354 
0355 static int cm36651_read_int_time(struct cm36651_data *cm36651,
0356                 struct iio_chan_spec const *chan, int *val2)
0357 {
0358     switch (chan->type) {
0359     case IIO_LIGHT:
0360         if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT1)
0361             *val2 = 80000;
0362         else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT2)
0363             *val2 = 160000;
0364         else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT3)
0365             *val2 = 320000;
0366         else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT4)
0367             *val2 = 640000;
0368         else
0369             return -EINVAL;
0370         break;
0371     case IIO_PROXIMITY:
0372         if (cm36651->ps_int_time == CM36651_PS_IT1)
0373             *val2 = 320;
0374         else if (cm36651->ps_int_time == CM36651_PS_IT2)
0375             *val2 = 420;
0376         else if (cm36651->ps_int_time == CM36651_PS_IT3)
0377             *val2 = 520;
0378         else if (cm36651->ps_int_time == CM36651_PS_IT4)
0379             *val2 = 640;
0380         else
0381             return -EINVAL;
0382         break;
0383     default:
0384         return -EINVAL;
0385     }
0386 
0387     return IIO_VAL_INT_PLUS_MICRO;
0388 }
0389 
0390 static int cm36651_write_int_time(struct cm36651_data *cm36651,
0391                 struct iio_chan_spec const *chan, int val)
0392 {
0393     struct i2c_client *client = cm36651->client;
0394     struct i2c_client *ps_client = cm36651->ps_client;
0395     int int_time, ret;
0396 
0397     switch (chan->type) {
0398     case IIO_LIGHT:
0399         if (val == 80000)
0400             int_time = CM36651_CS_IT1;
0401         else if (val == 160000)
0402             int_time = CM36651_CS_IT2;
0403         else if (val == 320000)
0404             int_time = CM36651_CS_IT3;
0405         else if (val == 640000)
0406             int_time = CM36651_CS_IT4;
0407         else
0408             return -EINVAL;
0409 
0410         ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF3,
0411                        int_time >> 2 * (chan->address));
0412         if (ret < 0) {
0413             dev_err(&client->dev, "CS integration time write failed\n");
0414             return ret;
0415         }
0416         cm36651->cs_int_time[chan->address] = int_time;
0417         break;
0418     case IIO_PROXIMITY:
0419         if (val == 320)
0420             int_time = CM36651_PS_IT1;
0421         else if (val == 420)
0422             int_time = CM36651_PS_IT2;
0423         else if (val == 520)
0424             int_time = CM36651_PS_IT3;
0425         else if (val == 640)
0426             int_time = CM36651_PS_IT4;
0427         else
0428             return -EINVAL;
0429 
0430         ret = i2c_smbus_write_byte_data(ps_client,
0431                         CM36651_PS_CONF1, int_time);
0432         if (ret < 0) {
0433             dev_err(&client->dev, "PS integration time write failed\n");
0434             return ret;
0435         }
0436         cm36651->ps_int_time = int_time;
0437         break;
0438     default:
0439         return -EINVAL;
0440     }
0441 
0442     return ret;
0443 }
0444 
0445 static int cm36651_read_raw(struct iio_dev *indio_dev,
0446                 struct iio_chan_spec const *chan,
0447                 int *val, int *val2, long mask)
0448 {
0449     struct cm36651_data *cm36651 = iio_priv(indio_dev);
0450     int ret;
0451 
0452     mutex_lock(&cm36651->lock);
0453 
0454     switch (mask) {
0455     case IIO_CHAN_INFO_RAW:
0456         ret = cm36651_read_channel(cm36651, chan, val);
0457         break;
0458     case IIO_CHAN_INFO_INT_TIME:
0459         *val = 0;
0460         ret = cm36651_read_int_time(cm36651, chan, val2);
0461         break;
0462     default:
0463         ret = -EINVAL;
0464     }
0465 
0466     mutex_unlock(&cm36651->lock);
0467 
0468     return ret;
0469 }
0470 
0471 static int cm36651_write_raw(struct iio_dev *indio_dev,
0472                  struct iio_chan_spec const *chan,
0473                  int val, int val2, long mask)
0474 {
0475     struct cm36651_data *cm36651 = iio_priv(indio_dev);
0476     struct i2c_client *client = cm36651->client;
0477     int ret = -EINVAL;
0478 
0479     if (mask == IIO_CHAN_INFO_INT_TIME) {
0480         ret = cm36651_write_int_time(cm36651, chan, val2);
0481         if (ret < 0)
0482             dev_err(&client->dev, "Integration time write failed\n");
0483     }
0484 
0485     return ret;
0486 }
0487 
0488 static int cm36651_read_prox_thresh(struct iio_dev *indio_dev,
0489                     const struct iio_chan_spec *chan,
0490                     enum iio_event_type type,
0491                     enum iio_event_direction dir,
0492                     enum iio_event_info info,
0493                     int *val, int *val2)
0494 {
0495     struct cm36651_data *cm36651 = iio_priv(indio_dev);
0496 
0497     *val = cm36651->ps_ctrl_regs[CM36651_PS_THD];
0498 
0499     return 0;
0500 }
0501 
0502 static int cm36651_write_prox_thresh(struct iio_dev *indio_dev,
0503                     const struct iio_chan_spec *chan,
0504                     enum iio_event_type type,
0505                     enum iio_event_direction dir,
0506                     enum iio_event_info info,
0507                     int val, int val2)
0508 {
0509     struct cm36651_data *cm36651 = iio_priv(indio_dev);
0510     struct i2c_client *client = cm36651->client;
0511     int ret;
0512 
0513     if (val < 3 || val > 255)
0514         return -EINVAL;
0515 
0516     cm36651->ps_ctrl_regs[CM36651_PS_THD] = val;
0517     ret = i2c_smbus_write_byte_data(cm36651->ps_client, CM36651_PS_THD,
0518                     cm36651->ps_ctrl_regs[CM36651_PS_THD]);
0519 
0520     if (ret < 0) {
0521         dev_err(&client->dev, "PS threshold write failed: %d\n", ret);
0522         return ret;
0523     }
0524 
0525     return 0;
0526 }
0527 
0528 static int cm36651_write_prox_event_config(struct iio_dev *indio_dev,
0529                     const struct iio_chan_spec *chan,
0530                     enum iio_event_type type,
0531                     enum iio_event_direction dir,
0532                     int state)
0533 {
0534     struct cm36651_data *cm36651 = iio_priv(indio_dev);
0535     int cmd, ret;
0536 
0537     mutex_lock(&cm36651->lock);
0538 
0539     cmd = state ? CM36651_CMD_PROX_EV_EN : CM36651_CMD_PROX_EV_DIS;
0540     ret = cm36651_set_operation_mode(cm36651, cmd);
0541 
0542     mutex_unlock(&cm36651->lock);
0543 
0544     return ret;
0545 }
0546 
0547 static int cm36651_read_prox_event_config(struct iio_dev *indio_dev,
0548                     const struct iio_chan_spec *chan,
0549                     enum iio_event_type type,
0550                     enum iio_event_direction dir)
0551 {
0552     struct cm36651_data *cm36651 = iio_priv(indio_dev);
0553     int event_en;
0554 
0555     mutex_lock(&cm36651->lock);
0556 
0557     event_en = test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
0558 
0559     mutex_unlock(&cm36651->lock);
0560 
0561     return event_en;
0562 }
0563 
0564 #define CM36651_LIGHT_CHANNEL(_color, _idx) {       \
0565     .type = IIO_LIGHT,              \
0566     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |  \
0567             BIT(IIO_CHAN_INFO_INT_TIME),    \
0568     .address = _idx,                \
0569     .modified = 1,                  \
0570     .channel2 = IIO_MOD_LIGHT_##_color,     \
0571 }                           \
0572 
0573 static const struct iio_event_spec cm36651_event_spec[] = {
0574     {
0575         .type = IIO_EV_TYPE_THRESH,
0576         .dir = IIO_EV_DIR_EITHER,
0577         .mask_separate = BIT(IIO_EV_INFO_VALUE) |
0578                 BIT(IIO_EV_INFO_ENABLE),
0579     }
0580 };
0581 
0582 static const struct iio_chan_spec cm36651_channels[] = {
0583     {
0584         .type = IIO_PROXIMITY,
0585         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0586                 BIT(IIO_CHAN_INFO_INT_TIME),
0587         .event_spec = cm36651_event_spec,
0588         .num_event_specs = ARRAY_SIZE(cm36651_event_spec),
0589     },
0590     CM36651_LIGHT_CHANNEL(RED, CM36651_LIGHT_CHANNEL_IDX_RED),
0591     CM36651_LIGHT_CHANNEL(GREEN, CM36651_LIGHT_CHANNEL_IDX_GREEN),
0592     CM36651_LIGHT_CHANNEL(BLUE, CM36651_LIGHT_CHANNEL_IDX_BLUE),
0593     CM36651_LIGHT_CHANNEL(CLEAR, CM36651_LIGHT_CHANNEL_IDX_CLEAR),
0594 };
0595 
0596 static IIO_CONST_ATTR(in_illuminance_integration_time_available,
0597                     CM36651_CS_INT_TIME_AVAIL);
0598 static IIO_CONST_ATTR(in_proximity_integration_time_available,
0599                     CM36651_PS_INT_TIME_AVAIL);
0600 
0601 static struct attribute *cm36651_attributes[] = {
0602     &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
0603     &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
0604     NULL,
0605 };
0606 
0607 static const struct attribute_group cm36651_attribute_group = {
0608     .attrs = cm36651_attributes
0609 };
0610 
0611 static const struct iio_info cm36651_info = {
0612     .read_raw       = &cm36651_read_raw,
0613     .write_raw      = &cm36651_write_raw,
0614     .read_event_value   = &cm36651_read_prox_thresh,
0615     .write_event_value  = &cm36651_write_prox_thresh,
0616     .read_event_config  = &cm36651_read_prox_event_config,
0617     .write_event_config = &cm36651_write_prox_event_config,
0618     .attrs          = &cm36651_attribute_group,
0619 };
0620 
0621 static int cm36651_probe(struct i2c_client *client,
0622                  const struct i2c_device_id *id)
0623 {
0624     struct cm36651_data *cm36651;
0625     struct iio_dev *indio_dev;
0626     int ret;
0627 
0628     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm36651));
0629     if (!indio_dev)
0630         return -ENOMEM;
0631 
0632     cm36651 = iio_priv(indio_dev);
0633 
0634     cm36651->vled_reg = devm_regulator_get(&client->dev, "vled");
0635     if (IS_ERR(cm36651->vled_reg))
0636         return dev_err_probe(&client->dev, PTR_ERR(cm36651->vled_reg),
0637                      "get regulator vled failed\n");
0638 
0639     ret = regulator_enable(cm36651->vled_reg);
0640     if (ret) {
0641         dev_err(&client->dev, "enable regulator vled failed\n");
0642         return ret;
0643     }
0644 
0645     i2c_set_clientdata(client, indio_dev);
0646 
0647     cm36651->client = client;
0648     cm36651->ps_client = i2c_new_dummy_device(client->adapter,
0649                              CM36651_I2C_ADDR_PS);
0650     if (IS_ERR(cm36651->ps_client)) {
0651         dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
0652         ret = PTR_ERR(cm36651->ps_client);
0653         goto error_disable_reg;
0654     }
0655 
0656     cm36651->ara_client = i2c_new_dummy_device(client->adapter, CM36651_ARA);
0657     if (IS_ERR(cm36651->ara_client)) {
0658         dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
0659         ret = PTR_ERR(cm36651->ara_client);
0660         goto error_i2c_unregister_ps;
0661     }
0662 
0663     mutex_init(&cm36651->lock);
0664     indio_dev->channels = cm36651_channels;
0665     indio_dev->num_channels = ARRAY_SIZE(cm36651_channels);
0666     indio_dev->info = &cm36651_info;
0667     indio_dev->name = id->name;
0668     indio_dev->modes = INDIO_DIRECT_MODE;
0669 
0670     ret = cm36651_setup_reg(cm36651);
0671     if (ret) {
0672         dev_err(&client->dev, "%s: register setup failed\n", __func__);
0673         goto error_i2c_unregister_ara;
0674     }
0675 
0676     ret = request_threaded_irq(client->irq, NULL, cm36651_irq_handler,
0677                     IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
0678                             "cm36651", indio_dev);
0679     if (ret) {
0680         dev_err(&client->dev, "%s: request irq failed\n", __func__);
0681         goto error_i2c_unregister_ara;
0682     }
0683 
0684     ret = iio_device_register(indio_dev);
0685     if (ret) {
0686         dev_err(&client->dev, "%s: regist device failed\n", __func__);
0687         goto error_free_irq;
0688     }
0689 
0690     return 0;
0691 
0692 error_free_irq:
0693     free_irq(client->irq, indio_dev);
0694 error_i2c_unregister_ara:
0695     i2c_unregister_device(cm36651->ara_client);
0696 error_i2c_unregister_ps:
0697     i2c_unregister_device(cm36651->ps_client);
0698 error_disable_reg:
0699     regulator_disable(cm36651->vled_reg);
0700     return ret;
0701 }
0702 
0703 static int cm36651_remove(struct i2c_client *client)
0704 {
0705     struct iio_dev *indio_dev = i2c_get_clientdata(client);
0706     struct cm36651_data *cm36651 = iio_priv(indio_dev);
0707 
0708     iio_device_unregister(indio_dev);
0709     regulator_disable(cm36651->vled_reg);
0710     free_irq(client->irq, indio_dev);
0711     i2c_unregister_device(cm36651->ps_client);
0712     i2c_unregister_device(cm36651->ara_client);
0713 
0714     return 0;
0715 }
0716 
0717 static const struct i2c_device_id cm36651_id[] = {
0718     { "cm36651", 0 },
0719     { }
0720 };
0721 
0722 MODULE_DEVICE_TABLE(i2c, cm36651_id);
0723 
0724 static const struct of_device_id cm36651_of_match[] = {
0725     { .compatible = "capella,cm36651" },
0726     { }
0727 };
0728 MODULE_DEVICE_TABLE(of, cm36651_of_match);
0729 
0730 static struct i2c_driver cm36651_driver = {
0731     .driver = {
0732         .name   = "cm36651",
0733         .of_match_table = cm36651_of_match,
0734     },
0735     .probe      = cm36651_probe,
0736     .remove     = cm36651_remove,
0737     .id_table   = cm36651_id,
0738 };
0739 
0740 module_i2c_driver(cm36651_driver);
0741 
0742 MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
0743 MODULE_DESCRIPTION("CM36651 proximity/ambient light sensor driver");
0744 MODULE_LICENSE("GPL v2");