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