pcm3168a.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PCM3168A codec driver
  4. *
  5. * Copyright (C) 2015 Imagination Technologies Ltd.
  6. *
  7. * Author: Damien Horsley <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/module.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <sound/pcm_params.h>
  17. #include <sound/soc.h>
  18. #include <sound/tlv.h>
  19. #include "pcm3168a.h"
  20. #define PCM3168A_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  21. SNDRV_PCM_FMTBIT_S24_3LE | \
  22. SNDRV_PCM_FMTBIT_S24_LE)
  23. #define PCM3168A_FMT_I2S 0x0
  24. #define PCM3168A_FMT_LEFT_J 0x1
  25. #define PCM3168A_FMT_RIGHT_J 0x2
  26. #define PCM3168A_FMT_RIGHT_J_16 0x3
  27. #define PCM3168A_FMT_DSP_A 0x4
  28. #define PCM3168A_FMT_DSP_B 0x5
  29. #define PCM3168A_FMT_I2S_TDM 0x6
  30. #define PCM3168A_FMT_LEFT_J_TDM 0x7
  31. static const char *const pcm3168a_supply_names[] = {
  32. "VDD1",
  33. "VDD2",
  34. "VCCAD1",
  35. "VCCAD2",
  36. "VCCDA1",
  37. "VCCDA2"
  38. };
  39. #define PCM3168A_DAI_DAC 0
  40. #define PCM3168A_DAI_ADC 1
  41. /* ADC/DAC side parameters */
  42. struct pcm3168a_io_params {
  43. bool provider_mode;
  44. unsigned int format;
  45. int tdm_slots;
  46. u32 tdm_mask;
  47. int slot_width;
  48. };
  49. struct pcm3168a_priv {
  50. struct regulator_bulk_data supplies[ARRAY_SIZE(pcm3168a_supply_names)];
  51. struct regmap *regmap;
  52. struct clk *scki;
  53. struct gpio_desc *gpio_rst;
  54. unsigned long sysclk;
  55. struct pcm3168a_io_params io_params[2];
  56. struct snd_soc_dai_driver dai_drv[2];
  57. };
  58. static const char *const pcm3168a_roll_off[] = { "Sharp", "Slow" };
  59. static SOC_ENUM_SINGLE_DECL(pcm3168a_d1_roll_off, PCM3168A_DAC_OP_FLT,
  60. PCM3168A_DAC_FLT_SHIFT, pcm3168a_roll_off);
  61. static SOC_ENUM_SINGLE_DECL(pcm3168a_d2_roll_off, PCM3168A_DAC_OP_FLT,
  62. PCM3168A_DAC_FLT_SHIFT + 1, pcm3168a_roll_off);
  63. static SOC_ENUM_SINGLE_DECL(pcm3168a_d3_roll_off, PCM3168A_DAC_OP_FLT,
  64. PCM3168A_DAC_FLT_SHIFT + 2, pcm3168a_roll_off);
  65. static SOC_ENUM_SINGLE_DECL(pcm3168a_d4_roll_off, PCM3168A_DAC_OP_FLT,
  66. PCM3168A_DAC_FLT_SHIFT + 3, pcm3168a_roll_off);
  67. static const char *const pcm3168a_volume_type[] = {
  68. "Individual", "Master + Individual" };
  69. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_volume_type, PCM3168A_DAC_ATT_DEMP_ZF,
  70. PCM3168A_DAC_ATMDDA_SHIFT, pcm3168a_volume_type);
  71. static const char *const pcm3168a_att_speed_mult[] = { "2048", "4096" };
  72. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_att_mult, PCM3168A_DAC_ATT_DEMP_ZF,
  73. PCM3168A_DAC_ATSPDA_SHIFT, pcm3168a_att_speed_mult);
  74. static const char *const pcm3168a_demp[] = {
  75. "Disabled", "48khz", "44.1khz", "32khz" };
  76. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_demp, PCM3168A_DAC_ATT_DEMP_ZF,
  77. PCM3168A_DAC_DEMP_SHIFT, pcm3168a_demp);
  78. static const char *const pcm3168a_zf_func[] = {
  79. "DAC 1/2/3/4 AND", "DAC 1/2/3/4 OR", "DAC 1/2/3 AND",
  80. "DAC 1/2/3 OR", "DAC 4 AND", "DAC 4 OR" };
  81. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_zf_func, PCM3168A_DAC_ATT_DEMP_ZF,
  82. PCM3168A_DAC_AZRO_SHIFT, pcm3168a_zf_func);
  83. static const char *const pcm3168a_pol[] = { "Active High", "Active Low" };
  84. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_zf_pol, PCM3168A_DAC_ATT_DEMP_ZF,
  85. PCM3168A_DAC_ATSPDA_SHIFT, pcm3168a_pol);
  86. static const char *const pcm3168a_con[] = { "Differential", "Single-Ended" };
  87. static SOC_ENUM_DOUBLE_DECL(pcm3168a_adc1_con, PCM3168A_ADC_SEAD,
  88. 0, 1, pcm3168a_con);
  89. static SOC_ENUM_DOUBLE_DECL(pcm3168a_adc2_con, PCM3168A_ADC_SEAD,
  90. 2, 3, pcm3168a_con);
  91. static SOC_ENUM_DOUBLE_DECL(pcm3168a_adc3_con, PCM3168A_ADC_SEAD,
  92. 4, 5, pcm3168a_con);
  93. static SOC_ENUM_SINGLE_DECL(pcm3168a_adc_volume_type, PCM3168A_ADC_ATT_OVF,
  94. PCM3168A_ADC_ATMDAD_SHIFT, pcm3168a_volume_type);
  95. static SOC_ENUM_SINGLE_DECL(pcm3168a_adc_att_mult, PCM3168A_ADC_ATT_OVF,
  96. PCM3168A_ADC_ATSPAD_SHIFT, pcm3168a_att_speed_mult);
  97. static SOC_ENUM_SINGLE_DECL(pcm3168a_adc_ov_pol, PCM3168A_ADC_ATT_OVF,
  98. PCM3168A_ADC_OVFP_SHIFT, pcm3168a_pol);
  99. /* -100db to 0db, register values 0-54 cause mute */
  100. static const DECLARE_TLV_DB_SCALE(pcm3168a_dac_tlv, -10050, 50, 1);
  101. /* -100db to 20db, register values 0-14 cause mute */
  102. static const DECLARE_TLV_DB_SCALE(pcm3168a_adc_tlv, -10050, 50, 1);
  103. static const struct snd_kcontrol_new pcm3168a_snd_controls[] = {
  104. SOC_SINGLE("DAC Power-Save Switch", PCM3168A_DAC_PWR_MST_FMT,
  105. PCM3168A_DAC_PSMDA_SHIFT, 1, 1),
  106. SOC_ENUM("DAC1 Digital Filter roll-off", pcm3168a_d1_roll_off),
  107. SOC_ENUM("DAC2 Digital Filter roll-off", pcm3168a_d2_roll_off),
  108. SOC_ENUM("DAC3 Digital Filter roll-off", pcm3168a_d3_roll_off),
  109. SOC_ENUM("DAC4 Digital Filter roll-off", pcm3168a_d4_roll_off),
  110. SOC_DOUBLE("DAC1 Invert Switch", PCM3168A_DAC_INV, 0, 1, 1, 0),
  111. SOC_DOUBLE("DAC2 Invert Switch", PCM3168A_DAC_INV, 2, 3, 1, 0),
  112. SOC_DOUBLE("DAC3 Invert Switch", PCM3168A_DAC_INV, 4, 5, 1, 0),
  113. SOC_DOUBLE("DAC4 Invert Switch", PCM3168A_DAC_INV, 6, 7, 1, 0),
  114. SOC_ENUM("DAC Volume Control Type", pcm3168a_dac_volume_type),
  115. SOC_ENUM("DAC Volume Rate Multiplier", pcm3168a_dac_att_mult),
  116. SOC_ENUM("DAC De-Emphasis", pcm3168a_dac_demp),
  117. SOC_ENUM("DAC Zero Flag Function", pcm3168a_dac_zf_func),
  118. SOC_ENUM("DAC Zero Flag Polarity", pcm3168a_dac_zf_pol),
  119. SOC_SINGLE_RANGE_TLV("Master Playback Volume",
  120. PCM3168A_DAC_VOL_MASTER, 0, 54, 255, 0,
  121. pcm3168a_dac_tlv),
  122. SOC_DOUBLE_R_RANGE_TLV("DAC1 Playback Volume",
  123. PCM3168A_DAC_VOL_CHAN_START,
  124. PCM3168A_DAC_VOL_CHAN_START + 1,
  125. 0, 54, 255, 0, pcm3168a_dac_tlv),
  126. SOC_DOUBLE_R_RANGE_TLV("DAC2 Playback Volume",
  127. PCM3168A_DAC_VOL_CHAN_START + 2,
  128. PCM3168A_DAC_VOL_CHAN_START + 3,
  129. 0, 54, 255, 0, pcm3168a_dac_tlv),
  130. SOC_DOUBLE_R_RANGE_TLV("DAC3 Playback Volume",
  131. PCM3168A_DAC_VOL_CHAN_START + 4,
  132. PCM3168A_DAC_VOL_CHAN_START + 5,
  133. 0, 54, 255, 0, pcm3168a_dac_tlv),
  134. SOC_DOUBLE_R_RANGE_TLV("DAC4 Playback Volume",
  135. PCM3168A_DAC_VOL_CHAN_START + 6,
  136. PCM3168A_DAC_VOL_CHAN_START + 7,
  137. 0, 54, 255, 0, pcm3168a_dac_tlv),
  138. SOC_SINGLE("ADC1 High-Pass Filter Switch", PCM3168A_ADC_PWR_HPFB,
  139. PCM3168A_ADC_BYP_SHIFT, 1, 1),
  140. SOC_SINGLE("ADC2 High-Pass Filter Switch", PCM3168A_ADC_PWR_HPFB,
  141. PCM3168A_ADC_BYP_SHIFT + 1, 1, 1),
  142. SOC_SINGLE("ADC3 High-Pass Filter Switch", PCM3168A_ADC_PWR_HPFB,
  143. PCM3168A_ADC_BYP_SHIFT + 2, 1, 1),
  144. SOC_ENUM("ADC1 Connection Type", pcm3168a_adc1_con),
  145. SOC_ENUM("ADC2 Connection Type", pcm3168a_adc2_con),
  146. SOC_ENUM("ADC3 Connection Type", pcm3168a_adc3_con),
  147. SOC_DOUBLE("ADC1 Invert Switch", PCM3168A_ADC_INV, 0, 1, 1, 0),
  148. SOC_DOUBLE("ADC2 Invert Switch", PCM3168A_ADC_INV, 2, 3, 1, 0),
  149. SOC_DOUBLE("ADC3 Invert Switch", PCM3168A_ADC_INV, 4, 5, 1, 0),
  150. SOC_DOUBLE("ADC1 Mute Switch", PCM3168A_ADC_MUTE, 0, 1, 1, 0),
  151. SOC_DOUBLE("ADC2 Mute Switch", PCM3168A_ADC_MUTE, 2, 3, 1, 0),
  152. SOC_DOUBLE("ADC3 Mute Switch", PCM3168A_ADC_MUTE, 4, 5, 1, 0),
  153. SOC_ENUM("ADC Volume Control Type", pcm3168a_adc_volume_type),
  154. SOC_ENUM("ADC Volume Rate Multiplier", pcm3168a_adc_att_mult),
  155. SOC_ENUM("ADC Overflow Flag Polarity", pcm3168a_adc_ov_pol),
  156. SOC_SINGLE_RANGE_TLV("Master Capture Volume",
  157. PCM3168A_ADC_VOL_MASTER, 0, 14, 255, 0,
  158. pcm3168a_adc_tlv),
  159. SOC_DOUBLE_R_RANGE_TLV("ADC1 Capture Volume",
  160. PCM3168A_ADC_VOL_CHAN_START,
  161. PCM3168A_ADC_VOL_CHAN_START + 1,
  162. 0, 14, 255, 0, pcm3168a_adc_tlv),
  163. SOC_DOUBLE_R_RANGE_TLV("ADC2 Capture Volume",
  164. PCM3168A_ADC_VOL_CHAN_START + 2,
  165. PCM3168A_ADC_VOL_CHAN_START + 3,
  166. 0, 14, 255, 0, pcm3168a_adc_tlv),
  167. SOC_DOUBLE_R_RANGE_TLV("ADC3 Capture Volume",
  168. PCM3168A_ADC_VOL_CHAN_START + 4,
  169. PCM3168A_ADC_VOL_CHAN_START + 5,
  170. 0, 14, 255, 0, pcm3168a_adc_tlv)
  171. };
  172. static const struct snd_soc_dapm_widget pcm3168a_dapm_widgets[] = {
  173. SND_SOC_DAPM_DAC("DAC1", "Playback", PCM3168A_DAC_OP_FLT,
  174. PCM3168A_DAC_OPEDA_SHIFT, 1),
  175. SND_SOC_DAPM_DAC("DAC2", "Playback", PCM3168A_DAC_OP_FLT,
  176. PCM3168A_DAC_OPEDA_SHIFT + 1, 1),
  177. SND_SOC_DAPM_DAC("DAC3", "Playback", PCM3168A_DAC_OP_FLT,
  178. PCM3168A_DAC_OPEDA_SHIFT + 2, 1),
  179. SND_SOC_DAPM_DAC("DAC4", "Playback", PCM3168A_DAC_OP_FLT,
  180. PCM3168A_DAC_OPEDA_SHIFT + 3, 1),
  181. SND_SOC_DAPM_OUTPUT("AOUT1L"),
  182. SND_SOC_DAPM_OUTPUT("AOUT1R"),
  183. SND_SOC_DAPM_OUTPUT("AOUT2L"),
  184. SND_SOC_DAPM_OUTPUT("AOUT2R"),
  185. SND_SOC_DAPM_OUTPUT("AOUT3L"),
  186. SND_SOC_DAPM_OUTPUT("AOUT3R"),
  187. SND_SOC_DAPM_OUTPUT("AOUT4L"),
  188. SND_SOC_DAPM_OUTPUT("AOUT4R"),
  189. SND_SOC_DAPM_ADC("ADC1", "Capture", PCM3168A_ADC_PWR_HPFB,
  190. PCM3168A_ADC_PSVAD_SHIFT, 1),
  191. SND_SOC_DAPM_ADC("ADC2", "Capture", PCM3168A_ADC_PWR_HPFB,
  192. PCM3168A_ADC_PSVAD_SHIFT + 1, 1),
  193. SND_SOC_DAPM_ADC("ADC3", "Capture", PCM3168A_ADC_PWR_HPFB,
  194. PCM3168A_ADC_PSVAD_SHIFT + 2, 1),
  195. SND_SOC_DAPM_INPUT("AIN1L"),
  196. SND_SOC_DAPM_INPUT("AIN1R"),
  197. SND_SOC_DAPM_INPUT("AIN2L"),
  198. SND_SOC_DAPM_INPUT("AIN2R"),
  199. SND_SOC_DAPM_INPUT("AIN3L"),
  200. SND_SOC_DAPM_INPUT("AIN3R")
  201. };
  202. static const struct snd_soc_dapm_route pcm3168a_dapm_routes[] = {
  203. /* Playback */
  204. { "AOUT1L", NULL, "DAC1" },
  205. { "AOUT1R", NULL, "DAC1" },
  206. { "AOUT2L", NULL, "DAC2" },
  207. { "AOUT2R", NULL, "DAC2" },
  208. { "AOUT3L", NULL, "DAC3" },
  209. { "AOUT3R", NULL, "DAC3" },
  210. { "AOUT4L", NULL, "DAC4" },
  211. { "AOUT4R", NULL, "DAC4" },
  212. /* Capture */
  213. { "ADC1", NULL, "AIN1L" },
  214. { "ADC1", NULL, "AIN1R" },
  215. { "ADC2", NULL, "AIN2L" },
  216. { "ADC2", NULL, "AIN2R" },
  217. { "ADC3", NULL, "AIN3L" },
  218. { "ADC3", NULL, "AIN3R" }
  219. };
  220. static unsigned int pcm3168a_scki_ratios[] = {
  221. 768,
  222. 512,
  223. 384,
  224. 256,
  225. 192,
  226. 128
  227. };
  228. #define PCM3168A_NUM_SCKI_RATIOS_DAC ARRAY_SIZE(pcm3168a_scki_ratios)
  229. #define PCM3168A_NUM_SCKI_RATIOS_ADC (ARRAY_SIZE(pcm3168a_scki_ratios) - 2)
  230. #define PCM3168A_MAX_SYSCLK 36864000
  231. static int pcm3168a_reset(struct pcm3168a_priv *pcm3168a)
  232. {
  233. int ret;
  234. ret = regmap_write(pcm3168a->regmap, PCM3168A_RST_SMODE, 0);
  235. if (ret)
  236. return ret;
  237. /* Internal reset is de-asserted after 3846 SCKI cycles */
  238. msleep(DIV_ROUND_UP(3846 * 1000, pcm3168a->sysclk));
  239. return regmap_write(pcm3168a->regmap, PCM3168A_RST_SMODE,
  240. PCM3168A_MRST_MASK | PCM3168A_SRST_MASK);
  241. }
  242. static int pcm3168a_mute(struct snd_soc_dai *dai, int mute, int direction)
  243. {
  244. struct snd_soc_component *component = dai->component;
  245. struct pcm3168a_priv *pcm3168a = snd_soc_component_get_drvdata(component);
  246. regmap_write(pcm3168a->regmap, PCM3168A_DAC_MUTE, mute ? 0xff : 0);
  247. return 0;
  248. }
  249. static int pcm3168a_set_dai_sysclk(struct snd_soc_dai *dai,
  250. int clk_id, unsigned int freq, int dir)
  251. {
  252. struct pcm3168a_priv *pcm3168a = snd_soc_component_get_drvdata(dai->component);
  253. int ret;
  254. /*
  255. * Some sound card sets 0 Hz as reset,
  256. * but it is impossible to set. Ignore it here
  257. */
  258. if (freq == 0)
  259. return 0;
  260. if (freq > PCM3168A_MAX_SYSCLK)
  261. return -EINVAL;
  262. ret = clk_set_rate(pcm3168a->scki, freq);
  263. if (ret)
  264. return ret;
  265. pcm3168a->sysclk = freq;
  266. return 0;
  267. }
  268. static void pcm3168a_update_fixup_pcm_stream(struct snd_soc_dai *dai)
  269. {
  270. struct snd_soc_component *component = dai->component;
  271. struct pcm3168a_priv *pcm3168a = snd_soc_component_get_drvdata(component);
  272. struct pcm3168a_io_params *io_params = &pcm3168a->io_params[dai->id];
  273. u64 formats = SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE;
  274. unsigned int channel_max = dai->id == PCM3168A_DAI_DAC ? 8 : 6;
  275. if (io_params->format == SND_SOC_DAIFMT_RIGHT_J) {
  276. /* S16_LE is only supported in RIGHT_J mode */
  277. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  278. /*
  279. * If multi DIN/DOUT is not selected, RIGHT_J can only support
  280. * two channels (no TDM support)
  281. */
  282. if (io_params->tdm_slots != 2)
  283. channel_max = 2;
  284. }
  285. if (dai->id == PCM3168A_DAI_DAC) {
  286. dai->driver->playback.channels_max = channel_max;
  287. dai->driver->playback.formats = formats;
  288. } else {
  289. dai->driver->capture.channels_max = channel_max;
  290. dai->driver->capture.formats = formats;
  291. }
  292. }
  293. static int pcm3168a_set_dai_fmt(struct snd_soc_dai *dai, unsigned int format)
  294. {
  295. struct snd_soc_component *component = dai->component;
  296. struct pcm3168a_priv *pcm3168a = snd_soc_component_get_drvdata(component);
  297. struct pcm3168a_io_params *io_params = &pcm3168a->io_params[dai->id];
  298. bool provider_mode;
  299. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  300. case SND_SOC_DAIFMT_LEFT_J:
  301. case SND_SOC_DAIFMT_I2S:
  302. case SND_SOC_DAIFMT_RIGHT_J:
  303. case SND_SOC_DAIFMT_DSP_A:
  304. case SND_SOC_DAIFMT_DSP_B:
  305. break;
  306. default:
  307. dev_err(component->dev, "unsupported dai format\n");
  308. return -EINVAL;
  309. }
  310. switch (format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
  311. case SND_SOC_DAIFMT_CBC_CFC:
  312. provider_mode = false;
  313. break;
  314. case SND_SOC_DAIFMT_CBP_CFP:
  315. provider_mode = true;
  316. break;
  317. default:
  318. dev_err(component->dev, "unsupported provider mode\n");
  319. return -EINVAL;
  320. }
  321. switch (format & SND_SOC_DAIFMT_INV_MASK) {
  322. case SND_SOC_DAIFMT_NB_NF:
  323. break;
  324. default:
  325. return -EINVAL;
  326. }
  327. io_params->provider_mode = provider_mode;
  328. io_params->format = format & SND_SOC_DAIFMT_FORMAT_MASK;
  329. pcm3168a_update_fixup_pcm_stream(dai);
  330. return 0;
  331. }
  332. static int pcm3168a_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
  333. unsigned int rx_mask, int slots,
  334. int slot_width)
  335. {
  336. struct snd_soc_component *component = dai->component;
  337. struct pcm3168a_priv *pcm3168a = snd_soc_component_get_drvdata(component);
  338. struct pcm3168a_io_params *io_params = &pcm3168a->io_params[dai->id];
  339. if (tx_mask >= (1<<slots) || rx_mask >= (1<<slots)) {
  340. dev_err(component->dev,
  341. "Bad tdm mask tx: 0x%08x rx: 0x%08x slots %d\n",
  342. tx_mask, rx_mask, slots);
  343. return -EINVAL;
  344. }
  345. if (slot_width &&
  346. (slot_width != 16 && slot_width != 24 && slot_width != 32 )) {
  347. dev_err(component->dev, "Unsupported slot_width %d\n",
  348. slot_width);
  349. return -EINVAL;
  350. }
  351. io_params->tdm_slots = slots;
  352. io_params->slot_width = slot_width;
  353. /* Ignore the not relevant mask for the DAI/direction */
  354. if (dai->id == PCM3168A_DAI_DAC)
  355. io_params->tdm_mask = tx_mask;
  356. else
  357. io_params->tdm_mask = rx_mask;
  358. pcm3168a_update_fixup_pcm_stream(dai);
  359. return 0;
  360. }
  361. static int pcm3168a_hw_params(struct snd_pcm_substream *substream,
  362. struct snd_pcm_hw_params *params,
  363. struct snd_soc_dai *dai)
  364. {
  365. struct snd_soc_component *component = dai->component;
  366. struct pcm3168a_priv *pcm3168a = snd_soc_component_get_drvdata(component);
  367. struct pcm3168a_io_params *io_params = &pcm3168a->io_params[dai->id];
  368. bool provider_mode, tdm_mode;
  369. unsigned int format;
  370. unsigned int reg, mask, ms, ms_shift, fmt, fmt_shift, ratio, tdm_slots;
  371. int i, num_scki_ratios, slot_width;
  372. if (dai->id == PCM3168A_DAI_DAC) {
  373. num_scki_ratios = PCM3168A_NUM_SCKI_RATIOS_DAC;
  374. reg = PCM3168A_DAC_PWR_MST_FMT;
  375. mask = PCM3168A_DAC_MSDA_MASK | PCM3168A_DAC_FMT_MASK;
  376. ms_shift = PCM3168A_DAC_MSDA_SHIFT;
  377. fmt_shift = PCM3168A_DAC_FMT_SHIFT;
  378. } else {
  379. num_scki_ratios = PCM3168A_NUM_SCKI_RATIOS_ADC;
  380. reg = PCM3168A_ADC_MST_FMT;
  381. mask = PCM3168A_ADC_MSAD_MASK | PCM3168A_ADC_FMTAD_MASK;
  382. ms_shift = PCM3168A_ADC_MSAD_SHIFT;
  383. fmt_shift = PCM3168A_ADC_FMTAD_SHIFT;
  384. }
  385. provider_mode = io_params->provider_mode;
  386. if (provider_mode) {
  387. ratio = pcm3168a->sysclk / params_rate(params);
  388. for (i = 0; i < num_scki_ratios; i++) {
  389. if (pcm3168a_scki_ratios[i] == ratio)
  390. break;
  391. }
  392. if (i == num_scki_ratios) {
  393. dev_err(component->dev, "unsupported sysclk ratio\n");
  394. return -EINVAL;
  395. }
  396. ms = (i + 1);
  397. } else {
  398. ms = 0;
  399. }
  400. format = io_params->format;
  401. if (io_params->slot_width)
  402. slot_width = io_params->slot_width;
  403. else
  404. slot_width = params_width(params);
  405. switch (slot_width) {
  406. case 16:
  407. if (provider_mode || (format != SND_SOC_DAIFMT_RIGHT_J)) {
  408. dev_err(component->dev, "16-bit slots are supported only for consumer mode using right justified\n");
  409. return -EINVAL;
  410. }
  411. break;
  412. case 24:
  413. if (provider_mode || (format == SND_SOC_DAIFMT_DSP_A) ||
  414. (format == SND_SOC_DAIFMT_DSP_B)) {
  415. dev_err(component->dev, "24-bit slots not supported in provider mode, or consumer mode using DSP\n");
  416. return -EINVAL;
  417. }
  418. break;
  419. case 32:
  420. break;
  421. default:
  422. dev_err(component->dev, "unsupported frame size: %d\n", slot_width);
  423. return -EINVAL;
  424. }
  425. if (io_params->tdm_slots)
  426. tdm_slots = io_params->tdm_slots;
  427. else
  428. tdm_slots = params_channels(params);
  429. /*
  430. * Switch the codec to TDM mode when more than 2 TDM slots are needed
  431. * for the stream.
  432. * If pcm3168a->tdm_slots is not set or set to more than 2 (8/6 usually)
  433. * then DIN1/DOUT1 is used in TDM mode.
  434. * If pcm3168a->tdm_slots is set to 2 then DIN1/2/3/4 and DOUT1/2/3 is
  435. * used in normal mode, no need to switch to TDM modes.
  436. */
  437. tdm_mode = (tdm_slots > 2);
  438. if (tdm_mode) {
  439. switch (format) {
  440. case SND_SOC_DAIFMT_I2S:
  441. case SND_SOC_DAIFMT_DSP_A:
  442. case SND_SOC_DAIFMT_LEFT_J:
  443. case SND_SOC_DAIFMT_DSP_B:
  444. break;
  445. default:
  446. dev_err(component->dev,
  447. "TDM is supported under DSP/I2S/Left_J only\n");
  448. return -EINVAL;
  449. }
  450. }
  451. switch (format) {
  452. case SND_SOC_DAIFMT_I2S:
  453. fmt = tdm_mode ? PCM3168A_FMT_I2S_TDM : PCM3168A_FMT_I2S;
  454. break;
  455. case SND_SOC_DAIFMT_LEFT_J:
  456. fmt = tdm_mode ? PCM3168A_FMT_LEFT_J_TDM : PCM3168A_FMT_LEFT_J;
  457. break;
  458. case SND_SOC_DAIFMT_RIGHT_J:
  459. fmt = (slot_width == 16) ? PCM3168A_FMT_RIGHT_J_16 :
  460. PCM3168A_FMT_RIGHT_J;
  461. break;
  462. case SND_SOC_DAIFMT_DSP_A:
  463. fmt = tdm_mode ? PCM3168A_FMT_I2S_TDM : PCM3168A_FMT_DSP_A;
  464. break;
  465. case SND_SOC_DAIFMT_DSP_B:
  466. fmt = tdm_mode ? PCM3168A_FMT_LEFT_J_TDM : PCM3168A_FMT_DSP_B;
  467. break;
  468. default:
  469. return -EINVAL;
  470. }
  471. regmap_update_bits(pcm3168a->regmap, reg, mask,
  472. (ms << ms_shift) | (fmt << fmt_shift));
  473. return 0;
  474. }
  475. static u64 pcm3168a_dai_formats[] = {
  476. /*
  477. * Select below from Sound Card, not here
  478. * SND_SOC_DAIFMT_CBC_CFC
  479. * SND_SOC_DAIFMT_CBP_CFP
  480. */
  481. /*
  482. * First Priority
  483. */
  484. SND_SOC_POSSIBLE_DAIFMT_I2S |
  485. SND_SOC_POSSIBLE_DAIFMT_LEFT_J,
  486. /*
  487. * Second Priority
  488. *
  489. * These have picky limitation.
  490. * see
  491. * pcm3168a_hw_params()
  492. */
  493. SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
  494. SND_SOC_POSSIBLE_DAIFMT_DSP_A |
  495. SND_SOC_POSSIBLE_DAIFMT_DSP_B,
  496. };
  497. static const struct snd_soc_dai_ops pcm3168a_dai_ops = {
  498. .set_fmt = pcm3168a_set_dai_fmt,
  499. .set_sysclk = pcm3168a_set_dai_sysclk,
  500. .hw_params = pcm3168a_hw_params,
  501. .mute_stream = pcm3168a_mute,
  502. .set_tdm_slot = pcm3168a_set_tdm_slot,
  503. .no_capture_mute = 1,
  504. .auto_selectable_formats = pcm3168a_dai_formats,
  505. .num_auto_selectable_formats = ARRAY_SIZE(pcm3168a_dai_formats),
  506. };
  507. static struct snd_soc_dai_driver pcm3168a_dais[] = {
  508. {
  509. .name = "pcm3168a-dac",
  510. .id = PCM3168A_DAI_DAC,
  511. .playback = {
  512. .stream_name = "Playback",
  513. .channels_min = 1,
  514. .channels_max = 8,
  515. .rates = SNDRV_PCM_RATE_8000_192000,
  516. .formats = PCM3168A_FORMATS
  517. },
  518. .ops = &pcm3168a_dai_ops
  519. },
  520. {
  521. .name = "pcm3168a-adc",
  522. .id = PCM3168A_DAI_ADC,
  523. .capture = {
  524. .stream_name = "Capture",
  525. .channels_min = 1,
  526. .channels_max = 6,
  527. .rates = SNDRV_PCM_RATE_8000_96000,
  528. .formats = PCM3168A_FORMATS
  529. },
  530. .ops = &pcm3168a_dai_ops
  531. },
  532. };
  533. static const struct reg_default pcm3168a_reg_default[] = {
  534. { PCM3168A_RST_SMODE, PCM3168A_MRST_MASK | PCM3168A_SRST_MASK },
  535. { PCM3168A_DAC_PWR_MST_FMT, 0x00 },
  536. { PCM3168A_DAC_OP_FLT, 0x00 },
  537. { PCM3168A_DAC_INV, 0x00 },
  538. { PCM3168A_DAC_MUTE, 0x00 },
  539. { PCM3168A_DAC_ZERO, 0x00 },
  540. { PCM3168A_DAC_ATT_DEMP_ZF, 0x00 },
  541. { PCM3168A_DAC_VOL_MASTER, 0xff },
  542. { PCM3168A_DAC_VOL_CHAN_START, 0xff },
  543. { PCM3168A_DAC_VOL_CHAN_START + 1, 0xff },
  544. { PCM3168A_DAC_VOL_CHAN_START + 2, 0xff },
  545. { PCM3168A_DAC_VOL_CHAN_START + 3, 0xff },
  546. { PCM3168A_DAC_VOL_CHAN_START + 4, 0xff },
  547. { PCM3168A_DAC_VOL_CHAN_START + 5, 0xff },
  548. { PCM3168A_DAC_VOL_CHAN_START + 6, 0xff },
  549. { PCM3168A_DAC_VOL_CHAN_START + 7, 0xff },
  550. { PCM3168A_ADC_SMODE, 0x00 },
  551. { PCM3168A_ADC_MST_FMT, 0x00 },
  552. { PCM3168A_ADC_PWR_HPFB, 0x00 },
  553. { PCM3168A_ADC_SEAD, 0x00 },
  554. { PCM3168A_ADC_INV, 0x00 },
  555. { PCM3168A_ADC_MUTE, 0x00 },
  556. { PCM3168A_ADC_OV, 0x00 },
  557. { PCM3168A_ADC_ATT_OVF, 0x00 },
  558. { PCM3168A_ADC_VOL_MASTER, 0xd3 },
  559. { PCM3168A_ADC_VOL_CHAN_START, 0xd3 },
  560. { PCM3168A_ADC_VOL_CHAN_START + 1, 0xd3 },
  561. { PCM3168A_ADC_VOL_CHAN_START + 2, 0xd3 },
  562. { PCM3168A_ADC_VOL_CHAN_START + 3, 0xd3 },
  563. { PCM3168A_ADC_VOL_CHAN_START + 4, 0xd3 },
  564. { PCM3168A_ADC_VOL_CHAN_START + 5, 0xd3 }
  565. };
  566. static bool pcm3168a_readable_register(struct device *dev, unsigned int reg)
  567. {
  568. if (reg >= PCM3168A_RST_SMODE)
  569. return true;
  570. else
  571. return false;
  572. }
  573. static bool pcm3168a_volatile_register(struct device *dev, unsigned int reg)
  574. {
  575. switch (reg) {
  576. case PCM3168A_RST_SMODE:
  577. case PCM3168A_DAC_ZERO:
  578. case PCM3168A_ADC_OV:
  579. return true;
  580. default:
  581. return false;
  582. }
  583. }
  584. static bool pcm3168a_writeable_register(struct device *dev, unsigned int reg)
  585. {
  586. if (reg < PCM3168A_RST_SMODE)
  587. return false;
  588. switch (reg) {
  589. case PCM3168A_DAC_ZERO:
  590. case PCM3168A_ADC_OV:
  591. return false;
  592. default:
  593. return true;
  594. }
  595. }
  596. const struct regmap_config pcm3168a_regmap = {
  597. .reg_bits = 8,
  598. .val_bits = 8,
  599. .max_register = PCM3168A_ADC_VOL_CHAN_START + 5,
  600. .reg_defaults = pcm3168a_reg_default,
  601. .num_reg_defaults = ARRAY_SIZE(pcm3168a_reg_default),
  602. .readable_reg = pcm3168a_readable_register,
  603. .volatile_reg = pcm3168a_volatile_register,
  604. .writeable_reg = pcm3168a_writeable_register,
  605. .cache_type = REGCACHE_FLAT
  606. };
  607. EXPORT_SYMBOL_GPL(pcm3168a_regmap);
  608. static const struct snd_soc_component_driver pcm3168a_driver = {
  609. .controls = pcm3168a_snd_controls,
  610. .num_controls = ARRAY_SIZE(pcm3168a_snd_controls),
  611. .dapm_widgets = pcm3168a_dapm_widgets,
  612. .num_dapm_widgets = ARRAY_SIZE(pcm3168a_dapm_widgets),
  613. .dapm_routes = pcm3168a_dapm_routes,
  614. .num_dapm_routes = ARRAY_SIZE(pcm3168a_dapm_routes),
  615. .use_pmdown_time = 1,
  616. .endianness = 1,
  617. };
  618. int pcm3168a_probe(struct device *dev, struct regmap *regmap)
  619. {
  620. struct pcm3168a_priv *pcm3168a;
  621. int ret, i;
  622. pcm3168a = devm_kzalloc(dev, sizeof(*pcm3168a), GFP_KERNEL);
  623. if (pcm3168a == NULL)
  624. return -ENOMEM;
  625. dev_set_drvdata(dev, pcm3168a);
  626. /*
  627. * Request the reset (connected to RST pin) gpio line as non exclusive
  628. * as the same reset line might be connected to multiple pcm3168a codec
  629. *
  630. * The RST is low active, we want the GPIO line to be high initially, so
  631. * request the initial level to LOW which in practice means DEASSERTED:
  632. * The deasserted level of GPIO_ACTIVE_LOW is HIGH.
  633. */
  634. pcm3168a->gpio_rst = devm_gpiod_get_optional(dev, "reset",
  635. GPIOD_OUT_LOW |
  636. GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  637. if (IS_ERR(pcm3168a->gpio_rst))
  638. return dev_err_probe(dev, PTR_ERR(pcm3168a->gpio_rst),
  639. "failed to acquire RST gpio\n");
  640. pcm3168a->scki = devm_clk_get(dev, "scki");
  641. if (IS_ERR(pcm3168a->scki))
  642. return dev_err_probe(dev, PTR_ERR(pcm3168a->scki),
  643. "failed to acquire clock 'scki'\n");
  644. ret = clk_prepare_enable(pcm3168a->scki);
  645. if (ret) {
  646. dev_err(dev, "Failed to enable mclk: %d\n", ret);
  647. return ret;
  648. }
  649. pcm3168a->sysclk = clk_get_rate(pcm3168a->scki);
  650. for (i = 0; i < ARRAY_SIZE(pcm3168a->supplies); i++)
  651. pcm3168a->supplies[i].supply = pcm3168a_supply_names[i];
  652. ret = devm_regulator_bulk_get(dev,
  653. ARRAY_SIZE(pcm3168a->supplies), pcm3168a->supplies);
  654. if (ret) {
  655. dev_err_probe(dev, ret, "failed to request supplies\n");
  656. goto err_clk;
  657. }
  658. ret = regulator_bulk_enable(ARRAY_SIZE(pcm3168a->supplies),
  659. pcm3168a->supplies);
  660. if (ret) {
  661. dev_err(dev, "failed to enable supplies: %d\n", ret);
  662. goto err_clk;
  663. }
  664. pcm3168a->regmap = regmap;
  665. if (IS_ERR(pcm3168a->regmap)) {
  666. ret = PTR_ERR(pcm3168a->regmap);
  667. dev_err(dev, "failed to allocate regmap: %d\n", ret);
  668. goto err_regulator;
  669. }
  670. if (pcm3168a->gpio_rst) {
  671. /*
  672. * The device is taken out from reset via GPIO line, wait for
  673. * 3846 SCKI clock cycles for the internal reset de-assertion
  674. */
  675. msleep(DIV_ROUND_UP(3846 * 1000, pcm3168a->sysclk));
  676. } else {
  677. ret = pcm3168a_reset(pcm3168a);
  678. if (ret) {
  679. dev_err(dev, "Failed to reset device: %d\n", ret);
  680. goto err_regulator;
  681. }
  682. }
  683. pm_runtime_set_active(dev);
  684. pm_runtime_enable(dev);
  685. pm_runtime_idle(dev);
  686. memcpy(pcm3168a->dai_drv, pcm3168a_dais, sizeof(pcm3168a->dai_drv));
  687. ret = devm_snd_soc_register_component(dev, &pcm3168a_driver,
  688. pcm3168a->dai_drv,
  689. ARRAY_SIZE(pcm3168a->dai_drv));
  690. if (ret) {
  691. dev_err(dev, "failed to register component: %d\n", ret);
  692. goto err_regulator;
  693. }
  694. return 0;
  695. err_regulator:
  696. regulator_bulk_disable(ARRAY_SIZE(pcm3168a->supplies),
  697. pcm3168a->supplies);
  698. err_clk:
  699. clk_disable_unprepare(pcm3168a->scki);
  700. return ret;
  701. }
  702. EXPORT_SYMBOL_GPL(pcm3168a_probe);
  703. static void pcm3168a_disable(struct device *dev)
  704. {
  705. struct pcm3168a_priv *pcm3168a = dev_get_drvdata(dev);
  706. regulator_bulk_disable(ARRAY_SIZE(pcm3168a->supplies),
  707. pcm3168a->supplies);
  708. clk_disable_unprepare(pcm3168a->scki);
  709. }
  710. void pcm3168a_remove(struct device *dev)
  711. {
  712. struct pcm3168a_priv *pcm3168a = dev_get_drvdata(dev);
  713. /*
  714. * The RST is low active, we want the GPIO line to be low when the
  715. * driver is removed, so set level to 1 which in practice means
  716. * ASSERTED:
  717. * The asserted level of GPIO_ACTIVE_LOW is LOW.
  718. */
  719. gpiod_set_value_cansleep(pcm3168a->gpio_rst, 1);
  720. pm_runtime_disable(dev);
  721. #ifndef CONFIG_PM
  722. pcm3168a_disable(dev);
  723. #endif
  724. }
  725. EXPORT_SYMBOL_GPL(pcm3168a_remove);
  726. #ifdef CONFIG_PM
  727. static int pcm3168a_rt_resume(struct device *dev)
  728. {
  729. struct pcm3168a_priv *pcm3168a = dev_get_drvdata(dev);
  730. int ret;
  731. ret = clk_prepare_enable(pcm3168a->scki);
  732. if (ret) {
  733. dev_err(dev, "Failed to enable mclk: %d\n", ret);
  734. return ret;
  735. }
  736. ret = regulator_bulk_enable(ARRAY_SIZE(pcm3168a->supplies),
  737. pcm3168a->supplies);
  738. if (ret) {
  739. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  740. goto err_clk;
  741. }
  742. ret = pcm3168a_reset(pcm3168a);
  743. if (ret) {
  744. dev_err(dev, "Failed to reset device: %d\n", ret);
  745. goto err_regulator;
  746. }
  747. regcache_cache_only(pcm3168a->regmap, false);
  748. regcache_mark_dirty(pcm3168a->regmap);
  749. ret = regcache_sync(pcm3168a->regmap);
  750. if (ret) {
  751. dev_err(dev, "Failed to sync regmap: %d\n", ret);
  752. goto err_regulator;
  753. }
  754. return 0;
  755. err_regulator:
  756. regulator_bulk_disable(ARRAY_SIZE(pcm3168a->supplies),
  757. pcm3168a->supplies);
  758. err_clk:
  759. clk_disable_unprepare(pcm3168a->scki);
  760. return ret;
  761. }
  762. static int pcm3168a_rt_suspend(struct device *dev)
  763. {
  764. struct pcm3168a_priv *pcm3168a = dev_get_drvdata(dev);
  765. regcache_cache_only(pcm3168a->regmap, true);
  766. pcm3168a_disable(dev);
  767. return 0;
  768. }
  769. #endif
  770. const struct dev_pm_ops pcm3168a_pm_ops = {
  771. SET_RUNTIME_PM_OPS(pcm3168a_rt_suspend, pcm3168a_rt_resume, NULL)
  772. };
  773. EXPORT_SYMBOL_GPL(pcm3168a_pm_ops);
  774. MODULE_DESCRIPTION("PCM3168A codec driver");
  775. MODULE_AUTHOR("Damien Horsley <[email protected]>");
  776. MODULE_LICENSE("GPL v2");