blob: 29d1bba4804ec95fa7c249ebbe6ad3ea057fe08a [file] [log] [blame]
Peter Ujfalusiaa765642012-11-27 11:09:58 +01001/*
2 * Driver for TWL4030/6030 Pulse Width Modulator used as LED driver
3 *
4 * Copyright (C) 2012 Texas Instruments
5 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
6 *
7 * This driver is a complete rewrite of the former pwm-twl6030.c authorded by:
8 * Hemanth V <hemanthv@ti.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published by
12 * the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <linux/module.h>
24#include <linux/platform_device.h>
25#include <linux/pwm.h>
26#include <linux/i2c/twl.h>
27#include <linux/slab.h>
28
29/*
30 * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
31 * To generate the signal on TWL4030:
32 * - LEDA uses PWMA
33 * - LEDB uses PWMB
34 * TWL6030 has one LED pin with dedicated LEDPWM
35 */
36
37#define TWL4030_LED_MAX 0x7f
38#define TWL6030_LED_MAX 0xff
39
40/* Registers, bits and macro for TWL4030 */
41#define TWL4030_LEDEN_REG 0x00
42#define TWL4030_PWMA_REG 0x01
43
44#define TWL4030_LEDXON (1 << 0)
45#define TWL4030_LEDXPWM (1 << 4)
46#define TWL4030_LED_PINS (TWL4030_LEDXON | TWL4030_LEDXPWM)
47#define TWL4030_LED_TOGGLE(led, x) ((x) << (led))
48
49/* Register, bits and macro for TWL6030 */
50#define TWL6030_LED_PWM_CTRL1 0xf4
51#define TWL6030_LED_PWM_CTRL2 0xf5
52
53#define TWL6040_LED_MODE_HW 0x00
54#define TWL6040_LED_MODE_ON 0x01
55#define TWL6040_LED_MODE_OFF 0x02
56#define TWL6040_LED_MODE_MASK 0x03
57
58struct twl_pwmled_chip {
59 struct pwm_chip chip;
60 struct mutex mutex;
61};
62
63static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
64{
65 return container_of(chip, struct twl_pwmled_chip, chip);
66}
67
68static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
69 int duty_ns, int period_ns)
70{
71 int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
72 u8 pwm_config[2] = { 1, 0 };
73 int base, ret;
74
75 /*
76 * To configure the duty period:
77 * On-cycle is set to 1 (the minimum allowed value)
78 * The off time of 0 is not configurable, so the mapping is:
79 * 0 -> off cycle = 2,
80 * 1 -> off cycle = 2,
81 * 2 -> off cycle = 3,
82 * 126 - > off cycle 127,
83 * 127 - > off cycle 1
84 * When on cycle == off cycle the PWM will be always on
85 */
86 if (duty_cycle == 1)
87 duty_cycle = 2;
88 else if (duty_cycle > TWL4030_LED_MAX)
89 duty_cycle = 1;
90
91 base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
92
93 pwm_config[1] = duty_cycle;
94
95 ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
96 if (ret < 0)
97 dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
98
99 return ret;
100}
101
102static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
103{
104 struct twl_pwmled_chip *twl = to_twl(chip);
105 int ret;
106 u8 val;
107
108 mutex_lock(&twl->mutex);
109 ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
110 if (ret < 0) {
111 dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
112 goto out;
113 }
114
115 val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
116
117 ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
118 if (ret < 0)
119 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
120
121out:
122 mutex_unlock(&twl->mutex);
123 return ret;
124}
125
126static void twl4030_pwmled_disable(struct pwm_chip *chip,
127 struct pwm_device *pwm)
128{
129 struct twl_pwmled_chip *twl = to_twl(chip);
130 int ret;
131 u8 val;
132
133 mutex_lock(&twl->mutex);
134 ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
135 if (ret < 0) {
136 dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
137 goto out;
138 }
139
140 val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
141
142 ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
143 if (ret < 0)
144 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
145
146out:
147 mutex_unlock(&twl->mutex);
148}
149
150static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
151 int duty_ns, int period_ns)
152{
153 int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
154 u8 on_time;
155 int ret;
156
157 on_time = duty_cycle & 0xff;
158
159 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
160 TWL6030_LED_PWM_CTRL1);
161 if (ret < 0)
162 dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
163
164 return ret;
165}
166
167static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
168{
169 struct twl_pwmled_chip *twl = to_twl(chip);
170 int ret;
171 u8 val;
172
173 mutex_lock(&twl->mutex);
174 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
175 if (ret < 0) {
176 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
177 pwm->label);
178 goto out;
179 }
180
181 val &= ~TWL6040_LED_MODE_MASK;
182 val |= TWL6040_LED_MODE_ON;
183
184 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
185 if (ret < 0)
186 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
187
188out:
189 mutex_unlock(&twl->mutex);
190 return ret;
191}
192
193static void twl6030_pwmled_disable(struct pwm_chip *chip,
194 struct pwm_device *pwm)
195{
196 struct twl_pwmled_chip *twl = to_twl(chip);
197 int ret;
198 u8 val;
199
200 mutex_lock(&twl->mutex);
201 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
202 if (ret < 0) {
203 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
204 pwm->label);
205 goto out;
206 }
207
208 val &= ~TWL6040_LED_MODE_MASK;
209 val |= TWL6040_LED_MODE_OFF;
210
211 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
212 if (ret < 0)
213 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
214
215out:
216 mutex_unlock(&twl->mutex);
217}
218
219static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
220{
221 struct twl_pwmled_chip *twl = to_twl(chip);
222 int ret;
223 u8 val;
224
225 mutex_lock(&twl->mutex);
226 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
227 if (ret < 0) {
228 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
229 pwm->label);
230 goto out;
231 }
232
233 val &= ~TWL6040_LED_MODE_MASK;
234 val |= TWL6040_LED_MODE_OFF;
235
236 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
237 if (ret < 0)
238 dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
239
240out:
241 mutex_unlock(&twl->mutex);
242 return ret;
243}
244
245static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
246{
247 struct twl_pwmled_chip *twl = to_twl(chip);
248 int ret;
249 u8 val;
250
251 mutex_lock(&twl->mutex);
252 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
253 if (ret < 0) {
254 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
255 pwm->label);
256 goto out;
257 }
258
259 val &= ~TWL6040_LED_MODE_MASK;
260 val |= TWL6040_LED_MODE_HW;
261
262 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
263 if (ret < 0)
264 dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
265
266out:
267 mutex_unlock(&twl->mutex);
268}
269
270static const struct pwm_ops twl4030_pwmled_ops = {
271 .enable = twl4030_pwmled_enable,
272 .disable = twl4030_pwmled_disable,
273 .config = twl4030_pwmled_config,
Axel Lin7fa25312013-03-31 11:16:14 +0800274 .owner = THIS_MODULE,
Peter Ujfalusiaa765642012-11-27 11:09:58 +0100275};
276
277static const struct pwm_ops twl6030_pwmled_ops = {
278 .enable = twl6030_pwmled_enable,
279 .disable = twl6030_pwmled_disable,
280 .config = twl6030_pwmled_config,
281 .request = twl6030_pwmled_request,
282 .free = twl6030_pwmled_free,
Axel Lin7fa25312013-03-31 11:16:14 +0800283 .owner = THIS_MODULE,
Peter Ujfalusiaa765642012-11-27 11:09:58 +0100284};
285
286static int twl_pwmled_probe(struct platform_device *pdev)
287{
288 struct twl_pwmled_chip *twl;
289 int ret;
290
291 twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
292 if (!twl)
293 return -ENOMEM;
294
295 if (twl_class_is_4030()) {
296 twl->chip.ops = &twl4030_pwmled_ops;
297 twl->chip.npwm = 2;
298 } else {
299 twl->chip.ops = &twl6030_pwmled_ops;
300 twl->chip.npwm = 1;
301 }
302
303 twl->chip.dev = &pdev->dev;
304 twl->chip.base = -1;
Florian Vaussard2e2a0f62013-01-28 15:00:58 +0100305 twl->chip.can_sleep = true;
Peter Ujfalusiaa765642012-11-27 11:09:58 +0100306
307 mutex_init(&twl->mutex);
308
309 ret = pwmchip_add(&twl->chip);
310 if (ret < 0)
311 return ret;
312
313 platform_set_drvdata(pdev, twl);
314
315 return 0;
316}
317
318static int twl_pwmled_remove(struct platform_device *pdev)
319{
320 struct twl_pwmled_chip *twl = platform_get_drvdata(pdev);
321
322 return pwmchip_remove(&twl->chip);
323}
324
325#ifdef CONFIG_OF
Thierry Redingf1a88702013-04-18 10:04:14 +0200326static const struct of_device_id twl_pwmled_of_match[] = {
Peter Ujfalusiaa765642012-11-27 11:09:58 +0100327 { .compatible = "ti,twl4030-pwmled" },
328 { .compatible = "ti,twl6030-pwmled" },
329 { },
330};
331MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
332#endif
333
334static struct platform_driver twl_pwmled_driver = {
335 .driver = {
336 .name = "twl-pwmled",
337 .of_match_table = of_match_ptr(twl_pwmled_of_match),
338 },
339 .probe = twl_pwmled_probe,
340 .remove = twl_pwmled_remove,
341};
342module_platform_driver(twl_pwmled_driver);
343
344MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
345MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
346MODULE_ALIAS("platform:twl-pwmled");
347MODULE_LICENSE("GPL");