tlv320dac33.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /*
  2. * ALSA SoC Texas Instruments TLV320DAC33 codec driver
  3. *
  4. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  5. *
  6. * Copyright: (C) 2009 Nokia Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/pm.h>
  28. #include <linux/i2c.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/gpio.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/slab.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/pcm_params.h>
  36. #include <sound/soc.h>
  37. #include <sound/initval.h>
  38. #include <sound/tlv.h>
  39. #include <sound/tlv320dac33-plat.h>
  40. #include "tlv320dac33.h"
  41. /*
  42. * The internal FIFO is 24576 bytes long
  43. * It can be configured to hold 16bit or 24bit samples
  44. * In 16bit configuration the FIFO can hold 6144 stereo samples
  45. * In 24bit configuration the FIFO can hold 4096 stereo samples
  46. */
  47. #define DAC33_FIFO_SIZE_16BIT 6144
  48. #define DAC33_FIFO_SIZE_24BIT 4096
  49. #define DAC33_MODE7_MARGIN 10 /* Safety margin for FIFO in Mode7 */
  50. #define BURST_BASEFREQ_HZ 49152000
  51. #define SAMPLES_TO_US(rate, samples) \
  52. (1000000000 / (((rate) * 1000) / (samples)))
  53. #define US_TO_SAMPLES(rate, us) \
  54. ((rate) / (1000000 / ((us) < 1000000 ? (us) : 1000000)))
  55. #define UTHR_FROM_PERIOD_SIZE(samples, playrate, burstrate) \
  56. (((samples)*5000) / (((burstrate)*5000) / ((burstrate) - (playrate))))
  57. static void dac33_calculate_times(struct snd_pcm_substream *substream,
  58. struct snd_soc_codec *codec);
  59. static int dac33_prepare_chip(struct snd_pcm_substream *substream,
  60. struct snd_soc_codec *codec);
  61. enum dac33_state {
  62. DAC33_IDLE = 0,
  63. DAC33_PREFILL,
  64. DAC33_PLAYBACK,
  65. DAC33_FLUSH,
  66. };
  67. enum dac33_fifo_modes {
  68. DAC33_FIFO_BYPASS = 0,
  69. DAC33_FIFO_MODE1,
  70. DAC33_FIFO_MODE7,
  71. DAC33_FIFO_LAST_MODE,
  72. };
  73. #define DAC33_NUM_SUPPLIES 3
  74. static const char *dac33_supply_names[DAC33_NUM_SUPPLIES] = {
  75. "AVDD",
  76. "DVDD",
  77. "IOVDD",
  78. };
  79. struct tlv320dac33_priv {
  80. struct mutex mutex;
  81. struct work_struct work;
  82. struct snd_soc_codec *codec;
  83. struct regulator_bulk_data supplies[DAC33_NUM_SUPPLIES];
  84. struct snd_pcm_substream *substream;
  85. int power_gpio;
  86. int chip_power;
  87. int irq;
  88. unsigned int refclk;
  89. unsigned int alarm_threshold; /* set to be half of LATENCY_TIME_MS */
  90. enum dac33_fifo_modes fifo_mode;/* FIFO mode selection */
  91. unsigned int fifo_size; /* Size of the FIFO in samples */
  92. unsigned int nsample; /* burst read amount from host */
  93. int mode1_latency; /* latency caused by the i2c writes in
  94. * us */
  95. u8 burst_bclkdiv; /* BCLK divider value in burst mode */
  96. unsigned int burst_rate; /* Interface speed in Burst modes */
  97. int keep_bclk; /* Keep the BCLK continuously running
  98. * in FIFO modes */
  99. spinlock_t lock;
  100. unsigned long long t_stamp1; /* Time stamp for FIFO modes to */
  101. unsigned long long t_stamp2; /* calculate the FIFO caused delay */
  102. unsigned int mode1_us_burst; /* Time to burst read n number of
  103. * samples */
  104. unsigned int mode7_us_to_lthr; /* Time to reach lthr from uthr */
  105. unsigned int uthr;
  106. enum dac33_state state;
  107. void *control_data;
  108. };
  109. static const u8 dac33_reg[DAC33_CACHEREGNUM] = {
  110. 0x00, 0x00, 0x00, 0x00, /* 0x00 - 0x03 */
  111. 0x00, 0x00, 0x00, 0x00, /* 0x04 - 0x07 */
  112. 0x00, 0x00, 0x00, 0x00, /* 0x08 - 0x0b */
  113. 0x00, 0x00, 0x00, 0x00, /* 0x0c - 0x0f */
  114. 0x00, 0x00, 0x00, 0x00, /* 0x10 - 0x13 */
  115. 0x00, 0x00, 0x00, 0x00, /* 0x14 - 0x17 */
  116. 0x00, 0x00, 0x00, 0x00, /* 0x18 - 0x1b */
  117. 0x00, 0x00, 0x00, 0x00, /* 0x1c - 0x1f */
  118. 0x00, 0x00, 0x00, 0x00, /* 0x20 - 0x23 */
  119. 0x00, 0x00, 0x00, 0x00, /* 0x24 - 0x27 */
  120. 0x00, 0x00, 0x00, 0x00, /* 0x28 - 0x2b */
  121. 0x00, 0x00, 0x00, 0x80, /* 0x2c - 0x2f */
  122. 0x80, 0x00, 0x00, 0x00, /* 0x30 - 0x33 */
  123. 0x00, 0x00, 0x00, 0x00, /* 0x34 - 0x37 */
  124. 0x00, 0x00, /* 0x38 - 0x39 */
  125. /* Registers 0x3a - 0x3f are reserved */
  126. 0x00, 0x00, /* 0x3a - 0x3b */
  127. 0x00, 0x00, 0x00, 0x00, /* 0x3c - 0x3f */
  128. 0x00, 0x00, 0x00, 0x00, /* 0x40 - 0x43 */
  129. 0x00, 0x80, /* 0x44 - 0x45 */
  130. /* Registers 0x46 - 0x47 are reserved */
  131. 0x80, 0x80, /* 0x46 - 0x47 */
  132. 0x80, 0x00, 0x00, /* 0x48 - 0x4a */
  133. /* Registers 0x4b - 0x7c are reserved */
  134. 0x00, /* 0x4b */
  135. 0x00, 0x00, 0x00, 0x00, /* 0x4c - 0x4f */
  136. 0x00, 0x00, 0x00, 0x00, /* 0x50 - 0x53 */
  137. 0x00, 0x00, 0x00, 0x00, /* 0x54 - 0x57 */
  138. 0x00, 0x00, 0x00, 0x00, /* 0x58 - 0x5b */
  139. 0x00, 0x00, 0x00, 0x00, /* 0x5c - 0x5f */
  140. 0x00, 0x00, 0x00, 0x00, /* 0x60 - 0x63 */
  141. 0x00, 0x00, 0x00, 0x00, /* 0x64 - 0x67 */
  142. 0x00, 0x00, 0x00, 0x00, /* 0x68 - 0x6b */
  143. 0x00, 0x00, 0x00, 0x00, /* 0x6c - 0x6f */
  144. 0x00, 0x00, 0x00, 0x00, /* 0x70 - 0x73 */
  145. 0x00, 0x00, 0x00, 0x00, /* 0x74 - 0x77 */
  146. 0x00, 0x00, 0x00, 0x00, /* 0x78 - 0x7b */
  147. 0x00, /* 0x7c */
  148. 0xda, 0x33, 0x03, /* 0x7d - 0x7f */
  149. };
  150. /* Register read and write */
  151. static inline unsigned int dac33_read_reg_cache(struct snd_soc_codec *codec,
  152. unsigned reg)
  153. {
  154. u8 *cache = codec->reg_cache;
  155. if (reg >= DAC33_CACHEREGNUM)
  156. return 0;
  157. return cache[reg];
  158. }
  159. static inline void dac33_write_reg_cache(struct snd_soc_codec *codec,
  160. u8 reg, u8 value)
  161. {
  162. u8 *cache = codec->reg_cache;
  163. if (reg >= DAC33_CACHEREGNUM)
  164. return;
  165. cache[reg] = value;
  166. }
  167. static int dac33_read(struct snd_soc_codec *codec, unsigned int reg,
  168. u8 *value)
  169. {
  170. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  171. int val, ret = 0;
  172. *value = reg & 0xff;
  173. /* If powered off, return the cached value */
  174. if (dac33->chip_power) {
  175. val = i2c_smbus_read_byte_data(codec->control_data, value[0]);
  176. if (val < 0) {
  177. dev_err(codec->dev, "Read failed (%d)\n", val);
  178. value[0] = dac33_read_reg_cache(codec, reg);
  179. ret = val;
  180. } else {
  181. value[0] = val;
  182. dac33_write_reg_cache(codec, reg, val);
  183. }
  184. } else {
  185. value[0] = dac33_read_reg_cache(codec, reg);
  186. }
  187. return ret;
  188. }
  189. static int dac33_write(struct snd_soc_codec *codec, unsigned int reg,
  190. unsigned int value)
  191. {
  192. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  193. u8 data[2];
  194. int ret = 0;
  195. /*
  196. * data is
  197. * D15..D8 dac33 register offset
  198. * D7...D0 register data
  199. */
  200. data[0] = reg & 0xff;
  201. data[1] = value & 0xff;
  202. dac33_write_reg_cache(codec, data[0], data[1]);
  203. if (dac33->chip_power) {
  204. ret = codec->hw_write(codec->control_data, data, 2);
  205. if (ret != 2)
  206. dev_err(codec->dev, "Write failed (%d)\n", ret);
  207. else
  208. ret = 0;
  209. }
  210. return ret;
  211. }
  212. static int dac33_write_locked(struct snd_soc_codec *codec, unsigned int reg,
  213. unsigned int value)
  214. {
  215. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  216. int ret;
  217. mutex_lock(&dac33->mutex);
  218. ret = dac33_write(codec, reg, value);
  219. mutex_unlock(&dac33->mutex);
  220. return ret;
  221. }
  222. #define DAC33_I2C_ADDR_AUTOINC 0x80
  223. static int dac33_write16(struct snd_soc_codec *codec, unsigned int reg,
  224. unsigned int value)
  225. {
  226. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  227. u8 data[3];
  228. int ret = 0;
  229. /*
  230. * data is
  231. * D23..D16 dac33 register offset
  232. * D15..D8 register data MSB
  233. * D7...D0 register data LSB
  234. */
  235. data[0] = reg & 0xff;
  236. data[1] = (value >> 8) & 0xff;
  237. data[2] = value & 0xff;
  238. dac33_write_reg_cache(codec, data[0], data[1]);
  239. dac33_write_reg_cache(codec, data[0] + 1, data[2]);
  240. if (dac33->chip_power) {
  241. /* We need to set autoincrement mode for 16 bit writes */
  242. data[0] |= DAC33_I2C_ADDR_AUTOINC;
  243. ret = codec->hw_write(codec->control_data, data, 3);
  244. if (ret != 3)
  245. dev_err(codec->dev, "Write failed (%d)\n", ret);
  246. else
  247. ret = 0;
  248. }
  249. return ret;
  250. }
  251. static void dac33_init_chip(struct snd_soc_codec *codec)
  252. {
  253. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  254. if (unlikely(!dac33->chip_power))
  255. return;
  256. /* A : DAC sample rate Fsref/1.5 */
  257. dac33_write(codec, DAC33_DAC_CTRL_A, DAC33_DACRATE(0));
  258. /* B : DAC src=normal, not muted */
  259. dac33_write(codec, DAC33_DAC_CTRL_B, DAC33_DACSRCR_RIGHT |
  260. DAC33_DACSRCL_LEFT);
  261. /* C : (defaults) */
  262. dac33_write(codec, DAC33_DAC_CTRL_C, 0x00);
  263. /* 73 : volume soft stepping control,
  264. clock source = internal osc (?) */
  265. dac33_write(codec, DAC33_ANA_VOL_SOFT_STEP_CTRL, DAC33_VOLCLKEN);
  266. /* Restore only selected registers (gains mostly) */
  267. dac33_write(codec, DAC33_LDAC_DIG_VOL_CTRL,
  268. dac33_read_reg_cache(codec, DAC33_LDAC_DIG_VOL_CTRL));
  269. dac33_write(codec, DAC33_RDAC_DIG_VOL_CTRL,
  270. dac33_read_reg_cache(codec, DAC33_RDAC_DIG_VOL_CTRL));
  271. dac33_write(codec, DAC33_LINEL_TO_LLO_VOL,
  272. dac33_read_reg_cache(codec, DAC33_LINEL_TO_LLO_VOL));
  273. dac33_write(codec, DAC33_LINER_TO_RLO_VOL,
  274. dac33_read_reg_cache(codec, DAC33_LINER_TO_RLO_VOL));
  275. dac33_write(codec, DAC33_OUT_AMP_CTRL,
  276. dac33_read_reg_cache(codec, DAC33_OUT_AMP_CTRL));
  277. dac33_write(codec, DAC33_LDAC_PWR_CTRL,
  278. dac33_read_reg_cache(codec, DAC33_LDAC_PWR_CTRL));
  279. dac33_write(codec, DAC33_RDAC_PWR_CTRL,
  280. dac33_read_reg_cache(codec, DAC33_RDAC_PWR_CTRL));
  281. }
  282. static inline int dac33_read_id(struct snd_soc_codec *codec)
  283. {
  284. int i, ret = 0;
  285. u8 reg;
  286. for (i = 0; i < 3; i++) {
  287. ret = dac33_read(codec, DAC33_DEVICE_ID_MSB + i, &reg);
  288. if (ret < 0)
  289. break;
  290. }
  291. return ret;
  292. }
  293. static inline void dac33_soft_power(struct snd_soc_codec *codec, int power)
  294. {
  295. u8 reg;
  296. reg = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
  297. if (power)
  298. reg |= DAC33_PDNALLB;
  299. else
  300. reg &= ~(DAC33_PDNALLB | DAC33_OSCPDNB |
  301. DAC33_DACRPDNB | DAC33_DACLPDNB);
  302. dac33_write(codec, DAC33_PWR_CTRL, reg);
  303. }
  304. static inline void dac33_disable_digital(struct snd_soc_codec *codec)
  305. {
  306. u8 reg;
  307. /* Stop the DAI clock */
  308. reg = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
  309. reg &= ~DAC33_BCLKON;
  310. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_B, reg);
  311. /* Power down the Oscillator, and DACs */
  312. reg = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
  313. reg &= ~(DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB);
  314. dac33_write(codec, DAC33_PWR_CTRL, reg);
  315. }
  316. static int dac33_hard_power(struct snd_soc_codec *codec, int power)
  317. {
  318. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  319. int ret = 0;
  320. mutex_lock(&dac33->mutex);
  321. /* Safety check */
  322. if (unlikely(power == dac33->chip_power)) {
  323. dev_dbg(codec->dev, "Trying to set the same power state: %s\n",
  324. power ? "ON" : "OFF");
  325. goto exit;
  326. }
  327. if (power) {
  328. ret = regulator_bulk_enable(ARRAY_SIZE(dac33->supplies),
  329. dac33->supplies);
  330. if (ret != 0) {
  331. dev_err(codec->dev,
  332. "Failed to enable supplies: %d\n", ret);
  333. goto exit;
  334. }
  335. if (dac33->power_gpio >= 0)
  336. gpio_set_value(dac33->power_gpio, 1);
  337. dac33->chip_power = 1;
  338. } else {
  339. dac33_soft_power(codec, 0);
  340. if (dac33->power_gpio >= 0)
  341. gpio_set_value(dac33->power_gpio, 0);
  342. ret = regulator_bulk_disable(ARRAY_SIZE(dac33->supplies),
  343. dac33->supplies);
  344. if (ret != 0) {
  345. dev_err(codec->dev,
  346. "Failed to disable supplies: %d\n", ret);
  347. goto exit;
  348. }
  349. dac33->chip_power = 0;
  350. }
  351. exit:
  352. mutex_unlock(&dac33->mutex);
  353. return ret;
  354. }
  355. static int dac33_playback_event(struct snd_soc_dapm_widget *w,
  356. struct snd_kcontrol *kcontrol, int event)
  357. {
  358. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  359. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  360. switch (event) {
  361. case SND_SOC_DAPM_PRE_PMU:
  362. if (likely(dac33->substream)) {
  363. dac33_calculate_times(dac33->substream, codec);
  364. dac33_prepare_chip(dac33->substream, codec);
  365. }
  366. break;
  367. case SND_SOC_DAPM_POST_PMD:
  368. dac33_disable_digital(codec);
  369. break;
  370. }
  371. return 0;
  372. }
  373. static int dac33_get_fifo_mode(struct snd_kcontrol *kcontrol,
  374. struct snd_ctl_elem_value *ucontrol)
  375. {
  376. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  377. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  378. ucontrol->value.enumerated.item[0] = dac33->fifo_mode;
  379. return 0;
  380. }
  381. static int dac33_set_fifo_mode(struct snd_kcontrol *kcontrol,
  382. struct snd_ctl_elem_value *ucontrol)
  383. {
  384. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  385. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  386. int ret = 0;
  387. if (dac33->fifo_mode == ucontrol->value.enumerated.item[0])
  388. return 0;
  389. /* Do not allow changes while stream is running*/
  390. if (snd_soc_codec_is_active(codec))
  391. return -EPERM;
  392. if (ucontrol->value.enumerated.item[0] >= DAC33_FIFO_LAST_MODE)
  393. ret = -EINVAL;
  394. else
  395. dac33->fifo_mode = ucontrol->value.enumerated.item[0];
  396. return ret;
  397. }
  398. /* Codec operation modes */
  399. static const char *dac33_fifo_mode_texts[] = {
  400. "Bypass", "Mode 1", "Mode 7"
  401. };
  402. static SOC_ENUM_SINGLE_EXT_DECL(dac33_fifo_mode_enum, dac33_fifo_mode_texts);
  403. /* L/R Line Output Gain */
  404. static const char *lr_lineout_gain_texts[] = {
  405. "Line -12dB DAC 0dB", "Line -6dB DAC 6dB",
  406. "Line 0dB DAC 12dB", "Line 6dB DAC 18dB",
  407. };
  408. static SOC_ENUM_SINGLE_DECL(l_lineout_gain_enum,
  409. DAC33_LDAC_PWR_CTRL, 0,
  410. lr_lineout_gain_texts);
  411. static SOC_ENUM_SINGLE_DECL(r_lineout_gain_enum,
  412. DAC33_RDAC_PWR_CTRL, 0,
  413. lr_lineout_gain_texts);
  414. /*
  415. * DACL/R digital volume control:
  416. * from 0 dB to -63.5 in 0.5 dB steps
  417. * Need to be inverted later on:
  418. * 0x00 == 0 dB
  419. * 0x7f == -63.5 dB
  420. */
  421. static DECLARE_TLV_DB_SCALE(dac_digivol_tlv, -6350, 50, 0);
  422. static const struct snd_kcontrol_new dac33_snd_controls[] = {
  423. SOC_DOUBLE_R_TLV("DAC Digital Playback Volume",
  424. DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL,
  425. 0, 0x7f, 1, dac_digivol_tlv),
  426. SOC_DOUBLE_R("DAC Digital Playback Switch",
  427. DAC33_LDAC_DIG_VOL_CTRL, DAC33_RDAC_DIG_VOL_CTRL, 7, 1, 1),
  428. SOC_DOUBLE_R("Line to Line Out Volume",
  429. DAC33_LINEL_TO_LLO_VOL, DAC33_LINER_TO_RLO_VOL, 0, 127, 1),
  430. SOC_ENUM("Left Line Output Gain", l_lineout_gain_enum),
  431. SOC_ENUM("Right Line Output Gain", r_lineout_gain_enum),
  432. };
  433. static const struct snd_kcontrol_new dac33_mode_snd_controls[] = {
  434. SOC_ENUM_EXT("FIFO Mode", dac33_fifo_mode_enum,
  435. dac33_get_fifo_mode, dac33_set_fifo_mode),
  436. };
  437. /* Analog bypass */
  438. static const struct snd_kcontrol_new dac33_dapm_abypassl_control =
  439. SOC_DAPM_SINGLE("Switch", DAC33_LINEL_TO_LLO_VOL, 7, 1, 1);
  440. static const struct snd_kcontrol_new dac33_dapm_abypassr_control =
  441. SOC_DAPM_SINGLE("Switch", DAC33_LINER_TO_RLO_VOL, 7, 1, 1);
  442. /* LOP L/R invert selection */
  443. static const char *dac33_lr_lom_texts[] = {"DAC", "LOP"};
  444. static SOC_ENUM_SINGLE_DECL(dac33_left_lom_enum,
  445. DAC33_OUT_AMP_CTRL, 3,
  446. dac33_lr_lom_texts);
  447. static const struct snd_kcontrol_new dac33_dapm_left_lom_control =
  448. SOC_DAPM_ENUM("Route", dac33_left_lom_enum);
  449. static SOC_ENUM_SINGLE_DECL(dac33_right_lom_enum,
  450. DAC33_OUT_AMP_CTRL, 2,
  451. dac33_lr_lom_texts);
  452. static const struct snd_kcontrol_new dac33_dapm_right_lom_control =
  453. SOC_DAPM_ENUM("Route", dac33_right_lom_enum);
  454. static const struct snd_soc_dapm_widget dac33_dapm_widgets[] = {
  455. SND_SOC_DAPM_OUTPUT("LEFT_LO"),
  456. SND_SOC_DAPM_OUTPUT("RIGHT_LO"),
  457. SND_SOC_DAPM_INPUT("LINEL"),
  458. SND_SOC_DAPM_INPUT("LINER"),
  459. SND_SOC_DAPM_DAC("DACL", "Left Playback", SND_SOC_NOPM, 0, 0),
  460. SND_SOC_DAPM_DAC("DACR", "Right Playback", SND_SOC_NOPM, 0, 0),
  461. /* Analog bypass */
  462. SND_SOC_DAPM_SWITCH("Analog Left Bypass", SND_SOC_NOPM, 0, 0,
  463. &dac33_dapm_abypassl_control),
  464. SND_SOC_DAPM_SWITCH("Analog Right Bypass", SND_SOC_NOPM, 0, 0,
  465. &dac33_dapm_abypassr_control),
  466. SND_SOC_DAPM_MUX("Left LOM Inverted From", SND_SOC_NOPM, 0, 0,
  467. &dac33_dapm_left_lom_control),
  468. SND_SOC_DAPM_MUX("Right LOM Inverted From", SND_SOC_NOPM, 0, 0,
  469. &dac33_dapm_right_lom_control),
  470. /*
  471. * For DAPM path, when only the anlog bypass path is enabled, and the
  472. * LOP inverted from the corresponding DAC side.
  473. * This is needed, so we can attach the DAC power supply in this case.
  474. */
  475. SND_SOC_DAPM_PGA("Left Bypass PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  476. SND_SOC_DAPM_PGA("Right Bypass PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
  477. SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Left Amplifier",
  478. DAC33_OUT_AMP_PWR_CTRL, 6, 3, 3, 0),
  479. SND_SOC_DAPM_REG(snd_soc_dapm_mixer, "Output Right Amplifier",
  480. DAC33_OUT_AMP_PWR_CTRL, 4, 3, 3, 0),
  481. SND_SOC_DAPM_SUPPLY("Left DAC Power",
  482. DAC33_LDAC_PWR_CTRL, 2, 0, NULL, 0),
  483. SND_SOC_DAPM_SUPPLY("Right DAC Power",
  484. DAC33_RDAC_PWR_CTRL, 2, 0, NULL, 0),
  485. SND_SOC_DAPM_SUPPLY("Codec Power",
  486. DAC33_PWR_CTRL, 4, 0, NULL, 0),
  487. SND_SOC_DAPM_PRE("Pre Playback", dac33_playback_event),
  488. SND_SOC_DAPM_POST("Post Playback", dac33_playback_event),
  489. };
  490. static const struct snd_soc_dapm_route audio_map[] = {
  491. /* Analog bypass */
  492. {"Analog Left Bypass", "Switch", "LINEL"},
  493. {"Analog Right Bypass", "Switch", "LINER"},
  494. {"Output Left Amplifier", NULL, "DACL"},
  495. {"Output Right Amplifier", NULL, "DACR"},
  496. {"Left Bypass PGA", NULL, "Analog Left Bypass"},
  497. {"Right Bypass PGA", NULL, "Analog Right Bypass"},
  498. {"Left LOM Inverted From", "DAC", "Left Bypass PGA"},
  499. {"Right LOM Inverted From", "DAC", "Right Bypass PGA"},
  500. {"Left LOM Inverted From", "LOP", "Analog Left Bypass"},
  501. {"Right LOM Inverted From", "LOP", "Analog Right Bypass"},
  502. {"Output Left Amplifier", NULL, "Left LOM Inverted From"},
  503. {"Output Right Amplifier", NULL, "Right LOM Inverted From"},
  504. {"DACL", NULL, "Left DAC Power"},
  505. {"DACR", NULL, "Right DAC Power"},
  506. {"Left Bypass PGA", NULL, "Left DAC Power"},
  507. {"Right Bypass PGA", NULL, "Right DAC Power"},
  508. /* output */
  509. {"LEFT_LO", NULL, "Output Left Amplifier"},
  510. {"RIGHT_LO", NULL, "Output Right Amplifier"},
  511. {"LEFT_LO", NULL, "Codec Power"},
  512. {"RIGHT_LO", NULL, "Codec Power"},
  513. };
  514. static int dac33_set_bias_level(struct snd_soc_codec *codec,
  515. enum snd_soc_bias_level level)
  516. {
  517. int ret;
  518. switch (level) {
  519. case SND_SOC_BIAS_ON:
  520. break;
  521. case SND_SOC_BIAS_PREPARE:
  522. break;
  523. case SND_SOC_BIAS_STANDBY:
  524. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  525. /* Coming from OFF, switch on the codec */
  526. ret = dac33_hard_power(codec, 1);
  527. if (ret != 0)
  528. return ret;
  529. dac33_init_chip(codec);
  530. }
  531. break;
  532. case SND_SOC_BIAS_OFF:
  533. /* Do not power off, when the codec is already off */
  534. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF)
  535. return 0;
  536. ret = dac33_hard_power(codec, 0);
  537. if (ret != 0)
  538. return ret;
  539. break;
  540. }
  541. return 0;
  542. }
  543. static inline void dac33_prefill_handler(struct tlv320dac33_priv *dac33)
  544. {
  545. struct snd_soc_codec *codec = dac33->codec;
  546. unsigned int delay;
  547. unsigned long flags;
  548. switch (dac33->fifo_mode) {
  549. case DAC33_FIFO_MODE1:
  550. dac33_write16(codec, DAC33_NSAMPLE_MSB,
  551. DAC33_THRREG(dac33->nsample));
  552. /* Take the timestamps */
  553. spin_lock_irqsave(&dac33->lock, flags);
  554. dac33->t_stamp2 = ktime_to_us(ktime_get());
  555. dac33->t_stamp1 = dac33->t_stamp2;
  556. spin_unlock_irqrestore(&dac33->lock, flags);
  557. dac33_write16(codec, DAC33_PREFILL_MSB,
  558. DAC33_THRREG(dac33->alarm_threshold));
  559. /* Enable Alarm Threshold IRQ with a delay */
  560. delay = SAMPLES_TO_US(dac33->burst_rate,
  561. dac33->alarm_threshold) + 1000;
  562. usleep_range(delay, delay + 500);
  563. dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MAT);
  564. break;
  565. case DAC33_FIFO_MODE7:
  566. /* Take the timestamp */
  567. spin_lock_irqsave(&dac33->lock, flags);
  568. dac33->t_stamp1 = ktime_to_us(ktime_get());
  569. /* Move back the timestamp with drain time */
  570. dac33->t_stamp1 -= dac33->mode7_us_to_lthr;
  571. spin_unlock_irqrestore(&dac33->lock, flags);
  572. dac33_write16(codec, DAC33_PREFILL_MSB,
  573. DAC33_THRREG(DAC33_MODE7_MARGIN));
  574. /* Enable Upper Threshold IRQ */
  575. dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MUT);
  576. break;
  577. default:
  578. dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
  579. dac33->fifo_mode);
  580. break;
  581. }
  582. }
  583. static inline void dac33_playback_handler(struct tlv320dac33_priv *dac33)
  584. {
  585. struct snd_soc_codec *codec = dac33->codec;
  586. unsigned long flags;
  587. switch (dac33->fifo_mode) {
  588. case DAC33_FIFO_MODE1:
  589. /* Take the timestamp */
  590. spin_lock_irqsave(&dac33->lock, flags);
  591. dac33->t_stamp2 = ktime_to_us(ktime_get());
  592. spin_unlock_irqrestore(&dac33->lock, flags);
  593. dac33_write16(codec, DAC33_NSAMPLE_MSB,
  594. DAC33_THRREG(dac33->nsample));
  595. break;
  596. case DAC33_FIFO_MODE7:
  597. /* At the moment we are not using interrupts in mode7 */
  598. break;
  599. default:
  600. dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
  601. dac33->fifo_mode);
  602. break;
  603. }
  604. }
  605. static void dac33_work(struct work_struct *work)
  606. {
  607. struct snd_soc_codec *codec;
  608. struct tlv320dac33_priv *dac33;
  609. u8 reg;
  610. dac33 = container_of(work, struct tlv320dac33_priv, work);
  611. codec = dac33->codec;
  612. mutex_lock(&dac33->mutex);
  613. switch (dac33->state) {
  614. case DAC33_PREFILL:
  615. dac33->state = DAC33_PLAYBACK;
  616. dac33_prefill_handler(dac33);
  617. break;
  618. case DAC33_PLAYBACK:
  619. dac33_playback_handler(dac33);
  620. break;
  621. case DAC33_IDLE:
  622. break;
  623. case DAC33_FLUSH:
  624. dac33->state = DAC33_IDLE;
  625. /* Mask all interrupts from dac33 */
  626. dac33_write(codec, DAC33_FIFO_IRQ_MASK, 0);
  627. /* flush fifo */
  628. reg = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
  629. reg |= DAC33_FIFOFLUSH;
  630. dac33_write(codec, DAC33_FIFO_CTRL_A, reg);
  631. break;
  632. }
  633. mutex_unlock(&dac33->mutex);
  634. }
  635. static irqreturn_t dac33_interrupt_handler(int irq, void *dev)
  636. {
  637. struct snd_soc_codec *codec = dev;
  638. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  639. unsigned long flags;
  640. spin_lock_irqsave(&dac33->lock, flags);
  641. dac33->t_stamp1 = ktime_to_us(ktime_get());
  642. spin_unlock_irqrestore(&dac33->lock, flags);
  643. /* Do not schedule the workqueue in Mode7 */
  644. if (dac33->fifo_mode != DAC33_FIFO_MODE7)
  645. schedule_work(&dac33->work);
  646. return IRQ_HANDLED;
  647. }
  648. static void dac33_oscwait(struct snd_soc_codec *codec)
  649. {
  650. int timeout = 60;
  651. u8 reg;
  652. do {
  653. usleep_range(1000, 2000);
  654. dac33_read(codec, DAC33_INT_OSC_STATUS, &reg);
  655. } while (((reg & 0x03) != DAC33_OSCSTATUS_NORMAL) && timeout--);
  656. if ((reg & 0x03) != DAC33_OSCSTATUS_NORMAL)
  657. dev_err(codec->dev,
  658. "internal oscillator calibration failed\n");
  659. }
  660. static int dac33_startup(struct snd_pcm_substream *substream,
  661. struct snd_soc_dai *dai)
  662. {
  663. struct snd_soc_codec *codec = dai->codec;
  664. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  665. /* Stream started, save the substream pointer */
  666. dac33->substream = substream;
  667. return 0;
  668. }
  669. static void dac33_shutdown(struct snd_pcm_substream *substream,
  670. struct snd_soc_dai *dai)
  671. {
  672. struct snd_soc_codec *codec = dai->codec;
  673. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  674. dac33->substream = NULL;
  675. }
  676. #define CALC_BURST_RATE(bclkdiv, bclk_per_sample) \
  677. (BURST_BASEFREQ_HZ / bclkdiv / bclk_per_sample)
  678. static int dac33_hw_params(struct snd_pcm_substream *substream,
  679. struct snd_pcm_hw_params *params,
  680. struct snd_soc_dai *dai)
  681. {
  682. struct snd_soc_codec *codec = dai->codec;
  683. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  684. /* Check parameters for validity */
  685. switch (params_rate(params)) {
  686. case 44100:
  687. case 48000:
  688. break;
  689. default:
  690. dev_err(codec->dev, "unsupported rate %d\n",
  691. params_rate(params));
  692. return -EINVAL;
  693. }
  694. switch (params_width(params)) {
  695. case 16:
  696. dac33->fifo_size = DAC33_FIFO_SIZE_16BIT;
  697. dac33->burst_rate = CALC_BURST_RATE(dac33->burst_bclkdiv, 32);
  698. break;
  699. case 32:
  700. dac33->fifo_size = DAC33_FIFO_SIZE_24BIT;
  701. dac33->burst_rate = CALC_BURST_RATE(dac33->burst_bclkdiv, 64);
  702. break;
  703. default:
  704. dev_err(codec->dev, "unsupported width %d\n",
  705. params_width(params));
  706. return -EINVAL;
  707. }
  708. return 0;
  709. }
  710. #define CALC_OSCSET(rate, refclk) ( \
  711. ((((rate * 10000) / refclk) * 4096) + 7000) / 10000)
  712. #define CALC_RATIOSET(rate, refclk) ( \
  713. ((((refclk * 100000) / rate) * 16384) + 50000) / 100000)
  714. /*
  715. * tlv320dac33 is strict on the sequence of the register writes, if the register
  716. * writes happens in different order, than dac33 might end up in unknown state.
  717. * Use the known, working sequence of register writes to initialize the dac33.
  718. */
  719. static int dac33_prepare_chip(struct snd_pcm_substream *substream,
  720. struct snd_soc_codec *codec)
  721. {
  722. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  723. unsigned int oscset, ratioset, pwr_ctrl, reg_tmp;
  724. u8 aictrl_a, aictrl_b, fifoctrl_a;
  725. switch (substream->runtime->rate) {
  726. case 44100:
  727. case 48000:
  728. oscset = CALC_OSCSET(substream->runtime->rate, dac33->refclk);
  729. ratioset = CALC_RATIOSET(substream->runtime->rate,
  730. dac33->refclk);
  731. break;
  732. default:
  733. dev_err(codec->dev, "unsupported rate %d\n",
  734. substream->runtime->rate);
  735. return -EINVAL;
  736. }
  737. aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
  738. aictrl_a &= ~(DAC33_NCYCL_MASK | DAC33_WLEN_MASK);
  739. /* Read FIFO control A, and clear FIFO flush bit */
  740. fifoctrl_a = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
  741. fifoctrl_a &= ~DAC33_FIFOFLUSH;
  742. fifoctrl_a &= ~DAC33_WIDTH;
  743. switch (substream->runtime->format) {
  744. case SNDRV_PCM_FORMAT_S16_LE:
  745. aictrl_a |= (DAC33_NCYCL_16 | DAC33_WLEN_16);
  746. fifoctrl_a |= DAC33_WIDTH;
  747. break;
  748. case SNDRV_PCM_FORMAT_S32_LE:
  749. aictrl_a |= (DAC33_NCYCL_32 | DAC33_WLEN_24);
  750. break;
  751. default:
  752. dev_err(codec->dev, "unsupported format %d\n",
  753. substream->runtime->format);
  754. return -EINVAL;
  755. }
  756. mutex_lock(&dac33->mutex);
  757. if (!dac33->chip_power) {
  758. /*
  759. * Chip is not powered yet.
  760. * Do the init in the dac33_set_bias_level later.
  761. */
  762. mutex_unlock(&dac33->mutex);
  763. return 0;
  764. }
  765. dac33_soft_power(codec, 0);
  766. dac33_soft_power(codec, 1);
  767. reg_tmp = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
  768. dac33_write(codec, DAC33_INT_OSC_CTRL, reg_tmp);
  769. /* Write registers 0x08 and 0x09 (MSB, LSB) */
  770. dac33_write16(codec, DAC33_INT_OSC_FREQ_RAT_A, oscset);
  771. /* OSC calibration time */
  772. dac33_write(codec, DAC33_CALIB_TIME, 96);
  773. /* adjustment treshold & step */
  774. dac33_write(codec, DAC33_INT_OSC_CTRL_B, DAC33_ADJTHRSHLD(2) |
  775. DAC33_ADJSTEP(1));
  776. /* div=4 / gain=1 / div */
  777. dac33_write(codec, DAC33_INT_OSC_CTRL_C, DAC33_REFDIV(4));
  778. pwr_ctrl = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
  779. pwr_ctrl |= DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB;
  780. dac33_write(codec, DAC33_PWR_CTRL, pwr_ctrl);
  781. dac33_oscwait(codec);
  782. if (dac33->fifo_mode) {
  783. /* Generic for all FIFO modes */
  784. /* 50-51 : ASRC Control registers */
  785. dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCLKDIV(1));
  786. dac33_write(codec, DAC33_ASRC_CTRL_B, 1); /* ??? */
  787. /* Write registers 0x34 and 0x35 (MSB, LSB) */
  788. dac33_write16(codec, DAC33_SRC_REF_CLK_RATIO_A, ratioset);
  789. /* Set interrupts to high active */
  790. dac33_write(codec, DAC33_INTP_CTRL_A, DAC33_INTPM_AHIGH);
  791. } else {
  792. /* FIFO bypass mode */
  793. /* 50-51 : ASRC Control registers */
  794. dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCBYP);
  795. dac33_write(codec, DAC33_ASRC_CTRL_B, 0); /* ??? */
  796. }
  797. /* Interrupt behaviour configuration */
  798. switch (dac33->fifo_mode) {
  799. case DAC33_FIFO_MODE1:
  800. dac33_write(codec, DAC33_FIFO_IRQ_MODE_B,
  801. DAC33_ATM(DAC33_FIFO_IRQ_MODE_LEVEL));
  802. break;
  803. case DAC33_FIFO_MODE7:
  804. dac33_write(codec, DAC33_FIFO_IRQ_MODE_A,
  805. DAC33_UTM(DAC33_FIFO_IRQ_MODE_LEVEL));
  806. break;
  807. default:
  808. /* in FIFO bypass mode, the interrupts are not used */
  809. break;
  810. }
  811. aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
  812. switch (dac33->fifo_mode) {
  813. case DAC33_FIFO_MODE1:
  814. /*
  815. * For mode1:
  816. * Disable the FIFO bypass (Enable the use of FIFO)
  817. * Select nSample mode
  818. * BCLK is only running when data is needed by DAC33
  819. */
  820. fifoctrl_a &= ~DAC33_FBYPAS;
  821. fifoctrl_a &= ~DAC33_FAUTO;
  822. if (dac33->keep_bclk)
  823. aictrl_b |= DAC33_BCLKON;
  824. else
  825. aictrl_b &= ~DAC33_BCLKON;
  826. break;
  827. case DAC33_FIFO_MODE7:
  828. /*
  829. * For mode1:
  830. * Disable the FIFO bypass (Enable the use of FIFO)
  831. * Select Threshold mode
  832. * BCLK is only running when data is needed by DAC33
  833. */
  834. fifoctrl_a &= ~DAC33_FBYPAS;
  835. fifoctrl_a |= DAC33_FAUTO;
  836. if (dac33->keep_bclk)
  837. aictrl_b |= DAC33_BCLKON;
  838. else
  839. aictrl_b &= ~DAC33_BCLKON;
  840. break;
  841. default:
  842. /*
  843. * For FIFO bypass mode:
  844. * Enable the FIFO bypass (Disable the FIFO use)
  845. * Set the BCLK as continuous
  846. */
  847. fifoctrl_a |= DAC33_FBYPAS;
  848. aictrl_b |= DAC33_BCLKON;
  849. break;
  850. }
  851. dac33_write(codec, DAC33_FIFO_CTRL_A, fifoctrl_a);
  852. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
  853. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);
  854. /*
  855. * BCLK divide ratio
  856. * 0: 1.5
  857. * 1: 1
  858. * 2: 2
  859. * ...
  860. * 254: 254
  861. * 255: 255
  862. */
  863. if (dac33->fifo_mode)
  864. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C,
  865. dac33->burst_bclkdiv);
  866. else
  867. if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE)
  868. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 32);
  869. else
  870. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 16);
  871. switch (dac33->fifo_mode) {
  872. case DAC33_FIFO_MODE1:
  873. dac33_write16(codec, DAC33_ATHR_MSB,
  874. DAC33_THRREG(dac33->alarm_threshold));
  875. break;
  876. case DAC33_FIFO_MODE7:
  877. /*
  878. * Configure the threshold levels, and leave 10 sample space
  879. * at the bottom, and also at the top of the FIFO
  880. */
  881. dac33_write16(codec, DAC33_UTHR_MSB, DAC33_THRREG(dac33->uthr));
  882. dac33_write16(codec, DAC33_LTHR_MSB,
  883. DAC33_THRREG(DAC33_MODE7_MARGIN));
  884. break;
  885. default:
  886. break;
  887. }
  888. mutex_unlock(&dac33->mutex);
  889. return 0;
  890. }
  891. static void dac33_calculate_times(struct snd_pcm_substream *substream,
  892. struct snd_soc_codec *codec)
  893. {
  894. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  895. unsigned int period_size = substream->runtime->period_size;
  896. unsigned int rate = substream->runtime->rate;
  897. unsigned int nsample_limit;
  898. /* In bypass mode we don't need to calculate */
  899. if (!dac33->fifo_mode)
  900. return;
  901. switch (dac33->fifo_mode) {
  902. case DAC33_FIFO_MODE1:
  903. /* Number of samples under i2c latency */
  904. dac33->alarm_threshold = US_TO_SAMPLES(rate,
  905. dac33->mode1_latency);
  906. nsample_limit = dac33->fifo_size - dac33->alarm_threshold;
  907. if (period_size <= dac33->alarm_threshold)
  908. /*
  909. * Configure nSamaple to number of periods,
  910. * which covers the latency requironment.
  911. */
  912. dac33->nsample = period_size *
  913. ((dac33->alarm_threshold / period_size) +
  914. (dac33->alarm_threshold % period_size ?
  915. 1 : 0));
  916. else if (period_size > nsample_limit)
  917. dac33->nsample = nsample_limit;
  918. else
  919. dac33->nsample = period_size;
  920. dac33->mode1_us_burst = SAMPLES_TO_US(dac33->burst_rate,
  921. dac33->nsample);
  922. dac33->t_stamp1 = 0;
  923. dac33->t_stamp2 = 0;
  924. break;
  925. case DAC33_FIFO_MODE7:
  926. dac33->uthr = UTHR_FROM_PERIOD_SIZE(period_size, rate,
  927. dac33->burst_rate) + 9;
  928. if (dac33->uthr > (dac33->fifo_size - DAC33_MODE7_MARGIN))
  929. dac33->uthr = dac33->fifo_size - DAC33_MODE7_MARGIN;
  930. if (dac33->uthr < (DAC33_MODE7_MARGIN + 10))
  931. dac33->uthr = (DAC33_MODE7_MARGIN + 10);
  932. dac33->mode7_us_to_lthr =
  933. SAMPLES_TO_US(substream->runtime->rate,
  934. dac33->uthr - DAC33_MODE7_MARGIN + 1);
  935. dac33->t_stamp1 = 0;
  936. break;
  937. default:
  938. break;
  939. }
  940. }
  941. static int dac33_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
  942. struct snd_soc_dai *dai)
  943. {
  944. struct snd_soc_codec *codec = dai->codec;
  945. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  946. int ret = 0;
  947. switch (cmd) {
  948. case SNDRV_PCM_TRIGGER_START:
  949. case SNDRV_PCM_TRIGGER_RESUME:
  950. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  951. if (dac33->fifo_mode) {
  952. dac33->state = DAC33_PREFILL;
  953. schedule_work(&dac33->work);
  954. }
  955. break;
  956. case SNDRV_PCM_TRIGGER_STOP:
  957. case SNDRV_PCM_TRIGGER_SUSPEND:
  958. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  959. if (dac33->fifo_mode) {
  960. dac33->state = DAC33_FLUSH;
  961. schedule_work(&dac33->work);
  962. }
  963. break;
  964. default:
  965. ret = -EINVAL;
  966. }
  967. return ret;
  968. }
  969. static snd_pcm_sframes_t dac33_dai_delay(
  970. struct snd_pcm_substream *substream,
  971. struct snd_soc_dai *dai)
  972. {
  973. struct snd_soc_codec *codec = dai->codec;
  974. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  975. unsigned long long t0, t1, t_now;
  976. unsigned int time_delta, uthr;
  977. int samples_out, samples_in, samples;
  978. snd_pcm_sframes_t delay = 0;
  979. unsigned long flags;
  980. switch (dac33->fifo_mode) {
  981. case DAC33_FIFO_BYPASS:
  982. break;
  983. case DAC33_FIFO_MODE1:
  984. spin_lock_irqsave(&dac33->lock, flags);
  985. t0 = dac33->t_stamp1;
  986. t1 = dac33->t_stamp2;
  987. spin_unlock_irqrestore(&dac33->lock, flags);
  988. t_now = ktime_to_us(ktime_get());
  989. /* We have not started to fill the FIFO yet, delay is 0 */
  990. if (!t1)
  991. goto out;
  992. if (t0 > t1) {
  993. /*
  994. * Phase 1:
  995. * After Alarm threshold, and before nSample write
  996. */
  997. time_delta = t_now - t0;
  998. samples_out = time_delta ? US_TO_SAMPLES(
  999. substream->runtime->rate,
  1000. time_delta) : 0;
  1001. if (likely(dac33->alarm_threshold > samples_out))
  1002. delay = dac33->alarm_threshold - samples_out;
  1003. else
  1004. delay = 0;
  1005. } else if ((t_now - t1) <= dac33->mode1_us_burst) {
  1006. /*
  1007. * Phase 2:
  1008. * After nSample write (during burst operation)
  1009. */
  1010. time_delta = t_now - t0;
  1011. samples_out = time_delta ? US_TO_SAMPLES(
  1012. substream->runtime->rate,
  1013. time_delta) : 0;
  1014. time_delta = t_now - t1;
  1015. samples_in = time_delta ? US_TO_SAMPLES(
  1016. dac33->burst_rate,
  1017. time_delta) : 0;
  1018. samples = dac33->alarm_threshold;
  1019. samples += (samples_in - samples_out);
  1020. if (likely(samples > 0))
  1021. delay = samples;
  1022. else
  1023. delay = 0;
  1024. } else {
  1025. /*
  1026. * Phase 3:
  1027. * After burst operation, before next alarm threshold
  1028. */
  1029. time_delta = t_now - t0;
  1030. samples_out = time_delta ? US_TO_SAMPLES(
  1031. substream->runtime->rate,
  1032. time_delta) : 0;
  1033. samples_in = dac33->nsample;
  1034. samples = dac33->alarm_threshold;
  1035. samples += (samples_in - samples_out);
  1036. if (likely(samples > 0))
  1037. delay = samples > dac33->fifo_size ?
  1038. dac33->fifo_size : samples;
  1039. else
  1040. delay = 0;
  1041. }
  1042. break;
  1043. case DAC33_FIFO_MODE7:
  1044. spin_lock_irqsave(&dac33->lock, flags);
  1045. t0 = dac33->t_stamp1;
  1046. uthr = dac33->uthr;
  1047. spin_unlock_irqrestore(&dac33->lock, flags);
  1048. t_now = ktime_to_us(ktime_get());
  1049. /* We have not started to fill the FIFO yet, delay is 0 */
  1050. if (!t0)
  1051. goto out;
  1052. if (t_now <= t0) {
  1053. /*
  1054. * Either the timestamps are messed or equal. Report
  1055. * maximum delay
  1056. */
  1057. delay = uthr;
  1058. goto out;
  1059. }
  1060. time_delta = t_now - t0;
  1061. if (time_delta <= dac33->mode7_us_to_lthr) {
  1062. /*
  1063. * Phase 1:
  1064. * After burst (draining phase)
  1065. */
  1066. samples_out = US_TO_SAMPLES(
  1067. substream->runtime->rate,
  1068. time_delta);
  1069. if (likely(uthr > samples_out))
  1070. delay = uthr - samples_out;
  1071. else
  1072. delay = 0;
  1073. } else {
  1074. /*
  1075. * Phase 2:
  1076. * During burst operation
  1077. */
  1078. time_delta = time_delta - dac33->mode7_us_to_lthr;
  1079. samples_out = US_TO_SAMPLES(
  1080. substream->runtime->rate,
  1081. time_delta);
  1082. samples_in = US_TO_SAMPLES(
  1083. dac33->burst_rate,
  1084. time_delta);
  1085. delay = DAC33_MODE7_MARGIN + samples_in - samples_out;
  1086. if (unlikely(delay > uthr))
  1087. delay = uthr;
  1088. }
  1089. break;
  1090. default:
  1091. dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
  1092. dac33->fifo_mode);
  1093. break;
  1094. }
  1095. out:
  1096. return delay;
  1097. }
  1098. static int dac33_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  1099. int clk_id, unsigned int freq, int dir)
  1100. {
  1101. struct snd_soc_codec *codec = codec_dai->codec;
  1102. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  1103. u8 ioc_reg, asrcb_reg;
  1104. ioc_reg = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
  1105. asrcb_reg = dac33_read_reg_cache(codec, DAC33_ASRC_CTRL_B);
  1106. switch (clk_id) {
  1107. case TLV320DAC33_MCLK:
  1108. ioc_reg |= DAC33_REFSEL;
  1109. asrcb_reg |= DAC33_SRCREFSEL;
  1110. break;
  1111. case TLV320DAC33_SLEEPCLK:
  1112. ioc_reg &= ~DAC33_REFSEL;
  1113. asrcb_reg &= ~DAC33_SRCREFSEL;
  1114. break;
  1115. default:
  1116. dev_err(codec->dev, "Invalid clock ID (%d)\n", clk_id);
  1117. break;
  1118. }
  1119. dac33->refclk = freq;
  1120. dac33_write_reg_cache(codec, DAC33_INT_OSC_CTRL, ioc_reg);
  1121. dac33_write_reg_cache(codec, DAC33_ASRC_CTRL_B, asrcb_reg);
  1122. return 0;
  1123. }
  1124. static int dac33_set_dai_fmt(struct snd_soc_dai *codec_dai,
  1125. unsigned int fmt)
  1126. {
  1127. struct snd_soc_codec *codec = codec_dai->codec;
  1128. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  1129. u8 aictrl_a, aictrl_b;
  1130. aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
  1131. aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
  1132. /* set master/slave audio interface */
  1133. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  1134. case SND_SOC_DAIFMT_CBM_CFM:
  1135. /* Codec Master */
  1136. aictrl_a |= (DAC33_MSBCLK | DAC33_MSWCLK);
  1137. break;
  1138. case SND_SOC_DAIFMT_CBS_CFS:
  1139. /* Codec Slave */
  1140. if (dac33->fifo_mode) {
  1141. dev_err(codec->dev, "FIFO mode requires master mode\n");
  1142. return -EINVAL;
  1143. } else
  1144. aictrl_a &= ~(DAC33_MSBCLK | DAC33_MSWCLK);
  1145. break;
  1146. default:
  1147. return -EINVAL;
  1148. }
  1149. aictrl_a &= ~DAC33_AFMT_MASK;
  1150. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  1151. case SND_SOC_DAIFMT_I2S:
  1152. aictrl_a |= DAC33_AFMT_I2S;
  1153. break;
  1154. case SND_SOC_DAIFMT_DSP_A:
  1155. aictrl_a |= DAC33_AFMT_DSP;
  1156. aictrl_b &= ~DAC33_DATA_DELAY_MASK;
  1157. aictrl_b |= DAC33_DATA_DELAY(0);
  1158. break;
  1159. case SND_SOC_DAIFMT_RIGHT_J:
  1160. aictrl_a |= DAC33_AFMT_RIGHT_J;
  1161. break;
  1162. case SND_SOC_DAIFMT_LEFT_J:
  1163. aictrl_a |= DAC33_AFMT_LEFT_J;
  1164. break;
  1165. default:
  1166. dev_err(codec->dev, "Unsupported format (%u)\n",
  1167. fmt & SND_SOC_DAIFMT_FORMAT_MASK);
  1168. return -EINVAL;
  1169. }
  1170. dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
  1171. dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);
  1172. return 0;
  1173. }
  1174. static int dac33_soc_probe(struct snd_soc_codec *codec)
  1175. {
  1176. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  1177. int ret = 0;
  1178. codec->control_data = dac33->control_data;
  1179. codec->hw_write = (hw_write_t) i2c_master_send;
  1180. dac33->codec = codec;
  1181. /* Read the tlv320dac33 ID registers */
  1182. ret = dac33_hard_power(codec, 1);
  1183. if (ret != 0) {
  1184. dev_err(codec->dev, "Failed to power up codec: %d\n", ret);
  1185. goto err_power;
  1186. }
  1187. ret = dac33_read_id(codec);
  1188. dac33_hard_power(codec, 0);
  1189. if (ret < 0) {
  1190. dev_err(codec->dev, "Failed to read chip ID: %d\n", ret);
  1191. ret = -ENODEV;
  1192. goto err_power;
  1193. }
  1194. /* Check if the IRQ number is valid and request it */
  1195. if (dac33->irq >= 0) {
  1196. ret = request_irq(dac33->irq, dac33_interrupt_handler,
  1197. IRQF_TRIGGER_RISING,
  1198. codec->component.name, codec);
  1199. if (ret < 0) {
  1200. dev_err(codec->dev, "Could not request IRQ%d (%d)\n",
  1201. dac33->irq, ret);
  1202. dac33->irq = -1;
  1203. }
  1204. if (dac33->irq != -1) {
  1205. INIT_WORK(&dac33->work, dac33_work);
  1206. }
  1207. }
  1208. /* Only add the FIFO controls, if we have valid IRQ number */
  1209. if (dac33->irq >= 0)
  1210. snd_soc_add_codec_controls(codec, dac33_mode_snd_controls,
  1211. ARRAY_SIZE(dac33_mode_snd_controls));
  1212. err_power:
  1213. return ret;
  1214. }
  1215. static int dac33_soc_remove(struct snd_soc_codec *codec)
  1216. {
  1217. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  1218. if (dac33->irq >= 0) {
  1219. free_irq(dac33->irq, dac33->codec);
  1220. flush_work(&dac33->work);
  1221. }
  1222. return 0;
  1223. }
  1224. static struct snd_soc_codec_driver soc_codec_dev_tlv320dac33 = {
  1225. .read = dac33_read_reg_cache,
  1226. .write = dac33_write_locked,
  1227. .set_bias_level = dac33_set_bias_level,
  1228. .idle_bias_off = true,
  1229. .reg_cache_size = ARRAY_SIZE(dac33_reg),
  1230. .reg_word_size = sizeof(u8),
  1231. .reg_cache_default = dac33_reg,
  1232. .probe = dac33_soc_probe,
  1233. .remove = dac33_soc_remove,
  1234. .component_driver = {
  1235. .controls = dac33_snd_controls,
  1236. .num_controls = ARRAY_SIZE(dac33_snd_controls),
  1237. .dapm_widgets = dac33_dapm_widgets,
  1238. .num_dapm_widgets = ARRAY_SIZE(dac33_dapm_widgets),
  1239. .dapm_routes = audio_map,
  1240. .num_dapm_routes = ARRAY_SIZE(audio_map),
  1241. },
  1242. };
  1243. #define DAC33_RATES (SNDRV_PCM_RATE_44100 | \
  1244. SNDRV_PCM_RATE_48000)
  1245. #define DAC33_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1246. static const struct snd_soc_dai_ops dac33_dai_ops = {
  1247. .startup = dac33_startup,
  1248. .shutdown = dac33_shutdown,
  1249. .hw_params = dac33_hw_params,
  1250. .trigger = dac33_pcm_trigger,
  1251. .delay = dac33_dai_delay,
  1252. .set_sysclk = dac33_set_dai_sysclk,
  1253. .set_fmt = dac33_set_dai_fmt,
  1254. };
  1255. static struct snd_soc_dai_driver dac33_dai = {
  1256. .name = "tlv320dac33-hifi",
  1257. .playback = {
  1258. .stream_name = "Playback",
  1259. .channels_min = 2,
  1260. .channels_max = 2,
  1261. .rates = DAC33_RATES,
  1262. .formats = DAC33_FORMATS,
  1263. .sig_bits = 24,
  1264. },
  1265. .ops = &dac33_dai_ops,
  1266. };
  1267. static int dac33_i2c_probe(struct i2c_client *client,
  1268. const struct i2c_device_id *id)
  1269. {
  1270. struct tlv320dac33_platform_data *pdata;
  1271. struct tlv320dac33_priv *dac33;
  1272. int ret, i;
  1273. if (client->dev.platform_data == NULL) {
  1274. dev_err(&client->dev, "Platform data not set\n");
  1275. return -ENODEV;
  1276. }
  1277. pdata = client->dev.platform_data;
  1278. dac33 = devm_kzalloc(&client->dev, sizeof(struct tlv320dac33_priv),
  1279. GFP_KERNEL);
  1280. if (dac33 == NULL)
  1281. return -ENOMEM;
  1282. dac33->control_data = client;
  1283. mutex_init(&dac33->mutex);
  1284. spin_lock_init(&dac33->lock);
  1285. i2c_set_clientdata(client, dac33);
  1286. dac33->power_gpio = pdata->power_gpio;
  1287. dac33->burst_bclkdiv = pdata->burst_bclkdiv;
  1288. dac33->keep_bclk = pdata->keep_bclk;
  1289. dac33->mode1_latency = pdata->mode1_latency;
  1290. if (!dac33->mode1_latency)
  1291. dac33->mode1_latency = 10000; /* 10ms */
  1292. dac33->irq = client->irq;
  1293. /* Disable FIFO use by default */
  1294. dac33->fifo_mode = DAC33_FIFO_BYPASS;
  1295. /* Check if the reset GPIO number is valid and request it */
  1296. if (dac33->power_gpio >= 0) {
  1297. ret = gpio_request(dac33->power_gpio, "tlv320dac33 reset");
  1298. if (ret < 0) {
  1299. dev_err(&client->dev,
  1300. "Failed to request reset GPIO (%d)\n",
  1301. dac33->power_gpio);
  1302. goto err_gpio;
  1303. }
  1304. gpio_direction_output(dac33->power_gpio, 0);
  1305. }
  1306. for (i = 0; i < ARRAY_SIZE(dac33->supplies); i++)
  1307. dac33->supplies[i].supply = dac33_supply_names[i];
  1308. ret = devm_regulator_bulk_get(&client->dev, ARRAY_SIZE(dac33->supplies),
  1309. dac33->supplies);
  1310. if (ret != 0) {
  1311. dev_err(&client->dev, "Failed to request supplies: %d\n", ret);
  1312. goto err_get;
  1313. }
  1314. ret = snd_soc_register_codec(&client->dev,
  1315. &soc_codec_dev_tlv320dac33, &dac33_dai, 1);
  1316. if (ret < 0)
  1317. goto err_get;
  1318. return ret;
  1319. err_get:
  1320. if (dac33->power_gpio >= 0)
  1321. gpio_free(dac33->power_gpio);
  1322. err_gpio:
  1323. return ret;
  1324. }
  1325. static int dac33_i2c_remove(struct i2c_client *client)
  1326. {
  1327. struct tlv320dac33_priv *dac33 = i2c_get_clientdata(client);
  1328. if (unlikely(dac33->chip_power))
  1329. dac33_hard_power(dac33->codec, 0);
  1330. if (dac33->power_gpio >= 0)
  1331. gpio_free(dac33->power_gpio);
  1332. snd_soc_unregister_codec(&client->dev);
  1333. return 0;
  1334. }
  1335. static const struct i2c_device_id tlv320dac33_i2c_id[] = {
  1336. {
  1337. .name = "tlv320dac33",
  1338. .driver_data = 0,
  1339. },
  1340. { },
  1341. };
  1342. MODULE_DEVICE_TABLE(i2c, tlv320dac33_i2c_id);
  1343. static struct i2c_driver tlv320dac33_i2c_driver = {
  1344. .driver = {
  1345. .name = "tlv320dac33-codec",
  1346. },
  1347. .probe = dac33_i2c_probe,
  1348. .remove = dac33_i2c_remove,
  1349. .id_table = tlv320dac33_i2c_id,
  1350. };
  1351. module_i2c_driver(tlv320dac33_i2c_driver);
  1352. MODULE_DESCRIPTION("ASoC TLV320DAC33 codec driver");
  1353. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  1354. MODULE_LICENSE("GPL");