blob: d666c9d454cb4a58e43a21884edb1afe1d2b12d1 [file] [log] [blame]
David Brownella603a7f2008-10-15 12:15:39 +02001/*
Balaji T Kfc7b92f2009-12-13 21:23:33 +01002 * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
3 * and audio CODEC devices
David Brownella603a7f2008-10-15 12:15:39 +02004 *
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
David Brownella603a7f2008-10-15 12:15:39 +020031#include <linux/init.h>
32#include <linux/mutex.h>
Paul Gortmaker4e36dd32011-07-03 15:13:27 -040033#include <linux/module.h>
David Brownella603a7f2008-10-15 12:15:39 +020034#include <linux/platform_device.h>
Peter Ujfalusi2473d252012-11-13 09:28:44 +010035#include <linux/regmap.h>
David Brownella603a7f2008-10-15 12:15:39 +020036#include <linux/clk.h>
David Brownella30d46c2008-10-20 23:46:28 +020037#include <linux/err.h>
Benoit Coussonaeb50322011-08-29 16:20:23 +020038#include <linux/device.h>
39#include <linux/of.h>
40#include <linux/of_irq.h>
41#include <linux/of_platform.h>
Grant Likelye7cc3ac2012-03-02 21:05:31 -070042#include <linux/irq.h>
Benoit Coussonaeb50322011-08-29 16:20:23 +020043#include <linux/irqdomain.h>
David Brownella603a7f2008-10-15 12:15:39 +020044
David Brownelldad759f2008-12-01 00:43:58 +010045#include <linux/regulator/machine.h>
46
David Brownella603a7f2008-10-15 12:15:39 +020047#include <linux/i2c.h>
Santosh Shilimkarb07682b2009-12-13 20:05:51 +010048#include <linux/i2c/twl.h>
David Brownella603a7f2008-10-15 12:15:39 +020049
Benoit Cousson1b8f3332012-02-29 19:28:14 +010050#include "twl-core.h"
David Brownella603a7f2008-10-15 12:15:39 +020051
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
Balaji T Kfc7b92f2009-12-13 21:23:33 +010065#define DRIVER_NAME "twl"
David Brownella603a7f2008-10-15 12:15:39 +020066
David Brownella603a7f2008-10-15 12:15:39 +020067/* Triton Core internal information (BEGIN) */
68
69/* Last - for index max*/
70#define TWL4030_MODULE_LAST TWL4030_MODULE_SECURED_REG
71
Balaji T Kfc7b92f2009-12-13 21:23:33 +010072#define TWL_NUM_SLAVES 4
David Brownella603a7f2008-10-15 12:15:39 +020073
Balaji T Kfc7b92f2009-12-13 21:23:33 +010074#define SUB_CHIP_ID0 0
75#define SUB_CHIP_ID1 1
76#define SUB_CHIP_ID2 2
77#define SUB_CHIP_ID3 3
Peter Ujfalusi364cedb2012-01-04 14:58:33 +020078#define SUB_CHIP_ID_INVAL 0xff
Balaji T Kfc7b92f2009-12-13 21:23:33 +010079
80#define TWL_MODULE_LAST TWL4030_MODULE_LAST
81
David Brownella603a7f2008-10-15 12:15:39 +020082/* Base Address defns for twl4030_map[] */
83
84/* subchip/slave 0 - USB ID */
85#define TWL4030_BASEADD_USB 0x0000
86
87/* subchip/slave 1 - AUD ID */
88#define TWL4030_BASEADD_AUDIO_VOICE 0x0000
89#define TWL4030_BASEADD_GPIO 0x0098
90#define TWL4030_BASEADD_INTBR 0x0085
91#define TWL4030_BASEADD_PIH 0x0080
92#define TWL4030_BASEADD_TEST 0x004C
93
94/* subchip/slave 2 - AUX ID */
95#define TWL4030_BASEADD_INTERRUPTS 0x00B9
96#define TWL4030_BASEADD_LED 0x00EE
97#define TWL4030_BASEADD_MADC 0x0000
98#define TWL4030_BASEADD_MAIN_CHARGE 0x0074
99#define TWL4030_BASEADD_PRECHARGE 0x00AA
100#define TWL4030_BASEADD_PWM0 0x00F8
101#define TWL4030_BASEADD_PWM1 0x00FB
102#define TWL4030_BASEADD_PWMA 0x00EF
103#define TWL4030_BASEADD_PWMB 0x00F1
104#define TWL4030_BASEADD_KEYPAD 0x00D2
105
Ilkka Koskinen1920a612009-11-10 17:26:15 +0200106#define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
107#define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
108 one */
109
David Brownella603a7f2008-10-15 12:15:39 +0200110/* subchip/slave 3 - POWER ID */
111#define TWL4030_BASEADD_BACKUP 0x0014
112#define TWL4030_BASEADD_INT 0x002E
113#define TWL4030_BASEADD_PM_MASTER 0x0036
114#define TWL4030_BASEADD_PM_RECEIVER 0x005B
115#define TWL4030_BASEADD_RTC 0x001C
116#define TWL4030_BASEADD_SECURED_REG 0x0000
117
118/* Triton Core internal information (END) */
119
120
Balaji T Ke8deb282009-12-14 00:25:31 +0100121/* subchip/slave 0 0x48 - POWER */
122#define TWL6030_BASEADD_RTC 0x0000
123#define TWL6030_BASEADD_MEM 0x0017
124#define TWL6030_BASEADD_PM_MASTER 0x001F
125#define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
126#define TWL6030_BASEADD_PM_MISC 0x00E2
127#define TWL6030_BASEADD_PM_PUPD 0x00F0
128
129/* subchip/slave 1 0x49 - FEATURE */
130#define TWL6030_BASEADD_USB 0x0000
131#define TWL6030_BASEADD_GPADC_CTRL 0x002E
132#define TWL6030_BASEADD_AUX 0x0090
133#define TWL6030_BASEADD_PWM 0x00BA
134#define TWL6030_BASEADD_GASGAUGE 0x00C0
135#define TWL6030_BASEADD_PIH 0x00D0
136#define TWL6030_BASEADD_CHARGER 0x00E0
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100137#define TWL6025_BASEADD_CHARGER 0x00DA
Balaji T Ke8deb282009-12-14 00:25:31 +0100138
139/* subchip/slave 2 0x4A - DFT */
140#define TWL6030_BASEADD_DIEID 0x00C0
141
142/* subchip/slave 3 0x4B - AUDIO */
143#define TWL6030_BASEADD_AUDIO 0x0000
144#define TWL6030_BASEADD_RSV 0x0000
Balaji T Kfa0d9762010-02-19 12:39:38 +0100145#define TWL6030_BASEADD_ZERO 0x0000
Balaji T Ke8deb282009-12-14 00:25:31 +0100146
David Brownella603a7f2008-10-15 12:15:39 +0200147/* Few power values */
148#define R_CFG_BOOT 0x05
David Brownella603a7f2008-10-15 12:15:39 +0200149
150/* some fields in R_CFG_BOOT */
151#define HFCLK_FREQ_19p2_MHZ (1 << 0)
152#define HFCLK_FREQ_26_MHZ (2 << 0)
153#define HFCLK_FREQ_38p4_MHZ (3 << 0)
154#define HIGH_PERF_SQ (1 << 3)
Ilkka Koskinen38a68492009-10-22 14:14:09 +0300155#define CK32K_LOWPWR_EN (1 << 7)
David Brownella603a7f2008-10-15 12:15:39 +0200156
David Brownella603a7f2008-10-15 12:15:39 +0200157/*----------------------------------------------------------------------*/
158
David Brownella603a7f2008-10-15 12:15:39 +0200159/* is driver active, bound to a chip? */
160static bool inuse;
161
Lesly A Mca972d12011-04-14 17:57:53 +0530162/* TWL IDCODE Register value */
163static u32 twl_idcode;
164
Balaji T Ke8deb282009-12-14 00:25:31 +0100165static unsigned int twl_id;
166unsigned int twl_rev(void)
167{
168 return twl_id;
169}
170EXPORT_SYMBOL(twl_rev);
171
172/* Structure for each TWL4030/TWL6030 Slave */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100173struct twl_client {
David Brownella603a7f2008-10-15 12:15:39 +0200174 struct i2c_client *client;
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100175 struct regmap *regmap;
David Brownella603a7f2008-10-15 12:15:39 +0200176};
177
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100178static struct twl_client twl_modules[TWL_NUM_SLAVES];
David Brownella603a7f2008-10-15 12:15:39 +0200179
David Brownella603a7f2008-10-15 12:15:39 +0200180/* mapping the module id to slave id and base address */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100181struct twl_mapping {
David Brownella603a7f2008-10-15 12:15:39 +0200182 unsigned char sid; /* Slave ID */
183 unsigned char base; /* base address */
184};
G, Manjunath Kondaiah2cfcce12010-09-21 15:31:17 +0530185static struct twl_mapping *twl_map;
David Brownella603a7f2008-10-15 12:15:39 +0200186
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100187static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
David Brownella603a7f2008-10-15 12:15:39 +0200188 /*
189 * NOTE: don't change this table without updating the
Balaji T Ke8deb282009-12-14 00:25:31 +0100190 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
David Brownella603a7f2008-10-15 12:15:39 +0200191 * so they continue to match the order in this table.
192 */
193
194 { 0, TWL4030_BASEADD_USB },
195
196 { 1, TWL4030_BASEADD_AUDIO_VOICE },
197 { 1, TWL4030_BASEADD_GPIO },
198 { 1, TWL4030_BASEADD_INTBR },
199 { 1, TWL4030_BASEADD_PIH },
200 { 1, TWL4030_BASEADD_TEST },
201
202 { 2, TWL4030_BASEADD_KEYPAD },
203 { 2, TWL4030_BASEADD_MADC },
204 { 2, TWL4030_BASEADD_INTERRUPTS },
205 { 2, TWL4030_BASEADD_LED },
206 { 2, TWL4030_BASEADD_MAIN_CHARGE },
207 { 2, TWL4030_BASEADD_PRECHARGE },
208 { 2, TWL4030_BASEADD_PWM0 },
209 { 2, TWL4030_BASEADD_PWM1 },
210 { 2, TWL4030_BASEADD_PWMA },
211 { 2, TWL4030_BASEADD_PWMB },
Ilkka Koskinen1920a612009-11-10 17:26:15 +0200212 { 2, TWL5031_BASEADD_ACCESSORY },
213 { 2, TWL5031_BASEADD_INTERRUPTS },
David Brownella603a7f2008-10-15 12:15:39 +0200214
215 { 3, TWL4030_BASEADD_BACKUP },
216 { 3, TWL4030_BASEADD_INT },
217 { 3, TWL4030_BASEADD_PM_MASTER },
218 { 3, TWL4030_BASEADD_PM_RECEIVER },
219 { 3, TWL4030_BASEADD_RTC },
220 { 3, TWL4030_BASEADD_SECURED_REG },
221};
222
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100223static struct regmap_config twl4030_regmap_config[4] = {
224 {
225 /* Address 0x48 */
226 .reg_bits = 8,
227 .val_bits = 8,
228 .max_register = 0xff,
229 },
230 {
231 /* Address 0x49 */
232 .reg_bits = 8,
233 .val_bits = 8,
234 .max_register = 0xff,
235 },
236 {
237 /* Address 0x4a */
238 .reg_bits = 8,
239 .val_bits = 8,
240 .max_register = 0xff,
241 },
242 {
243 /* Address 0x4b */
244 .reg_bits = 8,
245 .val_bits = 8,
246 .max_register = 0xff,
247 },
248};
249
Balaji T Ke8deb282009-12-14 00:25:31 +0100250static struct twl_mapping twl6030_map[] = {
251 /*
252 * NOTE: don't change this table without updating the
253 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
254 * so they continue to match the order in this table.
255 */
256 { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
Peter Ujfalusi364cedb2012-01-04 14:58:33 +0200257 { SUB_CHIP_ID_INVAL, TWL6030_BASEADD_AUDIO },
Balaji T Ke8deb282009-12-14 00:25:31 +0100258 { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
259 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
260 { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
261
262 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
263 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
264 { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
265 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
266 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
267
268 { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
269 { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
270 { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
Balaji T Kfa0d9762010-02-19 12:39:38 +0100271 { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
272 { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
Balaji T Ke8deb282009-12-14 00:25:31 +0100273
Balaji T Kfa0d9762010-02-19 12:39:38 +0100274 { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
275 { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
Balaji T Ke8deb282009-12-14 00:25:31 +0100276 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
277 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
278 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
279 { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
280 { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
281
282 { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
283 { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100284 { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
Balaji T Ke8deb282009-12-14 00:25:31 +0100285};
286
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100287static struct regmap_config twl6030_regmap_config[3] = {
288 {
289 /* Address 0x48 */
290 .reg_bits = 8,
291 .val_bits = 8,
292 .max_register = 0xff,
293 },
294 {
295 /* Address 0x49 */
296 .reg_bits = 8,
297 .val_bits = 8,
298 .max_register = 0xff,
299 },
300 {
301 /* Address 0x4a */
302 .reg_bits = 8,
303 .val_bits = 8,
304 .max_register = 0xff,
305 },
306};
307
David Brownella603a7f2008-10-15 12:15:39 +0200308/*----------------------------------------------------------------------*/
309
David Brownella603a7f2008-10-15 12:15:39 +0200310/* Exported Functions */
311
312/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100313 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200314 * @mod_no: module number
315 * @value: an array of num_bytes+1 containing data to write
316 * @reg: register address (just offset will do)
317 * @num_bytes: number of bytes to transfer
318 *
319 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
320 * valid data starts at Offset 1.
321 *
322 * Returns the result of operation - 0 is success
323 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100324int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200325{
326 int ret;
327 int sid;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100328 struct twl_client *twl;
David Brownella603a7f2008-10-15 12:15:39 +0200329
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100330 if (unlikely(mod_no > TWL_MODULE_LAST)) {
David Brownella603a7f2008-10-15 12:15:39 +0200331 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
332 return -EPERM;
333 }
Ilya Yanok8653be12011-08-01 23:00:28 +0200334 if (unlikely(!inuse)) {
335 pr_err("%s: not initialized\n", DRIVER_NAME);
336 return -EPERM;
337 }
Balaji T Ke8deb282009-12-14 00:25:31 +0100338 sid = twl_map[mod_no].sid;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +0200339 if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
340 pr_err("%s: module %d is not part of the pmic\n",
341 DRIVER_NAME, mod_no);
342 return -EINVAL;
343 }
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100344 twl = &twl_modules[sid];
David Brownella603a7f2008-10-15 12:15:39 +0200345
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100346 ret = regmap_bulk_write(twl->regmap, twl_map[mod_no].base + reg,
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100347 value, num_bytes);
David Brownella603a7f2008-10-15 12:15:39 +0200348
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100349 if (ret)
350 pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n",
351 DRIVER_NAME, mod_no, reg, num_bytes);
352
353 return ret;
David Brownella603a7f2008-10-15 12:15:39 +0200354}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100355EXPORT_SYMBOL(twl_i2c_write);
David Brownella603a7f2008-10-15 12:15:39 +0200356
357/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100358 * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200359 * @mod_no: module number
360 * @value: an array of num_bytes containing data to be read
361 * @reg: register address (just offset will do)
362 * @num_bytes: number of bytes to transfer
363 *
364 * Returns result of operation - num_bytes is success else failure.
365 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100366int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200367{
368 int ret;
David Brownella603a7f2008-10-15 12:15:39 +0200369 int sid;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100370 struct twl_client *twl;
David Brownella603a7f2008-10-15 12:15:39 +0200371
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100372 if (unlikely(mod_no > TWL_MODULE_LAST)) {
David Brownella603a7f2008-10-15 12:15:39 +0200373 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
374 return -EPERM;
375 }
Ilya Yanok8653be12011-08-01 23:00:28 +0200376 if (unlikely(!inuse)) {
377 pr_err("%s: not initialized\n", DRIVER_NAME);
378 return -EPERM;
379 }
Balaji T Ke8deb282009-12-14 00:25:31 +0100380 sid = twl_map[mod_no].sid;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +0200381 if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
382 pr_err("%s: module %d is not part of the pmic\n",
383 DRIVER_NAME, mod_no);
384 return -EINVAL;
385 }
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100386 twl = &twl_modules[sid];
David Brownella603a7f2008-10-15 12:15:39 +0200387
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100388 ret = regmap_bulk_read(twl->regmap, twl_map[mod_no].base + reg,
389 value, num_bytes);
David Brownella603a7f2008-10-15 12:15:39 +0200390
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100391 if (ret)
392 pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n",
393 DRIVER_NAME, mod_no, reg, num_bytes);
394
395 return ret;
David Brownella603a7f2008-10-15 12:15:39 +0200396}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100397EXPORT_SYMBOL(twl_i2c_read);
David Brownella603a7f2008-10-15 12:15:39 +0200398
399/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100400 * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200401 * @mod_no: module number
402 * @value: the value to be written 8 bit
403 * @reg: register address (just offset will do)
404 *
405 * Returns result of operation - 0 is success
406 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100407int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
David Brownella603a7f2008-10-15 12:15:39 +0200408{
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100409 return twl_i2c_write(mod_no, &value, reg, 1);
David Brownella603a7f2008-10-15 12:15:39 +0200410}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100411EXPORT_SYMBOL(twl_i2c_write_u8);
David Brownella603a7f2008-10-15 12:15:39 +0200412
413/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100414 * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200415 * @mod_no: module number
416 * @value: the value read 8 bit
417 * @reg: register address (just offset will do)
418 *
419 * Returns result of operation - 0 is success
420 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100421int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
David Brownella603a7f2008-10-15 12:15:39 +0200422{
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100423 return twl_i2c_read(mod_no, value, reg, 1);
David Brownella603a7f2008-10-15 12:15:39 +0200424}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100425EXPORT_SYMBOL(twl_i2c_read_u8);
David Brownella603a7f2008-10-15 12:15:39 +0200426
427/*----------------------------------------------------------------------*/
428
Lesly A Mca972d12011-04-14 17:57:53 +0530429/**
430 * twl_read_idcode_register - API to read the IDCODE register.
431 *
432 * Unlocks the IDCODE register and read the 32 bit value.
433 */
434static int twl_read_idcode_register(void)
435{
436 int err;
437
438 err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
439 REG_UNLOCK_TEST_REG);
440 if (err) {
441 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
442 goto fail;
443 }
444
445 err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
446 REG_IDCODE_7_0, 4);
447 if (err) {
448 pr_err("TWL4030: unable to read IDCODE -%d\n", err);
449 goto fail;
450 }
451
452 err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
453 if (err)
454 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
455fail:
456 return err;
457}
458
459/**
460 * twl_get_type - API to get TWL Si type.
461 *
462 * Api to get the TWL Si type from IDCODE value.
463 */
464int twl_get_type(void)
465{
466 return TWL_SIL_TYPE(twl_idcode);
467}
468EXPORT_SYMBOL_GPL(twl_get_type);
469
470/**
471 * twl_get_version - API to get TWL Si version.
472 *
473 * Api to get the TWL Si version from IDCODE value.
474 */
475int twl_get_version(void)
476{
477 return TWL_SIL_REV(twl_idcode);
478}
479EXPORT_SYMBOL_GPL(twl_get_version);
480
Peter Ujfalusi2275c542012-09-10 13:46:22 +0300481/**
482 * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate.
483 *
484 * Api to get the TWL HFCLK rate based on BOOT_CFG register.
485 */
486int twl_get_hfclk_rate(void)
487{
488 u8 ctrl;
489 int rate;
490
491 twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &ctrl, R_CFG_BOOT);
492
493 switch (ctrl & 0x3) {
494 case HFCLK_FREQ_19p2_MHZ:
495 rate = 19200000;
496 break;
497 case HFCLK_FREQ_26_MHZ:
498 rate = 26000000;
499 break;
500 case HFCLK_FREQ_38p4_MHZ:
501 rate = 38400000;
502 break;
503 default:
504 pr_err("TWL4030: HFCLK is not configured\n");
505 rate = -EINVAL;
506 break;
507 }
508
509 return rate;
510}
511EXPORT_SYMBOL_GPL(twl_get_hfclk_rate);
512
David Brownelldad759f2008-12-01 00:43:58 +0100513static struct device *
514add_numbered_child(unsigned chip, const char *name, int num,
David Brownell5725d662008-12-01 00:31:04 +0100515 void *pdata, unsigned pdata_len,
516 bool can_wakeup, int irq0, int irq1)
517{
518 struct platform_device *pdev;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100519 struct twl_client *twl = &twl_modules[chip];
David Brownell5725d662008-12-01 00:31:04 +0100520 int status;
521
David Brownelldad759f2008-12-01 00:43:58 +0100522 pdev = platform_device_alloc(name, num);
David Brownell5725d662008-12-01 00:31:04 +0100523 if (!pdev) {
524 dev_dbg(&twl->client->dev, "can't alloc dev\n");
525 status = -ENOMEM;
526 goto err;
527 }
528
David Brownell5725d662008-12-01 00:31:04 +0100529 pdev->dev.parent = &twl->client->dev;
530
531 if (pdata) {
532 status = platform_device_add_data(pdev, pdata, pdata_len);
533 if (status < 0) {
534 dev_dbg(&pdev->dev, "can't add platform_data\n");
535 goto err;
536 }
537 }
538
539 if (irq0) {
540 struct resource r[2] = {
541 { .start = irq0, .flags = IORESOURCE_IRQ, },
542 { .start = irq1, .flags = IORESOURCE_IRQ, },
543 };
544
545 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
546 if (status < 0) {
547 dev_dbg(&pdev->dev, "can't add irqs\n");
548 goto err;
549 }
550 }
551
552 status = platform_device_add(pdev);
NeilBrown17ffba62012-07-07 08:51:03 +1000553 if (status == 0)
554 device_init_wakeup(&pdev->dev, can_wakeup);
David Brownell5725d662008-12-01 00:31:04 +0100555
556err:
557 if (status < 0) {
558 platform_device_put(pdev);
559 dev_err(&twl->client->dev, "can't add %s dev\n", name);
560 return ERR_PTR(status);
561 }
562 return &pdev->dev;
563}
564
David Brownelldad759f2008-12-01 00:43:58 +0100565static inline struct device *add_child(unsigned chip, const char *name,
566 void *pdata, unsigned pdata_len,
567 bool can_wakeup, int irq0, int irq1)
568{
569 return add_numbered_child(chip, name, -1, pdata, pdata_len,
570 can_wakeup, irq0, irq1);
571}
572
573static struct device *
574add_regulator_linked(int num, struct regulator_init_data *pdata,
575 struct regulator_consumer_supply *consumers,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100576 unsigned num_consumers, unsigned long features)
David Brownelldad759f2008-12-01 00:43:58 +0100577{
Balaji T Ke8deb282009-12-14 00:25:31 +0100578 unsigned sub_chip_id;
Tero Kristo63bfff42012-02-16 12:27:52 +0200579 struct twl_regulator_driver_data drv_data;
580
David Brownelldad759f2008-12-01 00:43:58 +0100581 /* regulator framework demands init_data ... */
582 if (!pdata)
583 return NULL;
584
David Brownellb73eac72008-12-07 19:10:58 +0100585 if (consumers) {
David Brownelldad759f2008-12-01 00:43:58 +0100586 pdata->consumer_supplies = consumers;
587 pdata->num_consumer_supplies = num_consumers;
588 }
589
Tero Kristo63bfff42012-02-16 12:27:52 +0200590 if (pdata->driver_data) {
591 /* If we have existing drv_data, just add the flags */
592 struct twl_regulator_driver_data *tmp;
593 tmp = pdata->driver_data;
594 tmp->features |= features;
595 } else {
596 /* add new driver data struct, used only during init */
597 drv_data.features = features;
598 drv_data.set_voltage = NULL;
599 drv_data.get_voltage = NULL;
600 drv_data.data = NULL;
601 pdata->driver_data = &drv_data;
602 }
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100603
David Brownelldad759f2008-12-01 00:43:58 +0100604 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
Balaji T Ke8deb282009-12-14 00:25:31 +0100605 sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
606 return add_numbered_child(sub_chip_id, "twl_reg", num,
David Brownelldad759f2008-12-01 00:43:58 +0100607 pdata, sizeof(*pdata), false, 0, 0);
608}
609
610static struct device *
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100611add_regulator(int num, struct regulator_init_data *pdata,
612 unsigned long features)
David Brownelldad759f2008-12-01 00:43:58 +0100613{
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100614 return add_regulator_linked(num, pdata, NULL, 0, features);
David Brownelldad759f2008-12-01 00:43:58 +0100615}
616
David Brownella603a7f2008-10-15 12:15:39 +0200617/*
David Brownella30d46c2008-10-20 23:46:28 +0200618 * NOTE: We know the first 8 IRQs after pdata->base_irq are
619 * for the PIH, and the next are for the PWR_INT SIH, since
620 * that's how twl_init_irq() sets things up.
David Brownella603a7f2008-10-15 12:15:39 +0200621 */
David Brownella603a7f2008-10-15 12:15:39 +0200622
David Brownelldad759f2008-12-01 00:43:58 +0100623static int
Felipe Balbi9e178622012-02-22 14:32:16 +0200624add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
625 unsigned long features)
David Brownella603a7f2008-10-15 12:15:39 +0200626{
David Brownell5725d662008-12-01 00:31:04 +0100627 struct device *child;
Balaji T Ke8deb282009-12-14 00:25:31 +0100628 unsigned sub_chip_id;
David Brownella603a7f2008-10-15 12:15:39 +0200629
Thierry Redingf78959c2012-09-18 10:29:50 +0200630 if (IS_ENABLED(CONFIG_GPIO_TWL4030) && pdata->gpio) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100631 child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
David Brownell5725d662008-12-01 00:31:04 +0100632 pdata->gpio, sizeof(*pdata->gpio),
Felipe Balbi9e178622012-02-22 14:32:16 +0200633 false, irq_base + GPIO_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100634 if (IS_ERR(child))
635 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200636 }
637
Thierry Redingf78959c2012-09-18 10:29:50 +0200638 if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030) && pdata->keypad) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100639 child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
David Brownell5725d662008-12-01 00:31:04 +0100640 pdata->keypad, sizeof(*pdata->keypad),
Felipe Balbi9e178622012-02-22 14:32:16 +0200641 true, irq_base + KEYPAD_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100642 if (IS_ERR(child))
643 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200644 }
645
Peter Ujfalusi24ae36f2012-11-13 09:28:42 +0100646 if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc &&
647 twl_class_is_4030()) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100648 child = add_child(SUB_CHIP_ID2, "twl4030_madc",
David Brownell5725d662008-12-01 00:31:04 +0100649 pdata->madc, sizeof(*pdata->madc),
Felipe Balbi9e178622012-02-22 14:32:16 +0200650 true, irq_base + MADC_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100651 if (IS_ERR(child))
652 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200653 }
654
Thierry Redingf78959c2012-09-18 10:29:50 +0200655 if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030)) {
David Brownella603a7f2008-10-15 12:15:39 +0200656 /*
David Brownell5725d662008-12-01 00:31:04 +0100657 * REVISIT platform_data here currently might expose the
David Brownella603a7f2008-10-15 12:15:39 +0200658 * "msecure" line ... but for now we just expect board
David Brownell5725d662008-12-01 00:31:04 +0100659 * setup to tell the chip "it's always ok to SET_TIME".
David Brownella603a7f2008-10-15 12:15:39 +0200660 * Eventually, Linux might become more aware of such
661 * HW security concerns, and "least privilege".
662 */
Balaji T Ke8deb282009-12-14 00:25:31 +0100663 sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100664 child = add_child(sub_chip_id, "twl_rtc", NULL, 0,
Felipe Balbi9e178622012-02-22 14:32:16 +0200665 true, irq_base + RTC_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100666 if (IS_ERR(child))
667 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200668 }
669
Peter Ujfalusiafc45892012-11-13 09:28:43 +0100670 if (IS_ENABLED(CONFIG_PWM_TWL)) {
671 child = add_child(SUB_CHIP_ID1, "twl-pwm", NULL, 0,
672 false, 0, 0);
673 if (IS_ERR(child))
674 return PTR_ERR(child);
675 }
676
677 if (IS_ENABLED(CONFIG_PWM_TWL_LED)) {
678 child = add_child(SUB_CHIP_ID1, "twl-pwmled", NULL, 0,
Thierry Reding48a364b2012-09-18 10:29:51 +0200679 false, 0, 0);
680 if (IS_ERR(child))
681 return PTR_ERR(child);
682 }
683
Thierry Redingf78959c2012-09-18 10:29:50 +0200684 if (IS_ENABLED(CONFIG_TWL4030_USB) && pdata->usb &&
685 twl_class_is_4030()) {
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300686
687 static struct regulator_consumer_supply usb1v5 = {
688 .supply = "usb1v5",
689 };
690 static struct regulator_consumer_supply usb1v8 = {
691 .supply = "usb1v8",
692 };
NeilBrownab378132012-05-09 07:40:40 +1000693 static struct regulator_consumer_supply usb3v1[] = {
694 { .supply = "usb3v1" },
695 { .supply = "bci3v1" },
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300696 };
697
698 /* First add the regulators so that they can be used by transceiver */
Thierry Redingf78959c2012-09-18 10:29:50 +0200699 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300700 /* this is a template that gets copied */
701 struct regulator_init_data usb_fixed = {
702 .constraints.valid_modes_mask =
703 REGULATOR_MODE_NORMAL
704 | REGULATOR_MODE_STANDBY,
705 .constraints.valid_ops_mask =
706 REGULATOR_CHANGE_MODE
707 | REGULATOR_CHANGE_STATUS,
708 };
709
710 child = add_regulator_linked(TWL4030_REG_VUSB1V5,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100711 &usb_fixed, &usb1v5, 1,
712 features);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300713 if (IS_ERR(child))
714 return PTR_ERR(child);
715
716 child = add_regulator_linked(TWL4030_REG_VUSB1V8,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100717 &usb_fixed, &usb1v8, 1,
718 features);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300719 if (IS_ERR(child))
720 return PTR_ERR(child);
721
722 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
NeilBrownab378132012-05-09 07:40:40 +1000723 &usb_fixed, usb3v1, 2,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100724 features);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300725 if (IS_ERR(child))
726 return PTR_ERR(child);
727
728 }
729
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100730 child = add_child(SUB_CHIP_ID0, "twl4030_usb",
731 pdata->usb, sizeof(*pdata->usb), true,
David Brownell5725d662008-12-01 00:31:04 +0100732 /* irq0 = USB_PRES, irq1 = USB */
Felipe Balbi9e178622012-02-22 14:32:16 +0200733 irq_base + USB_PRES_INTR_OFFSET,
734 irq_base + USB_INTR_OFFSET);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300735
David Brownell5725d662008-12-01 00:31:04 +0100736 if (IS_ERR(child))
737 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100738
739 /* we need to connect regulators to this transceiver */
Thierry Redingf78959c2012-09-18 10:29:50 +0200740 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child) {
Mark Brown1b65fa82012-02-03 11:02:58 +0000741 usb1v5.dev_name = dev_name(child);
742 usb1v8.dev_name = dev_name(child);
NeilBrownab378132012-05-09 07:40:40 +1000743 usb3v1[0].dev_name = dev_name(child);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300744 }
David Brownelldad759f2008-12-01 00:43:58 +0100745 }
Thierry Redingf78959c2012-09-18 10:29:50 +0200746 if (IS_ENABLED(CONFIG_TWL6030_USB) && pdata->usb &&
747 twl_class_is_6030()) {
Hema HKe70357e2010-12-10 18:09:52 +0530748
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100749 static struct regulator_consumer_supply usb3v3;
750 int regulator;
Hema HKe70357e2010-12-10 18:09:52 +0530751
Thierry Redingf78959c2012-09-18 10:29:50 +0200752 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
Hema HKe70357e2010-12-10 18:09:52 +0530753 /* this is a template that gets copied */
754 struct regulator_init_data usb_fixed = {
755 .constraints.valid_modes_mask =
756 REGULATOR_MODE_NORMAL
757 | REGULATOR_MODE_STANDBY,
758 .constraints.valid_ops_mask =
759 REGULATOR_CHANGE_MODE
760 | REGULATOR_CHANGE_STATUS,
761 };
762
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100763 if (features & TWL6025_SUBCLASS) {
764 usb3v3.supply = "ldousb";
765 regulator = TWL6025_REG_LDOUSB;
766 } else {
767 usb3v3.supply = "vusb";
768 regulator = TWL6030_REG_VUSB;
769 }
770 child = add_regulator_linked(regulator, &usb_fixed,
771 &usb3v3, 1,
772 features);
Hema HKe70357e2010-12-10 18:09:52 +0530773 if (IS_ERR(child))
774 return PTR_ERR(child);
775 }
776
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100777 pdata->usb->features = features;
778
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100779 child = add_child(SUB_CHIP_ID0, "twl6030_usb",
780 pdata->usb, sizeof(*pdata->usb), true,
Hema HKe70357e2010-12-10 18:09:52 +0530781 /* irq1 = VBUS_PRES, irq0 = USB ID */
Felipe Balbi9e178622012-02-22 14:32:16 +0200782 irq_base + USBOTG_INTR_OFFSET,
783 irq_base + USB_PRES_INTR_OFFSET);
Hema HKe70357e2010-12-10 18:09:52 +0530784
785 if (IS_ERR(child))
786 return PTR_ERR(child);
787 /* we need to connect regulators to this transceiver */
Thierry Redingf78959c2012-09-18 10:29:50 +0200788 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child)
Mark Brown1b65fa82012-02-03 11:02:58 +0000789 usb3v3.dev_name = dev_name(child);
Thierry Redingf78959c2012-09-18 10:29:50 +0200790 } else if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) &&
791 twl_class_is_6030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100792 if (features & TWL6025_SUBCLASS)
793 child = add_regulator(TWL6025_REG_LDOUSB,
794 pdata->ldousb, features);
795 else
796 child = add_regulator(TWL6030_REG_VUSB,
797 pdata->vusb, features);
Hema HKe70357e2010-12-10 18:09:52 +0530798
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100799 if (IS_ERR(child))
800 return PTR_ERR(child);
Hema HKe70357e2010-12-10 18:09:52 +0530801 }
David Brownelldad759f2008-12-01 00:43:58 +0100802
Thierry Redingf78959c2012-09-18 10:29:50 +0200803 if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG) && twl_class_is_4030()) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100804 child = add_child(SUB_CHIP_ID3, "twl4030_wdt", NULL, 0,
805 false, 0, 0);
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200806 if (IS_ERR(child))
807 return PTR_ERR(child);
808 }
809
Thierry Redingf78959c2012-09-18 10:29:50 +0200810 if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON) && twl_class_is_4030()) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100811 child = add_child(SUB_CHIP_ID3, "twl4030_pwrbutton", NULL, 0,
812 true, irq_base + 8 + 0, 0);
Felipe Balbi9c3664d2009-08-03 18:16:38 +0200813 if (IS_ERR(child))
814 return PTR_ERR(child);
815 }
816
Thierry Redingf78959c2012-09-18 10:29:50 +0200817 if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO) && pdata->audio &&
818 twl_class_is_4030()) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100819 child = add_child(SUB_CHIP_ID1, "twl4030-audio",
Peter Ujfalusi4ae6df52011-05-31 15:21:13 +0300820 pdata->audio, sizeof(*pdata->audio),
Misael Lopez Cruzd62abe52010-02-23 18:10:19 -0600821 false, 0, 0);
822 if (IS_ERR(child))
823 return PTR_ERR(child);
824 }
825
Rajendra Nayak9da66532009-12-13 22:29:47 +0100826 /* twl4030 regulators */
Thierry Redingf78959c2012-09-18 10:29:50 +0200827 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_4030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100828 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
829 features);
David Brownelldad759f2008-12-01 00:43:58 +0100830 if (IS_ERR(child))
831 return PTR_ERR(child);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100832
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100833 child = add_regulator(TWL4030_REG_VIO, pdata->vio,
834 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100835 if (IS_ERR(child))
836 return PTR_ERR(child);
837
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100838 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
839 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100840 if (IS_ERR(child))
841 return PTR_ERR(child);
842
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100843 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
844 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100845 if (IS_ERR(child))
846 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100847
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100848 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
849 features);
David Brownelldad759f2008-12-01 00:43:58 +0100850 if (IS_ERR(child))
851 return PTR_ERR(child);
852
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100853 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
854 features);
David Brownelldad759f2008-12-01 00:43:58 +0100855 if (IS_ERR(child))
856 return PTR_ERR(child);
857
858 child = add_regulator((features & TWL4030_VAUX2)
859 ? TWL4030_REG_VAUX2_4030
860 : TWL4030_REG_VAUX2,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100861 pdata->vaux2, features);
David Brownelldad759f2008-12-01 00:43:58 +0100862 if (IS_ERR(child))
863 return PTR_ERR(child);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100864
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100865 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
866 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100867 if (IS_ERR(child))
868 return PTR_ERR(child);
869
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100870 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
871 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100872 if (IS_ERR(child))
873 return PTR_ERR(child);
874
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100875 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
876 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100877 if (IS_ERR(child))
878 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100879 }
880
David Brownelldad759f2008-12-01 00:43:58 +0100881 /* maybe add LDOs that are omitted on cost-reduced parts */
Thierry Redingf78959c2012-09-18 10:29:50 +0200882 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && !(features & TPS_SUBSET)
Rajendra Nayak9da66532009-12-13 22:29:47 +0100883 && twl_class_is_4030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100884 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
885 features);
David Brownelldad759f2008-12-01 00:43:58 +0100886 if (IS_ERR(child))
887 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100888
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100889 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
890 features);
David Brownelldad759f2008-12-01 00:43:58 +0100891 if (IS_ERR(child))
892 return PTR_ERR(child);
893
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100894 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
895 features);
David Brownelldad759f2008-12-01 00:43:58 +0100896 if (IS_ERR(child))
897 return PTR_ERR(child);
898
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100899 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
900 features);
David Brownelldad759f2008-12-01 00:43:58 +0100901 if (IS_ERR(child))
902 return PTR_ERR(child);
903
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100904 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
905 features);
David Brownelldad759f2008-12-01 00:43:58 +0100906 if (IS_ERR(child))
907 return PTR_ERR(child);
908
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100909 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
910 features);
David Brownelldad759f2008-12-01 00:43:58 +0100911 if (IS_ERR(child))
912 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200913 }
914
Rajendra Nayak9da66532009-12-13 22:29:47 +0100915 /* twl6030 regulators */
Thierry Redingf78959c2012-09-18 10:29:50 +0200916 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100917 !(features & TWL6025_SUBCLASS)) {
Tero Kristo34a38442012-02-28 15:09:10 +0530918 child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1,
919 features);
920 if (IS_ERR(child))
921 return PTR_ERR(child);
922
923 child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2,
924 features);
925 if (IS_ERR(child))
926 return PTR_ERR(child);
927
928 child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3,
929 features);
930 if (IS_ERR(child))
931 return PTR_ERR(child);
932
Peter Ujfalusi46eda3e2012-02-28 15:09:13 +0530933 child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8,
934 features);
935 if (IS_ERR(child))
936 return PTR_ERR(child);
937
938 child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1,
939 features);
940 if (IS_ERR(child))
941 return PTR_ERR(child);
942
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100943 child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
944 features);
945 if (IS_ERR(child))
946 return PTR_ERR(child);
947
948 child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
949 features);
950 if (IS_ERR(child))
951 return PTR_ERR(child);
952
953 child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
954 features);
955 if (IS_ERR(child))
956 return PTR_ERR(child);
957
958 child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
959 features);
960 if (IS_ERR(child))
961 return PTR_ERR(child);
962
963 child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
964 features);
965 if (IS_ERR(child))
966 return PTR_ERR(child);
967
968 child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
969 features);
970 if (IS_ERR(child))
971 return PTR_ERR(child);
972
973 child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
974 features);
975 if (IS_ERR(child))
976 return PTR_ERR(child);
977
978 child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
979 features);
980 if (IS_ERR(child))
981 return PTR_ERR(child);
982
983 child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
984 features);
985 if (IS_ERR(child))
986 return PTR_ERR(child);
987 }
988
989 /* 6030 and 6025 share this regulator */
Thierry Redingf78959c2012-09-18 10:29:50 +0200990 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100991 child = add_regulator(TWL6030_REG_VANA, pdata->vana,
992 features);
993 if (IS_ERR(child))
994 return PTR_ERR(child);
995 }
996
997 /* twl6025 regulators */
Thierry Redingf78959c2012-09-18 10:29:50 +0200998 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100999 (features & TWL6025_SUBCLASS)) {
1000 child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
1001 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001002 if (IS_ERR(child))
1003 return PTR_ERR(child);
1004
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001005 child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
1006 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001007 if (IS_ERR(child))
1008 return PTR_ERR(child);
1009
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001010 child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
1011 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001012 if (IS_ERR(child))
1013 return PTR_ERR(child);
1014
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001015 child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
1016 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001017 if (IS_ERR(child))
1018 return PTR_ERR(child);
1019
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001020 child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
1021 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001022 if (IS_ERR(child))
1023 return PTR_ERR(child);
1024
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001025 child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
1026 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001027 if (IS_ERR(child))
1028 return PTR_ERR(child);
1029
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001030 child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
1031 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001032 if (IS_ERR(child))
1033 return PTR_ERR(child);
1034
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001035 child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
1036 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001037 if (IS_ERR(child))
1038 return PTR_ERR(child);
1039
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001040 child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
1041 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001042 if (IS_ERR(child))
1043 return PTR_ERR(child);
Balaji T K8e6de4a2011-02-10 18:44:50 +05301044
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001045 child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
1046 features);
Balaji T K8e6de4a2011-02-10 18:44:50 +05301047 if (IS_ERR(child))
1048 return PTR_ERR(child);
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001049
1050 child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
1051 features);
1052 if (IS_ERR(child))
1053 return PTR_ERR(child);
1054
Rajendra Nayak9da66532009-12-13 22:29:47 +01001055 }
1056
Thierry Redingf78959c2012-09-18 10:29:50 +02001057 if (IS_ENABLED(CONFIG_CHARGER_TWL4030) && pdata->bci &&
Grazvydas Ignotas11c39c42010-10-01 13:17:50 +03001058 !(features & (TPS_SUBSET | TWL5031))) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +01001059 child = add_child(SUB_CHIP_ID3, "twl4030_bci",
Grazvydas Ignotas11c39c42010-10-01 13:17:50 +03001060 pdata->bci, sizeof(*pdata->bci), false,
1061 /* irq0 = CHG_PRES, irq1 = BCI */
Felipe Balbi9e178622012-02-22 14:32:16 +02001062 irq_base + BCI_PRES_INTR_OFFSET,
1063 irq_base + BCI_INTR_OFFSET);
Grazvydas Ignotas11c39c42010-10-01 13:17:50 +03001064 if (IS_ERR(child))
1065 return PTR_ERR(child);
1066 }
1067
David Brownell5725d662008-12-01 00:31:04 +01001068 return 0;
David Brownella603a7f2008-10-15 12:15:39 +02001069}
1070
1071/*----------------------------------------------------------------------*/
1072
1073/*
1074 * These three functions initialize the on-chip clock framework,
1075 * letting it generate the right frequencies for USB, MADC, and
1076 * other purposes.
1077 */
1078static inline int __init protect_pm_master(void)
1079{
1080 int e = 0;
1081
Felipe Balbi49e6f872010-08-18 09:19:35 +03001082 e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
1083 TWL4030_PM_MASTER_PROTECT_KEY);
David Brownella603a7f2008-10-15 12:15:39 +02001084 return e;
1085}
1086
1087static inline int __init unprotect_pm_master(void)
1088{
1089 int e = 0;
1090
Felipe Balbi49e6f872010-08-18 09:19:35 +03001091 e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1092 TWL4030_PM_MASTER_KEY_CFG1,
1093 TWL4030_PM_MASTER_PROTECT_KEY);
1094 e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1095 TWL4030_PM_MASTER_KEY_CFG2,
1096 TWL4030_PM_MASTER_PROTECT_KEY);
1097
David Brownella603a7f2008-10-15 12:15:39 +02001098 return e;
1099}
1100
Ilkka Koskinen38a68492009-10-22 14:14:09 +03001101static void clocks_init(struct device *dev,
1102 struct twl4030_clock_init_data *clock)
David Brownella603a7f2008-10-15 12:15:39 +02001103{
1104 int e = 0;
1105 struct clk *osc;
1106 u32 rate;
1107 u8 ctrl = HFCLK_FREQ_26_MHZ;
1108
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001109 osc = clk_get(dev, "fck");
David Brownella603a7f2008-10-15 12:15:39 +02001110 if (IS_ERR(osc)) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001111 printk(KERN_WARNING "Skipping twl internal clock init and "
David Brownella603a7f2008-10-15 12:15:39 +02001112 "using bootloader value (unknown osc rate)\n");
1113 return;
1114 }
1115
1116 rate = clk_get_rate(osc);
1117 clk_put(osc);
1118
1119 switch (rate) {
1120 case 19200000:
1121 ctrl = HFCLK_FREQ_19p2_MHZ;
1122 break;
1123 case 26000000:
1124 ctrl = HFCLK_FREQ_26_MHZ;
1125 break;
1126 case 38400000:
1127 ctrl = HFCLK_FREQ_38p4_MHZ;
1128 break;
1129 }
1130
1131 ctrl |= HIGH_PERF_SQ;
Ilkka Koskinen38a68492009-10-22 14:14:09 +03001132 if (clock && clock->ck32k_lowpwr_enable)
1133 ctrl |= CK32K_LOWPWR_EN;
1134
David Brownella603a7f2008-10-15 12:15:39 +02001135 e |= unprotect_pm_master();
1136 /* effect->MADC+USB ck en */
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001137 e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
David Brownella603a7f2008-10-15 12:15:39 +02001138 e |= protect_pm_master();
1139
1140 if (e < 0)
1141 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
1142}
1143
1144/*----------------------------------------------------------------------*/
1145
David Brownella603a7f2008-10-15 12:15:39 +02001146
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001147static int twl_remove(struct i2c_client *client)
David Brownella603a7f2008-10-15 12:15:39 +02001148{
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001149 unsigned i, num_slaves;
David Brownella30d46c2008-10-20 23:46:28 +02001150 int status;
David Brownella603a7f2008-10-15 12:15:39 +02001151
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001152 if (twl_class_is_4030()) {
Balaji T Ke8deb282009-12-14 00:25:31 +01001153 status = twl4030_exit_irq();
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001154 num_slaves = TWL_NUM_SLAVES;
1155 } else {
Balaji T Ke8deb282009-12-14 00:25:31 +01001156 status = twl6030_exit_irq();
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001157 num_slaves = TWL_NUM_SLAVES - 1;
1158 }
Balaji T Ke8deb282009-12-14 00:25:31 +01001159
David Brownella30d46c2008-10-20 23:46:28 +02001160 if (status < 0)
1161 return status;
David Brownella603a7f2008-10-15 12:15:39 +02001162
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001163 for (i = 0; i < num_slaves; i++) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001164 struct twl_client *twl = &twl_modules[i];
David Brownella603a7f2008-10-15 12:15:39 +02001165
1166 if (twl->client && twl->client != client)
1167 i2c_unregister_device(twl->client);
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001168 twl_modules[i].client = NULL;
David Brownella603a7f2008-10-15 12:15:39 +02001169 }
1170 inuse = false;
1171 return 0;
1172}
1173
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001174/* NOTE: This driver only handles a single twl4030/tps659x0 chip */
Bryan Wu5b9cecd2010-12-08 10:42:04 +08001175static int __devinit
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001176twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
David Brownella603a7f2008-10-15 12:15:39 +02001177{
David Brownella603a7f2008-10-15 12:15:39 +02001178 struct twl4030_platform_data *pdata = client->dev.platform_data;
Benoit Coussonaeb50322011-08-29 16:20:23 +02001179 struct device_node *node = client->dev.of_node;
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001180 struct platform_device *pdev;
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001181 struct regmap_config *twl_regmap_config;
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001182 int irq_base = 0;
1183 int status;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001184 unsigned i, num_slaves;
Benoit Coussonaeb50322011-08-29 16:20:23 +02001185
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001186 pdev = platform_device_alloc(DRIVER_NAME, -1);
1187 if (!pdev) {
1188 dev_err(&client->dev, "can't alloc pdev\n");
1189 return -ENOMEM;
1190 }
1191
1192 status = platform_device_add(pdev);
1193 if (status) {
1194 platform_device_put(pdev);
1195 return status;
1196 }
1197
Benoit Coussonaeb50322011-08-29 16:20:23 +02001198 if (node && !pdata) {
1199 /*
1200 * XXX: Temporary pdata until the information is correctly
1201 * retrieved by every TWL modules from DT.
1202 */
1203 pdata = devm_kzalloc(&client->dev,
1204 sizeof(struct twl4030_platform_data),
1205 GFP_KERNEL);
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001206 if (!pdata) {
1207 status = -ENOMEM;
1208 goto free;
1209 }
Benoit Coussonaeb50322011-08-29 16:20:23 +02001210 }
David Brownella603a7f2008-10-15 12:15:39 +02001211
1212 if (!pdata) {
1213 dev_dbg(&client->dev, "no platform data?\n");
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001214 status = -EINVAL;
1215 goto free;
David Brownella603a7f2008-10-15 12:15:39 +02001216 }
1217
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001218 platform_set_drvdata(pdev, pdata);
1219
David Brownella603a7f2008-10-15 12:15:39 +02001220 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1221 dev_dbg(&client->dev, "can't talk I2C?\n");
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001222 status = -EIO;
1223 goto free;
David Brownella603a7f2008-10-15 12:15:39 +02001224 }
1225
David Brownella30d46c2008-10-20 23:46:28 +02001226 if (inuse) {
David Brownella603a7f2008-10-15 12:15:39 +02001227 dev_dbg(&client->dev, "driver is already in use\n");
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001228 status = -EBUSY;
1229 goto free;
David Brownella603a7f2008-10-15 12:15:39 +02001230 }
1231
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001232 if ((id->driver_data) & TWL6030_CLASS) {
1233 twl_id = TWL6030_CLASS_ID;
1234 twl_map = &twl6030_map[0];
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001235 twl_regmap_config = twl6030_regmap_config;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001236 num_slaves = TWL_NUM_SLAVES - 1;
1237 } else {
1238 twl_id = TWL4030_CLASS_ID;
1239 twl_map = &twl4030_map[0];
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001240 twl_regmap_config = twl4030_regmap_config;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001241 num_slaves = TWL_NUM_SLAVES;
1242 }
1243
1244 for (i = 0; i < num_slaves; i++) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001245 struct twl_client *twl = &twl_modules[i];
David Brownella603a7f2008-10-15 12:15:39 +02001246
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001247 if (i == 0) {
David Brownella603a7f2008-10-15 12:15:39 +02001248 twl->client = client;
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001249 } else {
David Brownella603a7f2008-10-15 12:15:39 +02001250 twl->client = i2c_new_dummy(client->adapter,
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001251 client->addr + i);
David Brownella603a7f2008-10-15 12:15:39 +02001252 if (!twl->client) {
Ilkka Koskinena8643432009-10-20 16:22:52 +03001253 dev_err(&client->dev,
David Brownella603a7f2008-10-15 12:15:39 +02001254 "can't attach client %d\n", i);
1255 status = -ENOMEM;
1256 goto fail;
1257 }
David Brownella603a7f2008-10-15 12:15:39 +02001258 }
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001259
1260 twl->regmap = devm_regmap_init_i2c(twl->client,
1261 &twl_regmap_config[i]);
1262 if (IS_ERR(twl->regmap)) {
1263 status = PTR_ERR(twl->regmap);
1264 dev_err(&client->dev,
1265 "Failed to allocate regmap %d, err: %d\n", i,
1266 status);
1267 goto fail;
1268 }
David Brownella603a7f2008-10-15 12:15:39 +02001269 }
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001270
David Brownella603a7f2008-10-15 12:15:39 +02001271 inuse = true;
1272
1273 /* setup clock framework */
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001274 clocks_init(&pdev->dev, pdata->clock);
David Brownella603a7f2008-10-15 12:15:39 +02001275
Lesly A Mca972d12011-04-14 17:57:53 +05301276 /* read TWL IDCODE Register */
1277 if (twl_id == TWL4030_CLASS_ID) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001278 status = twl_read_idcode_register();
1279 WARN(status < 0, "Error: reading twl_idcode register value\n");
Lesly A Mca972d12011-04-14 17:57:53 +05301280 }
1281
Amit Kucheriaebf0bd32009-08-31 18:32:18 +02001282 /* load power event scripts */
Thierry Redingf78959c2012-09-18 10:29:50 +02001283 if (IS_ENABLED(CONFIG_TWL4030_POWER) && pdata->power)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +02001284 twl4030_power_init(pdata->power);
1285
David Brownella603a7f2008-10-15 12:15:39 +02001286 /* Maybe init the T2 Interrupt subsystem */
Felipe Balbi9e178622012-02-22 14:32:16 +02001287 if (client->irq) {
Balaji T Ke8deb282009-12-14 00:25:31 +01001288 if (twl_class_is_4030()) {
1289 twl4030_init_chip_irq(id->name);
Benoit Cousson78518ff2012-02-29 19:40:31 +01001290 irq_base = twl4030_init_irq(&client->dev, client->irq);
Balaji T Ke8deb282009-12-14 00:25:31 +01001291 } else {
Benoit Cousson78518ff2012-02-29 19:40:31 +01001292 irq_base = twl6030_init_irq(&client->dev, client->irq);
Balaji T Ke8deb282009-12-14 00:25:31 +01001293 }
1294
Benoit Cousson78518ff2012-02-29 19:40:31 +01001295 if (irq_base < 0) {
1296 status = irq_base;
David Brownella30d46c2008-10-20 23:46:28 +02001297 goto fail;
Benoit Cousson78518ff2012-02-29 19:40:31 +01001298 }
David Brownella603a7f2008-10-15 12:15:39 +02001299 }
1300
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001301 /*
1302 * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001303 * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1304 * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1305 */
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001306 if (twl_class_is_4030()) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001307 u8 temp;
1308
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001309 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1310 temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001311 I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001312 twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1313 }
1314
Grant Likely964dba22012-02-24 14:58:34 -07001315 status = -ENODEV;
Benoit Coussonaeb50322011-08-29 16:20:23 +02001316 if (node)
1317 status = of_platform_populate(node, NULL, NULL, &client->dev);
Grant Likely964dba22012-02-24 14:58:34 -07001318 if (status)
Felipe Balbi9e178622012-02-22 14:32:16 +02001319 status = add_children(pdata, irq_base, id->driver_data);
Benoit Coussonaeb50322011-08-29 16:20:23 +02001320
David Brownella603a7f2008-10-15 12:15:39 +02001321fail:
1322 if (status < 0)
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001323 twl_remove(client);
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001324free:
1325 if (status < 0)
1326 platform_device_unregister(pdev);
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001327
David Brownella603a7f2008-10-15 12:15:39 +02001328 return status;
1329}
1330
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001331static const struct i2c_device_id twl_ids[] = {
David Brownelldad759f2008-12-01 00:43:58 +01001332 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
1333 { "twl5030", 0 }, /* T2 updated */
Ilkka Koskinen1920a612009-11-10 17:26:15 +02001334 { "twl5031", TWL5031 }, /* TWL5030 updated */
David Brownelldad759f2008-12-01 00:43:58 +01001335 { "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 */
Oleg Drokin59dead52011-07-01 13:34:13 +02001338 { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
1339 and vibrator. Charger in USB module*/
Balaji T Ke8deb282009-12-14 00:25:31 +01001340 { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001341 { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
David Brownella603a7f2008-10-15 12:15:39 +02001342 { /* end of list */ },
1343};
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001344MODULE_DEVICE_TABLE(i2c, twl_ids);
David Brownella603a7f2008-10-15 12:15:39 +02001345
1346/* One Client Driver , 4 Clients */
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001347static struct i2c_driver twl_driver = {
David Brownella603a7f2008-10-15 12:15:39 +02001348 .driver.name = DRIVER_NAME,
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001349 .id_table = twl_ids,
1350 .probe = twl_probe,
1351 .remove = twl_remove,
David Brownella603a7f2008-10-15 12:15:39 +02001352};
1353
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001354static int __init twl_init(void)
David Brownella603a7f2008-10-15 12:15:39 +02001355{
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001356 return i2c_add_driver(&twl_driver);
David Brownella603a7f2008-10-15 12:15:39 +02001357}
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001358subsys_initcall(twl_init);
David Brownella603a7f2008-10-15 12:15:39 +02001359
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001360static void __exit twl_exit(void)
David Brownella603a7f2008-10-15 12:15:39 +02001361{
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001362 i2c_del_driver(&twl_driver);
David Brownella603a7f2008-10-15 12:15:39 +02001363}
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001364module_exit(twl_exit);
David Brownella603a7f2008-10-15 12:15:39 +02001365
1366MODULE_AUTHOR("Texas Instruments, Inc.");
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001367MODULE_DESCRIPTION("I2C Core interface for TWL");
David Brownella603a7f2008-10-15 12:15:39 +02001368MODULE_LICENSE("GPL");