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