wm8900.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. {
  598. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  599. struct snd_soc_device *socdev = rtd->socdev;
  600. struct snd_soc_codec *codec = socdev->codec;
  601. u16 reg;
  602. reg = wm8900_read(codec, WM8900_REG_AUDIO1) & ~0x60;
  603. switch (params_format(params)) {
  604. case SNDRV_PCM_FORMAT_S16_LE:
  605. break;
  606. case SNDRV_PCM_FORMAT_S20_3LE:
  607. reg |= 0x20;
  608. break;
  609. case SNDRV_PCM_FORMAT_S24_LE:
  610. reg |= 0x40;
  611. break;
  612. case SNDRV_PCM_FORMAT_S32_LE:
  613. reg |= 0x60;
  614. break;
  615. default:
  616. return -EINVAL;
  617. }
  618. wm8900_write(codec, WM8900_REG_AUDIO1, reg);
  619. return 0;
  620. }
  621. /* FLL divisors */
  622. struct _fll_div {
  623. u16 fll_ratio;
  624. u16 fllclk_div;
  625. u16 fll_slow_lock_ref;
  626. u16 n;
  627. u16 k;
  628. };
  629. /* The size in bits of the FLL divide multiplied by 10
  630. * to allow rounding later */
  631. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  632. static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
  633. unsigned int Fout)
  634. {
  635. u64 Kpart;
  636. unsigned int K, Ndiv, Nmod, target;
  637. unsigned int div;
  638. BUG_ON(!Fout);
  639. /* The FLL must run at 90-100MHz which is then scaled down to
  640. * the output value by FLLCLK_DIV. */
  641. target = Fout;
  642. div = 1;
  643. while (target < 90000000) {
  644. div *= 2;
  645. target *= 2;
  646. }
  647. if (target > 100000000)
  648. printk(KERN_WARNING "wm8900: FLL rate %d out of range, Fref=%d"
  649. " Fout=%d\n", target, Fref, Fout);
  650. if (div > 32) {
  651. printk(KERN_ERR "wm8900: Invalid FLL division rate %u, "
  652. "Fref=%d, Fout=%d, target=%d\n",
  653. div, Fref, Fout, target);
  654. return -EINVAL;
  655. }
  656. fll_div->fllclk_div = div >> 2;
  657. if (Fref < 48000)
  658. fll_div->fll_slow_lock_ref = 1;
  659. else
  660. fll_div->fll_slow_lock_ref = 0;
  661. Ndiv = target / Fref;
  662. if (Fref < 1000000)
  663. fll_div->fll_ratio = 8;
  664. else
  665. fll_div->fll_ratio = 1;
  666. fll_div->n = Ndiv / fll_div->fll_ratio;
  667. Nmod = (target / fll_div->fll_ratio) % Fref;
  668. /* Calculate fractional part - scale up so we can round. */
  669. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  670. do_div(Kpart, Fref);
  671. K = Kpart & 0xFFFFFFFF;
  672. if ((K % 10) >= 5)
  673. K += 5;
  674. /* Move down to proper range now rounding is done */
  675. fll_div->k = K / 10;
  676. BUG_ON(target != Fout * (fll_div->fllclk_div << 2));
  677. BUG_ON(!K && target != Fref * fll_div->fll_ratio * fll_div->n);
  678. return 0;
  679. }
  680. static int wm8900_set_fll(struct snd_soc_codec *codec,
  681. int fll_id, unsigned int freq_in, unsigned int freq_out)
  682. {
  683. struct wm8900_priv *wm8900 = codec->private_data;
  684. struct _fll_div fll_div;
  685. unsigned int reg;
  686. if (wm8900->fll_in == freq_in && wm8900->fll_out == freq_out)
  687. return 0;
  688. /* The digital side should be disabled during any change. */
  689. reg = wm8900_read(codec, WM8900_REG_POWER1);
  690. wm8900_write(codec, WM8900_REG_POWER1,
  691. reg & (~WM8900_REG_POWER1_FLL_ENA));
  692. /* Disable the FLL? */
  693. if (!freq_in || !freq_out) {
  694. reg = wm8900_read(codec, WM8900_REG_CLOCKING1);
  695. wm8900_write(codec, WM8900_REG_CLOCKING1,
  696. reg & (~WM8900_REG_CLOCKING1_MCLK_SRC));
  697. reg = wm8900_read(codec, WM8900_REG_FLLCTL1);
  698. wm8900_write(codec, WM8900_REG_FLLCTL1,
  699. reg & (~WM8900_REG_FLLCTL1_OSC_ENA));
  700. wm8900->fll_in = freq_in;
  701. wm8900->fll_out = freq_out;
  702. return 0;
  703. }
  704. if (fll_factors(&fll_div, freq_in, freq_out) != 0)
  705. goto reenable;
  706. wm8900->fll_in = freq_in;
  707. wm8900->fll_out = freq_out;
  708. /* The osclilator *MUST* be enabled before we enable the
  709. * digital circuit. */
  710. wm8900_write(codec, WM8900_REG_FLLCTL1,
  711. fll_div.fll_ratio | WM8900_REG_FLLCTL1_OSC_ENA);
  712. wm8900_write(codec, WM8900_REG_FLLCTL4, fll_div.n >> 5);
  713. wm8900_write(codec, WM8900_REG_FLLCTL5,
  714. (fll_div.fllclk_div << 6) | (fll_div.n & 0x1f));
  715. if (fll_div.k) {
  716. wm8900_write(codec, WM8900_REG_FLLCTL2,
  717. (fll_div.k >> 8) | 0x100);
  718. wm8900_write(codec, WM8900_REG_FLLCTL3, fll_div.k & 0xff);
  719. } else
  720. wm8900_write(codec, WM8900_REG_FLLCTL2, 0);
  721. if (fll_div.fll_slow_lock_ref)
  722. wm8900_write(codec, WM8900_REG_FLLCTL6,
  723. WM8900_REG_FLLCTL6_FLL_SLOW_LOCK_REF);
  724. else
  725. wm8900_write(codec, WM8900_REG_FLLCTL6, 0);
  726. reg = wm8900_read(codec, WM8900_REG_POWER1);
  727. wm8900_write(codec, WM8900_REG_POWER1,
  728. reg | WM8900_REG_POWER1_FLL_ENA);
  729. reenable:
  730. reg = wm8900_read(codec, WM8900_REG_CLOCKING1);
  731. wm8900_write(codec, WM8900_REG_CLOCKING1,
  732. reg | WM8900_REG_CLOCKING1_MCLK_SRC);
  733. return 0;
  734. }
  735. static int wm8900_set_dai_pll(struct snd_soc_dai *codec_dai,
  736. int pll_id, unsigned int freq_in, unsigned int freq_out)
  737. {
  738. return wm8900_set_fll(codec_dai->codec, pll_id, freq_in, freq_out);
  739. }
  740. static int wm8900_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
  741. int div_id, int div)
  742. {
  743. struct snd_soc_codec *codec = codec_dai->codec;
  744. unsigned int reg;
  745. switch (div_id) {
  746. case WM8900_BCLK_DIV:
  747. reg = wm8900_read(codec, WM8900_REG_CLOCKING1);
  748. wm8900_write(codec, WM8900_REG_CLOCKING1,
  749. div | (reg & WM8900_REG_CLOCKING1_BCLK_MASK));
  750. break;
  751. case WM8900_OPCLK_DIV:
  752. reg = wm8900_read(codec, WM8900_REG_CLOCKING1);
  753. wm8900_write(codec, WM8900_REG_CLOCKING1,
  754. div | (reg & WM8900_REG_CLOCKING1_OPCLK_MASK));
  755. break;
  756. case WM8900_DAC_LRCLK:
  757. reg = wm8900_read(codec, WM8900_REG_AUDIO4);
  758. wm8900_write(codec, WM8900_REG_AUDIO4,
  759. div | (reg & WM8900_LRC_MASK));
  760. break;
  761. case WM8900_ADC_LRCLK:
  762. reg = wm8900_read(codec, WM8900_REG_AUDIO3);
  763. wm8900_write(codec, WM8900_REG_AUDIO3,
  764. div | (reg & WM8900_LRC_MASK));
  765. break;
  766. case WM8900_DAC_CLKDIV:
  767. reg = wm8900_read(codec, WM8900_REG_CLOCKING2);
  768. wm8900_write(codec, WM8900_REG_CLOCKING2,
  769. div | (reg & WM8900_REG_CLOCKING2_DAC_CLKDIV));
  770. break;
  771. case WM8900_ADC_CLKDIV:
  772. reg = wm8900_read(codec, WM8900_REG_CLOCKING2);
  773. wm8900_write(codec, WM8900_REG_CLOCKING2,
  774. div | (reg & WM8900_REG_CLOCKING2_ADC_CLKDIV));
  775. break;
  776. case WM8900_LRCLK_MODE:
  777. reg = wm8900_read(codec, WM8900_REG_DACCTRL);
  778. wm8900_write(codec, WM8900_REG_DACCTRL,
  779. div | (reg & WM8900_REG_DACCTRL_AIF_LRCLKRATE));
  780. break;
  781. default:
  782. return -EINVAL;
  783. }
  784. return 0;
  785. }
  786. static int wm8900_set_dai_fmt(struct snd_soc_dai *codec_dai,
  787. unsigned int fmt)
  788. {
  789. struct snd_soc_codec *codec = codec_dai->codec;
  790. unsigned int clocking1, aif1, aif3, aif4;
  791. clocking1 = wm8900_read(codec, WM8900_REG_CLOCKING1);
  792. aif1 = wm8900_read(codec, WM8900_REG_AUDIO1);
  793. aif3 = wm8900_read(codec, WM8900_REG_AUDIO3);
  794. aif4 = wm8900_read(codec, WM8900_REG_AUDIO4);
  795. /* set master/slave audio interface */
  796. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  797. case SND_SOC_DAIFMT_CBS_CFS:
  798. clocking1 &= ~WM8900_REG_CLOCKING1_BCLK_DIR;
  799. aif3 &= ~WM8900_REG_AUDIO3_ADCLRC_DIR;
  800. aif4 &= ~WM8900_REG_AUDIO4_DACLRC_DIR;
  801. break;
  802. case SND_SOC_DAIFMT_CBS_CFM:
  803. clocking1 &= ~WM8900_REG_CLOCKING1_BCLK_DIR;
  804. aif3 |= WM8900_REG_AUDIO3_ADCLRC_DIR;
  805. aif4 |= WM8900_REG_AUDIO4_DACLRC_DIR;
  806. break;
  807. case SND_SOC_DAIFMT_CBM_CFM:
  808. clocking1 |= WM8900_REG_CLOCKING1_BCLK_DIR;
  809. aif3 |= WM8900_REG_AUDIO3_ADCLRC_DIR;
  810. aif4 |= WM8900_REG_AUDIO4_DACLRC_DIR;
  811. break;
  812. case SND_SOC_DAIFMT_CBM_CFS:
  813. clocking1 |= WM8900_REG_CLOCKING1_BCLK_DIR;
  814. aif3 &= ~WM8900_REG_AUDIO3_ADCLRC_DIR;
  815. aif4 &= ~WM8900_REG_AUDIO4_DACLRC_DIR;
  816. break;
  817. default:
  818. return -EINVAL;
  819. }
  820. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  821. case SND_SOC_DAIFMT_DSP_A:
  822. aif1 |= WM8900_REG_AUDIO1_AIF_FMT_MASK;
  823. aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
  824. break;
  825. case SND_SOC_DAIFMT_DSP_B:
  826. aif1 |= WM8900_REG_AUDIO1_AIF_FMT_MASK;
  827. aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
  828. break;
  829. case SND_SOC_DAIFMT_I2S:
  830. aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
  831. aif1 |= 0x10;
  832. break;
  833. case SND_SOC_DAIFMT_RIGHT_J:
  834. aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
  835. break;
  836. case SND_SOC_DAIFMT_LEFT_J:
  837. aif1 &= ~WM8900_REG_AUDIO1_AIF_FMT_MASK;
  838. aif1 |= 0x8;
  839. break;
  840. default:
  841. return -EINVAL;
  842. }
  843. /* Clock inversion */
  844. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  845. case SND_SOC_DAIFMT_DSP_A:
  846. case SND_SOC_DAIFMT_DSP_B:
  847. /* frame inversion not valid for DSP modes */
  848. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  849. case SND_SOC_DAIFMT_NB_NF:
  850. aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
  851. break;
  852. case SND_SOC_DAIFMT_IB_NF:
  853. aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
  854. break;
  855. default:
  856. return -EINVAL;
  857. }
  858. break;
  859. case SND_SOC_DAIFMT_I2S:
  860. case SND_SOC_DAIFMT_RIGHT_J:
  861. case SND_SOC_DAIFMT_LEFT_J:
  862. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  863. case SND_SOC_DAIFMT_NB_NF:
  864. aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
  865. aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
  866. break;
  867. case SND_SOC_DAIFMT_IB_IF:
  868. aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
  869. aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
  870. break;
  871. case SND_SOC_DAIFMT_IB_NF:
  872. aif1 |= WM8900_REG_AUDIO1_BCLK_INV;
  873. aif1 &= ~WM8900_REG_AUDIO1_LRCLK_INV;
  874. break;
  875. case SND_SOC_DAIFMT_NB_IF:
  876. aif1 &= ~WM8900_REG_AUDIO1_BCLK_INV;
  877. aif1 |= WM8900_REG_AUDIO1_LRCLK_INV;
  878. break;
  879. default:
  880. return -EINVAL;
  881. }
  882. break;
  883. default:
  884. return -EINVAL;
  885. }
  886. wm8900_write(codec, WM8900_REG_CLOCKING1, clocking1);
  887. wm8900_write(codec, WM8900_REG_AUDIO1, aif1);
  888. wm8900_write(codec, WM8900_REG_AUDIO3, aif3);
  889. wm8900_write(codec, WM8900_REG_AUDIO4, aif4);
  890. return 0;
  891. }
  892. static int wm8900_digital_mute(struct snd_soc_dai *codec_dai, int mute)
  893. {
  894. struct snd_soc_codec *codec = codec_dai->codec;
  895. u16 reg;
  896. reg = wm8900_read(codec, WM8900_REG_DACCTRL);
  897. if (mute)
  898. reg |= WM8900_REG_DACCTRL_MUTE;
  899. else
  900. reg &= ~WM8900_REG_DACCTRL_MUTE;
  901. wm8900_write(codec, WM8900_REG_DACCTRL, reg);
  902. return 0;
  903. }
  904. #define WM8900_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
  905. SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
  906. SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
  907. #define WM8900_PCM_FORMATS \
  908. (SNDRV_PCM_FORMAT_S16_LE | SNDRV_PCM_FORMAT_S20_3LE | \
  909. SNDRV_PCM_FORMAT_S24_LE)
  910. struct snd_soc_dai wm8900_dai = {
  911. .name = "WM8900 HiFi",
  912. .playback = {
  913. .stream_name = "HiFi Playback",
  914. .channels_min = 1,
  915. .channels_max = 2,
  916. .rates = WM8900_RATES,
  917. .formats = WM8900_PCM_FORMATS,
  918. },
  919. .capture = {
  920. .stream_name = "HiFi Capture",
  921. .channels_min = 1,
  922. .channels_max = 2,
  923. .rates = WM8900_RATES,
  924. .formats = WM8900_PCM_FORMATS,
  925. },
  926. .ops = {
  927. .hw_params = wm8900_hw_params,
  928. },
  929. .dai_ops = {
  930. .set_clkdiv = wm8900_set_dai_clkdiv,
  931. .set_pll = wm8900_set_dai_pll,
  932. .set_fmt = wm8900_set_dai_fmt,
  933. .digital_mute = wm8900_digital_mute,
  934. },
  935. };
  936. EXPORT_SYMBOL_GPL(wm8900_dai);
  937. static int wm8900_set_bias_level(struct snd_soc_codec *codec,
  938. enum snd_soc_bias_level level)
  939. {
  940. u16 reg;
  941. switch (level) {
  942. case SND_SOC_BIAS_ON:
  943. /* Enable thermal shutdown */
  944. reg = wm8900_read(codec, WM8900_REG_GPIO);
  945. wm8900_write(codec, WM8900_REG_GPIO,
  946. reg | WM8900_REG_GPIO_TEMP_ENA);
  947. reg = wm8900_read(codec, WM8900_REG_ADDCTL);
  948. wm8900_write(codec, WM8900_REG_ADDCTL,
  949. reg | WM8900_REG_ADDCTL_TEMP_SD);
  950. break;
  951. case SND_SOC_BIAS_PREPARE:
  952. break;
  953. case SND_SOC_BIAS_STANDBY:
  954. /* Charge capacitors if initial power up */
  955. if (codec->bias_level == SND_SOC_BIAS_OFF) {
  956. /* STARTUP_BIAS_ENA on */
  957. wm8900_write(codec, WM8900_REG_POWER1,
  958. WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  959. /* Startup bias mode */
  960. wm8900_write(codec, WM8900_REG_ADDCTL,
  961. WM8900_REG_ADDCTL_BIAS_SRC |
  962. WM8900_REG_ADDCTL_VMID_SOFTST);
  963. /* VMID 2x50k */
  964. wm8900_write(codec, WM8900_REG_POWER1,
  965. WM8900_REG_POWER1_STARTUP_BIAS_ENA | 0x1);
  966. /* Allow capacitors to charge */
  967. schedule_timeout_interruptible(msecs_to_jiffies(400));
  968. /* Enable bias */
  969. wm8900_write(codec, WM8900_REG_POWER1,
  970. WM8900_REG_POWER1_STARTUP_BIAS_ENA |
  971. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  972. wm8900_write(codec, WM8900_REG_ADDCTL, 0);
  973. wm8900_write(codec, WM8900_REG_POWER1,
  974. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  975. }
  976. reg = wm8900_read(codec, WM8900_REG_POWER1);
  977. wm8900_write(codec, WM8900_REG_POWER1,
  978. (reg & WM8900_REG_POWER1_FLL_ENA) |
  979. WM8900_REG_POWER1_BIAS_ENA | 0x1);
  980. wm8900_write(codec, WM8900_REG_POWER2,
  981. WM8900_REG_POWER2_SYSCLK_ENA);
  982. wm8900_write(codec, WM8900_REG_POWER3, 0);
  983. break;
  984. case SND_SOC_BIAS_OFF:
  985. /* Startup bias enable */
  986. reg = wm8900_read(codec, WM8900_REG_POWER1);
  987. wm8900_write(codec, WM8900_REG_POWER1,
  988. reg & WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  989. wm8900_write(codec, WM8900_REG_ADDCTL,
  990. WM8900_REG_ADDCTL_BIAS_SRC |
  991. WM8900_REG_ADDCTL_VMID_SOFTST);
  992. /* Discharge caps */
  993. wm8900_write(codec, WM8900_REG_POWER1,
  994. WM8900_REG_POWER1_STARTUP_BIAS_ENA);
  995. schedule_timeout_interruptible(msecs_to_jiffies(500));
  996. /* Remove clamp */
  997. wm8900_write(codec, WM8900_REG_HPCTL1, 0);
  998. /* Power down */
  999. wm8900_write(codec, WM8900_REG_ADDCTL, 0);
  1000. wm8900_write(codec, WM8900_REG_POWER1, 0);
  1001. wm8900_write(codec, WM8900_REG_POWER2, 0);
  1002. wm8900_write(codec, WM8900_REG_POWER3, 0);
  1003. /* Need to let things settle before stopping the clock
  1004. * to ensure that restart works, see "Stopping the
  1005. * master clock" in the datasheet. */
  1006. schedule_timeout_interruptible(msecs_to_jiffies(1));
  1007. wm8900_write(codec, WM8900_REG_POWER2,
  1008. WM8900_REG_POWER2_SYSCLK_ENA);
  1009. break;
  1010. }
  1011. codec->bias_level = level;
  1012. return 0;
  1013. }
  1014. static int wm8900_suspend(struct platform_device *pdev, pm_message_t state)
  1015. {
  1016. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1017. struct snd_soc_codec *codec = socdev->codec;
  1018. struct wm8900_priv *wm8900 = codec->private_data;
  1019. int fll_out = wm8900->fll_out;
  1020. int fll_in = wm8900->fll_in;
  1021. int ret;
  1022. /* Stop the FLL in an orderly fashion */
  1023. ret = wm8900_set_fll(codec, 0, 0, 0);
  1024. if (ret != 0) {
  1025. dev_err(&pdev->dev, "Failed to stop FLL\n");
  1026. return ret;
  1027. }
  1028. wm8900->fll_out = fll_out;
  1029. wm8900->fll_in = fll_in;
  1030. wm8900_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1031. return 0;
  1032. }
  1033. static int wm8900_resume(struct platform_device *pdev)
  1034. {
  1035. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1036. struct snd_soc_codec *codec = socdev->codec;
  1037. struct wm8900_priv *wm8900 = codec->private_data;
  1038. u16 *cache;
  1039. int i, ret;
  1040. cache = kmemdup(codec->reg_cache, sizeof(wm8900_reg_defaults),
  1041. GFP_KERNEL);
  1042. wm8900_reset(codec);
  1043. wm8900_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1044. /* Restart the FLL? */
  1045. if (wm8900->fll_out) {
  1046. int fll_out = wm8900->fll_out;
  1047. int fll_in = wm8900->fll_in;
  1048. wm8900->fll_in = 0;
  1049. wm8900->fll_out = 0;
  1050. ret = wm8900_set_fll(codec, 0, fll_in, fll_out);
  1051. if (ret != 0) {
  1052. dev_err(&pdev->dev, "Failed to restart FLL\n");
  1053. return ret;
  1054. }
  1055. }
  1056. if (cache) {
  1057. for (i = 0; i < WM8900_MAXREG; i++)
  1058. wm8900_write(codec, i, cache[i]);
  1059. kfree(cache);
  1060. } else
  1061. dev_err(&pdev->dev, "Unable to allocate register cache\n");
  1062. return 0;
  1063. }
  1064. /*
  1065. * initialise the WM8900 driver
  1066. * register the mixer and dsp interfaces with the kernel
  1067. */
  1068. static int wm8900_init(struct snd_soc_device *socdev)
  1069. {
  1070. struct snd_soc_codec *codec = socdev->codec;
  1071. int ret = 0;
  1072. unsigned int reg;
  1073. struct i2c_client *i2c_client = socdev->codec->control_data;
  1074. codec->name = "WM8900";
  1075. codec->owner = THIS_MODULE;
  1076. codec->read = wm8900_read;
  1077. codec->write = wm8900_write;
  1078. codec->dai = &wm8900_dai;
  1079. codec->num_dai = 1;
  1080. codec->reg_cache_size = WM8900_MAXREG;
  1081. codec->reg_cache = kmemdup(wm8900_reg_defaults,
  1082. sizeof(wm8900_reg_defaults), GFP_KERNEL);
  1083. if (codec->reg_cache == NULL)
  1084. return -ENOMEM;
  1085. reg = wm8900_read(codec, WM8900_REG_ID);
  1086. if (reg != 0x8900) {
  1087. dev_err(&i2c_client->dev, "Device is not a WM8900 - ID %x\n",
  1088. reg);
  1089. return -ENODEV;
  1090. }
  1091. codec->private_data = kzalloc(sizeof(struct wm8900_priv), GFP_KERNEL);
  1092. if (codec->private_data == NULL) {
  1093. ret = -ENOMEM;
  1094. goto priv_err;
  1095. }
  1096. /* Read back from the chip */
  1097. reg = wm8900_chip_read(codec, WM8900_REG_POWER1);
  1098. reg = (reg >> 12) & 0xf;
  1099. dev_info(&i2c_client->dev, "WM8900 revision %d\n", reg);
  1100. wm8900_reset(codec);
  1101. /* Latch the volume update bits */
  1102. wm8900_write(codec, WM8900_REG_LINVOL,
  1103. wm8900_read(codec, WM8900_REG_LINVOL) | 0x100);
  1104. wm8900_write(codec, WM8900_REG_RINVOL,
  1105. wm8900_read(codec, WM8900_REG_RINVOL) | 0x100);
  1106. wm8900_write(codec, WM8900_REG_LOUT1CTL,
  1107. wm8900_read(codec, WM8900_REG_LOUT1CTL) | 0x100);
  1108. wm8900_write(codec, WM8900_REG_ROUT1CTL,
  1109. wm8900_read(codec, WM8900_REG_ROUT1CTL) | 0x100);
  1110. wm8900_write(codec, WM8900_REG_LOUT2CTL,
  1111. wm8900_read(codec, WM8900_REG_LOUT2CTL) | 0x100);
  1112. wm8900_write(codec, WM8900_REG_ROUT2CTL,
  1113. wm8900_read(codec, WM8900_REG_ROUT2CTL) | 0x100);
  1114. wm8900_write(codec, WM8900_REG_LDAC_DV,
  1115. wm8900_read(codec, WM8900_REG_LDAC_DV) | 0x100);
  1116. wm8900_write(codec, WM8900_REG_RDAC_DV,
  1117. wm8900_read(codec, WM8900_REG_RDAC_DV) | 0x100);
  1118. wm8900_write(codec, WM8900_REG_LADC_DV,
  1119. wm8900_read(codec, WM8900_REG_LADC_DV) | 0x100);
  1120. wm8900_write(codec, WM8900_REG_RADC_DV,
  1121. wm8900_read(codec, WM8900_REG_RADC_DV) | 0x100);
  1122. /* Set the DAC and mixer output bias */
  1123. wm8900_write(codec, WM8900_REG_OUTBIASCTL, 0x81);
  1124. /* Register pcms */
  1125. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  1126. if (ret < 0) {
  1127. dev_err(&i2c_client->dev, "Failed to register new PCMs\n");
  1128. goto pcm_err;
  1129. }
  1130. /* Turn the chip on */
  1131. codec->bias_level = SND_SOC_BIAS_OFF;
  1132. wm8900_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1133. wm8900_add_controls(codec);
  1134. wm8900_add_widgets(codec);
  1135. ret = snd_soc_register_card(socdev);
  1136. if (ret < 0) {
  1137. dev_err(&i2c_client->dev, "Failed to register card\n");
  1138. goto card_err;
  1139. }
  1140. return ret;
  1141. card_err:
  1142. snd_soc_free_pcms(socdev);
  1143. snd_soc_dapm_free(socdev);
  1144. pcm_err:
  1145. kfree(codec->reg_cache);
  1146. priv_err:
  1147. kfree(codec->private_data);
  1148. return ret;
  1149. }
  1150. static struct snd_soc_device *wm8900_socdev;
  1151. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1152. static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
  1153. /* Magic definition of all other variables and things */
  1154. I2C_CLIENT_INSMOD;
  1155. static struct i2c_driver wm8900_i2c_driver;
  1156. static struct i2c_client client_template;
  1157. /* If the i2c layer weren't so broken, we could pass this kind of data
  1158. around */
  1159. static int wm8900_codec_probe(struct i2c_adapter *adap, int addr, int kind)
  1160. {
  1161. struct snd_soc_device *socdev = wm8900_socdev;
  1162. struct wm8900_setup_data *setup = socdev->codec_data;
  1163. struct snd_soc_codec *codec = socdev->codec;
  1164. struct i2c_client *i2c;
  1165. int ret;
  1166. if (addr != setup->i2c_address)
  1167. return -ENODEV;
  1168. dev_err(&adap->dev, "Probe on %x\n", addr);
  1169. client_template.adapter = adap;
  1170. client_template.addr = addr;
  1171. i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL);
  1172. if (i2c == NULL) {
  1173. kfree(codec);
  1174. return -ENOMEM;
  1175. }
  1176. i2c_set_clientdata(i2c, codec);
  1177. codec->control_data = i2c;
  1178. ret = i2c_attach_client(i2c);
  1179. if (ret < 0) {
  1180. dev_err(&adap->dev,
  1181. "failed to attach codec at addr %x\n", addr);
  1182. goto err;
  1183. }
  1184. ret = wm8900_init(socdev);
  1185. if (ret < 0) {
  1186. dev_err(&adap->dev, "failed to initialise WM8900\n");
  1187. goto err;
  1188. }
  1189. return ret;
  1190. err:
  1191. kfree(codec);
  1192. kfree(i2c);
  1193. return ret;
  1194. }
  1195. static int wm8900_i2c_detach(struct i2c_client *client)
  1196. {
  1197. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  1198. i2c_detach_client(client);
  1199. kfree(codec->reg_cache);
  1200. kfree(client);
  1201. return 0;
  1202. }
  1203. static int wm8900_i2c_attach(struct i2c_adapter *adap)
  1204. {
  1205. return i2c_probe(adap, &addr_data, wm8900_codec_probe);
  1206. }
  1207. /* corgi i2c codec control layer */
  1208. static struct i2c_driver wm8900_i2c_driver = {
  1209. .driver = {
  1210. .name = "WM8900 I2C codec",
  1211. .owner = THIS_MODULE,
  1212. },
  1213. .attach_adapter = wm8900_i2c_attach,
  1214. .detach_client = wm8900_i2c_detach,
  1215. .command = NULL,
  1216. };
  1217. static struct i2c_client client_template = {
  1218. .name = "WM8900",
  1219. .driver = &wm8900_i2c_driver,
  1220. };
  1221. #endif
  1222. static int wm8900_probe(struct platform_device *pdev)
  1223. {
  1224. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1225. struct wm8900_setup_data *setup;
  1226. struct snd_soc_codec *codec;
  1227. int ret = 0;
  1228. dev_info(&pdev->dev, "WM8900 Audio Codec\n");
  1229. setup = socdev->codec_data;
  1230. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  1231. if (codec == NULL)
  1232. return -ENOMEM;
  1233. mutex_init(&codec->mutex);
  1234. INIT_LIST_HEAD(&codec->dapm_widgets);
  1235. INIT_LIST_HEAD(&codec->dapm_paths);
  1236. socdev->codec = codec;
  1237. codec->set_bias_level = wm8900_set_bias_level;
  1238. wm8900_socdev = socdev;
  1239. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1240. if (setup->i2c_address) {
  1241. normal_i2c[0] = setup->i2c_address;
  1242. codec->hw_write = (hw_write_t)i2c_master_send;
  1243. ret = i2c_add_driver(&wm8900_i2c_driver);
  1244. if (ret != 0)
  1245. printk(KERN_ERR "can't add i2c driver");
  1246. }
  1247. #else
  1248. #error Non-I2C interfaces not yet supported
  1249. #endif
  1250. return ret;
  1251. }
  1252. /* power down chip */
  1253. static int wm8900_remove(struct platform_device *pdev)
  1254. {
  1255. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1256. struct snd_soc_codec *codec = socdev->codec;
  1257. if (codec->control_data)
  1258. wm8900_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1259. snd_soc_free_pcms(socdev);
  1260. snd_soc_dapm_free(socdev);
  1261. #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
  1262. i2c_del_driver(&wm8900_i2c_driver);
  1263. #endif
  1264. kfree(codec);
  1265. return 0;
  1266. }
  1267. struct snd_soc_codec_device soc_codec_dev_wm8900 = {
  1268. .probe = wm8900_probe,
  1269. .remove = wm8900_remove,
  1270. .suspend = wm8900_suspend,
  1271. .resume = wm8900_resume,
  1272. };
  1273. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8900);
  1274. MODULE_DESCRIPTION("ASoC WM8900 driver");
  1275. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>");
  1276. MODULE_LICENSE("GPL");