twl6040.c 42 KB

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