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>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pwm.h>
16 #include <linux/mfd/twl.h>
17 #include <linux/slab.h>
20 * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
21 * To generate the signal on TWL4030:
24 * TWL6030 has one LED pin with dedicated LEDPWM
27 #define TWL4030_LED_MAX 0x7f
28 #define TWL6030_LED_MAX 0xff
30 /* Registers, bits and macro for TWL4030 */
31 #define TWL4030_LEDEN_REG 0x00
32 #define TWL4030_PWMA_REG 0x01
34 #define TWL4030_LEDXON (1 << 0)
35 #define TWL4030_LEDXPWM (1 << 4)
36 #define TWL4030_LED_PINS (TWL4030_LEDXON | TWL4030_LEDXPWM)
37 #define TWL4030_LED_TOGGLE(led, x) ((x) << (led))
39 /* Register, bits and macro for TWL6030 */
40 #define TWL6030_LED_PWM_CTRL1 0xf4
41 #define TWL6030_LED_PWM_CTRL2 0xf5
43 #define TWL6040_LED_MODE_HW 0x00
44 #define TWL6040_LED_MODE_ON 0x01
45 #define TWL6040_LED_MODE_OFF 0x02
46 #define TWL6040_LED_MODE_MASK 0x03
48 struct twl_pwmled_chip {
53 static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
55 return container_of(chip, struct twl_pwmled_chip, chip);
58 static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
59 int duty_ns, int period_ns)
61 int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
62 u8 pwm_config[2] = { 1, 0 };
66 * To configure the duty period:
67 * On-cycle is set to 1 (the minimum allowed value)
68 * The off time of 0 is not configurable, so the mapping is:
72 * 126 - > off cycle 127,
74 * When on cycle == off cycle the PWM will be always on
78 else if (duty_cycle > TWL4030_LED_MAX)
81 base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
83 pwm_config[1] = duty_cycle;
85 ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
87 dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
92 static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
94 struct twl_pwmled_chip *twl = to_twl(chip);
98 mutex_lock(&twl->mutex);
99 ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
101 dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
105 val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
107 ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
109 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
112 mutex_unlock(&twl->mutex);
116 static void twl4030_pwmled_disable(struct pwm_chip *chip,
117 struct pwm_device *pwm)
119 struct twl_pwmled_chip *twl = to_twl(chip);
123 mutex_lock(&twl->mutex);
124 ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
126 dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
130 val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
132 ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
134 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
137 mutex_unlock(&twl->mutex);
140 static int twl4030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
141 const struct pwm_state *state)
145 if (state->polarity != PWM_POLARITY_NORMAL)
148 if (!state->enabled) {
149 if (pwm->state.enabled)
150 twl4030_pwmled_disable(chip, pwm);
156 * We cannot skip calling ->config even if state->period ==
157 * pwm->state.period && state->duty_cycle == pwm->state.duty_cycle
158 * because we might have exited early in the last call to
159 * pwm_apply_state because of !state->enabled and so the two values in
160 * pwm->state might not be configured in hardware.
162 ret = twl4030_pwmled_config(pwm->chip, pwm,
163 state->duty_cycle, state->period);
167 if (!pwm->state.enabled)
168 ret = twl4030_pwmled_enable(chip, pwm);
174 static const struct pwm_ops twl4030_pwmled_ops = {
175 .apply = twl4030_pwmled_apply,
176 .owner = THIS_MODULE,
179 static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
180 int duty_ns, int period_ns)
182 int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
186 on_time = duty_cycle & 0xff;
188 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
189 TWL6030_LED_PWM_CTRL1);
191 dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
196 static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
198 struct twl_pwmled_chip *twl = to_twl(chip);
202 mutex_lock(&twl->mutex);
203 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
205 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
210 val &= ~TWL6040_LED_MODE_MASK;
211 val |= TWL6040_LED_MODE_ON;
213 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
215 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
218 mutex_unlock(&twl->mutex);
222 static void twl6030_pwmled_disable(struct pwm_chip *chip,
223 struct pwm_device *pwm)
225 struct twl_pwmled_chip *twl = to_twl(chip);
229 mutex_lock(&twl->mutex);
230 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
232 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
237 val &= ~TWL6040_LED_MODE_MASK;
238 val |= TWL6040_LED_MODE_OFF;
240 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
242 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
245 mutex_unlock(&twl->mutex);
248 static int twl6030_pwmled_apply(struct pwm_chip *chip, struct pwm_device *pwm,
249 const struct pwm_state *state)
253 if (state->polarity != pwm->state.polarity)
256 if (!state->enabled) {
257 if (pwm->state.enabled)
258 twl6030_pwmled_disable(chip, pwm);
263 err = twl6030_pwmled_config(pwm->chip, pwm,
264 state->duty_cycle, state->period);
268 if (!pwm->state.enabled)
269 err = twl6030_pwmled_enable(chip, pwm);
274 static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
276 struct twl_pwmled_chip *twl = to_twl(chip);
280 mutex_lock(&twl->mutex);
281 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
283 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
288 val &= ~TWL6040_LED_MODE_MASK;
289 val |= TWL6040_LED_MODE_OFF;
291 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
293 dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
296 mutex_unlock(&twl->mutex);
300 static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
302 struct twl_pwmled_chip *twl = to_twl(chip);
306 mutex_lock(&twl->mutex);
307 ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
309 dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
314 val &= ~TWL6040_LED_MODE_MASK;
315 val |= TWL6040_LED_MODE_HW;
317 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
319 dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
322 mutex_unlock(&twl->mutex);
325 static const struct pwm_ops twl6030_pwmled_ops = {
326 .apply = twl6030_pwmled_apply,
327 .request = twl6030_pwmled_request,
328 .free = twl6030_pwmled_free,
329 .owner = THIS_MODULE,
332 static int twl_pwmled_probe(struct platform_device *pdev)
334 struct twl_pwmled_chip *twl;
336 twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
340 if (twl_class_is_4030()) {
341 twl->chip.ops = &twl4030_pwmled_ops;
344 twl->chip.ops = &twl6030_pwmled_ops;
348 twl->chip.dev = &pdev->dev;
350 mutex_init(&twl->mutex);
352 return devm_pwmchip_add(&pdev->dev, &twl->chip);
356 static const struct of_device_id twl_pwmled_of_match[] = {
357 { .compatible = "ti,twl4030-pwmled" },
358 { .compatible = "ti,twl6030-pwmled" },
361 MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
364 static struct platform_driver twl_pwmled_driver = {
366 .name = "twl-pwmled",
367 .of_match_table = of_match_ptr(twl_pwmled_of_match),
369 .probe = twl_pwmled_probe,
371 module_platform_driver(twl_pwmled_driver);
373 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
374 MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
375 MODULE_ALIAS("platform:twl-pwmled");
376 MODULE_LICENSE("GPL");