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