twl6040.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * ALSA SoC TWL6040 codec driver
  3. *
  4. * Author: Misael Lopez Cruz <x0052729@ti.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/pm.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/mfd/twl6040.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include <sound/soc-dapm.h>
  34. #include <sound/initval.h>
  35. #include <sound/tlv.h>
  36. #include "twl6040.h"
  37. enum twl6040_dai_id {
  38. TWL6040_DAI_LEGACY = 0,
  39. TWL6040_DAI_UL,
  40. TWL6040_DAI_DL1,
  41. TWL6040_DAI_DL2,
  42. TWL6040_DAI_VIB,
  43. };
  44. #define TWL6040_RATES SNDRV_PCM_RATE_8000_96000
  45. #define TWL6040_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
  46. #define TWL6040_OUTHS_0dB 0x00
  47. #define TWL6040_OUTHS_M30dB 0x0F
  48. #define TWL6040_OUTHF_0dB 0x03
  49. #define TWL6040_OUTHF_M52dB 0x1D
  50. #define TWL6040_CACHEREGNUM (TWL6040_REG_STATUS + 1)
  51. struct twl6040_jack_data {
  52. struct snd_soc_jack *jack;
  53. struct delayed_work work;
  54. int report;
  55. };
  56. /* codec private data */
  57. struct twl6040_data {
  58. int plug_irq;
  59. int codec_powered;
  60. int pll;
  61. int pll_power_mode;
  62. int hs_power_mode;
  63. int hs_power_mode_locked;
  64. bool dl1_unmuted;
  65. bool dl2_unmuted;
  66. u8 dl12_cache[TWL6040_REG_HFRCTL - TWL6040_REG_HSLCTL + 1];
  67. unsigned int clk_in;
  68. unsigned int sysclk;
  69. struct twl6040_jack_data hs_jack;
  70. struct snd_soc_codec *codec;
  71. struct mutex mutex;
  72. };
  73. /* set of rates for each pll: low-power and high-performance */
  74. static const unsigned int lp_rates[] = {
  75. 8000,
  76. 11250,
  77. 16000,
  78. 22500,
  79. 32000,
  80. 44100,
  81. 48000,
  82. 88200,
  83. 96000,
  84. };
  85. static const unsigned int hp_rates[] = {
  86. 8000,
  87. 16000,
  88. 32000,
  89. 48000,
  90. 96000,
  91. };
  92. static const struct snd_pcm_hw_constraint_list sysclk_constraints[] = {
  93. { .count = ARRAY_SIZE(lp_rates), .list = lp_rates, },
  94. { .count = ARRAY_SIZE(hp_rates), .list = hp_rates, },
  95. };
  96. static unsigned int twl6040_read(struct snd_soc_codec *codec, unsigned int reg)
  97. {
  98. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  99. struct twl6040 *twl6040 = codec->control_data;
  100. u8 value;
  101. if (reg >= TWL6040_CACHEREGNUM)
  102. return -EIO;
  103. switch (reg) {
  104. case TWL6040_REG_HSLCTL:
  105. case TWL6040_REG_HSRCTL:
  106. case TWL6040_REG_EARCTL:
  107. case TWL6040_REG_HFLCTL:
  108. case TWL6040_REG_HFRCTL:
  109. value = priv->dl12_cache[reg - TWL6040_REG_HSLCTL];
  110. break;
  111. default:
  112. value = twl6040_reg_read(twl6040, reg);
  113. break;
  114. }
  115. return value;
  116. }
  117. static bool twl6040_can_write_to_chip(struct snd_soc_codec *codec,
  118. unsigned int reg)
  119. {
  120. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  121. switch (reg) {
  122. case TWL6040_REG_HSLCTL:
  123. case TWL6040_REG_HSRCTL:
  124. case TWL6040_REG_EARCTL:
  125. /* DL1 path */
  126. return priv->dl1_unmuted;
  127. case TWL6040_REG_HFLCTL:
  128. case TWL6040_REG_HFRCTL:
  129. return priv->dl2_unmuted;
  130. default:
  131. return 1;
  132. }
  133. }
  134. static inline void twl6040_update_dl12_cache(struct snd_soc_codec *codec,
  135. u8 reg, u8 value)
  136. {
  137. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  138. switch (reg) {
  139. case TWL6040_REG_HSLCTL:
  140. case TWL6040_REG_HSRCTL:
  141. case TWL6040_REG_EARCTL:
  142. case TWL6040_REG_HFLCTL:
  143. case TWL6040_REG_HFRCTL:
  144. priv->dl12_cache[reg - TWL6040_REG_HSLCTL] = value;
  145. break;
  146. default:
  147. break;
  148. }
  149. }
  150. static int twl6040_write(struct snd_soc_codec *codec,
  151. unsigned int reg, unsigned int value)
  152. {
  153. struct twl6040 *twl6040 = codec->control_data;
  154. if (reg >= TWL6040_CACHEREGNUM)
  155. return -EIO;
  156. twl6040_update_dl12_cache(codec, reg, value);
  157. if (twl6040_can_write_to_chip(codec, reg))
  158. return twl6040_reg_write(twl6040, reg, value);
  159. else
  160. return 0;
  161. }
  162. static void twl6040_init_chip(struct snd_soc_codec *codec)
  163. {
  164. twl6040_read(codec, TWL6040_REG_TRIM1);
  165. twl6040_read(codec, TWL6040_REG_TRIM2);
  166. twl6040_read(codec, TWL6040_REG_TRIM3);
  167. twl6040_read(codec, TWL6040_REG_HSOTRIM);
  168. twl6040_read(codec, TWL6040_REG_HFOTRIM);
  169. /* Change chip defaults */
  170. /* No imput selected for microphone amplifiers */
  171. twl6040_write(codec, TWL6040_REG_MICLCTL, 0x18);
  172. twl6040_write(codec, TWL6040_REG_MICRCTL, 0x18);
  173. /*
  174. * We need to lower the default gain values, so the ramp code
  175. * can work correctly for the first playback.
  176. * This reduces the pop noise heard at the first playback.
  177. */
  178. twl6040_write(codec, TWL6040_REG_HSGAIN, 0xff);
  179. twl6040_write(codec, TWL6040_REG_EARCTL, 0x1e);
  180. twl6040_write(codec, TWL6040_REG_HFLGAIN, 0x1d);
  181. twl6040_write(codec, TWL6040_REG_HFRGAIN, 0x1d);
  182. twl6040_write(codec, TWL6040_REG_LINEGAIN, 0);
  183. }
  184. /* set headset dac and driver power mode */
  185. static int headset_power_mode(struct snd_soc_codec *codec, int high_perf)
  186. {
  187. int hslctl, hsrctl;
  188. int mask = TWL6040_HSDRVMODE | TWL6040_HSDACMODE;
  189. hslctl = twl6040_read(codec, TWL6040_REG_HSLCTL);
  190. hsrctl = twl6040_read(codec, TWL6040_REG_HSRCTL);
  191. if (high_perf) {
  192. hslctl &= ~mask;
  193. hsrctl &= ~mask;
  194. } else {
  195. hslctl |= mask;
  196. hsrctl |= mask;
  197. }
  198. twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
  199. twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
  200. return 0;
  201. }
  202. static int twl6040_hs_dac_event(struct snd_soc_dapm_widget *w,
  203. struct snd_kcontrol *kcontrol, int event)
  204. {
  205. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  206. u8 hslctl, hsrctl;
  207. /*
  208. * Workaround for Headset DC offset caused pop noise:
  209. * Both HS DAC need to be turned on (before the HS driver) and off at
  210. * the same time.
  211. */
  212. hslctl = twl6040_read(codec, TWL6040_REG_HSLCTL);
  213. hsrctl = twl6040_read(codec, TWL6040_REG_HSRCTL);
  214. if (SND_SOC_DAPM_EVENT_ON(event)) {
  215. hslctl |= TWL6040_HSDACENA;
  216. hsrctl |= TWL6040_HSDACENA;
  217. } else {
  218. hslctl &= ~TWL6040_HSDACENA;
  219. hsrctl &= ~TWL6040_HSDACENA;
  220. }
  221. twl6040_write(codec, TWL6040_REG_HSLCTL, hslctl);
  222. twl6040_write(codec, TWL6040_REG_HSRCTL, hsrctl);
  223. msleep(1);
  224. return 0;
  225. }
  226. static int twl6040_ep_drv_event(struct snd_soc_dapm_widget *w,
  227. struct snd_kcontrol *kcontrol, int event)
  228. {
  229. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  230. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  231. int ret = 0;
  232. if (SND_SOC_DAPM_EVENT_ON(event)) {
  233. /* Earphone doesn't support low power mode */
  234. priv->hs_power_mode_locked = 1;
  235. ret = headset_power_mode(codec, 1);
  236. } else {
  237. priv->hs_power_mode_locked = 0;
  238. ret = headset_power_mode(codec, priv->hs_power_mode);
  239. }
  240. msleep(1);
  241. return ret;
  242. }
  243. static void twl6040_hs_jack_report(struct snd_soc_codec *codec,
  244. struct snd_soc_jack *jack, int report)
  245. {
  246. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  247. int status;
  248. mutex_lock(&priv->mutex);
  249. /* Sync status */
  250. status = twl6040_read(codec, TWL6040_REG_STATUS);
  251. if (status & TWL6040_PLUGCOMP)
  252. snd_soc_jack_report(jack, report, report);
  253. else
  254. snd_soc_jack_report(jack, 0, report);
  255. mutex_unlock(&priv->mutex);
  256. }
  257. void twl6040_hs_jack_detect(struct snd_soc_codec *codec,
  258. struct snd_soc_jack *jack, int report)
  259. {
  260. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  261. struct twl6040_jack_data *hs_jack = &priv->hs_jack;
  262. hs_jack->jack = jack;
  263. hs_jack->report = report;
  264. twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
  265. }
  266. EXPORT_SYMBOL_GPL(twl6040_hs_jack_detect);
  267. static void twl6040_accessory_work(struct work_struct *work)
  268. {
  269. struct twl6040_data *priv = container_of(work,
  270. struct twl6040_data, hs_jack.work.work);
  271. struct snd_soc_codec *codec = priv->codec;
  272. struct twl6040_jack_data *hs_jack = &priv->hs_jack;
  273. twl6040_hs_jack_report(codec, hs_jack->jack, hs_jack->report);
  274. }
  275. /* audio interrupt handler */
  276. static irqreturn_t twl6040_audio_handler(int irq, void *data)
  277. {
  278. struct snd_soc_codec *codec = data;
  279. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  280. queue_delayed_work(system_power_efficient_wq,
  281. &priv->hs_jack.work, msecs_to_jiffies(200));
  282. return IRQ_HANDLED;
  283. }
  284. static int twl6040_soc_dapm_put_vibra_enum(struct snd_kcontrol *kcontrol,
  285. struct snd_ctl_elem_value *ucontrol)
  286. {
  287. struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
  288. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  289. unsigned int val;
  290. /* Do not allow changes while Input/FF efect is running */
  291. val = twl6040_read(codec, e->reg);
  292. if (val & TWL6040_VIBENA && !(val & TWL6040_VIBSEL))
  293. return -EBUSY;
  294. return snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
  295. }
  296. /*
  297. * MICATT volume control:
  298. * from -6 to 0 dB in 6 dB steps
  299. */
  300. static DECLARE_TLV_DB_SCALE(mic_preamp_tlv, -600, 600, 0);
  301. /*
  302. * MICGAIN volume control:
  303. * from 6 to 30 dB in 6 dB steps
  304. */
  305. static DECLARE_TLV_DB_SCALE(mic_amp_tlv, 600, 600, 0);
  306. /*
  307. * AFMGAIN volume control:
  308. * from -18 to 24 dB in 6 dB steps
  309. */
  310. static DECLARE_TLV_DB_SCALE(afm_amp_tlv, -1800, 600, 0);
  311. /*
  312. * HSGAIN volume control:
  313. * from -30 to 0 dB in 2 dB steps
  314. */
  315. static DECLARE_TLV_DB_SCALE(hs_tlv, -3000, 200, 0);
  316. /*
  317. * HFGAIN volume control:
  318. * from -52 to 6 dB in 2 dB steps
  319. */
  320. static DECLARE_TLV_DB_SCALE(hf_tlv, -5200, 200, 0);
  321. /*
  322. * EPGAIN volume control:
  323. * from -24 to 6 dB in 2 dB steps
  324. */
  325. static DECLARE_TLV_DB_SCALE(ep_tlv, -2400, 200, 0);
  326. /* Left analog microphone selection */
  327. static const char *twl6040_amicl_texts[] =
  328. {"Headset Mic", "Main Mic", "Aux/FM Left", "Off"};
  329. /* Right analog microphone selection */
  330. static const char *twl6040_amicr_texts[] =
  331. {"Headset Mic", "Sub Mic", "Aux/FM Right", "Off"};
  332. static const struct soc_enum twl6040_enum[] = {
  333. SOC_ENUM_SINGLE(TWL6040_REG_MICLCTL, 3,
  334. ARRAY_SIZE(twl6040_amicl_texts), twl6040_amicl_texts),
  335. SOC_ENUM_SINGLE(TWL6040_REG_MICRCTL, 3,
  336. ARRAY_SIZE(twl6040_amicr_texts), twl6040_amicr_texts),
  337. };
  338. static const char *twl6040_hs_texts[] = {
  339. "Off", "HS DAC", "Line-In amp"
  340. };
  341. static const struct soc_enum twl6040_hs_enum[] = {
  342. SOC_ENUM_SINGLE(TWL6040_REG_HSLCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
  343. twl6040_hs_texts),
  344. SOC_ENUM_SINGLE(TWL6040_REG_HSRCTL, 5, ARRAY_SIZE(twl6040_hs_texts),
  345. twl6040_hs_texts),
  346. };
  347. static const char *twl6040_hf_texts[] = {
  348. "Off", "HF DAC", "Line-In amp"
  349. };
  350. static const struct soc_enum twl6040_hf_enum[] = {
  351. SOC_ENUM_SINGLE(TWL6040_REG_HFLCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
  352. twl6040_hf_texts),
  353. SOC_ENUM_SINGLE(TWL6040_REG_HFRCTL, 2, ARRAY_SIZE(twl6040_hf_texts),
  354. twl6040_hf_texts),
  355. };
  356. static const char *twl6040_vibrapath_texts[] = {
  357. "Input FF", "Audio PDM"
  358. };
  359. static const struct soc_enum twl6040_vibra_enum[] = {
  360. SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLL, 1,
  361. ARRAY_SIZE(twl6040_vibrapath_texts),
  362. twl6040_vibrapath_texts),
  363. SOC_ENUM_SINGLE(TWL6040_REG_VIBCTLR, 1,
  364. ARRAY_SIZE(twl6040_vibrapath_texts),
  365. twl6040_vibrapath_texts),
  366. };
  367. static const struct snd_kcontrol_new amicl_control =
  368. SOC_DAPM_ENUM("Route", twl6040_enum[0]);
  369. static const struct snd_kcontrol_new amicr_control =
  370. SOC_DAPM_ENUM("Route", twl6040_enum[1]);
  371. /* Headset DAC playback switches */
  372. static const struct snd_kcontrol_new hsl_mux_controls =
  373. SOC_DAPM_ENUM("Route", twl6040_hs_enum[0]);
  374. static const struct snd_kcontrol_new hsr_mux_controls =
  375. SOC_DAPM_ENUM("Route", twl6040_hs_enum[1]);
  376. /* Handsfree DAC playback switches */
  377. static const struct snd_kcontrol_new hfl_mux_controls =
  378. SOC_DAPM_ENUM("Route", twl6040_hf_enum[0]);
  379. static const struct snd_kcontrol_new hfr_mux_controls =
  380. SOC_DAPM_ENUM("Route", twl6040_hf_enum[1]);
  381. static const struct snd_kcontrol_new ep_path_enable_control =
  382. SOC_DAPM_SINGLE_VIRT("Switch", 1);
  383. static const struct snd_kcontrol_new auxl_switch_control =
  384. SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFLCTL, 6, 1, 0);
  385. static const struct snd_kcontrol_new auxr_switch_control =
  386. SOC_DAPM_SINGLE("Switch", TWL6040_REG_HFRCTL, 6, 1, 0);
  387. /* Vibra playback switches */
  388. static const struct snd_kcontrol_new vibral_mux_controls =
  389. SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[0],
  390. snd_soc_dapm_get_enum_double,
  391. twl6040_soc_dapm_put_vibra_enum);
  392. static const struct snd_kcontrol_new vibrar_mux_controls =
  393. SOC_DAPM_ENUM_EXT("Route", twl6040_vibra_enum[1],
  394. snd_soc_dapm_get_enum_double,
  395. twl6040_soc_dapm_put_vibra_enum);
  396. /* Headset power mode */
  397. static const char *twl6040_power_mode_texts[] = {
  398. "Low-Power", "High-Performance",
  399. };
  400. static SOC_ENUM_SINGLE_EXT_DECL(twl6040_power_mode_enum,
  401. twl6040_power_mode_texts);
  402. static int twl6040_headset_power_get_enum(struct snd_kcontrol *kcontrol,
  403. struct snd_ctl_elem_value *ucontrol)
  404. {
  405. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  406. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  407. ucontrol->value.enumerated.item[0] = priv->hs_power_mode;
  408. return 0;
  409. }
  410. static int twl6040_headset_power_put_enum(struct snd_kcontrol *kcontrol,
  411. struct snd_ctl_elem_value *ucontrol)
  412. {
  413. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  414. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  415. int high_perf = ucontrol->value.enumerated.item[0];
  416. int ret = 0;
  417. if (!priv->hs_power_mode_locked)
  418. ret = headset_power_mode(codec, high_perf);
  419. if (!ret)
  420. priv->hs_power_mode = high_perf;
  421. return ret;
  422. }
  423. static int twl6040_pll_get_enum(struct snd_kcontrol *kcontrol,
  424. struct snd_ctl_elem_value *ucontrol)
  425. {
  426. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  427. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  428. ucontrol->value.enumerated.item[0] = priv->pll_power_mode;
  429. return 0;
  430. }
  431. static int twl6040_pll_put_enum(struct snd_kcontrol *kcontrol,
  432. struct snd_ctl_elem_value *ucontrol)
  433. {
  434. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  435. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  436. priv->pll_power_mode = ucontrol->value.enumerated.item[0];
  437. return 0;
  438. }
  439. int twl6040_get_dl1_gain(struct snd_soc_codec *codec)
  440. {
  441. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  442. if (snd_soc_dapm_get_pin_status(dapm, "EP"))
  443. return -1; /* -1dB */
  444. if (snd_soc_dapm_get_pin_status(dapm, "HSOR") ||
  445. snd_soc_dapm_get_pin_status(dapm, "HSOL")) {
  446. u8 val = snd_soc_read(codec, TWL6040_REG_HSLCTL);
  447. if (val & TWL6040_HSDACMODE)
  448. /* HSDACL in LP mode */
  449. return -8; /* -8dB */
  450. else
  451. /* HSDACL in HP mode */
  452. return -1; /* -1dB */
  453. }
  454. return 0; /* 0dB */
  455. }
  456. EXPORT_SYMBOL_GPL(twl6040_get_dl1_gain);
  457. int twl6040_get_clk_id(struct snd_soc_codec *codec)
  458. {
  459. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  460. return priv->pll_power_mode;
  461. }
  462. EXPORT_SYMBOL_GPL(twl6040_get_clk_id);
  463. int twl6040_get_trim_value(struct snd_soc_codec *codec, enum twl6040_trim trim)
  464. {
  465. if (unlikely(trim >= TWL6040_TRIM_INVAL))
  466. return -EINVAL;
  467. return twl6040_read(codec, TWL6040_REG_TRIM1 + trim);
  468. }
  469. EXPORT_SYMBOL_GPL(twl6040_get_trim_value);
  470. int twl6040_get_hs_step_size(struct snd_soc_codec *codec)
  471. {
  472. struct twl6040 *twl6040 = codec->control_data;
  473. if (twl6040_get_revid(twl6040) < TWL6040_REV_ES1_3)
  474. /* For ES under ES_1.3 HS step is 2 mV */
  475. return 2;
  476. else
  477. /* For ES_1.3 HS step is 1 mV */
  478. return 1;
  479. }
  480. EXPORT_SYMBOL_GPL(twl6040_get_hs_step_size);
  481. static const struct snd_kcontrol_new twl6040_snd_controls[] = {
  482. /* Capture gains */
  483. SOC_DOUBLE_TLV("Capture Preamplifier Volume",
  484. TWL6040_REG_MICGAIN, 6, 7, 1, 1, mic_preamp_tlv),
  485. SOC_DOUBLE_TLV("Capture Volume",
  486. TWL6040_REG_MICGAIN, 0, 3, 4, 0, mic_amp_tlv),
  487. /* AFM gains */
  488. SOC_DOUBLE_TLV("Aux FM Volume",
  489. TWL6040_REG_LINEGAIN, 0, 3, 7, 0, afm_amp_tlv),
  490. /* Playback gains */
  491. SOC_DOUBLE_TLV("Headset Playback Volume",
  492. TWL6040_REG_HSGAIN, 0, 4, 0xF, 1, hs_tlv),
  493. SOC_DOUBLE_R_TLV("Handsfree Playback Volume",
  494. TWL6040_REG_HFLGAIN, TWL6040_REG_HFRGAIN, 0, 0x1D, 1, hf_tlv),
  495. SOC_SINGLE_TLV("Earphone Playback Volume",
  496. TWL6040_REG_EARCTL, 1, 0xF, 1, ep_tlv),
  497. SOC_ENUM_EXT("Headset Power Mode", twl6040_power_mode_enum,
  498. twl6040_headset_power_get_enum,
  499. twl6040_headset_power_put_enum),
  500. SOC_ENUM_EXT("PLL Selection", twl6040_power_mode_enum,
  501. twl6040_pll_get_enum, twl6040_pll_put_enum),
  502. };
  503. static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
  504. /* Inputs */
  505. SND_SOC_DAPM_INPUT("MAINMIC"),
  506. SND_SOC_DAPM_INPUT("HSMIC"),
  507. SND_SOC_DAPM_INPUT("SUBMIC"),
  508. SND_SOC_DAPM_INPUT("AFML"),
  509. SND_SOC_DAPM_INPUT("AFMR"),
  510. /* Outputs */
  511. SND_SOC_DAPM_OUTPUT("HSOL"),
  512. SND_SOC_DAPM_OUTPUT("HSOR"),
  513. SND_SOC_DAPM_OUTPUT("HFL"),
  514. SND_SOC_DAPM_OUTPUT("HFR"),
  515. SND_SOC_DAPM_OUTPUT("EP"),
  516. SND_SOC_DAPM_OUTPUT("AUXL"),
  517. SND_SOC_DAPM_OUTPUT("AUXR"),
  518. SND_SOC_DAPM_OUTPUT("VIBRAL"),
  519. SND_SOC_DAPM_OUTPUT("VIBRAR"),
  520. /* Analog input muxes for the capture amplifiers */
  521. SND_SOC_DAPM_MUX("Analog Left Capture Route",
  522. SND_SOC_NOPM, 0, 0, &amicl_control),
  523. SND_SOC_DAPM_MUX("Analog Right Capture Route",
  524. SND_SOC_NOPM, 0, 0, &amicr_control),
  525. /* Analog capture PGAs */
  526. SND_SOC_DAPM_PGA("MicAmpL",
  527. TWL6040_REG_MICLCTL, 0, 0, NULL, 0),
  528. SND_SOC_DAPM_PGA("MicAmpR",
  529. TWL6040_REG_MICRCTL, 0, 0, NULL, 0),
  530. /* Auxiliary FM PGAs */
  531. SND_SOC_DAPM_PGA("AFMAmpL",
  532. TWL6040_REG_MICLCTL, 1, 0, NULL, 0),
  533. SND_SOC_DAPM_PGA("AFMAmpR",
  534. TWL6040_REG_MICRCTL, 1, 0, NULL, 0),
  535. /* ADCs */
  536. SND_SOC_DAPM_ADC("ADC Left", NULL, TWL6040_REG_MICLCTL, 2, 0),
  537. SND_SOC_DAPM_ADC("ADC Right", NULL, TWL6040_REG_MICRCTL, 2, 0),
  538. /* Microphone bias */
  539. SND_SOC_DAPM_SUPPLY("Headset Mic Bias",
  540. TWL6040_REG_AMICBCTL, 0, 0, NULL, 0),
  541. SND_SOC_DAPM_SUPPLY("Main Mic Bias",
  542. TWL6040_REG_AMICBCTL, 4, 0, NULL, 0),
  543. SND_SOC_DAPM_SUPPLY("Digital Mic1 Bias",
  544. TWL6040_REG_DMICBCTL, 0, 0, NULL, 0),
  545. SND_SOC_DAPM_SUPPLY("Digital Mic2 Bias",
  546. TWL6040_REG_DMICBCTL, 4, 0, NULL, 0),
  547. /* DACs */
  548. SND_SOC_DAPM_DAC("HSDAC Left", NULL, SND_SOC_NOPM, 0, 0),
  549. SND_SOC_DAPM_DAC("HSDAC Right", NULL, SND_SOC_NOPM, 0, 0),
  550. SND_SOC_DAPM_DAC("HFDAC Left", NULL, TWL6040_REG_HFLCTL, 0, 0),
  551. SND_SOC_DAPM_DAC("HFDAC Right", NULL, TWL6040_REG_HFRCTL, 0, 0),
  552. /* Virtual DAC for vibra path (DL4 channel) */
  553. SND_SOC_DAPM_DAC("VIBRA DAC", NULL, SND_SOC_NOPM, 0, 0),
  554. SND_SOC_DAPM_MUX("Handsfree Left Playback",
  555. SND_SOC_NOPM, 0, 0, &hfl_mux_controls),
  556. SND_SOC_DAPM_MUX("Handsfree Right Playback",
  557. SND_SOC_NOPM, 0, 0, &hfr_mux_controls),
  558. /* Analog playback Muxes */
  559. SND_SOC_DAPM_MUX("Headset Left Playback",
  560. SND_SOC_NOPM, 0, 0, &hsl_mux_controls),
  561. SND_SOC_DAPM_MUX("Headset Right Playback",
  562. SND_SOC_NOPM, 0, 0, &hsr_mux_controls),
  563. SND_SOC_DAPM_MUX("Vibra Left Playback", SND_SOC_NOPM, 0, 0,
  564. &vibral_mux_controls),
  565. SND_SOC_DAPM_MUX("Vibra Right Playback", SND_SOC_NOPM, 0, 0,
  566. &vibrar_mux_controls),
  567. SND_SOC_DAPM_SWITCH("Earphone Playback", SND_SOC_NOPM, 0, 0,
  568. &ep_path_enable_control),
  569. SND_SOC_DAPM_SWITCH("AUXL Playback", SND_SOC_NOPM, 0, 0,
  570. &auxl_switch_control),
  571. SND_SOC_DAPM_SWITCH("AUXR Playback", SND_SOC_NOPM, 0, 0,
  572. &auxr_switch_control),
  573. /* Analog playback drivers */
  574. SND_SOC_DAPM_OUT_DRV("HF Left Driver",
  575. TWL6040_REG_HFLCTL, 4, 0, NULL, 0),
  576. SND_SOC_DAPM_OUT_DRV("HF Right Driver",
  577. TWL6040_REG_HFRCTL, 4, 0, NULL, 0),
  578. SND_SOC_DAPM_OUT_DRV("HS Left Driver",
  579. TWL6040_REG_HSLCTL, 2, 0, NULL, 0),
  580. SND_SOC_DAPM_OUT_DRV("HS Right Driver",
  581. TWL6040_REG_HSRCTL, 2, 0, NULL, 0),
  582. SND_SOC_DAPM_OUT_DRV_E("Earphone Driver",
  583. TWL6040_REG_EARCTL, 0, 0, NULL, 0,
  584. twl6040_ep_drv_event,
  585. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  586. SND_SOC_DAPM_OUT_DRV("Vibra Left Driver",
  587. TWL6040_REG_VIBCTLL, 0, 0, NULL, 0),
  588. SND_SOC_DAPM_OUT_DRV("Vibra Right Driver",
  589. TWL6040_REG_VIBCTLR, 0, 0, NULL, 0),
  590. SND_SOC_DAPM_SUPPLY("Vibra Left Control", TWL6040_REG_VIBCTLL, 2, 0,
  591. NULL, 0),
  592. SND_SOC_DAPM_SUPPLY("Vibra Right Control", TWL6040_REG_VIBCTLR, 2, 0,
  593. NULL, 0),
  594. SND_SOC_DAPM_SUPPLY_S("HSDAC Power", 1, SND_SOC_NOPM, 0, 0,
  595. twl6040_hs_dac_event,
  596. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  597. /* Analog playback PGAs */
  598. SND_SOC_DAPM_PGA("HF Left PGA",
  599. TWL6040_REG_HFLCTL, 1, 0, NULL, 0),
  600. SND_SOC_DAPM_PGA("HF Right PGA",
  601. TWL6040_REG_HFRCTL, 1, 0, NULL, 0),
  602. };
  603. static const struct snd_soc_dapm_route intercon[] = {
  604. /* Stream -> DAC mapping */
  605. {"HSDAC Left", NULL, "Legacy Playback"},
  606. {"HSDAC Left", NULL, "Headset Playback"},
  607. {"HSDAC Right", NULL, "Legacy Playback"},
  608. {"HSDAC Right", NULL, "Headset Playback"},
  609. {"HFDAC Left", NULL, "Legacy Playback"},
  610. {"HFDAC Left", NULL, "Handsfree Playback"},
  611. {"HFDAC Right", NULL, "Legacy Playback"},
  612. {"HFDAC Right", NULL, "Handsfree Playback"},
  613. {"VIBRA DAC", NULL, "Legacy Playback"},
  614. {"VIBRA DAC", NULL, "Vibra Playback"},
  615. /* ADC -> Stream mapping */
  616. {"Legacy Capture" , NULL, "ADC Left"},
  617. {"Capture", NULL, "ADC Left"},
  618. {"Legacy Capture", NULL, "ADC Right"},
  619. {"Capture" , NULL, "ADC Right"},
  620. /* Capture path */
  621. {"Analog Left Capture Route", "Headset Mic", "HSMIC"},
  622. {"Analog Left Capture Route", "Main Mic", "MAINMIC"},
  623. {"Analog Left Capture Route", "Aux/FM Left", "AFML"},
  624. {"Analog Right Capture Route", "Headset Mic", "HSMIC"},
  625. {"Analog Right Capture Route", "Sub Mic", "SUBMIC"},
  626. {"Analog Right Capture Route", "Aux/FM Right", "AFMR"},
  627. {"MicAmpL", NULL, "Analog Left Capture Route"},
  628. {"MicAmpR", NULL, "Analog Right Capture Route"},
  629. {"ADC Left", NULL, "MicAmpL"},
  630. {"ADC Right", NULL, "MicAmpR"},
  631. /* AFM path */
  632. {"AFMAmpL", NULL, "AFML"},
  633. {"AFMAmpR", NULL, "AFMR"},
  634. {"HSDAC Left", NULL, "HSDAC Power"},
  635. {"HSDAC Right", NULL, "HSDAC Power"},
  636. {"Headset Left Playback", "HS DAC", "HSDAC Left"},
  637. {"Headset Left Playback", "Line-In amp", "AFMAmpL"},
  638. {"Headset Right Playback", "HS DAC", "HSDAC Right"},
  639. {"Headset Right Playback", "Line-In amp", "AFMAmpR"},
  640. {"HS Left Driver", NULL, "Headset Left Playback"},
  641. {"HS Right Driver", NULL, "Headset Right Playback"},
  642. {"HSOL", NULL, "HS Left Driver"},
  643. {"HSOR", NULL, "HS Right Driver"},
  644. /* Earphone playback path */
  645. {"Earphone Playback", "Switch", "HSDAC Left"},
  646. {"Earphone Driver", NULL, "Earphone Playback"},
  647. {"EP", NULL, "Earphone Driver"},
  648. {"Handsfree Left Playback", "HF DAC", "HFDAC Left"},
  649. {"Handsfree Left Playback", "Line-In amp", "AFMAmpL"},
  650. {"Handsfree Right Playback", "HF DAC", "HFDAC Right"},
  651. {"Handsfree Right Playback", "Line-In amp", "AFMAmpR"},
  652. {"HF Left PGA", NULL, "Handsfree Left Playback"},
  653. {"HF Right PGA", NULL, "Handsfree Right Playback"},
  654. {"HF Left Driver", NULL, "HF Left PGA"},
  655. {"HF Right Driver", NULL, "HF Right PGA"},
  656. {"HFL", NULL, "HF Left Driver"},
  657. {"HFR", NULL, "HF Right Driver"},
  658. {"AUXL Playback", "Switch", "HF Left PGA"},
  659. {"AUXR Playback", "Switch", "HF Right PGA"},
  660. {"AUXL", NULL, "AUXL Playback"},
  661. {"AUXR", NULL, "AUXR Playback"},
  662. /* Vibrator paths */
  663. {"Vibra Left Playback", "Audio PDM", "VIBRA DAC"},
  664. {"Vibra Right Playback", "Audio PDM", "VIBRA DAC"},
  665. {"Vibra Left Driver", NULL, "Vibra Left Playback"},
  666. {"Vibra Right Driver", NULL, "Vibra Right Playback"},
  667. {"Vibra Left Driver", NULL, "Vibra Left Control"},
  668. {"Vibra Right Driver", NULL, "Vibra Right Control"},
  669. {"VIBRAL", NULL, "Vibra Left Driver"},
  670. {"VIBRAR", NULL, "Vibra Right Driver"},
  671. };
  672. static int twl6040_set_bias_level(struct snd_soc_codec *codec,
  673. enum snd_soc_bias_level level)
  674. {
  675. struct twl6040 *twl6040 = codec->control_data;
  676. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  677. int ret = 0;
  678. switch (level) {
  679. case SND_SOC_BIAS_ON:
  680. break;
  681. case SND_SOC_BIAS_PREPARE:
  682. break;
  683. case SND_SOC_BIAS_STANDBY:
  684. if (priv->codec_powered) {
  685. /* Select low power PLL in standby */
  686. ret = twl6040_set_pll(twl6040, TWL6040_SYSCLK_SEL_LPPLL,
  687. 32768, 19200000);
  688. break;
  689. }
  690. ret = twl6040_power(twl6040, 1);
  691. if (ret)
  692. break;
  693. priv->codec_powered = 1;
  694. /* Set external boost GPO */
  695. twl6040_write(codec, TWL6040_REG_GPOCTL, 0x02);
  696. break;
  697. case SND_SOC_BIAS_OFF:
  698. if (!priv->codec_powered)
  699. break;
  700. twl6040_power(twl6040, 0);
  701. priv->codec_powered = 0;
  702. break;
  703. }
  704. return ret;
  705. }
  706. static int twl6040_startup(struct snd_pcm_substream *substream,
  707. struct snd_soc_dai *dai)
  708. {
  709. struct snd_soc_codec *codec = dai->codec;
  710. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  711. snd_pcm_hw_constraint_list(substream->runtime, 0,
  712. SNDRV_PCM_HW_PARAM_RATE,
  713. &sysclk_constraints[priv->pll_power_mode]);
  714. return 0;
  715. }
  716. static int twl6040_hw_params(struct snd_pcm_substream *substream,
  717. struct snd_pcm_hw_params *params,
  718. struct snd_soc_dai *dai)
  719. {
  720. struct snd_soc_codec *codec = dai->codec;
  721. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  722. int rate;
  723. rate = params_rate(params);
  724. switch (rate) {
  725. case 11250:
  726. case 22500:
  727. case 44100:
  728. case 88200:
  729. /* These rates are not supported when HPPLL is in use */
  730. if (unlikely(priv->pll == TWL6040_SYSCLK_SEL_HPPLL)) {
  731. dev_err(codec->dev, "HPPLL does not support rate %d\n",
  732. rate);
  733. return -EINVAL;
  734. }
  735. priv->sysclk = 17640000;
  736. break;
  737. case 8000:
  738. case 16000:
  739. case 32000:
  740. case 48000:
  741. case 96000:
  742. priv->sysclk = 19200000;
  743. break;
  744. default:
  745. dev_err(codec->dev, "unsupported rate %d\n", rate);
  746. return -EINVAL;
  747. }
  748. return 0;
  749. }
  750. static int twl6040_prepare(struct snd_pcm_substream *substream,
  751. struct snd_soc_dai *dai)
  752. {
  753. struct snd_soc_codec *codec = dai->codec;
  754. struct twl6040 *twl6040 = codec->control_data;
  755. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  756. int ret;
  757. if (!priv->sysclk) {
  758. dev_err(codec->dev,
  759. "no mclk configured, call set_sysclk() on init\n");
  760. return -EINVAL;
  761. }
  762. ret = twl6040_set_pll(twl6040, priv->pll, priv->clk_in, priv->sysclk);
  763. if (ret) {
  764. dev_err(codec->dev, "Can not set PLL (%d)\n", ret);
  765. return -EPERM;
  766. }
  767. return 0;
  768. }
  769. static int twl6040_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  770. int clk_id, unsigned int freq, int dir)
  771. {
  772. struct snd_soc_codec *codec = codec_dai->codec;
  773. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  774. switch (clk_id) {
  775. case TWL6040_SYSCLK_SEL_LPPLL:
  776. case TWL6040_SYSCLK_SEL_HPPLL:
  777. priv->pll = clk_id;
  778. priv->clk_in = freq;
  779. break;
  780. default:
  781. dev_err(codec->dev, "unknown clk_id %d\n", clk_id);
  782. return -EINVAL;
  783. }
  784. return 0;
  785. }
  786. static void twl6040_mute_path(struct snd_soc_codec *codec, enum twl6040_dai_id id,
  787. int mute)
  788. {
  789. struct twl6040 *twl6040 = codec->control_data;
  790. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  791. int hslctl, hsrctl, earctl;
  792. int hflctl, hfrctl;
  793. switch (id) {
  794. case TWL6040_DAI_DL1:
  795. hslctl = twl6040_read(codec, TWL6040_REG_HSLCTL);
  796. hsrctl = twl6040_read(codec, TWL6040_REG_HSRCTL);
  797. earctl = twl6040_read(codec, TWL6040_REG_EARCTL);
  798. if (mute) {
  799. /* Power down drivers and DACs */
  800. earctl &= ~0x01;
  801. hslctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
  802. hsrctl &= ~(TWL6040_HSDRVENA | TWL6040_HSDACENA);
  803. }
  804. twl6040_reg_write(twl6040, TWL6040_REG_EARCTL, earctl);
  805. twl6040_reg_write(twl6040, TWL6040_REG_HSLCTL, hslctl);
  806. twl6040_reg_write(twl6040, TWL6040_REG_HSRCTL, hsrctl);
  807. priv->dl1_unmuted = !mute;
  808. break;
  809. case TWL6040_DAI_DL2:
  810. hflctl = twl6040_read(codec, TWL6040_REG_HFLCTL);
  811. hfrctl = twl6040_read(codec, TWL6040_REG_HFRCTL);
  812. if (mute) {
  813. /* Power down drivers and DACs */
  814. hflctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
  815. TWL6040_HFDRVENA | TWL6040_HFSWENA);
  816. hfrctl &= ~(TWL6040_HFDACENA | TWL6040_HFPGAENA |
  817. TWL6040_HFDRVENA | TWL6040_HFSWENA);
  818. }
  819. twl6040_reg_write(twl6040, TWL6040_REG_HFLCTL, hflctl);
  820. twl6040_reg_write(twl6040, TWL6040_REG_HFRCTL, hfrctl);
  821. priv->dl2_unmuted = !mute;
  822. break;
  823. default:
  824. break;
  825. }
  826. }
  827. static int twl6040_digital_mute(struct snd_soc_dai *dai, int mute)
  828. {
  829. switch (dai->id) {
  830. case TWL6040_DAI_LEGACY:
  831. twl6040_mute_path(dai->codec, TWL6040_DAI_DL1, mute);
  832. twl6040_mute_path(dai->codec, TWL6040_DAI_DL2, mute);
  833. break;
  834. case TWL6040_DAI_DL1:
  835. case TWL6040_DAI_DL2:
  836. twl6040_mute_path(dai->codec, dai->id, mute);
  837. break;
  838. default:
  839. break;
  840. }
  841. return 0;
  842. }
  843. static const struct snd_soc_dai_ops twl6040_dai_ops = {
  844. .startup = twl6040_startup,
  845. .hw_params = twl6040_hw_params,
  846. .prepare = twl6040_prepare,
  847. .set_sysclk = twl6040_set_dai_sysclk,
  848. .digital_mute = twl6040_digital_mute,
  849. };
  850. static struct snd_soc_dai_driver twl6040_dai[] = {
  851. {
  852. .name = "twl6040-legacy",
  853. .id = TWL6040_DAI_LEGACY,
  854. .playback = {
  855. .stream_name = "Legacy Playback",
  856. .channels_min = 1,
  857. .channels_max = 5,
  858. .rates = TWL6040_RATES,
  859. .formats = TWL6040_FORMATS,
  860. },
  861. .capture = {
  862. .stream_name = "Legacy Capture",
  863. .channels_min = 1,
  864. .channels_max = 2,
  865. .rates = TWL6040_RATES,
  866. .formats = TWL6040_FORMATS,
  867. },
  868. .ops = &twl6040_dai_ops,
  869. },
  870. {
  871. .name = "twl6040-ul",
  872. .id = TWL6040_DAI_UL,
  873. .capture = {
  874. .stream_name = "Capture",
  875. .channels_min = 1,
  876. .channels_max = 2,
  877. .rates = TWL6040_RATES,
  878. .formats = TWL6040_FORMATS,
  879. },
  880. .ops = &twl6040_dai_ops,
  881. },
  882. {
  883. .name = "twl6040-dl1",
  884. .id = TWL6040_DAI_DL1,
  885. .playback = {
  886. .stream_name = "Headset Playback",
  887. .channels_min = 1,
  888. .channels_max = 2,
  889. .rates = TWL6040_RATES,
  890. .formats = TWL6040_FORMATS,
  891. },
  892. .ops = &twl6040_dai_ops,
  893. },
  894. {
  895. .name = "twl6040-dl2",
  896. .id = TWL6040_DAI_DL2,
  897. .playback = {
  898. .stream_name = "Handsfree Playback",
  899. .channels_min = 1,
  900. .channels_max = 2,
  901. .rates = TWL6040_RATES,
  902. .formats = TWL6040_FORMATS,
  903. },
  904. .ops = &twl6040_dai_ops,
  905. },
  906. {
  907. .name = "twl6040-vib",
  908. .id = TWL6040_DAI_VIB,
  909. .playback = {
  910. .stream_name = "Vibra Playback",
  911. .channels_min = 1,
  912. .channels_max = 1,
  913. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  914. .formats = TWL6040_FORMATS,
  915. },
  916. .ops = &twl6040_dai_ops,
  917. },
  918. };
  919. static int twl6040_probe(struct snd_soc_codec *codec)
  920. {
  921. struct twl6040_data *priv;
  922. struct twl6040 *twl6040 = dev_get_drvdata(codec->dev->parent);
  923. struct platform_device *pdev = to_platform_device(codec->dev);
  924. int ret = 0;
  925. priv = devm_kzalloc(codec->dev, sizeof(*priv), GFP_KERNEL);
  926. if (priv == NULL)
  927. return -ENOMEM;
  928. snd_soc_codec_set_drvdata(codec, priv);
  929. priv->codec = codec;
  930. codec->control_data = twl6040;
  931. priv->plug_irq = platform_get_irq(pdev, 0);
  932. if (priv->plug_irq < 0) {
  933. dev_err(codec->dev, "invalid irq\n");
  934. return -EINVAL;
  935. }
  936. INIT_DELAYED_WORK(&priv->hs_jack.work, twl6040_accessory_work);
  937. mutex_init(&priv->mutex);
  938. ret = request_threaded_irq(priv->plug_irq, NULL,
  939. twl6040_audio_handler,
  940. IRQF_NO_SUSPEND | IRQF_ONESHOT,
  941. "twl6040_irq_plug", codec);
  942. if (ret) {
  943. dev_err(codec->dev, "PLUG IRQ request failed: %d\n", ret);
  944. return ret;
  945. }
  946. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_STANDBY);
  947. twl6040_init_chip(codec);
  948. return 0;
  949. }
  950. static int twl6040_remove(struct snd_soc_codec *codec)
  951. {
  952. struct twl6040_data *priv = snd_soc_codec_get_drvdata(codec);
  953. free_irq(priv->plug_irq, codec);
  954. return 0;
  955. }
  956. static struct snd_soc_codec_driver soc_codec_dev_twl6040 = {
  957. .probe = twl6040_probe,
  958. .remove = twl6040_remove,
  959. .read = twl6040_read,
  960. .write = twl6040_write,
  961. .set_bias_level = twl6040_set_bias_level,
  962. .suspend_bias_off = true,
  963. .ignore_pmdown_time = true,
  964. .component_driver = {
  965. .controls = twl6040_snd_controls,
  966. .num_controls = ARRAY_SIZE(twl6040_snd_controls),
  967. .dapm_widgets = twl6040_dapm_widgets,
  968. .num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets),
  969. .dapm_routes = intercon,
  970. .num_dapm_routes = ARRAY_SIZE(intercon),
  971. },
  972. };
  973. static int twl6040_codec_probe(struct platform_device *pdev)
  974. {
  975. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl6040,
  976. twl6040_dai, ARRAY_SIZE(twl6040_dai));
  977. }
  978. static int twl6040_codec_remove(struct platform_device *pdev)
  979. {
  980. snd_soc_unregister_codec(&pdev->dev);
  981. return 0;
  982. }
  983. static struct platform_driver twl6040_codec_driver = {
  984. .driver = {
  985. .name = "twl6040-codec",
  986. },
  987. .probe = twl6040_codec_probe,
  988. .remove = twl6040_codec_remove,
  989. };
  990. module_platform_driver(twl6040_codec_driver);
  991. MODULE_DESCRIPTION("ASoC TWL6040 codec driver");
  992. MODULE_AUTHOR("Misael Lopez Cruz");
  993. MODULE_LICENSE("GPL");