tlv320dac33.c 36 KB

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