@@ -58,138 +58,103 @@ static void ep93xx_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
5858 ep93xx_pwm_release_gpio (pdev );
5959}
6060
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-
15961static int ep93xx_pwm_apply (struct pwm_chip * chip , struct pwm_device * pwm ,
16062 const struct pwm_state * state )
16163{
16264 int ret ;
65+ struct ep93xx_pwm * ep93xx_pwm = to_ep93xx_pwm (chip );
16366 bool enabled = state -> enabled ;
16467
16568 if (state -> polarity != pwm -> state .polarity ) {
16669 if (enabled ) {
167- ep93xx_pwm_disable (chip , pwm );
70+ writew (0x0 , ep93xx_pwm -> base + EP93XX_PWMx_ENABLE );
71+ clk_disable (ep93xx_pwm -> clk );
16872 enabled = false;
16973 }
17074
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 );
17280 if (ret )
17381 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 );
17489 }
17590
17691 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+ }
17996
18097 return 0 ;
18198 }
18299
183100 if (state -> period != pwm -> state .period ||
184101 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+
187147 if (ret )
188148 return ret ;
189149 }
190150
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+ }
193158
194159 return 0 ;
195160}
0 commit comments