ssm2518.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SSM2518 amplifier audio driver
  4. *
  5. * Copyright 2013 Analog Devices Inc.
  6. * Author: Lars-Peter Clausen <lars@metafoo.de>
  7. */
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/i2c.h>
  12. #include <linux/regmap.h>
  13. #include <linux/slab.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <sound/core.h>
  16. #include <sound/pcm.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/soc.h>
  19. #include <sound/initval.h>
  20. #include <sound/tlv.h>
  21. #include "ssm2518.h"
  22. #define SSM2518_REG_POWER1 0x00
  23. #define SSM2518_REG_CLOCK 0x01
  24. #define SSM2518_REG_SAI_CTRL1 0x02
  25. #define SSM2518_REG_SAI_CTRL2 0x03
  26. #define SSM2518_REG_CHAN_MAP 0x04
  27. #define SSM2518_REG_LEFT_VOL 0x05
  28. #define SSM2518_REG_RIGHT_VOL 0x06
  29. #define SSM2518_REG_MUTE_CTRL 0x07
  30. #define SSM2518_REG_FAULT_CTRL 0x08
  31. #define SSM2518_REG_POWER2 0x09
  32. #define SSM2518_REG_DRC_1 0x0a
  33. #define SSM2518_REG_DRC_2 0x0b
  34. #define SSM2518_REG_DRC_3 0x0c
  35. #define SSM2518_REG_DRC_4 0x0d
  36. #define SSM2518_REG_DRC_5 0x0e
  37. #define SSM2518_REG_DRC_6 0x0f
  38. #define SSM2518_REG_DRC_7 0x10
  39. #define SSM2518_REG_DRC_8 0x11
  40. #define SSM2518_REG_DRC_9 0x12
  41. #define SSM2518_POWER1_RESET BIT(7)
  42. #define SSM2518_POWER1_NO_BCLK BIT(5)
  43. #define SSM2518_POWER1_MCS_MASK (0xf << 1)
  44. #define SSM2518_POWER1_MCS_64FS (0x0 << 1)
  45. #define SSM2518_POWER1_MCS_128FS (0x1 << 1)
  46. #define SSM2518_POWER1_MCS_256FS (0x2 << 1)
  47. #define SSM2518_POWER1_MCS_384FS (0x3 << 1)
  48. #define SSM2518_POWER1_MCS_512FS (0x4 << 1)
  49. #define SSM2518_POWER1_MCS_768FS (0x5 << 1)
  50. #define SSM2518_POWER1_MCS_100FS (0x6 << 1)
  51. #define SSM2518_POWER1_MCS_200FS (0x7 << 1)
  52. #define SSM2518_POWER1_MCS_400FS (0x8 << 1)
  53. #define SSM2518_POWER1_SPWDN BIT(0)
  54. #define SSM2518_CLOCK_ASR BIT(0)
  55. #define SSM2518_SAI_CTRL1_FMT_MASK (0x3 << 5)
  56. #define SSM2518_SAI_CTRL1_FMT_I2S (0x0 << 5)
  57. #define SSM2518_SAI_CTRL1_FMT_LJ (0x1 << 5)
  58. #define SSM2518_SAI_CTRL1_FMT_RJ_24BIT (0x2 << 5)
  59. #define SSM2518_SAI_CTRL1_FMT_RJ_16BIT (0x3 << 5)
  60. #define SSM2518_SAI_CTRL1_SAI_MASK (0x7 << 2)
  61. #define SSM2518_SAI_CTRL1_SAI_I2S (0x0 << 2)
  62. #define SSM2518_SAI_CTRL1_SAI_TDM_2 (0x1 << 2)
  63. #define SSM2518_SAI_CTRL1_SAI_TDM_4 (0x2 << 2)
  64. #define SSM2518_SAI_CTRL1_SAI_TDM_8 (0x3 << 2)
  65. #define SSM2518_SAI_CTRL1_SAI_TDM_16 (0x4 << 2)
  66. #define SSM2518_SAI_CTRL1_SAI_MONO (0x5 << 2)
  67. #define SSM2518_SAI_CTRL1_FS_MASK (0x3)
  68. #define SSM2518_SAI_CTRL1_FS_8000_12000 (0x0)
  69. #define SSM2518_SAI_CTRL1_FS_16000_24000 (0x1)
  70. #define SSM2518_SAI_CTRL1_FS_32000_48000 (0x2)
  71. #define SSM2518_SAI_CTRL1_FS_64000_96000 (0x3)
  72. #define SSM2518_SAI_CTRL2_BCLK_INTERAL BIT(7)
  73. #define SSM2518_SAI_CTRL2_LRCLK_PULSE BIT(6)
  74. #define SSM2518_SAI_CTRL2_LRCLK_INVERT BIT(5)
  75. #define SSM2518_SAI_CTRL2_MSB BIT(4)
  76. #define SSM2518_SAI_CTRL2_SLOT_WIDTH_MASK (0x3 << 2)
  77. #define SSM2518_SAI_CTRL2_SLOT_WIDTH_32 (0x0 << 2)
  78. #define SSM2518_SAI_CTRL2_SLOT_WIDTH_24 (0x1 << 2)
  79. #define SSM2518_SAI_CTRL2_SLOT_WIDTH_16 (0x2 << 2)
  80. #define SSM2518_SAI_CTRL2_BCLK_INVERT BIT(1)
  81. #define SSM2518_CHAN_MAP_RIGHT_SLOT_OFFSET 4
  82. #define SSM2518_CHAN_MAP_RIGHT_SLOT_MASK 0xf0
  83. #define SSM2518_CHAN_MAP_LEFT_SLOT_OFFSET 0
  84. #define SSM2518_CHAN_MAP_LEFT_SLOT_MASK 0x0f
  85. #define SSM2518_MUTE_CTRL_ANA_GAIN BIT(5)
  86. #define SSM2518_MUTE_CTRL_MUTE_MASTER BIT(0)
  87. #define SSM2518_POWER2_APWDN BIT(0)
  88. #define SSM2518_DAC_MUTE BIT(6)
  89. #define SSM2518_DAC_FS_MASK 0x07
  90. #define SSM2518_DAC_FS_8000 0x00
  91. #define SSM2518_DAC_FS_16000 0x01
  92. #define SSM2518_DAC_FS_32000 0x02
  93. #define SSM2518_DAC_FS_64000 0x03
  94. #define SSM2518_DAC_FS_128000 0x04
  95. struct ssm2518 {
  96. struct regmap *regmap;
  97. bool right_j;
  98. unsigned int sysclk;
  99. const struct snd_pcm_hw_constraint_list *constraints;
  100. struct gpio_desc *enable_gpio;
  101. };
  102. static const struct reg_default ssm2518_reg_defaults[] = {
  103. { 0x00, 0x05 },
  104. { 0x01, 0x00 },
  105. { 0x02, 0x02 },
  106. { 0x03, 0x00 },
  107. { 0x04, 0x10 },
  108. { 0x05, 0x40 },
  109. { 0x06, 0x40 },
  110. { 0x07, 0x81 },
  111. { 0x08, 0x0c },
  112. { 0x09, 0x99 },
  113. { 0x0a, 0x7c },
  114. { 0x0b, 0x5b },
  115. { 0x0c, 0x57 },
  116. { 0x0d, 0x89 },
  117. { 0x0e, 0x8c },
  118. { 0x0f, 0x77 },
  119. { 0x10, 0x26 },
  120. { 0x11, 0x1c },
  121. { 0x12, 0x97 },
  122. };
  123. static const DECLARE_TLV_DB_MINMAX_MUTE(ssm2518_vol_tlv, -7125, 2400);
  124. static const DECLARE_TLV_DB_SCALE(ssm2518_compressor_tlv, -3400, 200, 0);
  125. static const DECLARE_TLV_DB_SCALE(ssm2518_expander_tlv, -8100, 300, 0);
  126. static const DECLARE_TLV_DB_SCALE(ssm2518_noise_gate_tlv, -9600, 300, 0);
  127. static const DECLARE_TLV_DB_SCALE(ssm2518_post_drc_tlv, -2400, 300, 0);
  128. static const DECLARE_TLV_DB_RANGE(ssm2518_limiter_tlv,
  129. 0, 7, TLV_DB_SCALE_ITEM(-2200, 200, 0),
  130. 7, 15, TLV_DB_SCALE_ITEM(-800, 100, 0),
  131. );
  132. static const char * const ssm2518_drc_peak_detector_attack_time_text[] = {
  133. "0 ms", "0.1 ms", "0.19 ms", "0.37 ms", "0.75 ms", "1.5 ms", "3 ms",
  134. "6 ms", "12 ms", "24 ms", "48 ms", "96 ms", "192 ms", "384 ms",
  135. "768 ms", "1536 ms",
  136. };
  137. static const char * const ssm2518_drc_peak_detector_release_time_text[] = {
  138. "0 ms", "1.5 ms", "3 ms", "6 ms", "12 ms", "24 ms", "48 ms", "96 ms",
  139. "192 ms", "384 ms", "768 ms", "1536 ms", "3072 ms", "6144 ms",
  140. "12288 ms", "24576 ms"
  141. };
  142. static const char * const ssm2518_drc_hold_time_text[] = {
  143. "0 ms", "0.67 ms", "1.33 ms", "2.67 ms", "5.33 ms", "10.66 ms",
  144. "21.32 ms", "42.64 ms", "85.28 ms", "170.56 ms", "341.12 ms",
  145. "682.24 ms", "1364 ms",
  146. };
  147. static SOC_ENUM_SINGLE_DECL(ssm2518_drc_peak_detector_attack_time_enum,
  148. SSM2518_REG_DRC_2, 4, ssm2518_drc_peak_detector_attack_time_text);
  149. static SOC_ENUM_SINGLE_DECL(ssm2518_drc_peak_detector_release_time_enum,
  150. SSM2518_REG_DRC_2, 0, ssm2518_drc_peak_detector_release_time_text);
  151. static SOC_ENUM_SINGLE_DECL(ssm2518_drc_attack_time_enum,
  152. SSM2518_REG_DRC_6, 4, ssm2518_drc_peak_detector_attack_time_text);
  153. static SOC_ENUM_SINGLE_DECL(ssm2518_drc_decay_time_enum,
  154. SSM2518_REG_DRC_6, 0, ssm2518_drc_peak_detector_release_time_text);
  155. static SOC_ENUM_SINGLE_DECL(ssm2518_drc_hold_time_enum,
  156. SSM2518_REG_DRC_7, 4, ssm2518_drc_hold_time_text);
  157. static SOC_ENUM_SINGLE_DECL(ssm2518_drc_noise_gate_hold_time_enum,
  158. SSM2518_REG_DRC_7, 0, ssm2518_drc_hold_time_text);
  159. static SOC_ENUM_SINGLE_DECL(ssm2518_drc_rms_averaging_time_enum,
  160. SSM2518_REG_DRC_9, 0, ssm2518_drc_peak_detector_release_time_text);
  161. static const struct snd_kcontrol_new ssm2518_snd_controls[] = {
  162. SOC_SINGLE("Playback De-emphasis Switch", SSM2518_REG_MUTE_CTRL,
  163. 4, 1, 0),
  164. SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2518_REG_LEFT_VOL,
  165. SSM2518_REG_RIGHT_VOL, 0, 0xff, 1, ssm2518_vol_tlv),
  166. SOC_DOUBLE("Master Playback Switch", SSM2518_REG_MUTE_CTRL, 2, 1, 1, 1),
  167. SOC_SINGLE("Amp Low Power Mode Switch", SSM2518_REG_POWER2, 4, 1, 0),
  168. SOC_SINGLE("DAC Low Power Mode Switch", SSM2518_REG_POWER2, 3, 1, 0),
  169. SOC_SINGLE("DRC Limiter Switch", SSM2518_REG_DRC_1, 5, 1, 0),
  170. SOC_SINGLE("DRC Compressor Switch", SSM2518_REG_DRC_1, 4, 1, 0),
  171. SOC_SINGLE("DRC Expander Switch", SSM2518_REG_DRC_1, 3, 1, 0),
  172. SOC_SINGLE("DRC Noise Gate Switch", SSM2518_REG_DRC_1, 2, 1, 0),
  173. SOC_DOUBLE("DRC Switch", SSM2518_REG_DRC_1, 0, 1, 1, 0),
  174. SOC_SINGLE_TLV("DRC Limiter Threshold Volume",
  175. SSM2518_REG_DRC_3, 4, 15, 1, ssm2518_limiter_tlv),
  176. SOC_SINGLE_TLV("DRC Compressor Lower Threshold Volume",
  177. SSM2518_REG_DRC_3, 0, 15, 1, ssm2518_compressor_tlv),
  178. SOC_SINGLE_TLV("DRC Expander Upper Threshold Volume", SSM2518_REG_DRC_4,
  179. 4, 15, 1, ssm2518_expander_tlv),
  180. SOC_SINGLE_TLV("DRC Noise Gate Threshold Volume",
  181. SSM2518_REG_DRC_4, 0, 15, 1, ssm2518_noise_gate_tlv),
  182. SOC_SINGLE_TLV("DRC Upper Output Threshold Volume",
  183. SSM2518_REG_DRC_5, 4, 15, 1, ssm2518_limiter_tlv),
  184. SOC_SINGLE_TLV("DRC Lower Output Threshold Volume",
  185. SSM2518_REG_DRC_5, 0, 15, 1, ssm2518_noise_gate_tlv),
  186. SOC_SINGLE_TLV("DRC Post Volume", SSM2518_REG_DRC_8,
  187. 2, 15, 1, ssm2518_post_drc_tlv),
  188. SOC_ENUM("DRC Peak Detector Attack Time",
  189. ssm2518_drc_peak_detector_attack_time_enum),
  190. SOC_ENUM("DRC Peak Detector Release Time",
  191. ssm2518_drc_peak_detector_release_time_enum),
  192. SOC_ENUM("DRC Attack Time", ssm2518_drc_attack_time_enum),
  193. SOC_ENUM("DRC Decay Time", ssm2518_drc_decay_time_enum),
  194. SOC_ENUM("DRC Hold Time", ssm2518_drc_hold_time_enum),
  195. SOC_ENUM("DRC Noise Gate Hold Time",
  196. ssm2518_drc_noise_gate_hold_time_enum),
  197. SOC_ENUM("DRC RMS Averaging Time", ssm2518_drc_rms_averaging_time_enum),
  198. };
  199. static const struct snd_soc_dapm_widget ssm2518_dapm_widgets[] = {
  200. SND_SOC_DAPM_DAC("DACL", "HiFi Playback", SSM2518_REG_POWER2, 1, 1),
  201. SND_SOC_DAPM_DAC("DACR", "HiFi Playback", SSM2518_REG_POWER2, 2, 1),
  202. SND_SOC_DAPM_OUTPUT("OUTL"),
  203. SND_SOC_DAPM_OUTPUT("OUTR"),
  204. };
  205. static const struct snd_soc_dapm_route ssm2518_routes[] = {
  206. { "OUTL", NULL, "DACL" },
  207. { "OUTR", NULL, "DACR" },
  208. };
  209. struct ssm2518_mcs_lut {
  210. unsigned int rate;
  211. const unsigned int *sysclks;
  212. };
  213. static const unsigned int ssm2518_sysclks_2048000[] = {
  214. 2048000, 4096000, 8192000, 12288000, 16384000, 24576000,
  215. 3200000, 6400000, 12800000, 0
  216. };
  217. static const unsigned int ssm2518_sysclks_2822000[] = {
  218. 2822000, 5644800, 11289600, 16934400, 22579200, 33868800,
  219. 4410000, 8820000, 17640000, 0
  220. };
  221. static const unsigned int ssm2518_sysclks_3072000[] = {
  222. 3072000, 6144000, 12288000, 16384000, 24576000, 38864000,
  223. 4800000, 9600000, 19200000, 0
  224. };
  225. static const struct ssm2518_mcs_lut ssm2518_mcs_lut[] = {
  226. { 8000, ssm2518_sysclks_2048000, },
  227. { 11025, ssm2518_sysclks_2822000, },
  228. { 12000, ssm2518_sysclks_3072000, },
  229. { 16000, ssm2518_sysclks_2048000, },
  230. { 24000, ssm2518_sysclks_3072000, },
  231. { 22050, ssm2518_sysclks_2822000, },
  232. { 32000, ssm2518_sysclks_2048000, },
  233. { 44100, ssm2518_sysclks_2822000, },
  234. { 48000, ssm2518_sysclks_3072000, },
  235. { 96000, ssm2518_sysclks_3072000, },
  236. };
  237. static const unsigned int ssm2518_rates_2048000[] = {
  238. 8000, 16000, 32000,
  239. };
  240. static const struct snd_pcm_hw_constraint_list ssm2518_constraints_2048000 = {
  241. .list = ssm2518_rates_2048000,
  242. .count = ARRAY_SIZE(ssm2518_rates_2048000),
  243. };
  244. static const unsigned int ssm2518_rates_2822000[] = {
  245. 11025, 22050, 44100,
  246. };
  247. static const struct snd_pcm_hw_constraint_list ssm2518_constraints_2822000 = {
  248. .list = ssm2518_rates_2822000,
  249. .count = ARRAY_SIZE(ssm2518_rates_2822000),
  250. };
  251. static const unsigned int ssm2518_rates_3072000[] = {
  252. 12000, 24000, 48000, 96000,
  253. };
  254. static const struct snd_pcm_hw_constraint_list ssm2518_constraints_3072000 = {
  255. .list = ssm2518_rates_3072000,
  256. .count = ARRAY_SIZE(ssm2518_rates_3072000),
  257. };
  258. static const unsigned int ssm2518_rates_12288000[] = {
  259. 8000, 12000, 16000, 24000, 32000, 48000, 96000,
  260. };
  261. static const struct snd_pcm_hw_constraint_list ssm2518_constraints_12288000 = {
  262. .list = ssm2518_rates_12288000,
  263. .count = ARRAY_SIZE(ssm2518_rates_12288000),
  264. };
  265. static int ssm2518_lookup_mcs(struct ssm2518 *ssm2518,
  266. unsigned int rate)
  267. {
  268. const unsigned int *sysclks = NULL;
  269. int i;
  270. for (i = 0; i < ARRAY_SIZE(ssm2518_mcs_lut); i++) {
  271. if (ssm2518_mcs_lut[i].rate == rate) {
  272. sysclks = ssm2518_mcs_lut[i].sysclks;
  273. break;
  274. }
  275. }
  276. if (!sysclks)
  277. return -EINVAL;
  278. for (i = 0; sysclks[i]; i++) {
  279. if (sysclks[i] == ssm2518->sysclk)
  280. return i;
  281. }
  282. return -EINVAL;
  283. }
  284. static int ssm2518_hw_params(struct snd_pcm_substream *substream,
  285. struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
  286. {
  287. struct snd_soc_component *component = dai->component;
  288. struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(component);
  289. unsigned int rate = params_rate(params);
  290. unsigned int ctrl1, ctrl1_mask;
  291. int mcs;
  292. int ret;
  293. mcs = ssm2518_lookup_mcs(ssm2518, rate);
  294. if (mcs < 0)
  295. return mcs;
  296. ctrl1_mask = SSM2518_SAI_CTRL1_FS_MASK;
  297. if (rate >= 8000 && rate <= 12000)
  298. ctrl1 = SSM2518_SAI_CTRL1_FS_8000_12000;
  299. else if (rate >= 16000 && rate <= 24000)
  300. ctrl1 = SSM2518_SAI_CTRL1_FS_16000_24000;
  301. else if (rate >= 32000 && rate <= 48000)
  302. ctrl1 = SSM2518_SAI_CTRL1_FS_32000_48000;
  303. else if (rate >= 64000 && rate <= 96000)
  304. ctrl1 = SSM2518_SAI_CTRL1_FS_64000_96000;
  305. else
  306. return -EINVAL;
  307. if (ssm2518->right_j) {
  308. switch (params_width(params)) {
  309. case 16:
  310. ctrl1 |= SSM2518_SAI_CTRL1_FMT_RJ_16BIT;
  311. break;
  312. case 24:
  313. ctrl1 |= SSM2518_SAI_CTRL1_FMT_RJ_24BIT;
  314. break;
  315. default:
  316. return -EINVAL;
  317. }
  318. ctrl1_mask |= SSM2518_SAI_CTRL1_FMT_MASK;
  319. }
  320. /* Disable auto samplerate detection */
  321. ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_CLOCK,
  322. SSM2518_CLOCK_ASR, SSM2518_CLOCK_ASR);
  323. if (ret < 0)
  324. return ret;
  325. ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_SAI_CTRL1,
  326. ctrl1_mask, ctrl1);
  327. if (ret < 0)
  328. return ret;
  329. return regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER1,
  330. SSM2518_POWER1_MCS_MASK, mcs << 1);
  331. }
  332. static int ssm2518_mute(struct snd_soc_dai *dai, int mute, int direction)
  333. {
  334. struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(dai->component);
  335. unsigned int val;
  336. if (mute)
  337. val = SSM2518_MUTE_CTRL_MUTE_MASTER;
  338. else
  339. val = 0;
  340. return regmap_update_bits(ssm2518->regmap, SSM2518_REG_MUTE_CTRL,
  341. SSM2518_MUTE_CTRL_MUTE_MASTER, val);
  342. }
  343. static int ssm2518_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  344. {
  345. struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(dai->component);
  346. unsigned int ctrl1 = 0, ctrl2 = 0;
  347. bool invert_fclk;
  348. int ret;
  349. switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  350. case SND_SOC_DAIFMT_CBC_CFC:
  351. break;
  352. default:
  353. return -EINVAL;
  354. }
  355. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  356. case SND_SOC_DAIFMT_NB_NF:
  357. invert_fclk = false;
  358. break;
  359. case SND_SOC_DAIFMT_IB_NF:
  360. ctrl2 |= SSM2518_SAI_CTRL2_BCLK_INVERT;
  361. invert_fclk = false;
  362. break;
  363. case SND_SOC_DAIFMT_NB_IF:
  364. invert_fclk = true;
  365. break;
  366. case SND_SOC_DAIFMT_IB_IF:
  367. ctrl2 |= SSM2518_SAI_CTRL2_BCLK_INVERT;
  368. invert_fclk = true;
  369. break;
  370. default:
  371. return -EINVAL;
  372. }
  373. ssm2518->right_j = false;
  374. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  375. case SND_SOC_DAIFMT_I2S:
  376. ctrl1 |= SSM2518_SAI_CTRL1_FMT_I2S;
  377. break;
  378. case SND_SOC_DAIFMT_LEFT_J:
  379. ctrl1 |= SSM2518_SAI_CTRL1_FMT_LJ;
  380. invert_fclk = !invert_fclk;
  381. break;
  382. case SND_SOC_DAIFMT_RIGHT_J:
  383. ctrl1 |= SSM2518_SAI_CTRL1_FMT_RJ_24BIT;
  384. ssm2518->right_j = true;
  385. invert_fclk = !invert_fclk;
  386. break;
  387. case SND_SOC_DAIFMT_DSP_A:
  388. ctrl2 |= SSM2518_SAI_CTRL2_LRCLK_PULSE;
  389. ctrl1 |= SSM2518_SAI_CTRL1_FMT_I2S;
  390. invert_fclk = false;
  391. break;
  392. case SND_SOC_DAIFMT_DSP_B:
  393. ctrl2 |= SSM2518_SAI_CTRL2_LRCLK_PULSE;
  394. ctrl1 |= SSM2518_SAI_CTRL1_FMT_LJ;
  395. invert_fclk = false;
  396. break;
  397. default:
  398. return -EINVAL;
  399. }
  400. if (invert_fclk)
  401. ctrl2 |= SSM2518_SAI_CTRL2_LRCLK_INVERT;
  402. ret = regmap_write(ssm2518->regmap, SSM2518_REG_SAI_CTRL1, ctrl1);
  403. if (ret)
  404. return ret;
  405. return regmap_write(ssm2518->regmap, SSM2518_REG_SAI_CTRL2, ctrl2);
  406. }
  407. static int ssm2518_set_power(struct ssm2518 *ssm2518, bool enable)
  408. {
  409. int ret = 0;
  410. if (!enable) {
  411. ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER1,
  412. SSM2518_POWER1_SPWDN, SSM2518_POWER1_SPWDN);
  413. regcache_mark_dirty(ssm2518->regmap);
  414. }
  415. if (ssm2518->enable_gpio)
  416. gpiod_set_value_cansleep(ssm2518->enable_gpio, enable);
  417. regcache_cache_only(ssm2518->regmap, !enable);
  418. if (enable) {
  419. ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER1,
  420. SSM2518_POWER1_SPWDN | SSM2518_POWER1_RESET, 0x00);
  421. regcache_sync(ssm2518->regmap);
  422. }
  423. return ret;
  424. }
  425. static int ssm2518_set_bias_level(struct snd_soc_component *component,
  426. enum snd_soc_bias_level level)
  427. {
  428. struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(component);
  429. int ret = 0;
  430. switch (level) {
  431. case SND_SOC_BIAS_ON:
  432. break;
  433. case SND_SOC_BIAS_PREPARE:
  434. break;
  435. case SND_SOC_BIAS_STANDBY:
  436. if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
  437. ret = ssm2518_set_power(ssm2518, true);
  438. break;
  439. case SND_SOC_BIAS_OFF:
  440. ret = ssm2518_set_power(ssm2518, false);
  441. break;
  442. }
  443. return ret;
  444. }
  445. static int ssm2518_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  446. unsigned int rx_mask, int slots, int width)
  447. {
  448. struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(dai->component);
  449. unsigned int ctrl1, ctrl2;
  450. int left_slot, right_slot;
  451. int ret;
  452. if (slots == 0)
  453. return regmap_update_bits(ssm2518->regmap,
  454. SSM2518_REG_SAI_CTRL1, SSM2518_SAI_CTRL1_SAI_MASK,
  455. SSM2518_SAI_CTRL1_SAI_I2S);
  456. if (tx_mask == 0 || rx_mask != 0)
  457. return -EINVAL;
  458. if (slots == 1) {
  459. if (tx_mask != 1)
  460. return -EINVAL;
  461. left_slot = 0;
  462. right_slot = 0;
  463. } else {
  464. /* We assume the left channel < right channel */
  465. left_slot = __ffs(tx_mask);
  466. tx_mask &= ~(1 << left_slot);
  467. if (tx_mask == 0) {
  468. right_slot = left_slot;
  469. } else {
  470. right_slot = __ffs(tx_mask);
  471. tx_mask &= ~(1 << right_slot);
  472. }
  473. }
  474. if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
  475. return -EINVAL;
  476. switch (width) {
  477. case 16:
  478. ctrl2 = SSM2518_SAI_CTRL2_SLOT_WIDTH_16;
  479. break;
  480. case 24:
  481. ctrl2 = SSM2518_SAI_CTRL2_SLOT_WIDTH_24;
  482. break;
  483. case 32:
  484. ctrl2 = SSM2518_SAI_CTRL2_SLOT_WIDTH_32;
  485. break;
  486. default:
  487. return -EINVAL;
  488. }
  489. switch (slots) {
  490. case 1:
  491. ctrl1 = SSM2518_SAI_CTRL1_SAI_MONO;
  492. break;
  493. case 2:
  494. ctrl1 = SSM2518_SAI_CTRL1_SAI_TDM_2;
  495. break;
  496. case 4:
  497. ctrl1 = SSM2518_SAI_CTRL1_SAI_TDM_4;
  498. break;
  499. case 8:
  500. ctrl1 = SSM2518_SAI_CTRL1_SAI_TDM_8;
  501. break;
  502. case 16:
  503. ctrl1 = SSM2518_SAI_CTRL1_SAI_TDM_16;
  504. break;
  505. default:
  506. return -EINVAL;
  507. }
  508. ret = regmap_write(ssm2518->regmap, SSM2518_REG_CHAN_MAP,
  509. (left_slot << SSM2518_CHAN_MAP_LEFT_SLOT_OFFSET) |
  510. (right_slot << SSM2518_CHAN_MAP_RIGHT_SLOT_OFFSET));
  511. if (ret)
  512. return ret;
  513. ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_SAI_CTRL1,
  514. SSM2518_SAI_CTRL1_SAI_MASK, ctrl1);
  515. if (ret)
  516. return ret;
  517. return regmap_update_bits(ssm2518->regmap, SSM2518_REG_SAI_CTRL2,
  518. SSM2518_SAI_CTRL2_SLOT_WIDTH_MASK, ctrl2);
  519. }
  520. static int ssm2518_startup(struct snd_pcm_substream *substream,
  521. struct snd_soc_dai *dai)
  522. {
  523. struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(dai->component);
  524. if (ssm2518->constraints)
  525. snd_pcm_hw_constraint_list(substream->runtime, 0,
  526. SNDRV_PCM_HW_PARAM_RATE, ssm2518->constraints);
  527. return 0;
  528. }
  529. #define SSM2518_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
  530. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32)
  531. static const struct snd_soc_dai_ops ssm2518_dai_ops = {
  532. .startup = ssm2518_startup,
  533. .hw_params = ssm2518_hw_params,
  534. .mute_stream = ssm2518_mute,
  535. .set_fmt = ssm2518_set_dai_fmt,
  536. .set_tdm_slot = ssm2518_set_tdm_slot,
  537. .no_capture_mute = 1,
  538. };
  539. static struct snd_soc_dai_driver ssm2518_dai = {
  540. .name = "ssm2518-hifi",
  541. .playback = {
  542. .stream_name = "Playback",
  543. .channels_min = 2,
  544. .channels_max = 2,
  545. .rates = SNDRV_PCM_RATE_8000_96000,
  546. .formats = SSM2518_FORMATS,
  547. },
  548. .ops = &ssm2518_dai_ops,
  549. };
  550. static int ssm2518_set_sysclk(struct snd_soc_component *component, int clk_id,
  551. int source, unsigned int freq, int dir)
  552. {
  553. struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(component);
  554. unsigned int val;
  555. if (clk_id != SSM2518_SYSCLK)
  556. return -EINVAL;
  557. switch (source) {
  558. case SSM2518_SYSCLK_SRC_MCLK:
  559. val = 0;
  560. break;
  561. case SSM2518_SYSCLK_SRC_BCLK:
  562. /* In this case the bitclock is used as the system clock, and
  563. * the bitclock signal needs to be connected to the MCLK pin and
  564. * the BCLK pin is left unconnected */
  565. val = SSM2518_POWER1_NO_BCLK;
  566. break;
  567. default:
  568. return -EINVAL;
  569. }
  570. switch (freq) {
  571. case 0:
  572. ssm2518->constraints = NULL;
  573. break;
  574. case 2048000:
  575. case 4096000:
  576. case 8192000:
  577. case 3200000:
  578. case 6400000:
  579. case 12800000:
  580. ssm2518->constraints = &ssm2518_constraints_2048000;
  581. break;
  582. case 2822000:
  583. case 5644800:
  584. case 11289600:
  585. case 16934400:
  586. case 22579200:
  587. case 33868800:
  588. case 4410000:
  589. case 8820000:
  590. case 17640000:
  591. ssm2518->constraints = &ssm2518_constraints_2822000;
  592. break;
  593. case 3072000:
  594. case 6144000:
  595. case 38864000:
  596. case 4800000:
  597. case 9600000:
  598. case 19200000:
  599. ssm2518->constraints = &ssm2518_constraints_3072000;
  600. break;
  601. case 12288000:
  602. case 16384000:
  603. case 24576000:
  604. ssm2518->constraints = &ssm2518_constraints_12288000;
  605. break;
  606. default:
  607. return -EINVAL;
  608. }
  609. ssm2518->sysclk = freq;
  610. return regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER1,
  611. SSM2518_POWER1_NO_BCLK, val);
  612. }
  613. static const struct snd_soc_component_driver ssm2518_component_driver = {
  614. .set_bias_level = ssm2518_set_bias_level,
  615. .set_sysclk = ssm2518_set_sysclk,
  616. .controls = ssm2518_snd_controls,
  617. .num_controls = ARRAY_SIZE(ssm2518_snd_controls),
  618. .dapm_widgets = ssm2518_dapm_widgets,
  619. .num_dapm_widgets = ARRAY_SIZE(ssm2518_dapm_widgets),
  620. .dapm_routes = ssm2518_routes,
  621. .num_dapm_routes = ARRAY_SIZE(ssm2518_routes),
  622. .use_pmdown_time = 1,
  623. .endianness = 1,
  624. };
  625. static const struct regmap_config ssm2518_regmap_config = {
  626. .val_bits = 8,
  627. .reg_bits = 8,
  628. .max_register = SSM2518_REG_DRC_9,
  629. .cache_type = REGCACHE_RBTREE,
  630. .reg_defaults = ssm2518_reg_defaults,
  631. .num_reg_defaults = ARRAY_SIZE(ssm2518_reg_defaults),
  632. };
  633. static int ssm2518_i2c_probe(struct i2c_client *i2c)
  634. {
  635. struct ssm2518 *ssm2518;
  636. int ret;
  637. ssm2518 = devm_kzalloc(&i2c->dev, sizeof(*ssm2518), GFP_KERNEL);
  638. if (ssm2518 == NULL)
  639. return -ENOMEM;
  640. /* Start with enabling the chip */
  641. ssm2518->enable_gpio = devm_gpiod_get_optional(&i2c->dev, NULL,
  642. GPIOD_OUT_HIGH);
  643. ret = PTR_ERR_OR_ZERO(ssm2518->enable_gpio);
  644. if (ret)
  645. return ret;
  646. gpiod_set_consumer_name(ssm2518->enable_gpio, "SSM2518 nSD");
  647. i2c_set_clientdata(i2c, ssm2518);
  648. ssm2518->regmap = devm_regmap_init_i2c(i2c, &ssm2518_regmap_config);
  649. if (IS_ERR(ssm2518->regmap))
  650. return PTR_ERR(ssm2518->regmap);
  651. /*
  652. * The reset bit is obviously volatile, but we need to be able to cache
  653. * the other bits in the register, so we can't just mark the whole
  654. * register as volatile. Since this is the only place where we'll ever
  655. * touch the reset bit just bypass the cache for this operation.
  656. */
  657. regcache_cache_bypass(ssm2518->regmap, true);
  658. ret = regmap_write(ssm2518->regmap, SSM2518_REG_POWER1,
  659. SSM2518_POWER1_RESET);
  660. regcache_cache_bypass(ssm2518->regmap, false);
  661. if (ret)
  662. return ret;
  663. ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER2,
  664. SSM2518_POWER2_APWDN, 0x00);
  665. if (ret)
  666. return ret;
  667. ret = ssm2518_set_power(ssm2518, false);
  668. if (ret)
  669. return ret;
  670. return devm_snd_soc_register_component(&i2c->dev,
  671. &ssm2518_component_driver,
  672. &ssm2518_dai, 1);
  673. }
  674. #ifdef CONFIG_OF
  675. static const struct of_device_id ssm2518_dt_ids[] = {
  676. { .compatible = "adi,ssm2518", },
  677. { }
  678. };
  679. MODULE_DEVICE_TABLE(of, ssm2518_dt_ids);
  680. #endif
  681. static const struct i2c_device_id ssm2518_i2c_ids[] = {
  682. { "ssm2518", 0 },
  683. { }
  684. };
  685. MODULE_DEVICE_TABLE(i2c, ssm2518_i2c_ids);
  686. static struct i2c_driver ssm2518_driver = {
  687. .driver = {
  688. .name = "ssm2518",
  689. .of_match_table = of_match_ptr(ssm2518_dt_ids),
  690. },
  691. .probe_new = ssm2518_i2c_probe,
  692. .id_table = ssm2518_i2c_ids,
  693. };
  694. module_i2c_driver(ssm2518_driver);
  695. MODULE_DESCRIPTION("ASoC SSM2518 driver");
  696. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  697. MODULE_LICENSE("GPL");