20
20
21
21
#include "tmp11x.h"
22
22
23
- #define EEPROM_SIZE_REG sizeof(uint16_t)
24
- #define EEPROM_TMP117_RESERVED (2 * sizeof(uint16_t))
25
- #define EEPROM_MIN_BUSY_MS 7
26
- #define RESET_MIN_BUSY_MS 2
23
+ #define EEPROM_SIZE_REG sizeof(uint16_t)
24
+ #define EEPROM_TMP117_RESERVED (2 * sizeof(uint16_t))
25
+ #define EEPROM_MIN_BUSY_MS 7
26
+ #define RESET_MIN_BUSY_MS 2
27
27
28
28
LOG_MODULE_REGISTER (TMP11X , CONFIG_SENSOR_LOG_LEVEL );
29
29
30
30
int tmp11x_reg_read (const struct device * dev , uint8_t reg , uint16_t * val )
31
31
{
32
32
const struct tmp11x_dev_config * cfg = dev -> config ;
33
33
34
- if (i2c_burst_read_dt (& cfg -> bus , reg , (uint8_t * )val , 2 )
35
- < 0 ) {
34
+ if (i2c_burst_read_dt (& cfg -> bus , reg , (uint8_t * )val , 2 ) < 0 ) {
36
35
return - EIO ;
37
36
}
38
37
@@ -96,13 +95,11 @@ static inline int16_t tmp11x_sensor_value_to_reg_format(const struct sensor_valu
96
95
}
97
96
}
98
97
99
- static bool check_eeprom_bounds (const struct device * dev , off_t offset ,
100
- size_t len )
98
+ static bool check_eeprom_bounds (const struct device * dev , off_t offset , size_t len )
101
99
{
102
100
struct tmp11x_data * drv_data = dev -> data ;
103
101
104
- if ((offset + len ) > EEPROM_TMP11X_SIZE ||
105
- offset % EEPROM_SIZE_REG != 0 ||
102
+ if ((offset + len ) > EEPROM_TMP11X_SIZE || offset % EEPROM_SIZE_REG != 0 ||
106
103
len % EEPROM_SIZE_REG != 0 ) {
107
104
return false;
108
105
}
@@ -130,8 +127,7 @@ int tmp11x_eeprom_await(const struct device *dev)
130
127
return res ;
131
128
}
132
129
133
- int tmp11x_eeprom_write (const struct device * dev , off_t offset ,
134
- const void * data , size_t len )
130
+ int tmp11x_eeprom_write (const struct device * dev , off_t offset , const void * data , size_t len )
135
131
{
136
132
uint8_t reg ;
137
133
const uint16_t * src = data ;
@@ -167,8 +163,7 @@ int tmp11x_eeprom_write(const struct device *dev, off_t offset,
167
163
return res ;
168
164
}
169
165
170
- int tmp11x_eeprom_read (const struct device * dev , off_t offset , void * data ,
171
- size_t len )
166
+ int tmp11x_eeprom_read (const struct device * dev , off_t offset , void * data , size_t len )
172
167
{
173
168
uint8_t reg ;
174
169
uint16_t * dst = data ;
@@ -189,7 +184,6 @@ int tmp11x_eeprom_read(const struct device *dev, off_t offset, void *data,
189
184
return res ;
190
185
}
191
186
192
-
193
187
/**
194
188
* @brief Check the Device ID
195
189
*
@@ -202,39 +196,34 @@ int tmp11x_eeprom_read(const struct device *dev, off_t offset, void *data,
202
196
static inline int tmp11x_device_id_check (const struct device * dev , uint16_t * id )
203
197
{
204
198
if (tmp11x_reg_read (dev , TMP11X_REG_DEVICE_ID , id ) != 0 ) {
205
- LOG_ERR ("%s: Failed to get Device ID register!" ,
206
- dev -> name );
199
+ LOG_ERR ("%s: Failed to get Device ID register!" , dev -> name );
207
200
return - EIO ;
208
201
}
209
202
210
203
if ((* id != TMP116_DEVICE_ID ) && (* id != TMP117_DEVICE_ID ) && (* id != TMP119_DEVICE_ID )) {
211
- LOG_ERR ("%s: Failed to match the device IDs!" ,
212
- dev -> name );
204
+ LOG_ERR ("%s: Failed to match the device IDs!" , dev -> name );
213
205
return - EINVAL ;
214
206
}
215
207
216
208
return 0 ;
217
209
}
218
210
219
- static int tmp11x_sample_fetch (const struct device * dev ,
220
- enum sensor_channel chan )
211
+ static int tmp11x_sample_fetch (const struct device * dev , enum sensor_channel chan )
221
212
{
222
213
struct tmp11x_data * drv_data = dev -> data ;
223
214
uint16_t value ;
224
215
uint16_t cfg_reg = 0 ;
225
216
int rc ;
226
217
227
- __ASSERT_NO_MSG (chan == SENSOR_CHAN_ALL ||
228
- chan == SENSOR_CHAN_AMBIENT_TEMP );
218
+ __ASSERT_NO_MSG (chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_AMBIENT_TEMP );
229
219
230
220
/* clear sensor values */
231
221
drv_data -> sample = 0U ;
232
222
233
223
/* Make sure that a data is available */
234
224
rc = tmp11x_reg_read (dev , TMP11X_REG_CFGR , & cfg_reg );
235
225
if (rc < 0 ) {
236
- LOG_ERR ("%s, Failed to read from CFGR register" ,
237
- dev -> name );
226
+ LOG_ERR ("%s, Failed to read from CFGR register" , dev -> name );
238
227
return rc ;
239
228
}
240
229
@@ -246,8 +235,7 @@ static int tmp11x_sample_fetch(const struct device *dev,
246
235
/* Get the most recent temperature measurement */
247
236
rc = tmp11x_reg_read (dev , TMP11X_REG_TEMP , & value );
248
237
if (rc < 0 ) {
249
- LOG_ERR ("%s: Failed to read from TEMP register!" ,
250
- dev -> name );
238
+ LOG_ERR ("%s: Failed to read from TEMP register!" , dev -> name );
251
239
return rc ;
252
240
}
253
241
@@ -270,8 +258,7 @@ static void tmp11x_temperature_to_sensor_value(int16_t temperature, struct senso
270
258
val -> val2 = tmp % 1000000 ;
271
259
}
272
260
273
- static int tmp11x_channel_get (const struct device * dev ,
274
- enum sensor_channel chan ,
261
+ static int tmp11x_channel_get (const struct device * dev , enum sensor_channel chan ,
275
262
struct sensor_value * val )
276
263
{
277
264
struct tmp11x_data * drv_data = dev -> data ;
@@ -314,7 +301,7 @@ static int16_t tmp11x_conv_value(const struct sensor_value *val)
314
301
315
302
static bool tmp11x_is_attr_store_supported (enum sensor_attribute attr )
316
303
{
317
- switch ((int ) attr ) {
304
+ switch ((int )attr ) {
318
305
case SENSOR_ATTR_SAMPLING_FREQUENCY :
319
306
case SENSOR_ATTR_LOWER_THRESH :
320
307
case SENSOR_ATTR_UPPER_THRESH :
@@ -342,10 +329,8 @@ static int tmp11x_attr_store_reload(const struct device *dev)
342
329
return await_res != 0 ? await_res : reset_res ;
343
330
}
344
331
345
- static int tmp11x_attr_set (const struct device * dev ,
346
- enum sensor_channel chan ,
347
- enum sensor_attribute attr ,
348
- const struct sensor_value * val )
332
+ static int tmp11x_attr_set (const struct device * dev , enum sensor_channel chan ,
333
+ enum sensor_attribute attr , const struct sensor_value * val )
349
334
{
350
335
const struct tmp11x_dev_config * cfg = dev -> config ;
351
336
struct tmp11x_data * drv_data = dev -> data ;
@@ -435,7 +420,7 @@ static int tmp11x_attr_set(const struct device *dev,
435
420
case SENSOR_ATTR_TMP11X_ALERT_PIN_POLARITY :
436
421
if (val -> val1 == TMP11X_ALERT_PIN_ACTIVE_HIGH ) {
437
422
res = tmp11x_write_config (dev , TMP11X_CFGR_ALERT_PIN_POL ,
438
- TMP11X_CFGR_ALERT_PIN_POL );
423
+ TMP11X_CFGR_ALERT_PIN_POL );
439
424
} else {
440
425
res = tmp11x_write_config (dev , TMP11X_CFGR_ALERT_PIN_POL , 0 );
441
426
}
@@ -444,7 +429,7 @@ static int tmp11x_attr_set(const struct device *dev,
444
429
case SENSOR_ATTR_TMP11X_ALERT_MODE :
445
430
if (val -> val1 == TMP11X_ALERT_THERM_MODE ) {
446
431
res = tmp11x_write_config (dev , TMP11X_CFGR_ALERT_MODE ,
447
- TMP11X_CFGR_ALERT_MODE );
432
+ TMP11X_CFGR_ALERT_MODE );
448
433
} else {
449
434
res = tmp11x_write_config (dev , TMP11X_CFGR_ALERT_MODE , 0 );
450
435
}
@@ -467,7 +452,7 @@ static int tmp11x_attr_set(const struct device *dev,
467
452
res = tmp11x_write_config (dev , TMP11X_CFGR_ALERT_DR_SEL , 0 );
468
453
} else {
469
454
res = tmp11x_write_config (dev , TMP11X_CFGR_ALERT_DR_SEL ,
470
- TMP11X_CFGR_ALERT_DR_SEL );
455
+ TMP11X_CFGR_ALERT_DR_SEL );
471
456
}
472
457
break ;
473
458
#endif /* CONFIG_TMP11X_TRIGGER */
@@ -643,20 +628,27 @@ static int tmp11x_pm_control(const struct device *dev, enum pm_device_action act
643
628
}
644
629
#endif /* CONFIG_PM_DEVICE */
645
630
646
- #define DEFINE_TMP11X (_num ) \
647
- static struct tmp11x_data tmp11x_data_##_num; \
648
- static const struct tmp11x_dev_config tmp11x_config_##_num = { \
631
+ #ifdef CONFIG_TMP11X_TRIGGER
632
+ #define DEFINE_TMP11X_TRIGGER (_num ) .alert_gpio = GPIO_DT_SPEC_INST_GET_OR(_num, alert_gpios, {}),
633
+ #else
634
+ #define DEFINE_TMP11X_TRIGGER (_num )
635
+ #endif
636
+
637
+ #define DEFINE_TMP11X (_num ) \
638
+ static struct tmp11x_data tmp11x_data_##_num; \
639
+ static const struct tmp11x_dev_config tmp11x_config_##_num = { \
649
640
.bus = I2C_DT_SPEC_INST_GET(_num), \
650
641
.odr = DT_INST_PROP(_num, odr), \
651
642
.oversampling = DT_INST_PROP(_num, oversampling), \
652
643
.alert_pin_polarity = DT_INST_PROP(_num, alert_polarity), \
653
644
.alert_mode = DT_INST_PROP(_num, alert_mode), \
654
645
.alert_dr_sel = DT_INST_PROP(_num, alert_dr_sel), \
655
646
.store_attr_values = DT_INST_PROP(_num, store_attr_values), \
656
- IF_ENABLED(CONFIG_TMP11X_TRIGGER, \
657
- (.alert_gpio = GPIO_DT_SPEC_INST_GET_OR(_num, alert_gpios, {}),)) }; \
658
- PM_DEVICE_DT_INST_DEFINE(_num, tmp11x_pm_control); \
659
- SENSOR_DEVICE_DT_INST_DEFINE(_num, tmp11x_init, PM_DEVICE_DT_INST_GET(_num), \
647
+ DEFINE_TMP11X_TRIGGER(_num)}; \
648
+ \
649
+ PM_DEVICE_DT_INST_DEFINE(_num, tmp11x_pm_control); \
650
+ \
651
+ SENSOR_DEVICE_DT_INST_DEFINE(_num, tmp11x_init, PM_DEVICE_DT_INST_GET(_num), \
660
652
&tmp11x_data_##_num, &tmp11x_config_##_num, POST_KERNEL, \
661
653
CONFIG_SENSOR_INIT_PRIORITY, &tmp11x_driver_api);
662
654
0 commit comments