bolero-cdc.c 37 KB

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