tlv320dac33.c 43 KB

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