sta350.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /*
  2. * Codec driver for ST STA350 2.1-channel high-efficiency digital audio system
  3. *
  4. * Copyright: 2014 Raumfeld GmbH
  5. * Author: Sven Brandau <info@brandau.biz>
  6. *
  7. * based on code from:
  8. * Raumfeld GmbH
  9. * Johannes Stezenbach <js@sig21.net>
  10. * Wolfson Microelectronics PLC.
  11. * Mark Brown <broonie@opensource.wolfsonmicro.com>
  12. * Freescale Semiconductor, Inc.
  13. * Timur Tabi <timur@freescale.com>
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ":%s:%d: " fmt, __func__, __LINE__
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/pm.h>
  26. #include <linux/i2c.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_gpio.h>
  29. #include <linux/regmap.h>
  30. #include <linux/regulator/consumer.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/slab.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/pcm_params.h>
  36. #include <sound/soc.h>
  37. #include <sound/soc-dapm.h>
  38. #include <sound/initval.h>
  39. #include <sound/tlv.h>
  40. #include <sound/sta350.h>
  41. #include "sta350.h"
  42. #define STA350_RATES (SNDRV_PCM_RATE_32000 | \
  43. SNDRV_PCM_RATE_44100 | \
  44. SNDRV_PCM_RATE_48000 | \
  45. SNDRV_PCM_RATE_88200 | \
  46. SNDRV_PCM_RATE_96000 | \
  47. SNDRV_PCM_RATE_176400 | \
  48. SNDRV_PCM_RATE_192000)
  49. #define STA350_FORMATS \
  50. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  51. SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE | \
  52. SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE | \
  53. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE | \
  54. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE | \
  55. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE)
  56. /* Power-up register defaults */
  57. static const struct reg_default sta350_regs[] = {
  58. { 0x0, 0x63 },
  59. { 0x1, 0x80 },
  60. { 0x2, 0xdf },
  61. { 0x3, 0x40 },
  62. { 0x4, 0xc2 },
  63. { 0x5, 0x5c },
  64. { 0x6, 0x00 },
  65. { 0x7, 0xff },
  66. { 0x8, 0x60 },
  67. { 0x9, 0x60 },
  68. { 0xa, 0x60 },
  69. { 0xb, 0x00 },
  70. { 0xc, 0x00 },
  71. { 0xd, 0x00 },
  72. { 0xe, 0x00 },
  73. { 0xf, 0x40 },
  74. { 0x10, 0x80 },
  75. { 0x11, 0x77 },
  76. { 0x12, 0x6a },
  77. { 0x13, 0x69 },
  78. { 0x14, 0x6a },
  79. { 0x15, 0x69 },
  80. { 0x16, 0x00 },
  81. { 0x17, 0x00 },
  82. { 0x18, 0x00 },
  83. { 0x19, 0x00 },
  84. { 0x1a, 0x00 },
  85. { 0x1b, 0x00 },
  86. { 0x1c, 0x00 },
  87. { 0x1d, 0x00 },
  88. { 0x1e, 0x00 },
  89. { 0x1f, 0x00 },
  90. { 0x20, 0x00 },
  91. { 0x21, 0x00 },
  92. { 0x22, 0x00 },
  93. { 0x23, 0x00 },
  94. { 0x24, 0x00 },
  95. { 0x25, 0x00 },
  96. { 0x26, 0x00 },
  97. { 0x27, 0x2a },
  98. { 0x28, 0xc0 },
  99. { 0x29, 0xf3 },
  100. { 0x2a, 0x33 },
  101. { 0x2b, 0x00 },
  102. { 0x2c, 0x0c },
  103. { 0x31, 0x00 },
  104. { 0x36, 0x00 },
  105. { 0x37, 0x00 },
  106. { 0x38, 0x00 },
  107. { 0x39, 0x01 },
  108. { 0x3a, 0xee },
  109. { 0x3b, 0xff },
  110. { 0x3c, 0x7e },
  111. { 0x3d, 0xc0 },
  112. { 0x3e, 0x26 },
  113. { 0x3f, 0x00 },
  114. { 0x48, 0x00 },
  115. { 0x49, 0x00 },
  116. { 0x4a, 0x00 },
  117. { 0x4b, 0x04 },
  118. { 0x4c, 0x00 },
  119. };
  120. static const struct regmap_range sta350_write_regs_range[] = {
  121. regmap_reg_range(STA350_CONFA, STA350_AUTO2),
  122. regmap_reg_range(STA350_C1CFG, STA350_FDRC2),
  123. regmap_reg_range(STA350_EQCFG, STA350_EVOLRES),
  124. regmap_reg_range(STA350_NSHAPE, STA350_MISC2),
  125. };
  126. static const struct regmap_range sta350_read_regs_range[] = {
  127. regmap_reg_range(STA350_CONFA, STA350_AUTO2),
  128. regmap_reg_range(STA350_C1CFG, STA350_STATUS),
  129. regmap_reg_range(STA350_EQCFG, STA350_EVOLRES),
  130. regmap_reg_range(STA350_NSHAPE, STA350_MISC2),
  131. };
  132. static const struct regmap_range sta350_volatile_regs_range[] = {
  133. regmap_reg_range(STA350_CFADDR2, STA350_CFUD),
  134. regmap_reg_range(STA350_STATUS, STA350_STATUS),
  135. };
  136. static const struct regmap_access_table sta350_write_regs = {
  137. .yes_ranges = sta350_write_regs_range,
  138. .n_yes_ranges = ARRAY_SIZE(sta350_write_regs_range),
  139. };
  140. static const struct regmap_access_table sta350_read_regs = {
  141. .yes_ranges = sta350_read_regs_range,
  142. .n_yes_ranges = ARRAY_SIZE(sta350_read_regs_range),
  143. };
  144. static const struct regmap_access_table sta350_volatile_regs = {
  145. .yes_ranges = sta350_volatile_regs_range,
  146. .n_yes_ranges = ARRAY_SIZE(sta350_volatile_regs_range),
  147. };
  148. /* regulator power supply names */
  149. static const char * const sta350_supply_names[] = {
  150. "vdd-dig", /* digital supply, 3.3V */
  151. "vdd-pll", /* pll supply, 3.3V */
  152. "vcc" /* power amp supply, 5V - 26V */
  153. };
  154. /* codec private data */
  155. struct sta350_priv {
  156. struct regmap *regmap;
  157. struct regulator_bulk_data supplies[ARRAY_SIZE(sta350_supply_names)];
  158. struct sta350_platform_data *pdata;
  159. unsigned int mclk;
  160. unsigned int format;
  161. u32 coef_shadow[STA350_COEF_COUNT];
  162. int shutdown;
  163. struct gpio_desc *gpiod_nreset;
  164. struct gpio_desc *gpiod_power_down;
  165. struct mutex coeff_lock;
  166. };
  167. static const DECLARE_TLV_DB_SCALE(mvol_tlv, -12750, 50, 1);
  168. static const DECLARE_TLV_DB_SCALE(chvol_tlv, -7950, 50, 1);
  169. static const DECLARE_TLV_DB_SCALE(tone_tlv, -1200, 200, 0);
  170. static const char * const sta350_drc_ac[] = {
  171. "Anti-Clipping", "Dynamic Range Compression"
  172. };
  173. static const char * const sta350_auto_gc_mode[] = {
  174. "User", "AC no clipping", "AC limited clipping (10%)",
  175. "DRC nighttime listening mode"
  176. };
  177. static const char * const sta350_auto_xo_mode[] = {
  178. "User", "80Hz", "100Hz", "120Hz", "140Hz", "160Hz", "180Hz",
  179. "200Hz", "220Hz", "240Hz", "260Hz", "280Hz", "300Hz", "320Hz",
  180. "340Hz", "360Hz"
  181. };
  182. static const char * const sta350_binary_output[] = {
  183. "FFX 3-state output - normal operation", "Binary output"
  184. };
  185. static const char * const sta350_limiter_select[] = {
  186. "Limiter Disabled", "Limiter #1", "Limiter #2"
  187. };
  188. static const char * const sta350_limiter_attack_rate[] = {
  189. "3.1584", "2.7072", "2.2560", "1.8048", "1.3536", "0.9024",
  190. "0.4512", "0.2256", "0.1504", "0.1123", "0.0902", "0.0752",
  191. "0.0645", "0.0564", "0.0501", "0.0451"
  192. };
  193. static const char * const sta350_limiter_release_rate[] = {
  194. "0.5116", "0.1370", "0.0744", "0.0499", "0.0360", "0.0299",
  195. "0.0264", "0.0208", "0.0198", "0.0172", "0.0147", "0.0137",
  196. "0.0134", "0.0117", "0.0110", "0.0104"
  197. };
  198. static const char * const sta350_noise_shaper_type[] = {
  199. "Third order", "Fourth order"
  200. };
  201. static DECLARE_TLV_DB_RANGE(sta350_limiter_ac_attack_tlv,
  202. 0, 7, TLV_DB_SCALE_ITEM(-1200, 200, 0),
  203. 8, 16, TLV_DB_SCALE_ITEM(300, 100, 0),
  204. );
  205. static DECLARE_TLV_DB_RANGE(sta350_limiter_ac_release_tlv,
  206. 0, 0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
  207. 1, 1, TLV_DB_SCALE_ITEM(-2900, 0, 0),
  208. 2, 2, TLV_DB_SCALE_ITEM(-2000, 0, 0),
  209. 3, 8, TLV_DB_SCALE_ITEM(-1400, 200, 0),
  210. 8, 16, TLV_DB_SCALE_ITEM(-700, 100, 0),
  211. );
  212. static DECLARE_TLV_DB_RANGE(sta350_limiter_drc_attack_tlv,
  213. 0, 7, TLV_DB_SCALE_ITEM(-3100, 200, 0),
  214. 8, 13, TLV_DB_SCALE_ITEM(-1600, 100, 0),
  215. 14, 16, TLV_DB_SCALE_ITEM(-1000, 300, 0),
  216. );
  217. static DECLARE_TLV_DB_RANGE(sta350_limiter_drc_release_tlv,
  218. 0, 0, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
  219. 1, 2, TLV_DB_SCALE_ITEM(-3800, 200, 0),
  220. 3, 4, TLV_DB_SCALE_ITEM(-3300, 200, 0),
  221. 5, 12, TLV_DB_SCALE_ITEM(-3000, 200, 0),
  222. 13, 16, TLV_DB_SCALE_ITEM(-1500, 300, 0),
  223. );
  224. static SOC_ENUM_SINGLE_DECL(sta350_drc_ac_enum,
  225. STA350_CONFD, STA350_CONFD_DRC_SHIFT,
  226. sta350_drc_ac);
  227. static SOC_ENUM_SINGLE_DECL(sta350_noise_shaper_enum,
  228. STA350_CONFE, STA350_CONFE_NSBW_SHIFT,
  229. sta350_noise_shaper_type);
  230. static SOC_ENUM_SINGLE_DECL(sta350_auto_gc_enum,
  231. STA350_AUTO1, STA350_AUTO1_AMGC_SHIFT,
  232. sta350_auto_gc_mode);
  233. static SOC_ENUM_SINGLE_DECL(sta350_auto_xo_enum,
  234. STA350_AUTO2, STA350_AUTO2_XO_SHIFT,
  235. sta350_auto_xo_mode);
  236. static SOC_ENUM_SINGLE_DECL(sta350_binary_output_ch1_enum,
  237. STA350_C1CFG, STA350_CxCFG_BO_SHIFT,
  238. sta350_binary_output);
  239. static SOC_ENUM_SINGLE_DECL(sta350_binary_output_ch2_enum,
  240. STA350_C2CFG, STA350_CxCFG_BO_SHIFT,
  241. sta350_binary_output);
  242. static SOC_ENUM_SINGLE_DECL(sta350_binary_output_ch3_enum,
  243. STA350_C3CFG, STA350_CxCFG_BO_SHIFT,
  244. sta350_binary_output);
  245. static SOC_ENUM_SINGLE_DECL(sta350_limiter_ch1_enum,
  246. STA350_C1CFG, STA350_CxCFG_LS_SHIFT,
  247. sta350_limiter_select);
  248. static SOC_ENUM_SINGLE_DECL(sta350_limiter_ch2_enum,
  249. STA350_C2CFG, STA350_CxCFG_LS_SHIFT,
  250. sta350_limiter_select);
  251. static SOC_ENUM_SINGLE_DECL(sta350_limiter_ch3_enum,
  252. STA350_C3CFG, STA350_CxCFG_LS_SHIFT,
  253. sta350_limiter_select);
  254. static SOC_ENUM_SINGLE_DECL(sta350_limiter1_attack_rate_enum,
  255. STA350_L1AR, STA350_LxA_SHIFT,
  256. sta350_limiter_attack_rate);
  257. static SOC_ENUM_SINGLE_DECL(sta350_limiter2_attack_rate_enum,
  258. STA350_L2AR, STA350_LxA_SHIFT,
  259. sta350_limiter_attack_rate);
  260. static SOC_ENUM_SINGLE_DECL(sta350_limiter1_release_rate_enum,
  261. STA350_L1AR, STA350_LxR_SHIFT,
  262. sta350_limiter_release_rate);
  263. static SOC_ENUM_SINGLE_DECL(sta350_limiter2_release_rate_enum,
  264. STA350_L2AR, STA350_LxR_SHIFT,
  265. sta350_limiter_release_rate);
  266. /*
  267. * byte array controls for setting biquad, mixer, scaling coefficients;
  268. * for biquads all five coefficients need to be set in one go,
  269. * mixer and pre/postscale coefs can be set individually;
  270. * each coef is 24bit, the bytes are ordered in the same way
  271. * as given in the STA350 data sheet (big endian; b1, b2, a1, a2, b0)
  272. */
  273. static int sta350_coefficient_info(struct snd_kcontrol *kcontrol,
  274. struct snd_ctl_elem_info *uinfo)
  275. {
  276. int numcoef = kcontrol->private_value >> 16;
  277. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  278. uinfo->count = 3 * numcoef;
  279. return 0;
  280. }
  281. static int sta350_coefficient_get(struct snd_kcontrol *kcontrol,
  282. struct snd_ctl_elem_value *ucontrol)
  283. {
  284. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  285. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  286. int numcoef = kcontrol->private_value >> 16;
  287. int index = kcontrol->private_value & 0xffff;
  288. unsigned int cfud, val;
  289. int i, ret = 0;
  290. mutex_lock(&sta350->coeff_lock);
  291. /* preserve reserved bits in STA350_CFUD */
  292. regmap_read(sta350->regmap, STA350_CFUD, &cfud);
  293. cfud &= 0xf0;
  294. /*
  295. * chip documentation does not say if the bits are self clearing,
  296. * so do it explicitly
  297. */
  298. regmap_write(sta350->regmap, STA350_CFUD, cfud);
  299. regmap_write(sta350->regmap, STA350_CFADDR2, index);
  300. if (numcoef == 1) {
  301. regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x04);
  302. } else if (numcoef == 5) {
  303. regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x08);
  304. } else {
  305. ret = -EINVAL;
  306. goto exit_unlock;
  307. }
  308. for (i = 0; i < 3 * numcoef; i++) {
  309. regmap_read(sta350->regmap, STA350_B1CF1 + i, &val);
  310. ucontrol->value.bytes.data[i] = val;
  311. }
  312. exit_unlock:
  313. mutex_unlock(&sta350->coeff_lock);
  314. return ret;
  315. }
  316. static int sta350_coefficient_put(struct snd_kcontrol *kcontrol,
  317. struct snd_ctl_elem_value *ucontrol)
  318. {
  319. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  320. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  321. int numcoef = kcontrol->private_value >> 16;
  322. int index = kcontrol->private_value & 0xffff;
  323. unsigned int cfud;
  324. int i;
  325. /* preserve reserved bits in STA350_CFUD */
  326. regmap_read(sta350->regmap, STA350_CFUD, &cfud);
  327. cfud &= 0xf0;
  328. /*
  329. * chip documentation does not say if the bits are self clearing,
  330. * so do it explicitly
  331. */
  332. regmap_write(sta350->regmap, STA350_CFUD, cfud);
  333. regmap_write(sta350->regmap, STA350_CFADDR2, index);
  334. for (i = 0; i < numcoef && (index + i < STA350_COEF_COUNT); i++)
  335. sta350->coef_shadow[index + i] =
  336. (ucontrol->value.bytes.data[3 * i] << 16)
  337. | (ucontrol->value.bytes.data[3 * i + 1] << 8)
  338. | (ucontrol->value.bytes.data[3 * i + 2]);
  339. for (i = 0; i < 3 * numcoef; i++)
  340. regmap_write(sta350->regmap, STA350_B1CF1 + i,
  341. ucontrol->value.bytes.data[i]);
  342. if (numcoef == 1)
  343. regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x01);
  344. else if (numcoef == 5)
  345. regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x02);
  346. else
  347. return -EINVAL;
  348. return 0;
  349. }
  350. static int sta350_sync_coef_shadow(struct snd_soc_codec *codec)
  351. {
  352. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  353. unsigned int cfud;
  354. int i;
  355. /* preserve reserved bits in STA350_CFUD */
  356. regmap_read(sta350->regmap, STA350_CFUD, &cfud);
  357. cfud &= 0xf0;
  358. for (i = 0; i < STA350_COEF_COUNT; i++) {
  359. regmap_write(sta350->regmap, STA350_CFADDR2, i);
  360. regmap_write(sta350->regmap, STA350_B1CF1,
  361. (sta350->coef_shadow[i] >> 16) & 0xff);
  362. regmap_write(sta350->regmap, STA350_B1CF2,
  363. (sta350->coef_shadow[i] >> 8) & 0xff);
  364. regmap_write(sta350->regmap, STA350_B1CF3,
  365. (sta350->coef_shadow[i]) & 0xff);
  366. /*
  367. * chip documentation does not say if the bits are
  368. * self-clearing, so do it explicitly
  369. */
  370. regmap_write(sta350->regmap, STA350_CFUD, cfud);
  371. regmap_write(sta350->regmap, STA350_CFUD, cfud | 0x01);
  372. }
  373. return 0;
  374. }
  375. static int sta350_cache_sync(struct snd_soc_codec *codec)
  376. {
  377. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  378. unsigned int mute;
  379. int rc;
  380. /* mute during register sync */
  381. regmap_read(sta350->regmap, STA350_CFUD, &mute);
  382. regmap_write(sta350->regmap, STA350_MMUTE, mute | STA350_MMUTE_MMUTE);
  383. sta350_sync_coef_shadow(codec);
  384. rc = regcache_sync(sta350->regmap);
  385. regmap_write(sta350->regmap, STA350_MMUTE, mute);
  386. return rc;
  387. }
  388. #define SINGLE_COEF(xname, index) \
  389. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  390. .info = sta350_coefficient_info, \
  391. .get = sta350_coefficient_get,\
  392. .put = sta350_coefficient_put, \
  393. .private_value = index | (1 << 16) }
  394. #define BIQUAD_COEFS(xname, index) \
  395. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  396. .info = sta350_coefficient_info, \
  397. .get = sta350_coefficient_get,\
  398. .put = sta350_coefficient_put, \
  399. .private_value = index | (5 << 16) }
  400. static const struct snd_kcontrol_new sta350_snd_controls[] = {
  401. SOC_SINGLE_TLV("Master Volume", STA350_MVOL, 0, 0xff, 1, mvol_tlv),
  402. /* VOL */
  403. SOC_SINGLE_TLV("Ch1 Volume", STA350_C1VOL, 0, 0xff, 1, chvol_tlv),
  404. SOC_SINGLE_TLV("Ch2 Volume", STA350_C2VOL, 0, 0xff, 1, chvol_tlv),
  405. SOC_SINGLE_TLV("Ch3 Volume", STA350_C3VOL, 0, 0xff, 1, chvol_tlv),
  406. /* CONFD */
  407. SOC_SINGLE("High Pass Filter Bypass Switch",
  408. STA350_CONFD, STA350_CONFD_HPB_SHIFT, 1, 1),
  409. SOC_SINGLE("De-emphasis Filter Switch",
  410. STA350_CONFD, STA350_CONFD_DEMP_SHIFT, 1, 0),
  411. SOC_SINGLE("DSP Bypass Switch",
  412. STA350_CONFD, STA350_CONFD_DSPB_SHIFT, 1, 0),
  413. SOC_SINGLE("Post-scale Link Switch",
  414. STA350_CONFD, STA350_CONFD_PSL_SHIFT, 1, 0),
  415. SOC_SINGLE("Biquad Coefficient Link Switch",
  416. STA350_CONFD, STA350_CONFD_BQL_SHIFT, 1, 0),
  417. SOC_ENUM("Compressor/Limiter Switch", sta350_drc_ac_enum),
  418. SOC_ENUM("Noise Shaper Bandwidth", sta350_noise_shaper_enum),
  419. SOC_SINGLE("Zero-detect Mute Enable Switch",
  420. STA350_CONFD, STA350_CONFD_ZDE_SHIFT, 1, 0),
  421. SOC_SINGLE("Submix Mode Switch",
  422. STA350_CONFD, STA350_CONFD_SME_SHIFT, 1, 0),
  423. /* CONFE */
  424. SOC_SINGLE("Zero Cross Switch", STA350_CONFE, STA350_CONFE_ZCE_SHIFT, 1, 0),
  425. SOC_SINGLE("Soft Ramp Switch", STA350_CONFE, STA350_CONFE_SVE_SHIFT, 1, 0),
  426. /* MUTE */
  427. SOC_SINGLE("Master Switch", STA350_MMUTE, STA350_MMUTE_MMUTE_SHIFT, 1, 1),
  428. SOC_SINGLE("Ch1 Switch", STA350_MMUTE, STA350_MMUTE_C1M_SHIFT, 1, 1),
  429. SOC_SINGLE("Ch2 Switch", STA350_MMUTE, STA350_MMUTE_C2M_SHIFT, 1, 1),
  430. SOC_SINGLE("Ch3 Switch", STA350_MMUTE, STA350_MMUTE_C3M_SHIFT, 1, 1),
  431. /* AUTOx */
  432. SOC_ENUM("Automode GC", sta350_auto_gc_enum),
  433. SOC_ENUM("Automode XO", sta350_auto_xo_enum),
  434. /* CxCFG */
  435. SOC_SINGLE("Ch1 Tone Control Bypass Switch",
  436. STA350_C1CFG, STA350_CxCFG_TCB_SHIFT, 1, 0),
  437. SOC_SINGLE("Ch2 Tone Control Bypass Switch",
  438. STA350_C2CFG, STA350_CxCFG_TCB_SHIFT, 1, 0),
  439. SOC_SINGLE("Ch1 EQ Bypass Switch",
  440. STA350_C1CFG, STA350_CxCFG_EQBP_SHIFT, 1, 0),
  441. SOC_SINGLE("Ch2 EQ Bypass Switch",
  442. STA350_C2CFG, STA350_CxCFG_EQBP_SHIFT, 1, 0),
  443. SOC_SINGLE("Ch1 Master Volume Bypass Switch",
  444. STA350_C1CFG, STA350_CxCFG_VBP_SHIFT, 1, 0),
  445. SOC_SINGLE("Ch2 Master Volume Bypass Switch",
  446. STA350_C1CFG, STA350_CxCFG_VBP_SHIFT, 1, 0),
  447. SOC_SINGLE("Ch3 Master Volume Bypass Switch",
  448. STA350_C1CFG, STA350_CxCFG_VBP_SHIFT, 1, 0),
  449. SOC_ENUM("Ch1 Binary Output Select", sta350_binary_output_ch1_enum),
  450. SOC_ENUM("Ch2 Binary Output Select", sta350_binary_output_ch2_enum),
  451. SOC_ENUM("Ch3 Binary Output Select", sta350_binary_output_ch3_enum),
  452. SOC_ENUM("Ch1 Limiter Select", sta350_limiter_ch1_enum),
  453. SOC_ENUM("Ch2 Limiter Select", sta350_limiter_ch2_enum),
  454. SOC_ENUM("Ch3 Limiter Select", sta350_limiter_ch3_enum),
  455. /* TONE */
  456. SOC_SINGLE_RANGE_TLV("Bass Tone Control Volume",
  457. STA350_TONE, STA350_TONE_BTC_SHIFT, 1, 13, 0, tone_tlv),
  458. SOC_SINGLE_RANGE_TLV("Treble Tone Control Volume",
  459. STA350_TONE, STA350_TONE_TTC_SHIFT, 1, 13, 0, tone_tlv),
  460. SOC_ENUM("Limiter1 Attack Rate (dB/ms)", sta350_limiter1_attack_rate_enum),
  461. SOC_ENUM("Limiter2 Attack Rate (dB/ms)", sta350_limiter2_attack_rate_enum),
  462. SOC_ENUM("Limiter1 Release Rate (dB/ms)", sta350_limiter1_release_rate_enum),
  463. SOC_ENUM("Limiter2 Release Rate (dB/ms)", sta350_limiter2_release_rate_enum),
  464. /*
  465. * depending on mode, the attack/release thresholds have
  466. * two different enum definitions; provide both
  467. */
  468. SOC_SINGLE_TLV("Limiter1 Attack Threshold (AC Mode)",
  469. STA350_L1ATRT, STA350_LxA_SHIFT,
  470. 16, 0, sta350_limiter_ac_attack_tlv),
  471. SOC_SINGLE_TLV("Limiter2 Attack Threshold (AC Mode)",
  472. STA350_L2ATRT, STA350_LxA_SHIFT,
  473. 16, 0, sta350_limiter_ac_attack_tlv),
  474. SOC_SINGLE_TLV("Limiter1 Release Threshold (AC Mode)",
  475. STA350_L1ATRT, STA350_LxR_SHIFT,
  476. 16, 0, sta350_limiter_ac_release_tlv),
  477. SOC_SINGLE_TLV("Limiter2 Release Threshold (AC Mode)",
  478. STA350_L2ATRT, STA350_LxR_SHIFT,
  479. 16, 0, sta350_limiter_ac_release_tlv),
  480. SOC_SINGLE_TLV("Limiter1 Attack Threshold (DRC Mode)",
  481. STA350_L1ATRT, STA350_LxA_SHIFT,
  482. 16, 0, sta350_limiter_drc_attack_tlv),
  483. SOC_SINGLE_TLV("Limiter2 Attack Threshold (DRC Mode)",
  484. STA350_L2ATRT, STA350_LxA_SHIFT,
  485. 16, 0, sta350_limiter_drc_attack_tlv),
  486. SOC_SINGLE_TLV("Limiter1 Release Threshold (DRC Mode)",
  487. STA350_L1ATRT, STA350_LxR_SHIFT,
  488. 16, 0, sta350_limiter_drc_release_tlv),
  489. SOC_SINGLE_TLV("Limiter2 Release Threshold (DRC Mode)",
  490. STA350_L2ATRT, STA350_LxR_SHIFT,
  491. 16, 0, sta350_limiter_drc_release_tlv),
  492. BIQUAD_COEFS("Ch1 - Biquad 1", 0),
  493. BIQUAD_COEFS("Ch1 - Biquad 2", 5),
  494. BIQUAD_COEFS("Ch1 - Biquad 3", 10),
  495. BIQUAD_COEFS("Ch1 - Biquad 4", 15),
  496. BIQUAD_COEFS("Ch2 - Biquad 1", 20),
  497. BIQUAD_COEFS("Ch2 - Biquad 2", 25),
  498. BIQUAD_COEFS("Ch2 - Biquad 3", 30),
  499. BIQUAD_COEFS("Ch2 - Biquad 4", 35),
  500. BIQUAD_COEFS("High-pass", 40),
  501. BIQUAD_COEFS("Low-pass", 45),
  502. SINGLE_COEF("Ch1 - Prescale", 50),
  503. SINGLE_COEF("Ch2 - Prescale", 51),
  504. SINGLE_COEF("Ch1 - Postscale", 52),
  505. SINGLE_COEF("Ch2 - Postscale", 53),
  506. SINGLE_COEF("Ch3 - Postscale", 54),
  507. SINGLE_COEF("Thermal warning - Postscale", 55),
  508. SINGLE_COEF("Ch1 - Mix 1", 56),
  509. SINGLE_COEF("Ch1 - Mix 2", 57),
  510. SINGLE_COEF("Ch2 - Mix 1", 58),
  511. SINGLE_COEF("Ch2 - Mix 2", 59),
  512. SINGLE_COEF("Ch3 - Mix 1", 60),
  513. SINGLE_COEF("Ch3 - Mix 2", 61),
  514. };
  515. static const struct snd_soc_dapm_widget sta350_dapm_widgets[] = {
  516. SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
  517. SND_SOC_DAPM_OUTPUT("LEFT"),
  518. SND_SOC_DAPM_OUTPUT("RIGHT"),
  519. SND_SOC_DAPM_OUTPUT("SUB"),
  520. };
  521. static const struct snd_soc_dapm_route sta350_dapm_routes[] = {
  522. { "LEFT", NULL, "DAC" },
  523. { "RIGHT", NULL, "DAC" },
  524. { "SUB", NULL, "DAC" },
  525. { "DAC", NULL, "Playback" },
  526. };
  527. /* MCLK interpolation ratio per fs */
  528. static struct {
  529. int fs;
  530. int ir;
  531. } interpolation_ratios[] = {
  532. { 32000, 0 },
  533. { 44100, 0 },
  534. { 48000, 0 },
  535. { 88200, 1 },
  536. { 96000, 1 },
  537. { 176400, 2 },
  538. { 192000, 2 },
  539. };
  540. /* MCLK to fs clock ratios */
  541. static int mcs_ratio_table[3][6] = {
  542. { 768, 512, 384, 256, 128, 576 },
  543. { 384, 256, 192, 128, 64, 0 },
  544. { 192, 128, 96, 64, 32, 0 },
  545. };
  546. /**
  547. * sta350_set_dai_sysclk - configure MCLK
  548. * @codec_dai: the codec DAI
  549. * @clk_id: the clock ID (ignored)
  550. * @freq: the MCLK input frequency
  551. * @dir: the clock direction (ignored)
  552. *
  553. * The value of MCLK is used to determine which sample rates are supported
  554. * by the STA350, based on the mcs_ratio_table.
  555. *
  556. * This function must be called by the machine driver's 'startup' function,
  557. * otherwise the list of supported sample rates will not be available in
  558. * time for ALSA.
  559. */
  560. static int sta350_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  561. int clk_id, unsigned int freq, int dir)
  562. {
  563. struct snd_soc_codec *codec = codec_dai->codec;
  564. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  565. dev_dbg(codec->dev, "mclk=%u\n", freq);
  566. sta350->mclk = freq;
  567. return 0;
  568. }
  569. /**
  570. * sta350_set_dai_fmt - configure the codec for the selected audio format
  571. * @codec_dai: the codec DAI
  572. * @fmt: a SND_SOC_DAIFMT_x value indicating the data format
  573. *
  574. * This function takes a bitmask of SND_SOC_DAIFMT_x bits and programs the
  575. * codec accordingly.
  576. */
  577. static int sta350_set_dai_fmt(struct snd_soc_dai *codec_dai,
  578. unsigned int fmt)
  579. {
  580. struct snd_soc_codec *codec = codec_dai->codec;
  581. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  582. unsigned int confb = 0;
  583. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  584. case SND_SOC_DAIFMT_CBS_CFS:
  585. break;
  586. default:
  587. return -EINVAL;
  588. }
  589. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  590. case SND_SOC_DAIFMT_I2S:
  591. case SND_SOC_DAIFMT_RIGHT_J:
  592. case SND_SOC_DAIFMT_LEFT_J:
  593. sta350->format = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
  594. break;
  595. default:
  596. return -EINVAL;
  597. }
  598. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  599. case SND_SOC_DAIFMT_NB_NF:
  600. confb |= STA350_CONFB_C2IM;
  601. break;
  602. case SND_SOC_DAIFMT_NB_IF:
  603. confb |= STA350_CONFB_C1IM;
  604. break;
  605. default:
  606. return -EINVAL;
  607. }
  608. return regmap_update_bits(sta350->regmap, STA350_CONFB,
  609. STA350_CONFB_C1IM | STA350_CONFB_C2IM, confb);
  610. }
  611. /**
  612. * sta350_hw_params - program the STA350 with the given hardware parameters.
  613. * @substream: the audio stream
  614. * @params: the hardware parameters to set
  615. * @dai: the SOC DAI (ignored)
  616. *
  617. * This function programs the hardware with the values provided.
  618. * Specifically, the sample rate and the data format.
  619. */
  620. static int sta350_hw_params(struct snd_pcm_substream *substream,
  621. struct snd_pcm_hw_params *params,
  622. struct snd_soc_dai *dai)
  623. {
  624. struct snd_soc_codec *codec = dai->codec;
  625. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  626. int i, mcs = -EINVAL, ir = -EINVAL;
  627. unsigned int confa, confb;
  628. unsigned int rate, ratio;
  629. int ret;
  630. if (!sta350->mclk) {
  631. dev_err(codec->dev,
  632. "sta350->mclk is unset. Unable to determine ratio\n");
  633. return -EIO;
  634. }
  635. rate = params_rate(params);
  636. ratio = sta350->mclk / rate;
  637. dev_dbg(codec->dev, "rate: %u, ratio: %u\n", rate, ratio);
  638. for (i = 0; i < ARRAY_SIZE(interpolation_ratios); i++) {
  639. if (interpolation_ratios[i].fs == rate) {
  640. ir = interpolation_ratios[i].ir;
  641. break;
  642. }
  643. }
  644. if (ir < 0) {
  645. dev_err(codec->dev, "Unsupported samplerate: %u\n", rate);
  646. return -EINVAL;
  647. }
  648. for (i = 0; i < 6; i++) {
  649. if (mcs_ratio_table[ir][i] == ratio) {
  650. mcs = i;
  651. break;
  652. }
  653. }
  654. if (mcs < 0) {
  655. dev_err(codec->dev, "Unresolvable ratio: %u\n", ratio);
  656. return -EINVAL;
  657. }
  658. confa = (ir << STA350_CONFA_IR_SHIFT) |
  659. (mcs << STA350_CONFA_MCS_SHIFT);
  660. confb = 0;
  661. switch (params_width(params)) {
  662. case 24:
  663. dev_dbg(codec->dev, "24bit\n");
  664. /* fall through */
  665. case 32:
  666. dev_dbg(codec->dev, "24bit or 32bit\n");
  667. switch (sta350->format) {
  668. case SND_SOC_DAIFMT_I2S:
  669. confb |= 0x0;
  670. break;
  671. case SND_SOC_DAIFMT_LEFT_J:
  672. confb |= 0x1;
  673. break;
  674. case SND_SOC_DAIFMT_RIGHT_J:
  675. confb |= 0x2;
  676. break;
  677. }
  678. break;
  679. case 20:
  680. dev_dbg(codec->dev, "20bit\n");
  681. switch (sta350->format) {
  682. case SND_SOC_DAIFMT_I2S:
  683. confb |= 0x4;
  684. break;
  685. case SND_SOC_DAIFMT_LEFT_J:
  686. confb |= 0x5;
  687. break;
  688. case SND_SOC_DAIFMT_RIGHT_J:
  689. confb |= 0x6;
  690. break;
  691. }
  692. break;
  693. case 18:
  694. dev_dbg(codec->dev, "18bit\n");
  695. switch (sta350->format) {
  696. case SND_SOC_DAIFMT_I2S:
  697. confb |= 0x8;
  698. break;
  699. case SND_SOC_DAIFMT_LEFT_J:
  700. confb |= 0x9;
  701. break;
  702. case SND_SOC_DAIFMT_RIGHT_J:
  703. confb |= 0xa;
  704. break;
  705. }
  706. break;
  707. case 16:
  708. dev_dbg(codec->dev, "16bit\n");
  709. switch (sta350->format) {
  710. case SND_SOC_DAIFMT_I2S:
  711. confb |= 0x0;
  712. break;
  713. case SND_SOC_DAIFMT_LEFT_J:
  714. confb |= 0xd;
  715. break;
  716. case SND_SOC_DAIFMT_RIGHT_J:
  717. confb |= 0xe;
  718. break;
  719. }
  720. break;
  721. default:
  722. return -EINVAL;
  723. }
  724. ret = regmap_update_bits(sta350->regmap, STA350_CONFA,
  725. STA350_CONFA_MCS_MASK | STA350_CONFA_IR_MASK,
  726. confa);
  727. if (ret < 0)
  728. return ret;
  729. ret = regmap_update_bits(sta350->regmap, STA350_CONFB,
  730. STA350_CONFB_SAI_MASK | STA350_CONFB_SAIFB,
  731. confb);
  732. if (ret < 0)
  733. return ret;
  734. return 0;
  735. }
  736. static int sta350_startup_sequence(struct sta350_priv *sta350)
  737. {
  738. if (sta350->gpiod_power_down)
  739. gpiod_set_value(sta350->gpiod_power_down, 1);
  740. if (sta350->gpiod_nreset) {
  741. gpiod_set_value(sta350->gpiod_nreset, 0);
  742. mdelay(1);
  743. gpiod_set_value(sta350->gpiod_nreset, 1);
  744. mdelay(1);
  745. }
  746. return 0;
  747. }
  748. /**
  749. * sta350_set_bias_level - DAPM callback
  750. * @codec: the codec device
  751. * @level: DAPM power level
  752. *
  753. * This is called by ALSA to put the codec into low power mode
  754. * or to wake it up. If the codec is powered off completely
  755. * all registers must be restored after power on.
  756. */
  757. static int sta350_set_bias_level(struct snd_soc_codec *codec,
  758. enum snd_soc_bias_level level)
  759. {
  760. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  761. int ret;
  762. dev_dbg(codec->dev, "level = %d\n", level);
  763. switch (level) {
  764. case SND_SOC_BIAS_ON:
  765. break;
  766. case SND_SOC_BIAS_PREPARE:
  767. /* Full power on */
  768. regmap_update_bits(sta350->regmap, STA350_CONFF,
  769. STA350_CONFF_PWDN | STA350_CONFF_EAPD,
  770. STA350_CONFF_PWDN | STA350_CONFF_EAPD);
  771. break;
  772. case SND_SOC_BIAS_STANDBY:
  773. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  774. ret = regulator_bulk_enable(
  775. ARRAY_SIZE(sta350->supplies),
  776. sta350->supplies);
  777. if (ret < 0) {
  778. dev_err(codec->dev,
  779. "Failed to enable supplies: %d\n",
  780. ret);
  781. return ret;
  782. }
  783. sta350_startup_sequence(sta350);
  784. sta350_cache_sync(codec);
  785. }
  786. /* Power down */
  787. regmap_update_bits(sta350->regmap, STA350_CONFF,
  788. STA350_CONFF_PWDN | STA350_CONFF_EAPD,
  789. 0);
  790. break;
  791. case SND_SOC_BIAS_OFF:
  792. /* The chip runs through the power down sequence for us */
  793. regmap_update_bits(sta350->regmap, STA350_CONFF,
  794. STA350_CONFF_PWDN | STA350_CONFF_EAPD, 0);
  795. /* power down: low */
  796. if (sta350->gpiod_power_down)
  797. gpiod_set_value(sta350->gpiod_power_down, 0);
  798. if (sta350->gpiod_nreset)
  799. gpiod_set_value(sta350->gpiod_nreset, 0);
  800. regulator_bulk_disable(ARRAY_SIZE(sta350->supplies),
  801. sta350->supplies);
  802. break;
  803. }
  804. return 0;
  805. }
  806. static const struct snd_soc_dai_ops sta350_dai_ops = {
  807. .hw_params = sta350_hw_params,
  808. .set_sysclk = sta350_set_dai_sysclk,
  809. .set_fmt = sta350_set_dai_fmt,
  810. };
  811. static struct snd_soc_dai_driver sta350_dai = {
  812. .name = "sta350-hifi",
  813. .playback = {
  814. .stream_name = "Playback",
  815. .channels_min = 2,
  816. .channels_max = 2,
  817. .rates = STA350_RATES,
  818. .formats = STA350_FORMATS,
  819. },
  820. .ops = &sta350_dai_ops,
  821. };
  822. static int sta350_probe(struct snd_soc_codec *codec)
  823. {
  824. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  825. struct sta350_platform_data *pdata = sta350->pdata;
  826. int i, ret = 0, thermal = 0;
  827. ret = regulator_bulk_enable(ARRAY_SIZE(sta350->supplies),
  828. sta350->supplies);
  829. if (ret < 0) {
  830. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  831. return ret;
  832. }
  833. ret = sta350_startup_sequence(sta350);
  834. if (ret < 0) {
  835. dev_err(codec->dev, "Failed to startup device\n");
  836. return ret;
  837. }
  838. /* CONFA */
  839. if (!pdata->thermal_warning_recovery)
  840. thermal |= STA350_CONFA_TWAB;
  841. if (!pdata->thermal_warning_adjustment)
  842. thermal |= STA350_CONFA_TWRB;
  843. if (!pdata->fault_detect_recovery)
  844. thermal |= STA350_CONFA_FDRB;
  845. regmap_update_bits(sta350->regmap, STA350_CONFA,
  846. STA350_CONFA_TWAB | STA350_CONFA_TWRB |
  847. STA350_CONFA_FDRB,
  848. thermal);
  849. /* CONFC */
  850. regmap_update_bits(sta350->regmap, STA350_CONFC,
  851. STA350_CONFC_OM_MASK,
  852. pdata->ffx_power_output_mode
  853. << STA350_CONFC_OM_SHIFT);
  854. regmap_update_bits(sta350->regmap, STA350_CONFC,
  855. STA350_CONFC_CSZ_MASK,
  856. pdata->drop_compensation_ns
  857. << STA350_CONFC_CSZ_SHIFT);
  858. regmap_update_bits(sta350->regmap,
  859. STA350_CONFC,
  860. STA350_CONFC_OCRB,
  861. pdata->oc_warning_adjustment ?
  862. STA350_CONFC_OCRB : 0);
  863. /* CONFE */
  864. regmap_update_bits(sta350->regmap, STA350_CONFE,
  865. STA350_CONFE_MPCV,
  866. pdata->max_power_use_mpcc ?
  867. STA350_CONFE_MPCV : 0);
  868. regmap_update_bits(sta350->regmap, STA350_CONFE,
  869. STA350_CONFE_MPC,
  870. pdata->max_power_correction ?
  871. STA350_CONFE_MPC : 0);
  872. regmap_update_bits(sta350->regmap, STA350_CONFE,
  873. STA350_CONFE_AME,
  874. pdata->am_reduction_mode ?
  875. STA350_CONFE_AME : 0);
  876. regmap_update_bits(sta350->regmap, STA350_CONFE,
  877. STA350_CONFE_PWMS,
  878. pdata->odd_pwm_speed_mode ?
  879. STA350_CONFE_PWMS : 0);
  880. regmap_update_bits(sta350->regmap, STA350_CONFE,
  881. STA350_CONFE_DCCV,
  882. pdata->distortion_compensation ?
  883. STA350_CONFE_DCCV : 0);
  884. /* CONFF */
  885. regmap_update_bits(sta350->regmap, STA350_CONFF,
  886. STA350_CONFF_IDE,
  887. pdata->invalid_input_detect_mute ?
  888. STA350_CONFF_IDE : 0);
  889. regmap_update_bits(sta350->regmap, STA350_CONFF,
  890. STA350_CONFF_OCFG_MASK,
  891. pdata->output_conf
  892. << STA350_CONFF_OCFG_SHIFT);
  893. /* channel to output mapping */
  894. regmap_update_bits(sta350->regmap, STA350_C1CFG,
  895. STA350_CxCFG_OM_MASK,
  896. pdata->ch1_output_mapping
  897. << STA350_CxCFG_OM_SHIFT);
  898. regmap_update_bits(sta350->regmap, STA350_C2CFG,
  899. STA350_CxCFG_OM_MASK,
  900. pdata->ch2_output_mapping
  901. << STA350_CxCFG_OM_SHIFT);
  902. regmap_update_bits(sta350->regmap, STA350_C3CFG,
  903. STA350_CxCFG_OM_MASK,
  904. pdata->ch3_output_mapping
  905. << STA350_CxCFG_OM_SHIFT);
  906. /* miscellaneous registers */
  907. regmap_update_bits(sta350->regmap, STA350_MISC1,
  908. STA350_MISC1_CPWMEN,
  909. pdata->activate_mute_output ?
  910. STA350_MISC1_CPWMEN : 0);
  911. regmap_update_bits(sta350->regmap, STA350_MISC1,
  912. STA350_MISC1_BRIDGOFF,
  913. pdata->bridge_immediate_off ?
  914. STA350_MISC1_BRIDGOFF : 0);
  915. regmap_update_bits(sta350->regmap, STA350_MISC1,
  916. STA350_MISC1_NSHHPEN,
  917. pdata->noise_shape_dc_cut ?
  918. STA350_MISC1_NSHHPEN : 0);
  919. regmap_update_bits(sta350->regmap, STA350_MISC1,
  920. STA350_MISC1_RPDNEN,
  921. pdata->powerdown_master_vol ?
  922. STA350_MISC1_RPDNEN: 0);
  923. regmap_update_bits(sta350->regmap, STA350_MISC2,
  924. STA350_MISC2_PNDLSL_MASK,
  925. pdata->powerdown_delay_divider
  926. << STA350_MISC2_PNDLSL_SHIFT);
  927. /* initialize coefficient shadow RAM with reset values */
  928. for (i = 4; i <= 49; i += 5)
  929. sta350->coef_shadow[i] = 0x400000;
  930. for (i = 50; i <= 54; i++)
  931. sta350->coef_shadow[i] = 0x7fffff;
  932. sta350->coef_shadow[55] = 0x5a9df7;
  933. sta350->coef_shadow[56] = 0x7fffff;
  934. sta350->coef_shadow[59] = 0x7fffff;
  935. sta350->coef_shadow[60] = 0x400000;
  936. sta350->coef_shadow[61] = 0x400000;
  937. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
  938. /* Bias level configuration will have done an extra enable */
  939. regulator_bulk_disable(ARRAY_SIZE(sta350->supplies), sta350->supplies);
  940. return 0;
  941. }
  942. static int sta350_remove(struct snd_soc_codec *codec)
  943. {
  944. struct sta350_priv *sta350 = snd_soc_codec_get_drvdata(codec);
  945. regulator_bulk_disable(ARRAY_SIZE(sta350->supplies), sta350->supplies);
  946. return 0;
  947. }
  948. static const struct snd_soc_codec_driver sta350_codec = {
  949. .probe = sta350_probe,
  950. .remove = sta350_remove,
  951. .set_bias_level = sta350_set_bias_level,
  952. .suspend_bias_off = true,
  953. .component_driver = {
  954. .controls = sta350_snd_controls,
  955. .num_controls = ARRAY_SIZE(sta350_snd_controls),
  956. .dapm_widgets = sta350_dapm_widgets,
  957. .num_dapm_widgets = ARRAY_SIZE(sta350_dapm_widgets),
  958. .dapm_routes = sta350_dapm_routes,
  959. .num_dapm_routes = ARRAY_SIZE(sta350_dapm_routes),
  960. },
  961. };
  962. static const struct regmap_config sta350_regmap = {
  963. .reg_bits = 8,
  964. .val_bits = 8,
  965. .max_register = STA350_MISC2,
  966. .reg_defaults = sta350_regs,
  967. .num_reg_defaults = ARRAY_SIZE(sta350_regs),
  968. .cache_type = REGCACHE_RBTREE,
  969. .wr_table = &sta350_write_regs,
  970. .rd_table = &sta350_read_regs,
  971. .volatile_table = &sta350_volatile_regs,
  972. };
  973. #ifdef CONFIG_OF
  974. static const struct of_device_id st350_dt_ids[] = {
  975. { .compatible = "st,sta350", },
  976. { }
  977. };
  978. MODULE_DEVICE_TABLE(of, st350_dt_ids);
  979. static const char * const sta350_ffx_modes[] = {
  980. [STA350_FFX_PM_DROP_COMP] = "drop-compensation",
  981. [STA350_FFX_PM_TAPERED_COMP] = "tapered-compensation",
  982. [STA350_FFX_PM_FULL_POWER] = "full-power-mode",
  983. [STA350_FFX_PM_VARIABLE_DROP_COMP] = "variable-drop-compensation",
  984. };
  985. static int sta350_probe_dt(struct device *dev, struct sta350_priv *sta350)
  986. {
  987. struct device_node *np = dev->of_node;
  988. struct sta350_platform_data *pdata;
  989. const char *ffx_power_mode;
  990. u16 tmp;
  991. u8 tmp8;
  992. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  993. if (!pdata)
  994. return -ENOMEM;
  995. of_property_read_u8(np, "st,output-conf",
  996. &pdata->output_conf);
  997. of_property_read_u8(np, "st,ch1-output-mapping",
  998. &pdata->ch1_output_mapping);
  999. of_property_read_u8(np, "st,ch2-output-mapping",
  1000. &pdata->ch2_output_mapping);
  1001. of_property_read_u8(np, "st,ch3-output-mapping",
  1002. &pdata->ch3_output_mapping);
  1003. if (of_get_property(np, "st,thermal-warning-recovery", NULL))
  1004. pdata->thermal_warning_recovery = 1;
  1005. if (of_get_property(np, "st,thermal-warning-adjustment", NULL))
  1006. pdata->thermal_warning_adjustment = 1;
  1007. if (of_get_property(np, "st,fault-detect-recovery", NULL))
  1008. pdata->fault_detect_recovery = 1;
  1009. pdata->ffx_power_output_mode = STA350_FFX_PM_VARIABLE_DROP_COMP;
  1010. if (!of_property_read_string(np, "st,ffx-power-output-mode",
  1011. &ffx_power_mode)) {
  1012. int i, mode = -EINVAL;
  1013. for (i = 0; i < ARRAY_SIZE(sta350_ffx_modes); i++)
  1014. if (!strcasecmp(ffx_power_mode, sta350_ffx_modes[i]))
  1015. mode = i;
  1016. if (mode < 0)
  1017. dev_warn(dev, "Unsupported ffx output mode: %s\n",
  1018. ffx_power_mode);
  1019. else
  1020. pdata->ffx_power_output_mode = mode;
  1021. }
  1022. tmp = 140;
  1023. of_property_read_u16(np, "st,drop-compensation-ns", &tmp);
  1024. pdata->drop_compensation_ns = clamp_t(u16, tmp, 0, 300) / 20;
  1025. if (of_get_property(np, "st,overcurrent-warning-adjustment", NULL))
  1026. pdata->oc_warning_adjustment = 1;
  1027. /* CONFE */
  1028. if (of_get_property(np, "st,max-power-use-mpcc", NULL))
  1029. pdata->max_power_use_mpcc = 1;
  1030. if (of_get_property(np, "st,max-power-correction", NULL))
  1031. pdata->max_power_correction = 1;
  1032. if (of_get_property(np, "st,am-reduction-mode", NULL))
  1033. pdata->am_reduction_mode = 1;
  1034. if (of_get_property(np, "st,odd-pwm-speed-mode", NULL))
  1035. pdata->odd_pwm_speed_mode = 1;
  1036. if (of_get_property(np, "st,distortion-compensation", NULL))
  1037. pdata->distortion_compensation = 1;
  1038. /* CONFF */
  1039. if (of_get_property(np, "st,invalid-input-detect-mute", NULL))
  1040. pdata->invalid_input_detect_mute = 1;
  1041. /* MISC */
  1042. if (of_get_property(np, "st,activate-mute-output", NULL))
  1043. pdata->activate_mute_output = 1;
  1044. if (of_get_property(np, "st,bridge-immediate-off", NULL))
  1045. pdata->bridge_immediate_off = 1;
  1046. if (of_get_property(np, "st,noise-shape-dc-cut", NULL))
  1047. pdata->noise_shape_dc_cut = 1;
  1048. if (of_get_property(np, "st,powerdown-master-volume", NULL))
  1049. pdata->powerdown_master_vol = 1;
  1050. if (!of_property_read_u8(np, "st,powerdown-delay-divider", &tmp8)) {
  1051. if (is_power_of_2(tmp8) && tmp8 >= 1 && tmp8 <= 128)
  1052. pdata->powerdown_delay_divider = ilog2(tmp8);
  1053. else
  1054. dev_warn(dev, "Unsupported powerdown delay divider %d\n",
  1055. tmp8);
  1056. }
  1057. sta350->pdata = pdata;
  1058. return 0;
  1059. }
  1060. #endif
  1061. static int sta350_i2c_probe(struct i2c_client *i2c,
  1062. const struct i2c_device_id *id)
  1063. {
  1064. struct device *dev = &i2c->dev;
  1065. struct sta350_priv *sta350;
  1066. int ret, i;
  1067. sta350 = devm_kzalloc(dev, sizeof(struct sta350_priv), GFP_KERNEL);
  1068. if (!sta350)
  1069. return -ENOMEM;
  1070. mutex_init(&sta350->coeff_lock);
  1071. sta350->pdata = dev_get_platdata(dev);
  1072. #ifdef CONFIG_OF
  1073. if (dev->of_node) {
  1074. ret = sta350_probe_dt(dev, sta350);
  1075. if (ret < 0)
  1076. return ret;
  1077. }
  1078. #endif
  1079. /* GPIOs */
  1080. sta350->gpiod_nreset = devm_gpiod_get_optional(dev, "reset",
  1081. GPIOD_OUT_LOW);
  1082. if (IS_ERR(sta350->gpiod_nreset))
  1083. return PTR_ERR(sta350->gpiod_nreset);
  1084. sta350->gpiod_power_down = devm_gpiod_get_optional(dev, "power-down",
  1085. GPIOD_OUT_LOW);
  1086. if (IS_ERR(sta350->gpiod_power_down))
  1087. return PTR_ERR(sta350->gpiod_power_down);
  1088. /* regulators */
  1089. for (i = 0; i < ARRAY_SIZE(sta350->supplies); i++)
  1090. sta350->supplies[i].supply = sta350_supply_names[i];
  1091. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(sta350->supplies),
  1092. sta350->supplies);
  1093. if (ret < 0) {
  1094. dev_err(dev, "Failed to request supplies: %d\n", ret);
  1095. return ret;
  1096. }
  1097. sta350->regmap = devm_regmap_init_i2c(i2c, &sta350_regmap);
  1098. if (IS_ERR(sta350->regmap)) {
  1099. ret = PTR_ERR(sta350->regmap);
  1100. dev_err(dev, "Failed to init regmap: %d\n", ret);
  1101. return ret;
  1102. }
  1103. i2c_set_clientdata(i2c, sta350);
  1104. ret = snd_soc_register_codec(dev, &sta350_codec, &sta350_dai, 1);
  1105. if (ret < 0)
  1106. dev_err(dev, "Failed to register codec (%d)\n", ret);
  1107. return ret;
  1108. }
  1109. static int sta350_i2c_remove(struct i2c_client *client)
  1110. {
  1111. snd_soc_unregister_codec(&client->dev);
  1112. return 0;
  1113. }
  1114. static const struct i2c_device_id sta350_i2c_id[] = {
  1115. { "sta350", 0 },
  1116. { }
  1117. };
  1118. MODULE_DEVICE_TABLE(i2c, sta350_i2c_id);
  1119. static struct i2c_driver sta350_i2c_driver = {
  1120. .driver = {
  1121. .name = "sta350",
  1122. .of_match_table = of_match_ptr(st350_dt_ids),
  1123. },
  1124. .probe = sta350_i2c_probe,
  1125. .remove = sta350_i2c_remove,
  1126. .id_table = sta350_i2c_id,
  1127. };
  1128. module_i2c_driver(sta350_i2c_driver);
  1129. MODULE_DESCRIPTION("ASoC STA350 driver");
  1130. MODULE_AUTHOR("Sven Brandau <info@brandau.biz>");
  1131. MODULE_LICENSE("GPL");