twl6040.c 43 KB

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