Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / pwm / pwm-pca9685.c
blob3fb775ded0dff062e07f6d34e2f37f7e8bb93d1a
1 /*
2 * Driver for PCA9685 16-channel 12-bit PWM LED controller
4 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
6 * based on the pwm-twl-led.c driver
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <linux/i2c.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/pwm.h>
25 #include <linux/regmap.h>
26 #include <linux/slab.h>
28 #define PCA9685_MODE1 0x00
29 #define PCA9685_MODE2 0x01
30 #define PCA9685_SUBADDR1 0x02
31 #define PCA9685_SUBADDR2 0x03
32 #define PCA9685_SUBADDR3 0x04
33 #define PCA9685_ALLCALLADDR 0x05
34 #define PCA9685_LEDX_ON_L 0x06
35 #define PCA9685_LEDX_ON_H 0x07
36 #define PCA9685_LEDX_OFF_L 0x08
37 #define PCA9685_LEDX_OFF_H 0x09
39 #define PCA9685_ALL_LED_ON_L 0xFA
40 #define PCA9685_ALL_LED_ON_H 0xFB
41 #define PCA9685_ALL_LED_OFF_L 0xFC
42 #define PCA9685_ALL_LED_OFF_H 0xFD
43 #define PCA9685_PRESCALE 0xFE
45 #define PCA9685_NUMREGS 0xFF
46 #define PCA9685_MAXCHAN 0x10
48 #define LED_FULL (1 << 4)
49 #define MODE1_SLEEP (1 << 4)
50 #define MODE2_INVRT (1 << 4)
51 #define MODE2_OUTDRV (1 << 2)
53 #define LED_N_ON_H(N) (PCA9685_LEDX_ON_H + (4 * (N)))
54 #define LED_N_ON_L(N) (PCA9685_LEDX_ON_L + (4 * (N)))
55 #define LED_N_OFF_H(N) (PCA9685_LEDX_OFF_H + (4 * (N)))
56 #define LED_N_OFF_L(N) (PCA9685_LEDX_OFF_L + (4 * (N)))
58 struct pca9685 {
59 struct pwm_chip chip;
60 struct regmap *regmap;
61 int active_cnt;
64 static inline struct pca9685 *to_pca(struct pwm_chip *chip)
66 return container_of(chip, struct pca9685, chip);
69 static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
70 int duty_ns, int period_ns)
72 struct pca9685 *pca = to_pca(chip);
73 unsigned long long duty;
74 unsigned int reg;
76 if (duty_ns < 1) {
77 if (pwm->hwpwm >= PCA9685_MAXCHAN)
78 reg = PCA9685_ALL_LED_OFF_H;
79 else
80 reg = LED_N_OFF_H(pwm->hwpwm);
82 regmap_write(pca->regmap, reg, LED_FULL);
84 return 0;
87 if (duty_ns == period_ns) {
88 if (pwm->hwpwm >= PCA9685_MAXCHAN)
89 reg = PCA9685_ALL_LED_ON_H;
90 else
91 reg = LED_N_ON_H(pwm->hwpwm);
93 regmap_write(pca->regmap, reg, LED_FULL);
95 return 0;
98 duty = 4096 * (unsigned long long)duty_ns;
99 duty = DIV_ROUND_UP_ULL(duty, period_ns);
101 if (pwm->hwpwm >= PCA9685_MAXCHAN)
102 reg = PCA9685_ALL_LED_OFF_L;
103 else
104 reg = LED_N_OFF_L(pwm->hwpwm);
106 regmap_write(pca->regmap, reg, (int)duty & 0xff);
108 if (pwm->hwpwm >= PCA9685_MAXCHAN)
109 reg = PCA9685_ALL_LED_OFF_H;
110 else
111 reg = LED_N_OFF_H(pwm->hwpwm);
113 regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
115 return 0;
118 static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
120 struct pca9685 *pca = to_pca(chip);
121 unsigned int reg;
124 * The PWM subsystem does not support a pre-delay.
125 * So, set the ON-timeout to 0
127 if (pwm->hwpwm >= PCA9685_MAXCHAN)
128 reg = PCA9685_ALL_LED_ON_L;
129 else
130 reg = LED_N_ON_L(pwm->hwpwm);
132 regmap_write(pca->regmap, reg, 0);
134 if (pwm->hwpwm >= PCA9685_MAXCHAN)
135 reg = PCA9685_ALL_LED_ON_H;
136 else
137 reg = LED_N_ON_H(pwm->hwpwm);
139 regmap_write(pca->regmap, reg, 0);
142 * Clear the full-off bit.
143 * It has precedence over the others and must be off.
145 if (pwm->hwpwm >= PCA9685_MAXCHAN)
146 reg = PCA9685_ALL_LED_OFF_H;
147 else
148 reg = LED_N_OFF_H(pwm->hwpwm);
150 regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
152 return 0;
155 static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
157 struct pca9685 *pca = to_pca(chip);
158 unsigned int reg;
160 if (pwm->hwpwm >= PCA9685_MAXCHAN)
161 reg = PCA9685_ALL_LED_OFF_H;
162 else
163 reg = LED_N_OFF_H(pwm->hwpwm);
165 regmap_write(pca->regmap, reg, LED_FULL);
167 /* Clear the LED_OFF counter. */
168 if (pwm->hwpwm >= PCA9685_MAXCHAN)
169 reg = PCA9685_ALL_LED_OFF_L;
170 else
171 reg = LED_N_OFF_L(pwm->hwpwm);
173 regmap_write(pca->regmap, reg, 0x0);
176 static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
178 struct pca9685 *pca = to_pca(chip);
180 if (pca->active_cnt++ == 0)
181 return regmap_update_bits(pca->regmap, PCA9685_MODE1,
182 MODE1_SLEEP, 0x0);
184 return 0;
187 static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
189 struct pca9685 *pca = to_pca(chip);
191 if (--pca->active_cnt == 0)
192 regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
193 MODE1_SLEEP);
196 static const struct pwm_ops pca9685_pwm_ops = {
197 .enable = pca9685_pwm_enable,
198 .disable = pca9685_pwm_disable,
199 .config = pca9685_pwm_config,
200 .request = pca9685_pwm_request,
201 .free = pca9685_pwm_free,
202 .owner = THIS_MODULE,
205 static struct regmap_config pca9685_regmap_i2c_config = {
206 .reg_bits = 8,
207 .val_bits = 8,
208 .max_register = PCA9685_NUMREGS,
209 .cache_type = REGCACHE_NONE,
212 static int pca9685_pwm_probe(struct i2c_client *client,
213 const struct i2c_device_id *id)
215 struct device_node *np = client->dev.of_node;
216 struct pca9685 *pca;
217 int ret;
218 int mode2;
220 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
221 if (!pca)
222 return -ENOMEM;
224 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
225 if (IS_ERR(pca->regmap)) {
226 ret = PTR_ERR(pca->regmap);
227 dev_err(&client->dev, "Failed to initialize register map: %d\n",
228 ret);
229 return ret;
232 i2c_set_clientdata(client, pca);
234 regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
236 if (of_property_read_bool(np, "invert"))
237 mode2 |= MODE2_INVRT;
238 else
239 mode2 &= ~MODE2_INVRT;
241 if (of_property_read_bool(np, "open-drain"))
242 mode2 &= ~MODE2_OUTDRV;
243 else
244 mode2 |= MODE2_OUTDRV;
246 regmap_write(pca->regmap, PCA9685_MODE2, mode2);
248 /* clear all "full off" bits */
249 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
250 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
252 pca->chip.ops = &pca9685_pwm_ops;
253 /* add an extra channel for ALL_LED */
254 pca->chip.npwm = PCA9685_MAXCHAN + 1;
256 pca->chip.dev = &client->dev;
257 pca->chip.base = -1;
258 pca->chip.can_sleep = true;
260 return pwmchip_add(&pca->chip);
263 static int pca9685_pwm_remove(struct i2c_client *client)
265 struct pca9685 *pca = i2c_get_clientdata(client);
267 regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
268 MODE1_SLEEP);
270 return pwmchip_remove(&pca->chip);
273 static const struct i2c_device_id pca9685_id[] = {
274 { "pca9685", 0 },
275 { /* sentinel */ },
277 MODULE_DEVICE_TABLE(i2c, pca9685_id);
279 static const struct of_device_id pca9685_dt_ids[] = {
280 { .compatible = "nxp,pca9685-pwm", },
281 { /* sentinel */ }
283 MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
285 static struct i2c_driver pca9685_i2c_driver = {
286 .driver = {
287 .name = "pca9685-pwm",
288 .owner = THIS_MODULE,
289 .of_match_table = pca9685_dt_ids,
291 .probe = pca9685_pwm_probe,
292 .remove = pca9685_pwm_remove,
293 .id_table = pca9685_id,
296 module_i2c_driver(pca9685_i2c_driver);
298 MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
299 MODULE_DESCRIPTION("PWM driver for PCA9685");
300 MODULE_LICENSE("GPL");