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