blob: a1c47ee95c0eef065e591cfeadde949eafb8e414 [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)
Samuel Ortizb29c06a2009-01-11 01:21:48 +010042#include <mach/cpu.h>
43#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
117/* Triton Core internal information (BEGIN) */
118
119/* Last - for index max*/
120#define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
121
122#define TWL4030_NUM_SLAVES 4
123
Felipe Balbi9c3664d2009-08-03 18:16:38 +0200124#if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
125 || defined(CONFIG_INPUT_TWL4030_PWBUTTON_MODULE)
126#define twl_has_pwrbutton() true
127#else
128#define twl_has_pwrbutton() false
129#endif
David Brownella603a7f2008-10-15 12:15:39 +0200130
131/* Base Address defns for twl4030_map[] */
132
133/* subchip/slave 0 - USB ID */
134#define TWL4030_BASEADD_USB 0x0000
135
136/* subchip/slave 1 - AUD ID */
137#define TWL4030_BASEADD_AUDIO_VOICE 0x0000
138#define TWL4030_BASEADD_GPIO 0x0098
139#define TWL4030_BASEADD_INTBR 0x0085
140#define TWL4030_BASEADD_PIH 0x0080
141#define TWL4030_BASEADD_TEST 0x004C
142
143/* subchip/slave 2 - AUX ID */
144#define TWL4030_BASEADD_INTERRUPTS 0x00B9
145#define TWL4030_BASEADD_LED 0x00EE
146#define TWL4030_BASEADD_MADC 0x0000
147#define TWL4030_BASEADD_MAIN_CHARGE 0x0074
148#define TWL4030_BASEADD_PRECHARGE 0x00AA
149#define TWL4030_BASEADD_PWM0 0x00F8
150#define TWL4030_BASEADD_PWM1 0x00FB
151#define TWL4030_BASEADD_PWMA 0x00EF
152#define TWL4030_BASEADD_PWMB 0x00F1
153#define TWL4030_BASEADD_KEYPAD 0x00D2
154
155/* subchip/slave 3 - POWER ID */
156#define TWL4030_BASEADD_BACKUP 0x0014
157#define TWL4030_BASEADD_INT 0x002E
158#define TWL4030_BASEADD_PM_MASTER 0x0036
159#define TWL4030_BASEADD_PM_RECEIVER 0x005B
160#define TWL4030_BASEADD_RTC 0x001C
161#define TWL4030_BASEADD_SECURED_REG 0x0000
162
163/* Triton Core internal information (END) */
164
165
166/* Few power values */
167#define R_CFG_BOOT 0x05
168#define R_PROTECT_KEY 0x0E
169
170/* access control values for R_PROTECT_KEY */
171#define KEY_UNLOCK1 0xce
172#define KEY_UNLOCK2 0xec
173#define KEY_LOCK 0x00
174
175/* some fields in R_CFG_BOOT */
176#define HFCLK_FREQ_19p2_MHZ (1 << 0)
177#define HFCLK_FREQ_26_MHZ (2 << 0)
178#define HFCLK_FREQ_38p4_MHZ (3 << 0)
179#define HIGH_PERF_SQ (1 << 3)
180
181
David Brownelldad759f2008-12-01 00:43:58 +0100182/* chip-specific feature flags, for i2c_device_id.driver_data */
183#define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
184#define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
185
David Brownella603a7f2008-10-15 12:15:39 +0200186/*----------------------------------------------------------------------*/
187
David Brownella603a7f2008-10-15 12:15:39 +0200188/* is driver active, bound to a chip? */
189static bool inuse;
190
191/* Structure for each TWL4030 Slave */
192struct twl4030_client {
193 struct i2c_client *client;
194 u8 address;
195
196 /* max numb of i2c_msg required is for read =2 */
197 struct i2c_msg xfer_msg[2];
198
199 /* To lock access to xfer_msg */
200 struct mutex xfer_lock;
201};
202
203static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
204
205
206/* mapping the module id to slave id and base address */
207struct twl4030mapping {
208 unsigned char sid; /* Slave ID */
209 unsigned char base; /* base address */
210};
211
212static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
213 /*
214 * NOTE: don't change this table without updating the
215 * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
216 * so they continue to match the order in this table.
217 */
218
219 { 0, TWL4030_BASEADD_USB },
220
221 { 1, TWL4030_BASEADD_AUDIO_VOICE },
222 { 1, TWL4030_BASEADD_GPIO },
223 { 1, TWL4030_BASEADD_INTBR },
224 { 1, TWL4030_BASEADD_PIH },
225 { 1, TWL4030_BASEADD_TEST },
226
227 { 2, TWL4030_BASEADD_KEYPAD },
228 { 2, TWL4030_BASEADD_MADC },
229 { 2, TWL4030_BASEADD_INTERRUPTS },
230 { 2, TWL4030_BASEADD_LED },
231 { 2, TWL4030_BASEADD_MAIN_CHARGE },
232 { 2, TWL4030_BASEADD_PRECHARGE },
233 { 2, TWL4030_BASEADD_PWM0 },
234 { 2, TWL4030_BASEADD_PWM1 },
235 { 2, TWL4030_BASEADD_PWMA },
236 { 2, TWL4030_BASEADD_PWMB },
237
238 { 3, TWL4030_BASEADD_BACKUP },
239 { 3, TWL4030_BASEADD_INT },
240 { 3, TWL4030_BASEADD_PM_MASTER },
241 { 3, TWL4030_BASEADD_PM_RECEIVER },
242 { 3, TWL4030_BASEADD_RTC },
243 { 3, TWL4030_BASEADD_SECURED_REG },
244};
245
246/*----------------------------------------------------------------------*/
247
David Brownella603a7f2008-10-15 12:15:39 +0200248/* Exported Functions */
249
250/**
251 * twl4030_i2c_write - Writes a n bit register in TWL4030
252 * @mod_no: module number
253 * @value: an array of num_bytes+1 containing data to write
254 * @reg: register address (just offset will do)
255 * @num_bytes: number of bytes to transfer
256 *
257 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
258 * valid data starts at Offset 1.
259 *
260 * Returns the result of operation - 0 is success
261 */
David Brownell3fba19e2008-11-08 01:13:16 +0100262int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200263{
264 int ret;
265 int sid;
266 struct twl4030_client *twl;
267 struct i2c_msg *msg;
268
269 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
270 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
271 return -EPERM;
272 }
273 sid = twl4030_map[mod_no].sid;
274 twl = &twl4030_modules[sid];
275
276 if (unlikely(!inuse)) {
277 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
278 return -EPERM;
279 }
280 mutex_lock(&twl->xfer_lock);
281 /*
282 * [MSG1]: fill the register address data
283 * fill the data Tx buffer
284 */
285 msg = &twl->xfer_msg[0];
286 msg->addr = twl->address;
287 msg->len = num_bytes + 1;
288 msg->flags = 0;
289 msg->buf = value;
290 /* over write the first byte of buffer with the register address */
291 *value = twl4030_map[mod_no].base + reg;
292 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
293 mutex_unlock(&twl->xfer_lock);
294
295 /* i2cTransfer returns num messages.translate it pls.. */
296 if (ret >= 0)
297 ret = 0;
298 return ret;
299}
300EXPORT_SYMBOL(twl4030_i2c_write);
301
302/**
303 * twl4030_i2c_read - Reads a n bit register in TWL4030
304 * @mod_no: module number
305 * @value: an array of num_bytes containing data to be read
306 * @reg: register address (just offset will do)
307 * @num_bytes: number of bytes to transfer
308 *
309 * Returns result of operation - num_bytes is success else failure.
310 */
David Brownell3fba19e2008-11-08 01:13:16 +0100311int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200312{
313 int ret;
314 u8 val;
315 int sid;
316 struct twl4030_client *twl;
317 struct i2c_msg *msg;
318
319 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
320 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
321 return -EPERM;
322 }
323 sid = twl4030_map[mod_no].sid;
324 twl = &twl4030_modules[sid];
325
326 if (unlikely(!inuse)) {
327 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
328 return -EPERM;
329 }
330 mutex_lock(&twl->xfer_lock);
331 /* [MSG1] fill the register address data */
332 msg = &twl->xfer_msg[0];
333 msg->addr = twl->address;
334 msg->len = 1;
335 msg->flags = 0; /* Read the register value */
336 val = twl4030_map[mod_no].base + reg;
337 msg->buf = &val;
338 /* [MSG2] fill the data rx buffer */
339 msg = &twl->xfer_msg[1];
340 msg->addr = twl->address;
341 msg->flags = I2C_M_RD; /* Read the register value */
342 msg->len = num_bytes; /* only n bytes */
343 msg->buf = value;
344 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
345 mutex_unlock(&twl->xfer_lock);
346
347 /* i2cTransfer returns num messages.translate it pls.. */
348 if (ret >= 0)
349 ret = 0;
350 return ret;
351}
352EXPORT_SYMBOL(twl4030_i2c_read);
353
354/**
355 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
356 * @mod_no: module number
357 * @value: the value to be written 8 bit
358 * @reg: register address (just offset will do)
359 *
360 * Returns result of operation - 0 is success
361 */
362int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
363{
364
365 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
366 u8 temp_buffer[2] = { 0 };
367 /* offset 1 contains the data */
368 temp_buffer[1] = value;
369 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
370}
371EXPORT_SYMBOL(twl4030_i2c_write_u8);
372
373/**
374 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
375 * @mod_no: module number
376 * @value: the value read 8 bit
377 * @reg: register address (just offset will do)
378 *
379 * Returns result of operation - 0 is success
380 */
381int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
382{
383 return twl4030_i2c_read(mod_no, value, reg, 1);
384}
385EXPORT_SYMBOL(twl4030_i2c_read_u8);
386
387/*----------------------------------------------------------------------*/
388
David Brownelldad759f2008-12-01 00:43:58 +0100389static struct device *
390add_numbered_child(unsigned chip, const char *name, int num,
David Brownell5725d662008-12-01 00:31:04 +0100391 void *pdata, unsigned pdata_len,
392 bool can_wakeup, int irq0, int irq1)
393{
394 struct platform_device *pdev;
395 struct twl4030_client *twl = &twl4030_modules[chip];
396 int status;
397
David Brownelldad759f2008-12-01 00:43:58 +0100398 pdev = platform_device_alloc(name, num);
David Brownell5725d662008-12-01 00:31:04 +0100399 if (!pdev) {
400 dev_dbg(&twl->client->dev, "can't alloc dev\n");
401 status = -ENOMEM;
402 goto err;
403 }
404
405 device_init_wakeup(&pdev->dev, can_wakeup);
406 pdev->dev.parent = &twl->client->dev;
407
408 if (pdata) {
409 status = platform_device_add_data(pdev, pdata, pdata_len);
410 if (status < 0) {
411 dev_dbg(&pdev->dev, "can't add platform_data\n");
412 goto err;
413 }
414 }
415
416 if (irq0) {
417 struct resource r[2] = {
418 { .start = irq0, .flags = IORESOURCE_IRQ, },
419 { .start = irq1, .flags = IORESOURCE_IRQ, },
420 };
421
422 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
423 if (status < 0) {
424 dev_dbg(&pdev->dev, "can't add irqs\n");
425 goto err;
426 }
427 }
428
429 status = platform_device_add(pdev);
430
431err:
432 if (status < 0) {
433 platform_device_put(pdev);
434 dev_err(&twl->client->dev, "can't add %s dev\n", name);
435 return ERR_PTR(status);
436 }
437 return &pdev->dev;
438}
439
David Brownelldad759f2008-12-01 00:43:58 +0100440static inline struct device *add_child(unsigned chip, const char *name,
441 void *pdata, unsigned pdata_len,
442 bool can_wakeup, int irq0, int irq1)
443{
444 return add_numbered_child(chip, name, -1, pdata, pdata_len,
445 can_wakeup, irq0, irq1);
446}
447
448static struct device *
449add_regulator_linked(int num, struct regulator_init_data *pdata,
450 struct regulator_consumer_supply *consumers,
451 unsigned num_consumers)
452{
453 /* regulator framework demands init_data ... */
454 if (!pdata)
455 return NULL;
456
David Brownellb73eac72008-12-07 19:10:58 +0100457 if (consumers) {
David Brownelldad759f2008-12-01 00:43:58 +0100458 pdata->consumer_supplies = consumers;
459 pdata->num_consumer_supplies = num_consumers;
460 }
461
462 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
463 return add_numbered_child(3, "twl4030_reg", num,
464 pdata, sizeof(*pdata), false, 0, 0);
465}
466
467static struct device *
468add_regulator(int num, struct regulator_init_data *pdata)
469{
470 return add_regulator_linked(num, pdata, NULL, 0);
471}
472
David Brownella603a7f2008-10-15 12:15:39 +0200473/*
David Brownella30d46c2008-10-20 23:46:28 +0200474 * NOTE: We know the first 8 IRQs after pdata->base_irq are
475 * for the PIH, and the next are for the PWR_INT SIH, since
476 * that's how twl_init_irq() sets things up.
David Brownella603a7f2008-10-15 12:15:39 +0200477 */
David Brownella603a7f2008-10-15 12:15:39 +0200478
David Brownelldad759f2008-12-01 00:43:58 +0100479static int
480add_children(struct twl4030_platform_data *pdata, unsigned long features)
David Brownella603a7f2008-10-15 12:15:39 +0200481{
David Brownell5725d662008-12-01 00:31:04 +0100482 struct device *child;
David Brownella603a7f2008-10-15 12:15:39 +0200483
David Brownelldad759f2008-12-01 00:43:58 +0100484 if (twl_has_bci() && pdata->bci && !(features & TPS_SUBSET)) {
David Brownell5725d662008-12-01 00:31:04 +0100485 child = add_child(3, "twl4030_bci",
486 pdata->bci, sizeof(*pdata->bci),
487 false,
488 /* irq0 = CHG_PRES, irq1 = BCI */
489 pdata->irq_base + 8 + 1, pdata->irq_base + 2);
490 if (IS_ERR(child))
491 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200492 }
493
494 if (twl_has_gpio() && pdata->gpio) {
David Brownell5725d662008-12-01 00:31:04 +0100495 child = add_child(1, "twl4030_gpio",
496 pdata->gpio, sizeof(*pdata->gpio),
497 false, pdata->irq_base + 0, 0);
498 if (IS_ERR(child))
499 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200500 }
501
502 if (twl_has_keypad() && pdata->keypad) {
David Brownell5725d662008-12-01 00:31:04 +0100503 child = add_child(2, "twl4030_keypad",
504 pdata->keypad, sizeof(*pdata->keypad),
505 true, pdata->irq_base + 1, 0);
506 if (IS_ERR(child))
507 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200508 }
509
510 if (twl_has_madc() && pdata->madc) {
David Brownell5725d662008-12-01 00:31:04 +0100511 child = add_child(2, "twl4030_madc",
512 pdata->madc, sizeof(*pdata->madc),
513 true, pdata->irq_base + 3, 0);
514 if (IS_ERR(child))
515 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200516 }
517
518 if (twl_has_rtc()) {
David Brownella603a7f2008-10-15 12:15:39 +0200519 /*
David Brownell5725d662008-12-01 00:31:04 +0100520 * REVISIT platform_data here currently might expose the
David Brownella603a7f2008-10-15 12:15:39 +0200521 * "msecure" line ... but for now we just expect board
David Brownell5725d662008-12-01 00:31:04 +0100522 * setup to tell the chip "it's always ok to SET_TIME".
David Brownella603a7f2008-10-15 12:15:39 +0200523 * Eventually, Linux might become more aware of such
524 * HW security concerns, and "least privilege".
525 */
David Brownell5725d662008-12-01 00:31:04 +0100526 child = add_child(3, "twl4030_rtc",
527 NULL, 0,
528 true, pdata->irq_base + 8 + 3, 0);
529 if (IS_ERR(child))
530 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200531 }
532
533 if (twl_has_usb() && pdata->usb) {
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300534
535 static struct regulator_consumer_supply usb1v5 = {
536 .supply = "usb1v5",
537 };
538 static struct regulator_consumer_supply usb1v8 = {
539 .supply = "usb1v8",
540 };
541 static struct regulator_consumer_supply usb3v1 = {
542 .supply = "usb3v1",
543 };
544
545 /* First add the regulators so that they can be used by transceiver */
546 if (twl_has_regulator()) {
547 /* this is a template that gets copied */
548 struct regulator_init_data usb_fixed = {
549 .constraints.valid_modes_mask =
550 REGULATOR_MODE_NORMAL
551 | REGULATOR_MODE_STANDBY,
552 .constraints.valid_ops_mask =
553 REGULATOR_CHANGE_MODE
554 | REGULATOR_CHANGE_STATUS,
555 };
556
557 child = add_regulator_linked(TWL4030_REG_VUSB1V5,
558 &usb_fixed, &usb1v5, 1);
559 if (IS_ERR(child))
560 return PTR_ERR(child);
561
562 child = add_regulator_linked(TWL4030_REG_VUSB1V8,
563 &usb_fixed, &usb1v8, 1);
564 if (IS_ERR(child))
565 return PTR_ERR(child);
566
567 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
568 &usb_fixed, &usb3v1, 1);
569 if (IS_ERR(child))
570 return PTR_ERR(child);
571
572 }
573
David Brownell5725d662008-12-01 00:31:04 +0100574 child = add_child(0, "twl4030_usb",
575 pdata->usb, sizeof(*pdata->usb),
576 true,
577 /* irq0 = USB_PRES, irq1 = USB */
578 pdata->irq_base + 8 + 2, pdata->irq_base + 4);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300579
David Brownell5725d662008-12-01 00:31:04 +0100580 if (IS_ERR(child))
581 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100582
583 /* we need to connect regulators to this transceiver */
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300584 if (twl_has_regulator() && child) {
585 usb1v5.dev = child;
586 usb1v8.dev = child;
587 usb3v1.dev = child;
588 }
David Brownelldad759f2008-12-01 00:43:58 +0100589 }
590
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200591 if (twl_has_watchdog()) {
592 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
593 if (IS_ERR(child))
594 return PTR_ERR(child);
595 }
596
Felipe Balbi9c3664d2009-08-03 18:16:38 +0200597 if (twl_has_pwrbutton()) {
598 child = add_child(1, "twl4030_pwrbutton",
599 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
600 if (IS_ERR(child))
601 return PTR_ERR(child);
602 }
603
David Brownelldad759f2008-12-01 00:43:58 +0100604 if (twl_has_regulator()) {
605 /*
606 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
607 if (IS_ERR(child))
608 return PTR_ERR(child);
609 */
610
611 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
612 if (IS_ERR(child))
613 return PTR_ERR(child);
614
615 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
616 if (IS_ERR(child))
617 return PTR_ERR(child);
618
619 child = add_regulator((features & TWL4030_VAUX2)
620 ? TWL4030_REG_VAUX2_4030
621 : TWL4030_REG_VAUX2,
622 pdata->vaux2);
623 if (IS_ERR(child))
624 return PTR_ERR(child);
625 }
626
David Brownelldad759f2008-12-01 00:43:58 +0100627 /* maybe add LDOs that are omitted on cost-reduced parts */
628 if (twl_has_regulator() && !(features & TPS_SUBSET)) {
David Brownelldad759f2008-12-01 00:43:58 +0100629 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
630 if (IS_ERR(child))
631 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100632
633 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
634 if (IS_ERR(child))
635 return PTR_ERR(child);
636
637 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
638 if (IS_ERR(child))
639 return PTR_ERR(child);
640
641 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
642 if (IS_ERR(child))
643 return PTR_ERR(child);
644
645 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
646 if (IS_ERR(child))
647 return PTR_ERR(child);
648
649 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
650 if (IS_ERR(child))
651 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200652 }
653
David Brownell5725d662008-12-01 00:31:04 +0100654 return 0;
David Brownella603a7f2008-10-15 12:15:39 +0200655}
656
657/*----------------------------------------------------------------------*/
658
659/*
660 * These three functions initialize the on-chip clock framework,
661 * letting it generate the right frequencies for USB, MADC, and
662 * other purposes.
663 */
664static inline int __init protect_pm_master(void)
665{
666 int e = 0;
667
668 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
669 R_PROTECT_KEY);
670 return e;
671}
672
673static inline int __init unprotect_pm_master(void)
674{
675 int e = 0;
676
677 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
678 R_PROTECT_KEY);
679 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
680 R_PROTECT_KEY);
681 return e;
682}
683
Rakib Mullick04ffa132009-04-19 08:56:49 +0600684static void clocks_init(struct device *dev)
David Brownella603a7f2008-10-15 12:15:39 +0200685{
686 int e = 0;
687 struct clk *osc;
688 u32 rate;
689 u8 ctrl = HFCLK_FREQ_26_MHZ;
690
691#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
692 if (cpu_is_omap2430())
Russell Kinge6b50c82009-01-10 10:40:42 +0000693 osc = clk_get(dev, "osc_ck");
David Brownella603a7f2008-10-15 12:15:39 +0200694 else
Russell Kinge6b50c82009-01-10 10:40:42 +0000695 osc = clk_get(dev, "osc_sys_ck");
Samuel Ortiz6354ab52008-12-07 20:50:25 +0100696
David Brownella603a7f2008-10-15 12:15:39 +0200697 if (IS_ERR(osc)) {
698 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
699 "using bootloader value (unknown osc rate)\n");
700 return;
701 }
702
703 rate = clk_get_rate(osc);
704 clk_put(osc);
705
Samuel Ortiz6354ab52008-12-07 20:50:25 +0100706#else
707 /* REVISIT for non-OMAP systems, pass the clock rate from
708 * board init code, using platform_data.
709 */
710 osc = ERR_PTR(-EIO);
711
712 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
713 "using bootloader value (unknown osc rate)\n");
714
715 return;
716#endif
717
David Brownella603a7f2008-10-15 12:15:39 +0200718 switch (rate) {
719 case 19200000:
720 ctrl = HFCLK_FREQ_19p2_MHZ;
721 break;
722 case 26000000:
723 ctrl = HFCLK_FREQ_26_MHZ;
724 break;
725 case 38400000:
726 ctrl = HFCLK_FREQ_38p4_MHZ;
727 break;
728 }
729
730 ctrl |= HIGH_PERF_SQ;
731 e |= unprotect_pm_master();
732 /* effect->MADC+USB ck en */
733 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
734 e |= protect_pm_master();
735
736 if (e < 0)
737 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
738}
739
740/*----------------------------------------------------------------------*/
741
David Brownella30d46c2008-10-20 23:46:28 +0200742int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
743int twl_exit_irq(void);
David Brownella603a7f2008-10-15 12:15:39 +0200744
745static int twl4030_remove(struct i2c_client *client)
746{
747 unsigned i;
David Brownella30d46c2008-10-20 23:46:28 +0200748 int status;
David Brownella603a7f2008-10-15 12:15:39 +0200749
David Brownella30d46c2008-10-20 23:46:28 +0200750 status = twl_exit_irq();
751 if (status < 0)
752 return status;
David Brownella603a7f2008-10-15 12:15:39 +0200753
754 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
755 struct twl4030_client *twl = &twl4030_modules[i];
756
757 if (twl->client && twl->client != client)
758 i2c_unregister_device(twl->client);
759 twl4030_modules[i].client = NULL;
760 }
761 inuse = false;
762 return 0;
763}
764
765/* NOTE: this driver only handles a single twl4030/tps659x0 chip */
766static int
767twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
768{
769 int status;
770 unsigned i;
771 struct twl4030_platform_data *pdata = client->dev.platform_data;
772
773 if (!pdata) {
774 dev_dbg(&client->dev, "no platform data?\n");
775 return -EINVAL;
776 }
777
778 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
779 dev_dbg(&client->dev, "can't talk I2C?\n");
780 return -EIO;
781 }
782
David Brownella30d46c2008-10-20 23:46:28 +0200783 if (inuse) {
David Brownella603a7f2008-10-15 12:15:39 +0200784 dev_dbg(&client->dev, "driver is already in use\n");
785 return -EBUSY;
786 }
787
788 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
789 struct twl4030_client *twl = &twl4030_modules[i];
790
791 twl->address = client->addr + i;
792 if (i == 0)
793 twl->client = client;
794 else {
795 twl->client = i2c_new_dummy(client->adapter,
796 twl->address);
797 if (!twl->client) {
Ilkka Koskinena8643432009-10-20 16:22:52 +0300798 dev_err(&client->dev,
David Brownella603a7f2008-10-15 12:15:39 +0200799 "can't attach client %d\n", i);
800 status = -ENOMEM;
801 goto fail;
802 }
803 strlcpy(twl->client->name, id->name,
804 sizeof(twl->client->name));
805 }
806 mutex_init(&twl->xfer_lock);
807 }
808 inuse = true;
809
810 /* setup clock framework */
Russell Kinge6b50c82009-01-10 10:40:42 +0000811 clocks_init(&client->dev);
David Brownella603a7f2008-10-15 12:15:39 +0200812
Amit Kucheriaebf0bd32009-08-31 18:32:18 +0200813 /* load power event scripts */
814 if (twl_has_power() && pdata->power)
815 twl4030_power_init(pdata->power);
816
David Brownella603a7f2008-10-15 12:15:39 +0200817 /* Maybe init the T2 Interrupt subsystem */
818 if (client->irq
819 && pdata->irq_base
820 && pdata->irq_end > pdata->irq_base) {
David Brownella30d46c2008-10-20 23:46:28 +0200821 status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
822 if (status < 0)
823 goto fail;
David Brownella603a7f2008-10-15 12:15:39 +0200824 }
825
David Brownelldad759f2008-12-01 00:43:58 +0100826 status = add_children(pdata, id->driver_data);
David Brownella603a7f2008-10-15 12:15:39 +0200827fail:
828 if (status < 0)
829 twl4030_remove(client);
830 return status;
831}
832
833static const struct i2c_device_id twl4030_ids[] = {
David Brownelldad759f2008-12-01 00:43:58 +0100834 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
835 { "twl5030", 0 }, /* T2 updated */
836 { "tps65950", 0 }, /* catalog version of twl5030 */
837 { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
838 { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
David Brownella603a7f2008-10-15 12:15:39 +0200839 { /* end of list */ },
840};
841MODULE_DEVICE_TABLE(i2c, twl4030_ids);
842
843/* One Client Driver , 4 Clients */
844static struct i2c_driver twl4030_driver = {
845 .driver.name = DRIVER_NAME,
846 .id_table = twl4030_ids,
847 .probe = twl4030_probe,
848 .remove = twl4030_remove,
849};
850
851static int __init twl4030_init(void)
852{
853 return i2c_add_driver(&twl4030_driver);
854}
855subsys_initcall(twl4030_init);
856
857static void __exit twl4030_exit(void)
858{
859 i2c_del_driver(&twl4030_driver);
860}
861module_exit(twl4030_exit);
862
863MODULE_AUTHOR("Texas Instruments, Inc.");
864MODULE_DESCRIPTION("I2C Core interface for TWL4030");
865MODULE_LICENSE("GPL");