wm8750.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /*
  2. * wm8750.c -- WM8750 ALSA SoC audio driver
  3. *
  4. * Copyright 2005 Openedhand Ltd.
  5. *
  6. * Author: Richard Purdie <richard@openedhand.com>
  7. *
  8. * Based on WM8753.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm.h>
  19. #include <linux/i2c.h>
  20. #include <linux/platform_device.h>
  21. #include <sound/driver.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <sound/soc-dapm.h>
  27. #include <sound/initval.h>
  28. #include "wm8750.h"
  29. #define AUDIO_NAME "WM8750"
  30. #define WM8750_VERSION "0.11"
  31. /*
  32. * Debug
  33. */
  34. #define WM8750_DEBUG 0
  35. #ifdef WM8750_DEBUG
  36. #define dbg(format, arg...) \
  37. printk(KERN_DEBUG AUDIO_NAME ": " format "\n" , ## arg)
  38. #else
  39. #define dbg(format, arg...) do {} while (0)
  40. #endif
  41. #define err(format, arg...) \
  42. printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
  43. #define info(format, arg...) \
  44. printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
  45. #define warn(format, arg...) \
  46. printk(KERN_WARNING AUDIO_NAME ": " format "\n" , ## arg)
  47. static struct workqueue_struct *wm8750_workq = NULL;
  48. static struct work_struct wm8750_dapm_work;
  49. /*
  50. * wm8750 register cache
  51. * We can't read the WM8750 register space when we
  52. * are using 2 wire for device control, so we cache them instead.
  53. */
  54. static const u16 wm8750_reg[] = {
  55. 0x0097, 0x0097, 0x0079, 0x0079, /* 0 */
  56. 0x0000, 0x0008, 0x0000, 0x000a, /* 4 */
  57. 0x0000, 0x0000, 0x00ff, 0x00ff, /* 8 */
  58. 0x000f, 0x000f, 0x0000, 0x0000, /* 12 */
  59. 0x0000, 0x007b, 0x0000, 0x0032, /* 16 */
  60. 0x0000, 0x00c3, 0x00c3, 0x00c0, /* 20 */
  61. 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
  62. 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
  63. 0x0000, 0x0000, 0x0050, 0x0050, /* 32 */
  64. 0x0050, 0x0050, 0x0050, 0x0050, /* 36 */
  65. 0x0079, 0x0079, 0x0079, /* 40 */
  66. };
  67. #define WM8750_HIFI_DAIFMT \
  68. (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_RIGHT_J | \
  69. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_NB_IF | SND_SOC_DAIFMT_IB_NF | \
  70. SND_SOC_DAIFMT_IB_IF)
  71. #define WM8750_DIR \
  72. (SND_SOC_DAIDIR_PLAYBACK | SND_SOC_DAIDIR_CAPTURE)
  73. #define WM8750_HIFI_FSB \
  74. (SND_SOC_FSBD(1) | SND_SOC_FSBD(2) | SND_SOC_FSBD(4) | \
  75. SND_SOC_FSBD(8) | SND_SOC_FSBD(16))
  76. #define WM8750_HIFI_RATES \
  77. (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
  78. SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  79. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  80. #define WM8750_HIFI_BITS \
  81. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  82. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  83. static struct snd_soc_dai_mode wm8750_modes[] = {
  84. /* common codec frame and clock master modes */
  85. /* 8k */
  86. {
  87. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  88. .pcmfmt = WM8750_HIFI_BITS,
  89. .pcmrate = SNDRV_PCM_RATE_8000,
  90. .pcmdir = WM8750_DIR,
  91. .flags = SND_SOC_DAI_BFS_DIV,
  92. .fs = 1536,
  93. .bfs = WM8750_HIFI_FSB,
  94. },
  95. {
  96. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  97. .pcmfmt = WM8750_HIFI_BITS,
  98. .pcmrate = SNDRV_PCM_RATE_8000,
  99. .pcmdir = WM8750_DIR,
  100. .flags = SND_SOC_DAI_BFS_DIV,
  101. .fs = 1408,
  102. .bfs = WM8750_HIFI_FSB,
  103. },
  104. {
  105. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  106. .pcmfmt = WM8750_HIFI_BITS,
  107. .pcmrate = SNDRV_PCM_RATE_8000,
  108. .pcmdir = WM8750_DIR,
  109. .flags = SND_SOC_DAI_BFS_DIV,
  110. .fs = 2304,
  111. .bfs = WM8750_HIFI_FSB,
  112. },
  113. {
  114. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  115. .pcmfmt = WM8750_HIFI_BITS,
  116. .pcmrate = SNDRV_PCM_RATE_8000,
  117. .pcmdir = WM8750_DIR,
  118. .flags = SND_SOC_DAI_BFS_DIV,
  119. .fs = 2112,
  120. .bfs = WM8750_HIFI_FSB,
  121. },
  122. {
  123. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  124. .pcmfmt = WM8750_HIFI_BITS,
  125. .pcmrate = SNDRV_PCM_RATE_8000,
  126. .pcmdir = WM8750_DIR,
  127. .flags = SND_SOC_DAI_BFS_DIV,
  128. .fs = 1500,
  129. .bfs = WM8750_HIFI_FSB,
  130. },
  131. /* 11.025k */
  132. {
  133. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  134. .pcmfmt = WM8750_HIFI_BITS,
  135. .pcmrate = SNDRV_PCM_RATE_11025,
  136. .pcmdir = WM8750_DIR,
  137. .flags = SND_SOC_DAI_BFS_DIV,
  138. .fs = 1024,
  139. .bfs = WM8750_HIFI_FSB,
  140. },
  141. {
  142. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  143. .pcmfmt = WM8750_HIFI_BITS,
  144. .pcmrate = SNDRV_PCM_RATE_11025,
  145. .pcmdir = WM8750_DIR,
  146. .flags = SND_SOC_DAI_BFS_DIV,
  147. .fs = 1536,
  148. .bfs = WM8750_HIFI_FSB,
  149. },
  150. {
  151. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  152. .pcmfmt = WM8750_HIFI_BITS,
  153. .pcmrate = SNDRV_PCM_RATE_11025,
  154. .pcmdir = WM8750_DIR,
  155. .flags = SND_SOC_DAI_BFS_DIV,
  156. .fs = 1088,
  157. .bfs = WM8750_HIFI_FSB,
  158. },
  159. /* 16k */
  160. {
  161. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  162. .pcmfmt = WM8750_HIFI_BITS,
  163. .pcmrate = SNDRV_PCM_RATE_16000,
  164. .pcmdir = WM8750_DIR,
  165. .flags = SND_SOC_DAI_BFS_DIV,
  166. .fs = 768,
  167. .bfs = WM8750_HIFI_FSB,
  168. },
  169. {
  170. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  171. .pcmfmt = WM8750_HIFI_BITS,
  172. .pcmrate = SNDRV_PCM_RATE_16000,
  173. .pcmdir = WM8750_DIR,
  174. .flags = SND_SOC_DAI_BFS_DIV,
  175. .fs = 1152,
  176. .bfs = WM8750_HIFI_FSB
  177. },
  178. {
  179. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  180. .pcmfmt = WM8750_HIFI_BITS,
  181. .pcmrate = SNDRV_PCM_RATE_16000,
  182. .pcmdir = WM8750_DIR,
  183. .flags = SND_SOC_DAI_BFS_DIV,
  184. .fs = 750,
  185. .bfs = WM8750_HIFI_FSB,
  186. },
  187. /* 22.05k */
  188. {
  189. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  190. .pcmfmt = WM8750_HIFI_BITS,
  191. .pcmrate = SNDRV_PCM_RATE_22050,
  192. .pcmdir = WM8750_DIR,
  193. .flags = SND_SOC_DAI_BFS_DIV,
  194. .fs = 512,
  195. .bfs = WM8750_HIFI_FSB,
  196. },
  197. {
  198. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  199. .pcmfmt = WM8750_HIFI_BITS,
  200. .pcmrate = SNDRV_PCM_RATE_22050,
  201. .pcmdir = WM8750_DIR,
  202. .flags = SND_SOC_DAI_BFS_DIV,
  203. .fs = 768,
  204. .bfs = WM8750_HIFI_FSB,
  205. },
  206. {
  207. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  208. .pcmfmt = WM8750_HIFI_BITS,
  209. .pcmrate = SNDRV_PCM_RATE_22050,
  210. .pcmdir = WM8750_DIR,
  211. .flags = SND_SOC_DAI_BFS_DIV,
  212. .fs = 544,
  213. .bfs = WM8750_HIFI_FSB,
  214. },
  215. /* 32k */
  216. {
  217. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  218. .pcmfmt = WM8750_HIFI_BITS,
  219. .pcmrate = SNDRV_PCM_RATE_32000,
  220. .pcmdir = WM8750_DIR,
  221. .flags = SND_SOC_DAI_BFS_DIV,
  222. .fs = 384,
  223. .bfs = WM8750_HIFI_FSB,
  224. },
  225. {
  226. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  227. .pcmfmt = WM8750_HIFI_BITS,
  228. .pcmrate = SNDRV_PCM_RATE_32000,
  229. .pcmdir = WM8750_DIR,
  230. .flags = SND_SOC_DAI_BFS_DIV,
  231. .fs = 576,
  232. .bfs = WM8750_HIFI_FSB,
  233. },
  234. {
  235. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  236. .pcmfmt = WM8750_HIFI_BITS,
  237. .pcmrate = SNDRV_PCM_RATE_32000,
  238. .pcmdir = WM8750_DIR,
  239. .flags = SND_SOC_DAI_BFS_DIV,
  240. .fs = 375,
  241. .bfs = WM8750_HIFI_FSB,
  242. },
  243. /* 44.1k & 48k */
  244. {
  245. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  246. .pcmfmt = WM8750_HIFI_BITS,
  247. .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  248. .pcmdir = WM8750_DIR,
  249. .flags = SND_SOC_DAI_BFS_DIV,
  250. .fs = 256,
  251. .bfs = WM8750_HIFI_FSB,
  252. },
  253. {
  254. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  255. .pcmfmt = WM8750_HIFI_BITS,
  256. .pcmrate = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  257. .pcmdir = WM8750_DIR,
  258. .flags = SND_SOC_DAI_BFS_DIV,
  259. .fs = 384,
  260. .bfs = WM8750_HIFI_FSB,
  261. },
  262. {
  263. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  264. .pcmfmt = WM8750_HIFI_BITS,
  265. .pcmrate = SNDRV_PCM_RATE_44100,
  266. .pcmdir = WM8750_DIR,
  267. .flags = SND_SOC_DAI_BFS_DIV,
  268. .fs = 272,
  269. .bfs = WM8750_HIFI_FSB,
  270. },
  271. {
  272. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  273. .pcmfmt = WM8750_HIFI_BITS,
  274. .pcmrate = SNDRV_PCM_RATE_48000,
  275. .pcmdir = WM8750_DIR,
  276. .flags = SND_SOC_DAI_BFS_DIV,
  277. .fs = 250,
  278. .bfs = WM8750_HIFI_FSB,
  279. },
  280. /* 88.2k & 96k */
  281. {
  282. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  283. .pcmfmt = WM8750_HIFI_BITS,
  284. .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
  285. .pcmdir = WM8750_DIR,
  286. .flags = SND_SOC_DAI_BFS_DIV,
  287. .fs = 128,
  288. .bfs = WM8750_HIFI_FSB,
  289. },
  290. {
  291. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  292. .pcmfmt = WM8750_HIFI_BITS,
  293. .pcmrate = SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000,
  294. .pcmdir = WM8750_DIR,
  295. .flags = SND_SOC_DAI_BFS_DIV,
  296. .fs = 192,
  297. .bfs = WM8750_HIFI_FSB,
  298. },
  299. {
  300. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  301. .pcmfmt = WM8750_HIFI_BITS,
  302. .pcmrate = SNDRV_PCM_RATE_88200,
  303. .pcmdir = WM8750_DIR,
  304. .flags = SND_SOC_DAI_BFS_DIV,
  305. .fs = 136,
  306. .bfs = WM8750_HIFI_FSB,
  307. },
  308. {
  309. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBM_CFM,
  310. .pcmfmt = WM8750_HIFI_BITS,
  311. .pcmrate = SNDRV_PCM_RATE_96000,
  312. .pcmdir = WM8750_DIR,
  313. .flags = SND_SOC_DAI_BFS_DIV,
  314. .fs = 125,
  315. .bfs = WM8750_HIFI_FSB,
  316. },
  317. /* codec frame and clock slave modes */
  318. {
  319. .fmt = WM8750_HIFI_DAIFMT | SND_SOC_DAIFMT_CBS_CFS,
  320. .pcmfmt = WM8750_HIFI_BITS,
  321. .pcmrate = WM8750_HIFI_RATES,
  322. .pcmdir = WM8750_DIR,
  323. .flags = SND_SOC_DAI_BFS_DIV,
  324. .fs = SND_SOC_FS_ALL,
  325. .bfs = SND_SOC_FSBD_ALL,
  326. },
  327. };
  328. /*
  329. * read wm8750 register cache
  330. */
  331. static inline unsigned int wm8750_read_reg_cache(struct snd_soc_codec *codec,
  332. unsigned int reg)
  333. {
  334. u16 *cache = codec->reg_cache;
  335. if (reg > WM8750_CACHE_REGNUM)
  336. return -1;
  337. return cache[reg];
  338. }
  339. /*
  340. * write wm8750 register cache
  341. */
  342. static inline void wm8750_write_reg_cache(struct snd_soc_codec *codec,
  343. unsigned int reg, unsigned int value)
  344. {
  345. u16 *cache = codec->reg_cache;
  346. if (reg > WM8750_CACHE_REGNUM)
  347. return;
  348. cache[reg] = value;
  349. }
  350. static int wm8750_write(struct snd_soc_codec *codec, unsigned int reg,
  351. unsigned int value)
  352. {
  353. u8 data[2];
  354. /* data is
  355. * D15..D9 WM8753 register offset
  356. * D8...D0 register data
  357. */
  358. data[0] = (reg << 1) | ((value >> 8) & 0x0001);
  359. data[1] = value & 0x00ff;
  360. wm8750_write_reg_cache (codec, reg, value);
  361. if (codec->hw_write(codec->control_data, data, 2) == 2)
  362. return 0;
  363. else
  364. return -EIO;
  365. }
  366. #define wm8750_reset(c) wm8750_write(c, WM8750_RESET, 0)
  367. /*
  368. * WM8750 Controls
  369. */
  370. static const char *wm8750_bass[] = {"Linear Control", "Adaptive Boost"};
  371. static const char *wm8750_bass_filter[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
  372. static const char *wm8750_treble[] = {"8kHz", "4kHz"};
  373. static const char *wm8750_3d_lc[] = {"200Hz", "500Hz"};
  374. static const char *wm8750_3d_uc[] = {"2.2kHz", "1.5kHz"};
  375. static const char *wm8750_3d_func[] = {"Capture", "Playback"};
  376. static const char *wm8750_alc_func[] = {"Off", "Right", "Left", "Stereo"};
  377. static const char *wm8750_ng_type[] = {"Constant PGA Gain",
  378. "Mute ADC Output"};
  379. static const char *wm8750_line_mux[] = {"Line 1", "Line 2", "Line 3", "PGA",
  380. "Differential"};
  381. static const char *wm8750_pga_sel[] = {"Line 1", "Line 2", "Line 3",
  382. "Differential"};
  383. static const char *wm8750_out3[] = {"VREF", "ROUT1 + Vol", "MonoOut",
  384. "ROUT1"};
  385. static const char *wm8750_diff_sel[] = {"Line 1", "Line 2"};
  386. static const char *wm8750_adcpol[] = {"Normal", "L Invert", "R Invert",
  387. "L + R Invert"};
  388. static const char *wm8750_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
  389. static const char *wm8750_mono_mux[] = {"Stereo", "Mono (Left)",
  390. "Mono (Right)", "Digital Mono"};
  391. static const struct soc_enum wm8750_enum[] = {
  392. SOC_ENUM_SINGLE(WM8750_BASS, 7, 2, wm8750_bass),
  393. SOC_ENUM_SINGLE(WM8750_BASS, 6, 2, wm8750_bass_filter),
  394. SOC_ENUM_SINGLE(WM8750_TREBLE, 6, 2, wm8750_treble),
  395. SOC_ENUM_SINGLE(WM8750_3D, 5, 2, wm8750_3d_lc),
  396. SOC_ENUM_SINGLE(WM8750_3D, 6, 2, wm8750_3d_uc),
  397. SOC_ENUM_SINGLE(WM8750_3D, 7, 2, wm8750_3d_func),
  398. SOC_ENUM_SINGLE(WM8750_ALC1, 7, 4, wm8750_alc_func),
  399. SOC_ENUM_SINGLE(WM8750_NGATE, 1, 2, wm8750_ng_type),
  400. SOC_ENUM_SINGLE(WM8750_LOUTM1, 0, 5, wm8750_line_mux),
  401. SOC_ENUM_SINGLE(WM8750_ROUTM1, 0, 5, wm8750_line_mux),
  402. SOC_ENUM_SINGLE(WM8750_LADCIN, 6, 4, wm8750_pga_sel), /* 10 */
  403. SOC_ENUM_SINGLE(WM8750_RADCIN, 6, 4, wm8750_pga_sel),
  404. SOC_ENUM_SINGLE(WM8750_ADCTL2, 7, 4, wm8750_out3),
  405. SOC_ENUM_SINGLE(WM8750_ADCIN, 8, 2, wm8750_diff_sel),
  406. SOC_ENUM_SINGLE(WM8750_ADCDAC, 5, 4, wm8750_adcpol),
  407. SOC_ENUM_SINGLE(WM8750_ADCDAC, 1, 4, wm8750_deemph),
  408. SOC_ENUM_SINGLE(WM8750_ADCIN, 6, 4, wm8750_mono_mux), /* 16 */
  409. };
  410. static const struct snd_kcontrol_new wm8750_snd_controls[] = {
  411. SOC_DOUBLE_R("Capture Volume", WM8750_LINVOL, WM8750_RINVOL, 0, 63, 0),
  412. SOC_DOUBLE_R("Capture ZC Switch", WM8750_LINVOL, WM8750_RINVOL, 6, 1, 0),
  413. SOC_DOUBLE_R("Capture Switch", WM8750_LINVOL, WM8750_RINVOL, 7, 1, 1),
  414. SOC_DOUBLE_R("Out1 Playback ZC Switch", WM8750_LOUT1V,
  415. WM8750_ROUT1V, 7, 1, 0),
  416. SOC_DOUBLE_R("Out2 Playback ZC Switch", WM8750_LOUT2V,
  417. WM8750_ROUT2V, 7, 1, 0),
  418. SOC_ENUM("Playback De-emphasis", wm8750_enum[15]),
  419. SOC_ENUM("Capture Polarity", wm8750_enum[14]),
  420. SOC_SINGLE("Playback 6dB Attenuate", WM8750_ADCDAC, 7, 1, 0),
  421. SOC_SINGLE("Capture 6dB Attenuate", WM8750_ADCDAC, 8, 1, 0),
  422. SOC_DOUBLE_R("PCM Volume", WM8750_LDAC, WM8750_RDAC, 0, 255, 0),
  423. SOC_ENUM("Bass Boost", wm8750_enum[0]),
  424. SOC_ENUM("Bass Filter", wm8750_enum[1]),
  425. SOC_SINGLE("Bass Volume", WM8750_BASS, 0, 15, 1),
  426. SOC_SINGLE("Treble Volume", WM8750_TREBLE, 0, 15, 0),
  427. SOC_ENUM("Treble Cut-off", wm8750_enum[2]),
  428. SOC_SINGLE("3D Switch", WM8750_3D, 0, 1, 0),
  429. SOC_SINGLE("3D Volume", WM8750_3D, 1, 15, 0),
  430. SOC_ENUM("3D Lower Cut-off", wm8750_enum[3]),
  431. SOC_ENUM("3D Upper Cut-off", wm8750_enum[4]),
  432. SOC_ENUM("3D Mode", wm8750_enum[5]),
  433. SOC_SINGLE("ALC Capture Target Volume", WM8750_ALC1, 0, 7, 0),
  434. SOC_SINGLE("ALC Capture Max Volume", WM8750_ALC1, 4, 7, 0),
  435. SOC_ENUM("ALC Capture Function", wm8750_enum[6]),
  436. SOC_SINGLE("ALC Capture ZC Switch", WM8750_ALC2, 7, 1, 0),
  437. SOC_SINGLE("ALC Capture Hold Time", WM8750_ALC2, 0, 15, 0),
  438. SOC_SINGLE("ALC Capture Decay Time", WM8750_ALC3, 4, 15, 0),
  439. SOC_SINGLE("ALC Capture Attack Time", WM8750_ALC3, 0, 15, 0),
  440. SOC_SINGLE("ALC Capture NG Threshold", WM8750_NGATE, 3, 31, 0),
  441. SOC_ENUM("ALC Capture NG Type", wm8750_enum[4]),
  442. SOC_SINGLE("ALC Capture NG Switch", WM8750_NGATE, 0, 1, 0),
  443. SOC_SINGLE("Left ADC Capture Volume", WM8750_LADC, 0, 255, 0),
  444. SOC_SINGLE("Right ADC Capture Volume", WM8750_RADC, 0, 255, 0),
  445. SOC_SINGLE("ZC Timeout Switch", WM8750_ADCTL1, 0, 1, 0),
  446. SOC_SINGLE("Playback Invert Switch", WM8750_ADCTL1, 1, 1, 0),
  447. SOC_SINGLE("Right Out2 Playback Invert Switch", WM8750_ADCTL2, 4, 1, 0),
  448. /* Unimplemented */
  449. /* ADCDAC Bit 0 - ADCHPD */
  450. /* ADCDAC Bit 4 - HPOR */
  451. /* ADCTL1 Bit 2,3 - DATSEL */
  452. /* ADCTL1 Bit 4,5 - DMONOMIX */
  453. /* ADCTL1 Bit 6,7 - VSEL */
  454. /* ADCTL2 Bit 2 - LRCM */
  455. /* ADCTL2 Bit 3 - TRI */
  456. /* ADCTL3 Bit 5 - HPFLREN */
  457. /* ADCTL3 Bit 6 - VROI */
  458. /* ADCTL3 Bit 7,8 - ADCLRM */
  459. /* ADCIN Bit 4 - LDCM */
  460. /* ADCIN Bit 5 - RDCM */
  461. SOC_DOUBLE_R("Mic Boost", WM8750_LADCIN, WM8750_RADCIN, 4, 3, 0),
  462. SOC_DOUBLE_R("Bypass Left Playback Volume", WM8750_LOUTM1,
  463. WM8750_LOUTM2, 4, 7, 1),
  464. SOC_DOUBLE_R("Bypass Right Playback Volume", WM8750_ROUTM1,
  465. WM8750_ROUTM2, 4, 7, 1),
  466. SOC_DOUBLE_R("Bypass Mono Playback Volume", WM8750_MOUTM1,
  467. WM8750_MOUTM2, 4, 7, 1),
  468. SOC_SINGLE("Mono Playback ZC Switch", WM8750_MOUTV, 7, 1, 0),
  469. SOC_DOUBLE_R("Out1 Playback Volume", WM8750_LOUT1V, WM8750_ROUT1V, 0, 127, 0),
  470. SOC_DOUBLE_R("Out2 Playback Volume", WM8750_LOUT2V, WM8750_ROUT2V, 0, 127, 0),
  471. SOC_SINGLE("Mono Playback Volume", WM8750_MOUTV, 0, 127, 0),
  472. };
  473. /* add non dapm controls */
  474. static int wm8750_add_controls(struct snd_soc_codec *codec)
  475. {
  476. int err, i;
  477. for (i = 0; i < ARRAY_SIZE(wm8750_snd_controls); i++) {
  478. err = snd_ctl_add(codec->card,
  479. snd_soc_cnew(&wm8750_snd_controls[i],codec, NULL));
  480. if (err < 0)
  481. return err;
  482. }
  483. return 0;
  484. }
  485. /*
  486. * DAPM Controls
  487. */
  488. /* Left Mixer */
  489. static const struct snd_kcontrol_new wm8750_left_mixer_controls[] = {
  490. SOC_DAPM_SINGLE("Playback Switch", WM8750_LOUTM1, 8, 1, 0),
  491. SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_LOUTM1, 7, 1, 0),
  492. SOC_DAPM_SINGLE("Right Playback Switch", WM8750_LOUTM2, 8, 1, 0),
  493. SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_LOUTM2, 7, 1, 0),
  494. };
  495. /* Right Mixer */
  496. static const struct snd_kcontrol_new wm8750_right_mixer_controls[] = {
  497. SOC_DAPM_SINGLE("Left Playback Switch", WM8750_ROUTM1, 8, 1, 0),
  498. SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_ROUTM1, 7, 1, 0),
  499. SOC_DAPM_SINGLE("Playback Switch", WM8750_ROUTM2, 8, 1, 0),
  500. SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_ROUTM2, 7, 1, 0),
  501. };
  502. /* Mono Mixer */
  503. static const struct snd_kcontrol_new wm8750_mono_mixer_controls[] = {
  504. SOC_DAPM_SINGLE("Left Playback Switch", WM8750_MOUTM1, 8, 1, 0),
  505. SOC_DAPM_SINGLE("Left Bypass Switch", WM8750_MOUTM1, 7, 1, 0),
  506. SOC_DAPM_SINGLE("Right Playback Switch", WM8750_MOUTM2, 8, 1, 0),
  507. SOC_DAPM_SINGLE("Right Bypass Switch", WM8750_MOUTM2, 7, 1, 0),
  508. };
  509. /* Left Line Mux */
  510. static const struct snd_kcontrol_new wm8750_left_line_controls =
  511. SOC_DAPM_ENUM("Route", wm8750_enum[8]);
  512. /* Right Line Mux */
  513. static const struct snd_kcontrol_new wm8750_right_line_controls =
  514. SOC_DAPM_ENUM("Route", wm8750_enum[9]);
  515. /* Left PGA Mux */
  516. static const struct snd_kcontrol_new wm8750_left_pga_controls =
  517. SOC_DAPM_ENUM("Route", wm8750_enum[10]);
  518. /* Right PGA Mux */
  519. static const struct snd_kcontrol_new wm8750_right_pga_controls =
  520. SOC_DAPM_ENUM("Route", wm8750_enum[11]);
  521. /* Out 3 Mux */
  522. static const struct snd_kcontrol_new wm8750_out3_controls =
  523. SOC_DAPM_ENUM("Route", wm8750_enum[12]);
  524. /* Differential Mux */
  525. static const struct snd_kcontrol_new wm8750_diffmux_controls =
  526. SOC_DAPM_ENUM("Route", wm8750_enum[13]);
  527. /* Mono ADC Mux */
  528. static const struct snd_kcontrol_new wm8750_monomux_controls =
  529. SOC_DAPM_ENUM("Route", wm8750_enum[16]);
  530. static const struct snd_soc_dapm_widget wm8750_dapm_widgets[] = {
  531. SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
  532. &wm8750_left_mixer_controls[0],
  533. ARRAY_SIZE(wm8750_left_mixer_controls)),
  534. SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
  535. &wm8750_right_mixer_controls[0],
  536. ARRAY_SIZE(wm8750_right_mixer_controls)),
  537. SND_SOC_DAPM_MIXER("Mono Mixer", WM8750_PWR2, 2, 0,
  538. &wm8750_mono_mixer_controls[0],
  539. ARRAY_SIZE(wm8750_mono_mixer_controls)),
  540. SND_SOC_DAPM_PGA("Right Out 2", WM8750_PWR2, 3, 0, NULL, 0),
  541. SND_SOC_DAPM_PGA("Left Out 2", WM8750_PWR2, 4, 0, NULL, 0),
  542. SND_SOC_DAPM_PGA("Right Out 1", WM8750_PWR2, 5, 0, NULL, 0),
  543. SND_SOC_DAPM_PGA("Left Out 1", WM8750_PWR2, 6, 0, NULL, 0),
  544. SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8750_PWR2, 7, 0),
  545. SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8750_PWR2, 8, 0),
  546. SND_SOC_DAPM_MICBIAS("Mic Bias", WM8750_PWR1, 1, 0),
  547. SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8750_PWR1, 2, 0),
  548. SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8750_PWR1, 3, 0),
  549. SND_SOC_DAPM_MUX("Left PGA Mux", WM8750_PWR1, 5, 0,
  550. &wm8750_left_pga_controls),
  551. SND_SOC_DAPM_MUX("Right PGA Mux", WM8750_PWR1, 4, 0,
  552. &wm8750_right_pga_controls),
  553. SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
  554. &wm8750_left_line_controls),
  555. SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
  556. &wm8750_right_line_controls),
  557. SND_SOC_DAPM_MUX("Out3 Mux", SND_SOC_NOPM, 0, 0, &wm8750_out3_controls),
  558. SND_SOC_DAPM_PGA("Out 3", WM8750_PWR2, 1, 0, NULL, 0),
  559. SND_SOC_DAPM_PGA("Mono Out 1", WM8750_PWR2, 2, 0, NULL, 0),
  560. SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
  561. &wm8750_diffmux_controls),
  562. SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
  563. &wm8750_monomux_controls),
  564. SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
  565. &wm8750_monomux_controls),
  566. SND_SOC_DAPM_OUTPUT("LOUT1"),
  567. SND_SOC_DAPM_OUTPUT("ROUT1"),
  568. SND_SOC_DAPM_OUTPUT("LOUT2"),
  569. SND_SOC_DAPM_OUTPUT("ROUT2"),
  570. SND_SOC_DAPM_OUTPUT("MONO"),
  571. SND_SOC_DAPM_OUTPUT("OUT3"),
  572. SND_SOC_DAPM_INPUT("LINPUT1"),
  573. SND_SOC_DAPM_INPUT("LINPUT2"),
  574. SND_SOC_DAPM_INPUT("LINPUT3"),
  575. SND_SOC_DAPM_INPUT("RINPUT1"),
  576. SND_SOC_DAPM_INPUT("RINPUT2"),
  577. SND_SOC_DAPM_INPUT("RINPUT3"),
  578. };
  579. static const char *audio_map[][3] = {
  580. /* left mixer */
  581. {"Left Mixer", "Playback Switch", "Left DAC"},
  582. {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
  583. {"Left Mixer", "Right Playback Switch", "Right DAC"},
  584. {"Left Mixer", "Right Bypass Switch", "Right Line Mux"},
  585. /* right mixer */
  586. {"Right Mixer", "Left Playback Switch", "Left DAC"},
  587. {"Right Mixer", "Left Bypass Switch", "Left Line Mux"},
  588. {"Right Mixer", "Playback Switch", "Right DAC"},
  589. {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
  590. /* left out 1 */
  591. {"Left Out 1", NULL, "Left Mixer"},
  592. {"LOUT1", NULL, "Left Out 1"},
  593. /* left out 2 */
  594. {"Left Out 2", NULL, "Left Mixer"},
  595. {"LOUT2", NULL, "Left Out 2"},
  596. /* right out 1 */
  597. {"Right Out 1", NULL, "Right Mixer"},
  598. {"ROUT1", NULL, "Right Out 1"},
  599. /* right out 2 */
  600. {"Right Out 2", NULL, "Right Mixer"},
  601. {"ROUT2", NULL, "Right Out 2"},
  602. /* mono mixer */
  603. {"Mono Mixer", "Left Playback Switch", "Left DAC"},
  604. {"Mono Mixer", "Left Bypass Switch", "Left Line Mux"},
  605. {"Mono Mixer", "Right Playback Switch", "Right DAC"},
  606. {"Mono Mixer", "Right Bypass Switch", "Right Line Mux"},
  607. /* mono out */
  608. {"Mono Out 1", NULL, "Mono Mixer"},
  609. {"MONO1", NULL, "Mono Out 1"},
  610. /* out 3 */
  611. {"Out3 Mux", "VREF", "VREF"},
  612. {"Out3 Mux", "ROUT1 + Vol", "ROUT1"},
  613. {"Out3 Mux", "ROUT1", "Right Mixer"},
  614. {"Out3 Mux", "MonoOut", "MONO1"},
  615. {"Out 3", NULL, "Out3 Mux"},
  616. {"OUT3", NULL, "Out 3"},
  617. /* Left Line Mux */
  618. {"Left Line Mux", "Line 1", "LINPUT1"},
  619. {"Left Line Mux", "Line 2", "LINPUT2"},
  620. {"Left Line Mux", "Line 3", "LINPUT3"},
  621. {"Left Line Mux", "PGA", "Left PGA Mux"},
  622. {"Left Line Mux", "Differential", "Differential Mux"},
  623. /* Right Line Mux */
  624. {"Right Line Mux", "Line 1", "RINPUT1"},
  625. {"Right Line Mux", "Line 2", "RINPUT2"},
  626. {"Right Line Mux", "Line 3", "RINPUT3"},
  627. {"Right Line Mux", "PGA", "Right PGA Mux"},
  628. {"Right Line Mux", "Differential", "Differential Mux"},
  629. /* Left PGA Mux */
  630. {"Left PGA Mux", "Line 1", "LINPUT1"},
  631. {"Left PGA Mux", "Line 2", "LINPUT2"},
  632. {"Left PGA Mux", "Line 3", "LINPUT3"},
  633. {"Left PGA Mux", "Differential", "Differential Mux"},
  634. /* Right PGA Mux */
  635. {"Right PGA Mux", "Line 1", "RINPUT1"},
  636. {"Right PGA Mux", "Line 2", "RINPUT2"},
  637. {"Right PGA Mux", "Line 3", "RINPUT3"},
  638. {"Right PGA Mux", "Differential", "Differential Mux"},
  639. /* Differential Mux */
  640. {"Differential Mux", "Line 1", "LINPUT1"},
  641. {"Differential Mux", "Line 1", "RINPUT1"},
  642. {"Differential Mux", "Line 2", "LINPUT2"},
  643. {"Differential Mux", "Line 2", "RINPUT2"},
  644. /* Left ADC Mux */
  645. {"Left ADC Mux", "Stereo", "Left PGA Mux"},
  646. {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
  647. {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
  648. /* Right ADC Mux */
  649. {"Right ADC Mux", "Stereo", "Right PGA Mux"},
  650. {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
  651. {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
  652. /* ADC */
  653. {"Left ADC", NULL, "Left ADC Mux"},
  654. {"Right ADC", NULL, "Right ADC Mux"},
  655. /* terminator */
  656. {NULL, NULL, NULL},
  657. };
  658. static int wm8750_add_widgets(struct snd_soc_codec *codec)
  659. {
  660. int i;
  661. for(i = 0; i < ARRAY_SIZE(wm8750_dapm_widgets); i++) {
  662. snd_soc_dapm_new_control(codec, &wm8750_dapm_widgets[i]);
  663. }
  664. /* set up audio path audio_mapnects */
  665. for(i = 0; audio_map[i][0] != NULL; i++) {
  666. snd_soc_dapm_connect_input(codec, audio_map[i][0],
  667. audio_map[i][1], audio_map[i][2]);
  668. }
  669. snd_soc_dapm_new_widgets(codec);
  670. return 0;
  671. }
  672. struct _coeff_div {
  673. u32 mclk;
  674. u32 rate;
  675. u16 fs;
  676. u8 sr:5;
  677. u8 usb:1;
  678. };
  679. /* codec hifi mclk clock divider coefficients */
  680. static const struct _coeff_div coeff_div[] = {
  681. /* 8k */
  682. {12288000, 8000, 1536, 0x6, 0x0},
  683. {11289600, 8000, 1408, 0x16, 0x0},
  684. {18432000, 8000, 2304, 0x7, 0x0},
  685. {16934400, 8000, 2112, 0x17, 0x0},
  686. {12000000, 8000, 1500, 0x6, 0x1},
  687. /* 11.025k */
  688. {11289600, 11025, 1024, 0x18, 0x0},
  689. {16934400, 11025, 1536, 0x19, 0x0},
  690. {12000000, 11025, 1088, 0x19, 0x1},
  691. /* 16k */
  692. {12288000, 16000, 768, 0xa, 0x0},
  693. {18432000, 16000, 1152, 0xb, 0x0},
  694. {12000000, 16000, 750, 0xa, 0x1},
  695. /* 22.05k */
  696. {11289600, 22050, 512, 0x1a, 0x0},
  697. {16934400, 22050, 768, 0x1b, 0x0},
  698. {12000000, 22050, 544, 0x1b, 0x1},
  699. /* 32k */
  700. {12288000, 32000, 384, 0xc, 0x0},
  701. {18432000, 32000, 576, 0xd, 0x0},
  702. {12000000, 32000, 375, 0xa, 0x1},
  703. /* 44.1k */
  704. {11289600, 44100, 256, 0x10, 0x0},
  705. {16934400, 44100, 384, 0x11, 0x0},
  706. {12000000, 44100, 272, 0x11, 0x1},
  707. /* 48k */
  708. {12288000, 48000, 256, 0x0, 0x0},
  709. {18432000, 48000, 384, 0x1, 0x0},
  710. {12000000, 48000, 250, 0x0, 0x1},
  711. /* 88.2k */
  712. {11289600, 88200, 128, 0x1e, 0x0},
  713. {16934400, 88200, 192, 0x1f, 0x0},
  714. {12000000, 88200, 136, 0x1f, 0x1},
  715. /* 96k */
  716. {12288000, 96000, 128, 0xe, 0x0},
  717. {18432000, 96000, 192, 0xf, 0x0},
  718. {12000000, 96000, 125, 0xe, 0x1},
  719. };
  720. static inline int get_coeff(int mclk, int rate)
  721. {
  722. int i;
  723. for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
  724. if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
  725. return i;
  726. }
  727. return -EINVAL;
  728. }
  729. /* WM8750 supports numerous input clocks per sample rate */
  730. static unsigned int wm8750_config_sysclk(struct snd_soc_codec_dai *dai,
  731. struct snd_soc_clock_info *info, unsigned int clk)
  732. {
  733. dai->mclk = 0;
  734. /* check that the calculated FS and rate actually match a clock from
  735. * the machine driver */
  736. if (info->fs * info->rate == clk)
  737. dai->mclk = clk;
  738. return dai->mclk;
  739. }
  740. static int wm8750_pcm_prepare(struct snd_pcm_substream *substream)
  741. {
  742. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  743. struct snd_soc_device *socdev = rtd->socdev;
  744. struct snd_soc_codec *codec = socdev->codec;
  745. u16 iface = 0, bfs, srate = 0;
  746. int i = get_coeff(rtd->codec_dai->mclk,
  747. snd_soc_get_rate(rtd->codec_dai->dai_runtime.pcmrate));
  748. /* is coefficient valid ? */
  749. if (i < 0)
  750. return i;
  751. bfs = SND_SOC_FSB_REAL(rtd->codec_dai->dai_runtime.bfs);
  752. /* set master/slave audio interface */
  753. switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
  754. case SND_SOC_DAIFMT_CBM_CFM:
  755. iface = 0x0040;
  756. break;
  757. case SND_SOC_DAIFMT_CBS_CFS:
  758. break;
  759. }
  760. /* interface format */
  761. switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  762. case SND_SOC_DAIFMT_I2S:
  763. iface |= 0x0002;
  764. break;
  765. case SND_SOC_DAIFMT_RIGHT_J:
  766. break;
  767. case SND_SOC_DAIFMT_LEFT_J:
  768. iface |= 0x0001;
  769. break;
  770. case SND_SOC_DAIFMT_DSP_A:
  771. iface |= 0x0003;
  772. break;
  773. case SND_SOC_DAIFMT_DSP_B:
  774. iface |= 0x0013;
  775. break;
  776. }
  777. /* bit size */
  778. switch (rtd->codec_dai->dai_runtime.pcmfmt) {
  779. case SNDRV_PCM_FMTBIT_S16_LE:
  780. break;
  781. case SNDRV_PCM_FMTBIT_S20_3LE:
  782. iface |= 0x0004;
  783. break;
  784. case SNDRV_PCM_FMTBIT_S24_LE:
  785. iface |= 0x0008;
  786. break;
  787. case SNDRV_PCM_FMTBIT_S32_LE:
  788. iface |= 0x000c;
  789. break;
  790. }
  791. /* clock inversion */
  792. switch (rtd->codec_dai->dai_runtime.fmt & SND_SOC_DAIFMT_INV_MASK) {
  793. case SND_SOC_DAIFMT_NB_NF:
  794. break;
  795. case SND_SOC_DAIFMT_IB_IF:
  796. iface |= 0x0090;
  797. break;
  798. case SND_SOC_DAIFMT_IB_NF:
  799. iface |= 0x0080;
  800. break;
  801. case SND_SOC_DAIFMT_NB_IF:
  802. iface |= 0x0010;
  803. break;
  804. }
  805. /* set bclk divisor rate */
  806. switch (bfs) {
  807. case 1:
  808. break;
  809. case 4:
  810. srate |= (0x1 << 7);
  811. break;
  812. case 8:
  813. srate |= (0x2 << 7);
  814. break;
  815. case 16:
  816. srate |= (0x3 << 7);
  817. break;
  818. }
  819. /* set iface & srate */
  820. wm8750_write(codec, WM8750_IFACE, iface);
  821. wm8750_write(codec, WM8750_SRATE, srate |
  822. (coeff_div[i].sr << 1) | coeff_div[i].usb);
  823. return 0;
  824. }
  825. static int wm8750_mute(struct snd_soc_codec *codec,
  826. struct snd_soc_codec_dai *dai, int mute)
  827. {
  828. u16 mute_reg = wm8750_read_reg_cache(codec, WM8750_ADCDAC) & 0xfff7;
  829. if (mute)
  830. wm8750_write(codec, WM8750_ADCDAC, mute_reg | 0x8);
  831. else
  832. wm8750_write(codec, WM8750_ADCDAC, mute_reg);
  833. return 0;
  834. }
  835. static int wm8750_dapm_event(struct snd_soc_codec *codec, int event)
  836. {
  837. u16 pwr_reg = wm8750_read_reg_cache(codec, WM8750_PWR1) & 0xfe3e;
  838. switch (event) {
  839. case SNDRV_CTL_POWER_D0: /* full On */
  840. /* set vmid to 50k and unmute dac */
  841. wm8750_write(codec, WM8750_PWR1, pwr_reg | 0x00c0);
  842. break;
  843. case SNDRV_CTL_POWER_D1: /* partial On */
  844. case SNDRV_CTL_POWER_D2: /* partial On */
  845. /* set vmid to 5k for quick power up */
  846. wm8750_write(codec, WM8750_PWR1, pwr_reg | 0x01c1);
  847. break;
  848. case SNDRV_CTL_POWER_D3hot: /* Off, with power */
  849. /* mute dac and set vmid to 500k, enable VREF */
  850. wm8750_write(codec, WM8750_PWR1, pwr_reg | 0x0141);
  851. break;
  852. case SNDRV_CTL_POWER_D3cold: /* Off, without power */
  853. wm8750_write(codec, WM8750_PWR1, 0x0001);
  854. break;
  855. }
  856. codec->dapm_state = event;
  857. return 0;
  858. }
  859. struct snd_soc_codec_dai wm8750_dai = {
  860. .name = "WM8750",
  861. .playback = {
  862. .stream_name = "Playback",
  863. .channels_min = 1,
  864. .channels_max = 2,
  865. },
  866. .capture = {
  867. .stream_name = "Capture",
  868. .channels_min = 1,
  869. .channels_max = 2,
  870. },
  871. .config_sysclk = wm8750_config_sysclk,
  872. .digital_mute = wm8750_mute,
  873. .ops = {
  874. .prepare = wm8750_pcm_prepare,
  875. },
  876. .caps = {
  877. .num_modes = ARRAY_SIZE(wm8750_modes),
  878. .mode = wm8750_modes,
  879. },
  880. };
  881. EXPORT_SYMBOL_GPL(wm8750_dai);
  882. static void wm8750_work(void *data)
  883. {
  884. struct snd_soc_codec *codec = (struct snd_soc_codec *)data;
  885. wm8750_dapm_event(codec, codec->dapm_state);
  886. }
  887. static int wm8750_suspend(struct platform_device *pdev, pm_message_t state)
  888. {
  889. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  890. struct snd_soc_codec *codec = socdev->codec;
  891. wm8750_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
  892. return 0;
  893. }
  894. static int wm8750_resume(struct platform_device *pdev)
  895. {
  896. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  897. struct snd_soc_codec *codec = socdev->codec;
  898. int i;
  899. u8 data[2];
  900. u16 *cache = codec->reg_cache;
  901. /* Sync reg_cache with the hardware */
  902. for (i = 0; i < ARRAY_SIZE(wm8750_reg); i++) {
  903. if (i == WM8750_RESET)
  904. continue;
  905. data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
  906. data[1] = cache[i] & 0x00ff;
  907. codec->hw_write(codec->control_data, data, 2);
  908. }
  909. wm8750_dapm_event(codec, SNDRV_CTL_POWER_D3hot);
  910. /* charge wm8750 caps */
  911. if (codec->suspend_dapm_state == SNDRV_CTL_POWER_D0) {
  912. wm8750_dapm_event(codec, SNDRV_CTL_POWER_D2);
  913. codec->dapm_state = SNDRV_CTL_POWER_D0;
  914. queue_delayed_work(wm8750_workq, &wm8750_dapm_work,
  915. msecs_to_jiffies(1000));
  916. }
  917. return 0;
  918. }
  919. /*
  920. * initialise the WM8750 driver
  921. * register the mixer and dsp interfaces with the kernel
  922. */
  923. static int wm8750_init(struct snd_soc_device *socdev)
  924. {
  925. struct snd_soc_codec *codec = socdev->codec;
  926. int reg, ret = 0;
  927. codec->name = "WM8750";
  928. codec->owner = THIS_MODULE;
  929. codec->read = wm8750_read_reg_cache;
  930. codec->write = wm8750_write;
  931. codec->dapm_event = wm8750_dapm_event;
  932. codec->dai = &wm8750_dai;
  933. codec->num_dai = 1;
  934. codec->reg_cache_size = ARRAY_SIZE(wm8750_reg);
  935. codec->reg_cache =
  936. kzalloc(sizeof(u16) * ARRAY_SIZE(wm8750_reg), GFP_KERNEL);
  937. if (codec->reg_cache == NULL)
  938. return -ENOMEM;
  939. memcpy(codec->reg_cache, wm8750_reg,
  940. sizeof(u16) * ARRAY_SIZE(wm8750_reg));
  941. codec->reg_cache_size = sizeof(u16) * ARRAY_SIZE(wm8750_reg);
  942. wm8750_reset(codec);
  943. /* register pcms */
  944. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  945. if (ret < 0) {
  946. kfree(codec->reg_cache);
  947. return ret;
  948. }
  949. /* charge output caps */
  950. wm8750_dapm_event(codec, SNDRV_CTL_POWER_D2);
  951. codec->dapm_state = SNDRV_CTL_POWER_D3hot;
  952. queue_delayed_work(wm8750_workq, &wm8750_dapm_work,
  953. msecs_to_jiffies(1000));
  954. /* set the update bits */
  955. reg = wm8750_read_reg_cache(codec, WM8750_LDAC);
  956. wm8750_write(codec, WM8750_LDAC, reg | 0x0100);
  957. reg = wm8750_read_reg_cache(codec, WM8750_RDAC);
  958. wm8750_write(codec, WM8750_RDAC, reg | 0x0100);
  959. reg = wm8750_read_reg_cache(codec, WM8750_LOUT1V);
  960. wm8750_write(codec, WM8750_LOUT1V, reg | 0x0100);
  961. reg = wm8750_read_reg_cache(codec, WM8750_ROUT1V);
  962. wm8750_write(codec, WM8750_ROUT1V, reg | 0x0100);
  963. reg = wm8750_read_reg_cache(codec, WM8750_LOUT2V);
  964. wm8750_write(codec, WM8750_LOUT2V, reg | 0x0100);
  965. reg = wm8750_read_reg_cache(codec, WM8750_ROUT2V);
  966. wm8750_write(codec, WM8750_ROUT2V, reg | 0x0100);
  967. reg = wm8750_read_reg_cache(codec, WM8750_LINVOL);
  968. wm8750_write(codec, WM8750_LINVOL, reg | 0x0100);
  969. reg = wm8750_read_reg_cache(codec, WM8750_RINVOL);
  970. wm8750_write(codec, WM8750_RINVOL, reg | 0x0100);
  971. wm8750_add_controls(codec);
  972. wm8750_add_widgets(codec);
  973. ret = snd_soc_register_card(socdev);
  974. if (ret < 0) {
  975. snd_soc_free_pcms(socdev);
  976. snd_soc_dapm_free(socdev);
  977. }
  978. return ret;
  979. }
  980. /* If the i2c layer weren't so broken, we could pass this kind of data
  981. around */
  982. static struct snd_soc_device *wm8750_socdev;
  983. #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
  984. /*
  985. * WM8731 2 wire address is determined by GPIO5
  986. * state during powerup.
  987. * low = 0x1a
  988. * high = 0x1b
  989. */
  990. static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END };
  991. /* Magic definition of all other variables and things */
  992. I2C_CLIENT_INSMOD;
  993. static struct i2c_driver wm8750_i2c_driver;
  994. static struct i2c_client client_template;
  995. static int wm8750_codec_probe(struct i2c_adapter *adap, int addr, int kind)
  996. {
  997. struct snd_soc_device *socdev = wm8750_socdev;
  998. struct wm8750_setup_data *setup = socdev->codec_data;
  999. struct snd_soc_codec *codec = socdev->codec;
  1000. struct i2c_client *i2c;
  1001. int ret;
  1002. if (addr != setup->i2c_address)
  1003. return -ENODEV;
  1004. client_template.adapter = adap;
  1005. client_template.addr = addr;
  1006. i2c = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
  1007. if (i2c == NULL) {
  1008. kfree(codec);
  1009. return -ENOMEM;
  1010. }
  1011. memcpy(i2c, &client_template, sizeof(struct i2c_client));
  1012. i2c_set_clientdata(i2c, codec);
  1013. codec->control_data = i2c;
  1014. ret = i2c_attach_client(i2c);
  1015. if (ret < 0) {
  1016. err("failed to attach codec at addr %x\n", addr);
  1017. goto err;
  1018. }
  1019. ret = wm8750_init(socdev);
  1020. if (ret < 0) {
  1021. err("failed to initialise WM8750\n");
  1022. goto err;
  1023. }
  1024. return ret;
  1025. err:
  1026. kfree(codec);
  1027. kfree(i2c);
  1028. return ret;
  1029. }
  1030. static int wm8750_i2c_detach(struct i2c_client *client)
  1031. {
  1032. struct snd_soc_codec *codec = i2c_get_clientdata(client);
  1033. i2c_detach_client(client);
  1034. kfree(codec->reg_cache);
  1035. kfree(client);
  1036. return 0;
  1037. }
  1038. static int wm8750_i2c_attach(struct i2c_adapter *adap)
  1039. {
  1040. return i2c_probe(adap, &addr_data, wm8750_codec_probe);
  1041. }
  1042. /* corgi i2c codec control layer */
  1043. static struct i2c_driver wm8750_i2c_driver = {
  1044. .driver = {
  1045. .name = "WM8750 I2C Codec",
  1046. .owner = THIS_MODULE,
  1047. },
  1048. .id = I2C_DRIVERID_WM8750,
  1049. .attach_adapter = wm8750_i2c_attach,
  1050. .detach_client = wm8750_i2c_detach,
  1051. .command = NULL,
  1052. };
  1053. static struct i2c_client client_template = {
  1054. .name = "WM8750",
  1055. .driver = &wm8750_i2c_driver,
  1056. };
  1057. #endif
  1058. static int wm8750_probe(struct platform_device *pdev)
  1059. {
  1060. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1061. struct wm8750_setup_data *setup = socdev->codec_data;
  1062. struct snd_soc_codec *codec;
  1063. int ret = 0;
  1064. info("WM8750 Audio Codec %s", WM8750_VERSION);
  1065. codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
  1066. if (codec == NULL)
  1067. return -ENOMEM;
  1068. socdev->codec = codec;
  1069. mutex_init(&codec->mutex);
  1070. INIT_LIST_HEAD(&codec->dapm_widgets);
  1071. INIT_LIST_HEAD(&codec->dapm_paths);
  1072. wm8750_socdev = socdev;
  1073. INIT_WORK(&wm8750_dapm_work, wm8750_work, codec);
  1074. wm8750_workq = create_workqueue("wm8750");
  1075. if (wm8750_workq == NULL) {
  1076. kfree(codec);
  1077. return -ENOMEM;
  1078. }
  1079. #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
  1080. if (setup->i2c_address) {
  1081. normal_i2c[0] = setup->i2c_address;
  1082. codec->hw_write = (hw_write_t)i2c_master_send;
  1083. ret = i2c_add_driver(&wm8750_i2c_driver);
  1084. if (ret != 0)
  1085. printk(KERN_ERR "can't add i2c driver");
  1086. }
  1087. #else
  1088. /* Add other interfaces here */
  1089. #endif
  1090. return ret;
  1091. }
  1092. /* power down chip */
  1093. static int wm8750_remove(struct platform_device *pdev)
  1094. {
  1095. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  1096. struct snd_soc_codec *codec = socdev->codec;
  1097. if (codec->control_data)
  1098. wm8750_dapm_event(codec, SNDRV_CTL_POWER_D3cold);
  1099. if (wm8750_workq)
  1100. destroy_workqueue(wm8750_workq);
  1101. snd_soc_free_pcms(socdev);
  1102. snd_soc_dapm_free(socdev);
  1103. #if defined (CONFIG_I2C) || defined (CONFIG_I2C_MODULE)
  1104. i2c_del_driver(&wm8750_i2c_driver);
  1105. #endif
  1106. kfree(codec);
  1107. return 0;
  1108. }
  1109. struct snd_soc_codec_device soc_codec_dev_wm8750 = {
  1110. .probe = wm8750_probe,
  1111. .remove = wm8750_remove,
  1112. .suspend = wm8750_suspend,
  1113. .resume = wm8750_resume,
  1114. };
  1115. EXPORT_SYMBOL_GPL(soc_codec_dev_wm8750);
  1116. MODULE_DESCRIPTION("ASoC WM8750 driver");
  1117. MODULE_AUTHOR("Liam Girdwood");
  1118. MODULE_LICENSE("GPL");