blob: 33f8decd9f38e0e0ef681fa8d4d8681d8c7b3baa [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
Russell King1a189b92008-04-13 21:41:55 +01008struct pwm_device;
Thierry Reding62099ab2012-03-26 09:31:48 +02009struct seq_file;
Russell King1a189b92008-04-13 21:41:55 +010010
Sascha Hauer557fe992014-01-24 08:54:16 +010011#if IS_ENABLED(CONFIG_PWM)
Russell King1a189b92008-04-13 21:41:55 +010012/*
13 * pwm_request - request a PWM device
14 */
15struct pwm_device *pwm_request(int pwm_id, const char *label);
16
17/*
18 * pwm_free - free a PWM device
19 */
20void pwm_free(struct pwm_device *pwm);
21
22/*
23 * pwm_config - change a PWM device configuration
24 */
25int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
26
27/*
28 * pwm_enable - start a PWM output toggling
29 */
30int pwm_enable(struct pwm_device *pwm);
31
32/*
33 * pwm_disable - stop a PWM output toggling
34 */
35void pwm_disable(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +053036#else
37static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
38{
39 return ERR_PTR(-ENODEV);
40}
Russell King1a189b92008-04-13 21:41:55 +010041
Tushar Behera0bcf1682012-09-12 15:31:46 +053042static inline void pwm_free(struct pwm_device *pwm)
43{
44}
45
46static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
47{
48 return -EINVAL;
49}
50
51static inline int pwm_enable(struct pwm_device *pwm)
52{
53 return -EINVAL;
54}
55
56static inline void pwm_disable(struct pwm_device *pwm)
57{
58}
59#endif
60
Sascha Hauer0c2498f2011-01-28 09:40:40 +010061struct pwm_chip;
62
Philip, Avinash0aa08692012-07-24 19:35:32 +053063/**
64 * enum pwm_polarity - polarity of a PWM signal
65 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
66 * cycle, followed by a low signal for the remainder of the pulse
67 * period
68 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
69 * cycle, followed by a high signal for the remainder of the pulse
70 * period
71 */
72enum pwm_polarity {
73 PWM_POLARITY_NORMAL,
74 PWM_POLARITY_INVERSED,
75};
76
Boris Brezillone39c0df2016-04-14 21:17:21 +020077/**
78 * struct pwm_args - board-dependent PWM arguments
79 * @period: reference period
80 * @polarity: reference polarity
81 *
82 * This structure describes board-dependent arguments attached to a PWM
83 * device. These arguments are usually retrieved from the PWM lookup table or
84 * device tree.
85 *
86 * Do not confuse this with the PWM state: PWM arguments represent the initial
87 * configuration that users want to use on this PWM device rather than the
88 * current PWM hardware state.
89 */
90struct pwm_args {
91 unsigned int period;
92 enum pwm_polarity polarity;
93};
94
Thierry Redingf051c462011-12-14 11:12:23 +010095enum {
96 PWMF_REQUESTED = 1 << 0,
Boris Brezillon09a7e4a2016-04-14 21:17:39 +020097 PWMF_EXPORTED = 1 << 1,
Thierry Redingf051c462011-12-14 11:12:23 +010098};
99
Boris Brezillon43a276b2016-04-14 21:17:38 +0200100/*
101 * struct pwm_state - state of a PWM channel
102 * @period: PWM period (in nanoseconds)
103 * @duty_cycle: PWM duty cycle (in nanoseconds)
104 * @polarity: PWM polarity
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200105 * @enabled: PWM enabled status
Boris Brezillon43a276b2016-04-14 21:17:38 +0200106 */
107struct pwm_state {
108 unsigned int period;
109 unsigned int duty_cycle;
110 enum pwm_polarity polarity;
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200111 bool enabled;
Boris Brezillon43a276b2016-04-14 21:17:38 +0200112};
113
Thierry Reding04883802015-07-27 11:58:32 +0200114/**
115 * struct pwm_device - PWM channel object
116 * @label: name of the PWM device
117 * @flags: flags associated with the PWM device
118 * @hwpwm: per-chip relative index of the PWM device
119 * @pwm: global index of the PWM device
120 * @chip: PWM chip providing this PWM device
121 * @chip_data: chip-private data associated with the PWM device
Boris Brezillone39c0df2016-04-14 21:17:21 +0200122 * @args: PWM arguments
Boris Brezillon43a276b2016-04-14 21:17:38 +0200123 * @state: curent PWM channel state
Thierry Reding04883802015-07-27 11:58:32 +0200124 */
Thierry Redingf051c462011-12-14 11:12:23 +0100125struct pwm_device {
Thierry Reding6bc70642015-07-27 11:57:28 +0200126 const char *label;
127 unsigned long flags;
128 unsigned int hwpwm;
129 unsigned int pwm;
130 struct pwm_chip *chip;
131 void *chip_data;
Thierry Redingf051c462011-12-14 11:12:23 +0100132
Boris Brezillone39c0df2016-04-14 21:17:21 +0200133 struct pwm_args args;
Boris Brezillon43a276b2016-04-14 21:17:38 +0200134 struct pwm_state state;
Thierry Redingf051c462011-12-14 11:12:23 +0100135};
136
Boris Brezillon43a276b2016-04-14 21:17:38 +0200137/**
138 * pwm_get_state() - retrieve the current PWM state
139 * @pwm: PWM device
140 * @state: state to fill with the current PWM state
141 */
142static inline void pwm_get_state(const struct pwm_device *pwm,
143 struct pwm_state *state)
144{
145 *state = pwm->state;
146}
147
Boris Brezillon5c312522015-07-01 10:21:47 +0200148static inline bool pwm_is_enabled(const struct pwm_device *pwm)
149{
Boris Brezillon09a7e4a2016-04-14 21:17:39 +0200150 struct pwm_state state;
151
152 pwm_get_state(pwm, &state);
153
154 return state.enabled;
Boris Brezillon5c312522015-07-01 10:21:47 +0200155}
156
Thierry Redingf051c462011-12-14 11:12:23 +0100157static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
158{
159 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200160 pwm->state.period = period;
Thierry Redingf051c462011-12-14 11:12:23 +0100161}
162
Boris Brezillona1cf4212015-07-01 10:21:48 +0200163static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100164{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200165 struct pwm_state state;
166
167 pwm_get_state(pwm, &state);
168
169 return state.period;
Thierry Redingf051c462011-12-14 11:12:23 +0100170}
171
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700172static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
173{
174 if (pwm)
Boris Brezillon43a276b2016-04-14 21:17:38 +0200175 pwm->state.duty_cycle = duty;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700176}
177
Boris Brezillona1cf4212015-07-01 10:21:48 +0200178static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700179{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200180 struct pwm_state state;
181
182 pwm_get_state(pwm, &state);
183
184 return state.duty_cycle;
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700185}
186
Philip, Avinash0aa08692012-07-24 19:35:32 +0530187/*
188 * pwm_set_polarity - configure the polarity of a PWM signal
189 */
190int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
191
Boris Brezillon011e7632015-07-01 10:21:49 +0200192static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
193{
Boris Brezillon43a276b2016-04-14 21:17:38 +0200194 struct pwm_state state;
195
196 pwm_get_state(pwm, &state);
197
198 return state.polarity;
Boris Brezillon011e7632015-07-01 10:21:49 +0200199}
200
Boris Brezillone39c0df2016-04-14 21:17:21 +0200201static inline void pwm_get_args(const struct pwm_device *pwm,
202 struct pwm_args *args)
203{
204 *args = pwm->args;
205}
206
207static inline void pwm_apply_args(struct pwm_device *pwm)
208{
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200209 /*
210 * PWM users calling pwm_apply_args() expect to have a fresh config
211 * where the polarity and period are set according to pwm_args info.
212 * The problem is, polarity can only be changed when the PWM is
213 * disabled.
214 *
215 * PWM drivers supporting hardware readout may declare the PWM device
216 * as enabled, and prevent polarity setting, which changes from the
217 * existing behavior, where all PWM devices are declared as disabled
218 * at startup (even if they are actually enabled), thus authorizing
219 * polarity setting.
220 *
221 * Instead of setting ->enabled to false, we call pwm_disable()
222 * before pwm_set_polarity() to ensure that everything is configured
223 * as expected, and the PWM is really disabled when the user request
224 * it.
225 *
226 * Note that PWM users requiring a smooth handover between the
227 * bootloader and the kernel (like critical regulators controlled by
228 * PWM devices) will have to switch to the atomic API and avoid calling
229 * pwm_apply_args().
230 */
231 pwm_disable(pwm);
Boris Brezillone39c0df2016-04-14 21:17:21 +0200232 pwm_set_polarity(pwm, pwm->args.polarity);
233}
234
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100235/**
236 * struct pwm_ops - PWM controller operations
237 * @request: optional hook for requesting a PWM
238 * @free: optional hook for freeing a PWM
239 * @config: configure duty cycles and period length for this PWM
Philip, Avinash0aa08692012-07-24 19:35:32 +0530240 * @set_polarity: configure the polarity of this PWM
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100241 * @enable: enable PWM output toggling
242 * @disable: disable PWM output toggling
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200243 * @get_state: get the current PWM state. This function is only
244 * called once per PWM device when the PWM chip is
245 * registered.
Thierry Reding62099ab2012-03-26 09:31:48 +0200246 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100247 * @owner: helps prevent removal of modules exporting active PWMs
248 */
249struct pwm_ops {
Thierry Reding6bc70642015-07-27 11:57:28 +0200250 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
251 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
252 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
253 int duty_ns, int period_ns);
254 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
255 enum pwm_polarity polarity);
256 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
257 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Boris Brezillon15fa8a432016-04-14 21:17:40 +0200258 void (*get_state)(struct pwm_chip *chip, struct pwm_device *pwm,
259 struct pwm_state *state);
Thierry Reding62099ab2012-03-26 09:31:48 +0200260#ifdef CONFIG_DEBUG_FS
Thierry Reding6bc70642015-07-27 11:57:28 +0200261 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
Thierry Reding62099ab2012-03-26 09:31:48 +0200262#endif
Thierry Reding6bc70642015-07-27 11:57:28 +0200263 struct module *owner;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100264};
265
266/**
Thierry Redingf051c462011-12-14 11:12:23 +0100267 * struct pwm_chip - abstract a PWM controller
268 * @dev: device providing the PWMs
269 * @list: list node for internal use
270 * @ops: callbacks for this PWM controller
271 * @base: number of first PWM controlled by this chip
272 * @npwm: number of PWMs controlled by this chip
273 * @pwms: array of PWM devices allocated by the framework
Thierry Reding04883802015-07-27 11:58:32 +0200274 * @of_xlate: request a PWM device given a device tree PWM specifier
275 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100276 * @can_sleep: must be true if the .config(), .enable() or .disable()
277 * operations may sleep
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100278 */
279struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200280 struct device *dev;
281 struct list_head list;
282 const struct pwm_ops *ops;
283 int base;
284 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100285
Thierry Reding6bc70642015-07-27 11:57:28 +0200286 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100287
Thierry Reding6bc70642015-07-27 11:57:28 +0200288 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
289 const struct of_phandle_args *args);
290 unsigned int of_pwm_n_cells;
291 bool can_sleep;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100292};
293
Tushar Behera0bcf1682012-09-12 15:31:46 +0530294#if IS_ENABLED(CONFIG_PWM)
Thierry Redingf051c462011-12-14 11:12:23 +0100295int pwm_set_chip_data(struct pwm_device *pwm, void *data);
296void *pwm_get_chip_data(struct pwm_device *pwm);
297
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700298int pwmchip_add_with_polarity(struct pwm_chip *chip,
299 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100300int pwmchip_add(struct pwm_chip *chip);
301int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100302struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
303 unsigned int index,
304 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200305
Philip, Avinash83af2402012-11-21 13:10:44 +0530306struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
307 const struct of_phandle_args *args);
308
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800309struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800310struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200311void pwm_put(struct pwm_device *pwm);
312
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800313struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800314struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
315 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900316void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100317
318bool pwm_can_sleep(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530319#else
320static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
321{
322 return -EINVAL;
323}
324
325static inline void *pwm_get_chip_data(struct pwm_device *pwm)
326{
327 return NULL;
328}
329
330static inline int pwmchip_add(struct pwm_chip *chip)
331{
332 return -EINVAL;
333}
334
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700335static inline int pwmchip_add_inversed(struct pwm_chip *chip)
336{
337 return -EINVAL;
338}
339
Tushar Behera0bcf1682012-09-12 15:31:46 +0530340static inline int pwmchip_remove(struct pwm_chip *chip)
341{
342 return -EINVAL;
343}
344
345static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
346 unsigned int index,
347 const char *label)
348{
349 return ERR_PTR(-ENODEV);
350}
351
352static inline struct pwm_device *pwm_get(struct device *dev,
353 const char *consumer)
354{
355 return ERR_PTR(-ENODEV);
356}
357
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800358static inline struct pwm_device *of_pwm_get(struct device_node *np,
359 const char *con_id)
360{
361 return ERR_PTR(-ENODEV);
362}
363
Tushar Behera0bcf1682012-09-12 15:31:46 +0530364static inline void pwm_put(struct pwm_device *pwm)
365{
366}
367
368static inline struct pwm_device *devm_pwm_get(struct device *dev,
369 const char *consumer)
370{
371 return ERR_PTR(-ENODEV);
372}
373
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800374static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
375 struct device_node *np,
376 const char *con_id)
377{
378 return ERR_PTR(-ENODEV);
379}
380
Tushar Behera0bcf1682012-09-12 15:31:46 +0530381static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
382{
383}
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100384
385static inline bool pwm_can_sleep(struct pwm_device *pwm)
386{
387 return false;
388}
Tushar Behera0bcf1682012-09-12 15:31:46 +0530389#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900390
Thierry Reding8138d2d2012-03-26 08:42:48 +0200391struct pwm_lookup {
392 struct list_head list;
393 const char *provider;
394 unsigned int index;
395 const char *dev_id;
396 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200397 unsigned int period;
398 enum pwm_polarity polarity;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200399};
400
Alexandre Belloni42844022014-05-19 22:42:37 +0200401#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200402 { \
403 .provider = _provider, \
404 .index = _index, \
405 .dev_id = _dev_id, \
406 .con_id = _con_id, \
Alexandre Belloni42844022014-05-19 22:42:37 +0200407 .period = _period, \
408 .polarity = _polarity \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200409 }
410
Tushar Behera0bcf1682012-09-12 15:31:46 +0530411#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200412void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530413void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530414#else
415static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
416{
417}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530418
419static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
420{
421}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100422#endif
423
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700424#ifdef CONFIG_PWM_SYSFS
425void pwmchip_sysfs_export(struct pwm_chip *chip);
426void pwmchip_sysfs_unexport(struct pwm_chip *chip);
427#else
428static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
429{
430}
431
432static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
433{
434}
435#endif /* CONFIG_PWM_SYSFS */
436
Mark Vels5243ef82009-01-18 18:42:45 +0100437#endif /* __LINUX_PWM_H */