wcd9xxx-resmgr.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /* Copyright (c) 2012-2013, 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/module.h>
  13. #include <linux/init.h>
  14. #include <linux/firmware.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/device.h>
  18. #include <linux/printk.h>
  19. #include <linux/ratelimit.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/mfd/wcd9xxx/core.h>
  22. #include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
  23. #include <linux/mfd/wcd9xxx/wcd9320_registers.h>
  24. #include <linux/mfd/wcd9xxx/pdata.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/soc-dapm.h>
  29. #include <sound/tlv.h>
  30. #include <linux/bitops.h>
  31. #include <linux/delay.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/kernel.h>
  34. #include <linux/gpio.h>
  35. #include "wcd9xxx-resmgr.h"
  36. #include "msm8x10_wcd_registers.h"
  37. static char wcd9xxx_event_string[][64] = {
  38. "WCD9XXX_EVENT_INVALID",
  39. "WCD9XXX_EVENT_PRE_RCO_ON",
  40. "WCD9XXX_EVENT_POST_RCO_ON",
  41. "WCD9XXX_EVENT_PRE_RCO_OFF",
  42. "WCD9XXX_EVENT_POST_RCO_OFF",
  43. "WCD9XXX_EVENT_PRE_MCLK_ON",
  44. "WCD9XXX_EVENT_POST_MCLK_ON",
  45. "WCD9XXX_EVENT_PRE_MCLK_OFF",
  46. "WCD9XXX_EVENT_POST_MCLK_OFF",
  47. "WCD9XXX_EVENT_PRE_BG_OFF",
  48. "WCD9XXX_EVENT_POST_BG_OFF",
  49. "WCD9XXX_EVENT_PRE_BG_AUDIO_ON",
  50. "WCD9XXX_EVENT_POST_BG_AUDIO_ON",
  51. "WCD9XXX_EVENT_PRE_BG_MBHC_ON",
  52. "WCD9XXX_EVENT_POST_BG_MBHC_ON",
  53. "WCD9XXX_EVENT_PRE_MICBIAS_1_OFF",
  54. "WCD9XXX_EVENT_POST_MICBIAS_1_OFF",
  55. "WCD9XXX_EVENT_PRE_MICBIAS_2_OFF",
  56. "WCD9XXX_EVENT_POST_MICBIAS_2_OFF",
  57. "WCD9XXX_EVENT_PRE_MICBIAS_3_OFF",
  58. "WCD9XXX_EVENT_POST_MICBIAS_3_OFF",
  59. "WCD9XXX_EVENT_PRE_MICBIAS_4_OFF",
  60. "WCD9XXX_EVENT_POST_MICBIAS_4_OFF",
  61. "WCD9XXX_EVENT_PRE_MICBIAS_1_ON",
  62. "WCD9XXX_EVENT_POST_MICBIAS_1_ON",
  63. "WCD9XXX_EVENT_PRE_MICBIAS_2_ON",
  64. "WCD9XXX_EVENT_POST_MICBIAS_2_ON",
  65. "WCD9XXX_EVENT_PRE_MICBIAS_3_ON",
  66. "WCD9XXX_EVENT_POST_MICBIAS_3_ON",
  67. "WCD9XXX_EVENT_PRE_MICBIAS_4_ON",
  68. "WCD9XXX_EVENT_POST_MICBIAS_4_ON",
  69. "WCD9XXX_EVENT_PRE_CFILT_1_OFF",
  70. "WCD9XXX_EVENT_POST_CFILT_1_OFF",
  71. "WCD9XXX_EVENT_PRE_CFILT_2_OFF",
  72. "WCD9XXX_EVENT_POST_CFILT_2_OFF",
  73. "WCD9XXX_EVENT_PRE_CFILT_3_OFF",
  74. "WCD9XXX_EVENT_POST_CFILT_3_OFF",
  75. "WCD9XXX_EVENT_PRE_CFILT_1_ON",
  76. "WCD9XXX_EVENT_POST_CFILT_1_ON",
  77. "WCD9XXX_EVENT_PRE_CFILT_2_ON",
  78. "WCD9XXX_EVENT_POST_CFILT_2_ON",
  79. "WCD9XXX_EVENT_PRE_CFILT_3_ON",
  80. "WCD9XXX_EVENT_POST_CFILT_3_ON",
  81. "WCD9XXX_EVENT_PRE_HPHL_PA_ON",
  82. "WCD9XXX_EVENT_POST_HPHL_PA_OFF",
  83. "WCD9XXX_EVENT_PRE_HPHR_PA_ON",
  84. "WCD9XXX_EVENT_POST_HPHR_PA_OFF",
  85. "WCD9XXX_EVENT_POST_RESUME",
  86. "WCD9XXX_EVENT_PRE_TX_1_3_ON",
  87. "WCD9XXX_EVENT_POST_TX_1_3_OFF",
  88. "WCD9XXX_EVENT_LAST",
  89. };
  90. struct wcd9xxx_resmgr_cond_entry {
  91. unsigned short reg;
  92. int shift;
  93. bool invert;
  94. enum wcd9xxx_resmgr_cond cond;
  95. struct list_head list;
  96. };
  97. static enum wcd9xxx_clock_type wcd9xxx_save_clock(struct wcd9xxx_resmgr
  98. *resmgr);
  99. static void wcd9xxx_restore_clock(struct wcd9xxx_resmgr *resmgr,
  100. enum wcd9xxx_clock_type type);
  101. const char *wcd9xxx_get_event_string(enum wcd9xxx_notify_event type)
  102. {
  103. return wcd9xxx_event_string[type];
  104. }
  105. void wcd9xxx_resmgr_notifier_call(struct wcd9xxx_resmgr *resmgr,
  106. const enum wcd9xxx_notify_event e)
  107. {
  108. pr_debug("%s: notifier call event %d\n", __func__, e);
  109. blocking_notifier_call_chain(&resmgr->notifier, e, resmgr);
  110. }
  111. static void wcd9xxx_disable_bg(struct wcd9xxx_resmgr *resmgr)
  112. {
  113. /* Notify bg mode change */
  114. wcd9xxx_resmgr_notifier_call(resmgr, WCD9XXX_EVENT_PRE_BG_OFF);
  115. /* Disable bg */
  116. snd_soc_update_bits(resmgr->codec, WCD9XXX_A_BIAS_CENTRAL_BG_CTL,
  117. 0x03, 0x00);
  118. usleep_range(100, 100);
  119. /* Notify bg mode change */
  120. wcd9xxx_resmgr_notifier_call(resmgr, WCD9XXX_EVENT_POST_BG_OFF);
  121. }
  122. /*
  123. * BG enablement should always enable in slow mode.
  124. * The fast mode doesn't need to be enabled as fast mode BG is to be driven
  125. * by MBHC override.
  126. */
  127. static void wcd9xxx_enable_bg(struct wcd9xxx_resmgr *resmgr)
  128. {
  129. struct snd_soc_codec *codec = resmgr->codec;
  130. /* Enable BG in slow mode and precharge */
  131. snd_soc_update_bits(codec, WCD9XXX_A_BIAS_CENTRAL_BG_CTL, 0x80, 0x80);
  132. snd_soc_update_bits(codec, WCD9XXX_A_BIAS_CENTRAL_BG_CTL, 0x04, 0x04);
  133. snd_soc_update_bits(codec, WCD9XXX_A_BIAS_CENTRAL_BG_CTL, 0x01, 0x01);
  134. usleep_range(1000, 1000);
  135. snd_soc_update_bits(codec, WCD9XXX_A_BIAS_CENTRAL_BG_CTL, 0x80, 0x00);
  136. }
  137. static void wcd9xxx_enable_bg_audio(struct wcd9xxx_resmgr *resmgr)
  138. {
  139. /* Notify bandgap mode change */
  140. wcd9xxx_resmgr_notifier_call(resmgr, WCD9XXX_EVENT_PRE_BG_AUDIO_ON);
  141. wcd9xxx_enable_bg(resmgr);
  142. /* Notify bandgap mode change */
  143. wcd9xxx_resmgr_notifier_call(resmgr, WCD9XXX_EVENT_POST_BG_AUDIO_ON);
  144. }
  145. static void wcd9xxx_enable_bg_mbhc(struct wcd9xxx_resmgr *resmgr)
  146. {
  147. struct snd_soc_codec *codec = resmgr->codec;
  148. /* Notify bandgap mode change */
  149. wcd9xxx_resmgr_notifier_call(resmgr, WCD9XXX_EVENT_PRE_BG_MBHC_ON);
  150. /*
  151. * mclk should be off or clk buff source souldn't be VBG
  152. * Let's turn off mclk always
  153. */
  154. if (resmgr->codec_type != WCD9XXX_CDC_TYPE_HELICON)
  155. WARN_ON(snd_soc_read(codec, WCD9XXX_A_CLK_BUFF_EN2) & (1 << 2));
  156. wcd9xxx_enable_bg(resmgr);
  157. /* Notify bandgap mode change */
  158. wcd9xxx_resmgr_notifier_call(resmgr, WCD9XXX_EVENT_POST_BG_MBHC_ON);
  159. }
  160. static void wcd9xxx_disable_clock_block(struct wcd9xxx_resmgr *resmgr)
  161. {
  162. struct snd_soc_codec *codec = resmgr->codec;
  163. pr_debug("%s: enter\n", __func__);
  164. WCD9XXX_BG_CLK_ASSERT_LOCKED(resmgr);
  165. /* Notify */
  166. if (resmgr->clk_type == WCD9XXX_CLK_RCO)
  167. wcd9xxx_resmgr_notifier_call(resmgr, WCD9XXX_EVENT_PRE_RCO_OFF);
  168. else
  169. wcd9xxx_resmgr_notifier_call(resmgr,
  170. WCD9XXX_EVENT_PRE_MCLK_OFF);
  171. /* Disable clock */
  172. if (resmgr->codec_type != WCD9XXX_CDC_TYPE_HELICON) {
  173. snd_soc_update_bits(codec, WCD9XXX_A_CLK_BUFF_EN2, 0x04, 0x00);
  174. #if defined(CONFIG_MACH_VIENNA)
  175. usleep_range(100, 100);
  176. #else
  177. usleep_range(50, 50);
  178. #endif
  179. snd_soc_update_bits(codec, WCD9XXX_A_CLK_BUFF_EN2, 0x02, 0x02);
  180. snd_soc_update_bits(codec, WCD9XXX_A_CLK_BUFF_EN1, 0x05, 0x00);
  181. #if defined(CONFIG_MACH_VIENNA)
  182. usleep_range(100, 100);
  183. #else
  184. usleep_range(50, 50);
  185. #endif
  186. }
  187. /* Notify */
  188. if (resmgr->clk_type == WCD9XXX_CLK_RCO) {
  189. wcd9xxx_resmgr_notifier_call(resmgr,
  190. WCD9XXX_EVENT_POST_RCO_OFF);
  191. } else {
  192. if (resmgr->codec_type == WCD9XXX_CDC_TYPE_HELICON)
  193. snd_soc_update_bits(codec,
  194. MSM8X10_WCD_A_CDC_CLK_PDM_CTL, 0x03, 0x00);
  195. wcd9xxx_resmgr_notifier_call(resmgr,
  196. WCD9XXX_EVENT_POST_MCLK_OFF);
  197. }
  198. pr_debug("%s: leave\n", __func__);
  199. }
  200. void wcd9xxx_resmgr_post_ssr(struct wcd9xxx_resmgr *resmgr)
  201. {
  202. int old_bg_audio_users, old_bg_mbhc_users;
  203. int old_clk_rco_users, old_clk_mclk_users;
  204. pr_debug("%s: enter\n", __func__);
  205. WCD9XXX_BG_CLK_LOCK(resmgr);
  206. old_bg_audio_users = resmgr->bg_audio_users;
  207. old_bg_mbhc_users = resmgr->bg_mbhc_users;
  208. old_clk_rco_users = resmgr->clk_rco_users;
  209. old_clk_mclk_users = resmgr->clk_mclk_users;
  210. resmgr->bg_audio_users = 0;
  211. resmgr->bg_mbhc_users = 0;
  212. resmgr->bandgap_type = WCD9XXX_BANDGAP_OFF;
  213. resmgr->clk_rco_users = 0;
  214. resmgr->clk_mclk_users = 0;
  215. resmgr->clk_type = WCD9XXX_CLK_OFF;
  216. if (old_bg_audio_users) {
  217. while (old_bg_audio_users--)
  218. wcd9xxx_resmgr_get_bandgap(resmgr,
  219. WCD9XXX_BANDGAP_AUDIO_MODE);
  220. }
  221. if (old_bg_mbhc_users) {
  222. while (old_bg_mbhc_users--)
  223. wcd9xxx_resmgr_get_bandgap(resmgr,
  224. WCD9XXX_BANDGAP_MBHC_MODE);
  225. }
  226. if (old_clk_mclk_users) {
  227. while (old_clk_mclk_users--)
  228. wcd9xxx_resmgr_get_clk_block(resmgr, WCD9XXX_CLK_MCLK);
  229. }
  230. if (old_clk_rco_users) {
  231. while (old_clk_rco_users--)
  232. wcd9xxx_resmgr_get_clk_block(resmgr, WCD9XXX_CLK_RCO);
  233. }
  234. WCD9XXX_BG_CLK_UNLOCK(resmgr);
  235. pr_debug("%s: leave\n", __func__);
  236. }
  237. /*
  238. * wcd9xxx_resmgr_get_bandgap : Vote for bandgap ref
  239. * choice : WCD9XXX_BANDGAP_AUDIO_MODE, WCD9XXX_BANDGAP_MBHC_MODE
  240. */
  241. void wcd9xxx_resmgr_get_bandgap(struct wcd9xxx_resmgr *resmgr,
  242. const enum wcd9xxx_bandgap_type choice)
  243. {
  244. enum wcd9xxx_clock_type clock_save;
  245. pr_debug("%s: enter, wants %d\n", __func__, choice);
  246. WCD9XXX_BG_CLK_ASSERT_LOCKED(resmgr);
  247. switch (choice) {
  248. case WCD9XXX_BANDGAP_AUDIO_MODE:
  249. resmgr->bg_audio_users++;
  250. if (resmgr->bg_audio_users == 1 && resmgr->bg_mbhc_users) {
  251. /*
  252. * Current bg is MBHC mode, about to switch to
  253. * audio mode.
  254. */
  255. WARN_ON(resmgr->bandgap_type !=
  256. WCD9XXX_BANDGAP_MBHC_MODE);
  257. /* BG mode can be changed only with clock off */
  258. clock_save = wcd9xxx_save_clock(resmgr);
  259. /* Swtich BG mode */
  260. wcd9xxx_disable_bg(resmgr);
  261. wcd9xxx_enable_bg_audio(resmgr);
  262. /* restore clock */
  263. wcd9xxx_restore_clock(resmgr, clock_save);
  264. } else if (resmgr->bg_audio_users == 1) {
  265. /* currently off, just enable it */
  266. WARN_ON(resmgr->bandgap_type != WCD9XXX_BANDGAP_OFF);
  267. wcd9xxx_enable_bg_audio(resmgr);
  268. }
  269. resmgr->bandgap_type = WCD9XXX_BANDGAP_AUDIO_MODE;
  270. break;
  271. case WCD9XXX_BANDGAP_MBHC_MODE:
  272. resmgr->bg_mbhc_users++;
  273. if (resmgr->bandgap_type == WCD9XXX_BANDGAP_MBHC_MODE ||
  274. resmgr->bandgap_type == WCD9XXX_BANDGAP_AUDIO_MODE)
  275. /* do nothing */
  276. break;
  277. /* bg mode can be changed only with clock off */
  278. clock_save = wcd9xxx_save_clock(resmgr);
  279. /* enable bg with MBHC mode */
  280. wcd9xxx_enable_bg_mbhc(resmgr);
  281. /* restore clock */
  282. wcd9xxx_restore_clock(resmgr, clock_save);
  283. /* save current mode */
  284. resmgr->bandgap_type = WCD9XXX_BANDGAP_MBHC_MODE;
  285. break;
  286. default:
  287. pr_err("%s: Error, Invalid bandgap settings\n", __func__);
  288. break;
  289. }
  290. pr_debug("%s: bg users audio %d, mbhc %d\n", __func__,
  291. resmgr->bg_audio_users, resmgr->bg_mbhc_users);
  292. }
  293. /*
  294. * wcd9xxx_resmgr_put_bandgap : Unvote bandgap ref that has been voted
  295. * choice : WCD9XXX_BANDGAP_AUDIO_MODE, WCD9XXX_BANDGAP_MBHC_MODE
  296. */
  297. void wcd9xxx_resmgr_put_bandgap(struct wcd9xxx_resmgr *resmgr,
  298. enum wcd9xxx_bandgap_type choice)
  299. {
  300. enum wcd9xxx_clock_type clock_save;
  301. pr_debug("%s: enter choice %d\n", __func__, choice);
  302. WCD9XXX_BG_CLK_ASSERT_LOCKED(resmgr);
  303. switch (choice) {
  304. case WCD9XXX_BANDGAP_AUDIO_MODE:
  305. if (--resmgr->bg_audio_users == 0) {
  306. if (resmgr->bg_mbhc_users) {
  307. /* bg mode can be changed only with clock off */
  308. clock_save = wcd9xxx_save_clock(resmgr);
  309. /* switch to MBHC mode */
  310. wcd9xxx_enable_bg_mbhc(resmgr);
  311. /* restore clock */
  312. wcd9xxx_restore_clock(resmgr, clock_save);
  313. resmgr->bandgap_type =
  314. WCD9XXX_BANDGAP_MBHC_MODE;
  315. } else {
  316. /* turn off */
  317. wcd9xxx_disable_bg(resmgr);
  318. resmgr->bandgap_type = WCD9XXX_BANDGAP_OFF;
  319. }
  320. }
  321. break;
  322. case WCD9XXX_BANDGAP_MBHC_MODE:
  323. WARN(resmgr->bandgap_type == WCD9XXX_BANDGAP_OFF,
  324. "Unexpected bandgap type %d\n", resmgr->bandgap_type);
  325. if (--resmgr->bg_mbhc_users == 0 &&
  326. resmgr->bandgap_type == WCD9XXX_BANDGAP_MBHC_MODE) {
  327. wcd9xxx_disable_bg(resmgr);
  328. resmgr->bandgap_type = WCD9XXX_BANDGAP_OFF;
  329. }
  330. break;
  331. default:
  332. pr_err("%s: Error, Invalid bandgap settings\n", __func__);
  333. break;
  334. }
  335. pr_debug("%s: bg users audio %d, mbhc %d\n", __func__,
  336. resmgr->bg_audio_users, resmgr->bg_mbhc_users);
  337. }
  338. void wcd9xxx_resmgr_enable_rx_bias(struct wcd9xxx_resmgr *resmgr, u32 enable)
  339. {
  340. struct snd_soc_codec *codec = resmgr->codec;
  341. if (enable) {
  342. resmgr->rx_bias_count++;
  343. if (resmgr->rx_bias_count == 1)
  344. snd_soc_update_bits(codec, WCD9XXX_A_RX_COM_BIAS,
  345. 0x80, 0x80);
  346. } else {
  347. resmgr->rx_bias_count--;
  348. if (!resmgr->rx_bias_count)
  349. snd_soc_update_bits(codec, WCD9XXX_A_RX_COM_BIAS,
  350. 0x80, 0x00);
  351. }
  352. }
  353. int wcd9xxx_resmgr_enable_config_mode(struct wcd9xxx_resmgr *resmgr, int enable)
  354. {
  355. struct snd_soc_codec *codec = resmgr->codec;
  356. pr_debug("%s: enable = %d\n", __func__, enable);
  357. if (enable) {
  358. snd_soc_update_bits(codec, WCD9XXX_A_RC_OSC_FREQ, 0x10, 0);
  359. /* bandgap mode to fast */
  360. snd_soc_write(codec, WCD9XXX_A_BIAS_OSC_BG_CTL, 0x17);
  361. usleep_range(5, 5);
  362. snd_soc_update_bits(codec, WCD9XXX_A_RC_OSC_FREQ, 0x80, 0x80);
  363. snd_soc_update_bits(codec, WCD9XXX_A_RC_OSC_TEST, 0x80, 0x80);
  364. usleep_range(10, 10);
  365. snd_soc_update_bits(codec, WCD9XXX_A_RC_OSC_TEST, 0x80, 0);
  366. #if defined(CONFIG_MACH_VIENNA)
  367. usleep_range(20000, 20000);
  368. #else
  369. usleep_range(10000, 10000);
  370. #endif
  371. if (resmgr->codec_type != WCD9XXX_CDC_TYPE_HELICON)
  372. snd_soc_update_bits(codec, WCD9XXX_A_CLK_BUFF_EN1,
  373. 0x08, 0x08);
  374. else
  375. snd_soc_update_bits(codec,
  376. MSM8X10_WCD_A_CDC_TOP_CLK_CTL,
  377. 0x20, 0x20);
  378. } else {
  379. snd_soc_update_bits(codec, WCD9XXX_A_BIAS_OSC_BG_CTL, 0x1, 0);
  380. snd_soc_update_bits(codec, WCD9XXX_A_RC_OSC_FREQ, 0x80, 0);
  381. if (resmgr->codec_type == WCD9XXX_CDC_TYPE_HELICON)
  382. snd_soc_update_bits(codec,
  383. MSM8X10_WCD_A_CDC_TOP_CLK_CTL,
  384. 0x20, 0x00);
  385. }
  386. return 0;
  387. }
  388. static void wcd9xxx_enable_clock_block(struct wcd9xxx_resmgr *resmgr,
  389. int config_mode)
  390. {
  391. struct snd_soc_codec *codec = resmgr->codec;
  392. pr_debug("%s: config_mode = %d\n", __func__, config_mode);
  393. /* transit to RCO requires mclk off */
  394. if (resmgr->codec_type != WCD9XXX_CDC_TYPE_HELICON)
  395. WARN_ON(snd_soc_read(codec, WCD9XXX_A_CLK_BUFF_EN2) & (1 << 2));
  396. if (config_mode) {
  397. /* Notify */
  398. wcd9xxx_resmgr_notifier_call(resmgr, WCD9XXX_EVENT_PRE_RCO_ON);
  399. /* enable RCO and switch to it */
  400. wcd9xxx_resmgr_enable_config_mode(resmgr, 1);
  401. if (resmgr->codec_type != WCD9XXX_CDC_TYPE_HELICON)
  402. snd_soc_write(codec, WCD9XXX_A_CLK_BUFF_EN2, 0x02);
  403. usleep_range(1000, 1000);
  404. } else {
  405. /* Notify */
  406. wcd9xxx_resmgr_notifier_call(resmgr, WCD9XXX_EVENT_PRE_MCLK_ON);
  407. /* switch to MCLK */
  408. if (resmgr->codec_type != WCD9XXX_CDC_TYPE_HELICON) {
  409. snd_soc_update_bits(codec, WCD9XXX_A_CLK_BUFF_EN1,
  410. 0x08, 0x00);
  411. } else {
  412. snd_soc_update_bits(codec,
  413. MSM8X10_WCD_A_CDC_CLK_PDM_CTL,
  414. 0x03, 0x03);
  415. snd_soc_update_bits(codec,
  416. MSM8X10_WCD_A_CDC_TOP_CLK_CTL,
  417. 0x0f, 0x0d);
  418. }
  419. /* if RCO is enabled, switch from it */
  420. if (snd_soc_read(codec, WCD9XXX_A_RC_OSC_FREQ) & 0x80) {
  421. if (resmgr->codec_type != WCD9XXX_CDC_TYPE_HELICON)
  422. snd_soc_write(codec, WCD9XXX_A_CLK_BUFF_EN2,
  423. 0x02);
  424. wcd9xxx_resmgr_enable_config_mode(resmgr, 0);
  425. }
  426. /* clk source to ext clk and clk buff ref to VBG */
  427. if (resmgr->codec_type != WCD9XXX_CDC_TYPE_HELICON)
  428. snd_soc_update_bits(codec, WCD9XXX_A_CLK_BUFF_EN1,
  429. 0x0C, 0x04);
  430. }
  431. if (resmgr->codec_type != WCD9XXX_CDC_TYPE_HELICON) {
  432. snd_soc_update_bits(codec, WCD9XXX_A_CLK_BUFF_EN1, 0x01, 0x01);
  433. /* sleep required by codec hardware to enable clock buffer */
  434. usleep_range(1000, 1200);
  435. snd_soc_update_bits(codec, WCD9XXX_A_CLK_BUFF_EN2, 0x02, 0x00);
  436. /* on MCLK */
  437. snd_soc_update_bits(codec, WCD9XXX_A_CLK_BUFF_EN2, 0x04, 0x04);
  438. snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLK_MCLK_CTL,
  439. 0x01, 0x01);
  440. } else {
  441. snd_soc_update_bits(codec, MSM8X10_WCD_A_CDC_CLK_MCLK_CTL,
  442. 0x01, 0x01);
  443. }
  444. usleep_range(50, 50);
  445. /* Notify */
  446. if (config_mode)
  447. wcd9xxx_resmgr_notifier_call(resmgr,
  448. WCD9XXX_EVENT_POST_RCO_ON);
  449. else
  450. wcd9xxx_resmgr_notifier_call(resmgr,
  451. WCD9XXX_EVENT_POST_MCLK_ON);
  452. }
  453. /*
  454. * disable clock and return previous clock state
  455. */
  456. static enum wcd9xxx_clock_type wcd9xxx_save_clock(struct wcd9xxx_resmgr *resmgr)
  457. {
  458. WCD9XXX_BG_CLK_ASSERT_LOCKED(resmgr);
  459. if (resmgr->clk_type != WCD9XXX_CLK_OFF)
  460. wcd9xxx_disable_clock_block(resmgr);
  461. return resmgr->clk_type != WCD9XXX_CLK_OFF;
  462. }
  463. static void wcd9xxx_restore_clock(struct wcd9xxx_resmgr *resmgr,
  464. enum wcd9xxx_clock_type type)
  465. {
  466. if (type != WCD9XXX_CLK_OFF)
  467. wcd9xxx_enable_clock_block(resmgr, type == WCD9XXX_CLK_RCO);
  468. }
  469. void wcd9xxx_resmgr_get_clk_block(struct wcd9xxx_resmgr *resmgr,
  470. enum wcd9xxx_clock_type type)
  471. {
  472. pr_debug("%s: current %d, requested %d, rco_users %d, mclk_users %d\n",
  473. __func__, resmgr->clk_type, type,
  474. resmgr->clk_rco_users, resmgr->clk_mclk_users);
  475. WCD9XXX_BG_CLK_ASSERT_LOCKED(resmgr);
  476. switch (type) {
  477. case WCD9XXX_CLK_RCO:
  478. if (++resmgr->clk_rco_users == 1 &&
  479. resmgr->clk_type == WCD9XXX_CLK_OFF) {
  480. /* enable RCO and switch to it */
  481. wcd9xxx_enable_clock_block(resmgr, 1);
  482. resmgr->clk_type = WCD9XXX_CLK_RCO;
  483. }
  484. break;
  485. case WCD9XXX_CLK_MCLK:
  486. if (++resmgr->clk_mclk_users == 1 &&
  487. resmgr->clk_type == WCD9XXX_CLK_OFF) {
  488. /* switch to MCLK */
  489. wcd9xxx_enable_clock_block(resmgr, 0);
  490. resmgr->clk_type = WCD9XXX_CLK_MCLK;
  491. } else if (resmgr->clk_mclk_users == 1 &&
  492. resmgr->clk_type == WCD9XXX_CLK_RCO) {
  493. /* if RCO is enabled, switch from it */
  494. WARN_ON(!(snd_soc_read(resmgr->codec,
  495. WCD9XXX_A_RC_OSC_FREQ) & 0x80));
  496. /* disable clock block */
  497. wcd9xxx_disable_clock_block(resmgr);
  498. /* switch to MCLK */
  499. wcd9xxx_enable_clock_block(resmgr, 0);
  500. resmgr->clk_type = WCD9XXX_CLK_MCLK;
  501. }
  502. break;
  503. default:
  504. pr_err("%s: Error, Invalid clock get request %d\n", __func__,
  505. type);
  506. break;
  507. }
  508. pr_debug("%s: leave\n", __func__);
  509. }
  510. void wcd9xxx_resmgr_put_clk_block(struct wcd9xxx_resmgr *resmgr,
  511. enum wcd9xxx_clock_type type)
  512. {
  513. pr_debug("%s: current %d, put %d\n", __func__, resmgr->clk_type, type);
  514. WCD9XXX_BG_CLK_ASSERT_LOCKED(resmgr);
  515. switch (type) {
  516. case WCD9XXX_CLK_RCO:
  517. if (--resmgr->clk_rco_users == 0 &&
  518. resmgr->clk_type == WCD9XXX_CLK_RCO) {
  519. wcd9xxx_disable_clock_block(resmgr);
  520. /* if RCO is enabled, switch from it */
  521. if (snd_soc_read(resmgr->codec, WCD9XXX_A_RC_OSC_FREQ)
  522. & 0x80) {
  523. if (resmgr->codec_type !=
  524. WCD9XXX_CDC_TYPE_HELICON)
  525. snd_soc_write(resmgr->codec,
  526. WCD9XXX_A_CLK_BUFF_EN2, 0x02);
  527. wcd9xxx_resmgr_enable_config_mode(resmgr, 0);
  528. }
  529. resmgr->clk_type = WCD9XXX_CLK_OFF;
  530. }
  531. break;
  532. case WCD9XXX_CLK_MCLK:
  533. if (--resmgr->clk_mclk_users == 0 &&
  534. resmgr->clk_rco_users == 0) {
  535. wcd9xxx_disable_clock_block(resmgr);
  536. resmgr->clk_type = WCD9XXX_CLK_OFF;
  537. } else if (resmgr->clk_mclk_users == 0 &&
  538. resmgr->clk_rco_users) {
  539. /* disable clock */
  540. wcd9xxx_disable_clock_block(resmgr);
  541. /* switch to RCO */
  542. wcd9xxx_enable_clock_block(resmgr, 1);
  543. resmgr->clk_type = WCD9XXX_CLK_RCO;
  544. }
  545. break;
  546. default:
  547. pr_err("%s: Error, Invalid clock get request %d\n", __func__,
  548. type);
  549. break;
  550. }
  551. WARN_ON(resmgr->clk_rco_users < 0);
  552. WARN_ON(resmgr->clk_mclk_users < 0);
  553. pr_debug("%s: new rco_users %d, mclk_users %d\n", __func__,
  554. resmgr->clk_rco_users, resmgr->clk_mclk_users);
  555. }
  556. static void wcd9xxx_resmgr_update_cfilt_usage(struct wcd9xxx_resmgr *resmgr,
  557. enum wcd9xxx_cfilt_sel cfilt_sel,
  558. bool inc)
  559. {
  560. u16 micb_cfilt_reg;
  561. enum wcd9xxx_notify_event e_pre_on, e_post_off;
  562. struct snd_soc_codec *codec = resmgr->codec;
  563. switch (cfilt_sel) {
  564. case WCD9XXX_CFILT1_SEL:
  565. micb_cfilt_reg = WCD9XXX_A_MICB_CFILT_1_CTL;
  566. e_pre_on = WCD9XXX_EVENT_PRE_CFILT_1_ON;
  567. e_post_off = WCD9XXX_EVENT_POST_CFILT_1_OFF;
  568. break;
  569. case WCD9XXX_CFILT2_SEL:
  570. micb_cfilt_reg = WCD9XXX_A_MICB_CFILT_2_CTL;
  571. e_pre_on = WCD9XXX_EVENT_PRE_CFILT_2_ON;
  572. e_post_off = WCD9XXX_EVENT_POST_CFILT_2_OFF;
  573. break;
  574. case WCD9XXX_CFILT3_SEL:
  575. micb_cfilt_reg = WCD9XXX_A_MICB_CFILT_3_CTL;
  576. e_pre_on = WCD9XXX_EVENT_PRE_CFILT_3_ON;
  577. e_post_off = WCD9XXX_EVENT_POST_CFILT_3_OFF;
  578. break;
  579. default:
  580. WARN(1, "Invalid CFILT selection %d\n", cfilt_sel);
  581. return; /* should not happen */
  582. }
  583. if (inc) {
  584. if ((resmgr->cfilt_users[cfilt_sel]++) == 0) {
  585. /* Notify */
  586. wcd9xxx_resmgr_notifier_call(resmgr, e_pre_on);
  587. /* Enable CFILT */
  588. snd_soc_update_bits(codec, micb_cfilt_reg, 0x80, 0x80);
  589. }
  590. } else {
  591. /*
  592. * Check if count not zero, decrease
  593. * then check if zero, go ahead disable cfilter
  594. */
  595. WARN(resmgr->cfilt_users[cfilt_sel] == 0,
  596. "Invalid CFILT use count 0\n");
  597. if ((--resmgr->cfilt_users[cfilt_sel]) == 0) {
  598. /* Disable CFILT */
  599. snd_soc_update_bits(codec, micb_cfilt_reg, 0x80, 0);
  600. /* Notify MBHC so MBHC can switch CFILT to fast mode */
  601. wcd9xxx_resmgr_notifier_call(resmgr, e_post_off);
  602. }
  603. }
  604. }
  605. void wcd9xxx_resmgr_cfilt_get(struct wcd9xxx_resmgr *resmgr,
  606. enum wcd9xxx_cfilt_sel cfilt_sel)
  607. {
  608. return wcd9xxx_resmgr_update_cfilt_usage(resmgr, cfilt_sel, true);
  609. }
  610. void wcd9xxx_resmgr_cfilt_put(struct wcd9xxx_resmgr *resmgr,
  611. enum wcd9xxx_cfilt_sel cfilt_sel)
  612. {
  613. return wcd9xxx_resmgr_update_cfilt_usage(resmgr, cfilt_sel, false);
  614. }
  615. int wcd9xxx_resmgr_get_k_val(struct wcd9xxx_resmgr *resmgr,
  616. unsigned int cfilt_mv)
  617. {
  618. int rc = -EINVAL;
  619. unsigned int ldoh_v = resmgr->micbias_pdata->ldoh_v;
  620. unsigned min_mv, max_mv;
  621. switch (ldoh_v) {
  622. case WCD9XXX_LDOH_1P95_V:
  623. min_mv = 160;
  624. max_mv = 1800;
  625. break;
  626. case WCD9XXX_LDOH_2P35_V:
  627. min_mv = 200;
  628. max_mv = 2200;
  629. break;
  630. case WCD9XXX_LDOH_2P75_V:
  631. min_mv = 240;
  632. max_mv = 2600;
  633. break;
  634. case WCD9XXX_LDOH_3P0_V:
  635. min_mv = 260;
  636. max_mv = 2875;
  637. break;
  638. default:
  639. goto done;
  640. }
  641. if (cfilt_mv < min_mv || cfilt_mv > max_mv)
  642. goto done;
  643. for (rc = 4; rc <= 44; rc++) {
  644. min_mv = max_mv * (rc) / 44;
  645. if (min_mv >= cfilt_mv) {
  646. rc -= 4;
  647. break;
  648. }
  649. }
  650. done:
  651. return rc;
  652. }
  653. static void wcd9xxx_resmgr_cond_trigger_cond(struct wcd9xxx_resmgr *resmgr,
  654. enum wcd9xxx_resmgr_cond cond)
  655. {
  656. struct list_head *l;
  657. struct wcd9xxx_resmgr_cond_entry *e;
  658. bool set;
  659. pr_debug("%s: enter\n", __func__);
  660. /* update bit if cond isn't available or cond is set */
  661. set = !test_bit(cond, &resmgr->cond_avail_flags) ||
  662. !!test_bit(cond, &resmgr->cond_flags);
  663. list_for_each(l, &resmgr->update_bit_cond_h) {
  664. e = list_entry(l, struct wcd9xxx_resmgr_cond_entry, list);
  665. if (e->cond == cond)
  666. snd_soc_update_bits(resmgr->codec, e->reg,
  667. 1 << e->shift,
  668. (set ? !e->invert : e->invert)
  669. << e->shift);
  670. }
  671. pr_debug("%s: leave\n", __func__);
  672. }
  673. /*
  674. * wcd9xxx_regmgr_cond_register : notify resmgr conditions in the condbits are
  675. * avaliable and notified.
  676. * condbits : contains bitmask of enum wcd9xxx_resmgr_cond
  677. */
  678. void wcd9xxx_regmgr_cond_register(struct wcd9xxx_resmgr *resmgr,
  679. unsigned long condbits)
  680. {
  681. unsigned int cond;
  682. for_each_set_bit(cond, &condbits, BITS_PER_BYTE * sizeof(condbits)) {
  683. mutex_lock(&resmgr->update_bit_cond_lock);
  684. WARN(test_bit(cond, &resmgr->cond_avail_flags),
  685. "Condition 0x%0x is already registered\n", cond);
  686. set_bit(cond, &resmgr->cond_avail_flags);
  687. wcd9xxx_resmgr_cond_trigger_cond(resmgr, cond);
  688. mutex_unlock(&resmgr->update_bit_cond_lock);
  689. pr_debug("%s: Condition 0x%x is registered\n", __func__, cond);
  690. }
  691. }
  692. void wcd9xxx_regmgr_cond_deregister(struct wcd9xxx_resmgr *resmgr,
  693. unsigned long condbits)
  694. {
  695. unsigned int cond;
  696. for_each_set_bit(cond, &condbits, BITS_PER_BYTE * sizeof(condbits)) {
  697. mutex_lock(&resmgr->update_bit_cond_lock);
  698. WARN(!test_bit(cond, &resmgr->cond_avail_flags),
  699. "Condition 0x%0x isn't registered\n", cond);
  700. clear_bit(cond, &resmgr->cond_avail_flags);
  701. wcd9xxx_resmgr_cond_trigger_cond(resmgr, cond);
  702. mutex_unlock(&resmgr->update_bit_cond_lock);
  703. pr_debug("%s: Condition 0x%x is deregistered\n", __func__,
  704. cond);
  705. }
  706. }
  707. void wcd9xxx_resmgr_cond_update_cond(struct wcd9xxx_resmgr *resmgr,
  708. enum wcd9xxx_resmgr_cond cond, bool set)
  709. {
  710. mutex_lock(&resmgr->update_bit_cond_lock);
  711. if ((set && !test_and_set_bit(cond, &resmgr->cond_flags)) ||
  712. (!set && test_and_clear_bit(cond, &resmgr->cond_flags))) {
  713. pr_debug("%s: Resource %d condition changed to %s\n", __func__,
  714. cond, set ? "set" : "clear");
  715. wcd9xxx_resmgr_cond_trigger_cond(resmgr, cond);
  716. }
  717. mutex_unlock(&resmgr->update_bit_cond_lock);
  718. }
  719. int wcd9xxx_resmgr_add_cond_update_bits(struct wcd9xxx_resmgr *resmgr,
  720. enum wcd9xxx_resmgr_cond cond,
  721. unsigned short reg, int shift,
  722. bool invert)
  723. {
  724. struct wcd9xxx_resmgr_cond_entry *entry;
  725. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  726. if (!entry)
  727. return -ENOMEM;
  728. entry->cond = cond;
  729. entry->reg = reg;
  730. entry->shift = shift;
  731. entry->invert = invert;
  732. mutex_lock(&resmgr->update_bit_cond_lock);
  733. list_add_tail(&entry->list, &resmgr->update_bit_cond_h);
  734. wcd9xxx_resmgr_cond_trigger_cond(resmgr, cond);
  735. mutex_unlock(&resmgr->update_bit_cond_lock);
  736. return 0;
  737. }
  738. /*
  739. * wcd9xxx_resmgr_rm_cond_update_bits :
  740. * Clear bit and remove from the conditional bit update list
  741. */
  742. int wcd9xxx_resmgr_rm_cond_update_bits(struct wcd9xxx_resmgr *resmgr,
  743. enum wcd9xxx_resmgr_cond cond,
  744. unsigned short reg, int shift,
  745. bool invert)
  746. {
  747. struct list_head *l, *next;
  748. struct wcd9xxx_resmgr_cond_entry *e = NULL;
  749. pr_debug("%s: enter\n", __func__);
  750. mutex_lock(&resmgr->update_bit_cond_lock);
  751. list_for_each_safe(l, next, &resmgr->update_bit_cond_h) {
  752. e = list_entry(l, struct wcd9xxx_resmgr_cond_entry, list);
  753. if (e->reg == reg && e->shift == shift && e->invert == invert) {
  754. snd_soc_update_bits(resmgr->codec, e->reg,
  755. 1 << e->shift,
  756. e->invert << e->shift);
  757. list_del(&e->list);
  758. mutex_unlock(&resmgr->update_bit_cond_lock);
  759. kfree(e);
  760. return 0;
  761. }
  762. }
  763. mutex_unlock(&resmgr->update_bit_cond_lock);
  764. pr_err("%s: Cannot find update bit entry reg 0x%x, shift %d\n",
  765. __func__, e ? e->reg : 0, e ? e->shift : 0);
  766. return -EINVAL;
  767. }
  768. int wcd9xxx_resmgr_register_notifier(struct wcd9xxx_resmgr *resmgr,
  769. struct notifier_block *nblock)
  770. {
  771. return blocking_notifier_chain_register(&resmgr->notifier, nblock);
  772. }
  773. int wcd9xxx_resmgr_unregister_notifier(struct wcd9xxx_resmgr *resmgr,
  774. struct notifier_block *nblock)
  775. {
  776. return blocking_notifier_chain_unregister(&resmgr->notifier, nblock);
  777. }
  778. int wcd9xxx_resmgr_init(struct wcd9xxx_resmgr *resmgr,
  779. struct snd_soc_codec *codec,
  780. struct wcd9xxx_core_resource *core_res,
  781. struct wcd9xxx_pdata *pdata,
  782. struct wcd9xxx_micbias_setting *micbias_pdata,
  783. struct wcd9xxx_reg_address *reg_addr,
  784. enum wcd9xxx_cdc_type cdc_type)
  785. {
  786. WARN(ARRAY_SIZE(wcd9xxx_event_string) != WCD9XXX_EVENT_LAST + 1,
  787. "Event string table isn't up to date!, %d != %d\n",
  788. ARRAY_SIZE(wcd9xxx_event_string), WCD9XXX_EVENT_LAST + 1);
  789. resmgr->bandgap_type = WCD9XXX_BANDGAP_OFF;
  790. resmgr->codec = codec;
  791. resmgr->codec_type = cdc_type;
  792. /* This gives access of core handle to lock/unlock suspend */
  793. resmgr->core_res = core_res;
  794. resmgr->pdata = pdata;
  795. resmgr->micbias_pdata = micbias_pdata;
  796. resmgr->reg_addr = reg_addr;
  797. INIT_LIST_HEAD(&resmgr->update_bit_cond_h);
  798. BLOCKING_INIT_NOTIFIER_HEAD(&resmgr->notifier);
  799. mutex_init(&resmgr->codec_resource_lock);
  800. mutex_init(&resmgr->codec_bg_clk_lock);
  801. mutex_init(&resmgr->update_bit_cond_lock);
  802. return 0;
  803. }
  804. void wcd9xxx_resmgr_deinit(struct wcd9xxx_resmgr *resmgr)
  805. {
  806. mutex_destroy(&resmgr->update_bit_cond_lock);
  807. mutex_destroy(&resmgr->codec_bg_clk_lock);
  808. mutex_destroy(&resmgr->codec_resource_lock);
  809. }
  810. void wcd9xxx_resmgr_bcl_lock(struct wcd9xxx_resmgr *resmgr)
  811. {
  812. mutex_lock(&resmgr->codec_resource_lock);
  813. }
  814. void wcd9xxx_resmgr_bcl_unlock(struct wcd9xxx_resmgr *resmgr)
  815. {
  816. mutex_unlock(&resmgr->codec_resource_lock);
  817. }
  818. MODULE_DESCRIPTION("wcd9xxx resmgr module");
  819. MODULE_LICENSE("GPL v2");