Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
twl-core.c
Go to the documentation of this file.
1 /*
2  * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
3  * and audio CODEC devices
4  *
5  * Copyright (C) 2005-2006 Texas Instruments, Inc.
6  *
7  * Modifications to defer interrupt handling to a kernel thread:
8  * Copyright (C) 2006 MontaVista Software, Inc.
9  *
10  * Based on tlv320aic23.c:
11  * Copyright (c) by Kai Svahn <[email protected]>
12  *
13  * Code cleanup and modifications to IRQ handler.
14  * by syed khasim <[email protected]>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30 
31 #include <linux/init.h>
32 #include <linux/mutex.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/clk.h>
36 #include <linux/err.h>
37 #include <linux/device.h>
38 #include <linux/of.h>
39 #include <linux/of_irq.h>
40 #include <linux/of_platform.h>
41 #include <linux/irq.h>
42 #include <linux/irqdomain.h>
43 
45 
46 #include <linux/i2c.h>
47 #include <linux/i2c/twl.h>
48 
49 #include "twl-core.h"
50 
51 /*
52  * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
53  * Management and System Companion Device" chips originally designed for
54  * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
55  * often at around 3 Mbit/sec, including for interrupt handling.
56  *
57  * This driver core provides genirq support for the interrupts emitted,
58  * by the various modules, and exports register access primitives.
59  *
60  * FIXME this driver currently requires use of the first interrupt line
61  * (and associated registers).
62  */
63 
64 #define DRIVER_NAME "twl"
65 
66 /* Triton Core internal information (BEGIN) */
67 
68 /* Last - for index max*/
69 #define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
70 
71 #define TWL_NUM_SLAVES 4
72 
73 #define SUB_CHIP_ID0 0
74 #define SUB_CHIP_ID1 1
75 #define SUB_CHIP_ID2 2
76 #define SUB_CHIP_ID3 3
77 #define SUB_CHIP_ID_INVAL 0xff
78 
79 #define TWL_MODULE_LAST TWL4030_MODULE_LAST
80 
81 /* Base Address defns for twl4030_map[] */
82 
83 /* subchip/slave 0 - USB ID */
84 #define TWL4030_BASEADD_USB 0x0000
85 
86 /* subchip/slave 1 - AUD ID */
87 #define TWL4030_BASEADD_AUDIO_VOICE 0x0000
88 #define TWL4030_BASEADD_GPIO 0x0098
89 #define TWL4030_BASEADD_INTBR 0x0085
90 #define TWL4030_BASEADD_PIH 0x0080
91 #define TWL4030_BASEADD_TEST 0x004C
92 
93 /* subchip/slave 2 - AUX ID */
94 #define TWL4030_BASEADD_INTERRUPTS 0x00B9
95 #define TWL4030_BASEADD_LED 0x00EE
96 #define TWL4030_BASEADD_MADC 0x0000
97 #define TWL4030_BASEADD_MAIN_CHARGE 0x0074
98 #define TWL4030_BASEADD_PRECHARGE 0x00AA
99 #define TWL4030_BASEADD_PWM0 0x00F8
100 #define TWL4030_BASEADD_PWM1 0x00FB
101 #define TWL4030_BASEADD_PWMA 0x00EF
102 #define TWL4030_BASEADD_PWMB 0x00F1
103 #define TWL4030_BASEADD_KEYPAD 0x00D2
104 
105 #define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
106 #define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
107  one */
108 
109 /* subchip/slave 3 - POWER ID */
110 #define TWL4030_BASEADD_BACKUP 0x0014
111 #define TWL4030_BASEADD_INT 0x002E
112 #define TWL4030_BASEADD_PM_MASTER 0x0036
113 #define TWL4030_BASEADD_PM_RECEIVER 0x005B
114 #define TWL4030_BASEADD_RTC 0x001C
115 #define TWL4030_BASEADD_SECURED_REG 0x0000
116 
117 /* Triton Core internal information (END) */
118 
119 
120 /* subchip/slave 0 0x48 - POWER */
121 #define TWL6030_BASEADD_RTC 0x0000
122 #define TWL6030_BASEADD_MEM 0x0017
123 #define TWL6030_BASEADD_PM_MASTER 0x001F
124 #define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
125 #define TWL6030_BASEADD_PM_MISC 0x00E2
126 #define TWL6030_BASEADD_PM_PUPD 0x00F0
127 
128 /* subchip/slave 1 0x49 - FEATURE */
129 #define TWL6030_BASEADD_USB 0x0000
130 #define TWL6030_BASEADD_GPADC_CTRL 0x002E
131 #define TWL6030_BASEADD_AUX 0x0090
132 #define TWL6030_BASEADD_PWM 0x00BA
133 #define TWL6030_BASEADD_GASGAUGE 0x00C0
134 #define TWL6030_BASEADD_PIH 0x00D0
135 #define TWL6030_BASEADD_CHARGER 0x00E0
136 #define TWL6025_BASEADD_CHARGER 0x00DA
137 
138 /* subchip/slave 2 0x4A - DFT */
139 #define TWL6030_BASEADD_DIEID 0x00C0
140 
141 /* subchip/slave 3 0x4B - AUDIO */
142 #define TWL6030_BASEADD_AUDIO 0x0000
143 #define TWL6030_BASEADD_RSV 0x0000
144 #define TWL6030_BASEADD_ZERO 0x0000
145 
146 /* Few power values */
147 #define R_CFG_BOOT 0x05
148 
149 /* some fields in R_CFG_BOOT */
150 #define HFCLK_FREQ_19p2_MHZ (1 << 0)
151 #define HFCLK_FREQ_26_MHZ (2 << 0)
152 #define HFCLK_FREQ_38p4_MHZ (3 << 0)
153 #define HIGH_PERF_SQ (1 << 3)
154 #define CK32K_LOWPWR_EN (1 << 7)
155 
156 /*----------------------------------------------------------------------*/
157 
158 /* is driver active, bound to a chip? */
159 static bool inuse;
160 
161 /* TWL IDCODE Register value */
162 static u32 twl_idcode;
163 
164 static unsigned int twl_id;
165 unsigned int twl_rev(void)
166 {
167  return twl_id;
168 }
170 
171 /* Structure for each TWL4030/TWL6030 Slave */
172 struct twl_client {
174  u8 address;
175 
176  /* max numb of i2c_msg required is for read =2 */
177  struct i2c_msg xfer_msg[2];
178 
179  /* To lock access to xfer_msg */
180  struct mutex xfer_lock;
181 };
182 
183 static struct twl_client twl_modules[TWL_NUM_SLAVES];
184 
185 /* mapping the module id to slave id and base address */
186 struct twl_mapping {
187  unsigned char sid; /* Slave ID */
188  unsigned char base; /* base address */
189 };
190 static struct twl_mapping *twl_map;
191 
192 static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
193  /*
194  * NOTE: don't change this table without updating the
195  * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
196  * so they continue to match the order in this table.
197  */
198 
199  { 0, TWL4030_BASEADD_USB },
200 
202  { 1, TWL4030_BASEADD_GPIO },
203  { 1, TWL4030_BASEADD_INTBR },
204  { 1, TWL4030_BASEADD_PIH },
205  { 1, TWL4030_BASEADD_TEST },
206 
207  { 2, TWL4030_BASEADD_KEYPAD },
208  { 2, TWL4030_BASEADD_MADC },
210  { 2, TWL4030_BASEADD_LED },
213  { 2, TWL4030_BASEADD_PWM0 },
214  { 2, TWL4030_BASEADD_PWM1 },
215  { 2, TWL4030_BASEADD_PWMA },
216  { 2, TWL4030_BASEADD_PWMB },
219 
220  { 3, TWL4030_BASEADD_BACKUP },
221  { 3, TWL4030_BASEADD_INT },
224  { 3, TWL4030_BASEADD_RTC },
226 };
227 
228 static struct twl_mapping twl6030_map[] = {
229  /*
230  * NOTE: don't change this table without updating the
231  * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
232  * so they continue to match the order in this table.
233  */
239 
245 
251 
259 
263 };
264 
265 /*----------------------------------------------------------------------*/
266 
267 /* Exported Functions */
268 
281 int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
282 {
283  int ret;
284  int sid;
285  struct twl_client *twl;
286  struct i2c_msg *msg;
287 
288  if (unlikely(mod_no > TWL_MODULE_LAST)) {
289  pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
290  return -EPERM;
291  }
292  if (unlikely(!inuse)) {
293  pr_err("%s: not initialized\n", DRIVER_NAME);
294  return -EPERM;
295  }
296  sid = twl_map[mod_no].sid;
297  if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
298  pr_err("%s: module %d is not part of the pmic\n",
299  DRIVER_NAME, mod_no);
300  return -EINVAL;
301  }
302  twl = &twl_modules[sid];
303 
304  mutex_lock(&twl->xfer_lock);
305  /*
306  * [MSG1]: fill the register address data
307  * fill the data Tx buffer
308  */
309  msg = &twl->xfer_msg[0];
310  msg->addr = twl->address;
311  msg->len = num_bytes + 1;
312  msg->flags = 0;
313  msg->buf = value;
314  /* over write the first byte of buffer with the register address */
315  *value = twl_map[mod_no].base + reg;
316  ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
317  mutex_unlock(&twl->xfer_lock);
318 
319  /* i2c_transfer returns number of messages transferred */
320  if (ret != 1) {
321  pr_err("%s: i2c_write failed to transfer all messages\n",
322  DRIVER_NAME);
323  if (ret < 0)
324  return ret;
325  else
326  return -EIO;
327  } else {
328  return 0;
329  }
330 }
332 
342 int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
343 {
344  int ret;
345  u8 val;
346  int sid;
347  struct twl_client *twl;
348  struct i2c_msg *msg;
349 
350  if (unlikely(mod_no > TWL_MODULE_LAST)) {
351  pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
352  return -EPERM;
353  }
354  if (unlikely(!inuse)) {
355  pr_err("%s: not initialized\n", DRIVER_NAME);
356  return -EPERM;
357  }
358  sid = twl_map[mod_no].sid;
359  if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
360  pr_err("%s: module %d is not part of the pmic\n",
361  DRIVER_NAME, mod_no);
362  return -EINVAL;
363  }
364  twl = &twl_modules[sid];
365 
366  mutex_lock(&twl->xfer_lock);
367  /* [MSG1] fill the register address data */
368  msg = &twl->xfer_msg[0];
369  msg->addr = twl->address;
370  msg->len = 1;
371  msg->flags = 0; /* Read the register value */
372  val = twl_map[mod_no].base + reg;
373  msg->buf = &val;
374  /* [MSG2] fill the data rx buffer */
375  msg = &twl->xfer_msg[1];
376  msg->addr = twl->address;
377  msg->flags = I2C_M_RD; /* Read the register value */
378  msg->len = num_bytes; /* only n bytes */
379  msg->buf = value;
380  ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
381  mutex_unlock(&twl->xfer_lock);
382 
383  /* i2c_transfer returns number of messages transferred */
384  if (ret != 2) {
385  pr_err("%s: i2c_read failed to transfer all messages\n",
386  DRIVER_NAME);
387  if (ret < 0)
388  return ret;
389  else
390  return -EIO;
391  } else {
392  return 0;
393  }
394 }
396 
405 int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
406 {
407 
408  /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
409  u8 temp_buffer[2] = { 0 };
410  /* offset 1 contains the data */
411  temp_buffer[1] = value;
412  return twl_i2c_write(mod_no, temp_buffer, reg, 1);
413 }
415 
424 int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
425 {
426  return twl_i2c_read(mod_no, value, reg, 1);
427 }
429 
430 /*----------------------------------------------------------------------*/
431 
437 static int twl_read_idcode_register(void)
438 {
439  int err;
440 
443  if (err) {
444  pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
445  goto fail;
446  }
447 
448  err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
449  REG_IDCODE_7_0, 4);
450  if (err) {
451  pr_err("TWL4030: unable to read IDCODE -%d\n", err);
452  goto fail;
453  }
454 
456  if (err)
457  pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
458 fail:
459  return err;
460 }
461 
467 int twl_get_type(void)
468 {
469  return TWL_SIL_TYPE(twl_idcode);
470 }
472 
478 int twl_get_version(void)
479 {
480  return TWL_SIL_REV(twl_idcode);
481 }
483 
489 int twl_get_hfclk_rate(void)
490 {
491  u8 ctrl;
492  int rate;
493 
495 
496  switch (ctrl & 0x3) {
497  case HFCLK_FREQ_19p2_MHZ:
498  rate = 19200000;
499  break;
500  case HFCLK_FREQ_26_MHZ:
501  rate = 26000000;
502  break;
503  case HFCLK_FREQ_38p4_MHZ:
504  rate = 38400000;
505  break;
506  default:
507  pr_err("TWL4030: HFCLK is not configured\n");
508  rate = -EINVAL;
509  break;
510  }
511 
512  return rate;
513 }
515 
516 static struct device *
517 add_numbered_child(unsigned chip, const char *name, int num,
518  void *pdata, unsigned pdata_len,
519  bool can_wakeup, int irq0, int irq1)
520 {
521  struct platform_device *pdev;
522  struct twl_client *twl = &twl_modules[chip];
523  int status;
524 
525  pdev = platform_device_alloc(name, num);
526  if (!pdev) {
527  dev_dbg(&twl->client->dev, "can't alloc dev\n");
528  status = -ENOMEM;
529  goto err;
530  }
531 
532  pdev->dev.parent = &twl->client->dev;
533 
534  if (pdata) {
535  status = platform_device_add_data(pdev, pdata, pdata_len);
536  if (status < 0) {
537  dev_dbg(&pdev->dev, "can't add platform_data\n");
538  goto err;
539  }
540  }
541 
542  if (irq0) {
543  struct resource r[2] = {
544  { .start = irq0, .flags = IORESOURCE_IRQ, },
545  { .start = irq1, .flags = IORESOURCE_IRQ, },
546  };
547 
548  status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
549  if (status < 0) {
550  dev_dbg(&pdev->dev, "can't add irqs\n");
551  goto err;
552  }
553  }
554 
555  status = platform_device_add(pdev);
556  if (status == 0)
557  device_init_wakeup(&pdev->dev, can_wakeup);
558 
559 err:
560  if (status < 0) {
561  platform_device_put(pdev);
562  dev_err(&twl->client->dev, "can't add %s dev\n", name);
563  return ERR_PTR(status);
564  }
565  return &pdev->dev;
566 }
567 
568 static inline struct device *add_child(unsigned chip, const char *name,
569  void *pdata, unsigned pdata_len,
570  bool can_wakeup, int irq0, int irq1)
571 {
572  return add_numbered_child(chip, name, -1, pdata, pdata_len,
573  can_wakeup, irq0, irq1);
574 }
575 
576 static struct device *
577 add_regulator_linked(int num, struct regulator_init_data *pdata,
578  struct regulator_consumer_supply *consumers,
579  unsigned num_consumers, unsigned long features)
580 {
581  unsigned sub_chip_id;
583 
584  /* regulator framework demands init_data ... */
585  if (!pdata)
586  return NULL;
587 
588  if (consumers) {
589  pdata->consumer_supplies = consumers;
590  pdata->num_consumer_supplies = num_consumers;
591  }
592 
593  if (pdata->driver_data) {
594  /* If we have existing drv_data, just add the flags */
596  tmp = pdata->driver_data;
597  tmp->features |= features;
598  } else {
599  /* add new driver data struct, used only during init */
600  drv_data.features = features;
601  drv_data.set_voltage = NULL;
602  drv_data.get_voltage = NULL;
603  drv_data.data = NULL;
604  pdata->driver_data = &drv_data;
605  }
606 
607  /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
608  sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
609  return add_numbered_child(sub_chip_id, "twl_reg", num,
610  pdata, sizeof(*pdata), false, 0, 0);
611 }
612 
613 static struct device *
614 add_regulator(int num, struct regulator_init_data *pdata,
615  unsigned long features)
616 {
617  return add_regulator_linked(num, pdata, NULL, 0, features);
618 }
619 
620 /*
621  * NOTE: We know the first 8 IRQs after pdata->base_irq are
622  * for the PIH, and the next are for the PWR_INT SIH, since
623  * that's how twl_init_irq() sets things up.
624  */
625 
626 static int
627 add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
628  unsigned long features)
629 {
630  struct device *child;
631  unsigned sub_chip_id;
632 
633  if (IS_ENABLED(CONFIG_GPIO_TWL4030) && pdata->gpio) {
634  child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
635  pdata->gpio, sizeof(*pdata->gpio),
636  false, irq_base + GPIO_INTR_OFFSET, 0);
637  if (IS_ERR(child))
638  return PTR_ERR(child);
639  }
640 
641  if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030) && pdata->keypad) {
642  child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
643  pdata->keypad, sizeof(*pdata->keypad),
644  true, irq_base + KEYPAD_INTR_OFFSET, 0);
645  if (IS_ERR(child))
646  return PTR_ERR(child);
647  }
648 
649  if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc) {
650  child = add_child(2, "twl4030_madc",
651  pdata->madc, sizeof(*pdata->madc),
652  true, irq_base + MADC_INTR_OFFSET, 0);
653  if (IS_ERR(child))
654  return PTR_ERR(child);
655  }
656 
657  if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030)) {
658  /*
659  * REVISIT platform_data here currently might expose the
660  * "msecure" line ... but for now we just expect board
661  * setup to tell the chip "it's always ok to SET_TIME".
662  * Eventually, Linux might become more aware of such
663  * HW security concerns, and "least privilege".
664  */
665  sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
666  child = add_child(sub_chip_id, "twl_rtc",
667  NULL, 0,
668  true, irq_base + RTC_INTR_OFFSET, 0);
669  if (IS_ERR(child))
670  return PTR_ERR(child);
671  }
672 
673  if (IS_ENABLED(CONFIG_PWM_TWL6030) && twl_class_is_6030()) {
674  child = add_child(SUB_CHIP_ID1, "twl6030-pwm", NULL, 0,
675  false, 0, 0);
676  if (IS_ERR(child))
677  return PTR_ERR(child);
678  }
679 
680  if (IS_ENABLED(CONFIG_TWL4030_USB) && pdata->usb &&
681  twl_class_is_4030()) {
682 
683  static struct regulator_consumer_supply usb1v5 = {
684  .supply = "usb1v5",
685  };
686  static struct regulator_consumer_supply usb1v8 = {
687  .supply = "usb1v8",
688  };
689  static struct regulator_consumer_supply usb3v1[] = {
690  { .supply = "usb3v1" },
691  { .supply = "bci3v1" },
692  };
693 
694  /* First add the regulators so that they can be used by transceiver */
695  if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
696  /* this is a template that gets copied */
697  struct regulator_init_data usb_fixed = {
698  .constraints.valid_modes_mask =
701  .constraints.valid_ops_mask =
704  };
705 
706  child = add_regulator_linked(TWL4030_REG_VUSB1V5,
707  &usb_fixed, &usb1v5, 1,
708  features);
709  if (IS_ERR(child))
710  return PTR_ERR(child);
711 
712  child = add_regulator_linked(TWL4030_REG_VUSB1V8,
713  &usb_fixed, &usb1v8, 1,
714  features);
715  if (IS_ERR(child))
716  return PTR_ERR(child);
717 
718  child = add_regulator_linked(TWL4030_REG_VUSB3V1,
719  &usb_fixed, usb3v1, 2,
720  features);
721  if (IS_ERR(child))
722  return PTR_ERR(child);
723 
724  }
725 
726  child = add_child(0, "twl4030_usb",
727  pdata->usb, sizeof(*pdata->usb),
728  true,
729  /* irq0 = USB_PRES, irq1 = USB */
730  irq_base + USB_PRES_INTR_OFFSET,
731  irq_base + USB_INTR_OFFSET);
732 
733  if (IS_ERR(child))
734  return PTR_ERR(child);
735 
736  /* we need to connect regulators to this transceiver */
737  if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child) {
738  usb1v5.dev_name = dev_name(child);
739  usb1v8.dev_name = dev_name(child);
740  usb3v1[0].dev_name = dev_name(child);
741  }
742  }
743  if (IS_ENABLED(CONFIG_TWL6030_USB) && pdata->usb &&
744  twl_class_is_6030()) {
745 
746  static struct regulator_consumer_supply usb3v3;
747  int regulator;
748 
749  if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
750  /* this is a template that gets copied */
751  struct regulator_init_data usb_fixed = {
752  .constraints.valid_modes_mask =
755  .constraints.valid_ops_mask =
758  };
759 
760  if (features & TWL6025_SUBCLASS) {
761  usb3v3.supply = "ldousb";
762  regulator = TWL6025_REG_LDOUSB;
763  } else {
764  usb3v3.supply = "vusb";
765  regulator = TWL6030_REG_VUSB;
766  }
767  child = add_regulator_linked(regulator, &usb_fixed,
768  &usb3v3, 1,
769  features);
770  if (IS_ERR(child))
771  return PTR_ERR(child);
772  }
773 
774  pdata->usb->features = features;
775 
776  child = add_child(0, "twl6030_usb",
777  pdata->usb, sizeof(*pdata->usb),
778  true,
779  /* irq1 = VBUS_PRES, irq0 = USB ID */
780  irq_base + USBOTG_INTR_OFFSET,
781  irq_base + USB_PRES_INTR_OFFSET);
782 
783  if (IS_ERR(child))
784  return PTR_ERR(child);
785  /* we need to connect regulators to this transceiver */
786  if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child)
787  usb3v3.dev_name = dev_name(child);
788  } else if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) &&
789  twl_class_is_6030()) {
790  if (features & TWL6025_SUBCLASS)
791  child = add_regulator(TWL6025_REG_LDOUSB,
792  pdata->ldousb, features);
793  else
794  child = add_regulator(TWL6030_REG_VUSB,
795  pdata->vusb, features);
796 
797  if (IS_ERR(child))
798  return PTR_ERR(child);
799  }
800 
801  if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG) && twl_class_is_4030()) {
802  child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
803  if (IS_ERR(child))
804  return PTR_ERR(child);
805  }
806 
807  if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON) && twl_class_is_4030()) {
808  child = add_child(1, "twl4030_pwrbutton",
809  NULL, 0, true, irq_base + 8 + 0, 0);
810  if (IS_ERR(child))
811  return PTR_ERR(child);
812  }
813 
814  if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO) && pdata->audio &&
815  twl_class_is_4030()) {
816  sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
817  child = add_child(sub_chip_id, "twl4030-audio",
818  pdata->audio, sizeof(*pdata->audio),
819  false, 0, 0);
820  if (IS_ERR(child))
821  return PTR_ERR(child);
822  }
823 
824  /* twl4030 regulators */
825  if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_4030()) {
826  child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
827  features);
828  if (IS_ERR(child))
829  return PTR_ERR(child);
830 
831  child = add_regulator(TWL4030_REG_VIO, pdata->vio,
832  features);
833  if (IS_ERR(child))
834  return PTR_ERR(child);
835 
836  child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
837  features);
838  if (IS_ERR(child))
839  return PTR_ERR(child);
840 
841  child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
842  features);
843  if (IS_ERR(child))
844  return PTR_ERR(child);
845 
846  child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
847  features);
848  if (IS_ERR(child))
849  return PTR_ERR(child);
850 
851  child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
852  features);
853  if (IS_ERR(child))
854  return PTR_ERR(child);
855 
856  child = add_regulator((features & TWL4030_VAUX2)
859  pdata->vaux2, features);
860  if (IS_ERR(child))
861  return PTR_ERR(child);
862 
863  child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
864  features);
865  if (IS_ERR(child))
866  return PTR_ERR(child);
867 
868  child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
869  features);
870  if (IS_ERR(child))
871  return PTR_ERR(child);
872 
873  child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
874  features);
875  if (IS_ERR(child))
876  return PTR_ERR(child);
877  }
878 
879  /* maybe add LDOs that are omitted on cost-reduced parts */
880  if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && !(features & TPS_SUBSET)
881  && twl_class_is_4030()) {
882  child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
883  features);
884  if (IS_ERR(child))
885  return PTR_ERR(child);
886 
887  child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
888  features);
889  if (IS_ERR(child))
890  return PTR_ERR(child);
891 
892  child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
893  features);
894  if (IS_ERR(child))
895  return PTR_ERR(child);
896 
897  child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
898  features);
899  if (IS_ERR(child))
900  return PTR_ERR(child);
901 
902  child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
903  features);
904  if (IS_ERR(child))
905  return PTR_ERR(child);
906 
907  child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
908  features);
909  if (IS_ERR(child))
910  return PTR_ERR(child);
911  }
912 
913  /* twl6030 regulators */
914  if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
915  !(features & TWL6025_SUBCLASS)) {
916  child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1,
917  features);
918  if (IS_ERR(child))
919  return PTR_ERR(child);
920 
921  child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2,
922  features);
923  if (IS_ERR(child))
924  return PTR_ERR(child);
925 
926  child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3,
927  features);
928  if (IS_ERR(child))
929  return PTR_ERR(child);
930 
931  child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8,
932  features);
933  if (IS_ERR(child))
934  return PTR_ERR(child);
935 
936  child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1,
937  features);
938  if (IS_ERR(child))
939  return PTR_ERR(child);
940 
941  child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
942  features);
943  if (IS_ERR(child))
944  return PTR_ERR(child);
945 
946  child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
947  features);
948  if (IS_ERR(child))
949  return PTR_ERR(child);
950 
951  child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
952  features);
953  if (IS_ERR(child))
954  return PTR_ERR(child);
955 
956  child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
957  features);
958  if (IS_ERR(child))
959  return PTR_ERR(child);
960 
961  child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
962  features);
963  if (IS_ERR(child))
964  return PTR_ERR(child);
965 
966  child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
967  features);
968  if (IS_ERR(child))
969  return PTR_ERR(child);
970 
971  child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
972  features);
973  if (IS_ERR(child))
974  return PTR_ERR(child);
975 
976  child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
977  features);
978  if (IS_ERR(child))
979  return PTR_ERR(child);
980 
981  child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
982  features);
983  if (IS_ERR(child))
984  return PTR_ERR(child);
985  }
986 
987  /* 6030 and 6025 share this regulator */
988  if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030()) {
989  child = add_regulator(TWL6030_REG_VANA, pdata->vana,
990  features);
991  if (IS_ERR(child))
992  return PTR_ERR(child);
993  }
994 
995  /* twl6025 regulators */
996  if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
997  (features & TWL6025_SUBCLASS)) {
998  child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
999  features);
1000  if (IS_ERR(child))
1001  return PTR_ERR(child);
1002 
1003  child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
1004  features);
1005  if (IS_ERR(child))
1006  return PTR_ERR(child);
1007 
1008  child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
1009  features);
1010  if (IS_ERR(child))
1011  return PTR_ERR(child);
1012 
1013  child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
1014  features);
1015  if (IS_ERR(child))
1016  return PTR_ERR(child);
1017 
1018  child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
1019  features);
1020  if (IS_ERR(child))
1021  return PTR_ERR(child);
1022 
1023  child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
1024  features);
1025  if (IS_ERR(child))
1026  return PTR_ERR(child);
1027 
1028  child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
1029  features);
1030  if (IS_ERR(child))
1031  return PTR_ERR(child);
1032 
1033  child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
1034  features);
1035  if (IS_ERR(child))
1036  return PTR_ERR(child);
1037 
1038  child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
1039  features);
1040  if (IS_ERR(child))
1041  return PTR_ERR(child);
1042 
1043  child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
1044  features);
1045  if (IS_ERR(child))
1046  return PTR_ERR(child);
1047 
1048  child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
1049  features);
1050  if (IS_ERR(child))
1051  return PTR_ERR(child);
1052 
1053  }
1054 
1055  if (IS_ENABLED(CONFIG_CHARGER_TWL4030) && pdata->bci &&
1056  !(features & (TPS_SUBSET | TWL5031))) {
1057  child = add_child(3, "twl4030_bci",
1058  pdata->bci, sizeof(*pdata->bci), false,
1059  /* irq0 = CHG_PRES, irq1 = BCI */
1060  irq_base + BCI_PRES_INTR_OFFSET,
1061  irq_base + BCI_INTR_OFFSET);
1062  if (IS_ERR(child))
1063  return PTR_ERR(child);
1064  }
1065 
1066  return 0;
1067 }
1068 
1069 /*----------------------------------------------------------------------*/
1070 
1071 /*
1072  * These three functions initialize the on-chip clock framework,
1073  * letting it generate the right frequencies for USB, MADC, and
1074  * other purposes.
1075  */
1076 static inline int __init protect_pm_master(void)
1077 {
1078  int e = 0;
1079 
1082  return e;
1083 }
1084 
1085 static inline int __init unprotect_pm_master(void)
1086 {
1087  int e = 0;
1088 
1095 
1096  return e;
1097 }
1098 
1099 static void clocks_init(struct device *dev,
1101 {
1102  int e = 0;
1103  struct clk *osc;
1104  u32 rate;
1105  u8 ctrl = HFCLK_FREQ_26_MHZ;
1106 
1107  osc = clk_get(dev, "fck");
1108  if (IS_ERR(osc)) {
1109  printk(KERN_WARNING "Skipping twl internal clock init and "
1110  "using bootloader value (unknown osc rate)\n");
1111  return;
1112  }
1113 
1114  rate = clk_get_rate(osc);
1115  clk_put(osc);
1116 
1117  switch (rate) {
1118  case 19200000:
1119  ctrl = HFCLK_FREQ_19p2_MHZ;
1120  break;
1121  case 26000000:
1122  ctrl = HFCLK_FREQ_26_MHZ;
1123  break;
1124  case 38400000:
1125  ctrl = HFCLK_FREQ_38p4_MHZ;
1126  break;
1127  }
1128 
1129  ctrl |= HIGH_PERF_SQ;
1130  if (clock && clock->ck32k_lowpwr_enable)
1131  ctrl |= CK32K_LOWPWR_EN;
1132 
1133  e |= unprotect_pm_master();
1134  /* effect->MADC+USB ck en */
1136  e |= protect_pm_master();
1137 
1138  if (e < 0)
1139  pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
1140 }
1141 
1142 /*----------------------------------------------------------------------*/
1143 
1144 
1145 static int twl_remove(struct i2c_client *client)
1146 {
1147  unsigned i, num_slaves;
1148  int status;
1149 
1150  if (twl_class_is_4030()) {
1151  status = twl4030_exit_irq();
1152  num_slaves = TWL_NUM_SLAVES;
1153  } else {
1154  status = twl6030_exit_irq();
1155  num_slaves = TWL_NUM_SLAVES - 1;
1156  }
1157 
1158  if (status < 0)
1159  return status;
1160 
1161  for (i = 0; i < num_slaves; i++) {
1162  struct twl_client *twl = &twl_modules[i];
1163 
1164  if (twl->client && twl->client != client)
1166  twl_modules[i].client = NULL;
1167  }
1168  inuse = false;
1169  return 0;
1170 }
1171 
1172 /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
1173 static int __devinit
1174 twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
1175 {
1176  struct twl4030_platform_data *pdata = client->dev.platform_data;
1177  struct device_node *node = client->dev.of_node;
1178  struct platform_device *pdev;
1179  int irq_base = 0;
1180  int status;
1181  unsigned i, num_slaves;
1182 
1183  pdev = platform_device_alloc(DRIVER_NAME, -1);
1184  if (!pdev) {
1185  dev_err(&client->dev, "can't alloc pdev\n");
1186  return -ENOMEM;
1187  }
1188 
1189  status = platform_device_add(pdev);
1190  if (status) {
1191  platform_device_put(pdev);
1192  return status;
1193  }
1194 
1195  if (node && !pdata) {
1196  /*
1197  * XXX: Temporary pdata until the information is correctly
1198  * retrieved by every TWL modules from DT.
1199  */
1200  pdata = devm_kzalloc(&client->dev,
1201  sizeof(struct twl4030_platform_data),
1202  GFP_KERNEL);
1203  if (!pdata) {
1204  status = -ENOMEM;
1205  goto free;
1206  }
1207  }
1208 
1209  if (!pdata) {
1210  dev_dbg(&client->dev, "no platform data?\n");
1211  status = -EINVAL;
1212  goto free;
1213  }
1214 
1215  platform_set_drvdata(pdev, pdata);
1216 
1217  if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1218  dev_dbg(&client->dev, "can't talk I2C?\n");
1219  status = -EIO;
1220  goto free;
1221  }
1222 
1223  if (inuse) {
1224  dev_dbg(&client->dev, "driver is already in use\n");
1225  status = -EBUSY;
1226  goto free;
1227  }
1228 
1229  if ((id->driver_data) & TWL6030_CLASS) {
1230  twl_id = TWL6030_CLASS_ID;
1231  twl_map = &twl6030_map[0];
1232  num_slaves = TWL_NUM_SLAVES - 1;
1233  } else {
1234  twl_id = TWL4030_CLASS_ID;
1235  twl_map = &twl4030_map[0];
1236  num_slaves = TWL_NUM_SLAVES;
1237  }
1238 
1239  for (i = 0; i < num_slaves; i++) {
1240  struct twl_client *twl = &twl_modules[i];
1241 
1242  twl->address = client->addr + i;
1243  if (i == 0) {
1244  twl->client = client;
1245  } else {
1246  twl->client = i2c_new_dummy(client->adapter,
1247  twl->address);
1248  if (!twl->client) {
1249  dev_err(&client->dev,
1250  "can't attach client %d\n", i);
1251  status = -ENOMEM;
1252  goto fail;
1253  }
1254  }
1255  mutex_init(&twl->xfer_lock);
1256  }
1257 
1258  inuse = true;
1259 
1260  /* setup clock framework */
1261  clocks_init(&pdev->dev, pdata->clock);
1262 
1263  /* read TWL IDCODE Register */
1264  if (twl_id == TWL4030_CLASS_ID) {
1265  status = twl_read_idcode_register();
1266  WARN(status < 0, "Error: reading twl_idcode register value\n");
1267  }
1268 
1269  /* load power event scripts */
1270  if (IS_ENABLED(CONFIG_TWL4030_POWER) && pdata->power)
1271  twl4030_power_init(pdata->power);
1272 
1273  /* Maybe init the T2 Interrupt subsystem */
1274  if (client->irq) {
1275  if (twl_class_is_4030()) {
1277  irq_base = twl4030_init_irq(&client->dev, client->irq);
1278  } else {
1279  irq_base = twl6030_init_irq(&client->dev, client->irq);
1280  }
1281 
1282  if (irq_base < 0) {
1283  status = irq_base;
1284  goto fail;
1285  }
1286  }
1287 
1288  /*
1289  * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1290  * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1291  * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1292  */
1293  if (twl_class_is_4030()) {
1294  u8 temp;
1295 
1297  temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
1298  I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
1300  }
1301 
1302  status = -ENODEV;
1303  if (node)
1304  status = of_platform_populate(node, NULL, NULL, &client->dev);
1305  if (status)
1306  status = add_children(pdata, irq_base, id->driver_data);
1307 
1308 fail:
1309  if (status < 0)
1310  twl_remove(client);
1311 free:
1312  if (status < 0)
1314 
1315  return status;
1316 }
1317 
1318 static const struct i2c_device_id twl_ids[] = {
1319  { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
1320  { "twl5030", 0 }, /* T2 updated */
1321  { "twl5031", TWL5031 }, /* TWL5030 updated */
1322  { "tps65950", 0 }, /* catalog version of twl5030 */
1323  { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
1324  { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
1325  { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
1326  and vibrator. Charger in USB module*/
1327  { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
1328  { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
1329  { /* end of list */ },
1330 };
1331 MODULE_DEVICE_TABLE(i2c, twl_ids);
1332 
1333 /* One Client Driver , 4 Clients */
1334 static struct i2c_driver twl_driver = {
1335  .driver.name = DRIVER_NAME,
1336  .id_table = twl_ids,
1337  .probe = twl_probe,
1338  .remove = twl_remove,
1339 };
1340 
1341 static int __init twl_init(void)
1342 {
1343  return i2c_add_driver(&twl_driver);
1344 }
1345 subsys_initcall(twl_init);
1346 
1347 static void __exit twl_exit(void)
1348 {
1349  i2c_del_driver(&twl_driver);
1350 }
1351 module_exit(twl_exit);
1352 
1353 MODULE_AUTHOR("Texas Instruments, Inc.");
1354 MODULE_DESCRIPTION("I2C Core interface for TWL");
1355 MODULE_LICENSE("GPL");