Skip to content

Commit 5f9363e

Browse files
Onebit5jic23
authored andcommitted
iio: light: add support for APDS9999 sensor
Add IIO driver for Broadcom APDS9999 ambient light sensor. The APDS9999 is a digital proximity and RGB sensor with ALS capability. The driver implements the ALS/Lux functionality using the green channel, which uses optical coating technology to approximate the human eye spectral response. Raw IIO_INTENSITY channels are exposed for red, green, blue, and IR so userspace can compute its own weighted lux. Proximity (PS) support is not yet implemented. Signed-off-by: Jose A. Perez de Azpillaga <azpijr@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 46968e0 commit 5f9363e

4 files changed

Lines changed: 349 additions & 0 deletions

File tree

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4997,6 +4997,7 @@ M: Jose A. Perez de Azpillaga <azpijr@gmail.com>
49974997
L: linux-iio@vger.kernel.org
49984998
S: Maintained
49994999
F: Documentation/devicetree/bindings/iio/light/brcm,apds9999.yaml
5000+
F: drivers/iio/light/apds9999.c
50005001

50015002
BROADCOM ASP 2.0 ETHERNET DRIVER
50025003
M: Justin Chen <justin.chen@broadcom.com>

drivers/iio/light/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ config APDS9960
119119
To compile this driver as a module, choose M here: the
120120
module will be called apds9960
121121

122+
config APDS9999
123+
tristate "Broadcom APDS9999 ALS, RGB and proximity sensor"
124+
depends on I2C
125+
help
126+
Say Y here if you want to build support for the Broadcom APDS9999
127+
ALS, RGB and proximity sensor with I2C interface.
128+
129+
This driver provides ambient light sensing (ALS/Lux), raw
130+
intensity data for red, green, blue and IR channels, plus
131+
proximity detection support.
132+
133+
To compile this driver as a module, choose M here: the
134+
module will be called apds9999.
135+
122136
config AS73211
123137
tristate "AMS AS73211 XYZ color sensor and AMS AS7331 UV sensor"
124138
depends on I2C

drivers/iio/light/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ obj-$(CONFIG_APDS9160) += apds9160.o
1414
obj-$(CONFIG_APDS9300) += apds9300.o
1515
obj-$(CONFIG_APDS9306) += apds9306.o
1616
obj-$(CONFIG_APDS9960) += apds9960.o
17+
obj-$(CONFIG_APDS9999) += apds9999.o
1718
obj-$(CONFIG_AS73211) += as73211.o
1819
obj-$(CONFIG_BH1745) += bh1745.o
1920
obj-$(CONFIG_BH1750) += bh1750.o

drivers/iio/light/apds9999.c

Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* IIO driver for Broadcom APDS9999 Lux Light Sensor
4+
*
5+
* Copyright (C) 2026
6+
* Author: Jose A. Perez de Azpillaga <azpijr@gmail.com>
7+
*
8+
* TODO: proximity sensor
9+
*/
10+
11+
#include <linux/bitfield.h>
12+
#include <linux/bitops.h>
13+
#include <linux/cleanup.h>
14+
#include <linux/delay.h>
15+
#include <linux/i2c.h>
16+
#include <linux/iio/iio.h>
17+
#include <linux/iio/sysfs.h>
18+
#include <linux/math64.h>
19+
#include <linux/mod_devicetable.h>
20+
#include <linux/module.h>
21+
#include <linux/mutex.h>
22+
#include <linux/unaligned.h>
23+
#include <linux/units.h>
24+
25+
#define APDS9999_REG_MAIN_CTRL 0x00
26+
#define APDS9999_MAIN_CTRL_LS_EN BIT(1)
27+
#define APDS9999_REG_LS_MEAS_RATE 0x04
28+
#define APDS9999_LS_RES_MASK GENMASK(6, 4)
29+
#define APDS9999_LS_RATE_MASK GENMASK(2, 0)
30+
#define APDS9999_REG_LS_GAIN 0x05
31+
#define APDS9999_REG_PART_ID 0x06
32+
#define APDS9999_REG_MAIN_STATUS 0x07
33+
#define APDS9999_MAIN_STATUS_LS_DATA BIT(3)
34+
#define APDS9999_REG_LS_DATA_IR_0 0x0A
35+
#define APDS9999_REG_LS_DATA_GREEN_0 0x0D
36+
#define APDS9999_REG_LS_DATA_BLUE_0 0x10
37+
#define APDS9999_REG_LS_DATA_RED_0 0x13
38+
39+
#define APDS9999_PART_ID 0xC2
40+
41+
#define APDS9999_GAIN_1X 0
42+
#define APDS9999_GAIN_3X 1
43+
#define APDS9999_GAIN_6X 2
44+
#define APDS9999_GAIN_9X 3
45+
#define APDS9999_GAIN_18X 4
46+
47+
static const int apds9999_gains[] = {
48+
[APDS9999_GAIN_1X] = 1,
49+
[APDS9999_GAIN_3X] = 3,
50+
[APDS9999_GAIN_6X] = 6,
51+
[APDS9999_GAIN_9X] = 9,
52+
[APDS9999_GAIN_18X] = 18,
53+
};
54+
55+
#define APDS9999_RES_20BIT 0
56+
#define APDS9999_RES_19BIT 1
57+
#define APDS9999_RES_18BIT 2
58+
#define APDS9999_RES_17BIT 3
59+
#define APDS9999_RES_16BIT 4
60+
#define APDS9999_RES_13BIT 5
61+
62+
static const int apds9999_itimes_us[] = {
63+
[APDS9999_RES_20BIT] = 400 * USEC_PER_MSEC,
64+
[APDS9999_RES_19BIT] = 200 * USEC_PER_MSEC,
65+
[APDS9999_RES_18BIT] = 100 * USEC_PER_MSEC,
66+
[APDS9999_RES_17BIT] = 50 * USEC_PER_MSEC,
67+
[APDS9999_RES_16BIT] = 25 * USEC_PER_MSEC,
68+
[APDS9999_RES_13BIT] = 3125,
69+
};
70+
71+
#define APDS9999_RATE_25_MS 0
72+
#define APDS9999_RATE_50_MS 1
73+
#define APDS9999_RATE_100_MS 2
74+
#define APDS9999_RATE_200_MS 3
75+
#define APDS9999_RATE_500_MS 4
76+
#define APDS9999_RATE_1000_MS 5
77+
#define APDS9999_RATE_2000_MS 6
78+
79+
struct apds9999_data {
80+
struct i2c_client *client;
81+
/* lock: serializes access to device registers and cached values */
82+
struct mutex lock;
83+
int als_gain_idx;
84+
int als_res;
85+
int als_rate;
86+
};
87+
88+
static void apds9999_standby(void *client)
89+
{
90+
i2c_smbus_write_byte_data(client, APDS9999_REG_MAIN_CTRL, 0);
91+
}
92+
93+
/*
94+
* Apply power-on defaults: 18-bit / 100 ms resolution and rate,
95+
* 3x gain. These match the datasheet reset values.
96+
*/
97+
static int apds9999_init(struct apds9999_data *data)
98+
{
99+
struct device *dev = &data->client->dev;
100+
struct i2c_client *client = data->client;
101+
u8 regval;
102+
int ret;
103+
104+
ret = devm_add_action_or_reset(dev, apds9999_standby, client);
105+
if (ret)
106+
return ret;
107+
108+
guard(mutex)(&data->lock);
109+
110+
regval = FIELD_PREP(APDS9999_LS_RES_MASK, APDS9999_RES_18BIT) |
111+
FIELD_PREP(APDS9999_LS_RATE_MASK, APDS9999_RATE_100_MS);
112+
ret = i2c_smbus_write_byte_data(client, APDS9999_REG_LS_MEAS_RATE,
113+
regval);
114+
if (ret)
115+
return ret;
116+
data->als_res = APDS9999_RES_18BIT;
117+
data->als_rate = APDS9999_RATE_100_MS;
118+
119+
ret = i2c_smbus_write_byte_data(client, APDS9999_REG_LS_GAIN,
120+
APDS9999_GAIN_3X);
121+
if (ret)
122+
return ret;
123+
data->als_gain_idx = APDS9999_GAIN_3X;
124+
125+
return i2c_smbus_write_byte_data(client, APDS9999_REG_MAIN_CTRL,
126+
APDS9999_MAIN_CTRL_LS_EN);
127+
}
128+
129+
static int apds9999_read_channel(struct apds9999_data *data, u8 reg,
130+
u32 *counts)
131+
{
132+
struct i2c_client *client = data->client;
133+
u8 buf[3];
134+
int ret, tries;
135+
136+
guard(mutex)(&data->lock);
137+
138+
/*
139+
* Poll MAIN_STATUS for new data. Timeout: ~2 integration periods
140+
* plus margin. Each try sleeps 20 ms.
141+
*/
142+
tries = max(2, (apds9999_itimes_us[data->als_res] * 2) / 20000);
143+
144+
while (tries--) {
145+
ret = i2c_smbus_read_byte_data(client,
146+
APDS9999_REG_MAIN_STATUS);
147+
if (ret < 0)
148+
return ret;
149+
if (ret & APDS9999_MAIN_STATUS_LS_DATA)
150+
break;
151+
fsleep(20000);
152+
}
153+
154+
if (tries < 0)
155+
return -ETIMEDOUT;
156+
157+
ret = i2c_smbus_read_i2c_block_data(client, reg, sizeof(buf), buf);
158+
if (ret < 0)
159+
return ret;
160+
if (ret != sizeof(buf))
161+
return -EIO;
162+
163+
*counts = get_unaligned_le24(buf) & GENMASK(19, 0);
164+
return 0;
165+
}
166+
167+
static int apds9999_read_raw(struct iio_dev *indio_dev,
168+
struct iio_chan_spec const *chan,
169+
int *val, int *val2, long mask)
170+
{
171+
struct apds9999_data *data = iio_priv(indio_dev);
172+
int gain, itime_us;
173+
u64 scale_nano;
174+
u32 counts;
175+
int ret;
176+
177+
switch (mask) {
178+
case IIO_CHAN_INFO_RAW:
179+
ret = apds9999_read_channel(data, chan->address, &counts);
180+
if (ret)
181+
return ret;
182+
*val = (int)counts;
183+
return IIO_VAL_INT;
184+
185+
case IIO_CHAN_INFO_SCALE: {
186+
u32 remainder;
187+
188+
/*
189+
* Scale (lux per count) = 54 / (gain * integration_time_ms)
190+
*
191+
* The constant 54 is derived from the datasheet table:
192+
* at gain = 3x, itime = 100 ms -> 0.180 lux/count
193+
* -> C = 0.180 * 3 * 100 = 54
194+
*
195+
* Expressed as IIO_VAL_INT_PLUS_NANO.
196+
*/
197+
gain = apds9999_gains[data->als_gain_idx];
198+
itime_us = apds9999_itimes_us[data->als_res];
199+
200+
/* scale_nano = 54 * 1e12 / (gain * itime_us) nano-lux/count */
201+
scale_nano = div_u64(54ULL * NSEC_PER_SEC * USEC_PER_MSEC, (u32)(gain * itime_us));
202+
*val = (int)div_u64_rem(scale_nano, NSEC_PER_SEC, &remainder);
203+
*val2 = (int)remainder;
204+
return IIO_VAL_INT_PLUS_NANO;
205+
}
206+
case IIO_CHAN_INFO_INT_TIME:
207+
*val = 0;
208+
*val2 = apds9999_itimes_us[data->als_res];
209+
return IIO_VAL_INT_PLUS_MICRO;
210+
211+
default:
212+
return -EINVAL;
213+
}
214+
}
215+
216+
static const struct iio_info apds9999_info = {
217+
.read_raw = apds9999_read_raw,
218+
};
219+
220+
/*
221+
* The green channel uses optical coating to approximate the human eye
222+
* spectral response. IIO_INTENSITY channels provide raw ADC data for
223+
* red, green, blue, and IR so userspace can compute weighted lux.
224+
*/
225+
static const struct iio_chan_spec apds9999_channels[] = {
226+
{
227+
.type = IIO_LIGHT,
228+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
229+
BIT(IIO_CHAN_INFO_SCALE),
230+
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
231+
.address = APDS9999_REG_LS_DATA_GREEN_0,
232+
},
233+
{
234+
.type = IIO_INTENSITY,
235+
.modified = 1,
236+
.channel2 = IIO_MOD_LIGHT_RED,
237+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
238+
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
239+
.address = APDS9999_REG_LS_DATA_RED_0,
240+
},
241+
{
242+
.type = IIO_INTENSITY,
243+
.modified = 1,
244+
.channel2 = IIO_MOD_LIGHT_GREEN,
245+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
246+
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
247+
.address = APDS9999_REG_LS_DATA_GREEN_0,
248+
},
249+
{
250+
.type = IIO_INTENSITY,
251+
.modified = 1,
252+
.channel2 = IIO_MOD_LIGHT_BLUE,
253+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
254+
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
255+
.address = APDS9999_REG_LS_DATA_BLUE_0,
256+
},
257+
{
258+
.type = IIO_INTENSITY,
259+
.modified = 1,
260+
.channel2 = IIO_MOD_LIGHT_IR,
261+
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
262+
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
263+
.address = APDS9999_REG_LS_DATA_IR_0,
264+
},
265+
};
266+
267+
static int apds9999_probe(struct i2c_client *client)
268+
{
269+
struct device *dev = &client->dev;
270+
struct apds9999_data *data;
271+
struct iio_dev *indio_dev;
272+
int ret, part_id;
273+
274+
indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
275+
if (!indio_dev)
276+
return -ENOMEM;
277+
278+
data = iio_priv(indio_dev);
279+
data->client = client;
280+
281+
ret = devm_mutex_init(dev, &data->lock);
282+
if (ret)
283+
return ret;
284+
285+
part_id = i2c_smbus_read_byte_data(client, APDS9999_REG_PART_ID);
286+
if (part_id < 0)
287+
return dev_err_probe(dev, part_id, "failed to read PART_ID\n");
288+
if (part_id != APDS9999_PART_ID)
289+
dev_info(dev, "unexpected PART_ID 0x%02x (expected 0x%02x)\n",
290+
part_id, APDS9999_PART_ID);
291+
292+
ret = apds9999_init(data);
293+
if (ret)
294+
return dev_err_probe(dev, ret, "failed to initialize device\n");
295+
296+
indio_dev->name = "apds9999";
297+
indio_dev->info = &apds9999_info;
298+
indio_dev->channels = apds9999_channels;
299+
indio_dev->num_channels = ARRAY_SIZE(apds9999_channels);
300+
indio_dev->modes = INDIO_DIRECT_MODE;
301+
302+
ret = devm_iio_device_register(dev, indio_dev);
303+
if (ret)
304+
return dev_err_probe(dev, ret, "failed to register IIO device\n");
305+
306+
return 0;
307+
}
308+
309+
static const struct i2c_device_id apds9999_id[] = {
310+
{ .name = "apds9999" },
311+
{ }
312+
};
313+
MODULE_DEVICE_TABLE(i2c, apds9999_id);
314+
315+
static const struct of_device_id apds9999_of_match[] = {
316+
{ .compatible = "brcm,apds9999" },
317+
{ }
318+
};
319+
MODULE_DEVICE_TABLE(of, apds9999_of_match);
320+
321+
static struct i2c_driver apds9999_driver = {
322+
.driver = {
323+
.name = "apds9999",
324+
.of_match_table = apds9999_of_match,
325+
},
326+
.probe = apds9999_probe,
327+
.id_table = apds9999_id,
328+
};
329+
module_i2c_driver(apds9999_driver);
330+
331+
MODULE_AUTHOR("Jose A. Perez de Azpillaga <azpijr@gmail.com>");
332+
MODULE_DESCRIPTION("APDS-9999 Lux Light Sensor IIO Driver");
333+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)