rouleur.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/device.h>
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/component.h>
  12. #include <linux/regmap.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/of_platform.h>
  15. #include <sound/soc.h>
  16. #include <sound/tlv.h>
  17. #include <soc/soundwire.h>
  18. #include <sound/soc.h>
  19. #include <sound/soc-dapm.h>
  20. #include "internal.h"
  21. #include "rouleur.h"
  22. #include <asoc/wcdcal-hwdep.h>
  23. #include "rouleur-registers.h"
  24. #include "pm2250-spmi.h"
  25. #include <asoc/msm-cdc-pinctrl.h>
  26. #include <dt-bindings/sound/audio-codec-port-types.h>
  27. #include <asoc/msm-cdc-supply.h>
  28. #define DRV_NAME "rouleur_codec"
  29. #define NUM_SWRS_DT_PARAMS 5
  30. #define ROULEUR_VERSION_1_0 1
  31. #define ROULEUR_VERSION_ENTRY_SIZE 32
  32. #define NUM_ATTEMPTS 5
  33. enum {
  34. CODEC_TX = 0,
  35. CODEC_RX,
  36. };
  37. enum {
  38. ALLOW_VPOS_DISABLE,
  39. HPH_COMP_DELAY,
  40. HPH_PA_DELAY,
  41. AMIC2_BCS_ENABLE,
  42. };
  43. /* TODO: Check on the step values */
  44. static const DECLARE_TLV_DB_SCALE(line_gain, 0, 7, 1);
  45. static const DECLARE_TLV_DB_SCALE(analog_gain, 0, 25, 1);
  46. static int rouleur_handle_post_irq(void *data);
  47. static int rouleur_reset(struct device *dev, int val);
  48. static const struct regmap_irq ROULEUR_IRQs[ROULEUR_NUM_IRQS] = {
  49. REGMAP_IRQ_REG(ROULEUR_IRQ_MBHC_BUTTON_PRESS_DET, 0, 0x01),
  50. REGMAP_IRQ_REG(ROULEUR_IRQ_MBHC_BUTTON_RELEASE_DET, 0, 0x02),
  51. REGMAP_IRQ_REG(ROULEUR_IRQ_MBHC_ELECT_INS_REM_DET, 0, 0x04),
  52. REGMAP_IRQ_REG(ROULEUR_IRQ_MBHC_ELECT_INS_REM_LEG_DET, 0, 0x08),
  53. REGMAP_IRQ_REG(ROULEUR_IRQ_MBHC_SW_DET, 0, 0x10),
  54. REGMAP_IRQ_REG(ROULEUR_IRQ_HPHR_OCP_INT, 0, 0x20),
  55. REGMAP_IRQ_REG(ROULEUR_IRQ_HPHR_CNP_INT, 0, 0x40),
  56. REGMAP_IRQ_REG(ROULEUR_IRQ_HPHL_OCP_INT, 0, 0x80),
  57. REGMAP_IRQ_REG(ROULEUR_IRQ_HPHL_CNP_INT, 1, 0x01),
  58. REGMAP_IRQ_REG(ROULEUR_IRQ_EAR_CNP_INT, 1, 0x02),
  59. REGMAP_IRQ_REG(ROULEUR_IRQ_EAR_OCP_INT, 1, 0x04),
  60. REGMAP_IRQ_REG(ROULEUR_IRQ_LO_CNP_INT, 1, 0x08),
  61. REGMAP_IRQ_REG(ROULEUR_IRQ_LO_OCP_INT, 1, 0x10),
  62. REGMAP_IRQ_REG(ROULEUR_IRQ_HPHL_PDM_WD_INT, 1, 0x20),
  63. REGMAP_IRQ_REG(ROULEUR_IRQ_HPHR_PDM_WD_INT, 1, 0x40),
  64. REGMAP_IRQ_REG(ROULEUR_IRQ_HPHL_SURGE_DET_INT, 2, 0x04),
  65. REGMAP_IRQ_REG(ROULEUR_IRQ_HPHR_SURGE_DET_INT, 2, 0x08),
  66. };
  67. static struct regmap_irq_chip rouleur_regmap_irq_chip = {
  68. .name = "rouleur",
  69. .irqs = ROULEUR_IRQs,
  70. .num_irqs = ARRAY_SIZE(ROULEUR_IRQs),
  71. .num_regs = 3,
  72. .status_base = ROULEUR_DIG_SWR_INTR_STATUS_0,
  73. .mask_base = ROULEUR_DIG_SWR_INTR_MASK_0,
  74. .ack_base = ROULEUR_DIG_SWR_INTR_CLEAR_0,
  75. .use_ack = 1,
  76. .type_base = ROULEUR_DIG_SWR_INTR_LEVEL_0,
  77. .runtime_pm = false,
  78. .handle_post_irq = rouleur_handle_post_irq,
  79. .irq_drv_data = NULL,
  80. };
  81. static int rouleur_handle_post_irq(void *data)
  82. {
  83. struct rouleur_priv *rouleur = data;
  84. u32 status1 = 0, status2 = 0, status3 = 0;
  85. regmap_read(rouleur->regmap, ROULEUR_DIG_SWR_INTR_STATUS_0, &status1);
  86. regmap_read(rouleur->regmap, ROULEUR_DIG_SWR_INTR_STATUS_1, &status2);
  87. regmap_read(rouleur->regmap, ROULEUR_DIG_SWR_INTR_STATUS_2, &status3);
  88. rouleur->tx_swr_dev->slave_irq_pending =
  89. ((status1 || status2 || status3) ? true : false);
  90. return IRQ_HANDLED;
  91. }
  92. static int rouleur_init_reg(struct snd_soc_component *component)
  93. {
  94. /* Enable surge protection */
  95. snd_soc_component_update_bits(component, ROULEUR_ANA_SURGE_EN,
  96. 0xC0, 0xC0);
  97. /* Disable mic bias pull down */
  98. snd_soc_component_update_bits(component, ROULEUR_ANA_MICBIAS_MICB_1_2_EN,
  99. 0x01, 0x00);
  100. return 0;
  101. }
  102. static int rouleur_set_port_params(struct snd_soc_component *component,
  103. u8 slv_prt_type, u8 *port_id, u8 *num_ch,
  104. u8 *ch_mask, u32 *ch_rate,
  105. u8 *port_type, u8 path)
  106. {
  107. int i, j;
  108. u8 num_ports = 0;
  109. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT] = NULL;
  110. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  111. switch (path) {
  112. case CODEC_RX:
  113. map = &rouleur->rx_port_mapping;
  114. num_ports = rouleur->num_rx_ports;
  115. break;
  116. case CODEC_TX:
  117. map = &rouleur->tx_port_mapping;
  118. num_ports = rouleur->num_tx_ports;
  119. break;
  120. default:
  121. dev_err(component->dev, "%s Invalid path: %d\n",
  122. __func__, path);
  123. return -EINVAL;
  124. }
  125. for (i = 0; i <= num_ports; i++) {
  126. for (j = 0; j < MAX_CH_PER_PORT; j++) {
  127. if ((*map)[i][j].slave_port_type == slv_prt_type)
  128. goto found;
  129. }
  130. }
  131. dev_err(component->dev, "%s Failed to find slave port for type %u\n",
  132. __func__, slv_prt_type);
  133. return -EINVAL;
  134. found:
  135. *port_id = i;
  136. *num_ch = (*map)[i][j].num_ch;
  137. *ch_mask = (*map)[i][j].ch_mask;
  138. *ch_rate = (*map)[i][j].ch_rate;
  139. *port_type = (*map)[i][j].master_port_type;
  140. return 0;
  141. }
  142. static int rouleur_parse_port_mapping(struct device *dev,
  143. char *prop, u8 path)
  144. {
  145. u32 *dt_array, map_size, map_length;
  146. u32 port_num = 0, ch_mask, ch_rate, old_port_num = 0;
  147. u32 slave_port_type, master_port_type;
  148. u32 i, ch_iter = 0;
  149. int ret = 0;
  150. u8 *num_ports = NULL;
  151. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT] = NULL;
  152. struct rouleur_priv *rouleur = dev_get_drvdata(dev);
  153. switch (path) {
  154. case CODEC_RX:
  155. map = &rouleur->rx_port_mapping;
  156. num_ports = &rouleur->num_rx_ports;
  157. break;
  158. case CODEC_TX:
  159. map = &rouleur->tx_port_mapping;
  160. num_ports = &rouleur->num_tx_ports;
  161. break;
  162. default:
  163. dev_err(dev, "%s Invalid path: %d\n",
  164. __func__, path);
  165. return -EINVAL;
  166. }
  167. if (!of_find_property(dev->of_node, prop,
  168. &map_size)) {
  169. dev_err(dev, "missing port mapping prop %s\n", prop);
  170. ret = -EINVAL;
  171. goto err;
  172. }
  173. map_length = map_size / (NUM_SWRS_DT_PARAMS * sizeof(u32));
  174. dt_array = kzalloc(map_size, GFP_KERNEL);
  175. if (!dt_array) {
  176. ret = -ENOMEM;
  177. goto err;
  178. }
  179. ret = of_property_read_u32_array(dev->of_node, prop, dt_array,
  180. NUM_SWRS_DT_PARAMS * map_length);
  181. if (ret) {
  182. dev_err(dev, "%s: Failed to read port mapping from prop %s\n",
  183. __func__, prop);
  184. ret = -EINVAL;
  185. goto err_pdata_fail;
  186. }
  187. for (i = 0; i < map_length; i++) {
  188. port_num = dt_array[NUM_SWRS_DT_PARAMS * i];
  189. slave_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 1];
  190. ch_mask = dt_array[NUM_SWRS_DT_PARAMS * i + 2];
  191. ch_rate = dt_array[NUM_SWRS_DT_PARAMS * i + 3];
  192. master_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 4];
  193. if (port_num != old_port_num)
  194. ch_iter = 0;
  195. (*map)[port_num][ch_iter].slave_port_type = slave_port_type;
  196. (*map)[port_num][ch_iter].ch_mask = ch_mask;
  197. (*map)[port_num][ch_iter].master_port_type = master_port_type;
  198. (*map)[port_num][ch_iter].num_ch = __sw_hweight8(ch_mask);
  199. (*map)[port_num][ch_iter++].ch_rate = ch_rate;
  200. old_port_num = port_num;
  201. }
  202. *num_ports = port_num;
  203. err_pdata_fail:
  204. kfree(dt_array);
  205. err:
  206. return ret;
  207. }
  208. static int rouleur_tx_connect_port(struct snd_soc_component *component,
  209. u8 slv_port_type, u8 enable)
  210. {
  211. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  212. u8 port_id;
  213. u8 num_ch;
  214. u8 ch_mask;
  215. u32 ch_rate;
  216. u8 port_type;
  217. u8 num_port = 1;
  218. int ret = 0;
  219. ret = rouleur_set_port_params(component, slv_port_type, &port_id,
  220. &num_ch, &ch_mask, &ch_rate,
  221. &port_type, CODEC_TX);
  222. if (ret) {
  223. dev_err(rouleur->dev, "%s:Failed to set port params: %d\n",
  224. __func__, ret);
  225. return ret;
  226. }
  227. if (enable)
  228. ret = swr_connect_port(rouleur->tx_swr_dev, &port_id,
  229. num_port, &ch_mask, &ch_rate,
  230. &num_ch, &port_type);
  231. else
  232. ret = swr_disconnect_port(rouleur->tx_swr_dev, &port_id,
  233. num_port, &ch_mask, &port_type);
  234. return ret;
  235. }
  236. static int rouleur_rx_connect_port(struct snd_soc_component *component,
  237. u8 slv_port_type, u8 enable)
  238. {
  239. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  240. u8 port_id;
  241. u8 num_ch;
  242. u8 ch_mask;
  243. u32 ch_rate;
  244. u8 port_type;
  245. u8 num_port = 1;
  246. int ret = 0;
  247. ret = rouleur_set_port_params(component, slv_port_type, &port_id,
  248. &num_ch, &ch_mask, &ch_rate,
  249. &port_type, CODEC_RX);
  250. if (ret) {
  251. dev_err(rouleur->dev, "%s:Failed to set port params: %d\n",
  252. __func__, ret);
  253. return ret;
  254. }
  255. if (enable)
  256. ret = swr_connect_port(rouleur->rx_swr_dev, &port_id,
  257. num_port, &ch_mask, &ch_rate,
  258. &num_ch, &port_type);
  259. else
  260. ret = swr_disconnect_port(rouleur->rx_swr_dev, &port_id,
  261. num_port, &ch_mask, &port_type);
  262. return ret;
  263. }
  264. static int rouleur_global_mbias_enable(struct snd_soc_component *component)
  265. {
  266. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  267. mutex_lock(&rouleur->main_bias_lock);
  268. if (rouleur->mbias_cnt == 0) {
  269. snd_soc_component_update_bits(component,
  270. ROULEUR_ANA_MBIAS_EN, 0x20, 0x20);
  271. snd_soc_component_update_bits(component,
  272. ROULEUR_ANA_MBIAS_EN, 0x10, 0x10);
  273. usleep_range(1000, 1100);
  274. }
  275. rouleur->mbias_cnt++;
  276. mutex_unlock(&rouleur->main_bias_lock);
  277. return 0;
  278. }
  279. static int rouleur_global_mbias_disable(struct snd_soc_component *component)
  280. {
  281. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  282. mutex_lock(&rouleur->main_bias_lock);
  283. if (rouleur->mbias_cnt == 0) {
  284. dev_dbg(rouleur->dev, "%s:mbias already disabled\n", __func__);
  285. mutex_unlock(&rouleur->main_bias_lock);
  286. return 0;
  287. }
  288. rouleur->mbias_cnt--;
  289. if (rouleur->mbias_cnt == 0) {
  290. snd_soc_component_update_bits(component,
  291. ROULEUR_ANA_MBIAS_EN, 0x10, 0x00);
  292. snd_soc_component_update_bits(component,
  293. ROULEUR_ANA_MBIAS_EN, 0x20, 0x00);
  294. }
  295. mutex_unlock(&rouleur->main_bias_lock);
  296. return 0;
  297. }
  298. static int rouleur_rx_clk_enable(struct snd_soc_component *component)
  299. {
  300. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  301. mutex_lock(&rouleur->rx_clk_lock);
  302. if (rouleur->rx_clk_cnt == 0) {
  303. snd_soc_component_update_bits(component,
  304. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL, 0x10, 0x10);
  305. snd_soc_component_update_bits(component,
  306. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL, 0x20, 0x20);
  307. usleep_range(5000, 5100);
  308. rouleur_global_mbias_enable(component);
  309. snd_soc_component_update_bits(component,
  310. ROULEUR_ANA_HPHPA_FSM_CLK, 0x11, 0x11);
  311. snd_soc_component_update_bits(component,
  312. ROULEUR_ANA_HPHPA_FSM_CLK, 0x80, 0x80);
  313. snd_soc_component_update_bits(component,
  314. ROULEUR_ANA_NCP_EN, 0x01, 0x01);
  315. usleep_range(500, 510);
  316. }
  317. rouleur->rx_clk_cnt++;
  318. mutex_unlock(&rouleur->rx_clk_lock);
  319. return 0;
  320. }
  321. static int rouleur_rx_clk_disable(struct snd_soc_component *component)
  322. {
  323. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  324. mutex_lock(&rouleur->rx_clk_lock);
  325. if (rouleur->rx_clk_cnt == 0) {
  326. dev_dbg(rouleur->dev, "%s:clk already disabled\n", __func__);
  327. mutex_unlock(&rouleur->rx_clk_lock);
  328. return 0;
  329. }
  330. rouleur->rx_clk_cnt--;
  331. if (rouleur->rx_clk_cnt == 0) {
  332. snd_soc_component_update_bits(component,
  333. ROULEUR_ANA_HPHPA_FSM_CLK, 0x80, 0x00);
  334. snd_soc_component_update_bits(component,
  335. ROULEUR_ANA_HPHPA_FSM_CLK, 0x11, 0x00);
  336. snd_soc_component_update_bits(component,
  337. ROULEUR_ANA_NCP_EN, 0x01, 0x00);
  338. rouleur_global_mbias_disable(component);
  339. snd_soc_component_update_bits(component,
  340. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL, 0x20, 0x00);
  341. snd_soc_component_update_bits(component,
  342. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL, 0x10, 0x00);
  343. }
  344. mutex_unlock(&rouleur->rx_clk_lock);
  345. return 0;
  346. }
  347. /*
  348. * rouleur_soc_get_mbhc: get rouleur_mbhc handle of corresponding component
  349. * @component: handle to snd_soc_component *
  350. *
  351. * return rouleur_mbhc handle or error code in case of failure
  352. */
  353. struct rouleur_mbhc *rouleur_soc_get_mbhc(struct snd_soc_component *component)
  354. {
  355. struct rouleur_priv *rouleur;
  356. if (!component) {
  357. pr_err("%s: Invalid params, NULL component\n", __func__);
  358. return NULL;
  359. }
  360. rouleur = snd_soc_component_get_drvdata(component);
  361. if (!rouleur) {
  362. pr_err("%s: Invalid params, NULL tavil\n", __func__);
  363. return NULL;
  364. }
  365. return rouleur->mbhc;
  366. }
  367. EXPORT_SYMBOL(rouleur_soc_get_mbhc);
  368. static int rouleur_codec_hphl_dac_event(struct snd_soc_dapm_widget *w,
  369. struct snd_kcontrol *kcontrol,
  370. int event)
  371. {
  372. struct snd_soc_component *component =
  373. snd_soc_dapm_to_component(w->dapm);
  374. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  375. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  376. w->name, event);
  377. switch (event) {
  378. case SND_SOC_DAPM_PRE_PMU:
  379. rouleur_rx_clk_enable(component);
  380. snd_soc_component_update_bits(component,
  381. ROULEUR_ANA_HPHPA_CNP_CTL_1,
  382. 0x02, 0x02);
  383. snd_soc_component_update_bits(component,
  384. ROULEUR_SWR_HPHPA_HD2,
  385. 0x38, 0x38);
  386. set_bit(HPH_COMP_DELAY, &rouleur->status_mask);
  387. break;
  388. case SND_SOC_DAPM_POST_PMU:
  389. if (rouleur->comp1_enable) {
  390. snd_soc_component_update_bits(component,
  391. ROULEUR_DIG_SWR_CDC_COMP_CTL_0,
  392. 0x02, 0x02);
  393. if (rouleur->comp2_enable)
  394. snd_soc_component_update_bits(component,
  395. ROULEUR_DIG_SWR_CDC_COMP_CTL_0,
  396. 0x01, 0x01);
  397. /*
  398. * 5ms sleep is required after COMP is enabled as per
  399. * HW requirement
  400. */
  401. if (test_bit(HPH_COMP_DELAY, &rouleur->status_mask)) {
  402. usleep_range(5000, 5100);
  403. clear_bit(HPH_COMP_DELAY,
  404. &rouleur->status_mask);
  405. }
  406. } else {
  407. snd_soc_component_update_bits(component,
  408. ROULEUR_DIG_SWR_CDC_COMP_CTL_0,
  409. 0x02, 0x00);
  410. }
  411. snd_soc_component_update_bits(component,
  412. ROULEUR_DIG_SWR_CDC_RX0_CTL,
  413. 0x7C, 0x7C);
  414. snd_soc_component_update_bits(component,
  415. ROULEUR_DIG_SWR_CDC_RX_GAIN_CTL,
  416. 0x04, 0x04);
  417. snd_soc_component_update_bits(component,
  418. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL, 0x01, 0x01);
  419. break;
  420. case SND_SOC_DAPM_POST_PMD:
  421. snd_soc_component_update_bits(component,
  422. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL,
  423. 0x01, 0x00);
  424. break;
  425. }
  426. return 0;
  427. }
  428. static int rouleur_codec_hphr_dac_event(struct snd_soc_dapm_widget *w,
  429. struct snd_kcontrol *kcontrol,
  430. int event)
  431. {
  432. struct snd_soc_component *component =
  433. snd_soc_dapm_to_component(w->dapm);
  434. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  435. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  436. w->name, event);
  437. switch (event) {
  438. case SND_SOC_DAPM_PRE_PMU:
  439. rouleur_rx_clk_enable(component);
  440. snd_soc_component_update_bits(component,
  441. ROULEUR_ANA_HPHPA_CNP_CTL_1,
  442. 0x02, 0x02);
  443. snd_soc_component_update_bits(component,
  444. ROULEUR_SWR_HPHPA_HD2,
  445. 0x07, 0x07);
  446. set_bit(HPH_COMP_DELAY, &rouleur->status_mask);
  447. break;
  448. case SND_SOC_DAPM_POST_PMU:
  449. if (rouleur->comp2_enable) {
  450. snd_soc_component_update_bits(component,
  451. ROULEUR_DIG_SWR_CDC_COMP_CTL_0,
  452. 0x01, 0x01);
  453. if (rouleur->comp1_enable)
  454. snd_soc_component_update_bits(component,
  455. ROULEUR_DIG_SWR_CDC_COMP_CTL_0,
  456. 0x02, 0x02);
  457. /*
  458. * 5ms sleep is required after COMP is enabled as per
  459. * HW requirement
  460. */
  461. if (test_bit(HPH_COMP_DELAY, &rouleur->status_mask)) {
  462. usleep_range(5000, 5100);
  463. clear_bit(HPH_COMP_DELAY,
  464. &rouleur->status_mask);
  465. }
  466. } else {
  467. snd_soc_component_update_bits(component,
  468. ROULEUR_DIG_SWR_CDC_COMP_CTL_0,
  469. 0x01, 0x00);
  470. }
  471. snd_soc_component_update_bits(component,
  472. ROULEUR_DIG_SWR_CDC_RX1_CTL,
  473. 0x7C, 0x7C);
  474. snd_soc_component_update_bits(component,
  475. ROULEUR_DIG_SWR_CDC_RX_GAIN_CTL,
  476. 0x08, 0x08);
  477. snd_soc_component_update_bits(component,
  478. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL, 0x02, 0x02);
  479. break;
  480. case SND_SOC_DAPM_POST_PMD:
  481. snd_soc_component_update_bits(component,
  482. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL, 0x02, 0x00);
  483. break;
  484. }
  485. return 0;
  486. }
  487. static int rouleur_codec_ear_lo_dac_event(struct snd_soc_dapm_widget *w,
  488. struct snd_kcontrol *kcontrol,
  489. int event)
  490. {
  491. struct snd_soc_component *component =
  492. snd_soc_dapm_to_component(w->dapm);
  493. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  494. w->name, event);
  495. switch (event) {
  496. case SND_SOC_DAPM_PRE_PMU:
  497. rouleur_rx_clk_enable(component);
  498. snd_soc_component_update_bits(component,
  499. ROULEUR_DIG_SWR_CDC_RX0_CTL,
  500. 0x7C, 0x7C);
  501. snd_soc_component_update_bits(component,
  502. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL,
  503. 0x01, 0x01);
  504. snd_soc_component_update_bits(component,
  505. ROULEUR_DIG_SWR_CDC_RX_GAIN_CTL,
  506. 0x04, 0x04);
  507. break;
  508. case SND_SOC_DAPM_POST_PMD:
  509. snd_soc_component_update_bits(component,
  510. ROULEUR_DIG_SWR_CDC_RX_CLK_CTL,
  511. 0x01, 0x00);
  512. break;
  513. };
  514. return 0;
  515. }
  516. static int rouleur_codec_enable_hphr_pa(struct snd_soc_dapm_widget *w,
  517. struct snd_kcontrol *kcontrol,
  518. int event)
  519. {
  520. struct snd_soc_component *component =
  521. snd_soc_dapm_to_component(w->dapm);
  522. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  523. int ret = 0;
  524. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  525. w->name, event);
  526. switch (event) {
  527. case SND_SOC_DAPM_PRE_PMU:
  528. ret = swr_slvdev_datapath_control(rouleur->rx_swr_dev,
  529. rouleur->rx_swr_dev->dev_num,
  530. true);
  531. set_bit(HPH_PA_DELAY, &rouleur->status_mask);
  532. usleep_range(5000, 5100);
  533. snd_soc_component_update_bits(component,
  534. ROULEUR_DIG_SWR_PDM_WD_CTL1,
  535. 0x03, 0x03);
  536. break;
  537. case SND_SOC_DAPM_POST_PMU:
  538. /*
  539. * 5ms sleep is required after PA is enabled as per
  540. * HW requirement.
  541. */
  542. if (test_bit(HPH_PA_DELAY, &rouleur->status_mask)) {
  543. usleep_range(5000, 5100);
  544. clear_bit(HPH_PA_DELAY, &rouleur->status_mask);
  545. }
  546. if (rouleur->update_wcd_event)
  547. rouleur->update_wcd_event(rouleur->handle,
  548. WCD_BOLERO_EVT_RX_MUTE,
  549. (WCD_RX2 << 0x10));
  550. wcd_enable_irq(&rouleur->irq_info,
  551. ROULEUR_IRQ_HPHR_PDM_WD_INT);
  552. break;
  553. case SND_SOC_DAPM_PRE_PMD:
  554. wcd_disable_irq(&rouleur->irq_info,
  555. ROULEUR_IRQ_HPHR_PDM_WD_INT);
  556. if (rouleur->update_wcd_event)
  557. rouleur->update_wcd_event(rouleur->handle,
  558. WCD_BOLERO_EVT_RX_MUTE,
  559. (WCD_RX2 << 0x10 | 0x1));
  560. blocking_notifier_call_chain(&rouleur->mbhc->notifier,
  561. WCD_EVENT_PRE_HPHR_PA_OFF,
  562. &rouleur->mbhc->wcd_mbhc);
  563. set_bit(HPH_PA_DELAY, &rouleur->status_mask);
  564. break;
  565. case SND_SOC_DAPM_POST_PMD:
  566. /*
  567. * 7ms sleep is required after PA is disabled as per
  568. * HW requirement. If compander is disabled, then
  569. * 20ms delay is required.
  570. */
  571. if (test_bit(HPH_PA_DELAY, &rouleur->status_mask)) {
  572. usleep_range(5000, 5100);
  573. clear_bit(HPH_PA_DELAY, &rouleur->status_mask);
  574. }
  575. blocking_notifier_call_chain(&rouleur->mbhc->notifier,
  576. WCD_EVENT_POST_HPHR_PA_OFF,
  577. &rouleur->mbhc->wcd_mbhc);
  578. snd_soc_component_update_bits(component,
  579. ROULEUR_DIG_SWR_PDM_WD_CTL1,
  580. 0x03, 0x00);
  581. break;
  582. };
  583. return ret;
  584. }
  585. static int rouleur_codec_enable_hphl_pa(struct snd_soc_dapm_widget *w,
  586. struct snd_kcontrol *kcontrol,
  587. int event)
  588. {
  589. struct snd_soc_component *component =
  590. snd_soc_dapm_to_component(w->dapm);
  591. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  592. int ret = 0;
  593. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  594. w->name, event);
  595. switch (event) {
  596. case SND_SOC_DAPM_PRE_PMU:
  597. ret = swr_slvdev_datapath_control(rouleur->rx_swr_dev,
  598. rouleur->rx_swr_dev->dev_num,
  599. true);
  600. set_bit(HPH_PA_DELAY, &rouleur->status_mask);
  601. usleep_range(5000, 5100);
  602. snd_soc_component_update_bits(component,
  603. ROULEUR_DIG_SWR_PDM_WD_CTL0,
  604. 0x03, 0x03);
  605. break;
  606. case SND_SOC_DAPM_POST_PMU:
  607. /*
  608. * 5ms sleep is required after PA is enabled as per
  609. * HW requirement.
  610. */
  611. if (test_bit(HPH_PA_DELAY, &rouleur->status_mask)) {
  612. usleep_range(5000, 5100);
  613. clear_bit(HPH_PA_DELAY, &rouleur->status_mask);
  614. }
  615. if (rouleur->update_wcd_event)
  616. rouleur->update_wcd_event(rouleur->handle,
  617. WCD_BOLERO_EVT_RX_MUTE,
  618. (WCD_RX1 << 0x10));
  619. wcd_enable_irq(&rouleur->irq_info,
  620. ROULEUR_IRQ_HPHL_PDM_WD_INT);
  621. break;
  622. case SND_SOC_DAPM_PRE_PMD:
  623. wcd_disable_irq(&rouleur->irq_info,
  624. ROULEUR_IRQ_HPHL_PDM_WD_INT);
  625. if (rouleur->update_wcd_event)
  626. rouleur->update_wcd_event(rouleur->handle,
  627. WCD_BOLERO_EVT_RX_MUTE,
  628. (WCD_RX1 << 0x10 | 0x1));
  629. blocking_notifier_call_chain(&rouleur->mbhc->notifier,
  630. WCD_EVENT_PRE_HPHL_PA_OFF,
  631. &rouleur->mbhc->wcd_mbhc);
  632. set_bit(HPH_PA_DELAY, &rouleur->status_mask);
  633. break;
  634. case SND_SOC_DAPM_POST_PMD:
  635. /*
  636. * 5ms sleep is required after PA is disabled as per
  637. * HW requirement.
  638. */
  639. if (test_bit(HPH_PA_DELAY, &rouleur->status_mask)) {
  640. usleep_range(5000, 5100);
  641. clear_bit(HPH_PA_DELAY, &rouleur->status_mask);
  642. }
  643. blocking_notifier_call_chain(&rouleur->mbhc->notifier,
  644. WCD_EVENT_POST_HPHL_PA_OFF,
  645. &rouleur->mbhc->wcd_mbhc);
  646. snd_soc_component_update_bits(component,
  647. ROULEUR_DIG_SWR_PDM_WD_CTL0,
  648. 0x03, 0x00);
  649. break;
  650. };
  651. return ret;
  652. }
  653. static int rouleur_codec_enable_ear_pa(struct snd_soc_dapm_widget *w,
  654. struct snd_kcontrol *kcontrol,
  655. int event)
  656. {
  657. struct snd_soc_component *component =
  658. snd_soc_dapm_to_component(w->dapm);
  659. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  660. int ret = 0;
  661. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  662. w->name, event);
  663. switch (event) {
  664. case SND_SOC_DAPM_PRE_PMU:
  665. ret = swr_slvdev_datapath_control(rouleur->rx_swr_dev,
  666. rouleur->rx_swr_dev->dev_num,
  667. true);
  668. usleep_range(5000, 5100);
  669. snd_soc_component_update_bits(component,
  670. ROULEUR_DIG_SWR_PDM_WD_CTL0,
  671. 0x03, 0x03);
  672. break;
  673. case SND_SOC_DAPM_POST_PMU:
  674. if (rouleur->update_wcd_event)
  675. rouleur->update_wcd_event(rouleur->handle,
  676. WCD_BOLERO_EVT_RX_MUTE,
  677. (WCD_RX1 << 0x10));
  678. wcd_enable_irq(&rouleur->irq_info,
  679. ROULEUR_IRQ_HPHL_PDM_WD_INT);
  680. break;
  681. case SND_SOC_DAPM_PRE_PMD:
  682. wcd_disable_irq(&rouleur->irq_info,
  683. ROULEUR_IRQ_HPHL_PDM_WD_INT);
  684. if (rouleur->update_wcd_event)
  685. rouleur->update_wcd_event(rouleur->handle,
  686. WCD_BOLERO_EVT_RX_MUTE,
  687. (WCD_RX1 << 0x10 | 0x1));
  688. break;
  689. case SND_SOC_DAPM_POST_PMD:
  690. usleep_range(5000, 5100);
  691. snd_soc_component_update_bits(component,
  692. ROULEUR_DIG_SWR_PDM_WD_CTL0,
  693. 0x03, 0x00);
  694. };
  695. return ret;
  696. }
  697. static int rouleur_codec_enable_lo_pa(struct snd_soc_dapm_widget *w,
  698. struct snd_kcontrol *kcontrol,
  699. int event)
  700. {
  701. struct snd_soc_component *component =
  702. snd_soc_dapm_to_component(w->dapm);
  703. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  704. int ret = 0;
  705. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  706. w->name, event);
  707. switch (event) {
  708. case SND_SOC_DAPM_PRE_PMU:
  709. ret = swr_slvdev_datapath_control(rouleur->rx_swr_dev,
  710. rouleur->rx_swr_dev->dev_num,
  711. true);
  712. snd_soc_component_update_bits(component,
  713. ROULEUR_ANA_COMBOPA_CTL,
  714. 0x40, 0x40);
  715. usleep_range(5000, 5100);
  716. snd_soc_component_update_bits(component,
  717. ROULEUR_DIG_SWR_PDM_WD_CTL0,
  718. 0x03, 0x03);
  719. break;
  720. case SND_SOC_DAPM_POST_PMU:
  721. if (rouleur->update_wcd_event)
  722. rouleur->update_wcd_event(rouleur->handle,
  723. WCD_BOLERO_EVT_RX_MUTE,
  724. (WCD_RX1 << 0x10));
  725. wcd_enable_irq(&rouleur->irq_info,
  726. ROULEUR_IRQ_HPHL_PDM_WD_INT);
  727. break;
  728. case SND_SOC_DAPM_PRE_PMD:
  729. wcd_disable_irq(&rouleur->irq_info,
  730. ROULEUR_IRQ_HPHL_PDM_WD_INT);
  731. if (rouleur->update_wcd_event)
  732. rouleur->update_wcd_event(rouleur->handle,
  733. WCD_BOLERO_EVT_RX_MUTE,
  734. (WCD_RX1 << 0x10 | 0x1));
  735. break;
  736. case SND_SOC_DAPM_POST_PMD:
  737. snd_soc_component_update_bits(component,
  738. ROULEUR_ANA_COMBOPA_CTL,
  739. 0x40, 0x00);
  740. usleep_range(5000, 5100);
  741. snd_soc_component_update_bits(component,
  742. ROULEUR_DIG_SWR_PDM_WD_CTL0,
  743. 0x03, 0x00);
  744. };
  745. return ret;
  746. }
  747. static int rouleur_enable_rx1(struct snd_soc_dapm_widget *w,
  748. struct snd_kcontrol *kcontrol,
  749. int event)
  750. {
  751. struct snd_soc_component *component =
  752. snd_soc_dapm_to_component(w->dapm);
  753. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  754. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  755. w->name, event);
  756. switch (event) {
  757. case SND_SOC_DAPM_PRE_PMU:
  758. rouleur_rx_connect_port(component, HPH_L, true);
  759. if (rouleur->comp1_enable)
  760. rouleur_rx_connect_port(component, COMP_L, true);
  761. break;
  762. case SND_SOC_DAPM_POST_PMD:
  763. rouleur_rx_connect_port(component, HPH_L, false);
  764. if (rouleur->comp1_enable)
  765. rouleur_rx_connect_port(component, COMP_L, false);
  766. rouleur_rx_clk_disable(component);
  767. break;
  768. };
  769. return 0;
  770. }
  771. static int rouleur_enable_rx2(struct snd_soc_dapm_widget *w,
  772. struct snd_kcontrol *kcontrol, int event)
  773. {
  774. struct snd_soc_component *component =
  775. snd_soc_dapm_to_component(w->dapm);
  776. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  777. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  778. w->name, event);
  779. switch (event) {
  780. case SND_SOC_DAPM_PRE_PMU:
  781. rouleur_rx_connect_port(component, HPH_R, true);
  782. if (rouleur->comp2_enable)
  783. rouleur_rx_connect_port(component, COMP_R, true);
  784. break;
  785. case SND_SOC_DAPM_POST_PMD:
  786. rouleur_rx_connect_port(component, HPH_R, false);
  787. if (rouleur->comp2_enable)
  788. rouleur_rx_connect_port(component, COMP_R, false);
  789. rouleur_rx_clk_disable(component);
  790. break;
  791. };
  792. return 0;
  793. }
  794. static int rouleur_codec_enable_dmic(struct snd_soc_dapm_widget *w,
  795. struct snd_kcontrol *kcontrol,
  796. int event)
  797. {
  798. struct snd_soc_component *component =
  799. snd_soc_dapm_to_component(w->dapm);
  800. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  801. u16 dmic_clk_reg;
  802. s32 *dmic_clk_cnt;
  803. unsigned int dmic;
  804. char *wname;
  805. int ret = 0;
  806. wname = strpbrk(w->name, "01");
  807. if (!wname) {
  808. dev_err(component->dev, "%s: widget not found\n", __func__);
  809. return -EINVAL;
  810. }
  811. ret = kstrtouint(wname, 10, &dmic);
  812. if (ret < 0) {
  813. dev_err(component->dev, "%s: Invalid DMIC line on the codec\n",
  814. __func__);
  815. return -EINVAL;
  816. }
  817. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  818. w->name, event);
  819. switch (dmic) {
  820. case 0:
  821. case 1:
  822. dmic_clk_cnt = &(rouleur->dmic_0_1_clk_cnt);
  823. dmic_clk_reg = ROULEUR_DIG_SWR_CDC_DMIC1_CTL;
  824. break;
  825. default:
  826. dev_err(component->dev, "%s: Invalid DMIC Selection\n",
  827. __func__);
  828. return -EINVAL;
  829. };
  830. dev_dbg(component->dev, "%s: event %d DMIC%d dmic_clk_cnt %d\n",
  831. __func__, event, dmic, *dmic_clk_cnt);
  832. switch (event) {
  833. case SND_SOC_DAPM_PRE_PMU:
  834. snd_soc_component_update_bits(component,
  835. ROULEUR_DIG_SWR_CDC_AMIC_CTL, 0x02, 0x00);
  836. snd_soc_component_update_bits(component,
  837. dmic_clk_reg, 0x08, 0x08);
  838. rouleur_tx_connect_port(component, DMIC0 + (w->shift), true);
  839. break;
  840. case SND_SOC_DAPM_POST_PMD:
  841. rouleur_tx_connect_port(component, DMIC0 + (w->shift), false);
  842. snd_soc_component_update_bits(component,
  843. dmic_clk_reg, 0x08, 0x00);
  844. snd_soc_component_update_bits(component,
  845. ROULEUR_DIG_SWR_CDC_AMIC_CTL, 0x02, 0x02);
  846. break;
  847. };
  848. return 0;
  849. }
  850. static int rouleur_tx_swr_ctrl(struct snd_soc_dapm_widget *w,
  851. struct snd_kcontrol *kcontrol,
  852. int event)
  853. {
  854. struct snd_soc_component *component =
  855. snd_soc_dapm_to_component(w->dapm);
  856. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  857. int ret = 0;
  858. switch (event) {
  859. case SND_SOC_DAPM_PRE_PMU:
  860. ret = swr_slvdev_datapath_control(rouleur->tx_swr_dev,
  861. rouleur->tx_swr_dev->dev_num,
  862. true);
  863. break;
  864. case SND_SOC_DAPM_POST_PMD:
  865. ret = swr_slvdev_datapath_control(rouleur->tx_swr_dev,
  866. rouleur->tx_swr_dev->dev_num,
  867. false);
  868. break;
  869. };
  870. return ret;
  871. }
  872. static int rouleur_codec_enable_adc(struct snd_soc_dapm_widget *w,
  873. struct snd_kcontrol *kcontrol,
  874. int event)
  875. {
  876. struct snd_soc_component *component =
  877. snd_soc_dapm_to_component(w->dapm);
  878. struct rouleur_priv *rouleur =
  879. snd_soc_component_get_drvdata(component);
  880. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  881. w->name, event);
  882. switch (event) {
  883. case SND_SOC_DAPM_PRE_PMU:
  884. /* Enable BCS for Headset mic */
  885. if (w->shift == 1 && !(snd_soc_component_read32(component,
  886. ROULEUR_ANA_TX_AMIC2) & 0x10)) {
  887. rouleur_tx_connect_port(component, MBHC, true);
  888. set_bit(AMIC2_BCS_ENABLE, &rouleur->status_mask);
  889. }
  890. rouleur_tx_connect_port(component, ADC1 + (w->shift), true);
  891. rouleur_global_mbias_enable(component);
  892. if (w->shift)
  893. snd_soc_component_update_bits(component,
  894. ROULEUR_DIG_SWR_CDC_TX_ANA_MODE_0_1,
  895. 0x30, 0x30);
  896. else
  897. snd_soc_component_update_bits(component,
  898. ROULEUR_DIG_SWR_CDC_TX_ANA_MODE_0_1,
  899. 0x03, 0x03);
  900. break;
  901. case SND_SOC_DAPM_POST_PMD:
  902. rouleur_tx_connect_port(component, ADC1 + (w->shift), false);
  903. if (w->shift == 1 &&
  904. test_bit(AMIC2_BCS_ENABLE, &rouleur->status_mask)) {
  905. rouleur_tx_connect_port(component, MBHC, false);
  906. clear_bit(AMIC2_BCS_ENABLE, &rouleur->status_mask);
  907. }
  908. if (w->shift)
  909. snd_soc_component_update_bits(component,
  910. ROULEUR_DIG_SWR_CDC_TX_ANA_MODE_0_1,
  911. 0x30, 0x00);
  912. else
  913. snd_soc_component_update_bits(component,
  914. ROULEUR_DIG_SWR_CDC_TX_ANA_MODE_0_1,
  915. 0x03, 0x00);
  916. rouleur_global_mbias_disable(component);
  917. break;
  918. };
  919. return 0;
  920. }
  921. /*
  922. * rouleur_get_micb_vout_ctl_val: converts micbias from volts to register value
  923. * @micb_mv: micbias in mv
  924. *
  925. * return register value converted
  926. */
  927. int rouleur_get_micb_vout_ctl_val(u32 micb_mv)
  928. {
  929. /* min micbias voltage is 1.6V and maximum is 2.85V */
  930. if (micb_mv < 1600 || micb_mv > 2850) {
  931. pr_err("%s: unsupported micbias voltage\n", __func__);
  932. return -EINVAL;
  933. }
  934. return (micb_mv - 1600) / 50;
  935. }
  936. EXPORT_SYMBOL(rouleur_get_micb_vout_ctl_val);
  937. /*
  938. * rouleur_mbhc_micb_adjust_voltage: adjust specific micbias voltage
  939. * @component: handle to snd_soc_component *
  940. * @req_volt: micbias voltage to be set
  941. * @micb_num: micbias to be set, e.g. micbias1 or micbias2
  942. *
  943. * return 0 if adjustment is success or error code in case of failure
  944. */
  945. int rouleur_mbhc_micb_adjust_voltage(struct snd_soc_component *component,
  946. int req_volt, int micb_num)
  947. {
  948. struct rouleur_priv *rouleur =
  949. snd_soc_component_get_drvdata(component);
  950. int cur_vout_ctl, req_vout_ctl;
  951. int micb_reg, micb_val, micb_en;
  952. int ret = 0;
  953. int pullup_mask;
  954. micb_reg = ROULEUR_ANA_MICBIAS_MICB_1_2_EN;
  955. switch (micb_num) {
  956. case MIC_BIAS_1:
  957. micb_val = snd_soc_component_read32(component, micb_reg);
  958. micb_en = (micb_val & 0x40) >> 6;
  959. pullup_mask = 0x20;
  960. break;
  961. case MIC_BIAS_2:
  962. micb_val = snd_soc_component_read32(component, micb_reg);
  963. micb_en = (micb_val & 0x04) >> 2;
  964. pullup_mask = 0x02;
  965. break;
  966. case MIC_BIAS_3:
  967. default:
  968. dev_err(component->dev, "%s: Invalid micbias number: %d\n",
  969. __func__, micb_num);
  970. return -EINVAL;
  971. }
  972. mutex_lock(&rouleur->micb_lock);
  973. /*
  974. * If requested micbias voltage is same as current micbias
  975. * voltage, then just return. Otherwise, adjust voltage as
  976. * per requested value. If micbias is already enabled, then
  977. * to avoid slow micbias ramp-up or down enable pull-up
  978. * momentarily, change the micbias value and then re-enable
  979. * micbias.
  980. */
  981. cur_vout_ctl = (snd_soc_component_read32(component,
  982. ROULEUR_ANA_MICBIAS_LDO_1_SETTING)) & 0xF8;
  983. cur_vout_ctl = cur_vout_ctl >> 3;
  984. req_vout_ctl = rouleur_get_micb_vout_ctl_val(req_volt);
  985. if (req_vout_ctl < 0) {
  986. ret = -EINVAL;
  987. goto exit;
  988. }
  989. if (cur_vout_ctl == req_vout_ctl) {
  990. ret = 0;
  991. goto exit;
  992. }
  993. dev_dbg(component->dev, "%s: micb_num: %d, cur_mv: %d, req_mv: %d, micb_en: %d\n",
  994. __func__, micb_num, WCD_VOUT_CTL_TO_MICB(cur_vout_ctl),
  995. req_volt, micb_en);
  996. if (micb_en == 0x1)
  997. snd_soc_component_update_bits(component, micb_reg, pullup_mask,
  998. pullup_mask);
  999. snd_soc_component_update_bits(component,
  1000. ROULEUR_ANA_MICBIAS_LDO_1_SETTING, 0xF8, req_vout_ctl << 3);
  1001. if (micb_en == 0x1) {
  1002. snd_soc_component_update_bits(component, micb_reg,
  1003. pullup_mask, 0x00);
  1004. /*
  1005. * Add 2ms delay as per HW requirement after enabling
  1006. * micbias
  1007. */
  1008. usleep_range(2000, 2100);
  1009. }
  1010. exit:
  1011. mutex_unlock(&rouleur->micb_lock);
  1012. return ret;
  1013. }
  1014. EXPORT_SYMBOL(rouleur_mbhc_micb_adjust_voltage);
  1015. int rouleur_micbias_control(struct snd_soc_component *component,
  1016. int micb_num, int req, bool is_dapm)
  1017. {
  1018. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  1019. int micb_index = micb_num - 1;
  1020. u16 micb_reg;
  1021. int pre_off_event = 0, post_off_event = 0;
  1022. int post_on_event = 0, post_dapm_off = 0;
  1023. int post_dapm_on = 0;
  1024. u8 pullup_mask = 0, enable_mask = 0;
  1025. int ret = 0;
  1026. if ((micb_index < 0) || (micb_index > ROULEUR_MAX_MICBIAS - 1)) {
  1027. dev_err(component->dev, "%s: Invalid micbias index, micb_ind:%d\n",
  1028. __func__, micb_index);
  1029. return -EINVAL;
  1030. }
  1031. switch (micb_num) {
  1032. case MIC_BIAS_1:
  1033. micb_reg = ROULEUR_ANA_MICBIAS_MICB_1_2_EN;
  1034. pullup_mask = 0x20;
  1035. enable_mask = 0x40;
  1036. break;
  1037. case MIC_BIAS_2:
  1038. micb_reg = ROULEUR_ANA_MICBIAS_MICB_1_2_EN;
  1039. pullup_mask = 0x02;
  1040. enable_mask = 0x04;
  1041. pre_off_event = WCD_EVENT_PRE_MICBIAS_2_OFF;
  1042. post_off_event = WCD_EVENT_POST_MICBIAS_2_OFF;
  1043. post_on_event = WCD_EVENT_POST_MICBIAS_2_ON;
  1044. post_dapm_on = WCD_EVENT_POST_DAPM_MICBIAS_2_ON;
  1045. post_dapm_off = WCD_EVENT_POST_DAPM_MICBIAS_2_OFF;
  1046. break;
  1047. case MIC_BIAS_3:
  1048. micb_reg = ROULEUR_ANA_MICBIAS_MICB_3_EN;
  1049. pullup_mask = 0x02;
  1050. break;
  1051. default:
  1052. dev_err(component->dev, "%s: Invalid micbias number: %d\n",
  1053. __func__, micb_num);
  1054. return -EINVAL;
  1055. };
  1056. mutex_lock(&rouleur->micb_lock);
  1057. switch (req) {
  1058. case MICB_PULLUP_ENABLE:
  1059. if (!rouleur->dev_up) {
  1060. dev_dbg(component->dev, "%s: enable req %d wcd device down\n",
  1061. __func__, req);
  1062. ret = -ENODEV;
  1063. goto done;
  1064. }
  1065. rouleur->pullup_ref[micb_index]++;
  1066. if ((rouleur->pullup_ref[micb_index] == 1) &&
  1067. (rouleur->micb_ref[micb_index] == 0))
  1068. snd_soc_component_update_bits(component, micb_reg,
  1069. pullup_mask, pullup_mask);
  1070. break;
  1071. case MICB_PULLUP_DISABLE:
  1072. if (!rouleur->dev_up) {
  1073. dev_dbg(component->dev, "%s: enable req %d wcd device down\n",
  1074. __func__, req);
  1075. ret = -ENODEV;
  1076. goto done;
  1077. }
  1078. if (rouleur->pullup_ref[micb_index] > 0)
  1079. rouleur->pullup_ref[micb_index]--;
  1080. if ((rouleur->pullup_ref[micb_index] == 0) &&
  1081. (rouleur->micb_ref[micb_index] == 0))
  1082. snd_soc_component_update_bits(component, micb_reg,
  1083. pullup_mask, 0x00);
  1084. break;
  1085. case MICB_ENABLE:
  1086. if (!rouleur->dev_up) {
  1087. dev_dbg(component->dev, "%s: enable req %d wcd device down\n",
  1088. __func__, req);
  1089. ret = -ENODEV;
  1090. goto done;
  1091. }
  1092. rouleur->micb_ref[micb_index]++;
  1093. if (rouleur->micb_ref[micb_index] == 1) {
  1094. rouleur_global_mbias_enable(component);
  1095. snd_soc_component_update_bits(component,
  1096. micb_reg, enable_mask, enable_mask);
  1097. if (post_on_event)
  1098. blocking_notifier_call_chain(
  1099. &rouleur->mbhc->notifier, post_on_event,
  1100. &rouleur->mbhc->wcd_mbhc);
  1101. }
  1102. if (is_dapm && post_dapm_on && rouleur->mbhc)
  1103. blocking_notifier_call_chain(
  1104. &rouleur->mbhc->notifier, post_dapm_on,
  1105. &rouleur->mbhc->wcd_mbhc);
  1106. break;
  1107. case MICB_DISABLE:
  1108. if (rouleur->micb_ref[micb_index] > 0)
  1109. rouleur->micb_ref[micb_index]--;
  1110. if (!rouleur->dev_up) {
  1111. dev_dbg(component->dev, "%s: enable req %d wcd device down\n",
  1112. __func__, req);
  1113. ret = -ENODEV;
  1114. goto done;
  1115. }
  1116. if ((rouleur->micb_ref[micb_index] == 0) &&
  1117. (rouleur->pullup_ref[micb_index] > 0)) {
  1118. snd_soc_component_update_bits(component, micb_reg,
  1119. pullup_mask, pullup_mask);
  1120. snd_soc_component_update_bits(component, micb_reg,
  1121. enable_mask, 0x00);
  1122. rouleur_global_mbias_disable(component);
  1123. } else if ((rouleur->micb_ref[micb_index] == 0) &&
  1124. (rouleur->pullup_ref[micb_index] == 0)) {
  1125. if (pre_off_event && rouleur->mbhc)
  1126. blocking_notifier_call_chain(
  1127. &rouleur->mbhc->notifier, pre_off_event,
  1128. &rouleur->mbhc->wcd_mbhc);
  1129. snd_soc_component_update_bits(component, micb_reg,
  1130. enable_mask, 0x00);
  1131. rouleur_global_mbias_disable(component);
  1132. if (post_off_event && rouleur->mbhc)
  1133. blocking_notifier_call_chain(
  1134. &rouleur->mbhc->notifier,
  1135. post_off_event,
  1136. &rouleur->mbhc->wcd_mbhc);
  1137. }
  1138. if (is_dapm && post_dapm_off && rouleur->mbhc)
  1139. blocking_notifier_call_chain(
  1140. &rouleur->mbhc->notifier, post_dapm_off,
  1141. &rouleur->mbhc->wcd_mbhc);
  1142. break;
  1143. };
  1144. dev_dbg(component->dev, "%s: micb_num:%d, micb_ref: %d, pullup_ref: %d\n",
  1145. __func__, micb_num, rouleur->micb_ref[micb_index],
  1146. rouleur->pullup_ref[micb_index]);
  1147. done:
  1148. mutex_unlock(&rouleur->micb_lock);
  1149. return 0;
  1150. }
  1151. EXPORT_SYMBOL(rouleur_micbias_control);
  1152. void rouleur_disable_bcs_before_slow_insert(struct snd_soc_component *component,
  1153. bool bcs_disable)
  1154. {
  1155. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  1156. if (rouleur->update_wcd_event) {
  1157. if (bcs_disable)
  1158. rouleur->update_wcd_event(rouleur->handle,
  1159. WCD_BOLERO_EVT_BCS_CLK_OFF, 0);
  1160. else
  1161. rouleur->update_wcd_event(rouleur->handle,
  1162. WCD_BOLERO_EVT_BCS_CLK_OFF, 1);
  1163. }
  1164. }
  1165. static int rouleur_get_logical_addr(struct swr_device *swr_dev)
  1166. {
  1167. int ret = 0;
  1168. uint8_t devnum = 0;
  1169. int num_retry = NUM_ATTEMPTS;
  1170. do {
  1171. ret = swr_get_logical_dev_num(swr_dev, swr_dev->addr, &devnum);
  1172. if (ret) {
  1173. dev_err(&swr_dev->dev,
  1174. "%s get devnum %d for dev addr %lx failed\n",
  1175. __func__, devnum, swr_dev->addr);
  1176. /* retry after 1ms */
  1177. usleep_range(1000, 1010);
  1178. }
  1179. } while (ret && --num_retry);
  1180. swr_dev->dev_num = devnum;
  1181. return 0;
  1182. }
  1183. static int rouleur_event_notify(struct notifier_block *block,
  1184. unsigned long val,
  1185. void *data)
  1186. {
  1187. u16 event = (val & 0xffff);
  1188. int ret = 0;
  1189. struct rouleur_priv *rouleur = dev_get_drvdata((struct device *)data);
  1190. struct snd_soc_component *component = rouleur->component;
  1191. struct wcd_mbhc *mbhc;
  1192. switch (event) {
  1193. case BOLERO_WCD_EVT_PA_OFF_PRE_SSR:
  1194. snd_soc_component_update_bits(component,
  1195. ROULEUR_ANA_HPHPA_CNP_CTL_2,
  1196. 0xC0, 0x00);
  1197. snd_soc_component_update_bits(component,
  1198. ROULEUR_ANA_COMBOPA_CTL,
  1199. 0x40, 0x00);
  1200. snd_soc_component_update_bits(component,
  1201. ROULEUR_ANA_COMBOPA_CTL,
  1202. 0x80, 0x00);
  1203. snd_soc_component_update_bits(component,
  1204. ROULEUR_ANA_COMBOPA_CTL,
  1205. 0x40, 0x40);
  1206. snd_soc_component_update_bits(component,
  1207. ROULEUR_ANA_COMBOPA_CTL,
  1208. 0x80, 0x00);
  1209. break;
  1210. case BOLERO_WCD_EVT_SSR_DOWN:
  1211. rouleur->dev_up = false;
  1212. rouleur->mbhc->wcd_mbhc.deinit_in_progress = true;
  1213. mbhc = &rouleur->mbhc->wcd_mbhc;
  1214. rouleur_mbhc_ssr_down(rouleur->mbhc, component);
  1215. rouleur_reset(rouleur->dev, 0x01);
  1216. break;
  1217. case BOLERO_WCD_EVT_SSR_UP:
  1218. rouleur_reset(rouleur->dev, 0x00);
  1219. /* allow reset to take effect */
  1220. usleep_range(10000, 10010);
  1221. rouleur_get_logical_addr(rouleur->tx_swr_dev);
  1222. rouleur_get_logical_addr(rouleur->rx_swr_dev);
  1223. rouleur_init_reg(component);
  1224. regcache_mark_dirty(rouleur->regmap);
  1225. regcache_sync(rouleur->regmap);
  1226. /* Initialize MBHC module */
  1227. mbhc = &rouleur->mbhc->wcd_mbhc;
  1228. ret = rouleur_mbhc_post_ssr_init(rouleur->mbhc, component);
  1229. if (ret) {
  1230. dev_err(component->dev, "%s: mbhc initialization failed\n",
  1231. __func__);
  1232. } else {
  1233. rouleur_mbhc_hs_detect(component, mbhc->mbhc_cfg);
  1234. }
  1235. rouleur->mbhc->wcd_mbhc.deinit_in_progress = false;
  1236. rouleur->dev_up = true;
  1237. break;
  1238. default:
  1239. dev_err(component->dev, "%s: invalid event %d\n", __func__,
  1240. event);
  1241. break;
  1242. }
  1243. return 0;
  1244. }
  1245. static int __rouleur_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1246. int event)
  1247. {
  1248. struct snd_soc_component *component =
  1249. snd_soc_dapm_to_component(w->dapm);
  1250. int micb_num;
  1251. dev_dbg(component->dev, "%s: wname: %s, event: %d\n",
  1252. __func__, w->name, event);
  1253. if (strnstr(w->name, "MIC BIAS1", sizeof("MIC BIAS1")))
  1254. micb_num = MIC_BIAS_1;
  1255. else if (strnstr(w->name, "MIC BIAS2", sizeof("MIC BIAS2")))
  1256. micb_num = MIC_BIAS_2;
  1257. else if (strnstr(w->name, "MIC BIAS3", sizeof("MIC BIAS3")))
  1258. micb_num = MIC_BIAS_3;
  1259. else
  1260. return -EINVAL;
  1261. switch (event) {
  1262. case SND_SOC_DAPM_PRE_PMU:
  1263. /* Micbias LD0 enable not supported for MicBias 3*/
  1264. if (micb_num == MIC_BIAS_3)
  1265. rouleur_micbias_control(component, micb_num,
  1266. MICB_PULLUP_ENABLE, true);
  1267. else
  1268. rouleur_micbias_control(component, micb_num,
  1269. MICB_ENABLE, true);
  1270. break;
  1271. case SND_SOC_DAPM_POST_PMU:
  1272. usleep_range(1000, 1100);
  1273. break;
  1274. case SND_SOC_DAPM_POST_PMD:
  1275. if (micb_num == MIC_BIAS_3)
  1276. rouleur_micbias_control(component, micb_num,
  1277. MICB_PULLUP_DISABLE, true);
  1278. else
  1279. rouleur_micbias_control(component, micb_num,
  1280. MICB_DISABLE, true);
  1281. break;
  1282. };
  1283. return 0;
  1284. }
  1285. static int rouleur_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1286. struct snd_kcontrol *kcontrol,
  1287. int event)
  1288. {
  1289. return __rouleur_codec_enable_micbias(w, event);
  1290. }
  1291. static int __rouleur_codec_enable_micbias_pullup(struct snd_soc_dapm_widget *w,
  1292. int event)
  1293. {
  1294. struct snd_soc_component *component =
  1295. snd_soc_dapm_to_component(w->dapm);
  1296. int micb_num;
  1297. dev_dbg(component->dev, "%s: wname: %s, event: %d\n",
  1298. __func__, w->name, event);
  1299. if (strnstr(w->name, "VA MIC BIAS1", sizeof("VA MIC BIAS1")))
  1300. micb_num = MIC_BIAS_1;
  1301. else if (strnstr(w->name, "VA MIC BIAS2", sizeof("VA MIC BIAS2")))
  1302. micb_num = MIC_BIAS_2;
  1303. else if (strnstr(w->name, "VA MIC BIAS3", sizeof("VA MIC BIAS3")))
  1304. micb_num = MIC_BIAS_3;
  1305. else
  1306. return -EINVAL;
  1307. switch (event) {
  1308. case SND_SOC_DAPM_PRE_PMU:
  1309. rouleur_micbias_control(component, micb_num,
  1310. MICB_PULLUP_ENABLE, true);
  1311. break;
  1312. case SND_SOC_DAPM_POST_PMU:
  1313. /* 1 msec delay as per HW requirement */
  1314. usleep_range(1000, 1100);
  1315. break;
  1316. case SND_SOC_DAPM_POST_PMD:
  1317. rouleur_micbias_control(component, micb_num,
  1318. MICB_PULLUP_DISABLE, true);
  1319. break;
  1320. };
  1321. return 0;
  1322. }
  1323. static int rouleur_codec_enable_micbias_pullup(struct snd_soc_dapm_widget *w,
  1324. struct snd_kcontrol *kcontrol,
  1325. int event)
  1326. {
  1327. return __rouleur_codec_enable_micbias_pullup(w, event);
  1328. }
  1329. static int rouleur_get_compander(struct snd_kcontrol *kcontrol,
  1330. struct snd_ctl_elem_value *ucontrol)
  1331. {
  1332. struct snd_soc_component *component =
  1333. snd_soc_kcontrol_component(kcontrol);
  1334. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  1335. bool hphr;
  1336. struct soc_multi_mixer_control *mc;
  1337. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  1338. hphr = mc->shift;
  1339. ucontrol->value.integer.value[0] = hphr ? rouleur->comp2_enable :
  1340. rouleur->comp1_enable;
  1341. return 0;
  1342. }
  1343. static int rouleur_set_compander(struct snd_kcontrol *kcontrol,
  1344. struct snd_ctl_elem_value *ucontrol)
  1345. {
  1346. struct snd_soc_component *component =
  1347. snd_soc_kcontrol_component(kcontrol);
  1348. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  1349. int value = ucontrol->value.integer.value[0];
  1350. bool hphr;
  1351. struct soc_multi_mixer_control *mc;
  1352. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  1353. hphr = mc->shift;
  1354. if (hphr)
  1355. rouleur->comp2_enable = value;
  1356. else
  1357. rouleur->comp1_enable = value;
  1358. return 0;
  1359. }
  1360. static int rouleur_codec_enable_pa_vpos(struct snd_soc_dapm_widget *w,
  1361. struct snd_kcontrol *kcontrol,
  1362. int event)
  1363. {
  1364. struct snd_soc_component *component =
  1365. snd_soc_dapm_to_component(w->dapm);
  1366. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  1367. struct rouleur_pdata *pdata = NULL;
  1368. int ret = 0;
  1369. pdata = dev_get_platdata(rouleur->dev);
  1370. if (!pdata) {
  1371. dev_err(component->dev, "%s: pdata is NULL\n", __func__);
  1372. return -EINVAL;
  1373. }
  1374. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1375. w->name, event);
  1376. switch (event) {
  1377. case SND_SOC_DAPM_PRE_PMU:
  1378. if (test_bit(ALLOW_VPOS_DISABLE, &rouleur->status_mask)) {
  1379. dev_dbg(component->dev,
  1380. "%s: vpos already in enabled state\n",
  1381. __func__);
  1382. clear_bit(ALLOW_VPOS_DISABLE, &rouleur->status_mask);
  1383. return 0;
  1384. }
  1385. ret = msm_cdc_enable_ondemand_supply(rouleur->dev,
  1386. rouleur->supplies,
  1387. pdata->regulator,
  1388. pdata->num_supplies,
  1389. "cdc-pa-vpos");
  1390. if (ret == -EINVAL) {
  1391. dev_err(component->dev, "%s: pa vpos is not enabled\n",
  1392. __func__);
  1393. return ret;
  1394. }
  1395. clear_bit(ALLOW_VPOS_DISABLE, &rouleur->status_mask);
  1396. /*
  1397. * 200us sleep is required after LDO15 is enabled as per
  1398. * HW requirement
  1399. */
  1400. usleep_range(200, 250);
  1401. break;
  1402. case SND_SOC_DAPM_POST_PMD:
  1403. set_bit(ALLOW_VPOS_DISABLE, &rouleur->status_mask);
  1404. ret = swr_slvdev_datapath_control(rouleur->rx_swr_dev,
  1405. rouleur->rx_swr_dev->dev_num,
  1406. false);
  1407. break;
  1408. }
  1409. return 0;
  1410. }
  1411. static const struct snd_kcontrol_new rouleur_snd_controls[] = {
  1412. SOC_SINGLE_EXT("HPHL_COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  1413. rouleur_get_compander, rouleur_set_compander),
  1414. SOC_SINGLE_EXT("HPHR_COMP Switch", SND_SOC_NOPM, 1, 1, 0,
  1415. rouleur_get_compander, rouleur_set_compander),
  1416. SOC_SINGLE_TLV("HPHL Volume", ROULEUR_ANA_HPHPA_L_GAIN, 0, 20, 1,
  1417. line_gain),
  1418. SOC_SINGLE_TLV("HPHR Volume", ROULEUR_ANA_HPHPA_R_GAIN, 0, 20, 1,
  1419. line_gain),
  1420. SOC_SINGLE_TLV("ADC1 Volume", ROULEUR_ANA_TX_AMIC1, 0, 8, 0,
  1421. analog_gain),
  1422. SOC_SINGLE_TLV("ADC2 Volume", ROULEUR_ANA_TX_AMIC2, 0, 8, 0,
  1423. analog_gain),
  1424. };
  1425. static const struct snd_kcontrol_new adc1_switch[] = {
  1426. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1427. };
  1428. static const struct snd_kcontrol_new adc2_switch[] = {
  1429. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1430. };
  1431. static const struct snd_kcontrol_new dmic1_switch[] = {
  1432. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1433. };
  1434. static const struct snd_kcontrol_new dmic2_switch[] = {
  1435. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1436. };
  1437. static const struct snd_kcontrol_new ear_rdac_switch[] = {
  1438. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1439. };
  1440. static const struct snd_kcontrol_new lo_rdac_switch[] = {
  1441. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1442. };
  1443. static const struct snd_kcontrol_new hphl_rdac_switch[] = {
  1444. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1445. };
  1446. static const struct snd_kcontrol_new hphr_rdac_switch[] = {
  1447. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  1448. };
  1449. static const char * const adc2_mux_text[] = {
  1450. "INP2", "INP3"
  1451. };
  1452. static const struct soc_enum adc2_enum =
  1453. SOC_ENUM_SINGLE(ROULEUR_ANA_TX_AMIC2, 4,
  1454. ARRAY_SIZE(adc2_mux_text), adc2_mux_text);
  1455. static const struct snd_kcontrol_new tx_adc2_mux =
  1456. SOC_DAPM_ENUM("ADC2 MUX Mux", adc2_enum);
  1457. static const struct snd_soc_dapm_widget rouleur_dapm_widgets[] = {
  1458. /*input widgets*/
  1459. SND_SOC_DAPM_INPUT("AMIC1"),
  1460. SND_SOC_DAPM_INPUT("AMIC2"),
  1461. SND_SOC_DAPM_INPUT("AMIC3"),
  1462. SND_SOC_DAPM_INPUT("IN1_HPHL"),
  1463. SND_SOC_DAPM_INPUT("IN2_HPHR"),
  1464. /*tx widgets*/
  1465. SND_SOC_DAPM_ADC_E("ADC1", NULL, SND_SOC_NOPM, 0, 0,
  1466. rouleur_codec_enable_adc,
  1467. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1468. SND_SOC_DAPM_ADC_E("ADC2", NULL, SND_SOC_NOPM, 1, 0,
  1469. rouleur_codec_enable_adc,
  1470. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1471. SND_SOC_DAPM_MUX("ADC2 MUX", SND_SOC_NOPM, 0, 0,
  1472. &tx_adc2_mux),
  1473. /*tx mixers*/
  1474. SND_SOC_DAPM_MIXER_E("ADC1_MIXER", SND_SOC_NOPM, 0, 0,
  1475. adc1_switch, ARRAY_SIZE(adc1_switch),
  1476. rouleur_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1477. SND_SOC_DAPM_POST_PMD),
  1478. SND_SOC_DAPM_MIXER_E("ADC2_MIXER", SND_SOC_NOPM, 0, 0,
  1479. adc2_switch, ARRAY_SIZE(adc2_switch),
  1480. rouleur_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1481. SND_SOC_DAPM_POST_PMD),
  1482. /* micbias widgets*/
  1483. SND_SOC_DAPM_MICBIAS_E("MIC BIAS1", SND_SOC_NOPM, 0, 0,
  1484. rouleur_codec_enable_micbias,
  1485. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1486. SND_SOC_DAPM_POST_PMD),
  1487. SND_SOC_DAPM_MICBIAS_E("MIC BIAS2", SND_SOC_NOPM, 0, 0,
  1488. rouleur_codec_enable_micbias,
  1489. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1490. SND_SOC_DAPM_POST_PMD),
  1491. SND_SOC_DAPM_MICBIAS_E("MIC BIAS3", SND_SOC_NOPM, 0, 0,
  1492. rouleur_codec_enable_micbias,
  1493. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1494. SND_SOC_DAPM_POST_PMD),
  1495. SND_SOC_DAPM_SUPPLY("PA_VPOS", SND_SOC_NOPM, 0, 0,
  1496. rouleur_codec_enable_pa_vpos,
  1497. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1498. /*rx widgets*/
  1499. SND_SOC_DAPM_PGA_E("EAR PGA", ROULEUR_ANA_COMBOPA_CTL, 7, 0, NULL, 0,
  1500. rouleur_codec_enable_ear_pa,
  1501. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1502. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1503. SND_SOC_DAPM_PGA_E("LO PGA", ROULEUR_ANA_COMBOPA_CTL, 7, 0, NULL, 0,
  1504. rouleur_codec_enable_lo_pa,
  1505. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1506. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1507. SND_SOC_DAPM_PGA_E("HPHL PGA", ROULEUR_ANA_HPHPA_CNP_CTL_2, 7, 0, NULL,
  1508. 0, rouleur_codec_enable_hphl_pa,
  1509. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1510. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1511. SND_SOC_DAPM_PGA_E("HPHR PGA", ROULEUR_ANA_HPHPA_CNP_CTL_2, 6, 0, NULL,
  1512. 0, rouleur_codec_enable_hphr_pa,
  1513. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1514. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1515. SND_SOC_DAPM_DAC_E("RDAC1", NULL, SND_SOC_NOPM, 0, 0,
  1516. rouleur_codec_hphl_dac_event,
  1517. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1518. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1519. SND_SOC_DAPM_DAC_E("RDAC2", NULL, SND_SOC_NOPM, 0, 0,
  1520. rouleur_codec_hphr_dac_event,
  1521. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1522. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1523. SND_SOC_DAPM_DAC_E("RDAC3", NULL, SND_SOC_NOPM, 0, 0,
  1524. rouleur_codec_ear_lo_dac_event,
  1525. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1526. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  1527. SND_SOC_DAPM_MIXER_E("RX1", SND_SOC_NOPM, 0, 0, NULL, 0,
  1528. rouleur_enable_rx1, SND_SOC_DAPM_PRE_PMU |
  1529. SND_SOC_DAPM_POST_PMD),
  1530. SND_SOC_DAPM_MIXER_E("RX2", SND_SOC_NOPM, 0, 0, NULL, 0,
  1531. rouleur_enable_rx2, SND_SOC_DAPM_PRE_PMU |
  1532. SND_SOC_DAPM_POST_PMD),
  1533. /* rx mixer widgets*/
  1534. SND_SOC_DAPM_MIXER("EAR_RDAC", SND_SOC_NOPM, 0, 0,
  1535. ear_rdac_switch, ARRAY_SIZE(ear_rdac_switch)),
  1536. SND_SOC_DAPM_MIXER("LO_RDAC", SND_SOC_NOPM, 0, 0,
  1537. lo_rdac_switch, ARRAY_SIZE(lo_rdac_switch)),
  1538. SND_SOC_DAPM_MIXER("HPHL_RDAC", SND_SOC_NOPM, 0, 0,
  1539. hphl_rdac_switch, ARRAY_SIZE(hphl_rdac_switch)),
  1540. SND_SOC_DAPM_MIXER("HPHR_RDAC", SND_SOC_NOPM, 0, 0,
  1541. hphr_rdac_switch, ARRAY_SIZE(hphr_rdac_switch)),
  1542. /*output widgets tx*/
  1543. SND_SOC_DAPM_OUTPUT("ADC1_OUTPUT"),
  1544. SND_SOC_DAPM_OUTPUT("ADC2_OUTPUT"),
  1545. /*output widgets rx*/
  1546. SND_SOC_DAPM_OUTPUT("EAR"),
  1547. SND_SOC_DAPM_OUTPUT("LO"),
  1548. SND_SOC_DAPM_OUTPUT("HPHL"),
  1549. SND_SOC_DAPM_OUTPUT("HPHR"),
  1550. /* micbias pull up widgets*/
  1551. SND_SOC_DAPM_MICBIAS_E("VA MIC BIAS1", SND_SOC_NOPM, 0, 0,
  1552. rouleur_codec_enable_micbias_pullup,
  1553. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1554. SND_SOC_DAPM_POST_PMD),
  1555. SND_SOC_DAPM_MICBIAS_E("VA MIC BIAS2", SND_SOC_NOPM, 0, 0,
  1556. rouleur_codec_enable_micbias_pullup,
  1557. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1558. SND_SOC_DAPM_POST_PMD),
  1559. SND_SOC_DAPM_MICBIAS_E("VA MIC BIAS3", SND_SOC_NOPM, 0, 0,
  1560. rouleur_codec_enable_micbias_pullup,
  1561. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1562. SND_SOC_DAPM_POST_PMD),
  1563. SND_SOC_DAPM_ADC_E("DMIC1", NULL, SND_SOC_NOPM, 0, 0,
  1564. rouleur_codec_enable_dmic,
  1565. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1566. SND_SOC_DAPM_ADC_E("DMIC2", NULL, SND_SOC_NOPM, 1, 0,
  1567. rouleur_codec_enable_dmic,
  1568. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1569. /*tx mixer widgets*/
  1570. SND_SOC_DAPM_MIXER_E("DMIC1_MIXER", SND_SOC_NOPM, 0,
  1571. 0, dmic1_switch, ARRAY_SIZE(dmic1_switch),
  1572. rouleur_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1573. SND_SOC_DAPM_POST_PMD),
  1574. SND_SOC_DAPM_MIXER_E("DMIC2_MIXER", SND_SOC_NOPM, 0,
  1575. 0, dmic2_switch, ARRAY_SIZE(dmic2_switch),
  1576. rouleur_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  1577. SND_SOC_DAPM_POST_PMD),
  1578. /*output widgets*/
  1579. SND_SOC_DAPM_OUTPUT("DMIC1_OUTPUT"),
  1580. SND_SOC_DAPM_OUTPUT("DMIC2_OUTPUT"),
  1581. };
  1582. static const struct snd_soc_dapm_route rouleur_audio_map[] = {
  1583. {"ADC1_OUTPUT", NULL, "ADC1_MIXER"},
  1584. {"ADC1_MIXER", "Switch", "ADC1"},
  1585. {"ADC1", NULL, "AMIC1"},
  1586. {"ADC2_OUTPUT", NULL, "ADC2_MIXER"},
  1587. {"ADC2_MIXER", "Switch", "ADC2"},
  1588. {"ADC2", NULL, "ADC2 MUX"},
  1589. {"ADC2 MUX", "INP3", "AMIC3"},
  1590. {"ADC2 MUX", "INP2", "AMIC2"},
  1591. {"IN1_HPHL", NULL, "PA_VPOS"},
  1592. {"RX1", NULL, "IN1_HPHL"},
  1593. {"RDAC1", NULL, "RX1"},
  1594. {"HPHL_RDAC", "Switch", "RDAC1"},
  1595. {"HPHL PGA", NULL, "HPHL_RDAC"},
  1596. {"HPHL", NULL, "HPHL PGA"},
  1597. {"IN2_HPHR", NULL, "PA_VPOS"},
  1598. {"RX2", NULL, "IN2_HPHR"},
  1599. {"RDAC2", NULL, "RX2"},
  1600. {"HPHR_RDAC", "Switch", "RDAC2"},
  1601. {"HPHR PGA", NULL, "HPHR_RDAC"},
  1602. {"HPHR", NULL, "HPHR PGA"},
  1603. {"RDAC3", NULL, "RX1"},
  1604. {"EAR_RDAC", "Switch", "RDAC3"},
  1605. {"EAR PGA", NULL, "EAR_RDAC"},
  1606. {"EAR", NULL, "EAR PGA"},
  1607. {"RDAC3", NULL, "RX1"},
  1608. {"LO_RDAC", "Switch", "RDAC3"},
  1609. {"LO PGA", NULL, "LO_RDAC"},
  1610. {"LO", NULL, "LO PGA"},
  1611. {"DMIC1_OUTPUT", NULL, "DMIC1_MIXER"},
  1612. {"DMIC1_MIXER", "Switch", "DMIC1"},
  1613. {"DMIC2_OUTPUT", NULL, "DMIC2_MIXER"},
  1614. {"DMIC2_MIXER", "Switch", "DMIC2"},
  1615. };
  1616. static ssize_t rouleur_version_read(struct snd_info_entry *entry,
  1617. void *file_private_data,
  1618. struct file *file,
  1619. char __user *buf, size_t count,
  1620. loff_t pos)
  1621. {
  1622. struct rouleur_priv *priv;
  1623. char buffer[ROULEUR_VERSION_ENTRY_SIZE];
  1624. int len = 0;
  1625. priv = (struct rouleur_priv *) entry->private_data;
  1626. if (!priv) {
  1627. pr_err("%s: rouleur priv is null\n", __func__);
  1628. return -EINVAL;
  1629. }
  1630. switch (priv->version) {
  1631. case ROULEUR_VERSION_1_0:
  1632. len = snprintf(buffer, sizeof(buffer), "ROULEUR_1_0\n");
  1633. break;
  1634. default:
  1635. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  1636. }
  1637. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  1638. }
  1639. static struct snd_info_entry_ops rouleur_info_ops = {
  1640. .read = rouleur_version_read,
  1641. };
  1642. /*
  1643. * rouleur_info_create_codec_entry - creates rouleur module
  1644. * @codec_root: The parent directory
  1645. * @component: component instance
  1646. *
  1647. * Creates rouleur module and version entry under the given
  1648. * parent directory.
  1649. *
  1650. * Return: 0 on success or negative error code on failure.
  1651. */
  1652. int rouleur_info_create_codec_entry(struct snd_info_entry *codec_root,
  1653. struct snd_soc_component *component)
  1654. {
  1655. struct snd_info_entry *version_entry;
  1656. struct rouleur_priv *priv;
  1657. struct snd_soc_card *card;
  1658. if (!codec_root || !component)
  1659. return -EINVAL;
  1660. priv = snd_soc_component_get_drvdata(component);
  1661. if (priv->entry) {
  1662. dev_dbg(priv->dev,
  1663. "%s:rouleur module already created\n", __func__);
  1664. return 0;
  1665. }
  1666. card = component->card;
  1667. priv->entry = snd_info_create_subdir(codec_root->module,
  1668. "rouleur", codec_root);
  1669. if (!priv->entry) {
  1670. dev_dbg(component->dev, "%s: failed to create rouleur entry\n",
  1671. __func__);
  1672. return -ENOMEM;
  1673. }
  1674. version_entry = snd_info_create_card_entry(card->snd_card,
  1675. "version",
  1676. priv->entry);
  1677. if (!version_entry) {
  1678. dev_dbg(component->dev, "%s: failed to create rouleur version entry\n",
  1679. __func__);
  1680. return -ENOMEM;
  1681. }
  1682. version_entry->private_data = priv;
  1683. version_entry->size = ROULEUR_VERSION_ENTRY_SIZE;
  1684. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  1685. version_entry->c.ops = &rouleur_info_ops;
  1686. if (snd_info_register(version_entry) < 0) {
  1687. snd_info_free_entry(version_entry);
  1688. return -ENOMEM;
  1689. }
  1690. priv->version_entry = version_entry;
  1691. return 0;
  1692. }
  1693. EXPORT_SYMBOL(rouleur_info_create_codec_entry);
  1694. static int rouleur_set_micbias_data(struct rouleur_priv *rouleur,
  1695. struct rouleur_pdata *pdata)
  1696. {
  1697. int vout_ctl = 0;
  1698. int rc = 0;
  1699. if (!pdata) {
  1700. dev_err(rouleur->dev, "%s: NULL pdata\n", __func__);
  1701. return -ENODEV;
  1702. }
  1703. /* set micbias voltage */
  1704. vout_ctl = rouleur_get_micb_vout_ctl_val(pdata->micbias.micb1_mv);
  1705. if (vout_ctl < 0) {
  1706. rc = -EINVAL;
  1707. goto done;
  1708. }
  1709. regmap_update_bits(rouleur->regmap, ROULEUR_ANA_MICBIAS_LDO_1_SETTING,
  1710. 0xF8, vout_ctl << 3);
  1711. done:
  1712. return rc;
  1713. }
  1714. static int rouleur_soc_codec_probe(struct snd_soc_component *component)
  1715. {
  1716. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  1717. struct snd_soc_dapm_context *dapm =
  1718. snd_soc_component_get_dapm(component);
  1719. int ret = -EINVAL;
  1720. dev_info(component->dev, "%s()\n", __func__);
  1721. rouleur = snd_soc_component_get_drvdata(component);
  1722. if (!rouleur)
  1723. return -EINVAL;
  1724. rouleur->component = component;
  1725. snd_soc_component_init_regmap(component, rouleur->regmap);
  1726. rouleur->fw_data = devm_kzalloc(component->dev,
  1727. sizeof(*(rouleur->fw_data)),
  1728. GFP_KERNEL);
  1729. if (!rouleur->fw_data) {
  1730. dev_err(component->dev, "Failed to allocate fw_data\n");
  1731. ret = -ENOMEM;
  1732. goto done;
  1733. }
  1734. set_bit(WCD9XXX_MBHC_CAL, rouleur->fw_data->cal_bit);
  1735. ret = wcd_cal_create_hwdep(rouleur->fw_data,
  1736. WCD9XXX_CODEC_HWDEP_NODE, component);
  1737. if (ret < 0) {
  1738. dev_err(component->dev, "%s hwdep failed %d\n", __func__, ret);
  1739. goto done;
  1740. }
  1741. ret = rouleur_mbhc_init(&rouleur->mbhc, component, rouleur->fw_data);
  1742. if (ret) {
  1743. pr_err("%s: mbhc initialization failed\n", __func__);
  1744. goto done;
  1745. }
  1746. snd_soc_dapm_ignore_suspend(dapm, "AMIC1");
  1747. snd_soc_dapm_ignore_suspend(dapm, "AMIC2");
  1748. snd_soc_dapm_ignore_suspend(dapm, "AMIC3");
  1749. snd_soc_dapm_ignore_suspend(dapm, "IN1_HPHL");
  1750. snd_soc_dapm_ignore_suspend(dapm, "IN2_HPHR");
  1751. snd_soc_dapm_ignore_suspend(dapm, "ADC1_OUTPUT");
  1752. snd_soc_dapm_ignore_suspend(dapm, "ADC2_OUTPUT");
  1753. snd_soc_dapm_ignore_suspend(dapm, "EAR");
  1754. snd_soc_dapm_ignore_suspend(dapm, "LO");
  1755. snd_soc_dapm_ignore_suspend(dapm, "HPHL");
  1756. snd_soc_dapm_ignore_suspend(dapm, "HPHR");
  1757. snd_soc_dapm_ignore_suspend(dapm, "DMIC1_OUTPUT");
  1758. snd_soc_dapm_ignore_suspend(dapm, "DMIC2_OUTPUT");
  1759. snd_soc_dapm_sync(dapm);
  1760. rouleur_init_reg(component);
  1761. rouleur->version = ROULEUR_VERSION_1_0;
  1762. /* Register event notifier */
  1763. rouleur->nblock.notifier_call = rouleur_event_notify;
  1764. if (rouleur->register_notifier) {
  1765. ret = rouleur->register_notifier(rouleur->handle,
  1766. &rouleur->nblock,
  1767. true);
  1768. if (ret) {
  1769. dev_err(component->dev,
  1770. "%s: Failed to register notifier %d\n",
  1771. __func__, ret);
  1772. return ret;
  1773. }
  1774. }
  1775. rouleur->dev_up = true;
  1776. done:
  1777. return ret;
  1778. }
  1779. static void rouleur_soc_codec_remove(struct snd_soc_component *component)
  1780. {
  1781. struct rouleur_priv *rouleur = snd_soc_component_get_drvdata(component);
  1782. if (!rouleur)
  1783. return;
  1784. if (rouleur->register_notifier)
  1785. rouleur->register_notifier(rouleur->handle,
  1786. &rouleur->nblock,
  1787. false);
  1788. }
  1789. static const struct snd_soc_component_driver soc_codec_dev_rouleur = {
  1790. .name = DRV_NAME,
  1791. .probe = rouleur_soc_codec_probe,
  1792. .remove = rouleur_soc_codec_remove,
  1793. .controls = rouleur_snd_controls,
  1794. .num_controls = ARRAY_SIZE(rouleur_snd_controls),
  1795. .dapm_widgets = rouleur_dapm_widgets,
  1796. .num_dapm_widgets = ARRAY_SIZE(rouleur_dapm_widgets),
  1797. .dapm_routes = rouleur_audio_map,
  1798. .num_dapm_routes = ARRAY_SIZE(rouleur_audio_map),
  1799. };
  1800. #ifdef CONFIG_PM_SLEEP
  1801. static int rouleur_suspend(struct device *dev)
  1802. {
  1803. struct rouleur_priv *rouleur = NULL;
  1804. int ret = 0;
  1805. struct rouleur_pdata *pdata = NULL;
  1806. if (!dev)
  1807. return -ENODEV;
  1808. rouleur = dev_get_drvdata(dev);
  1809. if (!rouleur)
  1810. return -EINVAL;
  1811. pdata = dev_get_platdata(rouleur->dev);
  1812. if (!pdata) {
  1813. dev_err(dev, "%s: pdata is NULL\n", __func__);
  1814. return -EINVAL;
  1815. }
  1816. if (test_bit(ALLOW_VPOS_DISABLE, &rouleur->status_mask)) {
  1817. ret = msm_cdc_disable_ondemand_supply(rouleur->dev,
  1818. rouleur->supplies,
  1819. pdata->regulator,
  1820. pdata->num_supplies,
  1821. "cdc-pa-vpos");
  1822. if (ret == -EINVAL) {
  1823. dev_err(dev, "%s: pa vpos is not disabled\n",
  1824. __func__);
  1825. return 0;
  1826. }
  1827. clear_bit(ALLOW_VPOS_DISABLE, &rouleur->status_mask);
  1828. }
  1829. return 0;
  1830. }
  1831. static int rouleur_resume(struct device *dev)
  1832. {
  1833. return 0;
  1834. }
  1835. #endif
  1836. static int rouleur_reset(struct device *dev, int reset_val)
  1837. {
  1838. struct rouleur_priv *rouleur = NULL;
  1839. if (!dev)
  1840. return -ENODEV;
  1841. rouleur = dev_get_drvdata(dev);
  1842. if (!rouleur)
  1843. return -EINVAL;
  1844. pm2250_spmi_write(rouleur->spmi_dev, rouleur->reset_reg, reset_val);
  1845. return 0;
  1846. }
  1847. static int rouleur_read_of_property_u32(struct device *dev, const char *name,
  1848. u32 *val)
  1849. {
  1850. int rc = 0;
  1851. rc = of_property_read_u32(dev->of_node, name, val);
  1852. if (rc)
  1853. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  1854. __func__, name, dev->of_node->full_name);
  1855. return rc;
  1856. }
  1857. static void rouleur_dt_parse_micbias_info(struct device *dev,
  1858. struct rouleur_micbias_setting *mb)
  1859. {
  1860. u32 prop_val = 0;
  1861. int rc = 0;
  1862. /* MB1 */
  1863. if (of_find_property(dev->of_node, "qcom,cdc-micbias1-mv",
  1864. NULL)) {
  1865. rc = rouleur_read_of_property_u32(dev,
  1866. "qcom,cdc-micbias1-mv",
  1867. &prop_val);
  1868. if (!rc)
  1869. mb->micb1_mv = prop_val;
  1870. } else {
  1871. dev_info(dev, "%s: Micbias1 DT property not found\n",
  1872. __func__);
  1873. }
  1874. /* MB2 */
  1875. if (of_find_property(dev->of_node, "qcom,cdc-micbias2-mv",
  1876. NULL)) {
  1877. rc = rouleur_read_of_property_u32(dev,
  1878. "qcom,cdc-micbias2-mv",
  1879. &prop_val);
  1880. if (!rc)
  1881. mb->micb2_mv = prop_val;
  1882. } else {
  1883. dev_info(dev, "%s: Micbias2 DT property not found\n",
  1884. __func__);
  1885. }
  1886. /* MB3 */
  1887. if (of_find_property(dev->of_node, "qcom,cdc-micbias3-mv",
  1888. NULL)) {
  1889. rc = rouleur_read_of_property_u32(dev,
  1890. "qcom,cdc-micbias3-mv",
  1891. &prop_val);
  1892. if (!rc)
  1893. mb->micb3_mv = prop_val;
  1894. } else {
  1895. dev_info(dev, "%s: Micbias3 DT property not found\n",
  1896. __func__);
  1897. }
  1898. }
  1899. struct rouleur_pdata *rouleur_populate_dt_data(struct device *dev)
  1900. {
  1901. struct rouleur_pdata *pdata = NULL;
  1902. u32 reg;
  1903. int ret = 0;
  1904. pdata = kzalloc(sizeof(struct rouleur_pdata),
  1905. GFP_KERNEL);
  1906. if (!pdata)
  1907. return NULL;
  1908. pdata->spmi_np = of_parse_phandle(dev->of_node,
  1909. "qcom,pmic-spmi-node", 0);
  1910. if (!pdata->spmi_np) {
  1911. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  1912. __func__, "qcom,pmic-spmi-node",
  1913. dev->of_node->full_name);
  1914. kfree(pdata);
  1915. return NULL;
  1916. }
  1917. ret = of_property_read_u32(dev->of_node, "qcom,wcd-reset-reg", &reg);
  1918. if (ret) {
  1919. dev_err(dev, "%s: Failed to obtain reset reg value %d\n",
  1920. __func__, ret);
  1921. kfree(pdata);
  1922. return NULL;
  1923. }
  1924. pdata->reset_reg = reg;
  1925. /* Parse power supplies */
  1926. msm_cdc_get_power_supplies(dev, &pdata->regulator,
  1927. &pdata->num_supplies);
  1928. if (!pdata->regulator || (pdata->num_supplies <= 0)) {
  1929. dev_err(dev, "%s: no power supplies defined for codec\n",
  1930. __func__);
  1931. kfree(pdata);
  1932. return NULL;
  1933. }
  1934. pdata->rx_slave = of_parse_phandle(dev->of_node, "qcom,rx-slave", 0);
  1935. pdata->tx_slave = of_parse_phandle(dev->of_node, "qcom,tx-slave", 0);
  1936. rouleur_dt_parse_micbias_info(dev, &pdata->micbias);
  1937. return pdata;
  1938. }
  1939. static int rouleur_wakeup(void *handle, bool enable)
  1940. {
  1941. struct rouleur_priv *priv;
  1942. if (!handle) {
  1943. pr_err("%s: NULL handle\n", __func__);
  1944. return -EINVAL;
  1945. }
  1946. priv = (struct rouleur_priv *)handle;
  1947. if (!priv->tx_swr_dev) {
  1948. pr_err("%s: tx swr dev is NULL\n", __func__);
  1949. return -EINVAL;
  1950. }
  1951. if (enable)
  1952. return swr_device_wakeup_vote(priv->tx_swr_dev);
  1953. else
  1954. return swr_device_wakeup_unvote(priv->tx_swr_dev);
  1955. }
  1956. static irqreturn_t rouleur_wd_handle_irq(int irq, void *data)
  1957. {
  1958. pr_err_ratelimited("%s: Watchdog interrupt for irq =%d triggered\n",
  1959. __func__, irq);
  1960. return IRQ_HANDLED;
  1961. }
  1962. static int rouleur_bind(struct device *dev)
  1963. {
  1964. int ret = 0, i = 0;
  1965. struct rouleur_priv *rouleur = NULL;
  1966. struct rouleur_pdata *pdata = NULL;
  1967. struct wcd_ctrl_platform_data *plat_data = NULL;
  1968. struct platform_device *pdev = NULL;
  1969. rouleur = kzalloc(sizeof(struct rouleur_priv), GFP_KERNEL);
  1970. if (!rouleur)
  1971. return -ENOMEM;
  1972. dev_set_drvdata(dev, rouleur);
  1973. pdata = rouleur_populate_dt_data(dev);
  1974. if (!pdata) {
  1975. dev_err(dev, "%s: Fail to obtain platform data\n", __func__);
  1976. kfree(rouleur);
  1977. return -EINVAL;
  1978. }
  1979. rouleur->dev = dev;
  1980. rouleur->dev->platform_data = pdata;
  1981. pdev = of_find_device_by_node(pdata->spmi_np);
  1982. if (!pdev) {
  1983. dev_err(dev, "%s: platform device from SPMI node is NULL\n",
  1984. __func__);
  1985. ret = -EINVAL;
  1986. goto err_bind_all;
  1987. }
  1988. rouleur->spmi_dev = &pdev->dev;
  1989. rouleur->reset_reg = pdata->reset_reg;
  1990. ret = msm_cdc_init_supplies(dev, &rouleur->supplies,
  1991. pdata->regulator, pdata->num_supplies);
  1992. if (!rouleur->supplies) {
  1993. dev_err(dev, "%s: Cannot init wcd supplies\n",
  1994. __func__);
  1995. goto err_bind_all;
  1996. }
  1997. plat_data = dev_get_platdata(dev->parent);
  1998. if (!plat_data) {
  1999. dev_err(dev, "%s: platform data from parent is NULL\n",
  2000. __func__);
  2001. ret = -EINVAL;
  2002. goto err_bind_all;
  2003. }
  2004. rouleur->handle = (void *)plat_data->handle;
  2005. if (!rouleur->handle) {
  2006. dev_err(dev, "%s: handle is NULL\n", __func__);
  2007. ret = -EINVAL;
  2008. goto err_bind_all;
  2009. }
  2010. rouleur->update_wcd_event = plat_data->update_wcd_event;
  2011. if (!rouleur->update_wcd_event) {
  2012. dev_err(dev, "%s: update_wcd_event api is null!\n",
  2013. __func__);
  2014. ret = -EINVAL;
  2015. goto err_bind_all;
  2016. }
  2017. rouleur->register_notifier = plat_data->register_notifier;
  2018. if (!rouleur->register_notifier) {
  2019. dev_err(dev, "%s: register_notifier api is null!\n",
  2020. __func__);
  2021. ret = -EINVAL;
  2022. goto err_bind_all;
  2023. }
  2024. ret = msm_cdc_enable_static_supplies(dev, rouleur->supplies,
  2025. pdata->regulator,
  2026. pdata->num_supplies);
  2027. if (ret) {
  2028. dev_err(dev, "%s: wcd static supply enable failed!\n",
  2029. __func__);
  2030. goto err_bind_all;
  2031. }
  2032. rouleur_reset(dev, 0x01);
  2033. usleep_range(20, 30);
  2034. rouleur_reset(dev, 0x00);
  2035. /*
  2036. * Add 5msec delay to provide sufficient time for
  2037. * soundwire auto enumeration of slave devices as
  2038. * as per HW requirement.
  2039. */
  2040. usleep_range(5000, 5010);
  2041. rouleur->wakeup = rouleur_wakeup;
  2042. ret = component_bind_all(dev, rouleur);
  2043. if (ret) {
  2044. dev_err(dev, "%s: Slave bind failed, ret = %d\n",
  2045. __func__, ret);
  2046. goto err_bind_all;
  2047. }
  2048. ret = rouleur_parse_port_mapping(dev, "qcom,rx_swr_ch_map", CODEC_RX);
  2049. ret |= rouleur_parse_port_mapping(dev, "qcom,tx_swr_ch_map", CODEC_TX);
  2050. if (ret) {
  2051. dev_err(dev, "Failed to read port mapping\n");
  2052. goto err;
  2053. }
  2054. rouleur->rx_swr_dev = get_matching_swr_slave_device(pdata->rx_slave);
  2055. if (!rouleur->rx_swr_dev) {
  2056. dev_err(dev, "%s: Could not find RX swr slave device\n",
  2057. __func__);
  2058. ret = -ENODEV;
  2059. goto err;
  2060. }
  2061. rouleur->tx_swr_dev = get_matching_swr_slave_device(pdata->tx_slave);
  2062. if (!rouleur->tx_swr_dev) {
  2063. dev_err(dev, "%s: Could not find TX swr slave device\n",
  2064. __func__);
  2065. ret = -ENODEV;
  2066. goto err;
  2067. }
  2068. rouleur->regmap = devm_regmap_init_swr(rouleur->tx_swr_dev,
  2069. &rouleur_regmap_config);
  2070. if (!rouleur->regmap) {
  2071. dev_err(dev, "%s: Regmap init failed\n",
  2072. __func__);
  2073. goto err;
  2074. }
  2075. /* Set all interupts as edge triggered */
  2076. for (i = 0; i < rouleur_regmap_irq_chip.num_regs; i++)
  2077. regmap_write(rouleur->regmap,
  2078. (ROULEUR_DIG_SWR_INTR_LEVEL_0 + i), 0);
  2079. rouleur_regmap_irq_chip.irq_drv_data = rouleur;
  2080. rouleur->irq_info.wcd_regmap_irq_chip = &rouleur_regmap_irq_chip;
  2081. rouleur->irq_info.codec_name = "rouleur";
  2082. rouleur->irq_info.regmap = rouleur->regmap;
  2083. rouleur->irq_info.dev = dev;
  2084. ret = wcd_irq_init(&rouleur->irq_info, &rouleur->virq);
  2085. if (ret) {
  2086. dev_err(dev, "%s: IRQ init failed: %d\n",
  2087. __func__, ret);
  2088. goto err;
  2089. }
  2090. rouleur->tx_swr_dev->slave_irq = rouleur->virq;
  2091. mutex_init(&rouleur->micb_lock);
  2092. mutex_init(&rouleur->main_bias_lock);
  2093. mutex_init(&rouleur->rx_clk_lock);
  2094. ret = rouleur_set_micbias_data(rouleur, pdata);
  2095. if (ret < 0) {
  2096. dev_err(dev, "%s: bad micbias pdata\n", __func__);
  2097. goto err_irq;
  2098. }
  2099. /* Request for watchdog interrupt */
  2100. wcd_request_irq(&rouleur->irq_info, ROULEUR_IRQ_HPHR_PDM_WD_INT,
  2101. "HPHR PDM WD INT", rouleur_wd_handle_irq, NULL);
  2102. wcd_request_irq(&rouleur->irq_info, ROULEUR_IRQ_HPHL_PDM_WD_INT,
  2103. "HPHL PDM WD INT", rouleur_wd_handle_irq, NULL);
  2104. /* Disable watchdog interrupt for HPH */
  2105. wcd_disable_irq(&rouleur->irq_info, ROULEUR_IRQ_HPHR_PDM_WD_INT);
  2106. wcd_disable_irq(&rouleur->irq_info, ROULEUR_IRQ_HPHL_PDM_WD_INT);
  2107. ret = snd_soc_register_component(dev, &soc_codec_dev_rouleur,
  2108. NULL, 0);
  2109. if (ret) {
  2110. dev_err(dev, "%s: Codec registration failed\n",
  2111. __func__);
  2112. goto err_irq;
  2113. }
  2114. return ret;
  2115. err_irq:
  2116. wcd_irq_exit(&rouleur->irq_info, rouleur->virq);
  2117. mutex_destroy(&rouleur->micb_lock);
  2118. mutex_destroy(&rouleur->main_bias_lock);
  2119. mutex_destroy(&rouleur->rx_clk_lock);
  2120. err:
  2121. component_unbind_all(dev, rouleur);
  2122. err_bind_all:
  2123. dev_set_drvdata(dev, NULL);
  2124. kfree(pdata);
  2125. kfree(rouleur);
  2126. return ret;
  2127. }
  2128. static void rouleur_unbind(struct device *dev)
  2129. {
  2130. struct rouleur_priv *rouleur = dev_get_drvdata(dev);
  2131. struct rouleur_pdata *pdata = dev_get_platdata(rouleur->dev);
  2132. wcd_irq_exit(&rouleur->irq_info, rouleur->virq);
  2133. snd_soc_unregister_component(dev);
  2134. component_unbind_all(dev, rouleur);
  2135. mutex_destroy(&rouleur->micb_lock);
  2136. mutex_destroy(&rouleur->main_bias_lock);
  2137. mutex_destroy(&rouleur->rx_clk_lock);
  2138. dev_set_drvdata(dev, NULL);
  2139. kfree(pdata);
  2140. kfree(rouleur);
  2141. }
  2142. static const struct of_device_id rouleur_dt_match[] = {
  2143. { .compatible = "qcom,rouleur-codec" , .data = "rouleur" },
  2144. {}
  2145. };
  2146. static const struct component_master_ops rouleur_comp_ops = {
  2147. .bind = rouleur_bind,
  2148. .unbind = rouleur_unbind,
  2149. };
  2150. static int rouleur_compare_of(struct device *dev, void *data)
  2151. {
  2152. return dev->of_node == data;
  2153. }
  2154. static void rouleur_release_of(struct device *dev, void *data)
  2155. {
  2156. of_node_put(data);
  2157. }
  2158. static int rouleur_add_slave_components(struct device *dev,
  2159. struct component_match **matchptr)
  2160. {
  2161. struct device_node *np, *rx_node, *tx_node;
  2162. np = dev->of_node;
  2163. rx_node = of_parse_phandle(np, "qcom,rx-slave", 0);
  2164. if (!rx_node) {
  2165. dev_err(dev, "%s: Rx-slave node not defined\n", __func__);
  2166. return -ENODEV;
  2167. }
  2168. of_node_get(rx_node);
  2169. component_match_add_release(dev, matchptr,
  2170. rouleur_release_of,
  2171. rouleur_compare_of,
  2172. rx_node);
  2173. tx_node = of_parse_phandle(np, "qcom,tx-slave", 0);
  2174. if (!tx_node) {
  2175. dev_err(dev, "%s: Tx-slave node not defined\n", __func__);
  2176. return -ENODEV;
  2177. }
  2178. of_node_get(tx_node);
  2179. component_match_add_release(dev, matchptr,
  2180. rouleur_release_of,
  2181. rouleur_compare_of,
  2182. tx_node);
  2183. return 0;
  2184. }
  2185. static int rouleur_probe(struct platform_device *pdev)
  2186. {
  2187. struct component_match *match = NULL;
  2188. int ret;
  2189. ret = rouleur_add_slave_components(&pdev->dev, &match);
  2190. if (ret)
  2191. return ret;
  2192. return component_master_add_with_match(&pdev->dev,
  2193. &rouleur_comp_ops, match);
  2194. }
  2195. static int rouleur_remove(struct platform_device *pdev)
  2196. {
  2197. component_master_del(&pdev->dev, &rouleur_comp_ops);
  2198. dev_set_drvdata(&pdev->dev, NULL);
  2199. return 0;
  2200. }
  2201. #ifdef CONFIG_PM_SLEEP
  2202. static const struct dev_pm_ops rouleur_dev_pm_ops = {
  2203. SET_SYSTEM_SLEEP_PM_OPS(
  2204. rouleur_suspend,
  2205. rouleur_resume
  2206. )
  2207. };
  2208. #endif
  2209. static struct platform_driver rouleur_codec_driver = {
  2210. .probe = rouleur_probe,
  2211. .remove = rouleur_remove,
  2212. .driver = {
  2213. .name = "rouleur_codec",
  2214. .owner = THIS_MODULE,
  2215. .of_match_table = of_match_ptr(rouleur_dt_match),
  2216. #ifdef CONFIG_PM_SLEEP
  2217. .pm = &rouleur_dev_pm_ops,
  2218. #endif
  2219. .suppress_bind_attrs = true,
  2220. },
  2221. };
  2222. module_platform_driver(rouleur_codec_driver);
  2223. MODULE_DESCRIPTION("Rouleur Codec driver");
  2224. MODULE_LICENSE("GPL v2");