]> Git Repo - linux.git/blame - drivers/mfd/twl-core.c
Merge branch 'x86/cleanups' into perf/uprobes
[linux.git] / drivers / mfd / twl-core.c
CommitLineData
a603a7fa 1/*
fc7b92fc
B
2 * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
3 * and audio CODEC devices
a603a7fa
DB
4 *
5 * Copyright (C) 2005-2006 Texas Instruments, Inc.
6 *
7 * Modifications to defer interrupt handling to a kernel thread:
8 * Copyright (C) 2006 MontaVista Software, Inc.
9 *
10 * Based on tlv320aic23.c:
11 * Copyright (c) by Kai Svahn <[email protected]>
12 *
13 * Code cleanup and modifications to IRQ handler.
14 * by syed khasim <[email protected]>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
a603a7fa
DB
31#include <linux/init.h>
32#include <linux/mutex.h>
4e36dd33 33#include <linux/module.h>
a603a7fa
DB
34#include <linux/platform_device.h>
35#include <linux/clk.h>
a30d46c0 36#include <linux/err.h>
aeb5032b
BC
37#include <linux/device.h>
38#include <linux/of.h>
39#include <linux/of_irq.h>
40#include <linux/of_platform.h>
41#include <linux/irqdomain.h>
a603a7fa 42
dad759ff
DB
43#include <linux/regulator/machine.h>
44
a603a7fa 45#include <linux/i2c.h>
b07682b6 46#include <linux/i2c/twl.h>
a603a7fa 47
a313d758 48#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
ce491cf8 49#include <plat/cpu.h>
b29c06ae 50#endif
a603a7fa
DB
51
52/*
53 * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
54 * Management and System Companion Device" chips originally designed for
55 * use in OMAP2 and OMAP 3 based systems. Its control interfaces use I2C,
56 * often at around 3 Mbit/sec, including for interrupt handling.
57 *
58 * This driver core provides genirq support for the interrupts emitted,
59 * by the various modules, and exports register access primitives.
60 *
61 * FIXME this driver currently requires use of the first interrupt line
62 * (and associated registers).
63 */
64
fc7b92fc 65#define DRIVER_NAME "twl"
a603a7fa 66
a603a7fa
DB
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
e70357e3
HH
104#if defined(CONFIG_TWL4030_USB) || defined(CONFIG_TWL4030_USB_MODULE) ||\
105 defined(CONFIG_TWL6030_USB) || defined(CONFIG_TWL6030_USB_MODULE)
a603a7fa
DB
106#define twl_has_usb() true
107#else
108#define twl_has_usb() false
109#endif
110
80e45b1e
TK
111#if defined(CONFIG_TWL4030_WATCHDOG) || \
112 defined(CONFIG_TWL4030_WATCHDOG_MODULE)
113#define twl_has_watchdog() true
114#else
115#define twl_has_watchdog() false
116#endif
a603a7fa 117
f09ee045 118#if defined(CONFIG_MFD_TWL4030_AUDIO) || defined(CONFIG_MFD_TWL4030_AUDIO_MODULE) ||\
f19b2823 119 defined(CONFIG_TWL6040_CORE) || defined(CONFIG_TWL6040_CORE_MODULE)
0b83ddeb
PU
120#define twl_has_codec() true
121#else
122#define twl_has_codec() false
123#endif
124
11c39c4b
GI
125#if defined(CONFIG_CHARGER_TWL4030) || defined(CONFIG_CHARGER_TWL4030_MODULE)
126#define twl_has_bci() true
127#else
128#define twl_has_bci() false
129#endif
130
a603a7fa
DB
131/* Triton Core internal information (BEGIN) */
132
133/* Last - for index max*/
134#define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
135
fc7b92fc 136#define TWL_NUM_SLAVES 4
a603a7fa 137
9c3664dd 138#if defined(CONFIG_INPUT_TWL4030_PWRBUTTON) \
14e5c82c 139 || defined(CONFIG_INPUT_TWL4030_PWRBUTTON_MODULE)
9c3664dd
FB
140#define twl_has_pwrbutton() true
141#else
142#define twl_has_pwrbutton() false
143#endif
a603a7fa 144
fc7b92fc
B
145#define SUB_CHIP_ID0 0
146#define SUB_CHIP_ID1 1
147#define SUB_CHIP_ID2 2
148#define SUB_CHIP_ID3 3
149
150#define TWL_MODULE_LAST TWL4030_MODULE_LAST
151
aeb5032b
BC
152#define TWL4030_NR_IRQS 8
153#define TWL6030_NR_IRQS 20
154
a603a7fa
DB
155/* Base Address defns for twl4030_map[] */
156
157/* subchip/slave 0 - USB ID */
158#define TWL4030_BASEADD_USB 0x0000
159
160/* subchip/slave 1 - AUD ID */
161#define TWL4030_BASEADD_AUDIO_VOICE 0x0000
162#define TWL4030_BASEADD_GPIO 0x0098
163#define TWL4030_BASEADD_INTBR 0x0085
164#define TWL4030_BASEADD_PIH 0x0080
165#define TWL4030_BASEADD_TEST 0x004C
166
167/* subchip/slave 2 - AUX ID */
168#define TWL4030_BASEADD_INTERRUPTS 0x00B9
169#define TWL4030_BASEADD_LED 0x00EE
170#define TWL4030_BASEADD_MADC 0x0000
171#define TWL4030_BASEADD_MAIN_CHARGE 0x0074
172#define TWL4030_BASEADD_PRECHARGE 0x00AA
173#define TWL4030_BASEADD_PWM0 0x00F8
174#define TWL4030_BASEADD_PWM1 0x00FB
175#define TWL4030_BASEADD_PWMA 0x00EF
176#define TWL4030_BASEADD_PWMB 0x00F1
177#define TWL4030_BASEADD_KEYPAD 0x00D2
178
1920a61e
IK
179#define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
180#define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
181 one */
182
a603a7fa
DB
183/* subchip/slave 3 - POWER ID */
184#define TWL4030_BASEADD_BACKUP 0x0014
185#define TWL4030_BASEADD_INT 0x002E
186#define TWL4030_BASEADD_PM_MASTER 0x0036
187#define TWL4030_BASEADD_PM_RECEIVER 0x005B
188#define TWL4030_BASEADD_RTC 0x001C
189#define TWL4030_BASEADD_SECURED_REG 0x0000
190
191/* Triton Core internal information (END) */
192
193
e8deb28c
B
194/* subchip/slave 0 0x48 - POWER */
195#define TWL6030_BASEADD_RTC 0x0000
196#define TWL6030_BASEADD_MEM 0x0017
197#define TWL6030_BASEADD_PM_MASTER 0x001F
198#define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
199#define TWL6030_BASEADD_PM_MISC 0x00E2
200#define TWL6030_BASEADD_PM_PUPD 0x00F0
201
202/* subchip/slave 1 0x49 - FEATURE */
203#define TWL6030_BASEADD_USB 0x0000
204#define TWL6030_BASEADD_GPADC_CTRL 0x002E
205#define TWL6030_BASEADD_AUX 0x0090
206#define TWL6030_BASEADD_PWM 0x00BA
207#define TWL6030_BASEADD_GASGAUGE 0x00C0
208#define TWL6030_BASEADD_PIH 0x00D0
209#define TWL6030_BASEADD_CHARGER 0x00E0
521d8ec3 210#define TWL6025_BASEADD_CHARGER 0x00DA
e8deb28c
B
211
212/* subchip/slave 2 0x4A - DFT */
213#define TWL6030_BASEADD_DIEID 0x00C0
214
215/* subchip/slave 3 0x4B - AUDIO */
216#define TWL6030_BASEADD_AUDIO 0x0000
217#define TWL6030_BASEADD_RSV 0x0000
fa0d9762 218#define TWL6030_BASEADD_ZERO 0x0000
e8deb28c 219
a603a7fa
DB
220/* Few power values */
221#define R_CFG_BOOT 0x05
a603a7fa
DB
222
223/* some fields in R_CFG_BOOT */
224#define HFCLK_FREQ_19p2_MHZ (1 << 0)
225#define HFCLK_FREQ_26_MHZ (2 << 0)
226#define HFCLK_FREQ_38p4_MHZ (3 << 0)
227#define HIGH_PERF_SQ (1 << 3)
38a68496 228#define CK32K_LOWPWR_EN (1 << 7)
a603a7fa
DB
229
230
dad759ff
DB
231/* chip-specific feature flags, for i2c_device_id.driver_data */
232#define TWL4030_VAUX2 BIT(0) /* pre-5030 voltage ranges */
233#define TPS_SUBSET BIT(1) /* tps659[23]0 have fewer LDOs */
1920a61e 234#define TWL5031 BIT(2) /* twl5031 has different registers */
e8deb28c 235#define TWL6030_CLASS BIT(3) /* TWL6030 class */
dad759ff 236
a603a7fa
DB
237/*----------------------------------------------------------------------*/
238
a603a7fa
DB
239/* is driver active, bound to a chip? */
240static bool inuse;
241
ca972d13
L
242/* TWL IDCODE Register value */
243static u32 twl_idcode;
244
e8deb28c
B
245static unsigned int twl_id;
246unsigned int twl_rev(void)
247{
248 return twl_id;
249}
250EXPORT_SYMBOL(twl_rev);
251
252/* Structure for each TWL4030/TWL6030 Slave */
fc7b92fc 253struct twl_client {
a603a7fa
DB
254 struct i2c_client *client;
255 u8 address;
256
257 /* max numb of i2c_msg required is for read =2 */
258 struct i2c_msg xfer_msg[2];
259
260 /* To lock access to xfer_msg */
261 struct mutex xfer_lock;
262};
263
fc7b92fc 264static struct twl_client twl_modules[TWL_NUM_SLAVES];
a603a7fa 265
6252547b 266#ifdef CONFIG_IRQ_DOMAIN
aeb5032b 267static struct irq_domain domain;
6252547b 268#endif
a603a7fa
DB
269
270/* mapping the module id to slave id and base address */
fc7b92fc 271struct twl_mapping {
a603a7fa
DB
272 unsigned char sid; /* Slave ID */
273 unsigned char base; /* base address */
274};
2cfcce18 275static struct twl_mapping *twl_map;
a603a7fa 276
fc7b92fc 277static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
a603a7fa
DB
278 /*
279 * NOTE: don't change this table without updating the
e8deb28c 280 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
a603a7fa
DB
281 * so they continue to match the order in this table.
282 */
283
284 { 0, TWL4030_BASEADD_USB },
285
286 { 1, TWL4030_BASEADD_AUDIO_VOICE },
287 { 1, TWL4030_BASEADD_GPIO },
288 { 1, TWL4030_BASEADD_INTBR },
289 { 1, TWL4030_BASEADD_PIH },
290 { 1, TWL4030_BASEADD_TEST },
291
292 { 2, TWL4030_BASEADD_KEYPAD },
293 { 2, TWL4030_BASEADD_MADC },
294 { 2, TWL4030_BASEADD_INTERRUPTS },
295 { 2, TWL4030_BASEADD_LED },
296 { 2, TWL4030_BASEADD_MAIN_CHARGE },
297 { 2, TWL4030_BASEADD_PRECHARGE },
298 { 2, TWL4030_BASEADD_PWM0 },
299 { 2, TWL4030_BASEADD_PWM1 },
300 { 2, TWL4030_BASEADD_PWMA },
301 { 2, TWL4030_BASEADD_PWMB },
1920a61e
IK
302 { 2, TWL5031_BASEADD_ACCESSORY },
303 { 2, TWL5031_BASEADD_INTERRUPTS },
a603a7fa
DB
304
305 { 3, TWL4030_BASEADD_BACKUP },
306 { 3, TWL4030_BASEADD_INT },
307 { 3, TWL4030_BASEADD_PM_MASTER },
308 { 3, TWL4030_BASEADD_PM_RECEIVER },
309 { 3, TWL4030_BASEADD_RTC },
310 { 3, TWL4030_BASEADD_SECURED_REG },
311};
312
e8deb28c
B
313static struct twl_mapping twl6030_map[] = {
314 /*
315 * NOTE: don't change this table without updating the
316 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
317 * so they continue to match the order in this table.
318 */
319 { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
320 { SUB_CHIP_ID3, TWL6030_BASEADD_AUDIO },
321 { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
322 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
323 { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
324
325 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
326 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
327 { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
328 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
329 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
330
331 { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
332 { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
333 { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
fa0d9762
B
334 { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
335 { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
e8deb28c 336
fa0d9762
B
337 { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
338 { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
e8deb28c
B
339 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
340 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
341 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
342 { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
343 { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
344
345 { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
346 { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
521d8ec3 347 { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
e8deb28c
B
348};
349
a603a7fa
DB
350/*----------------------------------------------------------------------*/
351
a603a7fa
DB
352/* Exported Functions */
353
354/**
fc7b92fc 355 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
a603a7fa
DB
356 * @mod_no: module number
357 * @value: an array of num_bytes+1 containing data to write
358 * @reg: register address (just offset will do)
359 * @num_bytes: number of bytes to transfer
360 *
361 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
362 * valid data starts at Offset 1.
363 *
364 * Returns the result of operation - 0 is success
365 */
fc7b92fc 366int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
a603a7fa
DB
367{
368 int ret;
369 int sid;
fc7b92fc 370 struct twl_client *twl;
a603a7fa
DB
371 struct i2c_msg *msg;
372
fc7b92fc 373 if (unlikely(mod_no > TWL_MODULE_LAST)) {
a603a7fa
DB
374 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
375 return -EPERM;
376 }
a603a7fa 377 if (unlikely(!inuse)) {
8653be1a 378 pr_err("%s: not initialized\n", DRIVER_NAME);
a603a7fa
DB
379 return -EPERM;
380 }
8653be1a
IY
381 sid = twl_map[mod_no].sid;
382 twl = &twl_modules[sid];
383
a603a7fa
DB
384 mutex_lock(&twl->xfer_lock);
385 /*
386 * [MSG1]: fill the register address data
387 * fill the data Tx buffer
388 */
389 msg = &twl->xfer_msg[0];
390 msg->addr = twl->address;
391 msg->len = num_bytes + 1;
392 msg->flags = 0;
393 msg->buf = value;
394 /* over write the first byte of buffer with the register address */
e8deb28c 395 *value = twl_map[mod_no].base + reg;
a603a7fa
DB
396 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
397 mutex_unlock(&twl->xfer_lock);
398
147e0847
AK
399 /* i2c_transfer returns number of messages transferred */
400 if (ret != 1) {
401 pr_err("%s: i2c_write failed to transfer all messages\n",
402 DRIVER_NAME);
403 if (ret < 0)
404 return ret;
405 else
406 return -EIO;
407 } else {
408 return 0;
409 }
a603a7fa 410}
fc7b92fc 411EXPORT_SYMBOL(twl_i2c_write);
a603a7fa
DB
412
413/**
fc7b92fc 414 * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
a603a7fa
DB
415 * @mod_no: module number
416 * @value: an array of num_bytes containing data to be read
417 * @reg: register address (just offset will do)
418 * @num_bytes: number of bytes to transfer
419 *
420 * Returns result of operation - num_bytes is success else failure.
421 */
fc7b92fc 422int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
a603a7fa
DB
423{
424 int ret;
425 u8 val;
426 int sid;
fc7b92fc 427 struct twl_client *twl;
a603a7fa
DB
428 struct i2c_msg *msg;
429
fc7b92fc 430 if (unlikely(mod_no > TWL_MODULE_LAST)) {
a603a7fa
DB
431 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
432 return -EPERM;
433 }
a603a7fa 434 if (unlikely(!inuse)) {
8653be1a 435 pr_err("%s: not initialized\n", DRIVER_NAME);
a603a7fa
DB
436 return -EPERM;
437 }
8653be1a
IY
438 sid = twl_map[mod_no].sid;
439 twl = &twl_modules[sid];
440
a603a7fa
DB
441 mutex_lock(&twl->xfer_lock);
442 /* [MSG1] fill the register address data */
443 msg = &twl->xfer_msg[0];
444 msg->addr = twl->address;
445 msg->len = 1;
446 msg->flags = 0; /* Read the register value */
e8deb28c 447 val = twl_map[mod_no].base + reg;
a603a7fa
DB
448 msg->buf = &val;
449 /* [MSG2] fill the data rx buffer */
450 msg = &twl->xfer_msg[1];
451 msg->addr = twl->address;
452 msg->flags = I2C_M_RD; /* Read the register value */
453 msg->len = num_bytes; /* only n bytes */
454 msg->buf = value;
455 ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
456 mutex_unlock(&twl->xfer_lock);
457
147e0847
AK
458 /* i2c_transfer returns number of messages transferred */
459 if (ret != 2) {
460 pr_err("%s: i2c_read failed to transfer all messages\n",
461 DRIVER_NAME);
462 if (ret < 0)
463 return ret;
464 else
465 return -EIO;
466 } else {
467 return 0;
468 }
a603a7fa 469}
fc7b92fc 470EXPORT_SYMBOL(twl_i2c_read);
a603a7fa
DB
471
472/**
fc7b92fc 473 * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
a603a7fa
DB
474 * @mod_no: module number
475 * @value: the value to be written 8 bit
476 * @reg: register address (just offset will do)
477 *
478 * Returns result of operation - 0 is success
479 */
fc7b92fc 480int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
a603a7fa
DB
481{
482
483 /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
484 u8 temp_buffer[2] = { 0 };
485 /* offset 1 contains the data */
486 temp_buffer[1] = value;
fc7b92fc 487 return twl_i2c_write(mod_no, temp_buffer, reg, 1);
a603a7fa 488}
fc7b92fc 489EXPORT_SYMBOL(twl_i2c_write_u8);
a603a7fa
DB
490
491/**
fc7b92fc 492 * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
a603a7fa
DB
493 * @mod_no: module number
494 * @value: the value read 8 bit
495 * @reg: register address (just offset will do)
496 *
497 * Returns result of operation - 0 is success
498 */
fc7b92fc 499int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
a603a7fa 500{
fc7b92fc 501 return twl_i2c_read(mod_no, value, reg, 1);
a603a7fa 502}
fc7b92fc 503EXPORT_SYMBOL(twl_i2c_read_u8);
a603a7fa
DB
504
505/*----------------------------------------------------------------------*/
506
ca972d13
L
507/**
508 * twl_read_idcode_register - API to read the IDCODE register.
509 *
510 * Unlocks the IDCODE register and read the 32 bit value.
511 */
512static int twl_read_idcode_register(void)
513{
514 int err;
515
516 err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
517 REG_UNLOCK_TEST_REG);
518 if (err) {
519 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
520 goto fail;
521 }
522
523 err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
524 REG_IDCODE_7_0, 4);
525 if (err) {
526 pr_err("TWL4030: unable to read IDCODE -%d\n", err);
527 goto fail;
528 }
529
530 err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
531 if (err)
532 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
533fail:
534 return err;
535}
536
537/**
538 * twl_get_type - API to get TWL Si type.
539 *
540 * Api to get the TWL Si type from IDCODE value.
541 */
542int twl_get_type(void)
543{
544 return TWL_SIL_TYPE(twl_idcode);
545}
546EXPORT_SYMBOL_GPL(twl_get_type);
547
548/**
549 * twl_get_version - API to get TWL Si version.
550 *
551 * Api to get the TWL Si version from IDCODE value.
552 */
553int twl_get_version(void)
554{
555 return TWL_SIL_REV(twl_idcode);
556}
557EXPORT_SYMBOL_GPL(twl_get_version);
558
dad759ff
DB
559static struct device *
560add_numbered_child(unsigned chip, const char *name, int num,
5725d66b
DB
561 void *pdata, unsigned pdata_len,
562 bool can_wakeup, int irq0, int irq1)
a603a7fa 563{
5725d66b 564 struct platform_device *pdev;
fc7b92fc 565 struct twl_client *twl = &twl_modules[chip];
5725d66b
DB
566 int status;
567
dad759ff 568 pdev = platform_device_alloc(name, num);
5725d66b
DB
569 if (!pdev) {
570 dev_dbg(&twl->client->dev, "can't alloc dev\n");
571 status = -ENOMEM;
572 goto err;
573 }
a603a7fa 574
5725d66b
DB
575 device_init_wakeup(&pdev->dev, can_wakeup);
576 pdev->dev.parent = &twl->client->dev;
a603a7fa 577
5725d66b
DB
578 if (pdata) {
579 status = platform_device_add_data(pdev, pdata, pdata_len);
580 if (status < 0) {
581 dev_dbg(&pdev->dev, "can't add platform_data\n");
a603a7fa
DB
582 goto err;
583 }
5725d66b 584 }
a603a7fa 585
5725d66b
DB
586 if (irq0) {
587 struct resource r[2] = {
588 { .start = irq0, .flags = IORESOURCE_IRQ, },
589 { .start = irq1, .flags = IORESOURCE_IRQ, },
590 };
a603a7fa 591
5725d66b 592 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
a603a7fa 593 if (status < 0) {
5725d66b 594 dev_dbg(&pdev->dev, "can't add irqs\n");
a603a7fa
DB
595 goto err;
596 }
597 }
598
5725d66b 599 status = platform_device_add(pdev);
a603a7fa 600
5725d66b
DB
601err:
602 if (status < 0) {
603 platform_device_put(pdev);
604 dev_err(&twl->client->dev, "can't add %s dev\n", name);
605 return ERR_PTR(status);
606 }
607 return &pdev->dev;
608}
a603a7fa 609
dad759ff
DB
610static inline struct device *add_child(unsigned chip, const char *name,
611 void *pdata, unsigned pdata_len,
612 bool can_wakeup, int irq0, int irq1)
613{
614 return add_numbered_child(chip, name, -1, pdata, pdata_len,
615 can_wakeup, irq0, irq1);
616}
617
618static struct device *
619add_regulator_linked(int num, struct regulator_init_data *pdata,
620 struct regulator_consumer_supply *consumers,
521d8ec3 621 unsigned num_consumers, unsigned long features)
dad759ff 622{
e8deb28c 623 unsigned sub_chip_id;
dad759ff
DB
624 /* regulator framework demands init_data ... */
625 if (!pdata)
626 return NULL;
627
b73eac78 628 if (consumers) {
dad759ff
DB
629 pdata->consumer_supplies = consumers;
630 pdata->num_consumer_supplies = num_consumers;
631 }
632
521d8ec3
GG
633 pdata->driver_data = (void *)features;
634
dad759ff 635 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
e8deb28c
B
636 sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
637 return add_numbered_child(sub_chip_id, "twl_reg", num,
dad759ff
DB
638 pdata, sizeof(*pdata), false, 0, 0);
639}
640
641static struct device *
521d8ec3
GG
642add_regulator(int num, struct regulator_init_data *pdata,
643 unsigned long features)
dad759ff 644{
521d8ec3 645 return add_regulator_linked(num, pdata, NULL, 0, features);
dad759ff
DB
646}
647
5725d66b
DB
648/*
649 * NOTE: We know the first 8 IRQs after pdata->base_irq are
650 * for the PIH, and the next are for the PWR_INT SIH, since
651 * that's how twl_init_irq() sets things up.
652 */
a603a7fa 653
dad759ff
DB
654static int
655add_children(struct twl4030_platform_data *pdata, unsigned long features)
5725d66b
DB
656{
657 struct device *child;
e8deb28c 658 unsigned sub_chip_id;
a603a7fa 659
5725d66b 660 if (twl_has_gpio() && pdata->gpio) {
fc7b92fc 661 child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
5725d66b 662 pdata->gpio, sizeof(*pdata->gpio),
fc7b92fc 663 false, pdata->irq_base + GPIO_INTR_OFFSET, 0);
5725d66b
DB
664 if (IS_ERR(child))
665 return PTR_ERR(child);
a603a7fa
DB
666 }
667
668 if (twl_has_keypad() && pdata->keypad) {
fc7b92fc 669 child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
5725d66b 670 pdata->keypad, sizeof(*pdata->keypad),
fc7b92fc 671 true, pdata->irq_base + KEYPAD_INTR_OFFSET, 0);
5725d66b
DB
672 if (IS_ERR(child))
673 return PTR_ERR(child);
a603a7fa
DB
674 }
675
676 if (twl_has_madc() && pdata->madc) {
5725d66b
DB
677 child = add_child(2, "twl4030_madc",
678 pdata->madc, sizeof(*pdata->madc),
fc7b92fc 679 true, pdata->irq_base + MADC_INTR_OFFSET, 0);
5725d66b
DB
680 if (IS_ERR(child))
681 return PTR_ERR(child);
a603a7fa
DB
682 }
683
684 if (twl_has_rtc()) {
a603a7fa 685 /*
5725d66b 686 * REVISIT platform_data here currently might expose the
a603a7fa 687 * "msecure" line ... but for now we just expect board
5725d66b 688 * setup to tell the chip "it's always ok to SET_TIME".
a603a7fa
DB
689 * Eventually, Linux might become more aware of such
690 * HW security concerns, and "least privilege".
691 */
e8deb28c
B
692 sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
693 child = add_child(sub_chip_id, "twl_rtc",
5725d66b 694 NULL, 0,
fc7b92fc 695 true, pdata->irq_base + RTC_INTR_OFFSET, 0);
5725d66b
DB
696 if (IS_ERR(child))
697 return PTR_ERR(child);
a603a7fa
DB
698 }
699
9da66539 700 if (twl_has_usb() && pdata->usb && twl_class_is_4030()) {
f8ebdff0
RQ
701
702 static struct regulator_consumer_supply usb1v5 = {
703 .supply = "usb1v5",
704 };
705 static struct regulator_consumer_supply usb1v8 = {
706 .supply = "usb1v8",
707 };
708 static struct regulator_consumer_supply usb3v1 = {
709 .supply = "usb3v1",
710 };
711
712 /* First add the regulators so that they can be used by transceiver */
713 if (twl_has_regulator()) {
714 /* this is a template that gets copied */
715 struct regulator_init_data usb_fixed = {
716 .constraints.valid_modes_mask =
717 REGULATOR_MODE_NORMAL
718 | REGULATOR_MODE_STANDBY,
719 .constraints.valid_ops_mask =
720 REGULATOR_CHANGE_MODE
721 | REGULATOR_CHANGE_STATUS,
722 };
723
724 child = add_regulator_linked(TWL4030_REG_VUSB1V5,
521d8ec3
GG
725 &usb_fixed, &usb1v5, 1,
726 features);
f8ebdff0
RQ
727 if (IS_ERR(child))
728 return PTR_ERR(child);
729
730 child = add_regulator_linked(TWL4030_REG_VUSB1V8,
521d8ec3
GG
731 &usb_fixed, &usb1v8, 1,
732 features);
f8ebdff0
RQ
733 if (IS_ERR(child))
734 return PTR_ERR(child);
735
736 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
521d8ec3
GG
737 &usb_fixed, &usb3v1, 1,
738 features);
f8ebdff0
RQ
739 if (IS_ERR(child))
740 return PTR_ERR(child);
741
742 }
743
5725d66b
DB
744 child = add_child(0, "twl4030_usb",
745 pdata->usb, sizeof(*pdata->usb),
746 true,
747 /* irq0 = USB_PRES, irq1 = USB */
fc7b92fc
B
748 pdata->irq_base + USB_PRES_INTR_OFFSET,
749 pdata->irq_base + USB_INTR_OFFSET);
f8ebdff0 750
5725d66b
DB
751 if (IS_ERR(child))
752 return PTR_ERR(child);
dad759ff
DB
753
754 /* we need to connect regulators to this transceiver */
f8ebdff0
RQ
755 if (twl_has_regulator() && child) {
756 usb1v5.dev = child;
757 usb1v8.dev = child;
758 usb3v1.dev = child;
759 }
dad759ff 760 }
e70357e3
HH
761 if (twl_has_usb() && pdata->usb && twl_class_is_6030()) {
762
521d8ec3
GG
763 static struct regulator_consumer_supply usb3v3;
764 int regulator;
e70357e3
HH
765
766 if (twl_has_regulator()) {
767 /* this is a template that gets copied */
768 struct regulator_init_data usb_fixed = {
769 .constraints.valid_modes_mask =
770 REGULATOR_MODE_NORMAL
771 | REGULATOR_MODE_STANDBY,
772 .constraints.valid_ops_mask =
773 REGULATOR_CHANGE_MODE
774 | REGULATOR_CHANGE_STATUS,
775 };
776
521d8ec3
GG
777 if (features & TWL6025_SUBCLASS) {
778 usb3v3.supply = "ldousb";
779 regulator = TWL6025_REG_LDOUSB;
780 } else {
781 usb3v3.supply = "vusb";
782 regulator = TWL6030_REG_VUSB;
783 }
784 child = add_regulator_linked(regulator, &usb_fixed,
785 &usb3v3, 1,
786 features);
e70357e3
HH
787 if (IS_ERR(child))
788 return PTR_ERR(child);
789 }
790
521d8ec3
GG
791 pdata->usb->features = features;
792
e70357e3
HH
793 child = add_child(0, "twl6030_usb",
794 pdata->usb, sizeof(*pdata->usb),
795 true,
796 /* irq1 = VBUS_PRES, irq0 = USB ID */
797 pdata->irq_base + USBOTG_INTR_OFFSET,
798 pdata->irq_base + USB_PRES_INTR_OFFSET);
799
800 if (IS_ERR(child))
801 return PTR_ERR(child);
802 /* we need to connect regulators to this transceiver */
803 if (twl_has_regulator() && child)
804 usb3v3.dev = child;
521d8ec3
GG
805 } else if (twl_has_regulator() && twl_class_is_6030()) {
806 if (features & TWL6025_SUBCLASS)
807 child = add_regulator(TWL6025_REG_LDOUSB,
808 pdata->ldousb, features);
809 else
810 child = add_regulator(TWL6030_REG_VUSB,
811 pdata->vusb, features);
e70357e3 812
521d8ec3
GG
813 if (IS_ERR(child))
814 return PTR_ERR(child);
e70357e3 815 }
dad759ff 816
153617fd 817 if (twl_has_watchdog() && twl_class_is_4030()) {
80e45b1e
TK
818 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
819 if (IS_ERR(child))
9c3664dd
FB
820 return PTR_ERR(child);
821 }
822
153617fd 823 if (twl_has_pwrbutton() && twl_class_is_4030()) {
9c3664dd
FB
824 child = add_child(1, "twl4030_pwrbutton",
825 NULL, 0, true, pdata->irq_base + 8 + 0, 0);
826 if (IS_ERR(child))
80e45b1e
TK
827 return PTR_ERR(child);
828 }
829
4ae6df5e 830 if (twl_has_codec() && pdata->audio && twl_class_is_4030()) {
d62abe56 831 sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
f0fba2ad 832 child = add_child(sub_chip_id, "twl4030-audio",
4ae6df5e 833 pdata->audio, sizeof(*pdata->audio),
d62abe56
MLC
834 false, 0, 0);
835 if (IS_ERR(child))
836 return PTR_ERR(child);
837 }
838
4ae6df5e 839 if (twl_has_codec() && pdata->audio && twl_class_is_6030()) {
d62abe56 840 sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
f19b2823 841 child = add_child(sub_chip_id, "twl6040",
4ae6df5e 842 pdata->audio, sizeof(*pdata->audio),
0b83ddeb
PU
843 false, 0, 0);
844 if (IS_ERR(child))
845 return PTR_ERR(child);
846 }
847
9da66539
RN
848 /* twl4030 regulators */
849 if (twl_has_regulator() && twl_class_is_4030()) {
521d8ec3
GG
850 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
851 features);
dad759ff
DB
852 if (IS_ERR(child))
853 return PTR_ERR(child);
ab4abe05 854
521d8ec3
GG
855 child = add_regulator(TWL4030_REG_VIO, pdata->vio,
856 features);
ab4abe05
JKS
857 if (IS_ERR(child))
858 return PTR_ERR(child);
859
521d8ec3
GG
860 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
861 features);
ab4abe05
JKS
862 if (IS_ERR(child))
863 return PTR_ERR(child);
864
521d8ec3
GG
865 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
866 features);
ab4abe05
JKS
867 if (IS_ERR(child))
868 return PTR_ERR(child);
dad759ff 869
521d8ec3
GG
870 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
871 features);
dad759ff
DB
872 if (IS_ERR(child))
873 return PTR_ERR(child);
874
521d8ec3
GG
875 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
876 features);
dad759ff
DB
877 if (IS_ERR(child))
878 return PTR_ERR(child);
879
880 child = add_regulator((features & TWL4030_VAUX2)
881 ? TWL4030_REG_VAUX2_4030
882 : TWL4030_REG_VAUX2,
521d8ec3 883 pdata->vaux2, features);
dad759ff
DB
884 if (IS_ERR(child))
885 return PTR_ERR(child);
ab4abe05 886
521d8ec3
GG
887 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
888 features);
ab4abe05
JKS
889 if (IS_ERR(child))
890 return PTR_ERR(child);
891
521d8ec3
GG
892 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
893 features);
ab4abe05
JKS
894 if (IS_ERR(child))
895 return PTR_ERR(child);
896
521d8ec3
GG
897 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
898 features);
ab4abe05
JKS
899 if (IS_ERR(child))
900 return PTR_ERR(child);
dad759ff
DB
901 }
902
dad759ff 903 /* maybe add LDOs that are omitted on cost-reduced parts */
9da66539
RN
904 if (twl_has_regulator() && !(features & TPS_SUBSET)
905 && twl_class_is_4030()) {
521d8ec3
GG
906 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
907 features);
dad759ff
DB
908 if (IS_ERR(child))
909 return PTR_ERR(child);
dad759ff 910
521d8ec3
GG
911 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
912 features);
dad759ff
DB
913 if (IS_ERR(child))
914 return PTR_ERR(child);
915
521d8ec3
GG
916 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
917 features);
dad759ff
DB
918 if (IS_ERR(child))
919 return PTR_ERR(child);
920
521d8ec3
GG
921 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
922 features);
dad759ff
DB
923 if (IS_ERR(child))
924 return PTR_ERR(child);
925
521d8ec3
GG
926 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
927 features);
dad759ff
DB
928 if (IS_ERR(child))
929 return PTR_ERR(child);
930
521d8ec3
GG
931 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
932 features);
dad759ff
DB
933 if (IS_ERR(child))
934 return PTR_ERR(child);
a603a7fa
DB
935 }
936
9da66539 937 /* twl6030 regulators */
521d8ec3
GG
938 if (twl_has_regulator() && twl_class_is_6030() &&
939 !(features & TWL6025_SUBCLASS)) {
940 child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
941 features);
942 if (IS_ERR(child))
943 return PTR_ERR(child);
944
945 child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
946 features);
947 if (IS_ERR(child))
948 return PTR_ERR(child);
949
950 child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
951 features);
952 if (IS_ERR(child))
953 return PTR_ERR(child);
954
955 child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
956 features);
957 if (IS_ERR(child))
958 return PTR_ERR(child);
959
960 child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
961 features);
962 if (IS_ERR(child))
963 return PTR_ERR(child);
964
965 child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
966 features);
967 if (IS_ERR(child))
968 return PTR_ERR(child);
969
970 child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
971 features);
972 if (IS_ERR(child))
973 return PTR_ERR(child);
974
975 child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
976 features);
977 if (IS_ERR(child))
978 return PTR_ERR(child);
979
980 child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
981 features);
982 if (IS_ERR(child))
983 return PTR_ERR(child);
984 }
985
986 /* 6030 and 6025 share this regulator */
9da66539 987 if (twl_has_regulator() && twl_class_is_6030()) {
521d8ec3
GG
988 child = add_regulator(TWL6030_REG_VANA, pdata->vana,
989 features);
990 if (IS_ERR(child))
991 return PTR_ERR(child);
992 }
993
994 /* twl6025 regulators */
995 if (twl_has_regulator() && twl_class_is_6030() &&
996 (features & TWL6025_SUBCLASS)) {
997 child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
998 features);
9da66539
RN
999 if (IS_ERR(child))
1000 return PTR_ERR(child);
1001
521d8ec3
GG
1002 child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
1003 features);
9da66539
RN
1004 if (IS_ERR(child))
1005 return PTR_ERR(child);
1006
521d8ec3
GG
1007 child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
1008 features);
9da66539
RN
1009 if (IS_ERR(child))
1010 return PTR_ERR(child);
1011
521d8ec3
GG
1012 child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
1013 features);
9da66539
RN
1014 if (IS_ERR(child))
1015 return PTR_ERR(child);
1016
521d8ec3
GG
1017 child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
1018 features);
9da66539
RN
1019 if (IS_ERR(child))
1020 return PTR_ERR(child);
1021
521d8ec3
GG
1022 child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
1023 features);
9da66539
RN
1024 if (IS_ERR(child))
1025 return PTR_ERR(child);
1026
521d8ec3
GG
1027 child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
1028 features);
9da66539
RN
1029 if (IS_ERR(child))
1030 return PTR_ERR(child);
1031
521d8ec3
GG
1032 child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
1033 features);
9da66539
RN
1034 if (IS_ERR(child))
1035 return PTR_ERR(child);
1036
521d8ec3
GG
1037 child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
1038 features);
9da66539
RN
1039 if (IS_ERR(child))
1040 return PTR_ERR(child);
8e6de4a3 1041
521d8ec3
GG
1042 child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
1043 features);
8e6de4a3
B
1044 if (IS_ERR(child))
1045 return PTR_ERR(child);
521d8ec3
GG
1046
1047 child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
1048 features);
1049 if (IS_ERR(child))
1050 return PTR_ERR(child);
1051
9da66539
RN
1052 }
1053
11c39c4b
GI
1054 if (twl_has_bci() && pdata->bci &&
1055 !(features & (TPS_SUBSET | TWL5031))) {
1056 child = add_child(3, "twl4030_bci",
1057 pdata->bci, sizeof(*pdata->bci), false,
1058 /* irq0 = CHG_PRES, irq1 = BCI */
1059 pdata->irq_base + BCI_PRES_INTR_OFFSET,
1060 pdata->irq_base + BCI_INTR_OFFSET);
1061 if (IS_ERR(child))
1062 return PTR_ERR(child);
1063 }
1064
5725d66b 1065 return 0;
a603a7fa
DB
1066}
1067
1068/*----------------------------------------------------------------------*/
1069
1070/*
1071 * These three functions initialize the on-chip clock framework,
1072 * letting it generate the right frequencies for USB, MADC, and
1073 * other purposes.
1074 */
1075static inline int __init protect_pm_master(void)
1076{
1077 int e = 0;
1078
49e6f87e
FB
1079 e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
1080 TWL4030_PM_MASTER_PROTECT_KEY);
a603a7fa
DB
1081 return e;
1082}
1083
1084static inline int __init unprotect_pm_master(void)
1085{
1086 int e = 0;
1087
49e6f87e
FB
1088 e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1089 TWL4030_PM_MASTER_KEY_CFG1,
1090 TWL4030_PM_MASTER_PROTECT_KEY);
1091 e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1092 TWL4030_PM_MASTER_KEY_CFG2,
1093 TWL4030_PM_MASTER_PROTECT_KEY);
1094
a603a7fa
DB
1095 return e;
1096}
1097
38a68496
IK
1098static void clocks_init(struct device *dev,
1099 struct twl4030_clock_init_data *clock)
a603a7fa
DB
1100{
1101 int e = 0;
1102 struct clk *osc;
1103 u32 rate;
1104 u8 ctrl = HFCLK_FREQ_26_MHZ;
1105
1106#if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
1107 if (cpu_is_omap2430())
e6b50c8d 1108 osc = clk_get(dev, "osc_ck");
a603a7fa 1109 else
e6b50c8d 1110 osc = clk_get(dev, "osc_sys_ck");
6354ab5c 1111
a603a7fa 1112 if (IS_ERR(osc)) {
fc7b92fc 1113 printk(KERN_WARNING "Skipping twl internal clock init and "
a603a7fa
DB
1114 "using bootloader value (unknown osc rate)\n");
1115 return;
1116 }
1117
1118 rate = clk_get_rate(osc);
1119 clk_put(osc);
1120
6354ab5c
SO
1121#else
1122 /* REVISIT for non-OMAP systems, pass the clock rate from
1123 * board init code, using platform_data.
1124 */
1125 osc = ERR_PTR(-EIO);
1126
fc7b92fc 1127 printk(KERN_WARNING "Skipping twl internal clock init and "
6354ab5c
SO
1128 "using bootloader value (unknown osc rate)\n");
1129
1130 return;
1131#endif
1132
a603a7fa
DB
1133 switch (rate) {
1134 case 19200000:
1135 ctrl = HFCLK_FREQ_19p2_MHZ;
1136 break;
1137 case 26000000:
1138 ctrl = HFCLK_FREQ_26_MHZ;
1139 break;
1140 case 38400000:
1141 ctrl = HFCLK_FREQ_38p4_MHZ;
1142 break;
1143 }
1144
1145 ctrl |= HIGH_PERF_SQ;
38a68496
IK
1146 if (clock && clock->ck32k_lowpwr_enable)
1147 ctrl |= CK32K_LOWPWR_EN;
1148
a603a7fa
DB
1149 e |= unprotect_pm_master();
1150 /* effect->MADC+USB ck en */
fc7b92fc 1151 e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
a603a7fa
DB
1152 e |= protect_pm_master();
1153
1154 if (e < 0)
1155 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
1156}
1157
1158/*----------------------------------------------------------------------*/
1159
e8deb28c
B
1160int twl4030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
1161int twl4030_exit_irq(void);
1162int twl4030_init_chip_irq(const char *chip);
1163int twl6030_init_irq(int irq_num, unsigned irq_base, unsigned irq_end);
1164int twl6030_exit_irq(void);
a603a7fa 1165
fc7b92fc 1166static int twl_remove(struct i2c_client *client)
a603a7fa
DB
1167{
1168 unsigned i;
a30d46c0 1169 int status;
a603a7fa 1170
e8deb28c
B
1171 if (twl_class_is_4030())
1172 status = twl4030_exit_irq();
1173 else
1174 status = twl6030_exit_irq();
1175
a30d46c0
DB
1176 if (status < 0)
1177 return status;
a603a7fa 1178
fc7b92fc
B
1179 for (i = 0; i < TWL_NUM_SLAVES; i++) {
1180 struct twl_client *twl = &twl_modules[i];
a603a7fa
DB
1181
1182 if (twl->client && twl->client != client)
1183 i2c_unregister_device(twl->client);
fc7b92fc 1184 twl_modules[i].client = NULL;
a603a7fa
DB
1185 }
1186 inuse = false;
1187 return 0;
1188}
1189
1190/* NOTE: this driver only handles a single twl4030/tps659x0 chip */
5b9cecd6 1191static int __devinit
fc7b92fc 1192twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
a603a7fa
DB
1193{
1194 int status;
1195 unsigned i;
1196 struct twl4030_platform_data *pdata = client->dev.platform_data;
aeb5032b 1197 struct device_node *node = client->dev.of_node;
a29aaf55 1198 u8 temp;
ca972d13 1199 int ret = 0;
aeb5032b
BC
1200 int nr_irqs = TWL4030_NR_IRQS;
1201
1202 if ((id->driver_data) & TWL6030_CLASS)
1203 nr_irqs = TWL6030_NR_IRQS;
1204
1205 if (node && !pdata) {
1206 /*
1207 * XXX: Temporary pdata until the information is correctly
1208 * retrieved by every TWL modules from DT.
1209 */
1210 pdata = devm_kzalloc(&client->dev,
1211 sizeof(struct twl4030_platform_data),
1212 GFP_KERNEL);
1213 if (!pdata)
1214 return -ENOMEM;
1215 }
a603a7fa
DB
1216
1217 if (!pdata) {
1218 dev_dbg(&client->dev, "no platform data?\n");
1219 return -EINVAL;
1220 }
1221
aeb5032b
BC
1222 status = irq_alloc_descs(-1, pdata->irq_base, nr_irqs, 0);
1223 if (IS_ERR_VALUE(status)) {
1224 dev_err(&client->dev, "Fail to allocate IRQ descs\n");
1225 return status;
1226 }
1227
1228 pdata->irq_base = status;
1229 pdata->irq_end = pdata->irq_base + nr_irqs;
1230
6252547b 1231#ifdef CONFIG_IRQ_DOMAIN
aeb5032b
BC
1232 domain.irq_base = pdata->irq_base;
1233 domain.nr_irq = nr_irqs;
aeb5032b
BC
1234 domain.of_node = of_node_get(node);
1235 domain.ops = &irq_domain_simple_ops;
aeb5032b 1236 irq_domain_add(&domain);
6252547b 1237#endif
aeb5032b 1238
a603a7fa
DB
1239 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1240 dev_dbg(&client->dev, "can't talk I2C?\n");
1241 return -EIO;
1242 }
1243
a30d46c0 1244 if (inuse) {
a603a7fa
DB
1245 dev_dbg(&client->dev, "driver is already in use\n");
1246 return -EBUSY;
1247 }
1248
fc7b92fc
B
1249 for (i = 0; i < TWL_NUM_SLAVES; i++) {
1250 struct twl_client *twl = &twl_modules[i];
a603a7fa
DB
1251
1252 twl->address = client->addr + i;
1253 if (i == 0)
1254 twl->client = client;
1255 else {
1256 twl->client = i2c_new_dummy(client->adapter,
1257 twl->address);
1258 if (!twl->client) {
a8643430 1259 dev_err(&client->dev,
a603a7fa
DB
1260 "can't attach client %d\n", i);
1261 status = -ENOMEM;
1262 goto fail;
1263 }
a603a7fa
DB
1264 }
1265 mutex_init(&twl->xfer_lock);
1266 }
1267 inuse = true;
e8deb28c
B
1268 if ((id->driver_data) & TWL6030_CLASS) {
1269 twl_id = TWL6030_CLASS_ID;
1270 twl_map = &twl6030_map[0];
1271 } else {
1272 twl_id = TWL4030_CLASS_ID;
1273 twl_map = &twl4030_map[0];
1274 }
a603a7fa
DB
1275
1276 /* setup clock framework */
38a68496 1277 clocks_init(&client->dev, pdata->clock);
a603a7fa 1278
ca972d13
L
1279 /* read TWL IDCODE Register */
1280 if (twl_id == TWL4030_CLASS_ID) {
1281 ret = twl_read_idcode_register();
1282 WARN(ret < 0, "Error: reading twl_idcode register value\n");
1283 }
1284
ebf0bd36
AK
1285 /* load power event scripts */
1286 if (twl_has_power() && pdata->power)
1287 twl4030_power_init(pdata->power);
1288
a603a7fa
DB
1289 /* Maybe init the T2 Interrupt subsystem */
1290 if (client->irq
1291 && pdata->irq_base
1292 && pdata->irq_end > pdata->irq_base) {
e8deb28c
B
1293 if (twl_class_is_4030()) {
1294 twl4030_init_chip_irq(id->name);
1295 status = twl4030_init_irq(client->irq, pdata->irq_base,
1296 pdata->irq_end);
1297 } else {
1298 status = twl6030_init_irq(client->irq, pdata->irq_base,
1299 pdata->irq_end);
1300 }
1301
a30d46c0
DB
1302 if (status < 0)
1303 goto fail;
a603a7fa
DB
1304 }
1305
a29aaf55
MS
1306 /* Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1307 * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1308 * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1309 */
1310
1311 if (twl_class_is_4030()) {
1312 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1313 temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
1314 I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
1315 twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1316 }
1317
aeb5032b
BC
1318#ifdef CONFIG_OF_DEVICE
1319 if (node)
1320 status = of_platform_populate(node, NULL, NULL, &client->dev);
1321 else
1322#endif
1323 status = add_children(pdata, id->driver_data);
1324
a603a7fa
DB
1325fail:
1326 if (status < 0)
fc7b92fc 1327 twl_remove(client);
a603a7fa
DB
1328 return status;
1329}
1330
fc7b92fc 1331static const struct i2c_device_id twl_ids[] = {
dad759ff
DB
1332 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
1333 { "twl5030", 0 }, /* T2 updated */
1920a61e 1334 { "twl5031", TWL5031 }, /* TWL5030 updated */
dad759ff
DB
1335 { "tps65950", 0 }, /* catalog version of twl5030 */
1336 { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
1337 { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
59dead5a
OD
1338 { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
1339 and vibrator. Charger in USB module*/
e8deb28c 1340 { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
521d8ec3 1341 { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
a603a7fa
DB
1342 { /* end of list */ },
1343};
fc7b92fc 1344MODULE_DEVICE_TABLE(i2c, twl_ids);
a603a7fa
DB
1345
1346/* One Client Driver , 4 Clients */
fc7b92fc 1347static struct i2c_driver twl_driver = {
a603a7fa 1348 .driver.name = DRIVER_NAME,
fc7b92fc
B
1349 .id_table = twl_ids,
1350 .probe = twl_probe,
1351 .remove = twl_remove,
a603a7fa
DB
1352};
1353
fc7b92fc 1354static int __init twl_init(void)
a603a7fa 1355{
fc7b92fc 1356 return i2c_add_driver(&twl_driver);
a603a7fa 1357}
fc7b92fc 1358subsys_initcall(twl_init);
a603a7fa 1359
fc7b92fc 1360static void __exit twl_exit(void)
a603a7fa 1361{
fc7b92fc 1362 i2c_del_driver(&twl_driver);
a603a7fa 1363}
fc7b92fc 1364module_exit(twl_exit);
a603a7fa
DB
1365
1366MODULE_AUTHOR("Texas Instruments, Inc.");
fc7b92fc 1367MODULE_DESCRIPTION("I2C Core interface for TWL");
a603a7fa 1368MODULE_LICENSE("GPL");
This page took 0.500418 seconds and 4 git commands to generate.