xref: /openbmc/linux/sound/soc/codecs/rt1308-sdw.c (revision 5efb40b335a70010999311187be4517f5f114009)
1a87a6653SShuming Fan // SPDX-License-Identifier: GPL-2.0
2a87a6653SShuming Fan //
3a87a6653SShuming Fan // rt1308-sdw.c -- rt1308 ALSA SoC audio driver
4a87a6653SShuming Fan //
5a87a6653SShuming Fan // Copyright(c) 2019 Realtek Semiconductor Corp.
6a87a6653SShuming Fan //
7a87a6653SShuming Fan //
8a87a6653SShuming Fan #include <linux/delay.h>
9a87a6653SShuming Fan #include <linux/device.h>
10a87a6653SShuming Fan #include <linux/pm_runtime.h>
11a87a6653SShuming Fan #include <linux/mod_devicetable.h>
12a87a6653SShuming Fan #include <linux/soundwire/sdw.h>
13a87a6653SShuming Fan #include <linux/soundwire/sdw_type.h>
14a87a6653SShuming Fan #include <linux/soundwire/sdw_registers.h>
15a87a6653SShuming Fan #include <linux/module.h>
16a87a6653SShuming Fan #include <linux/regmap.h>
17a87a6653SShuming Fan #include <sound/core.h>
18a87a6653SShuming Fan #include <sound/pcm.h>
19a87a6653SShuming Fan #include <sound/pcm_params.h>
20a87a6653SShuming Fan #include <sound/soc.h>
21a87a6653SShuming Fan #include <sound/soc-dapm.h>
22a87a6653SShuming Fan #include <sound/initval.h>
23a87a6653SShuming Fan 
24a87a6653SShuming Fan #include "rt1308.h"
25a87a6653SShuming Fan #include "rt1308-sdw.h"
26a87a6653SShuming Fan 
27a87a6653SShuming Fan static bool rt1308_readable_register(struct device *dev, unsigned int reg)
28a87a6653SShuming Fan {
29a87a6653SShuming Fan 	switch (reg) {
30a87a6653SShuming Fan 	case 0x00e0:
31a87a6653SShuming Fan 	case 0x00f0:
32a87a6653SShuming Fan 	case 0x2f01 ... 0x2f07:
33a87a6653SShuming Fan 	case 0x3000 ... 0x3001:
34a87a6653SShuming Fan 	case 0x3004 ... 0x3005:
35a87a6653SShuming Fan 	case 0x3008:
36a87a6653SShuming Fan 	case 0x300a:
37a87a6653SShuming Fan 	case 0xc000 ... 0xcff3:
38a87a6653SShuming Fan 		return true;
39a87a6653SShuming Fan 	default:
40a87a6653SShuming Fan 		return false;
41a87a6653SShuming Fan 	}
42a87a6653SShuming Fan }
43a87a6653SShuming Fan 
44a87a6653SShuming Fan static bool rt1308_volatile_register(struct device *dev, unsigned int reg)
45a87a6653SShuming Fan {
46a87a6653SShuming Fan 	switch (reg) {
47a87a6653SShuming Fan 	case 0x2f01 ... 0x2f07:
48a87a6653SShuming Fan 	case 0x3000 ... 0x3001:
49a87a6653SShuming Fan 	case 0x3004 ... 0x3005:
50a87a6653SShuming Fan 	case 0x3008:
51a87a6653SShuming Fan 	case 0x300a:
52a87a6653SShuming Fan 	case 0xc000:
5300aaf8bfSShuming Fan 	case 0xc710:
54abed17fdSShuming Fan 	case 0xc860 ... 0xc863:
55abed17fdSShuming Fan 	case 0xc870 ... 0xc873:
56a87a6653SShuming Fan 		return true;
57a87a6653SShuming Fan 	default:
58a87a6653SShuming Fan 		return false;
59a87a6653SShuming Fan 	}
60a87a6653SShuming Fan }
61a87a6653SShuming Fan 
62a87a6653SShuming Fan static const struct regmap_config rt1308_sdw_regmap = {
63a87a6653SShuming Fan 	.reg_bits = 32,
64a87a6653SShuming Fan 	.val_bits = 8,
65a87a6653SShuming Fan 	.readable_reg = rt1308_readable_register,
66a87a6653SShuming Fan 	.volatile_reg = rt1308_volatile_register,
67a87a6653SShuming Fan 	.max_register = 0xcfff,
68a87a6653SShuming Fan 	.reg_defaults = rt1308_reg_defaults,
69a87a6653SShuming Fan 	.num_reg_defaults = ARRAY_SIZE(rt1308_reg_defaults),
70a87a6653SShuming Fan 	.cache_type = REGCACHE_RBTREE,
71a87a6653SShuming Fan 	.use_single_read = true,
72a87a6653SShuming Fan 	.use_single_write = true,
73a87a6653SShuming Fan };
74a87a6653SShuming Fan 
75a87a6653SShuming Fan /* Bus clock frequency */
76a87a6653SShuming Fan #define RT1308_CLK_FREQ_9600000HZ 9600000
77a87a6653SShuming Fan #define RT1308_CLK_FREQ_12000000HZ 12000000
78a87a6653SShuming Fan #define RT1308_CLK_FREQ_6000000HZ 6000000
79a87a6653SShuming Fan #define RT1308_CLK_FREQ_4800000HZ 4800000
80a87a6653SShuming Fan #define RT1308_CLK_FREQ_2400000HZ 2400000
81a87a6653SShuming Fan #define RT1308_CLK_FREQ_12288000HZ 12288000
82a87a6653SShuming Fan 
83a87a6653SShuming Fan static int rt1308_clock_config(struct device *dev)
84a87a6653SShuming Fan {
85a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
86a87a6653SShuming Fan 	unsigned int clk_freq, value;
87a87a6653SShuming Fan 
88a87a6653SShuming Fan 	clk_freq = (rt1308->params.curr_dr_freq >> 1);
89a87a6653SShuming Fan 
90a87a6653SShuming Fan 	switch (clk_freq) {
91a87a6653SShuming Fan 	case RT1308_CLK_FREQ_12000000HZ:
92a87a6653SShuming Fan 		value = 0x0;
93a87a6653SShuming Fan 		break;
94a87a6653SShuming Fan 	case RT1308_CLK_FREQ_6000000HZ:
95a87a6653SShuming Fan 		value = 0x1;
96a87a6653SShuming Fan 		break;
97a87a6653SShuming Fan 	case RT1308_CLK_FREQ_9600000HZ:
98a87a6653SShuming Fan 		value = 0x2;
99a87a6653SShuming Fan 		break;
100a87a6653SShuming Fan 	case RT1308_CLK_FREQ_4800000HZ:
101a87a6653SShuming Fan 		value = 0x3;
102a87a6653SShuming Fan 		break;
103a87a6653SShuming Fan 	case RT1308_CLK_FREQ_2400000HZ:
104a87a6653SShuming Fan 		value = 0x4;
105a87a6653SShuming Fan 		break;
106a87a6653SShuming Fan 	case RT1308_CLK_FREQ_12288000HZ:
107a87a6653SShuming Fan 		value = 0x5;
108a87a6653SShuming Fan 		break;
109a87a6653SShuming Fan 	default:
110a87a6653SShuming Fan 		return -EINVAL;
111a87a6653SShuming Fan 	}
112a87a6653SShuming Fan 
113a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xe0, value);
114a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xf0, value);
115a87a6653SShuming Fan 
116a87a6653SShuming Fan 	dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
117a87a6653SShuming Fan 
118a87a6653SShuming Fan 	return 0;
119a87a6653SShuming Fan }
120a87a6653SShuming Fan 
121a87a6653SShuming Fan static int rt1308_read_prop(struct sdw_slave *slave)
122a87a6653SShuming Fan {
123a87a6653SShuming Fan 	struct sdw_slave_prop *prop = &slave->prop;
124d0bbcb4eSPierre-Louis Bossart 	int nval, i;
125a87a6653SShuming Fan 	u32 bit;
126a87a6653SShuming Fan 	unsigned long addr;
127a87a6653SShuming Fan 	struct sdw_dpn_prop *dpn;
128a87a6653SShuming Fan 
1292acd30b9SPierre-Louis Bossart 	prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
13038edbfaeSPierre-Louis Bossart 	prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
1312acd30b9SPierre-Louis Bossart 
132a87a6653SShuming Fan 	prop->paging_support = true;
133a87a6653SShuming Fan 
134a87a6653SShuming Fan 	/* first we need to allocate memory for set bits in port lists */
135a87a6653SShuming Fan 	prop->source_ports = 0x00; /* BITMAP: 00010100 (not enable yet) */
136a87a6653SShuming Fan 	prop->sink_ports = 0x2; /* BITMAP:  00000010 */
137a87a6653SShuming Fan 
138a87a6653SShuming Fan 	/* for sink */
139a87a6653SShuming Fan 	nval = hweight32(prop->sink_ports);
140a87a6653SShuming Fan 	prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
141a87a6653SShuming Fan 						sizeof(*prop->sink_dpn_prop),
142a87a6653SShuming Fan 						GFP_KERNEL);
143a87a6653SShuming Fan 	if (!prop->sink_dpn_prop)
144a87a6653SShuming Fan 		return -ENOMEM;
145a87a6653SShuming Fan 
146a87a6653SShuming Fan 	i = 0;
147a87a6653SShuming Fan 	dpn = prop->sink_dpn_prop;
148a87a6653SShuming Fan 	addr = prop->sink_ports;
149a87a6653SShuming Fan 	for_each_set_bit(bit, &addr, 32) {
150a87a6653SShuming Fan 		dpn[i].num = bit;
151a87a6653SShuming Fan 		dpn[i].type = SDW_DPN_FULL;
152a87a6653SShuming Fan 		dpn[i].simple_ch_prep_sm = true;
153a87a6653SShuming Fan 		dpn[i].ch_prep_timeout = 10;
154a87a6653SShuming Fan 		i++;
155a87a6653SShuming Fan 	}
156a87a6653SShuming Fan 
157a87a6653SShuming Fan 	/* set the timeout values */
158a87a6653SShuming Fan 	prop->clk_stop_timeout = 20;
159a87a6653SShuming Fan 
160a87a6653SShuming Fan 	dev_dbg(&slave->dev, "%s\n", __func__);
161a87a6653SShuming Fan 
162a87a6653SShuming Fan 	return 0;
163a87a6653SShuming Fan }
164a87a6653SShuming Fan 
165abed17fdSShuming Fan static void rt1308_apply_calib_params(struct rt1308_sdw_priv *rt1308)
166abed17fdSShuming Fan {
167abed17fdSShuming Fan 	unsigned int efuse_m_btl_l, efuse_m_btl_r, tmp;
168abed17fdSShuming Fan 	unsigned int efuse_c_btl_l, efuse_c_btl_r;
169abed17fdSShuming Fan 
170abed17fdSShuming Fan 	/* read efuse to apply calibration parameters */
171abed17fdSShuming Fan 	regmap_write(rt1308->regmap, 0xc7f0, 0x04);
172abed17fdSShuming Fan 	regmap_write(rt1308->regmap, 0xc7f1, 0xfe);
173abed17fdSShuming Fan 	msleep(100);
174abed17fdSShuming Fan 	regmap_write(rt1308->regmap, 0xc7f0, 0x44);
175abed17fdSShuming Fan 	msleep(20);
176abed17fdSShuming Fan 	regmap_write(rt1308->regmap, 0xc240, 0x10);
177abed17fdSShuming Fan 
178abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc861, &tmp);
179abed17fdSShuming Fan 	efuse_m_btl_l = tmp;
180abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc860, &tmp);
181abed17fdSShuming Fan 	efuse_m_btl_l = efuse_m_btl_l | (tmp << 8);
182abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc863, &tmp);
183abed17fdSShuming Fan 	efuse_c_btl_l = tmp;
184abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc862, &tmp);
185abed17fdSShuming Fan 	efuse_c_btl_l = efuse_c_btl_l | (tmp << 8);
186abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc871, &tmp);
187abed17fdSShuming Fan 	efuse_m_btl_r = tmp;
188abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc870, &tmp);
189abed17fdSShuming Fan 	efuse_m_btl_r = efuse_m_btl_r | (tmp << 8);
190abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc873, &tmp);
191abed17fdSShuming Fan 	efuse_c_btl_r = tmp;
192abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc872, &tmp);
193abed17fdSShuming Fan 	efuse_c_btl_r = efuse_c_btl_r | (tmp << 8);
194abed17fdSShuming Fan 	dev_dbg(&rt1308->sdw_slave->dev, "%s m_btl_l=0x%x, m_btl_r=0x%x\n", __func__,
195abed17fdSShuming Fan 		efuse_m_btl_l, efuse_m_btl_r);
196abed17fdSShuming Fan 	dev_dbg(&rt1308->sdw_slave->dev, "%s c_btl_l=0x%x, c_btl_r=0x%x\n", __func__,
197abed17fdSShuming Fan 		efuse_c_btl_l, efuse_c_btl_r);
198abed17fdSShuming Fan }
199abed17fdSShuming Fan 
200*5efb40b3SShuming Fan static void rt1308_apply_bq_params(struct rt1308_sdw_priv *rt1308)
201*5efb40b3SShuming Fan {
202*5efb40b3SShuming Fan 	unsigned int i, reg, data;
203*5efb40b3SShuming Fan 
204*5efb40b3SShuming Fan 	for (i = 0; i < rt1308->bq_params_cnt; i += 3) {
205*5efb40b3SShuming Fan 		reg = rt1308->bq_params[i] | (rt1308->bq_params[i + 1] << 8);
206*5efb40b3SShuming Fan 		data = rt1308->bq_params[i + 2];
207*5efb40b3SShuming Fan 		regmap_write(rt1308->regmap, reg, data);
208*5efb40b3SShuming Fan 	}
209*5efb40b3SShuming Fan }
210*5efb40b3SShuming Fan 
211a87a6653SShuming Fan static int rt1308_io_init(struct device *dev, struct sdw_slave *slave)
212a87a6653SShuming Fan {
213a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
214a87a6653SShuming Fan 	int ret = 0;
21500aaf8bfSShuming Fan 	unsigned int tmp;
216a87a6653SShuming Fan 
217a87a6653SShuming Fan 	if (rt1308->hw_init)
218a87a6653SShuming Fan 		return 0;
219a87a6653SShuming Fan 
220a87a6653SShuming Fan 	if (rt1308->first_hw_init) {
221a87a6653SShuming Fan 		regcache_cache_only(rt1308->regmap, false);
222a87a6653SShuming Fan 		regcache_cache_bypass(rt1308->regmap, true);
223a87a6653SShuming Fan 	}
224a87a6653SShuming Fan 
225a87a6653SShuming Fan 	/*
226a87a6653SShuming Fan 	 * PM runtime is only enabled when a Slave reports as Attached
227a87a6653SShuming Fan 	 */
228a87a6653SShuming Fan 	if (!rt1308->first_hw_init) {
229a87a6653SShuming Fan 		/* set autosuspend parameters */
230a87a6653SShuming Fan 		pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
231a87a6653SShuming Fan 		pm_runtime_use_autosuspend(&slave->dev);
232a87a6653SShuming Fan 
233a87a6653SShuming Fan 		/* update count of parent 'active' children */
234a87a6653SShuming Fan 		pm_runtime_set_active(&slave->dev);
235a87a6653SShuming Fan 
236a87a6653SShuming Fan 		/* make sure the device does not suspend immediately */
237a87a6653SShuming Fan 		pm_runtime_mark_last_busy(&slave->dev);
238a87a6653SShuming Fan 
239a87a6653SShuming Fan 		pm_runtime_enable(&slave->dev);
240a87a6653SShuming Fan 	}
241a87a6653SShuming Fan 
242a87a6653SShuming Fan 	pm_runtime_get_noresume(&slave->dev);
243a87a6653SShuming Fan 
244a87a6653SShuming Fan 	/* sw reset */
245a87a6653SShuming Fan 	regmap_write(rt1308->regmap, RT1308_SDW_RESET, 0);
246a87a6653SShuming Fan 
24700aaf8bfSShuming Fan 	regmap_read(rt1308->regmap, 0xc710, &tmp);
24800aaf8bfSShuming Fan 	rt1308->hw_ver = tmp;
24900aaf8bfSShuming Fan 	dev_dbg(dev, "%s, hw_ver=0x%x\n", __func__, rt1308->hw_ver);
25000aaf8bfSShuming Fan 
251a87a6653SShuming Fan 	/* initial settings */
252a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc103, 0xc0);
253a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc030, 0x17);
254a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc031, 0x81);
255a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc032, 0x26);
256a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc040, 0x80);
257a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc041, 0x80);
258a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc042, 0x06);
259a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc052, 0x0a);
260a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc080, 0x0a);
261a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc060, 0x02);
262a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc061, 0x75);
263a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc062, 0x05);
264a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc171, 0x07);
265a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc173, 0x0d);
26600aaf8bfSShuming Fan 	if (rt1308->hw_ver == RT1308_VER_C) {
267a87a6653SShuming Fan 		regmap_write(rt1308->regmap, 0xc311, 0x7f);
26800aaf8bfSShuming Fan 		regmap_write(rt1308->regmap, 0xc300, 0x09);
26900aaf8bfSShuming Fan 	} else {
27000aaf8bfSShuming Fan 		regmap_write(rt1308->regmap, 0xc311, 0x4f);
27100aaf8bfSShuming Fan 		regmap_write(rt1308->regmap, 0xc300, 0x0b);
27200aaf8bfSShuming Fan 	}
27300aaf8bfSShuming Fan 	regmap_write(rt1308->regmap, 0xc900, 0x5a);
274a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc1a0, 0x84);
275a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc1a1, 0x01);
276a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc360, 0x78);
277a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc361, 0x87);
278a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc0a1, 0x71);
279a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc210, 0x00);
280a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc070, 0x00);
281a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc100, 0xd7);
282a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc101, 0xd7);
283a87a6653SShuming Fan 
284a87a6653SShuming Fan 	if (rt1308->first_hw_init) {
285a87a6653SShuming Fan 		regcache_cache_bypass(rt1308->regmap, false);
286a87a6653SShuming Fan 		regcache_mark_dirty(rt1308->regmap);
287a87a6653SShuming Fan 	} else
288a87a6653SShuming Fan 		rt1308->first_hw_init = true;
289a87a6653SShuming Fan 
290a87a6653SShuming Fan 	/* Mark Slave initialization complete */
291a87a6653SShuming Fan 	rt1308->hw_init = true;
292a87a6653SShuming Fan 
293a87a6653SShuming Fan 	pm_runtime_mark_last_busy(&slave->dev);
294a87a6653SShuming Fan 	pm_runtime_put_autosuspend(&slave->dev);
295a87a6653SShuming Fan 
296a87a6653SShuming Fan 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
297a87a6653SShuming Fan 
298a87a6653SShuming Fan 	return ret;
299a87a6653SShuming Fan }
300a87a6653SShuming Fan 
301a87a6653SShuming Fan static int rt1308_update_status(struct sdw_slave *slave,
302a87a6653SShuming Fan 					enum sdw_slave_status status)
303a87a6653SShuming Fan {
304a87a6653SShuming Fan 	struct  rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
305a87a6653SShuming Fan 
306a87a6653SShuming Fan 	/* Update the status */
307a87a6653SShuming Fan 	rt1308->status = status;
308a87a6653SShuming Fan 
309a87a6653SShuming Fan 	if (status == SDW_SLAVE_UNATTACHED)
310a87a6653SShuming Fan 		rt1308->hw_init = false;
311a87a6653SShuming Fan 
312a87a6653SShuming Fan 	/*
313a87a6653SShuming Fan 	 * Perform initialization only if slave status is present and
314a87a6653SShuming Fan 	 * hw_init flag is false
315a87a6653SShuming Fan 	 */
316a87a6653SShuming Fan 	if (rt1308->hw_init || rt1308->status != SDW_SLAVE_ATTACHED)
317a87a6653SShuming Fan 		return 0;
318a87a6653SShuming Fan 
319a87a6653SShuming Fan 	/* perform I/O transfers required for Slave initialization */
320a87a6653SShuming Fan 	return rt1308_io_init(&slave->dev, slave);
321a87a6653SShuming Fan }
322a87a6653SShuming Fan 
323a87a6653SShuming Fan static int rt1308_bus_config(struct sdw_slave *slave,
324a87a6653SShuming Fan 				struct sdw_bus_params *params)
325a87a6653SShuming Fan {
326a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
327a87a6653SShuming Fan 	int ret;
328a87a6653SShuming Fan 
329a87a6653SShuming Fan 	memcpy(&rt1308->params, params, sizeof(*params));
330a87a6653SShuming Fan 
331a87a6653SShuming Fan 	ret = rt1308_clock_config(&slave->dev);
332a87a6653SShuming Fan 	if (ret < 0)
333a87a6653SShuming Fan 		dev_err(&slave->dev, "Invalid clk config");
334a87a6653SShuming Fan 
335a87a6653SShuming Fan 	return ret;
336a87a6653SShuming Fan }
337a87a6653SShuming Fan 
338a87a6653SShuming Fan static int rt1308_interrupt_callback(struct sdw_slave *slave,
339a87a6653SShuming Fan 					struct sdw_slave_intr_status *status)
340a87a6653SShuming Fan {
341a87a6653SShuming Fan 	dev_dbg(&slave->dev,
342a87a6653SShuming Fan 		"%s control_port_stat=%x", __func__, status->control_port);
343a87a6653SShuming Fan 
344a87a6653SShuming Fan 	return 0;
345a87a6653SShuming Fan }
346a87a6653SShuming Fan 
347a87a6653SShuming Fan static int rt1308_classd_event(struct snd_soc_dapm_widget *w,
348a87a6653SShuming Fan 	struct snd_kcontrol *kcontrol, int event)
349a87a6653SShuming Fan {
350a87a6653SShuming Fan 	struct snd_soc_component *component =
351a87a6653SShuming Fan 		snd_soc_dapm_to_component(w->dapm);
352abed17fdSShuming Fan 	struct rt1308_sdw_priv *rt1308 =
353abed17fdSShuming Fan 		snd_soc_component_get_drvdata(component);
354a87a6653SShuming Fan 
355a87a6653SShuming Fan 	switch (event) {
356a87a6653SShuming Fan 	case SND_SOC_DAPM_POST_PMU:
357a87a6653SShuming Fan 		msleep(30);
358a87a6653SShuming Fan 		snd_soc_component_update_bits(component,
359a87a6653SShuming Fan 			RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
360a87a6653SShuming Fan 			0x3,	0x3);
361a87a6653SShuming Fan 		msleep(40);
362abed17fdSShuming Fan 		rt1308_apply_calib_params(rt1308);
363a87a6653SShuming Fan 		break;
364a87a6653SShuming Fan 	case SND_SOC_DAPM_PRE_PMD:
365a87a6653SShuming Fan 		snd_soc_component_update_bits(component,
366a87a6653SShuming Fan 			RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
367a87a6653SShuming Fan 			0x3, 0);
368a87a6653SShuming Fan 		usleep_range(150000, 200000);
369a87a6653SShuming Fan 		break;
370a87a6653SShuming Fan 
371a87a6653SShuming Fan 	default:
372a87a6653SShuming Fan 		break;
373a87a6653SShuming Fan 	}
374a87a6653SShuming Fan 
375a87a6653SShuming Fan 	return 0;
376a87a6653SShuming Fan }
377a87a6653SShuming Fan 
378a87a6653SShuming Fan static const char * const rt1308_rx_data_ch_select[] = {
379a87a6653SShuming Fan 	"LR",
380a87a6653SShuming Fan 	"LL",
381a87a6653SShuming Fan 	"RL",
382a87a6653SShuming Fan 	"RR",
383a87a6653SShuming Fan };
384a87a6653SShuming Fan 
385a87a6653SShuming Fan static SOC_ENUM_SINGLE_DECL(rt1308_rx_data_ch_enum,
386a87a6653SShuming Fan 	RT1308_SDW_OFFSET | (RT1308_DATA_PATH << 4), 0,
387a87a6653SShuming Fan 	rt1308_rx_data_ch_select);
388a87a6653SShuming Fan 
389a87a6653SShuming Fan static const struct snd_kcontrol_new rt1308_snd_controls[] = {
390a87a6653SShuming Fan 
391a87a6653SShuming Fan 	/* I2S Data Channel Selection */
392a87a6653SShuming Fan 	SOC_ENUM("RX Channel Select", rt1308_rx_data_ch_enum),
393a87a6653SShuming Fan };
394a87a6653SShuming Fan 
395a87a6653SShuming Fan static const struct snd_kcontrol_new rt1308_sto_dac_l =
396a87a6653SShuming Fan 	SOC_DAPM_SINGLE_AUTODISABLE("Switch",
397a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
398a87a6653SShuming Fan 		RT1308_DVOL_MUTE_L_EN_SFT, 1, 1);
399a87a6653SShuming Fan 
400a87a6653SShuming Fan static const struct snd_kcontrol_new rt1308_sto_dac_r =
401a87a6653SShuming Fan 	SOC_DAPM_SINGLE_AUTODISABLE("Switch",
402a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
403a87a6653SShuming Fan 		RT1308_DVOL_MUTE_R_EN_SFT, 1, 1);
404a87a6653SShuming Fan 
405a87a6653SShuming Fan static const struct snd_soc_dapm_widget rt1308_dapm_widgets[] = {
406a87a6653SShuming Fan 	/* Audio Interface */
407a87a6653SShuming Fan 	SND_SOC_DAPM_AIF_IN("AIF1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
408a87a6653SShuming Fan 
409a87a6653SShuming Fan 	/* Supply Widgets */
410a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MBIAS20U",
411a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	7, 0, NULL, 0),
412a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("ALDO",
413a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	6, 0, NULL, 0),
414a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DBG",
415a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	5, 0, NULL, 0),
416a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DACL",
417a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	4, 0, NULL, 0),
418a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("CLK25M",
419a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	2, 0, NULL, 0),
420a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("ADC_R",
421a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	1, 0, NULL, 0),
422a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("ADC_L",
423a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	0, 0, NULL, 0),
424a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DAC Power",
425a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	3, 0, NULL, 0),
426a87a6653SShuming Fan 
427a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DLDO",
428a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	5, 0, NULL, 0),
429a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("VREF",
430a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	4, 0, NULL, 0),
431a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MIXER_R",
432a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	2, 0, NULL, 0),
433a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MIXER_L",
434a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	1, 0, NULL, 0),
435a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MBIAS4U",
436a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	0, 0, NULL, 0),
437a87a6653SShuming Fan 
438a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2_LDO",
439a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 4, 0, NULL, 0),
440a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2B",
441a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 3, 0, NULL, 0),
442a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2F",
443a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 2, 0, NULL, 0),
444a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2F2",
445a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 1, 0, NULL, 0),
446a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2B2",
447a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 0, 0, NULL, 0),
448a87a6653SShuming Fan 
449a87a6653SShuming Fan 	/* Digital Interface */
450a87a6653SShuming Fan 	SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
451a87a6653SShuming Fan 	SND_SOC_DAPM_SWITCH("DAC L", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_l),
452a87a6653SShuming Fan 	SND_SOC_DAPM_SWITCH("DAC R", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_r),
453a87a6653SShuming Fan 
454a87a6653SShuming Fan 	/* Output Lines */
455a87a6653SShuming Fan 	SND_SOC_DAPM_PGA_E("CLASS D", SND_SOC_NOPM, 0, 0, NULL, 0,
456a87a6653SShuming Fan 		rt1308_classd_event,
457a87a6653SShuming Fan 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
458a87a6653SShuming Fan 	SND_SOC_DAPM_OUTPUT("SPOL"),
459a87a6653SShuming Fan 	SND_SOC_DAPM_OUTPUT("SPOR"),
460a87a6653SShuming Fan };
461a87a6653SShuming Fan 
462a87a6653SShuming Fan static const struct snd_soc_dapm_route rt1308_dapm_routes[] = {
463a87a6653SShuming Fan 
464a87a6653SShuming Fan 	{ "DAC", NULL, "AIF1RX" },
465a87a6653SShuming Fan 
466a87a6653SShuming Fan 	{ "DAC", NULL, "MBIAS20U" },
467a87a6653SShuming Fan 	{ "DAC", NULL, "ALDO" },
468a87a6653SShuming Fan 	{ "DAC", NULL, "DBG" },
469a87a6653SShuming Fan 	{ "DAC", NULL, "DACL" },
470a87a6653SShuming Fan 	{ "DAC", NULL, "CLK25M" },
471a87a6653SShuming Fan 	{ "DAC", NULL, "ADC_R" },
472a87a6653SShuming Fan 	{ "DAC", NULL, "ADC_L" },
473a87a6653SShuming Fan 	{ "DAC", NULL, "DLDO" },
474a87a6653SShuming Fan 	{ "DAC", NULL, "VREF" },
475a87a6653SShuming Fan 	{ "DAC", NULL, "MIXER_R" },
476a87a6653SShuming Fan 	{ "DAC", NULL, "MIXER_L" },
477a87a6653SShuming Fan 	{ "DAC", NULL, "MBIAS4U" },
478a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2_LDO" },
479a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2B" },
480a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2F" },
481a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2F2" },
482a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2B2" },
483a87a6653SShuming Fan 
484a87a6653SShuming Fan 	{ "DAC L", "Switch", "DAC" },
485a87a6653SShuming Fan 	{ "DAC R", "Switch", "DAC" },
486a87a6653SShuming Fan 	{ "DAC L", NULL, "DAC Power" },
487a87a6653SShuming Fan 	{ "DAC R", NULL, "DAC Power" },
488a87a6653SShuming Fan 
489a87a6653SShuming Fan 	{ "CLASS D", NULL, "DAC L" },
490a87a6653SShuming Fan 	{ "CLASS D", NULL, "DAC R" },
491a87a6653SShuming Fan 	{ "SPOL", NULL, "CLASS D" },
492a87a6653SShuming Fan 	{ "SPOR", NULL, "CLASS D" },
493a87a6653SShuming Fan };
494a87a6653SShuming Fan 
495a87a6653SShuming Fan static int rt1308_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
496a87a6653SShuming Fan 				int direction)
497a87a6653SShuming Fan {
498a87a6653SShuming Fan 	struct sdw_stream_data *stream;
499a87a6653SShuming Fan 
50007b542feSPierre-Louis Bossart 	if (!sdw_stream)
50107b542feSPierre-Louis Bossart 		return 0;
50207b542feSPierre-Louis Bossart 
503a87a6653SShuming Fan 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
504a87a6653SShuming Fan 	if (!stream)
505a87a6653SShuming Fan 		return -ENOMEM;
506a87a6653SShuming Fan 
5074a550007SPierre-Louis Bossart 	stream->sdw_stream = sdw_stream;
508a87a6653SShuming Fan 
509a87a6653SShuming Fan 	/* Use tx_mask or rx_mask to configure stream tag and set dma_data */
510a87a6653SShuming Fan 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
511a87a6653SShuming Fan 		dai->playback_dma_data = stream;
512a87a6653SShuming Fan 	else
513a87a6653SShuming Fan 		dai->capture_dma_data = stream;
514a87a6653SShuming Fan 
515a87a6653SShuming Fan 	return 0;
516a87a6653SShuming Fan }
517a87a6653SShuming Fan 
518a87a6653SShuming Fan static void rt1308_sdw_shutdown(struct snd_pcm_substream *substream,
519a87a6653SShuming Fan 				struct snd_soc_dai *dai)
520a87a6653SShuming Fan {
521a87a6653SShuming Fan 	struct sdw_stream_data *stream;
522a87a6653SShuming Fan 
523a87a6653SShuming Fan 	stream = snd_soc_dai_get_dma_data(dai, substream);
524a87a6653SShuming Fan 	snd_soc_dai_set_dma_data(dai, substream, NULL);
525a87a6653SShuming Fan 	kfree(stream);
526a87a6653SShuming Fan }
527a87a6653SShuming Fan 
528f7cc9b99SPierre-Louis Bossart static int rt1308_sdw_set_tdm_slot(struct snd_soc_dai *dai,
529f7cc9b99SPierre-Louis Bossart 				   unsigned int tx_mask,
530f7cc9b99SPierre-Louis Bossart 				   unsigned int rx_mask,
531f7cc9b99SPierre-Louis Bossart 				   int slots, int slot_width)
532f7cc9b99SPierre-Louis Bossart {
533f7cc9b99SPierre-Louis Bossart 	struct snd_soc_component *component = dai->component;
534f7cc9b99SPierre-Louis Bossart 	struct rt1308_sdw_priv *rt1308 =
535f7cc9b99SPierre-Louis Bossart 		snd_soc_component_get_drvdata(component);
536f7cc9b99SPierre-Louis Bossart 
537f7cc9b99SPierre-Louis Bossart 	if (tx_mask)
538f7cc9b99SPierre-Louis Bossart 		return -EINVAL;
539f7cc9b99SPierre-Louis Bossart 
540f7cc9b99SPierre-Louis Bossart 	if (slots > 2)
541f7cc9b99SPierre-Louis Bossart 		return -EINVAL;
542f7cc9b99SPierre-Louis Bossart 
543f7cc9b99SPierre-Louis Bossart 	rt1308->rx_mask = rx_mask;
544f7cc9b99SPierre-Louis Bossart 	rt1308->slots = slots;
545f7cc9b99SPierre-Louis Bossart 	/* slot_width is not used since it's irrelevant for SoundWire */
546f7cc9b99SPierre-Louis Bossart 
547f7cc9b99SPierre-Louis Bossart 	return 0;
548f7cc9b99SPierre-Louis Bossart }
549f7cc9b99SPierre-Louis Bossart 
550a87a6653SShuming Fan static int rt1308_sdw_hw_params(struct snd_pcm_substream *substream,
551a87a6653SShuming Fan 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
552a87a6653SShuming Fan {
553a87a6653SShuming Fan 	struct snd_soc_component *component = dai->component;
554a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 =
555a87a6653SShuming Fan 		snd_soc_component_get_drvdata(component);
556a87a6653SShuming Fan 	struct sdw_stream_config stream_config;
557a87a6653SShuming Fan 	struct sdw_port_config port_config;
558a87a6653SShuming Fan 	enum sdw_data_direction direction;
559a87a6653SShuming Fan 	struct sdw_stream_data *stream;
56027a18e9eSPierre-Louis Bossart 	int retval, port, num_channels, ch_mask;
561a87a6653SShuming Fan 
562a87a6653SShuming Fan 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
563a87a6653SShuming Fan 	stream = snd_soc_dai_get_dma_data(dai, substream);
564a87a6653SShuming Fan 
565a87a6653SShuming Fan 	if (!stream)
566a87a6653SShuming Fan 		return -EINVAL;
567a87a6653SShuming Fan 
568a87a6653SShuming Fan 	if (!rt1308->sdw_slave)
569a87a6653SShuming Fan 		return -EINVAL;
570a87a6653SShuming Fan 
571a87a6653SShuming Fan 	/* SoundWire specific configuration */
572a87a6653SShuming Fan 	/* port 1 for playback */
573a87a6653SShuming Fan 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
574a87a6653SShuming Fan 		direction = SDW_DATA_DIR_RX;
575a87a6653SShuming Fan 		port = 1;
576a87a6653SShuming Fan 	} else {
577a87a6653SShuming Fan 		return -EINVAL;
578a87a6653SShuming Fan 	}
579a87a6653SShuming Fan 
58027a18e9eSPierre-Louis Bossart 	if (rt1308->slots) {
58127a18e9eSPierre-Louis Bossart 		num_channels = rt1308->slots;
58227a18e9eSPierre-Louis Bossart 		ch_mask = rt1308->rx_mask;
58327a18e9eSPierre-Louis Bossart 	} else {
58427a18e9eSPierre-Louis Bossart 		num_channels = params_channels(params);
58527a18e9eSPierre-Louis Bossart 		ch_mask = (1 << num_channels) - 1;
58627a18e9eSPierre-Louis Bossart 	}
58727a18e9eSPierre-Louis Bossart 
588a87a6653SShuming Fan 	stream_config.frame_rate = params_rate(params);
58927a18e9eSPierre-Louis Bossart 	stream_config.ch_count = num_channels;
590a87a6653SShuming Fan 	stream_config.bps = snd_pcm_format_width(params_format(params));
591a87a6653SShuming Fan 	stream_config.direction = direction;
592a87a6653SShuming Fan 
59327a18e9eSPierre-Louis Bossart 	port_config.ch_mask = ch_mask;
594a87a6653SShuming Fan 	port_config.num = port;
595a87a6653SShuming Fan 
596a87a6653SShuming Fan 	retval = sdw_stream_add_slave(rt1308->sdw_slave, &stream_config,
597a87a6653SShuming Fan 				&port_config, 1, stream->sdw_stream);
598a87a6653SShuming Fan 	if (retval) {
599a87a6653SShuming Fan 		dev_err(dai->dev, "Unable to configure port\n");
600a87a6653SShuming Fan 		return retval;
601a87a6653SShuming Fan 	}
602a87a6653SShuming Fan 
603a87a6653SShuming Fan 	return retval;
604a87a6653SShuming Fan }
605a87a6653SShuming Fan 
606a87a6653SShuming Fan static int rt1308_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
607a87a6653SShuming Fan 				struct snd_soc_dai *dai)
608a87a6653SShuming Fan {
609a87a6653SShuming Fan 	struct snd_soc_component *component = dai->component;
610a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 =
611a87a6653SShuming Fan 		snd_soc_component_get_drvdata(component);
612a87a6653SShuming Fan 	struct sdw_stream_data *stream =
613a87a6653SShuming Fan 		snd_soc_dai_get_dma_data(dai, substream);
614a87a6653SShuming Fan 
615a87a6653SShuming Fan 	if (!rt1308->sdw_slave)
616a87a6653SShuming Fan 		return -EINVAL;
617a87a6653SShuming Fan 
618a87a6653SShuming Fan 	sdw_stream_remove_slave(rt1308->sdw_slave, stream->sdw_stream);
619a87a6653SShuming Fan 	return 0;
620a87a6653SShuming Fan }
621a87a6653SShuming Fan 
622a87a6653SShuming Fan /*
623a87a6653SShuming Fan  * slave_ops: callbacks for get_clock_stop_mode, clock_stop and
624a87a6653SShuming Fan  * port_prep are not defined for now
625a87a6653SShuming Fan  */
626628fc9d9SRikard Falkeborn static const struct sdw_slave_ops rt1308_slave_ops = {
627a87a6653SShuming Fan 	.read_prop = rt1308_read_prop,
628a87a6653SShuming Fan 	.interrupt_callback = rt1308_interrupt_callback,
629a87a6653SShuming Fan 	.update_status = rt1308_update_status,
630a87a6653SShuming Fan 	.bus_config = rt1308_bus_config,
631a87a6653SShuming Fan };
632a87a6653SShuming Fan 
633*5efb40b3SShuming Fan static int rt1308_sdw_parse_dt(struct rt1308_sdw_priv *rt1308, struct device *dev)
634*5efb40b3SShuming Fan {
635*5efb40b3SShuming Fan 	int ret = 0;
636*5efb40b3SShuming Fan 
637*5efb40b3SShuming Fan 	device_property_read_u32(dev, "realtek,bq-params-cnt", &rt1308->bq_params_cnt);
638*5efb40b3SShuming Fan 	if (rt1308->bq_params_cnt) {
639*5efb40b3SShuming Fan 		rt1308->bq_params = devm_kzalloc(dev, rt1308->bq_params_cnt, GFP_KERNEL);
640*5efb40b3SShuming Fan 		if (!rt1308->bq_params) {
641*5efb40b3SShuming Fan 			dev_err(dev, "Could not allocate bq_params memory\n");
642*5efb40b3SShuming Fan 			ret = -ENOMEM;
643*5efb40b3SShuming Fan 		} else {
644*5efb40b3SShuming Fan 			ret = device_property_read_u8_array(dev, "realtek,bq-params", rt1308->bq_params, rt1308->bq_params_cnt);
645*5efb40b3SShuming Fan 			if (ret < 0)
646*5efb40b3SShuming Fan 				dev_err(dev, "Could not read list of realtek,bq-params\n");
647*5efb40b3SShuming Fan 		}
648*5efb40b3SShuming Fan 	}
649*5efb40b3SShuming Fan 
650*5efb40b3SShuming Fan 	dev_dbg(dev, "bq_params_cnt=%d\n", rt1308->bq_params_cnt);
651*5efb40b3SShuming Fan 	return ret;
652*5efb40b3SShuming Fan }
653*5efb40b3SShuming Fan 
654011e397fSPierre-Louis Bossart static int rt1308_sdw_component_probe(struct snd_soc_component *component)
655011e397fSPierre-Louis Bossart {
656*5efb40b3SShuming Fan 	struct rt1308_sdw_priv *rt1308 = snd_soc_component_get_drvdata(component);
657011e397fSPierre-Louis Bossart 	int ret;
658011e397fSPierre-Louis Bossart 
659*5efb40b3SShuming Fan 	rt1308->component = component;
660*5efb40b3SShuming Fan 	rt1308_sdw_parse_dt(rt1308, &rt1308->sdw_slave->dev);
661*5efb40b3SShuming Fan 
662011e397fSPierre-Louis Bossart 	ret = pm_runtime_resume(component->dev);
663011e397fSPierre-Louis Bossart 	if (ret < 0 && ret != -EACCES)
664011e397fSPierre-Louis Bossart 		return ret;
665011e397fSPierre-Louis Bossart 
666*5efb40b3SShuming Fan 	/* apply BQ params */
667*5efb40b3SShuming Fan 	rt1308_apply_bq_params(rt1308);
668*5efb40b3SShuming Fan 
669011e397fSPierre-Louis Bossart 	return 0;
670011e397fSPierre-Louis Bossart }
671011e397fSPierre-Louis Bossart 
672a87a6653SShuming Fan static const struct snd_soc_component_driver soc_component_sdw_rt1308 = {
673011e397fSPierre-Louis Bossart 	.probe = rt1308_sdw_component_probe,
674a87a6653SShuming Fan 	.controls = rt1308_snd_controls,
675a87a6653SShuming Fan 	.num_controls = ARRAY_SIZE(rt1308_snd_controls),
676a87a6653SShuming Fan 	.dapm_widgets = rt1308_dapm_widgets,
677a87a6653SShuming Fan 	.num_dapm_widgets = ARRAY_SIZE(rt1308_dapm_widgets),
678a87a6653SShuming Fan 	.dapm_routes = rt1308_dapm_routes,
679a87a6653SShuming Fan 	.num_dapm_routes = ARRAY_SIZE(rt1308_dapm_routes),
6809b536b34SCharles Keepax 	.endianness = 1,
681a87a6653SShuming Fan };
682a87a6653SShuming Fan 
683a87a6653SShuming Fan static const struct snd_soc_dai_ops rt1308_aif_dai_ops = {
684a87a6653SShuming Fan 	.hw_params = rt1308_sdw_hw_params,
685a87a6653SShuming Fan 	.hw_free	= rt1308_sdw_pcm_hw_free,
686e8444560SPierre-Louis Bossart 	.set_stream	= rt1308_set_sdw_stream,
687a87a6653SShuming Fan 	.shutdown	= rt1308_sdw_shutdown,
688f7cc9b99SPierre-Louis Bossart 	.set_tdm_slot	= rt1308_sdw_set_tdm_slot,
689a87a6653SShuming Fan };
690a87a6653SShuming Fan 
691a87a6653SShuming Fan #define RT1308_STEREO_RATES SNDRV_PCM_RATE_48000
692a87a6653SShuming Fan #define RT1308_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
693a87a6653SShuming Fan 			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE | \
694a87a6653SShuming Fan 			SNDRV_PCM_FMTBIT_S24_LE)
695a87a6653SShuming Fan 
696a87a6653SShuming Fan static struct snd_soc_dai_driver rt1308_sdw_dai[] = {
697a87a6653SShuming Fan 	{
698a87a6653SShuming Fan 		.name = "rt1308-aif",
699a87a6653SShuming Fan 		.playback = {
700a87a6653SShuming Fan 			.stream_name = "DP1 Playback",
701a87a6653SShuming Fan 			.channels_min = 1,
702a87a6653SShuming Fan 			.channels_max = 2,
703a87a6653SShuming Fan 			.rates = RT1308_STEREO_RATES,
704a87a6653SShuming Fan 			.formats = RT1308_FORMATS,
705a87a6653SShuming Fan 		},
706a87a6653SShuming Fan 		.ops = &rt1308_aif_dai_ops,
707a87a6653SShuming Fan 	},
708a87a6653SShuming Fan };
709a87a6653SShuming Fan 
710a87a6653SShuming Fan static int rt1308_sdw_init(struct device *dev, struct regmap *regmap,
711a87a6653SShuming Fan 				struct sdw_slave *slave)
712a87a6653SShuming Fan {
713a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308;
714a87a6653SShuming Fan 	int ret;
715a87a6653SShuming Fan 
716a87a6653SShuming Fan 	rt1308 = devm_kzalloc(dev, sizeof(*rt1308), GFP_KERNEL);
717a87a6653SShuming Fan 	if (!rt1308)
718a87a6653SShuming Fan 		return -ENOMEM;
719a87a6653SShuming Fan 
720a87a6653SShuming Fan 	dev_set_drvdata(dev, rt1308);
721a87a6653SShuming Fan 	rt1308->sdw_slave = slave;
722a87a6653SShuming Fan 	rt1308->regmap = regmap;
723a87a6653SShuming Fan 
724a87a6653SShuming Fan 	/*
725a87a6653SShuming Fan 	 * Mark hw_init to false
726a87a6653SShuming Fan 	 * HW init will be performed when device reports present
727a87a6653SShuming Fan 	 */
728a87a6653SShuming Fan 	rt1308->hw_init = false;
729a87a6653SShuming Fan 	rt1308->first_hw_init = false;
730a87a6653SShuming Fan 
731a87a6653SShuming Fan 	ret =  devm_snd_soc_register_component(dev,
732a87a6653SShuming Fan 				&soc_component_sdw_rt1308,
733a87a6653SShuming Fan 				rt1308_sdw_dai,
734a87a6653SShuming Fan 				ARRAY_SIZE(rt1308_sdw_dai));
735a87a6653SShuming Fan 
736a87a6653SShuming Fan 	dev_dbg(&slave->dev, "%s\n", __func__);
737a87a6653SShuming Fan 
738a87a6653SShuming Fan 	return ret;
739a87a6653SShuming Fan }
740a87a6653SShuming Fan 
741a87a6653SShuming Fan static int rt1308_sdw_probe(struct sdw_slave *slave,
742a87a6653SShuming Fan 				const struct sdw_device_id *id)
743a87a6653SShuming Fan {
744a87a6653SShuming Fan 	struct regmap *regmap;
745a87a6653SShuming Fan 
746a87a6653SShuming Fan 	/* Regmap Initialization */
747a87a6653SShuming Fan 	regmap = devm_regmap_init_sdw(slave, &rt1308_sdw_regmap);
748344850d9SVinod Koul 	if (IS_ERR(regmap))
749344850d9SVinod Koul 		return PTR_ERR(regmap);
750a87a6653SShuming Fan 
751a87a6653SShuming Fan 	rt1308_sdw_init(&slave->dev, regmap, slave);
752a87a6653SShuming Fan 
753a87a6653SShuming Fan 	return 0;
754a87a6653SShuming Fan }
755a87a6653SShuming Fan 
756f9e9bdd5SPierre-Louis Bossart static int rt1308_sdw_remove(struct sdw_slave *slave)
757f9e9bdd5SPierre-Louis Bossart {
758f9e9bdd5SPierre-Louis Bossart 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
759f9e9bdd5SPierre-Louis Bossart 
760f9e9bdd5SPierre-Louis Bossart 	if (rt1308->first_hw_init)
761f9e9bdd5SPierre-Louis Bossart 		pm_runtime_disable(&slave->dev);
762f9e9bdd5SPierre-Louis Bossart 
763f9e9bdd5SPierre-Louis Bossart 	return 0;
764f9e9bdd5SPierre-Louis Bossart }
765f9e9bdd5SPierre-Louis Bossart 
766a87a6653SShuming Fan static const struct sdw_device_id rt1308_id[] = {
7679e473058SPierre-Louis Bossart 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x1308, 0x2, 0, 0),
768a87a6653SShuming Fan 	{},
769a87a6653SShuming Fan };
770a87a6653SShuming Fan MODULE_DEVICE_TABLE(sdw, rt1308_id);
771a87a6653SShuming Fan 
772faa37a93STakashi Iwai static int __maybe_unused rt1308_dev_suspend(struct device *dev)
773a87a6653SShuming Fan {
774a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
775a87a6653SShuming Fan 
776a87a6653SShuming Fan 	if (!rt1308->hw_init)
777a87a6653SShuming Fan 		return 0;
778a87a6653SShuming Fan 
779a87a6653SShuming Fan 	regcache_cache_only(rt1308->regmap, true);
780a87a6653SShuming Fan 
781a87a6653SShuming Fan 	return 0;
782a87a6653SShuming Fan }
783a87a6653SShuming Fan 
7847ef8c9edSPierre-Louis Bossart #define RT1308_PROBE_TIMEOUT 5000
785a87a6653SShuming Fan 
786faa37a93STakashi Iwai static int __maybe_unused rt1308_dev_resume(struct device *dev)
787a87a6653SShuming Fan {
788a87a6653SShuming Fan 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
789a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
790a87a6653SShuming Fan 	unsigned long time;
791a87a6653SShuming Fan 
79230e102daSPierre-Louis Bossart 	if (!rt1308->first_hw_init)
793a87a6653SShuming Fan 		return 0;
794a87a6653SShuming Fan 
795a87a6653SShuming Fan 	if (!slave->unattach_request)
796a87a6653SShuming Fan 		goto regmap_sync;
797a87a6653SShuming Fan 
798a87a6653SShuming Fan 	time = wait_for_completion_timeout(&slave->initialization_complete,
799a87a6653SShuming Fan 				msecs_to_jiffies(RT1308_PROBE_TIMEOUT));
800a87a6653SShuming Fan 	if (!time) {
801a87a6653SShuming Fan 		dev_err(&slave->dev, "Initialization not complete, timed out\n");
802917df025SPierre-Louis Bossart 		sdw_show_ping_status(slave->bus, true);
803917df025SPierre-Louis Bossart 
804a87a6653SShuming Fan 		return -ETIMEDOUT;
805a87a6653SShuming Fan 	}
806a87a6653SShuming Fan 
807a87a6653SShuming Fan regmap_sync:
808a87a6653SShuming Fan 	slave->unattach_request = 0;
809a87a6653SShuming Fan 	regcache_cache_only(rt1308->regmap, false);
810a87a6653SShuming Fan 	regcache_sync_region(rt1308->regmap, 0xc000, 0xcfff);
811a87a6653SShuming Fan 
812a87a6653SShuming Fan 	return 0;
813a87a6653SShuming Fan }
814a87a6653SShuming Fan 
815a87a6653SShuming Fan static const struct dev_pm_ops rt1308_pm = {
816a87a6653SShuming Fan 	SET_SYSTEM_SLEEP_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume)
817a87a6653SShuming Fan 	SET_RUNTIME_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume, NULL)
818a87a6653SShuming Fan };
819a87a6653SShuming Fan 
820a87a6653SShuming Fan static struct sdw_driver rt1308_sdw_driver = {
821a87a6653SShuming Fan 	.driver = {
822a87a6653SShuming Fan 		.name = "rt1308",
823a87a6653SShuming Fan 		.owner = THIS_MODULE,
824a87a6653SShuming Fan 		.pm = &rt1308_pm,
825a87a6653SShuming Fan 	},
826a87a6653SShuming Fan 	.probe = rt1308_sdw_probe,
827f9e9bdd5SPierre-Louis Bossart 	.remove = rt1308_sdw_remove,
828a87a6653SShuming Fan 	.ops = &rt1308_slave_ops,
829a87a6653SShuming Fan 	.id_table = rt1308_id,
830a87a6653SShuming Fan };
831a87a6653SShuming Fan module_sdw_driver(rt1308_sdw_driver);
832a87a6653SShuming Fan 
833a87a6653SShuming Fan MODULE_DESCRIPTION("ASoC RT1308 driver SDW");
834a87a6653SShuming Fan MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
835a87a6653SShuming Fan MODULE_LICENSE("GPL v2");
836