blob: e6a8f5d2af79d35269aaa176bd4f6c233a2f8be1 [file] [log] [blame]
Ravishankar Sarawadi839fcf32012-11-14 12:13:00 -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#ifndef __WCD9XXX_COMMON_H__
13#define __WCD9XXX_COMMON_H__
14
15#include <linux/notifier.h>
16#include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
17
18enum wcd9xxx_bandgap_type {
19 WCD9XXX_BANDGAP_OFF,
20 WCD9XXX_BANDGAP_AUDIO_MODE,
21 WCD9XXX_BANDGAP_MBHC_MODE,
22};
23
24enum wcd9xxx_clock_type {
25 WCD9XXX_CLK_OFF,
26 WCD9XXX_CLK_RCO,
27 WCD9XXX_CLK_MCLK,
28};
29
30enum wcd9xxx_cfilt_sel {
31 WCD9XXX_CFILT1_SEL,
32 WCD9XXX_CFILT2_SEL,
33 WCD9XXX_CFILT3_SEL,
34 WCD9XXX_NUM_OF_CFILT,
35};
36
37struct wcd9xxx_reg_address {
38 u16 micb_4_ctl;
39 u16 micb_4_int_rbias;
40 u16 micb_4_mbhc;
41};
42
43enum wcd9xxx_notify_event {
44 WCD9XXX_EVENT_INVALID,
45
46 WCD9XXX_EVENT_PRE_RCO_ON,
47 WCD9XXX_EVENT_POST_RCO_ON,
48 WCD9XXX_EVENT_PRE_RCO_OFF,
49 WCD9XXX_EVENT_POST_RCO_OFF,
50
51 WCD9XXX_EVENT_PRE_MCLK_ON,
52 WCD9XXX_EVENT_POST_MCLK_ON,
53 WCD9XXX_EVENT_PRE_MCLK_OFF,
54 WCD9XXX_EVENT_POST_MCLK_OFF,
55
56 WCD9XXX_EVENT_PRE_BG_OFF,
57 WCD9XXX_EVENT_POST_BG_OFF,
58 WCD9XXX_EVENT_PRE_BG_AUDIO_ON,
59 WCD9XXX_EVENT_POST_BG_AUDIO_ON,
60 WCD9XXX_EVENT_PRE_BG_MBHC_ON,
61 WCD9XXX_EVENT_POST_BG_MBHC_ON,
62
63 WCD9XXX_EVENT_PRE_MICBIAS_1_OFF,
64 WCD9XXX_EVENT_POST_MICBIAS_1_OFF,
65 WCD9XXX_EVENT_PRE_MICBIAS_2_OFF,
66 WCD9XXX_EVENT_POST_MICBIAS_2_OFF,
67 WCD9XXX_EVENT_PRE_MICBIAS_3_OFF,
68 WCD9XXX_EVENT_POST_MICBIAS_3_OFF,
69 WCD9XXX_EVENT_PRE_MICBIAS_4_OFF,
70 WCD9XXX_EVENT_POST_MICBIAS_4_OFF,
71 WCD9XXX_EVENT_PRE_MICBIAS_1_ON,
72 WCD9XXX_EVENT_POST_MICBIAS_1_ON,
73 WCD9XXX_EVENT_PRE_MICBIAS_2_ON,
74 WCD9XXX_EVENT_POST_MICBIAS_2_ON,
75 WCD9XXX_EVENT_PRE_MICBIAS_3_ON,
76 WCD9XXX_EVENT_POST_MICBIAS_3_ON,
77 WCD9XXX_EVENT_PRE_MICBIAS_4_ON,
78 WCD9XXX_EVENT_POST_MICBIAS_4_ON,
79
80 WCD9XXX_EVENT_PRE_CFILT_1_OFF,
81 WCD9XXX_EVENT_POST_CFILT_1_OFF,
82 WCD9XXX_EVENT_PRE_CFILT_2_OFF,
83 WCD9XXX_EVENT_POST_CFILT_2_OFF,
84 WCD9XXX_EVENT_PRE_CFILT_3_OFF,
85 WCD9XXX_EVENT_POST_CFILT_3_OFF,
86 WCD9XXX_EVENT_PRE_CFILT_1_ON,
87 WCD9XXX_EVENT_POST_CFILT_1_ON,
88 WCD9XXX_EVENT_PRE_CFILT_2_ON,
89 WCD9XXX_EVENT_POST_CFILT_2_ON,
90 WCD9XXX_EVENT_PRE_CFILT_3_ON,
91 WCD9XXX_EVENT_POST_CFILT_3_ON,
92
93 WCD9XXX_EVENT_PRE_HPHL_PA_ON,
94 WCD9XXX_EVENT_POST_HPHL_PA_OFF,
95 WCD9XXX_EVENT_PRE_HPHR_PA_ON,
96 WCD9XXX_EVENT_POST_HPHR_PA_OFF,
97
98 WCD9XXX_EVENT_POST_RESUME,
99
100 WCD9XXX_EVENT_LAST,
101};
102
103struct wcd9xxx_resmgr {
104 struct snd_soc_codec *codec;
105 struct wcd9xxx *core;
106
107 u32 rx_bias_count;
108
Joonwoo Park533b3682013-06-13 11:41:21 -0700109 /*
110 * bandgap_type, bg_audio_users and bg_mbhc_users have to be
111 * referred/manipulated after acquiring codec_bg_clk_lock mutex
112 */
Joonwoo Parka8890262012-10-15 12:04:27 -0700113 enum wcd9xxx_bandgap_type bandgap_type;
114 u16 bg_audio_users;
115 u16 bg_mbhc_users;
116
Joonwoo Park533b3682013-06-13 11:41:21 -0700117 /*
118 * clk_type, clk_rco_users and clk_mclk_users have to be
119 * referred/manipulated after acquiring codec_bg_clk_lock mutex
120 */
Joonwoo Parka8890262012-10-15 12:04:27 -0700121 enum wcd9xxx_clock_type clk_type;
122 u16 clk_rco_users;
123 u16 clk_mclk_users;
124
125 /* cfilt users per cfilts */
126 u16 cfilt_users[WCD9XXX_NUM_OF_CFILT];
127
128 struct wcd9xxx_reg_address *reg_addr;
129
130 struct wcd9xxx_pdata *pdata;
131
132 struct blocking_notifier_head notifier;
133 /* Notifier needs mbhc pointer with resmgr */
134 struct wcd9xxx_mbhc *mbhc;
135
Joonwoo Parkcc52d472013-01-28 10:10:12 -0800136 unsigned long cond_flags;
Joonwoo Park3b268ca2013-07-17 13:11:43 -0700137 unsigned long cond_avail_flags;
Joonwoo Parkcc52d472013-01-28 10:10:12 -0800138 struct list_head update_bit_cond_h;
Joonwoo Parkccccba72013-04-26 11:19:46 -0700139 struct mutex update_bit_cond_lock;
Joonwoo Parkcc52d472013-01-28 10:10:12 -0800140
Joonwoo Parka8890262012-10-15 12:04:27 -0700141 /*
142 * Currently, only used for mbhc purpose, to protect
143 * concurrent execution of mbhc threaded irq handlers and
144 * kill race between DAPM and MBHC. But can serve as a
145 * general lock to protect codec resource
146 */
147 struct mutex codec_resource_lock;
Joonwoo Park533b3682013-06-13 11:41:21 -0700148 struct mutex codec_bg_clk_lock;
Joonwoo Parka8890262012-10-15 12:04:27 -0700149};
150
151int wcd9xxx_resmgr_init(struct wcd9xxx_resmgr *resmgr,
152 struct snd_soc_codec *codec,
153 struct wcd9xxx *wcd9xxx,
154 struct wcd9xxx_pdata *pdata,
155 struct wcd9xxx_reg_address *reg_addr);
156void wcd9xxx_resmgr_deinit(struct wcd9xxx_resmgr *resmgr);
157
158int wcd9xxx_resmgr_enable_config_mode(struct snd_soc_codec *codec, int enable);
159
160void wcd9xxx_resmgr_enable_rx_bias(struct wcd9xxx_resmgr *resmgr, u32 enable);
161void wcd9xxx_resmgr_get_clk_block(struct wcd9xxx_resmgr *resmgr,
162 enum wcd9xxx_clock_type type);
163void wcd9xxx_resmgr_put_clk_block(struct wcd9xxx_resmgr *resmgr,
164 enum wcd9xxx_clock_type type);
165void wcd9xxx_resmgr_get_bandgap(struct wcd9xxx_resmgr *resmgr,
166 const enum wcd9xxx_bandgap_type choice);
167void wcd9xxx_resmgr_put_bandgap(struct wcd9xxx_resmgr *resmgr,
168 enum wcd9xxx_bandgap_type choice);
169void wcd9xxx_resmgr_cfilt_get(struct wcd9xxx_resmgr *resmgr,
170 enum wcd9xxx_cfilt_sel cfilt_sel);
171void wcd9xxx_resmgr_cfilt_put(struct wcd9xxx_resmgr *resmgr,
172 enum wcd9xxx_cfilt_sel cfilt_sel);
173
174void wcd9xxx_resmgr_bcl_lock(struct wcd9xxx_resmgr *resmgr);
Ravishankar Sarawadi839fcf32012-11-14 12:13:00 -0800175void wcd9xxx_resmgr_post_ssr(struct wcd9xxx_resmgr *resmgr);
Joonwoo Parka8890262012-10-15 12:04:27 -0700176#define WCD9XXX_BCL_LOCK(resmgr) \
177{ \
178 pr_debug("%s: Acquiring BCL\n", __func__); \
179 wcd9xxx_resmgr_bcl_lock(resmgr); \
180 pr_debug("%s: Acquiring BCL done\n", __func__); \
181}
182
183void wcd9xxx_resmgr_bcl_unlock(struct wcd9xxx_resmgr *resmgr);
184#define WCD9XXX_BCL_UNLOCK(resmgr) \
185{ \
186 pr_debug("%s: Release BCL\n", __func__); \
187 wcd9xxx_resmgr_bcl_unlock(resmgr); \
188}
189
190#define WCD9XXX_BCL_ASSERT_LOCKED(resmgr) \
191{ \
192 WARN_ONCE(!mutex_is_locked(&resmgr->codec_resource_lock), \
193 "%s: BCL should have acquired\n", __func__); \
194}
195
Joonwoo Park533b3682013-06-13 11:41:21 -0700196#define WCD9XXX_BG_CLK_LOCK(resmgr) \
197{ \
198 struct wcd9xxx_resmgr *__resmgr = resmgr; \
199 pr_debug("%s: Acquiring BG_CLK\n", __func__); \
200 mutex_lock(&__resmgr->codec_bg_clk_lock); \
201 pr_debug("%s: Acquiring BG_CLK done\n", __func__); \
202}
203
204#define WCD9XXX_BG_CLK_UNLOCK(resmgr) \
205{ \
206 struct wcd9xxx_resmgr *__resmgr = resmgr; \
207 pr_debug("%s: Releasing BG_CLK\n", __func__); \
208 mutex_unlock(&__resmgr->codec_bg_clk_lock); \
209}
210
211#define WCD9XXX_BG_CLK_ASSERT_LOCKED(resmgr) \
212{ \
213 WARN_ONCE(!mutex_is_locked(&resmgr->codec_bg_clk_lock), \
214 "%s: BG_CLK lock should have acquired\n", __func__); \
215}
216
Joonwoo Parka8890262012-10-15 12:04:27 -0700217const char *wcd9xxx_get_event_string(enum wcd9xxx_notify_event type);
218int wcd9xxx_resmgr_get_k_val(struct wcd9xxx_resmgr *resmgr,
219 unsigned int cfilt_mv);
220int wcd9xxx_resmgr_register_notifier(struct wcd9xxx_resmgr *resmgr,
221 struct notifier_block *nblock);
222int wcd9xxx_resmgr_unregister_notifier(struct wcd9xxx_resmgr *resmgr,
223 struct notifier_block *nblock);
224void wcd9xxx_resmgr_notifier_call(struct wcd9xxx_resmgr *resmgr,
225 const enum wcd9xxx_notify_event e);
226
Joonwoo Parkcc52d472013-01-28 10:10:12 -0800227enum wcd9xxx_resmgr_cond {
Joonwoo Parkaf21f032013-03-05 18:07:40 -0800228 WCD9XXX_COND_HPH = 0x01, /* Headphone */
229 WCD9XXX_COND_HPH_MIC = 0x02, /* Microphone on the headset */
Joonwoo Parkcc52d472013-01-28 10:10:12 -0800230};
Joonwoo Park3b268ca2013-07-17 13:11:43 -0700231void wcd9xxx_regmgr_cond_register(struct wcd9xxx_resmgr *resmgr,
232 unsigned long condbits);
233void wcd9xxx_regmgr_cond_deregister(struct wcd9xxx_resmgr *resmgr,
234 unsigned long condbits);
Joonwoo Parkcc52d472013-01-28 10:10:12 -0800235int wcd9xxx_resmgr_rm_cond_update_bits(struct wcd9xxx_resmgr *resmgr,
236 enum wcd9xxx_resmgr_cond cond,
237 unsigned short reg, int shift,
238 bool invert);
239int wcd9xxx_resmgr_add_cond_update_bits(struct wcd9xxx_resmgr *resmgr,
240 enum wcd9xxx_resmgr_cond cond,
241 unsigned short reg, int shift,
242 bool invert);
243void wcd9xxx_resmgr_cond_update_cond(struct wcd9xxx_resmgr *resmgr,
244 enum wcd9xxx_resmgr_cond cond, bool set);
245
Joonwoo Parka8890262012-10-15 12:04:27 -0700246#endif /* __WCD9XXX_COMMON_H__ */