Skip to content

Commit 34ac008

Browse files
AdithyaBaglodyAnas Nashif
authored andcommitted
drivers: pwm: remove deprecated PWM API usage.
Removed the old PWM APIs usage and added support for the new PWM API. JIRA: ZEP-2018 Signed-off-by: Adithya Baglody <[email protected]> Signed-off-by: Anas Nashif <[email protected]>
1 parent 5fc31b8 commit 34ac008

File tree

3 files changed

+43
-315
lines changed

3 files changed

+43
-315
lines changed

drivers/pwm/pwm_dw.c

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,6 @@ static inline int pwm_dw_timer_ldcnt2_addr(struct device *dev, u32_t timer)
9696
}
9797

9898

99-
static int pwm_dw_configure(struct device *dev, int access_op,
100-
u32_t pwm, int flags)
101-
{
102-
ARG_UNUSED(dev);
103-
ARG_UNUSED(access_op);
104-
ARG_UNUSED(pwm);
105-
ARG_UNUSED(flags);
106-
107-
return 0;
108-
}
109-
11099
static int __set_one_port(struct device *dev, u32_t pwm,
111100
u32_t on, u32_t off)
112101
{
@@ -135,68 +124,51 @@ static int __set_one_port(struct device *dev, u32_t pwm,
135124
}
136125

137126
/**
138-
* Set the duration for on/off timer of PWM.
127+
* Set the period and the pulse of PWM.
139128
*
140-
* This sets the duration for the pin to low or high.
141129
*
142130
* Assumes a nominal system clock of 32MHz, each count of on/off represents
143131
* 31.25ns (e.g. on == 2 means the pin stays high for 62.5ns).
144132
* The duration of 1 count depends on system clock. Refer to the hardware
145133
* manual for more information.
146134
*
147135
* @param dev Device struct
148-
* @param access_op whether to set one pin or all
149-
* @param pwm Which PWM port to set, 0 if addressing all
150-
* @param on Duration for pin to stay high (must be >= 2)
151-
* @param off Duration for pin to stay low (must be >= 2)
136+
* @param pwm Which PWM pin to set
137+
* @param period_cycles Period in clock cycles of the pwm.
138+
* @param pulse_cycles PWM width in clock cycles
152139
*
153140
* @return 0
154141
*/
155-
static int pwm_dw_set_values(struct device *dev, int access_op,
156-
u32_t pwm, u32_t on, u32_t off)
142+
static int pwm_dw_pin_set_cycles(struct device *dev,
143+
u32_t pwm, u32_t period_cycles, u32_t pulse_cycles)
157144
{
158145
const struct pwm_dw_config * const cfg =
159146
(struct pwm_dw_config *)dev->config->config_info;
160147
int i;
148+
u32_t on, off;
161149

162-
switch (access_op) {
163-
case PWM_ACCESS_BY_PIN:
164-
/* make sure the PWM port exists */
165-
if (pwm >= cfg->num_ports) {
166-
return -EIO;
167-
}
168-
169-
return __set_one_port(dev, pwm, on, off);
170-
case PWM_ACCESS_ALL:
171-
for (i = 0; i < cfg->num_ports; i++) {
172-
__set_one_port(dev, i, on, off);
173-
}
174-
175-
return 0;
150+
/* make sure the PWM port exists */
151+
if (pwm >= cfg->num_ports) {
152+
return -EIO;
176153
}
177154

178-
return -ENOTSUP;
179-
}
155+
if (period_cycles == 0 || pulse_cycles > period_cycles) {
156+
return -EINVAL;
157+
}
158+
on = pulse_cycles;
159+
off = period_cycles - pulse_cycles;
180160

181-
static int pwm_dw_set_duty_cycle(struct device *dev, int access_op,
182-
u32_t pwm, u8_t duty)
183-
{
184-
/* The IP block does not natively support duty cycle settings.
185-
* So need to use set_values().
186-
**/
161+
if (off == 0) {
162+
on--;
163+
off++;
164+
}
187165

188-
ARG_UNUSED(dev);
189-
ARG_UNUSED(access_op);
190-
ARG_UNUSED(pwm);
191-
ARG_UNUSED(duty);
166+
return __set_one_port(dev, pwm, on, off);
192167

193-
return -ENOTSUP;
194168
}
195169

196170
static struct pwm_driver_api pwm_dw_drv_api_funcs = {
197-
.config = pwm_dw_configure,
198-
.set_values = pwm_dw_set_values,
199-
.set_duty_cycle = pwm_dw_set_duty_cycle,
171+
.pin_set = pwm_dw_pin_set_cycles,
200172
};
201173

202174
/**

drivers/pwm/pwm_pca9685.c

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,12 @@ static inline int _has_i2c_master(struct device *dev)
5555
return 0;
5656
}
5757

58-
static int pwm_pca9685_configure(struct device *dev, int access_op,
59-
u32_t pwm, int flags)
60-
{
61-
ARG_UNUSED(dev);
62-
ARG_UNUSED(access_op);
63-
ARG_UNUSED(pwm);
64-
ARG_UNUSED(flags);
65-
66-
return 0;
67-
}
68-
69-
static int pwm_pca9685_set_values(struct device *dev, int access_op,
70-
u32_t pwm, u32_t on, u32_t off)
58+
/*
59+
* period_count is always taken as 4095. To control the on period send
60+
* value to pulse_count
61+
*/
62+
static int pwm_pca9685_pin_set_cycles(struct device *dev, u32_t pwm,
63+
u32_t period_count, u32_t pulse_count)
7164
{
7265
const struct pwm_pca9685_config * const config =
7366
dev->config->config_info;
@@ -77,79 +70,46 @@ static int pwm_pca9685_set_values(struct device *dev, int access_op,
7770
u16_t i2c_addr = config->i2c_slave_addr;
7871
u8_t buf[] = { 0, 0, 0, 0, 0};
7972

73+
ARG_UNUSED(period_count);
8074
if (!_has_i2c_master(dev)) {
8175
return -EINVAL;
8276
}
8377

84-
switch (access_op) {
85-
case PWM_ACCESS_BY_PIN:
86-
if (pwm > MAX_PWM_OUT) {
87-
return -EINVAL;
88-
}
89-
buf[0] = REG_LED_ON_L(pwm);
90-
break;
91-
case PWM_ACCESS_ALL:
92-
buf[0] = REG_ALL_LED_ON_L;
93-
break;
94-
default:
95-
return -ENOTSUP;
78+
if (pwm > MAX_PWM_OUT) {
79+
return -EINVAL;
9680
}
81+
buf[0] = REG_LED_ON_L(pwm);
9782

98-
/* If either ON and/or OFF > max ticks, treat PWM as 100%.
99-
* If OFF value == 0, treat it as 0%.
83+
/* If either pulse_count > max ticks, treat PWM as 100%.
84+
* If pulse_count value == 0, treat it as 0%.
10085
* Otherwise, populate registers accordingly.
10186
*/
102-
if ((on >= PWM_ONE_PERIOD_TICKS) || (off >= PWM_ONE_PERIOD_TICKS)) {
87+
if (pulse_count >= PWM_ONE_PERIOD_TICKS) {
10388
buf[1] = 0x0;
10489
buf[2] = (1 << 4);
10590
buf[3] = 0x0;
10691
buf[4] = 0x0;
107-
} else if (off == 0) {
92+
} else if (pulse_count == 0) {
10893
buf[1] = 0x0;
10994
buf[2] = 0x0;
11095
buf[3] = 0x0;
11196
buf[4] = (1 << 4);
11297
} else {
113-
buf[1] = (on & 0xFF);
114-
buf[2] = ((on >> 8) & 0x0F);
115-
buf[3] = (off & 0xFF);
116-
buf[4] = ((off >> 8) & 0x0F);
98+
/* No start delay given. When the count is 0 the
99+
* pwm will be high .
100+
*/
101+
buf[0] = 0x0;
102+
buf[1] = 0x0;
103+
buf[2] = (pulse_count & 0xFF);
104+
buf[3] = ((pulse_count >> 8) & 0x0F);
117105
}
118106

119107
return i2c_write(i2c_master, buf, sizeof(buf), i2c_addr);
120108
}
121109

122-
/**
123-
* Duty cycle describes the percentage of time a signal is turned
124-
* to the ON state.
125-
*/
126-
static int pwm_pca9685_set_duty_cycle(struct device *dev, int access_op,
127-
u32_t pwm, u8_t duty)
128-
{
129-
u32_t on, off, phase;
130-
131-
phase = 0; /* Hard coded until API changes */
132-
133-
if (duty == 0) {
134-
/* Turn off PWM */
135-
on = 0;
136-
off = 0;
137-
} else if (duty >= 100) {
138-
/* Force PWM to be 100% */
139-
on = PWM_ONE_PERIOD_TICKS + 1;
140-
off = PWM_ONE_PERIOD_TICKS + 1;
141-
} else {
142-
off = PWM_ONE_PERIOD_TICKS * duty / 100;
143-
on = phase;
144-
}
145-
146-
return pwm_pca9685_set_values(dev, access_op, pwm, on, off);
147-
}
148110

149111
static const struct pwm_driver_api pwm_pca9685_drv_api_funcs = {
150-
.config = pwm_pca9685_configure,
151-
.set_values = pwm_pca9685_set_values,
152-
.set_duty_cycle = pwm_pca9685_set_duty_cycle,
112+
.pin_set = pwm_pca9685_pin_set_cycles
153113
};
154114

155115
/**

0 commit comments

Comments
 (0)