twl6040.c 43 KB

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