blob: 86cca9e09380f4ad0672514f3ff19531bdbd6d7a [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
David Brownella603a7f2008-10-15 12:15:39 +020069/* Base Address defns for twl4030_map[] */
70
71/* subchip/slave 0 - USB ID */
72#define TWL4030_BASEADD_USB 0x0000
73
74/* subchip/slave 1 - AUD ID */
75#define TWL4030_BASEADD_AUDIO_VOICE 0x0000
76#define TWL4030_BASEADD_GPIO 0x0098
77#define TWL4030_BASEADD_INTBR 0x0085
78#define TWL4030_BASEADD_PIH 0x0080
79#define TWL4030_BASEADD_TEST 0x004C
80
81/* subchip/slave 2 - AUX ID */
82#define TWL4030_BASEADD_INTERRUPTS 0x00B9
83#define TWL4030_BASEADD_LED 0x00EE
84#define TWL4030_BASEADD_MADC 0x0000
85#define TWL4030_BASEADD_MAIN_CHARGE 0x0074
86#define TWL4030_BASEADD_PRECHARGE 0x00AA
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +010087#define TWL4030_BASEADD_PWM 0x00F8
David Brownella603a7f2008-10-15 12:15:39 +020088#define TWL4030_BASEADD_KEYPAD 0x00D2
89
Ilkka Koskinen1920a612009-11-10 17:26:15 +020090#define TWL5031_BASEADD_ACCESSORY 0x0074 /* Replaces Main Charge */
91#define TWL5031_BASEADD_INTERRUPTS 0x00B9 /* Different than TWL4030's
92 one */
93
David Brownella603a7f2008-10-15 12:15:39 +020094/* subchip/slave 3 - POWER ID */
95#define TWL4030_BASEADD_BACKUP 0x0014
96#define TWL4030_BASEADD_INT 0x002E
97#define TWL4030_BASEADD_PM_MASTER 0x0036
98#define TWL4030_BASEADD_PM_RECEIVER 0x005B
99#define TWL4030_BASEADD_RTC 0x001C
100#define TWL4030_BASEADD_SECURED_REG 0x0000
101
102/* Triton Core internal information (END) */
103
104
Balaji T Ke8deb282009-12-14 00:25:31 +0100105/* subchip/slave 0 0x48 - POWER */
106#define TWL6030_BASEADD_RTC 0x0000
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100107#define TWL6030_BASEADD_SECURED_REG 0x0017
Balaji T Ke8deb282009-12-14 00:25:31 +0100108#define TWL6030_BASEADD_PM_MASTER 0x001F
109#define TWL6030_BASEADD_PM_SLAVE_MISC 0x0030 /* PM_RECEIVER */
110#define TWL6030_BASEADD_PM_MISC 0x00E2
111#define TWL6030_BASEADD_PM_PUPD 0x00F0
112
113/* subchip/slave 1 0x49 - FEATURE */
114#define TWL6030_BASEADD_USB 0x0000
115#define TWL6030_BASEADD_GPADC_CTRL 0x002E
116#define TWL6030_BASEADD_AUX 0x0090
117#define TWL6030_BASEADD_PWM 0x00BA
118#define TWL6030_BASEADD_GASGAUGE 0x00C0
119#define TWL6030_BASEADD_PIH 0x00D0
120#define TWL6030_BASEADD_CHARGER 0x00E0
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100121#define TWL6025_BASEADD_CHARGER 0x00DA
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100122#define TWL6030_BASEADD_LED 0x00F4
Balaji T Ke8deb282009-12-14 00:25:31 +0100123
124/* subchip/slave 2 0x4A - DFT */
125#define TWL6030_BASEADD_DIEID 0x00C0
126
127/* subchip/slave 3 0x4B - AUDIO */
128#define TWL6030_BASEADD_AUDIO 0x0000
129#define TWL6030_BASEADD_RSV 0x0000
Balaji T Kfa0d9762010-02-19 12:39:38 +0100130#define TWL6030_BASEADD_ZERO 0x0000
Balaji T Ke8deb282009-12-14 00:25:31 +0100131
David Brownella603a7f2008-10-15 12:15:39 +0200132/* Few power values */
133#define R_CFG_BOOT 0x05
David Brownella603a7f2008-10-15 12:15:39 +0200134
135/* some fields in R_CFG_BOOT */
136#define HFCLK_FREQ_19p2_MHZ (1 << 0)
137#define HFCLK_FREQ_26_MHZ (2 << 0)
138#define HFCLK_FREQ_38p4_MHZ (3 << 0)
139#define HIGH_PERF_SQ (1 << 3)
Ilkka Koskinen38a68492009-10-22 14:14:09 +0300140#define CK32K_LOWPWR_EN (1 << 7)
David Brownella603a7f2008-10-15 12:15:39 +0200141
David Brownella603a7f2008-10-15 12:15:39 +0200142/*----------------------------------------------------------------------*/
143
David Brownella603a7f2008-10-15 12:15:39 +0200144/* is driver active, bound to a chip? */
145static bool inuse;
146
Lesly A Mca972d12011-04-14 17:57:53 +0530147/* TWL IDCODE Register value */
148static u32 twl_idcode;
149
Balaji T Ke8deb282009-12-14 00:25:31 +0100150static unsigned int twl_id;
151unsigned int twl_rev(void)
152{
153 return twl_id;
154}
155EXPORT_SYMBOL(twl_rev);
156
157/* Structure for each TWL4030/TWL6030 Slave */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100158struct twl_client {
David Brownella603a7f2008-10-15 12:15:39 +0200159 struct i2c_client *client;
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100160 struct regmap *regmap;
David Brownella603a7f2008-10-15 12:15:39 +0200161};
162
Peter Ujfalusi6dd810b2013-01-16 14:53:53 +0100163static struct twl_client *twl_modules;
David Brownella603a7f2008-10-15 12:15:39 +0200164
David Brownella603a7f2008-10-15 12:15:39 +0200165/* mapping the module id to slave id and base address */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100166struct twl_mapping {
David Brownella603a7f2008-10-15 12:15:39 +0200167 unsigned char sid; /* Slave ID */
168 unsigned char base; /* base address */
169};
G, Manjunath Kondaiah2cfcce12010-09-21 15:31:17 +0530170static struct twl_mapping *twl_map;
David Brownella603a7f2008-10-15 12:15:39 +0200171
Peter Ujfalusida059ec2012-11-13 09:28:48 +0100172static struct twl_mapping twl4030_map[] = {
David Brownella603a7f2008-10-15 12:15:39 +0200173 /*
174 * NOTE: don't change this table without updating the
Balaji T Ke8deb282009-12-14 00:25:31 +0100175 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
David Brownella603a7f2008-10-15 12:15:39 +0200176 * so they continue to match the order in this table.
177 */
178
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100179 /* Common IPs */
David Brownella603a7f2008-10-15 12:15:39 +0200180 { 0, TWL4030_BASEADD_USB },
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100181 { 1, TWL4030_BASEADD_PIH },
182 { 2, TWL4030_BASEADD_MAIN_CHARGE },
183 { 3, TWL4030_BASEADD_PM_MASTER },
184 { 3, TWL4030_BASEADD_PM_RECEIVER },
185
186 { 3, TWL4030_BASEADD_RTC },
187 { 2, TWL4030_BASEADD_PWM },
188 { 2, TWL4030_BASEADD_LED },
189 { 3, TWL4030_BASEADD_SECURED_REG },
190
191 /* TWL4030 specific IPs */
David Brownella603a7f2008-10-15 12:15:39 +0200192 { 1, TWL4030_BASEADD_AUDIO_VOICE },
193 { 1, TWL4030_BASEADD_GPIO },
194 { 1, TWL4030_BASEADD_INTBR },
Peter Ujfalusi6691ccd2012-11-13 09:28:50 +0100195 { 1, TWL4030_BASEADD_TEST },
David Brownella603a7f2008-10-15 12:15:39 +0200196 { 2, TWL4030_BASEADD_KEYPAD },
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100197
David Brownella603a7f2008-10-15 12:15:39 +0200198 { 2, TWL4030_BASEADD_MADC },
199 { 2, TWL4030_BASEADD_INTERRUPTS },
David Brownella603a7f2008-10-15 12:15:39 +0200200 { 2, TWL4030_BASEADD_PRECHARGE },
David Brownella603a7f2008-10-15 12:15:39 +0200201 { 3, TWL4030_BASEADD_BACKUP },
202 { 3, TWL4030_BASEADD_INT },
Peter Ujfalusi6691ccd2012-11-13 09:28:50 +0100203
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100204 { 2, TWL5031_BASEADD_ACCESSORY },
205 { 2, TWL5031_BASEADD_INTERRUPTS },
David Brownella603a7f2008-10-15 12:15:39 +0200206};
207
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100208static struct regmap_config twl4030_regmap_config[4] = {
209 {
210 /* Address 0x48 */
211 .reg_bits = 8,
212 .val_bits = 8,
213 .max_register = 0xff,
214 },
215 {
216 /* Address 0x49 */
217 .reg_bits = 8,
218 .val_bits = 8,
219 .max_register = 0xff,
220 },
221 {
222 /* Address 0x4a */
223 .reg_bits = 8,
224 .val_bits = 8,
225 .max_register = 0xff,
226 },
227 {
228 /* Address 0x4b */
229 .reg_bits = 8,
230 .val_bits = 8,
231 .max_register = 0xff,
232 },
233};
234
Balaji T Ke8deb282009-12-14 00:25:31 +0100235static struct twl_mapping twl6030_map[] = {
236 /*
237 * NOTE: don't change this table without updating the
238 * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
239 * so they continue to match the order in this table.
240 */
Balaji T Ke8deb282009-12-14 00:25:31 +0100241
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100242 /* Common IPs */
243 { 1, TWL6030_BASEADD_USB },
244 { 1, TWL6030_BASEADD_PIH },
245 { 1, TWL6030_BASEADD_CHARGER },
246 { 0, TWL6030_BASEADD_PM_MASTER },
247 { 0, TWL6030_BASEADD_PM_SLAVE_MISC },
Balaji T Ke8deb282009-12-14 00:25:31 +0100248
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100249 { 0, TWL6030_BASEADD_RTC },
250 { 1, TWL6030_BASEADD_PWM },
251 { 1, TWL6030_BASEADD_LED },
252 { 0, TWL6030_BASEADD_SECURED_REG },
Balaji T Ke8deb282009-12-14 00:25:31 +0100253
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100254 /* TWL6030 specific IPs */
255 { 0, TWL6030_BASEADD_ZERO },
256 { 1, TWL6030_BASEADD_ZERO },
257 { 2, TWL6030_BASEADD_ZERO },
258 { 1, TWL6030_BASEADD_GPADC_CTRL },
259 { 1, TWL6030_BASEADD_GASGAUGE },
Balaji T Ke8deb282009-12-14 00:25:31 +0100260};
261
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100262static struct regmap_config twl6030_regmap_config[3] = {
263 {
264 /* Address 0x48 */
265 .reg_bits = 8,
266 .val_bits = 8,
267 .max_register = 0xff,
268 },
269 {
270 /* Address 0x49 */
271 .reg_bits = 8,
272 .val_bits = 8,
273 .max_register = 0xff,
274 },
275 {
276 /* Address 0x4a */
277 .reg_bits = 8,
278 .val_bits = 8,
279 .max_register = 0xff,
280 },
281};
282
David Brownella603a7f2008-10-15 12:15:39 +0200283/*----------------------------------------------------------------------*/
284
Peter Ujfalusi6dd810b2013-01-16 14:53:53 +0100285static inline int twl_get_num_slaves(void)
286{
287 if (twl_class_is_4030())
288 return 4; /* TWL4030 class have four slave address */
289 else
290 return 3; /* TWL6030 class have three slave address */
291}
292
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100293static inline int twl_get_last_module(void)
294{
295 if (twl_class_is_4030())
296 return TWL4030_MODULE_LAST;
297 else
298 return TWL6030_MODULE_LAST;
299}
300
David Brownella603a7f2008-10-15 12:15:39 +0200301/* Exported Functions */
302
303/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100304 * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200305 * @mod_no: module number
306 * @value: an array of num_bytes+1 containing data to write
307 * @reg: register address (just offset will do)
308 * @num_bytes: number of bytes to transfer
309 *
310 * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
311 * valid data starts at Offset 1.
312 *
313 * Returns the result of operation - 0 is success
314 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100315int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200316{
317 int ret;
318 int sid;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100319 struct twl_client *twl;
David Brownella603a7f2008-10-15 12:15:39 +0200320
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100321 if (unlikely(mod_no >= twl_get_last_module())) {
David Brownella603a7f2008-10-15 12:15:39 +0200322 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
323 return -EPERM;
324 }
Ilya Yanok8653be12011-08-01 23:00:28 +0200325 if (unlikely(!inuse)) {
326 pr_err("%s: not initialized\n", DRIVER_NAME);
327 return -EPERM;
328 }
Peter Ujfalusi050cde12013-01-16 14:53:51 +0100329
Balaji T Ke8deb282009-12-14 00:25:31 +0100330 sid = twl_map[mod_no].sid;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100331 twl = &twl_modules[sid];
David Brownella603a7f2008-10-15 12:15:39 +0200332
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100333 ret = regmap_bulk_write(twl->regmap, twl_map[mod_no].base + reg,
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100334 value, num_bytes);
David Brownella603a7f2008-10-15 12:15:39 +0200335
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100336 if (ret)
337 pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n",
338 DRIVER_NAME, mod_no, reg, num_bytes);
339
340 return ret;
David Brownella603a7f2008-10-15 12:15:39 +0200341}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100342EXPORT_SYMBOL(twl_i2c_write);
David Brownella603a7f2008-10-15 12:15:39 +0200343
344/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100345 * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200346 * @mod_no: module number
347 * @value: an array of num_bytes containing data to be read
348 * @reg: register address (just offset will do)
349 * @num_bytes: number of bytes to transfer
350 *
351 * Returns result of operation - num_bytes is success else failure.
352 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100353int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
David Brownella603a7f2008-10-15 12:15:39 +0200354{
355 int ret;
David Brownella603a7f2008-10-15 12:15:39 +0200356 int sid;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100357 struct twl_client *twl;
David Brownella603a7f2008-10-15 12:15:39 +0200358
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +0100359 if (unlikely(mod_no >= twl_get_last_module())) {
David Brownella603a7f2008-10-15 12:15:39 +0200360 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
361 return -EPERM;
362 }
Ilya Yanok8653be12011-08-01 23:00:28 +0200363 if (unlikely(!inuse)) {
364 pr_err("%s: not initialized\n", DRIVER_NAME);
365 return -EPERM;
366 }
Peter Ujfalusi050cde12013-01-16 14:53:51 +0100367
Balaji T Ke8deb282009-12-14 00:25:31 +0100368 sid = twl_map[mod_no].sid;
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100369 twl = &twl_modules[sid];
David Brownella603a7f2008-10-15 12:15:39 +0200370
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100371 ret = regmap_bulk_read(twl->regmap, twl_map[mod_no].base + reg,
372 value, num_bytes);
David Brownella603a7f2008-10-15 12:15:39 +0200373
Peter Ujfalusi2473d252012-11-13 09:28:44 +0100374 if (ret)
375 pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n",
376 DRIVER_NAME, mod_no, reg, num_bytes);
377
378 return ret;
David Brownella603a7f2008-10-15 12:15:39 +0200379}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100380EXPORT_SYMBOL(twl_i2c_read);
David Brownella603a7f2008-10-15 12:15:39 +0200381
382/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100383 * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200384 * @mod_no: module number
385 * @value: the value to be written 8 bit
386 * @reg: register address (just offset will do)
387 *
388 * Returns result of operation - 0 is success
389 */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100390int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
David Brownella603a7f2008-10-15 12:15:39 +0200391{
Peter Ujfalusi14591d82012-11-13 09:28:45 +0100392 return twl_i2c_write(mod_no, &value, reg, 1);
David Brownella603a7f2008-10-15 12:15:39 +0200393}
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100394EXPORT_SYMBOL(twl_i2c_write_u8);
David Brownella603a7f2008-10-15 12:15:39 +0200395
396/**
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100397 * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
David Brownella603a7f2008-10-15 12:15:39 +0200398 * @mod_no: module number
399 * @value: the value read 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_read_u8(u8 mod_no, u8 *value, u8 reg)
David Brownella603a7f2008-10-15 12:15:39 +0200405{
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100406 return twl_i2c_read(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_read_u8);
David Brownella603a7f2008-10-15 12:15:39 +0200409
410/*----------------------------------------------------------------------*/
411
Lesly A Mca972d12011-04-14 17:57:53 +0530412/**
413 * twl_read_idcode_register - API to read the IDCODE register.
414 *
415 * Unlocks the IDCODE register and read the 32 bit value.
416 */
417static int twl_read_idcode_register(void)
418{
419 int err;
420
421 err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
422 REG_UNLOCK_TEST_REG);
423 if (err) {
424 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
425 goto fail;
426 }
427
428 err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
429 REG_IDCODE_7_0, 4);
430 if (err) {
431 pr_err("TWL4030: unable to read IDCODE -%d\n", err);
432 goto fail;
433 }
434
435 err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
436 if (err)
437 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
438fail:
439 return err;
440}
441
442/**
443 * twl_get_type - API to get TWL Si type.
444 *
445 * Api to get the TWL Si type from IDCODE value.
446 */
447int twl_get_type(void)
448{
449 return TWL_SIL_TYPE(twl_idcode);
450}
451EXPORT_SYMBOL_GPL(twl_get_type);
452
453/**
454 * twl_get_version - API to get TWL Si version.
455 *
456 * Api to get the TWL Si version from IDCODE value.
457 */
458int twl_get_version(void)
459{
460 return TWL_SIL_REV(twl_idcode);
461}
462EXPORT_SYMBOL_GPL(twl_get_version);
463
Peter Ujfalusi2275c542012-09-10 13:46:22 +0300464/**
465 * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate.
466 *
467 * Api to get the TWL HFCLK rate based on BOOT_CFG register.
468 */
469int twl_get_hfclk_rate(void)
470{
471 u8 ctrl;
472 int rate;
473
474 twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &ctrl, R_CFG_BOOT);
475
476 switch (ctrl & 0x3) {
477 case HFCLK_FREQ_19p2_MHZ:
478 rate = 19200000;
479 break;
480 case HFCLK_FREQ_26_MHZ:
481 rate = 26000000;
482 break;
483 case HFCLK_FREQ_38p4_MHZ:
484 rate = 38400000;
485 break;
486 default:
487 pr_err("TWL4030: HFCLK is not configured\n");
488 rate = -EINVAL;
489 break;
490 }
491
492 return rate;
493}
494EXPORT_SYMBOL_GPL(twl_get_hfclk_rate);
495
David Brownelldad759f2008-12-01 00:43:58 +0100496static struct device *
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100497add_numbered_child(unsigned mod_no, const char *name, int num,
David Brownell5725d662008-12-01 00:31:04 +0100498 void *pdata, unsigned pdata_len,
499 bool can_wakeup, int irq0, int irq1)
500{
501 struct platform_device *pdev;
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100502 struct twl_client *twl;
503 int status, sid;
504
505 if (unlikely(mod_no >= twl_get_last_module())) {
506 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
507 return ERR_PTR(-EPERM);
508 }
509 sid = twl_map[mod_no].sid;
510 twl = &twl_modules[sid];
David Brownell5725d662008-12-01 00:31:04 +0100511
David Brownelldad759f2008-12-01 00:43:58 +0100512 pdev = platform_device_alloc(name, num);
David Brownell5725d662008-12-01 00:31:04 +0100513 if (!pdev) {
514 dev_dbg(&twl->client->dev, "can't alloc dev\n");
515 status = -ENOMEM;
516 goto err;
517 }
518
David Brownell5725d662008-12-01 00:31:04 +0100519 pdev->dev.parent = &twl->client->dev;
520
521 if (pdata) {
522 status = platform_device_add_data(pdev, pdata, pdata_len);
523 if (status < 0) {
524 dev_dbg(&pdev->dev, "can't add platform_data\n");
525 goto err;
526 }
527 }
528
529 if (irq0) {
530 struct resource r[2] = {
531 { .start = irq0, .flags = IORESOURCE_IRQ, },
532 { .start = irq1, .flags = IORESOURCE_IRQ, },
533 };
534
535 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
536 if (status < 0) {
537 dev_dbg(&pdev->dev, "can't add irqs\n");
538 goto err;
539 }
540 }
541
542 status = platform_device_add(pdev);
NeilBrown17ffba62012-07-07 08:51:03 +1000543 if (status == 0)
544 device_init_wakeup(&pdev->dev, can_wakeup);
David Brownell5725d662008-12-01 00:31:04 +0100545
546err:
547 if (status < 0) {
548 platform_device_put(pdev);
549 dev_err(&twl->client->dev, "can't add %s dev\n", name);
550 return ERR_PTR(status);
551 }
552 return &pdev->dev;
553}
554
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100555static inline struct device *add_child(unsigned mod_no, const char *name,
David Brownelldad759f2008-12-01 00:43:58 +0100556 void *pdata, unsigned pdata_len,
557 bool can_wakeup, int irq0, int irq1)
558{
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100559 return add_numbered_child(mod_no, name, -1, pdata, pdata_len,
David Brownelldad759f2008-12-01 00:43:58 +0100560 can_wakeup, irq0, irq1);
561}
562
563static struct device *
564add_regulator_linked(int num, struct regulator_init_data *pdata,
565 struct regulator_consumer_supply *consumers,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100566 unsigned num_consumers, unsigned long features)
David Brownelldad759f2008-12-01 00:43:58 +0100567{
Tero Kristo63bfff42012-02-16 12:27:52 +0200568 struct twl_regulator_driver_data drv_data;
569
David Brownelldad759f2008-12-01 00:43:58 +0100570 /* regulator framework demands init_data ... */
571 if (!pdata)
572 return NULL;
573
David Brownellb73eac72008-12-07 19:10:58 +0100574 if (consumers) {
David Brownelldad759f2008-12-01 00:43:58 +0100575 pdata->consumer_supplies = consumers;
576 pdata->num_consumer_supplies = num_consumers;
577 }
578
Tero Kristo63bfff42012-02-16 12:27:52 +0200579 if (pdata->driver_data) {
580 /* If we have existing drv_data, just add the flags */
581 struct twl_regulator_driver_data *tmp;
582 tmp = pdata->driver_data;
583 tmp->features |= features;
584 } else {
585 /* add new driver data struct, used only during init */
586 drv_data.features = features;
587 drv_data.set_voltage = NULL;
588 drv_data.get_voltage = NULL;
589 drv_data.data = NULL;
590 pdata->driver_data = &drv_data;
591 }
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100592
David Brownelldad759f2008-12-01 00:43:58 +0100593 /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100594 return add_numbered_child(TWL_MODULE_PM_MASTER, "twl_reg", num,
David Brownelldad759f2008-12-01 00:43:58 +0100595 pdata, sizeof(*pdata), false, 0, 0);
596}
597
598static struct device *
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100599add_regulator(int num, struct regulator_init_data *pdata,
600 unsigned long features)
David Brownelldad759f2008-12-01 00:43:58 +0100601{
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100602 return add_regulator_linked(num, pdata, NULL, 0, features);
David Brownelldad759f2008-12-01 00:43:58 +0100603}
604
David Brownella603a7f2008-10-15 12:15:39 +0200605/*
David Brownella30d46c2008-10-20 23:46:28 +0200606 * NOTE: We know the first 8 IRQs after pdata->base_irq are
607 * for the PIH, and the next are for the PWR_INT SIH, since
608 * that's how twl_init_irq() sets things up.
David Brownella603a7f2008-10-15 12:15:39 +0200609 */
David Brownella603a7f2008-10-15 12:15:39 +0200610
David Brownelldad759f2008-12-01 00:43:58 +0100611static int
Felipe Balbi9e178622012-02-22 14:32:16 +0200612add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
613 unsigned long features)
David Brownella603a7f2008-10-15 12:15:39 +0200614{
David Brownell5725d662008-12-01 00:31:04 +0100615 struct device *child;
David Brownella603a7f2008-10-15 12:15:39 +0200616
Thierry Redingf78959c2012-09-18 10:29:50 +0200617 if (IS_ENABLED(CONFIG_GPIO_TWL4030) && pdata->gpio) {
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100618 child = add_child(TWL4030_MODULE_GPIO, "twl4030_gpio",
David Brownell5725d662008-12-01 00:31:04 +0100619 pdata->gpio, sizeof(*pdata->gpio),
Felipe Balbi9e178622012-02-22 14:32:16 +0200620 false, irq_base + GPIO_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100621 if (IS_ERR(child))
622 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200623 }
624
Thierry Redingf78959c2012-09-18 10:29:50 +0200625 if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030) && pdata->keypad) {
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100626 child = add_child(TWL4030_MODULE_KEYPAD, "twl4030_keypad",
David Brownell5725d662008-12-01 00:31:04 +0100627 pdata->keypad, sizeof(*pdata->keypad),
Felipe Balbi9e178622012-02-22 14:32:16 +0200628 true, irq_base + KEYPAD_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100629 if (IS_ERR(child))
630 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200631 }
632
Peter Ujfalusi24ae36f2012-11-13 09:28:42 +0100633 if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc &&
634 twl_class_is_4030()) {
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100635 child = add_child(TWL4030_MODULE_MADC, "twl4030_madc",
David Brownell5725d662008-12-01 00:31:04 +0100636 pdata->madc, sizeof(*pdata->madc),
Felipe Balbi9e178622012-02-22 14:32:16 +0200637 true, irq_base + MADC_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100638 if (IS_ERR(child))
639 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200640 }
641
Thierry Redingf78959c2012-09-18 10:29:50 +0200642 if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030)) {
David Brownella603a7f2008-10-15 12:15:39 +0200643 /*
David Brownell5725d662008-12-01 00:31:04 +0100644 * REVISIT platform_data here currently might expose the
David Brownella603a7f2008-10-15 12:15:39 +0200645 * "msecure" line ... but for now we just expect board
David Brownell5725d662008-12-01 00:31:04 +0100646 * setup to tell the chip "it's always ok to SET_TIME".
David Brownella603a7f2008-10-15 12:15:39 +0200647 * Eventually, Linux might become more aware of such
648 * HW security concerns, and "least privilege".
649 */
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100650 child = add_child(TWL_MODULE_RTC, "twl_rtc", NULL, 0,
Felipe Balbi9e178622012-02-22 14:32:16 +0200651 true, irq_base + RTC_INTR_OFFSET, 0);
David Brownell5725d662008-12-01 00:31:04 +0100652 if (IS_ERR(child))
653 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200654 }
655
Peter Ujfalusiafc45892012-11-13 09:28:43 +0100656 if (IS_ENABLED(CONFIG_PWM_TWL)) {
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100657 child = add_child(TWL_MODULE_PWM, "twl-pwm", NULL, 0,
Peter Ujfalusiafc45892012-11-13 09:28:43 +0100658 false, 0, 0);
659 if (IS_ERR(child))
660 return PTR_ERR(child);
661 }
662
663 if (IS_ENABLED(CONFIG_PWM_TWL_LED)) {
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100664 child = add_child(TWL_MODULE_LED, "twl-pwmled", NULL, 0,
Thierry Reding48a364b2012-09-18 10:29:51 +0200665 false, 0, 0);
666 if (IS_ERR(child))
667 return PTR_ERR(child);
668 }
669
Thierry Redingf78959c2012-09-18 10:29:50 +0200670 if (IS_ENABLED(CONFIG_TWL4030_USB) && pdata->usb &&
671 twl_class_is_4030()) {
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300672
673 static struct regulator_consumer_supply usb1v5 = {
674 .supply = "usb1v5",
675 };
676 static struct regulator_consumer_supply usb1v8 = {
677 .supply = "usb1v8",
678 };
NeilBrownab378132012-05-09 07:40:40 +1000679 static struct regulator_consumer_supply usb3v1[] = {
680 { .supply = "usb3v1" },
681 { .supply = "bci3v1" },
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300682 };
683
684 /* First add the regulators so that they can be used by transceiver */
Thierry Redingf78959c2012-09-18 10:29:50 +0200685 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300686 /* this is a template that gets copied */
687 struct regulator_init_data usb_fixed = {
688 .constraints.valid_modes_mask =
689 REGULATOR_MODE_NORMAL
690 | REGULATOR_MODE_STANDBY,
691 .constraints.valid_ops_mask =
692 REGULATOR_CHANGE_MODE
693 | REGULATOR_CHANGE_STATUS,
694 };
695
696 child = add_regulator_linked(TWL4030_REG_VUSB1V5,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100697 &usb_fixed, &usb1v5, 1,
698 features);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300699 if (IS_ERR(child))
700 return PTR_ERR(child);
701
702 child = add_regulator_linked(TWL4030_REG_VUSB1V8,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100703 &usb_fixed, &usb1v8, 1,
704 features);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300705 if (IS_ERR(child))
706 return PTR_ERR(child);
707
708 child = add_regulator_linked(TWL4030_REG_VUSB3V1,
NeilBrownab378132012-05-09 07:40:40 +1000709 &usb_fixed, usb3v1, 2,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100710 features);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300711 if (IS_ERR(child))
712 return PTR_ERR(child);
713
714 }
715
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100716 child = add_child(TWL_MODULE_USB, "twl4030_usb",
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100717 pdata->usb, sizeof(*pdata->usb), true,
David Brownell5725d662008-12-01 00:31:04 +0100718 /* irq0 = USB_PRES, irq1 = USB */
Felipe Balbi9e178622012-02-22 14:32:16 +0200719 irq_base + USB_PRES_INTR_OFFSET,
720 irq_base + USB_INTR_OFFSET);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300721
David Brownell5725d662008-12-01 00:31:04 +0100722 if (IS_ERR(child))
723 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100724
725 /* we need to connect regulators to this transceiver */
Thierry Redingf78959c2012-09-18 10:29:50 +0200726 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child) {
Mark Brown1b65fa82012-02-03 11:02:58 +0000727 usb1v5.dev_name = dev_name(child);
728 usb1v8.dev_name = dev_name(child);
NeilBrownab378132012-05-09 07:40:40 +1000729 usb3v1[0].dev_name = dev_name(child);
Roger Quadrosf8ebdff2009-09-30 18:26:37 +0300730 }
David Brownelldad759f2008-12-01 00:43:58 +0100731 }
Thierry Redingf78959c2012-09-18 10:29:50 +0200732 if (IS_ENABLED(CONFIG_TWL6030_USB) && pdata->usb &&
733 twl_class_is_6030()) {
Hema HKe70357e2010-12-10 18:09:52 +0530734
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100735 static struct regulator_consumer_supply usb3v3;
736 int regulator;
Hema HKe70357e2010-12-10 18:09:52 +0530737
Thierry Redingf78959c2012-09-18 10:29:50 +0200738 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
Hema HKe70357e2010-12-10 18:09:52 +0530739 /* this is a template that gets copied */
740 struct regulator_init_data usb_fixed = {
741 .constraints.valid_modes_mask =
742 REGULATOR_MODE_NORMAL
743 | REGULATOR_MODE_STANDBY,
744 .constraints.valid_ops_mask =
745 REGULATOR_CHANGE_MODE
746 | REGULATOR_CHANGE_STATUS,
747 };
748
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100749 if (features & TWL6025_SUBCLASS) {
750 usb3v3.supply = "ldousb";
751 regulator = TWL6025_REG_LDOUSB;
752 } else {
753 usb3v3.supply = "vusb";
754 regulator = TWL6030_REG_VUSB;
755 }
756 child = add_regulator_linked(regulator, &usb_fixed,
757 &usb3v3, 1,
758 features);
Hema HKe70357e2010-12-10 18:09:52 +0530759 if (IS_ERR(child))
760 return PTR_ERR(child);
761 }
762
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100763 pdata->usb->features = features;
764
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100765 child = add_child(TWL_MODULE_USB, "twl6030_usb",
Peter Ujfalusi2d86ad32012-11-13 09:28:46 +0100766 pdata->usb, sizeof(*pdata->usb), true,
Hema HKe70357e2010-12-10 18:09:52 +0530767 /* irq1 = VBUS_PRES, irq0 = USB ID */
Felipe Balbi9e178622012-02-22 14:32:16 +0200768 irq_base + USBOTG_INTR_OFFSET,
769 irq_base + USB_PRES_INTR_OFFSET);
Hema HKe70357e2010-12-10 18:09:52 +0530770
771 if (IS_ERR(child))
772 return PTR_ERR(child);
773 /* we need to connect regulators to this transceiver */
Thierry Redingf78959c2012-09-18 10:29:50 +0200774 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child)
Mark Brown1b65fa82012-02-03 11:02:58 +0000775 usb3v3.dev_name = dev_name(child);
Thierry Redingf78959c2012-09-18 10:29:50 +0200776 } else if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) &&
777 twl_class_is_6030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100778 if (features & TWL6025_SUBCLASS)
779 child = add_regulator(TWL6025_REG_LDOUSB,
780 pdata->ldousb, features);
781 else
782 child = add_regulator(TWL6030_REG_VUSB,
783 pdata->vusb, features);
Hema HKe70357e2010-12-10 18:09:52 +0530784
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100785 if (IS_ERR(child))
786 return PTR_ERR(child);
Hema HKe70357e2010-12-10 18:09:52 +0530787 }
David Brownelldad759f2008-12-01 00:43:58 +0100788
Thierry Redingf78959c2012-09-18 10:29:50 +0200789 if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG) && twl_class_is_4030()) {
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100790 child = add_child(TWL_MODULE_PM_RECEIVER, "twl4030_wdt", NULL,
791 0, false, 0, 0);
Timo Kokkonen80e45b12009-03-27 16:42:17 +0200792 if (IS_ERR(child))
793 return PTR_ERR(child);
794 }
795
Thierry Redingf78959c2012-09-18 10:29:50 +0200796 if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON) && twl_class_is_4030()) {
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100797 child = add_child(TWL_MODULE_PM_MASTER, "twl4030_pwrbutton",
798 NULL, 0, true, irq_base + 8 + 0, 0);
Felipe Balbi9c3664d2009-08-03 18:16:38 +0200799 if (IS_ERR(child))
800 return PTR_ERR(child);
801 }
802
Thierry Redingf78959c2012-09-18 10:29:50 +0200803 if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO) && pdata->audio &&
804 twl_class_is_4030()) {
Peter Ujfalusi3c330272013-01-16 14:53:52 +0100805 child = add_child(TWL4030_MODULE_AUDIO_VOICE, "twl4030-audio",
Peter Ujfalusi4ae6df52011-05-31 15:21:13 +0300806 pdata->audio, sizeof(*pdata->audio),
Misael Lopez Cruzd62abe52010-02-23 18:10:19 -0600807 false, 0, 0);
808 if (IS_ERR(child))
809 return PTR_ERR(child);
810 }
811
Rajendra Nayak9da66532009-12-13 22:29:47 +0100812 /* twl4030 regulators */
Thierry Redingf78959c2012-09-18 10:29:50 +0200813 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_4030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100814 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
815 features);
David Brownelldad759f2008-12-01 00:43:58 +0100816 if (IS_ERR(child))
817 return PTR_ERR(child);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100818
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100819 child = add_regulator(TWL4030_REG_VIO, pdata->vio,
820 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100821 if (IS_ERR(child))
822 return PTR_ERR(child);
823
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100824 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
825 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100826 if (IS_ERR(child))
827 return PTR_ERR(child);
828
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100829 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
830 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100831 if (IS_ERR(child))
832 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100833
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100834 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
835 features);
David Brownelldad759f2008-12-01 00:43:58 +0100836 if (IS_ERR(child))
837 return PTR_ERR(child);
838
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100839 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
840 features);
David Brownelldad759f2008-12-01 00:43:58 +0100841 if (IS_ERR(child))
842 return PTR_ERR(child);
843
844 child = add_regulator((features & TWL4030_VAUX2)
845 ? TWL4030_REG_VAUX2_4030
846 : TWL4030_REG_VAUX2,
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100847 pdata->vaux2, features);
David Brownelldad759f2008-12-01 00:43:58 +0100848 if (IS_ERR(child))
849 return PTR_ERR(child);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100850
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100851 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
852 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100853 if (IS_ERR(child))
854 return PTR_ERR(child);
855
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100856 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
857 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100858 if (IS_ERR(child))
859 return PTR_ERR(child);
860
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100861 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
862 features);
Juha Keski-Saariab4abe052009-12-11 11:12:15 +0100863 if (IS_ERR(child))
864 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100865 }
866
David Brownelldad759f2008-12-01 00:43:58 +0100867 /* maybe add LDOs that are omitted on cost-reduced parts */
Thierry Redingf78959c2012-09-18 10:29:50 +0200868 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && !(features & TPS_SUBSET)
Rajendra Nayak9da66532009-12-13 22:29:47 +0100869 && twl_class_is_4030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100870 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
871 features);
David Brownelldad759f2008-12-01 00:43:58 +0100872 if (IS_ERR(child))
873 return PTR_ERR(child);
David Brownelldad759f2008-12-01 00:43:58 +0100874
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100875 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
876 features);
David Brownelldad759f2008-12-01 00:43:58 +0100877 if (IS_ERR(child))
878 return PTR_ERR(child);
879
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100880 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
881 features);
David Brownelldad759f2008-12-01 00:43:58 +0100882 if (IS_ERR(child))
883 return PTR_ERR(child);
884
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100885 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
886 features);
David Brownelldad759f2008-12-01 00:43:58 +0100887 if (IS_ERR(child))
888 return PTR_ERR(child);
889
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100890 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
891 features);
David Brownelldad759f2008-12-01 00:43:58 +0100892 if (IS_ERR(child))
893 return PTR_ERR(child);
894
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100895 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
896 features);
David Brownelldad759f2008-12-01 00:43:58 +0100897 if (IS_ERR(child))
898 return PTR_ERR(child);
David Brownella603a7f2008-10-15 12:15:39 +0200899 }
900
Rajendra Nayak9da66532009-12-13 22:29:47 +0100901 /* twl6030 regulators */
Thierry Redingf78959c2012-09-18 10:29:50 +0200902 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100903 !(features & TWL6025_SUBCLASS)) {
Tero Kristo34a38442012-02-28 15:09:10 +0530904 child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1,
905 features);
906 if (IS_ERR(child))
907 return PTR_ERR(child);
908
909 child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2,
910 features);
911 if (IS_ERR(child))
912 return PTR_ERR(child);
913
914 child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3,
915 features);
916 if (IS_ERR(child))
917 return PTR_ERR(child);
918
Peter Ujfalusi46eda3e2012-02-28 15:09:13 +0530919 child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8,
920 features);
921 if (IS_ERR(child))
922 return PTR_ERR(child);
923
924 child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1,
925 features);
926 if (IS_ERR(child))
927 return PTR_ERR(child);
928
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100929 child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
930 features);
931 if (IS_ERR(child))
932 return PTR_ERR(child);
933
934 child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
935 features);
936 if (IS_ERR(child))
937 return PTR_ERR(child);
938
939 child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
940 features);
941 if (IS_ERR(child))
942 return PTR_ERR(child);
943
944 child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
945 features);
946 if (IS_ERR(child))
947 return PTR_ERR(child);
948
949 child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
950 features);
951 if (IS_ERR(child))
952 return PTR_ERR(child);
953
954 child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
955 features);
956 if (IS_ERR(child))
957 return PTR_ERR(child);
958
959 child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
960 features);
961 if (IS_ERR(child))
962 return PTR_ERR(child);
963
964 child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
965 features);
966 if (IS_ERR(child))
967 return PTR_ERR(child);
968
969 child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
970 features);
971 if (IS_ERR(child))
972 return PTR_ERR(child);
973 }
974
975 /* 6030 and 6025 share this regulator */
Thierry Redingf78959c2012-09-18 10:29:50 +0200976 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030()) {
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100977 child = add_regulator(TWL6030_REG_VANA, pdata->vana,
978 features);
979 if (IS_ERR(child))
980 return PTR_ERR(child);
981 }
982
983 /* twl6025 regulators */
Thierry Redingf78959c2012-09-18 10:29:50 +0200984 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100985 (features & TWL6025_SUBCLASS)) {
986 child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
987 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +0100988 if (IS_ERR(child))
989 return PTR_ERR(child);
990
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100991 child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
992 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +0100993 if (IS_ERR(child))
994 return PTR_ERR(child);
995
Graeme Gregory521d8ec2011-05-12 14:27:55 +0100996 child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
997 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +0100998 if (IS_ERR(child))
999 return PTR_ERR(child);
1000
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001001 child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
1002 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001003 if (IS_ERR(child))
1004 return PTR_ERR(child);
1005
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001006 child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
1007 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001008 if (IS_ERR(child))
1009 return PTR_ERR(child);
1010
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001011 child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
1012 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001013 if (IS_ERR(child))
1014 return PTR_ERR(child);
1015
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001016 child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
1017 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001018 if (IS_ERR(child))
1019 return PTR_ERR(child);
1020
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001021 child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
1022 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001023 if (IS_ERR(child))
1024 return PTR_ERR(child);
1025
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001026 child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
1027 features);
Rajendra Nayak9da66532009-12-13 22:29:47 +01001028 if (IS_ERR(child))
1029 return PTR_ERR(child);
Balaji T K8e6de4a2011-02-10 18:44:50 +05301030
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001031 child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
1032 features);
Balaji T K8e6de4a2011-02-10 18:44:50 +05301033 if (IS_ERR(child))
1034 return PTR_ERR(child);
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001035
1036 child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
1037 features);
1038 if (IS_ERR(child))
1039 return PTR_ERR(child);
1040
Rajendra Nayak9da66532009-12-13 22:29:47 +01001041 }
1042
Thierry Redingf78959c2012-09-18 10:29:50 +02001043 if (IS_ENABLED(CONFIG_CHARGER_TWL4030) && pdata->bci &&
Grazvydas Ignotas11c39c42010-10-01 13:17:50 +03001044 !(features & (TPS_SUBSET | TWL5031))) {
Peter Ujfalusi3c330272013-01-16 14:53:52 +01001045 child = add_child(TWL_MODULE_MAIN_CHARGE, "twl4030_bci",
Grazvydas Ignotas11c39c42010-10-01 13:17:50 +03001046 pdata->bci, sizeof(*pdata->bci), false,
1047 /* irq0 = CHG_PRES, irq1 = BCI */
Felipe Balbi9e178622012-02-22 14:32:16 +02001048 irq_base + BCI_PRES_INTR_OFFSET,
1049 irq_base + BCI_INTR_OFFSET);
Grazvydas Ignotas11c39c42010-10-01 13:17:50 +03001050 if (IS_ERR(child))
1051 return PTR_ERR(child);
1052 }
1053
David Brownell5725d662008-12-01 00:31:04 +01001054 return 0;
David Brownella603a7f2008-10-15 12:15:39 +02001055}
1056
1057/*----------------------------------------------------------------------*/
1058
1059/*
1060 * These three functions initialize the on-chip clock framework,
1061 * letting it generate the right frequencies for USB, MADC, and
1062 * other purposes.
1063 */
1064static inline int __init protect_pm_master(void)
1065{
1066 int e = 0;
1067
Peter Ujfalusid640e752012-11-13 09:28:54 +01001068 e = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
1069 TWL4030_PM_MASTER_PROTECT_KEY);
David Brownella603a7f2008-10-15 12:15:39 +02001070 return e;
1071}
1072
1073static inline int __init unprotect_pm_master(void)
1074{
1075 int e = 0;
1076
Peter Ujfalusid640e752012-11-13 09:28:54 +01001077 e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
1078 TWL4030_PM_MASTER_PROTECT_KEY);
1079 e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG2,
1080 TWL4030_PM_MASTER_PROTECT_KEY);
Felipe Balbi49e6f872010-08-18 09:19:35 +03001081
David Brownella603a7f2008-10-15 12:15:39 +02001082 return e;
1083}
1084
Ilkka Koskinen38a68492009-10-22 14:14:09 +03001085static void clocks_init(struct device *dev,
1086 struct twl4030_clock_init_data *clock)
David Brownella603a7f2008-10-15 12:15:39 +02001087{
1088 int e = 0;
1089 struct clk *osc;
1090 u32 rate;
1091 u8 ctrl = HFCLK_FREQ_26_MHZ;
1092
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001093 osc = clk_get(dev, "fck");
David Brownella603a7f2008-10-15 12:15:39 +02001094 if (IS_ERR(osc)) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001095 printk(KERN_WARNING "Skipping twl internal clock init and "
David Brownella603a7f2008-10-15 12:15:39 +02001096 "using bootloader value (unknown osc rate)\n");
1097 return;
1098 }
1099
1100 rate = clk_get_rate(osc);
1101 clk_put(osc);
1102
1103 switch (rate) {
1104 case 19200000:
1105 ctrl = HFCLK_FREQ_19p2_MHZ;
1106 break;
1107 case 26000000:
1108 ctrl = HFCLK_FREQ_26_MHZ;
1109 break;
1110 case 38400000:
1111 ctrl = HFCLK_FREQ_38p4_MHZ;
1112 break;
1113 }
1114
1115 ctrl |= HIGH_PERF_SQ;
Ilkka Koskinen38a68492009-10-22 14:14:09 +03001116 if (clock && clock->ck32k_lowpwr_enable)
1117 ctrl |= CK32K_LOWPWR_EN;
1118
David Brownella603a7f2008-10-15 12:15:39 +02001119 e |= unprotect_pm_master();
1120 /* effect->MADC+USB ck en */
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001121 e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
David Brownella603a7f2008-10-15 12:15:39 +02001122 e |= protect_pm_master();
1123
1124 if (e < 0)
1125 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
1126}
1127
1128/*----------------------------------------------------------------------*/
1129
David Brownella603a7f2008-10-15 12:15:39 +02001130
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001131static int twl_remove(struct i2c_client *client)
David Brownella603a7f2008-10-15 12:15:39 +02001132{
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001133 unsigned i, num_slaves;
David Brownella30d46c2008-10-20 23:46:28 +02001134 int status;
David Brownella603a7f2008-10-15 12:15:39 +02001135
Peter Ujfalusi6dd810b2013-01-16 14:53:53 +01001136 if (twl_class_is_4030())
Balaji T Ke8deb282009-12-14 00:25:31 +01001137 status = twl4030_exit_irq();
Peter Ujfalusi6dd810b2013-01-16 14:53:53 +01001138 else
Balaji T Ke8deb282009-12-14 00:25:31 +01001139 status = twl6030_exit_irq();
1140
David Brownella30d46c2008-10-20 23:46:28 +02001141 if (status < 0)
1142 return status;
David Brownella603a7f2008-10-15 12:15:39 +02001143
Peter Ujfalusi6dd810b2013-01-16 14:53:53 +01001144 num_slaves = twl_get_num_slaves();
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001145 for (i = 0; i < num_slaves; i++) {
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001146 struct twl_client *twl = &twl_modules[i];
David Brownella603a7f2008-10-15 12:15:39 +02001147
1148 if (twl->client && twl->client != client)
1149 i2c_unregister_device(twl->client);
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001150 twl_modules[i].client = NULL;
David Brownella603a7f2008-10-15 12:15:39 +02001151 }
1152 inuse = false;
1153 return 0;
1154}
1155
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001156/* NOTE: This driver only handles a single twl4030/tps659x0 chip */
Bill Pembertonf791be42012-11-19 13:23:04 -05001157static int
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001158twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
David Brownella603a7f2008-10-15 12:15:39 +02001159{
David Brownella603a7f2008-10-15 12:15:39 +02001160 struct twl4030_platform_data *pdata = client->dev.platform_data;
Benoit Coussonaeb50322011-08-29 16:20:23 +02001161 struct device_node *node = client->dev.of_node;
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001162 struct platform_device *pdev;
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001163 struct regmap_config *twl_regmap_config;
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001164 int irq_base = 0;
1165 int status;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001166 unsigned i, num_slaves;
Benoit Coussonaeb50322011-08-29 16:20:23 +02001167
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001168 pdev = platform_device_alloc(DRIVER_NAME, -1);
1169 if (!pdev) {
1170 dev_err(&client->dev, "can't alloc pdev\n");
1171 return -ENOMEM;
1172 }
1173
1174 status = platform_device_add(pdev);
1175 if (status) {
1176 platform_device_put(pdev);
1177 return status;
1178 }
1179
Benoit Coussonaeb50322011-08-29 16:20:23 +02001180 if (node && !pdata) {
1181 /*
1182 * XXX: Temporary pdata until the information is correctly
1183 * retrieved by every TWL modules from DT.
1184 */
1185 pdata = devm_kzalloc(&client->dev,
1186 sizeof(struct twl4030_platform_data),
1187 GFP_KERNEL);
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001188 if (!pdata) {
1189 status = -ENOMEM;
1190 goto free;
1191 }
Benoit Coussonaeb50322011-08-29 16:20:23 +02001192 }
David Brownella603a7f2008-10-15 12:15:39 +02001193
1194 if (!pdata) {
1195 dev_dbg(&client->dev, "no platform data?\n");
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001196 status = -EINVAL;
1197 goto free;
David Brownella603a7f2008-10-15 12:15:39 +02001198 }
1199
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001200 platform_set_drvdata(pdev, pdata);
1201
David Brownella603a7f2008-10-15 12:15:39 +02001202 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1203 dev_dbg(&client->dev, "can't talk I2C?\n");
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001204 status = -EIO;
1205 goto free;
David Brownella603a7f2008-10-15 12:15:39 +02001206 }
1207
David Brownella30d46c2008-10-20 23:46:28 +02001208 if (inuse) {
David Brownella603a7f2008-10-15 12:15:39 +02001209 dev_dbg(&client->dev, "driver is already in use\n");
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001210 status = -EBUSY;
1211 goto free;
David Brownella603a7f2008-10-15 12:15:39 +02001212 }
1213
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001214 if ((id->driver_data) & TWL6030_CLASS) {
1215 twl_id = TWL6030_CLASS_ID;
1216 twl_map = &twl6030_map[0];
Peter Ujfalusi5d4e9bd2013-01-16 14:53:50 +01001217 /* The charger base address is different in twl6025 */
1218 if ((id->driver_data) & TWL6025_SUBCLASS)
1219 twl_map[TWL_MODULE_MAIN_CHARGE].base =
1220 TWL6025_BASEADD_CHARGER;
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001221 twl_regmap_config = twl6030_regmap_config;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001222 } else {
1223 twl_id = TWL4030_CLASS_ID;
1224 twl_map = &twl4030_map[0];
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001225 twl_regmap_config = twl4030_regmap_config;
Peter Ujfalusi6dd810b2013-01-16 14:53:53 +01001226 }
1227
1228 num_slaves = twl_get_num_slaves();
1229 twl_modules = devm_kzalloc(&client->dev,
1230 sizeof(struct twl_client) * num_slaves,
1231 GFP_KERNEL);
1232 if (!twl_modules) {
1233 status = -ENOMEM;
1234 goto free;
Peter Ujfalusi364cedb2012-01-04 14:58:33 +02001235 }
1236
1237 for (i = 0; i < num_slaves; i++) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001238 struct twl_client *twl = &twl_modules[i];
David Brownella603a7f2008-10-15 12:15:39 +02001239
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001240 if (i == 0) {
David Brownella603a7f2008-10-15 12:15:39 +02001241 twl->client = client;
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001242 } else {
David Brownella603a7f2008-10-15 12:15:39 +02001243 twl->client = i2c_new_dummy(client->adapter,
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001244 client->addr + i);
David Brownella603a7f2008-10-15 12:15:39 +02001245 if (!twl->client) {
Ilkka Koskinena8643432009-10-20 16:22:52 +03001246 dev_err(&client->dev,
David Brownella603a7f2008-10-15 12:15:39 +02001247 "can't attach client %d\n", i);
1248 status = -ENOMEM;
1249 goto fail;
1250 }
David Brownella603a7f2008-10-15 12:15:39 +02001251 }
Peter Ujfalusi2473d252012-11-13 09:28:44 +01001252
1253 twl->regmap = devm_regmap_init_i2c(twl->client,
1254 &twl_regmap_config[i]);
1255 if (IS_ERR(twl->regmap)) {
1256 status = PTR_ERR(twl->regmap);
1257 dev_err(&client->dev,
1258 "Failed to allocate regmap %d, err: %d\n", i,
1259 status);
1260 goto fail;
1261 }
David Brownella603a7f2008-10-15 12:15:39 +02001262 }
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001263
David Brownella603a7f2008-10-15 12:15:39 +02001264 inuse = true;
1265
1266 /* setup clock framework */
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001267 clocks_init(&pdev->dev, pdata->clock);
David Brownella603a7f2008-10-15 12:15:39 +02001268
Lesly A Mca972d12011-04-14 17:57:53 +05301269 /* read TWL IDCODE Register */
1270 if (twl_id == TWL4030_CLASS_ID) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001271 status = twl_read_idcode_register();
1272 WARN(status < 0, "Error: reading twl_idcode register value\n");
Lesly A Mca972d12011-04-14 17:57:53 +05301273 }
1274
Amit Kucheriaebf0bd32009-08-31 18:32:18 +02001275 /* load power event scripts */
Thierry Redingf78959c2012-09-18 10:29:50 +02001276 if (IS_ENABLED(CONFIG_TWL4030_POWER) && pdata->power)
Amit Kucheriaebf0bd32009-08-31 18:32:18 +02001277 twl4030_power_init(pdata->power);
1278
David Brownella603a7f2008-10-15 12:15:39 +02001279 /* Maybe init the T2 Interrupt subsystem */
Felipe Balbi9e178622012-02-22 14:32:16 +02001280 if (client->irq) {
Balaji T Ke8deb282009-12-14 00:25:31 +01001281 if (twl_class_is_4030()) {
1282 twl4030_init_chip_irq(id->name);
Benoit Cousson78518ff2012-02-29 19:40:31 +01001283 irq_base = twl4030_init_irq(&client->dev, client->irq);
Balaji T Ke8deb282009-12-14 00:25:31 +01001284 } else {
Benoit Cousson78518ff2012-02-29 19:40:31 +01001285 irq_base = twl6030_init_irq(&client->dev, client->irq);
Balaji T Ke8deb282009-12-14 00:25:31 +01001286 }
1287
Benoit Cousson78518ff2012-02-29 19:40:31 +01001288 if (irq_base < 0) {
1289 status = irq_base;
David Brownella30d46c2008-10-20 23:46:28 +02001290 goto fail;
Benoit Cousson78518ff2012-02-29 19:40:31 +01001291 }
David Brownella603a7f2008-10-15 12:15:39 +02001292 }
1293
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001294 /*
1295 * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001296 * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1297 * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1298 */
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001299 if (twl_class_is_4030()) {
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001300 u8 temp;
1301
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001302 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1303 temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001304 I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
Moiz Sonasatha29aaf52010-02-16 18:57:21 -06001305 twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1306 }
1307
Benoit Coussonaeb50322011-08-29 16:20:23 +02001308 if (node)
1309 status = of_platform_populate(node, NULL, NULL, &client->dev);
Peter Ujfalusie5812382013-01-16 14:53:54 +01001310 else
Felipe Balbi9e178622012-02-22 14:32:16 +02001311 status = add_children(pdata, irq_base, id->driver_data);
Benoit Coussonaeb50322011-08-29 16:20:23 +02001312
David Brownella603a7f2008-10-15 12:15:39 +02001313fail:
1314 if (status < 0)
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001315 twl_remove(client);
Tony Lindgrendefa6be2012-09-17 16:26:10 -07001316free:
1317 if (status < 0)
1318 platform_device_unregister(pdev);
Benoit Coussonec1a07b2012-03-02 11:11:26 +01001319
David Brownella603a7f2008-10-15 12:15:39 +02001320 return status;
1321}
1322
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001323static const struct i2c_device_id twl_ids[] = {
David Brownelldad759f2008-12-01 00:43:58 +01001324 { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */
1325 { "twl5030", 0 }, /* T2 updated */
Ilkka Koskinen1920a612009-11-10 17:26:15 +02001326 { "twl5031", TWL5031 }, /* TWL5030 updated */
David Brownelldad759f2008-12-01 00:43:58 +01001327 { "tps65950", 0 }, /* catalog version of twl5030 */
1328 { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */
1329 { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */
Oleg Drokin59dead52011-07-01 13:34:13 +02001330 { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED
1331 and vibrator. Charger in USB module*/
Balaji T Ke8deb282009-12-14 00:25:31 +01001332 { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */
Graeme Gregory521d8ec2011-05-12 14:27:55 +01001333 { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
David Brownella603a7f2008-10-15 12:15:39 +02001334 { /* end of list */ },
1335};
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001336MODULE_DEVICE_TABLE(i2c, twl_ids);
David Brownella603a7f2008-10-15 12:15:39 +02001337
1338/* One Client Driver , 4 Clients */
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001339static struct i2c_driver twl_driver = {
David Brownella603a7f2008-10-15 12:15:39 +02001340 .driver.name = DRIVER_NAME,
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001341 .id_table = twl_ids,
1342 .probe = twl_probe,
1343 .remove = twl_remove,
David Brownella603a7f2008-10-15 12:15:39 +02001344};
1345
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001346static int __init twl_init(void)
David Brownella603a7f2008-10-15 12:15:39 +02001347{
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001348 return i2c_add_driver(&twl_driver);
David Brownella603a7f2008-10-15 12:15:39 +02001349}
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001350subsys_initcall(twl_init);
David Brownella603a7f2008-10-15 12:15:39 +02001351
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001352static void __exit twl_exit(void)
David Brownella603a7f2008-10-15 12:15:39 +02001353{
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001354 i2c_del_driver(&twl_driver);
David Brownella603a7f2008-10-15 12:15:39 +02001355}
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001356module_exit(twl_exit);
David Brownella603a7f2008-10-15 12:15:39 +02001357
1358MODULE_AUTHOR("Texas Instruments, Inc.");
Balaji T Kfc7b92f2009-12-13 21:23:33 +01001359MODULE_DESCRIPTION("I2C Core interface for TWL");
David Brownella603a7f2008-10-15 12:15:39 +02001360MODULE_LICENSE("GPL");