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