blob: e5f7b795afff98d96688b82d7b6868559ab6200c [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>
40#include <linux/err.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050041#include <linux/mfd/core.h>
42#include <linux/mfd/twl6040.h>
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030043#include <linux/regulator/consumer.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050044
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030045#define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030046#define TWL6040_NUM_SUPPLIES (2)
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030047
Samuel Ortizca2cad62012-05-23 16:23:21 +020048static bool twl6040_has_vibra(struct twl6040_platform_data *pdata,
49 struct device_node *node)
50{
51 if (pdata && pdata->vibra)
52 return true;
53
54#ifdef CONFIG_OF
55 if (of_find_node_by_name(node, "vibra"))
56 return true;
57#endif
58
59 return false;
60}
61
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050062int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
63{
64 int ret;
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030065 unsigned int val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050066
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030067 /* Vibra control registers from cache */
68 if (unlikely(reg == TWL6040_REG_VIBCTLL ||
69 reg == TWL6040_REG_VIBCTLR)) {
70 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
71 } else {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030072 ret = regmap_read(twl6040->regmap, reg, &val);
Axel Linc6000402012-07-11 10:06:34 +080073 if (ret < 0)
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030074 return ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050075 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050076
77 return val;
78}
79EXPORT_SYMBOL(twl6040_reg_read);
80
81int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
82{
83 int ret;
84
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030085 ret = regmap_write(twl6040->regmap, reg, val);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030086 /* Cache the vibra control registers */
87 if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
88 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050089
90 return ret;
91}
92EXPORT_SYMBOL(twl6040_reg_write);
93
94int twl6040_set_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, mask);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050097}
98EXPORT_SYMBOL(twl6040_set_bits);
99
100int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
101{
Axel Linc6000402012-07-11 10:06:34 +0800102 return regmap_update_bits(twl6040->regmap, reg, mask, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500103}
104EXPORT_SYMBOL(twl6040_clear_bits);
105
106/* twl6040 codec manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200107static int twl6040_power_up_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500108{
109 u8 ldoctl, ncpctl, lppllctl;
110 int ret;
111
112 /* enable high-side LDO, reference system and internal oscillator */
113 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
114 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
115 if (ret)
116 return ret;
117 usleep_range(10000, 10500);
118
119 /* enable negative charge pump */
120 ncpctl = TWL6040_NCPENA;
121 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
122 if (ret)
123 goto ncp_err;
124 usleep_range(1000, 1500);
125
126 /* enable low-side LDO */
127 ldoctl |= TWL6040_LSLDOENA;
128 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
129 if (ret)
130 goto lsldo_err;
131 usleep_range(1000, 1500);
132
133 /* enable low-power PLL */
134 lppllctl = TWL6040_LPLLENA;
135 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
136 if (ret)
137 goto lppll_err;
138 usleep_range(5000, 5500);
139
140 /* disable internal oscillator */
141 ldoctl &= ~TWL6040_OSCENA;
142 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
143 if (ret)
144 goto osc_err;
145
146 return 0;
147
148osc_err:
149 lppllctl &= ~TWL6040_LPLLENA;
150 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
151lppll_err:
152 ldoctl &= ~TWL6040_LSLDOENA;
153 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
154lsldo_err:
155 ncpctl &= ~TWL6040_NCPENA;
156 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
157ncp_err:
158 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
159 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
160
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200161 dev_err(twl6040->dev, "manual power-up failed\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500162 return ret;
163}
164
165/* twl6040 manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200166static void twl6040_power_down_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500167{
168 u8 ncpctl, ldoctl, lppllctl;
169
170 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
171 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
172 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
173
174 /* enable internal oscillator */
175 ldoctl |= TWL6040_OSCENA;
176 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
177 usleep_range(1000, 1500);
178
179 /* disable low-power PLL */
180 lppllctl &= ~TWL6040_LPLLENA;
181 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
182
183 /* disable low-side LDO */
184 ldoctl &= ~TWL6040_LSLDOENA;
185 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
186
187 /* disable negative charge pump */
188 ncpctl &= ~TWL6040_NCPENA;
189 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
190
191 /* disable high-side LDO, reference system and internal oscillator */
192 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
193 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
194}
195
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200196static irqreturn_t twl6040_readyint_handler(int irq, void *data)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500197{
198 struct twl6040 *twl6040 = data;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500199
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200200 complete(&twl6040->ready);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500201
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200202 return IRQ_HANDLED;
203}
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500204
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200205static irqreturn_t twl6040_thint_handler(int irq, void *data)
206{
207 struct twl6040 *twl6040 = data;
208 u8 status;
209
210 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
211 if (status & TWL6040_TSHUTDET) {
212 dev_warn(twl6040->dev, "Thermal shutdown, powering-off");
213 twl6040_power(twl6040, 0);
214 } else {
215 dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on");
216 twl6040_power(twl6040, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500217 }
218
219 return IRQ_HANDLED;
220}
221
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200222static int twl6040_power_up_automatic(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500223{
224 int time_left;
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200225
226 gpio_set_value(twl6040->audpwron, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500227
228 time_left = wait_for_completion_timeout(&twl6040->ready,
229 msecs_to_jiffies(144));
230 if (!time_left) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200231 u8 intid;
232
233 dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500234 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
235 if (!(intid & TWL6040_READYINT)) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200236 dev_err(twl6040->dev, "automatic power-up failed\n");
237 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500238 return -ETIMEDOUT;
239 }
240 }
241
242 return 0;
243}
244
245int twl6040_power(struct twl6040 *twl6040, int on)
246{
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500247 int ret = 0;
248
249 mutex_lock(&twl6040->mutex);
250
251 if (on) {
252 /* already powered-up */
253 if (twl6040->power_count++)
254 goto out;
255
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200256 if (gpio_is_valid(twl6040->audpwron)) {
257 /* use automatic power-up sequence */
258 ret = twl6040_power_up_automatic(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500259 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500260 twl6040->power_count = 0;
261 goto out;
262 }
263 } else {
264 /* use manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200265 ret = twl6040_power_up_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500266 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500267 twl6040->power_count = 0;
268 goto out;
269 }
270 }
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300271 /* Default PLL configuration after power up */
272 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500273 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100274 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500275 } else {
276 /* already powered-down */
277 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300278 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500279 "device is already powered-off\n");
280 ret = -EPERM;
281 goto out;
282 }
283
284 if (--twl6040->power_count)
285 goto out;
286
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200287 if (gpio_is_valid(twl6040->audpwron)) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500288 /* use AUDPWRON line */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200289 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500290
291 /* power-down sequence latency */
292 usleep_range(500, 700);
293 } else {
294 /* use manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200295 twl6040_power_down_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500296 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500297 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100298 twl6040->mclk = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500299 }
300
301out:
302 mutex_unlock(&twl6040->mutex);
303 return ret;
304}
305EXPORT_SYMBOL(twl6040_power);
306
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300307int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500308 unsigned int freq_in, unsigned int freq_out)
309{
310 u8 hppllctl, lppllctl;
311 int ret = 0;
312
313 mutex_lock(&twl6040->mutex);
314
315 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
316 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
317
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100318 /* Force full reconfiguration when switching between PLL */
319 if (pll_id != twl6040->pll) {
320 twl6040->sysclk = 0;
321 twl6040->mclk = 0;
322 }
323
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300324 switch (pll_id) {
325 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500326 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100327 /* Change the sysclk configuration only if it has been canged */
328 if (twl6040->sysclk != freq_out) {
329 switch (freq_out) {
330 case 17640000:
331 lppllctl |= TWL6040_LPLLFIN;
332 break;
333 case 19200000:
334 lppllctl &= ~TWL6040_LPLLFIN;
335 break;
336 default:
337 dev_err(twl6040->dev,
338 "freq_out %d not supported\n",
339 freq_out);
340 ret = -EINVAL;
341 goto pll_out;
342 }
343 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
344 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500345 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100346
347 /* The PLL in use has not been change, we can exit */
348 if (twl6040->pll == pll_id)
349 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500350
351 switch (freq_in) {
352 case 32768:
353 lppllctl |= TWL6040_LPLLENA;
354 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
355 lppllctl);
356 mdelay(5);
357 lppllctl &= ~TWL6040_HPLLSEL;
358 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
359 lppllctl);
360 hppllctl &= ~TWL6040_HPLLENA;
361 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
362 hppllctl);
363 break;
364 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300365 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500366 "freq_in %d not supported\n", freq_in);
367 ret = -EINVAL;
368 goto pll_out;
369 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500370 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300371 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500372 /* high-performance PLL can provide only 19.2 MHz */
373 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300374 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500375 "freq_out %d not supported\n", freq_out);
376 ret = -EINVAL;
377 goto pll_out;
378 }
379
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100380 if (twl6040->mclk != freq_in) {
381 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500382
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100383 switch (freq_in) {
384 case 12000000:
385 /* PLL enabled, active mode */
386 hppllctl |= TWL6040_MCLK_12000KHZ |
387 TWL6040_HPLLENA;
388 break;
389 case 19200000:
390 /*
391 * PLL disabled
392 * (enable PLL if MCLK jitter quality
393 * doesn't meet specification)
394 */
395 hppllctl |= TWL6040_MCLK_19200KHZ;
396 break;
397 case 26000000:
398 /* PLL enabled, active mode */
399 hppllctl |= TWL6040_MCLK_26000KHZ |
400 TWL6040_HPLLENA;
401 break;
402 case 38400000:
403 /* PLL enabled, active mode */
404 hppllctl |= TWL6040_MCLK_38400KHZ |
405 TWL6040_HPLLENA;
406 break;
407 default:
408 dev_err(twl6040->dev,
409 "freq_in %d not supported\n", freq_in);
410 ret = -EINVAL;
411 goto pll_out;
412 }
413
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500414 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100415 * enable clock slicer to ensure input waveform is
416 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500417 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100418 hppllctl |= TWL6040_HPLLSQRENA;
419
420 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
421 hppllctl);
422 usleep_range(500, 700);
423 lppllctl |= TWL6040_HPLLSEL;
424 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
425 lppllctl);
426 lppllctl &= ~TWL6040_LPLLENA;
427 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
428 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500429 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500430 break;
431 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300432 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500433 ret = -EINVAL;
434 goto pll_out;
435 }
436
437 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100438 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300439 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500440
441pll_out:
442 mutex_unlock(&twl6040->mutex);
443 return ret;
444}
445EXPORT_SYMBOL(twl6040_set_pll);
446
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300447int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500448{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300449 if (twl6040->power_count)
450 return twl6040->pll;
451 else
452 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500453}
454EXPORT_SYMBOL(twl6040_get_pll);
455
456unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
457{
458 return twl6040->sysclk;
459}
460EXPORT_SYMBOL(twl6040_get_sysclk);
461
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300462/* Get the combined status of the vibra control register */
463int twl6040_get_vibralr_status(struct twl6040 *twl6040)
464{
465 u8 status;
466
467 status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
468 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
469
470 return status;
471}
472EXPORT_SYMBOL(twl6040_get_vibralr_status);
473
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300474static struct resource twl6040_vibra_rsrc[] = {
475 {
476 .flags = IORESOURCE_IRQ,
477 },
478};
479
480static struct resource twl6040_codec_rsrc[] = {
481 {
482 .flags = IORESOURCE_IRQ,
483 },
484};
485
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300486static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500487{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300488 /* Register 0 is not readable */
489 if (!reg)
490 return false;
491 return true;
492}
493
494static struct regmap_config twl6040_regmap_config = {
495 .reg_bits = 8,
496 .val_bits = 8,
497 .max_register = TWL6040_REG_STATUS, /* 0x2e */
498
499 .readable_reg = twl6040_readable_reg,
500};
501
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200502static const struct regmap_irq twl6040_irqs[] = {
503 { .reg_offset = 0, .mask = TWL6040_THINT, },
504 { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
505 { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
506 { .reg_offset = 0, .mask = TWL6040_HFINT, },
507 { .reg_offset = 0, .mask = TWL6040_VIBINT, },
508 { .reg_offset = 0, .mask = TWL6040_READYINT, },
509};
510
511static struct regmap_irq_chip twl6040_irq_chip = {
512 .name = "twl6040",
513 .irqs = twl6040_irqs,
514 .num_irqs = ARRAY_SIZE(twl6040_irqs),
515
516 .num_regs = 1,
517 .status_base = TWL6040_REG_INTID,
518 .mask_base = TWL6040_REG_INTMR,
519};
520
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300521static int __devinit twl6040_probe(struct i2c_client *client,
522 const struct i2c_device_id *id)
523{
524 struct twl6040_platform_data *pdata = client->dev.platform_data;
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300525 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500526 struct twl6040 *twl6040;
527 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300528 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500529
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300530 if (!pdata && !node) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300531 dev_err(&client->dev, "Platform data is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500532 return -EINVAL;
533 }
534
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300535 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300536 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300537 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300538 return -EINVAL;
539 }
540
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300541 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
542 GFP_KERNEL);
543 if (!twl6040) {
544 ret = -ENOMEM;
545 goto err;
546 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500547
Axel Linbbf6adc2012-04-25 10:09:46 +0800548 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300549 if (IS_ERR(twl6040->regmap)) {
550 ret = PTR_ERR(twl6040->regmap);
551 goto err;
552 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500553
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300554 i2c_set_clientdata(client, twl6040);
555
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300556 twl6040->supplies[0].supply = "vio";
557 twl6040->supplies[1].supply = "v2v1";
558 ret = regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
559 twl6040->supplies);
560 if (ret != 0) {
561 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
562 goto regulator_get_err;
563 }
564
565 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
566 if (ret != 0) {
567 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
568 goto power_err;
569 }
570
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300571 twl6040->dev = &client->dev;
572 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500573
574 mutex_init(&twl6040->mutex);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500575 init_completion(&twl6040->ready);
576
577 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
578
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300579 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300580 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) {
581 if (pdata)
582 twl6040->audpwron = pdata->audpwron_gpio;
583 else
584 twl6040->audpwron = of_get_named_gpio(node,
585 "ti,audpwron-gpio", 0);
586 } else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300587 twl6040->audpwron = -EINVAL;
588
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500589 if (gpio_is_valid(twl6040->audpwron)) {
Axel Linb04edb92011-12-01 09:55:07 +0800590 ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW,
591 "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500592 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300593 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500594 }
595
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200596 ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq,
597 IRQF_ONESHOT, 0, &twl6040_irq_chip,
598 &twl6040->irq_data);
599 if (ret < 0)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300600 goto irq_init_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500601
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200602 twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
603 TWL6040_IRQ_READY);
604 twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
605 TWL6040_IRQ_TH);
606
607 ret = request_threaded_irq(twl6040->irq_ready, NULL,
608 twl6040_readyint_handler, IRQF_ONESHOT,
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300609 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300610 if (ret) {
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200611 dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
612 goto readyirq_err;
613 }
614
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200615 ret = request_threaded_irq(twl6040->irq_th, NULL,
616 twl6040_thint_handler, IRQF_ONESHOT,
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200617 "twl6040_irq_th", twl6040);
618 if (ret) {
619 dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
620 goto thirq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500621 }
622
623 /* dual-access registers controlled by I2C only */
624 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
625
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300626 /*
627 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
628 * We can add the ASoC codec child whenever this driver has been loaded.
629 * The ASoC codec can work without pdata, pass the platform_data only if
630 * it has been provided.
631 */
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200632 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300633 cell = &twl6040->cells[children];
634 cell->name = "twl6040-codec";
635 twl6040_codec_rsrc[0].start = irq;
636 twl6040_codec_rsrc[0].end = irq;
637 cell->resources = twl6040_codec_rsrc;
638 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300639 if (pdata && pdata->codec) {
Peter Ujfalusi6c448632011-06-01 13:05:10 +0300640 cell->platform_data = pdata->codec;
641 cell->pdata_size = sizeof(*pdata->codec);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500642 }
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300643 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500644
Samuel Ortizca2cad62012-05-23 16:23:21 +0200645 if (twl6040_has_vibra(pdata, node)) {
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200646 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300647
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500648 cell = &twl6040->cells[children];
649 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300650 twl6040_vibra_rsrc[0].start = irq;
651 twl6040_vibra_rsrc[0].end = irq;
652 cell->resources = twl6040_vibra_rsrc;
653 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
654
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300655 if (pdata && pdata->vibra) {
656 cell->platform_data = pdata->vibra;
657 cell->pdata_size = sizeof(*pdata->vibra);
658 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500659 children++;
660 }
661
Peter Ujfalusi5cbe7862012-08-16 15:13:14 +0300662 /*
663 * Enable the GPO driver in the following cases:
664 * DT booted kernel or legacy boot with valid gpo platform_data
665 */
666 if (!pdata || (pdata && pdata->gpo)) {
667 cell = &twl6040->cells[children];
668 cell->name = "twl6040-gpo";
669
670 if (pdata) {
671 cell->platform_data = pdata->gpo;
672 cell->pdata_size = sizeof(*pdata->gpo);
673 }
674 children++;
675 }
676
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300677 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
Mark Brown55692af2012-09-11 15:16:36 +0800678 NULL, 0, NULL);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300679 if (ret)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500680 goto mfd_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500681
682 return 0;
683
684mfd_err:
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200685 free_irq(twl6040->irq_th, twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200686thirq_err:
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200687 free_irq(twl6040->irq_ready, twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200688readyirq_err:
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200689 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300690irq_init_err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500691 if (gpio_is_valid(twl6040->audpwron))
692 gpio_free(twl6040->audpwron);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300693gpio_err:
694 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
695power_err:
696 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
697regulator_get_err:
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300698 i2c_set_clientdata(client, NULL);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300699err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500700 return ret;
701}
702
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300703static int __devexit twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500704{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300705 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500706
707 if (twl6040->power_count)
708 twl6040_power(twl6040, 0);
709
710 if (gpio_is_valid(twl6040->audpwron))
711 gpio_free(twl6040->audpwron);
712
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200713 free_irq(twl6040->irq_ready, twl6040);
714 free_irq(twl6040->irq_th, twl6040);
715 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500716
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300717 mfd_remove_devices(&client->dev);
718 i2c_set_clientdata(client, NULL);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500719
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300720 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
721 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
722
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500723 return 0;
724}
725
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300726static const struct i2c_device_id twl6040_i2c_id[] = {
727 { "twl6040", 0, },
Peter Ujfalusi1fc74ae2012-07-16 11:49:44 +0200728 { "twl6041", 0, },
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300729 { },
730};
731MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
732
733static struct i2c_driver twl6040_driver = {
734 .driver = {
735 .name = "twl6040",
736 .owner = THIS_MODULE,
737 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500738 .probe = twl6040_probe,
739 .remove = __devexit_p(twl6040_remove),
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300740 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500741};
742
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300743module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500744
745MODULE_DESCRIPTION("TWL6040 MFD");
746MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
747MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
748MODULE_LICENSE("GPL");
749MODULE_ALIAS("platform:twl6040");