mfd: Mask and unmask wm8350 IRQs on request and free
[linux-2.6-block.git] / drivers / mfd / twl4030-core.c
CommitLineData
a603a7fa
DB
1/*
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
a603a7fa
DB
30#include <linux/init.h>
31#include <linux/mutex.h>
a603a7fa
DB
32#include <linux/platform_device.h>
33#include <linux/clk.h>
a30d46c0 34#include <linux/err.h>
a603a7fa 35
dad759ff
DB
36#include <linux/regulator/machine.h>
37
a603a7fa
DB
38#include <linux/i2c.h>
39#include <linux/i2c/twl4030.h>
40
a313d758 41#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
ce491cf8 42#include <plat/cpu.h>
b29c06ae 43#endif
a603a7fa
DB
44
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
dad759ff
DB
79#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
a603a7fa
DB
86#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
ebf0bd36
AK
92#ifdef CONFIG_TWL4030_POWER
93#define twl_has_power() true
94#else
95#define twl_has_power() false
96#endif
97
a603a7fa
DB
98#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
80e45b1e
TK
110#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
a603a7fa 116
0b83ddeb
PU
117#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
a603a7fa
DB
123/* 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
9c3664dd
FB
130#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
a603a7fa
DB
136
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)
38a68496 186#define CK32K_LOWPWR_EN (1 << 7)
a603a7fa
DB
187
188
dad759ff
DB
189/* chip-specific feature flags, for i2c_device_id.driver_data */
190#define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
191#define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
192
a603a7fa
DB
193/*----------------------------------------------------------------------*/
194
a603a7fa
DB
195/* is driver active, bound to a chip? */
196static bool inuse;
197
198/* Structure for each TWL4030 Slave */
199struct twl4030_client {
200 struct i2c_client *client;
201 u8 address;
202
203 /* max numb of i2c_msg required is for read =2 */
204 struct i2c_msg xfer_msg[2];
205
206 /* To lock access to xfer_msg */
207 struct mutex xfer_lock;
208};
209
210static struct twl4030_client twl4030_modules[TWL4030_NUM_SLAVES];
211
212
213/* mapping the module id to slave id and base address */
214struct twl4030mapping {
215 unsigned char sid; /* Slave ID */
216 unsigned char base; /* base address */
217};
218
219static struct twl4030mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
220 /*
221 * NOTE: don't change this table without updating the
222 * <linux/i2c/twl4030.h> defines for TWL4030_MODULE_*
223 * so they continue to match the order in this table.
224 */
225
226 { 0, TWL4030_BASEADD_USB },
227
228 { 1, TWL4030_BASEADD_AUDIO_VOICE },
229 { 1, TWL4030_BASEADD_GPIO },
230 { 1, TWL4030_BASEADD_INTBR },
231 { 1, TWL4030_BASEADD_PIH },
232 { 1, TWL4030_BASEADD_TEST },
233
234 { 2, TWL4030_BASEADD_KEYPAD },
235 { 2, TWL4030_BASEADD_MADC },
236 { 2, TWL4030_BASEADD_INTERRUPTS },
237 { 2, TWL4030_BASEADD_LED },
238 { 2, TWL4030_BASEADD_MAIN_CHARGE },
239 { 2, TWL4030_BASEADD_PRECHARGE },
240 { 2, TWL4030_BASEADD_PWM0 },
241 { 2, TWL4030_BASEADD_PWM1 },
242 { 2, TWL4030_BASEADD_PWMA },
243 { 2, TWL4030_BASEADD_PWMB },
244
245 { 3, TWL4030_BASEADD_BACKUP },
246 { 3, TWL4030_BASEADD_INT },
247 { 3, TWL4030_BASEADD_PM_MASTER },
248 { 3, TWL4030_BASEADD_PM_RECEIVER },
249 { 3, TWL4030_BASEADD_RTC },
250 { 3, TWL4030_BASEADD_SECURED_REG },
251};
252
253/*----------------------------------------------------------------------*/
254
a603a7fa
DB
255/* Exported Functions */
256
257/**
258 * twl4030_i2c_write - Writes a n bit register in TWL4030
259 * @mod_no: module number
260 * @value: an array of num_bytes+1 containing data to write
261 * @reg: register address (just offset will do)
262 * @num_bytes: number of bytes to transfer
263 *
264 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
265 * valid data starts at Offset 1.
266 *
267 * Returns the result of operation - 0 is success
268 */
3fba19ec 269int twl4030_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
a603a7fa
DB
270{
271 int ret;
272 int sid;
273 struct twl4030_client *twl;
274 struct i2c_msg *msg;
275
276 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
277 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
278 return -EPERM;
279 }
280 sid = twl4030_map[mod_no].sid;
281 twl = &twl4030_modules[sid];
282
283 if (unlikely(!inuse)) {
284 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
285 return -EPERM;
286 }
287 mutex_lock(&twl->xfer_lock);
288 /*
289 * [MSG1]: fill the register address data
290 * fill the data Tx buffer
291 */
292 msg = &twl->xfer_msg[0];
293 msg->addr = twl->address;
294 msg->len = num_bytes + 1;
295 msg->flags = 0;
296 msg->buf = value;
297 /* over write the first byte of buffer with the register address */
298 *value = twl4030_map[mod_no].base + reg;
299 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
300 mutex_unlock(&twl->xfer_lock);
301
302 /* i2cTransfer returns num messages.translate it pls.. */
303 if (ret >= 0)
304 ret = 0;
305 return ret;
306}
307EXPORT_SYMBOL(twl4030_i2c_write);
308
309/**
310 * twl4030_i2c_read - Reads a n bit register in TWL4030
311 * @mod_no: module number
312 * @value: an array of num_bytes containing data to be read
313 * @reg: register address (just offset will do)
314 * @num_bytes: number of bytes to transfer
315 *
316 * Returns result of operation - num_bytes is success else failure.
317 */
3fba19ec 318int twl4030_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
a603a7fa
DB
319{
320 int ret;
321 u8 val;
322 int sid;
323 struct twl4030_client *twl;
324 struct i2c_msg *msg;
325
326 if (unlikely(mod_no > TWL4030_MODULE_LAST)) {
327 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
328 return -EPERM;
329 }
330 sid = twl4030_map[mod_no].sid;
331 twl = &twl4030_modules[sid];
332
333 if (unlikely(!inuse)) {
334 pr_err("%s: client %d is not initialized\n", DRIVER_NAME, sid);
335 return -EPERM;
336 }
337 mutex_lock(&twl->xfer_lock);
338 /* [MSG1] fill the register address data */
339 msg = &twl->xfer_msg[0];
340 msg->addr = twl->address;
341 msg->len = 1;
342 msg->flags = 0; /* Read the register value */
343 val = twl4030_map[mod_no].base + reg;
344 msg->buf = &val;
345 /* [MSG2] fill the data rx buffer */
346 msg = &twl->xfer_msg[1];
347 msg->addr = twl->address;
348 msg->flags = I2C_M_RD; /* Read the register value */
349 msg->len = num_bytes; /* only n bytes */
350 msg->buf = value;
351 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
352 mutex_unlock(&twl->xfer_lock);
353
354 /* i2cTransfer returns num messages.translate it pls.. */
355 if (ret >= 0)
356 ret = 0;
357 return ret;
358}
359EXPORT_SYMBOL(twl4030_i2c_read);
360
361/**
362 * twl4030_i2c_write_u8 - Writes a 8 bit register in TWL4030
363 * @mod_no: module number
364 * @value: the value to be written 8 bit
365 * @reg: register address (just offset will do)
366 *
367 * Returns result of operation - 0 is success
368 */
369int twl4030_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
370{
371
372 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
373 u8 temp_buffer[2] = { 0 };
374 /* offset 1 contains the data */
375 temp_buffer[1] = value;
376 return twl4030_i2c_write(mod_no, temp_buffer, reg, 1);
377}
378EXPORT_SYMBOL(twl4030_i2c_write_u8);
379
380/**
381 * twl4030_i2c_read_u8 - Reads a 8 bit register from TWL4030
382 * @mod_no: module number
383 * @value: the value read 8 bit
384 * @reg: register address (just offset will do)
385 *
386 * Returns result of operation - 0 is success
387 */
388int twl4030_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
389{
390 return twl4030_i2c_read(mod_no, value, reg, 1);
391}
392EXPORT_SYMBOL(twl4030_i2c_read_u8);
393
394/*----------------------------------------------------------------------*/
395
dad759ff
DB
396static struct device *
397add_numbered_child(unsigned chip, const char *name, int num,
5725d66b
DB
398 void *pdata, unsigned pdata_len,
399 bool can_wakeup, int irq0, int irq1)
a603a7fa 400{
5725d66b
DB
401 struct platform_device *pdev;
402 struct twl4030_client *twl = &twl4030_modules[chip];
403 int status;
404
dad759ff 405 pdev = platform_device_alloc(name, num);
5725d66b
DB
406 if (!pdev) {
407 dev_dbg(&twl->client->dev, "can't alloc dev\n");
408 status = -ENOMEM;
409 goto err;
410 }
a603a7fa 411
5725d66b
DB
412 device_init_wakeup(&pdev->dev, can_wakeup);
413 pdev->dev.parent = &twl->client->dev;
a603a7fa 414
5725d66b
DB
415 if (pdata) {
416 status = platform_device_add_data(pdev, pdata, pdata_len);
417 if (status < 0) {
418 dev_dbg(&pdev->dev, "can't add platform_data\n");
a603a7fa
DB
419 goto err;
420 }
5725d66b 421 }
a603a7fa 422
5725d66b
DB
423 if (irq0) {
424 struct resource r[2] = {
425 { .start = irq0, .flags = IORESOURCE_IRQ, },
426 { .start = irq1, .flags = IORESOURCE_IRQ, },
427 };
a603a7fa 428
5725d66b 429 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
a603a7fa 430 if (status < 0) {
5725d66b 431 dev_dbg(&pdev->dev, "can't add irqs\n");
a603a7fa
DB
432 goto err;
433 }
434 }
435
5725d66b 436 status = platform_device_add(pdev);
a603a7fa 437
5725d66b
DB
438err:
439 if (status < 0) {
440 platform_device_put(pdev);
441 dev_err(&twl->client->dev, "can't add %s dev\n", name);
442 return ERR_PTR(status);
443 }
444 return &pdev->dev;
445}
a603a7fa 446
dad759ff
DB
447static inline struct device *add_child(unsigned chip, const char *name,
448 void *pdata, unsigned pdata_len,
449 bool can_wakeup, int irq0, int irq1)
450{
451 return add_numbered_child(chip, name, -1, pdata, pdata_len,
452 can_wakeup, irq0, irq1);
453}
454
455static struct device *
456add_regulator_linked(int num, struct regulator_init_data *pdata,
457 struct regulator_consumer_supply *consumers,
458 unsigned num_consumers)
459{
460 /* regulator framework demands init_data ... */
461 if (!pdata)
462 return NULL;
463
b73eac78 464 if (consumers) {
dad759ff
DB
465 pdata->consumer_supplies = consumers;
466 pdata->num_consumer_supplies = num_consumers;
467 }
468
469 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
470 return add_numbered_child(3, "twl4030_reg", num,
471 pdata, sizeof(*pdata), false, 0, 0);
472}
473
474static struct device *
475add_regulator(int num, struct regulator_init_data *pdata)
476{
477 return add_regulator_linked(num, pdata, NULL, 0);
478}
479
5725d66b
DB
480/*
481 * NOTE: We know the first 8 IRQs after pdata->base_irq are
482 * for the PIH, and the next are for the PWR_INT SIH, since
483 * that's how twl_init_irq() sets things up.
484 */
a603a7fa 485
dad759ff
DB
486static int
487add_children(struct twl4030_platform_data *pdata, unsigned long features)
5725d66b
DB
488{
489 struct device *child;
a603a7fa 490
dad759ff 491 if (twl_has_bci() && pdata->bci && !(features & TPS_SUBSET)) {
5725d66b
DB
492 child = add_child(3, "twl4030_bci",
493 pdata->bci, sizeof(*pdata->bci),
494 false,
495 /* irq0 = CHG_PRES, irq1 = BCI */
496 pdata->irq_base + 8 + 1, pdata->irq_base + 2);
497 if (IS_ERR(child))
498 return PTR_ERR(child);
499 }
a603a7fa 500
5725d66b
DB
501 if (twl_has_gpio() && pdata->gpio) {
502 child = add_child(1, "twl4030_gpio",
503 pdata->gpio, sizeof(*pdata->gpio),
504 false, pdata->irq_base + 0, 0);
505 if (IS_ERR(child))
506 return PTR_ERR(child);
a603a7fa
DB
507 }
508
509 if (twl_has_keypad() && pdata->keypad) {
5725d66b
DB
510 child = add_child(2, "twl4030_keypad",
511 pdata->keypad, sizeof(*pdata->keypad),
512 true, pdata->irq_base + 1, 0);
513 if (IS_ERR(child))
514 return PTR_ERR(child);
a603a7fa
DB
515 }
516
517 if (twl_has_madc() && pdata->madc) {
5725d66b
DB
518 child = add_child(2, "twl4030_madc",
519 pdata->madc, sizeof(*pdata->madc),
520 true, pdata->irq_base + 3, 0);
521 if (IS_ERR(child))
522 return PTR_ERR(child);
a603a7fa
DB
523 }
524
525 if (twl_has_rtc()) {
a603a7fa 526 /*
5725d66b 527 * REVISIT platform_data here currently might expose the
a603a7fa 528 * "msecure" line ... but for now we just expect board
5725d66b 529 * setup to tell the chip "it's always ok to SET_TIME".
a603a7fa
DB
530 * Eventually, Linux might become more aware of such
531 * HW security concerns, and "least privilege".
532 */
5725d66b
DB
533 child = add_child(3, "twl4030_rtc",
534 NULL, 0,
535 true, pdata->irq_base + 8 + 3, 0);
536 if (IS_ERR(child))
537 return PTR_ERR(child);
a603a7fa
DB
538 }
539
540 if (twl_has_usb() && pdata->usb) {
f8ebdff0
RQ
541
542 static struct regulator_consumer_supply usb1v5 = {
543 .supply = "usb1v5",
544 };
545 static struct regulator_consumer_supply usb1v8 = {
546 .supply = "usb1v8",
547 };
548 static struct regulator_consumer_supply usb3v1 = {
549 .supply = "usb3v1",
550 };
551
552 /* First add the regulators so that they can be used by transceiver */
553 if (twl_has_regulator()) {
554 /* this is a template that gets copied */
555 struct regulator_init_data usb_fixed = {
556 .constraints.valid_modes_mask =
557 REGULATOR_MODE_NORMAL
558 | REGULATOR_MODE_STANDBY,
559 .constraints.valid_ops_mask =
560 REGULATOR_CHANGE_MODE
561 | REGULATOR_CHANGE_STATUS,
562 };
563
564 child = add_regulator_linked(TWL4030_REG_VUSB1V5,
565 &usb_fixed, &usb1v5, 1);
566 if (IS_ERR(child))
567 return PTR_ERR(child);
568
569 child = add_regulator_linked(TWL4030_REG_VUSB1V8,
570 &usb_fixed, &usb1v8, 1);
571 if (IS_ERR(child))
572 return PTR_ERR(child);
573
574 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
575 &usb_fixed, &usb3v1, 1);
576 if (IS_ERR(child))
577 return PTR_ERR(child);
578
579 }
580
5725d66b
DB
581 child = add_child(0, "twl4030_usb",
582 pdata->usb, sizeof(*pdata->usb),
583 true,
584 /* irq0 = USB_PRES, irq1 = USB */
585 pdata->irq_base + 8 + 2, pdata->irq_base + 4);
f8ebdff0 586
5725d66b
DB
587 if (IS_ERR(child))
588 return PTR_ERR(child);
dad759ff
DB
589
590 /* we need to connect regulators to this transceiver */
f8ebdff0
RQ
591 if (twl_has_regulator() && child) {
592 usb1v5.dev = child;
593 usb1v8.dev = child;
594 usb3v1.dev = child;
595 }
dad759ff
DB
596 }
597
80e45b1e
TK
598 if (twl_has_watchdog()) {
599 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
600 if (IS_ERR(child))
9c3664dd
FB
601 return PTR_ERR(child);
602 }
603
604 if (twl_has_pwrbutton()) {
605 child = add_child(1, "twl4030_pwrbutton",
606 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
607 if (IS_ERR(child))
80e45b1e
TK
608 return PTR_ERR(child);
609 }
610
0b83ddeb
PU
611 if (twl_has_codec() && pdata->codec) {
612 child = add_child(1, "twl4030_codec",
613 pdata->codec, sizeof(*pdata->codec),
614 false, 0, 0);
615 if (IS_ERR(child))
616 return PTR_ERR(child);
617 }
618
dad759ff
DB
619 if (twl_has_regulator()) {
620 /*
621 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1);
622 if (IS_ERR(child))
623 return PTR_ERR(child);
624 */
625
626 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1);
627 if (IS_ERR(child))
628 return PTR_ERR(child);
629
630 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac);
631 if (IS_ERR(child))
632 return PTR_ERR(child);
633
634 child = add_regulator((features & TWL4030_VAUX2)
635 ? TWL4030_REG_VAUX2_4030
636 : TWL4030_REG_VAUX2,
637 pdata->vaux2);
638 if (IS_ERR(child))
639 return PTR_ERR(child);
640 }
641
dad759ff
DB
642 /* maybe add LDOs that are omitted on cost-reduced parts */
643 if (twl_has_regulator() && !(features & TPS_SUBSET)) {
dad759ff
DB
644 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2);
645 if (IS_ERR(child))
646 return PTR_ERR(child);
dad759ff
DB
647
648 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2);
649 if (IS_ERR(child))
650 return PTR_ERR(child);
651
652 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim);
653 if (IS_ERR(child))
654 return PTR_ERR(child);
655
656 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1);
657 if (IS_ERR(child))
658 return PTR_ERR(child);
659
660 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3);
661 if (IS_ERR(child))
662 return PTR_ERR(child);
663
664 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4);
665 if (IS_ERR(child))
666 return PTR_ERR(child);
a603a7fa
DB
667 }
668
5725d66b 669 return 0;
a603a7fa
DB
670}
671
672/*----------------------------------------------------------------------*/
673
674/*
675 * These three functions initialize the on-chip clock framework,
676 * letting it generate the right frequencies for USB, MADC, and
677 * other purposes.
678 */
679static inline int __init protect_pm_master(void)
680{
681 int e = 0;
682
683 e = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_LOCK,
684 R_PROTECT_KEY);
685 return e;
686}
687
688static inline int __init unprotect_pm_master(void)
689{
690 int e = 0;
691
692 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK1,
693 R_PROTECT_KEY);
694 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, KEY_UNLOCK2,
695 R_PROTECT_KEY);
696 return e;
697}
698
38a68496
IK
699static void clocks_init(struct device *dev,
700 struct twl4030_clock_init_data *clock)
a603a7fa
DB
701{
702 int e = 0;
703 struct clk *osc;
704 u32 rate;
705 u8 ctrl = HFCLK_FREQ_26_MHZ;
706
707#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
708 if (cpu_is_omap2430())
e6b50c8d 709 osc = clk_get(dev, "osc_ck");
a603a7fa 710 else
e6b50c8d 711 osc = clk_get(dev, "osc_sys_ck");
6354ab5c 712
a603a7fa
DB
713 if (IS_ERR(osc)) {
714 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
715 "using bootloader value (unknown osc rate)\n");
716 return;
717 }
718
719 rate = clk_get_rate(osc);
720 clk_put(osc);
721
6354ab5c
SO
722#else
723 /* REVISIT for non-OMAP systems, pass the clock rate from
724 * board init code, using platform_data.
725 */
726 osc = ERR_PTR(-EIO);
727
728 printk(KERN_WARNING "Skipping twl4030 internal clock init and "
729 "using bootloader value (unknown osc rate)\n");
730
731 return;
732#endif
733
a603a7fa
DB
734 switch (rate) {
735 case 19200000:
736 ctrl = HFCLK_FREQ_19p2_MHZ;
737 break;
738 case 26000000:
739 ctrl = HFCLK_FREQ_26_MHZ;
740 break;
741 case 38400000:
742 ctrl = HFCLK_FREQ_38p4_MHZ;
743 break;
744 }
745
746 ctrl |= HIGH_PERF_SQ;
38a68496
IK
747 if (clock && clock->ck32k_lowpwr_enable)
748 ctrl |= CK32K_LOWPWR_EN;
749
a603a7fa
DB
750 e |= unprotect_pm_master();
751 /* effect->MADC+USB ck en */
752 e |= twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
753 e |= protect_pm_master();
754
755 if (e < 0)
756 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
757}
758
759/*----------------------------------------------------------------------*/
760
a30d46c0
DB
761int twl_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
762int twl_exit_irq(void);
a603a7fa
DB
763
764static int twl4030_remove(struct i2c_client *client)
765{
766 unsigned i;
a30d46c0 767 int status;
a603a7fa 768
a30d46c0
DB
769 status = twl_exit_irq();
770 if (status < 0)
771 return status;
a603a7fa
DB
772
773 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
774 struct twl4030_client *twl = &twl4030_modules[i];
775
776 if (twl->client && twl->client != client)
777 i2c_unregister_device(twl->client);
778 twl4030_modules[i].client = NULL;
779 }
780 inuse = false;
781 return 0;
782}
783
784/* NOTE: this driver only handles a single twl4030/tps659x0 chip */
b5e102cd 785static int __init
a603a7fa
DB
786twl4030_probe(struct i2c_client *client, const struct i2c_device_id *id)
787{
788 int status;
789 unsigned i;
790 struct twl4030_platform_data *pdata = client->dev.platform_data;
791
792 if (!pdata) {
793 dev_dbg(&client->dev, "no platform data?\n");
794 return -EINVAL;
795 }
796
797 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
798 dev_dbg(&client->dev, "can't talk I2C?\n");
799 return -EIO;
800 }
801
a30d46c0 802 if (inuse) {
a603a7fa
DB
803 dev_dbg(&client->dev, "driver is already in use\n");
804 return -EBUSY;
805 }
806
807 for (i = 0; i < TWL4030_NUM_SLAVES; i++) {
808 struct twl4030_client *twl = &twl4030_modules[i];
809
810 twl->address = client->addr + i;
811 if (i == 0)
812 twl->client = client;
813 else {
814 twl->client = i2c_new_dummy(client->adapter,
815 twl->address);
816 if (!twl->client) {
a8643430 817 dev_err(&client->dev,
a603a7fa
DB
818 "can't attach client %d\n", i);
819 status = -ENOMEM;
820 goto fail;
821 }
a603a7fa
DB
822 }
823 mutex_init(&twl->xfer_lock);
824 }
825 inuse = true;
826
827 /* setup clock framework */
38a68496 828 clocks_init(&client->dev, pdata->clock);
a603a7fa 829
ebf0bd36
AK
830 /* load power event scripts */
831 if (twl_has_power() && pdata->power)
832 twl4030_power_init(pdata->power);
833
a603a7fa
DB
834 /* Maybe init the T2 Interrupt subsystem */
835 if (client->irq
836 && pdata->irq_base
837 && pdata->irq_end > pdata->irq_base) {
a30d46c0
DB
838 status = twl_init_irq(client->irq, pdata->irq_base, pdata->irq_end);
839 if (status < 0)
840 goto fail;
a603a7fa
DB
841 }
842
dad759ff 843 status = add_children(pdata, id->driver_data);
a603a7fa
DB
844fail:
845 if (status < 0)
846 twl4030_remove(client);
847 return status;
848}
849
850static const struct i2c_device_id twl4030_ids[] = {
dad759ff
DB
851 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
852 { "twl5030", 0 }, /* T2 updated */
853 { "tps65950", 0 }, /* catalog version of twl5030 */
854 { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
855 { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
a603a7fa
DB
856 { /* end of list */ },
857};
858MODULE_DEVICE_TABLE(i2c, twl4030_ids);
859
860/* One Client Driver , 4 Clients */
861static struct i2c_driver twl4030_driver = {
862 .driver.name = DRIVER_NAME,
863 .id_table = twl4030_ids,
864 .probe = twl4030_probe,
865 .remove = twl4030_remove,
866};
867
868static int __init twl4030_init(void)
869{
870 return i2c_add_driver(&twl4030_driver);
871}
872subsys_initcall(twl4030_init);
873
874static void __exit twl4030_exit(void)
875{
876 i2c_del_driver(&twl4030_driver);
877}
878module_exit(twl4030_exit);
879
880MODULE_AUTHOR("Texas Instruments, Inc.");
881MODULE_DESCRIPTION("I2C Core interface for TWL4030");
882MODULE_LICENSE("GPL");