Commit | Line | Data |
---|---|---|
f19b2823 MLC |
1 | /* |
2 | * MFD driver for TWL6040 audio device | |
3 | * | |
4 | * Authors: Misael Lopez Cruz <misael.lopez@ti.com> | |
5 | * Jorge Eduardo Candelaria <jorge.candelaria@ti.com> | |
6 | * Peter Ujfalusi <peter.ujfalusi@ti.com> | |
7 | * | |
8 | * Copyright: (C) 2011 Texas Instruments, Inc. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, but | |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
22 | * 02110-1301 USA | |
23 | * | |
24 | */ | |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/kernel.h> | |
5af7df6b | 30 | #include <linux/err.h> |
f19b2823 | 31 | #include <linux/platform_device.h> |
37e13cec PU |
32 | #include <linux/of.h> |
33 | #include <linux/of_irq.h> | |
34 | #include <linux/of_gpio.h> | |
35 | #include <linux/of_platform.h> | |
f19b2823 MLC |
36 | #include <linux/gpio.h> |
37 | #include <linux/delay.h> | |
8eaeb939 PU |
38 | #include <linux/i2c.h> |
39 | #include <linux/regmap.h> | |
40 | #include <linux/err.h> | |
f19b2823 MLC |
41 | #include <linux/mfd/core.h> |
42 | #include <linux/mfd/twl6040.h> | |
5af7df6b | 43 | #include <linux/regulator/consumer.h> |
f19b2823 | 44 | |
31b402e3 | 45 | #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1) |
5af7df6b | 46 | #define TWL6040_NUM_SUPPLIES (2) |
31b402e3 | 47 | |
ca2cad6a SO |
48 | static bool twl6040_has_vibra(struct twl6040_platform_data *pdata, |
49 | struct device_node *node) | |
50 | { | |
51 | if (pdata && pdata->vibra) | |
52 | return true; | |
53 | ||
54 | #ifdef CONFIG_OF | |
55 | if (of_find_node_by_name(node, "vibra")) | |
56 | return true; | |
57 | #endif | |
58 | ||
59 | return false; | |
60 | } | |
61 | ||
f19b2823 MLC |
62 | int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg) |
63 | { | |
64 | int ret; | |
8eaeb939 | 65 | unsigned int val; |
f19b2823 MLC |
66 | |
67 | mutex_lock(&twl6040->io_mutex); | |
31b402e3 PU |
68 | /* Vibra control registers from cache */ |
69 | if (unlikely(reg == TWL6040_REG_VIBCTLL || | |
70 | reg == TWL6040_REG_VIBCTLR)) { | |
71 | val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)]; | |
72 | } else { | |
8eaeb939 | 73 | ret = regmap_read(twl6040->regmap, reg, &val); |
31b402e3 PU |
74 | if (ret < 0) { |
75 | mutex_unlock(&twl6040->io_mutex); | |
76 | return ret; | |
77 | } | |
f19b2823 MLC |
78 | } |
79 | mutex_unlock(&twl6040->io_mutex); | |
80 | ||
81 | return val; | |
82 | } | |
83 | EXPORT_SYMBOL(twl6040_reg_read); | |
84 | ||
85 | int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val) | |
86 | { | |
87 | int ret; | |
88 | ||
89 | mutex_lock(&twl6040->io_mutex); | |
8eaeb939 | 90 | ret = regmap_write(twl6040->regmap, reg, val); |
31b402e3 PU |
91 | /* Cache the vibra control registers */ |
92 | if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR) | |
93 | twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val; | |
f19b2823 MLC |
94 | mutex_unlock(&twl6040->io_mutex); |
95 | ||
96 | return ret; | |
97 | } | |
98 | EXPORT_SYMBOL(twl6040_reg_write); | |
99 | ||
100 | int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask) | |
101 | { | |
102 | int ret; | |
f19b2823 MLC |
103 | |
104 | mutex_lock(&twl6040->io_mutex); | |
8eaeb939 | 105 | ret = regmap_update_bits(twl6040->regmap, reg, mask, mask); |
f19b2823 MLC |
106 | mutex_unlock(&twl6040->io_mutex); |
107 | return ret; | |
108 | } | |
109 | EXPORT_SYMBOL(twl6040_set_bits); | |
110 | ||
111 | int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask) | |
112 | { | |
113 | int ret; | |
f19b2823 MLC |
114 | |
115 | mutex_lock(&twl6040->io_mutex); | |
8eaeb939 | 116 | ret = regmap_update_bits(twl6040->regmap, reg, mask, 0); |
f19b2823 MLC |
117 | mutex_unlock(&twl6040->io_mutex); |
118 | return ret; | |
119 | } | |
120 | EXPORT_SYMBOL(twl6040_clear_bits); | |
121 | ||
122 | /* twl6040 codec manual power-up sequence */ | |
123 | static int twl6040_power_up(struct twl6040 *twl6040) | |
124 | { | |
125 | u8 ldoctl, ncpctl, lppllctl; | |
126 | int ret; | |
127 | ||
128 | /* enable high-side LDO, reference system and internal oscillator */ | |
129 | ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA; | |
130 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | |
131 | if (ret) | |
132 | return ret; | |
133 | usleep_range(10000, 10500); | |
134 | ||
135 | /* enable negative charge pump */ | |
136 | ncpctl = TWL6040_NCPENA; | |
137 | ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); | |
138 | if (ret) | |
139 | goto ncp_err; | |
140 | usleep_range(1000, 1500); | |
141 | ||
142 | /* enable low-side LDO */ | |
143 | ldoctl |= TWL6040_LSLDOENA; | |
144 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | |
145 | if (ret) | |
146 | goto lsldo_err; | |
147 | usleep_range(1000, 1500); | |
148 | ||
149 | /* enable low-power PLL */ | |
150 | lppllctl = TWL6040_LPLLENA; | |
151 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); | |
152 | if (ret) | |
153 | goto lppll_err; | |
154 | usleep_range(5000, 5500); | |
155 | ||
156 | /* disable internal oscillator */ | |
157 | ldoctl &= ~TWL6040_OSCENA; | |
158 | ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | |
159 | if (ret) | |
160 | goto osc_err; | |
161 | ||
162 | return 0; | |
163 | ||
164 | osc_err: | |
165 | lppllctl &= ~TWL6040_LPLLENA; | |
166 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); | |
167 | lppll_err: | |
168 | ldoctl &= ~TWL6040_LSLDOENA; | |
169 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | |
170 | lsldo_err: | |
171 | ncpctl &= ~TWL6040_NCPENA; | |
172 | twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); | |
173 | ncp_err: | |
174 | ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA); | |
175 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | |
176 | ||
177 | return ret; | |
178 | } | |
179 | ||
180 | /* twl6040 manual power-down sequence */ | |
181 | static void twl6040_power_down(struct twl6040 *twl6040) | |
182 | { | |
183 | u8 ncpctl, ldoctl, lppllctl; | |
184 | ||
185 | ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL); | |
186 | ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL); | |
187 | lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL); | |
188 | ||
189 | /* enable internal oscillator */ | |
190 | ldoctl |= TWL6040_OSCENA; | |
191 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | |
192 | usleep_range(1000, 1500); | |
193 | ||
194 | /* disable low-power PLL */ | |
195 | lppllctl &= ~TWL6040_LPLLENA; | |
196 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl); | |
197 | ||
198 | /* disable low-side LDO */ | |
199 | ldoctl &= ~TWL6040_LSLDOENA; | |
200 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | |
201 | ||
202 | /* disable negative charge pump */ | |
203 | ncpctl &= ~TWL6040_NCPENA; | |
204 | twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl); | |
205 | ||
206 | /* disable high-side LDO, reference system and internal oscillator */ | |
207 | ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA); | |
208 | twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl); | |
209 | } | |
210 | ||
211 | static irqreturn_t twl6040_naudint_handler(int irq, void *data) | |
212 | { | |
213 | struct twl6040 *twl6040 = data; | |
214 | u8 intid, status; | |
215 | ||
216 | intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID); | |
217 | ||
218 | if (intid & TWL6040_READYINT) | |
219 | complete(&twl6040->ready); | |
220 | ||
221 | if (intid & TWL6040_THINT) { | |
222 | status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS); | |
223 | if (status & TWL6040_TSHUTDET) { | |
2d7c957e | 224 | dev_warn(twl6040->dev, |
f19b2823 MLC |
225 | "Thermal shutdown, powering-off"); |
226 | twl6040_power(twl6040, 0); | |
227 | } else { | |
2d7c957e | 228 | dev_warn(twl6040->dev, |
f19b2823 MLC |
229 | "Leaving thermal shutdown, powering-on"); |
230 | twl6040_power(twl6040, 1); | |
231 | } | |
232 | } | |
233 | ||
234 | return IRQ_HANDLED; | |
235 | } | |
236 | ||
237 | static int twl6040_power_up_completion(struct twl6040 *twl6040, | |
238 | int naudint) | |
239 | { | |
240 | int time_left; | |
241 | u8 intid; | |
242 | ||
243 | time_left = wait_for_completion_timeout(&twl6040->ready, | |
244 | msecs_to_jiffies(144)); | |
245 | if (!time_left) { | |
246 | intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID); | |
247 | if (!(intid & TWL6040_READYINT)) { | |
2d7c957e | 248 | dev_err(twl6040->dev, |
f19b2823 MLC |
249 | "timeout waiting for READYINT\n"); |
250 | return -ETIMEDOUT; | |
251 | } | |
252 | } | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
257 | int twl6040_power(struct twl6040 *twl6040, int on) | |
258 | { | |
259 | int audpwron = twl6040->audpwron; | |
260 | int naudint = twl6040->irq; | |
261 | int ret = 0; | |
262 | ||
263 | mutex_lock(&twl6040->mutex); | |
264 | ||
265 | if (on) { | |
266 | /* already powered-up */ | |
267 | if (twl6040->power_count++) | |
268 | goto out; | |
269 | ||
270 | if (gpio_is_valid(audpwron)) { | |
271 | /* use AUDPWRON line */ | |
272 | gpio_set_value(audpwron, 1); | |
273 | /* wait for power-up completion */ | |
274 | ret = twl6040_power_up_completion(twl6040, naudint); | |
275 | if (ret) { | |
2d7c957e | 276 | dev_err(twl6040->dev, |
f19b2823 MLC |
277 | "automatic power-down failed\n"); |
278 | twl6040->power_count = 0; | |
279 | goto out; | |
280 | } | |
281 | } else { | |
282 | /* use manual power-up sequence */ | |
283 | ret = twl6040_power_up(twl6040); | |
284 | if (ret) { | |
2d7c957e | 285 | dev_err(twl6040->dev, |
f19b2823 MLC |
286 | "manual power-up failed\n"); |
287 | twl6040->power_count = 0; | |
288 | goto out; | |
289 | } | |
290 | } | |
cfb7a33b PU |
291 | /* Default PLL configuration after power up */ |
292 | twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL; | |
f19b2823 | 293 | twl6040->sysclk = 19200000; |
f8447d6c | 294 | twl6040->mclk = 32768; |
f19b2823 MLC |
295 | } else { |
296 | /* already powered-down */ | |
297 | if (!twl6040->power_count) { | |
2d7c957e | 298 | dev_err(twl6040->dev, |
f19b2823 MLC |
299 | "device is already powered-off\n"); |
300 | ret = -EPERM; | |
301 | goto out; | |
302 | } | |
303 | ||
304 | if (--twl6040->power_count) | |
305 | goto out; | |
306 | ||
307 | if (gpio_is_valid(audpwron)) { | |
308 | /* use AUDPWRON line */ | |
309 | gpio_set_value(audpwron, 0); | |
310 | ||
311 | /* power-down sequence latency */ | |
312 | usleep_range(500, 700); | |
313 | } else { | |
314 | /* use manual power-down sequence */ | |
315 | twl6040_power_down(twl6040); | |
316 | } | |
f19b2823 | 317 | twl6040->sysclk = 0; |
f8447d6c | 318 | twl6040->mclk = 0; |
f19b2823 MLC |
319 | } |
320 | ||
321 | out: | |
322 | mutex_unlock(&twl6040->mutex); | |
323 | return ret; | |
324 | } | |
325 | EXPORT_SYMBOL(twl6040_power); | |
326 | ||
cfb7a33b | 327 | int twl6040_set_pll(struct twl6040 *twl6040, int pll_id, |
f19b2823 MLC |
328 | unsigned int freq_in, unsigned int freq_out) |
329 | { | |
330 | u8 hppllctl, lppllctl; | |
331 | int ret = 0; | |
332 | ||
333 | mutex_lock(&twl6040->mutex); | |
334 | ||
335 | hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL); | |
336 | lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL); | |
337 | ||
2bd05db7 PU |
338 | /* Force full reconfiguration when switching between PLL */ |
339 | if (pll_id != twl6040->pll) { | |
340 | twl6040->sysclk = 0; | |
341 | twl6040->mclk = 0; | |
342 | } | |
343 | ||
cfb7a33b PU |
344 | switch (pll_id) { |
345 | case TWL6040_SYSCLK_SEL_LPPLL: | |
f19b2823 | 346 | /* low-power PLL divider */ |
2bd05db7 PU |
347 | /* Change the sysclk configuration only if it has been canged */ |
348 | if (twl6040->sysclk != freq_out) { | |
349 | switch (freq_out) { | |
350 | case 17640000: | |
351 | lppllctl |= TWL6040_LPLLFIN; | |
352 | break; | |
353 | case 19200000: | |
354 | lppllctl &= ~TWL6040_LPLLFIN; | |
355 | break; | |
356 | default: | |
357 | dev_err(twl6040->dev, | |
358 | "freq_out %d not supported\n", | |
359 | freq_out); | |
360 | ret = -EINVAL; | |
361 | goto pll_out; | |
362 | } | |
363 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, | |
364 | lppllctl); | |
f19b2823 | 365 | } |
2bd05db7 PU |
366 | |
367 | /* The PLL in use has not been change, we can exit */ | |
368 | if (twl6040->pll == pll_id) | |
369 | break; | |
f19b2823 MLC |
370 | |
371 | switch (freq_in) { | |
372 | case 32768: | |
373 | lppllctl |= TWL6040_LPLLENA; | |
374 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, | |
375 | lppllctl); | |
376 | mdelay(5); | |
377 | lppllctl &= ~TWL6040_HPLLSEL; | |
378 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, | |
379 | lppllctl); | |
380 | hppllctl &= ~TWL6040_HPLLENA; | |
381 | twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, | |
382 | hppllctl); | |
383 | break; | |
384 | default: | |
2d7c957e | 385 | dev_err(twl6040->dev, |
f19b2823 MLC |
386 | "freq_in %d not supported\n", freq_in); |
387 | ret = -EINVAL; | |
388 | goto pll_out; | |
389 | } | |
f19b2823 | 390 | break; |
cfb7a33b | 391 | case TWL6040_SYSCLK_SEL_HPPLL: |
f19b2823 MLC |
392 | /* high-performance PLL can provide only 19.2 MHz */ |
393 | if (freq_out != 19200000) { | |
2d7c957e | 394 | dev_err(twl6040->dev, |
f19b2823 MLC |
395 | "freq_out %d not supported\n", freq_out); |
396 | ret = -EINVAL; | |
397 | goto pll_out; | |
398 | } | |
399 | ||
2bd05db7 PU |
400 | if (twl6040->mclk != freq_in) { |
401 | hppllctl &= ~TWL6040_MCLK_MSK; | |
402 | ||
403 | switch (freq_in) { | |
404 | case 12000000: | |
405 | /* PLL enabled, active mode */ | |
406 | hppllctl |= TWL6040_MCLK_12000KHZ | | |
407 | TWL6040_HPLLENA; | |
408 | break; | |
409 | case 19200000: | |
410 | /* | |
411 | * PLL disabled | |
412 | * (enable PLL if MCLK jitter quality | |
413 | * doesn't meet specification) | |
414 | */ | |
415 | hppllctl |= TWL6040_MCLK_19200KHZ; | |
416 | break; | |
417 | case 26000000: | |
418 | /* PLL enabled, active mode */ | |
419 | hppllctl |= TWL6040_MCLK_26000KHZ | | |
420 | TWL6040_HPLLENA; | |
421 | break; | |
422 | case 38400000: | |
423 | /* PLL enabled, active mode */ | |
424 | hppllctl |= TWL6040_MCLK_38400KHZ | | |
425 | TWL6040_HPLLENA; | |
426 | break; | |
427 | default: | |
428 | dev_err(twl6040->dev, | |
429 | "freq_in %d not supported\n", freq_in); | |
430 | ret = -EINVAL; | |
431 | goto pll_out; | |
432 | } | |
f19b2823 | 433 | |
f19b2823 | 434 | /* |
2bd05db7 PU |
435 | * enable clock slicer to ensure input waveform is |
436 | * square | |
f19b2823 | 437 | */ |
2bd05db7 | 438 | hppllctl |= TWL6040_HPLLSQRENA; |
f19b2823 | 439 | |
2bd05db7 PU |
440 | twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, |
441 | hppllctl); | |
442 | usleep_range(500, 700); | |
443 | lppllctl |= TWL6040_HPLLSEL; | |
444 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, | |
445 | lppllctl); | |
446 | lppllctl &= ~TWL6040_LPLLENA; | |
447 | twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, | |
448 | lppllctl); | |
449 | } | |
f19b2823 MLC |
450 | break; |
451 | default: | |
2d7c957e | 452 | dev_err(twl6040->dev, "unknown pll id %d\n", pll_id); |
f19b2823 MLC |
453 | ret = -EINVAL; |
454 | goto pll_out; | |
455 | } | |
456 | ||
457 | twl6040->sysclk = freq_out; | |
f8447d6c | 458 | twl6040->mclk = freq_in; |
cfb7a33b | 459 | twl6040->pll = pll_id; |
f19b2823 MLC |
460 | |
461 | pll_out: | |
462 | mutex_unlock(&twl6040->mutex); | |
463 | return ret; | |
464 | } | |
465 | EXPORT_SYMBOL(twl6040_set_pll); | |
466 | ||
cfb7a33b | 467 | int twl6040_get_pll(struct twl6040 *twl6040) |
f19b2823 | 468 | { |
cfb7a33b PU |
469 | if (twl6040->power_count) |
470 | return twl6040->pll; | |
471 | else | |
472 | return -ENODEV; | |
f19b2823 MLC |
473 | } |
474 | EXPORT_SYMBOL(twl6040_get_pll); | |
475 | ||
476 | unsigned int twl6040_get_sysclk(struct twl6040 *twl6040) | |
477 | { | |
478 | return twl6040->sysclk; | |
479 | } | |
480 | EXPORT_SYMBOL(twl6040_get_sysclk); | |
481 | ||
70601ec1 PU |
482 | /* Get the combined status of the vibra control register */ |
483 | int twl6040_get_vibralr_status(struct twl6040 *twl6040) | |
484 | { | |
485 | u8 status; | |
486 | ||
487 | status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1]; | |
488 | status &= (TWL6040_VIBENA | TWL6040_VIBSEL); | |
489 | ||
490 | return status; | |
491 | } | |
492 | EXPORT_SYMBOL(twl6040_get_vibralr_status); | |
493 | ||
0f962ae2 PU |
494 | static struct resource twl6040_vibra_rsrc[] = { |
495 | { | |
496 | .flags = IORESOURCE_IRQ, | |
497 | }, | |
498 | }; | |
499 | ||
500 | static struct resource twl6040_codec_rsrc[] = { | |
501 | { | |
502 | .flags = IORESOURCE_IRQ, | |
503 | }, | |
504 | }; | |
505 | ||
8eaeb939 | 506 | static bool twl6040_readable_reg(struct device *dev, unsigned int reg) |
f19b2823 | 507 | { |
8eaeb939 PU |
508 | /* Register 0 is not readable */ |
509 | if (!reg) | |
510 | return false; | |
511 | return true; | |
512 | } | |
513 | ||
514 | static struct regmap_config twl6040_regmap_config = { | |
515 | .reg_bits = 8, | |
516 | .val_bits = 8, | |
517 | .max_register = TWL6040_REG_STATUS, /* 0x2e */ | |
518 | ||
519 | .readable_reg = twl6040_readable_reg, | |
520 | }; | |
521 | ||
522 | static int __devinit twl6040_probe(struct i2c_client *client, | |
523 | const struct i2c_device_id *id) | |
524 | { | |
525 | struct twl6040_platform_data *pdata = client->dev.platform_data; | |
37e13cec | 526 | struct device_node *node = client->dev.of_node; |
f19b2823 MLC |
527 | struct twl6040 *twl6040; |
528 | struct mfd_cell *cell = NULL; | |
1f01d60e | 529 | int irq, ret, children = 0; |
f19b2823 | 530 | |
37e13cec | 531 | if (!pdata && !node) { |
8eaeb939 | 532 | dev_err(&client->dev, "Platform data is missing\n"); |
f19b2823 MLC |
533 | return -EINVAL; |
534 | } | |
535 | ||
d20e1d21 | 536 | /* In order to operate correctly we need valid interrupt config */ |
6712419d | 537 | if (!client->irq) { |
8eaeb939 | 538 | dev_err(&client->dev, "Invalid IRQ configuration\n"); |
d20e1d21 PU |
539 | return -EINVAL; |
540 | } | |
541 | ||
8eaeb939 PU |
542 | twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040), |
543 | GFP_KERNEL); | |
544 | if (!twl6040) { | |
545 | ret = -ENOMEM; | |
546 | goto err; | |
547 | } | |
548 | ||
bbf6adc1 | 549 | twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config); |
8eaeb939 PU |
550 | if (IS_ERR(twl6040->regmap)) { |
551 | ret = PTR_ERR(twl6040->regmap); | |
552 | goto err; | |
553 | } | |
f19b2823 | 554 | |
8eaeb939 | 555 | i2c_set_clientdata(client, twl6040); |
f19b2823 | 556 | |
5af7df6b PU |
557 | twl6040->supplies[0].supply = "vio"; |
558 | twl6040->supplies[1].supply = "v2v1"; | |
559 | ret = regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES, | |
560 | twl6040->supplies); | |
561 | if (ret != 0) { | |
562 | dev_err(&client->dev, "Failed to get supplies: %d\n", ret); | |
563 | goto regulator_get_err; | |
564 | } | |
565 | ||
566 | ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies); | |
567 | if (ret != 0) { | |
568 | dev_err(&client->dev, "Failed to enable supplies: %d\n", ret); | |
569 | goto power_err; | |
570 | } | |
571 | ||
8eaeb939 PU |
572 | twl6040->dev = &client->dev; |
573 | twl6040->irq = client->irq; | |
f19b2823 MLC |
574 | |
575 | mutex_init(&twl6040->mutex); | |
576 | mutex_init(&twl6040->io_mutex); | |
577 | init_completion(&twl6040->ready); | |
578 | ||
579 | twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV); | |
580 | ||
77f63e06 | 581 | /* ERRATA: Automatic power-up is not possible in ES1.0 */ |
37e13cec PU |
582 | if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) { |
583 | if (pdata) | |
584 | twl6040->audpwron = pdata->audpwron_gpio; | |
585 | else | |
586 | twl6040->audpwron = of_get_named_gpio(node, | |
587 | "ti,audpwron-gpio", 0); | |
588 | } else | |
77f63e06 PU |
589 | twl6040->audpwron = -EINVAL; |
590 | ||
f19b2823 | 591 | if (gpio_is_valid(twl6040->audpwron)) { |
b04edb93 AL |
592 | ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW, |
593 | "audpwron"); | |
f19b2823 | 594 | if (ret) |
5af7df6b | 595 | goto gpio_err; |
f19b2823 MLC |
596 | } |
597 | ||
d20e1d21 PU |
598 | /* codec interrupt */ |
599 | ret = twl6040_irq_init(twl6040); | |
600 | if (ret) | |
5af7df6b | 601 | goto irq_init_err; |
d20e1d21 | 602 | |
1b7c4725 PU |
603 | ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY, |
604 | NULL, twl6040_naudint_handler, 0, | |
605 | "twl6040_irq_ready", twl6040); | |
d20e1d21 PU |
606 | if (ret) { |
607 | dev_err(twl6040->dev, "READY IRQ request failed: %d\n", | |
608 | ret); | |
609 | goto irq_err; | |
f19b2823 MLC |
610 | } |
611 | ||
612 | /* dual-access registers controlled by I2C only */ | |
613 | twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL); | |
614 | ||
1f01d60e PU |
615 | /* |
616 | * The main functionality of twl6040 to provide audio on OMAP4+ systems. | |
617 | * We can add the ASoC codec child whenever this driver has been loaded. | |
618 | * The ASoC codec can work without pdata, pass the platform_data only if | |
619 | * it has been provided. | |
620 | */ | |
621 | irq = twl6040->irq_base + TWL6040_IRQ_PLUG; | |
622 | cell = &twl6040->cells[children]; | |
623 | cell->name = "twl6040-codec"; | |
624 | twl6040_codec_rsrc[0].start = irq; | |
625 | twl6040_codec_rsrc[0].end = irq; | |
626 | cell->resources = twl6040_codec_rsrc; | |
627 | cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc); | |
37e13cec | 628 | if (pdata && pdata->codec) { |
6c448637 PU |
629 | cell->platform_data = pdata->codec; |
630 | cell->pdata_size = sizeof(*pdata->codec); | |
f19b2823 | 631 | } |
1f01d60e | 632 | children++; |
f19b2823 | 633 | |
ca2cad6a | 634 | if (twl6040_has_vibra(pdata, node)) { |
1f01d60e | 635 | irq = twl6040->irq_base + TWL6040_IRQ_VIB; |
0f962ae2 | 636 | |
f19b2823 MLC |
637 | cell = &twl6040->cells[children]; |
638 | cell->name = "twl6040-vibra"; | |
0f962ae2 PU |
639 | twl6040_vibra_rsrc[0].start = irq; |
640 | twl6040_vibra_rsrc[0].end = irq; | |
641 | cell->resources = twl6040_vibra_rsrc; | |
642 | cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc); | |
643 | ||
37e13cec PU |
644 | if (pdata && pdata->vibra) { |
645 | cell->platform_data = pdata->vibra; | |
646 | cell->pdata_size = sizeof(*pdata->vibra); | |
647 | } | |
f19b2823 MLC |
648 | children++; |
649 | } | |
650 | ||
1f01d60e PU |
651 | ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children, |
652 | NULL, 0); | |
653 | if (ret) | |
f19b2823 | 654 | goto mfd_err; |
f19b2823 MLC |
655 | |
656 | return 0; | |
657 | ||
658 | mfd_err: | |
1b7c4725 | 659 | free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040); |
f19b2823 | 660 | irq_err: |
d20e1d21 | 661 | twl6040_irq_exit(twl6040); |
5af7df6b | 662 | irq_init_err: |
f19b2823 MLC |
663 | if (gpio_is_valid(twl6040->audpwron)) |
664 | gpio_free(twl6040->audpwron); | |
5af7df6b PU |
665 | gpio_err: |
666 | regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies); | |
667 | power_err: | |
668 | regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies); | |
669 | regulator_get_err: | |
8eaeb939 | 670 | i2c_set_clientdata(client, NULL); |
8eaeb939 | 671 | err: |
f19b2823 MLC |
672 | return ret; |
673 | } | |
674 | ||
8eaeb939 | 675 | static int __devexit twl6040_remove(struct i2c_client *client) |
f19b2823 | 676 | { |
8eaeb939 | 677 | struct twl6040 *twl6040 = i2c_get_clientdata(client); |
f19b2823 MLC |
678 | |
679 | if (twl6040->power_count) | |
680 | twl6040_power(twl6040, 0); | |
681 | ||
682 | if (gpio_is_valid(twl6040->audpwron)) | |
683 | gpio_free(twl6040->audpwron); | |
684 | ||
1b7c4725 | 685 | free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040); |
d20e1d21 | 686 | twl6040_irq_exit(twl6040); |
f19b2823 | 687 | |
8eaeb939 PU |
688 | mfd_remove_devices(&client->dev); |
689 | i2c_set_clientdata(client, NULL); | |
f19b2823 | 690 | |
5af7df6b PU |
691 | regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies); |
692 | regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies); | |
693 | ||
f19b2823 MLC |
694 | return 0; |
695 | } | |
696 | ||
8eaeb939 PU |
697 | static const struct i2c_device_id twl6040_i2c_id[] = { |
698 | { "twl6040", 0, }, | |
699 | { }, | |
700 | }; | |
701 | MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id); | |
702 | ||
703 | static struct i2c_driver twl6040_driver = { | |
704 | .driver = { | |
705 | .name = "twl6040", | |
706 | .owner = THIS_MODULE, | |
707 | }, | |
f19b2823 MLC |
708 | .probe = twl6040_probe, |
709 | .remove = __devexit_p(twl6040_remove), | |
8eaeb939 | 710 | .id_table = twl6040_i2c_id, |
f19b2823 MLC |
711 | }; |
712 | ||
8eaeb939 | 713 | module_i2c_driver(twl6040_driver); |
f19b2823 MLC |
714 | |
715 | MODULE_DESCRIPTION("TWL6040 MFD"); | |
716 | MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>"); | |
717 | MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>"); | |
718 | MODULE_LICENSE("GPL"); | |
719 | MODULE_ALIAS("platform:twl6040"); |