blob: 039b9f2f8e94700f097cc58dc3a02ace0fae2dcf [file] [log] [blame]
Lucas Tanure8c704612021-08-11 19:56:28 +01001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * HD audio interface patch for Cirrus Logic CS8409 HDA bridge chip
4 *
5 * Copyright (C) 2021 Cirrus Logic, Inc. and
6 * Cirrus Logic International Semiconductor Ltd.
7 */
8
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/module.h>
12#include <sound/core.h>
13#include <linux/mutex.h>
Stefan Binding4ff2ae32021-08-11 19:56:53 +010014#include <linux/iopoll.h>
Lucas Tanure8c704612021-08-11 19:56:28 +010015
16#include "patch_cs8409.h"
17
Lucas Tanure636eb9d22021-08-11 19:56:44 +010018/******************************************************************************
19 * CS8409 Specific Functions
20 ******************************************************************************/
21
Lucas Tanure8c704612021-08-11 19:56:28 +010022static int cs8409_parse_auto_config(struct hda_codec *codec)
23{
24 struct cs8409_spec *spec = codec->spec;
25 int err;
26 int i;
27
28 err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
29 if (err < 0)
30 return err;
31
32 err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
33 if (err < 0)
34 return err;
35
36 /* keep the ADCs powered up when it's dynamically switchable */
37 if (spec->gen.dyn_adc_switch) {
38 unsigned int done = 0;
39
40 for (i = 0; i < spec->gen.input_mux.num_items; i++) {
41 int idx = spec->gen.dyn_adc_idx[i];
42
43 if (done & (1 << idx))
44 continue;
45 snd_hda_gen_fix_pin_power(codec, spec->gen.adc_nids[idx]);
46 done |= 1 << idx;
47 }
48 }
49
50 return 0;
51}
52
Lucas Tanure647d50a2021-08-11 19:56:40 +010053static void cs8409_disable_i2c_clock_worker(struct work_struct *work);
54
Lucas Tanure8c704612021-08-11 19:56:28 +010055static struct cs8409_spec *cs8409_alloc_spec(struct hda_codec *codec)
56{
57 struct cs8409_spec *spec;
58
59 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
60 if (!spec)
61 return NULL;
62 codec->spec = spec;
Lucas Tanure647d50a2021-08-11 19:56:40 +010063 spec->codec = codec;
Lucas Tanure8c704612021-08-11 19:56:28 +010064 codec->power_save_node = 1;
Lucas Tanure636eb9d22021-08-11 19:56:44 +010065 mutex_init(&spec->i2c_mux);
Lucas Tanure647d50a2021-08-11 19:56:40 +010066 INIT_DELAYED_WORK(&spec->i2c_clk_work, cs8409_disable_i2c_clock_worker);
Lucas Tanure8c704612021-08-11 19:56:28 +010067 snd_hda_gen_spec_init(&spec->gen);
68
69 return spec;
70}
71
Lucas Tanure8c704612021-08-11 19:56:28 +010072static inline int cs8409_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
73{
Stefan Bindingccff0062021-08-11 19:56:30 +010074 snd_hda_codec_write(codec, CS8409_PIN_VENDOR_WIDGET, 0, AC_VERB_SET_COEF_INDEX, idx);
75 return snd_hda_codec_read(codec, CS8409_PIN_VENDOR_WIDGET, 0, AC_VERB_GET_PROC_COEF, 0);
Lucas Tanure8c704612021-08-11 19:56:28 +010076}
77
78static inline void cs8409_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
79 unsigned int coef)
80{
Stefan Bindingccff0062021-08-11 19:56:30 +010081 snd_hda_codec_write(codec, CS8409_PIN_VENDOR_WIDGET, 0, AC_VERB_SET_COEF_INDEX, idx);
82 snd_hda_codec_write(codec, CS8409_PIN_VENDOR_WIDGET, 0, AC_VERB_SET_PROC_COEF, coef);
Lucas Tanure8c704612021-08-11 19:56:28 +010083}
84
Lucas Tanure647d50a2021-08-11 19:56:40 +010085/*
86 * cs8409_enable_i2c_clock - Disable I2C clocks
87 * @codec: the codec instance
88 * Disable I2C clocks.
89 * This must be called when the i2c mutex is unlocked.
90 */
91static void cs8409_disable_i2c_clock(struct hda_codec *codec)
92{
93 struct cs8409_spec *spec = codec->spec;
94
Lucas Tanure165b81c2021-08-11 19:56:43 +010095 mutex_lock(&spec->i2c_mux);
Lucas Tanure647d50a2021-08-11 19:56:40 +010096 if (spec->i2c_clck_enabled) {
97 cs8409_vendor_coef_set(spec->codec, 0x0,
98 cs8409_vendor_coef_get(spec->codec, 0x0) & 0xfffffff7);
99 spec->i2c_clck_enabled = 0;
100 }
Lucas Tanure165b81c2021-08-11 19:56:43 +0100101 mutex_unlock(&spec->i2c_mux);
Lucas Tanure647d50a2021-08-11 19:56:40 +0100102}
103
104/*
105 * cs8409_disable_i2c_clock_worker - Worker that disable the I2C Clock after 25ms without use
106 */
107static void cs8409_disable_i2c_clock_worker(struct work_struct *work)
108{
109 struct cs8409_spec *spec = container_of(work, struct cs8409_spec, i2c_clk_work.work);
110
111 cs8409_disable_i2c_clock(spec->codec);
112}
113
114/*
Lucas Tanure8c704612021-08-11 19:56:28 +0100115 * cs8409_enable_i2c_clock - Enable I2C clocks
116 * @codec: the codec instance
Lucas Tanure647d50a2021-08-11 19:56:40 +0100117 * Enable I2C clocks.
118 * This must be called when the i2c mutex is locked.
Lucas Tanure8c704612021-08-11 19:56:28 +0100119 */
Lucas Tanure647d50a2021-08-11 19:56:40 +0100120static void cs8409_enable_i2c_clock(struct hda_codec *codec)
Lucas Tanure8c704612021-08-11 19:56:28 +0100121{
Lucas Tanure647d50a2021-08-11 19:56:40 +0100122 struct cs8409_spec *spec = codec->spec;
Lucas Tanure8c704612021-08-11 19:56:28 +0100123
Lucas Tanure647d50a2021-08-11 19:56:40 +0100124 /* Cancel the disable timer, but do not wait for any running disable functions to finish.
125 * If the disable timer runs out before cancel, the delayed work thread will be blocked,
126 * waiting for the mutex to become unlocked. This mutex will be locked for the duration of
127 * any i2c transaction, so the disable function will run to completion immediately
128 * afterwards in the scenario. The next enable call will re-enable the clock, regardless.
129 */
130 cancel_delayed_work(&spec->i2c_clk_work);
131
132 if (!spec->i2c_clck_enabled) {
133 cs8409_vendor_coef_set(codec, 0x0, cs8409_vendor_coef_get(codec, 0x0) | 0x8);
134 spec->i2c_clck_enabled = 1;
135 }
136 queue_delayed_work(system_power_efficient_wq, &spec->i2c_clk_work, msecs_to_jiffies(25));
Lucas Tanure8c704612021-08-11 19:56:28 +0100137}
138
139/**
140 * cs8409_i2c_wait_complete - Wait for I2C transaction
141 * @codec: the codec instance
142 *
143 * Wait for I2C transaction to complete.
Stefan Binding928adf02021-08-11 19:56:51 +0100144 * Return -ETIMEDOUT if transaction wait times out.
Lucas Tanure8c704612021-08-11 19:56:28 +0100145 */
146static int cs8409_i2c_wait_complete(struct hda_codec *codec)
147{
Lucas Tanure8c704612021-08-11 19:56:28 +0100148 unsigned int retval;
149
Stefan Binding928adf02021-08-11 19:56:51 +0100150 return read_poll_timeout(cs8409_vendor_coef_get, retval, retval & 0x18,
151 CS42L42_I2C_SLEEP_US, CS42L42_I2C_TIMEOUT_US, false, codec, CS8409_I2C_STS);
Lucas Tanure8c704612021-08-11 19:56:28 +0100152}
153
154/**
Lucas Tanured395fd72021-08-11 19:56:41 +0100155 * cs8409_set_i2c_dev_addr - Set i2c address for transaction
156 * @codec: the codec instance
157 * @addr: I2C Address
158 */
159static void cs8409_set_i2c_dev_addr(struct hda_codec *codec, unsigned int addr)
160{
161 struct cs8409_spec *spec = codec->spec;
162
163 if (spec->dev_addr != addr) {
164 cs8409_vendor_coef_set(codec, CS8409_I2C_ADDR, addr);
165 spec->dev_addr = addr;
166 }
167}
168
169/**
Lucas Tanure8de4e5a2021-08-11 19:56:42 +0100170 * cs8409_i2c_set_page - CS8409 I2C set page register.
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100171 * @scodec: the codec instance
Lucas Tanure8de4e5a2021-08-11 19:56:42 +0100172 * @i2c_reg: Page register
173 *
174 * Returns negative on error.
175 */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100176static int cs8409_i2c_set_page(struct sub_codec *scodec, unsigned int i2c_reg)
Lucas Tanure8de4e5a2021-08-11 19:56:42 +0100177{
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100178 struct hda_codec *codec = scodec->codec;
Lucas Tanure8de4e5a2021-08-11 19:56:42 +0100179
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100180 if (scodec->paged && (scodec->last_page != (i2c_reg >> 8))) {
Lucas Tanure8de4e5a2021-08-11 19:56:42 +0100181 cs8409_vendor_coef_set(codec, CS8409_I2C_QWRITE, i2c_reg >> 8);
182 if (cs8409_i2c_wait_complete(codec) < 0)
183 return -EIO;
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100184 scodec->last_page = i2c_reg >> 8;
Lucas Tanure8de4e5a2021-08-11 19:56:42 +0100185 }
186
187 return 0;
188}
189
190/**
Lucas Tanure8c704612021-08-11 19:56:28 +0100191 * cs8409_i2c_read - CS8409 I2C Read.
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100192 * @scodec: the codec instance
Lucas Tanure165b81c2021-08-11 19:56:43 +0100193 * @addr: Register to read
Lucas Tanure8c704612021-08-11 19:56:28 +0100194 *
Lucas Tanure8c704612021-08-11 19:56:28 +0100195 * Returns negative on error, otherwise returns read value in bits 0-7.
196 */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100197static int cs8409_i2c_read(struct sub_codec *scodec, unsigned int addr)
Lucas Tanure8c704612021-08-11 19:56:28 +0100198{
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100199 struct hda_codec *codec = scodec->codec;
Lucas Tanurea1a6c7d2021-08-11 19:56:38 +0100200 struct cs8409_spec *spec = codec->spec;
Lucas Tanure8c704612021-08-11 19:56:28 +0100201 unsigned int i2c_reg_data;
202 unsigned int read_data;
203
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100204 if (scodec->suspended)
Lucas Tanurea1a6c7d2021-08-11 19:56:38 +0100205 return -EPERM;
206
Lucas Tanure165b81c2021-08-11 19:56:43 +0100207 mutex_lock(&spec->i2c_mux);
Lucas Tanure647d50a2021-08-11 19:56:40 +0100208 cs8409_enable_i2c_clock(codec);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100209 cs8409_set_i2c_dev_addr(codec, scodec->addr);
Lucas Tanure8c704612021-08-11 19:56:28 +0100210
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100211 if (cs8409_i2c_set_page(scodec, addr))
212 goto error;
Lucas Tanure8c704612021-08-11 19:56:28 +0100213
Lucas Tanure165b81c2021-08-11 19:56:43 +0100214 i2c_reg_data = (addr << 8) & 0x0ffff;
Stefan Bindingccff0062021-08-11 19:56:30 +0100215 cs8409_vendor_coef_set(codec, CS8409_I2C_QREAD, i2c_reg_data);
Lucas Tanure165b81c2021-08-11 19:56:43 +0100216 if (cs8409_i2c_wait_complete(codec) < 0)
217 goto error;
Lucas Tanure8c704612021-08-11 19:56:28 +0100218
219 /* Register in bits 15-8 and the data in 7-0 */
Stefan Bindingccff0062021-08-11 19:56:30 +0100220 read_data = cs8409_vendor_coef_get(codec, CS8409_I2C_QREAD);
Lucas Tanure8c704612021-08-11 19:56:28 +0100221
Lucas Tanure165b81c2021-08-11 19:56:43 +0100222 mutex_unlock(&spec->i2c_mux);
Stefan Binding4ff2ae32021-08-11 19:56:53 +0100223
Lucas Tanure8c704612021-08-11 19:56:28 +0100224 return read_data & 0x0ff;
Lucas Tanure165b81c2021-08-11 19:56:43 +0100225
226error:
227 mutex_unlock(&spec->i2c_mux);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100228 codec_err(codec, "%s() Failed 0x%02x : 0x%04x\n", __func__, scodec->addr, addr);
Lucas Tanure165b81c2021-08-11 19:56:43 +0100229 return -EIO;
230}
231
232/**
233 * cs8409_i2c_bulk_read - CS8409 I2C Read Sequence.
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100234 * @scodec: the codec instance
Lucas Tanure165b81c2021-08-11 19:56:43 +0100235 * @seq: Register Sequence to read
236 * @count: Number of registeres to read
237 *
238 * Returns negative on error, values are read into value element of cs8409_i2c_param sequence.
239 */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100240static int cs8409_i2c_bulk_read(struct sub_codec *scodec, struct cs8409_i2c_param *seq, int count)
Lucas Tanure165b81c2021-08-11 19:56:43 +0100241{
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100242 struct hda_codec *codec = scodec->codec;
Lucas Tanure165b81c2021-08-11 19:56:43 +0100243 struct cs8409_spec *spec = codec->spec;
244 unsigned int i2c_reg_data;
245 int i;
246
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100247 if (scodec->suspended)
Lucas Tanure165b81c2021-08-11 19:56:43 +0100248 return -EPERM;
249
250 mutex_lock(&spec->i2c_mux);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100251 cs8409_set_i2c_dev_addr(codec, scodec->addr);
Lucas Tanure165b81c2021-08-11 19:56:43 +0100252
253 for (i = 0; i < count; i++) {
254 cs8409_enable_i2c_clock(codec);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100255 if (cs8409_i2c_set_page(scodec, seq[i].addr))
Lucas Tanure165b81c2021-08-11 19:56:43 +0100256 goto error;
257
258 i2c_reg_data = (seq[i].addr << 8) & 0x0ffff;
259 cs8409_vendor_coef_set(codec, CS8409_I2C_QREAD, i2c_reg_data);
260
261 if (cs8409_i2c_wait_complete(codec) < 0)
262 goto error;
263
264 seq[i].value = cs8409_vendor_coef_get(codec, CS8409_I2C_QREAD) & 0xff;
265 }
266
267 mutex_unlock(&spec->i2c_mux);
268
269 return 0;
270
271error:
272 mutex_unlock(&spec->i2c_mux);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100273 codec_err(codec, "I2C Bulk Write Failed 0x%02x\n", scodec->addr);
Lucas Tanure165b81c2021-08-11 19:56:43 +0100274 return -EIO;
Lucas Tanure8c704612021-08-11 19:56:28 +0100275}
276
277/**
278 * cs8409_i2c_write - CS8409 I2C Write.
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100279 * @scodec: the codec instance
Lucas Tanure165b81c2021-08-11 19:56:43 +0100280 * @addr: Register to write to
281 * @value: Data to write
Lucas Tanure8c704612021-08-11 19:56:28 +0100282 *
Lucas Tanure8c704612021-08-11 19:56:28 +0100283 * Returns negative on error, otherwise returns 0.
284 */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100285static int cs8409_i2c_write(struct sub_codec *scodec, unsigned int addr, unsigned int value)
Lucas Tanure8c704612021-08-11 19:56:28 +0100286{
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100287 struct hda_codec *codec = scodec->codec;
Lucas Tanurea1a6c7d2021-08-11 19:56:38 +0100288 struct cs8409_spec *spec = codec->spec;
Lucas Tanure8c704612021-08-11 19:56:28 +0100289 unsigned int i2c_reg_data;
290
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100291 if (scodec->suspended)
Lucas Tanurea1a6c7d2021-08-11 19:56:38 +0100292 return -EPERM;
293
Lucas Tanure165b81c2021-08-11 19:56:43 +0100294 mutex_lock(&spec->i2c_mux);
295
Lucas Tanure647d50a2021-08-11 19:56:40 +0100296 cs8409_enable_i2c_clock(codec);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100297 cs8409_set_i2c_dev_addr(codec, scodec->addr);
Lucas Tanure8c704612021-08-11 19:56:28 +0100298
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100299 if (cs8409_i2c_set_page(scodec, addr))
300 goto error;
Lucas Tanure8c704612021-08-11 19:56:28 +0100301
Lucas Tanure165b81c2021-08-11 19:56:43 +0100302 i2c_reg_data = ((addr << 8) & 0x0ff00) | (value & 0x0ff);
Stefan Bindingccff0062021-08-11 19:56:30 +0100303 cs8409_vendor_coef_set(codec, CS8409_I2C_QWRITE, i2c_reg_data);
Lucas Tanure8c704612021-08-11 19:56:28 +0100304
Lucas Tanure165b81c2021-08-11 19:56:43 +0100305 if (cs8409_i2c_wait_complete(codec) < 0)
306 goto error;
307
308 mutex_unlock(&spec->i2c_mux);
309 return 0;
310
311error:
312 mutex_unlock(&spec->i2c_mux);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100313 codec_err(codec, "%s() Failed 0x%02x : 0x%04x\n", __func__, scodec->addr, addr);
Lucas Tanure165b81c2021-08-11 19:56:43 +0100314 return -EIO;
315}
316
317/**
318 * cs8409_i2c_bulk_write - CS8409 I2C Write Sequence.
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100319 * @scodec: the codec instance
Lucas Tanure165b81c2021-08-11 19:56:43 +0100320 * @seq: Register Sequence to write
321 * @count: Number of registeres to write
322 *
323 * Returns negative on error.
324 */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100325static int cs8409_i2c_bulk_write(struct sub_codec *scodec, const struct cs8409_i2c_param *seq,
326 int count)
Lucas Tanure165b81c2021-08-11 19:56:43 +0100327{
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100328 struct hda_codec *codec = scodec->codec;
Lucas Tanure165b81c2021-08-11 19:56:43 +0100329 struct cs8409_spec *spec = codec->spec;
330 unsigned int i2c_reg_data;
331 int i;
332
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100333 if (scodec->suspended)
Lucas Tanure165b81c2021-08-11 19:56:43 +0100334 return -EPERM;
335
336 mutex_lock(&spec->i2c_mux);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100337 cs8409_set_i2c_dev_addr(codec, scodec->addr);
Lucas Tanure165b81c2021-08-11 19:56:43 +0100338
339 for (i = 0; i < count; i++) {
340 cs8409_enable_i2c_clock(codec);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100341 if (cs8409_i2c_set_page(scodec, seq[i].addr))
Lucas Tanure165b81c2021-08-11 19:56:43 +0100342 goto error;
343
344 i2c_reg_data = ((seq[i].addr << 8) & 0x0ff00) | (seq[i].value & 0x0ff);
345 cs8409_vendor_coef_set(codec, CS8409_I2C_QWRITE, i2c_reg_data);
346
347 if (cs8409_i2c_wait_complete(codec) < 0)
348 goto error;
Lucas Tanure8c704612021-08-11 19:56:28 +0100349 }
350
Lucas Tanure165b81c2021-08-11 19:56:43 +0100351 mutex_unlock(&spec->i2c_mux);
352
Lucas Tanure8c704612021-08-11 19:56:28 +0100353 return 0;
Lucas Tanure165b81c2021-08-11 19:56:43 +0100354
355error:
356 mutex_unlock(&spec->i2c_mux);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100357 codec_err(codec, "I2C Bulk Write Failed 0x%02x\n", scodec->addr);
Lucas Tanure165b81c2021-08-11 19:56:43 +0100358 return -EIO;
Lucas Tanure8c704612021-08-11 19:56:28 +0100359}
360
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100361static int cs8409_init(struct hda_codec *codec)
362{
363 int ret = snd_hda_gen_init(codec);
364
365 if (!ret)
366 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
367
368 return ret;
369}
370
371static int cs8409_build_controls(struct hda_codec *codec)
372{
373 int err;
374
375 err = snd_hda_gen_build_controls(codec);
376 if (err < 0)
377 return err;
378 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
379
380 return 0;
381}
382
Stefan Bindingc076e202021-08-11 19:56:46 +0100383/* Enable/Disable Unsolicited Response */
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100384static void cs8409_enable_ur(struct hda_codec *codec, int flag)
385{
Stefan Bindingc076e202021-08-11 19:56:46 +0100386 struct cs8409_spec *spec = codec->spec;
387 unsigned int ur_gpios = 0;
388 int i;
389
390 for (i = 0; i < spec->num_scodecs; i++)
391 ur_gpios |= spec->scodecs[i]->irq_mask;
392
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100393 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_UNSOLICITED_RSP_MASK,
Stefan Bindingc076e202021-08-11 19:56:46 +0100394 flag ? ur_gpios : 0);
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100395
396 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_UNSOLICITED_ENABLE,
397 flag ? AC_UNSOL_ENABLED : 0);
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100398}
399
400static void cs8409_fix_caps(struct hda_codec *codec, unsigned int nid)
401{
402 int caps;
403
404 /* CS8409 is simple HDA bridge and intended to be used with a remote
405 * companion codec. Most of input/output PIN(s) have only basic
406 * capabilities. Receive and Transmit NID(s) have only OUTC and INC
407 * capabilities and no presence detect capable (PDC) and call to
408 * snd_hda_gen_build_controls() will mark them as non detectable
409 * phantom jacks. However, a companion codec may be
410 * connected to these pins which supports jack detect
411 * capabilities. We have to override pin capabilities,
412 * otherwise they will not be created as input devices.
413 */
414 caps = snd_hdac_read_parm(&codec->core, nid, AC_PAR_PIN_CAP);
415 if (caps >= 0)
416 snd_hdac_override_parm(&codec->core, nid, AC_PAR_PIN_CAP,
417 (caps | (AC_PINCAP_IMP_SENSE | AC_PINCAP_PRES_DETECT)));
418
419 snd_hda_override_wcaps(codec, nid, (get_wcaps(codec, nid) | AC_WCAP_UNSOL_CAP));
420}
421
422/******************************************************************************
423 * CS42L42 Specific Functions
424 ******************************************************************************/
425
426int cs42l42_volume_info(struct snd_kcontrol *kctrl, struct snd_ctl_elem_info *uinfo)
Lucas Tanure8c704612021-08-11 19:56:28 +0100427{
Lucas Tanureb2a88772021-08-11 19:56:39 +0100428 unsigned int ofs = get_amp_offset(kctrl);
Lucas Tanure8c704612021-08-11 19:56:28 +0100429 u8 chs = get_amp_channels(kctrl);
430
Lucas Tanureb2a88772021-08-11 19:56:39 +0100431 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
432 uinfo->value.integer.step = 1;
433 uinfo->count = chs == 3 ? 2 : 1;
434
435 switch (ofs) {
436 case CS42L42_VOL_DAC:
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100437 uinfo->value.integer.min = CS42L42_HP_VOL_REAL_MIN;
438 uinfo->value.integer.max = CS42L42_HP_VOL_REAL_MAX;
Lucas Tanure8c704612021-08-11 19:56:28 +0100439 break;
Lucas Tanureb2a88772021-08-11 19:56:39 +0100440 case CS42L42_VOL_ADC:
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100441 uinfo->value.integer.min = CS42L42_AMIC_VOL_REAL_MIN;
442 uinfo->value.integer.max = CS42L42_AMIC_VOL_REAL_MAX;
Lucas Tanure8c704612021-08-11 19:56:28 +0100443 break;
444 default:
445 break;
446 }
Lucas Tanureb2a88772021-08-11 19:56:39 +0100447
Lucas Tanure8c704612021-08-11 19:56:28 +0100448 return 0;
449}
450
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100451int cs42l42_volume_get(struct snd_kcontrol *kctrl, struct snd_ctl_elem_value *uctrl)
Lucas Tanure8c704612021-08-11 19:56:28 +0100452{
453 struct hda_codec *codec = snd_kcontrol_chip(kctrl);
454 struct cs8409_spec *spec = codec->spec;
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100455 struct sub_codec *cs42l42 = spec->scodecs[get_amp_index(kctrl)];
Lucas Tanure8c704612021-08-11 19:56:28 +0100456 int chs = get_amp_channels(kctrl);
Lucas Tanureb2a88772021-08-11 19:56:39 +0100457 unsigned int ofs = get_amp_offset(kctrl);
Lucas Tanure8c704612021-08-11 19:56:28 +0100458 long *valp = uctrl->value.integer.value;
459
Lucas Tanureb2a88772021-08-11 19:56:39 +0100460 switch (ofs) {
461 case CS42L42_VOL_DAC:
Lucas Tanure8c704612021-08-11 19:56:28 +0100462 if (chs & BIT(0))
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100463 *valp++ = cs42l42->vol[ofs];
Lucas Tanure8c704612021-08-11 19:56:28 +0100464 if (chs & BIT(1))
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100465 *valp = cs42l42->vol[ofs+1];
Lucas Tanure8c704612021-08-11 19:56:28 +0100466 break;
Lucas Tanureb2a88772021-08-11 19:56:39 +0100467 case CS42L42_VOL_ADC:
Lucas Tanure8c704612021-08-11 19:56:28 +0100468 if (chs & BIT(0))
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100469 *valp = cs42l42->vol[ofs];
Lucas Tanure8c704612021-08-11 19:56:28 +0100470 break;
471 default:
472 break;
473 }
Lucas Tanureb2a88772021-08-11 19:56:39 +0100474
Lucas Tanure8c704612021-08-11 19:56:28 +0100475 return 0;
476}
477
Stefan Binding7482ec72021-08-11 19:56:54 +0100478static void cs42l42_mute(struct sub_codec *cs42l42, int vol_type,
479 unsigned int chs, bool mute)
480{
481 if (mute) {
482 if (vol_type == CS42L42_VOL_DAC) {
483 if (chs & BIT(0))
484 cs8409_i2c_write(cs42l42, CS42L42_REG_HS_VOL_CHA, 0x3f);
485 if (chs & BIT(1))
486 cs8409_i2c_write(cs42l42, CS42L42_REG_HS_VOL_CHB, 0x3f);
487 } else if (vol_type == CS42L42_VOL_ADC) {
488 if (chs & BIT(0))
489 cs8409_i2c_write(cs42l42, CS42L42_REG_AMIC_VOL, 0x9f);
490 }
491 } else {
492 if (vol_type == CS42L42_VOL_DAC) {
493 if (chs & BIT(0))
494 cs8409_i2c_write(cs42l42, CS42L42_REG_HS_VOL_CHA,
495 -(cs42l42->vol[CS42L42_DAC_CH0_VOL_OFFSET])
496 & CS42L42_REG_HS_VOL_MASK);
497 if (chs & BIT(1))
498 cs8409_i2c_write(cs42l42, CS42L42_REG_HS_VOL_CHB,
499 -(cs42l42->vol[CS42L42_DAC_CH1_VOL_OFFSET])
500 & CS42L42_REG_HS_VOL_MASK);
501 } else if (vol_type == CS42L42_VOL_ADC) {
502 if (chs & BIT(0))
503 cs8409_i2c_write(cs42l42, CS42L42_REG_AMIC_VOL,
504 cs42l42->vol[CS42L42_ADC_VOL_OFFSET]
505 & CS42L42_REG_AMIC_VOL_MASK);
506 }
507 }
508}
509
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100510int cs42l42_volume_put(struct snd_kcontrol *kctrl, struct snd_ctl_elem_value *uctrl)
Lucas Tanure8c704612021-08-11 19:56:28 +0100511{
512 struct hda_codec *codec = snd_kcontrol_chip(kctrl);
513 struct cs8409_spec *spec = codec->spec;
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100514 struct sub_codec *cs42l42 = spec->scodecs[get_amp_index(kctrl)];
Lucas Tanure8c704612021-08-11 19:56:28 +0100515 int chs = get_amp_channels(kctrl);
Lucas Tanureb2a88772021-08-11 19:56:39 +0100516 unsigned int ofs = get_amp_offset(kctrl);
Lucas Tanure8c704612021-08-11 19:56:28 +0100517 long *valp = uctrl->value.integer.value;
Lucas Tanure8c704612021-08-11 19:56:28 +0100518
Lucas Tanureb2a88772021-08-11 19:56:39 +0100519 switch (ofs) {
520 case CS42L42_VOL_DAC:
Stefan Binding7482ec72021-08-11 19:56:54 +0100521 if (chs & BIT(0))
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100522 cs42l42->vol[ofs] = *valp;
Lucas Tanure8c704612021-08-11 19:56:28 +0100523 if (chs & BIT(1)) {
Lucas Tanureb2a88772021-08-11 19:56:39 +0100524 valp++;
Stefan Binding7482ec72021-08-11 19:56:54 +0100525 cs42l42->vol[ofs + 1] = *valp;
Lucas Tanure8c704612021-08-11 19:56:28 +0100526 }
Stefan Binding7482ec72021-08-11 19:56:54 +0100527 if (spec->playback_started)
528 cs42l42_mute(cs42l42, CS42L42_VOL_DAC, chs, false);
Lucas Tanure8c704612021-08-11 19:56:28 +0100529 break;
Lucas Tanureb2a88772021-08-11 19:56:39 +0100530 case CS42L42_VOL_ADC:
Stefan Binding7482ec72021-08-11 19:56:54 +0100531 if (chs & BIT(0))
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100532 cs42l42->vol[ofs] = *valp;
Stefan Binding7482ec72021-08-11 19:56:54 +0100533 if (spec->capture_started)
534 cs42l42_mute(cs42l42, CS42L42_VOL_ADC, chs, false);
Lucas Tanure8c704612021-08-11 19:56:28 +0100535 break;
536 default:
537 break;
538 }
Lucas Tanureb2a88772021-08-11 19:56:39 +0100539
540 return 0;
Lucas Tanure8c704612021-08-11 19:56:28 +0100541}
542
Stefan Binding7482ec72021-08-11 19:56:54 +0100543static void cs42l42_playback_pcm_hook(struct hda_pcm_stream *hinfo,
544 struct hda_codec *codec,
545 struct snd_pcm_substream *substream,
546 int action)
547{
548 struct cs8409_spec *spec = codec->spec;
549 struct sub_codec *cs42l42;
550 int i;
551 bool mute;
552
553 switch (action) {
554 case HDA_GEN_PCM_ACT_PREPARE:
555 mute = false;
556 spec->playback_started = 1;
557 break;
558 case HDA_GEN_PCM_ACT_CLEANUP:
559 mute = true;
560 spec->playback_started = 0;
561 break;
562 default:
563 return;
564 }
565
566 for (i = 0; i < spec->num_scodecs; i++) {
567 cs42l42 = spec->scodecs[i];
568 cs42l42_mute(cs42l42, CS42L42_VOL_DAC, 0x3, mute);
569 }
570}
571
572static void cs42l42_capture_pcm_hook(struct hda_pcm_stream *hinfo,
573 struct hda_codec *codec,
574 struct snd_pcm_substream *substream,
575 int action)
576{
577 struct cs8409_spec *spec = codec->spec;
578 struct sub_codec *cs42l42;
579 int i;
580 bool mute;
581
582 switch (action) {
583 case HDA_GEN_PCM_ACT_PREPARE:
584 mute = false;
585 spec->capture_started = 1;
586 break;
587 case HDA_GEN_PCM_ACT_CLEANUP:
588 mute = true;
589 spec->capture_started = 0;
590 break;
591 default:
592 return;
593 }
594
595 for (i = 0; i < spec->num_scodecs; i++) {
596 cs42l42 = spec->scodecs[i];
597 cs42l42_mute(cs42l42, CS42L42_VOL_ADC, 0x3, mute);
598 }
599}
600
Lucas Tanure8c704612021-08-11 19:56:28 +0100601/* Configure CS42L42 slave codec for jack autodetect */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100602static void cs42l42_enable_jack_detect(struct sub_codec *cs42l42)
Lucas Tanure8c704612021-08-11 19:56:28 +0100603{
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100604 cs8409_i2c_write(cs42l42, 0x1b70, cs42l42->hsbias_hiz);
Lucas Tanure8c704612021-08-11 19:56:28 +0100605 /* Clear WAKE# */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100606 cs8409_i2c_write(cs42l42, 0x1b71, 0x00C1);
Lucas Tanure8c704612021-08-11 19:56:28 +0100607 /* Wait ~2.5ms */
608 usleep_range(2500, 3000);
609 /* Set mode WAKE# output follows the combination logic directly */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100610 cs8409_i2c_write(cs42l42, 0x1b71, 0x00C0);
Lucas Tanure8c704612021-08-11 19:56:28 +0100611 /* Clear interrupts status */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100612 cs8409_i2c_read(cs42l42, 0x130f);
Lucas Tanure8c704612021-08-11 19:56:28 +0100613 /* Enable interrupt */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100614 cs8409_i2c_write(cs42l42, 0x1320, 0xF3);
Lucas Tanure8c704612021-08-11 19:56:28 +0100615}
616
617/* Enable and run CS42L42 slave codec jack auto detect */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100618static void cs42l42_run_jack_detect(struct sub_codec *cs42l42)
Lucas Tanure8c704612021-08-11 19:56:28 +0100619{
Lucas Tanure8c704612021-08-11 19:56:28 +0100620 /* Clear interrupts */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100621 cs8409_i2c_read(cs42l42, 0x1308);
622 cs8409_i2c_read(cs42l42, 0x1b77);
623 cs8409_i2c_write(cs42l42, 0x1320, 0xFF);
624 cs8409_i2c_read(cs42l42, 0x130f);
Lucas Tanure8c704612021-08-11 19:56:28 +0100625
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100626 cs8409_i2c_write(cs42l42, 0x1102, 0x87);
627 cs8409_i2c_write(cs42l42, 0x1f06, 0x86);
628 cs8409_i2c_write(cs42l42, 0x1b74, 0x07);
629 cs8409_i2c_write(cs42l42, 0x131b, 0xFD);
630 cs8409_i2c_write(cs42l42, 0x1120, 0x80);
Stefan Bindingc8b4f082021-08-11 19:56:52 +0100631 /* Wait ~100us*/
632 usleep_range(100, 200);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100633 cs8409_i2c_write(cs42l42, 0x111f, 0x77);
634 cs8409_i2c_write(cs42l42, 0x1120, 0xc0);
Lucas Tanure8c704612021-08-11 19:56:28 +0100635}
636
Stefan Binding404e7702021-08-11 19:56:47 +0100637static int cs42l42_handle_tip_sense(struct sub_codec *cs42l42, unsigned int reg_ts_status)
638{
639 int status_changed = 0;
640
641 /* TIP_SENSE INSERT/REMOVE */
642 switch (reg_ts_status) {
643 case CS42L42_JACK_INSERTED:
644 if (!cs42l42->hp_jack_in) {
645 if (cs42l42->no_type_dect) {
646 status_changed = 1;
647 cs42l42->hp_jack_in = 1;
648 cs42l42->mic_jack_in = 0;
649 } else {
650 cs42l42_run_jack_detect(cs42l42);
651 }
652 }
653 break;
654
655 case CS42L42_JACK_REMOVED:
656 if (cs42l42->hp_jack_in || cs42l42->mic_jack_in) {
657 status_changed = 1;
658 cs42l42->hp_jack_in = 0;
659 cs42l42->mic_jack_in = 0;
660 }
661 break;
662 default:
663 /* jack in transition */
664 break;
665 }
666
667 return status_changed;
668}
669
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100670static int cs42l42_jack_unsol_event(struct sub_codec *cs42l42)
Lucas Tanure8c704612021-08-11 19:56:28 +0100671{
Lucas Tanure8c704612021-08-11 19:56:28 +0100672 int status_changed = 0;
673 int reg_cdc_status;
674 int reg_hs_status;
675 int reg_ts_status;
676 int type;
Lucas Tanure8c704612021-08-11 19:56:28 +0100677
Lucas Tanure8c704612021-08-11 19:56:28 +0100678 /* Read jack detect status registers */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100679 reg_cdc_status = cs8409_i2c_read(cs42l42, 0x1308);
680 reg_hs_status = cs8409_i2c_read(cs42l42, 0x1124);
681 reg_ts_status = cs8409_i2c_read(cs42l42, 0x130f);
Lucas Tanure8c704612021-08-11 19:56:28 +0100682
Lucas Tanure8c704612021-08-11 19:56:28 +0100683 /* If status values are < 0, read error has occurred. */
684 if (reg_cdc_status < 0 || reg_hs_status < 0 || reg_ts_status < 0)
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100685 return -EIO;
Lucas Tanure8c704612021-08-11 19:56:28 +0100686
687 /* HSDET_AUTO_DONE */
688 if (reg_cdc_status & CS42L42_HSDET_AUTO_DONE) {
689
Stefan Bindingdb0ae842021-08-11 19:56:37 +0100690 /* Disable HSDET_AUTO_DONE */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100691 cs8409_i2c_write(cs42l42, 0x131b, 0xFF);
Stefan Bindingdb0ae842021-08-11 19:56:37 +0100692
Lucas Tanure8c704612021-08-11 19:56:28 +0100693 type = ((reg_hs_status & CS42L42_HSTYPE_MASK) + 1);
Stefan Binding404e7702021-08-11 19:56:47 +0100694
695 if (cs42l42->no_type_dect) {
696 status_changed = cs42l42_handle_tip_sense(cs42l42, reg_ts_status);
697 } else if (type == 4) {
698 /* Type 4 not supported */
699 status_changed = cs42l42_handle_tip_sense(cs42l42, CS42L42_JACK_REMOVED);
700 } else {
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100701 if (!cs42l42->hp_jack_in) {
Lucas Tanure8c704612021-08-11 19:56:28 +0100702 status_changed = 1;
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100703 cs42l42->hp_jack_in = 1;
Lucas Tanure8c704612021-08-11 19:56:28 +0100704 }
705 /* type = 3 has no mic */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100706 if ((!cs42l42->mic_jack_in) && (type != 3)) {
Lucas Tanure8c704612021-08-11 19:56:28 +0100707 status_changed = 1;
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100708 cs42l42->mic_jack_in = 1;
Lucas Tanure8c704612021-08-11 19:56:28 +0100709 }
Lucas Tanure8c704612021-08-11 19:56:28 +0100710 }
Stefan Binding1a048302021-08-12 19:34:32 +0100711 /* Configure the HSDET mode. */
712 cs8409_i2c_write(cs42l42, 0x1120, 0x80);
713 /* Enable the HPOUT ground clamp and configure the HP pull-down */
714 cs8409_i2c_write(cs42l42, 0x1F06, 0x02);
Stefan Bindingdb0ae842021-08-11 19:56:37 +0100715 /* Re-Enable Tip Sense Interrupt */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100716 cs8409_i2c_write(cs42l42, 0x1320, 0xF3);
Lucas Tanure8c704612021-08-11 19:56:28 +0100717 } else {
Stefan Binding404e7702021-08-11 19:56:47 +0100718 status_changed = cs42l42_handle_tip_sense(cs42l42, reg_ts_status);
Lucas Tanure8c704612021-08-11 19:56:28 +0100719 }
720
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100721 return status_changed;
Lucas Tanure8c704612021-08-11 19:56:28 +0100722}
723
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100724static void cs42l42_resume(struct sub_codec *cs42l42)
Stefan Bindingcc7df162021-08-11 19:56:34 +0100725{
Stefan Bindingc076e202021-08-11 19:56:46 +0100726 struct hda_codec *codec = cs42l42->codec;
727 unsigned int gpio_data;
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100728 struct cs8409_i2c_param irq_regs[] = {
729 { 0x1308, 0x00 },
730 { 0x1309, 0x00 },
731 { 0x130A, 0x00 },
732 { 0x130F, 0x00 },
733 };
Stefan Bindingcc7df162021-08-11 19:56:34 +0100734
Stefan Bindingc076e202021-08-11 19:56:46 +0100735 /* Bring CS42L42 out of Reset */
736 gpio_data = snd_hda_codec_read(codec, CS8409_PIN_AFG, 0, AC_VERB_GET_GPIO_DATA, 0);
737 gpio_data |= cs42l42->reset_gpio;
738 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DATA, gpio_data);
739 usleep_range(10000, 15000);
740
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100741 cs42l42->suspended = 0;
Stefan Bindingcc7df162021-08-11 19:56:34 +0100742
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100743 /* Initialize CS42L42 companion codec */
744 cs8409_i2c_bulk_write(cs42l42, cs42l42->init_seq, cs42l42->init_seq_num);
Stefan Bindingc8b4f082021-08-11 19:56:52 +0100745 usleep_range(20000, 25000);
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100746
747 /* Clear interrupts, by reading interrupt status registers */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100748 cs8409_i2c_bulk_read(cs42l42, irq_regs, ARRAY_SIZE(irq_regs));
749
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100750 if (cs42l42->full_scale_vol)
751 cs8409_i2c_write(cs42l42, 0x2001, 0x01);
752
Stefan Binding65cc4ad2021-11-28 11:55:58 +0000753 /* we have to explicitly allow unsol event handling even during the
754 * resume phase so that the jack event is processed properly
755 */
756 snd_hda_codec_allow_unsol_events(cs42l42->codec);
757
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100758 cs42l42_enable_jack_detect(cs42l42);
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100759}
760
761#ifdef CONFIG_PM
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100762static void cs42l42_suspend(struct sub_codec *cs42l42)
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100763{
Stefan Bindingc076e202021-08-11 19:56:46 +0100764 struct hda_codec *codec = cs42l42->codec;
765 unsigned int gpio_data;
Stefan Binding4ff2ae32021-08-11 19:56:53 +0100766 int reg_cdc_status = 0;
767 const struct cs8409_i2c_param cs42l42_pwr_down_seq[] = {
Stefan Binding1a048302021-08-12 19:34:32 +0100768 { 0x1F06, 0x02 },
769 { 0x1129, 0x00 },
Stefan Binding4ff2ae32021-08-11 19:56:53 +0100770 { 0x2301, 0x3F },
771 { 0x2302, 0x3F },
772 { 0x2303, 0x3F },
773 { 0x2001, 0x0F },
774 { 0x2A01, 0x00 },
775 { 0x1207, 0x00 },
776 { 0x1101, 0xFE },
777 { 0x1102, 0x8C },
778 { 0x1101, 0xFF },
779 };
780
781 cs8409_i2c_bulk_write(cs42l42, cs42l42_pwr_down_seq, ARRAY_SIZE(cs42l42_pwr_down_seq));
782
783 if (read_poll_timeout(cs8409_i2c_read, reg_cdc_status,
784 (reg_cdc_status & 0x1), CS42L42_PDN_SLEEP_US, CS42L42_PDN_TIMEOUT_US,
785 true, cs42l42, 0x1308) < 0)
786 codec_warn(codec, "Timeout waiting for PDN_DONE for CS42L42\n");
Stefan Bindingc076e202021-08-11 19:56:46 +0100787
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100788 /* Power down CS42L42 ASP/EQ/MIX/HP */
Stefan Binding4ff2ae32021-08-11 19:56:53 +0100789 cs8409_i2c_write(cs42l42, 0x1102, 0x9C);
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100790 cs42l42->suspended = 1;
791 cs42l42->last_page = 0;
Stefan Binding424e5312021-08-27 12:02:51 +0100792 cs42l42->hp_jack_in = 0;
793 cs42l42->mic_jack_in = 0;
Stefan Bindingc076e202021-08-11 19:56:46 +0100794
795 /* Put CS42L42 into Reset */
796 gpio_data = snd_hda_codec_read(codec, CS8409_PIN_AFG, 0, AC_VERB_GET_GPIO_DATA, 0);
797 gpio_data &= ~cs42l42->reset_gpio;
798 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DATA, gpio_data);
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100799}
800#endif
801
802static void cs8409_free(struct hda_codec *codec)
803{
804 struct cs8409_spec *spec = codec->spec;
805
806 /* Cancel i2c clock disable timer, and disable clock if left enabled */
807 cancel_delayed_work_sync(&spec->i2c_clk_work);
808 cs8409_disable_i2c_clock(codec);
809
810 snd_hda_gen_free(codec);
811}
812
813/******************************************************************************
814 * BULLSEYE / WARLOCK / CYBORG Specific Functions
815 * CS8409/CS42L42
816 ******************************************************************************/
817
818/*
819 * In the case of CS8409 we do not have unsolicited events from NID's 0x24
820 * and 0x34 where hs mic and hp are connected. Companion codec CS42L42 will
821 * generate interrupt via gpio 4 to notify jack events. We have to overwrite
822 * generic snd_hda_jack_unsol_event(), read CS42L42 jack detect status registers
823 * and then notify status via generic snd_hda_jack_unsol_event() call.
824 */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100825static void cs8409_cs42l42_jack_unsol_event(struct hda_codec *codec, unsigned int res)
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100826{
827 struct cs8409_spec *spec = codec->spec;
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100828 struct sub_codec *cs42l42 = spec->scodecs[CS8409_CODEC0];
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100829 struct hda_jack_tbl *jk;
830
831 /* jack_unsol_event() will be called every time gpio line changing state.
832 * In this case gpio4 line goes up as a result of reading interrupt status
833 * registers in previous cs8409_jack_unsol_event() call.
834 * We don't need to handle this event, ignoring...
835 */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100836 if (res & cs42l42->irq_mask)
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100837 return;
838
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100839 if (cs42l42_jack_unsol_event(cs42l42)) {
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100840 snd_hda_set_pin_ctl(codec, CS8409_CS42L42_SPK_PIN_NID,
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100841 cs42l42->hp_jack_in ? 0 : PIN_OUT);
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100842 /* Report jack*/
843 jk = snd_hda_jack_tbl_get_mst(codec, CS8409_CS42L42_HP_PIN_NID, 0);
844 if (jk)
845 snd_hda_jack_unsol_event(codec, (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
846 AC_UNSOL_RES_TAG);
847 /* Report jack*/
848 jk = snd_hda_jack_tbl_get_mst(codec, CS8409_CS42L42_AMIC_PIN_NID, 0);
849 if (jk)
850 snd_hda_jack_unsol_event(codec, (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
851 AC_UNSOL_RES_TAG);
852 }
Stefan Bindingcc7df162021-08-11 19:56:34 +0100853}
854
Lucas Tanure8c704612021-08-11 19:56:28 +0100855#ifdef CONFIG_PM
856/* Manage PDREF, when transition to D3hot */
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100857static int cs8409_cs42l42_suspend(struct hda_codec *codec)
Lucas Tanure8c704612021-08-11 19:56:28 +0100858{
859 struct cs8409_spec *spec = codec->spec;
Stefan Bindingc076e202021-08-11 19:56:46 +0100860 int i;
Lucas Tanure8c704612021-08-11 19:56:28 +0100861
Stefan Binding424e5312021-08-27 12:02:51 +0100862 spec->init_done = 0;
863
Stefan Bindingcc7df162021-08-11 19:56:34 +0100864 cs8409_enable_ur(codec, 0);
865
Stefan Bindingc076e202021-08-11 19:56:46 +0100866 for (i = 0; i < spec->num_scodecs; i++)
867 cs42l42_suspend(spec->scodecs[i]);
Lucas Tanure8c704612021-08-11 19:56:28 +0100868
Lucas Tanure647d50a2021-08-11 19:56:40 +0100869 /* Cancel i2c clock disable timer, and disable clock if left enabled */
870 cancel_delayed_work_sync(&spec->i2c_clk_work);
871 cs8409_disable_i2c_clock(codec);
872
Lucas Tanure8c704612021-08-11 19:56:28 +0100873 snd_hda_shutup_pins(codec);
874
875 return 0;
876}
877#endif
878
Lucas Tanure8c704612021-08-11 19:56:28 +0100879/* Vendor specific HW configuration
880 * PLL, ASP, I2C, SPI, GPIOs, DMIC etc...
881 */
882static void cs8409_cs42l42_hw_init(struct hda_codec *codec)
883{
884 const struct cs8409_cir_param *seq = cs8409_cs42l42_hw_cfg;
885 const struct cs8409_cir_param *seq_bullseye = cs8409_cs42l42_bullseye_atn;
886 struct cs8409_spec *spec = codec->spec;
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100887 struct sub_codec *cs42l42 = spec->scodecs[CS8409_CODEC0];
Lucas Tanure8c704612021-08-11 19:56:28 +0100888
889 if (spec->gpio_mask) {
Stefan Bindingccff0062021-08-11 19:56:30 +0100890 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_MASK,
891 spec->gpio_mask);
892 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DIRECTION,
893 spec->gpio_dir);
894 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DATA,
895 spec->gpio_data);
Lucas Tanure8c704612021-08-11 19:56:28 +0100896 }
897
898 for (; seq->nid; seq++)
899 cs8409_vendor_coef_set(codec, seq->cir, seq->coeff);
900
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100901 if (codec->fixup_id == CS8409_BULLSEYE) {
Lucas Tanure8c704612021-08-11 19:56:28 +0100902 for (; seq_bullseye->nid; seq_bullseye++)
903 cs8409_vendor_coef_set(codec, seq_bullseye->cir, seq_bullseye->coeff);
Lucas Tanure8c704612021-08-11 19:56:28 +0100904 }
905
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100906 /* DMIC1_MO=00b, DMIC1/2_SR=1 */
907 if (codec->fixup_id == CS8409_WARLOCK || codec->fixup_id == CS8409_CYBORG)
908 cs8409_vendor_coef_set(codec, 0x09, 0x0003);
Lucas Tanure8c704612021-08-11 19:56:28 +0100909
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100910 cs42l42_resume(cs42l42);
Lucas Tanure8c704612021-08-11 19:56:28 +0100911
912 /* Enable Unsolicited Response */
913 cs8409_enable_ur(codec, 1);
914}
915
Lucas Tanure8c704612021-08-11 19:56:28 +0100916static const struct hda_codec_ops cs8409_cs42l42_patch_ops = {
917 .build_controls = cs8409_build_controls,
918 .build_pcms = snd_hda_gen_build_pcms,
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100919 .init = cs8409_init,
Lucas Tanure647d50a2021-08-11 19:56:40 +0100920 .free = cs8409_free,
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100921 .unsol_event = cs8409_cs42l42_jack_unsol_event,
Lucas Tanure8c704612021-08-11 19:56:28 +0100922#ifdef CONFIG_PM
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100923 .suspend = cs8409_cs42l42_suspend,
Lucas Tanure8c704612021-08-11 19:56:28 +0100924#endif
925};
926
927static int cs8409_cs42l42_exec_verb(struct hdac_device *dev, unsigned int cmd, unsigned int flags,
928 unsigned int *res)
929{
930 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
931 struct cs8409_spec *spec = codec->spec;
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100932 struct sub_codec *cs42l42 = spec->scodecs[CS8409_CODEC0];
Lucas Tanure8c704612021-08-11 19:56:28 +0100933
934 unsigned int nid = ((cmd >> 20) & 0x07f);
935 unsigned int verb = ((cmd >> 8) & 0x0fff);
936
937 /* CS8409 pins have no AC_PINSENSE_PRESENCE
938 * capabilities. We have to intercept 2 calls for pins 0x24 and 0x34
939 * and return correct pin sense values for read_pin_sense() call from
940 * hda_jack based on CS42L42 jack detect status.
941 */
942 switch (nid) {
943 case CS8409_CS42L42_HP_PIN_NID:
944 if (verb == AC_VERB_GET_PIN_SENSE) {
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100945 *res = (cs42l42->hp_jack_in) ? AC_PINSENSE_PRESENCE : 0;
Lucas Tanure8c704612021-08-11 19:56:28 +0100946 return 0;
947 }
948 break;
Lucas Tanure8c704612021-08-11 19:56:28 +0100949 case CS8409_CS42L42_AMIC_PIN_NID:
950 if (verb == AC_VERB_GET_PIN_SENSE) {
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100951 *res = (cs42l42->mic_jack_in) ? AC_PINSENSE_PRESENCE : 0;
Lucas Tanure8c704612021-08-11 19:56:28 +0100952 return 0;
953 }
954 break;
Lucas Tanure8c704612021-08-11 19:56:28 +0100955 default:
956 break;
957 }
958
959 return spec->exec_verb(dev, cmd, flags, res);
960}
961
Lucas Tanure9e7647b2021-08-11 19:56:29 +0100962void cs8409_cs42l42_fixups(struct hda_codec *codec, const struct hda_fixup *fix, int action)
Lucas Tanure8c704612021-08-11 19:56:28 +0100963{
964 struct cs8409_spec *spec = codec->spec;
Lucas Tanure8c704612021-08-11 19:56:28 +0100965
966 switch (action) {
967 case HDA_FIXUP_ACT_PRE_PROBE:
968 snd_hda_add_verbs(codec, cs8409_cs42l42_init_verbs);
969 /* verb exec op override */
970 spec->exec_verb = codec->core.exec_verb;
971 codec->core.exec_verb = cs8409_cs42l42_exec_verb;
972
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100973 spec->scodecs[CS8409_CODEC0] = &cs8409_cs42l42_codec;
974 spec->num_scodecs = 1;
975 spec->scodecs[CS8409_CODEC0]->codec = codec;
Lucas Tanure8c704612021-08-11 19:56:28 +0100976 codec->patch_ops = cs8409_cs42l42_patch_ops;
977
978 spec->gen.suppress_auto_mute = 1;
979 spec->gen.no_primary_hp = 1;
980 spec->gen.suppress_vmaster = 1;
981
982 /* GPIO 5 out, 3,4 in */
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100983 spec->gpio_dir = spec->scodecs[CS8409_CODEC0]->reset_gpio;
Lucas Tanure8c704612021-08-11 19:56:28 +0100984 spec->gpio_data = 0;
985 spec->gpio_mask = 0x03f;
986
Lucas Tanure8c704612021-08-11 19:56:28 +0100987 /* Basic initial sequence for specific hw configuration */
988 snd_hda_sequence_write(codec, cs8409_cs42l42_init_verbs);
989
Lucas Tanure636eb9d22021-08-11 19:56:44 +0100990 cs8409_fix_caps(codec, CS8409_CS42L42_HP_PIN_NID);
991 cs8409_fix_caps(codec, CS8409_CS42L42_AMIC_PIN_NID);
Lucas Tanure8c704612021-08-11 19:56:28 +0100992
Lucas Tanure24f7ac32021-08-11 19:56:45 +0100993 /* Set TIP_SENSE_EN for analog front-end of tip sense.
994 * Additionally set HSBIAS_SENSE_EN and Full Scale volume for some variants.
995 */
996 switch (codec->fixup_id) {
997 case CS8409_WARLOCK:
998 spec->scodecs[CS8409_CODEC0]->hsbias_hiz = 0x0020;
999 spec->scodecs[CS8409_CODEC0]->full_scale_vol = 1;
1000 break;
1001 case CS8409_BULLSEYE:
1002 spec->scodecs[CS8409_CODEC0]->hsbias_hiz = 0x0020;
1003 spec->scodecs[CS8409_CODEC0]->full_scale_vol = 0;
1004 break;
1005 case CS8409_CYBORG:
1006 spec->scodecs[CS8409_CODEC0]->hsbias_hiz = 0x00a0;
1007 spec->scodecs[CS8409_CODEC0]->full_scale_vol = 1;
1008 break;
1009 default:
1010 spec->scodecs[CS8409_CODEC0]->hsbias_hiz = 0x0003;
1011 spec->scodecs[CS8409_CODEC0]->full_scale_vol = 1;
1012 break;
1013 }
1014
Lucas Tanure8c704612021-08-11 19:56:28 +01001015 break;
1016 case HDA_FIXUP_ACT_PROBE:
Stefan Bindingfed0aac2021-08-11 19:56:50 +01001017 /* Fix Sample Rate to 48kHz */
1018 spec->gen.stream_analog_playback = &cs42l42_48k_pcm_analog_playback;
1019 spec->gen.stream_analog_capture = &cs42l42_48k_pcm_analog_capture;
Stefan Binding7482ec72021-08-11 19:56:54 +01001020 /* add hooks */
1021 spec->gen.pcm_playback_hook = cs42l42_playback_pcm_hook;
1022 spec->gen.pcm_capture_hook = cs42l42_capture_pcm_hook;
Lucas Tanure8c704612021-08-11 19:56:28 +01001023 /* Set initial DMIC volume to -26 dB */
1024 snd_hda_codec_amp_init_stereo(codec, CS8409_CS42L42_DMIC_ADC_PIN_NID,
1025 HDA_INPUT, 0, 0xff, 0x19);
Lucas Tanureb2a88772021-08-11 19:56:39 +01001026 snd_hda_gen_add_kctl(&spec->gen, "Headphone Playback Volume",
1027 &cs42l42_dac_volume_mixer);
1028 snd_hda_gen_add_kctl(&spec->gen, "Mic Capture Volume",
1029 &cs42l42_adc_volume_mixer);
Lucas Tanure134ae782021-08-11 19:56:35 +01001030 /* Disable Unsolicited Response during boot */
1031 cs8409_enable_ur(codec, 0);
Lucas Tanure8c704612021-08-11 19:56:28 +01001032 snd_hda_codec_set_name(codec, "CS8409/CS42L42");
1033 break;
1034 case HDA_FIXUP_ACT_INIT:
1035 cs8409_cs42l42_hw_init(codec);
Stefan Binding424e5312021-08-27 12:02:51 +01001036 spec->init_done = 1;
1037 if (spec->init_done && spec->build_ctrl_done
1038 && !spec->scodecs[CS8409_CODEC0]->hp_jack_in)
1039 cs42l42_run_jack_detect(spec->scodecs[CS8409_CODEC0]);
1040 break;
Lucas Tanure8c704612021-08-11 19:56:28 +01001041 case HDA_FIXUP_ACT_BUILD:
Stefan Binding424e5312021-08-27 12:02:51 +01001042 spec->build_ctrl_done = 1;
Lucas Tanure8c704612021-08-11 19:56:28 +01001043 /* Run jack auto detect first time on boot
1044 * after controls have been added, to check if jack has
1045 * been already plugged in.
1046 * Run immediately after init.
1047 */
Stefan Binding424e5312021-08-27 12:02:51 +01001048 if (spec->init_done && spec->build_ctrl_done
1049 && !spec->scodecs[CS8409_CODEC0]->hp_jack_in)
1050 cs42l42_run_jack_detect(spec->scodecs[CS8409_CODEC0]);
Lucas Tanure8c704612021-08-11 19:56:28 +01001051 break;
1052 default:
1053 break;
1054 }
1055}
1056
Lucas Tanure20e50772021-08-11 19:56:48 +01001057/******************************************************************************
1058 * Dolphin Specific Functions
1059 * CS8409/ 2 X CS42L42
1060 ******************************************************************************/
1061
1062/*
1063 * In the case of CS8409 we do not have unsolicited events when
1064 * hs mic and hp are connected. Companion codec CS42L42 will
1065 * generate interrupt via irq_mask to notify jack events. We have to overwrite
1066 * generic snd_hda_jack_unsol_event(), read CS42L42 jack detect status registers
1067 * and then notify status via generic snd_hda_jack_unsol_event() call.
1068 */
1069static void dolphin_jack_unsol_event(struct hda_codec *codec, unsigned int res)
1070{
1071 struct cs8409_spec *spec = codec->spec;
1072 struct sub_codec *cs42l42;
1073 struct hda_jack_tbl *jk;
1074
1075 cs42l42 = spec->scodecs[CS8409_CODEC0];
1076 if (!cs42l42->suspended && (~res & cs42l42->irq_mask) &&
1077 cs42l42_jack_unsol_event(cs42l42)) {
1078 jk = snd_hda_jack_tbl_get_mst(codec, DOLPHIN_HP_PIN_NID, 0);
1079 if (jk)
1080 snd_hda_jack_unsol_event(codec,
1081 (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
1082 AC_UNSOL_RES_TAG);
1083
1084 jk = snd_hda_jack_tbl_get_mst(codec, DOLPHIN_AMIC_PIN_NID, 0);
1085 if (jk)
1086 snd_hda_jack_unsol_event(codec,
1087 (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
1088 AC_UNSOL_RES_TAG);
1089 }
1090
1091 cs42l42 = spec->scodecs[CS8409_CODEC1];
1092 if (!cs42l42->suspended && (~res & cs42l42->irq_mask) &&
1093 cs42l42_jack_unsol_event(cs42l42)) {
1094 jk = snd_hda_jack_tbl_get_mst(codec, DOLPHIN_LO_PIN_NID, 0);
1095 if (jk)
1096 snd_hda_jack_unsol_event(codec,
1097 (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
1098 AC_UNSOL_RES_TAG);
1099 }
1100}
1101
1102/* Vendor specific HW configuration
1103 * PLL, ASP, I2C, SPI, GPIOs, DMIC etc...
1104 */
1105static void dolphin_hw_init(struct hda_codec *codec)
1106{
1107 const struct cs8409_cir_param *seq = dolphin_hw_cfg;
1108 struct cs8409_spec *spec = codec->spec;
1109 struct sub_codec *cs42l42;
1110 int i;
1111
1112 if (spec->gpio_mask) {
1113 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_MASK,
1114 spec->gpio_mask);
1115 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DIRECTION,
1116 spec->gpio_dir);
1117 snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DATA,
1118 spec->gpio_data);
1119 }
1120
1121 for (; seq->nid; seq++)
1122 cs8409_vendor_coef_set(codec, seq->cir, seq->coeff);
1123
1124 for (i = 0; i < spec->num_scodecs; i++) {
1125 cs42l42 = spec->scodecs[i];
1126 cs42l42_resume(cs42l42);
1127 }
1128
1129 /* Enable Unsolicited Response */
1130 cs8409_enable_ur(codec, 1);
1131}
1132
1133static const struct hda_codec_ops cs8409_dolphin_patch_ops = {
1134 .build_controls = cs8409_build_controls,
1135 .build_pcms = snd_hda_gen_build_pcms,
1136 .init = cs8409_init,
1137 .free = cs8409_free,
1138 .unsol_event = dolphin_jack_unsol_event,
1139#ifdef CONFIG_PM
1140 .suspend = cs8409_cs42l42_suspend,
1141#endif
1142};
1143
1144static int dolphin_exec_verb(struct hdac_device *dev, unsigned int cmd, unsigned int flags,
1145 unsigned int *res)
1146{
1147 struct hda_codec *codec = container_of(dev, struct hda_codec, core);
1148 struct cs8409_spec *spec = codec->spec;
1149 struct sub_codec *cs42l42 = spec->scodecs[CS8409_CODEC0];
1150
1151 unsigned int nid = ((cmd >> 20) & 0x07f);
1152 unsigned int verb = ((cmd >> 8) & 0x0fff);
1153
1154 /* CS8409 pins have no AC_PINSENSE_PRESENCE
1155 * capabilities. We have to intercept calls for CS42L42 pins
1156 * and return correct pin sense values for read_pin_sense() call from
1157 * hda_jack based on CS42L42 jack detect status.
1158 */
1159 switch (nid) {
1160 case DOLPHIN_HP_PIN_NID:
1161 case DOLPHIN_LO_PIN_NID:
1162 if (nid == DOLPHIN_LO_PIN_NID)
1163 cs42l42 = spec->scodecs[CS8409_CODEC1];
1164 if (verb == AC_VERB_GET_PIN_SENSE) {
1165 *res = (cs42l42->hp_jack_in) ? AC_PINSENSE_PRESENCE : 0;
1166 return 0;
1167 }
1168 break;
1169 case DOLPHIN_AMIC_PIN_NID:
1170 if (verb == AC_VERB_GET_PIN_SENSE) {
1171 *res = (cs42l42->mic_jack_in) ? AC_PINSENSE_PRESENCE : 0;
1172 return 0;
1173 }
1174 break;
1175 default:
1176 break;
1177 }
1178
1179 return spec->exec_verb(dev, cmd, flags, res);
1180}
1181
1182void dolphin_fixups(struct hda_codec *codec, const struct hda_fixup *fix, int action)
1183{
1184 struct cs8409_spec *spec = codec->spec;
1185 struct snd_kcontrol_new *kctrl;
1186 int i;
1187
1188 switch (action) {
1189 case HDA_FIXUP_ACT_PRE_PROBE:
1190 snd_hda_add_verbs(codec, dolphin_init_verbs);
1191 /* verb exec op override */
1192 spec->exec_verb = codec->core.exec_verb;
1193 codec->core.exec_verb = dolphin_exec_verb;
1194
1195 spec->scodecs[CS8409_CODEC0] = &dolphin_cs42l42_0;
1196 spec->scodecs[CS8409_CODEC0]->codec = codec;
1197 spec->scodecs[CS8409_CODEC1] = &dolphin_cs42l42_1;
1198 spec->scodecs[CS8409_CODEC1]->codec = codec;
1199 spec->num_scodecs = 2;
1200
1201 codec->patch_ops = cs8409_dolphin_patch_ops;
1202
1203 /* GPIO 1,5 out, 0,4 in */
1204 spec->gpio_dir = spec->scodecs[CS8409_CODEC0]->reset_gpio |
1205 spec->scodecs[CS8409_CODEC1]->reset_gpio;
1206 spec->gpio_data = 0;
1207 spec->gpio_mask = 0x03f;
1208
1209 /* Basic initial sequence for specific hw configuration */
1210 snd_hda_sequence_write(codec, dolphin_init_verbs);
1211
1212 snd_hda_jack_add_kctl(codec, DOLPHIN_LO_PIN_NID, "Line Out", true,
1213 SND_JACK_HEADPHONE, NULL);
1214
Stefan Binding94d508f2021-09-16 10:56:46 +01001215 snd_hda_jack_add_kctl(codec, DOLPHIN_AMIC_PIN_NID, "Microphone", true,
1216 SND_JACK_MICROPHONE, NULL);
1217
Lucas Tanure20e50772021-08-11 19:56:48 +01001218 cs8409_fix_caps(codec, DOLPHIN_HP_PIN_NID);
1219 cs8409_fix_caps(codec, DOLPHIN_LO_PIN_NID);
1220 cs8409_fix_caps(codec, DOLPHIN_AMIC_PIN_NID);
1221
1222 break;
1223 case HDA_FIXUP_ACT_PROBE:
Stefan Bindingfed0aac2021-08-11 19:56:50 +01001224 /* Fix Sample Rate to 48kHz */
1225 spec->gen.stream_analog_playback = &cs42l42_48k_pcm_analog_playback;
1226 spec->gen.stream_analog_capture = &cs42l42_48k_pcm_analog_capture;
Stefan Binding7482ec72021-08-11 19:56:54 +01001227 /* add hooks */
1228 spec->gen.pcm_playback_hook = cs42l42_playback_pcm_hook;
1229 spec->gen.pcm_capture_hook = cs42l42_capture_pcm_hook;
Lucas Tanure20e50772021-08-11 19:56:48 +01001230 snd_hda_gen_add_kctl(&spec->gen, "Headphone Playback Volume",
1231 &cs42l42_dac_volume_mixer);
1232 snd_hda_gen_add_kctl(&spec->gen, "Mic Capture Volume", &cs42l42_adc_volume_mixer);
1233 kctrl = snd_hda_gen_add_kctl(&spec->gen, "Line Out Playback Volume",
1234 &cs42l42_dac_volume_mixer);
1235 /* Update Line Out kcontrol template */
1236 kctrl->private_value = HDA_COMPOSE_AMP_VAL_OFS(DOLPHIN_HP_PIN_NID, 3, CS8409_CODEC1,
1237 HDA_OUTPUT, CS42L42_VOL_DAC) | HDA_AMP_VAL_MIN_MUTE;
1238 cs8409_enable_ur(codec, 0);
Lucas Tanure20e50772021-08-11 19:56:48 +01001239 snd_hda_codec_set_name(codec, "CS8409/CS42L42");
1240 break;
1241 case HDA_FIXUP_ACT_INIT:
1242 dolphin_hw_init(codec);
Stefan Binding424e5312021-08-27 12:02:51 +01001243 spec->init_done = 1;
1244 if (spec->init_done && spec->build_ctrl_done) {
1245 for (i = 0; i < spec->num_scodecs; i++) {
1246 if (!spec->scodecs[i]->hp_jack_in)
1247 cs42l42_run_jack_detect(spec->scodecs[i]);
1248 }
1249 }
1250 break;
Lucas Tanure20e50772021-08-11 19:56:48 +01001251 case HDA_FIXUP_ACT_BUILD:
Stefan Binding424e5312021-08-27 12:02:51 +01001252 spec->build_ctrl_done = 1;
Lucas Tanure20e50772021-08-11 19:56:48 +01001253 /* Run jack auto detect first time on boot
1254 * after controls have been added, to check if jack has
1255 * been already plugged in.
1256 * Run immediately after init.
1257 */
Stefan Binding424e5312021-08-27 12:02:51 +01001258 if (spec->init_done && spec->build_ctrl_done) {
1259 for (i = 0; i < spec->num_scodecs; i++) {
1260 if (!spec->scodecs[i]->hp_jack_in)
1261 cs42l42_run_jack_detect(spec->scodecs[i]);
1262 }
1263 }
Lucas Tanure20e50772021-08-11 19:56:48 +01001264 break;
1265 default:
1266 break;
1267 }
1268}
1269
Lucas Tanure8c704612021-08-11 19:56:28 +01001270static int patch_cs8409(struct hda_codec *codec)
1271{
1272 int err;
1273
1274 if (!cs8409_alloc_spec(codec))
1275 return -ENOMEM;
1276
1277 snd_hda_pick_fixup(codec, cs8409_models, cs8409_fixup_tbl, cs8409_fixups);
1278
1279 codec_dbg(codec, "Picked ID=%d, VID=%08x, DEV=%08x\n", codec->fixup_id,
1280 codec->bus->pci->subsystem_vendor,
1281 codec->bus->pci->subsystem_device);
1282
1283 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1284
1285 err = cs8409_parse_auto_config(codec);
1286 if (err < 0) {
Lucas Tanure647d50a2021-08-11 19:56:40 +01001287 cs8409_free(codec);
Lucas Tanure8c704612021-08-11 19:56:28 +01001288 return err;
1289 }
1290
1291 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1292 return 0;
1293}
1294
1295static const struct hda_device_id snd_hda_id_cs8409[] = {
1296 HDA_CODEC_ENTRY(0x10138409, "CS8409", patch_cs8409),
1297 {} /* terminator */
1298};
1299MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_cs8409);
1300
1301static struct hda_codec_driver cs8409_driver = {
1302 .id = snd_hda_id_cs8409,
1303};
1304module_hda_codec_driver(cs8409_driver);
1305
1306MODULE_LICENSE("GPL");
1307MODULE_DESCRIPTION("Cirrus Logic HDA bridge");