blob: cb37bb8f8e41e76877a098f18941c06ee4d08864 [file] [log] [blame]
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -05001/*
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>
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030030#include <linux/err.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050031#include <linux/platform_device.h>
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +030032#include <linux/of.h>
33#include <linux/of_irq.h>
34#include <linux/of_gpio.h>
35#include <linux/of_platform.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050036#include <linux/gpio.h>
37#include <linux/delay.h>
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030038#include <linux/i2c.h>
39#include <linux/regmap.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050040#include <linux/mfd/core.h>
41#include <linux/mfd/twl6040.h>
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030042#include <linux/regulator/consumer.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050043
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030044#define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030045#define TWL6040_NUM_SUPPLIES (2)
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030046
Peter Ujfalusic7f91292013-11-29 16:03:45 +020047static 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
Wei Yongjunadc01fb2013-12-21 10:14:13 +080089static struct reg_default twl6040_patch[] = {
Peter Ujfalusi11e38e12014-04-01 16:44:59 +030090 /*
91 * Select I2C bus access to dual access registers
92 * Interrupt register is cleared on read
93 * Select fast mode for i2c (400KHz)
94 */
95 { TWL6040_REG_ACCCTL,
96 TWL6040_I2CSEL | TWL6040_INTCLRMODE | TWL6040_I2CMODE(1) },
Peter Ujfalusic7f91292013-11-29 16:03:45 +020097};
98
99
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200100static bool twl6040_has_vibra(struct device_node *node)
Samuel Ortizca2cad62012-05-23 16:23:21 +0200101{
Samuel Ortizca2cad62012-05-23 16:23:21 +0200102#ifdef CONFIG_OF
103 if (of_find_node_by_name(node, "vibra"))
104 return true;
105#endif
Samuel Ortizca2cad62012-05-23 16:23:21 +0200106 return false;
107}
108
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500109int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
110{
111 int ret;
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300112 unsigned int val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500113
Mark Brownc6f39252013-08-31 17:48:19 +0100114 ret = regmap_read(twl6040->regmap, reg, &val);
115 if (ret < 0)
116 return ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500117
118 return val;
119}
120EXPORT_SYMBOL(twl6040_reg_read);
121
122int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
123{
124 int ret;
125
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300126 ret = regmap_write(twl6040->regmap, reg, val);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500127
128 return ret;
129}
130EXPORT_SYMBOL(twl6040_reg_write);
131
132int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
133{
Axel Linc6000402012-07-11 10:06:34 +0800134 return regmap_update_bits(twl6040->regmap, reg, mask, mask);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500135}
136EXPORT_SYMBOL(twl6040_set_bits);
137
138int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
139{
Axel Linc6000402012-07-11 10:06:34 +0800140 return regmap_update_bits(twl6040->regmap, reg, mask, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500141}
142EXPORT_SYMBOL(twl6040_clear_bits);
143
144/* twl6040 codec manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200145static int twl6040_power_up_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500146{
147 u8 ldoctl, ncpctl, lppllctl;
148 int ret;
149
150 /* enable high-side LDO, reference system and internal oscillator */
151 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
152 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
153 if (ret)
154 return ret;
155 usleep_range(10000, 10500);
156
157 /* enable negative charge pump */
158 ncpctl = TWL6040_NCPENA;
159 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
160 if (ret)
161 goto ncp_err;
162 usleep_range(1000, 1500);
163
164 /* enable low-side LDO */
165 ldoctl |= TWL6040_LSLDOENA;
166 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
167 if (ret)
168 goto lsldo_err;
169 usleep_range(1000, 1500);
170
171 /* enable low-power PLL */
172 lppllctl = TWL6040_LPLLENA;
173 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
174 if (ret)
175 goto lppll_err;
176 usleep_range(5000, 5500);
177
178 /* disable internal oscillator */
179 ldoctl &= ~TWL6040_OSCENA;
180 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
181 if (ret)
182 goto osc_err;
183
184 return 0;
185
186osc_err:
187 lppllctl &= ~TWL6040_LPLLENA;
188 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
189lppll_err:
190 ldoctl &= ~TWL6040_LSLDOENA;
191 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
192lsldo_err:
193 ncpctl &= ~TWL6040_NCPENA;
194 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
195ncp_err:
196 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
197 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
198
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200199 dev_err(twl6040->dev, "manual power-up failed\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500200 return ret;
201}
202
203/* twl6040 manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200204static void twl6040_power_down_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500205{
206 u8 ncpctl, ldoctl, lppllctl;
207
208 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
209 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
210 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
211
212 /* enable internal oscillator */
213 ldoctl |= TWL6040_OSCENA;
214 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
215 usleep_range(1000, 1500);
216
217 /* disable low-power PLL */
218 lppllctl &= ~TWL6040_LPLLENA;
219 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
220
221 /* disable low-side LDO */
222 ldoctl &= ~TWL6040_LSLDOENA;
223 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
224
225 /* disable negative charge pump */
226 ncpctl &= ~TWL6040_NCPENA;
227 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
228
229 /* disable high-side LDO, reference system and internal oscillator */
230 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
231 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
232}
233
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200234static irqreturn_t twl6040_readyint_handler(int irq, void *data)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500235{
236 struct twl6040 *twl6040 = data;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500237
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200238 complete(&twl6040->ready);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500239
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200240 return IRQ_HANDLED;
241}
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500242
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200243static irqreturn_t twl6040_thint_handler(int irq, void *data)
244{
245 struct twl6040 *twl6040 = data;
246 u8 status;
247
248 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
249 if (status & TWL6040_TSHUTDET) {
250 dev_warn(twl6040->dev, "Thermal shutdown, powering-off");
251 twl6040_power(twl6040, 0);
252 } else {
253 dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on");
254 twl6040_power(twl6040, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500255 }
256
257 return IRQ_HANDLED;
258}
259
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200260static int twl6040_power_up_automatic(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500261{
262 int time_left;
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200263
264 gpio_set_value(twl6040->audpwron, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500265
266 time_left = wait_for_completion_timeout(&twl6040->ready,
267 msecs_to_jiffies(144));
268 if (!time_left) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200269 u8 intid;
270
271 dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500272 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
273 if (!(intid & TWL6040_READYINT)) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200274 dev_err(twl6040->dev, "automatic power-up failed\n");
275 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500276 return -ETIMEDOUT;
277 }
278 }
279
280 return 0;
281}
282
283int twl6040_power(struct twl6040 *twl6040, int on)
284{
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500285 int ret = 0;
286
287 mutex_lock(&twl6040->mutex);
288
289 if (on) {
290 /* already powered-up */
291 if (twl6040->power_count++)
292 goto out;
293
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200294 /* Allow writes to the chip */
295 regcache_cache_only(twl6040->regmap, false);
296
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200297 if (gpio_is_valid(twl6040->audpwron)) {
298 /* use automatic power-up sequence */
299 ret = twl6040_power_up_automatic(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500300 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500301 twl6040->power_count = 0;
302 goto out;
303 }
304 } else {
305 /* use manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200306 ret = twl6040_power_up_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500307 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500308 twl6040->power_count = 0;
309 goto out;
310 }
311 }
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200312
313 /* Sync with the HW */
314 regcache_sync(twl6040->regmap);
315
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300316 /* Default PLL configuration after power up */
317 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500318 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100319 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500320 } else {
321 /* already powered-down */
322 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300323 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500324 "device is already powered-off\n");
325 ret = -EPERM;
326 goto out;
327 }
328
329 if (--twl6040->power_count)
330 goto out;
331
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200332 if (gpio_is_valid(twl6040->audpwron)) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500333 /* use AUDPWRON line */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200334 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500335
336 /* power-down sequence latency */
337 usleep_range(500, 700);
338 } else {
339 /* use manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200340 twl6040_power_down_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500341 }
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200342
343 /* Set regmap to cache only and mark it as dirty */
344 regcache_cache_only(twl6040->regmap, true);
345 regcache_mark_dirty(twl6040->regmap);
346
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500347 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100348 twl6040->mclk = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500349 }
350
351out:
352 mutex_unlock(&twl6040->mutex);
353 return ret;
354}
355EXPORT_SYMBOL(twl6040_power);
356
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300357int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500358 unsigned int freq_in, unsigned int freq_out)
359{
360 u8 hppllctl, lppllctl;
361 int ret = 0;
362
363 mutex_lock(&twl6040->mutex);
364
365 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
366 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
367
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100368 /* Force full reconfiguration when switching between PLL */
369 if (pll_id != twl6040->pll) {
370 twl6040->sysclk = 0;
371 twl6040->mclk = 0;
372 }
373
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300374 switch (pll_id) {
375 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500376 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100377 /* Change the sysclk configuration only if it has been canged */
378 if (twl6040->sysclk != freq_out) {
379 switch (freq_out) {
380 case 17640000:
381 lppllctl |= TWL6040_LPLLFIN;
382 break;
383 case 19200000:
384 lppllctl &= ~TWL6040_LPLLFIN;
385 break;
386 default:
387 dev_err(twl6040->dev,
388 "freq_out %d not supported\n",
389 freq_out);
390 ret = -EINVAL;
391 goto pll_out;
392 }
393 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
394 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500395 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100396
397 /* The PLL in use has not been change, we can exit */
398 if (twl6040->pll == pll_id)
399 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500400
401 switch (freq_in) {
402 case 32768:
403 lppllctl |= TWL6040_LPLLENA;
404 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
405 lppllctl);
406 mdelay(5);
407 lppllctl &= ~TWL6040_HPLLSEL;
408 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
409 lppllctl);
410 hppllctl &= ~TWL6040_HPLLENA;
411 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
412 hppllctl);
413 break;
414 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300415 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500416 "freq_in %d not supported\n", freq_in);
417 ret = -EINVAL;
418 goto pll_out;
419 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500420 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300421 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500422 /* high-performance PLL can provide only 19.2 MHz */
423 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300424 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500425 "freq_out %d not supported\n", freq_out);
426 ret = -EINVAL;
427 goto pll_out;
428 }
429
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100430 if (twl6040->mclk != freq_in) {
431 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500432
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100433 switch (freq_in) {
434 case 12000000:
435 /* PLL enabled, active mode */
436 hppllctl |= TWL6040_MCLK_12000KHZ |
437 TWL6040_HPLLENA;
438 break;
439 case 19200000:
440 /*
441 * PLL disabled
442 * (enable PLL if MCLK jitter quality
443 * doesn't meet specification)
444 */
445 hppllctl |= TWL6040_MCLK_19200KHZ;
446 break;
447 case 26000000:
448 /* PLL enabled, active mode */
449 hppllctl |= TWL6040_MCLK_26000KHZ |
450 TWL6040_HPLLENA;
451 break;
452 case 38400000:
453 /* PLL enabled, active mode */
454 hppllctl |= TWL6040_MCLK_38400KHZ |
455 TWL6040_HPLLENA;
456 break;
457 default:
458 dev_err(twl6040->dev,
459 "freq_in %d not supported\n", freq_in);
460 ret = -EINVAL;
461 goto pll_out;
462 }
463
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500464 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100465 * enable clock slicer to ensure input waveform is
466 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500467 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100468 hppllctl |= TWL6040_HPLLSQRENA;
469
470 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
471 hppllctl);
472 usleep_range(500, 700);
473 lppllctl |= TWL6040_HPLLSEL;
474 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
475 lppllctl);
476 lppllctl &= ~TWL6040_LPLLENA;
477 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
478 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500479 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500480 break;
481 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300482 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500483 ret = -EINVAL;
484 goto pll_out;
485 }
486
487 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100488 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300489 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500490
491pll_out:
492 mutex_unlock(&twl6040->mutex);
493 return ret;
494}
495EXPORT_SYMBOL(twl6040_set_pll);
496
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300497int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500498{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300499 if (twl6040->power_count)
500 return twl6040->pll;
501 else
502 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500503}
504EXPORT_SYMBOL(twl6040_get_pll);
505
506unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
507{
508 return twl6040->sysclk;
509}
510EXPORT_SYMBOL(twl6040_get_sysclk);
511
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300512/* Get the combined status of the vibra control register */
513int twl6040_get_vibralr_status(struct twl6040 *twl6040)
514{
Mark Brownc6f39252013-08-31 17:48:19 +0100515 unsigned int reg;
516 int ret;
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300517 u8 status;
518
Mark Brownc6f39252013-08-31 17:48:19 +0100519 ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLL, &reg);
520 if (ret != 0)
521 return ret;
522 status = reg;
523
524 ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLR, &reg);
525 if (ret != 0)
526 return ret;
527 status |= reg;
528
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300529 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
530
531 return status;
532}
533EXPORT_SYMBOL(twl6040_get_vibralr_status);
534
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300535static struct resource twl6040_vibra_rsrc[] = {
536 {
537 .flags = IORESOURCE_IRQ,
538 },
539};
540
541static struct resource twl6040_codec_rsrc[] = {
542 {
543 .flags = IORESOURCE_IRQ,
544 },
545};
546
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300547static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500548{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300549 /* Register 0 is not readable */
550 if (!reg)
551 return false;
552 return true;
553}
554
Mark Brownc6f39252013-08-31 17:48:19 +0100555static bool twl6040_volatile_reg(struct device *dev, unsigned int reg)
556{
557 switch (reg) {
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200558 case TWL6040_REG_ASICID:
559 case TWL6040_REG_ASICREV:
560 case TWL6040_REG_INTID:
561 case TWL6040_REG_LPPLLCTL:
562 case TWL6040_REG_HPPLLCTL:
563 case TWL6040_REG_STATUS:
564 return true;
565 default:
566 return false;
567 }
568}
569
570static bool twl6040_writeable_reg(struct device *dev, unsigned int reg)
571{
572 switch (reg) {
573 case TWL6040_REG_ASICID:
574 case TWL6040_REG_ASICREV:
575 case TWL6040_REG_STATUS:
Mark Brownc6f39252013-08-31 17:48:19 +0100576 return false;
577 default:
578 return true;
579 }
580}
581
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300582static struct regmap_config twl6040_regmap_config = {
583 .reg_bits = 8,
584 .val_bits = 8,
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200585
586 .reg_defaults = twl6040_defaults,
587 .num_reg_defaults = ARRAY_SIZE(twl6040_defaults),
588
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300589 .max_register = TWL6040_REG_STATUS, /* 0x2e */
590
591 .readable_reg = twl6040_readable_reg,
Mark Brownc6f39252013-08-31 17:48:19 +0100592 .volatile_reg = twl6040_volatile_reg,
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200593 .writeable_reg = twl6040_writeable_reg,
Mark Brownc6f39252013-08-31 17:48:19 +0100594
595 .cache_type = REGCACHE_RBTREE,
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300596};
597
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200598static const struct regmap_irq twl6040_irqs[] = {
599 { .reg_offset = 0, .mask = TWL6040_THINT, },
600 { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
601 { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
602 { .reg_offset = 0, .mask = TWL6040_HFINT, },
603 { .reg_offset = 0, .mask = TWL6040_VIBINT, },
604 { .reg_offset = 0, .mask = TWL6040_READYINT, },
605};
606
607static struct regmap_irq_chip twl6040_irq_chip = {
608 .name = "twl6040",
609 .irqs = twl6040_irqs,
610 .num_irqs = ARRAY_SIZE(twl6040_irqs),
611
612 .num_regs = 1,
613 .status_base = TWL6040_REG_INTID,
614 .mask_base = TWL6040_REG_INTMR,
615};
616
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800617static int twl6040_probe(struct i2c_client *client,
618 const struct i2c_device_id *id)
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300619{
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300620 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500621 struct twl6040 *twl6040;
622 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300623 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500624
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200625 if (!node) {
626 dev_err(&client->dev, "of node is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500627 return -EINVAL;
628 }
629
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300630 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300631 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300632 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300633 return -EINVAL;
634 }
635
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300636 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
637 GFP_KERNEL);
Peter Ujfalusiecc8fa12013-07-12 13:32:04 +0200638 if (!twl6040)
639 return -ENOMEM;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500640
Axel Linbbf6adc2012-04-25 10:09:46 +0800641 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusiecc8fa12013-07-12 13:32:04 +0200642 if (IS_ERR(twl6040->regmap))
643 return PTR_ERR(twl6040->regmap);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500644
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300645 i2c_set_clientdata(client, twl6040);
646
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300647 twl6040->supplies[0].supply = "vio";
648 twl6040->supplies[1].supply = "v2v1";
Jingoo Han990810b2013-02-20 18:30:21 +0900649 ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200650 twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300651 if (ret != 0) {
652 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
Wolfram Sang501d6092013-10-13 18:06:12 +0200653 return ret;
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300654 }
655
656 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
657 if (ret != 0) {
658 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
Wolfram Sang501d6092013-10-13 18:06:12 +0200659 return ret;
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300660 }
661
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300662 twl6040->dev = &client->dev;
663 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500664
665 mutex_init(&twl6040->mutex);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500666 init_completion(&twl6040->ready);
667
668 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
Florian Vaussard89d68992014-02-25 14:28:19 +0100669 if (twl6040->rev < 0) {
670 dev_err(&client->dev, "Failed to read revision register: %d\n",
671 twl6040->rev);
672 goto gpio_err;
673 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500674
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300675 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200676 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
677 twl6040->audpwron = of_get_named_gpio(node,
678 "ti,audpwron-gpio", 0);
679 else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300680 twl6040->audpwron = -EINVAL;
681
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500682 if (gpio_is_valid(twl6040->audpwron)) {
Jingoo Han990810b2013-02-20 18:30:21 +0900683 ret = devm_gpio_request_one(&client->dev, twl6040->audpwron,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200684 GPIOF_OUT_INIT_LOW, "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500685 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300686 goto gpio_err;
Peter Ujfalusi02d02722014-04-01 16:45:01 +0300687
688 /* Clear any pending interrupt */
689 twl6040_reg_read(twl6040, TWL6040_REG_INTID);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500690 }
691
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200692 ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT,
693 0, &twl6040_irq_chip,&twl6040->irq_data);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200694 if (ret < 0)
Jingoo Han990810b2013-02-20 18:30:21 +0900695 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500696
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200697 twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200698 TWL6040_IRQ_READY);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200699 twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200700 TWL6040_IRQ_TH);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200701
Jingoo Han990810b2013-02-20 18:30:21 +0900702 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200703 twl6040_readyint_handler, IRQF_ONESHOT,
704 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300705 if (ret) {
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200706 dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
707 goto readyirq_err;
708 }
709
Jingoo Han990810b2013-02-20 18:30:21 +0900710 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200711 twl6040_thint_handler, IRQF_ONESHOT,
712 "twl6040_irq_th", twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200713 if (ret) {
714 dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
Wei Yongjunfc5ee962013-09-25 15:37:15 +0800715 goto readyirq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500716 }
717
718 /* dual-access registers controlled by I2C only */
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200719 regmap_register_patch(twl6040->regmap, twl6040_patch,
720 ARRAY_SIZE(twl6040_patch));
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500721
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300722 /*
723 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
724 * We can add the ASoC codec child whenever this driver has been loaded.
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300725 */
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200726 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300727 cell = &twl6040->cells[children];
728 cell->name = "twl6040-codec";
729 twl6040_codec_rsrc[0].start = irq;
730 twl6040_codec_rsrc[0].end = irq;
731 cell->resources = twl6040_codec_rsrc;
732 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300733 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500734
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200735 /* Vibra input driver support */
736 if (twl6040_has_vibra(node)) {
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200737 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300738
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500739 cell = &twl6040->cells[children];
740 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300741 twl6040_vibra_rsrc[0].start = irq;
742 twl6040_vibra_rsrc[0].end = irq;
743 cell->resources = twl6040_vibra_rsrc;
744 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500745 children++;
746 }
747
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200748 /* GPO support */
749 cell = &twl6040->cells[children];
750 cell->name = "twl6040-gpo";
751 children++;
Peter Ujfalusi5cbe7862012-08-16 15:13:14 +0300752
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200753 /* The chip is powered down so mark regmap to cache only and dirty */
754 regcache_cache_only(twl6040->regmap, true);
755 regcache_mark_dirty(twl6040->regmap);
756
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300757 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
Mark Brown55692af2012-09-11 15:16:36 +0800758 NULL, 0, NULL);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300759 if (ret)
Wei Yongjunfc5ee962013-09-25 15:37:15 +0800760 goto readyirq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500761
762 return 0;
763
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200764readyirq_err:
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200765 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300766gpio_err:
767 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500768 return ret;
769}
770
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800771static int twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500772{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300773 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500774
775 if (twl6040->power_count)
776 twl6040_power(twl6040, 0);
777
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200778 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500779
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300780 mfd_remove_devices(&client->dev);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500781
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300782 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300783
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500784 return 0;
785}
786
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300787static const struct i2c_device_id twl6040_i2c_id[] = {
788 { "twl6040", 0, },
Peter Ujfalusi1fc74ae2012-07-16 11:49:44 +0200789 { "twl6041", 0, },
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300790 { },
791};
792MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
793
794static struct i2c_driver twl6040_driver = {
795 .driver = {
796 .name = "twl6040",
797 .owner = THIS_MODULE,
798 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500799 .probe = twl6040_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800800 .remove = twl6040_remove,
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300801 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500802};
803
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300804module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500805
806MODULE_DESCRIPTION("TWL6040 MFD");
807MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
808MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
809MODULE_LICENSE("GPL");
810MODULE_ALIAS("platform:twl6040");