2 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
3 * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
5 * IIO features supported by the driver:
7 * Read-only raw channels:
8 * - illuminance_clear [lux]
18 * - illuminance_clear (rising and falling)
19 * - proximity (rising and falling)
20 * - both falling and rising thresholds for the proximity events
21 * must be set to the values greater than 0.
23 * The driver supports triggered buffers for all the three
24 * channels as well as high and low threshold events for the
25 * illuminance_clear and proxmimity channels. Triggers
26 * can be enabled simultaneously with both illuminance_clear
27 * events. Proximity events cannot be enabled simultaneously
28 * with any triggers or illuminance events. Enabling/disabling
29 * one of the proximity events automatically enables/disables
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License version 2, as
34 * published by the Free Software Foundation.
37 #include <linux/debugfs.h>
38 #include <linux/delay.h>
39 #include <linux/i2c.h>
40 #include <linux/interrupt.h>
41 #include <linux/irq.h>
42 #include <linux/irq_work.h>
43 #include <linux/module.h>
44 #include <linux/mutex.h>
46 #include <linux/regmap.h>
47 #include <linux/regulator/consumer.h>
48 #include <linux/slab.h>
49 #include <asm/unaligned.h>
50 #include <linux/iio/buffer.h>
51 #include <linux/iio/events.h>
52 #include <linux/iio/iio.h>
53 #include <linux/iio/sysfs.h>
54 #include <linux/iio/trigger.h>
55 #include <linux/iio/trigger_consumer.h>
56 #include <linux/iio/triggered_buffer.h>
58 #define GP2A_I2C_NAME "gp2ap020a00f"
61 #define GP2AP020A00F_OP_REG 0x00 /* Basic operations */
62 #define GP2AP020A00F_ALS_REG 0x01 /* ALS related settings */
63 #define GP2AP020A00F_PS_REG 0x02 /* PS related settings */
64 #define GP2AP020A00F_LED_REG 0x03 /* LED reg */
65 #define GP2AP020A00F_TL_L_REG 0x04 /* ALS: Threshold low LSB */
66 #define GP2AP020A00F_TL_H_REG 0x05 /* ALS: Threshold low MSB */
67 #define GP2AP020A00F_TH_L_REG 0x06 /* ALS: Threshold high LSB */
68 #define GP2AP020A00F_TH_H_REG 0x07 /* ALS: Threshold high MSB */
69 #define GP2AP020A00F_PL_L_REG 0x08 /* PS: Threshold low LSB */
70 #define GP2AP020A00F_PL_H_REG 0x09 /* PS: Threshold low MSB */
71 #define GP2AP020A00F_PH_L_REG 0x0a /* PS: Threshold high LSB */
72 #define GP2AP020A00F_PH_H_REG 0x0b /* PS: Threshold high MSB */
73 #define GP2AP020A00F_D0_L_REG 0x0c /* ALS result: Clear/Illuminance LSB */
74 #define GP2AP020A00F_D0_H_REG 0x0d /* ALS result: Clear/Illuminance MSB */
75 #define GP2AP020A00F_D1_L_REG 0x0e /* ALS result: IR LSB */
76 #define GP2AP020A00F_D1_H_REG 0x0f /* ALS result: IR LSB */
77 #define GP2AP020A00F_D2_L_REG 0x10 /* PS result LSB */
78 #define GP2AP020A00F_D2_H_REG 0x11 /* PS result MSB */
79 #define GP2AP020A00F_NUM_REGS 0x12 /* Number of registers */
82 #define GP2AP020A00F_OP3_MASK 0x80 /* Software shutdown */
83 #define GP2AP020A00F_OP3_SHUTDOWN 0x00
84 #define GP2AP020A00F_OP3_OPERATION 0x80
85 #define GP2AP020A00F_OP2_MASK 0x40 /* Auto shutdown/Continuous mode */
86 #define GP2AP020A00F_OP2_AUTO_SHUTDOWN 0x00
87 #define GP2AP020A00F_OP2_CONT_OPERATION 0x40
88 #define GP2AP020A00F_OP_MASK 0x30 /* Operating mode selection */
89 #define GP2AP020A00F_OP_ALS_AND_PS 0x00
90 #define GP2AP020A00F_OP_ALS 0x10
91 #define GP2AP020A00F_OP_PS 0x20
92 #define GP2AP020A00F_OP_DEBUG 0x30
93 #define GP2AP020A00F_PROX_MASK 0x08 /* PS: detection/non-detection */
94 #define GP2AP020A00F_PROX_NON_DETECT 0x00
95 #define GP2AP020A00F_PROX_DETECT 0x08
96 #define GP2AP020A00F_FLAG_P 0x04 /* PS: interrupt result */
97 #define GP2AP020A00F_FLAG_A 0x02 /* ALS: interrupt result */
98 #define GP2AP020A00F_TYPE_MASK 0x01 /* Output data type selection */
99 #define GP2AP020A00F_TYPE_MANUAL_CALC 0x00
100 #define GP2AP020A00F_TYPE_AUTO_CALC 0x01
103 #define GP2AP020A00F_PRST_MASK 0xc0 /* Number of measurement cycles */
104 #define GP2AP020A00F_PRST_ONCE 0x00
105 #define GP2AP020A00F_PRST_4_CYCLES 0x40
106 #define GP2AP020A00F_PRST_8_CYCLES 0x80
107 #define GP2AP020A00F_PRST_16_CYCLES 0xc0
108 #define GP2AP020A00F_RES_A_MASK 0x38 /* ALS: Resolution */
109 #define GP2AP020A00F_RES_A_800ms 0x00
110 #define GP2AP020A00F_RES_A_400ms 0x08
111 #define GP2AP020A00F_RES_A_200ms 0x10
112 #define GP2AP020A00F_RES_A_100ms 0x18
113 #define GP2AP020A00F_RES_A_25ms 0x20
114 #define GP2AP020A00F_RES_A_6_25ms 0x28
115 #define GP2AP020A00F_RES_A_1_56ms 0x30
116 #define GP2AP020A00F_RES_A_0_39ms 0x38
117 #define GP2AP020A00F_RANGE_A_MASK 0x07 /* ALS: Max measurable range */
118 #define GP2AP020A00F_RANGE_A_x1 0x00
119 #define GP2AP020A00F_RANGE_A_x2 0x01
120 #define GP2AP020A00F_RANGE_A_x4 0x02
121 #define GP2AP020A00F_RANGE_A_x8 0x03
122 #define GP2AP020A00F_RANGE_A_x16 0x04
123 #define GP2AP020A00F_RANGE_A_x32 0x05
124 #define GP2AP020A00F_RANGE_A_x64 0x06
125 #define GP2AP020A00F_RANGE_A_x128 0x07
128 #define GP2AP020A00F_ALC_MASK 0x80 /* Auto light cancel */
129 #define GP2AP020A00F_ALC_ON 0x80
130 #define GP2AP020A00F_ALC_OFF 0x00
131 #define GP2AP020A00F_INTTYPE_MASK 0x40 /* Interrupt type setting */
132 #define GP2AP020A00F_INTTYPE_LEVEL 0x00
133 #define GP2AP020A00F_INTTYPE_PULSE 0x40
134 #define GP2AP020A00F_RES_P_MASK 0x38 /* PS: Resolution */
135 #define GP2AP020A00F_RES_P_800ms_x2 0x00
136 #define GP2AP020A00F_RES_P_400ms_x2 0x08
137 #define GP2AP020A00F_RES_P_200ms_x2 0x10
138 #define GP2AP020A00F_RES_P_100ms_x2 0x18
139 #define GP2AP020A00F_RES_P_25ms_x2 0x20
140 #define GP2AP020A00F_RES_P_6_25ms_x2 0x28
141 #define GP2AP020A00F_RES_P_1_56ms_x2 0x30
142 #define GP2AP020A00F_RES_P_0_39ms_x2 0x38
143 #define GP2AP020A00F_RANGE_P_MASK 0x07 /* PS: Max measurable range */
144 #define GP2AP020A00F_RANGE_P_x1 0x00
145 #define GP2AP020A00F_RANGE_P_x2 0x01
146 #define GP2AP020A00F_RANGE_P_x4 0x02
147 #define GP2AP020A00F_RANGE_P_x8 0x03
148 #define GP2AP020A00F_RANGE_P_x16 0x04
149 #define GP2AP020A00F_RANGE_P_x32 0x05
150 #define GP2AP020A00F_RANGE_P_x64 0x06
151 #define GP2AP020A00F_RANGE_P_x128 0x07
154 #define GP2AP020A00F_INTVAL_MASK 0xc0 /* Intermittent operating */
155 #define GP2AP020A00F_INTVAL_0 0x00
156 #define GP2AP020A00F_INTVAL_4 0x40
157 #define GP2AP020A00F_INTVAL_8 0x80
158 #define GP2AP020A00F_INTVAL_16 0xc0
159 #define GP2AP020A00F_IS_MASK 0x30 /* ILED drive peak current */
160 #define GP2AP020A00F_IS_13_8mA 0x00
161 #define GP2AP020A00F_IS_27_5mA 0x10
162 #define GP2AP020A00F_IS_55mA 0x20
163 #define GP2AP020A00F_IS_110mA 0x30
164 #define GP2AP020A00F_PIN_MASK 0x0c /* INT terminal setting */
165 #define GP2AP020A00F_PIN_ALS_OR_PS 0x00
166 #define GP2AP020A00F_PIN_ALS 0x04
167 #define GP2AP020A00F_PIN_PS 0x08
168 #define GP2AP020A00F_PIN_PS_DETECT 0x0c
169 #define GP2AP020A00F_FREQ_MASK 0x02 /* LED modulation frequency */
170 #define GP2AP020A00F_FREQ_327_5kHz 0x00
171 #define GP2AP020A00F_FREQ_81_8kHz 0x02
172 #define GP2AP020A00F_RST 0x01 /* Software reset */
174 #define GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR 0
175 #define GP2AP020A00F_SCAN_MODE_LIGHT_IR 1
176 #define GP2AP020A00F_SCAN_MODE_PROXIMITY 2
177 #define GP2AP020A00F_CHAN_TIMESTAMP 3
179 #define GP2AP020A00F_DATA_READY_TIMEOUT msecs_to_jiffies(1000)
180 #define GP2AP020A00F_DATA_REG(chan) (GP2AP020A00F_D0_L_REG + \
182 #define GP2AP020A00F_THRESH_REG(th_val_id) (GP2AP020A00F_TL_L_REG + \
184 #define GP2AP020A00F_THRESH_VAL_ID(reg_addr) ((reg_addr - 4) / 2)
186 #define GP2AP020A00F_SUBTRACT_MODE 0
187 #define GP2AP020A00F_ADD_MODE 1
189 #define GP2AP020A00F_MAX_CHANNELS 3
191 enum gp2ap020a00f_opmode
{
192 GP2AP020A00F_OPMODE_READ_RAW_CLEAR
,
193 GP2AP020A00F_OPMODE_READ_RAW_IR
,
194 GP2AP020A00F_OPMODE_READ_RAW_PROXIMITY
,
195 GP2AP020A00F_OPMODE_ALS
,
196 GP2AP020A00F_OPMODE_PS
,
197 GP2AP020A00F_OPMODE_ALS_AND_PS
,
198 GP2AP020A00F_OPMODE_PROX_DETECT
,
199 GP2AP020A00F_OPMODE_SHUTDOWN
,
200 GP2AP020A00F_NUM_OPMODES
,
203 enum gp2ap020a00f_cmd
{
204 GP2AP020A00F_CMD_READ_RAW_CLEAR
,
205 GP2AP020A00F_CMD_READ_RAW_IR
,
206 GP2AP020A00F_CMD_READ_RAW_PROXIMITY
,
207 GP2AP020A00F_CMD_TRIGGER_CLEAR_EN
,
208 GP2AP020A00F_CMD_TRIGGER_CLEAR_DIS
,
209 GP2AP020A00F_CMD_TRIGGER_IR_EN
,
210 GP2AP020A00F_CMD_TRIGGER_IR_DIS
,
211 GP2AP020A00F_CMD_TRIGGER_PROX_EN
,
212 GP2AP020A00F_CMD_TRIGGER_PROX_DIS
,
213 GP2AP020A00F_CMD_ALS_HIGH_EV_EN
,
214 GP2AP020A00F_CMD_ALS_HIGH_EV_DIS
,
215 GP2AP020A00F_CMD_ALS_LOW_EV_EN
,
216 GP2AP020A00F_CMD_ALS_LOW_EV_DIS
,
217 GP2AP020A00F_CMD_PROX_HIGH_EV_EN
,
218 GP2AP020A00F_CMD_PROX_HIGH_EV_DIS
,
219 GP2AP020A00F_CMD_PROX_LOW_EV_EN
,
220 GP2AP020A00F_CMD_PROX_LOW_EV_DIS
,
223 enum gp2ap020a00f_flags
{
224 GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
,
225 GP2AP020A00F_FLAG_ALS_IR_TRIGGER
,
226 GP2AP020A00F_FLAG_PROX_TRIGGER
,
227 GP2AP020A00F_FLAG_PROX_RISING_EV
,
228 GP2AP020A00F_FLAG_PROX_FALLING_EV
,
229 GP2AP020A00F_FLAG_ALS_RISING_EV
,
230 GP2AP020A00F_FLAG_ALS_FALLING_EV
,
231 GP2AP020A00F_FLAG_LUX_MODE_HI
,
232 GP2AP020A00F_FLAG_DATA_READY
,
235 enum gp2ap020a00f_thresh_val_id
{
236 GP2AP020A00F_THRESH_TL
,
237 GP2AP020A00F_THRESH_TH
,
238 GP2AP020A00F_THRESH_PL
,
239 GP2AP020A00F_THRESH_PH
,
242 struct gp2ap020a00f_data
{
243 const struct gp2ap020a00f_platform_data
*pdata
;
244 struct i2c_client
*client
;
247 struct regulator
*vled_reg
;
249 enum gp2ap020a00f_opmode cur_opmode
;
250 struct iio_trigger
*trig
;
251 struct regmap
*regmap
;
252 unsigned int thresh_val
[4];
254 struct irq_work work
;
255 wait_queue_head_t data_ready_queue
;
258 static const u8 gp2ap020a00f_reg_init_tab
[] = {
259 [GP2AP020A00F_OP_REG
] = GP2AP020A00F_OP3_SHUTDOWN
,
260 [GP2AP020A00F_ALS_REG
] = GP2AP020A00F_RES_A_25ms
|
261 GP2AP020A00F_RANGE_A_x8
,
262 [GP2AP020A00F_PS_REG
] = GP2AP020A00F_ALC_ON
|
263 GP2AP020A00F_RES_P_1_56ms_x2
|
264 GP2AP020A00F_RANGE_P_x4
,
265 [GP2AP020A00F_LED_REG
] = GP2AP020A00F_INTVAL_0
|
266 GP2AP020A00F_IS_110mA
|
267 GP2AP020A00F_FREQ_327_5kHz
,
268 [GP2AP020A00F_TL_L_REG
] = 0,
269 [GP2AP020A00F_TL_H_REG
] = 0,
270 [GP2AP020A00F_TH_L_REG
] = 0,
271 [GP2AP020A00F_TH_H_REG
] = 0,
272 [GP2AP020A00F_PL_L_REG
] = 0,
273 [GP2AP020A00F_PL_H_REG
] = 0,
274 [GP2AP020A00F_PH_L_REG
] = 0,
275 [GP2AP020A00F_PH_H_REG
] = 0,
278 static bool gp2ap020a00f_is_volatile_reg(struct device
*dev
, unsigned int reg
)
281 case GP2AP020A00F_OP_REG
:
282 case GP2AP020A00F_D0_L_REG
:
283 case GP2AP020A00F_D0_H_REG
:
284 case GP2AP020A00F_D1_L_REG
:
285 case GP2AP020A00F_D1_H_REG
:
286 case GP2AP020A00F_D2_L_REG
:
287 case GP2AP020A00F_D2_H_REG
:
294 static const struct regmap_config gp2ap020a00f_regmap_config
= {
298 .max_register
= GP2AP020A00F_D2_H_REG
,
299 .cache_type
= REGCACHE_RBTREE
,
301 .volatile_reg
= gp2ap020a00f_is_volatile_reg
,
304 static const struct gp2ap020a00f_mutable_config_regs
{
309 } opmode_regs_settings
[GP2AP020A00F_NUM_OPMODES
] = {
310 [GP2AP020A00F_OPMODE_READ_RAW_CLEAR
] = {
311 GP2AP020A00F_OP_ALS
| GP2AP020A00F_OP2_CONT_OPERATION
312 | GP2AP020A00F_OP3_OPERATION
313 | GP2AP020A00F_TYPE_AUTO_CALC
,
314 GP2AP020A00F_PRST_ONCE
,
315 GP2AP020A00F_INTTYPE_LEVEL
,
318 [GP2AP020A00F_OPMODE_READ_RAW_IR
] = {
319 GP2AP020A00F_OP_ALS
| GP2AP020A00F_OP2_CONT_OPERATION
320 | GP2AP020A00F_OP3_OPERATION
321 | GP2AP020A00F_TYPE_MANUAL_CALC
,
322 GP2AP020A00F_PRST_ONCE
,
323 GP2AP020A00F_INTTYPE_LEVEL
,
326 [GP2AP020A00F_OPMODE_READ_RAW_PROXIMITY
] = {
327 GP2AP020A00F_OP_PS
| GP2AP020A00F_OP2_CONT_OPERATION
328 | GP2AP020A00F_OP3_OPERATION
329 | GP2AP020A00F_TYPE_MANUAL_CALC
,
330 GP2AP020A00F_PRST_ONCE
,
331 GP2AP020A00F_INTTYPE_LEVEL
,
334 [GP2AP020A00F_OPMODE_PROX_DETECT
] = {
335 GP2AP020A00F_OP_PS
| GP2AP020A00F_OP2_CONT_OPERATION
336 | GP2AP020A00F_OP3_OPERATION
337 | GP2AP020A00F_TYPE_MANUAL_CALC
,
338 GP2AP020A00F_PRST_4_CYCLES
,
339 GP2AP020A00F_INTTYPE_PULSE
,
340 GP2AP020A00F_PIN_PS_DETECT
342 [GP2AP020A00F_OPMODE_ALS
] = {
343 GP2AP020A00F_OP_ALS
| GP2AP020A00F_OP2_CONT_OPERATION
344 | GP2AP020A00F_OP3_OPERATION
345 | GP2AP020A00F_TYPE_AUTO_CALC
,
346 GP2AP020A00F_PRST_ONCE
,
347 GP2AP020A00F_INTTYPE_LEVEL
,
350 [GP2AP020A00F_OPMODE_PS
] = {
351 GP2AP020A00F_OP_PS
| GP2AP020A00F_OP2_CONT_OPERATION
352 | GP2AP020A00F_OP3_OPERATION
353 | GP2AP020A00F_TYPE_MANUAL_CALC
,
354 GP2AP020A00F_PRST_4_CYCLES
,
355 GP2AP020A00F_INTTYPE_LEVEL
,
358 [GP2AP020A00F_OPMODE_ALS_AND_PS
] = {
359 GP2AP020A00F_OP_ALS_AND_PS
360 | GP2AP020A00F_OP2_CONT_OPERATION
361 | GP2AP020A00F_OP3_OPERATION
362 | GP2AP020A00F_TYPE_AUTO_CALC
,
363 GP2AP020A00F_PRST_4_CYCLES
,
364 GP2AP020A00F_INTTYPE_LEVEL
,
365 GP2AP020A00F_PIN_ALS_OR_PS
367 [GP2AP020A00F_OPMODE_SHUTDOWN
] = { GP2AP020A00F_OP3_SHUTDOWN
, },
370 static int gp2ap020a00f_set_operation_mode(struct gp2ap020a00f_data
*data
,
371 enum gp2ap020a00f_opmode op
)
373 unsigned int op_reg_val
;
376 if (op
!= GP2AP020A00F_OPMODE_SHUTDOWN
) {
377 err
= regmap_read(data
->regmap
, GP2AP020A00F_OP_REG
,
382 * Shutdown the device if the operation being executed entails
385 if ((opmode_regs_settings
[op
].op_reg
& GP2AP020A00F_OP_MASK
) !=
386 (op_reg_val
& GP2AP020A00F_OP_MASK
)) {
387 /* set shutdown mode */
388 err
= regmap_update_bits(data
->regmap
,
389 GP2AP020A00F_OP_REG
, GP2AP020A00F_OP3_MASK
,
390 GP2AP020A00F_OP3_SHUTDOWN
);
395 err
= regmap_update_bits(data
->regmap
, GP2AP020A00F_ALS_REG
,
396 GP2AP020A00F_PRST_MASK
, opmode_regs_settings
[op
]
401 err
= regmap_update_bits(data
->regmap
, GP2AP020A00F_PS_REG
,
402 GP2AP020A00F_INTTYPE_MASK
, opmode_regs_settings
[op
]
407 err
= regmap_update_bits(data
->regmap
, GP2AP020A00F_LED_REG
,
408 GP2AP020A00F_PIN_MASK
, opmode_regs_settings
[op
]
414 /* Set OP_REG and apply operation mode (power on / off) */
415 err
= regmap_update_bits(data
->regmap
,
417 GP2AP020A00F_OP_MASK
| GP2AP020A00F_OP2_MASK
|
418 GP2AP020A00F_OP3_MASK
| GP2AP020A00F_TYPE_MASK
,
419 opmode_regs_settings
[op
].op_reg
);
423 data
->cur_opmode
= op
;
428 static bool gp2ap020a00f_als_enabled(struct gp2ap020a00f_data
*data
)
430 return test_bit(GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
, &data
->flags
) ||
431 test_bit(GP2AP020A00F_FLAG_ALS_IR_TRIGGER
, &data
->flags
) ||
432 test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
) ||
433 test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
);
436 static bool gp2ap020a00f_prox_detect_enabled(struct gp2ap020a00f_data
*data
)
438 return test_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
) ||
439 test_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
);
442 static int gp2ap020a00f_write_event_threshold(struct gp2ap020a00f_data
*data
,
443 enum gp2ap020a00f_thresh_val_id th_val_id
,
446 __le16 thresh_buf
= 0;
447 unsigned int thresh_reg_val
;
451 else if (test_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
) &&
452 th_val_id
!= GP2AP020A00F_THRESH_PL
&&
453 th_val_id
!= GP2AP020A00F_THRESH_PH
)
455 * For the high lux mode ALS threshold has to be scaled down
456 * to allow for proper comparison with the output value.
458 thresh_reg_val
= data
->thresh_val
[th_val_id
] / 16;
460 thresh_reg_val
= data
->thresh_val
[th_val_id
] > 16000 ?
462 data
->thresh_val
[th_val_id
];
464 thresh_buf
= cpu_to_le16(thresh_reg_val
);
466 return regmap_bulk_write(data
->regmap
,
467 GP2AP020A00F_THRESH_REG(th_val_id
),
468 (u8
*)&thresh_buf
, 2);
471 static int gp2ap020a00f_alter_opmode(struct gp2ap020a00f_data
*data
,
472 enum gp2ap020a00f_opmode diff_mode
, int add_sub
)
474 enum gp2ap020a00f_opmode new_mode
;
476 if (diff_mode
!= GP2AP020A00F_OPMODE_ALS
&&
477 diff_mode
!= GP2AP020A00F_OPMODE_PS
)
480 if (add_sub
== GP2AP020A00F_ADD_MODE
) {
481 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_SHUTDOWN
)
482 new_mode
= diff_mode
;
484 new_mode
= GP2AP020A00F_OPMODE_ALS_AND_PS
;
486 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_ALS_AND_PS
)
487 new_mode
= (diff_mode
== GP2AP020A00F_OPMODE_ALS
) ?
488 GP2AP020A00F_OPMODE_PS
:
489 GP2AP020A00F_OPMODE_ALS
;
491 new_mode
= GP2AP020A00F_OPMODE_SHUTDOWN
;
494 return gp2ap020a00f_set_operation_mode(data
, new_mode
);
497 static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data
*data
,
498 enum gp2ap020a00f_cmd cmd
)
503 case GP2AP020A00F_CMD_READ_RAW_CLEAR
:
504 if (data
->cur_opmode
!= GP2AP020A00F_OPMODE_SHUTDOWN
)
506 err
= gp2ap020a00f_set_operation_mode(data
,
507 GP2AP020A00F_OPMODE_READ_RAW_CLEAR
);
509 case GP2AP020A00F_CMD_READ_RAW_IR
:
510 if (data
->cur_opmode
!= GP2AP020A00F_OPMODE_SHUTDOWN
)
512 err
= gp2ap020a00f_set_operation_mode(data
,
513 GP2AP020A00F_OPMODE_READ_RAW_IR
);
515 case GP2AP020A00F_CMD_READ_RAW_PROXIMITY
:
516 if (data
->cur_opmode
!= GP2AP020A00F_OPMODE_SHUTDOWN
)
518 err
= gp2ap020a00f_set_operation_mode(data
,
519 GP2AP020A00F_OPMODE_READ_RAW_PROXIMITY
);
521 case GP2AP020A00F_CMD_TRIGGER_CLEAR_EN
:
522 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
524 if (!gp2ap020a00f_als_enabled(data
))
525 err
= gp2ap020a00f_alter_opmode(data
,
526 GP2AP020A00F_OPMODE_ALS
,
527 GP2AP020A00F_ADD_MODE
);
528 set_bit(GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
, &data
->flags
);
530 case GP2AP020A00F_CMD_TRIGGER_CLEAR_DIS
:
531 clear_bit(GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
, &data
->flags
);
532 if (gp2ap020a00f_als_enabled(data
))
534 err
= gp2ap020a00f_alter_opmode(data
,
535 GP2AP020A00F_OPMODE_ALS
,
536 GP2AP020A00F_SUBTRACT_MODE
);
538 case GP2AP020A00F_CMD_TRIGGER_IR_EN
:
539 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
541 if (!gp2ap020a00f_als_enabled(data
))
542 err
= gp2ap020a00f_alter_opmode(data
,
543 GP2AP020A00F_OPMODE_ALS
,
544 GP2AP020A00F_ADD_MODE
);
545 set_bit(GP2AP020A00F_FLAG_ALS_IR_TRIGGER
, &data
->flags
);
547 case GP2AP020A00F_CMD_TRIGGER_IR_DIS
:
548 clear_bit(GP2AP020A00F_FLAG_ALS_IR_TRIGGER
, &data
->flags
);
549 if (gp2ap020a00f_als_enabled(data
))
551 err
= gp2ap020a00f_alter_opmode(data
,
552 GP2AP020A00F_OPMODE_ALS
,
553 GP2AP020A00F_SUBTRACT_MODE
);
555 case GP2AP020A00F_CMD_TRIGGER_PROX_EN
:
556 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
558 err
= gp2ap020a00f_alter_opmode(data
,
559 GP2AP020A00F_OPMODE_PS
,
560 GP2AP020A00F_ADD_MODE
);
561 set_bit(GP2AP020A00F_FLAG_PROX_TRIGGER
, &data
->flags
);
563 case GP2AP020A00F_CMD_TRIGGER_PROX_DIS
:
564 clear_bit(GP2AP020A00F_FLAG_PROX_TRIGGER
, &data
->flags
);
565 err
= gp2ap020a00f_alter_opmode(data
,
566 GP2AP020A00F_OPMODE_PS
,
567 GP2AP020A00F_SUBTRACT_MODE
);
569 case GP2AP020A00F_CMD_ALS_HIGH_EV_EN
:
570 if (test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
))
572 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
574 if (!gp2ap020a00f_als_enabled(data
)) {
575 err
= gp2ap020a00f_alter_opmode(data
,
576 GP2AP020A00F_OPMODE_ALS
,
577 GP2AP020A00F_ADD_MODE
);
581 set_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
);
582 err
= gp2ap020a00f_write_event_threshold(data
,
583 GP2AP020A00F_THRESH_TH
, true);
585 case GP2AP020A00F_CMD_ALS_HIGH_EV_DIS
:
586 if (!test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
))
588 clear_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
);
589 if (!gp2ap020a00f_als_enabled(data
)) {
590 err
= gp2ap020a00f_alter_opmode(data
,
591 GP2AP020A00F_OPMODE_ALS
,
592 GP2AP020A00F_SUBTRACT_MODE
);
596 err
= gp2ap020a00f_write_event_threshold(data
,
597 GP2AP020A00F_THRESH_TH
, false);
599 case GP2AP020A00F_CMD_ALS_LOW_EV_EN
:
600 if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
))
602 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
604 if (!gp2ap020a00f_als_enabled(data
)) {
605 err
= gp2ap020a00f_alter_opmode(data
,
606 GP2AP020A00F_OPMODE_ALS
,
607 GP2AP020A00F_ADD_MODE
);
611 set_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
);
612 err
= gp2ap020a00f_write_event_threshold(data
,
613 GP2AP020A00F_THRESH_TL
, true);
615 case GP2AP020A00F_CMD_ALS_LOW_EV_DIS
:
616 if (!test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
))
618 clear_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
);
619 if (!gp2ap020a00f_als_enabled(data
)) {
620 err
= gp2ap020a00f_alter_opmode(data
,
621 GP2AP020A00F_OPMODE_ALS
,
622 GP2AP020A00F_SUBTRACT_MODE
);
626 err
= gp2ap020a00f_write_event_threshold(data
,
627 GP2AP020A00F_THRESH_TL
, false);
629 case GP2AP020A00F_CMD_PROX_HIGH_EV_EN
:
630 if (test_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
))
632 if (gp2ap020a00f_als_enabled(data
) ||
633 data
->cur_opmode
== GP2AP020A00F_OPMODE_PS
)
635 if (!gp2ap020a00f_prox_detect_enabled(data
)) {
636 err
= gp2ap020a00f_set_operation_mode(data
,
637 GP2AP020A00F_OPMODE_PROX_DETECT
);
641 set_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
);
642 err
= gp2ap020a00f_write_event_threshold(data
,
643 GP2AP020A00F_THRESH_PH
, true);
645 case GP2AP020A00F_CMD_PROX_HIGH_EV_DIS
:
646 if (!test_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
))
648 clear_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
);
649 err
= gp2ap020a00f_set_operation_mode(data
,
650 GP2AP020A00F_OPMODE_SHUTDOWN
);
653 err
= gp2ap020a00f_write_event_threshold(data
,
654 GP2AP020A00F_THRESH_PH
, false);
656 case GP2AP020A00F_CMD_PROX_LOW_EV_EN
:
657 if (test_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
))
659 if (gp2ap020a00f_als_enabled(data
) ||
660 data
->cur_opmode
== GP2AP020A00F_OPMODE_PS
)
662 if (!gp2ap020a00f_prox_detect_enabled(data
)) {
663 err
= gp2ap020a00f_set_operation_mode(data
,
664 GP2AP020A00F_OPMODE_PROX_DETECT
);
668 set_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
);
669 err
= gp2ap020a00f_write_event_threshold(data
,
670 GP2AP020A00F_THRESH_PL
, true);
672 case GP2AP020A00F_CMD_PROX_LOW_EV_DIS
:
673 if (!test_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
))
675 clear_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
);
676 err
= gp2ap020a00f_set_operation_mode(data
,
677 GP2AP020A00F_OPMODE_SHUTDOWN
);
680 err
= gp2ap020a00f_write_event_threshold(data
,
681 GP2AP020A00F_THRESH_PL
, false);
688 static int wait_conversion_complete_irq(struct gp2ap020a00f_data
*data
)
692 ret
= wait_event_timeout(data
->data_ready_queue
,
693 test_bit(GP2AP020A00F_FLAG_DATA_READY
,
695 GP2AP020A00F_DATA_READY_TIMEOUT
);
696 clear_bit(GP2AP020A00F_FLAG_DATA_READY
, &data
->flags
);
698 return ret
> 0 ? 0 : -ETIME
;
701 static int gp2ap020a00f_read_output(struct gp2ap020a00f_data
*data
,
702 unsigned int output_reg
, int *val
)
707 err
= wait_conversion_complete_irq(data
);
709 dev_dbg(&data
->client
->dev
, "data ready timeout\n");
711 err
= regmap_bulk_read(data
->regmap
, output_reg
, reg_buf
, 2);
715 *val
= le16_to_cpup((__le16
*)reg_buf
);
720 static bool gp2ap020a00f_adjust_lux_mode(struct gp2ap020a00f_data
*data
,
726 if (!test_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
)) {
727 if (output_val
> 16000) {
728 set_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
);
729 new_range
= GP2AP020A00F_RANGE_A_x128
;
732 if (output_val
< 1000) {
733 clear_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
);
734 new_range
= GP2AP020A00F_RANGE_A_x8
;
738 if (new_range
!= 0xff) {
739 /* Clear als threshold registers to avoid spurious
740 * events caused by lux mode transition.
742 err
= gp2ap020a00f_write_event_threshold(data
,
743 GP2AP020A00F_THRESH_TH
, false);
745 dev_err(&data
->client
->dev
,
746 "Clearing als threshold register failed.\n");
750 err
= gp2ap020a00f_write_event_threshold(data
,
751 GP2AP020A00F_THRESH_TL
, false);
753 dev_err(&data
->client
->dev
,
754 "Clearing als threshold register failed.\n");
758 /* Change lux mode */
759 err
= regmap_update_bits(data
->regmap
,
761 GP2AP020A00F_OP3_MASK
,
762 GP2AP020A00F_OP3_SHUTDOWN
);
765 dev_err(&data
->client
->dev
,
766 "Shutting down the device failed.\n");
770 err
= regmap_update_bits(data
->regmap
,
771 GP2AP020A00F_ALS_REG
,
772 GP2AP020A00F_RANGE_A_MASK
,
776 dev_err(&data
->client
->dev
,
777 "Adjusting device lux mode failed.\n");
781 err
= regmap_update_bits(data
->regmap
,
783 GP2AP020A00F_OP3_MASK
,
784 GP2AP020A00F_OP3_OPERATION
);
787 dev_err(&data
->client
->dev
,
788 "Powering up the device failed.\n");
792 /* Adjust als threshold register values to the new lux mode */
793 if (test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
)) {
794 err
= gp2ap020a00f_write_event_threshold(data
,
795 GP2AP020A00F_THRESH_TH
, true);
797 dev_err(&data
->client
->dev
,
798 "Adjusting als threshold value failed.\n");
803 if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
)) {
804 err
= gp2ap020a00f_write_event_threshold(data
,
805 GP2AP020A00F_THRESH_TL
, true);
807 dev_err(&data
->client
->dev
,
808 "Adjusting als threshold value failed.\n");
819 static void gp2ap020a00f_output_to_lux(struct gp2ap020a00f_data
*data
,
822 if (test_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
))
826 static void gp2ap020a00f_iio_trigger_work(struct irq_work
*work
)
828 struct gp2ap020a00f_data
*data
=
829 container_of(work
, struct gp2ap020a00f_data
, work
);
831 iio_trigger_poll(data
->trig
);
834 static irqreturn_t
gp2ap020a00f_prox_sensing_handler(int irq
, void *data
)
836 struct iio_dev
*indio_dev
= data
;
837 struct gp2ap020a00f_data
*priv
= iio_priv(indio_dev
);
838 unsigned int op_reg_val
;
841 /* Read interrupt flags */
842 ret
= regmap_read(priv
->regmap
, GP2AP020A00F_OP_REG
, &op_reg_val
);
846 if (gp2ap020a00f_prox_detect_enabled(priv
)) {
847 if (op_reg_val
& GP2AP020A00F_PROX_DETECT
) {
848 iio_push_event(indio_dev
,
849 IIO_UNMOD_EVENT_CODE(
851 GP2AP020A00F_SCAN_MODE_PROXIMITY
,
854 iio_get_time_ns(indio_dev
));
856 iio_push_event(indio_dev
,
857 IIO_UNMOD_EVENT_CODE(
859 GP2AP020A00F_SCAN_MODE_PROXIMITY
,
862 iio_get_time_ns(indio_dev
));
869 static irqreturn_t
gp2ap020a00f_thresh_event_handler(int irq
, void *data
)
871 struct iio_dev
*indio_dev
= data
;
872 struct gp2ap020a00f_data
*priv
= iio_priv(indio_dev
);
873 u8 op_reg_flags
, d0_reg_buf
[2];
874 unsigned int output_val
, op_reg_val
;
875 int thresh_val_id
, ret
;
877 /* Read interrupt flags */
878 ret
= regmap_read(priv
->regmap
, GP2AP020A00F_OP_REG
,
883 op_reg_flags
= op_reg_val
& (GP2AP020A00F_FLAG_A
| GP2AP020A00F_FLAG_P
884 | GP2AP020A00F_PROX_DETECT
);
886 op_reg_val
&= (~GP2AP020A00F_FLAG_A
& ~GP2AP020A00F_FLAG_P
887 & ~GP2AP020A00F_PROX_DETECT
);
889 /* Clear interrupt flags (if not in INTTYPE_PULSE mode) */
890 if (priv
->cur_opmode
!= GP2AP020A00F_OPMODE_PROX_DETECT
) {
891 ret
= regmap_write(priv
->regmap
, GP2AP020A00F_OP_REG
,
897 if (op_reg_flags
& GP2AP020A00F_FLAG_A
) {
898 /* Check D0 register to assess if the lux mode
899 * transition is required.
901 ret
= regmap_bulk_read(priv
->regmap
, GP2AP020A00F_D0_L_REG
,
906 output_val
= le16_to_cpup((__le16
*)d0_reg_buf
);
908 if (gp2ap020a00f_adjust_lux_mode(priv
, output_val
))
911 gp2ap020a00f_output_to_lux(priv
, &output_val
);
914 * We need to check output value to distinguish
915 * between high and low ambient light threshold event.
917 if (test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &priv
->flags
)) {
919 GP2AP020A00F_THRESH_VAL_ID(GP2AP020A00F_TH_L_REG
);
920 if (output_val
> priv
->thresh_val
[thresh_val_id
])
921 iio_push_event(indio_dev
,
924 GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
,
928 iio_get_time_ns(indio_dev
));
931 if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &priv
->flags
)) {
933 GP2AP020A00F_THRESH_VAL_ID(GP2AP020A00F_TL_L_REG
);
934 if (output_val
< priv
->thresh_val
[thresh_val_id
])
935 iio_push_event(indio_dev
,
938 GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
,
942 iio_get_time_ns(indio_dev
));
946 if (priv
->cur_opmode
== GP2AP020A00F_OPMODE_READ_RAW_CLEAR
||
947 priv
->cur_opmode
== GP2AP020A00F_OPMODE_READ_RAW_IR
||
948 priv
->cur_opmode
== GP2AP020A00F_OPMODE_READ_RAW_PROXIMITY
) {
949 set_bit(GP2AP020A00F_FLAG_DATA_READY
, &priv
->flags
);
950 wake_up(&priv
->data_ready_queue
);
954 if (test_bit(GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
, &priv
->flags
) ||
955 test_bit(GP2AP020A00F_FLAG_ALS_IR_TRIGGER
, &priv
->flags
) ||
956 test_bit(GP2AP020A00F_FLAG_PROX_TRIGGER
, &priv
->flags
))
957 /* This fires off the trigger. */
958 irq_work_queue(&priv
->work
);
964 static irqreturn_t
gp2ap020a00f_trigger_handler(int irq
, void *data
)
966 struct iio_poll_func
*pf
= data
;
967 struct iio_dev
*indio_dev
= pf
->indio_dev
;
968 struct gp2ap020a00f_data
*priv
= iio_priv(indio_dev
);
972 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
973 indio_dev
->masklength
) {
974 ret
= regmap_bulk_read(priv
->regmap
,
975 GP2AP020A00F_DATA_REG(i
),
976 &priv
->buffer
[d_size
], 2);
980 if (i
== GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
||
981 i
== GP2AP020A00F_SCAN_MODE_LIGHT_IR
) {
982 out_val
= le16_to_cpup((__le16
*)&priv
->buffer
[d_size
]);
983 gp2ap020a00f_output_to_lux(priv
, &out_val
);
985 put_unaligned_le32(out_val
, &priv
->buffer
[d_size
]);
992 iio_push_to_buffers_with_timestamp(indio_dev
, priv
->buffer
,
995 iio_trigger_notify_done(indio_dev
->trig
);
1000 static u8
gp2ap020a00f_get_thresh_reg(const struct iio_chan_spec
*chan
,
1001 enum iio_event_direction event_dir
)
1003 switch (chan
->type
) {
1005 if (event_dir
== IIO_EV_DIR_RISING
)
1006 return GP2AP020A00F_PH_L_REG
;
1008 return GP2AP020A00F_PL_L_REG
;
1010 if (event_dir
== IIO_EV_DIR_RISING
)
1011 return GP2AP020A00F_TH_L_REG
;
1013 return GP2AP020A00F_TL_L_REG
;
1021 static int gp2ap020a00f_write_event_val(struct iio_dev
*indio_dev
,
1022 const struct iio_chan_spec
*chan
,
1023 enum iio_event_type type
,
1024 enum iio_event_direction dir
,
1025 enum iio_event_info info
,
1028 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1029 bool event_en
= false;
1034 mutex_lock(&data
->lock
);
1036 thresh_reg_l
= gp2ap020a00f_get_thresh_reg(chan
, dir
);
1037 thresh_val_id
= GP2AP020A00F_THRESH_VAL_ID(thresh_reg_l
);
1039 if (thresh_val_id
> GP2AP020A00F_THRESH_PH
) {
1044 switch (thresh_reg_l
) {
1045 case GP2AP020A00F_TH_L_REG
:
1046 event_en
= test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
,
1049 case GP2AP020A00F_TL_L_REG
:
1050 event_en
= test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
,
1053 case GP2AP020A00F_PH_L_REG
:
1058 event_en
= test_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
,
1061 case GP2AP020A00F_PL_L_REG
:
1066 event_en
= test_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
,
1071 data
->thresh_val
[thresh_val_id
] = val
;
1072 err
= gp2ap020a00f_write_event_threshold(data
, thresh_val_id
,
1075 mutex_unlock(&data
->lock
);
1080 static int gp2ap020a00f_read_event_val(struct iio_dev
*indio_dev
,
1081 const struct iio_chan_spec
*chan
,
1082 enum iio_event_type type
,
1083 enum iio_event_direction dir
,
1084 enum iio_event_info info
,
1085 int *val
, int *val2
)
1087 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1089 int err
= IIO_VAL_INT
;
1091 mutex_lock(&data
->lock
);
1093 thresh_reg_l
= gp2ap020a00f_get_thresh_reg(chan
, dir
);
1095 if (thresh_reg_l
> GP2AP020A00F_PH_L_REG
) {
1100 *val
= data
->thresh_val
[GP2AP020A00F_THRESH_VAL_ID(thresh_reg_l
)];
1103 mutex_unlock(&data
->lock
);
1108 static int gp2ap020a00f_write_prox_event_config(struct iio_dev
*indio_dev
,
1111 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1112 enum gp2ap020a00f_cmd cmd_high_ev
, cmd_low_ev
;
1115 cmd_high_ev
= state
? GP2AP020A00F_CMD_PROX_HIGH_EV_EN
:
1116 GP2AP020A00F_CMD_PROX_HIGH_EV_DIS
;
1117 cmd_low_ev
= state
? GP2AP020A00F_CMD_PROX_LOW_EV_EN
:
1118 GP2AP020A00F_CMD_PROX_LOW_EV_DIS
;
1121 * In order to enable proximity detection feature in the device
1122 * both high and low threshold registers have to be written
1123 * with different values, greater than zero.
1126 if (data
->thresh_val
[GP2AP020A00F_THRESH_PL
] == 0)
1129 if (data
->thresh_val
[GP2AP020A00F_THRESH_PH
] == 0)
1133 err
= gp2ap020a00f_exec_cmd(data
, cmd_high_ev
);
1137 err
= gp2ap020a00f_exec_cmd(data
, cmd_low_ev
);
1141 free_irq(data
->client
->irq
, indio_dev
);
1144 err
= request_threaded_irq(data
->client
->irq
, NULL
,
1145 &gp2ap020a00f_prox_sensing_handler
,
1146 IRQF_TRIGGER_RISING
|
1147 IRQF_TRIGGER_FALLING
|
1149 "gp2ap020a00f_prox_sensing",
1152 err
= request_threaded_irq(data
->client
->irq
, NULL
,
1153 &gp2ap020a00f_thresh_event_handler
,
1154 IRQF_TRIGGER_FALLING
|
1156 "gp2ap020a00f_thresh_event",
1163 static int gp2ap020a00f_write_event_config(struct iio_dev
*indio_dev
,
1164 const struct iio_chan_spec
*chan
,
1165 enum iio_event_type type
,
1166 enum iio_event_direction dir
,
1169 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1170 enum gp2ap020a00f_cmd cmd
;
1173 mutex_lock(&data
->lock
);
1175 switch (chan
->type
) {
1177 err
= gp2ap020a00f_write_prox_event_config(indio_dev
, state
);
1180 if (dir
== IIO_EV_DIR_RISING
) {
1181 cmd
= state
? GP2AP020A00F_CMD_ALS_HIGH_EV_EN
:
1182 GP2AP020A00F_CMD_ALS_HIGH_EV_DIS
;
1183 err
= gp2ap020a00f_exec_cmd(data
, cmd
);
1185 cmd
= state
? GP2AP020A00F_CMD_ALS_LOW_EV_EN
:
1186 GP2AP020A00F_CMD_ALS_LOW_EV_DIS
;
1187 err
= gp2ap020a00f_exec_cmd(data
, cmd
);
1194 mutex_unlock(&data
->lock
);
1199 static int gp2ap020a00f_read_event_config(struct iio_dev
*indio_dev
,
1200 const struct iio_chan_spec
*chan
,
1201 enum iio_event_type type
,
1202 enum iio_event_direction dir
)
1204 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1207 mutex_lock(&data
->lock
);
1209 switch (chan
->type
) {
1211 if (dir
== IIO_EV_DIR_RISING
)
1212 event_en
= test_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
,
1215 event_en
= test_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
,
1219 if (dir
== IIO_EV_DIR_RISING
)
1220 event_en
= test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
,
1223 event_en
= test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
,
1231 mutex_unlock(&data
->lock
);
1236 static int gp2ap020a00f_read_channel(struct gp2ap020a00f_data
*data
,
1237 struct iio_chan_spec
const *chan
, int *val
)
1239 enum gp2ap020a00f_cmd cmd
;
1242 switch (chan
->scan_index
) {
1243 case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
:
1244 cmd
= GP2AP020A00F_CMD_READ_RAW_CLEAR
;
1246 case GP2AP020A00F_SCAN_MODE_LIGHT_IR
:
1247 cmd
= GP2AP020A00F_CMD_READ_RAW_IR
;
1249 case GP2AP020A00F_SCAN_MODE_PROXIMITY
:
1250 cmd
= GP2AP020A00F_CMD_READ_RAW_PROXIMITY
;
1256 err
= gp2ap020a00f_exec_cmd(data
, cmd
);
1258 dev_err(&data
->client
->dev
,
1259 "gp2ap020a00f_exec_cmd failed\n");
1263 err
= gp2ap020a00f_read_output(data
, chan
->address
, val
);
1265 dev_err(&data
->client
->dev
,
1266 "gp2ap020a00f_read_output failed\n");
1268 err
= gp2ap020a00f_set_operation_mode(data
,
1269 GP2AP020A00F_OPMODE_SHUTDOWN
);
1271 dev_err(&data
->client
->dev
,
1272 "Failed to shut down the device.\n");
1274 if (cmd
== GP2AP020A00F_CMD_READ_RAW_CLEAR
||
1275 cmd
== GP2AP020A00F_CMD_READ_RAW_IR
)
1276 gp2ap020a00f_output_to_lux(data
, val
);
1282 static int gp2ap020a00f_read_raw(struct iio_dev
*indio_dev
,
1283 struct iio_chan_spec
const *chan
,
1284 int *val
, int *val2
,
1287 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1290 if (mask
== IIO_CHAN_INFO_RAW
) {
1291 err
= iio_device_claim_direct_mode(indio_dev
);
1295 err
= gp2ap020a00f_read_channel(data
, chan
, val
);
1296 iio_device_release_direct_mode(indio_dev
);
1298 return err
< 0 ? err
: IIO_VAL_INT
;
1301 static const struct iio_event_spec gp2ap020a00f_event_spec_light
[] = {
1303 .type
= IIO_EV_TYPE_THRESH
,
1304 .dir
= IIO_EV_DIR_RISING
,
1305 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
1306 BIT(IIO_EV_INFO_ENABLE
),
1308 .type
= IIO_EV_TYPE_THRESH
,
1309 .dir
= IIO_EV_DIR_FALLING
,
1310 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
1311 BIT(IIO_EV_INFO_ENABLE
),
1315 static const struct iio_event_spec gp2ap020a00f_event_spec_prox
[] = {
1317 .type
= IIO_EV_TYPE_ROC
,
1318 .dir
= IIO_EV_DIR_RISING
,
1319 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
1320 BIT(IIO_EV_INFO_ENABLE
),
1322 .type
= IIO_EV_TYPE_ROC
,
1323 .dir
= IIO_EV_DIR_FALLING
,
1324 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
1325 BIT(IIO_EV_INFO_ENABLE
),
1329 static const struct iio_chan_spec gp2ap020a00f_channels
[] = {
1332 .channel2
= IIO_MOD_LIGHT_CLEAR
,
1334 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1340 .endianness
= IIO_LE
,
1342 .scan_index
= GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
,
1343 .address
= GP2AP020A00F_D0_L_REG
,
1344 .event_spec
= gp2ap020a00f_event_spec_light
,
1345 .num_event_specs
= ARRAY_SIZE(gp2ap020a00f_event_spec_light
),
1349 .channel2
= IIO_MOD_LIGHT_IR
,
1351 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1357 .endianness
= IIO_LE
,
1359 .scan_index
= GP2AP020A00F_SCAN_MODE_LIGHT_IR
,
1360 .address
= GP2AP020A00F_D1_L_REG
,
1363 .type
= IIO_PROXIMITY
,
1365 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1371 .endianness
= IIO_LE
,
1373 .scan_index
= GP2AP020A00F_SCAN_MODE_PROXIMITY
,
1374 .address
= GP2AP020A00F_D2_L_REG
,
1375 .event_spec
= gp2ap020a00f_event_spec_prox
,
1376 .num_event_specs
= ARRAY_SIZE(gp2ap020a00f_event_spec_prox
),
1378 IIO_CHAN_SOFT_TIMESTAMP(GP2AP020A00F_CHAN_TIMESTAMP
),
1381 static const struct iio_info gp2ap020a00f_info
= {
1382 .read_raw
= &gp2ap020a00f_read_raw
,
1383 .read_event_value
= &gp2ap020a00f_read_event_val
,
1384 .read_event_config
= &gp2ap020a00f_read_event_config
,
1385 .write_event_value
= &gp2ap020a00f_write_event_val
,
1386 .write_event_config
= &gp2ap020a00f_write_event_config
,
1387 .driver_module
= THIS_MODULE
,
1390 static int gp2ap020a00f_buffer_postenable(struct iio_dev
*indio_dev
)
1392 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1395 mutex_lock(&data
->lock
);
1398 * Enable triggers according to the scan_mask. Enabling either
1399 * LIGHT_CLEAR or LIGHT_IR scan mode results in enabling ALS
1400 * module in the device, which generates samples in both D0 (clear)
1401 * and D1 (ir) registers. As the two registers are bound to the
1402 * two separate IIO channels they are treated in the driver logic
1403 * as if they were controlled independently.
1405 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
1406 indio_dev
->masklength
) {
1408 case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
:
1409 err
= gp2ap020a00f_exec_cmd(data
,
1410 GP2AP020A00F_CMD_TRIGGER_CLEAR_EN
);
1412 case GP2AP020A00F_SCAN_MODE_LIGHT_IR
:
1413 err
= gp2ap020a00f_exec_cmd(data
,
1414 GP2AP020A00F_CMD_TRIGGER_IR_EN
);
1416 case GP2AP020A00F_SCAN_MODE_PROXIMITY
:
1417 err
= gp2ap020a00f_exec_cmd(data
,
1418 GP2AP020A00F_CMD_TRIGGER_PROX_EN
);
1426 data
->buffer
= kmalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
1427 if (!data
->buffer
) {
1432 err
= iio_triggered_buffer_postenable(indio_dev
);
1435 mutex_unlock(&data
->lock
);
1440 static int gp2ap020a00f_buffer_predisable(struct iio_dev
*indio_dev
)
1442 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1445 mutex_lock(&data
->lock
);
1447 err
= iio_triggered_buffer_predisable(indio_dev
);
1451 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
1452 indio_dev
->masklength
) {
1454 case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
:
1455 err
= gp2ap020a00f_exec_cmd(data
,
1456 GP2AP020A00F_CMD_TRIGGER_CLEAR_DIS
);
1458 case GP2AP020A00F_SCAN_MODE_LIGHT_IR
:
1459 err
= gp2ap020a00f_exec_cmd(data
,
1460 GP2AP020A00F_CMD_TRIGGER_IR_DIS
);
1462 case GP2AP020A00F_SCAN_MODE_PROXIMITY
:
1463 err
= gp2ap020a00f_exec_cmd(data
,
1464 GP2AP020A00F_CMD_TRIGGER_PROX_DIS
);
1470 kfree(data
->buffer
);
1473 mutex_unlock(&data
->lock
);
1478 static const struct iio_buffer_setup_ops gp2ap020a00f_buffer_setup_ops
= {
1479 .postenable
= &gp2ap020a00f_buffer_postenable
,
1480 .predisable
= &gp2ap020a00f_buffer_predisable
,
1483 static const struct iio_trigger_ops gp2ap020a00f_trigger_ops
= {
1484 .owner
= THIS_MODULE
,
1487 static int gp2ap020a00f_probe(struct i2c_client
*client
,
1488 const struct i2c_device_id
*id
)
1490 struct gp2ap020a00f_data
*data
;
1491 struct iio_dev
*indio_dev
;
1492 struct regmap
*regmap
;
1495 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1499 data
= iio_priv(indio_dev
);
1501 data
->vled_reg
= devm_regulator_get(&client
->dev
, "vled");
1502 if (IS_ERR(data
->vled_reg
))
1503 return PTR_ERR(data
->vled_reg
);
1505 err
= regulator_enable(data
->vled_reg
);
1509 regmap
= devm_regmap_init_i2c(client
, &gp2ap020a00f_regmap_config
);
1510 if (IS_ERR(regmap
)) {
1511 dev_err(&client
->dev
, "Regmap initialization failed.\n");
1512 err
= PTR_ERR(regmap
);
1513 goto error_regulator_disable
;
1516 /* Initialize device registers */
1517 err
= regmap_bulk_write(regmap
, GP2AP020A00F_OP_REG
,
1518 gp2ap020a00f_reg_init_tab
,
1519 ARRAY_SIZE(gp2ap020a00f_reg_init_tab
));
1522 dev_err(&client
->dev
, "Device initialization failed.\n");
1523 goto error_regulator_disable
;
1526 i2c_set_clientdata(client
, indio_dev
);
1528 data
->client
= client
;
1529 data
->cur_opmode
= GP2AP020A00F_OPMODE_SHUTDOWN
;
1530 data
->regmap
= regmap
;
1531 init_waitqueue_head(&data
->data_ready_queue
);
1533 mutex_init(&data
->lock
);
1534 indio_dev
->dev
.parent
= &client
->dev
;
1535 indio_dev
->channels
= gp2ap020a00f_channels
;
1536 indio_dev
->num_channels
= ARRAY_SIZE(gp2ap020a00f_channels
);
1537 indio_dev
->info
= &gp2ap020a00f_info
;
1538 indio_dev
->name
= id
->name
;
1539 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1541 /* Allocate buffer */
1542 err
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
1543 &gp2ap020a00f_trigger_handler
, &gp2ap020a00f_buffer_setup_ops
);
1545 goto error_regulator_disable
;
1547 /* Allocate trigger */
1548 data
->trig
= devm_iio_trigger_alloc(&client
->dev
, "%s-trigger",
1550 if (data
->trig
== NULL
) {
1552 dev_err(&indio_dev
->dev
, "Failed to allocate iio trigger.\n");
1553 goto error_uninit_buffer
;
1556 /* This needs to be requested here for read_raw calls to work. */
1557 err
= request_threaded_irq(client
->irq
, NULL
,
1558 &gp2ap020a00f_thresh_event_handler
,
1559 IRQF_TRIGGER_FALLING
|
1561 "gp2ap020a00f_als_event",
1564 dev_err(&client
->dev
, "Irq request failed.\n");
1565 goto error_uninit_buffer
;
1568 data
->trig
->ops
= &gp2ap020a00f_trigger_ops
;
1569 data
->trig
->dev
.parent
= &data
->client
->dev
;
1571 init_irq_work(&data
->work
, gp2ap020a00f_iio_trigger_work
);
1573 err
= iio_trigger_register(data
->trig
);
1575 dev_err(&client
->dev
, "Failed to register iio trigger.\n");
1576 goto error_free_irq
;
1579 err
= iio_device_register(indio_dev
);
1581 goto error_trigger_unregister
;
1585 error_trigger_unregister
:
1586 iio_trigger_unregister(data
->trig
);
1588 free_irq(client
->irq
, indio_dev
);
1589 error_uninit_buffer
:
1590 iio_triggered_buffer_cleanup(indio_dev
);
1591 error_regulator_disable
:
1592 regulator_disable(data
->vled_reg
);
1597 static int gp2ap020a00f_remove(struct i2c_client
*client
)
1599 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1600 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1603 err
= gp2ap020a00f_set_operation_mode(data
,
1604 GP2AP020A00F_OPMODE_SHUTDOWN
);
1606 dev_err(&indio_dev
->dev
, "Failed to power off the device.\n");
1608 iio_device_unregister(indio_dev
);
1609 iio_trigger_unregister(data
->trig
);
1610 free_irq(client
->irq
, indio_dev
);
1611 iio_triggered_buffer_cleanup(indio_dev
);
1612 regulator_disable(data
->vled_reg
);
1617 static const struct i2c_device_id gp2ap020a00f_id
[] = {
1618 { GP2A_I2C_NAME
, 0 },
1622 MODULE_DEVICE_TABLE(i2c
, gp2ap020a00f_id
);
1625 static const struct of_device_id gp2ap020a00f_of_match
[] = {
1626 { .compatible
= "sharp,gp2ap020a00f" },
1629 MODULE_DEVICE_TABLE(of
, gp2ap020a00f_of_match
);
1632 static struct i2c_driver gp2ap020a00f_driver
= {
1634 .name
= GP2A_I2C_NAME
,
1635 .of_match_table
= of_match_ptr(gp2ap020a00f_of_match
),
1637 .probe
= gp2ap020a00f_probe
,
1638 .remove
= gp2ap020a00f_remove
,
1639 .id_table
= gp2ap020a00f_id
,
1642 module_i2c_driver(gp2ap020a00f_driver
);
1644 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1645 MODULE_DESCRIPTION("Sharp GP2AP020A00F Proximity/ALS sensor driver");
1646 MODULE_LICENSE("GPL v2");