tlv320dac33.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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 = snd_soc_codec_get_drvdata(codec);
  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 = snd_soc_codec_get_drvdata(codec);
  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 = snd_soc_codec_get_drvdata(codec);
  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 = snd_soc_codec_get_drvdata(codec);
  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 = snd_soc_codec_get_drvdata(codec);
  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 = snd_soc_codec_get_drvdata(codec);
  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 = snd_soc_codec_get_drvdata(codec);
  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 = snd_soc_codec_get_drvdata(codec);
  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 = snd_soc_codec_get_drvdata(codec);
  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 = snd_soc_codec_get_drvdata(codec);
  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 + dac33->alarm_threshold));
  470. dac33_write16(codec, DAC33_PREFILL_MSB,
  471. DAC33_THRREG(dac33->alarm_threshold));
  472. /* Enable Alarm Threshold IRQ with a delay */
  473. udelay(SAMPLES_TO_US(dac33->burst_rate,
  474. dac33->alarm_threshold));
  475. dac33_write(codec, DAC33_FIFO_IRQ_MASK, DAC33_MAT);
  476. break;
  477. case DAC33_FIFO_MODE7:
  478. dac33_write16(codec, DAC33_PREFILL_MSB,
  479. DAC33_THRREG(10));
  480. break;
  481. default:
  482. dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
  483. dac33->fifo_mode);
  484. break;
  485. }
  486. }
  487. static inline void dac33_playback_handler(struct tlv320dac33_priv *dac33)
  488. {
  489. struct snd_soc_codec *codec;
  490. codec = &dac33->codec;
  491. switch (dac33->fifo_mode) {
  492. case DAC33_FIFO_MODE1:
  493. dac33_write16(codec, DAC33_NSAMPLE_MSB,
  494. DAC33_THRREG(dac33->nsample));
  495. break;
  496. case DAC33_FIFO_MODE7:
  497. /* At the moment we are not using interrupts in mode7 */
  498. break;
  499. default:
  500. dev_warn(codec->dev, "Unhandled FIFO mode: %d\n",
  501. dac33->fifo_mode);
  502. break;
  503. }
  504. }
  505. static void dac33_work(struct work_struct *work)
  506. {
  507. struct snd_soc_codec *codec;
  508. struct tlv320dac33_priv *dac33;
  509. u8 reg;
  510. dac33 = container_of(work, struct tlv320dac33_priv, work);
  511. codec = &dac33->codec;
  512. mutex_lock(&dac33->mutex);
  513. switch (dac33->state) {
  514. case DAC33_PREFILL:
  515. dac33->state = DAC33_PLAYBACK;
  516. dac33_prefill_handler(dac33);
  517. break;
  518. case DAC33_PLAYBACK:
  519. dac33_playback_handler(dac33);
  520. break;
  521. case DAC33_IDLE:
  522. break;
  523. case DAC33_FLUSH:
  524. dac33->state = DAC33_IDLE;
  525. /* Mask all interrupts from dac33 */
  526. dac33_write(codec, DAC33_FIFO_IRQ_MASK, 0);
  527. /* flush fifo */
  528. reg = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
  529. reg |= DAC33_FIFOFLUSH;
  530. dac33_write(codec, DAC33_FIFO_CTRL_A, reg);
  531. break;
  532. }
  533. mutex_unlock(&dac33->mutex);
  534. }
  535. static irqreturn_t dac33_interrupt_handler(int irq, void *dev)
  536. {
  537. struct snd_soc_codec *codec = dev;
  538. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  539. queue_work(dac33->dac33_wq, &dac33->work);
  540. return IRQ_HANDLED;
  541. }
  542. static void dac33_oscwait(struct snd_soc_codec *codec)
  543. {
  544. int timeout = 20;
  545. u8 reg;
  546. do {
  547. msleep(1);
  548. dac33_read(codec, DAC33_INT_OSC_STATUS, &reg);
  549. } while (((reg & 0x03) != DAC33_OSCSTATUS_NORMAL) && timeout--);
  550. if ((reg & 0x03) != DAC33_OSCSTATUS_NORMAL)
  551. dev_err(codec->dev,
  552. "internal oscillator calibration failed\n");
  553. }
  554. static int dac33_hw_params(struct snd_pcm_substream *substream,
  555. struct snd_pcm_hw_params *params,
  556. struct snd_soc_dai *dai)
  557. {
  558. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  559. struct snd_soc_device *socdev = rtd->socdev;
  560. struct snd_soc_codec *codec = socdev->card->codec;
  561. /* Check parameters for validity */
  562. switch (params_rate(params)) {
  563. case 44100:
  564. case 48000:
  565. break;
  566. default:
  567. dev_err(codec->dev, "unsupported rate %d\n",
  568. params_rate(params));
  569. return -EINVAL;
  570. }
  571. switch (params_format(params)) {
  572. case SNDRV_PCM_FORMAT_S16_LE:
  573. break;
  574. default:
  575. dev_err(codec->dev, "unsupported format %d\n",
  576. params_format(params));
  577. return -EINVAL;
  578. }
  579. return 0;
  580. }
  581. #define CALC_OSCSET(rate, refclk) ( \
  582. ((((rate * 10000) / refclk) * 4096) + 7000) / 10000)
  583. #define CALC_RATIOSET(rate, refclk) ( \
  584. ((((refclk * 100000) / rate) * 16384) + 50000) / 100000)
  585. /*
  586. * tlv320dac33 is strict on the sequence of the register writes, if the register
  587. * writes happens in different order, than dac33 might end up in unknown state.
  588. * Use the known, working sequence of register writes to initialize the dac33.
  589. */
  590. static int dac33_prepare_chip(struct snd_pcm_substream *substream)
  591. {
  592. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  593. struct snd_soc_device *socdev = rtd->socdev;
  594. struct snd_soc_codec *codec = socdev->card->codec;
  595. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  596. unsigned int oscset, ratioset, pwr_ctrl, reg_tmp;
  597. u8 aictrl_a, aictrl_b, fifoctrl_a;
  598. switch (substream->runtime->rate) {
  599. case 44100:
  600. case 48000:
  601. oscset = CALC_OSCSET(substream->runtime->rate, dac33->refclk);
  602. ratioset = CALC_RATIOSET(substream->runtime->rate,
  603. dac33->refclk);
  604. break;
  605. default:
  606. dev_err(codec->dev, "unsupported rate %d\n",
  607. substream->runtime->rate);
  608. return -EINVAL;
  609. }
  610. aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
  611. aictrl_a &= ~(DAC33_NCYCL_MASK | DAC33_WLEN_MASK);
  612. /* Read FIFO control A, and clear FIFO flush bit */
  613. fifoctrl_a = dac33_read_reg_cache(codec, DAC33_FIFO_CTRL_A);
  614. fifoctrl_a &= ~DAC33_FIFOFLUSH;
  615. fifoctrl_a &= ~DAC33_WIDTH;
  616. switch (substream->runtime->format) {
  617. case SNDRV_PCM_FORMAT_S16_LE:
  618. aictrl_a |= (DAC33_NCYCL_16 | DAC33_WLEN_16);
  619. fifoctrl_a |= DAC33_WIDTH;
  620. break;
  621. default:
  622. dev_err(codec->dev, "unsupported format %d\n",
  623. substream->runtime->format);
  624. return -EINVAL;
  625. }
  626. mutex_lock(&dac33->mutex);
  627. dac33_soft_power(codec, 0);
  628. dac33_soft_power(codec, 1);
  629. reg_tmp = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
  630. dac33_write(codec, DAC33_INT_OSC_CTRL, reg_tmp);
  631. /* Write registers 0x08 and 0x09 (MSB, LSB) */
  632. dac33_write16(codec, DAC33_INT_OSC_FREQ_RAT_A, oscset);
  633. /* calib time: 128 is a nice number ;) */
  634. dac33_write(codec, DAC33_CALIB_TIME, 128);
  635. /* adjustment treshold & step */
  636. dac33_write(codec, DAC33_INT_OSC_CTRL_B, DAC33_ADJTHRSHLD(2) |
  637. DAC33_ADJSTEP(1));
  638. /* div=4 / gain=1 / div */
  639. dac33_write(codec, DAC33_INT_OSC_CTRL_C, DAC33_REFDIV(4));
  640. pwr_ctrl = dac33_read_reg_cache(codec, DAC33_PWR_CTRL);
  641. pwr_ctrl |= DAC33_OSCPDNB | DAC33_DACRPDNB | DAC33_DACLPDNB;
  642. dac33_write(codec, DAC33_PWR_CTRL, pwr_ctrl);
  643. dac33_oscwait(codec);
  644. if (dac33->fifo_mode) {
  645. /* Generic for all FIFO modes */
  646. /* 50-51 : ASRC Control registers */
  647. dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCLKDIV(1));
  648. dac33_write(codec, DAC33_ASRC_CTRL_B, 1); /* ??? */
  649. /* Write registers 0x34 and 0x35 (MSB, LSB) */
  650. dac33_write16(codec, DAC33_SRC_REF_CLK_RATIO_A, ratioset);
  651. /* Set interrupts to high active */
  652. dac33_write(codec, DAC33_INTP_CTRL_A, DAC33_INTPM_AHIGH);
  653. } else {
  654. /* FIFO bypass mode */
  655. /* 50-51 : ASRC Control registers */
  656. dac33_write(codec, DAC33_ASRC_CTRL_A, DAC33_SRCBYP);
  657. dac33_write(codec, DAC33_ASRC_CTRL_B, 0); /* ??? */
  658. }
  659. /* Interrupt behaviour configuration */
  660. switch (dac33->fifo_mode) {
  661. case DAC33_FIFO_MODE1:
  662. dac33_write(codec, DAC33_FIFO_IRQ_MODE_B,
  663. DAC33_ATM(DAC33_FIFO_IRQ_MODE_LEVEL));
  664. break;
  665. case DAC33_FIFO_MODE7:
  666. /* Disable all interrupts */
  667. dac33_write(codec, DAC33_FIFO_IRQ_MASK, 0);
  668. break;
  669. default:
  670. /* in FIFO bypass mode, the interrupts are not used */
  671. break;
  672. }
  673. aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
  674. switch (dac33->fifo_mode) {
  675. case DAC33_FIFO_MODE1:
  676. /*
  677. * For mode1:
  678. * Disable the FIFO bypass (Enable the use of FIFO)
  679. * Select nSample mode
  680. * BCLK is only running when data is needed by DAC33
  681. */
  682. fifoctrl_a &= ~DAC33_FBYPAS;
  683. fifoctrl_a &= ~DAC33_FAUTO;
  684. if (dac33->keep_bclk)
  685. aictrl_b |= DAC33_BCLKON;
  686. else
  687. aictrl_b &= ~DAC33_BCLKON;
  688. break;
  689. case DAC33_FIFO_MODE7:
  690. /*
  691. * For mode1:
  692. * Disable the FIFO bypass (Enable the use of FIFO)
  693. * Select Threshold mode
  694. * BCLK is only running when data is needed by DAC33
  695. */
  696. fifoctrl_a &= ~DAC33_FBYPAS;
  697. fifoctrl_a |= DAC33_FAUTO;
  698. if (dac33->keep_bclk)
  699. aictrl_b |= DAC33_BCLKON;
  700. else
  701. aictrl_b &= ~DAC33_BCLKON;
  702. break;
  703. default:
  704. /*
  705. * For FIFO bypass mode:
  706. * Enable the FIFO bypass (Disable the FIFO use)
  707. * Set the BCLK as continous
  708. */
  709. fifoctrl_a |= DAC33_FBYPAS;
  710. aictrl_b |= DAC33_BCLKON;
  711. break;
  712. }
  713. dac33_write(codec, DAC33_FIFO_CTRL_A, fifoctrl_a);
  714. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
  715. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);
  716. /*
  717. * BCLK divide ratio
  718. * 0: 1.5
  719. * 1: 1
  720. * 2: 2
  721. * ...
  722. * 254: 254
  723. * 255: 255
  724. */
  725. if (dac33->fifo_mode)
  726. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C,
  727. dac33->burst_bclkdiv);
  728. else
  729. dac33_write(codec, DAC33_SER_AUDIOIF_CTRL_C, 32);
  730. switch (dac33->fifo_mode) {
  731. case DAC33_FIFO_MODE1:
  732. dac33_write16(codec, DAC33_ATHR_MSB,
  733. DAC33_THRREG(dac33->alarm_threshold));
  734. break;
  735. case DAC33_FIFO_MODE7:
  736. /*
  737. * Configure the threshold levels, and leave 10 sample space
  738. * at the bottom, and also at the top of the FIFO
  739. */
  740. dac33_write16(codec, DAC33_UTHR_MSB,
  741. DAC33_THRREG(DAC33_BUFFER_SIZE_SAMPLES - 10));
  742. dac33_write16(codec, DAC33_LTHR_MSB,
  743. DAC33_THRREG(10));
  744. break;
  745. default:
  746. break;
  747. }
  748. mutex_unlock(&dac33->mutex);
  749. return 0;
  750. }
  751. static void dac33_calculate_times(struct snd_pcm_substream *substream)
  752. {
  753. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  754. struct snd_soc_device *socdev = rtd->socdev;
  755. struct snd_soc_codec *codec = socdev->card->codec;
  756. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  757. unsigned int nsample_limit;
  758. /* In bypass mode we don't need to calculate */
  759. if (!dac33->fifo_mode)
  760. return;
  761. /* Number of samples (16bit, stereo) in one period */
  762. dac33->nsample_min = snd_pcm_lib_period_bytes(substream) / 4;
  763. /* Number of samples (16bit, stereo) in ALSA buffer */
  764. dac33->nsample_max = snd_pcm_lib_buffer_bytes(substream) / 4;
  765. /* Subtract one period from the total */
  766. dac33->nsample_max -= dac33->nsample_min;
  767. /* Number of samples for LATENCY_TIME_MS / 2 */
  768. dac33->alarm_threshold = substream->runtime->rate /
  769. (1000 / (LATENCY_TIME_MS / 2));
  770. /* Find and fix up the lowest nsmaple limit */
  771. nsample_limit = substream->runtime->rate / (1000 / LATENCY_TIME_MS);
  772. if (dac33->nsample_min < nsample_limit)
  773. dac33->nsample_min = nsample_limit;
  774. if (dac33->nsample < dac33->nsample_min)
  775. dac33->nsample = dac33->nsample_min;
  776. /*
  777. * Find and fix up the highest nsmaple limit
  778. * In order to not overflow the DAC33 buffer substract the
  779. * alarm_threshold value from the size of the DAC33 buffer
  780. */
  781. nsample_limit = DAC33_BUFFER_SIZE_SAMPLES - dac33->alarm_threshold;
  782. if (dac33->nsample_max > nsample_limit)
  783. dac33->nsample_max = nsample_limit;
  784. if (dac33->nsample > dac33->nsample_max)
  785. dac33->nsample = dac33->nsample_max;
  786. }
  787. static int dac33_pcm_prepare(struct snd_pcm_substream *substream,
  788. struct snd_soc_dai *dai)
  789. {
  790. dac33_calculate_times(substream);
  791. dac33_prepare_chip(substream);
  792. return 0;
  793. }
  794. static int dac33_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
  795. struct snd_soc_dai *dai)
  796. {
  797. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  798. struct snd_soc_device *socdev = rtd->socdev;
  799. struct snd_soc_codec *codec = socdev->card->codec;
  800. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  801. int ret = 0;
  802. switch (cmd) {
  803. case SNDRV_PCM_TRIGGER_START:
  804. case SNDRV_PCM_TRIGGER_RESUME:
  805. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  806. if (dac33->fifo_mode) {
  807. dac33->state = DAC33_PREFILL;
  808. queue_work(dac33->dac33_wq, &dac33->work);
  809. }
  810. break;
  811. case SNDRV_PCM_TRIGGER_STOP:
  812. case SNDRV_PCM_TRIGGER_SUSPEND:
  813. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  814. if (dac33->fifo_mode) {
  815. dac33->state = DAC33_FLUSH;
  816. queue_work(dac33->dac33_wq, &dac33->work);
  817. }
  818. break;
  819. default:
  820. ret = -EINVAL;
  821. }
  822. return ret;
  823. }
  824. static int dac33_set_dai_sysclk(struct snd_soc_dai *codec_dai,
  825. int clk_id, unsigned int freq, int dir)
  826. {
  827. struct snd_soc_codec *codec = codec_dai->codec;
  828. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  829. u8 ioc_reg, asrcb_reg;
  830. ioc_reg = dac33_read_reg_cache(codec, DAC33_INT_OSC_CTRL);
  831. asrcb_reg = dac33_read_reg_cache(codec, DAC33_ASRC_CTRL_B);
  832. switch (clk_id) {
  833. case TLV320DAC33_MCLK:
  834. ioc_reg |= DAC33_REFSEL;
  835. asrcb_reg |= DAC33_SRCREFSEL;
  836. break;
  837. case TLV320DAC33_SLEEPCLK:
  838. ioc_reg &= ~DAC33_REFSEL;
  839. asrcb_reg &= ~DAC33_SRCREFSEL;
  840. break;
  841. default:
  842. dev_err(codec->dev, "Invalid clock ID (%d)\n", clk_id);
  843. break;
  844. }
  845. dac33->refclk = freq;
  846. dac33_write_reg_cache(codec, DAC33_INT_OSC_CTRL, ioc_reg);
  847. dac33_write_reg_cache(codec, DAC33_ASRC_CTRL_B, asrcb_reg);
  848. return 0;
  849. }
  850. static int dac33_set_dai_fmt(struct snd_soc_dai *codec_dai,
  851. unsigned int fmt)
  852. {
  853. struct snd_soc_codec *codec = codec_dai->codec;
  854. struct tlv320dac33_priv *dac33 = snd_soc_codec_get_drvdata(codec);
  855. u8 aictrl_a, aictrl_b;
  856. aictrl_a = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A);
  857. aictrl_b = dac33_read_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B);
  858. /* set master/slave audio interface */
  859. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  860. case SND_SOC_DAIFMT_CBM_CFM:
  861. /* Codec Master */
  862. aictrl_a |= (DAC33_MSBCLK | DAC33_MSWCLK);
  863. break;
  864. case SND_SOC_DAIFMT_CBS_CFS:
  865. /* Codec Slave */
  866. if (dac33->fifo_mode) {
  867. dev_err(codec->dev, "FIFO mode requires master mode\n");
  868. return -EINVAL;
  869. } else
  870. aictrl_a &= ~(DAC33_MSBCLK | DAC33_MSWCLK);
  871. break;
  872. default:
  873. return -EINVAL;
  874. }
  875. aictrl_a &= ~DAC33_AFMT_MASK;
  876. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  877. case SND_SOC_DAIFMT_I2S:
  878. aictrl_a |= DAC33_AFMT_I2S;
  879. break;
  880. case SND_SOC_DAIFMT_DSP_A:
  881. aictrl_a |= DAC33_AFMT_DSP;
  882. aictrl_b &= ~DAC33_DATA_DELAY_MASK;
  883. aictrl_b |= DAC33_DATA_DELAY(0);
  884. break;
  885. case SND_SOC_DAIFMT_RIGHT_J:
  886. aictrl_a |= DAC33_AFMT_RIGHT_J;
  887. break;
  888. case SND_SOC_DAIFMT_LEFT_J:
  889. aictrl_a |= DAC33_AFMT_LEFT_J;
  890. break;
  891. default:
  892. dev_err(codec->dev, "Unsupported format (%u)\n",
  893. fmt & SND_SOC_DAIFMT_FORMAT_MASK);
  894. return -EINVAL;
  895. }
  896. dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_A, aictrl_a);
  897. dac33_write_reg_cache(codec, DAC33_SER_AUDIOIF_CTRL_B, aictrl_b);
  898. return 0;
  899. }
  900. static void dac33_init_chip(struct snd_soc_codec *codec)
  901. {
  902. /* 44-46: DAC Control Registers */
  903. /* A : DAC sample rate Fsref/1.5 */
  904. dac33_write(codec, DAC33_DAC_CTRL_A, DAC33_DACRATE(0));
  905. /* B : DAC src=normal, not muted */
  906. dac33_write(codec, DAC33_DAC_CTRL_B, DAC33_DACSRCR_RIGHT |
  907. DAC33_DACSRCL_LEFT);
  908. /* C : (defaults) */
  909. dac33_write(codec, DAC33_DAC_CTRL_C, 0x00);
  910. /* 64-65 : L&R DAC power control
  911. Line In -> OUT 1V/V Gain, DAC -> OUT 4V/V Gain*/
  912. dac33_write(codec, DAC33_LDAC_PWR_CTRL, DAC33_LROUT_GAIN(2));
  913. dac33_write(codec, DAC33_RDAC_PWR_CTRL, DAC33_LROUT_GAIN(2));
  914. /* 73 : volume soft stepping control,
  915. clock source = internal osc (?) */
  916. dac33_write(codec, DAC33_ANA_VOL_SOFT_STEP_CTRL, DAC33_VOLCLKEN);
  917. /* 66 : LOP/LOM Modes */
  918. dac33_write(codec, DAC33_OUT_AMP_CM_CTRL, 0xff);
  919. /* 68 : LOM inverted from LOP */
  920. dac33_write(codec, DAC33_OUT_AMP_CTRL, (3<<2));
  921. dac33_write(codec, DAC33_PWR_CTRL, DAC33_PDNALLB);
  922. }
  923. static int dac33_soc_probe(struct platform_device *pdev)
  924. {
  925. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  926. struct snd_soc_codec *codec;
  927. struct tlv320dac33_priv *dac33;
  928. int ret = 0;
  929. BUG_ON(!tlv320dac33_codec);
  930. codec = tlv320dac33_codec;
  931. socdev->card->codec = codec;
  932. dac33 = snd_soc_codec_get_drvdata(codec);
  933. /* Power up the codec */
  934. dac33_hard_power(codec, 1);
  935. /* Set default configuration */
  936. dac33_init_chip(codec);
  937. /* register pcms */
  938. ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
  939. if (ret < 0) {
  940. dev_err(codec->dev, "failed to create pcms\n");
  941. goto pcm_err;
  942. }
  943. snd_soc_add_controls(codec, dac33_snd_controls,
  944. ARRAY_SIZE(dac33_snd_controls));
  945. /* Only add the nSample controls, if we have valid IRQ number */
  946. if (dac33->irq >= 0)
  947. snd_soc_add_controls(codec, dac33_nsample_snd_controls,
  948. ARRAY_SIZE(dac33_nsample_snd_controls));
  949. dac33_add_widgets(codec);
  950. /* power on device */
  951. dac33_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  952. /* Bias level configuration has enabled regulator an extra time */
  953. regulator_bulk_disable(ARRAY_SIZE(dac33->supplies), dac33->supplies);
  954. return 0;
  955. pcm_err:
  956. dac33_hard_power(codec, 0);
  957. return ret;
  958. }
  959. static int dac33_soc_remove(struct platform_device *pdev)
  960. {
  961. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  962. struct snd_soc_codec *codec = socdev->card->codec;
  963. dac33_set_bias_level(codec, SND_SOC_BIAS_OFF);
  964. snd_soc_free_pcms(socdev);
  965. snd_soc_dapm_free(socdev);
  966. return 0;
  967. }
  968. static int dac33_soc_suspend(struct platform_device *pdev, pm_message_t state)
  969. {
  970. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  971. struct snd_soc_codec *codec = socdev->card->codec;
  972. dac33_set_bias_level(codec, SND_SOC_BIAS_OFF);
  973. return 0;
  974. }
  975. static int dac33_soc_resume(struct platform_device *pdev)
  976. {
  977. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  978. struct snd_soc_codec *codec = socdev->card->codec;
  979. dac33_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  980. dac33_set_bias_level(codec, codec->suspend_bias_level);
  981. return 0;
  982. }
  983. struct snd_soc_codec_device soc_codec_dev_tlv320dac33 = {
  984. .probe = dac33_soc_probe,
  985. .remove = dac33_soc_remove,
  986. .suspend = dac33_soc_suspend,
  987. .resume = dac33_soc_resume,
  988. };
  989. EXPORT_SYMBOL_GPL(soc_codec_dev_tlv320dac33);
  990. #define DAC33_RATES (SNDRV_PCM_RATE_44100 | \
  991. SNDRV_PCM_RATE_48000)
  992. #define DAC33_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  993. static struct snd_soc_dai_ops dac33_dai_ops = {
  994. .hw_params = dac33_hw_params,
  995. .prepare = dac33_pcm_prepare,
  996. .trigger = dac33_pcm_trigger,
  997. .set_sysclk = dac33_set_dai_sysclk,
  998. .set_fmt = dac33_set_dai_fmt,
  999. };
  1000. struct snd_soc_dai dac33_dai = {
  1001. .name = "tlv320dac33",
  1002. .playback = {
  1003. .stream_name = "Playback",
  1004. .channels_min = 2,
  1005. .channels_max = 2,
  1006. .rates = DAC33_RATES,
  1007. .formats = DAC33_FORMATS,},
  1008. .ops = &dac33_dai_ops,
  1009. };
  1010. EXPORT_SYMBOL_GPL(dac33_dai);
  1011. static int __devinit dac33_i2c_probe(struct i2c_client *client,
  1012. const struct i2c_device_id *id)
  1013. {
  1014. struct tlv320dac33_platform_data *pdata;
  1015. struct tlv320dac33_priv *dac33;
  1016. struct snd_soc_codec *codec;
  1017. int ret, i;
  1018. if (client->dev.platform_data == NULL) {
  1019. dev_err(&client->dev, "Platform data not set\n");
  1020. return -ENODEV;
  1021. }
  1022. pdata = client->dev.platform_data;
  1023. dac33 = kzalloc(sizeof(struct tlv320dac33_priv), GFP_KERNEL);
  1024. if (dac33 == NULL)
  1025. return -ENOMEM;
  1026. codec = &dac33->codec;
  1027. snd_soc_codec_set_drvdata(codec, dac33);
  1028. codec->control_data = client;
  1029. mutex_init(&codec->mutex);
  1030. mutex_init(&dac33->mutex);
  1031. INIT_LIST_HEAD(&codec->dapm_widgets);
  1032. INIT_LIST_HEAD(&codec->dapm_paths);
  1033. codec->name = "tlv320dac33";
  1034. codec->owner = THIS_MODULE;
  1035. codec->read = dac33_read_reg_cache;
  1036. codec->write = dac33_write_locked;
  1037. codec->hw_write = (hw_write_t) i2c_master_send;
  1038. codec->bias_level = SND_SOC_BIAS_OFF;
  1039. codec->set_bias_level = dac33_set_bias_level;
  1040. codec->dai = &dac33_dai;
  1041. codec->num_dai = 1;
  1042. codec->reg_cache_size = ARRAY_SIZE(dac33_reg);
  1043. codec->reg_cache = kmemdup(dac33_reg, ARRAY_SIZE(dac33_reg),
  1044. GFP_KERNEL);
  1045. if (codec->reg_cache == NULL) {
  1046. ret = -ENOMEM;
  1047. goto error_reg;
  1048. }
  1049. i2c_set_clientdata(client, dac33);
  1050. dac33->power_gpio = pdata->power_gpio;
  1051. dac33->burst_bclkdiv = pdata->burst_bclkdiv;
  1052. dac33->keep_bclk = pdata->keep_bclk;
  1053. dac33->irq = client->irq;
  1054. dac33->nsample = NSAMPLE_MAX;
  1055. dac33->nsample_max = NSAMPLE_MAX;
  1056. /* Disable FIFO use by default */
  1057. dac33->fifo_mode = DAC33_FIFO_BYPASS;
  1058. tlv320dac33_codec = codec;
  1059. codec->dev = &client->dev;
  1060. dac33_dai.dev = codec->dev;
  1061. /* Check if the reset GPIO number is valid and request it */
  1062. if (dac33->power_gpio >= 0) {
  1063. ret = gpio_request(dac33->power_gpio, "tlv320dac33 reset");
  1064. if (ret < 0) {
  1065. dev_err(codec->dev,
  1066. "Failed to request reset GPIO (%d)\n",
  1067. dac33->power_gpio);
  1068. snd_soc_unregister_dai(&dac33_dai);
  1069. snd_soc_unregister_codec(codec);
  1070. goto error_gpio;
  1071. }
  1072. gpio_direction_output(dac33->power_gpio, 0);
  1073. } else {
  1074. dac33->chip_power = 1;
  1075. }
  1076. /* Check if the IRQ number is valid and request it */
  1077. if (dac33->irq >= 0) {
  1078. ret = request_irq(dac33->irq, dac33_interrupt_handler,
  1079. IRQF_TRIGGER_RISING | IRQF_DISABLED,
  1080. codec->name, codec);
  1081. if (ret < 0) {
  1082. dev_err(codec->dev, "Could not request IRQ%d (%d)\n",
  1083. dac33->irq, ret);
  1084. dac33->irq = -1;
  1085. }
  1086. if (dac33->irq != -1) {
  1087. /* Setup work queue */
  1088. dac33->dac33_wq =
  1089. create_singlethread_workqueue("tlv320dac33");
  1090. if (dac33->dac33_wq == NULL) {
  1091. free_irq(dac33->irq, &dac33->codec);
  1092. ret = -ENOMEM;
  1093. goto error_wq;
  1094. }
  1095. INIT_WORK(&dac33->work, dac33_work);
  1096. }
  1097. }
  1098. for (i = 0; i < ARRAY_SIZE(dac33->supplies); i++)
  1099. dac33->supplies[i].supply = dac33_supply_names[i];
  1100. ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(dac33->supplies),
  1101. dac33->supplies);
  1102. if (ret != 0) {
  1103. dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
  1104. goto err_get;
  1105. }
  1106. ret = regulator_bulk_enable(ARRAY_SIZE(dac33->supplies),
  1107. dac33->supplies);
  1108. if (ret != 0) {
  1109. dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
  1110. goto err_enable;
  1111. }
  1112. ret = snd_soc_register_codec(codec);
  1113. if (ret != 0) {
  1114. dev_err(codec->dev, "Failed to register codec: %d\n", ret);
  1115. goto error_codec;
  1116. }
  1117. ret = snd_soc_register_dai(&dac33_dai);
  1118. if (ret != 0) {
  1119. dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
  1120. snd_soc_unregister_codec(codec);
  1121. goto error_codec;
  1122. }
  1123. /* Shut down the codec for now */
  1124. dac33_hard_power(codec, 0);
  1125. return ret;
  1126. error_codec:
  1127. regulator_bulk_disable(ARRAY_SIZE(dac33->supplies), dac33->supplies);
  1128. err_enable:
  1129. regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies);
  1130. err_get:
  1131. if (dac33->irq >= 0) {
  1132. free_irq(dac33->irq, &dac33->codec);
  1133. destroy_workqueue(dac33->dac33_wq);
  1134. }
  1135. error_wq:
  1136. if (dac33->power_gpio >= 0)
  1137. gpio_free(dac33->power_gpio);
  1138. error_gpio:
  1139. kfree(codec->reg_cache);
  1140. error_reg:
  1141. tlv320dac33_codec = NULL;
  1142. kfree(dac33);
  1143. return ret;
  1144. }
  1145. static int __devexit dac33_i2c_remove(struct i2c_client *client)
  1146. {
  1147. struct tlv320dac33_priv *dac33;
  1148. dac33 = i2c_get_clientdata(client);
  1149. dac33_hard_power(&dac33->codec, 0);
  1150. if (dac33->power_gpio >= 0)
  1151. gpio_free(dac33->power_gpio);
  1152. if (dac33->irq >= 0)
  1153. free_irq(dac33->irq, &dac33->codec);
  1154. regulator_bulk_free(ARRAY_SIZE(dac33->supplies), dac33->supplies);
  1155. destroy_workqueue(dac33->dac33_wq);
  1156. snd_soc_unregister_dai(&dac33_dai);
  1157. snd_soc_unregister_codec(&dac33->codec);
  1158. kfree(dac33->codec.reg_cache);
  1159. kfree(dac33);
  1160. tlv320dac33_codec = NULL;
  1161. return 0;
  1162. }
  1163. static const struct i2c_device_id tlv320dac33_i2c_id[] = {
  1164. {
  1165. .name = "tlv320dac33",
  1166. .driver_data = 0,
  1167. },
  1168. { },
  1169. };
  1170. static struct i2c_driver tlv320dac33_i2c_driver = {
  1171. .driver = {
  1172. .name = "tlv320dac33",
  1173. .owner = THIS_MODULE,
  1174. },
  1175. .probe = dac33_i2c_probe,
  1176. .remove = __devexit_p(dac33_i2c_remove),
  1177. .id_table = tlv320dac33_i2c_id,
  1178. };
  1179. static int __init dac33_module_init(void)
  1180. {
  1181. int r;
  1182. r = i2c_add_driver(&tlv320dac33_i2c_driver);
  1183. if (r < 0) {
  1184. printk(KERN_ERR "DAC33: driver registration failed\n");
  1185. return r;
  1186. }
  1187. return 0;
  1188. }
  1189. module_init(dac33_module_init);
  1190. static void __exit dac33_module_exit(void)
  1191. {
  1192. i2c_del_driver(&tlv320dac33_i2c_driver);
  1193. }
  1194. module_exit(dac33_module_exit);
  1195. MODULE_DESCRIPTION("ASoC TLV320DAC33 codec driver");
  1196. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
  1197. MODULE_LICENSE("GPL");