core.h 12 KB

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