Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
twl6040-core.c
Go to the documentation of this file.
1 /*
2  * MFD driver for TWL6040 audio device
3  *
4  * Authors: Misael Lopez Cruz <[email protected]>
5  * Jorge Eduardo Candelaria <[email protected]>
6  * Peter Ujfalusi <[email protected]>
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/err.h>
31 #include <linux/platform_device.h>
32 #include <linux/of.h>
33 #include <linux/of_irq.h>
34 #include <linux/of_gpio.h>
35 #include <linux/of_platform.h>
36 #include <linux/gpio.h>
37 #include <linux/delay.h>
38 #include <linux/i2c.h>
39 #include <linux/regmap.h>
40 #include <linux/err.h>
41 #include <linux/mfd/core.h>
42 #include <linux/mfd/twl6040.h>
44 
45 #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
46 #define TWL6040_NUM_SUPPLIES (2)
47 
48 static 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 
62 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
63 {
64  int ret;
65  unsigned int val;
66 
67  /* 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 {
72  ret = regmap_read(twl6040->regmap, reg, &val);
73  if (ret < 0)
74  return ret;
75  }
76 
77  return val;
78 }
80 
81 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
82 {
83  int ret;
84 
85  ret = regmap_write(twl6040->regmap, reg, val);
86  /* Cache the vibra control registers */
87  if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
88  twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
89 
90  return ret;
91 }
93 
94 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
95 {
96  return regmap_update_bits(twl6040->regmap, reg, mask, mask);
97 }
99 
100 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
101 {
102  return regmap_update_bits(twl6040->regmap, reg, mask, 0);
103 }
105 
106 /* twl6040 codec manual power-up sequence */
107 static int twl6040_power_up(struct twl6040 *twl6040)
108 {
109  u8 ldoctl, ncpctl, lppllctl;
110  int ret;
111 
112  /* enable high-side LDO, reference system and internal oscillator */
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 
148 osc_err:
149  lppllctl &= ~TWL6040_LPLLENA;
150  twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
151 lppll_err:
152  ldoctl &= ~TWL6040_LSLDOENA;
153  twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
154 lsldo_err:
155  ncpctl &= ~TWL6040_NCPENA;
156  twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
157 ncp_err:
159  twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
160 
161  return ret;
162 }
163 
164 /* twl6040 manual power-down sequence */
165 static void twl6040_power_down(struct twl6040 *twl6040)
166 {
167  u8 ncpctl, ldoctl, lppllctl;
168 
169  ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
170  ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
171  lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
172 
173  /* enable internal oscillator */
174  ldoctl |= TWL6040_OSCENA;
175  twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
176  usleep_range(1000, 1500);
177 
178  /* disable low-power PLL */
179  lppllctl &= ~TWL6040_LPLLENA;
180  twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
181 
182  /* disable low-side LDO */
183  ldoctl &= ~TWL6040_LSLDOENA;
184  twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
185 
186  /* disable negative charge pump */
187  ncpctl &= ~TWL6040_NCPENA;
188  twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
189 
190  /* disable high-side LDO, reference system and internal oscillator */
192  twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
193 }
194 
195 static irqreturn_t twl6040_naudint_handler(int irq, void *data)
196 {
197  struct twl6040 *twl6040 = data;
198  u8 intid, status;
199 
200  intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
201 
202  if (intid & TWL6040_READYINT)
203  complete(&twl6040->ready);
204 
205  if (intid & TWL6040_THINT) {
206  status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
207  if (status & TWL6040_TSHUTDET) {
208  dev_warn(twl6040->dev,
209  "Thermal shutdown, powering-off");
210  twl6040_power(twl6040, 0);
211  } else {
212  dev_warn(twl6040->dev,
213  "Leaving thermal shutdown, powering-on");
214  twl6040_power(twl6040, 1);
215  }
216  }
217 
218  return IRQ_HANDLED;
219 }
220 
221 static int twl6040_power_up_completion(struct twl6040 *twl6040,
222  int naudint)
223 {
224  int time_left;
225  u8 intid;
226 
227  time_left = wait_for_completion_timeout(&twl6040->ready,
228  msecs_to_jiffies(144));
229  if (!time_left) {
230  intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
231  if (!(intid & TWL6040_READYINT)) {
232  dev_err(twl6040->dev,
233  "timeout waiting for READYINT\n");
234  return -ETIMEDOUT;
235  }
236  }
237 
238  return 0;
239 }
240 
241 int twl6040_power(struct twl6040 *twl6040, int on)
242 {
243  int audpwron = twl6040->audpwron;
244  int naudint = twl6040->irq;
245  int ret = 0;
246 
247  mutex_lock(&twl6040->mutex);
248 
249  if (on) {
250  /* already powered-up */
251  if (twl6040->power_count++)
252  goto out;
253 
254  if (gpio_is_valid(audpwron)) {
255  /* use AUDPWRON line */
256  gpio_set_value(audpwron, 1);
257  /* wait for power-up completion */
258  ret = twl6040_power_up_completion(twl6040, naudint);
259  if (ret) {
260  dev_err(twl6040->dev,
261  "automatic power-down failed\n");
262  twl6040->power_count = 0;
263  goto out;
264  }
265  } else {
266  /* use manual power-up sequence */
267  ret = twl6040_power_up(twl6040);
268  if (ret) {
269  dev_err(twl6040->dev,
270  "manual power-up failed\n");
271  twl6040->power_count = 0;
272  goto out;
273  }
274  }
275  /* Default PLL configuration after power up */
276  twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
277  twl6040->sysclk = 19200000;
278  twl6040->mclk = 32768;
279  } else {
280  /* already powered-down */
281  if (!twl6040->power_count) {
282  dev_err(twl6040->dev,
283  "device is already powered-off\n");
284  ret = -EPERM;
285  goto out;
286  }
287 
288  if (--twl6040->power_count)
289  goto out;
290 
291  if (gpio_is_valid(audpwron)) {
292  /* use AUDPWRON line */
293  gpio_set_value(audpwron, 0);
294 
295  /* power-down sequence latency */
296  usleep_range(500, 700);
297  } else {
298  /* use manual power-down sequence */
299  twl6040_power_down(twl6040);
300  }
301  twl6040->sysclk = 0;
302  twl6040->mclk = 0;
303  }
304 
305 out:
306  mutex_unlock(&twl6040->mutex);
307  return ret;
308 }
310 
311 int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
312  unsigned int freq_in, unsigned int freq_out)
313 {
314  u8 hppllctl, lppllctl;
315  int ret = 0;
316 
317  mutex_lock(&twl6040->mutex);
318 
319  hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
320  lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
321 
322  /* Force full reconfiguration when switching between PLL */
323  if (pll_id != twl6040->pll) {
324  twl6040->sysclk = 0;
325  twl6040->mclk = 0;
326  }
327 
328  switch (pll_id) {
330  /* low-power PLL divider */
331  /* Change the sysclk configuration only if it has been canged */
332  if (twl6040->sysclk != freq_out) {
333  switch (freq_out) {
334  case 17640000:
335  lppllctl |= TWL6040_LPLLFIN;
336  break;
337  case 19200000:
338  lppllctl &= ~TWL6040_LPLLFIN;
339  break;
340  default:
341  dev_err(twl6040->dev,
342  "freq_out %d not supported\n",
343  freq_out);
344  ret = -EINVAL;
345  goto pll_out;
346  }
348  lppllctl);
349  }
350 
351  /* The PLL in use has not been change, we can exit */
352  if (twl6040->pll == pll_id)
353  break;
354 
355  switch (freq_in) {
356  case 32768:
357  lppllctl |= TWL6040_LPLLENA;
359  lppllctl);
360  mdelay(5);
361  lppllctl &= ~TWL6040_HPLLSEL;
363  lppllctl);
364  hppllctl &= ~TWL6040_HPLLENA;
366  hppllctl);
367  break;
368  default:
369  dev_err(twl6040->dev,
370  "freq_in %d not supported\n", freq_in);
371  ret = -EINVAL;
372  goto pll_out;
373  }
374  break;
376  /* high-performance PLL can provide only 19.2 MHz */
377  if (freq_out != 19200000) {
378  dev_err(twl6040->dev,
379  "freq_out %d not supported\n", freq_out);
380  ret = -EINVAL;
381  goto pll_out;
382  }
383 
384  if (twl6040->mclk != freq_in) {
385  hppllctl &= ~TWL6040_MCLK_MSK;
386 
387  switch (freq_in) {
388  case 12000000:
389  /* PLL enabled, active mode */
390  hppllctl |= TWL6040_MCLK_12000KHZ |
392  break;
393  case 19200000:
394  /*
395  * PLL disabled
396  * (enable PLL if MCLK jitter quality
397  * doesn't meet specification)
398  */
399  hppllctl |= TWL6040_MCLK_19200KHZ;
400  break;
401  case 26000000:
402  /* PLL enabled, active mode */
403  hppllctl |= TWL6040_MCLK_26000KHZ |
405  break;
406  case 38400000:
407  /* PLL enabled, active mode */
408  hppllctl |= TWL6040_MCLK_38400KHZ |
410  break;
411  default:
412  dev_err(twl6040->dev,
413  "freq_in %d not supported\n", freq_in);
414  ret = -EINVAL;
415  goto pll_out;
416  }
417 
418  /*
419  * enable clock slicer to ensure input waveform is
420  * square
421  */
422  hppllctl |= TWL6040_HPLLSQRENA;
423 
425  hppllctl);
426  usleep_range(500, 700);
427  lppllctl |= TWL6040_HPLLSEL;
429  lppllctl);
430  lppllctl &= ~TWL6040_LPLLENA;
432  lppllctl);
433  }
434  break;
435  default:
436  dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
437  ret = -EINVAL;
438  goto pll_out;
439  }
440 
441  twl6040->sysclk = freq_out;
442  twl6040->mclk = freq_in;
443  twl6040->pll = pll_id;
444 
445 pll_out:
446  mutex_unlock(&twl6040->mutex);
447  return ret;
448 }
450 
451 int twl6040_get_pll(struct twl6040 *twl6040)
452 {
453  if (twl6040->power_count)
454  return twl6040->pll;
455  else
456  return -ENODEV;
457 }
459 
460 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
461 {
462  return twl6040->sysclk;
463 }
465 
466 /* Get the combined status of the vibra control register */
467 int twl6040_get_vibralr_status(struct twl6040 *twl6040)
468 {
469  u8 status;
470 
471  status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
472  status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
473 
474  return status;
475 }
477 
478 static struct resource twl6040_vibra_rsrc[] = {
479  {
480  .flags = IORESOURCE_IRQ,
481  },
482 };
483 
484 static struct resource twl6040_codec_rsrc[] = {
485  {
486  .flags = IORESOURCE_IRQ,
487  },
488 };
489 
490 static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
491 {
492  /* Register 0 is not readable */
493  if (!reg)
494  return false;
495  return true;
496 }
497 
498 static struct regmap_config twl6040_regmap_config = {
499  .reg_bits = 8,
500  .val_bits = 8,
501  .max_register = TWL6040_REG_STATUS, /* 0x2e */
502 
503  .readable_reg = twl6040_readable_reg,
504 };
505 
506 static int __devinit twl6040_probe(struct i2c_client *client,
507  const struct i2c_device_id *id)
508 {
509  struct twl6040_platform_data *pdata = client->dev.platform_data;
510  struct device_node *node = client->dev.of_node;
511  struct twl6040 *twl6040;
512  struct mfd_cell *cell = NULL;
513  int irq, ret, children = 0;
514 
515  if (!pdata && !node) {
516  dev_err(&client->dev, "Platform data is missing\n");
517  return -EINVAL;
518  }
519 
520  /* In order to operate correctly we need valid interrupt config */
521  if (!client->irq) {
522  dev_err(&client->dev, "Invalid IRQ configuration\n");
523  return -EINVAL;
524  }
525 
526  twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
527  GFP_KERNEL);
528  if (!twl6040) {
529  ret = -ENOMEM;
530  goto err;
531  }
532 
533  twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
534  if (IS_ERR(twl6040->regmap)) {
535  ret = PTR_ERR(twl6040->regmap);
536  goto err;
537  }
538 
539  i2c_set_clientdata(client, twl6040);
540 
541  twl6040->supplies[0].supply = "vio";
542  twl6040->supplies[1].supply = "v2v1";
544  twl6040->supplies);
545  if (ret != 0) {
546  dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
547  goto regulator_get_err;
548  }
549 
551  if (ret != 0) {
552  dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
553  goto power_err;
554  }
555 
556  twl6040->dev = &client->dev;
557  twl6040->irq = client->irq;
558 
559  mutex_init(&twl6040->mutex);
560  init_completion(&twl6040->ready);
561 
562  twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
563 
564  /* ERRATA: Automatic power-up is not possible in ES1.0 */
565  if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) {
566  if (pdata)
567  twl6040->audpwron = pdata->audpwron_gpio;
568  else
569  twl6040->audpwron = of_get_named_gpio(node,
570  "ti,audpwron-gpio", 0);
571  } else
572  twl6040->audpwron = -EINVAL;
573 
574  if (gpio_is_valid(twl6040->audpwron)) {
576  "audpwron");
577  if (ret)
578  goto gpio_err;
579  }
580 
581  /* codec interrupt */
582  ret = twl6040_irq_init(twl6040);
583  if (ret)
584  goto irq_init_err;
585 
587  NULL, twl6040_naudint_handler, IRQF_ONESHOT,
588  "twl6040_irq_ready", twl6040);
589  if (ret) {
590  dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
591  ret);
592  goto irq_err;
593  }
594 
595  /* dual-access registers controlled by I2C only */
597 
598  /*
599  * The main functionality of twl6040 to provide audio on OMAP4+ systems.
600  * We can add the ASoC codec child whenever this driver has been loaded.
601  * The ASoC codec can work without pdata, pass the platform_data only if
602  * it has been provided.
603  */
604  irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
605  cell = &twl6040->cells[children];
606  cell->name = "twl6040-codec";
607  twl6040_codec_rsrc[0].start = irq;
608  twl6040_codec_rsrc[0].end = irq;
609  cell->resources = twl6040_codec_rsrc;
610  cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
611  if (pdata && pdata->codec) {
612  cell->platform_data = pdata->codec;
613  cell->pdata_size = sizeof(*pdata->codec);
614  }
615  children++;
616 
617  if (twl6040_has_vibra(pdata, node)) {
618  irq = twl6040->irq_base + TWL6040_IRQ_VIB;
619 
620  cell = &twl6040->cells[children];
621  cell->name = "twl6040-vibra";
622  twl6040_vibra_rsrc[0].start = irq;
623  twl6040_vibra_rsrc[0].end = irq;
624  cell->resources = twl6040_vibra_rsrc;
625  cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
626 
627  if (pdata && pdata->vibra) {
628  cell->platform_data = pdata->vibra;
629  cell->pdata_size = sizeof(*pdata->vibra);
630  }
631  children++;
632  }
633 
634  /*
635  * Enable the GPO driver in the following cases:
636  * DT booted kernel or legacy boot with valid gpo platform_data
637  */
638  if (!pdata || (pdata && pdata->gpo)) {
639  cell = &twl6040->cells[children];
640  cell->name = "twl6040-gpo";
641 
642  if (pdata) {
643  cell->platform_data = pdata->gpo;
644  cell->pdata_size = sizeof(*pdata->gpo);
645  }
646  children++;
647  }
648 
649  ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
650  NULL, 0, NULL);
651  if (ret)
652  goto mfd_err;
653 
654  return 0;
655 
656 mfd_err:
657  free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
658 irq_err:
659  twl6040_irq_exit(twl6040);
660 irq_init_err:
661  if (gpio_is_valid(twl6040->audpwron))
662  gpio_free(twl6040->audpwron);
663 gpio_err:
665 power_err:
667 regulator_get_err:
668  i2c_set_clientdata(client, NULL);
669 err:
670  return ret;
671 }
672 
673 static int __devexit twl6040_remove(struct i2c_client *client)
674 {
675  struct twl6040 *twl6040 = i2c_get_clientdata(client);
676 
677  if (twl6040->power_count)
678  twl6040_power(twl6040, 0);
679 
680  if (gpio_is_valid(twl6040->audpwron))
681  gpio_free(twl6040->audpwron);
682 
683  free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
684  twl6040_irq_exit(twl6040);
685 
686  mfd_remove_devices(&client->dev);
687  i2c_set_clientdata(client, NULL);
688 
691 
692  return 0;
693 }
694 
695 static const struct i2c_device_id twl6040_i2c_id[] = {
696  { "twl6040", 0, },
697  { "twl6041", 0, },
698  { },
699 };
700 MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
701 
702 static struct i2c_driver twl6040_driver = {
703  .driver = {
704  .name = "twl6040",
705  .owner = THIS_MODULE,
706  },
707  .probe = twl6040_probe,
708  .remove = __devexit_p(twl6040_remove),
709  .id_table = twl6040_i2c_id,
710 };
711 
712 module_i2c_driver(twl6040_driver);
713 
714 MODULE_DESCRIPTION("TWL6040 MFD");
715 MODULE_AUTHOR("Misael Lopez Cruz <[email protected]>");
716 MODULE_AUTHOR("Jorge Eduardo Candelaria <[email protected]>");
717 MODULE_LICENSE("GPL");
718 MODULE_ALIAS("platform:twl6040");