blob: 1fd2620819d0fffd9c7f06f59f9a017f22b388f5 [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
92#if defined(CONFIG_RTC_DRV_TWL4030) || defined(CONFIG_RTC_DRV_TWL4030_MODULE)
93#define twl_has_rtc() true
94#else
95#define twl_has_rtc() false
96#endif
97
98#if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE)
99#define twl_has_usb() true
100#else
101#define twl_has_usb() false
102#endif
103
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200104#if defined(CONFIG_TWL4030_WATCHDOG) || \
105 defined(CONFIG_TWL4030_WATCHDOG_MODULE)
106#define twl_has_watchdog() true
107#else
108#define twl_has_watchdog() false
109#endif
David Brownella603a7f2008-10-15 12:15:39 +0200110
111/* Triton Core internal information (BEGIN) */
112
113/* Last - for index max*/
114#define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
115
116#define TWL4030_NUM_SLAVES 4
117
Felipe Balbi9c3664d2009-08-03 18:16:38 +0200118#if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
119 || defined(CONFIG_INPUT_TWL4030_PWBUTTON_MODULE)
120#define twl_has_pwrbutton() true
121#else
122#define twl_has_pwrbutton() false
123#endif
David Brownella603a7f2008-10-15 12:15:39 +0200124
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/* subchip/slave 3 - POWER ID */
150#define TWL4030_BASEADD_BACKUP 0x0014
151#define TWL4030_BASEADD_INT 0x002E
152#define TWL4030_BASEADD_PM_MASTER 0x0036
153#define TWL4030_BASEADD_PM_RECEIVER 0x005B
154#define TWL4030_BASEADD_RTC 0x001C
155#define TWL4030_BASEADD_SECURED_REG 0x0000
156
157/* Triton Core internal information (END) */
158
159
160/* Few power values */
161#define R_CFG_BOOT 0x05
162#define R_PROTECT_KEY 0x0E
163
164/* access control values for R_PROTECT_KEY */
165#define KEY_UNLOCK1 0xce
166#define KEY_UNLOCK2 0xec
167#define KEY_LOCK 0x00
168
169/* some fields in R_CFG_BOOT */
170#define HFCLK_FREQ_19p2_MHZ (1 << 0)
171#define HFCLK_FREQ_26_MHZ (2 << 0)
172#define HFCLK_FREQ_38p4_MHZ (3 << 0)
173#define HIGH_PERF_SQ (1 << 3)
174
175
David Brownelldad759f2008-12-01 00:43:58 +0100176/* chip-specific feature flags, for i2c_device_id.driver_data */
177#define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
178#define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
179
David Brownella603a7f2008-10-15 12:15:39 +0200180/*----------------------------------------------------------------------*/
181
David Brownella603a7f2008-10-15 12:15:39 +0200182/* is driver active, bound to a chip? */
183static bool inuse;
184
185/* Structure for each TWL4030 Slave */
186struct twl4030_client {
187 struct i2c_client *client;
188 u8 address;
189
190 /* max numb of i2c_msg required is for read =2 */
191 struct i2c_msg xfer_msg[2];
192
193 /* To lock access to xfer_msg */
194 struct mutex xfer_lock;
195};
196
197static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
198
199
200/* mapping the module id to slave id and base address */
201struct twl4030mapping {
202 unsigned char sid; /* Slave ID */
203 unsigned char base; /* base address */
204};
205
206static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
207 /*
208 * NOTE: don't change this table without updating the
209 * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
210 * so they continue to match the order in this table.
211 */
212
213 { 0, TWL4030_BASEADD_USB },
214
215 { 1, TWL4030_BASEADD_AUDIO_VOICE },
216 { 1, TWL4030_BASEADD_GPIO },
217 { 1, TWL4030_BASEADD_INTBR },
218 { 1, TWL4030_BASEADD_PIH },
219 { 1, TWL4030_BASEADD_TEST },
220
221 { 2, TWL4030_BASEADD_KEYPAD },
222 { 2, TWL4030_BASEADD_MADC },
223 { 2, TWL4030_BASEADD_INTERRUPTS },
224 { 2, TWL4030_BASEADD_LED },
225 { 2, TWL4030_BASEADD_MAIN_CHARGE },
226 { 2, TWL4030_BASEADD_PRECHARGE },
227 { 2, TWL4030_BASEADD_PWM0 },
228 { 2, TWL4030_BASEADD_PWM1 },
229 { 2, TWL4030_BASEADD_PWMA },
230 { 2, TWL4030_BASEADD_PWMB },
231
232 { 3, TWL4030_BASEADD_BACKUP },
233 { 3, TWL4030_BASEADD_INT },
234 { 3, TWL4030_BASEADD_PM_MASTER },
235 { 3, TWL4030_BASEADD_PM_RECEIVER },
236 { 3, TWL4030_BASEADD_RTC },
237 { 3, TWL4030_BASEADD_SECURED_REG },
238};
239
240/*----------------------------------------------------------------------*/
241
David Brownella603a7f2008-10-15 12:15:39 +0200242/* Exported Functions */
243
244/**
245 * twl4030_i2c_write - Writes a n bit register in TWL4030
246 * @mod_no: module number
247 * @value: an array of num_bytes+1 containing data to write
248 * @reg: register address (just offset will do)
249 * @num_bytes: number of bytes to transfer
250 *
251 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
252 * valid data starts at Offset 1.
253 *
254 * Returns the result of operation - 0 is success
255 */
David Brownell3fba19e2008-11-08 01:13:16 +0100256int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200257{
258 int ret;
259 int sid;
260 struct twl4030_client *twl;
261 struct i2c_msg *msg;
262
263 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
264 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
265 return -EPERM;
266 }
267 sid = twl4030_map[mod_no].sid;
268 twl = &twl4030_modules[sid];
269
270 if (unlikely(!inuse)) {
271 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
272 return -EPERM;
273 }
274 mutex_lock(&twl->xfer_lock);
275 /*
276 * [MSG1]: fill the register address data
277 * fill the data Tx buffer
278 */
279 msg = &twl->xfer_msg[0];
280 msg->addr = twl->address;
281 msg->len = num_bytes + 1;
282 msg->flags = 0;
283 msg->buf = value;
284 /* over write the first byte of buffer with the register address */
285 *value = twl4030_map[mod_no].base + reg;
286 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
287 mutex_unlock(&twl->xfer_lock);
288
289 /* i2cTransfer returns num messages.translate it pls.. */
290 if (ret >= 0)
291 ret = 0;
292 return ret;
293}
294EXPORT_SYMBOL(twl4030_i2c_write);
295
296/**
297 * twl4030_i2c_read - Reads a n bit register in TWL4030
298 * @mod_no: module number
299 * @value: an array of num_bytes containing data to be read
300 * @reg: register address (just offset will do)
301 * @num_bytes: number of bytes to transfer
302 *
303 * Returns result of operation - num_bytes is success else failure.
304 */
David Brownell3fba19e2008-11-08 01:13:16 +0100305int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200306{
307 int ret;
308 u8 val;
309 int sid;
310 struct twl4030_client *twl;
311 struct i2c_msg *msg;
312
313 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
314 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
315 return -EPERM;
316 }
317 sid = twl4030_map[mod_no].sid;
318 twl = &twl4030_modules[sid];
319
320 if (unlikely(!inuse)) {
321 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
322 return -EPERM;
323 }
324 mutex_lock(&twl->xfer_lock);
325 /* [MSG1] fill the register address data */
326 msg = &twl->xfer_msg[0];
327 msg->addr = twl->address;
328 msg->len = 1;
329 msg->flags = 0; /* Read the register value */
330 val = twl4030_map[mod_no].base + reg;
331 msg->buf = &val;
332 /* [MSG2] fill the data rx buffer */
333 msg = &twl->xfer_msg[1];
334 msg->addr = twl->address;
335 msg->flags = I2C_M_RD; /* Read the register value */
336 msg->len = num_bytes; /* only n bytes */
337 msg->buf = value;
338 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
339 mutex_unlock(&twl->xfer_lock);
340
341 /* i2cTransfer returns num messages.translate it pls.. */
342 if (ret >= 0)
343 ret = 0;
344 return ret;
345}
346EXPORT_SYMBOL(twl4030_i2c_read);
347
348/**
349 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
350 * @mod_no: module number
351 * @value: the value to be written 8 bit
352 * @reg: register address (just offset will do)
353 *
354 * Returns result of operation - 0 is success
355 */
356int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
357{
358
359 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
360 u8 temp_buffer[2] = { 0 };
361 /* offset 1 contains the data */
362 temp_buffer[1] = value;
363 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
364}
365EXPORT_SYMBOL(twl4030_i2c_write_u8);
366
367/**
368 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
369 * @mod_no: module number
370 * @value: the value read 8 bit
371 * @reg: register address (just offset will do)
372 *
373 * Returns result of operation - 0 is success
374 */
375int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
376{
377 return twl4030_i2c_read(mod_no, value, reg, 1);
378}
379EXPORT_SYMBOL(twl4030_i2c_read_u8);
380
381/*----------------------------------------------------------------------*/
382
David Brownelldad759f2008-12-01 00:43:58 +0100383static struct device *
384add_numbered_child(unsigned chip, const char *name, int num,
David Brownell5725d662008-12-01 00:31:04 +0100385 void *pdata, unsigned pdata_len,
386 bool can_wakeup, int irq0, int irq1)
387{
388 struct platform_device *pdev;
389 struct twl4030_client *twl = &twl4030_modules[chip];
390 int status;
391
David Brownelldad759f2008-12-01 00:43:58 +0100392 pdev = platform_device_alloc(name, num);
David Brownell5725d662008-12-01 00:31:04 +0100393 if (!pdev) {
394 dev_dbg(&twl->client->dev, "can't alloc dev\n");
395 status = -ENOMEM;
396 goto err;
397 }
398
399 device_init_wakeup(&pdev->dev, can_wakeup);
400 pdev->dev.parent = &twl->client->dev;
401
402 if (pdata) {
403 status = platform_device_add_data(pdev, pdata, pdata_len);
404 if (status < 0) {
405 dev_dbg(&pdev->dev, "can't add platform_data\n");
406 goto err;
407 }
408 }
409
410 if (irq0) {
411 struct resource r[2] = {
412 { .start = irq0, .flags = IORESOURCE_IRQ, },
413 { .start = irq1, .flags = IORESOURCE_IRQ, },
414 };
415
416 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
417 if (status < 0) {
418 dev_dbg(&pdev->dev, "can't add irqs\n");
419 goto err;
420 }
421 }
422
423 status = platform_device_add(pdev);
424
425err:
426 if (status < 0) {
427 platform_device_put(pdev);
428 dev_err(&twl->client->dev, "can't add %s dev\n", name);
429 return ERR_PTR(status);
430 }
431 return &pdev->dev;
432}
433
David Brownelldad759f2008-12-01 00:43:58 +0100434static inline struct device *add_child(unsigned chip, const char *name,
435 void *pdata, unsigned pdata_len,
436 bool can_wakeup, int irq0, int irq1)
437{
438 return add_numbered_child(chip, name, -1, pdata, pdata_len,
439 can_wakeup, irq0, irq1);
440}
441
442static struct device *
443add_regulator_linked(int num, struct regulator_init_data *pdata,
444 struct regulator_consumer_supply *consumers,
445 unsigned num_consumers)
446{
447 /* regulator framework demands init_data ... */
448 if (!pdata)
449 return NULL;
450
David Brownellb73eac72008-12-07 19:10:58 +0100451 if (consumers) {
David Brownelldad759f2008-12-01 00:43:58 +0100452 pdata->consumer_supplies = consumers;
453 pdata->num_consumer_supplies = num_consumers;
454 }
455
456 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
457 return add_numbered_child(3, "twl4030_reg", num,
458 pdata, sizeof(*pdata), false, 0, 0);
459}
460
461static struct device *
462add_regulator(int num, struct regulator_init_data *pdata)
463{
464 return add_regulator_linked(num, pdata, NULL, 0);
465}
466
David Brownella603a7f2008-10-15 12:15:39 +0200467/*
David Brownella30d46c2008-10-20 23:46:28 +0200468 * NOTE: We know the first 8 IRQs after pdata->base_irq are
469 * for the PIH, and the next are for the PWR_INT SIH, since
470 * that's how twl_init_irq() sets things up.
David Brownella603a7f2008-10-15 12:15:39 +0200471 */
David Brownella603a7f2008-10-15 12:15:39 +0200472
David Brownelldad759f2008-12-01 00:43:58 +0100473static int
474add_children(struct twl4030_platform_data *pdata, unsigned long features)
David Brownella603a7f2008-10-15 12:15:39 +0200475{
David Brownell5725d662008-12-01 00:31:04 +0100476 struct device *child;
David Brownelldad759f2008-12-01 00:43:58 +0100477 struct device *usb_transceiver = NULL;
David Brownella603a7f2008-10-15 12:15:39 +0200478
David Brownelldad759f2008-12-01 00:43:58 +0100479 if (twl_has_bci() && pdata->bci && !(features & TPS_SUBSET)) {
David Brownell5725d662008-12-01 00:31:04 +0100480 child = add_child(3, "twl4030_bci",
481 pdata->bci, sizeof(*pdata->bci),
482 false,
483 /* irq0 = CHG_PRES, irq1 = BCI */
484 pdata->irq_base + 8 + 1, pdata->irq_base + 2);
485 if (IS_ERR(child))
486 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200487 }
488
489 if (twl_has_gpio() && pdata->gpio) {
David Brownell5725d662008-12-01 00:31:04 +0100490 child = add_child(1, "twl4030_gpio",
491 pdata->gpio, sizeof(*pdata->gpio),
492 false, pdata->irq_base + 0, 0);
493 if (IS_ERR(child))
494 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200495 }
496
497 if (twl_has_keypad() && pdata->keypad) {
David Brownell5725d662008-12-01 00:31:04 +0100498 child = add_child(2, "twl4030_keypad",
499 pdata->keypad, sizeof(*pdata->keypad),
500 true, pdata->irq_base + 1, 0);
501 if (IS_ERR(child))
502 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200503 }
504
505 if (twl_has_madc() && pdata->madc) {
David Brownell5725d662008-12-01 00:31:04 +0100506 child = add_child(2, "twl4030_madc",
507 pdata->madc, sizeof(*pdata->madc),
508 true, pdata->irq_base + 3, 0);
509 if (IS_ERR(child))
510 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200511 }
512
513 if (twl_has_rtc()) {
David Brownella603a7f2008-10-15 12:15:39 +0200514 /*
David Brownell5725d662008-12-01 00:31:04 +0100515 * REVISIT platform_data here currently might expose the
David Brownella603a7f2008-10-15 12:15:39 +0200516 * "msecure" line ... but for now we just expect board
David Brownell5725d662008-12-01 00:31:04 +0100517 * setup to tell the chip "it's always ok to SET_TIME".
David Brownella603a7f2008-10-15 12:15:39 +0200518 * Eventually, Linux might become more aware of such
519 * HW security concerns, and "least privilege".
520 */
David Brownell5725d662008-12-01 00:31:04 +0100521 child = add_child(3, "twl4030_rtc",
522 NULL, 0,
523 true, pdata->irq_base + 8 + 3, 0);
524 if (IS_ERR(child))
525 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200526 }
527
528 if (twl_has_usb() && pdata->usb) {
David Brownell5725d662008-12-01 00:31:04 +0100529 child = add_child(0, "twl4030_usb",
530 pdata->usb, sizeof(*pdata->usb),
531 true,
532 /* irq0 = USB_PRES, irq1 = USB */
533 pdata->irq_base + 8 + 2, pdata->irq_base + 4);
534 if (IS_ERR(child))
535 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100536
537 /* we need to connect regulators to this transceiver */
538 usb_transceiver = child;
539 }
540
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200541 if (twl_has_watchdog()) {
542 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
543 if (IS_ERR(child))
544 return PTR_ERR(child);
545 }
546
Felipe Balbi9c3664d2009-08-03 18:16:38 +0200547 if (twl_has_pwrbutton()) {
548 child = add_child(1, "twl4030_pwrbutton",
549 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
550 if (IS_ERR(child))
551 return PTR_ERR(child);
552 }
553
David Brownelldad759f2008-12-01 00:43:58 +0100554 if (twl_has_regulator()) {
555 /*
556 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
557 if (IS_ERR(child))
558 return PTR_ERR(child);
559 */
560
561 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
562 if (IS_ERR(child))
563 return PTR_ERR(child);
564
565 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
566 if (IS_ERR(child))
567 return PTR_ERR(child);
568
569 child = add_regulator((features & TWL4030_VAUX2)
570 ? TWL4030_REG_VAUX2_4030
571 : TWL4030_REG_VAUX2,
572 pdata->vaux2);
573 if (IS_ERR(child))
574 return PTR_ERR(child);
575 }
576
577 if (twl_has_regulator() && usb_transceiver) {
578 static struct regulator_consumer_supply usb1v5 = {
579 .supply = "usb1v5",
580 };
581 static struct regulator_consumer_supply usb1v8 = {
582 .supply = "usb1v8",
583 };
584 static struct regulator_consumer_supply usb3v1 = {
585 .supply = "usb3v1",
586 };
David Brownelldad759f2008-12-01 00:43:58 +0100587
588 /* this is a template that gets copied */
589 struct regulator_init_data usb_fixed = {
590 .constraints.valid_modes_mask =
591 REGULATOR_MODE_NORMAL
592 | REGULATOR_MODE_STANDBY,
593 .constraints.valid_ops_mask =
594 REGULATOR_CHANGE_MODE
595 | REGULATOR_CHANGE_STATUS,
596 };
597
598 usb1v5.dev = usb_transceiver;
599 usb1v8.dev = usb_transceiver;
600 usb3v1.dev = usb_transceiver;
David Brownelldad759f2008-12-01 00:43:58 +0100601
602 child = add_regulator_linked(TWL4030_REG_VUSB1V5, &usb_fixed,
603 &usb1v5, 1);
604 if (IS_ERR(child))
605 return PTR_ERR(child);
606
607 child = add_regulator_linked(TWL4030_REG_VUSB1V8, &usb_fixed,
608 &usb1v8, 1);
609 if (IS_ERR(child))
610 return PTR_ERR(child);
611
612 child = add_regulator_linked(TWL4030_REG_VUSB3V1, &usb_fixed,
613 &usb3v1, 1);
614 if (IS_ERR(child))
615 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100616 }
617
618 /* maybe add LDOs that are omitted on cost-reduced parts */
619 if (twl_has_regulator() && !(features & TPS_SUBSET)) {
David Brownelldad759f2008-12-01 00:43:58 +0100620 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
621 if (IS_ERR(child))
622 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100623
624 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
625 if (IS_ERR(child))
626 return PTR_ERR(child);
627
628 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
629 if (IS_ERR(child))
630 return PTR_ERR(child);
631
632 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
633 if (IS_ERR(child))
634 return PTR_ERR(child);
635
636 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
637 if (IS_ERR(child))
638 return PTR_ERR(child);
639
640 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
641 if (IS_ERR(child))
642 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200643 }
644
David Brownell5725d662008-12-01 00:31:04 +0100645 return 0;
David Brownella603a7f2008-10-15 12:15:39 +0200646}
647
648/*----------------------------------------------------------------------*/
649
650/*
651 * These three functions initialize the on-chip clock framework,
652 * letting it generate the right frequencies for USB, MADC, and
653 * other purposes.
654 */
655static inline int __init protect_pm_master(void)
656{
657 int e = 0;
658
659 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
660 R_PROTECT_KEY);
661 return e;
662}
663
664static inline int __init unprotect_pm_master(void)
665{
666 int e = 0;
667
668 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
669 R_PROTECT_KEY);
670 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
671 R_PROTECT_KEY);
672 return e;
673}
674
Rakib Mullick04ffa132009-04-19 08:56:49 +0600675static void clocks_init(struct device *dev)
David Brownella603a7f2008-10-15 12:15:39 +0200676{
677 int e = 0;
678 struct clk *osc;
679 u32 rate;
680 u8 ctrl = HFCLK_FREQ_26_MHZ;
681
682#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
683 if (cpu_is_omap2430())
Russell Kinge6b50c82009-01-10 10:40:42 +0000684 osc = clk_get(dev, "osc_ck");
David Brownella603a7f2008-10-15 12:15:39 +0200685 else
Russell Kinge6b50c82009-01-10 10:40:42 +0000686 osc = clk_get(dev, "osc_sys_ck");
Samuel Ortiz6354ab5c2008-12-07 20:50:25 +0100687
David Brownella603a7f2008-10-15 12:15:39 +0200688 if (IS_ERR(osc)) {
689 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
690 "using bootloader value (unknown osc rate)\n");
691 return;
692 }
693
694 rate = clk_get_rate(osc);
695 clk_put(osc);
696
Samuel Ortiz6354ab5c2008-12-07 20:50:25 +0100697#else
698 /* REVISIT for non-OMAP systems, pass the clock rate from
699 * board init code, using platform_data.
700 */
701 osc = ERR_PTR(-EIO);
702
703 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
704 "using bootloader value (unknown osc rate)\n");
705
706 return;
707#endif
708
David Brownella603a7f2008-10-15 12:15:39 +0200709 switch (rate) {
710 case 19200000:
711 ctrl = HFCLK_FREQ_19p2_MHZ;
712 break;
713 case 26000000:
714 ctrl = HFCLK_FREQ_26_MHZ;
715 break;
716 case 38400000:
717 ctrl = HFCLK_FREQ_38p4_MHZ;
718 break;
719 }
720
721 ctrl |= HIGH_PERF_SQ;
722 e |= unprotect_pm_master();
723 /* effect->MADC+USB ck en */
724 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
725 e |= protect_pm_master();
726
727 if (e < 0)
728 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
729}
730
731/*----------------------------------------------------------------------*/
732
David Brownella30d46c2008-10-20 23:46:28 +0200733int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
734int twl_exit_irq(void);
David Brownella603a7f2008-10-15 12:15:39 +0200735
736static int twl4030_remove(struct i2c_client *client)
737{
738 unsigned i;
David Brownella30d46c2008-10-20 23:46:28 +0200739 int status;
David Brownella603a7f2008-10-15 12:15:39 +0200740
David Brownella30d46c2008-10-20 23:46:28 +0200741 status = twl_exit_irq();
742 if (status < 0)
743 return status;
David Brownella603a7f2008-10-15 12:15:39 +0200744
745 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
746 struct twl4030_client *twl = &twl4030_modules[i];
747
748 if (twl->client && twl->client != client)
749 i2c_unregister_device(twl->client);
750 twl4030_modules[i].client = NULL;
751 }
752 inuse = false;
753 return 0;
754}
755
756/* NOTE: this driver only handles a single twl4030/tps659x0 chip */
757static int
758twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
759{
760 int status;
761 unsigned i;
762 struct twl4030_platform_data *pdata = client->dev.platform_data;
763
764 if (!pdata) {
765 dev_dbg(&client->dev, "no platform data?\n");
766 return -EINVAL;
767 }
768
769 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
770 dev_dbg(&client->dev, "can't talk I2C?\n");
771 return -EIO;
772 }
773
David Brownella30d46c2008-10-20 23:46:28 +0200774 if (inuse) {
David Brownella603a7f2008-10-15 12:15:39 +0200775 dev_dbg(&client->dev, "driver is already in use\n");
776 return -EBUSY;
777 }
778
779 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
780 struct twl4030_client *twl = &twl4030_modules[i];
781
782 twl->address = client->addr + i;
783 if (i == 0)
784 twl->client = client;
785 else {
786 twl->client = i2c_new_dummy(client->adapter,
787 twl->address);
788 if (!twl->client) {
789 dev_err(&twl->client->dev,
790 "can't attach client %d\n", i);
791 status = -ENOMEM;
792 goto fail;
793 }
794 strlcpy(twl->client->name, id->name,
795 sizeof(twl->client->name));
796 }
797 mutex_init(&twl->xfer_lock);
798 }
799 inuse = true;
800
801 /* setup clock framework */
Russell Kinge6b50c82009-01-10 10:40:42 +0000802 clocks_init(&client->dev);
David Brownella603a7f2008-10-15 12:15:39 +0200803
804 /* Maybe init the T2 Interrupt subsystem */
805 if (client->irq
806 && pdata->irq_base
807 && pdata->irq_end > pdata->irq_base) {
David Brownella30d46c2008-10-20 23:46:28 +0200808 status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
809 if (status < 0)
810 goto fail;
David Brownella603a7f2008-10-15 12:15:39 +0200811 }
812
David Brownelldad759f2008-12-01 00:43:58 +0100813 status = add_children(pdata, id->driver_data);
David Brownella603a7f2008-10-15 12:15:39 +0200814fail:
815 if (status < 0)
816 twl4030_remove(client);
817 return status;
818}
819
820static const struct i2c_device_id twl4030_ids[] = {
David Brownelldad759f2008-12-01 00:43:58 +0100821 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
822 { "twl5030", 0 }, /* T2 updated */
823 { "tps65950", 0 }, /* catalog version of twl5030 */
824 { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
825 { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
David Brownella603a7f2008-10-15 12:15:39 +0200826 { /* end of list */ },
827};
828MODULE_DEVICE_TABLE(i2c, twl4030_ids);
829
830/* One Client Driver , 4 Clients */
831static struct i2c_driver twl4030_driver = {
832 .driver.name = DRIVER_NAME,
833 .id_table = twl4030_ids,
834 .probe = twl4030_probe,
835 .remove = twl4030_remove,
836};
837
838static int __init twl4030_init(void)
839{
840 return i2c_add_driver(&twl4030_driver);
841}
842subsys_initcall(twl4030_init);
843
844static void __exit twl4030_exit(void)
845{
846 i2c_del_driver(&twl4030_driver);
847}
848module_exit(twl4030_exit);
849
850MODULE_AUTHOR("Texas Instruments, Inc.");
851MODULE_DESCRIPTION("I2C Core interface for TWL4030");
852MODULE_LICENSE("GPL");