blob: a4034ed66dc0f66f6a5438ebbb70b6327910a363 [file] [log] [blame]
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -05001/*
2 * MFD driver for TWL6040 audio device
3 *
4 * Authors: Misael Lopez Cruz <misael.lopez@ti.com>
5 * Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
6 * Peter Ujfalusi <peter.ujfalusi@ti.com>
7 *
8 * Copyright: (C) 2011 Texas Instruments, Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/kernel.h>
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030030#include <linux/err.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050031#include <linux/platform_device.h>
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +030032#include <linux/of.h>
33#include <linux/of_irq.h>
34#include <linux/of_gpio.h>
35#include <linux/of_platform.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050036#include <linux/gpio.h>
37#include <linux/delay.h>
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030038#include <linux/i2c.h>
39#include <linux/regmap.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050040#include <linux/mfd/core.h>
41#include <linux/mfd/twl6040.h>
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030042#include <linux/regulator/consumer.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050043
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030044#define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030045#define TWL6040_NUM_SUPPLIES (2)
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030046
Peter Ujfalusidf04b622013-07-12 13:32:02 +020047static bool twl6040_has_vibra(struct device_node *node)
Samuel Ortizca2cad62012-05-23 16:23:21 +020048{
Samuel Ortizca2cad62012-05-23 16:23:21 +020049#ifdef CONFIG_OF
50 if (of_find_node_by_name(node, "vibra"))
51 return true;
52#endif
Samuel Ortizca2cad62012-05-23 16:23:21 +020053 return false;
54}
55
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050056int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
57{
58 int ret;
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030059 unsigned int val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050060
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030061 /* Vibra control registers from cache */
62 if (unlikely(reg == TWL6040_REG_VIBCTLL ||
63 reg == TWL6040_REG_VIBCTLR)) {
64 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
65 } else {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030066 ret = regmap_read(twl6040->regmap, reg, &val);
Axel Linc6000402012-07-11 10:06:34 +080067 if (ret < 0)
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030068 return ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050069 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050070
71 return val;
72}
73EXPORT_SYMBOL(twl6040_reg_read);
74
75int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
76{
77 int ret;
78
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030079 ret = regmap_write(twl6040->regmap, reg, val);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030080 /* Cache the vibra control registers */
81 if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
82 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050083
84 return ret;
85}
86EXPORT_SYMBOL(twl6040_reg_write);
87
88int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
89{
Axel Linc6000402012-07-11 10:06:34 +080090 return regmap_update_bits(twl6040->regmap, reg, mask, mask);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050091}
92EXPORT_SYMBOL(twl6040_set_bits);
93
94int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
95{
Axel Linc6000402012-07-11 10:06:34 +080096 return regmap_update_bits(twl6040->regmap, reg, mask, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050097}
98EXPORT_SYMBOL(twl6040_clear_bits);
99
100/* twl6040 codec manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200101static int twl6040_power_up_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500102{
103 u8 ldoctl, ncpctl, lppllctl;
104 int ret;
105
106 /* enable high-side LDO, reference system and internal oscillator */
107 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
108 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
109 if (ret)
110 return ret;
111 usleep_range(10000, 10500);
112
113 /* enable negative charge pump */
114 ncpctl = TWL6040_NCPENA;
115 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
116 if (ret)
117 goto ncp_err;
118 usleep_range(1000, 1500);
119
120 /* enable low-side LDO */
121 ldoctl |= TWL6040_LSLDOENA;
122 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
123 if (ret)
124 goto lsldo_err;
125 usleep_range(1000, 1500);
126
127 /* enable low-power PLL */
128 lppllctl = TWL6040_LPLLENA;
129 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
130 if (ret)
131 goto lppll_err;
132 usleep_range(5000, 5500);
133
134 /* disable internal oscillator */
135 ldoctl &= ~TWL6040_OSCENA;
136 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
137 if (ret)
138 goto osc_err;
139
140 return 0;
141
142osc_err:
143 lppllctl &= ~TWL6040_LPLLENA;
144 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
145lppll_err:
146 ldoctl &= ~TWL6040_LSLDOENA;
147 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
148lsldo_err:
149 ncpctl &= ~TWL6040_NCPENA;
150 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
151ncp_err:
152 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
153 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
154
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200155 dev_err(twl6040->dev, "manual power-up failed\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500156 return ret;
157}
158
159/* twl6040 manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200160static void twl6040_power_down_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500161{
162 u8 ncpctl, ldoctl, lppllctl;
163
164 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
165 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
166 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
167
168 /* enable internal oscillator */
169 ldoctl |= TWL6040_OSCENA;
170 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
171 usleep_range(1000, 1500);
172
173 /* disable low-power PLL */
174 lppllctl &= ~TWL6040_LPLLENA;
175 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
176
177 /* disable low-side LDO */
178 ldoctl &= ~TWL6040_LSLDOENA;
179 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
180
181 /* disable negative charge pump */
182 ncpctl &= ~TWL6040_NCPENA;
183 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
184
185 /* disable high-side LDO, reference system and internal oscillator */
186 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
187 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
188}
189
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200190static irqreturn_t twl6040_readyint_handler(int irq, void *data)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500191{
192 struct twl6040 *twl6040 = data;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500193
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200194 complete(&twl6040->ready);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500195
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200196 return IRQ_HANDLED;
197}
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500198
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200199static irqreturn_t twl6040_thint_handler(int irq, void *data)
200{
201 struct twl6040 *twl6040 = data;
202 u8 status;
203
204 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
205 if (status & TWL6040_TSHUTDET) {
206 dev_warn(twl6040->dev, "Thermal shutdown, powering-off");
207 twl6040_power(twl6040, 0);
208 } else {
209 dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on");
210 twl6040_power(twl6040, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500211 }
212
213 return IRQ_HANDLED;
214}
215
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200216static int twl6040_power_up_automatic(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500217{
218 int time_left;
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200219
220 gpio_set_value(twl6040->audpwron, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500221
222 time_left = wait_for_completion_timeout(&twl6040->ready,
223 msecs_to_jiffies(144));
224 if (!time_left) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200225 u8 intid;
226
227 dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500228 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
229 if (!(intid & TWL6040_READYINT)) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200230 dev_err(twl6040->dev, "automatic power-up failed\n");
231 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500232 return -ETIMEDOUT;
233 }
234 }
235
236 return 0;
237}
238
239int twl6040_power(struct twl6040 *twl6040, int on)
240{
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500241 int ret = 0;
242
243 mutex_lock(&twl6040->mutex);
244
245 if (on) {
246 /* already powered-up */
247 if (twl6040->power_count++)
248 goto out;
249
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200250 if (gpio_is_valid(twl6040->audpwron)) {
251 /* use automatic power-up sequence */
252 ret = twl6040_power_up_automatic(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500253 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500254 twl6040->power_count = 0;
255 goto out;
256 }
257 } else {
258 /* use manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200259 ret = twl6040_power_up_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500260 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500261 twl6040->power_count = 0;
262 goto out;
263 }
264 }
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300265 /* Default PLL configuration after power up */
266 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500267 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100268 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500269 } else {
270 /* already powered-down */
271 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300272 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500273 "device is already powered-off\n");
274 ret = -EPERM;
275 goto out;
276 }
277
278 if (--twl6040->power_count)
279 goto out;
280
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200281 if (gpio_is_valid(twl6040->audpwron)) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500282 /* use AUDPWRON line */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200283 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500284
285 /* power-down sequence latency */
286 usleep_range(500, 700);
287 } else {
288 /* use manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200289 twl6040_power_down_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500290 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500291 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100292 twl6040->mclk = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500293 }
294
295out:
296 mutex_unlock(&twl6040->mutex);
297 return ret;
298}
299EXPORT_SYMBOL(twl6040_power);
300
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300301int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500302 unsigned int freq_in, unsigned int freq_out)
303{
304 u8 hppllctl, lppllctl;
305 int ret = 0;
306
307 mutex_lock(&twl6040->mutex);
308
309 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
310 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
311
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100312 /* Force full reconfiguration when switching between PLL */
313 if (pll_id != twl6040->pll) {
314 twl6040->sysclk = 0;
315 twl6040->mclk = 0;
316 }
317
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300318 switch (pll_id) {
319 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500320 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100321 /* Change the sysclk configuration only if it has been canged */
322 if (twl6040->sysclk != freq_out) {
323 switch (freq_out) {
324 case 17640000:
325 lppllctl |= TWL6040_LPLLFIN;
326 break;
327 case 19200000:
328 lppllctl &= ~TWL6040_LPLLFIN;
329 break;
330 default:
331 dev_err(twl6040->dev,
332 "freq_out %d not supported\n",
333 freq_out);
334 ret = -EINVAL;
335 goto pll_out;
336 }
337 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
338 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500339 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100340
341 /* The PLL in use has not been change, we can exit */
342 if (twl6040->pll == pll_id)
343 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500344
345 switch (freq_in) {
346 case 32768:
347 lppllctl |= TWL6040_LPLLENA;
348 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
349 lppllctl);
350 mdelay(5);
351 lppllctl &= ~TWL6040_HPLLSEL;
352 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
353 lppllctl);
354 hppllctl &= ~TWL6040_HPLLENA;
355 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
356 hppllctl);
357 break;
358 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300359 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500360 "freq_in %d not supported\n", freq_in);
361 ret = -EINVAL;
362 goto pll_out;
363 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500364 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300365 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500366 /* high-performance PLL can provide only 19.2 MHz */
367 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300368 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500369 "freq_out %d not supported\n", freq_out);
370 ret = -EINVAL;
371 goto pll_out;
372 }
373
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100374 if (twl6040->mclk != freq_in) {
375 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500376
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100377 switch (freq_in) {
378 case 12000000:
379 /* PLL enabled, active mode */
380 hppllctl |= TWL6040_MCLK_12000KHZ |
381 TWL6040_HPLLENA;
382 break;
383 case 19200000:
384 /*
385 * PLL disabled
386 * (enable PLL if MCLK jitter quality
387 * doesn't meet specification)
388 */
389 hppllctl |= TWL6040_MCLK_19200KHZ;
390 break;
391 case 26000000:
392 /* PLL enabled, active mode */
393 hppllctl |= TWL6040_MCLK_26000KHZ |
394 TWL6040_HPLLENA;
395 break;
396 case 38400000:
397 /* PLL enabled, active mode */
398 hppllctl |= TWL6040_MCLK_38400KHZ |
399 TWL6040_HPLLENA;
400 break;
401 default:
402 dev_err(twl6040->dev,
403 "freq_in %d not supported\n", freq_in);
404 ret = -EINVAL;
405 goto pll_out;
406 }
407
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500408 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100409 * enable clock slicer to ensure input waveform is
410 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500411 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100412 hppllctl |= TWL6040_HPLLSQRENA;
413
414 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
415 hppllctl);
416 usleep_range(500, 700);
417 lppllctl |= TWL6040_HPLLSEL;
418 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
419 lppllctl);
420 lppllctl &= ~TWL6040_LPLLENA;
421 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
422 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500423 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500424 break;
425 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300426 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500427 ret = -EINVAL;
428 goto pll_out;
429 }
430
431 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100432 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300433 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500434
435pll_out:
436 mutex_unlock(&twl6040->mutex);
437 return ret;
438}
439EXPORT_SYMBOL(twl6040_set_pll);
440
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300441int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500442{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300443 if (twl6040->power_count)
444 return twl6040->pll;
445 else
446 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500447}
448EXPORT_SYMBOL(twl6040_get_pll);
449
450unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
451{
452 return twl6040->sysclk;
453}
454EXPORT_SYMBOL(twl6040_get_sysclk);
455
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300456/* Get the combined status of the vibra control register */
457int twl6040_get_vibralr_status(struct twl6040 *twl6040)
458{
459 u8 status;
460
461 status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
462 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
463
464 return status;
465}
466EXPORT_SYMBOL(twl6040_get_vibralr_status);
467
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300468static struct resource twl6040_vibra_rsrc[] = {
469 {
470 .flags = IORESOURCE_IRQ,
471 },
472};
473
474static struct resource twl6040_codec_rsrc[] = {
475 {
476 .flags = IORESOURCE_IRQ,
477 },
478};
479
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300480static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500481{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300482 /* Register 0 is not readable */
483 if (!reg)
484 return false;
485 return true;
486}
487
488static struct regmap_config twl6040_regmap_config = {
489 .reg_bits = 8,
490 .val_bits = 8,
491 .max_register = TWL6040_REG_STATUS, /* 0x2e */
492
493 .readable_reg = twl6040_readable_reg,
494};
495
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200496static const struct regmap_irq twl6040_irqs[] = {
497 { .reg_offset = 0, .mask = TWL6040_THINT, },
498 { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
499 { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
500 { .reg_offset = 0, .mask = TWL6040_HFINT, },
501 { .reg_offset = 0, .mask = TWL6040_VIBINT, },
502 { .reg_offset = 0, .mask = TWL6040_READYINT, },
503};
504
505static struct regmap_irq_chip twl6040_irq_chip = {
506 .name = "twl6040",
507 .irqs = twl6040_irqs,
508 .num_irqs = ARRAY_SIZE(twl6040_irqs),
509
510 .num_regs = 1,
511 .status_base = TWL6040_REG_INTID,
512 .mask_base = TWL6040_REG_INTMR,
513};
514
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800515static int twl6040_probe(struct i2c_client *client,
516 const struct i2c_device_id *id)
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300517{
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300518 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500519 struct twl6040 *twl6040;
520 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300521 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500522
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200523 if (!node) {
524 dev_err(&client->dev, "of node is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500525 return -EINVAL;
526 }
527
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300528 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300529 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300530 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300531 return -EINVAL;
532 }
533
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300534 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
535 GFP_KERNEL);
536 if (!twl6040) {
537 ret = -ENOMEM;
538 goto err;
539 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500540
Axel Linbbf6adc2012-04-25 10:09:46 +0800541 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300542 if (IS_ERR(twl6040->regmap)) {
543 ret = PTR_ERR(twl6040->regmap);
544 goto err;
545 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500546
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300547 i2c_set_clientdata(client, twl6040);
548
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300549 twl6040->supplies[0].supply = "vio";
550 twl6040->supplies[1].supply = "v2v1";
Jingoo Han990810b2013-02-20 18:30:21 +0900551 ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300552 twl6040->supplies);
553 if (ret != 0) {
554 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
555 goto regulator_get_err;
556 }
557
558 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
559 if (ret != 0) {
560 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
Jingoo Han990810b2013-02-20 18:30:21 +0900561 goto regulator_get_err;
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300562 }
563
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300564 twl6040->dev = &client->dev;
565 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500566
567 mutex_init(&twl6040->mutex);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500568 init_completion(&twl6040->ready);
569
570 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
571
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300572 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200573 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
574 twl6040->audpwron = of_get_named_gpio(node,
575 "ti,audpwron-gpio", 0);
576 else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300577 twl6040->audpwron = -EINVAL;
578
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500579 if (gpio_is_valid(twl6040->audpwron)) {
Jingoo Han990810b2013-02-20 18:30:21 +0900580 ret = devm_gpio_request_one(&client->dev, twl6040->audpwron,
581 GPIOF_OUT_INIT_LOW, "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500582 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300583 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500584 }
585
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200586 ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq,
587 IRQF_ONESHOT, 0, &twl6040_irq_chip,
588 &twl6040->irq_data);
589 if (ret < 0)
Jingoo Han990810b2013-02-20 18:30:21 +0900590 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500591
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200592 twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
593 TWL6040_IRQ_READY);
594 twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
595 TWL6040_IRQ_TH);
596
Jingoo Han990810b2013-02-20 18:30:21 +0900597 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200598 twl6040_readyint_handler, IRQF_ONESHOT,
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300599 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300600 if (ret) {
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200601 dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
602 goto readyirq_err;
603 }
604
Jingoo Han990810b2013-02-20 18:30:21 +0900605 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200606 twl6040_thint_handler, IRQF_ONESHOT,
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200607 "twl6040_irq_th", twl6040);
608 if (ret) {
609 dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
610 goto thirq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500611 }
612
613 /* dual-access registers controlled by I2C only */
614 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
615
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300616 /*
617 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
618 * We can add the ASoC codec child whenever this driver has been loaded.
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300619 */
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200620 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300621 cell = &twl6040->cells[children];
622 cell->name = "twl6040-codec";
623 twl6040_codec_rsrc[0].start = irq;
624 twl6040_codec_rsrc[0].end = irq;
625 cell->resources = twl6040_codec_rsrc;
626 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300627 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500628
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200629 /* Vibra input driver support */
630 if (twl6040_has_vibra(node)) {
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200631 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300632
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500633 cell = &twl6040->cells[children];
634 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300635 twl6040_vibra_rsrc[0].start = irq;
636 twl6040_vibra_rsrc[0].end = irq;
637 cell->resources = twl6040_vibra_rsrc;
638 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500639 children++;
640 }
641
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200642 /* GPO support */
643 cell = &twl6040->cells[children];
644 cell->name = "twl6040-gpo";
645 children++;
Peter Ujfalusi5cbe7862012-08-16 15:13:14 +0300646
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300647 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
Mark Brown55692af2012-09-11 15:16:36 +0800648 NULL, 0, NULL);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300649 if (ret)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500650 goto mfd_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500651
652 return 0;
653
654mfd_err:
Jingoo Han990810b2013-02-20 18:30:21 +0900655 devm_free_irq(&client->dev, twl6040->irq_th, twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200656thirq_err:
Jingoo Han990810b2013-02-20 18:30:21 +0900657 devm_free_irq(&client->dev, twl6040->irq_ready, twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200658readyirq_err:
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200659 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300660gpio_err:
661 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300662regulator_get_err:
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300663 i2c_set_clientdata(client, NULL);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300664err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500665 return ret;
666}
667
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800668static int twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500669{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300670 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500671
672 if (twl6040->power_count)
673 twl6040_power(twl6040, 0);
674
Jingoo Han990810b2013-02-20 18:30:21 +0900675 devm_free_irq(&client->dev, twl6040->irq_ready, twl6040);
676 devm_free_irq(&client->dev, twl6040->irq_th, twl6040);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200677 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500678
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300679 mfd_remove_devices(&client->dev);
680 i2c_set_clientdata(client, NULL);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500681
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300682 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300683
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500684 return 0;
685}
686
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300687static const struct i2c_device_id twl6040_i2c_id[] = {
688 { "twl6040", 0, },
Peter Ujfalusi1fc74ae2012-07-16 11:49:44 +0200689 { "twl6041", 0, },
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300690 { },
691};
692MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
693
694static struct i2c_driver twl6040_driver = {
695 .driver = {
696 .name = "twl6040",
697 .owner = THIS_MODULE,
698 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500699 .probe = twl6040_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800700 .remove = twl6040_remove,
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300701 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500702};
703
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300704module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500705
706MODULE_DESCRIPTION("TWL6040 MFD");
707MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
708MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
709MODULE_LICENSE("GPL");
710MODULE_ALIAS("platform:twl6040");