blob: ef2743cc3390c5b6b3182011c8dc0b75442549b6 [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 {
Fenglin Wu9faf2742018-09-10 10:05:52 +080041 u64 period;
Boris Brezillone39c0df2016-04-14 21:17:21 +020042 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 {
Fenglin Wu9faf2742018-09-10 10:05:52 +080068 u64 *duty_pattern;
Fenglin Wuccee0bb2017-12-04 09:56:51 +080069 unsigned int num_entries;
Fenglin Wu9faf2742018-09-10 10:05:52 +080070 u64 cycles_per_duty;
Fenglin Wuccee0bb2017-12-04 09:56:51 +080071};
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 {
Fenglin Wu9faf2742018-09-10 10:05:52 +080081 u64 period;
82 u64 duty_cycle;
Boris Brezillon43a276b2016-04-14 21:17:38 +020083 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
Fenglin Wu9faf2742018-09-10 10:05:52 +0800138static inline void pwm_set_period_extend(struct pwm_device *pwm, u64 period)
139{
140 if (pwm)
141 pwm->state.period = period;
142}
143
Boris Brezillona1cf4212015-07-01 10:21:48 +0200144static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100145{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200146 struct pwm_state state;
147
148 pwm_get_state(pwm, &state);
149
Fenglin Wu9faf2742018-09-10 10:05:52 +0800150 if (state.period > UINT_MAX)
151 pr_warn("PWM period %llu is truncated\n", state.period);
152
153 return (unsigned int)state.period;
154}
155
156static inline u64 pwm_get_period_extend(const struct pwm_device *pwm)
157{
158 struct pwm_state state;
159
160 pwm_get_state(pwm, &state);
161
Boris Brezillon43a276b2016-04-14 21:17:38 +0200162 return state.period;
Thierry Redingf051c462011-12-14 11:12:23 +0100163}
164
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700165static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
166{
167 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200168 pwm->state.duty_cycle = duty;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700169}
170
Fenglin Wu9faf2742018-09-10 10:05:52 +0800171static inline void pwm_set_duty_cycle_extend(struct pwm_device *pwm, u64 duty)
172{
173 if (pwm)
174 pwm->state.duty_cycle = duty;
175}
176
Boris Brezillona1cf4212015-07-01 10:21:48 +0200177static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700178{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200179 struct pwm_state state;
180
181 pwm_get_state(pwm, &state);
182
Fenglin Wu9faf2742018-09-10 10:05:52 +0800183 if (state.duty_cycle > UINT_MAX)
184 pr_warn("PWM duty cycle %llu is truncated\n", state.duty_cycle);
185
186 return (unsigned int)state.duty_cycle;
187}
188
189static inline u64 pwm_get_duty_cycle_extend(const struct pwm_device *pwm)
190{
191 struct pwm_state state;
192
193 pwm_get_state(pwm, &state);
194
Boris Brezillon43a276b2016-04-14 21:17:38 +0200195 return state.duty_cycle;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700196}
197
Boris Brezillon011e7632015-07-01 10:21:49 +0200198static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
199{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200200 struct pwm_state state;
201
202 pwm_get_state(pwm, &state);
203
204 return state.polarity;
Boris Brezillon011e7632015-07-01 10:21:49 +0200205}
206
Fenglin Wuccee0bb2017-12-04 09:56:51 +0800207static inline enum pwm_output_type pwm_get_output_type(
208 const struct pwm_device *pwm)
209{
210 struct pwm_state state;
211
212 pwm_get_state(pwm, &state);
213
214 return state.output_type;
215}
216
217static inline struct pwm_output_pattern *pwm_get_output_pattern(
218 struct pwm_device *pwm)
219{
220 struct pwm_state state;
221
222 pwm_get_state(pwm, &state);
223
224 return pwm->state.output_pattern ?: NULL;
225}
226
Boris Brezillone39c0df2016-04-14 21:17:21 +0200227static inline void pwm_get_args(const struct pwm_device *pwm,
228 struct pwm_args *args)
229{
230 *args = pwm->args;
231}
232
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100233/**
Boris Brezillona6a0dbb2016-06-14 11:13:09 +0200234 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
235 * @pwm: PWM device
236 * @state: state to fill with the prepared PWM state
237 *
238 * This functions prepares a state that can later be tweaked and applied
239 * to the PWM device with pwm_apply_state(). This is a convenient function
240 * that first retrieves the current PWM state and the replaces the period
241 * and polarity fields with the reference values defined in pwm->args.
242 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
243 * fields according to your needs before calling pwm_apply_state().
244 *
245 * ->duty_cycle is initially set to zero to avoid cases where the current
246 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
247 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
248 * first.
249 */
250static inline void pwm_init_state(const struct pwm_device *pwm,
251 struct pwm_state *state)
252{
253 struct pwm_args args;
254
255 /* First get the current state. */
256 pwm_get_state(pwm, state);
257
258 /* Then fill it with the reference config */
259 pwm_get_args(pwm, &args);
260
261 state->period = args.period;
262 state->polarity = args.polarity;
263 state->duty_cycle = 0;
264}
265
266/**
Boris Brezillonf6f3bdd2016-06-14 11:13:10 +0200267 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
268 * @state: PWM state to extract the duty cycle from
269 * @scale: target scale of the relative duty cycle
270 *
271 * This functions converts the absolute duty cycle stored in @state (expressed
272 * in nanosecond) into a value relative to the period.
273 *
274 * For example if you want to get the duty_cycle expressed in percent, call:
275 *
276 * pwm_get_state(pwm, &state);
277 * duty = pwm_get_relative_duty_cycle(&state, 100);
278 */
279static inline unsigned int
280pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
281{
282 if (!state->period)
283 return 0;
284
285 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
286 state->period);
287}
288
289/**
290 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
291 * @state: PWM state to fill
292 * @duty_cycle: relative duty cycle value
293 * @scale: scale in which @duty_cycle is expressed
294 *
295 * This functions converts a relative into an absolute duty cycle (expressed
296 * in nanoseconds), and puts the result in state->duty_cycle.
297 *
298 * For example if you want to configure a 50% duty cycle, call:
299 *
300 * pwm_init_state(pwm, &state);
301 * pwm_set_relative_duty_cycle(&state, 50, 100);
302 * pwm_apply_state(pwm, &state);
303 *
304 * This functions returns -EINVAL if @duty_cycle and/or @scale are
305 * inconsistent (@scale == 0 or @duty_cycle > @scale).
306 */
307static inline int
308pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
309 unsigned int scale)
310{
311 if (!scale || duty_cycle > scale)
312 return -EINVAL;
313
314 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
315 state->period,
316 scale);
317
318 return 0;
319}
320
321/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100322 * struct pwm_ops - PWM controller operations
323 * @request: optional hook for requesting a PWM
324 * @free: optional hook for freeing a PWM
325 * @config: configure duty cycles and period length for this PWM
Fenglin Wu9faf2742018-09-10 10:05:52 +0800326 * @config_extend: configure duty cycles and period length for this
327 * PWM with u64 data type
Philip, Avinash0aa08692012-07-24 19:35:32 +0530328 * @set_polarity: configure the polarity of this PWM
Lee Jones3a3d1a42016-06-08 10:21:23 +0100329 * @capture: capture and report PWM signal
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100330 * @enable: enable PWM output toggling
331 * @disable: disable PWM output toggling
Fenglin Wuccee0bb2017-12-04 09:56:51 +0800332 * @get_output_type_supported: get the supported output type
333 * @set_output_type: set PWM output type
334 * @set_output_pattern: set the pattern for the modulated output
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200335 * @apply: atomically apply a new PWM config. The state argument
336 * should be adjusted with the real hardware config (if the
337 * approximate the period or duty_cycle value, state should
338 * reflect it)
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200339 * @get_state: get the current PWM state. This function is only
340 * called once per PWM device when the PWM chip is
341 * registered.
Thierry Reding62099ab2012-03-26 09:31:48 +0200342 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100343 * @owner: helps prevent removal of modules exporting active PWMs
344 */
345struct pwm_ops {
Thierry Reding6bc70642015-07-27 11:57:28 +0200346 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
347 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
348 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
349 int duty_ns, int period_ns);
Fenglin Wu9faf2742018-09-10 10:05:52 +0800350 int (*config_extend)(struct pwm_chip *chip, struct pwm_device *pwm,
351 u64 duty_ns, u64 period_ns);
Thierry Reding6bc70642015-07-27 11:57:28 +0200352 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
353 enum pwm_polarity polarity);
Lee Jones3a3d1a42016-06-08 10:21:23 +0100354 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
355 struct pwm_capture *result, unsigned long timeout);
Thierry Reding6bc70642015-07-27 11:57:28 +0200356 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
357 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Fenglin Wuccee0bb2017-12-04 09:56:51 +0800358 int (*get_output_type_supported)(struct pwm_chip *chip,
359 struct pwm_device *pwm);
360 int (*set_output_type)(struct pwm_chip *chip, struct pwm_device *pwm,
361 enum pwm_output_type output_type);
362 int (*set_output_pattern)(struct pwm_chip *chip,
363 struct pwm_device *pwm,
364 struct pwm_output_pattern *output_pattern);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200365 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
366 struct pwm_state *state);
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200367 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
368 struct pwm_state *state);
Thierry Reding62099ab2012-03-26 09:31:48 +0200369#ifdef CONFIG_DEBUG_FS
Thierry Reding6bc70642015-07-27 11:57:28 +0200370 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
Thierry Reding62099ab2012-03-26 09:31:48 +0200371#endif
Thierry Reding6bc70642015-07-27 11:57:28 +0200372 struct module *owner;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100373};
374
375/**
Thierry Redingf051c462011-12-14 11:12:23 +0100376 * struct pwm_chip - abstract a PWM controller
377 * @dev: device providing the PWMs
378 * @list: list node for internal use
379 * @ops: callbacks for this PWM controller
380 * @base: number of first PWM controlled by this chip
381 * @npwm: number of PWMs controlled by this chip
382 * @pwms: array of PWM devices allocated by the framework
Thierry Reding04883802015-07-27 11:58:32 +0200383 * @of_xlate: request a PWM device given a device tree PWM specifier
384 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100385 * @can_sleep: must be true if the .config(), .enable() or .disable()
386 * operations may sleep
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100387 */
388struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200389 struct device *dev;
390 struct list_head list;
391 const struct pwm_ops *ops;
392 int base;
393 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100394
Thierry Reding6bc70642015-07-27 11:57:28 +0200395 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100396
Thierry Reding6bc70642015-07-27 11:57:28 +0200397 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
398 const struct of_phandle_args *args);
399 unsigned int of_pwm_n_cells;
400 bool can_sleep;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100401};
402
Lee Jones3a3d1a42016-06-08 10:21:23 +0100403/**
404 * struct pwm_capture - PWM capture data
405 * @period: period of the PWM signal (in nanoseconds)
406 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
407 */
408struct pwm_capture {
Fenglin Wu9faf2742018-09-10 10:05:52 +0800409 u64 period;
410 u64 duty_cycle;
Lee Jones3a3d1a42016-06-08 10:21:23 +0100411};
412
Tushar Behera0bcf1682012-09-12 15:31:46 +0530413#if IS_ENABLED(CONFIG_PWM)
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200414/* PWM user APIs */
415struct pwm_device *pwm_request(int pwm_id, const char *label);
416void pwm_free(struct pwm_device *pwm);
417int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
418int pwm_adjust_config(struct pwm_device *pwm);
419
420/**
Fenglin Wuccee0bb2017-12-04 09:56:51 +0800421 * pwm_output_type_support()
422 * @pwm: PWM device
423 *
424 * Returns: output types supported by the PWM device
425 */
426static inline int pwm_get_output_type_supported(struct pwm_device *pwm)
427{
428 if (pwm->chip->ops->get_output_type_supported != NULL)
429 return pwm->chip->ops->
430 get_output_type_supported(pwm->chip, pwm);
431 else
432 return PWM_OUTPUT_FIXED;
433}
434
435/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200436 * pwm_config() - change a PWM device configuration
437 * @pwm: PWM device
438 * @duty_ns: "on" time (in nanoseconds)
439 * @period_ns: duration (in nanoseconds) of one cycle
440 *
441 * Returns: 0 on success or a negative error code on failure.
442 */
443static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
444 int period_ns)
445{
446 struct pwm_state state;
447
448 if (!pwm)
449 return -EINVAL;
450
Brian Norrisef2bf492016-05-27 09:45:49 -0700451 if (duty_ns < 0 || period_ns < 0)
452 return -EINVAL;
453
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200454 pwm_get_state(pwm, &state);
455 if (state.duty_cycle == duty_ns && state.period == period_ns)
456 return 0;
457
458 state.duty_cycle = duty_ns;
459 state.period = period_ns;
460 return pwm_apply_state(pwm, &state);
461}
462
463/**
Fenglin Wu9faf2742018-09-10 10:05:52 +0800464 * pwm_config_extend() - change PWM period and duty length with u64 data type
465 * @pwm: PWM device
466 * @duty_ns: "on" time (in nanoseconds)
467 * @period_ns: duration (in nanoseconds) of one cycle
468 *
469 * Returns: 0 on success or a negative error code on failure.
470 */
471static inline int pwm_config_extend(struct pwm_device *pwm, u64 duty_ns,
472 u64 period_ns)
473{
474 struct pwm_state state;
475
476 if (!pwm)
477 return -EINVAL;
478
479 pwm_get_state(pwm, &state);
480 if (state.duty_cycle == duty_ns && state.period == period_ns)
481 return 0;
482
483 state.duty_cycle = duty_ns;
484 state.period = period_ns;
485 return pwm_apply_state(pwm, &state);
486}
487
488/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200489 * pwm_set_polarity() - configure the polarity of a PWM signal
490 * @pwm: PWM device
491 * @polarity: new polarity of the PWM signal
492 *
493 * Note that the polarity cannot be configured while the PWM device is
494 * enabled.
495 *
496 * Returns: 0 on success or a negative error code on failure.
497 */
498static inline int pwm_set_polarity(struct pwm_device *pwm,
499 enum pwm_polarity polarity)
500{
501 struct pwm_state state;
502
503 if (!pwm)
504 return -EINVAL;
505
506 pwm_get_state(pwm, &state);
507 if (state.polarity == polarity)
508 return 0;
509
510 /*
511 * Changing the polarity of a running PWM without adjusting the
512 * dutycycle/period value is a bit risky (can introduce glitches).
513 * Return -EBUSY in this case.
514 * Note that this is allowed when using pwm_apply_state() because
515 * the user specifies all the parameters.
516 */
517 if (state.enabled)
518 return -EBUSY;
519
520 state.polarity = polarity;
521 return pwm_apply_state(pwm, &state);
522}
523
524/**
525 * pwm_enable() - start a PWM output toggling
526 * @pwm: PWM device
527 *
528 * Returns: 0 on success or a negative error code on failure.
529 */
530static inline int pwm_enable(struct pwm_device *pwm)
531{
532 struct pwm_state state;
533
534 if (!pwm)
535 return -EINVAL;
536
537 pwm_get_state(pwm, &state);
538 if (state.enabled)
539 return 0;
540
541 state.enabled = true;
542 return pwm_apply_state(pwm, &state);
543}
544
545/**
546 * pwm_disable() - stop a PWM output toggling
547 * @pwm: PWM device
548 */
549static inline void pwm_disable(struct pwm_device *pwm)
550{
551 struct pwm_state state;
552
553 if (!pwm)
554 return;
555
556 pwm_get_state(pwm, &state);
557 if (!state.enabled)
558 return;
559
560 state.enabled = false;
561 pwm_apply_state(pwm, &state);
562}
563
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200564/* PWM provider APIs */
Lee Jones3a3d1a42016-06-08 10:21:23 +0100565int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
566 unsigned long timeout);
Thierry Redingf051c462011-12-14 11:12:23 +0100567int pwm_set_chip_data(struct pwm_device *pwm, void *data);
568void *pwm_get_chip_data(struct pwm_device *pwm);
569
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700570int pwmchip_add_with_polarity(struct pwm_chip *chip,
571 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100572int pwmchip_add(struct pwm_chip *chip);
573int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100574struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
575 unsigned int index,
576 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200577
Philip, Avinash83af2402012-11-21 13:10:44 +0530578struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
579 const struct of_phandle_args *args);
580
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800581struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800582struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200583void pwm_put(struct pwm_device *pwm);
584
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800585struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800586struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
587 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900588void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100589
590bool pwm_can_sleep(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530591#else
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200592static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
593{
594 return ERR_PTR(-ENODEV);
595}
596
597static inline void pwm_free(struct pwm_device *pwm)
598{
599}
600
601static inline int pwm_apply_state(struct pwm_device *pwm,
602 const struct pwm_state *state)
603{
604 return -ENOTSUPP;
605}
606
607static inline int pwm_adjust_config(struct pwm_device *pwm)
608{
609 return -ENOTSUPP;
610}
611
612static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
613 int period_ns)
614{
615 return -EINVAL;
616}
617
Lee Jones3a3d1a42016-06-08 10:21:23 +0100618static inline int pwm_capture(struct pwm_device *pwm,
619 struct pwm_capture *result,
620 unsigned long timeout)
621{
622 return -EINVAL;
623}
624
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200625static inline int pwm_set_polarity(struct pwm_device *pwm,
626 enum pwm_polarity polarity)
627{
628 return -ENOTSUPP;
629}
630
631static inline int pwm_enable(struct pwm_device *pwm)
632{
633 return -EINVAL;
634}
635
636static inline void pwm_disable(struct pwm_device *pwm)
637{
638}
639
Tushar Behera0bcf1682012-09-12 15:31:46 +0530640static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
641{
642 return -EINVAL;
643}
644
645static inline void *pwm_get_chip_data(struct pwm_device *pwm)
646{
647 return NULL;
648}
649
650static inline int pwmchip_add(struct pwm_chip *chip)
651{
652 return -EINVAL;
653}
654
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700655static inline int pwmchip_add_inversed(struct pwm_chip *chip)
656{
657 return -EINVAL;
658}
659
Tushar Behera0bcf1682012-09-12 15:31:46 +0530660static inline int pwmchip_remove(struct pwm_chip *chip)
661{
662 return -EINVAL;
663}
664
665static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
666 unsigned int index,
667 const char *label)
668{
669 return ERR_PTR(-ENODEV);
670}
671
672static inline struct pwm_device *pwm_get(struct device *dev,
673 const char *consumer)
674{
675 return ERR_PTR(-ENODEV);
676}
677
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800678static inline struct pwm_device *of_pwm_get(struct device_node *np,
679 const char *con_id)
680{
681 return ERR_PTR(-ENODEV);
682}
683
Tushar Behera0bcf1682012-09-12 15:31:46 +0530684static inline void pwm_put(struct pwm_device *pwm)
685{
686}
687
688static inline struct pwm_device *devm_pwm_get(struct device *dev,
689 const char *consumer)
690{
691 return ERR_PTR(-ENODEV);
692}
693
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800694static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
695 struct device_node *np,
696 const char *con_id)
697{
698 return ERR_PTR(-ENODEV);
699}
700
Tushar Behera0bcf1682012-09-12 15:31:46 +0530701static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
702{
703}
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100704
705static inline bool pwm_can_sleep(struct pwm_device *pwm)
706{
707 return false;
708}
Tushar Behera0bcf1682012-09-12 15:31:46 +0530709#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900710
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200711static inline void pwm_apply_args(struct pwm_device *pwm)
712{
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200713 struct pwm_state state = { };
714
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200715 /*
716 * PWM users calling pwm_apply_args() expect to have a fresh config
717 * where the polarity and period are set according to pwm_args info.
718 * The problem is, polarity can only be changed when the PWM is
719 * disabled.
720 *
721 * PWM drivers supporting hardware readout may declare the PWM device
722 * as enabled, and prevent polarity setting, which changes from the
723 * existing behavior, where all PWM devices are declared as disabled
724 * at startup (even if they are actually enabled), thus authorizing
725 * polarity setting.
726 *
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200727 * To fulfill this requirement, we apply a new state which disables
728 * the PWM device and set the reference period and polarity config.
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200729 *
730 * Note that PWM users requiring a smooth handover between the
731 * bootloader and the kernel (like critical regulators controlled by
732 * PWM devices) will have to switch to the atomic API and avoid calling
733 * pwm_apply_args().
734 */
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200735
736 state.enabled = false;
737 state.polarity = pwm->args.polarity;
738 state.period = pwm->args.period;
739
740 pwm_apply_state(pwm, &state);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200741}
742
Thierry Reding8138d2d2012-03-26 08:42:48 +0200743struct pwm_lookup {
744 struct list_head list;
745 const char *provider;
746 unsigned int index;
747 const char *dev_id;
748 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200749 unsigned int period;
750 enum pwm_polarity polarity;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200751};
752
Alexandre Belloni42844022014-05-19 22:42:37 +0200753#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200754 { \
755 .provider = _provider, \
756 .index = _index, \
757 .dev_id = _dev_id, \
758 .con_id = _con_id, \
Alexandre Belloni42844022014-05-19 22:42:37 +0200759 .period = _period, \
760 .polarity = _polarity \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200761 }
762
Tushar Behera0bcf1682012-09-12 15:31:46 +0530763#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200764void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530765void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530766#else
767static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
768{
769}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530770
771static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
772{
773}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100774#endif
775
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700776#ifdef CONFIG_PWM_SYSFS
777void pwmchip_sysfs_export(struct pwm_chip *chip);
778void pwmchip_sysfs_unexport(struct pwm_chip *chip);
779#else
780static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
781{
782}
783
784static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
785{
786}
787#endif /* CONFIG_PWM_SYSFS */
788
Mark Vels5243ef82009-01-18 18:42:45 +0100789#endif /* __LINUX_PWM_H */