bolero-cdc.c 23 KB

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