5 #include <linux/mutex.h>
14 * enum pwm_polarity - polarity of a PWM signal
15 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
16 * cycle, followed by a low signal for the remainder of the pulse
18 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
19 * cycle, followed by a high signal for the remainder of the pulse
24 PWM_POLARITY_INVERSED,
28 * struct pwm_args - board-dependent PWM arguments
29 * @period: reference period
30 * @polarity: reference polarity
32 * This structure describes board-dependent arguments attached to a PWM
33 * device. These arguments are usually retrieved from the PWM lookup table or
36 * Do not confuse this with the PWM state: PWM arguments represent the initial
37 * configuration that users want to use on this PWM device rather than the
38 * current PWM hardware state.
42 enum pwm_polarity polarity;
46 PWMF_REQUESTED = 1 << 0,
47 PWMF_EXPORTED = 1 << 1,
51 * struct pwm_state - state of a PWM channel
52 * @period: PWM period (in nanoseconds)
53 * @duty_cycle: PWM duty cycle (in nanoseconds)
54 * @polarity: PWM polarity
55 * @enabled: PWM enabled status
59 unsigned int duty_cycle;
60 enum pwm_polarity polarity;
65 * struct pwm_device - PWM channel object
66 * @label: name of the PWM device
67 * @flags: flags associated with the PWM device
68 * @hwpwm: per-chip relative index of the PWM device
69 * @pwm: global index of the PWM device
70 * @chip: PWM chip providing this PWM device
71 * @chip_data: chip-private data associated with the PWM device
72 * @args: PWM arguments
73 * @state: curent PWM channel state
80 struct pwm_chip *chip;
84 struct pwm_state state;
88 * pwm_get_state() - retrieve the current PWM state
90 * @state: state to fill with the current PWM state
92 static inline void pwm_get_state(const struct pwm_device *pwm,
93 struct pwm_state *state)
98 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
100 struct pwm_state state;
102 pwm_get_state(pwm, &state);
104 return state.enabled;
107 static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
110 pwm->state.period = period;
113 static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
115 struct pwm_state state;
117 pwm_get_state(pwm, &state);
122 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
125 pwm->state.duty_cycle = duty;
128 static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
130 struct pwm_state state;
132 pwm_get_state(pwm, &state);
134 return state.duty_cycle;
137 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
139 struct pwm_state state;
141 pwm_get_state(pwm, &state);
143 return state.polarity;
146 static inline void pwm_get_args(const struct pwm_device *pwm,
147 struct pwm_args *args)
153 * struct pwm_ops - PWM controller operations
154 * @request: optional hook for requesting a PWM
155 * @free: optional hook for freeing a PWM
156 * @config: configure duty cycles and period length for this PWM
157 * @set_polarity: configure the polarity of this PWM
158 * @capture: capture and report PWM signal
159 * @enable: enable PWM output toggling
160 * @disable: disable PWM output toggling
161 * @apply: atomically apply a new PWM config. The state argument
162 * should be adjusted with the real hardware config (if the
163 * approximate the period or duty_cycle value, state should
165 * @get_state: get the current PWM state. This function is only
166 * called once per PWM device when the PWM chip is
168 * @dbg_show: optional routine to show contents in debugfs
169 * @owner: helps prevent removal of modules exporting active PWMs
172 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
173 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
174 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
175 int duty_ns, int period_ns);
176 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
177 enum pwm_polarity polarity);
178 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
179 struct pwm_capture *result, unsigned long timeout);
180 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
181 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
182 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
183 struct pwm_state *state);
184 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
185 struct pwm_state *state);
186 #ifdef CONFIG_DEBUG_FS
187 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
189 struct module *owner;
193 * struct pwm_chip - abstract a PWM controller
194 * @dev: device providing the PWMs
195 * @list: list node for internal use
196 * @ops: callbacks for this PWM controller
197 * @base: number of first PWM controlled by this chip
198 * @npwm: number of PWMs controlled by this chip
199 * @pwms: array of PWM devices allocated by the framework
200 * @of_xlate: request a PWM device given a device tree PWM specifier
201 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
202 * @can_sleep: must be true if the .config(), .enable() or .disable()
203 * operations may sleep
207 struct list_head list;
208 const struct pwm_ops *ops;
212 struct pwm_device *pwms;
214 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
215 const struct of_phandle_args *args);
216 unsigned int of_pwm_n_cells;
221 * struct pwm_capture - PWM capture data
222 * @period: period of the PWM signal (in nanoseconds)
223 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
227 unsigned int duty_cycle;
230 #if IS_ENABLED(CONFIG_PWM)
232 struct pwm_device *pwm_request(int pwm_id, const char *label);
233 void pwm_free(struct pwm_device *pwm);
234 int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
235 int pwm_adjust_config(struct pwm_device *pwm);
238 * pwm_config() - change a PWM device configuration
240 * @duty_ns: "on" time (in nanoseconds)
241 * @period_ns: duration (in nanoseconds) of one cycle
243 * Returns: 0 on success or a negative error code on failure.
245 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
248 struct pwm_state state;
253 if (duty_ns < 0 || period_ns < 0)
256 pwm_get_state(pwm, &state);
257 if (state.duty_cycle == duty_ns && state.period == period_ns)
260 state.duty_cycle = duty_ns;
261 state.period = period_ns;
262 return pwm_apply_state(pwm, &state);
266 * pwm_set_polarity() - configure the polarity of a PWM signal
268 * @polarity: new polarity of the PWM signal
270 * Note that the polarity cannot be configured while the PWM device is
273 * Returns: 0 on success or a negative error code on failure.
275 static inline int pwm_set_polarity(struct pwm_device *pwm,
276 enum pwm_polarity polarity)
278 struct pwm_state state;
283 pwm_get_state(pwm, &state);
284 if (state.polarity == polarity)
288 * Changing the polarity of a running PWM without adjusting the
289 * dutycycle/period value is a bit risky (can introduce glitches).
290 * Return -EBUSY in this case.
291 * Note that this is allowed when using pwm_apply_state() because
292 * the user specifies all the parameters.
297 state.polarity = polarity;
298 return pwm_apply_state(pwm, &state);
302 * pwm_enable() - start a PWM output toggling
305 * Returns: 0 on success or a negative error code on failure.
307 static inline int pwm_enable(struct pwm_device *pwm)
309 struct pwm_state state;
314 pwm_get_state(pwm, &state);
318 state.enabled = true;
319 return pwm_apply_state(pwm, &state);
323 * pwm_disable() - stop a PWM output toggling
326 static inline void pwm_disable(struct pwm_device *pwm)
328 struct pwm_state state;
333 pwm_get_state(pwm, &state);
337 state.enabled = false;
338 pwm_apply_state(pwm, &state);
342 /* PWM provider APIs */
343 int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
344 unsigned long timeout);
345 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
346 void *pwm_get_chip_data(struct pwm_device *pwm);
348 int pwmchip_add_with_polarity(struct pwm_chip *chip,
349 enum pwm_polarity polarity);
350 int pwmchip_add(struct pwm_chip *chip);
351 int pwmchip_remove(struct pwm_chip *chip);
352 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
356 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
357 const struct of_phandle_args *args);
359 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
360 struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
361 void pwm_put(struct pwm_device *pwm);
363 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
364 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
366 void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
368 bool pwm_can_sleep(struct pwm_device *pwm);
370 static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
372 return ERR_PTR(-ENODEV);
375 static inline void pwm_free(struct pwm_device *pwm)
379 static inline int pwm_apply_state(struct pwm_device *pwm,
380 const struct pwm_state *state)
385 static inline int pwm_adjust_config(struct pwm_device *pwm)
390 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
396 static inline int pwm_capture(struct pwm_device *pwm,
397 struct pwm_capture *result,
398 unsigned long timeout)
403 static inline int pwm_set_polarity(struct pwm_device *pwm,
404 enum pwm_polarity polarity)
409 static inline int pwm_enable(struct pwm_device *pwm)
414 static inline void pwm_disable(struct pwm_device *pwm)
418 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
423 static inline void *pwm_get_chip_data(struct pwm_device *pwm)
428 static inline int pwmchip_add(struct pwm_chip *chip)
433 static inline int pwmchip_add_inversed(struct pwm_chip *chip)
438 static inline int pwmchip_remove(struct pwm_chip *chip)
443 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
447 return ERR_PTR(-ENODEV);
450 static inline struct pwm_device *pwm_get(struct device *dev,
451 const char *consumer)
453 return ERR_PTR(-ENODEV);
456 static inline struct pwm_device *of_pwm_get(struct device_node *np,
459 return ERR_PTR(-ENODEV);
462 static inline void pwm_put(struct pwm_device *pwm)
466 static inline struct pwm_device *devm_pwm_get(struct device *dev,
467 const char *consumer)
469 return ERR_PTR(-ENODEV);
472 static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
473 struct device_node *np,
476 return ERR_PTR(-ENODEV);
479 static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
483 static inline bool pwm_can_sleep(struct pwm_device *pwm)
489 static inline void pwm_apply_args(struct pwm_device *pwm)
491 struct pwm_state state = { };
494 * PWM users calling pwm_apply_args() expect to have a fresh config
495 * where the polarity and period are set according to pwm_args info.
496 * The problem is, polarity can only be changed when the PWM is
499 * PWM drivers supporting hardware readout may declare the PWM device
500 * as enabled, and prevent polarity setting, which changes from the
501 * existing behavior, where all PWM devices are declared as disabled
502 * at startup (even if they are actually enabled), thus authorizing
505 * To fulfill this requirement, we apply a new state which disables
506 * the PWM device and set the reference period and polarity config.
508 * Note that PWM users requiring a smooth handover between the
509 * bootloader and the kernel (like critical regulators controlled by
510 * PWM devices) will have to switch to the atomic API and avoid calling
514 state.enabled = false;
515 state.polarity = pwm->args.polarity;
516 state.period = pwm->args.period;
518 pwm_apply_state(pwm, &state);
522 struct list_head list;
523 const char *provider;
528 enum pwm_polarity polarity;
531 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
533 .provider = _provider, \
538 .polarity = _polarity \
541 #if IS_ENABLED(CONFIG_PWM)
542 void pwm_add_table(struct pwm_lookup *table, size_t num);
543 void pwm_remove_table(struct pwm_lookup *table, size_t num);
545 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
549 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
554 #ifdef CONFIG_PWM_SYSFS
555 void pwmchip_sysfs_export(struct pwm_chip *chip);
556 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
557 void pwmchip_sysfs_unexport_children(struct pwm_chip *chip);
559 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
563 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
567 static inline void pwmchip_sysfs_unexport_children(struct pwm_chip *chip)
570 #endif /* CONFIG_PWM_SYSFS */
572 #endif /* __LINUX_PWM_H */