wcd937x.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  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_PRE_PMD:
  483. blocking_notifier_call_chain(&wcd937x->mbhc->notifier,
  484. WCD_EVENT_PRE_HPHR_PA_OFF,
  485. &wcd937x->mbhc->wcd_mbhc);
  486. break;
  487. case SND_SOC_DAPM_POST_PMD:
  488. usleep_range(7000, 7010);
  489. blocking_notifier_call_chain(&wcd937x->mbhc->notifier,
  490. WCD_EVENT_POST_HPHR_PA_OFF,
  491. &wcd937x->mbhc->wcd_mbhc);
  492. snd_soc_update_bits(codec, WCD937X_ANA_HPH, 0x10, 0x00);
  493. break;
  494. };
  495. return ret;
  496. }
  497. static int wcd937x_codec_enable_hphl_pa(struct snd_soc_dapm_widget *w,
  498. struct snd_kcontrol *kcontrol,
  499. int event)
  500. {
  501. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  502. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  503. int ret = 0;
  504. switch (event) {
  505. case SND_SOC_DAPM_PRE_PMU:
  506. snd_soc_update_bits(codec, WCD937X_ANA_HPH, 0x0C, 0x08);
  507. snd_soc_update_bits(codec, WCD937X_ANA_HPH, 0x20, 0x20);
  508. usleep_range(100, 110);
  509. break;
  510. case SND_SOC_DAPM_POST_PMU:
  511. usleep_range(7000, 7010);
  512. snd_soc_update_bits(codec, WCD937X_HPH_NEW_INT_HPH_TIMER1,
  513. 0x02, 0x02);
  514. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  515. 0x02, 0x02);
  516. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  517. wcd937x->rx_swr_dev->dev_num,
  518. true);
  519. break;
  520. case SND_SOC_DAPM_PRE_PMD:
  521. blocking_notifier_call_chain(&wcd937x->mbhc->notifier,
  522. WCD_EVENT_PRE_HPHL_PA_OFF,
  523. &wcd937x->mbhc->wcd_mbhc);
  524. break;
  525. case SND_SOC_DAPM_POST_PMD:
  526. usleep_range(7000, 7010);
  527. blocking_notifier_call_chain(&wcd937x->mbhc->notifier,
  528. WCD_EVENT_POST_HPHL_PA_OFF,
  529. &wcd937x->mbhc->wcd_mbhc);
  530. snd_soc_update_bits(codec, WCD937X_ANA_HPH, 0x20, 0x00);
  531. break;
  532. };
  533. return ret;
  534. }
  535. static int wcd937x_codec_enable_aux_pa(struct snd_soc_dapm_widget *w,
  536. struct snd_kcontrol *kcontrol,
  537. int event)
  538. {
  539. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  540. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  541. int ret = 0;
  542. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  543. w->name, event);
  544. switch (event) {
  545. case SND_SOC_DAPM_PRE_PMU:
  546. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  547. 0x80, 0x80);
  548. usleep_range(500, 510);
  549. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x3A);
  550. usleep_range(500, 510);
  551. break;
  552. case SND_SOC_DAPM_POST_PMU:
  553. usleep_range(1000, 1010);
  554. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  555. 0x20, 0x20);
  556. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  557. wcd937x->rx_swr_dev->dev_num,
  558. true);
  559. break;
  560. case SND_SOC_DAPM_POST_PMD:
  561. usleep_range(1000, 1010);
  562. usleep_range(1000, 1010);
  563. break;
  564. };
  565. return ret;
  566. }
  567. static int wcd937x_codec_enable_ear_pa(struct snd_soc_dapm_widget *w,
  568. struct snd_kcontrol *kcontrol,
  569. int event)
  570. {
  571. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  572. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  573. int ret = 0;
  574. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  575. w->name, event);
  576. switch (event) {
  577. case SND_SOC_DAPM_PRE_PMU:
  578. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  579. 0x08, 0x08);
  580. usleep_range(500, 510);
  581. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x3A);
  582. usleep_range(500, 510);
  583. break;
  584. case SND_SOC_DAPM_POST_PMU:
  585. usleep_range(6000, 6010);
  586. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  587. 0x02, 0x02);
  588. ret = swr_slvdev_datapath_control(wcd937x->rx_swr_dev,
  589. wcd937x->rx_swr_dev->dev_num,
  590. true);
  591. break;
  592. case SND_SOC_DAPM_POST_PMD:
  593. usleep_range(7000, 7010);
  594. break;
  595. };
  596. return ret;
  597. }
  598. static int wcd937x_enable_rx1(struct snd_soc_dapm_widget *w,
  599. struct snd_kcontrol *kcontrol,
  600. int event)
  601. {
  602. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  603. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  604. w->name, event);
  605. switch (event) {
  606. case SND_SOC_DAPM_PRE_PMU:
  607. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEG_CTRL_4,
  608. 0xF0, 0x80);
  609. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEGDAC_CTRL_2,
  610. 0xE0, 0xA0);
  611. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_3,
  612. 0x02, 0x02);
  613. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2,
  614. 0xFF, 0x1C);
  615. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  616. 0x40, 0x40);
  617. usleep_range(100, 110);
  618. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEGDAC_CTRL_2,
  619. 0xE0, 0xE0);
  620. usleep_range(100, 110);
  621. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  622. 0x80, 0x80);
  623. usleep_range(500, 510);
  624. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x3A);
  625. usleep_range(500, 510);
  626. wcd937x_rx_connect_port(codec, HPH_L, true);
  627. wcd937x_rx_connect_port(codec, COMP_L, true);
  628. break;
  629. case SND_SOC_DAPM_POST_PMD:
  630. wcd937x_rx_connect_port(codec, HPH_L, false);
  631. wcd937x_rx_connect_port(codec, COMP_L, false);
  632. wcd937x_rx_clk_disable(codec);
  633. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  634. 0x01, 0x00);
  635. break;
  636. };
  637. return 0;
  638. }
  639. static int wcd937x_enable_rx2(struct snd_soc_dapm_widget *w,
  640. struct snd_kcontrol *kcontrol, int event)
  641. {
  642. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  643. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  644. w->name, event);
  645. switch (event) {
  646. case SND_SOC_DAPM_PRE_PMU:
  647. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEG_CTRL_4,
  648. 0xF0, 0x80);
  649. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEGDAC_CTRL_2,
  650. 0xE0, 0xA0);
  651. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_3, 0x02, 0x02);
  652. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x1C);
  653. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  654. 0x40, 0x40);
  655. usleep_range(100, 110);
  656. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEGDAC_CTRL_2,
  657. 0xE0, 0xE0);
  658. usleep_range(100, 110);
  659. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  660. 0x80, 0x80);
  661. usleep_range(500, 510);
  662. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x3A);
  663. usleep_range(500, 510);
  664. wcd937x_rx_connect_port(codec, HPH_R, true);
  665. wcd937x_rx_connect_port(codec, COMP_R, true);
  666. break;
  667. case SND_SOC_DAPM_POST_PMD:
  668. wcd937x_rx_connect_port(codec, HPH_R, false);
  669. wcd937x_rx_connect_port(codec, COMP_R, false);
  670. wcd937x_rx_clk_disable(codec);
  671. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  672. 0x02, 0x00);
  673. break;
  674. };
  675. return 0;
  676. }
  677. static int wcd937x_enable_rx3(struct snd_soc_dapm_widget *w,
  678. struct snd_kcontrol *kcontrol,
  679. int event)
  680. {
  681. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  682. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  683. w->name, event);
  684. switch (event) {
  685. case SND_SOC_DAPM_PRE_PMU:
  686. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEG_CTRL_2,
  687. 0xE0, 0xA0);
  688. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_3, 0x02, 0x02);
  689. snd_soc_update_bits(codec, WCD937X_CLASSH_MODE_2, 0xFF, 0x1C);
  690. snd_soc_update_bits(codec, WCD937X_ANA_RX_SUPPLIES,
  691. 0x40, 0x40);
  692. usleep_range(100, 110);
  693. snd_soc_update_bits(codec, WCD937X_FLYBACK_VNEG_CTRL_2,
  694. 0xE0, 0xE0);
  695. usleep_range(100, 110);
  696. wcd937x_rx_connect_port(codec, LO, true);
  697. break;
  698. case SND_SOC_DAPM_POST_PMD:
  699. wcd937x_rx_connect_port(codec, LO, false);
  700. usleep_range(6000, 6010);
  701. wcd937x_rx_clk_disable(codec);
  702. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  703. 0x04, 0x00);
  704. break;
  705. }
  706. return 0;
  707. }
  708. static int wcd937x_codec_enable_dmic(struct snd_soc_dapm_widget *w,
  709. struct snd_kcontrol *kcontrol,
  710. int event)
  711. {
  712. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  713. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  714. u16 dmic_clk_reg;
  715. s32 *dmic_clk_cnt;
  716. unsigned int dmic;
  717. char *wname;
  718. int ret = 0;
  719. wname = strpbrk(w->name, "012345");
  720. if (!wname) {
  721. dev_err(codec->dev, "%s: widget not found\n", __func__);
  722. return -EINVAL;
  723. }
  724. ret = kstrtouint(wname, 10, &dmic);
  725. if (ret < 0) {
  726. dev_err(codec->dev, "%s: Invalid DMIC line on the codec\n",
  727. __func__);
  728. return -EINVAL;
  729. }
  730. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  731. w->name, event);
  732. switch (dmic) {
  733. case 0:
  734. case 1:
  735. dmic_clk_cnt = &(wcd937x->dmic_0_1_clk_cnt);
  736. dmic_clk_reg = WCD937X_DIGITAL_CDC_DMIC0_CTL;
  737. break;
  738. case 2:
  739. case 3:
  740. dmic_clk_cnt = &(wcd937x->dmic_2_3_clk_cnt);
  741. dmic_clk_reg = WCD937X_DIGITAL_CDC_DMIC1_CTL;
  742. break;
  743. case 4:
  744. case 5:
  745. dmic_clk_cnt = &(wcd937x->dmic_4_5_clk_cnt);
  746. dmic_clk_reg = WCD937X_DIGITAL_CDC_DMIC2_CTL;
  747. break;
  748. default:
  749. dev_err(codec->dev, "%s: Invalid DMIC Selection\n",
  750. __func__);
  751. return -EINVAL;
  752. };
  753. dev_dbg(codec->dev, "%s: event %d DMIC%d dmic_clk_cnt %d\n",
  754. __func__, event, dmic, *dmic_clk_cnt);
  755. switch (event) {
  756. case SND_SOC_DAPM_PRE_PMU:
  757. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  758. 0x80, 0x80);
  759. snd_soc_update_bits(codec, dmic_clk_reg, 0x07, 0x02);
  760. snd_soc_update_bits(codec, dmic_clk_reg, 0x08, 0x08);
  761. snd_soc_update_bits(codec, dmic_clk_reg, 0x70, 0x20);
  762. wcd937x_tx_connect_port(codec, DMIC0 + (w->shift), true);
  763. break;
  764. case SND_SOC_DAPM_POST_PMD:
  765. wcd937x_tx_connect_port(codec, DMIC0 + (w->shift), false);
  766. break;
  767. };
  768. return 0;
  769. }
  770. /*
  771. * wcd937x_get_micb_vout_ctl_val: converts micbias from volts to register value
  772. * @micb_mv: micbias in mv
  773. *
  774. * return register value converted
  775. */
  776. int wcd937x_get_micb_vout_ctl_val(u32 micb_mv)
  777. {
  778. /* min micbias voltage is 1V and maximum is 2.85V */
  779. if (micb_mv < 1000 || micb_mv > 2850) {
  780. pr_err("%s: unsupported micbias voltage\n", __func__);
  781. return -EINVAL;
  782. }
  783. return (micb_mv - 1000) / 50;
  784. }
  785. EXPORT_SYMBOL(wcd937x_get_micb_vout_ctl_val);
  786. /*
  787. * wcd937x_mbhc_micb_adjust_voltage: adjust specific micbias voltage
  788. * @codec: handle to snd_soc_codec *
  789. * @req_volt: micbias voltage to be set
  790. * @micb_num: micbias to be set, e.g. micbias1 or micbias2
  791. *
  792. * return 0 if adjustment is success or error code in case of failure
  793. */
  794. int wcd937x_mbhc_micb_adjust_voltage(struct snd_soc_codec *codec,
  795. int req_volt, int micb_num)
  796. {
  797. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  798. int cur_vout_ctl, req_vout_ctl;
  799. int micb_reg, micb_val, micb_en;
  800. int ret = 0;
  801. switch (micb_num) {
  802. case MIC_BIAS_1:
  803. micb_reg = WCD937X_ANA_MICB1;
  804. break;
  805. case MIC_BIAS_2:
  806. micb_reg = WCD937X_ANA_MICB2;
  807. break;
  808. case MIC_BIAS_3:
  809. micb_reg = WCD937X_ANA_MICB3;
  810. break;
  811. default:
  812. return -EINVAL;
  813. }
  814. mutex_lock(&wcd937x->micb_lock);
  815. /*
  816. * If requested micbias voltage is same as current micbias
  817. * voltage, then just return. Otherwise, adjust voltage as
  818. * per requested value. If micbias is already enabled, then
  819. * to avoid slow micbias ramp-up or down enable pull-up
  820. * momentarily, change the micbias value and then re-enable
  821. * micbias.
  822. */
  823. micb_val = snd_soc_read(codec, micb_reg);
  824. micb_en = (micb_val & 0xC0) >> 6;
  825. cur_vout_ctl = micb_val & 0x3F;
  826. req_vout_ctl = wcd937x_get_micb_vout_ctl_val(req_volt);
  827. if (req_vout_ctl < 0) {
  828. ret = -EINVAL;
  829. goto exit;
  830. }
  831. if (cur_vout_ctl == req_vout_ctl) {
  832. ret = 0;
  833. goto exit;
  834. }
  835. dev_dbg(codec->dev, "%s: micb_num: %d, cur_mv: %d, req_mv: %d, micb_en: %d\n",
  836. __func__, micb_num, WCD_VOUT_CTL_TO_MICB(cur_vout_ctl),
  837. req_volt, micb_en);
  838. if (micb_en == 0x1)
  839. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x80);
  840. snd_soc_update_bits(codec, micb_reg, 0x3F, req_vout_ctl);
  841. if (micb_en == 0x1) {
  842. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x40);
  843. /*
  844. * Add 2ms delay as per HW requirement after enabling
  845. * micbias
  846. */
  847. usleep_range(2000, 2100);
  848. }
  849. exit:
  850. mutex_unlock(&wcd937x->micb_lock);
  851. return ret;
  852. }
  853. EXPORT_SYMBOL(wcd937x_mbhc_micb_adjust_voltage);
  854. static int wcd937x_tx_swr_ctrl(struct snd_soc_dapm_widget *w,
  855. struct snd_kcontrol *kcontrol,
  856. int event)
  857. {
  858. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  859. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  860. int ret = 0;
  861. switch (event) {
  862. case SND_SOC_DAPM_PRE_PMU:
  863. ret = swr_slvdev_datapath_control(wcd937x->tx_swr_dev,
  864. wcd937x->tx_swr_dev->dev_num,
  865. true);
  866. break;
  867. case SND_SOC_DAPM_POST_PMD:
  868. ret = swr_slvdev_datapath_control(wcd937x->tx_swr_dev,
  869. wcd937x->tx_swr_dev->dev_num,
  870. false);
  871. break;
  872. };
  873. return ret;
  874. }
  875. static int wcd937x_codec_enable_adc(struct snd_soc_dapm_widget *w,
  876. struct snd_kcontrol *kcontrol,
  877. int event){
  878. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  879. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  880. w->name, event);
  881. switch (event) {
  882. case SND_SOC_DAPM_PRE_PMU:
  883. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  884. 0x80, 0x80);
  885. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  886. 0x08, 0x08);
  887. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  888. 0x10, 0x10);
  889. wcd937x_tx_connect_port(codec, ADC1 + (w->shift), true);
  890. break;
  891. case SND_SOC_DAPM_POST_PMD:
  892. wcd937x_tx_connect_port(codec, ADC1 + (w->shift), false);
  893. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  894. 0x08, 0x00);
  895. break;
  896. };
  897. return 0;
  898. }
  899. static int wcd937x_enable_req(struct snd_soc_dapm_widget *w,
  900. struct snd_kcontrol *kcontrol, int event)
  901. {
  902. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  903. dev_dbg(codec->dev, "%s wname: %s event: %d\n", __func__,
  904. w->name, event);
  905. switch (event) {
  906. case SND_SOC_DAPM_PRE_PMU:
  907. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_REQ_CTL,
  908. 0x02, 0x02);
  909. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_REQ_CTL, 0x01,
  910. 0x00);
  911. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH2, 0x40, 0x40);
  912. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  913. 0x30, 0x30);
  914. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH1, 0x80, 0x80);
  915. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH2, 0x40, 0x00);
  916. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH2, 0x80, 0x80);
  917. break;
  918. case SND_SOC_DAPM_POST_PMD:
  919. snd_soc_update_bits(codec, WCD937X_ANA_TX_CH1, 0x80, 0x00);
  920. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  921. 0x10, 0x00);
  922. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL,
  923. 0x10, 0x00);
  924. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL,
  925. 0x80, 0x00);
  926. break;
  927. };
  928. return 0;
  929. }
  930. int wcd937x_micbias_control(struct snd_soc_codec *codec,
  931. int micb_num, int req, bool is_dapm)
  932. {
  933. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  934. int micb_index = micb_num - 1;
  935. u16 micb_reg;
  936. int pre_off_event = 0, post_off_event = 0;
  937. int post_on_event = 0, post_dapm_off = 0;
  938. int post_dapm_on = 0;
  939. if ((micb_index < 0) || (micb_index > WCD937X_MAX_MICBIAS - 1)) {
  940. dev_err(codec->dev, "%s: Invalid micbias index, micb_ind:%d\n",
  941. __func__, micb_index);
  942. return -EINVAL;
  943. }
  944. switch (micb_num) {
  945. case MIC_BIAS_1:
  946. micb_reg = WCD937X_ANA_MICB1;
  947. break;
  948. case MIC_BIAS_2:
  949. micb_reg = WCD937X_ANA_MICB2;
  950. pre_off_event = WCD_EVENT_PRE_MICBIAS_2_OFF;
  951. post_off_event = WCD_EVENT_POST_MICBIAS_2_OFF;
  952. post_on_event = WCD_EVENT_POST_MICBIAS_2_ON;
  953. post_dapm_on = WCD_EVENT_POST_DAPM_MICBIAS_2_ON;
  954. post_dapm_off = WCD_EVENT_POST_DAPM_MICBIAS_2_OFF;
  955. break;
  956. case MIC_BIAS_3:
  957. micb_reg = WCD937X_ANA_MICB3;
  958. break;
  959. default:
  960. dev_err(codec->dev, "%s: Invalid micbias number: %d\n",
  961. __func__, micb_num);
  962. return -EINVAL;
  963. };
  964. mutex_lock(&wcd937x->micb_lock);
  965. switch (req) {
  966. case MICB_PULLUP_ENABLE:
  967. wcd937x->pullup_ref[micb_index]++;
  968. if ((wcd937x->pullup_ref[micb_index] == 1) &&
  969. (wcd937x->micb_ref[micb_index] == 0))
  970. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x80);
  971. break;
  972. case MICB_PULLUP_DISABLE:
  973. if (wcd937x->pullup_ref[micb_index] > 0)
  974. wcd937x->pullup_ref[micb_index]--;
  975. if ((wcd937x->pullup_ref[micb_index] == 0) &&
  976. (wcd937x->micb_ref[micb_index] == 0))
  977. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x00);
  978. break;
  979. case MICB_ENABLE:
  980. wcd937x->micb_ref[micb_index]++;
  981. if (wcd937x->micb_ref[micb_index] == 1) {
  982. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_DIG_CLK_CTL, 0xE0, 0xE0);
  983. snd_soc_update_bits(codec, WCD937X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x10);
  984. snd_soc_update_bits(codec, WCD937X_MICB1_TEST_CTL_2, 0x01, 0x01);
  985. snd_soc_update_bits(codec, WCD937X_MICB2_TEST_CTL_2, 0x01, 0x01);
  986. snd_soc_update_bits(codec, WCD937X_MICB3_TEST_CTL_2, 0x01, 0x01);
  987. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x40);
  988. if (post_on_event && wcd937x->mbhc)
  989. blocking_notifier_call_chain(
  990. &wcd937x->mbhc->notifier, post_on_event,
  991. &wcd937x->mbhc->wcd_mbhc);
  992. }
  993. if (is_dapm && post_dapm_on && wcd937x->mbhc)
  994. blocking_notifier_call_chain(
  995. &wcd937x->mbhc->notifier, post_dapm_on,
  996. &wcd937x->mbhc->wcd_mbhc);
  997. break;
  998. case MICB_DISABLE:
  999. if (wcd937x->micb_ref[micb_index] > 0)
  1000. wcd937x->micb_ref[micb_index]--;
  1001. if ((wcd937x->micb_ref[micb_index] == 0) &&
  1002. (wcd937x->pullup_ref[micb_index] > 0))
  1003. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x80);
  1004. else if ((wcd937x->micb_ref[micb_index] == 0) &&
  1005. (wcd937x->pullup_ref[micb_index] == 0)) {
  1006. if (pre_off_event && wcd937x->mbhc)
  1007. blocking_notifier_call_chain(
  1008. &wcd937x->mbhc->notifier, pre_off_event,
  1009. &wcd937x->mbhc->wcd_mbhc);
  1010. snd_soc_update_bits(codec, micb_reg, 0xC0, 0x00);
  1011. if (post_off_event && wcd937x->mbhc)
  1012. blocking_notifier_call_chain(
  1013. &wcd937x->mbhc->notifier,
  1014. post_off_event,
  1015. &wcd937x->mbhc->wcd_mbhc);
  1016. }
  1017. if (is_dapm && post_dapm_off && wcd937x->mbhc)
  1018. blocking_notifier_call_chain(
  1019. &wcd937x->mbhc->notifier, post_dapm_off,
  1020. &wcd937x->mbhc->wcd_mbhc);
  1021. break;
  1022. };
  1023. dev_dbg(codec->dev, "%s: micb_num:%d, micb_ref: %d, pullup_ref: %d\n",
  1024. __func__, micb_num, wcd937x->micb_ref[micb_index],
  1025. wcd937x->pullup_ref[micb_index]);
  1026. mutex_unlock(&wcd937x->micb_lock);
  1027. return 0;
  1028. }
  1029. EXPORT_SYMBOL(wcd937x_micbias_control);
  1030. static int __wcd937x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1031. int event)
  1032. {
  1033. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1034. int micb_num;
  1035. dev_dbg(codec->dev, "%s: wname: %s, event: %d\n",
  1036. __func__, w->name, event);
  1037. if (strnstr(w->name, "MIC BIAS1", sizeof("MIC BIAS1")))
  1038. micb_num = MIC_BIAS_1;
  1039. else if (strnstr(w->name, "MIC BIAS2", sizeof("MIC BIAS2")))
  1040. micb_num = MIC_BIAS_2;
  1041. else if (strnstr(w->name, "MIC BIAS3", sizeof("MIC BIAS3")))
  1042. micb_num = MIC_BIAS_3;
  1043. else
  1044. return -EINVAL;
  1045. switch (event) {
  1046. case SND_SOC_DAPM_PRE_PMU:
  1047. wcd937x_micbias_control(codec, micb_num, MICB_ENABLE, true);
  1048. break;
  1049. case SND_SOC_DAPM_POST_PMU:
  1050. usleep_range(1000, 1100);
  1051. break;
  1052. case SND_SOC_DAPM_POST_PMD:
  1053. wcd937x_micbias_control(codec, micb_num, MICB_DISABLE, true);
  1054. break;
  1055. };
  1056. return 0;
  1057. }
  1058. static int wcd937x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1059. struct snd_kcontrol *kcontrol,
  1060. int event)
  1061. {
  1062. return __wcd937x_codec_enable_micbias(w, event);
  1063. }
  1064. static int wcd937x_rx_hph_mode_get(struct snd_kcontrol *kcontrol,
  1065. struct snd_ctl_elem_value *ucontrol)
  1066. {
  1067. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1068. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  1069. ucontrol->value.integer.value[0] = wcd937x->hph_mode;
  1070. return 0;
  1071. }
  1072. static int wcd937x_rx_hph_mode_put(struct snd_kcontrol *kcontrol,
  1073. struct snd_ctl_elem_value *ucontrol)
  1074. {
  1075. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  1076. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  1077. u32 mode_val;
  1078. mode_val = ucontrol->value.enumerated.item[0];
  1079. dev_dbg(codec->dev, "%s: mode: %d\n", __func__, mode_val);
  1080. if (mode_val == 0) {
  1081. dev_warn(codec->dev, "%s:Invalid HPH Mode, default to class_AB\n",
  1082. __func__);
  1083. mode_val = 3; /* enum will be updated later */
  1084. }
  1085. wcd937x->hph_mode = mode_val;
  1086. return 0;
  1087. }
  1088. static const char * const rx_hph_mode_mux_text[] = {
  1089. "CLS_H_INVALID", "CLS_H_HIFI", "CLS_H_LP", "CLS_AB", "CLS_H_LOHIFI",
  1090. "CLS_H_ULP", "CLS_AB_HIFI",
  1091. };
  1092. static const struct soc_enum rx_hph_mode_mux_enum =
  1093. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(rx_hph_mode_mux_text),
  1094. rx_hph_mode_mux_text);
  1095. static const struct snd_kcontrol_new wcd937x_snd_controls[] = {
  1096. SOC_ENUM_EXT("RX HPH Mode", rx_hph_mode_mux_enum,
  1097. wcd937x_rx_hph_mode_get, wcd937x_rx_hph_mode_put),
  1098. SOC_SINGLE_TLV("HPHL Volume", WCD937X_HPH_L_EN, 0, 20, 1, line_gain),
  1099. SOC_SINGLE_TLV("HPHR Volume", WCD937X_HPH_R_EN, 0, 20, 1, line_gain),
  1100. SOC_SINGLE_TLV("ADC1 Volume", WCD937X_ANA_TX_CH1, 0, 20, 0, analog_gain),
  1101. SOC_SINGLE_TLV("ADC2 Volume", WCD937X_ANA_TX_CH2, 0, 20, 0, analog_gain),
  1102. SOC_SINGLE_TLV("ADC3 Volume", WCD937X_ANA_TX_CH3, 0, 20, 0, analog_gain),
  1103. };
  1104. static const struct snd_kcontrol_new adc1_switch[] = {
  1105. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1106. };
  1107. static const struct snd_kcontrol_new adc2_switch[] = {
  1108. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1109. };
  1110. static const struct snd_kcontrol_new adc3_switch[] = {
  1111. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1112. };
  1113. static const struct snd_kcontrol_new dmic1_switch[] = {
  1114. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1115. };
  1116. static const struct snd_kcontrol_new dmic2_switch[] = {
  1117. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1118. };
  1119. static const struct snd_kcontrol_new dmic3_switch[] = {
  1120. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1121. };
  1122. static const struct snd_kcontrol_new dmic4_switch[] = {
  1123. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1124. };
  1125. static const struct snd_kcontrol_new dmic5_switch[] = {
  1126. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1127. };
  1128. static const struct snd_kcontrol_new dmic6_switch[] = {
  1129. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1130. };
  1131. static const struct snd_kcontrol_new ear_rdac_switch[] = {
  1132. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1133. };
  1134. static const struct snd_kcontrol_new aux_rdac_switch[] = {
  1135. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1136. };
  1137. static const struct snd_kcontrol_new hphl_rdac_switch[] = {
  1138. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1139. };
  1140. static const struct snd_kcontrol_new hphr_rdac_switch[] = {
  1141. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1142. };
  1143. static const char * const adc2_mux_text[] = {
  1144. "INP2", "INP3"
  1145. };
  1146. static const char * const rdac3_mux_text[] = {
  1147. "RX1", "RX3"
  1148. };
  1149. static const struct soc_enum adc2_enum =
  1150. SOC_ENUM_SINGLE(WCD937X_TX_NEW_TX_CH2_SEL, 7,
  1151. ARRAY_SIZE(adc2_mux_text), adc2_mux_text);
  1152. static const struct soc_enum rdac3_enum =
  1153. SOC_ENUM_SINGLE(WCD937X_DIGITAL_CDC_EAR_PATH_CTL, 0,
  1154. ARRAY_SIZE(rdac3_mux_text), rdac3_mux_text);
  1155. static const struct snd_kcontrol_new tx_adc2_mux =
  1156. SOC_DAPM_ENUM("ADC2 MUX Mux", adc2_enum);
  1157. static const struct snd_kcontrol_new rx_rdac3_mux =
  1158. SOC_DAPM_ENUM("RDAC3_MUX Mux", rdac3_enum);
  1159. static const struct snd_soc_dapm_widget wcd937x_dapm_widgets[] = {
  1160. /*input widgets*/
  1161. SND_SOC_DAPM_INPUT("AMIC1"),
  1162. SND_SOC_DAPM_INPUT("AMIC2"),
  1163. SND_SOC_DAPM_INPUT("AMIC3"),
  1164. SND_SOC_DAPM_INPUT("IN1_HPHL"),
  1165. SND_SOC_DAPM_INPUT("IN2_HPHR"),
  1166. SND_SOC_DAPM_INPUT("IN3_AUX"),
  1167. /*tx widgets*/
  1168. SND_SOC_DAPM_ADC_E("ADC1", NULL, SND_SOC_NOPM, 0, 0,
  1169. wcd937x_codec_enable_adc,
  1170. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1171. SND_SOC_DAPM_ADC_E("ADC2", NULL, SND_SOC_NOPM, 1, 0,
  1172. wcd937x_codec_enable_adc,
  1173. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1174. SND_SOC_DAPM_MIXER_E("ADC1 REQ", SND_SOC_NOPM, 0, 0,
  1175. NULL, 0, wcd937x_enable_req,
  1176. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1177. SND_SOC_DAPM_MIXER_E("ADC2 REQ", SND_SOC_NOPM, 0, 0,
  1178. NULL, 0, wcd937x_enable_req,
  1179. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1180. SND_SOC_DAPM_MUX("ADC2 MUX", SND_SOC_NOPM, 0, 0,
  1181. &tx_adc2_mux),
  1182. /*tx mixers*/
  1183. SND_SOC_DAPM_MIXER_E("ADC1_MIXER", SND_SOC_NOPM, 0, 0,
  1184. adc1_switch, ARRAY_SIZE(adc1_switch),
  1185. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1186. SND_SOC_DAPM_POST_PMD),
  1187. SND_SOC_DAPM_MIXER_E("ADC2_MIXER", SND_SOC_NOPM, 0, 0,
  1188. adc2_switch, ARRAY_SIZE(adc2_switch),
  1189. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1190. SND_SOC_DAPM_POST_PMD),
  1191. /* micbias widgets*/
  1192. SND_SOC_DAPM_MICBIAS_E("MIC BIAS1", SND_SOC_NOPM, 0, 0,
  1193. wcd937x_codec_enable_micbias,
  1194. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1195. SND_SOC_DAPM_POST_PMD),
  1196. SND_SOC_DAPM_MICBIAS_E("MIC BIAS2", SND_SOC_NOPM, 0, 0,
  1197. wcd937x_codec_enable_micbias,
  1198. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1199. SND_SOC_DAPM_POST_PMD),
  1200. SND_SOC_DAPM_MICBIAS_E("MIC BIAS3", SND_SOC_NOPM, 0, 0,
  1201. wcd937x_codec_enable_micbias,
  1202. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1203. SND_SOC_DAPM_POST_PMD),
  1204. /*rx widgets*/
  1205. SND_SOC_DAPM_PGA_E("EAR PGA", WCD937X_ANA_EAR, 7, 0, NULL, 0,
  1206. wcd937x_codec_enable_ear_pa,
  1207. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1208. SND_SOC_DAPM_POST_PMD),
  1209. SND_SOC_DAPM_PGA_E("AUX PGA", WCD937X_AUX_AUXPA, 7, 0, NULL, 0,
  1210. wcd937x_codec_enable_aux_pa,
  1211. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1212. SND_SOC_DAPM_POST_PMD),
  1213. SND_SOC_DAPM_PGA_E("HPHL PGA", WCD937X_ANA_HPH, 7, 0, NULL, 0,
  1214. wcd937x_codec_enable_hphl_pa,
  1215. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1216. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1217. SND_SOC_DAPM_PGA_E("HPHR PGA", WCD937X_ANA_HPH, 6, 0, NULL, 0,
  1218. wcd937x_codec_enable_hphr_pa,
  1219. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1220. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1221. SND_SOC_DAPM_DAC_E("RDAC1", NULL, SND_SOC_NOPM, 0, 0,
  1222. wcd937x_codec_hphl_dac_event,
  1223. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1224. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1225. SND_SOC_DAPM_DAC_E("RDAC2", NULL, SND_SOC_NOPM, 0, 0,
  1226. wcd937x_codec_hphr_dac_event,
  1227. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1228. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1229. SND_SOC_DAPM_DAC_E("RDAC3", NULL, SND_SOC_NOPM, 0, 0,
  1230. wcd937x_codec_ear_dac_event,
  1231. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1232. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1233. SND_SOC_DAPM_DAC_E("RDAC4", NULL, SND_SOC_NOPM, 0, 0,
  1234. wcd937x_codec_aux_dac_event,
  1235. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1236. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1237. SND_SOC_DAPM_MUX("RDAC3_MUX", SND_SOC_NOPM, 0, 0, &rx_rdac3_mux),
  1238. SND_SOC_DAPM_MIXER_E("RX1", SND_SOC_NOPM, 0, 0, NULL, 0,
  1239. wcd937x_enable_rx1, SND_SOC_DAPM_PRE_PMU |
  1240. SND_SOC_DAPM_POST_PMD),
  1241. SND_SOC_DAPM_MIXER_E("RX2", SND_SOC_NOPM, 0, 0, NULL, 0,
  1242. wcd937x_enable_rx2, SND_SOC_DAPM_PRE_PMU |
  1243. SND_SOC_DAPM_POST_PMD),
  1244. SND_SOC_DAPM_MIXER_E("RX3", SND_SOC_NOPM, 0, 0, NULL, 0,
  1245. wcd937x_enable_rx3, SND_SOC_DAPM_PRE_PMU |
  1246. SND_SOC_DAPM_POST_PMD),
  1247. /* rx mixer widgets*/
  1248. SND_SOC_DAPM_MIXER("EAR_RDAC", SND_SOC_NOPM, 0, 0,
  1249. ear_rdac_switch, ARRAY_SIZE(ear_rdac_switch)),
  1250. SND_SOC_DAPM_MIXER("AUX_RDAC", SND_SOC_NOPM, 0, 0,
  1251. aux_rdac_switch, ARRAY_SIZE(aux_rdac_switch)),
  1252. SND_SOC_DAPM_MIXER("HPHL_RDAC", SND_SOC_NOPM, 0, 0,
  1253. hphl_rdac_switch, ARRAY_SIZE(hphl_rdac_switch)),
  1254. SND_SOC_DAPM_MIXER("HPHR_RDAC", SND_SOC_NOPM, 0, 0,
  1255. hphr_rdac_switch, ARRAY_SIZE(hphr_rdac_switch)),
  1256. /*output widgets tx*/
  1257. SND_SOC_DAPM_OUTPUT("ADC1_OUTPUT"),
  1258. SND_SOC_DAPM_OUTPUT("ADC2_OUTPUT"),
  1259. /*output widgets rx*/
  1260. SND_SOC_DAPM_OUTPUT("EAR"),
  1261. SND_SOC_DAPM_OUTPUT("AUX"),
  1262. SND_SOC_DAPM_OUTPUT("HPHL"),
  1263. SND_SOC_DAPM_OUTPUT("HPHR"),
  1264. };
  1265. static const struct snd_soc_dapm_widget wcd9375_dapm_widgets[] = {
  1266. /*input widgets*/
  1267. SND_SOC_DAPM_INPUT("AMIC4"),
  1268. /*tx widgets*/
  1269. SND_SOC_DAPM_ADC_E("ADC3", NULL, SND_SOC_NOPM, 2, 0,
  1270. wcd937x_codec_enable_adc,
  1271. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1272. SND_SOC_DAPM_MIXER_E("ADC3 REQ", SND_SOC_NOPM, 0, 0,
  1273. NULL, 0, wcd937x_enable_req,
  1274. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1275. SND_SOC_DAPM_ADC_E("DMIC1", NULL, SND_SOC_NOPM, 0, 0,
  1276. wcd937x_codec_enable_dmic,
  1277. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1278. SND_SOC_DAPM_ADC_E("DMIC2", NULL, SND_SOC_NOPM, 1, 0,
  1279. wcd937x_codec_enable_dmic,
  1280. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1281. SND_SOC_DAPM_ADC_E("DMIC3", NULL, SND_SOC_NOPM, 2, 0,
  1282. wcd937x_codec_enable_dmic,
  1283. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1284. SND_SOC_DAPM_ADC_E("DMIC4", NULL, SND_SOC_NOPM, 3, 0,
  1285. wcd937x_codec_enable_dmic,
  1286. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1287. SND_SOC_DAPM_ADC_E("DMIC5", NULL, SND_SOC_NOPM, 4, 0,
  1288. wcd937x_codec_enable_dmic,
  1289. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1290. SND_SOC_DAPM_ADC_E("DMIC6", NULL, SND_SOC_NOPM, 5, 0,
  1291. wcd937x_codec_enable_dmic,
  1292. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1293. /*tx mixer widgets*/
  1294. SND_SOC_DAPM_MIXER_E("DMIC1_MIXER", SND_SOC_NOPM, 0,
  1295. 0, dmic1_switch, ARRAY_SIZE(dmic1_switch),
  1296. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1297. SND_SOC_DAPM_POST_PMD),
  1298. SND_SOC_DAPM_MIXER_E("DMIC2_MIXER", SND_SOC_NOPM, 0,
  1299. 0, dmic2_switch, ARRAY_SIZE(dmic2_switch),
  1300. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1301. SND_SOC_DAPM_POST_PMD),
  1302. SND_SOC_DAPM_MIXER_E("DMIC3_MIXER", SND_SOC_NOPM, 0,
  1303. 0, dmic3_switch, ARRAY_SIZE(dmic3_switch),
  1304. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1305. SND_SOC_DAPM_POST_PMD),
  1306. SND_SOC_DAPM_MIXER_E("DMIC4_MIXER", SND_SOC_NOPM, 0,
  1307. 0, dmic4_switch, ARRAY_SIZE(dmic4_switch),
  1308. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1309. SND_SOC_DAPM_POST_PMD),
  1310. SND_SOC_DAPM_MIXER_E("DMIC5_MIXER", SND_SOC_NOPM, 0,
  1311. 0, dmic5_switch, ARRAY_SIZE(dmic5_switch),
  1312. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1313. SND_SOC_DAPM_POST_PMD),
  1314. SND_SOC_DAPM_MIXER_E("DMIC6_MIXER", SND_SOC_NOPM, 0,
  1315. 0, dmic6_switch, ARRAY_SIZE(dmic6_switch),
  1316. wcd937x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1317. SND_SOC_DAPM_POST_PMD),
  1318. SND_SOC_DAPM_MIXER_E("ADC3_MIXER", SND_SOC_NOPM, 0, 0, adc3_switch,
  1319. ARRAY_SIZE(adc3_switch), wcd937x_tx_swr_ctrl,
  1320. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1321. /*output widgets*/
  1322. SND_SOC_DAPM_OUTPUT("DMIC1_OUTPUT"),
  1323. SND_SOC_DAPM_OUTPUT("DMIC2_OUTPUT"),
  1324. SND_SOC_DAPM_OUTPUT("DMIC3_OUTPUT"),
  1325. SND_SOC_DAPM_OUTPUT("DMIC4_OUTPUT"),
  1326. SND_SOC_DAPM_OUTPUT("DMIC5_OUTPUT"),
  1327. SND_SOC_DAPM_OUTPUT("DMIC6_OUTPUT"),
  1328. SND_SOC_DAPM_OUTPUT("ADC3_OUTPUT"),
  1329. };
  1330. static const struct snd_soc_dapm_route wcd937x_audio_map[] = {
  1331. {"ADC1_OUTPUT", NULL, "ADC1_MIXER"},
  1332. {"ADC1_MIXER", "Switch", "ADC1 REQ"},
  1333. {"ADC1 REQ", NULL, "ADC1"},
  1334. {"ADC1", NULL, "AMIC1"},
  1335. {"ADC2_OUTPUT", NULL, "ADC2_MIXER"},
  1336. {"ADC2_MIXER", "Switch", "ADC2 REQ"},
  1337. {"ADC2 REQ", NULL, "ADC2"},
  1338. {"ADC2", NULL, "ADC2 MUX"},
  1339. {"ADC2 MUX", "INP3", "AMIC3"},
  1340. {"ADC2 MUX", "INP2", "AMIC2"},
  1341. {"RX1", NULL, "IN1_HPHL"},
  1342. {"RDAC1", NULL, "RX1"},
  1343. {"HPHL_RDAC", "Switch", "RDAC1"},
  1344. {"HPHL PGA", NULL, "HPHL_RDAC"},
  1345. {"HPHL", NULL, "HPHL PGA"},
  1346. {"RX2", NULL, "IN2_HPHR"},
  1347. {"RDAC2", NULL, "RX2"},
  1348. {"HPHR_RDAC", "Switch", "RDAC2"},
  1349. {"HPHR PGA", NULL, "HPHR_RDAC"},
  1350. {"HPHR", NULL, "HPHR PGA"},
  1351. {"RX3", NULL, "IN3_AUX"},
  1352. {"RDAC4", NULL, "RX3"},
  1353. {"AUX_RDAC", "Switch", "RDAC4"},
  1354. {"AUX PGA", NULL, "AUX_RDAC"},
  1355. {"AUX", NULL, "AUX PGA"},
  1356. {"RDAC3_MUX", "RX3", "RX3"},
  1357. {"RDAC3_MUX", "RX1", "RX1"},
  1358. {"RDAC3", NULL, "RDAC3_MUX"},
  1359. {"EAR_RDAC", "Switch", "RDAC3"},
  1360. {"EAR PGA", NULL, "EAR_RDAC"},
  1361. {"EAR", NULL, "EAR PGA"},
  1362. };
  1363. static const struct snd_soc_dapm_route wcd9375_audio_map[] = {
  1364. {"ADC3_OUTPUT", NULL, "ADC3_MIXER"},
  1365. {"ADC3_MIXER", "Switch", "ADC3 REQ"},
  1366. {"ADC3 REQ", NULL, "ADC3"},
  1367. {"ADC3", NULL, "AMIC4"},
  1368. {"DMIC1_OUTPUT", NULL, "DMIC1_MIXER"},
  1369. {"DMIC1_MIXER", "Switch", "DMIC1"},
  1370. {"DMIC2_OUTPUT", NULL, "DMIC2_MIXER"},
  1371. {"DMIC2_MIXER", "Switch", "DMIC2"},
  1372. {"DMIC3_OUTPUT", NULL, "DMIC3_MIXER"},
  1373. {"DMIC3_MIXER", "Switch", "DMIC3"},
  1374. {"DMIC4_OUTPUT", NULL, "DMIC4_MIXER"},
  1375. {"DMIC4_MIXER", "Switch", "DMIC4"},
  1376. {"DMIC5_OUTPUT", NULL, "DMIC5_MIXER"},
  1377. {"DMIC5_MIXER", "Switch", "DMIC5"},
  1378. {"DMIC6_OUTPUT", NULL, "DMIC6_MIXER"},
  1379. {"DMIC6_MIXER", "Switch", "DMIC6"},
  1380. };
  1381. static ssize_t wcd937x_version_read(struct snd_info_entry *entry,
  1382. void *file_private_data,
  1383. struct file *file,
  1384. char __user *buf, size_t count,
  1385. loff_t pos)
  1386. {
  1387. struct wcd937x_priv *priv;
  1388. char buffer[WCD937X_VERSION_ENTRY_SIZE];
  1389. int len = 0;
  1390. priv = (struct wcd937x_priv *) entry->private_data;
  1391. if (!priv) {
  1392. pr_err("%s: wcd937x priv is null\n", __func__);
  1393. return -EINVAL;
  1394. }
  1395. switch (priv->version) {
  1396. case WCD937X_VERSION_1_0:
  1397. len = snprintf(buffer, sizeof(buffer), "WCD937X_1_0\n");
  1398. break;
  1399. default:
  1400. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  1401. }
  1402. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  1403. }
  1404. static struct snd_info_entry_ops wcd937x_info_ops = {
  1405. .read = wcd937x_version_read,
  1406. };
  1407. /*
  1408. * wcd937x_info_create_codec_entry - creates wcd937x module
  1409. * @codec_root: The parent directory
  1410. * @codec: Codec instance
  1411. *
  1412. * Creates wcd937x module and version entry under the given
  1413. * parent directory.
  1414. *
  1415. * Return: 0 on success or negative error code on failure.
  1416. */
  1417. int wcd937x_info_create_codec_entry(struct snd_info_entry *codec_root,
  1418. struct snd_soc_codec *codec)
  1419. {
  1420. struct snd_info_entry *version_entry;
  1421. struct wcd937x_priv *priv;
  1422. struct snd_soc_card *card;
  1423. if (!codec_root || !codec)
  1424. return -EINVAL;
  1425. priv = snd_soc_codec_get_drvdata(codec);
  1426. if (priv->entry) {
  1427. dev_dbg(priv->dev,
  1428. "%s:wcd937x module already created\n", __func__);
  1429. return 0;
  1430. }
  1431. card = codec->component.card;
  1432. priv->entry = snd_info_create_subdir(codec_root->module,
  1433. "wcd937x", codec_root);
  1434. if (!priv->entry) {
  1435. dev_dbg(codec->dev, "%s: failed to create wcd937x entry\n",
  1436. __func__);
  1437. return -ENOMEM;
  1438. }
  1439. version_entry = snd_info_create_card_entry(card->snd_card,
  1440. "version",
  1441. priv->entry);
  1442. if (!version_entry) {
  1443. dev_dbg(codec->dev, "%s: failed to create wcd937x version entry\n",
  1444. __func__);
  1445. return -ENOMEM;
  1446. }
  1447. version_entry->private_data = priv;
  1448. version_entry->size = WCD937X_VERSION_ENTRY_SIZE;
  1449. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  1450. version_entry->c.ops = &wcd937x_info_ops;
  1451. if (snd_info_register(version_entry) < 0) {
  1452. snd_info_free_entry(version_entry);
  1453. return -ENOMEM;
  1454. }
  1455. priv->version_entry = version_entry;
  1456. return 0;
  1457. }
  1458. EXPORT_SYMBOL(wcd937x_info_create_codec_entry);
  1459. static int wcd937x_soc_codec_probe(struct snd_soc_codec *codec)
  1460. {
  1461. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  1462. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  1463. int variant;
  1464. int ret = -EINVAL;
  1465. dev_info(codec->dev, "%s()\n", __func__);
  1466. wcd937x = snd_soc_codec_get_drvdata(codec);
  1467. if (!wcd937x)
  1468. return -EINVAL;
  1469. wcd937x->codec = codec;
  1470. variant = (snd_soc_read(codec, WCD937X_DIGITAL_EFUSE_REG_0) & 0x0E) >> 1;
  1471. wcd937x->variant = variant;
  1472. wcd937x->fw_data = devm_kzalloc(codec->dev,
  1473. sizeof(*(wcd937x->fw_data)),
  1474. GFP_KERNEL);
  1475. if (!wcd937x->fw_data) {
  1476. dev_err(codec->dev, "Failed to allocate fw_data\n");
  1477. ret = -ENOMEM;
  1478. goto err;
  1479. }
  1480. set_bit(WCD9XXX_MBHC_CAL, wcd937x->fw_data->cal_bit);
  1481. ret = wcd_cal_create_hwdep(wcd937x->fw_data,
  1482. WCD9XXX_CODEC_HWDEP_NODE, codec);
  1483. if (ret < 0) {
  1484. dev_err(codec->dev, "%s hwdep failed %d\n", __func__, ret);
  1485. goto err_hwdep;
  1486. }
  1487. ret = wcd937x_mbhc_init(&wcd937x->mbhc, codec, wcd937x->fw_data);
  1488. if (ret) {
  1489. pr_err("%s: mbhc initialization failed\n", __func__);
  1490. goto err_hwdep;
  1491. }
  1492. wcd937x_init_reg(codec);
  1493. if (wcd937x->variant == WCD9375_VARIANT) {
  1494. ret = snd_soc_dapm_new_controls(dapm, wcd9375_dapm_widgets,
  1495. ARRAY_SIZE(wcd9375_dapm_widgets));
  1496. if (ret < 0) {
  1497. dev_err(codec->dev, "%s: Failed to add snd_ctls\n",
  1498. __func__);
  1499. goto err_hwdep;
  1500. }
  1501. ret = snd_soc_dapm_add_routes(dapm, wcd9375_audio_map,
  1502. ARRAY_SIZE(wcd9375_audio_map));
  1503. if (ret < 0) {
  1504. dev_err(codec->dev, "%s: Failed to add routes\n",
  1505. __func__);
  1506. goto err_hwdep;
  1507. }
  1508. snd_soc_dapm_sync(dapm);
  1509. }
  1510. wcd937x->version = WCD937X_VERSION_1_0;
  1511. return ret;
  1512. err_hwdep:
  1513. wcd937x->fw_data = NULL;
  1514. err:
  1515. return ret;
  1516. }
  1517. static int wcd937x_soc_codec_remove(struct snd_soc_codec *codec)
  1518. {
  1519. struct wcd937x_priv *wcd937x = snd_soc_codec_get_drvdata(codec);
  1520. if (!wcd937x)
  1521. return -EINVAL;
  1522. return 0;
  1523. }
  1524. static struct regmap *wcd937x_get_regmap(struct device *dev)
  1525. {
  1526. struct wcd937x_priv *wcd937x = dev_get_drvdata(dev);
  1527. return wcd937x->regmap;
  1528. }
  1529. static struct snd_soc_codec_driver soc_codec_dev_wcd937x = {
  1530. .probe = wcd937x_soc_codec_probe,
  1531. .remove = wcd937x_soc_codec_remove,
  1532. .get_regmap = wcd937x_get_regmap,
  1533. .component_driver = {
  1534. .controls = wcd937x_snd_controls,
  1535. .num_controls = ARRAY_SIZE(wcd937x_snd_controls),
  1536. .dapm_widgets = wcd937x_dapm_widgets,
  1537. .num_dapm_widgets = ARRAY_SIZE(wcd937x_dapm_widgets),
  1538. .dapm_routes = wcd937x_audio_map,
  1539. .num_dapm_routes = ARRAY_SIZE(wcd937x_audio_map),
  1540. },
  1541. };
  1542. int wcd937x_reset(struct device *dev)
  1543. {
  1544. struct wcd937x_priv *wcd937x = NULL;
  1545. int rc = 0;
  1546. int value = 0;
  1547. if (!dev)
  1548. return -ENODEV;
  1549. wcd937x = dev_get_drvdata(dev);
  1550. if (!wcd937x)
  1551. return -EINVAL;
  1552. if (!wcd937x->rst_np) {
  1553. dev_err(dev, "%s: reset gpio device node not specified\n",
  1554. __func__);
  1555. return -EINVAL;
  1556. }
  1557. value = msm_cdc_pinctrl_get_state(wcd937x->rst_np);
  1558. if (value > 0)
  1559. return 0;
  1560. rc = msm_cdc_pinctrl_select_sleep_state(wcd937x->rst_np);
  1561. if (rc) {
  1562. dev_err(dev, "%s: wcd sleep state request fail!\n",
  1563. __func__);
  1564. return rc;
  1565. }
  1566. /* 20ms sleep required after pulling the reset gpio to LOW */
  1567. usleep_range(20, 30);
  1568. rc = msm_cdc_pinctrl_select_active_state(wcd937x->rst_np);
  1569. if (rc) {
  1570. dev_err(dev, "%s: wcd active state request fail!\n",
  1571. __func__);
  1572. return rc;
  1573. }
  1574. /* 20ms sleep required after pulling the reset gpio to HIGH */
  1575. usleep_range(20, 30);
  1576. return rc;
  1577. }
  1578. static int wcd937x_read_of_property_u32(struct device *dev, const char *name,
  1579. u32 *val)
  1580. {
  1581. int rc = 0;
  1582. rc = of_property_read_u32(dev->of_node, name, val);
  1583. if (rc)
  1584. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  1585. __func__, name, dev->of_node->full_name);
  1586. return rc;
  1587. }
  1588. static void wcd937x_dt_parse_micbias_info(struct device *dev,
  1589. struct wcd937x_micbias_setting *mb)
  1590. {
  1591. u32 prop_val = 0;
  1592. int rc = 0;
  1593. /* MB1 */
  1594. if (of_find_property(dev->of_node, "qcom,cdc-micbias1-mv",
  1595. NULL)) {
  1596. rc = wcd937x_read_of_property_u32(dev,
  1597. "qcom,cdc-micbias1-mv",
  1598. &prop_val);
  1599. if (!rc)
  1600. mb->micb1_mv = prop_val;
  1601. } else {
  1602. dev_info(dev, "%s: Micbias1 DT property not found\n",
  1603. __func__);
  1604. }
  1605. /* MB2 */
  1606. if (of_find_property(dev->of_node, "qcom,cdc-micbias2-mv",
  1607. NULL)) {
  1608. rc = wcd937x_read_of_property_u32(dev,
  1609. "qcom,cdc-micbias2-mv",
  1610. &prop_val);
  1611. if (!rc)
  1612. mb->micb2_mv = prop_val;
  1613. } else {
  1614. dev_info(dev, "%s: Micbias2 DT property not found\n",
  1615. __func__);
  1616. }
  1617. /* MB3 */
  1618. if (of_find_property(dev->of_node, "qcom,cdc-micbias3-mv",
  1619. NULL)) {
  1620. rc = wcd937x_read_of_property_u32(dev,
  1621. "qcom,cdc-micbias3-mv",
  1622. &prop_val);
  1623. if (!rc)
  1624. mb->micb3_mv = prop_val;
  1625. } else {
  1626. dev_info(dev, "%s: Micbias3 DT property not found\n",
  1627. __func__);
  1628. }
  1629. }
  1630. struct wcd937x_pdata *wcd937x_populate_dt_data(struct device *dev)
  1631. {
  1632. struct wcd937x_pdata *pdata = NULL;
  1633. pdata = devm_kzalloc(dev, sizeof(struct wcd937x_pdata),
  1634. GFP_KERNEL);
  1635. if (!pdata)
  1636. return NULL;
  1637. pdata->rst_np = of_parse_phandle(dev->of_node,
  1638. "qcom,wcd-rst-gpio-node", 0);
  1639. if (!pdata->rst_np) {
  1640. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  1641. __func__, "qcom,wcd-rst-gpio-node",
  1642. dev->of_node->full_name);
  1643. return NULL;
  1644. }
  1645. /* Parse power supplies */
  1646. msm_cdc_get_power_supplies(dev, &pdata->regulator,
  1647. &pdata->num_supplies);
  1648. if (!pdata->regulator || (pdata->num_supplies <= 0)) {
  1649. dev_err(dev, "%s: no power supplies defined for codec\n",
  1650. __func__);
  1651. return NULL;
  1652. }
  1653. pdata->rx_slave = of_parse_phandle(dev->of_node, "qcom,rx-slave", 0);
  1654. pdata->tx_slave = of_parse_phandle(dev->of_node, "qcom,tx-slave", 0);
  1655. wcd937x_dt_parse_micbias_info(dev, &pdata->micbias);
  1656. return pdata;
  1657. }
  1658. static int wcd937x_bind(struct device *dev)
  1659. {
  1660. int ret = 0, i = 0;
  1661. struct wcd937x_priv *wcd937x = NULL;
  1662. struct wcd937x_pdata *pdata = NULL;
  1663. wcd937x = devm_kzalloc(dev, sizeof(struct wcd937x_priv), GFP_KERNEL);
  1664. if (!wcd937x)
  1665. return -ENOMEM;
  1666. dev_set_drvdata(dev, wcd937x);
  1667. pdata = wcd937x_populate_dt_data(dev);
  1668. if (!pdata) {
  1669. dev_err(dev, "%s: Fail to obtain platform data\n", __func__);
  1670. return -EINVAL;
  1671. }
  1672. wcd937x->dev = dev;
  1673. wcd937x->dev->platform_data = pdata;
  1674. wcd937x->rst_np = pdata->rst_np;
  1675. ret = msm_cdc_init_supplies(dev, &wcd937x->supplies,
  1676. pdata->regulator, pdata->num_supplies);
  1677. if (!wcd937x->supplies) {
  1678. dev_err(dev, "%s: Cannot init wcd supplies\n",
  1679. __func__);
  1680. return ret;
  1681. }
  1682. ret = msm_cdc_enable_static_supplies(dev, wcd937x->supplies,
  1683. pdata->regulator,
  1684. pdata->num_supplies);
  1685. if (ret) {
  1686. dev_err(dev, "%s: wcd static supply enable failed!\n",
  1687. __func__);
  1688. return ret;
  1689. }
  1690. wcd937x_reset(dev);
  1691. /*
  1692. * Add 5msec delay to provide sufficient time for
  1693. * soundwire auto enumeration of slave devices as
  1694. * as per HW requirement.
  1695. */
  1696. usleep_range(5000, 5010);
  1697. ret = component_bind_all(dev, wcd937x);
  1698. if (ret) {
  1699. dev_err(dev, "%s: Slave bind failed, ret = %d\n",
  1700. __func__, ret);
  1701. return ret;
  1702. }
  1703. ret = wcd937x_parse_port_mapping(dev, "qcom,rx_swr_ch_map", CODEC_RX);
  1704. ret |= wcd937x_parse_port_mapping(dev, "qcom,tx_swr_ch_map", CODEC_TX);
  1705. if (ret) {
  1706. dev_err(dev, "Failed to read port mapping\n");
  1707. goto err;
  1708. }
  1709. wcd937x->rx_swr_dev = get_matching_swr_slave_device(pdata->rx_slave);
  1710. if (!wcd937x->rx_swr_dev) {
  1711. dev_err(dev, "%s: Could not find RX swr slave device\n",
  1712. __func__);
  1713. ret = -ENODEV;
  1714. goto err;
  1715. }
  1716. wcd937x->tx_swr_dev = get_matching_swr_slave_device(pdata->tx_slave);
  1717. if (!wcd937x->tx_swr_dev) {
  1718. dev_err(dev, "%s: Could not find TX swr slave device\n",
  1719. __func__);
  1720. ret = -ENODEV;
  1721. goto err;
  1722. }
  1723. wcd937x->regmap = devm_regmap_init_swr(wcd937x->tx_swr_dev,
  1724. &wcd937x_regmap_config);
  1725. if (!wcd937x->regmap) {
  1726. dev_err(dev, "%s: Regmap init failed\n",
  1727. __func__);
  1728. goto err;
  1729. }
  1730. /* Set all interupts as edge triggered */
  1731. for (i = 0; i < wcd937x_regmap_irq_chip.num_regs; i++)
  1732. regmap_write(wcd937x->regmap,
  1733. (WCD937X_DIGITAL_INTR_LEVEL_0 + i), 0);
  1734. wcd937x_regmap_irq_chip.irq_drv_data = wcd937x;
  1735. wcd937x->irq_info.wcd_regmap_irq_chip = &wcd937x_regmap_irq_chip;
  1736. wcd937x->irq_info.codec_name = "WCD937X";
  1737. wcd937x->irq_info.regmap = wcd937x->regmap;
  1738. wcd937x->irq_info.dev = dev;
  1739. ret = wcd_irq_init(&wcd937x->irq_info, &wcd937x->virq);
  1740. if (ret) {
  1741. dev_err(dev, "%s: IRQ init failed: %d\n",
  1742. __func__, ret);
  1743. goto err;
  1744. }
  1745. wcd937x->tx_swr_dev->slave_irq = wcd937x->virq;
  1746. ret = snd_soc_register_codec(dev, &soc_codec_dev_wcd937x,
  1747. NULL, 0);
  1748. if (ret) {
  1749. dev_err(dev, "%s: Codec registration failed\n",
  1750. __func__);
  1751. goto err_irq;
  1752. }
  1753. return ret;
  1754. err_irq:
  1755. wcd_irq_exit(&wcd937x->irq_info, wcd937x->virq);
  1756. err:
  1757. component_unbind_all(dev, wcd937x);
  1758. return ret;
  1759. }
  1760. static void wcd937x_unbind(struct device *dev)
  1761. {
  1762. struct wcd937x_priv *wcd937x = dev_get_drvdata(dev);
  1763. wcd_irq_exit(&wcd937x->irq_info, wcd937x->virq);
  1764. snd_soc_unregister_codec(dev);
  1765. component_unbind_all(dev, wcd937x);
  1766. }
  1767. static const struct of_device_id wcd937x_dt_match[] = {
  1768. { .compatible = "qcom,wcd937x-codec" },
  1769. {}
  1770. };
  1771. static const struct component_master_ops wcd937x_comp_ops = {
  1772. .bind = wcd937x_bind,
  1773. .unbind = wcd937x_unbind,
  1774. };
  1775. static int wcd937x_compare_of(struct device *dev, void *data)
  1776. {
  1777. return dev->of_node == data;
  1778. }
  1779. static void wcd937x_release_of(struct device *dev, void *data)
  1780. {
  1781. of_node_put(data);
  1782. }
  1783. static int wcd937x_add_slave_components(struct device *dev,
  1784. struct component_match **matchptr)
  1785. {
  1786. struct device_node *np, *rx_node, *tx_node;
  1787. np = dev->of_node;
  1788. rx_node = of_parse_phandle(np, "qcom,rx-slave", 0);
  1789. if (!rx_node) {
  1790. dev_err(dev, "%s: Rx-slave node not defined\n", __func__);
  1791. return -ENODEV;
  1792. }
  1793. of_node_get(rx_node);
  1794. component_match_add_release(dev, matchptr,
  1795. wcd937x_release_of,
  1796. wcd937x_compare_of,
  1797. rx_node);
  1798. tx_node = of_parse_phandle(np, "qcom,tx-slave", 0);
  1799. if (!tx_node) {
  1800. dev_err(dev, "%s: Tx-slave node not defined\n", __func__);
  1801. return -ENODEV;
  1802. }
  1803. of_node_get(tx_node);
  1804. component_match_add_release(dev, matchptr,
  1805. wcd937x_release_of,
  1806. wcd937x_compare_of,
  1807. tx_node);
  1808. return 0;
  1809. }
  1810. static int wcd937x_probe(struct platform_device *pdev)
  1811. {
  1812. struct component_match *match = NULL;
  1813. int ret;
  1814. ret = wcd937x_add_slave_components(&pdev->dev, &match);
  1815. if (ret)
  1816. return ret;
  1817. return component_master_add_with_match(&pdev->dev,
  1818. &wcd937x_comp_ops, match);
  1819. }
  1820. static int wcd937x_remove(struct platform_device *pdev)
  1821. {
  1822. component_master_del(&pdev->dev, &wcd937x_comp_ops);
  1823. return 0;
  1824. }
  1825. static struct platform_driver wcd937x_codec_driver = {
  1826. .probe = wcd937x_probe,
  1827. .remove = wcd937x_remove,
  1828. .driver = {
  1829. .name = "wcd937x_codec",
  1830. .owner = THIS_MODULE,
  1831. .of_match_table = of_match_ptr(wcd937x_dt_match),
  1832. },
  1833. };
  1834. module_platform_driver(wcd937x_codec_driver);
  1835. MODULE_DESCRIPTION("WCD937X Codec driver");
  1836. MODULE_LICENSE("GPL v2");