blob: f0feafd184a0d655fff326bc979eebd21a7a28ab [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>
Thierry Reding7299ab72011-12-14 11:10:32 +01005#include <linux/of.h>
6
Russell King1a189b92008-04-13 21:41:55 +01007struct pwm_device;
Thierry Reding62099ab2012-03-26 09:31:48 +02008struct seq_file;
Russell King1a189b92008-04-13 21:41:55 +01009
Tushar Behera0bcf1682012-09-12 15:31:46 +053010#if IS_ENABLED(CONFIG_PWM) || IS_ENABLED(CONFIG_HAVE_PWM)
Russell King1a189b92008-04-13 21:41:55 +010011/*
12 * pwm_request - request a PWM device
13 */
14struct pwm_device *pwm_request(int pwm_id, const char *label);
15
16/*
17 * pwm_free - free a PWM device
18 */
19void pwm_free(struct pwm_device *pwm);
20
21/*
22 * pwm_config - change a PWM device configuration
23 */
24int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
25
26/*
27 * pwm_enable - start a PWM output toggling
28 */
29int pwm_enable(struct pwm_device *pwm);
30
31/*
32 * pwm_disable - stop a PWM output toggling
33 */
34void pwm_disable(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +053035#else
36static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
37{
38 return ERR_PTR(-ENODEV);
39}
Russell King1a189b92008-04-13 21:41:55 +010040
Tushar Behera0bcf1682012-09-12 15:31:46 +053041static inline void pwm_free(struct pwm_device *pwm)
42{
43}
44
45static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
46{
47 return -EINVAL;
48}
49
50static inline int pwm_enable(struct pwm_device *pwm)
51{
52 return -EINVAL;
53}
54
55static inline void pwm_disable(struct pwm_device *pwm)
56{
57}
58#endif
59
Sascha Hauer0c2498f2011-01-28 09:40:40 +010060struct pwm_chip;
61
Philip, Avinash0aa08692012-07-24 19:35:32 +053062/**
63 * enum pwm_polarity - polarity of a PWM signal
64 * @PWM_POLARITY_NORMAL: a high signal for the duration of the duty-
65 * cycle, followed by a low signal for the remainder of the pulse
66 * period
67 * @PWM_POLARITY_INVERSED: a low signal for the duration of the duty-
68 * cycle, followed by a high signal for the remainder of the pulse
69 * period
70 */
71enum pwm_polarity {
72 PWM_POLARITY_NORMAL,
73 PWM_POLARITY_INVERSED,
74};
75
Thierry Redingf051c462011-12-14 11:12:23 +010076enum {
77 PWMF_REQUESTED = 1 << 0,
78 PWMF_ENABLED = 1 << 1,
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -070079 PWMF_EXPORTED = 1 << 2,
Thierry Redingf051c462011-12-14 11:12:23 +010080};
81
82struct pwm_device {
83 const char *label;
84 unsigned long flags;
85 unsigned int hwpwm;
86 unsigned int pwm;
87 struct pwm_chip *chip;
88 void *chip_data;
89
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -070090 unsigned int period; /* in nanoseconds */
91 unsigned int duty_cycle; /* in nanoseconds */
92 enum pwm_polarity polarity;
Thierry Redingf051c462011-12-14 11:12:23 +010093};
94
95static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
96{
97 if (pwm)
98 pwm->period = period;
99}
100
101static inline unsigned int pwm_get_period(struct pwm_device *pwm)
102{
103 return pwm ? pwm->period : 0;
104}
105
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700106static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
107{
108 if (pwm)
109 pwm->duty_cycle = duty;
110}
111
112static inline unsigned int pwm_get_duty_cycle(struct pwm_device *pwm)
113{
114 return pwm ? pwm->duty_cycle : 0;
115}
116
Philip, Avinash0aa08692012-07-24 19:35:32 +0530117/*
118 * pwm_set_polarity - configure the polarity of a PWM signal
119 */
120int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
121
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100122/**
123 * struct pwm_ops - PWM controller operations
124 * @request: optional hook for requesting a PWM
125 * @free: optional hook for freeing a PWM
126 * @config: configure duty cycles and period length for this PWM
Philip, Avinash0aa08692012-07-24 19:35:32 +0530127 * @set_polarity: configure the polarity of this PWM
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100128 * @enable: enable PWM output toggling
129 * @disable: disable PWM output toggling
Thierry Reding62099ab2012-03-26 09:31:48 +0200130 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100131 * @owner: helps prevent removal of modules exporting active PWMs
132 */
133struct pwm_ops {
Thierry Redingf051c462011-12-14 11:12:23 +0100134 int (*request)(struct pwm_chip *chip,
135 struct pwm_device *pwm);
136 void (*free)(struct pwm_chip *chip,
137 struct pwm_device *pwm);
138 int (*config)(struct pwm_chip *chip,
139 struct pwm_device *pwm,
140 int duty_ns, int period_ns);
Philip, Avinash0aa08692012-07-24 19:35:32 +0530141 int (*set_polarity)(struct pwm_chip *chip,
142 struct pwm_device *pwm,
143 enum pwm_polarity polarity);
Thierry Redingf051c462011-12-14 11:12:23 +0100144 int (*enable)(struct pwm_chip *chip,
145 struct pwm_device *pwm);
146 void (*disable)(struct pwm_chip *chip,
147 struct pwm_device *pwm);
Thierry Reding62099ab2012-03-26 09:31:48 +0200148#ifdef CONFIG_DEBUG_FS
149 void (*dbg_show)(struct pwm_chip *chip,
150 struct seq_file *s);
151#endif
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100152 struct module *owner;
153};
154
155/**
Thierry Redingf051c462011-12-14 11:12:23 +0100156 * struct pwm_chip - abstract a PWM controller
157 * @dev: device providing the PWMs
158 * @list: list node for internal use
159 * @ops: callbacks for this PWM controller
160 * @base: number of first PWM controlled by this chip
161 * @npwm: number of PWMs controlled by this chip
162 * @pwms: array of PWM devices allocated by the framework
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100163 * @can_sleep: must be true if the .config(), .enable() or .disable()
164 * operations may sleep
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100165 */
166struct pwm_chip {
Thierry Redingf051c462011-12-14 11:12:23 +0100167 struct device *dev;
168 struct list_head list;
169 const struct pwm_ops *ops;
170 int base;
171 unsigned int npwm;
172
173 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100174
175 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
176 const struct of_phandle_args *args);
177 unsigned int of_pwm_n_cells;
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100178 bool can_sleep;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100179};
180
Tushar Behera0bcf1682012-09-12 15:31:46 +0530181#if IS_ENABLED(CONFIG_PWM)
Thierry Redingf051c462011-12-14 11:12:23 +0100182int pwm_set_chip_data(struct pwm_device *pwm, void *data);
183void *pwm_get_chip_data(struct pwm_device *pwm);
184
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100185int pwmchip_add(struct pwm_chip *chip);
186int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100187struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
188 unsigned int index,
189 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200190
Philip, Avinash83af2402012-11-21 13:10:44 +0530191struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
192 const struct of_phandle_args *args);
193
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800194struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800195struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200196void pwm_put(struct pwm_device *pwm);
197
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800198struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800199struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
200 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900201void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100202
203bool pwm_can_sleep(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530204#else
205static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
206{
207 return -EINVAL;
208}
209
210static inline void *pwm_get_chip_data(struct pwm_device *pwm)
211{
212 return NULL;
213}
214
215static inline int pwmchip_add(struct pwm_chip *chip)
216{
217 return -EINVAL;
218}
219
220static inline int pwmchip_remove(struct pwm_chip *chip)
221{
222 return -EINVAL;
223}
224
225static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
226 unsigned int index,
227 const char *label)
228{
229 return ERR_PTR(-ENODEV);
230}
231
232static inline struct pwm_device *pwm_get(struct device *dev,
233 const char *consumer)
234{
235 return ERR_PTR(-ENODEV);
236}
237
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800238static inline struct pwm_device *of_pwm_get(struct device_node *np,
239 const char *con_id)
240{
241 return ERR_PTR(-ENODEV);
242}
243
Tushar Behera0bcf1682012-09-12 15:31:46 +0530244static inline void pwm_put(struct pwm_device *pwm)
245{
246}
247
248static inline struct pwm_device *devm_pwm_get(struct device *dev,
249 const char *consumer)
250{
251 return ERR_PTR(-ENODEV);
252}
253
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800254static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
255 struct device_node *np,
256 const char *con_id)
257{
258 return ERR_PTR(-ENODEV);
259}
260
Tushar Behera0bcf1682012-09-12 15:31:46 +0530261static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
262{
263}
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100264
265static inline bool pwm_can_sleep(struct pwm_device *pwm)
266{
267 return false;
268}
Tushar Behera0bcf1682012-09-12 15:31:46 +0530269#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900270
Thierry Reding8138d2d2012-03-26 08:42:48 +0200271struct pwm_lookup {
272 struct list_head list;
273 const char *provider;
274 unsigned int index;
275 const char *dev_id;
276 const char *con_id;
277};
278
279#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id) \
280 { \
281 .provider = _provider, \
282 .index = _index, \
283 .dev_id = _dev_id, \
284 .con_id = _con_id, \
285 }
286
Tushar Behera0bcf1682012-09-12 15:31:46 +0530287#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200288void pwm_add_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530289#else
290static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
291{
292}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100293#endif
294
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700295#ifdef CONFIG_PWM_SYSFS
296void pwmchip_sysfs_export(struct pwm_chip *chip);
297void pwmchip_sysfs_unexport(struct pwm_chip *chip);
298#else
299static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
300{
301}
302
303static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
304{
305}
306#endif /* CONFIG_PWM_SYSFS */
307
Mark Vels5243ef82009-01-18 18:42:45 +0100308#endif /* __LINUX_PWM_H */