.. | .. |
---|
5 | 5 | #include <linux/err.h> |
---|
6 | 6 | #include <linux/mutex.h> |
---|
7 | 7 | #include <linux/of.h> |
---|
| 8 | +#include <linux/android_kabi.h> |
---|
8 | 9 | |
---|
9 | 10 | struct pwm_capture; |
---|
10 | 11 | struct seq_file; |
---|
.. | .. |
---|
59 | 60 | PWM_OUTPUT_MODULATED = 1 << 1, |
---|
60 | 61 | }; |
---|
61 | 62 | |
---|
62 | | -/** |
---|
63 | | - * struct pwm_output_pattern - PWM duty pattern for MODULATED duty type |
---|
64 | | - * @duty_pattern: PWM duty cycles in the pattern for duty modulation |
---|
65 | | - * @num_entries: number of entries in the pattern |
---|
66 | | - * @cycles_per_duty: number of PWM period cycles an entry stays at |
---|
67 | | - */ |
---|
68 | | -struct pwm_output_pattern { |
---|
69 | | - u64 *duty_pattern; |
---|
70 | | - unsigned int num_entries; |
---|
71 | | - u64 cycles_per_duty; |
---|
72 | | -}; |
---|
73 | | - |
---|
74 | 63 | /* |
---|
75 | 64 | * struct pwm_state - state of a PWM channel |
---|
76 | 65 | * @period: PWM period (in nanoseconds) |
---|
.. | .. |
---|
83 | 72 | u64 duty_cycle; |
---|
84 | 73 | enum pwm_polarity polarity; |
---|
85 | 74 | enum pwm_output_type output_type; |
---|
86 | | - struct pwm_output_pattern *output_pattern; |
---|
87 | 75 | #ifdef CONFIG_PWM_ROCKCHIP_ONESHOT |
---|
88 | 76 | u64 oneshot_count; |
---|
| 77 | + u32 oneshot_repeat; |
---|
| 78 | + u64 duty_offset; |
---|
89 | 79 | #endif /* CONFIG_PWM_ROCKCHIP_ONESHOT */ |
---|
90 | 80 | bool enabled; |
---|
91 | 81 | }; |
---|
.. | .. |
---|
99 | 89 | * @chip: PWM chip providing this PWM device |
---|
100 | 90 | * @chip_data: chip-private data associated with the PWM device |
---|
101 | 91 | * @args: PWM arguments |
---|
102 | | - * @state: curent PWM channel state |
---|
| 92 | + * @state: last applied state |
---|
| 93 | + * @last: last implemented state (for PWM_DEBUG) |
---|
103 | 94 | */ |
---|
104 | 95 | struct pwm_device { |
---|
105 | 96 | const char *label; |
---|
.. | .. |
---|
111 | 102 | |
---|
112 | 103 | struct pwm_args args; |
---|
113 | 104 | struct pwm_state state; |
---|
| 105 | + struct pwm_state last; |
---|
| 106 | + |
---|
| 107 | + ANDROID_KABI_RESERVE(1); |
---|
114 | 108 | }; |
---|
115 | 109 | |
---|
116 | 110 | /** |
---|
.. | .. |
---|
133 | 127 | return state.enabled; |
---|
134 | 128 | } |
---|
135 | 129 | |
---|
136 | | -static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period) |
---|
| 130 | +static inline void pwm_set_period(struct pwm_device *pwm, u64 period) |
---|
137 | 131 | { |
---|
138 | 132 | if (pwm) |
---|
139 | 133 | pwm->state.period = period; |
---|
140 | 134 | } |
---|
141 | 135 | |
---|
142 | | -static inline void pwm_set_period_extend(struct pwm_device *pwm, u64 period) |
---|
143 | | -{ |
---|
144 | | - if (pwm) |
---|
145 | | - pwm->state.period = period; |
---|
146 | | -} |
---|
147 | | - |
---|
148 | | -static inline unsigned int pwm_get_period(const struct pwm_device *pwm) |
---|
149 | | -{ |
---|
150 | | - struct pwm_state state; |
---|
151 | | - |
---|
152 | | - pwm_get_state(pwm, &state); |
---|
153 | | - |
---|
154 | | - if (state.period > UINT_MAX) |
---|
155 | | - pr_warn("PWM period %llu is truncated\n", state.period); |
---|
156 | | - |
---|
157 | | - return (unsigned int)state.period; |
---|
158 | | -} |
---|
159 | | - |
---|
160 | | -static inline u64 pwm_get_period_extend(const struct pwm_device *pwm) |
---|
| 136 | +static inline u64 pwm_get_period(const struct pwm_device *pwm) |
---|
161 | 137 | { |
---|
162 | 138 | struct pwm_state state; |
---|
163 | 139 | |
---|
.. | .. |
---|
172 | 148 | pwm->state.duty_cycle = duty; |
---|
173 | 149 | } |
---|
174 | 150 | |
---|
175 | | -static inline void pwm_set_duty_cycle_extend(struct pwm_device *pwm, u64 duty) |
---|
176 | | -{ |
---|
177 | | - if (pwm) |
---|
178 | | - pwm->state.duty_cycle = duty; |
---|
179 | | -} |
---|
180 | | - |
---|
181 | | -static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm) |
---|
182 | | -{ |
---|
183 | | - struct pwm_state state; |
---|
184 | | - |
---|
185 | | - pwm_get_state(pwm, &state); |
---|
186 | | - |
---|
187 | | - if (state.duty_cycle > UINT_MAX) |
---|
188 | | - pr_warn("PWM duty cycle %llu is truncated\n", state.duty_cycle); |
---|
189 | | - |
---|
190 | | - return (unsigned int)state.duty_cycle; |
---|
191 | | -} |
---|
192 | | - |
---|
193 | | -static inline u64 pwm_get_duty_cycle_extend(const struct pwm_device *pwm) |
---|
| 151 | +static inline u64 pwm_get_duty_cycle(const struct pwm_device *pwm) |
---|
194 | 152 | { |
---|
195 | 153 | struct pwm_state state; |
---|
196 | 154 | |
---|
.. | .. |
---|
216 | 174 | pwm_get_state(pwm, &state); |
---|
217 | 175 | |
---|
218 | 176 | return state.output_type; |
---|
219 | | -} |
---|
220 | | - |
---|
221 | | -static inline struct pwm_output_pattern *pwm_get_output_pattern( |
---|
222 | | - struct pwm_device *pwm) |
---|
223 | | -{ |
---|
224 | | - struct pwm_state state; |
---|
225 | | - |
---|
226 | | - pwm_get_state(pwm, &state); |
---|
227 | | - |
---|
228 | | - return pwm->state.output_pattern ?: NULL; |
---|
229 | 177 | } |
---|
230 | 178 | |
---|
231 | 179 | static inline void pwm_get_args(const struct pwm_device *pwm, |
---|
.. | .. |
---|
326 | 274 | * struct pwm_ops - PWM controller operations |
---|
327 | 275 | * @request: optional hook for requesting a PWM |
---|
328 | 276 | * @free: optional hook for freeing a PWM |
---|
329 | | - * @config: configure duty cycles and period length for this PWM |
---|
330 | | - * @config_extend: configure duty cycles and period length for this |
---|
331 | | - * PWM with u64 data type |
---|
332 | | - * @set_polarity: configure the polarity of this PWM |
---|
333 | 277 | * @capture: capture and report PWM signal |
---|
334 | | - * @enable: enable PWM output toggling |
---|
335 | | - * @disable: disable PWM output toggling |
---|
336 | | - * @get_output_type_supported: get the supported output type |
---|
337 | | - * @set_output_type: set PWM output type |
---|
338 | | - * @set_output_pattern: set the pattern for the modulated output |
---|
339 | | - * @apply: atomically apply a new PWM config. The state argument |
---|
340 | | - * should be adjusted with the real hardware config (if the |
---|
341 | | - * approximate the period or duty_cycle value, state should |
---|
342 | | - * reflect it) |
---|
| 278 | + * @apply: atomically apply a new PWM config |
---|
343 | 279 | * @get_state: get the current PWM state. This function is only |
---|
344 | 280 | * called once per PWM device when the PWM chip is |
---|
345 | 281 | * registered. |
---|
346 | | - * @dbg_show: optional routine to show contents in debugfs |
---|
| 282 | + * @get_output_type_supported: get the supported output type of this PWM |
---|
347 | 283 | * @owner: helps prevent removal of modules exporting active PWMs |
---|
| 284 | + * @config: configure duty cycles and period length for this PWM |
---|
| 285 | + * @set_polarity: configure the polarity of this PWM |
---|
| 286 | + * @enable: enable PWM output toggling |
---|
| 287 | + * @disable: disable PWM output toggling |
---|
348 | 288 | */ |
---|
349 | 289 | struct pwm_ops { |
---|
350 | 290 | int (*request)(struct pwm_chip *chip, struct pwm_device *pwm); |
---|
351 | 291 | void (*free)(struct pwm_chip *chip, struct pwm_device *pwm); |
---|
352 | | - int (*config)(struct pwm_chip *chip, struct pwm_device *pwm, |
---|
353 | | - int duty_ns, int period_ns); |
---|
354 | | - int (*config_extend)(struct pwm_chip *chip, struct pwm_device *pwm, |
---|
355 | | - u64 duty_ns, u64 period_ns); |
---|
356 | | - int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm, |
---|
357 | | - enum pwm_polarity polarity); |
---|
358 | 292 | int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm, |
---|
359 | 293 | struct pwm_capture *result, unsigned long timeout); |
---|
360 | | - int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); |
---|
361 | | - void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); |
---|
362 | | - int (*get_output_type_supported)(struct pwm_chip *chip, |
---|
363 | | - struct pwm_device *pwm); |
---|
364 | | - int (*set_output_type)(struct pwm_chip *chip, struct pwm_device *pwm, |
---|
365 | | - enum pwm_output_type output_type); |
---|
366 | | - int (*set_output_pattern)(struct pwm_chip *chip, |
---|
367 | | - struct pwm_device *pwm, |
---|
368 | | - struct pwm_output_pattern *output_pattern); |
---|
369 | 294 | int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm, |
---|
370 | | - struct pwm_state *state); |
---|
| 295 | + const struct pwm_state *state); |
---|
371 | 296 | void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm, |
---|
372 | 297 | struct pwm_state *state); |
---|
373 | | -#ifdef CONFIG_DEBUG_FS |
---|
374 | | - void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s); |
---|
375 | | -#endif |
---|
| 298 | + int (*get_output_type_supported)(struct pwm_chip *chip, |
---|
| 299 | + struct pwm_device *pwm); |
---|
376 | 300 | struct module *owner; |
---|
| 301 | + |
---|
| 302 | + /* Only used by legacy drivers */ |
---|
| 303 | + int (*config)(struct pwm_chip *chip, struct pwm_device *pwm, |
---|
| 304 | + int duty_ns, int period_ns); |
---|
| 305 | + int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm, |
---|
| 306 | + enum pwm_polarity polarity); |
---|
| 307 | + int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm); |
---|
| 308 | + void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm); |
---|
| 309 | + |
---|
| 310 | + ANDROID_KABI_RESERVE(1); |
---|
377 | 311 | }; |
---|
378 | 312 | |
---|
379 | 313 | /** |
---|
380 | 314 | * struct pwm_chip - abstract a PWM controller |
---|
381 | 315 | * @dev: device providing the PWMs |
---|
382 | | - * @list: list node for internal use |
---|
383 | 316 | * @ops: callbacks for this PWM controller |
---|
384 | 317 | * @base: number of first PWM controlled by this chip |
---|
385 | 318 | * @npwm: number of PWMs controlled by this chip |
---|
386 | | - * @pwms: array of PWM devices allocated by the framework |
---|
387 | 319 | * @of_xlate: request a PWM device given a device tree PWM specifier |
---|
388 | 320 | * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier |
---|
| 321 | + * @list: list node for internal use |
---|
| 322 | + * @pwms: array of PWM devices allocated by the framework |
---|
389 | 323 | */ |
---|
390 | 324 | struct pwm_chip { |
---|
391 | 325 | struct device *dev; |
---|
392 | | - struct list_head list; |
---|
393 | 326 | const struct pwm_ops *ops; |
---|
394 | 327 | int base; |
---|
395 | 328 | unsigned int npwm; |
---|
396 | 329 | |
---|
397 | | - struct pwm_device *pwms; |
---|
398 | | - |
---|
399 | 330 | struct pwm_device * (*of_xlate)(struct pwm_chip *pc, |
---|
400 | 331 | const struct of_phandle_args *args); |
---|
401 | 332 | unsigned int of_pwm_n_cells; |
---|
| 333 | + |
---|
| 334 | + /* only used internally by the PWM framework */ |
---|
| 335 | + struct list_head list; |
---|
| 336 | + struct pwm_device *pwms; |
---|
| 337 | + |
---|
| 338 | + ANDROID_KABI_RESERVE(1); |
---|
402 | 339 | }; |
---|
403 | 340 | |
---|
404 | 341 | /** |
---|
.. | .. |
---|
407 | 344 | * @duty_cycle: duty cycle of the PWM signal (in nanoseconds) |
---|
408 | 345 | */ |
---|
409 | 346 | struct pwm_capture { |
---|
410 | | - u64 period; |
---|
411 | | - u64 duty_cycle; |
---|
| 347 | + unsigned int period; |
---|
| 348 | + unsigned int duty_cycle; |
---|
412 | 349 | }; |
---|
413 | 350 | |
---|
414 | 351 | #if IS_ENABLED(CONFIG_PWM) |
---|
415 | 352 | /* PWM user APIs */ |
---|
416 | 353 | struct pwm_device *pwm_request(int pwm_id, const char *label); |
---|
417 | 354 | void pwm_free(struct pwm_device *pwm); |
---|
418 | | -int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state); |
---|
| 355 | +int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state); |
---|
419 | 356 | int pwm_adjust_config(struct pwm_device *pwm); |
---|
420 | 357 | |
---|
421 | 358 | /** |
---|
422 | | - * pwm_output_type_support() |
---|
| 359 | + * pwm_get_output_type_supported() - obtain output type of a PWM device. |
---|
423 | 360 | * @pwm: PWM device |
---|
424 | 361 | * |
---|
425 | | - * Returns: output types supported by the PWM device |
---|
| 362 | + * Returns: output type supported by the PWM device |
---|
426 | 363 | */ |
---|
427 | 364 | static inline int pwm_get_output_type_supported(struct pwm_device *pwm) |
---|
428 | 365 | { |
---|
429 | | - if (pwm->chip->ops->get_output_type_supported != NULL) |
---|
| 366 | + if (!pwm) |
---|
| 367 | + return -EINVAL; |
---|
| 368 | + |
---|
| 369 | + if (pwm->chip->ops->get_output_type_supported) |
---|
430 | 370 | return pwm->chip->ops->get_output_type_supported(pwm->chip, |
---|
431 | | - pwm); |
---|
432 | | - else |
---|
433 | | - return PWM_OUTPUT_FIXED; |
---|
| 371 | + pwm); |
---|
| 372 | + |
---|
| 373 | + return PWM_OUTPUT_FIXED; |
---|
434 | 374 | } |
---|
435 | 375 | |
---|
436 | 376 | /** |
---|
.. | .. |
---|
458 | 398 | |
---|
459 | 399 | state.duty_cycle = duty_ns; |
---|
460 | 400 | state.period = period_ns; |
---|
461 | | - return pwm_apply_state(pwm, &state); |
---|
462 | | -} |
---|
463 | | - |
---|
464 | | -/** |
---|
465 | | - * pwm_config_extend() - change PWM period and duty length with u64 data type |
---|
466 | | - * @pwm: PWM device |
---|
467 | | - * @duty_ns: "on" time (in nanoseconds) |
---|
468 | | - * @period_ns: duration (in nanoseconds) of one cycle |
---|
469 | | - * |
---|
470 | | - * Returns: 0 on success or a negative error code on failure. |
---|
471 | | - */ |
---|
472 | | -static inline int pwm_config_extend(struct pwm_device *pwm, u64 duty_ns, |
---|
473 | | - u64 period_ns) |
---|
474 | | -{ |
---|
475 | | - struct pwm_state state; |
---|
476 | | - |
---|
477 | | - if (!pwm) |
---|
478 | | - return -EINVAL; |
---|
479 | | - |
---|
480 | | - pwm_get_state(pwm, &state); |
---|
481 | | - if (state.duty_cycle == duty_ns && state.period == period_ns) |
---|
482 | | - return 0; |
---|
483 | | - |
---|
484 | | - state.duty_cycle = duty_ns; |
---|
485 | | - state.period = period_ns; |
---|
486 | | - return pwm_apply_state(pwm, &state); |
---|
487 | | -} |
---|
488 | | - |
---|
489 | | -/** |
---|
490 | | - * pwm_set_polarity() - configure the polarity of a PWM signal |
---|
491 | | - * @pwm: PWM device |
---|
492 | | - * @polarity: new polarity of the PWM signal |
---|
493 | | - * |
---|
494 | | - * Note that the polarity cannot be configured while the PWM device is |
---|
495 | | - * enabled. |
---|
496 | | - * |
---|
497 | | - * Returns: 0 on success or a negative error code on failure. |
---|
498 | | - */ |
---|
499 | | -static inline int pwm_set_polarity(struct pwm_device *pwm, |
---|
500 | | - enum pwm_polarity polarity) |
---|
501 | | -{ |
---|
502 | | - struct pwm_state state; |
---|
503 | | - |
---|
504 | | - if (!pwm) |
---|
505 | | - return -EINVAL; |
---|
506 | | - |
---|
507 | | - pwm_get_state(pwm, &state); |
---|
508 | | - if (state.polarity == polarity) |
---|
509 | | - return 0; |
---|
510 | | - |
---|
511 | | - /* |
---|
512 | | - * Changing the polarity of a running PWM without adjusting the |
---|
513 | | - * dutycycle/period value is a bit risky (can introduce glitches). |
---|
514 | | - * Return -EBUSY in this case. |
---|
515 | | - * Note that this is allowed when using pwm_apply_state() because |
---|
516 | | - * the user specifies all the parameters. |
---|
517 | | - */ |
---|
518 | | - if (state.enabled) |
---|
519 | | - return -EBUSY; |
---|
520 | | - |
---|
521 | | - state.polarity = polarity; |
---|
522 | 401 | return pwm_apply_state(pwm, &state); |
---|
523 | 402 | } |
---|
524 | 403 | |
---|
.. | .. |
---|
580 | 459 | const struct of_phandle_args *args); |
---|
581 | 460 | |
---|
582 | 461 | struct pwm_device *pwm_get(struct device *dev, const char *con_id); |
---|
583 | | -struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id); |
---|
| 462 | +struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np, |
---|
| 463 | + const char *con_id); |
---|
584 | 464 | void pwm_put(struct pwm_device *pwm); |
---|
585 | 465 | |
---|
586 | 466 | struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id); |
---|
587 | 467 | struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np, |
---|
588 | 468 | const char *con_id); |
---|
| 469 | +struct pwm_device *devm_fwnode_pwm_get(struct device *dev, |
---|
| 470 | + struct fwnode_handle *fwnode, |
---|
| 471 | + const char *con_id); |
---|
589 | 472 | void devm_pwm_put(struct device *dev, struct pwm_device *pwm); |
---|
590 | 473 | #else |
---|
591 | 474 | static inline struct pwm_device *pwm_request(int pwm_id, const char *label) |
---|
.. | .. |
---|
608 | 491 | return -ENOTSUPP; |
---|
609 | 492 | } |
---|
610 | 493 | |
---|
| 494 | +static inline int pwm_get_output_type_supported(struct pwm_device *pwm) |
---|
| 495 | +{ |
---|
| 496 | + return -EINVAL; |
---|
| 497 | +} |
---|
| 498 | + |
---|
611 | 499 | static inline int pwm_config(struct pwm_device *pwm, int duty_ns, |
---|
612 | 500 | int period_ns) |
---|
613 | 501 | { |
---|
.. | .. |
---|
619 | 507 | unsigned long timeout) |
---|
620 | 508 | { |
---|
621 | 509 | return -EINVAL; |
---|
622 | | -} |
---|
623 | | - |
---|
624 | | -static inline int pwm_set_polarity(struct pwm_device *pwm, |
---|
625 | | - enum pwm_polarity polarity) |
---|
626 | | -{ |
---|
627 | | - return -ENOTSUPP; |
---|
628 | 510 | } |
---|
629 | 511 | |
---|
630 | 512 | static inline int pwm_enable(struct pwm_device *pwm) |
---|
.. | .. |
---|
674 | 556 | return ERR_PTR(-ENODEV); |
---|
675 | 557 | } |
---|
676 | 558 | |
---|
677 | | -static inline struct pwm_device *of_pwm_get(struct device_node *np, |
---|
| 559 | +static inline struct pwm_device *of_pwm_get(struct device *dev, |
---|
| 560 | + struct device_node *np, |
---|
678 | 561 | const char *con_id) |
---|
679 | 562 | { |
---|
680 | 563 | return ERR_PTR(-ENODEV); |
---|
.. | .. |
---|
697 | 580 | return ERR_PTR(-ENODEV); |
---|
698 | 581 | } |
---|
699 | 582 | |
---|
| 583 | +static inline struct pwm_device * |
---|
| 584 | +devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode, |
---|
| 585 | + const char *con_id) |
---|
| 586 | +{ |
---|
| 587 | + return ERR_PTR(-ENODEV); |
---|
| 588 | +} |
---|
| 589 | + |
---|
700 | 590 | static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm) |
---|
701 | 591 | { |
---|
702 | 592 | } |
---|