Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
 *
 *Copyright (c) 2017 Intel Corporation
 *
 *SPDX-License-Identifier: Apache-2.0
 *
 */

/* @file
 * @brief driver for APDS9960 ALS/RGB/gesture/proximity sensor
 */

#include <device.h>
#include <sensor.h>
#include <i2c.h>
#include <misc/__assert.h>
#include <init.h>
#include <kernel.h>

#include "apds9960.h"

static int apds9960_sample_fetch(struct device *dev, enum sensor_channel chan)
{
	struct apds9960_data *data = dev->driver_data;
	u8_t cmsb, clsb, rmsb, rlsb, blsb, bmsb, glsb, gmsb;
	u8_t pdata;
	int temp = 0;

	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);

	/* Read lower byte following by MSB  Ref: Datasheet : RGBC register */
	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
			APDS9960_CDATAL_REG, &clsb);
	if (temp < 0) {
		return temp;
	}

	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
			APDS9960_CDATAH_REG, &cmsb);
	if (temp < 0) {
		return temp;
	}

	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
			APDS9960_RDATAL_REG, &rlsb);
	if (temp < 0) {
		return temp;
	}

	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
			APDS9960_RDATAH_REG, &rmsb);
	if (temp < 0) {
		return temp;
	}

	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
			APDS9960_GDATAL_REG, &glsb);
	if (temp < 0) {
		return temp;
	}

	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
			APDS9960_GDATAH_REG, &gmsb);
	if (temp < 0) {
		return temp;
	}

	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
			APDS9960_BDATAL_REG, &blsb);
	if (temp < 0) {
		return temp;
	}

	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
			APDS9960_BDATAH_REG, &bmsb);
	if (temp < 0) {
		return temp;
	}

	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
			APDS9960_PDATA_REG, &pdata);
	if (temp < 0) {
		return temp;
	}

	data->sample_c = (cmsb << 8) + clsb;
	data->sample_r = (rmsb << 8) + rlsb;
	data->sample_g = (gmsb << 8) + glsb;
	data->sample_b = (bmsb << 8) + blsb;
	data->pdata    = pdata;

	return 0;
}

static int apds9960_channel_get(struct device *dev,
				enum sensor_channel chan,
				struct sensor_value *val)
{
	struct apds9960_data *data = dev->driver_data;

	switch (chan) {
	case SENSOR_CHAN_LIGHT:
		val->val1 = data->sample_c;
		val->val2 = 0;
		break;
	case SENSOR_CHAN_RED:
		val->val1 = data->sample_r;
		val->val2 = 0;
		break;
	case SENSOR_CHAN_GREEN:
		val->val1 = data->sample_g;
		val->val2 = 0;
		break;
	case SENSOR_CHAN_BLUE:
		val->val1 = data->sample_b;
		val->val2 = 0;
		break;
	case SENSOR_CHAN_PROX:
		val->val1 = data->pdata;
		val->val2 = 0;
		break;
	default:
		return -ENOTSUP;
	}

	return 0;
}

static int apds9960_setproxint_lowthresh(struct device *dev, u8_t threshold)
{
	struct apds9960_data *data = dev->driver_data;
	int temp = 0;

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_PILT_REG, threshold);
	if (temp < 0) {
		SYS_LOG_ERR(" Failed");
		return temp;
	}

	return 0;
}

static int apds9960_setproxint_highthresh(struct device *dev, u8_t threshold)
{
	struct apds9960_data *data = dev->driver_data;
	int temp = 0;

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_PIHT_REG, threshold);
	if (temp < 0) {
		SYS_LOG_ERR(" Failed");
		return temp;
	}

	return 0;
}

static int apds9960_setlightint_lowthresh(struct device *dev, u16_t threshold)
{
	struct apds9960_data *data = dev->driver_data;
	int temp = 0;
	u8_t val_low;
	u8_t val_high;

	/* Break 16-bit threshold into 2 8-bit values */
	val_low = threshold & 0x00FF;
	val_high = (threshold & 0xFF00) >> 8;

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_INT_AILTL_REG, val_low);
	if (temp < 0) {
		SYS_LOG_ERR(" Failed");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_INT_AILTH_REG, val_high);
	if (temp < 0) {
		SYS_LOG_ERR(" Failed");
		return temp;
	}

	return 0;
}

static int apds9960_setlightint_highthresh(struct device *dev, u16_t threshold)
{
	struct apds9960_data *data = dev->driver_data;
	int temp = 0;
	u8_t val_low;
	u8_t val_high;

	/* Break 16-bit threshold into 2 8-bit values */
	val_low = threshold & 0x00FF;
	val_high = (threshold & 0xFF00) >> 8;

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_INT_AIHTL_REG, val_low);
	if (temp < 0) {
		SYS_LOG_ERR(" Failed");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_INT_AIHTH_REG, val_high);
	if (temp < 0) {
		SYS_LOG_ERR(" Failed");
		return temp;
	}

	return 0;
}

static int apds9960_proxy_setup(struct device *dev, int gain)
{
	struct apds9960_data *data = dev->driver_data;
	int temp = 0;
	u8_t mask = APDS9960_ENABLE_PROXY | APDS9960_ENABLE_PIEN;
	u8_t val = APDS9960_PROXY_ON;

	/* Power ON */
	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ENABLE_REG, BIT(0), APDS9960_POWER_ON);
	if (temp < 0) {
		SYS_LOG_ERR("Power on bit not set.");
		return temp;
	}

	/* ADC value */
	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ATIME_REG, APDS9960_ATIME_WRTIE, APDS9960_ADC_VALUE);
	if (temp < 0) {
		SYS_LOG_ERR("ADC bits are not written");
		return temp;
	}

	/* proxy Gain */
	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_CONTROL_REG, APDS9960_CONTROL_PGAIN,
		(gain & APDS9960_PGAIN_8X));

	if (temp < 0) {
		SYS_LOG_ERR("proxy Gain is not set");
		return temp;
	}

	/* Enable proxy */
	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ENABLE_REG, mask, val);
	if (temp < 0) {
		SYS_LOG_ERR("Proxy is not enabled");
		return temp;
	}

	return temp;
}

static int apds9960_ambient_setup(struct device *dev, int gain)
{
	struct apds9960_data *data = dev->driver_data;
	int temp = 0;

	/* Power ON */
	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ENABLE_REG, BIT(0), APDS9960_POWER_ON);
	if (temp < 0) {
		SYS_LOG_ERR("Power on bit not set.");
		return -EIO;
	}

	/* ADC value */
	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ATIME_REG, APDS9960_ATIME_WRTIE, APDS9960_ADC_VALUE);
	if (temp < 0) {
		SYS_LOG_ERR("ADC bits are not written");
		return temp;
	}

	/* ALE Gain */
	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_CONTROL_REG, APDS9960_CONTROL_AGAIN,
		(gain & APDS9960_AGAIN_64X));
	if (temp < 0) {
		SYS_LOG_ERR("Ambient Gain is not set");
		return temp;
	}

	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ENABLE_REG, BIT(4), 0x00);
	if (temp < 0) {
		return temp;
	}

	/* Enable ALE */
	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ENABLE_REG, APDS9960_ENABLE_ALE, APDS9960_RGB_ON);
	if (temp < 0) {
		SYS_LOG_ERR("Proxy is not enabled");
		return temp;
	}

	return 0;
}

static int apds9960_sensor_setup(struct device *dev, int gain)
{
	struct apds9960_data *data = dev->driver_data;
	int temp = 0;
	u8_t chip_id;

	temp = i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ID_REG, &chip_id);
	if (temp < 0) {
		SYS_LOG_ERR("failed reading chip id");
		return temp;
	}

	if (!((chip_id == APDS9960_ID_1) || (chip_id == APDS9960_ID_2))) {
		SYS_LOG_ERR("invalid chip id 0x%x", chip_id);
		return -EIO;
	}

	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ENABLE_REG, APDS9960_ALL_BITS,
		APDS9960_MODE_OFF);
	if (temp < 0) {
		SYS_LOG_ERR("ENABLE registered is not cleared");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_ATIME_REG, APDS9960_DEFAULT_ATIME);
	if (temp < 0) {
		SYS_LOG_ERR("Default integration time not set for ADC");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_WTIME_REG, APDS9960_DEFAULT_WTIME);
	if (temp < 0) {
		SYS_LOG_ERR("Default wait time not set ");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_PPULSE_REG, APDS9960_DEFAULT_PROX_PPULSE);
	if (temp < 0) {
		SYS_LOG_ERR("Default proximity ppulse not set ");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_POFFSET_UR_REG, APDS9960_DEFAULT_POFFSET_UR);
	if (temp < 0) {
		SYS_LOG_ERR("Default poffset UR not set ");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_POFFSET_DL_REG, APDS9960_DEFAULT_POFFSET_DL);
	if (temp < 0) {
		SYS_LOG_ERR("Default poffset DL not set ");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_CONFIG1_REG, APDS9960_DEFAULT_CONFIG1);
	if (temp < 0) {
		SYS_LOG_ERR("Default config1 not set ");
		return temp;
	}

	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_CONTROL_REG, APDS9960_CONTROL_LDRIVE,
		APDS9960_DEFAULT_LDRIVE);
	if (temp < 0) {
		SYS_LOG_ERR("LEDdrive not set");
		return -EIO;
	}

	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_CONTROL_REG, APDS9960_CONTROL_PGAIN,
		(gain & APDS9960_DEFAULT_PGAIN));
	if (temp < 0) {
		SYS_LOG_ERR("proxy Gain is not set");
		return temp;
	}

	temp = i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_CONTROL_REG, APDS9960_CONTROL_AGAIN,
		(gain & APDS9960_DEFAULT_AGAIN));
	if (temp < 0) {
		SYS_LOG_ERR("Ambient Gain is not set");
		return temp;
	}

	temp = apds9960_setproxint_lowthresh(dev, APDS9960_DEFAULT_PILT);
	if (temp < 0) {
		SYS_LOG_ERR("prox low threshold not set");
		return temp;
	}

	temp = apds9960_setproxint_highthresh(dev, APDS9960_DEFAULT_PIHT);
	if (temp < 0) {
		SYS_LOG_ERR("prox high threshold not set");
		return temp;
	}

	temp = apds9960_setlightint_lowthresh(dev, APDS9960_DEFAULT_AILT);
	if (temp < 0) {
		SYS_LOG_ERR("light low threshold not set");
		return temp;
	}

	temp = apds9960_setlightint_highthresh(dev, APDS9960_DEFAULT_AIHT);
	if (temp < 0) {
		SYS_LOG_ERR("light high threshold not set");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_PERS_REG, APDS9960_DEFAULT_PERS);
	if (temp < 0) {
		SYS_LOG_ERR("ALS interrupt persistence not set ");
		return temp;
	}

	temp = i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
		APDS9960_CONFIG2_REG, APDS9960_DEFAULT_CONFIG2);
	if (temp < 0) {
		SYS_LOG_ERR("clear diode saturation interrupt is not enabled");
		return temp;
	}

	temp = apds9960_proxy_setup(dev, gain);
	if (temp < 0) {
		SYS_LOG_ERR("Failed to setup proximity functionality");
		return temp;
	}

	temp = apds9960_ambient_setup(dev, gain);
	if (temp < 0) {
		SYS_LOG_ERR("Failed to setup ambient light functionality");
		return temp;
	}

	return 0;
}

static const struct sensor_driver_api apds9960_driver_api = {
	.sample_fetch = &apds9960_sample_fetch,
	.channel_get = &apds9960_channel_get,
};

static int apds9960_init(struct device *dev)
{
	struct apds9960_data *data = dev->driver_data;
	int als_gain = 0;

	data->i2c = device_get_binding(CONFIG_APDS9960_I2C_DEV_NAME);

	if (data->i2c == NULL) {
		SYS_LOG_ERR("Failed to get pointer to %s device!",
		CONFIG_APDS9960_I2C_DEV_NAME);
		return -EINVAL;
	}

	data->sample_c = 0;
	data->sample_r = 0;
	data->sample_g = 0;
	data->sample_b = 0;
	data->pdata = 0;

	apds9960_sensor_setup(dev, als_gain);

	return 0;
}

static struct apds9960_data apds9960_data;

DEVICE_AND_API_INIT(apds9960, CONFIG_APDS9960_DRV_NAME, &apds9960_init,
		&apds9960_data, NULL, POST_KERNEL,
		CONFIG_SENSOR_INIT_PRIORITY, &apds9960_driver_api);