5 #include <linux/mutex.h>
12 * enum pwm_polarity - polarity of a PWM signal
13 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
14 * cycle, followed by a low signal for the remainder of the pulse
16 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
17 * cycle, followed by a high signal for the remainder of the pulse
22 PWM_POLARITY_INVERSED
,
26 * struct pwm_args - board-dependent PWM arguments
27 * @period: reference period
28 * @polarity: reference polarity
30 * This structure describes board-dependent arguments attached to a PWM
31 * device. These arguments are usually retrieved from the PWM lookup table or
34 * Do not confuse this with the PWM state: PWM arguments represent the initial
35 * configuration that users want to use on this PWM device rather than the
36 * current PWM hardware state.
40 enum pwm_polarity polarity
;
44 PWMF_REQUESTED
= 1 << 0,
45 PWMF_EXPORTED
= 1 << 1,
49 * struct pwm_state - state of a PWM channel
50 * @period: PWM period (in nanoseconds)
51 * @duty_cycle: PWM duty cycle (in nanoseconds)
52 * @polarity: PWM polarity
53 * @enabled: PWM enabled status
57 unsigned int duty_cycle
;
58 enum pwm_polarity polarity
;
63 * struct pwm_device - PWM channel object
64 * @label: name of the PWM device
65 * @flags: flags associated with the PWM device
66 * @hwpwm: per-chip relative index of the PWM device
67 * @pwm: global index of the PWM device
68 * @chip: PWM chip providing this PWM device
69 * @chip_data: chip-private data associated with the PWM device
70 * @args: PWM arguments
71 * @state: curent PWM channel state
78 struct pwm_chip
*chip
;
82 struct pwm_state state
;
86 * pwm_get_state() - retrieve the current PWM state
88 * @state: state to fill with the current PWM state
90 static inline void pwm_get_state(const struct pwm_device
*pwm
,
91 struct pwm_state
*state
)
96 static inline bool pwm_is_enabled(const struct pwm_device
*pwm
)
98 struct pwm_state state
;
100 pwm_get_state(pwm
, &state
);
102 return state
.enabled
;
105 static inline void pwm_set_period(struct pwm_device
*pwm
, unsigned int period
)
108 pwm
->state
.period
= period
;
111 static inline unsigned int pwm_get_period(const struct pwm_device
*pwm
)
113 struct pwm_state state
;
115 pwm_get_state(pwm
, &state
);
120 static inline void pwm_set_duty_cycle(struct pwm_device
*pwm
, unsigned int duty
)
123 pwm
->state
.duty_cycle
= duty
;
126 static inline unsigned int pwm_get_duty_cycle(const struct pwm_device
*pwm
)
128 struct pwm_state state
;
130 pwm_get_state(pwm
, &state
);
132 return state
.duty_cycle
;
135 static inline enum pwm_polarity
pwm_get_polarity(const struct pwm_device
*pwm
)
137 struct pwm_state state
;
139 pwm_get_state(pwm
, &state
);
141 return state
.polarity
;
144 static inline void pwm_get_args(const struct pwm_device
*pwm
,
145 struct pwm_args
*args
)
151 * struct pwm_ops - PWM controller operations
152 * @request: optional hook for requesting a PWM
153 * @free: optional hook for freeing a PWM
154 * @config: configure duty cycles and period length for this PWM
155 * @set_polarity: configure the polarity of this PWM
156 * @enable: enable PWM output toggling
157 * @disable: disable PWM output toggling
158 * @apply: atomically apply a new PWM config. The state argument
159 * should be adjusted with the real hardware config (if the
160 * approximate the period or duty_cycle value, state should
162 * @get_state: get the current PWM state. This function is only
163 * called once per PWM device when the PWM chip is
165 * @dbg_show: optional routine to show contents in debugfs
166 * @owner: helps prevent removal of modules exporting active PWMs
169 int (*request
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
170 void (*free
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
171 int (*config
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
172 int duty_ns
, int period_ns
);
173 int (*set_polarity
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
174 enum pwm_polarity polarity
);
175 int (*enable
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
176 void (*disable
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
);
177 int (*apply
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
178 struct pwm_state
*state
);
179 void (*get_state
)(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
180 struct pwm_state
*state
);
181 #ifdef CONFIG_DEBUG_FS
182 void (*dbg_show
)(struct pwm_chip
*chip
, struct seq_file
*s
);
184 struct module
*owner
;
188 * struct pwm_chip - abstract a PWM controller
189 * @dev: device providing the PWMs
190 * @list: list node for internal use
191 * @ops: callbacks for this PWM controller
192 * @base: number of first PWM controlled by this chip
193 * @npwm: number of PWMs controlled by this chip
194 * @pwms: array of PWM devices allocated by the framework
195 * @of_xlate: request a PWM device given a device tree PWM specifier
196 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
197 * @can_sleep: must be true if the .config(), .enable() or .disable()
198 * operations may sleep
202 struct list_head list
;
203 const struct pwm_ops
*ops
;
207 struct pwm_device
*pwms
;
209 struct pwm_device
* (*of_xlate
)(struct pwm_chip
*pc
,
210 const struct of_phandle_args
*args
);
211 unsigned int of_pwm_n_cells
;
215 #if IS_ENABLED(CONFIG_PWM)
217 struct pwm_device
*pwm_request(int pwm_id
, const char *label
);
218 void pwm_free(struct pwm_device
*pwm
);
219 int pwm_apply_state(struct pwm_device
*pwm
, struct pwm_state
*state
);
220 int pwm_adjust_config(struct pwm_device
*pwm
);
223 * pwm_config() - change a PWM device configuration
225 * @duty_ns: "on" time (in nanoseconds)
226 * @period_ns: duration (in nanoseconds) of one cycle
228 * Returns: 0 on success or a negative error code on failure.
230 static inline int pwm_config(struct pwm_device
*pwm
, int duty_ns
,
233 struct pwm_state state
;
238 if (duty_ns
< 0 || period_ns
< 0)
241 pwm_get_state(pwm
, &state
);
242 if (state
.duty_cycle
== duty_ns
&& state
.period
== period_ns
)
245 state
.duty_cycle
= duty_ns
;
246 state
.period
= period_ns
;
247 return pwm_apply_state(pwm
, &state
);
251 * pwm_set_polarity() - configure the polarity of a PWM signal
253 * @polarity: new polarity of the PWM signal
255 * Note that the polarity cannot be configured while the PWM device is
258 * Returns: 0 on success or a negative error code on failure.
260 static inline int pwm_set_polarity(struct pwm_device
*pwm
,
261 enum pwm_polarity polarity
)
263 struct pwm_state state
;
268 pwm_get_state(pwm
, &state
);
269 if (state
.polarity
== polarity
)
273 * Changing the polarity of a running PWM without adjusting the
274 * dutycycle/period value is a bit risky (can introduce glitches).
275 * Return -EBUSY in this case.
276 * Note that this is allowed when using pwm_apply_state() because
277 * the user specifies all the parameters.
282 state
.polarity
= polarity
;
283 return pwm_apply_state(pwm
, &state
);
287 * pwm_enable() - start a PWM output toggling
290 * Returns: 0 on success or a negative error code on failure.
292 static inline int pwm_enable(struct pwm_device
*pwm
)
294 struct pwm_state state
;
299 pwm_get_state(pwm
, &state
);
303 state
.enabled
= true;
304 return pwm_apply_state(pwm
, &state
);
308 * pwm_disable() - stop a PWM output toggling
311 static inline void pwm_disable(struct pwm_device
*pwm
)
313 struct pwm_state state
;
318 pwm_get_state(pwm
, &state
);
322 state
.enabled
= false;
323 pwm_apply_state(pwm
, &state
);
327 /* PWM provider APIs */
328 int pwm_set_chip_data(struct pwm_device
*pwm
, void *data
);
329 void *pwm_get_chip_data(struct pwm_device
*pwm
);
331 int pwmchip_add_with_polarity(struct pwm_chip
*chip
,
332 enum pwm_polarity polarity
);
333 int pwmchip_add(struct pwm_chip
*chip
);
334 int pwmchip_remove(struct pwm_chip
*chip
);
335 struct pwm_device
*pwm_request_from_chip(struct pwm_chip
*chip
,
339 struct pwm_device
*of_pwm_xlate_with_flags(struct pwm_chip
*pc
,
340 const struct of_phandle_args
*args
);
342 struct pwm_device
*pwm_get(struct device
*dev
, const char *con_id
);
343 struct pwm_device
*of_pwm_get(struct device_node
*np
, const char *con_id
);
344 void pwm_put(struct pwm_device
*pwm
);
346 struct pwm_device
*devm_pwm_get(struct device
*dev
, const char *con_id
);
347 struct pwm_device
*devm_of_pwm_get(struct device
*dev
, struct device_node
*np
,
349 void devm_pwm_put(struct device
*dev
, struct pwm_device
*pwm
);
351 bool pwm_can_sleep(struct pwm_device
*pwm
);
353 static inline struct pwm_device
*pwm_request(int pwm_id
, const char *label
)
355 return ERR_PTR(-ENODEV
);
358 static inline void pwm_free(struct pwm_device
*pwm
)
362 static inline int pwm_apply_state(struct pwm_device
*pwm
,
363 const struct pwm_state
*state
)
368 static inline int pwm_adjust_config(struct pwm_device
*pwm
)
373 static inline int pwm_config(struct pwm_device
*pwm
, int duty_ns
,
379 static inline int pwm_set_polarity(struct pwm_device
*pwm
,
380 enum pwm_polarity polarity
)
385 static inline int pwm_enable(struct pwm_device
*pwm
)
390 static inline void pwm_disable(struct pwm_device
*pwm
)
394 static inline int pwm_set_chip_data(struct pwm_device
*pwm
, void *data
)
399 static inline void *pwm_get_chip_data(struct pwm_device
*pwm
)
404 static inline int pwmchip_add(struct pwm_chip
*chip
)
409 static inline int pwmchip_add_inversed(struct pwm_chip
*chip
)
414 static inline int pwmchip_remove(struct pwm_chip
*chip
)
419 static inline struct pwm_device
*pwm_request_from_chip(struct pwm_chip
*chip
,
423 return ERR_PTR(-ENODEV
);
426 static inline struct pwm_device
*pwm_get(struct device
*dev
,
427 const char *consumer
)
429 return ERR_PTR(-ENODEV
);
432 static inline struct pwm_device
*of_pwm_get(struct device_node
*np
,
435 return ERR_PTR(-ENODEV
);
438 static inline void pwm_put(struct pwm_device
*pwm
)
442 static inline struct pwm_device
*devm_pwm_get(struct device
*dev
,
443 const char *consumer
)
445 return ERR_PTR(-ENODEV
);
448 static inline struct pwm_device
*devm_of_pwm_get(struct device
*dev
,
449 struct device_node
*np
,
452 return ERR_PTR(-ENODEV
);
455 static inline void devm_pwm_put(struct device
*dev
, struct pwm_device
*pwm
)
459 static inline bool pwm_can_sleep(struct pwm_device
*pwm
)
465 static inline void pwm_apply_args(struct pwm_device
*pwm
)
467 struct pwm_state state
= { };
470 * PWM users calling pwm_apply_args() expect to have a fresh config
471 * where the polarity and period are set according to pwm_args info.
472 * The problem is, polarity can only be changed when the PWM is
475 * PWM drivers supporting hardware readout may declare the PWM device
476 * as enabled, and prevent polarity setting, which changes from the
477 * existing behavior, where all PWM devices are declared as disabled
478 * at startup (even if they are actually enabled), thus authorizing
481 * To fulfill this requirement, we apply a new state which disables
482 * the PWM device and set the reference period and polarity config.
484 * Note that PWM users requiring a smooth handover between the
485 * bootloader and the kernel (like critical regulators controlled by
486 * PWM devices) will have to switch to the atomic API and avoid calling
490 state
.enabled
= false;
491 state
.polarity
= pwm
->args
.polarity
;
492 state
.period
= pwm
->args
.period
;
494 pwm_apply_state(pwm
, &state
);
498 struct list_head list
;
499 const char *provider
;
504 enum pwm_polarity polarity
;
507 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
509 .provider = _provider, \
514 .polarity = _polarity \
517 #if IS_ENABLED(CONFIG_PWM)
518 void pwm_add_table(struct pwm_lookup
*table
, size_t num
);
519 void pwm_remove_table(struct pwm_lookup
*table
, size_t num
);
521 static inline void pwm_add_table(struct pwm_lookup
*table
, size_t num
)
525 static inline void pwm_remove_table(struct pwm_lookup
*table
, size_t num
)
530 #ifdef CONFIG_PWM_SYSFS
531 void pwmchip_sysfs_export(struct pwm_chip
*chip
);
532 void pwmchip_sysfs_unexport(struct pwm_chip
*chip
);
534 static inline void pwmchip_sysfs_export(struct pwm_chip
*chip
)
538 static inline void pwmchip_sysfs_unexport(struct pwm_chip
*chip
)
541 #endif /* CONFIG_PWM_SYSFS */
543 #endif /* __LINUX_PWM_H */
This page took 0.047178 seconds and 6 git commands to generate.