tlv320dac33.c 43 KB

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