twl6040.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. /*
  2. * ALSA SoC TWL6040 codec driver
  3. *
  4. * Author: Misael Lopez Cruz <x0052729@ti.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/pm.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/i2c/twl.h>
  29. #include <linux/mfd/twl6040.h>
  30. #include <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include <sound/soc.h>
  34. #include <sound/initval.h>
  35. #include <sound/tlv.h>
  36. #include "twl6040.h"
  37. #define TWL6040_RATES SNDRV_PCM_RATE_8000_96000
  38. #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
  39. #define TWL6040_OUTHS_0dB 0x00
  40. #define TWL6040_OUTHS_M30dB 0x0F
  41. #define TWL6040_OUTHF_0dB 0x03
  42. #define TWL6040_OUTHF_M52dB 0x1D
  43. #define TWL6040_RAMP_NONE 0
  44. #define TWL6040_RAMP_UP 1
  45. #define TWL6040_RAMP_DOWN 2
  46. #define TWL6040_HSL_VOL_MASK 0x0F
  47. #define TWL6040_HSL_VOL_SHIFT 0
  48. #define TWL6040_HSR_VOL_MASK 0xF0
  49. #define TWL6040_HSR_VOL_SHIFT 4
  50. #define TWL6040_HF_VOL_MASK 0x1F
  51. #define TWL6040_HF_VOL_SHIFT 0
  52. /* Shadow register used by the driver */
  53. #define TWL6040_REG_SW_SHADOW 0x2F
  54. #define TWL6040_CACHEREGNUM (TWL6040_REG_SW_SHADOW + 1)
  55. /* TWL6040_REG_SW_SHADOW (0x2F) fields */
  56. #define TWL6040_EAR_PATH_ENABLE 0x01
  57. struct twl6040_output {
  58. u16 active;
  59. u16 left_vol;
  60. u16 right_vol;
  61. u16 left_step;
  62. u16 right_step;
  63. unsigned int step_delay;
  64. u16 ramp;
  65. u16 mute;
  66. struct completion ramp_done;
  67. };
  68. struct twl6040_jack_data {
  69. struct snd_soc_jack *jack;
  70. int report;
  71. };
  72. /* codec private data */
  73. struct twl6040_data {
  74. int plug_irq;
  75. int codec_powered;
  76. int pll;
  77. int non_lp;
  78. int pll_power_mode;
  79. int hs_power_mode;
  80. int hs_power_mode_locked;
  81. unsigned int clk_in;
  82. unsigned int sysclk;
  83. u16 hs_left_step;
  84. u16 hs_right_step;
  85. u16 hf_left_step;
  86. u16 hf_right_step;
  87. struct twl6040_jack_data hs_jack;
  88. struct snd_soc_codec *codec;
  89. struct workqueue_struct *workqueue;
  90. struct delayed_work delayed_work;
  91. struct mutex mutex;
  92. struct twl6040_output headset;
  93. struct twl6040_output handsfree;
  94. struct workqueue_struct *hf_workqueue;
  95. struct workqueue_struct *hs_workqueue;
  96. struct delayed_work hs_delayed_work;
  97. struct delayed_work hf_delayed_work;
  98. };
  99. /*
  100. * twl6040 register cache & default register settings
  101. */
  102. static const u8 twl6040_reg[TWL6040_CACHEREGNUM] = {
  103. 0x00, /* not used 0x00 */
  104. 0x4B, /* REG_ASICID 0x01 (ro) */
  105. 0x00, /* REG_ASICREV 0x02 (ro) */
  106. 0x00, /* REG_INTID 0x03 */
  107. 0x00, /* REG_INTMR 0x04 */
  108. 0x00, /* REG_NCPCTRL 0x05 */
  109. 0x00, /* REG_LDOCTL 0x06 */
  110. 0x60, /* REG_HPPLLCTL 0x07 */
  111. 0x00, /* REG_LPPLLCTL 0x08 */
  112. 0x4A, /* REG_LPPLLDIV 0x09 */
  113. 0x00, /* REG_AMICBCTL 0x0A */
  114. 0x00, /* REG_DMICBCTL 0x0B */
  115. 0x00, /* REG_MICLCTL 0x0C */
  116. 0x00, /* REG_MICRCTL 0x0D */
  117. 0x00, /* REG_MICGAIN 0x0E */
  118. 0x1B, /* REG_LINEGAIN 0x0F */
  119. 0x00, /* REG_HSLCTL 0x10 */
  120. 0x00, /* REG_HSRCTL 0x11 */
  121. 0x00, /* REG_HSGAIN 0x12 */
  122. 0x00, /* REG_EARCTL 0x13 */
  123. 0x00, /* REG_HFLCTL 0x14 */
  124. 0x00, /* REG_HFLGAIN 0x15 */
  125. 0x00, /* REG_HFRCTL 0x16 */
  126. 0x00, /* REG_HFRGAIN 0x17 */
  127. 0x00, /* REG_VIBCTLL 0x18 */
  128. 0x00, /* REG_VIBDATL 0x19 */
  129. 0x00, /* REG_VIBCTLR 0x1A */
  130. 0x00, /* REG_VIBDATR 0x1B */
  131. 0x00, /* REG_HKCTL1 0x1C */
  132. 0x00, /* REG_HKCTL2 0x1D */
  133. 0x00, /* REG_GPOCTL 0x1E */
  134. 0x00, /* REG_ALB 0x1F */
  135. 0x00, /* REG_DLB 0x20 */
  136. 0x00, /* not used 0x21 */
  137. 0x00, /* not used 0x22 */
  138. 0x00, /* not used 0x23 */
  139. 0x00, /* not used 0x24 */
  140. 0x00, /* not used 0x25 */
  141. 0x00, /* not used 0x26 */
  142. 0x00, /* not used 0x27 */
  143. 0x00, /* REG_TRIM1 0x28 */
  144. 0x00, /* REG_TRIM2 0x29 */
  145. 0x00, /* REG_TRIM3 0x2A */
  146. 0x00, /* REG_HSOTRIM 0x2B */
  147. 0x00, /* REG_HFOTRIM 0x2C */
  148. 0x09, /* REG_ACCCTL 0x2D */
  149. 0x00, /* REG_STATUS 0x2E (ro) */
  150. 0x00, /* REG_SW_SHADOW 0x2F - Shadow, non HW register */
  151. };
  152. /* List of registers to be restored after power up */
  153. static const int twl6040_restore_list[] = {
  154. TWL6040_REG_MICLCTL,
  155. TWL6040_REG_MICRCTL,
  156. TWL6040_REG_MICGAIN,
  157. TWL6040_REG_LINEGAIN,
  158. TWL6040_REG_HSLCTL,
  159. TWL6040_REG_HSRCTL,
  160. TWL6040_REG_HSGAIN,
  161. TWL6040_REG_EARCTL,
  162. TWL6040_REG_HFLCTL,
  163. TWL6040_REG_HFLGAIN,
  164. TWL6040_REG_HFRCTL,
  165. TWL6040_REG_HFRGAIN,
  166. };
  167. /* set of rates for each pll: low-power and high-performance */
  168. static unsigned int lp_rates[] = {
  169. 8000,
  170. 11250,
  171. 16000,
  172. 22500,
  173. 32000,
  174. 44100,
  175. 48000,
  176. 88200,
  177. 96000,
  178. };
  179. static unsigned int hp_rates[] = {
  180. 8000,
  181. 16000,
  182. 32000,
  183. 48000,
  184. 96000,
  185. };
  186. static struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
  187. { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
  188. { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
  189. };
  190. /*
  191. * read twl6040 register cache
  192. */
  193. static inline unsigned int twl6040_read_reg_cache(struct snd_soc_codec *codec,
  194. unsigned int reg)
  195. {
  196. u8 *cache = codec->reg_cache;
  197. if (reg >= TWL6040_CACHEREGNUM)
  198. return -EIO;
  199. return cache[reg];
  200. }
  201. /*
  202. * write twl6040 register cache
  203. */
  204. static inline void twl6040_write_reg_cache(struct snd_soc_codec *codec,
  205. u8 reg, u8 value)
  206. {
  207. u8 *cache = codec->reg_cache;
  208. if (reg >= TWL6040_CACHEREGNUM)
  209. return;
  210. cache[reg] = value;
  211. }
  212. /*
  213. * read from twl6040 hardware register
  214. */
  215. static int twl6040_read_reg_volatile(struct snd_soc_codec *codec,
  216. unsigned int reg)
  217. {
  218. struct twl6040 *twl6040 = codec->control_data;
  219. u8 value;
  220. if (reg >= TWL6040_CACHEREGNUM)
  221. return -EIO;
  222. if (likely(reg < TWL6040_REG_SW_SHADOW)) {
  223. value = twl6040_reg_read(twl6040, reg);
  224. twl6040_write_reg_cache(codec, reg, value);
  225. } else {
  226. value = twl6040_read_reg_cache(codec, reg);
  227. }
  228. return value;
  229. }
  230. /*
  231. * write to the twl6040 register space
  232. */
  233. static int twl6040_write(struct snd_soc_codec *codec,
  234. unsigned int reg, unsigned int value)
  235. {
  236. struct twl6040 *twl6040 = codec->control_data;
  237. if (reg >= TWL6040_CACHEREGNUM)
  238. return -EIO;
  239. twl6040_write_reg_cache(codec, reg, value);
  240. if (likely(reg < TWL6040_REG_SW_SHADOW))
  241. return twl6040_reg_write(twl6040, reg, value);
  242. else
  243. return 0;
  244. }
  245. static void twl6040_init_chip(struct snd_soc_codec *codec)
  246. {
  247. struct twl6040 *twl6040 = codec->control_data;
  248. u8 val;
  249. val = twl6040_get_revid(twl6040);
  250. twl6040_write_reg_cache(codec, TWL6040_REG_ASICREV, val);
  251. /* Change chip defaults */
  252. /* No imput selected for microphone amplifiers */
  253. twl6040_write_reg_cache(codec, TWL6040_REG_MICLCTL, 0x18);
  254. twl6040_write_reg_cache(codec, TWL6040_REG_MICRCTL, 0x18);
  255. /*
  256. * We need to lower the default gain values, so the ramp code
  257. * can work correctly for the first playback.
  258. * This reduces the pop noise heard at the first playback.
  259. */
  260. twl6040_write_reg_cache(codec, TWL6040_REG_HSGAIN, 0xff);
  261. twl6040_write_reg_cache(codec, TWL6040_REG_EARCTL, 0x1e);
  262. twl6040_write_reg_cache(codec, TWL6040_REG_HFLGAIN, 0x1d);
  263. twl6040_write_reg_cache(codec, TWL6040_REG_HFRGAIN, 0x1d);
  264. twl6040_write_reg_cache(codec, TWL6040_REG_LINEGAIN, 0);
  265. }
  266. static void twl6040_restore_regs(struct snd_soc_codec *codec)
  267. {
  268. u8 *cache = codec->reg_cache;
  269. int reg, i;
  270. for (i = 0; i < ARRAY_SIZE(twl6040_restore_list); i++) {
  271. reg = twl6040_restore_list[i];
  272. twl6040_write(codec, reg, cache[reg]);
  273. }
  274. }
  275. /*
  276. * Ramp HS PGA volume to minimise pops at stream startup and shutdown.
  277. */
  278. static inline int twl6040_hs_ramp_step(struct snd_soc_codec *codec,
  279. unsigned int left_step, unsigned int right_step)
  280. {
  281. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  282. struct twl6040_output *headset = &priv->headset;
  283. int left_complete = 0, right_complete = 0;
  284. u8 reg, val;
  285. /* left channel */
  286. left_step = (left_step > 0xF) ? 0xF : left_step;
  287. reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
  288. val = (~reg & TWL6040_HSL_VOL_MASK);
  289. if (headset->ramp == TWL6040_RAMP_UP) {
  290. /* ramp step up */
  291. if (val < headset->left_vol) {
  292. if (val + left_step > headset->left_vol)
  293. val = headset->left_vol;
  294. else
  295. val += left_step;
  296. reg &= ~TWL6040_HSL_VOL_MASK;
  297. twl6040_write(codec, TWL6040_REG_HSGAIN,
  298. (reg | (~val & TWL6040_HSL_VOL_MASK)));
  299. } else {
  300. left_complete = 1;
  301. }
  302. } else if (headset->ramp == TWL6040_RAMP_DOWN) {
  303. /* ramp step down */
  304. if (val > 0x0) {
  305. if ((int)val - (int)left_step < 0)
  306. val = 0;
  307. else
  308. val -= left_step;
  309. reg &= ~TWL6040_HSL_VOL_MASK;
  310. twl6040_write(codec, TWL6040_REG_HSGAIN, reg |
  311. (~val & TWL6040_HSL_VOL_MASK));
  312. } else {
  313. left_complete = 1;
  314. }
  315. }
  316. /* right channel */
  317. right_step = (right_step > 0xF) ? 0xF : right_step;
  318. reg = twl6040_read_reg_cache(codec, TWL6040_REG_HSGAIN);
  319. val = (~reg & TWL6040_HSR_VOL_MASK) >> TWL6040_HSR_VOL_SHIFT;
  320. if (headset->ramp == TWL6040_RAMP_UP) {
  321. /* ramp step up */
  322. if (val < headset->right_vol) {
  323. if (val + right_step > headset->right_vol)
  324. val = headset->right_vol;
  325. else
  326. val += right_step;
  327. reg &= ~TWL6040_HSR_VOL_MASK;
  328. twl6040_write(codec, TWL6040_REG_HSGAIN,
  329. (reg | (~val << TWL6040_HSR_VOL_SHIFT)));
  330. } else {
  331. right_complete = 1;
  332. }
  333. } else if (headset->ramp == TWL6040_RAMP_DOWN) {
  334. /* ramp step down */
  335. if (val > 0x0) {
  336. if ((int)val - (int)right_step < 0)
  337. val = 0;
  338. else
  339. val -= right_step;
  340. reg &= ~TWL6040_HSR_VOL_MASK;
  341. twl6040_write(codec, TWL6040_REG_HSGAIN,
  342. reg | (~val << TWL6040_HSR_VOL_SHIFT));
  343. } else {
  344. right_complete = 1;
  345. }
  346. }
  347. return left_complete & right_complete;
  348. }
  349. /*
  350. * Ramp HF PGA volume to minimise pops at stream startup and shutdown.
  351. */
  352. static inline int twl6040_hf_ramp_step(struct snd_soc_codec *codec,
  353. unsigned int left_step, unsigned int right_step)
  354. {
  355. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  356. struct twl6040_output *handsfree = &priv->handsfree;
  357. int left_complete = 0, right_complete = 0;
  358. u16 reg, val;
  359. /* left channel */
  360. left_step = (left_step > 0x1D) ? 0x1D : left_step;
  361. reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFLGAIN);
  362. reg = 0x1D - reg;
  363. val = (reg & TWL6040_HF_VOL_MASK);
  364. if (handsfree->ramp == TWL6040_RAMP_UP) {
  365. /* ramp step up */
  366. if (val < handsfree->left_vol) {
  367. if (val + left_step > handsfree->left_vol)
  368. val = handsfree->left_vol;
  369. else
  370. val += left_step;
  371. reg &= ~TWL6040_HF_VOL_MASK;
  372. twl6040_write(codec, TWL6040_REG_HFLGAIN,
  373. reg | (0x1D - val));
  374. } else {
  375. left_complete = 1;
  376. }
  377. } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
  378. /* ramp step down */
  379. if (val > 0) {
  380. if ((int)val - (int)left_step < 0)
  381. val = 0;
  382. else
  383. val -= left_step;
  384. reg &= ~TWL6040_HF_VOL_MASK;
  385. twl6040_write(codec, TWL6040_REG_HFLGAIN,
  386. reg | (0x1D - val));
  387. } else {
  388. left_complete = 1;
  389. }
  390. }
  391. /* right channel */
  392. right_step = (right_step > 0x1D) ? 0x1D : right_step;
  393. reg = twl6040_read_reg_cache(codec, TWL6040_REG_HFRGAIN);
  394. reg = 0x1D - reg;
  395. val = (reg & TWL6040_HF_VOL_MASK);
  396. if (handsfree->ramp == TWL6040_RAMP_UP) {
  397. /* ramp step up */
  398. if (val < handsfree->right_vol) {
  399. if (val + right_step > handsfree->right_vol)
  400. val = handsfree->right_vol;
  401. else
  402. val += right_step;
  403. reg &= ~TWL6040_HF_VOL_MASK;
  404. twl6040_write(codec, TWL6040_REG_HFRGAIN,
  405. reg | (0x1D - val));
  406. } else {
  407. right_complete = 1;
  408. }
  409. } else if (handsfree->ramp == TWL6040_RAMP_DOWN) {
  410. /* ramp step down */
  411. if (val > 0) {
  412. if ((int)val - (int)right_step < 0)
  413. val = 0;
  414. else
  415. val -= right_step;
  416. reg &= ~TWL6040_HF_VOL_MASK;
  417. twl6040_write(codec, TWL6040_REG_HFRGAIN,
  418. reg | (0x1D - val));
  419. }
  420. }
  421. return left_complete & right_complete;
  422. }
  423. /*
  424. * This work ramps both output PGAs at stream start/stop time to
  425. * minimise pop associated with DAPM power switching.
  426. */
  427. static void twl6040_pga_hs_work(struct work_struct *work)
  428. {
  429. struct twl6040_data *priv =
  430. container_of(work, struct twl6040_data, hs_delayed_work.work);
  431. struct snd_soc_codec *codec = priv->codec;
  432. struct twl6040_output *headset = &priv->headset;
  433. unsigned int delay = headset->step_delay;
  434. int i, headset_complete;
  435. /* do we need to ramp at all ? */
  436. if (headset->ramp == TWL6040_RAMP_NONE)
  437. return;
  438. /* HS PGA volumes have 4 bits of resolution to ramp */
  439. for (i = 0; i <= 16; i++) {
  440. headset_complete = twl6040_hs_ramp_step(codec,
  441. headset->left_step,
  442. headset->right_step);
  443. /* ramp finished ? */
  444. if (headset_complete)
  445. break;
  446. /*
  447. * TODO: tune: delay is longer over 0dB
  448. * as increases are larger.
  449. */
  450. if (i >= 8)
  451. schedule_timeout_interruptible(msecs_to_jiffies(delay +
  452. (delay >> 1)));
  453. else
  454. schedule_timeout_interruptible(msecs_to_jiffies(delay));
  455. }
  456. if (headset->ramp == TWL6040_RAMP_DOWN) {
  457. headset->active = 0;
  458. complete(&headset->ramp_done);
  459. } else {
  460. headset->active = 1;
  461. }
  462. headset->ramp = TWL6040_RAMP_NONE;
  463. }
  464. static void twl6040_pga_hf_work(struct work_struct *work)
  465. {
  466. struct twl6040_data *priv =
  467. container_of(work, struct twl6040_data, hf_delayed_work.work);
  468. struct snd_soc_codec *codec = priv->codec;
  469. struct twl6040_output *handsfree = &priv->handsfree;
  470. unsigned int delay = handsfree->step_delay;
  471. int i, handsfree_complete;
  472. /* do we need to ramp at all ? */
  473. if (handsfree->ramp == TWL6040_RAMP_NONE)
  474. return;
  475. /* HF PGA volumes have 5 bits of resolution to ramp */
  476. for (i = 0; i <= 32; i++) {
  477. handsfree_complete = twl6040_hf_ramp_step(codec,
  478. handsfree->left_step,
  479. handsfree->right_step);
  480. /* ramp finished ? */
  481. if (handsfree_complete)
  482. break;
  483. /*
  484. * TODO: tune: delay is longer over 0dB
  485. * as increases are larger.
  486. */
  487. if (i >= 16)
  488. schedule_timeout_interruptible(msecs_to_jiffies(delay +
  489. (delay >> 1)));
  490. else
  491. schedule_timeout_interruptible(msecs_to_jiffies(delay));
  492. }
  493. if (handsfree->ramp == TWL6040_RAMP_DOWN) {
  494. handsfree->active = 0;
  495. complete(&handsfree->ramp_done);
  496. } else
  497. handsfree->active = 1;
  498. handsfree->ramp = TWL6040_RAMP_NONE;
  499. }
  500. static int pga_event(struct snd_soc_dapm_widget *w,
  501. struct snd_kcontrol *kcontrol, int event)
  502. {
  503. struct snd_soc_codec *codec = w->codec;
  504. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  505. struct twl6040_output *out;
  506. struct delayed_work *work;
  507. struct workqueue_struct *queue;
  508. switch (w->shift) {
  509. case 2:
  510. case 3:
  511. out = &priv->headset;
  512. work = &priv->hs_delayed_work;
  513. queue = priv->hs_workqueue;
  514. out->left_step = priv->hs_left_step;
  515. out->right_step = priv->hs_right_step;
  516. out->step_delay = 5; /* 5 ms between volume ramp steps */
  517. break;
  518. case 4:
  519. out = &priv->handsfree;
  520. work = &priv->hf_delayed_work;
  521. queue = priv->hf_workqueue;
  522. out->left_step = priv->hf_left_step;
  523. out->right_step = priv->hf_right_step;
  524. out->step_delay = 5; /* 5 ms between volume ramp steps */
  525. if (SND_SOC_DAPM_EVENT_ON(event))
  526. priv->non_lp++;
  527. else
  528. priv->non_lp--;
  529. break;
  530. default:
  531. return -1;
  532. }
  533. switch (event) {
  534. case SND_SOC_DAPM_POST_PMU:
  535. if (out->active)
  536. break;
  537. /* don't use volume ramp for power-up */
  538. out->left_step = out->left_vol;
  539. out->right_step = out->right_vol;
  540. if (!delayed_work_pending(work)) {
  541. out->ramp = TWL6040_RAMP_UP;
  542. queue_delayed_work(queue, work,
  543. msecs_to_jiffies(1));
  544. }
  545. break;
  546. case SND_SOC_DAPM_PRE_PMD:
  547. if (!out->active)
  548. break;
  549. if (!delayed_work_pending(work)) {
  550. /* use volume ramp for power-down */
  551. out->ramp = TWL6040_RAMP_DOWN;
  552. INIT_COMPLETION(out->ramp_done);
  553. queue_delayed_work(queue, work,
  554. msecs_to_jiffies(1));
  555. wait_for_completion_timeout(&out->ramp_done,
  556. msecs_to_jiffies(2000));
  557. }
  558. break;
  559. }
  560. return 0;
  561. }
  562. /* set headset dac and driver power mode */
  563. static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
  564. {
  565. int hslctl, hsrctl;
  566. int mask = TWL6040_HSDRVMODEL | TWL6040_HSDACMODEL;
  567. hslctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSLCTL);
  568. hsrctl = twl6040_read_reg_cache(codec, TWL6040_REG_HSRCTL);
  569. if (high_perf) {
  570. hslctl &= ~mask;
  571. hsrctl &= ~mask;
  572. } else {
  573. hslctl |= mask;
  574. hsrctl |= mask;
  575. }
  576. twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
  577. twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
  578. return 0;
  579. }
  580. static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
  581. struct snd_kcontrol *kcontrol, int event)
  582. {
  583. msleep(1);
  584. return 0;
  585. }
  586. static int twl6040_power_mode_event(struct snd_soc_dapm_widget *w,
  587. struct snd_kcontrol *kcontrol, int event)
  588. {
  589. struct snd_soc_codec *codec = w->codec;
  590. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  591. int ret = 0;
  592. if (SND_SOC_DAPM_EVENT_ON(event)) {
  593. priv->non_lp++;
  594. if (!strcmp(w->name, "Earphone Driver")) {
  595. /* Earphone doesn't support low power mode */
  596. priv->hs_power_mode_locked = 1;
  597. ret = headset_power_mode(codec, 1);
  598. }
  599. } else {
  600. priv->non_lp--;
  601. if (!strcmp(w->name, "Earphone Driver")) {
  602. priv->hs_power_mode_locked = 0;
  603. ret = headset_power_mode(codec, priv->hs_power_mode);
  604. }
  605. }
  606. msleep(1);
  607. return ret;
  608. }
  609. static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
  610. struct snd_soc_jack *jack, int report)
  611. {
  612. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  613. int status;
  614. mutex_lock(&priv->mutex);
  615. /* Sync status */
  616. status = twl6040_read_reg_volatile(codec, TWL6040_REG_STATUS);
  617. if (status & TWL6040_PLUGCOMP)
  618. snd_soc_jack_report(jack, report, report);
  619. else
  620. snd_soc_jack_report(jack, 0, report);
  621. mutex_unlock(&priv->mutex);
  622. }
  623. void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
  624. struct snd_soc_jack *jack, int report)
  625. {
  626. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  627. struct twl6040_jack_data *hs_jack = &priv->hs_jack;
  628. hs_jack->jack = jack;
  629. hs_jack->report = report;
  630. twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
  631. }
  632. EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
  633. static void twl6040_accessory_work(struct work_struct *work)
  634. {
  635. struct twl6040_data *priv = container_of(work,
  636. struct twl6040_data, delayed_work.work);
  637. struct snd_soc_codec *codec = priv->codec;
  638. struct twl6040_jack_data *hs_jack = &priv->hs_jack;
  639. twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
  640. }
  641. /* audio interrupt handler */
  642. static irqreturn_t twl6040_audio_handler(int irq, void *data)
  643. {
  644. struct snd_soc_codec *codec = data;
  645. struct twl6040 *twl6040 = codec->control_data;
  646. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  647. u8 intid;
  648. intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
  649. if ((intid & TWL6040_PLUGINT) || (intid & TWL6040_UNPLUGINT))
  650. queue_delayed_work(priv->workqueue, &priv->delayed_work,
  651. msecs_to_jiffies(200));
  652. return IRQ_HANDLED;
  653. }
  654. static int twl6040_put_volsw(struct snd_kcontrol *kcontrol,
  655. struct snd_ctl_elem_value *ucontrol)
  656. {
  657. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  658. struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
  659. struct twl6040_output *out = NULL;
  660. struct soc_mixer_control *mc =
  661. (struct soc_mixer_control *)kcontrol->private_value;
  662. int ret;
  663. unsigned int reg = mc->reg;
  664. /* For HS and HF we shadow the values and only actually write
  665. * them out when active in order to ensure the amplifier comes on
  666. * as quietly as possible. */
  667. switch (reg) {
  668. case TWL6040_REG_HSGAIN:
  669. out = &twl6040_priv->headset;
  670. break;
  671. default:
  672. break;
  673. }
  674. if (out) {
  675. out->left_vol = ucontrol->value.integer.value[0];
  676. out->right_vol = ucontrol->value.integer.value[1];
  677. if (!out->active)
  678. return 1;
  679. }
  680. ret = snd_soc_put_volsw(kcontrol, ucontrol);
  681. if (ret < 0)
  682. return ret;
  683. return 1;
  684. }
  685. static int twl6040_get_volsw(struct snd_kcontrol *kcontrol,
  686. struct snd_ctl_elem_value *ucontrol)
  687. {
  688. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  689. struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
  690. struct twl6040_output *out = &twl6040_priv->headset;
  691. struct soc_mixer_control *mc =
  692. (struct soc_mixer_control *)kcontrol->private_value;
  693. unsigned int reg = mc->reg;
  694. switch (reg) {
  695. case TWL6040_REG_HSGAIN:
  696. out = &twl6040_priv->headset;
  697. ucontrol->value.integer.value[0] = out->left_vol;
  698. ucontrol->value.integer.value[1] = out->right_vol;
  699. return 0;
  700. default:
  701. break;
  702. }
  703. return snd_soc_get_volsw(kcontrol, ucontrol);
  704. }
  705. static int twl6040_put_volsw_2r_vu(struct snd_kcontrol *kcontrol,
  706. struct snd_ctl_elem_value *ucontrol)
  707. {
  708. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  709. struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
  710. struct twl6040_output *out = NULL;
  711. struct soc_mixer_control *mc =
  712. (struct soc_mixer_control *)kcontrol->private_value;
  713. int ret;
  714. unsigned int reg = mc->reg;
  715. /* For HS and HF we shadow the values and only actually write
  716. * them out when active in order to ensure the amplifier comes on
  717. * as quietly as possible. */
  718. switch (reg) {
  719. case TWL6040_REG_HFLGAIN:
  720. case TWL6040_REG_HFRGAIN:
  721. out = &twl6040_priv->handsfree;
  722. break;
  723. default:
  724. break;
  725. }
  726. if (out) {
  727. out->left_vol = ucontrol->value.integer.value[0];
  728. out->right_vol = ucontrol->value.integer.value[1];
  729. if (!out->active)
  730. return 1;
  731. }
  732. ret = snd_soc_put_volsw_2r(kcontrol, ucontrol);
  733. if (ret < 0)
  734. return ret;
  735. return 1;
  736. }
  737. static int twl6040_get_volsw_2r(struct snd_kcontrol *kcontrol,
  738. struct snd_ctl_elem_value *ucontrol)
  739. {
  740. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  741. struct twl6040_data *twl6040_priv = snd_soc_codec_get_drvdata(codec);
  742. struct twl6040_output *out = &twl6040_priv->handsfree;
  743. struct soc_mixer_control *mc =
  744. (struct soc_mixer_control *)kcontrol->private_value;
  745. unsigned int reg = mc->reg;
  746. /* If these are cached registers use the cache */
  747. switch (reg) {
  748. case TWL6040_REG_HFLGAIN:
  749. case TWL6040_REG_HFRGAIN:
  750. out = &twl6040_priv->handsfree;
  751. ucontrol->value.integer.value[0] = out->left_vol;
  752. ucontrol->value.integer.value[1] = out->right_vol;
  753. return 0;
  754. default:
  755. break;
  756. }
  757. return snd_soc_get_volsw_2r(kcontrol, ucontrol);
  758. }
  759. /* double control with volume update */
  760. #define SOC_TWL6040_DOUBLE_TLV(xname, xreg, shift_left, shift_right, xmax,\
  761. xinvert, tlv_array)\
  762. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
  763. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
  764. SNDRV_CTL_ELEM_ACCESS_READWRITE,\
  765. .tlv.p = (tlv_array), \
  766. .info = snd_soc_info_volsw, .get = twl6040_get_volsw, \
  767. .put = twl6040_put_volsw, \
  768. .private_value = (unsigned long)&(struct soc_mixer_control) \
  769. {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
  770. .max = xmax, .platform_max = xmax, .invert = xinvert} }
  771. /* double control with volume update */
  772. #define SOC_TWL6040_DOUBLE_R_TLV(xname, reg_left, reg_right, xshift, xmax,\
  773. xinvert, tlv_array)\
  774. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
  775. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  776. SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  777. SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  778. .tlv.p = (tlv_array), \
  779. .info = snd_soc_info_volsw_2r, \
  780. .get = twl6040_get_volsw_2r, .put = twl6040_put_volsw_2r_vu, \
  781. .private_value = (unsigned long)&(struct soc_mixer_control) \
  782. {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
  783. .rshift = xshift, .max = xmax, .invert = xinvert}, }
  784. /*
  785. * MICATT volume control:
  786. * from -6 to 0 dB in 6 dB steps
  787. */
  788. static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
  789. /*
  790. * MICGAIN volume control:
  791. * from 6 to 30 dB in 6 dB steps
  792. */
  793. static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
  794. /*
  795. * AFMGAIN volume control:
  796. * from -18 to 24 dB in 6 dB steps
  797. */
  798. static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
  799. /*
  800. * HSGAIN volume control:
  801. * from -30 to 0 dB in 2 dB steps
  802. */
  803. static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
  804. /*
  805. * HFGAIN volume control:
  806. * from -52 to 6 dB in 2 dB steps
  807. */
  808. static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
  809. /*
  810. * EPGAIN volume control:
  811. * from -24 to 6 dB in 2 dB steps
  812. */
  813. static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
  814. /* Left analog microphone selection */
  815. static const char *twl6040_amicl_texts[] =
  816. {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
  817. /* Right analog microphone selection */
  818. static const char *twl6040_amicr_texts[] =
  819. {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
  820. static const struct soc_enum twl6040_enum[] = {
  821. SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3, 4, twl6040_amicl_texts),
  822. SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3, 4, twl6040_amicr_texts),
  823. };
  824. static const char *twl6040_hs_texts[] = {
  825. "Off", "HS DAC", "Line-In amp"
  826. };
  827. static const struct soc_enum twl6040_hs_enum[] = {
  828. SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
  829. twl6040_hs_texts),
  830. SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
  831. twl6040_hs_texts),
  832. };
  833. static const char *twl6040_hf_texts[] = {
  834. "Off", "HF DAC", "Line-In amp"
  835. };
  836. static const struct soc_enum twl6040_hf_enum[] = {
  837. SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
  838. twl6040_hf_texts),
  839. SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
  840. twl6040_hf_texts),
  841. };
  842. static const struct snd_kcontrol_new amicl_control =
  843. SOC_DAPM_ENUM("Route", twl6040_enum[0]);
  844. static const struct snd_kcontrol_new amicr_control =
  845. SOC_DAPM_ENUM("Route", twl6040_enum[1]);
  846. /* Headset DAC playback switches */
  847. static const struct snd_kcontrol_new hsl_mux_controls =
  848. SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
  849. static const struct snd_kcontrol_new hsr_mux_controls =
  850. SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
  851. /* Handsfree DAC playback switches */
  852. static const struct snd_kcontrol_new hfl_mux_controls =
  853. SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
  854. static const struct snd_kcontrol_new hfr_mux_controls =
  855. SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
  856. static const struct snd_kcontrol_new ep_path_enable_control =
  857. SOC_DAPM_SINGLE("Switch", TWL6040_REG_SW_SHADOW, 0, 1, 0);
  858. /* Headset power mode */
  859. static const char *twl6040_power_mode_texts[] = {
  860. "Low-Power", "High-Perfomance",
  861. };
  862. static const struct soc_enum twl6040_power_mode_enum =
  863. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl6040_power_mode_texts),
  864. twl6040_power_mode_texts);
  865. static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
  866. struct snd_ctl_elem_value *ucontrol)
  867. {
  868. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  869. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  870. ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
  871. return 0;
  872. }
  873. static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
  874. struct snd_ctl_elem_value *ucontrol)
  875. {
  876. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  877. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  878. int high_perf = ucontrol->value.enumerated.item[0];
  879. int ret = 0;
  880. if (!priv->hs_power_mode_locked)
  881. ret = headset_power_mode(codec, high_perf);
  882. if (!ret)
  883. priv->hs_power_mode = high_perf;
  884. return ret;
  885. }
  886. static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
  887. struct snd_ctl_elem_value *ucontrol)
  888. {
  889. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  890. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  891. ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
  892. return 0;
  893. }
  894. static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
  895. struct snd_ctl_elem_value *ucontrol)
  896. {
  897. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  898. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  899. priv->pll_power_mode = ucontrol->value.enumerated.item[0];
  900. return 0;
  901. }
  902. int twl6040_get_clk_id(struct snd_soc_codec *codec)
  903. {
  904. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  905. return priv->pll_power_mode;
  906. }
  907. EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
  908. static const struct snd_kcontrol_new twl6040_snd_controls[] = {
  909. /* Capture gains */
  910. SOC_DOUBLE_TLV("Capture Preamplifier Volume",
  911. TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
  912. SOC_DOUBLE_TLV("Capture Volume",
  913. TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
  914. /* AFM gains */
  915. SOC_DOUBLE_TLV("Aux FM Volume",
  916. TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
  917. /* Playback gains */
  918. SOC_TWL6040_DOUBLE_TLV("Headset Playback Volume",
  919. TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
  920. SOC_TWL6040_DOUBLE_R_TLV("Handsfree Playback Volume",
  921. TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
  922. SOC_SINGLE_TLV("Earphone Playback Volume",
  923. TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
  924. SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
  925. twl6040_headset_power_get_enum,
  926. twl6040_headset_power_put_enum),
  927. SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
  928. twl6040_pll_get_enum, twl6040_pll_put_enum),
  929. };
  930. static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
  931. /* Inputs */
  932. SND_SOC_DAPM_INPUT("MAINMIC"),
  933. SND_SOC_DAPM_INPUT("HSMIC"),
  934. SND_SOC_DAPM_INPUT("SUBMIC"),
  935. SND_SOC_DAPM_INPUT("AFML"),
  936. SND_SOC_DAPM_INPUT("AFMR"),
  937. /* Outputs */
  938. SND_SOC_DAPM_OUTPUT("HSOL"),
  939. SND_SOC_DAPM_OUTPUT("HSOR"),
  940. SND_SOC_DAPM_OUTPUT("HFL"),
  941. SND_SOC_DAPM_OUTPUT("HFR"),
  942. SND_SOC_DAPM_OUTPUT("EP"),
  943. /* Analog input muxes for the capture amplifiers */
  944. SND_SOC_DAPM_MUX("Analog Left Capture Route",
  945. SND_SOC_NOPM, 0, 0, &amicl_control),
  946. SND_SOC_DAPM_MUX("Analog Right Capture Route",
  947. SND_SOC_NOPM, 0, 0, &amicr_control),
  948. /* Analog capture PGAs */
  949. SND_SOC_DAPM_PGA("MicAmpL",
  950. TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
  951. SND_SOC_DAPM_PGA("MicAmpR",
  952. TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
  953. /* Auxiliary FM PGAs */
  954. SND_SOC_DAPM_PGA("AFMAmpL",
  955. TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
  956. SND_SOC_DAPM_PGA("AFMAmpR",
  957. TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
  958. /* ADCs */
  959. SND_SOC_DAPM_ADC("ADC Left", "Left Front Capture",
  960. TWL6040_REG_MICLCTL, 2, 0),
  961. SND_SOC_DAPM_ADC("ADC Right", "Right Front Capture",
  962. TWL6040_REG_MICRCTL, 2, 0),
  963. /* Microphone bias */
  964. SND_SOC_DAPM_MICBIAS("Headset Mic Bias",
  965. TWL6040_REG_AMICBCTL, 0, 0),
  966. SND_SOC_DAPM_MICBIAS("Main Mic Bias",
  967. TWL6040_REG_AMICBCTL, 4, 0),
  968. SND_SOC_DAPM_MICBIAS("Digital Mic1 Bias",
  969. TWL6040_REG_DMICBCTL, 0, 0),
  970. SND_SOC_DAPM_MICBIAS("Digital Mic2 Bias",
  971. TWL6040_REG_DMICBCTL, 4, 0),
  972. /* DACs */
  973. SND_SOC_DAPM_DAC_E("HSDAC Left", "Headset Playback",
  974. TWL6040_REG_HSLCTL, 0, 0,
  975. twl6040_hs_dac_event,
  976. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  977. SND_SOC_DAPM_DAC_E("HSDAC Right", "Headset Playback",
  978. TWL6040_REG_HSRCTL, 0, 0,
  979. twl6040_hs_dac_event,
  980. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  981. SND_SOC_DAPM_DAC_E("HFDAC Left", "Handsfree Playback",
  982. TWL6040_REG_HFLCTL, 0, 0,
  983. twl6040_power_mode_event,
  984. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  985. SND_SOC_DAPM_DAC_E("HFDAC Right", "Handsfree Playback",
  986. TWL6040_REG_HFRCTL, 0, 0,
  987. twl6040_power_mode_event,
  988. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  989. SND_SOC_DAPM_MUX("Handsfree Left Playback",
  990. SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
  991. SND_SOC_DAPM_MUX("Handsfree Right Playback",
  992. SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
  993. /* Analog playback Muxes */
  994. SND_SOC_DAPM_MUX("Headset Left Playback",
  995. SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
  996. SND_SOC_DAPM_MUX("Headset Right Playback",
  997. SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
  998. SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
  999. &ep_path_enable_control),
  1000. /* Analog playback drivers */
  1001. SND_SOC_DAPM_OUT_DRV_E("HF Left Driver",
  1002. TWL6040_REG_HFLCTL, 4, 0, NULL, 0,
  1003. pga_event,
  1004. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  1005. SND_SOC_DAPM_OUT_DRV_E("HF Right Driver",
  1006. TWL6040_REG_HFRCTL, 4, 0, NULL, 0,
  1007. pga_event,
  1008. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  1009. SND_SOC_DAPM_OUT_DRV_E("HS Left Driver",
  1010. TWL6040_REG_HSLCTL, 2, 0, NULL, 0,
  1011. pga_event,
  1012. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  1013. SND_SOC_DAPM_OUT_DRV_E("HS Right Driver",
  1014. TWL6040_REG_HSRCTL, 2, 0, NULL, 0,
  1015. pga_event,
  1016. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  1017. SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
  1018. TWL6040_REG_EARCTL, 0, 0, NULL, 0,
  1019. twl6040_power_mode_event,
  1020. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  1021. /* Analog playback PGAs */
  1022. SND_SOC_DAPM_PGA("HF Left PGA",
  1023. TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
  1024. SND_SOC_DAPM_PGA("HF Right PGA",
  1025. TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
  1026. };
  1027. static const struct snd_soc_dapm_route intercon[] = {
  1028. /* Capture path */
  1029. {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
  1030. {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
  1031. {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
  1032. {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
  1033. {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
  1034. {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
  1035. {"MicAmpL", NULL, "Analog Left Capture Route"},
  1036. {"MicAmpR", NULL, "Analog Right Capture Route"},
  1037. {"ADC Left", NULL, "MicAmpL"},
  1038. {"ADC Right", NULL, "MicAmpR"},
  1039. /* AFM path */
  1040. {"AFMAmpL", NULL, "AFML"},
  1041. {"AFMAmpR", NULL, "AFMR"},
  1042. {"Headset Left Playback", "HS DAC", "HSDAC Left"},
  1043. {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
  1044. {"Headset Right Playback", "HS DAC", "HSDAC Right"},
  1045. {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
  1046. {"HS Left Driver", NULL, "Headset Left Playback"},
  1047. {"HS Right Driver", NULL, "Headset Right Playback"},
  1048. {"HSOL", NULL, "HS Left Driver"},
  1049. {"HSOR", NULL, "HS Right Driver"},
  1050. /* Earphone playback path */
  1051. {"Earphone Playback", "Switch", "HSDAC Left"},
  1052. {"Earphone Driver", NULL, "Earphone Playback"},
  1053. {"EP", NULL, "Earphone Driver"},
  1054. {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
  1055. {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
  1056. {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
  1057. {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
  1058. {"HF Left PGA", NULL, "Handsfree Left Playback"},
  1059. {"HF Right PGA", NULL, "Handsfree Right Playback"},
  1060. {"HF Left Driver", NULL, "HF Left PGA"},
  1061. {"HF Right Driver", NULL, "HF Right PGA"},
  1062. {"HFL", NULL, "HF Left Driver"},
  1063. {"HFR", NULL, "HF Right Driver"},
  1064. };
  1065. static int twl6040_add_widgets(struct snd_soc_codec *codec)
  1066. {
  1067. struct snd_soc_dapm_context *dapm = &codec->dapm;
  1068. snd_soc_dapm_new_controls(dapm, twl6040_dapm_widgets,
  1069. ARRAY_SIZE(twl6040_dapm_widgets));
  1070. snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
  1071. snd_soc_dapm_new_widgets(dapm);
  1072. return 0;
  1073. }
  1074. static int twl6040_set_bias_level(struct snd_soc_codec *codec,
  1075. enum snd_soc_bias_level level)
  1076. {
  1077. struct twl6040 *twl6040 = codec->control_data;
  1078. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  1079. int ret;
  1080. switch (level) {
  1081. case SND_SOC_BIAS_ON:
  1082. break;
  1083. case SND_SOC_BIAS_PREPARE:
  1084. break;
  1085. case SND_SOC_BIAS_STANDBY:
  1086. if (priv->codec_powered)
  1087. break;
  1088. ret = twl6040_power(twl6040, 1);
  1089. if (ret)
  1090. return ret;
  1091. priv->codec_powered = 1;
  1092. twl6040_restore_regs(codec);
  1093. /* Set external boost GPO */
  1094. twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
  1095. break;
  1096. case SND_SOC_BIAS_OFF:
  1097. if (!priv->codec_powered)
  1098. break;
  1099. twl6040_power(twl6040, 0);
  1100. priv->codec_powered = 0;
  1101. break;
  1102. }
  1103. codec->dapm.bias_level = level;
  1104. return 0;
  1105. }
  1106. static int twl6040_startup(struct snd_pcm_substream *substream,
  1107. struct snd_soc_dai *dai)
  1108. {
  1109. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1110. struct snd_soc_codec *codec = rtd->codec;
  1111. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  1112. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1113. SNDRV_PCM_HW_PARAM_RATE,
  1114. &sysclk_constraints[priv->pll_power_mode]);
  1115. return 0;
  1116. }
  1117. static int twl6040_hw_params(struct snd_pcm_substream *substream,
  1118. struct snd_pcm_hw_params *params,
  1119. struct snd_soc_dai *dai)
  1120. {
  1121. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1122. struct snd_soc_codec *codec = rtd->codec;
  1123. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  1124. int rate;
  1125. rate = params_rate(params);
  1126. switch (rate) {
  1127. case 11250:
  1128. case 22500:
  1129. case 44100:
  1130. case 88200:
  1131. /* These rates are not supported when HPPLL is in use */
  1132. if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
  1133. dev_err(codec->dev, "HPPLL does not support rate %d\n",
  1134. rate);
  1135. return -EINVAL;
  1136. }
  1137. /* Capture is not supported with 17.64MHz sysclk */
  1138. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  1139. dev_err(codec->dev,
  1140. "capture mode is not supported at %dHz\n",
  1141. rate);
  1142. return -EINVAL;
  1143. }
  1144. priv->sysclk = 17640000;
  1145. break;
  1146. case 8000:
  1147. case 16000:
  1148. case 32000:
  1149. case 48000:
  1150. case 96000:
  1151. priv->sysclk = 19200000;
  1152. break;
  1153. default:
  1154. dev_err(codec->dev, "unsupported rate %d\n", rate);
  1155. return -EINVAL;
  1156. }
  1157. return 0;
  1158. }
  1159. static int twl6040_prepare(struct snd_pcm_substream *substream,
  1160. struct snd_soc_dai *dai)
  1161. {
  1162. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1163. struct snd_soc_codec *codec = rtd->codec;
  1164. struct twl6040 *twl6040 = codec->control_data;
  1165. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  1166. int ret;
  1167. if (!priv->sysclk) {
  1168. dev_err(codec->dev,
  1169. "no mclk configured, call set_sysclk() on init\n");
  1170. return -EINVAL;
  1171. }
  1172. if ((priv->sysclk == 17640000) && priv->non_lp) {
  1173. dev_err(codec->dev,
  1174. "some enabled paths aren't supported at %dHz\n",
  1175. priv->sysclk);
  1176. return -EPERM;
  1177. }
  1178. ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
  1179. if (ret) {
  1180. dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
  1181. return -EPERM;
  1182. }
  1183. return 0;
  1184. }
  1185. static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  1186. int clk_id, unsigned int freq, int dir)
  1187. {
  1188. struct snd_soc_codec *codec = codec_dai->codec;
  1189. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  1190. switch (clk_id) {
  1191. case TWL6040_SYSCLK_SEL_LPPLL:
  1192. case TWL6040_SYSCLK_SEL_HPPLL:
  1193. priv->pll = clk_id;
  1194. priv->clk_in = freq;
  1195. break;
  1196. default:
  1197. dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
  1198. return -EINVAL;
  1199. }
  1200. return 0;
  1201. }
  1202. static struct snd_soc_dai_ops twl6040_dai_ops = {
  1203. .startup = twl6040_startup,
  1204. .hw_params = twl6040_hw_params,
  1205. .prepare = twl6040_prepare,
  1206. .set_sysclk = twl6040_set_dai_sysclk,
  1207. };
  1208. static struct snd_soc_dai_driver twl6040_dai[] = {
  1209. {
  1210. .name = "twl6040-hifi",
  1211. .playback = {
  1212. .stream_name = "Playback",
  1213. .channels_min = 1,
  1214. .channels_max = 5,
  1215. .rates = TWL6040_RATES,
  1216. .formats = TWL6040_FORMATS,
  1217. },
  1218. .capture = {
  1219. .stream_name = "Capture",
  1220. .channels_min = 1,
  1221. .channels_max = 2,
  1222. .rates = TWL6040_RATES,
  1223. .formats = TWL6040_FORMATS,
  1224. },
  1225. .ops = &twl6040_dai_ops,
  1226. },
  1227. {
  1228. .name = "twl6040-ul",
  1229. .capture = {
  1230. .stream_name = "Capture",
  1231. .channels_min = 1,
  1232. .channels_max = 2,
  1233. .rates = TWL6040_RATES,
  1234. .formats = TWL6040_FORMATS,
  1235. },
  1236. .ops = &twl6040_dai_ops,
  1237. },
  1238. {
  1239. .name = "twl6040-dl1",
  1240. .playback = {
  1241. .stream_name = "Headset Playback",
  1242. .channels_min = 1,
  1243. .channels_max = 2,
  1244. .rates = TWL6040_RATES,
  1245. .formats = TWL6040_FORMATS,
  1246. },
  1247. .ops = &twl6040_dai_ops,
  1248. },
  1249. {
  1250. .name = "twl6040-dl2",
  1251. .playback = {
  1252. .stream_name = "Handsfree Playback",
  1253. .channels_min = 1,
  1254. .channels_max = 2,
  1255. .rates = TWL6040_RATES,
  1256. .formats = TWL6040_FORMATS,
  1257. },
  1258. .ops = &twl6040_dai_ops,
  1259. },
  1260. {
  1261. .name = "twl6040-vib",
  1262. .playback = {
  1263. .stream_name = "Vibra Playback",
  1264. .channels_min = 1,
  1265. .channels_max = 1,
  1266. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  1267. .formats = TWL6040_FORMATS,
  1268. },
  1269. .ops = &twl6040_dai_ops,
  1270. },
  1271. };
  1272. #ifdef CONFIG_PM
  1273. static int twl6040_suspend(struct snd_soc_codec *codec, pm_message_t state)
  1274. {
  1275. twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1276. return 0;
  1277. }
  1278. static int twl6040_resume(struct snd_soc_codec *codec)
  1279. {
  1280. twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1281. twl6040_set_bias_level(codec, codec->dapm.suspend_bias_level);
  1282. return 0;
  1283. }
  1284. #else
  1285. #define twl6040_suspend NULL
  1286. #define twl6040_resume NULL
  1287. #endif
  1288. static int twl6040_probe(struct snd_soc_codec *codec)
  1289. {
  1290. struct twl6040_data *priv;
  1291. struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
  1292. struct platform_device *pdev = container_of(codec->dev,
  1293. struct platform_device, dev);
  1294. int ret = 0;
  1295. priv = kzalloc(sizeof(struct twl6040_data), GFP_KERNEL);
  1296. if (priv == NULL)
  1297. return -ENOMEM;
  1298. snd_soc_codec_set_drvdata(codec, priv);
  1299. priv->codec = codec;
  1300. codec->control_data = dev_get_drvdata(codec->dev->parent);
  1301. if (pdata && pdata->hs_left_step && pdata->hs_right_step) {
  1302. priv->hs_left_step = pdata->hs_left_step;
  1303. priv->hs_right_step = pdata->hs_right_step;
  1304. } else {
  1305. priv->hs_left_step = 1;
  1306. priv->hs_right_step = 1;
  1307. }
  1308. if (pdata && pdata->hf_left_step && pdata->hf_right_step) {
  1309. priv->hf_left_step = pdata->hf_left_step;
  1310. priv->hf_right_step = pdata->hf_right_step;
  1311. } else {
  1312. priv->hf_left_step = 1;
  1313. priv->hf_right_step = 1;
  1314. }
  1315. priv->plug_irq = platform_get_irq(pdev, 0);
  1316. if (priv->plug_irq < 0) {
  1317. dev_err(codec->dev, "invalid irq\n");
  1318. ret = -EINVAL;
  1319. goto work_err;
  1320. }
  1321. priv->workqueue = create_singlethread_workqueue("twl6040-codec");
  1322. if (!priv->workqueue) {
  1323. ret = -ENOMEM;
  1324. goto work_err;
  1325. }
  1326. INIT_DELAYED_WORK(&priv->delayed_work, twl6040_accessory_work);
  1327. mutex_init(&priv->mutex);
  1328. init_completion(&priv->headset.ramp_done);
  1329. init_completion(&priv->handsfree.ramp_done);
  1330. priv->hf_workqueue = create_singlethread_workqueue("twl6040-hf");
  1331. if (priv->hf_workqueue == NULL) {
  1332. ret = -ENOMEM;
  1333. goto hfwq_err;
  1334. }
  1335. priv->hs_workqueue = create_singlethread_workqueue("twl6040-hs");
  1336. if (priv->hs_workqueue == NULL) {
  1337. ret = -ENOMEM;
  1338. goto hswq_err;
  1339. }
  1340. INIT_DELAYED_WORK(&priv->hs_delayed_work, twl6040_pga_hs_work);
  1341. INIT_DELAYED_WORK(&priv->hf_delayed_work, twl6040_pga_hf_work);
  1342. ret = request_threaded_irq(priv->plug_irq, NULL, twl6040_audio_handler,
  1343. 0, "twl6040_irq_plug", codec);
  1344. if (ret) {
  1345. dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
  1346. goto plugirq_err;
  1347. }
  1348. twl6040_init_chip(codec);
  1349. /* power on device */
  1350. ret = twl6040_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  1351. if (ret)
  1352. goto bias_err;
  1353. snd_soc_add_controls(codec, twl6040_snd_controls,
  1354. ARRAY_SIZE(twl6040_snd_controls));
  1355. twl6040_add_widgets(codec);
  1356. return 0;
  1357. bias_err:
  1358. free_irq(priv->plug_irq, codec);
  1359. plugirq_err:
  1360. destroy_workqueue(priv->hs_workqueue);
  1361. hswq_err:
  1362. destroy_workqueue(priv->hf_workqueue);
  1363. hfwq_err:
  1364. destroy_workqueue(priv->workqueue);
  1365. work_err:
  1366. kfree(priv);
  1367. return ret;
  1368. }
  1369. static int twl6040_remove(struct snd_soc_codec *codec)
  1370. {
  1371. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  1372. twl6040_set_bias_level(codec, SND_SOC_BIAS_OFF);
  1373. free_irq(priv->plug_irq, codec);
  1374. destroy_workqueue(priv->workqueue);
  1375. destroy_workqueue(priv->hf_workqueue);
  1376. destroy_workqueue(priv->hs_workqueue);
  1377. kfree(priv);
  1378. return 0;
  1379. }
  1380. static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
  1381. .probe = twl6040_probe,
  1382. .remove = twl6040_remove,
  1383. .suspend = twl6040_suspend,
  1384. .resume = twl6040_resume,
  1385. .read = twl6040_read_reg_cache,
  1386. .write = twl6040_write,
  1387. .set_bias_level = twl6040_set_bias_level,
  1388. .reg_cache_size = ARRAY_SIZE(twl6040_reg),
  1389. .reg_word_size = sizeof(u8),
  1390. .reg_cache_default = twl6040_reg,
  1391. };
  1392. static int __devinit twl6040_codec_probe(struct platform_device *pdev)
  1393. {
  1394. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
  1395. twl6040_dai, ARRAY_SIZE(twl6040_dai));
  1396. }
  1397. static int __devexit twl6040_codec_remove(struct platform_device *pdev)
  1398. {
  1399. snd_soc_unregister_codec(&pdev->dev);
  1400. return 0;
  1401. }
  1402. static struct platform_driver twl6040_codec_driver = {
  1403. .driver = {
  1404. .name = "twl6040-codec",
  1405. .owner = THIS_MODULE,
  1406. },
  1407. .probe = twl6040_codec_probe,
  1408. .remove = __devexit_p(twl6040_codec_remove),
  1409. };
  1410. static int __init twl6040_codec_init(void)
  1411. {
  1412. return platform_driver_register(&twl6040_codec_driver);
  1413. }
  1414. module_init(twl6040_codec_init);
  1415. static void __exit twl6040_codec_exit(void)
  1416. {
  1417. platform_driver_unregister(&twl6040_codec_driver);
  1418. }
  1419. module_exit(twl6040_codec_exit);
  1420. MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
  1421. MODULE_AUTHOR("Misael Lopez Cruz");
  1422. MODULE_LICENSE("GPL");