tlv320dac33.c 43 KB

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