aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMika Westerberg <mika.westerberg@linux.intel.com>2016-09-20 10:40:56 -0400
committerThierry Reding <thierry.reding@gmail.com>2017-01-18 18:38:16 -0500
commitbccec89f0a35f65302734d1cdb01479df0f33ac9 (patch)
tree9c29def1d3a26a2d1ddb5b4b77f30221ff5187f0
parent0c744ea4f77d72b3dcebb7a8f2684633ec79be88 (diff)
pwm: pca9685: Allow any of the 16 PWMs to be used as a GPIO
The PCA9685 controller has full on/off bit for each PWM channel. Setting this bit bypasses the PWM control and the line works just as it would be a GPIO. Furthermore in Intel Galileo it is actually used as GPIO output for discreet muxes on the board. This patch adds GPIO output only support for the driver so that we can control the muxes on Galileo using standard GPIO interfaces available in the kernel. GPIO and PWM functionality is exclusive so only one can be active at a time on a single PWM channel. Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com> Acked-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Thierry Reding <thierry.reding@gmail.com>
-rw-r--r--drivers/pwm/pwm-pca9685.c164
1 files changed, 163 insertions, 1 deletions
diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c
index 117fccf7934a..621656b9aa41 100644
--- a/drivers/pwm/pwm-pca9685.c
+++ b/drivers/pwm/pwm-pca9685.c
@@ -20,8 +20,10 @@
20 */ 20 */
21 21
22#include <linux/acpi.h> 22#include <linux/acpi.h>
23#include <linux/gpio/driver.h>
23#include <linux/i2c.h> 24#include <linux/i2c.h>
24#include <linux/module.h> 25#include <linux/module.h>
26#include <linux/mutex.h>
25#include <linux/platform_device.h> 27#include <linux/platform_device.h>
26#include <linux/property.h> 28#include <linux/property.h>
27#include <linux/pwm.h> 29#include <linux/pwm.h>
@@ -81,6 +83,10 @@ struct pca9685 {
81 int active_cnt; 83 int active_cnt;
82 int duty_ns; 84 int duty_ns;
83 int period_ns; 85 int period_ns;
86#if IS_ENABLED(CONFIG_GPIOLIB)
87 struct mutex lock;
88 struct gpio_chip gpio;
89#endif
84}; 90};
85 91
86static inline struct pca9685 *to_pca(struct pwm_chip *chip) 92static inline struct pca9685 *to_pca(struct pwm_chip *chip)
@@ -88,6 +94,151 @@ static inline struct pca9685 *to_pca(struct pwm_chip *chip)
88 return container_of(chip, struct pca9685, chip); 94 return container_of(chip, struct pca9685, chip);
89} 95}
90 96
97#if IS_ENABLED(CONFIG_GPIOLIB)
98static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
99{
100 struct pca9685 *pca = gpiochip_get_data(gpio);
101 struct pwm_device *pwm;
102
103 mutex_lock(&pca->lock);
104
105 pwm = &pca->chip.pwms[offset];
106
107 if (pwm->flags & (PWMF_REQUESTED | PWMF_EXPORTED)) {
108 mutex_unlock(&pca->lock);
109 return -EBUSY;
110 }
111
112 pwm_set_chip_data(pwm, (void *)1);
113
114 mutex_unlock(&pca->lock);
115 return 0;
116}
117
118static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
119{
120 struct pca9685 *pca = gpiochip_get_data(gpio);
121 struct pwm_device *pwm;
122
123 mutex_lock(&pca->lock);
124 pwm = &pca->chip.pwms[offset];
125 pwm_set_chip_data(pwm, NULL);
126 mutex_unlock(&pca->lock);
127}
128
129static bool pca9685_pwm_is_gpio(struct pca9685 *pca, struct pwm_device *pwm)
130{
131 bool is_gpio = false;
132
133 mutex_lock(&pca->lock);
134
135 if (pwm->hwpwm >= PCA9685_MAXCHAN) {
136 unsigned int i;
137
138 /*
139 * Check if any of the GPIOs are requested and in that case
140 * prevent using the "all LEDs" channel.
141 */
142 for (i = 0; i < pca->gpio.ngpio; i++)
143 if (gpiochip_is_requested(&pca->gpio, i)) {
144 is_gpio = true;
145 break;
146 }
147 } else if (pwm_get_chip_data(pwm)) {
148 is_gpio = true;
149 }
150
151 mutex_unlock(&pca->lock);
152 return is_gpio;
153}
154
155static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
156{
157 struct pca9685 *pca = gpiochip_get_data(gpio);
158 struct pwm_device *pwm = &pca->chip.pwms[offset];
159 unsigned int value;
160
161 regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
162
163 return value & LED_FULL;
164}
165
166static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
167 int value)
168{
169 struct pca9685 *pca = gpiochip_get_data(gpio);
170 struct pwm_device *pwm = &pca->chip.pwms[offset];
171 unsigned int on = value ? LED_FULL : 0;
172
173 /* Clear both OFF registers */
174 regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0);
175 regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0);
176
177 /* Set the full ON bit */
178 regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on);
179}
180
181static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
182 unsigned int offset)
183{
184 /* Always out */
185 return 0;
186}
187
188static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
189 unsigned int offset)
190{
191 return -EINVAL;
192}
193
194static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
195 unsigned int offset, int value)
196{
197 pca9685_pwm_gpio_set(gpio, offset, value);
198
199 return 0;
200}
201
202/*
203 * The PCA9685 has a bit for turning the PWM output full off or on. Some
204 * boards like Intel Galileo actually uses these as normal GPIOs so we
205 * expose a GPIO chip here which can exclusively take over the underlying
206 * PWM channel.
207 */
208static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
209{
210 struct device *dev = pca->chip.dev;
211
212 mutex_init(&pca->lock);
213
214 pca->gpio.label = dev_name(dev);
215 pca->gpio.parent = dev;
216 pca->gpio.request = pca9685_pwm_gpio_request;
217 pca->gpio.free = pca9685_pwm_gpio_free;
218 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
219 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
220 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
221 pca->gpio.get = pca9685_pwm_gpio_get;
222 pca->gpio.set = pca9685_pwm_gpio_set;
223 pca->gpio.base = -1;
224 pca->gpio.ngpio = PCA9685_MAXCHAN;
225 pca->gpio.can_sleep = true;
226
227 return devm_gpiochip_add_data(dev, &pca->gpio, pca);
228}
229#else
230static inline bool pca9685_pwm_is_gpio(struct pca9685 *pca,
231 struct pwm_device *pwm)
232{
233 return false;
234}
235
236static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
237{
238 return 0;
239}
240#endif
241
91static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 242static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
92 int duty_ns, int period_ns) 243 int duty_ns, int period_ns)
93{ 244{
@@ -264,6 +415,9 @@ static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
264{ 415{
265 struct pca9685 *pca = to_pca(chip); 416 struct pca9685 *pca = to_pca(chip);
266 417
418 if (pca9685_pwm_is_gpio(pca, pwm))
419 return -EBUSY;
420
267 if (pca->active_cnt++ == 0) 421 if (pca->active_cnt++ == 0)
268 return regmap_update_bits(pca->regmap, PCA9685_MODE1, 422 return regmap_update_bits(pca->regmap, PCA9685_MODE1,
269 MODE1_SLEEP, 0x0); 423 MODE1_SLEEP, 0x0);
@@ -345,7 +499,15 @@ static int pca9685_pwm_probe(struct i2c_client *client,
345 pca->chip.base = -1; 499 pca->chip.base = -1;
346 pca->chip.can_sleep = true; 500 pca->chip.can_sleep = true;
347 501
348 return pwmchip_add(&pca->chip); 502 ret = pwmchip_add(&pca->chip);
503 if (ret < 0)
504 return ret;
505
506 ret = pca9685_pwm_gpio_probe(pca);
507 if (ret < 0)
508 pwmchip_remove(&pca->chip);
509
510 return ret;
349} 511}
350 512
351static int pca9685_pwm_remove(struct i2c_client *client) 513static int pca9685_pwm_remove(struct i2c_client *client)