pcm3168a.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * PCM3168A codec driver
  3. *
  4. * Copyright (C) 2015 Imagination Technologies Ltd.
  5. *
  6. * Author: Damien Horsley <Damien.Horsley@imgtec.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <sound/pcm_params.h>
  18. #include <sound/soc.h>
  19. #include <sound/tlv.h>
  20. #include "pcm3168a.h"
  21. #define PCM3168A_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
  22. SNDRV_PCM_FMTBIT_S24_3LE | \
  23. SNDRV_PCM_FMTBIT_S24_LE | \
  24. SNDRV_PCM_FMTBIT_S32_LE)
  25. #define PCM3168A_FMT_I2S 0x0
  26. #define PCM3168A_FMT_LEFT_J 0x1
  27. #define PCM3168A_FMT_RIGHT_J 0x2
  28. #define PCM3168A_FMT_RIGHT_J_16 0x3
  29. #define PCM3168A_FMT_DSP_A 0x4
  30. #define PCM3168A_FMT_DSP_B 0x5
  31. #define PCM3168A_FMT_DSP_MASK 0x4
  32. #define PCM3168A_NUM_SUPPLIES 6
  33. static const char *const pcm3168a_supply_names[PCM3168A_NUM_SUPPLIES] = {
  34. "VDD1",
  35. "VDD2",
  36. "VCCAD1",
  37. "VCCAD2",
  38. "VCCDA1",
  39. "VCCDA2"
  40. };
  41. struct pcm3168a_priv {
  42. struct regulator_bulk_data supplies[PCM3168A_NUM_SUPPLIES];
  43. struct regmap *regmap;
  44. struct clk *scki;
  45. bool adc_master_mode;
  46. bool dac_master_mode;
  47. unsigned long sysclk;
  48. unsigned int adc_fmt;
  49. unsigned int dac_fmt;
  50. };
  51. static const char *const pcm3168a_roll_off[] = { "Sharp", "Slow" };
  52. static SOC_ENUM_SINGLE_DECL(pcm3168a_d1_roll_off, PCM3168A_DAC_OP_FLT,
  53. PCM3168A_DAC_FLT_SHIFT, pcm3168a_roll_off);
  54. static SOC_ENUM_SINGLE_DECL(pcm3168a_d2_roll_off, PCM3168A_DAC_OP_FLT,
  55. PCM3168A_DAC_FLT_SHIFT + 1, pcm3168a_roll_off);
  56. static SOC_ENUM_SINGLE_DECL(pcm3168a_d3_roll_off, PCM3168A_DAC_OP_FLT,
  57. PCM3168A_DAC_FLT_SHIFT + 2, pcm3168a_roll_off);
  58. static SOC_ENUM_SINGLE_DECL(pcm3168a_d4_roll_off, PCM3168A_DAC_OP_FLT,
  59. PCM3168A_DAC_FLT_SHIFT + 3, pcm3168a_roll_off);
  60. static const char *const pcm3168a_volume_type[] = {
  61. "Individual", "Master + Individual" };
  62. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_volume_type, PCM3168A_DAC_ATT_DEMP_ZF,
  63. PCM3168A_DAC_ATMDDA_SHIFT, pcm3168a_volume_type);
  64. static const char *const pcm3168a_att_speed_mult[] = { "2048", "4096" };
  65. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_att_mult, PCM3168A_DAC_ATT_DEMP_ZF,
  66. PCM3168A_DAC_ATSPDA_SHIFT, pcm3168a_att_speed_mult);
  67. static const char *const pcm3168a_demp[] = {
  68. "Disabled", "48khz", "44.1khz", "32khz" };
  69. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_demp, PCM3168A_DAC_ATT_DEMP_ZF,
  70. PCM3168A_DAC_DEMP_SHIFT, pcm3168a_demp);
  71. static const char *const pcm3168a_zf_func[] = {
  72. "DAC 1/2/3/4 AND", "DAC 1/2/3/4 OR", "DAC 1/2/3 AND",
  73. "DAC 1/2/3 OR", "DAC 4 AND", "DAC 4 OR" };
  74. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_zf_func, PCM3168A_DAC_ATT_DEMP_ZF,
  75. PCM3168A_DAC_AZRO_SHIFT, pcm3168a_zf_func);
  76. static const char *const pcm3168a_pol[] = { "Active High", "Active Low" };
  77. static SOC_ENUM_SINGLE_DECL(pcm3168a_dac_zf_pol, PCM3168A_DAC_ATT_DEMP_ZF,
  78. PCM3168A_DAC_ATSPDA_SHIFT, pcm3168a_pol);
  79. static const char *const pcm3168a_con[] = { "Differential", "Single-Ended" };
  80. static SOC_ENUM_DOUBLE_DECL(pcm3168a_adc1_con, PCM3168A_ADC_SEAD,
  81. 0, 1, pcm3168a_con);
  82. static SOC_ENUM_DOUBLE_DECL(pcm3168a_adc2_con, PCM3168A_ADC_SEAD,
  83. 2, 3, pcm3168a_con);
  84. static SOC_ENUM_DOUBLE_DECL(pcm3168a_adc3_con, PCM3168A_ADC_SEAD,
  85. 4, 5, pcm3168a_con);
  86. static SOC_ENUM_SINGLE_DECL(pcm3168a_adc_volume_type, PCM3168A_ADC_ATT_OVF,
  87. PCM3168A_ADC_ATMDAD_SHIFT, pcm3168a_volume_type);
  88. static SOC_ENUM_SINGLE_DECL(pcm3168a_adc_att_mult, PCM3168A_ADC_ATT_OVF,
  89. PCM3168A_ADC_ATSPAD_SHIFT, pcm3168a_att_speed_mult);
  90. static SOC_ENUM_SINGLE_DECL(pcm3168a_adc_ov_pol, PCM3168A_ADC_ATT_OVF,
  91. PCM3168A_ADC_OVFP_SHIFT, pcm3168a_pol);
  92. /* -100db to 0db, register values 0-54 cause mute */
  93. static const DECLARE_TLV_DB_SCALE(pcm3168a_dac_tlv, -10050, 50, 1);
  94. /* -100db to 20db, register values 0-14 cause mute */
  95. static const DECLARE_TLV_DB_SCALE(pcm3168a_adc_tlv, -10050, 50, 1);
  96. static const struct snd_kcontrol_new pcm3168a_snd_controls[] = {
  97. SOC_SINGLE("DAC Power-Save Switch", PCM3168A_DAC_PWR_MST_FMT,
  98. PCM3168A_DAC_PSMDA_SHIFT, 1, 1),
  99. SOC_ENUM("DAC1 Digital Filter roll-off", pcm3168a_d1_roll_off),
  100. SOC_ENUM("DAC2 Digital Filter roll-off", pcm3168a_d2_roll_off),
  101. SOC_ENUM("DAC3 Digital Filter roll-off", pcm3168a_d3_roll_off),
  102. SOC_ENUM("DAC4 Digital Filter roll-off", pcm3168a_d4_roll_off),
  103. SOC_DOUBLE("DAC1 Invert Switch", PCM3168A_DAC_INV, 0, 1, 1, 0),
  104. SOC_DOUBLE("DAC2 Invert Switch", PCM3168A_DAC_INV, 2, 3, 1, 0),
  105. SOC_DOUBLE("DAC3 Invert Switch", PCM3168A_DAC_INV, 4, 5, 1, 0),
  106. SOC_DOUBLE("DAC4 Invert Switch", PCM3168A_DAC_INV, 6, 7, 1, 0),
  107. SOC_DOUBLE_STS("DAC1 Zero Flag", PCM3168A_DAC_ZERO, 0, 1, 1, 0),
  108. SOC_DOUBLE_STS("DAC2 Zero Flag", PCM3168A_DAC_ZERO, 2, 3, 1, 0),
  109. SOC_DOUBLE_STS("DAC3 Zero Flag", PCM3168A_DAC_ZERO, 4, 5, 1, 0),
  110. SOC_DOUBLE_STS("DAC4 Zero Flag", PCM3168A_DAC_ZERO, 6, 7, 1, 0),
  111. SOC_ENUM("DAC Volume Control Type", pcm3168a_dac_volume_type),
  112. SOC_ENUM("DAC Volume Rate Multiplier", pcm3168a_dac_att_mult),
  113. SOC_ENUM("DAC De-Emphasis", pcm3168a_dac_demp),
  114. SOC_ENUM("DAC Zero Flag Function", pcm3168a_dac_zf_func),
  115. SOC_ENUM("DAC Zero Flag Polarity", pcm3168a_dac_zf_pol),
  116. SOC_SINGLE_RANGE_TLV("Master Playback Volume",
  117. PCM3168A_DAC_VOL_MASTER, 0, 54, 255, 0,
  118. pcm3168a_dac_tlv),
  119. SOC_DOUBLE_R_RANGE_TLV("DAC1 Playback Volume",
  120. PCM3168A_DAC_VOL_CHAN_START,
  121. PCM3168A_DAC_VOL_CHAN_START + 1,
  122. 0, 54, 255, 0, pcm3168a_dac_tlv),
  123. SOC_DOUBLE_R_RANGE_TLV("DAC2 Playback Volume",
  124. PCM3168A_DAC_VOL_CHAN_START + 2,
  125. PCM3168A_DAC_VOL_CHAN_START + 3,
  126. 0, 54, 255, 0, pcm3168a_dac_tlv),
  127. SOC_DOUBLE_R_RANGE_TLV("DAC3 Playback Volume",
  128. PCM3168A_DAC_VOL_CHAN_START + 4,
  129. PCM3168A_DAC_VOL_CHAN_START + 5,
  130. 0, 54, 255, 0, pcm3168a_dac_tlv),
  131. SOC_DOUBLE_R_RANGE_TLV("DAC4 Playback Volume",
  132. PCM3168A_DAC_VOL_CHAN_START + 6,
  133. PCM3168A_DAC_VOL_CHAN_START + 7,
  134. 0, 54, 255, 0, pcm3168a_dac_tlv),
  135. SOC_SINGLE("ADC1 High-Pass Filter Switch", PCM3168A_ADC_PWR_HPFB,
  136. PCM3168A_ADC_BYP_SHIFT, 1, 1),
  137. SOC_SINGLE("ADC2 High-Pass Filter Switch", PCM3168A_ADC_PWR_HPFB,
  138. PCM3168A_ADC_BYP_SHIFT + 1, 1, 1),
  139. SOC_SINGLE("ADC3 High-Pass Filter Switch", PCM3168A_ADC_PWR_HPFB,
  140. PCM3168A_ADC_BYP_SHIFT + 2, 1, 1),
  141. SOC_ENUM("ADC1 Connection Type", pcm3168a_adc1_con),
  142. SOC_ENUM("ADC2 Connection Type", pcm3168a_adc2_con),
  143. SOC_ENUM("ADC3 Connection Type", pcm3168a_adc3_con),
  144. SOC_DOUBLE("ADC1 Invert Switch", PCM3168A_ADC_INV, 0, 1, 1, 0),
  145. SOC_DOUBLE("ADC2 Invert Switch", PCM3168A_ADC_INV, 2, 3, 1, 0),
  146. SOC_DOUBLE("ADC3 Invert Switch", PCM3168A_ADC_INV, 4, 5, 1, 0),
  147. SOC_DOUBLE("ADC1 Mute Switch", PCM3168A_ADC_MUTE, 0, 1, 1, 0),
  148. SOC_DOUBLE("ADC2 Mute Switch", PCM3168A_ADC_MUTE, 2, 3, 1, 0),
  149. SOC_DOUBLE("ADC3 Mute Switch", PCM3168A_ADC_MUTE, 4, 5, 1, 0),
  150. SOC_DOUBLE_STS("ADC1 Overflow Flag", PCM3168A_ADC_OV, 0, 1, 1, 0),
  151. SOC_DOUBLE_STS("ADC2 Overflow Flag", PCM3168A_ADC_OV, 2, 3, 1, 0),
  152. SOC_DOUBLE_STS("ADC3 Overflow Flag", PCM3168A_ADC_OV, 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 PCM1368A_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_digital_mute(struct snd_soc_dai *dai, int mute)
  243. {
  244. struct snd_soc_codec *codec = dai->codec;
  245. struct pcm3168a_priv *pcm3168a = snd_soc_codec_get_drvdata(codec);
  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_codec_get_drvdata(dai->codec);
  253. int ret;
  254. if (freq > PCM1368A_MAX_SYSCLK)
  255. return -EINVAL;
  256. ret = clk_set_rate(pcm3168a->scki, freq);
  257. if (ret)
  258. return ret;
  259. pcm3168a->sysclk = freq;
  260. return 0;
  261. }
  262. static int pcm3168a_set_dai_fmt(struct snd_soc_dai *dai,
  263. unsigned int format, bool dac)
  264. {
  265. struct snd_soc_codec *codec = dai->codec;
  266. struct pcm3168a_priv *pcm3168a = snd_soc_codec_get_drvdata(codec);
  267. u32 fmt, reg, mask, shift;
  268. bool master_mode;
  269. switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
  270. case SND_SOC_DAIFMT_LEFT_J:
  271. fmt = PCM3168A_FMT_LEFT_J;
  272. break;
  273. case SND_SOC_DAIFMT_I2S:
  274. fmt = PCM3168A_FMT_I2S;
  275. break;
  276. case SND_SOC_DAIFMT_RIGHT_J:
  277. fmt = PCM3168A_FMT_RIGHT_J;
  278. break;
  279. case SND_SOC_DAIFMT_DSP_A:
  280. fmt = PCM3168A_FMT_DSP_A;
  281. break;
  282. case SND_SOC_DAIFMT_DSP_B:
  283. fmt = PCM3168A_FMT_DSP_B;
  284. break;
  285. default:
  286. dev_err(codec->dev, "unsupported dai format\n");
  287. return -EINVAL;
  288. }
  289. switch (format & SND_SOC_DAIFMT_MASTER_MASK) {
  290. case SND_SOC_DAIFMT_CBS_CFS:
  291. master_mode = false;
  292. break;
  293. case SND_SOC_DAIFMT_CBM_CFM:
  294. master_mode = true;
  295. break;
  296. default:
  297. dev_err(codec->dev, "unsupported master/slave mode\n");
  298. return -EINVAL;
  299. }
  300. switch (format & SND_SOC_DAIFMT_INV_MASK) {
  301. case SND_SOC_DAIFMT_NB_NF:
  302. break;
  303. default:
  304. return -EINVAL;
  305. }
  306. if (dac) {
  307. reg = PCM3168A_DAC_PWR_MST_FMT;
  308. mask = PCM3168A_DAC_FMT_MASK;
  309. shift = PCM3168A_DAC_FMT_SHIFT;
  310. pcm3168a->dac_master_mode = master_mode;
  311. pcm3168a->dac_fmt = fmt;
  312. } else {
  313. reg = PCM3168A_ADC_MST_FMT;
  314. mask = PCM3168A_ADC_FMTAD_MASK;
  315. shift = PCM3168A_ADC_FMTAD_SHIFT;
  316. pcm3168a->adc_master_mode = master_mode;
  317. pcm3168a->adc_fmt = fmt;
  318. }
  319. regmap_update_bits(pcm3168a->regmap, reg, mask, fmt << shift);
  320. return 0;
  321. }
  322. static int pcm3168a_set_dai_fmt_dac(struct snd_soc_dai *dai,
  323. unsigned int format)
  324. {
  325. return pcm3168a_set_dai_fmt(dai, format, true);
  326. }
  327. static int pcm3168a_set_dai_fmt_adc(struct snd_soc_dai *dai,
  328. unsigned int format)
  329. {
  330. return pcm3168a_set_dai_fmt(dai, format, false);
  331. }
  332. static int pcm3168a_hw_params(struct snd_pcm_substream *substream,
  333. struct snd_pcm_hw_params *params,
  334. struct snd_soc_dai *dai)
  335. {
  336. struct snd_soc_codec *codec = dai->codec;
  337. struct pcm3168a_priv *pcm3168a = snd_soc_codec_get_drvdata(codec);
  338. bool tx, master_mode;
  339. u32 val, mask, shift, reg;
  340. unsigned int rate, fmt, ratio, max_ratio;
  341. int i, min_frame_size;
  342. snd_pcm_format_t format;
  343. rate = params_rate(params);
  344. format = params_format(params);
  345. ratio = pcm3168a->sysclk / rate;
  346. tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  347. if (tx) {
  348. max_ratio = PCM3168A_NUM_SCKI_RATIOS_DAC;
  349. reg = PCM3168A_DAC_PWR_MST_FMT;
  350. mask = PCM3168A_DAC_MSDA_MASK;
  351. shift = PCM3168A_DAC_MSDA_SHIFT;
  352. master_mode = pcm3168a->dac_master_mode;
  353. fmt = pcm3168a->dac_fmt;
  354. } else {
  355. max_ratio = PCM3168A_NUM_SCKI_RATIOS_ADC;
  356. reg = PCM3168A_ADC_MST_FMT;
  357. mask = PCM3168A_ADC_MSAD_MASK;
  358. shift = PCM3168A_ADC_MSAD_SHIFT;
  359. master_mode = pcm3168a->adc_master_mode;
  360. fmt = pcm3168a->adc_fmt;
  361. }
  362. for (i = 0; i < max_ratio; i++) {
  363. if (pcm3168a_scki_ratios[i] == ratio)
  364. break;
  365. }
  366. if (i == max_ratio) {
  367. dev_err(codec->dev, "unsupported sysclk ratio\n");
  368. return -EINVAL;
  369. }
  370. min_frame_size = params_width(params) * 2;
  371. switch (min_frame_size) {
  372. case 32:
  373. if (master_mode || (fmt != PCM3168A_FMT_RIGHT_J)) {
  374. dev_err(codec->dev, "32-bit frames are supported only for slave mode using right justified\n");
  375. return -EINVAL;
  376. }
  377. fmt = PCM3168A_FMT_RIGHT_J_16;
  378. break;
  379. case 48:
  380. if (master_mode || (fmt & PCM3168A_FMT_DSP_MASK)) {
  381. dev_err(codec->dev, "48-bit frames not supported in master mode, or slave mode using DSP\n");
  382. return -EINVAL;
  383. }
  384. break;
  385. case 64:
  386. break;
  387. default:
  388. dev_err(codec->dev, "unsupported frame size: %d\n", min_frame_size);
  389. return -EINVAL;
  390. }
  391. if (master_mode)
  392. val = ((i + 1) << shift);
  393. else
  394. val = 0;
  395. regmap_update_bits(pcm3168a->regmap, reg, mask, val);
  396. if (tx) {
  397. mask = PCM3168A_DAC_FMT_MASK;
  398. shift = PCM3168A_DAC_FMT_SHIFT;
  399. } else {
  400. mask = PCM3168A_ADC_FMTAD_MASK;
  401. shift = PCM3168A_ADC_FMTAD_SHIFT;
  402. }
  403. regmap_update_bits(pcm3168a->regmap, reg, mask, fmt << shift);
  404. return 0;
  405. }
  406. static const struct snd_soc_dai_ops pcm3168a_dac_dai_ops = {
  407. .set_fmt = pcm3168a_set_dai_fmt_dac,
  408. .set_sysclk = pcm3168a_set_dai_sysclk,
  409. .hw_params = pcm3168a_hw_params,
  410. .digital_mute = pcm3168a_digital_mute
  411. };
  412. static const struct snd_soc_dai_ops pcm3168a_adc_dai_ops = {
  413. .set_fmt = pcm3168a_set_dai_fmt_adc,
  414. .set_sysclk = pcm3168a_set_dai_sysclk,
  415. .hw_params = pcm3168a_hw_params
  416. };
  417. static struct snd_soc_dai_driver pcm3168a_dais[] = {
  418. {
  419. .name = "pcm3168a-dac",
  420. .playback = {
  421. .stream_name = "Playback",
  422. .channels_min = 1,
  423. .channels_max = 8,
  424. .rates = SNDRV_PCM_RATE_8000_192000,
  425. .formats = PCM3168A_FORMATS
  426. },
  427. .ops = &pcm3168a_dac_dai_ops
  428. },
  429. {
  430. .name = "pcm3168a-adc",
  431. .capture = {
  432. .stream_name = "Capture",
  433. .channels_min = 1,
  434. .channels_max = 6,
  435. .rates = SNDRV_PCM_RATE_8000_96000,
  436. .formats = PCM3168A_FORMATS
  437. },
  438. .ops = &pcm3168a_adc_dai_ops
  439. },
  440. };
  441. static const struct reg_default pcm3168a_reg_default[] = {
  442. { PCM3168A_RST_SMODE, PCM3168A_MRST_MASK | PCM3168A_SRST_MASK },
  443. { PCM3168A_DAC_PWR_MST_FMT, 0x00 },
  444. { PCM3168A_DAC_OP_FLT, 0x00 },
  445. { PCM3168A_DAC_INV, 0x00 },
  446. { PCM3168A_DAC_MUTE, 0x00 },
  447. { PCM3168A_DAC_ZERO, 0x00 },
  448. { PCM3168A_DAC_ATT_DEMP_ZF, 0x00 },
  449. { PCM3168A_DAC_VOL_MASTER, 0xff },
  450. { PCM3168A_DAC_VOL_CHAN_START, 0xff },
  451. { PCM3168A_DAC_VOL_CHAN_START + 1, 0xff },
  452. { PCM3168A_DAC_VOL_CHAN_START + 2, 0xff },
  453. { PCM3168A_DAC_VOL_CHAN_START + 3, 0xff },
  454. { PCM3168A_DAC_VOL_CHAN_START + 4, 0xff },
  455. { PCM3168A_DAC_VOL_CHAN_START + 5, 0xff },
  456. { PCM3168A_DAC_VOL_CHAN_START + 6, 0xff },
  457. { PCM3168A_DAC_VOL_CHAN_START + 7, 0xff },
  458. { PCM3168A_ADC_SMODE, 0x00 },
  459. { PCM3168A_ADC_MST_FMT, 0x00 },
  460. { PCM3168A_ADC_PWR_HPFB, 0x00 },
  461. { PCM3168A_ADC_SEAD, 0x00 },
  462. { PCM3168A_ADC_INV, 0x00 },
  463. { PCM3168A_ADC_MUTE, 0x00 },
  464. { PCM3168A_ADC_OV, 0x00 },
  465. { PCM3168A_ADC_ATT_OVF, 0x00 },
  466. { PCM3168A_ADC_VOL_MASTER, 0xd3 },
  467. { PCM3168A_ADC_VOL_CHAN_START, 0xd3 },
  468. { PCM3168A_ADC_VOL_CHAN_START + 1, 0xd3 },
  469. { PCM3168A_ADC_VOL_CHAN_START + 2, 0xd3 },
  470. { PCM3168A_ADC_VOL_CHAN_START + 3, 0xd3 },
  471. { PCM3168A_ADC_VOL_CHAN_START + 4, 0xd3 },
  472. { PCM3168A_ADC_VOL_CHAN_START + 5, 0xd3 }
  473. };
  474. static bool pcm3168a_readable_register(struct device *dev, unsigned int reg)
  475. {
  476. if (reg >= PCM3168A_RST_SMODE)
  477. return true;
  478. else
  479. return false;
  480. }
  481. static bool pcm3168a_volatile_register(struct device *dev, unsigned int reg)
  482. {
  483. switch (reg) {
  484. case PCM3168A_DAC_ZERO:
  485. case PCM3168A_ADC_OV:
  486. return true;
  487. default:
  488. return false;
  489. }
  490. }
  491. static bool pcm3168a_writeable_register(struct device *dev, unsigned int reg)
  492. {
  493. if (reg < PCM3168A_RST_SMODE)
  494. return false;
  495. switch (reg) {
  496. case PCM3168A_DAC_ZERO:
  497. case PCM3168A_ADC_OV:
  498. return false;
  499. default:
  500. return true;
  501. }
  502. }
  503. const struct regmap_config pcm3168a_regmap = {
  504. .reg_bits = 8,
  505. .val_bits = 8,
  506. .max_register = PCM3168A_ADC_VOL_CHAN_START + 5,
  507. .reg_defaults = pcm3168a_reg_default,
  508. .num_reg_defaults = ARRAY_SIZE(pcm3168a_reg_default),
  509. .readable_reg = pcm3168a_readable_register,
  510. .volatile_reg = pcm3168a_volatile_register,
  511. .writeable_reg = pcm3168a_writeable_register,
  512. .cache_type = REGCACHE_FLAT
  513. };
  514. EXPORT_SYMBOL_GPL(pcm3168a_regmap);
  515. static const struct snd_soc_codec_driver pcm3168a_driver = {
  516. .idle_bias_off = true,
  517. .component_driver = {
  518. .controls = pcm3168a_snd_controls,
  519. .num_controls = ARRAY_SIZE(pcm3168a_snd_controls),
  520. .dapm_widgets = pcm3168a_dapm_widgets,
  521. .num_dapm_widgets = ARRAY_SIZE(pcm3168a_dapm_widgets),
  522. .dapm_routes = pcm3168a_dapm_routes,
  523. .num_dapm_routes = ARRAY_SIZE(pcm3168a_dapm_routes)
  524. },
  525. };
  526. int pcm3168a_probe(struct device *dev, struct regmap *regmap)
  527. {
  528. struct pcm3168a_priv *pcm3168a;
  529. int ret, i;
  530. pcm3168a = devm_kzalloc(dev, sizeof(*pcm3168a), GFP_KERNEL);
  531. if (pcm3168a == NULL)
  532. return -ENOMEM;
  533. dev_set_drvdata(dev, pcm3168a);
  534. pcm3168a->scki = devm_clk_get(dev, "scki");
  535. if (IS_ERR(pcm3168a->scki)) {
  536. ret = PTR_ERR(pcm3168a->scki);
  537. if (ret != -EPROBE_DEFER)
  538. dev_err(dev, "failed to acquire clock 'scki': %d\n", ret);
  539. return ret;
  540. }
  541. ret = clk_prepare_enable(pcm3168a->scki);
  542. if (ret) {
  543. dev_err(dev, "Failed to enable mclk: %d\n", ret);
  544. return ret;
  545. }
  546. pcm3168a->sysclk = clk_get_rate(pcm3168a->scki);
  547. for (i = 0; i < ARRAY_SIZE(pcm3168a->supplies); i++)
  548. pcm3168a->supplies[i].supply = pcm3168a_supply_names[i];
  549. ret = devm_regulator_bulk_get(dev,
  550. ARRAY_SIZE(pcm3168a->supplies), pcm3168a->supplies);
  551. if (ret) {
  552. if (ret != -EPROBE_DEFER)
  553. dev_err(dev, "failed to request supplies: %d\n", ret);
  554. goto err_clk;
  555. }
  556. ret = regulator_bulk_enable(ARRAY_SIZE(pcm3168a->supplies),
  557. pcm3168a->supplies);
  558. if (ret) {
  559. dev_err(dev, "failed to enable supplies: %d\n", ret);
  560. goto err_clk;
  561. }
  562. pcm3168a->regmap = regmap;
  563. if (IS_ERR(pcm3168a->regmap)) {
  564. ret = PTR_ERR(pcm3168a->regmap);
  565. dev_err(dev, "failed to allocate regmap: %d\n", ret);
  566. goto err_regulator;
  567. }
  568. ret = pcm3168a_reset(pcm3168a);
  569. if (ret) {
  570. dev_err(dev, "Failed to reset device: %d\n", ret);
  571. goto err_regulator;
  572. }
  573. pm_runtime_set_active(dev);
  574. pm_runtime_enable(dev);
  575. pm_runtime_idle(dev);
  576. ret = snd_soc_register_codec(dev, &pcm3168a_driver, pcm3168a_dais,
  577. ARRAY_SIZE(pcm3168a_dais));
  578. if (ret) {
  579. dev_err(dev, "failed to register codec: %d\n", ret);
  580. goto err_regulator;
  581. }
  582. return 0;
  583. err_regulator:
  584. regulator_bulk_disable(ARRAY_SIZE(pcm3168a->supplies),
  585. pcm3168a->supplies);
  586. err_clk:
  587. clk_disable_unprepare(pcm3168a->scki);
  588. return ret;
  589. }
  590. EXPORT_SYMBOL_GPL(pcm3168a_probe);
  591. void pcm3168a_remove(struct device *dev)
  592. {
  593. struct pcm3168a_priv *pcm3168a = dev_get_drvdata(dev);
  594. snd_soc_unregister_codec(dev);
  595. pm_runtime_disable(dev);
  596. regulator_bulk_disable(ARRAY_SIZE(pcm3168a->supplies),
  597. pcm3168a->supplies);
  598. clk_disable_unprepare(pcm3168a->scki);
  599. }
  600. EXPORT_SYMBOL_GPL(pcm3168a_remove);
  601. #ifdef CONFIG_PM
  602. static int pcm3168a_rt_resume(struct device *dev)
  603. {
  604. struct pcm3168a_priv *pcm3168a = dev_get_drvdata(dev);
  605. int ret;
  606. ret = clk_prepare_enable(pcm3168a->scki);
  607. if (ret) {
  608. dev_err(dev, "Failed to enable mclk: %d\n", ret);
  609. return ret;
  610. }
  611. ret = regulator_bulk_enable(ARRAY_SIZE(pcm3168a->supplies),
  612. pcm3168a->supplies);
  613. if (ret) {
  614. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  615. goto err_clk;
  616. }
  617. ret = pcm3168a_reset(pcm3168a);
  618. if (ret) {
  619. dev_err(dev, "Failed to reset device: %d\n", ret);
  620. goto err_regulator;
  621. }
  622. regcache_cache_only(pcm3168a->regmap, false);
  623. regcache_mark_dirty(pcm3168a->regmap);
  624. ret = regcache_sync(pcm3168a->regmap);
  625. if (ret) {
  626. dev_err(dev, "Failed to sync regmap: %d\n", ret);
  627. goto err_regulator;
  628. }
  629. return 0;
  630. err_regulator:
  631. regulator_bulk_disable(ARRAY_SIZE(pcm3168a->supplies),
  632. pcm3168a->supplies);
  633. err_clk:
  634. clk_disable_unprepare(pcm3168a->scki);
  635. return ret;
  636. }
  637. static int pcm3168a_rt_suspend(struct device *dev)
  638. {
  639. struct pcm3168a_priv *pcm3168a = dev_get_drvdata(dev);
  640. regcache_cache_only(pcm3168a->regmap, true);
  641. regulator_bulk_disable(ARRAY_SIZE(pcm3168a->supplies),
  642. pcm3168a->supplies);
  643. clk_disable_unprepare(pcm3168a->scki);
  644. return 0;
  645. }
  646. #endif
  647. const struct dev_pm_ops pcm3168a_pm_ops = {
  648. SET_RUNTIME_PM_OPS(pcm3168a_rt_suspend, pcm3168a_rt_resume, NULL)
  649. };
  650. EXPORT_SYMBOL_GPL(pcm3168a_pm_ops);
  651. MODULE_DESCRIPTION("PCM3168A codec driver");
  652. MODULE_AUTHOR("Damien Horsley <Damien.Horsley@imgtec.com>");
  653. MODULE_LICENSE("GPL v2");