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