blob: 200311fea369e6a952f38b8c205a0fb5c0c01f00 [file] [log] [blame]
Samu Onkalo92b1f842010-10-26 14:22:38 -07001/*
2 * This file is part of the APDS990x sensor driver.
3 * Chip is combined proximity and ambient light sensor.
4 *
5 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 *
7 * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25#include <linux/kernel.h>
26#include <linux/module.h>
27#include <linux/i2c.h>
28#include <linux/interrupt.h>
29#include <linux/mutex.h>
30#include <linux/regulator/consumer.h>
31#include <linux/pm_runtime.h>
32#include <linux/delay.h>
33#include <linux/wait.h>
34#include <linux/slab.h>
35#include <linux/i2c/apds990x.h>
36
37/* Register map */
38#define APDS990X_ENABLE 0x00 /* Enable of states and interrupts */
39#define APDS990X_ATIME 0x01 /* ALS ADC time */
40#define APDS990X_PTIME 0x02 /* Proximity ADC time */
41#define APDS990X_WTIME 0x03 /* Wait time */
42#define APDS990X_AILTL 0x04 /* ALS interrupt low threshold low byte */
43#define APDS990X_AILTH 0x05 /* ALS interrupt low threshold hi byte */
44#define APDS990X_AIHTL 0x06 /* ALS interrupt hi threshold low byte */
45#define APDS990X_AIHTH 0x07 /* ALS interrupt hi threshold hi byte */
46#define APDS990X_PILTL 0x08 /* Proximity interrupt low threshold low byte */
47#define APDS990X_PILTH 0x09 /* Proximity interrupt low threshold hi byte */
48#define APDS990X_PIHTL 0x0a /* Proximity interrupt hi threshold low byte */
49#define APDS990X_PIHTH 0x0b /* Proximity interrupt hi threshold hi byte */
50#define APDS990X_PERS 0x0c /* Interrupt persistence filters */
51#define APDS990X_CONFIG 0x0d /* Configuration */
52#define APDS990X_PPCOUNT 0x0e /* Proximity pulse count */
53#define APDS990X_CONTROL 0x0f /* Gain control register */
54#define APDS990X_REV 0x11 /* Revision Number */
55#define APDS990X_ID 0x12 /* Device ID */
56#define APDS990X_STATUS 0x13 /* Device status */
57#define APDS990X_CDATAL 0x14 /* Clear ADC low data register */
58#define APDS990X_CDATAH 0x15 /* Clear ADC high data register */
59#define APDS990X_IRDATAL 0x16 /* IR ADC low data register */
60#define APDS990X_IRDATAH 0x17 /* IR ADC high data register */
61#define APDS990X_PDATAL 0x18 /* Proximity ADC low data register */
62#define APDS990X_PDATAH 0x19 /* Proximity ADC high data register */
63
64/* Control */
65#define APDS990X_MAX_AGAIN 3
66
67/* Enable register */
68#define APDS990X_EN_PIEN (0x1 << 5)
69#define APDS990X_EN_AIEN (0x1 << 4)
70#define APDS990X_EN_WEN (0x1 << 3)
71#define APDS990X_EN_PEN (0x1 << 2)
72#define APDS990X_EN_AEN (0x1 << 1)
73#define APDS990X_EN_PON (0x1 << 0)
74#define APDS990X_EN_DISABLE_ALL 0
75
76/* Status register */
77#define APDS990X_ST_PINT (0x1 << 5)
78#define APDS990X_ST_AINT (0x1 << 4)
79
80/* I2C access types */
81#define APDS990x_CMD_TYPE_MASK (0x03 << 5)
82#define APDS990x_CMD_TYPE_RB (0x00 << 5) /* Repeated byte */
83#define APDS990x_CMD_TYPE_INC (0x01 << 5) /* Auto increment */
84#define APDS990x_CMD_TYPE_SPE (0x03 << 5) /* Special function */
85
86#define APDS990x_ADDR_SHIFT 0
87#define APDS990x_CMD 0x80
88
89/* Interrupt ack commands */
90#define APDS990X_INT_ACK_ALS 0x6
91#define APDS990X_INT_ACK_PS 0x5
92#define APDS990X_INT_ACK_BOTH 0x7
93
94/* ptime */
95#define APDS990X_PTIME_DEFAULT 0xff /* Recommended conversion time 2.7ms*/
96
97/* wtime */
98#define APDS990X_WTIME_DEFAULT 0xee /* ~50ms wait time */
99
100#define APDS990X_TIME_TO_ADC 1024 /* One timetick as ADC count value */
101
102/* Persistence */
103#define APDS990X_APERS_SHIFT 0
104#define APDS990X_PPERS_SHIFT 4
105
106/* Supported ID:s */
107#define APDS990X_ID_0 0x0
108#define APDS990X_ID_4 0x4
109#define APDS990X_ID_29 0x29
110
111/* pgain and pdiode settings */
112#define APDS_PGAIN_1X 0x0
113#define APDS_PDIODE_IR 0x2
114
115#define APDS990X_LUX_OUTPUT_SCALE 10
116
117/* Reverse chip factors for threshold calculation */
118struct reverse_factors {
119 u32 afactor;
120 int cf1;
121 int irf1;
122 int cf2;
123 int irf2;
124};
125
126struct apds990x_chip {
127 struct apds990x_platform_data *pdata;
128 struct i2c_client *client;
129 struct mutex mutex; /* avoid parallel access */
130 struct regulator_bulk_data regs[2];
131 wait_queue_head_t wait;
132
133 int prox_en;
134 bool prox_continuous_mode;
135 bool lux_wait_fresh_res;
136
137 /* Chip parameters */
138 struct apds990x_chip_factors cf;
139 struct reverse_factors rcf;
140 u16 atime; /* als integration time */
141 u16 arate; /* als reporting rate */
142 u16 a_max_result; /* Max possible ADC value with current atime */
143 u8 again_meas; /* Gain used in last measurement */
144 u8 again_next; /* Next calculated gain */
145 u8 pgain;
146 u8 pdiode;
147 u8 pdrive;
148 u8 lux_persistence;
149 u8 prox_persistence;
150
151 u32 lux_raw;
152 u32 lux;
153 u16 lux_clear;
154 u16 lux_ir;
155 u16 lux_calib;
156 u32 lux_thres_hi;
157 u32 lux_thres_lo;
158
159 u32 prox_thres;
160 u16 prox_data;
161 u16 prox_calib;
162
163 char chipname[10];
164 u8 revision;
165};
166
167#define APDS_CALIB_SCALER 8192
168#define APDS_LUX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
169#define APDS_PROX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
170
171#define APDS_PROX_DEF_THRES 600
172#define APDS_PROX_HYSTERESIS 50
173#define APDS_LUX_DEF_THRES_HI 101
174#define APDS_LUX_DEF_THRES_LO 100
175#define APDS_DEFAULT_PROX_PERS 1
176
177#define APDS_TIMEOUT 2000
178#define APDS_STARTUP_DELAY 25000 /* us */
179#define APDS_RANGE 65535
180#define APDS_PROX_RANGE 1023
181#define APDS_LUX_GAIN_LO_LIMIT 100
182#define APDS_LUX_GAIN_LO_LIMIT_STRICT 25
183
184#define TIMESTEP 87 /* 2.7ms is about 87 / 32 */
185#define TIME_STEP_SCALER 32
186
187#define APDS_LUX_AVERAGING_TIME 50 /* tolerates 50/60Hz ripple */
188#define APDS_LUX_DEFAULT_RATE 200
189
190static const u8 again[] = {1, 8, 16, 120}; /* ALS gain steps */
191static const u8 ir_currents[] = {100, 50, 25, 12}; /* IRled currents in mA */
192
193/* Following two tables must match i.e 10Hz rate means 1 as persistence value */
194static const u16 arates_hz[] = {10, 5, 2, 1};
195static const u8 apersis[] = {1, 2, 4, 5};
196
197/* Regulators */
198static const char reg_vcc[] = "Vdd";
199static const char reg_vled[] = "Vled";
200
201static int apds990x_read_byte(struct apds990x_chip *chip, u8 reg, u8 *data)
202{
203 struct i2c_client *client = chip->client;
204 s32 ret;
205
206 reg &= ~APDS990x_CMD_TYPE_MASK;
207 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
208
209 ret = i2c_smbus_read_byte_data(client, reg);
210 *data = ret;
211 return (int)ret;
212}
213
214static int apds990x_read_word(struct apds990x_chip *chip, u8 reg, u16 *data)
215{
216 struct i2c_client *client = chip->client;
217 s32 ret;
218
219 reg &= ~APDS990x_CMD_TYPE_MASK;
220 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
221
222 ret = i2c_smbus_read_word_data(client, reg);
223 *data = ret;
224 return (int)ret;
225}
226
227static int apds990x_write_byte(struct apds990x_chip *chip, u8 reg, u8 data)
228{
229 struct i2c_client *client = chip->client;
230 s32 ret;
231
232 reg &= ~APDS990x_CMD_TYPE_MASK;
233 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
234
235 ret = i2c_smbus_write_byte_data(client, reg, data);
236 return (int)ret;
237}
238
239static int apds990x_write_word(struct apds990x_chip *chip, u8 reg, u16 data)
240{
241 struct i2c_client *client = chip->client;
242 s32 ret;
243
244 reg &= ~APDS990x_CMD_TYPE_MASK;
245 reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
246
247 ret = i2c_smbus_write_word_data(client, reg, data);
248 return (int)ret;
249}
250
251static int apds990x_mode_on(struct apds990x_chip *chip)
252{
253 /* ALS is mandatory, proximity optional */
254 u8 reg = APDS990X_EN_AIEN | APDS990X_EN_PON | APDS990X_EN_AEN |
255 APDS990X_EN_WEN;
256
257 if (chip->prox_en)
258 reg |= APDS990X_EN_PIEN | APDS990X_EN_PEN;
259
260 return apds990x_write_byte(chip, APDS990X_ENABLE, reg);
261}
262
263static u16 apds990x_lux_to_threshold(struct apds990x_chip *chip, u32 lux)
264{
265 u32 thres;
266 u32 cpl;
267 u32 ir;
268
269 if (lux == 0)
270 return 0;
271 else if (lux == APDS_RANGE)
272 return APDS_RANGE;
273
274 /*
275 * Reported LUX value is a combination of the IR and CLEAR channel
276 * values. However, interrupt threshold is only for clear channel.
277 * This function approximates needed HW threshold value for a given
278 * LUX value in the current lightning type.
279 * IR level compared to visible light varies heavily depending on the
280 * source of the light
281 *
282 * Calculate threshold value for the next measurement period.
283 * Math: threshold = lux * cpl where
284 * cpl = atime * again / (glass_attenuation * device_factor)
285 * (count-per-lux)
286 *
287 * First remove calibration. Division by four is to avoid overflow
288 */
289 lux = lux * (APDS_CALIB_SCALER / 4) / (chip->lux_calib / 4);
290
291 /* Multiplication by 64 is to increase accuracy */
292 cpl = ((u32)chip->atime * (u32)again[chip->again_next] *
293 APDS_PARAM_SCALE * 64) / (chip->cf.ga * chip->cf.df);
294
295 thres = lux * cpl / 64;
296 /*
297 * Convert IR light from the latest result to match with
298 * new gain step. This helps to adapt with the current
299 * source of light.
300 */
301 ir = (u32)chip->lux_ir * (u32)again[chip->again_next] /
302 (u32)again[chip->again_meas];
303
304 /*
305 * Compensate count with IR light impact
306 * IAC1 > IAC2 (see apds990x_get_lux for formulas)
307 */
308 if (chip->lux_clear * APDS_PARAM_SCALE >=
309 chip->rcf.afactor * chip->lux_ir)
310 thres = (chip->rcf.cf1 * thres + chip->rcf.irf1 * ir) /
311 APDS_PARAM_SCALE;
312 else
313 thres = (chip->rcf.cf2 * thres + chip->rcf.irf2 * ir) /
314 APDS_PARAM_SCALE;
315
316 if (thres >= chip->a_max_result)
317 thres = chip->a_max_result - 1;
318 return thres;
319}
320
321static inline int apds990x_set_atime(struct apds990x_chip *chip, u32 time_ms)
322{
323 u8 reg_value;
324
325 chip->atime = time_ms;
326 /* Formula is specified in the data sheet */
327 reg_value = 256 - ((time_ms * TIME_STEP_SCALER) / TIMESTEP);
328 /* Calculate max ADC value for given integration time */
329 chip->a_max_result = (u16)(256 - reg_value) * APDS990X_TIME_TO_ADC;
330 return apds990x_write_byte(chip, APDS990X_ATIME, reg_value);
331}
332
333/* Called always with mutex locked */
334static int apds990x_refresh_pthres(struct apds990x_chip *chip, int data)
335{
336 int ret, lo, hi;
337
338 /* If the chip is not in use, don't try to access it */
339 if (pm_runtime_suspended(&chip->client->dev))
340 return 0;
341
342 if (data < chip->prox_thres) {
343 lo = 0;
344 hi = chip->prox_thres;
345 } else {
346 lo = chip->prox_thres - APDS_PROX_HYSTERESIS;
347 if (chip->prox_continuous_mode)
348 hi = chip->prox_thres;
349 else
350 hi = APDS_RANGE;
351 }
352
353 ret = apds990x_write_word(chip, APDS990X_PILTL, lo);
354 ret |= apds990x_write_word(chip, APDS990X_PIHTL, hi);
355 return ret;
356}
357
358/* Called always with mutex locked */
359static int apds990x_refresh_athres(struct apds990x_chip *chip)
360{
361 int ret;
362 /* If the chip is not in use, don't try to access it */
363 if (pm_runtime_suspended(&chip->client->dev))
364 return 0;
365
366 ret = apds990x_write_word(chip, APDS990X_AILTL,
367 apds990x_lux_to_threshold(chip, chip->lux_thres_lo));
368 ret |= apds990x_write_word(chip, APDS990X_AIHTL,
369 apds990x_lux_to_threshold(chip, chip->lux_thres_hi));
370
371 return ret;
372}
373
374/* Called always with mutex locked */
375static void apds990x_force_a_refresh(struct apds990x_chip *chip)
376{
377 /* This will force ALS interrupt after the next measurement. */
378 apds990x_write_word(chip, APDS990X_AILTL, APDS_LUX_DEF_THRES_LO);
379 apds990x_write_word(chip, APDS990X_AIHTL, APDS_LUX_DEF_THRES_HI);
380}
381
382/* Called always with mutex locked */
383static void apds990x_force_p_refresh(struct apds990x_chip *chip)
384{
385 /* This will force proximity interrupt after the next measurement. */
386 apds990x_write_word(chip, APDS990X_PILTL, APDS_PROX_DEF_THRES - 1);
387 apds990x_write_word(chip, APDS990X_PIHTL, APDS_PROX_DEF_THRES);
388}
389
390/* Called always with mutex locked */
391static int apds990x_calc_again(struct apds990x_chip *chip)
392{
393 int curr_again = chip->again_meas;
394 int next_again = chip->again_meas;
395 int ret = 0;
396
397 /* Calculate suitable als gain */
398 if (chip->lux_clear == chip->a_max_result)
399 next_again -= 2; /* ALS saturated. Decrease gain by 2 steps */
400 else if (chip->lux_clear > chip->a_max_result / 2)
401 next_again--;
402 else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
403 next_again += 2; /* Too dark. Increase gain by 2 steps */
404 else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT)
405 next_again++;
406
407 /* Limit gain to available range */
408 if (next_again < 0)
409 next_again = 0;
410 else if (next_again > APDS990X_MAX_AGAIN)
411 next_again = APDS990X_MAX_AGAIN;
412
413 /* Let's check can we trust the measured result */
414 if (chip->lux_clear == chip->a_max_result)
415 /* Result can be totally garbage due to saturation */
416 ret = -ERANGE;
417 else if (next_again != curr_again &&
418 chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
419 /*
420 * Gain is changed and measurement result is very small.
421 * Result can be totally garbage due to underflow
422 */
423 ret = -ERANGE;
424
425 chip->again_next = next_again;
426 apds990x_write_byte(chip, APDS990X_CONTROL,
427 (chip->pdrive << 6) |
428 (chip->pdiode << 4) |
429 (chip->pgain << 2) |
430 (chip->again_next << 0));
431
432 /*
433 * Error means bad result -> re-measurement is needed. The forced
434 * refresh uses fastest possible persistence setting to get result
435 * as soon as possible.
436 */
437 if (ret < 0)
438 apds990x_force_a_refresh(chip);
439 else
440 apds990x_refresh_athres(chip);
441
442 return ret;
443}
444
445/* Called always with mutex locked */
446static int apds990x_get_lux(struct apds990x_chip *chip, int clear, int ir)
447{
448 int iac, iac1, iac2; /* IR adjusted counts */
449 u32 lpc; /* Lux per count */
450
451 /* Formulas:
452 * iac1 = CF1 * CLEAR_CH - IRF1 * IR_CH
453 * iac2 = CF2 * CLEAR_CH - IRF2 * IR_CH
454 */
455 iac1 = (chip->cf.cf1 * clear - chip->cf.irf1 * ir) / APDS_PARAM_SCALE;
456 iac2 = (chip->cf.cf2 * clear - chip->cf.irf2 * ir) / APDS_PARAM_SCALE;
457
458 iac = max(iac1, iac2);
459 iac = max(iac, 0);
460
461 lpc = APDS990X_LUX_OUTPUT_SCALE * (chip->cf.df * chip->cf.ga) /
462 (u32)(again[chip->again_meas] * (u32)chip->atime);
463
464 return (iac * lpc) / APDS_PARAM_SCALE;
465}
466
467static int apds990x_ack_int(struct apds990x_chip *chip, u8 mode)
468{
469 struct i2c_client *client = chip->client;
470 s32 ret;
471 u8 reg = APDS990x_CMD | APDS990x_CMD_TYPE_SPE;
472
473 switch (mode & (APDS990X_ST_AINT | APDS990X_ST_PINT)) {
474 case APDS990X_ST_AINT:
475 reg |= APDS990X_INT_ACK_ALS;
476 break;
477 case APDS990X_ST_PINT:
478 reg |= APDS990X_INT_ACK_PS;
479 break;
480 default:
481 reg |= APDS990X_INT_ACK_BOTH;
482 break;
483 }
484
485 ret = i2c_smbus_read_byte_data(client, reg);
486 return (int)ret;
487}
488
489static irqreturn_t apds990x_irq(int irq, void *data)
490{
491 struct apds990x_chip *chip = data;
492 u8 status;
493
494 apds990x_read_byte(chip, APDS990X_STATUS, &status);
495 apds990x_ack_int(chip, status);
496
497 mutex_lock(&chip->mutex);
498 if (!pm_runtime_suspended(&chip->client->dev)) {
499 if (status & APDS990X_ST_AINT) {
500 apds990x_read_word(chip, APDS990X_CDATAL,
501 &chip->lux_clear);
502 apds990x_read_word(chip, APDS990X_IRDATAL,
503 &chip->lux_ir);
504 /* Store used gain for calculations */
505 chip->again_meas = chip->again_next;
506
507 chip->lux_raw = apds990x_get_lux(chip,
508 chip->lux_clear,
509 chip->lux_ir);
510
511 if (apds990x_calc_again(chip) == 0) {
512 /* Result is valid */
513 chip->lux = chip->lux_raw;
514 chip->lux_wait_fresh_res = false;
515 wake_up(&chip->wait);
516 sysfs_notify(&chip->client->dev.kobj,
517 NULL, "lux0_input");
518 }
519 }
520
521 if ((status & APDS990X_ST_PINT) && chip->prox_en) {
522 u16 clr_ch;
523
524 apds990x_read_word(chip, APDS990X_CDATAL, &clr_ch);
525 /*
526 * If ALS channel is saturated at min gain,
527 * proximity gives false posivite values.
528 * Just ignore them.
529 */
530 if (chip->again_meas == 0 &&
531 clr_ch == chip->a_max_result)
532 chip->prox_data = 0;
533 else
534 apds990x_read_word(chip,
535 APDS990X_PDATAL,
536 &chip->prox_data);
537
538 apds990x_refresh_pthres(chip, chip->prox_data);
539 if (chip->prox_data < chip->prox_thres)
540 chip->prox_data = 0;
541 else if (!chip->prox_continuous_mode)
542 chip->prox_data = APDS_PROX_RANGE;
543 sysfs_notify(&chip->client->dev.kobj,
544 NULL, "prox0_raw");
545 }
546 }
547 mutex_unlock(&chip->mutex);
548 return IRQ_HANDLED;
549}
550
551static int apds990x_configure(struct apds990x_chip *chip)
552{
553 /* It is recommended to use disabled mode during these operations */
554 apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
555
556 /* conversion and wait times for different state machince states */
557 apds990x_write_byte(chip, APDS990X_PTIME, APDS990X_PTIME_DEFAULT);
558 apds990x_write_byte(chip, APDS990X_WTIME, APDS990X_WTIME_DEFAULT);
559 apds990x_set_atime(chip, APDS_LUX_AVERAGING_TIME);
560
561 apds990x_write_byte(chip, APDS990X_CONFIG, 0);
562
563 /* Persistence levels */
564 apds990x_write_byte(chip, APDS990X_PERS,
565 (chip->lux_persistence << APDS990X_APERS_SHIFT) |
566 (chip->prox_persistence << APDS990X_PPERS_SHIFT));
567
568 apds990x_write_byte(chip, APDS990X_PPCOUNT, chip->pdata->ppcount);
569
570 /* Start with relatively small gain */
571 chip->again_meas = 1;
572 chip->again_next = 1;
573 apds990x_write_byte(chip, APDS990X_CONTROL,
574 (chip->pdrive << 6) |
575 (chip->pdiode << 4) |
576 (chip->pgain << 2) |
577 (chip->again_next << 0));
578 return 0;
579}
580
581static int apds990x_detect(struct apds990x_chip *chip)
582{
583 struct i2c_client *client = chip->client;
584 int ret;
585 u8 id;
586
587 ret = apds990x_read_byte(chip, APDS990X_ID, &id);
588 if (ret < 0) {
589 dev_err(&client->dev, "ID read failed\n");
590 return ret;
591 }
592
593 ret = apds990x_read_byte(chip, APDS990X_REV, &chip->revision);
594 if (ret < 0) {
595 dev_err(&client->dev, "REV read failed\n");
596 return ret;
597 }
598
599 switch (id) {
600 case APDS990X_ID_0:
601 case APDS990X_ID_4:
602 case APDS990X_ID_29:
603 snprintf(chip->chipname, sizeof(chip->chipname), "APDS-990x");
604 break;
605 default:
606 ret = -ENODEV;
607 break;
608 }
609 return ret;
610}
611
612static int apds990x_chip_on(struct apds990x_chip *chip)
613{
614 int err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
615 chip->regs);
616 if (err < 0)
617 return err;
618
619 usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
620
621 /* Refresh all configs in case of regulators were off */
622 chip->prox_data = 0;
623 apds990x_configure(chip);
624 apds990x_mode_on(chip);
625 return 0;
626}
627
628static int apds990x_chip_off(struct apds990x_chip *chip)
629{
630 apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
631 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
632 return 0;
633}
634
635static ssize_t apds990x_lux_show(struct device *dev,
636 struct device_attribute *attr, char *buf)
637{
638 struct apds990x_chip *chip = dev_get_drvdata(dev);
639 ssize_t ret;
640 u32 result;
641 long timeout;
642
643 if (pm_runtime_suspended(dev))
644 return -EIO;
645
646 timeout = wait_event_interruptible_timeout(chip->wait,
647 !chip->lux_wait_fresh_res,
648 msecs_to_jiffies(APDS_TIMEOUT));
649 if (!timeout)
650 return -EIO;
651
652 mutex_lock(&chip->mutex);
653 result = (chip->lux * chip->lux_calib) / APDS_CALIB_SCALER;
654 if (result > (APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE))
655 result = APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE;
656
657 ret = sprintf(buf, "%d.%d\n",
658 result / APDS990X_LUX_OUTPUT_SCALE,
659 result % APDS990X_LUX_OUTPUT_SCALE);
660 mutex_unlock(&chip->mutex);
661 return ret;
662}
663
664static DEVICE_ATTR(lux0_input, S_IRUGO, apds990x_lux_show, NULL);
665
666static ssize_t apds990x_lux_range_show(struct device *dev,
667 struct device_attribute *attr, char *buf)
668{
669 return sprintf(buf, "%u\n", APDS_RANGE);
670}
671
672static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, apds990x_lux_range_show, NULL);
673
674static ssize_t apds990x_lux_calib_format_show(struct device *dev,
675 struct device_attribute *attr, char *buf)
676{
677 return sprintf(buf, "%u\n", APDS_CALIB_SCALER);
678}
679
680static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
681 apds990x_lux_calib_format_show, NULL);
682
683static ssize_t apds990x_lux_calib_show(struct device *dev,
684 struct device_attribute *attr, char *buf)
685{
686 struct apds990x_chip *chip = dev_get_drvdata(dev);
687
688 return sprintf(buf, "%u\n", chip->lux_calib);
689}
690
691static ssize_t apds990x_lux_calib_store(struct device *dev,
692 struct device_attribute *attr,
693 const char *buf, size_t len)
694{
695 struct apds990x_chip *chip = dev_get_drvdata(dev);
696 unsigned long value;
697
698 if (strict_strtoul(buf, 0, &value))
699 return -EINVAL;
700
701 if (chip->lux_calib > APDS_RANGE)
702 return -EINVAL;
703
704 chip->lux_calib = value;
705
706 return len;
707}
708
709static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, apds990x_lux_calib_show,
710 apds990x_lux_calib_store);
711
712static ssize_t apds990x_rate_avail(struct device *dev,
713 struct device_attribute *attr, char *buf)
714{
715 int i;
716 int pos = 0;
717 for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
718 pos += sprintf(buf + pos, "%d ", arates_hz[i]);
719 sprintf(buf + pos - 1, "\n");
720 return pos;
721}
722
723static ssize_t apds990x_rate_show(struct device *dev,
724 struct device_attribute *attr, char *buf)
725{
726 struct apds990x_chip *chip = dev_get_drvdata(dev);
727 return sprintf(buf, "%d\n", chip->arate);
728}
729
730static int apds990x_set_arate(struct apds990x_chip *chip, int rate)
731{
732 int i;
733
734 for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
735 if (rate >= arates_hz[i])
736 break;
737
738 if (i == ARRAY_SIZE(arates_hz))
739 return -EINVAL;
740
741 /* Pick up corresponding persistence value */
742 chip->lux_persistence = apersis[i];
743 chip->arate = arates_hz[i];
744
745 /* If the chip is not in use, don't try to access it */
746 if (pm_runtime_suspended(&chip->client->dev))
747 return 0;
748
749 /* Persistence levels */
750 return apds990x_write_byte(chip, APDS990X_PERS,
751 (chip->lux_persistence << APDS990X_APERS_SHIFT) |
752 (chip->prox_persistence << APDS990X_PPERS_SHIFT));
753}
754
755static ssize_t apds990x_rate_store(struct device *dev,
756 struct device_attribute *attr,
757 const char *buf, size_t len)
758{
759 struct apds990x_chip *chip = dev_get_drvdata(dev);
760 unsigned long value;
761 int ret;
762
763 if (strict_strtoul(buf, 0, &value))
764 return -EINVAL;
765
766 mutex_lock(&chip->mutex);
767 ret = apds990x_set_arate(chip, value);
768 mutex_unlock(&chip->mutex);
769
770 if (ret < 0)
771 return ret;
772 return len;
773}
774
775static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, apds990x_rate_avail, NULL);
776
777static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, apds990x_rate_show,
778 apds990x_rate_store);
779
780static ssize_t apds990x_prox_show(struct device *dev,
781 struct device_attribute *attr, char *buf)
782{
783 ssize_t ret;
784 struct apds990x_chip *chip = dev_get_drvdata(dev);
785 if (pm_runtime_suspended(dev) || !chip->prox_en)
786 return -EIO;
787
788 mutex_lock(&chip->mutex);
789 ret = sprintf(buf, "%d\n", chip->prox_data);
790 mutex_unlock(&chip->mutex);
791 return ret;
792}
793
794static DEVICE_ATTR(prox0_raw, S_IRUGO, apds990x_prox_show, NULL);
795
796static ssize_t apds990x_prox_range_show(struct device *dev,
797 struct device_attribute *attr, char *buf)
798{
799 return sprintf(buf, "%u\n", APDS_PROX_RANGE);
800}
801
802static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, apds990x_prox_range_show, NULL);
803
804static ssize_t apds990x_prox_enable_show(struct device *dev,
805 struct device_attribute *attr, char *buf)
806{
807 struct apds990x_chip *chip = dev_get_drvdata(dev);
808 return sprintf(buf, "%d\n", chip->prox_en);
809}
810
811static ssize_t apds990x_prox_enable_store(struct device *dev,
812 struct device_attribute *attr,
813 const char *buf, size_t len)
814{
815 struct apds990x_chip *chip = dev_get_drvdata(dev);
816 unsigned long value;
817
818 if (strict_strtoul(buf, 0, &value))
819 return -EINVAL;
820
821 mutex_lock(&chip->mutex);
822
823 if (!chip->prox_en)
824 chip->prox_data = 0;
825
826 if (value)
827 chip->prox_en++;
828 else if (chip->prox_en > 0)
829 chip->prox_en--;
830
831 if (!pm_runtime_suspended(dev))
832 apds990x_mode_on(chip);
833 mutex_unlock(&chip->mutex);
834 return len;
835}
836
837static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, apds990x_prox_enable_show,
838 apds990x_prox_enable_store);
839
840static const char reporting_modes[][9] = {"trigger", "periodic"};
841
842static ssize_t apds990x_prox_reporting_mode_show(struct device *dev,
843 struct device_attribute *attr, char *buf)
844{
845 struct apds990x_chip *chip = dev_get_drvdata(dev);
846 return sprintf(buf, "%s\n",
847 reporting_modes[!!chip->prox_continuous_mode]);
848}
849
850static ssize_t apds990x_prox_reporting_mode_store(struct device *dev,
851 struct device_attribute *attr,
852 const char *buf, size_t len)
853{
854 struct apds990x_chip *chip = dev_get_drvdata(dev);
855
856 if (sysfs_streq(buf, reporting_modes[0]))
857 chip->prox_continuous_mode = 0;
858 else if (sysfs_streq(buf, reporting_modes[1]))
859 chip->prox_continuous_mode = 1;
860 else
861 return -EINVAL;
862 return len;
863}
864
865static DEVICE_ATTR(prox0_reporting_mode, S_IRUGO | S_IWUSR,
866 apds990x_prox_reporting_mode_show,
867 apds990x_prox_reporting_mode_store);
868
869static ssize_t apds990x_prox_reporting_avail_show(struct device *dev,
870 struct device_attribute *attr, char *buf)
871{
872 return sprintf(buf, "%s %s\n", reporting_modes[0], reporting_modes[1]);
873}
874
875static DEVICE_ATTR(prox0_reporting_mode_avail, S_IRUGO | S_IWUSR,
876 apds990x_prox_reporting_avail_show, NULL);
877
878
879static ssize_t apds990x_lux_thresh_above_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
881{
882 struct apds990x_chip *chip = dev_get_drvdata(dev);
883 return sprintf(buf, "%d\n", chip->lux_thres_hi);
884}
885
886static ssize_t apds990x_lux_thresh_below_show(struct device *dev,
887 struct device_attribute *attr, char *buf)
888{
889 struct apds990x_chip *chip = dev_get_drvdata(dev);
890 return sprintf(buf, "%d\n", chip->lux_thres_lo);
891}
892
893static ssize_t apds990x_set_lux_thresh(struct apds990x_chip *chip, u32 *target,
894 const char *buf)
895{
896 int ret = 0;
897 unsigned long thresh;
898
899 if (strict_strtoul(buf, 0, &thresh))
900 return -EINVAL;
901
902 if (thresh > APDS_RANGE)
903 return -EINVAL;
904
905 mutex_lock(&chip->mutex);
906 *target = thresh;
907 /*
908 * Don't update values in HW if we are still waiting for
909 * first interrupt to come after device handle open call.
910 */
911 if (!chip->lux_wait_fresh_res)
912 apds990x_refresh_athres(chip);
913 mutex_unlock(&chip->mutex);
914 return ret;
915
916}
917
918static ssize_t apds990x_lux_thresh_above_store(struct device *dev,
919 struct device_attribute *attr,
920 const char *buf, size_t len)
921{
922 struct apds990x_chip *chip = dev_get_drvdata(dev);
923 int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_hi, buf);
924 if (ret < 0)
925 return ret;
926 return len;
927}
928
929static ssize_t apds990x_lux_thresh_below_store(struct device *dev,
930 struct device_attribute *attr,
931 const char *buf, size_t len)
932{
933 struct apds990x_chip *chip = dev_get_drvdata(dev);
934 int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_lo, buf);
935 if (ret < 0)
936 return ret;
937 return len;
938}
939
940static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
941 apds990x_lux_thresh_above_show,
942 apds990x_lux_thresh_above_store);
943
944static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
945 apds990x_lux_thresh_below_show,
946 apds990x_lux_thresh_below_store);
947
948static ssize_t apds990x_prox_threshold_show(struct device *dev,
949 struct device_attribute *attr, char *buf)
950{
951 struct apds990x_chip *chip = dev_get_drvdata(dev);
952 return sprintf(buf, "%d\n", chip->prox_thres);
953}
954
955static ssize_t apds990x_prox_threshold_store(struct device *dev,
956 struct device_attribute *attr,
957 const char *buf, size_t len)
958{
959 struct apds990x_chip *chip = dev_get_drvdata(dev);
960 unsigned long value;
961
962 if (strict_strtoul(buf, 0, &value))
963 return -EINVAL;
964
965 if ((value > APDS_RANGE) || (value == 0) ||
966 (value < APDS_PROX_HYSTERESIS))
967 return -EINVAL;
968
969 mutex_lock(&chip->mutex);
970 chip->prox_thres = value;
971
972 apds990x_force_p_refresh(chip);
973 mutex_unlock(&chip->mutex);
974 return len;
975}
976
977static DEVICE_ATTR(prox0_thresh_above_value, S_IRUGO | S_IWUSR,
978 apds990x_prox_threshold_show,
979 apds990x_prox_threshold_store);
980
981static ssize_t apds990x_power_state_show(struct device *dev,
982 struct device_attribute *attr, char *buf)
983{
984 return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
985 return 0;
986}
987
988static ssize_t apds990x_power_state_store(struct device *dev,
989 struct device_attribute *attr,
990 const char *buf, size_t len)
991{
992 struct apds990x_chip *chip = dev_get_drvdata(dev);
993 unsigned long value;
994
995 if (strict_strtoul(buf, 0, &value))
996 return -EINVAL;
997 if (value) {
998 pm_runtime_get_sync(dev);
999 mutex_lock(&chip->mutex);
1000 chip->lux_wait_fresh_res = true;
1001 apds990x_force_a_refresh(chip);
1002 apds990x_force_p_refresh(chip);
1003 mutex_unlock(&chip->mutex);
1004 } else {
1005 if (!pm_runtime_suspended(dev))
1006 pm_runtime_put(dev);
1007 }
1008 return len;
1009}
1010
1011static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
1012 apds990x_power_state_show,
1013 apds990x_power_state_store);
1014
1015static ssize_t apds990x_chip_id_show(struct device *dev,
1016 struct device_attribute *attr, char *buf)
1017{
1018 struct apds990x_chip *chip = dev_get_drvdata(dev);
1019 return sprintf(buf, "%s %d\n", chip->chipname, chip->revision);
1020}
1021
1022static DEVICE_ATTR(chip_id, S_IRUGO, apds990x_chip_id_show, NULL);
1023
1024static struct attribute *sysfs_attrs_ctrl[] = {
1025 &dev_attr_lux0_calibscale.attr,
1026 &dev_attr_lux0_calibscale_default.attr,
1027 &dev_attr_lux0_input.attr,
1028 &dev_attr_lux0_sensor_range.attr,
1029 &dev_attr_lux0_rate.attr,
1030 &dev_attr_lux0_rate_avail.attr,
1031 &dev_attr_lux0_thresh_above_value.attr,
1032 &dev_attr_lux0_thresh_below_value.attr,
1033 &dev_attr_prox0_raw_en.attr,
1034 &dev_attr_prox0_raw.attr,
1035 &dev_attr_prox0_sensor_range.attr,
1036 &dev_attr_prox0_thresh_above_value.attr,
1037 &dev_attr_prox0_reporting_mode.attr,
1038 &dev_attr_prox0_reporting_mode_avail.attr,
1039 &dev_attr_chip_id.attr,
1040 &dev_attr_power_state.attr,
1041 NULL
1042};
1043
1044static struct attribute_group apds990x_attribute_group[] = {
1045 {.attrs = sysfs_attrs_ctrl },
1046};
1047
1048static int __devinit apds990x_probe(struct i2c_client *client,
1049 const struct i2c_device_id *id)
1050{
1051 struct apds990x_chip *chip;
1052 int err;
1053
1054 chip = kzalloc(sizeof *chip, GFP_KERNEL);
1055 if (!chip)
1056 return -ENOMEM;
1057
1058 i2c_set_clientdata(client, chip);
1059 chip->client = client;
1060
1061 init_waitqueue_head(&chip->wait);
1062 mutex_init(&chip->mutex);
1063 chip->pdata = client->dev.platform_data;
1064
1065 if (chip->pdata == NULL) {
1066 dev_err(&client->dev, "platform data is mandatory\n");
1067 err = -EINVAL;
1068 goto fail1;
1069 }
1070
1071 if (chip->pdata->cf.ga == 0) {
1072 /* set uncovered sensor default parameters */
1073 chip->cf.ga = 1966; /* 0.48 * APDS_PARAM_SCALE */
1074 chip->cf.cf1 = 4096; /* 1.00 * APDS_PARAM_SCALE */
1075 chip->cf.irf1 = 9134; /* 2.23 * APDS_PARAM_SCALE */
1076 chip->cf.cf2 = 2867; /* 0.70 * APDS_PARAM_SCALE */
1077 chip->cf.irf2 = 5816; /* 1.42 * APDS_PARAM_SCALE */
1078 chip->cf.df = 52;
1079 } else {
1080 chip->cf = chip->pdata->cf;
1081 }
1082
1083 /* precalculate inverse chip factors for threshold control */
1084 chip->rcf.afactor =
1085 (chip->cf.irf1 - chip->cf.irf2) * APDS_PARAM_SCALE /
1086 (chip->cf.cf1 - chip->cf.cf2);
1087 chip->rcf.cf1 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
1088 chip->cf.cf1;
1089 chip->rcf.irf1 = chip->cf.irf1 * APDS_PARAM_SCALE /
1090 chip->cf.cf1;
1091 chip->rcf.cf2 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
1092 chip->cf.cf2;
1093 chip->rcf.irf2 = chip->cf.irf2 * APDS_PARAM_SCALE /
1094 chip->cf.cf2;
1095
1096 /* Set something to start with */
1097 chip->lux_thres_hi = APDS_LUX_DEF_THRES_HI;
1098 chip->lux_thres_lo = APDS_LUX_DEF_THRES_LO;
1099 chip->lux_calib = APDS_LUX_NEUTRAL_CALIB_VALUE;
1100
1101 chip->prox_thres = APDS_PROX_DEF_THRES;
1102 chip->pdrive = chip->pdata->pdrive;
1103 chip->pdiode = APDS_PDIODE_IR;
1104 chip->pgain = APDS_PGAIN_1X;
1105 chip->prox_calib = APDS_PROX_NEUTRAL_CALIB_VALUE;
1106 chip->prox_persistence = APDS_DEFAULT_PROX_PERS;
1107 chip->prox_continuous_mode = false;
1108
1109 chip->regs[0].supply = reg_vcc;
1110 chip->regs[1].supply = reg_vled;
1111
1112 err = regulator_bulk_get(&client->dev,
1113 ARRAY_SIZE(chip->regs), chip->regs);
1114 if (err < 0) {
1115 dev_err(&client->dev, "Cannot get regulators\n");
1116 goto fail1;
1117 }
1118
1119 err = regulator_bulk_enable(ARRAY_SIZE(chip->regs), chip->regs);
1120 if (err < 0) {
1121 dev_err(&client->dev, "Cannot enable regulators\n");
1122 goto fail2;
1123 }
1124
1125 usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
1126
1127 err = apds990x_detect(chip);
1128 if (err < 0) {
1129 dev_err(&client->dev, "APDS990X not found\n");
1130 goto fail3;
1131 }
1132
1133 pm_runtime_set_active(&client->dev);
1134
1135 apds990x_configure(chip);
1136 apds990x_set_arate(chip, APDS_LUX_DEFAULT_RATE);
1137 apds990x_mode_on(chip);
1138
1139 pm_runtime_enable(&client->dev);
1140
1141 if (chip->pdata->setup_resources) {
1142 err = chip->pdata->setup_resources();
1143 if (err) {
1144 err = -EINVAL;
1145 goto fail3;
1146 }
1147 }
1148
1149 err = sysfs_create_group(&chip->client->dev.kobj,
1150 apds990x_attribute_group);
1151 if (err < 0) {
1152 dev_err(&chip->client->dev, "Sysfs registration failed\n");
1153 goto fail4;
1154 }
1155
1156 err = request_threaded_irq(client->irq, NULL,
1157 apds990x_irq,
1158 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW |
1159 IRQF_ONESHOT,
1160 "apds990x", chip);
1161 if (err) {
1162 dev_err(&client->dev, "could not get IRQ %d\n",
1163 client->irq);
1164 goto fail5;
1165 }
1166 return err;
1167fail5:
1168 sysfs_remove_group(&chip->client->dev.kobj,
1169 &apds990x_attribute_group[0]);
1170fail4:
1171 if (chip->pdata && chip->pdata->release_resources)
1172 chip->pdata->release_resources();
1173fail3:
1174 regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
1175fail2:
1176 regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
1177fail1:
1178 kfree(chip);
1179 return err;
1180}
1181
1182static int __devexit apds990x_remove(struct i2c_client *client)
1183{
1184 struct apds990x_chip *chip = i2c_get_clientdata(client);
1185
1186 free_irq(client->irq, chip);
1187 sysfs_remove_group(&chip->client->dev.kobj,
1188 apds990x_attribute_group);
1189
1190 if (chip->pdata && chip->pdata->release_resources)
1191 chip->pdata->release_resources();
1192
1193 if (!pm_runtime_suspended(&client->dev))
1194 apds990x_chip_off(chip);
1195
1196 pm_runtime_disable(&client->dev);
1197 pm_runtime_set_suspended(&client->dev);
1198
1199 regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
1200
1201 kfree(chip);
1202 return 0;
1203}
1204
1205#ifdef CONFIG_PM
1206static int apds990x_suspend(struct device *dev)
1207{
1208 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1209 struct apds990x_chip *chip = i2c_get_clientdata(client);
1210
1211 apds990x_chip_off(chip);
1212 return 0;
1213}
1214
1215static int apds990x_resume(struct device *dev)
1216{
1217 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1218 struct apds990x_chip *chip = i2c_get_clientdata(client);
1219
1220 /*
1221 * If we were enabled at suspend time, it is expected
1222 * everything works nice and smoothly. Chip_on is enough
1223 */
1224 apds990x_chip_on(chip);
1225
1226 return 0;
1227}
1228#else
1229#define apds990x_suspend NULL
1230#define apds990x_resume NULL
1231#define apds990x_shutdown NULL
1232#endif
1233
1234#ifdef CONFIG_PM_RUNTIME
1235static int apds990x_runtime_suspend(struct device *dev)
1236{
1237 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1238 struct apds990x_chip *chip = i2c_get_clientdata(client);
1239
1240 apds990x_chip_off(chip);
1241 return 0;
1242}
1243
1244static int apds990x_runtime_resume(struct device *dev)
1245{
1246 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
1247 struct apds990x_chip *chip = i2c_get_clientdata(client);
1248
1249 apds990x_chip_on(chip);
1250 return 0;
1251}
1252
1253#endif
1254
1255static const struct i2c_device_id apds990x_id[] = {
1256 {"apds990x", 0 },
1257 {}
1258};
1259
1260MODULE_DEVICE_TABLE(i2c, apds990x_id);
1261
1262static const struct dev_pm_ops apds990x_pm_ops = {
1263 SET_SYSTEM_SLEEP_PM_OPS(apds990x_suspend, apds990x_resume)
1264 SET_RUNTIME_PM_OPS(apds990x_runtime_suspend,
1265 apds990x_runtime_resume,
1266 NULL)
1267};
1268
1269static struct i2c_driver apds990x_driver = {
1270 .driver = {
1271 .name = "apds990x",
1272 .owner = THIS_MODULE,
1273 .pm = &apds990x_pm_ops,
1274 },
1275 .probe = apds990x_probe,
1276 .remove = __devexit_p(apds990x_remove),
1277 .id_table = apds990x_id,
1278};
1279
1280static int __init apds990x_init(void)
1281{
1282 return i2c_add_driver(&apds990x_driver);
1283}
1284
1285static void __exit apds990x_exit(void)
1286{
1287 i2c_del_driver(&apds990x_driver);
1288}
1289
1290MODULE_DESCRIPTION("APDS990X combined ALS and proximity sensor");
1291MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
1292MODULE_LICENSE("GPL v2");
1293
1294module_init(apds990x_init);
1295module_exit(apds990x_exit);