twl6040.c 42 KB

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