cs42l56.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /*
  2. * cs42l56.c -- CS42L56 ALSA SoC audio driver
  3. *
  4. * Copyright 2014 CirrusLogic, Inc.
  5. *
  6. * Author: Brian Austin <brian.austin@cirrus.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/input.h>
  21. #include <linux/regmap.h>
  22. #include <linux/slab.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/of_device.h>
  27. #include <linux/of_gpio.h>
  28. #include <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/soc.h>
  32. #include <sound/soc-dapm.h>
  33. #include <sound/initval.h>
  34. #include <sound/tlv.h>
  35. #include <sound/cs42l56.h>
  36. #include "cs42l56.h"
  37. #define CS42L56_NUM_SUPPLIES 3
  38. static const char *const cs42l56_supply_names[CS42L56_NUM_SUPPLIES] = {
  39. "VA",
  40. "VCP",
  41. "VLDO",
  42. };
  43. struct cs42l56_private {
  44. struct regmap *regmap;
  45. struct snd_soc_codec *codec;
  46. struct device *dev;
  47. struct cs42l56_platform_data pdata;
  48. struct regulator_bulk_data supplies[CS42L56_NUM_SUPPLIES];
  49. u32 mclk;
  50. u8 mclk_prediv;
  51. u8 mclk_div2;
  52. u8 mclk_ratio;
  53. u8 iface;
  54. u8 iface_fmt;
  55. u8 iface_inv;
  56. #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
  57. struct input_dev *beep;
  58. struct work_struct beep_work;
  59. int beep_rate;
  60. #endif
  61. };
  62. static const struct reg_default cs42l56_reg_defaults[] = {
  63. { 1, 0x56 }, /* r01 - ID 1 */
  64. { 2, 0x04 }, /* r02 - ID 2 */
  65. { 3, 0x7f }, /* r03 - Power Ctl 1 */
  66. { 4, 0xff }, /* r04 - Power Ctl 2 */
  67. { 5, 0x00 }, /* ro5 - Clocking Ctl 1 */
  68. { 6, 0x0b }, /* r06 - Clocking Ctl 2 */
  69. { 7, 0x00 }, /* r07 - Serial Format */
  70. { 8, 0x05 }, /* r08 - Class H Ctl */
  71. { 9, 0x0c }, /* r09 - Misc Ctl */
  72. { 10, 0x80 }, /* r0a - INT Status */
  73. { 11, 0x00 }, /* r0b - Playback Ctl */
  74. { 12, 0x0c }, /* r0c - DSP Mute Ctl */
  75. { 13, 0x00 }, /* r0d - ADCA Mixer Volume */
  76. { 14, 0x00 }, /* r0e - ADCB Mixer Volume */
  77. { 15, 0x00 }, /* r0f - PCMA Mixer Volume */
  78. { 16, 0x00 }, /* r10 - PCMB Mixer Volume */
  79. { 17, 0x00 }, /* r11 - Analog Input Advisory Volume */
  80. { 18, 0x00 }, /* r12 - Digital Input Advisory Volume */
  81. { 19, 0x00 }, /* r13 - Master A Volume */
  82. { 20, 0x00 }, /* r14 - Master B Volume */
  83. { 21, 0x00 }, /* r15 - Beep Freq / On Time */
  84. { 22, 0x00 }, /* r16 - Beep Volume / Off Time */
  85. { 23, 0x00 }, /* r17 - Beep Tone Ctl */
  86. { 24, 0x88 }, /* r18 - Tone Ctl */
  87. { 25, 0x00 }, /* r19 - Channel Mixer & Swap */
  88. { 26, 0x00 }, /* r1a - AIN Ref Config / ADC Mux */
  89. { 27, 0xa0 }, /* r1b - High-Pass Filter Ctl */
  90. { 28, 0x00 }, /* r1c - Misc ADC Ctl */
  91. { 29, 0x00 }, /* r1d - Gain & Bias Ctl */
  92. { 30, 0x00 }, /* r1e - PGAA Mux & Volume */
  93. { 31, 0x00 }, /* r1f - PGAB Mux & Volume */
  94. { 32, 0x00 }, /* r20 - ADCA Attenuator */
  95. { 33, 0x00 }, /* r21 - ADCB Attenuator */
  96. { 34, 0x00 }, /* r22 - ALC Enable & Attack Rate */
  97. { 35, 0xbf }, /* r23 - ALC Release Rate */
  98. { 36, 0x00 }, /* r24 - ALC Threshold */
  99. { 37, 0x00 }, /* r25 - Noise Gate Ctl */
  100. { 38, 0x00 }, /* r26 - ALC, Limiter, SFT, ZeroCross */
  101. { 39, 0x00 }, /* r27 - Analog Mute, LO & HP Mux */
  102. { 40, 0x00 }, /* r28 - HP A Volume */
  103. { 41, 0x00 }, /* r29 - HP B Volume */
  104. { 42, 0x00 }, /* r2a - LINEOUT A Volume */
  105. { 43, 0x00 }, /* r2b - LINEOUT B Volume */
  106. { 44, 0x00 }, /* r2c - Limit Threshold Ctl */
  107. { 45, 0x7f }, /* r2d - Limiter Ctl & Release Rate */
  108. { 46, 0x00 }, /* r2e - Limiter Attack Rate */
  109. };
  110. static bool cs42l56_readable_register(struct device *dev, unsigned int reg)
  111. {
  112. switch (reg) {
  113. case CS42L56_CHIP_ID_1 ... CS42L56_LIM_ATTACK_RATE:
  114. return true;
  115. default:
  116. return false;
  117. }
  118. }
  119. static bool cs42l56_volatile_register(struct device *dev, unsigned int reg)
  120. {
  121. switch (reg) {
  122. case CS42L56_INT_STATUS:
  123. return true;
  124. default:
  125. return false;
  126. }
  127. }
  128. static DECLARE_TLV_DB_SCALE(beep_tlv, -5000, 200, 0);
  129. static DECLARE_TLV_DB_SCALE(hl_tlv, -6000, 50, 0);
  130. static DECLARE_TLV_DB_SCALE(adv_tlv, -10200, 50, 0);
  131. static DECLARE_TLV_DB_SCALE(adc_tlv, -9600, 100, 0);
  132. static DECLARE_TLV_DB_SCALE(tone_tlv, -1050, 150, 0);
  133. static DECLARE_TLV_DB_SCALE(preamp_tlv, 0, 1000, 0);
  134. static DECLARE_TLV_DB_SCALE(pga_tlv, -600, 50, 0);
  135. static const DECLARE_TLV_DB_RANGE(ngnb_tlv,
  136. 0, 1, TLV_DB_SCALE_ITEM(-8200, 600, 0),
  137. 2, 5, TLV_DB_SCALE_ITEM(-7600, 300, 0)
  138. );
  139. static const DECLARE_TLV_DB_RANGE(ngb_tlv,
  140. 0, 2, TLV_DB_SCALE_ITEM(-6400, 600, 0),
  141. 3, 7, TLV_DB_SCALE_ITEM(-4600, 300, 0)
  142. );
  143. static const DECLARE_TLV_DB_RANGE(alc_tlv,
  144. 0, 2, TLV_DB_SCALE_ITEM(-3000, 600, 0),
  145. 3, 7, TLV_DB_SCALE_ITEM(-1200, 300, 0)
  146. );
  147. static const char * const beep_config_text[] = {
  148. "Off", "Single", "Multiple", "Continuous"
  149. };
  150. static const struct soc_enum beep_config_enum =
  151. SOC_ENUM_SINGLE(CS42L56_BEEP_TONE_CFG, 6,
  152. ARRAY_SIZE(beep_config_text), beep_config_text);
  153. static const char * const beep_pitch_text[] = {
  154. "C4", "C5", "D5", "E5", "F5", "G5", "A5", "B5",
  155. "C6", "D6", "E6", "F6", "G6", "A6", "B6", "C7"
  156. };
  157. static const struct soc_enum beep_pitch_enum =
  158. SOC_ENUM_SINGLE(CS42L56_BEEP_FREQ_ONTIME, 4,
  159. ARRAY_SIZE(beep_pitch_text), beep_pitch_text);
  160. static const char * const beep_ontime_text[] = {
  161. "86 ms", "430 ms", "780 ms", "1.20 s", "1.50 s",
  162. "1.80 s", "2.20 s", "2.50 s", "2.80 s", "3.20 s",
  163. "3.50 s", "3.80 s", "4.20 s", "4.50 s", "4.80 s", "5.20 s"
  164. };
  165. static const struct soc_enum beep_ontime_enum =
  166. SOC_ENUM_SINGLE(CS42L56_BEEP_FREQ_ONTIME, 0,
  167. ARRAY_SIZE(beep_ontime_text), beep_ontime_text);
  168. static const char * const beep_offtime_text[] = {
  169. "1.23 s", "2.58 s", "3.90 s", "5.20 s",
  170. "6.60 s", "8.05 s", "9.35 s", "10.80 s"
  171. };
  172. static const struct soc_enum beep_offtime_enum =
  173. SOC_ENUM_SINGLE(CS42L56_BEEP_FREQ_OFFTIME, 5,
  174. ARRAY_SIZE(beep_offtime_text), beep_offtime_text);
  175. static const char * const beep_treble_text[] = {
  176. "5kHz", "7kHz", "10kHz", "15kHz"
  177. };
  178. static const struct soc_enum beep_treble_enum =
  179. SOC_ENUM_SINGLE(CS42L56_BEEP_TONE_CFG, 3,
  180. ARRAY_SIZE(beep_treble_text), beep_treble_text);
  181. static const char * const beep_bass_text[] = {
  182. "50Hz", "100Hz", "200Hz", "250Hz"
  183. };
  184. static const struct soc_enum beep_bass_enum =
  185. SOC_ENUM_SINGLE(CS42L56_BEEP_TONE_CFG, 1,
  186. ARRAY_SIZE(beep_bass_text), beep_bass_text);
  187. static const char * const adc_swap_text[] = {
  188. "None", "A+B/2", "A-B/2", "Swap"
  189. };
  190. static const struct soc_enum adc_swap_enum =
  191. SOC_ENUM_SINGLE(CS42L56_MISC_ADC_CTL, 3,
  192. ARRAY_SIZE(adc_swap_text), adc_swap_text);
  193. static const char * const pgaa_mux_text[] = {
  194. "AIN1A", "AIN2A", "AIN3A"};
  195. static const struct soc_enum pgaa_mux_enum =
  196. SOC_ENUM_SINGLE(CS42L56_PGAA_MUX_VOLUME, 0,
  197. ARRAY_SIZE(pgaa_mux_text),
  198. pgaa_mux_text);
  199. static const struct snd_kcontrol_new pgaa_mux =
  200. SOC_DAPM_ENUM("Route", pgaa_mux_enum);
  201. static const char * const pgab_mux_text[] = {
  202. "AIN1B", "AIN2B", "AIN3B"};
  203. static const struct soc_enum pgab_mux_enum =
  204. SOC_ENUM_SINGLE(CS42L56_PGAB_MUX_VOLUME, 0,
  205. ARRAY_SIZE(pgab_mux_text),
  206. pgab_mux_text);
  207. static const struct snd_kcontrol_new pgab_mux =
  208. SOC_DAPM_ENUM("Route", pgab_mux_enum);
  209. static const char * const adca_mux_text[] = {
  210. "PGAA", "AIN1A", "AIN2A", "AIN3A"};
  211. static const struct soc_enum adca_mux_enum =
  212. SOC_ENUM_SINGLE(CS42L56_AIN_REFCFG_ADC_MUX, 0,
  213. ARRAY_SIZE(adca_mux_text),
  214. adca_mux_text);
  215. static const struct snd_kcontrol_new adca_mux =
  216. SOC_DAPM_ENUM("Route", adca_mux_enum);
  217. static const char * const adcb_mux_text[] = {
  218. "PGAB", "AIN1B", "AIN2B", "AIN3B"};
  219. static const struct soc_enum adcb_mux_enum =
  220. SOC_ENUM_SINGLE(CS42L56_AIN_REFCFG_ADC_MUX, 2,
  221. ARRAY_SIZE(adcb_mux_text),
  222. adcb_mux_text);
  223. static const struct snd_kcontrol_new adcb_mux =
  224. SOC_DAPM_ENUM("Route", adcb_mux_enum);
  225. static const char * const left_swap_text[] = {
  226. "Left", "LR 2", "Right"};
  227. static const char * const right_swap_text[] = {
  228. "Right", "LR 2", "Left"};
  229. static const unsigned int swap_values[] = { 0, 1, 3 };
  230. static const struct soc_enum adca_swap_enum =
  231. SOC_VALUE_ENUM_SINGLE(CS42L56_CHAN_MIX_SWAP, 0, 3,
  232. ARRAY_SIZE(left_swap_text),
  233. left_swap_text,
  234. swap_values);
  235. static const struct snd_kcontrol_new adca_swap_mux =
  236. SOC_DAPM_ENUM("Route", adca_swap_enum);
  237. static const struct soc_enum pcma_swap_enum =
  238. SOC_VALUE_ENUM_SINGLE(CS42L56_CHAN_MIX_SWAP, 4, 3,
  239. ARRAY_SIZE(left_swap_text),
  240. left_swap_text,
  241. swap_values);
  242. static const struct snd_kcontrol_new pcma_swap_mux =
  243. SOC_DAPM_ENUM("Route", pcma_swap_enum);
  244. static const struct soc_enum adcb_swap_enum =
  245. SOC_VALUE_ENUM_SINGLE(CS42L56_CHAN_MIX_SWAP, 2, 3,
  246. ARRAY_SIZE(right_swap_text),
  247. right_swap_text,
  248. swap_values);
  249. static const struct snd_kcontrol_new adcb_swap_mux =
  250. SOC_DAPM_ENUM("Route", adcb_swap_enum);
  251. static const struct soc_enum pcmb_swap_enum =
  252. SOC_VALUE_ENUM_SINGLE(CS42L56_CHAN_MIX_SWAP, 6, 3,
  253. ARRAY_SIZE(right_swap_text),
  254. right_swap_text,
  255. swap_values);
  256. static const struct snd_kcontrol_new pcmb_swap_mux =
  257. SOC_DAPM_ENUM("Route", pcmb_swap_enum);
  258. static const struct snd_kcontrol_new hpa_switch =
  259. SOC_DAPM_SINGLE("Switch", CS42L56_PWRCTL_2, 6, 1, 1);
  260. static const struct snd_kcontrol_new hpb_switch =
  261. SOC_DAPM_SINGLE("Switch", CS42L56_PWRCTL_2, 4, 1, 1);
  262. static const struct snd_kcontrol_new loa_switch =
  263. SOC_DAPM_SINGLE("Switch", CS42L56_PWRCTL_2, 2, 1, 1);
  264. static const struct snd_kcontrol_new lob_switch =
  265. SOC_DAPM_SINGLE("Switch", CS42L56_PWRCTL_2, 0, 1, 1);
  266. static const char * const hploa_input_text[] = {
  267. "DACA", "PGAA"};
  268. static const struct soc_enum lineouta_input_enum =
  269. SOC_ENUM_SINGLE(CS42L56_AMUTE_HPLO_MUX, 2,
  270. ARRAY_SIZE(hploa_input_text),
  271. hploa_input_text);
  272. static const struct snd_kcontrol_new lineouta_input =
  273. SOC_DAPM_ENUM("Route", lineouta_input_enum);
  274. static const struct soc_enum hpa_input_enum =
  275. SOC_ENUM_SINGLE(CS42L56_AMUTE_HPLO_MUX, 0,
  276. ARRAY_SIZE(hploa_input_text),
  277. hploa_input_text);
  278. static const struct snd_kcontrol_new hpa_input =
  279. SOC_DAPM_ENUM("Route", hpa_input_enum);
  280. static const char * const hplob_input_text[] = {
  281. "DACB", "PGAB"};
  282. static const struct soc_enum lineoutb_input_enum =
  283. SOC_ENUM_SINGLE(CS42L56_AMUTE_HPLO_MUX, 3,
  284. ARRAY_SIZE(hplob_input_text),
  285. hplob_input_text);
  286. static const struct snd_kcontrol_new lineoutb_input =
  287. SOC_DAPM_ENUM("Route", lineoutb_input_enum);
  288. static const struct soc_enum hpb_input_enum =
  289. SOC_ENUM_SINGLE(CS42L56_AMUTE_HPLO_MUX, 1,
  290. ARRAY_SIZE(hplob_input_text),
  291. hplob_input_text);
  292. static const struct snd_kcontrol_new hpb_input =
  293. SOC_DAPM_ENUM("Route", hpb_input_enum);
  294. static const char * const dig_mux_text[] = {
  295. "ADC", "DSP"};
  296. static const struct soc_enum dig_mux_enum =
  297. SOC_ENUM_SINGLE(CS42L56_MISC_CTL, 7,
  298. ARRAY_SIZE(dig_mux_text),
  299. dig_mux_text);
  300. static const struct snd_kcontrol_new dig_mux =
  301. SOC_DAPM_ENUM("Route", dig_mux_enum);
  302. static const char * const hpf_freq_text[] = {
  303. "1.8Hz", "119Hz", "236Hz", "464Hz"
  304. };
  305. static const struct soc_enum hpfa_freq_enum =
  306. SOC_ENUM_SINGLE(CS42L56_HPF_CTL, 0,
  307. ARRAY_SIZE(hpf_freq_text), hpf_freq_text);
  308. static const struct soc_enum hpfb_freq_enum =
  309. SOC_ENUM_SINGLE(CS42L56_HPF_CTL, 2,
  310. ARRAY_SIZE(hpf_freq_text), hpf_freq_text);
  311. static const char * const ng_delay_text[] = {
  312. "50ms", "100ms", "150ms", "200ms"
  313. };
  314. static const struct soc_enum ng_delay_enum =
  315. SOC_ENUM_SINGLE(CS42L56_NOISE_GATE_CTL, 0,
  316. ARRAY_SIZE(ng_delay_text), ng_delay_text);
  317. static const struct snd_kcontrol_new cs42l56_snd_controls[] = {
  318. SOC_DOUBLE_R_SX_TLV("Master Volume", CS42L56_MASTER_A_VOLUME,
  319. CS42L56_MASTER_B_VOLUME, 0, 0x34, 0xE4, adv_tlv),
  320. SOC_DOUBLE("Master Mute Switch", CS42L56_DSP_MUTE_CTL, 0, 1, 1, 1),
  321. SOC_DOUBLE_R_SX_TLV("ADC Mixer Volume", CS42L56_ADCA_MIX_VOLUME,
  322. CS42L56_ADCB_MIX_VOLUME, 0, 0x88, 0x90, hl_tlv),
  323. SOC_DOUBLE("ADC Mixer Mute Switch", CS42L56_DSP_MUTE_CTL, 6, 7, 1, 1),
  324. SOC_DOUBLE_R_SX_TLV("PCM Mixer Volume", CS42L56_PCMA_MIX_VOLUME,
  325. CS42L56_PCMB_MIX_VOLUME, 0, 0x88, 0x90, hl_tlv),
  326. SOC_DOUBLE("PCM Mixer Mute Switch", CS42L56_DSP_MUTE_CTL, 4, 5, 1, 1),
  327. SOC_SINGLE_TLV("Analog Advisory Volume",
  328. CS42L56_ANAINPUT_ADV_VOLUME, 0, 0x00, 1, adv_tlv),
  329. SOC_SINGLE_TLV("Digital Advisory Volume",
  330. CS42L56_DIGINPUT_ADV_VOLUME, 0, 0x00, 1, adv_tlv),
  331. SOC_DOUBLE_R_SX_TLV("PGA Volume", CS42L56_PGAA_MUX_VOLUME,
  332. CS42L56_PGAB_MUX_VOLUME, 0, 0x34, 0x24, pga_tlv),
  333. SOC_DOUBLE_R_TLV("ADC Volume", CS42L56_ADCA_ATTENUATOR,
  334. CS42L56_ADCB_ATTENUATOR, 0, 0x00, 1, adc_tlv),
  335. SOC_DOUBLE("ADC Mute Switch", CS42L56_MISC_ADC_CTL, 2, 3, 1, 1),
  336. SOC_DOUBLE("ADC Boost Switch", CS42L56_GAIN_BIAS_CTL, 3, 2, 1, 1),
  337. SOC_DOUBLE_R_SX_TLV("Headphone Volume", CS42L56_HPA_VOLUME,
  338. CS42L56_HPB_VOLUME, 0, 0x84, 0x48, hl_tlv),
  339. SOC_DOUBLE_R_SX_TLV("LineOut Volume", CS42L56_LOA_VOLUME,
  340. CS42L56_LOB_VOLUME, 0, 0x84, 0x48, hl_tlv),
  341. SOC_SINGLE_TLV("Bass Shelving Volume", CS42L56_TONE_CTL,
  342. 0, 0x00, 1, tone_tlv),
  343. SOC_SINGLE_TLV("Treble Shelving Volume", CS42L56_TONE_CTL,
  344. 4, 0x00, 1, tone_tlv),
  345. SOC_DOUBLE_TLV("PGA Preamp Volume", CS42L56_GAIN_BIAS_CTL,
  346. 4, 6, 0x02, 1, preamp_tlv),
  347. SOC_SINGLE("DSP Switch", CS42L56_PLAYBACK_CTL, 7, 1, 1),
  348. SOC_SINGLE("Gang Playback Switch", CS42L56_PLAYBACK_CTL, 4, 1, 1),
  349. SOC_SINGLE("Gang ADC Switch", CS42L56_MISC_ADC_CTL, 7, 1, 1),
  350. SOC_SINGLE("Gang PGA Switch", CS42L56_MISC_ADC_CTL, 6, 1, 1),
  351. SOC_SINGLE("PCMA Invert", CS42L56_PLAYBACK_CTL, 2, 1, 1),
  352. SOC_SINGLE("PCMB Invert", CS42L56_PLAYBACK_CTL, 3, 1, 1),
  353. SOC_SINGLE("ADCA Invert", CS42L56_MISC_ADC_CTL, 2, 1, 1),
  354. SOC_SINGLE("ADCB Invert", CS42L56_MISC_ADC_CTL, 3, 1, 1),
  355. SOC_DOUBLE("HPF Switch", CS42L56_HPF_CTL, 5, 7, 1, 1),
  356. SOC_DOUBLE("HPF Freeze Switch", CS42L56_HPF_CTL, 4, 6, 1, 1),
  357. SOC_ENUM("HPFA Corner Freq", hpfa_freq_enum),
  358. SOC_ENUM("HPFB Corner Freq", hpfb_freq_enum),
  359. SOC_SINGLE("Analog Soft Ramp", CS42L56_MISC_CTL, 4, 1, 1),
  360. SOC_DOUBLE("Analog Soft Ramp Disable", CS42L56_ALC_LIM_SFT_ZC,
  361. 7, 5, 1, 1),
  362. SOC_SINGLE("Analog Zero Cross", CS42L56_MISC_CTL, 3, 1, 1),
  363. SOC_DOUBLE("Analog Zero Cross Disable", CS42L56_ALC_LIM_SFT_ZC,
  364. 6, 4, 1, 1),
  365. SOC_SINGLE("Digital Soft Ramp", CS42L56_MISC_CTL, 2, 1, 1),
  366. SOC_SINGLE("Digital Soft Ramp Disable", CS42L56_ALC_LIM_SFT_ZC,
  367. 3, 1, 1),
  368. SOC_SINGLE("HL Deemphasis", CS42L56_PLAYBACK_CTL, 6, 1, 1),
  369. SOC_SINGLE("ALC Switch", CS42L56_ALC_EN_ATTACK_RATE, 6, 1, 1),
  370. SOC_SINGLE("ALC Limit All Switch", CS42L56_ALC_RELEASE_RATE, 7, 1, 1),
  371. SOC_SINGLE_RANGE("ALC Attack", CS42L56_ALC_EN_ATTACK_RATE,
  372. 0, 0, 0x3f, 0),
  373. SOC_SINGLE_RANGE("ALC Release", CS42L56_ALC_RELEASE_RATE,
  374. 0, 0x3f, 0, 0),
  375. SOC_SINGLE_TLV("ALC MAX", CS42L56_ALC_THRESHOLD,
  376. 5, 0x07, 1, alc_tlv),
  377. SOC_SINGLE_TLV("ALC MIN", CS42L56_ALC_THRESHOLD,
  378. 2, 0x07, 1, alc_tlv),
  379. SOC_SINGLE("Limiter Switch", CS42L56_LIM_CTL_RELEASE_RATE, 7, 1, 1),
  380. SOC_SINGLE("Limit All Switch", CS42L56_LIM_CTL_RELEASE_RATE, 6, 1, 1),
  381. SOC_SINGLE_RANGE("Limiter Attack", CS42L56_LIM_ATTACK_RATE,
  382. 0, 0, 0x3f, 0),
  383. SOC_SINGLE_RANGE("Limiter Release", CS42L56_LIM_CTL_RELEASE_RATE,
  384. 0, 0x3f, 0, 0),
  385. SOC_SINGLE_TLV("Limiter MAX", CS42L56_LIM_THRESHOLD_CTL,
  386. 5, 0x07, 1, alc_tlv),
  387. SOC_SINGLE_TLV("Limiter Cushion", CS42L56_ALC_THRESHOLD,
  388. 2, 0x07, 1, alc_tlv),
  389. SOC_SINGLE("NG Switch", CS42L56_NOISE_GATE_CTL, 6, 1, 1),
  390. SOC_SINGLE("NG All Switch", CS42L56_NOISE_GATE_CTL, 7, 1, 1),
  391. SOC_SINGLE("NG Boost Switch", CS42L56_NOISE_GATE_CTL, 5, 1, 1),
  392. SOC_SINGLE_TLV("NG Unboost Threshold", CS42L56_NOISE_GATE_CTL,
  393. 2, 0x07, 1, ngnb_tlv),
  394. SOC_SINGLE_TLV("NG Boost Threshold", CS42L56_NOISE_GATE_CTL,
  395. 2, 0x07, 1, ngb_tlv),
  396. SOC_ENUM("NG Delay", ng_delay_enum),
  397. SOC_ENUM("Beep Config", beep_config_enum),
  398. SOC_ENUM("Beep Pitch", beep_pitch_enum),
  399. SOC_ENUM("Beep on Time", beep_ontime_enum),
  400. SOC_ENUM("Beep off Time", beep_offtime_enum),
  401. SOC_SINGLE_SX_TLV("Beep Volume", CS42L56_BEEP_FREQ_OFFTIME,
  402. 0, 0x07, 0x23, beep_tlv),
  403. SOC_SINGLE("Beep Tone Ctl Switch", CS42L56_BEEP_TONE_CFG, 0, 1, 1),
  404. SOC_ENUM("Beep Treble Corner Freq", beep_treble_enum),
  405. SOC_ENUM("Beep Bass Corner Freq", beep_bass_enum),
  406. };
  407. static const struct snd_soc_dapm_widget cs42l56_dapm_widgets[] = {
  408. SND_SOC_DAPM_SIGGEN("Beep"),
  409. SND_SOC_DAPM_SUPPLY("VBUF", CS42L56_PWRCTL_1, 5, 1, NULL, 0),
  410. SND_SOC_DAPM_MICBIAS("MIC1 Bias", CS42L56_PWRCTL_1, 4, 1),
  411. SND_SOC_DAPM_SUPPLY("Charge Pump", CS42L56_PWRCTL_1, 3, 1, NULL, 0),
  412. SND_SOC_DAPM_INPUT("AIN1A"),
  413. SND_SOC_DAPM_INPUT("AIN2A"),
  414. SND_SOC_DAPM_INPUT("AIN1B"),
  415. SND_SOC_DAPM_INPUT("AIN2B"),
  416. SND_SOC_DAPM_INPUT("AIN3A"),
  417. SND_SOC_DAPM_INPUT("AIN3B"),
  418. SND_SOC_DAPM_AIF_OUT("SDOUT", NULL, 0,
  419. SND_SOC_NOPM, 0, 0),
  420. SND_SOC_DAPM_AIF_IN("SDIN", NULL, 0,
  421. SND_SOC_NOPM, 0, 0),
  422. SND_SOC_DAPM_MUX("Digital Output Mux", SND_SOC_NOPM,
  423. 0, 0, &dig_mux),
  424. SND_SOC_DAPM_PGA("PGAA", SND_SOC_NOPM, 0, 0, NULL, 0),
  425. SND_SOC_DAPM_PGA("PGAB", SND_SOC_NOPM, 0, 0, NULL, 0),
  426. SND_SOC_DAPM_MUX("PGAA Input Mux",
  427. SND_SOC_NOPM, 0, 0, &pgaa_mux),
  428. SND_SOC_DAPM_MUX("PGAB Input Mux",
  429. SND_SOC_NOPM, 0, 0, &pgab_mux),
  430. SND_SOC_DAPM_MUX("ADCA Mux", SND_SOC_NOPM,
  431. 0, 0, &adca_mux),
  432. SND_SOC_DAPM_MUX("ADCB Mux", SND_SOC_NOPM,
  433. 0, 0, &adcb_mux),
  434. SND_SOC_DAPM_ADC("ADCA", NULL, CS42L56_PWRCTL_1, 1, 1),
  435. SND_SOC_DAPM_ADC("ADCB", NULL, CS42L56_PWRCTL_1, 2, 1),
  436. SND_SOC_DAPM_MUX("ADCA Swap Mux", SND_SOC_NOPM, 0, 0,
  437. &adca_swap_mux),
  438. SND_SOC_DAPM_MUX("ADCB Swap Mux", SND_SOC_NOPM, 0, 0,
  439. &adcb_swap_mux),
  440. SND_SOC_DAPM_MUX("PCMA Swap Mux", SND_SOC_NOPM, 0, 0,
  441. &pcma_swap_mux),
  442. SND_SOC_DAPM_MUX("PCMB Swap Mux", SND_SOC_NOPM, 0, 0,
  443. &pcmb_swap_mux),
  444. SND_SOC_DAPM_DAC("DACA", NULL, SND_SOC_NOPM, 0, 0),
  445. SND_SOC_DAPM_DAC("DACB", NULL, SND_SOC_NOPM, 0, 0),
  446. SND_SOC_DAPM_OUTPUT("HPA"),
  447. SND_SOC_DAPM_OUTPUT("LOA"),
  448. SND_SOC_DAPM_OUTPUT("HPB"),
  449. SND_SOC_DAPM_OUTPUT("LOB"),
  450. SND_SOC_DAPM_SWITCH("Headphone Right",
  451. CS42L56_PWRCTL_2, 4, 1, &hpb_switch),
  452. SND_SOC_DAPM_SWITCH("Headphone Left",
  453. CS42L56_PWRCTL_2, 6, 1, &hpa_switch),
  454. SND_SOC_DAPM_SWITCH("Lineout Right",
  455. CS42L56_PWRCTL_2, 0, 1, &lob_switch),
  456. SND_SOC_DAPM_SWITCH("Lineout Left",
  457. CS42L56_PWRCTL_2, 2, 1, &loa_switch),
  458. SND_SOC_DAPM_MUX("LINEOUTA Input Mux", SND_SOC_NOPM,
  459. 0, 0, &lineouta_input),
  460. SND_SOC_DAPM_MUX("LINEOUTB Input Mux", SND_SOC_NOPM,
  461. 0, 0, &lineoutb_input),
  462. SND_SOC_DAPM_MUX("HPA Input Mux", SND_SOC_NOPM,
  463. 0, 0, &hpa_input),
  464. SND_SOC_DAPM_MUX("HPB Input Mux", SND_SOC_NOPM,
  465. 0, 0, &hpb_input),
  466. };
  467. static const struct snd_soc_dapm_route cs42l56_audio_map[] = {
  468. {"HiFi Capture", "DSP", "Digital Output Mux"},
  469. {"HiFi Capture", "ADC", "Digital Output Mux"},
  470. {"Digital Output Mux", NULL, "ADCA"},
  471. {"Digital Output Mux", NULL, "ADCB"},
  472. {"ADCB", NULL, "ADCB Swap Mux"},
  473. {"ADCA", NULL, "ADCA Swap Mux"},
  474. {"ADCA Swap Mux", NULL, "ADCA"},
  475. {"ADCB Swap Mux", NULL, "ADCB"},
  476. {"DACA", "Left", "ADCA Swap Mux"},
  477. {"DACA", "LR 2", "ADCA Swap Mux"},
  478. {"DACA", "Right", "ADCA Swap Mux"},
  479. {"DACB", "Left", "ADCB Swap Mux"},
  480. {"DACB", "LR 2", "ADCB Swap Mux"},
  481. {"DACB", "Right", "ADCB Swap Mux"},
  482. {"ADCA Mux", NULL, "AIN3A"},
  483. {"ADCA Mux", NULL, "AIN2A"},
  484. {"ADCA Mux", NULL, "AIN1A"},
  485. {"ADCA Mux", NULL, "PGAA"},
  486. {"ADCB Mux", NULL, "AIN3B"},
  487. {"ADCB Mux", NULL, "AIN2B"},
  488. {"ADCB Mux", NULL, "AIN1B"},
  489. {"ADCB Mux", NULL, "PGAB"},
  490. {"PGAA", "AIN1A", "PGAA Input Mux"},
  491. {"PGAA", "AIN2A", "PGAA Input Mux"},
  492. {"PGAA", "AIN3A", "PGAA Input Mux"},
  493. {"PGAB", "AIN1B", "PGAB Input Mux"},
  494. {"PGAB", "AIN2B", "PGAB Input Mux"},
  495. {"PGAB", "AIN3B", "PGAB Input Mux"},
  496. {"PGAA Input Mux", NULL, "AIN1A"},
  497. {"PGAA Input Mux", NULL, "AIN2A"},
  498. {"PGAA Input Mux", NULL, "AIN3A"},
  499. {"PGAB Input Mux", NULL, "AIN1B"},
  500. {"PGAB Input Mux", NULL, "AIN2B"},
  501. {"PGAB Input Mux", NULL, "AIN3B"},
  502. {"LOB", "Switch", "LINEOUTB Input Mux"},
  503. {"LOA", "Switch", "LINEOUTA Input Mux"},
  504. {"LINEOUTA Input Mux", "PGAA", "PGAA"},
  505. {"LINEOUTB Input Mux", "PGAB", "PGAB"},
  506. {"LINEOUTA Input Mux", "DACA", "DACA"},
  507. {"LINEOUTB Input Mux", "DACB", "DACB"},
  508. {"HPA", "Switch", "HPB Input Mux"},
  509. {"HPB", "Switch", "HPA Input Mux"},
  510. {"HPA Input Mux", "PGAA", "PGAA"},
  511. {"HPB Input Mux", "PGAB", "PGAB"},
  512. {"HPA Input Mux", "DACA", "DACA"},
  513. {"HPB Input Mux", "DACB", "DACB"},
  514. {"DACA", NULL, "PCMA Swap Mux"},
  515. {"DACB", NULL, "PCMB Swap Mux"},
  516. {"PCMB Swap Mux", "Left", "HiFi Playback"},
  517. {"PCMB Swap Mux", "LR 2", "HiFi Playback"},
  518. {"PCMB Swap Mux", "Right", "HiFi Playback"},
  519. {"PCMA Swap Mux", "Left", "HiFi Playback"},
  520. {"PCMA Swap Mux", "LR 2", "HiFi Playback"},
  521. {"PCMA Swap Mux", "Right", "HiFi Playback"},
  522. };
  523. struct cs42l56_clk_para {
  524. u32 mclk;
  525. u32 srate;
  526. u8 ratio;
  527. };
  528. static const struct cs42l56_clk_para clk_ratio_table[] = {
  529. /* 8k */
  530. { 6000000, 8000, CS42L56_MCLK_LRCLK_768 },
  531. { 6144000, 8000, CS42L56_MCLK_LRCLK_750 },
  532. { 12000000, 8000, CS42L56_MCLK_LRCLK_768 },
  533. { 12288000, 8000, CS42L56_MCLK_LRCLK_750 },
  534. { 24000000, 8000, CS42L56_MCLK_LRCLK_768 },
  535. { 24576000, 8000, CS42L56_MCLK_LRCLK_750 },
  536. /* 11.025k */
  537. { 5644800, 11025, CS42L56_MCLK_LRCLK_512},
  538. { 11289600, 11025, CS42L56_MCLK_LRCLK_512},
  539. { 22579200, 11025, CS42L56_MCLK_LRCLK_512 },
  540. /* 11.0294k */
  541. { 6000000, 110294, CS42L56_MCLK_LRCLK_544 },
  542. { 12000000, 110294, CS42L56_MCLK_LRCLK_544 },
  543. { 24000000, 110294, CS42L56_MCLK_LRCLK_544 },
  544. /* 12k */
  545. { 6000000, 12000, CS42L56_MCLK_LRCLK_500 },
  546. { 6144000, 12000, CS42L56_MCLK_LRCLK_512 },
  547. { 12000000, 12000, CS42L56_MCLK_LRCLK_500 },
  548. { 12288000, 12000, CS42L56_MCLK_LRCLK_512 },
  549. { 24000000, 12000, CS42L56_MCLK_LRCLK_500 },
  550. { 24576000, 12000, CS42L56_MCLK_LRCLK_512 },
  551. /* 16k */
  552. { 6000000, 16000, CS42L56_MCLK_LRCLK_375 },
  553. { 6144000, 16000, CS42L56_MCLK_LRCLK_384 },
  554. { 12000000, 16000, CS42L56_MCLK_LRCLK_375 },
  555. { 12288000, 16000, CS42L56_MCLK_LRCLK_384 },
  556. { 24000000, 16000, CS42L56_MCLK_LRCLK_375 },
  557. { 24576000, 16000, CS42L56_MCLK_LRCLK_384 },
  558. /* 22.050k */
  559. { 5644800, 22050, CS42L56_MCLK_LRCLK_256 },
  560. { 11289600, 22050, CS42L56_MCLK_LRCLK_256 },
  561. { 22579200, 22050, CS42L56_MCLK_LRCLK_256 },
  562. /* 22.0588k */
  563. { 6000000, 220588, CS42L56_MCLK_LRCLK_272 },
  564. { 12000000, 220588, CS42L56_MCLK_LRCLK_272 },
  565. { 24000000, 220588, CS42L56_MCLK_LRCLK_272 },
  566. /* 24k */
  567. { 6000000, 24000, CS42L56_MCLK_LRCLK_250 },
  568. { 6144000, 24000, CS42L56_MCLK_LRCLK_256 },
  569. { 12000000, 24000, CS42L56_MCLK_LRCLK_250 },
  570. { 12288000, 24000, CS42L56_MCLK_LRCLK_256 },
  571. { 24000000, 24000, CS42L56_MCLK_LRCLK_250 },
  572. { 24576000, 24000, CS42L56_MCLK_LRCLK_256 },
  573. /* 32k */
  574. { 6000000, 32000, CS42L56_MCLK_LRCLK_187P5 },
  575. { 6144000, 32000, CS42L56_MCLK_LRCLK_192 },
  576. { 12000000, 32000, CS42L56_MCLK_LRCLK_187P5 },
  577. { 12288000, 32000, CS42L56_MCLK_LRCLK_192 },
  578. { 24000000, 32000, CS42L56_MCLK_LRCLK_187P5 },
  579. { 24576000, 32000, CS42L56_MCLK_LRCLK_192 },
  580. /* 44.118k */
  581. { 6000000, 44118, CS42L56_MCLK_LRCLK_136 },
  582. { 12000000, 44118, CS42L56_MCLK_LRCLK_136 },
  583. { 24000000, 44118, CS42L56_MCLK_LRCLK_136 },
  584. /* 44.1k */
  585. { 5644800, 44100, CS42L56_MCLK_LRCLK_128 },
  586. { 11289600, 44100, CS42L56_MCLK_LRCLK_128 },
  587. { 22579200, 44100, CS42L56_MCLK_LRCLK_128 },
  588. /* 48k */
  589. { 6000000, 48000, CS42L56_MCLK_LRCLK_125 },
  590. { 6144000, 48000, CS42L56_MCLK_LRCLK_128 },
  591. { 12000000, 48000, CS42L56_MCLK_LRCLK_125 },
  592. { 12288000, 48000, CS42L56_MCLK_LRCLK_128 },
  593. { 24000000, 48000, CS42L56_MCLK_LRCLK_125 },
  594. { 24576000, 48000, CS42L56_MCLK_LRCLK_128 },
  595. };
  596. static int cs42l56_get_mclk_ratio(int mclk, int rate)
  597. {
  598. int i;
  599. for (i = 0; i < ARRAY_SIZE(clk_ratio_table); i++) {
  600. if (clk_ratio_table[i].mclk == mclk &&
  601. clk_ratio_table[i].srate == rate)
  602. return clk_ratio_table[i].ratio;
  603. }
  604. return -EINVAL;
  605. }
  606. static int cs42l56_set_sysclk(struct snd_soc_dai *codec_dai,
  607. int clk_id, unsigned int freq, int dir)
  608. {
  609. struct snd_soc_codec *codec = codec_dai->codec;
  610. struct cs42l56_private *cs42l56 = snd_soc_codec_get_drvdata(codec);
  611. switch (freq) {
  612. case CS42L56_MCLK_5P6448MHZ:
  613. case CS42L56_MCLK_6MHZ:
  614. case CS42L56_MCLK_6P144MHZ:
  615. cs42l56->mclk_div2 = 0;
  616. cs42l56->mclk_prediv = 0;
  617. break;
  618. case CS42L56_MCLK_11P2896MHZ:
  619. case CS42L56_MCLK_12MHZ:
  620. case CS42L56_MCLK_12P288MHZ:
  621. cs42l56->mclk_div2 = CS42L56_MCLK_DIV2;
  622. cs42l56->mclk_prediv = 0;
  623. break;
  624. case CS42L56_MCLK_22P5792MHZ:
  625. case CS42L56_MCLK_24MHZ:
  626. case CS42L56_MCLK_24P576MHZ:
  627. cs42l56->mclk_div2 = CS42L56_MCLK_DIV2;
  628. cs42l56->mclk_prediv = CS42L56_MCLK_PREDIV;
  629. break;
  630. default:
  631. return -EINVAL;
  632. }
  633. cs42l56->mclk = freq;
  634. snd_soc_update_bits(codec, CS42L56_CLKCTL_1,
  635. CS42L56_MCLK_PREDIV_MASK,
  636. cs42l56->mclk_prediv);
  637. snd_soc_update_bits(codec, CS42L56_CLKCTL_1,
  638. CS42L56_MCLK_DIV2_MASK,
  639. cs42l56->mclk_div2);
  640. return 0;
  641. }
  642. static int cs42l56_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
  643. {
  644. struct snd_soc_codec *codec = codec_dai->codec;
  645. struct cs42l56_private *cs42l56 = snd_soc_codec_get_drvdata(codec);
  646. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  647. case SND_SOC_DAIFMT_CBM_CFM:
  648. cs42l56->iface = CS42L56_MASTER_MODE;
  649. break;
  650. case SND_SOC_DAIFMT_CBS_CFS:
  651. cs42l56->iface = CS42L56_SLAVE_MODE;
  652. break;
  653. default:
  654. return -EINVAL;
  655. }
  656. /* interface format */
  657. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  658. case SND_SOC_DAIFMT_I2S:
  659. cs42l56->iface_fmt = CS42L56_DIG_FMT_I2S;
  660. break;
  661. case SND_SOC_DAIFMT_LEFT_J:
  662. cs42l56->iface_fmt = CS42L56_DIG_FMT_LEFT_J;
  663. break;
  664. default:
  665. return -EINVAL;
  666. }
  667. /* sclk inversion */
  668. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  669. case SND_SOC_DAIFMT_NB_NF:
  670. cs42l56->iface_inv = 0;
  671. break;
  672. case SND_SOC_DAIFMT_IB_NF:
  673. cs42l56->iface_inv = CS42L56_SCLK_INV;
  674. break;
  675. default:
  676. return -EINVAL;
  677. }
  678. snd_soc_update_bits(codec, CS42L56_CLKCTL_1,
  679. CS42L56_MS_MODE_MASK, cs42l56->iface);
  680. snd_soc_update_bits(codec, CS42L56_SERIAL_FMT,
  681. CS42L56_DIG_FMT_MASK, cs42l56->iface_fmt);
  682. snd_soc_update_bits(codec, CS42L56_CLKCTL_1,
  683. CS42L56_SCLK_INV_MASK, cs42l56->iface_inv);
  684. return 0;
  685. }
  686. static int cs42l56_digital_mute(struct snd_soc_dai *dai, int mute)
  687. {
  688. struct snd_soc_codec *codec = dai->codec;
  689. if (mute) {
  690. /* Hit the DSP Mixer first */
  691. snd_soc_update_bits(codec, CS42L56_DSP_MUTE_CTL,
  692. CS42L56_ADCAMIX_MUTE_MASK |
  693. CS42L56_ADCBMIX_MUTE_MASK |
  694. CS42L56_PCMAMIX_MUTE_MASK |
  695. CS42L56_PCMBMIX_MUTE_MASK |
  696. CS42L56_MSTB_MUTE_MASK |
  697. CS42L56_MSTA_MUTE_MASK,
  698. CS42L56_MUTE_ALL);
  699. /* Mute ADC's */
  700. snd_soc_update_bits(codec, CS42L56_MISC_ADC_CTL,
  701. CS42L56_ADCA_MUTE_MASK |
  702. CS42L56_ADCB_MUTE_MASK,
  703. CS42L56_MUTE_ALL);
  704. /* HP And LO */
  705. snd_soc_update_bits(codec, CS42L56_HPA_VOLUME,
  706. CS42L56_HP_MUTE_MASK, CS42L56_MUTE_ALL);
  707. snd_soc_update_bits(codec, CS42L56_HPB_VOLUME,
  708. CS42L56_HP_MUTE_MASK, CS42L56_MUTE_ALL);
  709. snd_soc_update_bits(codec, CS42L56_LOA_VOLUME,
  710. CS42L56_LO_MUTE_MASK, CS42L56_MUTE_ALL);
  711. snd_soc_update_bits(codec, CS42L56_LOB_VOLUME,
  712. CS42L56_LO_MUTE_MASK, CS42L56_MUTE_ALL);
  713. } else {
  714. snd_soc_update_bits(codec, CS42L56_DSP_MUTE_CTL,
  715. CS42L56_ADCAMIX_MUTE_MASK |
  716. CS42L56_ADCBMIX_MUTE_MASK |
  717. CS42L56_PCMAMIX_MUTE_MASK |
  718. CS42L56_PCMBMIX_MUTE_MASK |
  719. CS42L56_MSTB_MUTE_MASK |
  720. CS42L56_MSTA_MUTE_MASK,
  721. CS42L56_UNMUTE);
  722. snd_soc_update_bits(codec, CS42L56_MISC_ADC_CTL,
  723. CS42L56_ADCA_MUTE_MASK |
  724. CS42L56_ADCB_MUTE_MASK,
  725. CS42L56_UNMUTE);
  726. snd_soc_update_bits(codec, CS42L56_HPA_VOLUME,
  727. CS42L56_HP_MUTE_MASK, CS42L56_UNMUTE);
  728. snd_soc_update_bits(codec, CS42L56_HPB_VOLUME,
  729. CS42L56_HP_MUTE_MASK, CS42L56_UNMUTE);
  730. snd_soc_update_bits(codec, CS42L56_LOA_VOLUME,
  731. CS42L56_LO_MUTE_MASK, CS42L56_UNMUTE);
  732. snd_soc_update_bits(codec, CS42L56_LOB_VOLUME,
  733. CS42L56_LO_MUTE_MASK, CS42L56_UNMUTE);
  734. }
  735. return 0;
  736. }
  737. static int cs42l56_pcm_hw_params(struct snd_pcm_substream *substream,
  738. struct snd_pcm_hw_params *params,
  739. struct snd_soc_dai *dai)
  740. {
  741. struct snd_soc_codec *codec = dai->codec;
  742. struct cs42l56_private *cs42l56 = snd_soc_codec_get_drvdata(codec);
  743. int ratio;
  744. ratio = cs42l56_get_mclk_ratio(cs42l56->mclk, params_rate(params));
  745. if (ratio >= 0) {
  746. snd_soc_update_bits(codec, CS42L56_CLKCTL_2,
  747. CS42L56_CLK_RATIO_MASK, ratio);
  748. } else {
  749. dev_err(codec->dev, "unsupported mclk/sclk/lrclk ratio\n");
  750. return -EINVAL;
  751. }
  752. return 0;
  753. }
  754. static int cs42l56_set_bias_level(struct snd_soc_codec *codec,
  755. enum snd_soc_bias_level level)
  756. {
  757. struct cs42l56_private *cs42l56 = snd_soc_codec_get_drvdata(codec);
  758. int ret;
  759. switch (level) {
  760. case SND_SOC_BIAS_ON:
  761. break;
  762. case SND_SOC_BIAS_PREPARE:
  763. snd_soc_update_bits(codec, CS42L56_CLKCTL_1,
  764. CS42L56_MCLK_DIS_MASK, 0);
  765. snd_soc_update_bits(codec, CS42L56_PWRCTL_1,
  766. CS42L56_PDN_ALL_MASK, 0);
  767. break;
  768. case SND_SOC_BIAS_STANDBY:
  769. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  770. regcache_cache_only(cs42l56->regmap, false);
  771. regcache_sync(cs42l56->regmap);
  772. ret = regulator_bulk_enable(ARRAY_SIZE(cs42l56->supplies),
  773. cs42l56->supplies);
  774. if (ret != 0) {
  775. dev_err(cs42l56->dev,
  776. "Failed to enable regulators: %d\n",
  777. ret);
  778. return ret;
  779. }
  780. }
  781. snd_soc_update_bits(codec, CS42L56_PWRCTL_1,
  782. CS42L56_PDN_ALL_MASK, 1);
  783. break;
  784. case SND_SOC_BIAS_OFF:
  785. snd_soc_update_bits(codec, CS42L56_PWRCTL_1,
  786. CS42L56_PDN_ALL_MASK, 1);
  787. snd_soc_update_bits(codec, CS42L56_CLKCTL_1,
  788. CS42L56_MCLK_DIS_MASK, 1);
  789. regcache_cache_only(cs42l56->regmap, true);
  790. regulator_bulk_disable(ARRAY_SIZE(cs42l56->supplies),
  791. cs42l56->supplies);
  792. break;
  793. }
  794. return 0;
  795. }
  796. #define CS42L56_RATES (SNDRV_PCM_RATE_8000_48000)
  797. #define CS42L56_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S18_3LE | \
  798. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE | \
  799. SNDRV_PCM_FMTBIT_S32_LE)
  800. static const struct snd_soc_dai_ops cs42l56_ops = {
  801. .hw_params = cs42l56_pcm_hw_params,
  802. .digital_mute = cs42l56_digital_mute,
  803. .set_fmt = cs42l56_set_dai_fmt,
  804. .set_sysclk = cs42l56_set_sysclk,
  805. };
  806. static struct snd_soc_dai_driver cs42l56_dai = {
  807. .name = "cs42l56",
  808. .playback = {
  809. .stream_name = "HiFi Playback",
  810. .channels_min = 1,
  811. .channels_max = 2,
  812. .rates = CS42L56_RATES,
  813. .formats = CS42L56_FORMATS,
  814. },
  815. .capture = {
  816. .stream_name = "HiFi Capture",
  817. .channels_min = 1,
  818. .channels_max = 2,
  819. .rates = CS42L56_RATES,
  820. .formats = CS42L56_FORMATS,
  821. },
  822. .ops = &cs42l56_ops,
  823. };
  824. static int beep_freq[] = {
  825. 261, 522, 585, 667, 706, 774, 889, 1000,
  826. 1043, 1200, 1333, 1412, 1600, 1714, 2000, 2182
  827. };
  828. static void cs42l56_beep_work(struct work_struct *work)
  829. {
  830. struct cs42l56_private *cs42l56 =
  831. container_of(work, struct cs42l56_private, beep_work);
  832. struct snd_soc_codec *codec = cs42l56->codec;
  833. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  834. int i;
  835. int val = 0;
  836. int best = 0;
  837. if (cs42l56->beep_rate) {
  838. for (i = 0; i < ARRAY_SIZE(beep_freq); i++) {
  839. if (abs(cs42l56->beep_rate - beep_freq[i]) <
  840. abs(cs42l56->beep_rate - beep_freq[best]))
  841. best = i;
  842. }
  843. dev_dbg(codec->dev, "Set beep rate %dHz for requested %dHz\n",
  844. beep_freq[best], cs42l56->beep_rate);
  845. val = (best << CS42L56_BEEP_RATE_SHIFT);
  846. snd_soc_dapm_enable_pin(dapm, "Beep");
  847. } else {
  848. dev_dbg(codec->dev, "Disabling beep\n");
  849. snd_soc_dapm_disable_pin(dapm, "Beep");
  850. }
  851. snd_soc_update_bits(codec, CS42L56_BEEP_FREQ_ONTIME,
  852. CS42L56_BEEP_FREQ_MASK, val);
  853. snd_soc_dapm_sync(dapm);
  854. }
  855. /* For usability define a way of injecting beep events for the device -
  856. * many systems will not have a keyboard.
  857. */
  858. static int cs42l56_beep_event(struct input_dev *dev, unsigned int type,
  859. unsigned int code, int hz)
  860. {
  861. struct snd_soc_codec *codec = input_get_drvdata(dev);
  862. struct cs42l56_private *cs42l56 = snd_soc_codec_get_drvdata(codec);
  863. dev_dbg(codec->dev, "Beep event %x %x\n", code, hz);
  864. switch (code) {
  865. case SND_BELL:
  866. if (hz)
  867. hz = 261;
  868. case SND_TONE:
  869. break;
  870. default:
  871. return -1;
  872. }
  873. /* Kick the beep from a workqueue */
  874. cs42l56->beep_rate = hz;
  875. schedule_work(&cs42l56->beep_work);
  876. return 0;
  877. }
  878. static ssize_t cs42l56_beep_set(struct device *dev,
  879. struct device_attribute *attr,
  880. const char *buf, size_t count)
  881. {
  882. struct cs42l56_private *cs42l56 = dev_get_drvdata(dev);
  883. long int time;
  884. int ret;
  885. ret = kstrtol(buf, 10, &time);
  886. if (ret != 0)
  887. return ret;
  888. input_event(cs42l56->beep, EV_SND, SND_TONE, time);
  889. return count;
  890. }
  891. static DEVICE_ATTR(beep, 0200, NULL, cs42l56_beep_set);
  892. static void cs42l56_init_beep(struct snd_soc_codec *codec)
  893. {
  894. struct cs42l56_private *cs42l56 = snd_soc_codec_get_drvdata(codec);
  895. int ret;
  896. cs42l56->beep = devm_input_allocate_device(codec->dev);
  897. if (!cs42l56->beep) {
  898. dev_err(codec->dev, "Failed to allocate beep device\n");
  899. return;
  900. }
  901. INIT_WORK(&cs42l56->beep_work, cs42l56_beep_work);
  902. cs42l56->beep_rate = 0;
  903. cs42l56->beep->name = "CS42L56 Beep Generator";
  904. cs42l56->beep->phys = dev_name(codec->dev);
  905. cs42l56->beep->id.bustype = BUS_I2C;
  906. cs42l56->beep->evbit[0] = BIT_MASK(EV_SND);
  907. cs42l56->beep->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  908. cs42l56->beep->event = cs42l56_beep_event;
  909. cs42l56->beep->dev.parent = codec->dev;
  910. input_set_drvdata(cs42l56->beep, codec);
  911. ret = input_register_device(cs42l56->beep);
  912. if (ret != 0) {
  913. cs42l56->beep = NULL;
  914. dev_err(codec->dev, "Failed to register beep device\n");
  915. }
  916. ret = device_create_file(codec->dev, &dev_attr_beep);
  917. if (ret != 0) {
  918. dev_err(codec->dev, "Failed to create keyclick file: %d\n",
  919. ret);
  920. }
  921. }
  922. static void cs42l56_free_beep(struct snd_soc_codec *codec)
  923. {
  924. struct cs42l56_private *cs42l56 = snd_soc_codec_get_drvdata(codec);
  925. device_remove_file(codec->dev, &dev_attr_beep);
  926. cancel_work_sync(&cs42l56->beep_work);
  927. cs42l56->beep = NULL;
  928. snd_soc_update_bits(codec, CS42L56_BEEP_TONE_CFG,
  929. CS42L56_BEEP_EN_MASK, 0);
  930. }
  931. static int cs42l56_probe(struct snd_soc_codec *codec)
  932. {
  933. cs42l56_init_beep(codec);
  934. return 0;
  935. }
  936. static int cs42l56_remove(struct snd_soc_codec *codec)
  937. {
  938. cs42l56_free_beep(codec);
  939. return 0;
  940. }
  941. static const struct snd_soc_codec_driver soc_codec_dev_cs42l56 = {
  942. .probe = cs42l56_probe,
  943. .remove = cs42l56_remove,
  944. .set_bias_level = cs42l56_set_bias_level,
  945. .suspend_bias_off = true,
  946. .dapm_widgets = cs42l56_dapm_widgets,
  947. .num_dapm_widgets = ARRAY_SIZE(cs42l56_dapm_widgets),
  948. .dapm_routes = cs42l56_audio_map,
  949. .num_dapm_routes = ARRAY_SIZE(cs42l56_audio_map),
  950. .controls = cs42l56_snd_controls,
  951. .num_controls = ARRAY_SIZE(cs42l56_snd_controls),
  952. };
  953. static const struct regmap_config cs42l56_regmap = {
  954. .reg_bits = 8,
  955. .val_bits = 8,
  956. .max_register = CS42L56_MAX_REGISTER,
  957. .reg_defaults = cs42l56_reg_defaults,
  958. .num_reg_defaults = ARRAY_SIZE(cs42l56_reg_defaults),
  959. .readable_reg = cs42l56_readable_register,
  960. .volatile_reg = cs42l56_volatile_register,
  961. .cache_type = REGCACHE_RBTREE,
  962. };
  963. static int cs42l56_handle_of_data(struct i2c_client *i2c_client,
  964. struct cs42l56_platform_data *pdata)
  965. {
  966. struct device_node *np = i2c_client->dev.of_node;
  967. u32 val32;
  968. if (of_property_read_bool(np, "cirrus,ain1a-reference-cfg"))
  969. pdata->ain1a_ref_cfg = true;
  970. if (of_property_read_bool(np, "cirrus,ain2a-reference-cfg"))
  971. pdata->ain2a_ref_cfg = true;
  972. if (of_property_read_bool(np, "cirrus,ain1b-reference-cfg"))
  973. pdata->ain1b_ref_cfg = true;
  974. if (of_property_read_bool(np, "cirrus,ain2b-reference-cfg"))
  975. pdata->ain2b_ref_cfg = true;
  976. if (of_property_read_u32(np, "cirrus,micbias-lvl", &val32) >= 0)
  977. pdata->micbias_lvl = val32;
  978. if (of_property_read_u32(np, "cirrus,chgfreq-divisor", &val32) >= 0)
  979. pdata->chgfreq = val32;
  980. if (of_property_read_u32(np, "cirrus,adaptive-pwr-cfg", &val32) >= 0)
  981. pdata->adaptive_pwr = val32;
  982. if (of_property_read_u32(np, "cirrus,hpf-left-freq", &val32) >= 0)
  983. pdata->hpfa_freq = val32;
  984. if (of_property_read_u32(np, "cirrus,hpf-left-freq", &val32) >= 0)
  985. pdata->hpfb_freq = val32;
  986. pdata->gpio_nreset = of_get_named_gpio(np, "cirrus,gpio-nreset", 0);
  987. return 0;
  988. }
  989. static int cs42l56_i2c_probe(struct i2c_client *i2c_client,
  990. const struct i2c_device_id *id)
  991. {
  992. struct cs42l56_private *cs42l56;
  993. struct cs42l56_platform_data *pdata =
  994. dev_get_platdata(&i2c_client->dev);
  995. int ret, i;
  996. unsigned int devid = 0;
  997. unsigned int alpha_rev, metal_rev;
  998. unsigned int reg;
  999. cs42l56 = devm_kzalloc(&i2c_client->dev,
  1000. sizeof(struct cs42l56_private),
  1001. GFP_KERNEL);
  1002. if (cs42l56 == NULL)
  1003. return -ENOMEM;
  1004. cs42l56->dev = &i2c_client->dev;
  1005. cs42l56->regmap = devm_regmap_init_i2c(i2c_client, &cs42l56_regmap);
  1006. if (IS_ERR(cs42l56->regmap)) {
  1007. ret = PTR_ERR(cs42l56->regmap);
  1008. dev_err(&i2c_client->dev, "regmap_init() failed: %d\n", ret);
  1009. return ret;
  1010. }
  1011. if (pdata) {
  1012. cs42l56->pdata = *pdata;
  1013. } else {
  1014. pdata = devm_kzalloc(&i2c_client->dev,
  1015. sizeof(struct cs42l56_platform_data),
  1016. GFP_KERNEL);
  1017. if (!pdata) {
  1018. dev_err(&i2c_client->dev,
  1019. "could not allocate pdata\n");
  1020. return -ENOMEM;
  1021. }
  1022. if (i2c_client->dev.of_node) {
  1023. ret = cs42l56_handle_of_data(i2c_client,
  1024. &cs42l56->pdata);
  1025. if (ret != 0)
  1026. return ret;
  1027. }
  1028. cs42l56->pdata = *pdata;
  1029. }
  1030. if (cs42l56->pdata.gpio_nreset) {
  1031. ret = gpio_request_one(cs42l56->pdata.gpio_nreset,
  1032. GPIOF_OUT_INIT_HIGH, "CS42L56 /RST");
  1033. if (ret < 0) {
  1034. dev_err(&i2c_client->dev,
  1035. "Failed to request /RST %d: %d\n",
  1036. cs42l56->pdata.gpio_nreset, ret);
  1037. return ret;
  1038. }
  1039. gpio_set_value_cansleep(cs42l56->pdata.gpio_nreset, 0);
  1040. gpio_set_value_cansleep(cs42l56->pdata.gpio_nreset, 1);
  1041. }
  1042. i2c_set_clientdata(i2c_client, cs42l56);
  1043. for (i = 0; i < ARRAY_SIZE(cs42l56->supplies); i++)
  1044. cs42l56->supplies[i].supply = cs42l56_supply_names[i];
  1045. ret = devm_regulator_bulk_get(&i2c_client->dev,
  1046. ARRAY_SIZE(cs42l56->supplies),
  1047. cs42l56->supplies);
  1048. if (ret != 0) {
  1049. dev_err(&i2c_client->dev,
  1050. "Failed to request supplies: %d\n", ret);
  1051. return ret;
  1052. }
  1053. ret = regulator_bulk_enable(ARRAY_SIZE(cs42l56->supplies),
  1054. cs42l56->supplies);
  1055. if (ret != 0) {
  1056. dev_err(&i2c_client->dev,
  1057. "Failed to enable supplies: %d\n", ret);
  1058. return ret;
  1059. }
  1060. regcache_cache_bypass(cs42l56->regmap, true);
  1061. ret = regmap_read(cs42l56->regmap, CS42L56_CHIP_ID_1, &reg);
  1062. devid = reg & CS42L56_CHIP_ID_MASK;
  1063. if (devid != CS42L56_DEVID) {
  1064. dev_err(&i2c_client->dev,
  1065. "CS42L56 Device ID (%X). Expected %X\n",
  1066. devid, CS42L56_DEVID);
  1067. goto err_enable;
  1068. }
  1069. alpha_rev = reg & CS42L56_AREV_MASK;
  1070. metal_rev = reg & CS42L56_MTLREV_MASK;
  1071. dev_info(&i2c_client->dev, "Cirrus Logic CS42L56 ");
  1072. dev_info(&i2c_client->dev, "Alpha Rev %X Metal Rev %X\n",
  1073. alpha_rev, metal_rev);
  1074. regcache_cache_bypass(cs42l56->regmap, false);
  1075. if (cs42l56->pdata.ain1a_ref_cfg)
  1076. regmap_update_bits(cs42l56->regmap, CS42L56_AIN_REFCFG_ADC_MUX,
  1077. CS42L56_AIN1A_REF_MASK, 1);
  1078. if (cs42l56->pdata.ain1b_ref_cfg)
  1079. regmap_update_bits(cs42l56->regmap, CS42L56_AIN_REFCFG_ADC_MUX,
  1080. CS42L56_AIN1B_REF_MASK, 1);
  1081. if (cs42l56->pdata.ain2a_ref_cfg)
  1082. regmap_update_bits(cs42l56->regmap, CS42L56_AIN_REFCFG_ADC_MUX,
  1083. CS42L56_AIN2A_REF_MASK, 1);
  1084. if (cs42l56->pdata.ain2b_ref_cfg)
  1085. regmap_update_bits(cs42l56->regmap, CS42L56_AIN_REFCFG_ADC_MUX,
  1086. CS42L56_AIN2B_REF_MASK, 1);
  1087. if (cs42l56->pdata.micbias_lvl)
  1088. regmap_update_bits(cs42l56->regmap, CS42L56_GAIN_BIAS_CTL,
  1089. CS42L56_MIC_BIAS_MASK,
  1090. cs42l56->pdata.micbias_lvl);
  1091. if (cs42l56->pdata.chgfreq)
  1092. regmap_update_bits(cs42l56->regmap, CS42L56_CLASSH_CTL,
  1093. CS42L56_CHRG_FREQ_MASK,
  1094. cs42l56->pdata.chgfreq);
  1095. if (cs42l56->pdata.hpfb_freq)
  1096. regmap_update_bits(cs42l56->regmap, CS42L56_HPF_CTL,
  1097. CS42L56_HPFB_FREQ_MASK,
  1098. cs42l56->pdata.hpfb_freq);
  1099. if (cs42l56->pdata.hpfa_freq)
  1100. regmap_update_bits(cs42l56->regmap, CS42L56_HPF_CTL,
  1101. CS42L56_HPFA_FREQ_MASK,
  1102. cs42l56->pdata.hpfa_freq);
  1103. if (cs42l56->pdata.adaptive_pwr)
  1104. regmap_update_bits(cs42l56->regmap, CS42L56_CLASSH_CTL,
  1105. CS42L56_ADAPT_PWR_MASK,
  1106. cs42l56->pdata.adaptive_pwr);
  1107. ret = snd_soc_register_codec(&i2c_client->dev,
  1108. &soc_codec_dev_cs42l56, &cs42l56_dai, 1);
  1109. if (ret < 0)
  1110. return ret;
  1111. return 0;
  1112. err_enable:
  1113. regulator_bulk_disable(ARRAY_SIZE(cs42l56->supplies),
  1114. cs42l56->supplies);
  1115. return ret;
  1116. }
  1117. static int cs42l56_i2c_remove(struct i2c_client *client)
  1118. {
  1119. struct cs42l56_private *cs42l56 = i2c_get_clientdata(client);
  1120. snd_soc_unregister_codec(&client->dev);
  1121. regulator_bulk_disable(ARRAY_SIZE(cs42l56->supplies),
  1122. cs42l56->supplies);
  1123. return 0;
  1124. }
  1125. static const struct of_device_id cs42l56_of_match[] = {
  1126. { .compatible = "cirrus,cs42l56", },
  1127. { }
  1128. };
  1129. MODULE_DEVICE_TABLE(of, cs42l56_of_match);
  1130. static const struct i2c_device_id cs42l56_id[] = {
  1131. { "cs42l56", 0 },
  1132. { }
  1133. };
  1134. MODULE_DEVICE_TABLE(i2c, cs42l56_id);
  1135. static struct i2c_driver cs42l56_i2c_driver = {
  1136. .driver = {
  1137. .name = "cs42l56",
  1138. .of_match_table = cs42l56_of_match,
  1139. },
  1140. .id_table = cs42l56_id,
  1141. .probe = cs42l56_i2c_probe,
  1142. .remove = cs42l56_i2c_remove,
  1143. };
  1144. module_i2c_driver(cs42l56_i2c_driver);
  1145. MODULE_DESCRIPTION("ASoC CS42L56 driver");
  1146. MODULE_AUTHOR("Brian Austin, Cirrus Logic Inc, <brian.austin@cirrus.com>");
  1147. MODULE_LICENSE("GPL");