Skip to content

Commit ea6ad73

Browse files
aviscontiMaureenHelm
authored andcommitted
drivers/sensor: lis2ds12: Move odr Kconfig property into dts
Move odr options from Kconfigs to Device Tree. Moreover add in DT a power-mode option to select among 4 possible values (PD, LP, HR, HF). The power mode cannot be currently set from sensor APIs. Signed-off-by: Armando Visconti <[email protected]>
1 parent e0f06e2 commit ea6ad73

File tree

4 files changed

+106
-31
lines changed

4 files changed

+106
-31
lines changed

drivers/sensor/lis2ds12/Kconfig

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,4 @@ config LIS2DS12_ENABLE_TEMP
5555
help
5656
Enable/disable temperature
5757

58-
menu "Attributes"
59-
60-
config LIS2DS12_ODR
61-
int "Accelerometer Output data rate frequency"
62-
range 0 10
63-
default 0
64-
help
65-
Specify the default accelerometer output data rate expressed in
66-
samples per second (Hz).
67-
0: ODR selected at runtime
68-
1: 12.5Hz
69-
2: 25Hz
70-
3: 50Hz
71-
4: 100Hz
72-
5: 200Hz
73-
6: 400Hz
74-
7: 800Hz
75-
endmenu
76-
7758
endif # LIS2DS12

drivers/sensor/lis2ds12/lis2ds12.c

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,73 @@
2323

2424
LOG_MODULE_REGISTER(LIS2DS12, CONFIG_SENSOR_LOG_LEVEL);
2525

26-
static int lis2ds12_set_odr(const struct device *dev, uint16_t odr)
26+
static int lis2ds12_set_odr(const struct device *dev, uint8_t odr)
2727
{
2828
const struct lis2ds12_config *cfg = dev->config;
2929
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
30-
uint8_t val;
30+
lis2ds12_odr_t val;
3131

3232
/* check if power off */
3333
if (odr == 0U) {
34+
LOG_DBG("%s: set power-down", dev->name);
3435
return lis2ds12_xl_data_rate_set(ctx, LIS2DS12_XL_ODR_OFF);
3536
}
3637

37-
val = LIS2DS12_HR_ODR_TO_REG(odr);
38-
if (val > LIS2DS12_XL_ODR_800Hz_HR) {
39-
LOG_ERR("ODR too high");
40-
return -EINVAL;
38+
/*
39+
* odr >= 1600Hz are available in HF mode only
40+
* 12,5Hz <= odr <= 800Hz are available in LP and HR mode only
41+
* odr == 1Hz is available in LP mode only
42+
*/
43+
if ((odr >= 9 && cfg->pm != 3) || (odr < 9 && cfg->pm == 3) ||
44+
(odr == 1 && cfg->pm != 1)) {
45+
LOG_ERR("%s: bad odr and pm combination", dev->name);
46+
return -ENOTSUP;
47+
}
48+
49+
switch (odr) {
50+
case 1:
51+
val = LIS2DS12_XL_ODR_1Hz_LP;
52+
break;
53+
case 2:
54+
val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_12Hz5_LP :
55+
LIS2DS12_XL_ODR_12Hz5_HR;
56+
break;
57+
case 3:
58+
val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_25Hz_LP :
59+
LIS2DS12_XL_ODR_25Hz_HR;
60+
break;
61+
case 4:
62+
val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_50Hz_LP :
63+
LIS2DS12_XL_ODR_50Hz_HR;
64+
break;
65+
case 5:
66+
val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_100Hz_LP :
67+
LIS2DS12_XL_ODR_100Hz_HR;
68+
break;
69+
case 6:
70+
val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_200Hz_LP :
71+
LIS2DS12_XL_ODR_200Hz_HR;
72+
break;
73+
case 7:
74+
val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_400Hz_LP :
75+
LIS2DS12_XL_ODR_400Hz_HR;
76+
break;
77+
case 8:
78+
val = (cfg->pm == 1) ? LIS2DS12_XL_ODR_800Hz_LP :
79+
LIS2DS12_XL_ODR_800Hz_HR;
80+
break;
81+
case 9:
82+
val = LIS2DS12_XL_ODR_1k6Hz_HF;
83+
break;
84+
case 10:
85+
val = LIS2DS12_XL_ODR_3k2Hz_HF;
86+
break;
87+
case 11:
88+
val = LIS2DS12_XL_ODR_6k4Hz_HF;
89+
break;
90+
default:
91+
LOG_ERR("%s: bad odr %d", dev->name, odr);
92+
return -ENOTSUP;
4193
}
4294

4395
return lis2ds12_xl_data_rate_set(ctx, val);
@@ -82,7 +134,8 @@ static int lis2ds12_accel_config(const struct device *dev,
82134
case SENSOR_ATTR_FULL_SCALE:
83135
return lis2ds12_set_range(dev, sensor_ms2_to_g(val));
84136
case SENSOR_ATTR_SAMPLING_FREQUENCY:
85-
return lis2ds12_set_odr(dev, val->val1);
137+
LOG_DBG("%s: set odr to %d Hz", dev->name, val->val1);
138+
return lis2ds12_set_odr(dev, LIS2DS12_ODR_TO_REG(val->val1));
86139
default:
87140
LOG_DBG("Accel attribute not supported.");
88141
return -ENOTSUP;
@@ -250,8 +303,9 @@ static int lis2ds12_init(const struct device *dev)
250303
}
251304
#endif
252305

253-
/* set sensor default odr */
254-
ret = lis2ds12_set_odr(dev, 12);
306+
/* set sensor default pm and odr */
307+
LOG_DBG("%s: pm: %d, odr: %d", dev->name, cfg->pm, cfg->odr);
308+
ret = lis2ds12_set_odr(dev, (cfg->pm == 0) ? 0 : cfg->odr);
255309
if (ret < 0) {
256310
LOG_ERR("%s: odr init error (12.5 Hz)", dev->name);
257311
return ret;
@@ -319,6 +373,8 @@ static int lis2ds12_init(const struct device *dev)
319373
0), \
320374
}, \
321375
.range = DT_INST_PROP(inst, range), \
376+
.pm = DT_INST_PROP(inst, power_mode), \
377+
.odr = DT_INST_PROP(inst, odr), \
322378
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
323379
(LIS2DS12_CFG_IRQ(inst)), ()) \
324380
}
@@ -341,6 +397,8 @@ static int lis2ds12_init(const struct device *dev)
341397
.i2c = I2C_DT_SPEC_INST_GET(inst), \
342398
}, \
343399
.range = DT_INST_PROP(inst, range), \
400+
.pm = DT_INST_PROP(inst, power_mode), \
401+
.odr = DT_INST_PROP(inst, odr), \
344402
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, irq_gpios), \
345403
(LIS2DS12_CFG_IRQ(inst)), ()) \
346404
}

drivers/sensor/lis2ds12/lis2ds12.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
#endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
2727

2828
/* Return ODR reg value based on data rate set */
29-
#define LIS2DS12_HR_ODR_TO_REG(_odr) \
30-
((_odr <= 12) ? LIS2DS12_XL_ODR_12Hz5_HR : \
31-
((31 - __builtin_clz(_odr / 25))) + 2)
29+
#define LIS2DS12_ODR_TO_REG(_odr) \
30+
((_odr <= 1) ? 1 : \
31+
((31 - __builtin_clz(_odr / 25))) + 3)
3232

3333
struct lis2ds12_config {
3434
stmdev_ctx_t ctx;
@@ -41,6 +41,8 @@ struct lis2ds12_config {
4141
#endif
4242
} stmemsc_cfg;
4343
uint8_t range;
44+
uint8_t pm;
45+
uint8_t odr;
4446
#ifdef CONFIG_LIS2DS12_TRIGGER
4547
struct gpio_dt_spec gpio_int;
4648
#endif

dts/bindings/sensor/st,lis2ds12-common.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,37 @@ properties:
2424
- 8 # 8g (0.244 mg/LSB)
2525
- 4 # 4g (0.122 mg/LSB)
2626
- 2 # 2g (0.061 mg/LSB)
27+
28+
power-mode:
29+
type: int
30+
required: false
31+
default: 0
32+
description: |
33+
Specify the sensor power mode. Default is power-down mode
34+
35+
enum:
36+
- 0 # Power Down (PD)
37+
- 1 # Low Power (LP)
38+
- 2 # High Resolution (HR)
39+
- 3 # High Frequency (HF)
40+
41+
odr:
42+
type: int
43+
required: false
44+
default: 0
45+
description: |
46+
Specify the default output data rate expressed in samples per second (Hz).
47+
Default is power-down mode
48+
enum:
49+
- 0 # Power-Down
50+
- 1 # 1Hz (available in LP mode only)
51+
- 2 # 12.5Hz (available in LP and HR mode)
52+
- 3 # 25Hz (available in LP and HR mode)
53+
- 4 # 50Hz (available in LP and HR mode)
54+
- 5 # 100Hz (available in LP and HR mode)
55+
- 6 # 200Hz (available in LP and HR mode)
56+
- 7 # 400Hz (available in LP and HR mode)
57+
- 8 # 800Hz (available in LP and HR mode)
58+
- 9 # 1600Hz (available in HF mode only)
59+
- 10 # 3200Hz (available in HF mode only)
60+
- 11 # 6400Hz (available in HF mode only)

0 commit comments

Comments
 (0)