wcd934x-mbhc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /*
  2. * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/device.h>
  17. #include <linux/printk.h>
  18. #include <linux/ratelimit.h>
  19. #include <linux/kernel.h>
  20. #include <linux/gpio.h>
  21. #include <linux/delay.h>
  22. #include <linux/regmap.h>
  23. #include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/soc-dapm.h>
  28. #include "wcd934x.h"
  29. #include "wcd934x-mbhc.h"
  30. #include <asoc/wcd934x_registers.h>
  31. #include "wcd934x_irq.h"
  32. #include "../core.h"
  33. #include "../pdata.h"
  34. #include "../wcd9xxx-irq.h"
  35. #include "../wcdcal-hwdep.h"
  36. #include "../wcd-mbhc-v2-api.h"
  37. #define TAVIL_ZDET_SUPPORTED true
  38. /* Z value defined in milliohm */
  39. #define TAVIL_ZDET_VAL_32 32000
  40. #define TAVIL_ZDET_VAL_400 400000
  41. #define TAVIL_ZDET_VAL_1200 1200000
  42. #define TAVIL_ZDET_VAL_100K 100000000
  43. /* Z floating defined in ohms */
  44. #define TAVIL_ZDET_FLOATING_IMPEDANCE 0x0FFFFFFE
  45. #define TAVIL_ZDET_NUM_MEASUREMENTS 900
  46. #define TAVIL_MBHC_GET_C1(c) ((c & 0xC000) >> 14)
  47. #define TAVIL_MBHC_GET_X1(x) (x & 0x3FFF)
  48. /* Z value compared in milliOhm */
  49. #define TAVIL_MBHC_IS_SECOND_RAMP_REQUIRED(z) ((z > 400000) || (z < 32000))
  50. #define TAVIL_MBHC_ZDET_CONST (86 * 16384)
  51. #define TAVIL_MBHC_MOISTURE_RREF R_24_KOHM
  52. static struct wcd_mbhc_register
  53. wcd_mbhc_registers[WCD_MBHC_REG_FUNC_MAX] = {
  54. WCD_MBHC_REGISTER("WCD_MBHC_L_DET_EN",
  55. WCD934X_ANA_MBHC_MECH, 0x80, 7, 0),
  56. WCD_MBHC_REGISTER("WCD_MBHC_GND_DET_EN",
  57. WCD934X_ANA_MBHC_MECH, 0x40, 6, 0),
  58. WCD_MBHC_REGISTER("WCD_MBHC_MECH_DETECTION_TYPE",
  59. WCD934X_ANA_MBHC_MECH, 0x20, 5, 0),
  60. WCD_MBHC_REGISTER("WCD_MBHC_MIC_CLAMP_CTL",
  61. WCD934X_MBHC_NEW_PLUG_DETECT_CTL, 0x30, 4, 0),
  62. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_DETECTION_TYPE",
  63. WCD934X_ANA_MBHC_ELECT, 0x08, 3, 0),
  64. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_CTRL",
  65. WCD934X_MBHC_NEW_PLUG_DETECT_CTL, 0xC0, 6, 0),
  66. WCD_MBHC_REGISTER("WCD_MBHC_HS_L_DET_PULL_UP_COMP_CTRL",
  67. WCD934X_ANA_MBHC_MECH, 0x04, 2, 0),
  68. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PLUG_TYPE",
  69. WCD934X_ANA_MBHC_MECH, 0x10, 4, 0),
  70. WCD_MBHC_REGISTER("WCD_MBHC_GND_PLUG_TYPE",
  71. WCD934X_ANA_MBHC_MECH, 0x08, 3, 0),
  72. WCD_MBHC_REGISTER("WCD_MBHC_SW_HPH_LP_100K_TO_GND",
  73. WCD934X_ANA_MBHC_MECH, 0x01, 0, 0),
  74. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_SCHMT_ISRC",
  75. WCD934X_ANA_MBHC_ELECT, 0x06, 1, 0),
  76. WCD_MBHC_REGISTER("WCD_MBHC_FSM_EN",
  77. WCD934X_ANA_MBHC_ELECT, 0x80, 7, 0),
  78. WCD_MBHC_REGISTER("WCD_MBHC_INSREM_DBNC",
  79. WCD934X_MBHC_NEW_PLUG_DETECT_CTL, 0x0F, 0, 0),
  80. WCD_MBHC_REGISTER("WCD_MBHC_BTN_DBNC",
  81. WCD934X_MBHC_NEW_CTL_1, 0x03, 0, 0),
  82. WCD_MBHC_REGISTER("WCD_MBHC_HS_VREF",
  83. WCD934X_MBHC_NEW_CTL_2, 0x03, 0, 0),
  84. WCD_MBHC_REGISTER("WCD_MBHC_HS_COMP_RESULT",
  85. WCD934X_ANA_MBHC_RESULT_3, 0x08, 3, 0),
  86. WCD_MBHC_REGISTER("WCD_MBHC_MIC_SCHMT_RESULT",
  87. WCD934X_ANA_MBHC_RESULT_3, 0x20, 5, 0),
  88. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_SCHMT_RESULT",
  89. WCD934X_ANA_MBHC_RESULT_3, 0x80, 7, 0),
  90. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_SCHMT_RESULT",
  91. WCD934X_ANA_MBHC_RESULT_3, 0x40, 6, 0),
  92. WCD_MBHC_REGISTER("WCD_MBHC_OCP_FSM_EN",
  93. WCD934X_HPH_OCP_CTL, 0x10, 4, 0),
  94. WCD_MBHC_REGISTER("WCD_MBHC_BTN_RESULT",
  95. WCD934X_ANA_MBHC_RESULT_3, 0x07, 0, 0),
  96. WCD_MBHC_REGISTER("WCD_MBHC_BTN_ISRC_CTL",
  97. WCD934X_ANA_MBHC_ELECT, 0x70, 4, 0),
  98. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_RESULT",
  99. WCD934X_ANA_MBHC_RESULT_3, 0xFF, 0, 0),
  100. WCD_MBHC_REGISTER("WCD_MBHC_MICB_CTRL",
  101. WCD934X_ANA_MICB2, 0xC0, 6, 0),
  102. WCD_MBHC_REGISTER("WCD_MBHC_HPH_CNP_WG_TIME",
  103. WCD934X_HPH_CNP_WG_TIME, 0xFF, 0, 0),
  104. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_PA_EN",
  105. WCD934X_ANA_HPH, 0x40, 6, 0),
  106. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_PA_EN",
  107. WCD934X_ANA_HPH, 0x80, 7, 0),
  108. WCD_MBHC_REGISTER("WCD_MBHC_HPH_PA_EN",
  109. WCD934X_ANA_HPH, 0xC0, 6, 0),
  110. WCD_MBHC_REGISTER("WCD_MBHC_SWCH_LEVEL_REMOVE",
  111. WCD934X_ANA_MBHC_RESULT_3, 0x10, 4, 0),
  112. WCD_MBHC_REGISTER("WCD_MBHC_PULLDOWN_CTRL",
  113. 0, 0, 0, 0),
  114. WCD_MBHC_REGISTER("WCD_MBHC_ANC_DET_EN",
  115. WCD934X_MBHC_CTL_BCS, 0x02, 1, 0),
  116. WCD_MBHC_REGISTER("WCD_MBHC_FSM_STATUS",
  117. WCD934X_MBHC_STATUS_SPARE_1, 0x01, 0, 0),
  118. WCD_MBHC_REGISTER("WCD_MBHC_MUX_CTL",
  119. WCD934X_MBHC_NEW_CTL_2, 0x70, 4, 0),
  120. WCD_MBHC_REGISTER("WCD_MBHC_MOISTURE_STATUS",
  121. WCD934X_MBHC_NEW_FSM_STATUS, 0x20, 5, 0),
  122. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_GND",
  123. WCD934X_HPH_PA_CTL2, 0x40, 6, 0),
  124. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_GND",
  125. WCD934X_HPH_PA_CTL2, 0x10, 4, 0),
  126. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_DET_EN",
  127. WCD934X_HPH_L_TEST, 0x01, 0, 0),
  128. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_DET_EN",
  129. WCD934X_HPH_R_TEST, 0x01, 0, 0),
  130. WCD_MBHC_REGISTER("WCD_MBHC_HPHL_OCP_STATUS",
  131. WCD934X_INTR_PIN1_STATUS0, 0x04, 2, 0),
  132. WCD_MBHC_REGISTER("WCD_MBHC_HPHR_OCP_STATUS",
  133. WCD934X_INTR_PIN1_STATUS0, 0x08, 3, 0),
  134. WCD_MBHC_REGISTER("WCD_MBHC_ADC_EN",
  135. WCD934X_MBHC_NEW_CTL_1, 0x08, 3, 0),
  136. WCD_MBHC_REGISTER("WCD_MBHC_ADC_COMPLETE", WCD934X_MBHC_NEW_FSM_STATUS,
  137. 0x40, 6, 0),
  138. WCD_MBHC_REGISTER("WCD_MBHC_ADC_TIMEOUT", WCD934X_MBHC_NEW_FSM_STATUS,
  139. 0x80, 7, 0),
  140. WCD_MBHC_REGISTER("WCD_MBHC_ADC_RESULT", WCD934X_MBHC_NEW_ADC_RESULT,
  141. 0xFF, 0, 0),
  142. WCD_MBHC_REGISTER("WCD_MBHC_MICB2_VOUT", WCD934X_ANA_MICB2, 0x3F, 0, 0),
  143. WCD_MBHC_REGISTER("WCD_MBHC_ADC_MODE",
  144. WCD934X_MBHC_NEW_CTL_1, 0x10, 4, 0),
  145. WCD_MBHC_REGISTER("WCD_MBHC_DETECTION_DONE",
  146. WCD934X_MBHC_NEW_CTL_1, 0x04, 2, 0),
  147. WCD_MBHC_REGISTER("WCD_MBHC_ELECT_ISRC_EN",
  148. WCD934X_ANA_MBHC_ZDET, 0x02, 1, 0),
  149. };
  150. static const struct wcd_mbhc_intr intr_ids = {
  151. .mbhc_sw_intr = WCD934X_IRQ_MBHC_SW_DET,
  152. .mbhc_btn_press_intr = WCD934X_IRQ_MBHC_BUTTON_PRESS_DET,
  153. .mbhc_btn_release_intr = WCD934X_IRQ_MBHC_BUTTON_RELEASE_DET,
  154. .mbhc_hs_ins_intr = WCD934X_IRQ_MBHC_ELECT_INS_REM_LEG_DET,
  155. .mbhc_hs_rem_intr = WCD934X_IRQ_MBHC_ELECT_INS_REM_DET,
  156. .hph_left_ocp = WCD934X_IRQ_HPH_PA_OCPL_FAULT,
  157. .hph_right_ocp = WCD934X_IRQ_HPH_PA_OCPR_FAULT,
  158. };
  159. static char on_demand_supply_name[][MAX_ON_DEMAND_SUPPLY_NAME_LENGTH] = {
  160. "cdc-vdd-mic-bias",
  161. };
  162. struct tavil_mbhc_zdet_param {
  163. u16 ldo_ctl;
  164. u16 noff;
  165. u16 nshift;
  166. u16 btn5;
  167. u16 btn6;
  168. u16 btn7;
  169. };
  170. static int tavil_mbhc_request_irq(struct snd_soc_codec *codec,
  171. int irq, irq_handler_t handler,
  172. const char *name, void *data)
  173. {
  174. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  175. struct wcd9xxx_core_resource *core_res =
  176. &wcd9xxx->core_res;
  177. return wcd9xxx_request_irq(core_res, irq, handler, name, data);
  178. }
  179. static void tavil_mbhc_irq_control(struct snd_soc_codec *codec,
  180. int irq, bool enable)
  181. {
  182. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  183. struct wcd9xxx_core_resource *core_res =
  184. &wcd9xxx->core_res;
  185. if (enable)
  186. wcd9xxx_enable_irq(core_res, irq);
  187. else
  188. wcd9xxx_disable_irq(core_res, irq);
  189. }
  190. static int tavil_mbhc_free_irq(struct snd_soc_codec *codec,
  191. int irq, void *data)
  192. {
  193. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  194. struct wcd9xxx_core_resource *core_res =
  195. &wcd9xxx->core_res;
  196. wcd9xxx_free_irq(core_res, irq, data);
  197. return 0;
  198. }
  199. static void tavil_mbhc_clk_setup(struct snd_soc_codec *codec,
  200. bool enable)
  201. {
  202. if (enable)
  203. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_1,
  204. 0x80, 0x80);
  205. else
  206. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_1,
  207. 0x80, 0x00);
  208. }
  209. static int tavil_mbhc_btn_to_num(struct snd_soc_codec *codec)
  210. {
  211. return snd_soc_read(codec, WCD934X_ANA_MBHC_RESULT_3) & 0x7;
  212. }
  213. static int tavil_enable_ext_mb_source(struct wcd_mbhc *mbhc,
  214. bool turn_on)
  215. {
  216. struct wcd934x_mbhc *wcd934x_mbhc;
  217. struct snd_soc_codec *codec = mbhc->codec;
  218. struct wcd934x_on_demand_supply *supply;
  219. int ret = 0;
  220. wcd934x_mbhc = container_of(mbhc, struct wcd934x_mbhc, wcd_mbhc);
  221. supply = &wcd934x_mbhc->on_demand_list[WCD934X_ON_DEMAND_MICBIAS];
  222. if (!supply->supply) {
  223. dev_dbg(codec->dev, "%s: warning supply not present ond for %s\n",
  224. __func__, "onDemand Micbias");
  225. return ret;
  226. }
  227. dev_dbg(codec->dev, "%s turn_on: %d count: %d\n", __func__, turn_on,
  228. supply->ondemand_supply_count);
  229. if (turn_on) {
  230. if (!(supply->ondemand_supply_count)) {
  231. ret = snd_soc_dapm_force_enable_pin(
  232. snd_soc_codec_get_dapm(codec),
  233. "MICBIAS_REGULATOR");
  234. snd_soc_dapm_sync(snd_soc_codec_get_dapm(codec));
  235. }
  236. supply->ondemand_supply_count++;
  237. } else {
  238. if (supply->ondemand_supply_count > 0)
  239. supply->ondemand_supply_count--;
  240. if (!(supply->ondemand_supply_count)) {
  241. ret = snd_soc_dapm_disable_pin(
  242. snd_soc_codec_get_dapm(codec),
  243. "MICBIAS_REGULATOR");
  244. snd_soc_dapm_sync(snd_soc_codec_get_dapm(codec));
  245. }
  246. }
  247. if (ret)
  248. dev_err(codec->dev, "%s: Failed to %s external micbias source\n",
  249. __func__, turn_on ? "enable" : "disabled");
  250. else
  251. dev_dbg(codec->dev, "%s: %s external micbias source\n",
  252. __func__, turn_on ? "Enabled" : "Disabled");
  253. return ret;
  254. }
  255. static void tavil_mbhc_mbhc_bias_control(struct snd_soc_codec *codec,
  256. bool enable)
  257. {
  258. if (enable)
  259. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_ELECT,
  260. 0x01, 0x01);
  261. else
  262. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_ELECT,
  263. 0x01, 0x00);
  264. }
  265. static void tavil_mbhc_program_btn_thr(struct snd_soc_codec *codec,
  266. s16 *btn_low, s16 *btn_high,
  267. int num_btn, bool is_micbias)
  268. {
  269. int i;
  270. int vth;
  271. if (num_btn > WCD_MBHC_DEF_BUTTONS) {
  272. dev_err(codec->dev, "%s: invalid number of buttons: %d\n",
  273. __func__, num_btn);
  274. return;
  275. }
  276. /*
  277. * Tavil just needs one set of thresholds for button detection
  278. * due to micbias voltage ramp to pullup upon button press. So
  279. * btn_low and is_micbias are ignored and always program button
  280. * thresholds using btn_high.
  281. */
  282. for (i = 0; i < num_btn; i++) {
  283. vth = ((btn_high[i] * 2) / 25) & 0x3F;
  284. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_BTN0 + i,
  285. 0xFC, vth << 2);
  286. dev_dbg(codec->dev, "%s: btn_high[%d]: %d, vth: %d\n",
  287. __func__, i, btn_high[i], vth);
  288. }
  289. }
  290. static bool tavil_mbhc_lock_sleep(struct wcd_mbhc *mbhc, bool lock)
  291. {
  292. struct snd_soc_codec *codec = mbhc->codec;
  293. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  294. struct wcd9xxx_core_resource *core_res =
  295. &wcd9xxx->core_res;
  296. bool ret = 0;
  297. if (lock)
  298. ret = wcd9xxx_lock_sleep(core_res);
  299. else
  300. wcd9xxx_unlock_sleep(core_res);
  301. return ret;
  302. }
  303. static int tavil_mbhc_register_notifier(struct wcd_mbhc *mbhc,
  304. struct notifier_block *nblock,
  305. bool enable)
  306. {
  307. struct wcd934x_mbhc *wcd934x_mbhc;
  308. wcd934x_mbhc = container_of(mbhc, struct wcd934x_mbhc, wcd_mbhc);
  309. if (enable)
  310. return blocking_notifier_chain_register(&wcd934x_mbhc->notifier,
  311. nblock);
  312. else
  313. return blocking_notifier_chain_unregister(
  314. &wcd934x_mbhc->notifier, nblock);
  315. }
  316. static bool tavil_mbhc_micb_en_status(struct wcd_mbhc *mbhc, int micb_num)
  317. {
  318. u8 val;
  319. if (micb_num == MIC_BIAS_2) {
  320. val = (snd_soc_read(mbhc->codec, WCD934X_ANA_MICB2) >> 6);
  321. if (val == 0x01)
  322. return true;
  323. }
  324. return false;
  325. }
  326. static bool tavil_mbhc_hph_pa_on_status(struct snd_soc_codec *codec)
  327. {
  328. return (snd_soc_read(codec, WCD934X_ANA_HPH) & 0xC0) ? true : false;
  329. }
  330. static void tavil_mbhc_hph_l_pull_up_control(
  331. struct snd_soc_codec *codec,
  332. enum mbhc_hs_pullup_iref pull_up_cur)
  333. {
  334. /* Default pull up current to 2uA */
  335. if (pull_up_cur < I_OFF || pull_up_cur > I_3P0_UA ||
  336. pull_up_cur == I_DEFAULT)
  337. pull_up_cur = I_2P0_UA;
  338. dev_dbg(codec->dev, "%s: HS pull up current:%d\n",
  339. __func__, pull_up_cur);
  340. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_PLUG_DETECT_CTL,
  341. 0xC0, pull_up_cur << 6);
  342. }
  343. static int tavil_mbhc_request_micbias(struct snd_soc_codec *codec,
  344. int micb_num, int req)
  345. {
  346. int ret;
  347. /*
  348. * If micbias is requested, make sure that there
  349. * is vote to enable mclk
  350. */
  351. if (req == MICB_ENABLE)
  352. tavil_cdc_mclk_enable(codec, true);
  353. ret = tavil_micbias_control(codec, micb_num, req, false);
  354. /*
  355. * Release vote for mclk while requesting for
  356. * micbias disable
  357. */
  358. if (req == MICB_DISABLE)
  359. tavil_cdc_mclk_enable(codec, false);
  360. return ret;
  361. }
  362. static void tavil_mbhc_micb_ramp_control(struct snd_soc_codec *codec,
  363. bool enable)
  364. {
  365. if (enable) {
  366. snd_soc_update_bits(codec, WCD934X_ANA_MICB2_RAMP,
  367. 0x1C, 0x0C);
  368. snd_soc_update_bits(codec, WCD934X_ANA_MICB2_RAMP,
  369. 0x80, 0x80);
  370. } else {
  371. snd_soc_update_bits(codec, WCD934X_ANA_MICB2_RAMP,
  372. 0x80, 0x00);
  373. snd_soc_update_bits(codec, WCD934X_ANA_MICB2_RAMP,
  374. 0x1C, 0x00);
  375. }
  376. }
  377. static struct firmware_cal *tavil_get_hwdep_fw_cal(struct wcd_mbhc *mbhc,
  378. enum wcd_cal_type type)
  379. {
  380. struct wcd934x_mbhc *wcd934x_mbhc;
  381. struct firmware_cal *hwdep_cal;
  382. struct snd_soc_codec *codec = mbhc->codec;
  383. wcd934x_mbhc = container_of(mbhc, struct wcd934x_mbhc, wcd_mbhc);
  384. if (!codec) {
  385. pr_err("%s: NULL codec pointer\n", __func__);
  386. return NULL;
  387. }
  388. hwdep_cal = wcdcal_get_fw_cal(wcd934x_mbhc->fw_data, type);
  389. if (!hwdep_cal)
  390. dev_err(codec->dev, "%s: cal not sent by %d\n",
  391. __func__, type);
  392. return hwdep_cal;
  393. }
  394. static int tavil_mbhc_micb_ctrl_threshold_mic(struct snd_soc_codec *codec,
  395. int micb_num, bool req_en)
  396. {
  397. struct wcd9xxx_pdata *pdata = dev_get_platdata(codec->dev->parent);
  398. int rc, micb_mv;
  399. if (micb_num != MIC_BIAS_2)
  400. return -EINVAL;
  401. /*
  402. * If device tree micbias level is already above the minimum
  403. * voltage needed to detect threshold microphone, then do
  404. * not change the micbias, just return.
  405. */
  406. if (pdata->micbias.micb2_mv >= WCD_MBHC_THR_HS_MICB_MV)
  407. return 0;
  408. micb_mv = req_en ? WCD_MBHC_THR_HS_MICB_MV : pdata->micbias.micb2_mv;
  409. rc = tavil_mbhc_micb_adjust_voltage(codec, micb_mv, MIC_BIAS_2);
  410. return rc;
  411. }
  412. static inline void tavil_mbhc_get_result_params(struct wcd9xxx *wcd9xxx,
  413. s16 *d1_a, u16 noff,
  414. int32_t *zdet)
  415. {
  416. int i;
  417. int val, val1;
  418. s16 c1;
  419. s32 x1, d1;
  420. int32_t denom;
  421. int minCode_param[] = {
  422. 3277, 1639, 820, 410, 205, 103, 52, 26
  423. };
  424. regmap_update_bits(wcd9xxx->regmap, WCD934X_ANA_MBHC_ZDET, 0x20, 0x20);
  425. for (i = 0; i < TAVIL_ZDET_NUM_MEASUREMENTS; i++) {
  426. regmap_read(wcd9xxx->regmap, WCD934X_ANA_MBHC_RESULT_2, &val);
  427. if (val & 0x80)
  428. break;
  429. }
  430. val = val << 0x8;
  431. regmap_read(wcd9xxx->regmap, WCD934X_ANA_MBHC_RESULT_1, &val1);
  432. val |= val1;
  433. regmap_update_bits(wcd9xxx->regmap, WCD934X_ANA_MBHC_ZDET, 0x20, 0x00);
  434. x1 = TAVIL_MBHC_GET_X1(val);
  435. c1 = TAVIL_MBHC_GET_C1(val);
  436. /* If ramp is not complete, give additional 5ms */
  437. if ((c1 < 2) && x1)
  438. usleep_range(5000, 5050);
  439. if (!c1 || !x1) {
  440. dev_dbg(wcd9xxx->dev,
  441. "%s: Impedance detect ramp error, c1=%d, x1=0x%x\n",
  442. __func__, c1, x1);
  443. goto ramp_down;
  444. }
  445. d1 = d1_a[c1];
  446. denom = (x1 * d1) - (1 << (14 - noff));
  447. if (denom > 0)
  448. *zdet = (TAVIL_MBHC_ZDET_CONST * 1000) / denom;
  449. else if (x1 < minCode_param[noff])
  450. *zdet = TAVIL_ZDET_FLOATING_IMPEDANCE;
  451. dev_dbg(wcd9xxx->dev, "%s: d1=%d, c1=%d, x1=0x%x, z_val=%d(milliOhm)\n",
  452. __func__, d1, c1, x1, *zdet);
  453. ramp_down:
  454. i = 0;
  455. while (x1) {
  456. regmap_bulk_read(wcd9xxx->regmap,
  457. WCD934X_ANA_MBHC_RESULT_1, (u8 *)&val, 2);
  458. x1 = TAVIL_MBHC_GET_X1(val);
  459. i++;
  460. if (i == TAVIL_ZDET_NUM_MEASUREMENTS)
  461. break;
  462. }
  463. }
  464. static void tavil_mbhc_zdet_ramp(struct snd_soc_codec *codec,
  465. struct tavil_mbhc_zdet_param *zdet_param,
  466. int32_t *zl, int32_t *zr, s16 *d1_a)
  467. {
  468. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  469. int32_t zdet = 0;
  470. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_ZDET_ANA_CTL, 0x70,
  471. zdet_param->ldo_ctl << 4);
  472. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_BTN5, 0xFC,
  473. zdet_param->btn5);
  474. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_BTN6, 0xFC,
  475. zdet_param->btn6);
  476. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_BTN7, 0xFC,
  477. zdet_param->btn7);
  478. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_ZDET_ANA_CTL, 0x0F,
  479. zdet_param->noff);
  480. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_ZDET_RAMP_CTL, 0x0F,
  481. zdet_param->nshift);
  482. if (!zl)
  483. goto z_right;
  484. /* Start impedance measurement for HPH_L */
  485. regmap_update_bits(wcd9xxx->regmap,
  486. WCD934X_ANA_MBHC_ZDET, 0x80, 0x80);
  487. dev_dbg(wcd9xxx->dev, "%s: ramp for HPH_L, noff = %d\n",
  488. __func__, zdet_param->noff);
  489. tavil_mbhc_get_result_params(wcd9xxx, d1_a, zdet_param->noff, &zdet);
  490. regmap_update_bits(wcd9xxx->regmap,
  491. WCD934X_ANA_MBHC_ZDET, 0x80, 0x00);
  492. *zl = zdet;
  493. z_right:
  494. if (!zr)
  495. return;
  496. /* Start impedance measurement for HPH_R */
  497. regmap_update_bits(wcd9xxx->regmap,
  498. WCD934X_ANA_MBHC_ZDET, 0x40, 0x40);
  499. dev_dbg(wcd9xxx->dev, "%s: ramp for HPH_R, noff = %d\n",
  500. __func__, zdet_param->noff);
  501. tavil_mbhc_get_result_params(wcd9xxx, d1_a, zdet_param->noff, &zdet);
  502. regmap_update_bits(wcd9xxx->regmap,
  503. WCD934X_ANA_MBHC_ZDET, 0x40, 0x00);
  504. *zr = zdet;
  505. }
  506. static inline void tavil_wcd_mbhc_qfuse_cal(struct snd_soc_codec *codec,
  507. int32_t *z_val, int flag_l_r)
  508. {
  509. s16 q1;
  510. int q1_cal;
  511. if (*z_val < (TAVIL_ZDET_VAL_400/1000))
  512. q1 = snd_soc_read(codec,
  513. WCD934X_CHIP_TIER_CTRL_EFUSE_VAL_OUT1 + (2 * flag_l_r));
  514. else
  515. q1 = snd_soc_read(codec,
  516. WCD934X_CHIP_TIER_CTRL_EFUSE_VAL_OUT2 + (2 * flag_l_r));
  517. if (q1 & 0x80)
  518. q1_cal = (10000 - ((q1 & 0x7F) * 25));
  519. else
  520. q1_cal = (10000 + (q1 * 25));
  521. if (q1_cal > 0)
  522. *z_val = ((*z_val) * 10000) / q1_cal;
  523. }
  524. static void tavil_wcd_mbhc_calc_impedance(struct wcd_mbhc *mbhc, uint32_t *zl,
  525. uint32_t *zr)
  526. {
  527. struct snd_soc_codec *codec = mbhc->codec;
  528. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  529. s16 reg0, reg1, reg2, reg3, reg4;
  530. int32_t z1L, z1R, z1Ls;
  531. int zMono, z_diff1, z_diff2;
  532. bool is_fsm_disable = false;
  533. struct tavil_mbhc_zdet_param zdet_param[] = {
  534. {4, 0, 4, 0x08, 0x14, 0x18}, /* < 32ohm */
  535. {2, 0, 3, 0x18, 0x7C, 0x90}, /* 32ohm < Z < 400ohm */
  536. {1, 4, 5, 0x18, 0x7C, 0x90}, /* 400ohm < Z < 1200ohm */
  537. {1, 6, 7, 0x18, 0x7C, 0x90}, /* >1200ohm */
  538. };
  539. struct tavil_mbhc_zdet_param *zdet_param_ptr = NULL;
  540. s16 d1_a[][4] = {
  541. {0, 30, 90, 30},
  542. {0, 30, 30, 5},
  543. {0, 30, 30, 5},
  544. {0, 30, 30, 5},
  545. };
  546. s16 *d1 = NULL;
  547. WCD_MBHC_RSC_ASSERT_LOCKED(mbhc);
  548. reg0 = snd_soc_read(codec, WCD934X_ANA_MBHC_BTN5);
  549. reg1 = snd_soc_read(codec, WCD934X_ANA_MBHC_BTN6);
  550. reg2 = snd_soc_read(codec, WCD934X_ANA_MBHC_BTN7);
  551. reg3 = snd_soc_read(codec, WCD934X_MBHC_CTL_CLK);
  552. reg4 = snd_soc_read(codec, WCD934X_MBHC_NEW_ZDET_ANA_CTL);
  553. if (snd_soc_read(codec, WCD934X_ANA_MBHC_ELECT) & 0x80) {
  554. is_fsm_disable = true;
  555. regmap_update_bits(wcd9xxx->regmap,
  556. WCD934X_ANA_MBHC_ELECT, 0x80, 0x00);
  557. }
  558. /* For NO-jack, disable L_DET_EN before Z-det measurements */
  559. if (mbhc->hphl_swh)
  560. regmap_update_bits(wcd9xxx->regmap,
  561. WCD934X_ANA_MBHC_MECH, 0x80, 0x00);
  562. /* Turn off 100k pull down on HPHL */
  563. regmap_update_bits(wcd9xxx->regmap,
  564. WCD934X_ANA_MBHC_MECH, 0x01, 0x00);
  565. /* First get impedance on Left */
  566. d1 = d1_a[1];
  567. zdet_param_ptr = &zdet_param[1];
  568. tavil_mbhc_zdet_ramp(codec, zdet_param_ptr, &z1L, NULL, d1);
  569. if (!TAVIL_MBHC_IS_SECOND_RAMP_REQUIRED(z1L))
  570. goto left_ch_impedance;
  571. /* Second ramp for left ch */
  572. if (z1L < TAVIL_ZDET_VAL_32) {
  573. zdet_param_ptr = &zdet_param[0];
  574. d1 = d1_a[0];
  575. } else if ((z1L > TAVIL_ZDET_VAL_400) && (z1L <= TAVIL_ZDET_VAL_1200)) {
  576. zdet_param_ptr = &zdet_param[2];
  577. d1 = d1_a[2];
  578. } else if (z1L > TAVIL_ZDET_VAL_1200) {
  579. zdet_param_ptr = &zdet_param[3];
  580. d1 = d1_a[3];
  581. }
  582. tavil_mbhc_zdet_ramp(codec, zdet_param_ptr, &z1L, NULL, d1);
  583. left_ch_impedance:
  584. if ((z1L == TAVIL_ZDET_FLOATING_IMPEDANCE) ||
  585. (z1L > TAVIL_ZDET_VAL_100K)) {
  586. *zl = TAVIL_ZDET_FLOATING_IMPEDANCE;
  587. zdet_param_ptr = &zdet_param[1];
  588. d1 = d1_a[1];
  589. } else {
  590. *zl = z1L/1000;
  591. tavil_wcd_mbhc_qfuse_cal(codec, zl, 0);
  592. }
  593. dev_dbg(codec->dev, "%s: impedance on HPH_L = %d(ohms)\n",
  594. __func__, *zl);
  595. /* Start of right impedance ramp and calculation */
  596. tavil_mbhc_zdet_ramp(codec, zdet_param_ptr, NULL, &z1R, d1);
  597. if (TAVIL_MBHC_IS_SECOND_RAMP_REQUIRED(z1R)) {
  598. if (((z1R > TAVIL_ZDET_VAL_1200) &&
  599. (zdet_param_ptr->noff == 0x6)) ||
  600. ((*zl) != TAVIL_ZDET_FLOATING_IMPEDANCE))
  601. goto right_ch_impedance;
  602. /* Second ramp for right ch */
  603. if (z1R < TAVIL_ZDET_VAL_32) {
  604. zdet_param_ptr = &zdet_param[0];
  605. d1 = d1_a[0];
  606. } else if ((z1R > TAVIL_ZDET_VAL_400) &&
  607. (z1R <= TAVIL_ZDET_VAL_1200)) {
  608. zdet_param_ptr = &zdet_param[2];
  609. d1 = d1_a[2];
  610. } else if (z1R > TAVIL_ZDET_VAL_1200) {
  611. zdet_param_ptr = &zdet_param[3];
  612. d1 = d1_a[3];
  613. }
  614. tavil_mbhc_zdet_ramp(codec, zdet_param_ptr, NULL, &z1R, d1);
  615. }
  616. right_ch_impedance:
  617. if ((z1R == TAVIL_ZDET_FLOATING_IMPEDANCE) ||
  618. (z1R > TAVIL_ZDET_VAL_100K)) {
  619. *zr = TAVIL_ZDET_FLOATING_IMPEDANCE;
  620. } else {
  621. *zr = z1R/1000;
  622. tavil_wcd_mbhc_qfuse_cal(codec, zr, 1);
  623. }
  624. dev_dbg(codec->dev, "%s: impedance on HPH_R = %d(ohms)\n",
  625. __func__, *zr);
  626. /* Mono/stereo detection */
  627. if ((*zl == TAVIL_ZDET_FLOATING_IMPEDANCE) &&
  628. (*zr == TAVIL_ZDET_FLOATING_IMPEDANCE)) {
  629. dev_dbg(codec->dev,
  630. "%s: plug type is invalid or extension cable\n",
  631. __func__);
  632. goto zdet_complete;
  633. }
  634. if ((*zl == TAVIL_ZDET_FLOATING_IMPEDANCE) ||
  635. (*zr == TAVIL_ZDET_FLOATING_IMPEDANCE) ||
  636. ((*zl < WCD_MONO_HS_MIN_THR) && (*zr > WCD_MONO_HS_MIN_THR)) ||
  637. ((*zl > WCD_MONO_HS_MIN_THR) && (*zr < WCD_MONO_HS_MIN_THR))) {
  638. dev_dbg(codec->dev,
  639. "%s: Mono plug type with one ch floating or shorted to GND\n",
  640. __func__);
  641. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  642. goto zdet_complete;
  643. }
  644. snd_soc_update_bits(codec, WCD934X_HPH_R_ATEST, 0x02, 0x02);
  645. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2, 0x40, 0x01);
  646. if (*zl < (TAVIL_ZDET_VAL_32/1000))
  647. tavil_mbhc_zdet_ramp(codec, &zdet_param[0], &z1Ls, NULL, d1);
  648. else
  649. tavil_mbhc_zdet_ramp(codec, &zdet_param[1], &z1Ls, NULL, d1);
  650. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2, 0x40, 0x00);
  651. snd_soc_update_bits(codec, WCD934X_HPH_R_ATEST, 0x02, 0x00);
  652. z1Ls /= 1000;
  653. tavil_wcd_mbhc_qfuse_cal(codec, &z1Ls, 0);
  654. /* Parallel of left Z and 9 ohm pull down resistor */
  655. zMono = ((*zl) * 9) / ((*zl) + 9);
  656. z_diff1 = (z1Ls > zMono) ? (z1Ls - zMono) : (zMono - z1Ls);
  657. z_diff2 = ((*zl) > z1Ls) ? ((*zl) - z1Ls) : (z1Ls - (*zl));
  658. if ((z_diff1 * (*zl + z1Ls)) > (z_diff2 * (z1Ls + zMono))) {
  659. dev_dbg(codec->dev, "%s: stereo plug type detected\n",
  660. __func__);
  661. mbhc->hph_type = WCD_MBHC_HPH_STEREO;
  662. } else {
  663. dev_dbg(codec->dev, "%s: MONO plug type detected\n",
  664. __func__);
  665. mbhc->hph_type = WCD_MBHC_HPH_MONO;
  666. }
  667. zdet_complete:
  668. snd_soc_write(codec, WCD934X_ANA_MBHC_BTN5, reg0);
  669. snd_soc_write(codec, WCD934X_ANA_MBHC_BTN6, reg1);
  670. snd_soc_write(codec, WCD934X_ANA_MBHC_BTN7, reg2);
  671. /* Turn on 100k pull down on HPHL */
  672. regmap_update_bits(wcd9xxx->regmap,
  673. WCD934X_ANA_MBHC_MECH, 0x01, 0x01);
  674. /* For NO-jack, re-enable L_DET_EN after Z-det measurements */
  675. if (mbhc->hphl_swh)
  676. regmap_update_bits(wcd9xxx->regmap,
  677. WCD934X_ANA_MBHC_MECH, 0x80, 0x80);
  678. snd_soc_write(codec, WCD934X_MBHC_NEW_ZDET_ANA_CTL, reg4);
  679. snd_soc_write(codec, WCD934X_MBHC_CTL_CLK, reg3);
  680. if (is_fsm_disable)
  681. regmap_update_bits(wcd9xxx->regmap,
  682. WCD934X_ANA_MBHC_ELECT, 0x80, 0x80);
  683. }
  684. static void tavil_mbhc_gnd_det_ctrl(struct snd_soc_codec *codec, bool enable)
  685. {
  686. if (enable) {
  687. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_MECH,
  688. 0x02, 0x02);
  689. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_MECH,
  690. 0x40, 0x40);
  691. } else {
  692. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_MECH,
  693. 0x40, 0x00);
  694. snd_soc_update_bits(codec, WCD934X_ANA_MBHC_MECH,
  695. 0x02, 0x00);
  696. }
  697. }
  698. static void tavil_mbhc_hph_pull_down_ctrl(struct snd_soc_codec *codec,
  699. bool enable)
  700. {
  701. if (enable) {
  702. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2,
  703. 0x40, 0x40);
  704. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2,
  705. 0x10, 0x10);
  706. } else {
  707. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2,
  708. 0x40, 0x00);
  709. snd_soc_update_bits(codec, WCD934X_HPH_PA_CTL2,
  710. 0x10, 0x00);
  711. }
  712. }
  713. static void tavil_mbhc_moisture_config(struct wcd_mbhc *mbhc)
  714. {
  715. struct snd_soc_codec *codec = mbhc->codec;
  716. if ((mbhc->moist_rref == R_OFF) ||
  717. (mbhc->mbhc_cfg->enable_usbc_analog)) {
  718. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_2,
  719. 0x0C, R_OFF << 2);
  720. return;
  721. }
  722. /* Donot enable moisture detection if jack type is NC */
  723. if (!mbhc->hphl_swh) {
  724. dev_dbg(codec->dev, "%s: disable moisture detection for NC\n",
  725. __func__);
  726. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_2,
  727. 0x0C, R_OFF << 2);
  728. return;
  729. }
  730. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_2,
  731. 0x0C, mbhc->moist_rref << 2);
  732. }
  733. static bool tavil_hph_register_recovery(struct wcd_mbhc *mbhc)
  734. {
  735. struct snd_soc_codec *codec = mbhc->codec;
  736. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  737. if (!wcd934x_mbhc)
  738. return false;
  739. wcd934x_mbhc->is_hph_recover = false;
  740. snd_soc_dapm_force_enable_pin(snd_soc_codec_get_dapm(codec),
  741. "RESET_HPH_REGISTERS");
  742. snd_soc_dapm_sync(snd_soc_codec_get_dapm(codec));
  743. snd_soc_dapm_disable_pin(snd_soc_codec_get_dapm(codec),
  744. "RESET_HPH_REGISTERS");
  745. snd_soc_dapm_sync(snd_soc_codec_get_dapm(codec));
  746. return wcd934x_mbhc->is_hph_recover;
  747. }
  748. static void tavil_update_anc_state(struct snd_soc_codec *codec, bool enable,
  749. int anc_num)
  750. {
  751. if (enable)
  752. snd_soc_update_bits(codec, WCD934X_CDC_RX1_RX_PATH_CFG0 +
  753. (20 * anc_num), 0x10, 0x10);
  754. else
  755. snd_soc_update_bits(codec, WCD934X_CDC_RX1_RX_PATH_CFG0 +
  756. (20 * anc_num), 0x10, 0x00);
  757. }
  758. static bool tavil_is_anc_on(struct wcd_mbhc *mbhc)
  759. {
  760. bool anc_on = false;
  761. u16 ancl, ancr;
  762. ancl =
  763. (snd_soc_read(mbhc->codec, WCD934X_CDC_RX1_RX_PATH_CFG0)) & 0x10;
  764. ancr =
  765. (snd_soc_read(mbhc->codec, WCD934X_CDC_RX2_RX_PATH_CFG0)) & 0x10;
  766. anc_on = !!(ancl | ancr);
  767. return anc_on;
  768. }
  769. static const struct wcd_mbhc_cb mbhc_cb = {
  770. .request_irq = tavil_mbhc_request_irq,
  771. .irq_control = tavil_mbhc_irq_control,
  772. .free_irq = tavil_mbhc_free_irq,
  773. .clk_setup = tavil_mbhc_clk_setup,
  774. .map_btn_code_to_num = tavil_mbhc_btn_to_num,
  775. .enable_mb_source = tavil_enable_ext_mb_source,
  776. .mbhc_bias = tavil_mbhc_mbhc_bias_control,
  777. .set_btn_thr = tavil_mbhc_program_btn_thr,
  778. .lock_sleep = tavil_mbhc_lock_sleep,
  779. .register_notifier = tavil_mbhc_register_notifier,
  780. .micbias_enable_status = tavil_mbhc_micb_en_status,
  781. .hph_pa_on_status = tavil_mbhc_hph_pa_on_status,
  782. .hph_pull_up_control = tavil_mbhc_hph_l_pull_up_control,
  783. .mbhc_micbias_control = tavil_mbhc_request_micbias,
  784. .mbhc_micb_ramp_control = tavil_mbhc_micb_ramp_control,
  785. .get_hwdep_fw_cal = tavil_get_hwdep_fw_cal,
  786. .mbhc_micb_ctrl_thr_mic = tavil_mbhc_micb_ctrl_threshold_mic,
  787. .compute_impedance = tavil_wcd_mbhc_calc_impedance,
  788. .mbhc_gnd_det_ctrl = tavil_mbhc_gnd_det_ctrl,
  789. .hph_pull_down_ctrl = tavil_mbhc_hph_pull_down_ctrl,
  790. .mbhc_moisture_config = tavil_mbhc_moisture_config,
  791. .hph_register_recovery = tavil_hph_register_recovery,
  792. .update_anc_state = tavil_update_anc_state,
  793. .is_anc_on = tavil_is_anc_on,
  794. };
  795. static struct regulator *tavil_codec_find_ondemand_regulator(
  796. struct snd_soc_codec *codec, const char *name)
  797. {
  798. int i;
  799. struct wcd9xxx *wcd9xxx = dev_get_drvdata(codec->dev->parent);
  800. struct wcd9xxx_pdata *pdata = dev_get_platdata(codec->dev->parent);
  801. for (i = 0; i < wcd9xxx->num_of_supplies; ++i) {
  802. if (pdata->regulator[i].ondemand &&
  803. wcd9xxx->supplies[i].supply &&
  804. !strcmp(wcd9xxx->supplies[i].supply, name))
  805. return wcd9xxx->supplies[i].consumer;
  806. }
  807. dev_dbg(codec->dev, "Warning: regulator not found:%s\n",
  808. name);
  809. return NULL;
  810. }
  811. static int tavil_get_hph_type(struct snd_kcontrol *kcontrol,
  812. struct snd_ctl_elem_value *ucontrol)
  813. {
  814. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  815. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  816. struct wcd_mbhc *mbhc;
  817. if (!wcd934x_mbhc) {
  818. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  819. return -EINVAL;
  820. }
  821. mbhc = &wcd934x_mbhc->wcd_mbhc;
  822. ucontrol->value.integer.value[0] = (u32) mbhc->hph_type;
  823. dev_dbg(codec->dev, "%s: hph_type = %u\n", __func__, mbhc->hph_type);
  824. return 0;
  825. }
  826. static int tavil_hph_impedance_get(struct snd_kcontrol *kcontrol,
  827. struct snd_ctl_elem_value *ucontrol)
  828. {
  829. uint32_t zl, zr;
  830. bool hphr;
  831. struct soc_multi_mixer_control *mc;
  832. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  833. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  834. if (!wcd934x_mbhc) {
  835. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  836. return -EINVAL;
  837. }
  838. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  839. hphr = mc->shift;
  840. wcd_mbhc_get_impedance(&wcd934x_mbhc->wcd_mbhc, &zl, &zr);
  841. dev_dbg(codec->dev, "%s: zl=%u(ohms), zr=%u(ohms)\n", __func__, zl, zr);
  842. ucontrol->value.integer.value[0] = hphr ? zr : zl;
  843. return 0;
  844. }
  845. static const struct snd_kcontrol_new hph_type_detect_controls[] = {
  846. SOC_SINGLE_EXT("HPH Type", 0, 0, UINT_MAX, 0,
  847. tavil_get_hph_type, NULL),
  848. };
  849. static const struct snd_kcontrol_new impedance_detect_controls[] = {
  850. SOC_SINGLE_EXT("HPHL Impedance", 0, 0, UINT_MAX, 0,
  851. tavil_hph_impedance_get, NULL),
  852. SOC_SINGLE_EXT("HPHR Impedance", 0, 1, UINT_MAX, 0,
  853. tavil_hph_impedance_get, NULL),
  854. };
  855. /*
  856. * tavil_mbhc_get_impedance: get impedance of headphone left and right channels
  857. * @wcd934x_mbhc: handle to struct wcd934x_mbhc *
  858. * @zl: handle to left-ch impedance
  859. * @zr: handle to right-ch impedance
  860. * return 0 for success or error code in case of failure
  861. */
  862. int tavil_mbhc_get_impedance(struct wcd934x_mbhc *wcd934x_mbhc,
  863. uint32_t *zl, uint32_t *zr)
  864. {
  865. if (!wcd934x_mbhc) {
  866. pr_err("%s: mbhc not initialized!\n", __func__);
  867. return -EINVAL;
  868. }
  869. if (!zl || !zr) {
  870. pr_err("%s: zl or zr null!\n", __func__);
  871. return -EINVAL;
  872. }
  873. return wcd_mbhc_get_impedance(&wcd934x_mbhc->wcd_mbhc, zl, zr);
  874. }
  875. EXPORT_SYMBOL(tavil_mbhc_get_impedance);
  876. /*
  877. * tavil_mbhc_hs_detect: starts mbhc insertion/removal functionality
  878. * @codec: handle to snd_soc_codec *
  879. * @mbhc_cfg: handle to mbhc configuration structure
  880. * return 0 if mbhc_start is success or error code in case of failure
  881. */
  882. int tavil_mbhc_hs_detect(struct snd_soc_codec *codec,
  883. struct wcd_mbhc_config *mbhc_cfg)
  884. {
  885. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  886. if (!wcd934x_mbhc) {
  887. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  888. return -EINVAL;
  889. }
  890. return wcd_mbhc_start(&wcd934x_mbhc->wcd_mbhc, mbhc_cfg);
  891. }
  892. EXPORT_SYMBOL(tavil_mbhc_hs_detect);
  893. /*
  894. * tavil_mbhc_hs_detect_exit: stop mbhc insertion/removal functionality
  895. * @codec: handle to snd_soc_codec *
  896. */
  897. void tavil_mbhc_hs_detect_exit(struct snd_soc_codec *codec)
  898. {
  899. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  900. if (!wcd934x_mbhc) {
  901. dev_err(codec->dev, "%s: mbhc not initialized!\n", __func__);
  902. return;
  903. }
  904. wcd_mbhc_stop(&wcd934x_mbhc->wcd_mbhc);
  905. }
  906. EXPORT_SYMBOL(tavil_mbhc_hs_detect_exit);
  907. /*
  908. * tavil_mbhc_post_ssr_init: initialize mbhc for tavil post subsystem restart
  909. * @mbhc: poniter to wcd934x_mbhc structure
  910. * @codec: handle to snd_soc_codec *
  911. *
  912. * return 0 if mbhc_init is success or error code in case of failure
  913. */
  914. int tavil_mbhc_post_ssr_init(struct wcd934x_mbhc *mbhc,
  915. struct snd_soc_codec *codec)
  916. {
  917. int ret;
  918. struct wcd_mbhc *wcd_mbhc;
  919. if (!mbhc || !codec)
  920. return -EINVAL;
  921. wcd_mbhc = &mbhc->wcd_mbhc;
  922. if (wcd_mbhc == NULL) {
  923. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  924. return -EINVAL;
  925. }
  926. tavil_mbhc_hs_detect_exit(codec);
  927. wcd_mbhc_deinit(wcd_mbhc);
  928. ret = wcd_mbhc_init(wcd_mbhc, codec, &mbhc_cb, &intr_ids,
  929. wcd_mbhc_registers, TAVIL_ZDET_SUPPORTED);
  930. if (ret) {
  931. dev_err(codec->dev, "%s: mbhc initialization failed\n",
  932. __func__);
  933. goto done;
  934. }
  935. if (wcd_mbhc->mbhc_detection_logic == WCD_DETECTION_LEGACY) {
  936. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_1, 0x04, 0x04);
  937. snd_soc_update_bits(codec, WCD934X_MBHC_CTL_BCS, 0x01, 0x01);
  938. }
  939. done:
  940. return ret;
  941. }
  942. EXPORT_SYMBOL(tavil_mbhc_post_ssr_init);
  943. /*
  944. * tavil_mbhc_init: initialize mbhc for tavil
  945. * @mbhc: poniter to wcd934x_mbhc struct pointer to store the configs
  946. * @codec: handle to snd_soc_codec *
  947. * @fw_data: handle to firmware data
  948. *
  949. * return 0 if mbhc_init is success or error code in case of failure
  950. */
  951. int tavil_mbhc_init(struct wcd934x_mbhc **mbhc, struct snd_soc_codec *codec,
  952. struct fw_info *fw_data)
  953. {
  954. struct regulator *supply;
  955. struct wcd934x_mbhc *wcd934x_mbhc;
  956. struct wcd_mbhc *wcd_mbhc;
  957. int ret;
  958. wcd934x_mbhc = devm_kzalloc(codec->dev, sizeof(struct wcd934x_mbhc),
  959. GFP_KERNEL);
  960. if (!wcd934x_mbhc)
  961. return -ENOMEM;
  962. wcd934x_mbhc->wcd9xxx = dev_get_drvdata(codec->dev->parent);
  963. wcd934x_mbhc->fw_data = fw_data;
  964. BLOCKING_INIT_NOTIFIER_HEAD(&wcd934x_mbhc->notifier);
  965. wcd_mbhc = &wcd934x_mbhc->wcd_mbhc;
  966. if (wcd_mbhc == NULL) {
  967. pr_err("%s: wcd_mbhc is NULL\n", __func__);
  968. ret = -EINVAL;
  969. goto err;
  970. }
  971. /* Setting default mbhc detection logic to ADC for Tavil */
  972. wcd_mbhc->mbhc_detection_logic = WCD_DETECTION_ADC;
  973. ret = wcd_mbhc_init(wcd_mbhc, codec, &mbhc_cb,
  974. &intr_ids, wcd_mbhc_registers,
  975. TAVIL_ZDET_SUPPORTED);
  976. if (ret) {
  977. dev_err(codec->dev, "%s: mbhc initialization failed\n",
  978. __func__);
  979. goto err;
  980. }
  981. supply = tavil_codec_find_ondemand_regulator(codec,
  982. on_demand_supply_name[WCD934X_ON_DEMAND_MICBIAS]);
  983. if (supply) {
  984. wcd934x_mbhc->on_demand_list[
  985. WCD934X_ON_DEMAND_MICBIAS].supply =
  986. supply;
  987. wcd934x_mbhc->on_demand_list[
  988. WCD934X_ON_DEMAND_MICBIAS].ondemand_supply_count =
  989. 0;
  990. }
  991. (*mbhc) = wcd934x_mbhc;
  992. snd_soc_add_codec_controls(codec, impedance_detect_controls,
  993. ARRAY_SIZE(impedance_detect_controls));
  994. snd_soc_add_codec_controls(codec, hph_type_detect_controls,
  995. ARRAY_SIZE(hph_type_detect_controls));
  996. if (wcd_mbhc->mbhc_detection_logic == WCD_DETECTION_LEGACY) {
  997. snd_soc_update_bits(codec, WCD934X_MBHC_NEW_CTL_1, 0x04, 0x04);
  998. snd_soc_update_bits(codec, WCD934X_MBHC_CTL_BCS, 0x01, 0x01);
  999. }
  1000. return 0;
  1001. err:
  1002. devm_kfree(codec->dev, wcd934x_mbhc);
  1003. return ret;
  1004. }
  1005. EXPORT_SYMBOL(tavil_mbhc_init);
  1006. /*
  1007. * tavil_mbhc_deinit: deinitialize mbhc for tavil
  1008. * @codec: handle to snd_soc_codec *
  1009. */
  1010. void tavil_mbhc_deinit(struct snd_soc_codec *codec)
  1011. {
  1012. struct wcd934x_mbhc *wcd934x_mbhc = tavil_soc_get_mbhc(codec);
  1013. if (wcd934x_mbhc) {
  1014. wcd_mbhc_deinit(&wcd934x_mbhc->wcd_mbhc);
  1015. devm_kfree(codec->dev, wcd934x_mbhc);
  1016. }
  1017. }
  1018. EXPORT_SYMBOL(tavil_mbhc_deinit);