blob: 75316fb33448d6041d13e91f1c02c9321cd03818 [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 Ujfalusic7f91292013-11-29 16:03:45 +020090 /* Select I2C bus access to dual access registers */
91 { TWL6040_REG_ACCCTL, 0x09 },
92};
93
94
Peter Ujfalusidf04b622013-07-12 13:32:02 +020095static bool twl6040_has_vibra(struct device_node *node)
Samuel Ortizca2cad62012-05-23 16:23:21 +020096{
Samuel Ortizca2cad62012-05-23 16:23:21 +020097#ifdef CONFIG_OF
98 if (of_find_node_by_name(node, "vibra"))
99 return true;
100#endif
Samuel Ortizca2cad62012-05-23 16:23:21 +0200101 return false;
102}
103
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500104int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
105{
106 int ret;
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300107 unsigned int val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500108
Mark Brownc6f39252013-08-31 17:48:19 +0100109 ret = regmap_read(twl6040->regmap, reg, &val);
110 if (ret < 0)
111 return ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500112
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
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300121 ret = regmap_write(twl6040->regmap, reg, val);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500122
123 return ret;
124}
125EXPORT_SYMBOL(twl6040_reg_write);
126
127int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
128{
Axel Linc6000402012-07-11 10:06:34 +0800129 return regmap_update_bits(twl6040->regmap, reg, mask, mask);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500130}
131EXPORT_SYMBOL(twl6040_set_bits);
132
133int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
134{
Axel Linc6000402012-07-11 10:06:34 +0800135 return regmap_update_bits(twl6040->regmap, reg, mask, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500136}
137EXPORT_SYMBOL(twl6040_clear_bits);
138
139/* twl6040 codec manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200140static int twl6040_power_up_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500141{
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
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200194 dev_err(twl6040->dev, "manual power-up failed\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500195 return ret;
196}
197
198/* twl6040 manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200199static void twl6040_power_down_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500200{
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
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200229static irqreturn_t twl6040_readyint_handler(int irq, void *data)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500230{
231 struct twl6040 *twl6040 = data;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500232
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200233 complete(&twl6040->ready);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500234
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200235 return IRQ_HANDLED;
236}
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500237
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200238static 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);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500250 }
251
252 return IRQ_HANDLED;
253}
254
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200255static int twl6040_power_up_automatic(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500256{
257 int time_left;
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200258
259 gpio_set_value(twl6040->audpwron, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500260
261 time_left = wait_for_completion_timeout(&twl6040->ready,
262 msecs_to_jiffies(144));
263 if (!time_left) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200264 u8 intid;
265
266 dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500267 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
268 if (!(intid & TWL6040_READYINT)) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200269 dev_err(twl6040->dev, "automatic power-up failed\n");
270 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500271 return -ETIMEDOUT;
272 }
273 }
274
275 return 0;
276}
277
278int twl6040_power(struct twl6040 *twl6040, int on)
279{
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500280 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
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200289 /* Allow writes to the chip */
290 regcache_cache_only(twl6040->regmap, false);
291
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200292 if (gpio_is_valid(twl6040->audpwron)) {
293 /* use automatic power-up sequence */
294 ret = twl6040_power_up_automatic(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500295 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500296 twl6040->power_count = 0;
297 goto out;
298 }
299 } else {
300 /* use manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200301 ret = twl6040_power_up_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500302 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500303 twl6040->power_count = 0;
304 goto out;
305 }
306 }
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200307
308 /* Sync with the HW */
309 regcache_sync(twl6040->regmap);
310
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300311 /* Default PLL configuration after power up */
312 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500313 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100314 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500315 } else {
316 /* already powered-down */
317 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300318 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500319 "device is already powered-off\n");
320 ret = -EPERM;
321 goto out;
322 }
323
324 if (--twl6040->power_count)
325 goto out;
326
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200327 if (gpio_is_valid(twl6040->audpwron)) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500328 /* use AUDPWRON line */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200329 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500330
331 /* power-down sequence latency */
332 usleep_range(500, 700);
333 } else {
334 /* use manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200335 twl6040_power_down_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500336 }
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200337
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
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500342 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100343 twl6040->mclk = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500344 }
345
346out:
347 mutex_unlock(&twl6040->mutex);
348 return ret;
349}
350EXPORT_SYMBOL(twl6040_power);
351
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300352int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500353 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
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100363 /* Force full reconfiguration when switching between PLL */
364 if (pll_id != twl6040->pll) {
365 twl6040->sysclk = 0;
366 twl6040->mclk = 0;
367 }
368
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300369 switch (pll_id) {
370 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500371 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100372 /* 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);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500390 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100391
392 /* The PLL in use has not been change, we can exit */
393 if (twl6040->pll == pll_id)
394 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500395
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:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300410 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500411 "freq_in %d not supported\n", freq_in);
412 ret = -EINVAL;
413 goto pll_out;
414 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500415 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300416 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500417 /* high-performance PLL can provide only 19.2 MHz */
418 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300419 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500420 "freq_out %d not supported\n", freq_out);
421 ret = -EINVAL;
422 goto pll_out;
423 }
424
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100425 if (twl6040->mclk != freq_in) {
426 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500427
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100428 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 }
458
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500459 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100460 * enable clock slicer to ensure input waveform is
461 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500462 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100463 hppllctl |= TWL6040_HPLLSQRENA;
464
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);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500474 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500475 break;
476 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300477 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500478 ret = -EINVAL;
479 goto pll_out;
480 }
481
482 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100483 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300484 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500485
486pll_out:
487 mutex_unlock(&twl6040->mutex);
488 return ret;
489}
490EXPORT_SYMBOL(twl6040_set_pll);
491
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300492int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500493{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300494 if (twl6040->power_count)
495 return twl6040->pll;
496 else
497 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500498}
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
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300507/* Get the combined status of the vibra control register */
508int twl6040_get_vibralr_status(struct twl6040 *twl6040)
509{
Mark Brownc6f39252013-08-31 17:48:19 +0100510 unsigned int reg;
511 int ret;
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300512 u8 status;
513
Mark Brownc6f39252013-08-31 17:48:19 +0100514 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
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300524 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
525
526 return status;
527}
528EXPORT_SYMBOL(twl6040_get_vibralr_status);
529
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300530static 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
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300542static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500543{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300544 /* Register 0 is not readable */
545 if (!reg)
546 return false;
547 return true;
548}
549
Mark Brownc6f39252013-08-31 17:48:19 +0100550static bool twl6040_volatile_reg(struct device *dev, unsigned int reg)
551{
552 switch (reg) {
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200553 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:
Mark Brownc6f39252013-08-31 17:48:19 +0100571 return false;
572 default:
573 return true;
574 }
575}
576
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300577static struct regmap_config twl6040_regmap_config = {
578 .reg_bits = 8,
579 .val_bits = 8,
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200580
581 .reg_defaults = twl6040_defaults,
582 .num_reg_defaults = ARRAY_SIZE(twl6040_defaults),
583
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300584 .max_register = TWL6040_REG_STATUS, /* 0x2e */
585
586 .readable_reg = twl6040_readable_reg,
Mark Brownc6f39252013-08-31 17:48:19 +0100587 .volatile_reg = twl6040_volatile_reg,
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200588 .writeable_reg = twl6040_writeable_reg,
Mark Brownc6f39252013-08-31 17:48:19 +0100589
590 .cache_type = REGCACHE_RBTREE,
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300591};
592
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200593static 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
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800612static int twl6040_probe(struct i2c_client *client,
613 const struct i2c_device_id *id)
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300614{
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300615 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500616 struct twl6040 *twl6040;
617 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300618 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500619
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200620 if (!node) {
621 dev_err(&client->dev, "of node is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500622 return -EINVAL;
623 }
624
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300625 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300626 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300627 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300628 return -EINVAL;
629 }
630
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300631 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
632 GFP_KERNEL);
Peter Ujfalusiecc8fa12013-07-12 13:32:04 +0200633 if (!twl6040)
634 return -ENOMEM;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500635
Axel Linbbf6adc2012-04-25 10:09:46 +0800636 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusiecc8fa12013-07-12 13:32:04 +0200637 if (IS_ERR(twl6040->regmap))
638 return PTR_ERR(twl6040->regmap);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500639
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300640 i2c_set_clientdata(client, twl6040);
641
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300642 twl6040->supplies[0].supply = "vio";
643 twl6040->supplies[1].supply = "v2v1";
Jingoo Han990810b2013-02-20 18:30:21 +0900644 ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200645 twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300646 if (ret != 0) {
647 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
Wolfram Sang501d6092013-10-13 18:06:12 +0200648 return ret;
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300649 }
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);
Wolfram Sang501d6092013-10-13 18:06:12 +0200654 return ret;
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300655 }
656
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300657 twl6040->dev = &client->dev;
658 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500659
660 mutex_init(&twl6040->mutex);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500661 init_completion(&twl6040->ready);
662
663 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
664
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300665 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200666 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
667 twl6040->audpwron = of_get_named_gpio(node,
668 "ti,audpwron-gpio", 0);
669 else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300670 twl6040->audpwron = -EINVAL;
671
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500672 if (gpio_is_valid(twl6040->audpwron)) {
Jingoo Han990810b2013-02-20 18:30:21 +0900673 ret = devm_gpio_request_one(&client->dev, twl6040->audpwron,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200674 GPIOF_OUT_INIT_LOW, "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500675 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300676 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500677 }
678
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200679 ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT,
680 0, &twl6040_irq_chip,&twl6040->irq_data);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200681 if (ret < 0)
Jingoo Han990810b2013-02-20 18:30:21 +0900682 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500683
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200684 twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200685 TWL6040_IRQ_READY);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200686 twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200687 TWL6040_IRQ_TH);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200688
Jingoo Han990810b2013-02-20 18:30:21 +0900689 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200690 twl6040_readyint_handler, IRQF_ONESHOT,
691 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300692 if (ret) {
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200693 dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
694 goto readyirq_err;
695 }
696
Jingoo Han990810b2013-02-20 18:30:21 +0900697 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200698 twl6040_thint_handler, IRQF_ONESHOT,
699 "twl6040_irq_th", twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200700 if (ret) {
701 dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
Wei Yongjunfc5ee962013-09-25 15:37:15 +0800702 goto readyirq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500703 }
704
705 /* dual-access registers controlled by I2C only */
706 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200707 regmap_register_patch(twl6040->regmap, twl6040_patch,
708 ARRAY_SIZE(twl6040_patch));
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500709
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300710 /*
711 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
712 * We can add the ASoC codec child whenever this driver has been loaded.
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300713 */
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200714 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300715 cell = &twl6040->cells[children];
716 cell->name = "twl6040-codec";
717 twl6040_codec_rsrc[0].start = irq;
718 twl6040_codec_rsrc[0].end = irq;
719 cell->resources = twl6040_codec_rsrc;
720 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300721 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500722
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200723 /* Vibra input driver support */
724 if (twl6040_has_vibra(node)) {
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200725 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300726
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500727 cell = &twl6040->cells[children];
728 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300729 twl6040_vibra_rsrc[0].start = irq;
730 twl6040_vibra_rsrc[0].end = irq;
731 cell->resources = twl6040_vibra_rsrc;
732 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500733 children++;
734 }
735
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200736 /* GPO support */
737 cell = &twl6040->cells[children];
738 cell->name = "twl6040-gpo";
739 children++;
Peter Ujfalusi5cbe7862012-08-16 15:13:14 +0300740
Peter Ujfalusic7f91292013-11-29 16:03:45 +0200741 /* The chip is powered down so mark regmap to cache only and dirty */
742 regcache_cache_only(twl6040->regmap, true);
743 regcache_mark_dirty(twl6040->regmap);
744
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300745 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
Mark Brown55692af2012-09-11 15:16:36 +0800746 NULL, 0, NULL);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300747 if (ret)
Wei Yongjunfc5ee962013-09-25 15:37:15 +0800748 goto readyirq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500749
750 return 0;
751
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200752readyirq_err:
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200753 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300754gpio_err:
755 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500756 return ret;
757}
758
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800759static int twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500760{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300761 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500762
763 if (twl6040->power_count)
764 twl6040_power(twl6040, 0);
765
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200766 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500767
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300768 mfd_remove_devices(&client->dev);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500769
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300770 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300771
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500772 return 0;
773}
774
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300775static const struct i2c_device_id twl6040_i2c_id[] = {
776 { "twl6040", 0, },
Peter Ujfalusi1fc74ae2012-07-16 11:49:44 +0200777 { "twl6041", 0, },
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300778 { },
779};
780MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
781
782static struct i2c_driver twl6040_driver = {
783 .driver = {
784 .name = "twl6040",
785 .owner = THIS_MODULE,
786 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500787 .probe = twl6040_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800788 .remove = twl6040_remove,
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300789 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500790};
791
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300792module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500793
794MODULE_DESCRIPTION("TWL6040 MFD");
795MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
796MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
797MODULE_LICENSE("GPL");
798MODULE_ALIAS("platform:twl6040");