twl6040.c 31 KB

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