tlv320dac33.c 42 KB

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