twl6040.c 42 KB

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