@@ -204,43 +204,59 @@ static void gb_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
204204 gb_pwm_deactivate_operation (pwmc , pwm -> hwpwm );
205205}
206206
207- static int gb_pwm_config (struct pwm_chip * chip , struct pwm_device * pwm ,
208- int duty_ns , int period_ns )
207+ static int gb_pwm_apply (struct pwm_chip * chip , struct pwm_device * pwm ,
208+ const struct pwm_state * state )
209209{
210+ int err ;
211+ bool enabled = pwm -> state .enabled ;
212+ u64 period = state -> period ;
213+ u64 duty_cycle = state -> duty_cycle ;
210214 struct gb_pwm_chip * pwmc = pwm_chip_to_gb_pwm_chip (chip );
211215
212- return gb_pwm_config_operation (pwmc , pwm -> hwpwm , duty_ns , period_ns );
213- };
216+ /* Set polarity */
217+ if (state -> polarity != pwm -> state .polarity ) {
218+ if (enabled ) {
219+ gb_pwm_disable_operation (pwmc , pwm -> hwpwm );
220+ enabled = false;
221+ }
222+ err = gb_pwm_set_polarity_operation (pwmc , pwm -> hwpwm , state -> polarity );
223+ if (err )
224+ return err ;
225+ }
214226
215- static int gb_pwm_set_polarity (struct pwm_chip * chip , struct pwm_device * pwm ,
216- enum pwm_polarity polarity )
217- {
218- struct gb_pwm_chip * pwmc = pwm_chip_to_gb_pwm_chip (chip );
227+ if (!state -> enabled ) {
228+ if (enabled )
229+ gb_pwm_disable_operation (pwmc , pwm -> hwpwm );
230+ return 0 ;
231+ }
219232
220- return gb_pwm_set_polarity_operation (pwmc , pwm -> hwpwm , polarity );
221- };
233+ /*
234+ * Set period and duty cycle
235+ *
236+ * PWM privodes 64-bit period and duty_cycle, but greybus only accepts
237+ * 32-bit, so their values have to be limited to U32_MAX.
238+ */
239+ if (period > U32_MAX )
240+ period = U32_MAX ;
222241
223- static int gb_pwm_enable (struct pwm_chip * chip , struct pwm_device * pwm )
224- {
225- struct gb_pwm_chip * pwmc = pwm_chip_to_gb_pwm_chip (chip );
242+ if (duty_cycle > period )
243+ duty_cycle = period ;
226244
227- return gb_pwm_enable_operation (pwmc , pwm -> hwpwm );
228- };
245+ err = gb_pwm_config_operation (pwmc , pwm -> hwpwm , duty_cycle , period );
246+ if (err )
247+ return err ;
229248
230- static void gb_pwm_disable ( struct pwm_chip * chip , struct pwm_device * pwm )
231- {
232- struct gb_pwm_chip * pwmc = pwm_chip_to_gb_pwm_chip ( chip );
249+ /* enable/disable */
250+ if (! enabled )
251+ return gb_pwm_enable_operation ( pwmc , pwm -> hwpwm );
233252
234- gb_pwm_disable_operation ( pwmc , pwm -> hwpwm ) ;
235- };
253+ return 0 ;
254+ }
236255
237256static const struct pwm_ops gb_pwm_ops = {
238257 .request = gb_pwm_request ,
239258 .free = gb_pwm_free ,
240- .config = gb_pwm_config ,
241- .set_polarity = gb_pwm_set_polarity ,
242- .enable = gb_pwm_enable ,
243- .disable = gb_pwm_disable ,
259+ .apply = gb_pwm_apply ,
244260 .owner = THIS_MODULE ,
245261};
246262
0 commit comments