bolero-cdc.c 41 KB

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