Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * These are the two Sharp GP2AP002 variants supported by this driver:
0004  * GP2AP002A00F Ambient Light and Proximity Sensor
0005  * GP2AP002S00F Proximity Sensor
0006  *
0007  * Copyright (C) 2020 Linaro Ltd.
0008  * Author: Linus Walleij <linus.walleij@linaro.org>
0009  *
0010  * Based partly on the code in Sony Ericssons GP2AP00200F driver by
0011  * Courtney Cavin and Oskar Andero in drivers/input/misc/gp2ap002a00f.c
0012  * Based partly on a Samsung misc driver submitted by
0013  * Donggeun Kim & Minkyu Kang in 2011:
0014  * https://lore.kernel.org/lkml/1315556546-7445-1-git-send-email-dg77.kim@samsung.com/
0015  * Based partly on a submission by
0016  * Jonathan Bakker and Paweł Chmiel in january 2019:
0017  * https://lore.kernel.org/linux-input/20190125175045.22576-1-pawel.mikolaj.chmiel@gmail.com/
0018  * Based partly on code from the Samsung GT-S7710 by <mjchen@sta.samsung.com>
0019  * Based partly on the code in LG Electronics GP2AP00200F driver by
0020  * Kenobi Lee <sungyoung.lee@lge.com> and EunYoung Cho <ey.cho@lge.com>
0021  */
0022 #include <linux/module.h>
0023 #include <linux/i2c.h>
0024 #include <linux/regmap.h>
0025 #include <linux/iio/iio.h>
0026 #include <linux/iio/sysfs.h>
0027 #include <linux/iio/events.h>
0028 #include <linux/iio/consumer.h> /* To get our ADC channel */
0029 #include <linux/iio/types.h> /* To deal with our ADC channel */
0030 #include <linux/init.h>
0031 #include <linux/delay.h>
0032 #include <linux/regulator/consumer.h>
0033 #include <linux/pm_runtime.h>
0034 #include <linux/interrupt.h>
0035 #include <linux/bits.h>
0036 #include <linux/math64.h>
0037 #include <linux/pm.h>
0038 
0039 #define GP2AP002_PROX_CHANNEL 0
0040 #define GP2AP002_ALS_CHANNEL 1
0041 
0042 /* ------------------------------------------------------------------------ */
0043 /* ADDRESS SYMBOL             DATA                                 Init R/W */
0044 /*                   D7    D6    D5    D4    D3    D2    D1    D0           */
0045 /* ------------------------------------------------------------------------ */
0046 /*    0      PROX     X     X     X     X     X     X     X    VO  H'00   R */
0047 /*    1      GAIN     X     X     X     X  LED0     X     X     X  H'00   W */
0048 /*    2       HYS  HYSD HYSC1 HYSC0     X HYSF3 HYSF2 HYSF1 HYSF0  H'00   W */
0049 /*    3     CYCLE     X     X CYCL2 CYCL1 CYCL0  OSC2     X     X  H'00   W */
0050 /*    4     OPMOD     X     X     X   ASD     X     X  VCON   SSD  H'00   W */
0051 /*    6       CON     X     X     X OCON1 OCON0     X     X     X  H'00   W */
0052 /* ------------------------------------------------------------------------ */
0053 /* VO   :Proximity sensing result(0: no detection, 1: detection)            */
0054 /* LED0 :Select switch for LED driver's On-registence(0:2x higher, 1:normal)*/
0055 /* HYSD/HYSF :Adjusts the receiver sensitivity                              */
0056 /* OSC  :Select switch internal clocl frequency hoppling(0:effective)       */
0057 /* CYCL :Determine the detection cycle(typically 8ms, up to 128x)           */
0058 /* SSD  :Software Shutdown function(0:shutdown, 1:operating)                */
0059 /* VCON :VOUT output method control(0:normal, 1:interrupt)                  */
0060 /* ASD  :Select switch for analog sleep function(0:ineffective, 1:effective)*/
0061 /* OCON :Select switch for enabling/disabling VOUT (00:enable, 11:disable)  */
0062 
0063 #define GP2AP002_PROX               0x00
0064 #define GP2AP002_GAIN               0x01
0065 #define GP2AP002_HYS                0x02
0066 #define GP2AP002_CYCLE              0x03
0067 #define GP2AP002_OPMOD              0x04
0068 #define GP2AP002_CON                0x06
0069 
0070 #define GP2AP002_PROX_VO_DETECT         BIT(0)
0071 
0072 /* Setting this bit to 0 means 2x higher LED resistance */
0073 #define GP2AP002_GAIN_LED_NORMAL        BIT(3)
0074 
0075 /*
0076  * These bits adjusts the proximity sensitivity, determining characteristics
0077  * of the detection distance and its hysteresis.
0078  */
0079 #define GP2AP002_HYS_HYSD_SHIFT     7
0080 #define GP2AP002_HYS_HYSD_MASK      BIT(7)
0081 #define GP2AP002_HYS_HYSC_SHIFT     5
0082 #define GP2AP002_HYS_HYSC_MASK      GENMASK(6, 5)
0083 #define GP2AP002_HYS_HYSF_SHIFT     0
0084 #define GP2AP002_HYS_HYSF_MASK      GENMASK(3, 0)
0085 #define GP2AP002_HYS_MASK       (GP2AP002_HYS_HYSD_MASK | \
0086                      GP2AP002_HYS_HYSC_MASK | \
0087                      GP2AP002_HYS_HYSF_MASK)
0088 
0089 /*
0090  * These values determine the detection cycle response time
0091  * 0: 8ms, 1: 16ms, 2: 32ms, 3: 64ms, 4: 128ms,
0092  * 5: 256ms, 6: 512ms, 7: 1024ms
0093  */
0094 #define GP2AP002_CYCLE_CYCL_SHIFT   3
0095 #define GP2AP002_CYCLE_CYCL_MASK    GENMASK(5, 3)
0096 
0097 /*
0098  * Select switch for internal clock frequency hopping
0099  *  0: effective,
0100  *  1: ineffective
0101  */
0102 #define GP2AP002_CYCLE_OSC_EFFECTIVE    0
0103 #define GP2AP002_CYCLE_OSC_INEFFECTIVE  BIT(2)
0104 #define GP2AP002_CYCLE_OSC_MASK     BIT(2)
0105 
0106 /* Analog sleep effective */
0107 #define GP2AP002_OPMOD_ASD      BIT(4)
0108 /* Enable chip */
0109 #define GP2AP002_OPMOD_SSD_OPERATING    BIT(0)
0110 /* IRQ mode */
0111 #define GP2AP002_OPMOD_VCON_IRQ     BIT(1)
0112 #define GP2AP002_OPMOD_MASK     (BIT(0) | BIT(1) | BIT(4))
0113 
0114 /*
0115  * Select switch for enabling/disabling Vout pin
0116  * 0: enable
0117  * 2: force to go Low
0118  * 3: force to go High
0119  */
0120 #define GP2AP002_CON_OCON_SHIFT     3
0121 #define GP2AP002_CON_OCON_ENABLE    (0x0 << GP2AP002_CON_OCON_SHIFT)
0122 #define GP2AP002_CON_OCON_LOW       (0x2 << GP2AP002_CON_OCON_SHIFT)
0123 #define GP2AP002_CON_OCON_HIGH      (0x3 << GP2AP002_CON_OCON_SHIFT)
0124 #define GP2AP002_CON_OCON_MASK      (0x3 << GP2AP002_CON_OCON_SHIFT)
0125 
0126 /**
0127  * struct gp2ap002 - GP2AP002 state
0128  * @map: regmap pointer for the i2c regmap
0129  * @dev: pointer to parent device
0130  * @vdd: regulator controlling VDD
0131  * @vio: regulator controlling VIO
0132  * @alsout: IIO ADC channel to convert the ALSOUT signal
0133  * @hys_far: hysteresis control from device tree
0134  * @hys_close: hysteresis control from device tree
0135  * @is_gp2ap002s00f: this is the GP2AP002F variant of the chip
0136  * @irq: the IRQ line used by this device
0137  * @enabled: we cannot read the status of the hardware so we need to
0138  * keep track of whether the event is enabled using this state variable
0139  */
0140 struct gp2ap002 {
0141     struct regmap *map;
0142     struct device *dev;
0143     struct regulator *vdd;
0144     struct regulator *vio;
0145     struct iio_channel *alsout;
0146     u8 hys_far;
0147     u8 hys_close;
0148     bool is_gp2ap002s00f;
0149     int irq;
0150     bool enabled;
0151 };
0152 
0153 static irqreturn_t gp2ap002_prox_irq(int irq, void *d)
0154 {
0155     struct iio_dev *indio_dev = d;
0156     struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
0157     u64 ev;
0158     int val;
0159     int ret;
0160 
0161     if (!gp2ap002->enabled)
0162         goto err_retrig;
0163 
0164     ret = regmap_read(gp2ap002->map, GP2AP002_PROX, &val);
0165     if (ret) {
0166         dev_err(gp2ap002->dev, "error reading proximity\n");
0167         goto err_retrig;
0168     }
0169 
0170     if (val & GP2AP002_PROX_VO_DETECT) {
0171         /* Close */
0172         dev_dbg(gp2ap002->dev, "close\n");
0173         ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
0174                    gp2ap002->hys_far);
0175         if (ret)
0176             dev_err(gp2ap002->dev,
0177                 "error setting up proximity hysteresis\n");
0178         ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
0179                     IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING);
0180     } else {
0181         /* Far */
0182         dev_dbg(gp2ap002->dev, "far\n");
0183         ret = regmap_write(gp2ap002->map, GP2AP002_HYS,
0184                    gp2ap002->hys_close);
0185         if (ret)
0186             dev_err(gp2ap002->dev,
0187                 "error setting up proximity hysteresis\n");
0188         ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, GP2AP002_PROX_CHANNEL,
0189                     IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING);
0190     }
0191     iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
0192 
0193     /*
0194      * After changing hysteresis, we need to wait for one detection
0195      * cycle to see if anything changed, or we will just trigger the
0196      * previous interrupt again. A detection cycle depends on the CYCLE
0197      * register, we are hard-coding ~8 ms in probe() so wait some more
0198      * than this, 20-30 ms.
0199      */
0200     usleep_range(20000, 30000);
0201 
0202 err_retrig:
0203     ret = regmap_write(gp2ap002->map, GP2AP002_CON,
0204                GP2AP002_CON_OCON_ENABLE);
0205     if (ret)
0206         dev_err(gp2ap002->dev, "error setting up VOUT control\n");
0207 
0208     return IRQ_HANDLED;
0209 }
0210 
0211 /*
0212  * This array maps current and lux.
0213  *
0214  * Ambient light sensing range is 3 to 55000 lux.
0215  *
0216  * This mapping is based on the following formula.
0217  * illuminance = 10 ^ (current[mA] / 10)
0218  *
0219  * When the ADC measures 0, return 0 lux.
0220  */
0221 static const u16 gp2ap002_illuminance_table[] = {
0222     0, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 63, 79,
0223     100, 126, 158, 200, 251, 316, 398, 501, 631, 794, 1000, 1259, 1585,
0224     1995, 2512, 3162, 3981, 5012, 6310, 7943, 10000, 12589, 15849, 19953,
0225     25119, 31623, 39811, 50119,
0226 };
0227 
0228 static int gp2ap002_get_lux(struct gp2ap002 *gp2ap002)
0229 {
0230     int ret, res;
0231     u16 lux;
0232 
0233     ret = iio_read_channel_processed(gp2ap002->alsout, &res);
0234     if (ret < 0)
0235         return ret;
0236 
0237     dev_dbg(gp2ap002->dev, "read %d mA from ADC\n", res);
0238 
0239     /* ensure we don't under/overflow */
0240     res = clamp(res, 0, (int)ARRAY_SIZE(gp2ap002_illuminance_table) - 1);
0241     lux = gp2ap002_illuminance_table[res];
0242 
0243     return (int)lux;
0244 }
0245 
0246 static int gp2ap002_read_raw(struct iio_dev *indio_dev,
0247                struct iio_chan_spec const *chan,
0248                int *val, int *val2, long mask)
0249 {
0250     struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
0251     int ret;
0252 
0253     pm_runtime_get_sync(gp2ap002->dev);
0254 
0255     switch (mask) {
0256     case IIO_CHAN_INFO_RAW:
0257         switch (chan->type) {
0258         case IIO_LIGHT:
0259             ret = gp2ap002_get_lux(gp2ap002);
0260             if (ret < 0)
0261                 return ret;
0262             *val = ret;
0263             ret = IIO_VAL_INT;
0264             goto out;
0265         default:
0266             ret = -EINVAL;
0267             goto out;
0268         }
0269     default:
0270         ret = -EINVAL;
0271     }
0272 
0273 out:
0274     pm_runtime_mark_last_busy(gp2ap002->dev);
0275     pm_runtime_put_autosuspend(gp2ap002->dev);
0276 
0277     return ret;
0278 }
0279 
0280 static int gp2ap002_init(struct gp2ap002 *gp2ap002)
0281 {
0282     int ret;
0283 
0284     /* Set up the IR LED resistance */
0285     ret = regmap_write(gp2ap002->map, GP2AP002_GAIN,
0286                GP2AP002_GAIN_LED_NORMAL);
0287     if (ret) {
0288         dev_err(gp2ap002->dev, "error setting up LED gain\n");
0289         return ret;
0290     }
0291     ret = regmap_write(gp2ap002->map, GP2AP002_HYS, gp2ap002->hys_far);
0292     if (ret) {
0293         dev_err(gp2ap002->dev,
0294             "error setting up proximity hysteresis\n");
0295         return ret;
0296     }
0297 
0298     /* Disable internal frequency hopping */
0299     ret = regmap_write(gp2ap002->map, GP2AP002_CYCLE,
0300                GP2AP002_CYCLE_OSC_INEFFECTIVE);
0301     if (ret) {
0302         dev_err(gp2ap002->dev,
0303             "error setting up internal frequency hopping\n");
0304         return ret;
0305     }
0306 
0307     /* Enable chip and IRQ, disable analog sleep */
0308     ret = regmap_write(gp2ap002->map, GP2AP002_OPMOD,
0309                GP2AP002_OPMOD_SSD_OPERATING |
0310                GP2AP002_OPMOD_VCON_IRQ);
0311     if (ret) {
0312         dev_err(gp2ap002->dev, "error setting up operation mode\n");
0313         return ret;
0314     }
0315 
0316     /* Interrupt on VOUT enabled */
0317     ret = regmap_write(gp2ap002->map, GP2AP002_CON,
0318                GP2AP002_CON_OCON_ENABLE);
0319     if (ret)
0320         dev_err(gp2ap002->dev, "error setting up VOUT control\n");
0321 
0322     return ret;
0323 }
0324 
0325 static int gp2ap002_read_event_config(struct iio_dev *indio_dev,
0326                       const struct iio_chan_spec *chan,
0327                       enum iio_event_type type,
0328                       enum iio_event_direction dir)
0329 {
0330     struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
0331 
0332     /*
0333      * We just keep track of this internally, as it is not possible to
0334      * query the hardware.
0335      */
0336     return gp2ap002->enabled;
0337 }
0338 
0339 static int gp2ap002_write_event_config(struct iio_dev *indio_dev,
0340                        const struct iio_chan_spec *chan,
0341                        enum iio_event_type type,
0342                        enum iio_event_direction dir,
0343                        int state)
0344 {
0345     struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
0346 
0347     if (state) {
0348         /*
0349          * This will bring the regulators up (unless they are on
0350          * already) and reintialize the sensor by using runtime_pm
0351          * callbacks.
0352          */
0353         pm_runtime_get_sync(gp2ap002->dev);
0354         gp2ap002->enabled = true;
0355     } else {
0356         pm_runtime_mark_last_busy(gp2ap002->dev);
0357         pm_runtime_put_autosuspend(gp2ap002->dev);
0358         gp2ap002->enabled = false;
0359     }
0360 
0361     return 0;
0362 }
0363 
0364 static const struct iio_info gp2ap002_info = {
0365     .read_raw = gp2ap002_read_raw,
0366     .read_event_config = gp2ap002_read_event_config,
0367     .write_event_config = gp2ap002_write_event_config,
0368 };
0369 
0370 static const struct iio_event_spec gp2ap002_events[] = {
0371     {
0372         .type = IIO_EV_TYPE_THRESH,
0373         .dir = IIO_EV_DIR_EITHER,
0374         .mask_separate = BIT(IIO_EV_INFO_ENABLE),
0375     },
0376 };
0377 
0378 static const struct iio_chan_spec gp2ap002_channels[] = {
0379     {
0380         .type = IIO_PROXIMITY,
0381         .event_spec = gp2ap002_events,
0382         .num_event_specs = ARRAY_SIZE(gp2ap002_events),
0383     },
0384     {
0385         .type = IIO_LIGHT,
0386         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0387         .channel = GP2AP002_ALS_CHANNEL,
0388     },
0389 };
0390 
0391 /*
0392  * We need a special regmap because this hardware expects to
0393  * write single bytes to registers but read a 16bit word on some
0394  * variants and discard the lower 8 bits so combine
0395  * i2c_smbus_read_word_data() with i2c_smbus_write_byte_data()
0396  * selectively like this.
0397  */
0398 static int gp2ap002_regmap_i2c_read(void *context, unsigned int reg,
0399                     unsigned int *val)
0400 {
0401     struct device *dev = context;
0402     struct i2c_client *i2c = to_i2c_client(dev);
0403     int ret;
0404 
0405     ret = i2c_smbus_read_word_data(i2c, reg);
0406     if (ret < 0)
0407         return ret;
0408 
0409     *val = (ret >> 8) & 0xFF;
0410 
0411     return 0;
0412 }
0413 
0414 static int gp2ap002_regmap_i2c_write(void *context, unsigned int reg,
0415                      unsigned int val)
0416 {
0417     struct device *dev = context;
0418     struct i2c_client *i2c = to_i2c_client(dev);
0419 
0420     return i2c_smbus_write_byte_data(i2c, reg, val);
0421 }
0422 
0423 static struct regmap_bus gp2ap002_regmap_bus = {
0424     .reg_read = gp2ap002_regmap_i2c_read,
0425     .reg_write = gp2ap002_regmap_i2c_write,
0426 };
0427 
0428 static int gp2ap002_probe(struct i2c_client *client,
0429               const struct i2c_device_id *id)
0430 {
0431     struct gp2ap002 *gp2ap002;
0432     struct iio_dev *indio_dev;
0433     struct device *dev = &client->dev;
0434     enum iio_chan_type ch_type;
0435     static const struct regmap_config config = {
0436         .reg_bits = 8,
0437         .val_bits = 8,
0438         .max_register = GP2AP002_CON,
0439     };
0440     struct regmap *regmap;
0441     int num_chan;
0442     const char *compat;
0443     u8 val;
0444     int ret;
0445 
0446     indio_dev = devm_iio_device_alloc(dev, sizeof(*gp2ap002));
0447     if (!indio_dev)
0448         return -ENOMEM;
0449     i2c_set_clientdata(client, indio_dev);
0450 
0451     gp2ap002 = iio_priv(indio_dev);
0452     gp2ap002->dev = dev;
0453 
0454     /*
0455      * Check the device compatible like this makes it possible to use
0456      * ACPI PRP0001 for registering the sensor using device tree
0457      * properties.
0458      */
0459     ret = device_property_read_string(dev, "compatible", &compat);
0460     if (ret) {
0461         dev_err(dev, "cannot check compatible\n");
0462         return ret;
0463     }
0464     gp2ap002->is_gp2ap002s00f = !strcmp(compat, "sharp,gp2ap002s00f");
0465 
0466     regmap = devm_regmap_init(dev, &gp2ap002_regmap_bus, dev, &config);
0467     if (IS_ERR(regmap)) {
0468         dev_err(dev, "Failed to register i2c regmap %ld\n", PTR_ERR(regmap));
0469         return PTR_ERR(regmap);
0470     }
0471     gp2ap002->map = regmap;
0472 
0473     /*
0474      * The hysteresis settings are coded into the device tree as values
0475      * to be written into the hysteresis register. The datasheet defines
0476      * modes "A", "B1" and "B2" with fixed values to be use but vendor
0477      * code trees for actual devices are tweaking these values and refer to
0478      * modes named things like "B1.5". To be able to support any devices,
0479      * we allow passing an arbitrary hysteresis setting for "near" and
0480      * "far".
0481      */
0482 
0483     /* Check the device tree for the IR LED hysteresis */
0484     ret = device_property_read_u8(dev, "sharp,proximity-far-hysteresis",
0485                       &val);
0486     if (ret) {
0487         dev_err(dev, "failed to obtain proximity far setting\n");
0488         return ret;
0489     }
0490     dev_dbg(dev, "proximity far setting %02x\n", val);
0491     gp2ap002->hys_far = val;
0492 
0493     ret = device_property_read_u8(dev, "sharp,proximity-close-hysteresis",
0494                       &val);
0495     if (ret) {
0496         dev_err(dev, "failed to obtain proximity close setting\n");
0497         return ret;
0498     }
0499     dev_dbg(dev, "proximity close setting %02x\n", val);
0500     gp2ap002->hys_close = val;
0501 
0502     /* The GP2AP002A00F has a light sensor too */
0503     if (!gp2ap002->is_gp2ap002s00f) {
0504         gp2ap002->alsout = devm_iio_channel_get(dev, "alsout");
0505         if (IS_ERR(gp2ap002->alsout)) {
0506             ret = PTR_ERR(gp2ap002->alsout);
0507             ret = (ret == -ENODEV) ? -EPROBE_DEFER : ret;
0508             return dev_err_probe(dev, ret, "failed to get ALSOUT ADC channel\n");
0509         }
0510         ret = iio_get_channel_type(gp2ap002->alsout, &ch_type);
0511         if (ret < 0)
0512             return ret;
0513         if (ch_type != IIO_CURRENT) {
0514             dev_err(dev,
0515                 "wrong type of IIO channel specified for ALSOUT\n");
0516             return -EINVAL;
0517         }
0518     }
0519 
0520     gp2ap002->vdd = devm_regulator_get(dev, "vdd");
0521     if (IS_ERR(gp2ap002->vdd))
0522         return dev_err_probe(dev, PTR_ERR(gp2ap002->vdd),
0523                      "failed to get VDD regulator\n");
0524 
0525     gp2ap002->vio = devm_regulator_get(dev, "vio");
0526     if (IS_ERR(gp2ap002->vio))
0527         return dev_err_probe(dev, PTR_ERR(gp2ap002->vio),
0528                      "failed to get VIO regulator\n");
0529 
0530     /* Operating voltage 2.4V .. 3.6V according to datasheet */
0531     ret = regulator_set_voltage(gp2ap002->vdd, 2400000, 3600000);
0532     if (ret) {
0533         dev_err(dev, "failed to sett VDD voltage\n");
0534         return ret;
0535     }
0536 
0537     /* VIO should be between 1.65V and VDD */
0538     ret = regulator_get_voltage(gp2ap002->vdd);
0539     if (ret < 0) {
0540         dev_err(dev, "failed to get VDD voltage\n");
0541         return ret;
0542     }
0543     ret = regulator_set_voltage(gp2ap002->vio, 1650000, ret);
0544     if (ret) {
0545         dev_err(dev, "failed to set VIO voltage\n");
0546         return ret;
0547     }
0548 
0549     ret = regulator_enable(gp2ap002->vdd);
0550     if (ret) {
0551         dev_err(dev, "failed to enable VDD regulator\n");
0552         return ret;
0553     }
0554     ret = regulator_enable(gp2ap002->vio);
0555     if (ret) {
0556         dev_err(dev, "failed to enable VIO regulator\n");
0557         goto out_disable_vdd;
0558     }
0559 
0560     msleep(20);
0561 
0562     /*
0563      * Initialize the device and signal to runtime PM that now we are
0564      * definitely up and using power.
0565      */
0566     ret = gp2ap002_init(gp2ap002);
0567     if (ret) {
0568         dev_err(dev, "initialization failed\n");
0569         goto out_disable_vio;
0570     }
0571     pm_runtime_get_noresume(dev);
0572     pm_runtime_set_active(dev);
0573     pm_runtime_enable(dev);
0574     gp2ap002->enabled = false;
0575 
0576     ret = devm_request_threaded_irq(dev, client->irq, NULL,
0577                     gp2ap002_prox_irq, IRQF_ONESHOT,
0578                     "gp2ap002", indio_dev);
0579     if (ret) {
0580         dev_err(dev, "unable to request IRQ\n");
0581         goto out_put_pm;
0582     }
0583     gp2ap002->irq = client->irq;
0584 
0585     /*
0586      * As the device takes 20 ms + regulator delay to come up with a fresh
0587      * measurement after power-on, do not shut it down unnecessarily.
0588      * Set autosuspend to a one second.
0589      */
0590     pm_runtime_set_autosuspend_delay(dev, 1000);
0591     pm_runtime_use_autosuspend(dev);
0592     pm_runtime_put(dev);
0593 
0594     indio_dev->info = &gp2ap002_info;
0595     indio_dev->name = "gp2ap002";
0596     indio_dev->channels = gp2ap002_channels;
0597     /* Skip light channel for the proximity-only sensor */
0598     num_chan = ARRAY_SIZE(gp2ap002_channels);
0599     if (gp2ap002->is_gp2ap002s00f)
0600         num_chan--;
0601     indio_dev->num_channels = num_chan;
0602     indio_dev->modes = INDIO_DIRECT_MODE;
0603 
0604     ret = iio_device_register(indio_dev);
0605     if (ret)
0606         goto out_disable_pm;
0607     dev_dbg(dev, "Sharp GP2AP002 probed successfully\n");
0608 
0609     return 0;
0610 
0611 out_put_pm:
0612     pm_runtime_put_noidle(dev);
0613 out_disable_pm:
0614     pm_runtime_disable(dev);
0615 out_disable_vio:
0616     regulator_disable(gp2ap002->vio);
0617 out_disable_vdd:
0618     regulator_disable(gp2ap002->vdd);
0619     return ret;
0620 }
0621 
0622 static int gp2ap002_remove(struct i2c_client *client)
0623 {
0624     struct iio_dev *indio_dev = i2c_get_clientdata(client);
0625     struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
0626     struct device *dev = &client->dev;
0627 
0628     pm_runtime_get_sync(dev);
0629     pm_runtime_put_noidle(dev);
0630     pm_runtime_disable(dev);
0631     iio_device_unregister(indio_dev);
0632     regulator_disable(gp2ap002->vio);
0633     regulator_disable(gp2ap002->vdd);
0634 
0635     return 0;
0636 }
0637 
0638 static int gp2ap002_runtime_suspend(struct device *dev)
0639 {
0640     struct iio_dev *indio_dev = dev_get_drvdata(dev);
0641     struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
0642     int ret;
0643 
0644     /* Deactivate the IRQ */
0645     disable_irq(gp2ap002->irq);
0646 
0647     /* Disable chip and IRQ, everything off */
0648     ret = regmap_write(gp2ap002->map, GP2AP002_OPMOD, 0x00);
0649     if (ret) {
0650         dev_err(gp2ap002->dev, "error setting up operation mode\n");
0651         return ret;
0652     }
0653     /*
0654      * As these regulators may be shared, at least we are now in
0655      * sleep even if the regulators aren't really turned off.
0656      */
0657     regulator_disable(gp2ap002->vio);
0658     regulator_disable(gp2ap002->vdd);
0659 
0660     return 0;
0661 }
0662 
0663 static int gp2ap002_runtime_resume(struct device *dev)
0664 {
0665     struct iio_dev *indio_dev = dev_get_drvdata(dev);
0666     struct gp2ap002 *gp2ap002 = iio_priv(indio_dev);
0667     int ret;
0668 
0669     ret = regulator_enable(gp2ap002->vdd);
0670     if (ret) {
0671         dev_err(dev, "failed to enable VDD regulator in resume path\n");
0672         return ret;
0673     }
0674     ret = regulator_enable(gp2ap002->vio);
0675     if (ret) {
0676         dev_err(dev, "failed to enable VIO regulator in resume path\n");
0677         return ret;
0678     }
0679 
0680     msleep(20);
0681 
0682     ret = gp2ap002_init(gp2ap002);
0683     if (ret) {
0684         dev_err(dev, "re-initialization failed\n");
0685         return ret;
0686     }
0687 
0688     /* Re-activate the IRQ */
0689     enable_irq(gp2ap002->irq);
0690 
0691     return 0;
0692 }
0693 
0694 static DEFINE_RUNTIME_DEV_PM_OPS(gp2ap002_dev_pm_ops, gp2ap002_runtime_suspend,
0695                  gp2ap002_runtime_resume, NULL);
0696 
0697 static const struct i2c_device_id gp2ap002_id_table[] = {
0698     { "gp2ap002", 0 },
0699     { },
0700 };
0701 MODULE_DEVICE_TABLE(i2c, gp2ap002_id_table);
0702 
0703 static const struct of_device_id gp2ap002_of_match[] = {
0704     { .compatible = "sharp,gp2ap002a00f" },
0705     { .compatible = "sharp,gp2ap002s00f" },
0706     { },
0707 };
0708 MODULE_DEVICE_TABLE(of, gp2ap002_of_match);
0709 
0710 static struct i2c_driver gp2ap002_driver = {
0711     .driver = {
0712         .name = "gp2ap002",
0713         .of_match_table = gp2ap002_of_match,
0714         .pm = pm_ptr(&gp2ap002_dev_pm_ops),
0715     },
0716     .probe = gp2ap002_probe,
0717     .remove = gp2ap002_remove,
0718     .id_table = gp2ap002_id_table,
0719 };
0720 module_i2c_driver(gp2ap002_driver);
0721 
0722 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
0723 MODULE_DESCRIPTION("GP2AP002 ambient light and proximity sensor driver");
0724 MODULE_LICENSE("GPL v2");