tlv320dac33.c 34 KB

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