xref: /openbmc/linux/sound/soc/codecs/rt5682-i2c.c (revision 8957261c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // rt5682.c  --  RT5682 ALSA SoC audio component driver
4 //
5 // Copyright 2018 Realtek Semiconductor Corp.
6 // Author: Bard Liao <bardliao@realtek.com>
7 //
8 
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/pm.h>
14 #include <linux/i2c.h>
15 #include <linux/platform_device.h>
16 #include <linux/spi/spi.h>
17 #include <linux/acpi.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/mutex.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/jack.h>
24 #include <sound/soc.h>
25 #include <sound/soc-dapm.h>
26 #include <sound/initval.h>
27 #include <sound/tlv.h>
28 #include <sound/rt5682.h>
29 
30 #include "rl6231.h"
31 #include "rt5682.h"
32 
33 static const struct rt5682_platform_data i2s_default_platform_data = {
34 	.dmic1_data_pin = RT5682_DMIC1_DATA_GPIO2,
35 	.dmic1_clk_pin = RT5682_DMIC1_CLK_GPIO3,
36 	.jd_src = RT5682_JD1,
37 	.btndet_delay = 16,
38 	.dai_clk_names[RT5682_DAI_WCLK_IDX] = "rt5682-dai-wclk",
39 	.dai_clk_names[RT5682_DAI_BCLK_IDX] = "rt5682-dai-bclk",
40 };
41 
42 static const struct regmap_config rt5682_regmap = {
43 	.reg_bits = 16,
44 	.val_bits = 16,
45 	.max_register = RT5682_I2C_MODE,
46 	.volatile_reg = rt5682_volatile_register,
47 	.readable_reg = rt5682_readable_register,
48 	.cache_type = REGCACHE_MAPLE,
49 	.reg_defaults = rt5682_reg,
50 	.num_reg_defaults = RT5682_REG_NUM,
51 	.use_single_read = true,
52 	.use_single_write = true,
53 };
54 
55 static void rt5682_jd_check_handler(struct work_struct *work)
56 {
57 	struct rt5682_priv *rt5682 = container_of(work, struct rt5682_priv,
58 		jd_check_work.work);
59 
60 	if (snd_soc_component_read(rt5682->component, RT5682_AJD1_CTRL) & RT5682_JDH_RS_MASK)
61 		/* jack out */
62 		mod_delayed_work(system_power_efficient_wq,
63 				 &rt5682->jack_detect_work, 0);
64 	else
65 		schedule_delayed_work(&rt5682->jd_check_work, 500);
66 }
67 
68 static irqreturn_t rt5682_irq(int irq, void *data)
69 {
70 	struct rt5682_priv *rt5682 = data;
71 
72 	mod_delayed_work(system_power_efficient_wq,
73 		&rt5682->jack_detect_work, msecs_to_jiffies(rt5682->irq_work_delay_time));
74 
75 	return IRQ_HANDLED;
76 }
77 
78 static struct snd_soc_dai_driver rt5682_dai[] = {
79 	{
80 		.name = "rt5682-aif1",
81 		.id = RT5682_AIF1,
82 		.playback = {
83 			.stream_name = "AIF1 Playback",
84 			.channels_min = 1,
85 			.channels_max = 2,
86 			.rates = RT5682_STEREO_RATES,
87 			.formats = RT5682_FORMATS,
88 		},
89 		.capture = {
90 			.stream_name = "AIF1 Capture",
91 			.channels_min = 1,
92 			.channels_max = 2,
93 			.rates = RT5682_STEREO_RATES,
94 			.formats = RT5682_FORMATS,
95 		},
96 		.ops = &rt5682_aif1_dai_ops,
97 	},
98 	{
99 		.name = "rt5682-aif2",
100 		.id = RT5682_AIF2,
101 		.capture = {
102 			.stream_name = "AIF2 Capture",
103 			.channels_min = 1,
104 			.channels_max = 2,
105 			.rates = RT5682_STEREO_RATES,
106 			.formats = RT5682_FORMATS,
107 		},
108 		.ops = &rt5682_aif2_dai_ops,
109 	},
110 };
111 
112 static void rt5682_i2c_disable_regulators(void *data)
113 {
114 	struct rt5682_priv *rt5682 = data;
115 
116 	regulator_bulk_disable(ARRAY_SIZE(rt5682->supplies), rt5682->supplies);
117 }
118 
119 static int rt5682_i2c_probe(struct i2c_client *i2c)
120 {
121 	struct rt5682_platform_data *pdata = dev_get_platdata(&i2c->dev);
122 	struct rt5682_priv *rt5682;
123 	int i, ret;
124 	unsigned int val;
125 
126 	rt5682 = devm_kzalloc(&i2c->dev, sizeof(struct rt5682_priv),
127 		GFP_KERNEL);
128 	if (!rt5682)
129 		return -ENOMEM;
130 
131 	i2c_set_clientdata(i2c, rt5682);
132 
133 	rt5682->i2c_dev = &i2c->dev;
134 
135 	rt5682->pdata = i2s_default_platform_data;
136 
137 	if (pdata)
138 		rt5682->pdata = *pdata;
139 	else
140 		rt5682_parse_dt(rt5682, &i2c->dev);
141 
142 	rt5682->regmap = devm_regmap_init_i2c(i2c, &rt5682_regmap);
143 	if (IS_ERR(rt5682->regmap)) {
144 		ret = PTR_ERR(rt5682->regmap);
145 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
146 			ret);
147 		return ret;
148 	}
149 
150 	for (i = 0; i < ARRAY_SIZE(rt5682->supplies); i++)
151 		rt5682->supplies[i].supply = rt5682_supply_names[i];
152 
153 	ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(rt5682->supplies),
154 				      rt5682->supplies);
155 	if (ret) {
156 		dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
157 		return ret;
158 	}
159 
160 	ret = devm_add_action_or_reset(&i2c->dev, rt5682_i2c_disable_regulators,
161 				       rt5682);
162 	if (ret)
163 		return ret;
164 
165 	ret = regulator_bulk_enable(ARRAY_SIZE(rt5682->supplies),
166 				    rt5682->supplies);
167 	if (ret) {
168 		dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret);
169 		return ret;
170 	}
171 
172 	ret = rt5682_get_ldo1(rt5682, &i2c->dev);
173 	if (ret)
174 		return ret;
175 
176 	/* Sleep for 300 ms miniumum */
177 	usleep_range(300000, 350000);
178 
179 	regmap_write(rt5682->regmap, RT5682_I2C_MODE, 0x1);
180 	usleep_range(10000, 15000);
181 
182 	regmap_read(rt5682->regmap, RT5682_DEVICE_ID, &val);
183 	if (val != DEVICE_ID) {
184 		dev_err(&i2c->dev,
185 			"Device with ID register %x is not rt5682\n", val);
186 		return -ENODEV;
187 	}
188 
189 	mutex_init(&rt5682->calibrate_mutex);
190 	rt5682_calibrate(rt5682);
191 
192 	rt5682_apply_patch_list(rt5682, &i2c->dev);
193 
194 	regmap_write(rt5682->regmap, RT5682_DEPOP_1, 0x0000);
195 
196 	/* DMIC pin*/
197 	if (rt5682->pdata.dmic1_data_pin != RT5682_DMIC1_NULL) {
198 		switch (rt5682->pdata.dmic1_data_pin) {
199 		case RT5682_DMIC1_DATA_GPIO2: /* share with LRCK2 */
200 			regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1,
201 				RT5682_DMIC_1_DP_MASK, RT5682_DMIC_1_DP_GPIO2);
202 			regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1,
203 				RT5682_GP2_PIN_MASK, RT5682_GP2_PIN_DMIC_SDA);
204 			break;
205 
206 		case RT5682_DMIC1_DATA_GPIO5: /* share with DACDAT1 */
207 			regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1,
208 				RT5682_DMIC_1_DP_MASK, RT5682_DMIC_1_DP_GPIO5);
209 			regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1,
210 				RT5682_GP5_PIN_MASK, RT5682_GP5_PIN_DMIC_SDA);
211 			break;
212 
213 		default:
214 			dev_warn(&i2c->dev, "invalid DMIC_DAT pin\n");
215 			break;
216 		}
217 
218 		switch (rt5682->pdata.dmic1_clk_pin) {
219 		case RT5682_DMIC1_CLK_GPIO1: /* share with IRQ */
220 			regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1,
221 				RT5682_GP1_PIN_MASK, RT5682_GP1_PIN_DMIC_CLK);
222 			break;
223 
224 		case RT5682_DMIC1_CLK_GPIO3: /* share with BCLK2 */
225 			regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1,
226 				RT5682_GP3_PIN_MASK, RT5682_GP3_PIN_DMIC_CLK);
227 			if (rt5682->pdata.dmic_clk_driving_high)
228 				regmap_update_bits(rt5682->regmap,
229 					RT5682_PAD_DRIVING_CTRL,
230 					RT5682_PAD_DRV_GP3_MASK,
231 					2 << RT5682_PAD_DRV_GP3_SFT);
232 			break;
233 
234 		default:
235 			dev_warn(&i2c->dev, "invalid DMIC_CLK pin\n");
236 			break;
237 		}
238 	}
239 
240 	regmap_update_bits(rt5682->regmap, RT5682_PWR_ANLG_1,
241 		RT5682_LDO1_DVO_MASK | RT5682_HP_DRIVER_MASK,
242 		RT5682_LDO1_DVO_12 | RT5682_HP_DRIVER_5X);
243 	regmap_write(rt5682->regmap, RT5682_MICBIAS_2, 0x0080);
244 	regmap_update_bits(rt5682->regmap, RT5682_GPIO_CTRL_1,
245 		RT5682_GP4_PIN_MASK | RT5682_GP5_PIN_MASK,
246 		RT5682_GP4_PIN_ADCDAT1 | RT5682_GP5_PIN_DACDAT1);
247 	regmap_write(rt5682->regmap, RT5682_TEST_MODE_CTRL_1, 0x0000);
248 	regmap_update_bits(rt5682->regmap, RT5682_BIAS_CUR_CTRL_8,
249 		RT5682_HPA_CP_BIAS_CTRL_MASK, RT5682_HPA_CP_BIAS_3UA);
250 	regmap_update_bits(rt5682->regmap, RT5682_CHARGE_PUMP_1,
251 		RT5682_CP_CLK_HP_MASK, RT5682_CP_CLK_HP_300KHZ);
252 	regmap_update_bits(rt5682->regmap, RT5682_HP_CHARGE_PUMP_1,
253 		RT5682_PM_HP_MASK, RT5682_PM_HP_HV);
254 	regmap_update_bits(rt5682->regmap, RT5682_DMIC_CTRL_1,
255 		RT5682_FIFO_CLK_DIV_MASK, RT5682_FIFO_CLK_DIV_2);
256 
257 	INIT_DELAYED_WORK(&rt5682->jack_detect_work,
258 		rt5682_jack_detect_handler);
259 	INIT_DELAYED_WORK(&rt5682->jd_check_work,
260 		rt5682_jd_check_handler);
261 
262 	if (i2c->irq) {
263 		ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
264 			rt5682_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
265 			| IRQF_ONESHOT, "rt5682", rt5682);
266 		if (!ret)
267 			rt5682->irq = i2c->irq;
268 		else
269 			dev_err(&i2c->dev, "Failed to reguest IRQ: %d\n", ret);
270 	}
271 
272 #ifdef CONFIG_COMMON_CLK
273 	/* Check if MCLK provided */
274 	rt5682->mclk = devm_clk_get_optional(&i2c->dev, "mclk");
275 	if (IS_ERR(rt5682->mclk))
276 		return PTR_ERR(rt5682->mclk);
277 
278 	/* Register CCF DAI clock control */
279 	ret = rt5682_register_dai_clks(rt5682);
280 	if (ret)
281 		return ret;
282 
283 	/* Initial setup for CCF */
284 	rt5682->lrck[RT5682_AIF1] = 48000;
285 #endif
286 
287 	return devm_snd_soc_register_component(&i2c->dev,
288 					       &rt5682_soc_component_dev,
289 					       rt5682_dai, ARRAY_SIZE(rt5682_dai));
290 }
291 
292 static void rt5682_i2c_shutdown(struct i2c_client *client)
293 {
294 	struct rt5682_priv *rt5682 = i2c_get_clientdata(client);
295 
296 	disable_irq(client->irq);
297 	cancel_delayed_work_sync(&rt5682->jack_detect_work);
298 	cancel_delayed_work_sync(&rt5682->jd_check_work);
299 
300 	rt5682_reset(rt5682);
301 }
302 
303 static void rt5682_i2c_remove(struct i2c_client *client)
304 {
305 	rt5682_i2c_shutdown(client);
306 }
307 
308 static const struct of_device_id rt5682_of_match[] = {
309 	{.compatible = "realtek,rt5682i"},
310 	{},
311 };
312 MODULE_DEVICE_TABLE(of, rt5682_of_match);
313 
314 static const struct acpi_device_id rt5682_acpi_match[] = {
315 	{"10EC5682", 0,},
316 	{},
317 };
318 MODULE_DEVICE_TABLE(acpi, rt5682_acpi_match);
319 
320 static const struct i2c_device_id rt5682_i2c_id[] = {
321 	{"rt5682", 0},
322 	{}
323 };
324 MODULE_DEVICE_TABLE(i2c, rt5682_i2c_id);
325 
326 static struct i2c_driver rt5682_i2c_driver = {
327 	.driver = {
328 		.name = "rt5682",
329 		.of_match_table = rt5682_of_match,
330 		.acpi_match_table = rt5682_acpi_match,
331 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
332 	},
333 	.probe = rt5682_i2c_probe,
334 	.remove = rt5682_i2c_remove,
335 	.shutdown = rt5682_i2c_shutdown,
336 	.id_table = rt5682_i2c_id,
337 };
338 module_i2c_driver(rt5682_i2c_driver);
339 
340 MODULE_DESCRIPTION("ASoC RT5682 driver");
341 MODULE_AUTHOR("Bard Liao <bardliao@realtek.com>");
342 MODULE_LICENSE("GPL v2");
343