blob: 0d17d99b749b6cfe43f7b3e3e8f17be3acd38f7a [file] [log] [blame]
Steffen Trumtrar88b613e2013-05-30 09:50:12 +02001/*
2 * Driver for PCA9685 16-channel 12-bit PWM LED controller
3 *
4 * Copyright (C) 2013 Steffen Trumtrar <s.trumtrar@pengutronix.de>
5 *
6 * based on the pwm-twl-led.c driver
7 *
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.
11 *
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.
16 *
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/>.
19 */
20
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>
27
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
38
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
44
45#define PCA9685_NUMREGS 0xFF
46#define PCA9685_MAXCHAN 0x10
47
48#define LED_FULL (1 << 4)
49#define MODE1_SLEEP (1 << 4)
50#define MODE2_INVRT (1 << 4)
51#define MODE2_OUTDRV (1 << 2)
52
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)))
57
58struct pca9685 {
59 struct pwm_chip chip;
60 struct regmap *regmap;
61 int active_cnt;
62};
63
64static inline struct pca9685 *to_pca(struct pwm_chip *chip)
65{
66 return container_of(chip, struct pca9685, chip);
67}
68
69static int pca9685_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
70 int duty_ns, int period_ns)
71{
72 struct pca9685 *pca = to_pca(chip);
73 unsigned long long duty;
74 unsigned int reg;
75
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);
81
82 regmap_write(pca->regmap, reg, LED_FULL);
83
84 return 0;
85 }
86
87 if (duty_ns == period_ns) {
Clemens Gruber4a627b52015-07-23 17:19:01 +020088 /* Clear both OFF registers */
89 if (pwm->hwpwm >= PCA9685_MAXCHAN)
90 reg = PCA9685_ALL_LED_OFF_L;
91 else
92 reg = LED_N_OFF_L(pwm->hwpwm);
93
94 regmap_write(pca->regmap, reg, 0x0);
95
96 if (pwm->hwpwm >= PCA9685_MAXCHAN)
97 reg = PCA9685_ALL_LED_OFF_H;
98 else
99 reg = LED_N_OFF_H(pwm->hwpwm);
100
101 regmap_write(pca->regmap, reg, 0x0);
102
103 /* Set the full ON bit */
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200104 if (pwm->hwpwm >= PCA9685_MAXCHAN)
105 reg = PCA9685_ALL_LED_ON_H;
106 else
107 reg = LED_N_ON_H(pwm->hwpwm);
108
109 regmap_write(pca->regmap, reg, LED_FULL);
110
111 return 0;
112 }
113
114 duty = 4096 * (unsigned long long)duty_ns;
115 duty = DIV_ROUND_UP_ULL(duty, period_ns);
116
117 if (pwm->hwpwm >= PCA9685_MAXCHAN)
118 reg = PCA9685_ALL_LED_OFF_L;
119 else
120 reg = LED_N_OFF_L(pwm->hwpwm);
121
122 regmap_write(pca->regmap, reg, (int)duty & 0xff);
123
124 if (pwm->hwpwm >= PCA9685_MAXCHAN)
125 reg = PCA9685_ALL_LED_OFF_H;
126 else
127 reg = LED_N_OFF_H(pwm->hwpwm);
128
129 regmap_write(pca->regmap, reg, ((int)duty >> 8) & 0xf);
130
Clemens Gruber4a627b52015-07-23 17:19:01 +0200131 /* Clear the full ON bit, otherwise the set OFF time has no effect */
132 if (pwm->hwpwm >= PCA9685_MAXCHAN)
133 reg = PCA9685_ALL_LED_ON_H;
134 else
135 reg = LED_N_ON_H(pwm->hwpwm);
136
137 regmap_write(pca->regmap, reg, 0);
138
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200139 return 0;
140}
141
142static int pca9685_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
143{
144 struct pca9685 *pca = to_pca(chip);
145 unsigned int reg;
146
147 /*
148 * The PWM subsystem does not support a pre-delay.
149 * So, set the ON-timeout to 0
150 */
151 if (pwm->hwpwm >= PCA9685_MAXCHAN)
152 reg = PCA9685_ALL_LED_ON_L;
153 else
154 reg = LED_N_ON_L(pwm->hwpwm);
155
156 regmap_write(pca->regmap, reg, 0);
157
158 if (pwm->hwpwm >= PCA9685_MAXCHAN)
159 reg = PCA9685_ALL_LED_ON_H;
160 else
161 reg = LED_N_ON_H(pwm->hwpwm);
162
163 regmap_write(pca->regmap, reg, 0);
164
165 /*
166 * Clear the full-off bit.
167 * It has precedence over the others and must be off.
168 */
169 if (pwm->hwpwm >= PCA9685_MAXCHAN)
170 reg = PCA9685_ALL_LED_OFF_H;
171 else
172 reg = LED_N_OFF_H(pwm->hwpwm);
173
174 regmap_update_bits(pca->regmap, reg, LED_FULL, 0x0);
175
176 return 0;
177}
178
179static void pca9685_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
180{
181 struct pca9685 *pca = to_pca(chip);
182 unsigned int reg;
183
184 if (pwm->hwpwm >= PCA9685_MAXCHAN)
185 reg = PCA9685_ALL_LED_OFF_H;
186 else
187 reg = LED_N_OFF_H(pwm->hwpwm);
188
189 regmap_write(pca->regmap, reg, LED_FULL);
190
191 /* Clear the LED_OFF counter. */
192 if (pwm->hwpwm >= PCA9685_MAXCHAN)
193 reg = PCA9685_ALL_LED_OFF_L;
194 else
195 reg = LED_N_OFF_L(pwm->hwpwm);
196
197 regmap_write(pca->regmap, reg, 0x0);
198}
199
200static int pca9685_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
201{
202 struct pca9685 *pca = to_pca(chip);
203
204 if (pca->active_cnt++ == 0)
205 return regmap_update_bits(pca->regmap, PCA9685_MODE1,
206 MODE1_SLEEP, 0x0);
207
208 return 0;
209}
210
211static void pca9685_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
212{
213 struct pca9685 *pca = to_pca(chip);
214
215 if (--pca->active_cnt == 0)
216 regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
Axel Linfed1bf82013-06-20 01:27:27 +0800217 MODE1_SLEEP);
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200218}
219
220static const struct pwm_ops pca9685_pwm_ops = {
221 .enable = pca9685_pwm_enable,
222 .disable = pca9685_pwm_disable,
223 .config = pca9685_pwm_config,
224 .request = pca9685_pwm_request,
225 .free = pca9685_pwm_free,
Thierry Reding3dd0a902013-06-12 13:18:29 +0200226 .owner = THIS_MODULE,
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200227};
228
Krzysztof Kozlowskic456fbb2015-02-24 10:40:24 +0100229static const struct regmap_config pca9685_regmap_i2c_config = {
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200230 .reg_bits = 8,
231 .val_bits = 8,
232 .max_register = PCA9685_NUMREGS,
233 .cache_type = REGCACHE_NONE,
234};
235
236static int pca9685_pwm_probe(struct i2c_client *client,
237 const struct i2c_device_id *id)
238{
239 struct device_node *np = client->dev.of_node;
240 struct pca9685 *pca;
241 int ret;
242 int mode2;
243
244 pca = devm_kzalloc(&client->dev, sizeof(*pca), GFP_KERNEL);
245 if (!pca)
246 return -ENOMEM;
247
248 pca->regmap = devm_regmap_init_i2c(client, &pca9685_regmap_i2c_config);
249 if (IS_ERR(pca->regmap)) {
250 ret = PTR_ERR(pca->regmap);
251 dev_err(&client->dev, "Failed to initialize register map: %d\n",
252 ret);
253 return ret;
254 }
255
256 i2c_set_clientdata(client, pca);
257
258 regmap_read(pca->regmap, PCA9685_MODE2, &mode2);
259
260 if (of_property_read_bool(np, "invert"))
261 mode2 |= MODE2_INVRT;
262 else
263 mode2 &= ~MODE2_INVRT;
264
265 if (of_property_read_bool(np, "open-drain"))
266 mode2 &= ~MODE2_OUTDRV;
267 else
268 mode2 |= MODE2_OUTDRV;
269
270 regmap_write(pca->regmap, PCA9685_MODE2, mode2);
271
272 /* clear all "full off" bits */
273 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_L, 0);
274 regmap_write(pca->regmap, PCA9685_ALL_LED_OFF_H, 0);
275
276 pca->chip.ops = &pca9685_pwm_ops;
277 /* add an extra channel for ALL_LED */
278 pca->chip.npwm = PCA9685_MAXCHAN + 1;
279
280 pca->chip.dev = &client->dev;
281 pca->chip.base = -1;
282 pca->chip.can_sleep = true;
283
284 return pwmchip_add(&pca->chip);
285}
286
287static int pca9685_pwm_remove(struct i2c_client *client)
288{
289 struct pca9685 *pca = i2c_get_clientdata(client);
290
Axel Linfed1bf82013-06-20 01:27:27 +0800291 regmap_update_bits(pca->regmap, PCA9685_MODE1, MODE1_SLEEP,
292 MODE1_SLEEP);
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200293
294 return pwmchip_remove(&pca->chip);
295}
296
297static const struct i2c_device_id pca9685_id[] = {
298 { "pca9685", 0 },
299 { /* sentinel */ },
300};
301MODULE_DEVICE_TABLE(i2c, pca9685_id);
302
303static const struct of_device_id pca9685_dt_ids[] = {
304 { .compatible = "nxp,pca9685-pwm", },
305 { /* sentinel */ }
306};
307MODULE_DEVICE_TABLE(of, pca9685_dt_ids);
308
309static struct i2c_driver pca9685_i2c_driver = {
310 .driver = {
311 .name = "pca9685-pwm",
Steffen Trumtrar88b613e2013-05-30 09:50:12 +0200312 .of_match_table = pca9685_dt_ids,
313 },
314 .probe = pca9685_pwm_probe,
315 .remove = pca9685_pwm_remove,
316 .id_table = pca9685_id,
317};
318
319module_i2c_driver(pca9685_i2c_driver);
320
321MODULE_AUTHOR("Steffen Trumtrar <s.trumtrar@pengutronix.de>");
322MODULE_DESCRIPTION("PWM driver for PCA9685");
323MODULE_LICENSE("GPL");