bolero-cdc.c 26 KB

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