aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pwm/pwm-pca9685.c
diff options
context:
space:
mode:
authorThierry Reding <thierry.reding@gmail.com>2017-02-10 09:15:56 -0500
committerThierry Reding <thierry.reding@gmail.com>2017-02-10 09:15:56 -0500
commit38b0a526ec33314ee1d9926e3a347078f63eac8e (patch)
tree7deea94e326a36567ccdf9a35188657305d1b5cc /drivers/pwm/pwm-pca9685.c
parent776906ff0350de74441a2e2387a2a69f55f0f71c (diff)
parent326ed314fefebb259563926c8c6110a009562e07 (diff)
Merge branch 'for-4.11/drivers' into for-next
Diffstat (limited to 'drivers/pwm/pwm-pca9685.c')
-rw-r--r--drivers/pwm/pwm-pca9685.c175
1 files changed, 163 insertions, 12 deletions
diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c
index c8282a2650be..0cfb3571a732 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>
@@ -65,7 +67,6 @@
65#define PCA9685_MAXCHAN 0x10 67#define PCA9685_MAXCHAN 0x10
66 68
67#define LED_FULL (1 << 4) 69#define LED_FULL (1 << 4)
68#define MODE1_RESTART (1 << 7)
69#define MODE1_SLEEP (1 << 4) 70#define MODE1_SLEEP (1 << 4)
70#define MODE2_INVRT (1 << 4) 71#define MODE2_INVRT (1 << 4)
71#define MODE2_OUTDRV (1 << 2) 72#define MODE2_OUTDRV (1 << 2)
@@ -81,6 +82,10 @@ struct pca9685 {
81 int active_cnt; 82 int active_cnt;
82 int duty_ns; 83 int duty_ns;
83 int period_ns; 84 int period_ns;
85#if IS_ENABLED(CONFIG_GPIOLIB)
86 struct mutex lock;
87 struct gpio_chip gpio;
88#endif
84}; 89};
85 90
86static inline struct pca9685 *to_pca(struct pwm_chip *chip) 91static inline struct pca9685 *to_pca(struct pwm_chip *chip)
@@ -88,6 +93,151 @@ static inline struct pca9685 *to_pca(struct pwm_chip *chip)
88 return container_of(chip, struct pca9685, chip); 93 return container_of(chip, struct pca9685, chip);
89} 94}
90 95
96#if IS_ENABLED(CONFIG_GPIOLIB)
97static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
98{
99 struct pca9685 *pca = gpiochip_get_data(gpio);
100 struct pwm_device *pwm;
101
102 mutex_lock(&pca->lock);
103
104 pwm = &pca->chip.pwms[offset];
105
106 if (pwm->flags & (PWMF_REQUESTED | PWMF_EXPORTED)) {
107 mutex_unlock(&pca->lock);
108 return -EBUSY;
109 }
110
111 pwm_set_chip_data(pwm, (void *)1);
112
113 mutex_unlock(&pca->lock);
114 return 0;
115}
116
117static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
118{
119 struct pca9685 *pca = gpiochip_get_data(gpio);
120 struct pwm_device *pwm;
121
122 mutex_lock(&pca->lock);
123 pwm = &pca->chip.pwms[offset];
124 pwm_set_chip_data(pwm, NULL);
125 mutex_unlock(&pca->lock);
126}
127
128static bool pca9685_pwm_is_gpio(struct pca9685 *pca, struct pwm_device *pwm)
129{
130 bool is_gpio = false;
131
132 mutex_lock(&pca->lock);
133
134 if (pwm->hwpwm >= PCA9685_MAXCHAN) {
135 unsigned int i;
136
137 /*
138 * Check if any of the GPIOs are requested and in that case
139 * prevent using the "all LEDs" channel.
140 */
141 for (i = 0; i < pca->gpio.ngpio; i++)
142 if (gpiochip_is_requested(&pca->gpio, i)) {
143 is_gpio = true;
144 break;
145 }
146 } else if (pwm_get_chip_data(pwm)) {
147 is_gpio = true;
148 }
149
150 mutex_unlock(&pca->lock);
151 return is_gpio;
152}
153
154static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
155{
156 struct pca9685 *pca = gpiochip_get_data(gpio);
157 struct pwm_device *pwm = &pca->chip.pwms[offset];
158 unsigned int value;
159
160 regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
161
162 return value & LED_FULL;
163}
164
165static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
166 int value)
167{
168 struct pca9685 *pca = gpiochip_get_data(gpio);
169 struct pwm_device *pwm = &pca->chip.pwms[offset];
170 unsigned int on = value ? LED_FULL : 0;
171
172 /* Clear both OFF registers */
173 regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0);
174 regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0);
175
176 /* Set the full ON bit */
177 regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on);
178}
179
180static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
181 unsigned int offset)
182{
183 /* Always out */
184 return 0;
185}
186
187static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
188 unsigned int offset)
189{
190 return -EINVAL;
191}
192
193static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
194 unsigned int offset, int value)
195{
196 pca9685_pwm_gpio_set(gpio, offset, value);
197
198 return 0;
199}
200
201/*
202 * The PCA9685 has a bit for turning the PWM output full off or on. Some
203 * boards like Intel Galileo actually uses these as normal GPIOs so we
204 * expose a GPIO chip here which can exclusively take over the underlying
205 * PWM channel.
206 */
207static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
208{
209 struct device *dev = pca->chip.dev;
210
211 mutex_init(&pca->lock);
212
213 pca->gpio.label = dev_name(dev);
214 pca->gpio.parent = dev;
215 pca->gpio.request = pca9685_pwm_gpio_request;
216 pca->gpio.free = pca9685_pwm_gpio_free;
217 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
218 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
219 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
220 pca->gpio.get = pca9685_pwm_gpio_get;
221 pca->gpio.set = pca9685_pwm_gpio_set;
222 pca->gpio.base = -1;
223 pca->gpio.ngpio = PCA9685_MAXCHAN;
224 pca->gpio.can_sleep = true;
225
226 return devm_gpiochip_add_data(dev, &pca->gpio, pca);
227}
228#else
229static inline bool pca9685_pwm_is_gpio(struct pca9685 *pca,
230 struct pwm_device *pwm)
231{
232 return false;
233}
234
235static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
236{
237 return 0;
238}
239#endif
240
91static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm, 241static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
92 int duty_ns, int period_ns) 242 int duty_ns, int period_ns)
93{ 243{
@@ -117,16 +267,6 @@ static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
117 udelay(500); 267 udelay(500);
118 268
119 pca->period_ns = period_ns; 269 pca->period_ns = period_ns;
120
121 /*
122 * If the duty cycle did not change, restart PWM with
123 * the same duty cycle to period ratio and return.
124 */
125 if (duty_ns == pca->duty_ns) {
126 regmap_update_bits(pca->regmap, PCA9685_MODE1,
127 MODE1_RESTART, 0x1);
128 return 0;
129 }
130 } else { 270 } else {
131 dev_err(chip->dev, 271 dev_err(chip->dev,
132 "prescaler not set: period out of bounds!\n"); 272 "prescaler not set: period out of bounds!\n");
@@ -264,6 +404,9 @@ static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
264{ 404{
265 struct pca9685 *pca = to_pca(chip); 405 struct pca9685 *pca = to_pca(chip);
266 406
407 if (pca9685_pwm_is_gpio(pca, pwm))
408 return -EBUSY;
409
267 if (pca->active_cnt++ == 0) 410 if (pca->active_cnt++ == 0)
268 return regmap_update_bits(pca->regmap, PCA9685_MODE1, 411 return regmap_update_bits(pca->regmap, PCA9685_MODE1,
269 MODE1_SLEEP, 0x0); 412 MODE1_SLEEP, 0x0);
@@ -344,7 +487,15 @@ static int pca9685_pwm_probe(struct i2c_client *client,
344 pca->chip.dev = &client->dev; 487 pca->chip.dev = &client->dev;
345 pca->chip.base = -1; 488 pca->chip.base = -1;
346 489
347 return pwmchip_add(&pca->chip); 490 ret = pwmchip_add(&pca->chip);
491 if (ret < 0)
492 return ret;
493
494 ret = pca9685_pwm_gpio_probe(pca);
495 if (ret < 0)
496 pwmchip_remove(&pca->chip);
497
498 return ret;
348} 499}
349 500
350static int pca9685_pwm_remove(struct i2c_client *client) 501static int pca9685_pwm_remove(struct i2c_client *client)