Skip to content

Commit 0cff42f

Browse files
MaureenHelmgalak
authored andcommitted
drivers: sensor: Refactor apds9960 to use const config struct
Refactors the apds9960 sensor driver to get the i2c device name, i2c device address, gpio device name, and gpio pin from a constant device configuration structure, rather than using hardcoded macros. This will make it easier to change the names of the macros and to instantiate multiple instances of the driver. Signed-off-by: Maureen Helm <[email protected]>
1 parent cfab327 commit 0cff42f

File tree

4 files changed

+75
-51
lines changed

4 files changed

+75
-51
lines changed

drivers/sensor/apds9960/apds9960.c

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static void apds9960_gpio_callback(struct device *dev,
3030
struct apds9960_data *drv_data =
3131
CONTAINER_OF(cb, struct apds9960_data, gpio_cb);
3232

33-
gpio_pin_disable_callback(dev, DT_AVAGO_APDS9960_0_INT_GPIOS_PIN);
33+
gpio_pin_disable_callback(dev, drv_data->gpio_pin);
3434

3535
#ifdef CONFIG_APDS9960_TRIGGER
3636
k_work_submit(&drv_data->work);
@@ -41,6 +41,7 @@ static void apds9960_gpio_callback(struct device *dev,
4141

4242
static int apds9960_sample_fetch(struct device *dev, enum sensor_channel chan)
4343
{
44+
const struct apds9960_config *config = dev->config->config_info;
4445
struct apds9960_data *data = dev->driver_data;
4546
u8_t status;
4647

@@ -50,10 +51,9 @@ static int apds9960_sample_fetch(struct device *dev, enum sensor_channel chan)
5051
}
5152

5253
#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);
5555

56-
if (i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
56+
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
5757
APDS9960_ENABLE_REG,
5858
APDS9960_ENABLE_PON | APDS9960_ENABLE_AIEN,
5959
APDS9960_ENABLE_PON | APDS9960_ENABLE_AIEN)) {
@@ -64,21 +64,21 @@ static int apds9960_sample_fetch(struct device *dev, enum sensor_channel chan)
6464
k_sem_take(&data->data_sem, K_FOREVER);
6565
#endif
6666

67-
if (i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
67+
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
6868
APDS9960_STATUS_REG, &status)) {
6969
return -EIO;
7070
}
7171

7272
LOG_DBG("status: 0x%x", status);
7373
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,
7575
APDS9960_PDATA_REG, &data->pdata)) {
7676
return -EIO;
7777
}
7878
}
7979

8080
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,
8282
APDS9960_CDATAL_REG,
8383
(u8_t *)&data->sample_crgb,
8484
sizeof(data->sample_crgb))) {
@@ -88,15 +88,15 @@ static int apds9960_sample_fetch(struct device *dev, enum sensor_channel chan)
8888
}
8989

9090
#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,
9292
APDS9960_ENABLE_REG,
9393
APDS9960_ENABLE_PON,
9494
0)) {
9595
return -EIO;
9696
}
9797
#endif
9898

99-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
99+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
100100
APDS9960_AICLEAR_REG, 0)) {
101101
return -EIO;
102102
}
@@ -140,57 +140,58 @@ static int apds9960_channel_get(struct device *dev,
140140

141141
static int apds9960_proxy_setup(struct device *dev, int gain)
142142
{
143+
const struct apds9960_config *config = dev->config->config_info;
143144
struct apds9960_data *data = dev->driver_data;
144145

145-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
146+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
146147
APDS9960_POFFSET_UR_REG,
147148
APDS9960_DEFAULT_POFFSET_UR)) {
148149
LOG_ERR("Default offset UR not set ");
149150
return -EIO;
150151
}
151152

152-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
153+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
153154
APDS9960_POFFSET_DL_REG,
154155
APDS9960_DEFAULT_POFFSET_DL)) {
155156
LOG_ERR("Default offset DL not set ");
156157
return -EIO;
157158
}
158159

159-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
160+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
160161
APDS9960_PPULSE_REG,
161162
APDS9960_DEFAULT_PROX_PPULSE)) {
162163
LOG_ERR("Default pulse count not set ");
163164
return -EIO;
164165
}
165166

166-
if (i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
167+
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
167168
APDS9960_CONTROL_REG,
168169
APDS9960_CONTROL_LDRIVE,
169170
APDS9960_DEFAULT_LDRIVE)) {
170171
LOG_ERR("LED Drive Strength not set");
171172
return -EIO;
172173
}
173174

174-
if (i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
175+
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
175176
APDS9960_CONTROL_REG, APDS9960_CONTROL_PGAIN,
176177
(gain & APDS9960_PGAIN_8X))) {
177178
LOG_ERR("Gain is not set");
178179
return -EIO;
179180
}
180181

181-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
182+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
182183
APDS9960_PILT_REG, APDS9960_DEFAULT_PILT)) {
183184
LOG_ERR("Low threshold not set");
184185
return -EIO;
185186
}
186187

187-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
188+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
188189
APDS9960_PIHT_REG, APDS9960_DEFAULT_PIHT)) {
189190
LOG_ERR("High threshold not set");
190191
return -EIO;
191192
}
192193

193-
if (i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
194+
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
194195
APDS9960_ENABLE_REG, APDS9960_ENABLE_PEN,
195196
APDS9960_ENABLE_PEN)) {
196197
LOG_ERR("Proximity mode is not enabled");
@@ -202,18 +203,19 @@ static int apds9960_proxy_setup(struct device *dev, int gain)
202203

203204
static int apds9960_ambient_setup(struct device *dev, int gain)
204205
{
206+
const struct apds9960_config *config = dev->config->config_info;
205207
struct apds9960_data *data = dev->driver_data;
206208
u16_t th;
207209

208210
/* ADC value */
209-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
211+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
210212
APDS9960_ATIME_REG, APDS9960_DEFAULT_ATIME)) {
211213
LOG_ERR("Default integration time not set for ADC");
212214
return -EIO;
213215
}
214216

215217
/* ALS Gain */
216-
if (i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
218+
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
217219
APDS9960_CONTROL_REG,
218220
APDS9960_CONTROL_AGAIN,
219221
(gain & APDS9960_AGAIN_64X))) {
@@ -222,23 +224,23 @@ static int apds9960_ambient_setup(struct device *dev, int gain)
222224
}
223225

224226
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,
226228
APDS9960_INT_AILTL_REG,
227229
(u8_t *)&th, sizeof(th))) {
228230
LOG_ERR("ALS low threshold not set");
229231
return -EIO;
230232
}
231233

232234
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,
234236
APDS9960_INT_AIHTL_REG,
235237
(u8_t *)&th, sizeof(th))) {
236238
LOG_ERR("ALS low threshold not set");
237239
return -EIO;
238240
}
239241

240242
/* Enable ALS */
241-
if (i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
243+
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
242244
APDS9960_ENABLE_REG, APDS9960_ENABLE_AEN,
243245
APDS9960_ENABLE_AEN)) {
244246
LOG_ERR("ALS is not enabled");
@@ -250,10 +252,11 @@ static int apds9960_ambient_setup(struct device *dev, int gain)
250252

251253
static int apds9960_sensor_setup(struct device *dev)
252254
{
255+
const struct apds9960_config *config = dev->config->config_info;
253256
struct apds9960_data *data = dev->driver_data;
254257
u8_t chip_id;
255258

256-
if (i2c_reg_read_byte(data->i2c, APDS9960_I2C_ADDRESS,
259+
if (i2c_reg_read_byte(data->i2c, config->i2c_address,
257260
APDS9960_ID_REG, &chip_id)) {
258261
LOG_ERR("Failed reading chip id");
259262
return -EIO;
@@ -265,52 +268,52 @@ static int apds9960_sensor_setup(struct device *dev)
265268
}
266269

267270
/* 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,
269272
APDS9960_ENABLE_REG, 0)) {
270273
LOG_ERR("ENABLE register is not cleared");
271274
return -EIO;
272275
}
273276

274-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
277+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
275278
APDS9960_AICLEAR_REG, 0)) {
276279
return -EIO;
277280
}
278281

279282
/* 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,
281284
APDS9960_GCONFIG4_REG, 0)) {
282285
LOG_ERR("GCONFIG4 register is not cleared");
283286
return -EIO;
284287
}
285288

286-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
289+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
287290
APDS9960_WTIME_REG, APDS9960_DEFAULT_WTIME)) {
288291
LOG_ERR("Default wait time not set");
289292
return -EIO;
290293
}
291294

292-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
295+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
293296
APDS9960_CONFIG1_REG,
294297
APDS9960_DEFAULT_CONFIG1)) {
295298
LOG_ERR("Default WLONG not set");
296299
return -EIO;
297300
}
298301

299-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
302+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
300303
APDS9960_CONFIG2_REG,
301304
APDS9960_DEFAULT_CONFIG2)) {
302305
LOG_ERR("Configuration Register Two not set");
303306
return -EIO;
304307
}
305308

306-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
309+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
307310
APDS9960_CONFIG3_REG,
308311
APDS9960_DEFAULT_CONFIG3)) {
309312
LOG_ERR("Configuration Register Three not set");
310313
return -EIO;
311314
}
312315

313-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
316+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
314317
APDS9960_PERS_REG,
315318
APDS9960_DEFAULT_PERS)) {
316319
LOG_ERR("Interrupt persistence not set");
@@ -332,24 +335,27 @@ static int apds9960_sensor_setup(struct device *dev)
332335

333336
static int apds9960_init_interrupt(struct device *dev)
334337
{
338+
const struct apds9960_config *config = dev->config->config_info;
335339
struct apds9960_data *drv_data = dev->driver_data;
336340

337341
/* 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);
339343
if (drv_data->gpio == NULL) {
340344
LOG_ERR("Failed to get pointer to %s device!",
341-
DT_AVAGO_APDS9960_0_INT_GPIOS_CONTROLLER);
345+
config->gpio_name);
342346
return -EINVAL;
343347
}
344348

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,
346352
GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
347353
GPIO_INT_ACTIVE_LOW | GPIO_INT_DEBOUNCE |
348354
GPIO_PUD_PULL_UP);
349355

350356
gpio_init_callback(&drv_data->gpio_cb,
351357
apds9960_gpio_callback,
352-
BIT(DT_AVAGO_APDS9960_0_INT_GPIOS_PIN));
358+
BIT(config->gpio_pin));
353359

354360
if (gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb) < 0) {
355361
LOG_DBG("Failed to set gpio callback!");
@@ -359,7 +365,7 @@ static int apds9960_init_interrupt(struct device *dev)
359365
#ifdef CONFIG_APDS9960_TRIGGER
360366
drv_data->work.handler = apds9960_work_cb;
361367
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,
363369
APDS9960_ENABLE_REG,
364370
APDS9960_ENABLE_PON,
365371
APDS9960_ENABLE_PON)) {
@@ -377,14 +383,15 @@ static int apds9960_init_interrupt(struct device *dev)
377383
static int apds9960_device_ctrl(struct device *dev, u32_t ctrl_command,
378384
void *context, device_pm_cb cb, void *arg)
379385
{
386+
const struct apds9960_config *config = dev->config->config_info;
380387
struct apds9960_data *data = dev->driver_data;
381388
int ret = 0;
382389

383390
if (ctrl_command == DEVICE_PM_SET_POWER_STATE) {
384391
u32_t device_pm_state = *(u32_t *)context;
385392

386393
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,
388395
APDS9960_ENABLE_REG,
389396
APDS9960_ENABLE_PON,
390397
APDS9960_ENABLE_PON)) {
@@ -393,13 +400,13 @@ static int apds9960_device_ctrl(struct device *dev, u32_t ctrl_command,
393400

394401
} else {
395402

396-
if (i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS,
403+
if (i2c_reg_update_byte(data->i2c, config->i2c_address,
397404
APDS9960_ENABLE_REG,
398405
APDS9960_ENABLE_PON, 0)) {
399406
ret = -EIO;
400407
}
401408

402-
if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS,
409+
if (i2c_reg_write_byte(data->i2c, config->i2c_address,
403410
APDS9960_AICLEAR_REG, 0)) {
404411
ret = -EIO;
405412
}
@@ -419,15 +426,16 @@ static int apds9960_device_ctrl(struct device *dev, u32_t ctrl_command,
419426

420427
static int apds9960_init(struct device *dev)
421428
{
429+
const struct apds9960_config *config = dev->config->config_info;
422430
struct apds9960_data *data = dev->driver_data;
423431

424432
/* Initialize time 5.7ms */
425433
k_sleep(6);
426-
data->i2c = device_get_binding(DT_AVAGO_APDS9960_0_BUS_NAME);
434+
data->i2c = device_get_binding(config->i2c_name);
427435

428436
if (data->i2c == NULL) {
429437
LOG_ERR("Failed to get pointer to %s device!",
430-
DT_AVAGO_APDS9960_0_BUS_NAME);
438+
config->i2c_name);
431439
return -EINVAL;
432440
}
433441

@@ -453,14 +461,21 @@ static const struct sensor_driver_api apds9960_driver_api = {
453461
#endif
454462
};
455463

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+
456471
static struct apds9960_data apds9960_data;
457472

458473
#ifndef CONFIG_DEVICE_POWER_MANAGEMENT
459474
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,
461476
CONFIG_SENSOR_INIT_PRIORITY, &apds9960_driver_api);
462477
#else
463478
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,
465480
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &apds9960_driver_api);
466481
#endif

0 commit comments

Comments
 (0)