wm8900.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /*
  2. * wm8900.c -- WM8900 ALSA Soc Audio driver
  3. *
  4. * Copyright 2007, 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * TODO:
  13. * - Tristating.
  14. * - TDM.
  15. * - Jack detect.
  16. * - FLL source configuration, currently only MCLK is supported.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/pm.h>
  24. #include <linux/i2c.h>
  25. #include <linux/platform_device.h>
  26. #include <sound/core.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/soc.h>
  30. #include <sound/soc-dapm.h>
  31. #include <sound/initval.h>
  32. #include <sound/tlv.h>
  33. #include "wm8900.h"
  34. /* WM8900 register space */
  35. #define WM8900_REG_RESET 0x0
  36. #define WM8900_REG_ID 0x0
  37. #define WM8900_REG_POWER1 0x1
  38. #define WM8900_REG_POWER2 0x2
  39. #define WM8900_REG_POWER3 0x3
  40. #define WM8900_REG_AUDIO1 0x4
  41. #define WM8900_REG_AUDIO2 0x5
  42. #define WM8900_REG_CLOCKING1 0x6
  43. #define WM8900_REG_CLOCKING2 0x7
  44. #define WM8900_REG_AUDIO3 0x8
  45. #define WM8900_REG_AUDIO4 0x9
  46. #define WM8900_REG_DACCTRL 0xa
  47. #define WM8900_REG_LDAC_DV 0xb
  48. #define WM8900_REG_RDAC_DV 0xc
  49. #define WM8900_REG_SIDETONE 0xd
  50. #define WM8900_REG_ADCCTRL 0xe
  51. #define WM8900_REG_LADC_DV 0xf
  52. #define WM8900_REG_RADC_DV 0x10
  53. #define WM8900_REG_GPIO 0x12
  54. #define WM8900_REG_INCTL 0x15
  55. #define WM8900_REG_LINVOL 0x16
  56. #define WM8900_REG_RINVOL 0x17
  57. #define WM8900_REG_INBOOSTMIX1 0x18
  58. #define WM8900_REG_INBOOSTMIX2 0x19
  59. #define WM8900_REG_ADCPATH 0x1a
  60. #define WM8900_REG_AUXBOOST 0x1b
  61. #define WM8900_REG_ADDCTL 0x1e
  62. #define WM8900_REG_FLLCTL1 0x24
  63. #define WM8900_REG_FLLCTL2 0x25
  64. #define WM8900_REG_FLLCTL3 0x26
  65. #define WM8900_REG_FLLCTL4 0x27
  66. #define WM8900_REG_FLLCTL5 0x28
  67. #define WM8900_REG_FLLCTL6 0x29
  68. #define WM8900_REG_LOUTMIXCTL1 0x2c
  69. #define WM8900_REG_ROUTMIXCTL1 0x2d
  70. #define WM8900_REG_BYPASS1 0x2e
  71. #define WM8900_REG_BYPASS2 0x2f
  72. #define WM8900_REG_AUXOUT_CTL 0x30
  73. #define WM8900_REG_LOUT1CTL 0x33
  74. #define WM8900_REG_ROUT1CTL 0x34
  75. #define WM8900_REG_LOUT2CTL 0x35
  76. #define WM8900_REG_ROUT2CTL 0x36
  77. #define WM8900_REG_HPCTL1 0x3a
  78. #define WM8900_REG_OUTBIASCTL 0x73
  79. #define WM8900_MAXREG 0x80
  80. #define WM8900_REG_ADDCTL_OUT1_DIS 0x80
  81. #define WM8900_REG_ADDCTL_OUT2_DIS 0x40
  82. #define WM8900_REG_ADDCTL_VMID_DIS 0x20
  83. #define WM8900_REG_ADDCTL_BIAS_SRC 0x10
  84. #define WM8900_REG_ADDCTL_VMID_SOFTST 0x04
  85. #define WM8900_REG_ADDCTL_TEMP_SD 0x02
  86. #define WM8900_REG_GPIO_TEMP_ENA 0x2
  87. #define WM8900_REG_POWER1_STARTUP_BIAS_ENA 0x0100
  88. #define WM8900_REG_POWER1_BIAS_ENA 0x0008
  89. #define WM8900_REG_POWER1_VMID_BUF_ENA 0x0004
  90. #define WM8900_REG_POWER1_FLL_ENA 0x0040
  91. #define WM8900_REG_POWER2_SYSCLK_ENA 0x8000
  92. #define WM8900_REG_POWER2_ADCL_ENA 0x0002
  93. #define WM8900_REG_POWER2_ADCR_ENA 0x0001
  94. #define WM8900_REG_POWER3_DACL_ENA 0x0002
  95. #define WM8900_REG_POWER3_DACR_ENA 0x0001
  96. #define WM8900_REG_AUDIO1_AIF_FMT_MASK 0x0018
  97. #define WM8900_REG_AUDIO1_LRCLK_INV 0x0080
  98. #define WM8900_REG_AUDIO1_BCLK_INV 0x0100
  99. #define WM8900_REG_CLOCKING1_BCLK_DIR 0x1
  100. #define WM8900_REG_CLOCKING1_MCLK_SRC 0x100
  101. #define WM8900_REG_CLOCKING1_BCLK_MASK (~0x01e)
  102. #define WM8900_REG_CLOCKING1_OPCLK_MASK (~0x7000)
  103. #define WM8900_REG_CLOCKING2_ADC_CLKDIV 0xe0
  104. #define WM8900_REG_CLOCKING2_DAC_CLKDIV 0x1c
  105. #define WM8900_REG_DACCTRL_MUTE 0x004
  106. #define WM8900_REG_DACCTRL_AIF_LRCLKRATE 0x400
  107. #define WM8900_REG_AUDIO3_ADCLRC_DIR 0x0800
  108. #define WM8900_REG_AUDIO4_DACLRC_DIR 0x0800
  109. #define WM8900_REG_FLLCTL1_OSC_ENA 0x100
  110. #define WM8900_REG_FLLCTL6_FLL_SLOW_LOCK_REF 0x100
  111. #define WM8900_REG_HPCTL1_HP_IPSTAGE_ENA 0x80
  112. #define WM8900_REG_HPCTL1_HP_OPSTAGE_ENA 0x40
  113. #define WM8900_REG_HPCTL1_HP_CLAMP_IP 0x20
  114. #define WM8900_REG_HPCTL1_HP_CLAMP_OP 0x10
  115. #define WM8900_REG_HPCTL1_HP_SHORT 0x08
  116. #define WM8900_REG_HPCTL1_HP_SHORT2 0x04
  117. #define WM8900_LRC_MASK 0xfc00
  118. struct snd_soc_codec_device soc_codec_dev_wm8900;
  119. struct wm8900_priv {
  120. u32 fll_in; /* FLL input frequency */
  121. u32 fll_out; /* FLL output frequency */
  122. };
  123. /*
  124. * wm8900 register cache. We can't read the entire register space and we
  125. * have slow control buses so we cache the registers.
  126. */
  127. static const u16 wm8900_reg_defaults[WM8900_MAXREG] = {
  128. 0x8900, 0x0000,
  129. 0xc000, 0x0000,
  130. 0x4050, 0x4000,
  131. 0x0008, 0x0000,
  132. 0x0040, 0x0040,
  133. 0x1004, 0x00c0,
  134. 0x00c0, 0x0000,
  135. 0x0100, 0x00c0,
  136. 0x00c0, 0x0000,
  137. 0xb001, 0x0000,
  138. 0x0000, 0x0044,
  139. 0x004c, 0x004c,
  140. 0x0044, 0x0044,
  141. 0x0000, 0x0044,
  142. 0x0000, 0x0000,
  143. 0x0002, 0x0000,
  144. 0x0000, 0x0000,
  145. 0x0000, 0x0000,
  146. 0x0008, 0x0000,
  147. 0x0000, 0x0008,
  148. 0x0097, 0x0100,
  149. 0x0000, 0x0000,
  150. 0x0050, 0x0050,
  151. 0x0055, 0x0055,
  152. 0x0055, 0x0000,
  153. 0x0000, 0x0079,
  154. 0x0079, 0x0079,
  155. 0x0079, 0x0000,
  156. /* Remaining registers all zero */
  157. };
  158. /*
  159. * read wm8900 register cache
  160. */
  161. static inline unsigned int wm8900_read_reg_cache(struct snd_soc_codec *codec,
  162. unsigned int reg)
  163. {
  164. u16 *cache = codec->reg_cache;
  165. BUG_ON(reg >= WM8900_MAXREG);
  166. if (reg == WM8900_REG_ID)
  167. return 0;
  168. return cache[reg];
  169. }
  170. /*
  171. * write wm8900 register cache
  172. */
  173. static inline void wm8900_write_reg_cache(struct snd_soc_codec *codec,
  174. u16 reg, unsigned int value)
  175. {
  176. u16 *cache = codec->reg_cache;
  177. BUG_ON(reg >= WM8900_MAXREG);
  178. cache[reg] = value;
  179. }
  180. /*
  181. * write to the WM8900 register space
  182. */
  183. static int wm8900_write(struct snd_soc_codec *codec, unsigned int reg,
  184. unsigned int value)
  185. {
  186. u8 data[3];
  187. if (value == wm8900_read_reg_cache(codec, reg))
  188. return 0;
  189. /* data is
  190. * D15..D9 WM8900 register offset
  191. * D8...D0 register data
  192. */
  193. data[0] = reg;
  194. data[1] = value >> 8;
  195. data[2] = value & 0x00ff;
  196. wm8900_write_reg_cache(codec, reg, value);
  197. if (codec->hw_write(codec->control_data, data, 3) == 3)
  198. return 0;
  199. else
  200. return -EIO;
  201. }
  202. /*
  203. * Read from the wm8900.
  204. */
  205. static unsigned int wm8900_chip_read(struct snd_soc_codec *codec, u8 reg)
  206. {
  207. struct i2c_msg xfer[2];
  208. u16 data;
  209. int ret;
  210. struct i2c_client *client = codec->control_data;
  211. BUG_ON(reg != WM8900_REG_ID && reg != WM8900_REG_POWER1);
  212. /* Write register */
  213. xfer[0].addr = client->addr;
  214. xfer[0].flags = 0;
  215. xfer[0].len = 1;
  216. xfer[0].buf = &reg;
  217. /* Read data */
  218. xfer[1].addr = client->addr;
  219. xfer[1].flags = I2C_M_RD;
  220. xfer[1].len = 2;
  221. xfer[1].buf = (u8 *)&data;
  222. ret = i2c_transfer(client->adapter, xfer, 2);
  223. if (ret != 2) {
  224. printk(KERN_CRIT "i2c_transfer returned %d\n", ret);
  225. return 0;
  226. }
  227. return (data >> 8) | ((data & 0xff) << 8);
  228. }
  229. /*
  230. * Read from the WM8900 register space. Most registers can't be read
  231. * and are therefore supplied from cache.
  232. */
  233. static unsigned int wm8900_read(struct snd_soc_codec *codec, unsigned int reg)
  234. {
  235. switch (reg) {
  236. case WM8900_REG_ID:
  237. return wm8900_chip_read(codec, reg);
  238. default:
  239. return wm8900_read_reg_cache(codec, reg);
  240. }
  241. }
  242. static void wm8900_reset(struct snd_soc_codec *codec)
  243. {
  244. wm8900_write(codec, WM8900_REG_RESET, 0);
  245. memcpy(codec->reg_cache, wm8900_reg_defaults,
  246. sizeof(codec->reg_cache));
  247. }
  248. static int wm8900_hp_event(struct snd_soc_dapm_widget *w,
  249. struct snd_kcontrol *kcontrol, int event)
  250. {
  251. struct snd_soc_codec *codec = w->codec;
  252. u16 hpctl1 = wm8900_read(codec, WM8900_REG_HPCTL1);
  253. switch (event) {
  254. case SND_SOC_DAPM_PRE_PMU:
  255. /* Clamp headphone outputs */
  256. hpctl1 = WM8900_REG_HPCTL1_HP_CLAMP_IP |
  257. WM8900_REG_HPCTL1_HP_CLAMP_OP;
  258. wm8900_write(codec, WM8900_REG_HPCTL1, hpctl1);
  259. break;
  260. case SND_SOC_DAPM_POST_PMU:
  261. /* Enable the input stage */
  262. hpctl1 &= ~WM8900_REG_HPCTL1_HP_CLAMP_IP;
  263. hpctl1 |= WM8900_REG_HPCTL1_HP_SHORT |
  264. WM8900_REG_HPCTL1_HP_SHORT2 |
  265. WM8900_REG_HPCTL1_HP_IPSTAGE_ENA;
  266. wm8900_write(codec, WM8900_REG_HPCTL1, hpctl1);
  267. msleep(400);
  268. /* Enable the output stage */
  269. hpctl1 &= ~WM8900_REG_HPCTL1_HP_CLAMP_OP;
  270. hpctl1 |= WM8900_REG_HPCTL1_HP_OPSTAGE_ENA;
  271. wm8900_write(codec, WM8900_REG_HPCTL1, hpctl1);
  272. /* Remove the shorts */
  273. hpctl1 &= ~WM8900_REG_HPCTL1_HP_SHORT2;
  274. wm8900_write(codec, WM8900_REG_HPCTL1, hpctl1);
  275. hpctl1 &= ~WM8900_REG_HPCTL1_HP_SHORT;
  276. wm8900_write(codec, WM8900_REG_HPCTL1, hpctl1);
  277. break;
  278. case SND_SOC_DAPM_PRE_PMD:
  279. /* Short the output */
  280. hpctl1 |= WM8900_REG_HPCTL1_HP_SHORT;
  281. wm8900_write(codec, WM8900_REG_HPCTL1, hpctl1);
  282. /* Disable the output stage */
  283. hpctl1 &= ~WM8900_REG_HPCTL1_HP_OPSTAGE_ENA;
  284. wm8900_write(codec, WM8900_REG_HPCTL1, hpctl1);
  285. /* Clamp the outputs and power down input */
  286. hpctl1 |= WM8900_REG_HPCTL1_HP_CLAMP_IP |
  287. WM8900_REG_HPCTL1_HP_CLAMP_OP;
  288. hpctl1 &= ~WM8900_REG_HPCTL1_HP_IPSTAGE_ENA;
  289. wm8900_write(codec, WM8900_REG_HPCTL1, hpctl1);
  290. break;
  291. case SND_SOC_DAPM_POST_PMD:
  292. /* Disable everything */
  293. wm8900_write(codec, WM8900_REG_HPCTL1, 0);
  294. break;
  295. default:
  296. BUG();
  297. }
  298. return 0;
  299. }
  300. static const DECLARE_TLV_DB_SCALE(out_pga_tlv, -5700, 100, 0);
  301. static const DECLARE_TLV_DB_SCALE(out_mix_tlv, -1500, 300, 0);
  302. static const DECLARE_TLV_DB_SCALE(in_boost_tlv, -1200, 600, 0);
  303. static const DECLARE_TLV_DB_SCALE(in_pga_tlv, -1200, 100, 0);
  304. static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
  305. static const DECLARE_TLV_DB_SCALE(dac_tlv, -7200, 75, 1);
  306. static const DECLARE_TLV_DB_SCALE(adc_svol_tlv, -3600, 300, 0);
  307. static const DECLARE_TLV_DB_SCALE(adc_tlv, -7200, 75, 1);
  308. static const char *mic_bias_level_txt[] = { "0.9*AVDD", "0.65*AVDD" };
  309. static const struct soc_enum mic_bias_level =
  310. SOC_ENUM_SINGLE(WM8900_REG_INCTL, 8, 2, mic_bias_level_txt);
  311. static const char *dac_mute_rate_txt[] = { "Fast", "Slow" };
  312. static const struct soc_enum dac_mute_rate =
  313. SOC_ENUM_SINGLE(WM8900_REG_DACCTRL, 7, 2, dac_mute_rate_txt);
  314. static const char *dac_deemphasis_txt[] = {
  315. "Disabled", "32kHz", "44.1kHz", "48kHz"
  316. };
  317. static const struct soc_enum dac_deemphasis =
  318. SOC_ENUM_SINGLE(WM8900_REG_DACCTRL, 4, 4, dac_deemphasis_txt);
  319. static const char *adc_hpf_cut_txt[] = {
  320. "Hi-fi mode", "Voice mode 1", "Voice mode 2", "Voice mode 3"
  321. };
  322. static const struct soc_enum adc_hpf_cut =
  323. SOC_ENUM_SINGLE(WM8900_REG_ADCCTRL, 5, 4, adc_hpf_cut_txt);
  324. static const char *lr_txt[] = {
  325. "Left", "Right"
  326. };
  327. static const struct soc_enum aifl_src =
  328. SOC_ENUM_SINGLE(WM8900_REG_AUDIO1, 15, 2, lr_txt);
  329. static const struct soc_enum aifr_src =
  330. SOC_ENUM_SINGLE(WM8900_REG_AUDIO1, 14, 2, lr_txt);
  331. static const struct soc_enum dacl_src =
  332. SOC_ENUM_SINGLE(WM8900_REG_AUDIO2, 15, 2, lr_txt);
  333. static const struct soc_enum dacr_src =
  334. SOC_ENUM_SINGLE(WM8900_REG_AUDIO2, 14, 2, lr_txt);
  335. static const char *sidetone_txt[] = {
  336. "Disabled", "Left ADC", "Right ADC"
  337. };
  338. static const struct soc_enum dacl_sidetone =
  339. SOC_ENUM_SINGLE(WM8900_REG_SIDETONE, 2, 3, sidetone_txt);
  340. static const struct soc_enum dacr_sidetone =
  341. SOC_ENUM_SINGLE(WM8900_REG_SIDETONE, 0, 3, sidetone_txt);
  342. static const struct snd_kcontrol_new wm8900_snd_controls[] = {
  343. SOC_ENUM("Mic Bias Level", mic_bias_level),
  344. SOC_SINGLE_TLV("Left Input PGA Volume", WM8900_REG_LINVOL, 0, 31, 0,
  345. in_pga_tlv),
  346. SOC_SINGLE("Left Input PGA Switch", WM8900_REG_LINVOL, 6, 1, 1),
  347. SOC_SINGLE("Left Input PGA ZC Switch", WM8900_REG_LINVOL, 7, 1, 0),
  348. SOC_SINGLE_TLV("Right Input PGA Volume", WM8900_REG_RINVOL, 0, 31, 0,
  349. in_pga_tlv),
  350. SOC_SINGLE("Right Input PGA Switch", WM8900_REG_RINVOL, 6, 1, 1),
  351. SOC_SINGLE("Right Input PGA ZC Switch", WM8900_REG_RINVOL, 7, 1, 0),
  352. SOC_SINGLE("DAC Soft Mute Switch", WM8900_REG_DACCTRL, 6, 1, 1),
  353. SOC_ENUM("DAC Mute Rate", dac_mute_rate),
  354. SOC_SINGLE("DAC Mono Switch", WM8900_REG_DACCTRL, 9, 1, 0),
  355. SOC_ENUM("DAC Deemphasis", dac_deemphasis),
  356. SOC_SINGLE("DAC Sloping Stopband Filter Switch", WM8900_REG_DACCTRL, 8, 1, 0),
  357. SOC_SINGLE("DAC Sigma-Delta Modulator Clock Switch", WM8900_REG_DACCTRL,
  358. 12, 1, 0),
  359. SOC_SINGLE("ADC HPF Switch", WM8900_REG_ADCCTRL, 8, 1, 0),
  360. SOC_ENUM("ADC HPF Cut-Off", adc_hpf_cut),
  361. SOC_DOUBLE("ADC Invert Switch", WM8900_REG_ADCCTRL, 1, 0, 1, 0),
  362. SOC_SINGLE_TLV("Left ADC Sidetone Volume", WM8900_REG_SIDETONE, 9, 12, 0,
  363. adc_svol_tlv),
  364. SOC_SINGLE_TLV("Right ADC Sidetone Volume", WM8900_REG_SIDETONE, 5, 12, 0,
  365. adc_svol_tlv),
  366. SOC_ENUM("Left Digital Audio Source", aifl_src),
  367. SOC_ENUM("Right Digital Audio Source", aifr_src),
  368. SOC_SINGLE_TLV("DAC Input Boost Volume", WM8900_REG_AUDIO2, 10, 4, 0,
  369. dac_boost_tlv),
  370. SOC_ENUM("Left DAC Source", dacl_src),
  371. SOC_ENUM("Right DAC Source", dacr_src),
  372. SOC_ENUM("Left DAC Sidetone", dacl_sidetone),
  373. SOC_ENUM("Right DAC Sidetone", dacr_sidetone),
  374. SOC_DOUBLE("DAC Invert Switch", WM8900_REG_DACCTRL, 1, 0, 1, 0),
  375. SOC_DOUBLE_R_TLV("Digital Playback Volume",
  376. WM8900_REG_LDAC_DV, WM8900_REG_RDAC_DV,
  377. 1, 96, 0, dac_tlv),
  378. SOC_DOUBLE_R_TLV("Digital Capture Volume",
  379. WM8900_REG_LADC_DV, WM8900_REG_RADC_DV, 1, 119, 0, adc_tlv),
  380. SOC_SINGLE_TLV("LINPUT3 Bypass Volume", WM8900_REG_LOUTMIXCTL1, 4, 7, 0,
  381. out_mix_tlv),
  382. SOC_SINGLE_TLV("RINPUT3 Bypass Volume", WM8900_REG_ROUTMIXCTL1, 4, 7, 0,
  383. out_mix_tlv),
  384. SOC_SINGLE_TLV("Left AUX Bypass Volume", WM8900_REG_AUXOUT_CTL, 4, 7, 0,
  385. out_mix_tlv),
  386. SOC_SINGLE_TLV("Right AUX Bypass Volume", WM8900_REG_AUXOUT_CTL, 0, 7, 0,
  387. out_mix_tlv),
  388. SOC_SINGLE_TLV("LeftIn to RightOut Mixer Volume", WM8900_REG_BYPASS1, 0, 7, 0,
  389. out_mix_tlv),
  390. SOC_SINGLE_TLV("LeftIn to LeftOut Mixer Volume", WM8900_REG_BYPASS1, 4, 7, 0,
  391. out_mix_tlv),
  392. SOC_SINGLE_TLV("RightIn to LeftOut Mixer Volume", WM8900_REG_BYPASS2, 0, 7, 0,
  393. out_mix_tlv),
  394. SOC_SINGLE_TLV("RightIn to RightOut Mixer Volume", WM8900_REG_BYPASS2, 4, 7, 0,
  395. out_mix_tlv),
  396. SOC_SINGLE_TLV("IN2L Boost Volume", WM8900_REG_INBOOSTMIX1, 0, 3, 0,
  397. in_boost_tlv),
  398. SOC_SINGLE_TLV("IN3L Boost Volume", WM8900_REG_INBOOSTMIX1, 4, 3, 0,
  399. in_boost_tlv),
  400. SOC_SINGLE_TLV("IN2R Boost Volume", WM8900_REG_INBOOSTMIX2, 0, 3, 0,
  401. in_boost_tlv),
  402. SOC_SINGLE_TLV("IN3R Boost Volume", WM8900_REG_INBOOSTMIX2, 4, 3, 0,
  403. in_boost_tlv),
  404. SOC_SINGLE_TLV("Left AUX Boost Volume", WM8900_REG_AUXBOOST, 4, 3, 0,
  405. in_boost_tlv),
  406. SOC_SINGLE_TLV("Right AUX Boost Volume", WM8900_REG_AUXBOOST, 0, 3, 0,
  407. in_boost_tlv),
  408. SOC_DOUBLE_R_TLV("LINEOUT1 Volume", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
  409. 0, 63, 0, out_pga_tlv),
  410. SOC_DOUBLE_R("LINEOUT1 Switch", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
  411. 6, 1, 1),
  412. SOC_DOUBLE_R("LINEOUT1 ZC Switch", WM8900_REG_LOUT1CTL, WM8900_REG_ROUT1CTL,
  413. 7, 1, 0),
  414. SOC_DOUBLE_R_TLV("LINEOUT2 Volume",
  415. WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL,
  416. 0, 63, 0, out_pga_tlv),
  417. SOC_DOUBLE_R("LINEOUT2 Switch",
  418. WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL, 6, 1, 1),
  419. SOC_DOUBLE_R("LINEOUT2 ZC Switch",
  420. WM8900_REG_LOUT2CTL, WM8900_REG_ROUT2CTL, 7, 1, 0),
  421. SOC_SINGLE("LINEOUT2 LP -12dB", WM8900_REG_LOUTMIXCTL1,
  422. 0, 1, 1),
  423. };
  424. /* add non dapm controls */
  425. static int wm8900_add_controls(struct snd_soc_codec *codec)
  426. {
  427. int err, i;
  428. for (i = 0; i < ARRAY_SIZE(wm8900_snd_controls); i++) {
  429. err = snd_ctl_add(codec->card,
  430. snd_soc_cnew(&wm8900_snd_controls[i],
  431. codec, NULL));
  432. if (err < 0)
  433. return err;
  434. }
  435. return 0;
  436. }
  437. static const struct snd_kcontrol_new wm8900_dapm_loutput2_control =
  438. SOC_DAPM_SINGLE("LINEOUT2L Switch", WM8900_REG_POWER3, 6, 1, 0);
  439. static const struct snd_kcontrol_new wm8900_dapm_routput2_control =
  440. SOC_DAPM_SINGLE("LINEOUT2R Switch", WM8900_REG_POWER3, 5, 1, 0);
  441. static const struct snd_kcontrol_new wm8900_loutmix_controls[] = {
  442. SOC_DAPM_SINGLE("LINPUT3 Bypass Switch", WM8900_REG_LOUTMIXCTL1, 7, 1, 0),
  443. SOC_DAPM_SINGLE("AUX Bypass Switch", WM8900_REG_AUXOUT_CTL, 7, 1, 0),
  444. SOC_DAPM_SINGLE("Left Input Mixer Switch", WM8900_REG_BYPASS1, 7, 1, 0),
  445. SOC_DAPM_SINGLE("Right Input Mixer Switch", WM8900_REG_BYPASS2, 3, 1, 0),
  446. SOC_DAPM_SINGLE("DACL Switch", WM8900_REG_LOUTMIXCTL1, 8, 1, 0),
  447. };
  448. static const struct snd_kcontrol_new wm8900_routmix_controls[] = {
  449. SOC_DAPM_SINGLE("RINPUT3 Bypass Switch", WM8900_REG_ROUTMIXCTL1, 7, 1, 0),
  450. SOC_DAPM_SINGLE("AUX Bypass Switch", WM8900_REG_AUXOUT_CTL, 3, 1, 0),
  451. SOC_DAPM_SINGLE("Left Input Mixer Switch", WM8900_REG_BYPASS1, 3, 1, 0),
  452. SOC_DAPM_SINGLE("Right Input Mixer Switch", WM8900_REG_BYPASS2, 7, 1, 0),
  453. SOC_DAPM_SINGLE("DACR Switch", WM8900_REG_ROUTMIXCTL1, 8, 1, 0),
  454. };
  455. static const struct snd_kcontrol_new wm8900_linmix_controls[] = {
  456. SOC_DAPM_SINGLE("LINPUT2 Switch", WM8900_REG_INBOOSTMIX1, 2, 1, 1),
  457. SOC_DAPM_SINGLE("LINPUT3 Switch", WM8900_REG_INBOOSTMIX1, 6, 1, 1),
  458. SOC_DAPM_SINGLE("AUX Switch", WM8900_REG_AUXBOOST, 6, 1, 1),
  459. SOC_DAPM_SINGLE("Input PGA Switch", WM8900_REG_ADCPATH, 6, 1, 0),
  460. };
  461. static const struct snd_kcontrol_new wm8900_rinmix_controls[] = {
  462. SOC_DAPM_SINGLE("RINPUT2 Switch", WM8900_REG_INBOOSTMIX2, 2, 1, 1),
  463. SOC_DAPM_SINGLE("RINPUT3 Switch", WM8900_REG_INBOOSTMIX2, 6, 1, 1),
  464. SOC_DAPM_SINGLE("AUX Switch", WM8900_REG_AUXBOOST, 2, 1, 1),
  465. SOC_DAPM_SINGLE("Input PGA Switch", WM8900_REG_ADCPATH, 2, 1, 0),
  466. };
  467. static const struct snd_kcontrol_new wm8900_linpga_controls[] = {
  468. SOC_DAPM_SINGLE("LINPUT1 Switch", WM8900_REG_INCTL, 6, 1, 0),
  469. SOC_DAPM_SINGLE("LINPUT2 Switch", WM8900_REG_INCTL, 5, 1, 0),
  470. SOC_DAPM_SINGLE("LINPUT3 Switch", WM8900_REG_INCTL, 4, 1, 0),
  471. };
  472. static const struct snd_kcontrol_new wm8900_rinpga_controls[] = {
  473. SOC_DAPM_SINGLE("RINPUT1 Switch", WM8900_REG_INCTL, 2, 1, 0),
  474. SOC_DAPM_SINGLE("RINPUT2 Switch", WM8900_REG_INCTL, 1, 1, 0),
  475. SOC_DAPM_SINGLE("RINPUT3 Switch", WM8900_REG_INCTL, 0, 1, 0),
  476. };
  477. static const char *wm9700_lp_mux[] = { "Disabled", "Enabled" };
  478. static const struct soc_enum wm8900_lineout2_lp_mux =
  479. SOC_ENUM_SINGLE(WM8900_REG_LOUTMIXCTL1, 1, 2, wm9700_lp_mux);
  480. static const struct snd_kcontrol_new wm8900_lineout2_lp =
  481. SOC_DAPM_ENUM("Route", wm8900_lineout2_lp_mux);
  482. static const struct snd_soc_dapm_widget wm8900_dapm_widgets[] = {
  483. /* Externally visible pins */
  484. SND_SOC_DAPM_OUTPUT("LINEOUT1L"),
  485. SND_SOC_DAPM_OUTPUT("LINEOUT1R"),
  486. SND_SOC_DAPM_OUTPUT("LINEOUT2L"),
  487. SND_SOC_DAPM_OUTPUT("LINEOUT2R"),
  488. SND_SOC_DAPM_OUTPUT("HP_L"),
  489. SND_SOC_DAPM_OUTPUT("HP_R"),
  490. SND_SOC_DAPM_INPUT("RINPUT1"),
  491. SND_SOC_DAPM_INPUT("LINPUT1"),
  492. SND_SOC_DAPM_INPUT("RINPUT2"),
  493. SND_SOC_DAPM_INPUT("LINPUT2"),
  494. SND_SOC_DAPM_INPUT("RINPUT3"),
  495. SND_SOC_DAPM_INPUT("LINPUT3"),
  496. SND_SOC_DAPM_INPUT("AUX"),
  497. SND_SOC_DAPM_VMID("VMID"),
  498. /* Input */
  499. SND_SOC_DAPM_MIXER("Left Input PGA", WM8900_REG_POWER2, 3, 0,
  500. wm8900_linpga_controls,
  501. ARRAY_SIZE(wm8900_linpga_controls)),
  502. SND_SOC_DAPM_MIXER("Right Input PGA", WM8900_REG_POWER2, 2, 0,
  503. wm8900_rinpga_controls,
  504. ARRAY_SIZE(wm8900_rinpga_controls)),
  505. SND_SOC_DAPM_MIXER("Left Input Mixer", WM8900_REG_POWER2, 5, 0,
  506. wm8900_linmix_controls,
  507. ARRAY_SIZE(wm8900_linmix_controls)),
  508. SND_SOC_DAPM_MIXER("Right Input Mixer", WM8900_REG_POWER2, 4, 0,
  509. wm8900_rinmix_controls,
  510. ARRAY_SIZE(wm8900_rinmix_controls)),
  511. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8900_REG_POWER1, 4, 0),
  512. SND_SOC_DAPM_ADC("ADCL", "Left HiFi Capture", WM8900_REG_POWER2, 1, 0),
  513. SND_SOC_DAPM_ADC("ADCR", "Right HiFi Capture", WM8900_REG_POWER2, 0, 0),
  514. /* Output */
  515. SND_SOC_DAPM_DAC("DACL", "Left HiFi Playback", WM8900_REG_POWER3, 1, 0),
  516. SND_SOC_DAPM_DAC("DACR", "Right HiFi Playback", WM8900_REG_POWER3, 0, 0),
  517. SND_SOC_DAPM_PGA_E("Headphone Amplifier", WM8900_REG_POWER3, 7, 0, NULL, 0,
  518. wm8900_hp_event,
  519. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  520. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  521. SND_SOC_DAPM_PGA("LINEOUT1L PGA", WM8900_REG_POWER2, 8, 0, NULL, 0),
  522. SND_SOC_DAPM_PGA("LINEOUT1R PGA", WM8900_REG_POWER2, 7, 0, NULL, 0),
  523. SND_SOC_DAPM_MUX("LINEOUT2 LP", SND_SOC_NOPM, 0, 0, &wm8900_lineout2_lp),
  524. SND_SOC_DAPM_PGA("LINEOUT2L PGA", WM8900_REG_POWER3, 6, 0, NULL, 0),
  525. SND_SOC_DAPM_PGA("LINEOUT2R PGA", WM8900_REG_POWER3, 5, 0, NULL, 0),
  526. SND_SOC_DAPM_MIXER("Left Output Mixer", WM8900_REG_POWER3, 3, 0,
  527. wm8900_loutmix_controls,
  528. ARRAY_SIZE(wm8900_loutmix_controls)),
  529. SND_SOC_DAPM_MIXER("Right Output Mixer", WM8900_REG_POWER3, 2, 0,
  530. wm8900_routmix_controls,
  531. ARRAY_SIZE(wm8900_routmix_controls)),
  532. };
  533. /* Target, Path, Source */
  534. static const struct snd_soc_dapm_route audio_map[] = {
  535. /* Inputs */
  536. {"Left Input PGA", "LINPUT1 Switch", "LINPUT1"},
  537. {"Left Input PGA", "LINPUT2 Switch", "LINPUT2"},
  538. {"Left Input PGA", "LINPUT3 Switch", "LINPUT3"},
  539. {"Right Input PGA", "RINPUT1 Switch", "RINPUT1"},
  540. {"Right Input PGA", "RINPUT2 Switch", "RINPUT2"},
  541. {"Right Input PGA", "RINPUT3 Switch", "RINPUT3"},
  542. {"Left Input Mixer", "LINPUT2 Switch", "LINPUT2"},
  543. {"Left Input Mixer", "LINPUT3 Switch", "LINPUT3"},
  544. {"Left Input Mixer", "AUX Switch", "AUX"},
  545. {"Left Input Mixer", "Input PGA Switch", "Left Input PGA"},
  546. {"Right Input Mixer", "RINPUT2 Switch", "RINPUT2"},
  547. {"Right Input Mixer", "RINPUT3 Switch", "RINPUT3"},
  548. {"Right Input Mixer", "AUX Switch", "AUX"},
  549. {"Right Input Mixer", "Input PGA Switch", "Right Input PGA"},
  550. {"ADCL", NULL, "Left Input Mixer"},
  551. {"ADCR", NULL, "Right Input Mixer"},
  552. /* Outputs */
  553. {"LINEOUT1L", NULL, "LINEOUT1L PGA"},
  554. {"LINEOUT1L PGA", NULL, "Left Output Mixer"},
  555. {"LINEOUT1R", NULL, "LINEOUT1R PGA"},
  556. {"LINEOUT1R PGA", NULL, "Right Output Mixer"},
  557. {"LINEOUT2L PGA", NULL, "Left Output Mixer"},
  558. {"LINEOUT2 LP", "Disabled", "LINEOUT2L PGA"},
  559. {"LINEOUT2 LP", "Enabled", "Left Output Mixer"},
  560. {"LINEOUT2L", NULL, "LINEOUT2 LP"},
  561. {"LINEOUT2R PGA", NULL, "Right Output Mixer"},
  562. {"LINEOUT2 LP", "Disabled", "LINEOUT2R PGA"},
  563. {"LINEOUT2 LP", "Enabled", "Right Output Mixer"},
  564. {"LINEOUT2R", NULL, "LINEOUT2 LP"},
  565. {"Left Output Mixer", "LINPUT3 Bypass Switch", "LINPUT3"},
  566. {"Left Output Mixer", "AUX Bypass Switch", "AUX"},
  567. {"Left Output Mixer", "Left Input Mixer Switch", "Left Input Mixer"},
  568. {"Left Output Mixer", "Right Input Mixer Switch", "Right Input Mixer"},
  569. {"Left Output Mixer", "DACL Switch", "DACL"},
  570. {"Right Output Mixer", "RINPUT3 Bypass Switch", "RINPUT3"},
  571. {"Right Output Mixer", "AUX Bypass Switch", "AUX"},
  572. {"Right Output Mixer", "Left Input Mixer Switch", "Left Input Mixer"},
  573. {"Right Output Mixer", "Right Input Mixer Switch", "Right Input Mixer"},
  574. {"Right Output Mixer", "DACR Switch", "DACR"},
  575. /* Note that the headphone output stage needs to be connected
  576. * externally to LINEOUT2 via DC blocking capacitors. Other
  577. * configurations are not supported.
  578. *
  579. * Note also that left and right headphone paths are treated as a
  580. * mono path.
  581. */
  582. {"Headphone Amplifier", NULL, "LINEOUT2 LP"},
  583. {"Headphone Amplifier", NULL, "LINEOUT2 LP"},
  584. {"HP_L", NULL, "Headphone Amplifier"},
  585. {"HP_R", NULL, "Headphone Amplifier"},
  586. };
  587. static int wm8900_add_widgets(struct snd_soc_codec *codec)
  588. {
  589. snd_soc_dapm_new_controls(codec, wm8900_dapm_widgets,
  590. ARRAY_SIZE(wm8900_dapm_widgets));
  591. snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
  592. snd_soc_dapm_new_widgets(codec);
  593. return 0;
  594. }
  595. static int wm8900_hw_params(struct snd_pcm_substream *substream,
  596. struct snd_pcm_hw_params *params,
  597. struct snd_soc_dai *dai)
  598. {
  599. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  600. struct snd_soc_device *socdev = rtd->socdev;
  601. struct snd_soc_codec *codec = socdev->codec;
  602. u16 reg;
  603. reg = wm8900_read(codec, WM8900_REG_AUDIO1) & ~0x60;
  604. switch (params_format(params)) {
  605. case SNDRV_PCM_FORMAT_S16_LE:
  606. break;
  607. case SNDRV_PCM_FORMAT_S20_3LE:
  608. reg |= 0x20;
  609. break;
  610. case SNDRV_PCM_FORMAT_S24_LE:
  611. reg |= 0x40;
  612. break;
  613. case SNDRV_PCM_FORMAT_S32_LE:
  614. reg |= 0x60;
  615. break;
  616. default:
  617. return -EINVAL;
  618. }
  619. wm8900_write(codec, WM8900_REG_AUDIO1, reg);
  620. return 0;
  621. }
  622. /* FLL divisors */
  623. struct _fll_div {
  624. u16 fll_ratio;
  625. u16 fllclk_div;
  626. u16 fll_slow_lock_ref;
  627. u16 n;
  628. u16 k;
  629. };
  630. /* The size in bits of the FLL divide multiplied by 10
  631. * to allow rounding later */
  632. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  633. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  634. unsigned int Fout)
  635. {
  636. u64 Kpart;
  637. unsigned int K, Ndiv, Nmod, target;
  638. unsigned int div;
  639. BUG_ON(!Fout);
  640. /* The FLL must run at 90-100MHz which is then scaled down to
  641. * the output value by FLLCLK_DIV. */
  642. target = Fout;
  643. div = 1;
  644. while (target < 90000000) {
  645. div *= 2;
  646. target *= 2;
  647. }
  648. if (target > 100000000)
  649. printk(KERN_WARNING "wm8900: FLL rate %d out of range, Fref=%d"
  650. " Fout=%d\n", target, Fref, Fout);
  651. if (div > 32) {
  652. printk(KERN_ERR "wm8900: Invalid FLL division rate %u, "
  653. "Fref=%d, Fout=%d, target=%d\n",
  654. div, Fref, Fout, target);
  655. return -EINVAL;
  656. }
  657. fll_div->fllclk_div = div >> 2;
  658. if (Fref < 48000)
  659. fll_div->fll_slow_lock_ref = 1;
  660. else
  661. fll_div->fll_slow_lock_ref = 0;
  662. Ndiv = target / Fref;
  663. if (Fref < 1000000)
  664. fll_div->fll_ratio = 8;
  665. else
  666. fll_div->fll_ratio = 1;
  667. fll_div->n = Ndiv / fll_div->fll_ratio;
  668. Nmod = (target / fll_div->fll_ratio) % Fref;
  669. /* Calculate fractional part - scale up so we can round. */
  670. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  671. do_div(Kpart, Fref);
  672. K = Kpart & 0xFFFFFFFF;
  673. if ((K % 10) >= 5)
  674. K += 5;
  675. /* Move down to proper range now rounding is done */
  676. fll_div->k = K / 10;
  677. BUG_ON(target != Fout * (fll_div->fllclk_div << 2));
  678. BUG_ON(!K && target != Fref * fll_div->fll_ratio * fll_div->n);
  679. return 0;
  680. }
  681. static int wm8900_set_fll(struct snd_soc_codec *codec,
  682. int fll_id, unsigned int freq_in, unsigned int freq_out)
  683. {
  684. struct wm8900_priv *wm8900 = codec->private_data;
  685. struct _fll_div fll_div;
  686. unsigned int reg;
  687. if (wm8900->fll_in == freq_in && wm8900->fll_out == freq_out)
  688. return 0;
  689. /* The digital side should be disabled during any change. */
  690. reg = wm8900_read(codec, WM8900_REG_POWER1);
  691. wm8900_write(codec, WM8900_REG_POWER1,
  692. reg & (~WM8900_REG_POWER1_FLL_ENA));
  693. /* Disable the FLL? */
  694. if (!freq_in || !freq_out) {
  695. reg = wm8900_read(codec, WM8900_REG_CLOCKING1);
  696. wm8900_write(codec, WM8900_REG_CLOCKING1,
  697. reg & (~WM8900_REG_CLOCKING1_MCLK_SRC));
  698. reg = wm8900_read(codec, WM8900_REG_FLLCTL1);
  699. wm8900_write(codec, WM8900_REG_FLLCTL1,
  700. reg & (~WM8900_REG_FLLCTL1_OSC_ENA));
  701. wm8900->fll_in = freq_in;
  702. wm8900->fll_out = freq_out;
  703. return 0;
  704. }
  705. if (fll_factors(&fll_div, freq_in, freq_out) != 0)
  706. goto reenable;
  707. wm8900->fll_in = freq_in;
  708. wm8900->fll_out = freq_out;
  709. /* The osclilator *MUST* be enabled before we enable the
  710. * digital circuit. */
  711. wm8900_write(codec, WM8900_REG_FLLCTL1,
  712. fll_div.fll_ratio | WM8900_REG_FLLCTL1_OSC_ENA);
  713. wm8900_write(codec, WM8900_REG_FLLCTL4, fll_div.n >> 5);
  714. wm8900_write(codec, WM8900_REG_FLLCTL5,
  715. (fll_div.fllclk_div << 6) | (fll_div.n & 0x1f));
  716. if (fll_div.k) {
  717. wm8900_write(codec, WM8900_REG_FLLCTL2,
  718. (fll_div.k >> 8) | 0x100);
  719. wm8900_write(codec, WM8900_REG_FLLCTL3, fll_div.k & 0xff);
  720. } else
  721. wm8900_write(codec, WM8900_REG_FLLCTL2, 0);
  722. if (fll_div.fll_slow_lock_ref)
  723. wm8900_write(codec, WM8900_REG_FLLCTL6,
  724. WM8900_REG_FLLCTL6_FLL_SLOW_LOCK_REF);
  725. else
  726. wm8900_write(codec, WM8900_REG_FLLCTL6, 0);
  727. reg = wm8900_read(codec, WM8900_REG_POWER1);
  728. wm8900_write(codec, WM8900_REG_POWER1,
  729. reg | WM8900_REG_POWER1_FLL_ENA);
  730. reenable:
  731. reg = wm8900_read(codec, WM8900_REG_CLOCKING1);
  732. wm8900_write(codec, WM8900_REG_CLOCKING1,
  733. reg | WM8900_REG_CLOCKING1_MCLK_SRC);
  734. return 0;
  735. }
  736. static int wm8900_set_dai_pll(struct snd_soc_dai *codec_dai,
  737. int pll_id, unsigned int freq_in, unsigned int freq_out)
  738. {
  739. return wm8900_set_fll(codec_dai->codec, pll_id, freq_in, freq_out);
  740. }
  741. static int wm8900_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  742. int div_id, int div)
  743. {
  744. struct snd_soc_codec *codec = codec_dai->codec;
  745. unsigned int reg;
  746. switch (div_id) {
  747. case WM8900_BCLK_DIV:
  748. reg = wm8900_read(codec, WM8900_REG_CLOCKING1);
  749. wm8900_write(codec, WM8900_REG_CLOCKING1,
  750. div | (reg & WM8900_REG_CLOCKING1_BCLK_MASK));
  751. break;
  752. case WM8900_OPCLK_DIV:
  753. reg = wm8900_read(codec, WM8900_REG_CLOCKING1);
  754. wm8900_write(codec, WM8900_REG_CLOCKING1,
  755. div | (reg & WM8900_REG_CLOCKING1_OPCLK_MASK));
  756. break;
  757. case WM8900_DAC_LRCLK:
  758. reg = wm8900_read(codec, WM8900_REG_AUDIO4);
  759. wm8900_write(codec, WM8900_REG_AUDIO4,
  760. div | (reg & WM8900_LRC_MASK));
  761. break;
  762. case WM8900_ADC_LRCLK:
  763. reg = wm8900_read(codec, WM8900_REG_AUDIO3);
  764. wm8900_write(codec, WM8900_REG_AUDIO3,
  765. div | (reg & WM8900_LRC_MASK));
  766. break;
  767. case WM8900_DAC_CLKDIV:
  768. reg = wm8900_read(codec, WM8900_REG_CLOCKING2);
  769. wm8900_write(codec, WM8900_REG_CLOCKING2,
  770. div | (reg & WM8900_REG_CLOCKING2_DAC_CLKDIV));
  771. break;
  772. case WM8900_ADC_CLKDIV:
  773. reg = wm8900_read(codec, WM8900_REG_CLOCKING2);
  774. wm8900_write(codec, WM8900_REG_CLOCKING2,
  775. div | (reg & WM8900_REG_CLOCKING2_ADC_CLKDIV));
  776. break;
  777. case WM8900_LRCLK_MODE:
  778. reg = wm8900_read(codec, WM8900_REG_DACCTRL);
  779. wm8900_write(codec, WM8900_REG_DACCTRL,
  780. div | (reg & WM8900_REG_DACCTRL_AIF_LRCLKRATE));
  781. break;
  782. default:
  783. return -EINVAL;
  784. }
  785. return 0;
  786. }
  787. static int wm8900_set_dai_fmt(struct snd_soc_dai *codec_dai,
  788. unsigned int fmt)
  789. {
  790. struct snd_soc_codec *codec = codec_dai->codec;
  791. unsigned int clocking1, aif1, aif3, aif4;
  792. clocking1 = wm8900_read(codec, WM8900_REG_CLOCKING1);
  793. aif1 = wm8900_read(codec, WM8900_REG_AUDIO1);
  794. aif3 = wm8900_read(codec, WM8900_REG_AUDIO3);
  795. aif4 = wm8900_read(codec, WM8900_REG_AUDIO4);
  796. /* set master/slave audio interface */
  797. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  798. case SND_SOC_DAIFMT_CBS_CFS:
  799. clocking1 &= ~WM8900_REG_CLOCKING1_BCLK_DIR;
  800. aif3 &= ~WM8900_REG_AUDIO3_ADCLRC_DIR;
  801. aif4 &= ~WM8900_REG_AUDIO4_DACLRC_DIR;
  802. break;
  803. case SND_SOC_DAIFMT_CBS_CFM:
  804. clocking1 &= ~WM8900_REG_CLOCKING1_BCLK_DIR;
  805. aif3 |= WM8900_REG_AUDIO3_ADCLRC_DIR;
  806. aif4 |= WM8900_REG_AUDIO4_DACLRC_DIR;
  807. break;
  808. case SND_SOC_DAIFMT_CBM_CFM:
  809. clocking1 |= WM8900_REG_CLOCKING1_BCLK_DIR;
  810. aif3 |= WM8900_REG_AUDIO3_ADCLRC_DIR;
  811. aif4 |= WM8900_REG_AUDIO4_DACLRC_DIR;
  812. break;
  813. case SND_SOC_DAIFMT_CBM_CFS:
  814. clocking1 |= WM8900_REG_CLOCKING1_BCLK_DIR;
  815. aif3 &= ~WM8900_REG_AUDIO3_ADCLRC_DIR;
  816. aif4 &= ~WM8900_REG_AUDIO4_DACLRC_DIR;
  817. break;
  818. default:
  819. return -EINVAL;
  820. }
  821. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  822. case SND_SOC_DAIFMT_DSP_A:
  823. aif1 |= WM8900_REG_AUDIO1_AIF_FMT_MASK;
  824. aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
  825. break;
  826. case SND_SOC_DAIFMT_DSP_B:
  827. aif1 |= WM8900_REG_AUDIO1_AIF_FMT_MASK;
  828. aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
  829. break;
  830. case SND_SOC_DAIFMT_I2S:
  831. aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
  832. aif1 |= 0x10;
  833. break;
  834. case SND_SOC_DAIFMT_RIGHT_J:
  835. aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
  836. break;
  837. case SND_SOC_DAIFMT_LEFT_J:
  838. aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
  839. aif1 |= 0x8;
  840. break;
  841. default:
  842. return -EINVAL;
  843. }
  844. /* Clock inversion */
  845. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  846. case SND_SOC_DAIFMT_DSP_A:
  847. case SND_SOC_DAIFMT_DSP_B:
  848. /* frame inversion not valid for DSP modes */
  849. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  850. case SND_SOC_DAIFMT_NB_NF:
  851. aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
  852. break;
  853. case SND_SOC_DAIFMT_IB_NF:
  854. aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
  855. break;
  856. default:
  857. return -EINVAL;
  858. }
  859. break;
  860. case SND_SOC_DAIFMT_I2S:
  861. case SND_SOC_DAIFMT_RIGHT_J:
  862. case SND_SOC_DAIFMT_LEFT_J:
  863. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  864. case SND_SOC_DAIFMT_NB_NF:
  865. aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
  866. aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
  867. break;
  868. case SND_SOC_DAIFMT_IB_IF:
  869. aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
  870. aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
  871. break;
  872. case SND_SOC_DAIFMT_IB_NF:
  873. aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
  874. aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
  875. break;
  876. case SND_SOC_DAIFMT_NB_IF:
  877. aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
  878. aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
  879. break;
  880. default:
  881. return -EINVAL;
  882. }
  883. break;
  884. default:
  885. return -EINVAL;
  886. }
  887. wm8900_write(codec, WM8900_REG_CLOCKING1, clocking1);
  888. wm8900_write(codec, WM8900_REG_AUDIO1, aif1);
  889. wm8900_write(codec, WM8900_REG_AUDIO3, aif3);
  890. wm8900_write(codec, WM8900_REG_AUDIO4, aif4);
  891. return 0;
  892. }
  893. static int wm8900_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  894. {
  895. struct snd_soc_codec *codec = codec_dai->codec;
  896. u16 reg;
  897. reg = wm8900_read(codec, WM8900_REG_DACCTRL);
  898. if (mute)
  899. reg |= WM8900_REG_DACCTRL_MUTE;
  900. else
  901. reg &= ~WM8900_REG_DACCTRL_MUTE;
  902. wm8900_write(codec, WM8900_REG_DACCTRL, reg);
  903. return 0;
  904. }
  905. #define WM8900_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  906. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  907. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  908. #define WM8900_PCM_FORMATS \
  909. (SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
  910. SNDRV_PCM_FORMAT_S24_LE)
  911. struct snd_soc_dai wm8900_dai = {
  912. .name = "WM8900 HiFi",
  913. .playback = {
  914. .stream_name = "HiFi Playback",
  915. .channels_min = 1,
  916. .channels_max = 2,
  917. .rates = WM8900_RATES,
  918. .formats = WM8900_PCM_FORMATS,
  919. },
  920. .capture = {
  921. .stream_name = "HiFi Capture",
  922. .channels_min = 1,
  923. .channels_max = 2,
  924. .rates = WM8900_RATES,
  925. .formats = WM8900_PCM_FORMATS,
  926. },
  927. .ops = {
  928. .hw_params = wm8900_hw_params,
  929. .set_clkdiv = wm8900_set_dai_clkdiv,
  930. .set_pll = wm8900_set_dai_pll,
  931. .set_fmt = wm8900_set_dai_fmt,
  932. .digital_mute = wm8900_digital_mute,
  933. },
  934. };
  935. EXPORT_SYMBOL_GPL(wm8900_dai);
  936. static int wm8900_set_bias_level(struct snd_soc_codec *codec,
  937. enum snd_soc_bias_level level)
  938. {
  939. u16 reg;
  940. switch (level) {
  941. case SND_SOC_BIAS_ON:
  942. /* Enable thermal shutdown */
  943. reg = wm8900_read(codec, WM8900_REG_GPIO);
  944. wm8900_write(codec, WM8900_REG_GPIO,
  945. reg | WM8900_REG_GPIO_TEMP_ENA);
  946. reg = wm8900_read(codec, WM8900_REG_ADDCTL);
  947. wm8900_write(codec, WM8900_REG_ADDCTL,
  948. reg | WM8900_REG_ADDCTL_TEMP_SD);
  949. break;
  950. case SND_SOC_BIAS_PREPARE:
  951. break;
  952. case SND_SOC_BIAS_STANDBY:
  953. /* Charge capacitors if initial power up */
  954. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  955. /* STARTUP_BIAS_ENA on */
  956. wm8900_write(codec, WM8900_REG_POWER1,
  957. WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  958. /* Startup bias mode */
  959. wm8900_write(codec, WM8900_REG_ADDCTL,
  960. WM8900_REG_ADDCTL_BIAS_SRC |
  961. WM8900_REG_ADDCTL_VMID_SOFTST);
  962. /* VMID 2x50k */
  963. wm8900_write(codec, WM8900_REG_POWER1,
  964. WM8900_REG_POWER1_STARTUP_BIAS_ENA | 0x1);
  965. /* Allow capacitors to charge */
  966. schedule_timeout_interruptible(msecs_to_jiffies(400));
  967. /* Enable bias */
  968. wm8900_write(codec, WM8900_REG_POWER1,
  969. WM8900_REG_POWER1_STARTUP_BIAS_ENA |
  970. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  971. wm8900_write(codec, WM8900_REG_ADDCTL, 0);
  972. wm8900_write(codec, WM8900_REG_POWER1,
  973. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  974. }
  975. reg = wm8900_read(codec, WM8900_REG_POWER1);
  976. wm8900_write(codec, WM8900_REG_POWER1,
  977. (reg & WM8900_REG_POWER1_FLL_ENA) |
  978. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  979. wm8900_write(codec, WM8900_REG_POWER2,
  980. WM8900_REG_POWER2_SYSCLK_ENA);
  981. wm8900_write(codec, WM8900_REG_POWER3, 0);
  982. break;
  983. case SND_SOC_BIAS_OFF:
  984. /* Startup bias enable */
  985. reg = wm8900_read(codec, WM8900_REG_POWER1);
  986. wm8900_write(codec, WM8900_REG_POWER1,
  987. reg & WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  988. wm8900_write(codec, WM8900_REG_ADDCTL,
  989. WM8900_REG_ADDCTL_BIAS_SRC |
  990. WM8900_REG_ADDCTL_VMID_SOFTST);
  991. /* Discharge caps */
  992. wm8900_write(codec, WM8900_REG_POWER1,
  993. WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  994. schedule_timeout_interruptible(msecs_to_jiffies(500));
  995. /* Remove clamp */
  996. wm8900_write(codec, WM8900_REG_HPCTL1, 0);
  997. /* Power down */
  998. wm8900_write(codec, WM8900_REG_ADDCTL, 0);
  999. wm8900_write(codec, WM8900_REG_POWER1, 0);
  1000. wm8900_write(codec, WM8900_REG_POWER2, 0);
  1001. wm8900_write(codec, WM8900_REG_POWER3, 0);
  1002. /* Need to let things settle before stopping the clock
  1003. * to ensure that restart works, see "Stopping the
  1004. * master clock" in the datasheet. */
  1005. schedule_timeout_interruptible(msecs_to_jiffies(1));
  1006. wm8900_write(codec, WM8900_REG_POWER2,
  1007. WM8900_REG_POWER2_SYSCLK_ENA);
  1008. break;
  1009. }
  1010. codec->bias_level = level;
  1011. return 0;
  1012. }
  1013. static int wm8900_suspend(struct platform_device *pdev, pm_message_t state)
  1014. {
  1015. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1016. struct snd_soc_codec *codec = socdev->codec;
  1017. struct wm8900_priv *wm8900 = codec->private_data;
  1018. int fll_out = wm8900->fll_out;
  1019. int fll_in = wm8900->fll_in;
  1020. int ret;
  1021. /* Stop the FLL in an orderly fashion */
  1022. ret = wm8900_set_fll(codec, 0, 0, 0);
  1023. if (ret != 0) {
  1024. dev_err(&pdev->dev, "Failed to stop FLL\n");
  1025. return ret;
  1026. }
  1027. wm8900->fll_out = fll_out;
  1028. wm8900->fll_in = fll_in;
  1029. wm8900_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1030. return 0;
  1031. }
  1032. static int wm8900_resume(struct platform_device *pdev)
  1033. {
  1034. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1035. struct snd_soc_codec *codec = socdev->codec;
  1036. struct wm8900_priv *wm8900 = codec->private_data;
  1037. u16 *cache;
  1038. int i, ret;
  1039. cache = kmemdup(codec->reg_cache, sizeof(wm8900_reg_defaults),
  1040. GFP_KERNEL);
  1041. wm8900_reset(codec);
  1042. wm8900_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1043. /* Restart the FLL? */
  1044. if (wm8900->fll_out) {
  1045. int fll_out = wm8900->fll_out;
  1046. int fll_in = wm8900->fll_in;
  1047. wm8900->fll_in = 0;
  1048. wm8900->fll_out = 0;
  1049. ret = wm8900_set_fll(codec, 0, fll_in, fll_out);
  1050. if (ret != 0) {
  1051. dev_err(&pdev->dev, "Failed to restart FLL\n");
  1052. return ret;
  1053. }
  1054. }
  1055. if (cache) {
  1056. for (i = 0; i < WM8900_MAXREG; i++)
  1057. wm8900_write(codec, i, cache[i]);
  1058. kfree(cache);
  1059. } else
  1060. dev_err(&pdev->dev, "Unable to allocate register cache\n");
  1061. return 0;
  1062. }
  1063. /*
  1064. * initialise the WM8900 driver
  1065. * register the mixer and dsp interfaces with the kernel
  1066. */
  1067. static int wm8900_init(struct snd_soc_device *socdev)
  1068. {
  1069. struct snd_soc_codec *codec = socdev->codec;
  1070. int ret = 0;
  1071. unsigned int reg;
  1072. struct i2c_client *i2c_client = socdev->codec->control_data;
  1073. codec->name = "WM8900";
  1074. codec->owner = THIS_MODULE;
  1075. codec->read = wm8900_read;
  1076. codec->write = wm8900_write;
  1077. codec->dai = &wm8900_dai;
  1078. codec->num_dai = 1;
  1079. codec->reg_cache_size = WM8900_MAXREG;
  1080. codec->reg_cache = kmemdup(wm8900_reg_defaults,
  1081. sizeof(wm8900_reg_defaults), GFP_KERNEL);
  1082. if (codec->reg_cache == NULL)
  1083. return -ENOMEM;
  1084. reg = wm8900_read(codec, WM8900_REG_ID);
  1085. if (reg != 0x8900) {
  1086. dev_err(&i2c_client->dev, "Device is not a WM8900 - ID %x\n",
  1087. reg);
  1088. return -ENODEV;
  1089. }
  1090. codec->private_data = kzalloc(sizeof(struct wm8900_priv), GFP_KERNEL);
  1091. if (codec->private_data == NULL) {
  1092. ret = -ENOMEM;
  1093. goto priv_err;
  1094. }
  1095. /* Read back from the chip */
  1096. reg = wm8900_chip_read(codec, WM8900_REG_POWER1);
  1097. reg = (reg >> 12) & 0xf;
  1098. dev_info(&i2c_client->dev, "WM8900 revision %d\n", reg);
  1099. wm8900_reset(codec);
  1100. /* Latch the volume update bits */
  1101. wm8900_write(codec, WM8900_REG_LINVOL,
  1102. wm8900_read(codec, WM8900_REG_LINVOL) | 0x100);
  1103. wm8900_write(codec, WM8900_REG_RINVOL,
  1104. wm8900_read(codec, WM8900_REG_RINVOL) | 0x100);
  1105. wm8900_write(codec, WM8900_REG_LOUT1CTL,
  1106. wm8900_read(codec, WM8900_REG_LOUT1CTL) | 0x100);
  1107. wm8900_write(codec, WM8900_REG_ROUT1CTL,
  1108. wm8900_read(codec, WM8900_REG_ROUT1CTL) | 0x100);
  1109. wm8900_write(codec, WM8900_REG_LOUT2CTL,
  1110. wm8900_read(codec, WM8900_REG_LOUT2CTL) | 0x100);
  1111. wm8900_write(codec, WM8900_REG_ROUT2CTL,
  1112. wm8900_read(codec, WM8900_REG_ROUT2CTL) | 0x100);
  1113. wm8900_write(codec, WM8900_REG_LDAC_DV,
  1114. wm8900_read(codec, WM8900_REG_LDAC_DV) | 0x100);
  1115. wm8900_write(codec, WM8900_REG_RDAC_DV,
  1116. wm8900_read(codec, WM8900_REG_RDAC_DV) | 0x100);
  1117. wm8900_write(codec, WM8900_REG_LADC_DV,
  1118. wm8900_read(codec, WM8900_REG_LADC_DV) | 0x100);
  1119. wm8900_write(codec, WM8900_REG_RADC_DV,
  1120. wm8900_read(codec, WM8900_REG_RADC_DV) | 0x100);
  1121. /* Set the DAC and mixer output bias */
  1122. wm8900_write(codec, WM8900_REG_OUTBIASCTL, 0x81);
  1123. /* Register pcms */
  1124. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  1125. if (ret < 0) {
  1126. dev_err(&i2c_client->dev, "Failed to register new PCMs\n");
  1127. goto pcm_err;
  1128. }
  1129. /* Turn the chip on */
  1130. codec->bias_level = SND_SOC_BIAS_OFF;
  1131. wm8900_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1132. wm8900_add_controls(codec);
  1133. wm8900_add_widgets(codec);
  1134. ret = snd_soc_init_card(socdev);
  1135. if (ret < 0) {
  1136. dev_err(&i2c_client->dev, "Failed to register card\n");
  1137. goto card_err;
  1138. }
  1139. return ret;
  1140. card_err:
  1141. snd_soc_free_pcms(socdev);
  1142. snd_soc_dapm_free(socdev);
  1143. pcm_err:
  1144. kfree(codec->reg_cache);
  1145. priv_err:
  1146. kfree(codec->private_data);
  1147. return ret;
  1148. }
  1149. static struct snd_soc_device *wm8900_socdev;
  1150. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1151. /* If the i2c layer weren't so broken, we could pass this kind of data
  1152. around */
  1153. static int wm8900_i2c_probe(struct i2c_client *i2c,
  1154. const struct i2c_device_id *id)
  1155. {
  1156. struct snd_soc_device *socdev = wm8900_socdev;
  1157. struct snd_soc_codec *codec = socdev->codec;
  1158. int ret;
  1159. i2c_set_clientdata(i2c, codec);
  1160. codec->control_data = i2c;
  1161. ret = wm8900_init(socdev);
  1162. if (ret < 0)
  1163. dev_err(&i2c->dev, "failed to initialise WM8900\n");
  1164. return ret;
  1165. }
  1166. static int wm8900_i2c_remove(struct i2c_client *client)
  1167. {
  1168. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  1169. kfree(codec->reg_cache);
  1170. return 0;
  1171. }
  1172. static const struct i2c_device_id wm8900_i2c_id[] = {
  1173. { "wm8900", 0 },
  1174. { }
  1175. };
  1176. MODULE_DEVICE_TABLE(i2c, wm8900_i2c_id);
  1177. static struct i2c_driver wm8900_i2c_driver = {
  1178. .driver = {
  1179. .name = "WM8900 I2C codec",
  1180. .owner = THIS_MODULE,
  1181. },
  1182. .probe = wm8900_i2c_probe,
  1183. .remove = wm8900_i2c_remove,
  1184. .id_table = wm8900_i2c_id,
  1185. };
  1186. static int wm8900_add_i2c_device(struct platform_device *pdev,
  1187. const struct wm8900_setup_data *setup)
  1188. {
  1189. struct i2c_board_info info;
  1190. struct i2c_adapter *adapter;
  1191. struct i2c_client *client;
  1192. int ret;
  1193. ret = i2c_add_driver(&wm8900_i2c_driver);
  1194. if (ret != 0) {
  1195. dev_err(&pdev->dev, "can't add i2c driver\n");
  1196. return ret;
  1197. }
  1198. memset(&info, 0, sizeof(struct i2c_board_info));
  1199. info.addr = setup->i2c_address;
  1200. strlcpy(info.type, "wm8900", I2C_NAME_SIZE);
  1201. adapter = i2c_get_adapter(setup->i2c_bus);
  1202. if (!adapter) {
  1203. dev_err(&pdev->dev, "can't get i2c adapter %d\n",
  1204. setup->i2c_bus);
  1205. goto err_driver;
  1206. }
  1207. client = i2c_new_device(adapter, &info);
  1208. i2c_put_adapter(adapter);
  1209. if (!client) {
  1210. dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
  1211. (unsigned int)info.addr);
  1212. goto err_driver;
  1213. }
  1214. return 0;
  1215. err_driver:
  1216. i2c_del_driver(&wm8900_i2c_driver);
  1217. return -ENODEV;
  1218. }
  1219. #endif
  1220. static int wm8900_probe(struct platform_device *pdev)
  1221. {
  1222. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1223. struct wm8900_setup_data *setup;
  1224. struct snd_soc_codec *codec;
  1225. int ret = 0;
  1226. dev_info(&pdev->dev, "WM8900 Audio Codec\n");
  1227. setup = socdev->codec_data;
  1228. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  1229. if (codec == NULL)
  1230. return -ENOMEM;
  1231. mutex_init(&codec->mutex);
  1232. INIT_LIST_HEAD(&codec->dapm_widgets);
  1233. INIT_LIST_HEAD(&codec->dapm_paths);
  1234. socdev->codec = codec;
  1235. codec->set_bias_level = wm8900_set_bias_level;
  1236. wm8900_socdev = socdev;
  1237. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1238. if (setup->i2c_address) {
  1239. codec->hw_write = (hw_write_t)i2c_master_send;
  1240. ret = wm8900_add_i2c_device(pdev, setup);
  1241. }
  1242. #else
  1243. #error Non-I2C interfaces not yet supported
  1244. #endif
  1245. return ret;
  1246. }
  1247. /* power down chip */
  1248. static int wm8900_remove(struct platform_device *pdev)
  1249. {
  1250. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1251. struct snd_soc_codec *codec = socdev->codec;
  1252. if (codec->control_data)
  1253. wm8900_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1254. snd_soc_free_pcms(socdev);
  1255. snd_soc_dapm_free(socdev);
  1256. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1257. i2c_unregister_device(codec->control_data);
  1258. i2c_del_driver(&wm8900_i2c_driver);
  1259. #endif
  1260. kfree(codec);
  1261. return 0;
  1262. }
  1263. struct snd_soc_codec_device soc_codec_dev_wm8900 = {
  1264. .probe = wm8900_probe,
  1265. .remove = wm8900_remove,
  1266. .suspend = wm8900_suspend,
  1267. .resume = wm8900_resume,
  1268. };
  1269. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8900);
  1270. static int __devinit wm8900_modinit(void)
  1271. {
  1272. return snd_soc_register_dai(&wm8900_dai);
  1273. }
  1274. module_init(wm8900_modinit);
  1275. static void __exit wm8900_exit(void)
  1276. {
  1277. snd_soc_unregister_dai(&wm8900_dai);
  1278. }
  1279. module_exit(wm8900_exit);
  1280. MODULE_DESCRIPTION("ASoC WM8900 driver");
  1281. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
  1282. MODULE_LICENSE("GPL");