4 * Models a single-channel PWM device using a timer and a GPIO pin.
6 * Copyright (C) 2010 Bill Gatliff <bgat@billgatliff.com>
8 * This program is free software; you may redistribute and/or modify
9 * it under the terms of the GNU General Public License Version 2, as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/hrtimer.h>
26 #include <linux/err.h>
27 #include <linux/platform_device.h>
28 #include <linux/workqueue.h>
29 #include <linux/gpio.h>
30 #include <linux/slab.h>
31 #include <linux/pwm/pwm.h>
34 struct pwm_device pwm;
36 struct work_struct work;
37 pwm_callback_t callback;
39 unsigned long polarity : 1;
40 unsigned long active : 1;
43 static inline struct gpio_pwm *to_gpio_pwm(const struct pwm_channel *p)
45 return container_of(p->pwm, struct gpio_pwm, pwm);
49 gpio_pwm_work (struct work_struct *work)
51 struct gpio_pwm *gp = container_of(work, struct gpio_pwm, work);
54 gpio_direction_output(gp->gpio, gp->polarity ? 1 : 0);
56 gpio_direction_output(gp->gpio, gp->polarity ? 0 : 1);
59 static enum hrtimer_restart
60 gpio_pwm_timeout(struct hrtimer *t)
62 struct gpio_pwm *gp = container_of(t, struct gpio_pwm, timer);
65 if (unlikely(gp->pwm.channels[0].duty_ticks == 0))
67 else if (unlikely(gp->pwm.channels[0].duty_ticks
68 == gp->pwm.channels[0].period_ticks))
73 if (gpio_cansleep(gp->gpio))
74 schedule_work(&gp->work);
76 gpio_pwm_work(&gp->work);
78 if (!gp->active && gp->pwm.channels[0].callback)
79 gp->pwm.channels[0].callback(&gp->pwm.channels[0]);
81 if (unlikely(!gp->active &&
82 (gp->pwm.channels[0].flags & BIT(FLAG_STOP)))) {
83 clear_bit(FLAG_STOP, &gp->pwm.channels[0].flags);
84 complete_all(&gp->pwm.channels[0].complete);
85 return HRTIMER_NORESTART;
89 tnew = ktime_set(0, gp->pwm.channels[0].duty_ticks);
91 tnew = ktime_set(0, gp->pwm.channels[0].period_ticks
92 - gp->pwm.channels[0].duty_ticks);
93 hrtimer_start(&gp->timer, tnew, HRTIMER_MODE_REL);
95 return HRTIMER_NORESTART;
98 static void gpio_pwm_start(struct pwm_channel *p)
100 struct gpio_pwm *gp = to_gpio_pwm(p);
103 gpio_pwm_timeout(&gp->timer);
107 gpio_pwm_config_nosleep(struct pwm_channel *p,
108 struct pwm_channel_config *c)
110 struct gpio_pwm *gp = to_gpio_pwm(p);
114 spin_lock_irqsave(&p->lock, flags);
116 switch (c->config_mask) {
118 case PWM_CONFIG_DUTY_TICKS:
119 p->duty_ticks = c->duty_ticks;
122 case PWM_CONFIG_START:
123 if (!hrtimer_active(&gp->timer)) {
132 spin_unlock_irqrestore(&p->lock, flags);
137 gpio_pwm_stop_sync(struct pwm_channel *p)
139 struct gpio_pwm *gp = to_gpio_pwm(p);
141 int was_on = hrtimer_active(&gp->timer);
145 init_completion(&p->complete);
146 set_bit(FLAG_STOP, &p->flags);
147 ret = wait_for_completion_interruptible(&p->complete);
150 } while (p->flags & BIT(FLAG_STOP));
157 gpio_pwm_config(struct pwm_channel *p,
158 struct pwm_channel_config *c)
160 struct gpio_pwm *gp = to_gpio_pwm(p);
163 if (p->pwm->config_nosleep) {
164 if (!p->pwm->config_nosleep(p, c))
170 was_on = gpio_pwm_stop_sync(p);
174 if (c->config_mask & PWM_CONFIG_PERIOD_TICKS)
175 p->period_ticks = c->period_ticks;
177 if (c->config_mask & PWM_CONFIG_DUTY_TICKS)
178 p->duty_ticks = c->duty_ticks;
180 if (c->config_mask & PWM_CONFIG_POLARITY) {
181 gp->polarity = c->polarity ? 1 : 0;
182 p->active_high = gp->polarity;
185 if ((c->config_mask & PWM_CONFIG_START)
186 || (was_on && !(c->config_mask & PWM_CONFIG_STOP)))
193 gpio_pwm_set_callback(struct pwm_channel *p,
194 pwm_callback_t callback)
196 struct gpio_pwm *gp = to_gpio_pwm(p);
197 gp->callback = callback;
202 gpio_pwm_request(struct pwm_channel *p)
204 p->tick_hz = 1000000000UL;
209 gpio_pwm_probe(struct platform_device *pdev)
212 struct gpio_pwm_platform_data *gpd = pdev->dev.platform_data;
215 /* TODO: create configfs entries, so users can assign GPIOs to
216 * PWMs at runtime instead of creating a platform_device
217 * specification and rebuilding their kernel */
219 if (!gpd || gpio_request(gpd->gpio, dev_name(&pdev->dev)))
222 gp = kzalloc(sizeof(*gp), GFP_KERNEL);
228 platform_set_drvdata(pdev, gp);
230 gp->pwm.dev = &pdev->dev;
231 gp->pwm.bus_id = dev_name(&pdev->dev);
233 gp->gpio = gpd->gpio;
235 INIT_WORK(&gp->work, gpio_pwm_work);
237 hrtimer_init(&gp->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
238 gp->timer.function = gpio_pwm_timeout;
240 gp->pwm.owner = THIS_MODULE;
241 gp->pwm.config_nosleep = gpio_pwm_config_nosleep;
242 gp->pwm.config = gpio_pwm_config;
243 gp->pwm.request = gpio_pwm_request;
244 gp->pwm.set_callback = gpio_pwm_set_callback;
246 ret = pwm_register(&gp->pwm);
248 goto err_pwm_register;
253 platform_set_drvdata(pdev, 0);
260 gpio_pwm_remove(struct platform_device *pdev)
262 struct gpio_pwm *gp = platform_get_drvdata(pdev);
265 ret = pwm_unregister(&gp->pwm);
266 hrtimer_cancel(&gp->timer);
267 cancel_work_sync(&gp->work);
268 platform_set_drvdata(pdev, 0);
274 static struct platform_driver gpio_pwm_driver = {
277 .owner = THIS_MODULE,
279 .probe = gpio_pwm_probe,
280 .remove = gpio_pwm_remove,
283 static int __init gpio_pwm_init(void)
285 return platform_driver_register(&gpio_pwm_driver);
287 module_init(gpio_pwm_init);
289 static void __exit gpio_pwm_exit(void)
291 platform_driver_unregister(&gpio_pwm_driver);
293 module_exit(gpio_pwm_exit);
295 MODULE_AUTHOR("Bill Gatliff <bgat@billgatliff.com>");
296 MODULE_DESCRIPTION("PWM output using GPIO and a high-resolution timer");
297 MODULE_LICENSE("GPL");
298 MODULE_ALIAS("platform:gpio_pwm");