Linux 4.6-rc6
[cris-mirror.git] / include / linux / pwm.h
blobcfc3ed46cad20a26ffa131b51e25927ac6045f4c
1 #ifndef __LINUX_PWM_H
2 #define __LINUX_PWM_H
4 #include <linux/err.h>
5 #include <linux/mutex.h>
6 #include <linux/of.h>
8 struct pwm_device;
9 struct seq_file;
11 #if IS_ENABLED(CONFIG_PWM)
13 * pwm_request - request a PWM device
15 struct pwm_device *pwm_request(int pwm_id, const char *label);
18 * pwm_free - free a PWM device
20 void pwm_free(struct pwm_device *pwm);
23 * pwm_config - change a PWM device configuration
25 int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
28 * pwm_enable - start a PWM output toggling
30 int pwm_enable(struct pwm_device *pwm);
33 * pwm_disable - stop a PWM output toggling
35 void pwm_disable(struct pwm_device *pwm);
36 #else
37 static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
39 return ERR_PTR(-ENODEV);
42 static inline void pwm_free(struct pwm_device *pwm)
46 static inline int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns)
48 return -EINVAL;
51 static inline int pwm_enable(struct pwm_device *pwm)
53 return -EINVAL;
56 static inline void pwm_disable(struct pwm_device *pwm)
59 #endif
61 struct pwm_chip;
63 /**
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
72 enum pwm_polarity {
73 PWM_POLARITY_NORMAL,
74 PWM_POLARITY_INVERSED,
77 enum {
78 PWMF_REQUESTED = 1 << 0,
79 PWMF_ENABLED = 1 << 1,
80 PWMF_EXPORTED = 1 << 2,
83 /**
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
91 * @lock: used to serialize accesses to the PWM device where necessary
92 * @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
96 struct pwm_device {
97 const char *label;
98 unsigned long flags;
99 unsigned int hwpwm;
100 unsigned int pwm;
101 struct pwm_chip *chip;
102 void *chip_data;
103 struct mutex lock;
105 unsigned int period;
106 unsigned int duty_cycle;
107 enum pwm_polarity polarity;
110 static inline bool pwm_is_enabled(const struct pwm_device *pwm)
112 return test_bit(PWMF_ENABLED, &pwm->flags);
115 static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
117 if (pwm)
118 pwm->period = period;
121 static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
123 return pwm ? pwm->period : 0;
126 static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
128 if (pwm)
129 pwm->duty_cycle = duty;
132 static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
134 return pwm ? pwm->duty_cycle : 0;
138 * pwm_set_polarity - configure the polarity of a PWM signal
140 int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
142 static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
144 return pwm ? pwm->polarity : PWM_POLARITY_NORMAL;
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
152 * @set_polarity: configure the polarity of this PWM
153 * @enable: enable PWM output toggling
154 * @disable: disable PWM output toggling
155 * @dbg_show: optional routine to show contents in debugfs
156 * @owner: helps prevent removal of modules exporting active PWMs
158 struct pwm_ops {
159 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);
167 #ifdef CONFIG_DEBUG_FS
168 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
169 #endif
170 struct module *owner;
174 * 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
181 * @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
183 * @can_sleep: must be true if the .config(), .enable() or .disable()
184 * operations may sleep
186 struct pwm_chip {
187 struct device *dev;
188 struct list_head list;
189 const struct pwm_ops *ops;
190 int base;
191 unsigned int npwm;
193 struct pwm_device *pwms;
195 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;
201 #if IS_ENABLED(CONFIG_PWM)
202 int pwm_set_chip_data(struct pwm_device *pwm, void *data);
203 void *pwm_get_chip_data(struct pwm_device *pwm);
205 int pwmchip_add_with_polarity(struct pwm_chip *chip,
206 enum pwm_polarity polarity);
207 int pwmchip_add(struct pwm_chip *chip);
208 int pwmchip_remove(struct pwm_chip *chip);
209 struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
210 unsigned int index,
211 const char *label);
213 struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
214 const struct of_phandle_args *args);
216 struct pwm_device *pwm_get(struct device *dev, const char *con_id);
217 struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
218 void pwm_put(struct pwm_device *pwm);
220 struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
221 struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
222 const char *con_id);
223 void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
225 bool pwm_can_sleep(struct pwm_device *pwm);
226 #else
227 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
229 return -EINVAL;
232 static inline void *pwm_get_chip_data(struct pwm_device *pwm)
234 return NULL;
237 static inline int pwmchip_add(struct pwm_chip *chip)
239 return -EINVAL;
242 static inline int pwmchip_add_inversed(struct pwm_chip *chip)
244 return -EINVAL;
247 static inline int pwmchip_remove(struct pwm_chip *chip)
249 return -EINVAL;
252 static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
253 unsigned int index,
254 const char *label)
256 return ERR_PTR(-ENODEV);
259 static inline struct pwm_device *pwm_get(struct device *dev,
260 const char *consumer)
262 return ERR_PTR(-ENODEV);
265 static inline struct pwm_device *of_pwm_get(struct device_node *np,
266 const char *con_id)
268 return ERR_PTR(-ENODEV);
271 static inline void pwm_put(struct pwm_device *pwm)
275 static inline struct pwm_device *devm_pwm_get(struct device *dev,
276 const char *consumer)
278 return ERR_PTR(-ENODEV);
281 static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
282 struct device_node *np,
283 const char *con_id)
285 return ERR_PTR(-ENODEV);
288 static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
292 static inline bool pwm_can_sleep(struct pwm_device *pwm)
294 return false;
296 #endif
298 struct 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;
304 unsigned int period;
305 enum pwm_polarity polarity;
308 #define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
310 .provider = _provider, \
311 .index = _index, \
312 .dev_id = _dev_id, \
313 .con_id = _con_id, \
314 .period = _period, \
315 .polarity = _polarity \
318 #if IS_ENABLED(CONFIG_PWM)
319 void pwm_add_table(struct pwm_lookup *table, size_t num);
320 void pwm_remove_table(struct pwm_lookup *table, size_t num);
321 #else
322 static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
326 static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
329 #endif
331 #ifdef CONFIG_PWM_SYSFS
332 void pwmchip_sysfs_export(struct pwm_chip *chip);
333 void pwmchip_sysfs_unexport(struct pwm_chip *chip);
334 #else
335 static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
339 static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
342 #endif /* CONFIG_PWM_SYSFS */
344 #endif /* __LINUX_PWM_H */