blob: 40449cdf09db15acb93a3a6c11cfe468da4db12c [file] [log] [blame]
David Brownella603a7f2008-10-15 12:15:39 +02001/*
2 * twl4030_core.c - driver for TWL4030/TPS659x0 PM and audio CODEC devices
3 *
4 * Copyright (C) 2005-2006 Texas Instruments, Inc.
5 *
6 * Modifications to defer interrupt handling to a kernel thread:
7 * Copyright (C) 2006 MontaVista Software, Inc.
8 *
9 * Based on tlv320aic23.c:
10 * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
11 *
12 * Code cleanup and modifications to IRQ handler.
13 * by syed khasim <x0khasim@ti.com>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 */
29
David Brownella603a7f2008-10-15 12:15:39 +020030#include <linux/init.h>
31#include <linux/mutex.h>
David Brownella603a7f2008-10-15 12:15:39 +020032#include <linux/platform_device.h>
33#include <linux/clk.h>
David Brownella30d46c2008-10-20 23:46:28 +020034#include <linux/err.h>
David Brownella603a7f2008-10-15 12:15:39 +020035
David Brownelldad759f2008-12-01 00:43:58 +010036#include <linux/regulator/machine.h>
37
David Brownella603a7f2008-10-15 12:15:39 +020038#include <linux/i2c.h>
39#include <linux/i2c/twl4030.h>
40
Mark Browna313d752009-02-04 21:26:07 +010041#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
Tony Lindgrence491cf2009-10-20 09:40:47 -070042#include <plat/cpu.h>
Samuel Ortizb29c06a2009-01-11 01:21:48 +010043#endif
David Brownella603a7f2008-10-15 12:15:39 +020044
45/*
46 * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
47 * Management and System Companion Device" chips originally designed for
48 * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
49 * often at around 3 Mbit/sec, including for interrupt handling.
50 *
51 * This driver core provides genirq support for the interrupts emitted,
52 * by the various modules, and exports register access primitives.
53 *
54 * FIXME this driver currently requires use of the first interrupt line
55 * (and associated registers).
56 */
57
58#define DRIVER_NAME "twl4030"
59
60#if defined(CONFIG_TWL4030_BCI_BATTERY) || \
61 defined(CONFIG_TWL4030_BCI_BATTERY_MODULE)
62#define twl_has_bci() true
63#else
64#define twl_has_bci() false
65#endif
66
67#if defined(CONFIG_KEYBOARD_TWL4030) || defined(CONFIG_KEYBOARD_TWL4030_MODULE)
68#define twl_has_keypad() true
69#else
70#define twl_has_keypad() false
71#endif
72
73#if defined(CONFIG_GPIO_TWL4030) || defined(CONFIG_GPIO_TWL4030_MODULE)
74#define twl_has_gpio() true
75#else
76#define twl_has_gpio() false
77#endif
78
David Brownelldad759f2008-12-01 00:43:58 +010079#if defined(CONFIG_REGULATOR_TWL4030) \
80 || defined(CONFIG_REGULATOR_TWL4030_MODULE)
81#define twl_has_regulator() true
82#else
83#define twl_has_regulator() false
84#endif
85
David Brownella603a7f2008-10-15 12:15:39 +020086#if defined(CONFIG_TWL4030_MADC) || defined(CONFIG_TWL4030_MADC_MODULE)
87#define twl_has_madc() true
88#else
89#define twl_has_madc() false
90#endif
91
Amit Kucheriaebf0bd32009-08-31 18:32:18 +020092#ifdef CONFIG_TWL4030_POWER
93#define twl_has_power() true
94#else
95#define twl_has_power() false
96#endif
97
David Brownella603a7f2008-10-15 12:15:39 +020098#if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
99#define twl_has_rtc() true
100#else
101#define twl_has_rtc() false
102#endif
103
104#if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
105#define twl_has_usb() true
106#else
107#define twl_has_usb() false
108#endif
109
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200110#if defined(CONFIG_TWL4030_WATCHDOG) || \
111 defined(CONFIG_TWL4030_WATCHDOG_MODULE)
112#define twl_has_watchdog() true
113#else
114#define twl_has_watchdog() false
115#endif
David Brownella603a7f2008-10-15 12:15:39 +0200116
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300117#if defined(CONFIG_TWL4030_CODEC) || defined(CONFIG_TWL4030_CODEC_MODULE)
118#define twl_has_codec() true
119#else
120#define twl_has_codec() false
121#endif
122
David Brownella603a7f2008-10-15 12:15:39 +0200123/* Triton Core internal information (BEGIN) */
124
125/* Last - for index max*/
126#define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
127
128#define TWL4030_NUM_SLAVES 4
129
Felipe Balbi9c3664d2009-08-03 18:16:38 +0200130#if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
131 || defined(CONFIG_INPUT_TWL4030_PWBUTTON_MODULE)
132#define twl_has_pwrbutton() true
133#else
134#define twl_has_pwrbutton() false
135#endif
David Brownella603a7f2008-10-15 12:15:39 +0200136
137/* Base Address defns for twl4030_map[] */
138
139/* subchip/slave 0 - USB ID */
140#define TWL4030_BASEADD_USB 0x0000
141
142/* subchip/slave 1 - AUD ID */
143#define TWL4030_BASEADD_AUDIO_VOICE 0x0000
144#define TWL4030_BASEADD_GPIO 0x0098
145#define TWL4030_BASEADD_INTBR 0x0085
146#define TWL4030_BASEADD_PIH 0x0080
147#define TWL4030_BASEADD_TEST 0x004C
148
149/* subchip/slave 2 - AUX ID */
150#define TWL4030_BASEADD_INTERRUPTS 0x00B9
151#define TWL4030_BASEADD_LED 0x00EE
152#define TWL4030_BASEADD_MADC 0x0000
153#define TWL4030_BASEADD_MAIN_CHARGE 0x0074
154#define TWL4030_BASEADD_PRECHARGE 0x00AA
155#define TWL4030_BASEADD_PWM0 0x00F8
156#define TWL4030_BASEADD_PWM1 0x00FB
157#define TWL4030_BASEADD_PWMA 0x00EF
158#define TWL4030_BASEADD_PWMB 0x00F1
159#define TWL4030_BASEADD_KEYPAD 0x00D2
160
161/* subchip/slave 3 - POWER ID */
162#define TWL4030_BASEADD_BACKUP 0x0014
163#define TWL4030_BASEADD_INT 0x002E
164#define TWL4030_BASEADD_PM_MASTER 0x0036
165#define TWL4030_BASEADD_PM_RECEIVER 0x005B
166#define TWL4030_BASEADD_RTC 0x001C
167#define TWL4030_BASEADD_SECURED_REG 0x0000
168
169/* Triton Core internal information (END) */
170
171
172/* Few power values */
173#define R_CFG_BOOT 0x05
174#define R_PROTECT_KEY 0x0E
175
176/* access control values for R_PROTECT_KEY */
177#define KEY_UNLOCK1 0xce
178#define KEY_UNLOCK2 0xec
179#define KEY_LOCK 0x00
180
181/* some fields in R_CFG_BOOT */
182#define HFCLK_FREQ_19p2_MHZ (1 << 0)
183#define HFCLK_FREQ_26_MHZ (2 << 0)
184#define HFCLK_FREQ_38p4_MHZ (3 << 0)
185#define HIGH_PERF_SQ (1 << 3)
186
187
David Brownelldad759f2008-12-01 00:43:58 +0100188/* chip-specific feature flags, for i2c_device_id.driver_data */
189#define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
190#define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
191
David Brownella603a7f2008-10-15 12:15:39 +0200192/*----------------------------------------------------------------------*/
193
David Brownella603a7f2008-10-15 12:15:39 +0200194/* is driver active, bound to a chip? */
195static bool inuse;
196
197/* Structure for each TWL4030 Slave */
198struct twl4030_client {
199 struct i2c_client *client;
200 u8 address;
201
202 /* max numb of i2c_msg required is for read =2 */
203 struct i2c_msg xfer_msg[2];
204
205 /* To lock access to xfer_msg */
206 struct mutex xfer_lock;
207};
208
209static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
210
211
212/* mapping the module id to slave id and base address */
213struct twl4030mapping {
214 unsigned char sid; /* Slave ID */
215 unsigned char base; /* base address */
216};
217
218static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
219 /*
220 * NOTE: don't change this table without updating the
221 * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
222 * so they continue to match the order in this table.
223 */
224
225 { 0, TWL4030_BASEADD_USB },
226
227 { 1, TWL4030_BASEADD_AUDIO_VOICE },
228 { 1, TWL4030_BASEADD_GPIO },
229 { 1, TWL4030_BASEADD_INTBR },
230 { 1, TWL4030_BASEADD_PIH },
231 { 1, TWL4030_BASEADD_TEST },
232
233 { 2, TWL4030_BASEADD_KEYPAD },
234 { 2, TWL4030_BASEADD_MADC },
235 { 2, TWL4030_BASEADD_INTERRUPTS },
236 { 2, TWL4030_BASEADD_LED },
237 { 2, TWL4030_BASEADD_MAIN_CHARGE },
238 { 2, TWL4030_BASEADD_PRECHARGE },
239 { 2, TWL4030_BASEADD_PWM0 },
240 { 2, TWL4030_BASEADD_PWM1 },
241 { 2, TWL4030_BASEADD_PWMA },
242 { 2, TWL4030_BASEADD_PWMB },
243
244 { 3, TWL4030_BASEADD_BACKUP },
245 { 3, TWL4030_BASEADD_INT },
246 { 3, TWL4030_BASEADD_PM_MASTER },
247 { 3, TWL4030_BASEADD_PM_RECEIVER },
248 { 3, TWL4030_BASEADD_RTC },
249 { 3, TWL4030_BASEADD_SECURED_REG },
250};
251
252/*----------------------------------------------------------------------*/
253
David Brownella603a7f2008-10-15 12:15:39 +0200254/* Exported Functions */
255
256/**
257 * twl4030_i2c_write - Writes a n bit register in TWL4030
258 * @mod_no: module number
259 * @value: an array of num_bytes+1 containing data to write
260 * @reg: register address (just offset will do)
261 * @num_bytes: number of bytes to transfer
262 *
263 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
264 * valid data starts at Offset 1.
265 *
266 * Returns the result of operation - 0 is success
267 */
David Brownell3fba19e2008-11-08 01:13:16 +0100268int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200269{
270 int ret;
271 int sid;
272 struct twl4030_client *twl;
273 struct i2c_msg *msg;
274
275 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
276 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
277 return -EPERM;
278 }
279 sid = twl4030_map[mod_no].sid;
280 twl = &twl4030_modules[sid];
281
282 if (unlikely(!inuse)) {
283 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
284 return -EPERM;
285 }
286 mutex_lock(&twl->xfer_lock);
287 /*
288 * [MSG1]: fill the register address data
289 * fill the data Tx buffer
290 */
291 msg = &twl->xfer_msg[0];
292 msg->addr = twl->address;
293 msg->len = num_bytes + 1;
294 msg->flags = 0;
295 msg->buf = value;
296 /* over write the first byte of buffer with the register address */
297 *value = twl4030_map[mod_no].base + reg;
298 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
299 mutex_unlock(&twl->xfer_lock);
300
301 /* i2cTransfer returns num messages.translate it pls.. */
302 if (ret >= 0)
303 ret = 0;
304 return ret;
305}
306EXPORT_SYMBOL(twl4030_i2c_write);
307
308/**
309 * twl4030_i2c_read - Reads a n bit register in TWL4030
310 * @mod_no: module number
311 * @value: an array of num_bytes containing data to be read
312 * @reg: register address (just offset will do)
313 * @num_bytes: number of bytes to transfer
314 *
315 * Returns result of operation - num_bytes is success else failure.
316 */
David Brownell3fba19e2008-11-08 01:13:16 +0100317int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200318{
319 int ret;
320 u8 val;
321 int sid;
322 struct twl4030_client *twl;
323 struct i2c_msg *msg;
324
325 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
326 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
327 return -EPERM;
328 }
329 sid = twl4030_map[mod_no].sid;
330 twl = &twl4030_modules[sid];
331
332 if (unlikely(!inuse)) {
333 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
334 return -EPERM;
335 }
336 mutex_lock(&twl->xfer_lock);
337 /* [MSG1] fill the register address data */
338 msg = &twl->xfer_msg[0];
339 msg->addr = twl->address;
340 msg->len = 1;
341 msg->flags = 0; /* Read the register value */
342 val = twl4030_map[mod_no].base + reg;
343 msg->buf = &val;
344 /* [MSG2] fill the data rx buffer */
345 msg = &twl->xfer_msg[1];
346 msg->addr = twl->address;
347 msg->flags = I2C_M_RD; /* Read the register value */
348 msg->len = num_bytes; /* only n bytes */
349 msg->buf = value;
350 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
351 mutex_unlock(&twl->xfer_lock);
352
353 /* i2cTransfer returns num messages.translate it pls.. */
354 if (ret >= 0)
355 ret = 0;
356 return ret;
357}
358EXPORT_SYMBOL(twl4030_i2c_read);
359
360/**
361 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
362 * @mod_no: module number
363 * @value: the value to be written 8 bit
364 * @reg: register address (just offset will do)
365 *
366 * Returns result of operation - 0 is success
367 */
368int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
369{
370
371 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
372 u8 temp_buffer[2] = { 0 };
373 /* offset 1 contains the data */
374 temp_buffer[1] = value;
375 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
376}
377EXPORT_SYMBOL(twl4030_i2c_write_u8);
378
379/**
380 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
381 * @mod_no: module number
382 * @value: the value read 8 bit
383 * @reg: register address (just offset will do)
384 *
385 * Returns result of operation - 0 is success
386 */
387int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
388{
389 return twl4030_i2c_read(mod_no, value, reg, 1);
390}
391EXPORT_SYMBOL(twl4030_i2c_read_u8);
392
393/*----------------------------------------------------------------------*/
394
David Brownelldad759f2008-12-01 00:43:58 +0100395static struct device *
396add_numbered_child(unsigned chip, const char *name, int num,
David Brownell5725d662008-12-01 00:31:04 +0100397 void *pdata, unsigned pdata_len,
398 bool can_wakeup, int irq0, int irq1)
399{
400 struct platform_device *pdev;
401 struct twl4030_client *twl = &twl4030_modules[chip];
402 int status;
403
David Brownelldad759f2008-12-01 00:43:58 +0100404 pdev = platform_device_alloc(name, num);
David Brownell5725d662008-12-01 00:31:04 +0100405 if (!pdev) {
406 dev_dbg(&twl->client->dev, "can't alloc dev\n");
407 status = -ENOMEM;
408 goto err;
409 }
410
411 device_init_wakeup(&pdev->dev, can_wakeup);
412 pdev->dev.parent = &twl->client->dev;
413
414 if (pdata) {
415 status = platform_device_add_data(pdev, pdata, pdata_len);
416 if (status < 0) {
417 dev_dbg(&pdev->dev, "can't add platform_data\n");
418 goto err;
419 }
420 }
421
422 if (irq0) {
423 struct resource r[2] = {
424 { .start = irq0, .flags = IORESOURCE_IRQ, },
425 { .start = irq1, .flags = IORESOURCE_IRQ, },
426 };
427
428 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
429 if (status < 0) {
430 dev_dbg(&pdev->dev, "can't add irqs\n");
431 goto err;
432 }
433 }
434
435 status = platform_device_add(pdev);
436
437err:
438 if (status < 0) {
439 platform_device_put(pdev);
440 dev_err(&twl->client->dev, "can't add %s dev\n", name);
441 return ERR_PTR(status);
442 }
443 return &pdev->dev;
444}
445
David Brownelldad759f2008-12-01 00:43:58 +0100446static inline struct device *add_child(unsigned chip, const char *name,
447 void *pdata, unsigned pdata_len,
448 bool can_wakeup, int irq0, int irq1)
449{
450 return add_numbered_child(chip, name, -1, pdata, pdata_len,
451 can_wakeup, irq0, irq1);
452}
453
454static struct device *
455add_regulator_linked(int num, struct regulator_init_data *pdata,
456 struct regulator_consumer_supply *consumers,
457 unsigned num_consumers)
458{
459 /* regulator framework demands init_data ... */
460 if (!pdata)
461 return NULL;
462
David Brownellb73eac72008-12-07 19:10:58 +0100463 if (consumers) {
David Brownelldad759f2008-12-01 00:43:58 +0100464 pdata->consumer_supplies = consumers;
465 pdata->num_consumer_supplies = num_consumers;
466 }
467
468 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
469 return add_numbered_child(3, "twl4030_reg", num,
470 pdata, sizeof(*pdata), false, 0, 0);
471}
472
473static struct device *
474add_regulator(int num, struct regulator_init_data *pdata)
475{
476 return add_regulator_linked(num, pdata, NULL, 0);
477}
478
David Brownella603a7f2008-10-15 12:15:39 +0200479/*
David Brownella30d46c2008-10-20 23:46:28 +0200480 * NOTE: We know the first 8 IRQs after pdata->base_irq are
481 * for the PIH, and the next are for the PWR_INT SIH, since
482 * that's how twl_init_irq() sets things up.
David Brownella603a7f2008-10-15 12:15:39 +0200483 */
David Brownella603a7f2008-10-15 12:15:39 +0200484
David Brownelldad759f2008-12-01 00:43:58 +0100485static int
486add_children(struct twl4030_platform_data *pdata, unsigned long features)
David Brownella603a7f2008-10-15 12:15:39 +0200487{
David Brownell5725d662008-12-01 00:31:04 +0100488 struct device *child;
David Brownella603a7f2008-10-15 12:15:39 +0200489
David Brownelldad759f2008-12-01 00:43:58 +0100490 if (twl_has_bci() && pdata->bci && !(features & TPS_SUBSET)) {
David Brownell5725d662008-12-01 00:31:04 +0100491 child = add_child(3, "twl4030_bci",
492 pdata->bci, sizeof(*pdata->bci),
493 false,
494 /* irq0 = CHG_PRES, irq1 = BCI */
495 pdata->irq_base + 8 + 1, pdata->irq_base + 2);
496 if (IS_ERR(child))
497 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200498 }
499
500 if (twl_has_gpio() && pdata->gpio) {
David Brownell5725d662008-12-01 00:31:04 +0100501 child = add_child(1, "twl4030_gpio",
502 pdata->gpio, sizeof(*pdata->gpio),
503 false, pdata->irq_base + 0, 0);
504 if (IS_ERR(child))
505 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200506 }
507
508 if (twl_has_keypad() && pdata->keypad) {
David Brownell5725d662008-12-01 00:31:04 +0100509 child = add_child(2, "twl4030_keypad",
510 pdata->keypad, sizeof(*pdata->keypad),
511 true, pdata->irq_base + 1, 0);
512 if (IS_ERR(child))
513 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200514 }
515
516 if (twl_has_madc() && pdata->madc) {
David Brownell5725d662008-12-01 00:31:04 +0100517 child = add_child(2, "twl4030_madc",
518 pdata->madc, sizeof(*pdata->madc),
519 true, pdata->irq_base + 3, 0);
520 if (IS_ERR(child))
521 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200522 }
523
524 if (twl_has_rtc()) {
David Brownella603a7f2008-10-15 12:15:39 +0200525 /*
David Brownell5725d662008-12-01 00:31:04 +0100526 * REVISIT platform_data here currently might expose the
David Brownella603a7f2008-10-15 12:15:39 +0200527 * "msecure" line ... but for now we just expect board
David Brownell5725d662008-12-01 00:31:04 +0100528 * setup to tell the chip "it's always ok to SET_TIME".
David Brownella603a7f2008-10-15 12:15:39 +0200529 * Eventually, Linux might become more aware of such
530 * HW security concerns, and "least privilege".
531 */
David Brownell5725d662008-12-01 00:31:04 +0100532 child = add_child(3, "twl4030_rtc",
533 NULL, 0,
534 true, pdata->irq_base + 8 + 3, 0);
535 if (IS_ERR(child))
536 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200537 }
538
539 if (twl_has_usb() && pdata->usb) {
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300540
541 static struct regulator_consumer_supply usb1v5 = {
542 .supply = "usb1v5",
543 };
544 static struct regulator_consumer_supply usb1v8 = {
545 .supply = "usb1v8",
546 };
547 static struct regulator_consumer_supply usb3v1 = {
548 .supply = "usb3v1",
549 };
550
551 /* First add the regulators so that they can be used by transceiver */
552 if (twl_has_regulator()) {
553 /* this is a template that gets copied */
554 struct regulator_init_data usb_fixed = {
555 .constraints.valid_modes_mask =
556 REGULATOR_MODE_NORMAL
557 | REGULATOR_MODE_STANDBY,
558 .constraints.valid_ops_mask =
559 REGULATOR_CHANGE_MODE
560 | REGULATOR_CHANGE_STATUS,
561 };
562
563 child = add_regulator_linked(TWL4030_REG_VUSB1V5,
564 &usb_fixed, &usb1v5, 1);
565 if (IS_ERR(child))
566 return PTR_ERR(child);
567
568 child = add_regulator_linked(TWL4030_REG_VUSB1V8,
569 &usb_fixed, &usb1v8, 1);
570 if (IS_ERR(child))
571 return PTR_ERR(child);
572
573 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
574 &usb_fixed, &usb3v1, 1);
575 if (IS_ERR(child))
576 return PTR_ERR(child);
577
578 }
579
David Brownell5725d662008-12-01 00:31:04 +0100580 child = add_child(0, "twl4030_usb",
581 pdata->usb, sizeof(*pdata->usb),
582 true,
583 /* irq0 = USB_PRES, irq1 = USB */
584 pdata->irq_base + 8 + 2, pdata->irq_base + 4);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300585
David Brownell5725d662008-12-01 00:31:04 +0100586 if (IS_ERR(child))
587 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100588
589 /* we need to connect regulators to this transceiver */
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300590 if (twl_has_regulator() && child) {
591 usb1v5.dev = child;
592 usb1v8.dev = child;
593 usb3v1.dev = child;
594 }
David Brownelldad759f2008-12-01 00:43:58 +0100595 }
596
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200597 if (twl_has_watchdog()) {
598 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
599 if (IS_ERR(child))
600 return PTR_ERR(child);
601 }
602
Felipe Balbi9c3664d2009-08-03 18:16:38 +0200603 if (twl_has_pwrbutton()) {
604 child = add_child(1, "twl4030_pwrbutton",
605 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
606 if (IS_ERR(child))
607 return PTR_ERR(child);
608 }
609
Peter Ujfalusi0b83dde2009-10-22 13:26:45 +0300610 if (twl_has_codec() && pdata->codec) {
611 child = add_child(1, "twl4030_codec",
612 pdata->codec, sizeof(*pdata->codec),
613 false, 0, 0);
614 if (IS_ERR(child))
615 return PTR_ERR(child);
616 }
617
David Brownelldad759f2008-12-01 00:43:58 +0100618 if (twl_has_regulator()) {
619 /*
620 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
621 if (IS_ERR(child))
622 return PTR_ERR(child);
623 */
624
625 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
626 if (IS_ERR(child))
627 return PTR_ERR(child);
628
629 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
630 if (IS_ERR(child))
631 return PTR_ERR(child);
632
633 child = add_regulator((features & TWL4030_VAUX2)
634 ? TWL4030_REG_VAUX2_4030
635 : TWL4030_REG_VAUX2,
636 pdata->vaux2);
637 if (IS_ERR(child))
638 return PTR_ERR(child);
639 }
640
David Brownelldad759f2008-12-01 00:43:58 +0100641 /* maybe add LDOs that are omitted on cost-reduced parts */
642 if (twl_has_regulator() && !(features & TPS_SUBSET)) {
David Brownelldad759f2008-12-01 00:43:58 +0100643 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
644 if (IS_ERR(child))
645 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100646
647 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
648 if (IS_ERR(child))
649 return PTR_ERR(child);
650
651 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
652 if (IS_ERR(child))
653 return PTR_ERR(child);
654
655 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
656 if (IS_ERR(child))
657 return PTR_ERR(child);
658
659 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
660 if (IS_ERR(child))
661 return PTR_ERR(child);
662
663 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
664 if (IS_ERR(child))
665 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200666 }
667
David Brownell5725d662008-12-01 00:31:04 +0100668 return 0;
David Brownella603a7f2008-10-15 12:15:39 +0200669}
670
671/*----------------------------------------------------------------------*/
672
673/*
674 * These three functions initialize the on-chip clock framework,
675 * letting it generate the right frequencies for USB, MADC, and
676 * other purposes.
677 */
678static inline int __init protect_pm_master(void)
679{
680 int e = 0;
681
682 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
683 R_PROTECT_KEY);
684 return e;
685}
686
687static inline int __init unprotect_pm_master(void)
688{
689 int e = 0;
690
691 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
692 R_PROTECT_KEY);
693 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
694 R_PROTECT_KEY);
695 return e;
696}
697
Rakib Mullick04ffa132009-04-19 08:56:49 +0600698static void clocks_init(struct device *dev)
David Brownella603a7f2008-10-15 12:15:39 +0200699{
700 int e = 0;
701 struct clk *osc;
702 u32 rate;
703 u8 ctrl = HFCLK_FREQ_26_MHZ;
704
705#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
706 if (cpu_is_omap2430())
Russell Kinge6b50c82009-01-10 10:40:42 +0000707 osc = clk_get(dev, "osc_ck");
David Brownella603a7f2008-10-15 12:15:39 +0200708 else
Russell Kinge6b50c82009-01-10 10:40:42 +0000709 osc = clk_get(dev, "osc_sys_ck");
Samuel Ortiz6354ab5c2008-12-07 20:50:25 +0100710
David Brownella603a7f2008-10-15 12:15:39 +0200711 if (IS_ERR(osc)) {
712 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
713 "using bootloader value (unknown osc rate)\n");
714 return;
715 }
716
717 rate = clk_get_rate(osc);
718 clk_put(osc);
719
Samuel Ortiz6354ab5c2008-12-07 20:50:25 +0100720#else
721 /* REVISIT for non-OMAP systems, pass the clock rate from
722 * board init code, using platform_data.
723 */
724 osc = ERR_PTR(-EIO);
725
726 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
727 "using bootloader value (unknown osc rate)\n");
728
729 return;
730#endif
731
David Brownella603a7f2008-10-15 12:15:39 +0200732 switch (rate) {
733 case 19200000:
734 ctrl = HFCLK_FREQ_19p2_MHZ;
735 break;
736 case 26000000:
737 ctrl = HFCLK_FREQ_26_MHZ;
738 break;
739 case 38400000:
740 ctrl = HFCLK_FREQ_38p4_MHZ;
741 break;
742 }
743
744 ctrl |= HIGH_PERF_SQ;
745 e |= unprotect_pm_master();
746 /* effect->MADC+USB ck en */
747 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
748 e |= protect_pm_master();
749
750 if (e < 0)
751 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
752}
753
754/*----------------------------------------------------------------------*/
755
David Brownella30d46c2008-10-20 23:46:28 +0200756int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
757int twl_exit_irq(void);
David Brownella603a7f2008-10-15 12:15:39 +0200758
759static int twl4030_remove(struct i2c_client *client)
760{
761 unsigned i;
David Brownella30d46c2008-10-20 23:46:28 +0200762 int status;
David Brownella603a7f2008-10-15 12:15:39 +0200763
David Brownella30d46c2008-10-20 23:46:28 +0200764 status = twl_exit_irq();
765 if (status < 0)
766 return status;
David Brownella603a7f2008-10-15 12:15:39 +0200767
768 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
769 struct twl4030_client *twl = &twl4030_modules[i];
770
771 if (twl->client && twl->client != client)
772 i2c_unregister_device(twl->client);
773 twl4030_modules[i].client = NULL;
774 }
775 inuse = false;
776 return 0;
777}
778
779/* NOTE: this driver only handles a single twl4030/tps659x0 chip */
Daniel J Bluemanb5e102c2009-11-25 12:12:59 +0000780static int __init
David Brownella603a7f2008-10-15 12:15:39 +0200781twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
782{
783 int status;
784 unsigned i;
785 struct twl4030_platform_data *pdata = client->dev.platform_data;
786
787 if (!pdata) {
788 dev_dbg(&client->dev, "no platform data?\n");
789 return -EINVAL;
790 }
791
792 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
793 dev_dbg(&client->dev, "can't talk I2C?\n");
794 return -EIO;
795 }
796
David Brownella30d46c2008-10-20 23:46:28 +0200797 if (inuse) {
David Brownella603a7f2008-10-15 12:15:39 +0200798 dev_dbg(&client->dev, "driver is already in use\n");
799 return -EBUSY;
800 }
801
802 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
803 struct twl4030_client *twl = &twl4030_modules[i];
804
805 twl->address = client->addr + i;
806 if (i == 0)
807 twl->client = client;
808 else {
809 twl->client = i2c_new_dummy(client->adapter,
810 twl->address);
811 if (!twl->client) {
Ilkka Koskinena8643432009-10-20 16:22:52 +0300812 dev_err(&client->dev,
David Brownella603a7f2008-10-15 12:15:39 +0200813 "can't attach client %d\n", i);
814 status = -ENOMEM;
815 goto fail;
816 }
817 strlcpy(twl->client->name, id->name,
818 sizeof(twl->client->name));
819 }
820 mutex_init(&twl->xfer_lock);
821 }
822 inuse = true;
823
824 /* setup clock framework */
Russell Kinge6b50c82009-01-10 10:40:42 +0000825 clocks_init(&client->dev);
David Brownella603a7f2008-10-15 12:15:39 +0200826
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200827 /* load power event scripts */
828 if (twl_has_power() && pdata->power)
829 twl4030_power_init(pdata->power);
830
David Brownella603a7f2008-10-15 12:15:39 +0200831 /* Maybe init the T2 Interrupt subsystem */
832 if (client->irq
833 && pdata->irq_base
834 && pdata->irq_end > pdata->irq_base) {
David Brownella30d46c2008-10-20 23:46:28 +0200835 status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
836 if (status < 0)
837 goto fail;
David Brownella603a7f2008-10-15 12:15:39 +0200838 }
839
David Brownelldad759f2008-12-01 00:43:58 +0100840 status = add_children(pdata, id->driver_data);
David Brownella603a7f2008-10-15 12:15:39 +0200841fail:
842 if (status < 0)
843 twl4030_remove(client);
844 return status;
845}
846
847static const struct i2c_device_id twl4030_ids[] = {
David Brownelldad759f2008-12-01 00:43:58 +0100848 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
849 { "twl5030", 0 }, /* T2 updated */
850 { "tps65950", 0 }, /* catalog version of twl5030 */
851 { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
852 { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
David Brownella603a7f2008-10-15 12:15:39 +0200853 { /* end of list */ },
854};
855MODULE_DEVICE_TABLE(i2c, twl4030_ids);
856
857/* One Client Driver , 4 Clients */
858static struct i2c_driver twl4030_driver = {
859 .driver.name = DRIVER_NAME,
860 .id_table = twl4030_ids,
861 .probe = twl4030_probe,
862 .remove = twl4030_remove,
863};
864
865static int __init twl4030_init(void)
866{
867 return i2c_add_driver(&twl4030_driver);
868}
869subsys_initcall(twl4030_init);
870
871static void __exit twl4030_exit(void)
872{
873 i2c_del_driver(&twl4030_driver);
874}
875module_exit(twl4030_exit);
876
877MODULE_AUTHOR("Texas Instruments, Inc.");
878MODULE_DESCRIPTION("I2C Core interface for TWL4030");
879MODULE_LICENSE("GPL");