Skip to content

Commit 3857110

Browse files
committed
pwm: pwm_seesaw: new driver
Add new pwm driver for the Adafruit Seesaw Signed-off-by: Titouan Christophe <[email protected]>
1 parent 27220d0 commit 3857110

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

drivers/pwm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ zephyr_library_sources_ifdef(CONFIG_PWM_INFINEON_CAT1 pwm_ifx_cat1.c)
5252
zephyr_library_sources_ifdef(CONFIG_PWM_FAKE pwm_fake.c)
5353
zephyr_library_sources_ifdef(CONFIG_PWM_RENESAS_RZ_GPT pwm_renesas_rz_gpt.c)
5454
zephyr_library_sources_ifdef(CONFIG_PWM_NEORV32 pwm_neorv32.c)
55+
zephyr_library_sources_ifdef(CONFIG_PWM_SEESAW pwm_seesaw.c)
5556
zephyr_library_sources_ifdef(CONFIG_USERSPACE pwm_handlers.c)
5657
zephyr_library_sources_ifdef(CONFIG_PWM_CAPTURE pwm_capture.c)
5758
zephyr_library_sources_ifdef(CONFIG_PWM_SHELL pwm_shell.c)

drivers/pwm/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,6 @@ source "drivers/pwm/Kconfig.rts5912"
126126

127127
source "drivers/pwm/Kconfig.neorv32"
128128

129+
source "drivers/pwm/Kconfig.seesaw"
130+
129131
endif # PWM

drivers/pwm/Kconfig.seesaw

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright 2025 Titouan Chrisotphe
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config PWM_SEESAW
5+
bool "Adafruit Seesaw PWM over I2C"
6+
default y
7+
depends on MFD_SEESAW
8+
depends on DT_HAS_ADAFRUIT_SEESAW_PWM_ENABLED
9+
help
10+
Enable support for Adafruit Seesaw pwm

drivers/pwm/pwm_seesaw.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2025 Titouan Christophe
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT adafruit_seesaw_pwm
8+
9+
#include <zephyr/drivers/mfd/mfd_seesaw.h>
10+
#include <zephyr/drivers/pwm.h>
11+
12+
/* See https://learn.adafruit.com/adafruit-seesaw-atsamd09-breakout/pwm */
13+
14+
#define FIXED_CYCLES 0xffff
15+
16+
enum seesaw_mod_pwm {
17+
PWM_VAL = 0x01,
18+
PWM_FREQ = 0x02,
19+
};
20+
21+
struct seesaw_pwm_config {
22+
const struct device *seesaw;
23+
};
24+
25+
static int seesaw_pwm_get_cycles_per_sec(const struct device *dev,
26+
uint32_t channel, uint64_t *cycles)
27+
{
28+
*cycles = FIXED_CYCLES;
29+
return 0;
30+
}
31+
32+
static inline int seesaw_pwm_write(const struct device *seesaw_dev, uint8_t reg,
33+
uint8_t channel, uint16_t value)
34+
{
35+
const uint8_t buf[3] = {channel, (value >> 8), (value & 0xff)};
36+
37+
return seesaw_write(seesaw_dev, SEESAW_MOD_PWM, reg, buf, 3);
38+
}
39+
40+
static int seesaw_pwm_set_cycles(const struct device *dev, uint32_t channel,
41+
uint32_t period, uint32_t pulse,
42+
pwm_flags_t flags)
43+
{
44+
int ret;
45+
const struct seesaw_pwm_config *const config = dev->config;
46+
uint16_t freq = 0;
47+
uint16_t val = 0;
48+
49+
if (period > 0) {
50+
freq = FIXED_CYCLES / period;
51+
val = 0xffff * pulse / period;
52+
}
53+
54+
ret = seesaw_pwm_write(config->seesaw, PWM_VAL, channel, val);
55+
if (ret) {
56+
return ret;
57+
}
58+
59+
return seesaw_pwm_write(config->seesaw, PWM_FREQ, channel, freq);
60+
}
61+
62+
static DEVICE_API(pwm, seesaw_pwm_api) = {
63+
.set_cycles = seesaw_pwm_set_cycles,
64+
.get_cycles_per_sec = seesaw_pwm_get_cycles_per_sec,
65+
};
66+
67+
static int seesaw_pwm_init(const struct device *dev)
68+
{
69+
const struct seesaw_pwm_config *const config = dev->config;
70+
71+
return seesaw_claim_module(config->seesaw, SEESAW_MOD_PWM, dev);
72+
}
73+
74+
#define SEESAW_PWM_INIT(inst) \
75+
static const struct seesaw_pwm_config config_##inst = { \
76+
.seesaw = DEVICE_DT_GET(DT_INST_PARENT(inst)), \
77+
}; \
78+
DEVICE_DT_INST_DEFINE(inst, seesaw_pwm_init, NULL, NULL, &config_##inst, \
79+
POST_KERNEL, CONFIG_MFD_INIT_PRIORITY, &seesaw_pwm_api);
80+
81+
DT_INST_FOREACH_STATUS_OKAY(SEESAW_PWM_INIT)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2025 Titouan Christophe
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Adafruit Seesaw PWM
5+
6+
compatible: "adafruit,seesaw-pwm"
7+
8+
include: [pwm-controller.yaml]
9+
10+
properties:
11+
"#pwm-cells":
12+
const: 3
13+
description: |
14+
Number of items to expect in a PWM
15+
- channel
16+
- period
17+
- flags
18+
19+
pwm-cells:
20+
- channel
21+
- period
22+
- flags

0 commit comments

Comments
 (0)