bolero-cdc.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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].clk_switch = ops->clk_switch;
  574. priv->macro_params[macro_id].reg_evt_listener =
  575. ops->reg_evt_listener;
  576. priv->macro_params[macro_id].clk_enable = ops->clk_enable;
  577. }
  578. if (macro_id == TX_MACRO || macro_id == VA_MACRO)
  579. priv->macro_params[macro_id].clk_div_get = ops->clk_div_get;
  580. if (priv->version == BOLERO_VERSION_2_1) {
  581. if (macro_id == VA_MACRO)
  582. priv->macro_params[macro_id].reg_wake_irq =
  583. ops->reg_wake_irq;
  584. }
  585. priv->num_dais += ops->num_dais;
  586. priv->num_macros_registered++;
  587. priv->macros_supported[macro_id] = true;
  588. dev_dbg(dev, "%s: register macro successful:%d\n", macro_id);
  589. if (priv->num_macros_registered == priv->num_macros) {
  590. ret = bolero_copy_dais_from_macro(priv);
  591. if (ret < 0) {
  592. dev_err(dev, "%s: copy_dais failed\n", __func__);
  593. return ret;
  594. }
  595. if (priv->macros_supported[TX_MACRO] == false) {
  596. bolero_mclk_mux_tbl[WSA_MACRO][MCLK_MUX0] = WSA_MACRO;
  597. priv->current_mclk_mux_macro[WSA_MACRO] = WSA_MACRO;
  598. bolero_mclk_mux_tbl[VA_MACRO][MCLK_MUX0] = VA_MACRO;
  599. priv->current_mclk_mux_macro[VA_MACRO] = VA_MACRO;
  600. }
  601. ret = snd_soc_register_component(dev->parent, &bolero,
  602. priv->bolero_dais, priv->num_dais);
  603. if (ret < 0) {
  604. dev_err(dev, "%s: register codec failed\n", __func__);
  605. return ret;
  606. }
  607. }
  608. return 0;
  609. }
  610. EXPORT_SYMBOL(bolero_register_macro);
  611. /**
  612. * bolero_unregister_macro - De-Register macro from bolero
  613. *
  614. * @dev: macro device ptr.
  615. * @macro_id: ID of macro calling this API.
  616. *
  617. */
  618. void bolero_unregister_macro(struct device *dev, u16 macro_id)
  619. {
  620. struct bolero_priv *priv;
  621. if (!dev) {
  622. pr_err("%s: dev is null\n", __func__);
  623. return;
  624. }
  625. if (!bolero_is_valid_child_dev(dev)) {
  626. dev_err(dev, "%s: macro:%d not in valid registered macro-list\n",
  627. __func__, macro_id);
  628. return;
  629. }
  630. priv = dev_get_drvdata(dev->parent);
  631. if (!priv || (macro_id >= MAX_MACRO)) {
  632. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  633. return;
  634. }
  635. priv->macro_params[macro_id].init = NULL;
  636. priv->macro_params[macro_id].num_dais = 0;
  637. priv->macro_params[macro_id].dai_ptr = NULL;
  638. priv->macro_params[macro_id].event_handler = NULL;
  639. priv->macro_params[macro_id].dev = NULL;
  640. if (macro_id == TX_MACRO) {
  641. priv->macro_params[macro_id].reg_wake_irq = NULL;
  642. priv->macro_params[macro_id].clk_switch = NULL;
  643. priv->macro_params[macro_id].reg_evt_listener = NULL;
  644. priv->macro_params[macro_id].clk_enable = NULL;
  645. }
  646. if (macro_id == TX_MACRO || macro_id == VA_MACRO)
  647. priv->macro_params[macro_id].clk_div_get = NULL;
  648. priv->num_dais -= priv->macro_params[macro_id].num_dais;
  649. priv->num_macros_registered--;
  650. /* UNREGISTER CODEC HERE */
  651. if (priv->num_macros - 1 == priv->num_macros_registered)
  652. snd_soc_unregister_component(dev->parent);
  653. }
  654. EXPORT_SYMBOL(bolero_unregister_macro);
  655. void bolero_wsa_pa_on(struct device *dev)
  656. {
  657. struct bolero_priv *priv;
  658. if (!dev) {
  659. pr_err("%s: dev is null\n", __func__);
  660. return;
  661. }
  662. if (!bolero_is_valid_child_dev(dev)) {
  663. dev_err(dev, "%s: not a valid child dev\n",
  664. __func__);
  665. return;
  666. }
  667. priv = dev_get_drvdata(dev->parent);
  668. if (!priv) {
  669. dev_err(dev, "%s: priv is null\n", __func__);
  670. return;
  671. }
  672. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_PA_ON_POST_FSCLK);
  673. }
  674. EXPORT_SYMBOL(bolero_wsa_pa_on);
  675. int bolero_get_version(struct device *dev)
  676. {
  677. struct bolero_priv *priv;
  678. if (!dev) {
  679. pr_err("%s: dev is null\n", __func__);
  680. return -EINVAL;
  681. }
  682. if (!bolero_is_valid_child_dev(dev)) {
  683. dev_err(dev, "%s: child device for macro not added yet\n",
  684. __func__);
  685. return -EINVAL;
  686. }
  687. priv = dev_get_drvdata(dev->parent);
  688. if (!priv) {
  689. dev_err(dev, "%s: priv is null\n", __func__);
  690. return -EINVAL;
  691. }
  692. return priv->version;
  693. }
  694. EXPORT_SYMBOL(bolero_get_version);
  695. static ssize_t bolero_version_read(struct snd_info_entry *entry,
  696. void *file_private_data,
  697. struct file *file,
  698. char __user *buf, size_t count,
  699. loff_t pos)
  700. {
  701. struct bolero_priv *priv;
  702. char buffer[BOLERO_VERSION_ENTRY_SIZE];
  703. int len = 0;
  704. priv = (struct bolero_priv *) entry->private_data;
  705. if (!priv) {
  706. pr_err("%s: bolero priv is null\n", __func__);
  707. return -EINVAL;
  708. }
  709. switch (priv->version) {
  710. case BOLERO_VERSION_1_0:
  711. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_0\n");
  712. break;
  713. case BOLERO_VERSION_1_1:
  714. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_1\n");
  715. break;
  716. case BOLERO_VERSION_1_2:
  717. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_2\n");
  718. break;
  719. case BOLERO_VERSION_2_1:
  720. len = snprintf(buffer, sizeof(buffer), "BOLERO_2_1\n");
  721. break;
  722. default:
  723. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  724. }
  725. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  726. }
  727. static int bolero_ssr_enable(struct device *dev, void *data)
  728. {
  729. struct bolero_priv *priv = data;
  730. int macro_idx;
  731. if (priv->initial_boot) {
  732. priv->initial_boot = false;
  733. return 0;
  734. }
  735. if (priv->rsc_clk_cb)
  736. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_UP);
  737. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  738. if (priv->macro_params[macro_idx].event_handler)
  739. priv->macro_params[macro_idx].event_handler(
  740. priv->component,
  741. BOLERO_MACRO_EVT_CLK_RESET, 0x0);
  742. }
  743. trace_printk("%s: clk count reset\n", __func__);
  744. if (priv->rsc_clk_cb)
  745. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_GFMUX_UP);
  746. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  747. if (!priv->macro_params[macro_idx].event_handler)
  748. continue;
  749. priv->macro_params[macro_idx].event_handler(
  750. priv->component,
  751. BOLERO_MACRO_EVT_PRE_SSR_UP, 0x0);
  752. }
  753. regcache_cache_only(priv->regmap, false);
  754. mutex_lock(&priv->clk_lock);
  755. priv->dev_up = true;
  756. mutex_unlock(&priv->clk_lock);
  757. regcache_mark_dirty(priv->regmap);
  758. bolero_clk_rsc_enable_all_clocks(priv->clk_dev, true);
  759. regcache_sync(priv->regmap);
  760. /* Add a 100usec sleep to ensure last register write is done */
  761. usleep_range(100,110);
  762. bolero_clk_rsc_enable_all_clocks(priv->clk_dev, false);
  763. trace_printk("%s: regcache_sync done\n", __func__);
  764. /* call ssr event for supported macros */
  765. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  766. if (!priv->macro_params[macro_idx].event_handler)
  767. continue;
  768. priv->macro_params[macro_idx].event_handler(
  769. priv->component,
  770. BOLERO_MACRO_EVT_SSR_UP, 0x0);
  771. }
  772. trace_printk("%s: SSR up events processed by all macros\n", __func__);
  773. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_UP);
  774. return 0;
  775. }
  776. static void bolero_ssr_disable(struct device *dev, void *data)
  777. {
  778. struct bolero_priv *priv = data;
  779. int macro_idx;
  780. if (!priv->dev_up) {
  781. dev_err_ratelimited(priv->dev,
  782. "%s: already disabled\n", __func__);
  783. return;
  784. }
  785. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_PA_OFF_PRE_SSR);
  786. regcache_cache_only(priv->regmap, true);
  787. mutex_lock(&priv->clk_lock);
  788. priv->dev_up = false;
  789. mutex_unlock(&priv->clk_lock);
  790. if (priv->rsc_clk_cb)
  791. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_DOWN);
  792. /* call ssr event for supported macros */
  793. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  794. if (!priv->macro_params[macro_idx].event_handler)
  795. continue;
  796. priv->macro_params[macro_idx].event_handler(
  797. priv->component,
  798. BOLERO_MACRO_EVT_SSR_DOWN, 0x0);
  799. }
  800. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_DOWN);
  801. }
  802. static struct snd_info_entry_ops bolero_info_ops = {
  803. .read = bolero_version_read,
  804. };
  805. static const struct snd_event_ops bolero_ssr_ops = {
  806. .enable = bolero_ssr_enable,
  807. .disable = bolero_ssr_disable,
  808. };
  809. /*
  810. * bolero_info_create_codec_entry - creates bolero module
  811. * @codec_root: The parent directory
  812. * @component: Codec component instance
  813. *
  814. * Creates bolero module and version entry under the given
  815. * parent directory.
  816. *
  817. * Return: 0 on success or negative error code on failure.
  818. */
  819. int bolero_info_create_codec_entry(struct snd_info_entry *codec_root,
  820. struct snd_soc_component *component)
  821. {
  822. struct snd_info_entry *version_entry;
  823. struct bolero_priv *priv;
  824. struct snd_soc_card *card;
  825. if (!codec_root || !component)
  826. return -EINVAL;
  827. priv = snd_soc_component_get_drvdata(component);
  828. if (priv->entry) {
  829. dev_dbg(priv->dev,
  830. "%s:bolero module already created\n", __func__);
  831. return 0;
  832. }
  833. card = component->card;
  834. priv->entry = snd_info_create_module_entry(codec_root->module,
  835. "bolero", codec_root);
  836. if (!priv->entry) {
  837. dev_dbg(component->dev, "%s: failed to create bolero entry\n",
  838. __func__);
  839. return -ENOMEM;
  840. }
  841. priv->entry->mode = S_IFDIR | 0555;
  842. if (snd_info_register(priv->entry) < 0) {
  843. snd_info_free_entry(priv->entry);
  844. return -ENOMEM;
  845. }
  846. version_entry = snd_info_create_card_entry(card->snd_card,
  847. "version",
  848. priv->entry);
  849. if (!version_entry) {
  850. dev_err(component->dev, "%s: failed to create bolero version entry\n",
  851. __func__);
  852. snd_info_free_entry(priv->entry);
  853. return -ENOMEM;
  854. }
  855. version_entry->private_data = priv;
  856. version_entry->size = BOLERO_VERSION_ENTRY_SIZE;
  857. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  858. version_entry->c.ops = &bolero_info_ops;
  859. if (snd_info_register(version_entry) < 0) {
  860. snd_info_free_entry(version_entry);
  861. snd_info_free_entry(priv->entry);
  862. return -ENOMEM;
  863. }
  864. priv->version_entry = version_entry;
  865. return 0;
  866. }
  867. EXPORT_SYMBOL(bolero_info_create_codec_entry);
  868. /**
  869. * bolero_register_wake_irq - Register wake irq of Tx macro
  870. *
  871. * @component: codec component ptr.
  872. * @ipc_wakeup: bool to identify ipc_wakeup to be used or HW interrupt line.
  873. *
  874. * Return: 0 on success or negative error code on failure.
  875. */
  876. int bolero_register_wake_irq(struct snd_soc_component *component,
  877. u32 ipc_wakeup)
  878. {
  879. struct bolero_priv *priv = NULL;
  880. if (!component)
  881. return -EINVAL;
  882. priv = snd_soc_component_get_drvdata(component);
  883. if (!priv)
  884. return -EINVAL;
  885. if (!bolero_is_valid_codec_dev(priv->dev)) {
  886. dev_err(component->dev, "%s: invalid codec\n", __func__);
  887. return -EINVAL;
  888. }
  889. if (priv->version == BOLERO_VERSION_2_1) {
  890. if (priv->macro_params[VA_MACRO].reg_wake_irq)
  891. priv->macro_params[VA_MACRO].reg_wake_irq(
  892. component, ipc_wakeup);
  893. } else {
  894. if (priv->macro_params[TX_MACRO].reg_wake_irq)
  895. priv->macro_params[TX_MACRO].reg_wake_irq(
  896. component, ipc_wakeup);
  897. }
  898. return 0;
  899. }
  900. EXPORT_SYMBOL(bolero_register_wake_irq);
  901. /**
  902. * bolero_tx_clk_switch - Switch tx macro clock
  903. *
  904. * @component: pointer to codec component instance.
  905. *
  906. * @clk_src: 0 for TX_RCG and 1 for VA_RCG
  907. *
  908. * Returns 0 on success or -EINVAL on error.
  909. */
  910. int bolero_tx_clk_switch(struct snd_soc_component *component, int clk_src)
  911. {
  912. struct bolero_priv *priv = NULL;
  913. int ret = 0;
  914. if (!component)
  915. return -EINVAL;
  916. priv = snd_soc_component_get_drvdata(component);
  917. if (!priv)
  918. return -EINVAL;
  919. if (!bolero_is_valid_codec_dev(priv->dev)) {
  920. dev_err(component->dev, "%s: invalid codec\n", __func__);
  921. return -EINVAL;
  922. }
  923. if (priv->macro_params[TX_MACRO].clk_switch)
  924. ret = priv->macro_params[TX_MACRO].clk_switch(component,
  925. clk_src);
  926. return ret;
  927. }
  928. EXPORT_SYMBOL(bolero_tx_clk_switch);
  929. /**
  930. * bolero_tx_mclk_enable - Enable/Disable TX Macro mclk
  931. *
  932. * @component: pointer to codec component instance.
  933. * @enable: set true to enable, otherwise false.
  934. *
  935. * Returns 0 on success or -EINVAL on error.
  936. */
  937. int bolero_tx_mclk_enable(struct snd_soc_component *component,
  938. bool enable)
  939. {
  940. struct bolero_priv *priv = NULL;
  941. int ret = 0;
  942. if (!component)
  943. return -EINVAL;
  944. priv = snd_soc_component_get_drvdata(component);
  945. if (!priv)
  946. return -EINVAL;
  947. if (!bolero_is_valid_codec_dev(priv->dev)) {
  948. dev_err(component->dev, "%s: invalid codec\n", __func__);
  949. return -EINVAL;
  950. }
  951. if (priv->macro_params[TX_MACRO].clk_enable)
  952. ret = priv->macro_params[TX_MACRO].clk_enable(component,
  953. enable);
  954. return ret;
  955. }
  956. EXPORT_SYMBOL(bolero_tx_mclk_enable);
  957. /**
  958. * bolero_register_event_listener - Register/Deregister to event listener
  959. *
  960. * @component: pointer to codec component instance.
  961. * @enable: when set to 1 registers to event listener otherwise, derigisters
  962. * from the event listener
  963. *
  964. * Returns 0 on success or -EINVAL on error.
  965. */
  966. int bolero_register_event_listener(struct snd_soc_component *component,
  967. bool enable)
  968. {
  969. struct bolero_priv *priv = NULL;
  970. int ret = 0;
  971. if (!component)
  972. return -EINVAL;
  973. priv = snd_soc_component_get_drvdata(component);
  974. if (!priv)
  975. return -EINVAL;
  976. if (!bolero_is_valid_codec_dev(priv->dev)) {
  977. dev_err(component->dev, "%s: invalid codec\n", __func__);
  978. return -EINVAL;
  979. }
  980. if (priv->macro_params[TX_MACRO].reg_evt_listener)
  981. ret = priv->macro_params[TX_MACRO].reg_evt_listener(component,
  982. enable);
  983. return ret;
  984. }
  985. EXPORT_SYMBOL(bolero_register_event_listener);
  986. static int bolero_soc_codec_probe(struct snd_soc_component *component)
  987. {
  988. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  989. int macro_idx, ret = 0;
  990. u8 core_id_0 = 0, core_id_1 = 0;
  991. snd_soc_component_init_regmap(component, priv->regmap);
  992. if (!priv->version) {
  993. /*
  994. * In order for the ADIE RTC to differentiate between targets
  995. * version info is used.
  996. * Assign 1.0 for target with only one macro
  997. * Assign 1.1 for target with two macros
  998. * Assign 1.2 for target with more than two macros
  999. */
  1000. if (priv->num_macros_registered == 1)
  1001. priv->version = BOLERO_VERSION_1_0;
  1002. else if (priv->num_macros_registered == 2)
  1003. priv->version = BOLERO_VERSION_1_1;
  1004. else if (priv->num_macros_registered > 2)
  1005. priv->version = BOLERO_VERSION_1_2;
  1006. }
  1007. /* Assign bolero version */
  1008. core_id_0 = snd_soc_component_read32(component,
  1009. BOLERO_CDC_VA_TOP_CSR_CORE_ID_0);
  1010. core_id_1 = snd_soc_component_read32(component,
  1011. BOLERO_CDC_VA_TOP_CSR_CORE_ID_1);
  1012. if ((core_id_0 == 0x01) && (core_id_1 == 0x0F))
  1013. priv->version = BOLERO_VERSION_2_0;
  1014. if ((core_id_0 == 0x02) && (core_id_1 == 0x0E))
  1015. priv->version = BOLERO_VERSION_2_1;
  1016. /* call init for supported macros */
  1017. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  1018. if (priv->macro_params[macro_idx].init) {
  1019. ret = priv->macro_params[macro_idx].init(component);
  1020. if (ret < 0) {
  1021. dev_err(component->dev,
  1022. "%s: init for macro %d failed\n",
  1023. __func__, macro_idx);
  1024. goto err;
  1025. }
  1026. }
  1027. }
  1028. priv->component = component;
  1029. ret = snd_event_client_register(priv->dev, &bolero_ssr_ops, priv);
  1030. if (!ret) {
  1031. snd_event_notify(priv->dev, SND_EVENT_UP);
  1032. } else {
  1033. dev_err(component->dev,
  1034. "%s: Registration with SND event FWK failed ret = %d\n",
  1035. __func__, ret);
  1036. goto err;
  1037. }
  1038. dev_dbg(component->dev, "%s: bolero soc codec probe success\n",
  1039. __func__);
  1040. err:
  1041. return ret;
  1042. }
  1043. static void bolero_soc_codec_remove(struct snd_soc_component *component)
  1044. {
  1045. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  1046. int macro_idx;
  1047. snd_event_client_deregister(priv->dev);
  1048. /* call exit for supported macros */
  1049. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++)
  1050. if (priv->macro_params[macro_idx].exit)
  1051. priv->macro_params[macro_idx].exit(component);
  1052. return;
  1053. }
  1054. static const struct snd_soc_component_driver bolero = {
  1055. .name = DRV_NAME,
  1056. .probe = bolero_soc_codec_probe,
  1057. .remove = bolero_soc_codec_remove,
  1058. };
  1059. static void bolero_add_child_devices(struct work_struct *work)
  1060. {
  1061. struct bolero_priv *priv;
  1062. bool split_codec = false;
  1063. struct platform_device *pdev;
  1064. struct device_node *node;
  1065. int ret = 0, count = 0;
  1066. struct wcd_ctrl_platform_data *platdata = NULL;
  1067. char plat_dev_name[BOLERO_CDC_STRING_LEN] = "";
  1068. priv = container_of(work, struct bolero_priv,
  1069. bolero_add_child_devices_work);
  1070. if (!priv) {
  1071. pr_err("%s: Memory for bolero priv does not exist\n",
  1072. __func__);
  1073. return;
  1074. }
  1075. if (!priv->dev || !priv->dev->of_node) {
  1076. dev_err(priv->dev, "%s: DT node for bolero does not exist\n",
  1077. __func__);
  1078. return;
  1079. }
  1080. platdata = &priv->plat_data;
  1081. priv->child_count = 0;
  1082. for_each_available_child_of_node(priv->dev->of_node, node) {
  1083. split_codec = false;
  1084. if (of_find_property(node, "qcom,split-codec", NULL)) {
  1085. split_codec = true;
  1086. dev_dbg(priv->dev, "%s: split codec slave exists\n",
  1087. __func__);
  1088. }
  1089. strlcpy(plat_dev_name, node->name,
  1090. (BOLERO_CDC_STRING_LEN - 1));
  1091. pdev = platform_device_alloc(plat_dev_name, -1);
  1092. if (!pdev) {
  1093. dev_err(priv->dev, "%s: pdev memory alloc failed\n",
  1094. __func__);
  1095. ret = -ENOMEM;
  1096. goto err;
  1097. }
  1098. pdev->dev.parent = priv->dev;
  1099. pdev->dev.of_node = node;
  1100. priv->dev->platform_data = platdata;
  1101. if (split_codec)
  1102. priv->wcd_dev = &pdev->dev;
  1103. ret = platform_device_add(pdev);
  1104. if (ret) {
  1105. dev_err(&pdev->dev,
  1106. "%s: Cannot add platform device\n",
  1107. __func__);
  1108. platform_device_put(pdev);
  1109. goto fail_pdev_add;
  1110. }
  1111. if (priv->child_count < BOLERO_CDC_CHILD_DEVICES_MAX)
  1112. priv->pdev_child_devices[priv->child_count++] = pdev;
  1113. else
  1114. goto err;
  1115. }
  1116. return;
  1117. fail_pdev_add:
  1118. for (count = 0; count < priv->child_count; count++)
  1119. platform_device_put(priv->pdev_child_devices[count]);
  1120. err:
  1121. return;
  1122. }
  1123. static int bolero_probe(struct platform_device *pdev)
  1124. {
  1125. struct bolero_priv *priv;
  1126. u32 num_macros = 0;
  1127. int ret;
  1128. struct clk *lpass_core_hw_vote = NULL;
  1129. struct clk *lpass_audio_hw_vote = NULL;
  1130. priv = devm_kzalloc(&pdev->dev, sizeof(struct bolero_priv),
  1131. GFP_KERNEL);
  1132. if (!priv)
  1133. return -ENOMEM;
  1134. ret = of_property_read_u32(pdev->dev.of_node, "qcom,num-macros",
  1135. &num_macros);
  1136. if (ret) {
  1137. dev_err(&pdev->dev,
  1138. "%s:num-macros property not found\n",
  1139. __func__);
  1140. return ret;
  1141. }
  1142. priv->num_macros = num_macros;
  1143. if (priv->num_macros > MAX_MACRO) {
  1144. dev_err(&pdev->dev,
  1145. "%s:num_macros(%d) > MAX_MACRO(%d) than supported\n",
  1146. __func__, priv->num_macros, MAX_MACRO);
  1147. return -EINVAL;
  1148. }
  1149. priv->va_without_decimation = of_property_read_bool(pdev->dev.of_node,
  1150. "qcom,va-without-decimation");
  1151. if (priv->va_without_decimation)
  1152. bolero_reg_access[VA_MACRO] = bolero_va_top_reg_access;
  1153. ret = of_property_read_u32(pdev->dev.of_node,
  1154. "qcom,bolero-version", &priv->version);
  1155. if (ret) {
  1156. dev_dbg(&pdev->dev, "%s:bolero version not specified\n",
  1157. __func__);
  1158. ret = 0;
  1159. }
  1160. if (priv->version == BOLERO_VERSION_2_1) {
  1161. bolero_reg_access[TX_MACRO] = bolero_tx_reg_access_v2;
  1162. bolero_reg_access[VA_MACRO] = bolero_va_reg_access_v2;
  1163. } else if (priv->version == BOLERO_VERSION_2_0) {
  1164. bolero_reg_access[VA_MACRO] = bolero_va_reg_access_v3;
  1165. }
  1166. priv->dev = &pdev->dev;
  1167. priv->dev_up = true;
  1168. priv->initial_boot = true;
  1169. priv->regmap = bolero_regmap_init(priv->dev,
  1170. &bolero_regmap_config);
  1171. if (IS_ERR_OR_NULL((void *)(priv->regmap))) {
  1172. dev_err(&pdev->dev, "%s:regmap init failed\n", __func__);
  1173. return -EINVAL;
  1174. }
  1175. priv->read_dev = __bolero_reg_read;
  1176. priv->write_dev = __bolero_reg_write;
  1177. priv->plat_data.handle = (void *) priv;
  1178. priv->plat_data.update_wcd_event = bolero_cdc_update_wcd_event;
  1179. priv->plat_data.register_notifier = bolero_cdc_register_notifier;
  1180. priv->core_hw_vote_count = 0;
  1181. priv->core_audio_vote_count = 0;
  1182. dev_set_drvdata(&pdev->dev, priv);
  1183. mutex_init(&priv->io_lock);
  1184. mutex_init(&priv->clk_lock);
  1185. mutex_init(&priv->vote_lock);
  1186. INIT_WORK(&priv->bolero_add_child_devices_work,
  1187. bolero_add_child_devices);
  1188. schedule_work(&priv->bolero_add_child_devices_work);
  1189. /* Register LPASS core hw vote */
  1190. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  1191. if (IS_ERR(lpass_core_hw_vote)) {
  1192. ret = PTR_ERR(lpass_core_hw_vote);
  1193. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  1194. __func__, "lpass_core_hw_vote", ret);
  1195. lpass_core_hw_vote = NULL;
  1196. ret = 0;
  1197. }
  1198. priv->lpass_core_hw_vote = lpass_core_hw_vote;
  1199. /* Register LPASS audio hw vote */
  1200. lpass_audio_hw_vote = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  1201. if (IS_ERR(lpass_audio_hw_vote)) {
  1202. ret = PTR_ERR(lpass_audio_hw_vote);
  1203. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  1204. __func__, "lpass_audio_hw_vote", ret);
  1205. lpass_audio_hw_vote = NULL;
  1206. ret = 0;
  1207. }
  1208. priv->lpass_audio_hw_vote = lpass_audio_hw_vote;
  1209. return 0;
  1210. }
  1211. static int bolero_remove(struct platform_device *pdev)
  1212. {
  1213. struct bolero_priv *priv = dev_get_drvdata(&pdev->dev);
  1214. if (!priv)
  1215. return -EINVAL;
  1216. of_platform_depopulate(&pdev->dev);
  1217. mutex_destroy(&priv->io_lock);
  1218. mutex_destroy(&priv->clk_lock);
  1219. mutex_destroy(&priv->vote_lock);
  1220. return 0;
  1221. }
  1222. #ifdef CONFIG_PM
  1223. int bolero_runtime_resume(struct device *dev)
  1224. {
  1225. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  1226. int ret = 0;
  1227. mutex_lock(&priv->vote_lock);
  1228. if (priv->lpass_core_hw_vote == NULL) {
  1229. dev_dbg(dev, "%s: Invalid lpass core hw node\n", __func__);
  1230. goto audio_vote;
  1231. }
  1232. if (priv->core_hw_vote_count == 0) {
  1233. ret = digital_cdc_rsc_mgr_hw_vote_enable(priv->lpass_core_hw_vote);
  1234. if (ret < 0) {
  1235. dev_err(dev, "%s:lpass core hw enable failed\n",
  1236. __func__);
  1237. goto audio_vote;
  1238. }
  1239. }
  1240. priv->core_hw_vote_count++;
  1241. trace_printk("%s: hw vote count %d\n",
  1242. __func__, priv->core_hw_vote_count);
  1243. audio_vote:
  1244. if (priv->lpass_audio_hw_vote == NULL) {
  1245. dev_dbg(dev, "%s: Invalid lpass audio hw node\n", __func__);
  1246. goto done;
  1247. }
  1248. if (priv->core_audio_vote_count == 0) {
  1249. ret = digital_cdc_rsc_mgr_hw_vote_enable(priv->lpass_audio_hw_vote);
  1250. if (ret < 0) {
  1251. dev_err(dev, "%s:lpass audio hw enable failed\n",
  1252. __func__);
  1253. goto done;
  1254. }
  1255. }
  1256. priv->core_audio_vote_count++;
  1257. trace_printk("%s: audio vote count %d\n",
  1258. __func__, priv->core_audio_vote_count);
  1259. done:
  1260. mutex_unlock(&priv->vote_lock);
  1261. pm_runtime_set_autosuspend_delay(priv->dev, BOLERO_AUTO_SUSPEND_DELAY);
  1262. return 0;
  1263. }
  1264. EXPORT_SYMBOL(bolero_runtime_resume);
  1265. int bolero_runtime_suspend(struct device *dev)
  1266. {
  1267. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  1268. mutex_lock(&priv->vote_lock);
  1269. if (priv->lpass_core_hw_vote != NULL) {
  1270. if (--priv->core_hw_vote_count == 0)
  1271. digital_cdc_rsc_mgr_hw_vote_disable(
  1272. priv->lpass_core_hw_vote);
  1273. if (priv->core_hw_vote_count < 0)
  1274. priv->core_hw_vote_count = 0;
  1275. } else {
  1276. dev_dbg(dev, "%s: Invalid lpass core hw node\n",
  1277. __func__);
  1278. }
  1279. trace_printk("%s: hw vote count %d\n",
  1280. __func__, priv->core_hw_vote_count);
  1281. if (priv->lpass_audio_hw_vote != NULL) {
  1282. if (--priv->core_audio_vote_count == 0)
  1283. digital_cdc_rsc_mgr_hw_vote_disable(
  1284. priv->lpass_audio_hw_vote);
  1285. if (priv->core_audio_vote_count < 0)
  1286. priv->core_audio_vote_count = 0;
  1287. } else {
  1288. dev_dbg(dev, "%s: Invalid lpass audio hw node\n",
  1289. __func__);
  1290. }
  1291. trace_printk("%s: audio vote count %d\n",
  1292. __func__, priv->core_audio_vote_count);
  1293. mutex_unlock(&priv->vote_lock);
  1294. return 0;
  1295. }
  1296. EXPORT_SYMBOL(bolero_runtime_suspend);
  1297. #endif /* CONFIG_PM */
  1298. bool bolero_check_core_votes(struct device *dev)
  1299. {
  1300. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  1301. bool ret = true;
  1302. mutex_lock(&priv->vote_lock);
  1303. if ((priv->lpass_core_hw_vote && !priv->core_hw_vote_count) ||
  1304. (priv->lpass_audio_hw_vote && !priv->core_audio_vote_count))
  1305. ret = false;
  1306. mutex_unlock(&priv->vote_lock);
  1307. return ret;
  1308. }
  1309. EXPORT_SYMBOL(bolero_check_core_votes);
  1310. static const struct of_device_id bolero_dt_match[] = {
  1311. {.compatible = "qcom,bolero-codec"},
  1312. {}
  1313. };
  1314. MODULE_DEVICE_TABLE(of, bolero_dt_match);
  1315. static struct platform_driver bolero_drv = {
  1316. .driver = {
  1317. .name = "bolero-codec",
  1318. .owner = THIS_MODULE,
  1319. .of_match_table = bolero_dt_match,
  1320. .suppress_bind_attrs = true,
  1321. },
  1322. .probe = bolero_probe,
  1323. .remove = bolero_remove,
  1324. };
  1325. static int bolero_drv_init(void)
  1326. {
  1327. return platform_driver_register(&bolero_drv);
  1328. }
  1329. static void bolero_drv_exit(void)
  1330. {
  1331. platform_driver_unregister(&bolero_drv);
  1332. }
  1333. static int __init bolero_init(void)
  1334. {
  1335. bolero_drv_init();
  1336. bolero_clk_rsc_mgr_init();
  1337. return 0;
  1338. }
  1339. module_init(bolero_init);
  1340. static void __exit bolero_exit(void)
  1341. {
  1342. bolero_clk_rsc_mgr_exit();
  1343. bolero_drv_exit();
  1344. }
  1345. module_exit(bolero_exit);
  1346. MODULE_DESCRIPTION("Bolero driver");
  1347. MODULE_LICENSE("GPL v2");