xref: /OK3568_Linux_fs/kernel/drivers/pwm/pwm-gpio.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2015 Olliver Schinagl <oliver@schinagl.nl>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * This driver adds a high-resolution timer based PWM driver. Since this is a
15  * bit-banged driver, accuracy will always depend on a lot of factors, such as
16  * GPIO toggle speed and system load.
17  */
18 
19 #include <linux/device.h>
20 #include <linux/errno.h>
21 #include <linux/err.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/hrtimer.h>
24 #include <linux/ktime.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/mod_devicetable.h>
28 #include <linux/of.h>
29 #include <linux/of_gpio.h>
30 #include <linux/platform_device.h>
31 #include <linux/property.h>
32 #include <linux/pwm.h>
33 
34 struct gpio_pwm_chip {
35 	struct pwm_chip chip;
36 	struct hrtimer timer;
37 	struct gpio_desc *gpiod;
38 	unsigned int on_time;
39 	unsigned int off_time;
40 	bool pin_on;
41 };
42 
to_gpio_pwm_chip(struct pwm_chip * c)43 static inline struct gpio_pwm_chip *to_gpio_pwm_chip(struct pwm_chip *c)
44 {
45 	return container_of(c, struct gpio_pwm_chip, chip);
46 }
47 
gpio_pwm_off(struct gpio_pwm_chip * pc)48 static void gpio_pwm_off(struct gpio_pwm_chip *pc)
49 {
50 	enum pwm_polarity polarity = pwm_get_polarity(pc->chip.pwms);
51 
52 	gpiod_set_value(pc->gpiod, polarity ? 1 : 0);
53 }
54 
gpio_pwm_on(struct gpio_pwm_chip * pc)55 static void gpio_pwm_on(struct gpio_pwm_chip *pc)
56 {
57 	enum pwm_polarity polarity = pwm_get_polarity(pc->chip.pwms);
58 
59 	gpiod_set_value(pc->gpiod, polarity ? 0 : 1);
60 }
61 
gpio_pwm_timer(struct hrtimer * timer)62 static enum hrtimer_restart gpio_pwm_timer(struct hrtimer *timer)
63 {
64 	struct gpio_pwm_chip *pc = container_of(timer,
65 						struct gpio_pwm_chip,
66 						timer);
67 	if (!pwm_is_enabled(pc->chip.pwms)) {
68 		gpio_pwm_off(pc);
69 		pc->pin_on = false;
70 		return HRTIMER_NORESTART;
71 	}
72 
73 	if (!pc->pin_on) {
74 		hrtimer_forward_now(&pc->timer, ns_to_ktime(pc->on_time));
75 
76 		if (pc->on_time) {
77 			gpio_pwm_on(pc);
78 			pc->pin_on = true;
79 		}
80 
81 	} else {
82 		hrtimer_forward_now(&pc->timer, ns_to_ktime(pc->off_time));
83 
84 		if (pc->off_time) {
85 			gpio_pwm_off(pc);
86 			pc->pin_on = false;
87 		}
88 	}
89 
90 	return HRTIMER_RESTART;
91 }
92 
gpio_pwm_config(struct pwm_chip * chip,struct pwm_device * pwm,int duty_ns,int period_ns)93 static int gpio_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
94 			    int duty_ns, int period_ns)
95 {
96 	struct gpio_pwm_chip *pc = to_gpio_pwm_chip(chip);
97 
98 	pc->on_time = duty_ns;
99 	pc->off_time = period_ns - duty_ns;
100 
101 	return 0;
102 }
103 
gpio_pwm_set_polarity(struct pwm_chip * chip,struct pwm_device * pwm,enum pwm_polarity polarity)104 static int gpio_pwm_set_polarity(struct pwm_chip *chip, struct pwm_device *pwm,
105 				 enum pwm_polarity polarity)
106 {
107 	return 0;
108 }
109 
gpio_pwm_enable(struct pwm_chip * chip,struct pwm_device * pwm)110 static int gpio_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
111 {
112 	struct gpio_pwm_chip *pc = to_gpio_pwm_chip(chip);
113 
114 	if (pwm_is_enabled(pc->chip.pwms))
115 		return -EBUSY;
116 
117 	if (pc->off_time) {
118 		hrtimer_start(&pc->timer, ktime_set(0, 0), HRTIMER_MODE_REL);
119 	} else {
120 		if (pc->on_time)
121 			gpio_pwm_on(pc);
122 		else
123 			gpio_pwm_off(pc);
124 	}
125 
126 	return 0;
127 }
128 
gpio_pwm_disable(struct pwm_chip * chip,struct pwm_device * pwm)129 static void gpio_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
130 {
131 	struct gpio_pwm_chip *pc = to_gpio_pwm_chip(chip);
132 
133 	if (!pc->off_time)
134 		gpio_pwm_off(pc);
135 }
136 
137 static const struct pwm_ops gpio_pwm_ops = {
138 	.config = gpio_pwm_config,
139 	.set_polarity = gpio_pwm_set_polarity,
140 	.enable = gpio_pwm_enable,
141 	.disable = gpio_pwm_disable,
142 	.owner = THIS_MODULE,
143 };
144 
gpio_pwm_probe(struct platform_device * pdev)145 static int gpio_pwm_probe(struct platform_device *pdev)
146 {
147 	int ret;
148 	struct gpio_pwm_chip *pc;
149 
150 	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
151 	if (!pc)
152 		return -ENOMEM;
153 
154 	pc->chip.dev = &pdev->dev;
155 	pc->chip.ops = &gpio_pwm_ops;
156 	pc->chip.base = -1;
157 	pc->chip.npwm = 1;
158 	pc->chip.of_xlate = of_pwm_xlate_with_flags;
159 	pc->chip.of_pwm_n_cells = 3;
160 
161 	pc->gpiod = devm_gpiod_get(&pdev->dev, "pwm", GPIOD_OUT_LOW);
162 
163 	if (IS_ERR(pc->gpiod))
164 		return PTR_ERR(pc->gpiod);
165 
166 	hrtimer_init(&pc->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
167 	pc->timer.function = &gpio_pwm_timer;
168 	pc->pin_on = false;
169 
170 	if (!hrtimer_is_hres_active(&pc->timer))
171 		dev_warn(&pdev->dev, "HR timer unavailable, restricting to "
172 				     "low resolution\n");
173 
174 	ret = pwmchip_add(&pc->chip);
175 	if (ret < 0) {
176 		dev_err(&pdev->dev, "failed to add PWM chip: %d\n", ret);
177 		return ret;
178 	}
179 
180 	platform_set_drvdata(pdev, pc);
181 	return 0;
182 }
183 
gpio_pwm_remove(struct platform_device * pdev)184 static int gpio_pwm_remove(struct platform_device *pdev)
185 {
186 	struct gpio_pwm_chip *pc = platform_get_drvdata(pdev);
187 
188 	hrtimer_cancel(&pc->timer);
189 	return pwmchip_remove(&pc->chip);
190 }
191 
192 #ifdef CONFIG_OF
193 static const struct of_device_id gpio_pwm_of_match[] = {
194 	{ .compatible = "pwm-gpio", },
195 	{ }
196 };
197 MODULE_DEVICE_TABLE(of, gpio_pwm_of_match);
198 #endif
199 
200 static struct platform_driver gpio_pwm_driver = {
201 	.probe = gpio_pwm_probe,
202 	.remove = gpio_pwm_remove,
203 	.driver = {
204 		.name = "pwm-gpio",
205 		.of_match_table = of_match_ptr(gpio_pwm_of_match),
206 	},
207 };
208 module_platform_driver(gpio_pwm_driver);
209 
210 MODULE_AUTHOR("Olliver Schinagl <oliver@schinagl.nl>");
211 MODULE_DESCRIPTION("Generic GPIO bit-banged PWM driver");
212 MODULE_LICENSE("GPL");
213