Linux Audio

Check our new training course

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");
v6.2
  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#include <linux/bitmap.h>
 24
 25/*
 26 * Because the PCA9685 has only one prescaler per chip, only the first channel
 27 * that is enabled is allowed to change the prescale register.
 28 * PWM channels requested afterwards must use a period that results in the same
 29 * prescale setting as the one set by the first requested channel.
 30 * GPIOs do not count as enabled PWMs as they are not using the prescaler.
 31 */
 32
 33#define PCA9685_MODE1		0x00
 34#define PCA9685_MODE2		0x01
 35#define PCA9685_SUBADDR1	0x02
 36#define PCA9685_SUBADDR2	0x03
 37#define PCA9685_SUBADDR3	0x04
 38#define PCA9685_ALLCALLADDR	0x05
 39#define PCA9685_LEDX_ON_L	0x06
 40#define PCA9685_LEDX_ON_H	0x07
 41#define PCA9685_LEDX_OFF_L	0x08
 42#define PCA9685_LEDX_OFF_H	0x09
 43
 44#define PCA9685_ALL_LED_ON_L	0xFA
 45#define PCA9685_ALL_LED_ON_H	0xFB
 46#define PCA9685_ALL_LED_OFF_L	0xFC
 47#define PCA9685_ALL_LED_OFF_H	0xFD
 48#define PCA9685_PRESCALE	0xFE
 49
 50#define PCA9685_PRESCALE_MIN	0x03	/* => max. frequency of 1526 Hz */
 51#define PCA9685_PRESCALE_MAX	0xFF	/* => min. frequency of 24 Hz */
 52
 53#define PCA9685_COUNTER_RANGE	4096
 
 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		BIT(4)
 60#define MODE1_ALLCALL		BIT(0)
 61#define MODE1_SUB3		BIT(1)
 62#define MODE1_SUB2		BIT(2)
 63#define MODE1_SUB1		BIT(3)
 64#define MODE1_SLEEP		BIT(4)
 65#define MODE2_INVRT		BIT(4)
 66#define MODE2_OUTDRV		BIT(2)
 67
 68#define LED_N_ON_H(N)	(PCA9685_LEDX_ON_H + (4 * (N)))
 69#define LED_N_ON_L(N)	(PCA9685_LEDX_ON_L + (4 * (N)))
 70#define LED_N_OFF_H(N)	(PCA9685_LEDX_OFF_H + (4 * (N)))
 71#define LED_N_OFF_L(N)	(PCA9685_LEDX_OFF_L + (4 * (N)))
 72
 73#define REG_ON_H(C)	((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_H : LED_N_ON_H((C)))
 74#define REG_ON_L(C)	((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_ON_L : LED_N_ON_L((C)))
 75#define REG_OFF_H(C)	((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_H : LED_N_OFF_H((C)))
 76#define REG_OFF_L(C)	((C) >= PCA9685_MAXCHAN ? PCA9685_ALL_LED_OFF_L : LED_N_OFF_L((C)))
 77
 78struct pca9685 {
 79	struct pwm_chip chip;
 80	struct regmap *regmap;
 81	struct mutex lock;
 82	DECLARE_BITMAP(pwms_enabled, PCA9685_MAXCHAN + 1);
 83#if IS_ENABLED(CONFIG_GPIOLIB)
 84	struct gpio_chip gpio;
 85	DECLARE_BITMAP(pwms_inuse, PCA9685_MAXCHAN + 1);
 86#endif
 87};
 88
 89static inline struct pca9685 *to_pca(struct pwm_chip *chip)
 90{
 91	return container_of(chip, struct pca9685, chip);
 92}
 93
 94/* This function is supposed to be called with the lock mutex held */
 95static bool pca9685_prescaler_can_change(struct pca9685 *pca, int channel)
 96{
 97	/* No PWM enabled: Change allowed */
 98	if (bitmap_empty(pca->pwms_enabled, PCA9685_MAXCHAN + 1))
 99		return true;
100	/* More than one PWM enabled: Change not allowed */
101	if (bitmap_weight(pca->pwms_enabled, PCA9685_MAXCHAN + 1) > 1)
102		return false;
103	/*
104	 * Only one PWM enabled: Change allowed if the PWM about to
105	 * be changed is the one that is already enabled
106	 */
107	return test_bit(channel, pca->pwms_enabled);
108}
109
110static int pca9685_read_reg(struct pca9685 *pca, unsigned int reg, unsigned int *val)
111{
112	struct device *dev = pca->chip.dev;
113	int err;
114
115	err = regmap_read(pca->regmap, reg, val);
116	if (err)
117		dev_err(dev, "regmap_read of register 0x%x failed: %pe\n", reg, ERR_PTR(err));
118
119	return err;
120}
121
122static int pca9685_write_reg(struct pca9685 *pca, unsigned int reg, unsigned int val)
123{
124	struct device *dev = pca->chip.dev;
125	int err;
126
127	err = regmap_write(pca->regmap, reg, val);
128	if (err)
129		dev_err(dev, "regmap_write to register 0x%x failed: %pe\n", reg, ERR_PTR(err));
130
131	return err;
132}
133
134/* Helper function to set the duty cycle ratio to duty/4096 (e.g. duty=2048 -> 50%) */
135static void pca9685_pwm_set_duty(struct pca9685 *pca, int channel, unsigned int duty)
136{
137	struct pwm_device *pwm = &pca->chip.pwms[channel];
138	unsigned int on, off;
139
140	if (duty == 0) {
141		/* Set the full OFF bit, which has the highest precedence */
142		pca9685_write_reg(pca, REG_OFF_H(channel), LED_FULL);
143		return;
144	} else if (duty >= PCA9685_COUNTER_RANGE) {
145		/* Set the full ON bit and clear the full OFF bit */
146		pca9685_write_reg(pca, REG_ON_H(channel), LED_FULL);
147		pca9685_write_reg(pca, REG_OFF_H(channel), 0);
148		return;
149	}
150
 
151
152	if (pwm->state.usage_power && channel < PCA9685_MAXCHAN) {
153		/*
154		 * If usage_power is set, the pca9685 driver will phase shift
155		 * the individual channels relative to their channel number.
156		 * This improves EMI because the enabled channels no longer
157		 * turn on at the same time, while still maintaining the
158		 * configured duty cycle / power output.
159		 */
160		on = channel * PCA9685_COUNTER_RANGE / PCA9685_MAXCHAN;
161	} else
162		on = 0;
163
164	off = (on + duty) % PCA9685_COUNTER_RANGE;
165
166	/* Set ON time (clears full ON bit) */
167	pca9685_write_reg(pca, REG_ON_L(channel), on & 0xff);
168	pca9685_write_reg(pca, REG_ON_H(channel), (on >> 8) & 0xf);
169	/* Set OFF time (clears full OFF bit) */
170	pca9685_write_reg(pca, REG_OFF_L(channel), off & 0xff);
171	pca9685_write_reg(pca, REG_OFF_H(channel), (off >> 8) & 0xf);
172}
173
174static unsigned int pca9685_pwm_get_duty(struct pca9685 *pca, int channel)
175{
176	struct pwm_device *pwm = &pca->chip.pwms[channel];
177	unsigned int off = 0, on = 0, val = 0;
178
179	if (WARN_ON(channel >= PCA9685_MAXCHAN)) {
180		/* HW does not support reading state of "all LEDs" channel */
181		return 0;
182	}
183
184	pca9685_read_reg(pca, LED_N_OFF_H(channel), &off);
185	if (off & LED_FULL) {
186		/* Full OFF bit is set */
187		return 0;
188	}
 
189
190	pca9685_read_reg(pca, LED_N_ON_H(channel), &on);
191	if (on & LED_FULL) {
192		/* Full ON bit is set */
193		return PCA9685_COUNTER_RANGE;
194	}
195
196	pca9685_read_reg(pca, LED_N_OFF_L(channel), &val);
197	off = ((off & 0xf) << 8) | (val & 0xff);
198	if (!pwm->state.usage_power)
199		return off;
200
201	/* Read ON register to calculate duty cycle of staggered output */
202	if (pca9685_read_reg(pca, LED_N_ON_L(channel), &val)) {
203		/* Reset val to 0 in case reading LED_N_ON_L failed */
204		val = 0;
205	}
206	on = ((on & 0xf) << 8) | (val & 0xff);
207	return (off - on) & (PCA9685_COUNTER_RANGE - 1);
208}
209
210#if IS_ENABLED(CONFIG_GPIOLIB)
211static bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca, int pwm_idx)
212{
213	bool is_inuse;
214
215	mutex_lock(&pca->lock);
216	if (pwm_idx >= PCA9685_MAXCHAN) {
217		/*
218		 * "All LEDs" channel:
219		 * pretend already in use if any of the PWMs are requested
220		 */
221		if (!bitmap_empty(pca->pwms_inuse, PCA9685_MAXCHAN)) {
222			is_inuse = true;
223			goto out;
224		}
225	} else {
226		/*
227		 * Regular channel:
228		 * pretend already in use if the "all LEDs" channel is requested
229		 */
230		if (test_bit(PCA9685_MAXCHAN, pca->pwms_inuse)) {
231			is_inuse = true;
232			goto out;
233		}
234	}
235	is_inuse = test_and_set_bit(pwm_idx, pca->pwms_inuse);
236out:
237	mutex_unlock(&pca->lock);
238	return is_inuse;
239}
240
241static void pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
242{
243	mutex_lock(&pca->lock);
244	clear_bit(pwm_idx, pca->pwms_inuse);
245	mutex_unlock(&pca->lock);
246}
247
248static int pca9685_pwm_gpio_request(struct gpio_chip *gpio, unsigned int offset)
249{
250	struct pca9685 *pca = gpiochip_get_data(gpio);
251
252	if (pca9685_pwm_test_and_set_inuse(pca, offset))
253		return -EBUSY;
254	pm_runtime_get_sync(pca->chip.dev);
255	return 0;
256}
257
258static int pca9685_pwm_gpio_get(struct gpio_chip *gpio, unsigned int offset)
259{
260	struct pca9685 *pca = gpiochip_get_data(gpio);
 
261
262	return pca9685_pwm_get_duty(pca, offset) != 0;
263}
264
265static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
266				 int value)
267{
268	struct pca9685 *pca = gpiochip_get_data(gpio);
269
270	pca9685_pwm_set_duty(pca, offset, value ? PCA9685_COUNTER_RANGE : 0);
271}
272
273static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
274{
275	struct pca9685 *pca = gpiochip_get_data(gpio);
 
 
276
277	pca9685_pwm_set_duty(pca, offset, 0);
278	pm_runtime_put(pca->chip.dev);
279	pca9685_pwm_clear_inuse(pca, offset);
280}
281
282static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
283					  unsigned int offset)
284{
285	/* Always out */
286	return GPIO_LINE_DIRECTION_OUT;
287}
288
289static int pca9685_pwm_gpio_direction_input(struct gpio_chip *gpio,
290					    unsigned int offset)
291{
292	return -EINVAL;
293}
294
295static int pca9685_pwm_gpio_direction_output(struct gpio_chip *gpio,
296					     unsigned int offset, int value)
297{
298	pca9685_pwm_gpio_set(gpio, offset, value);
299
300	return 0;
301}
302
303/*
304 * The PCA9685 has a bit for turning the PWM output full off or on. Some
305 * boards like Intel Galileo actually uses these as normal GPIOs so we
306 * expose a GPIO chip here which can exclusively take over the underlying
307 * PWM channel.
308 */
309static int pca9685_pwm_gpio_probe(struct pca9685 *pca)
310{
311	struct device *dev = pca->chip.dev;
312
313	pca->gpio.label = dev_name(dev);
314	pca->gpio.parent = dev;
315	pca->gpio.request = pca9685_pwm_gpio_request;
316	pca->gpio.free = pca9685_pwm_gpio_free;
317	pca->gpio.get_direction = pca9685_pwm_gpio_get_direction;
318	pca->gpio.direction_input = pca9685_pwm_gpio_direction_input;
319	pca->gpio.direction_output = pca9685_pwm_gpio_direction_output;
320	pca->gpio.get = pca9685_pwm_gpio_get;
321	pca->gpio.set = pca9685_pwm_gpio_set;
322	pca->gpio.base = -1;
323	pca->gpio.ngpio = PCA9685_MAXCHAN;
324	pca->gpio.can_sleep = true;
325
326	return devm_gpiochip_add_data(dev, &pca->gpio, pca);
327}
328#else
329static inline bool pca9685_pwm_test_and_set_inuse(struct pca9685 *pca,
330						  int pwm_idx)
331{
332	return false;
333}
334
335static inline void
336pca9685_pwm_clear_inuse(struct pca9685 *pca, int pwm_idx)
337{
338}
339
340static inline int pca9685_pwm_gpio_probe(struct pca9685 *pca)
341{
342	return 0;
343}
344#endif
345
346static void pca9685_set_sleep_mode(struct pca9685 *pca, bool enable)
347{
348	struct device *dev = pca->chip.dev;
349	int err = regmap_update_bits(pca->regmap, PCA9685_MODE1,
350				     MODE1_SLEEP, enable ? MODE1_SLEEP : 0);
351	if (err) {
352		dev_err(dev, "regmap_update_bits of register 0x%x failed: %pe\n",
353			PCA9685_MODE1, ERR_PTR(err));
354		return;
355	}
356
357	if (!enable) {
358		/* Wait 500us for the oscillator to be back up */
359		udelay(500);
360	}
361}
362
363static int __pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
364			       const struct pwm_state *state)
365{
366	struct pca9685 *pca = to_pca(chip);
367	unsigned long long duty, prescale;
368	unsigned int val = 0;
369
370	if (state->polarity != PWM_POLARITY_NORMAL)
371		return -EINVAL;
 
 
 
 
 
 
372
373	prescale = DIV_ROUND_CLOSEST_ULL(PCA9685_OSC_CLOCK_MHZ * state->period,
374					 PCA9685_COUNTER_RANGE * 1000) - 1;
375	if (prescale < PCA9685_PRESCALE_MIN || prescale > PCA9685_PRESCALE_MAX) {
376		dev_err(chip->dev, "pwm not changed: period out of bounds!\n");
377		return -EINVAL;
378	}
379
380	if (!state->enabled) {
381		pca9685_pwm_set_duty(pca, pwm->hwpwm, 0);
382		return 0;
383	}
384
385	pca9685_read_reg(pca, PCA9685_PRESCALE, &val);
386	if (prescale != val) {
387		if (!pca9685_prescaler_can_change(pca, pwm->hwpwm)) {
388			dev_err(chip->dev,
389				"pwm not changed: periods of enabled pwms must match!\n");
390			return -EBUSY;
391		}
392
393		/*
394		 * Putting the chip briefly into SLEEP mode
395		 * at this point won't interfere with the
396		 * pm_runtime framework, because the pm_runtime
397		 * state is guaranteed active here.
398		 */
399		/* Put chip into sleep mode */
400		pca9685_set_sleep_mode(pca, true);
401
402		/* Change the chip-wide output frequency */
403		pca9685_write_reg(pca, PCA9685_PRESCALE, prescale);
404
405		/* Wake the chip up */
406		pca9685_set_sleep_mode(pca, false);
407	}
408
409	duty = PCA9685_COUNTER_RANGE * state->duty_cycle;
410	duty = DIV_ROUND_UP_ULL(duty, state->period);
411	pca9685_pwm_set_duty(pca, pwm->hwpwm, duty);
412	return 0;
413}
414
415static int pca9685_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
416			     const struct pwm_state *state)
417{
418	struct pca9685 *pca = to_pca(chip);
419	int ret;
420
421	mutex_lock(&pca->lock);
422	ret = __pca9685_pwm_apply(chip, pwm, state);
423	if (ret == 0) {
424		if (state->enabled)
425			set_bit(pwm->hwpwm, pca->pwms_enabled);
426		else
427			clear_bit(pwm->hwpwm, pca->pwms_enabled);
428	}
429	mutex_unlock(&pca->lock);
430
431	return ret;
432}
433
434static int pca9685_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
435				 struct pwm_state *state)
436{
437	struct pca9685 *pca = to_pca(chip);
438	unsigned long long duty;
439	unsigned int val = 0;
440
441	/* Calculate (chip-wide) period from prescale value */
442	pca9685_read_reg(pca, PCA9685_PRESCALE, &val);
443	/*
444	 * PCA9685_OSC_CLOCK_MHZ is 25, i.e. an integer divider of 1000.
445	 * The following calculation is therefore only a multiplication
446	 * and we are not losing precision.
447	 */
448	state->period = (PCA9685_COUNTER_RANGE * 1000 / PCA9685_OSC_CLOCK_MHZ) *
449			(val + 1);
450
451	/* The (per-channel) polarity is fixed */
452	state->polarity = PWM_POLARITY_NORMAL;
453
454	if (pwm->hwpwm >= PCA9685_MAXCHAN) {
455		/*
456		 * The "all LEDs" channel does not support HW readout
457		 * Return 0 and disabled for backwards compatibility
458		 */
459		state->duty_cycle = 0;
460		state->enabled = false;
461		return 0;
462	}
463
464	state->enabled = true;
465	duty = pca9685_pwm_get_duty(pca, pwm->hwpwm);
466	state->duty_cycle = DIV_ROUND_DOWN_ULL(duty * state->period, PCA9685_COUNTER_RANGE);
467
468	return 0;
469}
470
471static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
472{
473	struct pca9685 *pca = to_pca(chip);
474
475	if (pca9685_pwm_test_and_set_inuse(pca, pwm->hwpwm))
476		return -EBUSY;
477
478	if (pwm->hwpwm < PCA9685_MAXCHAN) {
479		/* PWMs - except the "all LEDs" channel - default to enabled */
480		mutex_lock(&pca->lock);
481		set_bit(pwm->hwpwm, pca->pwms_enabled);
482		mutex_unlock(&pca->lock);
483	}
484
485	pm_runtime_get_sync(chip->dev);
486
487	return 0;
488}
489
490static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
491{
492	struct pca9685 *pca = to_pca(chip);
493
494	mutex_lock(&pca->lock);
495	pca9685_pwm_set_duty(pca, pwm->hwpwm, 0);
496	clear_bit(pwm->hwpwm, pca->pwms_enabled);
497	mutex_unlock(&pca->lock);
498
499	pm_runtime_put(chip->dev);
500	pca9685_pwm_clear_inuse(pca, pwm->hwpwm);
501}
502
503static const struct pwm_ops pca9685_pwm_ops = {
504	.apply = pca9685_pwm_apply,
505	.get_state = pca9685_pwm_get_state,
 
506	.request = pca9685_pwm_request,
507	.free = pca9685_pwm_free,
508	.owner = THIS_MODULE,
509};
510
511static const struct regmap_config pca9685_regmap_i2c_config = {
512	.reg_bits = 8,
513	.val_bits = 8,
514	.max_register = PCA9685_NUMREGS,
515	.cache_type = REGCACHE_NONE,
516};
517
518static int pca9685_pwm_probe(struct i2c_client *client)
 
519{
520	struct pca9685 *pca;
521	unsigned int reg;
522	int ret;
 
523
524	pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
525	if (!pca)
526		return -ENOMEM;
527
528	pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
529	if (IS_ERR(pca->regmap)) {
530		ret = PTR_ERR(pca->regmap);
531		dev_err(&client->dev, "Failed to initialize register map: %d\n",
532			ret);
533		return ret;
534	}
 
 
535
536	i2c_set_clientdata(client, pca);
537
538	mutex_init(&pca->lock);
539
540	ret = pca9685_read_reg(pca, PCA9685_MODE2, &reg);
541	if (ret)
542		return ret;
543
544	if (device_property_read_bool(&client->dev, "invert"))
545		reg |= MODE2_INVRT;
546	else
547		reg &= ~MODE2_INVRT;
548
549	if (device_property_read_bool(&client->dev, "open-drain"))
550		reg &= ~MODE2_OUTDRV;
551	else
552		reg |= MODE2_OUTDRV;
553
554	ret = pca9685_write_reg(pca, PCA9685_MODE2, reg);
555	if (ret)
556		return ret;
557
558	/* Disable all LED ALLCALL and SUBx addresses to avoid bus collisions */
559	pca9685_read_reg(pca, PCA9685_MODE1, &reg);
560	reg &= ~(MODE1_ALLCALL | MODE1_SUB1 | MODE1_SUB2 | MODE1_SUB3);
561	pca9685_write_reg(pca, PCA9685_MODE1, reg);
562
563	/* Reset OFF/ON registers to POR default */
564	pca9685_write_reg(pca, PCA9685_ALL_LED_OFF_L, 0);
565	pca9685_write_reg(pca, PCA9685_ALL_LED_OFF_H, LED_FULL);
566	pca9685_write_reg(pca, PCA9685_ALL_LED_ON_L, 0);
567	pca9685_write_reg(pca, PCA9685_ALL_LED_ON_H, LED_FULL);
568
569	pca->chip.ops = &pca9685_pwm_ops;
570	/* Add an extra channel for ALL_LED */
571	pca->chip.npwm = PCA9685_MAXCHAN + 1;
572
573	pca->chip.dev = &client->dev;
 
 
574
575	ret = pwmchip_add(&pca->chip);
576	if (ret < 0)
577		return ret;
578
579	ret = pca9685_pwm_gpio_probe(pca);
580	if (ret < 0) {
581		pwmchip_remove(&pca->chip);
582		return ret;
583	}
584
585	pm_runtime_enable(&client->dev);
586
587	if (pm_runtime_enabled(&client->dev)) {
588		/*
589		 * Although the chip comes out of power-up in the sleep state,
590		 * we force it to sleep in case it was woken up before
591		 */
592		pca9685_set_sleep_mode(pca, true);
593		pm_runtime_set_suspended(&client->dev);
594	} else {
595		/* Wake the chip up if runtime PM is disabled */
596		pca9685_set_sleep_mode(pca, false);
597	}
598
599	return 0;
600}
601
602static void pca9685_pwm_remove(struct i2c_client *client)
603{
604	struct pca9685 *pca = i2c_get_clientdata(client);
605
606	pwmchip_remove(&pca->chip);
607
608	if (!pm_runtime_enabled(&client->dev)) {
609		/* Put chip in sleep state if runtime PM is disabled */
610		pca9685_set_sleep_mode(pca, true);
611	}
612
613	pm_runtime_disable(&client->dev);
614}
615
616static int __maybe_unused pca9685_pwm_runtime_suspend(struct device *dev)
617{
618	struct i2c_client *client = to_i2c_client(dev);
619	struct pca9685 *pca = i2c_get_clientdata(client);
620
621	pca9685_set_sleep_mode(pca, true);
622	return 0;
623}
624
625static int __maybe_unused pca9685_pwm_runtime_resume(struct device *dev)
626{
627	struct i2c_client *client = to_i2c_client(dev);
628	struct pca9685 *pca = i2c_get_clientdata(client);
629
630	pca9685_set_sleep_mode(pca, false);
631	return 0;
632}
633
634static const struct i2c_device_id pca9685_id[] = {
635	{ "pca9685", 0 },
636	{ /* sentinel */ },
637};
638MODULE_DEVICE_TABLE(i2c, pca9685_id);
639
640#ifdef CONFIG_ACPI
641static const struct acpi_device_id pca9685_acpi_ids[] = {
642	{ "INT3492", 0 },
643	{ /* sentinel */ },
644};
645MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
646#endif
647
648#ifdef CONFIG_OF
649static const struct of_device_id pca9685_dt_ids[] = {
650	{ .compatible = "nxp,pca9685-pwm", },
651	{ /* sentinel */ }
652};
653MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
654#endif
655
656static const struct dev_pm_ops pca9685_pwm_pm = {
657	SET_RUNTIME_PM_OPS(pca9685_pwm_runtime_suspend,
658			   pca9685_pwm_runtime_resume, NULL)
659};
660
661static struct i2c_driver pca9685_i2c_driver = {
662	.driver = {
663		.name = "pca9685-pwm",
664		.acpi_match_table = ACPI_PTR(pca9685_acpi_ids),
665		.of_match_table = of_match_ptr(pca9685_dt_ids),
666		.pm = &pca9685_pwm_pm,
667	},
668	.probe_new = pca9685_pwm_probe,
669	.remove = pca9685_pwm_remove,
670	.id_table = pca9685_id,
671};
672
673module_i2c_driver(pca9685_i2c_driver);
674
675MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
676MODULE_DESCRIPTION("PWM driver for PCA9685");
677MODULE_LICENSE("GPL");