12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2210afeecSPeter Ujfalusi /*
3210afeecSPeter Ujfalusi * MFD driver for TWL6040 audio device
4210afeecSPeter Ujfalusi *
5210afeecSPeter Ujfalusi * Authors: Misael Lopez Cruz <misael.lopez@ti.com>
6210afeecSPeter Ujfalusi * Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
7210afeecSPeter Ujfalusi * Peter Ujfalusi <peter.ujfalusi@ti.com>
8210afeecSPeter Ujfalusi *
9210afeecSPeter Ujfalusi * Copyright: (C) 2011 Texas Instruments, Inc.
10210afeecSPeter Ujfalusi */
11210afeecSPeter Ujfalusi
12210afeecSPeter Ujfalusi #include <linux/module.h>
13210afeecSPeter Ujfalusi #include <linux/types.h>
14210afeecSPeter Ujfalusi #include <linux/slab.h>
15210afeecSPeter Ujfalusi #include <linux/kernel.h>
16210afeecSPeter Ujfalusi #include <linux/err.h>
17210afeecSPeter Ujfalusi #include <linux/platform_device.h>
18210afeecSPeter Ujfalusi #include <linux/of.h>
193c92699aSDmitry Torokhov #include <linux/gpio/consumer.h>
20210afeecSPeter Ujfalusi #include <linux/delay.h>
21210afeecSPeter Ujfalusi #include <linux/i2c.h>
22210afeecSPeter Ujfalusi #include <linux/regmap.h>
23210afeecSPeter Ujfalusi #include <linux/mfd/core.h>
24210afeecSPeter Ujfalusi #include <linux/mfd/twl6040.h>
25210afeecSPeter Ujfalusi #include <linux/regulator/consumer.h>
26210afeecSPeter Ujfalusi
27210afeecSPeter Ujfalusi #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
28210afeecSPeter Ujfalusi #define TWL6040_NUM_SUPPLIES (2)
29210afeecSPeter Ujfalusi
30de1e23f8SKrzysztof Kozlowski static const struct reg_default twl6040_defaults[] = {
31c7f9129dSPeter Ujfalusi { 0x01, 0x4B }, /* REG_ASICID (ro) */
32c7f9129dSPeter Ujfalusi { 0x02, 0x00 }, /* REG_ASICREV (ro) */
33c7f9129dSPeter Ujfalusi { 0x03, 0x00 }, /* REG_INTID */
34c7f9129dSPeter Ujfalusi { 0x04, 0x00 }, /* REG_INTMR */
35c7f9129dSPeter Ujfalusi { 0x05, 0x00 }, /* REG_NCPCTRL */
36c7f9129dSPeter Ujfalusi { 0x06, 0x00 }, /* REG_LDOCTL */
37c7f9129dSPeter Ujfalusi { 0x07, 0x60 }, /* REG_HPPLLCTL */
38c7f9129dSPeter Ujfalusi { 0x08, 0x00 }, /* REG_LPPLLCTL */
39c7f9129dSPeter Ujfalusi { 0x09, 0x4A }, /* REG_LPPLLDIV */
40c7f9129dSPeter Ujfalusi { 0x0A, 0x00 }, /* REG_AMICBCTL */
41c7f9129dSPeter Ujfalusi { 0x0B, 0x00 }, /* REG_DMICBCTL */
42c7f9129dSPeter Ujfalusi { 0x0C, 0x00 }, /* REG_MICLCTL */
43c7f9129dSPeter Ujfalusi { 0x0D, 0x00 }, /* REG_MICRCTL */
44c7f9129dSPeter Ujfalusi { 0x0E, 0x00 }, /* REG_MICGAIN */
45c7f9129dSPeter Ujfalusi { 0x0F, 0x1B }, /* REG_LINEGAIN */
46c7f9129dSPeter Ujfalusi { 0x10, 0x00 }, /* REG_HSLCTL */
47c7f9129dSPeter Ujfalusi { 0x11, 0x00 }, /* REG_HSRCTL */
48c7f9129dSPeter Ujfalusi { 0x12, 0x00 }, /* REG_HSGAIN */
49c7f9129dSPeter Ujfalusi { 0x13, 0x00 }, /* REG_EARCTL */
50c7f9129dSPeter Ujfalusi { 0x14, 0x00 }, /* REG_HFLCTL */
51c7f9129dSPeter Ujfalusi { 0x15, 0x00 }, /* REG_HFLGAIN */
52c7f9129dSPeter Ujfalusi { 0x16, 0x00 }, /* REG_HFRCTL */
53c7f9129dSPeter Ujfalusi { 0x17, 0x00 }, /* REG_HFRGAIN */
54c7f9129dSPeter Ujfalusi { 0x18, 0x00 }, /* REG_VIBCTLL */
55c7f9129dSPeter Ujfalusi { 0x19, 0x00 }, /* REG_VIBDATL */
56c7f9129dSPeter Ujfalusi { 0x1A, 0x00 }, /* REG_VIBCTLR */
57c7f9129dSPeter Ujfalusi { 0x1B, 0x00 }, /* REG_VIBDATR */
58c7f9129dSPeter Ujfalusi { 0x1C, 0x00 }, /* REG_HKCTL1 */
59c7f9129dSPeter Ujfalusi { 0x1D, 0x00 }, /* REG_HKCTL2 */
60c7f9129dSPeter Ujfalusi { 0x1E, 0x00 }, /* REG_GPOCTL */
61c7f9129dSPeter Ujfalusi { 0x1F, 0x00 }, /* REG_ALB */
62c7f9129dSPeter Ujfalusi { 0x20, 0x00 }, /* REG_DLB */
63c7f9129dSPeter Ujfalusi /* 0x28, REG_TRIM1 */
64c7f9129dSPeter Ujfalusi /* 0x29, REG_TRIM2 */
65c7f9129dSPeter Ujfalusi /* 0x2A, REG_TRIM3 */
66c7f9129dSPeter Ujfalusi /* 0x2B, REG_HSOTRIM */
67c7f9129dSPeter Ujfalusi /* 0x2C, REG_HFOTRIM */
68c7f9129dSPeter Ujfalusi { 0x2D, 0x08 }, /* REG_ACCCTL */
69c7f9129dSPeter Ujfalusi { 0x2E, 0x00 }, /* REG_STATUS (ro) */
70c7f9129dSPeter Ujfalusi };
71c7f9129dSPeter Ujfalusi
728019ff6cSNariman Poushin static struct reg_sequence twl6040_patch[] = {
7311e38e11SPeter Ujfalusi /*
7411e38e11SPeter Ujfalusi * Select I2C bus access to dual access registers
7511e38e11SPeter Ujfalusi * Interrupt register is cleared on read
7611e38e11SPeter Ujfalusi * Select fast mode for i2c (400KHz)
7711e38e11SPeter Ujfalusi */
7811e38e11SPeter Ujfalusi { TWL6040_REG_ACCCTL,
7911e38e11SPeter Ujfalusi TWL6040_I2CSEL | TWL6040_INTCLRMODE | TWL6040_I2CMODE(1) },
80c7f9129dSPeter Ujfalusi };
81c7f9129dSPeter Ujfalusi
82c7f9129dSPeter Ujfalusi
twl6040_has_vibra(struct device_node * parent)8385e9b13cSJohan Hovold static bool twl6040_has_vibra(struct device_node *parent)
84210afeecSPeter Ujfalusi {
8585e9b13cSJohan Hovold struct device_node *node;
8685e9b13cSJohan Hovold
8785e9b13cSJohan Hovold node = of_get_child_by_name(parent, "vibra");
8885e9b13cSJohan Hovold if (node) {
8985e9b13cSJohan Hovold of_node_put(node);
90210afeecSPeter Ujfalusi return true;
9185e9b13cSJohan Hovold }
9285e9b13cSJohan Hovold
93210afeecSPeter Ujfalusi return false;
94210afeecSPeter Ujfalusi }
95210afeecSPeter Ujfalusi
twl6040_reg_read(struct twl6040 * twl6040,unsigned int reg)96210afeecSPeter Ujfalusi int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
97210afeecSPeter Ujfalusi {
98210afeecSPeter Ujfalusi int ret;
99210afeecSPeter Ujfalusi unsigned int val;
100210afeecSPeter Ujfalusi
101210afeecSPeter Ujfalusi ret = regmap_read(twl6040->regmap, reg, &val);
102210afeecSPeter Ujfalusi if (ret < 0)
103210afeecSPeter Ujfalusi return ret;
104210afeecSPeter Ujfalusi
105210afeecSPeter Ujfalusi return val;
106210afeecSPeter Ujfalusi }
107210afeecSPeter Ujfalusi EXPORT_SYMBOL(twl6040_reg_read);
108210afeecSPeter Ujfalusi
twl6040_reg_write(struct twl6040 * twl6040,unsigned int reg,u8 val)109210afeecSPeter Ujfalusi int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
110210afeecSPeter Ujfalusi {
111210afeecSPeter Ujfalusi int ret;
112210afeecSPeter Ujfalusi
113210afeecSPeter Ujfalusi ret = regmap_write(twl6040->regmap, reg, val);
114210afeecSPeter Ujfalusi
115210afeecSPeter Ujfalusi return ret;
116210afeecSPeter Ujfalusi }
117210afeecSPeter Ujfalusi EXPORT_SYMBOL(twl6040_reg_write);
118210afeecSPeter Ujfalusi
twl6040_set_bits(struct twl6040 * twl6040,unsigned int reg,u8 mask)119210afeecSPeter Ujfalusi int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
120210afeecSPeter Ujfalusi {
121210afeecSPeter Ujfalusi return regmap_update_bits(twl6040->regmap, reg, mask, mask);
122210afeecSPeter Ujfalusi }
123210afeecSPeter Ujfalusi EXPORT_SYMBOL(twl6040_set_bits);
124210afeecSPeter Ujfalusi
twl6040_clear_bits(struct twl6040 * twl6040,unsigned int reg,u8 mask)125210afeecSPeter Ujfalusi int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
126210afeecSPeter Ujfalusi {
127210afeecSPeter Ujfalusi return regmap_update_bits(twl6040->regmap, reg, mask, 0);
128210afeecSPeter Ujfalusi }
129210afeecSPeter Ujfalusi EXPORT_SYMBOL(twl6040_clear_bits);
130210afeecSPeter Ujfalusi
131210afeecSPeter Ujfalusi /* twl6040 codec manual power-up sequence */
twl6040_power_up_manual(struct twl6040 * twl6040)132210afeecSPeter Ujfalusi static int twl6040_power_up_manual(struct twl6040 *twl6040)
133210afeecSPeter Ujfalusi {
134210afeecSPeter Ujfalusi u8 ldoctl, ncpctl, lppllctl;
135210afeecSPeter Ujfalusi int ret;
136210afeecSPeter Ujfalusi
137210afeecSPeter Ujfalusi /* enable high-side LDO, reference system and internal oscillator */
138210afeecSPeter Ujfalusi ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
139210afeecSPeter Ujfalusi ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
140210afeecSPeter Ujfalusi if (ret)
141210afeecSPeter Ujfalusi return ret;
142210afeecSPeter Ujfalusi usleep_range(10000, 10500);
143210afeecSPeter Ujfalusi
144210afeecSPeter Ujfalusi /* enable negative charge pump */
145210afeecSPeter Ujfalusi ncpctl = TWL6040_NCPENA;
146210afeecSPeter Ujfalusi ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
147210afeecSPeter Ujfalusi if (ret)
148210afeecSPeter Ujfalusi goto ncp_err;
149210afeecSPeter Ujfalusi usleep_range(1000, 1500);
150210afeecSPeter Ujfalusi
151210afeecSPeter Ujfalusi /* enable low-side LDO */
152210afeecSPeter Ujfalusi ldoctl |= TWL6040_LSLDOENA;
153210afeecSPeter Ujfalusi ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
154210afeecSPeter Ujfalusi if (ret)
155210afeecSPeter Ujfalusi goto lsldo_err;
156210afeecSPeter Ujfalusi usleep_range(1000, 1500);
157210afeecSPeter Ujfalusi
158210afeecSPeter Ujfalusi /* enable low-power PLL */
159210afeecSPeter Ujfalusi lppllctl = TWL6040_LPLLENA;
160210afeecSPeter Ujfalusi ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
161210afeecSPeter Ujfalusi if (ret)
162210afeecSPeter Ujfalusi goto lppll_err;
163210afeecSPeter Ujfalusi usleep_range(5000, 5500);
164210afeecSPeter Ujfalusi
165210afeecSPeter Ujfalusi /* disable internal oscillator */
166210afeecSPeter Ujfalusi ldoctl &= ~TWL6040_OSCENA;
167210afeecSPeter Ujfalusi ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
168210afeecSPeter Ujfalusi if (ret)
169210afeecSPeter Ujfalusi goto osc_err;
170210afeecSPeter Ujfalusi
171210afeecSPeter Ujfalusi return 0;
172210afeecSPeter Ujfalusi
173210afeecSPeter Ujfalusi osc_err:
174210afeecSPeter Ujfalusi lppllctl &= ~TWL6040_LPLLENA;
175210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
176210afeecSPeter Ujfalusi lppll_err:
177210afeecSPeter Ujfalusi ldoctl &= ~TWL6040_LSLDOENA;
178210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
179210afeecSPeter Ujfalusi lsldo_err:
180210afeecSPeter Ujfalusi ncpctl &= ~TWL6040_NCPENA;
181210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
182210afeecSPeter Ujfalusi ncp_err:
183210afeecSPeter Ujfalusi ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
184210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
185210afeecSPeter Ujfalusi
186210afeecSPeter Ujfalusi dev_err(twl6040->dev, "manual power-up failed\n");
187210afeecSPeter Ujfalusi return ret;
188210afeecSPeter Ujfalusi }
189210afeecSPeter Ujfalusi
190210afeecSPeter Ujfalusi /* twl6040 manual power-down sequence */
twl6040_power_down_manual(struct twl6040 * twl6040)191210afeecSPeter Ujfalusi static void twl6040_power_down_manual(struct twl6040 *twl6040)
192210afeecSPeter Ujfalusi {
193210afeecSPeter Ujfalusi u8 ncpctl, ldoctl, lppllctl;
194210afeecSPeter Ujfalusi
195210afeecSPeter Ujfalusi ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
196210afeecSPeter Ujfalusi ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
197210afeecSPeter Ujfalusi lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
198210afeecSPeter Ujfalusi
199210afeecSPeter Ujfalusi /* enable internal oscillator */
200210afeecSPeter Ujfalusi ldoctl |= TWL6040_OSCENA;
201210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
202210afeecSPeter Ujfalusi usleep_range(1000, 1500);
203210afeecSPeter Ujfalusi
204210afeecSPeter Ujfalusi /* disable low-power PLL */
205210afeecSPeter Ujfalusi lppllctl &= ~TWL6040_LPLLENA;
206210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
207210afeecSPeter Ujfalusi
208210afeecSPeter Ujfalusi /* disable low-side LDO */
209210afeecSPeter Ujfalusi ldoctl &= ~TWL6040_LSLDOENA;
210210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
211210afeecSPeter Ujfalusi
212210afeecSPeter Ujfalusi /* disable negative charge pump */
213210afeecSPeter Ujfalusi ncpctl &= ~TWL6040_NCPENA;
214210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
215210afeecSPeter Ujfalusi
216210afeecSPeter Ujfalusi /* disable high-side LDO, reference system and internal oscillator */
217210afeecSPeter Ujfalusi ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
218210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
219210afeecSPeter Ujfalusi }
220210afeecSPeter Ujfalusi
twl6040_readyint_handler(int irq,void * data)221210afeecSPeter Ujfalusi static irqreturn_t twl6040_readyint_handler(int irq, void *data)
222210afeecSPeter Ujfalusi {
223210afeecSPeter Ujfalusi struct twl6040 *twl6040 = data;
224210afeecSPeter Ujfalusi
225210afeecSPeter Ujfalusi complete(&twl6040->ready);
226210afeecSPeter Ujfalusi
227210afeecSPeter Ujfalusi return IRQ_HANDLED;
228210afeecSPeter Ujfalusi }
229210afeecSPeter Ujfalusi
twl6040_thint_handler(int irq,void * data)230210afeecSPeter Ujfalusi static irqreturn_t twl6040_thint_handler(int irq, void *data)
231210afeecSPeter Ujfalusi {
232210afeecSPeter Ujfalusi struct twl6040 *twl6040 = data;
233210afeecSPeter Ujfalusi u8 status;
234210afeecSPeter Ujfalusi
235210afeecSPeter Ujfalusi status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
236210afeecSPeter Ujfalusi if (status & TWL6040_TSHUTDET) {
237210afeecSPeter Ujfalusi dev_warn(twl6040->dev, "Thermal shutdown, powering-off");
238210afeecSPeter Ujfalusi twl6040_power(twl6040, 0);
239210afeecSPeter Ujfalusi } else {
240210afeecSPeter Ujfalusi dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on");
241210afeecSPeter Ujfalusi twl6040_power(twl6040, 1);
242210afeecSPeter Ujfalusi }
243210afeecSPeter Ujfalusi
244210afeecSPeter Ujfalusi return IRQ_HANDLED;
245210afeecSPeter Ujfalusi }
246210afeecSPeter Ujfalusi
twl6040_power_up_automatic(struct twl6040 * twl6040)247210afeecSPeter Ujfalusi static int twl6040_power_up_automatic(struct twl6040 *twl6040)
248210afeecSPeter Ujfalusi {
249210afeecSPeter Ujfalusi int time_left;
250210afeecSPeter Ujfalusi
2513c92699aSDmitry Torokhov gpiod_set_value_cansleep(twl6040->audpwron, 1);
252210afeecSPeter Ujfalusi
253210afeecSPeter Ujfalusi time_left = wait_for_completion_timeout(&twl6040->ready,
254210afeecSPeter Ujfalusi msecs_to_jiffies(144));
255210afeecSPeter Ujfalusi if (!time_left) {
256210afeecSPeter Ujfalusi u8 intid;
257210afeecSPeter Ujfalusi
258210afeecSPeter Ujfalusi dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
259210afeecSPeter Ujfalusi intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
260210afeecSPeter Ujfalusi if (!(intid & TWL6040_READYINT)) {
261210afeecSPeter Ujfalusi dev_err(twl6040->dev, "automatic power-up failed\n");
2623c92699aSDmitry Torokhov gpiod_set_value_cansleep(twl6040->audpwron, 0);
263210afeecSPeter Ujfalusi return -ETIMEDOUT;
264210afeecSPeter Ujfalusi }
265210afeecSPeter Ujfalusi }
266210afeecSPeter Ujfalusi
267210afeecSPeter Ujfalusi return 0;
268210afeecSPeter Ujfalusi }
269210afeecSPeter Ujfalusi
twl6040_power(struct twl6040 * twl6040,int on)270210afeecSPeter Ujfalusi int twl6040_power(struct twl6040 *twl6040, int on)
271210afeecSPeter Ujfalusi {
272210afeecSPeter Ujfalusi int ret = 0;
273210afeecSPeter Ujfalusi
274210afeecSPeter Ujfalusi mutex_lock(&twl6040->mutex);
275210afeecSPeter Ujfalusi
276210afeecSPeter Ujfalusi if (on) {
277210afeecSPeter Ujfalusi /* already powered-up */
278210afeecSPeter Ujfalusi if (twl6040->power_count++)
279210afeecSPeter Ujfalusi goto out;
280210afeecSPeter Ujfalusi
28132852bcaSJavier Martinez Canillas ret = clk_prepare_enable(twl6040->clk32k);
28232852bcaSJavier Martinez Canillas if (ret) {
28332852bcaSJavier Martinez Canillas twl6040->power_count = 0;
28432852bcaSJavier Martinez Canillas goto out;
28532852bcaSJavier Martinez Canillas }
28668bab866SPeter Ujfalusi
287c7f9129dSPeter Ujfalusi /* Allow writes to the chip */
288c7f9129dSPeter Ujfalusi regcache_cache_only(twl6040->regmap, false);
289c7f9129dSPeter Ujfalusi
2903c92699aSDmitry Torokhov if (twl6040->audpwron) {
291210afeecSPeter Ujfalusi /* use automatic power-up sequence */
292210afeecSPeter Ujfalusi ret = twl6040_power_up_automatic(twl6040);
293210afeecSPeter Ujfalusi if (ret) {
294d6441dc5SJavier Martinez Canillas clk_disable_unprepare(twl6040->clk32k);
295210afeecSPeter Ujfalusi twl6040->power_count = 0;
296210afeecSPeter Ujfalusi goto out;
297210afeecSPeter Ujfalusi }
298210afeecSPeter Ujfalusi } else {
299210afeecSPeter Ujfalusi /* use manual power-up sequence */
300210afeecSPeter Ujfalusi ret = twl6040_power_up_manual(twl6040);
301210afeecSPeter Ujfalusi if (ret) {
302d6441dc5SJavier Martinez Canillas clk_disable_unprepare(twl6040->clk32k);
303210afeecSPeter Ujfalusi twl6040->power_count = 0;
304210afeecSPeter Ujfalusi goto out;
305210afeecSPeter Ujfalusi }
306210afeecSPeter Ujfalusi }
307c7f9129dSPeter Ujfalusi
30848171d0eSTony Lindgren /*
30948171d0eSTony Lindgren * Register access can produce errors after power-up unless we
31048171d0eSTony Lindgren * wait at least 8ms based on measurements on duovero.
31148171d0eSTony Lindgren */
31248171d0eSTony Lindgren usleep_range(10000, 12000);
31348171d0eSTony Lindgren
314c7f9129dSPeter Ujfalusi /* Sync with the HW */
31548171d0eSTony Lindgren ret = regcache_sync(twl6040->regmap);
31648171d0eSTony Lindgren if (ret) {
31748171d0eSTony Lindgren dev_err(twl6040->dev, "Failed to sync with the HW: %i\n",
31848171d0eSTony Lindgren ret);
31948171d0eSTony Lindgren goto out;
32048171d0eSTony Lindgren }
321c7f9129dSPeter Ujfalusi
322210afeecSPeter Ujfalusi /* Default PLL configuration after power up */
323210afeecSPeter Ujfalusi twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
3240a58da1eSPeter Ujfalusi twl6040->sysclk_rate = 19200000;
325210afeecSPeter Ujfalusi } else {
326210afeecSPeter Ujfalusi /* already powered-down */
327210afeecSPeter Ujfalusi if (!twl6040->power_count) {
328210afeecSPeter Ujfalusi dev_err(twl6040->dev,
329210afeecSPeter Ujfalusi "device is already powered-off\n");
330210afeecSPeter Ujfalusi ret = -EPERM;
331210afeecSPeter Ujfalusi goto out;
332210afeecSPeter Ujfalusi }
333210afeecSPeter Ujfalusi
334210afeecSPeter Ujfalusi if (--twl6040->power_count)
335210afeecSPeter Ujfalusi goto out;
336210afeecSPeter Ujfalusi
3373c92699aSDmitry Torokhov if (twl6040->audpwron) {
338210afeecSPeter Ujfalusi /* use AUDPWRON line */
3393c92699aSDmitry Torokhov gpiod_set_value_cansleep(twl6040->audpwron, 0);
340210afeecSPeter Ujfalusi
341210afeecSPeter Ujfalusi /* power-down sequence latency */
342210afeecSPeter Ujfalusi usleep_range(500, 700);
343210afeecSPeter Ujfalusi } else {
344210afeecSPeter Ujfalusi /* use manual power-down sequence */
345210afeecSPeter Ujfalusi twl6040_power_down_manual(twl6040);
346210afeecSPeter Ujfalusi }
347c7f9129dSPeter Ujfalusi
348c7f9129dSPeter Ujfalusi /* Set regmap to cache only and mark it as dirty */
349c7f9129dSPeter Ujfalusi regcache_cache_only(twl6040->regmap, true);
350c7f9129dSPeter Ujfalusi regcache_mark_dirty(twl6040->regmap);
351c7f9129dSPeter Ujfalusi
3520a58da1eSPeter Ujfalusi twl6040->sysclk_rate = 0;
3530a58da1eSPeter Ujfalusi
3540a58da1eSPeter Ujfalusi if (twl6040->pll == TWL6040_SYSCLK_SEL_HPPLL) {
3550a58da1eSPeter Ujfalusi clk_disable_unprepare(twl6040->mclk);
3560a58da1eSPeter Ujfalusi twl6040->mclk_rate = 0;
3570a58da1eSPeter Ujfalusi }
35868bab866SPeter Ujfalusi
35968bab866SPeter Ujfalusi clk_disable_unprepare(twl6040->clk32k);
360210afeecSPeter Ujfalusi }
361210afeecSPeter Ujfalusi
362210afeecSPeter Ujfalusi out:
363210afeecSPeter Ujfalusi mutex_unlock(&twl6040->mutex);
364210afeecSPeter Ujfalusi return ret;
365210afeecSPeter Ujfalusi }
366210afeecSPeter Ujfalusi EXPORT_SYMBOL(twl6040_power);
367210afeecSPeter Ujfalusi
twl6040_set_pll(struct twl6040 * twl6040,int pll_id,unsigned int freq_in,unsigned int freq_out)368210afeecSPeter Ujfalusi int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
369210afeecSPeter Ujfalusi unsigned int freq_in, unsigned int freq_out)
370210afeecSPeter Ujfalusi {
371210afeecSPeter Ujfalusi u8 hppllctl, lppllctl;
372210afeecSPeter Ujfalusi int ret = 0;
373210afeecSPeter Ujfalusi
374210afeecSPeter Ujfalusi mutex_lock(&twl6040->mutex);
375210afeecSPeter Ujfalusi
376210afeecSPeter Ujfalusi hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
377210afeecSPeter Ujfalusi lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
378210afeecSPeter Ujfalusi
379210afeecSPeter Ujfalusi /* Force full reconfiguration when switching between PLL */
380210afeecSPeter Ujfalusi if (pll_id != twl6040->pll) {
3810a58da1eSPeter Ujfalusi twl6040->sysclk_rate = 0;
3820a58da1eSPeter Ujfalusi twl6040->mclk_rate = 0;
383210afeecSPeter Ujfalusi }
384210afeecSPeter Ujfalusi
385210afeecSPeter Ujfalusi switch (pll_id) {
386210afeecSPeter Ujfalusi case TWL6040_SYSCLK_SEL_LPPLL:
387210afeecSPeter Ujfalusi /* low-power PLL divider */
388210afeecSPeter Ujfalusi /* Change the sysclk configuration only if it has been canged */
3890a58da1eSPeter Ujfalusi if (twl6040->sysclk_rate != freq_out) {
390210afeecSPeter Ujfalusi switch (freq_out) {
391210afeecSPeter Ujfalusi case 17640000:
392210afeecSPeter Ujfalusi lppllctl |= TWL6040_LPLLFIN;
393210afeecSPeter Ujfalusi break;
394210afeecSPeter Ujfalusi case 19200000:
395210afeecSPeter Ujfalusi lppllctl &= ~TWL6040_LPLLFIN;
396210afeecSPeter Ujfalusi break;
397210afeecSPeter Ujfalusi default:
398210afeecSPeter Ujfalusi dev_err(twl6040->dev,
399210afeecSPeter Ujfalusi "freq_out %d not supported\n",
400210afeecSPeter Ujfalusi freq_out);
401210afeecSPeter Ujfalusi ret = -EINVAL;
402210afeecSPeter Ujfalusi goto pll_out;
403210afeecSPeter Ujfalusi }
404210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
405210afeecSPeter Ujfalusi lppllctl);
406210afeecSPeter Ujfalusi }
407210afeecSPeter Ujfalusi
408210afeecSPeter Ujfalusi /* The PLL in use has not been change, we can exit */
409210afeecSPeter Ujfalusi if (twl6040->pll == pll_id)
410210afeecSPeter Ujfalusi break;
411210afeecSPeter Ujfalusi
412210afeecSPeter Ujfalusi switch (freq_in) {
413210afeecSPeter Ujfalusi case 32768:
414210afeecSPeter Ujfalusi lppllctl |= TWL6040_LPLLENA;
415210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
416210afeecSPeter Ujfalusi lppllctl);
417210afeecSPeter Ujfalusi mdelay(5);
418210afeecSPeter Ujfalusi lppllctl &= ~TWL6040_HPLLSEL;
419210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
420210afeecSPeter Ujfalusi lppllctl);
421210afeecSPeter Ujfalusi hppllctl &= ~TWL6040_HPLLENA;
422210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
423210afeecSPeter Ujfalusi hppllctl);
424210afeecSPeter Ujfalusi break;
425210afeecSPeter Ujfalusi default:
426210afeecSPeter Ujfalusi dev_err(twl6040->dev,
427210afeecSPeter Ujfalusi "freq_in %d not supported\n", freq_in);
428210afeecSPeter Ujfalusi ret = -EINVAL;
429210afeecSPeter Ujfalusi goto pll_out;
430210afeecSPeter Ujfalusi }
4310a58da1eSPeter Ujfalusi
4320a58da1eSPeter Ujfalusi clk_disable_unprepare(twl6040->mclk);
433210afeecSPeter Ujfalusi break;
434210afeecSPeter Ujfalusi case TWL6040_SYSCLK_SEL_HPPLL:
435210afeecSPeter Ujfalusi /* high-performance PLL can provide only 19.2 MHz */
436210afeecSPeter Ujfalusi if (freq_out != 19200000) {
437210afeecSPeter Ujfalusi dev_err(twl6040->dev,
438210afeecSPeter Ujfalusi "freq_out %d not supported\n", freq_out);
439210afeecSPeter Ujfalusi ret = -EINVAL;
440210afeecSPeter Ujfalusi goto pll_out;
441210afeecSPeter Ujfalusi }
442210afeecSPeter Ujfalusi
4430a58da1eSPeter Ujfalusi if (twl6040->mclk_rate != freq_in) {
444210afeecSPeter Ujfalusi hppllctl &= ~TWL6040_MCLK_MSK;
445210afeecSPeter Ujfalusi
446210afeecSPeter Ujfalusi switch (freq_in) {
447210afeecSPeter Ujfalusi case 12000000:
448210afeecSPeter Ujfalusi /* PLL enabled, active mode */
449210afeecSPeter Ujfalusi hppllctl |= TWL6040_MCLK_12000KHZ |
450210afeecSPeter Ujfalusi TWL6040_HPLLENA;
451210afeecSPeter Ujfalusi break;
452210afeecSPeter Ujfalusi case 19200000:
453ac8320c4SPeter Ujfalusi /* PLL enabled, bypass mode */
454ac8320c4SPeter Ujfalusi hppllctl |= TWL6040_MCLK_19200KHZ |
455ac8320c4SPeter Ujfalusi TWL6040_HPLLBP | TWL6040_HPLLENA;
456210afeecSPeter Ujfalusi break;
457210afeecSPeter Ujfalusi case 26000000:
458210afeecSPeter Ujfalusi /* PLL enabled, active mode */
459210afeecSPeter Ujfalusi hppllctl |= TWL6040_MCLK_26000KHZ |
460210afeecSPeter Ujfalusi TWL6040_HPLLENA;
461210afeecSPeter Ujfalusi break;
462210afeecSPeter Ujfalusi case 38400000:
463ac8320c4SPeter Ujfalusi /* PLL enabled, bypass mode */
464210afeecSPeter Ujfalusi hppllctl |= TWL6040_MCLK_38400KHZ |
465ac8320c4SPeter Ujfalusi TWL6040_HPLLBP | TWL6040_HPLLENA;
466210afeecSPeter Ujfalusi break;
467210afeecSPeter Ujfalusi default:
468210afeecSPeter Ujfalusi dev_err(twl6040->dev,
469210afeecSPeter Ujfalusi "freq_in %d not supported\n", freq_in);
470210afeecSPeter Ujfalusi ret = -EINVAL;
471210afeecSPeter Ujfalusi goto pll_out;
472210afeecSPeter Ujfalusi }
473210afeecSPeter Ujfalusi
4740a58da1eSPeter Ujfalusi /* When switching to HPPLL, enable the mclk first */
4750a58da1eSPeter Ujfalusi if (pll_id != twl6040->pll)
4760a58da1eSPeter Ujfalusi clk_prepare_enable(twl6040->mclk);
477210afeecSPeter Ujfalusi /*
478210afeecSPeter Ujfalusi * enable clock slicer to ensure input waveform is
479210afeecSPeter Ujfalusi * square
480210afeecSPeter Ujfalusi */
481210afeecSPeter Ujfalusi hppllctl |= TWL6040_HPLLSQRENA;
482210afeecSPeter Ujfalusi
483210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
484210afeecSPeter Ujfalusi hppllctl);
485210afeecSPeter Ujfalusi usleep_range(500, 700);
486210afeecSPeter Ujfalusi lppllctl |= TWL6040_HPLLSEL;
487210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
488210afeecSPeter Ujfalusi lppllctl);
489210afeecSPeter Ujfalusi lppllctl &= ~TWL6040_LPLLENA;
490210afeecSPeter Ujfalusi twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
491210afeecSPeter Ujfalusi lppllctl);
4920a58da1eSPeter Ujfalusi
4930a58da1eSPeter Ujfalusi twl6040->mclk_rate = freq_in;
494210afeecSPeter Ujfalusi }
495210afeecSPeter Ujfalusi break;
496210afeecSPeter Ujfalusi default:
497210afeecSPeter Ujfalusi dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
498210afeecSPeter Ujfalusi ret = -EINVAL;
499210afeecSPeter Ujfalusi goto pll_out;
500210afeecSPeter Ujfalusi }
501210afeecSPeter Ujfalusi
5020a58da1eSPeter Ujfalusi twl6040->sysclk_rate = freq_out;
503210afeecSPeter Ujfalusi twl6040->pll = pll_id;
504210afeecSPeter Ujfalusi
505210afeecSPeter Ujfalusi pll_out:
506210afeecSPeter Ujfalusi mutex_unlock(&twl6040->mutex);
507210afeecSPeter Ujfalusi return ret;
508210afeecSPeter Ujfalusi }
509210afeecSPeter Ujfalusi EXPORT_SYMBOL(twl6040_set_pll);
510210afeecSPeter Ujfalusi
twl6040_get_pll(struct twl6040 * twl6040)511210afeecSPeter Ujfalusi int twl6040_get_pll(struct twl6040 *twl6040)
512210afeecSPeter Ujfalusi {
513210afeecSPeter Ujfalusi if (twl6040->power_count)
514210afeecSPeter Ujfalusi return twl6040->pll;
515210afeecSPeter Ujfalusi else
516210afeecSPeter Ujfalusi return -ENODEV;
517210afeecSPeter Ujfalusi }
518210afeecSPeter Ujfalusi EXPORT_SYMBOL(twl6040_get_pll);
519210afeecSPeter Ujfalusi
twl6040_get_sysclk(struct twl6040 * twl6040)520210afeecSPeter Ujfalusi unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
521210afeecSPeter Ujfalusi {
5220a58da1eSPeter Ujfalusi return twl6040->sysclk_rate;
523210afeecSPeter Ujfalusi }
524210afeecSPeter Ujfalusi EXPORT_SYMBOL(twl6040_get_sysclk);
525210afeecSPeter Ujfalusi
526210afeecSPeter Ujfalusi /* Get the combined status of the vibra control register */
twl6040_get_vibralr_status(struct twl6040 * twl6040)527210afeecSPeter Ujfalusi int twl6040_get_vibralr_status(struct twl6040 *twl6040)
528210afeecSPeter Ujfalusi {
529c6f39257SMark Brown unsigned int reg;
530c6f39257SMark Brown int ret;
531210afeecSPeter Ujfalusi u8 status;
532210afeecSPeter Ujfalusi
533c6f39257SMark Brown ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLL, ®);
534c6f39257SMark Brown if (ret != 0)
535c6f39257SMark Brown return ret;
536c6f39257SMark Brown status = reg;
537c6f39257SMark Brown
538c6f39257SMark Brown ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLR, ®);
539c6f39257SMark Brown if (ret != 0)
540c6f39257SMark Brown return ret;
541c6f39257SMark Brown status |= reg;
542c6f39257SMark Brown
543210afeecSPeter Ujfalusi status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
544210afeecSPeter Ujfalusi
545210afeecSPeter Ujfalusi return status;
546210afeecSPeter Ujfalusi }
547210afeecSPeter Ujfalusi EXPORT_SYMBOL(twl6040_get_vibralr_status);
548210afeecSPeter Ujfalusi
549210afeecSPeter Ujfalusi static struct resource twl6040_vibra_rsrc[] = {
550210afeecSPeter Ujfalusi {
551210afeecSPeter Ujfalusi .flags = IORESOURCE_IRQ,
552210afeecSPeter Ujfalusi },
553210afeecSPeter Ujfalusi };
554210afeecSPeter Ujfalusi
555210afeecSPeter Ujfalusi static struct resource twl6040_codec_rsrc[] = {
556210afeecSPeter Ujfalusi {
557210afeecSPeter Ujfalusi .flags = IORESOURCE_IRQ,
558210afeecSPeter Ujfalusi },
559210afeecSPeter Ujfalusi };
560210afeecSPeter Ujfalusi
twl6040_readable_reg(struct device * dev,unsigned int reg)561210afeecSPeter Ujfalusi static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
562210afeecSPeter Ujfalusi {
563210afeecSPeter Ujfalusi /* Register 0 is not readable */
564210afeecSPeter Ujfalusi if (!reg)
565210afeecSPeter Ujfalusi return false;
566210afeecSPeter Ujfalusi return true;
567210afeecSPeter Ujfalusi }
568210afeecSPeter Ujfalusi
twl6040_volatile_reg(struct device * dev,unsigned int reg)569c6f39257SMark Brown static bool twl6040_volatile_reg(struct device *dev, unsigned int reg)
570c6f39257SMark Brown {
571c6f39257SMark Brown switch (reg) {
572c7f9129dSPeter Ujfalusi case TWL6040_REG_ASICID:
573c7f9129dSPeter Ujfalusi case TWL6040_REG_ASICREV:
574c7f9129dSPeter Ujfalusi case TWL6040_REG_INTID:
575c7f9129dSPeter Ujfalusi case TWL6040_REG_LPPLLCTL:
576c7f9129dSPeter Ujfalusi case TWL6040_REG_HPPLLCTL:
577c7f9129dSPeter Ujfalusi case TWL6040_REG_STATUS:
578c7f9129dSPeter Ujfalusi return true;
579c7f9129dSPeter Ujfalusi default:
580c7f9129dSPeter Ujfalusi return false;
581c7f9129dSPeter Ujfalusi }
582c7f9129dSPeter Ujfalusi }
583c7f9129dSPeter Ujfalusi
twl6040_writeable_reg(struct device * dev,unsigned int reg)584c7f9129dSPeter Ujfalusi static bool twl6040_writeable_reg(struct device *dev, unsigned int reg)
585c7f9129dSPeter Ujfalusi {
586c7f9129dSPeter Ujfalusi switch (reg) {
587c7f9129dSPeter Ujfalusi case TWL6040_REG_ASICID:
588c7f9129dSPeter Ujfalusi case TWL6040_REG_ASICREV:
589c7f9129dSPeter Ujfalusi case TWL6040_REG_STATUS:
590c6f39257SMark Brown return false;
591c6f39257SMark Brown default:
592c6f39257SMark Brown return true;
593c6f39257SMark Brown }
594c6f39257SMark Brown }
595c6f39257SMark Brown
596de1e23f8SKrzysztof Kozlowski static const struct regmap_config twl6040_regmap_config = {
597210afeecSPeter Ujfalusi .reg_bits = 8,
598210afeecSPeter Ujfalusi .val_bits = 8,
599c7f9129dSPeter Ujfalusi
600c7f9129dSPeter Ujfalusi .reg_defaults = twl6040_defaults,
601c7f9129dSPeter Ujfalusi .num_reg_defaults = ARRAY_SIZE(twl6040_defaults),
602c7f9129dSPeter Ujfalusi
603210afeecSPeter Ujfalusi .max_register = TWL6040_REG_STATUS, /* 0x2e */
604210afeecSPeter Ujfalusi
605210afeecSPeter Ujfalusi .readable_reg = twl6040_readable_reg,
606c6f39257SMark Brown .volatile_reg = twl6040_volatile_reg,
607c7f9129dSPeter Ujfalusi .writeable_reg = twl6040_writeable_reg,
608c6f39257SMark Brown
609d99df657SMark Brown .cache_type = REGCACHE_MAPLE,
6101c96a2f6SDavid Frey .use_single_read = true,
6111c96a2f6SDavid Frey .use_single_write = true,
612210afeecSPeter Ujfalusi };
613210afeecSPeter Ujfalusi
614210afeecSPeter Ujfalusi static const struct regmap_irq twl6040_irqs[] = {
615210afeecSPeter Ujfalusi { .reg_offset = 0, .mask = TWL6040_THINT, },
616210afeecSPeter Ujfalusi { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
617210afeecSPeter Ujfalusi { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
618210afeecSPeter Ujfalusi { .reg_offset = 0, .mask = TWL6040_HFINT, },
619210afeecSPeter Ujfalusi { .reg_offset = 0, .mask = TWL6040_VIBINT, },
620210afeecSPeter Ujfalusi { .reg_offset = 0, .mask = TWL6040_READYINT, },
621210afeecSPeter Ujfalusi };
622210afeecSPeter Ujfalusi
623210afeecSPeter Ujfalusi static struct regmap_irq_chip twl6040_irq_chip = {
624210afeecSPeter Ujfalusi .name = "twl6040",
625210afeecSPeter Ujfalusi .irqs = twl6040_irqs,
626210afeecSPeter Ujfalusi .num_irqs = ARRAY_SIZE(twl6040_irqs),
627210afeecSPeter Ujfalusi
628210afeecSPeter Ujfalusi .num_regs = 1,
629210afeecSPeter Ujfalusi .status_base = TWL6040_REG_INTID,
630210afeecSPeter Ujfalusi .mask_base = TWL6040_REG_INTMR,
631210afeecSPeter Ujfalusi };
632210afeecSPeter Ujfalusi
twl6040_probe(struct i2c_client * client)633d85213beSUwe Kleine-König static int twl6040_probe(struct i2c_client *client)
634210afeecSPeter Ujfalusi {
635210afeecSPeter Ujfalusi struct device_node *node = client->dev.of_node;
636210afeecSPeter Ujfalusi struct twl6040 *twl6040;
637210afeecSPeter Ujfalusi struct mfd_cell *cell = NULL;
638210afeecSPeter Ujfalusi int irq, ret, children = 0;
639210afeecSPeter Ujfalusi
640df04b624SPeter Ujfalusi if (!node) {
641df04b624SPeter Ujfalusi dev_err(&client->dev, "of node is missing\n");
642210afeecSPeter Ujfalusi return -EINVAL;
643210afeecSPeter Ujfalusi }
644210afeecSPeter Ujfalusi
645210afeecSPeter Ujfalusi /* In order to operate correctly we need valid interrupt config */
646210afeecSPeter Ujfalusi if (!client->irq) {
647210afeecSPeter Ujfalusi dev_err(&client->dev, "Invalid IRQ configuration\n");
648210afeecSPeter Ujfalusi return -EINVAL;
649210afeecSPeter Ujfalusi }
650210afeecSPeter Ujfalusi
651210afeecSPeter Ujfalusi twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
652210afeecSPeter Ujfalusi GFP_KERNEL);
653ecc8fa1cSPeter Ujfalusi if (!twl6040)
654ecc8fa1cSPeter Ujfalusi return -ENOMEM;
655210afeecSPeter Ujfalusi
656210afeecSPeter Ujfalusi twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
657ecc8fa1cSPeter Ujfalusi if (IS_ERR(twl6040->regmap))
658ecc8fa1cSPeter Ujfalusi return PTR_ERR(twl6040->regmap);
659210afeecSPeter Ujfalusi
660210afeecSPeter Ujfalusi i2c_set_clientdata(client, twl6040);
661210afeecSPeter Ujfalusi
66268bab866SPeter Ujfalusi twl6040->clk32k = devm_clk_get(&client->dev, "clk32k");
66368bab866SPeter Ujfalusi if (IS_ERR(twl6040->clk32k)) {
66475c08f17STony Lindgren if (PTR_ERR(twl6040->clk32k) == -EPROBE_DEFER)
66575c08f17STony Lindgren return -EPROBE_DEFER;
6660a58da1eSPeter Ujfalusi dev_dbg(&client->dev, "clk32k is not handled\n");
66768bab866SPeter Ujfalusi twl6040->clk32k = NULL;
66868bab866SPeter Ujfalusi }
66968bab866SPeter Ujfalusi
6700a58da1eSPeter Ujfalusi twl6040->mclk = devm_clk_get(&client->dev, "mclk");
6710a58da1eSPeter Ujfalusi if (IS_ERR(twl6040->mclk)) {
6720a58da1eSPeter Ujfalusi if (PTR_ERR(twl6040->mclk) == -EPROBE_DEFER)
6730a58da1eSPeter Ujfalusi return -EPROBE_DEFER;
6740a58da1eSPeter Ujfalusi dev_dbg(&client->dev, "mclk is not handled\n");
6750a58da1eSPeter Ujfalusi twl6040->mclk = NULL;
6760a58da1eSPeter Ujfalusi }
6770a58da1eSPeter Ujfalusi
678210afeecSPeter Ujfalusi twl6040->supplies[0].supply = "vio";
679210afeecSPeter Ujfalusi twl6040->supplies[1].supply = "v2v1";
680990810b0SJingoo Han ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
681210afeecSPeter Ujfalusi twl6040->supplies);
682210afeecSPeter Ujfalusi if (ret != 0) {
683210afeecSPeter Ujfalusi dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
684501d609aSWolfram Sang return ret;
685210afeecSPeter Ujfalusi }
686210afeecSPeter Ujfalusi
687210afeecSPeter Ujfalusi ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
688210afeecSPeter Ujfalusi if (ret != 0) {
689210afeecSPeter Ujfalusi dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
690501d609aSWolfram Sang return ret;
691210afeecSPeter Ujfalusi }
692210afeecSPeter Ujfalusi
693210afeecSPeter Ujfalusi twl6040->dev = &client->dev;
694210afeecSPeter Ujfalusi twl6040->irq = client->irq;
695210afeecSPeter Ujfalusi
696210afeecSPeter Ujfalusi mutex_init(&twl6040->mutex);
697210afeecSPeter Ujfalusi init_completion(&twl6040->ready);
698210afeecSPeter Ujfalusi
699006cea3aSPeter Ujfalusi regmap_register_patch(twl6040->regmap, twl6040_patch,
700006cea3aSPeter Ujfalusi ARRAY_SIZE(twl6040_patch));
701006cea3aSPeter Ujfalusi
702210afeecSPeter Ujfalusi twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
70389d68998SFlorian Vaussard if (twl6040->rev < 0) {
70489d68998SFlorian Vaussard dev_err(&client->dev, "Failed to read revision register: %d\n",
70589d68998SFlorian Vaussard twl6040->rev);
706f2b86781SJulia Lawall ret = twl6040->rev;
70789d68998SFlorian Vaussard goto gpio_err;
70889d68998SFlorian Vaussard }
709210afeecSPeter Ujfalusi
710210afeecSPeter Ujfalusi /* ERRATA: Automatic power-up is not possible in ES1.0 */
7113c92699aSDmitry Torokhov if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) {
7123c92699aSDmitry Torokhov twl6040->audpwron = devm_gpiod_get_optional(&client->dev,
7133c92699aSDmitry Torokhov "ti,audpwron",
7143c92699aSDmitry Torokhov GPIOD_OUT_LOW);
7153c92699aSDmitry Torokhov ret = PTR_ERR_OR_ZERO(twl6040->audpwron);
716210afeecSPeter Ujfalusi if (ret)
717210afeecSPeter Ujfalusi goto gpio_err;
71802d02728SPeter Ujfalusi
7193c92699aSDmitry Torokhov gpiod_set_consumer_name(twl6040->audpwron, "audpwron");
7203c92699aSDmitry Torokhov
72102d02728SPeter Ujfalusi /* Clear any pending interrupt */
72202d02728SPeter Ujfalusi twl6040_reg_read(twl6040, TWL6040_REG_INTID);
723210afeecSPeter Ujfalusi }
724210afeecSPeter Ujfalusi
72537aefe9fSPeter Ujfalusi ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT,
72637aefe9fSPeter Ujfalusi 0, &twl6040_irq_chip, &twl6040->irq_data);
727210afeecSPeter Ujfalusi if (ret < 0)
728990810b0SJingoo Han goto gpio_err;
729210afeecSPeter Ujfalusi
730210afeecSPeter Ujfalusi twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
731210afeecSPeter Ujfalusi TWL6040_IRQ_READY);
732210afeecSPeter Ujfalusi twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
733210afeecSPeter Ujfalusi TWL6040_IRQ_TH);
734210afeecSPeter Ujfalusi
735990810b0SJingoo Han ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
736210afeecSPeter Ujfalusi twl6040_readyint_handler, IRQF_ONESHOT,
737210afeecSPeter Ujfalusi "twl6040_irq_ready", twl6040);
738210afeecSPeter Ujfalusi if (ret) {
739210afeecSPeter Ujfalusi dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
740210afeecSPeter Ujfalusi goto readyirq_err;
741210afeecSPeter Ujfalusi }
742210afeecSPeter Ujfalusi
743990810b0SJingoo Han ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
744210afeecSPeter Ujfalusi twl6040_thint_handler, IRQF_ONESHOT,
745210afeecSPeter Ujfalusi "twl6040_irq_th", twl6040);
746210afeecSPeter Ujfalusi if (ret) {
747210afeecSPeter Ujfalusi dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
748fc5ee96fSWei Yongjun goto readyirq_err;
749210afeecSPeter Ujfalusi }
750210afeecSPeter Ujfalusi
751210afeecSPeter Ujfalusi /*
752210afeecSPeter Ujfalusi * The main functionality of twl6040 to provide audio on OMAP4+ systems.
753210afeecSPeter Ujfalusi * We can add the ASoC codec child whenever this driver has been loaded.
754210afeecSPeter Ujfalusi */
755210afeecSPeter Ujfalusi irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
756210afeecSPeter Ujfalusi cell = &twl6040->cells[children];
757210afeecSPeter Ujfalusi cell->name = "twl6040-codec";
758210afeecSPeter Ujfalusi twl6040_codec_rsrc[0].start = irq;
759210afeecSPeter Ujfalusi twl6040_codec_rsrc[0].end = irq;
760210afeecSPeter Ujfalusi cell->resources = twl6040_codec_rsrc;
761210afeecSPeter Ujfalusi cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
762210afeecSPeter Ujfalusi children++;
763210afeecSPeter Ujfalusi
764df04b624SPeter Ujfalusi /* Vibra input driver support */
765df04b624SPeter Ujfalusi if (twl6040_has_vibra(node)) {
766210afeecSPeter Ujfalusi irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
767210afeecSPeter Ujfalusi
768210afeecSPeter Ujfalusi cell = &twl6040->cells[children];
769210afeecSPeter Ujfalusi cell->name = "twl6040-vibra";
770210afeecSPeter Ujfalusi twl6040_vibra_rsrc[0].start = irq;
771210afeecSPeter Ujfalusi twl6040_vibra_rsrc[0].end = irq;
772210afeecSPeter Ujfalusi cell->resources = twl6040_vibra_rsrc;
773210afeecSPeter Ujfalusi cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
774210afeecSPeter Ujfalusi children++;
775210afeecSPeter Ujfalusi }
776210afeecSPeter Ujfalusi
777df04b624SPeter Ujfalusi /* GPO support */
778210afeecSPeter Ujfalusi cell = &twl6040->cells[children];
779210afeecSPeter Ujfalusi cell->name = "twl6040-gpo";
780210afeecSPeter Ujfalusi children++;
781210afeecSPeter Ujfalusi
7820133d323SPeter Ujfalusi /* PDM clock support */
7830133d323SPeter Ujfalusi cell = &twl6040->cells[children];
7840133d323SPeter Ujfalusi cell->name = "twl6040-pdmclk";
7850133d323SPeter Ujfalusi children++;
7860133d323SPeter Ujfalusi
787c7f9129dSPeter Ujfalusi /* The chip is powered down so mark regmap to cache only and dirty */
788c7f9129dSPeter Ujfalusi regcache_cache_only(twl6040->regmap, true);
789c7f9129dSPeter Ujfalusi regcache_mark_dirty(twl6040->regmap);
790c7f9129dSPeter Ujfalusi
791210afeecSPeter Ujfalusi ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
792210afeecSPeter Ujfalusi NULL, 0, NULL);
793210afeecSPeter Ujfalusi if (ret)
794fc5ee96fSWei Yongjun goto readyirq_err;
795210afeecSPeter Ujfalusi
796210afeecSPeter Ujfalusi return 0;
797210afeecSPeter Ujfalusi
798210afeecSPeter Ujfalusi readyirq_err:
799210afeecSPeter Ujfalusi regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
800210afeecSPeter Ujfalusi gpio_err:
801210afeecSPeter Ujfalusi regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
802210afeecSPeter Ujfalusi return ret;
803210afeecSPeter Ujfalusi }
804210afeecSPeter Ujfalusi
twl6040_remove(struct i2c_client * client)805ed5c2f5fSUwe Kleine-König static void twl6040_remove(struct i2c_client *client)
806210afeecSPeter Ujfalusi {
807210afeecSPeter Ujfalusi struct twl6040 *twl6040 = i2c_get_clientdata(client);
808210afeecSPeter Ujfalusi
809210afeecSPeter Ujfalusi if (twl6040->power_count)
810210afeecSPeter Ujfalusi twl6040_power(twl6040, 0);
811210afeecSPeter Ujfalusi
812210afeecSPeter Ujfalusi regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
813210afeecSPeter Ujfalusi
814210afeecSPeter Ujfalusi mfd_remove_devices(&client->dev);
815210afeecSPeter Ujfalusi
816210afeecSPeter Ujfalusi regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
817210afeecSPeter Ujfalusi }
818210afeecSPeter Ujfalusi
819210afeecSPeter Ujfalusi static const struct i2c_device_id twl6040_i2c_id[] = {
820210afeecSPeter Ujfalusi { "twl6040", 0, },
821210afeecSPeter Ujfalusi { "twl6041", 0, },
822210afeecSPeter Ujfalusi { },
823210afeecSPeter Ujfalusi };
824210afeecSPeter Ujfalusi MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
825210afeecSPeter Ujfalusi
826210afeecSPeter Ujfalusi static struct i2c_driver twl6040_driver = {
827210afeecSPeter Ujfalusi .driver = {
828210afeecSPeter Ujfalusi .name = "twl6040",
829210afeecSPeter Ujfalusi },
830*9816d859SUwe Kleine-König .probe = twl6040_probe,
831612b95cdSGreg Kroah-Hartman .remove = twl6040_remove,
832210afeecSPeter Ujfalusi .id_table = twl6040_i2c_id,
833210afeecSPeter Ujfalusi };
834210afeecSPeter Ujfalusi
835210afeecSPeter Ujfalusi module_i2c_driver(twl6040_driver);
836210afeecSPeter Ujfalusi
837210afeecSPeter Ujfalusi MODULE_DESCRIPTION("TWL6040 MFD");
838210afeecSPeter Ujfalusi MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
839210afeecSPeter Ujfalusi MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
840