1 /*
2 * Driver for PCA9685 16-channel 12-bit PWM LED controller
3 *
4 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
5 * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
6 *
7 * based on the pwm-twl-led.c driver
8 *
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.
12 *
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.
17 *
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/>.
20 */
21
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
35 /*
36 * Because the PCA9685 has only one prescaler per chip, changing the period of
37 * one channel affects the period of all 16 PWM outputs!
38 * However, the ratio between each configured duty cycle and the chip-wide
39 * period remains constant, because the OFF time is set in proportion to the
40 * counter range.
41 */
42
43 #define PCA9685_MODE1 0x00
44 #define PCA9685_MODE2 0x01
45 #define PCA9685_SUBADDR1 0x02
46 #define PCA9685_SUBADDR2 0x03
47 #define PCA9685_SUBADDR3 0x04
48 #define PCA9685_ALLCALLADDR 0x05
49 #define PCA9685_LEDX_ON_L 0x06
50 #define PCA9685_LEDX_ON_H 0x07
51 #define PCA9685_LEDX_OFF_L 0x08
52 #define PCA9685_LEDX_OFF_H 0x09
53
54 #define PCA9685_ALL_LED_ON_L 0xFA
55 #define PCA9685_ALL_LED_ON_H 0xFB
56 #define PCA9685_ALL_LED_OFF_L 0xFC
57 #define PCA9685_ALL_LED_OFF_H 0xFD
58 #define PCA9685_PRESCALE 0xFE
59
60 #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
61 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
62
63 #define PCA9685_COUNTER_RANGE 4096
64 #define PCA9685_DEFAULT_PERIOD 5000000 /* Default period_ns = 1/200 Hz */
65 #define PCA9685_OSC_CLOCK_MHZ 25 /* Internal oscillator with 25 MHz */
66
67 #define PCA9685_NUMREGS 0xFF
68 #define PCA9685_MAXCHAN 0x10
69
70 #define LED_FULL (1 << 4)
71 #define MODE1_SLEEP (1 << 4)
72 #define MODE2_INVRT (1 << 4)
73 #define MODE2_OUTDRV (1 << 2)
74
75 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
76 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
77 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
78 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
79
80 struct pca9685 {
81 struct pwm_chip chip;
82 struct regmap *regmap;
83 int duty_ns;
84 int period_ns;
85 #if IS_ENABLED(CONFIG_GPIOLIB)
86 struct mutex lock;
87 struct gpio_chip gpio;
88 #endif
89 };
90
to_pca(struct pwm_chip * chip)91 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
92 {
93 return container_of(chip, struct pca9685, chip);
94 }
95
96 #if IS_ENABLED(CONFIG_GPIOLIB)
pca9685_pwm_gpio_request(struct gpio_chip * gpio,unsigned int offset)97 static 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 pm_runtime_get_sync(pca->chip.dev);
115 return 0;
116 }
117
pca9685_pwm_is_gpio(struct pca9685 * pca,struct pwm_device * pwm)118 static bool pca9685_pwm_is_gpio(struct pca9685 *pca, struct pwm_device *pwm)
119 {
120 bool is_gpio = false;
121
122 mutex_lock(&pca->lock);
123
124 if (pwm->hwpwm >= PCA9685_MAXCHAN) {
125 unsigned int i;
126
127 /*
128 * Check if any of the GPIOs are requested and in that case
129 * prevent using the "all LEDs" channel.
130 */
131 for (i = 0; i < pca->gpio.ngpio; i++)
132 if (gpiochip_is_requested(&pca->gpio, i)) {
133 is_gpio = true;
134 break;
135 }
136 } else if (pwm_get_chip_data(pwm)) {
137 is_gpio = true;
138 }
139
140 mutex_unlock(&pca->lock);
141 return is_gpio;
142 }
143
pca9685_pwm_gpio_get(struct gpio_chip * gpio,unsigned int offset)144 static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
145 {
146 struct pca9685 *pca = gpiochip_get_data(gpio);
147 struct pwm_device *pwm = &pca->chip.pwms[offset];
148 unsigned int value;
149
150 regmap_read(pca->regmap, LED_N_ON_H(pwm->hwpwm), &value);
151
152 return value & LED_FULL;
153 }
154
pca9685_pwm_gpio_set(struct gpio_chip * gpio,unsigned int offset,int value)155 static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
156 int value)
157 {
158 struct pca9685 *pca = gpiochip_get_data(gpio);
159 struct pwm_device *pwm = &pca->chip.pwms[offset];
160 unsigned int on = value ? LED_FULL : 0;
161
162 /* Clear both OFF registers */
163 regmap_write(pca->regmap, LED_N_OFF_L(pwm->hwpwm), 0);
164 regmap_write(pca->regmap, LED_N_OFF_H(pwm->hwpwm), 0);
165
166 /* Set the full ON bit */
167 regmap_write(pca->regmap, LED_N_ON_H(pwm->hwpwm), on);
168 }
169
pca9685_pwm_gpio_free(struct gpio_chip * gpio,unsigned int offset)170 static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
171 {
172 struct pca9685 *pca = gpiochip_get_data(gpio);
173
174 pca9685_pwm_gpio_set(gpio, offset, 0);
175 pm_runtime_put(pca->chip.dev);
176 }
177
pca9685_pwm_gpio_get_direction(struct gpio_chip * chip,unsigned int offset)178 static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
179 unsigned int offset)
180 {
181 /* Always out */
182 return 0;
183 }
184
pca9685_pwm_gpio_direction_input(struct gpio_chip * gpio,unsigned int offset)185 static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
186 unsigned int offset)
187 {
188 return -EINVAL;
189 }
190
pca9685_pwm_gpio_direction_output(struct gpio_chip * gpio,unsigned int offset,int value)191 static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
192 unsigned int offset, int value)
193 {
194 pca9685_pwm_gpio_set(gpio, offset, value);
195
196 return 0;
197 }
198
199 /*
200 * The PCA9685 has a bit for turning the PWM output full off or on. Some
201 * boards like Intel Galileo actually uses these as normal GPIOs so we
202 * expose a GPIO chip here which can exclusively take over the underlying
203 * PWM channel.
204 */
pca9685_pwm_gpio_probe(struct pca9685 * pca)205 static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
206 {
207 struct device *dev = pca->chip.dev;
208
209 mutex_init(&pca->lock);
210
211 pca->gpio.label = dev_name(dev);
212 pca->gpio.parent = dev;
213 pca->gpio.request = pca9685_pwm_gpio_request;
214 pca->gpio.free = pca9685_pwm_gpio_free;
215 pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
216 pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
217 pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
218 pca->gpio.get = pca9685_pwm_gpio_get;
219 pca->gpio.set = pca9685_pwm_gpio_set;
220 pca->gpio.base = -1;
221 pca->gpio.ngpio = PCA9685_MAXCHAN;
222 pca->gpio.can_sleep = true;
223
224 return devm_gpiochip_add_data(dev, &pca->gpio, pca);
225 }
226 #else
pca9685_pwm_is_gpio(struct pca9685 * pca,struct pwm_device * pwm)227 static inline bool pca9685_pwm_is_gpio(struct pca9685 *pca,
228 struct pwm_device *pwm)
229 {
230 return false;
231 }
232
pca9685_pwm_gpio_probe(struct pca9685 * pca)233 static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
234 {
235 return 0;
236 }
237 #endif
238
pca9685_set_sleep_mode(struct pca9685 * pca,bool enable)239 static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
240 {
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);
246 }
247 }
248
pca9685_pwm_config(struct pwm_chip * chip,struct pwm_device * pwm,int duty_ns,int period_ns)249 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
250 int duty_ns, int period_ns)
251 {
252 struct pca9685 *pca = to_pca(chip);
253 unsigned long long duty;
254 unsigned int reg;
255 int prescale;
256
257 if (period_ns != pca->period_ns) {
258 prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
259 PCA9685_COUNTER_RANGE * 1000) - 1;
260
261 if (prescale >= PCA9685_PRESCALE_MIN &&
262 prescale <= PCA9685_PRESCALE_MAX) {
263 /*
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.
268 */
269 /* Put chip into sleep mode */
270 pca9685_set_sleep_mode(pca, true);
271
272 /* Change the chip-wide output frequency */
273 regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
274
275 /* Wake the chip up */
276 pca9685_set_sleep_mode(pca, false);
277
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;
283 }
284 }
285
286 pca->duty_ns = duty_ns;
287
288 if (duty_ns < 1) {
289 if (pwm->hwpwm >= PCA9685_MAXCHAN)
290 reg = PCA9685_ALL_LED_OFF_H;
291 else
292 reg = LED_N_OFF_H(pwm->hwpwm);
293
294 regmap_write(pca->regmap, reg, LED_FULL);
295
296 return 0;
297 }
298
299 if (duty_ns == period_ns) {
300 /* Clear both OFF registers */
301 if (pwm->hwpwm >= PCA9685_MAXCHAN)
302 reg = PCA9685_ALL_LED_OFF_L;
303 else
304 reg = LED_N_OFF_L(pwm->hwpwm);
305
306 regmap_write(pca->regmap, reg, 0x0);
307
308 if (pwm->hwpwm >= PCA9685_MAXCHAN)
309 reg = PCA9685_ALL_LED_OFF_H;
310 else
311 reg = LED_N_OFF_H(pwm->hwpwm);
312
313 regmap_write(pca->regmap, reg, 0x0);
314
315 /* Set the full ON bit */
316 if (pwm->hwpwm >= PCA9685_MAXCHAN)
317 reg = PCA9685_ALL_LED_ON_H;
318 else
319 reg = LED_N_ON_H(pwm->hwpwm);
320
321 regmap_write(pca->regmap, reg, LED_FULL);
322
323 return 0;
324 }
325
326 duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
327 duty = DIV_ROUND_UP_ULL(duty, period_ns);
328
329 if (pwm->hwpwm >= PCA9685_MAXCHAN)
330 reg = PCA9685_ALL_LED_OFF_L;
331 else
332 reg = LED_N_OFF_L(pwm->hwpwm);
333
334 regmap_write(pca->regmap, reg, (int)duty & 0xff);
335
336 if (pwm->hwpwm >= PCA9685_MAXCHAN)
337 reg = PCA9685_ALL_LED_OFF_H;
338 else
339 reg = LED_N_OFF_H(pwm->hwpwm);
340
341 regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
342
343 /* Clear the full ON bit, otherwise the set OFF time has no effect */
344 if (pwm->hwpwm >= PCA9685_MAXCHAN)
345 reg = PCA9685_ALL_LED_ON_H;
346 else
347 reg = LED_N_ON_H(pwm->hwpwm);
348
349 regmap_write(pca->regmap, reg, 0);
350
351 return 0;
352 }
353
pca9685_pwm_enable(struct pwm_chip * chip,struct pwm_device * pwm)354 static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
355 {
356 struct pca9685 *pca = to_pca(chip);
357 unsigned int reg;
358
359 /*
360 * The PWM subsystem does not support a pre-delay.
361 * So, set the ON-timeout to 0
362 */
363 if (pwm->hwpwm >= PCA9685_MAXCHAN)
364 reg = PCA9685_ALL_LED_ON_L;
365 else
366 reg = LED_N_ON_L(pwm->hwpwm);
367
368 regmap_write(pca->regmap, reg, 0);
369
370 if (pwm->hwpwm >= PCA9685_MAXCHAN)
371 reg = PCA9685_ALL_LED_ON_H;
372 else
373 reg = LED_N_ON_H(pwm->hwpwm);
374
375 regmap_write(pca->regmap, reg, 0);
376
377 /*
378 * Clear the full-off bit.
379 * It has precedence over the others and must be off.
380 */
381 if (pwm->hwpwm >= PCA9685_MAXCHAN)
382 reg = PCA9685_ALL_LED_OFF_H;
383 else
384 reg = LED_N_OFF_H(pwm->hwpwm);
385
386 regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
387
388 return 0;
389 }
390
pca9685_pwm_disable(struct pwm_chip * chip,struct pwm_device * pwm)391 static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
392 {
393 struct pca9685 *pca = to_pca(chip);
394 unsigned int reg;
395
396 if (pwm->hwpwm >= PCA9685_MAXCHAN)
397 reg = PCA9685_ALL_LED_OFF_H;
398 else
399 reg = LED_N_OFF_H(pwm->hwpwm);
400
401 regmap_write(pca->regmap, reg, LED_FULL);
402
403 /* Clear the LED_OFF counter. */
404 if (pwm->hwpwm >= PCA9685_MAXCHAN)
405 reg = PCA9685_ALL_LED_OFF_L;
406 else
407 reg = LED_N_OFF_L(pwm->hwpwm);
408
409 regmap_write(pca->regmap, reg, 0x0);
410 }
411
pca9685_pwm_request(struct pwm_chip * chip,struct pwm_device * pwm)412 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
413 {
414 struct pca9685 *pca = to_pca(chip);
415
416 if (pca9685_pwm_is_gpio(pca, pwm))
417 return -EBUSY;
418 pm_runtime_get_sync(chip->dev);
419
420 return 0;
421 }
422
pca9685_pwm_free(struct pwm_chip * chip,struct pwm_device * pwm)423 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
424 {
425 pca9685_pwm_disable(chip, pwm);
426 pm_runtime_put(chip->dev);
427 }
428
429 static const struct pwm_ops pca9685_pwm_ops = {
430 .enable = pca9685_pwm_enable,
431 .disable = pca9685_pwm_disable,
432 .config = pca9685_pwm_config,
433 .request = pca9685_pwm_request,
434 .free = pca9685_pwm_free,
435 .owner = THIS_MODULE,
436 };
437
438 static const struct regmap_config pca9685_regmap_i2c_config = {
439 .reg_bits = 8,
440 .val_bits = 8,
441 .max_register = PCA9685_NUMREGS,
442 .cache_type = REGCACHE_NONE,
443 };
444
pca9685_pwm_probe(struct i2c_client * client,const struct i2c_device_id * id)445 static int pca9685_pwm_probe(struct i2c_client *client,
446 const struct i2c_device_id *id)
447 {
448 struct pca9685 *pca;
449 int ret;
450 int mode2;
451
452 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
453 if (!pca)
454 return -ENOMEM;
455
456 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
457 if (IS_ERR(pca->regmap)) {
458 ret = PTR_ERR(pca->regmap);
459 dev_err(&client->dev, "Failed to initialize register map: %d\n",
460 ret);
461 return ret;
462 }
463 pca->duty_ns = 0;
464 pca->period_ns = PCA9685_DEFAULT_PERIOD;
465
466 i2c_set_clientdata(client, pca);
467
468 regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
469
470 if (device_property_read_bool(&client->dev, "invert"))
471 mode2 |= MODE2_INVRT;
472 else
473 mode2 &= ~MODE2_INVRT;
474
475 if (device_property_read_bool(&client->dev, "open-drain"))
476 mode2 &= ~MODE2_OUTDRV;
477 else
478 mode2 |= MODE2_OUTDRV;
479
480 regmap_write(pca->regmap, PCA9685_MODE2, mode2);
481
482 /* clear all "full off" bits */
483 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
484 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
485
486 pca->chip.ops = &pca9685_pwm_ops;
487 /* add an extra channel for ALL_LED */
488 pca->chip.npwm = PCA9685_MAXCHAN + 1;
489
490 pca->chip.dev = &client->dev;
491 pca->chip.base = -1;
492
493 ret = pwmchip_add(&pca->chip);
494 if (ret < 0)
495 return ret;
496
497 ret = pca9685_pwm_gpio_probe(pca);
498 if (ret < 0) {
499 pwmchip_remove(&pca->chip);
500 return ret;
501 }
502
503 /* the chip comes out of power-up in the active state */
504 pm_runtime_set_active(&client->dev);
505 /*
506 * enable will put the chip into suspend, which is what we
507 * want as all outputs are disabled at this point
508 */
509 pm_runtime_enable(&client->dev);
510
511 return 0;
512 }
513
pca9685_pwm_remove(struct i2c_client * client)514 static int pca9685_pwm_remove(struct i2c_client *client)
515 {
516 struct pca9685 *pca = i2c_get_clientdata(client);
517 int ret;
518
519 ret = pwmchip_remove(&pca->chip);
520 if (ret)
521 return ret;
522 pm_runtime_disable(&client->dev);
523 return 0;
524 }
525
526 #ifdef CONFIG_PM
pca9685_pwm_runtime_suspend(struct device * dev)527 static int pca9685_pwm_runtime_suspend(struct device *dev)
528 {
529 struct i2c_client *client = to_i2c_client(dev);
530 struct pca9685 *pca = i2c_get_clientdata(client);
531
532 pca9685_set_sleep_mode(pca, true);
533 return 0;
534 }
535
pca9685_pwm_runtime_resume(struct device * dev)536 static int pca9685_pwm_runtime_resume(struct device *dev)
537 {
538 struct i2c_client *client = to_i2c_client(dev);
539 struct pca9685 *pca = i2c_get_clientdata(client);
540
541 pca9685_set_sleep_mode(pca, false);
542 return 0;
543 }
544 #endif
545
546 static const struct i2c_device_id pca9685_id[] = {
547 { "pca9685", 0 },
548 { /* sentinel */ },
549 };
550 MODULE_DEVICE_TABLE(i2c, pca9685_id);
551
552 #ifdef CONFIG_ACPI
553 static const struct acpi_device_id pca9685_acpi_ids[] = {
554 { "INT3492", 0 },
555 { /* sentinel */ },
556 };
557 MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
558 #endif
559
560 #ifdef CONFIG_OF
561 static const struct of_device_id pca9685_dt_ids[] = {
562 { .compatible = "nxp,pca9685-pwm", },
563 { /* sentinel */ }
564 };
565 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
566 #endif
567
568 static const struct dev_pm_ops pca9685_pwm_pm = {
569 SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
570 pca9685_pwm_runtime_resume, NULL)
571 };
572
573 static struct i2c_driver pca9685_i2c_driver = {
574 .driver = {
575 .name = "pca9685-pwm",
576 .acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
577 .of_match_table = of_match_ptr(pca9685_dt_ids),
578 .pm = &pca9685_pwm_pm,
579 },
580 .probe = pca9685_pwm_probe,
581 .remove = pca9685_pwm_remove,
582 .id_table = pca9685_id,
583 };
584
585 module_i2c_driver(pca9685_i2c_driver);
586
587 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
588 MODULE_DESCRIPTION("PWM driver for PCA9685");
589 MODULE_LICENSE("GPL");
590