blob: 4ded9e7aa246efdc0e38347fab044f46ef2dc7ef [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
67 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030068 /* Vibra control registers from cache */
69 if (unlikely(reg == TWL6040_REG_VIBCTLL ||
70 reg == TWL6040_REG_VIBCTLR)) {
71 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
72 } else {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030073 ret = regmap_read(twl6040->regmap, reg, &val);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030074 if (ret < 0) {
75 mutex_unlock(&twl6040->io_mutex);
76 return ret;
77 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050078 }
79 mutex_unlock(&twl6040->io_mutex);
80
81 return val;
82}
83EXPORT_SYMBOL(twl6040_reg_read);
84
85int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
86{
87 int ret;
88
89 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030090 ret = regmap_write(twl6040->regmap, reg, val);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030091 /* Cache the vibra control registers */
92 if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
93 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050094 mutex_unlock(&twl6040->io_mutex);
95
96 return ret;
97}
98EXPORT_SYMBOL(twl6040_reg_write);
99
100int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
101{
102 int ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500103
104 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300105 ret = regmap_update_bits(twl6040->regmap, reg, mask, mask);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500106 mutex_unlock(&twl6040->io_mutex);
107 return ret;
108}
109EXPORT_SYMBOL(twl6040_set_bits);
110
111int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
112{
113 int ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500114
115 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300116 ret = regmap_update_bits(twl6040->regmap, reg, mask, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500117 mutex_unlock(&twl6040->io_mutex);
118 return ret;
119}
120EXPORT_SYMBOL(twl6040_clear_bits);
121
122/* twl6040 codec manual power-up sequence */
123static int twl6040_power_up(struct twl6040 *twl6040)
124{
125 u8 ldoctl, ncpctl, lppllctl;
126 int ret;
127
128 /* enable high-side LDO, reference system and internal oscillator */
129 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
130 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
131 if (ret)
132 return ret;
133 usleep_range(10000, 10500);
134
135 /* enable negative charge pump */
136 ncpctl = TWL6040_NCPENA;
137 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
138 if (ret)
139 goto ncp_err;
140 usleep_range(1000, 1500);
141
142 /* enable low-side LDO */
143 ldoctl |= TWL6040_LSLDOENA;
144 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
145 if (ret)
146 goto lsldo_err;
147 usleep_range(1000, 1500);
148
149 /* enable low-power PLL */
150 lppllctl = TWL6040_LPLLENA;
151 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
152 if (ret)
153 goto lppll_err;
154 usleep_range(5000, 5500);
155
156 /* disable internal oscillator */
157 ldoctl &= ~TWL6040_OSCENA;
158 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
159 if (ret)
160 goto osc_err;
161
162 return 0;
163
164osc_err:
165 lppllctl &= ~TWL6040_LPLLENA;
166 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
167lppll_err:
168 ldoctl &= ~TWL6040_LSLDOENA;
169 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
170lsldo_err:
171 ncpctl &= ~TWL6040_NCPENA;
172 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
173ncp_err:
174 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
175 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
176
177 return ret;
178}
179
180/* twl6040 manual power-down sequence */
181static void twl6040_power_down(struct twl6040 *twl6040)
182{
183 u8 ncpctl, ldoctl, lppllctl;
184
185 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
186 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
187 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
188
189 /* enable internal oscillator */
190 ldoctl |= TWL6040_OSCENA;
191 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
192 usleep_range(1000, 1500);
193
194 /* disable low-power PLL */
195 lppllctl &= ~TWL6040_LPLLENA;
196 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
197
198 /* disable low-side LDO */
199 ldoctl &= ~TWL6040_LSLDOENA;
200 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
201
202 /* disable negative charge pump */
203 ncpctl &= ~TWL6040_NCPENA;
204 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
205
206 /* disable high-side LDO, reference system and internal oscillator */
207 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
208 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
209}
210
211static irqreturn_t twl6040_naudint_handler(int irq, void *data)
212{
213 struct twl6040 *twl6040 = data;
214 u8 intid, status;
215
216 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
217
218 if (intid & TWL6040_READYINT)
219 complete(&twl6040->ready);
220
221 if (intid & TWL6040_THINT) {
222 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
223 if (status & TWL6040_TSHUTDET) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300224 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500225 "Thermal shutdown, powering-off");
226 twl6040_power(twl6040, 0);
227 } else {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300228 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500229 "Leaving thermal shutdown, powering-on");
230 twl6040_power(twl6040, 1);
231 }
232 }
233
234 return IRQ_HANDLED;
235}
236
237static int twl6040_power_up_completion(struct twl6040 *twl6040,
238 int naudint)
239{
240 int time_left;
241 u8 intid;
242
243 time_left = wait_for_completion_timeout(&twl6040->ready,
244 msecs_to_jiffies(144));
245 if (!time_left) {
246 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
247 if (!(intid & TWL6040_READYINT)) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300248 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500249 "timeout waiting for READYINT\n");
250 return -ETIMEDOUT;
251 }
252 }
253
254 return 0;
255}
256
257int twl6040_power(struct twl6040 *twl6040, int on)
258{
259 int audpwron = twl6040->audpwron;
260 int naudint = twl6040->irq;
261 int ret = 0;
262
263 mutex_lock(&twl6040->mutex);
264
265 if (on) {
266 /* already powered-up */
267 if (twl6040->power_count++)
268 goto out;
269
270 if (gpio_is_valid(audpwron)) {
271 /* use AUDPWRON line */
272 gpio_set_value(audpwron, 1);
273 /* wait for power-up completion */
274 ret = twl6040_power_up_completion(twl6040, naudint);
275 if (ret) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300276 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500277 "automatic power-down failed\n");
278 twl6040->power_count = 0;
279 goto out;
280 }
281 } else {
282 /* use manual power-up sequence */
283 ret = twl6040_power_up(twl6040);
284 if (ret) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300285 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500286 "manual power-up failed\n");
287 twl6040->power_count = 0;
288 goto out;
289 }
290 }
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300291 /* Default PLL configuration after power up */
292 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500293 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100294 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500295 } else {
296 /* already powered-down */
297 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300298 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500299 "device is already powered-off\n");
300 ret = -EPERM;
301 goto out;
302 }
303
304 if (--twl6040->power_count)
305 goto out;
306
307 if (gpio_is_valid(audpwron)) {
308 /* use AUDPWRON line */
309 gpio_set_value(audpwron, 0);
310
311 /* power-down sequence latency */
312 usleep_range(500, 700);
313 } else {
314 /* use manual power-down sequence */
315 twl6040_power_down(twl6040);
316 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500317 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100318 twl6040->mclk = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500319 }
320
321out:
322 mutex_unlock(&twl6040->mutex);
323 return ret;
324}
325EXPORT_SYMBOL(twl6040_power);
326
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300327int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500328 unsigned int freq_in, unsigned int freq_out)
329{
330 u8 hppllctl, lppllctl;
331 int ret = 0;
332
333 mutex_lock(&twl6040->mutex);
334
335 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
336 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
337
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100338 /* Force full reconfiguration when switching between PLL */
339 if (pll_id != twl6040->pll) {
340 twl6040->sysclk = 0;
341 twl6040->mclk = 0;
342 }
343
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300344 switch (pll_id) {
345 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500346 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100347 /* Change the sysclk configuration only if it has been canged */
348 if (twl6040->sysclk != freq_out) {
349 switch (freq_out) {
350 case 17640000:
351 lppllctl |= TWL6040_LPLLFIN;
352 break;
353 case 19200000:
354 lppllctl &= ~TWL6040_LPLLFIN;
355 break;
356 default:
357 dev_err(twl6040->dev,
358 "freq_out %d not supported\n",
359 freq_out);
360 ret = -EINVAL;
361 goto pll_out;
362 }
363 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
364 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500365 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100366
367 /* The PLL in use has not been change, we can exit */
368 if (twl6040->pll == pll_id)
369 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500370
371 switch (freq_in) {
372 case 32768:
373 lppllctl |= TWL6040_LPLLENA;
374 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
375 lppllctl);
376 mdelay(5);
377 lppllctl &= ~TWL6040_HPLLSEL;
378 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
379 lppllctl);
380 hppllctl &= ~TWL6040_HPLLENA;
381 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
382 hppllctl);
383 break;
384 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300385 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500386 "freq_in %d not supported\n", freq_in);
387 ret = -EINVAL;
388 goto pll_out;
389 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500390 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300391 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500392 /* high-performance PLL can provide only 19.2 MHz */
393 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300394 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500395 "freq_out %d not supported\n", freq_out);
396 ret = -EINVAL;
397 goto pll_out;
398 }
399
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100400 if (twl6040->mclk != freq_in) {
401 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500402
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100403 switch (freq_in) {
404 case 12000000:
405 /* PLL enabled, active mode */
406 hppllctl |= TWL6040_MCLK_12000KHZ |
407 TWL6040_HPLLENA;
408 break;
409 case 19200000:
410 /*
411 * PLL disabled
412 * (enable PLL if MCLK jitter quality
413 * doesn't meet specification)
414 */
415 hppllctl |= TWL6040_MCLK_19200KHZ;
416 break;
417 case 26000000:
418 /* PLL enabled, active mode */
419 hppllctl |= TWL6040_MCLK_26000KHZ |
420 TWL6040_HPLLENA;
421 break;
422 case 38400000:
423 /* PLL enabled, active mode */
424 hppllctl |= TWL6040_MCLK_38400KHZ |
425 TWL6040_HPLLENA;
426 break;
427 default:
428 dev_err(twl6040->dev,
429 "freq_in %d not supported\n", freq_in);
430 ret = -EINVAL;
431 goto pll_out;
432 }
433
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500434 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100435 * enable clock slicer to ensure input waveform is
436 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500437 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100438 hppllctl |= TWL6040_HPLLSQRENA;
439
440 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
441 hppllctl);
442 usleep_range(500, 700);
443 lppllctl |= TWL6040_HPLLSEL;
444 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
445 lppllctl);
446 lppllctl &= ~TWL6040_LPLLENA;
447 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
448 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500449 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500450 break;
451 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300452 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500453 ret = -EINVAL;
454 goto pll_out;
455 }
456
457 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100458 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300459 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500460
461pll_out:
462 mutex_unlock(&twl6040->mutex);
463 return ret;
464}
465EXPORT_SYMBOL(twl6040_set_pll);
466
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300467int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500468{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300469 if (twl6040->power_count)
470 return twl6040->pll;
471 else
472 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500473}
474EXPORT_SYMBOL(twl6040_get_pll);
475
476unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
477{
478 return twl6040->sysclk;
479}
480EXPORT_SYMBOL(twl6040_get_sysclk);
481
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300482/* Get the combined status of the vibra control register */
483int twl6040_get_vibralr_status(struct twl6040 *twl6040)
484{
485 u8 status;
486
487 status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
488 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
489
490 return status;
491}
492EXPORT_SYMBOL(twl6040_get_vibralr_status);
493
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300494static struct resource twl6040_vibra_rsrc[] = {
495 {
496 .flags = IORESOURCE_IRQ,
497 },
498};
499
500static struct resource twl6040_codec_rsrc[] = {
501 {
502 .flags = IORESOURCE_IRQ,
503 },
504};
505
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300506static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500507{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300508 /* Register 0 is not readable */
509 if (!reg)
510 return false;
511 return true;
512}
513
514static struct regmap_config twl6040_regmap_config = {
515 .reg_bits = 8,
516 .val_bits = 8,
517 .max_register = TWL6040_REG_STATUS, /* 0x2e */
518
519 .readable_reg = twl6040_readable_reg,
520};
521
522static int __devinit twl6040_probe(struct i2c_client *client,
523 const struct i2c_device_id *id)
524{
525 struct twl6040_platform_data *pdata = client->dev.platform_data;
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300526 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500527 struct twl6040 *twl6040;
528 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300529 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500530
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300531 if (!pdata && !node) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300532 dev_err(&client->dev, "Platform data is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500533 return -EINVAL;
534 }
535
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300536 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300537 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300538 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300539 return -EINVAL;
540 }
541
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300542 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
543 GFP_KERNEL);
544 if (!twl6040) {
545 ret = -ENOMEM;
546 goto err;
547 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500548
Axel Linbbf6adc2012-04-25 10:09:46 +0800549 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300550 if (IS_ERR(twl6040->regmap)) {
551 ret = PTR_ERR(twl6040->regmap);
552 goto err;
553 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500554
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300555 i2c_set_clientdata(client, twl6040);
556
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300557 twl6040->supplies[0].supply = "vio";
558 twl6040->supplies[1].supply = "v2v1";
559 ret = regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
560 twl6040->supplies);
561 if (ret != 0) {
562 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
563 goto regulator_get_err;
564 }
565
566 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
567 if (ret != 0) {
568 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
569 goto power_err;
570 }
571
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300572 twl6040->dev = &client->dev;
573 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500574
575 mutex_init(&twl6040->mutex);
576 mutex_init(&twl6040->io_mutex);
577 init_completion(&twl6040->ready);
578
579 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
580
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300581 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300582 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) {
583 if (pdata)
584 twl6040->audpwron = pdata->audpwron_gpio;
585 else
586 twl6040->audpwron = of_get_named_gpio(node,
587 "ti,audpwron-gpio", 0);
588 } else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300589 twl6040->audpwron = -EINVAL;
590
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500591 if (gpio_is_valid(twl6040->audpwron)) {
Axel Linb04edb92011-12-01 09:55:07 +0800592 ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW,
593 "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500594 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300595 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500596 }
597
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300598 /* codec interrupt */
599 ret = twl6040_irq_init(twl6040);
600 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300601 goto irq_init_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500602
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300603 ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
604 NULL, twl6040_naudint_handler, 0,
605 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300606 if (ret) {
607 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
608 ret);
609 goto irq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500610 }
611
612 /* dual-access registers controlled by I2C only */
613 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
614
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300615 /*
616 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
617 * We can add the ASoC codec child whenever this driver has been loaded.
618 * The ASoC codec can work without pdata, pass the platform_data only if
619 * it has been provided.
620 */
621 irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
622 cell = &twl6040->cells[children];
623 cell->name = "twl6040-codec";
624 twl6040_codec_rsrc[0].start = irq;
625 twl6040_codec_rsrc[0].end = irq;
626 cell->resources = twl6040_codec_rsrc;
627 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300628 if (pdata && pdata->codec) {
Peter Ujfalusi6c448632011-06-01 13:05:10 +0300629 cell->platform_data = pdata->codec;
630 cell->pdata_size = sizeof(*pdata->codec);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500631 }
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300632 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500633
Samuel Ortizca2cad62012-05-23 16:23:21 +0200634 if (twl6040_has_vibra(pdata, node)) {
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300635 irq = twl6040->irq_base + TWL6040_IRQ_VIB;
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300636
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500637 cell = &twl6040->cells[children];
638 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300639 twl6040_vibra_rsrc[0].start = irq;
640 twl6040_vibra_rsrc[0].end = irq;
641 cell->resources = twl6040_vibra_rsrc;
642 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
643
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300644 if (pdata && pdata->vibra) {
645 cell->platform_data = pdata->vibra;
646 cell->pdata_size = sizeof(*pdata->vibra);
647 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500648 children++;
649 }
650
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300651 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
652 NULL, 0);
653 if (ret)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500654 goto mfd_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500655
656 return 0;
657
658mfd_err:
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300659 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500660irq_err:
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300661 twl6040_irq_exit(twl6040);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300662irq_init_err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500663 if (gpio_is_valid(twl6040->audpwron))
664 gpio_free(twl6040->audpwron);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300665gpio_err:
666 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
667power_err:
668 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
669regulator_get_err:
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300670 i2c_set_clientdata(client, NULL);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300671err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500672 return ret;
673}
674
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300675static int __devexit twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500676{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300677 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500678
679 if (twl6040->power_count)
680 twl6040_power(twl6040, 0);
681
682 if (gpio_is_valid(twl6040->audpwron))
683 gpio_free(twl6040->audpwron);
684
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300685 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300686 twl6040_irq_exit(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500687
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300688 mfd_remove_devices(&client->dev);
689 i2c_set_clientdata(client, NULL);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500690
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300691 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
692 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
693
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500694 return 0;
695}
696
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300697static const struct i2c_device_id twl6040_i2c_id[] = {
698 { "twl6040", 0, },
699 { },
700};
701MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
702
703static struct i2c_driver twl6040_driver = {
704 .driver = {
705 .name = "twl6040",
706 .owner = THIS_MODULE,
707 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500708 .probe = twl6040_probe,
709 .remove = __devexit_p(twl6040_remove),
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300710 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500711};
712
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300713module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500714
715MODULE_DESCRIPTION("TWL6040 MFD");
716MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
717MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
718MODULE_LICENSE("GPL");
719MODULE_ALIAS("platform:twl6040");