bolero-cdc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/of_platform.h>
  5. #include <linux/module.h>
  6. #include <linux/io.h>
  7. #include <linux/init.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/printk.h>
  10. #include <linux/delay.h>
  11. #include <linux/kernel.h>
  12. #include <linux/clk.h>
  13. #include <soc/snd_event.h>
  14. #include <linux/pm_runtime.h>
  15. #include <soc/swr-common.h>
  16. #include "bolero-cdc.h"
  17. #include "internal.h"
  18. #include "bolero-clk-rsc.h"
  19. #define DRV_NAME "bolero_codec"
  20. #define BOLERO_VERSION_1_0 0x0001
  21. #define BOLERO_VERSION_1_1 0x0002
  22. #define BOLERO_VERSION_1_2 0x0003
  23. #define BOLERO_VERSION_ENTRY_SIZE 32
  24. #define BOLERO_CDC_STRING_LEN 80
  25. static const struct snd_soc_component_driver bolero;
  26. /* pm runtime auto suspend timer in msecs */
  27. #define BOLERO_AUTO_SUSPEND_DELAY 100 /* delay in msec */
  28. /* MCLK_MUX table for all macros */
  29. static u16 bolero_mclk_mux_tbl[MAX_MACRO][MCLK_MUX_MAX] = {
  30. {TX_MACRO, VA_MACRO},
  31. {TX_MACRO, RX_MACRO},
  32. {TX_MACRO, WSA_MACRO},
  33. {TX_MACRO, VA_MACRO},
  34. };
  35. static bool bolero_is_valid_codec_dev(struct device *dev);
  36. int bolero_set_port_map(struct snd_soc_component *component,
  37. u32 size, void *data)
  38. {
  39. struct bolero_priv *priv = NULL;
  40. struct swr_mstr_port_map *map = NULL;
  41. u16 idx;
  42. if (!component || (size == 0) || !data)
  43. return -EINVAL;
  44. priv = snd_soc_component_get_drvdata(component);
  45. if (!priv)
  46. return -EINVAL;
  47. if (!bolero_is_valid_codec_dev(priv->dev)) {
  48. dev_err(priv->dev, "%s: invalid codec\n", __func__);
  49. return -EINVAL;
  50. }
  51. map = (struct swr_mstr_port_map *)data;
  52. for (idx = 0; idx < size; idx++) {
  53. if (priv->macro_params[map->id].set_port_map)
  54. priv->macro_params[map->id].set_port_map(component,
  55. map->uc,
  56. SWR_MSTR_PORT_LEN,
  57. map->swr_port_params);
  58. map += 1;
  59. }
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(bolero_set_port_map);
  63. static void bolero_ahb_write_device(char __iomem *io_base,
  64. u16 reg, u8 value)
  65. {
  66. u32 temp = (u32)(value) & 0x000000FF;
  67. iowrite32(temp, io_base + reg);
  68. }
  69. static void bolero_ahb_read_device(char __iomem *io_base,
  70. u16 reg, u8 *value)
  71. {
  72. u32 temp;
  73. temp = ioread32(io_base + reg);
  74. *value = (u8)temp;
  75. }
  76. static int __bolero_reg_read(struct bolero_priv *priv,
  77. u16 macro_id, u16 reg, u8 *val)
  78. {
  79. int ret = 0;
  80. mutex_lock(&priv->clk_lock);
  81. if (!priv->dev_up) {
  82. dev_dbg_ratelimited(priv->dev,
  83. "%s: SSR in progress, exit\n", __func__);
  84. ret = -EINVAL;
  85. goto ssr_err;
  86. }
  87. if (priv->macro_params[VA_MACRO].dev)
  88. pm_runtime_get_sync(priv->macro_params[VA_MACRO].dev);
  89. /* Request Clk before register access */
  90. ret = bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  91. priv->macro_params[macro_id].default_clk_id,
  92. priv->macro_params[macro_id].clk_id_req,
  93. true);
  94. if (ret < 0) {
  95. dev_err_ratelimited(priv->dev,
  96. "%s: Failed to enable clock, ret:%d\n", __func__, ret);
  97. goto err;
  98. }
  99. bolero_ahb_read_device(
  100. priv->macro_params[macro_id].io_base, reg, val);
  101. bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  102. priv->macro_params[macro_id].default_clk_id,
  103. priv->macro_params[macro_id].clk_id_req,
  104. false);
  105. err:
  106. if (priv->macro_params[VA_MACRO].dev) {
  107. pm_runtime_mark_last_busy(priv->macro_params[VA_MACRO].dev);
  108. pm_runtime_put_autosuspend(priv->macro_params[VA_MACRO].dev);
  109. }
  110. ssr_err:
  111. mutex_unlock(&priv->clk_lock);
  112. return ret;
  113. }
  114. static int __bolero_reg_write(struct bolero_priv *priv,
  115. u16 macro_id, u16 reg, u8 val)
  116. {
  117. int ret = 0;
  118. mutex_lock(&priv->clk_lock);
  119. if (!priv->dev_up) {
  120. dev_dbg_ratelimited(priv->dev,
  121. "%s: SSR in progress, exit\n", __func__);
  122. ret = -EINVAL;
  123. goto ssr_err;
  124. }
  125. if (priv->macro_params[VA_MACRO].dev)
  126. pm_runtime_get_sync(priv->macro_params[VA_MACRO].dev);
  127. /* Request Clk before register access */
  128. ret = bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  129. priv->macro_params[macro_id].default_clk_id,
  130. priv->macro_params[macro_id].clk_id_req,
  131. true);
  132. if (ret < 0) {
  133. dev_err_ratelimited(priv->dev,
  134. "%s: Failed to enable clock, ret:%d\n", __func__, ret);
  135. goto err;
  136. }
  137. bolero_ahb_write_device(
  138. priv->macro_params[macro_id].io_base, reg, val);
  139. bolero_clk_rsc_request_clock(priv->macro_params[macro_id].dev,
  140. priv->macro_params[macro_id].default_clk_id,
  141. priv->macro_params[macro_id].clk_id_req,
  142. false);
  143. err:
  144. if (priv->macro_params[VA_MACRO].dev) {
  145. pm_runtime_mark_last_busy(priv->macro_params[VA_MACRO].dev);
  146. pm_runtime_put_autosuspend(priv->macro_params[VA_MACRO].dev);
  147. }
  148. ssr_err:
  149. mutex_unlock(&priv->clk_lock);
  150. return ret;
  151. }
  152. static int bolero_cdc_update_wcd_event(void *handle, u16 event, u32 data)
  153. {
  154. struct bolero_priv *priv = (struct bolero_priv *)handle;
  155. if (!priv) {
  156. pr_err("%s:Invalid bolero priv handle\n", __func__);
  157. return -EINVAL;
  158. }
  159. switch (event) {
  160. case WCD_BOLERO_EVT_RX_MUTE:
  161. if (priv->macro_params[RX_MACRO].event_handler)
  162. priv->macro_params[RX_MACRO].event_handler(
  163. priv->component,
  164. BOLERO_MACRO_EVT_RX_MUTE, data);
  165. break;
  166. case WCD_BOLERO_EVT_IMPED_TRUE:
  167. if (priv->macro_params[RX_MACRO].event_handler)
  168. priv->macro_params[RX_MACRO].event_handler(
  169. priv->component,
  170. BOLERO_MACRO_EVT_IMPED_TRUE, data);
  171. break;
  172. case WCD_BOLERO_EVT_IMPED_FALSE:
  173. if (priv->macro_params[RX_MACRO].event_handler)
  174. priv->macro_params[RX_MACRO].event_handler(
  175. priv->component,
  176. BOLERO_MACRO_EVT_IMPED_FALSE, data);
  177. break;
  178. case WCD_BOLERO_EVT_RX_COMPANDER_SOFT_RST:
  179. if (priv->macro_params[RX_MACRO].event_handler)
  180. priv->macro_params[RX_MACRO].event_handler(
  181. priv->component,
  182. BOLERO_MACRO_EVT_RX_COMPANDER_SOFT_RST, data);
  183. break;
  184. default:
  185. dev_err(priv->dev, "%s: Invalid event %d trigger from wcd\n",
  186. __func__, event);
  187. return -EINVAL;
  188. }
  189. return 0;
  190. }
  191. static int bolero_cdc_register_notifier(void *handle,
  192. struct notifier_block *nblock,
  193. bool enable)
  194. {
  195. struct bolero_priv *priv = (struct bolero_priv *)handle;
  196. if (!priv) {
  197. pr_err("%s: bolero priv is null\n", __func__);
  198. return -EINVAL;
  199. }
  200. if (enable)
  201. return blocking_notifier_chain_register(&priv->notifier,
  202. nblock);
  203. return blocking_notifier_chain_unregister(&priv->notifier,
  204. nblock);
  205. }
  206. static void bolero_cdc_notifier_call(struct bolero_priv *priv,
  207. u32 data)
  208. {
  209. dev_dbg(priv->dev, "%s: notifier call, data:%d\n", __func__, data);
  210. blocking_notifier_call_chain(&priv->notifier,
  211. data, (void *)priv->wcd_dev);
  212. }
  213. static bool bolero_is_valid_child_dev(struct device *dev)
  214. {
  215. if (of_device_is_compatible(dev->parent->of_node, "qcom,bolero-codec"))
  216. return true;
  217. return false;
  218. }
  219. static bool bolero_is_valid_codec_dev(struct device *dev)
  220. {
  221. if (of_device_is_compatible(dev->of_node, "qcom,bolero-codec"))
  222. return true;
  223. return false;
  224. }
  225. /**
  226. * bolero_clear_amic_tx_hold - clears AMIC register on analog codec
  227. *
  228. * @dev: bolero device ptr.
  229. *
  230. */
  231. void bolero_clear_amic_tx_hold(struct device *dev, u16 adc_n)
  232. {
  233. struct bolero_priv *priv;
  234. u16 event;
  235. u16 amic = 0;
  236. if (!dev) {
  237. pr_err("%s: dev is null\n", __func__);
  238. return;
  239. }
  240. if (!bolero_is_valid_codec_dev(dev)) {
  241. pr_err("%s: invalid codec\n", __func__);
  242. return;
  243. }
  244. priv = dev_get_drvdata(dev);
  245. if (!priv) {
  246. dev_err(dev, "%s: priv is null\n", __func__);
  247. return;
  248. }
  249. event = BOLERO_WCD_EVT_TX_CH_HOLD_CLEAR;
  250. if (adc_n == BOLERO_ADC0)
  251. amic = 0x1;
  252. else if (adc_n == BOLERO_ADC1)
  253. amic = 0x2;
  254. else if (adc_n == BOLERO_ADC2)
  255. amic = 0x2;
  256. else if (adc_n == BOLERO_ADC3)
  257. amic = 0x3;
  258. else
  259. return;
  260. bolero_cdc_notifier_call(priv, (amic << 0x10 | event));
  261. }
  262. EXPORT_SYMBOL(bolero_clear_amic_tx_hold);
  263. /**
  264. * bolero_get_device_ptr - Get child or macro device ptr
  265. *
  266. * @dev: bolero device ptr.
  267. * @macro_id: ID of macro calling this API.
  268. *
  269. * Returns dev ptr on success or NULL on error.
  270. */
  271. struct device *bolero_get_device_ptr(struct device *dev, u16 macro_id)
  272. {
  273. struct bolero_priv *priv;
  274. if (!dev) {
  275. pr_err("%s: dev is null\n", __func__);
  276. return NULL;
  277. }
  278. if (!bolero_is_valid_codec_dev(dev)) {
  279. pr_err("%s: invalid codec\n", __func__);
  280. return NULL;
  281. }
  282. priv = dev_get_drvdata(dev);
  283. if (!priv || (macro_id >= MAX_MACRO)) {
  284. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  285. return NULL;
  286. }
  287. return priv->macro_params[macro_id].dev;
  288. }
  289. EXPORT_SYMBOL(bolero_get_device_ptr);
  290. /**
  291. * bolero_get_rsc_clk_device_ptr - Get rsc clk device ptr
  292. *
  293. * @dev: bolero device ptr.
  294. *
  295. * Returns dev ptr on success or NULL on error.
  296. */
  297. struct device *bolero_get_rsc_clk_device_ptr(struct device *dev)
  298. {
  299. struct bolero_priv *priv;
  300. if (!dev) {
  301. pr_err("%s: dev is null\n", __func__);
  302. return NULL;
  303. }
  304. if (!bolero_is_valid_codec_dev(dev)) {
  305. pr_err("%s: invalid codec\n", __func__);
  306. return NULL;
  307. }
  308. priv = dev_get_drvdata(dev);
  309. if (!priv) {
  310. dev_err(dev, "%s: priv is null\n", __func__);
  311. return NULL;
  312. }
  313. return priv->clk_dev;
  314. }
  315. EXPORT_SYMBOL(bolero_get_rsc_clk_device_ptr);
  316. static int bolero_copy_dais_from_macro(struct bolero_priv *priv)
  317. {
  318. struct snd_soc_dai_driver *dai_ptr;
  319. u16 macro_idx;
  320. /* memcpy into bolero_dais all macro dais */
  321. if (!priv->bolero_dais)
  322. priv->bolero_dais = devm_kzalloc(priv->dev,
  323. priv->num_dais *
  324. sizeof(
  325. struct snd_soc_dai_driver),
  326. GFP_KERNEL);
  327. if (!priv->bolero_dais)
  328. return -ENOMEM;
  329. dai_ptr = priv->bolero_dais;
  330. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  331. if (priv->macro_params[macro_idx].dai_ptr) {
  332. memcpy(dai_ptr,
  333. priv->macro_params[macro_idx].dai_ptr,
  334. priv->macro_params[macro_idx].num_dais *
  335. sizeof(struct snd_soc_dai_driver));
  336. dai_ptr += priv->macro_params[macro_idx].num_dais;
  337. }
  338. }
  339. return 0;
  340. }
  341. /**
  342. * bolero_register_res_clk - Registers rsc clk driver to bolero
  343. *
  344. * @dev: rsc clk device ptr.
  345. * @rsc_clk_cb: event handler callback for notifications like SSR
  346. *
  347. * Returns 0 on success or -EINVAL on error.
  348. */
  349. int bolero_register_res_clk(struct device *dev, rsc_clk_cb_t rsc_clk_cb)
  350. {
  351. struct bolero_priv *priv;
  352. if (!dev || !rsc_clk_cb) {
  353. pr_err("%s: dev or rsc_clk_cb is null\n", __func__);
  354. return -EINVAL;
  355. }
  356. if (!bolero_is_valid_child_dev(dev)) {
  357. dev_err(dev, "%s: child device :%pK not added yet\n",
  358. __func__, dev);
  359. return -EINVAL;
  360. }
  361. priv = dev_get_drvdata(dev->parent);
  362. if (!priv) {
  363. dev_err(dev, "%s: priv is null\n", __func__);
  364. return -EINVAL;
  365. }
  366. priv->clk_dev = dev;
  367. priv->rsc_clk_cb = rsc_clk_cb;
  368. return 0;
  369. }
  370. EXPORT_SYMBOL(bolero_register_res_clk);
  371. /**
  372. * bolero_unregister_res_clk - Unregisters rsc clk driver from bolero
  373. *
  374. * @dev: resource clk device ptr.
  375. */
  376. void bolero_unregister_res_clk(struct device *dev)
  377. {
  378. struct bolero_priv *priv;
  379. if (!dev) {
  380. pr_err("%s: dev is NULL\n", __func__);
  381. return;
  382. }
  383. if (!bolero_is_valid_child_dev(dev)) {
  384. dev_err(dev, "%s: child device :%pK not added\n",
  385. __func__, dev);
  386. return;
  387. }
  388. priv = dev_get_drvdata(dev->parent);
  389. if (!priv) {
  390. dev_err(dev, "%s: priv is null\n", __func__);
  391. return;
  392. }
  393. priv->clk_dev = NULL;
  394. priv->rsc_clk_cb = NULL;
  395. }
  396. EXPORT_SYMBOL(bolero_unregister_res_clk);
  397. /**
  398. * bolero_register_macro - Registers macro to bolero
  399. *
  400. * @dev: macro device ptr.
  401. * @macro_id: ID of macro calling this API.
  402. * @ops: macro params to register.
  403. *
  404. * Returns 0 on success or -EINVAL on error.
  405. */
  406. int bolero_register_macro(struct device *dev, u16 macro_id,
  407. struct macro_ops *ops)
  408. {
  409. struct bolero_priv *priv;
  410. int ret = -EINVAL;
  411. if (!dev || !ops) {
  412. pr_err("%s: dev or ops is null\n", __func__);
  413. return -EINVAL;
  414. }
  415. if (!bolero_is_valid_child_dev(dev)) {
  416. dev_err(dev, "%s: child device for macro:%d not added yet\n",
  417. __func__, macro_id);
  418. return -EINVAL;
  419. }
  420. priv = dev_get_drvdata(dev->parent);
  421. if (!priv || (macro_id >= MAX_MACRO)) {
  422. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  423. return -EINVAL;
  424. }
  425. priv->macro_params[macro_id].clk_id_req = ops->clk_id_req;
  426. priv->macro_params[macro_id].default_clk_id = ops->default_clk_id;
  427. priv->macro_params[macro_id].init = ops->init;
  428. priv->macro_params[macro_id].exit = ops->exit;
  429. priv->macro_params[macro_id].io_base = ops->io_base;
  430. priv->macro_params[macro_id].num_dais = ops->num_dais;
  431. priv->macro_params[macro_id].dai_ptr = ops->dai_ptr;
  432. priv->macro_params[macro_id].event_handler = ops->event_handler;
  433. priv->macro_params[macro_id].set_port_map = ops->set_port_map;
  434. priv->macro_params[macro_id].dev = dev;
  435. priv->current_mclk_mux_macro[macro_id] =
  436. bolero_mclk_mux_tbl[macro_id][MCLK_MUX0];
  437. if (macro_id == TX_MACRO) {
  438. priv->macro_params[macro_id].reg_wake_irq = ops->reg_wake_irq;
  439. priv->macro_params[macro_id].clk_switch = ops->clk_switch;
  440. }
  441. priv->num_dais += ops->num_dais;
  442. priv->num_macros_registered++;
  443. priv->macros_supported[macro_id] = true;
  444. if (priv->num_macros_registered == priv->num_macros) {
  445. ret = bolero_copy_dais_from_macro(priv);
  446. if (ret < 0) {
  447. dev_err(dev, "%s: copy_dais failed\n", __func__);
  448. return ret;
  449. }
  450. if (priv->macros_supported[TX_MACRO] == false) {
  451. bolero_mclk_mux_tbl[WSA_MACRO][MCLK_MUX0] = WSA_MACRO;
  452. priv->current_mclk_mux_macro[WSA_MACRO] = WSA_MACRO;
  453. bolero_mclk_mux_tbl[VA_MACRO][MCLK_MUX0] = VA_MACRO;
  454. priv->current_mclk_mux_macro[VA_MACRO] = VA_MACRO;
  455. }
  456. ret = snd_soc_register_component(dev->parent, &bolero,
  457. priv->bolero_dais, priv->num_dais);
  458. if (ret < 0) {
  459. dev_err(dev, "%s: register codec failed\n", __func__);
  460. return ret;
  461. }
  462. }
  463. return 0;
  464. }
  465. EXPORT_SYMBOL(bolero_register_macro);
  466. /**
  467. * bolero_unregister_macro - De-Register macro from bolero
  468. *
  469. * @dev: macro device ptr.
  470. * @macro_id: ID of macro calling this API.
  471. *
  472. */
  473. void bolero_unregister_macro(struct device *dev, u16 macro_id)
  474. {
  475. struct bolero_priv *priv;
  476. if (!dev) {
  477. pr_err("%s: dev is null\n", __func__);
  478. return;
  479. }
  480. if (!bolero_is_valid_child_dev(dev)) {
  481. dev_err(dev, "%s: macro:%d not in valid registered macro-list\n",
  482. __func__, macro_id);
  483. return;
  484. }
  485. priv = dev_get_drvdata(dev->parent);
  486. if (!priv || (macro_id >= MAX_MACRO)) {
  487. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  488. return;
  489. }
  490. priv->macro_params[macro_id].init = NULL;
  491. priv->macro_params[macro_id].num_dais = 0;
  492. priv->macro_params[macro_id].dai_ptr = NULL;
  493. priv->macro_params[macro_id].event_handler = NULL;
  494. priv->macro_params[macro_id].dev = NULL;
  495. if (macro_id == TX_MACRO) {
  496. priv->macro_params[macro_id].reg_wake_irq = NULL;
  497. priv->macro_params[macro_id].clk_switch = NULL;
  498. }
  499. priv->num_dais -= priv->macro_params[macro_id].num_dais;
  500. priv->num_macros_registered--;
  501. /* UNREGISTER CODEC HERE */
  502. if (priv->num_macros - 1 == priv->num_macros_registered)
  503. snd_soc_unregister_component(dev->parent);
  504. }
  505. EXPORT_SYMBOL(bolero_unregister_macro);
  506. static ssize_t bolero_version_read(struct snd_info_entry *entry,
  507. void *file_private_data,
  508. struct file *file,
  509. char __user *buf, size_t count,
  510. loff_t pos)
  511. {
  512. struct bolero_priv *priv;
  513. char buffer[BOLERO_VERSION_ENTRY_SIZE];
  514. int len = 0;
  515. priv = (struct bolero_priv *) entry->private_data;
  516. if (!priv) {
  517. pr_err("%s: bolero priv is null\n", __func__);
  518. return -EINVAL;
  519. }
  520. switch (priv->version) {
  521. case BOLERO_VERSION_1_0:
  522. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_0\n");
  523. break;
  524. case BOLERO_VERSION_1_1:
  525. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_1\n");
  526. break;
  527. case BOLERO_VERSION_1_2:
  528. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_2\n");
  529. break;
  530. default:
  531. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  532. }
  533. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  534. }
  535. static int bolero_ssr_enable(struct device *dev, void *data)
  536. {
  537. struct bolero_priv *priv = data;
  538. int macro_idx;
  539. if (priv->initial_boot) {
  540. priv->initial_boot = false;
  541. return 0;
  542. }
  543. if (priv->rsc_clk_cb)
  544. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_UP);
  545. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  546. if (priv->macro_params[macro_idx].event_handler)
  547. priv->macro_params[macro_idx].event_handler(
  548. priv->component,
  549. BOLERO_MACRO_EVT_CLK_RESET, 0x0);
  550. }
  551. regcache_cache_only(priv->regmap, false);
  552. mutex_lock(&priv->clk_lock);
  553. priv->dev_up = true;
  554. mutex_unlock(&priv->clk_lock);
  555. regcache_mark_dirty(priv->regmap);
  556. regcache_sync(priv->regmap);
  557. /* call ssr event for supported macros */
  558. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  559. if (!priv->macro_params[macro_idx].event_handler)
  560. continue;
  561. priv->macro_params[macro_idx].event_handler(
  562. priv->component,
  563. BOLERO_MACRO_EVT_SSR_UP, 0x0);
  564. }
  565. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_UP);
  566. return 0;
  567. }
  568. static void bolero_ssr_disable(struct device *dev, void *data)
  569. {
  570. struct bolero_priv *priv = data;
  571. int macro_idx;
  572. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_PA_OFF_PRE_SSR);
  573. regcache_cache_only(priv->regmap, true);
  574. mutex_lock(&priv->clk_lock);
  575. priv->dev_up = false;
  576. mutex_unlock(&priv->clk_lock);
  577. if (priv->rsc_clk_cb)
  578. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_DOWN);
  579. /* call ssr event for supported macros */
  580. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  581. if (!priv->macro_params[macro_idx].event_handler)
  582. continue;
  583. priv->macro_params[macro_idx].event_handler(
  584. priv->component,
  585. BOLERO_MACRO_EVT_SSR_DOWN, 0x0);
  586. }
  587. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_DOWN);
  588. }
  589. static struct snd_info_entry_ops bolero_info_ops = {
  590. .read = bolero_version_read,
  591. };
  592. static const struct snd_event_ops bolero_ssr_ops = {
  593. .enable = bolero_ssr_enable,
  594. .disable = bolero_ssr_disable,
  595. };
  596. /*
  597. * bolero_info_create_codec_entry - creates bolero module
  598. * @codec_root: The parent directory
  599. * @component: Codec component instance
  600. *
  601. * Creates bolero module and version entry under the given
  602. * parent directory.
  603. *
  604. * Return: 0 on success or negative error code on failure.
  605. */
  606. int bolero_info_create_codec_entry(struct snd_info_entry *codec_root,
  607. struct snd_soc_component *component)
  608. {
  609. struct snd_info_entry *version_entry;
  610. struct bolero_priv *priv;
  611. struct snd_soc_card *card;
  612. if (!codec_root || !component)
  613. return -EINVAL;
  614. priv = snd_soc_component_get_drvdata(component);
  615. if (priv->entry) {
  616. dev_dbg(priv->dev,
  617. "%s:bolero module already created\n", __func__);
  618. return 0;
  619. }
  620. card = component->card;
  621. priv->entry = snd_info_create_subdir(codec_root->module,
  622. "bolero", codec_root);
  623. if (!priv->entry) {
  624. dev_dbg(component->dev, "%s: failed to create bolero entry\n",
  625. __func__);
  626. return -ENOMEM;
  627. }
  628. version_entry = snd_info_create_card_entry(card->snd_card,
  629. "version",
  630. priv->entry);
  631. if (!version_entry) {
  632. dev_err(component->dev, "%s: failed to create bolero version entry\n",
  633. __func__);
  634. return -ENOMEM;
  635. }
  636. version_entry->private_data = priv;
  637. version_entry->size = BOLERO_VERSION_ENTRY_SIZE;
  638. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  639. version_entry->c.ops = &bolero_info_ops;
  640. if (snd_info_register(version_entry) < 0) {
  641. snd_info_free_entry(version_entry);
  642. return -ENOMEM;
  643. }
  644. priv->version_entry = version_entry;
  645. return 0;
  646. }
  647. EXPORT_SYMBOL(bolero_info_create_codec_entry);
  648. /**
  649. * bolero_register_wake_irq - Register wake irq of Tx macro
  650. *
  651. * @component: codec component ptr.
  652. * @ipc_wakeup: bool to identify ipc_wakeup to be used or HW interrupt line.
  653. *
  654. * Return: 0 on success or negative error code on failure.
  655. */
  656. int bolero_register_wake_irq(struct snd_soc_component *component,
  657. u32 ipc_wakeup)
  658. {
  659. struct bolero_priv *priv = NULL;
  660. if (!component)
  661. return -EINVAL;
  662. priv = snd_soc_component_get_drvdata(component);
  663. if (!priv)
  664. return -EINVAL;
  665. if (!bolero_is_valid_codec_dev(priv->dev)) {
  666. dev_err(component->dev, "%s: invalid codec\n", __func__);
  667. return -EINVAL;
  668. }
  669. if (priv->macro_params[TX_MACRO].reg_wake_irq)
  670. priv->macro_params[TX_MACRO].reg_wake_irq(
  671. component, ipc_wakeup);
  672. return 0;
  673. }
  674. EXPORT_SYMBOL(bolero_register_wake_irq);
  675. /**
  676. * bolero_tx_clk_switch - Switch tx macro clock
  677. *
  678. * @component: pointer to codec component instance.
  679. *
  680. * Returns 0 on success or -EINVAL on error.
  681. */
  682. int bolero_tx_clk_switch(struct snd_soc_component *component)
  683. {
  684. struct bolero_priv *priv = NULL;
  685. int ret = 0;
  686. if (!component)
  687. return -EINVAL;
  688. priv = snd_soc_component_get_drvdata(component);
  689. if (!priv)
  690. return -EINVAL;
  691. if (!bolero_is_valid_codec_dev(priv->dev)) {
  692. dev_err(component->dev, "%s: invalid codec\n", __func__);
  693. return -EINVAL;
  694. }
  695. if (priv->macro_params[TX_MACRO].clk_switch)
  696. ret = priv->macro_params[TX_MACRO].clk_switch(component);
  697. return ret;
  698. }
  699. EXPORT_SYMBOL(bolero_tx_clk_switch);
  700. static int bolero_soc_codec_probe(struct snd_soc_component *component)
  701. {
  702. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  703. int macro_idx, ret = 0;
  704. snd_soc_component_init_regmap(component, priv->regmap);
  705. /* call init for supported macros */
  706. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  707. if (priv->macro_params[macro_idx].init) {
  708. ret = priv->macro_params[macro_idx].init(component);
  709. if (ret < 0) {
  710. dev_err(component->dev,
  711. "%s: init for macro %d failed\n",
  712. __func__, macro_idx);
  713. goto err;
  714. }
  715. }
  716. }
  717. priv->component = component;
  718. /*
  719. * In order for the ADIE RTC to differentiate between targets
  720. * version info is used.
  721. * Assign 1.0 for target with only one macro
  722. * Assign 1.1 for target with two macros
  723. * Assign 1.2 for target with more than two macros
  724. */
  725. if (priv->num_macros_registered == 1)
  726. priv->version = BOLERO_VERSION_1_0;
  727. else if (priv->num_macros_registered == 2)
  728. priv->version = BOLERO_VERSION_1_1;
  729. else if (priv->num_macros_registered > 2)
  730. priv->version = BOLERO_VERSION_1_2;
  731. ret = snd_event_client_register(priv->dev, &bolero_ssr_ops, priv);
  732. if (!ret) {
  733. snd_event_notify(priv->dev, SND_EVENT_UP);
  734. } else {
  735. dev_err(component->dev,
  736. "%s: Registration with SND event FWK failed ret = %d\n",
  737. __func__, ret);
  738. goto err;
  739. }
  740. dev_dbg(component->dev, "%s: bolero soc codec probe success\n",
  741. __func__);
  742. err:
  743. return ret;
  744. }
  745. static void bolero_soc_codec_remove(struct snd_soc_component *component)
  746. {
  747. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  748. int macro_idx;
  749. snd_event_client_deregister(priv->dev);
  750. /* call exit for supported macros */
  751. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++)
  752. if (priv->macro_params[macro_idx].exit)
  753. priv->macro_params[macro_idx].exit(component);
  754. return;
  755. }
  756. static const struct snd_soc_component_driver bolero = {
  757. .name = DRV_NAME,
  758. .probe = bolero_soc_codec_probe,
  759. .remove = bolero_soc_codec_remove,
  760. };
  761. static void bolero_add_child_devices(struct work_struct *work)
  762. {
  763. struct bolero_priv *priv;
  764. bool split_codec = false;
  765. struct platform_device *pdev;
  766. struct device_node *node;
  767. int ret = 0, count = 0;
  768. struct wcd_ctrl_platform_data *platdata = NULL;
  769. char plat_dev_name[BOLERO_CDC_STRING_LEN] = "";
  770. priv = container_of(work, struct bolero_priv,
  771. bolero_add_child_devices_work);
  772. if (!priv) {
  773. pr_err("%s: Memory for bolero priv does not exist\n",
  774. __func__);
  775. return;
  776. }
  777. if (!priv->dev || !priv->dev->of_node) {
  778. dev_err(priv->dev, "%s: DT node for bolero does not exist\n",
  779. __func__);
  780. return;
  781. }
  782. platdata = &priv->plat_data;
  783. priv->child_count = 0;
  784. for_each_available_child_of_node(priv->dev->of_node, node) {
  785. split_codec = false;
  786. if (of_find_property(node, "qcom,split-codec", NULL)) {
  787. split_codec = true;
  788. dev_dbg(priv->dev, "%s: split codec slave exists\n",
  789. __func__);
  790. }
  791. strlcpy(plat_dev_name, node->name,
  792. (BOLERO_CDC_STRING_LEN - 1));
  793. pdev = platform_device_alloc(plat_dev_name, -1);
  794. if (!pdev) {
  795. dev_err(priv->dev, "%s: pdev memory alloc failed\n",
  796. __func__);
  797. ret = -ENOMEM;
  798. goto err;
  799. }
  800. pdev->dev.parent = priv->dev;
  801. pdev->dev.of_node = node;
  802. if (split_codec) {
  803. priv->dev->platform_data = platdata;
  804. priv->wcd_dev = &pdev->dev;
  805. }
  806. ret = platform_device_add(pdev);
  807. if (ret) {
  808. dev_err(&pdev->dev,
  809. "%s: Cannot add platform device\n",
  810. __func__);
  811. platform_device_put(pdev);
  812. goto fail_pdev_add;
  813. }
  814. if (priv->child_count < BOLERO_CDC_CHILD_DEVICES_MAX)
  815. priv->pdev_child_devices[priv->child_count++] = pdev;
  816. else
  817. goto err;
  818. }
  819. return;
  820. fail_pdev_add:
  821. for (count = 0; count < priv->child_count; count++)
  822. platform_device_put(priv->pdev_child_devices[count]);
  823. err:
  824. return;
  825. }
  826. static int bolero_probe(struct platform_device *pdev)
  827. {
  828. struct bolero_priv *priv;
  829. u32 num_macros = 0;
  830. int ret;
  831. struct clk *lpass_core_hw_vote = NULL;
  832. struct clk *lpass_audio_hw_vote = NULL;
  833. priv = devm_kzalloc(&pdev->dev, sizeof(struct bolero_priv),
  834. GFP_KERNEL);
  835. if (!priv)
  836. return -ENOMEM;
  837. ret = of_property_read_u32(pdev->dev.of_node, "qcom,num-macros",
  838. &num_macros);
  839. if (ret) {
  840. dev_err(&pdev->dev,
  841. "%s:num-macros property not found\n",
  842. __func__);
  843. return ret;
  844. }
  845. priv->num_macros = num_macros;
  846. if (priv->num_macros > MAX_MACRO) {
  847. dev_err(&pdev->dev,
  848. "%s:num_macros(%d) > MAX_MACRO(%d) than supported\n",
  849. __func__, priv->num_macros, MAX_MACRO);
  850. return -EINVAL;
  851. }
  852. priv->va_without_decimation = of_property_read_bool(pdev->dev.of_node,
  853. "qcom,va-without-decimation");
  854. if (priv->va_without_decimation)
  855. bolero_reg_access[VA_MACRO] = bolero_va_top_reg_access;
  856. priv->dev = &pdev->dev;
  857. priv->dev_up = true;
  858. priv->initial_boot = true;
  859. priv->regmap = bolero_regmap_init(priv->dev,
  860. &bolero_regmap_config);
  861. if (IS_ERR_OR_NULL((void *)(priv->regmap))) {
  862. dev_err(&pdev->dev, "%s:regmap init failed\n", __func__);
  863. return -EINVAL;
  864. }
  865. priv->read_dev = __bolero_reg_read;
  866. priv->write_dev = __bolero_reg_write;
  867. priv->plat_data.handle = (void *) priv;
  868. priv->plat_data.update_wcd_event = bolero_cdc_update_wcd_event;
  869. priv->plat_data.register_notifier = bolero_cdc_register_notifier;
  870. dev_set_drvdata(&pdev->dev, priv);
  871. mutex_init(&priv->io_lock);
  872. mutex_init(&priv->clk_lock);
  873. INIT_WORK(&priv->bolero_add_child_devices_work,
  874. bolero_add_child_devices);
  875. schedule_work(&priv->bolero_add_child_devices_work);
  876. /* Register LPASS core hw vote */
  877. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  878. if (IS_ERR(lpass_core_hw_vote)) {
  879. ret = PTR_ERR(lpass_core_hw_vote);
  880. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  881. __func__, "lpass_core_hw_vote", ret);
  882. lpass_core_hw_vote = NULL;
  883. ret = 0;
  884. }
  885. priv->lpass_core_hw_vote = lpass_core_hw_vote;
  886. /* Register LPASS audio hw vote */
  887. lpass_audio_hw_vote = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  888. if (IS_ERR(lpass_audio_hw_vote)) {
  889. ret = PTR_ERR(lpass_audio_hw_vote);
  890. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  891. __func__, "lpass_audio_hw_vote", ret);
  892. lpass_audio_hw_vote = NULL;
  893. ret = 0;
  894. }
  895. priv->lpass_audio_hw_vote = lpass_audio_hw_vote;
  896. return 0;
  897. }
  898. static int bolero_remove(struct platform_device *pdev)
  899. {
  900. struct bolero_priv *priv = dev_get_drvdata(&pdev->dev);
  901. if (!priv)
  902. return -EINVAL;
  903. of_platform_depopulate(&pdev->dev);
  904. mutex_destroy(&priv->io_lock);
  905. mutex_destroy(&priv->clk_lock);
  906. return 0;
  907. }
  908. int bolero_runtime_resume(struct device *dev)
  909. {
  910. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  911. int ret = 0;
  912. if (priv->lpass_core_hw_vote == NULL) {
  913. dev_dbg(dev, "%s: Invalid lpass core hw node\n", __func__);
  914. return 0;
  915. }
  916. ret = clk_prepare_enable(priv->lpass_core_hw_vote);
  917. if (ret < 0)
  918. dev_err(dev, "%s:lpass core hw enable failed\n",
  919. __func__);
  920. if (priv->lpass_audio_hw_vote == NULL) {
  921. dev_dbg(dev, "%s: Invalid lpass audio hw node\n", __func__);
  922. return 0;
  923. }
  924. ret = clk_prepare_enable(priv->lpass_audio_hw_vote);
  925. if (ret < 0)
  926. dev_err(dev, "%s:lpass audio hw enable failed\n",
  927. __func__);
  928. pm_runtime_set_autosuspend_delay(priv->dev, BOLERO_AUTO_SUSPEND_DELAY);
  929. return 0;
  930. }
  931. EXPORT_SYMBOL(bolero_runtime_resume);
  932. int bolero_runtime_suspend(struct device *dev)
  933. {
  934. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  935. if (priv->lpass_core_hw_vote != NULL)
  936. clk_disable_unprepare(priv->lpass_core_hw_vote);
  937. else
  938. dev_dbg(dev, "%s: Invalid lpass core hw node\n",
  939. __func__);
  940. if (priv->lpass_audio_hw_vote != NULL)
  941. clk_disable_unprepare(priv->lpass_audio_hw_vote);
  942. else
  943. dev_dbg(dev, "%s: Invalid lpass audio hw node\n",
  944. __func__);
  945. return 0;
  946. }
  947. EXPORT_SYMBOL(bolero_runtime_suspend);
  948. static const struct of_device_id bolero_dt_match[] = {
  949. {.compatible = "qcom,bolero-codec"},
  950. {}
  951. };
  952. MODULE_DEVICE_TABLE(of, bolero_dt_match);
  953. static struct platform_driver bolero_drv = {
  954. .driver = {
  955. .name = "bolero-codec",
  956. .owner = THIS_MODULE,
  957. .of_match_table = bolero_dt_match,
  958. .suppress_bind_attrs = true,
  959. },
  960. .probe = bolero_probe,
  961. .remove = bolero_remove,
  962. };
  963. static int bolero_drv_init(void)
  964. {
  965. return platform_driver_register(&bolero_drv);
  966. }
  967. static void bolero_drv_exit(void)
  968. {
  969. platform_driver_unregister(&bolero_drv);
  970. }
  971. static int __init bolero_init(void)
  972. {
  973. bolero_drv_init();
  974. bolero_clk_rsc_mgr_init();
  975. return 0;
  976. }
  977. module_init(bolero_init);
  978. static void __exit bolero_exit(void)
  979. {
  980. bolero_clk_rsc_mgr_exit();
  981. bolero_drv_exit();
  982. }
  983. module_exit(bolero_exit);
  984. MODULE_DESCRIPTION("Bolero driver");
  985. MODULE_LICENSE("GPL v2");