blob: 21d076c5089e9646418113fb9d27d30e4ba109f7 [file] [log] [blame]
Russell King1a189b92008-04-13 21:41:55 +01001#ifndef __LINUX_PWM_H
2#define __LINUX_PWM_H
3
Thierry Reding7299ab72011-12-14 11:10:32 +01004#include <linux/of.h>
5
Russell King1a189b92008-04-13 21:41:55 +01006struct pwm_device;
Thierry Reding62099ab2012-03-26 09:31:48 +02007struct seq_file;
Russell King1a189b92008-04-13 21:41:55 +01008
9/*
10 * pwm_request - request a PWM device
11 */
12struct pwm_device *pwm_request(int pwm_id, const char *label);
13
14/*
15 * pwm_free - free a PWM device
16 */
17void pwm_free(struct pwm_device *pwm);
18
19/*
20 * pwm_config - change a PWM device configuration
21 */
22int pwm_config(struct pwm_device *pwm, int duty_ns, int period_ns);
23
24/*
25 * pwm_enable - start a PWM output toggling
26 */
27int pwm_enable(struct pwm_device *pwm);
28
29/*
30 * pwm_disable - stop a PWM output toggling
31 */
32void pwm_disable(struct pwm_device *pwm);
33
Sascha Hauer0c2498f2011-01-28 09:40:40 +010034#ifdef CONFIG_PWM
35struct pwm_chip;
36
Thierry Redingf051c462011-12-14 11:12:23 +010037enum {
38 PWMF_REQUESTED = 1 << 0,
39 PWMF_ENABLED = 1 << 1,
40};
41
42struct pwm_device {
43 const char *label;
44 unsigned long flags;
45 unsigned int hwpwm;
46 unsigned int pwm;
47 struct pwm_chip *chip;
48 void *chip_data;
49
50 unsigned int period; /* in nanoseconds */
51};
52
53static inline void pwm_set_period(struct pwm_device *pwm, unsigned int period)
54{
55 if (pwm)
56 pwm->period = period;
57}
58
59static inline unsigned int pwm_get_period(struct pwm_device *pwm)
60{
61 return pwm ? pwm->period : 0;
62}
63
Sascha Hauer0c2498f2011-01-28 09:40:40 +010064/**
65 * struct pwm_ops - PWM controller operations
66 * @request: optional hook for requesting a PWM
67 * @free: optional hook for freeing a PWM
68 * @config: configure duty cycles and period length for this PWM
69 * @enable: enable PWM output toggling
70 * @disable: disable PWM output toggling
Thierry Reding62099ab2012-03-26 09:31:48 +020071 * @dbg_show: optional routine to show contents in debugfs
Sascha Hauer0c2498f2011-01-28 09:40:40 +010072 * @owner: helps prevent removal of modules exporting active PWMs
73 */
74struct pwm_ops {
Thierry Redingf051c462011-12-14 11:12:23 +010075 int (*request)(struct pwm_chip *chip,
76 struct pwm_device *pwm);
77 void (*free)(struct pwm_chip *chip,
78 struct pwm_device *pwm);
79 int (*config)(struct pwm_chip *chip,
80 struct pwm_device *pwm,
81 int duty_ns, int period_ns);
82 int (*enable)(struct pwm_chip *chip,
83 struct pwm_device *pwm);
84 void (*disable)(struct pwm_chip *chip,
85 struct pwm_device *pwm);
Thierry Reding62099ab2012-03-26 09:31:48 +020086#ifdef CONFIG_DEBUG_FS
87 void (*dbg_show)(struct pwm_chip *chip,
88 struct seq_file *s);
89#endif
Sascha Hauer0c2498f2011-01-28 09:40:40 +010090 struct module *owner;
91};
92
93/**
Thierry Redingf051c462011-12-14 11:12:23 +010094 * struct pwm_chip - abstract a PWM controller
95 * @dev: device providing the PWMs
96 * @list: list node for internal use
97 * @ops: callbacks for this PWM controller
98 * @base: number of first PWM controlled by this chip
99 * @npwm: number of PWMs controlled by this chip
100 * @pwms: array of PWM devices allocated by the framework
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100101 */
102struct pwm_chip {
Thierry Redingf051c462011-12-14 11:12:23 +0100103 struct device *dev;
104 struct list_head list;
105 const struct pwm_ops *ops;
106 int base;
107 unsigned int npwm;
108
109 struct pwm_device *pwms;
Thierry Reding7299ab72011-12-14 11:10:32 +0100110
111 struct pwm_device * (*of_xlate)(struct pwm_chip *pc,
112 const struct of_phandle_args *args);
113 unsigned int of_pwm_n_cells;
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100114};
115
Thierry Redingf051c462011-12-14 11:12:23 +0100116int pwm_set_chip_data(struct pwm_device *pwm, void *data);
117void *pwm_get_chip_data(struct pwm_device *pwm);
118
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100119int pwmchip_add(struct pwm_chip *chip);
120int pwmchip_remove(struct pwm_chip *chip);
Thierry Redingf051c462011-12-14 11:12:23 +0100121struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
122 unsigned int index,
123 const char *label);
Thierry Reding8138d2d2012-03-26 08:42:48 +0200124
125struct pwm_device *pwm_get(struct device *dev, const char *consumer);
126void pwm_put(struct pwm_device *pwm);
127
128struct pwm_lookup {
129 struct list_head list;
130 const char *provider;
131 unsigned int index;
132 const char *dev_id;
133 const char *con_id;
134};
135
136#define PWM_LOOKUP(_provider, _index, _dev_id, _con_id) \
137 { \
138 .provider = _provider, \
139 .index = _index, \
140 .dev_id = _dev_id, \
141 .con_id = _con_id, \
142 }
143
144void pwm_add_table(struct pwm_lookup *table, size_t num);
145
Sascha Hauer0c2498f2011-01-28 09:40:40 +0100146#endif
147
Mark Vels5243ef82009-01-18 18:42:45 +0100148#endif /* __LINUX_PWM_H */