patch_ca0132.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /*
  2. * HD audio interface patch for Creative CA0132 chip
  3. *
  4. * Copyright (c) 2011, Creative Technology Ltd.
  5. *
  6. * Based on patch_ca0110.c
  7. * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
  8. *
  9. * This driver is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This driver is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/pci.h>
  27. #include <linux/mutex.h>
  28. #include <linux/module.h>
  29. #include <sound/core.h>
  30. #include "hda_codec.h"
  31. #include "hda_local.h"
  32. #define WIDGET_CHIP_CTRL 0x15
  33. #define WIDGET_DSP_CTRL 0x16
  34. #define WUH_MEM_CONNID 10
  35. #define DSP_MEM_CONNID 16
  36. enum hda_cmd_vendor_io {
  37. /* for DspIO node */
  38. VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
  39. VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
  40. VENDOR_DSPIO_STATUS = 0xF01,
  41. VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
  42. VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
  43. VENDOR_DSPIO_DSP_INIT = 0x703,
  44. VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
  45. VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
  46. /* for ChipIO node */
  47. VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
  48. VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
  49. VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
  50. VENDOR_CHIPIO_DATA_LOW = 0x300,
  51. VENDOR_CHIPIO_DATA_HIGH = 0x400,
  52. VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
  53. VENDOR_CHIPIO_STATUS = 0xF01,
  54. VENDOR_CHIPIO_HIC_POST_READ = 0x702,
  55. VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
  56. VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
  57. VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
  58. VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
  59. VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
  60. VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
  61. VENDOR_CHIPIO_FLAG_SET = 0x70F,
  62. VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
  63. VENDOR_CHIPIO_PARAMETER_SET = 0x710,
  64. VENDOR_CHIPIO_PARAMETER_GET = 0xF10,
  65. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
  66. VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
  67. VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
  68. VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
  69. VENDOR_CHIPIO_PARAMETER_EX_ID_GET = 0xF17,
  70. VENDOR_CHIPIO_PARAMETER_EX_ID_SET = 0x717,
  71. VENDOR_CHIPIO_PARAMETER_EX_VALUE_GET = 0xF18,
  72. VENDOR_CHIPIO_PARAMETER_EX_VALUE_SET = 0x718
  73. };
  74. /*
  75. * Control flag IDs
  76. */
  77. enum control_flag_id {
  78. /* Connection manager stream setup is bypassed/enabled */
  79. CONTROL_FLAG_C_MGR = 0,
  80. /* DSP DMA is bypassed/enabled */
  81. CONTROL_FLAG_DMA = 1,
  82. /* 8051 'idle' mode is disabled/enabled */
  83. CONTROL_FLAG_IDLE_ENABLE = 2,
  84. /* Tracker for the SPDIF-in path is bypassed/enabled */
  85. CONTROL_FLAG_TRACKER = 3,
  86. /* DigitalOut to Spdif2Out connection is disabled/enabled */
  87. CONTROL_FLAG_SPDIF2OUT = 4,
  88. /* Digital Microphone is disabled/enabled */
  89. CONTROL_FLAG_DMIC = 5,
  90. /* ADC_B rate is 48 kHz/96 kHz */
  91. CONTROL_FLAG_ADC_B_96KHZ = 6,
  92. /* ADC_C rate is 48 kHz/96 kHz */
  93. CONTROL_FLAG_ADC_C_96KHZ = 7,
  94. /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
  95. CONTROL_FLAG_DAC_96KHZ = 8,
  96. /* DSP rate is 48 kHz/96 kHz */
  97. CONTROL_FLAG_DSP_96KHZ = 9,
  98. /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
  99. CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
  100. /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
  101. CONTROL_FLAG_SRC_RATE_96KHZ = 11,
  102. /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
  103. CONTROL_FLAG_DECODE_LOOP = 12,
  104. /* De-emphasis filter on DAC-1 disabled/enabled */
  105. CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
  106. /* De-emphasis filter on DAC-2 disabled/enabled */
  107. CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
  108. /* De-emphasis filter on DAC-3 disabled/enabled */
  109. CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
  110. /* High-pass filter on ADC_B disabled/enabled */
  111. CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
  112. /* High-pass filter on ADC_C disabled/enabled */
  113. CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
  114. /* Common mode on Port_A disabled/enabled */
  115. CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
  116. /* Common mode on Port_D disabled/enabled */
  117. CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
  118. /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
  119. CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
  120. /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
  121. CONTROL_FLAG_PORT_D_10K0HM_LOAD = 21,
  122. /* ASI rate is 48kHz/96kHz */
  123. CONTROL_FLAG_ASI_96KHZ = 22,
  124. /* DAC power settings able to control attached ports no/yes */
  125. CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
  126. /* Clock Stop OK reporting is disabled/enabled */
  127. CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
  128. /* Number of control flags */
  129. CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
  130. };
  131. /*
  132. * Control parameter IDs
  133. */
  134. enum control_parameter_id {
  135. /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
  136. CONTROL_PARAM_SPDIF1_SOURCE = 2,
  137. /* Stream Control */
  138. /* Select stream with the given ID */
  139. CONTROL_PARAM_STREAM_ID = 24,
  140. /* Source connection point for the selected stream */
  141. CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
  142. /* Destination connection point for the selected stream */
  143. CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
  144. /* Number of audio channels in the selected stream */
  145. CONTROL_PARAM_STREAMS_CHANNELS = 27,
  146. /*Enable control for the selected stream */
  147. CONTROL_PARAM_STREAM_CONTROL = 28,
  148. /* Connection Point Control */
  149. /* Select connection point with the given ID */
  150. CONTROL_PARAM_CONN_POINT_ID = 29,
  151. /* Connection point sample rate */
  152. CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
  153. /* Node Control */
  154. /* Select HDA node with the given ID */
  155. CONTROL_PARAM_NODE_ID = 31
  156. };
  157. /*
  158. * Dsp Io Status codes
  159. */
  160. enum hda_vendor_status_dspio {
  161. /* Success */
  162. VENDOR_STATUS_DSPIO_OK = 0x00,
  163. /* Busy, unable to accept new command, the host must retry */
  164. VENDOR_STATUS_DSPIO_BUSY = 0x01,
  165. /* SCP command queue is full */
  166. VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
  167. /* SCP response queue is empty */
  168. VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
  169. };
  170. /*
  171. * Chip Io Status codes
  172. */
  173. enum hda_vendor_status_chipio {
  174. /* Success */
  175. VENDOR_STATUS_CHIPIO_OK = 0x00,
  176. /* Busy, unable to accept new command, the host must retry */
  177. VENDOR_STATUS_CHIPIO_BUSY = 0x01
  178. };
  179. /*
  180. * CA0132 sample rate
  181. */
  182. enum ca0132_sample_rate {
  183. SR_6_000 = 0x00,
  184. SR_8_000 = 0x01,
  185. SR_9_600 = 0x02,
  186. SR_11_025 = 0x03,
  187. SR_16_000 = 0x04,
  188. SR_22_050 = 0x05,
  189. SR_24_000 = 0x06,
  190. SR_32_000 = 0x07,
  191. SR_44_100 = 0x08,
  192. SR_48_000 = 0x09,
  193. SR_88_200 = 0x0A,
  194. SR_96_000 = 0x0B,
  195. SR_144_000 = 0x0C,
  196. SR_176_400 = 0x0D,
  197. SR_192_000 = 0x0E,
  198. SR_384_000 = 0x0F,
  199. SR_COUNT = 0x10,
  200. SR_RATE_UNKNOWN = 0x1F
  201. };
  202. /*
  203. * Scp Helper function
  204. */
  205. enum get_set {
  206. IS_SET = 0,
  207. IS_GET = 1,
  208. };
  209. /*
  210. * Duplicated from ca0110 codec
  211. */
  212. static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
  213. {
  214. if (pin) {
  215. snd_hda_codec_write(codec, pin, 0,
  216. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
  217. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  218. snd_hda_codec_write(codec, pin, 0,
  219. AC_VERB_SET_AMP_GAIN_MUTE,
  220. AMP_OUT_UNMUTE);
  221. }
  222. if (dac)
  223. snd_hda_codec_write(codec, dac, 0,
  224. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
  225. }
  226. static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
  227. {
  228. if (pin) {
  229. snd_hda_codec_write(codec, pin, 0,
  230. AC_VERB_SET_PIN_WIDGET_CONTROL,
  231. PIN_VREF80);
  232. if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
  233. snd_hda_codec_write(codec, pin, 0,
  234. AC_VERB_SET_AMP_GAIN_MUTE,
  235. AMP_IN_UNMUTE(0));
  236. }
  237. if (adc)
  238. snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  239. AMP_IN_UNMUTE(0));
  240. }
  241. static char *dirstr[2] = { "Playback", "Capture" };
  242. static int _add_switch(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
  243. int chan, int dir)
  244. {
  245. char namestr[44];
  246. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  247. struct snd_kcontrol_new knew =
  248. HDA_CODEC_MUTE_MONO(namestr, nid, chan, 0, type);
  249. sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
  250. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  251. }
  252. static int _add_volume(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
  253. int chan, int dir)
  254. {
  255. char namestr[44];
  256. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  257. struct snd_kcontrol_new knew =
  258. HDA_CODEC_VOLUME_MONO(namestr, nid, chan, 0, type);
  259. sprintf(namestr, "%s %s Volume", pfx, dirstr[dir]);
  260. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  261. }
  262. #define add_out_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 0)
  263. #define add_out_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 0)
  264. #define add_in_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 1)
  265. #define add_in_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 1)
  266. #define add_mono_switch(codec, nid, pfx, chan) \
  267. _add_switch(codec, nid, pfx, chan, 0)
  268. #define add_mono_volume(codec, nid, pfx, chan) \
  269. _add_volume(codec, nid, pfx, chan, 0)
  270. #define add_in_mono_switch(codec, nid, pfx, chan) \
  271. _add_switch(codec, nid, pfx, chan, 1)
  272. #define add_in_mono_volume(codec, nid, pfx, chan) \
  273. _add_volume(codec, nid, pfx, chan, 1)
  274. /*
  275. * CA0132 specific
  276. */
  277. struct ca0132_spec {
  278. struct auto_pin_cfg autocfg;
  279. struct hda_multi_out multiout;
  280. hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
  281. hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
  282. hda_nid_t hp_dac;
  283. hda_nid_t input_pins[AUTO_PIN_LAST];
  284. hda_nid_t adcs[AUTO_PIN_LAST];
  285. hda_nid_t dig_out;
  286. hda_nid_t dig_in;
  287. unsigned int num_inputs;
  288. long curr_hp_switch;
  289. long curr_hp_volume[2];
  290. long curr_speaker_switch;
  291. struct mutex chipio_mutex;
  292. const char *input_labels[AUTO_PIN_LAST];
  293. struct hda_pcm pcm_rec[2]; /* PCM information */
  294. };
  295. /* Chip access helper function */
  296. static int chipio_send(struct hda_codec *codec,
  297. unsigned int reg,
  298. unsigned int data)
  299. {
  300. unsigned int res;
  301. int retry = 50;
  302. /* send bits of data specified by reg */
  303. do {
  304. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  305. reg, data);
  306. if (res == VENDOR_STATUS_CHIPIO_OK)
  307. return 0;
  308. } while (--retry);
  309. return -EIO;
  310. }
  311. /*
  312. * Write chip address through the vendor widget -- NOT protected by the Mutex!
  313. */
  314. static int chipio_write_address(struct hda_codec *codec,
  315. unsigned int chip_addx)
  316. {
  317. int res;
  318. /* send low 16 bits of the address */
  319. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
  320. chip_addx & 0xffff);
  321. if (res != -EIO) {
  322. /* send high 16 bits of the address */
  323. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
  324. chip_addx >> 16);
  325. }
  326. return res;
  327. }
  328. /*
  329. * Write data through the vendor widget -- NOT protected by the Mutex!
  330. */
  331. static int chipio_write_data(struct hda_codec *codec, unsigned int data)
  332. {
  333. int res;
  334. /* send low 16 bits of the data */
  335. res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
  336. if (res != -EIO) {
  337. /* send high 16 bits of the data */
  338. res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
  339. data >> 16);
  340. }
  341. return res;
  342. }
  343. /*
  344. * Read data through the vendor widget -- NOT protected by the Mutex!
  345. */
  346. static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
  347. {
  348. int res;
  349. /* post read */
  350. res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
  351. if (res != -EIO) {
  352. /* read status */
  353. res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  354. }
  355. if (res != -EIO) {
  356. /* read data */
  357. *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  358. VENDOR_CHIPIO_HIC_READ_DATA,
  359. 0);
  360. }
  361. return res;
  362. }
  363. /*
  364. * Write given value to the given address through the chip I/O widget.
  365. * protected by the Mutex
  366. */
  367. static int chipio_write(struct hda_codec *codec,
  368. unsigned int chip_addx, const unsigned int data)
  369. {
  370. struct ca0132_spec *spec = codec->spec;
  371. int err;
  372. mutex_lock(&spec->chipio_mutex);
  373. /* write the address, and if successful proceed to write data */
  374. err = chipio_write_address(codec, chip_addx);
  375. if (err < 0)
  376. goto exit;
  377. err = chipio_write_data(codec, data);
  378. if (err < 0)
  379. goto exit;
  380. exit:
  381. mutex_unlock(&spec->chipio_mutex);
  382. return err;
  383. }
  384. /*
  385. * Read the given address through the chip I/O widget
  386. * protected by the Mutex
  387. */
  388. static int chipio_read(struct hda_codec *codec,
  389. unsigned int chip_addx, unsigned int *data)
  390. {
  391. struct ca0132_spec *spec = codec->spec;
  392. int err;
  393. mutex_lock(&spec->chipio_mutex);
  394. /* write the address, and if successful proceed to write data */
  395. err = chipio_write_address(codec, chip_addx);
  396. if (err < 0)
  397. goto exit;
  398. err = chipio_read_data(codec, data);
  399. if (err < 0)
  400. goto exit;
  401. exit:
  402. mutex_unlock(&spec->chipio_mutex);
  403. return err;
  404. }
  405. /*
  406. * PCM stuffs
  407. */
  408. static void ca0132_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  409. u32 stream_tag,
  410. int channel_id, int format)
  411. {
  412. unsigned int oldval, newval;
  413. if (!nid)
  414. return;
  415. snd_printdd("ca0132_setup_stream: "
  416. "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  417. nid, stream_tag, channel_id, format);
  418. /* update the format-id if changed */
  419. oldval = snd_hda_codec_read(codec, nid, 0,
  420. AC_VERB_GET_STREAM_FORMAT,
  421. 0);
  422. if (oldval != format) {
  423. msleep(20);
  424. snd_hda_codec_write(codec, nid, 0,
  425. AC_VERB_SET_STREAM_FORMAT,
  426. format);
  427. }
  428. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  429. newval = (stream_tag << 4) | channel_id;
  430. if (oldval != newval) {
  431. snd_hda_codec_write(codec, nid, 0,
  432. AC_VERB_SET_CHANNEL_STREAMID,
  433. newval);
  434. }
  435. }
  436. static void ca0132_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
  437. {
  438. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
  439. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  440. }
  441. /*
  442. * PCM callbacks
  443. */
  444. static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  445. struct hda_codec *codec,
  446. unsigned int stream_tag,
  447. unsigned int format,
  448. struct snd_pcm_substream *substream)
  449. {
  450. struct ca0132_spec *spec = codec->spec;
  451. ca0132_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
  452. return 0;
  453. }
  454. static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  455. struct hda_codec *codec,
  456. struct snd_pcm_substream *substream)
  457. {
  458. struct ca0132_spec *spec = codec->spec;
  459. ca0132_cleanup_stream(codec, spec->dacs[0]);
  460. return 0;
  461. }
  462. /*
  463. * Digital out
  464. */
  465. static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  466. struct hda_codec *codec,
  467. unsigned int stream_tag,
  468. unsigned int format,
  469. struct snd_pcm_substream *substream)
  470. {
  471. struct ca0132_spec *spec = codec->spec;
  472. ca0132_setup_stream(codec, spec->dig_out, stream_tag, 0, format);
  473. return 0;
  474. }
  475. static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  476. struct hda_codec *codec,
  477. struct snd_pcm_substream *substream)
  478. {
  479. struct ca0132_spec *spec = codec->spec;
  480. ca0132_cleanup_stream(codec, spec->dig_out);
  481. return 0;
  482. }
  483. /*
  484. * Analog capture
  485. */
  486. static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  487. struct hda_codec *codec,
  488. unsigned int stream_tag,
  489. unsigned int format,
  490. struct snd_pcm_substream *substream)
  491. {
  492. struct ca0132_spec *spec = codec->spec;
  493. ca0132_setup_stream(codec, spec->adcs[substream->number],
  494. stream_tag, 0, format);
  495. return 0;
  496. }
  497. static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  498. struct hda_codec *codec,
  499. struct snd_pcm_substream *substream)
  500. {
  501. struct ca0132_spec *spec = codec->spec;
  502. ca0132_cleanup_stream(codec, spec->adcs[substream->number]);
  503. return 0;
  504. }
  505. /*
  506. * Digital capture
  507. */
  508. static int ca0132_dig_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  509. struct hda_codec *codec,
  510. unsigned int stream_tag,
  511. unsigned int format,
  512. struct snd_pcm_substream *substream)
  513. {
  514. struct ca0132_spec *spec = codec->spec;
  515. ca0132_setup_stream(codec, spec->dig_in, stream_tag, 0, format);
  516. return 0;
  517. }
  518. static int ca0132_dig_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  519. struct hda_codec *codec,
  520. struct snd_pcm_substream *substream)
  521. {
  522. struct ca0132_spec *spec = codec->spec;
  523. ca0132_cleanup_stream(codec, spec->dig_in);
  524. return 0;
  525. }
  526. /*
  527. */
  528. static struct hda_pcm_stream ca0132_pcm_analog_playback = {
  529. .substreams = 1,
  530. .channels_min = 2,
  531. .channels_max = 2,
  532. .ops = {
  533. .prepare = ca0132_playback_pcm_prepare,
  534. .cleanup = ca0132_playback_pcm_cleanup
  535. },
  536. };
  537. static struct hda_pcm_stream ca0132_pcm_analog_capture = {
  538. .substreams = 1,
  539. .channels_min = 2,
  540. .channels_max = 2,
  541. .ops = {
  542. .prepare = ca0132_capture_pcm_prepare,
  543. .cleanup = ca0132_capture_pcm_cleanup
  544. },
  545. };
  546. static struct hda_pcm_stream ca0132_pcm_digital_playback = {
  547. .substreams = 1,
  548. .channels_min = 2,
  549. .channels_max = 2,
  550. .ops = {
  551. .prepare = ca0132_dig_playback_pcm_prepare,
  552. .cleanup = ca0132_dig_playback_pcm_cleanup
  553. },
  554. };
  555. static struct hda_pcm_stream ca0132_pcm_digital_capture = {
  556. .substreams = 1,
  557. .channels_min = 2,
  558. .channels_max = 2,
  559. .ops = {
  560. .prepare = ca0132_dig_capture_pcm_prepare,
  561. .cleanup = ca0132_dig_capture_pcm_cleanup
  562. },
  563. };
  564. static int ca0132_build_pcms(struct hda_codec *codec)
  565. {
  566. struct ca0132_spec *spec = codec->spec;
  567. struct hda_pcm *info = spec->pcm_rec;
  568. codec->pcm_info = info;
  569. codec->num_pcms = 0;
  570. info->name = "CA0132 Analog";
  571. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
  572. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
  573. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  574. spec->multiout.max_channels;
  575. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  576. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_inputs;
  577. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  578. codec->num_pcms++;
  579. if (!spec->dig_out && !spec->dig_in)
  580. return 0;
  581. info++;
  582. info->name = "CA0132 Digital";
  583. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  584. if (spec->dig_out) {
  585. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  586. ca0132_pcm_digital_playback;
  587. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  588. }
  589. if (spec->dig_in) {
  590. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  591. ca0132_pcm_digital_capture;
  592. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  593. }
  594. codec->num_pcms++;
  595. return 0;
  596. }
  597. #define REG_CODEC_MUTE 0x18b014
  598. #define REG_CODEC_HP_VOL_L 0x18b070
  599. #define REG_CODEC_HP_VOL_R 0x18b074
  600. static int ca0132_hp_switch_get(struct snd_kcontrol *kcontrol,
  601. struct snd_ctl_elem_value *ucontrol)
  602. {
  603. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  604. struct ca0132_spec *spec = codec->spec;
  605. long *valp = ucontrol->value.integer.value;
  606. *valp = spec->curr_hp_switch;
  607. return 0;
  608. }
  609. static int ca0132_hp_switch_put(struct snd_kcontrol *kcontrol,
  610. struct snd_ctl_elem_value *ucontrol)
  611. {
  612. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  613. struct ca0132_spec *spec = codec->spec;
  614. long *valp = ucontrol->value.integer.value;
  615. unsigned int data;
  616. int err;
  617. /* any change? */
  618. if (spec->curr_hp_switch == *valp)
  619. return 0;
  620. snd_hda_power_up(codec);
  621. err = chipio_read(codec, REG_CODEC_MUTE, &data);
  622. if (err < 0)
  623. goto exit;
  624. /* *valp 0 is mute, 1 is unmute */
  625. data = (data & 0x7f) | (*valp ? 0 : 0x80);
  626. err = chipio_write(codec, REG_CODEC_MUTE, data);
  627. if (err < 0)
  628. goto exit;
  629. spec->curr_hp_switch = *valp;
  630. exit:
  631. snd_hda_power_down(codec);
  632. return err < 0 ? err : 1;
  633. }
  634. static int ca0132_speaker_switch_get(struct snd_kcontrol *kcontrol,
  635. struct snd_ctl_elem_value *ucontrol)
  636. {
  637. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  638. struct ca0132_spec *spec = codec->spec;
  639. long *valp = ucontrol->value.integer.value;
  640. *valp = spec->curr_speaker_switch;
  641. return 0;
  642. }
  643. static int ca0132_speaker_switch_put(struct snd_kcontrol *kcontrol,
  644. struct snd_ctl_elem_value *ucontrol)
  645. {
  646. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  647. struct ca0132_spec *spec = codec->spec;
  648. long *valp = ucontrol->value.integer.value;
  649. unsigned int data;
  650. int err;
  651. /* any change? */
  652. if (spec->curr_speaker_switch == *valp)
  653. return 0;
  654. snd_hda_power_up(codec);
  655. err = chipio_read(codec, REG_CODEC_MUTE, &data);
  656. if (err < 0)
  657. goto exit;
  658. /* *valp 0 is mute, 1 is unmute */
  659. data = (data & 0xef) | (*valp ? 0 : 0x10);
  660. err = chipio_write(codec, REG_CODEC_MUTE, data);
  661. if (err < 0)
  662. goto exit;
  663. spec->curr_speaker_switch = *valp;
  664. exit:
  665. snd_hda_power_down(codec);
  666. return err < 0 ? err : 1;
  667. }
  668. static int ca0132_hp_volume_get(struct snd_kcontrol *kcontrol,
  669. struct snd_ctl_elem_value *ucontrol)
  670. {
  671. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  672. struct ca0132_spec *spec = codec->spec;
  673. long *valp = ucontrol->value.integer.value;
  674. *valp++ = spec->curr_hp_volume[0];
  675. *valp = spec->curr_hp_volume[1];
  676. return 0;
  677. }
  678. static int ca0132_hp_volume_put(struct snd_kcontrol *kcontrol,
  679. struct snd_ctl_elem_value *ucontrol)
  680. {
  681. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  682. struct ca0132_spec *spec = codec->spec;
  683. long *valp = ucontrol->value.integer.value;
  684. long left_vol, right_vol;
  685. unsigned int data;
  686. int val;
  687. int err;
  688. left_vol = *valp++;
  689. right_vol = *valp;
  690. /* any change? */
  691. if ((spec->curr_hp_volume[0] == left_vol) &&
  692. (spec->curr_hp_volume[1] == right_vol))
  693. return 0;
  694. snd_hda_power_up(codec);
  695. err = chipio_read(codec, REG_CODEC_HP_VOL_L, &data);
  696. if (err < 0)
  697. goto exit;
  698. val = 31 - left_vol;
  699. data = (data & 0xe0) | val;
  700. err = chipio_write(codec, REG_CODEC_HP_VOL_L, data);
  701. if (err < 0)
  702. goto exit;
  703. val = 31 - right_vol;
  704. data = (data & 0xe0) | val;
  705. err = chipio_write(codec, REG_CODEC_HP_VOL_R, data);
  706. if (err < 0)
  707. goto exit;
  708. spec->curr_hp_volume[0] = left_vol;
  709. spec->curr_hp_volume[1] = right_vol;
  710. exit:
  711. snd_hda_power_down(codec);
  712. return err < 0 ? err : 1;
  713. }
  714. static int add_hp_switch(struct hda_codec *codec, hda_nid_t nid)
  715. {
  716. struct snd_kcontrol_new knew =
  717. HDA_CODEC_MUTE_MONO("Headphone Playback Switch",
  718. nid, 1, 0, HDA_OUTPUT);
  719. knew.get = ca0132_hp_switch_get;
  720. knew.put = ca0132_hp_switch_put;
  721. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  722. }
  723. static int add_hp_volume(struct hda_codec *codec, hda_nid_t nid)
  724. {
  725. struct snd_kcontrol_new knew =
  726. HDA_CODEC_VOLUME_MONO("Headphone Playback Volume",
  727. nid, 3, 0, HDA_OUTPUT);
  728. knew.get = ca0132_hp_volume_get;
  729. knew.put = ca0132_hp_volume_put;
  730. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  731. }
  732. static int add_speaker_switch(struct hda_codec *codec, hda_nid_t nid)
  733. {
  734. struct snd_kcontrol_new knew =
  735. HDA_CODEC_MUTE_MONO("Speaker Playback Switch",
  736. nid, 1, 0, HDA_OUTPUT);
  737. knew.get = ca0132_speaker_switch_get;
  738. knew.put = ca0132_speaker_switch_put;
  739. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  740. }
  741. static void ca0132_fix_hp_caps(struct hda_codec *codec)
  742. {
  743. struct ca0132_spec *spec = codec->spec;
  744. struct auto_pin_cfg *cfg = &spec->autocfg;
  745. unsigned int caps;
  746. /* set mute-capable, 1db step, 32 steps, ofs 6 */
  747. caps = 0x80031f06;
  748. snd_hda_override_amp_caps(codec, cfg->hp_pins[0], HDA_OUTPUT, caps);
  749. }
  750. static int ca0132_build_controls(struct hda_codec *codec)
  751. {
  752. struct ca0132_spec *spec = codec->spec;
  753. struct auto_pin_cfg *cfg = &spec->autocfg;
  754. int i, err;
  755. if (spec->multiout.num_dacs) {
  756. err = add_speaker_switch(codec, spec->out_pins[0]);
  757. if (err < 0)
  758. return err;
  759. }
  760. if (cfg->hp_outs) {
  761. ca0132_fix_hp_caps(codec);
  762. err = add_hp_switch(codec, cfg->hp_pins[0]);
  763. if (err < 0)
  764. return err;
  765. err = add_hp_volume(codec, cfg->hp_pins[0]);
  766. if (err < 0)
  767. return err;
  768. }
  769. for (i = 0; i < spec->num_inputs; i++) {
  770. const char *label = spec->input_labels[i];
  771. err = add_in_switch(codec, spec->adcs[i], label);
  772. if (err < 0)
  773. return err;
  774. err = add_in_volume(codec, spec->adcs[i], label);
  775. if (err < 0)
  776. return err;
  777. if (cfg->inputs[i].type == AUTO_PIN_MIC) {
  778. /* add Mic-Boost */
  779. err = add_in_mono_volume(codec, spec->input_pins[i],
  780. "Mic Boost", 1);
  781. if (err < 0)
  782. return err;
  783. }
  784. }
  785. if (spec->dig_out) {
  786. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  787. spec->dig_out);
  788. if (err < 0)
  789. return err;
  790. err = add_out_volume(codec, spec->dig_out, "IEC958");
  791. if (err < 0)
  792. return err;
  793. }
  794. if (spec->dig_in) {
  795. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  796. if (err < 0)
  797. return err;
  798. err = add_in_volume(codec, spec->dig_in, "IEC958");
  799. if (err < 0)
  800. return err;
  801. }
  802. return 0;
  803. }
  804. static void ca0132_set_ct_ext(struct hda_codec *codec, int enable)
  805. {
  806. /* Set Creative extension */
  807. snd_printdd("SET CREATIVE EXTENSION\n");
  808. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  809. VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE,
  810. enable);
  811. msleep(20);
  812. }
  813. static void ca0132_config(struct hda_codec *codec)
  814. {
  815. struct ca0132_spec *spec = codec->spec;
  816. struct auto_pin_cfg *cfg = &spec->autocfg;
  817. /* line-outs */
  818. cfg->line_outs = 1;
  819. cfg->line_out_pins[0] = 0x0b; /* front */
  820. cfg->line_out_type = AUTO_PIN_LINE_OUT;
  821. spec->dacs[0] = 0x02;
  822. spec->out_pins[0] = 0x0b;
  823. spec->multiout.dac_nids = spec->dacs;
  824. spec->multiout.num_dacs = 1;
  825. spec->multiout.max_channels = 2;
  826. /* headphone */
  827. cfg->hp_outs = 1;
  828. cfg->hp_pins[0] = 0x0f;
  829. spec->hp_dac = 0;
  830. spec->multiout.hp_nid = 0;
  831. /* inputs */
  832. cfg->num_inputs = 2; /* Mic-in and line-in */
  833. cfg->inputs[0].pin = 0x12;
  834. cfg->inputs[0].type = AUTO_PIN_MIC;
  835. cfg->inputs[1].pin = 0x11;
  836. cfg->inputs[1].type = AUTO_PIN_LINE_IN;
  837. /* Mic-in */
  838. spec->input_pins[0] = 0x12;
  839. spec->input_labels[0] = "Mic-In";
  840. spec->adcs[0] = 0x07;
  841. /* Line-In */
  842. spec->input_pins[1] = 0x11;
  843. spec->input_labels[1] = "Line-In";
  844. spec->adcs[1] = 0x08;
  845. spec->num_inputs = 2;
  846. }
  847. static void ca0132_init_chip(struct hda_codec *codec)
  848. {
  849. struct ca0132_spec *spec = codec->spec;
  850. mutex_init(&spec->chipio_mutex);
  851. }
  852. static void ca0132_exit_chip(struct hda_codec *codec)
  853. {
  854. /* put any chip cleanup stuffs here. */
  855. }
  856. static int ca0132_init(struct hda_codec *codec)
  857. {
  858. struct ca0132_spec *spec = codec->spec;
  859. struct auto_pin_cfg *cfg = &spec->autocfg;
  860. int i;
  861. for (i = 0; i < spec->multiout.num_dacs; i++) {
  862. init_output(codec, spec->out_pins[i],
  863. spec->multiout.dac_nids[i]);
  864. }
  865. init_output(codec, cfg->hp_pins[0], spec->hp_dac);
  866. init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
  867. for (i = 0; i < spec->num_inputs; i++)
  868. init_input(codec, spec->input_pins[i], spec->adcs[i]);
  869. init_input(codec, cfg->dig_in_pin, spec->dig_in);
  870. ca0132_set_ct_ext(codec, 1);
  871. return 0;
  872. }
  873. static void ca0132_free(struct hda_codec *codec)
  874. {
  875. ca0132_set_ct_ext(codec, 0);
  876. ca0132_exit_chip(codec);
  877. kfree(codec->spec);
  878. }
  879. static struct hda_codec_ops ca0132_patch_ops = {
  880. .build_controls = ca0132_build_controls,
  881. .build_pcms = ca0132_build_pcms,
  882. .init = ca0132_init,
  883. .free = ca0132_free,
  884. };
  885. static int patch_ca0132(struct hda_codec *codec)
  886. {
  887. struct ca0132_spec *spec;
  888. snd_printdd("patch_ca0132\n");
  889. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  890. if (!spec)
  891. return -ENOMEM;
  892. codec->spec = spec;
  893. ca0132_init_chip(codec);
  894. ca0132_config(codec);
  895. codec->patch_ops = ca0132_patch_ops;
  896. return 0;
  897. }
  898. /*
  899. * patch entries
  900. */
  901. static struct hda_codec_preset snd_hda_preset_ca0132[] = {
  902. { .id = 0x11020011, .name = "CA0132", .patch = patch_ca0132 },
  903. {} /* terminator */
  904. };
  905. MODULE_ALIAS("snd-hda-codec-id:11020011");
  906. MODULE_LICENSE("GPL");
  907. MODULE_DESCRIPTION("Creative CA0132, CA0132 HD-audio codec");
  908. static struct hda_codec_preset_list ca0132_list = {
  909. .preset = snd_hda_preset_ca0132,
  910. .owner = THIS_MODULE,
  911. };
  912. static int __init patch_ca0132_init(void)
  913. {
  914. return snd_hda_add_codec_preset(&ca0132_list);
  915. }
  916. static void __exit patch_ca0132_exit(void)
  917. {
  918. snd_hda_delete_codec_preset(&ca0132_list);
  919. }
  920. module_init(patch_ca0132_init)
  921. module_exit(patch_ca0132_exit)