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