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