perf bench futex: Cache align the worker struct
[linux/fpc-iii.git] / drivers / pwm / pwm-pca9685.c
blob117fccf7934a73a3c4098d809ff72bb9bef8006a
1 /*
2 * Driver for PCA9685 16-channel 12-bit PWM LED controller
4 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
5 * Copyright (C) 2015 Clemens Gruber <clemens.gruber@pqgruber.com>
7 * based on the pwm-twl-led.c driver
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.
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.
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/>.
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>
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.
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
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
57 #define PCA9685_PRESCALE_MIN 0x03 /* => max. frequency of 1526 Hz */
58 #define PCA9685_PRESCALE_MAX 0xFF /* => min. frequency of 24 Hz */
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 */
64 #define PCA9685_NUMREGS 0xFF
65 #define PCA9685_MAXCHAN 0x10
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)
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)))
78 struct pca9685 {
79 struct pwm_chip chip;
80 struct regmap *regmap;
81 int active_cnt;
82 int duty_ns;
83 int period_ns;
86 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
88 return container_of(chip, struct pca9685, chip);
91 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
92 int duty_ns, int period_ns)
94 struct pca9685 *pca = to_pca(chip);
95 unsigned long long duty;
96 unsigned int reg;
97 int prescale;
99 if (period_ns != pca->period_ns) {
100 prescale = DIV_ROUND_CLOSEST(PCA9685_OSC_CLOCK_MHZ * period_ns,
101 PCA9685_COUNTER_RANGE * 1000) - 1;
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);
109 /* Change the chip-wide output frequency */
110 regmap_write(pca->regmap, PCA9685_PRESCALE, prescale);
112 /* Wake the chip up */
113 regmap_update_bits(pca->regmap, PCA9685_MODE1,
114 MODE1_SLEEP, 0x0);
116 /* Wait 500us for the oscillator to be back up */
117 udelay(500);
119 pca->period_ns = period_ns;
122 * If the duty cycle did not change, restart PWM with
123 * the same duty cycle to period ratio and return.
125 if (duty_ns == pca->duty_ns) {
126 regmap_update_bits(pca->regmap, PCA9685_MODE1,
127 MODE1_RESTART, 0x1);
128 return 0;
130 } else {
131 dev_err(chip->dev,
132 "prescaler not set: period out of bounds!\n");
133 return -EINVAL;
137 pca->duty_ns = duty_ns;
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);
145 regmap_write(pca->regmap, reg, LED_FULL);
147 return 0;
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);
157 regmap_write(pca->regmap, reg, 0x0);
159 if (pwm->hwpwm >= PCA9685_MAXCHAN)
160 reg = PCA9685_ALL_LED_OFF_H;
161 else
162 reg = LED_N_OFF_H(pwm->hwpwm);
164 regmap_write(pca->regmap, reg, 0x0);
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);
172 regmap_write(pca->regmap, reg, LED_FULL);
174 return 0;
177 duty = PCA9685_COUNTER_RANGE * (unsigned long long)duty_ns;
178 duty = DIV_ROUND_UP_ULL(duty, period_ns);
180 if (pwm->hwpwm >= PCA9685_MAXCHAN)
181 reg = PCA9685_ALL_LED_OFF_L;
182 else
183 reg = LED_N_OFF_L(pwm->hwpwm);
185 regmap_write(pca->regmap, reg, (int)duty & 0xff);
187 if (pwm->hwpwm >= PCA9685_MAXCHAN)
188 reg = PCA9685_ALL_LED_OFF_H;
189 else
190 reg = LED_N_OFF_H(pwm->hwpwm);
192 regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
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);
200 regmap_write(pca->regmap, reg, 0);
202 return 0;
205 static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
207 struct pca9685 *pca = to_pca(chip);
208 unsigned int reg;
211 * The PWM subsystem does not support a pre-delay.
212 * So, set the ON-timeout to 0
214 if (pwm->hwpwm >= PCA9685_MAXCHAN)
215 reg = PCA9685_ALL_LED_ON_L;
216 else
217 reg = LED_N_ON_L(pwm->hwpwm);
219 regmap_write(pca->regmap, reg, 0);
221 if (pwm->hwpwm >= PCA9685_MAXCHAN)
222 reg = PCA9685_ALL_LED_ON_H;
223 else
224 reg = LED_N_ON_H(pwm->hwpwm);
226 regmap_write(pca->regmap, reg, 0);
229 * Clear the full-off bit.
230 * It has precedence over the others and must be off.
232 if (pwm->hwpwm >= PCA9685_MAXCHAN)
233 reg = PCA9685_ALL_LED_OFF_H;
234 else
235 reg = LED_N_OFF_H(pwm->hwpwm);
237 regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
239 return 0;
242 static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
244 struct pca9685 *pca = to_pca(chip);
245 unsigned int reg;
247 if (pwm->hwpwm >= PCA9685_MAXCHAN)
248 reg = PCA9685_ALL_LED_OFF_H;
249 else
250 reg = LED_N_OFF_H(pwm->hwpwm);
252 regmap_write(pca->regmap, reg, LED_FULL);
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);
260 regmap_write(pca->regmap, reg, 0x0);
263 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
265 struct pca9685 *pca = to_pca(chip);
267 if (pca->active_cnt++ == 0)
268 return regmap_update_bits(pca->regmap, PCA9685_MODE1,
269 MODE1_SLEEP, 0x0);
271 return 0;
274 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
276 struct pca9685 *pca = to_pca(chip);
278 if (--pca->active_cnt == 0)
279 regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
280 MODE1_SLEEP);
283 static 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,
292 static 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,
299 static int pca9685_pwm_probe(struct i2c_client *client,
300 const struct i2c_device_id *id)
302 struct pca9685 *pca;
303 int ret;
304 int mode2;
306 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
307 if (!pca)
308 return -ENOMEM;
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;
317 pca->duty_ns = 0;
318 pca->period_ns = PCA9685_DEFAULT_PERIOD;
320 i2c_set_clientdata(client, pca);
322 regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
324 if (device_property_read_bool(&client->dev, "invert"))
325 mode2 |= MODE2_INVRT;
326 else
327 mode2 &= ~MODE2_INVRT;
329 if (device_property_read_bool(&client->dev, "open-drain"))
330 mode2 &= ~MODE2_OUTDRV;
331 else
332 mode2 |= MODE2_OUTDRV;
334 regmap_write(pca->regmap, PCA9685_MODE2, mode2);
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);
340 pca->chip.ops = &pca9685_pwm_ops;
341 /* add an extra channel for ALL_LED */
342 pca->chip.npwm = PCA9685_MAXCHAN + 1;
344 pca->chip.dev = &client->dev;
345 pca->chip.base = -1;
346 pca->chip.can_sleep = true;
348 return pwmchip_add(&pca->chip);
351 static int pca9685_pwm_remove(struct i2c_client *client)
353 struct pca9685 *pca = i2c_get_clientdata(client);
355 regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
356 MODE1_SLEEP);
358 return pwmchip_remove(&pca->chip);
361 static const struct i2c_device_id pca9685_id[] = {
362 { "pca9685", 0 },
363 { /* sentinel */ },
365 MODULE_DEVICE_TABLE(i2c, pca9685_id);
367 #ifdef CONFIG_ACPI
368 static const struct acpi_device_id pca9685_acpi_ids[] = {
369 { "INT3492", 0 },
370 { /* sentinel */ },
372 MODULE_DEVICE_TABLE(acpi, pca9685_acpi_ids);
373 #endif
375 #ifdef CONFIG_OF
376 static const struct of_device_id pca9685_dt_ids[] = {
377 { .compatible = "nxp,pca9685-pwm", },
378 { /* sentinel */ }
380 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
381 #endif
383 static 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),
389 .probe = pca9685_pwm_probe,
390 .remove = pca9685_pwm_remove,
391 .id_table = pca9685_id,
394 module_i2c_driver(pca9685_i2c_driver);
396 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
397 MODULE_DESCRIPTION("PWM driver for PCA9685");
398 MODULE_LICENSE("GPL");