bolero-cdc.c 27 KB

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