Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * drivers/iio/light/tsl2563.c
0004  *
0005  * Copyright (C) 2008 Nokia Corporation
0006  *
0007  * Written by Timo O. Karjalainen <timo.o.karjalainen@nokia.com>
0008  * Contact: Amit Kucheria <amit.kucheria@verdurent.com>
0009  *
0010  * Converted to IIO driver
0011  * Amit Kucheria <amit.kucheria@verdurent.com>
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/mod_devicetable.h>
0016 #include <linux/property.h>
0017 #include <linux/i2c.h>
0018 #include <linux/interrupt.h>
0019 #include <linux/irq.h>
0020 #include <linux/sched.h>
0021 #include <linux/mutex.h>
0022 #include <linux/delay.h>
0023 #include <linux/pm.h>
0024 #include <linux/err.h>
0025 #include <linux/slab.h>
0026 
0027 #include <linux/iio/iio.h>
0028 #include <linux/iio/sysfs.h>
0029 #include <linux/iio/events.h>
0030 #include <linux/platform_data/tsl2563.h>
0031 
0032 /* Use this many bits for fraction part. */
0033 #define ADC_FRAC_BITS       14
0034 
0035 /* Given number of 1/10000's in ADC_FRAC_BITS precision. */
0036 #define FRAC10K(f)      (((f) * (1L << (ADC_FRAC_BITS))) / (10000))
0037 
0038 /* Bits used for fraction in calibration coefficients.*/
0039 #define CALIB_FRAC_BITS     10
0040 /* 0.5 in CALIB_FRAC_BITS precision */
0041 #define CALIB_FRAC_HALF     (1 << (CALIB_FRAC_BITS - 1))
0042 /* Make a fraction from a number n that was multiplied with b. */
0043 #define CALIB_FRAC(n, b)    (((n) << CALIB_FRAC_BITS) / (b))
0044 /* Decimal 10^(digits in sysfs presentation) */
0045 #define CALIB_BASE_SYSFS    1000
0046 
0047 #define TSL2563_CMD     0x80
0048 #define TSL2563_CLEARINT    0x40
0049 
0050 #define TSL2563_REG_CTRL    0x00
0051 #define TSL2563_REG_TIMING  0x01
0052 #define TSL2563_REG_LOWLOW  0x02 /* data0 low threshold, 2 bytes */
0053 #define TSL2563_REG_LOWHIGH 0x03
0054 #define TSL2563_REG_HIGHLOW 0x04 /* data0 high threshold, 2 bytes */
0055 #define TSL2563_REG_HIGHHIGH    0x05
0056 #define TSL2563_REG_INT     0x06
0057 #define TSL2563_REG_ID      0x0a
0058 #define TSL2563_REG_DATA0LOW    0x0c /* broadband sensor value, 2 bytes */
0059 #define TSL2563_REG_DATA0HIGH   0x0d
0060 #define TSL2563_REG_DATA1LOW    0x0e /* infrared sensor value, 2 bytes */
0061 #define TSL2563_REG_DATA1HIGH   0x0f
0062 
0063 #define TSL2563_CMD_POWER_ON    0x03
0064 #define TSL2563_CMD_POWER_OFF   0x00
0065 #define TSL2563_CTRL_POWER_MASK 0x03
0066 
0067 #define TSL2563_TIMING_13MS 0x00
0068 #define TSL2563_TIMING_100MS    0x01
0069 #define TSL2563_TIMING_400MS    0x02
0070 #define TSL2563_TIMING_MASK 0x03
0071 #define TSL2563_TIMING_GAIN16   0x10
0072 #define TSL2563_TIMING_GAIN1    0x00
0073 
0074 #define TSL2563_INT_DISABLED    0x00
0075 #define TSL2563_INT_LEVEL   0x10
0076 #define TSL2563_INT_PERSIST(n)  ((n) & 0x0F)
0077 
0078 struct tsl2563_gainlevel_coeff {
0079     u8 gaintime;
0080     u16 min;
0081     u16 max;
0082 };
0083 
0084 static const struct tsl2563_gainlevel_coeff tsl2563_gainlevel_table[] = {
0085     {
0086         .gaintime   = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN16,
0087         .min        = 0,
0088         .max        = 65534,
0089     }, {
0090         .gaintime   = TSL2563_TIMING_400MS | TSL2563_TIMING_GAIN1,
0091         .min        = 2048,
0092         .max        = 65534,
0093     }, {
0094         .gaintime   = TSL2563_TIMING_100MS | TSL2563_TIMING_GAIN1,
0095         .min        = 4095,
0096         .max        = 37177,
0097     }, {
0098         .gaintime   = TSL2563_TIMING_13MS | TSL2563_TIMING_GAIN1,
0099         .min        = 3000,
0100         .max        = 65535,
0101     },
0102 };
0103 
0104 struct tsl2563_chip {
0105     struct mutex        lock;
0106     struct i2c_client   *client;
0107     struct delayed_work poweroff_work;
0108 
0109     /* Remember state for suspend and resume functions */
0110     bool suspended;
0111 
0112     struct tsl2563_gainlevel_coeff const *gainlevel;
0113 
0114     u16         low_thres;
0115     u16         high_thres;
0116     u8          intr;
0117     bool            int_enabled;
0118 
0119     /* Calibration coefficients */
0120     u32         calib0;
0121     u32         calib1;
0122     int         cover_comp_gain;
0123 
0124     /* Cache current values, to be returned while suspended */
0125     u32         data0;
0126     u32         data1;
0127 };
0128 
0129 static int tsl2563_set_power(struct tsl2563_chip *chip, int on)
0130 {
0131     struct i2c_client *client = chip->client;
0132     u8 cmd;
0133 
0134     cmd = on ? TSL2563_CMD_POWER_ON : TSL2563_CMD_POWER_OFF;
0135     return i2c_smbus_write_byte_data(client,
0136                      TSL2563_CMD | TSL2563_REG_CTRL, cmd);
0137 }
0138 
0139 /*
0140  * Return value is 0 for off, 1 for on, or a negative error
0141  * code if reading failed.
0142  */
0143 static int tsl2563_get_power(struct tsl2563_chip *chip)
0144 {
0145     struct i2c_client *client = chip->client;
0146     int ret;
0147 
0148     ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_CTRL);
0149     if (ret < 0)
0150         return ret;
0151 
0152     return (ret & TSL2563_CTRL_POWER_MASK) == TSL2563_CMD_POWER_ON;
0153 }
0154 
0155 static int tsl2563_configure(struct tsl2563_chip *chip)
0156 {
0157     int ret;
0158 
0159     ret = i2c_smbus_write_byte_data(chip->client,
0160             TSL2563_CMD | TSL2563_REG_TIMING,
0161             chip->gainlevel->gaintime);
0162     if (ret)
0163         goto error_ret;
0164     ret = i2c_smbus_write_byte_data(chip->client,
0165             TSL2563_CMD | TSL2563_REG_HIGHLOW,
0166             chip->high_thres & 0xFF);
0167     if (ret)
0168         goto error_ret;
0169     ret = i2c_smbus_write_byte_data(chip->client,
0170             TSL2563_CMD | TSL2563_REG_HIGHHIGH,
0171             (chip->high_thres >> 8) & 0xFF);
0172     if (ret)
0173         goto error_ret;
0174     ret = i2c_smbus_write_byte_data(chip->client,
0175             TSL2563_CMD | TSL2563_REG_LOWLOW,
0176             chip->low_thres & 0xFF);
0177     if (ret)
0178         goto error_ret;
0179     ret = i2c_smbus_write_byte_data(chip->client,
0180             TSL2563_CMD | TSL2563_REG_LOWHIGH,
0181             (chip->low_thres >> 8) & 0xFF);
0182 /*
0183  * Interrupt register is automatically written anyway if it is relevant
0184  * so is not here.
0185  */
0186 error_ret:
0187     return ret;
0188 }
0189 
0190 static void tsl2563_poweroff_work(struct work_struct *work)
0191 {
0192     struct tsl2563_chip *chip =
0193         container_of(work, struct tsl2563_chip, poweroff_work.work);
0194     tsl2563_set_power(chip, 0);
0195 }
0196 
0197 static int tsl2563_detect(struct tsl2563_chip *chip)
0198 {
0199     int ret;
0200 
0201     ret = tsl2563_set_power(chip, 1);
0202     if (ret)
0203         return ret;
0204 
0205     ret = tsl2563_get_power(chip);
0206     if (ret < 0)
0207         return ret;
0208 
0209     return ret ? 0 : -ENODEV;
0210 }
0211 
0212 static int tsl2563_read_id(struct tsl2563_chip *chip, u8 *id)
0213 {
0214     struct i2c_client *client = chip->client;
0215     int ret;
0216 
0217     ret = i2c_smbus_read_byte_data(client, TSL2563_CMD | TSL2563_REG_ID);
0218     if (ret < 0)
0219         return ret;
0220 
0221     *id = ret;
0222 
0223     return 0;
0224 }
0225 
0226 /*
0227  * "Normalized" ADC value is one obtained with 400ms of integration time and
0228  * 16x gain. This function returns the number of bits of shift needed to
0229  * convert between normalized values and HW values obtained using given
0230  * timing and gain settings.
0231  */
0232 static int tsl2563_adc_shiftbits(u8 timing)
0233 {
0234     int shift = 0;
0235 
0236     switch (timing & TSL2563_TIMING_MASK) {
0237     case TSL2563_TIMING_13MS:
0238         shift += 5;
0239         break;
0240     case TSL2563_TIMING_100MS:
0241         shift += 2;
0242         break;
0243     case TSL2563_TIMING_400MS:
0244         /* no-op */
0245         break;
0246     }
0247 
0248     if (!(timing & TSL2563_TIMING_GAIN16))
0249         shift += 4;
0250 
0251     return shift;
0252 }
0253 
0254 /* Convert a HW ADC value to normalized scale. */
0255 static u32 tsl2563_normalize_adc(u16 adc, u8 timing)
0256 {
0257     return adc << tsl2563_adc_shiftbits(timing);
0258 }
0259 
0260 static void tsl2563_wait_adc(struct tsl2563_chip *chip)
0261 {
0262     unsigned int delay;
0263 
0264     switch (chip->gainlevel->gaintime & TSL2563_TIMING_MASK) {
0265     case TSL2563_TIMING_13MS:
0266         delay = 14;
0267         break;
0268     case TSL2563_TIMING_100MS:
0269         delay = 101;
0270         break;
0271     default:
0272         delay = 402;
0273     }
0274     /*
0275      * TODO: Make sure that we wait at least required delay but why we
0276      * have to extend it one tick more?
0277      */
0278     schedule_timeout_interruptible(msecs_to_jiffies(delay) + 2);
0279 }
0280 
0281 static int tsl2563_adjust_gainlevel(struct tsl2563_chip *chip, u16 adc)
0282 {
0283     struct i2c_client *client = chip->client;
0284 
0285     if (adc > chip->gainlevel->max || adc < chip->gainlevel->min) {
0286 
0287         (adc > chip->gainlevel->max) ?
0288             chip->gainlevel++ : chip->gainlevel--;
0289 
0290         i2c_smbus_write_byte_data(client,
0291                       TSL2563_CMD | TSL2563_REG_TIMING,
0292                       chip->gainlevel->gaintime);
0293 
0294         tsl2563_wait_adc(chip);
0295         tsl2563_wait_adc(chip);
0296 
0297         return 1;
0298     } else
0299         return 0;
0300 }
0301 
0302 static int tsl2563_get_adc(struct tsl2563_chip *chip)
0303 {
0304     struct i2c_client *client = chip->client;
0305     u16 adc0, adc1;
0306     int retry = 1;
0307     int ret = 0;
0308 
0309     if (chip->suspended)
0310         goto out;
0311 
0312     if (!chip->int_enabled) {
0313         cancel_delayed_work_sync(&chip->poweroff_work);
0314 
0315         if (!tsl2563_get_power(chip)) {
0316             ret = tsl2563_set_power(chip, 1);
0317             if (ret)
0318                 goto out;
0319             ret = tsl2563_configure(chip);
0320             if (ret)
0321                 goto out;
0322             tsl2563_wait_adc(chip);
0323         }
0324     }
0325 
0326     while (retry) {
0327         ret = i2c_smbus_read_word_data(client,
0328                 TSL2563_CMD | TSL2563_REG_DATA0LOW);
0329         if (ret < 0)
0330             goto out;
0331         adc0 = ret;
0332 
0333         ret = i2c_smbus_read_word_data(client,
0334                 TSL2563_CMD | TSL2563_REG_DATA1LOW);
0335         if (ret < 0)
0336             goto out;
0337         adc1 = ret;
0338 
0339         retry = tsl2563_adjust_gainlevel(chip, adc0);
0340     }
0341 
0342     chip->data0 = tsl2563_normalize_adc(adc0, chip->gainlevel->gaintime);
0343     chip->data1 = tsl2563_normalize_adc(adc1, chip->gainlevel->gaintime);
0344 
0345     if (!chip->int_enabled)
0346         schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
0347 
0348     ret = 0;
0349 out:
0350     return ret;
0351 }
0352 
0353 static inline int tsl2563_calib_to_sysfs(u32 calib)
0354 {
0355     return (int) (((calib * CALIB_BASE_SYSFS) +
0356                CALIB_FRAC_HALF) >> CALIB_FRAC_BITS);
0357 }
0358 
0359 static inline u32 tsl2563_calib_from_sysfs(int value)
0360 {
0361     return (((u32) value) << CALIB_FRAC_BITS) / CALIB_BASE_SYSFS;
0362 }
0363 
0364 /*
0365  * Conversions between lux and ADC values.
0366  *
0367  * The basic formula is lux = c0 * adc0 - c1 * adc1, where c0 and c1 are
0368  * appropriate constants. Different constants are needed for different
0369  * kinds of light, determined by the ratio adc1/adc0 (basically the ratio
0370  * of the intensities in infrared and visible wavelengths). lux_table below
0371  * lists the upper threshold of the adc1/adc0 ratio and the corresponding
0372  * constants.
0373  */
0374 
0375 struct tsl2563_lux_coeff {
0376     unsigned long ch_ratio;
0377     unsigned long ch0_coeff;
0378     unsigned long ch1_coeff;
0379 };
0380 
0381 static const struct tsl2563_lux_coeff lux_table[] = {
0382     {
0383         .ch_ratio   = FRAC10K(1300),
0384         .ch0_coeff  = FRAC10K(315),
0385         .ch1_coeff  = FRAC10K(262),
0386     }, {
0387         .ch_ratio   = FRAC10K(2600),
0388         .ch0_coeff  = FRAC10K(337),
0389         .ch1_coeff  = FRAC10K(430),
0390     }, {
0391         .ch_ratio   = FRAC10K(3900),
0392         .ch0_coeff  = FRAC10K(363),
0393         .ch1_coeff  = FRAC10K(529),
0394     }, {
0395         .ch_ratio   = FRAC10K(5200),
0396         .ch0_coeff  = FRAC10K(392),
0397         .ch1_coeff  = FRAC10K(605),
0398     }, {
0399         .ch_ratio   = FRAC10K(6500),
0400         .ch0_coeff  = FRAC10K(229),
0401         .ch1_coeff  = FRAC10K(291),
0402     }, {
0403         .ch_ratio   = FRAC10K(8000),
0404         .ch0_coeff  = FRAC10K(157),
0405         .ch1_coeff  = FRAC10K(180),
0406     }, {
0407         .ch_ratio   = FRAC10K(13000),
0408         .ch0_coeff  = FRAC10K(34),
0409         .ch1_coeff  = FRAC10K(26),
0410     }, {
0411         .ch_ratio   = ULONG_MAX,
0412         .ch0_coeff  = 0,
0413         .ch1_coeff  = 0,
0414     },
0415 };
0416 
0417 /* Convert normalized, scaled ADC values to lux. */
0418 static unsigned int tsl2563_adc_to_lux(u32 adc0, u32 adc1)
0419 {
0420     const struct tsl2563_lux_coeff *lp = lux_table;
0421     unsigned long ratio, lux, ch0 = adc0, ch1 = adc1;
0422 
0423     ratio = ch0 ? ((ch1 << ADC_FRAC_BITS) / ch0) : ULONG_MAX;
0424 
0425     while (lp->ch_ratio < ratio)
0426         lp++;
0427 
0428     lux = ch0 * lp->ch0_coeff - ch1 * lp->ch1_coeff;
0429 
0430     return (unsigned int) (lux >> ADC_FRAC_BITS);
0431 }
0432 
0433 /* Apply calibration coefficient to ADC count. */
0434 static u32 tsl2563_calib_adc(u32 adc, u32 calib)
0435 {
0436     unsigned long scaled = adc;
0437 
0438     scaled *= calib;
0439     scaled >>= CALIB_FRAC_BITS;
0440 
0441     return (u32) scaled;
0442 }
0443 
0444 static int tsl2563_write_raw(struct iio_dev *indio_dev,
0445                    struct iio_chan_spec const *chan,
0446                    int val,
0447                    int val2,
0448                    long mask)
0449 {
0450     struct tsl2563_chip *chip = iio_priv(indio_dev);
0451 
0452     if (mask != IIO_CHAN_INFO_CALIBSCALE)
0453         return -EINVAL;
0454     if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
0455         chip->calib0 = tsl2563_calib_from_sysfs(val);
0456     else if (chan->channel2 == IIO_MOD_LIGHT_IR)
0457         chip->calib1 = tsl2563_calib_from_sysfs(val);
0458     else
0459         return -EINVAL;
0460 
0461     return 0;
0462 }
0463 
0464 static int tsl2563_read_raw(struct iio_dev *indio_dev,
0465                 struct iio_chan_spec const *chan,
0466                 int *val,
0467                 int *val2,
0468                 long mask)
0469 {
0470     int ret = -EINVAL;
0471     u32 calib0, calib1;
0472     struct tsl2563_chip *chip = iio_priv(indio_dev);
0473 
0474     mutex_lock(&chip->lock);
0475     switch (mask) {
0476     case IIO_CHAN_INFO_RAW:
0477     case IIO_CHAN_INFO_PROCESSED:
0478         switch (chan->type) {
0479         case IIO_LIGHT:
0480             ret = tsl2563_get_adc(chip);
0481             if (ret)
0482                 goto error_ret;
0483             calib0 = tsl2563_calib_adc(chip->data0, chip->calib0) *
0484                 chip->cover_comp_gain;
0485             calib1 = tsl2563_calib_adc(chip->data1, chip->calib1) *
0486                 chip->cover_comp_gain;
0487             *val = tsl2563_adc_to_lux(calib0, calib1);
0488             ret = IIO_VAL_INT;
0489             break;
0490         case IIO_INTENSITY:
0491             ret = tsl2563_get_adc(chip);
0492             if (ret)
0493                 goto error_ret;
0494             if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
0495                 *val = chip->data0;
0496             else
0497                 *val = chip->data1;
0498             ret = IIO_VAL_INT;
0499             break;
0500         default:
0501             break;
0502         }
0503         break;
0504 
0505     case IIO_CHAN_INFO_CALIBSCALE:
0506         if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
0507             *val = tsl2563_calib_to_sysfs(chip->calib0);
0508         else
0509             *val = tsl2563_calib_to_sysfs(chip->calib1);
0510         ret = IIO_VAL_INT;
0511         break;
0512     default:
0513         ret = -EINVAL;
0514         goto error_ret;
0515     }
0516 
0517 error_ret:
0518     mutex_unlock(&chip->lock);
0519     return ret;
0520 }
0521 
0522 static const struct iio_event_spec tsl2563_events[] = {
0523     {
0524         .type = IIO_EV_TYPE_THRESH,
0525         .dir = IIO_EV_DIR_RISING,
0526         .mask_separate = BIT(IIO_EV_INFO_VALUE) |
0527                 BIT(IIO_EV_INFO_ENABLE),
0528     }, {
0529         .type = IIO_EV_TYPE_THRESH,
0530         .dir = IIO_EV_DIR_FALLING,
0531         .mask_separate = BIT(IIO_EV_INFO_VALUE) |
0532                 BIT(IIO_EV_INFO_ENABLE),
0533     },
0534 };
0535 
0536 static const struct iio_chan_spec tsl2563_channels[] = {
0537     {
0538         .type = IIO_LIGHT,
0539         .indexed = 1,
0540         .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
0541         .channel = 0,
0542     }, {
0543         .type = IIO_INTENSITY,
0544         .modified = 1,
0545         .channel2 = IIO_MOD_LIGHT_BOTH,
0546         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0547         BIT(IIO_CHAN_INFO_CALIBSCALE),
0548         .event_spec = tsl2563_events,
0549         .num_event_specs = ARRAY_SIZE(tsl2563_events),
0550     }, {
0551         .type = IIO_INTENSITY,
0552         .modified = 1,
0553         .channel2 = IIO_MOD_LIGHT_IR,
0554         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0555         BIT(IIO_CHAN_INFO_CALIBSCALE),
0556     }
0557 };
0558 
0559 static int tsl2563_read_thresh(struct iio_dev *indio_dev,
0560     const struct iio_chan_spec *chan, enum iio_event_type type,
0561     enum iio_event_direction dir, enum iio_event_info info, int *val,
0562     int *val2)
0563 {
0564     struct tsl2563_chip *chip = iio_priv(indio_dev);
0565 
0566     switch (dir) {
0567     case IIO_EV_DIR_RISING:
0568         *val = chip->high_thres;
0569         break;
0570     case IIO_EV_DIR_FALLING:
0571         *val = chip->low_thres;
0572         break;
0573     default:
0574         return -EINVAL;
0575     }
0576 
0577     return IIO_VAL_INT;
0578 }
0579 
0580 static int tsl2563_write_thresh(struct iio_dev *indio_dev,
0581     const struct iio_chan_spec *chan, enum iio_event_type type,
0582     enum iio_event_direction dir, enum iio_event_info info, int val,
0583     int val2)
0584 {
0585     struct tsl2563_chip *chip = iio_priv(indio_dev);
0586     int ret;
0587     u8 address;
0588 
0589     if (dir == IIO_EV_DIR_RISING)
0590         address = TSL2563_REG_HIGHLOW;
0591     else
0592         address = TSL2563_REG_LOWLOW;
0593     mutex_lock(&chip->lock);
0594     ret = i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | address,
0595                     val & 0xFF);
0596     if (ret)
0597         goto error_ret;
0598     ret = i2c_smbus_write_byte_data(chip->client,
0599                     TSL2563_CMD | (address + 1),
0600                     (val >> 8) & 0xFF);
0601     if (dir == IIO_EV_DIR_RISING)
0602         chip->high_thres = val;
0603     else
0604         chip->low_thres = val;
0605 
0606 error_ret:
0607     mutex_unlock(&chip->lock);
0608 
0609     return ret;
0610 }
0611 
0612 static irqreturn_t tsl2563_event_handler(int irq, void *private)
0613 {
0614     struct iio_dev *dev_info = private;
0615     struct tsl2563_chip *chip = iio_priv(dev_info);
0616 
0617     iio_push_event(dev_info,
0618                IIO_UNMOD_EVENT_CODE(IIO_INTENSITY,
0619                         0,
0620                         IIO_EV_TYPE_THRESH,
0621                         IIO_EV_DIR_EITHER),
0622                iio_get_time_ns(dev_info));
0623 
0624     /* clear the interrupt and push the event */
0625     i2c_smbus_write_byte(chip->client, TSL2563_CMD | TSL2563_CLEARINT);
0626     return IRQ_HANDLED;
0627 }
0628 
0629 static int tsl2563_write_interrupt_config(struct iio_dev *indio_dev,
0630     const struct iio_chan_spec *chan, enum iio_event_type type,
0631     enum iio_event_direction dir, int state)
0632 {
0633     struct tsl2563_chip *chip = iio_priv(indio_dev);
0634     int ret = 0;
0635 
0636     mutex_lock(&chip->lock);
0637     if (state && !(chip->intr & 0x30)) {
0638         chip->intr &= ~0x30;
0639         chip->intr |= 0x10;
0640         /* ensure the chip is actually on */
0641         cancel_delayed_work_sync(&chip->poweroff_work);
0642         if (!tsl2563_get_power(chip)) {
0643             ret = tsl2563_set_power(chip, 1);
0644             if (ret)
0645                 goto out;
0646             ret = tsl2563_configure(chip);
0647             if (ret)
0648                 goto out;
0649         }
0650         ret = i2c_smbus_write_byte_data(chip->client,
0651                         TSL2563_CMD | TSL2563_REG_INT,
0652                         chip->intr);
0653         chip->int_enabled = true;
0654     }
0655 
0656     if (!state && (chip->intr & 0x30)) {
0657         chip->intr &= ~0x30;
0658         ret = i2c_smbus_write_byte_data(chip->client,
0659                         TSL2563_CMD | TSL2563_REG_INT,
0660                         chip->intr);
0661         chip->int_enabled = false;
0662         /* now the interrupt is not enabled, we can go to sleep */
0663         schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
0664     }
0665 out:
0666     mutex_unlock(&chip->lock);
0667 
0668     return ret;
0669 }
0670 
0671 static int tsl2563_read_interrupt_config(struct iio_dev *indio_dev,
0672     const struct iio_chan_spec *chan, enum iio_event_type type,
0673     enum iio_event_direction dir)
0674 {
0675     struct tsl2563_chip *chip = iio_priv(indio_dev);
0676     int ret;
0677 
0678     mutex_lock(&chip->lock);
0679     ret = i2c_smbus_read_byte_data(chip->client,
0680                        TSL2563_CMD | TSL2563_REG_INT);
0681     mutex_unlock(&chip->lock);
0682     if (ret < 0)
0683         return ret;
0684 
0685     return !!(ret & 0x30);
0686 }
0687 
0688 static const struct iio_info tsl2563_info_no_irq = {
0689     .read_raw = &tsl2563_read_raw,
0690     .write_raw = &tsl2563_write_raw,
0691 };
0692 
0693 static const struct iio_info tsl2563_info = {
0694     .read_raw = &tsl2563_read_raw,
0695     .write_raw = &tsl2563_write_raw,
0696     .read_event_value = &tsl2563_read_thresh,
0697     .write_event_value = &tsl2563_write_thresh,
0698     .read_event_config = &tsl2563_read_interrupt_config,
0699     .write_event_config = &tsl2563_write_interrupt_config,
0700 };
0701 
0702 static int tsl2563_probe(struct i2c_client *client,
0703                 const struct i2c_device_id *device_id)
0704 {
0705     struct iio_dev *indio_dev;
0706     struct tsl2563_chip *chip;
0707     struct tsl2563_platform_data *pdata = client->dev.platform_data;
0708     int err = 0;
0709     u8 id = 0;
0710 
0711     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
0712     if (!indio_dev)
0713         return -ENOMEM;
0714 
0715     chip = iio_priv(indio_dev);
0716 
0717     i2c_set_clientdata(client, indio_dev);
0718     chip->client = client;
0719 
0720     err = tsl2563_detect(chip);
0721     if (err) {
0722         dev_err(&client->dev, "detect error %d\n", -err);
0723         return err;
0724     }
0725 
0726     err = tsl2563_read_id(chip, &id);
0727     if (err) {
0728         dev_err(&client->dev, "read id error %d\n", -err);
0729         return err;
0730     }
0731 
0732     mutex_init(&chip->lock);
0733 
0734     /* Default values used until userspace says otherwise */
0735     chip->low_thres = 0x0;
0736     chip->high_thres = 0xffff;
0737     chip->gainlevel = tsl2563_gainlevel_table;
0738     chip->intr = TSL2563_INT_PERSIST(4);
0739     chip->calib0 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
0740     chip->calib1 = tsl2563_calib_from_sysfs(CALIB_BASE_SYSFS);
0741 
0742     if (pdata) {
0743         chip->cover_comp_gain = pdata->cover_comp_gain;
0744     } else {
0745         err = device_property_read_u32(&client->dev, "amstaos,cover-comp-gain",
0746                            &chip->cover_comp_gain);
0747         if (err)
0748             chip->cover_comp_gain = 1;
0749     }
0750 
0751     dev_info(&client->dev, "model %d, rev. %d\n", id >> 4, id & 0x0f);
0752     indio_dev->name = client->name;
0753     indio_dev->channels = tsl2563_channels;
0754     indio_dev->num_channels = ARRAY_SIZE(tsl2563_channels);
0755     indio_dev->modes = INDIO_DIRECT_MODE;
0756 
0757     if (client->irq)
0758         indio_dev->info = &tsl2563_info;
0759     else
0760         indio_dev->info = &tsl2563_info_no_irq;
0761 
0762     if (client->irq) {
0763         err = devm_request_threaded_irq(&client->dev, client->irq,
0764                        NULL,
0765                        &tsl2563_event_handler,
0766                        IRQF_TRIGGER_RISING | IRQF_ONESHOT,
0767                        "tsl2563_event",
0768                        indio_dev);
0769         if (err) {
0770             dev_err(&client->dev, "irq request error %d\n", -err);
0771             return err;
0772         }
0773     }
0774 
0775     err = tsl2563_configure(chip);
0776     if (err) {
0777         dev_err(&client->dev, "configure error %d\n", -err);
0778         return err;
0779     }
0780 
0781     INIT_DELAYED_WORK(&chip->poweroff_work, tsl2563_poweroff_work);
0782 
0783     /* The interrupt cannot yet be enabled so this is fine without lock */
0784     schedule_delayed_work(&chip->poweroff_work, 5 * HZ);
0785 
0786     err = iio_device_register(indio_dev);
0787     if (err) {
0788         dev_err(&client->dev, "iio registration error %d\n", -err);
0789         goto fail;
0790     }
0791 
0792     return 0;
0793 
0794 fail:
0795     cancel_delayed_work_sync(&chip->poweroff_work);
0796     return err;
0797 }
0798 
0799 static int tsl2563_remove(struct i2c_client *client)
0800 {
0801     struct iio_dev *indio_dev = i2c_get_clientdata(client);
0802     struct tsl2563_chip *chip = iio_priv(indio_dev);
0803 
0804     iio_device_unregister(indio_dev);
0805     if (!chip->int_enabled)
0806         cancel_delayed_work_sync(&chip->poweroff_work);
0807     /* Ensure that interrupts are disabled - then flush any bottom halves */
0808     chip->intr &= ~0x30;
0809     i2c_smbus_write_byte_data(chip->client, TSL2563_CMD | TSL2563_REG_INT,
0810                   chip->intr);
0811     tsl2563_set_power(chip, 0);
0812 
0813     return 0;
0814 }
0815 
0816 static int tsl2563_suspend(struct device *dev)
0817 {
0818     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
0819     struct tsl2563_chip *chip = iio_priv(indio_dev);
0820     int ret;
0821 
0822     mutex_lock(&chip->lock);
0823 
0824     ret = tsl2563_set_power(chip, 0);
0825     if (ret)
0826         goto out;
0827 
0828     chip->suspended = true;
0829 
0830 out:
0831     mutex_unlock(&chip->lock);
0832     return ret;
0833 }
0834 
0835 static int tsl2563_resume(struct device *dev)
0836 {
0837     struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
0838     struct tsl2563_chip *chip = iio_priv(indio_dev);
0839     int ret;
0840 
0841     mutex_lock(&chip->lock);
0842 
0843     ret = tsl2563_set_power(chip, 1);
0844     if (ret)
0845         goto out;
0846 
0847     ret = tsl2563_configure(chip);
0848     if (ret)
0849         goto out;
0850 
0851     chip->suspended = false;
0852 
0853 out:
0854     mutex_unlock(&chip->lock);
0855     return ret;
0856 }
0857 
0858 static DEFINE_SIMPLE_DEV_PM_OPS(tsl2563_pm_ops, tsl2563_suspend,
0859                 tsl2563_resume);
0860 
0861 static const struct i2c_device_id tsl2563_id[] = {
0862     { "tsl2560", 0 },
0863     { "tsl2561", 1 },
0864     { "tsl2562", 2 },
0865     { "tsl2563", 3 },
0866     {}
0867 };
0868 MODULE_DEVICE_TABLE(i2c, tsl2563_id);
0869 
0870 static const struct of_device_id tsl2563_of_match[] = {
0871     { .compatible = "amstaos,tsl2560" },
0872     { .compatible = "amstaos,tsl2561" },
0873     { .compatible = "amstaos,tsl2562" },
0874     { .compatible = "amstaos,tsl2563" },
0875     {}
0876 };
0877 MODULE_DEVICE_TABLE(of, tsl2563_of_match);
0878 
0879 static struct i2c_driver tsl2563_i2c_driver = {
0880     .driver = {
0881         .name    = "tsl2563",
0882         .of_match_table = tsl2563_of_match,
0883         .pm = pm_sleep_ptr(&tsl2563_pm_ops),
0884     },
0885     .probe      = tsl2563_probe,
0886     .remove     = tsl2563_remove,
0887     .id_table   = tsl2563_id,
0888 };
0889 module_i2c_driver(tsl2563_i2c_driver);
0890 
0891 MODULE_AUTHOR("Nokia Corporation");
0892 MODULE_DESCRIPTION("tsl2563 light sensor driver");
0893 MODULE_LICENSE("GPL");