bolero-cdc.c 38 KB

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