2 * Driver for TWL4030/6030 Generic Pulse Width Modulator
4 * Copyright (C) 2012 Texas Instruments
5 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/pwm.h>
24 #include <linux/mfd/twl.h>
25 #include <linux/slab.h>
28 * This driver handles the PWMs of TWL4030 and TWL6030.
29 * The TRM names for the PWMs on TWL4030 are: PWM0, PWM1
30 * TWL6030 also have two PWMs named in the TRM as PWM1, PWM2
33 #define TWL_PWM_MAX 0x7f
35 /* Registers, bits and macro for TWL4030 */
36 #define TWL4030_GPBR1_REG 0x0c
37 #define TWL4030_PMBR1_REG 0x0d
39 /* GPBR1 register bits */
40 #define TWL4030_PWMXCLK_ENABLE (1 << 0)
41 #define TWL4030_PWMX_ENABLE (1 << 2)
42 #define TWL4030_PWMX_BITS (TWL4030_PWMX_ENABLE | TWL4030_PWMXCLK_ENABLE)
43 #define TWL4030_PWM_TOGGLE(pwm, x) ((x) << (pwm))
45 /* PMBR1 register bits */
46 #define TWL4030_GPIO6_PWM0_MUTE_MASK (0x03 << 2)
47 #define TWL4030_GPIO6_PWM0_MUTE_PWM0 (0x01 << 2)
48 #define TWL4030_GPIO7_VIBRASYNC_PWM1_MASK (0x03 << 4)
49 #define TWL4030_GPIO7_VIBRASYNC_PWM1_PWM1 (0x03 << 4)
51 /* Register, bits and macro for TWL6030 */
52 #define TWL6030_TOGGLE3_REG 0x92
54 #define TWL6030_PWMXR (1 << 0)
55 #define TWL6030_PWMXS (1 << 1)
56 #define TWL6030_PWMXEN (1 << 2)
57 #define TWL6030_PWM_TOGGLE(pwm, x) ((x) << (pwm * 3))
66 static inline struct twl_pwm_chip
*to_twl(struct pwm_chip
*chip
)
68 return container_of(chip
, struct twl_pwm_chip
, chip
);
71 static int twl_pwm_config(struct pwm_chip
*chip
, struct pwm_device
*pwm
,
72 int duty_ns
, int period_ns
)
74 int duty_cycle
= DIV_ROUND_UP(duty_ns
* TWL_PWM_MAX
, period_ns
) + 1;
75 u8 pwm_config
[2] = { 1, 0 };
79 * To configure the duty period:
80 * On-cycle is set to 1 (the minimum allowed value)
81 * The off time of 0 is not configurable, so the mapping is:
85 * 126 - > off cycle 127,
87 * When on cycle == off cycle the PWM will be always on
91 else if (duty_cycle
> TWL_PWM_MAX
)
94 base
= pwm
->hwpwm
* 3;
96 pwm_config
[1] = duty_cycle
;
98 ret
= twl_i2c_write(TWL_MODULE_PWM
, pwm_config
, base
, 2);
100 dev_err(chip
->dev
, "%s: Failed to configure PWM\n", pwm
->label
);
105 static int twl4030_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
107 struct twl_pwm_chip
*twl
= to_twl(chip
);
111 mutex_lock(&twl
->mutex
);
112 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &val
, TWL4030_GPBR1_REG
);
114 dev_err(chip
->dev
, "%s: Failed to read GPBR1\n", pwm
->label
);
118 val
|= TWL4030_PWM_TOGGLE(pwm
->hwpwm
, TWL4030_PWMXCLK_ENABLE
);
120 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_GPBR1_REG
);
122 dev_err(chip
->dev
, "%s: Failed to enable PWM\n", pwm
->label
);
124 val
|= TWL4030_PWM_TOGGLE(pwm
->hwpwm
, TWL4030_PWMX_ENABLE
);
126 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_GPBR1_REG
);
128 dev_err(chip
->dev
, "%s: Failed to enable PWM\n", pwm
->label
);
131 mutex_unlock(&twl
->mutex
);
135 static void twl4030_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
137 struct twl_pwm_chip
*twl
= to_twl(chip
);
141 mutex_lock(&twl
->mutex
);
142 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &val
, TWL4030_GPBR1_REG
);
144 dev_err(chip
->dev
, "%s: Failed to read GPBR1\n", pwm
->label
);
148 val
&= ~TWL4030_PWM_TOGGLE(pwm
->hwpwm
, TWL4030_PWMX_ENABLE
);
150 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_GPBR1_REG
);
152 dev_err(chip
->dev
, "%s: Failed to disable PWM\n", pwm
->label
);
154 val
&= ~TWL4030_PWM_TOGGLE(pwm
->hwpwm
, TWL4030_PWMXCLK_ENABLE
);
156 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_GPBR1_REG
);
158 dev_err(chip
->dev
, "%s: Failed to disable PWM\n", pwm
->label
);
161 mutex_unlock(&twl
->mutex
);
164 static int twl4030_pwm_request(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
166 struct twl_pwm_chip
*twl
= to_twl(chip
);
170 if (pwm
->hwpwm
== 1) {
171 mask
= TWL4030_GPIO7_VIBRASYNC_PWM1_MASK
;
172 bits
= TWL4030_GPIO7_VIBRASYNC_PWM1_PWM1
;
174 mask
= TWL4030_GPIO6_PWM0_MUTE_MASK
;
175 bits
= TWL4030_GPIO6_PWM0_MUTE_PWM0
;
178 mutex_lock(&twl
->mutex
);
179 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &val
, TWL4030_PMBR1_REG
);
181 dev_err(chip
->dev
, "%s: Failed to read PMBR1\n", pwm
->label
);
185 /* Save the current MUX configuration for the PWM */
186 twl
->twl4030_pwm_mux
&= ~mask
;
187 twl
->twl4030_pwm_mux
|= (val
& mask
);
189 /* Select PWM functionality */
193 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_PMBR1_REG
);
195 dev_err(chip
->dev
, "%s: Failed to request PWM\n", pwm
->label
);
198 mutex_unlock(&twl
->mutex
);
202 static void twl4030_pwm_free(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
204 struct twl_pwm_chip
*twl
= to_twl(chip
);
209 mask
= TWL4030_GPIO7_VIBRASYNC_PWM1_MASK
;
211 mask
= TWL4030_GPIO6_PWM0_MUTE_MASK
;
213 mutex_lock(&twl
->mutex
);
214 ret
= twl_i2c_read_u8(TWL4030_MODULE_INTBR
, &val
, TWL4030_PMBR1_REG
);
216 dev_err(chip
->dev
, "%s: Failed to read PMBR1\n", pwm
->label
);
220 /* Restore the MUX configuration for the PWM */
222 val
|= (twl
->twl4030_pwm_mux
& mask
);
224 ret
= twl_i2c_write_u8(TWL4030_MODULE_INTBR
, val
, TWL4030_PMBR1_REG
);
226 dev_err(chip
->dev
, "%s: Failed to free PWM\n", pwm
->label
);
229 mutex_unlock(&twl
->mutex
);
232 static int twl6030_pwm_enable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
234 struct twl_pwm_chip
*twl
= to_twl(chip
);
238 mutex_lock(&twl
->mutex
);
239 val
= twl
->twl6030_toggle3
;
240 val
|= TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXS
| TWL6030_PWMXEN
);
241 val
&= ~TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXR
);
243 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_TOGGLE3_REG
);
245 dev_err(chip
->dev
, "%s: Failed to enable PWM\n", pwm
->label
);
249 twl
->twl6030_toggle3
= val
;
251 mutex_unlock(&twl
->mutex
);
255 static void twl6030_pwm_disable(struct pwm_chip
*chip
, struct pwm_device
*pwm
)
257 struct twl_pwm_chip
*twl
= to_twl(chip
);
261 mutex_lock(&twl
->mutex
);
262 val
= twl
->twl6030_toggle3
;
263 val
|= TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXR
);
264 val
&= ~TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXS
| TWL6030_PWMXEN
);
266 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_TOGGLE3_REG
);
268 dev_err(chip
->dev
, "%s: Failed to disable PWM\n", pwm
->label
);
272 val
|= TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXEN
);
274 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_TOGGLE3_REG
);
276 dev_err(chip
->dev
, "%s: Failed to disable PWM\n", pwm
->label
);
280 val
&= ~TWL6030_PWM_TOGGLE(pwm
->hwpwm
, TWL6030_PWMXEN
);
282 ret
= twl_i2c_write_u8(TWL6030_MODULE_ID1
, val
, TWL6030_TOGGLE3_REG
);
284 dev_err(chip
->dev
, "%s: Failed to disable PWM\n", pwm
->label
);
288 twl
->twl6030_toggle3
= val
;
290 mutex_unlock(&twl
->mutex
);
293 static const struct pwm_ops twl4030_pwm_ops
= {
294 .config
= twl_pwm_config
,
295 .enable
= twl4030_pwm_enable
,
296 .disable
= twl4030_pwm_disable
,
297 .request
= twl4030_pwm_request
,
298 .free
= twl4030_pwm_free
,
299 .owner
= THIS_MODULE
,
302 static const struct pwm_ops twl6030_pwm_ops
= {
303 .config
= twl_pwm_config
,
304 .enable
= twl6030_pwm_enable
,
305 .disable
= twl6030_pwm_disable
,
306 .owner
= THIS_MODULE
,
309 static int twl_pwm_probe(struct platform_device
*pdev
)
311 struct twl_pwm_chip
*twl
;
314 twl
= devm_kzalloc(&pdev
->dev
, sizeof(*twl
), GFP_KERNEL
);
318 if (twl_class_is_4030())
319 twl
->chip
.ops
= &twl4030_pwm_ops
;
321 twl
->chip
.ops
= &twl6030_pwm_ops
;
323 twl
->chip
.dev
= &pdev
->dev
;
327 mutex_init(&twl
->mutex
);
329 ret
= pwmchip_add(&twl
->chip
);
333 platform_set_drvdata(pdev
, twl
);
338 static int twl_pwm_remove(struct platform_device
*pdev
)
340 struct twl_pwm_chip
*twl
= platform_get_drvdata(pdev
);
342 return pwmchip_remove(&twl
->chip
);
346 static const struct of_device_id twl_pwm_of_match
[] = {
347 { .compatible
= "ti,twl4030-pwm" },
348 { .compatible
= "ti,twl6030-pwm" },
351 MODULE_DEVICE_TABLE(of
, twl_pwm_of_match
);
354 static struct platform_driver twl_pwm_driver
= {
357 .of_match_table
= of_match_ptr(twl_pwm_of_match
),
359 .probe
= twl_pwm_probe
,
360 .remove
= twl_pwm_remove
,
362 module_platform_driver(twl_pwm_driver
);
364 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
365 MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030");
366 MODULE_ALIAS("platform:twl-pwm");
367 MODULE_LICENSE("GPL");