Skip to content

Commit eb811d1

Browse files
seov-nordickartben
authored andcommitted
drivers: mfd: npm2100: fix timer value calculation
A -1 was missing from the timer value calculation. In addition, DIV_ROUND_CLOSEST was replaced with a custom macro fixing #84782. Signed-off-by: Sergei Ovchinnikov <[email protected]>
1 parent bcf7717 commit eb811d1

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

drivers/mfd/mfd_npm2100.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@
4949

5050
#define TIMER_STATUS_IDLE 0U
5151

52-
#define TIMER_PRESCALER_MUL 64ULL
53-
#define TIMER_PRESCALER_DIV 1000ULL
52+
#define TIMER_PRESCALER_MUL 64
53+
#define TIMER_PRESCALER_DIV 1000
5454
#define TIMER_MAX 0xFFFFFFU
5555

56+
/* Formula for timer value: Value = ROUND[time_ms / 15.625] - 1 */
57+
#define TIMER_MS_TO_TICKS(t) \
58+
(((int64_t)(t) * TIMER_PRESCALER_MUL + TIMER_PRESCALER_DIV / 2) / TIMER_PRESCALER_DIV - 1)
59+
5660
#define EVENTS_SIZE 5U
5761

5862
#define GPIO_USAGE_INTLO 0x01U
@@ -304,12 +308,12 @@ int mfd_npm2100_set_timer(const struct device *dev, uint32_t time_ms,
304308
{
305309
const struct mfd_npm2100_config *config = dev->config;
306310
uint8_t buff[4] = {TIMER_TARGET};
307-
uint32_t ticks = (uint32_t)DIV_ROUND_CLOSEST(((uint64_t)time_ms * TIMER_PRESCALER_MUL),
308-
TIMER_PRESCALER_DIV);
311+
int64_t ticks = TIMER_MS_TO_TICKS(time_ms);
312+
309313
uint8_t timer_status;
310314
int ret;
311315

312-
if (ticks > TIMER_MAX) {
316+
if (ticks > TIMER_MAX || ticks < 0) {
313317
return -EINVAL;
314318
}
315319

@@ -322,7 +326,7 @@ int mfd_npm2100_set_timer(const struct device *dev, uint32_t time_ms,
322326
return -EBUSY;
323327
}
324328

325-
sys_put_be24(ticks, &buff[1]);
329+
sys_put_be24((uint32_t)ticks, &buff[1]);
326330

327331
ret = i2c_write_dt(&config->i2c, buff, sizeof(buff));
328332
if (ret < 0) {

0 commit comments

Comments
 (0)