bolero-cdc.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/of_platform.h>
  5. #include <linux/module.h>
  6. #include <linux/io.h>
  7. #include <linux/init.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/printk.h>
  10. #include <linux/delay.h>
  11. #include <linux/kernel.h>
  12. #include <linux/clk.h>
  13. #include <soc/snd_event.h>
  14. #include <linux/pm_runtime.h>
  15. #include <soc/swr-common.h>
  16. #include <dsp/digital-cdc-rsc-mgr.h>
  17. #include "bolero-cdc.h"
  18. #include "internal.h"
  19. #include "bolero-clk-rsc.h"
  20. #define DRV_NAME "bolero_codec"
  21. #define BOLERO_VERSION_ENTRY_SIZE 32
  22. #define BOLERO_CDC_STRING_LEN 80
  23. static const struct snd_soc_component_driver bolero;
  24. /* pm runtime auto suspend timer in msecs */
  25. #define BOLERO_AUTO_SUSPEND_DELAY 100 /* delay in msec */
  26. /* MCLK_MUX table for all macros */
  27. static u16 bolero_mclk_mux_tbl[MAX_MACRO][MCLK_MUX_MAX] = {
  28. {TX_MACRO, VA_MACRO},
  29. {TX_MACRO, RX_MACRO},
  30. {TX_MACRO, WSA_MACRO},
  31. {TX_MACRO, VA_MACRO},
  32. };
  33. static bool bolero_is_valid_codec_dev(struct device *dev);
  34. int bolero_set_port_map(struct snd_soc_component *component,
  35. u32 size, void *data)
  36. {
  37. struct bolero_priv *priv = NULL;
  38. struct swr_mstr_port_map *map = NULL;
  39. u16 idx;
  40. if (!component || (size == 0) || !data)
  41. return -EINVAL;
  42. priv = snd_soc_component_get_drvdata(component);
  43. if (!priv)
  44. return -EINVAL;
  45. if (!bolero_is_valid_codec_dev(priv->dev)) {
  46. dev_err(priv->dev, "%s: invalid codec\n", __func__);
  47. return -EINVAL;
  48. }
  49. map = (struct swr_mstr_port_map *)data;
  50. for (idx = 0; idx < size; idx++) {
  51. if (priv->macro_params[map->id].set_port_map)
  52. priv->macro_params[map->id].set_port_map(component,
  53. map->uc,
  54. SWR_MSTR_PORT_LEN,
  55. map->swr_port_params);
  56. map += 1;
  57. }
  58. return 0;
  59. }
  60. EXPORT_SYMBOL(bolero_set_port_map);
  61. static void bolero_ahb_write_device(char __iomem *io_base,
  62. u16 reg, u8 value)
  63. {
  64. u32 temp = (u32)(value) & 0x000000FF;
  65. iowrite32(temp, io_base + reg);
  66. }
  67. static void bolero_ahb_read_device(char __iomem *io_base,
  68. u16 reg, u8 *value)
  69. {
  70. u32 temp;
  71. temp = ioread32(io_base + reg);
  72. *value = (u8)temp;
  73. }
  74. static int __bolero_reg_read(struct bolero_priv *priv,
  75. u16 macro_id, u16 reg, u8 *val)
  76. {
  77. int ret = 0;
  78. mutex_lock(&priv->clk_lock);
  79. if (!priv->dev_up) {
  80. dev_dbg_ratelimited(priv->dev,
  81. "%s: SSR in progress, exit\n", __func__);
  82. ret = -EINVAL;
  83. goto ssr_err;
  84. }
  85. if (priv->macro_params[VA_MACRO].dev) {
  86. pm_runtime_get_sync(priv->macro_params[VA_MACRO].dev);
  87. if (!bolero_check_core_votes(priv->macro_params[VA_MACRO].dev))
  88. goto ssr_err;
  89. }
  90. if (priv->version < BOLERO_VERSION_2_0) {
  91. /* Request Clk before register access */
  92. ret = bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  93. priv->macro_params[macro_id].default_clk_id,
  94. priv->macro_params[macro_id].clk_id_req,
  95. true);
  96. if (ret < 0) {
  97. dev_err_ratelimited(priv->dev,
  98. "%s: Failed to enable clock, ret:%d\n",
  99. __func__, ret);
  100. goto err;
  101. }
  102. }
  103. bolero_ahb_read_device(
  104. priv->macro_params[macro_id].io_base, reg, val);
  105. if (priv->version < BOLERO_VERSION_2_0)
  106. bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  107. priv->macro_params[macro_id].default_clk_id,
  108. priv->macro_params[macro_id].clk_id_req,
  109. false);
  110. err:
  111. if (priv->macro_params[VA_MACRO].dev) {
  112. pm_runtime_mark_last_busy(priv->macro_params[VA_MACRO].dev);
  113. pm_runtime_put_autosuspend(priv->macro_params[VA_MACRO].dev);
  114. }
  115. ssr_err:
  116. mutex_unlock(&priv->clk_lock);
  117. return ret;
  118. }
  119. static int __bolero_reg_write(struct bolero_priv *priv,
  120. u16 macro_id, u16 reg, u8 val)
  121. {
  122. int ret = 0;
  123. mutex_lock(&priv->clk_lock);
  124. if (!priv->dev_up) {
  125. dev_dbg_ratelimited(priv->dev,
  126. "%s: SSR in progress, exit\n", __func__);
  127. ret = -EINVAL;
  128. goto ssr_err;
  129. }
  130. if (priv->macro_params[VA_MACRO].dev) {
  131. pm_runtime_get_sync(priv->macro_params[VA_MACRO].dev);
  132. if (!bolero_check_core_votes(priv->macro_params[VA_MACRO].dev))
  133. goto ssr_err;
  134. }
  135. if (priv->version < BOLERO_VERSION_2_0) {
  136. /* Request Clk before register access */
  137. ret = bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  138. priv->macro_params[macro_id].default_clk_id,
  139. priv->macro_params[macro_id].clk_id_req,
  140. true);
  141. if (ret < 0) {
  142. dev_err_ratelimited(priv->dev,
  143. "%s: Failed to enable clock, ret:%d\n",
  144. __func__, ret);
  145. goto err;
  146. }
  147. }
  148. bolero_ahb_write_device(
  149. priv->macro_params[macro_id].io_base, reg, val);
  150. if (priv->version < BOLERO_VERSION_2_0)
  151. bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  152. priv->macro_params[macro_id].default_clk_id,
  153. priv->macro_params[macro_id].clk_id_req,
  154. false);
  155. err:
  156. if (priv->macro_params[VA_MACRO].dev) {
  157. pm_runtime_mark_last_busy(priv->macro_params[VA_MACRO].dev);
  158. pm_runtime_put_autosuspend(priv->macro_params[VA_MACRO].dev);
  159. }
  160. ssr_err:
  161. mutex_unlock(&priv->clk_lock);
  162. return ret;
  163. }
  164. static int bolero_cdc_update_wcd_event(void *handle, u16 event, u32 data)
  165. {
  166. struct bolero_priv *priv = (struct bolero_priv *)handle;
  167. if (!priv) {
  168. pr_err("%s:Invalid bolero priv handle\n", __func__);
  169. return -EINVAL;
  170. }
  171. switch (event) {
  172. case WCD_BOLERO_EVT_RX_MUTE:
  173. if (priv->macro_params[RX_MACRO].event_handler)
  174. priv->macro_params[RX_MACRO].event_handler(
  175. priv->component,
  176. BOLERO_MACRO_EVT_RX_MUTE, data);
  177. break;
  178. case WCD_BOLERO_EVT_IMPED_TRUE:
  179. if (priv->macro_params[RX_MACRO].event_handler)
  180. priv->macro_params[RX_MACRO].event_handler(
  181. priv->component,
  182. BOLERO_MACRO_EVT_IMPED_TRUE, data);
  183. break;
  184. case WCD_BOLERO_EVT_IMPED_FALSE:
  185. if (priv->macro_params[RX_MACRO].event_handler)
  186. priv->macro_params[RX_MACRO].event_handler(
  187. priv->component,
  188. BOLERO_MACRO_EVT_IMPED_FALSE, data);
  189. break;
  190. case WCD_BOLERO_EVT_RX_COMPANDER_SOFT_RST:
  191. if (priv->macro_params[RX_MACRO].event_handler)
  192. priv->macro_params[RX_MACRO].event_handler(
  193. priv->component,
  194. BOLERO_MACRO_EVT_RX_COMPANDER_SOFT_RST, data);
  195. break;
  196. case WCD_BOLERO_EVT_BCS_CLK_OFF:
  197. if (priv->macro_params[TX_MACRO].event_handler)
  198. priv->macro_params[TX_MACRO].event_handler(
  199. priv->component,
  200. BOLERO_MACRO_EVT_BCS_CLK_OFF, data);
  201. break;
  202. case WCD_BOLERO_EVT_RX_PA_GAIN_UPDATE:
  203. /* Update PA Gain only for bolero version 2.1 */
  204. if (priv->version == BOLERO_VERSION_2_1)
  205. if (priv->macro_params[RX_MACRO].event_handler)
  206. priv->macro_params[RX_MACRO].event_handler(
  207. priv->component,
  208. BOLERO_MACRO_EVT_RX_PA_GAIN_UPDATE,
  209. data);
  210. break;
  211. case WCD_BOLERO_EVT_HPHL_HD2_ENABLE:
  212. if (priv->macro_params[RX_MACRO].event_handler)
  213. priv->macro_params[RX_MACRO].event_handler(
  214. priv->component,
  215. BOLERO_MACRO_EVT_HPHL_HD2_ENABLE, data);
  216. break;
  217. case WCD_BOLERO_EVT_HPHR_HD2_ENABLE:
  218. if (priv->macro_params[RX_MACRO].event_handler)
  219. priv->macro_params[RX_MACRO].event_handler(
  220. priv->component,
  221. BOLERO_MACRO_EVT_HPHR_HD2_ENABLE, data);
  222. break;
  223. default:
  224. dev_err(priv->dev, "%s: Invalid event %d trigger from wcd\n",
  225. __func__, event);
  226. return -EINVAL;
  227. }
  228. return 0;
  229. }
  230. static int bolero_cdc_register_notifier(void *handle,
  231. struct notifier_block *nblock,
  232. bool enable)
  233. {
  234. struct bolero_priv *priv = (struct bolero_priv *)handle;
  235. if (!priv) {
  236. pr_err("%s: bolero priv is null\n", __func__);
  237. return -EINVAL;
  238. }
  239. if (enable)
  240. return blocking_notifier_chain_register(&priv->notifier,
  241. nblock);
  242. return blocking_notifier_chain_unregister(&priv->notifier,
  243. nblock);
  244. }
  245. static void bolero_cdc_notifier_call(struct bolero_priv *priv,
  246. u32 data)
  247. {
  248. dev_dbg(priv->dev, "%s: notifier call, data:%d\n", __func__, data);
  249. blocking_notifier_call_chain(&priv->notifier,
  250. data, (void *)priv->wcd_dev);
  251. }
  252. static bool bolero_is_valid_child_dev(struct device *dev)
  253. {
  254. if (of_device_is_compatible(dev->parent->of_node, "qcom,bolero-codec"))
  255. return true;
  256. return false;
  257. }
  258. static bool bolero_is_valid_codec_dev(struct device *dev)
  259. {
  260. if (of_device_is_compatible(dev->of_node, "qcom,bolero-codec"))
  261. return true;
  262. return false;
  263. }
  264. /**
  265. * bolero_clear_amic_tx_hold - clears AMIC register on analog codec
  266. *
  267. * @dev: bolero device ptr.
  268. *
  269. */
  270. void bolero_clear_amic_tx_hold(struct device *dev, u16 adc_n)
  271. {
  272. struct bolero_priv *priv;
  273. u16 event;
  274. u16 amic = 0;
  275. if (!dev) {
  276. pr_err("%s: dev is null\n", __func__);
  277. return;
  278. }
  279. if (!bolero_is_valid_codec_dev(dev)) {
  280. pr_err("%s: invalid codec\n", __func__);
  281. return;
  282. }
  283. priv = dev_get_drvdata(dev);
  284. if (!priv) {
  285. dev_err(dev, "%s: priv is null\n", __func__);
  286. return;
  287. }
  288. event = BOLERO_WCD_EVT_TX_CH_HOLD_CLEAR;
  289. if (adc_n == BOLERO_ADC0)
  290. amic = 0x1;
  291. else if (adc_n == BOLERO_ADC1)
  292. amic = 0x2;
  293. else if (adc_n == BOLERO_ADC2)
  294. amic = 0x2;
  295. else if (adc_n == BOLERO_ADC3)
  296. amic = 0x3;
  297. else
  298. return;
  299. bolero_cdc_notifier_call(priv, (amic << 0x10 | event));
  300. }
  301. EXPORT_SYMBOL(bolero_clear_amic_tx_hold);
  302. /**
  303. * bolero_get_device_ptr - Get child or macro device ptr
  304. *
  305. * @dev: bolero device ptr.
  306. * @macro_id: ID of macro calling this API.
  307. *
  308. * Returns dev ptr on success or NULL on error.
  309. */
  310. struct device *bolero_get_device_ptr(struct device *dev, u16 macro_id)
  311. {
  312. struct bolero_priv *priv;
  313. if (!dev) {
  314. pr_err("%s: dev is null\n", __func__);
  315. return NULL;
  316. }
  317. if (!bolero_is_valid_codec_dev(dev)) {
  318. pr_err("%s: invalid codec\n", __func__);
  319. return NULL;
  320. }
  321. priv = dev_get_drvdata(dev);
  322. if (!priv || (macro_id >= MAX_MACRO)) {
  323. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  324. return NULL;
  325. }
  326. return priv->macro_params[macro_id].dev;
  327. }
  328. EXPORT_SYMBOL(bolero_get_device_ptr);
  329. /**
  330. * bolero_get_rsc_clk_device_ptr - Get rsc clk device ptr
  331. *
  332. * @dev: bolero device ptr.
  333. *
  334. * Returns dev ptr on success or NULL on error.
  335. */
  336. struct device *bolero_get_rsc_clk_device_ptr(struct device *dev)
  337. {
  338. struct bolero_priv *priv;
  339. if (!dev) {
  340. pr_err("%s: dev is null\n", __func__);
  341. return NULL;
  342. }
  343. if (!bolero_is_valid_codec_dev(dev)) {
  344. pr_err("%s: invalid codec\n", __func__);
  345. return NULL;
  346. }
  347. priv = dev_get_drvdata(dev);
  348. if (!priv) {
  349. dev_err(dev, "%s: priv is null\n", __func__);
  350. return NULL;
  351. }
  352. return priv->clk_dev;
  353. }
  354. EXPORT_SYMBOL(bolero_get_rsc_clk_device_ptr);
  355. static int bolero_copy_dais_from_macro(struct bolero_priv *priv)
  356. {
  357. struct snd_soc_dai_driver *dai_ptr;
  358. u16 macro_idx;
  359. /* memcpy into bolero_dais all macro dais */
  360. if (!priv->bolero_dais)
  361. priv->bolero_dais = devm_kzalloc(priv->dev,
  362. priv->num_dais *
  363. sizeof(
  364. struct snd_soc_dai_driver),
  365. GFP_KERNEL);
  366. if (!priv->bolero_dais)
  367. return -ENOMEM;
  368. dai_ptr = priv->bolero_dais;
  369. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  370. if (priv->macro_params[macro_idx].dai_ptr) {
  371. memcpy(dai_ptr,
  372. priv->macro_params[macro_idx].dai_ptr,
  373. priv->macro_params[macro_idx].num_dais *
  374. sizeof(struct snd_soc_dai_driver));
  375. dai_ptr += priv->macro_params[macro_idx].num_dais;
  376. }
  377. }
  378. return 0;
  379. }
  380. /**
  381. * bolero_register_res_clk - Registers rsc clk driver to bolero
  382. *
  383. * @dev: rsc clk device ptr.
  384. * @rsc_clk_cb: event handler callback for notifications like SSR
  385. *
  386. * Returns 0 on success or -EINVAL on error.
  387. */
  388. int bolero_register_res_clk(struct device *dev, rsc_clk_cb_t rsc_clk_cb)
  389. {
  390. struct bolero_priv *priv;
  391. if (!dev || !rsc_clk_cb) {
  392. pr_err("%s: dev or rsc_clk_cb is null\n", __func__);
  393. return -EINVAL;
  394. }
  395. if (!bolero_is_valid_child_dev(dev)) {
  396. dev_err(dev, "%s: child device :%pK not added yet\n",
  397. __func__, dev);
  398. return -EINVAL;
  399. }
  400. priv = dev_get_drvdata(dev->parent);
  401. if (!priv) {
  402. dev_err(dev, "%s: priv is null\n", __func__);
  403. return -EINVAL;
  404. }
  405. priv->clk_dev = dev;
  406. priv->rsc_clk_cb = rsc_clk_cb;
  407. return 0;
  408. }
  409. EXPORT_SYMBOL(bolero_register_res_clk);
  410. /**
  411. * bolero_unregister_res_clk - Unregisters rsc clk driver from bolero
  412. *
  413. * @dev: resource clk device ptr.
  414. */
  415. void bolero_unregister_res_clk(struct device *dev)
  416. {
  417. struct bolero_priv *priv;
  418. if (!dev) {
  419. pr_err("%s: dev is NULL\n", __func__);
  420. return;
  421. }
  422. if (!bolero_is_valid_child_dev(dev)) {
  423. dev_err(dev, "%s: child device :%pK not added\n",
  424. __func__, dev);
  425. return;
  426. }
  427. priv = dev_get_drvdata(dev->parent);
  428. if (!priv) {
  429. dev_err(dev, "%s: priv is null\n", __func__);
  430. return;
  431. }
  432. priv->clk_dev = NULL;
  433. priv->rsc_clk_cb = NULL;
  434. }
  435. EXPORT_SYMBOL(bolero_unregister_res_clk);
  436. static u8 bolero_dmic_clk_div_get(struct snd_soc_component *component,
  437. int mode)
  438. {
  439. struct bolero_priv* priv = snd_soc_component_get_drvdata(component);
  440. int macro = (mode ? VA_MACRO : TX_MACRO);
  441. int ret = 0;
  442. if (priv->macro_params[macro].clk_div_get) {
  443. ret = priv->macro_params[macro].clk_div_get(component);
  444. if (ret > 0)
  445. return ret;
  446. }
  447. return 1;
  448. }
  449. int bolero_dmic_clk_enable(struct snd_soc_component *component,
  450. u32 dmic, u32 tx_mode, bool enable)
  451. {
  452. struct bolero_priv* priv = snd_soc_component_get_drvdata(component);
  453. u8 dmic_clk_en = 0x01;
  454. u16 dmic_clk_reg = 0;
  455. s32 *dmic_clk_cnt = NULL;
  456. u8 *dmic_clk_div = NULL;
  457. u8 freq_change_mask = 0;
  458. u8 clk_div = 0;
  459. dev_dbg(component->dev, "%s: enable: %d, tx_mode:%d, dmic: %d\n",
  460. __func__, enable, tx_mode, dmic);
  461. switch (dmic) {
  462. case 0:
  463. case 1:
  464. dmic_clk_cnt = &(priv->dmic_0_1_clk_cnt);
  465. dmic_clk_div = &(priv->dmic_0_1_clk_div);
  466. dmic_clk_reg = BOLERO_CDC_VA_TOP_CSR_DMIC0_CTL;
  467. freq_change_mask = 0x01;
  468. break;
  469. case 2:
  470. case 3:
  471. dmic_clk_cnt = &(priv->dmic_2_3_clk_cnt);
  472. dmic_clk_div = &(priv->dmic_2_3_clk_div);
  473. dmic_clk_reg = BOLERO_CDC_VA_TOP_CSR_DMIC1_CTL;
  474. freq_change_mask = 0x02;
  475. break;
  476. case 4:
  477. case 5:
  478. dmic_clk_cnt = &(priv->dmic_4_5_clk_cnt);
  479. dmic_clk_div = &(priv->dmic_4_5_clk_div);
  480. dmic_clk_reg = BOLERO_CDC_VA_TOP_CSR_DMIC2_CTL;
  481. freq_change_mask = 0x04;
  482. break;
  483. case 6:
  484. case 7:
  485. dmic_clk_cnt = &(priv->dmic_6_7_clk_cnt);
  486. dmic_clk_div = &(priv->dmic_6_7_clk_div);
  487. dmic_clk_reg = BOLERO_CDC_VA_TOP_CSR_DMIC3_CTL;
  488. freq_change_mask = 0x08;
  489. break;
  490. default:
  491. dev_err(component->dev, "%s: Invalid DMIC Selection\n",
  492. __func__);
  493. return -EINVAL;
  494. }
  495. dev_dbg(component->dev, "%s: DMIC%d dmic_clk_cnt %d\n",
  496. __func__, dmic, *dmic_clk_cnt);
  497. if (enable) {
  498. clk_div = bolero_dmic_clk_div_get(component, tx_mode);
  499. (*dmic_clk_cnt)++;
  500. if (*dmic_clk_cnt == 1) {
  501. snd_soc_component_update_bits(component,
  502. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  503. 0x80, 0x00);
  504. snd_soc_component_update_bits(component, dmic_clk_reg,
  505. 0x0E, clk_div << 0x1);
  506. snd_soc_component_update_bits(component, dmic_clk_reg,
  507. dmic_clk_en, dmic_clk_en);
  508. } else {
  509. if (*dmic_clk_div > clk_div) {
  510. snd_soc_component_update_bits(component,
  511. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  512. freq_change_mask, freq_change_mask);
  513. snd_soc_component_update_bits(component, dmic_clk_reg,
  514. 0x0E, clk_div << 0x1);
  515. snd_soc_component_update_bits(component,
  516. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  517. freq_change_mask, 0x00);
  518. } else {
  519. clk_div = *dmic_clk_div;
  520. }
  521. }
  522. *dmic_clk_div = clk_div;
  523. } else {
  524. (*dmic_clk_cnt)--;
  525. if (*dmic_clk_cnt == 0) {
  526. snd_soc_component_update_bits(component, dmic_clk_reg,
  527. dmic_clk_en, 0);
  528. clk_div = 0;
  529. snd_soc_component_update_bits(component, dmic_clk_reg,
  530. 0x0E, clk_div << 0x1);
  531. } else {
  532. clk_div = bolero_dmic_clk_div_get(component, tx_mode);
  533. if (*dmic_clk_div > clk_div) {
  534. clk_div = bolero_dmic_clk_div_get(component, !tx_mode);
  535. snd_soc_component_update_bits(component,
  536. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  537. freq_change_mask, freq_change_mask);
  538. snd_soc_component_update_bits(component, dmic_clk_reg,
  539. 0x0E, clk_div << 0x1);
  540. snd_soc_component_update_bits(component,
  541. BOLERO_CDC_VA_TOP_CSR_DMIC_CFG,
  542. freq_change_mask, 0x00);
  543. } else {
  544. clk_div = *dmic_clk_div;
  545. }
  546. }
  547. *dmic_clk_div = clk_div;
  548. }
  549. return 0;
  550. }
  551. EXPORT_SYMBOL(bolero_dmic_clk_enable);
  552. bool bolero_is_va_macro_registered(struct device *dev)
  553. {
  554. struct bolero_priv *priv;
  555. if (!dev) {
  556. pr_err("%s: dev is null\n", __func__);
  557. return false;
  558. }
  559. if (!bolero_is_valid_child_dev(dev)) {
  560. dev_err(dev, "%s: child device calling is not added yet\n",
  561. __func__);
  562. return false;
  563. }
  564. priv = dev_get_drvdata(dev->parent);
  565. if (!priv) {
  566. dev_err(dev, "%s: priv is null\n", __func__);
  567. return false;
  568. }
  569. return priv->macros_supported[VA_MACRO];
  570. }
  571. EXPORT_SYMBOL(bolero_is_va_macro_registered);
  572. /**
  573. * bolero_register_macro - Registers macro to bolero
  574. *
  575. * @dev: macro device ptr.
  576. * @macro_id: ID of macro calling this API.
  577. * @ops: macro params to register.
  578. *
  579. * Returns 0 on success or -EINVAL on error.
  580. */
  581. int bolero_register_macro(struct device *dev, u16 macro_id,
  582. struct macro_ops *ops)
  583. {
  584. struct bolero_priv *priv;
  585. int ret = -EINVAL;
  586. if (!dev || !ops) {
  587. pr_err("%s: dev or ops is null\n", __func__);
  588. return -EINVAL;
  589. }
  590. if (!bolero_is_valid_child_dev(dev)) {
  591. dev_err(dev, "%s: child device for macro:%d not added yet\n",
  592. __func__, macro_id);
  593. return -EINVAL;
  594. }
  595. priv = dev_get_drvdata(dev->parent);
  596. if (!priv || (macro_id >= MAX_MACRO)) {
  597. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  598. return -EINVAL;
  599. }
  600. priv->macro_params[macro_id].clk_id_req = ops->clk_id_req;
  601. priv->macro_params[macro_id].default_clk_id = ops->default_clk_id;
  602. priv->macro_params[macro_id].init = ops->init;
  603. priv->macro_params[macro_id].exit = ops->exit;
  604. priv->macro_params[macro_id].io_base = ops->io_base;
  605. priv->macro_params[macro_id].num_dais = ops->num_dais;
  606. priv->macro_params[macro_id].dai_ptr = ops->dai_ptr;
  607. priv->macro_params[macro_id].event_handler = ops->event_handler;
  608. priv->macro_params[macro_id].set_port_map = ops->set_port_map;
  609. priv->macro_params[macro_id].dev = dev;
  610. priv->current_mclk_mux_macro[macro_id] =
  611. bolero_mclk_mux_tbl[macro_id][MCLK_MUX0];
  612. if (macro_id == TX_MACRO) {
  613. priv->macro_params[macro_id].reg_wake_irq = ops->reg_wake_irq;
  614. priv->macro_params[macro_id].clk_switch = ops->clk_switch;
  615. priv->macro_params[macro_id].reg_evt_listener =
  616. ops->reg_evt_listener;
  617. priv->macro_params[macro_id].clk_enable = ops->clk_enable;
  618. }
  619. if (macro_id == TX_MACRO || macro_id == VA_MACRO)
  620. priv->macro_params[macro_id].clk_div_get = ops->clk_div_get;
  621. if (priv->version == BOLERO_VERSION_2_1) {
  622. if (macro_id == VA_MACRO)
  623. priv->macro_params[macro_id].reg_wake_irq =
  624. ops->reg_wake_irq;
  625. }
  626. priv->num_dais += ops->num_dais;
  627. priv->num_macros_registered++;
  628. priv->macros_supported[macro_id] = true;
  629. dev_info(dev, "%s: register macro successful:%d\n", __func__, macro_id);
  630. if (priv->num_macros_registered == priv->num_macros) {
  631. ret = bolero_copy_dais_from_macro(priv);
  632. if (ret < 0) {
  633. dev_err(dev, "%s: copy_dais failed\n", __func__);
  634. return ret;
  635. }
  636. if (priv->macros_supported[TX_MACRO] == false) {
  637. bolero_mclk_mux_tbl[WSA_MACRO][MCLK_MUX0] = WSA_MACRO;
  638. priv->current_mclk_mux_macro[WSA_MACRO] = WSA_MACRO;
  639. bolero_mclk_mux_tbl[VA_MACRO][MCLK_MUX0] = VA_MACRO;
  640. priv->current_mclk_mux_macro[VA_MACRO] = VA_MACRO;
  641. }
  642. ret = snd_soc_register_component(dev->parent, &bolero,
  643. priv->bolero_dais, priv->num_dais);
  644. if (ret < 0) {
  645. dev_err(dev, "%s: register codec failed\n", __func__);
  646. return ret;
  647. }
  648. }
  649. return 0;
  650. }
  651. EXPORT_SYMBOL(bolero_register_macro);
  652. /**
  653. * bolero_unregister_macro - De-Register macro from bolero
  654. *
  655. * @dev: macro device ptr.
  656. * @macro_id: ID of macro calling this API.
  657. *
  658. */
  659. void bolero_unregister_macro(struct device *dev, u16 macro_id)
  660. {
  661. struct bolero_priv *priv;
  662. if (!dev) {
  663. pr_err("%s: dev is null\n", __func__);
  664. return;
  665. }
  666. if (!bolero_is_valid_child_dev(dev)) {
  667. dev_err(dev, "%s: macro:%d not in valid registered macro-list\n",
  668. __func__, macro_id);
  669. return;
  670. }
  671. priv = dev_get_drvdata(dev->parent);
  672. if (!priv || (macro_id >= MAX_MACRO)) {
  673. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  674. return;
  675. }
  676. priv->macro_params[macro_id].init = NULL;
  677. priv->macro_params[macro_id].num_dais = 0;
  678. priv->macro_params[macro_id].dai_ptr = NULL;
  679. priv->macro_params[macro_id].event_handler = NULL;
  680. priv->macro_params[macro_id].dev = NULL;
  681. if (macro_id == TX_MACRO) {
  682. priv->macro_params[macro_id].reg_wake_irq = NULL;
  683. priv->macro_params[macro_id].clk_switch = NULL;
  684. priv->macro_params[macro_id].reg_evt_listener = NULL;
  685. priv->macro_params[macro_id].clk_enable = NULL;
  686. }
  687. if (macro_id == TX_MACRO || macro_id == VA_MACRO)
  688. priv->macro_params[macro_id].clk_div_get = NULL;
  689. priv->num_dais -= priv->macro_params[macro_id].num_dais;
  690. priv->num_macros_registered--;
  691. /* UNREGISTER CODEC HERE */
  692. if (priv->num_macros - 1 == priv->num_macros_registered)
  693. snd_soc_unregister_component(dev->parent);
  694. }
  695. EXPORT_SYMBOL(bolero_unregister_macro);
  696. void bolero_wsa_pa_on(struct device *dev, bool adie_lb)
  697. {
  698. struct bolero_priv *priv;
  699. if (!dev) {
  700. pr_err("%s: dev is null\n", __func__);
  701. return;
  702. }
  703. if (!bolero_is_valid_child_dev(dev)) {
  704. dev_err(dev, "%s: not a valid child dev\n",
  705. __func__);
  706. return;
  707. }
  708. priv = dev_get_drvdata(dev->parent);
  709. if (!priv) {
  710. dev_err(dev, "%s: priv is null\n", __func__);
  711. return;
  712. }
  713. if (adie_lb)
  714. bolero_cdc_notifier_call(priv,
  715. BOLERO_WCD_EVT_PA_ON_POST_FSCLK_ADIE_LB);
  716. else
  717. bolero_cdc_notifier_call(priv,
  718. BOLERO_WCD_EVT_PA_ON_POST_FSCLK);
  719. }
  720. EXPORT_SYMBOL(bolero_wsa_pa_on);
  721. int bolero_get_version(struct device *dev)
  722. {
  723. struct bolero_priv *priv;
  724. if (!dev) {
  725. pr_err("%s: dev is null\n", __func__);
  726. return -EINVAL;
  727. }
  728. if (!bolero_is_valid_child_dev(dev)) {
  729. dev_err(dev, "%s: child device for macro not added yet\n",
  730. __func__);
  731. return -EINVAL;
  732. }
  733. priv = dev_get_drvdata(dev->parent);
  734. if (!priv) {
  735. dev_err(dev, "%s: priv is null\n", __func__);
  736. return -EINVAL;
  737. }
  738. return priv->version;
  739. }
  740. EXPORT_SYMBOL(bolero_get_version);
  741. static ssize_t bolero_version_read(struct snd_info_entry *entry,
  742. void *file_private_data,
  743. struct file *file,
  744. char __user *buf, size_t count,
  745. loff_t pos)
  746. {
  747. struct bolero_priv *priv;
  748. char buffer[BOLERO_VERSION_ENTRY_SIZE];
  749. int len = 0;
  750. priv = (struct bolero_priv *) entry->private_data;
  751. if (!priv) {
  752. pr_err("%s: bolero priv is null\n", __func__);
  753. return -EINVAL;
  754. }
  755. switch (priv->version) {
  756. case BOLERO_VERSION_1_0:
  757. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_0\n");
  758. break;
  759. case BOLERO_VERSION_1_1:
  760. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_1\n");
  761. break;
  762. case BOLERO_VERSION_1_2:
  763. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_2\n");
  764. break;
  765. case BOLERO_VERSION_2_0:
  766. len = snprintf(buffer, sizeof(buffer), "BOLERO_2_0\n");
  767. break;
  768. case BOLERO_VERSION_2_1:
  769. len = snprintf(buffer, sizeof(buffer), "BOLERO_2_1\n");
  770. break;
  771. default:
  772. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  773. }
  774. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  775. }
  776. static int bolero_ssr_enable(struct device *dev, void *data)
  777. {
  778. struct bolero_priv *priv = data;
  779. int macro_idx;
  780. if (priv->initial_boot) {
  781. priv->initial_boot = false;
  782. return 0;
  783. }
  784. if (priv->rsc_clk_cb)
  785. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_UP);
  786. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  787. if (priv->macro_params[macro_idx].event_handler)
  788. priv->macro_params[macro_idx].event_handler(
  789. priv->component,
  790. BOLERO_MACRO_EVT_CLK_RESET, 0x0);
  791. }
  792. trace_printk("%s: clk count reset\n", __func__);
  793. if (priv->rsc_clk_cb)
  794. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_GFMUX_UP);
  795. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  796. if (!priv->macro_params[macro_idx].event_handler)
  797. continue;
  798. priv->macro_params[macro_idx].event_handler(
  799. priv->component,
  800. BOLERO_MACRO_EVT_PRE_SSR_UP, 0x0);
  801. }
  802. regcache_cache_only(priv->regmap, false);
  803. mutex_lock(&priv->clk_lock);
  804. priv->dev_up = true;
  805. mutex_unlock(&priv->clk_lock);
  806. regcache_mark_dirty(priv->regmap);
  807. bolero_clk_rsc_enable_all_clocks(priv->clk_dev, true);
  808. regcache_sync(priv->regmap);
  809. /* Add a 100usec sleep to ensure last register write is done */
  810. usleep_range(100,110);
  811. bolero_clk_rsc_enable_all_clocks(priv->clk_dev, false);
  812. trace_printk("%s: regcache_sync done\n", __func__);
  813. /* call ssr event for supported macros */
  814. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  815. if (!priv->macro_params[macro_idx].event_handler)
  816. continue;
  817. priv->macro_params[macro_idx].event_handler(
  818. priv->component,
  819. BOLERO_MACRO_EVT_SSR_UP, 0x0);
  820. }
  821. trace_printk("%s: SSR up events processed by all macros\n", __func__);
  822. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_UP);
  823. return 0;
  824. }
  825. static void bolero_ssr_disable(struct device *dev, void *data)
  826. {
  827. struct bolero_priv *priv = data;
  828. int macro_idx;
  829. if (!priv->dev_up) {
  830. dev_err_ratelimited(priv->dev,
  831. "%s: already disabled\n", __func__);
  832. return;
  833. }
  834. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_PA_OFF_PRE_SSR);
  835. regcache_cache_only(priv->regmap, true);
  836. mutex_lock(&priv->clk_lock);
  837. priv->dev_up = false;
  838. mutex_unlock(&priv->clk_lock);
  839. if (priv->rsc_clk_cb)
  840. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_DOWN);
  841. /* call ssr event for supported macros */
  842. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  843. if (!priv->macro_params[macro_idx].event_handler)
  844. continue;
  845. priv->macro_params[macro_idx].event_handler(
  846. priv->component,
  847. BOLERO_MACRO_EVT_SSR_DOWN, 0x0);
  848. }
  849. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_DOWN);
  850. }
  851. static struct snd_info_entry_ops bolero_info_ops = {
  852. .read = bolero_version_read,
  853. };
  854. static const struct snd_event_ops bolero_ssr_ops = {
  855. .enable = bolero_ssr_enable,
  856. .disable = bolero_ssr_disable,
  857. };
  858. /*
  859. * bolero_info_create_codec_entry - creates bolero module
  860. * @codec_root: The parent directory
  861. * @component: Codec component instance
  862. *
  863. * Creates bolero module and version entry under the given
  864. * parent directory.
  865. *
  866. * Return: 0 on success or negative error code on failure.
  867. */
  868. int bolero_info_create_codec_entry(struct snd_info_entry *codec_root,
  869. struct snd_soc_component *component)
  870. {
  871. struct snd_info_entry *version_entry;
  872. struct bolero_priv *priv;
  873. struct snd_soc_card *card;
  874. if (!codec_root || !component)
  875. return -EINVAL;
  876. priv = snd_soc_component_get_drvdata(component);
  877. if (priv->entry) {
  878. dev_dbg(priv->dev,
  879. "%s:bolero module already created\n", __func__);
  880. return 0;
  881. }
  882. card = component->card;
  883. priv->entry = snd_info_create_module_entry(codec_root->module,
  884. "bolero", codec_root);
  885. if (!priv->entry) {
  886. dev_dbg(component->dev, "%s: failed to create bolero entry\n",
  887. __func__);
  888. return -ENOMEM;
  889. }
  890. priv->entry->mode = S_IFDIR | 0555;
  891. if (snd_info_register(priv->entry) < 0) {
  892. snd_info_free_entry(priv->entry);
  893. return -ENOMEM;
  894. }
  895. version_entry = snd_info_create_card_entry(card->snd_card,
  896. "version",
  897. priv->entry);
  898. if (!version_entry) {
  899. dev_err(component->dev, "%s: failed to create bolero version entry\n",
  900. __func__);
  901. snd_info_free_entry(priv->entry);
  902. return -ENOMEM;
  903. }
  904. version_entry->private_data = priv;
  905. version_entry->size = BOLERO_VERSION_ENTRY_SIZE;
  906. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  907. version_entry->c.ops = &bolero_info_ops;
  908. if (snd_info_register(version_entry) < 0) {
  909. snd_info_free_entry(version_entry);
  910. snd_info_free_entry(priv->entry);
  911. return -ENOMEM;
  912. }
  913. priv->version_entry = version_entry;
  914. return 0;
  915. }
  916. EXPORT_SYMBOL(bolero_info_create_codec_entry);
  917. /**
  918. * bolero_register_wake_irq - Register wake irq of Tx macro
  919. *
  920. * @component: codec component ptr.
  921. * @ipc_wakeup: bool to identify ipc_wakeup to be used or HW interrupt line.
  922. *
  923. * Return: 0 on success or negative error code on failure.
  924. */
  925. int bolero_register_wake_irq(struct snd_soc_component *component,
  926. u32 ipc_wakeup)
  927. {
  928. struct bolero_priv *priv = NULL;
  929. if (!component)
  930. return -EINVAL;
  931. priv = snd_soc_component_get_drvdata(component);
  932. if (!priv)
  933. return -EINVAL;
  934. if (!bolero_is_valid_codec_dev(priv->dev)) {
  935. dev_err(component->dev, "%s: invalid codec\n", __func__);
  936. return -EINVAL;
  937. }
  938. if (priv->version == BOLERO_VERSION_2_1) {
  939. if (priv->macro_params[VA_MACRO].reg_wake_irq)
  940. priv->macro_params[VA_MACRO].reg_wake_irq(
  941. component, ipc_wakeup);
  942. } else {
  943. if (priv->macro_params[TX_MACRO].reg_wake_irq)
  944. priv->macro_params[TX_MACRO].reg_wake_irq(
  945. component, ipc_wakeup);
  946. }
  947. return 0;
  948. }
  949. EXPORT_SYMBOL(bolero_register_wake_irq);
  950. /**
  951. * bolero_tx_clk_switch - Switch tx macro clock
  952. *
  953. * @component: pointer to codec component instance.
  954. *
  955. * @clk_src: 0 for TX_RCG and 1 for VA_RCG
  956. *
  957. * Returns 0 on success or -EINVAL on error.
  958. */
  959. int bolero_tx_clk_switch(struct snd_soc_component *component, int clk_src)
  960. {
  961. struct bolero_priv *priv = NULL;
  962. int ret = 0;
  963. if (!component)
  964. return -EINVAL;
  965. priv = snd_soc_component_get_drvdata(component);
  966. if (!priv)
  967. return -EINVAL;
  968. if (!bolero_is_valid_codec_dev(priv->dev)) {
  969. dev_err(component->dev, "%s: invalid codec\n", __func__);
  970. return -EINVAL;
  971. }
  972. if (priv->macro_params[TX_MACRO].clk_switch)
  973. ret = priv->macro_params[TX_MACRO].clk_switch(component,
  974. clk_src);
  975. return ret;
  976. }
  977. EXPORT_SYMBOL(bolero_tx_clk_switch);
  978. /**
  979. * bolero_tx_mclk_enable - Enable/Disable TX Macro mclk
  980. *
  981. * @component: pointer to codec component instance.
  982. * @enable: set true to enable, otherwise false.
  983. *
  984. * Returns 0 on success or -EINVAL on error.
  985. */
  986. int bolero_tx_mclk_enable(struct snd_soc_component *component,
  987. bool enable)
  988. {
  989. struct bolero_priv *priv = NULL;
  990. int ret = 0;
  991. if (!component)
  992. return -EINVAL;
  993. priv = snd_soc_component_get_drvdata(component);
  994. if (!priv)
  995. return -EINVAL;
  996. if (!bolero_is_valid_codec_dev(priv->dev)) {
  997. dev_err(component->dev, "%s: invalid codec\n", __func__);
  998. return -EINVAL;
  999. }
  1000. if (priv->macro_params[TX_MACRO].clk_enable)
  1001. ret = priv->macro_params[TX_MACRO].clk_enable(component,
  1002. enable);
  1003. return ret;
  1004. }
  1005. EXPORT_SYMBOL(bolero_tx_mclk_enable);
  1006. /**
  1007. * bolero_register_event_listener - Register/Deregister to event listener
  1008. *
  1009. * @component: pointer to codec component instance.
  1010. * @enable: when set to 1 registers to event listener otherwise, derigisters
  1011. * from the event listener
  1012. *
  1013. * Returns 0 on success or -EINVAL on error.
  1014. */
  1015. int bolero_register_event_listener(struct snd_soc_component *component,
  1016. bool enable)
  1017. {
  1018. struct bolero_priv *priv = NULL;
  1019. int ret = 0;
  1020. if (!component)
  1021. return -EINVAL;
  1022. priv = snd_soc_component_get_drvdata(component);
  1023. if (!priv)
  1024. return -EINVAL;
  1025. if (!bolero_is_valid_codec_dev(priv->dev)) {
  1026. dev_err(component->dev, "%s: invalid codec\n", __func__);
  1027. return -EINVAL;
  1028. }
  1029. if (priv->macro_params[TX_MACRO].reg_evt_listener)
  1030. ret = priv->macro_params[TX_MACRO].reg_evt_listener(component,
  1031. enable);
  1032. return ret;
  1033. }
  1034. EXPORT_SYMBOL(bolero_register_event_listener);
  1035. static int bolero_soc_codec_probe(struct snd_soc_component *component)
  1036. {
  1037. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  1038. int macro_idx, ret = 0;
  1039. u8 core_id_0 = 0, core_id_1 = 0;
  1040. snd_soc_component_init_regmap(component, priv->regmap);
  1041. if (!priv->version) {
  1042. /*
  1043. * In order for the ADIE RTC to differentiate between targets
  1044. * version info is used.
  1045. * Assign 1.0 for target with only one macro
  1046. * Assign 1.1 for target with two macros
  1047. * Assign 1.2 for target with more than two macros
  1048. */
  1049. if (priv->num_macros_registered == 1)
  1050. priv->version = BOLERO_VERSION_1_0;
  1051. else if (priv->num_macros_registered == 2)
  1052. priv->version = BOLERO_VERSION_1_1;
  1053. else if (priv->num_macros_registered > 2)
  1054. priv->version = BOLERO_VERSION_1_2;
  1055. }
  1056. /* Assign bolero version */
  1057. core_id_0 = snd_soc_component_read32(component,
  1058. BOLERO_CDC_VA_TOP_CSR_CORE_ID_0);
  1059. core_id_1 = snd_soc_component_read32(component,
  1060. BOLERO_CDC_VA_TOP_CSR_CORE_ID_1);
  1061. if ((core_id_0 == 0x01) && (core_id_1 == 0x0F))
  1062. priv->version = BOLERO_VERSION_2_0;
  1063. if ((core_id_0 == 0x02) && (core_id_1 == 0x0E))
  1064. priv->version = BOLERO_VERSION_2_1;
  1065. /* call init for supported macros */
  1066. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  1067. if (priv->macro_params[macro_idx].init) {
  1068. ret = priv->macro_params[macro_idx].init(component);
  1069. if (ret < 0) {
  1070. dev_err(component->dev,
  1071. "%s: init for macro %d failed\n",
  1072. __func__, macro_idx);
  1073. goto err;
  1074. }
  1075. }
  1076. }
  1077. priv->component = component;
  1078. ret = snd_event_client_register(priv->dev, &bolero_ssr_ops, priv);
  1079. if (!ret) {
  1080. snd_event_notify(priv->dev, SND_EVENT_UP);
  1081. } else {
  1082. dev_err(component->dev,
  1083. "%s: Registration with SND event FWK failed ret = %d\n",
  1084. __func__, ret);
  1085. goto err;
  1086. }
  1087. dev_dbg(component->dev, "%s: bolero soc codec probe success\n",
  1088. __func__);
  1089. err:
  1090. return ret;
  1091. }
  1092. static void bolero_soc_codec_remove(struct snd_soc_component *component)
  1093. {
  1094. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  1095. int macro_idx;
  1096. snd_event_client_deregister(priv->dev);
  1097. /* call exit for supported macros */
  1098. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++)
  1099. if (priv->macro_params[macro_idx].exit)
  1100. priv->macro_params[macro_idx].exit(component);
  1101. return;
  1102. }
  1103. static const struct snd_soc_component_driver bolero = {
  1104. .name = DRV_NAME,
  1105. .probe = bolero_soc_codec_probe,
  1106. .remove = bolero_soc_codec_remove,
  1107. };
  1108. static void bolero_add_child_devices(struct work_struct *work)
  1109. {
  1110. struct bolero_priv *priv;
  1111. bool split_codec = false;
  1112. struct platform_device *pdev;
  1113. struct device_node *node;
  1114. int ret = 0, count = 0;
  1115. struct wcd_ctrl_platform_data *platdata = NULL;
  1116. char plat_dev_name[BOLERO_CDC_STRING_LEN] = "";
  1117. priv = container_of(work, struct bolero_priv,
  1118. bolero_add_child_devices_work);
  1119. if (!priv) {
  1120. pr_err("%s: Memory for bolero priv does not exist\n",
  1121. __func__);
  1122. return;
  1123. }
  1124. if (!priv->dev || !priv->dev->of_node) {
  1125. dev_err(priv->dev, "%s: DT node for bolero does not exist\n",
  1126. __func__);
  1127. return;
  1128. }
  1129. platdata = &priv->plat_data;
  1130. priv->child_count = 0;
  1131. for_each_available_child_of_node(priv->dev->of_node, node) {
  1132. split_codec = false;
  1133. if (of_find_property(node, "qcom,split-codec", NULL)) {
  1134. split_codec = true;
  1135. dev_dbg(priv->dev, "%s: split codec slave exists\n",
  1136. __func__);
  1137. }
  1138. strlcpy(plat_dev_name, node->name,
  1139. (BOLERO_CDC_STRING_LEN - 1));
  1140. pdev = platform_device_alloc(plat_dev_name, -1);
  1141. if (!pdev) {
  1142. dev_err(priv->dev, "%s: pdev memory alloc failed\n",
  1143. __func__);
  1144. ret = -ENOMEM;
  1145. goto err;
  1146. }
  1147. pdev->dev.parent = priv->dev;
  1148. pdev->dev.of_node = node;
  1149. priv->dev->platform_data = platdata;
  1150. if (split_codec)
  1151. priv->wcd_dev = &pdev->dev;
  1152. ret = platform_device_add(pdev);
  1153. if (ret) {
  1154. dev_err(&pdev->dev,
  1155. "%s: Cannot add platform device\n",
  1156. __func__);
  1157. platform_device_put(pdev);
  1158. goto fail_pdev_add;
  1159. }
  1160. if (priv->child_count < BOLERO_CDC_CHILD_DEVICES_MAX)
  1161. priv->pdev_child_devices[priv->child_count++] = pdev;
  1162. else
  1163. goto err;
  1164. }
  1165. return;
  1166. fail_pdev_add:
  1167. for (count = 0; count < priv->child_count; count++)
  1168. platform_device_put(priv->pdev_child_devices[count]);
  1169. err:
  1170. return;
  1171. }
  1172. static int bolero_probe(struct platform_device *pdev)
  1173. {
  1174. struct bolero_priv *priv;
  1175. u32 num_macros = 0;
  1176. int ret;
  1177. struct clk *lpass_core_hw_vote = NULL;
  1178. struct clk *lpass_audio_hw_vote = NULL;
  1179. priv = devm_kzalloc(&pdev->dev, sizeof(struct bolero_priv),
  1180. GFP_KERNEL);
  1181. if (!priv)
  1182. return -ENOMEM;
  1183. ret = of_property_read_u32(pdev->dev.of_node, "qcom,num-macros",
  1184. &num_macros);
  1185. if (ret) {
  1186. dev_err(&pdev->dev,
  1187. "%s:num-macros property not found\n",
  1188. __func__);
  1189. return ret;
  1190. }
  1191. priv->num_macros = num_macros;
  1192. if (priv->num_macros > MAX_MACRO) {
  1193. dev_err(&pdev->dev,
  1194. "%s:num_macros(%d) > MAX_MACRO(%d) than supported\n",
  1195. __func__, priv->num_macros, MAX_MACRO);
  1196. return -EINVAL;
  1197. }
  1198. priv->va_without_decimation = of_property_read_bool(pdev->dev.of_node,
  1199. "qcom,va-without-decimation");
  1200. if (priv->va_without_decimation)
  1201. bolero_reg_access[VA_MACRO] = bolero_va_top_reg_access;
  1202. ret = of_property_read_u32(pdev->dev.of_node,
  1203. "qcom,bolero-version", &priv->version);
  1204. if (ret) {
  1205. dev_dbg(&pdev->dev, "%s:bolero version not specified\n",
  1206. __func__);
  1207. ret = 0;
  1208. }
  1209. if (priv->version == BOLERO_VERSION_2_1) {
  1210. bolero_reg_access[TX_MACRO] = bolero_tx_reg_access_v2;
  1211. bolero_reg_access[VA_MACRO] = bolero_va_reg_access_v2;
  1212. } else if (priv->version == BOLERO_VERSION_2_0) {
  1213. bolero_reg_access[VA_MACRO] = bolero_va_reg_access_v3;
  1214. }
  1215. priv->dev = &pdev->dev;
  1216. priv->dev_up = true;
  1217. priv->initial_boot = true;
  1218. priv->regmap = bolero_regmap_init(priv->dev,
  1219. &bolero_regmap_config);
  1220. if (IS_ERR_OR_NULL((void *)(priv->regmap))) {
  1221. dev_err(&pdev->dev, "%s:regmap init failed\n", __func__);
  1222. return -EINVAL;
  1223. }
  1224. priv->read_dev = __bolero_reg_read;
  1225. priv->write_dev = __bolero_reg_write;
  1226. priv->plat_data.handle = (void *) priv;
  1227. priv->plat_data.update_wcd_event = bolero_cdc_update_wcd_event;
  1228. priv->plat_data.register_notifier = bolero_cdc_register_notifier;
  1229. priv->core_hw_vote_count = 0;
  1230. priv->core_audio_vote_count = 0;
  1231. dev_set_drvdata(&pdev->dev, priv);
  1232. mutex_init(&priv->io_lock);
  1233. mutex_init(&priv->clk_lock);
  1234. mutex_init(&priv->vote_lock);
  1235. INIT_WORK(&priv->bolero_add_child_devices_work,
  1236. bolero_add_child_devices);
  1237. schedule_work(&priv->bolero_add_child_devices_work);
  1238. /* Register LPASS core hw vote */
  1239. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  1240. if (IS_ERR(lpass_core_hw_vote)) {
  1241. ret = PTR_ERR(lpass_core_hw_vote);
  1242. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  1243. __func__, "lpass_core_hw_vote", ret);
  1244. lpass_core_hw_vote = NULL;
  1245. ret = 0;
  1246. }
  1247. priv->lpass_core_hw_vote = lpass_core_hw_vote;
  1248. /* Register LPASS audio hw vote */
  1249. lpass_audio_hw_vote = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  1250. if (IS_ERR(lpass_audio_hw_vote)) {
  1251. ret = PTR_ERR(lpass_audio_hw_vote);
  1252. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  1253. __func__, "lpass_audio_hw_vote", ret);
  1254. lpass_audio_hw_vote = NULL;
  1255. ret = 0;
  1256. }
  1257. priv->lpass_audio_hw_vote = lpass_audio_hw_vote;
  1258. return 0;
  1259. }
  1260. static int bolero_remove(struct platform_device *pdev)
  1261. {
  1262. struct bolero_priv *priv = dev_get_drvdata(&pdev->dev);
  1263. if (!priv)
  1264. return -EINVAL;
  1265. of_platform_depopulate(&pdev->dev);
  1266. mutex_destroy(&priv->io_lock);
  1267. mutex_destroy(&priv->clk_lock);
  1268. mutex_destroy(&priv->vote_lock);
  1269. return 0;
  1270. }
  1271. #ifdef CONFIG_PM
  1272. int bolero_runtime_resume(struct device *dev)
  1273. {
  1274. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  1275. int ret = 0;
  1276. mutex_lock(&priv->vote_lock);
  1277. if (priv->lpass_core_hw_vote == NULL) {
  1278. dev_dbg(dev, "%s: Invalid lpass core hw node\n", __func__);
  1279. goto audio_vote;
  1280. }
  1281. if (priv->core_hw_vote_count == 0) {
  1282. ret = digital_cdc_rsc_mgr_hw_vote_enable(priv->lpass_core_hw_vote);
  1283. if (ret < 0) {
  1284. dev_err(dev, "%s:lpass core hw enable failed\n",
  1285. __func__);
  1286. goto audio_vote;
  1287. }
  1288. }
  1289. priv->core_hw_vote_count++;
  1290. trace_printk("%s: hw vote count %d\n",
  1291. __func__, priv->core_hw_vote_count);
  1292. audio_vote:
  1293. if (priv->lpass_audio_hw_vote == NULL) {
  1294. dev_dbg(dev, "%s: Invalid lpass audio hw node\n", __func__);
  1295. goto done;
  1296. }
  1297. if (priv->core_audio_vote_count == 0) {
  1298. ret = digital_cdc_rsc_mgr_hw_vote_enable(priv->lpass_audio_hw_vote);
  1299. if (ret < 0) {
  1300. dev_err(dev, "%s:lpass audio hw enable failed\n",
  1301. __func__);
  1302. goto done;
  1303. }
  1304. }
  1305. priv->core_audio_vote_count++;
  1306. trace_printk("%s: audio vote count %d\n",
  1307. __func__, priv->core_audio_vote_count);
  1308. done:
  1309. mutex_unlock(&priv->vote_lock);
  1310. pm_runtime_set_autosuspend_delay(priv->dev, BOLERO_AUTO_SUSPEND_DELAY);
  1311. return 0;
  1312. }
  1313. EXPORT_SYMBOL(bolero_runtime_resume);
  1314. int bolero_runtime_suspend(struct device *dev)
  1315. {
  1316. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  1317. mutex_lock(&priv->vote_lock);
  1318. if (priv->lpass_core_hw_vote != NULL) {
  1319. if (--priv->core_hw_vote_count == 0)
  1320. digital_cdc_rsc_mgr_hw_vote_disable(
  1321. priv->lpass_core_hw_vote);
  1322. if (priv->core_hw_vote_count < 0)
  1323. priv->core_hw_vote_count = 0;
  1324. } else {
  1325. dev_dbg(dev, "%s: Invalid lpass core hw node\n",
  1326. __func__);
  1327. }
  1328. trace_printk("%s: hw vote count %d\n",
  1329. __func__, priv->core_hw_vote_count);
  1330. if (priv->lpass_audio_hw_vote != NULL) {
  1331. if (--priv->core_audio_vote_count == 0)
  1332. digital_cdc_rsc_mgr_hw_vote_disable(
  1333. priv->lpass_audio_hw_vote);
  1334. if (priv->core_audio_vote_count < 0)
  1335. priv->core_audio_vote_count = 0;
  1336. } else {
  1337. dev_dbg(dev, "%s: Invalid lpass audio hw node\n",
  1338. __func__);
  1339. }
  1340. trace_printk("%s: audio vote count %d\n",
  1341. __func__, priv->core_audio_vote_count);
  1342. mutex_unlock(&priv->vote_lock);
  1343. return 0;
  1344. }
  1345. EXPORT_SYMBOL(bolero_runtime_suspend);
  1346. #endif /* CONFIG_PM */
  1347. bool bolero_check_core_votes(struct device *dev)
  1348. {
  1349. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  1350. bool ret = true;
  1351. mutex_lock(&priv->vote_lock);
  1352. if ((priv->lpass_core_hw_vote && !priv->core_hw_vote_count) ||
  1353. (priv->lpass_audio_hw_vote && !priv->core_audio_vote_count))
  1354. ret = false;
  1355. mutex_unlock(&priv->vote_lock);
  1356. return ret;
  1357. }
  1358. EXPORT_SYMBOL(bolero_check_core_votes);
  1359. static const struct of_device_id bolero_dt_match[] = {
  1360. {.compatible = "qcom,bolero-codec"},
  1361. {}
  1362. };
  1363. MODULE_DEVICE_TABLE(of, bolero_dt_match);
  1364. static struct platform_driver bolero_drv = {
  1365. .driver = {
  1366. .name = "bolero-codec",
  1367. .owner = THIS_MODULE,
  1368. .of_match_table = bolero_dt_match,
  1369. .suppress_bind_attrs = true,
  1370. },
  1371. .probe = bolero_probe,
  1372. .remove = bolero_remove,
  1373. };
  1374. static int bolero_drv_init(void)
  1375. {
  1376. return platform_driver_register(&bolero_drv);
  1377. }
  1378. static void bolero_drv_exit(void)
  1379. {
  1380. platform_driver_unregister(&bolero_drv);
  1381. }
  1382. static int __init bolero_init(void)
  1383. {
  1384. bolero_drv_init();
  1385. bolero_clk_rsc_mgr_init();
  1386. return 0;
  1387. }
  1388. module_init(bolero_init);
  1389. static void __exit bolero_exit(void)
  1390. {
  1391. bolero_clk_rsc_mgr_exit();
  1392. bolero_drv_exit();
  1393. }
  1394. module_exit(bolero_exit);
  1395. MODULE_DESCRIPTION("Bolero driver");
  1396. MODULE_LICENSE("GPL v2");