wcd937x.c 59 KB

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