bolero-cdc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. priv->macro_params[macro_id].reg_evt_listener =
  441. ops->reg_evt_listener;
  442. }
  443. priv->num_dais += ops->num_dais;
  444. priv->num_macros_registered++;
  445. priv->macros_supported[macro_id] = true;
  446. if (priv->num_macros_registered == priv->num_macros) {
  447. ret = bolero_copy_dais_from_macro(priv);
  448. if (ret < 0) {
  449. dev_err(dev, "%s: copy_dais failed\n", __func__);
  450. return ret;
  451. }
  452. if (priv->macros_supported[TX_MACRO] == false) {
  453. bolero_mclk_mux_tbl[WSA_MACRO][MCLK_MUX0] = WSA_MACRO;
  454. priv->current_mclk_mux_macro[WSA_MACRO] = WSA_MACRO;
  455. bolero_mclk_mux_tbl[VA_MACRO][MCLK_MUX0] = VA_MACRO;
  456. priv->current_mclk_mux_macro[VA_MACRO] = VA_MACRO;
  457. }
  458. ret = snd_soc_register_component(dev->parent, &bolero,
  459. priv->bolero_dais, priv->num_dais);
  460. if (ret < 0) {
  461. dev_err(dev, "%s: register codec failed\n", __func__);
  462. return ret;
  463. }
  464. }
  465. return 0;
  466. }
  467. EXPORT_SYMBOL(bolero_register_macro);
  468. /**
  469. * bolero_unregister_macro - De-Register macro from bolero
  470. *
  471. * @dev: macro device ptr.
  472. * @macro_id: ID of macro calling this API.
  473. *
  474. */
  475. void bolero_unregister_macro(struct device *dev, u16 macro_id)
  476. {
  477. struct bolero_priv *priv;
  478. if (!dev) {
  479. pr_err("%s: dev is null\n", __func__);
  480. return;
  481. }
  482. if (!bolero_is_valid_child_dev(dev)) {
  483. dev_err(dev, "%s: macro:%d not in valid registered macro-list\n",
  484. __func__, macro_id);
  485. return;
  486. }
  487. priv = dev_get_drvdata(dev->parent);
  488. if (!priv || (macro_id >= MAX_MACRO)) {
  489. dev_err(dev, "%s: priv is null or invalid macro\n", __func__);
  490. return;
  491. }
  492. priv->macro_params[macro_id].init = NULL;
  493. priv->macro_params[macro_id].num_dais = 0;
  494. priv->macro_params[macro_id].dai_ptr = NULL;
  495. priv->macro_params[macro_id].event_handler = NULL;
  496. priv->macro_params[macro_id].dev = NULL;
  497. if (macro_id == TX_MACRO) {
  498. priv->macro_params[macro_id].reg_wake_irq = NULL;
  499. priv->macro_params[macro_id].clk_switch = NULL;
  500. priv->macro_params[macro_id].reg_evt_listener = NULL;
  501. }
  502. priv->num_dais -= priv->macro_params[macro_id].num_dais;
  503. priv->num_macros_registered--;
  504. /* UNREGISTER CODEC HERE */
  505. if (priv->num_macros - 1 == priv->num_macros_registered)
  506. snd_soc_unregister_component(dev->parent);
  507. }
  508. EXPORT_SYMBOL(bolero_unregister_macro);
  509. static ssize_t bolero_version_read(struct snd_info_entry *entry,
  510. void *file_private_data,
  511. struct file *file,
  512. char __user *buf, size_t count,
  513. loff_t pos)
  514. {
  515. struct bolero_priv *priv;
  516. char buffer[BOLERO_VERSION_ENTRY_SIZE];
  517. int len = 0;
  518. priv = (struct bolero_priv *) entry->private_data;
  519. if (!priv) {
  520. pr_err("%s: bolero priv is null\n", __func__);
  521. return -EINVAL;
  522. }
  523. switch (priv->version) {
  524. case BOLERO_VERSION_1_0:
  525. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_0\n");
  526. break;
  527. case BOLERO_VERSION_1_1:
  528. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_1\n");
  529. break;
  530. case BOLERO_VERSION_1_2:
  531. len = snprintf(buffer, sizeof(buffer), "BOLERO_1_2\n");
  532. break;
  533. default:
  534. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  535. }
  536. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  537. }
  538. static int bolero_ssr_enable(struct device *dev, void *data)
  539. {
  540. struct bolero_priv *priv = data;
  541. int macro_idx;
  542. if (priv->initial_boot) {
  543. priv->initial_boot = false;
  544. return 0;
  545. }
  546. if (priv->rsc_clk_cb)
  547. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_UP);
  548. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  549. if (priv->macro_params[macro_idx].event_handler)
  550. priv->macro_params[macro_idx].event_handler(
  551. priv->component,
  552. BOLERO_MACRO_EVT_CLK_RESET, 0x0);
  553. }
  554. regcache_cache_only(priv->regmap, false);
  555. mutex_lock(&priv->clk_lock);
  556. priv->dev_up = true;
  557. mutex_unlock(&priv->clk_lock);
  558. regcache_mark_dirty(priv->regmap);
  559. regcache_sync(priv->regmap);
  560. /* call ssr event for supported macros */
  561. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  562. if (!priv->macro_params[macro_idx].event_handler)
  563. continue;
  564. priv->macro_params[macro_idx].event_handler(
  565. priv->component,
  566. BOLERO_MACRO_EVT_SSR_UP, 0x0);
  567. }
  568. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_UP);
  569. return 0;
  570. }
  571. static void bolero_ssr_disable(struct device *dev, void *data)
  572. {
  573. struct bolero_priv *priv = data;
  574. int macro_idx;
  575. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_PA_OFF_PRE_SSR);
  576. regcache_cache_only(priv->regmap, true);
  577. mutex_lock(&priv->clk_lock);
  578. priv->dev_up = false;
  579. mutex_unlock(&priv->clk_lock);
  580. if (priv->rsc_clk_cb)
  581. priv->rsc_clk_cb(priv->clk_dev, BOLERO_MACRO_EVT_SSR_DOWN);
  582. /* call ssr event for supported macros */
  583. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  584. if (!priv->macro_params[macro_idx].event_handler)
  585. continue;
  586. priv->macro_params[macro_idx].event_handler(
  587. priv->component,
  588. BOLERO_MACRO_EVT_SSR_DOWN, 0x0);
  589. }
  590. bolero_cdc_notifier_call(priv, BOLERO_WCD_EVT_SSR_DOWN);
  591. }
  592. static struct snd_info_entry_ops bolero_info_ops = {
  593. .read = bolero_version_read,
  594. };
  595. static const struct snd_event_ops bolero_ssr_ops = {
  596. .enable = bolero_ssr_enable,
  597. .disable = bolero_ssr_disable,
  598. };
  599. /*
  600. * bolero_info_create_codec_entry - creates bolero module
  601. * @codec_root: The parent directory
  602. * @component: Codec component instance
  603. *
  604. * Creates bolero module and version entry under the given
  605. * parent directory.
  606. *
  607. * Return: 0 on success or negative error code on failure.
  608. */
  609. int bolero_info_create_codec_entry(struct snd_info_entry *codec_root,
  610. struct snd_soc_component *component)
  611. {
  612. struct snd_info_entry *version_entry;
  613. struct bolero_priv *priv;
  614. struct snd_soc_card *card;
  615. if (!codec_root || !component)
  616. return -EINVAL;
  617. priv = snd_soc_component_get_drvdata(component);
  618. if (priv->entry) {
  619. dev_dbg(priv->dev,
  620. "%s:bolero module already created\n", __func__);
  621. return 0;
  622. }
  623. card = component->card;
  624. priv->entry = snd_info_create_subdir(codec_root->module,
  625. "bolero", codec_root);
  626. if (!priv->entry) {
  627. dev_dbg(component->dev, "%s: failed to create bolero entry\n",
  628. __func__);
  629. return -ENOMEM;
  630. }
  631. version_entry = snd_info_create_card_entry(card->snd_card,
  632. "version",
  633. priv->entry);
  634. if (!version_entry) {
  635. dev_err(component->dev, "%s: failed to create bolero version entry\n",
  636. __func__);
  637. return -ENOMEM;
  638. }
  639. version_entry->private_data = priv;
  640. version_entry->size = BOLERO_VERSION_ENTRY_SIZE;
  641. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  642. version_entry->c.ops = &bolero_info_ops;
  643. if (snd_info_register(version_entry) < 0) {
  644. snd_info_free_entry(version_entry);
  645. return -ENOMEM;
  646. }
  647. priv->version_entry = version_entry;
  648. return 0;
  649. }
  650. EXPORT_SYMBOL(bolero_info_create_codec_entry);
  651. /**
  652. * bolero_register_wake_irq - Register wake irq of Tx macro
  653. *
  654. * @component: codec component ptr.
  655. * @ipc_wakeup: bool to identify ipc_wakeup to be used or HW interrupt line.
  656. *
  657. * Return: 0 on success or negative error code on failure.
  658. */
  659. int bolero_register_wake_irq(struct snd_soc_component *component,
  660. u32 ipc_wakeup)
  661. {
  662. struct bolero_priv *priv = NULL;
  663. if (!component)
  664. return -EINVAL;
  665. priv = snd_soc_component_get_drvdata(component);
  666. if (!priv)
  667. return -EINVAL;
  668. if (!bolero_is_valid_codec_dev(priv->dev)) {
  669. dev_err(component->dev, "%s: invalid codec\n", __func__);
  670. return -EINVAL;
  671. }
  672. if (priv->macro_params[TX_MACRO].reg_wake_irq)
  673. priv->macro_params[TX_MACRO].reg_wake_irq(
  674. component, ipc_wakeup);
  675. return 0;
  676. }
  677. EXPORT_SYMBOL(bolero_register_wake_irq);
  678. /**
  679. * bolero_tx_clk_switch - Switch tx macro clock
  680. *
  681. * @component: pointer to codec component instance.
  682. *
  683. * Returns 0 on success or -EINVAL on error.
  684. */
  685. int bolero_tx_clk_switch(struct snd_soc_component *component)
  686. {
  687. struct bolero_priv *priv = NULL;
  688. int ret = 0;
  689. if (!component)
  690. return -EINVAL;
  691. priv = snd_soc_component_get_drvdata(component);
  692. if (!priv)
  693. return -EINVAL;
  694. if (!bolero_is_valid_codec_dev(priv->dev)) {
  695. dev_err(component->dev, "%s: invalid codec\n", __func__);
  696. return -EINVAL;
  697. }
  698. if (priv->macro_params[TX_MACRO].clk_switch)
  699. ret = priv->macro_params[TX_MACRO].clk_switch(component);
  700. return ret;
  701. }
  702. EXPORT_SYMBOL(bolero_tx_clk_switch);
  703. /**
  704. * bolero_register_event_listener - Register/Deregister to event listener
  705. *
  706. * @component: pointer to codec component instance.
  707. * @enable: when set to 1 registers to event listener otherwise, derigisters
  708. * from the event listener
  709. *
  710. * Returns 0 on success or -EINVAL on error.
  711. */
  712. int bolero_register_event_listener(struct snd_soc_component *component,
  713. bool enable)
  714. {
  715. struct bolero_priv *priv = NULL;
  716. int ret = 0;
  717. if (!component)
  718. return -EINVAL;
  719. priv = snd_soc_component_get_drvdata(component);
  720. if (!priv)
  721. return -EINVAL;
  722. if (!bolero_is_valid_codec_dev(priv->dev)) {
  723. dev_err(component->dev, "%s: invalid codec\n", __func__);
  724. return -EINVAL;
  725. }
  726. if (priv->macro_params[TX_MACRO].reg_evt_listener)
  727. ret = priv->macro_params[TX_MACRO].reg_evt_listener(component,
  728. enable);
  729. return ret;
  730. }
  731. EXPORT_SYMBOL(bolero_register_event_listener);
  732. static int bolero_soc_codec_probe(struct snd_soc_component *component)
  733. {
  734. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  735. int macro_idx, ret = 0;
  736. snd_soc_component_init_regmap(component, priv->regmap);
  737. /* call init for supported macros */
  738. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++) {
  739. if (priv->macro_params[macro_idx].init) {
  740. ret = priv->macro_params[macro_idx].init(component);
  741. if (ret < 0) {
  742. dev_err(component->dev,
  743. "%s: init for macro %d failed\n",
  744. __func__, macro_idx);
  745. goto err;
  746. }
  747. }
  748. }
  749. priv->component = component;
  750. /*
  751. * In order for the ADIE RTC to differentiate between targets
  752. * version info is used.
  753. * Assign 1.0 for target with only one macro
  754. * Assign 1.1 for target with two macros
  755. * Assign 1.2 for target with more than two macros
  756. */
  757. if (priv->num_macros_registered == 1)
  758. priv->version = BOLERO_VERSION_1_0;
  759. else if (priv->num_macros_registered == 2)
  760. priv->version = BOLERO_VERSION_1_1;
  761. else if (priv->num_macros_registered > 2)
  762. priv->version = BOLERO_VERSION_1_2;
  763. ret = snd_event_client_register(priv->dev, &bolero_ssr_ops, priv);
  764. if (!ret) {
  765. snd_event_notify(priv->dev, SND_EVENT_UP);
  766. } else {
  767. dev_err(component->dev,
  768. "%s: Registration with SND event FWK failed ret = %d\n",
  769. __func__, ret);
  770. goto err;
  771. }
  772. dev_dbg(component->dev, "%s: bolero soc codec probe success\n",
  773. __func__);
  774. err:
  775. return ret;
  776. }
  777. static void bolero_soc_codec_remove(struct snd_soc_component *component)
  778. {
  779. struct bolero_priv *priv = dev_get_drvdata(component->dev);
  780. int macro_idx;
  781. snd_event_client_deregister(priv->dev);
  782. /* call exit for supported macros */
  783. for (macro_idx = START_MACRO; macro_idx < MAX_MACRO; macro_idx++)
  784. if (priv->macro_params[macro_idx].exit)
  785. priv->macro_params[macro_idx].exit(component);
  786. return;
  787. }
  788. static const struct snd_soc_component_driver bolero = {
  789. .name = DRV_NAME,
  790. .probe = bolero_soc_codec_probe,
  791. .remove = bolero_soc_codec_remove,
  792. };
  793. static void bolero_add_child_devices(struct work_struct *work)
  794. {
  795. struct bolero_priv *priv;
  796. bool split_codec = false;
  797. struct platform_device *pdev;
  798. struct device_node *node;
  799. int ret = 0, count = 0;
  800. struct wcd_ctrl_platform_data *platdata = NULL;
  801. char plat_dev_name[BOLERO_CDC_STRING_LEN] = "";
  802. priv = container_of(work, struct bolero_priv,
  803. bolero_add_child_devices_work);
  804. if (!priv) {
  805. pr_err("%s: Memory for bolero priv does not exist\n",
  806. __func__);
  807. return;
  808. }
  809. if (!priv->dev || !priv->dev->of_node) {
  810. dev_err(priv->dev, "%s: DT node for bolero does not exist\n",
  811. __func__);
  812. return;
  813. }
  814. platdata = &priv->plat_data;
  815. priv->child_count = 0;
  816. for_each_available_child_of_node(priv->dev->of_node, node) {
  817. split_codec = false;
  818. if (of_find_property(node, "qcom,split-codec", NULL)) {
  819. split_codec = true;
  820. dev_dbg(priv->dev, "%s: split codec slave exists\n",
  821. __func__);
  822. }
  823. strlcpy(plat_dev_name, node->name,
  824. (BOLERO_CDC_STRING_LEN - 1));
  825. pdev = platform_device_alloc(plat_dev_name, -1);
  826. if (!pdev) {
  827. dev_err(priv->dev, "%s: pdev memory alloc failed\n",
  828. __func__);
  829. ret = -ENOMEM;
  830. goto err;
  831. }
  832. pdev->dev.parent = priv->dev;
  833. pdev->dev.of_node = node;
  834. if (split_codec) {
  835. priv->dev->platform_data = platdata;
  836. priv->wcd_dev = &pdev->dev;
  837. }
  838. ret = platform_device_add(pdev);
  839. if (ret) {
  840. dev_err(&pdev->dev,
  841. "%s: Cannot add platform device\n",
  842. __func__);
  843. platform_device_put(pdev);
  844. goto fail_pdev_add;
  845. }
  846. if (priv->child_count < BOLERO_CDC_CHILD_DEVICES_MAX)
  847. priv->pdev_child_devices[priv->child_count++] = pdev;
  848. else
  849. goto err;
  850. }
  851. return;
  852. fail_pdev_add:
  853. for (count = 0; count < priv->child_count; count++)
  854. platform_device_put(priv->pdev_child_devices[count]);
  855. err:
  856. return;
  857. }
  858. static int bolero_probe(struct platform_device *pdev)
  859. {
  860. struct bolero_priv *priv;
  861. u32 num_macros = 0;
  862. int ret;
  863. struct clk *lpass_core_hw_vote = NULL;
  864. struct clk *lpass_audio_hw_vote = NULL;
  865. priv = devm_kzalloc(&pdev->dev, sizeof(struct bolero_priv),
  866. GFP_KERNEL);
  867. if (!priv)
  868. return -ENOMEM;
  869. ret = of_property_read_u32(pdev->dev.of_node, "qcom,num-macros",
  870. &num_macros);
  871. if (ret) {
  872. dev_err(&pdev->dev,
  873. "%s:num-macros property not found\n",
  874. __func__);
  875. return ret;
  876. }
  877. priv->num_macros = num_macros;
  878. if (priv->num_macros > MAX_MACRO) {
  879. dev_err(&pdev->dev,
  880. "%s:num_macros(%d) > MAX_MACRO(%d) than supported\n",
  881. __func__, priv->num_macros, MAX_MACRO);
  882. return -EINVAL;
  883. }
  884. priv->va_without_decimation = of_property_read_bool(pdev->dev.of_node,
  885. "qcom,va-without-decimation");
  886. if (priv->va_without_decimation)
  887. bolero_reg_access[VA_MACRO] = bolero_va_top_reg_access;
  888. priv->dev = &pdev->dev;
  889. priv->dev_up = true;
  890. priv->initial_boot = true;
  891. priv->regmap = bolero_regmap_init(priv->dev,
  892. &bolero_regmap_config);
  893. if (IS_ERR_OR_NULL((void *)(priv->regmap))) {
  894. dev_err(&pdev->dev, "%s:regmap init failed\n", __func__);
  895. return -EINVAL;
  896. }
  897. priv->read_dev = __bolero_reg_read;
  898. priv->write_dev = __bolero_reg_write;
  899. priv->plat_data.handle = (void *) priv;
  900. priv->plat_data.update_wcd_event = bolero_cdc_update_wcd_event;
  901. priv->plat_data.register_notifier = bolero_cdc_register_notifier;
  902. priv->core_hw_vote_count = 0;
  903. priv->core_audio_vote_count = 0;
  904. dev_set_drvdata(&pdev->dev, priv);
  905. mutex_init(&priv->io_lock);
  906. mutex_init(&priv->clk_lock);
  907. mutex_init(&priv->vote_lock);
  908. INIT_WORK(&priv->bolero_add_child_devices_work,
  909. bolero_add_child_devices);
  910. schedule_work(&priv->bolero_add_child_devices_work);
  911. /* Register LPASS core hw vote */
  912. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  913. if (IS_ERR(lpass_core_hw_vote)) {
  914. ret = PTR_ERR(lpass_core_hw_vote);
  915. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  916. __func__, "lpass_core_hw_vote", ret);
  917. lpass_core_hw_vote = NULL;
  918. ret = 0;
  919. }
  920. priv->lpass_core_hw_vote = lpass_core_hw_vote;
  921. /* Register LPASS audio hw vote */
  922. lpass_audio_hw_vote = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  923. if (IS_ERR(lpass_audio_hw_vote)) {
  924. ret = PTR_ERR(lpass_audio_hw_vote);
  925. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  926. __func__, "lpass_audio_hw_vote", ret);
  927. lpass_audio_hw_vote = NULL;
  928. ret = 0;
  929. }
  930. priv->lpass_audio_hw_vote = lpass_audio_hw_vote;
  931. return 0;
  932. }
  933. static int bolero_remove(struct platform_device *pdev)
  934. {
  935. struct bolero_priv *priv = dev_get_drvdata(&pdev->dev);
  936. if (!priv)
  937. return -EINVAL;
  938. of_platform_depopulate(&pdev->dev);
  939. mutex_destroy(&priv->io_lock);
  940. mutex_destroy(&priv->clk_lock);
  941. mutex_destroy(&priv->vote_lock);
  942. return 0;
  943. }
  944. int bolero_runtime_resume(struct device *dev)
  945. {
  946. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  947. int ret = 0;
  948. if (priv->lpass_core_hw_vote == NULL) {
  949. dev_dbg(dev, "%s: Invalid lpass core hw node\n", __func__);
  950. return 0;
  951. }
  952. mutex_lock(&priv->vote_lock);
  953. if (priv->core_hw_vote_count == 0) {
  954. ret = clk_prepare_enable(priv->lpass_core_hw_vote);
  955. if (ret < 0) {
  956. dev_err(dev, "%s:lpass core hw enable failed\n",
  957. __func__);
  958. goto audio_vote;
  959. }
  960. }
  961. priv->core_hw_vote_count++;
  962. audio_vote:
  963. if (priv->lpass_audio_hw_vote == NULL) {
  964. dev_dbg(dev, "%s: Invalid lpass audio hw node\n", __func__);
  965. goto done;
  966. }
  967. if (priv->core_audio_vote_count == 0) {
  968. ret = clk_prepare_enable(priv->lpass_audio_hw_vote);
  969. if (ret < 0) {
  970. dev_err(dev, "%s:lpass audio hw enable failed\n",
  971. __func__);
  972. goto done;
  973. }
  974. }
  975. priv->core_audio_vote_count++;
  976. done:
  977. mutex_unlock(&priv->vote_lock);
  978. pm_runtime_set_autosuspend_delay(priv->dev, BOLERO_AUTO_SUSPEND_DELAY);
  979. return 0;
  980. }
  981. EXPORT_SYMBOL(bolero_runtime_resume);
  982. int bolero_runtime_suspend(struct device *dev)
  983. {
  984. struct bolero_priv *priv = dev_get_drvdata(dev->parent);
  985. mutex_lock(&priv->vote_lock);
  986. if (priv->lpass_core_hw_vote != NULL) {
  987. if (--priv->core_hw_vote_count == 0)
  988. clk_disable_unprepare(priv->lpass_core_hw_vote);
  989. if (priv->core_hw_vote_count < 0)
  990. priv->core_hw_vote_count = 0;
  991. } else {
  992. dev_dbg(dev, "%s: Invalid lpass core hw node\n",
  993. __func__);
  994. }
  995. if (priv->lpass_audio_hw_vote != NULL) {
  996. if (--priv->core_audio_vote_count == 0)
  997. clk_disable_unprepare(priv->lpass_audio_hw_vote);
  998. if (priv->core_audio_vote_count < 0)
  999. priv->core_audio_vote_count = 0;
  1000. } else {
  1001. dev_dbg(dev, "%s: Invalid lpass audio hw node\n",
  1002. __func__);
  1003. }
  1004. mutex_unlock(&priv->vote_lock);
  1005. return 0;
  1006. }
  1007. EXPORT_SYMBOL(bolero_runtime_suspend);
  1008. static const struct of_device_id bolero_dt_match[] = {
  1009. {.compatible = "qcom,bolero-codec"},
  1010. {}
  1011. };
  1012. MODULE_DEVICE_TABLE(of, bolero_dt_match);
  1013. static struct platform_driver bolero_drv = {
  1014. .driver = {
  1015. .name = "bolero-codec",
  1016. .owner = THIS_MODULE,
  1017. .of_match_table = bolero_dt_match,
  1018. .suppress_bind_attrs = true,
  1019. },
  1020. .probe = bolero_probe,
  1021. .remove = bolero_remove,
  1022. };
  1023. static int bolero_drv_init(void)
  1024. {
  1025. return platform_driver_register(&bolero_drv);
  1026. }
  1027. static void bolero_drv_exit(void)
  1028. {
  1029. platform_driver_unregister(&bolero_drv);
  1030. }
  1031. static int __init bolero_init(void)
  1032. {
  1033. bolero_drv_init();
  1034. bolero_clk_rsc_mgr_init();
  1035. return 0;
  1036. }
  1037. module_init(bolero_init);
  1038. static void __exit bolero_exit(void)
  1039. {
  1040. bolero_clk_rsc_mgr_exit();
  1041. bolero_drv_exit();
  1042. }
  1043. module_exit(bolero_exit);
  1044. MODULE_DESCRIPTION("Bolero driver");
  1045. MODULE_LICENSE("GPL v2");