Linux Audio

Check our new training course

Yocto distribution development and maintenance

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