• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/drivers/mfd/
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 <kai.svahn@nokia.com>
12 *
13 * Code cleanup and modifications to IRQ handler.
14 * by syed khasim <x0khasim@ti.com>
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/platform_device.h>
34#include <linux/clk.h>
35#include <linux/err.h>
36
37#include <linux/regulator/machine.h>
38
39#include <linux/i2c.h>
40#include <linux/i2c/twl.h>
41
42#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
43#include <plat/cpu.h>
44#endif
45
46
47#define DRIVER_NAME			"twl"
48
49#if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
50#define twl_has_keypad()	true
51#else
52#define twl_has_keypad()	false
53#endif
54
55#if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
56#define twl_has_gpio()	true
57#else
58#define twl_has_gpio()	false
59#endif
60
61#if defined(CONFIG_REGULATOR_TWL4030) || defined(CONFIG_REGULATOR_TWL4030_MODULE)
62#define twl_has_regulator()	true
63#else
64#define twl_has_regulator()	false
65#endif
66
67#if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
68#define twl_has_madc()	true
69#else
70#define twl_has_madc()	false
71#endif
72
73#ifdef CONFIG_TWL4030_POWER
74#define twl_has_power()        true
75#else
76#define twl_has_power()        false
77#endif
78
79#if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
80#define twl_has_rtc()	true
81#else
82#define twl_has_rtc()	false
83#endif
84
85#if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
86#define twl_has_usb()	true
87#else
88#define twl_has_usb()	false
89#endif
90
91#if defined(CONFIG_TWL4030_WATCHDOG) || defined(CONFIG_TWL4030_WATCHDOG_MODULE)
92#define twl_has_watchdog()        true
93#else
94#define twl_has_watchdog()        false
95#endif
96
97#if defined(CONFIG_TWL4030_CODEC) || defined(CONFIG_TWL4030_CODEC_MODULE) || \
98	defined(CONFIG_SND_SOC_TWL6040) || defined(CONFIG_SND_SOC_TWL6040_MODULE)
99#define twl_has_codec()	true
100#else
101#define twl_has_codec()	false
102#endif
103
104/* Triton Core internal information (BEGIN) */
105
106/* Last - for index max*/
107#define TWL4030_MODULE_LAST		TWL4030_MODULE_SECURED_REG
108
109#define TWL_NUM_SLAVES		4
110
111#if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) || \
112	defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
113#define twl_has_pwrbutton()	true
114#else
115#define twl_has_pwrbutton()	false
116#endif
117
118#define SUB_CHIP_ID0 0
119#define SUB_CHIP_ID1 1
120#define SUB_CHIP_ID2 2
121#define SUB_CHIP_ID3 3
122
123#define TWL_MODULE_LAST TWL4030_MODULE_LAST
124
125/* Base Address defns for twl4030_map[] */
126
127/* subchip/slave 0 - USB ID */
128#define TWL4030_BASEADD_USB		0x0000
129
130/* subchip/slave 1 - AUD ID */
131#define TWL4030_BASEADD_AUDIO_VOICE	0x0000
132#define TWL4030_BASEADD_GPIO		0x0098
133#define TWL4030_BASEADD_INTBR		0x0085
134#define TWL4030_BASEADD_PIH		0x0080
135#define TWL4030_BASEADD_TEST		0x004C
136
137/* subchip/slave 2 - AUX ID */
138#define TWL4030_BASEADD_INTERRUPTS	0x00B9
139#define TWL4030_BASEADD_LED		0x00EE
140#define TWL4030_BASEADD_MADC		0x0000
141#define TWL4030_BASEADD_MAIN_CHARGE	0x0074
142#define TWL4030_BASEADD_PRECHARGE	0x00AA
143#define TWL4030_BASEADD_PWM0		0x00F8
144#define TWL4030_BASEADD_PWM1		0x00FB
145#define TWL4030_BASEADD_PWMA		0x00EF
146#define TWL4030_BASEADD_PWMB		0x00F1
147#define TWL4030_BASEADD_KEYPAD		0x00D2
148
149#define TWL5031_BASEADD_ACCESSORY	0x0074 /* Replaces Main Charge */
150#define TWL5031_BASEADD_INTERRUPTS	0x00B9 /* Different than TWL4030's
151						  one */
152
153/* subchip/slave 3 - POWER ID */
154#define TWL4030_BASEADD_BACKUP		0x0014
155#define TWL4030_BASEADD_INT		0x002E
156#define TWL4030_BASEADD_PM_MASTER	0x0036
157#define TWL4030_BASEADD_PM_RECEIVER	0x005B
158#define TWL4030_BASEADD_RTC		0x001C
159#define TWL4030_BASEADD_SECURED_REG	0x0000
160
161/* Triton Core internal information (END) */
162
163
164/* subchip/slave 0 0x48 - POWER */
165#define TWL6030_BASEADD_RTC		0x0000
166#define TWL6030_BASEADD_MEM		0x0017
167#define TWL6030_BASEADD_PM_MASTER	0x001F
168#define TWL6030_BASEADD_PM_SLAVE_MISC	0x0030 /* PM_RECEIVER */
169#define TWL6030_BASEADD_PM_MISC		0x00E2
170#define TWL6030_BASEADD_PM_PUPD		0x00F0
171
172/* subchip/slave 1 0x49 - FEATURE */
173#define TWL6030_BASEADD_USB		0x0000
174#define TWL6030_BASEADD_GPADC_CTRL	0x002E
175#define TWL6030_BASEADD_AUX		0x0090
176#define TWL6030_BASEADD_PWM		0x00BA
177#define TWL6030_BASEADD_GASGAUGE	0x00C0
178#define TWL6030_BASEADD_PIH		0x00D0
179#define TWL6030_BASEADD_CHARGER		0x00E0
180
181/* subchip/slave 2 0x4A - DFT */
182#define TWL6030_BASEADD_DIEID		0x00C0
183
184/* subchip/slave 3 0x4B - AUDIO */
185#define TWL6030_BASEADD_AUDIO		0x0000
186#define TWL6030_BASEADD_RSV		0x0000
187#define TWL6030_BASEADD_ZERO		0x0000
188
189/* Few power values */
190#define R_CFG_BOOT			0x05
191#define R_PROTECT_KEY			0x0E
192
193/* access control values for R_PROTECT_KEY */
194#define KEY_UNLOCK1			0xce
195#define KEY_UNLOCK2			0xec
196#define KEY_LOCK			0x00
197
198/* some fields in R_CFG_BOOT */
199#define HFCLK_FREQ_19p2_MHZ		(1 << 0)
200#define HFCLK_FREQ_26_MHZ		(2 << 0)
201#define HFCLK_FREQ_38p4_MHZ		(3 << 0)
202#define HIGH_PERF_SQ			(1 << 3)
203#define CK32K_LOWPWR_EN			(1 << 7)
204
205
206/* chip-specific feature flags, for i2c_device_id.driver_data */
207#define TWL4030_VAUX2		BIT(0)	/* pre-5030 voltage ranges */
208#define TPS_SUBSET		BIT(1)	/* tps659[23]0 have fewer LDOs */
209#define TWL5031			BIT(2)  /* twl5031 has different registers */
210#define TWL6030_CLASS		BIT(3)	/* TWL6030 class */
211
212/*----------------------------------------------------------------------*/
213
214/* is driver active, bound to a chip? */
215static bool inuse;
216
217static unsigned int twl_id;
218unsigned int twl_rev(void)
219{
220	return twl_id;
221}
222EXPORT_SYMBOL(twl_rev);
223
224/* Structure for each TWL4030/TWL6030 Slave */
225struct twl_client {
226	struct i2c_client *client;
227	u8 address;
228
229	/* max numb of i2c_msg required is for read =2 */
230	struct i2c_msg xfer_msg[2];
231
232	/* To lock access to xfer_msg */
233	struct mutex xfer_lock;
234};
235
236static struct twl_client twl_modules[TWL_NUM_SLAVES];
237
238
239/* mapping the module id to slave id and base address */
240struct twl_mapping {
241	unsigned char sid;	/* Slave ID */
242	unsigned char base;	/* base address */
243};
244struct twl_mapping *twl_map;
245
246static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
247	/*
248	 * NOTE:  don't change this table without updating the
249	 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
250	 * so they continue to match the order in this table.
251	 */
252
253	{ 0, TWL4030_BASEADD_USB },
254
255	{ 1, TWL4030_BASEADD_AUDIO_VOICE },
256	{ 1, TWL4030_BASEADD_GPIO },
257	{ 1, TWL4030_BASEADD_INTBR },
258	{ 1, TWL4030_BASEADD_PIH },
259	{ 1, TWL4030_BASEADD_TEST },
260
261	{ 2, TWL4030_BASEADD_KEYPAD },
262	{ 2, TWL4030_BASEADD_MADC },
263	{ 2, TWL4030_BASEADD_INTERRUPTS },
264	{ 2, TWL4030_BASEADD_LED },
265	{ 2, TWL4030_BASEADD_MAIN_CHARGE },
266	{ 2, TWL4030_BASEADD_PRECHARGE },
267	{ 2, TWL4030_BASEADD_PWM0 },
268	{ 2, TWL4030_BASEADD_PWM1 },
269	{ 2, TWL4030_BASEADD_PWMA },
270	{ 2, TWL4030_BASEADD_PWMB },
271	{ 2, TWL5031_BASEADD_ACCESSORY },
272	{ 2, TWL5031_BASEADD_INTERRUPTS },
273
274	{ 3, TWL4030_BASEADD_BACKUP },
275	{ 3, TWL4030_BASEADD_INT },
276	{ 3, TWL4030_BASEADD_PM_MASTER },
277	{ 3, TWL4030_BASEADD_PM_RECEIVER },
278	{ 3, TWL4030_BASEADD_RTC },
279	{ 3, TWL4030_BASEADD_SECURED_REG },
280};
281
282static struct twl_mapping twl6030_map[] = {
283	/*
284	 * NOTE:  don't change this table without updating the
285	 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
286	 * so they continue to match the order in this table.
287	 */
288	{ SUB_CHIP_ID1, TWL6030_BASEADD_USB },
289	{ SUB_CHIP_ID3, TWL6030_BASEADD_AUDIO },
290	{ SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
291	{ SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
292	{ SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
293
294	{ SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
295	{ SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
296	{ SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
297	{ SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
298	{ SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
299
300	{ SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
301	{ SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
302	{ SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
303	{ SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
304	{ SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
305
306	{ SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
307	{ SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
308	{ SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
309	{ SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
310	{ SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
311	{ SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
312	{ SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
313
314	{ SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
315	{ SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
316};
317
318/*----------------------------------------------------------------------*/
319
320/* Exported Functions */
321
322/**
323 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
324 * @mod_no: module number
325 * @value: an array of num_bytes+1 containing data to write
326 * @reg: register address (just offset will do)
327 * @num_bytes: number of bytes to transfer
328 *
329 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
330 * valid data starts at Offset 1.
331 *
332 * Returns the result of operation - 0 is success
333 */
334int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
335{
336	int ret;
337	int sid;
338	struct twl_client *twl;
339	struct i2c_msg *msg;
340
341	if (unlikely(mod_no > TWL_MODULE_LAST)) {
342		pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
343		return -EPERM;
344	}
345	sid = twl_map[mod_no].sid;
346	twl = &twl_modules[sid];
347
348	if (unlikely(!inuse)) {
349		pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
350		return -EPERM;
351	}
352	mutex_lock(&twl->xfer_lock);
353	/*
354	 * [MSG1]: fill the register address data
355	 * fill the data Tx buffer
356	 */
357	msg = &twl->xfer_msg[0];
358	msg->addr = twl->address;
359	msg->len = num_bytes + 1;
360	msg->flags = 0;
361	msg->buf = value;
362	/* over write the first byte of buffer with the register address */
363	*value = twl_map[mod_no].base + reg;
364	ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
365	mutex_unlock(&twl->xfer_lock);
366
367	/* i2c_transfer returns number of messages transferred */
368	if (ret != 1) {
369		pr_err("%s: i2c_write failed to transfer all messages\n",
370			DRIVER_NAME);
371		if (ret < 0)
372			return ret;
373		else
374			return -EIO;
375	} else {
376		return 0;
377	}
378}
379EXPORT_SYMBOL(twl_i2c_write);
380
381/**
382 * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
383 * @mod_no: module number
384 * @value: an array of num_bytes containing data to be read
385 * @reg: register address (just offset will do)
386 * @num_bytes: number of bytes to transfer
387 *
388 * Returns result of operation - num_bytes is success else failure.
389 */
390int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
391{
392	int ret;
393	u8 val;
394	int sid;
395	struct twl_client *twl;
396	struct i2c_msg *msg;
397
398	if (unlikely(mod_no > TWL_MODULE_LAST)) {
399		pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
400		return -EPERM;
401	}
402	sid = twl_map[mod_no].sid;
403	twl = &twl_modules[sid];
404
405	if (unlikely(!inuse)) {
406		pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
407		return -EPERM;
408	}
409	mutex_lock(&twl->xfer_lock);
410	/* [MSG1] fill the register address data */
411	msg = &twl->xfer_msg[0];
412	msg->addr = twl->address;
413	msg->len = 1;
414	msg->flags = 0;	/* Read the register value */
415	val = twl_map[mod_no].base + reg;
416	msg->buf = &val;
417	/* [MSG2] fill the data rx buffer */
418	msg = &twl->xfer_msg[1];
419	msg->addr = twl->address;
420	msg->flags = I2C_M_RD;	/* Read the register value */
421	msg->len = num_bytes;	/* only n bytes */
422	msg->buf = value;
423	ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
424	mutex_unlock(&twl->xfer_lock);
425
426	/* i2c_transfer returns number of messages transferred */
427	if (ret != 2) {
428		pr_err("%s: i2c_read failed to transfer all messages\n",
429			DRIVER_NAME);
430		if (ret < 0)
431			return ret;
432		else
433			return -EIO;
434	} else {
435		return 0;
436	}
437}
438EXPORT_SYMBOL(twl_i2c_read);
439
440/**
441 * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
442 * @mod_no: module number
443 * @value: the value to be written 8 bit
444 * @reg: register address (just offset will do)
445 *
446 * Returns result of operation - 0 is success
447 */
448int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
449{
450
451	/* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
452	u8 temp_buffer[2] = { 0 };
453	/* offset 1 contains the data */
454	temp_buffer[1] = value;
455	return twl_i2c_write(mod_no, temp_buffer, reg, 1);
456}
457EXPORT_SYMBOL(twl_i2c_write_u8);
458
459/**
460 * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
461 * @mod_no: module number
462 * @value: the value read 8 bit
463 * @reg: register address (just offset will do)
464 *
465 * Returns result of operation - 0 is success
466 */
467int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
468{
469	return twl_i2c_read(mod_no, value, reg, 1);
470}
471EXPORT_SYMBOL(twl_i2c_read_u8);
472
473/*----------------------------------------------------------------------*/
474
475static struct device *
476add_numbered_child(unsigned chip, const char *name, int num,
477		void *pdata, unsigned pdata_len,
478		bool can_wakeup, int irq0, int irq1)
479{
480	struct platform_device	*pdev;
481	struct twl_client	*twl = &twl_modules[chip];
482	int			status;
483
484	pdev = platform_device_alloc(name, num);
485	if (!pdev) {
486		dev_dbg(&twl->client->dev, "can't alloc dev\n");
487		status = -ENOMEM;
488		goto err;
489	}
490
491	device_init_wakeup(&pdev->dev, can_wakeup);
492	pdev->dev.parent = &twl->client->dev;
493
494	if (pdata) {
495		status = platform_device_add_data(pdev, pdata, pdata_len);
496		if (status < 0) {
497			dev_dbg(&pdev->dev, "can't add platform_data\n");
498			goto err;
499		}
500	}
501
502	if (irq0) {
503		struct resource r[2] = {
504			{ .start = irq0, .flags = IORESOURCE_IRQ, },
505			{ .start = irq1, .flags = IORESOURCE_IRQ, },
506		};
507
508		status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
509		if (status < 0) {
510			dev_dbg(&pdev->dev, "can't add irqs\n");
511			goto err;
512		}
513	}
514
515	status = platform_device_add(pdev);
516
517err:
518	if (status < 0) {
519		platform_device_put(pdev);
520		dev_err(&twl->client->dev, "can't add %s dev\n", name);
521		return ERR_PTR(status);
522	}
523	return &pdev->dev;
524}
525
526static inline struct device *add_child(unsigned chip, const char *name,
527		void *pdata, unsigned pdata_len,
528		bool can_wakeup, int irq0, int irq1)
529{
530	return add_numbered_child(chip, name, -1, pdata, pdata_len,
531		can_wakeup, irq0, irq1);
532}
533
534static struct device *
535add_regulator_linked(int num, struct regulator_init_data *pdata,
536		struct regulator_consumer_supply *consumers,
537		unsigned num_consumers)
538{
539	unsigned sub_chip_id;
540	/* regulator framework demands init_data ... */
541	if (!pdata)
542		return NULL;
543
544	if (consumers) {
545		pdata->consumer_supplies = consumers;
546		pdata->num_consumer_supplies = num_consumers;
547	}
548
549	/* NOTE:  we currently ignore regulator IRQs, e.g. for short circuits */
550	sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
551	return add_numbered_child(sub_chip_id, "twl_reg", num,
552		pdata, sizeof(*pdata), false, 0, 0);
553}
554
555static struct device *
556add_regulator(int num, struct regulator_init_data *pdata)
557{
558	return add_regulator_linked(num, pdata, NULL, 0);
559}
560
561/*
562 * NOTE:  We know the first 8 IRQs after pdata->base_irq are
563 * for the PIH, and the next are for the PWR_INT SIH, since
564 * that's how twl_init_irq() sets things up.
565 */
566
567static int
568add_children(struct twl4030_platform_data *pdata, unsigned long features)
569{
570	struct device	*child;
571	unsigned sub_chip_id;
572
573	if (twl_has_gpio() && pdata->gpio) {
574		child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
575				pdata->gpio, sizeof(*pdata->gpio),
576				false, pdata->irq_base + GPIO_INTR_OFFSET, 0);
577		if (IS_ERR(child))
578			return PTR_ERR(child);
579	}
580
581	if (twl_has_keypad() && pdata->keypad) {
582		child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
583				pdata->keypad, sizeof(*pdata->keypad),
584				true, pdata->irq_base + KEYPAD_INTR_OFFSET, 0);
585		if (IS_ERR(child))
586			return PTR_ERR(child);
587	}
588
589	if (twl_has_madc() && pdata->madc) {
590		child = add_child(2, "twl4030_madc",
591				pdata->madc, sizeof(*pdata->madc),
592				true, pdata->irq_base + MADC_INTR_OFFSET, 0);
593		if (IS_ERR(child))
594			return PTR_ERR(child);
595	}
596
597	if (twl_has_rtc()) {
598		/*
599		 * REVISIT platform_data here currently might expose the
600		 * "msecure" line ... but for now we just expect board
601		 * setup to tell the chip "it's always ok to SET_TIME".
602		 * Eventually, Linux might become more aware of such
603		 * HW security concerns, and "least privilege".
604		 */
605		sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
606		child = add_child(sub_chip_id, "twl_rtc",
607				NULL, 0,
608				true, pdata->irq_base + RTC_INTR_OFFSET, 0);
609		if (IS_ERR(child))
610			return PTR_ERR(child);
611	}
612
613	if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
614
615		static struct regulator_consumer_supply usb1v5 = {
616			.supply =	"usb1v5",
617		};
618		static struct regulator_consumer_supply usb1v8 = {
619			.supply =	"usb1v8",
620		};
621		static struct regulator_consumer_supply usb3v1 = {
622			.supply =	"usb3v1",
623		};
624
625	/* First add the regulators so that they can be used by transceiver */
626		if (twl_has_regulator()) {
627			/* this is a template that gets copied */
628			struct regulator_init_data usb_fixed = {
629				.constraints.valid_modes_mask =
630					REGULATOR_MODE_NORMAL
631					| REGULATOR_MODE_STANDBY,
632				.constraints.valid_ops_mask =
633					REGULATOR_CHANGE_MODE
634					| REGULATOR_CHANGE_STATUS,
635			};
636
637			child = add_regulator_linked(TWL4030_REG_VUSB1V5,
638						      &usb_fixed, &usb1v5, 1);
639			if (IS_ERR(child))
640				return PTR_ERR(child);
641
642			child = add_regulator_linked(TWL4030_REG_VUSB1V8,
643						      &usb_fixed, &usb1v8, 1);
644			if (IS_ERR(child))
645				return PTR_ERR(child);
646
647			child = add_regulator_linked(TWL4030_REG_VUSB3V1,
648						      &usb_fixed, &usb3v1, 1);
649			if (IS_ERR(child))
650				return PTR_ERR(child);
651
652		}
653
654		child = add_child(0, "twl4030_usb",
655				pdata->usb, sizeof(*pdata->usb),
656				true,
657				/* irq0 = USB_PRES, irq1 = USB */
658				pdata->irq_base + USB_PRES_INTR_OFFSET,
659				pdata->irq_base + USB_INTR_OFFSET);
660
661		if (IS_ERR(child))
662			return PTR_ERR(child);
663
664		/* we need to connect regulators to this transceiver */
665		if (twl_has_regulator() && child) {
666			usb1v5.dev = child;
667			usb1v8.dev = child;
668			usb3v1.dev = child;
669		}
670	}
671
672	if (twl_has_watchdog()) {
673		child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
674		if (IS_ERR(child))
675			return PTR_ERR(child);
676	}
677
678	if (twl_has_pwrbutton()) {
679		child = add_child(1, "twl4030_pwrbutton",
680				NULL, 0, true, pdata->irq_base + 8 + 0, 0);
681		if (IS_ERR(child))
682			return PTR_ERR(child);
683	}
684
685	if (twl_has_codec() && pdata->codec && twl_class_is_4030()) {
686		sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
687		child = add_child(sub_chip_id, "twl4030_codec",
688				pdata->codec, sizeof(*pdata->codec),
689				false, 0, 0);
690		if (IS_ERR(child))
691			return PTR_ERR(child);
692	}
693
694	/* Phoenix*/
695	if (twl_has_codec() && pdata->codec && twl_class_is_6030()) {
696		sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
697		child = add_child(sub_chip_id, "twl6040_codec",
698				pdata->codec, sizeof(*pdata->codec),
699				false, 0, 0);
700		if (IS_ERR(child))
701			return PTR_ERR(child);
702	}
703
704	/* twl4030 regulators */
705	if (twl_has_regulator() && twl_class_is_4030()) {
706		child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
707		if (IS_ERR(child))
708			return PTR_ERR(child);
709
710		child = add_regulator(TWL4030_REG_VIO, pdata->vio);
711		if (IS_ERR(child))
712			return PTR_ERR(child);
713
714		child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1);
715		if (IS_ERR(child))
716			return PTR_ERR(child);
717
718		child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2);
719		if (IS_ERR(child))
720			return PTR_ERR(child);
721
722		child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
723		if (IS_ERR(child))
724			return PTR_ERR(child);
725
726		child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
727		if (IS_ERR(child))
728			return PTR_ERR(child);
729
730		child = add_regulator((features & TWL4030_VAUX2)
731					? TWL4030_REG_VAUX2_4030
732					: TWL4030_REG_VAUX2,
733				pdata->vaux2);
734		if (IS_ERR(child))
735			return PTR_ERR(child);
736
737		child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1);
738		if (IS_ERR(child))
739			return PTR_ERR(child);
740
741		child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2);
742		if (IS_ERR(child))
743			return PTR_ERR(child);
744
745		child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig);
746		if (IS_ERR(child))
747			return PTR_ERR(child);
748	}
749
750	/* maybe add LDOs that are omitted on cost-reduced parts */
751	if (twl_has_regulator() && !(features & TPS_SUBSET)
752	  && twl_class_is_4030()) {
753		child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
754		if (IS_ERR(child))
755			return PTR_ERR(child);
756
757		child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
758		if (IS_ERR(child))
759			return PTR_ERR(child);
760
761		child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
762		if (IS_ERR(child))
763			return PTR_ERR(child);
764
765		child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
766		if (IS_ERR(child))
767			return PTR_ERR(child);
768
769		child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
770		if (IS_ERR(child))
771			return PTR_ERR(child);
772
773		child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
774		if (IS_ERR(child))
775			return PTR_ERR(child);
776	}
777
778	/* twl6030 regulators */
779	if (twl_has_regulator() && twl_class_is_6030()) {
780		child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc);
781		if (IS_ERR(child))
782			return PTR_ERR(child);
783
784		child = add_regulator(TWL6030_REG_VPP, pdata->vpp);
785		if (IS_ERR(child))
786			return PTR_ERR(child);
787
788		child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim);
789		if (IS_ERR(child))
790			return PTR_ERR(child);
791
792		child = add_regulator(TWL6030_REG_VANA, pdata->vana);
793		if (IS_ERR(child))
794			return PTR_ERR(child);
795
796		child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio);
797		if (IS_ERR(child))
798			return PTR_ERR(child);
799
800		child = add_regulator(TWL6030_REG_VDAC, pdata->vdac);
801		if (IS_ERR(child))
802			return PTR_ERR(child);
803
804		child = add_regulator(TWL6030_REG_VUSB, pdata->vusb);
805		if (IS_ERR(child))
806			return PTR_ERR(child);
807
808		child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1);
809		if (IS_ERR(child))
810			return PTR_ERR(child);
811
812		child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2);
813		if (IS_ERR(child))
814			return PTR_ERR(child);
815
816		child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3);
817		if (IS_ERR(child))
818			return PTR_ERR(child);
819	}
820
821	return 0;
822}
823
824/*----------------------------------------------------------------------*/
825
826/*
827 * These three functions initialize the on-chip clock framework,
828 * letting it generate the right frequencies for USB, MADC, and
829 * other purposes.
830 */
831static inline int __init protect_pm_master(void)
832{
833	int e = 0;
834
835	e = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, KEY_LOCK,
836			R_PROTECT_KEY);
837	return e;
838}
839
840static inline int __init unprotect_pm_master(void)
841{
842	int e = 0;
843
844	e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, KEY_UNLOCK1,
845			R_PROTECT_KEY);
846	e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, KEY_UNLOCK2,
847			R_PROTECT_KEY);
848	return e;
849}
850
851static void clocks_init(struct device *dev,
852			struct twl4030_clock_init_data *clock)
853{
854	int e = 0;
855	struct clk *osc;
856	u32 rate;
857	u8 ctrl = HFCLK_FREQ_26_MHZ;
858
859#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
860	if (cpu_is_omap2430())
861		osc = clk_get(dev, "osc_ck");
862	else
863		osc = clk_get(dev, "osc_sys_ck");
864
865	if (IS_ERR(osc)) {
866		printk(KERN_WARNING "Skipping twl internal clock init and "
867				"using bootloader value (unknown osc rate)\n");
868		return;
869	}
870
871	rate = clk_get_rate(osc);
872	clk_put(osc);
873
874#else
875	/* REVISIT for non-OMAP systems, pass the clock rate from
876	 * board init code, using platform_data.
877	 */
878	osc = ERR_PTR(-EIO);
879
880	printk(KERN_WARNING "Skipping twl internal clock init and "
881	       "using bootloader value (unknown osc rate)\n");
882
883	return;
884#endif
885
886	switch (rate) {
887	case 19200000:
888		ctrl = HFCLK_FREQ_19p2_MHZ;
889		break;
890	case 26000000:
891		ctrl = HFCLK_FREQ_26_MHZ;
892		break;
893	case 38400000:
894		ctrl = HFCLK_FREQ_38p4_MHZ;
895		break;
896	}
897
898	ctrl |= HIGH_PERF_SQ;
899	if (clock && clock->ck32k_lowpwr_enable)
900		ctrl |= CK32K_LOWPWR_EN;
901
902	e |= unprotect_pm_master();
903	/* effect->MADC+USB ck en */
904	e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
905	e |= protect_pm_master();
906
907	if (e < 0)
908		pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
909}
910
911/*----------------------------------------------------------------------*/
912
913int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
914int twl4030_exit_irq(void);
915int twl4030_init_chip_irq(const char *chip);
916int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
917int twl6030_exit_irq(void);
918
919static int twl_remove(struct i2c_client *client)
920{
921	unsigned i;
922	int status;
923
924	if (twl_class_is_4030())
925		status = twl4030_exit_irq();
926	else
927		status = twl6030_exit_irq();
928
929	if (status < 0)
930		return status;
931
932	for (i = 0; i < TWL_NUM_SLAVES; i++) {
933		struct twl_client	*twl = &twl_modules[i];
934
935		if (twl->client && twl->client != client)
936			i2c_unregister_device(twl->client);
937		twl_modules[i].client = NULL;
938	}
939	inuse = false;
940	return 0;
941}
942
943/* NOTE:  this driver only handles a single twl4030/tps659x0 chip */
944static int __init
945twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
946{
947	int				status;
948	unsigned			i;
949	struct twl4030_platform_data	*pdata = client->dev.platform_data;
950	u8 temp;
951
952	if (!pdata) {
953		dev_dbg(&client->dev, "no platform data?\n");
954		return -EINVAL;
955	}
956
957	if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
958		dev_dbg(&client->dev, "can't talk I2C?\n");
959		return -EIO;
960	}
961
962	if (inuse) {
963		dev_dbg(&client->dev, "driver is already in use\n");
964		return -EBUSY;
965	}
966
967	for (i = 0; i < TWL_NUM_SLAVES; i++) {
968		struct twl_client	*twl = &twl_modules[i];
969
970		twl->address = client->addr + i;
971		if (i == 0)
972			twl->client = client;
973		else {
974			twl->client = i2c_new_dummy(client->adapter,
975					twl->address);
976			if (!twl->client) {
977				dev_err(&client->dev,
978					"can't attach client %d\n", i);
979				status = -ENOMEM;
980				goto fail;
981			}
982		}
983		mutex_init(&twl->xfer_lock);
984	}
985	inuse = true;
986	if ((id->driver_data) & TWL6030_CLASS) {
987		twl_id = TWL6030_CLASS_ID;
988		twl_map = &twl6030_map[0];
989	} else {
990		twl_id = TWL4030_CLASS_ID;
991		twl_map = &twl4030_map[0];
992	}
993
994	/* setup clock framework */
995	clocks_init(&client->dev, pdata->clock);
996
997	/* load power event scripts */
998	if (twl_has_power() && pdata->power)
999		twl4030_power_init(pdata->power);
1000
1001	/* Maybe init the T2 Interrupt subsystem */
1002	if (client->irq
1003			&& pdata->irq_base
1004			&& pdata->irq_end > pdata->irq_base) {
1005		if (twl_class_is_4030()) {
1006			twl4030_init_chip_irq(id->name);
1007			status = twl4030_init_irq(client->irq, pdata->irq_base,
1008			pdata->irq_end);
1009		} else {
1010			status = twl6030_init_irq(client->irq, pdata->irq_base,
1011			pdata->irq_end);
1012		}
1013
1014		if (status < 0)
1015			goto fail;
1016	}
1017
1018	/* Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1019	 * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1020	 * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1021	 */
1022
1023	if (twl_class_is_4030()) {
1024		twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1025		temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
1026		I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
1027		twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1028	}
1029
1030	status = add_children(pdata, id->driver_data);
1031fail:
1032	if (status < 0)
1033		twl_remove(client);
1034	return status;
1035}
1036
1037static const struct i2c_device_id twl_ids[] = {
1038	{ "twl4030", TWL4030_VAUX2 },	/* "Triton 2" */
1039	{ "twl5030", 0 },		/* T2 updated */
1040	{ "twl5031", TWL5031 },		/* TWL5030 updated */
1041	{ "tps65950", 0 },		/* catalog version of twl5030 */
1042	{ "tps65930", TPS_SUBSET },	/* fewer LDOs and DACs; no charger */
1043	{ "tps65920", TPS_SUBSET },	/* fewer LDOs; no codec or charger */
1044	{ "twl6030", TWL6030_CLASS },	/* "Phoenix power chip" */
1045	{ /* end of list */ },
1046};
1047MODULE_DEVICE_TABLE(i2c, twl_ids);
1048
1049/* One Client Driver , 4 Clients */
1050static struct i2c_driver twl_driver = {
1051	.driver.name	= DRIVER_NAME,
1052	.id_table	= twl_ids,
1053	.probe		= twl_probe,
1054	.remove		= twl_remove,
1055};
1056
1057static int __init twl_init(void)
1058{
1059	return i2c_add_driver(&twl_driver);
1060}
1061subsys_initcall(twl_init);
1062
1063static void __exit twl_exit(void)
1064{
1065	i2c_del_driver(&twl_driver);
1066}
1067module_exit(twl_exit);
1068
1069MODULE_AUTHOR("Texas Instruments, Inc.");
1070MODULE_DESCRIPTION("I2C Core interface for TWL");
1071MODULE_LICENSE("GPL");
1072