ak4671.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * ak4671.c -- audio driver for AK4671
  3. *
  4. * Copyright (C) 2009 Samsung Electronics Co.Ltd
  5. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/i2c.h>
  16. #include <linux/delay.h>
  17. #include <linux/regmap.h>
  18. #include <linux/slab.h>
  19. #include <sound/soc.h>
  20. #include <sound/initval.h>
  21. #include <sound/tlv.h>
  22. #include "ak4671.h"
  23. /* ak4671 register cache & default register settings */
  24. static const struct reg_default ak4671_reg_defaults[] = {
  25. { 0x00, 0x00 }, /* AK4671_AD_DA_POWER_MANAGEMENT (0x00) */
  26. { 0x01, 0xf6 }, /* AK4671_PLL_MODE_SELECT0 (0x01) */
  27. { 0x02, 0x00 }, /* AK4671_PLL_MODE_SELECT1 (0x02) */
  28. { 0x03, 0x02 }, /* AK4671_FORMAT_SELECT (0x03) */
  29. { 0x04, 0x00 }, /* AK4671_MIC_SIGNAL_SELECT (0x04) */
  30. { 0x05, 0x55 }, /* AK4671_MIC_AMP_GAIN (0x05) */
  31. { 0x06, 0x00 }, /* AK4671_MIXING_POWER_MANAGEMENT0 (0x06) */
  32. { 0x07, 0x00 }, /* AK4671_MIXING_POWER_MANAGEMENT1 (0x07) */
  33. { 0x08, 0xb5 }, /* AK4671_OUTPUT_VOLUME_CONTROL (0x08) */
  34. { 0x09, 0x00 }, /* AK4671_LOUT1_SIGNAL_SELECT (0x09) */
  35. { 0x0a, 0x00 }, /* AK4671_ROUT1_SIGNAL_SELECT (0x0a) */
  36. { 0x0b, 0x00 }, /* AK4671_LOUT2_SIGNAL_SELECT (0x0b) */
  37. { 0x0c, 0x00 }, /* AK4671_ROUT2_SIGNAL_SELECT (0x0c) */
  38. { 0x0d, 0x00 }, /* AK4671_LOUT3_SIGNAL_SELECT (0x0d) */
  39. { 0x0e, 0x00 }, /* AK4671_ROUT3_SIGNAL_SELECT (0x0e) */
  40. { 0x0f, 0x00 }, /* AK4671_LOUT1_POWER_MANAGERMENT (0x0f) */
  41. { 0x10, 0x00 }, /* AK4671_LOUT2_POWER_MANAGERMENT (0x10) */
  42. { 0x11, 0x80 }, /* AK4671_LOUT3_POWER_MANAGERMENT (0x11) */
  43. { 0x12, 0x91 }, /* AK4671_LCH_INPUT_VOLUME_CONTROL (0x12) */
  44. { 0x13, 0x91 }, /* AK4671_RCH_INPUT_VOLUME_CONTROL (0x13) */
  45. { 0x14, 0xe1 }, /* AK4671_ALC_REFERENCE_SELECT (0x14) */
  46. { 0x15, 0x00 }, /* AK4671_DIGITAL_MIXING_CONTROL (0x15) */
  47. { 0x16, 0x00 }, /* AK4671_ALC_TIMER_SELECT (0x16) */
  48. { 0x17, 0x00 }, /* AK4671_ALC_MODE_CONTROL (0x17) */
  49. { 0x18, 0x02 }, /* AK4671_MODE_CONTROL1 (0x18) */
  50. { 0x19, 0x01 }, /* AK4671_MODE_CONTROL2 (0x19) */
  51. { 0x1a, 0x18 }, /* AK4671_LCH_OUTPUT_VOLUME_CONTROL (0x1a) */
  52. { 0x1b, 0x18 }, /* AK4671_RCH_OUTPUT_VOLUME_CONTROL (0x1b) */
  53. { 0x1c, 0x00 }, /* AK4671_SIDETONE_A_CONTROL (0x1c) */
  54. { 0x1d, 0x02 }, /* AK4671_DIGITAL_FILTER_SELECT (0x1d) */
  55. { 0x1e, 0x00 }, /* AK4671_FIL3_COEFFICIENT0 (0x1e) */
  56. { 0x1f, 0x00 }, /* AK4671_FIL3_COEFFICIENT1 (0x1f) */
  57. { 0x20, 0x00 }, /* AK4671_FIL3_COEFFICIENT2 (0x20) */
  58. { 0x21, 0x00 }, /* AK4671_FIL3_COEFFICIENT3 (0x21) */
  59. { 0x22, 0x00 }, /* AK4671_EQ_COEFFICIENT0 (0x22) */
  60. { 0x23, 0x00 }, /* AK4671_EQ_COEFFICIENT1 (0x23) */
  61. { 0x24, 0x00 }, /* AK4671_EQ_COEFFICIENT2 (0x24) */
  62. { 0x25, 0x00 }, /* AK4671_EQ_COEFFICIENT3 (0x25) */
  63. { 0x26, 0x00 }, /* AK4671_EQ_COEFFICIENT4 (0x26) */
  64. { 0x27, 0x00 }, /* AK4671_EQ_COEFFICIENT5 (0x27) */
  65. { 0x28, 0xa9 }, /* AK4671_FIL1_COEFFICIENT0 (0x28) */
  66. { 0x29, 0x1f }, /* AK4671_FIL1_COEFFICIENT1 (0x29) */
  67. { 0x2a, 0xad }, /* AK4671_FIL1_COEFFICIENT2 (0x2a) */
  68. { 0x2b, 0x20 }, /* AK4671_FIL1_COEFFICIENT3 (0x2b) */
  69. { 0x2c, 0x00 }, /* AK4671_FIL2_COEFFICIENT0 (0x2c) */
  70. { 0x2d, 0x00 }, /* AK4671_FIL2_COEFFICIENT1 (0x2d) */
  71. { 0x2e, 0x00 }, /* AK4671_FIL2_COEFFICIENT2 (0x2e) */
  72. { 0x2f, 0x00 }, /* AK4671_FIL2_COEFFICIENT3 (0x2f) */
  73. { 0x30, 0x00 }, /* AK4671_DIGITAL_FILTER_SELECT2 (0x30) */
  74. { 0x32, 0x00 }, /* AK4671_E1_COEFFICIENT0 (0x32) */
  75. { 0x33, 0x00 }, /* AK4671_E1_COEFFICIENT1 (0x33) */
  76. { 0x34, 0x00 }, /* AK4671_E1_COEFFICIENT2 (0x34) */
  77. { 0x35, 0x00 }, /* AK4671_E1_COEFFICIENT3 (0x35) */
  78. { 0x36, 0x00 }, /* AK4671_E1_COEFFICIENT4 (0x36) */
  79. { 0x37, 0x00 }, /* AK4671_E1_COEFFICIENT5 (0x37) */
  80. { 0x38, 0x00 }, /* AK4671_E2_COEFFICIENT0 (0x38) */
  81. { 0x39, 0x00 }, /* AK4671_E2_COEFFICIENT1 (0x39) */
  82. { 0x3a, 0x00 }, /* AK4671_E2_COEFFICIENT2 (0x3a) */
  83. { 0x3b, 0x00 }, /* AK4671_E2_COEFFICIENT3 (0x3b) */
  84. { 0x3c, 0x00 }, /* AK4671_E2_COEFFICIENT4 (0x3c) */
  85. { 0x3d, 0x00 }, /* AK4671_E2_COEFFICIENT5 (0x3d) */
  86. { 0x3e, 0x00 }, /* AK4671_E3_COEFFICIENT0 (0x3e) */
  87. { 0x3f, 0x00 }, /* AK4671_E3_COEFFICIENT1 (0x3f) */
  88. { 0x40, 0x00 }, /* AK4671_E3_COEFFICIENT2 (0x40) */
  89. { 0x41, 0x00 }, /* AK4671_E3_COEFFICIENT3 (0x41) */
  90. { 0x42, 0x00 }, /* AK4671_E3_COEFFICIENT4 (0x42) */
  91. { 0x43, 0x00 }, /* AK4671_E3_COEFFICIENT5 (0x43) */
  92. { 0x44, 0x00 }, /* AK4671_E4_COEFFICIENT0 (0x44) */
  93. { 0x45, 0x00 }, /* AK4671_E4_COEFFICIENT1 (0x45) */
  94. { 0x46, 0x00 }, /* AK4671_E4_COEFFICIENT2 (0x46) */
  95. { 0x47, 0x00 }, /* AK4671_E4_COEFFICIENT3 (0x47) */
  96. { 0x48, 0x00 }, /* AK4671_E4_COEFFICIENT4 (0x48) */
  97. { 0x49, 0x00 }, /* AK4671_E4_COEFFICIENT5 (0x49) */
  98. { 0x4a, 0x00 }, /* AK4671_E5_COEFFICIENT0 (0x4a) */
  99. { 0x4b, 0x00 }, /* AK4671_E5_COEFFICIENT1 (0x4b) */
  100. { 0x4c, 0x00 }, /* AK4671_E5_COEFFICIENT2 (0x4c) */
  101. { 0x4d, 0x00 }, /* AK4671_E5_COEFFICIENT3 (0x4d) */
  102. { 0x4e, 0x00 }, /* AK4671_E5_COEFFICIENT4 (0x4e) */
  103. { 0x4f, 0x00 }, /* AK4671_E5_COEFFICIENT5 (0x4f) */
  104. { 0x50, 0x88 }, /* AK4671_EQ_CONTROL_250HZ_100HZ (0x50) */
  105. { 0x51, 0x88 }, /* AK4671_EQ_CONTROL_3500HZ_1KHZ (0x51) */
  106. { 0x52, 0x08 }, /* AK4671_EQ_CONTRO_10KHZ (0x52) */
  107. { 0x53, 0x00 }, /* AK4671_PCM_IF_CONTROL0 (0x53) */
  108. { 0x54, 0x00 }, /* AK4671_PCM_IF_CONTROL1 (0x54) */
  109. { 0x55, 0x00 }, /* AK4671_PCM_IF_CONTROL2 (0x55) */
  110. { 0x56, 0x18 }, /* AK4671_DIGITAL_VOLUME_B_CONTROL (0x56) */
  111. { 0x57, 0x18 }, /* AK4671_DIGITAL_VOLUME_C_CONTROL (0x57) */
  112. { 0x58, 0x00 }, /* AK4671_SIDETONE_VOLUME_CONTROL (0x58) */
  113. { 0x59, 0x00 }, /* AK4671_DIGITAL_MIXING_CONTROL2 (0x59) */
  114. { 0x5a, 0x00 }, /* AK4671_SAR_ADC_CONTROL (0x5a) */
  115. };
  116. /*
  117. * LOUT1/ROUT1 output volume control:
  118. * from -24 to 6 dB in 6 dB steps (mute instead of -30 dB)
  119. */
  120. static DECLARE_TLV_DB_SCALE(out1_tlv, -3000, 600, 1);
  121. /*
  122. * LOUT2/ROUT2 output volume control:
  123. * from -33 to 6 dB in 3 dB steps (mute instead of -33 dB)
  124. */
  125. static DECLARE_TLV_DB_SCALE(out2_tlv, -3300, 300, 1);
  126. /*
  127. * LOUT3/ROUT3 output volume control:
  128. * from -6 to 3 dB in 3 dB steps
  129. */
  130. static DECLARE_TLV_DB_SCALE(out3_tlv, -600, 300, 0);
  131. /*
  132. * Mic amp gain control:
  133. * from -15 to 30 dB in 3 dB steps
  134. * REVISIT: The actual min value(0x01) is -12 dB and the reg value 0x00 is not
  135. * available
  136. */
  137. static DECLARE_TLV_DB_SCALE(mic_amp_tlv, -1500, 300, 0);
  138. static const struct snd_kcontrol_new ak4671_snd_controls[] = {
  139. /* Common playback gain controls */
  140. SOC_SINGLE_TLV("Line Output1 Playback Volume",
  141. AK4671_OUTPUT_VOLUME_CONTROL, 0, 0x6, 0, out1_tlv),
  142. SOC_SINGLE_TLV("Headphone Output2 Playback Volume",
  143. AK4671_OUTPUT_VOLUME_CONTROL, 4, 0xd, 0, out2_tlv),
  144. SOC_SINGLE_TLV("Line Output3 Playback Volume",
  145. AK4671_LOUT3_POWER_MANAGERMENT, 6, 0x3, 0, out3_tlv),
  146. /* Common capture gain controls */
  147. SOC_DOUBLE_TLV("Mic Amp Capture Volume",
  148. AK4671_MIC_AMP_GAIN, 0, 4, 0xf, 0, mic_amp_tlv),
  149. };
  150. /* event handlers */
  151. static int ak4671_out2_event(struct snd_soc_dapm_widget *w,
  152. struct snd_kcontrol *kcontrol, int event)
  153. {
  154. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  155. switch (event) {
  156. case SND_SOC_DAPM_POST_PMU:
  157. snd_soc_update_bits(codec, AK4671_LOUT2_POWER_MANAGERMENT,
  158. AK4671_MUTEN, AK4671_MUTEN);
  159. break;
  160. case SND_SOC_DAPM_PRE_PMD:
  161. snd_soc_update_bits(codec, AK4671_LOUT2_POWER_MANAGERMENT,
  162. AK4671_MUTEN, 0);
  163. break;
  164. }
  165. return 0;
  166. }
  167. /* Output Mixers */
  168. static const struct snd_kcontrol_new ak4671_lout1_mixer_controls[] = {
  169. SOC_DAPM_SINGLE("DACL", AK4671_LOUT1_SIGNAL_SELECT, 0, 1, 0),
  170. SOC_DAPM_SINGLE("LINL1", AK4671_LOUT1_SIGNAL_SELECT, 1, 1, 0),
  171. SOC_DAPM_SINGLE("LINL2", AK4671_LOUT1_SIGNAL_SELECT, 2, 1, 0),
  172. SOC_DAPM_SINGLE("LINL3", AK4671_LOUT1_SIGNAL_SELECT, 3, 1, 0),
  173. SOC_DAPM_SINGLE("LINL4", AK4671_LOUT1_SIGNAL_SELECT, 4, 1, 0),
  174. SOC_DAPM_SINGLE("LOOPL", AK4671_LOUT1_SIGNAL_SELECT, 5, 1, 0),
  175. };
  176. static const struct snd_kcontrol_new ak4671_rout1_mixer_controls[] = {
  177. SOC_DAPM_SINGLE("DACR", AK4671_ROUT1_SIGNAL_SELECT, 0, 1, 0),
  178. SOC_DAPM_SINGLE("RINR1", AK4671_ROUT1_SIGNAL_SELECT, 1, 1, 0),
  179. SOC_DAPM_SINGLE("RINR2", AK4671_ROUT1_SIGNAL_SELECT, 2, 1, 0),
  180. SOC_DAPM_SINGLE("RINR3", AK4671_ROUT1_SIGNAL_SELECT, 3, 1, 0),
  181. SOC_DAPM_SINGLE("RINR4", AK4671_ROUT1_SIGNAL_SELECT, 4, 1, 0),
  182. SOC_DAPM_SINGLE("LOOPR", AK4671_ROUT1_SIGNAL_SELECT, 5, 1, 0),
  183. };
  184. static const struct snd_kcontrol_new ak4671_lout2_mixer_controls[] = {
  185. SOC_DAPM_SINGLE("DACHL", AK4671_LOUT2_SIGNAL_SELECT, 0, 1, 0),
  186. SOC_DAPM_SINGLE("LINH1", AK4671_LOUT2_SIGNAL_SELECT, 1, 1, 0),
  187. SOC_DAPM_SINGLE("LINH2", AK4671_LOUT2_SIGNAL_SELECT, 2, 1, 0),
  188. SOC_DAPM_SINGLE("LINH3", AK4671_LOUT2_SIGNAL_SELECT, 3, 1, 0),
  189. SOC_DAPM_SINGLE("LINH4", AK4671_LOUT2_SIGNAL_SELECT, 4, 1, 0),
  190. SOC_DAPM_SINGLE("LOOPHL", AK4671_LOUT2_SIGNAL_SELECT, 5, 1, 0),
  191. };
  192. static const struct snd_kcontrol_new ak4671_rout2_mixer_controls[] = {
  193. SOC_DAPM_SINGLE("DACHR", AK4671_ROUT2_SIGNAL_SELECT, 0, 1, 0),
  194. SOC_DAPM_SINGLE("RINH1", AK4671_ROUT2_SIGNAL_SELECT, 1, 1, 0),
  195. SOC_DAPM_SINGLE("RINH2", AK4671_ROUT2_SIGNAL_SELECT, 2, 1, 0),
  196. SOC_DAPM_SINGLE("RINH3", AK4671_ROUT2_SIGNAL_SELECT, 3, 1, 0),
  197. SOC_DAPM_SINGLE("RINH4", AK4671_ROUT2_SIGNAL_SELECT, 4, 1, 0),
  198. SOC_DAPM_SINGLE("LOOPHR", AK4671_ROUT2_SIGNAL_SELECT, 5, 1, 0),
  199. };
  200. static const struct snd_kcontrol_new ak4671_lout3_mixer_controls[] = {
  201. SOC_DAPM_SINGLE("DACSL", AK4671_LOUT3_SIGNAL_SELECT, 0, 1, 0),
  202. SOC_DAPM_SINGLE("LINS1", AK4671_LOUT3_SIGNAL_SELECT, 1, 1, 0),
  203. SOC_DAPM_SINGLE("LINS2", AK4671_LOUT3_SIGNAL_SELECT, 2, 1, 0),
  204. SOC_DAPM_SINGLE("LINS3", AK4671_LOUT3_SIGNAL_SELECT, 3, 1, 0),
  205. SOC_DAPM_SINGLE("LINS4", AK4671_LOUT3_SIGNAL_SELECT, 4, 1, 0),
  206. SOC_DAPM_SINGLE("LOOPSL", AK4671_LOUT3_SIGNAL_SELECT, 5, 1, 0),
  207. };
  208. static const struct snd_kcontrol_new ak4671_rout3_mixer_controls[] = {
  209. SOC_DAPM_SINGLE("DACSR", AK4671_ROUT3_SIGNAL_SELECT, 0, 1, 0),
  210. SOC_DAPM_SINGLE("RINS1", AK4671_ROUT3_SIGNAL_SELECT, 1, 1, 0),
  211. SOC_DAPM_SINGLE("RINS2", AK4671_ROUT3_SIGNAL_SELECT, 2, 1, 0),
  212. SOC_DAPM_SINGLE("RINS3", AK4671_ROUT3_SIGNAL_SELECT, 3, 1, 0),
  213. SOC_DAPM_SINGLE("RINS4", AK4671_ROUT3_SIGNAL_SELECT, 4, 1, 0),
  214. SOC_DAPM_SINGLE("LOOPSR", AK4671_ROUT3_SIGNAL_SELECT, 5, 1, 0),
  215. };
  216. /* Input MUXs */
  217. static const char *ak4671_lin_mux_texts[] =
  218. {"LIN1", "LIN2", "LIN3", "LIN4"};
  219. static SOC_ENUM_SINGLE_DECL(ak4671_lin_mux_enum,
  220. AK4671_MIC_SIGNAL_SELECT, 0,
  221. ak4671_lin_mux_texts);
  222. static const struct snd_kcontrol_new ak4671_lin_mux_control =
  223. SOC_DAPM_ENUM("Route", ak4671_lin_mux_enum);
  224. static const char *ak4671_rin_mux_texts[] =
  225. {"RIN1", "RIN2", "RIN3", "RIN4"};
  226. static SOC_ENUM_SINGLE_DECL(ak4671_rin_mux_enum,
  227. AK4671_MIC_SIGNAL_SELECT, 2,
  228. ak4671_rin_mux_texts);
  229. static const struct snd_kcontrol_new ak4671_rin_mux_control =
  230. SOC_DAPM_ENUM("Route", ak4671_rin_mux_enum);
  231. static const struct snd_soc_dapm_widget ak4671_dapm_widgets[] = {
  232. /* Inputs */
  233. SND_SOC_DAPM_INPUT("LIN1"),
  234. SND_SOC_DAPM_INPUT("RIN1"),
  235. SND_SOC_DAPM_INPUT("LIN2"),
  236. SND_SOC_DAPM_INPUT("RIN2"),
  237. SND_SOC_DAPM_INPUT("LIN3"),
  238. SND_SOC_DAPM_INPUT("RIN3"),
  239. SND_SOC_DAPM_INPUT("LIN4"),
  240. SND_SOC_DAPM_INPUT("RIN4"),
  241. /* Outputs */
  242. SND_SOC_DAPM_OUTPUT("LOUT1"),
  243. SND_SOC_DAPM_OUTPUT("ROUT1"),
  244. SND_SOC_DAPM_OUTPUT("LOUT2"),
  245. SND_SOC_DAPM_OUTPUT("ROUT2"),
  246. SND_SOC_DAPM_OUTPUT("LOUT3"),
  247. SND_SOC_DAPM_OUTPUT("ROUT3"),
  248. /* DAC */
  249. SND_SOC_DAPM_DAC("DAC Left", "Left HiFi Playback",
  250. AK4671_AD_DA_POWER_MANAGEMENT, 6, 0),
  251. SND_SOC_DAPM_DAC("DAC Right", "Right HiFi Playback",
  252. AK4671_AD_DA_POWER_MANAGEMENT, 7, 0),
  253. /* ADC */
  254. SND_SOC_DAPM_ADC("ADC Left", "Left HiFi Capture",
  255. AK4671_AD_DA_POWER_MANAGEMENT, 4, 0),
  256. SND_SOC_DAPM_ADC("ADC Right", "Right HiFi Capture",
  257. AK4671_AD_DA_POWER_MANAGEMENT, 5, 0),
  258. /* PGA */
  259. SND_SOC_DAPM_PGA("LOUT2 Mix Amp",
  260. AK4671_LOUT2_POWER_MANAGERMENT, 5, 0, NULL, 0),
  261. SND_SOC_DAPM_PGA("ROUT2 Mix Amp",
  262. AK4671_LOUT2_POWER_MANAGERMENT, 6, 0, NULL, 0),
  263. SND_SOC_DAPM_PGA("LIN1 Mixing Circuit",
  264. AK4671_MIXING_POWER_MANAGEMENT1, 0, 0, NULL, 0),
  265. SND_SOC_DAPM_PGA("RIN1 Mixing Circuit",
  266. AK4671_MIXING_POWER_MANAGEMENT1, 1, 0, NULL, 0),
  267. SND_SOC_DAPM_PGA("LIN2 Mixing Circuit",
  268. AK4671_MIXING_POWER_MANAGEMENT1, 2, 0, NULL, 0),
  269. SND_SOC_DAPM_PGA("RIN2 Mixing Circuit",
  270. AK4671_MIXING_POWER_MANAGEMENT1, 3, 0, NULL, 0),
  271. SND_SOC_DAPM_PGA("LIN3 Mixing Circuit",
  272. AK4671_MIXING_POWER_MANAGEMENT1, 4, 0, NULL, 0),
  273. SND_SOC_DAPM_PGA("RIN3 Mixing Circuit",
  274. AK4671_MIXING_POWER_MANAGEMENT1, 5, 0, NULL, 0),
  275. SND_SOC_DAPM_PGA("LIN4 Mixing Circuit",
  276. AK4671_MIXING_POWER_MANAGEMENT1, 6, 0, NULL, 0),
  277. SND_SOC_DAPM_PGA("RIN4 Mixing Circuit",
  278. AK4671_MIXING_POWER_MANAGEMENT1, 7, 0, NULL, 0),
  279. /* Output Mixers */
  280. SND_SOC_DAPM_MIXER("LOUT1 Mixer", AK4671_LOUT1_POWER_MANAGERMENT, 0, 0,
  281. &ak4671_lout1_mixer_controls[0],
  282. ARRAY_SIZE(ak4671_lout1_mixer_controls)),
  283. SND_SOC_DAPM_MIXER("ROUT1 Mixer", AK4671_LOUT1_POWER_MANAGERMENT, 1, 0,
  284. &ak4671_rout1_mixer_controls[0],
  285. ARRAY_SIZE(ak4671_rout1_mixer_controls)),
  286. SND_SOC_DAPM_MIXER_E("LOUT2 Mixer", AK4671_LOUT2_POWER_MANAGERMENT,
  287. 0, 0, &ak4671_lout2_mixer_controls[0],
  288. ARRAY_SIZE(ak4671_lout2_mixer_controls),
  289. ak4671_out2_event,
  290. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_PRE_PMD),
  291. SND_SOC_DAPM_MIXER_E("ROUT2 Mixer", AK4671_LOUT2_POWER_MANAGERMENT,
  292. 1, 0, &ak4671_rout2_mixer_controls[0],
  293. ARRAY_SIZE(ak4671_rout2_mixer_controls),
  294. ak4671_out2_event,
  295. SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_PRE_PMD),
  296. SND_SOC_DAPM_MIXER("LOUT3 Mixer", AK4671_LOUT3_POWER_MANAGERMENT, 0, 0,
  297. &ak4671_lout3_mixer_controls[0],
  298. ARRAY_SIZE(ak4671_lout3_mixer_controls)),
  299. SND_SOC_DAPM_MIXER("ROUT3 Mixer", AK4671_LOUT3_POWER_MANAGERMENT, 1, 0,
  300. &ak4671_rout3_mixer_controls[0],
  301. ARRAY_SIZE(ak4671_rout3_mixer_controls)),
  302. /* Input MUXs */
  303. SND_SOC_DAPM_MUX("LIN MUX", AK4671_AD_DA_POWER_MANAGEMENT, 2, 0,
  304. &ak4671_lin_mux_control),
  305. SND_SOC_DAPM_MUX("RIN MUX", AK4671_AD_DA_POWER_MANAGEMENT, 3, 0,
  306. &ak4671_rin_mux_control),
  307. /* Mic Power */
  308. SND_SOC_DAPM_MICBIAS("Mic Bias", AK4671_AD_DA_POWER_MANAGEMENT, 1, 0),
  309. /* Supply */
  310. SND_SOC_DAPM_SUPPLY("PMPLL", AK4671_PLL_MODE_SELECT1, 0, 0, NULL, 0),
  311. };
  312. static const struct snd_soc_dapm_route ak4671_intercon[] = {
  313. {"DAC Left", NULL, "PMPLL"},
  314. {"DAC Right", NULL, "PMPLL"},
  315. {"ADC Left", NULL, "PMPLL"},
  316. {"ADC Right", NULL, "PMPLL"},
  317. /* Outputs */
  318. {"LOUT1", NULL, "LOUT1 Mixer"},
  319. {"ROUT1", NULL, "ROUT1 Mixer"},
  320. {"LOUT2", NULL, "LOUT2 Mix Amp"},
  321. {"ROUT2", NULL, "ROUT2 Mix Amp"},
  322. {"LOUT3", NULL, "LOUT3 Mixer"},
  323. {"ROUT3", NULL, "ROUT3 Mixer"},
  324. {"LOUT1 Mixer", "DACL", "DAC Left"},
  325. {"ROUT1 Mixer", "DACR", "DAC Right"},
  326. {"LOUT2 Mixer", "DACHL", "DAC Left"},
  327. {"ROUT2 Mixer", "DACHR", "DAC Right"},
  328. {"LOUT2 Mix Amp", NULL, "LOUT2 Mixer"},
  329. {"ROUT2 Mix Amp", NULL, "ROUT2 Mixer"},
  330. {"LOUT3 Mixer", "DACSL", "DAC Left"},
  331. {"ROUT3 Mixer", "DACSR", "DAC Right"},
  332. /* Inputs */
  333. {"LIN MUX", "LIN1", "LIN1"},
  334. {"LIN MUX", "LIN2", "LIN2"},
  335. {"LIN MUX", "LIN3", "LIN3"},
  336. {"LIN MUX", "LIN4", "LIN4"},
  337. {"RIN MUX", "RIN1", "RIN1"},
  338. {"RIN MUX", "RIN2", "RIN2"},
  339. {"RIN MUX", "RIN3", "RIN3"},
  340. {"RIN MUX", "RIN4", "RIN4"},
  341. {"LIN1", NULL, "Mic Bias"},
  342. {"RIN1", NULL, "Mic Bias"},
  343. {"LIN2", NULL, "Mic Bias"},
  344. {"RIN2", NULL, "Mic Bias"},
  345. {"ADC Left", NULL, "LIN MUX"},
  346. {"ADC Right", NULL, "RIN MUX"},
  347. /* Analog Loops */
  348. {"LIN1 Mixing Circuit", NULL, "LIN1"},
  349. {"RIN1 Mixing Circuit", NULL, "RIN1"},
  350. {"LIN2 Mixing Circuit", NULL, "LIN2"},
  351. {"RIN2 Mixing Circuit", NULL, "RIN2"},
  352. {"LIN3 Mixing Circuit", NULL, "LIN3"},
  353. {"RIN3 Mixing Circuit", NULL, "RIN3"},
  354. {"LIN4 Mixing Circuit", NULL, "LIN4"},
  355. {"RIN4 Mixing Circuit", NULL, "RIN4"},
  356. {"LOUT1 Mixer", "LINL1", "LIN1 Mixing Circuit"},
  357. {"ROUT1 Mixer", "RINR1", "RIN1 Mixing Circuit"},
  358. {"LOUT2 Mixer", "LINH1", "LIN1 Mixing Circuit"},
  359. {"ROUT2 Mixer", "RINH1", "RIN1 Mixing Circuit"},
  360. {"LOUT3 Mixer", "LINS1", "LIN1 Mixing Circuit"},
  361. {"ROUT3 Mixer", "RINS1", "RIN1 Mixing Circuit"},
  362. {"LOUT1 Mixer", "LINL2", "LIN2 Mixing Circuit"},
  363. {"ROUT1 Mixer", "RINR2", "RIN2 Mixing Circuit"},
  364. {"LOUT2 Mixer", "LINH2", "LIN2 Mixing Circuit"},
  365. {"ROUT2 Mixer", "RINH2", "RIN2 Mixing Circuit"},
  366. {"LOUT3 Mixer", "LINS2", "LIN2 Mixing Circuit"},
  367. {"ROUT3 Mixer", "RINS2", "RIN2 Mixing Circuit"},
  368. {"LOUT1 Mixer", "LINL3", "LIN3 Mixing Circuit"},
  369. {"ROUT1 Mixer", "RINR3", "RIN3 Mixing Circuit"},
  370. {"LOUT2 Mixer", "LINH3", "LIN3 Mixing Circuit"},
  371. {"ROUT2 Mixer", "RINH3", "RIN3 Mixing Circuit"},
  372. {"LOUT3 Mixer", "LINS3", "LIN3 Mixing Circuit"},
  373. {"ROUT3 Mixer", "RINS3", "RIN3 Mixing Circuit"},
  374. {"LOUT1 Mixer", "LINL4", "LIN4 Mixing Circuit"},
  375. {"ROUT1 Mixer", "RINR4", "RIN4 Mixing Circuit"},
  376. {"LOUT2 Mixer", "LINH4", "LIN4 Mixing Circuit"},
  377. {"ROUT2 Mixer", "RINH4", "RIN4 Mixing Circuit"},
  378. {"LOUT3 Mixer", "LINS4", "LIN4 Mixing Circuit"},
  379. {"ROUT3 Mixer", "RINS4", "RIN4 Mixing Circuit"},
  380. };
  381. static int ak4671_hw_params(struct snd_pcm_substream *substream,
  382. struct snd_pcm_hw_params *params,
  383. struct snd_soc_dai *dai)
  384. {
  385. struct snd_soc_codec *codec = dai->codec;
  386. u8 fs;
  387. fs = snd_soc_read(codec, AK4671_PLL_MODE_SELECT0);
  388. fs &= ~AK4671_FS;
  389. switch (params_rate(params)) {
  390. case 8000:
  391. fs |= AK4671_FS_8KHZ;
  392. break;
  393. case 12000:
  394. fs |= AK4671_FS_12KHZ;
  395. break;
  396. case 16000:
  397. fs |= AK4671_FS_16KHZ;
  398. break;
  399. case 24000:
  400. fs |= AK4671_FS_24KHZ;
  401. break;
  402. case 11025:
  403. fs |= AK4671_FS_11_025KHZ;
  404. break;
  405. case 22050:
  406. fs |= AK4671_FS_22_05KHZ;
  407. break;
  408. case 32000:
  409. fs |= AK4671_FS_32KHZ;
  410. break;
  411. case 44100:
  412. fs |= AK4671_FS_44_1KHZ;
  413. break;
  414. case 48000:
  415. fs |= AK4671_FS_48KHZ;
  416. break;
  417. default:
  418. return -EINVAL;
  419. }
  420. snd_soc_write(codec, AK4671_PLL_MODE_SELECT0, fs);
  421. return 0;
  422. }
  423. static int ak4671_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
  424. unsigned int freq, int dir)
  425. {
  426. struct snd_soc_codec *codec = dai->codec;
  427. u8 pll;
  428. pll = snd_soc_read(codec, AK4671_PLL_MODE_SELECT0);
  429. pll &= ~AK4671_PLL;
  430. switch (freq) {
  431. case 11289600:
  432. pll |= AK4671_PLL_11_2896MHZ;
  433. break;
  434. case 12000000:
  435. pll |= AK4671_PLL_12MHZ;
  436. break;
  437. case 12288000:
  438. pll |= AK4671_PLL_12_288MHZ;
  439. break;
  440. case 13000000:
  441. pll |= AK4671_PLL_13MHZ;
  442. break;
  443. case 13500000:
  444. pll |= AK4671_PLL_13_5MHZ;
  445. break;
  446. case 19200000:
  447. pll |= AK4671_PLL_19_2MHZ;
  448. break;
  449. case 24000000:
  450. pll |= AK4671_PLL_24MHZ;
  451. break;
  452. case 26000000:
  453. pll |= AK4671_PLL_26MHZ;
  454. break;
  455. case 27000000:
  456. pll |= AK4671_PLL_27MHZ;
  457. break;
  458. default:
  459. return -EINVAL;
  460. }
  461. snd_soc_write(codec, AK4671_PLL_MODE_SELECT0, pll);
  462. return 0;
  463. }
  464. static int ak4671_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  465. {
  466. struct snd_soc_codec *codec = dai->codec;
  467. u8 mode;
  468. u8 format;
  469. /* set master/slave audio interface */
  470. mode = snd_soc_read(codec, AK4671_PLL_MODE_SELECT1);
  471. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  472. case SND_SOC_DAIFMT_CBM_CFM:
  473. mode |= AK4671_M_S;
  474. break;
  475. case SND_SOC_DAIFMT_CBM_CFS:
  476. mode &= ~(AK4671_M_S);
  477. break;
  478. default:
  479. return -EINVAL;
  480. }
  481. /* interface format */
  482. format = snd_soc_read(codec, AK4671_FORMAT_SELECT);
  483. format &= ~AK4671_DIF;
  484. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  485. case SND_SOC_DAIFMT_I2S:
  486. format |= AK4671_DIF_I2S_MODE;
  487. break;
  488. case SND_SOC_DAIFMT_LEFT_J:
  489. format |= AK4671_DIF_MSB_MODE;
  490. break;
  491. case SND_SOC_DAIFMT_DSP_A:
  492. format |= AK4671_DIF_DSP_MODE;
  493. format |= AK4671_BCKP;
  494. format |= AK4671_MSBS;
  495. break;
  496. default:
  497. return -EINVAL;
  498. }
  499. /* set mode and format */
  500. snd_soc_write(codec, AK4671_PLL_MODE_SELECT1, mode);
  501. snd_soc_write(codec, AK4671_FORMAT_SELECT, format);
  502. return 0;
  503. }
  504. static int ak4671_set_bias_level(struct snd_soc_codec *codec,
  505. enum snd_soc_bias_level level)
  506. {
  507. switch (level) {
  508. case SND_SOC_BIAS_ON:
  509. case SND_SOC_BIAS_PREPARE:
  510. case SND_SOC_BIAS_STANDBY:
  511. snd_soc_update_bits(codec, AK4671_AD_DA_POWER_MANAGEMENT,
  512. AK4671_PMVCM, AK4671_PMVCM);
  513. break;
  514. case SND_SOC_BIAS_OFF:
  515. snd_soc_write(codec, AK4671_AD_DA_POWER_MANAGEMENT, 0x00);
  516. break;
  517. }
  518. return 0;
  519. }
  520. #define AK4671_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  521. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  522. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
  523. SNDRV_PCM_RATE_48000)
  524. #define AK4671_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  525. static const struct snd_soc_dai_ops ak4671_dai_ops = {
  526. .hw_params = ak4671_hw_params,
  527. .set_sysclk = ak4671_set_dai_sysclk,
  528. .set_fmt = ak4671_set_dai_fmt,
  529. };
  530. static struct snd_soc_dai_driver ak4671_dai = {
  531. .name = "ak4671-hifi",
  532. .playback = {
  533. .stream_name = "Playback",
  534. .channels_min = 1,
  535. .channels_max = 2,
  536. .rates = AK4671_RATES,
  537. .formats = AK4671_FORMATS,},
  538. .capture = {
  539. .stream_name = "Capture",
  540. .channels_min = 1,
  541. .channels_max = 2,
  542. .rates = AK4671_RATES,
  543. .formats = AK4671_FORMATS,},
  544. .ops = &ak4671_dai_ops,
  545. };
  546. static struct snd_soc_codec_driver soc_codec_dev_ak4671 = {
  547. .set_bias_level = ak4671_set_bias_level,
  548. .component_driver = {
  549. .controls = ak4671_snd_controls,
  550. .num_controls = ARRAY_SIZE(ak4671_snd_controls),
  551. .dapm_widgets = ak4671_dapm_widgets,
  552. .num_dapm_widgets = ARRAY_SIZE(ak4671_dapm_widgets),
  553. .dapm_routes = ak4671_intercon,
  554. .num_dapm_routes = ARRAY_SIZE(ak4671_intercon),
  555. },
  556. };
  557. static const struct regmap_config ak4671_regmap = {
  558. .reg_bits = 8,
  559. .val_bits = 8,
  560. .max_register = AK4671_SAR_ADC_CONTROL,
  561. .reg_defaults = ak4671_reg_defaults,
  562. .num_reg_defaults = ARRAY_SIZE(ak4671_reg_defaults),
  563. .cache_type = REGCACHE_RBTREE,
  564. };
  565. static int ak4671_i2c_probe(struct i2c_client *client,
  566. const struct i2c_device_id *id)
  567. {
  568. struct regmap *regmap;
  569. int ret;
  570. regmap = devm_regmap_init_i2c(client, &ak4671_regmap);
  571. if (IS_ERR(regmap)) {
  572. ret = PTR_ERR(regmap);
  573. dev_err(&client->dev, "Failed to create regmap: %d\n", ret);
  574. return ret;
  575. }
  576. ret = snd_soc_register_codec(&client->dev,
  577. &soc_codec_dev_ak4671, &ak4671_dai, 1);
  578. return ret;
  579. }
  580. static int ak4671_i2c_remove(struct i2c_client *client)
  581. {
  582. snd_soc_unregister_codec(&client->dev);
  583. return 0;
  584. }
  585. static const struct i2c_device_id ak4671_i2c_id[] = {
  586. { "ak4671", 0 },
  587. { }
  588. };
  589. MODULE_DEVICE_TABLE(i2c, ak4671_i2c_id);
  590. static struct i2c_driver ak4671_i2c_driver = {
  591. .driver = {
  592. .name = "ak4671-codec",
  593. },
  594. .probe = ak4671_i2c_probe,
  595. .remove = ak4671_i2c_remove,
  596. .id_table = ak4671_i2c_id,
  597. };
  598. module_i2c_driver(ak4671_i2c_driver);
  599. MODULE_DESCRIPTION("ASoC AK4671 codec driver");
  600. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  601. MODULE_LICENSE("GPL");