Linux Audio

Check our new training course

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