pcm512x.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. /*
  2. * Driver for the PCM512x CODECs
  3. *
  4. * Author: Mark Brown <broonie@linaro.org>
  5. * Copyright 2014 Linaro Ltd
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/clk.h>
  19. #include <linux/kernel.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/regmap.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/gcd.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/tlv.h>
  28. #include "pcm512x.h"
  29. #define DIV_ROUND_DOWN_ULL(ll, d) \
  30. ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
  31. #define PCM512x_NUM_SUPPLIES 3
  32. static const char * const pcm512x_supply_names[PCM512x_NUM_SUPPLIES] = {
  33. "AVDD",
  34. "DVDD",
  35. "CPVDD",
  36. };
  37. struct pcm512x_priv {
  38. struct regmap *regmap;
  39. struct clk *sclk;
  40. struct regulator_bulk_data supplies[PCM512x_NUM_SUPPLIES];
  41. struct notifier_block supply_nb[PCM512x_NUM_SUPPLIES];
  42. int fmt;
  43. int pll_in;
  44. int pll_out;
  45. int pll_r;
  46. int pll_j;
  47. int pll_d;
  48. int pll_p;
  49. unsigned long real_pll;
  50. unsigned long overclock_pll;
  51. unsigned long overclock_dac;
  52. unsigned long overclock_dsp;
  53. };
  54. /*
  55. * We can't use the same notifier block for more than one supply and
  56. * there's no way I can see to get from a callback to the caller
  57. * except container_of().
  58. */
  59. #define PCM512x_REGULATOR_EVENT(n) \
  60. static int pcm512x_regulator_event_##n(struct notifier_block *nb, \
  61. unsigned long event, void *data) \
  62. { \
  63. struct pcm512x_priv *pcm512x = container_of(nb, struct pcm512x_priv, \
  64. supply_nb[n]); \
  65. if (event & REGULATOR_EVENT_DISABLE) { \
  66. regcache_mark_dirty(pcm512x->regmap); \
  67. regcache_cache_only(pcm512x->regmap, true); \
  68. } \
  69. return 0; \
  70. }
  71. PCM512x_REGULATOR_EVENT(0)
  72. PCM512x_REGULATOR_EVENT(1)
  73. PCM512x_REGULATOR_EVENT(2)
  74. static const struct reg_default pcm512x_reg_defaults[] = {
  75. { PCM512x_RESET, 0x00 },
  76. { PCM512x_POWER, 0x00 },
  77. { PCM512x_MUTE, 0x00 },
  78. { PCM512x_DSP, 0x00 },
  79. { PCM512x_PLL_REF, 0x00 },
  80. { PCM512x_DAC_REF, 0x00 },
  81. { PCM512x_DAC_ROUTING, 0x11 },
  82. { PCM512x_DSP_PROGRAM, 0x01 },
  83. { PCM512x_CLKDET, 0x00 },
  84. { PCM512x_AUTO_MUTE, 0x00 },
  85. { PCM512x_ERROR_DETECT, 0x00 },
  86. { PCM512x_DIGITAL_VOLUME_1, 0x00 },
  87. { PCM512x_DIGITAL_VOLUME_2, 0x30 },
  88. { PCM512x_DIGITAL_VOLUME_3, 0x30 },
  89. { PCM512x_DIGITAL_MUTE_1, 0x22 },
  90. { PCM512x_DIGITAL_MUTE_2, 0x00 },
  91. { PCM512x_DIGITAL_MUTE_3, 0x07 },
  92. { PCM512x_OUTPUT_AMPLITUDE, 0x00 },
  93. { PCM512x_ANALOG_GAIN_CTRL, 0x00 },
  94. { PCM512x_UNDERVOLTAGE_PROT, 0x00 },
  95. { PCM512x_ANALOG_MUTE_CTRL, 0x00 },
  96. { PCM512x_ANALOG_GAIN_BOOST, 0x00 },
  97. { PCM512x_VCOM_CTRL_1, 0x00 },
  98. { PCM512x_VCOM_CTRL_2, 0x01 },
  99. { PCM512x_BCLK_LRCLK_CFG, 0x00 },
  100. { PCM512x_MASTER_MODE, 0x7c },
  101. { PCM512x_GPIO_DACIN, 0x00 },
  102. { PCM512x_GPIO_PLLIN, 0x00 },
  103. { PCM512x_SYNCHRONIZE, 0x10 },
  104. { PCM512x_PLL_COEFF_0, 0x00 },
  105. { PCM512x_PLL_COEFF_1, 0x00 },
  106. { PCM512x_PLL_COEFF_2, 0x00 },
  107. { PCM512x_PLL_COEFF_3, 0x00 },
  108. { PCM512x_PLL_COEFF_4, 0x00 },
  109. { PCM512x_DSP_CLKDIV, 0x00 },
  110. { PCM512x_DAC_CLKDIV, 0x00 },
  111. { PCM512x_NCP_CLKDIV, 0x00 },
  112. { PCM512x_OSR_CLKDIV, 0x00 },
  113. { PCM512x_MASTER_CLKDIV_1, 0x00 },
  114. { PCM512x_MASTER_CLKDIV_2, 0x00 },
  115. { PCM512x_FS_SPEED_MODE, 0x00 },
  116. { PCM512x_IDAC_1, 0x01 },
  117. { PCM512x_IDAC_2, 0x00 },
  118. };
  119. static bool pcm512x_readable(struct device *dev, unsigned int reg)
  120. {
  121. switch (reg) {
  122. case PCM512x_RESET:
  123. case PCM512x_POWER:
  124. case PCM512x_MUTE:
  125. case PCM512x_PLL_EN:
  126. case PCM512x_SPI_MISO_FUNCTION:
  127. case PCM512x_DSP:
  128. case PCM512x_GPIO_EN:
  129. case PCM512x_BCLK_LRCLK_CFG:
  130. case PCM512x_DSP_GPIO_INPUT:
  131. case PCM512x_MASTER_MODE:
  132. case PCM512x_PLL_REF:
  133. case PCM512x_DAC_REF:
  134. case PCM512x_GPIO_DACIN:
  135. case PCM512x_GPIO_PLLIN:
  136. case PCM512x_SYNCHRONIZE:
  137. case PCM512x_PLL_COEFF_0:
  138. case PCM512x_PLL_COEFF_1:
  139. case PCM512x_PLL_COEFF_2:
  140. case PCM512x_PLL_COEFF_3:
  141. case PCM512x_PLL_COEFF_4:
  142. case PCM512x_DSP_CLKDIV:
  143. case PCM512x_DAC_CLKDIV:
  144. case PCM512x_NCP_CLKDIV:
  145. case PCM512x_OSR_CLKDIV:
  146. case PCM512x_MASTER_CLKDIV_1:
  147. case PCM512x_MASTER_CLKDIV_2:
  148. case PCM512x_FS_SPEED_MODE:
  149. case PCM512x_IDAC_1:
  150. case PCM512x_IDAC_2:
  151. case PCM512x_ERROR_DETECT:
  152. case PCM512x_I2S_1:
  153. case PCM512x_I2S_2:
  154. case PCM512x_DAC_ROUTING:
  155. case PCM512x_DSP_PROGRAM:
  156. case PCM512x_CLKDET:
  157. case PCM512x_AUTO_MUTE:
  158. case PCM512x_DIGITAL_VOLUME_1:
  159. case PCM512x_DIGITAL_VOLUME_2:
  160. case PCM512x_DIGITAL_VOLUME_3:
  161. case PCM512x_DIGITAL_MUTE_1:
  162. case PCM512x_DIGITAL_MUTE_2:
  163. case PCM512x_DIGITAL_MUTE_3:
  164. case PCM512x_GPIO_OUTPUT_1:
  165. case PCM512x_GPIO_OUTPUT_2:
  166. case PCM512x_GPIO_OUTPUT_3:
  167. case PCM512x_GPIO_OUTPUT_4:
  168. case PCM512x_GPIO_OUTPUT_5:
  169. case PCM512x_GPIO_OUTPUT_6:
  170. case PCM512x_GPIO_CONTROL_1:
  171. case PCM512x_GPIO_CONTROL_2:
  172. case PCM512x_OVERFLOW:
  173. case PCM512x_RATE_DET_1:
  174. case PCM512x_RATE_DET_2:
  175. case PCM512x_RATE_DET_3:
  176. case PCM512x_RATE_DET_4:
  177. case PCM512x_CLOCK_STATUS:
  178. case PCM512x_ANALOG_MUTE_DET:
  179. case PCM512x_GPIN:
  180. case PCM512x_DIGITAL_MUTE_DET:
  181. case PCM512x_OUTPUT_AMPLITUDE:
  182. case PCM512x_ANALOG_GAIN_CTRL:
  183. case PCM512x_UNDERVOLTAGE_PROT:
  184. case PCM512x_ANALOG_MUTE_CTRL:
  185. case PCM512x_ANALOG_GAIN_BOOST:
  186. case PCM512x_VCOM_CTRL_1:
  187. case PCM512x_VCOM_CTRL_2:
  188. case PCM512x_CRAM_CTRL:
  189. case PCM512x_FLEX_A:
  190. case PCM512x_FLEX_B:
  191. return true;
  192. default:
  193. /* There are 256 raw register addresses */
  194. return reg < 0xff;
  195. }
  196. }
  197. static bool pcm512x_volatile(struct device *dev, unsigned int reg)
  198. {
  199. switch (reg) {
  200. case PCM512x_PLL_EN:
  201. case PCM512x_OVERFLOW:
  202. case PCM512x_RATE_DET_1:
  203. case PCM512x_RATE_DET_2:
  204. case PCM512x_RATE_DET_3:
  205. case PCM512x_RATE_DET_4:
  206. case PCM512x_CLOCK_STATUS:
  207. case PCM512x_ANALOG_MUTE_DET:
  208. case PCM512x_GPIN:
  209. case PCM512x_DIGITAL_MUTE_DET:
  210. case PCM512x_CRAM_CTRL:
  211. return true;
  212. default:
  213. /* There are 256 raw register addresses */
  214. return reg < 0xff;
  215. }
  216. }
  217. static int pcm512x_overclock_pll_get(struct snd_kcontrol *kcontrol,
  218. struct snd_ctl_elem_value *ucontrol)
  219. {
  220. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  221. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  222. ucontrol->value.integer.value[0] = pcm512x->overclock_pll;
  223. return 0;
  224. }
  225. static int pcm512x_overclock_pll_put(struct snd_kcontrol *kcontrol,
  226. struct snd_ctl_elem_value *ucontrol)
  227. {
  228. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  229. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  230. switch (snd_soc_codec_get_bias_level(codec)) {
  231. case SND_SOC_BIAS_OFF:
  232. case SND_SOC_BIAS_STANDBY:
  233. break;
  234. default:
  235. return -EBUSY;
  236. }
  237. pcm512x->overclock_pll = ucontrol->value.integer.value[0];
  238. return 0;
  239. }
  240. static int pcm512x_overclock_dsp_get(struct snd_kcontrol *kcontrol,
  241. struct snd_ctl_elem_value *ucontrol)
  242. {
  243. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  244. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  245. ucontrol->value.integer.value[0] = pcm512x->overclock_dsp;
  246. return 0;
  247. }
  248. static int pcm512x_overclock_dsp_put(struct snd_kcontrol *kcontrol,
  249. struct snd_ctl_elem_value *ucontrol)
  250. {
  251. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  252. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  253. switch (snd_soc_codec_get_bias_level(codec)) {
  254. case SND_SOC_BIAS_OFF:
  255. case SND_SOC_BIAS_STANDBY:
  256. break;
  257. default:
  258. return -EBUSY;
  259. }
  260. pcm512x->overclock_dsp = ucontrol->value.integer.value[0];
  261. return 0;
  262. }
  263. static int pcm512x_overclock_dac_get(struct snd_kcontrol *kcontrol,
  264. struct snd_ctl_elem_value *ucontrol)
  265. {
  266. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  267. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  268. ucontrol->value.integer.value[0] = pcm512x->overclock_dac;
  269. return 0;
  270. }
  271. static int pcm512x_overclock_dac_put(struct snd_kcontrol *kcontrol,
  272. struct snd_ctl_elem_value *ucontrol)
  273. {
  274. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  275. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  276. switch (snd_soc_codec_get_bias_level(codec)) {
  277. case SND_SOC_BIAS_OFF:
  278. case SND_SOC_BIAS_STANDBY:
  279. break;
  280. default:
  281. return -EBUSY;
  282. }
  283. pcm512x->overclock_dac = ucontrol->value.integer.value[0];
  284. return 0;
  285. }
  286. static const DECLARE_TLV_DB_SCALE(digital_tlv, -10350, 50, 1);
  287. static const DECLARE_TLV_DB_SCALE(analog_tlv, -600, 600, 0);
  288. static const DECLARE_TLV_DB_SCALE(boost_tlv, 0, 80, 0);
  289. static const char * const pcm512x_dsp_program_texts[] = {
  290. "FIR interpolation with de-emphasis",
  291. "Low latency IIR with de-emphasis",
  292. "High attenuation with de-emphasis",
  293. "Fixed process flow",
  294. "Ringing-less low latency FIR",
  295. };
  296. static const unsigned int pcm512x_dsp_program_values[] = {
  297. 1,
  298. 2,
  299. 3,
  300. 5,
  301. 7,
  302. };
  303. static SOC_VALUE_ENUM_SINGLE_DECL(pcm512x_dsp_program,
  304. PCM512x_DSP_PROGRAM, 0, 0x1f,
  305. pcm512x_dsp_program_texts,
  306. pcm512x_dsp_program_values);
  307. static const char * const pcm512x_clk_missing_text[] = {
  308. "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s"
  309. };
  310. static const struct soc_enum pcm512x_clk_missing =
  311. SOC_ENUM_SINGLE(PCM512x_CLKDET, 0, 8, pcm512x_clk_missing_text);
  312. static const char * const pcm512x_autom_text[] = {
  313. "21ms", "106ms", "213ms", "533ms", "1.07s", "2.13s", "5.33s", "10.66s"
  314. };
  315. static const struct soc_enum pcm512x_autom_l =
  316. SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATML_SHIFT, 8,
  317. pcm512x_autom_text);
  318. static const struct soc_enum pcm512x_autom_r =
  319. SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATMR_SHIFT, 8,
  320. pcm512x_autom_text);
  321. static const char * const pcm512x_ramp_rate_text[] = {
  322. "1 sample/update", "2 samples/update", "4 samples/update",
  323. "Immediate"
  324. };
  325. static const struct soc_enum pcm512x_vndf =
  326. SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDF_SHIFT, 4,
  327. pcm512x_ramp_rate_text);
  328. static const struct soc_enum pcm512x_vnuf =
  329. SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUF_SHIFT, 4,
  330. pcm512x_ramp_rate_text);
  331. static const struct soc_enum pcm512x_vedf =
  332. SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDF_SHIFT, 4,
  333. pcm512x_ramp_rate_text);
  334. static const char * const pcm512x_ramp_step_text[] = {
  335. "4dB/step", "2dB/step", "1dB/step", "0.5dB/step"
  336. };
  337. static const struct soc_enum pcm512x_vnds =
  338. SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDS_SHIFT, 4,
  339. pcm512x_ramp_step_text);
  340. static const struct soc_enum pcm512x_vnus =
  341. SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUS_SHIFT, 4,
  342. pcm512x_ramp_step_text);
  343. static const struct soc_enum pcm512x_veds =
  344. SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDS_SHIFT, 4,
  345. pcm512x_ramp_step_text);
  346. static const struct snd_kcontrol_new pcm512x_controls[] = {
  347. SOC_DOUBLE_R_TLV("Digital Playback Volume", PCM512x_DIGITAL_VOLUME_2,
  348. PCM512x_DIGITAL_VOLUME_3, 0, 255, 1, digital_tlv),
  349. SOC_DOUBLE_TLV("Analogue Playback Volume", PCM512x_ANALOG_GAIN_CTRL,
  350. PCM512x_LAGN_SHIFT, PCM512x_RAGN_SHIFT, 1, 1, analog_tlv),
  351. SOC_DOUBLE_TLV("Analogue Playback Boost Volume", PCM512x_ANALOG_GAIN_BOOST,
  352. PCM512x_AGBL_SHIFT, PCM512x_AGBR_SHIFT, 1, 0, boost_tlv),
  353. SOC_DOUBLE("Digital Playback Switch", PCM512x_MUTE, PCM512x_RQML_SHIFT,
  354. PCM512x_RQMR_SHIFT, 1, 1),
  355. SOC_SINGLE("Deemphasis Switch", PCM512x_DSP, PCM512x_DEMP_SHIFT, 1, 1),
  356. SOC_ENUM("DSP Program", pcm512x_dsp_program),
  357. SOC_ENUM("Clock Missing Period", pcm512x_clk_missing),
  358. SOC_ENUM("Auto Mute Time Left", pcm512x_autom_l),
  359. SOC_ENUM("Auto Mute Time Right", pcm512x_autom_r),
  360. SOC_SINGLE("Auto Mute Mono Switch", PCM512x_DIGITAL_MUTE_3,
  361. PCM512x_ACTL_SHIFT, 1, 0),
  362. SOC_DOUBLE("Auto Mute Switch", PCM512x_DIGITAL_MUTE_3, PCM512x_AMLE_SHIFT,
  363. PCM512x_AMRE_SHIFT, 1, 0),
  364. SOC_ENUM("Volume Ramp Down Rate", pcm512x_vndf),
  365. SOC_ENUM("Volume Ramp Down Step", pcm512x_vnds),
  366. SOC_ENUM("Volume Ramp Up Rate", pcm512x_vnuf),
  367. SOC_ENUM("Volume Ramp Up Step", pcm512x_vnus),
  368. SOC_ENUM("Volume Ramp Down Emergency Rate", pcm512x_vedf),
  369. SOC_ENUM("Volume Ramp Down Emergency Step", pcm512x_veds),
  370. SOC_SINGLE_EXT("Max Overclock PLL", SND_SOC_NOPM, 0, 20, 0,
  371. pcm512x_overclock_pll_get, pcm512x_overclock_pll_put),
  372. SOC_SINGLE_EXT("Max Overclock DSP", SND_SOC_NOPM, 0, 40, 0,
  373. pcm512x_overclock_dsp_get, pcm512x_overclock_dsp_put),
  374. SOC_SINGLE_EXT("Max Overclock DAC", SND_SOC_NOPM, 0, 40, 0,
  375. pcm512x_overclock_dac_get, pcm512x_overclock_dac_put),
  376. };
  377. static const struct snd_soc_dapm_widget pcm512x_dapm_widgets[] = {
  378. SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0),
  379. SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0),
  380. SND_SOC_DAPM_OUTPUT("OUTL"),
  381. SND_SOC_DAPM_OUTPUT("OUTR"),
  382. };
  383. static const struct snd_soc_dapm_route pcm512x_dapm_routes[] = {
  384. { "DACL", NULL, "Playback" },
  385. { "DACR", NULL, "Playback" },
  386. { "OUTL", NULL, "DACL" },
  387. { "OUTR", NULL, "DACR" },
  388. };
  389. static unsigned long pcm512x_pll_max(struct pcm512x_priv *pcm512x)
  390. {
  391. return 25000000 + 25000000 * pcm512x->overclock_pll / 100;
  392. }
  393. static unsigned long pcm512x_dsp_max(struct pcm512x_priv *pcm512x)
  394. {
  395. return 50000000 + 50000000 * pcm512x->overclock_dsp / 100;
  396. }
  397. static unsigned long pcm512x_dac_max(struct pcm512x_priv *pcm512x,
  398. unsigned long rate)
  399. {
  400. return rate + rate * pcm512x->overclock_dac / 100;
  401. }
  402. static unsigned long pcm512x_sck_max(struct pcm512x_priv *pcm512x)
  403. {
  404. if (!pcm512x->pll_out)
  405. return 25000000;
  406. return pcm512x_pll_max(pcm512x);
  407. }
  408. static unsigned long pcm512x_ncp_target(struct pcm512x_priv *pcm512x,
  409. unsigned long dac_rate)
  410. {
  411. /*
  412. * If the DAC is not actually overclocked, use the good old
  413. * NCP target rate...
  414. */
  415. if (dac_rate <= 6144000)
  416. return 1536000;
  417. /*
  418. * ...but if the DAC is in fact overclocked, bump the NCP target
  419. * rate to get the recommended dividers even when overclocking.
  420. */
  421. return pcm512x_dac_max(pcm512x, 1536000);
  422. }
  423. static const u32 pcm512x_dai_rates[] = {
  424. 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
  425. 88200, 96000, 176400, 192000, 384000,
  426. };
  427. static const struct snd_pcm_hw_constraint_list constraints_slave = {
  428. .count = ARRAY_SIZE(pcm512x_dai_rates),
  429. .list = pcm512x_dai_rates,
  430. };
  431. static int pcm512x_hw_rule_rate(struct snd_pcm_hw_params *params,
  432. struct snd_pcm_hw_rule *rule)
  433. {
  434. struct pcm512x_priv *pcm512x = rule->private;
  435. struct snd_interval ranges[2];
  436. int frame_size;
  437. frame_size = snd_soc_params_to_frame_size(params);
  438. if (frame_size < 0)
  439. return frame_size;
  440. switch (frame_size) {
  441. case 32:
  442. /* No hole when the frame size is 32. */
  443. return 0;
  444. case 48:
  445. case 64:
  446. /* There is only one hole in the range of supported
  447. * rates, but it moves with the frame size.
  448. */
  449. memset(ranges, 0, sizeof(ranges));
  450. ranges[0].min = 8000;
  451. ranges[0].max = pcm512x_sck_max(pcm512x) / frame_size / 2;
  452. ranges[1].min = DIV_ROUND_UP(16000000, frame_size);
  453. ranges[1].max = 384000;
  454. break;
  455. default:
  456. return -EINVAL;
  457. }
  458. return snd_interval_ranges(hw_param_interval(params, rule->var),
  459. ARRAY_SIZE(ranges), ranges, 0);
  460. }
  461. static int pcm512x_dai_startup_master(struct snd_pcm_substream *substream,
  462. struct snd_soc_dai *dai)
  463. {
  464. struct snd_soc_codec *codec = dai->codec;
  465. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  466. struct device *dev = dai->dev;
  467. struct snd_pcm_hw_constraint_ratnums *constraints_no_pll;
  468. struct snd_ratnum *rats_no_pll;
  469. if (IS_ERR(pcm512x->sclk)) {
  470. dev_err(dev, "Need SCLK for master mode: %ld\n",
  471. PTR_ERR(pcm512x->sclk));
  472. return PTR_ERR(pcm512x->sclk);
  473. }
  474. if (pcm512x->pll_out)
  475. return snd_pcm_hw_rule_add(substream->runtime, 0,
  476. SNDRV_PCM_HW_PARAM_RATE,
  477. pcm512x_hw_rule_rate,
  478. pcm512x,
  479. SNDRV_PCM_HW_PARAM_FRAME_BITS,
  480. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  481. constraints_no_pll = devm_kzalloc(dev, sizeof(*constraints_no_pll),
  482. GFP_KERNEL);
  483. if (!constraints_no_pll)
  484. return -ENOMEM;
  485. constraints_no_pll->nrats = 1;
  486. rats_no_pll = devm_kzalloc(dev, sizeof(*rats_no_pll), GFP_KERNEL);
  487. if (!rats_no_pll)
  488. return -ENOMEM;
  489. constraints_no_pll->rats = rats_no_pll;
  490. rats_no_pll->num = clk_get_rate(pcm512x->sclk) / 64;
  491. rats_no_pll->den_min = 1;
  492. rats_no_pll->den_max = 128;
  493. rats_no_pll->den_step = 1;
  494. return snd_pcm_hw_constraint_ratnums(substream->runtime, 0,
  495. SNDRV_PCM_HW_PARAM_RATE,
  496. constraints_no_pll);
  497. }
  498. static int pcm512x_dai_startup_slave(struct snd_pcm_substream *substream,
  499. struct snd_soc_dai *dai)
  500. {
  501. struct snd_soc_codec *codec = dai->codec;
  502. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  503. struct device *dev = dai->dev;
  504. struct regmap *regmap = pcm512x->regmap;
  505. if (IS_ERR(pcm512x->sclk)) {
  506. dev_info(dev, "No SCLK, using BCLK: %ld\n",
  507. PTR_ERR(pcm512x->sclk));
  508. /* Disable reporting of missing SCLK as an error */
  509. regmap_update_bits(regmap, PCM512x_ERROR_DETECT,
  510. PCM512x_IDCH, PCM512x_IDCH);
  511. /* Switch PLL input to BCLK */
  512. regmap_update_bits(regmap, PCM512x_PLL_REF,
  513. PCM512x_SREF, PCM512x_SREF_BCK);
  514. }
  515. return snd_pcm_hw_constraint_list(substream->runtime, 0,
  516. SNDRV_PCM_HW_PARAM_RATE,
  517. &constraints_slave);
  518. }
  519. static int pcm512x_dai_startup(struct snd_pcm_substream *substream,
  520. struct snd_soc_dai *dai)
  521. {
  522. struct snd_soc_codec *codec = dai->codec;
  523. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  524. switch (pcm512x->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  525. case SND_SOC_DAIFMT_CBM_CFM:
  526. case SND_SOC_DAIFMT_CBM_CFS:
  527. return pcm512x_dai_startup_master(substream, dai);
  528. case SND_SOC_DAIFMT_CBS_CFS:
  529. return pcm512x_dai_startup_slave(substream, dai);
  530. default:
  531. return -EINVAL;
  532. }
  533. }
  534. static int pcm512x_set_bias_level(struct snd_soc_codec *codec,
  535. enum snd_soc_bias_level level)
  536. {
  537. struct pcm512x_priv *pcm512x = dev_get_drvdata(codec->dev);
  538. int ret;
  539. switch (level) {
  540. case SND_SOC_BIAS_ON:
  541. case SND_SOC_BIAS_PREPARE:
  542. break;
  543. case SND_SOC_BIAS_STANDBY:
  544. ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  545. PCM512x_RQST, 0);
  546. if (ret != 0) {
  547. dev_err(codec->dev, "Failed to remove standby: %d\n",
  548. ret);
  549. return ret;
  550. }
  551. break;
  552. case SND_SOC_BIAS_OFF:
  553. ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  554. PCM512x_RQST, PCM512x_RQST);
  555. if (ret != 0) {
  556. dev_err(codec->dev, "Failed to request standby: %d\n",
  557. ret);
  558. return ret;
  559. }
  560. break;
  561. }
  562. return 0;
  563. }
  564. static unsigned long pcm512x_find_sck(struct snd_soc_dai *dai,
  565. unsigned long bclk_rate)
  566. {
  567. struct device *dev = dai->dev;
  568. struct snd_soc_codec *codec = dai->codec;
  569. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  570. unsigned long sck_rate;
  571. int pow2;
  572. /* 64 MHz <= pll_rate <= 100 MHz, VREF mode */
  573. /* 16 MHz <= sck_rate <= 25 MHz, VREF mode */
  574. /* select sck_rate as a multiple of bclk_rate but still with
  575. * as many factors of 2 as possible, as that makes it easier
  576. * to find a fast DAC rate
  577. */
  578. pow2 = 1 << fls((pcm512x_pll_max(pcm512x) - 16000000) / bclk_rate);
  579. for (; pow2; pow2 >>= 1) {
  580. sck_rate = rounddown(pcm512x_pll_max(pcm512x),
  581. bclk_rate * pow2);
  582. if (sck_rate >= 16000000)
  583. break;
  584. }
  585. if (!pow2) {
  586. dev_err(dev, "Impossible to generate a suitable SCK\n");
  587. return 0;
  588. }
  589. dev_dbg(dev, "sck_rate %lu\n", sck_rate);
  590. return sck_rate;
  591. }
  592. /* pll_rate = pllin_rate * R * J.D / P
  593. * 1 <= R <= 16
  594. * 1 <= J <= 63
  595. * 0 <= D <= 9999
  596. * 1 <= P <= 15
  597. * 64 MHz <= pll_rate <= 100 MHz
  598. * if D == 0
  599. * 1 MHz <= pllin_rate / P <= 20 MHz
  600. * else if D > 0
  601. * 6.667 MHz <= pllin_rate / P <= 20 MHz
  602. * 4 <= J <= 11
  603. * R = 1
  604. */
  605. static int pcm512x_find_pll_coeff(struct snd_soc_dai *dai,
  606. unsigned long pllin_rate,
  607. unsigned long pll_rate)
  608. {
  609. struct device *dev = dai->dev;
  610. struct snd_soc_codec *codec = dai->codec;
  611. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  612. unsigned long common;
  613. int R, J, D, P;
  614. unsigned long K; /* 10000 * J.D */
  615. unsigned long num;
  616. unsigned long den;
  617. common = gcd(pll_rate, pllin_rate);
  618. dev_dbg(dev, "pll %lu pllin %lu common %lu\n",
  619. pll_rate, pllin_rate, common);
  620. num = pll_rate / common;
  621. den = pllin_rate / common;
  622. /* pllin_rate / P (or here, den) cannot be greater than 20 MHz */
  623. if (pllin_rate / den > 20000000 && num < 8) {
  624. num *= DIV_ROUND_UP(pllin_rate / den, 20000000);
  625. den *= DIV_ROUND_UP(pllin_rate / den, 20000000);
  626. }
  627. dev_dbg(dev, "num / den = %lu / %lu\n", num, den);
  628. P = den;
  629. if (den <= 15 && num <= 16 * 63
  630. && 1000000 <= pllin_rate / P && pllin_rate / P <= 20000000) {
  631. /* Try the case with D = 0 */
  632. D = 0;
  633. /* factor 'num' into J and R, such that R <= 16 and J <= 63 */
  634. for (R = 16; R; R--) {
  635. if (num % R)
  636. continue;
  637. J = num / R;
  638. if (J == 0 || J > 63)
  639. continue;
  640. dev_dbg(dev, "R * J / P = %d * %d / %d\n", R, J, P);
  641. pcm512x->real_pll = pll_rate;
  642. goto done;
  643. }
  644. /* no luck */
  645. }
  646. R = 1;
  647. if (num > 0xffffffffUL / 10000)
  648. goto fallback;
  649. /* Try to find an exact pll_rate using the D > 0 case */
  650. common = gcd(10000 * num, den);
  651. num = 10000 * num / common;
  652. den /= common;
  653. dev_dbg(dev, "num %lu den %lu common %lu\n", num, den, common);
  654. for (P = den; P <= 15; P++) {
  655. if (pllin_rate / P < 6667000 || 200000000 < pllin_rate / P)
  656. continue;
  657. if (num * P % den)
  658. continue;
  659. K = num * P / den;
  660. /* J == 12 is ok if D == 0 */
  661. if (K < 40000 || K > 120000)
  662. continue;
  663. J = K / 10000;
  664. D = K % 10000;
  665. dev_dbg(dev, "J.D / P = %d.%04d / %d\n", J, D, P);
  666. pcm512x->real_pll = pll_rate;
  667. goto done;
  668. }
  669. /* Fall back to an approximate pll_rate */
  670. fallback:
  671. /* find smallest possible P */
  672. P = DIV_ROUND_UP(pllin_rate, 20000000);
  673. if (!P)
  674. P = 1;
  675. else if (P > 15) {
  676. dev_err(dev, "Need a slower clock as pll-input\n");
  677. return -EINVAL;
  678. }
  679. if (pllin_rate / P < 6667000) {
  680. dev_err(dev, "Need a faster clock as pll-input\n");
  681. return -EINVAL;
  682. }
  683. K = DIV_ROUND_CLOSEST_ULL(10000ULL * pll_rate * P, pllin_rate);
  684. if (K < 40000)
  685. K = 40000;
  686. /* J == 12 is ok if D == 0 */
  687. if (K > 120000)
  688. K = 120000;
  689. J = K / 10000;
  690. D = K % 10000;
  691. dev_dbg(dev, "J.D / P ~ %d.%04d / %d\n", J, D, P);
  692. pcm512x->real_pll = DIV_ROUND_DOWN_ULL((u64)K * pllin_rate, 10000 * P);
  693. done:
  694. pcm512x->pll_r = R;
  695. pcm512x->pll_j = J;
  696. pcm512x->pll_d = D;
  697. pcm512x->pll_p = P;
  698. return 0;
  699. }
  700. static unsigned long pcm512x_pllin_dac_rate(struct snd_soc_dai *dai,
  701. unsigned long osr_rate,
  702. unsigned long pllin_rate)
  703. {
  704. struct snd_soc_codec *codec = dai->codec;
  705. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  706. unsigned long dac_rate;
  707. if (!pcm512x->pll_out)
  708. return 0; /* no PLL to bypass, force SCK as DAC input */
  709. if (pllin_rate % osr_rate)
  710. return 0; /* futile, quit early */
  711. /* run DAC no faster than 6144000 Hz */
  712. for (dac_rate = rounddown(pcm512x_dac_max(pcm512x, 6144000), osr_rate);
  713. dac_rate;
  714. dac_rate -= osr_rate) {
  715. if (pllin_rate / dac_rate > 128)
  716. return 0; /* DAC divider would be too big */
  717. if (!(pllin_rate % dac_rate))
  718. return dac_rate;
  719. dac_rate -= osr_rate;
  720. }
  721. return 0;
  722. }
  723. static int pcm512x_set_dividers(struct snd_soc_dai *dai,
  724. struct snd_pcm_hw_params *params)
  725. {
  726. struct device *dev = dai->dev;
  727. struct snd_soc_codec *codec = dai->codec;
  728. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  729. unsigned long pllin_rate = 0;
  730. unsigned long pll_rate;
  731. unsigned long sck_rate;
  732. unsigned long mck_rate;
  733. unsigned long bclk_rate;
  734. unsigned long sample_rate;
  735. unsigned long osr_rate;
  736. unsigned long dacsrc_rate;
  737. int bclk_div;
  738. int lrclk_div;
  739. int dsp_div;
  740. int dac_div;
  741. unsigned long dac_rate;
  742. int ncp_div;
  743. int osr_div;
  744. int ret;
  745. int idac;
  746. int fssp;
  747. int gpio;
  748. lrclk_div = snd_soc_params_to_frame_size(params);
  749. if (lrclk_div == 0) {
  750. dev_err(dev, "No LRCLK?\n");
  751. return -EINVAL;
  752. }
  753. if (!pcm512x->pll_out) {
  754. sck_rate = clk_get_rate(pcm512x->sclk);
  755. bclk_div = params->rate_den * 64 / lrclk_div;
  756. bclk_rate = DIV_ROUND_CLOSEST(sck_rate, bclk_div);
  757. mck_rate = sck_rate;
  758. } else {
  759. ret = snd_soc_params_to_bclk(params);
  760. if (ret < 0) {
  761. dev_err(dev, "Failed to find suitable BCLK: %d\n", ret);
  762. return ret;
  763. }
  764. if (ret == 0) {
  765. dev_err(dev, "No BCLK?\n");
  766. return -EINVAL;
  767. }
  768. bclk_rate = ret;
  769. pllin_rate = clk_get_rate(pcm512x->sclk);
  770. sck_rate = pcm512x_find_sck(dai, bclk_rate);
  771. if (!sck_rate)
  772. return -EINVAL;
  773. pll_rate = 4 * sck_rate;
  774. ret = pcm512x_find_pll_coeff(dai, pllin_rate, pll_rate);
  775. if (ret != 0)
  776. return ret;
  777. ret = regmap_write(pcm512x->regmap,
  778. PCM512x_PLL_COEFF_0, pcm512x->pll_p - 1);
  779. if (ret != 0) {
  780. dev_err(dev, "Failed to write PLL P: %d\n", ret);
  781. return ret;
  782. }
  783. ret = regmap_write(pcm512x->regmap,
  784. PCM512x_PLL_COEFF_1, pcm512x->pll_j);
  785. if (ret != 0) {
  786. dev_err(dev, "Failed to write PLL J: %d\n", ret);
  787. return ret;
  788. }
  789. ret = regmap_write(pcm512x->regmap,
  790. PCM512x_PLL_COEFF_2, pcm512x->pll_d >> 8);
  791. if (ret != 0) {
  792. dev_err(dev, "Failed to write PLL D msb: %d\n", ret);
  793. return ret;
  794. }
  795. ret = regmap_write(pcm512x->regmap,
  796. PCM512x_PLL_COEFF_3, pcm512x->pll_d & 0xff);
  797. if (ret != 0) {
  798. dev_err(dev, "Failed to write PLL D lsb: %d\n", ret);
  799. return ret;
  800. }
  801. ret = regmap_write(pcm512x->regmap,
  802. PCM512x_PLL_COEFF_4, pcm512x->pll_r - 1);
  803. if (ret != 0) {
  804. dev_err(dev, "Failed to write PLL R: %d\n", ret);
  805. return ret;
  806. }
  807. mck_rate = pcm512x->real_pll;
  808. bclk_div = DIV_ROUND_CLOSEST(sck_rate, bclk_rate);
  809. }
  810. if (bclk_div > 128) {
  811. dev_err(dev, "Failed to find BCLK divider\n");
  812. return -EINVAL;
  813. }
  814. /* the actual rate */
  815. sample_rate = sck_rate / bclk_div / lrclk_div;
  816. osr_rate = 16 * sample_rate;
  817. /* run DSP no faster than 50 MHz */
  818. dsp_div = mck_rate > pcm512x_dsp_max(pcm512x) ? 2 : 1;
  819. dac_rate = pcm512x_pllin_dac_rate(dai, osr_rate, pllin_rate);
  820. if (dac_rate) {
  821. /* the desired clock rate is "compatible" with the pll input
  822. * clock, so use that clock as dac input instead of the pll
  823. * output clock since the pll will introduce jitter and thus
  824. * noise.
  825. */
  826. dev_dbg(dev, "using pll input as dac input\n");
  827. ret = regmap_update_bits(pcm512x->regmap, PCM512x_DAC_REF,
  828. PCM512x_SDAC, PCM512x_SDAC_GPIO);
  829. if (ret != 0) {
  830. dev_err(codec->dev,
  831. "Failed to set gpio as dacref: %d\n", ret);
  832. return ret;
  833. }
  834. gpio = PCM512x_GREF_GPIO1 + pcm512x->pll_in - 1;
  835. ret = regmap_update_bits(pcm512x->regmap, PCM512x_GPIO_DACIN,
  836. PCM512x_GREF, gpio);
  837. if (ret != 0) {
  838. dev_err(codec->dev,
  839. "Failed to set gpio %d as dacin: %d\n",
  840. pcm512x->pll_in, ret);
  841. return ret;
  842. }
  843. dacsrc_rate = pllin_rate;
  844. } else {
  845. /* run DAC no faster than 6144000 Hz */
  846. unsigned long dac_mul = pcm512x_dac_max(pcm512x, 6144000)
  847. / osr_rate;
  848. unsigned long sck_mul = sck_rate / osr_rate;
  849. for (; dac_mul; dac_mul--) {
  850. if (!(sck_mul % dac_mul))
  851. break;
  852. }
  853. if (!dac_mul) {
  854. dev_err(dev, "Failed to find DAC rate\n");
  855. return -EINVAL;
  856. }
  857. dac_rate = dac_mul * osr_rate;
  858. dev_dbg(dev, "dac_rate %lu sample_rate %lu\n",
  859. dac_rate, sample_rate);
  860. ret = regmap_update_bits(pcm512x->regmap, PCM512x_DAC_REF,
  861. PCM512x_SDAC, PCM512x_SDAC_SCK);
  862. if (ret != 0) {
  863. dev_err(codec->dev,
  864. "Failed to set sck as dacref: %d\n", ret);
  865. return ret;
  866. }
  867. dacsrc_rate = sck_rate;
  868. }
  869. osr_div = DIV_ROUND_CLOSEST(dac_rate, osr_rate);
  870. if (osr_div > 128) {
  871. dev_err(dev, "Failed to find OSR divider\n");
  872. return -EINVAL;
  873. }
  874. dac_div = DIV_ROUND_CLOSEST(dacsrc_rate, dac_rate);
  875. if (dac_div > 128) {
  876. dev_err(dev, "Failed to find DAC divider\n");
  877. return -EINVAL;
  878. }
  879. dac_rate = dacsrc_rate / dac_div;
  880. ncp_div = DIV_ROUND_CLOSEST(dac_rate,
  881. pcm512x_ncp_target(pcm512x, dac_rate));
  882. if (ncp_div > 128 || dac_rate / ncp_div > 2048000) {
  883. /* run NCP no faster than 2048000 Hz, but why? */
  884. ncp_div = DIV_ROUND_UP(dac_rate, 2048000);
  885. if (ncp_div > 128) {
  886. dev_err(dev, "Failed to find NCP divider\n");
  887. return -EINVAL;
  888. }
  889. }
  890. idac = mck_rate / (dsp_div * sample_rate);
  891. ret = regmap_write(pcm512x->regmap, PCM512x_DSP_CLKDIV, dsp_div - 1);
  892. if (ret != 0) {
  893. dev_err(dev, "Failed to write DSP divider: %d\n", ret);
  894. return ret;
  895. }
  896. ret = regmap_write(pcm512x->regmap, PCM512x_DAC_CLKDIV, dac_div - 1);
  897. if (ret != 0) {
  898. dev_err(dev, "Failed to write DAC divider: %d\n", ret);
  899. return ret;
  900. }
  901. ret = regmap_write(pcm512x->regmap, PCM512x_NCP_CLKDIV, ncp_div - 1);
  902. if (ret != 0) {
  903. dev_err(dev, "Failed to write NCP divider: %d\n", ret);
  904. return ret;
  905. }
  906. ret = regmap_write(pcm512x->regmap, PCM512x_OSR_CLKDIV, osr_div - 1);
  907. if (ret != 0) {
  908. dev_err(dev, "Failed to write OSR divider: %d\n", ret);
  909. return ret;
  910. }
  911. ret = regmap_write(pcm512x->regmap,
  912. PCM512x_MASTER_CLKDIV_1, bclk_div - 1);
  913. if (ret != 0) {
  914. dev_err(dev, "Failed to write BCLK divider: %d\n", ret);
  915. return ret;
  916. }
  917. ret = regmap_write(pcm512x->regmap,
  918. PCM512x_MASTER_CLKDIV_2, lrclk_div - 1);
  919. if (ret != 0) {
  920. dev_err(dev, "Failed to write LRCLK divider: %d\n", ret);
  921. return ret;
  922. }
  923. ret = regmap_write(pcm512x->regmap, PCM512x_IDAC_1, idac >> 8);
  924. if (ret != 0) {
  925. dev_err(dev, "Failed to write IDAC msb divider: %d\n", ret);
  926. return ret;
  927. }
  928. ret = regmap_write(pcm512x->regmap, PCM512x_IDAC_2, idac & 0xff);
  929. if (ret != 0) {
  930. dev_err(dev, "Failed to write IDAC lsb divider: %d\n", ret);
  931. return ret;
  932. }
  933. if (sample_rate <= pcm512x_dac_max(pcm512x, 48000))
  934. fssp = PCM512x_FSSP_48KHZ;
  935. else if (sample_rate <= pcm512x_dac_max(pcm512x, 96000))
  936. fssp = PCM512x_FSSP_96KHZ;
  937. else if (sample_rate <= pcm512x_dac_max(pcm512x, 192000))
  938. fssp = PCM512x_FSSP_192KHZ;
  939. else
  940. fssp = PCM512x_FSSP_384KHZ;
  941. ret = regmap_update_bits(pcm512x->regmap, PCM512x_FS_SPEED_MODE,
  942. PCM512x_FSSP, fssp);
  943. if (ret != 0) {
  944. dev_err(codec->dev, "Failed to set fs speed: %d\n", ret);
  945. return ret;
  946. }
  947. dev_dbg(codec->dev, "DSP divider %d\n", dsp_div);
  948. dev_dbg(codec->dev, "DAC divider %d\n", dac_div);
  949. dev_dbg(codec->dev, "NCP divider %d\n", ncp_div);
  950. dev_dbg(codec->dev, "OSR divider %d\n", osr_div);
  951. dev_dbg(codec->dev, "BCK divider %d\n", bclk_div);
  952. dev_dbg(codec->dev, "LRCK divider %d\n", lrclk_div);
  953. dev_dbg(codec->dev, "IDAC %d\n", idac);
  954. dev_dbg(codec->dev, "1<<FSSP %d\n", 1 << fssp);
  955. return 0;
  956. }
  957. static int pcm512x_hw_params(struct snd_pcm_substream *substream,
  958. struct snd_pcm_hw_params *params,
  959. struct snd_soc_dai *dai)
  960. {
  961. struct snd_soc_codec *codec = dai->codec;
  962. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  963. int alen;
  964. int gpio;
  965. int clock_output;
  966. int master_mode;
  967. int ret;
  968. dev_dbg(codec->dev, "hw_params %u Hz, %u channels\n",
  969. params_rate(params),
  970. params_channels(params));
  971. switch (params_width(params)) {
  972. case 16:
  973. alen = PCM512x_ALEN_16;
  974. break;
  975. case 20:
  976. alen = PCM512x_ALEN_20;
  977. break;
  978. case 24:
  979. alen = PCM512x_ALEN_24;
  980. break;
  981. case 32:
  982. alen = PCM512x_ALEN_32;
  983. break;
  984. default:
  985. dev_err(codec->dev, "Bad frame size: %d\n",
  986. params_width(params));
  987. return -EINVAL;
  988. }
  989. switch (pcm512x->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  990. case SND_SOC_DAIFMT_CBS_CFS:
  991. ret = regmap_update_bits(pcm512x->regmap,
  992. PCM512x_BCLK_LRCLK_CFG,
  993. PCM512x_BCKP
  994. | PCM512x_BCKO | PCM512x_LRKO,
  995. 0);
  996. if (ret != 0) {
  997. dev_err(codec->dev,
  998. "Failed to enable slave mode: %d\n", ret);
  999. return ret;
  1000. }
  1001. ret = regmap_update_bits(pcm512x->regmap, PCM512x_ERROR_DETECT,
  1002. PCM512x_DCAS, 0);
  1003. if (ret != 0) {
  1004. dev_err(codec->dev,
  1005. "Failed to enable clock divider autoset: %d\n",
  1006. ret);
  1007. return ret;
  1008. }
  1009. return 0;
  1010. case SND_SOC_DAIFMT_CBM_CFM:
  1011. clock_output = PCM512x_BCKO | PCM512x_LRKO;
  1012. master_mode = PCM512x_RLRK | PCM512x_RBCK;
  1013. break;
  1014. case SND_SOC_DAIFMT_CBM_CFS:
  1015. clock_output = PCM512x_BCKO;
  1016. master_mode = PCM512x_RBCK;
  1017. break;
  1018. default:
  1019. return -EINVAL;
  1020. }
  1021. ret = regmap_update_bits(pcm512x->regmap, PCM512x_I2S_1,
  1022. PCM512x_ALEN, alen);
  1023. if (ret != 0) {
  1024. dev_err(codec->dev, "Failed to set frame size: %d\n", ret);
  1025. return ret;
  1026. }
  1027. if (pcm512x->pll_out) {
  1028. ret = regmap_write(pcm512x->regmap, PCM512x_FLEX_A, 0x11);
  1029. if (ret != 0) {
  1030. dev_err(codec->dev, "Failed to set FLEX_A: %d\n", ret);
  1031. return ret;
  1032. }
  1033. ret = regmap_write(pcm512x->regmap, PCM512x_FLEX_B, 0xff);
  1034. if (ret != 0) {
  1035. dev_err(codec->dev, "Failed to set FLEX_B: %d\n", ret);
  1036. return ret;
  1037. }
  1038. ret = regmap_update_bits(pcm512x->regmap, PCM512x_ERROR_DETECT,
  1039. PCM512x_IDFS | PCM512x_IDBK
  1040. | PCM512x_IDSK | PCM512x_IDCH
  1041. | PCM512x_IDCM | PCM512x_DCAS
  1042. | PCM512x_IPLK,
  1043. PCM512x_IDFS | PCM512x_IDBK
  1044. | PCM512x_IDSK | PCM512x_IDCH
  1045. | PCM512x_DCAS);
  1046. if (ret != 0) {
  1047. dev_err(codec->dev,
  1048. "Failed to ignore auto-clock failures: %d\n",
  1049. ret);
  1050. return ret;
  1051. }
  1052. } else {
  1053. ret = regmap_update_bits(pcm512x->regmap, PCM512x_ERROR_DETECT,
  1054. PCM512x_IDFS | PCM512x_IDBK
  1055. | PCM512x_IDSK | PCM512x_IDCH
  1056. | PCM512x_IDCM | PCM512x_DCAS
  1057. | PCM512x_IPLK,
  1058. PCM512x_IDFS | PCM512x_IDBK
  1059. | PCM512x_IDSK | PCM512x_IDCH
  1060. | PCM512x_DCAS | PCM512x_IPLK);
  1061. if (ret != 0) {
  1062. dev_err(codec->dev,
  1063. "Failed to ignore auto-clock failures: %d\n",
  1064. ret);
  1065. return ret;
  1066. }
  1067. ret = regmap_update_bits(pcm512x->regmap, PCM512x_PLL_EN,
  1068. PCM512x_PLLE, 0);
  1069. if (ret != 0) {
  1070. dev_err(codec->dev, "Failed to disable pll: %d\n", ret);
  1071. return ret;
  1072. }
  1073. }
  1074. ret = pcm512x_set_dividers(dai, params);
  1075. if (ret != 0)
  1076. return ret;
  1077. if (pcm512x->pll_out) {
  1078. ret = regmap_update_bits(pcm512x->regmap, PCM512x_PLL_REF,
  1079. PCM512x_SREF, PCM512x_SREF_GPIO);
  1080. if (ret != 0) {
  1081. dev_err(codec->dev,
  1082. "Failed to set gpio as pllref: %d\n", ret);
  1083. return ret;
  1084. }
  1085. gpio = PCM512x_GREF_GPIO1 + pcm512x->pll_in - 1;
  1086. ret = regmap_update_bits(pcm512x->regmap, PCM512x_GPIO_PLLIN,
  1087. PCM512x_GREF, gpio);
  1088. if (ret != 0) {
  1089. dev_err(codec->dev,
  1090. "Failed to set gpio %d as pllin: %d\n",
  1091. pcm512x->pll_in, ret);
  1092. return ret;
  1093. }
  1094. ret = regmap_update_bits(pcm512x->regmap, PCM512x_PLL_EN,
  1095. PCM512x_PLLE, PCM512x_PLLE);
  1096. if (ret != 0) {
  1097. dev_err(codec->dev, "Failed to enable pll: %d\n", ret);
  1098. return ret;
  1099. }
  1100. }
  1101. ret = regmap_update_bits(pcm512x->regmap, PCM512x_BCLK_LRCLK_CFG,
  1102. PCM512x_BCKP | PCM512x_BCKO | PCM512x_LRKO,
  1103. clock_output);
  1104. if (ret != 0) {
  1105. dev_err(codec->dev, "Failed to enable clock output: %d\n", ret);
  1106. return ret;
  1107. }
  1108. ret = regmap_update_bits(pcm512x->regmap, PCM512x_MASTER_MODE,
  1109. PCM512x_RLRK | PCM512x_RBCK,
  1110. master_mode);
  1111. if (ret != 0) {
  1112. dev_err(codec->dev, "Failed to enable master mode: %d\n", ret);
  1113. return ret;
  1114. }
  1115. if (pcm512x->pll_out) {
  1116. gpio = PCM512x_G1OE << (pcm512x->pll_out - 1);
  1117. ret = regmap_update_bits(pcm512x->regmap, PCM512x_GPIO_EN,
  1118. gpio, gpio);
  1119. if (ret != 0) {
  1120. dev_err(codec->dev, "Failed to enable gpio %d: %d\n",
  1121. pcm512x->pll_out, ret);
  1122. return ret;
  1123. }
  1124. gpio = PCM512x_GPIO_OUTPUT_1 + pcm512x->pll_out - 1;
  1125. ret = regmap_update_bits(pcm512x->regmap, gpio,
  1126. PCM512x_GxSL, PCM512x_GxSL_PLLCK);
  1127. if (ret != 0) {
  1128. dev_err(codec->dev, "Failed to output pll on %d: %d\n",
  1129. ret, pcm512x->pll_out);
  1130. return ret;
  1131. }
  1132. }
  1133. ret = regmap_update_bits(pcm512x->regmap, PCM512x_SYNCHRONIZE,
  1134. PCM512x_RQSY, PCM512x_RQSY_HALT);
  1135. if (ret != 0) {
  1136. dev_err(codec->dev, "Failed to halt clocks: %d\n", ret);
  1137. return ret;
  1138. }
  1139. ret = regmap_update_bits(pcm512x->regmap, PCM512x_SYNCHRONIZE,
  1140. PCM512x_RQSY, PCM512x_RQSY_RESUME);
  1141. if (ret != 0) {
  1142. dev_err(codec->dev, "Failed to resume clocks: %d\n", ret);
  1143. return ret;
  1144. }
  1145. return 0;
  1146. }
  1147. static int pcm512x_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1148. {
  1149. struct snd_soc_codec *codec = dai->codec;
  1150. struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
  1151. pcm512x->fmt = fmt;
  1152. return 0;
  1153. }
  1154. static const struct snd_soc_dai_ops pcm512x_dai_ops = {
  1155. .startup = pcm512x_dai_startup,
  1156. .hw_params = pcm512x_hw_params,
  1157. .set_fmt = pcm512x_set_fmt,
  1158. };
  1159. static struct snd_soc_dai_driver pcm512x_dai = {
  1160. .name = "pcm512x-hifi",
  1161. .playback = {
  1162. .stream_name = "Playback",
  1163. .channels_min = 2,
  1164. .channels_max = 2,
  1165. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  1166. .rate_min = 8000,
  1167. .rate_max = 384000,
  1168. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  1169. SNDRV_PCM_FMTBIT_S24_LE |
  1170. SNDRV_PCM_FMTBIT_S32_LE
  1171. },
  1172. .ops = &pcm512x_dai_ops,
  1173. };
  1174. static struct snd_soc_codec_driver pcm512x_codec_driver = {
  1175. .set_bias_level = pcm512x_set_bias_level,
  1176. .idle_bias_off = true,
  1177. .component_driver = {
  1178. .controls = pcm512x_controls,
  1179. .num_controls = ARRAY_SIZE(pcm512x_controls),
  1180. .dapm_widgets = pcm512x_dapm_widgets,
  1181. .num_dapm_widgets = ARRAY_SIZE(pcm512x_dapm_widgets),
  1182. .dapm_routes = pcm512x_dapm_routes,
  1183. .num_dapm_routes = ARRAY_SIZE(pcm512x_dapm_routes),
  1184. },
  1185. };
  1186. static const struct regmap_range_cfg pcm512x_range = {
  1187. .name = "Pages", .range_min = PCM512x_VIRT_BASE,
  1188. .range_max = PCM512x_MAX_REGISTER,
  1189. .selector_reg = PCM512x_PAGE,
  1190. .selector_mask = 0xff,
  1191. .window_start = 0, .window_len = 0x100,
  1192. };
  1193. const struct regmap_config pcm512x_regmap = {
  1194. .reg_bits = 8,
  1195. .val_bits = 8,
  1196. .readable_reg = pcm512x_readable,
  1197. .volatile_reg = pcm512x_volatile,
  1198. .ranges = &pcm512x_range,
  1199. .num_ranges = 1,
  1200. .max_register = PCM512x_MAX_REGISTER,
  1201. .reg_defaults = pcm512x_reg_defaults,
  1202. .num_reg_defaults = ARRAY_SIZE(pcm512x_reg_defaults),
  1203. .cache_type = REGCACHE_RBTREE,
  1204. };
  1205. EXPORT_SYMBOL_GPL(pcm512x_regmap);
  1206. int pcm512x_probe(struct device *dev, struct regmap *regmap)
  1207. {
  1208. struct pcm512x_priv *pcm512x;
  1209. int i, ret;
  1210. pcm512x = devm_kzalloc(dev, sizeof(struct pcm512x_priv), GFP_KERNEL);
  1211. if (!pcm512x)
  1212. return -ENOMEM;
  1213. dev_set_drvdata(dev, pcm512x);
  1214. pcm512x->regmap = regmap;
  1215. for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++)
  1216. pcm512x->supplies[i].supply = pcm512x_supply_names[i];
  1217. ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(pcm512x->supplies),
  1218. pcm512x->supplies);
  1219. if (ret != 0) {
  1220. dev_err(dev, "Failed to get supplies: %d\n", ret);
  1221. return ret;
  1222. }
  1223. pcm512x->supply_nb[0].notifier_call = pcm512x_regulator_event_0;
  1224. pcm512x->supply_nb[1].notifier_call = pcm512x_regulator_event_1;
  1225. pcm512x->supply_nb[2].notifier_call = pcm512x_regulator_event_2;
  1226. for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++) {
  1227. ret = regulator_register_notifier(pcm512x->supplies[i].consumer,
  1228. &pcm512x->supply_nb[i]);
  1229. if (ret != 0) {
  1230. dev_err(dev,
  1231. "Failed to register regulator notifier: %d\n",
  1232. ret);
  1233. }
  1234. }
  1235. ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies),
  1236. pcm512x->supplies);
  1237. if (ret != 0) {
  1238. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  1239. return ret;
  1240. }
  1241. /* Reset the device, verifying I/O in the process for I2C */
  1242. ret = regmap_write(regmap, PCM512x_RESET,
  1243. PCM512x_RSTM | PCM512x_RSTR);
  1244. if (ret != 0) {
  1245. dev_err(dev, "Failed to reset device: %d\n", ret);
  1246. goto err;
  1247. }
  1248. ret = regmap_write(regmap, PCM512x_RESET, 0);
  1249. if (ret != 0) {
  1250. dev_err(dev, "Failed to reset device: %d\n", ret);
  1251. goto err;
  1252. }
  1253. pcm512x->sclk = devm_clk_get(dev, NULL);
  1254. if (PTR_ERR(pcm512x->sclk) == -EPROBE_DEFER)
  1255. return -EPROBE_DEFER;
  1256. if (!IS_ERR(pcm512x->sclk)) {
  1257. ret = clk_prepare_enable(pcm512x->sclk);
  1258. if (ret != 0) {
  1259. dev_err(dev, "Failed to enable SCLK: %d\n", ret);
  1260. return ret;
  1261. }
  1262. }
  1263. /* Default to standby mode */
  1264. ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  1265. PCM512x_RQST, PCM512x_RQST);
  1266. if (ret != 0) {
  1267. dev_err(dev, "Failed to request standby: %d\n",
  1268. ret);
  1269. goto err_clk;
  1270. }
  1271. pm_runtime_set_active(dev);
  1272. pm_runtime_enable(dev);
  1273. pm_runtime_idle(dev);
  1274. #ifdef CONFIG_OF
  1275. if (dev->of_node) {
  1276. const struct device_node *np = dev->of_node;
  1277. u32 val;
  1278. if (of_property_read_u32(np, "pll-in", &val) >= 0) {
  1279. if (val > 6) {
  1280. dev_err(dev, "Invalid pll-in\n");
  1281. ret = -EINVAL;
  1282. goto err_clk;
  1283. }
  1284. pcm512x->pll_in = val;
  1285. }
  1286. if (of_property_read_u32(np, "pll-out", &val) >= 0) {
  1287. if (val > 6) {
  1288. dev_err(dev, "Invalid pll-out\n");
  1289. ret = -EINVAL;
  1290. goto err_clk;
  1291. }
  1292. pcm512x->pll_out = val;
  1293. }
  1294. if (!pcm512x->pll_in != !pcm512x->pll_out) {
  1295. dev_err(dev,
  1296. "Error: both pll-in and pll-out, or none\n");
  1297. ret = -EINVAL;
  1298. goto err_clk;
  1299. }
  1300. if (pcm512x->pll_in && pcm512x->pll_in == pcm512x->pll_out) {
  1301. dev_err(dev, "Error: pll-in == pll-out\n");
  1302. ret = -EINVAL;
  1303. goto err_clk;
  1304. }
  1305. }
  1306. #endif
  1307. ret = snd_soc_register_codec(dev, &pcm512x_codec_driver,
  1308. &pcm512x_dai, 1);
  1309. if (ret != 0) {
  1310. dev_err(dev, "Failed to register CODEC: %d\n", ret);
  1311. goto err_pm;
  1312. }
  1313. return 0;
  1314. err_pm:
  1315. pm_runtime_disable(dev);
  1316. err_clk:
  1317. if (!IS_ERR(pcm512x->sclk))
  1318. clk_disable_unprepare(pcm512x->sclk);
  1319. err:
  1320. regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
  1321. pcm512x->supplies);
  1322. return ret;
  1323. }
  1324. EXPORT_SYMBOL_GPL(pcm512x_probe);
  1325. void pcm512x_remove(struct device *dev)
  1326. {
  1327. struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
  1328. snd_soc_unregister_codec(dev);
  1329. pm_runtime_disable(dev);
  1330. if (!IS_ERR(pcm512x->sclk))
  1331. clk_disable_unprepare(pcm512x->sclk);
  1332. regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
  1333. pcm512x->supplies);
  1334. }
  1335. EXPORT_SYMBOL_GPL(pcm512x_remove);
  1336. #ifdef CONFIG_PM
  1337. static int pcm512x_suspend(struct device *dev)
  1338. {
  1339. struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
  1340. int ret;
  1341. ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  1342. PCM512x_RQPD, PCM512x_RQPD);
  1343. if (ret != 0) {
  1344. dev_err(dev, "Failed to request power down: %d\n", ret);
  1345. return ret;
  1346. }
  1347. ret = regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies),
  1348. pcm512x->supplies);
  1349. if (ret != 0) {
  1350. dev_err(dev, "Failed to disable supplies: %d\n", ret);
  1351. return ret;
  1352. }
  1353. if (!IS_ERR(pcm512x->sclk))
  1354. clk_disable_unprepare(pcm512x->sclk);
  1355. return 0;
  1356. }
  1357. static int pcm512x_resume(struct device *dev)
  1358. {
  1359. struct pcm512x_priv *pcm512x = dev_get_drvdata(dev);
  1360. int ret;
  1361. if (!IS_ERR(pcm512x->sclk)) {
  1362. ret = clk_prepare_enable(pcm512x->sclk);
  1363. if (ret != 0) {
  1364. dev_err(dev, "Failed to enable SCLK: %d\n", ret);
  1365. return ret;
  1366. }
  1367. }
  1368. ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies),
  1369. pcm512x->supplies);
  1370. if (ret != 0) {
  1371. dev_err(dev, "Failed to enable supplies: %d\n", ret);
  1372. return ret;
  1373. }
  1374. regcache_cache_only(pcm512x->regmap, false);
  1375. ret = regcache_sync(pcm512x->regmap);
  1376. if (ret != 0) {
  1377. dev_err(dev, "Failed to sync cache: %d\n", ret);
  1378. return ret;
  1379. }
  1380. ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER,
  1381. PCM512x_RQPD, 0);
  1382. if (ret != 0) {
  1383. dev_err(dev, "Failed to remove power down: %d\n", ret);
  1384. return ret;
  1385. }
  1386. return 0;
  1387. }
  1388. #endif
  1389. const struct dev_pm_ops pcm512x_pm_ops = {
  1390. SET_RUNTIME_PM_OPS(pcm512x_suspend, pcm512x_resume, NULL)
  1391. };
  1392. EXPORT_SYMBOL_GPL(pcm512x_pm_ops);
  1393. MODULE_DESCRIPTION("ASoC PCM512x codec driver");
  1394. MODULE_AUTHOR("Mark Brown <broonie@linaro.org>");
  1395. MODULE_LICENSE("GPL v2");