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