da9055.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. /*
  2. * DA9055 ALSA Soc codec driver
  3. *
  4. * Copyright (c) 2012 Dialog Semiconductor
  5. *
  6. * Tested on (Samsung SMDK6410 board + DA9055 EVB) using I2S and I2C
  7. * Written by David Chen <david.chen@diasemi.com> and
  8. * Ashish Chavan <ashish.chavan@kpitcummins.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <sound/initval.h>
  26. #include <sound/tlv.h>
  27. #include <sound/da9055.h>
  28. /* DA9055 register space */
  29. /* Status Registers */
  30. #define DA9055_STATUS1 0x02
  31. #define DA9055_PLL_STATUS 0x03
  32. #define DA9055_AUX_L_GAIN_STATUS 0x04
  33. #define DA9055_AUX_R_GAIN_STATUS 0x05
  34. #define DA9055_MIC_L_GAIN_STATUS 0x06
  35. #define DA9055_MIC_R_GAIN_STATUS 0x07
  36. #define DA9055_MIXIN_L_GAIN_STATUS 0x08
  37. #define DA9055_MIXIN_R_GAIN_STATUS 0x09
  38. #define DA9055_ADC_L_GAIN_STATUS 0x0A
  39. #define DA9055_ADC_R_GAIN_STATUS 0x0B
  40. #define DA9055_DAC_L_GAIN_STATUS 0x0C
  41. #define DA9055_DAC_R_GAIN_STATUS 0x0D
  42. #define DA9055_HP_L_GAIN_STATUS 0x0E
  43. #define DA9055_HP_R_GAIN_STATUS 0x0F
  44. #define DA9055_LINE_GAIN_STATUS 0x10
  45. /* System Initialisation Registers */
  46. #define DA9055_CIF_CTRL 0x20
  47. #define DA9055_DIG_ROUTING_AIF 0X21
  48. #define DA9055_SR 0x22
  49. #define DA9055_REFERENCES 0x23
  50. #define DA9055_PLL_FRAC_TOP 0x24
  51. #define DA9055_PLL_FRAC_BOT 0x25
  52. #define DA9055_PLL_INTEGER 0x26
  53. #define DA9055_PLL_CTRL 0x27
  54. #define DA9055_AIF_CLK_MODE 0x28
  55. #define DA9055_AIF_CTRL 0x29
  56. #define DA9055_DIG_ROUTING_DAC 0x2A
  57. #define DA9055_ALC_CTRL1 0x2B
  58. /* Input - Gain, Select and Filter Registers */
  59. #define DA9055_AUX_L_GAIN 0x30
  60. #define DA9055_AUX_R_GAIN 0x31
  61. #define DA9055_MIXIN_L_SELECT 0x32
  62. #define DA9055_MIXIN_R_SELECT 0x33
  63. #define DA9055_MIXIN_L_GAIN 0x34
  64. #define DA9055_MIXIN_R_GAIN 0x35
  65. #define DA9055_ADC_L_GAIN 0x36
  66. #define DA9055_ADC_R_GAIN 0x37
  67. #define DA9055_ADC_FILTERS1 0x38
  68. #define DA9055_MIC_L_GAIN 0x39
  69. #define DA9055_MIC_R_GAIN 0x3A
  70. /* Output - Gain, Select and Filter Registers */
  71. #define DA9055_DAC_FILTERS5 0x40
  72. #define DA9055_DAC_FILTERS2 0x41
  73. #define DA9055_DAC_FILTERS3 0x42
  74. #define DA9055_DAC_FILTERS4 0x43
  75. #define DA9055_DAC_FILTERS1 0x44
  76. #define DA9055_DAC_L_GAIN 0x45
  77. #define DA9055_DAC_R_GAIN 0x46
  78. #define DA9055_CP_CTRL 0x47
  79. #define DA9055_HP_L_GAIN 0x48
  80. #define DA9055_HP_R_GAIN 0x49
  81. #define DA9055_LINE_GAIN 0x4A
  82. #define DA9055_MIXOUT_L_SELECT 0x4B
  83. #define DA9055_MIXOUT_R_SELECT 0x4C
  84. /* System Controller Registers */
  85. #define DA9055_SYSTEM_MODES_INPUT 0x50
  86. #define DA9055_SYSTEM_MODES_OUTPUT 0x51
  87. /* Control Registers */
  88. #define DA9055_AUX_L_CTRL 0x60
  89. #define DA9055_AUX_R_CTRL 0x61
  90. #define DA9055_MIC_BIAS_CTRL 0x62
  91. #define DA9055_MIC_L_CTRL 0x63
  92. #define DA9055_MIC_R_CTRL 0x64
  93. #define DA9055_MIXIN_L_CTRL 0x65
  94. #define DA9055_MIXIN_R_CTRL 0x66
  95. #define DA9055_ADC_L_CTRL 0x67
  96. #define DA9055_ADC_R_CTRL 0x68
  97. #define DA9055_DAC_L_CTRL 0x69
  98. #define DA9055_DAC_R_CTRL 0x6A
  99. #define DA9055_HP_L_CTRL 0x6B
  100. #define DA9055_HP_R_CTRL 0x6C
  101. #define DA9055_LINE_CTRL 0x6D
  102. #define DA9055_MIXOUT_L_CTRL 0x6E
  103. #define DA9055_MIXOUT_R_CTRL 0x6F
  104. /* Configuration Registers */
  105. #define DA9055_LDO_CTRL 0x90
  106. #define DA9055_IO_CTRL 0x91
  107. #define DA9055_GAIN_RAMP_CTRL 0x92
  108. #define DA9055_MIC_CONFIG 0x93
  109. #define DA9055_PC_COUNT 0x94
  110. #define DA9055_CP_VOL_THRESHOLD1 0x95
  111. #define DA9055_CP_DELAY 0x96
  112. #define DA9055_CP_DETECTOR 0x97
  113. #define DA9055_AIF_OFFSET 0x98
  114. #define DA9055_DIG_CTRL 0x99
  115. #define DA9055_ALC_CTRL2 0x9A
  116. #define DA9055_ALC_CTRL3 0x9B
  117. #define DA9055_ALC_NOISE 0x9C
  118. #define DA9055_ALC_TARGET_MIN 0x9D
  119. #define DA9055_ALC_TARGET_MAX 0x9E
  120. #define DA9055_ALC_GAIN_LIMITS 0x9F
  121. #define DA9055_ALC_ANA_GAIN_LIMITS 0xA0
  122. #define DA9055_ALC_ANTICLIP_CTRL 0xA1
  123. #define DA9055_ALC_ANTICLIP_LEVEL 0xA2
  124. #define DA9055_ALC_OFFSET_OP2M_L 0xA6
  125. #define DA9055_ALC_OFFSET_OP2U_L 0xA7
  126. #define DA9055_ALC_OFFSET_OP2M_R 0xAB
  127. #define DA9055_ALC_OFFSET_OP2U_R 0xAC
  128. #define DA9055_ALC_CIC_OP_LVL_CTRL 0xAD
  129. #define DA9055_ALC_CIC_OP_LVL_DATA 0xAE
  130. #define DA9055_DAC_NG_SETUP_TIME 0xAF
  131. #define DA9055_DAC_NG_OFF_THRESHOLD 0xB0
  132. #define DA9055_DAC_NG_ON_THRESHOLD 0xB1
  133. #define DA9055_DAC_NG_CTRL 0xB2
  134. /* SR bit fields */
  135. #define DA9055_SR_8000 (0x1 << 0)
  136. #define DA9055_SR_11025 (0x2 << 0)
  137. #define DA9055_SR_12000 (0x3 << 0)
  138. #define DA9055_SR_16000 (0x5 << 0)
  139. #define DA9055_SR_22050 (0x6 << 0)
  140. #define DA9055_SR_24000 (0x7 << 0)
  141. #define DA9055_SR_32000 (0x9 << 0)
  142. #define DA9055_SR_44100 (0xA << 0)
  143. #define DA9055_SR_48000 (0xB << 0)
  144. #define DA9055_SR_88200 (0xE << 0)
  145. #define DA9055_SR_96000 (0xF << 0)
  146. /* REFERENCES bit fields */
  147. #define DA9055_BIAS_EN (1 << 3)
  148. #define DA9055_VMID_EN (1 << 7)
  149. /* PLL_CTRL bit fields */
  150. #define DA9055_PLL_INDIV_10_20_MHZ (1 << 2)
  151. #define DA9055_PLL_SRM_EN (1 << 6)
  152. #define DA9055_PLL_EN (1 << 7)
  153. /* AIF_CLK_MODE bit fields */
  154. #define DA9055_AIF_BCLKS_PER_WCLK_32 (0 << 0)
  155. #define DA9055_AIF_BCLKS_PER_WCLK_64 (1 << 0)
  156. #define DA9055_AIF_BCLKS_PER_WCLK_128 (2 << 0)
  157. #define DA9055_AIF_BCLKS_PER_WCLK_256 (3 << 0)
  158. #define DA9055_AIF_CLK_EN_SLAVE_MODE (0 << 7)
  159. #define DA9055_AIF_CLK_EN_MASTER_MODE (1 << 7)
  160. /* AIF_CTRL bit fields */
  161. #define DA9055_AIF_FORMAT_I2S_MODE (0 << 0)
  162. #define DA9055_AIF_FORMAT_LEFT_J (1 << 0)
  163. #define DA9055_AIF_FORMAT_RIGHT_J (2 << 0)
  164. #define DA9055_AIF_FORMAT_DSP (3 << 0)
  165. #define DA9055_AIF_WORD_S16_LE (0 << 2)
  166. #define DA9055_AIF_WORD_S20_3LE (1 << 2)
  167. #define DA9055_AIF_WORD_S24_LE (2 << 2)
  168. #define DA9055_AIF_WORD_S32_LE (3 << 2)
  169. /* MIC_L_CTRL bit fields */
  170. #define DA9055_MIC_L_MUTE_EN (1 << 6)
  171. /* MIC_R_CTRL bit fields */
  172. #define DA9055_MIC_R_MUTE_EN (1 << 6)
  173. /* MIXIN_L_CTRL bit fields */
  174. #define DA9055_MIXIN_L_MIX_EN (1 << 3)
  175. /* MIXIN_R_CTRL bit fields */
  176. #define DA9055_MIXIN_R_MIX_EN (1 << 3)
  177. /* ADC_L_CTRL bit fields */
  178. #define DA9055_ADC_L_EN (1 << 7)
  179. /* ADC_R_CTRL bit fields */
  180. #define DA9055_ADC_R_EN (1 << 7)
  181. /* DAC_L_CTRL bit fields */
  182. #define DA9055_DAC_L_MUTE_EN (1 << 6)
  183. /* DAC_R_CTRL bit fields */
  184. #define DA9055_DAC_R_MUTE_EN (1 << 6)
  185. /* HP_L_CTRL bit fields */
  186. #define DA9055_HP_L_AMP_OE (1 << 3)
  187. /* HP_R_CTRL bit fields */
  188. #define DA9055_HP_R_AMP_OE (1 << 3)
  189. /* LINE_CTRL bit fields */
  190. #define DA9055_LINE_AMP_OE (1 << 3)
  191. /* MIXOUT_L_CTRL bit fields */
  192. #define DA9055_MIXOUT_L_MIX_EN (1 << 3)
  193. /* MIXOUT_R_CTRL bit fields */
  194. #define DA9055_MIXOUT_R_MIX_EN (1 << 3)
  195. /* MIC bias select bit fields */
  196. #define DA9055_MICBIAS2_EN (1 << 6)
  197. /* ALC_CIC_OP_LEVEL_CTRL bit fields */
  198. #define DA9055_ALC_DATA_MIDDLE (2 << 0)
  199. #define DA9055_ALC_DATA_TOP (3 << 0)
  200. #define DA9055_ALC_CIC_OP_CHANNEL_LEFT (0 << 7)
  201. #define DA9055_ALC_CIC_OP_CHANNEL_RIGHT (1 << 7)
  202. #define DA9055_AIF_BCLK_MASK (3 << 0)
  203. #define DA9055_AIF_CLK_MODE_MASK (1 << 7)
  204. #define DA9055_AIF_FORMAT_MASK (3 << 0)
  205. #define DA9055_AIF_WORD_LENGTH_MASK (3 << 2)
  206. #define DA9055_GAIN_RAMPING_EN (1 << 5)
  207. #define DA9055_MICBIAS_LEVEL_MASK (3 << 4)
  208. #define DA9055_ALC_OFFSET_15_8 0x00FF00
  209. #define DA9055_ALC_OFFSET_17_16 0x030000
  210. #define DA9055_ALC_AVG_ITERATIONS 5
  211. struct pll_div {
  212. int fref;
  213. int fout;
  214. u8 frac_top;
  215. u8 frac_bot;
  216. u8 integer;
  217. u8 mode; /* 0 = slave, 1 = master */
  218. };
  219. /* PLL divisor table */
  220. static const struct pll_div da9055_pll_div[] = {
  221. /* for MASTER mode, fs = 44.1Khz and its harmonics */
  222. {11289600, 2822400, 0x00, 0x00, 0x20, 1}, /* MCLK=11.2896Mhz */
  223. {12000000, 2822400, 0x03, 0x61, 0x1E, 1}, /* MCLK=12Mhz */
  224. {12288000, 2822400, 0x0C, 0xCC, 0x1D, 1}, /* MCLK=12.288Mhz */
  225. {13000000, 2822400, 0x19, 0x45, 0x1B, 1}, /* MCLK=13Mhz */
  226. {13500000, 2822400, 0x18, 0x56, 0x1A, 1}, /* MCLK=13.5Mhz */
  227. {14400000, 2822400, 0x02, 0xD0, 0x19, 1}, /* MCLK=14.4Mhz */
  228. {19200000, 2822400, 0x1A, 0x1C, 0x12, 1}, /* MCLK=19.2Mhz */
  229. {19680000, 2822400, 0x0B, 0x6D, 0x12, 1}, /* MCLK=19.68Mhz */
  230. {19800000, 2822400, 0x07, 0xDD, 0x12, 1}, /* MCLK=19.8Mhz */
  231. /* for MASTER mode, fs = 48Khz and its harmonics */
  232. {11289600, 3072000, 0x1A, 0x8E, 0x22, 1}, /* MCLK=11.2896Mhz */
  233. {12000000, 3072000, 0x18, 0x93, 0x20, 1}, /* MCLK=12Mhz */
  234. {12288000, 3072000, 0x00, 0x00, 0x20, 1}, /* MCLK=12.288Mhz */
  235. {13000000, 3072000, 0x07, 0xEA, 0x1E, 1}, /* MCLK=13Mhz */
  236. {13500000, 3072000, 0x04, 0x11, 0x1D, 1}, /* MCLK=13.5Mhz */
  237. {14400000, 3072000, 0x09, 0xD0, 0x1B, 1}, /* MCLK=14.4Mhz */
  238. {19200000, 3072000, 0x0F, 0x5C, 0x14, 1}, /* MCLK=19.2Mhz */
  239. {19680000, 3072000, 0x1F, 0x60, 0x13, 1}, /* MCLK=19.68Mhz */
  240. {19800000, 3072000, 0x1B, 0x80, 0x13, 1}, /* MCLK=19.8Mhz */
  241. /* for SLAVE mode with SRM */
  242. {11289600, 2822400, 0x0D, 0x47, 0x21, 0}, /* MCLK=11.2896Mhz */
  243. {12000000, 2822400, 0x0D, 0xFA, 0x1F, 0}, /* MCLK=12Mhz */
  244. {12288000, 2822400, 0x16, 0x66, 0x1E, 0}, /* MCLK=12.288Mhz */
  245. {13000000, 2822400, 0x00, 0x98, 0x1D, 0}, /* MCLK=13Mhz */
  246. {13500000, 2822400, 0x1E, 0x33, 0x1B, 0}, /* MCLK=13.5Mhz */
  247. {14400000, 2822400, 0x06, 0x50, 0x1A, 0}, /* MCLK=14.4Mhz */
  248. {19200000, 2822400, 0x14, 0xBC, 0x13, 0}, /* MCLK=19.2Mhz */
  249. {19680000, 2822400, 0x05, 0x66, 0x13, 0}, /* MCLK=19.68Mhz */
  250. {19800000, 2822400, 0x01, 0xAE, 0x13, 0}, /* MCLK=19.8Mhz */
  251. };
  252. enum clk_src {
  253. DA9055_CLKSRC_MCLK
  254. };
  255. /* Gain and Volume */
  256. static const DECLARE_TLV_DB_RANGE(aux_vol_tlv,
  257. 0x0, 0x10, TLV_DB_SCALE_ITEM(-5400, 0, 0),
  258. /* -54dB to 15dB */
  259. 0x11, 0x3f, TLV_DB_SCALE_ITEM(-5400, 150, 0)
  260. );
  261. static const DECLARE_TLV_DB_RANGE(digital_gain_tlv,
  262. 0x0, 0x07, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  263. /* -78dB to 12dB */
  264. 0x08, 0x7f, TLV_DB_SCALE_ITEM(-7800, 75, 0)
  265. );
  266. static const DECLARE_TLV_DB_RANGE(alc_analog_gain_tlv,
  267. 0x0, 0x0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 1),
  268. /* 0dB to 36dB */
  269. 0x01, 0x07, TLV_DB_SCALE_ITEM(0, 600, 0)
  270. );
  271. static const DECLARE_TLV_DB_SCALE(mic_vol_tlv, -600, 600, 0);
  272. static const DECLARE_TLV_DB_SCALE(mixin_gain_tlv, -450, 150, 0);
  273. static const DECLARE_TLV_DB_SCALE(eq_gain_tlv, -1050, 150, 0);
  274. static const DECLARE_TLV_DB_SCALE(hp_vol_tlv, -5700, 100, 0);
  275. static const DECLARE_TLV_DB_SCALE(lineout_vol_tlv, -4800, 100, 0);
  276. static const DECLARE_TLV_DB_SCALE(alc_threshold_tlv, -9450, 150, 0);
  277. static const DECLARE_TLV_DB_SCALE(alc_gain_tlv, 0, 600, 0);
  278. /* ADC and DAC high pass filter cutoff value */
  279. static const char * const da9055_hpf_cutoff_txt[] = {
  280. "Fs/24000", "Fs/12000", "Fs/6000", "Fs/3000"
  281. };
  282. static SOC_ENUM_SINGLE_DECL(da9055_dac_hpf_cutoff,
  283. DA9055_DAC_FILTERS1, 4, da9055_hpf_cutoff_txt);
  284. static SOC_ENUM_SINGLE_DECL(da9055_adc_hpf_cutoff,
  285. DA9055_ADC_FILTERS1, 4, da9055_hpf_cutoff_txt);
  286. /* ADC and DAC voice mode (8kHz) high pass cutoff value */
  287. static const char * const da9055_vf_cutoff_txt[] = {
  288. "2.5Hz", "25Hz", "50Hz", "100Hz", "150Hz", "200Hz", "300Hz", "400Hz"
  289. };
  290. static SOC_ENUM_SINGLE_DECL(da9055_dac_vf_cutoff,
  291. DA9055_DAC_FILTERS1, 0, da9055_vf_cutoff_txt);
  292. static SOC_ENUM_SINGLE_DECL(da9055_adc_vf_cutoff,
  293. DA9055_ADC_FILTERS1, 0, da9055_vf_cutoff_txt);
  294. /* Gain ramping rate value */
  295. static const char * const da9055_gain_ramping_txt[] = {
  296. "nominal rate", "nominal rate * 4", "nominal rate * 8",
  297. "nominal rate / 8"
  298. };
  299. static SOC_ENUM_SINGLE_DECL(da9055_gain_ramping_rate,
  300. DA9055_GAIN_RAMP_CTRL, 0, da9055_gain_ramping_txt);
  301. /* DAC noise gate setup time value */
  302. static const char * const da9055_dac_ng_setup_time_txt[] = {
  303. "256 samples", "512 samples", "1024 samples", "2048 samples"
  304. };
  305. static SOC_ENUM_SINGLE_DECL(da9055_dac_ng_setup_time,
  306. DA9055_DAC_NG_SETUP_TIME, 0,
  307. da9055_dac_ng_setup_time_txt);
  308. /* DAC noise gate rampup rate value */
  309. static const char * const da9055_dac_ng_rampup_txt[] = {
  310. "0.02 ms/dB", "0.16 ms/dB"
  311. };
  312. static SOC_ENUM_SINGLE_DECL(da9055_dac_ng_rampup_rate,
  313. DA9055_DAC_NG_SETUP_TIME, 2,
  314. da9055_dac_ng_rampup_txt);
  315. /* DAC noise gate rampdown rate value */
  316. static const char * const da9055_dac_ng_rampdown_txt[] = {
  317. "0.64 ms/dB", "20.48 ms/dB"
  318. };
  319. static SOC_ENUM_SINGLE_DECL(da9055_dac_ng_rampdown_rate,
  320. DA9055_DAC_NG_SETUP_TIME, 3,
  321. da9055_dac_ng_rampdown_txt);
  322. /* DAC soft mute rate value */
  323. static const char * const da9055_dac_soft_mute_rate_txt[] = {
  324. "1", "2", "4", "8", "16", "32", "64"
  325. };
  326. static SOC_ENUM_SINGLE_DECL(da9055_dac_soft_mute_rate,
  327. DA9055_DAC_FILTERS5, 4,
  328. da9055_dac_soft_mute_rate_txt);
  329. /* DAC routing select */
  330. static const char * const da9055_dac_src_txt[] = {
  331. "ADC output left", "ADC output right", "AIF input left",
  332. "AIF input right"
  333. };
  334. static SOC_ENUM_SINGLE_DECL(da9055_dac_l_src,
  335. DA9055_DIG_ROUTING_DAC, 0, da9055_dac_src_txt);
  336. static SOC_ENUM_SINGLE_DECL(da9055_dac_r_src,
  337. DA9055_DIG_ROUTING_DAC, 4, da9055_dac_src_txt);
  338. /* MIC PGA Left source select */
  339. static const char * const da9055_mic_l_src_txt[] = {
  340. "MIC1_P_N", "MIC1_P", "MIC1_N", "MIC2_L"
  341. };
  342. static SOC_ENUM_SINGLE_DECL(da9055_mic_l_src,
  343. DA9055_MIXIN_L_SELECT, 4, da9055_mic_l_src_txt);
  344. /* MIC PGA Right source select */
  345. static const char * const da9055_mic_r_src_txt[] = {
  346. "MIC2_R_L", "MIC2_R", "MIC2_L"
  347. };
  348. static SOC_ENUM_SINGLE_DECL(da9055_mic_r_src,
  349. DA9055_MIXIN_R_SELECT, 4, da9055_mic_r_src_txt);
  350. /* ALC Input Signal Tracking rate select */
  351. static const char * const da9055_signal_tracking_rate_txt[] = {
  352. "1/4", "1/16", "1/256", "1/65536"
  353. };
  354. static SOC_ENUM_SINGLE_DECL(da9055_integ_attack_rate,
  355. DA9055_ALC_CTRL3, 4,
  356. da9055_signal_tracking_rate_txt);
  357. static SOC_ENUM_SINGLE_DECL(da9055_integ_release_rate,
  358. DA9055_ALC_CTRL3, 6,
  359. da9055_signal_tracking_rate_txt);
  360. /* ALC Attack Rate select */
  361. static const char * const da9055_attack_rate_txt[] = {
  362. "44/fs", "88/fs", "176/fs", "352/fs", "704/fs", "1408/fs", "2816/fs",
  363. "5632/fs", "11264/fs", "22528/fs", "45056/fs", "90112/fs", "180224/fs"
  364. };
  365. static SOC_ENUM_SINGLE_DECL(da9055_attack_rate,
  366. DA9055_ALC_CTRL2, 0, da9055_attack_rate_txt);
  367. /* ALC Release Rate select */
  368. static const char * const da9055_release_rate_txt[] = {
  369. "176/fs", "352/fs", "704/fs", "1408/fs", "2816/fs", "5632/fs",
  370. "11264/fs", "22528/fs", "45056/fs", "90112/fs", "180224/fs"
  371. };
  372. static SOC_ENUM_SINGLE_DECL(da9055_release_rate,
  373. DA9055_ALC_CTRL2, 4, da9055_release_rate_txt);
  374. /* ALC Hold Time select */
  375. static const char * const da9055_hold_time_txt[] = {
  376. "62/fs", "124/fs", "248/fs", "496/fs", "992/fs", "1984/fs", "3968/fs",
  377. "7936/fs", "15872/fs", "31744/fs", "63488/fs", "126976/fs",
  378. "253952/fs", "507904/fs", "1015808/fs", "2031616/fs"
  379. };
  380. static SOC_ENUM_SINGLE_DECL(da9055_hold_time,
  381. DA9055_ALC_CTRL3, 0, da9055_hold_time_txt);
  382. static int da9055_get_alc_data(struct snd_soc_codec *codec, u8 reg_val)
  383. {
  384. int mid_data, top_data;
  385. int sum = 0;
  386. u8 iteration;
  387. for (iteration = 0; iteration < DA9055_ALC_AVG_ITERATIONS;
  388. iteration++) {
  389. /* Select the left or right channel and capture data */
  390. snd_soc_write(codec, DA9055_ALC_CIC_OP_LVL_CTRL, reg_val);
  391. /* Select middle 8 bits for read back from data register */
  392. snd_soc_write(codec, DA9055_ALC_CIC_OP_LVL_CTRL,
  393. reg_val | DA9055_ALC_DATA_MIDDLE);
  394. mid_data = snd_soc_read(codec, DA9055_ALC_CIC_OP_LVL_DATA);
  395. /* Select top 8 bits for read back from data register */
  396. snd_soc_write(codec, DA9055_ALC_CIC_OP_LVL_CTRL,
  397. reg_val | DA9055_ALC_DATA_TOP);
  398. top_data = snd_soc_read(codec, DA9055_ALC_CIC_OP_LVL_DATA);
  399. sum += ((mid_data << 8) | (top_data << 16));
  400. }
  401. return sum / DA9055_ALC_AVG_ITERATIONS;
  402. }
  403. static int da9055_put_alc_sw(struct snd_kcontrol *kcontrol,
  404. struct snd_ctl_elem_value *ucontrol)
  405. {
  406. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  407. u8 reg_val, adc_left, adc_right, mic_left, mic_right;
  408. int avg_left_data, avg_right_data, offset_l, offset_r;
  409. if (ucontrol->value.integer.value[0]) {
  410. /*
  411. * While enabling ALC (or ALC sync mode), calibration of the DC
  412. * offsets must be done first
  413. */
  414. /* Save current values from Mic control registers */
  415. mic_left = snd_soc_read(codec, DA9055_MIC_L_CTRL);
  416. mic_right = snd_soc_read(codec, DA9055_MIC_R_CTRL);
  417. /* Mute Mic PGA Left and Right */
  418. snd_soc_update_bits(codec, DA9055_MIC_L_CTRL,
  419. DA9055_MIC_L_MUTE_EN, DA9055_MIC_L_MUTE_EN);
  420. snd_soc_update_bits(codec, DA9055_MIC_R_CTRL,
  421. DA9055_MIC_R_MUTE_EN, DA9055_MIC_R_MUTE_EN);
  422. /* Save current values from ADC control registers */
  423. adc_left = snd_soc_read(codec, DA9055_ADC_L_CTRL);
  424. adc_right = snd_soc_read(codec, DA9055_ADC_R_CTRL);
  425. /* Enable ADC Left and Right */
  426. snd_soc_update_bits(codec, DA9055_ADC_L_CTRL,
  427. DA9055_ADC_L_EN, DA9055_ADC_L_EN);
  428. snd_soc_update_bits(codec, DA9055_ADC_R_CTRL,
  429. DA9055_ADC_R_EN, DA9055_ADC_R_EN);
  430. /* Calculate average for Left and Right data */
  431. /* Left Data */
  432. avg_left_data = da9055_get_alc_data(codec,
  433. DA9055_ALC_CIC_OP_CHANNEL_LEFT);
  434. /* Right Data */
  435. avg_right_data = da9055_get_alc_data(codec,
  436. DA9055_ALC_CIC_OP_CHANNEL_RIGHT);
  437. /* Calculate DC offset */
  438. offset_l = -avg_left_data;
  439. offset_r = -avg_right_data;
  440. reg_val = (offset_l & DA9055_ALC_OFFSET_15_8) >> 8;
  441. snd_soc_write(codec, DA9055_ALC_OFFSET_OP2M_L, reg_val);
  442. reg_val = (offset_l & DA9055_ALC_OFFSET_17_16) >> 16;
  443. snd_soc_write(codec, DA9055_ALC_OFFSET_OP2U_L, reg_val);
  444. reg_val = (offset_r & DA9055_ALC_OFFSET_15_8) >> 8;
  445. snd_soc_write(codec, DA9055_ALC_OFFSET_OP2M_R, reg_val);
  446. reg_val = (offset_r & DA9055_ALC_OFFSET_17_16) >> 16;
  447. snd_soc_write(codec, DA9055_ALC_OFFSET_OP2U_R, reg_val);
  448. /* Restore original values of ADC control registers */
  449. snd_soc_write(codec, DA9055_ADC_L_CTRL, adc_left);
  450. snd_soc_write(codec, DA9055_ADC_R_CTRL, adc_right);
  451. /* Restore original values of Mic control registers */
  452. snd_soc_write(codec, DA9055_MIC_L_CTRL, mic_left);
  453. snd_soc_write(codec, DA9055_MIC_R_CTRL, mic_right);
  454. }
  455. return snd_soc_put_volsw(kcontrol, ucontrol);
  456. }
  457. static const struct snd_kcontrol_new da9055_snd_controls[] = {
  458. /* Volume controls */
  459. SOC_DOUBLE_R_TLV("Mic Volume",
  460. DA9055_MIC_L_GAIN, DA9055_MIC_R_GAIN,
  461. 0, 0x7, 0, mic_vol_tlv),
  462. SOC_DOUBLE_R_TLV("Aux Volume",
  463. DA9055_AUX_L_GAIN, DA9055_AUX_R_GAIN,
  464. 0, 0x3f, 0, aux_vol_tlv),
  465. SOC_DOUBLE_R_TLV("Mixin PGA Volume",
  466. DA9055_MIXIN_L_GAIN, DA9055_MIXIN_R_GAIN,
  467. 0, 0xf, 0, mixin_gain_tlv),
  468. SOC_DOUBLE_R_TLV("ADC Volume",
  469. DA9055_ADC_L_GAIN, DA9055_ADC_R_GAIN,
  470. 0, 0x7f, 0, digital_gain_tlv),
  471. SOC_DOUBLE_R_TLV("DAC Volume",
  472. DA9055_DAC_L_GAIN, DA9055_DAC_R_GAIN,
  473. 0, 0x7f, 0, digital_gain_tlv),
  474. SOC_DOUBLE_R_TLV("Headphone Volume",
  475. DA9055_HP_L_GAIN, DA9055_HP_R_GAIN,
  476. 0, 0x3f, 0, hp_vol_tlv),
  477. SOC_SINGLE_TLV("Lineout Volume", DA9055_LINE_GAIN, 0, 0x3f, 0,
  478. lineout_vol_tlv),
  479. /* DAC Equalizer controls */
  480. SOC_SINGLE("DAC EQ Switch", DA9055_DAC_FILTERS4, 7, 1, 0),
  481. SOC_SINGLE_TLV("DAC EQ1 Volume", DA9055_DAC_FILTERS2, 0, 0xf, 0,
  482. eq_gain_tlv),
  483. SOC_SINGLE_TLV("DAC EQ2 Volume", DA9055_DAC_FILTERS2, 4, 0xf, 0,
  484. eq_gain_tlv),
  485. SOC_SINGLE_TLV("DAC EQ3 Volume", DA9055_DAC_FILTERS3, 0, 0xf, 0,
  486. eq_gain_tlv),
  487. SOC_SINGLE_TLV("DAC EQ4 Volume", DA9055_DAC_FILTERS3, 4, 0xf, 0,
  488. eq_gain_tlv),
  489. SOC_SINGLE_TLV("DAC EQ5 Volume", DA9055_DAC_FILTERS4, 0, 0xf, 0,
  490. eq_gain_tlv),
  491. /* High Pass Filter and Voice Mode controls */
  492. SOC_SINGLE("ADC HPF Switch", DA9055_ADC_FILTERS1, 7, 1, 0),
  493. SOC_ENUM("ADC HPF Cutoff", da9055_adc_hpf_cutoff),
  494. SOC_SINGLE("ADC Voice Mode Switch", DA9055_ADC_FILTERS1, 3, 1, 0),
  495. SOC_ENUM("ADC Voice Cutoff", da9055_adc_vf_cutoff),
  496. SOC_SINGLE("DAC HPF Switch", DA9055_DAC_FILTERS1, 7, 1, 0),
  497. SOC_ENUM("DAC HPF Cutoff", da9055_dac_hpf_cutoff),
  498. SOC_SINGLE("DAC Voice Mode Switch", DA9055_DAC_FILTERS1, 3, 1, 0),
  499. SOC_ENUM("DAC Voice Cutoff", da9055_dac_vf_cutoff),
  500. /* Mute controls */
  501. SOC_DOUBLE_R("Mic Switch", DA9055_MIC_L_CTRL,
  502. DA9055_MIC_R_CTRL, 6, 1, 0),
  503. SOC_DOUBLE_R("Aux Switch", DA9055_AUX_L_CTRL,
  504. DA9055_AUX_R_CTRL, 6, 1, 0),
  505. SOC_DOUBLE_R("Mixin PGA Switch", DA9055_MIXIN_L_CTRL,
  506. DA9055_MIXIN_R_CTRL, 6, 1, 0),
  507. SOC_DOUBLE_R("ADC Switch", DA9055_ADC_L_CTRL,
  508. DA9055_ADC_R_CTRL, 6, 1, 0),
  509. SOC_DOUBLE_R("Headphone Switch", DA9055_HP_L_CTRL,
  510. DA9055_HP_R_CTRL, 6, 1, 0),
  511. SOC_SINGLE("Lineout Switch", DA9055_LINE_CTRL, 6, 1, 0),
  512. SOC_SINGLE("DAC Soft Mute Switch", DA9055_DAC_FILTERS5, 7, 1, 0),
  513. SOC_ENUM("DAC Soft Mute Rate", da9055_dac_soft_mute_rate),
  514. /* Zero Cross controls */
  515. SOC_DOUBLE_R("Aux ZC Switch", DA9055_AUX_L_CTRL,
  516. DA9055_AUX_R_CTRL, 4, 1, 0),
  517. SOC_DOUBLE_R("Mixin PGA ZC Switch", DA9055_MIXIN_L_CTRL,
  518. DA9055_MIXIN_R_CTRL, 4, 1, 0),
  519. SOC_DOUBLE_R("Headphone ZC Switch", DA9055_HP_L_CTRL,
  520. DA9055_HP_R_CTRL, 4, 1, 0),
  521. SOC_SINGLE("Lineout ZC Switch", DA9055_LINE_CTRL, 4, 1, 0),
  522. /* Gain Ramping controls */
  523. SOC_DOUBLE_R("Aux Gain Ramping Switch", DA9055_AUX_L_CTRL,
  524. DA9055_AUX_R_CTRL, 5, 1, 0),
  525. SOC_DOUBLE_R("Mixin Gain Ramping Switch", DA9055_MIXIN_L_CTRL,
  526. DA9055_MIXIN_R_CTRL, 5, 1, 0),
  527. SOC_DOUBLE_R("ADC Gain Ramping Switch", DA9055_ADC_L_CTRL,
  528. DA9055_ADC_R_CTRL, 5, 1, 0),
  529. SOC_DOUBLE_R("DAC Gain Ramping Switch", DA9055_DAC_L_CTRL,
  530. DA9055_DAC_R_CTRL, 5, 1, 0),
  531. SOC_DOUBLE_R("Headphone Gain Ramping Switch", DA9055_HP_L_CTRL,
  532. DA9055_HP_R_CTRL, 5, 1, 0),
  533. SOC_SINGLE("Lineout Gain Ramping Switch", DA9055_LINE_CTRL, 5, 1, 0),
  534. SOC_ENUM("Gain Ramping Rate", da9055_gain_ramping_rate),
  535. /* DAC Noise Gate controls */
  536. SOC_SINGLE("DAC NG Switch", DA9055_DAC_NG_CTRL, 7, 1, 0),
  537. SOC_SINGLE("DAC NG ON Threshold", DA9055_DAC_NG_ON_THRESHOLD,
  538. 0, 0x7, 0),
  539. SOC_SINGLE("DAC NG OFF Threshold", DA9055_DAC_NG_OFF_THRESHOLD,
  540. 0, 0x7, 0),
  541. SOC_ENUM("DAC NG Setup Time", da9055_dac_ng_setup_time),
  542. SOC_ENUM("DAC NG Rampup Rate", da9055_dac_ng_rampup_rate),
  543. SOC_ENUM("DAC NG Rampdown Rate", da9055_dac_ng_rampdown_rate),
  544. /* DAC Invertion control */
  545. SOC_SINGLE("DAC Left Invert", DA9055_DIG_CTRL, 3, 1, 0),
  546. SOC_SINGLE("DAC Right Invert", DA9055_DIG_CTRL, 7, 1, 0),
  547. /* DMIC controls */
  548. SOC_DOUBLE_R("DMIC Switch", DA9055_MIXIN_L_SELECT,
  549. DA9055_MIXIN_R_SELECT, 7, 1, 0),
  550. /* ALC Controls */
  551. SOC_DOUBLE_EXT("ALC Switch", DA9055_ALC_CTRL1, 3, 7, 1, 0,
  552. snd_soc_get_volsw, da9055_put_alc_sw),
  553. SOC_SINGLE_EXT("ALC Sync Mode Switch", DA9055_ALC_CTRL1, 1, 1, 0,
  554. snd_soc_get_volsw, da9055_put_alc_sw),
  555. SOC_SINGLE("ALC Offset Switch", DA9055_ALC_CTRL1, 0, 1, 0),
  556. SOC_SINGLE("ALC Anticlip Mode Switch", DA9055_ALC_ANTICLIP_CTRL,
  557. 7, 1, 0),
  558. SOC_SINGLE("ALC Anticlip Level", DA9055_ALC_ANTICLIP_LEVEL,
  559. 0, 0x7f, 0),
  560. SOC_SINGLE_TLV("ALC Min Threshold Volume", DA9055_ALC_TARGET_MIN,
  561. 0, 0x3f, 1, alc_threshold_tlv),
  562. SOC_SINGLE_TLV("ALC Max Threshold Volume", DA9055_ALC_TARGET_MAX,
  563. 0, 0x3f, 1, alc_threshold_tlv),
  564. SOC_SINGLE_TLV("ALC Noise Threshold Volume", DA9055_ALC_NOISE,
  565. 0, 0x3f, 1, alc_threshold_tlv),
  566. SOC_SINGLE_TLV("ALC Max Gain Volume", DA9055_ALC_GAIN_LIMITS,
  567. 4, 0xf, 0, alc_gain_tlv),
  568. SOC_SINGLE_TLV("ALC Max Attenuation Volume", DA9055_ALC_GAIN_LIMITS,
  569. 0, 0xf, 0, alc_gain_tlv),
  570. SOC_SINGLE_TLV("ALC Min Analog Gain Volume",
  571. DA9055_ALC_ANA_GAIN_LIMITS,
  572. 0, 0x7, 0, alc_analog_gain_tlv),
  573. SOC_SINGLE_TLV("ALC Max Analog Gain Volume",
  574. DA9055_ALC_ANA_GAIN_LIMITS,
  575. 4, 0x7, 0, alc_analog_gain_tlv),
  576. SOC_ENUM("ALC Attack Rate", da9055_attack_rate),
  577. SOC_ENUM("ALC Release Rate", da9055_release_rate),
  578. SOC_ENUM("ALC Hold Time", da9055_hold_time),
  579. /*
  580. * Rate at which input signal envelope is tracked as the signal gets
  581. * larger
  582. */
  583. SOC_ENUM("ALC Integ Attack Rate", da9055_integ_attack_rate),
  584. /*
  585. * Rate at which input signal envelope is tracked as the signal gets
  586. * smaller
  587. */
  588. SOC_ENUM("ALC Integ Release Rate", da9055_integ_release_rate),
  589. };
  590. /* DAPM Controls */
  591. /* Mic PGA Left Source */
  592. static const struct snd_kcontrol_new da9055_mic_l_mux_controls =
  593. SOC_DAPM_ENUM("Route", da9055_mic_l_src);
  594. /* Mic PGA Right Source */
  595. static const struct snd_kcontrol_new da9055_mic_r_mux_controls =
  596. SOC_DAPM_ENUM("Route", da9055_mic_r_src);
  597. /* In Mixer Left */
  598. static const struct snd_kcontrol_new da9055_dapm_mixinl_controls[] = {
  599. SOC_DAPM_SINGLE("Aux Left Switch", DA9055_MIXIN_L_SELECT, 0, 1, 0),
  600. SOC_DAPM_SINGLE("Mic Left Switch", DA9055_MIXIN_L_SELECT, 1, 1, 0),
  601. SOC_DAPM_SINGLE("Mic Right Switch", DA9055_MIXIN_L_SELECT, 2, 1, 0),
  602. };
  603. /* In Mixer Right */
  604. static const struct snd_kcontrol_new da9055_dapm_mixinr_controls[] = {
  605. SOC_DAPM_SINGLE("Aux Right Switch", DA9055_MIXIN_R_SELECT, 0, 1, 0),
  606. SOC_DAPM_SINGLE("Mic Right Switch", DA9055_MIXIN_R_SELECT, 1, 1, 0),
  607. SOC_DAPM_SINGLE("Mic Left Switch", DA9055_MIXIN_R_SELECT, 2, 1, 0),
  608. SOC_DAPM_SINGLE("Mixin Left Switch", DA9055_MIXIN_R_SELECT, 3, 1, 0),
  609. };
  610. /* DAC Left Source */
  611. static const struct snd_kcontrol_new da9055_dac_l_mux_controls =
  612. SOC_DAPM_ENUM("Route", da9055_dac_l_src);
  613. /* DAC Right Source */
  614. static const struct snd_kcontrol_new da9055_dac_r_mux_controls =
  615. SOC_DAPM_ENUM("Route", da9055_dac_r_src);
  616. /* Out Mixer Left */
  617. static const struct snd_kcontrol_new da9055_dapm_mixoutl_controls[] = {
  618. SOC_DAPM_SINGLE("Aux Left Switch", DA9055_MIXOUT_L_SELECT, 0, 1, 0),
  619. SOC_DAPM_SINGLE("Mixin Left Switch", DA9055_MIXOUT_L_SELECT, 1, 1, 0),
  620. SOC_DAPM_SINGLE("Mixin Right Switch", DA9055_MIXOUT_L_SELECT, 2, 1, 0),
  621. SOC_DAPM_SINGLE("DAC Left Switch", DA9055_MIXOUT_L_SELECT, 3, 1, 0),
  622. SOC_DAPM_SINGLE("Aux Left Invert Switch", DA9055_MIXOUT_L_SELECT,
  623. 4, 1, 0),
  624. SOC_DAPM_SINGLE("Mixin Left Invert Switch", DA9055_MIXOUT_L_SELECT,
  625. 5, 1, 0),
  626. SOC_DAPM_SINGLE("Mixin Right Invert Switch", DA9055_MIXOUT_L_SELECT,
  627. 6, 1, 0),
  628. };
  629. /* Out Mixer Right */
  630. static const struct snd_kcontrol_new da9055_dapm_mixoutr_controls[] = {
  631. SOC_DAPM_SINGLE("Aux Right Switch", DA9055_MIXOUT_R_SELECT, 0, 1, 0),
  632. SOC_DAPM_SINGLE("Mixin Right Switch", DA9055_MIXOUT_R_SELECT, 1, 1, 0),
  633. SOC_DAPM_SINGLE("Mixin Left Switch", DA9055_MIXOUT_R_SELECT, 2, 1, 0),
  634. SOC_DAPM_SINGLE("DAC Right Switch", DA9055_MIXOUT_R_SELECT, 3, 1, 0),
  635. SOC_DAPM_SINGLE("Aux Right Invert Switch", DA9055_MIXOUT_R_SELECT,
  636. 4, 1, 0),
  637. SOC_DAPM_SINGLE("Mixin Right Invert Switch", DA9055_MIXOUT_R_SELECT,
  638. 5, 1, 0),
  639. SOC_DAPM_SINGLE("Mixin Left Invert Switch", DA9055_MIXOUT_R_SELECT,
  640. 6, 1, 0),
  641. };
  642. /* Headphone Output Enable */
  643. static const struct snd_kcontrol_new da9055_dapm_hp_l_control =
  644. SOC_DAPM_SINGLE("Switch", DA9055_HP_L_CTRL, 3, 1, 0);
  645. static const struct snd_kcontrol_new da9055_dapm_hp_r_control =
  646. SOC_DAPM_SINGLE("Switch", DA9055_HP_R_CTRL, 3, 1, 0);
  647. /* Lineout Output Enable */
  648. static const struct snd_kcontrol_new da9055_dapm_lineout_control =
  649. SOC_DAPM_SINGLE("Switch", DA9055_LINE_CTRL, 3, 1, 0);
  650. /* DAPM widgets */
  651. static const struct snd_soc_dapm_widget da9055_dapm_widgets[] = {
  652. /* Input Side */
  653. /* Input Lines */
  654. SND_SOC_DAPM_INPUT("MIC1"),
  655. SND_SOC_DAPM_INPUT("MIC2"),
  656. SND_SOC_DAPM_INPUT("AUXL"),
  657. SND_SOC_DAPM_INPUT("AUXR"),
  658. /* MUXs for Mic PGA source selection */
  659. SND_SOC_DAPM_MUX("Mic Left Source", SND_SOC_NOPM, 0, 0,
  660. &da9055_mic_l_mux_controls),
  661. SND_SOC_DAPM_MUX("Mic Right Source", SND_SOC_NOPM, 0, 0,
  662. &da9055_mic_r_mux_controls),
  663. /* Input PGAs */
  664. SND_SOC_DAPM_PGA("Mic Left", DA9055_MIC_L_CTRL, 7, 0, NULL, 0),
  665. SND_SOC_DAPM_PGA("Mic Right", DA9055_MIC_R_CTRL, 7, 0, NULL, 0),
  666. SND_SOC_DAPM_PGA("Aux Left", DA9055_AUX_L_CTRL, 7, 0, NULL, 0),
  667. SND_SOC_DAPM_PGA("Aux Right", DA9055_AUX_R_CTRL, 7, 0, NULL, 0),
  668. SND_SOC_DAPM_PGA("MIXIN Left", DA9055_MIXIN_L_CTRL, 7, 0, NULL, 0),
  669. SND_SOC_DAPM_PGA("MIXIN Right", DA9055_MIXIN_R_CTRL, 7, 0, NULL, 0),
  670. SND_SOC_DAPM_SUPPLY("Mic Bias", DA9055_MIC_BIAS_CTRL, 7, 0, NULL, 0),
  671. SND_SOC_DAPM_SUPPLY("AIF", DA9055_AIF_CTRL, 7, 0, NULL, 0),
  672. SND_SOC_DAPM_SUPPLY("Charge Pump", DA9055_CP_CTRL, 7, 0, NULL, 0),
  673. /* Input Mixers */
  674. SND_SOC_DAPM_MIXER("In Mixer Left", SND_SOC_NOPM, 0, 0,
  675. &da9055_dapm_mixinl_controls[0],
  676. ARRAY_SIZE(da9055_dapm_mixinl_controls)),
  677. SND_SOC_DAPM_MIXER("In Mixer Right", SND_SOC_NOPM, 0, 0,
  678. &da9055_dapm_mixinr_controls[0],
  679. ARRAY_SIZE(da9055_dapm_mixinr_controls)),
  680. /* ADCs */
  681. SND_SOC_DAPM_ADC("ADC Left", "Capture", DA9055_ADC_L_CTRL, 7, 0),
  682. SND_SOC_DAPM_ADC("ADC Right", "Capture", DA9055_ADC_R_CTRL, 7, 0),
  683. /* Output Side */
  684. /* MUXs for DAC source selection */
  685. SND_SOC_DAPM_MUX("DAC Left Source", SND_SOC_NOPM, 0, 0,
  686. &da9055_dac_l_mux_controls),
  687. SND_SOC_DAPM_MUX("DAC Right Source", SND_SOC_NOPM, 0, 0,
  688. &da9055_dac_r_mux_controls),
  689. /* AIF input */
  690. SND_SOC_DAPM_AIF_IN("AIFIN Left", "Playback", 0, SND_SOC_NOPM, 0, 0),
  691. SND_SOC_DAPM_AIF_IN("AIFIN Right", "Playback", 0, SND_SOC_NOPM, 0, 0),
  692. /* DACs */
  693. SND_SOC_DAPM_DAC("DAC Left", "Playback", DA9055_DAC_L_CTRL, 7, 0),
  694. SND_SOC_DAPM_DAC("DAC Right", "Playback", DA9055_DAC_R_CTRL, 7, 0),
  695. /* Output Mixers */
  696. SND_SOC_DAPM_MIXER("Out Mixer Left", SND_SOC_NOPM, 0, 0,
  697. &da9055_dapm_mixoutl_controls[0],
  698. ARRAY_SIZE(da9055_dapm_mixoutl_controls)),
  699. SND_SOC_DAPM_MIXER("Out Mixer Right", SND_SOC_NOPM, 0, 0,
  700. &da9055_dapm_mixoutr_controls[0],
  701. ARRAY_SIZE(da9055_dapm_mixoutr_controls)),
  702. /* Output Enable Switches */
  703. SND_SOC_DAPM_SWITCH("Headphone Left Enable", SND_SOC_NOPM, 0, 0,
  704. &da9055_dapm_hp_l_control),
  705. SND_SOC_DAPM_SWITCH("Headphone Right Enable", SND_SOC_NOPM, 0, 0,
  706. &da9055_dapm_hp_r_control),
  707. SND_SOC_DAPM_SWITCH("Lineout Enable", SND_SOC_NOPM, 0, 0,
  708. &da9055_dapm_lineout_control),
  709. /* Output PGAs */
  710. SND_SOC_DAPM_PGA("MIXOUT Left", DA9055_MIXOUT_L_CTRL, 7, 0, NULL, 0),
  711. SND_SOC_DAPM_PGA("MIXOUT Right", DA9055_MIXOUT_R_CTRL, 7, 0, NULL, 0),
  712. SND_SOC_DAPM_PGA("Lineout", DA9055_LINE_CTRL, 7, 0, NULL, 0),
  713. SND_SOC_DAPM_PGA("Headphone Left", DA9055_HP_L_CTRL, 7, 0, NULL, 0),
  714. SND_SOC_DAPM_PGA("Headphone Right", DA9055_HP_R_CTRL, 7, 0, NULL, 0),
  715. /* Output Lines */
  716. SND_SOC_DAPM_OUTPUT("HPL"),
  717. SND_SOC_DAPM_OUTPUT("HPR"),
  718. SND_SOC_DAPM_OUTPUT("LINE"),
  719. };
  720. /* DAPM audio route definition */
  721. static const struct snd_soc_dapm_route da9055_audio_map[] = {
  722. /* Dest Connecting Widget source */
  723. /* Input path */
  724. {"Mic Left Source", "MIC1_P_N", "MIC1"},
  725. {"Mic Left Source", "MIC1_P", "MIC1"},
  726. {"Mic Left Source", "MIC1_N", "MIC1"},
  727. {"Mic Left Source", "MIC2_L", "MIC2"},
  728. {"Mic Right Source", "MIC2_R_L", "MIC2"},
  729. {"Mic Right Source", "MIC2_R", "MIC2"},
  730. {"Mic Right Source", "MIC2_L", "MIC2"},
  731. {"Mic Left", NULL, "Mic Left Source"},
  732. {"Mic Right", NULL, "Mic Right Source"},
  733. {"Aux Left", NULL, "AUXL"},
  734. {"Aux Right", NULL, "AUXR"},
  735. {"In Mixer Left", "Mic Left Switch", "Mic Left"},
  736. {"In Mixer Left", "Mic Right Switch", "Mic Right"},
  737. {"In Mixer Left", "Aux Left Switch", "Aux Left"},
  738. {"In Mixer Right", "Mic Right Switch", "Mic Right"},
  739. {"In Mixer Right", "Mic Left Switch", "Mic Left"},
  740. {"In Mixer Right", "Aux Right Switch", "Aux Right"},
  741. {"In Mixer Right", "Mixin Left Switch", "MIXIN Left"},
  742. {"MIXIN Left", NULL, "In Mixer Left"},
  743. {"ADC Left", NULL, "MIXIN Left"},
  744. {"MIXIN Right", NULL, "In Mixer Right"},
  745. {"ADC Right", NULL, "MIXIN Right"},
  746. {"ADC Left", NULL, "AIF"},
  747. {"ADC Right", NULL, "AIF"},
  748. /* Output path */
  749. {"AIFIN Left", NULL, "AIF"},
  750. {"AIFIN Right", NULL, "AIF"},
  751. {"DAC Left Source", "ADC output left", "ADC Left"},
  752. {"DAC Left Source", "ADC output right", "ADC Right"},
  753. {"DAC Left Source", "AIF input left", "AIFIN Left"},
  754. {"DAC Left Source", "AIF input right", "AIFIN Right"},
  755. {"DAC Right Source", "ADC output left", "ADC Left"},
  756. {"DAC Right Source", "ADC output right", "ADC Right"},
  757. {"DAC Right Source", "AIF input left", "AIFIN Left"},
  758. {"DAC Right Source", "AIF input right", "AIFIN Right"},
  759. {"DAC Left", NULL, "DAC Left Source"},
  760. {"DAC Right", NULL, "DAC Right Source"},
  761. {"Out Mixer Left", "Aux Left Switch", "Aux Left"},
  762. {"Out Mixer Left", "Mixin Left Switch", "MIXIN Left"},
  763. {"Out Mixer Left", "Mixin Right Switch", "MIXIN Right"},
  764. {"Out Mixer Left", "Aux Left Invert Switch", "Aux Left"},
  765. {"Out Mixer Left", "Mixin Left Invert Switch", "MIXIN Left"},
  766. {"Out Mixer Left", "Mixin Right Invert Switch", "MIXIN Right"},
  767. {"Out Mixer Left", "DAC Left Switch", "DAC Left"},
  768. {"Out Mixer Right", "Aux Right Switch", "Aux Right"},
  769. {"Out Mixer Right", "Mixin Right Switch", "MIXIN Right"},
  770. {"Out Mixer Right", "Mixin Left Switch", "MIXIN Left"},
  771. {"Out Mixer Right", "Aux Right Invert Switch", "Aux Right"},
  772. {"Out Mixer Right", "Mixin Right Invert Switch", "MIXIN Right"},
  773. {"Out Mixer Right", "Mixin Left Invert Switch", "MIXIN Left"},
  774. {"Out Mixer Right", "DAC Right Switch", "DAC Right"},
  775. {"MIXOUT Left", NULL, "Out Mixer Left"},
  776. {"Headphone Left Enable", "Switch", "MIXOUT Left"},
  777. {"Headphone Left", NULL, "Headphone Left Enable"},
  778. {"Headphone Left", NULL, "Charge Pump"},
  779. {"HPL", NULL, "Headphone Left"},
  780. {"MIXOUT Right", NULL, "Out Mixer Right"},
  781. {"Headphone Right Enable", "Switch", "MIXOUT Right"},
  782. {"Headphone Right", NULL, "Headphone Right Enable"},
  783. {"Headphone Right", NULL, "Charge Pump"},
  784. {"HPR", NULL, "Headphone Right"},
  785. {"MIXOUT Right", NULL, "Out Mixer Right"},
  786. {"Lineout Enable", "Switch", "MIXOUT Right"},
  787. {"Lineout", NULL, "Lineout Enable"},
  788. {"LINE", NULL, "Lineout"},
  789. };
  790. /* Codec private data */
  791. struct da9055_priv {
  792. struct regmap *regmap;
  793. unsigned int mclk_rate;
  794. int master;
  795. struct da9055_platform_data *pdata;
  796. };
  797. static const struct reg_default da9055_reg_defaults[] = {
  798. { 0x21, 0x10 },
  799. { 0x22, 0x0A },
  800. { 0x23, 0x00 },
  801. { 0x24, 0x00 },
  802. { 0x25, 0x00 },
  803. { 0x26, 0x00 },
  804. { 0x27, 0x0C },
  805. { 0x28, 0x01 },
  806. { 0x29, 0x08 },
  807. { 0x2A, 0x32 },
  808. { 0x2B, 0x00 },
  809. { 0x30, 0x35 },
  810. { 0x31, 0x35 },
  811. { 0x32, 0x00 },
  812. { 0x33, 0x00 },
  813. { 0x34, 0x03 },
  814. { 0x35, 0x03 },
  815. { 0x36, 0x6F },
  816. { 0x37, 0x6F },
  817. { 0x38, 0x80 },
  818. { 0x39, 0x01 },
  819. { 0x3A, 0x01 },
  820. { 0x40, 0x00 },
  821. { 0x41, 0x88 },
  822. { 0x42, 0x88 },
  823. { 0x43, 0x08 },
  824. { 0x44, 0x80 },
  825. { 0x45, 0x6F },
  826. { 0x46, 0x6F },
  827. { 0x47, 0x61 },
  828. { 0x48, 0x35 },
  829. { 0x49, 0x35 },
  830. { 0x4A, 0x35 },
  831. { 0x4B, 0x00 },
  832. { 0x4C, 0x00 },
  833. { 0x60, 0x44 },
  834. { 0x61, 0x44 },
  835. { 0x62, 0x00 },
  836. { 0x63, 0x40 },
  837. { 0x64, 0x40 },
  838. { 0x65, 0x40 },
  839. { 0x66, 0x40 },
  840. { 0x67, 0x40 },
  841. { 0x68, 0x40 },
  842. { 0x69, 0x48 },
  843. { 0x6A, 0x40 },
  844. { 0x6B, 0x41 },
  845. { 0x6C, 0x40 },
  846. { 0x6D, 0x40 },
  847. { 0x6E, 0x10 },
  848. { 0x6F, 0x10 },
  849. { 0x90, 0x80 },
  850. { 0x92, 0x02 },
  851. { 0x93, 0x00 },
  852. { 0x99, 0x00 },
  853. { 0x9A, 0x00 },
  854. { 0x9B, 0x00 },
  855. { 0x9C, 0x3F },
  856. { 0x9D, 0x00 },
  857. { 0x9E, 0x3F },
  858. { 0x9F, 0xFF },
  859. { 0xA0, 0x71 },
  860. { 0xA1, 0x00 },
  861. { 0xA2, 0x00 },
  862. { 0xA6, 0x00 },
  863. { 0xA7, 0x00 },
  864. { 0xAB, 0x00 },
  865. { 0xAC, 0x00 },
  866. { 0xAD, 0x00 },
  867. { 0xAF, 0x08 },
  868. { 0xB0, 0x00 },
  869. { 0xB1, 0x00 },
  870. { 0xB2, 0x00 },
  871. };
  872. static bool da9055_volatile_register(struct device *dev,
  873. unsigned int reg)
  874. {
  875. switch (reg) {
  876. case DA9055_STATUS1:
  877. case DA9055_PLL_STATUS:
  878. case DA9055_AUX_L_GAIN_STATUS:
  879. case DA9055_AUX_R_GAIN_STATUS:
  880. case DA9055_MIC_L_GAIN_STATUS:
  881. case DA9055_MIC_R_GAIN_STATUS:
  882. case DA9055_MIXIN_L_GAIN_STATUS:
  883. case DA9055_MIXIN_R_GAIN_STATUS:
  884. case DA9055_ADC_L_GAIN_STATUS:
  885. case DA9055_ADC_R_GAIN_STATUS:
  886. case DA9055_DAC_L_GAIN_STATUS:
  887. case DA9055_DAC_R_GAIN_STATUS:
  888. case DA9055_HP_L_GAIN_STATUS:
  889. case DA9055_HP_R_GAIN_STATUS:
  890. case DA9055_LINE_GAIN_STATUS:
  891. case DA9055_ALC_CIC_OP_LVL_DATA:
  892. return 1;
  893. default:
  894. return 0;
  895. }
  896. }
  897. /* Set DAI word length */
  898. static int da9055_hw_params(struct snd_pcm_substream *substream,
  899. struct snd_pcm_hw_params *params,
  900. struct snd_soc_dai *dai)
  901. {
  902. struct snd_soc_codec *codec = dai->codec;
  903. struct da9055_priv *da9055 = snd_soc_codec_get_drvdata(codec);
  904. u8 aif_ctrl, fs;
  905. u32 sysclk;
  906. switch (params_width(params)) {
  907. case 16:
  908. aif_ctrl = DA9055_AIF_WORD_S16_LE;
  909. break;
  910. case 20:
  911. aif_ctrl = DA9055_AIF_WORD_S20_3LE;
  912. break;
  913. case 24:
  914. aif_ctrl = DA9055_AIF_WORD_S24_LE;
  915. break;
  916. case 32:
  917. aif_ctrl = DA9055_AIF_WORD_S32_LE;
  918. break;
  919. default:
  920. return -EINVAL;
  921. }
  922. /* Set AIF format */
  923. snd_soc_update_bits(codec, DA9055_AIF_CTRL, DA9055_AIF_WORD_LENGTH_MASK,
  924. aif_ctrl);
  925. switch (params_rate(params)) {
  926. case 8000:
  927. fs = DA9055_SR_8000;
  928. sysclk = 3072000;
  929. break;
  930. case 11025:
  931. fs = DA9055_SR_11025;
  932. sysclk = 2822400;
  933. break;
  934. case 12000:
  935. fs = DA9055_SR_12000;
  936. sysclk = 3072000;
  937. break;
  938. case 16000:
  939. fs = DA9055_SR_16000;
  940. sysclk = 3072000;
  941. break;
  942. case 22050:
  943. fs = DA9055_SR_22050;
  944. sysclk = 2822400;
  945. break;
  946. case 32000:
  947. fs = DA9055_SR_32000;
  948. sysclk = 3072000;
  949. break;
  950. case 44100:
  951. fs = DA9055_SR_44100;
  952. sysclk = 2822400;
  953. break;
  954. case 48000:
  955. fs = DA9055_SR_48000;
  956. sysclk = 3072000;
  957. break;
  958. case 88200:
  959. fs = DA9055_SR_88200;
  960. sysclk = 2822400;
  961. break;
  962. case 96000:
  963. fs = DA9055_SR_96000;
  964. sysclk = 3072000;
  965. break;
  966. default:
  967. return -EINVAL;
  968. }
  969. if (da9055->mclk_rate) {
  970. /* PLL Mode, Write actual FS */
  971. snd_soc_write(codec, DA9055_SR, fs);
  972. } else {
  973. /*
  974. * Non-PLL Mode
  975. * When PLL is bypassed, chip assumes constant MCLK of
  976. * 12.288MHz and uses sample rate value to divide this MCLK
  977. * to derive its sys clk. As sys clk has to be 256 * Fs, we
  978. * need to write constant sample rate i.e. 48KHz.
  979. */
  980. snd_soc_write(codec, DA9055_SR, DA9055_SR_48000);
  981. }
  982. if (da9055->mclk_rate && (da9055->mclk_rate != sysclk)) {
  983. /* PLL Mode */
  984. if (!da9055->master) {
  985. /* PLL slave mode, enable PLL and also SRM */
  986. snd_soc_update_bits(codec, DA9055_PLL_CTRL,
  987. DA9055_PLL_EN | DA9055_PLL_SRM_EN,
  988. DA9055_PLL_EN | DA9055_PLL_SRM_EN);
  989. } else {
  990. /* PLL master mode, only enable PLL */
  991. snd_soc_update_bits(codec, DA9055_PLL_CTRL,
  992. DA9055_PLL_EN, DA9055_PLL_EN);
  993. }
  994. } else {
  995. /* Non PLL Mode, disable PLL */
  996. snd_soc_update_bits(codec, DA9055_PLL_CTRL, DA9055_PLL_EN, 0);
  997. }
  998. return 0;
  999. }
  1000. /* Set DAI mode and Format */
  1001. static int da9055_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  1002. {
  1003. struct snd_soc_codec *codec = codec_dai->codec;
  1004. struct da9055_priv *da9055 = snd_soc_codec_get_drvdata(codec);
  1005. u8 aif_clk_mode, aif_ctrl, mode;
  1006. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1007. case SND_SOC_DAIFMT_CBM_CFM:
  1008. /* DA9055 in I2S Master Mode */
  1009. mode = 1;
  1010. aif_clk_mode = DA9055_AIF_CLK_EN_MASTER_MODE;
  1011. break;
  1012. case SND_SOC_DAIFMT_CBS_CFS:
  1013. /* DA9055 in I2S Slave Mode */
  1014. mode = 0;
  1015. aif_clk_mode = DA9055_AIF_CLK_EN_SLAVE_MODE;
  1016. break;
  1017. default:
  1018. return -EINVAL;
  1019. }
  1020. /* Don't allow change of mode if PLL is enabled */
  1021. if ((snd_soc_read(codec, DA9055_PLL_CTRL) & DA9055_PLL_EN) &&
  1022. (da9055->master != mode))
  1023. return -EINVAL;
  1024. da9055->master = mode;
  1025. /* Only I2S is supported */
  1026. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1027. case SND_SOC_DAIFMT_I2S:
  1028. aif_ctrl = DA9055_AIF_FORMAT_I2S_MODE;
  1029. break;
  1030. case SND_SOC_DAIFMT_LEFT_J:
  1031. aif_ctrl = DA9055_AIF_FORMAT_LEFT_J;
  1032. break;
  1033. case SND_SOC_DAIFMT_RIGHT_J:
  1034. aif_ctrl = DA9055_AIF_FORMAT_RIGHT_J;
  1035. break;
  1036. case SND_SOC_DAIFMT_DSP_A:
  1037. aif_ctrl = DA9055_AIF_FORMAT_DSP;
  1038. break;
  1039. default:
  1040. return -EINVAL;
  1041. }
  1042. /* By default only 32 BCLK per WCLK is supported */
  1043. aif_clk_mode |= DA9055_AIF_BCLKS_PER_WCLK_32;
  1044. snd_soc_update_bits(codec, DA9055_AIF_CLK_MODE,
  1045. (DA9055_AIF_CLK_MODE_MASK | DA9055_AIF_BCLK_MASK),
  1046. aif_clk_mode);
  1047. snd_soc_update_bits(codec, DA9055_AIF_CTRL, DA9055_AIF_FORMAT_MASK,
  1048. aif_ctrl);
  1049. return 0;
  1050. }
  1051. static int da9055_mute(struct snd_soc_dai *dai, int mute)
  1052. {
  1053. struct snd_soc_codec *codec = dai->codec;
  1054. if (mute) {
  1055. snd_soc_update_bits(codec, DA9055_DAC_L_CTRL,
  1056. DA9055_DAC_L_MUTE_EN, DA9055_DAC_L_MUTE_EN);
  1057. snd_soc_update_bits(codec, DA9055_DAC_R_CTRL,
  1058. DA9055_DAC_R_MUTE_EN, DA9055_DAC_R_MUTE_EN);
  1059. } else {
  1060. snd_soc_update_bits(codec, DA9055_DAC_L_CTRL,
  1061. DA9055_DAC_L_MUTE_EN, 0);
  1062. snd_soc_update_bits(codec, DA9055_DAC_R_CTRL,
  1063. DA9055_DAC_R_MUTE_EN, 0);
  1064. }
  1065. return 0;
  1066. }
  1067. #define DA9055_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  1068. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1069. static int da9055_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  1070. int clk_id, unsigned int freq, int dir)
  1071. {
  1072. struct snd_soc_codec *codec = codec_dai->codec;
  1073. struct da9055_priv *da9055 = snd_soc_codec_get_drvdata(codec);
  1074. switch (clk_id) {
  1075. case DA9055_CLKSRC_MCLK:
  1076. switch (freq) {
  1077. case 11289600:
  1078. case 12000000:
  1079. case 12288000:
  1080. case 13000000:
  1081. case 13500000:
  1082. case 14400000:
  1083. case 19200000:
  1084. case 19680000:
  1085. case 19800000:
  1086. da9055->mclk_rate = freq;
  1087. return 0;
  1088. default:
  1089. dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
  1090. freq);
  1091. return -EINVAL;
  1092. }
  1093. break;
  1094. default:
  1095. dev_err(codec_dai->dev, "Unknown clock source %d\n", clk_id);
  1096. return -EINVAL;
  1097. }
  1098. }
  1099. /*
  1100. * da9055_set_dai_pll : Configure the codec PLL
  1101. * @param codec_dai : Pointer to codec DAI
  1102. * @param pll_id : da9055 has only one pll, so pll_id is always zero
  1103. * @param fref : Input MCLK frequency
  1104. * @param fout : FsDM value
  1105. * @return int : Zero for success, negative error code for error
  1106. *
  1107. * Note: Supported PLL input frequencies are 11.2896MHz, 12MHz, 12.288MHz,
  1108. * 13MHz, 13.5MHz, 14.4MHz, 19.2MHz, 19.6MHz and 19.8MHz
  1109. */
  1110. static int da9055_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id,
  1111. int source, unsigned int fref, unsigned int fout)
  1112. {
  1113. struct snd_soc_codec *codec = codec_dai->codec;
  1114. struct da9055_priv *da9055 = snd_soc_codec_get_drvdata(codec);
  1115. u8 pll_frac_top, pll_frac_bot, pll_integer, cnt;
  1116. /* Disable PLL before setting the divisors */
  1117. snd_soc_update_bits(codec, DA9055_PLL_CTRL, DA9055_PLL_EN, 0);
  1118. /* In slave mode, there is only one set of divisors */
  1119. if (!da9055->master && (fout != 2822400))
  1120. goto pll_err;
  1121. /* Search pll div array for correct divisors */
  1122. for (cnt = 0; cnt < ARRAY_SIZE(da9055_pll_div); cnt++) {
  1123. /* Check fref, mode and fout */
  1124. if ((fref == da9055_pll_div[cnt].fref) &&
  1125. (da9055->master == da9055_pll_div[cnt].mode) &&
  1126. (fout == da9055_pll_div[cnt].fout)) {
  1127. /* All match, pick up divisors */
  1128. pll_frac_top = da9055_pll_div[cnt].frac_top;
  1129. pll_frac_bot = da9055_pll_div[cnt].frac_bot;
  1130. pll_integer = da9055_pll_div[cnt].integer;
  1131. break;
  1132. }
  1133. }
  1134. if (cnt >= ARRAY_SIZE(da9055_pll_div))
  1135. goto pll_err;
  1136. /* Write PLL dividers */
  1137. snd_soc_write(codec, DA9055_PLL_FRAC_TOP, pll_frac_top);
  1138. snd_soc_write(codec, DA9055_PLL_FRAC_BOT, pll_frac_bot);
  1139. snd_soc_write(codec, DA9055_PLL_INTEGER, pll_integer);
  1140. return 0;
  1141. pll_err:
  1142. dev_err(codec_dai->dev, "Error in setting up PLL\n");
  1143. return -EINVAL;
  1144. }
  1145. /* DAI operations */
  1146. static const struct snd_soc_dai_ops da9055_dai_ops = {
  1147. .hw_params = da9055_hw_params,
  1148. .set_fmt = da9055_set_dai_fmt,
  1149. .set_sysclk = da9055_set_dai_sysclk,
  1150. .set_pll = da9055_set_dai_pll,
  1151. .digital_mute = da9055_mute,
  1152. };
  1153. static struct snd_soc_dai_driver da9055_dai = {
  1154. .name = "da9055-hifi",
  1155. /* Playback Capabilities */
  1156. .playback = {
  1157. .stream_name = "Playback",
  1158. .channels_min = 1,
  1159. .channels_max = 2,
  1160. .rates = SNDRV_PCM_RATE_8000_96000,
  1161. .formats = DA9055_FORMATS,
  1162. },
  1163. /* Capture Capabilities */
  1164. .capture = {
  1165. .stream_name = "Capture",
  1166. .channels_min = 1,
  1167. .channels_max = 2,
  1168. .rates = SNDRV_PCM_RATE_8000_96000,
  1169. .formats = DA9055_FORMATS,
  1170. },
  1171. .ops = &da9055_dai_ops,
  1172. .symmetric_rates = 1,
  1173. };
  1174. static int da9055_set_bias_level(struct snd_soc_codec *codec,
  1175. enum snd_soc_bias_level level)
  1176. {
  1177. switch (level) {
  1178. case SND_SOC_BIAS_ON:
  1179. case SND_SOC_BIAS_PREPARE:
  1180. break;
  1181. case SND_SOC_BIAS_STANDBY:
  1182. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  1183. /* Enable VMID reference & master bias */
  1184. snd_soc_update_bits(codec, DA9055_REFERENCES,
  1185. DA9055_VMID_EN | DA9055_BIAS_EN,
  1186. DA9055_VMID_EN | DA9055_BIAS_EN);
  1187. }
  1188. break;
  1189. case SND_SOC_BIAS_OFF:
  1190. /* Disable VMID reference & master bias */
  1191. snd_soc_update_bits(codec, DA9055_REFERENCES,
  1192. DA9055_VMID_EN | DA9055_BIAS_EN, 0);
  1193. break;
  1194. }
  1195. return 0;
  1196. }
  1197. static int da9055_probe(struct snd_soc_codec *codec)
  1198. {
  1199. struct da9055_priv *da9055 = snd_soc_codec_get_drvdata(codec);
  1200. /* Enable all Gain Ramps */
  1201. snd_soc_update_bits(codec, DA9055_AUX_L_CTRL,
  1202. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1203. snd_soc_update_bits(codec, DA9055_AUX_R_CTRL,
  1204. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1205. snd_soc_update_bits(codec, DA9055_MIXIN_L_CTRL,
  1206. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1207. snd_soc_update_bits(codec, DA9055_MIXIN_R_CTRL,
  1208. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1209. snd_soc_update_bits(codec, DA9055_ADC_L_CTRL,
  1210. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1211. snd_soc_update_bits(codec, DA9055_ADC_R_CTRL,
  1212. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1213. snd_soc_update_bits(codec, DA9055_DAC_L_CTRL,
  1214. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1215. snd_soc_update_bits(codec, DA9055_DAC_R_CTRL,
  1216. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1217. snd_soc_update_bits(codec, DA9055_HP_L_CTRL,
  1218. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1219. snd_soc_update_bits(codec, DA9055_HP_R_CTRL,
  1220. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1221. snd_soc_update_bits(codec, DA9055_LINE_CTRL,
  1222. DA9055_GAIN_RAMPING_EN, DA9055_GAIN_RAMPING_EN);
  1223. /*
  1224. * There are two separate control bits for input and output mixers.
  1225. * One to enable corresponding amplifier and other to enable its
  1226. * output. As amplifier bits are related to power control, they are
  1227. * being managed by DAPM while other (non power related) bits are
  1228. * enabled here
  1229. */
  1230. snd_soc_update_bits(codec, DA9055_MIXIN_L_CTRL,
  1231. DA9055_MIXIN_L_MIX_EN, DA9055_MIXIN_L_MIX_EN);
  1232. snd_soc_update_bits(codec, DA9055_MIXIN_R_CTRL,
  1233. DA9055_MIXIN_R_MIX_EN, DA9055_MIXIN_R_MIX_EN);
  1234. snd_soc_update_bits(codec, DA9055_MIXOUT_L_CTRL,
  1235. DA9055_MIXOUT_L_MIX_EN, DA9055_MIXOUT_L_MIX_EN);
  1236. snd_soc_update_bits(codec, DA9055_MIXOUT_R_CTRL,
  1237. DA9055_MIXOUT_R_MIX_EN, DA9055_MIXOUT_R_MIX_EN);
  1238. /* Set this as per your system configuration */
  1239. snd_soc_write(codec, DA9055_PLL_CTRL, DA9055_PLL_INDIV_10_20_MHZ);
  1240. /* Set platform data values */
  1241. if (da9055->pdata) {
  1242. /* set mic bias source */
  1243. if (da9055->pdata->micbias_source) {
  1244. snd_soc_update_bits(codec, DA9055_MIXIN_R_SELECT,
  1245. DA9055_MICBIAS2_EN,
  1246. DA9055_MICBIAS2_EN);
  1247. } else {
  1248. snd_soc_update_bits(codec, DA9055_MIXIN_R_SELECT,
  1249. DA9055_MICBIAS2_EN, 0);
  1250. }
  1251. /* set mic bias voltage */
  1252. switch (da9055->pdata->micbias) {
  1253. case DA9055_MICBIAS_2_2V:
  1254. case DA9055_MICBIAS_2_1V:
  1255. case DA9055_MICBIAS_1_8V:
  1256. case DA9055_MICBIAS_1_6V:
  1257. snd_soc_update_bits(codec, DA9055_MIC_CONFIG,
  1258. DA9055_MICBIAS_LEVEL_MASK,
  1259. (da9055->pdata->micbias) << 4);
  1260. break;
  1261. }
  1262. }
  1263. return 0;
  1264. }
  1265. static struct snd_soc_codec_driver soc_codec_dev_da9055 = {
  1266. .probe = da9055_probe,
  1267. .set_bias_level = da9055_set_bias_level,
  1268. .component_driver = {
  1269. .controls = da9055_snd_controls,
  1270. .num_controls = ARRAY_SIZE(da9055_snd_controls),
  1271. .dapm_widgets = da9055_dapm_widgets,
  1272. .num_dapm_widgets = ARRAY_SIZE(da9055_dapm_widgets),
  1273. .dapm_routes = da9055_audio_map,
  1274. .num_dapm_routes = ARRAY_SIZE(da9055_audio_map),
  1275. },
  1276. };
  1277. static const struct regmap_config da9055_regmap_config = {
  1278. .reg_bits = 8,
  1279. .val_bits = 8,
  1280. .reg_defaults = da9055_reg_defaults,
  1281. .num_reg_defaults = ARRAY_SIZE(da9055_reg_defaults),
  1282. .volatile_reg = da9055_volatile_register,
  1283. .cache_type = REGCACHE_RBTREE,
  1284. };
  1285. static int da9055_i2c_probe(struct i2c_client *i2c,
  1286. const struct i2c_device_id *id)
  1287. {
  1288. struct da9055_priv *da9055;
  1289. struct da9055_platform_data *pdata = dev_get_platdata(&i2c->dev);
  1290. int ret;
  1291. da9055 = devm_kzalloc(&i2c->dev, sizeof(struct da9055_priv),
  1292. GFP_KERNEL);
  1293. if (!da9055)
  1294. return -ENOMEM;
  1295. if (pdata)
  1296. da9055->pdata = pdata;
  1297. i2c_set_clientdata(i2c, da9055);
  1298. da9055->regmap = devm_regmap_init_i2c(i2c, &da9055_regmap_config);
  1299. if (IS_ERR(da9055->regmap)) {
  1300. ret = PTR_ERR(da9055->regmap);
  1301. dev_err(&i2c->dev, "regmap_init() failed: %d\n", ret);
  1302. return ret;
  1303. }
  1304. ret = snd_soc_register_codec(&i2c->dev,
  1305. &soc_codec_dev_da9055, &da9055_dai, 1);
  1306. if (ret < 0) {
  1307. dev_err(&i2c->dev, "Failed to register da9055 codec: %d\n",
  1308. ret);
  1309. }
  1310. return ret;
  1311. }
  1312. static int da9055_remove(struct i2c_client *client)
  1313. {
  1314. snd_soc_unregister_codec(&client->dev);
  1315. return 0;
  1316. }
  1317. /*
  1318. * DO NOT change the device Ids. The naming is intentionally specific as both
  1319. * the CODEC and PMIC parts of this chip are instantiated separately as I2C
  1320. * devices (both have configurable I2C addresses, and are to all intents and
  1321. * purposes separate). As a result there are specific DA9055 Ids for CODEC
  1322. * and PMIC, which must be different to operate together.
  1323. */
  1324. static const struct i2c_device_id da9055_i2c_id[] = {
  1325. { "da9055-codec", 0 },
  1326. { }
  1327. };
  1328. MODULE_DEVICE_TABLE(i2c, da9055_i2c_id);
  1329. static const struct of_device_id da9055_of_match[] = {
  1330. { .compatible = "dlg,da9055-codec", },
  1331. { }
  1332. };
  1333. MODULE_DEVICE_TABLE(of, da9055_of_match);
  1334. /* I2C codec control layer */
  1335. static struct i2c_driver da9055_i2c_driver = {
  1336. .driver = {
  1337. .name = "da9055-codec",
  1338. .of_match_table = of_match_ptr(da9055_of_match),
  1339. },
  1340. .probe = da9055_i2c_probe,
  1341. .remove = da9055_remove,
  1342. .id_table = da9055_i2c_id,
  1343. };
  1344. module_i2c_driver(da9055_i2c_driver);
  1345. MODULE_DESCRIPTION("ASoC DA9055 Codec driver");
  1346. MODULE_AUTHOR("David Chen, Ashish Chavan");
  1347. MODULE_LICENSE("GPL");