@@ -58,138 +58,103 @@ static void ep93xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
58
58
ep93xx_pwm_release_gpio (pdev );
59
59
}
60
60
61
- static int ep93xx_pwm_config (struct pwm_chip * chip , struct pwm_device * pwm ,
62
- int duty_ns , int period_ns )
63
- {
64
- struct ep93xx_pwm * ep93xx_pwm = to_ep93xx_pwm (chip );
65
- void __iomem * base = ep93xx_pwm -> base ;
66
- unsigned long long c ;
67
- unsigned long period_cycles ;
68
- unsigned long duty_cycles ;
69
- unsigned long term ;
70
- int ret = 0 ;
71
-
72
- /*
73
- * The clock needs to be enabled to access the PWM registers.
74
- * Configuration can be changed at any time.
75
- */
76
- if (!pwm_is_enabled (pwm )) {
77
- ret = clk_enable (ep93xx_pwm -> clk );
78
- if (ret )
79
- return ret ;
80
- }
81
-
82
- c = clk_get_rate (ep93xx_pwm -> clk );
83
- c *= period_ns ;
84
- do_div (c , 1000000000 );
85
- period_cycles = c ;
86
-
87
- c = period_cycles ;
88
- c *= duty_ns ;
89
- do_div (c , period_ns );
90
- duty_cycles = c ;
91
-
92
- if (period_cycles < 0x10000 && duty_cycles < 0x10000 ) {
93
- term = readw (base + EP93XX_PWMx_TERM_COUNT );
94
-
95
- /* Order is important if PWM is running */
96
- if (period_cycles > term ) {
97
- writew (period_cycles , base + EP93XX_PWMx_TERM_COUNT );
98
- writew (duty_cycles , base + EP93XX_PWMx_DUTY_CYCLE );
99
- } else {
100
- writew (duty_cycles , base + EP93XX_PWMx_DUTY_CYCLE );
101
- writew (period_cycles , base + EP93XX_PWMx_TERM_COUNT );
102
- }
103
- } else {
104
- ret = - EINVAL ;
105
- }
106
-
107
- if (!pwm_is_enabled (pwm ))
108
- clk_disable (ep93xx_pwm -> clk );
109
-
110
- return ret ;
111
- }
112
-
113
- static int ep93xx_pwm_polarity (struct pwm_chip * chip , struct pwm_device * pwm ,
114
- enum pwm_polarity polarity )
115
- {
116
- struct ep93xx_pwm * ep93xx_pwm = to_ep93xx_pwm (chip );
117
- int ret ;
118
-
119
- /*
120
- * The clock needs to be enabled to access the PWM registers.
121
- * Polarity can only be changed when the PWM is disabled.
122
- */
123
- ret = clk_enable (ep93xx_pwm -> clk );
124
- if (ret )
125
- return ret ;
126
-
127
- if (polarity == PWM_POLARITY_INVERSED )
128
- writew (0x1 , ep93xx_pwm -> base + EP93XX_PWMx_INVERT );
129
- else
130
- writew (0x0 , ep93xx_pwm -> base + EP93XX_PWMx_INVERT );
131
-
132
- clk_disable (ep93xx_pwm -> clk );
133
-
134
- return 0 ;
135
- }
136
-
137
- static int ep93xx_pwm_enable (struct pwm_chip * chip , struct pwm_device * pwm )
138
- {
139
- struct ep93xx_pwm * ep93xx_pwm = to_ep93xx_pwm (chip );
140
- int ret ;
141
-
142
- ret = clk_enable (ep93xx_pwm -> clk );
143
- if (ret )
144
- return ret ;
145
-
146
- writew (0x1 , ep93xx_pwm -> base + EP93XX_PWMx_ENABLE );
147
-
148
- return 0 ;
149
- }
150
-
151
- static void ep93xx_pwm_disable (struct pwm_chip * chip , struct pwm_device * pwm )
152
- {
153
- struct ep93xx_pwm * ep93xx_pwm = to_ep93xx_pwm (chip );
154
-
155
- writew (0x0 , ep93xx_pwm -> base + EP93XX_PWMx_ENABLE );
156
- clk_disable (ep93xx_pwm -> clk );
157
- }
158
-
159
61
static int ep93xx_pwm_apply (struct pwm_chip * chip , struct pwm_device * pwm ,
160
62
const struct pwm_state * state )
161
63
{
162
64
int ret ;
65
+ struct ep93xx_pwm * ep93xx_pwm = to_ep93xx_pwm (chip );
163
66
bool enabled = state -> enabled ;
164
67
165
68
if (state -> polarity != pwm -> state .polarity ) {
166
69
if (enabled ) {
167
- ep93xx_pwm_disable (chip , pwm );
70
+ writew (0x0 , ep93xx_pwm -> base + EP93XX_PWMx_ENABLE );
71
+ clk_disable (ep93xx_pwm -> clk );
168
72
enabled = false;
169
73
}
170
74
171
- ret = ep93xx_pwm_polarity (chip , pwm , state -> polarity );
75
+ /*
76
+ * The clock needs to be enabled to access the PWM registers.
77
+ * Polarity can only be changed when the PWM is disabled.
78
+ */
79
+ ret = clk_enable (ep93xx_pwm -> clk );
172
80
if (ret )
173
81
return ret ;
82
+
83
+ if (state -> polarity == PWM_POLARITY_INVERSED )
84
+ writew (0x1 , ep93xx_pwm -> base + EP93XX_PWMx_INVERT );
85
+ else
86
+ writew (0x0 , ep93xx_pwm -> base + EP93XX_PWMx_INVERT );
87
+
88
+ clk_disable (ep93xx_pwm -> clk );
174
89
}
175
90
176
91
if (!state -> enabled ) {
177
- if (enabled )
178
- ep93xx_pwm_disable (chip , pwm );
92
+ if (enabled ) {
93
+ writew (0x0 , ep93xx_pwm -> base + EP93XX_PWMx_ENABLE );
94
+ clk_disable (ep93xx_pwm -> clk );
95
+ }
179
96
180
97
return 0 ;
181
98
}
182
99
183
100
if (state -> period != pwm -> state .period ||
184
101
state -> duty_cycle != pwm -> state .duty_cycle ) {
185
- ret = ep93xx_pwm_config (chip , pwm , (int )state -> duty_cycle ,
186
- (int )state -> period );
102
+ struct ep93xx_pwm * ep93xx_pwm = to_ep93xx_pwm (chip );
103
+ void __iomem * base = ep93xx_pwm -> base ;
104
+ unsigned long long c ;
105
+ unsigned long period_cycles ;
106
+ unsigned long duty_cycles ;
107
+ unsigned long term ;
108
+
109
+ /*
110
+ * The clock needs to be enabled to access the PWM registers.
111
+ * Configuration can be changed at any time.
112
+ */
113
+ if (!pwm_is_enabled (pwm )) {
114
+ ret = clk_enable (ep93xx_pwm -> clk );
115
+ if (ret )
116
+ return ret ;
117
+ }
118
+
119
+ c = clk_get_rate (ep93xx_pwm -> clk );
120
+ c *= state -> period ;
121
+ do_div (c , 1000000000 );
122
+ period_cycles = c ;
123
+
124
+ c = period_cycles ;
125
+ c *= state -> duty_cycle ;
126
+ do_div (c , state -> period );
127
+ duty_cycles = c ;
128
+
129
+ if (period_cycles < 0x10000 && duty_cycles < 0x10000 ) {
130
+ term = readw (base + EP93XX_PWMx_TERM_COUNT );
131
+
132
+ /* Order is important if PWM is running */
133
+ if (period_cycles > term ) {
134
+ writew (period_cycles , base + EP93XX_PWMx_TERM_COUNT );
135
+ writew (duty_cycles , base + EP93XX_PWMx_DUTY_CYCLE );
136
+ } else {
137
+ writew (duty_cycles , base + EP93XX_PWMx_DUTY_CYCLE );
138
+ writew (period_cycles , base + EP93XX_PWMx_TERM_COUNT );
139
+ }
140
+ } else {
141
+ ret = - EINVAL ;
142
+ }
143
+
144
+ if (!pwm_is_enabled (pwm ))
145
+ clk_disable (ep93xx_pwm -> clk );
146
+
187
147
if (ret )
188
148
return ret ;
189
149
}
190
150
191
- if (!enabled )
192
- return ep93xx_pwm_enable (chip , pwm );
151
+ if (!enabled ) {
152
+ ret = clk_enable (ep93xx_pwm -> clk );
153
+ if (ret )
154
+ return ret ;
155
+
156
+ writew (0x1 , ep93xx_pwm -> base + EP93XX_PWMx_ENABLE );
157
+ }
193
158
194
159
return 0 ;
195
160
}
0 commit comments