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 | 346 | ret = regmap_bulk_write(twl->regmap, twl_map[mod_no].base + reg, |
14591d88 | 347 | value, num_bytes); |
2473d25a PU |
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 | 408 | { |
14591d88 | 409 | return twl_i2c_write(mod_no, &value, reg, 1); |
a603a7fa | 410 | } |
fc7b92fc | 411 | EXPORT_SYMBOL(twl_i2c_write_u8); |
a603a7fa DB |
412 | |
413 | /** | |
fc7b92fc | 414 | * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0 |
a603a7fa DB |
415 | * @mod_no: module number |
416 | * @value: the value read 8 bit | |
417 | * @reg: register address (just offset will do) | |
418 | * | |
419 | * Returns result of operation - 0 is success | |
420 | */ | |
fc7b92fc | 421 | int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg) |
a603a7fa | 422 | { |
fc7b92fc | 423 | return twl_i2c_read(mod_no, value, reg, 1); |
a603a7fa | 424 | } |
fc7b92fc | 425 | EXPORT_SYMBOL(twl_i2c_read_u8); |
a603a7fa DB |
426 | |
427 | /*----------------------------------------------------------------------*/ | |
428 | ||
ca972d13 L |
429 | /** |
430 | * twl_read_idcode_register - API to read the IDCODE register. | |
431 | * | |
432 | * Unlocks the IDCODE register and read the 32 bit value. | |
433 | */ | |
434 | static int twl_read_idcode_register(void) | |
435 | { | |
436 | int err; | |
437 | ||
438 | err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK, | |
439 | REG_UNLOCK_TEST_REG); | |
440 | if (err) { | |
441 | pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err); | |
442 | goto fail; | |
443 | } | |
444 | ||
445 | err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode), | |
446 | REG_IDCODE_7_0, 4); | |
447 | if (err) { | |
448 | pr_err("TWL4030: unable to read IDCODE -%d\n", err); | |
449 | goto fail; | |
450 | } | |
451 | ||
452 | err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG); | |
453 | if (err) | |
454 | pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err); | |
455 | fail: | |
456 | return err; | |
457 | } | |
458 | ||
459 | /** | |
460 | * twl_get_type - API to get TWL Si type. | |
461 | * | |
462 | * Api to get the TWL Si type from IDCODE value. | |
463 | */ | |
464 | int twl_get_type(void) | |
465 | { | |
466 | return TWL_SIL_TYPE(twl_idcode); | |
467 | } | |
468 | EXPORT_SYMBOL_GPL(twl_get_type); | |
469 | ||
470 | /** | |
471 | * twl_get_version - API to get TWL Si version. | |
472 | * | |
473 | * Api to get the TWL Si version from IDCODE value. | |
474 | */ | |
475 | int twl_get_version(void) | |
476 | { | |
477 | return TWL_SIL_REV(twl_idcode); | |
478 | } | |
479 | EXPORT_SYMBOL_GPL(twl_get_version); | |
480 | ||
2275c544 PU |
481 | /** |
482 | * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate. | |
483 | * | |
484 | * Api to get the TWL HFCLK rate based on BOOT_CFG register. | |
485 | */ | |
486 | int twl_get_hfclk_rate(void) | |
487 | { | |
488 | u8 ctrl; | |
489 | int rate; | |
490 | ||
491 | twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &ctrl, R_CFG_BOOT); | |
492 | ||
493 | switch (ctrl & 0x3) { | |
494 | case HFCLK_FREQ_19p2_MHZ: | |
495 | rate = 19200000; | |
496 | break; | |
497 | case HFCLK_FREQ_26_MHZ: | |
498 | rate = 26000000; | |
499 | break; | |
500 | case HFCLK_FREQ_38p4_MHZ: | |
501 | rate = 38400000; | |
502 | break; | |
503 | default: | |
504 | pr_err("TWL4030: HFCLK is not configured\n"); | |
505 | rate = -EINVAL; | |
506 | break; | |
507 | } | |
508 | ||
509 | return rate; | |
510 | } | |
511 | EXPORT_SYMBOL_GPL(twl_get_hfclk_rate); | |
512 | ||
dad759ff DB |
513 | static struct device * |
514 | add_numbered_child(unsigned chip, const char *name, int num, | |
5725d66b DB |
515 | void *pdata, unsigned pdata_len, |
516 | bool can_wakeup, int irq0, int irq1) | |
a603a7fa | 517 | { |
5725d66b | 518 | struct platform_device *pdev; |
fc7b92fc | 519 | struct twl_client *twl = &twl_modules[chip]; |
5725d66b DB |
520 | int status; |
521 | ||
dad759ff | 522 | pdev = platform_device_alloc(name, num); |
5725d66b DB |
523 | if (!pdev) { |
524 | dev_dbg(&twl->client->dev, "can't alloc dev\n"); | |
525 | status = -ENOMEM; | |
526 | goto err; | |
527 | } | |
a603a7fa | 528 | |
5725d66b | 529 | pdev->dev.parent = &twl->client->dev; |
a603a7fa | 530 | |
5725d66b DB |
531 | if (pdata) { |
532 | status = platform_device_add_data(pdev, pdata, pdata_len); | |
533 | if (status < 0) { | |
534 | dev_dbg(&pdev->dev, "can't add platform_data\n"); | |
a603a7fa DB |
535 | goto err; |
536 | } | |
5725d66b | 537 | } |
a603a7fa | 538 | |
5725d66b DB |
539 | if (irq0) { |
540 | struct resource r[2] = { | |
541 | { .start = irq0, .flags = IORESOURCE_IRQ, }, | |
542 | { .start = irq1, .flags = IORESOURCE_IRQ, }, | |
543 | }; | |
a603a7fa | 544 | |
5725d66b | 545 | status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1); |
a603a7fa | 546 | if (status < 0) { |
5725d66b | 547 | dev_dbg(&pdev->dev, "can't add irqs\n"); |
a603a7fa DB |
548 | goto err; |
549 | } | |
550 | } | |
551 | ||
5725d66b | 552 | status = platform_device_add(pdev); |
17ffba6a N |
553 | if (status == 0) |
554 | device_init_wakeup(&pdev->dev, can_wakeup); | |
a603a7fa | 555 | |
5725d66b DB |
556 | err: |
557 | if (status < 0) { | |
558 | platform_device_put(pdev); | |
559 | dev_err(&twl->client->dev, "can't add %s dev\n", name); | |
560 | return ERR_PTR(status); | |
561 | } | |
562 | return &pdev->dev; | |
563 | } | |
a603a7fa | 564 | |
dad759ff DB |
565 | static inline struct device *add_child(unsigned chip, const char *name, |
566 | void *pdata, unsigned pdata_len, | |
567 | bool can_wakeup, int irq0, int irq1) | |
568 | { | |
569 | return add_numbered_child(chip, name, -1, pdata, pdata_len, | |
570 | can_wakeup, irq0, irq1); | |
571 | } | |
572 | ||
573 | static struct device * | |
574 | add_regulator_linked(int num, struct regulator_init_data *pdata, | |
575 | struct regulator_consumer_supply *consumers, | |
521d8ec3 | 576 | unsigned num_consumers, unsigned long features) |
dad759ff | 577 | { |
e8deb28c | 578 | unsigned sub_chip_id; |
63bfff4e TK |
579 | struct twl_regulator_driver_data drv_data; |
580 | ||
dad759ff DB |
581 | /* regulator framework demands init_data ... */ |
582 | if (!pdata) | |
583 | return NULL; | |
584 | ||
b73eac78 | 585 | if (consumers) { |
dad759ff DB |
586 | pdata->consumer_supplies = consumers; |
587 | pdata->num_consumer_supplies = num_consumers; | |
588 | } | |
589 | ||
63bfff4e TK |
590 | if (pdata->driver_data) { |
591 | /* If we have existing drv_data, just add the flags */ | |
592 | struct twl_regulator_driver_data *tmp; | |
593 | tmp = pdata->driver_data; | |
594 | tmp->features |= features; | |
595 | } else { | |
596 | /* add new driver data struct, used only during init */ | |
597 | drv_data.features = features; | |
598 | drv_data.set_voltage = NULL; | |
599 | drv_data.get_voltage = NULL; | |
600 | drv_data.data = NULL; | |
601 | pdata->driver_data = &drv_data; | |
602 | } | |
521d8ec3 | 603 | |
dad759ff | 604 | /* NOTE: we currently ignore regulator IRQs, e.g. for short circuits */ |
e8deb28c B |
605 | sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid; |
606 | return add_numbered_child(sub_chip_id, "twl_reg", num, | |
dad759ff DB |
607 | pdata, sizeof(*pdata), false, 0, 0); |
608 | } | |
609 | ||
610 | static struct device * | |
521d8ec3 GG |
611 | add_regulator(int num, struct regulator_init_data *pdata, |
612 | unsigned long features) | |
dad759ff | 613 | { |
521d8ec3 | 614 | return add_regulator_linked(num, pdata, NULL, 0, features); |
dad759ff DB |
615 | } |
616 | ||
5725d66b DB |
617 | /* |
618 | * NOTE: We know the first 8 IRQs after pdata->base_irq are | |
619 | * for the PIH, and the next are for the PWR_INT SIH, since | |
620 | * that's how twl_init_irq() sets things up. | |
621 | */ | |
a603a7fa | 622 | |
dad759ff | 623 | static int |
9e178620 FB |
624 | add_children(struct twl4030_platform_data *pdata, unsigned irq_base, |
625 | unsigned long features) | |
5725d66b DB |
626 | { |
627 | struct device *child; | |
e8deb28c | 628 | unsigned sub_chip_id; |
a603a7fa | 629 | |
f78959cf | 630 | if (IS_ENABLED(CONFIG_GPIO_TWL4030) && pdata->gpio) { |
fc7b92fc | 631 | child = add_child(SUB_CHIP_ID1, "twl4030_gpio", |
5725d66b | 632 | pdata->gpio, sizeof(*pdata->gpio), |
9e178620 | 633 | false, irq_base + GPIO_INTR_OFFSET, 0); |
5725d66b DB |
634 | if (IS_ERR(child)) |
635 | return PTR_ERR(child); | |
a603a7fa DB |
636 | } |
637 | ||
f78959cf | 638 | if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030) && pdata->keypad) { |
fc7b92fc | 639 | child = add_child(SUB_CHIP_ID2, "twl4030_keypad", |
5725d66b | 640 | pdata->keypad, sizeof(*pdata->keypad), |
9e178620 | 641 | true, irq_base + KEYPAD_INTR_OFFSET, 0); |
5725d66b DB |
642 | if (IS_ERR(child)) |
643 | return PTR_ERR(child); | |
a603a7fa DB |
644 | } |
645 | ||
24ae36f5 PU |
646 | if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc && |
647 | twl_class_is_4030()) { | |
5725d66b DB |
648 | child = add_child(2, "twl4030_madc", |
649 | pdata->madc, sizeof(*pdata->madc), | |
9e178620 | 650 | true, irq_base + MADC_INTR_OFFSET, 0); |
5725d66b DB |
651 | if (IS_ERR(child)) |
652 | return PTR_ERR(child); | |
a603a7fa DB |
653 | } |
654 | ||
f78959cf | 655 | if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030)) { |
a603a7fa | 656 | /* |
5725d66b | 657 | * REVISIT platform_data here currently might expose the |
a603a7fa | 658 | * "msecure" line ... but for now we just expect board |
5725d66b | 659 | * setup to tell the chip "it's always ok to SET_TIME". |
a603a7fa DB |
660 | * Eventually, Linux might become more aware of such |
661 | * HW security concerns, and "least privilege". | |
662 | */ | |
e8deb28c B |
663 | sub_chip_id = twl_map[TWL_MODULE_RTC].sid; |
664 | child = add_child(sub_chip_id, "twl_rtc", | |
5725d66b | 665 | NULL, 0, |
9e178620 | 666 | true, irq_base + RTC_INTR_OFFSET, 0); |
5725d66b DB |
667 | if (IS_ERR(child)) |
668 | return PTR_ERR(child); | |
a603a7fa DB |
669 | } |
670 | ||
afc45898 PU |
671 | if (IS_ENABLED(CONFIG_PWM_TWL)) { |
672 | child = add_child(SUB_CHIP_ID1, "twl-pwm", NULL, 0, | |
673 | false, 0, 0); | |
674 | if (IS_ERR(child)) | |
675 | return PTR_ERR(child); | |
676 | } | |
677 | ||
678 | if (IS_ENABLED(CONFIG_PWM_TWL_LED)) { | |
679 | child = add_child(SUB_CHIP_ID1, "twl-pwmled", NULL, 0, | |
48a364b7 TR |
680 | false, 0, 0); |
681 | if (IS_ERR(child)) | |
682 | return PTR_ERR(child); | |
683 | } | |
684 | ||
f78959cf TR |
685 | if (IS_ENABLED(CONFIG_TWL4030_USB) && pdata->usb && |
686 | twl_class_is_4030()) { | |
f8ebdff0 RQ |
687 | |
688 | static struct regulator_consumer_supply usb1v5 = { | |
689 | .supply = "usb1v5", | |
690 | }; | |
691 | static struct regulator_consumer_supply usb1v8 = { | |
692 | .supply = "usb1v8", | |
693 | }; | |
ab37813f N |
694 | static struct regulator_consumer_supply usb3v1[] = { |
695 | { .supply = "usb3v1" }, | |
696 | { .supply = "bci3v1" }, | |
f8ebdff0 RQ |
697 | }; |
698 | ||
699 | /* First add the regulators so that they can be used by transceiver */ | |
f78959cf | 700 | if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) { |
f8ebdff0 RQ |
701 | /* this is a template that gets copied */ |
702 | struct regulator_init_data usb_fixed = { | |
703 | .constraints.valid_modes_mask = | |
704 | REGULATOR_MODE_NORMAL | |
705 | | REGULATOR_MODE_STANDBY, | |
706 | .constraints.valid_ops_mask = | |
707 | REGULATOR_CHANGE_MODE | |
708 | | REGULATOR_CHANGE_STATUS, | |
709 | }; | |
710 | ||
711 | child = add_regulator_linked(TWL4030_REG_VUSB1V5, | |
521d8ec3 GG |
712 | &usb_fixed, &usb1v5, 1, |
713 | features); | |
f8ebdff0 RQ |
714 | if (IS_ERR(child)) |
715 | return PTR_ERR(child); | |
716 | ||
717 | child = add_regulator_linked(TWL4030_REG_VUSB1V8, | |
521d8ec3 GG |
718 | &usb_fixed, &usb1v8, 1, |
719 | features); | |
f8ebdff0 RQ |
720 | if (IS_ERR(child)) |
721 | return PTR_ERR(child); | |
722 | ||
723 | child = add_regulator_linked(TWL4030_REG_VUSB3V1, | |
ab37813f | 724 | &usb_fixed, usb3v1, 2, |
521d8ec3 | 725 | features); |
f8ebdff0 RQ |
726 | if (IS_ERR(child)) |
727 | return PTR_ERR(child); | |
728 | ||
729 | } | |
730 | ||
5725d66b DB |
731 | child = add_child(0, "twl4030_usb", |
732 | pdata->usb, sizeof(*pdata->usb), | |
733 | true, | |
734 | /* irq0 = USB_PRES, irq1 = USB */ | |
9e178620 FB |
735 | irq_base + USB_PRES_INTR_OFFSET, |
736 | irq_base + USB_INTR_OFFSET); | |
f8ebdff0 | 737 | |
5725d66b DB |
738 | if (IS_ERR(child)) |
739 | return PTR_ERR(child); | |
dad759ff DB |
740 | |
741 | /* we need to connect regulators to this transceiver */ | |
f78959cf | 742 | if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child) { |
1b65fa84 MB |
743 | usb1v5.dev_name = dev_name(child); |
744 | usb1v8.dev_name = dev_name(child); | |
ab37813f | 745 | usb3v1[0].dev_name = dev_name(child); |
f8ebdff0 | 746 | } |
dad759ff | 747 | } |
f78959cf TR |
748 | if (IS_ENABLED(CONFIG_TWL6030_USB) && pdata->usb && |
749 | twl_class_is_6030()) { | |
e70357e3 | 750 | |
521d8ec3 GG |
751 | static struct regulator_consumer_supply usb3v3; |
752 | int regulator; | |
e70357e3 | 753 | |
f78959cf | 754 | if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) { |
e70357e3 HH |
755 | /* this is a template that gets copied */ |
756 | struct regulator_init_data usb_fixed = { | |
757 | .constraints.valid_modes_mask = | |
758 | REGULATOR_MODE_NORMAL | |
759 | | REGULATOR_MODE_STANDBY, | |
760 | .constraints.valid_ops_mask = | |
761 | REGULATOR_CHANGE_MODE | |
762 | | REGULATOR_CHANGE_STATUS, | |
763 | }; | |
764 | ||
521d8ec3 GG |
765 | if (features & TWL6025_SUBCLASS) { |
766 | usb3v3.supply = "ldousb"; | |
767 | regulator = TWL6025_REG_LDOUSB; | |
768 | } else { | |
769 | usb3v3.supply = "vusb"; | |
770 | regulator = TWL6030_REG_VUSB; | |
771 | } | |
772 | child = add_regulator_linked(regulator, &usb_fixed, | |
773 | &usb3v3, 1, | |
774 | features); | |
e70357e3 HH |
775 | if (IS_ERR(child)) |
776 | return PTR_ERR(child); | |
777 | } | |
778 | ||
521d8ec3 GG |
779 | pdata->usb->features = features; |
780 | ||
e70357e3 HH |
781 | child = add_child(0, "twl6030_usb", |
782 | pdata->usb, sizeof(*pdata->usb), | |
783 | true, | |
784 | /* irq1 = VBUS_PRES, irq0 = USB ID */ | |
9e178620 FB |
785 | irq_base + USBOTG_INTR_OFFSET, |
786 | irq_base + USB_PRES_INTR_OFFSET); | |
e70357e3 HH |
787 | |
788 | if (IS_ERR(child)) | |
789 | return PTR_ERR(child); | |
790 | /* we need to connect regulators to this transceiver */ | |
f78959cf | 791 | if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child) |
1b65fa84 | 792 | usb3v3.dev_name = dev_name(child); |
f78959cf TR |
793 | } else if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && |
794 | twl_class_is_6030()) { | |
521d8ec3 GG |
795 | if (features & TWL6025_SUBCLASS) |
796 | child = add_regulator(TWL6025_REG_LDOUSB, | |
797 | pdata->ldousb, features); | |
798 | else | |
799 | child = add_regulator(TWL6030_REG_VUSB, | |
800 | pdata->vusb, features); | |
e70357e3 | 801 | |
521d8ec3 GG |
802 | if (IS_ERR(child)) |
803 | return PTR_ERR(child); | |
e70357e3 | 804 | } |
dad759ff | 805 | |
f78959cf | 806 | if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG) && twl_class_is_4030()) { |
80e45b1e TK |
807 | child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0); |
808 | if (IS_ERR(child)) | |
9c3664dd FB |
809 | return PTR_ERR(child); |
810 | } | |
811 | ||
f78959cf | 812 | if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON) && twl_class_is_4030()) { |
9c3664dd | 813 | child = add_child(1, "twl4030_pwrbutton", |
9e178620 | 814 | NULL, 0, true, irq_base + 8 + 0, 0); |
9c3664dd | 815 | if (IS_ERR(child)) |
80e45b1e TK |
816 | return PTR_ERR(child); |
817 | } | |
818 | ||
f78959cf TR |
819 | if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO) && pdata->audio && |
820 | twl_class_is_4030()) { | |
d62abe56 | 821 | sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid; |
f0fba2ad | 822 | child = add_child(sub_chip_id, "twl4030-audio", |
4ae6df5e | 823 | pdata->audio, sizeof(*pdata->audio), |
d62abe56 MLC |
824 | false, 0, 0); |
825 | if (IS_ERR(child)) | |
826 | return PTR_ERR(child); | |
827 | } | |
828 | ||
9da66539 | 829 | /* twl4030 regulators */ |
f78959cf | 830 | if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_4030()) { |
521d8ec3 GG |
831 | child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1, |
832 | features); | |
dad759ff DB |
833 | if (IS_ERR(child)) |
834 | return PTR_ERR(child); | |
ab4abe05 | 835 | |
521d8ec3 GG |
836 | child = add_regulator(TWL4030_REG_VIO, pdata->vio, |
837 | features); | |
ab4abe05 JKS |
838 | if (IS_ERR(child)) |
839 | return PTR_ERR(child); | |
840 | ||
521d8ec3 GG |
841 | child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1, |
842 | features); | |
ab4abe05 JKS |
843 | if (IS_ERR(child)) |
844 | return PTR_ERR(child); | |
845 | ||
521d8ec3 GG |
846 | child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2, |
847 | features); | |
ab4abe05 JKS |
848 | if (IS_ERR(child)) |
849 | return PTR_ERR(child); | |
dad759ff | 850 | |
521d8ec3 GG |
851 | child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1, |
852 | features); | |
dad759ff DB |
853 | if (IS_ERR(child)) |
854 | return PTR_ERR(child); | |
855 | ||
521d8ec3 GG |
856 | child = add_regulator(TWL4030_REG_VDAC, pdata->vdac, |
857 | features); | |
dad759ff DB |
858 | if (IS_ERR(child)) |
859 | return PTR_ERR(child); | |
860 | ||
861 | child = add_regulator((features & TWL4030_VAUX2) | |
862 | ? TWL4030_REG_VAUX2_4030 | |
863 | : TWL4030_REG_VAUX2, | |
521d8ec3 | 864 | pdata->vaux2, features); |
dad759ff DB |
865 | if (IS_ERR(child)) |
866 | return PTR_ERR(child); | |
ab4abe05 | 867 | |
521d8ec3 GG |
868 | child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1, |
869 | features); | |
ab4abe05 JKS |
870 | if (IS_ERR(child)) |
871 | return PTR_ERR(child); | |
872 | ||
521d8ec3 GG |
873 | child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2, |
874 | features); | |
ab4abe05 JKS |
875 | if (IS_ERR(child)) |
876 | return PTR_ERR(child); | |
877 | ||
521d8ec3 GG |
878 | child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig, |
879 | features); | |
ab4abe05 JKS |
880 | if (IS_ERR(child)) |
881 | return PTR_ERR(child); | |
dad759ff DB |
882 | } |
883 | ||
dad759ff | 884 | /* maybe add LDOs that are omitted on cost-reduced parts */ |
f78959cf | 885 | if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && !(features & TPS_SUBSET) |
9da66539 | 886 | && twl_class_is_4030()) { |
521d8ec3 GG |
887 | child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2, |
888 | features); | |
dad759ff DB |
889 | if (IS_ERR(child)) |
890 | return PTR_ERR(child); | |
dad759ff | 891 | |
521d8ec3 GG |
892 | child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2, |
893 | features); | |
dad759ff DB |
894 | if (IS_ERR(child)) |
895 | return PTR_ERR(child); | |
896 | ||
521d8ec3 GG |
897 | child = add_regulator(TWL4030_REG_VSIM, pdata->vsim, |
898 | features); | |
dad759ff DB |
899 | if (IS_ERR(child)) |
900 | return PTR_ERR(child); | |
901 | ||
521d8ec3 GG |
902 | child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1, |
903 | features); | |
dad759ff DB |
904 | if (IS_ERR(child)) |
905 | return PTR_ERR(child); | |
906 | ||
521d8ec3 GG |
907 | child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3, |
908 | features); | |
dad759ff DB |
909 | if (IS_ERR(child)) |
910 | return PTR_ERR(child); | |
911 | ||
521d8ec3 GG |
912 | child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4, |
913 | features); | |
dad759ff DB |
914 | if (IS_ERR(child)) |
915 | return PTR_ERR(child); | |
a603a7fa DB |
916 | } |
917 | ||
9da66539 | 918 | /* twl6030 regulators */ |
f78959cf | 919 | if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() && |
521d8ec3 | 920 | !(features & TWL6025_SUBCLASS)) { |
34a38440 TK |
921 | child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1, |
922 | features); | |
923 | if (IS_ERR(child)) | |
924 | return PTR_ERR(child); | |
925 | ||
926 | child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2, | |
927 | features); | |
928 | if (IS_ERR(child)) | |
929 | return PTR_ERR(child); | |
930 | ||
931 | child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3, | |
932 | features); | |
933 | if (IS_ERR(child)) | |
934 | return PTR_ERR(child); | |
935 | ||
46eda3e9 PU |
936 | child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8, |
937 | features); | |
938 | if (IS_ERR(child)) | |
939 | return PTR_ERR(child); | |
940 | ||
941 | child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1, | |
942 | features); | |
943 | if (IS_ERR(child)) | |
944 | return PTR_ERR(child); | |
945 | ||
521d8ec3 GG |
946 | child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc, |
947 | features); | |
948 | if (IS_ERR(child)) | |
949 | return PTR_ERR(child); | |
950 | ||
951 | child = add_regulator(TWL6030_REG_VPP, pdata->vpp, | |
952 | features); | |
953 | if (IS_ERR(child)) | |
954 | return PTR_ERR(child); | |
955 | ||
956 | child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim, | |
957 | features); | |
958 | if (IS_ERR(child)) | |
959 | return PTR_ERR(child); | |
960 | ||
961 | child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio, | |
962 | features); | |
963 | if (IS_ERR(child)) | |
964 | return PTR_ERR(child); | |
965 | ||
966 | child = add_regulator(TWL6030_REG_VDAC, pdata->vdac, | |
967 | features); | |
968 | if (IS_ERR(child)) | |
969 | return PTR_ERR(child); | |
970 | ||
971 | child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1, | |
972 | features); | |
973 | if (IS_ERR(child)) | |
974 | return PTR_ERR(child); | |
975 | ||
976 | child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2, | |
977 | features); | |
978 | if (IS_ERR(child)) | |
979 | return PTR_ERR(child); | |
980 | ||
981 | child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3, | |
982 | features); | |
983 | if (IS_ERR(child)) | |
984 | return PTR_ERR(child); | |
985 | ||
986 | child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg, | |
987 | features); | |
988 | if (IS_ERR(child)) | |
989 | return PTR_ERR(child); | |
990 | } | |
991 | ||
992 | /* 6030 and 6025 share this regulator */ | |
f78959cf | 993 | if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030()) { |
521d8ec3 GG |
994 | child = add_regulator(TWL6030_REG_VANA, pdata->vana, |
995 | features); | |
996 | if (IS_ERR(child)) | |
997 | return PTR_ERR(child); | |
998 | } | |
999 | ||
1000 | /* twl6025 regulators */ | |
f78959cf | 1001 | if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() && |
521d8ec3 GG |
1002 | (features & TWL6025_SUBCLASS)) { |
1003 | child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5, | |
1004 | features); | |
9da66539 RN |
1005 | if (IS_ERR(child)) |
1006 | return PTR_ERR(child); | |
1007 | ||
521d8ec3 GG |
1008 | child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1, |
1009 | features); | |
9da66539 RN |
1010 | if (IS_ERR(child)) |
1011 | return PTR_ERR(child); | |
1012 | ||
521d8ec3 GG |
1013 | child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7, |
1014 | features); | |
9da66539 RN |
1015 | if (IS_ERR(child)) |
1016 | return PTR_ERR(child); | |
1017 | ||
521d8ec3 GG |
1018 | child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6, |
1019 | features); | |
9da66539 RN |
1020 | if (IS_ERR(child)) |
1021 | return PTR_ERR(child); | |
1022 | ||
521d8ec3 GG |
1023 | child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln, |
1024 | features); | |
9da66539 RN |
1025 | if (IS_ERR(child)) |
1026 | return PTR_ERR(child); | |
1027 | ||
521d8ec3 GG |
1028 | child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2, |
1029 | features); | |
9da66539 RN |
1030 | if (IS_ERR(child)) |
1031 | return PTR_ERR(child); | |
1032 | ||
521d8ec3 GG |
1033 | child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4, |
1034 | features); | |
9da66539 RN |
1035 | if (IS_ERR(child)) |
1036 | return PTR_ERR(child); | |
1037 | ||
521d8ec3 GG |
1038 | child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3, |
1039 | features); | |
9da66539 RN |
1040 | if (IS_ERR(child)) |
1041 | return PTR_ERR(child); | |
1042 | ||
521d8ec3 GG |
1043 | child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3, |
1044 | features); | |
9da66539 RN |
1045 | if (IS_ERR(child)) |
1046 | return PTR_ERR(child); | |
8e6de4a3 | 1047 | |
521d8ec3 GG |
1048 | child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4, |
1049 | features); | |
8e6de4a3 B |
1050 | if (IS_ERR(child)) |
1051 | return PTR_ERR(child); | |
521d8ec3 GG |
1052 | |
1053 | child = add_regulator(TWL6025_REG_VIO, pdata->vio6025, | |
1054 | features); | |
1055 | if (IS_ERR(child)) | |
1056 | return PTR_ERR(child); | |
1057 | ||
9da66539 RN |
1058 | } |
1059 | ||
f78959cf | 1060 | if (IS_ENABLED(CONFIG_CHARGER_TWL4030) && pdata->bci && |
11c39c4b GI |
1061 | !(features & (TPS_SUBSET | TWL5031))) { |
1062 | child = add_child(3, "twl4030_bci", | |
1063 | pdata->bci, sizeof(*pdata->bci), false, | |
1064 | /* irq0 = CHG_PRES, irq1 = BCI */ | |
9e178620 FB |
1065 | irq_base + BCI_PRES_INTR_OFFSET, |
1066 | irq_base + BCI_INTR_OFFSET); | |
11c39c4b GI |
1067 | if (IS_ERR(child)) |
1068 | return PTR_ERR(child); | |
1069 | } | |
1070 | ||
5725d66b | 1071 | return 0; |
a603a7fa DB |
1072 | } |
1073 | ||
1074 | /*----------------------------------------------------------------------*/ | |
1075 | ||
1076 | /* | |
1077 | * These three functions initialize the on-chip clock framework, | |
1078 | * letting it generate the right frequencies for USB, MADC, and | |
1079 | * other purposes. | |
1080 | */ | |
1081 | static inline int __init protect_pm_master(void) | |
1082 | { | |
1083 | int e = 0; | |
1084 | ||
49e6f87e FB |
1085 | e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0, |
1086 | TWL4030_PM_MASTER_PROTECT_KEY); | |
a603a7fa DB |
1087 | return e; |
1088 | } | |
1089 | ||
1090 | static inline int __init unprotect_pm_master(void) | |
1091 | { | |
1092 | int e = 0; | |
1093 | ||
49e6f87e FB |
1094 | e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, |
1095 | TWL4030_PM_MASTER_KEY_CFG1, | |
1096 | TWL4030_PM_MASTER_PROTECT_KEY); | |
1097 | e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, | |
1098 | TWL4030_PM_MASTER_KEY_CFG2, | |
1099 | TWL4030_PM_MASTER_PROTECT_KEY); | |
1100 | ||
a603a7fa DB |
1101 | return e; |
1102 | } | |
1103 | ||
38a68496 IK |
1104 | static void clocks_init(struct device *dev, |
1105 | struct twl4030_clock_init_data *clock) | |
a603a7fa DB |
1106 | { |
1107 | int e = 0; | |
1108 | struct clk *osc; | |
1109 | u32 rate; | |
1110 | u8 ctrl = HFCLK_FREQ_26_MHZ; | |
1111 | ||
defa6be1 | 1112 | osc = clk_get(dev, "fck"); |
a603a7fa | 1113 | if (IS_ERR(osc)) { |
fc7b92fc | 1114 | printk(KERN_WARNING "Skipping twl internal clock init and " |
a603a7fa DB |
1115 | "using bootloader value (unknown osc rate)\n"); |
1116 | return; | |
1117 | } | |
1118 | ||
1119 | rate = clk_get_rate(osc); | |
1120 | clk_put(osc); | |
1121 | ||
1122 | switch (rate) { | |
1123 | case 19200000: | |
1124 | ctrl = HFCLK_FREQ_19p2_MHZ; | |
1125 | break; | |
1126 | case 26000000: | |
1127 | ctrl = HFCLK_FREQ_26_MHZ; | |
1128 | break; | |
1129 | case 38400000: | |
1130 | ctrl = HFCLK_FREQ_38p4_MHZ; | |
1131 | break; | |
1132 | } | |
1133 | ||
1134 | ctrl |= HIGH_PERF_SQ; | |
38a68496 IK |
1135 | if (clock && clock->ck32k_lowpwr_enable) |
1136 | ctrl |= CK32K_LOWPWR_EN; | |
1137 | ||
a603a7fa DB |
1138 | e |= unprotect_pm_master(); |
1139 | /* effect->MADC+USB ck en */ | |
fc7b92fc | 1140 | e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT); |
a603a7fa DB |
1141 | e |= protect_pm_master(); |
1142 | ||
1143 | if (e < 0) | |
1144 | pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e); | |
1145 | } | |
1146 | ||
1147 | /*----------------------------------------------------------------------*/ | |
1148 | ||
a603a7fa | 1149 | |
fc7b92fc | 1150 | static int twl_remove(struct i2c_client *client) |
a603a7fa | 1151 | { |
364cedb2 | 1152 | unsigned i, num_slaves; |
a30d46c0 | 1153 | int status; |
a603a7fa | 1154 | |
364cedb2 | 1155 | if (twl_class_is_4030()) { |
e8deb28c | 1156 | status = twl4030_exit_irq(); |
364cedb2 PU |
1157 | num_slaves = TWL_NUM_SLAVES; |
1158 | } else { | |
e8deb28c | 1159 | status = twl6030_exit_irq(); |
364cedb2 PU |
1160 | num_slaves = TWL_NUM_SLAVES - 1; |
1161 | } | |
e8deb28c | 1162 | |
a30d46c0 DB |
1163 | if (status < 0) |
1164 | return status; | |
a603a7fa | 1165 | |
364cedb2 | 1166 | for (i = 0; i < num_slaves; i++) { |
fc7b92fc | 1167 | struct twl_client *twl = &twl_modules[i]; |
a603a7fa DB |
1168 | |
1169 | if (twl->client && twl->client != client) | |
1170 | i2c_unregister_device(twl->client); | |
fc7b92fc | 1171 | twl_modules[i].client = NULL; |
a603a7fa DB |
1172 | } |
1173 | inuse = false; | |
1174 | return 0; | |
1175 | } | |
1176 | ||
ec1a07b3 | 1177 | /* NOTE: This driver only handles a single twl4030/tps659x0 chip */ |
5b9cecd6 | 1178 | static int __devinit |
fc7b92fc | 1179 | twl_probe(struct i2c_client *client, const struct i2c_device_id *id) |
a603a7fa | 1180 | { |
a603a7fa | 1181 | struct twl4030_platform_data *pdata = client->dev.platform_data; |
aeb5032b | 1182 | struct device_node *node = client->dev.of_node; |
defa6be1 | 1183 | struct platform_device *pdev; |
2473d25a | 1184 | struct regmap_config *twl_regmap_config; |
ec1a07b3 BC |
1185 | int irq_base = 0; |
1186 | int status; | |
364cedb2 | 1187 | unsigned i, num_slaves; |
aeb5032b | 1188 | |
defa6be1 TL |
1189 | pdev = platform_device_alloc(DRIVER_NAME, -1); |
1190 | if (!pdev) { | |
1191 | dev_err(&client->dev, "can't alloc pdev\n"); | |
1192 | return -ENOMEM; | |
1193 | } | |
1194 | ||
1195 | status = platform_device_add(pdev); | |
1196 | if (status) { | |
1197 | platform_device_put(pdev); | |
1198 | return status; | |
1199 | } | |
1200 | ||
aeb5032b BC |
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); | |
defa6be1 TL |
1209 | if (!pdata) { |
1210 | status = -ENOMEM; | |
1211 | goto free; | |
1212 | } | |
aeb5032b | 1213 | } |
a603a7fa DB |
1214 | |
1215 | if (!pdata) { | |
1216 | dev_dbg(&client->dev, "no platform data?\n"); | |
defa6be1 TL |
1217 | status = -EINVAL; |
1218 | goto free; | |
a603a7fa DB |
1219 | } |
1220 | ||
defa6be1 TL |
1221 | platform_set_drvdata(pdev, pdata); |
1222 | ||
a603a7fa DB |
1223 | if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) { |
1224 | dev_dbg(&client->dev, "can't talk I2C?\n"); | |
defa6be1 TL |
1225 | status = -EIO; |
1226 | goto free; | |
a603a7fa DB |
1227 | } |
1228 | ||
a30d46c0 | 1229 | if (inuse) { |
a603a7fa | 1230 | dev_dbg(&client->dev, "driver is already in use\n"); |
defa6be1 TL |
1231 | status = -EBUSY; |
1232 | goto free; | |
a603a7fa DB |
1233 | } |
1234 | ||
364cedb2 PU |
1235 | if ((id->driver_data) & TWL6030_CLASS) { |
1236 | twl_id = TWL6030_CLASS_ID; | |
1237 | twl_map = &twl6030_map[0]; | |
2473d25a | 1238 | twl_regmap_config = twl6030_regmap_config; |
364cedb2 PU |
1239 | num_slaves = TWL_NUM_SLAVES - 1; |
1240 | } else { | |
1241 | twl_id = TWL4030_CLASS_ID; | |
1242 | twl_map = &twl4030_map[0]; | |
2473d25a | 1243 | twl_regmap_config = twl4030_regmap_config; |
364cedb2 PU |
1244 | num_slaves = TWL_NUM_SLAVES; |
1245 | } | |
1246 | ||
1247 | for (i = 0; i < num_slaves; i++) { | |
ec1a07b3 | 1248 | struct twl_client *twl = &twl_modules[i]; |
a603a7fa | 1249 | |
ec1a07b3 | 1250 | if (i == 0) { |
a603a7fa | 1251 | twl->client = client; |
ec1a07b3 | 1252 | } else { |
a603a7fa | 1253 | twl->client = i2c_new_dummy(client->adapter, |
2473d25a | 1254 | client->addr + i); |
a603a7fa | 1255 | if (!twl->client) { |
a8643430 | 1256 | dev_err(&client->dev, |
a603a7fa DB |
1257 | "can't attach client %d\n", i); |
1258 | status = -ENOMEM; | |
1259 | goto fail; | |
1260 | } | |
a603a7fa | 1261 | } |
2473d25a PU |
1262 | |
1263 | twl->regmap = devm_regmap_init_i2c(twl->client, | |
1264 | &twl_regmap_config[i]); | |
1265 | if (IS_ERR(twl->regmap)) { | |
1266 | status = PTR_ERR(twl->regmap); | |
1267 | dev_err(&client->dev, | |
1268 | "Failed to allocate regmap %d, err: %d\n", i, | |
1269 | status); | |
1270 | goto fail; | |
1271 | } | |
a603a7fa | 1272 | } |
ec1a07b3 | 1273 | |
a603a7fa DB |
1274 | inuse = true; |
1275 | ||
1276 | /* setup clock framework */ | |
defa6be1 | 1277 | clocks_init(&pdev->dev, pdata->clock); |
a603a7fa | 1278 | |
ca972d13 L |
1279 | /* read TWL IDCODE Register */ |
1280 | if (twl_id == TWL4030_CLASS_ID) { | |
ec1a07b3 BC |
1281 | status = twl_read_idcode_register(); |
1282 | WARN(status < 0, "Error: reading twl_idcode register value\n"); | |
ca972d13 L |
1283 | } |
1284 | ||
ebf0bd36 | 1285 | /* load power event scripts */ |
f78959cf | 1286 | if (IS_ENABLED(CONFIG_TWL4030_POWER) && pdata->power) |
ebf0bd36 AK |
1287 | twl4030_power_init(pdata->power); |
1288 | ||
a603a7fa | 1289 | /* Maybe init the T2 Interrupt subsystem */ |
9e178620 | 1290 | if (client->irq) { |
e8deb28c B |
1291 | if (twl_class_is_4030()) { |
1292 | twl4030_init_chip_irq(id->name); | |
78518ffa | 1293 | irq_base = twl4030_init_irq(&client->dev, client->irq); |
e8deb28c | 1294 | } else { |
78518ffa | 1295 | irq_base = twl6030_init_irq(&client->dev, client->irq); |
e8deb28c B |
1296 | } |
1297 | ||
78518ffa BC |
1298 | if (irq_base < 0) { |
1299 | status = irq_base; | |
a30d46c0 | 1300 | goto fail; |
78518ffa | 1301 | } |
a603a7fa DB |
1302 | } |
1303 | ||
ec1a07b3 BC |
1304 | /* |
1305 | * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface. | |
a29aaf55 MS |
1306 | * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0, |
1307 | * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0. | |
1308 | */ | |
a29aaf55 | 1309 | if (twl_class_is_4030()) { |
ec1a07b3 BC |
1310 | u8 temp; |
1311 | ||
a29aaf55 MS |
1312 | twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1); |
1313 | temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \ | |
ec1a07b3 | 1314 | I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU); |
a29aaf55 MS |
1315 | twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1); |
1316 | } | |
1317 | ||
964dba28 | 1318 | status = -ENODEV; |
aeb5032b BC |
1319 | if (node) |
1320 | status = of_platform_populate(node, NULL, NULL, &client->dev); | |
964dba28 | 1321 | if (status) |
9e178620 | 1322 | status = add_children(pdata, irq_base, id->driver_data); |
aeb5032b | 1323 | |
a603a7fa DB |
1324 | fail: |
1325 | if (status < 0) | |
fc7b92fc | 1326 | twl_remove(client); |
defa6be1 TL |
1327 | free: |
1328 | if (status < 0) | |
1329 | platform_device_unregister(pdev); | |
ec1a07b3 | 1330 | |
a603a7fa DB |
1331 | return status; |
1332 | } | |
1333 | ||
fc7b92fc | 1334 | static const struct i2c_device_id twl_ids[] = { |
dad759ff DB |
1335 | { "twl4030", TWL4030_VAUX2 }, /* "Triton 2" */ |
1336 | { "twl5030", 0 }, /* T2 updated */ | |
1920a61e | 1337 | { "twl5031", TWL5031 }, /* TWL5030 updated */ |
dad759ff DB |
1338 | { "tps65950", 0 }, /* catalog version of twl5030 */ |
1339 | { "tps65930", TPS_SUBSET }, /* fewer LDOs and DACs; no charger */ | |
1340 | { "tps65920", TPS_SUBSET }, /* fewer LDOs; no codec or charger */ | |
59dead5a OD |
1341 | { "tps65921", TPS_SUBSET }, /* fewer LDOs; no codec, no LED |
1342 | and vibrator. Charger in USB module*/ | |
e8deb28c | 1343 | { "twl6030", TWL6030_CLASS }, /* "Phoenix power chip" */ |
521d8ec3 | 1344 | { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */ |
a603a7fa DB |
1345 | { /* end of list */ }, |
1346 | }; | |
fc7b92fc | 1347 | MODULE_DEVICE_TABLE(i2c, twl_ids); |
a603a7fa DB |
1348 | |
1349 | /* One Client Driver , 4 Clients */ | |
fc7b92fc | 1350 | static struct i2c_driver twl_driver = { |
a603a7fa | 1351 | .driver.name = DRIVER_NAME, |
fc7b92fc B |
1352 | .id_table = twl_ids, |
1353 | .probe = twl_probe, | |
1354 | .remove = twl_remove, | |
a603a7fa DB |
1355 | }; |
1356 | ||
fc7b92fc | 1357 | static int __init twl_init(void) |
a603a7fa | 1358 | { |
fc7b92fc | 1359 | return i2c_add_driver(&twl_driver); |
a603a7fa | 1360 | } |
fc7b92fc | 1361 | subsys_initcall(twl_init); |
a603a7fa | 1362 | |
fc7b92fc | 1363 | static void __exit twl_exit(void) |
a603a7fa | 1364 | { |
fc7b92fc | 1365 | i2c_del_driver(&twl_driver); |
a603a7fa | 1366 | } |
fc7b92fc | 1367 | module_exit(twl_exit); |
a603a7fa DB |
1368 | |
1369 | MODULE_AUTHOR("Texas Instruments, Inc."); | |
fc7b92fc | 1370 | MODULE_DESCRIPTION("I2C Core interface for TWL"); |
a603a7fa | 1371 | MODULE_LICENSE("GPL"); |