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