Merge branch 'work.regset' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux/fpc-iii.git] / drivers / pwm / pwm-pca9685.c
blob76cd22bd6614baaf22c3c13f6f9ee499aca9523f
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 (1 << 4)
61 #define MODE1_SLEEP (1 << 4)
62 #define MODE2_INVRT (1 << 4)
63 #define MODE2_OUTDRV (1 << 2)
65 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
66 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
67 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
68 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
70 struct pca9685 {
71 struct pwm_chip chip;
72 struct regmap *regmap;
73 int period_ns;
74 #if IS_ENABLED(CONFIG_GPIOLIB)
75 struct mutex lock;
76 struct gpio_chip gpio;
77 DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
78 #endif
81 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
83 return container_of(chip, struct pca9685, chip);
86 #if IS_ENABLED(CONFIG_GPIOLIB)
87 static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
89 bool is_inuse;
91 mutex_lock(&pca->lock);
92 if (pwm_idx >= PCA9685_MAXCHAN) {
94 * "all LEDs" channel:
95 * pretend already in use if any of the PWMs are requested
97 if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
98 is_inuse = true;
99 goto out;
101 } else {
103 * regular channel:
104 * pretend already in use if the "all LEDs" channel is requested
106 if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
107 is_inuse = true;
108 goto out;
111 is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
112 out:
113 mutex_unlock(&pca->lock);
114 return is_inuse;
117 static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
119 mutex_lock(&pca->lock);
120 clear_bit(pwm_idx, pca->pwms_inuse);
121 mutex_unlock(&pca->lock);
124 static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
126 struct pca9685 *pca = gpiochip_get_data(gpio);
128 if (pca9685_pwm_test_and_set_inuse(pca, offset))
129 return -EBUSY;
130 pm_runtime_get_sync(pca->chip.dev);
131 return 0;
134 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
136 struct pca9685 *pca = gpiochip_get_data(gpio);
137 struct pwm_device *pwm = &pca->chip.pwms[offset];
138 unsigned int value;
140 regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
142 return value & LED_FULL;
145 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
146 int value)
148 struct pca9685 *pca = gpiochip_get_data(gpio);
149 struct pwm_device *pwm = &pca->chip.pwms[offset];
150 unsigned int on = value ? LED_FULL : 0;
152 /* Clear both OFF registers */
153 regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0);
154 regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0);
156 /* Set the full ON bit */
157 regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on);
160 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
162 struct pca9685 *pca = gpiochip_get_data(gpio);
164 pca9685_pwm_gpio_set(gpio, offset, 0);
165 pm_runtime_put(pca->chip.dev);
166 pca9685_pwm_clear_inuse(pca, offset);
169 static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
170 unsigned int offset)
172 /* Always out */
173 return GPIO_LINE_DIRECTION_OUT;
176 static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
177 unsigned int offset)
179 return -EINVAL;
182 static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
183 unsigned int offset, int value)
185 pca9685_pwm_gpio_set(gpio, offset, value);
187 return 0;
191 * The PCA9685 has a bit for turning the PWM output full off or on. Some
192 * boards like Intel Galileo actually uses these as normal GPIOs so we
193 * expose a GPIO chip here which can exclusively take over the underlying
194 * PWM channel.
196 static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
198 struct device *dev = pca->chip.dev;
200 mutex_init(&pca->lock);
202 pca->gpio.label = dev_name(dev);
203 pca->gpio.parent = dev;
204 pca->gpio.request = pca9685_pwm_gpio_request;
205 pca->gpio.free = pca9685_pwm_gpio_free;
206 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
207 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
208 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
209 pca->gpio.get = pca9685_pwm_gpio_get;
210 pca->gpio.set = pca9685_pwm_gpio_set;
211 pca->gpio.base = -1;
212 pca->gpio.ngpio = PCA9685_MAXCHAN;
213 pca->gpio.can_sleep = true;
215 return devm_gpiochip_add_data(dev, &pca->gpio, pca);
217 #else
218 static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
219 int pwm_idx)
221 return false;
224 static inline void
225 pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
229 static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
231 return 0;
233 #endif
235 static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
237 regmap_update_bits(pca->regmap, PCA9685_MODE1,
238 MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
239 if (!enable) {
240 /* Wait 500us for the oscillator to be back up */
241 udelay(500);
245 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
246 int duty_ns, int period_ns)
248 struct pca9685 *pca = to_pca(chip);
249 unsigned long long duty;
250 unsigned int reg;
251 int prescale;
253 if (period_ns != pca->period_ns) {
254 prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
255 PCA9685_COUNTER_RANGE * 1000) - 1;
257 if (prescale >= PCA9685_PRESCALE_MIN &&
258 prescale <= PCA9685_PRESCALE_MAX) {
260 * putting the chip briefly into SLEEP mode
261 * at this point won't interfere with the
262 * pm_runtime framework, because the pm_runtime
263 * state is guaranteed active here.
265 /* Put chip into sleep mode */
266 pca9685_set_sleep_mode(pca, true);
268 /* Change the chip-wide output frequency */
269 regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
271 /* Wake the chip up */
272 pca9685_set_sleep_mode(pca, false);
274 pca->period_ns = period_ns;
275 } else {
276 dev_err(chip->dev,
277 "prescaler not set: period out of bounds!\n");
278 return -EINVAL;
282 if (duty_ns < 1) {
283 if (pwm->hwpwm >= PCA9685_MAXCHAN)
284 reg = PCA9685_ALL_LED_OFF_H;
285 else
286 reg = LED_N_OFF_H(pwm->hwpwm);
288 regmap_write(pca->regmap, reg, LED_FULL);
290 return 0;
293 if (duty_ns == period_ns) {
294 /* Clear both OFF registers */
295 if (pwm->hwpwm >= PCA9685_MAXCHAN)
296 reg = PCA9685_ALL_LED_OFF_L;
297 else
298 reg = LED_N_OFF_L(pwm->hwpwm);
300 regmap_write(pca->regmap, reg, 0x0);
302 if (pwm->hwpwm >= PCA9685_MAXCHAN)
303 reg = PCA9685_ALL_LED_OFF_H;
304 else
305 reg = LED_N_OFF_H(pwm->hwpwm);
307 regmap_write(pca->regmap, reg, 0x0);
309 /* Set the full ON bit */
310 if (pwm->hwpwm >= PCA9685_MAXCHAN)
311 reg = PCA9685_ALL_LED_ON_H;
312 else
313 reg = LED_N_ON_H(pwm->hwpwm);
315 regmap_write(pca->regmap, reg, LED_FULL);
317 return 0;
320 duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
321 duty = DIV_ROUND_UP_ULL(duty, period_ns);
323 if (pwm->hwpwm >= PCA9685_MAXCHAN)
324 reg = PCA9685_ALL_LED_OFF_L;
325 else
326 reg = LED_N_OFF_L(pwm->hwpwm);
328 regmap_write(pca->regmap, reg, (int)duty & 0xff);
330 if (pwm->hwpwm >= PCA9685_MAXCHAN)
331 reg = PCA9685_ALL_LED_OFF_H;
332 else
333 reg = LED_N_OFF_H(pwm->hwpwm);
335 regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
337 /* Clear the full ON bit, otherwise the set OFF time has no effect */
338 if (pwm->hwpwm >= PCA9685_MAXCHAN)
339 reg = PCA9685_ALL_LED_ON_H;
340 else
341 reg = LED_N_ON_H(pwm->hwpwm);
343 regmap_write(pca->regmap, reg, 0);
345 return 0;
348 static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
350 struct pca9685 *pca = to_pca(chip);
351 unsigned int reg;
354 * The PWM subsystem does not support a pre-delay.
355 * So, set the ON-timeout to 0
357 if (pwm->hwpwm >= PCA9685_MAXCHAN)
358 reg = PCA9685_ALL_LED_ON_L;
359 else
360 reg = LED_N_ON_L(pwm->hwpwm);
362 regmap_write(pca->regmap, reg, 0);
364 if (pwm->hwpwm >= PCA9685_MAXCHAN)
365 reg = PCA9685_ALL_LED_ON_H;
366 else
367 reg = LED_N_ON_H(pwm->hwpwm);
369 regmap_write(pca->regmap, reg, 0);
372 * Clear the full-off bit.
373 * It has precedence over the others and must be off.
375 if (pwm->hwpwm >= PCA9685_MAXCHAN)
376 reg = PCA9685_ALL_LED_OFF_H;
377 else
378 reg = LED_N_OFF_H(pwm->hwpwm);
380 regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
382 return 0;
385 static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
387 struct pca9685 *pca = to_pca(chip);
388 unsigned int reg;
390 if (pwm->hwpwm >= PCA9685_MAXCHAN)
391 reg = PCA9685_ALL_LED_OFF_H;
392 else
393 reg = LED_N_OFF_H(pwm->hwpwm);
395 regmap_write(pca->regmap, reg, LED_FULL);
397 /* Clear the LED_OFF counter. */
398 if (pwm->hwpwm >= PCA9685_MAXCHAN)
399 reg = PCA9685_ALL_LED_OFF_L;
400 else
401 reg = LED_N_OFF_L(pwm->hwpwm);
403 regmap_write(pca->regmap, reg, 0x0);
406 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
408 struct pca9685 *pca = to_pca(chip);
410 if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
411 return -EBUSY;
412 pm_runtime_get_sync(chip->dev);
414 return 0;
417 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
419 struct pca9685 *pca = to_pca(chip);
421 pca9685_pwm_disable(chip, pwm);
422 pm_runtime_put(chip->dev);
423 pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
426 static const struct pwm_ops pca9685_pwm_ops = {
427 .enable = pca9685_pwm_enable,
428 .disable = pca9685_pwm_disable,
429 .config = pca9685_pwm_config,
430 .request = pca9685_pwm_request,
431 .free = pca9685_pwm_free,
432 .owner = THIS_MODULE,
435 static const struct regmap_config pca9685_regmap_i2c_config = {
436 .reg_bits = 8,
437 .val_bits = 8,
438 .max_register = PCA9685_NUMREGS,
439 .cache_type = REGCACHE_NONE,
442 static int pca9685_pwm_probe(struct i2c_client *client,
443 const struct i2c_device_id *id)
445 struct pca9685 *pca;
446 int ret;
447 int mode2;
449 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
450 if (!pca)
451 return -ENOMEM;
453 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
454 if (IS_ERR(pca->regmap)) {
455 ret = PTR_ERR(pca->regmap);
456 dev_err(&client->dev, "Failed to initialize register map: %d\n",
457 ret);
458 return ret;
460 pca->period_ns = PCA9685_DEFAULT_PERIOD;
462 i2c_set_clientdata(client, pca);
464 regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
466 if (device_property_read_bool(&client->dev, "invert"))
467 mode2 |= MODE2_INVRT;
468 else
469 mode2 &= ~MODE2_INVRT;
471 if (device_property_read_bool(&client->dev, "open-drain"))
472 mode2 &= ~MODE2_OUTDRV;
473 else
474 mode2 |= MODE2_OUTDRV;
476 regmap_write(pca->regmap, PCA9685_MODE2, mode2);
478 /* clear all "full off" bits */
479 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
480 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
482 pca->chip.ops = &pca9685_pwm_ops;
483 /* add an extra channel for ALL_LED */
484 pca->chip.npwm = PCA9685_MAXCHAN + 1;
486 pca->chip.dev = &client->dev;
487 pca->chip.base = -1;
489 ret = pwmchip_add(&pca->chip);
490 if (ret < 0)
491 return ret;
493 ret = pca9685_pwm_gpio_probe(pca);
494 if (ret < 0) {
495 pwmchip_remove(&pca->chip);
496 return ret;
499 /* the chip comes out of power-up in the active state */
500 pm_runtime_set_active(&client->dev);
502 * enable will put the chip into suspend, which is what we
503 * want as all outputs are disabled at this point
505 pm_runtime_enable(&client->dev);
507 return 0;
510 static int pca9685_pwm_remove(struct i2c_client *client)
512 struct pca9685 *pca = i2c_get_clientdata(client);
513 int ret;
515 ret = pwmchip_remove(&pca->chip);
516 if (ret)
517 return ret;
518 pm_runtime_disable(&client->dev);
519 return 0;
522 static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev)
524 struct i2c_client *client = to_i2c_client(dev);
525 struct pca9685 *pca = i2c_get_clientdata(client);
527 pca9685_set_sleep_mode(pca, true);
528 return 0;
531 static int __maybe_unused pca9685_pwm_runtime_resume(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, false);
537 return 0;
540 static const struct i2c_device_id pca9685_id[] = {
541 { "pca9685", 0 },
542 { /* sentinel */ },
544 MODULE_DEVICE_TABLE(i2c, pca9685_id);
546 #ifdef CONFIG_ACPI
547 static const struct acpi_device_id pca9685_acpi_ids[] = {
548 { "INT3492", 0 },
549 { /* sentinel */ },
551 MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
552 #endif
554 #ifdef CONFIG_OF
555 static const struct of_device_id pca9685_dt_ids[] = {
556 { .compatible = "nxp,pca9685-pwm", },
557 { /* sentinel */ }
559 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
560 #endif
562 static const struct dev_pm_ops pca9685_pwm_pm = {
563 SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
564 pca9685_pwm_runtime_resume, NULL)
567 static struct i2c_driver pca9685_i2c_driver = {
568 .driver = {
569 .name = "pca9685-pwm",
570 .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
571 .of_match_table = of_match_ptr(pca9685_dt_ids),
572 .pm = &pca9685_pwm_pm,
574 .probe = pca9685_pwm_probe,
575 .remove = pca9685_pwm_remove,
576 .id_table = pca9685_id,
579 module_i2c_driver(pca9685_i2c_driver);
581 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
582 MODULE_DESCRIPTION("PWM driver for PCA9685");
583 MODULE_LICENSE("GPL");