Linux Audio

Check our new training course

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