1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for TWL4030/6030 Generic Pulse Width Modulator
5 * Copyright (C) 2012 Texas Instruments
6 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
9 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pwm.h>
13 #include <linux/mfd/twl.h>
14 #include <linux/slab.h>
17 * This driver handles the PWMs of TWL4030 and TWL6030.
18 * The TRM names for the PWMs on TWL4030 are: PWM0, PWM1
19 * TWL6030 also have two PWMs named in the TRM as PWM1, PWM2
22 #define TWL_PWM_MAX 0x7f
24 /* Registers, bits and macro for TWL4030 */
25 #define TWL4030_GPBR1_REG 0x0c
26 #define TWL4030_PMBR1_REG 0x0d
28 /* GPBR1 register bits */
29 #define TWL4030_PWMXCLK_ENABLE (1 << 0)
30 #define TWL4030_PWMX_ENABLE (1 << 2)
31 #define TWL4030_PWMX_BITS (TWL4030_PWMX_ENABLE | TWL4030_PWMXCLK_ENABLE)
32 #define TWL4030_PWM_TOGGLE(pwm, x) ((x) << (pwm))
34 /* PMBR1 register bits */
35 #define TWL4030_GPIO6_PWM0_MUTE_MASK (0x03 << 2)
36 #define TWL4030_GPIO6_PWM0_MUTE_PWM0 (0x01 << 2)
37 #define TWL4030_GPIO7_VIBRASYNC_PWM1_MASK (0x03 << 4)
38 #define TWL4030_GPIO7_VIBRASYNC_PWM1_PWM1 (0x03 << 4)
40 /* Register, bits and macro for TWL6030 */
41 #define TWL6030_TOGGLE3_REG 0x92
43 #define TWL6030_PWMXR (1 << 0)
44 #define TWL6030_PWMXS (1 << 1)
45 #define TWL6030_PWMXEN (1 << 2)
46 #define TWL6030_PWM_TOGGLE(pwm, x) ((x) << (pwm * 3))
55 static inline struct twl_pwm_chip *to_twl(struct pwm_chip *chip)
57 return container_of(chip, struct twl_pwm_chip, chip);
60 static int twl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
61 u64 duty_ns, u64 period_ns)
63 int duty_cycle = DIV64_U64_ROUND_UP(duty_ns * TWL_PWM_MAX, period_ns) + 1;
64 u8 pwm_config[2] = { 1, 0 };
68 * To configure the duty period:
69 * On-cycle is set to 1 (the minimum allowed value)
70 * The off time of 0 is not configurable, so the mapping is:
74 * 126 - > off cycle 127,
76 * When on cycle == off cycle the PWM will be always on
80 else if (duty_cycle > TWL_PWM_MAX)
83 base = pwm->hwpwm * 3;
85 pwm_config[1] = duty_cycle;
87 ret = twl_i2c_write(TWL_MODULE_PWM, pwm_config, base, 2);
89 dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
94 static int twl4030_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
96 struct twl_pwm_chip *twl = to_twl(chip);
100 mutex_lock(&twl->mutex);
101 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG);
103 dev_err(chip->dev, "%s: Failed to read GPBR1\n", pwm->label);
107 val |= TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMXCLK_ENABLE);
109 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
111 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
113 val |= TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMX_ENABLE);
115 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
117 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
120 mutex_unlock(&twl->mutex);
124 static void twl4030_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
126 struct twl_pwm_chip *twl = to_twl(chip);
130 mutex_lock(&twl->mutex);
131 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG);
133 dev_err(chip->dev, "%s: Failed to read GPBR1\n", pwm->label);
137 val &= ~TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMX_ENABLE);
139 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
141 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
143 val &= ~TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMXCLK_ENABLE);
145 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
147 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
150 mutex_unlock(&twl->mutex);
153 static int twl4030_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
155 struct twl_pwm_chip *twl = to_twl(chip);
159 if (pwm->hwpwm == 1) {
160 mask = TWL4030_GPIO7_VIBRASYNC_PWM1_MASK;
161 bits = TWL4030_GPIO7_VIBRASYNC_PWM1_PWM1;
163 mask = TWL4030_GPIO6_PWM0_MUTE_MASK;
164 bits = TWL4030_GPIO6_PWM0_MUTE_PWM0;
167 mutex_lock(&twl->mutex);
168 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_PMBR1_REG);
170 dev_err(chip->dev, "%s: Failed to read PMBR1\n", pwm->label);
174 /* Save the current MUX configuration for the PWM */
175 twl->twl4030_pwm_mux &= ~mask;
176 twl->twl4030_pwm_mux |= (val & mask);
178 /* Select PWM functionality */
182 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_PMBR1_REG);
184 dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
187 mutex_unlock(&twl->mutex);
191 static void twl4030_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
193 struct twl_pwm_chip *twl = to_twl(chip);
198 mask = TWL4030_GPIO7_VIBRASYNC_PWM1_MASK;
200 mask = TWL4030_GPIO6_PWM0_MUTE_MASK;
202 mutex_lock(&twl->mutex);
203 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_PMBR1_REG);
205 dev_err(chip->dev, "%s: Failed to read PMBR1\n", pwm->label);
209 /* Restore the MUX configuration for the PWM */
211 val |= (twl->twl4030_pwm_mux & mask);
213 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_PMBR1_REG);
215 dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
218 mutex_unlock(&twl->mutex);
221 static int twl6030_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
223 struct twl_pwm_chip *twl = to_twl(chip);
227 mutex_lock(&twl->mutex);
228 val = twl->twl6030_toggle3;
229 val |= TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXS | TWL6030_PWMXEN);
230 val &= ~TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXR);
232 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
234 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
238 twl->twl6030_toggle3 = val;
240 mutex_unlock(&twl->mutex);
244 static void twl6030_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
246 struct twl_pwm_chip *twl = to_twl(chip);
250 mutex_lock(&twl->mutex);
251 val = twl->twl6030_toggle3;
252 val |= TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXR);
253 val &= ~TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXS | TWL6030_PWMXEN);
255 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
257 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
261 val |= TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXEN);
263 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
265 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
269 val &= ~TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXEN);
271 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
273 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
277 twl->twl6030_toggle3 = val;
279 mutex_unlock(&twl->mutex);
282 static int twl4030_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
283 const struct pwm_state *state)
287 if (state->polarity != PWM_POLARITY_NORMAL)
290 if (!state->enabled) {
291 if (pwm->state.enabled)
292 twl4030_pwm_disable(chip, pwm);
297 err = twl_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period);
301 if (!pwm->state.enabled)
302 err = twl4030_pwm_enable(chip, pwm);
307 static int twl6030_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
308 const struct pwm_state *state)
312 if (state->polarity != PWM_POLARITY_NORMAL)
315 if (!state->enabled) {
316 if (pwm->state.enabled)
317 twl6030_pwm_disable(chip, pwm);
322 err = twl_pwm_config(pwm->chip, pwm, state->duty_cycle, state->period);
326 if (!pwm->state.enabled)
327 err = twl6030_pwm_enable(chip, pwm);
332 static const struct pwm_ops twl4030_pwm_ops = {
333 .apply = twl4030_pwm_apply,
334 .request = twl4030_pwm_request,
335 .free = twl4030_pwm_free,
336 .owner = THIS_MODULE,
339 static const struct pwm_ops twl6030_pwm_ops = {
340 .apply = twl6030_pwm_apply,
341 .owner = THIS_MODULE,
344 static int twl_pwm_probe(struct platform_device *pdev)
346 struct twl_pwm_chip *twl;
348 twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
352 if (twl_class_is_4030())
353 twl->chip.ops = &twl4030_pwm_ops;
355 twl->chip.ops = &twl6030_pwm_ops;
357 twl->chip.dev = &pdev->dev;
360 mutex_init(&twl->mutex);
362 return devm_pwmchip_add(&pdev->dev, &twl->chip);
366 static const struct of_device_id twl_pwm_of_match[] = {
367 { .compatible = "ti,twl4030-pwm" },
368 { .compatible = "ti,twl6030-pwm" },
371 MODULE_DEVICE_TABLE(of, twl_pwm_of_match);
374 static struct platform_driver twl_pwm_driver = {
377 .of_match_table = of_match_ptr(twl_pwm_of_match),
379 .probe = twl_pwm_probe,
381 module_platform_driver(twl_pwm_driver);
383 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
384 MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030");
385 MODULE_ALIAS("platform:twl-pwm");
386 MODULE_LICENSE("GPL");