xref: /linux/drivers/pwm/pwm-pca9685.c (revision a2cce7a9f1b8cc3d4edce106fb971529f1d4d9ce)
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/i2c.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/pwm.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 
30 /*
31  * Because the PCA9685 has only one prescaler per chip, changing the period of
32  * one channel affects the period of all 16 PWM outputs!
33  * However, the ratio between each configured duty cycle and the chip-wide
34  * period remains constant, because the OFF time is set in proportion to the
35  * counter range.
36  */
37 
38 #define PCA9685_MODE1		0x00
39 #define PCA9685_MODE2		0x01
40 #define PCA9685_SUBADDR1	0x02
41 #define PCA9685_SUBADDR2	0x03
42 #define PCA9685_SUBADDR3	0x04
43 #define PCA9685_ALLCALLADDR	0x05
44 #define PCA9685_LEDX_ON_L	0x06
45 #define PCA9685_LEDX_ON_H	0x07
46 #define PCA9685_LEDX_OFF_L	0x08
47 #define PCA9685_LEDX_OFF_H	0x09
48 
49 #define PCA9685_ALL_LED_ON_L	0xFA
50 #define PCA9685_ALL_LED_ON_H	0xFB
51 #define PCA9685_ALL_LED_OFF_L	0xFC
52 #define PCA9685_ALL_LED_OFF_H	0xFD
53 #define PCA9685_PRESCALE	0xFE
54 
55 #define PCA9685_PRESCALE_MIN	0x03	/* => max. frequency of 1526 Hz */
56 #define PCA9685_PRESCALE_MAX	0xFF	/* => min. frequency of 24 Hz */
57 
58 #define PCA9685_COUNTER_RANGE	4096
59 #define PCA9685_DEFAULT_PERIOD	5000000	/* Default period_ns = 1/200 Hz */
60 #define PCA9685_OSC_CLOCK_MHZ	25	/* Internal oscillator with 25 MHz */
61 
62 #define PCA9685_NUMREGS		0xFF
63 #define PCA9685_MAXCHAN		0x10
64 
65 #define LED_FULL		(1 << 4)
66 #define MODE1_RESTART		(1 << 7)
67 #define MODE1_SLEEP		(1 << 4)
68 #define MODE2_INVRT		(1 << 4)
69 #define MODE2_OUTDRV		(1 << 2)
70 
71 #define LED_N_ON_H(N)	(PCA9685_LEDX_ON_H + (4 * (N)))
72 #define LED_N_ON_L(N)	(PCA9685_LEDX_ON_L + (4 * (N)))
73 #define LED_N_OFF_H(N)	(PCA9685_LEDX_OFF_H + (4 * (N)))
74 #define LED_N_OFF_L(N)	(PCA9685_LEDX_OFF_L + (4 * (N)))
75 
76 struct pca9685 {
77 	struct pwm_chip chip;
78 	struct regmap *regmap;
79 	int active_cnt;
80 	int duty_ns;
81 	int period_ns;
82 };
83 
84 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
85 {
86 	return container_of(chip, struct pca9685, chip);
87 }
88 
89 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
90 			      int duty_ns, int period_ns)
91 {
92 	struct pca9685 *pca = to_pca(chip);
93 	unsigned long long duty;
94 	unsigned int reg;
95 	int prescale;
96 
97 	if (period_ns != pca->period_ns) {
98 		prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
99 					     PCA9685_COUNTER_RANGE * 1000) - 1;
100 
101 		if (prescale >= PCA9685_PRESCALE_MIN &&
102 			prescale <= PCA9685_PRESCALE_MAX) {
103 			/* Put chip into sleep mode */
104 			regmap_update_bits(pca->regmap, PCA9685_MODE1,
105 					   MODE1_SLEEP, MODE1_SLEEP);
106 
107 			/* Change the chip-wide output frequency */
108 			regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
109 
110 			/* Wake the chip up */
111 			regmap_update_bits(pca->regmap, PCA9685_MODE1,
112 					   MODE1_SLEEP, 0x0);
113 
114 			/* Wait 500us for the oscillator to be back up */
115 			udelay(500);
116 
117 			pca->period_ns = period_ns;
118 
119 			/*
120 			 * If the duty cycle did not change, restart PWM with
121 			 * the same duty cycle to period ratio and return.
122 			 */
123 			if (duty_ns == pca->duty_ns) {
124 				regmap_update_bits(pca->regmap, PCA9685_MODE1,
125 						   MODE1_RESTART, 0x1);
126 				return 0;
127 			}
128 		} else {
129 			dev_err(chip->dev,
130 				"prescaler not set: period out of bounds!\n");
131 			return -EINVAL;
132 		}
133 	}
134 
135 	pca->duty_ns = duty_ns;
136 
137 	if (duty_ns < 1) {
138 		if (pwm->hwpwm >= PCA9685_MAXCHAN)
139 			reg = PCA9685_ALL_LED_OFF_H;
140 		else
141 			reg = LED_N_OFF_H(pwm->hwpwm);
142 
143 		regmap_write(pca->regmap, reg, LED_FULL);
144 
145 		return 0;
146 	}
147 
148 	if (duty_ns == period_ns) {
149 		/* Clear both OFF registers */
150 		if (pwm->hwpwm >= PCA9685_MAXCHAN)
151 			reg = PCA9685_ALL_LED_OFF_L;
152 		else
153 			reg = LED_N_OFF_L(pwm->hwpwm);
154 
155 		regmap_write(pca->regmap, reg, 0x0);
156 
157 		if (pwm->hwpwm >= PCA9685_MAXCHAN)
158 			reg = PCA9685_ALL_LED_OFF_H;
159 		else
160 			reg = LED_N_OFF_H(pwm->hwpwm);
161 
162 		regmap_write(pca->regmap, reg, 0x0);
163 
164 		/* Set the full ON bit */
165 		if (pwm->hwpwm >= PCA9685_MAXCHAN)
166 			reg = PCA9685_ALL_LED_ON_H;
167 		else
168 			reg = LED_N_ON_H(pwm->hwpwm);
169 
170 		regmap_write(pca->regmap, reg, LED_FULL);
171 
172 		return 0;
173 	}
174 
175 	duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
176 	duty = DIV_ROUND_UP_ULL(duty, period_ns);
177 
178 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
179 		reg = PCA9685_ALL_LED_OFF_L;
180 	else
181 		reg = LED_N_OFF_L(pwm->hwpwm);
182 
183 	regmap_write(pca->regmap, reg, (int)duty & 0xff);
184 
185 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
186 		reg = PCA9685_ALL_LED_OFF_H;
187 	else
188 		reg = LED_N_OFF_H(pwm->hwpwm);
189 
190 	regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
191 
192 	/* Clear the full ON bit, otherwise the set OFF time has no effect */
193 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
194 		reg = PCA9685_ALL_LED_ON_H;
195 	else
196 		reg = LED_N_ON_H(pwm->hwpwm);
197 
198 	regmap_write(pca->regmap, reg, 0);
199 
200 	return 0;
201 }
202 
203 static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
204 {
205 	struct pca9685 *pca = to_pca(chip);
206 	unsigned int reg;
207 
208 	/*
209 	 * The PWM subsystem does not support a pre-delay.
210 	 * So, set the ON-timeout to 0
211 	 */
212 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
213 		reg = PCA9685_ALL_LED_ON_L;
214 	else
215 		reg = LED_N_ON_L(pwm->hwpwm);
216 
217 	regmap_write(pca->regmap, reg, 0);
218 
219 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
220 		reg = PCA9685_ALL_LED_ON_H;
221 	else
222 		reg = LED_N_ON_H(pwm->hwpwm);
223 
224 	regmap_write(pca->regmap, reg, 0);
225 
226 	/*
227 	 * Clear the full-off bit.
228 	 * It has precedence over the others and must be off.
229 	 */
230 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
231 		reg = PCA9685_ALL_LED_OFF_H;
232 	else
233 		reg = LED_N_OFF_H(pwm->hwpwm);
234 
235 	regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
236 
237 	return 0;
238 }
239 
240 static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
241 {
242 	struct pca9685 *pca = to_pca(chip);
243 	unsigned int reg;
244 
245 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
246 		reg = PCA9685_ALL_LED_OFF_H;
247 	else
248 		reg = LED_N_OFF_H(pwm->hwpwm);
249 
250 	regmap_write(pca->regmap, reg, LED_FULL);
251 
252 	/* Clear the LED_OFF counter. */
253 	if (pwm->hwpwm >= PCA9685_MAXCHAN)
254 		reg = PCA9685_ALL_LED_OFF_L;
255 	else
256 		reg = LED_N_OFF_L(pwm->hwpwm);
257 
258 	regmap_write(pca->regmap, reg, 0x0);
259 }
260 
261 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
262 {
263 	struct pca9685 *pca = to_pca(chip);
264 
265 	if (pca->active_cnt++ == 0)
266 		return regmap_update_bits(pca->regmap, PCA9685_MODE1,
267 					  MODE1_SLEEP, 0x0);
268 
269 	return 0;
270 }
271 
272 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
273 {
274 	struct pca9685 *pca = to_pca(chip);
275 
276 	if (--pca->active_cnt == 0)
277 		regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
278 				   MODE1_SLEEP);
279 }
280 
281 static const struct pwm_ops pca9685_pwm_ops = {
282 	.enable = pca9685_pwm_enable,
283 	.disable = pca9685_pwm_disable,
284 	.config = pca9685_pwm_config,
285 	.request = pca9685_pwm_request,
286 	.free = pca9685_pwm_free,
287 	.owner = THIS_MODULE,
288 };
289 
290 static const struct regmap_config pca9685_regmap_i2c_config = {
291 	.reg_bits = 8,
292 	.val_bits = 8,
293 	.max_register = PCA9685_NUMREGS,
294 	.cache_type = REGCACHE_NONE,
295 };
296 
297 static int pca9685_pwm_probe(struct i2c_client *client,
298 				const struct i2c_device_id *id)
299 {
300 	struct device_node *np = client->dev.of_node;
301 	struct pca9685 *pca;
302 	int ret;
303 	int mode2;
304 
305 	pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
306 	if (!pca)
307 		return -ENOMEM;
308 
309 	pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
310 	if (IS_ERR(pca->regmap)) {
311 		ret = PTR_ERR(pca->regmap);
312 		dev_err(&client->dev, "Failed to initialize register map: %d\n",
313 			ret);
314 		return ret;
315 	}
316 	pca->duty_ns = 0;
317 	pca->period_ns = PCA9685_DEFAULT_PERIOD;
318 
319 	i2c_set_clientdata(client, pca);
320 
321 	regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
322 
323 	if (of_property_read_bool(np, "invert"))
324 		mode2 |= MODE2_INVRT;
325 	else
326 		mode2 &= ~MODE2_INVRT;
327 
328 	if (of_property_read_bool(np, "open-drain"))
329 		mode2 &= ~MODE2_OUTDRV;
330 	else
331 		mode2 |= MODE2_OUTDRV;
332 
333 	regmap_write(pca->regmap, PCA9685_MODE2, mode2);
334 
335 	/* clear all "full off" bits */
336 	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
337 	regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
338 
339 	pca->chip.ops = &pca9685_pwm_ops;
340 	/* add an extra channel for ALL_LED */
341 	pca->chip.npwm = PCA9685_MAXCHAN + 1;
342 
343 	pca->chip.dev = &client->dev;
344 	pca->chip.base = -1;
345 	pca->chip.can_sleep = true;
346 
347 	return pwmchip_add(&pca->chip);
348 }
349 
350 static int pca9685_pwm_remove(struct i2c_client *client)
351 {
352 	struct pca9685 *pca = i2c_get_clientdata(client);
353 
354 	regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
355 			   MODE1_SLEEP);
356 
357 	return pwmchip_remove(&pca->chip);
358 }
359 
360 static const struct i2c_device_id pca9685_id[] = {
361 	{ "pca9685", 0 },
362 	{ /* sentinel */ },
363 };
364 MODULE_DEVICE_TABLE(i2c, pca9685_id);
365 
366 static const struct of_device_id pca9685_dt_ids[] = {
367 	{ .compatible = "nxp,pca9685-pwm", },
368 	{ /* sentinel */ }
369 };
370 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
371 
372 static struct i2c_driver pca9685_i2c_driver = {
373 	.driver = {
374 		.name = "pca9685-pwm",
375 		.of_match_table = pca9685_dt_ids,
376 	},
377 	.probe = pca9685_pwm_probe,
378 	.remove = pca9685_pwm_remove,
379 	.id_table = pca9685_id,
380 };
381 
382 module_i2c_driver(pca9685_i2c_driver);
383 
384 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
385 MODULE_DESCRIPTION("PWM driver for PCA9685");
386 MODULE_LICENSE("GPL");
387