wcd937x.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. /*
  2. * Copyright (c) 2018, 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/slab.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/device.h>
  17. #include <linux/delay.h>
  18. #include <linux/kernel.h>
  19. #include <linux/component.h>
  20. #include <sound/soc.h>
  21. #include <sound/tlv.h>
  22. #include <soc/soundwire.h>
  23. #include <linux/regmap.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include "internal.h"
  27. #include "../wcdcal-hwdep.h"
  28. #include "wcd937x-registers.h"
  29. #include "../msm-cdc-pinctrl.h"
  30. #include <dt-bindings/sound/audio-codec-port-types.h>
  31. #include "../msm-cdc-supply.h"
  32. #define WCD9370_VARIANT 0
  33. #define WCD9375_VARIANT 5
  34. #define NUM_SWRS_DT_PARAMS 5
  35. #define WCD937X_VERSION_1_0 1
  36. #define WCD937X_VERSION_ENTRY_SIZE 32
  37. enum {
  38. CODEC_TX = 0,
  39. CODEC_RX,
  40. };
  41. static const DECLARE_TLV_DB_SCALE(line_gain, 0, 7, 1);
  42. static const DECLARE_TLV_DB_SCALE(analog_gain, 0, 25, 1);
  43. static int wcd937x_handle_post_irq(void *data);
  44. static const struct regmap_irq wcd937x_irqs[WCD937X_NUM_IRQS] = {
  45. REGMAP_IRQ_REG(WCD937X_IRQ_MBHC_BUTTON_PRESS_DET, 0, 0x01),
  46. REGMAP_IRQ_REG(WCD937X_IRQ_MBHC_BUTTON_RELEASE_DET, 0, 0x02),
  47. REGMAP_IRQ_REG(WCD937X_IRQ_MBHC_ELECT_INS_REM_DET, 0, 0x04),
  48. REGMAP_IRQ_REG(WCD937X_IRQ_MBHC_ELECT_INS_REM_LEG_DET, 0, 0x08),
  49. REGMAP_IRQ_REG(WCD937X_IRQ_MBHC_SW_DET, 0, 0x10),
  50. REGMAP_IRQ_REG(WCD937X_IRQ_HPHR_OCP_INT, 0, 0x20),
  51. REGMAP_IRQ_REG(WCD937X_IRQ_HPHR_CNP_INT, 0, 0x40),
  52. REGMAP_IRQ_REG(WCD937X_IRQ_HPHL_OCP_INT, 0, 0x80),
  53. REGMAP_IRQ_REG(WCD937X_IRQ_HPHL_CNP_INT, 1, 0x01),
  54. REGMAP_IRQ_REG(WCD937X_IRQ_EAR_CNP_INT, 1, 0x02),
  55. REGMAP_IRQ_REG(WCD937X_IRQ_EAR_SCD_INT, 1, 0x04),
  56. REGMAP_IRQ_REG(WCD937X_IRQ_AUX_CNP_INT, 1, 0x08),
  57. REGMAP_IRQ_REG(WCD937X_IRQ_AUX_SCD_INT, 1, 0x10),
  58. REGMAP_IRQ_REG(WCD937X_IRQ_HPHL_PDM_WD_INT, 1, 0x20),
  59. REGMAP_IRQ_REG(WCD937X_IRQ_HPHR_PDM_WD_INT, 1, 0x40),
  60. REGMAP_IRQ_REG(WCD937X_IRQ_AUX_PDM_WD_INT, 1, 0x80),
  61. REGMAP_IRQ_REG(WCD937X_IRQ_LDORT_SCD_INT, 2, 0x01),
  62. REGMAP_IRQ_REG(WCD937X_IRQ_MBHC_MOISTURE_INT, 2, 0x02),
  63. REGMAP_IRQ_REG(WCD937X_IRQ_HPHL_SURGE_DET_INT, 2, 0x04),
  64. REGMAP_IRQ_REG(WCD937X_IRQ_HPHR_SURGE_DET_INT, 2, 0x08),
  65. };
  66. static struct regmap_irq_chip wcd937x_regmap_irq_chip = {
  67. .name = "wcd937x",
  68. .irqs = wcd937x_irqs,
  69. .num_irqs = ARRAY_SIZE(wcd937x_irqs),
  70. .num_regs = 3,
  71. .status_base = WCD937X_DIGITAL_INTR_STATUS_0,
  72. .mask_base = WCD937X_DIGITAL_INTR_MASK_0,
  73. .type_base = WCD937X_DIGITAL_INTR_LEVEL_0,
  74. .runtime_pm = false,
  75. .handle_post_irq = wcd937x_handle_post_irq,
  76. .irq_drv_data = NULL,
  77. };
  78. static int wcd937x_handle_post_irq(void *data)
  79. {
  80. struct wcd937x_priv *wcd937x = data;
  81. int val = 0;
  82. struct wcd937x_pdata *pdata = NULL;
  83. pdata = dev_get_platdata(wcd937x->dev);
  84. regmap_read(wcd937x->regmap, WCD937X_DIGITAL_INTR_STATUS_0, &val);
  85. dev_dbg(wcd937x->dev, "%s Clear OCP interupts\n", __func__);
  86. regmap_write(wcd937x->regmap,
  87. WCD937X_DIGITAL_INTR_CLEAR_0, 0xFF);
  88. regmap_write(wcd937x->regmap,
  89. WCD937X_DIGITAL_INTR_CLEAR_0, 0x0);
  90. regmap_read(wcd937x->regmap, WCD937X_DIGITAL_INTR_STATUS_1, &val);
  91. dev_dbg(wcd937x->dev, "%s Clear SCD interupts\n", __func__);
  92. regmap_write(wcd937x->regmap,
  93. WCD937X_DIGITAL_INTR_CLEAR_1, 0xFF);
  94. regmap_write(wcd937x->regmap,
  95. WCD937X_DIGITAL_INTR_CLEAR_1, 0x0);
  96. regmap_write(wcd937x->regmap,
  97. WCD937X_DIGITAL_INTR_CLEAR_2, 0xFF);
  98. regmap_write(wcd937x->regmap,
  99. WCD937X_DIGITAL_INTR_CLEAR_2, 0x0);
  100. regmap_read(wcd937x->regmap, WCD937X_DIGITAL_INTR_STATUS_0, &val);
  101. regmap_read(wcd937x->regmap, WCD937X_DIGITAL_INTR_STATUS_1, &val);
  102. regmap_read(wcd937x->regmap, WCD937X_DIGITAL_INTR_STATUS_2, &val);
  103. regmap_read(wcd937x->regmap, WCD937X_DIGITAL_INTR_CLEAR_0, &val);
  104. regmap_read(wcd937x->regmap, WCD937X_DIGITAL_INTR_CLEAR_1, &val);
  105. regmap_read(wcd937x->regmap, WCD937X_DIGITAL_INTR_CLEAR_2, &val);
  106. return IRQ_HANDLED;
  107. }
  108. static int wcd937x_init_reg(struct snd_soc_codec *codec)
  109. {
  110. snd_soc_update_bits(codec, WCD937X_SLEEP_CTL, 0x0E, 0x0E);
  111. snd_soc_update_bits(codec, WCD937X_SLEEP_CTL, 0x80, 0x80);
  112. usleep_range(1000, 1010);
  113. snd_soc_update_bits(codec, WCD937X_SLEEP_CTL, 0x40, 0x40);
  114. usleep_range(1000, 1010);
  115. snd_soc_update_bits(codec, WCD937X_LDORXTX_CONFIG, 0x10, 0x00);
  116. snd_soc_update_bits(codec, WCD937X_BIAS_VBG_FINE_ADJ, 0xF0, 0x80);
  117. snd_soc_update_bits(codec, WCD937X_ANA_BIAS, 0x80, 0x80);
  118. snd_soc_update_bits(codec, WCD937X_ANA_BIAS, 0x40, 0x40);
  119. usleep_range(10000, 10010);
  120. snd_soc_update_bits(codec, WCD937X_ANA_BIAS, 0x40, 0x00);
  121. return 0;
  122. }
  123. static int wcd937x_set_port_params(struct snd_soc_codec *codec, u8 slv_prt_type,
  124. u8 *port_id, u8 *num_ch, u8 *ch_mask, u32 *ch_rate,
  125. u8 *port_type, u8 path)
  126. {
  127. int i, j;
  128. u8 num_ports;
  129. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT];
  130. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  131. switch (path) {
  132. case CODEC_RX:
  133. map = &wcd937x->rx_port_mapping;
  134. num_ports = wcd937x->num_rx_ports;
  135. break;
  136. case CODEC_TX:
  137. map = &wcd937x->tx_port_mapping;
  138. num_ports = wcd937x->num_tx_ports;
  139. break;
  140. }
  141. for (i = 0; i <= num_ports; i++) {
  142. for (j = 0; j < MAX_CH_PER_PORT; j++) {
  143. if ((*map)[i][j].slave_port_type == slv_prt_type)
  144. goto found;
  145. }
  146. }
  147. found:
  148. if (i > num_ports || j == MAX_CH_PER_PORT) {
  149. dev_err(codec->dev, "%s Failed to find slave port for type %u\n",
  150. __func__, slv_prt_type);
  151. return -EINVAL;
  152. }
  153. *port_id = i;
  154. *num_ch = (*map)[i][j].num_ch;
  155. *ch_mask = (*map)[i][j].ch_mask;
  156. *ch_rate = (*map)[i][j].ch_rate;
  157. *port_type = (*map)[i][j].master_port_type;
  158. return 0;
  159. }
  160. static int wcd937x_parse_port_mapping(struct device *dev,
  161. char *prop, u8 path)
  162. {
  163. u32 *dt_array, map_size, map_length;
  164. u32 port_num, ch_mask, ch_rate, old_port_num = 0;
  165. u32 slave_port_type, master_port_type;
  166. u32 i, ch_iter = 0;
  167. int ret = 0;
  168. u8 *num_ports;
  169. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT];
  170. struct wcd937x_priv *wcd937x = dev_get_drvdata(dev);
  171. switch (path) {
  172. case CODEC_RX:
  173. map = &wcd937x->rx_port_mapping;
  174. num_ports = &wcd937x->num_rx_ports;
  175. break;
  176. case CODEC_TX:
  177. map = &wcd937x->tx_port_mapping;
  178. num_ports = &wcd937x->num_tx_ports;
  179. break;
  180. }
  181. if (!of_find_property(dev->of_node, prop,
  182. &map_size)) {
  183. dev_err(dev, "missing port mapping prop %s\n", prop);
  184. goto err_pdata_fail;
  185. }
  186. map_length = map_size / (NUM_SWRS_DT_PARAMS * sizeof(u32));
  187. dt_array = kzalloc(map_size, GFP_KERNEL);
  188. if (!dt_array) {
  189. ret = -ENOMEM;
  190. goto err_pdata_fail;
  191. }
  192. ret = of_property_read_u32_array(dev->of_node, prop, dt_array,
  193. NUM_SWRS_DT_PARAMS * map_length);
  194. if (ret) {
  195. dev_err(dev, "%s: Failed to read port mapping from prop %s\n",
  196. __func__, prop);
  197. goto err_pdata_fail;
  198. }
  199. for (i = 0; i < map_length; i++) {
  200. port_num = dt_array[NUM_SWRS_DT_PARAMS * i];
  201. slave_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 1];
  202. ch_mask = dt_array[NUM_SWRS_DT_PARAMS * i + 2];
  203. ch_rate = dt_array[NUM_SWRS_DT_PARAMS * i + 3];
  204. master_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 4];
  205. if (port_num != old_port_num)
  206. ch_iter = 0;
  207. (*map)[port_num][ch_iter].slave_port_type = slave_port_type;
  208. (*map)[port_num][ch_iter].ch_mask = ch_mask;
  209. (*map)[port_num][ch_iter].master_port_type = master_port_type;
  210. (*map)[port_num][ch_iter].num_ch = __sw_hweight8(ch_mask);
  211. (*map)[port_num][ch_iter++].ch_rate = ch_rate;
  212. old_port_num = port_num;
  213. }
  214. *num_ports = port_num;
  215. kfree(dt_array);
  216. return 0;
  217. err_pdata_fail:
  218. kfree(dt_array);
  219. return -EINVAL;
  220. }
  221. static int wcd937x_tx_connect_port(struct snd_soc_codec *codec,
  222. u8 slv_port_type, u8 enable)
  223. {
  224. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  225. u8 port_id;
  226. u8 num_ch;
  227. u8 ch_mask;
  228. u32 ch_rate;
  229. u8 port_type;
  230. u8 num_port = 1;
  231. int ret = 0;
  232. ret = wcd937x_set_port_params(codec, slv_port_type, &port_id,
  233. &num_ch, &ch_mask, &ch_rate,
  234. &port_type, CODEC_TX);
  235. if (ret)
  236. return ret;
  237. if (enable)
  238. ret = swr_connect_port(wcd937x->tx_swr_dev, &port_id,
  239. num_port, &ch_mask, &ch_rate,
  240. &num_ch, &port_type);
  241. else
  242. ret = swr_disconnect_port(wcd937x->tx_swr_dev, &port_id,
  243. num_port, &ch_mask, &port_type);
  244. return ret;
  245. }
  246. static int wcd937x_rx_connect_port(struct snd_soc_codec *codec,
  247. u8 slv_port_type, u8 enable)
  248. {
  249. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  250. u8 port_id;
  251. u8 num_ch;
  252. u8 ch_mask;
  253. u32 ch_rate;
  254. u8 port_type;
  255. u8 num_port = 1;
  256. int ret = 0;
  257. ret = wcd937x_set_port_params(codec, slv_port_type, &port_id,
  258. &num_ch, &ch_mask, &ch_rate,
  259. &port_type, CODEC_RX);
  260. if (ret)
  261. return ret;
  262. if (enable)
  263. ret = swr_connect_port(wcd937x->rx_swr_dev, &port_id,
  264. num_port, &ch_mask, &ch_rate,
  265. &num_ch, &port_type);
  266. else
  267. ret = swr_disconnect_port(wcd937x->rx_swr_dev, &port_id,
  268. num_port, &ch_mask, &port_type);
  269. return ret;
  270. }
  271. static int wcd937x_rx_clk_enable(struct snd_soc_codec *codec)
  272. {
  273. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  274. if (wcd937x->rx_clk_cnt == 0) {
  275. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  276. 0x08, 0x08);
  277. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  278. 0x01, 0x01);
  279. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES, 0x01, 0x01);
  280. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_RX0_CTL,
  281. 0x40, 0x00);
  282. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_RX1_CTL,
  283. 0x40, 0x00);
  284. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_RX2_CTL,
  285. 0x40, 0x00);
  286. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  287. 0x02, 0x02);
  288. }
  289. wcd937x->rx_clk_cnt++;
  290. return 0;
  291. }
  292. static int wcd937x_rx_clk_disable(struct snd_soc_codec *codec)
  293. {
  294. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  295. wcd937x->rx_clk_cnt--;
  296. if (wcd937x->rx_clk_cnt == 0) {
  297. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES, 0x40, 0x00);
  298. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES, 0x80, 0x00);
  299. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES, 0x01, 0x00);
  300. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  301. 0x02, 0x00);
  302. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  303. 0x01, 0x00);
  304. }
  305. return 0;
  306. }
  307. /*
  308. * wcd937x_soc_get_mbhc: get wcd937x_mbhc handle of corresponding codec
  309. * @codec: handle to snd_soc_codec *
  310. *
  311. * return wcd937x_mbhc handle or error code in case of failure
  312. */
  313. struct wcd937x_mbhc *wcd937x_soc_get_mbhc(struct snd_soc_codec *codec)
  314. {
  315. struct wcd937x_priv *wcd937x;
  316. if (!codec) {
  317. pr_err("%s: Invalid params, NULL codec\n", __func__);
  318. return NULL;
  319. }
  320. wcd937x = snd_soc_codec_get_drvdata(codec);
  321. if (!wcd937x) {
  322. pr_err("%s: Invalid params, NULL tavil\n", __func__);
  323. return NULL;
  324. }
  325. return wcd937x->mbhc;
  326. }
  327. EXPORT_SYMBOL(wcd937x_soc_get_mbhc);
  328. static int wcd937x_codec_hphl_dac_event(struct snd_soc_dapm_widget *w,
  329. struct snd_kcontrol *kcontrol,
  330. int event)
  331. {
  332. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  333. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  334. int ret = 0;
  335. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  336. w->name, event);
  337. switch (event) {
  338. case SND_SOC_DAPM_PRE_PMU:
  339. wcd937x_rx_clk_enable(codec);
  340. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  341. 0x01, 0x01);
  342. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_HPH_GAIN_CTL,
  343. 0x04, 0x04);
  344. snd_soc_update_bits(codec, WCD937X_HPH_RDAC_CLK_CTL1,
  345. 0x80, 0x00);
  346. break;
  347. case SND_SOC_DAPM_POST_PMU:
  348. snd_soc_update_bits(codec, WCD937X_HPH_NEW_INT_RDAC_HD2_CTL_L,
  349. 0x0F, 0x02);
  350. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_COMP_CTL_0,
  351. 0x02, 0x02);
  352. usleep_range(5000, 5010);
  353. snd_soc_update_bits(codec, WCD937X_HPH_NEW_INT_HPH_TIMER1,
  354. 0x02, 0x00);
  355. break;
  356. case SND_SOC_DAPM_POST_PMD:
  357. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  358. wcd937x->rx_swr_dev->dev_num,
  359. false);
  360. break;
  361. }
  362. return ret;
  363. }
  364. static int wcd937x_codec_hphr_dac_event(struct snd_soc_dapm_widget *w,
  365. struct snd_kcontrol *kcontrol,
  366. int event)
  367. {
  368. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  369. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  370. int ret = 0;
  371. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  372. w->name, event);
  373. switch (event) {
  374. case SND_SOC_DAPM_PRE_PMU:
  375. wcd937x_rx_clk_enable(codec);
  376. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  377. 0x02, 0x02);
  378. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_HPH_GAIN_CTL,
  379. 0x08, 0x08);
  380. snd_soc_update_bits(codec, WCD937X_HPH_RDAC_CLK_CTL1,
  381. 0x80, 0x00);
  382. break;
  383. case SND_SOC_DAPM_POST_PMU:
  384. snd_soc_update_bits(codec, WCD937X_HPH_NEW_INT_RDAC_HD2_CTL_R,
  385. 0x0F, 0x02);
  386. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_COMP_CTL_0,
  387. 0x01, 0x01);
  388. usleep_range(5000, 5010);
  389. snd_soc_update_bits(codec, WCD937X_HPH_NEW_INT_HPH_TIMER1,
  390. 0x02, 0x00);
  391. break;
  392. case SND_SOC_DAPM_POST_PMD:
  393. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  394. wcd937x->rx_swr_dev->dev_num,
  395. false);
  396. break;
  397. }
  398. return ret;
  399. }
  400. static int wcd937x_codec_ear_dac_event(struct snd_soc_dapm_widget *w,
  401. struct snd_kcontrol *kcontrol,
  402. int event)
  403. {
  404. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  405. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  406. int ret = 0;
  407. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  408. w->name, event);
  409. switch (event) {
  410. case SND_SOC_DAPM_PRE_PMU:
  411. wcd937x_rx_clk_enable(codec);
  412. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_HPH_GAIN_CTL,
  413. 0x04, 0x04);
  414. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  415. 0x01, 0x01);
  416. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_COMP_CTL_0,
  417. 0x02, 0x02);
  418. usleep_range(5000, 5010);
  419. break;
  420. case SND_SOC_DAPM_POST_PMD:
  421. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  422. wcd937x->rx_swr_dev->dev_num,
  423. false);
  424. break;
  425. };
  426. return ret;
  427. }
  428. static int wcd937x_codec_aux_dac_event(struct snd_soc_dapm_widget *w,
  429. struct snd_kcontrol *kcontrol,
  430. int event)
  431. {
  432. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  433. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  434. int ret = 0;
  435. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  436. w->name, event);
  437. switch (event) {
  438. case SND_SOC_DAPM_PRE_PMU:
  439. wcd937x_rx_clk_enable(codec);
  440. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  441. 0x04, 0x04);
  442. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  443. 0x04, 0x04);
  444. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_AUX_GAIN_CTL,
  445. 0x01, 0x01);
  446. break;
  447. case SND_SOC_DAPM_POST_PMD:
  448. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  449. wcd937x->rx_swr_dev->dev_num,
  450. false);
  451. wcd937x_rx_clk_disable(codec);
  452. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  453. 0x04, 0x00);
  454. break;
  455. };
  456. return ret;
  457. }
  458. static int wcd937x_codec_enable_hphr_pa(struct snd_soc_dapm_widget *w,
  459. struct snd_kcontrol *kcontrol,
  460. int event)
  461. {
  462. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  463. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  464. int ret = 0;
  465. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  466. w->name, event);
  467. switch (event) {
  468. case SND_SOC_DAPM_PRE_PMU:
  469. snd_soc_update_bits(codec, WCD937X_ANA_HPH, 0x10, 0x10);
  470. usleep_range(100, 110);
  471. break;
  472. case SND_SOC_DAPM_POST_PMU:
  473. usleep_range(7000, 7010);
  474. snd_soc_update_bits(codec, WCD937X_HPH_NEW_INT_HPH_TIMER1,
  475. 0x02, 0x02);
  476. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  477. 0x02, 0x02);
  478. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  479. wcd937x->rx_swr_dev->dev_num,
  480. true);
  481. break;
  482. case SND_SOC_DAPM_POST_PMD:
  483. usleep_range(7000, 7010);
  484. snd_soc_update_bits(codec, WCD937X_ANA_HPH, 0x10, 0x00);
  485. break;
  486. };
  487. return ret;
  488. }
  489. static int wcd937x_codec_enable_hphl_pa(struct snd_soc_dapm_widget *w,
  490. struct snd_kcontrol *kcontrol,
  491. int event)
  492. {
  493. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  494. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  495. int ret = 0;
  496. switch (event) {
  497. case SND_SOC_DAPM_PRE_PMU:
  498. snd_soc_update_bits(codec, WCD937X_ANA_HPH, 0x0C, 0x08);
  499. snd_soc_update_bits(codec, WCD937X_ANA_HPH, 0x20, 0x20);
  500. usleep_range(100, 110);
  501. break;
  502. case SND_SOC_DAPM_POST_PMU:
  503. usleep_range(7000, 7010);
  504. snd_soc_update_bits(codec, WCD937X_HPH_NEW_INT_HPH_TIMER1,
  505. 0x02, 0x02);
  506. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  507. 0x02, 0x02);
  508. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  509. wcd937x->rx_swr_dev->dev_num,
  510. true);
  511. break;
  512. case SND_SOC_DAPM_POST_PMD:
  513. usleep_range(7000, 7010);
  514. snd_soc_update_bits(codec, WCD937X_ANA_HPH, 0x20, 0x00);
  515. break;
  516. };
  517. return ret;
  518. }
  519. static int wcd937x_codec_enable_aux_pa(struct snd_soc_dapm_widget *w,
  520. struct snd_kcontrol *kcontrol,
  521. int event)
  522. {
  523. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  524. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  525. int ret = 0;
  526. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  527. w->name, event);
  528. switch (event) {
  529. case SND_SOC_DAPM_PRE_PMU:
  530. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  531. 0x80, 0x80);
  532. usleep_range(500, 510);
  533. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x3A);
  534. usleep_range(500, 510);
  535. break;
  536. case SND_SOC_DAPM_POST_PMU:
  537. usleep_range(1000, 1010);
  538. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  539. 0x20, 0x20);
  540. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  541. wcd937x->rx_swr_dev->dev_num,
  542. true);
  543. break;
  544. case SND_SOC_DAPM_POST_PMD:
  545. usleep_range(1000, 1010);
  546. usleep_range(1000, 1010);
  547. break;
  548. };
  549. return ret;
  550. }
  551. static int wcd937x_codec_enable_ear_pa(struct snd_soc_dapm_widget *w,
  552. struct snd_kcontrol *kcontrol,
  553. int event)
  554. {
  555. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  556. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  557. int ret = 0;
  558. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  559. w->name, event);
  560. switch (event) {
  561. case SND_SOC_DAPM_PRE_PMU:
  562. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  563. 0x08, 0x08);
  564. usleep_range(500, 510);
  565. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x3A);
  566. usleep_range(500, 510);
  567. break;
  568. case SND_SOC_DAPM_POST_PMU:
  569. usleep_range(6000, 6010);
  570. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  571. 0x02, 0x02);
  572. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  573. wcd937x->rx_swr_dev->dev_num,
  574. true);
  575. break;
  576. case SND_SOC_DAPM_POST_PMD:
  577. usleep_range(7000, 7010);
  578. break;
  579. };
  580. return ret;
  581. }
  582. static int wcd937x_enable_rx1(struct snd_soc_dapm_widget *w,
  583. struct snd_kcontrol *kcontrol,
  584. int event)
  585. {
  586. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  587. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  588. w->name, event);
  589. switch (event) {
  590. case SND_SOC_DAPM_PRE_PMU:
  591. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEG_CTRL_4,
  592. 0xF0, 0x80);
  593. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEGDAC_CTRL_2,
  594. 0xE0, 0xA0);
  595. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_3,
  596. 0x02, 0x02);
  597. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2,
  598. 0xFF, 0x1C);
  599. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  600. 0x40, 0x40);
  601. usleep_range(100, 110);
  602. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEGDAC_CTRL_2,
  603. 0xE0, 0xE0);
  604. usleep_range(100, 110);
  605. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  606. 0x80, 0x80);
  607. usleep_range(500, 510);
  608. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x3A);
  609. usleep_range(500, 510);
  610. wcd937x_rx_connect_port(codec, HPH_L, true);
  611. wcd937x_rx_connect_port(codec, COMP_L, true);
  612. break;
  613. case SND_SOC_DAPM_POST_PMD:
  614. wcd937x_rx_connect_port(codec, HPH_L, false);
  615. wcd937x_rx_connect_port(codec, COMP_L, false);
  616. wcd937x_rx_clk_disable(codec);
  617. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  618. 0x01, 0x00);
  619. break;
  620. };
  621. return 0;
  622. }
  623. static int wcd937x_enable_rx2(struct snd_soc_dapm_widget *w,
  624. struct snd_kcontrol *kcontrol, int event)
  625. {
  626. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  627. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  628. w->name, event);
  629. switch (event) {
  630. case SND_SOC_DAPM_PRE_PMU:
  631. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEG_CTRL_4,
  632. 0xF0, 0x80);
  633. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEGDAC_CTRL_2,
  634. 0xE0, 0xA0);
  635. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_3, 0x02, 0x02);
  636. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x1C);
  637. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  638. 0x40, 0x40);
  639. usleep_range(100, 110);
  640. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEGDAC_CTRL_2,
  641. 0xE0, 0xE0);
  642. usleep_range(100, 110);
  643. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  644. 0x80, 0x80);
  645. usleep_range(500, 510);
  646. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x3A);
  647. usleep_range(500, 510);
  648. wcd937x_rx_connect_port(codec, HPH_R, true);
  649. wcd937x_rx_connect_port(codec, COMP_R, true);
  650. break;
  651. case SND_SOC_DAPM_POST_PMD:
  652. wcd937x_rx_connect_port(codec, HPH_R, false);
  653. wcd937x_rx_connect_port(codec, COMP_R, false);
  654. wcd937x_rx_clk_disable(codec);
  655. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  656. 0x02, 0x00);
  657. break;
  658. };
  659. return 0;
  660. }
  661. static int wcd937x_enable_rx3(struct snd_soc_dapm_widget *w,
  662. struct snd_kcontrol *kcontrol,
  663. int event)
  664. {
  665. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  666. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  667. w->name, event);
  668. switch (event) {
  669. case SND_SOC_DAPM_PRE_PMU:
  670. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEG_CTRL_2,
  671. 0xE0, 0xA0);
  672. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_3, 0x02, 0x02);
  673. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x1C);
  674. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  675. 0x40, 0x40);
  676. usleep_range(100, 110);
  677. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEG_CTRL_2,
  678. 0xE0, 0xE0);
  679. usleep_range(100, 110);
  680. wcd937x_rx_connect_port(codec, LO, true);
  681. break;
  682. case SND_SOC_DAPM_POST_PMD:
  683. wcd937x_rx_connect_port(codec, LO, false);
  684. usleep_range(6000, 6010);
  685. wcd937x_rx_clk_disable(codec);
  686. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  687. 0x04, 0x00);
  688. break;
  689. }
  690. return 0;
  691. }
  692. static int wcd937x_codec_enable_dmic(struct snd_soc_dapm_widget *w,
  693. struct snd_kcontrol *kcontrol,
  694. int event)
  695. {
  696. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  697. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  698. u16 dmic_clk_reg;
  699. s32 *dmic_clk_cnt;
  700. unsigned int dmic;
  701. char *wname;
  702. int ret = 0;
  703. wname = strpbrk(w->name, "012345");
  704. if (!wname) {
  705. dev_err(codec->dev, "%s: widget not found\n", __func__);
  706. return -EINVAL;
  707. }
  708. ret = kstrtouint(wname, 10, &dmic);
  709. if (ret < 0) {
  710. dev_err(codec->dev, "%s: Invalid DMIC line on the codec\n",
  711. __func__);
  712. return -EINVAL;
  713. }
  714. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  715. w->name, event);
  716. switch (dmic) {
  717. case 0:
  718. case 1:
  719. dmic_clk_cnt = &(wcd937x->dmic_0_1_clk_cnt);
  720. dmic_clk_reg = WCD937X_DIGITAL_CDC_DMIC0_CTL;
  721. break;
  722. case 2:
  723. case 3:
  724. dmic_clk_cnt = &(wcd937x->dmic_2_3_clk_cnt);
  725. dmic_clk_reg = WCD937X_DIGITAL_CDC_DMIC1_CTL;
  726. break;
  727. case 4:
  728. case 5:
  729. dmic_clk_cnt = &(wcd937x->dmic_4_5_clk_cnt);
  730. dmic_clk_reg = WCD937X_DIGITAL_CDC_DMIC2_CTL;
  731. break;
  732. default:
  733. dev_err(codec->dev, "%s: Invalid DMIC Selection\n",
  734. __func__);
  735. return -EINVAL;
  736. };
  737. dev_dbg(codec->dev, "%s: event %d DMIC%d dmic_clk_cnt %d\n",
  738. __func__, event, dmic, *dmic_clk_cnt);
  739. switch (event) {
  740. case SND_SOC_DAPM_PRE_PMU:
  741. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  742. 0x80, 0x80);
  743. snd_soc_update_bits(codec, dmic_clk_reg, 0x07, 0x02);
  744. snd_soc_update_bits(codec, dmic_clk_reg, 0x08, 0x08);
  745. snd_soc_update_bits(codec, dmic_clk_reg, 0x70, 0x20);
  746. wcd937x_tx_connect_port(codec, DMIC0 + (w->shift), true);
  747. break;
  748. case SND_SOC_DAPM_POST_PMD:
  749. wcd937x_tx_connect_port(codec, DMIC0 + (w->shift), false);
  750. break;
  751. };
  752. return 0;
  753. }
  754. /*
  755. * wcd937x_get_micb_vout_ctl_val: converts micbias from volts to register value
  756. * @micb_mv: micbias in mv
  757. *
  758. * return register value converted
  759. */
  760. int wcd937x_get_micb_vout_ctl_val(u32 micb_mv)
  761. {
  762. /* min micbias voltage is 1V and maximum is 2.85V */
  763. if (micb_mv < 1000 || micb_mv > 2850) {
  764. pr_err("%s: unsupported micbias voltage\n", __func__);
  765. return -EINVAL;
  766. }
  767. return (micb_mv - 1000) / 50;
  768. }
  769. EXPORT_SYMBOL(wcd937x_get_micb_vout_ctl_val);
  770. /*
  771. * wcd937x_mbhc_micb_adjust_voltage: adjust specific micbias voltage
  772. * @codec: handle to snd_soc_codec *
  773. * @req_volt: micbias voltage to be set
  774. * @micb_num: micbias to be set, e.g. micbias1 or micbias2
  775. *
  776. * return 0 if adjustment is success or error code in case of failure
  777. */
  778. int wcd937x_mbhc_micb_adjust_voltage(struct snd_soc_codec *codec,
  779. int req_volt, int micb_num)
  780. {
  781. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  782. int cur_vout_ctl, req_vout_ctl;
  783. int micb_reg, micb_val, micb_en;
  784. int ret = 0;
  785. switch (micb_num) {
  786. case MIC_BIAS_1:
  787. micb_reg = WCD937X_ANA_MICB1;
  788. break;
  789. case MIC_BIAS_2:
  790. micb_reg = WCD937X_ANA_MICB2;
  791. break;
  792. case MIC_BIAS_3:
  793. micb_reg = WCD937X_ANA_MICB3;
  794. break;
  795. default:
  796. return -EINVAL;
  797. }
  798. mutex_lock(&wcd937x->micb_lock);
  799. /*
  800. * If requested micbias voltage is same as current micbias
  801. * voltage, then just return. Otherwise, adjust voltage as
  802. * per requested value. If micbias is already enabled, then
  803. * to avoid slow micbias ramp-up or down enable pull-up
  804. * momentarily, change the micbias value and then re-enable
  805. * micbias.
  806. */
  807. micb_val = snd_soc_read(codec, micb_reg);
  808. micb_en = (micb_val & 0xC0) >> 6;
  809. cur_vout_ctl = micb_val & 0x3F;
  810. req_vout_ctl = wcd937x_get_micb_vout_ctl_val(req_volt);
  811. if (req_vout_ctl < 0) {
  812. ret = -EINVAL;
  813. goto exit;
  814. }
  815. if (cur_vout_ctl == req_vout_ctl) {
  816. ret = 0;
  817. goto exit;
  818. }
  819. dev_dbg(codec->dev, "%s: micb_num: %d, cur_mv: %d, req_mv: %d, micb_en: %d\n",
  820. __func__, micb_num, WCD_VOUT_CTL_TO_MICB(cur_vout_ctl),
  821. req_volt, micb_en);
  822. if (micb_en == 0x1)
  823. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x80);
  824. snd_soc_update_bits(codec, micb_reg, 0x3F, req_vout_ctl);
  825. if (micb_en == 0x1) {
  826. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x40);
  827. /*
  828. * Add 2ms delay as per HW requirement after enabling
  829. * micbias
  830. */
  831. usleep_range(2000, 2100);
  832. }
  833. exit:
  834. mutex_unlock(&wcd937x->micb_lock);
  835. return ret;
  836. }
  837. EXPORT_SYMBOL(wcd937x_mbhc_micb_adjust_voltage);
  838. static int wcd937x_tx_swr_ctrl(struct snd_soc_dapm_widget *w,
  839. struct snd_kcontrol *kcontrol,
  840. int event)
  841. {
  842. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  843. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  844. int ret = 0;
  845. switch (event) {
  846. case SND_SOC_DAPM_PRE_PMU:
  847. ret = swr_slvdev_datapath_control(wcd937x->tx_swr_dev,
  848. wcd937x->tx_swr_dev->dev_num,
  849. true);
  850. break;
  851. case SND_SOC_DAPM_POST_PMD:
  852. ret = swr_slvdev_datapath_control(wcd937x->tx_swr_dev,
  853. wcd937x->tx_swr_dev->dev_num,
  854. false);
  855. break;
  856. };
  857. return ret;
  858. }
  859. static int wcd937x_codec_enable_adc(struct snd_soc_dapm_widget *w,
  860. struct snd_kcontrol *kcontrol,
  861. int event){
  862. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  863. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  864. w->name, event);
  865. switch (event) {
  866. case SND_SOC_DAPM_PRE_PMU:
  867. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  868. 0x80, 0x80);
  869. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  870. 0x08, 0x08);
  871. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  872. 0x10, 0x10);
  873. wcd937x_tx_connect_port(codec, ADC1 + (w->shift), true);
  874. break;
  875. case SND_SOC_DAPM_POST_PMD:
  876. wcd937x_tx_connect_port(codec, ADC1 + (w->shift), false);
  877. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  878. 0x08, 0x00);
  879. break;
  880. };
  881. return 0;
  882. }
  883. static int wcd937x_enable_req(struct snd_soc_dapm_widget *w,
  884. struct snd_kcontrol *kcontrol, int event)
  885. {
  886. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  887. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  888. w->name, event);
  889. switch (event) {
  890. case SND_SOC_DAPM_PRE_PMU:
  891. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_REQ_CTL,
  892. 0x02, 0x02);
  893. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_REQ_CTL, 0x01,
  894. 0x00);
  895. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH2, 0x40, 0x40);
  896. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  897. 0x30, 0x30);
  898. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH1, 0x80, 0x80);
  899. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH2, 0x40, 0x00);
  900. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH2, 0x80, 0x80);
  901. break;
  902. case SND_SOC_DAPM_POST_PMD:
  903. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH1, 0x80, 0x00);
  904. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  905. 0x10, 0x00);
  906. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  907. 0x10, 0x00);
  908. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  909. 0x80, 0x00);
  910. break;
  911. };
  912. return 0;
  913. }
  914. int wcd937x_micbias_control(struct snd_soc_codec *codec,
  915. int micb_num, int req, bool is_dapm)
  916. {
  917. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  918. int micb_index = micb_num - 1;
  919. u16 micb_reg;
  920. int pre_off_event = 0, post_off_event = 0;
  921. int post_on_event = 0, post_dapm_off = 0;
  922. int post_dapm_on = 0;
  923. if ((micb_index < 0) || (micb_index > WCD937X_MAX_MICBIAS - 1)) {
  924. dev_err(codec->dev, "%s: Invalid micbias index, micb_ind:%d\n",
  925. __func__, micb_index);
  926. return -EINVAL;
  927. }
  928. switch (micb_num) {
  929. case MIC_BIAS_1:
  930. micb_reg = WCD937X_ANA_MICB1;
  931. break;
  932. case MIC_BIAS_2:
  933. micb_reg = WCD937X_ANA_MICB2;
  934. pre_off_event = WCD_EVENT_PRE_MICBIAS_2_OFF;
  935. post_off_event = WCD_EVENT_POST_MICBIAS_2_OFF;
  936. post_on_event = WCD_EVENT_POST_MICBIAS_2_ON;
  937. post_dapm_on = WCD_EVENT_POST_DAPM_MICBIAS_2_ON;
  938. post_dapm_off = WCD_EVENT_POST_DAPM_MICBIAS_2_OFF;
  939. break;
  940. case MIC_BIAS_3:
  941. micb_reg = WCD937X_ANA_MICB3;
  942. break;
  943. default:
  944. dev_err(codec->dev, "%s: Invalid micbias number: %d\n",
  945. __func__, micb_num);
  946. return -EINVAL;
  947. };
  948. mutex_lock(&wcd937x->micb_lock);
  949. switch (req) {
  950. case MICB_PULLUP_ENABLE:
  951. wcd937x->pullup_ref[micb_index]++;
  952. if ((wcd937x->pullup_ref[micb_index] == 1) &&
  953. (wcd937x->micb_ref[micb_index] == 0))
  954. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x80);
  955. break;
  956. case MICB_PULLUP_DISABLE:
  957. if (wcd937x->pullup_ref[micb_index] > 0)
  958. wcd937x->pullup_ref[micb_index]--;
  959. if ((wcd937x->pullup_ref[micb_index] == 0) &&
  960. (wcd937x->micb_ref[micb_index] == 0))
  961. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x00);
  962. break;
  963. case MICB_ENABLE:
  964. wcd937x->micb_ref[micb_index]++;
  965. if (wcd937x->micb_ref[micb_index] == 1) {
  966. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL, 0xE0, 0xE0);
  967. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x10);
  968. snd_soc_update_bits(codec, WCD937X_MICB1_TEST_CTL_2, 0x01, 0x01);
  969. snd_soc_update_bits(codec, WCD937X_MICB2_TEST_CTL_2, 0x01, 0x01);
  970. snd_soc_update_bits(codec, WCD937X_MICB3_TEST_CTL_2, 0x01, 0x01);
  971. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x40);
  972. if (post_on_event)
  973. blocking_notifier_call_chain(&wcd937x->notifier,
  974. post_on_event,
  975. &wcd937x->mbhc);
  976. }
  977. if (is_dapm && post_dapm_on)
  978. blocking_notifier_call_chain(&wcd937x->notifier,
  979. post_dapm_on,
  980. &wcd937x->mbhc);
  981. break;
  982. case MICB_DISABLE:
  983. if (wcd937x->micb_ref[micb_index] > 0)
  984. wcd937x->micb_ref[micb_index]--;
  985. if ((wcd937x->micb_ref[micb_index] == 0) &&
  986. (wcd937x->pullup_ref[micb_index] > 0))
  987. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x80);
  988. else if ((wcd937x->micb_ref[micb_index] == 0) &&
  989. (wcd937x->pullup_ref[micb_index] == 0)) {
  990. if (pre_off_event)
  991. blocking_notifier_call_chain(&wcd937x->notifier,
  992. pre_off_event,
  993. &wcd937x->mbhc);
  994. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x00);
  995. if (post_off_event)
  996. blocking_notifier_call_chain(&wcd937x->notifier,
  997. post_off_event,
  998. &wcd937x->mbhc);
  999. }
  1000. if (is_dapm && post_dapm_off)
  1001. blocking_notifier_call_chain(&wcd937x->notifier,
  1002. post_dapm_off,
  1003. &wcd937x->mbhc);
  1004. break;
  1005. };
  1006. dev_dbg(codec->dev, "%s: micb_num:%d, micb_ref: %d, pullup_ref: %d\n",
  1007. __func__, micb_num, wcd937x->micb_ref[micb_index],
  1008. wcd937x->pullup_ref[micb_index]);
  1009. mutex_unlock(&wcd937x->micb_lock);
  1010. return 0;
  1011. }
  1012. EXPORT_SYMBOL(wcd937x_micbias_control);
  1013. static int __wcd937x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1014. int event)
  1015. {
  1016. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1017. int micb_num;
  1018. dev_dbg(codec->dev, "%s: wname: %s, event: %d\n",
  1019. __func__, w->name, event);
  1020. if (strnstr(w->name, "MIC BIAS1", sizeof("MIC BIAS1")))
  1021. micb_num = MIC_BIAS_1;
  1022. else if (strnstr(w->name, "MIC BIAS2", sizeof("MIC BIAS2")))
  1023. micb_num = MIC_BIAS_2;
  1024. else if (strnstr(w->name, "MIC BIAS3", sizeof("MIC BIAS3")))
  1025. micb_num = MIC_BIAS_3;
  1026. else
  1027. return -EINVAL;
  1028. switch (event) {
  1029. case SND_SOC_DAPM_PRE_PMU:
  1030. wcd937x_micbias_control(codec, micb_num, MICB_ENABLE, true);
  1031. break;
  1032. case SND_SOC_DAPM_POST_PMU:
  1033. usleep_range(1000, 1100);
  1034. break;
  1035. case SND_SOC_DAPM_POST_PMD:
  1036. wcd937x_micbias_control(codec, micb_num, MICB_DISABLE, true);
  1037. break;
  1038. };
  1039. return 0;
  1040. }
  1041. static int wcd937x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1042. struct snd_kcontrol *kcontrol,
  1043. int event)
  1044. {
  1045. return __wcd937x_codec_enable_micbias(w, event);
  1046. }
  1047. static int wcd937x_rx_hph_mode_get(struct snd_kcontrol *kcontrol,
  1048. struct snd_ctl_elem_value *ucontrol)
  1049. {
  1050. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1051. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  1052. ucontrol->value.integer.value[0] = wcd937x->hph_mode;
  1053. return 0;
  1054. }
  1055. static int wcd937x_rx_hph_mode_put(struct snd_kcontrol *kcontrol,
  1056. struct snd_ctl_elem_value *ucontrol)
  1057. {
  1058. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1059. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  1060. u32 mode_val;
  1061. mode_val = ucontrol->value.enumerated.item[0];
  1062. dev_dbg(codec->dev, "%s: mode: %d\n", __func__, mode_val);
  1063. if (mode_val == 0) {
  1064. dev_warn(codec->dev, "%s:Invalid HPH Mode, default to class_AB\n",
  1065. __func__);
  1066. mode_val = 3; /* enum will be updated later */
  1067. }
  1068. wcd937x->hph_mode = mode_val;
  1069. return 0;
  1070. }
  1071. static const char * const rx_hph_mode_mux_text[] = {
  1072. "CLS_H_INVALID", "CLS_H_HIFI", "CLS_H_LP", "CLS_AB", "CLS_H_LOHIFI",
  1073. "CLS_H_ULP", "CLS_AB_HIFI",
  1074. };
  1075. static const struct soc_enum rx_hph_mode_mux_enum =
  1076. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(rx_hph_mode_mux_text),
  1077. rx_hph_mode_mux_text);
  1078. static const struct snd_kcontrol_new wcd937x_snd_controls[] = {
  1079. SOC_ENUM_EXT("RX HPH Mode", rx_hph_mode_mux_enum,
  1080. wcd937x_rx_hph_mode_get, wcd937x_rx_hph_mode_put),
  1081. SOC_SINGLE_TLV("HPHL Volume", WCD937X_HPH_L_EN, 0, 20, 1, line_gain),
  1082. SOC_SINGLE_TLV("HPHR Volume", WCD937X_HPH_R_EN, 0, 20, 1, line_gain),
  1083. SOC_SINGLE_TLV("ADC1 Volume", WCD937X_ANA_TX_CH1, 0, 20, 0, analog_gain),
  1084. SOC_SINGLE_TLV("ADC2 Volume", WCD937X_ANA_TX_CH2, 0, 20, 0, analog_gain),
  1085. SOC_SINGLE_TLV("ADC3 Volume", WCD937X_ANA_TX_CH3, 0, 20, 0, analog_gain),
  1086. };
  1087. static const struct snd_kcontrol_new adc1_switch[] = {
  1088. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1089. };
  1090. static const struct snd_kcontrol_new adc2_switch[] = {
  1091. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1092. };
  1093. static const struct snd_kcontrol_new adc3_switch[] = {
  1094. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1095. };
  1096. static const struct snd_kcontrol_new dmic1_switch[] = {
  1097. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1098. };
  1099. static const struct snd_kcontrol_new dmic2_switch[] = {
  1100. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1101. };
  1102. static const struct snd_kcontrol_new dmic3_switch[] = {
  1103. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1104. };
  1105. static const struct snd_kcontrol_new dmic4_switch[] = {
  1106. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1107. };
  1108. static const struct snd_kcontrol_new dmic5_switch[] = {
  1109. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1110. };
  1111. static const struct snd_kcontrol_new dmic6_switch[] = {
  1112. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1113. };
  1114. static const struct snd_kcontrol_new ear_rdac_switch[] = {
  1115. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1116. };
  1117. static const struct snd_kcontrol_new aux_rdac_switch[] = {
  1118. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1119. };
  1120. static const struct snd_kcontrol_new hphl_rdac_switch[] = {
  1121. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1122. };
  1123. static const struct snd_kcontrol_new hphr_rdac_switch[] = {
  1124. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1125. };
  1126. static const char * const adc2_mux_text[] = {
  1127. "INP2", "INP3"
  1128. };
  1129. static const char * const rdac3_mux_text[] = {
  1130. "RX1", "RX3"
  1131. };
  1132. static const struct soc_enum adc2_enum =
  1133. SOC_ENUM_SINGLE(WCD937X_TX_NEW_TX_CH2_SEL, 7,
  1134. ARRAY_SIZE(adc2_mux_text), adc2_mux_text);
  1135. static const struct soc_enum rdac3_enum =
  1136. SOC_ENUM_SINGLE(WCD937X_DIGITAL_CDC_EAR_PATH_CTL, 0,
  1137. ARRAY_SIZE(rdac3_mux_text), rdac3_mux_text);
  1138. static const struct snd_kcontrol_new tx_adc2_mux =
  1139. SOC_DAPM_ENUM("ADC2 MUX Mux", adc2_enum);
  1140. static const struct snd_kcontrol_new rx_rdac3_mux =
  1141. SOC_DAPM_ENUM("RDAC3_MUX Mux", rdac3_enum);
  1142. static const struct snd_soc_dapm_widget wcd937x_dapm_widgets[] = {
  1143. /*input widgets*/
  1144. SND_SOC_DAPM_INPUT("AMIC1"),
  1145. SND_SOC_DAPM_INPUT("AMIC2"),
  1146. SND_SOC_DAPM_INPUT("AMIC3"),
  1147. SND_SOC_DAPM_INPUT("IN1_HPHL"),
  1148. SND_SOC_DAPM_INPUT("IN2_HPHR"),
  1149. SND_SOC_DAPM_INPUT("IN3_AUX"),
  1150. /*tx widgets*/
  1151. SND_SOC_DAPM_ADC_E("ADC1", NULL, SND_SOC_NOPM, 0, 0,
  1152. wcd937x_codec_enable_adc,
  1153. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1154. SND_SOC_DAPM_ADC_E("ADC2", NULL, SND_SOC_NOPM, 1, 0,
  1155. wcd937x_codec_enable_adc,
  1156. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1157. SND_SOC_DAPM_MIXER_E("ADC1 REQ", SND_SOC_NOPM, 0, 0,
  1158. NULL, 0, wcd937x_enable_req,
  1159. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1160. SND_SOC_DAPM_MIXER_E("ADC2 REQ", SND_SOC_NOPM, 0, 0,
  1161. NULL, 0, wcd937x_enable_req,
  1162. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1163. SND_SOC_DAPM_MUX("ADC2 MUX", SND_SOC_NOPM, 0, 0,
  1164. &tx_adc2_mux),
  1165. /*tx mixers*/
  1166. SND_SOC_DAPM_MIXER_E("ADC1_MIXER", SND_SOC_NOPM, 0, 0,
  1167. adc1_switch, ARRAY_SIZE(adc1_switch),
  1168. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1169. SND_SOC_DAPM_POST_PMD),
  1170. SND_SOC_DAPM_MIXER_E("ADC2_MIXER", SND_SOC_NOPM, 0, 0,
  1171. adc2_switch, ARRAY_SIZE(adc2_switch),
  1172. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1173. SND_SOC_DAPM_POST_PMD),
  1174. /* micbias widgets*/
  1175. SND_SOC_DAPM_MICBIAS_E("MIC BIAS1", SND_SOC_NOPM, 0, 0,
  1176. wcd937x_codec_enable_micbias,
  1177. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1178. SND_SOC_DAPM_POST_PMD),
  1179. SND_SOC_DAPM_MICBIAS_E("MIC BIAS2", SND_SOC_NOPM, 0, 0,
  1180. wcd937x_codec_enable_micbias,
  1181. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1182. SND_SOC_DAPM_POST_PMD),
  1183. SND_SOC_DAPM_MICBIAS_E("MIC BIAS3", SND_SOC_NOPM, 0, 0,
  1184. wcd937x_codec_enable_micbias,
  1185. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1186. SND_SOC_DAPM_POST_PMD),
  1187. /*rx widgets*/
  1188. SND_SOC_DAPM_PGA_E("EAR PGA", WCD937X_ANA_EAR, 7, 0, NULL, 0,
  1189. wcd937x_codec_enable_ear_pa,
  1190. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1191. SND_SOC_DAPM_POST_PMD),
  1192. SND_SOC_DAPM_PGA_E("AUX PGA", WCD937X_AUX_AUXPA, 7, 0, NULL, 0,
  1193. wcd937x_codec_enable_aux_pa,
  1194. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1195. SND_SOC_DAPM_POST_PMD),
  1196. SND_SOC_DAPM_PGA_E("HPHL PGA", WCD937X_ANA_HPH, 7, 0, NULL, 0,
  1197. wcd937x_codec_enable_hphl_pa,
  1198. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1199. SND_SOC_DAPM_POST_PMD),
  1200. SND_SOC_DAPM_PGA_E("HPHR PGA", WCD937X_ANA_HPH, 6, 0, NULL, 0,
  1201. wcd937x_codec_enable_hphr_pa,
  1202. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1203. SND_SOC_DAPM_POST_PMD),
  1204. SND_SOC_DAPM_DAC_E("RDAC1", NULL, SND_SOC_NOPM, 0, 0,
  1205. wcd937x_codec_hphl_dac_event,
  1206. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1207. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1208. SND_SOC_DAPM_DAC_E("RDAC2", NULL, SND_SOC_NOPM, 0, 0,
  1209. wcd937x_codec_hphr_dac_event,
  1210. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1211. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1212. SND_SOC_DAPM_DAC_E("RDAC3", NULL, SND_SOC_NOPM, 0, 0,
  1213. wcd937x_codec_ear_dac_event,
  1214. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1215. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1216. SND_SOC_DAPM_DAC_E("RDAC4", NULL, SND_SOC_NOPM, 0, 0,
  1217. wcd937x_codec_aux_dac_event,
  1218. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1219. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1220. SND_SOC_DAPM_MUX("RDAC3_MUX", SND_SOC_NOPM, 0, 0, &rx_rdac3_mux),
  1221. SND_SOC_DAPM_MIXER_E("RX1", SND_SOC_NOPM, 0, 0, NULL, 0,
  1222. wcd937x_enable_rx1, SND_SOC_DAPM_PRE_PMU |
  1223. SND_SOC_DAPM_POST_PMD),
  1224. SND_SOC_DAPM_MIXER_E("RX2", SND_SOC_NOPM, 0, 0, NULL, 0,
  1225. wcd937x_enable_rx2, SND_SOC_DAPM_PRE_PMU |
  1226. SND_SOC_DAPM_POST_PMD),
  1227. SND_SOC_DAPM_MIXER_E("RX3", SND_SOC_NOPM, 0, 0, NULL, 0,
  1228. wcd937x_enable_rx3, SND_SOC_DAPM_PRE_PMU |
  1229. SND_SOC_DAPM_POST_PMD),
  1230. /* rx mixer widgets*/
  1231. SND_SOC_DAPM_MIXER("EAR_RDAC", SND_SOC_NOPM, 0, 0,
  1232. ear_rdac_switch, ARRAY_SIZE(ear_rdac_switch)),
  1233. SND_SOC_DAPM_MIXER("AUX_RDAC", SND_SOC_NOPM, 0, 0,
  1234. aux_rdac_switch, ARRAY_SIZE(aux_rdac_switch)),
  1235. SND_SOC_DAPM_MIXER("HPHL_RDAC", SND_SOC_NOPM, 0, 0,
  1236. hphl_rdac_switch, ARRAY_SIZE(hphl_rdac_switch)),
  1237. SND_SOC_DAPM_MIXER("HPHR_RDAC", SND_SOC_NOPM, 0, 0,
  1238. hphr_rdac_switch, ARRAY_SIZE(hphr_rdac_switch)),
  1239. /*output widgets tx*/
  1240. SND_SOC_DAPM_OUTPUT("ADC1_OUTPUT"),
  1241. SND_SOC_DAPM_OUTPUT("ADC2_OUTPUT"),
  1242. /*output widgets rx*/
  1243. SND_SOC_DAPM_OUTPUT("EAR"),
  1244. SND_SOC_DAPM_OUTPUT("AUX"),
  1245. SND_SOC_DAPM_OUTPUT("HPHL"),
  1246. SND_SOC_DAPM_OUTPUT("HPHR"),
  1247. };
  1248. static const struct snd_soc_dapm_widget wcd9375_dapm_widgets[] = {
  1249. /*input widgets*/
  1250. SND_SOC_DAPM_INPUT("AMIC4"),
  1251. /*tx widgets*/
  1252. SND_SOC_DAPM_ADC_E("ADC3", NULL, SND_SOC_NOPM, 2, 0,
  1253. wcd937x_codec_enable_adc,
  1254. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1255. SND_SOC_DAPM_MIXER_E("ADC3 REQ", SND_SOC_NOPM, 0, 0,
  1256. NULL, 0, wcd937x_enable_req,
  1257. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1258. SND_SOC_DAPM_ADC_E("DMIC1", NULL, SND_SOC_NOPM, 0, 0,
  1259. wcd937x_codec_enable_dmic,
  1260. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1261. SND_SOC_DAPM_ADC_E("DMIC2", NULL, SND_SOC_NOPM, 1, 0,
  1262. wcd937x_codec_enable_dmic,
  1263. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1264. SND_SOC_DAPM_ADC_E("DMIC3", NULL, SND_SOC_NOPM, 2, 0,
  1265. wcd937x_codec_enable_dmic,
  1266. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1267. SND_SOC_DAPM_ADC_E("DMIC4", NULL, SND_SOC_NOPM, 3, 0,
  1268. wcd937x_codec_enable_dmic,
  1269. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1270. SND_SOC_DAPM_ADC_E("DMIC5", NULL, SND_SOC_NOPM, 4, 0,
  1271. wcd937x_codec_enable_dmic,
  1272. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1273. SND_SOC_DAPM_ADC_E("DMIC6", NULL, SND_SOC_NOPM, 5, 0,
  1274. wcd937x_codec_enable_dmic,
  1275. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1276. /*tx mixer widgets*/
  1277. SND_SOC_DAPM_MIXER_E("DMIC1_MIXER", SND_SOC_NOPM, 0,
  1278. 0, dmic1_switch, ARRAY_SIZE(dmic1_switch),
  1279. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1280. SND_SOC_DAPM_POST_PMD),
  1281. SND_SOC_DAPM_MIXER_E("DMIC2_MIXER", SND_SOC_NOPM, 0,
  1282. 0, dmic2_switch, ARRAY_SIZE(dmic2_switch),
  1283. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1284. SND_SOC_DAPM_POST_PMD),
  1285. SND_SOC_DAPM_MIXER_E("DMIC3_MIXER", SND_SOC_NOPM, 0,
  1286. 0, dmic3_switch, ARRAY_SIZE(dmic3_switch),
  1287. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1288. SND_SOC_DAPM_POST_PMD),
  1289. SND_SOC_DAPM_MIXER_E("DMIC4_MIXER", SND_SOC_NOPM, 0,
  1290. 0, dmic4_switch, ARRAY_SIZE(dmic4_switch),
  1291. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1292. SND_SOC_DAPM_POST_PMD),
  1293. SND_SOC_DAPM_MIXER_E("DMIC5_MIXER", SND_SOC_NOPM, 0,
  1294. 0, dmic5_switch, ARRAY_SIZE(dmic5_switch),
  1295. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1296. SND_SOC_DAPM_POST_PMD),
  1297. SND_SOC_DAPM_MIXER_E("DMIC6_MIXER", SND_SOC_NOPM, 0,
  1298. 0, dmic6_switch, ARRAY_SIZE(dmic6_switch),
  1299. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1300. SND_SOC_DAPM_POST_PMD),
  1301. SND_SOC_DAPM_MIXER_E("ADC3_MIXER", SND_SOC_NOPM, 0, 0, adc3_switch,
  1302. ARRAY_SIZE(adc3_switch), wcd937x_tx_swr_ctrl,
  1303. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1304. /*output widgets*/
  1305. SND_SOC_DAPM_OUTPUT("DMIC1_OUTPUT"),
  1306. SND_SOC_DAPM_OUTPUT("DMIC2_OUTPUT"),
  1307. SND_SOC_DAPM_OUTPUT("DMIC3_OUTPUT"),
  1308. SND_SOC_DAPM_OUTPUT("DMIC4_OUTPUT"),
  1309. SND_SOC_DAPM_OUTPUT("DMIC5_OUTPUT"),
  1310. SND_SOC_DAPM_OUTPUT("DMIC6_OUTPUT"),
  1311. SND_SOC_DAPM_OUTPUT("ADC3_OUTPUT"),
  1312. };
  1313. static const struct snd_soc_dapm_route wcd937x_audio_map[] = {
  1314. {"ADC1_OUTPUT", NULL, "ADC1_MIXER"},
  1315. {"ADC1_MIXER", "Switch", "ADC1 REQ"},
  1316. {"ADC1 REQ", NULL, "ADC1"},
  1317. {"ADC1", NULL, "AMIC1"},
  1318. {"ADC2_OUTPUT", NULL, "ADC2_MIXER"},
  1319. {"ADC2_MIXER", "Switch", "ADC2 REQ"},
  1320. {"ADC2 REQ", NULL, "ADC2"},
  1321. {"ADC2", NULL, "ADC2 MUX"},
  1322. {"ADC2 MUX", "INP3", "AMIC3"},
  1323. {"ADC2 MUX", "INP2", "AMIC2"},
  1324. {"RX1", NULL, "IN1_HPHL"},
  1325. {"RDAC1", NULL, "RX1"},
  1326. {"HPHL_RDAC", "Switch", "RDAC1"},
  1327. {"HPHL PGA", NULL, "HPHL_RDAC"},
  1328. {"HPHL", NULL, "HPHL PGA"},
  1329. {"RX2", NULL, "IN2_HPHR"},
  1330. {"RDAC2", NULL, "RX2"},
  1331. {"HPHR_RDAC", "Switch", "RDAC2"},
  1332. {"HPHR PGA", NULL, "HPHR_RDAC"},
  1333. {"HPHR", NULL, "HPHR PGA"},
  1334. {"RX3", NULL, "IN3_AUX"},
  1335. {"RDAC4", NULL, "RX3"},
  1336. {"AUX_RDAC", "Switch", "RDAC4"},
  1337. {"AUX PGA", NULL, "AUX_RDAC"},
  1338. {"AUX", NULL, "AUX PGA"},
  1339. {"RDAC3_MUX", "RX3", "RX3"},
  1340. {"RDAC3_MUX", "RX1", "RX1"},
  1341. {"RDAC3", NULL, "RDAC3_MUX"},
  1342. {"EAR_RDAC", "Switch", "RDAC3"},
  1343. {"EAR PGA", NULL, "EAR_RDAC"},
  1344. {"EAR", NULL, "EAR PGA"},
  1345. };
  1346. static const struct snd_soc_dapm_route wcd9375_audio_map[] = {
  1347. {"ADC3_OUTPUT", NULL, "ADC3_MIXER"},
  1348. {"ADC3_MIXER", "Switch", "ADC3 REQ"},
  1349. {"ADC3 REQ", NULL, "ADC3"},
  1350. {"ADC3", NULL, "AMIC4"},
  1351. {"DMIC1_OUTPUT", NULL, "DMIC1_MIXER"},
  1352. {"DMIC1_MIXER", "Switch", "DMIC1"},
  1353. {"DMIC2_OUTPUT", NULL, "DMIC2_MIXER"},
  1354. {"DMIC2_MIXER", "Switch", "DMIC2"},
  1355. {"DMIC3_OUTPUT", NULL, "DMIC3_MIXER"},
  1356. {"DMIC3_MIXER", "Switch", "DMIC3"},
  1357. {"DMIC4_OUTPUT", NULL, "DMIC4_MIXER"},
  1358. {"DMIC4_MIXER", "Switch", "DMIC4"},
  1359. {"DMIC5_OUTPUT", NULL, "DMIC5_MIXER"},
  1360. {"DMIC5_MIXER", "Switch", "DMIC5"},
  1361. {"DMIC6_OUTPUT", NULL, "DMIC6_MIXER"},
  1362. {"DMIC6_MIXER", "Switch", "DMIC6"},
  1363. };
  1364. static ssize_t wcd937x_version_read(struct snd_info_entry *entry,
  1365. void *file_private_data,
  1366. struct file *file,
  1367. char __user *buf, size_t count,
  1368. loff_t pos)
  1369. {
  1370. struct wcd937x_priv *priv;
  1371. char buffer[WCD937X_VERSION_ENTRY_SIZE];
  1372. int len = 0;
  1373. priv = (struct wcd937x_priv *) entry->private_data;
  1374. if (!priv) {
  1375. pr_err("%s: wcd937x priv is null\n", __func__);
  1376. return -EINVAL;
  1377. }
  1378. switch (priv->version) {
  1379. case WCD937X_VERSION_1_0:
  1380. len = snprintf(buffer, sizeof(buffer), "WCD937X_1_0\n");
  1381. break;
  1382. default:
  1383. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  1384. }
  1385. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  1386. }
  1387. static struct snd_info_entry_ops wcd937x_info_ops = {
  1388. .read = wcd937x_version_read,
  1389. };
  1390. /*
  1391. * wcd937x_info_create_codec_entry - creates wcd937x module
  1392. * @codec_root: The parent directory
  1393. * @codec: Codec instance
  1394. *
  1395. * Creates wcd937x module and version entry under the given
  1396. * parent directory.
  1397. *
  1398. * Return: 0 on success or negative error code on failure.
  1399. */
  1400. int wcd937x_info_create_codec_entry(struct snd_info_entry *codec_root,
  1401. struct snd_soc_codec *codec)
  1402. {
  1403. struct snd_info_entry *version_entry;
  1404. struct wcd937x_priv *priv;
  1405. struct snd_soc_card *card;
  1406. if (!codec_root || !codec)
  1407. return -EINVAL;
  1408. priv = snd_soc_codec_get_drvdata(codec);
  1409. if (priv->entry) {
  1410. dev_dbg(priv->dev,
  1411. "%s:wcd937x module already created\n", __func__);
  1412. return 0;
  1413. }
  1414. card = codec->component.card;
  1415. priv->entry = snd_info_create_subdir(codec_root->module,
  1416. "wcd937x", codec_root);
  1417. if (!priv->entry) {
  1418. dev_dbg(codec->dev, "%s: failed to create wcd937x entry\n",
  1419. __func__);
  1420. return -ENOMEM;
  1421. }
  1422. version_entry = snd_info_create_card_entry(card->snd_card,
  1423. "version",
  1424. priv->entry);
  1425. if (!version_entry) {
  1426. dev_dbg(codec->dev, "%s: failed to create wcd937x version entry\n",
  1427. __func__);
  1428. return -ENOMEM;
  1429. }
  1430. version_entry->private_data = priv;
  1431. version_entry->size = WCD937X_VERSION_ENTRY_SIZE;
  1432. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  1433. version_entry->c.ops = &wcd937x_info_ops;
  1434. if (snd_info_register(version_entry) < 0) {
  1435. snd_info_free_entry(version_entry);
  1436. return -ENOMEM;
  1437. }
  1438. priv->version_entry = version_entry;
  1439. return 0;
  1440. }
  1441. EXPORT_SYMBOL(wcd937x_info_create_codec_entry);
  1442. static int wcd937x_soc_codec_probe(struct snd_soc_codec *codec)
  1443. {
  1444. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  1445. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  1446. int variant;
  1447. int ret = -EINVAL;
  1448. dev_info(codec->dev, "%s()\n", __func__);
  1449. wcd937x = snd_soc_codec_get_drvdata(codec);
  1450. if (!wcd937x)
  1451. return -EINVAL;
  1452. wcd937x->codec = codec;
  1453. variant = (snd_soc_read(codec, WCD937X_DIGITAL_EFUSE_REG_0) & 0x0E) >> 1;
  1454. wcd937x->variant = variant;
  1455. wcd937x->fw_data = devm_kzalloc(codec->dev,
  1456. sizeof(*(wcd937x->fw_data)),
  1457. GFP_KERNEL);
  1458. if (!wcd937x->fw_data) {
  1459. dev_err(codec->dev, "Failed to allocate fw_data\n");
  1460. ret = -ENOMEM;
  1461. goto err;
  1462. }
  1463. set_bit(WCD9XXX_MBHC_CAL, wcd937x->fw_data->cal_bit);
  1464. ret = wcd_cal_create_hwdep(wcd937x->fw_data,
  1465. WCD9XXX_CODEC_HWDEP_NODE, codec);
  1466. if (ret < 0) {
  1467. dev_err(codec->dev, "%s hwdep failed %d\n", __func__, ret);
  1468. goto err_hwdep;
  1469. }
  1470. ret = wcd937x_mbhc_init(&wcd937x->mbhc, codec, wcd937x->fw_data);
  1471. if (ret) {
  1472. pr_err("%s: mbhc initialization failed\n", __func__);
  1473. goto err_hwdep;
  1474. }
  1475. wcd937x_init_reg(codec);
  1476. if (wcd937x->variant == WCD9375_VARIANT) {
  1477. ret = snd_soc_dapm_new_controls(dapm, wcd9375_dapm_widgets,
  1478. ARRAY_SIZE(wcd9375_dapm_widgets));
  1479. if (ret < 0) {
  1480. dev_err(codec->dev, "%s: Failed to add snd_ctls\n",
  1481. __func__);
  1482. goto err_hwdep;
  1483. }
  1484. ret = snd_soc_dapm_add_routes(dapm, wcd9375_audio_map,
  1485. ARRAY_SIZE(wcd9375_audio_map));
  1486. if (ret < 0) {
  1487. dev_err(codec->dev, "%s: Failed to add routes\n",
  1488. __func__);
  1489. goto err_hwdep;
  1490. }
  1491. ret = snd_soc_dapm_new_widgets(dapm->card);
  1492. if (ret < 0) {
  1493. dev_err(codec->dev, "%s: Failed to add widgets\n",
  1494. __func__);
  1495. goto err_hwdep;
  1496. }
  1497. }
  1498. wcd937x->version = WCD937X_VERSION_1_0;
  1499. return ret;
  1500. err_hwdep:
  1501. wcd937x->fw_data = NULL;
  1502. err:
  1503. return ret;
  1504. }
  1505. static int wcd937x_soc_codec_remove(struct snd_soc_codec *codec)
  1506. {
  1507. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  1508. if (!wcd937x)
  1509. return -EINVAL;
  1510. return 0;
  1511. }
  1512. static struct regmap *wcd937x_get_regmap(struct device *dev)
  1513. {
  1514. struct wcd937x_priv *wcd937x = dev_get_drvdata(dev);
  1515. return wcd937x->regmap;
  1516. }
  1517. static struct snd_soc_codec_driver soc_codec_dev_wcd937x = {
  1518. .probe = wcd937x_soc_codec_probe,
  1519. .remove = wcd937x_soc_codec_remove,
  1520. .get_regmap = wcd937x_get_regmap,
  1521. .component_driver = {
  1522. .controls = wcd937x_snd_controls,
  1523. .num_controls = ARRAY_SIZE(wcd937x_snd_controls),
  1524. .dapm_widgets = wcd937x_dapm_widgets,
  1525. .num_dapm_widgets = ARRAY_SIZE(wcd937x_dapm_widgets),
  1526. .dapm_routes = wcd937x_audio_map,
  1527. .num_dapm_routes = ARRAY_SIZE(wcd937x_audio_map),
  1528. },
  1529. };
  1530. int wcd937x_reset(struct device *dev)
  1531. {
  1532. struct wcd937x_priv *wcd937x = NULL;
  1533. int rc = 0;
  1534. int value = 0;
  1535. if (!dev)
  1536. return -ENODEV;
  1537. wcd937x = dev_get_drvdata(dev);
  1538. if (!wcd937x)
  1539. return -EINVAL;
  1540. if (!wcd937x->rst_np) {
  1541. dev_err(dev, "%s: reset gpio device node not specified\n",
  1542. __func__);
  1543. return -EINVAL;
  1544. }
  1545. value = msm_cdc_pinctrl_get_state(wcd937x->rst_np);
  1546. if (value > 0)
  1547. return 0;
  1548. rc = msm_cdc_pinctrl_select_sleep_state(wcd937x->rst_np);
  1549. if (rc) {
  1550. dev_err(dev, "%s: wcd sleep state request fail!\n",
  1551. __func__);
  1552. return rc;
  1553. }
  1554. /* 20ms sleep required after pulling the reset gpio to LOW */
  1555. usleep_range(20, 30);
  1556. rc = msm_cdc_pinctrl_select_active_state(wcd937x->rst_np);
  1557. if (rc) {
  1558. dev_err(dev, "%s: wcd active state request fail!\n",
  1559. __func__);
  1560. return rc;
  1561. }
  1562. /* 20ms sleep required after pulling the reset gpio to HIGH */
  1563. usleep_range(20, 30);
  1564. return rc;
  1565. }
  1566. static int wcd937x_read_of_property_u32(struct device *dev, const char *name,
  1567. u32 *val)
  1568. {
  1569. int rc = 0;
  1570. rc = of_property_read_u32(dev->of_node, name, val);
  1571. if (rc)
  1572. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  1573. __func__, name, dev->of_node->full_name);
  1574. return rc;
  1575. }
  1576. static void wcd937x_dt_parse_micbias_info(struct device *dev,
  1577. struct wcd937x_micbias_setting *mb)
  1578. {
  1579. u32 prop_val = 0;
  1580. int rc = 0;
  1581. /* MB1 */
  1582. if (of_find_property(dev->of_node, "qcom,cdc-micbias1-mv",
  1583. NULL)) {
  1584. rc = wcd937x_read_of_property_u32(dev,
  1585. "qcom,cdc-micbias1-mv",
  1586. &prop_val);
  1587. if (!rc)
  1588. mb->micb1_mv = prop_val;
  1589. } else {
  1590. dev_info(dev, "%s: Micbias1 DT property not found\n",
  1591. __func__);
  1592. }
  1593. /* MB2 */
  1594. if (of_find_property(dev->of_node, "qcom,cdc-micbias2-mv",
  1595. NULL)) {
  1596. rc = wcd937x_read_of_property_u32(dev,
  1597. "qcom,cdc-micbias2-mv",
  1598. &prop_val);
  1599. if (!rc)
  1600. mb->micb2_mv = prop_val;
  1601. } else {
  1602. dev_info(dev, "%s: Micbias2 DT property not found\n",
  1603. __func__);
  1604. }
  1605. /* MB3 */
  1606. if (of_find_property(dev->of_node, "qcom,cdc-micbias3-mv",
  1607. NULL)) {
  1608. rc = wcd937x_read_of_property_u32(dev,
  1609. "qcom,cdc-micbias3-mv",
  1610. &prop_val);
  1611. if (!rc)
  1612. mb->micb3_mv = prop_val;
  1613. } else {
  1614. dev_info(dev, "%s: Micbias3 DT property not found\n",
  1615. __func__);
  1616. }
  1617. }
  1618. struct wcd937x_pdata *wcd937x_populate_dt_data(struct device *dev)
  1619. {
  1620. struct wcd937x_pdata *pdata = NULL;
  1621. pdata = devm_kzalloc(dev, sizeof(struct wcd937x_pdata),
  1622. GFP_KERNEL);
  1623. if (!pdata)
  1624. return NULL;
  1625. pdata->rst_np = of_parse_phandle(dev->of_node,
  1626. "qcom,wcd-rst-gpio-node", 0);
  1627. if (!pdata->rst_np) {
  1628. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  1629. __func__, "qcom,wcd-rst-gpio-node",
  1630. dev->of_node->full_name);
  1631. return NULL;
  1632. }
  1633. /* Parse power supplies */
  1634. msm_cdc_get_power_supplies(dev, &pdata->regulator,
  1635. &pdata->num_supplies);
  1636. if (!pdata->regulator || (pdata->num_supplies <= 0)) {
  1637. dev_err(dev, "%s: no power supplies defined for codec\n",
  1638. __func__);
  1639. return NULL;
  1640. }
  1641. pdata->rx_slave = of_parse_phandle(dev->of_node, "qcom,rx-slave", 0);
  1642. pdata->tx_slave = of_parse_phandle(dev->of_node, "qcom,tx-slave", 0);
  1643. wcd937x_dt_parse_micbias_info(dev, &pdata->micbias);
  1644. return pdata;
  1645. }
  1646. static int wcd937x_bind(struct device *dev)
  1647. {
  1648. int ret = 0, i = 0;
  1649. struct wcd937x_priv *wcd937x = NULL;
  1650. struct wcd937x_pdata *pdata = NULL;
  1651. wcd937x = devm_kzalloc(dev, sizeof(struct wcd937x_priv), GFP_KERNEL);
  1652. if (!wcd937x)
  1653. return -ENOMEM;
  1654. dev_set_drvdata(dev, wcd937x);
  1655. pdata = wcd937x_populate_dt_data(dev);
  1656. if (!pdata) {
  1657. dev_err(dev, "%s: Fail to obtain platform data\n", __func__);
  1658. return -EINVAL;
  1659. }
  1660. wcd937x->dev = dev;
  1661. wcd937x->dev->platform_data = pdata;
  1662. wcd937x->rst_np = pdata->rst_np;
  1663. ret = msm_cdc_init_supplies(dev, &wcd937x->supplies,
  1664. pdata->regulator, pdata->num_supplies);
  1665. if (!wcd937x->supplies) {
  1666. dev_err(dev, "%s: Cannot init wcd supplies\n",
  1667. __func__);
  1668. return ret;
  1669. }
  1670. ret = msm_cdc_enable_static_supplies(dev, wcd937x->supplies,
  1671. pdata->regulator,
  1672. pdata->num_supplies);
  1673. if (ret) {
  1674. dev_err(dev, "%s: wcd static supply enable failed!\n",
  1675. __func__);
  1676. return ret;
  1677. }
  1678. wcd937x_reset(dev);
  1679. /*
  1680. * Add 5msec delay to provide sufficient time for
  1681. * soundwire auto enumeration of slave devices as
  1682. * as per HW requirement.
  1683. */
  1684. usleep_range(5000, 5010);
  1685. ret = component_bind_all(dev, wcd937x);
  1686. if (ret) {
  1687. dev_err(dev, "%s: Slave bind failed, ret = %d\n",
  1688. __func__, ret);
  1689. return ret;
  1690. }
  1691. ret = wcd937x_parse_port_mapping(dev, "qcom,rx_swr_ch_map", CODEC_RX);
  1692. ret |= wcd937x_parse_port_mapping(dev, "qcom,tx_swr_ch_map", CODEC_TX);
  1693. if (ret) {
  1694. dev_err(dev, "Failed to read port mapping\n");
  1695. goto err;
  1696. }
  1697. wcd937x->rx_swr_dev = get_matching_swr_slave_device(pdata->rx_slave);
  1698. if (!wcd937x->rx_swr_dev) {
  1699. dev_err(dev, "%s: Could not find RX swr slave device\n",
  1700. __func__);
  1701. ret = -ENODEV;
  1702. goto err;
  1703. }
  1704. wcd937x->tx_swr_dev = get_matching_swr_slave_device(pdata->tx_slave);
  1705. if (!wcd937x->tx_swr_dev) {
  1706. dev_err(dev, "%s: Could not find TX swr slave device\n",
  1707. __func__);
  1708. ret = -ENODEV;
  1709. goto err;
  1710. }
  1711. wcd937x->regmap = devm_regmap_init_swr(wcd937x->tx_swr_dev,
  1712. &wcd937x_regmap_config);
  1713. if (!wcd937x->regmap) {
  1714. dev_err(dev, "%s: Regmap init failed\n",
  1715. __func__);
  1716. goto err;
  1717. }
  1718. /* Set all interupts as edge triggered */
  1719. for (i = 0; i < wcd937x_regmap_irq_chip.num_regs; i++)
  1720. regmap_write(wcd937x->regmap,
  1721. (WCD937X_DIGITAL_INTR_LEVEL_0 + i), 0);
  1722. wcd937x_regmap_irq_chip.irq_drv_data = wcd937x;
  1723. wcd937x->irq_info.wcd_regmap_irq_chip = &wcd937x_regmap_irq_chip;
  1724. wcd937x->irq_info.codec_name = "WCD937X";
  1725. wcd937x->irq_info.regmap = wcd937x->regmap;
  1726. wcd937x->irq_info.dev = dev;
  1727. ret = wcd_irq_init(&wcd937x->irq_info, &wcd937x->virq);
  1728. if (ret) {
  1729. dev_err(dev, "%s: IRQ init failed: %d\n",
  1730. __func__, ret);
  1731. goto err;
  1732. }
  1733. wcd937x->tx_swr_dev->slave_irq = wcd937x->virq;
  1734. ret = snd_soc_register_codec(dev, &soc_codec_dev_wcd937x,
  1735. NULL, 0);
  1736. if (ret) {
  1737. dev_err(dev, "%s: Codec registration failed\n",
  1738. __func__);
  1739. goto err_irq;
  1740. }
  1741. return ret;
  1742. err_irq:
  1743. wcd_irq_exit(&wcd937x->irq_info, wcd937x->virq);
  1744. err:
  1745. component_unbind_all(dev, wcd937x);
  1746. return ret;
  1747. }
  1748. static void wcd937x_unbind(struct device *dev)
  1749. {
  1750. struct wcd937x_priv *wcd937x = dev_get_drvdata(dev);
  1751. wcd_irq_exit(&wcd937x->irq_info, wcd937x->virq);
  1752. snd_soc_unregister_codec(dev);
  1753. component_unbind_all(dev, wcd937x);
  1754. }
  1755. static const struct of_device_id wcd937x_dt_match[] = {
  1756. { .compatible = "qcom,wcd937x-codec" },
  1757. {}
  1758. };
  1759. static const struct component_master_ops wcd937x_comp_ops = {
  1760. .bind = wcd937x_bind,
  1761. .unbind = wcd937x_unbind,
  1762. };
  1763. static int wcd937x_compare_of(struct device *dev, void *data)
  1764. {
  1765. return dev->of_node == data;
  1766. }
  1767. static void wcd937x_release_of(struct device *dev, void *data)
  1768. {
  1769. of_node_put(data);
  1770. }
  1771. static int wcd937x_add_slave_components(struct device *dev,
  1772. struct component_match **matchptr)
  1773. {
  1774. struct device_node *np, *rx_node, *tx_node;
  1775. np = dev->of_node;
  1776. rx_node = of_parse_phandle(np, "qcom,rx-slave", 0);
  1777. if (!rx_node) {
  1778. dev_err(dev, "%s: Rx-slave node not defined\n", __func__);
  1779. return -ENODEV;
  1780. }
  1781. of_node_get(rx_node);
  1782. component_match_add_release(dev, matchptr,
  1783. wcd937x_release_of,
  1784. wcd937x_compare_of,
  1785. rx_node);
  1786. tx_node = of_parse_phandle(np, "qcom,tx-slave", 0);
  1787. if (!tx_node) {
  1788. dev_err(dev, "%s: Tx-slave node not defined\n", __func__);
  1789. return -ENODEV;
  1790. }
  1791. of_node_get(tx_node);
  1792. component_match_add_release(dev, matchptr,
  1793. wcd937x_release_of,
  1794. wcd937x_compare_of,
  1795. tx_node);
  1796. return 0;
  1797. }
  1798. static int wcd937x_probe(struct platform_device *pdev)
  1799. {
  1800. struct component_match *match = NULL;
  1801. int ret;
  1802. ret = wcd937x_add_slave_components(&pdev->dev, &match);
  1803. if (ret)
  1804. return ret;
  1805. return component_master_add_with_match(&pdev->dev,
  1806. &wcd937x_comp_ops, match);
  1807. }
  1808. static int wcd937x_remove(struct platform_device *pdev)
  1809. {
  1810. component_master_del(&pdev->dev, &wcd937x_comp_ops);
  1811. return 0;
  1812. }
  1813. static struct platform_driver wcd937x_codec_driver = {
  1814. .probe = wcd937x_probe,
  1815. .remove = wcd937x_remove,
  1816. .driver = {
  1817. .name = "wcd937x_codec",
  1818. .owner = THIS_MODULE,
  1819. .of_match_table = of_match_ptr(wcd937x_dt_match),
  1820. },
  1821. };
  1822. module_platform_driver(wcd937x_codec_driver);
  1823. MODULE_DESCRIPTION("WCD937X Codec driver");
  1824. MODULE_LICENSE("GPL v2");