bolero-cdc.c 38 KB

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