tlv320dac33.c 43 KB

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