blob: 7caf549f720e3bec9d217d1d4862fb1a6f1da55b [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
Boris Brezillone39c0df2016-04-14 21:17:21 +020077/**
78 * struct pwm_args - board-dependent PWM arguments
79 * @period: reference period
80 * @polarity: reference polarity
81 *
82 * This structure describes board-dependent arguments attached to a PWM
83 * device. These arguments are usually retrieved from the PWM lookup table or
84 * device tree.
85 *
86 * Do not confuse this with the PWM state: PWM arguments represent the initial
87 * configuration that users want to use on this PWM device rather than the
88 * current PWM hardware state.
89 */
90struct pwm_args {
91 unsigned int period;
92 enum pwm_polarity polarity;
93};
94
Thierry Redingf051c462011-12-14 11:12:23 +010095enum {
96 PWMF_REQUESTED = 1 << 0,
97 PWMF_ENABLED = 1 << 1,
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -070098 PWMF_EXPORTED = 1 << 2,
Thierry Redingf051c462011-12-14 11:12:23 +010099};
100
Thierry Reding04883802015-07-27 11:58:32 +0200101/**
102 * struct pwm_device - PWM channel object
103 * @label: name of the PWM device
104 * @flags: flags associated with the PWM device
105 * @hwpwm: per-chip relative index of the PWM device
106 * @pwm: global index of the PWM device
107 * @chip: PWM chip providing this PWM device
108 * @chip_data: chip-private data associated with the PWM device
109 * @period: period of the PWM signal (in nanoseconds)
110 * @duty_cycle: duty cycle of the PWM signal (in nanoseconds)
111 * @polarity: polarity of the PWM signal
Boris Brezillone39c0df2016-04-14 21:17:21 +0200112 * @args: PWM arguments
Thierry Reding04883802015-07-27 11:58:32 +0200113 */
Thierry Redingf051c462011-12-14 11:12:23 +0100114struct pwm_device {
Thierry Reding6bc70642015-07-27 11:57:28 +0200115 const char *label;
116 unsigned long flags;
117 unsigned int hwpwm;
118 unsigned int pwm;
119 struct pwm_chip *chip;
120 void *chip_data;
Thierry Redingf051c462011-12-14 11:12:23 +0100121
Thierry Reding6bc70642015-07-27 11:57:28 +0200122 unsigned int period;
123 unsigned int duty_cycle;
124 enum pwm_polarity polarity;
Boris Brezillone39c0df2016-04-14 21:17:21 +0200125
126 struct pwm_args args;
Thierry Redingf051c462011-12-14 11:12:23 +0100127};
128
Boris Brezillon5c312522015-07-01 10:21:47 +0200129static inline bool pwm_is_enabled(const struct pwm_device *pwm)
130{
131 return test_bit(PWMF_ENABLED, &pwm->flags);
132}
133
Thierry Redingf051c462011-12-14 11:12:23 +0100134static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
135{
136 if (pwm)
137 pwm->period = period;
138}
139
Boris Brezillona1cf4212015-07-01 10:21:48 +0200140static inline unsigned int pwm_get_period(const struct pwm_device *pwm)
Thierry Redingf051c462011-12-14 11:12:23 +0100141{
142 return pwm ? pwm->period : 0;
143}
144
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700145static inline void pwm_set_duty_cycle(struct pwm_device *pwm, unsigned int duty)
146{
147 if (pwm)
148 pwm->duty_cycle = duty;
149}
150
Boris Brezillona1cf4212015-07-01 10:21:48 +0200151static inline unsigned int pwm_get_duty_cycle(const struct pwm_device *pwm)
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700152{
153 return pwm ? pwm->duty_cycle : 0;
154}
155
Philip, Avinash0aa08692012-07-24 19:35:32 +0530156/*
157 * pwm_set_polarity - configure the polarity of a PWM signal
158 */
159int pwm_set_polarity(struct pwm_device *pwm, enum pwm_polarity polarity);
160
Boris Brezillon011e7632015-07-01 10:21:49 +0200161static inline enum pwm_polarity pwm_get_polarity(const struct pwm_device *pwm)
162{
163 return pwm ? pwm->polarity : PWM_POLARITY_NORMAL;
164}
165
Boris Brezillone39c0df2016-04-14 21:17:21 +0200166static inline void pwm_get_args(const struct pwm_device *pwm,
167 struct pwm_args *args)
168{
169 *args = pwm->args;
170}
171
172static inline void pwm_apply_args(struct pwm_device *pwm)
173{
Boris Brezillone39c0df2016-04-14 21:17:21 +0200174 pwm_set_polarity(pwm, pwm->args.polarity);
175}
176
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100177/**
178 * struct pwm_ops - PWM controller operations
179 * @request: optional hook for requesting a PWM
180 * @free: optional hook for freeing a PWM
181 * @config: configure duty cycles and period length for this PWM
Philip, Avinash0aa08692012-07-24 19:35:32 +0530182 * @set_polarity: configure the polarity of this PWM
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100183 * @enable: enable PWM output toggling
184 * @disable: disable PWM output toggling
Thierry Reding62099ab2012-03-26 09:31:48 +0200185 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100186 * @owner: helps prevent removal of modules exporting active PWMs
187 */
188struct pwm_ops {
Thierry Reding6bc70642015-07-27 11:57:28 +0200189 int (*request)(struct pwm_chip *chip, struct pwm_device *pwm);
190 void (*free)(struct pwm_chip *chip, struct pwm_device *pwm);
191 int (*config)(struct pwm_chip *chip, struct pwm_device *pwm,
192 int duty_ns, int period_ns);
193 int (*set_polarity)(struct pwm_chip *chip, struct pwm_device *pwm,
194 enum pwm_polarity polarity);
195 int (*enable)(struct pwm_chip *chip, struct pwm_device *pwm);
196 void (*disable)(struct pwm_chip *chip, struct pwm_device *pwm);
Thierry Reding62099ab2012-03-26 09:31:48 +0200197#ifdef CONFIG_DEBUG_FS
Thierry Reding6bc70642015-07-27 11:57:28 +0200198 void (*dbg_show)(struct pwm_chip *chip, struct seq_file *s);
Thierry Reding62099ab2012-03-26 09:31:48 +0200199#endif
Thierry Reding6bc70642015-07-27 11:57:28 +0200200 struct module *owner;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100201};
202
203/**
Thierry Redingf051c462011-12-14 11:12:23 +0100204 * struct pwm_chip - abstract a PWM controller
205 * @dev: device providing the PWMs
206 * @list: list node for internal use
207 * @ops: callbacks for this PWM controller
208 * @base: number of first PWM controlled by this chip
209 * @npwm: number of PWMs controlled by this chip
210 * @pwms: array of PWM devices allocated by the framework
Thierry Reding04883802015-07-27 11:58:32 +0200211 * @of_xlate: request a PWM device given a device tree PWM specifier
212 * @of_pwm_n_cells: number of cells expected in the device tree PWM specifier
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100213 * @can_sleep: must be true if the .config(), .enable() or .disable()
214 * operations may sleep
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100215 */
216struct pwm_chip {
Thierry Reding6bc70642015-07-27 11:57:28 +0200217 struct device *dev;
218 struct list_head list;
219 const struct pwm_ops *ops;
220 int base;
221 unsigned int npwm;
Thierry Redingf051c462011-12-14 11:12:23 +0100222
Thierry Reding6bc70642015-07-27 11:57:28 +0200223 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100224
Thierry Reding6bc70642015-07-27 11:57:28 +0200225 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
226 const struct of_phandle_args *args);
227 unsigned int of_pwm_n_cells;
228 bool can_sleep;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100229};
230
Tushar Behera0bcf1682012-09-12 15:31:46 +0530231#if IS_ENABLED(CONFIG_PWM)
Thierry Redingf051c462011-12-14 11:12:23 +0100232int pwm_set_chip_data(struct pwm_device *pwm, void *data);
233void *pwm_get_chip_data(struct pwm_device *pwm);
234
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700235int pwmchip_add_with_polarity(struct pwm_chip *chip,
236 enum pwm_polarity polarity);
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100237int pwmchip_add(struct pwm_chip *chip);
238int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100239struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
240 unsigned int index,
241 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200242
Philip, Avinash83af2402012-11-21 13:10:44 +0530243struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc,
244 const struct of_phandle_args *args);
245
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800246struct pwm_device *pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800247struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200248void pwm_put(struct pwm_device *pwm);
249
Peter Ujfalusid4c0c472012-12-21 01:43:57 -0800250struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id);
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800251struct pwm_device *devm_of_pwm_get(struct device *dev, struct device_node *np,
252 const char *con_id);
Alexandre Courbot63543162012-08-01 19:20:58 +0900253void devm_pwm_put(struct device *dev, struct pwm_device *pwm);
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100254
255bool pwm_can_sleep(struct pwm_device *pwm);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530256#else
257static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
258{
259 return -EINVAL;
260}
261
262static inline void *pwm_get_chip_data(struct pwm_device *pwm)
263{
264 return NULL;
265}
266
267static inline int pwmchip_add(struct pwm_chip *chip)
268{
269 return -EINVAL;
270}
271
Tim Krygerb6a00fa2015-05-26 13:08:16 -0700272static inline int pwmchip_add_inversed(struct pwm_chip *chip)
273{
274 return -EINVAL;
275}
276
Tushar Behera0bcf1682012-09-12 15:31:46 +0530277static inline int pwmchip_remove(struct pwm_chip *chip)
278{
279 return -EINVAL;
280}
281
282static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
283 unsigned int index,
284 const char *label)
285{
286 return ERR_PTR(-ENODEV);
287}
288
289static inline struct pwm_device *pwm_get(struct device *dev,
290 const char *consumer)
291{
292 return ERR_PTR(-ENODEV);
293}
294
Peter Ujfalusi8eb96122012-12-21 01:43:58 -0800295static inline struct pwm_device *of_pwm_get(struct device_node *np,
296 const char *con_id)
297{
298 return ERR_PTR(-ENODEV);
299}
300
Tushar Behera0bcf1682012-09-12 15:31:46 +0530301static inline void pwm_put(struct pwm_device *pwm)
302{
303}
304
305static inline struct pwm_device *devm_pwm_get(struct device *dev,
306 const char *consumer)
307{
308 return ERR_PTR(-ENODEV);
309}
310
Peter Ujfalusi261a5ed2012-12-21 01:43:59 -0800311static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
312 struct device_node *np,
313 const char *con_id)
314{
315 return ERR_PTR(-ENODEV);
316}
317
Tushar Behera0bcf1682012-09-12 15:31:46 +0530318static inline void devm_pwm_put(struct device *dev, struct pwm_device *pwm)
319{
320}
Florian Vaussard6e69ab12013-01-28 15:00:57 +0100321
322static inline bool pwm_can_sleep(struct pwm_device *pwm)
323{
324 return false;
325}
Tushar Behera0bcf1682012-09-12 15:31:46 +0530326#endif
Alexandre Courbot63543162012-08-01 19:20:58 +0900327
Thierry Reding8138d2d2012-03-26 08:42:48 +0200328struct pwm_lookup {
329 struct list_head list;
330 const char *provider;
331 unsigned int index;
332 const char *dev_id;
333 const char *con_id;
Alexandre Belloni3796ce12014-05-19 22:42:32 +0200334 unsigned int period;
335 enum pwm_polarity polarity;
Thierry Reding8138d2d2012-03-26 08:42:48 +0200336};
337
Alexandre Belloni42844022014-05-19 22:42:37 +0200338#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id, _period, _polarity) \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200339 { \
340 .provider = _provider, \
341 .index = _index, \
342 .dev_id = _dev_id, \
343 .con_id = _con_id, \
Alexandre Belloni42844022014-05-19 22:42:37 +0200344 .period = _period, \
345 .polarity = _polarity \
Thierry Reding8138d2d2012-03-26 08:42:48 +0200346 }
347
Tushar Behera0bcf1682012-09-12 15:31:46 +0530348#if IS_ENABLED(CONFIG_PWM)
Thierry Reding8138d2d2012-03-26 08:42:48 +0200349void pwm_add_table(struct pwm_lookup *table, size_t num);
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530350void pwm_remove_table(struct pwm_lookup *table, size_t num);
Tushar Behera0bcf1682012-09-12 15:31:46 +0530351#else
352static inline void pwm_add_table(struct pwm_lookup *table, size_t num)
353{
354}
Shobhit Kumarefb0de52015-05-05 15:04:18 +0530355
356static inline void pwm_remove_table(struct pwm_lookup *table, size_t num)
357{
358}
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100359#endif
360
H Hartley Sweeten76abbdde2013-06-11 10:38:59 -0700361#ifdef CONFIG_PWM_SYSFS
362void pwmchip_sysfs_export(struct pwm_chip *chip);
363void pwmchip_sysfs_unexport(struct pwm_chip *chip);
364#else
365static inline void pwmchip_sysfs_export(struct pwm_chip *chip)
366{
367}
368
369static inline void pwmchip_sysfs_unexport(struct pwm_chip *chip)
370{
371}
372#endif /* CONFIG_PWM_SYSFS */
373
Mark Vels5243ef82009-01-18 18:42:45 +0100374#endif /* __LINUX_PWM_H */