twl6040.c 42 KB

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