pandora: reserve CMA area for c64_tools
[pandora-kernel.git] / drivers / mfd / twl6040-core.c
1 /*
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>
30 #include <linux/platform_device.h>
31 #include <linux/gpio.h>
32 #include <linux/delay.h>
33 #include <linux/i2c/twl.h>
34 #include <linux/mfd/core.h>
35 #include <linux/mfd/twl6040.h>
36
37 #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
38
39 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
40 {
41         int ret;
42         u8 val = 0;
43
44         mutex_lock(&twl6040->io_mutex);
45         /* Vibra control registers from cache */
46         if (unlikely(reg == TWL6040_REG_VIBCTLL ||
47                      reg == TWL6040_REG_VIBCTLR)) {
48                 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
49         } else {
50                 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
51                 if (ret < 0) {
52                         mutex_unlock(&twl6040->io_mutex);
53                         return ret;
54                 }
55         }
56         mutex_unlock(&twl6040->io_mutex);
57
58         return val;
59 }
60 EXPORT_SYMBOL(twl6040_reg_read);
61
62 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
63 {
64         int ret;
65
66         mutex_lock(&twl6040->io_mutex);
67         ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
68         /* Cache the vibra control registers */
69         if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
70                 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
71         mutex_unlock(&twl6040->io_mutex);
72
73         return ret;
74 }
75 EXPORT_SYMBOL(twl6040_reg_write);
76
77 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
78 {
79         int ret;
80         u8 val;
81
82         mutex_lock(&twl6040->io_mutex);
83         ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
84         if (ret)
85                 goto out;
86
87         val |= mask;
88         ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
89 out:
90         mutex_unlock(&twl6040->io_mutex);
91         return ret;
92 }
93 EXPORT_SYMBOL(twl6040_set_bits);
94
95 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
96 {
97         int ret;
98         u8 val;
99
100         mutex_lock(&twl6040->io_mutex);
101         ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
102         if (ret)
103                 goto out;
104
105         val &= ~mask;
106         ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
107 out:
108         mutex_unlock(&twl6040->io_mutex);
109         return ret;
110 }
111 EXPORT_SYMBOL(twl6040_clear_bits);
112
113 /* twl6040 codec manual power-up sequence */
114 static int twl6040_power_up(struct twl6040 *twl6040)
115 {
116         u8 ldoctl, ncpctl, lppllctl;
117         int ret;
118
119         /* enable high-side LDO, reference system and internal oscillator */
120         ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
121         ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
122         if (ret)
123                 return ret;
124         usleep_range(10000, 10500);
125
126         /* enable negative charge pump */
127         ncpctl = TWL6040_NCPENA;
128         ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
129         if (ret)
130                 goto ncp_err;
131         usleep_range(1000, 1500);
132
133         /* enable low-side LDO */
134         ldoctl |= TWL6040_LSLDOENA;
135         ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
136         if (ret)
137                 goto lsldo_err;
138         usleep_range(1000, 1500);
139
140         /* enable low-power PLL */
141         lppllctl = TWL6040_LPLLENA;
142         ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
143         if (ret)
144                 goto lppll_err;
145         usleep_range(5000, 5500);
146
147         /* disable internal oscillator */
148         ldoctl &= ~TWL6040_OSCENA;
149         ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
150         if (ret)
151                 goto osc_err;
152
153         return 0;
154
155 osc_err:
156         lppllctl &= ~TWL6040_LPLLENA;
157         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
158 lppll_err:
159         ldoctl &= ~TWL6040_LSLDOENA;
160         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
161 lsldo_err:
162         ncpctl &= ~TWL6040_NCPENA;
163         twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
164 ncp_err:
165         ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
166         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
167
168         return ret;
169 }
170
171 /* twl6040 manual power-down sequence */
172 static void twl6040_power_down(struct twl6040 *twl6040)
173 {
174         u8 ncpctl, ldoctl, lppllctl;
175
176         ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
177         ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
178         lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
179
180         /* enable internal oscillator */
181         ldoctl |= TWL6040_OSCENA;
182         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
183         usleep_range(1000, 1500);
184
185         /* disable low-power PLL */
186         lppllctl &= ~TWL6040_LPLLENA;
187         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
188
189         /* disable low-side LDO */
190         ldoctl &= ~TWL6040_LSLDOENA;
191         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
192
193         /* disable negative charge pump */
194         ncpctl &= ~TWL6040_NCPENA;
195         twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
196
197         /* disable high-side LDO, reference system and internal oscillator */
198         ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
199         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
200 }
201
202 static irqreturn_t twl6040_naudint_handler(int irq, void *data)
203 {
204         struct twl6040 *twl6040 = data;
205         u8 intid, status;
206
207         intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
208
209         if (intid & TWL6040_READYINT)
210                 complete(&twl6040->ready);
211
212         if (intid & TWL6040_THINT) {
213                 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
214                 if (status & TWL6040_TSHUTDET) {
215                         dev_warn(twl6040->dev,
216                                  "Thermal shutdown, powering-off");
217                         twl6040_power(twl6040, 0);
218                 } else {
219                         dev_warn(twl6040->dev,
220                                  "Leaving thermal shutdown, powering-on");
221                         twl6040_power(twl6040, 1);
222                 }
223         }
224
225         return IRQ_HANDLED;
226 }
227
228 static int twl6040_power_up_completion(struct twl6040 *twl6040,
229                                        int naudint)
230 {
231         int time_left;
232         u8 intid;
233
234         time_left = wait_for_completion_timeout(&twl6040->ready,
235                                                 msecs_to_jiffies(144));
236         if (!time_left) {
237                 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
238                 if (!(intid & TWL6040_READYINT)) {
239                         dev_err(twl6040->dev,
240                                 "timeout waiting for READYINT\n");
241                         return -ETIMEDOUT;
242                 }
243         }
244
245         return 0;
246 }
247
248 int twl6040_power(struct twl6040 *twl6040, int on)
249 {
250         int audpwron = twl6040->audpwron;
251         int naudint = twl6040->irq;
252         int ret = 0;
253
254         mutex_lock(&twl6040->mutex);
255
256         if (on) {
257                 /* already powered-up */
258                 if (twl6040->power_count++)
259                         goto out;
260
261                 if (gpio_is_valid(audpwron)) {
262                         /* use AUDPWRON line */
263                         gpio_set_value(audpwron, 1);
264                         /* wait for power-up completion */
265                         ret = twl6040_power_up_completion(twl6040, naudint);
266                         if (ret) {
267                                 dev_err(twl6040->dev,
268                                         "automatic power-down failed\n");
269                                 twl6040->power_count = 0;
270                                 goto out;
271                         }
272                 } else {
273                         /* use manual power-up sequence */
274                         ret = twl6040_power_up(twl6040);
275                         if (ret) {
276                                 dev_err(twl6040->dev,
277                                         "manual power-up failed\n");
278                                 twl6040->power_count = 0;
279                                 goto out;
280                         }
281                 }
282                 /* Default PLL configuration after power up */
283                 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
284                 twl6040->sysclk = 19200000;
285         } else {
286                 /* already powered-down */
287                 if (!twl6040->power_count) {
288                         dev_err(twl6040->dev,
289                                 "device is already powered-off\n");
290                         ret = -EPERM;
291                         goto out;
292                 }
293
294                 if (--twl6040->power_count)
295                         goto out;
296
297                 if (gpio_is_valid(audpwron)) {
298                         /* use AUDPWRON line */
299                         gpio_set_value(audpwron, 0);
300
301                         /* power-down sequence latency */
302                         usleep_range(500, 700);
303                 } else {
304                         /* use manual power-down sequence */
305                         twl6040_power_down(twl6040);
306                 }
307                 twl6040->sysclk = 0;
308         }
309
310 out:
311         mutex_unlock(&twl6040->mutex);
312         return ret;
313 }
314 EXPORT_SYMBOL(twl6040_power);
315
316 int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
317                     unsigned int freq_in, unsigned int freq_out)
318 {
319         u8 hppllctl, lppllctl;
320         int ret = 0;
321
322         mutex_lock(&twl6040->mutex);
323
324         hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
325         lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
326
327         switch (pll_id) {
328         case TWL6040_SYSCLK_SEL_LPPLL:
329                 /* low-power PLL divider */
330                 switch (freq_out) {
331                 case 17640000:
332                         lppllctl |= TWL6040_LPLLFIN;
333                         break;
334                 case 19200000:
335                         lppllctl &= ~TWL6040_LPLLFIN;
336                         break;
337                 default:
338                         dev_err(twl6040->dev,
339                                 "freq_out %d not supported\n", freq_out);
340                         ret = -EINVAL;
341                         goto pll_out;
342                 }
343                 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
344
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:
359                         dev_err(twl6040->dev,
360                                 "freq_in %d not supported\n", freq_in);
361                         ret = -EINVAL;
362                         goto pll_out;
363                 }
364                 break;
365         case TWL6040_SYSCLK_SEL_HPPLL:
366                 /* high-performance PLL can provide only 19.2 MHz */
367                 if (freq_out != 19200000) {
368                         dev_err(twl6040->dev,
369                                 "freq_out %d not supported\n", freq_out);
370                         ret = -EINVAL;
371                         goto pll_out;
372                 }
373
374                 hppllctl &= ~TWL6040_MCLK_MSK;
375
376                 switch (freq_in) {
377                 case 12000000:
378                         /* PLL enabled, active mode */
379                         hppllctl |= TWL6040_MCLK_12000KHZ |
380                                     TWL6040_HPLLENA;
381                         break;
382                 case 19200000:
383                         /*
384                          * PLL disabled
385                          * (enable PLL if MCLK jitter quality
386                          *  doesn't meet specification)
387                          */
388                         hppllctl |= TWL6040_MCLK_19200KHZ;
389                         break;
390                 case 26000000:
391                         /* PLL enabled, active mode */
392                         hppllctl |= TWL6040_MCLK_26000KHZ |
393                                     TWL6040_HPLLENA;
394                         break;
395                 case 38400000:
396                         /* PLL enabled, active mode */
397                         hppllctl |= TWL6040_MCLK_38400KHZ |
398                                     TWL6040_HPLLENA;
399                         break;
400                 default:
401                         dev_err(twl6040->dev,
402                                 "freq_in %d not supported\n", freq_in);
403                         ret = -EINVAL;
404                         goto pll_out;
405                 }
406
407                 /* enable clock slicer to ensure input waveform is square */
408                 hppllctl |= TWL6040_HPLLSQRENA;
409
410                 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, hppllctl);
411                 usleep_range(500, 700);
412                 lppllctl |= TWL6040_HPLLSEL;
413                 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
414                 lppllctl &= ~TWL6040_LPLLENA;
415                 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
416                 break;
417         default:
418                 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
419                 ret = -EINVAL;
420                 goto pll_out;
421         }
422
423         twl6040->sysclk = freq_out;
424         twl6040->pll = pll_id;
425
426 pll_out:
427         mutex_unlock(&twl6040->mutex);
428         return ret;
429 }
430 EXPORT_SYMBOL(twl6040_set_pll);
431
432 int twl6040_get_pll(struct twl6040 *twl6040)
433 {
434         if (twl6040->power_count)
435                 return twl6040->pll;
436         else
437                 return -ENODEV;
438 }
439 EXPORT_SYMBOL(twl6040_get_pll);
440
441 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
442 {
443         return twl6040->sysclk;
444 }
445 EXPORT_SYMBOL(twl6040_get_sysclk);
446
447 /* Get the combined status of the vibra control register */
448 int twl6040_get_vibralr_status(struct twl6040 *twl6040)
449 {
450         u8 status;
451
452         status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
453         status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
454
455         return status;
456 }
457 EXPORT_SYMBOL(twl6040_get_vibralr_status);
458
459 static struct resource twl6040_vibra_rsrc[] = {
460         {
461                 .flags = IORESOURCE_IRQ,
462         },
463 };
464
465 static struct resource twl6040_codec_rsrc[] = {
466         {
467                 .flags = IORESOURCE_IRQ,
468         },
469 };
470
471 static int __devinit twl6040_probe(struct platform_device *pdev)
472 {
473         struct twl4030_audio_data *pdata = pdev->dev.platform_data;
474         struct twl6040 *twl6040;
475         struct mfd_cell *cell = NULL;
476         int ret, children = 0;
477
478         if (!pdata) {
479                 dev_err(&pdev->dev, "Platform data is missing\n");
480                 return -EINVAL;
481         }
482
483         /* In order to operate correctly we need valid interrupt config */
484         if (!pdata->naudint_irq || !pdata->irq_base) {
485                 dev_err(&pdev->dev, "Invalid IRQ configuration\n");
486                 return -EINVAL;
487         }
488
489         twl6040 = kzalloc(sizeof(struct twl6040), GFP_KERNEL);
490         if (!twl6040)
491                 return -ENOMEM;
492
493         platform_set_drvdata(pdev, twl6040);
494
495         twl6040->dev = &pdev->dev;
496         twl6040->irq = pdata->naudint_irq;
497         twl6040->irq_base = pdata->irq_base;
498
499         mutex_init(&twl6040->mutex);
500         mutex_init(&twl6040->io_mutex);
501         init_completion(&twl6040->ready);
502
503         twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
504
505         /* ERRATA: Automatic power-up is not possible in ES1.0 */
506         if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
507                 twl6040->audpwron = pdata->audpwron_gpio;
508         else
509                 twl6040->audpwron = -EINVAL;
510
511         if (gpio_is_valid(twl6040->audpwron)) {
512                 ret = gpio_request(twl6040->audpwron, "audpwron");
513                 if (ret)
514                         goto gpio1_err;
515
516                 ret = gpio_direction_output(twl6040->audpwron, 0);
517                 if (ret)
518                         goto gpio2_err;
519         }
520
521         /* codec interrupt */
522         ret = twl6040_irq_init(twl6040);
523         if (ret)
524                 goto gpio2_err;
525
526         ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
527                                    NULL, twl6040_naudint_handler, 0,
528                                    "twl6040_irq_ready", twl6040);
529         if (ret) {
530                 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
531                         ret);
532                 goto irq_err;
533         }
534
535         /* dual-access registers controlled by I2C only */
536         twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
537
538         if (pdata->codec) {
539                 int irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
540
541                 cell = &twl6040->cells[children];
542                 cell->name = "twl6040-codec";
543                 twl6040_codec_rsrc[0].start = irq;
544                 twl6040_codec_rsrc[0].end = irq;
545                 cell->resources = twl6040_codec_rsrc;
546                 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
547                 cell->platform_data = pdata->codec;
548                 cell->pdata_size = sizeof(*pdata->codec);
549                 children++;
550         }
551
552         if (pdata->vibra) {
553                 int irq = twl6040->irq_base + TWL6040_IRQ_VIB;
554
555                 cell = &twl6040->cells[children];
556                 cell->name = "twl6040-vibra";
557                 twl6040_vibra_rsrc[0].start = irq;
558                 twl6040_vibra_rsrc[0].end = irq;
559                 cell->resources = twl6040_vibra_rsrc;
560                 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
561
562                 cell->platform_data = pdata->vibra;
563                 cell->pdata_size = sizeof(*pdata->vibra);
564                 children++;
565         }
566
567         if (children) {
568                 ret = mfd_add_devices(&pdev->dev, pdev->id, twl6040->cells,
569                                       children, NULL, 0);
570                 if (ret)
571                         goto mfd_err;
572         } else {
573                 dev_err(&pdev->dev, "No platform data found for children\n");
574                 ret = -ENODEV;
575                 goto mfd_err;
576         }
577
578         return 0;
579
580 mfd_err:
581         free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
582 irq_err:
583         twl6040_irq_exit(twl6040);
584 gpio2_err:
585         if (gpio_is_valid(twl6040->audpwron))
586                 gpio_free(twl6040->audpwron);
587 gpio1_err:
588         platform_set_drvdata(pdev, NULL);
589         kfree(twl6040);
590         return ret;
591 }
592
593 static int __devexit twl6040_remove(struct platform_device *pdev)
594 {
595         struct twl6040 *twl6040 = platform_get_drvdata(pdev);
596
597         if (twl6040->power_count)
598                 twl6040_power(twl6040, 0);
599
600         if (gpio_is_valid(twl6040->audpwron))
601                 gpio_free(twl6040->audpwron);
602
603         free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
604         twl6040_irq_exit(twl6040);
605
606         mfd_remove_devices(&pdev->dev);
607         platform_set_drvdata(pdev, NULL);
608         kfree(twl6040);
609
610         return 0;
611 }
612
613 static struct platform_driver twl6040_driver = {
614         .probe          = twl6040_probe,
615         .remove         = __devexit_p(twl6040_remove),
616         .driver         = {
617                 .owner  = THIS_MODULE,
618                 .name   = "twl6040",
619         },
620 };
621
622 static int __devinit twl6040_init(void)
623 {
624         return platform_driver_register(&twl6040_driver);
625 }
626 module_init(twl6040_init);
627
628 static void __devexit twl6040_exit(void)
629 {
630         platform_driver_unregister(&twl6040_driver);
631 }
632
633 module_exit(twl6040_exit);
634
635 MODULE_DESCRIPTION("TWL6040 MFD");
636 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
637 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
638 MODULE_LICENSE("GPL");
639 MODULE_ALIAS("platform:twl6040");