wcd937x.c 64 KB

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