bolero-cdc.c 26 KB

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