Skip to content

Commit 8075a76

Browse files
committed
drivers: sensor: apds9960: Allow multiple sensor instances
Allow for multiple apds9960s to be present Signed-off-by: Thomas Lang <[email protected]>
1 parent 4fe2c5b commit 8075a76

File tree

4 files changed

+69
-130
lines changed

4 files changed

+69
-130
lines changed

drivers/sensor/apds9960/Kconfig

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -58,81 +58,4 @@ config APDS9960_ENABLE_ALS
5858
help
5959
Enable Ambient Light Sense (ALS).
6060

61-
choice
62-
prompt "Proximity Gain"
63-
default APDS9960_PGAIN_4X
64-
65-
config APDS9960_PGAIN_1X
66-
bool "1x"
67-
68-
config APDS9960_PGAIN_2X
69-
bool "2x"
70-
71-
config APDS9960_PGAIN_4X
72-
bool "4x"
73-
74-
config APDS9960_PGAIN_8X
75-
bool "8x"
76-
77-
endchoice
78-
79-
choice
80-
prompt "ALS and Color Gain"
81-
default APDS9960_AGAIN_4X
82-
83-
config APDS9960_AGAIN_1X
84-
bool "1x"
85-
86-
config APDS9960_AGAIN_4X
87-
bool "4x"
88-
89-
config APDS9960_AGAIN_16X
90-
bool "16x"
91-
92-
config APDS9960_AGAIN_64X
93-
bool "64x"
94-
95-
endchoice
96-
97-
choice
98-
prompt "Proximity Pulse Length"
99-
default APDS9960_PPULSE_LENGTH_8US
100-
101-
config APDS9960_PPULSE_LENGTH_4US
102-
bool "4us"
103-
104-
config APDS9960_PPULSE_LENGTH_8US
105-
bool "8us"
106-
107-
config APDS9960_PPULSE_LENGTH_16US
108-
bool "16us"
109-
110-
config APDS9960_PPULSE_LENGTH_32US
111-
bool "32us"
112-
113-
endchoice
114-
115-
choice
116-
prompt "Proximity LED boost current"
117-
default APDS9960_PLED_BOOST_100PCT
118-
119-
config APDS9960_PLED_BOOST_300PCT
120-
bool "300%"
121-
122-
config APDS9960_PLED_BOOST_200PCT
123-
bool "200%"
124-
125-
config APDS9960_PLED_BOOST_150PCT
126-
bool "150%"
127-
128-
config APDS9960_PLED_BOOST_100PCT
129-
bool "100%"
130-
131-
endchoice
132-
133-
config APDS9960_PPULSE_COUNT
134-
int "Proximity Pulse Count"
135-
range 1 64
136-
default 8
137-
13861
endif # APDS9960

drivers/sensor/apds9960/apds9960.c

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <string.h>
2323
#include <zephyr/logging/log.h>
2424

25-
#include "apds9960.h"
25+
#include <zephyr/drivers/sensor/apds9960.h>
2626

2727
LOG_MODULE_REGISTER(APDS9960, CONFIG_SENSOR_LOG_LEVEL);
2828

@@ -520,57 +520,28 @@ static DEVICE_API(sensor, apds9960_driver_api) = {
520520
#endif
521521
};
522522

523-
static const struct apds9960_config apds9960_config = {
524-
.i2c = I2C_DT_SPEC_INST_GET(0),
525-
#ifdef CONFIG_APDS9960_FETCH_MODE_INTERRUPT
526-
.int_gpio = GPIO_DT_SPEC_INST_GET(0, int_gpios),
527-
#endif
528-
#if CONFIG_APDS9960_PGAIN_8X
529-
.pgain = APDS9960_PGAIN_8X,
530-
#elif CONFIG_APDS9960_PGAIN_4X
531-
.pgain = APDS9960_PGAIN_4X,
532-
#elif CONFIG_APDS9960_PGAIN_2X
533-
.pgain = APDS9960_PGAIN_2X,
534-
#else
535-
.pgain = APDS9960_PGAIN_1X,
536-
#endif
537-
#if CONFIG_APDS9960_AGAIN_64X
538-
.again = APDS9960_AGAIN_64X,
539-
#elif CONFIG_APDS9960_AGAIN_16X
540-
.again = APDS9960_AGAIN_16X,
541-
#elif CONFIG_APDS9960_AGAIN_4X
542-
.again = APDS9960_AGAIN_4X,
543-
#else
544-
.again = APDS9960_AGAIN_1X,
545-
#endif
546-
#if CONFIG_APDS9960_PPULSE_LENGTH_32US
547-
.ppcount = APDS9960_PPULSE_LENGTH_32US |
548-
(CONFIG_APDS9960_PPULSE_COUNT - 1),
549-
#elif CONFIG_APDS9960_PPULSE_LENGTH_16US
550-
.ppcount = APDS9960_PPULSE_LENGTH_16US |
551-
(CONFIG_APDS9960_PPULSE_COUNT - 1),
552-
#elif CONFIG_APDS9960_PPULSE_LENGTH_8US
553-
.ppcount = APDS9960_PPULSE_LENGTH_8US |
554-
(CONFIG_APDS9960_PPULSE_COUNT - 1),
523+
#if CONFIG_APDS9960_FETCH_MODE_INTERRUPT
524+
#define APDS9960_CONFIG_INTERRUPT(inst) \
525+
.int_gpio = GPIO_DT_SPEC_INST_GET(i, int_gpios),
555526
#else
556-
.ppcount = APDS9960_PPULSE_LENGTH_4US |
557-
(CONFIG_APDS9960_PPULSE_COUNT - 1),
527+
#define APDS9960_CONFIG_INTERRUPT(inst)
558528
#endif
559-
#if CONFIG_APDS9960_PLED_BOOST_300PCT
560-
.pled_boost = APDS9960_PLED_BOOST_300,
561-
#elif CONFIG_APDS9960_PLED_BOOST_200PCT
562-
.pled_boost = APDS9960_PLED_BOOST_200,
563-
#elif CONFIG_APDS9960_PLED_BOOST_150PCT
564-
.pled_boost = APDS9960_PLED_BOOST_150,
565-
#else
566-
.pled_boost = APDS9960_PLED_BOOST_100,
567-
#endif
568-
};
569-
570-
static struct apds9960_data apds9960_data;
571-
572-
PM_DEVICE_DT_INST_DEFINE(0, apds9960_pm_action);
573529

574-
SENSOR_DEVICE_DT_INST_DEFINE(0, apds9960_init,
575-
PM_DEVICE_DT_INST_GET(0), &apds9960_data, &apds9960_config,
576-
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &apds9960_driver_api);
530+
#define APDS9960_INIT(i) \
531+
static struct apds9960_config apds9960_data_##i; \
532+
static const struct apds9960_config apds9960_config_##i = { \
533+
.i2c = I2C_DT_SPEC_INST_GET(i), \
534+
APDS9960_CONFIG_INTERRUPT(i) \
535+
.pgain = DT_INST_PROP(i, pgain) << 1, \
536+
.again = DT_INST_PROP(i, again), \
537+
.ppcount = DT_INST_PROP(i, ppulse_length) | (DT_INST_PROP(i, ppulse_count) - 1), \
538+
.pled_boost = DT_INST_PROP(i, pled_boost) << 4, \
539+
}; \
540+
\
541+
PM_DEVICE_DT_INST_DEFINE(i, apds9960_pm_action); \
542+
\
543+
SENSOR_DEVICE_DT_INST_DEFINE(i, apds9960_init, \
544+
PM_DEVICE_DT_INST_GET(i), &apds9960_data_##i, &apds9960_config_##i, \
545+
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &apds9960_driver_api);
546+
547+
DT_INST_FOREACH_STATUS_OKAY(APDS9960_INIT)

drivers/sensor/apds9960/apds9960_trigger.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <zephyr/sys/util.h>
1111
#include <zephyr/kernel.h>
1212
#include <zephyr/drivers/sensor.h>
13-
#include "apds9960.h"
13+
#include <zephyr/drivers/sensor/apds9960.h>
1414

1515
extern struct apds9960_data apds9960_driver;
1616

dts/bindings/sensor/avago,apds9960.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,48 @@ properties:
1515
The interrupt pin of APDS9960 is open-drain, active low.
1616
If connected directly the MCU pin should be configured
1717
as pull-up, active low.
18+
19+
pled-boost:
20+
type: int
21+
default: 0x01
22+
description: Proximity LED Boost Current
23+
enum:
24+
- 0x00 # 100
25+
- 0x01 # 150
26+
- 0x10 # 200
27+
- 0x11 # 300
28+
29+
pgain:
30+
type: int
31+
default: 0x01
32+
description: ALS and Color Gain
33+
enum:
34+
- 0x00 # 1x
35+
- 0x01 # 4x
36+
- 0x10 # 4x
37+
- 0x11 # 8x
38+
39+
again:
40+
type: int
41+
default: 0x01
42+
description: ALS and Color Gain
43+
enum:
44+
- 0x00 # 1x
45+
- 0x01 # 4x
46+
- 0x10 # 16x
47+
- 0x11 # 64x
48+
49+
ppulse-length:
50+
type: int
51+
default: 0x01
52+
description: Proximity Pulse Length
53+
enum:
54+
- 0x00 # 4us
55+
- 0x01 # 8us
56+
- 0x10 # 16us
57+
- 0x11 # 32us
58+
59+
ppulse-count:
60+
type: int
61+
default: 0x8
62+
description: Proximity Pulse Count

0 commit comments

Comments
 (0)