blob: b4c1be40ff313da18fe38a3c15975fd00d60d084 [file] [log] [blame]
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +05301/* Copyright (c) 2011-2017, 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
13#ifndef __MFD_TABLA_CORE_H__
14#define __MFD_TABLA_CORE_H__
15
16#include <linux/types.h>
17#include <linux/platform_device.h>
18#include <linux/of_irq.h>
19#include <linux/interrupt.h>
20#include <linux/pm_qos.h>
21
22#define WCD9XXX_MAX_IRQ_REGS 4
23#define WCD9XXX_MAX_NUM_IRQS (WCD9XXX_MAX_IRQ_REGS * 8)
24#define WCD9XXX_SLIM_NUM_PORT_REG 3
25#define TABLA_VERSION_1_0 0
26#define TABLA_VERSION_1_1 1
27#define TABLA_VERSION_2_0 2
28#define TABLA_IS_1_X(ver) \
29 (((ver == TABLA_VERSION_1_0) || (ver == TABLA_VERSION_1_1)) ? 1 : 0)
30#define TABLA_IS_2_0(ver) ((ver == TABLA_VERSION_2_0) ? 1 : 0)
31
32#define WCD9XXX_SUPPLY_BUCK_NAME "cdc-vdd-buck"
33
34#define SITAR_VERSION_1P0 0
35#define SITAR_VERSION_1P1 1
36#define SITAR_IS_1P0(ver) \
37 ((ver == SITAR_VERSION_1P0) ? 1 : 0)
38#define SITAR_IS_1P1(ver) \
39 ((ver == SITAR_VERSION_1P1) ? 1 : 0)
40
41#define TAIKO_VERSION_1_0 1
42#define TAIKO_IS_1_0(ver) \
43 ((ver == TAIKO_VERSION_1_0) ? 1 : 0)
44
45#define TAPAN_VERSION_1_0 0
46#define TAPAN_IS_1_0(ver) \
47 ((ver == TAPAN_VERSION_1_0) ? 1 : 0)
48
49#define TOMTOM_VERSION_1_0 1
50#define TOMTOM_IS_1_0(ver) \
51 ((ver == TOMTOM_VERSION_1_0) ? 1 : 0)
52
53#define TASHA_VERSION_1_0 0
54#define TASHA_VERSION_1_1 1
55#define TASHA_VERSION_2_0 2
56
57#define TASHA_IS_1_0(wcd) \
58 ((wcd->type == WCD9335 || wcd->type == WCD9326) ? \
59 ((wcd->version == TASHA_VERSION_1_0) ? 1 : 0) : 0)
60
61#define TASHA_IS_1_1(wcd) \
62 ((wcd->type == WCD9335 || wcd->type == WCD9326) ? \
63 ((wcd->version == TASHA_VERSION_1_1) ? 1 : 0) : 0)
64
65#define TASHA_IS_2_0(wcd) \
66 ((wcd->type == WCD9335 || wcd->type == WCD9326) ? \
67 ((wcd->version == TASHA_VERSION_2_0) ? 1 : 0) : 0)
68
69/*
70 * As fine version info cannot be retrieved before tavil probe.
71 * Define three coarse versions for possible future use before tavil probe.
72 */
73#define TAVIL_VERSION_1_0 0
74#define TAVIL_VERSION_1_1 1
75#define TAVIL_VERSION_WCD9340_1_0 2
76#define TAVIL_VERSION_WCD9341_1_0 3
77#define TAVIL_VERSION_WCD9340_1_1 4
78#define TAVIL_VERSION_WCD9341_1_1 5
79
80#define TAVIL_IS_1_0(wcd) \
81 ((wcd->type == WCD934X) ? \
82 ((wcd->version == TAVIL_VERSION_1_0 || \
83 wcd->version == TAVIL_VERSION_WCD9340_1_0 || \
84 wcd->version == TAVIL_VERSION_WCD9341_1_0) ? 1 : 0) : 0)
85#define TAVIL_IS_1_1(wcd) \
86 ((wcd->type == WCD934X) ? \
87 ((wcd->version == TAVIL_VERSION_1_1 || \
88 wcd->version == TAVIL_VERSION_WCD9340_1_1 || \
89 wcd->version == TAVIL_VERSION_WCD9341_1_1) ? 1 : 0) : 0)
90#define TAVIL_IS_WCD9340_1_0(wcd) \
91 ((wcd->type == WCD934X) ? \
92 ((wcd->version == TAVIL_VERSION_WCD9340_1_0) ? 1 : 0) : 0)
93#define TAVIL_IS_WCD9341_1_0(wcd) \
94 ((wcd->type == WCD934X) ? \
95 ((wcd->version == TAVIL_VERSION_WCD9341_1_0) ? 1 : 0) : 0)
96#define TAVIL_IS_WCD9340_1_1(wcd) \
97 ((wcd->type == WCD934X) ? \
98 ((wcd->version == TAVIL_VERSION_WCD9340_1_1) ? 1 : 0) : 0)
99#define TAVIL_IS_WCD9341_1_1(wcd) \
100 ((wcd->type == WCD934X) ? \
101 ((wcd->version == TAVIL_VERSION_WCD9341_1_1) ? 1 : 0) : 0)
102
103#define IS_CODEC_TYPE(wcd, wcdtype) \
104 ((wcd->type == wcdtype) ? true : false)
105#define IS_CODEC_VERSION(wcd, wcdversion) \
106 ((wcd->version == wcdversion) ? true : false)
107
108enum {
109 CDC_V_1_0,
110 CDC_V_1_1,
111 CDC_V_2_0,
112};
113
114enum codec_variant {
115 WCD9XXX,
116 WCD9330,
117 WCD9335,
118 WCD9326,
119 WCD934X,
120};
121
122enum wcd9xxx_slim_slave_addr_type {
123 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_0,
124 WCD9XXX_SLIM_SLAVE_ADDR_TYPE_1,
125};
126
127enum wcd9xxx_pm_state {
128 WCD9XXX_PM_SLEEPABLE,
129 WCD9XXX_PM_AWAKE,
130 WCD9XXX_PM_ASLEEP,
131};
132
133enum {
134 WCD9XXX_INTR_STATUS_BASE = 0,
135 WCD9XXX_INTR_CLEAR_BASE,
136 WCD9XXX_INTR_MASK_BASE,
137 WCD9XXX_INTR_LEVEL_BASE,
138 WCD9XXX_INTR_CLR_COMMIT,
139 WCD9XXX_INTR_REG_MAX,
140};
141
142enum wcd9xxx_intf_status {
143 WCD9XXX_INTERFACE_TYPE_PROBING,
144 WCD9XXX_INTERFACE_TYPE_SLIMBUS,
145 WCD9XXX_INTERFACE_TYPE_I2C,
146};
147
148enum {
149 /* INTR_REG 0 */
150 WCD9XXX_IRQ_SLIMBUS = 0,
151 WCD9XXX_IRQ_MBHC_REMOVAL,
152 WCD9XXX_IRQ_MBHC_SHORT_TERM,
153 WCD9XXX_IRQ_MBHC_PRESS,
154 WCD9XXX_IRQ_MBHC_RELEASE,
155 WCD9XXX_IRQ_MBHC_POTENTIAL,
156 WCD9XXX_IRQ_MBHC_INSERTION,
157 WCD9XXX_IRQ_BG_PRECHARGE,
158 /* INTR_REG 1 */
159 WCD9XXX_IRQ_PA1_STARTUP,
160 WCD9XXX_IRQ_PA2_STARTUP,
161 WCD9XXX_IRQ_PA3_STARTUP,
162 WCD9XXX_IRQ_PA4_STARTUP,
163 WCD9306_IRQ_HPH_PA_OCPR_FAULT = WCD9XXX_IRQ_PA4_STARTUP,
164 WCD9XXX_IRQ_PA5_STARTUP,
165 WCD9XXX_IRQ_MICBIAS1_PRECHARGE,
166 WCD9306_IRQ_HPH_PA_OCPL_FAULT = WCD9XXX_IRQ_MICBIAS1_PRECHARGE,
167 WCD9XXX_IRQ_MICBIAS2_PRECHARGE,
168 WCD9XXX_IRQ_MICBIAS3_PRECHARGE,
169 /* INTR_REG 2 */
170 WCD9XXX_IRQ_HPH_PA_OCPL_FAULT,
171 WCD9XXX_IRQ_HPH_PA_OCPR_FAULT,
172 WCD9XXX_IRQ_EAR_PA_OCPL_FAULT,
173 WCD9XXX_IRQ_HPH_L_PA_STARTUP,
174 WCD9XXX_IRQ_HPH_R_PA_STARTUP,
175 WCD9320_IRQ_EAR_PA_STARTUP,
176 WCD9306_IRQ_MBHC_JACK_SWITCH = WCD9320_IRQ_EAR_PA_STARTUP,
177 WCD9310_NUM_IRQS,
178 WCD9XXX_IRQ_RESERVED_0 = WCD9310_NUM_IRQS,
179 WCD9XXX_IRQ_RESERVED_1,
180 WCD9330_IRQ_SVASS_ERR_EXCEPTION = WCD9310_NUM_IRQS,
181 WCD9330_IRQ_MBHC_JACK_SWITCH,
182 /* INTR_REG 3 */
183 WCD9XXX_IRQ_MAD_AUDIO,
184 WCD9XXX_IRQ_MAD_ULTRASOUND,
185 WCD9XXX_IRQ_MAD_BEACON,
186 WCD9XXX_IRQ_SPEAKER_CLIPPING,
187 WCD9320_IRQ_MBHC_JACK_SWITCH,
188 WCD9306_NUM_IRQS,
189 WCD9XXX_IRQ_VBAT_MONITOR_ATTACK = WCD9306_NUM_IRQS,
190 WCD9XXX_IRQ_VBAT_MONITOR_RELEASE,
191 WCD9XXX_NUM_IRQS,
192 /* WCD9330 INTR1_REG 3*/
193 WCD9330_IRQ_SVASS_ENGINE = WCD9XXX_IRQ_MAD_AUDIO,
194 WCD9330_IRQ_MAD_AUDIO,
195 WCD9330_IRQ_MAD_ULTRASOUND,
196 WCD9330_IRQ_MAD_BEACON,
197 WCD9330_IRQ_SPEAKER1_CLIPPING,
198 WCD9330_IRQ_SPEAKER2_CLIPPING,
199 WCD9330_IRQ_VBAT_MONITOR_ATTACK,
200 WCD9330_IRQ_VBAT_MONITOR_RELEASE,
201 WCD9330_NUM_IRQS,
202 WCD9XXX_IRQ_RESERVED_2 = WCD9330_NUM_IRQS,
203};
204
205enum {
206 TABLA_NUM_IRQS = WCD9310_NUM_IRQS,
207 SITAR_NUM_IRQS = WCD9310_NUM_IRQS,
208 TAIKO_NUM_IRQS = WCD9XXX_NUM_IRQS,
209 TAPAN_NUM_IRQS = WCD9306_NUM_IRQS,
210 TOMTOM_NUM_IRQS = WCD9330_NUM_IRQS,
211};
212
213struct intr_data {
214 int intr_num;
215 bool clear_first;
216};
217
218struct wcd9xxx_core_resource {
219 struct mutex irq_lock;
220 struct mutex nested_irq_lock;
221
222 enum wcd9xxx_pm_state pm_state;
223 struct mutex pm_lock;
224 /* pm_wq notifies change of pm_state */
225 wait_queue_head_t pm_wq;
226 struct pm_qos_request pm_qos_req;
227 int wlock_holders;
228
229
230 /* holds the table of interrupts per codec */
231 const struct intr_data *intr_table;
232 int intr_table_size;
233 unsigned int irq_base;
234 unsigned int irq;
235 u8 irq_masks_cur[WCD9XXX_MAX_IRQ_REGS];
236 u8 irq_masks_cache[WCD9XXX_MAX_IRQ_REGS];
237 bool irq_level_high[WCD9XXX_MAX_NUM_IRQS];
238 int num_irqs;
239 int num_irq_regs;
240 u16 intr_reg[WCD9XXX_INTR_REG_MAX];
241 struct regmap *wcd_core_regmap;
242
243 /* Pointer to parent container data structure */
244 void *parent;
245
246 struct device *dev;
247 struct irq_domain *domain;
248};
249
250/*
251 * data structure for Slimbus and I2S channel.
252 * Some of fields are only used in smilbus mode
253 */
254struct wcd9xxx_ch {
255 u32 sph; /* share channel handle - slimbus only */
256 u32 ch_num; /*
257 * vitrual channel number, such as 128 -144.
258 * apply for slimbus only
259 */
260 u16 ch_h; /* chanel handle - slimbus only */
261 u16 port; /*
262 * tabla port for RX and TX
263 * such as 0-9 for TX and 10 -16 for RX
264 * apply for both i2s and slimbus
265 */
266 u16 shift; /*
267 * shift bit for RX and TX
268 * apply for both i2s and slimbus
269 */
270 struct list_head list; /*
271 * channel link list
272 * apply for both i2s and slimbus
273 */
274};
275
276struct wcd9xxx_codec_dai_data {
277 u32 rate; /* sample rate */
278 u32 bit_width; /* sit width 16,24,32 */
279 struct list_head wcd9xxx_ch_list; /* channel list */
280 u16 grph; /* slimbus group handle */
281 unsigned long ch_mask;
282 wait_queue_head_t dai_wait;
283 bool bus_down_in_recovery;
284};
285
286#define WCD9XXX_CH(xport, xshift) \
287 {.port = xport, .shift = xshift}
288
289enum wcd9xxx_chipid_major {
290 TABLA_MAJOR = cpu_to_le16(0x100),
291 SITAR_MAJOR = cpu_to_le16(0x101),
292 TAIKO_MAJOR = cpu_to_le16(0x102),
293 TAPAN_MAJOR = cpu_to_le16(0x103),
294 TOMTOM_MAJOR = cpu_to_le16(0x105),
295 TASHA_MAJOR = cpu_to_le16(0x0),
296 TASHA2P0_MAJOR = cpu_to_le16(0x107),
297 TAVIL_MAJOR = cpu_to_le16(0x108),
298};
299
300enum codec_power_states {
301 WCD_REGION_POWER_COLLAPSE_REMOVE,
302 WCD_REGION_POWER_COLLAPSE_BEGIN,
303 WCD_REGION_POWER_DOWN,
304};
305
306enum wcd_power_regions {
307 WCD9XXX_DIG_CORE_REGION_1,
308 WCD9XXX_MAX_PWR_REGIONS,
309};
310
311struct wcd9xxx_codec_type {
312 u16 id_major;
313 u16 id_minor;
314 struct mfd_cell *dev;
315 int size;
316 int num_irqs;
317 int version; /* -1 to retrieve version from chip version register */
318 enum wcd9xxx_slim_slave_addr_type slim_slave_type;
319 u16 i2c_chip_status;
320 const struct intr_data *intr_tbl;
321 int intr_tbl_size;
322 u16 intr_reg[WCD9XXX_INTR_REG_MAX];
323};
324
325struct wcd9xxx_power_region {
326 enum codec_power_states power_state;
327 u16 pwr_collapse_reg_min;
328 u16 pwr_collapse_reg_max;
329};
330
331struct wcd9xxx {
332 struct device *dev;
333 struct slim_device *slim;
334 struct slim_device *slim_slave;
335 struct mutex io_lock;
336 struct mutex xfer_lock;
337 struct mutex reset_lock;
338 u8 version;
339
340 int reset_gpio;
341 struct device_node *wcd_rst_np;
342
343 int (*read_dev)(struct wcd9xxx *wcd9xxx, unsigned short reg,
344 int bytes, void *dest, bool interface_reg);
345 int (*write_dev)(struct wcd9xxx *wcd9xxx, unsigned short reg,
346 int bytes, void *src, bool interface_reg);
347 int (*multi_reg_write)(struct wcd9xxx *wcd9xxx, const void *data,
348 size_t count);
349 int (*dev_down)(struct wcd9xxx *wcd9xxx);
350 int (*post_reset)(struct wcd9xxx *wcd9xxx);
351
352 void *ssr_priv;
Banajit Goswamif6bc7132017-10-20 22:29:42 -0700353 bool dev_up;
Asish Bhattacharya8e2277f2017-07-20 18:31:55 +0530354
355 u32 num_of_supplies;
356 struct regulator_bulk_data *supplies;
357
358 struct wcd9xxx_core_resource core_res;
359
360 u16 id_minor;
361 u16 id_major;
362
363 /* Slimbus or I2S port */
364 u32 num_rx_port;
365 u32 num_tx_port;
366 struct wcd9xxx_ch *rx_chs;
367 struct wcd9xxx_ch *tx_chs;
368 u32 mclk_rate;
369 enum codec_variant type;
370 struct regmap *regmap;
371
372 struct wcd9xxx_codec_type *codec_type;
373 bool prev_pg_valid;
374 u8 prev_pg;
375 u8 avoid_cdc_rstlow;
376 struct wcd9xxx_power_region *wcd9xxx_pwr[WCD9XXX_MAX_PWR_REGIONS];
377};
378
379struct wcd9xxx_reg_val {
380 unsigned short reg; /* register address */
381 u8 *buf; /* buffer to be written to reg. addr */
382 int bytes; /* number of bytes to be written */
383};
384
385int wcd9xxx_interface_reg_read(struct wcd9xxx *wcd9xxx, unsigned short reg);
386int wcd9xxx_interface_reg_write(struct wcd9xxx *wcd9xxx, unsigned short reg,
387 u8 val);
388int wcd9xxx_get_logical_addresses(u8 *pgd_la, u8 *inf_la);
389int wcd9xxx_slim_write_repeat(struct wcd9xxx *wcd9xxx, unsigned short reg,
390 int bytes, void *src);
391int wcd9xxx_slim_reserve_bw(struct wcd9xxx *wcd9xxx,
392 u32 bw_ops, bool commit);
393int wcd9xxx_set_power_state(struct wcd9xxx *wcd9xxx, enum codec_power_states,
394 enum wcd_power_regions);
395int wcd9xxx_get_current_power_state(struct wcd9xxx *wcd9xxx,
396 enum wcd_power_regions);
397
398int wcd9xxx_page_write(struct wcd9xxx *wcd9xxx, unsigned short *reg);
399
400int wcd9xxx_slim_bulk_write(struct wcd9xxx *wcd9xxx,
401 struct wcd9xxx_reg_val *bulk_reg,
402 unsigned int size, bool interface);
403
404extern int wcd9xxx_core_res_init(
405 struct wcd9xxx_core_resource *wcd9xxx_core_res,
406 int num_irqs, int num_irq_regs, struct regmap *wcd_regmap);
407
408extern void wcd9xxx_core_res_deinit(
409 struct wcd9xxx_core_resource *wcd9xxx_core_res);
410
411extern int wcd9xxx_core_res_suspend(
412 struct wcd9xxx_core_resource *wcd9xxx_core_res,
413 pm_message_t pmesg);
414
415extern int wcd9xxx_core_res_resume(
416 struct wcd9xxx_core_resource *wcd9xxx_core_res);
417
418extern int wcd9xxx_core_irq_init(
419 struct wcd9xxx_core_resource *wcd9xxx_core_res);
420
421extern int wcd9xxx_assign_irq(struct wcd9xxx_core_resource *wcd9xxx_core_res,
422 unsigned int irq,
423 unsigned int irq_base);
424
425extern enum wcd9xxx_intf_status wcd9xxx_get_intf_type(void);
426extern void wcd9xxx_set_intf_type(enum wcd9xxx_intf_status);
427
428extern enum wcd9xxx_pm_state wcd9xxx_pm_cmpxchg(
429 struct wcd9xxx_core_resource *wcd9xxx_core_res,
430 enum wcd9xxx_pm_state o,
431 enum wcd9xxx_pm_state n);
432static inline int __init wcd9xxx_irq_of_init(struct device_node *node,
433 struct device_node *parent)
434{
435 return 0;
436}
437
438int wcd9xxx_init(void);
439void wcd9xxx_exit(void);
440#endif