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 {
69 static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
71 return container_of(chip, struct twl_pwmled_chip, chip);
74 static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
75 int duty_ns, int period_ns)
77 int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
78 u8 pwm_config[2] = { 1, 0 };
82 * To configure the duty period:
83 * On-cycle is set to 1 (the minimum allowed value)
84 * The off time of 0 is not configurable, so the mapping is:
88 * 126 - > off cycle 127,
90 * When on cycle == off cycle the PWM will be always on
94 else if (duty_cycle > TWL4030_LED_MAX)
97 base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
99 pwm_config[1] = duty_cycle;
101 ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
103 dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
108 static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
110 struct twl_pwmled_chip *twl = to_twl(chip);
114 mutex_lock(&twl->mutex);
115 ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
117 dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
121 val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
123 ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
125 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
128 mutex_unlock(&twl->mutex);
132 static void twl4030_pwmled_disable(struct pwm_chip *chip,
133 struct pwm_device *pwm)
135 struct twl_pwmled_chip *twl = to_twl(chip);
139 mutex_lock(&twl->mutex);
140 ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
142 dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
146 val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
148 ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
150 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
153 mutex_unlock(&twl->mutex);
156 static int twl4030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
157 const struct pwm_state *state)
161 if (state->polarity != PWM_POLARITY_NORMAL)
164 if (!state->enabled) {
165 if (pwm->state.enabled)
166 twl4030_pwmled_disable(chip, pwm);
172 * We cannot skip calling ->config even if state->period ==
173 * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
174 * because we might have exited early in the last call to
175 * pwm_apply_state because of !state->enabled and so the two values in
176 * pwm->state might not be configured in hardware.
178 ret = twl4030_pwmled_config(pwm->chip, pwm,
179 state->duty_cycle, state->period);
183 if (!pwm->state.enabled)
184 ret = twl4030_pwmled_enable(chip, pwm);
190 static const struct pwm_ops twl4030_pwmled_ops = {
191 .apply = twl4030_pwmled_apply,
192 .owner = THIS_MODULE,
195 static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
196 int duty_ns, int period_ns)
198 int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
202 on_time = duty_cycle & 0xff;
204 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
205 TWL6030_LED_PWM_CTRL1);
207 dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
212 static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
214 struct twl_pwmled_chip *twl = to_twl(chip);
218 mutex_lock(&twl->mutex);
219 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
221 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
226 val &= ~TWL6040_LED_MODE_MASK;
227 val |= TWL6040_LED_MODE_ON;
229 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
231 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
234 mutex_unlock(&twl->mutex);
238 static void twl6030_pwmled_disable(struct pwm_chip *chip,
239 struct pwm_device *pwm)
241 struct twl_pwmled_chip *twl = to_twl(chip);
245 mutex_lock(&twl->mutex);
246 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
248 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
253 val &= ~TWL6040_LED_MODE_MASK;
254 val |= TWL6040_LED_MODE_OFF;
256 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
258 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
261 mutex_unlock(&twl->mutex);
264 static int twl6030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
265 const struct pwm_state *state)
269 if (state->polarity != pwm->state.polarity)
272 if (!state->enabled) {
273 if (pwm->state.enabled)
274 twl6030_pwmled_disable(chip, pwm);
279 err = twl6030_pwmled_config(pwm->chip, pwm,
280 state->duty_cycle, state->period);
284 if (!pwm->state.enabled)
285 err = twl6030_pwmled_enable(chip, pwm);
290 static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
292 struct twl_pwmled_chip *twl = to_twl(chip);
296 mutex_lock(&twl->mutex);
297 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
299 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
304 val &= ~TWL6040_LED_MODE_MASK;
305 val |= TWL6040_LED_MODE_OFF;
307 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
309 dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
312 mutex_unlock(&twl->mutex);
316 static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
318 struct twl_pwmled_chip *twl = to_twl(chip);
322 mutex_lock(&twl->mutex);
323 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
325 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
330 val &= ~TWL6040_LED_MODE_MASK;
331 val |= TWL6040_LED_MODE_HW;
333 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
335 dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
338 mutex_unlock(&twl->mutex);
341 static const struct pwm_ops twl6030_pwmled_ops = {
342 .apply = twl6030_pwmled_apply,
343 .request = twl6030_pwmled_request,
344 .free = twl6030_pwmled_free,
345 .owner = THIS_MODULE,
348 static int twl_pwmled_probe(struct platform_device *pdev)
350 struct twl_pwmled_chip *twl;
352 twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
356 if (twl_class_is_4030()) {
357 twl->chip.ops = &twl4030_pwmled_ops;
360 twl->chip.ops = &twl6030_pwmled_ops;
364 twl->chip.dev = &pdev->dev;
366 mutex_init(&twl->mutex);
368 return devm_pwmchip_add(&pdev->dev, &twl->chip);
372 static const struct of_device_id twl_pwmled_of_match[] = {
373 { .compatible = "ti,twl4030-pwmled" },
374 { .compatible = "ti,twl6030-pwmled" },
377 MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
380 static struct platform_driver twl_pwmled_driver = {
382 .name = "twl-pwmled",
383 .of_match_table = of_match_ptr(twl_pwmled_of_match),
385 .probe = twl_pwmled_probe,
387 module_platform_driver(twl_pwmled_driver);
389 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
390 MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
391 MODULE_ALIAS("platform:twl-pwmled");
392 MODULE_LICENSE("GPL");