blob: 6c0168b0f44cdc0d29ca10655f089ff4763ba990 [file] [log] [blame]
Russell King1a189b92008-04-13 21:41:55 +01001#ifndef __LINUX_PWM_H
2#define __LINUX_PWM_H
3
Tushar Behera0bcf1682012-09-12 15:31:46 +05304#include <linux/err.h>
Jonathan Richardsond1cd2142015-10-16 17:40:58 -07005#include <linux/mutex.h>
Thierry Reding7299ab72011-12-14 11:10:32 +01006#include <linux/of.h>
7
Lee Jones3a3d1a42016-06-08 10:21:23 +01008struct pwm_capture;
Thierry Reding62099ab2012-03-26 09:31:48 +02009struct seq_file;
Lee Jones3a3d1a42016-06-08 10:21:23 +010010
Sascha Hauer0c2498f2011-01-28 09:40:40 +010011struct pwm_chip;
12
Philip, Avinash0aa08692012-07-24 19:35:32 +053013/**
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
17 * period
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
20 * period
21 */
22enum pwm_polarity {
23 PWM_POLARITY_NORMAL,
24 PWM_POLARITY_INVERSED,
25};
26
Boris Brezillone39c0df2016-04-14 21:17:21 +020027/**
28 * struct pwm_args - board-dependent PWM arguments
29 * @period: reference period
30 * @polarity: reference polarity
31 *
32 * This structure describes board-dependent arguments attached to a PWM
33 * device. These arguments are usually retrieved from the PWM lookup table or
34 * device tree.
35 *
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.
39 */
40struct pwm_args {
41 unsigned int period;
42 enum pwm_polarity polarity;
43};
44
Thierry Redingf051c462011-12-14 11:12:23 +010045enum {
46 PWMF_REQUESTED = 1 << 0,
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020047 PWMF_EXPORTED = 1 << 1,
Thierry Redingf051c462011-12-14 11:12:23 +010048};
49
Fenglin Wuccee0bb2017-12-04 09:56:51 +080050/**
51 * enum pwm_output_type - output type of the PWM signal
52 * @PWM_OUTPUT_FIXED: PWM output is fixed until a change request
53 * @PWM_OUTPUT_MODULATED: PWM output is modulated in hardware
54 * autonomously with a predefined pattern
55 */
56enum pwm_output_type {
57 PWM_OUTPUT_FIXED = 1 << 0,
58 PWM_OUTPUT_MODULATED = 1 << 1,
59};
60
61/**
62 * struct pwm_output_pattern - PWM duty pattern for MODULATED duty type
63 * @duty_pattern: PWM duty cycles in the pattern for duty modulation
64 * @num_entries: number of entries in the pattern
65 * @cycles_per_duty: number of PWM period cycles an entry stays at
66 */
67struct pwm_output_pattern {
68 unsigned int *duty_pattern;
69 unsigned int num_entries;
70 unsigned int cycles_per_duty;
71};
72
Boris Brezillon43a276b2016-04-14 21:17:38 +020073/*
74 * struct pwm_state - state of a PWM channel
75 * @period: PWM period (in nanoseconds)
76 * @duty_cycle: PWM duty cycle (in nanoseconds)
77 * @polarity: PWM polarity
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020078 * @enabled: PWM enabled status
Boris Brezillon43a276b2016-04-14 21:17:38 +020079 */
80struct pwm_state {
81 unsigned int period;
82 unsigned int duty_cycle;
83 enum pwm_polarity polarity;
Fenglin Wuccee0bb2017-12-04 09:56:51 +080084 enum pwm_output_type output_type;
85 struct pwm_output_pattern *output_pattern;
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020086 bool enabled;
Boris Brezillon43a276b2016-04-14 21:17:38 +020087};
88
Thierry Reding04883802015-07-27 11:58:32 +020089/**
90 * struct pwm_device - PWM channel object
91 * @label: name of the PWM device
92 * @flags: flags associated with the PWM device
93 * @hwpwm: per-chip relative index of the PWM device
94 * @pwm: global index of the PWM device
95 * @chip: PWM chip providing this PWM device
96 * @chip_data: chip-private data associated with the PWM device
Boris Brezillone39c0df2016-04-14 21:17:21 +020097 * @args: PWM arguments
Boris Brezillon43a276b2016-04-14 21:17:38 +020098 * @state: curent PWM channel state
Thierry Reding04883802015-07-27 11:58:32 +020099 */
Thierry Redingf051c462011-12-14 11:12:23 +0100100struct pwm_device {
Thierry Reding6bc70642015-07-27 11:57:28 +0200101 const char *label;
102 unsigned long flags;
103 unsigned int hwpwm;
104 unsigned int pwm;
105 struct pwm_chip *chip;
106 void *chip_data;
Thierry Redingf051c462011-12-14 11:12:23 +0100107
Boris Brezillone39c0df2016-04-14 21:17:21 +0200108 struct pwm_args args;
Boris Brezillon43a276b2016-04-14 21:17:38 +0200109 struct pwm_state state;
Thierry Redingf051c462011-12-14 11:12:23 +0100110};
111
Boris Brezillon43a276b2016-04-14 21:17:38 +0200112/**
113 * pwm_get_state() - retrieve the current PWM state
114 * @pwm: PWM device
115 * @state: state to fill with the current PWM state
116 */
117static inline void pwm_get_state(const struct pwm_device *pwm,
118 struct pwm_state *state)
119{
120 *state = pwm->state;
121}
122
Boris Brezillon5c312522015-07-01 10:21:47 +0200123static inline bool pwm_is_enabled(const struct pwm_device *pwm)
124{
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200125 struct pwm_state state;
126
127 pwm_get_state(pwm, &state);
128
129 return state.enabled;
Boris Brezillon5c312522015-07-01 10:21:47 +0200130}
131
Thierry Redingf051c462011-12-14 11:12:23 +0100132static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
133{
134 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200135 pwm->state.period = period;
Thierry Redingf051c462011-12-14 11:12:23 +0100136}
137
Boris Brezillona1cf4212015-07-01 10:21:48 +0200138static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100139{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200140 struct pwm_state state;
141
142 pwm_get_state(pwm, &state);
143
144 return state.period;
Thierry Redingf051c462011-12-14 11:12:23 +0100145}
146
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700147static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
148{
149 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200150 pwm->state.duty_cycle = duty;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700151}
152
Boris Brezillona1cf4212015-07-01 10:21:48 +0200153static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700154{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200155 struct pwm_state state;
156
157 pwm_get_state(pwm, &state);
158
159 return state.duty_cycle;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700160}
161
Boris Brezillon011e7632015-07-01 10:21:49 +0200162static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
163{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200164 struct pwm_state state;
165
166 pwm_get_state(pwm, &state);
167
168 return state.polarity;
Boris Brezillon011e7632015-07-01 10:21:49 +0200169}
170
Fenglin Wuccee0bb2017-12-04 09:56:51 +0800171static inline enum pwm_output_type pwm_get_output_type(
172 const struct pwm_device *pwm)
173{
174 struct pwm_state state;
175
176 pwm_get_state(pwm, &state);
177
178 return state.output_type;
179}
180
181static inline struct pwm_output_pattern *pwm_get_output_pattern(
182 struct pwm_device *pwm)
183{
184 struct pwm_state state;
185
186 pwm_get_state(pwm, &state);
187
188 return pwm->state.output_pattern ?: NULL;
189}
190
Boris Brezillone39c0df2016-04-14 21:17:21 +0200191static inline void pwm_get_args(const struct pwm_device *pwm,
192 struct pwm_args *args)
193{
194 *args = pwm->args;
195}
196
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100197/**
Boris Brezillona6a0dbb2016-06-14 11:13:09 +0200198 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
199 * @pwm: PWM device
200 * @state: state to fill with the prepared PWM state
201 *
202 * This functions prepares a state that can later be tweaked and applied
203 * to the PWM device with pwm_apply_state(). This is a convenient function
204 * that first retrieves the current PWM state and the replaces the period
205 * and polarity fields with the reference values defined in pwm->args.
206 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
207 * fields according to your needs before calling pwm_apply_state().
208 *
209 * ->duty_cycle is initially set to zero to avoid cases where the current
210 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
211 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
212 * first.
213 */
214static inline void pwm_init_state(const struct pwm_device *pwm,
215 struct pwm_state *state)
216{
217 struct pwm_args args;
218
219 /* First get the current state. */
220 pwm_get_state(pwm, state);
221
222 /* Then fill it with the reference config */
223 pwm_get_args(pwm, &args);
224
225 state->period = args.period;
226 state->polarity = args.polarity;
227 state->duty_cycle = 0;
228}
229
230/**
Boris Brezillonf6f3bdd2016-06-14 11:13:10 +0200231 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
232 * @state: PWM state to extract the duty cycle from
233 * @scale: target scale of the relative duty cycle
234 *
235 * This functions converts the absolute duty cycle stored in @state (expressed
236 * in nanosecond) into a value relative to the period.
237 *
238 * For example if you want to get the duty_cycle expressed in percent, call:
239 *
240 * pwm_get_state(pwm, &state);
241 * duty = pwm_get_relative_duty_cycle(&state, 100);
242 */
243static inline unsigned int
244pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
245{
246 if (!state->period)
247 return 0;
248
249 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
250 state->period);
251}
252
253/**
254 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
255 * @state: PWM state to fill
256 * @duty_cycle: relative duty cycle value
257 * @scale: scale in which @duty_cycle is expressed
258 *
259 * This functions converts a relative into an absolute duty cycle (expressed
260 * in nanoseconds), and puts the result in state->duty_cycle.
261 *
262 * For example if you want to configure a 50% duty cycle, call:
263 *
264 * pwm_init_state(pwm, &state);
265 * pwm_set_relative_duty_cycle(&state, 50, 100);
266 * pwm_apply_state(pwm, &state);
267 *
268 * This functions returns -EINVAL if @duty_cycle and/or @scale are
269 * inconsistent (@scale == 0 or @duty_cycle > @scale).
270 */
271static inline int
272pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
273 unsigned int scale)
274{
275 if (!scale || duty_cycle > scale)
276 return -EINVAL;
277
278 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
279 state->period,
280 scale);
281
282 return 0;
283}
284
285/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100286 * struct pwm_ops - PWM controller operations
287 * @request: optional hook for requesting a PWM
288 * @free: optional hook for freeing a PWM
289 * @config: configure duty cycles and period length for this PWM
Philip, Avinash0aa08692012-07-24 19:35:32 +0530290 * @set_polarity: configure the polarity of this PWM
Lee Jones3a3d1a42016-06-08 10:21:23 +0100291 * @capture: capture and report PWM signal
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100292 * @enable: enable PWM output toggling
293 * @disable: disable PWM output toggling
Fenglin Wuccee0bb2017-12-04 09:56:51 +0800294 * @get_output_type_supported: get the supported output type
295 * @set_output_type: set PWM output type
296 * @set_output_pattern: set the pattern for the modulated output
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200297 * @apply: atomically apply a new PWM config. The state argument
298 * should be adjusted with the real hardware config (if the
299 * approximate the period or duty_cycle value, state should
300 * reflect it)
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200301 * @get_state: get the current PWM state. This function is only
302 * called once per PWM device when the PWM chip is
303 * registered.
Thierry Reding62099ab2012-03-26 09:31:48 +0200304 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100305 * @owner: helps prevent removal of modules exporting active PWMs
306 */
307struct pwm_ops {
Thierry Reding6bc70642015-07-27 11:57:28 +0200308 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
309 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
310 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
311 int duty_ns, int period_ns);
312 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
313 enum pwm_polarity polarity);
Lee Jones3a3d1a42016-06-08 10:21:23 +0100314 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
315 struct pwm_capture *result, unsigned long timeout);
Thierry Reding6bc70642015-07-27 11:57:28 +0200316 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
317 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Fenglin Wuccee0bb2017-12-04 09:56:51 +0800318 int (*get_output_type_supported)(struct pwm_chip *chip,
319 struct pwm_device *pwm);
320 int (*set_output_type)(struct pwm_chip *chip, struct pwm_device *pwm,
321 enum pwm_output_type output_type);
322 int (*set_output_pattern)(struct pwm_chip *chip,
323 struct pwm_device *pwm,
324 struct pwm_output_pattern *output_pattern);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200325 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
326 struct pwm_state *state);
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200327 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
328 struct pwm_state *state);
Thierry Reding62099ab2012-03-26 09:31:48 +0200329#ifdef CONFIG_DEBUG_FS
Thierry Reding6bc70642015-07-27 11:57:28 +0200330 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
Thierry Reding62099ab2012-03-26 09:31:48 +0200331#endif
Thierry Reding6bc70642015-07-27 11:57:28 +0200332 struct module *owner;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100333};
334
335/**
Thierry Redingf051c462011-12-14 11:12:23 +0100336 * struct pwm_chip - abstract a PWM controller
337 * @dev: device providing the PWMs
338 * @list: list node for internal use
339 * @ops: callbacks for this PWM controller
340 * @base: number of first PWM controlled by this chip
341 * @npwm: number of PWMs controlled by this chip
342 * @pwms: array of PWM devices allocated by the framework
Thierry Reding04883802015-07-27 11:58:32 +0200343 * @of_xlate: request a PWM device given a device tree PWM specifier
344 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100345 * @can_sleep: must be true if the .config(), .enable() or .disable()
346 * operations may sleep
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100347 */
348struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200349 struct device *dev;
350 struct list_head list;
351 const struct pwm_ops *ops;
352 int base;
353 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100354
Thierry Reding6bc70642015-07-27 11:57:28 +0200355 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100356
Thierry Reding6bc70642015-07-27 11:57:28 +0200357 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
358 const struct of_phandle_args *args);
359 unsigned int of_pwm_n_cells;
360 bool can_sleep;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100361};
362
Lee Jones3a3d1a42016-06-08 10:21:23 +0100363/**
364 * struct pwm_capture - PWM capture data
365 * @period: period of the PWM signal (in nanoseconds)
366 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
367 */
368struct pwm_capture {
369 unsigned int period;
370 unsigned int duty_cycle;
371};
372
Tushar Behera0bcf1682012-09-12 15:31:46 +0530373#if IS_ENABLED(CONFIG_PWM)
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200374/* PWM user APIs */
375struct pwm_device *pwm_request(int pwm_id, const char *label);
376void pwm_free(struct pwm_device *pwm);
377int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
378int pwm_adjust_config(struct pwm_device *pwm);
379
380/**
Fenglin Wuccee0bb2017-12-04 09:56:51 +0800381 * pwm_output_type_support()
382 * @pwm: PWM device
383 *
384 * Returns: output types supported by the PWM device
385 */
386static inline int pwm_get_output_type_supported(struct pwm_device *pwm)
387{
388 if (pwm->chip->ops->get_output_type_supported != NULL)
389 return pwm->chip->ops->
390 get_output_type_supported(pwm->chip, pwm);
391 else
392 return PWM_OUTPUT_FIXED;
393}
394
395/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200396 * pwm_config() - change a PWM device configuration
397 * @pwm: PWM device
398 * @duty_ns: "on" time (in nanoseconds)
399 * @period_ns: duration (in nanoseconds) of one cycle
400 *
401 * Returns: 0 on success or a negative error code on failure.
402 */
403static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
404 int period_ns)
405{
406 struct pwm_state state;
407
408 if (!pwm)
409 return -EINVAL;
410
Brian Norrisef2bf492016-05-27 09:45:49 -0700411 if (duty_ns < 0 || period_ns < 0)
412 return -EINVAL;
413
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200414 pwm_get_state(pwm, &state);
415 if (state.duty_cycle == duty_ns && state.period == period_ns)
416 return 0;
417
418 state.duty_cycle = duty_ns;
419 state.period = period_ns;
420 return pwm_apply_state(pwm, &state);
421}
422
423/**
424 * pwm_set_polarity() - configure the polarity of a PWM signal
425 * @pwm: PWM device
426 * @polarity: new polarity of the PWM signal
427 *
428 * Note that the polarity cannot be configured while the PWM device is
429 * enabled.
430 *
431 * Returns: 0 on success or a negative error code on failure.
432 */
433static inline int pwm_set_polarity(struct pwm_device *pwm,
434 enum pwm_polarity polarity)
435{
436 struct pwm_state state;
437
438 if (!pwm)
439 return -EINVAL;
440
441 pwm_get_state(pwm, &state);
442 if (state.polarity == polarity)
443 return 0;
444
445 /*
446 * Changing the polarity of a running PWM without adjusting the
447 * dutycycle/period value is a bit risky (can introduce glitches).
448 * Return -EBUSY in this case.
449 * Note that this is allowed when using pwm_apply_state() because
450 * the user specifies all the parameters.
451 */
452 if (state.enabled)
453 return -EBUSY;
454
455 state.polarity = polarity;
456 return pwm_apply_state(pwm, &state);
457}
458
459/**
460 * pwm_enable() - start a PWM output toggling
461 * @pwm: PWM device
462 *
463 * Returns: 0 on success or a negative error code on failure.
464 */
465static inline int pwm_enable(struct pwm_device *pwm)
466{
467 struct pwm_state state;
468
469 if (!pwm)
470 return -EINVAL;
471
472 pwm_get_state(pwm, &state);
473 if (state.enabled)
474 return 0;
475
476 state.enabled = true;
477 return pwm_apply_state(pwm, &state);
478}
479
480/**
481 * pwm_disable() - stop a PWM output toggling
482 * @pwm: PWM device
483 */
484static inline void pwm_disable(struct pwm_device *pwm)
485{
486 struct pwm_state state;
487
488 if (!pwm)
489 return;
490
491 pwm_get_state(pwm, &state);
492 if (!state.enabled)
493 return;
494
495 state.enabled = false;
496 pwm_apply_state(pwm, &state);
497}
498
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200499/* PWM provider APIs */
Lee Jones3a3d1a42016-06-08 10:21:23 +0100500int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
501 unsigned long timeout);
Thierry Redingf051c462011-12-14 11:12:23 +0100502int pwm_set_chip_data(struct pwm_device *pwm, void *data);
503void *pwm_get_chip_data(struct pwm_device *pwm);
504
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700505int pwmchip_add_with_polarity(struct pwm_chip *chip,
506 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100507int pwmchip_add(struct pwm_chip *chip);
508int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100509struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
510 unsigned int index,
511 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200512
Philip, Avinash83af2402012-11-21 13:10:44 +0530513struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
514 const struct of_phandle_args *args);
515
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800516struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800517struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200518void pwm_put(struct pwm_device *pwm);
519
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800520struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800521struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
522 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900523void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100524
525bool pwm_can_sleep(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530526#else
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200527static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
528{
529 return ERR_PTR(-ENODEV);
530}
531
532static inline void pwm_free(struct pwm_device *pwm)
533{
534}
535
536static inline int pwm_apply_state(struct pwm_device *pwm,
537 const struct pwm_state *state)
538{
539 return -ENOTSUPP;
540}
541
542static inline int pwm_adjust_config(struct pwm_device *pwm)
543{
544 return -ENOTSUPP;
545}
546
547static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
548 int period_ns)
549{
550 return -EINVAL;
551}
552
Lee Jones3a3d1a42016-06-08 10:21:23 +0100553static inline int pwm_capture(struct pwm_device *pwm,
554 struct pwm_capture *result,
555 unsigned long timeout)
556{
557 return -EINVAL;
558}
559
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200560static inline int pwm_set_polarity(struct pwm_device *pwm,
561 enum pwm_polarity polarity)
562{
563 return -ENOTSUPP;
564}
565
566static inline int pwm_enable(struct pwm_device *pwm)
567{
568 return -EINVAL;
569}
570
571static inline void pwm_disable(struct pwm_device *pwm)
572{
573}
574
Tushar Behera0bcf1682012-09-12 15:31:46 +0530575static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
576{
577 return -EINVAL;
578}
579
580static inline void *pwm_get_chip_data(struct pwm_device *pwm)
581{
582 return NULL;
583}
584
585static inline int pwmchip_add(struct pwm_chip *chip)
586{
587 return -EINVAL;
588}
589
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700590static inline int pwmchip_add_inversed(struct pwm_chip *chip)
591{
592 return -EINVAL;
593}
594
Tushar Behera0bcf1682012-09-12 15:31:46 +0530595static inline int pwmchip_remove(struct pwm_chip *chip)
596{
597 return -EINVAL;
598}
599
600static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
601 unsigned int index,
602 const char *label)
603{
604 return ERR_PTR(-ENODEV);
605}
606
607static inline struct pwm_device *pwm_get(struct device *dev,
608 const char *consumer)
609{
610 return ERR_PTR(-ENODEV);
611}
612
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800613static inline struct pwm_device *of_pwm_get(struct device_node *np,
614 const char *con_id)
615{
616 return ERR_PTR(-ENODEV);
617}
618
Tushar Behera0bcf1682012-09-12 15:31:46 +0530619static inline void pwm_put(struct pwm_device *pwm)
620{
621}
622
623static inline struct pwm_device *devm_pwm_get(struct device *dev,
624 const char *consumer)
625{
626 return ERR_PTR(-ENODEV);
627}
628
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800629static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
630 struct device_node *np,
631 const char *con_id)
632{
633 return ERR_PTR(-ENODEV);
634}
635
Tushar Behera0bcf1682012-09-12 15:31:46 +0530636static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
637{
638}
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100639
640static inline bool pwm_can_sleep(struct pwm_device *pwm)
641{
642 return false;
643}
Tushar Behera0bcf1682012-09-12 15:31:46 +0530644#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900645
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200646static inline void pwm_apply_args(struct pwm_device *pwm)
647{
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200648 struct pwm_state state = { };
649
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200650 /*
651 * PWM users calling pwm_apply_args() expect to have a fresh config
652 * where the polarity and period are set according to pwm_args info.
653 * The problem is, polarity can only be changed when the PWM is
654 * disabled.
655 *
656 * PWM drivers supporting hardware readout may declare the PWM device
657 * as enabled, and prevent polarity setting, which changes from the
658 * existing behavior, where all PWM devices are declared as disabled
659 * at startup (even if they are actually enabled), thus authorizing
660 * polarity setting.
661 *
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200662 * To fulfill this requirement, we apply a new state which disables
663 * the PWM device and set the reference period and polarity config.
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200664 *
665 * Note that PWM users requiring a smooth handover between the
666 * bootloader and the kernel (like critical regulators controlled by
667 * PWM devices) will have to switch to the atomic API and avoid calling
668 * pwm_apply_args().
669 */
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200670
671 state.enabled = false;
672 state.polarity = pwm->args.polarity;
673 state.period = pwm->args.period;
674
675 pwm_apply_state(pwm, &state);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200676}
677
Thierry Reding8138d2d2012-03-26 08:42:48 +0200678struct pwm_lookup {
679 struct list_head list;
680 const char *provider;
681 unsigned int index;
682 const char *dev_id;
683 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200684 unsigned int period;
685 enum pwm_polarity polarity;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200686};
687
Alexandre Belloni42844022014-05-19 22:42:37 +0200688#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200689 { \
690 .provider = _provider, \
691 .index = _index, \
692 .dev_id = _dev_id, \
693 .con_id = _con_id, \
Alexandre Belloni42844022014-05-19 22:42:37 +0200694 .period = _period, \
695 .polarity = _polarity \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200696 }
697
Tushar Behera0bcf1682012-09-12 15:31:46 +0530698#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200699void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530700void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530701#else
702static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
703{
704}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530705
706static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
707{
708}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100709#endif
710
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700711#ifdef CONFIG_PWM_SYSFS
712void pwmchip_sysfs_export(struct pwm_chip *chip);
713void pwmchip_sysfs_unexport(struct pwm_chip *chip);
David Hsu07334242016-08-09 14:57:46 -0700714void pwmchip_sysfs_unexport_children(struct pwm_chip *chip);
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700715#else
716static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
717{
718}
719
720static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
721{
722}
David Hsu07334242016-08-09 14:57:46 -0700723
724static inline void pwmchip_sysfs_unexport_children(struct pwm_chip *chip)
725{
726}
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700727#endif /* CONFIG_PWM_SYSFS */
728
Mark Vels5243ef82009-01-18 18:42:45 +0100729#endif /* __LINUX_PWM_H */