tlv320dac33.c 44 KB

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