tlv320dac33.c 44 KB

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