@@ -30,7 +30,7 @@ static void apds9960_gpio_callback(struct device *dev,
30
30
struct apds9960_data * drv_data =
31
31
CONTAINER_OF (cb , struct apds9960_data , gpio_cb );
32
32
33
- gpio_pin_disable_callback (dev , DT_AVAGO_APDS9960_0_INT_GPIOS_PIN );
33
+ gpio_pin_disable_callback (dev , drv_data -> gpio_pin );
34
34
35
35
#ifdef CONFIG_APDS9960_TRIGGER
36
36
k_work_submit (& drv_data -> work );
@@ -41,6 +41,7 @@ static void apds9960_gpio_callback(struct device *dev,
41
41
42
42
static int apds9960_sample_fetch (struct device * dev , enum sensor_channel chan )
43
43
{
44
+ const struct apds9960_config * config = dev -> config -> config_info ;
44
45
struct apds9960_data * data = dev -> driver_data ;
45
46
u8_t status ;
46
47
@@ -50,10 +51,9 @@ static int apds9960_sample_fetch(struct device *dev, enum sensor_channel chan)
50
51
}
51
52
52
53
#ifndef CONFIG_APDS9960_TRIGGER
53
- gpio_pin_enable_callback (data -> gpio ,
54
- DT_AVAGO_APDS9960_0_INT_GPIOS_PIN );
54
+ gpio_pin_enable_callback (data -> gpio , config -> gpio_pin );
55
55
56
- if (i2c_reg_update_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
56
+ if (i2c_reg_update_byte (data -> i2c , config -> i2c_address ,
57
57
APDS9960_ENABLE_REG ,
58
58
APDS9960_ENABLE_PON | APDS9960_ENABLE_AIEN ,
59
59
APDS9960_ENABLE_PON | APDS9960_ENABLE_AIEN )) {
@@ -64,21 +64,21 @@ static int apds9960_sample_fetch(struct device *dev, enum sensor_channel chan)
64
64
k_sem_take (& data -> data_sem , K_FOREVER );
65
65
#endif
66
66
67
- if (i2c_reg_read_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
67
+ if (i2c_reg_read_byte (data -> i2c , config -> i2c_address ,
68
68
APDS9960_STATUS_REG , & status )) {
69
69
return - EIO ;
70
70
}
71
71
72
72
LOG_DBG ("status: 0x%x" , status );
73
73
if (status & APDS9960_STATUS_PINT ) {
74
- if (i2c_reg_read_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
74
+ if (i2c_reg_read_byte (data -> i2c , config -> i2c_address ,
75
75
APDS9960_PDATA_REG , & data -> pdata )) {
76
76
return - EIO ;
77
77
}
78
78
}
79
79
80
80
if (status & APDS9960_STATUS_AINT ) {
81
- if (i2c_burst_read (data -> i2c , APDS9960_I2C_ADDRESS ,
81
+ if (i2c_burst_read (data -> i2c , config -> i2c_address ,
82
82
APDS9960_CDATAL_REG ,
83
83
(u8_t * )& data -> sample_crgb ,
84
84
sizeof (data -> sample_crgb ))) {
@@ -88,15 +88,15 @@ static int apds9960_sample_fetch(struct device *dev, enum sensor_channel chan)
88
88
}
89
89
90
90
#ifndef CONFIG_APDS9960_TRIGGER
91
- if (i2c_reg_update_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
91
+ if (i2c_reg_update_byte (data -> i2c , config -> i2c_address ,
92
92
APDS9960_ENABLE_REG ,
93
93
APDS9960_ENABLE_PON ,
94
94
0 )) {
95
95
return - EIO ;
96
96
}
97
97
#endif
98
98
99
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
99
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
100
100
APDS9960_AICLEAR_REG , 0 )) {
101
101
return - EIO ;
102
102
}
@@ -140,57 +140,58 @@ static int apds9960_channel_get(struct device *dev,
140
140
141
141
static int apds9960_proxy_setup (struct device * dev , int gain )
142
142
{
143
+ const struct apds9960_config * config = dev -> config -> config_info ;
143
144
struct apds9960_data * data = dev -> driver_data ;
144
145
145
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
146
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
146
147
APDS9960_POFFSET_UR_REG ,
147
148
APDS9960_DEFAULT_POFFSET_UR )) {
148
149
LOG_ERR ("Default offset UR not set " );
149
150
return - EIO ;
150
151
}
151
152
152
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
153
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
153
154
APDS9960_POFFSET_DL_REG ,
154
155
APDS9960_DEFAULT_POFFSET_DL )) {
155
156
LOG_ERR ("Default offset DL not set " );
156
157
return - EIO ;
157
158
}
158
159
159
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
160
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
160
161
APDS9960_PPULSE_REG ,
161
162
APDS9960_DEFAULT_PROX_PPULSE )) {
162
163
LOG_ERR ("Default pulse count not set " );
163
164
return - EIO ;
164
165
}
165
166
166
- if (i2c_reg_update_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
167
+ if (i2c_reg_update_byte (data -> i2c , config -> i2c_address ,
167
168
APDS9960_CONTROL_REG ,
168
169
APDS9960_CONTROL_LDRIVE ,
169
170
APDS9960_DEFAULT_LDRIVE )) {
170
171
LOG_ERR ("LED Drive Strength not set" );
171
172
return - EIO ;
172
173
}
173
174
174
- if (i2c_reg_update_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
175
+ if (i2c_reg_update_byte (data -> i2c , config -> i2c_address ,
175
176
APDS9960_CONTROL_REG , APDS9960_CONTROL_PGAIN ,
176
177
(gain & APDS9960_PGAIN_8X ))) {
177
178
LOG_ERR ("Gain is not set" );
178
179
return - EIO ;
179
180
}
180
181
181
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
182
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
182
183
APDS9960_PILT_REG , APDS9960_DEFAULT_PILT )) {
183
184
LOG_ERR ("Low threshold not set" );
184
185
return - EIO ;
185
186
}
186
187
187
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
188
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
188
189
APDS9960_PIHT_REG , APDS9960_DEFAULT_PIHT )) {
189
190
LOG_ERR ("High threshold not set" );
190
191
return - EIO ;
191
192
}
192
193
193
- if (i2c_reg_update_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
194
+ if (i2c_reg_update_byte (data -> i2c , config -> i2c_address ,
194
195
APDS9960_ENABLE_REG , APDS9960_ENABLE_PEN ,
195
196
APDS9960_ENABLE_PEN )) {
196
197
LOG_ERR ("Proximity mode is not enabled" );
@@ -202,18 +203,19 @@ static int apds9960_proxy_setup(struct device *dev, int gain)
202
203
203
204
static int apds9960_ambient_setup (struct device * dev , int gain )
204
205
{
206
+ const struct apds9960_config * config = dev -> config -> config_info ;
205
207
struct apds9960_data * data = dev -> driver_data ;
206
208
u16_t th ;
207
209
208
210
/* ADC value */
209
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
211
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
210
212
APDS9960_ATIME_REG , APDS9960_DEFAULT_ATIME )) {
211
213
LOG_ERR ("Default integration time not set for ADC" );
212
214
return - EIO ;
213
215
}
214
216
215
217
/* ALS Gain */
216
- if (i2c_reg_update_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
218
+ if (i2c_reg_update_byte (data -> i2c , config -> i2c_address ,
217
219
APDS9960_CONTROL_REG ,
218
220
APDS9960_CONTROL_AGAIN ,
219
221
(gain & APDS9960_AGAIN_64X ))) {
@@ -222,23 +224,23 @@ static int apds9960_ambient_setup(struct device *dev, int gain)
222
224
}
223
225
224
226
th = sys_cpu_to_le16 (APDS9960_DEFAULT_AILT );
225
- if (i2c_burst_write (data -> i2c , APDS9960_I2C_ADDRESS ,
227
+ if (i2c_burst_write (data -> i2c , config -> i2c_address ,
226
228
APDS9960_INT_AILTL_REG ,
227
229
(u8_t * )& th , sizeof (th ))) {
228
230
LOG_ERR ("ALS low threshold not set" );
229
231
return - EIO ;
230
232
}
231
233
232
234
th = sys_cpu_to_le16 (APDS9960_DEFAULT_AIHT );
233
- if (i2c_burst_write (data -> i2c , APDS9960_I2C_ADDRESS ,
235
+ if (i2c_burst_write (data -> i2c , config -> i2c_address ,
234
236
APDS9960_INT_AIHTL_REG ,
235
237
(u8_t * )& th , sizeof (th ))) {
236
238
LOG_ERR ("ALS low threshold not set" );
237
239
return - EIO ;
238
240
}
239
241
240
242
/* Enable ALS */
241
- if (i2c_reg_update_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
243
+ if (i2c_reg_update_byte (data -> i2c , config -> i2c_address ,
242
244
APDS9960_ENABLE_REG , APDS9960_ENABLE_AEN ,
243
245
APDS9960_ENABLE_AEN )) {
244
246
LOG_ERR ("ALS is not enabled" );
@@ -250,10 +252,11 @@ static int apds9960_ambient_setup(struct device *dev, int gain)
250
252
251
253
static int apds9960_sensor_setup (struct device * dev )
252
254
{
255
+ const struct apds9960_config * config = dev -> config -> config_info ;
253
256
struct apds9960_data * data = dev -> driver_data ;
254
257
u8_t chip_id ;
255
258
256
- if (i2c_reg_read_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
259
+ if (i2c_reg_read_byte (data -> i2c , config -> i2c_address ,
257
260
APDS9960_ID_REG , & chip_id )) {
258
261
LOG_ERR ("Failed reading chip id" );
259
262
return - EIO ;
@@ -265,52 +268,52 @@ static int apds9960_sensor_setup(struct device *dev)
265
268
}
266
269
267
270
/* Disable all functions and interrupts */
268
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
271
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
269
272
APDS9960_ENABLE_REG , 0 )) {
270
273
LOG_ERR ("ENABLE register is not cleared" );
271
274
return - EIO ;
272
275
}
273
276
274
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
277
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
275
278
APDS9960_AICLEAR_REG , 0 )) {
276
279
return - EIO ;
277
280
}
278
281
279
282
/* Disable gesture interrupt */
280
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
283
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
281
284
APDS9960_GCONFIG4_REG , 0 )) {
282
285
LOG_ERR ("GCONFIG4 register is not cleared" );
283
286
return - EIO ;
284
287
}
285
288
286
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
289
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
287
290
APDS9960_WTIME_REG , APDS9960_DEFAULT_WTIME )) {
288
291
LOG_ERR ("Default wait time not set" );
289
292
return - EIO ;
290
293
}
291
294
292
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
295
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
293
296
APDS9960_CONFIG1_REG ,
294
297
APDS9960_DEFAULT_CONFIG1 )) {
295
298
LOG_ERR ("Default WLONG not set" );
296
299
return - EIO ;
297
300
}
298
301
299
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
302
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
300
303
APDS9960_CONFIG2_REG ,
301
304
APDS9960_DEFAULT_CONFIG2 )) {
302
305
LOG_ERR ("Configuration Register Two not set" );
303
306
return - EIO ;
304
307
}
305
308
306
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
309
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
307
310
APDS9960_CONFIG3_REG ,
308
311
APDS9960_DEFAULT_CONFIG3 )) {
309
312
LOG_ERR ("Configuration Register Three not set" );
310
313
return - EIO ;
311
314
}
312
315
313
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
316
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
314
317
APDS9960_PERS_REG ,
315
318
APDS9960_DEFAULT_PERS )) {
316
319
LOG_ERR ("Interrupt persistence not set" );
@@ -332,24 +335,27 @@ static int apds9960_sensor_setup(struct device *dev)
332
335
333
336
static int apds9960_init_interrupt (struct device * dev )
334
337
{
338
+ const struct apds9960_config * config = dev -> config -> config_info ;
335
339
struct apds9960_data * drv_data = dev -> driver_data ;
336
340
337
341
/* setup gpio interrupt */
338
- drv_data -> gpio = device_get_binding (DT_AVAGO_APDS9960_0_INT_GPIOS_CONTROLLER );
342
+ drv_data -> gpio = device_get_binding (config -> gpio_name );
339
343
if (drv_data -> gpio == NULL ) {
340
344
LOG_ERR ("Failed to get pointer to %s device!" ,
341
- DT_AVAGO_APDS9960_0_INT_GPIOS_CONTROLLER );
345
+ config -> gpio_name );
342
346
return - EINVAL ;
343
347
}
344
348
345
- gpio_pin_configure (drv_data -> gpio , DT_AVAGO_APDS9960_0_INT_GPIOS_PIN ,
349
+ drv_data -> gpio_pin = config -> gpio_pin ;
350
+
351
+ gpio_pin_configure (drv_data -> gpio , config -> gpio_pin ,
346
352
GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
347
353
GPIO_INT_ACTIVE_LOW | GPIO_INT_DEBOUNCE |
348
354
GPIO_PUD_PULL_UP );
349
355
350
356
gpio_init_callback (& drv_data -> gpio_cb ,
351
357
apds9960_gpio_callback ,
352
- BIT (DT_AVAGO_APDS9960_0_INT_GPIOS_PIN ));
358
+ BIT (config -> gpio_pin ));
353
359
354
360
if (gpio_add_callback (drv_data -> gpio , & drv_data -> gpio_cb ) < 0 ) {
355
361
LOG_DBG ("Failed to set gpio callback!" );
@@ -359,7 +365,7 @@ static int apds9960_init_interrupt(struct device *dev)
359
365
#ifdef CONFIG_APDS9960_TRIGGER
360
366
drv_data -> work .handler = apds9960_work_cb ;
361
367
drv_data -> dev = dev ;
362
- if (i2c_reg_update_byte (drv_data -> i2c , APDS9960_I2C_ADDRESS ,
368
+ if (i2c_reg_update_byte (drv_data -> i2c , config -> i2c_address ,
363
369
APDS9960_ENABLE_REG ,
364
370
APDS9960_ENABLE_PON ,
365
371
APDS9960_ENABLE_PON )) {
@@ -377,14 +383,15 @@ static int apds9960_init_interrupt(struct device *dev)
377
383
static int apds9960_device_ctrl (struct device * dev , u32_t ctrl_command ,
378
384
void * context , device_pm_cb cb , void * arg )
379
385
{
386
+ const struct apds9960_config * config = dev -> config -> config_info ;
380
387
struct apds9960_data * data = dev -> driver_data ;
381
388
int ret = 0 ;
382
389
383
390
if (ctrl_command == DEVICE_PM_SET_POWER_STATE ) {
384
391
u32_t device_pm_state = * (u32_t * )context ;
385
392
386
393
if (device_pm_state == DEVICE_PM_ACTIVE_STATE ) {
387
- if (i2c_reg_update_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
394
+ if (i2c_reg_update_byte (data -> i2c , config -> i2c_address ,
388
395
APDS9960_ENABLE_REG ,
389
396
APDS9960_ENABLE_PON ,
390
397
APDS9960_ENABLE_PON )) {
@@ -393,13 +400,13 @@ static int apds9960_device_ctrl(struct device *dev, u32_t ctrl_command,
393
400
394
401
} else {
395
402
396
- if (i2c_reg_update_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
403
+ if (i2c_reg_update_byte (data -> i2c , config -> i2c_address ,
397
404
APDS9960_ENABLE_REG ,
398
405
APDS9960_ENABLE_PON , 0 )) {
399
406
ret = - EIO ;
400
407
}
401
408
402
- if (i2c_reg_write_byte (data -> i2c , APDS9960_I2C_ADDRESS ,
409
+ if (i2c_reg_write_byte (data -> i2c , config -> i2c_address ,
403
410
APDS9960_AICLEAR_REG , 0 )) {
404
411
ret = - EIO ;
405
412
}
@@ -419,15 +426,16 @@ static int apds9960_device_ctrl(struct device *dev, u32_t ctrl_command,
419
426
420
427
static int apds9960_init (struct device * dev )
421
428
{
429
+ const struct apds9960_config * config = dev -> config -> config_info ;
422
430
struct apds9960_data * data = dev -> driver_data ;
423
431
424
432
/* Initialize time 5.7ms */
425
433
k_sleep (6 );
426
- data -> i2c = device_get_binding (DT_AVAGO_APDS9960_0_BUS_NAME );
434
+ data -> i2c = device_get_binding (config -> i2c_name );
427
435
428
436
if (data -> i2c == NULL ) {
429
437
LOG_ERR ("Failed to get pointer to %s device!" ,
430
- DT_AVAGO_APDS9960_0_BUS_NAME );
438
+ config -> i2c_name );
431
439
return - EINVAL ;
432
440
}
433
441
@@ -453,14 +461,21 @@ static const struct sensor_driver_api apds9960_driver_api = {
453
461
#endif
454
462
};
455
463
464
+ static const struct apds9960_config apds9960_config = {
465
+ .i2c_name = DT_AVAGO_APDS9960_0_BUS_NAME ,
466
+ .i2c_address = DT_AVAGO_APDS9960_0_BASE_ADDRESS ,
467
+ .gpio_name = DT_AVAGO_APDS9960_0_INT_GPIOS_CONTROLLER ,
468
+ .gpio_pin = DT_AVAGO_APDS9960_0_INT_GPIOS_PIN ,
469
+ };
470
+
456
471
static struct apds9960_data apds9960_data ;
457
472
458
473
#ifndef CONFIG_DEVICE_POWER_MANAGEMENT
459
474
DEVICE_AND_API_INIT (apds9960 , DT_AVAGO_APDS9960_0_LABEL , & apds9960_init ,
460
- & apds9960_data , NULL , POST_KERNEL ,
475
+ & apds9960_data , & apds9960_config , POST_KERNEL ,
461
476
CONFIG_SENSOR_INIT_PRIORITY , & apds9960_driver_api );
462
477
#else
463
478
DEVICE_DEFINE (apds9960 , DT_AVAGO_APDS9960_0_LABEL , apds9960_init ,
464
- apds9960_device_ctrl , & apds9960_data , NULL ,
479
+ apds9960_device_ctrl , & apds9960_data , & apds9960_config ,
465
480
POST_KERNEL , CONFIG_SENSOR_INIT_PRIORITY , & apds9960_driver_api );
466
481
#endif
0 commit comments