blob: bb33b52cfa57fd36a836b4c9faae1d47d96d2d40 [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
Balaji T Kfc7b92f2009-12-13 21:23:33 +010069#define TWL_NUM_SLAVES 4
David Brownella603a7f2008-10-15 12:15:39 +020070
Balaji T Kfc7b92f2009-12-13 21:23:33 +010071#define SUB_CHIP_ID0 0
72#define SUB_CHIP_ID1 1
73#define SUB_CHIP_ID2 2
74#define SUB_CHIP_ID3 3
Peter Ujfalusi364cedb2012-01-04 14:58:33 +020075#define SUB_CHIP_ID_INVAL 0xff
Balaji T Kfc7b92f2009-12-13 21:23:33 +010076
77#define TWL_MODULE_LAST TWL4030_MODULE_LAST
78
David Brownella603a7f2008-10-15 12:15:39 +020079/* Base Address defns for twl4030_map[] */
80
81/* subchip/slave 0 - USB ID */
82#define TWL4030_BASEADD_USB 0x0000
83
84/* subchip/slave 1 - AUD ID */
85#define TWL4030_BASEADD_AUDIO_VOICE 0x0000
86#define TWL4030_BASEADD_GPIO 0x0098
87#define TWL4030_BASEADD_INTBR 0x0085
88#define TWL4030_BASEADD_PIH 0x0080
89#define TWL4030_BASEADD_TEST 0x004C
90
91/* subchip/slave 2 - AUX ID */
92#define TWL4030_BASEADD_INTERRUPTS 0x00B9
93#define TWL4030_BASEADD_LED 0x00EE
94#define TWL4030_BASEADD_MADC 0x0000
95#define TWL4030_BASEADD_MAIN_CHARGE 0x0074
96#define TWL4030_BASEADD_PRECHARGE 0x00AA
97#define TWL4030_BASEADD_PWM0 0x00F8
98#define TWL4030_BASEADD_PWM1 0x00FB
99#define TWL4030_BASEADD_PWMA 0x00EF
100#define TWL4030_BASEADD_PWMB 0x00F1
101#define TWL4030_BASEADD_KEYPAD 0x00D2
102
Ilkka Koskinen1920a612009-11-10 17:26:15 +0200103#define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
104#define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
105 one */
106
David Brownella603a7f2008-10-15 12:15:39 +0200107/* subchip/slave 3 - POWER ID */
108#define TWL4030_BASEADD_BACKUP 0x0014
109#define TWL4030_BASEADD_INT 0x002E
110#define TWL4030_BASEADD_PM_MASTER 0x0036
111#define TWL4030_BASEADD_PM_RECEIVER 0x005B
112#define TWL4030_BASEADD_RTC 0x001C
113#define TWL4030_BASEADD_SECURED_REG 0x0000
114
115/* Triton Core internal information (END) */
116
117
Balaji T Ke8deb282009-12-14 00:25:31 +0100118/* subchip/slave 0 0x48 - POWER */
119#define TWL6030_BASEADD_RTC 0x0000
120#define TWL6030_BASEADD_MEM 0x0017
121#define TWL6030_BASEADD_PM_MASTER 0x001F
122#define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
123#define TWL6030_BASEADD_PM_MISC 0x00E2
124#define TWL6030_BASEADD_PM_PUPD 0x00F0
125
126/* subchip/slave 1 0x49 - FEATURE */
127#define TWL6030_BASEADD_USB 0x0000
128#define TWL6030_BASEADD_GPADC_CTRL 0x002E
129#define TWL6030_BASEADD_AUX 0x0090
130#define TWL6030_BASEADD_PWM 0x00BA
131#define TWL6030_BASEADD_GASGAUGE 0x00C0
132#define TWL6030_BASEADD_PIH 0x00D0
133#define TWL6030_BASEADD_CHARGER 0x00E0
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100134#define TWL6025_BASEADD_CHARGER 0x00DA
Balaji T Ke8deb282009-12-14 00:25:31 +0100135
136/* subchip/slave 2 0x4A - DFT */
137#define TWL6030_BASEADD_DIEID 0x00C0
138
139/* subchip/slave 3 0x4B - AUDIO */
140#define TWL6030_BASEADD_AUDIO 0x0000
141#define TWL6030_BASEADD_RSV 0x0000
Balaji T Kfa0d9762010-02-19 12:39:38 +0100142#define TWL6030_BASEADD_ZERO 0x0000
Balaji T Ke8deb282009-12-14 00:25:31 +0100143
David Brownella603a7f2008-10-15 12:15:39 +0200144/* Few power values */
145#define R_CFG_BOOT 0x05
David Brownella603a7f2008-10-15 12:15:39 +0200146
147/* some fields in R_CFG_BOOT */
148#define HFCLK_FREQ_19p2_MHZ (1 << 0)
149#define HFCLK_FREQ_26_MHZ (2 << 0)
150#define HFCLK_FREQ_38p4_MHZ (3 << 0)
151#define HIGH_PERF_SQ (1 << 3)
Ilkka Koskinen38a68492009-10-22 14:14:09 +0300152#define CK32K_LOWPWR_EN (1 << 7)
David Brownella603a7f2008-10-15 12:15:39 +0200153
David Brownella603a7f2008-10-15 12:15:39 +0200154/*----------------------------------------------------------------------*/
155
David Brownella603a7f2008-10-15 12:15:39 +0200156/* is driver active, bound to a chip? */
157static bool inuse;
158
Lesly A Mca972d12011-04-14 17:57:53 +0530159/* TWL IDCODE Register value */
160static u32 twl_idcode;
161
Balaji T Ke8deb282009-12-14 00:25:31 +0100162static unsigned int twl_id;
163unsigned int twl_rev(void)
164{
165 return twl_id;
166}
167EXPORT_SYMBOL(twl_rev);
168
169/* Structure for each TWL4030/TWL6030 Slave */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100170struct twl_client {
David Brownella603a7f2008-10-15 12:15:39 +0200171 struct i2c_client *client;
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100172 struct regmap *regmap;
David Brownella603a7f2008-10-15 12:15:39 +0200173};
174
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100175static struct twl_client twl_modules[TWL_NUM_SLAVES];
David Brownella603a7f2008-10-15 12:15:39 +0200176
David Brownella603a7f2008-10-15 12:15:39 +0200177/* mapping the module id to slave id and base address */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100178struct twl_mapping {
David Brownella603a7f2008-10-15 12:15:39 +0200179 unsigned char sid; /* Slave ID */
180 unsigned char base; /* base address */
181};
G, Manjunath Kondaiah2cfcce12010-09-21 15:31:17 +0530182static struct twl_mapping *twl_map;
David Brownella603a7f2008-10-15 12:15:39 +0200183
Peter Ujfalusida059ec2012-11-13 09:28:48 +0100184static struct twl_mapping twl4030_map[] = {
David Brownella603a7f2008-10-15 12:15:39 +0200185 /*
186 * NOTE: don't change this table without updating the
Balaji T Ke8deb282009-12-14 00:25:31 +0100187 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
David Brownella603a7f2008-10-15 12:15:39 +0200188 * so they continue to match the order in this table.
189 */
190
191 { 0, TWL4030_BASEADD_USB },
192
193 { 1, TWL4030_BASEADD_AUDIO_VOICE },
194 { 1, TWL4030_BASEADD_GPIO },
195 { 1, TWL4030_BASEADD_INTBR },
196 { 1, TWL4030_BASEADD_PIH },
197 { 1, TWL4030_BASEADD_TEST },
198
199 { 2, TWL4030_BASEADD_KEYPAD },
200 { 2, TWL4030_BASEADD_MADC },
201 { 2, TWL4030_BASEADD_INTERRUPTS },
202 { 2, TWL4030_BASEADD_LED },
203 { 2, TWL4030_BASEADD_MAIN_CHARGE },
204 { 2, TWL4030_BASEADD_PRECHARGE },
205 { 2, TWL4030_BASEADD_PWM0 },
206 { 2, TWL4030_BASEADD_PWM1 },
207 { 2, TWL4030_BASEADD_PWMA },
208 { 2, TWL4030_BASEADD_PWMB },
Ilkka Koskinen1920a612009-11-10 17:26:15 +0200209 { 2, TWL5031_BASEADD_ACCESSORY },
210 { 2, TWL5031_BASEADD_INTERRUPTS },
David Brownella603a7f2008-10-15 12:15:39 +0200211
212 { 3, TWL4030_BASEADD_BACKUP },
213 { 3, TWL4030_BASEADD_INT },
214 { 3, TWL4030_BASEADD_PM_MASTER },
215 { 3, TWL4030_BASEADD_PM_RECEIVER },
216 { 3, TWL4030_BASEADD_RTC },
217 { 3, TWL4030_BASEADD_SECURED_REG },
218};
219
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100220static struct regmap_config twl4030_regmap_config[4] = {
221 {
222 /* Address 0x48 */
223 .reg_bits = 8,
224 .val_bits = 8,
225 .max_register = 0xff,
226 },
227 {
228 /* Address 0x49 */
229 .reg_bits = 8,
230 .val_bits = 8,
231 .max_register = 0xff,
232 },
233 {
234 /* Address 0x4a */
235 .reg_bits = 8,
236 .val_bits = 8,
237 .max_register = 0xff,
238 },
239 {
240 /* Address 0x4b */
241 .reg_bits = 8,
242 .val_bits = 8,
243 .max_register = 0xff,
244 },
245};
246
Balaji T Ke8deb282009-12-14 00:25:31 +0100247static struct twl_mapping twl6030_map[] = {
248 /*
249 * NOTE: don't change this table without updating the
250 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
251 * so they continue to match the order in this table.
252 */
253 { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
Peter Ujfalusi364cedb2012-01-04 14:58:33 +0200254 { SUB_CHIP_ID_INVAL, TWL6030_BASEADD_AUDIO },
Balaji T Ke8deb282009-12-14 00:25:31 +0100255 { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
256 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
257 { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
258
259 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
260 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
261 { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
262 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
263 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
264
265 { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
266 { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
267 { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
Balaji T Kfa0d9762010-02-19 12:39:38 +0100268 { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
269 { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
Balaji T Ke8deb282009-12-14 00:25:31 +0100270
Balaji T Kfa0d9762010-02-19 12:39:38 +0100271 { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
272 { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
Balaji T Ke8deb282009-12-14 00:25:31 +0100273 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
274 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
275 { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
276 { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
277 { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
278
279 { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
280 { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100281 { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
Balaji T Ke8deb282009-12-14 00:25:31 +0100282};
283
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100284static struct regmap_config twl6030_regmap_config[3] = {
285 {
286 /* Address 0x48 */
287 .reg_bits = 8,
288 .val_bits = 8,
289 .max_register = 0xff,
290 },
291 {
292 /* Address 0x49 */
293 .reg_bits = 8,
294 .val_bits = 8,
295 .max_register = 0xff,
296 },
297 {
298 /* Address 0x4a */
299 .reg_bits = 8,
300 .val_bits = 8,
301 .max_register = 0xff,
302 },
303};
304
David Brownella603a7f2008-10-15 12:15:39 +0200305/*----------------------------------------------------------------------*/
306
David Brownella603a7f2008-10-15 12:15:39 +0200307/* Exported Functions */
308
309/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100310 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200311 * @mod_no: module number
312 * @value: an array of num_bytes+1 containing data to write
313 * @reg: register address (just offset will do)
314 * @num_bytes: number of bytes to transfer
315 *
316 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
317 * valid data starts at Offset 1.
318 *
319 * Returns the result of operation - 0 is success
320 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100321int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200322{
323 int ret;
324 int sid;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100325 struct twl_client *twl;
David Brownella603a7f2008-10-15 12:15:39 +0200326
Peter Ujfalusida059ec2012-11-13 09:28:48 +0100327 if (unlikely(mod_no >= TWL_MODULE_LAST)) {
David Brownella603a7f2008-10-15 12:15:39 +0200328 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
329 return -EPERM;
330 }
Ilya Yanok8653be12011-08-01 23:00:28 +0200331 if (unlikely(!inuse)) {
332 pr_err("%s: not initialized\n", DRIVER_NAME);
333 return -EPERM;
334 }
Balaji T Ke8deb282009-12-14 00:25:31 +0100335 sid = twl_map[mod_no].sid;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +0200336 if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
337 pr_err("%s: module %d is not part of the pmic\n",
338 DRIVER_NAME, mod_no);
339 return -EINVAL;
340 }
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100341 twl = &twl_modules[sid];
David Brownella603a7f2008-10-15 12:15:39 +0200342
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100343 ret = regmap_bulk_write(twl->regmap, twl_map[mod_no].base + reg,
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100344 value, num_bytes);
David Brownella603a7f2008-10-15 12:15:39 +0200345
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100346 if (ret)
347 pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n",
348 DRIVER_NAME, mod_no, reg, num_bytes);
349
350 return ret;
David Brownella603a7f2008-10-15 12:15:39 +0200351}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100352EXPORT_SYMBOL(twl_i2c_write);
David Brownella603a7f2008-10-15 12:15:39 +0200353
354/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100355 * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200356 * @mod_no: module number
357 * @value: an array of num_bytes containing data to be read
358 * @reg: register address (just offset will do)
359 * @num_bytes: number of bytes to transfer
360 *
361 * Returns result of operation - num_bytes is success else failure.
362 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100363int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200364{
365 int ret;
David Brownella603a7f2008-10-15 12:15:39 +0200366 int sid;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100367 struct twl_client *twl;
David Brownella603a7f2008-10-15 12:15:39 +0200368
Peter Ujfalusida059ec2012-11-13 09:28:48 +0100369 if (unlikely(mod_no >= TWL_MODULE_LAST)) {
David Brownella603a7f2008-10-15 12:15:39 +0200370 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
371 return -EPERM;
372 }
Ilya Yanok8653be12011-08-01 23:00:28 +0200373 if (unlikely(!inuse)) {
374 pr_err("%s: not initialized\n", DRIVER_NAME);
375 return -EPERM;
376 }
Balaji T Ke8deb282009-12-14 00:25:31 +0100377 sid = twl_map[mod_no].sid;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +0200378 if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
379 pr_err("%s: module %d is not part of the pmic\n",
380 DRIVER_NAME, mod_no);
381 return -EINVAL;
382 }
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100383 twl = &twl_modules[sid];
David Brownella603a7f2008-10-15 12:15:39 +0200384
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100385 ret = regmap_bulk_read(twl->regmap, twl_map[mod_no].base + reg,
386 value, num_bytes);
David Brownella603a7f2008-10-15 12:15:39 +0200387
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100388 if (ret)
389 pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n",
390 DRIVER_NAME, mod_no, reg, num_bytes);
391
392 return ret;
David Brownella603a7f2008-10-15 12:15:39 +0200393}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100394EXPORT_SYMBOL(twl_i2c_read);
David Brownella603a7f2008-10-15 12:15:39 +0200395
396/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100397 * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200398 * @mod_no: module number
399 * @value: the value to be written 8 bit
400 * @reg: register address (just offset will do)
401 *
402 * Returns result of operation - 0 is success
403 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100404int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
David Brownella603a7f2008-10-15 12:15:39 +0200405{
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100406 return twl_i2c_write(mod_no, &value, reg, 1);
David Brownella603a7f2008-10-15 12:15:39 +0200407}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100408EXPORT_SYMBOL(twl_i2c_write_u8);
David Brownella603a7f2008-10-15 12:15:39 +0200409
410/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100411 * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200412 * @mod_no: module number
413 * @value: the value read 8 bit
414 * @reg: register address (just offset will do)
415 *
416 * Returns result of operation - 0 is success
417 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100418int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
David Brownella603a7f2008-10-15 12:15:39 +0200419{
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100420 return twl_i2c_read(mod_no, value, reg, 1);
David Brownella603a7f2008-10-15 12:15:39 +0200421}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100422EXPORT_SYMBOL(twl_i2c_read_u8);
David Brownella603a7f2008-10-15 12:15:39 +0200423
424/*----------------------------------------------------------------------*/
425
Lesly A Mca972d12011-04-14 17:57:53 +0530426/**
427 * twl_read_idcode_register - API to read the IDCODE register.
428 *
429 * Unlocks the IDCODE register and read the 32 bit value.
430 */
431static int twl_read_idcode_register(void)
432{
433 int err;
434
435 err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
436 REG_UNLOCK_TEST_REG);
437 if (err) {
438 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
439 goto fail;
440 }
441
442 err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
443 REG_IDCODE_7_0, 4);
444 if (err) {
445 pr_err("TWL4030: unable to read IDCODE -%d\n", err);
446 goto fail;
447 }
448
449 err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
450 if (err)
451 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
452fail:
453 return err;
454}
455
456/**
457 * twl_get_type - API to get TWL Si type.
458 *
459 * Api to get the TWL Si type from IDCODE value.
460 */
461int twl_get_type(void)
462{
463 return TWL_SIL_TYPE(twl_idcode);
464}
465EXPORT_SYMBOL_GPL(twl_get_type);
466
467/**
468 * twl_get_version - API to get TWL Si version.
469 *
470 * Api to get the TWL Si version from IDCODE value.
471 */
472int twl_get_version(void)
473{
474 return TWL_SIL_REV(twl_idcode);
475}
476EXPORT_SYMBOL_GPL(twl_get_version);
477
Peter Ujfalusi2275c542012-09-10 13:46:22 +0300478/**
479 * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate.
480 *
481 * Api to get the TWL HFCLK rate based on BOOT_CFG register.
482 */
483int twl_get_hfclk_rate(void)
484{
485 u8 ctrl;
486 int rate;
487
488 twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &ctrl, R_CFG_BOOT);
489
490 switch (ctrl & 0x3) {
491 case HFCLK_FREQ_19p2_MHZ:
492 rate = 19200000;
493 break;
494 case HFCLK_FREQ_26_MHZ:
495 rate = 26000000;
496 break;
497 case HFCLK_FREQ_38p4_MHZ:
498 rate = 38400000;
499 break;
500 default:
501 pr_err("TWL4030: HFCLK is not configured\n");
502 rate = -EINVAL;
503 break;
504 }
505
506 return rate;
507}
508EXPORT_SYMBOL_GPL(twl_get_hfclk_rate);
509
David Brownelldad759f2008-12-01 00:43:58 +0100510static struct device *
511add_numbered_child(unsigned chip, const char *name, int num,
David Brownell5725d662008-12-01 00:31:04 +0100512 void *pdata, unsigned pdata_len,
513 bool can_wakeup, int irq0, int irq1)
514{
515 struct platform_device *pdev;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100516 struct twl_client *twl = &twl_modules[chip];
David Brownell5725d662008-12-01 00:31:04 +0100517 int status;
518
David Brownelldad759f2008-12-01 00:43:58 +0100519 pdev = platform_device_alloc(name, num);
David Brownell5725d662008-12-01 00:31:04 +0100520 if (!pdev) {
521 dev_dbg(&twl->client->dev, "can't alloc dev\n");
522 status = -ENOMEM;
523 goto err;
524 }
525
David Brownell5725d662008-12-01 00:31:04 +0100526 pdev->dev.parent = &twl->client->dev;
527
528 if (pdata) {
529 status = platform_device_add_data(pdev, pdata, pdata_len);
530 if (status < 0) {
531 dev_dbg(&pdev->dev, "can't add platform_data\n");
532 goto err;
533 }
534 }
535
536 if (irq0) {
537 struct resource r[2] = {
538 { .start = irq0, .flags = IORESOURCE_IRQ, },
539 { .start = irq1, .flags = IORESOURCE_IRQ, },
540 };
541
542 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
543 if (status < 0) {
544 dev_dbg(&pdev->dev, "can't add irqs\n");
545 goto err;
546 }
547 }
548
549 status = platform_device_add(pdev);
NeilBrown17ffba62012-07-07 08:51:03 +1000550 if (status == 0)
551 device_init_wakeup(&pdev->dev, can_wakeup);
David Brownell5725d662008-12-01 00:31:04 +0100552
553err:
554 if (status < 0) {
555 platform_device_put(pdev);
556 dev_err(&twl->client->dev, "can't add %s dev\n", name);
557 return ERR_PTR(status);
558 }
559 return &pdev->dev;
560}
561
David Brownelldad759f2008-12-01 00:43:58 +0100562static inline struct device *add_child(unsigned chip, const char *name,
563 void *pdata, unsigned pdata_len,
564 bool can_wakeup, int irq0, int irq1)
565{
566 return add_numbered_child(chip, name, -1, pdata, pdata_len,
567 can_wakeup, irq0, irq1);
568}
569
570static struct device *
571add_regulator_linked(int num, struct regulator_init_data *pdata,
572 struct regulator_consumer_supply *consumers,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100573 unsigned num_consumers, unsigned long features)
David Brownelldad759f2008-12-01 00:43:58 +0100574{
Balaji T Ke8deb282009-12-14 00:25:31 +0100575 unsigned sub_chip_id;
Tero Kristo63bfff42012-02-16 12:27:52 +0200576 struct twl_regulator_driver_data drv_data;
577
David Brownelldad759f2008-12-01 00:43:58 +0100578 /* regulator framework demands init_data ... */
579 if (!pdata)
580 return NULL;
581
David Brownellb73eac72008-12-07 19:10:58 +0100582 if (consumers) {
David Brownelldad759f2008-12-01 00:43:58 +0100583 pdata->consumer_supplies = consumers;
584 pdata->num_consumer_supplies = num_consumers;
585 }
586
Tero Kristo63bfff42012-02-16 12:27:52 +0200587 if (pdata->driver_data) {
588 /* If we have existing drv_data, just add the flags */
589 struct twl_regulator_driver_data *tmp;
590 tmp = pdata->driver_data;
591 tmp->features |= features;
592 } else {
593 /* add new driver data struct, used only during init */
594 drv_data.features = features;
595 drv_data.set_voltage = NULL;
596 drv_data.get_voltage = NULL;
597 drv_data.data = NULL;
598 pdata->driver_data = &drv_data;
599 }
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100600
David Brownelldad759f2008-12-01 00:43:58 +0100601 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
Balaji T Ke8deb282009-12-14 00:25:31 +0100602 sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
603 return add_numbered_child(sub_chip_id, "twl_reg", num,
David Brownelldad759f2008-12-01 00:43:58 +0100604 pdata, sizeof(*pdata), false, 0, 0);
605}
606
607static struct device *
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100608add_regulator(int num, struct regulator_init_data *pdata,
609 unsigned long features)
David Brownelldad759f2008-12-01 00:43:58 +0100610{
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100611 return add_regulator_linked(num, pdata, NULL, 0, features);
David Brownelldad759f2008-12-01 00:43:58 +0100612}
613
David Brownella603a7f2008-10-15 12:15:39 +0200614/*
David Brownella30d46c2008-10-20 23:46:28 +0200615 * NOTE: We know the first 8 IRQs after pdata->base_irq are
616 * for the PIH, and the next are for the PWR_INT SIH, since
617 * that's how twl_init_irq() sets things up.
David Brownella603a7f2008-10-15 12:15:39 +0200618 */
David Brownella603a7f2008-10-15 12:15:39 +0200619
David Brownelldad759f2008-12-01 00:43:58 +0100620static int
Felipe Balbi9e178622012-02-22 14:32:16 +0200621add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
622 unsigned long features)
David Brownella603a7f2008-10-15 12:15:39 +0200623{
David Brownell5725d662008-12-01 00:31:04 +0100624 struct device *child;
Balaji T Ke8deb282009-12-14 00:25:31 +0100625 unsigned sub_chip_id;
David Brownella603a7f2008-10-15 12:15:39 +0200626
Thierry Redingf78959c2012-09-18 10:29:50 +0200627 if (IS_ENABLED(CONFIG_GPIO_TWL4030) && pdata->gpio) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100628 child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
David Brownell5725d662008-12-01 00:31:04 +0100629 pdata->gpio, sizeof(*pdata->gpio),
Felipe Balbi9e178622012-02-22 14:32:16 +0200630 false, irq_base + GPIO_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100631 if (IS_ERR(child))
632 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200633 }
634
Thierry Redingf78959c2012-09-18 10:29:50 +0200635 if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030) && pdata->keypad) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100636 child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
David Brownell5725d662008-12-01 00:31:04 +0100637 pdata->keypad, sizeof(*pdata->keypad),
Felipe Balbi9e178622012-02-22 14:32:16 +0200638 true, irq_base + KEYPAD_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100639 if (IS_ERR(child))
640 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200641 }
642
Peter Ujfalusi24ae36f2012-11-13 09:28:42 +0100643 if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc &&
644 twl_class_is_4030()) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100645 child = add_child(SUB_CHIP_ID2, "twl4030_madc",
David Brownell5725d662008-12-01 00:31:04 +0100646 pdata->madc, sizeof(*pdata->madc),
Felipe Balbi9e178622012-02-22 14:32:16 +0200647 true, irq_base + MADC_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100648 if (IS_ERR(child))
649 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200650 }
651
Thierry Redingf78959c2012-09-18 10:29:50 +0200652 if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030)) {
David Brownella603a7f2008-10-15 12:15:39 +0200653 /*
David Brownell5725d662008-12-01 00:31:04 +0100654 * REVISIT platform_data here currently might expose the
David Brownella603a7f2008-10-15 12:15:39 +0200655 * "msecure" line ... but for now we just expect board
David Brownell5725d662008-12-01 00:31:04 +0100656 * setup to tell the chip "it's always ok to SET_TIME".
David Brownella603a7f2008-10-15 12:15:39 +0200657 * Eventually, Linux might become more aware of such
658 * HW security concerns, and "least privilege".
659 */
Balaji T Ke8deb282009-12-14 00:25:31 +0100660 sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100661 child = add_child(sub_chip_id, "twl_rtc", NULL, 0,
Felipe Balbi9e178622012-02-22 14:32:16 +0200662 true, irq_base + RTC_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100663 if (IS_ERR(child))
664 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200665 }
666
Peter Ujfalusiafc45892012-11-13 09:28:43 +0100667 if (IS_ENABLED(CONFIG_PWM_TWL)) {
668 child = add_child(SUB_CHIP_ID1, "twl-pwm", NULL, 0,
669 false, 0, 0);
670 if (IS_ERR(child))
671 return PTR_ERR(child);
672 }
673
674 if (IS_ENABLED(CONFIG_PWM_TWL_LED)) {
675 child = add_child(SUB_CHIP_ID1, "twl-pwmled", NULL, 0,
Thierry Reding48a364b2012-09-18 10:29:51 +0200676 false, 0, 0);
677 if (IS_ERR(child))
678 return PTR_ERR(child);
679 }
680
Thierry Redingf78959c2012-09-18 10:29:50 +0200681 if (IS_ENABLED(CONFIG_TWL4030_USB) && pdata->usb &&
682 twl_class_is_4030()) {
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300683
684 static struct regulator_consumer_supply usb1v5 = {
685 .supply = "usb1v5",
686 };
687 static struct regulator_consumer_supply usb1v8 = {
688 .supply = "usb1v8",
689 };
NeilBrownab378132012-05-09 07:40:40 +1000690 static struct regulator_consumer_supply usb3v1[] = {
691 { .supply = "usb3v1" },
692 { .supply = "bci3v1" },
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300693 };
694
695 /* First add the regulators so that they can be used by transceiver */
Thierry Redingf78959c2012-09-18 10:29:50 +0200696 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300697 /* this is a template that gets copied */
698 struct regulator_init_data usb_fixed = {
699 .constraints.valid_modes_mask =
700 REGULATOR_MODE_NORMAL
701 | REGULATOR_MODE_STANDBY,
702 .constraints.valid_ops_mask =
703 REGULATOR_CHANGE_MODE
704 | REGULATOR_CHANGE_STATUS,
705 };
706
707 child = add_regulator_linked(TWL4030_REG_VUSB1V5,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100708 &usb_fixed, &usb1v5, 1,
709 features);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300710 if (IS_ERR(child))
711 return PTR_ERR(child);
712
713 child = add_regulator_linked(TWL4030_REG_VUSB1V8,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100714 &usb_fixed, &usb1v8, 1,
715 features);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300716 if (IS_ERR(child))
717 return PTR_ERR(child);
718
719 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
NeilBrownab378132012-05-09 07:40:40 +1000720 &usb_fixed, usb3v1, 2,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100721 features);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300722 if (IS_ERR(child))
723 return PTR_ERR(child);
724
725 }
726
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100727 child = add_child(SUB_CHIP_ID0, "twl4030_usb",
728 pdata->usb, sizeof(*pdata->usb), true,
David Brownell5725d662008-12-01 00:31:04 +0100729 /* irq0 = USB_PRES, irq1 = USB */
Felipe Balbi9e178622012-02-22 14:32:16 +0200730 irq_base + USB_PRES_INTR_OFFSET,
731 irq_base + USB_INTR_OFFSET);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300732
David Brownell5725d662008-12-01 00:31:04 +0100733 if (IS_ERR(child))
734 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100735
736 /* we need to connect regulators to this transceiver */
Thierry Redingf78959c2012-09-18 10:29:50 +0200737 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child) {
Mark Brown1b65fa82012-02-03 11:02:58 +0000738 usb1v5.dev_name = dev_name(child);
739 usb1v8.dev_name = dev_name(child);
NeilBrownab378132012-05-09 07:40:40 +1000740 usb3v1[0].dev_name = dev_name(child);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300741 }
David Brownelldad759f2008-12-01 00:43:58 +0100742 }
Thierry Redingf78959c2012-09-18 10:29:50 +0200743 if (IS_ENABLED(CONFIG_TWL6030_USB) && pdata->usb &&
744 twl_class_is_6030()) {
Hema HKe70357e32010-12-10 18:09:52 +0530745
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100746 static struct regulator_consumer_supply usb3v3;
747 int regulator;
Hema HKe70357e32010-12-10 18:09:52 +0530748
Thierry Redingf78959c2012-09-18 10:29:50 +0200749 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
Hema HKe70357e32010-12-10 18:09:52 +0530750 /* this is a template that gets copied */
751 struct regulator_init_data usb_fixed = {
752 .constraints.valid_modes_mask =
753 REGULATOR_MODE_NORMAL
754 | REGULATOR_MODE_STANDBY,
755 .constraints.valid_ops_mask =
756 REGULATOR_CHANGE_MODE
757 | REGULATOR_CHANGE_STATUS,
758 };
759
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100760 if (features & TWL6025_SUBCLASS) {
761 usb3v3.supply = "ldousb";
762 regulator = TWL6025_REG_LDOUSB;
763 } else {
764 usb3v3.supply = "vusb";
765 regulator = TWL6030_REG_VUSB;
766 }
767 child = add_regulator_linked(regulator, &usb_fixed,
768 &usb3v3, 1,
769 features);
Hema HKe70357e32010-12-10 18:09:52 +0530770 if (IS_ERR(child))
771 return PTR_ERR(child);
772 }
773
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100774 pdata->usb->features = features;
775
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100776 child = add_child(SUB_CHIP_ID0, "twl6030_usb",
777 pdata->usb, sizeof(*pdata->usb), true,
Hema HKe70357e32010-12-10 18:09:52 +0530778 /* irq1 = VBUS_PRES, irq0 = USB ID */
Felipe Balbi9e178622012-02-22 14:32:16 +0200779 irq_base + USBOTG_INTR_OFFSET,
780 irq_base + USB_PRES_INTR_OFFSET);
Hema HKe70357e32010-12-10 18:09:52 +0530781
782 if (IS_ERR(child))
783 return PTR_ERR(child);
784 /* we need to connect regulators to this transceiver */
Thierry Redingf78959c2012-09-18 10:29:50 +0200785 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child)
Mark Brown1b65fa82012-02-03 11:02:58 +0000786 usb3v3.dev_name = dev_name(child);
Thierry Redingf78959c2012-09-18 10:29:50 +0200787 } else if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) &&
788 twl_class_is_6030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100789 if (features & TWL6025_SUBCLASS)
790 child = add_regulator(TWL6025_REG_LDOUSB,
791 pdata->ldousb, features);
792 else
793 child = add_regulator(TWL6030_REG_VUSB,
794 pdata->vusb, features);
Hema HKe70357e32010-12-10 18:09:52 +0530795
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100796 if (IS_ERR(child))
797 return PTR_ERR(child);
Hema HKe70357e32010-12-10 18:09:52 +0530798 }
David Brownelldad759f2008-12-01 00:43:58 +0100799
Thierry Redingf78959c2012-09-18 10:29:50 +0200800 if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG) && twl_class_is_4030()) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100801 child = add_child(SUB_CHIP_ID3, "twl4030_wdt", NULL, 0,
802 false, 0, 0);
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200803 if (IS_ERR(child))
804 return PTR_ERR(child);
805 }
806
Thierry Redingf78959c2012-09-18 10:29:50 +0200807 if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON) && twl_class_is_4030()) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100808 child = add_child(SUB_CHIP_ID3, "twl4030_pwrbutton", NULL, 0,
809 true, irq_base + 8 + 0, 0);
Felipe Balbi9c3664d2009-08-03 18:16:38 +0200810 if (IS_ERR(child))
811 return PTR_ERR(child);
812 }
813
Thierry Redingf78959c2012-09-18 10:29:50 +0200814 if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO) && pdata->audio &&
815 twl_class_is_4030()) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100816 child = add_child(SUB_CHIP_ID1, "twl4030-audio",
Peter Ujfalusi4ae6df5e2011-05-31 15:21:13 +0300817 pdata->audio, sizeof(*pdata->audio),
Misael Lopez Cruzd62abe52010-02-23 18:10:19 -0600818 false, 0, 0);
819 if (IS_ERR(child))
820 return PTR_ERR(child);
821 }
822
Rajendra Nayak9da66532009-12-13 22:29:47 +0100823 /* twl4030 regulators */
Thierry Redingf78959c2012-09-18 10:29:50 +0200824 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_4030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100825 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
826 features);
David Brownelldad759f2008-12-01 00:43:58 +0100827 if (IS_ERR(child))
828 return PTR_ERR(child);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100829
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100830 child = add_regulator(TWL4030_REG_VIO, pdata->vio,
831 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100832 if (IS_ERR(child))
833 return PTR_ERR(child);
834
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100835 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
836 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100837 if (IS_ERR(child))
838 return PTR_ERR(child);
839
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100840 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
841 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100842 if (IS_ERR(child))
843 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100844
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100845 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
846 features);
David Brownelldad759f2008-12-01 00:43:58 +0100847 if (IS_ERR(child))
848 return PTR_ERR(child);
849
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100850 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
851 features);
David Brownelldad759f2008-12-01 00:43:58 +0100852 if (IS_ERR(child))
853 return PTR_ERR(child);
854
855 child = add_regulator((features & TWL4030_VAUX2)
856 ? TWL4030_REG_VAUX2_4030
857 : TWL4030_REG_VAUX2,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100858 pdata->vaux2, features);
David Brownelldad759f2008-12-01 00:43:58 +0100859 if (IS_ERR(child))
860 return PTR_ERR(child);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100861
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100862 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
863 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100864 if (IS_ERR(child))
865 return PTR_ERR(child);
866
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100867 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
868 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100869 if (IS_ERR(child))
870 return PTR_ERR(child);
871
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100872 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
873 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100874 if (IS_ERR(child))
875 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100876 }
877
David Brownelldad759f2008-12-01 00:43:58 +0100878 /* maybe add LDOs that are omitted on cost-reduced parts */
Thierry Redingf78959c2012-09-18 10:29:50 +0200879 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && !(features & TPS_SUBSET)
Rajendra Nayak9da66532009-12-13 22:29:47 +0100880 && twl_class_is_4030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100881 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
882 features);
David Brownelldad759f2008-12-01 00:43:58 +0100883 if (IS_ERR(child))
884 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100885
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100886 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
887 features);
David Brownelldad759f2008-12-01 00:43:58 +0100888 if (IS_ERR(child))
889 return PTR_ERR(child);
890
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100891 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
892 features);
David Brownelldad759f2008-12-01 00:43:58 +0100893 if (IS_ERR(child))
894 return PTR_ERR(child);
895
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100896 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
897 features);
David Brownelldad759f2008-12-01 00:43:58 +0100898 if (IS_ERR(child))
899 return PTR_ERR(child);
900
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100901 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
902 features);
David Brownelldad759f2008-12-01 00:43:58 +0100903 if (IS_ERR(child))
904 return PTR_ERR(child);
905
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100906 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
907 features);
David Brownelldad759f2008-12-01 00:43:58 +0100908 if (IS_ERR(child))
909 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200910 }
911
Rajendra Nayak9da66532009-12-13 22:29:47 +0100912 /* twl6030 regulators */
Thierry Redingf78959c2012-09-18 10:29:50 +0200913 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100914 !(features & TWL6025_SUBCLASS)) {
Tero Kristo34a38442012-02-28 15:09:10 +0530915 child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1,
916 features);
917 if (IS_ERR(child))
918 return PTR_ERR(child);
919
920 child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2,
921 features);
922 if (IS_ERR(child))
923 return PTR_ERR(child);
924
925 child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3,
926 features);
927 if (IS_ERR(child))
928 return PTR_ERR(child);
929
Peter Ujfalusi46eda3e2012-02-28 15:09:13 +0530930 child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8,
931 features);
932 if (IS_ERR(child))
933 return PTR_ERR(child);
934
935 child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1,
936 features);
937 if (IS_ERR(child))
938 return PTR_ERR(child);
939
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100940 child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
941 features);
942 if (IS_ERR(child))
943 return PTR_ERR(child);
944
945 child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
946 features);
947 if (IS_ERR(child))
948 return PTR_ERR(child);
949
950 child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
951 features);
952 if (IS_ERR(child))
953 return PTR_ERR(child);
954
955 child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
956 features);
957 if (IS_ERR(child))
958 return PTR_ERR(child);
959
960 child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
961 features);
962 if (IS_ERR(child))
963 return PTR_ERR(child);
964
965 child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
966 features);
967 if (IS_ERR(child))
968 return PTR_ERR(child);
969
970 child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
971 features);
972 if (IS_ERR(child))
973 return PTR_ERR(child);
974
975 child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
976 features);
977 if (IS_ERR(child))
978 return PTR_ERR(child);
979
980 child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
981 features);
982 if (IS_ERR(child))
983 return PTR_ERR(child);
984 }
985
986 /* 6030 and 6025 share this regulator */
Thierry Redingf78959c2012-09-18 10:29:50 +0200987 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100988 child = add_regulator(TWL6030_REG_VANA, pdata->vana,
989 features);
990 if (IS_ERR(child))
991 return PTR_ERR(child);
992 }
993
994 /* twl6025 regulators */
Thierry Redingf78959c2012-09-18 10:29:50 +0200995 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100996 (features & TWL6025_SUBCLASS)) {
997 child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
998 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +0100999 if (IS_ERR(child))
1000 return PTR_ERR(child);
1001
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001002 child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
1003 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001004 if (IS_ERR(child))
1005 return PTR_ERR(child);
1006
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001007 child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
1008 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001009 if (IS_ERR(child))
1010 return PTR_ERR(child);
1011
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001012 child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
1013 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001014 if (IS_ERR(child))
1015 return PTR_ERR(child);
1016
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001017 child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
1018 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001019 if (IS_ERR(child))
1020 return PTR_ERR(child);
1021
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001022 child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
1023 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001024 if (IS_ERR(child))
1025 return PTR_ERR(child);
1026
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001027 child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
1028 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001029 if (IS_ERR(child))
1030 return PTR_ERR(child);
1031
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001032 child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
1033 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001034 if (IS_ERR(child))
1035 return PTR_ERR(child);
1036
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001037 child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
1038 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001039 if (IS_ERR(child))
1040 return PTR_ERR(child);
Balaji T K8e6de4a2011-02-10 18:44:50 +05301041
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001042 child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
1043 features);
Balaji T K8e6de4a2011-02-10 18:44:50 +05301044 if (IS_ERR(child))
1045 return PTR_ERR(child);
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001046
1047 child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
1048 features);
1049 if (IS_ERR(child))
1050 return PTR_ERR(child);
1051
Rajendra Nayak9da66532009-12-13 22:29:47 +01001052 }
1053
Thierry Redingf78959c2012-09-18 10:29:50 +02001054 if (IS_ENABLED(CONFIG_CHARGER_TWL4030) && pdata->bci &&
Grazvydas Ignotas11c39c42010-10-01 13:17:50 +03001055 !(features & (TPS_SUBSET | TWL5031))) {
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +01001056 child = add_child(SUB_CHIP_ID3, "twl4030_bci",
Grazvydas Ignotas11c39c42010-10-01 13:17:50 +03001057 pdata->bci, sizeof(*pdata->bci), false,
1058 /* irq0 = CHG_PRES, irq1 = BCI */
Felipe Balbi9e178622012-02-22 14:32:16 +02001059 irq_base + BCI_PRES_INTR_OFFSET,
1060 irq_base + BCI_INTR_OFFSET);
Grazvydas Ignotas11c39c42010-10-01 13:17:50 +03001061 if (IS_ERR(child))
1062 return PTR_ERR(child);
1063 }
1064
David Brownell5725d662008-12-01 00:31:04 +01001065 return 0;
David Brownella603a7f2008-10-15 12:15:39 +02001066}
1067
1068/*----------------------------------------------------------------------*/
1069
1070/*
1071 * These three functions initialize the on-chip clock framework,
1072 * letting it generate the right frequencies for USB, MADC, and
1073 * other purposes.
1074 */
1075static inline int __init protect_pm_master(void)
1076{
1077 int e = 0;
1078
Felipe Balbi49e6f872010-08-18 09:19:35 +03001079 e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
1080 TWL4030_PM_MASTER_PROTECT_KEY);
David Brownella603a7f2008-10-15 12:15:39 +02001081 return e;
1082}
1083
1084static inline int __init unprotect_pm_master(void)
1085{
1086 int e = 0;
1087
Felipe Balbi49e6f872010-08-18 09:19:35 +03001088 e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1089 TWL4030_PM_MASTER_KEY_CFG1,
1090 TWL4030_PM_MASTER_PROTECT_KEY);
1091 e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1092 TWL4030_PM_MASTER_KEY_CFG2,
1093 TWL4030_PM_MASTER_PROTECT_KEY);
1094
David Brownella603a7f2008-10-15 12:15:39 +02001095 return e;
1096}
1097
Ilkka Koskinen38a68492009-10-22 14:14:09 +03001098static void clocks_init(struct device *dev,
1099 struct twl4030_clock_init_data *clock)
David Brownella603a7f2008-10-15 12:15:39 +02001100{
1101 int e = 0;
1102 struct clk *osc;
1103 u32 rate;
1104 u8 ctrl = HFCLK_FREQ_26_MHZ;
1105
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001106 osc = clk_get(dev, "fck");
David Brownella603a7f2008-10-15 12:15:39 +02001107 if (IS_ERR(osc)) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001108 printk(KERN_WARNING "Skipping twl internal clock init and "
David Brownella603a7f2008-10-15 12:15:39 +02001109 "using bootloader value (unknown osc rate)\n");
1110 return;
1111 }
1112
1113 rate = clk_get_rate(osc);
1114 clk_put(osc);
1115
1116 switch (rate) {
1117 case 19200000:
1118 ctrl = HFCLK_FREQ_19p2_MHZ;
1119 break;
1120 case 26000000:
1121 ctrl = HFCLK_FREQ_26_MHZ;
1122 break;
1123 case 38400000:
1124 ctrl = HFCLK_FREQ_38p4_MHZ;
1125 break;
1126 }
1127
1128 ctrl |= HIGH_PERF_SQ;
Ilkka Koskinen38a68492009-10-22 14:14:09 +03001129 if (clock && clock->ck32k_lowpwr_enable)
1130 ctrl |= CK32K_LOWPWR_EN;
1131
David Brownella603a7f2008-10-15 12:15:39 +02001132 e |= unprotect_pm_master();
1133 /* effect->MADC+USB ck en */
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001134 e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
David Brownella603a7f2008-10-15 12:15:39 +02001135 e |= protect_pm_master();
1136
1137 if (e < 0)
1138 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
1139}
1140
1141/*----------------------------------------------------------------------*/
1142
David Brownella603a7f2008-10-15 12:15:39 +02001143
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001144static int twl_remove(struct i2c_client *client)
David Brownella603a7f2008-10-15 12:15:39 +02001145{
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001146 unsigned i, num_slaves;
David Brownella30d46c2008-10-20 23:46:28 +02001147 int status;
David Brownella603a7f2008-10-15 12:15:39 +02001148
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001149 if (twl_class_is_4030()) {
Balaji T Ke8deb282009-12-14 00:25:31 +01001150 status = twl4030_exit_irq();
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001151 num_slaves = TWL_NUM_SLAVES;
1152 } else {
Balaji T Ke8deb282009-12-14 00:25:31 +01001153 status = twl6030_exit_irq();
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001154 num_slaves = TWL_NUM_SLAVES - 1;
1155 }
Balaji T Ke8deb282009-12-14 00:25:31 +01001156
David Brownella30d46c2008-10-20 23:46:28 +02001157 if (status < 0)
1158 return status;
David Brownella603a7f2008-10-15 12:15:39 +02001159
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001160 for (i = 0; i < num_slaves; i++) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001161 struct twl_client *twl = &twl_modules[i];
David Brownella603a7f2008-10-15 12:15:39 +02001162
1163 if (twl->client && twl->client != client)
1164 i2c_unregister_device(twl->client);
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001165 twl_modules[i].client = NULL;
David Brownella603a7f2008-10-15 12:15:39 +02001166 }
1167 inuse = false;
1168 return 0;
1169}
1170
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001171/* NOTE: This driver only handles a single twl4030/tps659x0 chip */
Bryan Wu5b9cecd2010-12-08 10:42:04 +08001172static int __devinit
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001173twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
David Brownella603a7f2008-10-15 12:15:39 +02001174{
David Brownella603a7f2008-10-15 12:15:39 +02001175 struct twl4030_platform_data *pdata = client->dev.platform_data;
Benoit Coussonaeb50322011-08-29 16:20:23 +02001176 struct device_node *node = client->dev.of_node;
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001177 struct platform_device *pdev;
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001178 struct regmap_config *twl_regmap_config;
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001179 int irq_base = 0;
1180 int status;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001181 unsigned i, num_slaves;
Benoit Coussonaeb50322011-08-29 16:20:23 +02001182
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001183 pdev = platform_device_alloc(DRIVER_NAME, -1);
1184 if (!pdev) {
1185 dev_err(&client->dev, "can't alloc pdev\n");
1186 return -ENOMEM;
1187 }
1188
1189 status = platform_device_add(pdev);
1190 if (status) {
1191 platform_device_put(pdev);
1192 return status;
1193 }
1194
Benoit Coussonaeb50322011-08-29 16:20:23 +02001195 if (node && !pdata) {
1196 /*
1197 * XXX: Temporary pdata until the information is correctly
1198 * retrieved by every TWL modules from DT.
1199 */
1200 pdata = devm_kzalloc(&client->dev,
1201 sizeof(struct twl4030_platform_data),
1202 GFP_KERNEL);
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001203 if (!pdata) {
1204 status = -ENOMEM;
1205 goto free;
1206 }
Benoit Coussonaeb50322011-08-29 16:20:23 +02001207 }
David Brownella603a7f2008-10-15 12:15:39 +02001208
1209 if (!pdata) {
1210 dev_dbg(&client->dev, "no platform data?\n");
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001211 status = -EINVAL;
1212 goto free;
David Brownella603a7f2008-10-15 12:15:39 +02001213 }
1214
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001215 platform_set_drvdata(pdev, pdata);
1216
David Brownella603a7f2008-10-15 12:15:39 +02001217 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1218 dev_dbg(&client->dev, "can't talk I2C?\n");
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001219 status = -EIO;
1220 goto free;
David Brownella603a7f2008-10-15 12:15:39 +02001221 }
1222
David Brownella30d46c2008-10-20 23:46:28 +02001223 if (inuse) {
David Brownella603a7f2008-10-15 12:15:39 +02001224 dev_dbg(&client->dev, "driver is already in use\n");
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001225 status = -EBUSY;
1226 goto free;
David Brownella603a7f2008-10-15 12:15:39 +02001227 }
1228
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001229 if ((id->driver_data) & TWL6030_CLASS) {
1230 twl_id = TWL6030_CLASS_ID;
1231 twl_map = &twl6030_map[0];
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001232 twl_regmap_config = twl6030_regmap_config;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001233 num_slaves = TWL_NUM_SLAVES - 1;
1234 } else {
1235 twl_id = TWL4030_CLASS_ID;
1236 twl_map = &twl4030_map[0];
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001237 twl_regmap_config = twl4030_regmap_config;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001238 num_slaves = TWL_NUM_SLAVES;
1239 }
1240
1241 for (i = 0; i < num_slaves; i++) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001242 struct twl_client *twl = &twl_modules[i];
David Brownella603a7f2008-10-15 12:15:39 +02001243
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001244 if (i == 0) {
David Brownella603a7f2008-10-15 12:15:39 +02001245 twl->client = client;
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001246 } else {
David Brownella603a7f2008-10-15 12:15:39 +02001247 twl->client = i2c_new_dummy(client->adapter,
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001248 client->addr + i);
David Brownella603a7f2008-10-15 12:15:39 +02001249 if (!twl->client) {
Ilkka Koskinena8643432009-10-20 16:22:52 +03001250 dev_err(&client->dev,
David Brownella603a7f2008-10-15 12:15:39 +02001251 "can't attach client %d\n", i);
1252 status = -ENOMEM;
1253 goto fail;
1254 }
David Brownella603a7f2008-10-15 12:15:39 +02001255 }
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001256
1257 twl->regmap = devm_regmap_init_i2c(twl->client,
1258 &twl_regmap_config[i]);
1259 if (IS_ERR(twl->regmap)) {
1260 status = PTR_ERR(twl->regmap);
1261 dev_err(&client->dev,
1262 "Failed to allocate regmap %d, err: %d\n", i,
1263 status);
1264 goto fail;
1265 }
David Brownella603a7f2008-10-15 12:15:39 +02001266 }
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001267
David Brownella603a7f2008-10-15 12:15:39 +02001268 inuse = true;
1269
1270 /* setup clock framework */
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001271 clocks_init(&pdev->dev, pdata->clock);
David Brownella603a7f2008-10-15 12:15:39 +02001272
Lesly A Mca972d12011-04-14 17:57:53 +05301273 /* read TWL IDCODE Register */
1274 if (twl_id == TWL4030_CLASS_ID) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001275 status = twl_read_idcode_register();
1276 WARN(status < 0, "Error: reading twl_idcode register value\n");
Lesly A Mca972d12011-04-14 17:57:53 +05301277 }
1278
Amit Kucheriaebf0bd32009-08-31 18:32:18 +02001279 /* load power event scripts */
Thierry Redingf78959c2012-09-18 10:29:50 +02001280 if (IS_ENABLED(CONFIG_TWL4030_POWER) && pdata->power)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +02001281 twl4030_power_init(pdata->power);
1282
David Brownella603a7f2008-10-15 12:15:39 +02001283 /* Maybe init the T2 Interrupt subsystem */
Felipe Balbi9e178622012-02-22 14:32:16 +02001284 if (client->irq) {
Balaji T Ke8deb282009-12-14 00:25:31 +01001285 if (twl_class_is_4030()) {
1286 twl4030_init_chip_irq(id->name);
Benoit Cousson78518ff2012-02-29 19:40:31 +01001287 irq_base = twl4030_init_irq(&client->dev, client->irq);
Balaji T Ke8deb282009-12-14 00:25:31 +01001288 } else {
Benoit Cousson78518ff2012-02-29 19:40:31 +01001289 irq_base = twl6030_init_irq(&client->dev, client->irq);
Balaji T Ke8deb282009-12-14 00:25:31 +01001290 }
1291
Benoit Cousson78518ff2012-02-29 19:40:31 +01001292 if (irq_base < 0) {
1293 status = irq_base;
David Brownella30d46c2008-10-20 23:46:28 +02001294 goto fail;
Benoit Cousson78518ff2012-02-29 19:40:31 +01001295 }
David Brownella603a7f2008-10-15 12:15:39 +02001296 }
1297
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001298 /*
1299 * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001300 * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1301 * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1302 */
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001303 if (twl_class_is_4030()) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001304 u8 temp;
1305
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001306 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1307 temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001308 I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001309 twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1310 }
1311
Grant Likely964dba22012-02-24 14:58:34 -07001312 status = -ENODEV;
Benoit Coussonaeb50322011-08-29 16:20:23 +02001313 if (node)
1314 status = of_platform_populate(node, NULL, NULL, &client->dev);
Grant Likely964dba22012-02-24 14:58:34 -07001315 if (status)
Felipe Balbi9e178622012-02-22 14:32:16 +02001316 status = add_children(pdata, irq_base, id->driver_data);
Benoit Coussonaeb50322011-08-29 16:20:23 +02001317
David Brownella603a7f2008-10-15 12:15:39 +02001318fail:
1319 if (status < 0)
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001320 twl_remove(client);
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001321free:
1322 if (status < 0)
1323 platform_device_unregister(pdev);
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001324
David Brownella603a7f2008-10-15 12:15:39 +02001325 return status;
1326}
1327
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001328static const struct i2c_device_id twl_ids[] = {
David Brownelldad759f2008-12-01 00:43:58 +01001329 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
1330 { "twl5030", 0 }, /* T2 updated */
Ilkka Koskinen1920a612009-11-10 17:26:15 +02001331 { "twl5031", TWL5031 }, /* TWL5030 updated */
David Brownelldad759f2008-12-01 00:43:58 +01001332 { "tps65950", 0 }, /* catalog version of twl5030 */
1333 { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
1334 { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
Oleg Drokin59dead52011-07-01 13:34:13 +02001335 { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
1336 and vibrator. Charger in USB module*/
Balaji T Ke8deb282009-12-14 00:25:31 +01001337 { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001338 { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
David Brownella603a7f2008-10-15 12:15:39 +02001339 { /* end of list */ },
1340};
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001341MODULE_DEVICE_TABLE(i2c, twl_ids);
David Brownella603a7f2008-10-15 12:15:39 +02001342
1343/* One Client Driver , 4 Clients */
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001344static struct i2c_driver twl_driver = {
David Brownella603a7f2008-10-15 12:15:39 +02001345 .driver.name = DRIVER_NAME,
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001346 .id_table = twl_ids,
1347 .probe = twl_probe,
1348 .remove = twl_remove,
David Brownella603a7f2008-10-15 12:15:39 +02001349};
1350
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001351static int __init twl_init(void)
David Brownella603a7f2008-10-15 12:15:39 +02001352{
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001353 return i2c_add_driver(&twl_driver);
David Brownella603a7f2008-10-15 12:15:39 +02001354}
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001355subsys_initcall(twl_init);
David Brownella603a7f2008-10-15 12:15:39 +02001356
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001357static void __exit twl_exit(void)
David Brownella603a7f2008-10-15 12:15:39 +02001358{
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001359 i2c_del_driver(&twl_driver);
David Brownella603a7f2008-10-15 12:15:39 +02001360}
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001361module_exit(twl_exit);
David Brownella603a7f2008-10-15 12:15:39 +02001362
1363MODULE_AUTHOR("Texas Instruments, Inc.");
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001364MODULE_DESCRIPTION("I2C Core interface for TWL");
David Brownella603a7f2008-10-15 12:15:39 +02001365MODULE_LICENSE("GPL");