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