blob: 85cc32a2e0e052984e0eeb7c59fca558f3d91e0e [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
Krzysztof Kozlowskide1e23f2015-01-05 10:01:33 +010047static const struct reg_default twl6040_defaults[] = {
Peter Ujfalusic7f91292013-11-29 16:03:45 +020048 { 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
Nariman Poushin8019ff62015-07-16 16:36:21 +010089static struct reg_sequence 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
Javier Martinez Canillas32852bc2016-04-20 11:16:38 -0400294 ret = clk_prepare_enable(twl6040->clk32k);
295 if (ret) {
296 twl6040->power_count = 0;
297 goto out;
298 }
Peter Ujfalusi68bab862014-04-03 13:54:41 +0300299
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200300 /* Allow writes to the chip */
301 regcache_cache_only(twl6040->regmap, false);
302
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200303 if (gpio_is_valid(twl6040->audpwron)) {
304 /* use automatic power-up sequence */
305 ret = twl6040_power_up_automatic(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500306 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500307 twl6040->power_count = 0;
308 goto out;
309 }
310 } else {
311 /* use manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200312 ret = twl6040_power_up_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500313 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500314 twl6040->power_count = 0;
315 goto out;
316 }
317 }
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200318
319 /* Sync with the HW */
320 regcache_sync(twl6040->regmap);
321
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300322 /* Default PLL configuration after power up */
323 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500324 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100325 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500326 } else {
327 /* already powered-down */
328 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300329 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500330 "device is already powered-off\n");
331 ret = -EPERM;
332 goto out;
333 }
334
335 if (--twl6040->power_count)
336 goto out;
337
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200338 if (gpio_is_valid(twl6040->audpwron)) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500339 /* use AUDPWRON line */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200340 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500341
342 /* power-down sequence latency */
343 usleep_range(500, 700);
344 } else {
345 /* use manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200346 twl6040_power_down_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500347 }
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200348
349 /* Set regmap to cache only and mark it as dirty */
350 regcache_cache_only(twl6040->regmap, true);
351 regcache_mark_dirty(twl6040->regmap);
352
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500353 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100354 twl6040->mclk = 0;
Peter Ujfalusi68bab862014-04-03 13:54:41 +0300355
356 clk_disable_unprepare(twl6040->clk32k);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500357 }
358
359out:
360 mutex_unlock(&twl6040->mutex);
361 return ret;
362}
363EXPORT_SYMBOL(twl6040_power);
364
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300365int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500366 unsigned int freq_in, unsigned int freq_out)
367{
368 u8 hppllctl, lppllctl;
369 int ret = 0;
370
371 mutex_lock(&twl6040->mutex);
372
373 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
374 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
375
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100376 /* Force full reconfiguration when switching between PLL */
377 if (pll_id != twl6040->pll) {
378 twl6040->sysclk = 0;
379 twl6040->mclk = 0;
380 }
381
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300382 switch (pll_id) {
383 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500384 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100385 /* Change the sysclk configuration only if it has been canged */
386 if (twl6040->sysclk != freq_out) {
387 switch (freq_out) {
388 case 17640000:
389 lppllctl |= TWL6040_LPLLFIN;
390 break;
391 case 19200000:
392 lppllctl &= ~TWL6040_LPLLFIN;
393 break;
394 default:
395 dev_err(twl6040->dev,
396 "freq_out %d not supported\n",
397 freq_out);
398 ret = -EINVAL;
399 goto pll_out;
400 }
401 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
402 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500403 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100404
405 /* The PLL in use has not been change, we can exit */
406 if (twl6040->pll == pll_id)
407 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500408
409 switch (freq_in) {
410 case 32768:
411 lppllctl |= TWL6040_LPLLENA;
412 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
413 lppllctl);
414 mdelay(5);
415 lppllctl &= ~TWL6040_HPLLSEL;
416 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
417 lppllctl);
418 hppllctl &= ~TWL6040_HPLLENA;
419 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
420 hppllctl);
421 break;
422 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300423 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500424 "freq_in %d not supported\n", freq_in);
425 ret = -EINVAL;
426 goto pll_out;
427 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500428 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300429 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500430 /* high-performance PLL can provide only 19.2 MHz */
431 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300432 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500433 "freq_out %d not supported\n", freq_out);
434 ret = -EINVAL;
435 goto pll_out;
436 }
437
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100438 if (twl6040->mclk != freq_in) {
439 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500440
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100441 switch (freq_in) {
442 case 12000000:
443 /* PLL enabled, active mode */
444 hppllctl |= TWL6040_MCLK_12000KHZ |
445 TWL6040_HPLLENA;
446 break;
447 case 19200000:
Peter Ujfalusiac8320c2014-05-06 11:46:10 +0300448 /* PLL enabled, bypass mode */
449 hppllctl |= TWL6040_MCLK_19200KHZ |
450 TWL6040_HPLLBP | TWL6040_HPLLENA;
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100451 break;
452 case 26000000:
453 /* PLL enabled, active mode */
454 hppllctl |= TWL6040_MCLK_26000KHZ |
455 TWL6040_HPLLENA;
456 break;
457 case 38400000:
Peter Ujfalusiac8320c2014-05-06 11:46:10 +0300458 /* PLL enabled, bypass mode */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100459 hppllctl |= TWL6040_MCLK_38400KHZ |
Peter Ujfalusiac8320c2014-05-06 11:46:10 +0300460 TWL6040_HPLLBP | TWL6040_HPLLENA;
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100461 break;
462 default:
463 dev_err(twl6040->dev,
464 "freq_in %d not supported\n", freq_in);
465 ret = -EINVAL;
466 goto pll_out;
467 }
468
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500469 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100470 * enable clock slicer to ensure input waveform is
471 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500472 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100473 hppllctl |= TWL6040_HPLLSQRENA;
474
475 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
476 hppllctl);
477 usleep_range(500, 700);
478 lppllctl |= TWL6040_HPLLSEL;
479 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
480 lppllctl);
481 lppllctl &= ~TWL6040_LPLLENA;
482 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
483 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500484 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500485 break;
486 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300487 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500488 ret = -EINVAL;
489 goto pll_out;
490 }
491
492 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100493 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300494 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500495
496pll_out:
497 mutex_unlock(&twl6040->mutex);
498 return ret;
499}
500EXPORT_SYMBOL(twl6040_set_pll);
501
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300502int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500503{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300504 if (twl6040->power_count)
505 return twl6040->pll;
506 else
507 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500508}
509EXPORT_SYMBOL(twl6040_get_pll);
510
511unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
512{
513 return twl6040->sysclk;
514}
515EXPORT_SYMBOL(twl6040_get_sysclk);
516
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300517/* Get the combined status of the vibra control register */
518int twl6040_get_vibralr_status(struct twl6040 *twl6040)
519{
Mark Brownc6f39252013-08-31 17:48:19 +0100520 unsigned int reg;
521 int ret;
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300522 u8 status;
523
Mark Brownc6f39252013-08-31 17:48:19 +0100524 ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLL, &reg);
525 if (ret != 0)
526 return ret;
527 status = reg;
528
529 ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLR, &reg);
530 if (ret != 0)
531 return ret;
532 status |= reg;
533
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300534 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
535
536 return status;
537}
538EXPORT_SYMBOL(twl6040_get_vibralr_status);
539
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300540static struct resource twl6040_vibra_rsrc[] = {
541 {
542 .flags = IORESOURCE_IRQ,
543 },
544};
545
546static struct resource twl6040_codec_rsrc[] = {
547 {
548 .flags = IORESOURCE_IRQ,
549 },
550};
551
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300552static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500553{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300554 /* Register 0 is not readable */
555 if (!reg)
556 return false;
557 return true;
558}
559
Mark Brownc6f39252013-08-31 17:48:19 +0100560static bool twl6040_volatile_reg(struct device *dev, unsigned int reg)
561{
562 switch (reg) {
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200563 case TWL6040_REG_ASICID:
564 case TWL6040_REG_ASICREV:
565 case TWL6040_REG_INTID:
566 case TWL6040_REG_LPPLLCTL:
567 case TWL6040_REG_HPPLLCTL:
568 case TWL6040_REG_STATUS:
569 return true;
570 default:
571 return false;
572 }
573}
574
575static bool twl6040_writeable_reg(struct device *dev, unsigned int reg)
576{
577 switch (reg) {
578 case TWL6040_REG_ASICID:
579 case TWL6040_REG_ASICREV:
580 case TWL6040_REG_STATUS:
Mark Brownc6f39252013-08-31 17:48:19 +0100581 return false;
582 default:
583 return true;
584 }
585}
586
Krzysztof Kozlowskide1e23f2015-01-05 10:01:33 +0100587static const struct regmap_config twl6040_regmap_config = {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300588 .reg_bits = 8,
589 .val_bits = 8,
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200590
591 .reg_defaults = twl6040_defaults,
592 .num_reg_defaults = ARRAY_SIZE(twl6040_defaults),
593
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300594 .max_register = TWL6040_REG_STATUS, /* 0x2e */
595
596 .readable_reg = twl6040_readable_reg,
Mark Brownc6f39252013-08-31 17:48:19 +0100597 .volatile_reg = twl6040_volatile_reg,
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200598 .writeable_reg = twl6040_writeable_reg,
Mark Brownc6f39252013-08-31 17:48:19 +0100599
600 .cache_type = REGCACHE_RBTREE,
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300601};
602
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200603static const struct regmap_irq twl6040_irqs[] = {
604 { .reg_offset = 0, .mask = TWL6040_THINT, },
605 { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
606 { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
607 { .reg_offset = 0, .mask = TWL6040_HFINT, },
608 { .reg_offset = 0, .mask = TWL6040_VIBINT, },
609 { .reg_offset = 0, .mask = TWL6040_READYINT, },
610};
611
612static struct regmap_irq_chip twl6040_irq_chip = {
613 .name = "twl6040",
614 .irqs = twl6040_irqs,
615 .num_irqs = ARRAY_SIZE(twl6040_irqs),
616
617 .num_regs = 1,
618 .status_base = TWL6040_REG_INTID,
619 .mask_base = TWL6040_REG_INTMR,
620};
621
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800622static int twl6040_probe(struct i2c_client *client,
623 const struct i2c_device_id *id)
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300624{
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300625 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500626 struct twl6040 *twl6040;
627 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300628 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500629
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200630 if (!node) {
631 dev_err(&client->dev, "of node is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500632 return -EINVAL;
633 }
634
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300635 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300636 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300637 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300638 return -EINVAL;
639 }
640
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300641 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
642 GFP_KERNEL);
Peter Ujfalusiecc8fa12013-07-12 13:32:04 +0200643 if (!twl6040)
644 return -ENOMEM;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500645
Axel Linbbf6adc2012-04-25 10:09:46 +0800646 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusiecc8fa12013-07-12 13:32:04 +0200647 if (IS_ERR(twl6040->regmap))
648 return PTR_ERR(twl6040->regmap);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500649
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300650 i2c_set_clientdata(client, twl6040);
651
Peter Ujfalusi68bab862014-04-03 13:54:41 +0300652 twl6040->clk32k = devm_clk_get(&client->dev, "clk32k");
653 if (IS_ERR(twl6040->clk32k)) {
Tony Lindgren75c08f12015-09-18 09:29:04 -0700654 if (PTR_ERR(twl6040->clk32k) == -EPROBE_DEFER)
655 return -EPROBE_DEFER;
Peter Ujfalusi68bab862014-04-03 13:54:41 +0300656 dev_info(&client->dev, "clk32k is not handled\n");
657 twl6040->clk32k = NULL;
658 }
659
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300660 twl6040->supplies[0].supply = "vio";
661 twl6040->supplies[1].supply = "v2v1";
Jingoo Han990810b2013-02-20 18:30:21 +0900662 ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200663 twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300664 if (ret != 0) {
665 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
Wolfram Sang501d6092013-10-13 18:06:12 +0200666 return ret;
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300667 }
668
669 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
670 if (ret != 0) {
671 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
Wolfram Sang501d6092013-10-13 18:06:12 +0200672 return ret;
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300673 }
674
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300675 twl6040->dev = &client->dev;
676 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500677
678 mutex_init(&twl6040->mutex);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500679 init_completion(&twl6040->ready);
680
Peter Ujfalusi006cea32014-04-01 16:45:00 +0300681 regmap_register_patch(twl6040->regmap, twl6040_patch,
682 ARRAY_SIZE(twl6040_patch));
683
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500684 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
Florian Vaussard89d68992014-02-25 14:28:19 +0100685 if (twl6040->rev < 0) {
686 dev_err(&client->dev, "Failed to read revision register: %d\n",
687 twl6040->rev);
Julia Lawallf2b86782014-08-06 22:12:15 +0200688 ret = twl6040->rev;
Florian Vaussard89d68992014-02-25 14:28:19 +0100689 goto gpio_err;
690 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500691
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300692 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200693 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
694 twl6040->audpwron = of_get_named_gpio(node,
695 "ti,audpwron-gpio", 0);
696 else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300697 twl6040->audpwron = -EINVAL;
698
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500699 if (gpio_is_valid(twl6040->audpwron)) {
Jingoo Han990810b2013-02-20 18:30:21 +0900700 ret = devm_gpio_request_one(&client->dev, twl6040->audpwron,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200701 GPIOF_OUT_INIT_LOW, "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500702 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300703 goto gpio_err;
Peter Ujfalusi02d02722014-04-01 16:45:01 +0300704
705 /* Clear any pending interrupt */
706 twl6040_reg_read(twl6040, TWL6040_REG_INTID);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500707 }
708
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200709 ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT,
Lee Jonesc06f3082014-07-21 13:13:48 +0100710 0, &twl6040_irq_chip, &twl6040->irq_data);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200711 if (ret < 0)
Jingoo Han990810b2013-02-20 18:30:21 +0900712 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500713
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200714 twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200715 TWL6040_IRQ_READY);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200716 twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200717 TWL6040_IRQ_TH);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200718
Jingoo Han990810b2013-02-20 18:30:21 +0900719 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200720 twl6040_readyint_handler, IRQF_ONESHOT,
721 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300722 if (ret) {
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200723 dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
724 goto readyirq_err;
725 }
726
Jingoo Han990810b2013-02-20 18:30:21 +0900727 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200728 twl6040_thint_handler, IRQF_ONESHOT,
729 "twl6040_irq_th", twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200730 if (ret) {
731 dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
Wei Yongjunfc5ee962013-09-25 15:37:15 +0800732 goto readyirq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500733 }
734
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300735 /*
736 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
737 * We can add the ASoC codec child whenever this driver has been loaded.
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300738 */
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200739 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300740 cell = &twl6040->cells[children];
741 cell->name = "twl6040-codec";
742 twl6040_codec_rsrc[0].start = irq;
743 twl6040_codec_rsrc[0].end = irq;
744 cell->resources = twl6040_codec_rsrc;
745 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300746 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500747
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200748 /* Vibra input driver support */
749 if (twl6040_has_vibra(node)) {
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200750 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300751
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500752 cell = &twl6040->cells[children];
753 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300754 twl6040_vibra_rsrc[0].start = irq;
755 twl6040_vibra_rsrc[0].end = irq;
756 cell->resources = twl6040_vibra_rsrc;
757 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500758 children++;
759 }
760
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200761 /* GPO support */
762 cell = &twl6040->cells[children];
763 cell->name = "twl6040-gpo";
764 children++;
Peter Ujfalusi5cbe7862012-08-16 15:13:14 +0300765
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200766 /* The chip is powered down so mark regmap to cache only and dirty */
767 regcache_cache_only(twl6040->regmap, true);
768 regcache_mark_dirty(twl6040->regmap);
769
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300770 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
Mark Brown55692af2012-09-11 15:16:36 +0800771 NULL, 0, NULL);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300772 if (ret)
Wei Yongjunfc5ee962013-09-25 15:37:15 +0800773 goto readyirq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500774
775 return 0;
776
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200777readyirq_err:
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200778 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300779gpio_err:
780 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500781 return ret;
782}
783
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800784static int twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500785{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300786 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500787
788 if (twl6040->power_count)
789 twl6040_power(twl6040, 0);
790
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200791 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500792
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300793 mfd_remove_devices(&client->dev);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500794
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300795 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300796
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500797 return 0;
798}
799
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300800static const struct i2c_device_id twl6040_i2c_id[] = {
801 { "twl6040", 0, },
Peter Ujfalusi1fc74ae2012-07-16 11:49:44 +0200802 { "twl6041", 0, },
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300803 { },
804};
805MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
806
807static struct i2c_driver twl6040_driver = {
808 .driver = {
809 .name = "twl6040",
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300810 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500811 .probe = twl6040_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800812 .remove = twl6040_remove,
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300813 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500814};
815
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300816module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500817
818MODULE_DESCRIPTION("TWL6040 MFD");
819MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
820MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
821MODULE_LICENSE("GPL");