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