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