patch_ca0132.c 28 KB

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