xref: /linux/sound/soc/codecs/rt1308-sdw.c (revision 33e02dc69afbd8f1b85a51d74d72f139ba4ca623)
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>
20896c59edSCharles Keepax #include <sound/sdw.h>
21a87a6653SShuming Fan #include <sound/soc.h>
22a87a6653SShuming Fan #include <sound/soc-dapm.h>
23a87a6653SShuming Fan #include <sound/initval.h>
24a87a6653SShuming Fan 
25a87a6653SShuming Fan #include "rt1308.h"
26a87a6653SShuming Fan #include "rt1308-sdw.h"
27a87a6653SShuming Fan 
rt1308_readable_register(struct device * dev,unsigned int reg)28a87a6653SShuming Fan static bool rt1308_readable_register(struct device *dev, unsigned int reg)
29a87a6653SShuming Fan {
30a87a6653SShuming Fan 	switch (reg) {
31a87a6653SShuming Fan 	case 0x00e0:
32a87a6653SShuming Fan 	case 0x00f0:
33a87a6653SShuming Fan 	case 0x2f01 ... 0x2f07:
34a87a6653SShuming Fan 	case 0x3000 ... 0x3001:
35a87a6653SShuming Fan 	case 0x3004 ... 0x3005:
36a87a6653SShuming Fan 	case 0x3008:
37a87a6653SShuming Fan 	case 0x300a:
38a87a6653SShuming Fan 	case 0xc000 ... 0xcff3:
39a87a6653SShuming Fan 		return true;
40a87a6653SShuming Fan 	default:
41a87a6653SShuming Fan 		return false;
42a87a6653SShuming Fan 	}
43a87a6653SShuming Fan }
44a87a6653SShuming Fan 
rt1308_volatile_register(struct device * dev,unsigned int reg)45a87a6653SShuming Fan static bool rt1308_volatile_register(struct device *dev, unsigned int reg)
46a87a6653SShuming Fan {
47a87a6653SShuming Fan 	switch (reg) {
48a87a6653SShuming Fan 	case 0x2f01 ... 0x2f07:
49a87a6653SShuming Fan 	case 0x3000 ... 0x3001:
50a87a6653SShuming Fan 	case 0x3004 ... 0x3005:
51a87a6653SShuming Fan 	case 0x3008:
52a87a6653SShuming Fan 	case 0x300a:
53a87a6653SShuming Fan 	case 0xc000:
5400aaf8bfSShuming Fan 	case 0xc710:
55*37aba319SShuming Fan 	case 0xcf01:
56abed17fdSShuming Fan 	case 0xc860 ... 0xc863:
57abed17fdSShuming Fan 	case 0xc870 ... 0xc873:
58a87a6653SShuming Fan 		return true;
59a87a6653SShuming Fan 	default:
60a87a6653SShuming Fan 		return false;
61a87a6653SShuming Fan 	}
62a87a6653SShuming Fan }
63a87a6653SShuming Fan 
64a87a6653SShuming Fan static const struct regmap_config rt1308_sdw_regmap = {
65a87a6653SShuming Fan 	.reg_bits = 32,
66a87a6653SShuming Fan 	.val_bits = 8,
67a87a6653SShuming Fan 	.readable_reg = rt1308_readable_register,
68a87a6653SShuming Fan 	.volatile_reg = rt1308_volatile_register,
69a87a6653SShuming Fan 	.max_register = 0xcfff,
70a87a6653SShuming Fan 	.reg_defaults = rt1308_reg_defaults,
71a87a6653SShuming Fan 	.num_reg_defaults = ARRAY_SIZE(rt1308_reg_defaults),
724f69e29aSMark Brown 	.cache_type = REGCACHE_MAPLE,
73a87a6653SShuming Fan 	.use_single_read = true,
74a87a6653SShuming Fan 	.use_single_write = true,
75a87a6653SShuming Fan };
76a87a6653SShuming Fan 
77a87a6653SShuming Fan /* Bus clock frequency */
78a87a6653SShuming Fan #define RT1308_CLK_FREQ_9600000HZ 9600000
79a87a6653SShuming Fan #define RT1308_CLK_FREQ_12000000HZ 12000000
80a87a6653SShuming Fan #define RT1308_CLK_FREQ_6000000HZ 6000000
81a87a6653SShuming Fan #define RT1308_CLK_FREQ_4800000HZ 4800000
82a87a6653SShuming Fan #define RT1308_CLK_FREQ_2400000HZ 2400000
83a87a6653SShuming Fan #define RT1308_CLK_FREQ_12288000HZ 12288000
84a87a6653SShuming Fan 
rt1308_clock_config(struct device * dev)85a87a6653SShuming Fan static int rt1308_clock_config(struct device *dev)
86a87a6653SShuming Fan {
87a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
88a87a6653SShuming Fan 	unsigned int clk_freq, value;
89a87a6653SShuming Fan 
90a87a6653SShuming Fan 	clk_freq = (rt1308->params.curr_dr_freq >> 1);
91a87a6653SShuming Fan 
92a87a6653SShuming Fan 	switch (clk_freq) {
93a87a6653SShuming Fan 	case RT1308_CLK_FREQ_12000000HZ:
94a87a6653SShuming Fan 		value = 0x0;
95a87a6653SShuming Fan 		break;
96a87a6653SShuming Fan 	case RT1308_CLK_FREQ_6000000HZ:
97a87a6653SShuming Fan 		value = 0x1;
98a87a6653SShuming Fan 		break;
99a87a6653SShuming Fan 	case RT1308_CLK_FREQ_9600000HZ:
100a87a6653SShuming Fan 		value = 0x2;
101a87a6653SShuming Fan 		break;
102a87a6653SShuming Fan 	case RT1308_CLK_FREQ_4800000HZ:
103a87a6653SShuming Fan 		value = 0x3;
104a87a6653SShuming Fan 		break;
105a87a6653SShuming Fan 	case RT1308_CLK_FREQ_2400000HZ:
106a87a6653SShuming Fan 		value = 0x4;
107a87a6653SShuming Fan 		break;
108a87a6653SShuming Fan 	case RT1308_CLK_FREQ_12288000HZ:
109a87a6653SShuming Fan 		value = 0x5;
110a87a6653SShuming Fan 		break;
111a87a6653SShuming Fan 	default:
112a87a6653SShuming Fan 		return -EINVAL;
113a87a6653SShuming Fan 	}
114a87a6653SShuming Fan 
115a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xe0, value);
116a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xf0, value);
117a87a6653SShuming Fan 
118a87a6653SShuming Fan 	dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
119a87a6653SShuming Fan 
120a87a6653SShuming Fan 	return 0;
121a87a6653SShuming Fan }
122a87a6653SShuming Fan 
rt1308_read_prop(struct sdw_slave * slave)123a87a6653SShuming Fan static int rt1308_read_prop(struct sdw_slave *slave)
124a87a6653SShuming Fan {
125a87a6653SShuming Fan 	struct sdw_slave_prop *prop = &slave->prop;
126d0bbcb4eSPierre-Louis Bossart 	int nval, i;
127a87a6653SShuming Fan 	u32 bit;
128a87a6653SShuming Fan 	unsigned long addr;
129a87a6653SShuming Fan 	struct sdw_dpn_prop *dpn;
130a87a6653SShuming Fan 
1312acd30b9SPierre-Louis Bossart 	prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY;
13238edbfaeSPierre-Louis Bossart 	prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
1332acd30b9SPierre-Louis Bossart 
134a87a6653SShuming Fan 	prop->paging_support = true;
135a87a6653SShuming Fan 
136a87a6653SShuming Fan 	/* first we need to allocate memory for set bits in port lists */
137a87a6653SShuming Fan 	prop->source_ports = 0x00; /* BITMAP: 00010100 (not enable yet) */
138a87a6653SShuming Fan 	prop->sink_ports = 0x2; /* BITMAP:  00000010 */
139a87a6653SShuming Fan 
140a87a6653SShuming Fan 	/* for sink */
141a87a6653SShuming Fan 	nval = hweight32(prop->sink_ports);
142a87a6653SShuming Fan 	prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
143a87a6653SShuming Fan 						sizeof(*prop->sink_dpn_prop),
144a87a6653SShuming Fan 						GFP_KERNEL);
145a87a6653SShuming Fan 	if (!prop->sink_dpn_prop)
146a87a6653SShuming Fan 		return -ENOMEM;
147a87a6653SShuming Fan 
148a87a6653SShuming Fan 	i = 0;
149a87a6653SShuming Fan 	dpn = prop->sink_dpn_prop;
150a87a6653SShuming Fan 	addr = prop->sink_ports;
151a87a6653SShuming Fan 	for_each_set_bit(bit, &addr, 32) {
152a87a6653SShuming Fan 		dpn[i].num = bit;
153a87a6653SShuming Fan 		dpn[i].type = SDW_DPN_FULL;
154a87a6653SShuming Fan 		dpn[i].simple_ch_prep_sm = true;
155a87a6653SShuming Fan 		dpn[i].ch_prep_timeout = 10;
156a87a6653SShuming Fan 		i++;
157a87a6653SShuming Fan 	}
158a87a6653SShuming Fan 
159a87a6653SShuming Fan 	/* set the timeout values */
160a87a6653SShuming Fan 	prop->clk_stop_timeout = 20;
161a87a6653SShuming Fan 
162a87a6653SShuming Fan 	dev_dbg(&slave->dev, "%s\n", __func__);
163a87a6653SShuming Fan 
164a87a6653SShuming Fan 	return 0;
165a87a6653SShuming Fan }
166a87a6653SShuming Fan 
rt1308_apply_calib_params(struct rt1308_sdw_priv * rt1308)167abed17fdSShuming Fan static void rt1308_apply_calib_params(struct rt1308_sdw_priv *rt1308)
168abed17fdSShuming Fan {
169abed17fdSShuming Fan 	unsigned int efuse_m_btl_l, efuse_m_btl_r, tmp;
170abed17fdSShuming Fan 	unsigned int efuse_c_btl_l, efuse_c_btl_r;
171abed17fdSShuming Fan 
172abed17fdSShuming Fan 	/* read efuse to apply calibration parameters */
173abed17fdSShuming Fan 	regmap_write(rt1308->regmap, 0xc7f0, 0x04);
174abed17fdSShuming Fan 	regmap_write(rt1308->regmap, 0xc7f1, 0xfe);
175abed17fdSShuming Fan 	msleep(100);
176abed17fdSShuming Fan 	regmap_write(rt1308->regmap, 0xc7f0, 0x44);
177abed17fdSShuming Fan 	msleep(20);
178abed17fdSShuming Fan 	regmap_write(rt1308->regmap, 0xc240, 0x10);
179abed17fdSShuming Fan 
180abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc861, &tmp);
181abed17fdSShuming Fan 	efuse_m_btl_l = tmp;
182abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc860, &tmp);
183abed17fdSShuming Fan 	efuse_m_btl_l = efuse_m_btl_l | (tmp << 8);
184abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc863, &tmp);
185abed17fdSShuming Fan 	efuse_c_btl_l = tmp;
186abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc862, &tmp);
187abed17fdSShuming Fan 	efuse_c_btl_l = efuse_c_btl_l | (tmp << 8);
188abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc871, &tmp);
189abed17fdSShuming Fan 	efuse_m_btl_r = tmp;
190abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc870, &tmp);
191abed17fdSShuming Fan 	efuse_m_btl_r = efuse_m_btl_r | (tmp << 8);
192abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc873, &tmp);
193abed17fdSShuming Fan 	efuse_c_btl_r = tmp;
194abed17fdSShuming Fan 	regmap_read(rt1308->regmap, 0xc872, &tmp);
195abed17fdSShuming Fan 	efuse_c_btl_r = efuse_c_btl_r | (tmp << 8);
196abed17fdSShuming Fan 	dev_dbg(&rt1308->sdw_slave->dev, "%s m_btl_l=0x%x, m_btl_r=0x%x\n", __func__,
197abed17fdSShuming Fan 		efuse_m_btl_l, efuse_m_btl_r);
198abed17fdSShuming Fan 	dev_dbg(&rt1308->sdw_slave->dev, "%s c_btl_l=0x%x, c_btl_r=0x%x\n", __func__,
199abed17fdSShuming Fan 		efuse_c_btl_l, efuse_c_btl_r);
200abed17fdSShuming Fan }
201abed17fdSShuming Fan 
rt1308_apply_bq_params(struct rt1308_sdw_priv * rt1308)2025efb40b3SShuming Fan static void rt1308_apply_bq_params(struct rt1308_sdw_priv *rt1308)
2035efb40b3SShuming Fan {
2045efb40b3SShuming Fan 	unsigned int i, reg, data;
2055efb40b3SShuming Fan 
2065efb40b3SShuming Fan 	for (i = 0; i < rt1308->bq_params_cnt; i += 3) {
2075efb40b3SShuming Fan 		reg = rt1308->bq_params[i] | (rt1308->bq_params[i + 1] << 8);
2085efb40b3SShuming Fan 		data = rt1308->bq_params[i + 2];
2095efb40b3SShuming Fan 		regmap_write(rt1308->regmap, reg, data);
2105efb40b3SShuming Fan 	}
2115efb40b3SShuming Fan }
2125efb40b3SShuming Fan 
rt1308_io_init(struct device * dev,struct sdw_slave * slave)213a87a6653SShuming Fan static int rt1308_io_init(struct device *dev, struct sdw_slave *slave)
214a87a6653SShuming Fan {
215a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
216a87a6653SShuming Fan 	int ret = 0;
217*37aba319SShuming Fan 	unsigned int tmp, hibernation_flag;
218a87a6653SShuming Fan 
219a87a6653SShuming Fan 	if (rt1308->hw_init)
220a87a6653SShuming Fan 		return 0;
221a87a6653SShuming Fan 
222a87a6653SShuming Fan 	regcache_cache_only(rt1308->regmap, false);
22349ae74abSPierre-Louis Bossart 	if (rt1308->first_hw_init)
224a87a6653SShuming Fan 		regcache_cache_bypass(rt1308->regmap, true);
225a87a6653SShuming Fan 
226a87a6653SShuming Fan 	/*
2271772552eSPierre-Louis Bossart 	 * PM runtime status is marked as 'active' only when a Slave reports as Attached
228a87a6653SShuming Fan 	 */
2291772552eSPierre-Louis Bossart 	if (!rt1308->first_hw_init)
230a87a6653SShuming Fan 		/* update count of parent 'active' children */
231a87a6653SShuming Fan 		pm_runtime_set_active(&slave->dev);
232a87a6653SShuming Fan 
233a87a6653SShuming Fan 	pm_runtime_get_noresume(&slave->dev);
234a87a6653SShuming Fan 
235*37aba319SShuming Fan 	regmap_read(rt1308->regmap, 0xcf01, &hibernation_flag);
236*37aba319SShuming Fan 	if ((hibernation_flag != 0x00) && rt1308->first_hw_init)
237*37aba319SShuming Fan 		goto _preset_ready_;
238*37aba319SShuming Fan 
239a87a6653SShuming Fan 	/* sw reset */
240a87a6653SShuming Fan 	regmap_write(rt1308->regmap, RT1308_SDW_RESET, 0);
241a87a6653SShuming Fan 
24200aaf8bfSShuming Fan 	regmap_read(rt1308->regmap, 0xc710, &tmp);
24300aaf8bfSShuming Fan 	rt1308->hw_ver = tmp;
24400aaf8bfSShuming Fan 	dev_dbg(dev, "%s, hw_ver=0x%x\n", __func__, rt1308->hw_ver);
24500aaf8bfSShuming Fan 
246a87a6653SShuming Fan 	/* initial settings */
247a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc103, 0xc0);
248a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc030, 0x17);
249a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc031, 0x81);
250a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc032, 0x26);
251a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc040, 0x80);
252a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc041, 0x80);
253a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc042, 0x06);
254a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc052, 0x0a);
255a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc080, 0x0a);
256a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc060, 0x02);
257a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc061, 0x75);
258a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc062, 0x05);
259a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc171, 0x07);
260a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc173, 0x0d);
26100aaf8bfSShuming Fan 	if (rt1308->hw_ver == RT1308_VER_C) {
262a87a6653SShuming Fan 		regmap_write(rt1308->regmap, 0xc311, 0x7f);
26300aaf8bfSShuming Fan 		regmap_write(rt1308->regmap, 0xc300, 0x09);
26400aaf8bfSShuming Fan 	} else {
26500aaf8bfSShuming Fan 		regmap_write(rt1308->regmap, 0xc311, 0x4f);
26600aaf8bfSShuming Fan 		regmap_write(rt1308->regmap, 0xc300, 0x0b);
26700aaf8bfSShuming Fan 	}
26800aaf8bfSShuming Fan 	regmap_write(rt1308->regmap, 0xc900, 0x5a);
269a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc1a0, 0x84);
270a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc1a1, 0x01);
271a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc360, 0x78);
272a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc361, 0x87);
273a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc0a1, 0x71);
274a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc210, 0x00);
275a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc070, 0x00);
276a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc100, 0xd7);
277a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc101, 0xd7);
278a87a6653SShuming Fan 
279*37aba319SShuming Fan 	/* apply BQ params */
280*37aba319SShuming Fan 	rt1308_apply_bq_params(rt1308);
281*37aba319SShuming Fan 
282*37aba319SShuming Fan 	regmap_write(rt1308->regmap, 0xcf01, 0x01);
283*37aba319SShuming Fan 
284*37aba319SShuming Fan _preset_ready_:
285a87a6653SShuming Fan 	if (rt1308->first_hw_init) {
286a87a6653SShuming Fan 		regcache_cache_bypass(rt1308->regmap, false);
287a87a6653SShuming Fan 		regcache_mark_dirty(rt1308->regmap);
288a87a6653SShuming Fan 	} else
289a87a6653SShuming Fan 		rt1308->first_hw_init = true;
290a87a6653SShuming Fan 
291a87a6653SShuming Fan 	/* Mark Slave initialization complete */
292a87a6653SShuming Fan 	rt1308->hw_init = true;
293a87a6653SShuming Fan 
294a87a6653SShuming Fan 	pm_runtime_mark_last_busy(&slave->dev);
295a87a6653SShuming Fan 	pm_runtime_put_autosuspend(&slave->dev);
296a87a6653SShuming Fan 
297a87a6653SShuming Fan 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
298a87a6653SShuming Fan 
299a87a6653SShuming Fan 	return ret;
300a87a6653SShuming Fan }
301a87a6653SShuming Fan 
rt1308_update_status(struct sdw_slave * slave,enum sdw_slave_status status)302a87a6653SShuming Fan static int rt1308_update_status(struct sdw_slave *slave,
303a87a6653SShuming Fan 					enum sdw_slave_status status)
304a87a6653SShuming Fan {
305a87a6653SShuming Fan 	struct  rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
306a87a6653SShuming Fan 
307a87a6653SShuming Fan 	if (status == SDW_SLAVE_UNATTACHED)
308a87a6653SShuming Fan 		rt1308->hw_init = false;
309a87a6653SShuming Fan 
310a87a6653SShuming Fan 	/*
311a87a6653SShuming Fan 	 * Perform initialization only if slave status is present and
312a87a6653SShuming Fan 	 * hw_init flag is false
313a87a6653SShuming Fan 	 */
314cc3ff544SKrzysztof Kozlowski 	if (rt1308->hw_init || status != SDW_SLAVE_ATTACHED)
315a87a6653SShuming Fan 		return 0;
316a87a6653SShuming Fan 
317a87a6653SShuming Fan 	/* perform I/O transfers required for Slave initialization */
318a87a6653SShuming Fan 	return rt1308_io_init(&slave->dev, slave);
319a87a6653SShuming Fan }
320a87a6653SShuming Fan 
rt1308_bus_config(struct sdw_slave * slave,struct sdw_bus_params * params)321a87a6653SShuming Fan static int rt1308_bus_config(struct sdw_slave *slave,
322a87a6653SShuming Fan 				struct sdw_bus_params *params)
323a87a6653SShuming Fan {
324a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
325a87a6653SShuming Fan 	int ret;
326a87a6653SShuming Fan 
327a87a6653SShuming Fan 	memcpy(&rt1308->params, params, sizeof(*params));
328a87a6653SShuming Fan 
329a87a6653SShuming Fan 	ret = rt1308_clock_config(&slave->dev);
330a87a6653SShuming Fan 	if (ret < 0)
331a87a6653SShuming Fan 		dev_err(&slave->dev, "Invalid clk config");
332a87a6653SShuming Fan 
333a87a6653SShuming Fan 	return ret;
334a87a6653SShuming Fan }
335a87a6653SShuming Fan 
rt1308_interrupt_callback(struct sdw_slave * slave,struct sdw_slave_intr_status * status)336a87a6653SShuming Fan static int rt1308_interrupt_callback(struct sdw_slave *slave,
337a87a6653SShuming Fan 					struct sdw_slave_intr_status *status)
338a87a6653SShuming Fan {
339a87a6653SShuming Fan 	dev_dbg(&slave->dev,
340a87a6653SShuming Fan 		"%s control_port_stat=%x", __func__, status->control_port);
341a87a6653SShuming Fan 
342a87a6653SShuming Fan 	return 0;
343a87a6653SShuming Fan }
344a87a6653SShuming Fan 
rt1308_classd_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)345a87a6653SShuming Fan static int rt1308_classd_event(struct snd_soc_dapm_widget *w,
346a87a6653SShuming Fan 	struct snd_kcontrol *kcontrol, int event)
347a87a6653SShuming Fan {
348a87a6653SShuming Fan 	struct snd_soc_component *component =
349a87a6653SShuming Fan 		snd_soc_dapm_to_component(w->dapm);
350abed17fdSShuming Fan 	struct rt1308_sdw_priv *rt1308 =
351abed17fdSShuming Fan 		snd_soc_component_get_drvdata(component);
352a87a6653SShuming Fan 
353a87a6653SShuming Fan 	switch (event) {
354a87a6653SShuming Fan 	case SND_SOC_DAPM_POST_PMU:
355a87a6653SShuming Fan 		msleep(30);
356a87a6653SShuming Fan 		snd_soc_component_update_bits(component,
357a87a6653SShuming Fan 			RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
358a87a6653SShuming Fan 			0x3,	0x3);
359a87a6653SShuming Fan 		msleep(40);
360abed17fdSShuming Fan 		rt1308_apply_calib_params(rt1308);
361a87a6653SShuming Fan 		break;
362a87a6653SShuming Fan 	case SND_SOC_DAPM_PRE_PMD:
363a87a6653SShuming Fan 		snd_soc_component_update_bits(component,
364a87a6653SShuming Fan 			RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
365a87a6653SShuming Fan 			0x3, 0);
366a87a6653SShuming Fan 		usleep_range(150000, 200000);
367a87a6653SShuming Fan 		break;
368a87a6653SShuming Fan 
369a87a6653SShuming Fan 	default:
370a87a6653SShuming Fan 		break;
371a87a6653SShuming Fan 	}
372a87a6653SShuming Fan 
373a87a6653SShuming Fan 	return 0;
374a87a6653SShuming Fan }
375a87a6653SShuming Fan 
376a87a6653SShuming Fan static const char * const rt1308_rx_data_ch_select[] = {
377a87a6653SShuming Fan 	"LR",
378a87a6653SShuming Fan 	"LL",
379a87a6653SShuming Fan 	"RL",
380a87a6653SShuming Fan 	"RR",
381a87a6653SShuming Fan };
382a87a6653SShuming Fan 
383a87a6653SShuming Fan static SOC_ENUM_SINGLE_DECL(rt1308_rx_data_ch_enum,
384a87a6653SShuming Fan 	RT1308_SDW_OFFSET | (RT1308_DATA_PATH << 4), 0,
385a87a6653SShuming Fan 	rt1308_rx_data_ch_select);
386a87a6653SShuming Fan 
387a87a6653SShuming Fan static const struct snd_kcontrol_new rt1308_snd_controls[] = {
388a87a6653SShuming Fan 
389a87a6653SShuming Fan 	/* I2S Data Channel Selection */
390a87a6653SShuming Fan 	SOC_ENUM("RX Channel Select", rt1308_rx_data_ch_enum),
391a87a6653SShuming Fan };
392a87a6653SShuming Fan 
393a87a6653SShuming Fan static const struct snd_kcontrol_new rt1308_sto_dac_l =
394a87a6653SShuming Fan 	SOC_DAPM_SINGLE_AUTODISABLE("Switch",
395a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
396a87a6653SShuming Fan 		RT1308_DVOL_MUTE_L_EN_SFT, 1, 1);
397a87a6653SShuming Fan 
398a87a6653SShuming Fan static const struct snd_kcontrol_new rt1308_sto_dac_r =
399a87a6653SShuming Fan 	SOC_DAPM_SINGLE_AUTODISABLE("Switch",
400a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
401a87a6653SShuming Fan 		RT1308_DVOL_MUTE_R_EN_SFT, 1, 1);
402a87a6653SShuming Fan 
403a87a6653SShuming Fan static const struct snd_soc_dapm_widget rt1308_dapm_widgets[] = {
404a87a6653SShuming Fan 	/* Audio Interface */
405a87a6653SShuming Fan 	SND_SOC_DAPM_AIF_IN("AIF1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
406a87a6653SShuming Fan 
407a87a6653SShuming Fan 	/* Supply Widgets */
408a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MBIAS20U",
409a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	7, 0, NULL, 0),
410a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("ALDO",
411a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	6, 0, NULL, 0),
412a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DBG",
413a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	5, 0, NULL, 0),
414a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DACL",
415a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	4, 0, NULL, 0),
416a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("CLK25M",
417a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	2, 0, NULL, 0),
418a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("ADC_R",
419a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	1, 0, NULL, 0),
420a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("ADC_L",
421a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	0, 0, NULL, 0),
422a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DAC Power",
423a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	3, 0, NULL, 0),
424a87a6653SShuming Fan 
425a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DLDO",
426a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	5, 0, NULL, 0),
427a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("VREF",
428a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	4, 0, NULL, 0),
429a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MIXER_R",
430a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	2, 0, NULL, 0),
431a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MIXER_L",
432a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	1, 0, NULL, 0),
433a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MBIAS4U",
434a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	0, 0, NULL, 0),
435a87a6653SShuming Fan 
436a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2_LDO",
437a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 4, 0, NULL, 0),
438a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2B",
439a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 3, 0, NULL, 0),
440a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2F",
441a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 2, 0, NULL, 0),
442a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2F2",
443a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 1, 0, NULL, 0),
444a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2B2",
445a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 0, 0, NULL, 0),
446a87a6653SShuming Fan 
447a87a6653SShuming Fan 	/* Digital Interface */
448a87a6653SShuming Fan 	SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
449a87a6653SShuming Fan 	SND_SOC_DAPM_SWITCH("DAC L", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_l),
450a87a6653SShuming Fan 	SND_SOC_DAPM_SWITCH("DAC R", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_r),
451a87a6653SShuming Fan 
452a87a6653SShuming Fan 	/* Output Lines */
453a87a6653SShuming Fan 	SND_SOC_DAPM_PGA_E("CLASS D", SND_SOC_NOPM, 0, 0, NULL, 0,
454a87a6653SShuming Fan 		rt1308_classd_event,
455a87a6653SShuming Fan 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
456a87a6653SShuming Fan 	SND_SOC_DAPM_OUTPUT("SPOL"),
457a87a6653SShuming Fan 	SND_SOC_DAPM_OUTPUT("SPOR"),
458a87a6653SShuming Fan };
459a87a6653SShuming Fan 
460a87a6653SShuming Fan static const struct snd_soc_dapm_route rt1308_dapm_routes[] = {
461a87a6653SShuming Fan 
462a87a6653SShuming Fan 	{ "DAC", NULL, "AIF1RX" },
463a87a6653SShuming Fan 
464a87a6653SShuming Fan 	{ "DAC", NULL, "MBIAS20U" },
465a87a6653SShuming Fan 	{ "DAC", NULL, "ALDO" },
466a87a6653SShuming Fan 	{ "DAC", NULL, "DBG" },
467a87a6653SShuming Fan 	{ "DAC", NULL, "DACL" },
468a87a6653SShuming Fan 	{ "DAC", NULL, "CLK25M" },
469a87a6653SShuming Fan 	{ "DAC", NULL, "ADC_R" },
470a87a6653SShuming Fan 	{ "DAC", NULL, "ADC_L" },
471a87a6653SShuming Fan 	{ "DAC", NULL, "DLDO" },
472a87a6653SShuming Fan 	{ "DAC", NULL, "VREF" },
473a87a6653SShuming Fan 	{ "DAC", NULL, "MIXER_R" },
474a87a6653SShuming Fan 	{ "DAC", NULL, "MIXER_L" },
475a87a6653SShuming Fan 	{ "DAC", NULL, "MBIAS4U" },
476a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2_LDO" },
477a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2B" },
478a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2F" },
479a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2F2" },
480a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2B2" },
481a87a6653SShuming Fan 
482a87a6653SShuming Fan 	{ "DAC L", "Switch", "DAC" },
483a87a6653SShuming Fan 	{ "DAC R", "Switch", "DAC" },
484a87a6653SShuming Fan 	{ "DAC L", NULL, "DAC Power" },
485a87a6653SShuming Fan 	{ "DAC R", NULL, "DAC Power" },
486a87a6653SShuming Fan 
487a87a6653SShuming Fan 	{ "CLASS D", NULL, "DAC L" },
488a87a6653SShuming Fan 	{ "CLASS D", NULL, "DAC R" },
489a87a6653SShuming Fan 	{ "SPOL", NULL, "CLASS D" },
490a87a6653SShuming Fan 	{ "SPOR", NULL, "CLASS D" },
491a87a6653SShuming Fan };
492a87a6653SShuming Fan 
rt1308_set_sdw_stream(struct snd_soc_dai * dai,void * sdw_stream,int direction)493a87a6653SShuming Fan static int rt1308_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
494a87a6653SShuming Fan 				int direction)
495a87a6653SShuming Fan {
496ce8ffc1bSPierre-Louis Bossart 	snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
497a87a6653SShuming Fan 
498a87a6653SShuming Fan 	return 0;
499a87a6653SShuming Fan }
500a87a6653SShuming Fan 
rt1308_sdw_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)501a87a6653SShuming Fan static void rt1308_sdw_shutdown(struct snd_pcm_substream *substream,
502a87a6653SShuming Fan 				struct snd_soc_dai *dai)
503a87a6653SShuming Fan {
504a87a6653SShuming Fan 	snd_soc_dai_set_dma_data(dai, substream, NULL);
505a87a6653SShuming Fan }
506a87a6653SShuming Fan 
rt1308_sdw_set_tdm_slot(struct snd_soc_dai * dai,unsigned int tx_mask,unsigned int rx_mask,int slots,int slot_width)507f7cc9b99SPierre-Louis Bossart static int rt1308_sdw_set_tdm_slot(struct snd_soc_dai *dai,
508f7cc9b99SPierre-Louis Bossart 				   unsigned int tx_mask,
509f7cc9b99SPierre-Louis Bossart 				   unsigned int rx_mask,
510f7cc9b99SPierre-Louis Bossart 				   int slots, int slot_width)
511f7cc9b99SPierre-Louis Bossart {
512f7cc9b99SPierre-Louis Bossart 	struct snd_soc_component *component = dai->component;
513f7cc9b99SPierre-Louis Bossart 	struct rt1308_sdw_priv *rt1308 =
514f7cc9b99SPierre-Louis Bossart 		snd_soc_component_get_drvdata(component);
515f7cc9b99SPierre-Louis Bossart 
516f7cc9b99SPierre-Louis Bossart 	if (tx_mask)
517f7cc9b99SPierre-Louis Bossart 		return -EINVAL;
518f7cc9b99SPierre-Louis Bossart 
519f7cc9b99SPierre-Louis Bossart 	if (slots > 2)
520f7cc9b99SPierre-Louis Bossart 		return -EINVAL;
521f7cc9b99SPierre-Louis Bossart 
522f7cc9b99SPierre-Louis Bossart 	rt1308->rx_mask = rx_mask;
523f7cc9b99SPierre-Louis Bossart 	rt1308->slots = slots;
524f7cc9b99SPierre-Louis Bossart 	/* slot_width is not used since it's irrelevant for SoundWire */
525f7cc9b99SPierre-Louis Bossart 
526f7cc9b99SPierre-Louis Bossart 	return 0;
527f7cc9b99SPierre-Louis Bossart }
528f7cc9b99SPierre-Louis Bossart 
rt1308_sdw_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)529a87a6653SShuming Fan static int rt1308_sdw_hw_params(struct snd_pcm_substream *substream,
530a87a6653SShuming Fan 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
531a87a6653SShuming Fan {
532a87a6653SShuming Fan 	struct snd_soc_component *component = dai->component;
533a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 =
534a87a6653SShuming Fan 		snd_soc_component_get_drvdata(component);
535896c59edSCharles Keepax 	struct sdw_stream_config stream_config = {0};
536896c59edSCharles Keepax 	struct sdw_port_config port_config = {0};
537ce8ffc1bSPierre-Louis Bossart 	struct sdw_stream_runtime *sdw_stream;
538896c59edSCharles Keepax 	int retval;
539a87a6653SShuming Fan 
540a87a6653SShuming Fan 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
541ce8ffc1bSPierre-Louis Bossart 	sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
542a87a6653SShuming Fan 
543ce8ffc1bSPierre-Louis Bossart 	if (!sdw_stream)
544a87a6653SShuming Fan 		return -EINVAL;
545a87a6653SShuming Fan 
546a87a6653SShuming Fan 	if (!rt1308->sdw_slave)
547a87a6653SShuming Fan 		return -EINVAL;
548a87a6653SShuming Fan 
549a87a6653SShuming Fan 	/* SoundWire specific configuration */
550896c59edSCharles Keepax 	snd_sdw_params_to_config(substream, params, &stream_config, &port_config);
551896c59edSCharles Keepax 
552a87a6653SShuming Fan 	/* port 1 for playback */
553896c59edSCharles Keepax 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
554896c59edSCharles Keepax 		port_config.num = 1;
555896c59edSCharles Keepax 	else
556a87a6653SShuming Fan 		return -EINVAL;
557a87a6653SShuming Fan 
55827a18e9eSPierre-Louis Bossart 	if (rt1308->slots) {
559896c59edSCharles Keepax 		stream_config.ch_count = rt1308->slots;
560896c59edSCharles Keepax 		port_config.ch_mask = rt1308->rx_mask;
56127a18e9eSPierre-Louis Bossart 	}
56227a18e9eSPierre-Louis Bossart 
563a87a6653SShuming Fan 	retval = sdw_stream_add_slave(rt1308->sdw_slave, &stream_config,
564ce8ffc1bSPierre-Louis Bossart 				&port_config, 1, sdw_stream);
565a87a6653SShuming Fan 	if (retval) {
566a87a6653SShuming Fan 		dev_err(dai->dev, "Unable to configure port\n");
567a87a6653SShuming Fan 		return retval;
568a87a6653SShuming Fan 	}
569a87a6653SShuming Fan 
570a87a6653SShuming Fan 	return retval;
571a87a6653SShuming Fan }
572a87a6653SShuming Fan 
rt1308_sdw_pcm_hw_free(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)573a87a6653SShuming Fan static int rt1308_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
574a87a6653SShuming Fan 				struct snd_soc_dai *dai)
575a87a6653SShuming Fan {
576a87a6653SShuming Fan 	struct snd_soc_component *component = dai->component;
577a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 =
578a87a6653SShuming Fan 		snd_soc_component_get_drvdata(component);
579ce8ffc1bSPierre-Louis Bossart 	struct sdw_stream_runtime *sdw_stream =
580a87a6653SShuming Fan 		snd_soc_dai_get_dma_data(dai, substream);
581a87a6653SShuming Fan 
582a87a6653SShuming Fan 	if (!rt1308->sdw_slave)
583a87a6653SShuming Fan 		return -EINVAL;
584a87a6653SShuming Fan 
585ce8ffc1bSPierre-Louis Bossart 	sdw_stream_remove_slave(rt1308->sdw_slave, sdw_stream);
586a87a6653SShuming Fan 	return 0;
587a87a6653SShuming Fan }
588a87a6653SShuming Fan 
589a87a6653SShuming Fan /*
590a87a6653SShuming Fan  * slave_ops: callbacks for get_clock_stop_mode, clock_stop and
591a87a6653SShuming Fan  * port_prep are not defined for now
592a87a6653SShuming Fan  */
593628fc9d9SRikard Falkeborn static const struct sdw_slave_ops rt1308_slave_ops = {
594a87a6653SShuming Fan 	.read_prop = rt1308_read_prop,
595a87a6653SShuming Fan 	.interrupt_callback = rt1308_interrupt_callback,
596a87a6653SShuming Fan 	.update_status = rt1308_update_status,
597a87a6653SShuming Fan 	.bus_config = rt1308_bus_config,
598a87a6653SShuming Fan };
599a87a6653SShuming Fan 
rt1308_sdw_parse_dt(struct rt1308_sdw_priv * rt1308,struct device * dev)6005efb40b3SShuming Fan static int rt1308_sdw_parse_dt(struct rt1308_sdw_priv *rt1308, struct device *dev)
6015efb40b3SShuming Fan {
6025efb40b3SShuming Fan 	int ret = 0;
6035efb40b3SShuming Fan 
6045efb40b3SShuming Fan 	device_property_read_u32(dev, "realtek,bq-params-cnt", &rt1308->bq_params_cnt);
6055efb40b3SShuming Fan 	if (rt1308->bq_params_cnt) {
6065efb40b3SShuming Fan 		rt1308->bq_params = devm_kzalloc(dev, rt1308->bq_params_cnt, GFP_KERNEL);
6075efb40b3SShuming Fan 		if (!rt1308->bq_params) {
6085efb40b3SShuming Fan 			dev_err(dev, "Could not allocate bq_params memory\n");
6095efb40b3SShuming Fan 			ret = -ENOMEM;
6105efb40b3SShuming Fan 		} else {
6115efb40b3SShuming Fan 			ret = device_property_read_u8_array(dev, "realtek,bq-params", rt1308->bq_params, rt1308->bq_params_cnt);
6125efb40b3SShuming Fan 			if (ret < 0)
6135efb40b3SShuming Fan 				dev_err(dev, "Could not read list of realtek,bq-params\n");
6145efb40b3SShuming Fan 		}
6155efb40b3SShuming Fan 	}
6165efb40b3SShuming Fan 
6175efb40b3SShuming Fan 	dev_dbg(dev, "bq_params_cnt=%d\n", rt1308->bq_params_cnt);
6185efb40b3SShuming Fan 	return ret;
6195efb40b3SShuming Fan }
6205efb40b3SShuming Fan 
rt1308_sdw_component_probe(struct snd_soc_component * component)621011e397fSPierre-Louis Bossart static int rt1308_sdw_component_probe(struct snd_soc_component *component)
622011e397fSPierre-Louis Bossart {
6235efb40b3SShuming Fan 	struct rt1308_sdw_priv *rt1308 = snd_soc_component_get_drvdata(component);
624011e397fSPierre-Louis Bossart 	int ret;
625011e397fSPierre-Louis Bossart 
6265efb40b3SShuming Fan 	rt1308->component = component;
6275efb40b3SShuming Fan 	rt1308_sdw_parse_dt(rt1308, &rt1308->sdw_slave->dev);
6285efb40b3SShuming Fan 
6291772552eSPierre-Louis Bossart 	if (!rt1308->first_hw_init)
6301772552eSPierre-Louis Bossart 		return 0;
6311772552eSPierre-Louis Bossart 
632011e397fSPierre-Louis Bossart 	ret = pm_runtime_resume(component->dev);
633011e397fSPierre-Louis Bossart 	if (ret < 0 && ret != -EACCES)
634011e397fSPierre-Louis Bossart 		return ret;
635011e397fSPierre-Louis Bossart 
6365efb40b3SShuming Fan 	/* apply BQ params */
6375efb40b3SShuming Fan 	rt1308_apply_bq_params(rt1308);
6385efb40b3SShuming Fan 
639011e397fSPierre-Louis Bossart 	return 0;
640011e397fSPierre-Louis Bossart }
641011e397fSPierre-Louis Bossart 
642a87a6653SShuming Fan static const struct snd_soc_component_driver soc_component_sdw_rt1308 = {
643011e397fSPierre-Louis Bossart 	.probe = rt1308_sdw_component_probe,
644a87a6653SShuming Fan 	.controls = rt1308_snd_controls,
645a87a6653SShuming Fan 	.num_controls = ARRAY_SIZE(rt1308_snd_controls),
646a87a6653SShuming Fan 	.dapm_widgets = rt1308_dapm_widgets,
647a87a6653SShuming Fan 	.num_dapm_widgets = ARRAY_SIZE(rt1308_dapm_widgets),
648a87a6653SShuming Fan 	.dapm_routes = rt1308_dapm_routes,
649a87a6653SShuming Fan 	.num_dapm_routes = ARRAY_SIZE(rt1308_dapm_routes),
6509b536b34SCharles Keepax 	.endianness = 1,
651a87a6653SShuming Fan };
652a87a6653SShuming Fan 
653a87a6653SShuming Fan static const struct snd_soc_dai_ops rt1308_aif_dai_ops = {
654a87a6653SShuming Fan 	.hw_params = rt1308_sdw_hw_params,
655a87a6653SShuming Fan 	.hw_free	= rt1308_sdw_pcm_hw_free,
656e8444560SPierre-Louis Bossart 	.set_stream	= rt1308_set_sdw_stream,
657a87a6653SShuming Fan 	.shutdown	= rt1308_sdw_shutdown,
658f7cc9b99SPierre-Louis Bossart 	.set_tdm_slot	= rt1308_sdw_set_tdm_slot,
659a87a6653SShuming Fan };
660a87a6653SShuming Fan 
661a87a6653SShuming Fan #define RT1308_STEREO_RATES SNDRV_PCM_RATE_48000
662a87a6653SShuming Fan #define RT1308_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
663a87a6653SShuming Fan 			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE | \
664a87a6653SShuming Fan 			SNDRV_PCM_FMTBIT_S24_LE)
665a87a6653SShuming Fan 
666a87a6653SShuming Fan static struct snd_soc_dai_driver rt1308_sdw_dai[] = {
667a87a6653SShuming Fan 	{
668a87a6653SShuming Fan 		.name = "rt1308-aif",
669a87a6653SShuming Fan 		.playback = {
670a87a6653SShuming Fan 			.stream_name = "DP1 Playback",
671a87a6653SShuming Fan 			.channels_min = 1,
672a87a6653SShuming Fan 			.channels_max = 2,
673a87a6653SShuming Fan 			.rates = RT1308_STEREO_RATES,
674a87a6653SShuming Fan 			.formats = RT1308_FORMATS,
675a87a6653SShuming Fan 		},
676a87a6653SShuming Fan 		.ops = &rt1308_aif_dai_ops,
677a87a6653SShuming Fan 	},
678a87a6653SShuming Fan };
679a87a6653SShuming Fan 
rt1308_sdw_init(struct device * dev,struct regmap * regmap,struct sdw_slave * slave)680a87a6653SShuming Fan static int rt1308_sdw_init(struct device *dev, struct regmap *regmap,
681a87a6653SShuming Fan 				struct sdw_slave *slave)
682a87a6653SShuming Fan {
683a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308;
684a87a6653SShuming Fan 	int ret;
685a87a6653SShuming Fan 
686a87a6653SShuming Fan 	rt1308 = devm_kzalloc(dev, sizeof(*rt1308), GFP_KERNEL);
687a87a6653SShuming Fan 	if (!rt1308)
688a87a6653SShuming Fan 		return -ENOMEM;
689a87a6653SShuming Fan 
690a87a6653SShuming Fan 	dev_set_drvdata(dev, rt1308);
691a87a6653SShuming Fan 	rt1308->sdw_slave = slave;
692a87a6653SShuming Fan 	rt1308->regmap = regmap;
693a87a6653SShuming Fan 
69449ae74abSPierre-Louis Bossart 	regcache_cache_only(rt1308->regmap, true);
69549ae74abSPierre-Louis Bossart 
696a87a6653SShuming Fan 	/*
697a87a6653SShuming Fan 	 * Mark hw_init to false
698a87a6653SShuming Fan 	 * HW init will be performed when device reports present
699a87a6653SShuming Fan 	 */
700a87a6653SShuming Fan 	rt1308->hw_init = false;
701a87a6653SShuming Fan 	rt1308->first_hw_init = false;
702a87a6653SShuming Fan 
703a87a6653SShuming Fan 	ret =  devm_snd_soc_register_component(dev,
704a87a6653SShuming Fan 				&soc_component_sdw_rt1308,
705a87a6653SShuming Fan 				rt1308_sdw_dai,
706a87a6653SShuming Fan 				ARRAY_SIZE(rt1308_sdw_dai));
7071772552eSPierre-Louis Bossart 	if (ret < 0)
708a87a6653SShuming Fan 		return ret;
7091772552eSPierre-Louis Bossart 
7101772552eSPierre-Louis Bossart 	/* set autosuspend parameters */
7111772552eSPierre-Louis Bossart 	pm_runtime_set_autosuspend_delay(dev, 3000);
7121772552eSPierre-Louis Bossart 	pm_runtime_use_autosuspend(dev);
7131772552eSPierre-Louis Bossart 
7141772552eSPierre-Louis Bossart 	/* make sure the device does not suspend immediately */
7151772552eSPierre-Louis Bossart 	pm_runtime_mark_last_busy(dev);
7161772552eSPierre-Louis Bossart 
7171772552eSPierre-Louis Bossart 	pm_runtime_enable(dev);
7181772552eSPierre-Louis Bossart 
7191772552eSPierre-Louis Bossart 	/* important note: the device is NOT tagged as 'active' and will remain
7201772552eSPierre-Louis Bossart 	 * 'suspended' until the hardware is enumerated/initialized. This is required
7211772552eSPierre-Louis Bossart 	 * to make sure the ASoC framework use of pm_runtime_get_sync() does not silently
7221772552eSPierre-Louis Bossart 	 * fail with -EACCESS because of race conditions between card creation and enumeration
7231772552eSPierre-Louis Bossart 	 */
7241772552eSPierre-Louis Bossart 
7251772552eSPierre-Louis Bossart 	dev_dbg(dev, "%s\n", __func__);
7261772552eSPierre-Louis Bossart 
7271772552eSPierre-Louis Bossart 	return 0;
728a87a6653SShuming Fan }
729a87a6653SShuming Fan 
rt1308_sdw_probe(struct sdw_slave * slave,const struct sdw_device_id * id)730a87a6653SShuming Fan static int rt1308_sdw_probe(struct sdw_slave *slave,
731a87a6653SShuming Fan 				const struct sdw_device_id *id)
732a87a6653SShuming Fan {
733a87a6653SShuming Fan 	struct regmap *regmap;
734a87a6653SShuming Fan 
735a87a6653SShuming Fan 	/* Regmap Initialization */
736a87a6653SShuming Fan 	regmap = devm_regmap_init_sdw(slave, &rt1308_sdw_regmap);
737344850d9SVinod Koul 	if (IS_ERR(regmap))
738344850d9SVinod Koul 		return PTR_ERR(regmap);
739a87a6653SShuming Fan 
7403f3d66baSPierre-Louis Bossart 	return rt1308_sdw_init(&slave->dev, regmap, slave);
741a87a6653SShuming Fan }
742a87a6653SShuming Fan 
rt1308_sdw_remove(struct sdw_slave * slave)743f9e9bdd5SPierre-Louis Bossart static int rt1308_sdw_remove(struct sdw_slave *slave)
744f9e9bdd5SPierre-Louis Bossart {
745f9e9bdd5SPierre-Louis Bossart 	pm_runtime_disable(&slave->dev);
746f9e9bdd5SPierre-Louis Bossart 
747f9e9bdd5SPierre-Louis Bossart 	return 0;
748f9e9bdd5SPierre-Louis Bossart }
749f9e9bdd5SPierre-Louis Bossart 
750a87a6653SShuming Fan static const struct sdw_device_id rt1308_id[] = {
7519e473058SPierre-Louis Bossart 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x1308, 0x2, 0, 0),
752a87a6653SShuming Fan 	{},
753a87a6653SShuming Fan };
754a87a6653SShuming Fan MODULE_DEVICE_TABLE(sdw, rt1308_id);
755a87a6653SShuming Fan 
rt1308_dev_suspend(struct device * dev)756faa37a93STakashi Iwai static int __maybe_unused rt1308_dev_suspend(struct device *dev)
757a87a6653SShuming Fan {
758a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
759a87a6653SShuming Fan 
760a87a6653SShuming Fan 	if (!rt1308->hw_init)
761a87a6653SShuming Fan 		return 0;
762a87a6653SShuming Fan 
763a87a6653SShuming Fan 	regcache_cache_only(rt1308->regmap, true);
764a87a6653SShuming Fan 
765a87a6653SShuming Fan 	return 0;
766a87a6653SShuming Fan }
767a87a6653SShuming Fan 
7687ef8c9edSPierre-Louis Bossart #define RT1308_PROBE_TIMEOUT 5000
769a87a6653SShuming Fan 
rt1308_dev_resume(struct device * dev)770faa37a93STakashi Iwai static int __maybe_unused rt1308_dev_resume(struct device *dev)
771a87a6653SShuming Fan {
772a87a6653SShuming Fan 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
773a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
774a87a6653SShuming Fan 	unsigned long time;
775a87a6653SShuming Fan 
77630e102daSPierre-Louis Bossart 	if (!rt1308->first_hw_init)
777a87a6653SShuming Fan 		return 0;
778a87a6653SShuming Fan 
779a87a6653SShuming Fan 	if (!slave->unattach_request)
780a87a6653SShuming Fan 		goto regmap_sync;
781a87a6653SShuming Fan 
782a87a6653SShuming Fan 	time = wait_for_completion_timeout(&slave->initialization_complete,
783a87a6653SShuming Fan 				msecs_to_jiffies(RT1308_PROBE_TIMEOUT));
784a87a6653SShuming Fan 	if (!time) {
785a87a6653SShuming Fan 		dev_err(&slave->dev, "Initialization not complete, timed out\n");
786917df025SPierre-Louis Bossart 		sdw_show_ping_status(slave->bus, true);
787917df025SPierre-Louis Bossart 
788a87a6653SShuming Fan 		return -ETIMEDOUT;
789a87a6653SShuming Fan 	}
790a87a6653SShuming Fan 
791a87a6653SShuming Fan regmap_sync:
792a87a6653SShuming Fan 	slave->unattach_request = 0;
793a87a6653SShuming Fan 	regcache_cache_only(rt1308->regmap, false);
794a87a6653SShuming Fan 	regcache_sync_region(rt1308->regmap, 0xc000, 0xcfff);
795a87a6653SShuming Fan 
796a87a6653SShuming Fan 	return 0;
797a87a6653SShuming Fan }
798a87a6653SShuming Fan 
799a87a6653SShuming Fan static const struct dev_pm_ops rt1308_pm = {
800a87a6653SShuming Fan 	SET_SYSTEM_SLEEP_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume)
801a87a6653SShuming Fan 	SET_RUNTIME_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume, NULL)
802a87a6653SShuming Fan };
803a87a6653SShuming Fan 
804a87a6653SShuming Fan static struct sdw_driver rt1308_sdw_driver = {
805a87a6653SShuming Fan 	.driver = {
806a87a6653SShuming Fan 		.name = "rt1308",
807a87a6653SShuming Fan 		.pm = &rt1308_pm,
808a87a6653SShuming Fan 	},
809a87a6653SShuming Fan 	.probe = rt1308_sdw_probe,
810f9e9bdd5SPierre-Louis Bossart 	.remove = rt1308_sdw_remove,
811a87a6653SShuming Fan 	.ops = &rt1308_slave_ops,
812a87a6653SShuming Fan 	.id_table = rt1308_id,
813a87a6653SShuming Fan };
814a87a6653SShuming Fan module_sdw_driver(rt1308_sdw_driver);
815a87a6653SShuming Fan 
816a87a6653SShuming Fan MODULE_DESCRIPTION("ASoC RT1308 driver SDW");
817a87a6653SShuming Fan MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
818a87a6653SShuming Fan MODULE_LICENSE("GPL v2");
819