bolero-cdc.c 26 KB

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