Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / pwm / pwm-pca9685.c
blob4a55dc18656c51ce876cd4e086e78d26c11fd4e5
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Driver for PCA9685 16-channel 12-bit PWM LED controller
5 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
6 * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
8 * based on the pwm-twl-led.c driver
9 */
11 #include <linux/acpi.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/platform_device.h>
17 #include <linux/property.h>
18 #include <linux/pwm.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/bitmap.h>
26 * Because the PCA9685 has only one prescaler per chip, changing the period of
27 * one channel affects the period of all 16 PWM outputs!
28 * However, the ratio between each configured duty cycle and the chip-wide
29 * period remains constant, because the OFF time is set in proportion to the
30 * counter range.
33 #define PCA9685_MODE1 0x00
34 #define PCA9685_MODE2 0x01
35 #define PCA9685_SUBADDR1 0x02
36 #define PCA9685_SUBADDR2 0x03
37 #define PCA9685_SUBADDR3 0x04
38 #define PCA9685_ALLCALLADDR 0x05
39 #define PCA9685_LEDX_ON_L 0x06
40 #define PCA9685_LEDX_ON_H 0x07
41 #define PCA9685_LEDX_OFF_L 0x08
42 #define PCA9685_LEDX_OFF_H 0x09
44 #define PCA9685_ALL_LED_ON_L 0xFA
45 #define PCA9685_ALL_LED_ON_H 0xFB
46 #define PCA9685_ALL_LED_OFF_L 0xFC
47 #define PCA9685_ALL_LED_OFF_H 0xFD
48 #define PCA9685_PRESCALE 0xFE
50 #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
51 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
53 #define PCA9685_COUNTER_RANGE 4096
54 #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */
55 #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
57 #define PCA9685_NUMREGS 0xFF
58 #define PCA9685_MAXCHAN 0x10
60 #define LED_FULL BIT(4)
61 #define MODE1_ALLCALL BIT(0)
62 #define MODE1_SUB3 BIT(1)
63 #define MODE1_SUB2 BIT(2)
64 #define MODE1_SUB1 BIT(3)
65 #define MODE1_SLEEP BIT(4)
66 #define MODE2_INVRT BIT(4)
67 #define MODE2_OUTDRV BIT(2)
69 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
70 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
71 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
72 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
74 struct pca9685 {
75 struct pwm_chip chip;
76 struct regmap *regmap;
77 int period_ns;
78 #if IS_ENABLED(CONFIG_GPIOLIB)
79 struct mutex lock;
80 struct gpio_chip gpio;
81 DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
82 #endif
85 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
87 return container_of(chip, struct pca9685, chip);
90 #if IS_ENABLED(CONFIG_GPIOLIB)
91 static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
93 bool is_inuse;
95 mutex_lock(&pca->lock);
96 if (pwm_idx >= PCA9685_MAXCHAN) {
98 * "All LEDs" channel:
99 * pretend already in use if any of the PWMs are requested
101 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
102 is_inuse = true;
103 goto out;
105 } else {
107 * Regular channel:
108 * pretend already in use if the "all LEDs" channel is requested
110 if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
111 is_inuse = true;
112 goto out;
115 is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
116 out:
117 mutex_unlock(&pca->lock);
118 return is_inuse;
121 static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
123 mutex_lock(&pca->lock);
124 clear_bit(pwm_idx, pca->pwms_inuse);
125 mutex_unlock(&pca->lock);
128 static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
130 struct pca9685 *pca = gpiochip_get_data(gpio);
132 if (pca9685_pwm_test_and_set_inuse(pca, offset))
133 return -EBUSY;
134 pm_runtime_get_sync(pca->chip.dev);
135 return 0;
138 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
140 struct pca9685 *pca = gpiochip_get_data(gpio);
141 struct pwm_device *pwm = &pca->chip.pwms[offset];
142 unsigned int value;
144 regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
146 return value & LED_FULL;
149 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
150 int value)
152 struct pca9685 *pca = gpiochip_get_data(gpio);
153 struct pwm_device *pwm = &pca->chip.pwms[offset];
154 unsigned int on = value ? LED_FULL : 0;
156 /* Clear both OFF registers */
157 regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0);
158 regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0);
160 /* Set the full ON bit */
161 regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on);
164 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
166 struct pca9685 *pca = gpiochip_get_data(gpio);
168 pca9685_pwm_gpio_set(gpio, offset, 0);
169 pm_runtime_put(pca->chip.dev);
170 pca9685_pwm_clear_inuse(pca, offset);
173 static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
174 unsigned int offset)
176 /* Always out */
177 return GPIO_LINE_DIRECTION_OUT;
180 static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
181 unsigned int offset)
183 return -EINVAL;
186 static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
187 unsigned int offset, int value)
189 pca9685_pwm_gpio_set(gpio, offset, value);
191 return 0;
195 * The PCA9685 has a bit for turning the PWM output full off or on. Some
196 * boards like Intel Galileo actually uses these as normal GPIOs so we
197 * expose a GPIO chip here which can exclusively take over the underlying
198 * PWM channel.
200 static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
202 struct device *dev = pca->chip.dev;
204 mutex_init(&pca->lock);
206 pca->gpio.label = dev_name(dev);
207 pca->gpio.parent = dev;
208 pca->gpio.request = pca9685_pwm_gpio_request;
209 pca->gpio.free = pca9685_pwm_gpio_free;
210 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
211 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
212 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
213 pca->gpio.get = pca9685_pwm_gpio_get;
214 pca->gpio.set = pca9685_pwm_gpio_set;
215 pca->gpio.base = -1;
216 pca->gpio.ngpio = PCA9685_MAXCHAN;
217 pca->gpio.can_sleep = true;
219 return devm_gpiochip_add_data(dev, &pca->gpio, pca);
221 #else
222 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
223 int pwm_idx)
225 return false;
228 static inline void
229 pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
233 static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
235 return 0;
237 #endif
239 static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
241 regmap_update_bits(pca->regmap, PCA9685_MODE1,
242 MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
243 if (!enable) {
244 /* Wait 500us for the oscillator to be back up */
245 udelay(500);
249 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
250 int duty_ns, int period_ns)
252 struct pca9685 *pca = to_pca(chip);
253 unsigned long long duty;
254 unsigned int reg;
255 int prescale;
257 if (period_ns != pca->period_ns) {
258 prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
259 PCA9685_COUNTER_RANGE * 1000) - 1;
261 if (prescale >= PCA9685_PRESCALE_MIN &&
262 prescale <= PCA9685_PRESCALE_MAX) {
264 * Putting the chip briefly into SLEEP mode
265 * at this point won't interfere with the
266 * pm_runtime framework, because the pm_runtime
267 * state is guaranteed active here.
269 /* Put chip into sleep mode */
270 pca9685_set_sleep_mode(pca, true);
272 /* Change the chip-wide output frequency */
273 regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
275 /* Wake the chip up */
276 pca9685_set_sleep_mode(pca, false);
278 pca->period_ns = period_ns;
279 } else {
280 dev_err(chip->dev,
281 "prescaler not set: period out of bounds!\n");
282 return -EINVAL;
286 if (duty_ns < 1) {
287 if (pwm->hwpwm >= PCA9685_MAXCHAN)
288 reg = PCA9685_ALL_LED_OFF_H;
289 else
290 reg = LED_N_OFF_H(pwm->hwpwm);
292 regmap_write(pca->regmap, reg, LED_FULL);
294 return 0;
297 if (duty_ns == period_ns) {
298 /* Clear both OFF registers */
299 if (pwm->hwpwm >= PCA9685_MAXCHAN)
300 reg = PCA9685_ALL_LED_OFF_L;
301 else
302 reg = LED_N_OFF_L(pwm->hwpwm);
304 regmap_write(pca->regmap, reg, 0x0);
306 if (pwm->hwpwm >= PCA9685_MAXCHAN)
307 reg = PCA9685_ALL_LED_OFF_H;
308 else
309 reg = LED_N_OFF_H(pwm->hwpwm);
311 regmap_write(pca->regmap, reg, 0x0);
313 /* Set the full ON bit */
314 if (pwm->hwpwm >= PCA9685_MAXCHAN)
315 reg = PCA9685_ALL_LED_ON_H;
316 else
317 reg = LED_N_ON_H(pwm->hwpwm);
319 regmap_write(pca->regmap, reg, LED_FULL);
321 return 0;
324 duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
325 duty = DIV_ROUND_UP_ULL(duty, period_ns);
327 if (pwm->hwpwm >= PCA9685_MAXCHAN)
328 reg = PCA9685_ALL_LED_OFF_L;
329 else
330 reg = LED_N_OFF_L(pwm->hwpwm);
332 regmap_write(pca->regmap, reg, (int)duty & 0xff);
334 if (pwm->hwpwm >= PCA9685_MAXCHAN)
335 reg = PCA9685_ALL_LED_OFF_H;
336 else
337 reg = LED_N_OFF_H(pwm->hwpwm);
339 regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
341 /* Clear the full ON bit, otherwise the set OFF time has no effect */
342 if (pwm->hwpwm >= PCA9685_MAXCHAN)
343 reg = PCA9685_ALL_LED_ON_H;
344 else
345 reg = LED_N_ON_H(pwm->hwpwm);
347 regmap_write(pca->regmap, reg, 0);
349 return 0;
352 static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
354 struct pca9685 *pca = to_pca(chip);
355 unsigned int reg;
358 * The PWM subsystem does not support a pre-delay.
359 * So, set the ON-timeout to 0
361 if (pwm->hwpwm >= PCA9685_MAXCHAN)
362 reg = PCA9685_ALL_LED_ON_L;
363 else
364 reg = LED_N_ON_L(pwm->hwpwm);
366 regmap_write(pca->regmap, reg, 0);
368 if (pwm->hwpwm >= PCA9685_MAXCHAN)
369 reg = PCA9685_ALL_LED_ON_H;
370 else
371 reg = LED_N_ON_H(pwm->hwpwm);
373 regmap_write(pca->regmap, reg, 0);
376 * Clear the full-off bit.
377 * It has precedence over the others and must be off.
379 if (pwm->hwpwm >= PCA9685_MAXCHAN)
380 reg = PCA9685_ALL_LED_OFF_H;
381 else
382 reg = LED_N_OFF_H(pwm->hwpwm);
384 regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
386 return 0;
389 static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
391 struct pca9685 *pca = to_pca(chip);
392 unsigned int reg;
394 if (pwm->hwpwm >= PCA9685_MAXCHAN)
395 reg = PCA9685_ALL_LED_OFF_H;
396 else
397 reg = LED_N_OFF_H(pwm->hwpwm);
399 regmap_write(pca->regmap, reg, LED_FULL);
401 /* Clear the LED_OFF counter. */
402 if (pwm->hwpwm >= PCA9685_MAXCHAN)
403 reg = PCA9685_ALL_LED_OFF_L;
404 else
405 reg = LED_N_OFF_L(pwm->hwpwm);
407 regmap_write(pca->regmap, reg, 0x0);
410 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
412 struct pca9685 *pca = to_pca(chip);
414 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
415 return -EBUSY;
416 pm_runtime_get_sync(chip->dev);
418 return 0;
421 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
423 struct pca9685 *pca = to_pca(chip);
425 pca9685_pwm_disable(chip, pwm);
426 pm_runtime_put(chip->dev);
427 pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
430 static const struct pwm_ops pca9685_pwm_ops = {
431 .enable = pca9685_pwm_enable,
432 .disable = pca9685_pwm_disable,
433 .config = pca9685_pwm_config,
434 .request = pca9685_pwm_request,
435 .free = pca9685_pwm_free,
436 .owner = THIS_MODULE,
439 static const struct regmap_config pca9685_regmap_i2c_config = {
440 .reg_bits = 8,
441 .val_bits = 8,
442 .max_register = PCA9685_NUMREGS,
443 .cache_type = REGCACHE_NONE,
446 static int pca9685_pwm_probe(struct i2c_client *client,
447 const struct i2c_device_id *id)
449 struct pca9685 *pca;
450 unsigned int reg;
451 int ret;
453 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
454 if (!pca)
455 return -ENOMEM;
457 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
458 if (IS_ERR(pca->regmap)) {
459 ret = PTR_ERR(pca->regmap);
460 dev_err(&client->dev, "Failed to initialize register map: %d\n",
461 ret);
462 return ret;
464 pca->period_ns = PCA9685_DEFAULT_PERIOD;
466 i2c_set_clientdata(client, pca);
468 regmap_read(pca->regmap, PCA9685_MODE2, &reg);
470 if (device_property_read_bool(&client->dev, "invert"))
471 reg |= MODE2_INVRT;
472 else
473 reg &= ~MODE2_INVRT;
475 if (device_property_read_bool(&client->dev, "open-drain"))
476 reg &= ~MODE2_OUTDRV;
477 else
478 reg |= MODE2_OUTDRV;
480 regmap_write(pca->regmap, PCA9685_MODE2, reg);
482 /* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */
483 regmap_read(pca->regmap, PCA9685_MODE1, &reg);
484 reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3);
485 regmap_write(pca->regmap, PCA9685_MODE1, reg);
487 /* Clear all "full off" bits */
488 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
489 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
491 pca->chip.ops = &pca9685_pwm_ops;
492 /* Add an extra channel for ALL_LED */
493 pca->chip.npwm = PCA9685_MAXCHAN + 1;
495 pca->chip.dev = &client->dev;
496 pca->chip.base = -1;
498 ret = pwmchip_add(&pca->chip);
499 if (ret < 0)
500 return ret;
502 ret = pca9685_pwm_gpio_probe(pca);
503 if (ret < 0) {
504 pwmchip_remove(&pca->chip);
505 return ret;
508 /* The chip comes out of power-up in the active state */
509 pm_runtime_set_active(&client->dev);
511 * Enable will put the chip into suspend, which is what we
512 * want as all outputs are disabled at this point
514 pm_runtime_enable(&client->dev);
516 return 0;
519 static int pca9685_pwm_remove(struct i2c_client *client)
521 struct pca9685 *pca = i2c_get_clientdata(client);
522 int ret;
524 ret = pwmchip_remove(&pca->chip);
525 if (ret)
526 return ret;
527 pm_runtime_disable(&client->dev);
528 return 0;
531 static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev)
533 struct i2c_client *client = to_i2c_client(dev);
534 struct pca9685 *pca = i2c_get_clientdata(client);
536 pca9685_set_sleep_mode(pca, true);
537 return 0;
540 static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev)
542 struct i2c_client *client = to_i2c_client(dev);
543 struct pca9685 *pca = i2c_get_clientdata(client);
545 pca9685_set_sleep_mode(pca, false);
546 return 0;
549 static const struct i2c_device_id pca9685_id[] = {
550 { "pca9685", 0 },
551 { /* sentinel */ },
553 MODULE_DEVICE_TABLE(i2c, pca9685_id);
555 #ifdef CONFIG_ACPI
556 static const struct acpi_device_id pca9685_acpi_ids[] = {
557 { "INT3492", 0 },
558 { /* sentinel */ },
560 MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
561 #endif
563 #ifdef CONFIG_OF
564 static const struct of_device_id pca9685_dt_ids[] = {
565 { .compatible = "nxp,pca9685-pwm", },
566 { /* sentinel */ }
568 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
569 #endif
571 static const struct dev_pm_ops pca9685_pwm_pm = {
572 SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
573 pca9685_pwm_runtime_resume, NULL)
576 static struct i2c_driver pca9685_i2c_driver = {
577 .driver = {
578 .name = "pca9685-pwm",
579 .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
580 .of_match_table = of_match_ptr(pca9685_dt_ids),
581 .pm = &pca9685_pwm_pm,
583 .probe = pca9685_pwm_probe,
584 .remove = pca9685_pwm_remove,
585 .id_table = pca9685_id,
588 module_i2c_driver(pca9685_i2c_driver);
590 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
591 MODULE_DESCRIPTION("PWM driver for PCA9685");
592 MODULE_LICENSE("GPL");