Linux 4.19.133
[linux/fpc-iii.git] / drivers / pwm / pwm-pca9685.c
blob259fd58812aed13f293c52105ec415a6f48ca12c
1 /*
2 * Driver for PCA9685 16-channel 12-bit PWM LED controller
4 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
5 * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
7 * based on the pwm-twl-led.c driver
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <linux/acpi.h>
23 #include <linux/gpio/driver.h>
24 #include <linux/i2c.h>
25 #include <linux/module.h>
26 #include <linux/mutex.h>
27 #include <linux/platform_device.h>
28 #include <linux/property.h>
29 #include <linux/pwm.h>
30 #include <linux/regmap.h>
31 #include <linux/slab.h>
32 #include <linux/delay.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/bitmap.h>
37 * Because the PCA9685 has only one prescaler per chip, changing the period of
38 * one channel affects the period of all 16 PWM outputs!
39 * However, the ratio between each configured duty cycle and the chip-wide
40 * period remains constant, because the OFF time is set in proportion to the
41 * counter range.
44 #define PCA9685_MODE1 0x00
45 #define PCA9685_MODE2 0x01
46 #define PCA9685_SUBADDR1 0x02
47 #define PCA9685_SUBADDR2 0x03
48 #define PCA9685_SUBADDR3 0x04
49 #define PCA9685_ALLCALLADDR 0x05
50 #define PCA9685_LEDX_ON_L 0x06
51 #define PCA9685_LEDX_ON_H 0x07
52 #define PCA9685_LEDX_OFF_L 0x08
53 #define PCA9685_LEDX_OFF_H 0x09
55 #define PCA9685_ALL_LED_ON_L 0xFA
56 #define PCA9685_ALL_LED_ON_H 0xFB
57 #define PCA9685_ALL_LED_OFF_L 0xFC
58 #define PCA9685_ALL_LED_OFF_H 0xFD
59 #define PCA9685_PRESCALE 0xFE
61 #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
62 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
64 #define PCA9685_COUNTER_RANGE 4096
65 #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */
66 #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
68 #define PCA9685_NUMREGS 0xFF
69 #define PCA9685_MAXCHAN 0x10
71 #define LED_FULL (1 << 4)
72 #define MODE1_SLEEP (1 << 4)
73 #define MODE2_INVRT (1 << 4)
74 #define MODE2_OUTDRV (1 << 2)
76 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
77 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
78 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
79 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
81 struct pca9685 {
82 struct pwm_chip chip;
83 struct regmap *regmap;
84 int duty_ns;
85 int period_ns;
86 #if IS_ENABLED(CONFIG_GPIOLIB)
87 struct mutex lock;
88 struct gpio_chip gpio;
89 DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
90 #endif
93 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
95 return container_of(chip, struct pca9685, chip);
98 #if IS_ENABLED(CONFIG_GPIOLIB)
99 static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
101 bool is_inuse;
103 mutex_lock(&pca->lock);
104 if (pwm_idx >= PCA9685_MAXCHAN) {
106 * "all LEDs" channel:
107 * pretend already in use if any of the PWMs are requested
109 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
110 is_inuse = true;
111 goto out;
113 } else {
115 * regular channel:
116 * pretend already in use if the "all LEDs" channel is requested
118 if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
119 is_inuse = true;
120 goto out;
123 is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
124 out:
125 mutex_unlock(&pca->lock);
126 return is_inuse;
129 static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
131 mutex_lock(&pca->lock);
132 clear_bit(pwm_idx, pca->pwms_inuse);
133 mutex_unlock(&pca->lock);
136 static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
138 struct pca9685 *pca = gpiochip_get_data(gpio);
140 if (pca9685_pwm_test_and_set_inuse(pca, offset))
141 return -EBUSY;
142 pm_runtime_get_sync(pca->chip.dev);
143 return 0;
146 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
148 struct pca9685 *pca = gpiochip_get_data(gpio);
149 struct pwm_device *pwm = &pca->chip.pwms[offset];
150 unsigned int value;
152 regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
154 return value & LED_FULL;
157 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
158 int value)
160 struct pca9685 *pca = gpiochip_get_data(gpio);
161 struct pwm_device *pwm = &pca->chip.pwms[offset];
162 unsigned int on = value ? LED_FULL : 0;
164 /* Clear both OFF registers */
165 regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0);
166 regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0);
168 /* Set the full ON bit */
169 regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on);
172 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
174 struct pca9685 *pca = gpiochip_get_data(gpio);
176 pca9685_pwm_gpio_set(gpio, offset, 0);
177 pm_runtime_put(pca->chip.dev);
178 pca9685_pwm_clear_inuse(pca, offset);
181 static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
182 unsigned int offset)
184 /* Always out */
185 return 0;
188 static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
189 unsigned int offset)
191 return -EINVAL;
194 static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
195 unsigned int offset, int value)
197 pca9685_pwm_gpio_set(gpio, offset, value);
199 return 0;
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.
208 static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
210 struct device *dev = pca->chip.dev;
212 mutex_init(&pca->lock);
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;
227 return devm_gpiochip_add_data(dev, &pca->gpio, pca);
229 #else
230 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
231 int pwm_idx)
233 return false;
236 static inline void
237 pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
241 static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
243 return 0;
245 #endif
247 static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
249 regmap_update_bits(pca->regmap, PCA9685_MODE1,
250 MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
251 if (!enable) {
252 /* Wait 500us for the oscillator to be back up */
253 udelay(500);
257 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
258 int duty_ns, int period_ns)
260 struct pca9685 *pca = to_pca(chip);
261 unsigned long long duty;
262 unsigned int reg;
263 int prescale;
265 if (period_ns != pca->period_ns) {
266 prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
267 PCA9685_COUNTER_RANGE * 1000) - 1;
269 if (prescale >= PCA9685_PRESCALE_MIN &&
270 prescale <= PCA9685_PRESCALE_MAX) {
272 * putting the chip briefly into SLEEP mode
273 * at this point won't interfere with the
274 * pm_runtime framework, because the pm_runtime
275 * state is guaranteed active here.
277 /* Put chip into sleep mode */
278 pca9685_set_sleep_mode(pca, true);
280 /* Change the chip-wide output frequency */
281 regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
283 /* Wake the chip up */
284 pca9685_set_sleep_mode(pca, false);
286 pca->period_ns = period_ns;
287 } else {
288 dev_err(chip->dev,
289 "prescaler not set: period out of bounds!\n");
290 return -EINVAL;
294 pca->duty_ns = duty_ns;
296 if (duty_ns < 1) {
297 if (pwm->hwpwm >= PCA9685_MAXCHAN)
298 reg = PCA9685_ALL_LED_OFF_H;
299 else
300 reg = LED_N_OFF_H(pwm->hwpwm);
302 regmap_write(pca->regmap, reg, LED_FULL);
304 return 0;
307 if (duty_ns == period_ns) {
308 /* Clear both OFF registers */
309 if (pwm->hwpwm >= PCA9685_MAXCHAN)
310 reg = PCA9685_ALL_LED_OFF_L;
311 else
312 reg = LED_N_OFF_L(pwm->hwpwm);
314 regmap_write(pca->regmap, reg, 0x0);
316 if (pwm->hwpwm >= PCA9685_MAXCHAN)
317 reg = PCA9685_ALL_LED_OFF_H;
318 else
319 reg = LED_N_OFF_H(pwm->hwpwm);
321 regmap_write(pca->regmap, reg, 0x0);
323 /* Set the full ON bit */
324 if (pwm->hwpwm >= PCA9685_MAXCHAN)
325 reg = PCA9685_ALL_LED_ON_H;
326 else
327 reg = LED_N_ON_H(pwm->hwpwm);
329 regmap_write(pca->regmap, reg, LED_FULL);
331 return 0;
334 duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
335 duty = DIV_ROUND_UP_ULL(duty, period_ns);
337 if (pwm->hwpwm >= PCA9685_MAXCHAN)
338 reg = PCA9685_ALL_LED_OFF_L;
339 else
340 reg = LED_N_OFF_L(pwm->hwpwm);
342 regmap_write(pca->regmap, reg, (int)duty & 0xff);
344 if (pwm->hwpwm >= PCA9685_MAXCHAN)
345 reg = PCA9685_ALL_LED_OFF_H;
346 else
347 reg = LED_N_OFF_H(pwm->hwpwm);
349 regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
351 /* Clear the full ON bit, otherwise the set OFF time has no effect */
352 if (pwm->hwpwm >= PCA9685_MAXCHAN)
353 reg = PCA9685_ALL_LED_ON_H;
354 else
355 reg = LED_N_ON_H(pwm->hwpwm);
357 regmap_write(pca->regmap, reg, 0);
359 return 0;
362 static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
364 struct pca9685 *pca = to_pca(chip);
365 unsigned int reg;
368 * The PWM subsystem does not support a pre-delay.
369 * So, set the ON-timeout to 0
371 if (pwm->hwpwm >= PCA9685_MAXCHAN)
372 reg = PCA9685_ALL_LED_ON_L;
373 else
374 reg = LED_N_ON_L(pwm->hwpwm);
376 regmap_write(pca->regmap, reg, 0);
378 if (pwm->hwpwm >= PCA9685_MAXCHAN)
379 reg = PCA9685_ALL_LED_ON_H;
380 else
381 reg = LED_N_ON_H(pwm->hwpwm);
383 regmap_write(pca->regmap, reg, 0);
386 * Clear the full-off bit.
387 * It has precedence over the others and must be off.
389 if (pwm->hwpwm >= PCA9685_MAXCHAN)
390 reg = PCA9685_ALL_LED_OFF_H;
391 else
392 reg = LED_N_OFF_H(pwm->hwpwm);
394 regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
396 return 0;
399 static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
401 struct pca9685 *pca = to_pca(chip);
402 unsigned int reg;
404 if (pwm->hwpwm >= PCA9685_MAXCHAN)
405 reg = PCA9685_ALL_LED_OFF_H;
406 else
407 reg = LED_N_OFF_H(pwm->hwpwm);
409 regmap_write(pca->regmap, reg, LED_FULL);
411 /* Clear the LED_OFF counter. */
412 if (pwm->hwpwm >= PCA9685_MAXCHAN)
413 reg = PCA9685_ALL_LED_OFF_L;
414 else
415 reg = LED_N_OFF_L(pwm->hwpwm);
417 regmap_write(pca->regmap, reg, 0x0);
420 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
422 struct pca9685 *pca = to_pca(chip);
424 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
425 return -EBUSY;
426 pm_runtime_get_sync(chip->dev);
428 return 0;
431 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
433 struct pca9685 *pca = to_pca(chip);
435 pca9685_pwm_disable(chip, pwm);
436 pm_runtime_put(chip->dev);
437 pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
440 static const struct pwm_ops pca9685_pwm_ops = {
441 .enable = pca9685_pwm_enable,
442 .disable = pca9685_pwm_disable,
443 .config = pca9685_pwm_config,
444 .request = pca9685_pwm_request,
445 .free = pca9685_pwm_free,
446 .owner = THIS_MODULE,
449 static const struct regmap_config pca9685_regmap_i2c_config = {
450 .reg_bits = 8,
451 .val_bits = 8,
452 .max_register = PCA9685_NUMREGS,
453 .cache_type = REGCACHE_NONE,
456 static int pca9685_pwm_probe(struct i2c_client *client,
457 const struct i2c_device_id *id)
459 struct pca9685 *pca;
460 int ret;
461 int mode2;
463 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
464 if (!pca)
465 return -ENOMEM;
467 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
468 if (IS_ERR(pca->regmap)) {
469 ret = PTR_ERR(pca->regmap);
470 dev_err(&client->dev, "Failed to initialize register map: %d\n",
471 ret);
472 return ret;
474 pca->duty_ns = 0;
475 pca->period_ns = PCA9685_DEFAULT_PERIOD;
477 i2c_set_clientdata(client, pca);
479 regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
481 if (device_property_read_bool(&client->dev, "invert"))
482 mode2 |= MODE2_INVRT;
483 else
484 mode2 &= ~MODE2_INVRT;
486 if (device_property_read_bool(&client->dev, "open-drain"))
487 mode2 &= ~MODE2_OUTDRV;
488 else
489 mode2 |= MODE2_OUTDRV;
491 regmap_write(pca->regmap, PCA9685_MODE2, mode2);
493 /* clear all "full off" bits */
494 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
495 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
497 pca->chip.ops = &pca9685_pwm_ops;
498 /* add an extra channel for ALL_LED */
499 pca->chip.npwm = PCA9685_MAXCHAN + 1;
501 pca->chip.dev = &client->dev;
502 pca->chip.base = -1;
504 ret = pwmchip_add(&pca->chip);
505 if (ret < 0)
506 return ret;
508 ret = pca9685_pwm_gpio_probe(pca);
509 if (ret < 0) {
510 pwmchip_remove(&pca->chip);
511 return ret;
514 /* the chip comes out of power-up in the active state */
515 pm_runtime_set_active(&client->dev);
517 * enable will put the chip into suspend, which is what we
518 * want as all outputs are disabled at this point
520 pm_runtime_enable(&client->dev);
522 return 0;
525 static int pca9685_pwm_remove(struct i2c_client *client)
527 struct pca9685 *pca = i2c_get_clientdata(client);
528 int ret;
530 ret = pwmchip_remove(&pca->chip);
531 if (ret)
532 return ret;
533 pm_runtime_disable(&client->dev);
534 return 0;
537 #ifdef CONFIG_PM
538 static int pca9685_pwm_runtime_suspend(struct device *dev)
540 struct i2c_client *client = to_i2c_client(dev);
541 struct pca9685 *pca = i2c_get_clientdata(client);
543 pca9685_set_sleep_mode(pca, true);
544 return 0;
547 static int pca9685_pwm_runtime_resume(struct device *dev)
549 struct i2c_client *client = to_i2c_client(dev);
550 struct pca9685 *pca = i2c_get_clientdata(client);
552 pca9685_set_sleep_mode(pca, false);
553 return 0;
555 #endif
557 static const struct i2c_device_id pca9685_id[] = {
558 { "pca9685", 0 },
559 { /* sentinel */ },
561 MODULE_DEVICE_TABLE(i2c, pca9685_id);
563 #ifdef CONFIG_ACPI
564 static const struct acpi_device_id pca9685_acpi_ids[] = {
565 { "INT3492", 0 },
566 { /* sentinel */ },
568 MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
569 #endif
571 #ifdef CONFIG_OF
572 static const struct of_device_id pca9685_dt_ids[] = {
573 { .compatible = "nxp,pca9685-pwm", },
574 { /* sentinel */ }
576 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
577 #endif
579 static const struct dev_pm_ops pca9685_pwm_pm = {
580 SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
581 pca9685_pwm_runtime_resume, NULL)
584 static struct i2c_driver pca9685_i2c_driver = {
585 .driver = {
586 .name = "pca9685-pwm",
587 .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
588 .of_match_table = of_match_ptr(pca9685_dt_ids),
589 .pm = &pca9685_pwm_pm,
591 .probe = pca9685_pwm_probe,
592 .remove = pca9685_pwm_remove,
593 .id_table = pca9685_id,
596 module_i2c_driver(pca9685_i2c_driver);
598 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
599 MODULE_DESCRIPTION("PWM driver for PCA9685");
600 MODULE_LICENSE("GPL");