blob: 08fad7c6a4713c459051779baf4bc8e909bc3377 [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, Avinash0aa0869c2012-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
Boris Brezillon43a276b2016-04-14 21:17:38 +020050/*
51 * struct pwm_state - state of a PWM channel
52 * @period: PWM period (in nanoseconds)
53 * @duty_cycle: PWM duty cycle (in nanoseconds)
54 * @polarity: PWM polarity
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020055 * @enabled: PWM enabled status
Boris Brezillon43a276b2016-04-14 21:17:38 +020056 */
57struct pwm_state {
58 unsigned int period;
59 unsigned int duty_cycle;
60 enum pwm_polarity polarity;
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020061 bool enabled;
Boris Brezillon43a276b2016-04-14 21:17:38 +020062};
63
Thierry Reding04883802015-07-27 11:58:32 +020064/**
65 * struct pwm_device - PWM channel object
66 * @label: name of the PWM device
67 * @flags: flags associated with the PWM device
68 * @hwpwm: per-chip relative index of the PWM device
69 * @pwm: global index of the PWM device
70 * @chip: PWM chip providing this PWM device
71 * @chip_data: chip-private data associated with the PWM device
Boris Brezillone39c0df2016-04-14 21:17:21 +020072 * @args: PWM arguments
Boris Brezillon43a276b2016-04-14 21:17:38 +020073 * @state: curent PWM channel state
Thierry Reding04883802015-07-27 11:58:32 +020074 */
Thierry Redingf051c462011-12-14 11:12:23 +010075struct pwm_device {
Thierry Reding6bc70642015-07-27 11:57:28 +020076 const char *label;
77 unsigned long flags;
78 unsigned int hwpwm;
79 unsigned int pwm;
80 struct pwm_chip *chip;
81 void *chip_data;
Thierry Redingf051c462011-12-14 11:12:23 +010082
Boris Brezillone39c0df2016-04-14 21:17:21 +020083 struct pwm_args args;
Boris Brezillon43a276b2016-04-14 21:17:38 +020084 struct pwm_state state;
Thierry Redingf051c462011-12-14 11:12:23 +010085};
86
Boris Brezillon43a276b2016-04-14 21:17:38 +020087/**
88 * pwm_get_state() - retrieve the current PWM state
89 * @pwm: PWM device
90 * @state: state to fill with the current PWM state
91 */
92static inline void pwm_get_state(const struct pwm_device *pwm,
93 struct pwm_state *state)
94{
95 *state = pwm->state;
96}
97
Boris Brezillon5c312522015-07-01 10:21:47 +020098static inline bool pwm_is_enabled(const struct pwm_device *pwm)
99{
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200100 struct pwm_state state;
101
102 pwm_get_state(pwm, &state);
103
104 return state.enabled;
Boris Brezillon5c312522015-07-01 10:21:47 +0200105}
106
Thierry Redingf051c462011-12-14 11:12:23 +0100107static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
108{
109 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200110 pwm->state.period = period;
Thierry Redingf051c462011-12-14 11:12:23 +0100111}
112
Boris Brezillona1cf4212015-07-01 10:21:48 +0200113static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100114{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200115 struct pwm_state state;
116
117 pwm_get_state(pwm, &state);
118
119 return state.period;
Thierry Redingf051c462011-12-14 11:12:23 +0100120}
121
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700122static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
123{
124 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200125 pwm->state.duty_cycle = duty;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700126}
127
Boris Brezillona1cf4212015-07-01 10:21:48 +0200128static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700129{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200130 struct pwm_state state;
131
132 pwm_get_state(pwm, &state);
133
134 return state.duty_cycle;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700135}
136
Boris Brezillon011e7632015-07-01 10:21:49 +0200137static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
138{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200139 struct pwm_state state;
140
141 pwm_get_state(pwm, &state);
142
143 return state.polarity;
Boris Brezillon011e7632015-07-01 10:21:49 +0200144}
145
Boris Brezillone39c0df2016-04-14 21:17:21 +0200146static inline void pwm_get_args(const struct pwm_device *pwm,
147 struct pwm_args *args)
148{
149 *args = pwm->args;
150}
151
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100152/**
Boris Brezillona6a0dbb2016-06-14 11:13:09 +0200153 * pwm_init_state() - prepare a new state to be applied with pwm_apply_state()
154 * @pwm: PWM device
155 * @state: state to fill with the prepared PWM state
156 *
157 * This functions prepares a state that can later be tweaked and applied
158 * to the PWM device with pwm_apply_state(). This is a convenient function
159 * that first retrieves the current PWM state and the replaces the period
160 * and polarity fields with the reference values defined in pwm->args.
161 * Once the function returns, you can adjust the ->enabled and ->duty_cycle
162 * fields according to your needs before calling pwm_apply_state().
163 *
164 * ->duty_cycle is initially set to zero to avoid cases where the current
165 * ->duty_cycle value exceed the pwm_args->period one, which would trigger
166 * an error if the user calls pwm_apply_state() without adjusting ->duty_cycle
167 * first.
168 */
169static inline void pwm_init_state(const struct pwm_device *pwm,
170 struct pwm_state *state)
171{
172 struct pwm_args args;
173
174 /* First get the current state. */
175 pwm_get_state(pwm, state);
176
177 /* Then fill it with the reference config */
178 pwm_get_args(pwm, &args);
179
180 state->period = args.period;
181 state->polarity = args.polarity;
182 state->duty_cycle = 0;
183}
184
185/**
Boris Brezillonf6f3bdd2016-06-14 11:13:10 +0200186 * pwm_get_relative_duty_cycle() - Get a relative duty cycle value
187 * @state: PWM state to extract the duty cycle from
188 * @scale: target scale of the relative duty cycle
189 *
190 * This functions converts the absolute duty cycle stored in @state (expressed
191 * in nanosecond) into a value relative to the period.
192 *
193 * For example if you want to get the duty_cycle expressed in percent, call:
194 *
195 * pwm_get_state(pwm, &state);
196 * duty = pwm_get_relative_duty_cycle(&state, 100);
197 */
198static inline unsigned int
199pwm_get_relative_duty_cycle(const struct pwm_state *state, unsigned int scale)
200{
201 if (!state->period)
202 return 0;
203
204 return DIV_ROUND_CLOSEST_ULL((u64)state->duty_cycle * scale,
205 state->period);
206}
207
208/**
209 * pwm_set_relative_duty_cycle() - Set a relative duty cycle value
210 * @state: PWM state to fill
211 * @duty_cycle: relative duty cycle value
212 * @scale: scale in which @duty_cycle is expressed
213 *
214 * This functions converts a relative into an absolute duty cycle (expressed
215 * in nanoseconds), and puts the result in state->duty_cycle.
216 *
217 * For example if you want to configure a 50% duty cycle, call:
218 *
219 * pwm_init_state(pwm, &state);
220 * pwm_set_relative_duty_cycle(&state, 50, 100);
221 * pwm_apply_state(pwm, &state);
222 *
223 * This functions returns -EINVAL if @duty_cycle and/or @scale are
224 * inconsistent (@scale == 0 or @duty_cycle > @scale).
225 */
226static inline int
227pwm_set_relative_duty_cycle(struct pwm_state *state, unsigned int duty_cycle,
228 unsigned int scale)
229{
230 if (!scale || duty_cycle > scale)
231 return -EINVAL;
232
233 state->duty_cycle = DIV_ROUND_CLOSEST_ULL((u64)duty_cycle *
234 state->period,
235 scale);
236
237 return 0;
238}
239
240/**
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100241 * struct pwm_ops - PWM controller operations
242 * @request: optional hook for requesting a PWM
243 * @free: optional hook for freeing a PWM
244 * @config: configure duty cycles and period length for this PWM
Philip, Avinash0aa0869c2012-07-24 19:35:32 +0530245 * @set_polarity: configure the polarity of this PWM
Lee Jones3a3d1a42016-06-08 10:21:23 +0100246 * @capture: capture and report PWM signal
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100247 * @enable: enable PWM output toggling
248 * @disable: disable PWM output toggling
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200249 * @apply: atomically apply a new PWM config. The state argument
250 * should be adjusted with the real hardware config (if the
251 * approximate the period or duty_cycle value, state should
252 * reflect it)
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200253 * @get_state: get the current PWM state. This function is only
254 * called once per PWM device when the PWM chip is
255 * registered.
Thierry Reding62099ab2012-03-26 09:31:48 +0200256 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100257 * @owner: helps prevent removal of modules exporting active PWMs
258 */
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);
262 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
263 int duty_ns, int period_ns);
264 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
265 enum pwm_polarity polarity);
Lee Jones3a3d1a42016-06-08 10:21:23 +0100266 int (*capture)(struct pwm_chip *chip, struct pwm_device *pwm,
267 struct pwm_capture *result, unsigned long timeout);
Thierry Reding6bc70642015-07-27 11:57:28 +0200268 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
269 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200270 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
271 struct pwm_state *state);
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200272 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
273 struct pwm_state *state);
Thierry Reding62099ab2012-03-26 09:31:48 +0200274#ifdef CONFIG_DEBUG_FS
Thierry Reding6bc70642015-07-27 11:57:28 +0200275 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
Thierry Reding62099ab2012-03-26 09:31:48 +0200276#endif
Thierry Reding6bc70642015-07-27 11:57:28 +0200277 struct module *owner;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100278};
279
280/**
Thierry Redingf051c462011-12-14 11:12:23 +0100281 * struct pwm_chip - abstract a PWM controller
282 * @dev: device providing the PWMs
283 * @list: list node for internal use
284 * @ops: callbacks for this PWM controller
285 * @base: number of first PWM controlled by this chip
286 * @npwm: number of PWMs controlled by this chip
287 * @pwms: array of PWM devices allocated by the framework
Thierry Reding04883802015-07-27 11:58:32 +0200288 * @of_xlate: request a PWM device given a device tree PWM specifier
289 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100290 */
291struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200292 struct device *dev;
293 struct list_head list;
294 const struct pwm_ops *ops;
295 int base;
296 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100297
Thierry Reding6bc70642015-07-27 11:57:28 +0200298 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100299
Thierry Reding6bc70642015-07-27 11:57:28 +0200300 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
301 const struct of_phandle_args *args);
302 unsigned int of_pwm_n_cells;
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/**
351 * pwm_set_polarity() - configure the polarity of a PWM signal
352 * @pwm: PWM device
353 * @polarity: new polarity of the PWM signal
354 *
355 * Note that the polarity cannot be configured while the PWM device is
356 * enabled.
357 *
358 * Returns: 0 on success or a negative error code on failure.
359 */
360static inline int pwm_set_polarity(struct pwm_device *pwm,
361 enum pwm_polarity polarity)
362{
363 struct pwm_state state;
364
365 if (!pwm)
366 return -EINVAL;
367
368 pwm_get_state(pwm, &state);
369 if (state.polarity == polarity)
370 return 0;
371
372 /*
373 * Changing the polarity of a running PWM without adjusting the
374 * dutycycle/period value is a bit risky (can introduce glitches).
375 * Return -EBUSY in this case.
376 * Note that this is allowed when using pwm_apply_state() because
377 * the user specifies all the parameters.
378 */
379 if (state.enabled)
380 return -EBUSY;
381
382 state.polarity = polarity;
383 return pwm_apply_state(pwm, &state);
384}
385
386/**
387 * pwm_enable() - start a PWM output toggling
388 * @pwm: PWM device
389 *
390 * Returns: 0 on success or a negative error code on failure.
391 */
392static inline int pwm_enable(struct pwm_device *pwm)
393{
394 struct pwm_state state;
395
396 if (!pwm)
397 return -EINVAL;
398
399 pwm_get_state(pwm, &state);
400 if (state.enabled)
401 return 0;
402
403 state.enabled = true;
404 return pwm_apply_state(pwm, &state);
405}
406
407/**
408 * pwm_disable() - stop a PWM output toggling
409 * @pwm: PWM device
410 */
411static inline void pwm_disable(struct pwm_device *pwm)
412{
413 struct pwm_state state;
414
415 if (!pwm)
416 return;
417
418 pwm_get_state(pwm, &state);
419 if (!state.enabled)
420 return;
421
422 state.enabled = false;
423 pwm_apply_state(pwm, &state);
424}
425
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200426/* PWM provider APIs */
Lee Jones3a3d1a42016-06-08 10:21:23 +0100427int pwm_capture(struct pwm_device *pwm, struct pwm_capture *result,
428 unsigned long timeout);
Thierry Redingf051c462011-12-14 11:12:23 +0100429int pwm_set_chip_data(struct pwm_device *pwm, void *data);
430void *pwm_get_chip_data(struct pwm_device *pwm);
431
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700432int pwmchip_add_with_polarity(struct pwm_chip *chip,
433 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100434int pwmchip_add(struct pwm_chip *chip);
435int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100436struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
437 unsigned int index,
438 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200439
Philip, Avinash83af2402012-11-21 13:10:44 +0530440struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
441 const struct of_phandle_args *args);
442
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800443struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800444struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200445void pwm_put(struct pwm_device *pwm);
446
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800447struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800448struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
449 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900450void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530451#else
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200452static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
453{
454 return ERR_PTR(-ENODEV);
455}
456
457static inline void pwm_free(struct pwm_device *pwm)
458{
459}
460
461static inline int pwm_apply_state(struct pwm_device *pwm,
462 const struct pwm_state *state)
463{
464 return -ENOTSUPP;
465}
466
467static inline int pwm_adjust_config(struct pwm_device *pwm)
468{
469 return -ENOTSUPP;
470}
471
472static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
473 int period_ns)
474{
475 return -EINVAL;
476}
477
Lee Jones3a3d1a42016-06-08 10:21:23 +0100478static inline int pwm_capture(struct pwm_device *pwm,
479 struct pwm_capture *result,
480 unsigned long timeout)
481{
482 return -EINVAL;
483}
484
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200485static inline int pwm_set_polarity(struct pwm_device *pwm,
486 enum pwm_polarity polarity)
487{
488 return -ENOTSUPP;
489}
490
491static inline int pwm_enable(struct pwm_device *pwm)
492{
493 return -EINVAL;
494}
495
496static inline void pwm_disable(struct pwm_device *pwm)
497{
498}
499
Tushar Behera0bcf1682012-09-12 15:31:46 +0530500static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
501{
502 return -EINVAL;
503}
504
505static inline void *pwm_get_chip_data(struct pwm_device *pwm)
506{
507 return NULL;
508}
509
510static inline int pwmchip_add(struct pwm_chip *chip)
511{
512 return -EINVAL;
513}
514
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700515static inline int pwmchip_add_inversed(struct pwm_chip *chip)
516{
517 return -EINVAL;
518}
519
Tushar Behera0bcf1682012-09-12 15:31:46 +0530520static inline int pwmchip_remove(struct pwm_chip *chip)
521{
522 return -EINVAL;
523}
524
525static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
526 unsigned int index,
527 const char *label)
528{
529 return ERR_PTR(-ENODEV);
530}
531
532static inline struct pwm_device *pwm_get(struct device *dev,
533 const char *consumer)
534{
535 return ERR_PTR(-ENODEV);
536}
537
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800538static inline struct pwm_device *of_pwm_get(struct device_node *np,
539 const char *con_id)
540{
541 return ERR_PTR(-ENODEV);
542}
543
Tushar Behera0bcf1682012-09-12 15:31:46 +0530544static inline void pwm_put(struct pwm_device *pwm)
545{
546}
547
548static inline struct pwm_device *devm_pwm_get(struct device *dev,
549 const char *consumer)
550{
551 return ERR_PTR(-ENODEV);
552}
553
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800554static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
555 struct device_node *np,
556 const char *con_id)
557{
558 return ERR_PTR(-ENODEV);
559}
560
Tushar Behera0bcf1682012-09-12 15:31:46 +0530561static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
562{
563}
564#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900565
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200566static inline void pwm_apply_args(struct pwm_device *pwm)
567{
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200568 struct pwm_state state = { };
569
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200570 /*
571 * PWM users calling pwm_apply_args() expect to have a fresh config
572 * where the polarity and period are set according to pwm_args info.
573 * The problem is, polarity can only be changed when the PWM is
574 * disabled.
575 *
576 * PWM drivers supporting hardware readout may declare the PWM device
577 * as enabled, and prevent polarity setting, which changes from the
578 * existing behavior, where all PWM devices are declared as disabled
579 * at startup (even if they are actually enabled), thus authorizing
580 * polarity setting.
581 *
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200582 * To fulfill this requirement, we apply a new state which disables
583 * the PWM device and set the reference period and polarity config.
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200584 *
585 * Note that PWM users requiring a smooth handover between the
586 * bootloader and the kernel (like critical regulators controlled by
587 * PWM devices) will have to switch to the atomic API and avoid calling
588 * pwm_apply_args().
589 */
Boris Brezillon33cdcee2016-06-22 09:25:14 +0200590
591 state.enabled = false;
592 state.polarity = pwm->args.polarity;
593 state.period = pwm->args.period;
594
595 pwm_apply_state(pwm, &state);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200596}
597
Thierry Reding8138d2d2012-03-26 08:42:48 +0200598struct pwm_lookup {
599 struct list_head list;
600 const char *provider;
601 unsigned int index;
602 const char *dev_id;
603 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200604 unsigned int period;
605 enum pwm_polarity polarity;
Hans de Goedeb526a312017-01-22 17:14:08 +0100606 const char *module; /* optional, may be NULL */
Thierry Reding8138d2d2012-03-26 08:42:48 +0200607};
608
Hans de Goedeb526a312017-01-22 17:14:08 +0100609#define PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, \
610 _period, _polarity, _module) \
611 { \
612 .provider = _provider, \
613 .index = _index, \
614 .dev_id = _dev_id, \
615 .con_id = _con_id, \
616 .period = _period, \
617 .polarity = _polarity, \
618 .module = _module, \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200619 }
620
Hans de Goedeb526a312017-01-22 17:14:08 +0100621#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
622 PWM_LOOKUP_WITH_MODULE(_provider, _index, _dev_id, _con_id, _period, \
623 _polarity, NULL)
624
Tushar Behera0bcf1682012-09-12 15:31:46 +0530625#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200626void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530627void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530628#else
629static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
630{
631}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530632
633static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
634{
635}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100636#endif
637
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700638#ifdef CONFIG_PWM_SYSFS
639void pwmchip_sysfs_export(struct pwm_chip *chip);
640void pwmchip_sysfs_unexport(struct pwm_chip *chip);
David Hsu07334242016-08-09 14:57:46 -0700641void pwmchip_sysfs_unexport_children(struct pwm_chip *chip);
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700642#else
643static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
644{
645}
646
647static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
648{
649}
David Hsu07334242016-08-09 14:57:46 -0700650
651static inline void pwmchip_sysfs_unexport_children(struct pwm_chip *chip)
652{
653}
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700654#endif /* CONFIG_PWM_SYSFS */
655
Mark Vels5243ef82009-01-18 18:42:45 +0100656#endif /* __LINUX_PWM_H */