blob: cfc3ed46cad20a26ffa131b51e25927ac6045f4c [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
Thierry Redingf051c462011-12-14 11:12:23 +010077enum {
78 PWMF_REQUESTED = 1 << 0,
79 PWMF_ENABLED = 1 << 1,
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -070080 PWMF_EXPORTED = 1 << 2,
Thierry Redingf051c462011-12-14 11:12:23 +010081};
82
Thierry Reding04883802015-07-27 11:58:32 +020083/**
84 * struct pwm_device - PWM channel object
85 * @label: name of the PWM device
86 * @flags: flags associated with the PWM device
87 * @hwpwm: per-chip relative index of the PWM device
88 * @pwm: global index of the PWM device
89 * @chip: PWM chip providing this PWM device
90 * @chip_data: chip-private data associated with the PWM device
Jonathan Richardsond1cd2142015-10-16 17:40:58 -070091 * @lock: used to serialize accesses to the PWM device where necessary
Thierry Reding04883802015-07-27 11:58:32 +020092 * @period: period of the PWM signal (in nanoseconds)
93 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
94 * @polarity: polarity of the PWM signal
95 */
Thierry Redingf051c462011-12-14 11:12:23 +010096struct pwm_device {
Thierry Reding6bc70642015-07-27 11:57:28 +020097 const char *label;
98 unsigned long flags;
99 unsigned int hwpwm;
100 unsigned int pwm;
101 struct pwm_chip *chip;
102 void *chip_data;
Jonathan Richardsond1cd2142015-10-16 17:40:58 -0700103 struct mutex lock;
Thierry Redingf051c462011-12-14 11:12:23 +0100104
Thierry Reding6bc70642015-07-27 11:57:28 +0200105 unsigned int period;
106 unsigned int duty_cycle;
107 enum pwm_polarity polarity;
Thierry Redingf051c462011-12-14 11:12:23 +0100108};
109
Boris Brezillon5c312522015-07-01 10:21:47 +0200110static inline bool pwm_is_enabled(const struct pwm_device *pwm)
111{
112 return test_bit(PWMF_ENABLED, &pwm->flags);
113}
114
Thierry Redingf051c462011-12-14 11:12:23 +0100115static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
116{
117 if (pwm)
118 pwm->period = period;
119}
120
Boris Brezillona1cf4212015-07-01 10:21:48 +0200121static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100122{
123 return pwm ? pwm->period : 0;
124}
125
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700126static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
127{
128 if (pwm)
129 pwm->duty_cycle = duty;
130}
131
Boris Brezillona1cf4212015-07-01 10:21:48 +0200132static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700133{
134 return pwm ? pwm->duty_cycle : 0;
135}
136
Philip, Avinash0aa08692012-07-24 19:35:32 +0530137/*
138 * pwm_set_polarity - configure the polarity of a PWM signal
139 */
140int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
141
Boris Brezillon011e7632015-07-01 10:21:49 +0200142static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
143{
144 return pwm ? pwm->polarity : PWM_POLARITY_NORMAL;
145}
146
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100147/**
148 * struct pwm_ops - PWM controller operations
149 * @request: optional hook for requesting a PWM
150 * @free: optional hook for freeing a PWM
151 * @config: configure duty cycles and period length for this PWM
Philip, Avinash0aa08692012-07-24 19:35:32 +0530152 * @set_polarity: configure the polarity of this PWM
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100153 * @enable: enable PWM output toggling
154 * @disable: disable PWM output toggling
Thierry Reding62099ab2012-03-26 09:31:48 +0200155 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100156 * @owner: helps prevent removal of modules exporting active PWMs
157 */
158struct pwm_ops {
Thierry Reding6bc70642015-07-27 11:57:28 +0200159 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
160 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
161 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
162 int duty_ns, int period_ns);
163 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
164 enum pwm_polarity polarity);
165 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
166 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Thierry Reding62099ab2012-03-26 09:31:48 +0200167#ifdef CONFIG_DEBUG_FS
Thierry Reding6bc70642015-07-27 11:57:28 +0200168 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
Thierry Reding62099ab2012-03-26 09:31:48 +0200169#endif
Thierry Reding6bc70642015-07-27 11:57:28 +0200170 struct module *owner;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100171};
172
173/**
Thierry Redingf051c462011-12-14 11:12:23 +0100174 * struct pwm_chip - abstract a PWM controller
175 * @dev: device providing the PWMs
176 * @list: list node for internal use
177 * @ops: callbacks for this PWM controller
178 * @base: number of first PWM controlled by this chip
179 * @npwm: number of PWMs controlled by this chip
180 * @pwms: array of PWM devices allocated by the framework
Thierry Reding04883802015-07-27 11:58:32 +0200181 * @of_xlate: request a PWM device given a device tree PWM specifier
182 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100183 * @can_sleep: must be true if the .config(), .enable() or .disable()
184 * operations may sleep
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100185 */
186struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200187 struct device *dev;
188 struct list_head list;
189 const struct pwm_ops *ops;
190 int base;
191 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100192
Thierry Reding6bc70642015-07-27 11:57:28 +0200193 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100194
Thierry Reding6bc70642015-07-27 11:57:28 +0200195 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
196 const struct of_phandle_args *args);
197 unsigned int of_pwm_n_cells;
198 bool can_sleep;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100199};
200
Tushar Behera0bcf1682012-09-12 15:31:46 +0530201#if IS_ENABLED(CONFIG_PWM)
Thierry Redingf051c462011-12-14 11:12:23 +0100202int pwm_set_chip_data(struct pwm_device *pwm, void *data);
203void *pwm_get_chip_data(struct pwm_device *pwm);
204
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700205int pwmchip_add_with_polarity(struct pwm_chip *chip,
206 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100207int pwmchip_add(struct pwm_chip *chip);
208int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100209struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
210 unsigned int index,
211 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200212
Philip, Avinash83af2402012-11-21 13:10:44 +0530213struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
214 const struct of_phandle_args *args);
215
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800216struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800217struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200218void pwm_put(struct pwm_device *pwm);
219
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800220struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800221struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
222 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900223void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100224
225bool pwm_can_sleep(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530226#else
227static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
228{
229 return -EINVAL;
230}
231
232static inline void *pwm_get_chip_data(struct pwm_device *pwm)
233{
234 return NULL;
235}
236
237static inline int pwmchip_add(struct pwm_chip *chip)
238{
239 return -EINVAL;
240}
241
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700242static inline int pwmchip_add_inversed(struct pwm_chip *chip)
243{
244 return -EINVAL;
245}
246
Tushar Behera0bcf1682012-09-12 15:31:46 +0530247static inline int pwmchip_remove(struct pwm_chip *chip)
248{
249 return -EINVAL;
250}
251
252static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
253 unsigned int index,
254 const char *label)
255{
256 return ERR_PTR(-ENODEV);
257}
258
259static inline struct pwm_device *pwm_get(struct device *dev,
260 const char *consumer)
261{
262 return ERR_PTR(-ENODEV);
263}
264
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800265static inline struct pwm_device *of_pwm_get(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 pwm_put(struct pwm_device *pwm)
272{
273}
274
275static inline struct pwm_device *devm_pwm_get(struct device *dev,
276 const char *consumer)
277{
278 return ERR_PTR(-ENODEV);
279}
280
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800281static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
282 struct device_node *np,
283 const char *con_id)
284{
285 return ERR_PTR(-ENODEV);
286}
287
Tushar Behera0bcf1682012-09-12 15:31:46 +0530288static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
289{
290}
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100291
292static inline bool pwm_can_sleep(struct pwm_device *pwm)
293{
294 return false;
295}
Tushar Behera0bcf1682012-09-12 15:31:46 +0530296#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900297
Thierry Reding8138d2d2012-03-26 08:42:48 +0200298struct pwm_lookup {
299 struct list_head list;
300 const char *provider;
301 unsigned int index;
302 const char *dev_id;
303 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200304 unsigned int period;
305 enum pwm_polarity polarity;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200306};
307
Alexandre Belloni42844022014-05-19 22:42:37 +0200308#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200309 { \
310 .provider = _provider, \
311 .index = _index, \
312 .dev_id = _dev_id, \
313 .con_id = _con_id, \
Alexandre Belloni42844022014-05-19 22:42:37 +0200314 .period = _period, \
315 .polarity = _polarity \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200316 }
317
Tushar Behera0bcf1682012-09-12 15:31:46 +0530318#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200319void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530320void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530321#else
322static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
323{
324}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530325
326static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
327{
328}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100329#endif
330
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700331#ifdef CONFIG_PWM_SYSFS
332void pwmchip_sysfs_export(struct pwm_chip *chip);
333void pwmchip_sysfs_unexport(struct pwm_chip *chip);
334#else
335static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
336{
337}
338
339static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
340{
341}
342#endif /* CONFIG_PWM_SYSFS */
343
Mark Vels5243ef82009-01-18 18:42:45 +0100344#endif /* __LINUX_PWM_H */