blob: a4df2042b79c15f75bf53dd8d185fd6c80426b05 [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,
79};
80
81struct pwm_device {
82 const char *label;
83 unsigned long flags;
84 unsigned int hwpwm;
85 unsigned int pwm;
86 struct pwm_chip *chip;
87 void *chip_data;
88
89 unsigned int period; /* in nanoseconds */
90};
91
92static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
93{
94 if (pwm)
95 pwm->period = period;
96}
97
98static inline unsigned int pwm_get_period(struct pwm_device *pwm)
99{
100 return pwm ? pwm->period : 0;
101}
102
Philip, Avinash0aa08692012-07-24 19:35:32 +0530103/*
104 * pwm_set_polarity - configure the polarity of a PWM signal
105 */
106int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
107
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100108/**
109 * struct pwm_ops - PWM controller operations
110 * @request: optional hook for requesting a PWM
111 * @free: optional hook for freeing a PWM
112 * @config: configure duty cycles and period length for this PWM
Philip, Avinash0aa08692012-07-24 19:35:32 +0530113 * @set_polarity: configure the polarity of this PWM
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100114 * @enable: enable PWM output toggling
115 * @disable: disable PWM output toggling
Thierry Reding62099ab2012-03-26 09:31:48 +0200116 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100117 * @owner: helps prevent removal of modules exporting active PWMs
118 */
119struct pwm_ops {
Thierry Redingf051c462011-12-14 11:12:23 +0100120 int (*request)(struct pwm_chip *chip,
121 struct pwm_device *pwm);
122 void (*free)(struct pwm_chip *chip,
123 struct pwm_device *pwm);
124 int (*config)(struct pwm_chip *chip,
125 struct pwm_device *pwm,
126 int duty_ns, int period_ns);
Philip, Avinash0aa08692012-07-24 19:35:32 +0530127 int (*set_polarity)(struct pwm_chip *chip,
128 struct pwm_device *pwm,
129 enum pwm_polarity polarity);
Thierry Redingf051c462011-12-14 11:12:23 +0100130 int (*enable)(struct pwm_chip *chip,
131 struct pwm_device *pwm);
132 void (*disable)(struct pwm_chip *chip,
133 struct pwm_device *pwm);
Thierry Reding62099ab2012-03-26 09:31:48 +0200134#ifdef CONFIG_DEBUG_FS
135 void (*dbg_show)(struct pwm_chip *chip,
136 struct seq_file *s);
137#endif
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100138 struct module *owner;
139};
140
141/**
Thierry Redingf051c462011-12-14 11:12:23 +0100142 * struct pwm_chip - abstract a PWM controller
143 * @dev: device providing the PWMs
144 * @list: list node for internal use
145 * @ops: callbacks for this PWM controller
146 * @base: number of first PWM controlled by this chip
147 * @npwm: number of PWMs controlled by this chip
148 * @pwms: array of PWM devices allocated by the framework
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100149 * @can_sleep: must be true if the .config(), .enable() or .disable()
150 * operations may sleep
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100151 */
152struct pwm_chip {
Thierry Redingf051c462011-12-14 11:12:23 +0100153 struct device *dev;
154 struct list_head list;
155 const struct pwm_ops *ops;
156 int base;
157 unsigned int npwm;
158
159 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100160
161 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
162 const struct of_phandle_args *args);
163 unsigned int of_pwm_n_cells;
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100164 bool can_sleep;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100165};
166
Tushar Behera0bcf1682012-09-12 15:31:46 +0530167#if IS_ENABLED(CONFIG_PWM)
Thierry Redingf051c462011-12-14 11:12:23 +0100168int pwm_set_chip_data(struct pwm_device *pwm, void *data);
169void *pwm_get_chip_data(struct pwm_device *pwm);
170
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100171int pwmchip_add(struct pwm_chip *chip);
172int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100173struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
174 unsigned int index,
175 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200176
Philip, Avinash83af2402012-11-21 13:10:44 +0530177struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
178 const struct of_phandle_args *args);
179
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800180struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800181struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200182void pwm_put(struct pwm_device *pwm);
183
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800184struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800185struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
186 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900187void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100188
189bool pwm_can_sleep(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530190#else
191static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
192{
193 return -EINVAL;
194}
195
196static inline void *pwm_get_chip_data(struct pwm_device *pwm)
197{
198 return NULL;
199}
200
201static inline int pwmchip_add(struct pwm_chip *chip)
202{
203 return -EINVAL;
204}
205
206static inline int pwmchip_remove(struct pwm_chip *chip)
207{
208 return -EINVAL;
209}
210
211static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
212 unsigned int index,
213 const char *label)
214{
215 return ERR_PTR(-ENODEV);
216}
217
218static inline struct pwm_device *pwm_get(struct device *dev,
219 const char *consumer)
220{
221 return ERR_PTR(-ENODEV);
222}
223
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800224static inline struct pwm_device *of_pwm_get(struct device_node *np,
225 const char *con_id)
226{
227 return ERR_PTR(-ENODEV);
228}
229
Tushar Behera0bcf1682012-09-12 15:31:46 +0530230static inline void pwm_put(struct pwm_device *pwm)
231{
232}
233
234static inline struct pwm_device *devm_pwm_get(struct device *dev,
235 const char *consumer)
236{
237 return ERR_PTR(-ENODEV);
238}
239
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800240static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
241 struct device_node *np,
242 const char *con_id)
243{
244 return ERR_PTR(-ENODEV);
245}
246
Tushar Behera0bcf1682012-09-12 15:31:46 +0530247static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
248{
249}
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100250
251static inline bool pwm_can_sleep(struct pwm_device *pwm)
252{
253 return false;
254}
Tushar Behera0bcf1682012-09-12 15:31:46 +0530255#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900256
Thierry Reding8138d2d2012-03-26 08:42:48 +0200257struct pwm_lookup {
258 struct list_head list;
259 const char *provider;
260 unsigned int index;
261 const char *dev_id;
262 const char *con_id;
263};
264
265#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id) \
266 { \
267 .provider = _provider, \
268 .index = _index, \
269 .dev_id = _dev_id, \
270 .con_id = _con_id, \
271 }
272
Tushar Behera0bcf1682012-09-12 15:31:46 +0530273#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200274void pwm_add_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530275#else
276static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
277{
278}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100279#endif
280
Mark Vels5243ef82009-01-18 18:42:45 +0100281#endif /* __LINUX_PWM_H */