blob: 8bf5d5f6267d1eca6c0ad7df0bfd1d488aeea139 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Russell King1a189b92008-04-13 21:41:55 +01002#ifndef __LINUX_PWM_H
3#define __LINUX_PWM_H
4
Tushar Behera0bcf1682012-09-12 15:31:46 +05305#include <linux/err.h>
Jonathan Richardsond1cd2142015-10-16 17:40:58 -07006#include <linux/mutex.h>
Thierry Reding7299ab72011-12-14 11:10:32 +01007#include <linux/of.h>
8
Lee Jones3a3d1a42016-06-08 10:21:23 +01009struct pwm_capture;
Thierry Reding62099ab2012-03-26 09:31:48 +020010struct seq_file;
Lee Jones3a3d1a42016-06-08 10:21:23 +010011
Sascha Hauer0c2498f2011-01-28 09:40:40 +010012struct pwm_chip;
13
Philip, Avinash0aa0869c2012-07-24 19:35:32 +053014/**
15 * enum pwm_polarity - polarity of a PWM signal
16 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
17 * cycle, followed by a low signal for the remainder of the pulse
18 * period
19 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
20 * cycle, followed by a high signal for the remainder of the pulse
21 * period
22 */
23enum pwm_polarity {
24 PWM_POLARITY_NORMAL,
25 PWM_POLARITY_INVERSED,
26};
27
Boris Brezillone39c0df2016-04-14 21:17:21 +020028/**
29 * struct pwm_args - board-dependent PWM arguments
30 * @period: reference period
31 * @polarity: reference polarity
32 *
33 * This structure describes board-dependent arguments attached to a PWM
34 * device. These arguments are usually retrieved from the PWM lookup table or
35 * device tree.
36 *
37 * Do not confuse this with the PWM state: PWM arguments represent the initial
38 * configuration that users want to use on this PWM device rather than the
39 * current PWM hardware state.
40 */
41struct pwm_args {
42 unsigned int period;
43 enum pwm_polarity polarity;
44};
45
Thierry Redingf051c462011-12-14 11:12:23 +010046enum {
47 PWMF_REQUESTED = 1 << 0,
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020048 PWMF_EXPORTED = 1 << 1,
Thierry Redingf051c462011-12-14 11:12:23 +010049};
50
Boris Brezillon43a276b2016-04-14 21:17:38 +020051/*
52 * struct pwm_state - state of a PWM channel
53 * @period: PWM period (in nanoseconds)
54 * @duty_cycle: PWM duty cycle (in nanoseconds)
55 * @polarity: PWM polarity
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020056 * @enabled: PWM enabled status
Boris Brezillon43a276b2016-04-14 21:17:38 +020057 */
58struct pwm_state {
59 unsigned int period;
60 unsigned int duty_cycle;
61 enum pwm_polarity polarity;
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020062 bool enabled;
Boris Brezillon43a276b2016-04-14 21:17:38 +020063};
64
Thierry Reding04883802015-07-27 11:58:32 +020065/**
66 * struct pwm_device - PWM channel object
67 * @label: name of the PWM device
68 * @flags: flags associated with the PWM device
69 * @hwpwm: per-chip relative index of the PWM device
70 * @pwm: global index of the PWM device
71 * @chip: PWM chip providing this PWM device
72 * @chip_data: chip-private data associated with the PWM device
Boris Brezillone39c0df2016-04-14 21:17:21 +020073 * @args: PWM arguments
Boris Brezillon43a276b2016-04-14 21:17:38 +020074 * @state: curent PWM channel state
Thierry Reding04883802015-07-27 11:58:32 +020075 */
Thierry Redingf051c462011-12-14 11:12:23 +010076struct pwm_device {
Thierry Reding6bc70642015-07-27 11:57:28 +020077 const char *label;
78 unsigned long flags;
79 unsigned int hwpwm;
80 unsigned int pwm;
81 struct pwm_chip *chip;
82 void *chip_data;
Thierry Redingf051c462011-12-14 11:12:23 +010083
Boris Brezillone39c0df2016-04-14 21:17:21 +020084 struct pwm_args args;
Boris Brezillon43a276b2016-04-14 21:17:38 +020085 struct pwm_state state;
Thierry Redingf051c462011-12-14 11:12:23 +010086};
87
Boris Brezillon43a276b2016-04-14 21:17:38 +020088/**
89 * pwm_get_state() - retrieve the current PWM state
90 * @pwm: PWM device
91 * @state: state to fill with the current PWM state
92 */
93static inline void pwm_get_state(const struct pwm_device *pwm,
94 struct pwm_state *state)
95{
96 *state = pwm->state;
97}
98
Boris Brezillon5c312522015-07-01 10:21:47 +020099static inline bool pwm_is_enabled(const struct pwm_device *pwm)
100{
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200101 struct pwm_state state;
102
103 pwm_get_state(pwm, &state);
104
105 return state.enabled;
Boris Brezillon5c312522015-07-01 10:21:47 +0200106}
107
Thierry Redingf051c462011-12-14 11:12:23 +0100108static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
109{
110 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200111 pwm->state.period = period;
Thierry Redingf051c462011-12-14 11:12:23 +0100112}
113
Boris Brezillona1cf4212015-07-01 10:21:48 +0200114static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100115{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200116 struct pwm_state state;
117
118 pwm_get_state(pwm, &state);
119
120 return state.period;
Thierry Redingf051c462011-12-14 11:12:23 +0100121}
122
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700123static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
124{
125 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200126 pwm->state.duty_cycle = duty;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700127}
128
Boris Brezillona1cf4212015-07-01 10:21:48 +0200129static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700130{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200131 struct pwm_state state;
132
133 pwm_get_state(pwm, &state);
134
135 return state.duty_cycle;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700136}
137
Boris Brezillon011e7632015-07-01 10:21:49 +0200138static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
139{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200140 struct pwm_state state;
141
142 pwm_get_state(pwm, &state);
143
144 return state.polarity;
Boris Brezillon011e7632015-07-01 10:21:49 +0200145}
146
Boris Brezillone39c0df2016-04-14 21:17:21 +0200147static inline void pwm_get_args(const struct pwm_device *pwm,
148 struct pwm_args *args)
149{
150 *args = pwm->args;
151}
152
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100153/**
Boris Brezillona6a0dbb2016-06-14 11:13:09 +0200154 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
155 * @pwm: PWM device
156 * @state: state to fill with the prepared PWM state
157 *
158 * This functions prepares a state that can later be tweaked and applied
159 * to the PWM device with pwm_apply_state(). This is a convenient function
160 * that first retrieves the current PWM state and the replaces the period
161 * and polarity fields with the reference values defined in pwm->args.
162 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
163 * fields according to your needs before calling pwm_apply_state().
164 *
165 * ->duty_cycle is initially set to zero to avoid cases where the current
166 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
167 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
168 * first.
169 */
170static inline void pwm_init_state(const struct pwm_device *pwm,
171 struct pwm_state *state)
172{
173 struct pwm_args args;
174
175 /* First get the current state. */
176 pwm_get_state(pwm, state);
177
178 /* Then fill it with the reference config */
179 pwm_get_args(pwm, &args);
180
181 state->period = args.period;
182 state->polarity = args.polarity;
183 state->duty_cycle = 0;
184}
185
186/**
Boris Brezillonf6f3bdd2016-06-14 11:13:10 +0200187 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
188 * @state: PWM state to extract the duty cycle from
189 * @scale: target scale of the relative duty cycle
190 *
191 * This functions converts the absolute duty cycle stored in @state (expressed
192 * in nanosecond) into a value relative to the period.
193 *
194 * For example if you want to get the duty_cycle expressed in percent, call:
195 *
196 * pwm_get_state(pwm, &state);
197 * duty = pwm_get_relative_duty_cycle(&state, 100);
198 */
199static inline unsigned int
200pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
201{
202 if (!state->period)
203 return 0;
204
205 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
206 state->period);
207}
208
209/**
210 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
211 * @state: PWM state to fill
212 * @duty_cycle: relative duty cycle value
213 * @scale: scale in which @duty_cycle is expressed
214 *
215 * This functions converts a relative into an absolute duty cycle (expressed
216 * in nanoseconds), and puts the result in state->duty_cycle.
217 *
218 * For example if you want to configure a 50% duty cycle, call:
219 *
220 * pwm_init_state(pwm, &state);
221 * pwm_set_relative_duty_cycle(&state, 50, 100);
222 * pwm_apply_state(pwm, &state);
223 *
224 * This functions returns -EINVAL if @duty_cycle and/or @scale are
225 * inconsistent (@scale == 0 or @duty_cycle > @scale).
226 */
227static inline int
228pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
229 unsigned int scale)
230{
231 if (!scale || duty_cycle > scale)
232 return -EINVAL;
233
234 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
235 state->period,
236 scale);
237
238 return 0;
239}
240
241/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100242 * struct pwm_ops - PWM controller operations
243 * @request: optional hook for requesting a PWM
244 * @free: optional hook for freeing a PWM
Lee Jones3a3d1a42016-06-08 10:21:23 +0100245 * @capture: capture and report PWM signal
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200246 * @apply: atomically apply a new PWM config. The state argument
247 * should be adjusted with the real hardware config (if the
248 * approximate the period or duty_cycle value, state should
249 * reflect it)
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200250 * @get_state: get the current PWM state. This function is only
251 * called once per PWM device when the PWM chip is
252 * registered.
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100253 * @owner: helps prevent removal of modules exporting active PWMs
Uwe Kleine-König5d0a4c12019-01-07 20:49:41 +0100254 * @config: configure duty cycles and period length for this PWM
255 * @set_polarity: configure the polarity of this PWM
256 * @enable: enable PWM output toggling
257 * @disable: disable PWM output toggling
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100258 */
259struct pwm_ops {
Thierry Reding6bc70642015-07-27 11:57:28 +0200260 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
261 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
Lee Jones3a3d1a42016-06-08 10:21:23 +0100262 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
263 struct pwm_capture *result, unsigned long timeout);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200264 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
265 struct pwm_state *state);
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200266 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
267 struct pwm_state *state);
Thierry Reding6bc70642015-07-27 11:57:28 +0200268 struct module *owner;
Uwe Kleine-König5d0a4c12019-01-07 20:49:41 +0100269
270 /* Only used by legacy drivers */
271 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
272 int duty_ns, int period_ns);
273 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
274 enum pwm_polarity polarity);
275 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
276 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100277};
278
279/**
Thierry Redingf051c462011-12-14 11:12:23 +0100280 * struct pwm_chip - abstract a PWM controller
281 * @dev: device providing the PWMs
Thierry Redingf051c462011-12-14 11:12:23 +0100282 * @ops: callbacks for this PWM controller
283 * @base: number of first PWM controlled by this chip
284 * @npwm: number of PWMs controlled by this chip
Thierry Reding04883802015-07-27 11:58:32 +0200285 * @of_xlate: request a PWM device given a device tree PWM specifier
286 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
Uwe Kleine-König5d0a4c12019-01-07 20:49:41 +0100287 * @list: list node for internal use
288 * @pwms: array of PWM devices allocated by the framework
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100289 */
290struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200291 struct device *dev;
Thierry Reding6bc70642015-07-27 11:57:28 +0200292 const struct pwm_ops *ops;
293 int base;
294 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100295
Thierry Reding6bc70642015-07-27 11:57:28 +0200296 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
297 const struct of_phandle_args *args);
298 unsigned int of_pwm_n_cells;
Uwe Kleine-König5d0a4c12019-01-07 20:49:41 +0100299
300 /* only used internally by the PWM framework */
301 struct list_head list;
302 struct pwm_device *pwms;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100303};
304
Lee Jones3a3d1a42016-06-08 10:21:23 +0100305/**
306 * struct pwm_capture - PWM capture data
307 * @period: period of the PWM signal (in nanoseconds)
308 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
309 */
310struct pwm_capture {
311 unsigned int period;
312 unsigned int duty_cycle;
313};
314
Tushar Behera0bcf1682012-09-12 15:31:46 +0530315#if IS_ENABLED(CONFIG_PWM)
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200316/* PWM user APIs */
317struct pwm_device *pwm_request(int pwm_id, const char *label);
318void pwm_free(struct pwm_device *pwm);
319int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
320int pwm_adjust_config(struct pwm_device *pwm);
321
322/**
323 * pwm_config() - change a PWM device configuration
324 * @pwm: PWM device
325 * @duty_ns: "on" time (in nanoseconds)
326 * @period_ns: duration (in nanoseconds) of one cycle
327 *
328 * Returns: 0 on success or a negative error code on failure.
329 */
330static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
331 int period_ns)
332{
333 struct pwm_state state;
334
335 if (!pwm)
336 return -EINVAL;
337
Brian Norrisef2bf492016-05-27 09:45:49 -0700338 if (duty_ns < 0 || period_ns < 0)
339 return -EINVAL;
340
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200341 pwm_get_state(pwm, &state);
342 if (state.duty_cycle == duty_ns && state.period == period_ns)
343 return 0;
344
345 state.duty_cycle = duty_ns;
346 state.period = period_ns;
347 return pwm_apply_state(pwm, &state);
348}
349
350/**
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200351 * pwm_enable() - start a PWM output toggling
352 * @pwm: PWM device
353 *
354 * Returns: 0 on success or a negative error code on failure.
355 */
356static inline int pwm_enable(struct pwm_device *pwm)
357{
358 struct pwm_state state;
359
360 if (!pwm)
361 return -EINVAL;
362
363 pwm_get_state(pwm, &state);
364 if (state.enabled)
365 return 0;
366
367 state.enabled = true;
368 return pwm_apply_state(pwm, &state);
369}
370
371/**
372 * pwm_disable() - stop a PWM output toggling
373 * @pwm: PWM device
374 */
375static inline void pwm_disable(struct pwm_device *pwm)
376{
377 struct pwm_state state;
378
379 if (!pwm)
380 return;
381
382 pwm_get_state(pwm, &state);
383 if (!state.enabled)
384 return;
385
386 state.enabled = false;
387 pwm_apply_state(pwm, &state);
388}
389
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200390/* PWM provider APIs */
Lee Jones3a3d1a42016-06-08 10:21:23 +0100391int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
392 unsigned long timeout);
Thierry Redingf051c462011-12-14 11:12:23 +0100393int pwm_set_chip_data(struct pwm_device *pwm, void *data);
394void *pwm_get_chip_data(struct pwm_device *pwm);
395
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700396int pwmchip_add_with_polarity(struct pwm_chip *chip,
397 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100398int pwmchip_add(struct pwm_chip *chip);
399int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100400struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
401 unsigned int index,
402 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200403
Philip, Avinash83af2402012-11-21 13:10:44 +0530404struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
405 const struct of_phandle_args *args);
406
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800407struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Fabrice Gasnierb2c200e2019-04-18 11:37:47 +0200408struct pwm_device *of_pwm_get(struct device *dev, struct device_node *np,
409 const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200410void pwm_put(struct pwm_device *pwm);
411
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800412struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800413struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
414 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900415void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530416#else
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200417static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
418{
419 return ERR_PTR(-ENODEV);
420}
421
422static inline void pwm_free(struct pwm_device *pwm)
423{
424}
425
426static inline int pwm_apply_state(struct pwm_device *pwm,
427 const struct pwm_state *state)
428{
429 return -ENOTSUPP;
430}
431
432static inline int pwm_adjust_config(struct pwm_device *pwm)
433{
434 return -ENOTSUPP;
435}
436
437static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
438 int period_ns)
439{
440 return -EINVAL;
441}
442
Lee Jones3a3d1a42016-06-08 10:21:23 +0100443static inline int pwm_capture(struct pwm_device *pwm,
444 struct pwm_capture *result,
445 unsigned long timeout)
446{
447 return -EINVAL;
448}
449
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200450static inline int pwm_enable(struct pwm_device *pwm)
451{
452 return -EINVAL;
453}
454
455static inline void pwm_disable(struct pwm_device *pwm)
456{
457}
458
Tushar Behera0bcf1682012-09-12 15:31:46 +0530459static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
460{
461 return -EINVAL;
462}
463
464static inline void *pwm_get_chip_data(struct pwm_device *pwm)
465{
466 return NULL;
467}
468
469static inline int pwmchip_add(struct pwm_chip *chip)
470{
471 return -EINVAL;
472}
473
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700474static inline int pwmchip_add_inversed(struct pwm_chip *chip)
475{
476 return -EINVAL;
477}
478
Tushar Behera0bcf1682012-09-12 15:31:46 +0530479static inline int pwmchip_remove(struct pwm_chip *chip)
480{
481 return -EINVAL;
482}
483
484static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
485 unsigned int index,
486 const char *label)
487{
488 return ERR_PTR(-ENODEV);
489}
490
491static inline struct pwm_device *pwm_get(struct device *dev,
492 const char *consumer)
493{
494 return ERR_PTR(-ENODEV);
495}
496
Fabrice Gasnierb2c200e2019-04-18 11:37:47 +0200497static inline struct pwm_device *of_pwm_get(struct device *dev,
498 struct device_node *np,
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800499 const char *con_id)
500{
501 return ERR_PTR(-ENODEV);
502}
503
Tushar Behera0bcf1682012-09-12 15:31:46 +0530504static inline void pwm_put(struct pwm_device *pwm)
505{
506}
507
508static inline struct pwm_device *devm_pwm_get(struct device *dev,
509 const char *consumer)
510{
511 return ERR_PTR(-ENODEV);
512}
513
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800514static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
515 struct device_node *np,
516 const char *con_id)
517{
518 return ERR_PTR(-ENODEV);
519}
520
Tushar Behera0bcf1682012-09-12 15:31:46 +0530521static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
522{
523}
524#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900525
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200526static inline void pwm_apply_args(struct pwm_device *pwm)
527{
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200528 struct pwm_state state = { };
529
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200530 /*
531 * PWM users calling pwm_apply_args() expect to have a fresh config
532 * where the polarity and period are set according to pwm_args info.
533 * The problem is, polarity can only be changed when the PWM is
534 * disabled.
535 *
536 * PWM drivers supporting hardware readout may declare the PWM device
537 * as enabled, and prevent polarity setting, which changes from the
538 * existing behavior, where all PWM devices are declared as disabled
539 * at startup (even if they are actually enabled), thus authorizing
540 * polarity setting.
541 *
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200542 * To fulfill this requirement, we apply a new state which disables
543 * the PWM device and set the reference period and polarity config.
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200544 *
545 * Note that PWM users requiring a smooth handover between the
546 * bootloader and the kernel (like critical regulators controlled by
547 * PWM devices) will have to switch to the atomic API and avoid calling
548 * pwm_apply_args().
549 */
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200550
551 state.enabled = false;
552 state.polarity = pwm->args.polarity;
553 state.period = pwm->args.period;
554
555 pwm_apply_state(pwm, &state);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200556}
557
Thierry Reding8138d2d2012-03-26 08:42:48 +0200558struct pwm_lookup {
559 struct list_head list;
560 const char *provider;
561 unsigned int index;
562 const char *dev_id;
563 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200564 unsigned int period;
565 enum pwm_polarity polarity;
Hans de Goedeb526a312017-01-22 17:14:08 +0100566 const char *module; /* optional, may be NULL */
Thierry Reding8138d2d2012-03-26 08:42:48 +0200567};
568
Hans de Goedeb526a312017-01-22 17:14:08 +0100569#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
570 _period, _polarity, _module) \
571 { \
572 .provider = _provider, \
573 .index = _index, \
574 .dev_id = _dev_id, \
575 .con_id = _con_id, \
576 .period = _period, \
577 .polarity = _polarity, \
578 .module = _module, \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200579 }
580
Hans de Goedeb526a312017-01-22 17:14:08 +0100581#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
582 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
583 _polarity, NULL)
584
Tushar Behera0bcf1682012-09-12 15:31:46 +0530585#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200586void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530587void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530588#else
589static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
590{
591}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530592
593static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
594{
595}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100596#endif
597
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700598#ifdef CONFIG_PWM_SYSFS
599void pwmchip_sysfs_export(struct pwm_chip *chip);
600void pwmchip_sysfs_unexport(struct pwm_chip *chip);
601#else
602static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
603{
604}
605
606static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
607{
608}
609#endif /* CONFIG_PWM_SYSFS */
610
Mark Vels5243ef82009-01-18 18:42:45 +0100611#endif /* __LINUX_PWM_H */