sta350.c 36 KB

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