patch_cs8409.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. // 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. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <sound/core.h>
  12. #include <linux/mutex.h>
  13. #include <linux/iopoll.h>
  14. #include "patch_cs8409.h"
  15. /******************************************************************************
  16. * CS8409 Specific Functions
  17. ******************************************************************************/
  18. static int cs8409_parse_auto_config(struct hda_codec *codec)
  19. {
  20. struct cs8409_spec *spec = codec->spec;
  21. int err;
  22. int i;
  23. err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL, 0);
  24. if (err < 0)
  25. return err;
  26. err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
  27. if (err < 0)
  28. return err;
  29. /* keep the ADCs powered up when it's dynamically switchable */
  30. if (spec->gen.dyn_adc_switch) {
  31. unsigned int done = 0;
  32. for (i = 0; i < spec->gen.input_mux.num_items; i++) {
  33. int idx = spec->gen.dyn_adc_idx[i];
  34. if (done & (1 << idx))
  35. continue;
  36. snd_hda_gen_fix_pin_power(codec, spec->gen.adc_nids[idx]);
  37. done |= 1 << idx;
  38. }
  39. }
  40. return 0;
  41. }
  42. static void cs8409_disable_i2c_clock_worker(struct work_struct *work);
  43. static struct cs8409_spec *cs8409_alloc_spec(struct hda_codec *codec)
  44. {
  45. struct cs8409_spec *spec;
  46. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  47. if (!spec)
  48. return NULL;
  49. codec->spec = spec;
  50. spec->codec = codec;
  51. codec->power_save_node = 1;
  52. mutex_init(&spec->i2c_mux);
  53. INIT_DELAYED_WORK(&spec->i2c_clk_work, cs8409_disable_i2c_clock_worker);
  54. snd_hda_gen_spec_init(&spec->gen);
  55. return spec;
  56. }
  57. static inline int cs8409_vendor_coef_get(struct hda_codec *codec, unsigned int idx)
  58. {
  59. snd_hda_codec_write(codec, CS8409_PIN_VENDOR_WIDGET, 0, AC_VERB_SET_COEF_INDEX, idx);
  60. return snd_hda_codec_read(codec, CS8409_PIN_VENDOR_WIDGET, 0, AC_VERB_GET_PROC_COEF, 0);
  61. }
  62. static inline void cs8409_vendor_coef_set(struct hda_codec *codec, unsigned int idx,
  63. unsigned int coef)
  64. {
  65. snd_hda_codec_write(codec, CS8409_PIN_VENDOR_WIDGET, 0, AC_VERB_SET_COEF_INDEX, idx);
  66. snd_hda_codec_write(codec, CS8409_PIN_VENDOR_WIDGET, 0, AC_VERB_SET_PROC_COEF, coef);
  67. }
  68. /*
  69. * cs8409_enable_i2c_clock - Disable I2C clocks
  70. * @codec: the codec instance
  71. * Disable I2C clocks.
  72. * This must be called when the i2c mutex is unlocked.
  73. */
  74. static void cs8409_disable_i2c_clock(struct hda_codec *codec)
  75. {
  76. struct cs8409_spec *spec = codec->spec;
  77. mutex_lock(&spec->i2c_mux);
  78. if (spec->i2c_clck_enabled) {
  79. cs8409_vendor_coef_set(spec->codec, 0x0,
  80. cs8409_vendor_coef_get(spec->codec, 0x0) & 0xfffffff7);
  81. spec->i2c_clck_enabled = 0;
  82. }
  83. mutex_unlock(&spec->i2c_mux);
  84. }
  85. /*
  86. * cs8409_disable_i2c_clock_worker - Worker that disable the I2C Clock after 25ms without use
  87. */
  88. static void cs8409_disable_i2c_clock_worker(struct work_struct *work)
  89. {
  90. struct cs8409_spec *spec = container_of(work, struct cs8409_spec, i2c_clk_work.work);
  91. cs8409_disable_i2c_clock(spec->codec);
  92. }
  93. /*
  94. * cs8409_enable_i2c_clock - Enable I2C clocks
  95. * @codec: the codec instance
  96. * Enable I2C clocks.
  97. * This must be called when the i2c mutex is locked.
  98. */
  99. static void cs8409_enable_i2c_clock(struct hda_codec *codec)
  100. {
  101. struct cs8409_spec *spec = codec->spec;
  102. /* Cancel the disable timer, but do not wait for any running disable functions to finish.
  103. * If the disable timer runs out before cancel, the delayed work thread will be blocked,
  104. * waiting for the mutex to become unlocked. This mutex will be locked for the duration of
  105. * any i2c transaction, so the disable function will run to completion immediately
  106. * afterwards in the scenario. The next enable call will re-enable the clock, regardless.
  107. */
  108. cancel_delayed_work(&spec->i2c_clk_work);
  109. if (!spec->i2c_clck_enabled) {
  110. cs8409_vendor_coef_set(codec, 0x0, cs8409_vendor_coef_get(codec, 0x0) | 0x8);
  111. spec->i2c_clck_enabled = 1;
  112. }
  113. queue_delayed_work(system_power_efficient_wq, &spec->i2c_clk_work, msecs_to_jiffies(25));
  114. }
  115. /**
  116. * cs8409_i2c_wait_complete - Wait for I2C transaction
  117. * @codec: the codec instance
  118. *
  119. * Wait for I2C transaction to complete.
  120. * Return -ETIMEDOUT if transaction wait times out.
  121. */
  122. static int cs8409_i2c_wait_complete(struct hda_codec *codec)
  123. {
  124. unsigned int retval;
  125. return read_poll_timeout(cs8409_vendor_coef_get, retval, retval & 0x18,
  126. CS42L42_I2C_SLEEP_US, CS42L42_I2C_TIMEOUT_US, false, codec, CS8409_I2C_STS);
  127. }
  128. /**
  129. * cs8409_set_i2c_dev_addr - Set i2c address for transaction
  130. * @codec: the codec instance
  131. * @addr: I2C Address
  132. */
  133. static void cs8409_set_i2c_dev_addr(struct hda_codec *codec, unsigned int addr)
  134. {
  135. struct cs8409_spec *spec = codec->spec;
  136. if (spec->dev_addr != addr) {
  137. cs8409_vendor_coef_set(codec, CS8409_I2C_ADDR, addr);
  138. spec->dev_addr = addr;
  139. }
  140. }
  141. /**
  142. * cs8409_i2c_set_page - CS8409 I2C set page register.
  143. * @scodec: the codec instance
  144. * @i2c_reg: Page register
  145. *
  146. * Returns negative on error.
  147. */
  148. static int cs8409_i2c_set_page(struct sub_codec *scodec, unsigned int i2c_reg)
  149. {
  150. struct hda_codec *codec = scodec->codec;
  151. if (scodec->paged && (scodec->last_page != (i2c_reg >> 8))) {
  152. cs8409_vendor_coef_set(codec, CS8409_I2C_QWRITE, i2c_reg >> 8);
  153. if (cs8409_i2c_wait_complete(codec) < 0)
  154. return -EIO;
  155. scodec->last_page = i2c_reg >> 8;
  156. }
  157. return 0;
  158. }
  159. /**
  160. * cs8409_i2c_read - CS8409 I2C Read.
  161. * @scodec: the codec instance
  162. * @addr: Register to read
  163. *
  164. * Returns negative on error, otherwise returns read value in bits 0-7.
  165. */
  166. static int cs8409_i2c_read(struct sub_codec *scodec, unsigned int addr)
  167. {
  168. struct hda_codec *codec = scodec->codec;
  169. struct cs8409_spec *spec = codec->spec;
  170. unsigned int i2c_reg_data;
  171. unsigned int read_data;
  172. if (scodec->suspended)
  173. return -EPERM;
  174. mutex_lock(&spec->i2c_mux);
  175. cs8409_enable_i2c_clock(codec);
  176. cs8409_set_i2c_dev_addr(codec, scodec->addr);
  177. if (cs8409_i2c_set_page(scodec, addr))
  178. goto error;
  179. i2c_reg_data = (addr << 8) & 0x0ffff;
  180. cs8409_vendor_coef_set(codec, CS8409_I2C_QREAD, i2c_reg_data);
  181. if (cs8409_i2c_wait_complete(codec) < 0)
  182. goto error;
  183. /* Register in bits 15-8 and the data in 7-0 */
  184. read_data = cs8409_vendor_coef_get(codec, CS8409_I2C_QREAD);
  185. mutex_unlock(&spec->i2c_mux);
  186. return read_data & 0x0ff;
  187. error:
  188. mutex_unlock(&spec->i2c_mux);
  189. codec_err(codec, "%s() Failed 0x%02x : 0x%04x\n", __func__, scodec->addr, addr);
  190. return -EIO;
  191. }
  192. /**
  193. * cs8409_i2c_bulk_read - CS8409 I2C Read Sequence.
  194. * @scodec: the codec instance
  195. * @seq: Register Sequence to read
  196. * @count: Number of registeres to read
  197. *
  198. * Returns negative on error, values are read into value element of cs8409_i2c_param sequence.
  199. */
  200. static int cs8409_i2c_bulk_read(struct sub_codec *scodec, struct cs8409_i2c_param *seq, int count)
  201. {
  202. struct hda_codec *codec = scodec->codec;
  203. struct cs8409_spec *spec = codec->spec;
  204. unsigned int i2c_reg_data;
  205. int i;
  206. if (scodec->suspended)
  207. return -EPERM;
  208. mutex_lock(&spec->i2c_mux);
  209. cs8409_set_i2c_dev_addr(codec, scodec->addr);
  210. for (i = 0; i < count; i++) {
  211. cs8409_enable_i2c_clock(codec);
  212. if (cs8409_i2c_set_page(scodec, seq[i].addr))
  213. goto error;
  214. i2c_reg_data = (seq[i].addr << 8) & 0x0ffff;
  215. cs8409_vendor_coef_set(codec, CS8409_I2C_QREAD, i2c_reg_data);
  216. if (cs8409_i2c_wait_complete(codec) < 0)
  217. goto error;
  218. seq[i].value = cs8409_vendor_coef_get(codec, CS8409_I2C_QREAD) & 0xff;
  219. }
  220. mutex_unlock(&spec->i2c_mux);
  221. return 0;
  222. error:
  223. mutex_unlock(&spec->i2c_mux);
  224. codec_err(codec, "I2C Bulk Write Failed 0x%02x\n", scodec->addr);
  225. return -EIO;
  226. }
  227. /**
  228. * cs8409_i2c_write - CS8409 I2C Write.
  229. * @scodec: the codec instance
  230. * @addr: Register to write to
  231. * @value: Data to write
  232. *
  233. * Returns negative on error, otherwise returns 0.
  234. */
  235. static int cs8409_i2c_write(struct sub_codec *scodec, unsigned int addr, unsigned int value)
  236. {
  237. struct hda_codec *codec = scodec->codec;
  238. struct cs8409_spec *spec = codec->spec;
  239. unsigned int i2c_reg_data;
  240. if (scodec->suspended)
  241. return -EPERM;
  242. mutex_lock(&spec->i2c_mux);
  243. cs8409_enable_i2c_clock(codec);
  244. cs8409_set_i2c_dev_addr(codec, scodec->addr);
  245. if (cs8409_i2c_set_page(scodec, addr))
  246. goto error;
  247. i2c_reg_data = ((addr << 8) & 0x0ff00) | (value & 0x0ff);
  248. cs8409_vendor_coef_set(codec, CS8409_I2C_QWRITE, i2c_reg_data);
  249. if (cs8409_i2c_wait_complete(codec) < 0)
  250. goto error;
  251. mutex_unlock(&spec->i2c_mux);
  252. return 0;
  253. error:
  254. mutex_unlock(&spec->i2c_mux);
  255. codec_err(codec, "%s() Failed 0x%02x : 0x%04x\n", __func__, scodec->addr, addr);
  256. return -EIO;
  257. }
  258. /**
  259. * cs8409_i2c_bulk_write - CS8409 I2C Write Sequence.
  260. * @scodec: the codec instance
  261. * @seq: Register Sequence to write
  262. * @count: Number of registeres to write
  263. *
  264. * Returns negative on error.
  265. */
  266. static int cs8409_i2c_bulk_write(struct sub_codec *scodec, const struct cs8409_i2c_param *seq,
  267. int count)
  268. {
  269. struct hda_codec *codec = scodec->codec;
  270. struct cs8409_spec *spec = codec->spec;
  271. unsigned int i2c_reg_data;
  272. int i;
  273. if (scodec->suspended)
  274. return -EPERM;
  275. mutex_lock(&spec->i2c_mux);
  276. cs8409_set_i2c_dev_addr(codec, scodec->addr);
  277. for (i = 0; i < count; i++) {
  278. cs8409_enable_i2c_clock(codec);
  279. if (cs8409_i2c_set_page(scodec, seq[i].addr))
  280. goto error;
  281. i2c_reg_data = ((seq[i].addr << 8) & 0x0ff00) | (seq[i].value & 0x0ff);
  282. cs8409_vendor_coef_set(codec, CS8409_I2C_QWRITE, i2c_reg_data);
  283. if (cs8409_i2c_wait_complete(codec) < 0)
  284. goto error;
  285. }
  286. mutex_unlock(&spec->i2c_mux);
  287. return 0;
  288. error:
  289. mutex_unlock(&spec->i2c_mux);
  290. codec_err(codec, "I2C Bulk Write Failed 0x%02x\n", scodec->addr);
  291. return -EIO;
  292. }
  293. static int cs8409_init(struct hda_codec *codec)
  294. {
  295. int ret = snd_hda_gen_init(codec);
  296. if (!ret)
  297. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
  298. return ret;
  299. }
  300. static int cs8409_build_controls(struct hda_codec *codec)
  301. {
  302. int err;
  303. err = snd_hda_gen_build_controls(codec);
  304. if (err < 0)
  305. return err;
  306. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
  307. return 0;
  308. }
  309. /* Enable/Disable Unsolicited Response */
  310. static void cs8409_enable_ur(struct hda_codec *codec, int flag)
  311. {
  312. struct cs8409_spec *spec = codec->spec;
  313. unsigned int ur_gpios = 0;
  314. int i;
  315. for (i = 0; i < spec->num_scodecs; i++)
  316. ur_gpios |= spec->scodecs[i]->irq_mask;
  317. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_UNSOLICITED_RSP_MASK,
  318. flag ? ur_gpios : 0);
  319. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_UNSOLICITED_ENABLE,
  320. flag ? AC_UNSOL_ENABLED : 0);
  321. }
  322. static void cs8409_fix_caps(struct hda_codec *codec, unsigned int nid)
  323. {
  324. int caps;
  325. /* CS8409 is simple HDA bridge and intended to be used with a remote
  326. * companion codec. Most of input/output PIN(s) have only basic
  327. * capabilities. Receive and Transmit NID(s) have only OUTC and INC
  328. * capabilities and no presence detect capable (PDC) and call to
  329. * snd_hda_gen_build_controls() will mark them as non detectable
  330. * phantom jacks. However, a companion codec may be
  331. * connected to these pins which supports jack detect
  332. * capabilities. We have to override pin capabilities,
  333. * otherwise they will not be created as input devices.
  334. */
  335. caps = snd_hdac_read_parm(&codec->core, nid, AC_PAR_PIN_CAP);
  336. if (caps >= 0)
  337. snd_hdac_override_parm(&codec->core, nid, AC_PAR_PIN_CAP,
  338. (caps | (AC_PINCAP_IMP_SENSE | AC_PINCAP_PRES_DETECT)));
  339. snd_hda_override_wcaps(codec, nid, (get_wcaps(codec, nid) | AC_WCAP_UNSOL_CAP));
  340. }
  341. static int cs8409_spk_sw_gpio_get(struct snd_kcontrol *kcontrol,
  342. struct snd_ctl_elem_value *ucontrol)
  343. {
  344. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  345. struct cs8409_spec *spec = codec->spec;
  346. ucontrol->value.integer.value[0] = !!(spec->gpio_data & spec->speaker_pdn_gpio);
  347. return 0;
  348. }
  349. static int cs8409_spk_sw_gpio_put(struct snd_kcontrol *kcontrol,
  350. struct snd_ctl_elem_value *ucontrol)
  351. {
  352. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  353. struct cs8409_spec *spec = codec->spec;
  354. unsigned int gpio_data;
  355. gpio_data = (spec->gpio_data & ~spec->speaker_pdn_gpio) |
  356. (ucontrol->value.integer.value[0] ? spec->speaker_pdn_gpio : 0);
  357. if (gpio_data == spec->gpio_data)
  358. return 0;
  359. spec->gpio_data = gpio_data;
  360. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DATA, spec->gpio_data);
  361. return 1;
  362. }
  363. static const struct snd_kcontrol_new cs8409_spk_sw_ctrl = {
  364. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  365. .info = snd_ctl_boolean_mono_info,
  366. .get = cs8409_spk_sw_gpio_get,
  367. .put = cs8409_spk_sw_gpio_put,
  368. };
  369. /******************************************************************************
  370. * CS42L42 Specific Functions
  371. ******************************************************************************/
  372. int cs42l42_volume_info(struct snd_kcontrol *kctrl, struct snd_ctl_elem_info *uinfo)
  373. {
  374. unsigned int ofs = get_amp_offset(kctrl);
  375. u8 chs = get_amp_channels(kctrl);
  376. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  377. uinfo->value.integer.step = 1;
  378. uinfo->count = chs == 3 ? 2 : 1;
  379. switch (ofs) {
  380. case CS42L42_VOL_DAC:
  381. uinfo->value.integer.min = CS42L42_HP_VOL_REAL_MIN;
  382. uinfo->value.integer.max = CS42L42_HP_VOL_REAL_MAX;
  383. break;
  384. case CS42L42_VOL_ADC:
  385. uinfo->value.integer.min = CS42L42_AMIC_VOL_REAL_MIN;
  386. uinfo->value.integer.max = CS42L42_AMIC_VOL_REAL_MAX;
  387. break;
  388. default:
  389. break;
  390. }
  391. return 0;
  392. }
  393. int cs42l42_volume_get(struct snd_kcontrol *kctrl, struct snd_ctl_elem_value *uctrl)
  394. {
  395. struct hda_codec *codec = snd_kcontrol_chip(kctrl);
  396. struct cs8409_spec *spec = codec->spec;
  397. struct sub_codec *cs42l42 = spec->scodecs[get_amp_index(kctrl)];
  398. int chs = get_amp_channels(kctrl);
  399. unsigned int ofs = get_amp_offset(kctrl);
  400. long *valp = uctrl->value.integer.value;
  401. switch (ofs) {
  402. case CS42L42_VOL_DAC:
  403. if (chs & BIT(0))
  404. *valp++ = cs42l42->vol[ofs];
  405. if (chs & BIT(1))
  406. *valp = cs42l42->vol[ofs+1];
  407. break;
  408. case CS42L42_VOL_ADC:
  409. if (chs & BIT(0))
  410. *valp = cs42l42->vol[ofs];
  411. break;
  412. default:
  413. break;
  414. }
  415. return 0;
  416. }
  417. static void cs42l42_mute(struct sub_codec *cs42l42, int vol_type,
  418. unsigned int chs, bool mute)
  419. {
  420. if (mute) {
  421. if (vol_type == CS42L42_VOL_DAC) {
  422. if (chs & BIT(0))
  423. cs8409_i2c_write(cs42l42, CS42L42_MIXER_CHA_VOL, 0x3f);
  424. if (chs & BIT(1))
  425. cs8409_i2c_write(cs42l42, CS42L42_MIXER_CHB_VOL, 0x3f);
  426. } else if (vol_type == CS42L42_VOL_ADC) {
  427. if (chs & BIT(0))
  428. cs8409_i2c_write(cs42l42, CS42L42_ADC_VOLUME, 0x9f);
  429. }
  430. } else {
  431. if (vol_type == CS42L42_VOL_DAC) {
  432. if (chs & BIT(0))
  433. cs8409_i2c_write(cs42l42, CS42L42_MIXER_CHA_VOL,
  434. -(cs42l42->vol[CS42L42_DAC_CH0_VOL_OFFSET])
  435. & CS42L42_MIXER_CH_VOL_MASK);
  436. if (chs & BIT(1))
  437. cs8409_i2c_write(cs42l42, CS42L42_MIXER_CHB_VOL,
  438. -(cs42l42->vol[CS42L42_DAC_CH1_VOL_OFFSET])
  439. & CS42L42_MIXER_CH_VOL_MASK);
  440. } else if (vol_type == CS42L42_VOL_ADC) {
  441. if (chs & BIT(0))
  442. cs8409_i2c_write(cs42l42, CS42L42_ADC_VOLUME,
  443. cs42l42->vol[CS42L42_ADC_VOL_OFFSET]
  444. & CS42L42_REG_AMIC_VOL_MASK);
  445. }
  446. }
  447. }
  448. int cs42l42_volume_put(struct snd_kcontrol *kctrl, struct snd_ctl_elem_value *uctrl)
  449. {
  450. struct hda_codec *codec = snd_kcontrol_chip(kctrl);
  451. struct cs8409_spec *spec = codec->spec;
  452. struct sub_codec *cs42l42 = spec->scodecs[get_amp_index(kctrl)];
  453. int chs = get_amp_channels(kctrl);
  454. unsigned int ofs = get_amp_offset(kctrl);
  455. long *valp = uctrl->value.integer.value;
  456. switch (ofs) {
  457. case CS42L42_VOL_DAC:
  458. if (chs & BIT(0))
  459. cs42l42->vol[ofs] = *valp;
  460. if (chs & BIT(1)) {
  461. valp++;
  462. cs42l42->vol[ofs + 1] = *valp;
  463. }
  464. if (spec->playback_started)
  465. cs42l42_mute(cs42l42, CS42L42_VOL_DAC, chs, false);
  466. break;
  467. case CS42L42_VOL_ADC:
  468. if (chs & BIT(0))
  469. cs42l42->vol[ofs] = *valp;
  470. if (spec->capture_started)
  471. cs42l42_mute(cs42l42, CS42L42_VOL_ADC, chs, false);
  472. break;
  473. default:
  474. break;
  475. }
  476. return 0;
  477. }
  478. static void cs42l42_playback_pcm_hook(struct hda_pcm_stream *hinfo,
  479. struct hda_codec *codec,
  480. struct snd_pcm_substream *substream,
  481. int action)
  482. {
  483. struct cs8409_spec *spec = codec->spec;
  484. struct sub_codec *cs42l42;
  485. int i;
  486. bool mute;
  487. switch (action) {
  488. case HDA_GEN_PCM_ACT_PREPARE:
  489. mute = false;
  490. spec->playback_started = 1;
  491. break;
  492. case HDA_GEN_PCM_ACT_CLEANUP:
  493. mute = true;
  494. spec->playback_started = 0;
  495. break;
  496. default:
  497. return;
  498. }
  499. for (i = 0; i < spec->num_scodecs; i++) {
  500. cs42l42 = spec->scodecs[i];
  501. cs42l42_mute(cs42l42, CS42L42_VOL_DAC, 0x3, mute);
  502. }
  503. }
  504. static void cs42l42_capture_pcm_hook(struct hda_pcm_stream *hinfo,
  505. struct hda_codec *codec,
  506. struct snd_pcm_substream *substream,
  507. int action)
  508. {
  509. struct cs8409_spec *spec = codec->spec;
  510. struct sub_codec *cs42l42;
  511. int i;
  512. bool mute;
  513. switch (action) {
  514. case HDA_GEN_PCM_ACT_PREPARE:
  515. mute = false;
  516. spec->capture_started = 1;
  517. break;
  518. case HDA_GEN_PCM_ACT_CLEANUP:
  519. mute = true;
  520. spec->capture_started = 0;
  521. break;
  522. default:
  523. return;
  524. }
  525. for (i = 0; i < spec->num_scodecs; i++) {
  526. cs42l42 = spec->scodecs[i];
  527. cs42l42_mute(cs42l42, CS42L42_VOL_ADC, 0x3, mute);
  528. }
  529. }
  530. /* Configure CS42L42 slave codec for jack autodetect */
  531. static void cs42l42_enable_jack_detect(struct sub_codec *cs42l42)
  532. {
  533. cs8409_i2c_write(cs42l42, CS42L42_HSBIAS_SC_AUTOCTL, cs42l42->hsbias_hiz);
  534. /* Clear WAKE# */
  535. cs8409_i2c_write(cs42l42, CS42L42_WAKE_CTL, 0x00C1);
  536. /* Wait ~2.5ms */
  537. usleep_range(2500, 3000);
  538. /* Set mode WAKE# output follows the combination logic directly */
  539. cs8409_i2c_write(cs42l42, CS42L42_WAKE_CTL, 0x00C0);
  540. /* Clear interrupts status */
  541. cs8409_i2c_read(cs42l42, CS42L42_TSRS_PLUG_STATUS);
  542. /* Enable interrupt */
  543. cs8409_i2c_write(cs42l42, CS42L42_TSRS_PLUG_INT_MASK, 0xF3);
  544. }
  545. /* Enable and run CS42L42 slave codec jack auto detect */
  546. static void cs42l42_run_jack_detect(struct sub_codec *cs42l42)
  547. {
  548. /* Clear interrupts */
  549. cs8409_i2c_read(cs42l42, CS42L42_CODEC_STATUS);
  550. cs8409_i2c_read(cs42l42, CS42L42_DET_STATUS1);
  551. cs8409_i2c_write(cs42l42, CS42L42_TSRS_PLUG_INT_MASK, 0xFF);
  552. cs8409_i2c_read(cs42l42, CS42L42_TSRS_PLUG_STATUS);
  553. cs8409_i2c_write(cs42l42, CS42L42_PWR_CTL2, 0x87);
  554. cs8409_i2c_write(cs42l42, CS42L42_DAC_CTL2, 0x86);
  555. cs8409_i2c_write(cs42l42, CS42L42_MISC_DET_CTL, 0x07);
  556. cs8409_i2c_write(cs42l42, CS42L42_CODEC_INT_MASK, 0xFD);
  557. cs8409_i2c_write(cs42l42, CS42L42_HSDET_CTL2, 0x80);
  558. /* Wait ~20ms*/
  559. usleep_range(20000, 25000);
  560. cs8409_i2c_write(cs42l42, CS42L42_HSDET_CTL1, 0x77);
  561. cs8409_i2c_write(cs42l42, CS42L42_HSDET_CTL2, 0xc0);
  562. }
  563. static int cs42l42_manual_hs_det(struct sub_codec *cs42l42)
  564. {
  565. unsigned int hs_det_status;
  566. unsigned int hs_det_comp1;
  567. unsigned int hs_det_comp2;
  568. unsigned int hs_det_sw;
  569. unsigned int hs_type;
  570. /* Set hs detect to manual, active mode */
  571. cs8409_i2c_write(cs42l42, CS42L42_HSDET_CTL2,
  572. (1 << CS42L42_HSDET_CTRL_SHIFT) |
  573. (0 << CS42L42_HSDET_SET_SHIFT) |
  574. (0 << CS42L42_HSBIAS_REF_SHIFT) |
  575. (0 << CS42L42_HSDET_AUTO_TIME_SHIFT));
  576. /* Configure HS DET comparator reference levels. */
  577. cs8409_i2c_write(cs42l42, CS42L42_HSDET_CTL1,
  578. (CS42L42_HSDET_COMP1_LVL_VAL << CS42L42_HSDET_COMP1_LVL_SHIFT) |
  579. (CS42L42_HSDET_COMP2_LVL_VAL << CS42L42_HSDET_COMP2_LVL_SHIFT));
  580. /* Open the SW_HSB_HS3 switch and close SW_HSB_HS4 for a Type 1 headset. */
  581. cs8409_i2c_write(cs42l42, CS42L42_HS_SWITCH_CTL, CS42L42_HSDET_SW_COMP1);
  582. msleep(100);
  583. hs_det_status = cs8409_i2c_read(cs42l42, CS42L42_HS_DET_STATUS);
  584. hs_det_comp1 = (hs_det_status & CS42L42_HSDET_COMP1_OUT_MASK) >>
  585. CS42L42_HSDET_COMP1_OUT_SHIFT;
  586. hs_det_comp2 = (hs_det_status & CS42L42_HSDET_COMP2_OUT_MASK) >>
  587. CS42L42_HSDET_COMP2_OUT_SHIFT;
  588. /* Close the SW_HSB_HS3 switch for a Type 2 headset. */
  589. cs8409_i2c_write(cs42l42, CS42L42_HS_SWITCH_CTL, CS42L42_HSDET_SW_COMP2);
  590. msleep(100);
  591. hs_det_status = cs8409_i2c_read(cs42l42, CS42L42_HS_DET_STATUS);
  592. hs_det_comp1 |= ((hs_det_status & CS42L42_HSDET_COMP1_OUT_MASK) >>
  593. CS42L42_HSDET_COMP1_OUT_SHIFT) << 1;
  594. hs_det_comp2 |= ((hs_det_status & CS42L42_HSDET_COMP2_OUT_MASK) >>
  595. CS42L42_HSDET_COMP2_OUT_SHIFT) << 1;
  596. /* Use Comparator 1 with 1.25V Threshold. */
  597. switch (hs_det_comp1) {
  598. case CS42L42_HSDET_COMP_TYPE1:
  599. hs_type = CS42L42_PLUG_CTIA;
  600. hs_det_sw = CS42L42_HSDET_SW_TYPE1;
  601. break;
  602. case CS42L42_HSDET_COMP_TYPE2:
  603. hs_type = CS42L42_PLUG_OMTP;
  604. hs_det_sw = CS42L42_HSDET_SW_TYPE2;
  605. break;
  606. default:
  607. /* Fallback to Comparator 2 with 1.75V Threshold. */
  608. switch (hs_det_comp2) {
  609. case CS42L42_HSDET_COMP_TYPE1:
  610. hs_type = CS42L42_PLUG_CTIA;
  611. hs_det_sw = CS42L42_HSDET_SW_TYPE1;
  612. break;
  613. case CS42L42_HSDET_COMP_TYPE2:
  614. hs_type = CS42L42_PLUG_OMTP;
  615. hs_det_sw = CS42L42_HSDET_SW_TYPE2;
  616. break;
  617. case CS42L42_HSDET_COMP_TYPE3:
  618. hs_type = CS42L42_PLUG_HEADPHONE;
  619. hs_det_sw = CS42L42_HSDET_SW_TYPE3;
  620. break;
  621. default:
  622. hs_type = CS42L42_PLUG_INVALID;
  623. hs_det_sw = CS42L42_HSDET_SW_TYPE4;
  624. break;
  625. }
  626. }
  627. /* Set Switches */
  628. cs8409_i2c_write(cs42l42, CS42L42_HS_SWITCH_CTL, hs_det_sw);
  629. /* Set HSDET mode to Manual—Disabled */
  630. cs8409_i2c_write(cs42l42, CS42L42_HSDET_CTL2,
  631. (0 << CS42L42_HSDET_CTRL_SHIFT) |
  632. (0 << CS42L42_HSDET_SET_SHIFT) |
  633. (0 << CS42L42_HSBIAS_REF_SHIFT) |
  634. (0 << CS42L42_HSDET_AUTO_TIME_SHIFT));
  635. /* Configure HS DET comparator reference levels. */
  636. cs8409_i2c_write(cs42l42, CS42L42_HSDET_CTL1,
  637. (CS42L42_HSDET_COMP1_LVL_DEFAULT << CS42L42_HSDET_COMP1_LVL_SHIFT) |
  638. (CS42L42_HSDET_COMP2_LVL_DEFAULT << CS42L42_HSDET_COMP2_LVL_SHIFT));
  639. return hs_type;
  640. }
  641. static int cs42l42_handle_tip_sense(struct sub_codec *cs42l42, unsigned int reg_ts_status)
  642. {
  643. int status_changed = 0;
  644. /* TIP_SENSE INSERT/REMOVE */
  645. switch (reg_ts_status) {
  646. case CS42L42_TS_PLUG:
  647. if (cs42l42->no_type_dect) {
  648. status_changed = 1;
  649. cs42l42->hp_jack_in = 1;
  650. cs42l42->mic_jack_in = 0;
  651. } else {
  652. cs42l42_run_jack_detect(cs42l42);
  653. }
  654. break;
  655. case CS42L42_TS_UNPLUG:
  656. status_changed = 1;
  657. cs42l42->hp_jack_in = 0;
  658. cs42l42->mic_jack_in = 0;
  659. break;
  660. default:
  661. /* jack in transition */
  662. break;
  663. }
  664. codec_dbg(cs42l42->codec, "Tip Sense Detection: (%d)\n", reg_ts_status);
  665. return status_changed;
  666. }
  667. static int cs42l42_jack_unsol_event(struct sub_codec *cs42l42)
  668. {
  669. int current_plug_status;
  670. int status_changed = 0;
  671. int reg_cdc_status;
  672. int reg_hs_status;
  673. int reg_ts_status;
  674. int type;
  675. /* Read jack detect status registers */
  676. reg_cdc_status = cs8409_i2c_read(cs42l42, CS42L42_CODEC_STATUS);
  677. reg_hs_status = cs8409_i2c_read(cs42l42, CS42L42_HS_DET_STATUS);
  678. reg_ts_status = cs8409_i2c_read(cs42l42, CS42L42_TSRS_PLUG_STATUS);
  679. /* If status values are < 0, read error has occurred. */
  680. if (reg_cdc_status < 0 || reg_hs_status < 0 || reg_ts_status < 0)
  681. return -EIO;
  682. current_plug_status = (reg_ts_status & (CS42L42_TS_PLUG_MASK | CS42L42_TS_UNPLUG_MASK))
  683. >> CS42L42_TS_PLUG_SHIFT;
  684. /* HSDET_AUTO_DONE */
  685. if (reg_cdc_status & CS42L42_HSDET_AUTO_DONE_MASK) {
  686. /* Disable HSDET_AUTO_DONE */
  687. cs8409_i2c_write(cs42l42, CS42L42_CODEC_INT_MASK, 0xFF);
  688. type = (reg_hs_status & CS42L42_HSDET_TYPE_MASK) >> CS42L42_HSDET_TYPE_SHIFT;
  689. /* Configure the HSDET mode. */
  690. cs8409_i2c_write(cs42l42, CS42L42_HSDET_CTL2, 0x80);
  691. if (cs42l42->no_type_dect) {
  692. status_changed = cs42l42_handle_tip_sense(cs42l42, current_plug_status);
  693. } else {
  694. if (type == CS42L42_PLUG_INVALID || type == CS42L42_PLUG_HEADPHONE) {
  695. codec_dbg(cs42l42->codec,
  696. "Auto detect value not valid (%d), running manual det\n",
  697. type);
  698. type = cs42l42_manual_hs_det(cs42l42);
  699. }
  700. switch (type) {
  701. case CS42L42_PLUG_CTIA:
  702. case CS42L42_PLUG_OMTP:
  703. status_changed = 1;
  704. cs42l42->hp_jack_in = 1;
  705. cs42l42->mic_jack_in = 1;
  706. break;
  707. case CS42L42_PLUG_HEADPHONE:
  708. status_changed = 1;
  709. cs42l42->hp_jack_in = 1;
  710. cs42l42->mic_jack_in = 0;
  711. break;
  712. default:
  713. status_changed = 1;
  714. cs42l42->hp_jack_in = 0;
  715. cs42l42->mic_jack_in = 0;
  716. break;
  717. }
  718. codec_dbg(cs42l42->codec, "Detection done (%d)\n", type);
  719. }
  720. /* Enable the HPOUT ground clamp and configure the HP pull-down */
  721. cs8409_i2c_write(cs42l42, CS42L42_DAC_CTL2, 0x02);
  722. /* Re-Enable Tip Sense Interrupt */
  723. cs8409_i2c_write(cs42l42, CS42L42_TSRS_PLUG_INT_MASK, 0xF3);
  724. } else {
  725. status_changed = cs42l42_handle_tip_sense(cs42l42, current_plug_status);
  726. }
  727. return status_changed;
  728. }
  729. static void cs42l42_resume(struct sub_codec *cs42l42)
  730. {
  731. struct hda_codec *codec = cs42l42->codec;
  732. struct cs8409_spec *spec = codec->spec;
  733. struct cs8409_i2c_param irq_regs[] = {
  734. { CS42L42_CODEC_STATUS, 0x00 },
  735. { CS42L42_DET_INT_STATUS1, 0x00 },
  736. { CS42L42_DET_INT_STATUS2, 0x00 },
  737. { CS42L42_TSRS_PLUG_STATUS, 0x00 },
  738. };
  739. int fsv_old, fsv_new;
  740. /* Bring CS42L42 out of Reset */
  741. spec->gpio_data = snd_hda_codec_read(codec, CS8409_PIN_AFG, 0, AC_VERB_GET_GPIO_DATA, 0);
  742. spec->gpio_data |= cs42l42->reset_gpio;
  743. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DATA, spec->gpio_data);
  744. usleep_range(10000, 15000);
  745. cs42l42->suspended = 0;
  746. /* Initialize CS42L42 companion codec */
  747. cs8409_i2c_bulk_write(cs42l42, cs42l42->init_seq, cs42l42->init_seq_num);
  748. msleep(CS42L42_INIT_TIMEOUT_MS);
  749. /* Clear interrupts, by reading interrupt status registers */
  750. cs8409_i2c_bulk_read(cs42l42, irq_regs, ARRAY_SIZE(irq_regs));
  751. fsv_old = cs8409_i2c_read(cs42l42, CS42L42_HP_CTL);
  752. if (cs42l42->full_scale_vol == CS42L42_FULL_SCALE_VOL_0DB)
  753. fsv_new = fsv_old & ~CS42L42_FULL_SCALE_VOL_MASK;
  754. else
  755. fsv_new = fsv_old & CS42L42_FULL_SCALE_VOL_MASK;
  756. if (fsv_new != fsv_old)
  757. cs8409_i2c_write(cs42l42, CS42L42_HP_CTL, fsv_new);
  758. /* we have to explicitly allow unsol event handling even during the
  759. * resume phase so that the jack event is processed properly
  760. */
  761. snd_hda_codec_allow_unsol_events(cs42l42->codec);
  762. cs42l42_enable_jack_detect(cs42l42);
  763. }
  764. #ifdef CONFIG_PM
  765. static void cs42l42_suspend(struct sub_codec *cs42l42)
  766. {
  767. struct hda_codec *codec = cs42l42->codec;
  768. struct cs8409_spec *spec = codec->spec;
  769. int reg_cdc_status = 0;
  770. const struct cs8409_i2c_param cs42l42_pwr_down_seq[] = {
  771. { CS42L42_DAC_CTL2, 0x02 },
  772. { CS42L42_HS_CLAMP_DISABLE, 0x00 },
  773. { CS42L42_MIXER_CHA_VOL, 0x3F },
  774. { CS42L42_MIXER_ADC_VOL, 0x3F },
  775. { CS42L42_MIXER_CHB_VOL, 0x3F },
  776. { CS42L42_HP_CTL, 0x0F },
  777. { CS42L42_ASP_RX_DAI0_EN, 0x00 },
  778. { CS42L42_ASP_CLK_CFG, 0x00 },
  779. { CS42L42_PWR_CTL1, 0xFE },
  780. { CS42L42_PWR_CTL2, 0x8C },
  781. { CS42L42_PWR_CTL1, 0xFF },
  782. };
  783. cs8409_i2c_bulk_write(cs42l42, cs42l42_pwr_down_seq, ARRAY_SIZE(cs42l42_pwr_down_seq));
  784. if (read_poll_timeout(cs8409_i2c_read, reg_cdc_status,
  785. (reg_cdc_status & 0x1), CS42L42_PDN_SLEEP_US, CS42L42_PDN_TIMEOUT_US,
  786. true, cs42l42, CS42L42_CODEC_STATUS) < 0)
  787. codec_warn(codec, "Timeout waiting for PDN_DONE for CS42L42\n");
  788. /* Power down CS42L42 ASP/EQ/MIX/HP */
  789. cs8409_i2c_write(cs42l42, CS42L42_PWR_CTL2, 0x9C);
  790. cs42l42->suspended = 1;
  791. cs42l42->last_page = 0;
  792. cs42l42->hp_jack_in = 0;
  793. cs42l42->mic_jack_in = 0;
  794. /* Put CS42L42 into Reset */
  795. spec->gpio_data = snd_hda_codec_read(codec, CS8409_PIN_AFG, 0, AC_VERB_GET_GPIO_DATA, 0);
  796. spec->gpio_data &= ~cs42l42->reset_gpio;
  797. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DATA, spec->gpio_data);
  798. }
  799. #endif
  800. static void cs8409_free(struct hda_codec *codec)
  801. {
  802. struct cs8409_spec *spec = codec->spec;
  803. /* Cancel i2c clock disable timer, and disable clock if left enabled */
  804. cancel_delayed_work_sync(&spec->i2c_clk_work);
  805. cs8409_disable_i2c_clock(codec);
  806. snd_hda_gen_free(codec);
  807. }
  808. /******************************************************************************
  809. * BULLSEYE / WARLOCK / CYBORG Specific Functions
  810. * CS8409/CS42L42
  811. ******************************************************************************/
  812. /*
  813. * In the case of CS8409 we do not have unsolicited events from NID's 0x24
  814. * and 0x34 where hs mic and hp are connected. Companion codec CS42L42 will
  815. * generate interrupt via gpio 4 to notify jack events. We have to overwrite
  816. * generic snd_hda_jack_unsol_event(), read CS42L42 jack detect status registers
  817. * and then notify status via generic snd_hda_jack_unsol_event() call.
  818. */
  819. static void cs8409_cs42l42_jack_unsol_event(struct hda_codec *codec, unsigned int res)
  820. {
  821. struct cs8409_spec *spec = codec->spec;
  822. struct sub_codec *cs42l42 = spec->scodecs[CS8409_CODEC0];
  823. struct hda_jack_tbl *jk;
  824. /* jack_unsol_event() will be called every time gpio line changing state.
  825. * In this case gpio4 line goes up as a result of reading interrupt status
  826. * registers in previous cs8409_jack_unsol_event() call.
  827. * We don't need to handle this event, ignoring...
  828. */
  829. if (res & cs42l42->irq_mask)
  830. return;
  831. if (cs42l42_jack_unsol_event(cs42l42)) {
  832. snd_hda_set_pin_ctl(codec, CS8409_CS42L42_SPK_PIN_NID,
  833. cs42l42->hp_jack_in ? 0 : PIN_OUT);
  834. /* Report jack*/
  835. jk = snd_hda_jack_tbl_get_mst(codec, CS8409_CS42L42_HP_PIN_NID, 0);
  836. if (jk)
  837. snd_hda_jack_unsol_event(codec, (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
  838. AC_UNSOL_RES_TAG);
  839. /* Report jack*/
  840. jk = snd_hda_jack_tbl_get_mst(codec, CS8409_CS42L42_AMIC_PIN_NID, 0);
  841. if (jk)
  842. snd_hda_jack_unsol_event(codec, (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
  843. AC_UNSOL_RES_TAG);
  844. }
  845. }
  846. #ifdef CONFIG_PM
  847. /* Manage PDREF, when transition to D3hot */
  848. static int cs8409_cs42l42_suspend(struct hda_codec *codec)
  849. {
  850. struct cs8409_spec *spec = codec->spec;
  851. int i;
  852. spec->init_done = 0;
  853. cs8409_enable_ur(codec, 0);
  854. for (i = 0; i < spec->num_scodecs; i++)
  855. cs42l42_suspend(spec->scodecs[i]);
  856. /* Cancel i2c clock disable timer, and disable clock if left enabled */
  857. cancel_delayed_work_sync(&spec->i2c_clk_work);
  858. cs8409_disable_i2c_clock(codec);
  859. snd_hda_shutup_pins(codec);
  860. return 0;
  861. }
  862. #endif
  863. /* Vendor specific HW configuration
  864. * PLL, ASP, I2C, SPI, GPIOs, DMIC etc...
  865. */
  866. static void cs8409_cs42l42_hw_init(struct hda_codec *codec)
  867. {
  868. const struct cs8409_cir_param *seq = cs8409_cs42l42_hw_cfg;
  869. const struct cs8409_cir_param *seq_bullseye = cs8409_cs42l42_bullseye_atn;
  870. struct cs8409_spec *spec = codec->spec;
  871. struct sub_codec *cs42l42 = spec->scodecs[CS8409_CODEC0];
  872. if (spec->gpio_mask) {
  873. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_MASK,
  874. spec->gpio_mask);
  875. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DIRECTION,
  876. spec->gpio_dir);
  877. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DATA,
  878. spec->gpio_data);
  879. }
  880. for (; seq->nid; seq++)
  881. cs8409_vendor_coef_set(codec, seq->cir, seq->coeff);
  882. if (codec->fixup_id == CS8409_BULLSEYE) {
  883. for (; seq_bullseye->nid; seq_bullseye++)
  884. cs8409_vendor_coef_set(codec, seq_bullseye->cir, seq_bullseye->coeff);
  885. }
  886. switch (codec->fixup_id) {
  887. case CS8409_CYBORG:
  888. case CS8409_WARLOCK_MLK_DUAL_MIC:
  889. /* DMIC1_MO=00b, DMIC1/2_SR=1 */
  890. cs8409_vendor_coef_set(codec, CS8409_DMIC_CFG, 0x0003);
  891. break;
  892. case CS8409_ODIN:
  893. /* ASP1/2_xxx_EN=1, ASP1/2_MCLK_EN=0, DMIC1_SCL_EN=0 */
  894. cs8409_vendor_coef_set(codec, CS8409_PAD_CFG_SLW_RATE_CTRL, 0xfc00);
  895. break;
  896. default:
  897. break;
  898. }
  899. cs42l42_resume(cs42l42);
  900. /* Enable Unsolicited Response */
  901. cs8409_enable_ur(codec, 1);
  902. }
  903. static const struct hda_codec_ops cs8409_cs42l42_patch_ops = {
  904. .build_controls = cs8409_build_controls,
  905. .build_pcms = snd_hda_gen_build_pcms,
  906. .init = cs8409_init,
  907. .free = cs8409_free,
  908. .unsol_event = cs8409_cs42l42_jack_unsol_event,
  909. #ifdef CONFIG_PM
  910. .suspend = cs8409_cs42l42_suspend,
  911. #endif
  912. };
  913. static int cs8409_cs42l42_exec_verb(struct hdac_device *dev, unsigned int cmd, unsigned int flags,
  914. unsigned int *res)
  915. {
  916. struct hda_codec *codec = container_of(dev, struct hda_codec, core);
  917. struct cs8409_spec *spec = codec->spec;
  918. struct sub_codec *cs42l42 = spec->scodecs[CS8409_CODEC0];
  919. unsigned int nid = ((cmd >> 20) & 0x07f);
  920. unsigned int verb = ((cmd >> 8) & 0x0fff);
  921. /* CS8409 pins have no AC_PINSENSE_PRESENCE
  922. * capabilities. We have to intercept 2 calls for pins 0x24 and 0x34
  923. * and return correct pin sense values for read_pin_sense() call from
  924. * hda_jack based on CS42L42 jack detect status.
  925. */
  926. switch (nid) {
  927. case CS8409_CS42L42_HP_PIN_NID:
  928. if (verb == AC_VERB_GET_PIN_SENSE) {
  929. *res = (cs42l42->hp_jack_in) ? AC_PINSENSE_PRESENCE : 0;
  930. return 0;
  931. }
  932. break;
  933. case CS8409_CS42L42_AMIC_PIN_NID:
  934. if (verb == AC_VERB_GET_PIN_SENSE) {
  935. *res = (cs42l42->mic_jack_in) ? AC_PINSENSE_PRESENCE : 0;
  936. return 0;
  937. }
  938. break;
  939. default:
  940. break;
  941. }
  942. return spec->exec_verb(dev, cmd, flags, res);
  943. }
  944. void cs8409_cs42l42_fixups(struct hda_codec *codec, const struct hda_fixup *fix, int action)
  945. {
  946. struct cs8409_spec *spec = codec->spec;
  947. switch (action) {
  948. case HDA_FIXUP_ACT_PRE_PROBE:
  949. snd_hda_add_verbs(codec, cs8409_cs42l42_init_verbs);
  950. /* verb exec op override */
  951. spec->exec_verb = codec->core.exec_verb;
  952. codec->core.exec_verb = cs8409_cs42l42_exec_verb;
  953. spec->scodecs[CS8409_CODEC0] = &cs8409_cs42l42_codec;
  954. spec->num_scodecs = 1;
  955. spec->scodecs[CS8409_CODEC0]->codec = codec;
  956. codec->patch_ops = cs8409_cs42l42_patch_ops;
  957. spec->gen.suppress_auto_mute = 1;
  958. spec->gen.no_primary_hp = 1;
  959. spec->gen.suppress_vmaster = 1;
  960. spec->speaker_pdn_gpio = 0;
  961. /* GPIO 5 out, 3,4 in */
  962. spec->gpio_dir = spec->scodecs[CS8409_CODEC0]->reset_gpio;
  963. spec->gpio_data = 0;
  964. spec->gpio_mask = 0x03f;
  965. /* Basic initial sequence for specific hw configuration */
  966. snd_hda_sequence_write(codec, cs8409_cs42l42_init_verbs);
  967. cs8409_fix_caps(codec, CS8409_CS42L42_HP_PIN_NID);
  968. cs8409_fix_caps(codec, CS8409_CS42L42_AMIC_PIN_NID);
  969. spec->scodecs[CS8409_CODEC0]->hsbias_hiz = 0x0020;
  970. switch (codec->fixup_id) {
  971. case CS8409_CYBORG:
  972. spec->scodecs[CS8409_CODEC0]->full_scale_vol =
  973. CS42L42_FULL_SCALE_VOL_MINUS6DB;
  974. spec->speaker_pdn_gpio = CS8409_CYBORG_SPEAKER_PDN;
  975. break;
  976. case CS8409_ODIN:
  977. spec->scodecs[CS8409_CODEC0]->full_scale_vol = CS42L42_FULL_SCALE_VOL_0DB;
  978. spec->speaker_pdn_gpio = CS8409_CYBORG_SPEAKER_PDN;
  979. break;
  980. case CS8409_WARLOCK_MLK:
  981. case CS8409_WARLOCK_MLK_DUAL_MIC:
  982. spec->scodecs[CS8409_CODEC0]->full_scale_vol = CS42L42_FULL_SCALE_VOL_0DB;
  983. spec->speaker_pdn_gpio = CS8409_WARLOCK_SPEAKER_PDN;
  984. break;
  985. default:
  986. spec->scodecs[CS8409_CODEC0]->full_scale_vol =
  987. CS42L42_FULL_SCALE_VOL_MINUS6DB;
  988. spec->speaker_pdn_gpio = CS8409_WARLOCK_SPEAKER_PDN;
  989. break;
  990. }
  991. if (spec->speaker_pdn_gpio > 0) {
  992. spec->gpio_dir |= spec->speaker_pdn_gpio;
  993. spec->gpio_data |= spec->speaker_pdn_gpio;
  994. }
  995. break;
  996. case HDA_FIXUP_ACT_PROBE:
  997. /* Fix Sample Rate to 48kHz */
  998. spec->gen.stream_analog_playback = &cs42l42_48k_pcm_analog_playback;
  999. spec->gen.stream_analog_capture = &cs42l42_48k_pcm_analog_capture;
  1000. /* add hooks */
  1001. spec->gen.pcm_playback_hook = cs42l42_playback_pcm_hook;
  1002. spec->gen.pcm_capture_hook = cs42l42_capture_pcm_hook;
  1003. if (codec->fixup_id != CS8409_ODIN)
  1004. /* Set initial DMIC volume to -26 dB */
  1005. snd_hda_codec_amp_init_stereo(codec, CS8409_CS42L42_DMIC_ADC_PIN_NID,
  1006. HDA_INPUT, 0, 0xff, 0x19);
  1007. snd_hda_gen_add_kctl(&spec->gen, "Headphone Playback Volume",
  1008. &cs42l42_dac_volume_mixer);
  1009. snd_hda_gen_add_kctl(&spec->gen, "Mic Capture Volume",
  1010. &cs42l42_adc_volume_mixer);
  1011. if (spec->speaker_pdn_gpio > 0)
  1012. snd_hda_gen_add_kctl(&spec->gen, "Speaker Playback Switch",
  1013. &cs8409_spk_sw_ctrl);
  1014. /* Disable Unsolicited Response during boot */
  1015. cs8409_enable_ur(codec, 0);
  1016. snd_hda_codec_set_name(codec, "CS8409/CS42L42");
  1017. break;
  1018. case HDA_FIXUP_ACT_INIT:
  1019. cs8409_cs42l42_hw_init(codec);
  1020. spec->init_done = 1;
  1021. if (spec->init_done && spec->build_ctrl_done
  1022. && !spec->scodecs[CS8409_CODEC0]->hp_jack_in)
  1023. cs42l42_run_jack_detect(spec->scodecs[CS8409_CODEC0]);
  1024. break;
  1025. case HDA_FIXUP_ACT_BUILD:
  1026. spec->build_ctrl_done = 1;
  1027. /* Run jack auto detect first time on boot
  1028. * after controls have been added, to check if jack has
  1029. * been already plugged in.
  1030. * Run immediately after init.
  1031. */
  1032. if (spec->init_done && spec->build_ctrl_done
  1033. && !spec->scodecs[CS8409_CODEC0]->hp_jack_in)
  1034. cs42l42_run_jack_detect(spec->scodecs[CS8409_CODEC0]);
  1035. break;
  1036. default:
  1037. break;
  1038. }
  1039. }
  1040. /******************************************************************************
  1041. * Dolphin Specific Functions
  1042. * CS8409/ 2 X CS42L42
  1043. ******************************************************************************/
  1044. /*
  1045. * In the case of CS8409 we do not have unsolicited events when
  1046. * hs mic and hp are connected. Companion codec CS42L42 will
  1047. * generate interrupt via irq_mask to notify jack events. We have to overwrite
  1048. * generic snd_hda_jack_unsol_event(), read CS42L42 jack detect status registers
  1049. * and then notify status via generic snd_hda_jack_unsol_event() call.
  1050. */
  1051. static void dolphin_jack_unsol_event(struct hda_codec *codec, unsigned int res)
  1052. {
  1053. struct cs8409_spec *spec = codec->spec;
  1054. struct sub_codec *cs42l42;
  1055. struct hda_jack_tbl *jk;
  1056. cs42l42 = spec->scodecs[CS8409_CODEC0];
  1057. if (!cs42l42->suspended && (~res & cs42l42->irq_mask) &&
  1058. cs42l42_jack_unsol_event(cs42l42)) {
  1059. jk = snd_hda_jack_tbl_get_mst(codec, DOLPHIN_HP_PIN_NID, 0);
  1060. if (jk)
  1061. snd_hda_jack_unsol_event(codec,
  1062. (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
  1063. AC_UNSOL_RES_TAG);
  1064. jk = snd_hda_jack_tbl_get_mst(codec, DOLPHIN_AMIC_PIN_NID, 0);
  1065. if (jk)
  1066. snd_hda_jack_unsol_event(codec,
  1067. (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
  1068. AC_UNSOL_RES_TAG);
  1069. }
  1070. cs42l42 = spec->scodecs[CS8409_CODEC1];
  1071. if (!cs42l42->suspended && (~res & cs42l42->irq_mask) &&
  1072. cs42l42_jack_unsol_event(cs42l42)) {
  1073. jk = snd_hda_jack_tbl_get_mst(codec, DOLPHIN_LO_PIN_NID, 0);
  1074. if (jk)
  1075. snd_hda_jack_unsol_event(codec,
  1076. (jk->tag << AC_UNSOL_RES_TAG_SHIFT) &
  1077. AC_UNSOL_RES_TAG);
  1078. }
  1079. }
  1080. /* Vendor specific HW configuration
  1081. * PLL, ASP, I2C, SPI, GPIOs, DMIC etc...
  1082. */
  1083. static void dolphin_hw_init(struct hda_codec *codec)
  1084. {
  1085. const struct cs8409_cir_param *seq = dolphin_hw_cfg;
  1086. struct cs8409_spec *spec = codec->spec;
  1087. struct sub_codec *cs42l42;
  1088. int i;
  1089. if (spec->gpio_mask) {
  1090. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_MASK,
  1091. spec->gpio_mask);
  1092. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DIRECTION,
  1093. spec->gpio_dir);
  1094. snd_hda_codec_write(codec, CS8409_PIN_AFG, 0, AC_VERB_SET_GPIO_DATA,
  1095. spec->gpio_data);
  1096. }
  1097. for (; seq->nid; seq++)
  1098. cs8409_vendor_coef_set(codec, seq->cir, seq->coeff);
  1099. for (i = 0; i < spec->num_scodecs; i++) {
  1100. cs42l42 = spec->scodecs[i];
  1101. cs42l42_resume(cs42l42);
  1102. }
  1103. /* Enable Unsolicited Response */
  1104. cs8409_enable_ur(codec, 1);
  1105. }
  1106. static const struct hda_codec_ops cs8409_dolphin_patch_ops = {
  1107. .build_controls = cs8409_build_controls,
  1108. .build_pcms = snd_hda_gen_build_pcms,
  1109. .init = cs8409_init,
  1110. .free = cs8409_free,
  1111. .unsol_event = dolphin_jack_unsol_event,
  1112. #ifdef CONFIG_PM
  1113. .suspend = cs8409_cs42l42_suspend,
  1114. #endif
  1115. };
  1116. static int dolphin_exec_verb(struct hdac_device *dev, unsigned int cmd, unsigned int flags,
  1117. unsigned int *res)
  1118. {
  1119. struct hda_codec *codec = container_of(dev, struct hda_codec, core);
  1120. struct cs8409_spec *spec = codec->spec;
  1121. struct sub_codec *cs42l42 = spec->scodecs[CS8409_CODEC0];
  1122. unsigned int nid = ((cmd >> 20) & 0x07f);
  1123. unsigned int verb = ((cmd >> 8) & 0x0fff);
  1124. /* CS8409 pins have no AC_PINSENSE_PRESENCE
  1125. * capabilities. We have to intercept calls for CS42L42 pins
  1126. * and return correct pin sense values for read_pin_sense() call from
  1127. * hda_jack based on CS42L42 jack detect status.
  1128. */
  1129. switch (nid) {
  1130. case DOLPHIN_HP_PIN_NID:
  1131. case DOLPHIN_LO_PIN_NID:
  1132. if (nid == DOLPHIN_LO_PIN_NID)
  1133. cs42l42 = spec->scodecs[CS8409_CODEC1];
  1134. if (verb == AC_VERB_GET_PIN_SENSE) {
  1135. *res = (cs42l42->hp_jack_in) ? AC_PINSENSE_PRESENCE : 0;
  1136. return 0;
  1137. }
  1138. break;
  1139. case DOLPHIN_AMIC_PIN_NID:
  1140. if (verb == AC_VERB_GET_PIN_SENSE) {
  1141. *res = (cs42l42->mic_jack_in) ? AC_PINSENSE_PRESENCE : 0;
  1142. return 0;
  1143. }
  1144. break;
  1145. default:
  1146. break;
  1147. }
  1148. return spec->exec_verb(dev, cmd, flags, res);
  1149. }
  1150. void dolphin_fixups(struct hda_codec *codec, const struct hda_fixup *fix, int action)
  1151. {
  1152. struct cs8409_spec *spec = codec->spec;
  1153. struct snd_kcontrol_new *kctrl;
  1154. int i;
  1155. switch (action) {
  1156. case HDA_FIXUP_ACT_PRE_PROBE:
  1157. snd_hda_add_verbs(codec, dolphin_init_verbs);
  1158. /* verb exec op override */
  1159. spec->exec_verb = codec->core.exec_verb;
  1160. codec->core.exec_verb = dolphin_exec_verb;
  1161. spec->scodecs[CS8409_CODEC0] = &dolphin_cs42l42_0;
  1162. spec->scodecs[CS8409_CODEC0]->codec = codec;
  1163. spec->scodecs[CS8409_CODEC1] = &dolphin_cs42l42_1;
  1164. spec->scodecs[CS8409_CODEC1]->codec = codec;
  1165. spec->num_scodecs = 2;
  1166. codec->patch_ops = cs8409_dolphin_patch_ops;
  1167. /* GPIO 1,5 out, 0,4 in */
  1168. spec->gpio_dir = spec->scodecs[CS8409_CODEC0]->reset_gpio |
  1169. spec->scodecs[CS8409_CODEC1]->reset_gpio;
  1170. spec->gpio_data = 0;
  1171. spec->gpio_mask = 0x03f;
  1172. /* Basic initial sequence for specific hw configuration */
  1173. snd_hda_sequence_write(codec, dolphin_init_verbs);
  1174. snd_hda_jack_add_kctl(codec, DOLPHIN_LO_PIN_NID, "Line Out", true,
  1175. SND_JACK_HEADPHONE, NULL);
  1176. snd_hda_jack_add_kctl(codec, DOLPHIN_AMIC_PIN_NID, "Microphone", true,
  1177. SND_JACK_MICROPHONE, NULL);
  1178. cs8409_fix_caps(codec, DOLPHIN_HP_PIN_NID);
  1179. cs8409_fix_caps(codec, DOLPHIN_LO_PIN_NID);
  1180. cs8409_fix_caps(codec, DOLPHIN_AMIC_PIN_NID);
  1181. spec->scodecs[CS8409_CODEC0]->full_scale_vol = CS42L42_FULL_SCALE_VOL_MINUS6DB;
  1182. spec->scodecs[CS8409_CODEC1]->full_scale_vol = CS42L42_FULL_SCALE_VOL_MINUS6DB;
  1183. break;
  1184. case HDA_FIXUP_ACT_PROBE:
  1185. /* Fix Sample Rate to 48kHz */
  1186. spec->gen.stream_analog_playback = &cs42l42_48k_pcm_analog_playback;
  1187. spec->gen.stream_analog_capture = &cs42l42_48k_pcm_analog_capture;
  1188. /* add hooks */
  1189. spec->gen.pcm_playback_hook = cs42l42_playback_pcm_hook;
  1190. spec->gen.pcm_capture_hook = cs42l42_capture_pcm_hook;
  1191. snd_hda_gen_add_kctl(&spec->gen, "Headphone Playback Volume",
  1192. &cs42l42_dac_volume_mixer);
  1193. snd_hda_gen_add_kctl(&spec->gen, "Mic Capture Volume", &cs42l42_adc_volume_mixer);
  1194. kctrl = snd_hda_gen_add_kctl(&spec->gen, "Line Out Playback Volume",
  1195. &cs42l42_dac_volume_mixer);
  1196. /* Update Line Out kcontrol template */
  1197. kctrl->private_value = HDA_COMPOSE_AMP_VAL_OFS(DOLPHIN_HP_PIN_NID, 3, CS8409_CODEC1,
  1198. HDA_OUTPUT, CS42L42_VOL_DAC) | HDA_AMP_VAL_MIN_MUTE;
  1199. cs8409_enable_ur(codec, 0);
  1200. snd_hda_codec_set_name(codec, "CS8409/CS42L42");
  1201. break;
  1202. case HDA_FIXUP_ACT_INIT:
  1203. dolphin_hw_init(codec);
  1204. spec->init_done = 1;
  1205. if (spec->init_done && spec->build_ctrl_done) {
  1206. for (i = 0; i < spec->num_scodecs; i++) {
  1207. if (!spec->scodecs[i]->hp_jack_in)
  1208. cs42l42_run_jack_detect(spec->scodecs[i]);
  1209. }
  1210. }
  1211. break;
  1212. case HDA_FIXUP_ACT_BUILD:
  1213. spec->build_ctrl_done = 1;
  1214. /* Run jack auto detect first time on boot
  1215. * after controls have been added, to check if jack has
  1216. * been already plugged in.
  1217. * Run immediately after init.
  1218. */
  1219. if (spec->init_done && spec->build_ctrl_done) {
  1220. for (i = 0; i < spec->num_scodecs; i++) {
  1221. if (!spec->scodecs[i]->hp_jack_in)
  1222. cs42l42_run_jack_detect(spec->scodecs[i]);
  1223. }
  1224. }
  1225. break;
  1226. default:
  1227. break;
  1228. }
  1229. }
  1230. static int patch_cs8409(struct hda_codec *codec)
  1231. {
  1232. int err;
  1233. if (!cs8409_alloc_spec(codec))
  1234. return -ENOMEM;
  1235. snd_hda_pick_fixup(codec, cs8409_models, cs8409_fixup_tbl, cs8409_fixups);
  1236. codec_dbg(codec, "Picked ID=%d, VID=%08x, DEV=%08x\n", codec->fixup_id,
  1237. codec->bus->pci->subsystem_vendor,
  1238. codec->bus->pci->subsystem_device);
  1239. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
  1240. err = cs8409_parse_auto_config(codec);
  1241. if (err < 0) {
  1242. cs8409_free(codec);
  1243. return err;
  1244. }
  1245. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
  1246. return 0;
  1247. }
  1248. static const struct hda_device_id snd_hda_id_cs8409[] = {
  1249. HDA_CODEC_ENTRY(0x10138409, "CS8409", patch_cs8409),
  1250. {} /* terminator */
  1251. };
  1252. MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_cs8409);
  1253. static struct hda_codec_driver cs8409_driver = {
  1254. .id = snd_hda_id_cs8409,
  1255. };
  1256. module_hda_codec_driver(cs8409_driver);
  1257. MODULE_LICENSE("GPL");
  1258. MODULE_DESCRIPTION("Cirrus Logic HDA bridge");