twl6040.c 39 KB

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