blob: 7c4b6f35241d716f907ef6e918717e0070506faf [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
Sascha Hauer557fe992014-01-24 08:54:16 +010010#if IS_ENABLED(CONFIG_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 {
Thierry Reding6bc70642015-07-27 11:57:28 +020083 const char *label;
84 unsigned long flags;
85 unsigned int hwpwm;
86 unsigned int pwm;
87 struct pwm_chip *chip;
88 void *chip_data;
Thierry Redingf051c462011-12-14 11:12:23 +010089
Thierry Reding6bc70642015-07-27 11:57:28 +020090 unsigned int period;
91 unsigned int duty_cycle;
92 enum pwm_polarity polarity;
Thierry Redingf051c462011-12-14 11:12:23 +010093};
94
Boris Brezillon5c312522015-07-01 10:21:47 +020095static inline bool pwm_is_enabled(const struct pwm_device *pwm)
96{
97 return test_bit(PWMF_ENABLED, &pwm->flags);
98}
99
Thierry Redingf051c462011-12-14 11:12:23 +0100100static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
101{
102 if (pwm)
103 pwm->period = period;
104}
105
Boris Brezillona1cf4212015-07-01 10:21:48 +0200106static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100107{
108 return pwm ? pwm->period : 0;
109}
110
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700111static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
112{
113 if (pwm)
114 pwm->duty_cycle = duty;
115}
116
Boris Brezillona1cf4212015-07-01 10:21:48 +0200117static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700118{
119 return pwm ? pwm->duty_cycle : 0;
120}
121
Philip, Avinash0aa08692012-07-24 19:35:32 +0530122/*
123 * pwm_set_polarity - configure the polarity of a PWM signal
124 */
125int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
126
Boris Brezillon011e7632015-07-01 10:21:49 +0200127static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
128{
129 return pwm ? pwm->polarity : PWM_POLARITY_NORMAL;
130}
131
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100132/**
133 * struct pwm_ops - PWM controller operations
134 * @request: optional hook for requesting a PWM
135 * @free: optional hook for freeing a PWM
136 * @config: configure duty cycles and period length for this PWM
Philip, Avinash0aa08692012-07-24 19:35:32 +0530137 * @set_polarity: configure the polarity of this PWM
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100138 * @enable: enable PWM output toggling
139 * @disable: disable PWM output toggling
Thierry Reding62099ab2012-03-26 09:31:48 +0200140 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100141 * @owner: helps prevent removal of modules exporting active PWMs
142 */
143struct pwm_ops {
Thierry Reding6bc70642015-07-27 11:57:28 +0200144 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
145 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
146 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
147 int duty_ns, int period_ns);
148 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
149 enum pwm_polarity polarity);
150 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
151 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Thierry Reding62099ab2012-03-26 09:31:48 +0200152#ifdef CONFIG_DEBUG_FS
Thierry Reding6bc70642015-07-27 11:57:28 +0200153 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
Thierry Reding62099ab2012-03-26 09:31:48 +0200154#endif
Thierry Reding6bc70642015-07-27 11:57:28 +0200155 struct module *owner;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100156};
157
158/**
Thierry Redingf051c462011-12-14 11:12:23 +0100159 * struct pwm_chip - abstract a PWM controller
160 * @dev: device providing the PWMs
161 * @list: list node for internal use
162 * @ops: callbacks for this PWM controller
163 * @base: number of first PWM controlled by this chip
164 * @npwm: number of PWMs controlled by this chip
165 * @pwms: array of PWM devices allocated by the framework
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100166 * @can_sleep: must be true if the .config(), .enable() or .disable()
167 * operations may sleep
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100168 */
169struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200170 struct device *dev;
171 struct list_head list;
172 const struct pwm_ops *ops;
173 int base;
174 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100175
Thierry Reding6bc70642015-07-27 11:57:28 +0200176 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100177
Thierry Reding6bc70642015-07-27 11:57:28 +0200178 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
179 const struct of_phandle_args *args);
180 unsigned int of_pwm_n_cells;
181 bool can_sleep;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100182};
183
Tushar Behera0bcf1682012-09-12 15:31:46 +0530184#if IS_ENABLED(CONFIG_PWM)
Thierry Redingf051c462011-12-14 11:12:23 +0100185int pwm_set_chip_data(struct pwm_device *pwm, void *data);
186void *pwm_get_chip_data(struct pwm_device *pwm);
187
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700188int pwmchip_add_with_polarity(struct pwm_chip *chip,
189 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100190int pwmchip_add(struct pwm_chip *chip);
191int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100192struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
193 unsigned int index,
194 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200195
Philip, Avinash83af2402012-11-21 13:10:44 +0530196struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
197 const struct of_phandle_args *args);
198
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800199struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800200struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200201void pwm_put(struct pwm_device *pwm);
202
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800203struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800204struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
205 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900206void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100207
208bool pwm_can_sleep(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530209#else
210static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
211{
212 return -EINVAL;
213}
214
215static inline void *pwm_get_chip_data(struct pwm_device *pwm)
216{
217 return NULL;
218}
219
220static inline int pwmchip_add(struct pwm_chip *chip)
221{
222 return -EINVAL;
223}
224
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700225static inline int pwmchip_add_inversed(struct pwm_chip *chip)
226{
227 return -EINVAL;
228}
229
Tushar Behera0bcf1682012-09-12 15:31:46 +0530230static inline int pwmchip_remove(struct pwm_chip *chip)
231{
232 return -EINVAL;
233}
234
235static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
236 unsigned int index,
237 const char *label)
238{
239 return ERR_PTR(-ENODEV);
240}
241
242static inline struct pwm_device *pwm_get(struct device *dev,
243 const char *consumer)
244{
245 return ERR_PTR(-ENODEV);
246}
247
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800248static inline struct pwm_device *of_pwm_get(struct device_node *np,
249 const char *con_id)
250{
251 return ERR_PTR(-ENODEV);
252}
253
Tushar Behera0bcf1682012-09-12 15:31:46 +0530254static inline void pwm_put(struct pwm_device *pwm)
255{
256}
257
258static inline struct pwm_device *devm_pwm_get(struct device *dev,
259 const char *consumer)
260{
261 return ERR_PTR(-ENODEV);
262}
263
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800264static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
265 struct device_node *np,
266 const char *con_id)
267{
268 return ERR_PTR(-ENODEV);
269}
270
Tushar Behera0bcf1682012-09-12 15:31:46 +0530271static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
272{
273}
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100274
275static inline bool pwm_can_sleep(struct pwm_device *pwm)
276{
277 return false;
278}
Tushar Behera0bcf1682012-09-12 15:31:46 +0530279#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900280
Thierry Reding8138d2d2012-03-26 08:42:48 +0200281struct pwm_lookup {
282 struct list_head list;
283 const char *provider;
284 unsigned int index;
285 const char *dev_id;
286 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200287 unsigned int period;
288 enum pwm_polarity polarity;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200289};
290
Alexandre Belloni42844022014-05-19 22:42:37 +0200291#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200292 { \
293 .provider = _provider, \
294 .index = _index, \
295 .dev_id = _dev_id, \
296 .con_id = _con_id, \
Alexandre Belloni42844022014-05-19 22:42:37 +0200297 .period = _period, \
298 .polarity = _polarity \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200299 }
300
Tushar Behera0bcf1682012-09-12 15:31:46 +0530301#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200302void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530303void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530304#else
305static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
306{
307}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530308
309static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
310{
311}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100312#endif
313
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700314#ifdef CONFIG_PWM_SYSFS
315void pwmchip_sysfs_export(struct pwm_chip *chip);
316void pwmchip_sysfs_unexport(struct pwm_chip *chip);
317#else
318static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
319{
320}
321
322static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
323{
324}
325#endif /* CONFIG_PWM_SYSFS */
326
Mark Vels5243ef82009-01-18 18:42:45 +0100327#endif /* __LINUX_PWM_H */