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 * - illiminance_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 <linux/iio/buffer.h>
50 #include <linux/iio/events.h>
51 #include <linux/iio/iio.h>
52 #include <linux/iio/sysfs.h>
53 #include <linux/iio/trigger.h>
54 #include <linux/iio/trigger_consumer.h>
55 #include <linux/iio/triggered_buffer.h>
57 #define GP2A_I2C_NAME "gp2ap020a00f"
60 #define GP2AP020A00F_OP_REG 0x00 /* Basic operations */
61 #define GP2AP020A00F_ALS_REG 0x01 /* ALS related settings */
62 #define GP2AP020A00F_PS_REG 0x02 /* PS related settings */
63 #define GP2AP020A00F_LED_REG 0x03 /* LED reg */
64 #define GP2AP020A00F_TL_L_REG 0x04 /* ALS: Threshold low LSB */
65 #define GP2AP020A00F_TL_H_REG 0x05 /* ALS: Threshold low MSB */
66 #define GP2AP020A00F_TH_L_REG 0x06 /* ALS: Threshold high LSB */
67 #define GP2AP020A00F_TH_H_REG 0x07 /* ALS: Threshold high MSB */
68 #define GP2AP020A00F_PL_L_REG 0x08 /* PS: Threshold low LSB */
69 #define GP2AP020A00F_PL_H_REG 0x09 /* PS: Threshold low MSB */
70 #define GP2AP020A00F_PH_L_REG 0x0a /* PS: Threshold high LSB */
71 #define GP2AP020A00F_PH_H_REG 0x0b /* PS: Threshold high MSB */
72 #define GP2AP020A00F_D0_L_REG 0x0c /* ALS result: Clear/Illuminance LSB */
73 #define GP2AP020A00F_D0_H_REG 0x0d /* ALS result: Clear/Illuminance MSB */
74 #define GP2AP020A00F_D1_L_REG 0x0e /* ALS result: IR LSB */
75 #define GP2AP020A00F_D1_H_REG 0x0f /* ALS result: IR LSB */
76 #define GP2AP020A00F_D2_L_REG 0x10 /* PS result LSB */
77 #define GP2AP020A00F_D2_H_REG 0x11 /* PS result MSB */
78 #define GP2AP020A00F_NUM_REGS 0x12 /* Number of registers */
81 #define GP2AP020A00F_OP3_MASK 0x80 /* Software shutdown */
82 #define GP2AP020A00F_OP3_SHUTDOWN 0x00
83 #define GP2AP020A00F_OP3_OPERATION 0x80
84 #define GP2AP020A00F_OP2_MASK 0x40 /* Auto shutdown/Continuous mode */
85 #define GP2AP020A00F_OP2_AUTO_SHUTDOWN 0x00
86 #define GP2AP020A00F_OP2_CONT_OPERATION 0x40
87 #define GP2AP020A00F_OP_MASK 0x30 /* Operating mode selection */
88 #define GP2AP020A00F_OP_ALS_AND_PS 0x00
89 #define GP2AP020A00F_OP_ALS 0x10
90 #define GP2AP020A00F_OP_PS 0x20
91 #define GP2AP020A00F_OP_DEBUG 0x30
92 #define GP2AP020A00F_PROX_MASK 0x08 /* PS: detection/non-detection */
93 #define GP2AP020A00F_PROX_NON_DETECT 0x00
94 #define GP2AP020A00F_PROX_DETECT 0x08
95 #define GP2AP020A00F_FLAG_P 0x04 /* PS: interrupt result */
96 #define GP2AP020A00F_FLAG_A 0x02 /* ALS: interrupt result */
97 #define GP2AP020A00F_TYPE_MASK 0x01 /* Output data type selection */
98 #define GP2AP020A00F_TYPE_MANUAL_CALC 0x00
99 #define GP2AP020A00F_TYPE_AUTO_CALC 0x01
102 #define GP2AP020A00F_PRST_MASK 0xc0 /* Number of measurement cycles */
103 #define GP2AP020A00F_PRST_ONCE 0x00
104 #define GP2AP020A00F_PRST_4_CYCLES 0x40
105 #define GP2AP020A00F_PRST_8_CYCLES 0x80
106 #define GP2AP020A00F_PRST_16_CYCLES 0xc0
107 #define GP2AP020A00F_RES_A_MASK 0x38 /* ALS: Resolution */
108 #define GP2AP020A00F_RES_A_800ms 0x00
109 #define GP2AP020A00F_RES_A_400ms 0x08
110 #define GP2AP020A00F_RES_A_200ms 0x10
111 #define GP2AP020A00F_RES_A_100ms 0x18
112 #define GP2AP020A00F_RES_A_25ms 0x20
113 #define GP2AP020A00F_RES_A_6_25ms 0x28
114 #define GP2AP020A00F_RES_A_1_56ms 0x30
115 #define GP2AP020A00F_RES_A_0_39ms 0x38
116 #define GP2AP020A00F_RANGE_A_MASK 0x07 /* ALS: Max measurable range */
117 #define GP2AP020A00F_RANGE_A_x1 0x00
118 #define GP2AP020A00F_RANGE_A_x2 0x01
119 #define GP2AP020A00F_RANGE_A_x4 0x02
120 #define GP2AP020A00F_RANGE_A_x8 0x03
121 #define GP2AP020A00F_RANGE_A_x16 0x04
122 #define GP2AP020A00F_RANGE_A_x32 0x05
123 #define GP2AP020A00F_RANGE_A_x64 0x06
124 #define GP2AP020A00F_RANGE_A_x128 0x07
127 #define GP2AP020A00F_ALC_MASK 0x80 /* Auto light cancel */
128 #define GP2AP020A00F_ALC_ON 0x80
129 #define GP2AP020A00F_ALC_OFF 0x00
130 #define GP2AP020A00F_INTTYPE_MASK 0x40 /* Interrupt type setting */
131 #define GP2AP020A00F_INTTYPE_LEVEL 0x00
132 #define GP2AP020A00F_INTTYPE_PULSE 0x40
133 #define GP2AP020A00F_RES_P_MASK 0x38 /* PS: Resolution */
134 #define GP2AP020A00F_RES_P_800ms_x2 0x00
135 #define GP2AP020A00F_RES_P_400ms_x2 0x08
136 #define GP2AP020A00F_RES_P_200ms_x2 0x10
137 #define GP2AP020A00F_RES_P_100ms_x2 0x18
138 #define GP2AP020A00F_RES_P_25ms_x2 0x20
139 #define GP2AP020A00F_RES_P_6_25ms_x2 0x28
140 #define GP2AP020A00F_RES_P_1_56ms_x2 0x30
141 #define GP2AP020A00F_RES_P_0_39ms_x2 0x38
142 #define GP2AP020A00F_RANGE_P_MASK 0x07 /* PS: Max measurable range */
143 #define GP2AP020A00F_RANGE_P_x1 0x00
144 #define GP2AP020A00F_RANGE_P_x2 0x01
145 #define GP2AP020A00F_RANGE_P_x4 0x02
146 #define GP2AP020A00F_RANGE_P_x8 0x03
147 #define GP2AP020A00F_RANGE_P_x16 0x04
148 #define GP2AP020A00F_RANGE_P_x32 0x05
149 #define GP2AP020A00F_RANGE_P_x64 0x06
150 #define GP2AP020A00F_RANGE_P_x128 0x07
153 #define GP2AP020A00F_INTVAL_MASK 0xc0 /* Intermittent operating */
154 #define GP2AP020A00F_INTVAL_0 0x00
155 #define GP2AP020A00F_INTVAL_4 0x40
156 #define GP2AP020A00F_INTVAL_8 0x80
157 #define GP2AP020A00F_INTVAL_16 0xc0
158 #define GP2AP020A00F_IS_MASK 0x30 /* ILED drive peak current */
159 #define GP2AP020A00F_IS_13_8mA 0x00
160 #define GP2AP020A00F_IS_27_5mA 0x10
161 #define GP2AP020A00F_IS_55mA 0x20
162 #define GP2AP020A00F_IS_110mA 0x30
163 #define GP2AP020A00F_PIN_MASK 0x0c /* INT terminal setting */
164 #define GP2AP020A00F_PIN_ALS_OR_PS 0x00
165 #define GP2AP020A00F_PIN_ALS 0x04
166 #define GP2AP020A00F_PIN_PS 0x08
167 #define GP2AP020A00F_PIN_PS_DETECT 0x0c
168 #define GP2AP020A00F_FREQ_MASK 0x02 /* LED modulation frequency */
169 #define GP2AP020A00F_FREQ_327_5kHz 0x00
170 #define GP2AP020A00F_FREQ_81_8kHz 0x02
171 #define GP2AP020A00F_RST 0x01 /* Software reset */
173 #define GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR 0
174 #define GP2AP020A00F_SCAN_MODE_LIGHT_IR 1
175 #define GP2AP020A00F_SCAN_MODE_PROXIMITY 2
176 #define GP2AP020A00F_CHAN_TIMESTAMP 3
178 #define GP2AP020A00F_DATA_READY_TIMEOUT msecs_to_jiffies(1000)
179 #define GP2AP020A00F_DATA_REG(chan) (GP2AP020A00F_D0_L_REG + \
181 #define GP2AP020A00F_THRESH_REG(th_val_id) (GP2AP020A00F_TL_L_REG + \
183 #define GP2AP020A00F_THRESH_VAL_ID(reg_addr) ((reg_addr - 4) / 2)
185 #define GP2AP020A00F_SUBTRACT_MODE 0
186 #define GP2AP020A00F_ADD_MODE 1
188 #define GP2AP020A00F_MAX_CHANNELS 3
190 enum gp2ap020a00f_opmode
{
191 GP2AP020A00F_OPMODE_READ_RAW_CLEAR
,
192 GP2AP020A00F_OPMODE_READ_RAW_IR
,
193 GP2AP020A00F_OPMODE_READ_RAW_PROXIMITY
,
194 GP2AP020A00F_OPMODE_ALS
,
195 GP2AP020A00F_OPMODE_PS
,
196 GP2AP020A00F_OPMODE_ALS_AND_PS
,
197 GP2AP020A00F_OPMODE_PROX_DETECT
,
198 GP2AP020A00F_OPMODE_SHUTDOWN
,
199 GP2AP020A00F_NUM_OPMODES
,
202 enum gp2ap020a00f_cmd
{
203 GP2AP020A00F_CMD_READ_RAW_CLEAR
,
204 GP2AP020A00F_CMD_READ_RAW_IR
,
205 GP2AP020A00F_CMD_READ_RAW_PROXIMITY
,
206 GP2AP020A00F_CMD_TRIGGER_CLEAR_EN
,
207 GP2AP020A00F_CMD_TRIGGER_CLEAR_DIS
,
208 GP2AP020A00F_CMD_TRIGGER_IR_EN
,
209 GP2AP020A00F_CMD_TRIGGER_IR_DIS
,
210 GP2AP020A00F_CMD_TRIGGER_PROX_EN
,
211 GP2AP020A00F_CMD_TRIGGER_PROX_DIS
,
212 GP2AP020A00F_CMD_ALS_HIGH_EV_EN
,
213 GP2AP020A00F_CMD_ALS_HIGH_EV_DIS
,
214 GP2AP020A00F_CMD_ALS_LOW_EV_EN
,
215 GP2AP020A00F_CMD_ALS_LOW_EV_DIS
,
216 GP2AP020A00F_CMD_PROX_HIGH_EV_EN
,
217 GP2AP020A00F_CMD_PROX_HIGH_EV_DIS
,
218 GP2AP020A00F_CMD_PROX_LOW_EV_EN
,
219 GP2AP020A00F_CMD_PROX_LOW_EV_DIS
,
222 enum gp2ap020a00f_flags
{
223 GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
,
224 GP2AP020A00F_FLAG_ALS_IR_TRIGGER
,
225 GP2AP020A00F_FLAG_PROX_TRIGGER
,
226 GP2AP020A00F_FLAG_PROX_RISING_EV
,
227 GP2AP020A00F_FLAG_PROX_FALLING_EV
,
228 GP2AP020A00F_FLAG_ALS_RISING_EV
,
229 GP2AP020A00F_FLAG_ALS_FALLING_EV
,
230 GP2AP020A00F_FLAG_LUX_MODE_HI
,
231 GP2AP020A00F_FLAG_DATA_READY
,
234 enum gp2ap020a00f_thresh_val_id
{
235 GP2AP020A00F_THRESH_TL
,
236 GP2AP020A00F_THRESH_TH
,
237 GP2AP020A00F_THRESH_PL
,
238 GP2AP020A00F_THRESH_PH
,
241 struct gp2ap020a00f_data
{
242 const struct gp2ap020a00f_platform_data
*pdata
;
243 struct i2c_client
*client
;
246 struct regulator
*vled_reg
;
248 enum gp2ap020a00f_opmode cur_opmode
;
249 struct iio_trigger
*trig
;
250 struct regmap
*regmap
;
251 unsigned int thresh_val
[4];
253 struct irq_work work
;
254 wait_queue_head_t data_ready_queue
;
257 static const u8 gp2ap020a00f_reg_init_tab
[] = {
258 [GP2AP020A00F_OP_REG
] = GP2AP020A00F_OP3_SHUTDOWN
,
259 [GP2AP020A00F_ALS_REG
] = GP2AP020A00F_RES_A_25ms
|
260 GP2AP020A00F_RANGE_A_x8
,
261 [GP2AP020A00F_PS_REG
] = GP2AP020A00F_ALC_ON
|
262 GP2AP020A00F_RES_P_1_56ms_x2
|
263 GP2AP020A00F_RANGE_P_x4
,
264 [GP2AP020A00F_LED_REG
] = GP2AP020A00F_INTVAL_0
|
265 GP2AP020A00F_IS_110mA
|
266 GP2AP020A00F_FREQ_327_5kHz
,
267 [GP2AP020A00F_TL_L_REG
] = 0,
268 [GP2AP020A00F_TL_H_REG
] = 0,
269 [GP2AP020A00F_TH_L_REG
] = 0,
270 [GP2AP020A00F_TH_H_REG
] = 0,
271 [GP2AP020A00F_PL_L_REG
] = 0,
272 [GP2AP020A00F_PL_H_REG
] = 0,
273 [GP2AP020A00F_PH_L_REG
] = 0,
274 [GP2AP020A00F_PH_H_REG
] = 0,
277 static bool gp2ap020a00f_is_volatile_reg(struct device
*dev
, unsigned int reg
)
280 case GP2AP020A00F_OP_REG
:
281 case GP2AP020A00F_D0_L_REG
:
282 case GP2AP020A00F_D0_H_REG
:
283 case GP2AP020A00F_D1_L_REG
:
284 case GP2AP020A00F_D1_H_REG
:
285 case GP2AP020A00F_D2_L_REG
:
286 case GP2AP020A00F_D2_H_REG
:
293 static const struct regmap_config gp2ap020a00f_regmap_config
= {
297 .max_register
= GP2AP020A00F_D2_H_REG
,
298 .cache_type
= REGCACHE_RBTREE
,
300 .volatile_reg
= gp2ap020a00f_is_volatile_reg
,
303 static const struct gp2ap020a00f_mutable_config_regs
{
308 } opmode_regs_settings
[GP2AP020A00F_NUM_OPMODES
] = {
309 [GP2AP020A00F_OPMODE_READ_RAW_CLEAR
] = {
310 GP2AP020A00F_OP_ALS
| GP2AP020A00F_OP2_CONT_OPERATION
311 | GP2AP020A00F_OP3_OPERATION
312 | GP2AP020A00F_TYPE_AUTO_CALC
,
313 GP2AP020A00F_PRST_ONCE
,
314 GP2AP020A00F_INTTYPE_LEVEL
,
317 [GP2AP020A00F_OPMODE_READ_RAW_IR
] = {
318 GP2AP020A00F_OP_ALS
| GP2AP020A00F_OP2_CONT_OPERATION
319 | GP2AP020A00F_OP3_OPERATION
320 | GP2AP020A00F_TYPE_MANUAL_CALC
,
321 GP2AP020A00F_PRST_ONCE
,
322 GP2AP020A00F_INTTYPE_LEVEL
,
325 [GP2AP020A00F_OPMODE_READ_RAW_PROXIMITY
] = {
326 GP2AP020A00F_OP_PS
| GP2AP020A00F_OP2_CONT_OPERATION
327 | GP2AP020A00F_OP3_OPERATION
328 | GP2AP020A00F_TYPE_MANUAL_CALC
,
329 GP2AP020A00F_PRST_ONCE
,
330 GP2AP020A00F_INTTYPE_LEVEL
,
333 [GP2AP020A00F_OPMODE_PROX_DETECT
] = {
334 GP2AP020A00F_OP_PS
| GP2AP020A00F_OP2_CONT_OPERATION
335 | GP2AP020A00F_OP3_OPERATION
336 | GP2AP020A00F_TYPE_MANUAL_CALC
,
337 GP2AP020A00F_PRST_4_CYCLES
,
338 GP2AP020A00F_INTTYPE_PULSE
,
339 GP2AP020A00F_PIN_PS_DETECT
341 [GP2AP020A00F_OPMODE_ALS
] = {
342 GP2AP020A00F_OP_ALS
| GP2AP020A00F_OP2_CONT_OPERATION
343 | GP2AP020A00F_OP3_OPERATION
344 | GP2AP020A00F_TYPE_AUTO_CALC
,
345 GP2AP020A00F_PRST_ONCE
,
346 GP2AP020A00F_INTTYPE_LEVEL
,
349 [GP2AP020A00F_OPMODE_PS
] = {
350 GP2AP020A00F_OP_PS
| GP2AP020A00F_OP2_CONT_OPERATION
351 | GP2AP020A00F_OP3_OPERATION
352 | GP2AP020A00F_TYPE_MANUAL_CALC
,
353 GP2AP020A00F_PRST_4_CYCLES
,
354 GP2AP020A00F_INTTYPE_LEVEL
,
357 [GP2AP020A00F_OPMODE_ALS_AND_PS
] = {
358 GP2AP020A00F_OP_ALS_AND_PS
359 | GP2AP020A00F_OP2_CONT_OPERATION
360 | GP2AP020A00F_OP3_OPERATION
361 | GP2AP020A00F_TYPE_AUTO_CALC
,
362 GP2AP020A00F_PRST_4_CYCLES
,
363 GP2AP020A00F_INTTYPE_LEVEL
,
364 GP2AP020A00F_PIN_ALS_OR_PS
366 [GP2AP020A00F_OPMODE_SHUTDOWN
] = { GP2AP020A00F_OP3_SHUTDOWN
, },
369 static int gp2ap020a00f_set_operation_mode(struct gp2ap020a00f_data
*data
,
370 enum gp2ap020a00f_opmode op
)
372 unsigned int op_reg_val
;
375 if (op
!= GP2AP020A00F_OPMODE_SHUTDOWN
) {
376 err
= regmap_read(data
->regmap
, GP2AP020A00F_OP_REG
,
381 * Shutdown the device if the operation being executed entails
384 if ((opmode_regs_settings
[op
].op_reg
& GP2AP020A00F_OP_MASK
) !=
385 (op_reg_val
& GP2AP020A00F_OP_MASK
)) {
386 /* set shutdown mode */
387 err
= regmap_update_bits(data
->regmap
,
388 GP2AP020A00F_OP_REG
, GP2AP020A00F_OP3_MASK
,
389 GP2AP020A00F_OP3_SHUTDOWN
);
394 err
= regmap_update_bits(data
->regmap
, GP2AP020A00F_ALS_REG
,
395 GP2AP020A00F_PRST_MASK
, opmode_regs_settings
[op
]
400 err
= regmap_update_bits(data
->regmap
, GP2AP020A00F_PS_REG
,
401 GP2AP020A00F_INTTYPE_MASK
, opmode_regs_settings
[op
]
406 err
= regmap_update_bits(data
->regmap
, GP2AP020A00F_LED_REG
,
407 GP2AP020A00F_PIN_MASK
, opmode_regs_settings
[op
]
413 /* Set OP_REG and apply operation mode (power on / off) */
414 err
= regmap_update_bits(data
->regmap
,
416 GP2AP020A00F_OP_MASK
| GP2AP020A00F_OP2_MASK
|
417 GP2AP020A00F_OP3_MASK
| GP2AP020A00F_TYPE_MASK
,
418 opmode_regs_settings
[op
].op_reg
);
422 data
->cur_opmode
= op
;
427 static bool gp2ap020a00f_als_enabled(struct gp2ap020a00f_data
*data
)
429 return test_bit(GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
, &data
->flags
) ||
430 test_bit(GP2AP020A00F_FLAG_ALS_IR_TRIGGER
, &data
->flags
) ||
431 test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
) ||
432 test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
);
435 static bool gp2ap020a00f_prox_detect_enabled(struct gp2ap020a00f_data
*data
)
437 return test_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
) ||
438 test_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
);
441 static int gp2ap020a00f_write_event_threshold(struct gp2ap020a00f_data
*data
,
442 enum gp2ap020a00f_thresh_val_id th_val_id
,
445 __le16 thresh_buf
= 0;
446 unsigned int thresh_reg_val
;
450 else if (test_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
) &&
451 th_val_id
!= GP2AP020A00F_THRESH_PL
&&
452 th_val_id
!= GP2AP020A00F_THRESH_PH
)
454 * For the high lux mode ALS threshold has to be scaled down
455 * to allow for proper comparison with the output value.
457 thresh_reg_val
= data
->thresh_val
[th_val_id
] / 16;
459 thresh_reg_val
= data
->thresh_val
[th_val_id
] > 16000 ?
461 data
->thresh_val
[th_val_id
];
463 thresh_buf
= cpu_to_le16(thresh_reg_val
);
465 return regmap_bulk_write(data
->regmap
,
466 GP2AP020A00F_THRESH_REG(th_val_id
),
467 (u8
*)&thresh_buf
, 2);
470 static int gp2ap020a00f_alter_opmode(struct gp2ap020a00f_data
*data
,
471 enum gp2ap020a00f_opmode diff_mode
, int add_sub
)
473 enum gp2ap020a00f_opmode new_mode
;
475 if (diff_mode
!= GP2AP020A00F_OPMODE_ALS
&&
476 diff_mode
!= GP2AP020A00F_OPMODE_PS
)
479 if (add_sub
== GP2AP020A00F_ADD_MODE
) {
480 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_SHUTDOWN
)
481 new_mode
= diff_mode
;
483 new_mode
= GP2AP020A00F_OPMODE_ALS_AND_PS
;
485 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_ALS_AND_PS
)
486 new_mode
= (diff_mode
== GP2AP020A00F_OPMODE_ALS
) ?
487 GP2AP020A00F_OPMODE_PS
:
488 GP2AP020A00F_OPMODE_ALS
;
490 new_mode
= GP2AP020A00F_OPMODE_SHUTDOWN
;
493 return gp2ap020a00f_set_operation_mode(data
, new_mode
);
496 static int gp2ap020a00f_exec_cmd(struct gp2ap020a00f_data
*data
,
497 enum gp2ap020a00f_cmd cmd
)
502 case GP2AP020A00F_CMD_READ_RAW_CLEAR
:
503 if (data
->cur_opmode
!= GP2AP020A00F_OPMODE_SHUTDOWN
)
505 err
= gp2ap020a00f_set_operation_mode(data
,
506 GP2AP020A00F_OPMODE_READ_RAW_CLEAR
);
508 case GP2AP020A00F_CMD_READ_RAW_IR
:
509 if (data
->cur_opmode
!= GP2AP020A00F_OPMODE_SHUTDOWN
)
511 err
= gp2ap020a00f_set_operation_mode(data
,
512 GP2AP020A00F_OPMODE_READ_RAW_IR
);
514 case GP2AP020A00F_CMD_READ_RAW_PROXIMITY
:
515 if (data
->cur_opmode
!= GP2AP020A00F_OPMODE_SHUTDOWN
)
517 err
= gp2ap020a00f_set_operation_mode(data
,
518 GP2AP020A00F_OPMODE_READ_RAW_PROXIMITY
);
520 case GP2AP020A00F_CMD_TRIGGER_CLEAR_EN
:
521 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
523 if (!gp2ap020a00f_als_enabled(data
))
524 err
= gp2ap020a00f_alter_opmode(data
,
525 GP2AP020A00F_OPMODE_ALS
,
526 GP2AP020A00F_ADD_MODE
);
527 set_bit(GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
, &data
->flags
);
529 case GP2AP020A00F_CMD_TRIGGER_CLEAR_DIS
:
530 clear_bit(GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
, &data
->flags
);
531 if (gp2ap020a00f_als_enabled(data
))
533 err
= gp2ap020a00f_alter_opmode(data
,
534 GP2AP020A00F_OPMODE_ALS
,
535 GP2AP020A00F_SUBTRACT_MODE
);
537 case GP2AP020A00F_CMD_TRIGGER_IR_EN
:
538 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
540 if (!gp2ap020a00f_als_enabled(data
))
541 err
= gp2ap020a00f_alter_opmode(data
,
542 GP2AP020A00F_OPMODE_ALS
,
543 GP2AP020A00F_ADD_MODE
);
544 set_bit(GP2AP020A00F_FLAG_ALS_IR_TRIGGER
, &data
->flags
);
546 case GP2AP020A00F_CMD_TRIGGER_IR_DIS
:
547 clear_bit(GP2AP020A00F_FLAG_ALS_IR_TRIGGER
, &data
->flags
);
548 if (gp2ap020a00f_als_enabled(data
))
550 err
= gp2ap020a00f_alter_opmode(data
,
551 GP2AP020A00F_OPMODE_ALS
,
552 GP2AP020A00F_SUBTRACT_MODE
);
554 case GP2AP020A00F_CMD_TRIGGER_PROX_EN
:
555 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
557 err
= gp2ap020a00f_alter_opmode(data
,
558 GP2AP020A00F_OPMODE_PS
,
559 GP2AP020A00F_ADD_MODE
);
560 set_bit(GP2AP020A00F_FLAG_PROX_TRIGGER
, &data
->flags
);
562 case GP2AP020A00F_CMD_TRIGGER_PROX_DIS
:
563 clear_bit(GP2AP020A00F_FLAG_PROX_TRIGGER
, &data
->flags
);
564 err
= gp2ap020a00f_alter_opmode(data
,
565 GP2AP020A00F_OPMODE_PS
,
566 GP2AP020A00F_SUBTRACT_MODE
);
568 case GP2AP020A00F_CMD_ALS_HIGH_EV_EN
:
569 if (test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
))
571 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
573 if (!gp2ap020a00f_als_enabled(data
)) {
574 err
= gp2ap020a00f_alter_opmode(data
,
575 GP2AP020A00F_OPMODE_ALS
,
576 GP2AP020A00F_ADD_MODE
);
580 set_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
);
581 err
= gp2ap020a00f_write_event_threshold(data
,
582 GP2AP020A00F_THRESH_TH
, true);
584 case GP2AP020A00F_CMD_ALS_HIGH_EV_DIS
:
585 if (!test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
))
587 clear_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
);
588 if (!gp2ap020a00f_als_enabled(data
)) {
589 err
= gp2ap020a00f_alter_opmode(data
,
590 GP2AP020A00F_OPMODE_ALS
,
591 GP2AP020A00F_SUBTRACT_MODE
);
595 err
= gp2ap020a00f_write_event_threshold(data
,
596 GP2AP020A00F_THRESH_TH
, false);
598 case GP2AP020A00F_CMD_ALS_LOW_EV_EN
:
599 if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
))
601 if (data
->cur_opmode
== GP2AP020A00F_OPMODE_PROX_DETECT
)
603 if (!gp2ap020a00f_als_enabled(data
)) {
604 err
= gp2ap020a00f_alter_opmode(data
,
605 GP2AP020A00F_OPMODE_ALS
,
606 GP2AP020A00F_ADD_MODE
);
610 set_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
);
611 err
= gp2ap020a00f_write_event_threshold(data
,
612 GP2AP020A00F_THRESH_TL
, true);
614 case GP2AP020A00F_CMD_ALS_LOW_EV_DIS
:
615 if (!test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
))
617 clear_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
);
618 if (!gp2ap020a00f_als_enabled(data
)) {
619 err
= gp2ap020a00f_alter_opmode(data
,
620 GP2AP020A00F_OPMODE_ALS
,
621 GP2AP020A00F_SUBTRACT_MODE
);
625 err
= gp2ap020a00f_write_event_threshold(data
,
626 GP2AP020A00F_THRESH_TL
, false);
628 case GP2AP020A00F_CMD_PROX_HIGH_EV_EN
:
629 if (test_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
))
631 if (gp2ap020a00f_als_enabled(data
) ||
632 data
->cur_opmode
== GP2AP020A00F_OPMODE_PS
)
634 if (!gp2ap020a00f_prox_detect_enabled(data
)) {
635 err
= gp2ap020a00f_set_operation_mode(data
,
636 GP2AP020A00F_OPMODE_PROX_DETECT
);
640 set_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
);
641 err
= gp2ap020a00f_write_event_threshold(data
,
642 GP2AP020A00F_THRESH_PH
, true);
644 case GP2AP020A00F_CMD_PROX_HIGH_EV_DIS
:
645 if (!test_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
))
647 clear_bit(GP2AP020A00F_FLAG_PROX_RISING_EV
, &data
->flags
);
648 err
= gp2ap020a00f_set_operation_mode(data
,
649 GP2AP020A00F_OPMODE_SHUTDOWN
);
652 err
= gp2ap020a00f_write_event_threshold(data
,
653 GP2AP020A00F_THRESH_PH
, false);
655 case GP2AP020A00F_CMD_PROX_LOW_EV_EN
:
656 if (test_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
))
658 if (gp2ap020a00f_als_enabled(data
) ||
659 data
->cur_opmode
== GP2AP020A00F_OPMODE_PS
)
661 if (!gp2ap020a00f_prox_detect_enabled(data
)) {
662 err
= gp2ap020a00f_set_operation_mode(data
,
663 GP2AP020A00F_OPMODE_PROX_DETECT
);
667 set_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
);
668 err
= gp2ap020a00f_write_event_threshold(data
,
669 GP2AP020A00F_THRESH_PL
, true);
671 case GP2AP020A00F_CMD_PROX_LOW_EV_DIS
:
672 if (!test_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
))
674 clear_bit(GP2AP020A00F_FLAG_PROX_FALLING_EV
, &data
->flags
);
675 err
= gp2ap020a00f_set_operation_mode(data
,
676 GP2AP020A00F_OPMODE_SHUTDOWN
);
679 err
= gp2ap020a00f_write_event_threshold(data
,
680 GP2AP020A00F_THRESH_PL
, false);
687 static int wait_conversion_complete_irq(struct gp2ap020a00f_data
*data
)
691 ret
= wait_event_timeout(data
->data_ready_queue
,
692 test_bit(GP2AP020A00F_FLAG_DATA_READY
,
694 GP2AP020A00F_DATA_READY_TIMEOUT
);
695 clear_bit(GP2AP020A00F_FLAG_DATA_READY
, &data
->flags
);
697 return ret
> 0 ? 0 : -ETIME
;
700 static int gp2ap020a00f_read_output(struct gp2ap020a00f_data
*data
,
701 unsigned int output_reg
, int *val
)
706 err
= wait_conversion_complete_irq(data
);
708 dev_dbg(&data
->client
->dev
, "data ready timeout\n");
710 err
= regmap_bulk_read(data
->regmap
, output_reg
, reg_buf
, 2);
714 *val
= le16_to_cpup((__le16
*)reg_buf
);
719 static bool gp2ap020a00f_adjust_lux_mode(struct gp2ap020a00f_data
*data
,
725 if (!test_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
)) {
726 if (output_val
> 16000) {
727 set_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
);
728 new_range
= GP2AP020A00F_RANGE_A_x128
;
731 if (output_val
< 1000) {
732 clear_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
);
733 new_range
= GP2AP020A00F_RANGE_A_x8
;
737 if (new_range
!= 0xff) {
738 /* Clear als threshold registers to avoid spurious
739 * events caused by lux mode transition.
741 err
= gp2ap020a00f_write_event_threshold(data
,
742 GP2AP020A00F_THRESH_TH
, false);
744 dev_err(&data
->client
->dev
,
745 "Clearing als threshold register failed.\n");
749 err
= gp2ap020a00f_write_event_threshold(data
,
750 GP2AP020A00F_THRESH_TL
, false);
752 dev_err(&data
->client
->dev
,
753 "Clearing als threshold register failed.\n");
757 /* Change lux mode */
758 err
= regmap_update_bits(data
->regmap
,
760 GP2AP020A00F_OP3_MASK
,
761 GP2AP020A00F_OP3_SHUTDOWN
);
764 dev_err(&data
->client
->dev
,
765 "Shutting down the device failed.\n");
769 err
= regmap_update_bits(data
->regmap
,
770 GP2AP020A00F_ALS_REG
,
771 GP2AP020A00F_RANGE_A_MASK
,
775 dev_err(&data
->client
->dev
,
776 "Adjusting device lux mode failed.\n");
780 err
= regmap_update_bits(data
->regmap
,
782 GP2AP020A00F_OP3_MASK
,
783 GP2AP020A00F_OP3_OPERATION
);
786 dev_err(&data
->client
->dev
,
787 "Powering up the device failed.\n");
791 /* Adjust als threshold register values to the new lux mode */
792 if (test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &data
->flags
)) {
793 err
= gp2ap020a00f_write_event_threshold(data
,
794 GP2AP020A00F_THRESH_TH
, true);
796 dev_err(&data
->client
->dev
,
797 "Adjusting als threshold value failed.\n");
802 if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &data
->flags
)) {
803 err
= gp2ap020a00f_write_event_threshold(data
,
804 GP2AP020A00F_THRESH_TL
, true);
806 dev_err(&data
->client
->dev
,
807 "Adjusting als threshold value failed.\n");
818 static void gp2ap020a00f_output_to_lux(struct gp2ap020a00f_data
*data
,
821 if (test_bit(GP2AP020A00F_FLAG_LUX_MODE_HI
, &data
->flags
))
825 static void gp2ap020a00f_iio_trigger_work(struct irq_work
*work
)
827 struct gp2ap020a00f_data
*data
=
828 container_of(work
, struct gp2ap020a00f_data
, work
);
830 iio_trigger_poll(data
->trig
, 0);
833 static irqreturn_t
gp2ap020a00f_prox_sensing_handler(int irq
, void *data
)
835 struct iio_dev
*indio_dev
= data
;
836 struct gp2ap020a00f_data
*priv
= iio_priv(indio_dev
);
837 unsigned int op_reg_val
;
840 /* Read interrupt flags */
841 ret
= regmap_read(priv
->regmap
, GP2AP020A00F_OP_REG
, &op_reg_val
);
845 if (gp2ap020a00f_prox_detect_enabled(priv
)) {
846 if (op_reg_val
& GP2AP020A00F_PROX_DETECT
) {
847 iio_push_event(indio_dev
,
848 IIO_UNMOD_EVENT_CODE(
850 GP2AP020A00F_SCAN_MODE_PROXIMITY
,
855 iio_push_event(indio_dev
,
856 IIO_UNMOD_EVENT_CODE(
858 GP2AP020A00F_SCAN_MODE_PROXIMITY
,
868 static irqreturn_t
gp2ap020a00f_thresh_event_handler(int irq
, void *data
)
870 struct iio_dev
*indio_dev
= data
;
871 struct gp2ap020a00f_data
*priv
= iio_priv(indio_dev
);
872 u8 op_reg_flags
, d0_reg_buf
[2];
873 unsigned int output_val
, op_reg_val
;
874 int thresh_val_id
, ret
;
876 /* Read interrupt flags */
877 ret
= regmap_read(priv
->regmap
, GP2AP020A00F_OP_REG
,
882 op_reg_flags
= op_reg_val
& (GP2AP020A00F_FLAG_A
| GP2AP020A00F_FLAG_P
883 | GP2AP020A00F_PROX_DETECT
);
885 op_reg_val
&= (~GP2AP020A00F_FLAG_A
& ~GP2AP020A00F_FLAG_P
886 & ~GP2AP020A00F_PROX_DETECT
);
888 /* Clear interrupt flags (if not in INTTYPE_PULSE mode) */
889 if (priv
->cur_opmode
!= GP2AP020A00F_OPMODE_PROX_DETECT
) {
890 ret
= regmap_write(priv
->regmap
, GP2AP020A00F_OP_REG
,
896 if (op_reg_flags
& GP2AP020A00F_FLAG_A
) {
897 /* Check D0 register to assess if the lux mode
898 * transition is required.
900 ret
= regmap_bulk_read(priv
->regmap
, GP2AP020A00F_D0_L_REG
,
905 output_val
= le16_to_cpup((__le16
*)d0_reg_buf
);
907 if (gp2ap020a00f_adjust_lux_mode(priv
, output_val
))
910 gp2ap020a00f_output_to_lux(priv
, &output_val
);
913 * We need to check output value to distinguish
914 * between high and low ambient light threshold event.
916 if (test_bit(GP2AP020A00F_FLAG_ALS_RISING_EV
, &priv
->flags
)) {
918 GP2AP020A00F_THRESH_VAL_ID(GP2AP020A00F_TH_L_REG
);
919 if (output_val
> priv
->thresh_val
[thresh_val_id
])
920 iio_push_event(indio_dev
,
923 GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
,
930 if (test_bit(GP2AP020A00F_FLAG_ALS_FALLING_EV
, &priv
->flags
)) {
932 GP2AP020A00F_THRESH_VAL_ID(GP2AP020A00F_TL_L_REG
);
933 if (output_val
< priv
->thresh_val
[thresh_val_id
])
934 iio_push_event(indio_dev
,
937 GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
,
945 if (priv
->cur_opmode
== GP2AP020A00F_OPMODE_READ_RAW_CLEAR
||
946 priv
->cur_opmode
== GP2AP020A00F_OPMODE_READ_RAW_IR
||
947 priv
->cur_opmode
== GP2AP020A00F_OPMODE_READ_RAW_PROXIMITY
) {
948 set_bit(GP2AP020A00F_FLAG_DATA_READY
, &priv
->flags
);
949 wake_up(&priv
->data_ready_queue
);
953 if (test_bit(GP2AP020A00F_FLAG_ALS_CLEAR_TRIGGER
, &priv
->flags
) ||
954 test_bit(GP2AP020A00F_FLAG_ALS_IR_TRIGGER
, &priv
->flags
) ||
955 test_bit(GP2AP020A00F_FLAG_PROX_TRIGGER
, &priv
->flags
))
956 /* This fires off the trigger. */
957 irq_work_queue(&priv
->work
);
963 static irqreturn_t
gp2ap020a00f_trigger_handler(int irq
, void *data
)
965 struct iio_poll_func
*pf
= data
;
966 struct iio_dev
*indio_dev
= pf
->indio_dev
;
967 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
);
984 light_lux
= cpu_to_le32(out_val
);
985 memcpy(&priv
->buffer
[d_size
], (u8
*)&light_lux
, 4);
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 mutex_lock(&data
->lock
);
1293 case IIO_CHAN_INFO_RAW
:
1294 if (iio_buffer_enabled(indio_dev
)) {
1299 err
= gp2ap020a00f_read_channel(data
, chan
, val
);
1304 mutex_unlock(&data
->lock
);
1306 return err
< 0 ? err
: IIO_VAL_INT
;
1309 static const struct iio_event_spec gp2ap020a00f_event_spec_light
[] = {
1311 .type
= IIO_EV_TYPE_THRESH
,
1312 .dir
= IIO_EV_DIR_RISING
,
1313 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
1314 BIT(IIO_EV_INFO_ENABLE
),
1316 .type
= IIO_EV_TYPE_THRESH
,
1317 .dir
= IIO_EV_DIR_FALLING
,
1318 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
1319 BIT(IIO_EV_INFO_ENABLE
),
1323 static const struct iio_event_spec gp2ap020a00f_event_spec_prox
[] = {
1325 .type
= IIO_EV_TYPE_ROC
,
1326 .dir
= IIO_EV_DIR_RISING
,
1327 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
1328 BIT(IIO_EV_INFO_ENABLE
),
1330 .type
= IIO_EV_TYPE_ROC
,
1331 .dir
= IIO_EV_DIR_FALLING
,
1332 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
1333 BIT(IIO_EV_INFO_ENABLE
),
1337 static const struct iio_chan_spec gp2ap020a00f_channels
[] = {
1340 .channel2
= IIO_MOD_LIGHT_CLEAR
,
1342 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1348 .endianness
= IIO_LE
,
1350 .scan_index
= GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
,
1351 .address
= GP2AP020A00F_D0_L_REG
,
1352 .event_spec
= gp2ap020a00f_event_spec_light
,
1353 .num_event_specs
= ARRAY_SIZE(gp2ap020a00f_event_spec_light
),
1357 .channel2
= IIO_MOD_LIGHT_IR
,
1359 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1365 .endianness
= IIO_LE
,
1367 .scan_index
= GP2AP020A00F_SCAN_MODE_LIGHT_IR
,
1368 .address
= GP2AP020A00F_D1_L_REG
,
1371 .type
= IIO_PROXIMITY
,
1373 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
1379 .endianness
= IIO_LE
,
1381 .scan_index
= GP2AP020A00F_SCAN_MODE_PROXIMITY
,
1382 .address
= GP2AP020A00F_D2_L_REG
,
1383 .event_spec
= gp2ap020a00f_event_spec_prox
,
1384 .num_event_specs
= ARRAY_SIZE(gp2ap020a00f_event_spec_prox
),
1386 IIO_CHAN_SOFT_TIMESTAMP(GP2AP020A00F_CHAN_TIMESTAMP
),
1389 static const struct iio_info gp2ap020a00f_info
= {
1390 .read_raw
= &gp2ap020a00f_read_raw
,
1391 .read_event_value
= &gp2ap020a00f_read_event_val
,
1392 .read_event_config
= &gp2ap020a00f_read_event_config
,
1393 .write_event_value
= &gp2ap020a00f_write_event_val
,
1394 .write_event_config
= &gp2ap020a00f_write_event_config
,
1395 .driver_module
= THIS_MODULE
,
1398 static int gp2ap020a00f_buffer_postenable(struct iio_dev
*indio_dev
)
1400 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1403 mutex_lock(&data
->lock
);
1406 * Enable triggers according to the scan_mask. Enabling either
1407 * LIGHT_CLEAR or LIGHT_IR scan mode results in enabling ALS
1408 * module in the device, which generates samples in both D0 (clear)
1409 * and D1 (ir) registers. As the two registers are bound to the
1410 * two separate IIO channels they are treated in the driver logic
1411 * as if they were controlled independently.
1413 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
1414 indio_dev
->masklength
) {
1416 case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
:
1417 err
= gp2ap020a00f_exec_cmd(data
,
1418 GP2AP020A00F_CMD_TRIGGER_CLEAR_EN
);
1420 case GP2AP020A00F_SCAN_MODE_LIGHT_IR
:
1421 err
= gp2ap020a00f_exec_cmd(data
,
1422 GP2AP020A00F_CMD_TRIGGER_IR_EN
);
1424 case GP2AP020A00F_SCAN_MODE_PROXIMITY
:
1425 err
= gp2ap020a00f_exec_cmd(data
,
1426 GP2AP020A00F_CMD_TRIGGER_PROX_EN
);
1434 data
->buffer
= kmalloc(indio_dev
->scan_bytes
, GFP_KERNEL
);
1435 if (!data
->buffer
) {
1440 err
= iio_triggered_buffer_postenable(indio_dev
);
1443 mutex_unlock(&data
->lock
);
1448 static int gp2ap020a00f_buffer_predisable(struct iio_dev
*indio_dev
)
1450 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1453 mutex_lock(&data
->lock
);
1455 err
= iio_triggered_buffer_predisable(indio_dev
);
1459 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
1460 indio_dev
->masklength
) {
1462 case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR
:
1463 err
= gp2ap020a00f_exec_cmd(data
,
1464 GP2AP020A00F_CMD_TRIGGER_CLEAR_DIS
);
1466 case GP2AP020A00F_SCAN_MODE_LIGHT_IR
:
1467 err
= gp2ap020a00f_exec_cmd(data
,
1468 GP2AP020A00F_CMD_TRIGGER_IR_DIS
);
1470 case GP2AP020A00F_SCAN_MODE_PROXIMITY
:
1471 err
= gp2ap020a00f_exec_cmd(data
,
1472 GP2AP020A00F_CMD_TRIGGER_PROX_DIS
);
1478 kfree(data
->buffer
);
1481 mutex_unlock(&data
->lock
);
1486 static const struct iio_buffer_setup_ops gp2ap020a00f_buffer_setup_ops
= {
1487 .postenable
= &gp2ap020a00f_buffer_postenable
,
1488 .predisable
= &gp2ap020a00f_buffer_predisable
,
1491 static const struct iio_trigger_ops gp2ap020a00f_trigger_ops
= {
1492 .owner
= THIS_MODULE
,
1495 static int gp2ap020a00f_probe(struct i2c_client
*client
,
1496 const struct i2c_device_id
*id
)
1498 struct gp2ap020a00f_data
*data
;
1499 struct iio_dev
*indio_dev
;
1500 struct regmap
*regmap
;
1503 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
1507 data
= iio_priv(indio_dev
);
1509 data
->vled_reg
= devm_regulator_get(&client
->dev
, "vled");
1510 if (IS_ERR(data
->vled_reg
))
1511 return PTR_ERR(data
->vled_reg
);
1513 err
= regulator_enable(data
->vled_reg
);
1517 regmap
= devm_regmap_init_i2c(client
, &gp2ap020a00f_regmap_config
);
1518 if (IS_ERR(regmap
)) {
1519 dev_err(&client
->dev
, "Regmap initialization failed.\n");
1520 err
= PTR_ERR(regmap
);
1521 goto error_regulator_disable
;
1524 /* Initialize device registers */
1525 err
= regmap_bulk_write(regmap
, GP2AP020A00F_OP_REG
,
1526 gp2ap020a00f_reg_init_tab
,
1527 ARRAY_SIZE(gp2ap020a00f_reg_init_tab
));
1530 dev_err(&client
->dev
, "Device initialization failed.\n");
1531 goto error_regulator_disable
;
1534 i2c_set_clientdata(client
, indio_dev
);
1536 data
->client
= client
;
1537 data
->cur_opmode
= GP2AP020A00F_OPMODE_SHUTDOWN
;
1538 data
->regmap
= regmap
;
1539 init_waitqueue_head(&data
->data_ready_queue
);
1541 mutex_init(&data
->lock
);
1542 indio_dev
->dev
.parent
= &client
->dev
;
1543 indio_dev
->channels
= gp2ap020a00f_channels
;
1544 indio_dev
->num_channels
= ARRAY_SIZE(gp2ap020a00f_channels
);
1545 indio_dev
->info
= &gp2ap020a00f_info
;
1546 indio_dev
->name
= id
->name
;
1547 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1549 /* Allocate buffer */
1550 err
= iio_triggered_buffer_setup(indio_dev
, &iio_pollfunc_store_time
,
1551 &gp2ap020a00f_trigger_handler
, &gp2ap020a00f_buffer_setup_ops
);
1553 goto error_regulator_disable
;
1555 /* Allocate trigger */
1556 data
->trig
= devm_iio_trigger_alloc(&client
->dev
, "%s-trigger",
1558 if (data
->trig
== NULL
) {
1560 dev_err(&indio_dev
->dev
, "Failed to allocate iio trigger.\n");
1561 goto error_uninit_buffer
;
1564 /* This needs to be requested here for read_raw calls to work. */
1565 err
= request_threaded_irq(client
->irq
, NULL
,
1566 &gp2ap020a00f_thresh_event_handler
,
1567 IRQF_TRIGGER_FALLING
|
1569 "gp2ap020a00f_als_event",
1572 dev_err(&client
->dev
, "Irq request failed.\n");
1573 goto error_uninit_buffer
;
1576 data
->trig
->ops
= &gp2ap020a00f_trigger_ops
;
1577 data
->trig
->dev
.parent
= &data
->client
->dev
;
1579 init_irq_work(&data
->work
, gp2ap020a00f_iio_trigger_work
);
1581 err
= iio_trigger_register(data
->trig
);
1583 dev_err(&client
->dev
, "Failed to register iio trigger.\n");
1584 goto error_free_irq
;
1587 err
= iio_device_register(indio_dev
);
1589 goto error_trigger_unregister
;
1593 error_trigger_unregister
:
1594 iio_trigger_unregister(data
->trig
);
1596 free_irq(client
->irq
, indio_dev
);
1597 error_uninit_buffer
:
1598 iio_triggered_buffer_cleanup(indio_dev
);
1599 error_regulator_disable
:
1600 regulator_disable(data
->vled_reg
);
1605 static int gp2ap020a00f_remove(struct i2c_client
*client
)
1607 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
1608 struct gp2ap020a00f_data
*data
= iio_priv(indio_dev
);
1611 err
= gp2ap020a00f_set_operation_mode(data
,
1612 GP2AP020A00F_OPMODE_SHUTDOWN
);
1614 dev_err(&indio_dev
->dev
, "Failed to power off the device.\n");
1616 iio_device_unregister(indio_dev
);
1617 iio_trigger_unregister(data
->trig
);
1618 free_irq(client
->irq
, indio_dev
);
1619 iio_triggered_buffer_cleanup(indio_dev
);
1620 regulator_disable(data
->vled_reg
);
1625 static const struct i2c_device_id gp2ap020a00f_id
[] = {
1626 { GP2A_I2C_NAME
, 0 },
1630 MODULE_DEVICE_TABLE(i2c
, gp2ap020a00f_id
);
1633 static const struct of_device_id gp2ap020a00f_of_match
[] = {
1634 { .compatible
= "sharp,gp2ap020a00f" },
1639 static struct i2c_driver gp2ap020a00f_driver
= {
1641 .name
= GP2A_I2C_NAME
,
1642 .of_match_table
= of_match_ptr(gp2ap020a00f_of_match
),
1643 .owner
= THIS_MODULE
,
1645 .probe
= gp2ap020a00f_probe
,
1646 .remove
= gp2ap020a00f_remove
,
1647 .id_table
= gp2ap020a00f_id
,
1650 module_i2c_driver(gp2ap020a00f_driver
);
1652 MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
1653 MODULE_DESCRIPTION("Sharp GP2AP020A00F Proximity/ALS sensor driver");
1654 MODULE_LICENSE("GPL v2");