1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for TWL4030/6030 Pulse Width Modulator used as LED driver
5 * Copyright (C) 2012 Texas Instruments
6 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
8 * This driver is a complete rewrite of the former pwm-twl6030.c authorded by:
9 * Hemanth V <hemanthv@ti.com>
11 * Reference manual for the twl6030 is available at:
12 * https://www.ti.com/lit/ds/symlink/twl6030.pdf
15 * - The twl6030 hardware only supports two period lengths (128 clock ticks and
16 * 64 clock ticks), the driver only uses 128 ticks
17 * - The hardware doesn't support ON = 0, so the active part of a period doesn't
18 * start at its beginning.
19 * - The hardware could support inverted polarity (with a similar limitation as
20 * for normal: the last clock tick is always inactive).
21 * - The hardware emits a constant low output when disabled.
22 * - A request for .duty_cycle = 0 results in an output wave with one active
23 * clock tick per period. This should better use the disabled state.
24 * - The driver only implements setting the relative duty cycle.
25 * - The driver doesn't implement .get_state().
28 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/pwm.h>
32 #include <linux/mfd/twl.h>
33 #include <linux/slab.h>
36 * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
37 * To generate the signal on TWL4030:
40 * TWL6030 has one LED pin with dedicated LEDPWM
43 #define TWL4030_LED_MAX 0x7f
44 #define TWL6030_LED_MAX 0xff
46 /* Registers, bits and macro for TWL4030 */
47 #define TWL4030_LEDEN_REG 0x00
48 #define TWL4030_PWMA_REG 0x01
50 #define TWL4030_LEDXON (1 << 0)
51 #define TWL4030_LEDXPWM (1 << 4)
52 #define TWL4030_LED_PINS (TWL4030_LEDXON | TWL4030_LEDXPWM)
53 #define TWL4030_LED_TOGGLE(led, x) ((x) << (led))
55 /* Register, bits and macro for TWL6030 */
56 #define TWL6030_LED_PWM_CTRL1 0xf4
57 #define TWL6030_LED_PWM_CTRL2 0xf5
59 #define TWL6040_LED_MODE_HW 0x00
60 #define TWL6040_LED_MODE_ON 0x01
61 #define TWL6040_LED_MODE_OFF 0x02
62 #define TWL6040_LED_MODE_MASK 0x03
64 struct twl_pwmled_chip
{
68 static inline struct twl_pwmled_chip
*to_twl(struct pwm_chip
*chip
)
70 return pwmchip_get_drvdata(chip
);
73 static int twl4030_pwmled_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
74 int duty_ns
, int period_ns
)
76 int duty_cycle
= DIV_ROUND_UP(duty_ns
* TWL4030_LED_MAX
, period_ns
) + 1;
77 u8 pwm_config
[2] = { 1, 0 };
81 * To configure the duty period:
82 * On-cycle is set to 1 (the minimum allowed value)
83 * The off time of 0 is not configurable, so the mapping is:
87 * 126 - > off cycle 127,
89 * When on cycle == off cycle the PWM will be always on
93 else if (duty_cycle
> TWL4030_LED_MAX
)
96 base
= pwm
->hwpwm
* 2 + TWL4030_PWMA_REG
;
98 pwm_config
[1] = duty_cycle
;
100 ret
= twl_i2c_write(TWL4030_MODULE_LED
, pwm_config
, base
, 2);
102 dev_err(pwmchip_parent(chip
), "%s: Failed to configure PWM\n", pwm
->label
);
107 static int twl4030_pwmled_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
109 struct twl_pwmled_chip
*twl
= to_twl(chip
);
113 mutex_lock(&twl
->mutex
);
114 ret
= twl_i2c_read_u8(TWL4030_MODULE_LED
, &val
, TWL4030_LEDEN_REG
);
116 dev_err(pwmchip_parent(chip
), "%s: Failed to read LEDEN\n", pwm
->label
);
120 val
|= TWL4030_LED_TOGGLE(pwm
->hwpwm
, TWL4030_LED_PINS
);
122 ret
= twl_i2c_write_u8(TWL4030_MODULE_LED
, val
, TWL4030_LEDEN_REG
);
124 dev_err(pwmchip_parent(chip
), "%s: Failed to enable PWM\n", pwm
->label
);
127 mutex_unlock(&twl
->mutex
);
131 static void twl4030_pwmled_disable(struct pwm_chip
*chip
,
132 struct pwm_device
*pwm
)
134 struct twl_pwmled_chip
*twl
= to_twl(chip
);
138 mutex_lock(&twl
->mutex
);
139 ret
= twl_i2c_read_u8(TWL4030_MODULE_LED
, &val
, TWL4030_LEDEN_REG
);
141 dev_err(pwmchip_parent(chip
), "%s: Failed to read LEDEN\n", pwm
->label
);
145 val
&= ~TWL4030_LED_TOGGLE(pwm
->hwpwm
, TWL4030_LED_PINS
);
147 ret
= twl_i2c_write_u8(TWL4030_MODULE_LED
, val
, TWL4030_LEDEN_REG
);
149 dev_err(pwmchip_parent(chip
), "%s: Failed to disable PWM\n", pwm
->label
);
152 mutex_unlock(&twl
->mutex
);
155 static int twl4030_pwmled_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
156 const struct pwm_state
*state
)
160 if (state
->polarity
!= PWM_POLARITY_NORMAL
)
163 if (!state
->enabled
) {
164 if (pwm
->state
.enabled
)
165 twl4030_pwmled_disable(chip
, pwm
);
171 * We cannot skip calling ->config even if state->period ==
172 * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
173 * because we might have exited early in the last call to
174 * pwm_apply_might_sleep because of !state->enabled and so the two values in
175 * pwm->state might not be configured in hardware.
177 ret
= twl4030_pwmled_config(chip
, pwm
,
178 state
->duty_cycle
, state
->period
);
182 if (!pwm
->state
.enabled
)
183 ret
= twl4030_pwmled_enable(chip
, pwm
);
189 static const struct pwm_ops twl4030_pwmled_ops
= {
190 .apply
= twl4030_pwmled_apply
,
193 static int twl6030_pwmled_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
194 int duty_ns
, int period_ns
)
196 int duty_cycle
= (duty_ns
* TWL6030_LED_MAX
) / period_ns
;
200 on_time
= duty_cycle
& 0xff;
202 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, on_time
,
203 TWL6030_LED_PWM_CTRL1
);
205 dev_err(pwmchip_parent(chip
), "%s: Failed to configure PWM\n", pwm
->label
);
210 static int twl6030_pwmled_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
212 struct twl_pwmled_chip
*twl
= to_twl(chip
);
216 mutex_lock(&twl
->mutex
);
217 ret
= twl_i2c_read_u8(TWL6030_MODULE_ID1
, &val
, TWL6030_LED_PWM_CTRL2
);
219 dev_err(pwmchip_parent(chip
), "%s: Failed to read PWM_CTRL2\n",
224 val
&= ~TWL6040_LED_MODE_MASK
;
225 val
|= TWL6040_LED_MODE_ON
;
227 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_LED_PWM_CTRL2
);
229 dev_err(pwmchip_parent(chip
), "%s: Failed to enable PWM\n", pwm
->label
);
232 mutex_unlock(&twl
->mutex
);
236 static void twl6030_pwmled_disable(struct pwm_chip
*chip
,
237 struct pwm_device
*pwm
)
239 struct twl_pwmled_chip
*twl
= to_twl(chip
);
243 mutex_lock(&twl
->mutex
);
244 ret
= twl_i2c_read_u8(TWL6030_MODULE_ID1
, &val
, TWL6030_LED_PWM_CTRL2
);
246 dev_err(pwmchip_parent(chip
), "%s: Failed to read PWM_CTRL2\n",
251 val
&= ~TWL6040_LED_MODE_MASK
;
252 val
|= TWL6040_LED_MODE_OFF
;
254 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_LED_PWM_CTRL2
);
256 dev_err(pwmchip_parent(chip
), "%s: Failed to disable PWM\n", pwm
->label
);
259 mutex_unlock(&twl
->mutex
);
262 static int twl6030_pwmled_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
263 const struct pwm_state
*state
)
267 if (state
->polarity
!= pwm
->state
.polarity
)
270 if (!state
->enabled
) {
271 if (pwm
->state
.enabled
)
272 twl6030_pwmled_disable(chip
, pwm
);
277 err
= twl6030_pwmled_config(chip
, pwm
,
278 state
->duty_cycle
, state
->period
);
282 if (!pwm
->state
.enabled
)
283 err
= twl6030_pwmled_enable(chip
, pwm
);
288 static int twl6030_pwmled_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
290 struct twl_pwmled_chip
*twl
= to_twl(chip
);
294 mutex_lock(&twl
->mutex
);
295 ret
= twl_i2c_read_u8(TWL6030_MODULE_ID1
, &val
, TWL6030_LED_PWM_CTRL2
);
297 dev_err(pwmchip_parent(chip
), "%s: Failed to read PWM_CTRL2\n",
302 val
&= ~TWL6040_LED_MODE_MASK
;
303 val
|= TWL6040_LED_MODE_OFF
;
305 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_LED_PWM_CTRL2
);
307 dev_err(pwmchip_parent(chip
), "%s: Failed to request PWM\n", pwm
->label
);
310 mutex_unlock(&twl
->mutex
);
314 static void twl6030_pwmled_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
316 struct twl_pwmled_chip
*twl
= to_twl(chip
);
320 mutex_lock(&twl
->mutex
);
321 ret
= twl_i2c_read_u8(TWL6030_MODULE_ID1
, &val
, TWL6030_LED_PWM_CTRL2
);
323 dev_err(pwmchip_parent(chip
), "%s: Failed to read PWM_CTRL2\n",
328 val
&= ~TWL6040_LED_MODE_MASK
;
329 val
|= TWL6040_LED_MODE_HW
;
331 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_LED_PWM_CTRL2
);
333 dev_err(pwmchip_parent(chip
), "%s: Failed to free PWM\n", pwm
->label
);
336 mutex_unlock(&twl
->mutex
);
339 static const struct pwm_ops twl6030_pwmled_ops
= {
340 .apply
= twl6030_pwmled_apply
,
341 .request
= twl6030_pwmled_request
,
342 .free
= twl6030_pwmled_free
,
345 static int twl_pwmled_probe(struct platform_device
*pdev
)
347 struct pwm_chip
*chip
;
348 struct twl_pwmled_chip
*twl
;
350 const struct pwm_ops
*ops
;
352 if (twl_class_is_4030()) {
353 ops
= &twl4030_pwmled_ops
;
356 ops
= &twl6030_pwmled_ops
;
360 chip
= devm_pwmchip_alloc(&pdev
->dev
, npwm
, sizeof(*twl
));
362 return PTR_ERR(chip
);
367 mutex_init(&twl
->mutex
);
369 return devm_pwmchip_add(&pdev
->dev
, chip
);
373 static const struct of_device_id twl_pwmled_of_match
[] = {
374 { .compatible
= "ti,twl4030-pwmled" },
375 { .compatible
= "ti,twl6030-pwmled" },
378 MODULE_DEVICE_TABLE(of
, twl_pwmled_of_match
);
381 static struct platform_driver twl_pwmled_driver
= {
383 .name
= "twl-pwmled",
384 .of_match_table
= of_match_ptr(twl_pwmled_of_match
),
386 .probe
= twl_pwmled_probe
,
388 module_platform_driver(twl_pwmled_driver
);
390 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
391 MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
392 MODULE_ALIAS("platform:twl-pwmled");
393 MODULE_LICENSE("GPL");