tlv320dac33.c 43 KB

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