blob: 908b67c847cd656489f0a679422852e7c845d18f [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
Thierry Reding62099ab2012-03-26 09:31:48 +02008struct seq_file;
Sascha Hauer0c2498f2011-01-28 09:40:40 +01009struct pwm_chip;
10
Philip, Avinash0aa08692012-07-24 19:35:32 +053011/**
12 * enum pwm_polarity - polarity of a PWM signal
13 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
14 * cycle, followed by a low signal for the remainder of the pulse
15 * period
16 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
17 * cycle, followed by a high signal for the remainder of the pulse
18 * period
19 */
20enum pwm_polarity {
21 PWM_POLARITY_NORMAL,
22 PWM_POLARITY_INVERSED,
23};
24
Boris Brezillone39c0df2016-04-14 21:17:21 +020025/**
26 * struct pwm_args - board-dependent PWM arguments
27 * @period: reference period
28 * @polarity: reference polarity
29 *
30 * This structure describes board-dependent arguments attached to a PWM
31 * device. These arguments are usually retrieved from the PWM lookup table or
32 * device tree.
33 *
34 * Do not confuse this with the PWM state: PWM arguments represent the initial
35 * configuration that users want to use on this PWM device rather than the
36 * current PWM hardware state.
37 */
38struct pwm_args {
39 unsigned int period;
40 enum pwm_polarity polarity;
41};
42
Thierry Redingf051c462011-12-14 11:12:23 +010043enum {
44 PWMF_REQUESTED = 1 << 0,
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020045 PWMF_EXPORTED = 1 << 1,
Thierry Redingf051c462011-12-14 11:12:23 +010046};
47
Boris Brezillon43a276b2016-04-14 21:17:38 +020048/*
49 * struct pwm_state - state of a PWM channel
50 * @period: PWM period (in nanoseconds)
51 * @duty_cycle: PWM duty cycle (in nanoseconds)
52 * @polarity: PWM polarity
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020053 * @enabled: PWM enabled status
Boris Brezillon43a276b2016-04-14 21:17:38 +020054 */
55struct pwm_state {
56 unsigned int period;
57 unsigned int duty_cycle;
58 enum pwm_polarity polarity;
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020059 bool enabled;
Boris Brezillon43a276b2016-04-14 21:17:38 +020060};
61
Thierry Reding04883802015-07-27 11:58:32 +020062/**
63 * struct pwm_device - PWM channel object
64 * @label: name of the PWM device
65 * @flags: flags associated with the PWM device
66 * @hwpwm: per-chip relative index of the PWM device
67 * @pwm: global index of the PWM device
68 * @chip: PWM chip providing this PWM device
69 * @chip_data: chip-private data associated with the PWM device
Boris Brezillone39c0df2016-04-14 21:17:21 +020070 * @args: PWM arguments
Boris Brezillon43a276b2016-04-14 21:17:38 +020071 * @state: curent PWM channel state
Thierry Reding04883802015-07-27 11:58:32 +020072 */
Thierry Redingf051c462011-12-14 11:12:23 +010073struct pwm_device {
Thierry Reding6bc70642015-07-27 11:57:28 +020074 const char *label;
75 unsigned long flags;
76 unsigned int hwpwm;
77 unsigned int pwm;
78 struct pwm_chip *chip;
79 void *chip_data;
Thierry Redingf051c462011-12-14 11:12:23 +010080
Boris Brezillone39c0df2016-04-14 21:17:21 +020081 struct pwm_args args;
Boris Brezillon43a276b2016-04-14 21:17:38 +020082 struct pwm_state state;
Thierry Redingf051c462011-12-14 11:12:23 +010083};
84
Boris Brezillon43a276b2016-04-14 21:17:38 +020085/**
86 * pwm_get_state() - retrieve the current PWM state
87 * @pwm: PWM device
88 * @state: state to fill with the current PWM state
89 */
90static inline void pwm_get_state(const struct pwm_device *pwm,
91 struct pwm_state *state)
92{
93 *state = pwm->state;
94}
95
Boris Brezillon5c312522015-07-01 10:21:47 +020096static inline bool pwm_is_enabled(const struct pwm_device *pwm)
97{
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020098 struct pwm_state state;
99
100 pwm_get_state(pwm, &state);
101
102 return state.enabled;
Boris Brezillon5c312522015-07-01 10:21:47 +0200103}
104
Thierry Redingf051c462011-12-14 11:12:23 +0100105static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
106{
107 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200108 pwm->state.period = period;
Thierry Redingf051c462011-12-14 11:12:23 +0100109}
110
Boris Brezillona1cf4212015-07-01 10:21:48 +0200111static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100112{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200113 struct pwm_state state;
114
115 pwm_get_state(pwm, &state);
116
117 return state.period;
Thierry Redingf051c462011-12-14 11:12:23 +0100118}
119
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700120static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
121{
122 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200123 pwm->state.duty_cycle = duty;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700124}
125
Boris Brezillona1cf4212015-07-01 10:21:48 +0200126static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700127{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200128 struct pwm_state state;
129
130 pwm_get_state(pwm, &state);
131
132 return state.duty_cycle;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700133}
134
Boris Brezillon011e7632015-07-01 10:21:49 +0200135static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
136{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200137 struct pwm_state state;
138
139 pwm_get_state(pwm, &state);
140
141 return state.polarity;
Boris Brezillon011e7632015-07-01 10:21:49 +0200142}
143
Boris Brezillone39c0df2016-04-14 21:17:21 +0200144static inline void pwm_get_args(const struct pwm_device *pwm,
145 struct pwm_args *args)
146{
147 *args = pwm->args;
148}
149
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100150/**
151 * struct pwm_ops - PWM controller operations
152 * @request: optional hook for requesting a PWM
153 * @free: optional hook for freeing a PWM
154 * @config: configure duty cycles and period length for this PWM
Philip, Avinash0aa08692012-07-24 19:35:32 +0530155 * @set_polarity: configure the polarity of this PWM
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100156 * @enable: enable PWM output toggling
157 * @disable: disable PWM output toggling
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200158 * @apply: atomically apply a new PWM config. The state argument
159 * should be adjusted with the real hardware config (if the
160 * approximate the period or duty_cycle value, state should
161 * reflect it)
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200162 * @get_state: get the current PWM state. This function is only
163 * called once per PWM device when the PWM chip is
164 * registered.
Thierry Reding62099ab2012-03-26 09:31:48 +0200165 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100166 * @owner: helps prevent removal of modules exporting active PWMs
167 */
168struct pwm_ops {
Thierry Reding6bc70642015-07-27 11:57:28 +0200169 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
170 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
171 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
172 int duty_ns, int period_ns);
173 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
174 enum pwm_polarity polarity);
175 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
176 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200177 int (*apply)(struct pwm_chip *chip, struct pwm_device *pwm,
178 struct pwm_state *state);
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200179 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
180 struct pwm_state *state);
Thierry Reding62099ab2012-03-26 09:31:48 +0200181#ifdef CONFIG_DEBUG_FS
Thierry Reding6bc70642015-07-27 11:57:28 +0200182 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
Thierry Reding62099ab2012-03-26 09:31:48 +0200183#endif
Thierry Reding6bc70642015-07-27 11:57:28 +0200184 struct module *owner;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100185};
186
187/**
Thierry Redingf051c462011-12-14 11:12:23 +0100188 * struct pwm_chip - abstract a PWM controller
189 * @dev: device providing the PWMs
190 * @list: list node for internal use
191 * @ops: callbacks for this PWM controller
192 * @base: number of first PWM controlled by this chip
193 * @npwm: number of PWMs controlled by this chip
194 * @pwms: array of PWM devices allocated by the framework
Thierry Reding04883802015-07-27 11:58:32 +0200195 * @of_xlate: request a PWM device given a device tree PWM specifier
196 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100197 * @can_sleep: must be true if the .config(), .enable() or .disable()
198 * operations may sleep
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100199 */
200struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200201 struct device *dev;
202 struct list_head list;
203 const struct pwm_ops *ops;
204 int base;
205 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100206
Thierry Reding6bc70642015-07-27 11:57:28 +0200207 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100208
Thierry Reding6bc70642015-07-27 11:57:28 +0200209 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
210 const struct of_phandle_args *args);
211 unsigned int of_pwm_n_cells;
212 bool can_sleep;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100213};
214
Tushar Behera0bcf1682012-09-12 15:31:46 +0530215#if IS_ENABLED(CONFIG_PWM)
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200216/* PWM user APIs */
217struct pwm_device *pwm_request(int pwm_id, const char *label);
218void pwm_free(struct pwm_device *pwm);
219int pwm_apply_state(struct pwm_device *pwm, struct pwm_state *state);
220int pwm_adjust_config(struct pwm_device *pwm);
221
222/**
223 * pwm_config() - change a PWM device configuration
224 * @pwm: PWM device
225 * @duty_ns: "on" time (in nanoseconds)
226 * @period_ns: duration (in nanoseconds) of one cycle
227 *
228 * Returns: 0 on success or a negative error code on failure.
229 */
230static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
231 int period_ns)
232{
233 struct pwm_state state;
234
235 if (!pwm)
236 return -EINVAL;
237
Brian Norrisef2bf492016-05-27 09:45:49 -0700238 if (duty_ns < 0 || period_ns < 0)
239 return -EINVAL;
240
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200241 pwm_get_state(pwm, &state);
242 if (state.duty_cycle == duty_ns && state.period == period_ns)
243 return 0;
244
245 state.duty_cycle = duty_ns;
246 state.period = period_ns;
247 return pwm_apply_state(pwm, &state);
248}
249
250/**
251 * pwm_set_polarity() - configure the polarity of a PWM signal
252 * @pwm: PWM device
253 * @polarity: new polarity of the PWM signal
254 *
255 * Note that the polarity cannot be configured while the PWM device is
256 * enabled.
257 *
258 * Returns: 0 on success or a negative error code on failure.
259 */
260static inline int pwm_set_polarity(struct pwm_device *pwm,
261 enum pwm_polarity polarity)
262{
263 struct pwm_state state;
264
265 if (!pwm)
266 return -EINVAL;
267
268 pwm_get_state(pwm, &state);
269 if (state.polarity == polarity)
270 return 0;
271
272 /*
273 * Changing the polarity of a running PWM without adjusting the
274 * dutycycle/period value is a bit risky (can introduce glitches).
275 * Return -EBUSY in this case.
276 * Note that this is allowed when using pwm_apply_state() because
277 * the user specifies all the parameters.
278 */
279 if (state.enabled)
280 return -EBUSY;
281
282 state.polarity = polarity;
283 return pwm_apply_state(pwm, &state);
284}
285
286/**
287 * pwm_enable() - start a PWM output toggling
288 * @pwm: PWM device
289 *
290 * Returns: 0 on success or a negative error code on failure.
291 */
292static inline int pwm_enable(struct pwm_device *pwm)
293{
294 struct pwm_state state;
295
296 if (!pwm)
297 return -EINVAL;
298
299 pwm_get_state(pwm, &state);
300 if (state.enabled)
301 return 0;
302
303 state.enabled = true;
304 return pwm_apply_state(pwm, &state);
305}
306
307/**
308 * pwm_disable() - stop a PWM output toggling
309 * @pwm: PWM device
310 */
311static inline void pwm_disable(struct pwm_device *pwm)
312{
313 struct pwm_state state;
314
315 if (!pwm)
316 return;
317
318 pwm_get_state(pwm, &state);
319 if (!state.enabled)
320 return;
321
322 state.enabled = false;
323 pwm_apply_state(pwm, &state);
324}
325
326
327/* PWM provider APIs */
Thierry Redingf051c462011-12-14 11:12:23 +0100328int pwm_set_chip_data(struct pwm_device *pwm, void *data);
329void *pwm_get_chip_data(struct pwm_device *pwm);
330
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700331int pwmchip_add_with_polarity(struct pwm_chip *chip,
332 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100333int pwmchip_add(struct pwm_chip *chip);
334int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100335struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
336 unsigned int index,
337 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200338
Philip, Avinash83af2402012-11-21 13:10:44 +0530339struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
340 const struct of_phandle_args *args);
341
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800342struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800343struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200344void pwm_put(struct pwm_device *pwm);
345
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800346struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800347struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
348 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900349void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100350
351bool pwm_can_sleep(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530352#else
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200353static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
354{
355 return ERR_PTR(-ENODEV);
356}
357
358static inline void pwm_free(struct pwm_device *pwm)
359{
360}
361
362static inline int pwm_apply_state(struct pwm_device *pwm,
363 const struct pwm_state *state)
364{
365 return -ENOTSUPP;
366}
367
368static inline int pwm_adjust_config(struct pwm_device *pwm)
369{
370 return -ENOTSUPP;
371}
372
373static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
374 int period_ns)
375{
376 return -EINVAL;
377}
378
379static inline int pwm_set_polarity(struct pwm_device *pwm,
380 enum pwm_polarity polarity)
381{
382 return -ENOTSUPP;
383}
384
385static inline int pwm_enable(struct pwm_device *pwm)
386{
387 return -EINVAL;
388}
389
390static inline void pwm_disable(struct pwm_device *pwm)
391{
392}
393
Tushar Behera0bcf1682012-09-12 15:31:46 +0530394static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
395{
396 return -EINVAL;
397}
398
399static inline void *pwm_get_chip_data(struct pwm_device *pwm)
400{
401 return NULL;
402}
403
404static inline int pwmchip_add(struct pwm_chip *chip)
405{
406 return -EINVAL;
407}
408
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700409static inline int pwmchip_add_inversed(struct pwm_chip *chip)
410{
411 return -EINVAL;
412}
413
Tushar Behera0bcf1682012-09-12 15:31:46 +0530414static inline int pwmchip_remove(struct pwm_chip *chip)
415{
416 return -EINVAL;
417}
418
419static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
420 unsigned int index,
421 const char *label)
422{
423 return ERR_PTR(-ENODEV);
424}
425
426static inline struct pwm_device *pwm_get(struct device *dev,
427 const char *consumer)
428{
429 return ERR_PTR(-ENODEV);
430}
431
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800432static inline struct pwm_device *of_pwm_get(struct device_node *np,
433 const char *con_id)
434{
435 return ERR_PTR(-ENODEV);
436}
437
Tushar Behera0bcf1682012-09-12 15:31:46 +0530438static inline void pwm_put(struct pwm_device *pwm)
439{
440}
441
442static inline struct pwm_device *devm_pwm_get(struct device *dev,
443 const char *consumer)
444{
445 return ERR_PTR(-ENODEV);
446}
447
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800448static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
449 struct device_node *np,
450 const char *con_id)
451{
452 return ERR_PTR(-ENODEV);
453}
454
Tushar Behera0bcf1682012-09-12 15:31:46 +0530455static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
456{
457}
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100458
459static inline bool pwm_can_sleep(struct pwm_device *pwm)
460{
461 return false;
462}
Tushar Behera0bcf1682012-09-12 15:31:46 +0530463#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900464
Boris Brezillon5ec803e2016-04-14 21:17:41 +0200465static inline void pwm_apply_args(struct pwm_device *pwm)
466{
467 /*
468 * PWM users calling pwm_apply_args() expect to have a fresh config
469 * where the polarity and period are set according to pwm_args info.
470 * The problem is, polarity can only be changed when the PWM is
471 * disabled.
472 *
473 * PWM drivers supporting hardware readout may declare the PWM device
474 * as enabled, and prevent polarity setting, which changes from the
475 * existing behavior, where all PWM devices are declared as disabled
476 * at startup (even if they are actually enabled), thus authorizing
477 * polarity setting.
478 *
479 * Instead of setting ->enabled to false, we call pwm_disable()
480 * before pwm_set_polarity() to ensure that everything is configured
481 * as expected, and the PWM is really disabled when the user request
482 * it.
483 *
484 * Note that PWM users requiring a smooth handover between the
485 * bootloader and the kernel (like critical regulators controlled by
486 * PWM devices) will have to switch to the atomic API and avoid calling
487 * pwm_apply_args().
488 */
489 pwm_disable(pwm);
490 pwm_set_polarity(pwm, pwm->args.polarity);
491}
492
Thierry Reding8138d2d2012-03-26 08:42:48 +0200493struct pwm_lookup {
494 struct list_head list;
495 const char *provider;
496 unsigned int index;
497 const char *dev_id;
498 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200499 unsigned int period;
500 enum pwm_polarity polarity;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200501};
502
Alexandre Belloni42844022014-05-19 22:42:37 +0200503#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200504 { \
505 .provider = _provider, \
506 .index = _index, \
507 .dev_id = _dev_id, \
508 .con_id = _con_id, \
Alexandre Belloni42844022014-05-19 22:42:37 +0200509 .period = _period, \
510 .polarity = _polarity \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200511 }
512
Tushar Behera0bcf1682012-09-12 15:31:46 +0530513#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200514void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530515void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530516#else
517static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
518{
519}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530520
521static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
522{
523}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100524#endif
525
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700526#ifdef CONFIG_PWM_SYSFS
527void pwmchip_sysfs_export(struct pwm_chip *chip);
528void pwmchip_sysfs_unexport(struct pwm_chip *chip);
529#else
530static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
531{
532}
533
534static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
535{
536}
537#endif /* CONFIG_PWM_SYSFS */
538
Mark Vels5243ef82009-01-18 18:42:45 +0100539#endif /* __LINUX_PWM_H */