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))
54 static inline struct twl_pwm_chip
*to_twl(struct pwm_chip
*chip
)
56 return pwmchip_get_drvdata(chip
);
59 static int twl_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
60 u64 duty_ns
, u64 period_ns
)
62 int duty_cycle
= DIV64_U64_ROUND_UP(duty_ns
* TWL_PWM_MAX
, period_ns
) + 1;
63 u8 pwm_config
[2] = { 1, 0 };
67 * To configure the duty period:
68 * On-cycle is set to 1 (the minimum allowed value)
69 * The off time of 0 is not configurable, so the mapping is:
73 * 126 - > off cycle 127,
75 * When on cycle == off cycle the PWM will be always on
79 else if (duty_cycle
> TWL_PWM_MAX
)
82 base
= pwm
->hwpwm
* 3;
84 pwm_config
[1] = duty_cycle
;
86 ret
= twl_i2c_write(TWL_MODULE_PWM
, pwm_config
, base
, 2);
88 dev_err(pwmchip_parent(chip
), "%s: Failed to configure PWM\n", pwm
->label
);
93 static int twl4030_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
95 struct twl_pwm_chip
*twl
= to_twl(chip
);
99 mutex_lock(&twl
->mutex
);
100 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &val
, TWL4030_GPBR1_REG
);
102 dev_err(pwmchip_parent(chip
), "%s: Failed to read GPBR1\n", pwm
->label
);
106 val
|= TWL4030_PWM_TOGGLE(pwm
->hwpwm
, TWL4030_PWMXCLK_ENABLE
);
108 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_GPBR1_REG
);
110 dev_err(pwmchip_parent(chip
), "%s: Failed to enable PWM\n", pwm
->label
);
112 val
|= TWL4030_PWM_TOGGLE(pwm
->hwpwm
, TWL4030_PWMX_ENABLE
);
114 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_GPBR1_REG
);
116 dev_err(pwmchip_parent(chip
), "%s: Failed to enable PWM\n", pwm
->label
);
119 mutex_unlock(&twl
->mutex
);
123 static void twl4030_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
125 struct twl_pwm_chip
*twl
= to_twl(chip
);
129 mutex_lock(&twl
->mutex
);
130 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &val
, TWL4030_GPBR1_REG
);
132 dev_err(pwmchip_parent(chip
), "%s: Failed to read GPBR1\n", pwm
->label
);
136 val
&= ~TWL4030_PWM_TOGGLE(pwm
->hwpwm
, TWL4030_PWMX_ENABLE
);
138 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_GPBR1_REG
);
140 dev_err(pwmchip_parent(chip
), "%s: Failed to disable PWM\n", pwm
->label
);
142 val
&= ~TWL4030_PWM_TOGGLE(pwm
->hwpwm
, TWL4030_PWMXCLK_ENABLE
);
144 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_GPBR1_REG
);
146 dev_err(pwmchip_parent(chip
), "%s: Failed to disable PWM\n", pwm
->label
);
149 mutex_unlock(&twl
->mutex
);
152 static int twl4030_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
154 struct twl_pwm_chip
*twl
= to_twl(chip
);
158 if (pwm
->hwpwm
== 1) {
159 mask
= TWL4030_GPIO7_VIBRASYNC_PWM1_MASK
;
160 bits
= TWL4030_GPIO7_VIBRASYNC_PWM1_PWM1
;
162 mask
= TWL4030_GPIO6_PWM0_MUTE_MASK
;
163 bits
= TWL4030_GPIO6_PWM0_MUTE_PWM0
;
166 mutex_lock(&twl
->mutex
);
167 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &val
, TWL4030_PMBR1_REG
);
169 dev_err(pwmchip_parent(chip
), "%s: Failed to read PMBR1\n", pwm
->label
);
173 /* Save the current MUX configuration for the PWM */
174 twl
->twl4030_pwm_mux
&= ~mask
;
175 twl
->twl4030_pwm_mux
|= (val
& mask
);
177 /* Select PWM functionality */
181 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_PMBR1_REG
);
183 dev_err(pwmchip_parent(chip
), "%s: Failed to request PWM\n", pwm
->label
);
186 mutex_unlock(&twl
->mutex
);
190 static void twl4030_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
192 struct twl_pwm_chip
*twl
= to_twl(chip
);
197 mask
= TWL4030_GPIO7_VIBRASYNC_PWM1_MASK
;
199 mask
= TWL4030_GPIO6_PWM0_MUTE_MASK
;
201 mutex_lock(&twl
->mutex
);
202 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &val
, TWL4030_PMBR1_REG
);
204 dev_err(pwmchip_parent(chip
), "%s: Failed to read PMBR1\n", pwm
->label
);
208 /* Restore the MUX configuration for the PWM */
210 val
|= (twl
->twl4030_pwm_mux
& mask
);
212 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_PMBR1_REG
);
214 dev_err(pwmchip_parent(chip
), "%s: Failed to free PWM\n", pwm
->label
);
217 mutex_unlock(&twl
->mutex
);
220 static int twl6030_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
222 struct twl_pwm_chip
*twl
= to_twl(chip
);
226 mutex_lock(&twl
->mutex
);
227 val
= twl
->twl6030_toggle3
;
228 val
|= TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXS
| TWL6030_PWMXEN
);
229 val
&= ~TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXR
);
231 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_TOGGLE3_REG
);
233 dev_err(pwmchip_parent(chip
), "%s: Failed to enable PWM\n", pwm
->label
);
237 twl
->twl6030_toggle3
= val
;
239 mutex_unlock(&twl
->mutex
);
243 static void twl6030_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
245 struct twl_pwm_chip
*twl
= to_twl(chip
);
249 mutex_lock(&twl
->mutex
);
250 val
= twl
->twl6030_toggle3
;
251 val
|= TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXR
);
252 val
&= ~TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXS
| TWL6030_PWMXEN
);
254 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_TOGGLE3_REG
);
256 dev_err(pwmchip_parent(chip
), "%s: Failed to disable PWM\n", pwm
->label
);
260 val
|= TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXEN
);
262 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_TOGGLE3_REG
);
264 dev_err(pwmchip_parent(chip
), "%s: Failed to disable PWM\n", pwm
->label
);
268 val
&= ~TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXEN
);
270 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_TOGGLE3_REG
);
272 dev_err(pwmchip_parent(chip
), "%s: Failed to disable PWM\n", pwm
->label
);
276 twl
->twl6030_toggle3
= val
;
278 mutex_unlock(&twl
->mutex
);
281 static int twl4030_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
282 const struct pwm_state
*state
)
286 if (state
->polarity
!= PWM_POLARITY_NORMAL
)
289 if (!state
->enabled
) {
290 if (pwm
->state
.enabled
)
291 twl4030_pwm_disable(chip
, pwm
);
296 err
= twl_pwm_config(chip
, pwm
, state
->duty_cycle
, state
->period
);
300 if (!pwm
->state
.enabled
)
301 err
= twl4030_pwm_enable(chip
, pwm
);
306 static int twl6030_pwm_apply(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
307 const struct pwm_state
*state
)
311 if (state
->polarity
!= PWM_POLARITY_NORMAL
)
314 if (!state
->enabled
) {
315 if (pwm
->state
.enabled
)
316 twl6030_pwm_disable(chip
, pwm
);
321 err
= twl_pwm_config(chip
, pwm
, state
->duty_cycle
, state
->period
);
325 if (!pwm
->state
.enabled
)
326 err
= twl6030_pwm_enable(chip
, pwm
);
331 static const struct pwm_ops twl4030_pwm_ops
= {
332 .apply
= twl4030_pwm_apply
,
333 .request
= twl4030_pwm_request
,
334 .free
= twl4030_pwm_free
,
337 static const struct pwm_ops twl6030_pwm_ops
= {
338 .apply
= twl6030_pwm_apply
,
341 static int twl_pwm_probe(struct platform_device
*pdev
)
343 struct pwm_chip
*chip
;
344 struct twl_pwm_chip
*twl
;
346 chip
= devm_pwmchip_alloc(&pdev
->dev
, 2, sizeof(*twl
));
348 return PTR_ERR(chip
);
351 if (twl_class_is_4030())
352 chip
->ops
= &twl4030_pwm_ops
;
354 chip
->ops
= &twl6030_pwm_ops
;
356 mutex_init(&twl
->mutex
);
358 return devm_pwmchip_add(&pdev
->dev
, chip
);
362 static const struct of_device_id twl_pwm_of_match
[] = {
363 { .compatible
= "ti,twl4030-pwm" },
364 { .compatible
= "ti,twl6030-pwm" },
367 MODULE_DEVICE_TABLE(of
, twl_pwm_of_match
);
370 static struct platform_driver twl_pwm_driver
= {
373 .of_match_table
= of_match_ptr(twl_pwm_of_match
),
375 .probe
= twl_pwm_probe
,
377 module_platform_driver(twl_pwm_driver
);
379 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
380 MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030");
381 MODULE_ALIAS("platform:twl-pwm");
382 MODULE_LICENSE("GPL");