twl6040.c 43 KB

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