wcd9xxx-common.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /* Copyright (c) 2013-2014, 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/slab.h>
  14. #include <sound/soc.h>
  15. #include <linux/kernel.h>
  16. #include <linux/delay.h>
  17. #include <linux/mfd/wcd9xxx/wcd9xxx_registers.h>
  18. #include "wcd9xxx-common.h"
  19. #define CLSH_COMPUTE_EAR 0x01
  20. #define CLSH_COMPUTE_HPH_L 0x02
  21. #define CLSH_COMPUTE_HPH_R 0x03
  22. #define BUCK_VREF_2V 0xFF
  23. #define BUCK_VREF_1P8V 0xE6
  24. #define BUCK_SETTLE_TIME_US 50
  25. #define NCP_SETTLE_TIME_US 50
  26. #define MAX_IMPED_PARAMS 13
  27. #define USLEEP_RANGE_MARGIN_US 100
  28. struct wcd9xxx_imped_val {
  29. u32 imped_val;
  30. u8 index;
  31. };
  32. static const struct wcd9xxx_reg_mask_val imped_table[][MAX_IMPED_PARAMS] = {
  33. {
  34. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x46},
  35. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x04},
  36. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  37. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x11},
  38. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x02},
  39. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x9B},
  40. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x02},
  41. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x15},
  42. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  43. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1C},
  44. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  45. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x04},
  46. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x0C},
  47. },
  48. {
  49. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x47},
  50. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x05},
  51. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  52. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x11},
  53. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x02},
  54. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x9B},
  55. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x02},
  56. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x15},
  57. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  58. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1C},
  59. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  60. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x05},
  61. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x0C},
  62. },
  63. {
  64. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x49},
  65. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x07},
  66. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  67. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  68. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x02},
  69. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x12},
  70. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  71. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x35},
  72. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  73. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x4E},
  74. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  75. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x06},
  76. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x0E},
  77. },
  78. {
  79. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x49},
  80. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x16},
  81. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  82. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAC},
  83. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x02},
  84. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x17},
  85. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  86. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x5F},
  87. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  88. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xCF},
  89. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  90. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x06},
  91. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x0F},
  92. },
  93. {
  94. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x59},
  95. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x15},
  96. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  97. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x9C},
  98. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x02},
  99. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1B},
  100. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  101. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xCE},
  102. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  103. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xBD},
  104. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  105. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x07},
  106. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x10},
  107. },
  108. {
  109. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x66},
  110. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x04},
  111. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  112. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x9A},
  113. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x02},
  114. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2E},
  115. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  116. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xBD},
  117. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  118. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xA6},
  119. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  120. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x07},
  121. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x11},
  122. },
  123. {
  124. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x79},
  125. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x04},
  126. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  127. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x11},
  128. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  129. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x37},
  130. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  131. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xA6},
  132. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  133. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAD},
  134. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  135. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x08},
  136. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x12},
  137. },
  138. {
  139. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x76},
  140. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x04},
  141. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  142. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x11},
  143. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  144. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x4E},
  145. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  146. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAD},
  147. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  148. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAC},
  149. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  150. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x09},
  151. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x12},
  152. },
  153. {
  154. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x78},
  155. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x05},
  156. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  157. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x12},
  158. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  159. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xD0},
  160. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  161. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAC},
  162. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  163. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x13},
  164. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  165. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x0A},
  166. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x13},
  167. },
  168. {
  169. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x7A},
  170. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x06},
  171. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  172. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x14},
  173. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  174. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xB7},
  175. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  176. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x13},
  177. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  178. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x14},
  179. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  180. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x0B},
  181. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x14},
  182. },
  183. {
  184. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x60},
  185. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x09},
  186. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  187. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1C},
  188. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  189. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xA4},
  190. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  191. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x14},
  192. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  193. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1F},
  194. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  195. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x0C},
  196. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x14},
  197. },
  198. {
  199. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x79},
  200. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x17},
  201. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  202. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x25},
  203. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  204. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAE},
  205. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  206. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1F},
  207. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  208. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1D},
  209. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  210. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x0D},
  211. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x15},
  212. },
  213. {
  214. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x78},
  215. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x16},
  216. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  217. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2C},
  218. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  219. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAC},
  220. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  221. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1D},
  222. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  223. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1C},
  224. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  225. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x0E},
  226. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x16},
  227. },
  228. {
  229. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x89},
  230. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x05},
  231. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  232. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x40},
  233. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  234. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x13},
  235. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  236. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1C},
  237. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  238. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1B},
  239. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  240. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x10},
  241. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x16},
  242. },
  243. {
  244. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x97},
  245. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x05},
  246. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  247. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xD0},
  248. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  249. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x14},
  250. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  251. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1B},
  252. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  253. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1B},
  254. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  255. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x12},
  256. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x17},
  257. },
  258. {
  259. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x8A},
  260. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x06},
  261. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  262. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xB7},
  263. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  264. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x10},
  265. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  266. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1B},
  267. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  268. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x24},
  269. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  270. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x13},
  271. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x17},
  272. },
  273. {
  274. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x8A},
  275. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x07},
  276. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  277. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xA4},
  278. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  279. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1D},
  280. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  281. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x24},
  282. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  283. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x25},
  284. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  285. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x15},
  286. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x18},
  287. },
  288. {
  289. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x9A},
  290. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x08},
  291. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  292. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAE},
  293. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  294. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1C},
  295. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  296. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x25},
  297. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  298. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x27},
  299. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  300. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x18},
  301. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x19},
  302. },
  303. {
  304. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x8B},
  305. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x18},
  306. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  307. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAC},
  308. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  309. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1B},
  310. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  311. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x20},
  312. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  313. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2E},
  314. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  315. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x1A},
  316. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x19},
  317. },
  318. {
  319. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x9A},
  320. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x17},
  321. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  322. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x13},
  323. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  324. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1B},
  325. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  326. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2E},
  327. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  328. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2D},
  329. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  330. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x1D},
  331. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x1A},
  332. },
  333. {
  334. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0xA9},
  335. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x06},
  336. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  337. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x14},
  338. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  339. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x24},
  340. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  341. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2D},
  342. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  343. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2C},
  344. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  345. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x1F},
  346. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x19},
  347. },
  348. {
  349. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0xB9},
  350. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x06},
  351. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  352. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x10},
  353. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  354. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x25},
  355. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  356. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2C},
  357. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  358. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2C},
  359. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  360. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x23},
  361. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x18},
  362. },
  363. {
  364. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0xA9},
  365. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x07},
  366. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0xff, 0x00},
  367. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1D},
  368. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  369. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x27},
  370. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  371. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2C},
  372. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  373. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x35},
  374. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  375. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0xff, 0x26},
  376. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0xff, 0x16},
  377. },
  378. };
  379. static const struct wcd9xxx_imped_val imped_index[] = {
  380. {4000, 0},
  381. {4500, 1},
  382. {5000, 2},
  383. {5500, 3},
  384. {6000, 4},
  385. {6500, 5},
  386. {7000, 6},
  387. {7700, 7},
  388. {8470, 8},
  389. {9317, 9},
  390. {10248, 10},
  391. {11273, 11},
  392. {12400, 12},
  393. {13641, 13},
  394. {15005, 14},
  395. {16505, 15},
  396. {18156, 16},
  397. {19971, 17},
  398. {21969, 18},
  399. {24165, 19},
  400. {26582, 20},
  401. {29240, 21},
  402. {32164, 22},
  403. };
  404. static inline void
  405. wcd9xxx_enable_clsh_block(struct snd_soc_codec *codec,
  406. struct wcd9xxx_clsh_cdc_data *clsh_d, bool enable)
  407. {
  408. if ((enable && ++clsh_d->clsh_users == 1) ||
  409. (!enable && --clsh_d->clsh_users == 0))
  410. snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLSH_B1_CTL,
  411. 0x01, enable ? 0x01 : 0x00);
  412. dev_dbg(codec->dev, "%s: clsh_users %d, enable %d", __func__,
  413. clsh_d->clsh_users, enable);
  414. }
  415. static inline void wcd9xxx_enable_anc_delay(
  416. struct snd_soc_codec *codec,
  417. bool on)
  418. {
  419. snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLSH_B1_CTL,
  420. 0x02, on ? 0x02 : 0x00);
  421. }
  422. static inline void
  423. wcd9xxx_enable_buck(struct snd_soc_codec *codec,
  424. struct wcd9xxx_clsh_cdc_data *clsh_d, bool enable)
  425. {
  426. if ((enable && ++clsh_d->buck_users == 1) ||
  427. (!enable && --clsh_d->buck_users == 0))
  428. snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_1,
  429. 0x80, enable ? 0x80 : 0x00);
  430. dev_dbg(codec->dev, "%s: buck_users %d, enable %d", __func__,
  431. clsh_d->buck_users, enable);
  432. }
  433. static void (*clsh_state_fp[NUM_CLSH_STATES])(struct snd_soc_codec *,
  434. struct wcd9xxx_clsh_cdc_data *,
  435. u8 req_state, bool req_type);
  436. static const char *state_to_str(u8 state, char *buf, size_t buflen)
  437. {
  438. int i;
  439. int cnt = 0;
  440. /*
  441. * This array of strings should match with enum wcd9xxx_clsh_state_bit.
  442. */
  443. const char *states[] = {
  444. "STATE_EAR",
  445. "STATE_HPH_L",
  446. "STATE_HPH_R",
  447. "STATE_LO",
  448. };
  449. if (state == WCD9XXX_CLSH_STATE_IDLE) {
  450. snprintf(buf, buflen, "[STATE_IDLE]");
  451. goto done;
  452. }
  453. buf[0] = '\0';
  454. for (i = 0; i < ARRAY_SIZE(states); i++) {
  455. if (!(state & (1 << i)))
  456. continue;
  457. cnt = snprintf(buf, buflen - cnt - 1, "%s%s%s", buf,
  458. buf[0] == '\0' ? "[" : "|",
  459. states[i]);
  460. }
  461. if (cnt > 0)
  462. strlcat(buf + cnt, "]", buflen);
  463. done:
  464. if (buf[0] == '\0')
  465. snprintf(buf, buflen, "[STATE_UNKNOWN]");
  466. return buf;
  467. }
  468. static void wcd9xxx_cfg_clsh_param_common(
  469. struct snd_soc_codec *codec)
  470. {
  471. int i;
  472. const struct wcd9xxx_reg_mask_val reg_set[] = {
  473. {WCD9XXX_A_CDC_CLSH_BUCK_NCP_VARS, 0x3 << 0, 0},
  474. {WCD9XXX_A_CDC_CLSH_BUCK_NCP_VARS, 0x3 << 2, 1 << 2},
  475. {WCD9XXX_A_CDC_CLSH_BUCK_NCP_VARS, (0x1 << 4), 0},
  476. {WCD9XXX_A_CDC_CLSH_B2_CTL, (0x3 << 0), 0x01},
  477. {WCD9XXX_A_CDC_CLSH_B2_CTL, (0x3 << 2), (0x01 << 2)},
  478. {WCD9XXX_A_CDC_CLSH_B2_CTL, (0xf << 4), (0x03 << 4)},
  479. {WCD9XXX_A_CDC_CLSH_B3_CTL, (0xf << 4), (0x03 << 4)},
  480. {WCD9XXX_A_CDC_CLSH_B3_CTL, (0xf << 0), (0x0B)},
  481. {WCD9XXX_A_CDC_CLSH_B1_CTL, (0x1 << 5), (0x01 << 5)},
  482. {WCD9XXX_A_CDC_CLSH_B1_CTL, (0x1 << 1), (0x01 << 1)},
  483. };
  484. for (i = 0; i < ARRAY_SIZE(reg_set); i++)
  485. snd_soc_update_bits(codec, reg_set[i].reg, reg_set[i].mask,
  486. reg_set[i].val);
  487. dev_dbg(codec->dev, "%s: Programmed class H controller common parameters",
  488. __func__);
  489. }
  490. static void wcd9xxx_chargepump_request(struct snd_soc_codec *codec, bool on)
  491. {
  492. static int cp_count;
  493. if (on && (++cp_count == 1)) {
  494. snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLK_OTHR_CTL,
  495. 0x01, 0x01);
  496. dev_dbg(codec->dev, "%s: Charge Pump enabled, count = %d\n",
  497. __func__, cp_count);
  498. } else if (!on) {
  499. if (--cp_count < 0) {
  500. dev_dbg(codec->dev,
  501. "%s: Unbalanced disable for charge pump\n",
  502. __func__);
  503. if (snd_soc_read(codec, WCD9XXX_A_CDC_CLK_OTHR_CTL) &
  504. 0x01) {
  505. dev_dbg(codec->dev,
  506. "%s: Actual chargepump is ON\n",
  507. __func__);
  508. }
  509. cp_count = 0;
  510. WARN_ON(1);
  511. }
  512. if (cp_count == 0) {
  513. snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLK_OTHR_CTL,
  514. 0x01, 0x00);
  515. dev_dbg(codec->dev,
  516. "%s: Charge pump disabled, count = %d\n",
  517. __func__, cp_count);
  518. }
  519. }
  520. }
  521. static int get_impedance_index(u32 imped)
  522. {
  523. int i = 0;
  524. if (imped < imped_index[i].imped_val) {
  525. pr_debug("%s, detected impedance is less than 4 Ohm\n",
  526. __func__);
  527. goto ret;
  528. }
  529. if (imped >= imped_index[ARRAY_SIZE(imped_index) - 1].imped_val) {
  530. pr_debug("%s, detected impedance is greater than 32164 Ohm\n",
  531. __func__);
  532. i = ARRAY_SIZE(imped_index) - 1;
  533. goto ret;
  534. }
  535. for (i = 0; i < ARRAY_SIZE(imped_index) - 1; i++) {
  536. if (imped >= imped_index[i].imped_val &&
  537. imped < imped_index[i + 1].imped_val)
  538. break;
  539. }
  540. ret:
  541. pr_debug("%s: selected impedance index = %d\n",
  542. __func__, imped_index[i].index);
  543. return imped_index[i].index;
  544. }
  545. void wcd9xxx_clsh_imped_config(struct snd_soc_codec *codec,
  546. int imped)
  547. {
  548. int i = 0;
  549. int index = 0;
  550. index = get_impedance_index(imped);
  551. if (index >= ARRAY_SIZE(imped_index)) {
  552. pr_err("%s, invalid imped = %d\n", __func__, imped);
  553. return;
  554. }
  555. for (i = 0; i < MAX_IMPED_PARAMS; i++)
  556. snd_soc_write(codec, imped_table[index][i].reg,
  557. imped_table[index][i].val);
  558. }
  559. static void wcd9xxx_clsh_comp_req(struct snd_soc_codec *codec,
  560. struct wcd9xxx_clsh_cdc_data *clsh_d,
  561. int compute_pa, bool on)
  562. {
  563. u8 shift;
  564. if (compute_pa == CLSH_COMPUTE_EAR) {
  565. snd_soc_update_bits(codec, WCD9XXX_A_CDC_CLSH_B1_CTL, 0x10,
  566. (on ? 0x10 : 0));
  567. } else {
  568. if (compute_pa == CLSH_COMPUTE_HPH_L) {
  569. shift = 3;
  570. } else if (compute_pa == CLSH_COMPUTE_HPH_R) {
  571. shift = 2;
  572. } else {
  573. dev_dbg(codec->dev,
  574. "%s: classh computation request is incorrect\n",
  575. __func__);
  576. return;
  577. }
  578. if (on)
  579. wcd9xxx_resmgr_add_cond_update_bits(clsh_d->resmgr,
  580. WCD9XXX_COND_HPH,
  581. WCD9XXX_A_CDC_CLSH_B1_CTL,
  582. shift, false);
  583. else
  584. wcd9xxx_resmgr_rm_cond_update_bits(clsh_d->resmgr,
  585. WCD9XXX_COND_HPH,
  586. WCD9XXX_A_CDC_CLSH_B1_CTL,
  587. shift, false);
  588. }
  589. }
  590. int wcd9xxx_soc_update_bits_push(struct snd_soc_codec *codec,
  591. struct list_head *list,
  592. uint16_t reg, uint8_t mask,
  593. uint8_t value, int delay)
  594. {
  595. int rc;
  596. struct wcd9xxx_register_save_node *node;
  597. node = kmalloc(sizeof(*node), GFP_KERNEL);
  598. if (unlikely(!node)) {
  599. pr_err("%s: Not enough memory\n", __func__);
  600. return -ENOMEM;
  601. }
  602. node->reg = reg;
  603. node->value = snd_soc_read(codec, reg);
  604. list_add(&node->lh, list);
  605. if (mask == 0xFF)
  606. rc = snd_soc_write(codec, reg, value);
  607. else
  608. rc = snd_soc_update_bits(codec, reg, mask, value);
  609. if (delay)
  610. usleep_range(delay, delay + USLEEP_RANGE_MARGIN_US);
  611. return rc;
  612. }
  613. EXPORT_SYMBOL(wcd9xxx_soc_update_bits_push);
  614. void wcd9xxx_restore_registers(struct snd_soc_codec *codec,
  615. struct list_head *lh)
  616. {
  617. struct wcd9xxx_register_save_node *node, *nodetmp;
  618. list_for_each_entry_safe(node, nodetmp, lh, lh) {
  619. snd_soc_write(codec, node->reg, node->value);
  620. list_del(&node->lh);
  621. kfree(node);
  622. }
  623. }
  624. EXPORT_SYMBOL(wcd9xxx_restore_registers);
  625. static void wcd9xxx_dynamic_bypass_buck_ctrl_lo(struct snd_soc_codec *cdc,
  626. bool enable)
  627. {
  628. int i;
  629. const struct wcd9xxx_reg_mask_val reg_set[] = {
  630. {WCD9XXX_A_BUCK_MODE_3, (0x1 << 3), (enable << 3)},
  631. {WCD9XXX_A_BUCK_MODE_5, enable ? 0xFF : 0x02, 0x02},
  632. {WCD9XXX_A_BUCK_MODE_5, 0x1, 0x01}
  633. };
  634. if (!enable) {
  635. snd_soc_update_bits(cdc, WCD9XXX_A_BUCK_MODE_1,
  636. (0x1 << 3), 0x00);
  637. snd_soc_update_bits(cdc, WCD9XXX_A_BUCK_MODE_4,
  638. 0xFF, BUCK_VREF_2V);
  639. }
  640. for (i = 0; i < ARRAY_SIZE(reg_set); i++)
  641. snd_soc_update_bits(cdc, reg_set[i].reg, reg_set[i].mask,
  642. reg_set[i].val);
  643. /* 50us sleep is reqd. as per the class H HW design sequence */
  644. usleep_range(BUCK_SETTLE_TIME_US, BUCK_SETTLE_TIME_US+10);
  645. }
  646. static void wcd9xxx_dynamic_bypass_buck_ctrl(struct snd_soc_codec *cdc,
  647. bool enable)
  648. {
  649. int i;
  650. const struct wcd9xxx_reg_mask_val reg_set[] = {
  651. {WCD9XXX_A_BUCK_MODE_3, (0x1 << 3), (enable << 3)},
  652. {WCD9XXX_A_BUCK_MODE_5, (0x1 << 1), ((!enable) << 1)},
  653. {WCD9XXX_A_BUCK_MODE_5, 0x1, !enable}
  654. };
  655. if (!enable) {
  656. snd_soc_update_bits(cdc, WCD9XXX_A_BUCK_MODE_1,
  657. (0x1 << 3), 0x00);
  658. snd_soc_update_bits(cdc, WCD9XXX_A_BUCK_MODE_4,
  659. 0xFF, BUCK_VREF_2V);
  660. }
  661. for (i = 0; i < ARRAY_SIZE(reg_set); i++)
  662. snd_soc_update_bits(cdc, reg_set[i].reg, reg_set[i].mask,
  663. reg_set[i].val);
  664. /* 50us sleep is reqd. as per the class H HW design sequence */
  665. usleep_range(BUCK_SETTLE_TIME_US, BUCK_SETTLE_TIME_US+10);
  666. }
  667. static void wcd9xxx_set_buck_mode(struct snd_soc_codec *codec, u8 buck_vref)
  668. {
  669. int i;
  670. const struct wcd9xxx_reg_mask_val reg_set[] = {
  671. {WCD9XXX_A_BUCK_MODE_5, 0x02, 0x02},
  672. {WCD9XXX_A_BUCK_MODE_4, 0xFF, buck_vref},
  673. {WCD9XXX_A_BUCK_MODE_1, 0x04, 0x04},
  674. {WCD9XXX_A_BUCK_MODE_3, 0x04, 0x00},
  675. {WCD9XXX_A_BUCK_MODE_3, 0x08, 0x00},
  676. };
  677. for (i = 0; i < ARRAY_SIZE(reg_set); i++)
  678. snd_soc_update_bits(codec, reg_set[i].reg,
  679. reg_set[i].mask, reg_set[i].val);
  680. dev_dbg(codec->dev, "%s: Done\n", __func__);
  681. usleep_range(BUCK_SETTLE_TIME_US, BUCK_SETTLE_TIME_US);
  682. }
  683. /* This will be called for all states except Lineout */
  684. static void wcd9xxx_clsh_enable_post_pa(struct snd_soc_codec *codec,
  685. struct wcd9xxx_clsh_cdc_data *cdc_clsh_d)
  686. {
  687. int i;
  688. const struct wcd9xxx_reg_mask_val reg_set[] = {
  689. {WCD9XXX_A_BUCK_MODE_5, 0x02, 0x00},
  690. {WCD9XXX_A_NCP_STATIC, 0x20, 0x00},
  691. {WCD9XXX_A_BUCK_MODE_3, 0x04, 0x04},
  692. };
  693. for (i = 0; i < ARRAY_SIZE(reg_set); i++)
  694. snd_soc_update_bits(codec, reg_set[i].reg,
  695. reg_set[i].mask, reg_set[i].val);
  696. if (!cdc_clsh_d->is_dynamic_vdd_cp)
  697. snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_3,
  698. 0x08, 0x08);
  699. dev_dbg(codec->dev, "%s: completed clsh mode settings after PA enable\n",
  700. __func__);
  701. }
  702. static void wcd9xxx_set_fclk_get_ncp(struct snd_soc_codec *codec,
  703. struct wcd9xxx_clsh_cdc_data *clsh_d,
  704. enum ncp_fclk_level fclk_level)
  705. {
  706. clsh_d->ncp_users[fclk_level]++;
  707. pr_debug("%s: enter ncp type %d users fclk8 %d, fclk5 %d\n", __func__,
  708. fclk_level, clsh_d->ncp_users[NCP_FCLK_LEVEL_8],
  709. clsh_d->ncp_users[NCP_FCLK_LEVEL_5]);
  710. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, 0x10, 0x00);
  711. /* fclk level 8 dominates level 5 */
  712. if (clsh_d->ncp_users[NCP_FCLK_LEVEL_8] > 0)
  713. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, 0x0F, 0x08);
  714. else if (clsh_d->ncp_users[NCP_FCLK_LEVEL_5] > 0)
  715. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, 0x0F, 0x05);
  716. else
  717. WARN_ONCE(1, "Unexpected users %d,%d\n",
  718. clsh_d->ncp_users[NCP_FCLK_LEVEL_8],
  719. clsh_d->ncp_users[NCP_FCLK_LEVEL_5]);
  720. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, 0x20, 0x20);
  721. /* enable NCP and wait until settles down */
  722. if (snd_soc_update_bits(codec, WCD9XXX_A_NCP_EN, 0x01, 0x01))
  723. usleep_range(NCP_SETTLE_TIME_US, NCP_SETTLE_TIME_US);
  724. pr_debug("%s: leave\n", __func__);
  725. }
  726. static void wcd9xxx_set_fclk_put_ncp(struct snd_soc_codec *codec,
  727. struct wcd9xxx_clsh_cdc_data *clsh_d,
  728. enum ncp_fclk_level fclk_level)
  729. {
  730. clsh_d->ncp_users[fclk_level]--;
  731. pr_debug("%s: enter ncp type %d users fclk8 %d, fclk5 %d\n", __func__,
  732. fclk_level, clsh_d->ncp_users[NCP_FCLK_LEVEL_8],
  733. clsh_d->ncp_users[NCP_FCLK_LEVEL_5]);
  734. if (clsh_d->ncp_users[NCP_FCLK_LEVEL_8] == 0 &&
  735. clsh_d->ncp_users[NCP_FCLK_LEVEL_5] == 0)
  736. snd_soc_update_bits(codec, WCD9XXX_A_NCP_EN, 0x01, 0x00);
  737. else if (clsh_d->ncp_users[NCP_FCLK_LEVEL_8] == 0)
  738. /* if dominating level 8 has gone, switch to 5 */
  739. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, 0x0F, 0x05);
  740. pr_debug("%s: leave\n", __func__);
  741. }
  742. static void wcd9xxx_cfg_clsh_param_ear(struct snd_soc_codec *codec)
  743. {
  744. int i;
  745. const struct wcd9xxx_reg_mask_val reg_set[] = {
  746. {WCD9XXX_A_CDC_CLSH_B1_CTL, (0x1 << 7), 0},
  747. {WCD9XXX_A_CDC_CLSH_V_PA_HD_EAR, (0x3f << 0), 0x0D},
  748. {WCD9XXX_A_CDC_CLSH_V_PA_MIN_EAR, (0x3f << 0), 0x3A},
  749. /* Under assumption that EAR load is 10.7ohm */
  750. {WCD9XXX_A_CDC_CLSH_IDLE_EAR_THSD, (0x3f << 0), 0x26},
  751. {WCD9XXX_A_CDC_CLSH_FCLKONLY_EAR_THSD, (0x3f << 0), 0x2C},
  752. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_EAR_L, 0xff, 0xA9},
  753. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_EAR_U, 0xff, 0x07},
  754. {WCD9XXX_A_CDC_CLSH_K_ADDR, (0x1 << 7), 0},
  755. {WCD9XXX_A_CDC_CLSH_K_ADDR, (0xf << 0), 0x08},
  756. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1b},
  757. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  758. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x2d},
  759. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  760. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x36},
  761. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  762. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x37},
  763. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  764. };
  765. for (i = 0; i < ARRAY_SIZE(reg_set); i++)
  766. snd_soc_update_bits(codec, reg_set[i].reg,
  767. reg_set[i].mask, reg_set[i].val);
  768. dev_dbg(codec->dev, "%s: Programmed Class H controller EAR specific params\n",
  769. __func__);
  770. }
  771. static void wcd9xxx_cfg_clsh_param_hph(struct snd_soc_codec *codec)
  772. {
  773. int i;
  774. const struct wcd9xxx_reg_mask_val reg_set[] = {
  775. {WCD9XXX_A_CDC_CLSH_B1_CTL, (0x1 << 6), 0},
  776. {WCD9XXX_A_CDC_CLSH_V_PA_HD_HPH, 0x3f, 0x0D},
  777. {WCD9XXX_A_CDC_CLSH_V_PA_MIN_HPH, 0x3f, 0x1D},
  778. /* Under assumption that HPH load is 16ohm per channel */
  779. {WCD9XXX_A_CDC_CLSH_IDLE_HPH_THSD, 0x3f, 0x13},
  780. {WCD9XXX_A_CDC_CLSH_FCLKONLY_HPH_THSD, 0x1f, 0x19},
  781. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_L, 0xff, 0x97},
  782. {WCD9XXX_A_CDC_CLSH_I_PA_FACT_HPH_U, 0xff, 0x05},
  783. {WCD9XXX_A_CDC_CLSH_K_ADDR, (0x1 << 7), 0},
  784. {WCD9XXX_A_CDC_CLSH_K_ADDR, 0x0f, 0},
  785. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0xAE},
  786. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x01},
  787. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x1C},
  788. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  789. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x24},
  790. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  791. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x25},
  792. {WCD9XXX_A_CDC_CLSH_K_DATA, 0xff, 0x00},
  793. };
  794. for (i = 0; i < ARRAY_SIZE(reg_set); i++)
  795. snd_soc_update_bits(codec, reg_set[i].reg, reg_set[i].mask,
  796. reg_set[i].val);
  797. dev_dbg(codec->dev, "%s: Programmed Class H controller HPH specific params\n",
  798. __func__);
  799. }
  800. static void wcd9xxx_ncp_bypass_enable(struct snd_soc_codec *cdc, bool enable)
  801. {
  802. snd_soc_update_bits(cdc, WCD9XXX_A_NCP_STATIC, 0x10, (enable << 4));
  803. /* 50us sleep is reqd. as per the class H HW design sequence */
  804. usleep_range(BUCK_SETTLE_TIME_US, BUCK_SETTLE_TIME_US+10);
  805. }
  806. static void wcd9xxx_clsh_set_Iest(struct snd_soc_codec *codec,
  807. u8 value)
  808. {
  809. snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_5,
  810. 0x01, (0x01 & 0x03));
  811. snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_5,
  812. 0xFC, (value << 2));
  813. }
  814. static void wcd9xxx_clsh_state_hph_ear(struct snd_soc_codec *codec,
  815. struct wcd9xxx_clsh_cdc_data *clsh_d,
  816. u8 req_state, bool is_enable)
  817. {
  818. int compute_pa = 0;
  819. dev_dbg(codec->dev, "%s: enter %s\n", __func__,
  820. is_enable ? "enable" : "disable");
  821. if (is_enable) {
  822. /*
  823. * The below check condition is required to make sure
  824. * functions inside if condition will execute only once.
  825. */
  826. if ((clsh_d->state == WCD9XXX_CLSH_STATE_EAR) ||
  827. (req_state == WCD9XXX_CLSH_STATE_EAR)) {
  828. wcd9xxx_dynamic_bypass_buck_ctrl(codec, false);
  829. wcd9xxx_ncp_bypass_enable(codec, true);
  830. }
  831. switch (req_state) {
  832. case WCD9XXX_CLSH_STATE_HPHL:
  833. compute_pa = CLSH_COMPUTE_HPH_L;
  834. break;
  835. case WCD9XXX_CLSH_STATE_HPHR:
  836. compute_pa = CLSH_COMPUTE_HPH_R;
  837. break;
  838. case WCD9XXX_CLSH_STATE_EAR:
  839. compute_pa = CLSH_COMPUTE_EAR;
  840. break;
  841. default:
  842. dev_dbg(codec->dev,
  843. "%s:Invalid state:0x%x,enable:0x%x\n",
  844. __func__, req_state, is_enable);
  845. break;
  846. }
  847. wcd9xxx_clsh_comp_req(codec, clsh_d, compute_pa, true);
  848. dev_dbg(codec->dev, "%s: Enabled hph+ear mode clsh\n",
  849. __func__);
  850. } else {
  851. switch (req_state) {
  852. case WCD9XXX_CLSH_STATE_HPHL:
  853. compute_pa = CLSH_COMPUTE_HPH_L;
  854. break;
  855. case WCD9XXX_CLSH_STATE_HPHR:
  856. compute_pa = CLSH_COMPUTE_HPH_R;
  857. break;
  858. case WCD9XXX_CLSH_STATE_EAR:
  859. compute_pa = CLSH_COMPUTE_EAR;
  860. break;
  861. default:
  862. dev_dbg(codec->dev,
  863. "%s:Invalid state:0x%x,enable:0x%x\n",
  864. __func__, req_state, is_enable);
  865. break;
  866. }
  867. wcd9xxx_clsh_comp_req(codec, clsh_d, compute_pa, false);
  868. if (((clsh_d->state & (~req_state)) ==
  869. WCD9XXX_CLSH_STATE_EAR) ||
  870. (req_state == WCD9XXX_CLSH_STATE_EAR)) {
  871. wcd9xxx_ncp_bypass_enable(codec, false);
  872. wcd9xxx_dynamic_bypass_buck_ctrl(codec, true);
  873. }
  874. }
  875. }
  876. static void wcd9xxx_clsh_state_hph_lo(struct snd_soc_codec *codec,
  877. struct wcd9xxx_clsh_cdc_data *clsh_d,
  878. u8 req_state, bool is_enable)
  879. {
  880. dev_dbg(codec->dev, "%s: enter %s\n", __func__,
  881. is_enable ? "enable" : "disable");
  882. if (is_enable) {
  883. if ((clsh_d->state == WCD9XXX_CLSH_STATE_LO) ||
  884. (req_state == WCD9XXX_CLSH_STATE_LO)) {
  885. wcd9xxx_dynamic_bypass_buck_ctrl_lo(codec, false);
  886. wcd9xxx_set_fclk_get_ncp(codec, clsh_d,
  887. NCP_FCLK_LEVEL_8);
  888. if (req_state & WCD9XXX_CLSH_STATE_HPH_ST) {
  889. wcd9xxx_ncp_bypass_enable(codec, true);
  890. wcd9xxx_enable_clsh_block(codec, clsh_d, true);
  891. wcd9xxx_chargepump_request(codec, true);
  892. wcd9xxx_enable_anc_delay(codec, true);
  893. }
  894. }
  895. if (req_state == WCD9XXX_CLSH_STATE_HPHL)
  896. wcd9xxx_clsh_comp_req(codec, clsh_d,
  897. CLSH_COMPUTE_HPH_L, true);
  898. if (req_state == WCD9XXX_CLSH_STATE_HPHR)
  899. wcd9xxx_clsh_comp_req(codec, clsh_d,
  900. CLSH_COMPUTE_HPH_R, true);
  901. } else {
  902. switch (req_state) {
  903. case WCD9XXX_CLSH_STATE_LO:
  904. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC,
  905. 0x20, 0x00);
  906. wcd9xxx_dynamic_bypass_buck_ctrl_lo(codec, true);
  907. break;
  908. case WCD9XXX_CLSH_STATE_HPHL:
  909. wcd9xxx_clsh_comp_req(codec, clsh_d,
  910. CLSH_COMPUTE_HPH_L, false);
  911. break;
  912. case WCD9XXX_CLSH_STATE_HPHR:
  913. wcd9xxx_clsh_comp_req(codec, clsh_d,
  914. CLSH_COMPUTE_HPH_R, false);
  915. break;
  916. default:
  917. dev_dbg(codec->dev,
  918. "%s:Invalid state:0x%x,enable:0x%x\n",
  919. __func__, req_state, is_enable);
  920. break;
  921. }
  922. if ((req_state == WCD9XXX_CLSH_STATE_LO) ||
  923. ((clsh_d->state & (~req_state)) == WCD9XXX_CLSH_STATE_LO)) {
  924. wcd9xxx_set_fclk_put_ncp(codec, clsh_d,
  925. NCP_FCLK_LEVEL_8);
  926. wcd9xxx_ncp_bypass_enable(codec, false);
  927. if (req_state & WCD9XXX_CLSH_STATE_HPH_ST) {
  928. usleep_range(BUCK_SETTLE_TIME_US,
  929. BUCK_SETTLE_TIME_US + 10);
  930. if (clsh_d->buck_mv ==
  931. WCD9XXX_CDC_BUCK_MV_1P8) {
  932. wcd9xxx_enable_buck(codec, clsh_d,
  933. false);
  934. wcd9xxx_ncp_bypass_enable(codec, true);
  935. } else {
  936. /*
  937. *NCP settle time recommended by codec
  938. *specification
  939. */
  940. usleep_range(NCP_SETTLE_TIME_US,
  941. NCP_SETTLE_TIME_US + 10);
  942. wcd9xxx_clsh_set_Iest(codec, 0x02);
  943. }
  944. snd_soc_update_bits(codec,
  945. WCD9XXX_A_BUCK_MODE_1,
  946. 0x04, 0x00);
  947. snd_soc_update_bits(codec,
  948. WCD9XXX_A_BUCK_MODE_4,
  949. 0xFF, BUCK_VREF_1P8V);
  950. }
  951. }
  952. }
  953. }
  954. static void wcd9xxx_clsh_state_ear_lo(struct snd_soc_codec *codec,
  955. struct wcd9xxx_clsh_cdc_data *clsh_d,
  956. u8 req_state, bool is_enable)
  957. {
  958. dev_dbg(codec->dev, "%s: enter %s\n", __func__,
  959. is_enable ? "enable" : "disable");
  960. if (is_enable) {
  961. wcd9xxx_dynamic_bypass_buck_ctrl(codec, false);
  962. wcd9xxx_enable_buck(codec, clsh_d, true);
  963. if (req_state & WCD9XXX_CLSH_STATE_EAR) {
  964. wcd9xxx_set_fclk_get_ncp(codec, clsh_d,
  965. NCP_FCLK_LEVEL_8);
  966. wcd9xxx_ncp_bypass_enable(codec, true);
  967. wcd9xxx_enable_clsh_block(codec, clsh_d, true);
  968. wcd9xxx_chargepump_request(codec, true);
  969. wcd9xxx_enable_anc_delay(codec, true);
  970. wcd9xxx_clsh_comp_req(codec, clsh_d,
  971. CLSH_COMPUTE_EAR, true);
  972. }
  973. } else {
  974. wcd9xxx_set_fclk_put_ncp(codec, clsh_d, NCP_FCLK_LEVEL_8);
  975. wcd9xxx_ncp_bypass_enable(codec, false);
  976. if (req_state & WCD9XXX_CLSH_STATE_LO) {
  977. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC,
  978. 0x20, 0x00);
  979. wcd9xxx_dynamic_bypass_buck_ctrl(codec, true);
  980. } else if (req_state & WCD9XXX_CLSH_STATE_EAR) {
  981. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_EAR,
  982. false);
  983. /*sleep 5ms*/
  984. if (clsh_d->buck_mv == WCD9XXX_CDC_BUCK_MV_1P8) {
  985. wcd9xxx_enable_buck(codec, clsh_d, false);
  986. wcd9xxx_ncp_bypass_enable(codec, true);
  987. } else {
  988. /* NCP settle time recommended by codec spec */
  989. usleep_range(NCP_SETTLE_TIME_US,
  990. NCP_SETTLE_TIME_US + 10);
  991. wcd9xxx_clsh_set_Iest(codec, 0x02);
  992. }
  993. snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_1,
  994. 0x04, 0x00);
  995. snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_4,
  996. 0xFF, BUCK_VREF_1P8V);
  997. }
  998. }
  999. }
  1000. static void wcd9xxx_clsh_state_hph_ear_lo(struct snd_soc_codec *codec,
  1001. struct wcd9xxx_clsh_cdc_data *clsh_d,
  1002. u8 req_state, bool is_enable)
  1003. {
  1004. dev_dbg(codec->dev, "%s: enter %s\n", __func__,
  1005. is_enable ? "enable" : "disable");
  1006. if (req_state & WCD9XXX_CLSH_STATE_HPHL)
  1007. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_L,
  1008. is_enable);
  1009. if (req_state & WCD9XXX_CLSH_STATE_HPHR)
  1010. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_R,
  1011. is_enable);
  1012. if (req_state & WCD9XXX_CLSH_STATE_EAR)
  1013. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_EAR,
  1014. is_enable);
  1015. }
  1016. static void wcd9xxx_clsh_state_ear(struct snd_soc_codec *codec,
  1017. struct wcd9xxx_clsh_cdc_data *clsh_d,
  1018. u8 req_state, bool is_enable)
  1019. {
  1020. pr_debug("%s: enter %s\n", __func__, is_enable ? "enable" : "disable");
  1021. if (is_enable) {
  1022. wcd9xxx_cfg_clsh_param_common(codec);
  1023. wcd9xxx_cfg_clsh_param_ear(codec);
  1024. wcd9xxx_enable_clsh_block(codec, clsh_d, true);
  1025. wcd9xxx_chargepump_request(codec, true);
  1026. wcd9xxx_enable_anc_delay(codec, true);
  1027. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_EAR, true);
  1028. wcd9xxx_set_buck_mode(codec, BUCK_VREF_2V);
  1029. wcd9xxx_enable_buck(codec, clsh_d, true);
  1030. wcd9xxx_set_fclk_get_ncp(codec, clsh_d, NCP_FCLK_LEVEL_8);
  1031. dev_dbg(codec->dev, "%s: Enabled ear mode class h\n", __func__);
  1032. } else {
  1033. dev_dbg(codec->dev, "%s: stub fallback to ear\n", __func__);
  1034. wcd9xxx_set_fclk_put_ncp(codec, clsh_d, NCP_FCLK_LEVEL_8);
  1035. wcd9xxx_enable_buck(codec, clsh_d, false);
  1036. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_EAR, true);
  1037. wcd9xxx_chargepump_request(codec, false);
  1038. wcd9xxx_enable_clsh_block(codec, clsh_d, false);
  1039. }
  1040. }
  1041. static void wcd9xxx_clsh_set_hs_performance_mode(struct snd_soc_codec *codec,
  1042. struct wcd9xxx_clsh_cdc_data *clsh_d)
  1043. {
  1044. clsh_d->ncp_users[NCP_FCLK_LEVEL_8]++;
  1045. pr_debug("%s: users fclk8 %d, fclk5 %d\n", __func__,
  1046. clsh_d->ncp_users[NCP_FCLK_LEVEL_8],
  1047. clsh_d->ncp_users[NCP_FCLK_LEVEL_5]);
  1048. if (clsh_d->ncp_users[NCP_FCLK_LEVEL_8] > 0)
  1049. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, 0x0F, 0x08);
  1050. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC, 0x30, 0x30);
  1051. /* enable NCP and wait until settles down */
  1052. if (snd_soc_update_bits(codec, WCD9XXX_A_NCP_EN, 0x01, 0x01))
  1053. usleep_range(NCP_SETTLE_TIME_US, NCP_SETTLE_TIME_US);
  1054. }
  1055. static void wcd9xxx_clsh_state_hph_l(struct snd_soc_codec *codec,
  1056. struct wcd9xxx_clsh_cdc_data *clsh_d,
  1057. u8 req_state, bool is_enable)
  1058. {
  1059. pr_debug("%s: enter %s\n", __func__, is_enable ? "enable" : "disable");
  1060. if (is_enable) {
  1061. wcd9xxx_cfg_clsh_param_common(codec);
  1062. wcd9xxx_cfg_clsh_param_hph(codec);
  1063. wcd9xxx_enable_clsh_block(codec, clsh_d, true);
  1064. wcd9xxx_chargepump_request(codec, true);
  1065. wcd9xxx_enable_anc_delay(codec, true);
  1066. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_L, true);
  1067. if (clsh_d->hs_perf_mode_enabled)
  1068. wcd9xxx_clsh_set_hs_performance_mode(codec, clsh_d);
  1069. else {
  1070. wcd9xxx_set_buck_mode(codec, BUCK_VREF_2V);
  1071. wcd9xxx_enable_buck(codec, clsh_d, true);
  1072. wcd9xxx_set_fclk_get_ncp(codec, clsh_d, NCP_FCLK_LEVEL_8);
  1073. }
  1074. dev_dbg(codec->dev, "%s: Done\n", __func__);
  1075. } else {
  1076. wcd9xxx_set_fclk_put_ncp(codec, clsh_d, NCP_FCLK_LEVEL_8);
  1077. if (!clsh_d->hs_perf_mode_enabled)
  1078. wcd9xxx_enable_buck(codec, clsh_d, false);
  1079. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_L, false);
  1080. wcd9xxx_chargepump_request(codec, false);
  1081. wcd9xxx_enable_clsh_block(codec, clsh_d, false);
  1082. }
  1083. }
  1084. static void wcd9xxx_clsh_state_hph_r(struct snd_soc_codec *codec,
  1085. struct wcd9xxx_clsh_cdc_data *clsh_d,
  1086. u8 req_state, bool is_enable)
  1087. {
  1088. pr_debug("%s: enter %s\n", __func__, is_enable ? "enable" : "disable");
  1089. if (is_enable) {
  1090. wcd9xxx_cfg_clsh_param_common(codec);
  1091. wcd9xxx_cfg_clsh_param_hph(codec);
  1092. wcd9xxx_enable_clsh_block(codec, clsh_d, true);
  1093. wcd9xxx_chargepump_request(codec, true);
  1094. wcd9xxx_enable_anc_delay(codec, true);
  1095. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_R, true);
  1096. if (clsh_d->hs_perf_mode_enabled)
  1097. wcd9xxx_clsh_set_hs_performance_mode(codec, clsh_d);
  1098. else {
  1099. wcd9xxx_set_buck_mode(codec, BUCK_VREF_2V);
  1100. wcd9xxx_enable_buck(codec, clsh_d, true);
  1101. wcd9xxx_set_fclk_get_ncp(codec, clsh_d, NCP_FCLK_LEVEL_8);
  1102. }
  1103. dev_dbg(codec->dev, "%s: Done\n", __func__);
  1104. } else {
  1105. wcd9xxx_set_fclk_put_ncp(codec, clsh_d, NCP_FCLK_LEVEL_8);
  1106. if (!clsh_d->hs_perf_mode_enabled)
  1107. wcd9xxx_enable_buck(codec, clsh_d, false);
  1108. wcd9xxx_clsh_comp_req(codec, clsh_d, CLSH_COMPUTE_HPH_R, false);
  1109. wcd9xxx_chargepump_request(codec, false);
  1110. wcd9xxx_enable_clsh_block(codec, clsh_d, false);
  1111. }
  1112. }
  1113. static void wcd9xxx_clsh_state_hph_st(struct snd_soc_codec *codec,
  1114. struct wcd9xxx_clsh_cdc_data *clsh_d,
  1115. u8 req_state, bool is_enable)
  1116. {
  1117. pr_debug("%s: enter %s\n", __func__, is_enable ? "enable" : "disable");
  1118. if (is_enable) {
  1119. if (req_state == WCD9XXX_CLSH_STATE_HPHL)
  1120. wcd9xxx_clsh_comp_req(codec, clsh_d,
  1121. CLSH_COMPUTE_HPH_L, true);
  1122. if (req_state == WCD9XXX_CLSH_STATE_HPHR)
  1123. wcd9xxx_clsh_comp_req(codec, clsh_d,
  1124. CLSH_COMPUTE_HPH_R, true);
  1125. } else {
  1126. dev_dbg(codec->dev, "%s: stub fallback to hph_st\n", __func__);
  1127. if (req_state == WCD9XXX_CLSH_STATE_HPHL)
  1128. wcd9xxx_clsh_comp_req(codec, clsh_d,
  1129. CLSH_COMPUTE_HPH_L, false);
  1130. if (req_state == WCD9XXX_CLSH_STATE_HPHR)
  1131. wcd9xxx_clsh_comp_req(codec, clsh_d,
  1132. CLSH_COMPUTE_HPH_R, false);
  1133. }
  1134. }
  1135. static void wcd9xxx_clsh_state_lo(struct snd_soc_codec *codec,
  1136. struct wcd9xxx_clsh_cdc_data *clsh_d,
  1137. u8 req_state, bool is_enable)
  1138. {
  1139. pr_debug("%s: enter %s, buck_mv %d\n", __func__,
  1140. is_enable ? "enable" : "disable", clsh_d->buck_mv);
  1141. if (is_enable) {
  1142. wcd9xxx_set_buck_mode(codec, BUCK_VREF_1P8V);
  1143. wcd9xxx_enable_buck(codec, clsh_d, true);
  1144. wcd9xxx_set_fclk_get_ncp(codec, clsh_d, NCP_FCLK_LEVEL_5);
  1145. if (clsh_d->buck_mv == WCD9XXX_CDC_BUCK_MV_1P8) {
  1146. wcd9xxx_enable_buck(codec, clsh_d, false);
  1147. snd_soc_update_bits(codec, WCD9XXX_A_NCP_STATIC,
  1148. 1 << 4, 1 << 4);
  1149. /* NCP settle time recommended by codec specification */
  1150. usleep_range(NCP_SETTLE_TIME_US,
  1151. NCP_SETTLE_TIME_US + 10);
  1152. } else {
  1153. /* NCP settle time recommended by codec specification */
  1154. usleep_range(NCP_SETTLE_TIME_US,
  1155. NCP_SETTLE_TIME_US + 10);
  1156. snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_5,
  1157. 0x01, (0x01 & 0x03));
  1158. snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_5,
  1159. 0xFC, (0xFC & 0xB));
  1160. }
  1161. snd_soc_update_bits(codec, WCD9XXX_A_BUCK_MODE_1, 0x04, 0x00);
  1162. } else {
  1163. dev_dbg(codec->dev, "%s: stub fallback to lineout\n", __func__);
  1164. wcd9xxx_set_fclk_put_ncp(codec, clsh_d, NCP_FCLK_LEVEL_5);
  1165. if (clsh_d->buck_mv != WCD9XXX_CDC_BUCK_MV_1P8)
  1166. wcd9xxx_enable_buck(codec, clsh_d, false);
  1167. }
  1168. }
  1169. static void wcd9xxx_clsh_state_err(struct snd_soc_codec *codec,
  1170. struct wcd9xxx_clsh_cdc_data *clsh_d,
  1171. u8 req_state, bool is_enable)
  1172. {
  1173. char msg[128];
  1174. dev_dbg(codec->dev,
  1175. "%s Wrong request for class H state machine requested to %s %s",
  1176. __func__, is_enable ? "enable" : "disable",
  1177. state_to_str(req_state, msg, sizeof(msg)));
  1178. WARN_ON(1);
  1179. }
  1180. /*
  1181. * Function: wcd9xxx_clsh_is_state_valid
  1182. * Params: state
  1183. * Description:
  1184. * Provides information on valid states of Class H configuration
  1185. */
  1186. static int wcd9xxx_clsh_is_state_valid(u8 state)
  1187. {
  1188. switch (state) {
  1189. case WCD9XXX_CLSH_STATE_IDLE:
  1190. case WCD9XXX_CLSH_STATE_EAR:
  1191. case WCD9XXX_CLSH_STATE_HPHL:
  1192. case WCD9XXX_CLSH_STATE_HPHR:
  1193. case WCD9XXX_CLSH_STATE_HPH_ST:
  1194. case WCD9XXX_CLSH_STATE_LO:
  1195. case WCD9XXX_CLSH_STATE_HPHL_EAR:
  1196. case WCD9XXX_CLSH_STATE_HPHR_EAR:
  1197. case WCD9XXX_CLSH_STATE_HPH_ST_EAR:
  1198. case WCD9XXX_CLSH_STATE_HPHL_LO:
  1199. case WCD9XXX_CLSH_STATE_HPHR_LO:
  1200. case WCD9XXX_CLSH_STATE_HPH_ST_LO:
  1201. case WCD9XXX_CLSH_STATE_EAR_LO:
  1202. case WCD9XXX_CLSH_STATE_HPHL_EAR_LO:
  1203. case WCD9XXX_CLSH_STATE_HPHR_EAR_LO:
  1204. case WCD9XXX_CLSH_STATE_HPH_ST_EAR_LO:
  1205. return 1;
  1206. default:
  1207. break;
  1208. }
  1209. return 0;
  1210. }
  1211. /*
  1212. * Function: wcd9xxx_clsh_fsm
  1213. * Params: codec, cdc_clsh_d, req_state, req_type, clsh_event
  1214. * Description:
  1215. * This function handles PRE DAC and POST DAC conditions of different devices
  1216. * and updates class H configuration of different combination of devices
  1217. * based on validity of their states. cdc_clsh_d will contain current
  1218. * class h state information
  1219. */
  1220. void wcd9xxx_clsh_fsm(struct snd_soc_codec *codec,
  1221. struct wcd9xxx_clsh_cdc_data *cdc_clsh_d,
  1222. u8 req_state, bool req_type, u8 clsh_event)
  1223. {
  1224. u8 old_state, new_state;
  1225. char msg0[128], msg1[128];
  1226. switch (clsh_event) {
  1227. case WCD9XXX_CLSH_EVENT_PRE_DAC:
  1228. /* PRE_DAC event should be used only for Enable */
  1229. BUG_ON(req_type != WCD9XXX_CLSH_REQ_ENABLE);
  1230. old_state = cdc_clsh_d->state;
  1231. new_state = old_state | req_state;
  1232. if (!wcd9xxx_clsh_is_state_valid(new_state)) {
  1233. dev_dbg(codec->dev,
  1234. "%s: classH not a valid new state: %s\n",
  1235. __func__,
  1236. state_to_str(new_state, msg0, sizeof(msg0)));
  1237. return;
  1238. }
  1239. if (new_state == old_state) {
  1240. dev_dbg(codec->dev,
  1241. "%s: classH already in requested state: %s\n",
  1242. __func__,
  1243. state_to_str(new_state, msg0, sizeof(msg0)));
  1244. return;
  1245. }
  1246. (*clsh_state_fp[new_state]) (codec, cdc_clsh_d, req_state,
  1247. req_type);
  1248. cdc_clsh_d->state = new_state;
  1249. dev_dbg(codec->dev,
  1250. "%s: ClassH state transition from %s to %s\n",
  1251. __func__, state_to_str(old_state, msg0, sizeof(msg0)),
  1252. state_to_str(cdc_clsh_d->state, msg1, sizeof(msg1)));
  1253. break;
  1254. case WCD9XXX_CLSH_EVENT_POST_PA:
  1255. if (req_type == WCD9XXX_CLSH_REQ_DISABLE) {
  1256. old_state = cdc_clsh_d->state;
  1257. new_state = old_state & (~req_state);
  1258. if (new_state < NUM_CLSH_STATES) {
  1259. if (!wcd9xxx_clsh_is_state_valid(old_state)) {
  1260. dev_dbg(codec->dev,
  1261. "%s:Invalid old state:%s\n",
  1262. __func__,
  1263. state_to_str(old_state, msg0,
  1264. sizeof(msg0)));
  1265. return;
  1266. }
  1267. if (new_state == old_state) {
  1268. dev_dbg(codec->dev,
  1269. "%s: clsH already in old state: %s\n",
  1270. __func__,
  1271. state_to_str(new_state, msg0,
  1272. sizeof(msg0)));
  1273. return;
  1274. }
  1275. (*clsh_state_fp[old_state]) (codec, cdc_clsh_d,
  1276. req_state,
  1277. req_type);
  1278. cdc_clsh_d->state = new_state;
  1279. dev_dbg(codec->dev, "%s: ClassH state transition from %s to %s\n",
  1280. __func__, state_to_str(old_state, msg0,
  1281. sizeof(msg0)),
  1282. state_to_str(cdc_clsh_d->state, msg1,
  1283. sizeof(msg1)));
  1284. } else {
  1285. dev_dbg(codec->dev, "%s:wrong new state=0x%x\n",
  1286. __func__, new_state);
  1287. }
  1288. } else if (!(cdc_clsh_d->state & WCD9XXX_CLSH_STATE_LO)) {
  1289. if (!cdc_clsh_d->hs_perf_mode_enabled)
  1290. wcd9xxx_clsh_enable_post_pa(codec, cdc_clsh_d);
  1291. }
  1292. break;
  1293. }
  1294. }
  1295. EXPORT_SYMBOL_GPL(wcd9xxx_clsh_fsm);
  1296. void wcd9xxx_clsh_init(struct wcd9xxx_clsh_cdc_data *clsh,
  1297. struct wcd9xxx_resmgr *resmgr)
  1298. {
  1299. int i;
  1300. clsh->state = WCD9XXX_CLSH_STATE_IDLE;
  1301. clsh->resmgr = resmgr;
  1302. for (i = 0; i < NUM_CLSH_STATES; i++)
  1303. clsh_state_fp[i] = wcd9xxx_clsh_state_err;
  1304. clsh_state_fp[WCD9XXX_CLSH_STATE_EAR] = wcd9xxx_clsh_state_ear;
  1305. clsh_state_fp[WCD9XXX_CLSH_STATE_HPHL] =
  1306. wcd9xxx_clsh_state_hph_l;
  1307. clsh_state_fp[WCD9XXX_CLSH_STATE_HPHR] =
  1308. wcd9xxx_clsh_state_hph_r;
  1309. clsh_state_fp[WCD9XXX_CLSH_STATE_HPH_ST] =
  1310. wcd9xxx_clsh_state_hph_st;
  1311. clsh_state_fp[WCD9XXX_CLSH_STATE_LO] = wcd9xxx_clsh_state_lo;
  1312. clsh_state_fp[WCD9XXX_CLSH_STATE_HPHL_EAR] =
  1313. wcd9xxx_clsh_state_hph_ear;
  1314. clsh_state_fp[WCD9XXX_CLSH_STATE_HPHR_EAR] =
  1315. wcd9xxx_clsh_state_hph_ear;
  1316. clsh_state_fp[WCD9XXX_CLSH_STATE_HPH_ST_EAR] =
  1317. wcd9xxx_clsh_state_hph_ear;
  1318. clsh_state_fp[WCD9XXX_CLSH_STATE_HPHL_LO] = wcd9xxx_clsh_state_hph_lo;
  1319. clsh_state_fp[WCD9XXX_CLSH_STATE_HPHR_LO] = wcd9xxx_clsh_state_hph_lo;
  1320. clsh_state_fp[WCD9XXX_CLSH_STATE_HPH_ST_LO] =
  1321. wcd9xxx_clsh_state_hph_lo;
  1322. clsh_state_fp[WCD9XXX_CLSH_STATE_EAR_LO] = wcd9xxx_clsh_state_ear_lo;
  1323. clsh_state_fp[WCD9XXX_CLSH_STATE_HPHL_EAR_LO] =
  1324. wcd9xxx_clsh_state_hph_ear_lo;
  1325. clsh_state_fp[WCD9XXX_CLSH_STATE_HPHR_EAR_LO] =
  1326. wcd9xxx_clsh_state_hph_ear_lo;
  1327. clsh_state_fp[WCD9XXX_CLSH_STATE_HPH_ST_EAR_LO] =
  1328. wcd9xxx_clsh_state_hph_ear_lo;
  1329. }
  1330. EXPORT_SYMBOL_GPL(wcd9xxx_clsh_init);
  1331. MODULE_DESCRIPTION("WCD9XXX Common");
  1332. MODULE_LICENSE("GPL v2");