88pm860x-codec.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  1. /*
  2. * 88pm860x-codec.c -- 88PM860x ALSA SoC Audio Driver
  3. *
  4. * Copyright 2010 Marvell International Ltd.
  5. * Author: Haojian Zhuang <haojian.zhuang@marvell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/mfd/88pm860x.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <sound/core.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include <sound/tlv.h>
  23. #include <sound/initval.h>
  24. #include <sound/jack.h>
  25. #include <trace/events/asoc.h>
  26. #include "88pm860x-codec.h"
  27. #define MAX_NAME_LEN 20
  28. #define REG_CACHE_SIZE 0x40
  29. #define REG_CACHE_BASE 0xb0
  30. /* Status Register 1 (0x01) */
  31. #define REG_STATUS_1 0x01
  32. #define MIC_STATUS (1 << 7)
  33. #define HOOK_STATUS (1 << 6)
  34. #define HEADSET_STATUS (1 << 5)
  35. /* Mic Detection Register (0x37) */
  36. #define REG_MIC_DET 0x37
  37. #define CONTINUOUS_POLLING (3 << 1)
  38. #define EN_MIC_DET (1 << 0)
  39. #define MICDET_MASK 0x07
  40. /* Headset Detection Register (0x38) */
  41. #define REG_HS_DET 0x38
  42. #define EN_HS_DET (1 << 0)
  43. /* Misc2 Register (0x42) */
  44. #define REG_MISC2 0x42
  45. #define AUDIO_PLL (1 << 5)
  46. #define AUDIO_SECTION_RESET (1 << 4)
  47. #define AUDIO_SECTION_ON (1 << 3)
  48. /* PCM Interface Register 2 (0xb1) */
  49. #define PCM_INF2_BCLK (1 << 6) /* Bit clock polarity */
  50. #define PCM_INF2_FS (1 << 5) /* Frame Sync polarity */
  51. #define PCM_INF2_MASTER (1 << 4) /* Master / Slave */
  52. #define PCM_INF2_18WL (1 << 3) /* 18 / 16 bits */
  53. #define PCM_GENERAL_I2S 0
  54. #define PCM_EXACT_I2S 1
  55. #define PCM_LEFT_I2S 2
  56. #define PCM_RIGHT_I2S 3
  57. #define PCM_SHORT_FS 4
  58. #define PCM_LONG_FS 5
  59. #define PCM_MODE_MASK 7
  60. /* I2S Interface Register 4 (0xbe) */
  61. #define I2S_EQU_BYP (1 << 6)
  62. /* DAC Offset Register (0xcb) */
  63. #define DAC_MUTE (1 << 7)
  64. #define MUTE_LEFT (1 << 6)
  65. #define MUTE_RIGHT (1 << 2)
  66. /* ADC Analog Register 1 (0xd0) */
  67. #define REG_ADC_ANA_1 0xd0
  68. #define MIC1BIAS_MASK 0x60
  69. /* Earpiece/Speaker Control Register 2 (0xda) */
  70. #define REG_EAR2 0xda
  71. #define RSYNC_CHANGE (1 << 2)
  72. /* Audio Supplies Register 2 (0xdc) */
  73. #define REG_SUPPLIES2 0xdc
  74. #define LDO15_READY (1 << 4)
  75. #define LDO15_EN (1 << 3)
  76. #define CPUMP_READY (1 << 2)
  77. #define CPUMP_EN (1 << 1)
  78. #define AUDIO_EN (1 << 0)
  79. #define SUPPLY_MASK (LDO15_EN | CPUMP_EN | AUDIO_EN)
  80. /* Audio Enable Register 1 (0xdd) */
  81. #define ADC_MOD_RIGHT (1 << 1)
  82. #define ADC_MOD_LEFT (1 << 0)
  83. /* Audio Enable Register 2 (0xde) */
  84. #define ADC_LEFT (1 << 5)
  85. #define ADC_RIGHT (1 << 4)
  86. /* DAC Enable Register 2 (0xe1) */
  87. #define DAC_LEFT (1 << 5)
  88. #define DAC_RIGHT (1 << 4)
  89. #define MODULATOR (1 << 3)
  90. /* Shorts Register (0xeb) */
  91. #define REG_SHORTS 0xeb
  92. #define CLR_SHORT_LO2 (1 << 7)
  93. #define SHORT_LO2 (1 << 6)
  94. #define CLR_SHORT_LO1 (1 << 5)
  95. #define SHORT_LO1 (1 << 4)
  96. #define CLR_SHORT_HS2 (1 << 3)
  97. #define SHORT_HS2 (1 << 2)
  98. #define CLR_SHORT_HS1 (1 << 1)
  99. #define SHORT_HS1 (1 << 0)
  100. /*
  101. * This widget should be just after DAC & PGA in DAPM power-on sequence and
  102. * before DAC & PGA in DAPM power-off sequence.
  103. */
  104. #define PM860X_DAPM_OUTPUT(wname, wevent) \
  105. { .id = snd_soc_dapm_pga, .name = wname, .reg = SND_SOC_NOPM, \
  106. .shift = 0, .invert = 0, .kcontrol_news = NULL, \
  107. .num_kcontrols = 0, .event = wevent, \
  108. .event_flags = SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD, }
  109. struct pm860x_det {
  110. struct snd_soc_jack *hp_jack;
  111. struct snd_soc_jack *mic_jack;
  112. int hp_det;
  113. int mic_det;
  114. int hook_det;
  115. int hs_shrt;
  116. int lo_shrt;
  117. };
  118. struct pm860x_priv {
  119. unsigned int sysclk;
  120. unsigned int pcmclk;
  121. unsigned int dir;
  122. unsigned int filter;
  123. struct snd_soc_codec *codec;
  124. struct i2c_client *i2c;
  125. struct pm860x_chip *chip;
  126. struct pm860x_det det;
  127. int irq[4];
  128. unsigned char name[4][MAX_NAME_LEN];
  129. };
  130. /* -9450dB to 0dB in 150dB steps ( mute instead of -9450dB) */
  131. static const DECLARE_TLV_DB_SCALE(dpga_tlv, -9450, 150, 1);
  132. /* -9dB to 0db in 3dB steps */
  133. static const DECLARE_TLV_DB_SCALE(adc_tlv, -900, 300, 0);
  134. /* {-23, -17, -13.5, -11, -9, -6, -3, 0}dB */
  135. static const unsigned int mic_tlv[] = {
  136. TLV_DB_RANGE_HEAD(5),
  137. 0, 0, TLV_DB_SCALE_ITEM(-2300, 0, 0),
  138. 1, 1, TLV_DB_SCALE_ITEM(-1700, 0, 0),
  139. 2, 2, TLV_DB_SCALE_ITEM(-1350, 0, 0),
  140. 3, 3, TLV_DB_SCALE_ITEM(-1100, 0, 0),
  141. 4, 7, TLV_DB_SCALE_ITEM(-900, 300, 0),
  142. };
  143. /* {0, 0, 0, -6, 0, 6, 12, 18}dB */
  144. static const unsigned int aux_tlv[] = {
  145. TLV_DB_RANGE_HEAD(2),
  146. 0, 2, TLV_DB_SCALE_ITEM(0, 0, 0),
  147. 3, 7, TLV_DB_SCALE_ITEM(-600, 600, 0),
  148. };
  149. /* {-16, -13, -10, -7, -5.2, -3,3, -2.2, 0}dB, mute instead of -16dB */
  150. static const unsigned int out_tlv[] = {
  151. TLV_DB_RANGE_HEAD(4),
  152. 0, 3, TLV_DB_SCALE_ITEM(-1600, 300, 1),
  153. 4, 4, TLV_DB_SCALE_ITEM(-520, 0, 0),
  154. 5, 5, TLV_DB_SCALE_ITEM(-330, 0, 0),
  155. 6, 7, TLV_DB_SCALE_ITEM(-220, 220, 0),
  156. };
  157. static const unsigned int st_tlv[] = {
  158. TLV_DB_RANGE_HEAD(8),
  159. 0, 1, TLV_DB_SCALE_ITEM(-12041, 602, 0),
  160. 2, 3, TLV_DB_SCALE_ITEM(-11087, 250, 0),
  161. 4, 5, TLV_DB_SCALE_ITEM(-10643, 158, 0),
  162. 6, 7, TLV_DB_SCALE_ITEM(-10351, 116, 0),
  163. 8, 9, TLV_DB_SCALE_ITEM(-10133, 92, 0),
  164. 10, 13, TLV_DB_SCALE_ITEM(-9958, 70, 0),
  165. 14, 17, TLV_DB_SCALE_ITEM(-9689, 53, 0),
  166. 18, 271, TLV_DB_SCALE_ITEM(-9484, 37, 0),
  167. };
  168. /* Sidetone Gain = M * 2^(-5-N) */
  169. struct st_gain {
  170. unsigned int db;
  171. unsigned int m;
  172. unsigned int n;
  173. };
  174. static struct st_gain st_table[] = {
  175. {-12041, 1, 15}, {-11439, 1, 14}, {-11087, 3, 15}, {-10837, 1, 13},
  176. {-10643, 5, 15}, {-10485, 3, 14}, {-10351, 7, 15}, {-10235, 1, 12},
  177. {-10133, 9, 15}, {-10041, 5, 14}, { -9958, 11, 15}, { -9883, 3, 13},
  178. { -9813, 13, 15}, { -9749, 7, 14}, { -9689, 15, 15}, { -9633, 1, 11},
  179. { -9580, 17, 15}, { -9531, 9, 14}, { -9484, 19, 15}, { -9439, 5, 13},
  180. { -9397, 21, 15}, { -9356, 11, 14}, { -9318, 23, 15}, { -9281, 3, 12},
  181. { -9245, 25, 15}, { -9211, 13, 14}, { -9178, 27, 15}, { -9147, 7, 13},
  182. { -9116, 29, 15}, { -9087, 15, 14}, { -9058, 31, 15}, { -9031, 1, 10},
  183. { -8978, 17, 14}, { -8929, 9, 13}, { -8882, 19, 14}, { -8837, 5, 12},
  184. { -8795, 21, 14}, { -8754, 11, 13}, { -8716, 23, 14}, { -8679, 3, 11},
  185. { -8643, 25, 14}, { -8609, 13, 13}, { -8576, 27, 14}, { -8545, 7, 12},
  186. { -8514, 29, 14}, { -8485, 15, 13}, { -8456, 31, 14}, { -8429, 1, 9},
  187. { -8376, 17, 13}, { -8327, 9, 12}, { -8280, 19, 13}, { -8235, 5, 11},
  188. { -8193, 21, 13}, { -8152, 11, 12}, { -8114, 23, 13}, { -8077, 3, 10},
  189. { -8041, 25, 13}, { -8007, 13, 12}, { -7974, 27, 13}, { -7943, 7, 11},
  190. { -7912, 29, 13}, { -7883, 15, 12}, { -7854, 31, 13}, { -7827, 1, 8},
  191. { -7774, 17, 12}, { -7724, 9, 11}, { -7678, 19, 12}, { -7633, 5, 10},
  192. { -7591, 21, 12}, { -7550, 11, 11}, { -7512, 23, 12}, { -7475, 3, 9},
  193. { -7439, 25, 12}, { -7405, 13, 11}, { -7372, 27, 12}, { -7341, 7, 10},
  194. { -7310, 29, 12}, { -7281, 15, 11}, { -7252, 31, 12}, { -7225, 1, 7},
  195. { -7172, 17, 11}, { -7122, 9, 10}, { -7075, 19, 11}, { -7031, 5, 9},
  196. { -6989, 21, 11}, { -6948, 11, 10}, { -6910, 23, 11}, { -6873, 3, 8},
  197. { -6837, 25, 11}, { -6803, 13, 10}, { -6770, 27, 11}, { -6739, 7, 9},
  198. { -6708, 29, 11}, { -6679, 15, 10}, { -6650, 31, 11}, { -6623, 1, 6},
  199. { -6570, 17, 10}, { -6520, 9, 9}, { -6473, 19, 10}, { -6429, 5, 8},
  200. { -6386, 21, 10}, { -6346, 11, 9}, { -6307, 23, 10}, { -6270, 3, 7},
  201. { -6235, 25, 10}, { -6201, 13, 9}, { -6168, 27, 10}, { -6137, 7, 8},
  202. { -6106, 29, 10}, { -6077, 15, 9}, { -6048, 31, 10}, { -6021, 1, 5},
  203. { -5968, 17, 9}, { -5918, 9, 8}, { -5871, 19, 9}, { -5827, 5, 7},
  204. { -5784, 21, 9}, { -5744, 11, 8}, { -5705, 23, 9}, { -5668, 3, 6},
  205. { -5633, 25, 9}, { -5599, 13, 8}, { -5566, 27, 9}, { -5535, 7, 7},
  206. { -5504, 29, 9}, { -5475, 15, 8}, { -5446, 31, 9}, { -5419, 1, 4},
  207. { -5366, 17, 8}, { -5316, 9, 7}, { -5269, 19, 8}, { -5225, 5, 6},
  208. { -5182, 21, 8}, { -5142, 11, 7}, { -5103, 23, 8}, { -5066, 3, 5},
  209. { -5031, 25, 8}, { -4997, 13, 7}, { -4964, 27, 8}, { -4932, 7, 6},
  210. { -4902, 29, 8}, { -4873, 15, 7}, { -4844, 31, 8}, { -4816, 1, 3},
  211. { -4764, 17, 7}, { -4714, 9, 6}, { -4667, 19, 7}, { -4623, 5, 5},
  212. { -4580, 21, 7}, { -4540, 11, 6}, { -4501, 23, 7}, { -4464, 3, 4},
  213. { -4429, 25, 7}, { -4395, 13, 6}, { -4362, 27, 7}, { -4330, 7, 5},
  214. { -4300, 29, 7}, { -4270, 15, 6}, { -4242, 31, 7}, { -4214, 1, 2},
  215. { -4162, 17, 6}, { -4112, 9, 5}, { -4065, 19, 6}, { -4021, 5, 4},
  216. { -3978, 21, 6}, { -3938, 11, 5}, { -3899, 23, 6}, { -3862, 3, 3},
  217. { -3827, 25, 6}, { -3793, 13, 5}, { -3760, 27, 6}, { -3728, 7, 4},
  218. { -3698, 29, 6}, { -3668, 15, 5}, { -3640, 31, 6}, { -3612, 1, 1},
  219. { -3560, 17, 5}, { -3510, 9, 4}, { -3463, 19, 5}, { -3419, 5, 3},
  220. { -3376, 21, 5}, { -3336, 11, 4}, { -3297, 23, 5}, { -3260, 3, 2},
  221. { -3225, 25, 5}, { -3191, 13, 4}, { -3158, 27, 5}, { -3126, 7, 3},
  222. { -3096, 29, 5}, { -3066, 15, 4}, { -3038, 31, 5}, { -3010, 1, 0},
  223. { -2958, 17, 4}, { -2908, 9, 3}, { -2861, 19, 4}, { -2816, 5, 2},
  224. { -2774, 21, 4}, { -2734, 11, 3}, { -2695, 23, 4}, { -2658, 3, 1},
  225. { -2623, 25, 4}, { -2589, 13, 3}, { -2556, 27, 4}, { -2524, 7, 2},
  226. { -2494, 29, 4}, { -2464, 15, 3}, { -2436, 31, 4}, { -2408, 2, 0},
  227. { -2356, 17, 3}, { -2306, 9, 2}, { -2259, 19, 3}, { -2214, 5, 1},
  228. { -2172, 21, 3}, { -2132, 11, 2}, { -2093, 23, 3}, { -2056, 3, 0},
  229. { -2021, 25, 3}, { -1987, 13, 2}, { -1954, 27, 3}, { -1922, 7, 1},
  230. { -1892, 29, 3}, { -1862, 15, 2}, { -1834, 31, 3}, { -1806, 4, 0},
  231. { -1754, 17, 2}, { -1704, 9, 1}, { -1657, 19, 2}, { -1612, 5, 0},
  232. { -1570, 21, 2}, { -1530, 11, 1}, { -1491, 23, 2}, { -1454, 6, 0},
  233. { -1419, 25, 2}, { -1384, 13, 1}, { -1352, 27, 2}, { -1320, 7, 0},
  234. { -1290, 29, 2}, { -1260, 15, 1}, { -1232, 31, 2}, { -1204, 8, 0},
  235. { -1151, 17, 1}, { -1102, 9, 0}, { -1055, 19, 1}, { -1010, 10, 0},
  236. { -968, 21, 1}, { -928, 11, 0}, { -889, 23, 1}, { -852, 12, 0},
  237. { -816, 25, 1}, { -782, 13, 0}, { -750, 27, 1}, { -718, 14, 0},
  238. { -688, 29, 1}, { -658, 15, 0}, { -630, 31, 1}, { -602, 16, 0},
  239. { -549, 17, 0}, { -500, 18, 0}, { -453, 19, 0}, { -408, 20, 0},
  240. { -366, 21, 0}, { -325, 22, 0}, { -287, 23, 0}, { -250, 24, 0},
  241. { -214, 25, 0}, { -180, 26, 0}, { -148, 27, 0}, { -116, 28, 0},
  242. { -86, 29, 0}, { -56, 30, 0}, { -28, 31, 0}, { 0, 0, 0},
  243. };
  244. static int pm860x_volatile(unsigned int reg)
  245. {
  246. BUG_ON(reg >= REG_CACHE_SIZE);
  247. switch (reg) {
  248. case PM860X_AUDIO_SUPPLIES_2:
  249. return 1;
  250. }
  251. return 0;
  252. }
  253. static unsigned int pm860x_read_reg_cache(struct snd_soc_codec *codec,
  254. unsigned int reg)
  255. {
  256. unsigned char *cache = codec->reg_cache;
  257. BUG_ON(reg >= REG_CACHE_SIZE);
  258. if (pm860x_volatile(reg))
  259. return cache[reg];
  260. reg += REG_CACHE_BASE;
  261. return pm860x_reg_read(codec->control_data, reg);
  262. }
  263. static int pm860x_write_reg_cache(struct snd_soc_codec *codec,
  264. unsigned int reg, unsigned int value)
  265. {
  266. unsigned char *cache = codec->reg_cache;
  267. BUG_ON(reg >= REG_CACHE_SIZE);
  268. if (!pm860x_volatile(reg))
  269. cache[reg] = (unsigned char)value;
  270. reg += REG_CACHE_BASE;
  271. return pm860x_reg_write(codec->control_data, reg, value);
  272. }
  273. static int snd_soc_get_volsw_2r_st(struct snd_kcontrol *kcontrol,
  274. struct snd_ctl_elem_value *ucontrol)
  275. {
  276. struct soc_mixer_control *mc =
  277. (struct soc_mixer_control *)kcontrol->private_value;
  278. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  279. unsigned int reg = mc->reg;
  280. unsigned int reg2 = mc->rreg;
  281. int val[2], val2[2], i;
  282. val[0] = snd_soc_read(codec, reg) & 0x3f;
  283. val[1] = (snd_soc_read(codec, PM860X_SIDETONE_SHIFT) >> 4) & 0xf;
  284. val2[0] = snd_soc_read(codec, reg2) & 0x3f;
  285. val2[1] = (snd_soc_read(codec, PM860X_SIDETONE_SHIFT)) & 0xf;
  286. for (i = 0; i < ARRAY_SIZE(st_table); i++) {
  287. if ((st_table[i].m == val[0]) && (st_table[i].n == val[1]))
  288. ucontrol->value.integer.value[0] = i;
  289. if ((st_table[i].m == val2[0]) && (st_table[i].n == val2[1]))
  290. ucontrol->value.integer.value[1] = i;
  291. }
  292. return 0;
  293. }
  294. static int snd_soc_put_volsw_2r_st(struct snd_kcontrol *kcontrol,
  295. struct snd_ctl_elem_value *ucontrol)
  296. {
  297. struct soc_mixer_control *mc =
  298. (struct soc_mixer_control *)kcontrol->private_value;
  299. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  300. unsigned int reg = mc->reg;
  301. unsigned int reg2 = mc->rreg;
  302. int err;
  303. unsigned int val, val2;
  304. val = ucontrol->value.integer.value[0];
  305. val2 = ucontrol->value.integer.value[1];
  306. if (val >= ARRAY_SIZE(st_table) || val2 >= ARRAY_SIZE(st_table))
  307. return -EINVAL;
  308. err = snd_soc_update_bits(codec, reg, 0x3f, st_table[val].m);
  309. if (err < 0)
  310. return err;
  311. err = snd_soc_update_bits(codec, PM860X_SIDETONE_SHIFT, 0xf0,
  312. st_table[val].n << 4);
  313. if (err < 0)
  314. return err;
  315. err = snd_soc_update_bits(codec, reg2, 0x3f, st_table[val2].m);
  316. if (err < 0)
  317. return err;
  318. err = snd_soc_update_bits(codec, PM860X_SIDETONE_SHIFT, 0x0f,
  319. st_table[val2].n);
  320. return err;
  321. }
  322. static int snd_soc_get_volsw_2r_out(struct snd_kcontrol *kcontrol,
  323. struct snd_ctl_elem_value *ucontrol)
  324. {
  325. struct soc_mixer_control *mc =
  326. (struct soc_mixer_control *)kcontrol->private_value;
  327. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  328. unsigned int reg = mc->reg;
  329. unsigned int reg2 = mc->rreg;
  330. unsigned int shift = mc->shift;
  331. int max = mc->max, val, val2;
  332. unsigned int mask = (1 << fls(max)) - 1;
  333. val = snd_soc_read(codec, reg) >> shift;
  334. val2 = snd_soc_read(codec, reg2) >> shift;
  335. ucontrol->value.integer.value[0] = (max - val) & mask;
  336. ucontrol->value.integer.value[1] = (max - val2) & mask;
  337. return 0;
  338. }
  339. static int snd_soc_put_volsw_2r_out(struct snd_kcontrol *kcontrol,
  340. struct snd_ctl_elem_value *ucontrol)
  341. {
  342. struct soc_mixer_control *mc =
  343. (struct soc_mixer_control *)kcontrol->private_value;
  344. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  345. unsigned int reg = mc->reg;
  346. unsigned int reg2 = mc->rreg;
  347. unsigned int shift = mc->shift;
  348. int max = mc->max;
  349. unsigned int mask = (1 << fls(max)) - 1;
  350. int err;
  351. unsigned int val, val2, val_mask;
  352. val_mask = mask << shift;
  353. val = ((max - ucontrol->value.integer.value[0]) & mask);
  354. val2 = ((max - ucontrol->value.integer.value[1]) & mask);
  355. val = val << shift;
  356. val2 = val2 << shift;
  357. err = snd_soc_update_bits(codec, reg, val_mask, val);
  358. if (err < 0)
  359. return err;
  360. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  361. return err;
  362. }
  363. /* DAPM Widget Events */
  364. /*
  365. * A lot registers are belong to RSYNC domain. It requires enabling RSYNC bit
  366. * after updating these registers. Otherwise, these updated registers won't
  367. * be effective.
  368. */
  369. static int pm860x_rsync_event(struct snd_soc_dapm_widget *w,
  370. struct snd_kcontrol *kcontrol, int event)
  371. {
  372. struct snd_soc_codec *codec = w->codec;
  373. /*
  374. * In order to avoid current on the load, mute power-on and power-off
  375. * should be transients.
  376. * Unmute by DAC_MUTE. It should be unmuted when DAPM sequence is
  377. * finished.
  378. */
  379. snd_soc_update_bits(codec, PM860X_DAC_OFFSET, DAC_MUTE, 0);
  380. snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
  381. RSYNC_CHANGE, RSYNC_CHANGE);
  382. return 0;
  383. }
  384. static int pm860x_dac_event(struct snd_soc_dapm_widget *w,
  385. struct snd_kcontrol *kcontrol, int event)
  386. {
  387. struct snd_soc_codec *codec = w->codec;
  388. unsigned int dac = 0;
  389. int data;
  390. if (!strcmp(w->name, "Left DAC"))
  391. dac = DAC_LEFT;
  392. if (!strcmp(w->name, "Right DAC"))
  393. dac = DAC_RIGHT;
  394. switch (event) {
  395. case SND_SOC_DAPM_PRE_PMU:
  396. if (dac) {
  397. /* Auto mute in power-on sequence. */
  398. dac |= MODULATOR;
  399. snd_soc_update_bits(codec, PM860X_DAC_OFFSET,
  400. DAC_MUTE, DAC_MUTE);
  401. snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
  402. RSYNC_CHANGE, RSYNC_CHANGE);
  403. /* update dac */
  404. snd_soc_update_bits(codec, PM860X_DAC_EN_2,
  405. dac, dac);
  406. }
  407. break;
  408. case SND_SOC_DAPM_PRE_PMD:
  409. if (dac) {
  410. /* Auto mute in power-off sequence. */
  411. snd_soc_update_bits(codec, PM860X_DAC_OFFSET,
  412. DAC_MUTE, DAC_MUTE);
  413. snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
  414. RSYNC_CHANGE, RSYNC_CHANGE);
  415. /* update dac */
  416. data = snd_soc_read(codec, PM860X_DAC_EN_2);
  417. data &= ~dac;
  418. if (!(data & (DAC_LEFT | DAC_RIGHT)))
  419. data &= ~MODULATOR;
  420. snd_soc_write(codec, PM860X_DAC_EN_2, data);
  421. }
  422. break;
  423. }
  424. return 0;
  425. }
  426. static const char *pm860x_opamp_texts[] = {"-50%", "-25%", "0%", "75%"};
  427. static const char *pm860x_pa_texts[] = {"-33%", "0%", "33%", "66%"};
  428. static const struct soc_enum pm860x_hs1_opamp_enum =
  429. SOC_ENUM_SINGLE(PM860X_HS1_CTRL, 5, 4, pm860x_opamp_texts);
  430. static const struct soc_enum pm860x_hs2_opamp_enum =
  431. SOC_ENUM_SINGLE(PM860X_HS2_CTRL, 5, 4, pm860x_opamp_texts);
  432. static const struct soc_enum pm860x_hs1_pa_enum =
  433. SOC_ENUM_SINGLE(PM860X_HS1_CTRL, 3, 4, pm860x_pa_texts);
  434. static const struct soc_enum pm860x_hs2_pa_enum =
  435. SOC_ENUM_SINGLE(PM860X_HS2_CTRL, 3, 4, pm860x_pa_texts);
  436. static const struct soc_enum pm860x_lo1_opamp_enum =
  437. SOC_ENUM_SINGLE(PM860X_LO1_CTRL, 5, 4, pm860x_opamp_texts);
  438. static const struct soc_enum pm860x_lo2_opamp_enum =
  439. SOC_ENUM_SINGLE(PM860X_LO2_CTRL, 5, 4, pm860x_opamp_texts);
  440. static const struct soc_enum pm860x_lo1_pa_enum =
  441. SOC_ENUM_SINGLE(PM860X_LO1_CTRL, 3, 4, pm860x_pa_texts);
  442. static const struct soc_enum pm860x_lo2_pa_enum =
  443. SOC_ENUM_SINGLE(PM860X_LO2_CTRL, 3, 4, pm860x_pa_texts);
  444. static const struct soc_enum pm860x_spk_pa_enum =
  445. SOC_ENUM_SINGLE(PM860X_EAR_CTRL_1, 5, 4, pm860x_pa_texts);
  446. static const struct soc_enum pm860x_ear_pa_enum =
  447. SOC_ENUM_SINGLE(PM860X_EAR_CTRL_2, 0, 4, pm860x_pa_texts);
  448. static const struct soc_enum pm860x_spk_ear_opamp_enum =
  449. SOC_ENUM_SINGLE(PM860X_EAR_CTRL_1, 3, 4, pm860x_opamp_texts);
  450. static const struct snd_kcontrol_new pm860x_snd_controls[] = {
  451. SOC_DOUBLE_R_TLV("ADC Capture Volume", PM860X_ADC_ANA_2,
  452. PM860X_ADC_ANA_3, 6, 3, 0, adc_tlv),
  453. SOC_DOUBLE_TLV("AUX Capture Volume", PM860X_ADC_ANA_3, 0, 3, 7, 0,
  454. aux_tlv),
  455. SOC_SINGLE_TLV("MIC1 Capture Volume", PM860X_ADC_ANA_2, 0, 7, 0,
  456. mic_tlv),
  457. SOC_SINGLE_TLV("MIC3 Capture Volume", PM860X_ADC_ANA_2, 3, 7, 0,
  458. mic_tlv),
  459. SOC_DOUBLE_R_EXT_TLV("Sidetone Volume", PM860X_SIDETONE_L_GAIN,
  460. PM860X_SIDETONE_R_GAIN, 0, ARRAY_SIZE(st_table)-1,
  461. 0, snd_soc_get_volsw_2r_st,
  462. snd_soc_put_volsw_2r_st, st_tlv),
  463. SOC_SINGLE_TLV("Speaker Playback Volume", PM860X_EAR_CTRL_1,
  464. 0, 7, 0, out_tlv),
  465. SOC_DOUBLE_R_TLV("Line Playback Volume", PM860X_LO1_CTRL,
  466. PM860X_LO2_CTRL, 0, 7, 0, out_tlv),
  467. SOC_DOUBLE_R_TLV("Headset Playback Volume", PM860X_HS1_CTRL,
  468. PM860X_HS2_CTRL, 0, 7, 0, out_tlv),
  469. SOC_DOUBLE_R_EXT_TLV("Hifi Left Playback Volume",
  470. PM860X_HIFIL_GAIN_LEFT,
  471. PM860X_HIFIL_GAIN_RIGHT, 0, 63, 0,
  472. snd_soc_get_volsw_2r_out,
  473. snd_soc_put_volsw_2r_out, dpga_tlv),
  474. SOC_DOUBLE_R_EXT_TLV("Hifi Right Playback Volume",
  475. PM860X_HIFIR_GAIN_LEFT,
  476. PM860X_HIFIR_GAIN_RIGHT, 0, 63, 0,
  477. snd_soc_get_volsw_2r_out,
  478. snd_soc_put_volsw_2r_out, dpga_tlv),
  479. SOC_DOUBLE_R_EXT_TLV("Lofi Playback Volume", PM860X_LOFI_GAIN_LEFT,
  480. PM860X_LOFI_GAIN_RIGHT, 0, 63, 0,
  481. snd_soc_get_volsw_2r_out,
  482. snd_soc_put_volsw_2r_out, dpga_tlv),
  483. SOC_ENUM("Headset1 Operational Amplifier Current",
  484. pm860x_hs1_opamp_enum),
  485. SOC_ENUM("Headset2 Operational Amplifier Current",
  486. pm860x_hs2_opamp_enum),
  487. SOC_ENUM("Headset1 Amplifier Current", pm860x_hs1_pa_enum),
  488. SOC_ENUM("Headset2 Amplifier Current", pm860x_hs2_pa_enum),
  489. SOC_ENUM("Lineout1 Operational Amplifier Current",
  490. pm860x_lo1_opamp_enum),
  491. SOC_ENUM("Lineout2 Operational Amplifier Current",
  492. pm860x_lo2_opamp_enum),
  493. SOC_ENUM("Lineout1 Amplifier Current", pm860x_lo1_pa_enum),
  494. SOC_ENUM("Lineout2 Amplifier Current", pm860x_lo2_pa_enum),
  495. SOC_ENUM("Speaker Operational Amplifier Current",
  496. pm860x_spk_ear_opamp_enum),
  497. SOC_ENUM("Speaker Amplifier Current", pm860x_spk_pa_enum),
  498. SOC_ENUM("Earpiece Amplifier Current", pm860x_ear_pa_enum),
  499. };
  500. /*
  501. * DAPM Controls
  502. */
  503. /* PCM Switch / PCM Interface */
  504. static const struct snd_kcontrol_new pcm_switch_controls =
  505. SOC_DAPM_SINGLE("Switch", PM860X_ADC_EN_2, 0, 1, 0);
  506. /* AUX1 Switch */
  507. static const struct snd_kcontrol_new aux1_switch_controls =
  508. SOC_DAPM_SINGLE("Switch", PM860X_ANA_TO_ANA, 4, 1, 0);
  509. /* AUX2 Switch */
  510. static const struct snd_kcontrol_new aux2_switch_controls =
  511. SOC_DAPM_SINGLE("Switch", PM860X_ANA_TO_ANA, 5, 1, 0);
  512. /* Left Ex. PA Switch */
  513. static const struct snd_kcontrol_new lepa_switch_controls =
  514. SOC_DAPM_SINGLE("Switch", PM860X_DAC_EN_2, 2, 1, 0);
  515. /* Right Ex. PA Switch */
  516. static const struct snd_kcontrol_new repa_switch_controls =
  517. SOC_DAPM_SINGLE("Switch", PM860X_DAC_EN_2, 1, 1, 0);
  518. /* PCM Mux / Mux7 */
  519. static const char *aif1_text[] = {
  520. "PCM L", "PCM R",
  521. };
  522. static const struct soc_enum aif1_enum =
  523. SOC_ENUM_SINGLE(PM860X_PCM_IFACE_3, 6, 2, aif1_text);
  524. static const struct snd_kcontrol_new aif1_mux =
  525. SOC_DAPM_ENUM("PCM Mux", aif1_enum);
  526. /* I2S Mux / Mux9 */
  527. static const char *i2s_din_text[] = {
  528. "DIN", "DIN1",
  529. };
  530. static const struct soc_enum i2s_din_enum =
  531. SOC_ENUM_SINGLE(PM860X_I2S_IFACE_3, 1, 2, i2s_din_text);
  532. static const struct snd_kcontrol_new i2s_din_mux =
  533. SOC_DAPM_ENUM("I2S DIN Mux", i2s_din_enum);
  534. /* I2S Mic Mux / Mux8 */
  535. static const char *i2s_mic_text[] = {
  536. "Ex PA", "ADC",
  537. };
  538. static const struct soc_enum i2s_mic_enum =
  539. SOC_ENUM_SINGLE(PM860X_I2S_IFACE_3, 4, 2, i2s_mic_text);
  540. static const struct snd_kcontrol_new i2s_mic_mux =
  541. SOC_DAPM_ENUM("I2S Mic Mux", i2s_mic_enum);
  542. /* ADCL Mux / Mux2 */
  543. static const char *adcl_text[] = {
  544. "ADCR", "ADCL",
  545. };
  546. static const struct soc_enum adcl_enum =
  547. SOC_ENUM_SINGLE(PM860X_PCM_IFACE_3, 4, 2, adcl_text);
  548. static const struct snd_kcontrol_new adcl_mux =
  549. SOC_DAPM_ENUM("ADC Left Mux", adcl_enum);
  550. /* ADCR Mux / Mux3 */
  551. static const char *adcr_text[] = {
  552. "ADCL", "ADCR",
  553. };
  554. static const struct soc_enum adcr_enum =
  555. SOC_ENUM_SINGLE(PM860X_PCM_IFACE_3, 2, 2, adcr_text);
  556. static const struct snd_kcontrol_new adcr_mux =
  557. SOC_DAPM_ENUM("ADC Right Mux", adcr_enum);
  558. /* ADCR EC Mux / Mux6 */
  559. static const char *adcr_ec_text[] = {
  560. "ADCR", "EC",
  561. };
  562. static const struct soc_enum adcr_ec_enum =
  563. SOC_ENUM_SINGLE(PM860X_ADC_EN_2, 3, 2, adcr_ec_text);
  564. static const struct snd_kcontrol_new adcr_ec_mux =
  565. SOC_DAPM_ENUM("ADCR EC Mux", adcr_ec_enum);
  566. /* EC Mux / Mux4 */
  567. static const char *ec_text[] = {
  568. "Left", "Right", "Left + Right",
  569. };
  570. static const struct soc_enum ec_enum =
  571. SOC_ENUM_SINGLE(PM860X_EC_PATH, 1, 3, ec_text);
  572. static const struct snd_kcontrol_new ec_mux =
  573. SOC_DAPM_ENUM("EC Mux", ec_enum);
  574. static const char *dac_text[] = {
  575. "No input", "Right", "Left", "No input",
  576. };
  577. /* DAC Headset 1 Mux / Mux10 */
  578. static const struct soc_enum dac_hs1_enum =
  579. SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 0, 4, dac_text);
  580. static const struct snd_kcontrol_new dac_hs1_mux =
  581. SOC_DAPM_ENUM("DAC HS1 Mux", dac_hs1_enum);
  582. /* DAC Headset 2 Mux / Mux11 */
  583. static const struct soc_enum dac_hs2_enum =
  584. SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 2, 4, dac_text);
  585. static const struct snd_kcontrol_new dac_hs2_mux =
  586. SOC_DAPM_ENUM("DAC HS2 Mux", dac_hs2_enum);
  587. /* DAC Lineout 1 Mux / Mux12 */
  588. static const struct soc_enum dac_lo1_enum =
  589. SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 4, 4, dac_text);
  590. static const struct snd_kcontrol_new dac_lo1_mux =
  591. SOC_DAPM_ENUM("DAC LO1 Mux", dac_lo1_enum);
  592. /* DAC Lineout 2 Mux / Mux13 */
  593. static const struct soc_enum dac_lo2_enum =
  594. SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_1, 6, 4, dac_text);
  595. static const struct snd_kcontrol_new dac_lo2_mux =
  596. SOC_DAPM_ENUM("DAC LO2 Mux", dac_lo2_enum);
  597. /* DAC Spearker Earphone Mux / Mux14 */
  598. static const struct soc_enum dac_spk_ear_enum =
  599. SOC_ENUM_SINGLE(PM860X_ANA_INPUT_SEL_2, 0, 4, dac_text);
  600. static const struct snd_kcontrol_new dac_spk_ear_mux =
  601. SOC_DAPM_ENUM("DAC SP Mux", dac_spk_ear_enum);
  602. /* Headset 1 Mux / Mux15 */
  603. static const char *in_text[] = {
  604. "Digital", "Analog",
  605. };
  606. static const struct soc_enum hs1_enum =
  607. SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 0, 2, in_text);
  608. static const struct snd_kcontrol_new hs1_mux =
  609. SOC_DAPM_ENUM("Headset1 Mux", hs1_enum);
  610. /* Headset 2 Mux / Mux16 */
  611. static const struct soc_enum hs2_enum =
  612. SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 1, 2, in_text);
  613. static const struct snd_kcontrol_new hs2_mux =
  614. SOC_DAPM_ENUM("Headset2 Mux", hs2_enum);
  615. /* Lineout 1 Mux / Mux17 */
  616. static const struct soc_enum lo1_enum =
  617. SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 2, 2, in_text);
  618. static const struct snd_kcontrol_new lo1_mux =
  619. SOC_DAPM_ENUM("Lineout1 Mux", lo1_enum);
  620. /* Lineout 2 Mux / Mux18 */
  621. static const struct soc_enum lo2_enum =
  622. SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 3, 2, in_text);
  623. static const struct snd_kcontrol_new lo2_mux =
  624. SOC_DAPM_ENUM("Lineout2 Mux", lo2_enum);
  625. /* Speaker Earpiece Demux */
  626. static const char *spk_text[] = {
  627. "Earpiece", "Speaker",
  628. };
  629. static const struct soc_enum spk_enum =
  630. SOC_ENUM_SINGLE(PM860X_ANA_TO_ANA, 6, 2, spk_text);
  631. static const struct snd_kcontrol_new spk_demux =
  632. SOC_DAPM_ENUM("Speaker Earpiece Demux", spk_enum);
  633. /* MIC Mux / Mux1 */
  634. static const char *mic_text[] = {
  635. "Mic 1", "Mic 2",
  636. };
  637. static const struct soc_enum mic_enum =
  638. SOC_ENUM_SINGLE(PM860X_ADC_ANA_4, 4, 2, mic_text);
  639. static const struct snd_kcontrol_new mic_mux =
  640. SOC_DAPM_ENUM("MIC Mux", mic_enum);
  641. static const struct snd_soc_dapm_widget pm860x_dapm_widgets[] = {
  642. SND_SOC_DAPM_AIF_IN("PCM SDI", "PCM Playback", 0,
  643. PM860X_ADC_EN_2, 0, 0),
  644. SND_SOC_DAPM_AIF_OUT("PCM SDO", "PCM Capture", 0,
  645. PM860X_PCM_IFACE_3, 1, 1),
  646. SND_SOC_DAPM_AIF_IN("I2S DIN", "I2S Playback", 0,
  647. SND_SOC_NOPM, 0, 0),
  648. SND_SOC_DAPM_AIF_IN("I2S DIN1", "I2S Playback", 0,
  649. SND_SOC_NOPM, 0, 0),
  650. SND_SOC_DAPM_AIF_OUT("I2S DOUT", "I2S Capture", 0,
  651. PM860X_I2S_IFACE_3, 5, 1),
  652. SND_SOC_DAPM_SUPPLY("I2S CLK", PM860X_DAC_EN_2, 0, 0, NULL, 0),
  653. SND_SOC_DAPM_MUX("I2S Mic Mux", SND_SOC_NOPM, 0, 0, &i2s_mic_mux),
  654. SND_SOC_DAPM_MUX("ADC Left Mux", SND_SOC_NOPM, 0, 0, &adcl_mux),
  655. SND_SOC_DAPM_MUX("ADC Right Mux", SND_SOC_NOPM, 0, 0, &adcr_mux),
  656. SND_SOC_DAPM_MUX("EC Mux", SND_SOC_NOPM, 0, 0, &ec_mux),
  657. SND_SOC_DAPM_MUX("ADCR EC Mux", SND_SOC_NOPM, 0, 0, &adcr_ec_mux),
  658. SND_SOC_DAPM_SWITCH("Left EPA", SND_SOC_NOPM, 0, 0,
  659. &lepa_switch_controls),
  660. SND_SOC_DAPM_SWITCH("Right EPA", SND_SOC_NOPM, 0, 0,
  661. &repa_switch_controls),
  662. SND_SOC_DAPM_REG(snd_soc_dapm_supply, "Left ADC MOD", PM860X_ADC_EN_1,
  663. 0, 1, 1, 0),
  664. SND_SOC_DAPM_REG(snd_soc_dapm_supply, "Right ADC MOD", PM860X_ADC_EN_1,
  665. 1, 1, 1, 0),
  666. SND_SOC_DAPM_ADC("Left ADC", NULL, PM860X_ADC_EN_2, 5, 0),
  667. SND_SOC_DAPM_ADC("Right ADC", NULL, PM860X_ADC_EN_2, 4, 0),
  668. SND_SOC_DAPM_SWITCH("AUX1 Switch", SND_SOC_NOPM, 0, 0,
  669. &aux1_switch_controls),
  670. SND_SOC_DAPM_SWITCH("AUX2 Switch", SND_SOC_NOPM, 0, 0,
  671. &aux2_switch_controls),
  672. SND_SOC_DAPM_MUX("MIC Mux", SND_SOC_NOPM, 0, 0, &mic_mux),
  673. SND_SOC_DAPM_MICBIAS("Mic1 Bias", PM860X_ADC_ANA_1, 2, 0),
  674. SND_SOC_DAPM_MICBIAS("Mic3 Bias", PM860X_ADC_ANA_1, 7, 0),
  675. SND_SOC_DAPM_PGA("MIC1 Volume", PM860X_ADC_EN_1, 2, 0, NULL, 0),
  676. SND_SOC_DAPM_PGA("MIC3 Volume", PM860X_ADC_EN_1, 3, 0, NULL, 0),
  677. SND_SOC_DAPM_PGA("AUX1 Volume", PM860X_ADC_EN_1, 4, 0, NULL, 0),
  678. SND_SOC_DAPM_PGA("AUX2 Volume", PM860X_ADC_EN_1, 5, 0, NULL, 0),
  679. SND_SOC_DAPM_PGA("Sidetone PGA", PM860X_ADC_EN_2, 1, 0, NULL, 0),
  680. SND_SOC_DAPM_PGA("Lofi PGA", PM860X_ADC_EN_2, 2, 0, NULL, 0),
  681. SND_SOC_DAPM_INPUT("AUX1"),
  682. SND_SOC_DAPM_INPUT("AUX2"),
  683. SND_SOC_DAPM_INPUT("MIC1P"),
  684. SND_SOC_DAPM_INPUT("MIC1N"),
  685. SND_SOC_DAPM_INPUT("MIC2P"),
  686. SND_SOC_DAPM_INPUT("MIC2N"),
  687. SND_SOC_DAPM_INPUT("MIC3P"),
  688. SND_SOC_DAPM_INPUT("MIC3N"),
  689. SND_SOC_DAPM_DAC_E("Left DAC", NULL, SND_SOC_NOPM, 0, 0,
  690. pm860x_dac_event,
  691. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  692. SND_SOC_DAPM_DAC_E("Right DAC", NULL, SND_SOC_NOPM, 0, 0,
  693. pm860x_dac_event,
  694. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD),
  695. SND_SOC_DAPM_MUX("I2S DIN Mux", SND_SOC_NOPM, 0, 0, &i2s_din_mux),
  696. SND_SOC_DAPM_MUX("DAC HS1 Mux", SND_SOC_NOPM, 0, 0, &dac_hs1_mux),
  697. SND_SOC_DAPM_MUX("DAC HS2 Mux", SND_SOC_NOPM, 0, 0, &dac_hs2_mux),
  698. SND_SOC_DAPM_MUX("DAC LO1 Mux", SND_SOC_NOPM, 0, 0, &dac_lo1_mux),
  699. SND_SOC_DAPM_MUX("DAC LO2 Mux", SND_SOC_NOPM, 0, 0, &dac_lo2_mux),
  700. SND_SOC_DAPM_MUX("DAC SP Mux", SND_SOC_NOPM, 0, 0, &dac_spk_ear_mux),
  701. SND_SOC_DAPM_MUX("Headset1 Mux", SND_SOC_NOPM, 0, 0, &hs1_mux),
  702. SND_SOC_DAPM_MUX("Headset2 Mux", SND_SOC_NOPM, 0, 0, &hs2_mux),
  703. SND_SOC_DAPM_MUX("Lineout1 Mux", SND_SOC_NOPM, 0, 0, &lo1_mux),
  704. SND_SOC_DAPM_MUX("Lineout2 Mux", SND_SOC_NOPM, 0, 0, &lo2_mux),
  705. SND_SOC_DAPM_MUX("Speaker Earpiece Demux", SND_SOC_NOPM, 0, 0,
  706. &spk_demux),
  707. SND_SOC_DAPM_PGA("Headset1 PGA", PM860X_DAC_EN_1, 0, 0, NULL, 0),
  708. SND_SOC_DAPM_PGA("Headset2 PGA", PM860X_DAC_EN_1, 1, 0, NULL, 0),
  709. SND_SOC_DAPM_OUTPUT("HS1"),
  710. SND_SOC_DAPM_OUTPUT("HS2"),
  711. SND_SOC_DAPM_PGA("Lineout1 PGA", PM860X_DAC_EN_1, 2, 0, NULL, 0),
  712. SND_SOC_DAPM_PGA("Lineout2 PGA", PM860X_DAC_EN_1, 3, 0, NULL, 0),
  713. SND_SOC_DAPM_OUTPUT("LINEOUT1"),
  714. SND_SOC_DAPM_OUTPUT("LINEOUT2"),
  715. SND_SOC_DAPM_PGA("Earpiece PGA", PM860X_DAC_EN_1, 4, 0, NULL, 0),
  716. SND_SOC_DAPM_OUTPUT("EARP"),
  717. SND_SOC_DAPM_OUTPUT("EARN"),
  718. SND_SOC_DAPM_PGA("Speaker PGA", PM860X_DAC_EN_1, 5, 0, NULL, 0),
  719. SND_SOC_DAPM_OUTPUT("LSP"),
  720. SND_SOC_DAPM_OUTPUT("LSN"),
  721. SND_SOC_DAPM_REG(snd_soc_dapm_supply, "VCODEC", PM860X_AUDIO_SUPPLIES_2,
  722. 0, SUPPLY_MASK, SUPPLY_MASK, 0),
  723. PM860X_DAPM_OUTPUT("RSYNC", pm860x_rsync_event),
  724. };
  725. static const struct snd_soc_dapm_route pm860x_dapm_routes[] = {
  726. /* supply */
  727. {"Left DAC", NULL, "VCODEC"},
  728. {"Right DAC", NULL, "VCODEC"},
  729. {"Left ADC", NULL, "VCODEC"},
  730. {"Right ADC", NULL, "VCODEC"},
  731. {"Left ADC", NULL, "Left ADC MOD"},
  732. {"Right ADC", NULL, "Right ADC MOD"},
  733. /* I2S Clock */
  734. {"I2S DIN", NULL, "I2S CLK"},
  735. {"I2S DIN1", NULL, "I2S CLK"},
  736. {"I2S DOUT", NULL, "I2S CLK"},
  737. /* PCM/AIF1 Inputs */
  738. {"PCM SDO", NULL, "ADC Left Mux"},
  739. {"PCM SDO", NULL, "ADCR EC Mux"},
  740. /* PCM/AFI2 Outputs */
  741. {"Lofi PGA", NULL, "PCM SDI"},
  742. {"Lofi PGA", NULL, "Sidetone PGA"},
  743. {"Left DAC", NULL, "Lofi PGA"},
  744. {"Right DAC", NULL, "Lofi PGA"},
  745. /* I2S/AIF2 Inputs */
  746. {"MIC Mux", "Mic 1", "MIC1P"},
  747. {"MIC Mux", "Mic 1", "MIC1N"},
  748. {"MIC Mux", "Mic 2", "MIC2P"},
  749. {"MIC Mux", "Mic 2", "MIC2N"},
  750. {"MIC1 Volume", NULL, "MIC Mux"},
  751. {"MIC3 Volume", NULL, "MIC3P"},
  752. {"MIC3 Volume", NULL, "MIC3N"},
  753. {"Left ADC", NULL, "MIC1 Volume"},
  754. {"Right ADC", NULL, "MIC3 Volume"},
  755. {"ADC Left Mux", "ADCR", "Right ADC"},
  756. {"ADC Left Mux", "ADCL", "Left ADC"},
  757. {"ADC Right Mux", "ADCL", "Left ADC"},
  758. {"ADC Right Mux", "ADCR", "Right ADC"},
  759. {"Left EPA", "Switch", "Left DAC"},
  760. {"Right EPA", "Switch", "Right DAC"},
  761. {"EC Mux", "Left", "Left DAC"},
  762. {"EC Mux", "Right", "Right DAC"},
  763. {"EC Mux", "Left + Right", "Left DAC"},
  764. {"EC Mux", "Left + Right", "Right DAC"},
  765. {"ADCR EC Mux", "ADCR", "ADC Right Mux"},
  766. {"ADCR EC Mux", "EC", "EC Mux"},
  767. {"I2S Mic Mux", "Ex PA", "Left EPA"},
  768. {"I2S Mic Mux", "Ex PA", "Right EPA"},
  769. {"I2S Mic Mux", "ADC", "ADC Left Mux"},
  770. {"I2S Mic Mux", "ADC", "ADCR EC Mux"},
  771. {"I2S DOUT", NULL, "I2S Mic Mux"},
  772. /* I2S/AIF2 Outputs */
  773. {"I2S DIN Mux", "DIN", "I2S DIN"},
  774. {"I2S DIN Mux", "DIN1", "I2S DIN1"},
  775. {"Left DAC", NULL, "I2S DIN Mux"},
  776. {"Right DAC", NULL, "I2S DIN Mux"},
  777. {"DAC HS1 Mux", "Left", "Left DAC"},
  778. {"DAC HS1 Mux", "Right", "Right DAC"},
  779. {"DAC HS2 Mux", "Left", "Left DAC"},
  780. {"DAC HS2 Mux", "Right", "Right DAC"},
  781. {"DAC LO1 Mux", "Left", "Left DAC"},
  782. {"DAC LO1 Mux", "Right", "Right DAC"},
  783. {"DAC LO2 Mux", "Left", "Left DAC"},
  784. {"DAC LO2 Mux", "Right", "Right DAC"},
  785. {"Headset1 Mux", "Digital", "DAC HS1 Mux"},
  786. {"Headset2 Mux", "Digital", "DAC HS2 Mux"},
  787. {"Lineout1 Mux", "Digital", "DAC LO1 Mux"},
  788. {"Lineout2 Mux", "Digital", "DAC LO2 Mux"},
  789. {"Headset1 PGA", NULL, "Headset1 Mux"},
  790. {"Headset2 PGA", NULL, "Headset2 Mux"},
  791. {"Lineout1 PGA", NULL, "Lineout1 Mux"},
  792. {"Lineout2 PGA", NULL, "Lineout2 Mux"},
  793. {"DAC SP Mux", "Left", "Left DAC"},
  794. {"DAC SP Mux", "Right", "Right DAC"},
  795. {"Speaker Earpiece Demux", "Speaker", "DAC SP Mux"},
  796. {"Speaker PGA", NULL, "Speaker Earpiece Demux"},
  797. {"Earpiece PGA", NULL, "Speaker Earpiece Demux"},
  798. {"RSYNC", NULL, "Headset1 PGA"},
  799. {"RSYNC", NULL, "Headset2 PGA"},
  800. {"RSYNC", NULL, "Lineout1 PGA"},
  801. {"RSYNC", NULL, "Lineout2 PGA"},
  802. {"RSYNC", NULL, "Speaker PGA"},
  803. {"RSYNC", NULL, "Speaker PGA"},
  804. {"RSYNC", NULL, "Earpiece PGA"},
  805. {"RSYNC", NULL, "Earpiece PGA"},
  806. {"HS1", NULL, "RSYNC"},
  807. {"HS2", NULL, "RSYNC"},
  808. {"LINEOUT1", NULL, "RSYNC"},
  809. {"LINEOUT2", NULL, "RSYNC"},
  810. {"LSP", NULL, "RSYNC"},
  811. {"LSN", NULL, "RSYNC"},
  812. {"EARP", NULL, "RSYNC"},
  813. {"EARN", NULL, "RSYNC"},
  814. };
  815. /*
  816. * Use MUTE_LEFT & MUTE_RIGHT to implement digital mute.
  817. * These bits can also be used to mute.
  818. */
  819. static int pm860x_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  820. {
  821. struct snd_soc_codec *codec = codec_dai->codec;
  822. int data = 0, mask = MUTE_LEFT | MUTE_RIGHT;
  823. if (mute)
  824. data = mask;
  825. snd_soc_update_bits(codec, PM860X_DAC_OFFSET, mask, data);
  826. snd_soc_update_bits(codec, PM860X_EAR_CTRL_2,
  827. RSYNC_CHANGE, RSYNC_CHANGE);
  828. return 0;
  829. }
  830. static int pm860x_pcm_hw_params(struct snd_pcm_substream *substream,
  831. struct snd_pcm_hw_params *params,
  832. struct snd_soc_dai *dai)
  833. {
  834. struct snd_soc_codec *codec = dai->codec;
  835. unsigned char inf = 0, mask = 0;
  836. /* bit size */
  837. switch (params_format(params)) {
  838. case SNDRV_PCM_FORMAT_S16_LE:
  839. inf &= ~PCM_INF2_18WL;
  840. break;
  841. case SNDRV_PCM_FORMAT_S18_3LE:
  842. inf |= PCM_INF2_18WL;
  843. break;
  844. default:
  845. return -EINVAL;
  846. }
  847. mask |= PCM_INF2_18WL;
  848. snd_soc_update_bits(codec, PM860X_PCM_IFACE_2, mask, inf);
  849. /* sample rate */
  850. switch (params_rate(params)) {
  851. case 8000:
  852. inf = 0;
  853. break;
  854. case 16000:
  855. inf = 3;
  856. break;
  857. case 32000:
  858. inf = 6;
  859. break;
  860. case 48000:
  861. inf = 8;
  862. break;
  863. default:
  864. return -EINVAL;
  865. }
  866. snd_soc_update_bits(codec, PM860X_PCM_RATE, 0x0f, inf);
  867. return 0;
  868. }
  869. static int pm860x_pcm_set_dai_fmt(struct snd_soc_dai *codec_dai,
  870. unsigned int fmt)
  871. {
  872. struct snd_soc_codec *codec = codec_dai->codec;
  873. struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
  874. unsigned char inf = 0, mask = 0;
  875. int ret = -EINVAL;
  876. mask |= PCM_INF2_BCLK | PCM_INF2_FS | PCM_INF2_MASTER;
  877. /* set master/slave audio interface */
  878. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  879. case SND_SOC_DAIFMT_CBM_CFM:
  880. case SND_SOC_DAIFMT_CBM_CFS:
  881. if (pm860x->dir == PM860X_CLK_DIR_OUT) {
  882. inf |= PCM_INF2_MASTER;
  883. ret = 0;
  884. }
  885. break;
  886. case SND_SOC_DAIFMT_CBS_CFS:
  887. if (pm860x->dir == PM860X_CLK_DIR_IN) {
  888. inf &= ~PCM_INF2_MASTER;
  889. ret = 0;
  890. }
  891. break;
  892. }
  893. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  894. case SND_SOC_DAIFMT_I2S:
  895. inf |= PCM_EXACT_I2S;
  896. ret = 0;
  897. break;
  898. }
  899. mask |= PCM_MODE_MASK;
  900. if (ret)
  901. return ret;
  902. snd_soc_update_bits(codec, PM860X_PCM_IFACE_2, mask, inf);
  903. return 0;
  904. }
  905. static int pm860x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  906. int clk_id, unsigned int freq, int dir)
  907. {
  908. struct snd_soc_codec *codec = codec_dai->codec;
  909. struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
  910. if (dir == PM860X_CLK_DIR_OUT)
  911. pm860x->dir = PM860X_CLK_DIR_OUT;
  912. else {
  913. pm860x->dir = PM860X_CLK_DIR_IN;
  914. return -EINVAL;
  915. }
  916. return 0;
  917. }
  918. static int pm860x_i2s_hw_params(struct snd_pcm_substream *substream,
  919. struct snd_pcm_hw_params *params,
  920. struct snd_soc_dai *dai)
  921. {
  922. struct snd_soc_codec *codec = dai->codec;
  923. unsigned char inf;
  924. /* bit size */
  925. switch (params_format(params)) {
  926. case SNDRV_PCM_FORMAT_S16_LE:
  927. inf = 0;
  928. break;
  929. case SNDRV_PCM_FORMAT_S18_3LE:
  930. inf = PCM_INF2_18WL;
  931. break;
  932. default:
  933. return -EINVAL;
  934. }
  935. snd_soc_update_bits(codec, PM860X_I2S_IFACE_2, PCM_INF2_18WL, inf);
  936. /* sample rate */
  937. switch (params_rate(params)) {
  938. case 8000:
  939. inf = 0;
  940. break;
  941. case 11025:
  942. inf = 1;
  943. break;
  944. case 16000:
  945. inf = 3;
  946. break;
  947. case 22050:
  948. inf = 4;
  949. break;
  950. case 32000:
  951. inf = 6;
  952. break;
  953. case 44100:
  954. inf = 7;
  955. break;
  956. case 48000:
  957. inf = 8;
  958. break;
  959. default:
  960. return -EINVAL;
  961. }
  962. snd_soc_update_bits(codec, PM860X_I2S_IFACE_4, 0xf, inf);
  963. return 0;
  964. }
  965. static int pm860x_i2s_set_dai_fmt(struct snd_soc_dai *codec_dai,
  966. unsigned int fmt)
  967. {
  968. struct snd_soc_codec *codec = codec_dai->codec;
  969. struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
  970. unsigned char inf = 0, mask = 0;
  971. mask |= PCM_INF2_BCLK | PCM_INF2_FS | PCM_INF2_MASTER;
  972. /* set master/slave audio interface */
  973. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  974. case SND_SOC_DAIFMT_CBM_CFM:
  975. if (pm860x->dir == PM860X_CLK_DIR_OUT)
  976. inf |= PCM_INF2_MASTER;
  977. else
  978. return -EINVAL;
  979. break;
  980. case SND_SOC_DAIFMT_CBS_CFS:
  981. if (pm860x->dir == PM860X_CLK_DIR_IN)
  982. inf &= ~PCM_INF2_MASTER;
  983. else
  984. return -EINVAL;
  985. break;
  986. default:
  987. return -EINVAL;
  988. }
  989. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  990. case SND_SOC_DAIFMT_I2S:
  991. inf |= PCM_EXACT_I2S;
  992. break;
  993. default:
  994. return -EINVAL;
  995. }
  996. mask |= PCM_MODE_MASK;
  997. snd_soc_update_bits(codec, PM860X_I2S_IFACE_2, mask, inf);
  998. return 0;
  999. }
  1000. static int pm860x_set_bias_level(struct snd_soc_codec *codec,
  1001. enum snd_soc_bias_level level)
  1002. {
  1003. int data;
  1004. switch (level) {
  1005. case SND_SOC_BIAS_ON:
  1006. break;
  1007. case SND_SOC_BIAS_PREPARE:
  1008. break;
  1009. case SND_SOC_BIAS_STANDBY:
  1010. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
  1011. /* Enable Audio PLL & Audio section */
  1012. data = AUDIO_PLL | AUDIO_SECTION_ON;
  1013. pm860x_reg_write(codec->control_data, REG_MISC2, data);
  1014. udelay(300);
  1015. data = AUDIO_PLL | AUDIO_SECTION_RESET
  1016. | AUDIO_SECTION_ON;
  1017. pm860x_reg_write(codec->control_data, REG_MISC2, data);
  1018. }
  1019. break;
  1020. case SND_SOC_BIAS_OFF:
  1021. data = AUDIO_PLL | AUDIO_SECTION_RESET | AUDIO_SECTION_ON;
  1022. pm860x_set_bits(codec->control_data, REG_MISC2, data, 0);
  1023. break;
  1024. }
  1025. codec->dapm.bias_level = level;
  1026. return 0;
  1027. }
  1028. static const struct snd_soc_dai_ops pm860x_pcm_dai_ops = {
  1029. .digital_mute = pm860x_digital_mute,
  1030. .hw_params = pm860x_pcm_hw_params,
  1031. .set_fmt = pm860x_pcm_set_dai_fmt,
  1032. .set_sysclk = pm860x_set_dai_sysclk,
  1033. };
  1034. static const struct snd_soc_dai_ops pm860x_i2s_dai_ops = {
  1035. .digital_mute = pm860x_digital_mute,
  1036. .hw_params = pm860x_i2s_hw_params,
  1037. .set_fmt = pm860x_i2s_set_dai_fmt,
  1038. .set_sysclk = pm860x_set_dai_sysclk,
  1039. };
  1040. #define PM860X_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \
  1041. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000)
  1042. static struct snd_soc_dai_driver pm860x_dai[] = {
  1043. {
  1044. /* DAI PCM */
  1045. .name = "88pm860x-pcm",
  1046. .id = 1,
  1047. .playback = {
  1048. .stream_name = "PCM Playback",
  1049. .channels_min = 2,
  1050. .channels_max = 2,
  1051. .rates = PM860X_RATES,
  1052. .formats = SNDRV_PCM_FORMAT_S16_LE | \
  1053. SNDRV_PCM_FORMAT_S18_3LE,
  1054. },
  1055. .capture = {
  1056. .stream_name = "PCM Capture",
  1057. .channels_min = 2,
  1058. .channels_max = 2,
  1059. .rates = PM860X_RATES,
  1060. .formats = SNDRV_PCM_FORMAT_S16_LE | \
  1061. SNDRV_PCM_FORMAT_S18_3LE,
  1062. },
  1063. .ops = &pm860x_pcm_dai_ops,
  1064. }, {
  1065. /* DAI I2S */
  1066. .name = "88pm860x-i2s",
  1067. .id = 2,
  1068. .playback = {
  1069. .stream_name = "I2S Playback",
  1070. .channels_min = 2,
  1071. .channels_max = 2,
  1072. .rates = SNDRV_PCM_RATE_8000_48000,
  1073. .formats = SNDRV_PCM_FORMAT_S16_LE | \
  1074. SNDRV_PCM_FORMAT_S18_3LE,
  1075. },
  1076. .capture = {
  1077. .stream_name = "I2S Capture",
  1078. .channels_min = 2,
  1079. .channels_max = 2,
  1080. .rates = SNDRV_PCM_RATE_8000_48000,
  1081. .formats = SNDRV_PCM_FORMAT_S16_LE | \
  1082. SNDRV_PCM_FORMAT_S18_3LE,
  1083. },
  1084. .ops = &pm860x_i2s_dai_ops,
  1085. },
  1086. };
  1087. static irqreturn_t pm860x_codec_handler(int irq, void *data)
  1088. {
  1089. struct pm860x_priv *pm860x = data;
  1090. int status, shrt, report = 0, mic_report = 0;
  1091. int mask;
  1092. status = pm860x_reg_read(pm860x->i2c, REG_STATUS_1);
  1093. shrt = pm860x_reg_read(pm860x->i2c, REG_SHORTS);
  1094. mask = pm860x->det.hs_shrt | pm860x->det.hook_det | pm860x->det.lo_shrt
  1095. | pm860x->det.hp_det;
  1096. #ifndef CONFIG_SND_SOC_88PM860X_MODULE
  1097. if (status & (HEADSET_STATUS | MIC_STATUS | SHORT_HS1 | SHORT_HS2 |
  1098. SHORT_LO1 | SHORT_LO2))
  1099. trace_snd_soc_jack_irq(dev_name(pm860x->codec->dev));
  1100. #endif
  1101. if ((pm860x->det.hp_det & SND_JACK_HEADPHONE)
  1102. && (status & HEADSET_STATUS))
  1103. report |= SND_JACK_HEADPHONE;
  1104. if ((pm860x->det.mic_det & SND_JACK_MICROPHONE)
  1105. && (status & MIC_STATUS))
  1106. mic_report |= SND_JACK_MICROPHONE;
  1107. if (pm860x->det.hs_shrt && (shrt & (SHORT_HS1 | SHORT_HS2)))
  1108. report |= pm860x->det.hs_shrt;
  1109. if (pm860x->det.hook_det && (status & HOOK_STATUS))
  1110. report |= pm860x->det.hook_det;
  1111. if (pm860x->det.lo_shrt && (shrt & (SHORT_LO1 | SHORT_LO2)))
  1112. report |= pm860x->det.lo_shrt;
  1113. if (report)
  1114. snd_soc_jack_report(pm860x->det.hp_jack, report, mask);
  1115. if (mic_report)
  1116. snd_soc_jack_report(pm860x->det.mic_jack, SND_JACK_MICROPHONE,
  1117. SND_JACK_MICROPHONE);
  1118. dev_dbg(pm860x->codec->dev, "headphone report:0x%x, mask:%x\n",
  1119. report, mask);
  1120. dev_dbg(pm860x->codec->dev, "microphone report:0x%x\n", mic_report);
  1121. return IRQ_HANDLED;
  1122. }
  1123. int pm860x_hs_jack_detect(struct snd_soc_codec *codec,
  1124. struct snd_soc_jack *jack,
  1125. int det, int hook, int hs_shrt, int lo_shrt)
  1126. {
  1127. struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
  1128. int data;
  1129. pm860x->det.hp_jack = jack;
  1130. pm860x->det.hp_det = det;
  1131. pm860x->det.hook_det = hook;
  1132. pm860x->det.hs_shrt = hs_shrt;
  1133. pm860x->det.lo_shrt = lo_shrt;
  1134. if (det & SND_JACK_HEADPHONE)
  1135. pm860x_set_bits(codec->control_data, REG_HS_DET,
  1136. EN_HS_DET, EN_HS_DET);
  1137. /* headset short detect */
  1138. if (hs_shrt) {
  1139. data = CLR_SHORT_HS2 | CLR_SHORT_HS1;
  1140. pm860x_set_bits(codec->control_data, REG_SHORTS, data, data);
  1141. }
  1142. /* Lineout short detect */
  1143. if (lo_shrt) {
  1144. data = CLR_SHORT_LO2 | CLR_SHORT_LO1;
  1145. pm860x_set_bits(codec->control_data, REG_SHORTS, data, data);
  1146. }
  1147. /* sync status */
  1148. pm860x_codec_handler(0, pm860x);
  1149. return 0;
  1150. }
  1151. EXPORT_SYMBOL_GPL(pm860x_hs_jack_detect);
  1152. int pm860x_mic_jack_detect(struct snd_soc_codec *codec,
  1153. struct snd_soc_jack *jack, int det)
  1154. {
  1155. struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
  1156. pm860x->det.mic_jack = jack;
  1157. pm860x->det.mic_det = det;
  1158. if (det & SND_JACK_MICROPHONE)
  1159. pm860x_set_bits(codec->control_data, REG_MIC_DET,
  1160. MICDET_MASK, MICDET_MASK);
  1161. /* sync status */
  1162. pm860x_codec_handler(0, pm860x);
  1163. return 0;
  1164. }
  1165. EXPORT_SYMBOL_GPL(pm860x_mic_jack_detect);
  1166. static int pm860x_probe(struct snd_soc_codec *codec)
  1167. {
  1168. struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
  1169. int i, ret;
  1170. pm860x->codec = codec;
  1171. codec->control_data = pm860x->i2c;
  1172. for (i = 0; i < 4; i++) {
  1173. ret = request_threaded_irq(pm860x->irq[i], NULL,
  1174. pm860x_codec_handler, IRQF_ONESHOT,
  1175. pm860x->name[i], pm860x);
  1176. if (ret < 0) {
  1177. dev_err(codec->dev, "Failed to request IRQ!\n");
  1178. goto out;
  1179. }
  1180. }
  1181. pm860x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1182. ret = pm860x_bulk_read(codec->control_data, REG_CACHE_BASE,
  1183. REG_CACHE_SIZE, codec->reg_cache);
  1184. if (ret < 0) {
  1185. dev_err(codec->dev, "Failed to fill register cache: %d\n",
  1186. ret);
  1187. goto out;
  1188. }
  1189. return 0;
  1190. out:
  1191. while (--i >= 0)
  1192. free_irq(pm860x->irq[i], pm860x);
  1193. return ret;
  1194. }
  1195. static int pm860x_remove(struct snd_soc_codec *codec)
  1196. {
  1197. struct pm860x_priv *pm860x = snd_soc_codec_get_drvdata(codec);
  1198. int i;
  1199. for (i = 3; i >= 0; i--)
  1200. free_irq(pm860x->irq[i], pm860x);
  1201. pm860x_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1202. return 0;
  1203. }
  1204. static struct snd_soc_codec_driver soc_codec_dev_pm860x = {
  1205. .probe = pm860x_probe,
  1206. .remove = pm860x_remove,
  1207. .read = pm860x_read_reg_cache,
  1208. .write = pm860x_write_reg_cache,
  1209. .reg_cache_size = REG_CACHE_SIZE,
  1210. .reg_word_size = sizeof(u8),
  1211. .set_bias_level = pm860x_set_bias_level,
  1212. .controls = pm860x_snd_controls,
  1213. .num_controls = ARRAY_SIZE(pm860x_snd_controls),
  1214. .dapm_widgets = pm860x_dapm_widgets,
  1215. .num_dapm_widgets = ARRAY_SIZE(pm860x_dapm_widgets),
  1216. .dapm_routes = pm860x_dapm_routes,
  1217. .num_dapm_routes = ARRAY_SIZE(pm860x_dapm_routes),
  1218. };
  1219. static int __devinit pm860x_codec_probe(struct platform_device *pdev)
  1220. {
  1221. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  1222. struct pm860x_priv *pm860x;
  1223. struct resource *res;
  1224. int i, ret;
  1225. pm860x = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_priv),
  1226. GFP_KERNEL);
  1227. if (pm860x == NULL)
  1228. return -ENOMEM;
  1229. pm860x->chip = chip;
  1230. pm860x->i2c = (chip->id == CHIP_PM8607) ? chip->client
  1231. : chip->companion;
  1232. platform_set_drvdata(pdev, pm860x);
  1233. for (i = 0; i < 4; i++) {
  1234. res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
  1235. if (!res) {
  1236. dev_err(&pdev->dev, "Failed to get IRQ resources\n");
  1237. goto out;
  1238. }
  1239. pm860x->irq[i] = res->start + chip->irq_base;
  1240. strncpy(pm860x->name[i], res->name, MAX_NAME_LEN);
  1241. }
  1242. ret = snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pm860x,
  1243. pm860x_dai, ARRAY_SIZE(pm860x_dai));
  1244. if (ret) {
  1245. dev_err(&pdev->dev, "Failed to register codec\n");
  1246. goto out;
  1247. }
  1248. return ret;
  1249. out:
  1250. platform_set_drvdata(pdev, NULL);
  1251. return -EINVAL;
  1252. }
  1253. static int __devexit pm860x_codec_remove(struct platform_device *pdev)
  1254. {
  1255. snd_soc_unregister_codec(&pdev->dev);
  1256. platform_set_drvdata(pdev, NULL);
  1257. return 0;
  1258. }
  1259. static struct platform_driver pm860x_codec_driver = {
  1260. .driver = {
  1261. .name = "88pm860x-codec",
  1262. .owner = THIS_MODULE,
  1263. },
  1264. .probe = pm860x_codec_probe,
  1265. .remove = __devexit_p(pm860x_codec_remove),
  1266. };
  1267. module_platform_driver(pm860x_codec_driver);
  1268. MODULE_DESCRIPTION("ASoC 88PM860x driver");
  1269. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  1270. MODULE_LICENSE("GPL");
  1271. MODULE_ALIAS("platform:88pm860x-codec");