Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Sensortek STK3310/STK3311 Ambient Light and Proximity Sensor
0004  *
0005  * Copyright (c) 2015, Intel Corporation.
0006  *
0007  * IIO driver for STK3310/STK3311. 7-bit I2C address: 0x48.
0008  */
0009 
0010 #include <linux/acpi.h>
0011 #include <linux/i2c.h>
0012 #include <linux/interrupt.h>
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/regmap.h>
0016 #include <linux/iio/events.h>
0017 #include <linux/iio/iio.h>
0018 #include <linux/iio/sysfs.h>
0019 
0020 #define STK3310_REG_STATE           0x00
0021 #define STK3310_REG_PSCTRL          0x01
0022 #define STK3310_REG_ALSCTRL         0x02
0023 #define STK3310_REG_INT             0x04
0024 #define STK3310_REG_THDH_PS         0x06
0025 #define STK3310_REG_THDL_PS         0x08
0026 #define STK3310_REG_FLAG            0x10
0027 #define STK3310_REG_PS_DATA_MSB         0x11
0028 #define STK3310_REG_PS_DATA_LSB         0x12
0029 #define STK3310_REG_ALS_DATA_MSB        0x13
0030 #define STK3310_REG_ALS_DATA_LSB        0x14
0031 #define STK3310_REG_ID              0x3E
0032 #define STK3310_MAX_REG             0x80
0033 
0034 #define STK3310_STATE_EN_PS         BIT(0)
0035 #define STK3310_STATE_EN_ALS            BIT(1)
0036 #define STK3310_STATE_STANDBY           0x00
0037 
0038 #define STK3310_CHIP_ID_VAL         0x13
0039 #define STK3311_CHIP_ID_VAL         0x1D
0040 #define STK3311X_CHIP_ID_VAL            0x12
0041 #define STK3335_CHIP_ID_VAL         0x51
0042 #define STK3310_PSINT_EN            0x01
0043 #define STK3310_PS_MAX_VAL          0xFFFF
0044 
0045 #define STK3310_DRIVER_NAME         "stk3310"
0046 #define STK3310_REGMAP_NAME         "stk3310_regmap"
0047 #define STK3310_EVENT               "stk3310_event"
0048 
0049 #define STK3310_SCALE_AVAILABLE         "6.4 1.6 0.4 0.1"
0050 
0051 #define STK3310_IT_AVAILABLE \
0052     "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \
0053     "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \
0054     "3.031040 6.062080"
0055 
0056 #define STK3310_REGFIELD(name)                          \
0057     do {                                    \
0058         data->reg_##name =                      \
0059             devm_regmap_field_alloc(&client->dev, regmap,       \
0060                 stk3310_reg_field_##name);          \
0061         if (IS_ERR(data->reg_##name)) {                 \
0062             dev_err(&client->dev, "reg field alloc failed.\n"); \
0063             return PTR_ERR(data->reg_##name);           \
0064         }                               \
0065     } while (0)
0066 
0067 static const struct reg_field stk3310_reg_field_state =
0068                 REG_FIELD(STK3310_REG_STATE, 0, 2);
0069 static const struct reg_field stk3310_reg_field_als_gain =
0070                 REG_FIELD(STK3310_REG_ALSCTRL, 4, 5);
0071 static const struct reg_field stk3310_reg_field_ps_gain =
0072                 REG_FIELD(STK3310_REG_PSCTRL, 4, 5);
0073 static const struct reg_field stk3310_reg_field_als_it =
0074                 REG_FIELD(STK3310_REG_ALSCTRL, 0, 3);
0075 static const struct reg_field stk3310_reg_field_ps_it =
0076                 REG_FIELD(STK3310_REG_PSCTRL, 0, 3);
0077 static const struct reg_field stk3310_reg_field_int_ps =
0078                 REG_FIELD(STK3310_REG_INT, 0, 2);
0079 static const struct reg_field stk3310_reg_field_flag_psint =
0080                 REG_FIELD(STK3310_REG_FLAG, 4, 4);
0081 static const struct reg_field stk3310_reg_field_flag_nf =
0082                 REG_FIELD(STK3310_REG_FLAG, 0, 0);
0083 
0084 /* Estimate maximum proximity values with regard to measurement scale. */
0085 static const int stk3310_ps_max[4] = {
0086     STK3310_PS_MAX_VAL / 640,
0087     STK3310_PS_MAX_VAL / 160,
0088     STK3310_PS_MAX_VAL /  40,
0089     STK3310_PS_MAX_VAL /  10
0090 };
0091 
0092 static const int stk3310_scale_table[][2] = {
0093     {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
0094 };
0095 
0096 /* Integration time in seconds, microseconds */
0097 static const int stk3310_it_table[][2] = {
0098     {0, 185},   {0, 370},   {0, 741},   {0, 1480},
0099     {0, 2960},  {0, 5920},  {0, 11840}, {0, 23680},
0100     {0, 47360}, {0, 94720}, {0, 189440},    {0, 378880},
0101     {0, 757760},    {1, 515520},    {3, 31040}, {6, 62080},
0102 };
0103 
0104 struct stk3310_data {
0105     struct i2c_client *client;
0106     struct mutex lock;
0107     bool als_enabled;
0108     bool ps_enabled;
0109     uint32_t ps_near_level;
0110     u64 timestamp;
0111     struct regmap *regmap;
0112     struct regmap_field *reg_state;
0113     struct regmap_field *reg_als_gain;
0114     struct regmap_field *reg_ps_gain;
0115     struct regmap_field *reg_als_it;
0116     struct regmap_field *reg_ps_it;
0117     struct regmap_field *reg_int_ps;
0118     struct regmap_field *reg_flag_psint;
0119     struct regmap_field *reg_flag_nf;
0120 };
0121 
0122 static const struct iio_event_spec stk3310_events[] = {
0123     /* Proximity event */
0124     {
0125         .type = IIO_EV_TYPE_THRESH,
0126         .dir = IIO_EV_DIR_RISING,
0127         .mask_separate = BIT(IIO_EV_INFO_VALUE) |
0128                  BIT(IIO_EV_INFO_ENABLE),
0129     },
0130     /* Out-of-proximity event */
0131     {
0132         .type = IIO_EV_TYPE_THRESH,
0133         .dir = IIO_EV_DIR_FALLING,
0134         .mask_separate = BIT(IIO_EV_INFO_VALUE) |
0135                  BIT(IIO_EV_INFO_ENABLE),
0136     },
0137 };
0138 
0139 static ssize_t stk3310_read_near_level(struct iio_dev *indio_dev,
0140                        uintptr_t priv,
0141                        const struct iio_chan_spec *chan,
0142                        char *buf)
0143 {
0144     struct stk3310_data *data = iio_priv(indio_dev);
0145 
0146     return sprintf(buf, "%u\n", data->ps_near_level);
0147 }
0148 
0149 static const struct iio_chan_spec_ext_info stk3310_ext_info[] = {
0150     {
0151         .name = "nearlevel",
0152         .shared = IIO_SEPARATE,
0153         .read = stk3310_read_near_level,
0154     },
0155     { /* sentinel */ }
0156 };
0157 
0158 static const struct iio_chan_spec stk3310_channels[] = {
0159     {
0160         .type = IIO_LIGHT,
0161         .info_mask_separate =
0162             BIT(IIO_CHAN_INFO_RAW) |
0163             BIT(IIO_CHAN_INFO_SCALE) |
0164             BIT(IIO_CHAN_INFO_INT_TIME),
0165     },
0166     {
0167         .type = IIO_PROXIMITY,
0168         .info_mask_separate =
0169             BIT(IIO_CHAN_INFO_RAW) |
0170             BIT(IIO_CHAN_INFO_SCALE) |
0171             BIT(IIO_CHAN_INFO_INT_TIME),
0172         .event_spec = stk3310_events,
0173         .num_event_specs = ARRAY_SIZE(stk3310_events),
0174         .ext_info = stk3310_ext_info,
0175     }
0176 };
0177 
0178 static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE);
0179 
0180 static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE);
0181 
0182 static IIO_CONST_ATTR(in_illuminance_integration_time_available,
0183               STK3310_IT_AVAILABLE);
0184 
0185 static IIO_CONST_ATTR(in_proximity_integration_time_available,
0186               STK3310_IT_AVAILABLE);
0187 
0188 static struct attribute *stk3310_attributes[] = {
0189     &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
0190     &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
0191     &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
0192     &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
0193     NULL,
0194 };
0195 
0196 static const struct attribute_group stk3310_attribute_group = {
0197     .attrs = stk3310_attributes
0198 };
0199 
0200 static int stk3310_get_index(const int table[][2], int table_size,
0201                  int val, int val2)
0202 {
0203     int i;
0204 
0205     for (i = 0; i < table_size; i++) {
0206         if (val == table[i][0] && val2 == table[i][1])
0207             return i;
0208     }
0209 
0210     return -EINVAL;
0211 }
0212 
0213 static int stk3310_read_event(struct iio_dev *indio_dev,
0214                   const struct iio_chan_spec *chan,
0215                   enum iio_event_type type,
0216                   enum iio_event_direction dir,
0217                   enum iio_event_info info,
0218                   int *val, int *val2)
0219 {
0220     u8 reg;
0221     __be16 buf;
0222     int ret;
0223     struct stk3310_data *data = iio_priv(indio_dev);
0224 
0225     if (info != IIO_EV_INFO_VALUE)
0226         return -EINVAL;
0227 
0228     /* Only proximity interrupts are implemented at the moment. */
0229     if (dir == IIO_EV_DIR_RISING)
0230         reg = STK3310_REG_THDH_PS;
0231     else if (dir == IIO_EV_DIR_FALLING)
0232         reg = STK3310_REG_THDL_PS;
0233     else
0234         return -EINVAL;
0235 
0236     mutex_lock(&data->lock);
0237     ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
0238     mutex_unlock(&data->lock);
0239     if (ret < 0) {
0240         dev_err(&data->client->dev, "register read failed\n");
0241         return ret;
0242     }
0243     *val = be16_to_cpu(buf);
0244 
0245     return IIO_VAL_INT;
0246 }
0247 
0248 static int stk3310_write_event(struct iio_dev *indio_dev,
0249                    const struct iio_chan_spec *chan,
0250                    enum iio_event_type type,
0251                    enum iio_event_direction dir,
0252                    enum iio_event_info info,
0253                    int val, int val2)
0254 {
0255     u8 reg;
0256     __be16 buf;
0257     int ret;
0258     unsigned int index;
0259     struct stk3310_data *data = iio_priv(indio_dev);
0260     struct i2c_client *client = data->client;
0261 
0262     ret = regmap_field_read(data->reg_ps_gain, &index);
0263     if (ret < 0)
0264         return ret;
0265 
0266     if (val < 0 || val > stk3310_ps_max[index])
0267         return -EINVAL;
0268 
0269     if (dir == IIO_EV_DIR_RISING)
0270         reg = STK3310_REG_THDH_PS;
0271     else if (dir == IIO_EV_DIR_FALLING)
0272         reg = STK3310_REG_THDL_PS;
0273     else
0274         return -EINVAL;
0275 
0276     buf = cpu_to_be16(val);
0277     ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
0278     if (ret < 0)
0279         dev_err(&client->dev, "failed to set PS threshold!\n");
0280 
0281     return ret;
0282 }
0283 
0284 static int stk3310_read_event_config(struct iio_dev *indio_dev,
0285                      const struct iio_chan_spec *chan,
0286                      enum iio_event_type type,
0287                      enum iio_event_direction dir)
0288 {
0289     unsigned int event_val;
0290     int ret;
0291     struct stk3310_data *data = iio_priv(indio_dev);
0292 
0293     ret = regmap_field_read(data->reg_int_ps, &event_val);
0294     if (ret < 0)
0295         return ret;
0296 
0297     return event_val;
0298 }
0299 
0300 static int stk3310_write_event_config(struct iio_dev *indio_dev,
0301                       const struct iio_chan_spec *chan,
0302                       enum iio_event_type type,
0303                       enum iio_event_direction dir,
0304                       int state)
0305 {
0306     int ret;
0307     struct stk3310_data *data = iio_priv(indio_dev);
0308     struct i2c_client *client = data->client;
0309 
0310     if (state < 0 || state > 7)
0311         return -EINVAL;
0312 
0313     /* Set INT_PS value */
0314     mutex_lock(&data->lock);
0315     ret = regmap_field_write(data->reg_int_ps, state);
0316     if (ret < 0)
0317         dev_err(&client->dev, "failed to set interrupt mode\n");
0318     mutex_unlock(&data->lock);
0319 
0320     return ret;
0321 }
0322 
0323 static int stk3310_read_raw(struct iio_dev *indio_dev,
0324                 struct iio_chan_spec const *chan,
0325                 int *val, int *val2, long mask)
0326 {
0327     u8 reg;
0328     __be16 buf;
0329     int ret;
0330     unsigned int index;
0331     struct stk3310_data *data = iio_priv(indio_dev);
0332     struct i2c_client *client = data->client;
0333 
0334     if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
0335         return -EINVAL;
0336 
0337     switch (mask) {
0338     case IIO_CHAN_INFO_RAW:
0339         if (chan->type == IIO_LIGHT)
0340             reg = STK3310_REG_ALS_DATA_MSB;
0341         else
0342             reg = STK3310_REG_PS_DATA_MSB;
0343 
0344         mutex_lock(&data->lock);
0345         ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
0346         if (ret < 0) {
0347             dev_err(&client->dev, "register read failed\n");
0348             mutex_unlock(&data->lock);
0349             return ret;
0350         }
0351         *val = be16_to_cpu(buf);
0352         mutex_unlock(&data->lock);
0353         return IIO_VAL_INT;
0354     case IIO_CHAN_INFO_INT_TIME:
0355         if (chan->type == IIO_LIGHT)
0356             ret = regmap_field_read(data->reg_als_it, &index);
0357         else
0358             ret = regmap_field_read(data->reg_ps_it, &index);
0359         if (ret < 0)
0360             return ret;
0361 
0362         *val = stk3310_it_table[index][0];
0363         *val2 = stk3310_it_table[index][1];
0364         return IIO_VAL_INT_PLUS_MICRO;
0365     case IIO_CHAN_INFO_SCALE:
0366         if (chan->type == IIO_LIGHT)
0367             ret = regmap_field_read(data->reg_als_gain, &index);
0368         else
0369             ret = regmap_field_read(data->reg_ps_gain, &index);
0370         if (ret < 0)
0371             return ret;
0372 
0373         *val = stk3310_scale_table[index][0];
0374         *val2 = stk3310_scale_table[index][1];
0375         return IIO_VAL_INT_PLUS_MICRO;
0376     }
0377 
0378     return -EINVAL;
0379 }
0380 
0381 static int stk3310_write_raw(struct iio_dev *indio_dev,
0382                  struct iio_chan_spec const *chan,
0383                  int val, int val2, long mask)
0384 {
0385     int ret;
0386     int index;
0387     struct stk3310_data *data = iio_priv(indio_dev);
0388 
0389     if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
0390         return -EINVAL;
0391 
0392     switch (mask) {
0393     case IIO_CHAN_INFO_INT_TIME:
0394         index = stk3310_get_index(stk3310_it_table,
0395                       ARRAY_SIZE(stk3310_it_table),
0396                       val, val2);
0397         if (index < 0)
0398             return -EINVAL;
0399         mutex_lock(&data->lock);
0400         if (chan->type == IIO_LIGHT)
0401             ret = regmap_field_write(data->reg_als_it, index);
0402         else
0403             ret = regmap_field_write(data->reg_ps_it, index);
0404         if (ret < 0)
0405             dev_err(&data->client->dev,
0406                 "sensor configuration failed\n");
0407         mutex_unlock(&data->lock);
0408         return ret;
0409 
0410     case IIO_CHAN_INFO_SCALE:
0411         index = stk3310_get_index(stk3310_scale_table,
0412                       ARRAY_SIZE(stk3310_scale_table),
0413                       val, val2);
0414         if (index < 0)
0415             return -EINVAL;
0416         mutex_lock(&data->lock);
0417         if (chan->type == IIO_LIGHT)
0418             ret = regmap_field_write(data->reg_als_gain, index);
0419         else
0420             ret = regmap_field_write(data->reg_ps_gain, index);
0421         if (ret < 0)
0422             dev_err(&data->client->dev,
0423                 "sensor configuration failed\n");
0424         mutex_unlock(&data->lock);
0425         return ret;
0426     }
0427 
0428     return -EINVAL;
0429 }
0430 
0431 static const struct iio_info stk3310_info = {
0432     .read_raw       = stk3310_read_raw,
0433     .write_raw      = stk3310_write_raw,
0434     .attrs          = &stk3310_attribute_group,
0435     .read_event_value   = stk3310_read_event,
0436     .write_event_value  = stk3310_write_event,
0437     .read_event_config  = stk3310_read_event_config,
0438     .write_event_config = stk3310_write_event_config,
0439 };
0440 
0441 static int stk3310_set_state(struct stk3310_data *data, u8 state)
0442 {
0443     int ret;
0444     struct i2c_client *client = data->client;
0445 
0446     /* 3-bit state; 0b100 is not supported. */
0447     if (state > 7 || state == 4)
0448         return -EINVAL;
0449 
0450     mutex_lock(&data->lock);
0451     ret = regmap_field_write(data->reg_state, state);
0452     if (ret < 0) {
0453         dev_err(&client->dev, "failed to change sensor state\n");
0454     } else if (state != STK3310_STATE_STANDBY) {
0455         /* Don't reset the 'enabled' flags if we're going in standby */
0456         data->ps_enabled  = !!(state & STK3310_STATE_EN_PS);
0457         data->als_enabled = !!(state & STK3310_STATE_EN_ALS);
0458     }
0459     mutex_unlock(&data->lock);
0460 
0461     return ret;
0462 }
0463 
0464 static int stk3310_init(struct iio_dev *indio_dev)
0465 {
0466     int ret;
0467     int chipid;
0468     u8 state;
0469     struct stk3310_data *data = iio_priv(indio_dev);
0470     struct i2c_client *client = data->client;
0471 
0472     ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid);
0473     if (ret < 0)
0474         return ret;
0475 
0476     if (chipid != STK3310_CHIP_ID_VAL &&
0477         chipid != STK3311_CHIP_ID_VAL &&
0478         chipid != STK3311X_CHIP_ID_VAL &&
0479         chipid != STK3335_CHIP_ID_VAL) {
0480         dev_err(&client->dev, "invalid chip id: 0x%x\n", chipid);
0481         return -ENODEV;
0482     }
0483 
0484     state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS;
0485     ret = stk3310_set_state(data, state);
0486     if (ret < 0) {
0487         dev_err(&client->dev, "failed to enable sensor");
0488         return ret;
0489     }
0490 
0491     /* Enable PS interrupts */
0492     ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN);
0493     if (ret < 0)
0494         dev_err(&client->dev, "failed to enable interrupts!\n");
0495 
0496     return ret;
0497 }
0498 
0499 static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg)
0500 {
0501     switch (reg) {
0502     case STK3310_REG_ALS_DATA_MSB:
0503     case STK3310_REG_ALS_DATA_LSB:
0504     case STK3310_REG_PS_DATA_LSB:
0505     case STK3310_REG_PS_DATA_MSB:
0506     case STK3310_REG_FLAG:
0507         return true;
0508     default:
0509         return false;
0510     }
0511 }
0512 
0513 static const struct regmap_config stk3310_regmap_config = {
0514     .name = STK3310_REGMAP_NAME,
0515     .reg_bits = 8,
0516     .val_bits = 8,
0517     .max_register = STK3310_MAX_REG,
0518     .cache_type = REGCACHE_RBTREE,
0519     .volatile_reg = stk3310_is_volatile_reg,
0520 };
0521 
0522 static int stk3310_regmap_init(struct stk3310_data *data)
0523 {
0524     struct regmap *regmap;
0525     struct i2c_client *client;
0526 
0527     client = data->client;
0528     regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config);
0529     if (IS_ERR(regmap)) {
0530         dev_err(&client->dev, "regmap initialization failed.\n");
0531         return PTR_ERR(regmap);
0532     }
0533     data->regmap = regmap;
0534 
0535     STK3310_REGFIELD(state);
0536     STK3310_REGFIELD(als_gain);
0537     STK3310_REGFIELD(ps_gain);
0538     STK3310_REGFIELD(als_it);
0539     STK3310_REGFIELD(ps_it);
0540     STK3310_REGFIELD(int_ps);
0541     STK3310_REGFIELD(flag_psint);
0542     STK3310_REGFIELD(flag_nf);
0543 
0544     return 0;
0545 }
0546 
0547 static irqreturn_t stk3310_irq_handler(int irq, void *private)
0548 {
0549     struct iio_dev *indio_dev = private;
0550     struct stk3310_data *data = iio_priv(indio_dev);
0551 
0552     data->timestamp = iio_get_time_ns(indio_dev);
0553 
0554     return IRQ_WAKE_THREAD;
0555 }
0556 
0557 static irqreturn_t stk3310_irq_event_handler(int irq, void *private)
0558 {
0559     int ret;
0560     unsigned int dir;
0561     u64 event;
0562 
0563     struct iio_dev *indio_dev = private;
0564     struct stk3310_data *data = iio_priv(indio_dev);
0565 
0566     /* Read FLAG_NF to figure out what threshold has been met. */
0567     mutex_lock(&data->lock);
0568     ret = regmap_field_read(data->reg_flag_nf, &dir);
0569     if (ret < 0) {
0570         dev_err(&data->client->dev, "register read failed: %d\n", ret);
0571         goto out;
0572     }
0573     event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1,
0574                      IIO_EV_TYPE_THRESH,
0575                      (dir ? IIO_EV_DIR_FALLING :
0576                         IIO_EV_DIR_RISING));
0577     iio_push_event(indio_dev, event, data->timestamp);
0578 
0579     /* Reset the interrupt flag */
0580     ret = regmap_field_write(data->reg_flag_psint, 0);
0581     if (ret < 0)
0582         dev_err(&data->client->dev, "failed to reset interrupts\n");
0583 out:
0584     mutex_unlock(&data->lock);
0585 
0586     return IRQ_HANDLED;
0587 }
0588 
0589 static int stk3310_probe(struct i2c_client *client,
0590              const struct i2c_device_id *id)
0591 {
0592     int ret;
0593     struct iio_dev *indio_dev;
0594     struct stk3310_data *data;
0595 
0596     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
0597     if (!indio_dev) {
0598         dev_err(&client->dev, "iio allocation failed!\n");
0599         return -ENOMEM;
0600     }
0601 
0602     data = iio_priv(indio_dev);
0603     data->client = client;
0604     i2c_set_clientdata(client, indio_dev);
0605 
0606     device_property_read_u32(&client->dev, "proximity-near-level",
0607                  &data->ps_near_level);
0608 
0609     mutex_init(&data->lock);
0610 
0611     ret = stk3310_regmap_init(data);
0612     if (ret < 0)
0613         return ret;
0614 
0615     indio_dev->info = &stk3310_info;
0616     indio_dev->name = STK3310_DRIVER_NAME;
0617     indio_dev->modes = INDIO_DIRECT_MODE;
0618     indio_dev->channels = stk3310_channels;
0619     indio_dev->num_channels = ARRAY_SIZE(stk3310_channels);
0620 
0621     ret = stk3310_init(indio_dev);
0622     if (ret < 0)
0623         return ret;
0624 
0625     if (client->irq > 0) {
0626         ret = devm_request_threaded_irq(&client->dev, client->irq,
0627                         stk3310_irq_handler,
0628                         stk3310_irq_event_handler,
0629                         IRQF_TRIGGER_FALLING |
0630                         IRQF_ONESHOT,
0631                         STK3310_EVENT, indio_dev);
0632         if (ret < 0) {
0633             dev_err(&client->dev, "request irq %d failed\n",
0634                 client->irq);
0635             goto err_standby;
0636         }
0637     }
0638 
0639     ret = iio_device_register(indio_dev);
0640     if (ret < 0) {
0641         dev_err(&client->dev, "device_register failed\n");
0642         goto err_standby;
0643     }
0644 
0645     return 0;
0646 
0647 err_standby:
0648     stk3310_set_state(data, STK3310_STATE_STANDBY);
0649     return ret;
0650 }
0651 
0652 static int stk3310_remove(struct i2c_client *client)
0653 {
0654     struct iio_dev *indio_dev = i2c_get_clientdata(client);
0655 
0656     iio_device_unregister(indio_dev);
0657     stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY);
0658 
0659     return 0;
0660 }
0661 
0662 static int stk3310_suspend(struct device *dev)
0663 {
0664     struct stk3310_data *data;
0665 
0666     data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
0667 
0668     return stk3310_set_state(data, STK3310_STATE_STANDBY);
0669 }
0670 
0671 static int stk3310_resume(struct device *dev)
0672 {
0673     u8 state = 0;
0674     struct stk3310_data *data;
0675 
0676     data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
0677     if (data->ps_enabled)
0678         state |= STK3310_STATE_EN_PS;
0679     if (data->als_enabled)
0680         state |= STK3310_STATE_EN_ALS;
0681 
0682     return stk3310_set_state(data, state);
0683 }
0684 
0685 static DEFINE_SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend,
0686                 stk3310_resume);
0687 
0688 static const struct i2c_device_id stk3310_i2c_id[] = {
0689     {"STK3310", 0},
0690     {"STK3311", 0},
0691     {"STK3335", 0},
0692     {}
0693 };
0694 MODULE_DEVICE_TABLE(i2c, stk3310_i2c_id);
0695 
0696 static const struct acpi_device_id stk3310_acpi_id[] = {
0697     {"STK3310", 0},
0698     {"STK3311", 0},
0699     {"STK3335", 0},
0700     {}
0701 };
0702 
0703 MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id);
0704 
0705 static const struct of_device_id stk3310_of_match[] = {
0706     { .compatible = "sensortek,stk3310", },
0707     { .compatible = "sensortek,stk3311", },
0708     { .compatible = "sensortek,stk3335", },
0709     {}
0710 };
0711 MODULE_DEVICE_TABLE(of, stk3310_of_match);
0712 
0713 static struct i2c_driver stk3310_driver = {
0714     .driver = {
0715         .name = "stk3310",
0716         .of_match_table = stk3310_of_match,
0717         .pm = pm_sleep_ptr(&stk3310_pm_ops),
0718         .acpi_match_table = ACPI_PTR(stk3310_acpi_id),
0719     },
0720     .probe =            stk3310_probe,
0721     .remove =           stk3310_remove,
0722     .id_table =         stk3310_i2c_id,
0723 };
0724 
0725 module_i2c_driver(stk3310_driver);
0726 
0727 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
0728 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
0729 MODULE_LICENSE("GPL v2");