blob: 6d9db275b0f1220bacbe227d1edf3fa9d5d56eab [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
196static irqreturn_t twl6040_naudint_handler(int irq, void *data)
197{
198 struct twl6040 *twl6040 = data;
199 u8 intid, status;
200
201 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
202
203 if (intid & TWL6040_READYINT)
204 complete(&twl6040->ready);
205
206 if (intid & TWL6040_THINT) {
207 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
208 if (status & TWL6040_TSHUTDET) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300209 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500210 "Thermal shutdown, powering-off");
211 twl6040_power(twl6040, 0);
212 } else {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300213 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500214 "Leaving thermal shutdown, powering-on");
215 twl6040_power(twl6040, 1);
216 }
217 }
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
502static int __devinit twl6040_probe(struct i2c_client *client,
503 const struct i2c_device_id *id)
504{
505 struct twl6040_platform_data *pdata = client->dev.platform_data;
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300506 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500507 struct twl6040 *twl6040;
508 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300509 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500510
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300511 if (!pdata && !node) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300512 dev_err(&client->dev, "Platform data is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500513 return -EINVAL;
514 }
515
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300516 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300517 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300518 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300519 return -EINVAL;
520 }
521
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300522 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
523 GFP_KERNEL);
524 if (!twl6040) {
525 ret = -ENOMEM;
526 goto err;
527 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500528
Axel Linbbf6adc2012-04-25 10:09:46 +0800529 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300530 if (IS_ERR(twl6040->regmap)) {
531 ret = PTR_ERR(twl6040->regmap);
532 goto err;
533 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500534
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300535 i2c_set_clientdata(client, twl6040);
536
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300537 twl6040->supplies[0].supply = "vio";
538 twl6040->supplies[1].supply = "v2v1";
539 ret = regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
540 twl6040->supplies);
541 if (ret != 0) {
542 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
543 goto regulator_get_err;
544 }
545
546 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
547 if (ret != 0) {
548 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
549 goto power_err;
550 }
551
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300552 twl6040->dev = &client->dev;
553 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500554
555 mutex_init(&twl6040->mutex);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500556 init_completion(&twl6040->ready);
557
558 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
559
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300560 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300561 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) {
562 if (pdata)
563 twl6040->audpwron = pdata->audpwron_gpio;
564 else
565 twl6040->audpwron = of_get_named_gpio(node,
566 "ti,audpwron-gpio", 0);
567 } else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300568 twl6040->audpwron = -EINVAL;
569
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500570 if (gpio_is_valid(twl6040->audpwron)) {
Axel Linb04edb92011-12-01 09:55:07 +0800571 ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW,
572 "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500573 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300574 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500575 }
576
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300577 /* codec interrupt */
578 ret = twl6040_irq_init(twl6040);
579 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300580 goto irq_init_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500581
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300582 ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
Fengguang Wu09e09062012-09-19 09:32:38 +0800583 NULL, twl6040_naudint_handler, IRQF_ONESHOT,
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300584 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300585 if (ret) {
586 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
587 ret);
588 goto irq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500589 }
590
591 /* dual-access registers controlled by I2C only */
592 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
593
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300594 /*
595 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
596 * We can add the ASoC codec child whenever this driver has been loaded.
597 * The ASoC codec can work without pdata, pass the platform_data only if
598 * it has been provided.
599 */
600 irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
601 cell = &twl6040->cells[children];
602 cell->name = "twl6040-codec";
603 twl6040_codec_rsrc[0].start = irq;
604 twl6040_codec_rsrc[0].end = irq;
605 cell->resources = twl6040_codec_rsrc;
606 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300607 if (pdata && pdata->codec) {
Peter Ujfalusi6c448632011-06-01 13:05:10 +0300608 cell->platform_data = pdata->codec;
609 cell->pdata_size = sizeof(*pdata->codec);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500610 }
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300611 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500612
Samuel Ortizca2cad62012-05-23 16:23:21 +0200613 if (twl6040_has_vibra(pdata, node)) {
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300614 irq = twl6040->irq_base + TWL6040_IRQ_VIB;
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300615
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500616 cell = &twl6040->cells[children];
617 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300618 twl6040_vibra_rsrc[0].start = irq;
619 twl6040_vibra_rsrc[0].end = irq;
620 cell->resources = twl6040_vibra_rsrc;
621 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
622
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300623 if (pdata && pdata->vibra) {
624 cell->platform_data = pdata->vibra;
625 cell->pdata_size = sizeof(*pdata->vibra);
626 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500627 children++;
628 }
629
Peter Ujfalusi5cbe7862012-08-16 15:13:14 +0300630 /*
631 * Enable the GPO driver in the following cases:
632 * DT booted kernel or legacy boot with valid gpo platform_data
633 */
634 if (!pdata || (pdata && pdata->gpo)) {
635 cell = &twl6040->cells[children];
636 cell->name = "twl6040-gpo";
637
638 if (pdata) {
639 cell->platform_data = pdata->gpo;
640 cell->pdata_size = sizeof(*pdata->gpo);
641 }
642 children++;
643 }
644
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300645 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
Mark Brown55692af2012-09-11 15:16:36 +0800646 NULL, 0, NULL);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300647 if (ret)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500648 goto mfd_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500649
650 return 0;
651
652mfd_err:
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300653 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500654irq_err:
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300655 twl6040_irq_exit(twl6040);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300656irq_init_err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500657 if (gpio_is_valid(twl6040->audpwron))
658 gpio_free(twl6040->audpwron);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300659gpio_err:
660 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
661power_err:
662 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
663regulator_get_err:
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300664 i2c_set_clientdata(client, NULL);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300665err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500666 return ret;
667}
668
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300669static int __devexit twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500670{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300671 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500672
673 if (twl6040->power_count)
674 twl6040_power(twl6040, 0);
675
676 if (gpio_is_valid(twl6040->audpwron))
677 gpio_free(twl6040->audpwron);
678
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300679 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300680 twl6040_irq_exit(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500681
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300682 mfd_remove_devices(&client->dev);
683 i2c_set_clientdata(client, NULL);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500684
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300685 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
686 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
687
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500688 return 0;
689}
690
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300691static const struct i2c_device_id twl6040_i2c_id[] = {
692 { "twl6040", 0, },
Peter Ujfalusi1fc74ae2012-07-16 11:49:44 +0200693 { "twl6041", 0, },
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300694 { },
695};
696MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
697
698static struct i2c_driver twl6040_driver = {
699 .driver = {
700 .name = "twl6040",
701 .owner = THIS_MODULE,
702 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500703 .probe = twl6040_probe,
704 .remove = __devexit_p(twl6040_remove),
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300705 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500706};
707
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300708module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500709
710MODULE_DESCRIPTION("TWL6040 MFD");
711MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
712MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
713MODULE_LICENSE("GPL");
714MODULE_ALIAS("platform:twl6040");