blob: daf66942071c9084779497f6b40ac7bc2c8c916d [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 Ujfalusidf04b622013-07-12 13:32:02 +020047static bool twl6040_has_vibra(struct device_node *node)
Samuel Ortizca2cad62012-05-23 16:23:21 +020048{
Samuel Ortizca2cad62012-05-23 16:23:21 +020049#ifdef CONFIG_OF
50 if (of_find_node_by_name(node, "vibra"))
51 return true;
52#endif
Samuel Ortizca2cad62012-05-23 16:23:21 +020053 return false;
54}
55
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050056int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
57{
58 int ret;
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030059 unsigned int val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050060
Mark Brownc6f39252013-08-31 17:48:19 +010061 ret = regmap_read(twl6040->regmap, reg, &val);
62 if (ret < 0)
63 return ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050064
65 return val;
66}
67EXPORT_SYMBOL(twl6040_reg_read);
68
69int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
70{
71 int ret;
72
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030073 ret = regmap_write(twl6040->regmap, reg, val);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050074
75 return ret;
76}
77EXPORT_SYMBOL(twl6040_reg_write);
78
79int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
80{
Axel Linc6000402012-07-11 10:06:34 +080081 return regmap_update_bits(twl6040->regmap, reg, mask, mask);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050082}
83EXPORT_SYMBOL(twl6040_set_bits);
84
85int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
86{
Axel Linc6000402012-07-11 10:06:34 +080087 return regmap_update_bits(twl6040->regmap, reg, mask, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050088}
89EXPORT_SYMBOL(twl6040_clear_bits);
90
91/* twl6040 codec manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +020092static int twl6040_power_up_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050093{
94 u8 ldoctl, ncpctl, lppllctl;
95 int ret;
96
97 /* enable high-side LDO, reference system and internal oscillator */
98 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
99 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
100 if (ret)
101 return ret;
102 usleep_range(10000, 10500);
103
104 /* enable negative charge pump */
105 ncpctl = TWL6040_NCPENA;
106 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
107 if (ret)
108 goto ncp_err;
109 usleep_range(1000, 1500);
110
111 /* enable low-side LDO */
112 ldoctl |= TWL6040_LSLDOENA;
113 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
114 if (ret)
115 goto lsldo_err;
116 usleep_range(1000, 1500);
117
118 /* enable low-power PLL */
119 lppllctl = TWL6040_LPLLENA;
120 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
121 if (ret)
122 goto lppll_err;
123 usleep_range(5000, 5500);
124
125 /* disable internal oscillator */
126 ldoctl &= ~TWL6040_OSCENA;
127 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
128 if (ret)
129 goto osc_err;
130
131 return 0;
132
133osc_err:
134 lppllctl &= ~TWL6040_LPLLENA;
135 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
136lppll_err:
137 ldoctl &= ~TWL6040_LSLDOENA;
138 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
139lsldo_err:
140 ncpctl &= ~TWL6040_NCPENA;
141 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
142ncp_err:
143 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
144 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
145
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200146 dev_err(twl6040->dev, "manual power-up failed\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500147 return ret;
148}
149
150/* twl6040 manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200151static void twl6040_power_down_manual(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500152{
153 u8 ncpctl, ldoctl, lppllctl;
154
155 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
156 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
157 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
158
159 /* enable internal oscillator */
160 ldoctl |= TWL6040_OSCENA;
161 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
162 usleep_range(1000, 1500);
163
164 /* disable low-power PLL */
165 lppllctl &= ~TWL6040_LPLLENA;
166 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
167
168 /* disable low-side LDO */
169 ldoctl &= ~TWL6040_LSLDOENA;
170 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
171
172 /* disable negative charge pump */
173 ncpctl &= ~TWL6040_NCPENA;
174 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
175
176 /* disable high-side LDO, reference system and internal oscillator */
177 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
178 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
179}
180
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200181static irqreturn_t twl6040_readyint_handler(int irq, void *data)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500182{
183 struct twl6040 *twl6040 = data;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500184
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200185 complete(&twl6040->ready);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500186
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200187 return IRQ_HANDLED;
188}
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500189
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200190static irqreturn_t twl6040_thint_handler(int irq, void *data)
191{
192 struct twl6040 *twl6040 = data;
193 u8 status;
194
195 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
196 if (status & TWL6040_TSHUTDET) {
197 dev_warn(twl6040->dev, "Thermal shutdown, powering-off");
198 twl6040_power(twl6040, 0);
199 } else {
200 dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on");
201 twl6040_power(twl6040, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500202 }
203
204 return IRQ_HANDLED;
205}
206
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200207static int twl6040_power_up_automatic(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500208{
209 int time_left;
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200210
211 gpio_set_value(twl6040->audpwron, 1);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500212
213 time_left = wait_for_completion_timeout(&twl6040->ready,
214 msecs_to_jiffies(144));
215 if (!time_left) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200216 u8 intid;
217
218 dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500219 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
220 if (!(intid & TWL6040_READYINT)) {
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200221 dev_err(twl6040->dev, "automatic power-up failed\n");
222 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500223 return -ETIMEDOUT;
224 }
225 }
226
227 return 0;
228}
229
230int twl6040_power(struct twl6040 *twl6040, int on)
231{
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500232 int ret = 0;
233
234 mutex_lock(&twl6040->mutex);
235
236 if (on) {
237 /* already powered-up */
238 if (twl6040->power_count++)
239 goto out;
240
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200241 if (gpio_is_valid(twl6040->audpwron)) {
242 /* use automatic power-up sequence */
243 ret = twl6040_power_up_automatic(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500244 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500245 twl6040->power_count = 0;
246 goto out;
247 }
248 } else {
249 /* use manual power-up sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200250 ret = twl6040_power_up_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500251 if (ret) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500252 twl6040->power_count = 0;
253 goto out;
254 }
255 }
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300256 /* Default PLL configuration after power up */
257 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500258 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100259 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500260 } else {
261 /* already powered-down */
262 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300263 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500264 "device is already powered-off\n");
265 ret = -EPERM;
266 goto out;
267 }
268
269 if (--twl6040->power_count)
270 goto out;
271
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200272 if (gpio_is_valid(twl6040->audpwron)) {
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500273 /* use AUDPWRON line */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200274 gpio_set_value(twl6040->audpwron, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500275
276 /* power-down sequence latency */
277 usleep_range(500, 700);
278 } else {
279 /* use manual power-down sequence */
Peter Ujfalusif9be1342012-10-11 13:55:30 +0200280 twl6040_power_down_manual(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500281 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500282 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100283 twl6040->mclk = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500284 }
285
286out:
287 mutex_unlock(&twl6040->mutex);
288 return ret;
289}
290EXPORT_SYMBOL(twl6040_power);
291
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300292int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500293 unsigned int freq_in, unsigned int freq_out)
294{
295 u8 hppllctl, lppllctl;
296 int ret = 0;
297
298 mutex_lock(&twl6040->mutex);
299
300 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
301 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
302
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100303 /* Force full reconfiguration when switching between PLL */
304 if (pll_id != twl6040->pll) {
305 twl6040->sysclk = 0;
306 twl6040->mclk = 0;
307 }
308
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300309 switch (pll_id) {
310 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500311 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100312 /* Change the sysclk configuration only if it has been canged */
313 if (twl6040->sysclk != freq_out) {
314 switch (freq_out) {
315 case 17640000:
316 lppllctl |= TWL6040_LPLLFIN;
317 break;
318 case 19200000:
319 lppllctl &= ~TWL6040_LPLLFIN;
320 break;
321 default:
322 dev_err(twl6040->dev,
323 "freq_out %d not supported\n",
324 freq_out);
325 ret = -EINVAL;
326 goto pll_out;
327 }
328 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
329 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500330 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100331
332 /* The PLL in use has not been change, we can exit */
333 if (twl6040->pll == pll_id)
334 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500335
336 switch (freq_in) {
337 case 32768:
338 lppllctl |= TWL6040_LPLLENA;
339 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
340 lppllctl);
341 mdelay(5);
342 lppllctl &= ~TWL6040_HPLLSEL;
343 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
344 lppllctl);
345 hppllctl &= ~TWL6040_HPLLENA;
346 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
347 hppllctl);
348 break;
349 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300350 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500351 "freq_in %d not supported\n", freq_in);
352 ret = -EINVAL;
353 goto pll_out;
354 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500355 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300356 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500357 /* high-performance PLL can provide only 19.2 MHz */
358 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300359 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500360 "freq_out %d not supported\n", freq_out);
361 ret = -EINVAL;
362 goto pll_out;
363 }
364
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100365 if (twl6040->mclk != freq_in) {
366 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500367
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100368 switch (freq_in) {
369 case 12000000:
370 /* PLL enabled, active mode */
371 hppllctl |= TWL6040_MCLK_12000KHZ |
372 TWL6040_HPLLENA;
373 break;
374 case 19200000:
375 /*
376 * PLL disabled
377 * (enable PLL if MCLK jitter quality
378 * doesn't meet specification)
379 */
380 hppllctl |= TWL6040_MCLK_19200KHZ;
381 break;
382 case 26000000:
383 /* PLL enabled, active mode */
384 hppllctl |= TWL6040_MCLK_26000KHZ |
385 TWL6040_HPLLENA;
386 break;
387 case 38400000:
388 /* PLL enabled, active mode */
389 hppllctl |= TWL6040_MCLK_38400KHZ |
390 TWL6040_HPLLENA;
391 break;
392 default:
393 dev_err(twl6040->dev,
394 "freq_in %d not supported\n", freq_in);
395 ret = -EINVAL;
396 goto pll_out;
397 }
398
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500399 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100400 * enable clock slicer to ensure input waveform is
401 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500402 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100403 hppllctl |= TWL6040_HPLLSQRENA;
404
405 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
406 hppllctl);
407 usleep_range(500, 700);
408 lppllctl |= TWL6040_HPLLSEL;
409 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
410 lppllctl);
411 lppllctl &= ~TWL6040_LPLLENA;
412 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
413 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500414 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500415 break;
416 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300417 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500418 ret = -EINVAL;
419 goto pll_out;
420 }
421
422 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100423 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300424 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500425
426pll_out:
427 mutex_unlock(&twl6040->mutex);
428 return ret;
429}
430EXPORT_SYMBOL(twl6040_set_pll);
431
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300432int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500433{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300434 if (twl6040->power_count)
435 return twl6040->pll;
436 else
437 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500438}
439EXPORT_SYMBOL(twl6040_get_pll);
440
441unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
442{
443 return twl6040->sysclk;
444}
445EXPORT_SYMBOL(twl6040_get_sysclk);
446
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300447/* Get the combined status of the vibra control register */
448int twl6040_get_vibralr_status(struct twl6040 *twl6040)
449{
Mark Brownc6f39252013-08-31 17:48:19 +0100450 unsigned int reg;
451 int ret;
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300452 u8 status;
453
Mark Brownc6f39252013-08-31 17:48:19 +0100454 ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLL, &reg);
455 if (ret != 0)
456 return ret;
457 status = reg;
458
459 ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLR, &reg);
460 if (ret != 0)
461 return ret;
462 status |= reg;
463
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300464 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
465
466 return status;
467}
468EXPORT_SYMBOL(twl6040_get_vibralr_status);
469
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300470static struct resource twl6040_vibra_rsrc[] = {
471 {
472 .flags = IORESOURCE_IRQ,
473 },
474};
475
476static struct resource twl6040_codec_rsrc[] = {
477 {
478 .flags = IORESOURCE_IRQ,
479 },
480};
481
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300482static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500483{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300484 /* Register 0 is not readable */
485 if (!reg)
486 return false;
487 return true;
488}
489
Mark Brownc6f39252013-08-31 17:48:19 +0100490static bool twl6040_volatile_reg(struct device *dev, unsigned int reg)
491{
492 switch (reg) {
493 case TWL6040_REG_VIBCTLL:
494 case TWL6040_REG_VIBCTLR:
495 case TWL6040_REG_INTMR:
496 return false;
497 default:
498 return true;
499 }
500}
501
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300502static struct regmap_config twl6040_regmap_config = {
503 .reg_bits = 8,
504 .val_bits = 8,
505 .max_register = TWL6040_REG_STATUS, /* 0x2e */
506
507 .readable_reg = twl6040_readable_reg,
Mark Brownc6f39252013-08-31 17:48:19 +0100508 .volatile_reg = twl6040_volatile_reg,
509
510 .cache_type = REGCACHE_RBTREE,
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300511};
512
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200513static const struct regmap_irq twl6040_irqs[] = {
514 { .reg_offset = 0, .mask = TWL6040_THINT, },
515 { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
516 { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
517 { .reg_offset = 0, .mask = TWL6040_HFINT, },
518 { .reg_offset = 0, .mask = TWL6040_VIBINT, },
519 { .reg_offset = 0, .mask = TWL6040_READYINT, },
520};
521
522static struct regmap_irq_chip twl6040_irq_chip = {
523 .name = "twl6040",
524 .irqs = twl6040_irqs,
525 .num_irqs = ARRAY_SIZE(twl6040_irqs),
526
527 .num_regs = 1,
528 .status_base = TWL6040_REG_INTID,
529 .mask_base = TWL6040_REG_INTMR,
530};
531
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800532static int twl6040_probe(struct i2c_client *client,
533 const struct i2c_device_id *id)
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300534{
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300535 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500536 struct twl6040 *twl6040;
537 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300538 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500539
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200540 if (!node) {
541 dev_err(&client->dev, "of node is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500542 return -EINVAL;
543 }
544
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300545 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300546 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300547 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300548 return -EINVAL;
549 }
550
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300551 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
552 GFP_KERNEL);
Peter Ujfalusiecc8fa12013-07-12 13:32:04 +0200553 if (!twl6040)
554 return -ENOMEM;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500555
Axel Linbbf6adc2012-04-25 10:09:46 +0800556 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusiecc8fa12013-07-12 13:32:04 +0200557 if (IS_ERR(twl6040->regmap))
558 return PTR_ERR(twl6040->regmap);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500559
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300560 i2c_set_clientdata(client, twl6040);
561
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300562 twl6040->supplies[0].supply = "vio";
563 twl6040->supplies[1].supply = "v2v1";
Jingoo Han990810b2013-02-20 18:30:21 +0900564 ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200565 twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300566 if (ret != 0) {
567 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
568 goto regulator_get_err;
569 }
570
571 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
572 if (ret != 0) {
573 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
Jingoo Han990810b2013-02-20 18:30:21 +0900574 goto regulator_get_err;
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300575 }
576
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300577 twl6040->dev = &client->dev;
578 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500579
580 mutex_init(&twl6040->mutex);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500581 init_completion(&twl6040->ready);
582
583 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
584
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300585 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200586 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
587 twl6040->audpwron = of_get_named_gpio(node,
588 "ti,audpwron-gpio", 0);
589 else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300590 twl6040->audpwron = -EINVAL;
591
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500592 if (gpio_is_valid(twl6040->audpwron)) {
Jingoo Han990810b2013-02-20 18:30:21 +0900593 ret = devm_gpio_request_one(&client->dev, twl6040->audpwron,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200594 GPIOF_OUT_INIT_LOW, "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500595 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300596 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500597 }
598
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200599 ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT,
600 0, &twl6040_irq_chip,&twl6040->irq_data);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200601 if (ret < 0)
Jingoo Han990810b2013-02-20 18:30:21 +0900602 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500603
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200604 twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200605 TWL6040_IRQ_READY);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200606 twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200607 TWL6040_IRQ_TH);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200608
Jingoo Han990810b2013-02-20 18:30:21 +0900609 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200610 twl6040_readyint_handler, IRQF_ONESHOT,
611 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300612 if (ret) {
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200613 dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
614 goto readyirq_err;
615 }
616
Jingoo Han990810b2013-02-20 18:30:21 +0900617 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
Peter Ujfalusi37aefe92013-07-12 13:32:03 +0200618 twl6040_thint_handler, IRQF_ONESHOT,
619 "twl6040_irq_th", twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200620 if (ret) {
621 dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
622 goto thirq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500623 }
624
625 /* dual-access registers controlled by I2C only */
626 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
627
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300628 /*
629 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
630 * We can add the ASoC codec child whenever this driver has been loaded.
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300631 */
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200632 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300633 cell = &twl6040->cells[children];
634 cell->name = "twl6040-codec";
635 twl6040_codec_rsrc[0].start = irq;
636 twl6040_codec_rsrc[0].end = irq;
637 cell->resources = twl6040_codec_rsrc;
638 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300639 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500640
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200641 /* Vibra input driver support */
642 if (twl6040_has_vibra(node)) {
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200643 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300644
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500645 cell = &twl6040->cells[children];
646 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300647 twl6040_vibra_rsrc[0].start = irq;
648 twl6040_vibra_rsrc[0].end = irq;
649 cell->resources = twl6040_vibra_rsrc;
650 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500651 children++;
652 }
653
Peter Ujfalusidf04b622013-07-12 13:32:02 +0200654 /* GPO support */
655 cell = &twl6040->cells[children];
656 cell->name = "twl6040-gpo";
657 children++;
Peter Ujfalusi5cbe7862012-08-16 15:13:14 +0300658
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300659 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
Mark Brown55692af2012-09-11 15:16:36 +0800660 NULL, 0, NULL);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300661 if (ret)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500662 goto mfd_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500663
664 return 0;
665
666mfd_err:
Jingoo Han990810b2013-02-20 18:30:21 +0900667 devm_free_irq(&client->dev, twl6040->irq_th, twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200668thirq_err:
Jingoo Han990810b2013-02-20 18:30:21 +0900669 devm_free_irq(&client->dev, twl6040->irq_ready, twl6040);
Peter Ujfalusi1ac96262012-10-11 13:55:31 +0200670readyirq_err:
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200671 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300672gpio_err:
673 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300674regulator_get_err:
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300675 i2c_set_clientdata(client, NULL);
Peter Ujfalusiecc8fa12013-07-12 13:32:04 +0200676
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500677 return ret;
678}
679
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800680static int twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500681{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300682 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500683
684 if (twl6040->power_count)
685 twl6040_power(twl6040, 0);
686
Jingoo Han990810b2013-02-20 18:30:21 +0900687 devm_free_irq(&client->dev, twl6040->irq_ready, twl6040);
688 devm_free_irq(&client->dev, twl6040->irq_th, twl6040);
Peter Ujfalusiab7edb12012-10-11 13:55:32 +0200689 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500690
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300691 mfd_remove_devices(&client->dev);
692 i2c_set_clientdata(client, NULL);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500693
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300694 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300695
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500696 return 0;
697}
698
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300699static const struct i2c_device_id twl6040_i2c_id[] = {
700 { "twl6040", 0, },
Peter Ujfalusi1fc74ae2012-07-16 11:49:44 +0200701 { "twl6041", 0, },
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300702 { },
703};
704MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
705
706static struct i2c_driver twl6040_driver = {
707 .driver = {
708 .name = "twl6040",
709 .owner = THIS_MODULE,
710 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500711 .probe = twl6040_probe,
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800712 .remove = twl6040_remove,
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300713 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500714};
715
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300716module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500717
718MODULE_DESCRIPTION("TWL6040 MFD");
719MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
720MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
721MODULE_LICENSE("GPL");
722MODULE_ALIAS("platform:twl6040");