0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/i2c.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/mutex.h>
0016 #include <linux/regulator/consumer.h>
0017 #include <linux/pm_runtime.h>
0018 #include <linux/delay.h>
0019 #include <linux/wait.h>
0020 #include <linux/slab.h>
0021 #include <linux/platform_data/apds990x.h>
0022
0023
0024 #define APDS990X_ENABLE 0x00
0025 #define APDS990X_ATIME 0x01
0026 #define APDS990X_PTIME 0x02
0027 #define APDS990X_WTIME 0x03
0028 #define APDS990X_AILTL 0x04
0029 #define APDS990X_AILTH 0x05
0030 #define APDS990X_AIHTL 0x06
0031 #define APDS990X_AIHTH 0x07
0032 #define APDS990X_PILTL 0x08
0033 #define APDS990X_PILTH 0x09
0034 #define APDS990X_PIHTL 0x0a
0035 #define APDS990X_PIHTH 0x0b
0036 #define APDS990X_PERS 0x0c
0037 #define APDS990X_CONFIG 0x0d
0038 #define APDS990X_PPCOUNT 0x0e
0039 #define APDS990X_CONTROL 0x0f
0040 #define APDS990X_REV 0x11
0041 #define APDS990X_ID 0x12
0042 #define APDS990X_STATUS 0x13
0043 #define APDS990X_CDATAL 0x14
0044 #define APDS990X_CDATAH 0x15
0045 #define APDS990X_IRDATAL 0x16
0046 #define APDS990X_IRDATAH 0x17
0047 #define APDS990X_PDATAL 0x18
0048 #define APDS990X_PDATAH 0x19
0049
0050
0051 #define APDS990X_MAX_AGAIN 3
0052
0053
0054 #define APDS990X_EN_PIEN (0x1 << 5)
0055 #define APDS990X_EN_AIEN (0x1 << 4)
0056 #define APDS990X_EN_WEN (0x1 << 3)
0057 #define APDS990X_EN_PEN (0x1 << 2)
0058 #define APDS990X_EN_AEN (0x1 << 1)
0059 #define APDS990X_EN_PON (0x1 << 0)
0060 #define APDS990X_EN_DISABLE_ALL 0
0061
0062
0063 #define APDS990X_ST_PINT (0x1 << 5)
0064 #define APDS990X_ST_AINT (0x1 << 4)
0065
0066
0067 #define APDS990x_CMD_TYPE_MASK (0x03 << 5)
0068 #define APDS990x_CMD_TYPE_RB (0x00 << 5)
0069 #define APDS990x_CMD_TYPE_INC (0x01 << 5)
0070 #define APDS990x_CMD_TYPE_SPE (0x03 << 5)
0071
0072 #define APDS990x_ADDR_SHIFT 0
0073 #define APDS990x_CMD 0x80
0074
0075
0076 #define APDS990X_INT_ACK_ALS 0x6
0077 #define APDS990X_INT_ACK_PS 0x5
0078 #define APDS990X_INT_ACK_BOTH 0x7
0079
0080
0081 #define APDS990X_PTIME_DEFAULT 0xff
0082
0083
0084 #define APDS990X_WTIME_DEFAULT 0xee
0085
0086 #define APDS990X_TIME_TO_ADC 1024
0087
0088
0089 #define APDS990X_APERS_SHIFT 0
0090 #define APDS990X_PPERS_SHIFT 4
0091
0092
0093 #define APDS990X_ID_0 0x0
0094 #define APDS990X_ID_4 0x4
0095 #define APDS990X_ID_29 0x29
0096
0097
0098 #define APDS_PGAIN_1X 0x0
0099 #define APDS_PDIODE_IR 0x2
0100
0101 #define APDS990X_LUX_OUTPUT_SCALE 10
0102
0103
0104 struct reverse_factors {
0105 u32 afactor;
0106 int cf1;
0107 int irf1;
0108 int cf2;
0109 int irf2;
0110 };
0111
0112 struct apds990x_chip {
0113 struct apds990x_platform_data *pdata;
0114 struct i2c_client *client;
0115 struct mutex mutex;
0116 struct regulator_bulk_data regs[2];
0117 wait_queue_head_t wait;
0118
0119 int prox_en;
0120 bool prox_continuous_mode;
0121 bool lux_wait_fresh_res;
0122
0123
0124 struct apds990x_chip_factors cf;
0125 struct reverse_factors rcf;
0126 u16 atime;
0127 u16 arate;
0128 u16 a_max_result;
0129 u8 again_meas;
0130 u8 again_next;
0131 u8 pgain;
0132 u8 pdiode;
0133 u8 pdrive;
0134 u8 lux_persistence;
0135 u8 prox_persistence;
0136
0137 u32 lux_raw;
0138 u32 lux;
0139 u16 lux_clear;
0140 u16 lux_ir;
0141 u16 lux_calib;
0142 u32 lux_thres_hi;
0143 u32 lux_thres_lo;
0144
0145 u32 prox_thres;
0146 u16 prox_data;
0147 u16 prox_calib;
0148
0149 char chipname[10];
0150 u8 revision;
0151 };
0152
0153 #define APDS_CALIB_SCALER 8192
0154 #define APDS_LUX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
0155 #define APDS_PROX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
0156
0157 #define APDS_PROX_DEF_THRES 600
0158 #define APDS_PROX_HYSTERESIS 50
0159 #define APDS_LUX_DEF_THRES_HI 101
0160 #define APDS_LUX_DEF_THRES_LO 100
0161 #define APDS_DEFAULT_PROX_PERS 1
0162
0163 #define APDS_TIMEOUT 2000
0164 #define APDS_STARTUP_DELAY 25000
0165 #define APDS_RANGE 65535
0166 #define APDS_PROX_RANGE 1023
0167 #define APDS_LUX_GAIN_LO_LIMIT 100
0168 #define APDS_LUX_GAIN_LO_LIMIT_STRICT 25
0169
0170 #define TIMESTEP 87
0171 #define TIME_STEP_SCALER 32
0172
0173 #define APDS_LUX_AVERAGING_TIME 50
0174 #define APDS_LUX_DEFAULT_RATE 200
0175
0176 static const u8 again[] = {1, 8, 16, 120};
0177
0178
0179 static const u16 arates_hz[] = {10, 5, 2, 1};
0180 static const u8 apersis[] = {1, 2, 4, 5};
0181
0182
0183 static const char reg_vcc[] = "Vdd";
0184 static const char reg_vled[] = "Vled";
0185
0186 static int apds990x_read_byte(struct apds990x_chip *chip, u8 reg, u8 *data)
0187 {
0188 struct i2c_client *client = chip->client;
0189 s32 ret;
0190
0191 reg &= ~APDS990x_CMD_TYPE_MASK;
0192 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
0193
0194 ret = i2c_smbus_read_byte_data(client, reg);
0195 *data = ret;
0196 return (int)ret;
0197 }
0198
0199 static int apds990x_read_word(struct apds990x_chip *chip, u8 reg, u16 *data)
0200 {
0201 struct i2c_client *client = chip->client;
0202 s32 ret;
0203
0204 reg &= ~APDS990x_CMD_TYPE_MASK;
0205 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
0206
0207 ret = i2c_smbus_read_word_data(client, reg);
0208 *data = ret;
0209 return (int)ret;
0210 }
0211
0212 static int apds990x_write_byte(struct apds990x_chip *chip, u8 reg, u8 data)
0213 {
0214 struct i2c_client *client = chip->client;
0215 s32 ret;
0216
0217 reg &= ~APDS990x_CMD_TYPE_MASK;
0218 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
0219
0220 ret = i2c_smbus_write_byte_data(client, reg, data);
0221 return (int)ret;
0222 }
0223
0224 static int apds990x_write_word(struct apds990x_chip *chip, u8 reg, u16 data)
0225 {
0226 struct i2c_client *client = chip->client;
0227 s32 ret;
0228
0229 reg &= ~APDS990x_CMD_TYPE_MASK;
0230 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
0231
0232 ret = i2c_smbus_write_word_data(client, reg, data);
0233 return (int)ret;
0234 }
0235
0236 static int apds990x_mode_on(struct apds990x_chip *chip)
0237 {
0238
0239 u8 reg = APDS990X_EN_AIEN | APDS990X_EN_PON | APDS990X_EN_AEN |
0240 APDS990X_EN_WEN;
0241
0242 if (chip->prox_en)
0243 reg |= APDS990X_EN_PIEN | APDS990X_EN_PEN;
0244
0245 return apds990x_write_byte(chip, APDS990X_ENABLE, reg);
0246 }
0247
0248 static u16 apds990x_lux_to_threshold(struct apds990x_chip *chip, u32 lux)
0249 {
0250 u32 thres;
0251 u32 cpl;
0252 u32 ir;
0253
0254 if (lux == 0)
0255 return 0;
0256 else if (lux == APDS_RANGE)
0257 return APDS_RANGE;
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274 lux = lux * (APDS_CALIB_SCALER / 4) / (chip->lux_calib / 4);
0275
0276
0277 cpl = ((u32)chip->atime * (u32)again[chip->again_next] *
0278 APDS_PARAM_SCALE * 64) / (chip->cf.ga * chip->cf.df);
0279
0280 thres = lux * cpl / 64;
0281
0282
0283
0284
0285
0286 ir = (u32)chip->lux_ir * (u32)again[chip->again_next] /
0287 (u32)again[chip->again_meas];
0288
0289
0290
0291
0292
0293 if (chip->lux_clear * APDS_PARAM_SCALE >=
0294 chip->rcf.afactor * chip->lux_ir)
0295 thres = (chip->rcf.cf1 * thres + chip->rcf.irf1 * ir) /
0296 APDS_PARAM_SCALE;
0297 else
0298 thres = (chip->rcf.cf2 * thres + chip->rcf.irf2 * ir) /
0299 APDS_PARAM_SCALE;
0300
0301 if (thres >= chip->a_max_result)
0302 thres = chip->a_max_result - 1;
0303 return thres;
0304 }
0305
0306 static inline int apds990x_set_atime(struct apds990x_chip *chip, u32 time_ms)
0307 {
0308 u8 reg_value;
0309
0310 chip->atime = time_ms;
0311
0312 reg_value = 256 - ((time_ms * TIME_STEP_SCALER) / TIMESTEP);
0313
0314 chip->a_max_result = (u16)(256 - reg_value) * APDS990X_TIME_TO_ADC;
0315 return apds990x_write_byte(chip, APDS990X_ATIME, reg_value);
0316 }
0317
0318
0319 static int apds990x_refresh_pthres(struct apds990x_chip *chip, int data)
0320 {
0321 int ret, lo, hi;
0322
0323
0324 if (pm_runtime_suspended(&chip->client->dev))
0325 return 0;
0326
0327 if (data < chip->prox_thres) {
0328 lo = 0;
0329 hi = chip->prox_thres;
0330 } else {
0331 lo = chip->prox_thres - APDS_PROX_HYSTERESIS;
0332 if (chip->prox_continuous_mode)
0333 hi = chip->prox_thres;
0334 else
0335 hi = APDS_RANGE;
0336 }
0337
0338 ret = apds990x_write_word(chip, APDS990X_PILTL, lo);
0339 ret |= apds990x_write_word(chip, APDS990X_PIHTL, hi);
0340 return ret;
0341 }
0342
0343
0344 static int apds990x_refresh_athres(struct apds990x_chip *chip)
0345 {
0346 int ret;
0347
0348 if (pm_runtime_suspended(&chip->client->dev))
0349 return 0;
0350
0351 ret = apds990x_write_word(chip, APDS990X_AILTL,
0352 apds990x_lux_to_threshold(chip, chip->lux_thres_lo));
0353 ret |= apds990x_write_word(chip, APDS990X_AIHTL,
0354 apds990x_lux_to_threshold(chip, chip->lux_thres_hi));
0355
0356 return ret;
0357 }
0358
0359
0360 static void apds990x_force_a_refresh(struct apds990x_chip *chip)
0361 {
0362
0363 apds990x_write_word(chip, APDS990X_AILTL, APDS_LUX_DEF_THRES_LO);
0364 apds990x_write_word(chip, APDS990X_AIHTL, APDS_LUX_DEF_THRES_HI);
0365 }
0366
0367
0368 static void apds990x_force_p_refresh(struct apds990x_chip *chip)
0369 {
0370
0371 apds990x_write_word(chip, APDS990X_PILTL, APDS_PROX_DEF_THRES - 1);
0372 apds990x_write_word(chip, APDS990X_PIHTL, APDS_PROX_DEF_THRES);
0373 }
0374
0375
0376 static int apds990x_calc_again(struct apds990x_chip *chip)
0377 {
0378 int curr_again = chip->again_meas;
0379 int next_again = chip->again_meas;
0380 int ret = 0;
0381
0382
0383 if (chip->lux_clear == chip->a_max_result)
0384 next_again -= 2;
0385 else if (chip->lux_clear > chip->a_max_result / 2)
0386 next_again--;
0387 else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
0388 next_again += 2;
0389 else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT)
0390 next_again++;
0391
0392
0393 if (next_again < 0)
0394 next_again = 0;
0395 else if (next_again > APDS990X_MAX_AGAIN)
0396 next_again = APDS990X_MAX_AGAIN;
0397
0398
0399 if (chip->lux_clear == chip->a_max_result)
0400
0401 ret = -ERANGE;
0402 else if (next_again != curr_again &&
0403 chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
0404
0405
0406
0407
0408 ret = -ERANGE;
0409
0410 chip->again_next = next_again;
0411 apds990x_write_byte(chip, APDS990X_CONTROL,
0412 (chip->pdrive << 6) |
0413 (chip->pdiode << 4) |
0414 (chip->pgain << 2) |
0415 (chip->again_next << 0));
0416
0417
0418
0419
0420
0421
0422 if (ret < 0)
0423 apds990x_force_a_refresh(chip);
0424 else
0425 apds990x_refresh_athres(chip);
0426
0427 return ret;
0428 }
0429
0430
0431 static int apds990x_get_lux(struct apds990x_chip *chip, int clear, int ir)
0432 {
0433 int iac, iac1, iac2;
0434 u32 lpc;
0435
0436
0437
0438
0439
0440 iac1 = (chip->cf.cf1 * clear - chip->cf.irf1 * ir) / APDS_PARAM_SCALE;
0441 iac2 = (chip->cf.cf2 * clear - chip->cf.irf2 * ir) / APDS_PARAM_SCALE;
0442
0443 iac = max(iac1, iac2);
0444 iac = max(iac, 0);
0445
0446 lpc = APDS990X_LUX_OUTPUT_SCALE * (chip->cf.df * chip->cf.ga) /
0447 (u32)(again[chip->again_meas] * (u32)chip->atime);
0448
0449 return (iac * lpc) / APDS_PARAM_SCALE;
0450 }
0451
0452 static int apds990x_ack_int(struct apds990x_chip *chip, u8 mode)
0453 {
0454 struct i2c_client *client = chip->client;
0455 s32 ret;
0456 u8 reg = APDS990x_CMD | APDS990x_CMD_TYPE_SPE;
0457
0458 switch (mode & (APDS990X_ST_AINT | APDS990X_ST_PINT)) {
0459 case APDS990X_ST_AINT:
0460 reg |= APDS990X_INT_ACK_ALS;
0461 break;
0462 case APDS990X_ST_PINT:
0463 reg |= APDS990X_INT_ACK_PS;
0464 break;
0465 default:
0466 reg |= APDS990X_INT_ACK_BOTH;
0467 break;
0468 }
0469
0470 ret = i2c_smbus_read_byte_data(client, reg);
0471 return (int)ret;
0472 }
0473
0474 static irqreturn_t apds990x_irq(int irq, void *data)
0475 {
0476 struct apds990x_chip *chip = data;
0477 u8 status;
0478
0479 apds990x_read_byte(chip, APDS990X_STATUS, &status);
0480 apds990x_ack_int(chip, status);
0481
0482 mutex_lock(&chip->mutex);
0483 if (!pm_runtime_suspended(&chip->client->dev)) {
0484 if (status & APDS990X_ST_AINT) {
0485 apds990x_read_word(chip, APDS990X_CDATAL,
0486 &chip->lux_clear);
0487 apds990x_read_word(chip, APDS990X_IRDATAL,
0488 &chip->lux_ir);
0489
0490 chip->again_meas = chip->again_next;
0491
0492 chip->lux_raw = apds990x_get_lux(chip,
0493 chip->lux_clear,
0494 chip->lux_ir);
0495
0496 if (apds990x_calc_again(chip) == 0) {
0497
0498 chip->lux = chip->lux_raw;
0499 chip->lux_wait_fresh_res = false;
0500 wake_up(&chip->wait);
0501 sysfs_notify(&chip->client->dev.kobj,
0502 NULL, "lux0_input");
0503 }
0504 }
0505
0506 if ((status & APDS990X_ST_PINT) && chip->prox_en) {
0507 u16 clr_ch;
0508
0509 apds990x_read_word(chip, APDS990X_CDATAL, &clr_ch);
0510
0511
0512
0513
0514
0515 if (chip->again_meas == 0 &&
0516 clr_ch == chip->a_max_result)
0517 chip->prox_data = 0;
0518 else
0519 apds990x_read_word(chip,
0520 APDS990X_PDATAL,
0521 &chip->prox_data);
0522
0523 apds990x_refresh_pthres(chip, chip->prox_data);
0524 if (chip->prox_data < chip->prox_thres)
0525 chip->prox_data = 0;
0526 else if (!chip->prox_continuous_mode)
0527 chip->prox_data = APDS_PROX_RANGE;
0528 sysfs_notify(&chip->client->dev.kobj,
0529 NULL, "prox0_raw");
0530 }
0531 }
0532 mutex_unlock(&chip->mutex);
0533 return IRQ_HANDLED;
0534 }
0535
0536 static int apds990x_configure(struct apds990x_chip *chip)
0537 {
0538
0539 apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
0540
0541
0542 apds990x_write_byte(chip, APDS990X_PTIME, APDS990X_PTIME_DEFAULT);
0543 apds990x_write_byte(chip, APDS990X_WTIME, APDS990X_WTIME_DEFAULT);
0544 apds990x_set_atime(chip, APDS_LUX_AVERAGING_TIME);
0545
0546 apds990x_write_byte(chip, APDS990X_CONFIG, 0);
0547
0548
0549 apds990x_write_byte(chip, APDS990X_PERS,
0550 (chip->lux_persistence << APDS990X_APERS_SHIFT) |
0551 (chip->prox_persistence << APDS990X_PPERS_SHIFT));
0552
0553 apds990x_write_byte(chip, APDS990X_PPCOUNT, chip->pdata->ppcount);
0554
0555
0556 chip->again_meas = 1;
0557 chip->again_next = 1;
0558 apds990x_write_byte(chip, APDS990X_CONTROL,
0559 (chip->pdrive << 6) |
0560 (chip->pdiode << 4) |
0561 (chip->pgain << 2) |
0562 (chip->again_next << 0));
0563 return 0;
0564 }
0565
0566 static int apds990x_detect(struct apds990x_chip *chip)
0567 {
0568 struct i2c_client *client = chip->client;
0569 int ret;
0570 u8 id;
0571
0572 ret = apds990x_read_byte(chip, APDS990X_ID, &id);
0573 if (ret < 0) {
0574 dev_err(&client->dev, "ID read failed\n");
0575 return ret;
0576 }
0577
0578 ret = apds990x_read_byte(chip, APDS990X_REV, &chip->revision);
0579 if (ret < 0) {
0580 dev_err(&client->dev, "REV read failed\n");
0581 return ret;
0582 }
0583
0584 switch (id) {
0585 case APDS990X_ID_0:
0586 case APDS990X_ID_4:
0587 case APDS990X_ID_29:
0588 snprintf(chip->chipname, sizeof(chip->chipname), "APDS-990x");
0589 break;
0590 default:
0591 ret = -ENODEV;
0592 break;
0593 }
0594 return ret;
0595 }
0596
0597 #ifdef CONFIG_PM
0598 static int apds990x_chip_on(struct apds990x_chip *chip)
0599 {
0600 int err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
0601 chip->regs);
0602 if (err < 0)
0603 return err;
0604
0605 usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
0606
0607
0608 chip->prox_data = 0;
0609 apds990x_configure(chip);
0610 apds990x_mode_on(chip);
0611 return 0;
0612 }
0613 #endif
0614
0615 static int apds990x_chip_off(struct apds990x_chip *chip)
0616 {
0617 apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
0618 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
0619 return 0;
0620 }
0621
0622 static ssize_t apds990x_lux_show(struct device *dev,
0623 struct device_attribute *attr, char *buf)
0624 {
0625 struct apds990x_chip *chip = dev_get_drvdata(dev);
0626 ssize_t ret;
0627 u32 result;
0628 long timeout;
0629
0630 if (pm_runtime_suspended(dev))
0631 return -EIO;
0632
0633 timeout = wait_event_interruptible_timeout(chip->wait,
0634 !chip->lux_wait_fresh_res,
0635 msecs_to_jiffies(APDS_TIMEOUT));
0636 if (!timeout)
0637 return -EIO;
0638
0639 mutex_lock(&chip->mutex);
0640 result = (chip->lux * chip->lux_calib) / APDS_CALIB_SCALER;
0641 if (result > (APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE))
0642 result = APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE;
0643
0644 ret = sprintf(buf, "%d.%d\n",
0645 result / APDS990X_LUX_OUTPUT_SCALE,
0646 result % APDS990X_LUX_OUTPUT_SCALE);
0647 mutex_unlock(&chip->mutex);
0648 return ret;
0649 }
0650
0651 static DEVICE_ATTR(lux0_input, S_IRUGO, apds990x_lux_show, NULL);
0652
0653 static ssize_t apds990x_lux_range_show(struct device *dev,
0654 struct device_attribute *attr, char *buf)
0655 {
0656 return sprintf(buf, "%u\n", APDS_RANGE);
0657 }
0658
0659 static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, apds990x_lux_range_show, NULL);
0660
0661 static ssize_t apds990x_lux_calib_format_show(struct device *dev,
0662 struct device_attribute *attr, char *buf)
0663 {
0664 return sprintf(buf, "%u\n", APDS_CALIB_SCALER);
0665 }
0666
0667 static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
0668 apds990x_lux_calib_format_show, NULL);
0669
0670 static ssize_t apds990x_lux_calib_show(struct device *dev,
0671 struct device_attribute *attr, char *buf)
0672 {
0673 struct apds990x_chip *chip = dev_get_drvdata(dev);
0674
0675 return sprintf(buf, "%u\n", chip->lux_calib);
0676 }
0677
0678 static ssize_t apds990x_lux_calib_store(struct device *dev,
0679 struct device_attribute *attr,
0680 const char *buf, size_t len)
0681 {
0682 struct apds990x_chip *chip = dev_get_drvdata(dev);
0683 unsigned long value;
0684 int ret;
0685
0686 ret = kstrtoul(buf, 0, &value);
0687 if (ret)
0688 return ret;
0689
0690 chip->lux_calib = value;
0691
0692 return len;
0693 }
0694
0695 static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, apds990x_lux_calib_show,
0696 apds990x_lux_calib_store);
0697
0698 static ssize_t apds990x_rate_avail(struct device *dev,
0699 struct device_attribute *attr, char *buf)
0700 {
0701 int i;
0702 int pos = 0;
0703
0704 for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
0705 pos += sprintf(buf + pos, "%d ", arates_hz[i]);
0706 sprintf(buf + pos - 1, "\n");
0707 return pos;
0708 }
0709
0710 static ssize_t apds990x_rate_show(struct device *dev,
0711 struct device_attribute *attr, char *buf)
0712 {
0713 struct apds990x_chip *chip = dev_get_drvdata(dev);
0714
0715 return sprintf(buf, "%d\n", chip->arate);
0716 }
0717
0718 static int apds990x_set_arate(struct apds990x_chip *chip, int rate)
0719 {
0720 int i;
0721
0722 for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
0723 if (rate >= arates_hz[i])
0724 break;
0725
0726 if (i == ARRAY_SIZE(arates_hz))
0727 return -EINVAL;
0728
0729
0730 chip->lux_persistence = apersis[i];
0731 chip->arate = arates_hz[i];
0732
0733
0734 if (pm_runtime_suspended(&chip->client->dev))
0735 return 0;
0736
0737
0738 return apds990x_write_byte(chip, APDS990X_PERS,
0739 (chip->lux_persistence << APDS990X_APERS_SHIFT) |
0740 (chip->prox_persistence << APDS990X_PPERS_SHIFT));
0741 }
0742
0743 static ssize_t apds990x_rate_store(struct device *dev,
0744 struct device_attribute *attr,
0745 const char *buf, size_t len)
0746 {
0747 struct apds990x_chip *chip = dev_get_drvdata(dev);
0748 unsigned long value;
0749 int ret;
0750
0751 ret = kstrtoul(buf, 0, &value);
0752 if (ret)
0753 return ret;
0754
0755 mutex_lock(&chip->mutex);
0756 ret = apds990x_set_arate(chip, value);
0757 mutex_unlock(&chip->mutex);
0758
0759 if (ret < 0)
0760 return ret;
0761 return len;
0762 }
0763
0764 static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, apds990x_rate_avail, NULL);
0765
0766 static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, apds990x_rate_show,
0767 apds990x_rate_store);
0768
0769 static ssize_t apds990x_prox_show(struct device *dev,
0770 struct device_attribute *attr, char *buf)
0771 {
0772 ssize_t ret;
0773 struct apds990x_chip *chip = dev_get_drvdata(dev);
0774
0775 if (pm_runtime_suspended(dev) || !chip->prox_en)
0776 return -EIO;
0777
0778 mutex_lock(&chip->mutex);
0779 ret = sprintf(buf, "%d\n", chip->prox_data);
0780 mutex_unlock(&chip->mutex);
0781 return ret;
0782 }
0783
0784 static DEVICE_ATTR(prox0_raw, S_IRUGO, apds990x_prox_show, NULL);
0785
0786 static ssize_t apds990x_prox_range_show(struct device *dev,
0787 struct device_attribute *attr, char *buf)
0788 {
0789 return sprintf(buf, "%u\n", APDS_PROX_RANGE);
0790 }
0791
0792 static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, apds990x_prox_range_show, NULL);
0793
0794 static ssize_t apds990x_prox_enable_show(struct device *dev,
0795 struct device_attribute *attr, char *buf)
0796 {
0797 struct apds990x_chip *chip = dev_get_drvdata(dev);
0798
0799 return sprintf(buf, "%d\n", chip->prox_en);
0800 }
0801
0802 static ssize_t apds990x_prox_enable_store(struct device *dev,
0803 struct device_attribute *attr,
0804 const char *buf, size_t len)
0805 {
0806 struct apds990x_chip *chip = dev_get_drvdata(dev);
0807 unsigned long value;
0808 int ret;
0809
0810 ret = kstrtoul(buf, 0, &value);
0811 if (ret)
0812 return ret;
0813
0814 mutex_lock(&chip->mutex);
0815
0816 if (!chip->prox_en)
0817 chip->prox_data = 0;
0818
0819 if (value)
0820 chip->prox_en++;
0821 else if (chip->prox_en > 0)
0822 chip->prox_en--;
0823
0824 if (!pm_runtime_suspended(dev))
0825 apds990x_mode_on(chip);
0826 mutex_unlock(&chip->mutex);
0827 return len;
0828 }
0829
0830 static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, apds990x_prox_enable_show,
0831 apds990x_prox_enable_store);
0832
0833 static const char *reporting_modes[] = {"trigger", "periodic"};
0834
0835 static ssize_t apds990x_prox_reporting_mode_show(struct device *dev,
0836 struct device_attribute *attr, char *buf)
0837 {
0838 struct apds990x_chip *chip = dev_get_drvdata(dev);
0839
0840 return sprintf(buf, "%s\n",
0841 reporting_modes[!!chip->prox_continuous_mode]);
0842 }
0843
0844 static ssize_t apds990x_prox_reporting_mode_store(struct device *dev,
0845 struct device_attribute *attr,
0846 const char *buf, size_t len)
0847 {
0848 struct apds990x_chip *chip = dev_get_drvdata(dev);
0849 int ret;
0850
0851 ret = sysfs_match_string(reporting_modes, buf);
0852 if (ret < 0)
0853 return ret;
0854
0855 chip->prox_continuous_mode = ret;
0856 return len;
0857 }
0858
0859 static DEVICE_ATTR(prox0_reporting_mode, S_IRUGO | S_IWUSR,
0860 apds990x_prox_reporting_mode_show,
0861 apds990x_prox_reporting_mode_store);
0862
0863 static ssize_t apds990x_prox_reporting_avail_show(struct device *dev,
0864 struct device_attribute *attr, char *buf)
0865 {
0866 return sprintf(buf, "%s %s\n", reporting_modes[0], reporting_modes[1]);
0867 }
0868
0869 static DEVICE_ATTR(prox0_reporting_mode_avail, S_IRUGO | S_IWUSR,
0870 apds990x_prox_reporting_avail_show, NULL);
0871
0872
0873 static ssize_t apds990x_lux_thresh_above_show(struct device *dev,
0874 struct device_attribute *attr, char *buf)
0875 {
0876 struct apds990x_chip *chip = dev_get_drvdata(dev);
0877
0878 return sprintf(buf, "%d\n", chip->lux_thres_hi);
0879 }
0880
0881 static ssize_t apds990x_lux_thresh_below_show(struct device *dev,
0882 struct device_attribute *attr, char *buf)
0883 {
0884 struct apds990x_chip *chip = dev_get_drvdata(dev);
0885
0886 return sprintf(buf, "%d\n", chip->lux_thres_lo);
0887 }
0888
0889 static ssize_t apds990x_set_lux_thresh(struct apds990x_chip *chip, u32 *target,
0890 const char *buf)
0891 {
0892 unsigned long thresh;
0893 int ret;
0894
0895 ret = kstrtoul(buf, 0, &thresh);
0896 if (ret)
0897 return ret;
0898
0899 if (thresh > APDS_RANGE)
0900 return -EINVAL;
0901
0902 mutex_lock(&chip->mutex);
0903 *target = thresh;
0904
0905
0906
0907
0908 if (!chip->lux_wait_fresh_res)
0909 apds990x_refresh_athres(chip);
0910 mutex_unlock(&chip->mutex);
0911 return ret;
0912
0913 }
0914
0915 static ssize_t apds990x_lux_thresh_above_store(struct device *dev,
0916 struct device_attribute *attr,
0917 const char *buf, size_t len)
0918 {
0919 struct apds990x_chip *chip = dev_get_drvdata(dev);
0920 int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_hi, buf);
0921
0922 if (ret < 0)
0923 return ret;
0924 return len;
0925 }
0926
0927 static ssize_t apds990x_lux_thresh_below_store(struct device *dev,
0928 struct device_attribute *attr,
0929 const char *buf, size_t len)
0930 {
0931 struct apds990x_chip *chip = dev_get_drvdata(dev);
0932 int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_lo, buf);
0933
0934 if (ret < 0)
0935 return ret;
0936 return len;
0937 }
0938
0939 static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
0940 apds990x_lux_thresh_above_show,
0941 apds990x_lux_thresh_above_store);
0942
0943 static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
0944 apds990x_lux_thresh_below_show,
0945 apds990x_lux_thresh_below_store);
0946
0947 static ssize_t apds990x_prox_threshold_show(struct device *dev,
0948 struct device_attribute *attr, char *buf)
0949 {
0950 struct apds990x_chip *chip = dev_get_drvdata(dev);
0951
0952 return sprintf(buf, "%d\n", chip->prox_thres);
0953 }
0954
0955 static ssize_t apds990x_prox_threshold_store(struct device *dev,
0956 struct device_attribute *attr,
0957 const char *buf, size_t len)
0958 {
0959 struct apds990x_chip *chip = dev_get_drvdata(dev);
0960 unsigned long value;
0961 int ret;
0962
0963 ret = kstrtoul(buf, 0, &value);
0964 if (ret)
0965 return ret;
0966
0967 if ((value > APDS_RANGE) || (value == 0) ||
0968 (value < APDS_PROX_HYSTERESIS))
0969 return -EINVAL;
0970
0971 mutex_lock(&chip->mutex);
0972 chip->prox_thres = value;
0973
0974 apds990x_force_p_refresh(chip);
0975 mutex_unlock(&chip->mutex);
0976 return len;
0977 }
0978
0979 static DEVICE_ATTR(prox0_thresh_above_value, S_IRUGO | S_IWUSR,
0980 apds990x_prox_threshold_show,
0981 apds990x_prox_threshold_store);
0982
0983 static ssize_t apds990x_power_state_show(struct device *dev,
0984 struct device_attribute *attr, char *buf)
0985 {
0986 return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
0987 return 0;
0988 }
0989
0990 static ssize_t apds990x_power_state_store(struct device *dev,
0991 struct device_attribute *attr,
0992 const char *buf, size_t len)
0993 {
0994 struct apds990x_chip *chip = dev_get_drvdata(dev);
0995 unsigned long value;
0996 int ret;
0997
0998 ret = kstrtoul(buf, 0, &value);
0999 if (ret)
1000 return ret;
1001
1002 if (value) {
1003 pm_runtime_get_sync(dev);
1004 mutex_lock(&chip->mutex);
1005 chip->lux_wait_fresh_res = true;
1006 apds990x_force_a_refresh(chip);
1007 apds990x_force_p_refresh(chip);
1008 mutex_unlock(&chip->mutex);
1009 } else {
1010 if (!pm_runtime_suspended(dev))
1011 pm_runtime_put(dev);
1012 }
1013 return len;
1014 }
1015
1016 static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
1017 apds990x_power_state_show,
1018 apds990x_power_state_store);
1019
1020 static ssize_t apds990x_chip_id_show(struct device *dev,
1021 struct device_attribute *attr, char *buf)
1022 {
1023 struct apds990x_chip *chip = dev_get_drvdata(dev);
1024
1025 return sprintf(buf, "%s %d\n", chip->chipname, chip->revision);
1026 }
1027
1028 static DEVICE_ATTR(chip_id, S_IRUGO, apds990x_chip_id_show, NULL);
1029
1030 static struct attribute *sysfs_attrs_ctrl[] = {
1031 &dev_attr_lux0_calibscale.attr,
1032 &dev_attr_lux0_calibscale_default.attr,
1033 &dev_attr_lux0_input.attr,
1034 &dev_attr_lux0_sensor_range.attr,
1035 &dev_attr_lux0_rate.attr,
1036 &dev_attr_lux0_rate_avail.attr,
1037 &dev_attr_lux0_thresh_above_value.attr,
1038 &dev_attr_lux0_thresh_below_value.attr,
1039 &dev_attr_prox0_raw_en.attr,
1040 &dev_attr_prox0_raw.attr,
1041 &dev_attr_prox0_sensor_range.attr,
1042 &dev_attr_prox0_thresh_above_value.attr,
1043 &dev_attr_prox0_reporting_mode.attr,
1044 &dev_attr_prox0_reporting_mode_avail.attr,
1045 &dev_attr_chip_id.attr,
1046 &dev_attr_power_state.attr,
1047 NULL
1048 };
1049
1050 static const struct attribute_group apds990x_attribute_group[] = {
1051 {.attrs = sysfs_attrs_ctrl },
1052 };
1053
1054 static int apds990x_probe(struct i2c_client *client,
1055 const struct i2c_device_id *id)
1056 {
1057 struct apds990x_chip *chip;
1058 int err;
1059
1060 chip = kzalloc(sizeof *chip, GFP_KERNEL);
1061 if (!chip)
1062 return -ENOMEM;
1063
1064 i2c_set_clientdata(client, chip);
1065 chip->client = client;
1066
1067 init_waitqueue_head(&chip->wait);
1068 mutex_init(&chip->mutex);
1069 chip->pdata = client->dev.platform_data;
1070
1071 if (chip->pdata == NULL) {
1072 dev_err(&client->dev, "platform data is mandatory\n");
1073 err = -EINVAL;
1074 goto fail1;
1075 }
1076
1077 if (chip->pdata->cf.ga == 0) {
1078
1079 chip->cf.ga = 1966;
1080 chip->cf.cf1 = 4096;
1081 chip->cf.irf1 = 9134;
1082 chip->cf.cf2 = 2867;
1083 chip->cf.irf2 = 5816;
1084 chip->cf.df = 52;
1085 } else {
1086 chip->cf = chip->pdata->cf;
1087 }
1088
1089
1090 chip->rcf.afactor =
1091 (chip->cf.irf1 - chip->cf.irf2) * APDS_PARAM_SCALE /
1092 (chip->cf.cf1 - chip->cf.cf2);
1093 chip->rcf.cf1 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
1094 chip->cf.cf1;
1095 chip->rcf.irf1 = chip->cf.irf1 * APDS_PARAM_SCALE /
1096 chip->cf.cf1;
1097 chip->rcf.cf2 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
1098 chip->cf.cf2;
1099 chip->rcf.irf2 = chip->cf.irf2 * APDS_PARAM_SCALE /
1100 chip->cf.cf2;
1101
1102
1103 chip->lux_thres_hi = APDS_LUX_DEF_THRES_HI;
1104 chip->lux_thres_lo = APDS_LUX_DEF_THRES_LO;
1105 chip->lux_calib = APDS_LUX_NEUTRAL_CALIB_VALUE;
1106
1107 chip->prox_thres = APDS_PROX_DEF_THRES;
1108 chip->pdrive = chip->pdata->pdrive;
1109 chip->pdiode = APDS_PDIODE_IR;
1110 chip->pgain = APDS_PGAIN_1X;
1111 chip->prox_calib = APDS_PROX_NEUTRAL_CALIB_VALUE;
1112 chip->prox_persistence = APDS_DEFAULT_PROX_PERS;
1113 chip->prox_continuous_mode = false;
1114
1115 chip->regs[0].supply = reg_vcc;
1116 chip->regs[1].supply = reg_vled;
1117
1118 err = regulator_bulk_get(&client->dev,
1119 ARRAY_SIZE(chip->regs), chip->regs);
1120 if (err < 0) {
1121 dev_err(&client->dev, "Cannot get regulators\n");
1122 goto fail1;
1123 }
1124
1125 err = regulator_bulk_enable(ARRAY_SIZE(chip->regs), chip->regs);
1126 if (err < 0) {
1127 dev_err(&client->dev, "Cannot enable regulators\n");
1128 goto fail2;
1129 }
1130
1131 usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
1132
1133 err = apds990x_detect(chip);
1134 if (err < 0) {
1135 dev_err(&client->dev, "APDS990X not found\n");
1136 goto fail3;
1137 }
1138
1139 pm_runtime_set_active(&client->dev);
1140
1141 apds990x_configure(chip);
1142 apds990x_set_arate(chip, APDS_LUX_DEFAULT_RATE);
1143 apds990x_mode_on(chip);
1144
1145 pm_runtime_enable(&client->dev);
1146
1147 if (chip->pdata->setup_resources) {
1148 err = chip->pdata->setup_resources();
1149 if (err) {
1150 err = -EINVAL;
1151 goto fail3;
1152 }
1153 }
1154
1155 err = sysfs_create_group(&chip->client->dev.kobj,
1156 apds990x_attribute_group);
1157 if (err < 0) {
1158 dev_err(&chip->client->dev, "Sysfs registration failed\n");
1159 goto fail4;
1160 }
1161
1162 err = request_threaded_irq(client->irq, NULL,
1163 apds990x_irq,
1164 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW |
1165 IRQF_ONESHOT,
1166 "apds990x", chip);
1167 if (err) {
1168 dev_err(&client->dev, "could not get IRQ %d\n",
1169 client->irq);
1170 goto fail5;
1171 }
1172 return err;
1173 fail5:
1174 sysfs_remove_group(&chip->client->dev.kobj,
1175 &apds990x_attribute_group[0]);
1176 fail4:
1177 if (chip->pdata && chip->pdata->release_resources)
1178 chip->pdata->release_resources();
1179 fail3:
1180 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1181 fail2:
1182 regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
1183 fail1:
1184 kfree(chip);
1185 return err;
1186 }
1187
1188 static int apds990x_remove(struct i2c_client *client)
1189 {
1190 struct apds990x_chip *chip = i2c_get_clientdata(client);
1191
1192 free_irq(client->irq, chip);
1193 sysfs_remove_group(&chip->client->dev.kobj,
1194 apds990x_attribute_group);
1195
1196 if (chip->pdata && chip->pdata->release_resources)
1197 chip->pdata->release_resources();
1198
1199 if (!pm_runtime_suspended(&client->dev))
1200 apds990x_chip_off(chip);
1201
1202 pm_runtime_disable(&client->dev);
1203 pm_runtime_set_suspended(&client->dev);
1204
1205 regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
1206
1207 kfree(chip);
1208 return 0;
1209 }
1210
1211 #ifdef CONFIG_PM_SLEEP
1212 static int apds990x_suspend(struct device *dev)
1213 {
1214 struct i2c_client *client = to_i2c_client(dev);
1215 struct apds990x_chip *chip = i2c_get_clientdata(client);
1216
1217 apds990x_chip_off(chip);
1218 return 0;
1219 }
1220
1221 static int apds990x_resume(struct device *dev)
1222 {
1223 struct i2c_client *client = to_i2c_client(dev);
1224 struct apds990x_chip *chip = i2c_get_clientdata(client);
1225
1226
1227
1228
1229
1230 apds990x_chip_on(chip);
1231
1232 return 0;
1233 }
1234 #endif
1235
1236 #ifdef CONFIG_PM
1237 static int apds990x_runtime_suspend(struct device *dev)
1238 {
1239 struct i2c_client *client = to_i2c_client(dev);
1240 struct apds990x_chip *chip = i2c_get_clientdata(client);
1241
1242 apds990x_chip_off(chip);
1243 return 0;
1244 }
1245
1246 static int apds990x_runtime_resume(struct device *dev)
1247 {
1248 struct i2c_client *client = to_i2c_client(dev);
1249 struct apds990x_chip *chip = i2c_get_clientdata(client);
1250
1251 apds990x_chip_on(chip);
1252 return 0;
1253 }
1254
1255 #endif
1256
1257 static const struct i2c_device_id apds990x_id[] = {
1258 {"apds990x", 0 },
1259 {}
1260 };
1261
1262 MODULE_DEVICE_TABLE(i2c, apds990x_id);
1263
1264 static const struct dev_pm_ops apds990x_pm_ops = {
1265 SET_SYSTEM_SLEEP_PM_OPS(apds990x_suspend, apds990x_resume)
1266 SET_RUNTIME_PM_OPS(apds990x_runtime_suspend,
1267 apds990x_runtime_resume,
1268 NULL)
1269 };
1270
1271 static struct i2c_driver apds990x_driver = {
1272 .driver = {
1273 .name = "apds990x",
1274 .pm = &apds990x_pm_ops,
1275 },
1276 .probe = apds990x_probe,
1277 .remove = apds990x_remove,
1278 .id_table = apds990x_id,
1279 };
1280
1281 module_i2c_driver(apds990x_driver);
1282
1283 MODULE_DESCRIPTION("APDS990X combined ALS and proximity sensor");
1284 MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1285 MODULE_LICENSE("GPL v2");