blob: b99a50e626a64f82ca98783cc373bd2bc4c82e01 [file] [log] [blame]
Peter Ujfalusi3744c262012-11-27 11:09:57 +01001/*
2 * Driver for TWL4030/6030 Generic Pulse Width Modulator
3 *
4 * Copyright (C) 2012 Texas Instruments
5 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
6 *
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.
10 *
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
14 * more details.
15 *
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/>.
18 */
19
20#include <linux/module.h>
Sachin Kamatea8eeb12013-09-27 16:53:26 +053021#include <linux/of.h>
Peter Ujfalusi3744c262012-11-27 11:09:57 +010022#include <linux/platform_device.h>
23#include <linux/pwm.h>
24#include <linux/i2c/twl.h>
25#include <linux/slab.h>
26
27/*
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
31 */
32
33#define TWL_PWM_MAX 0x7f
34
35/* Registers, bits and macro for TWL4030 */
36#define TWL4030_GPBR1_REG 0x0c
37#define TWL4030_PMBR1_REG 0x0d
38
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))
44
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)
50
51/* Register, bits and macro for TWL6030 */
52#define TWL6030_TOGGLE3_REG 0x92
53
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))
58
59struct twl_pwm_chip {
60 struct pwm_chip chip;
61 struct mutex mutex;
62 u8 twl6030_toggle3;
63 u8 twl4030_pwm_mux;
64};
65
66static inline struct twl_pwm_chip *to_twl(struct pwm_chip *chip)
67{
68 return container_of(chip, struct twl_pwm_chip, chip);
69}
70
71static int twl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
72 int duty_ns, int period_ns)
73{
74 int duty_cycle = DIV_ROUND_UP(duty_ns * TWL_PWM_MAX, period_ns) + 1;
75 u8 pwm_config[2] = { 1, 0 };
76 int base, ret;
77
78 /*
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:
82 * 0 -> off cycle = 2,
83 * 1 -> off cycle = 2,
84 * 2 -> off cycle = 3,
85 * 126 - > off cycle 127,
86 * 127 - > off cycle 1
87 * When on cycle == off cycle the PWM will be always on
88 */
89 if (duty_cycle == 1)
90 duty_cycle = 2;
91 else if (duty_cycle > TWL_PWM_MAX)
92 duty_cycle = 1;
93
94 base = pwm->hwpwm * 3;
95
96 pwm_config[1] = duty_cycle;
97
98 ret = twl_i2c_write(TWL_MODULE_PWM, pwm_config, base, 2);
99 if (ret < 0)
100 dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
101
102 return ret;
103}
104
105static int twl4030_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
106{
107 struct twl_pwm_chip *twl = to_twl(chip);
108 int ret;
109 u8 val;
110
111 mutex_lock(&twl->mutex);
112 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG);
113 if (ret < 0) {
114 dev_err(chip->dev, "%s: Failed to read GPBR1\n", pwm->label);
115 goto out;
116 }
117
118 val |= TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMXCLK_ENABLE);
119
120 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
121 if (ret < 0)
122 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
123
124 val |= TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMX_ENABLE);
125
126 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
127 if (ret < 0)
128 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
129
130out:
131 mutex_unlock(&twl->mutex);
132 return ret;
133}
134
135static void twl4030_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
136{
137 struct twl_pwm_chip *twl = to_twl(chip);
138 int ret;
139 u8 val;
140
141 mutex_lock(&twl->mutex);
142 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG);
143 if (ret < 0) {
144 dev_err(chip->dev, "%s: Failed to read GPBR1\n", pwm->label);
145 goto out;
146 }
147
148 val &= ~TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMX_ENABLE);
149
150 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
151 if (ret < 0)
152 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
153
154 val &= ~TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMXCLK_ENABLE);
155
156 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
157 if (ret < 0)
158 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
159
160out:
161 mutex_unlock(&twl->mutex);
162}
163
164static int twl4030_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
165{
166 struct twl_pwm_chip *twl = to_twl(chip);
167 int ret;
168 u8 val, mask, bits;
169
170 if (pwm->hwpwm == 1) {
171 mask = TWL4030_GPIO7_VIBRASYNC_PWM1_MASK;
172 bits = TWL4030_GPIO7_VIBRASYNC_PWM1_PWM1;
173 } else {
174 mask = TWL4030_GPIO6_PWM0_MUTE_MASK;
175 bits = TWL4030_GPIO6_PWM0_MUTE_PWM0;
176 }
177
178 mutex_lock(&twl->mutex);
179 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_PMBR1_REG);
180 if (ret < 0) {
181 dev_err(chip->dev, "%s: Failed to read PMBR1\n", pwm->label);
182 goto out;
183 }
184
185 /* Save the current MUX configuration for the PWM */
186 twl->twl4030_pwm_mux &= ~mask;
187 twl->twl4030_pwm_mux |= (val & mask);
188
189 /* Select PWM functionality */
190 val &= ~mask;
191 val |= bits;
192
193 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_PMBR1_REG);
194 if (ret < 0)
195 dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
196
197out:
198 mutex_unlock(&twl->mutex);
199 return ret;
200}
201
202static void twl4030_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
203{
Johannes Thumshirn30f78612013-02-16 19:20:51 +0100204 struct twl_pwm_chip *twl = to_twl(chip);
Peter Ujfalusi3744c262012-11-27 11:09:57 +0100205 int ret;
206 u8 val, mask;
207
208 if (pwm->hwpwm == 1)
209 mask = TWL4030_GPIO7_VIBRASYNC_PWM1_MASK;
210 else
211 mask = TWL4030_GPIO6_PWM0_MUTE_MASK;
212
213 mutex_lock(&twl->mutex);
214 ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_PMBR1_REG);
215 if (ret < 0) {
216 dev_err(chip->dev, "%s: Failed to read PMBR1\n", pwm->label);
217 goto out;
218 }
219
220 /* Restore the MUX configuration for the PWM */
221 val &= ~mask;
222 val |= (twl->twl4030_pwm_mux & mask);
223
224 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_PMBR1_REG);
225 if (ret < 0)
226 dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
227
228out:
229 mutex_unlock(&twl->mutex);
230}
231
232static int twl6030_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
233{
Johannes Thumshirn30f78612013-02-16 19:20:51 +0100234 struct twl_pwm_chip *twl = to_twl(chip);
Peter Ujfalusi3744c262012-11-27 11:09:57 +0100235 int ret;
236 u8 val;
237
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);
242
243 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
244 if (ret < 0) {
245 dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
246 goto out;
247 }
248
249 twl->twl6030_toggle3 = val;
250out:
251 mutex_unlock(&twl->mutex);
Axel Linc8e4df32013-03-31 23:07:30 +0800252 return ret;
Peter Ujfalusi3744c262012-11-27 11:09:57 +0100253}
254
255static void twl6030_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
256{
Johannes Thumshirn30f78612013-02-16 19:20:51 +0100257 struct twl_pwm_chip *twl = to_twl(chip);
Peter Ujfalusi3744c262012-11-27 11:09:57 +0100258 int ret;
259 u8 val;
260
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);
265
266 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
267 if (ret < 0) {
268 dev_err(chip->dev, "%s: Failed to read TOGGLE3\n", pwm->label);
269 goto out;
270 }
271
272 val |= TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXS | TWL6030_PWMXEN);
273
274 ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
275 if (ret < 0) {
276 dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
277 goto out;
278 }
279
280 twl->twl6030_toggle3 = val;
281out:
282 mutex_unlock(&twl->mutex);
283}
284
285static const struct pwm_ops twl4030_pwm_ops = {
286 .config = twl_pwm_config,
287 .enable = twl4030_pwm_enable,
288 .disable = twl4030_pwm_disable,
289 .request = twl4030_pwm_request,
290 .free = twl4030_pwm_free,
Axel Lind5714e82013-03-31 11:17:05 +0800291 .owner = THIS_MODULE,
Peter Ujfalusi3744c262012-11-27 11:09:57 +0100292};
293
294static const struct pwm_ops twl6030_pwm_ops = {
295 .config = twl_pwm_config,
296 .enable = twl6030_pwm_enable,
297 .disable = twl6030_pwm_disable,
Axel Lind5714e82013-03-31 11:17:05 +0800298 .owner = THIS_MODULE,
Peter Ujfalusi3744c262012-11-27 11:09:57 +0100299};
300
301static int twl_pwm_probe(struct platform_device *pdev)
302{
303 struct twl_pwm_chip *twl;
304 int ret;
305
306 twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
307 if (!twl)
308 return -ENOMEM;
309
310 if (twl_class_is_4030())
311 twl->chip.ops = &twl4030_pwm_ops;
312 else
313 twl->chip.ops = &twl6030_pwm_ops;
314
315 twl->chip.dev = &pdev->dev;
316 twl->chip.base = -1;
317 twl->chip.npwm = 2;
Florian Vaussard2e2a0f62013-01-28 15:00:58 +0100318 twl->chip.can_sleep = true;
Peter Ujfalusi3744c262012-11-27 11:09:57 +0100319
320 mutex_init(&twl->mutex);
321
322 ret = pwmchip_add(&twl->chip);
323 if (ret < 0)
324 return ret;
325
326 platform_set_drvdata(pdev, twl);
327
328 return 0;
329}
330
331static int twl_pwm_remove(struct platform_device *pdev)
332{
333 struct twl_pwm_chip *twl = platform_get_drvdata(pdev);
334
335 return pwmchip_remove(&twl->chip);
336}
337
338#ifdef CONFIG_OF
Thierry Redingf1a88702013-04-18 10:04:14 +0200339static const struct of_device_id twl_pwm_of_match[] = {
Peter Ujfalusi3744c262012-11-27 11:09:57 +0100340 { .compatible = "ti,twl4030-pwm" },
341 { .compatible = "ti,twl6030-pwm" },
342 { },
343};
344MODULE_DEVICE_TABLE(of, twl_pwm_of_match);
345#endif
346
347static struct platform_driver twl_pwm_driver = {
348 .driver = {
349 .name = "twl-pwm",
350 .of_match_table = of_match_ptr(twl_pwm_of_match),
351 },
352 .probe = twl_pwm_probe,
353 .remove = twl_pwm_remove,
354};
355module_platform_driver(twl_pwm_driver);
356
357MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
358MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030");
359MODULE_ALIAS("platform:twl-pwm");
360MODULE_LICENSE("GPL");