dac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Copyright 2003 NVIDIA, Corporation
  3. * Copyright 2006 Dave Airlie
  4. * Copyright 2007 Maarten Maathuis
  5. * Copyright 2007-2009 Stuart Bennett
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the next
  15. * paragraph) shall be included in all copies or substantial portions of the
  16. * Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/drm_crtc_helper.h>
  28. #include "nouveau_drm.h"
  29. #include "nouveau_encoder.h"
  30. #include "nouveau_connector.h"
  31. #include "nouveau_crtc.h"
  32. #include "hw.h"
  33. #include "nvreg.h"
  34. #include <subdev/bios/gpio.h>
  35. #include <subdev/gpio.h>
  36. #include <subdev/timer.h>
  37. int nv04_dac_output_offset(struct drm_encoder *encoder)
  38. {
  39. struct dcb_output *dcb = nouveau_encoder(encoder)->dcb;
  40. int offset = 0;
  41. if (dcb->or & (8 | DCB_OUTPUT_C))
  42. offset += 0x68;
  43. if (dcb->or & (8 | DCB_OUTPUT_B))
  44. offset += 0x2000;
  45. return offset;
  46. }
  47. /*
  48. * arbitrary limit to number of sense oscillations tolerated in one sample
  49. * period (observed to be at least 13 in "nvidia")
  50. */
  51. #define MAX_HBLANK_OSC 20
  52. /*
  53. * arbitrary limit to number of conflicting sample pairs to tolerate at a
  54. * voltage step (observed to be at least 5 in "nvidia")
  55. */
  56. #define MAX_SAMPLE_PAIRS 10
  57. static int sample_load_twice(struct drm_device *dev, bool sense[2])
  58. {
  59. struct nouveau_drm *drm = nouveau_drm(dev);
  60. struct nvif_object *device = &drm->device.object;
  61. int i;
  62. for (i = 0; i < 2; i++) {
  63. bool sense_a, sense_b, sense_b_prime;
  64. int j = 0;
  65. /*
  66. * wait for bit 0 clear -- out of hblank -- (say reg value 0x4),
  67. * then wait for transition 0x4->0x5->0x4: enter hblank, leave
  68. * hblank again
  69. * use a 10ms timeout (guards against crtc being inactive, in
  70. * which case blank state would never change)
  71. */
  72. if (nvif_msec(&drm->device, 10,
  73. if (!(nvif_rd32(device, NV_PRMCIO_INP0__COLOR) & 1))
  74. break;
  75. ) < 0)
  76. return -EBUSY;
  77. if (nvif_msec(&drm->device, 10,
  78. if ( (nvif_rd32(device, NV_PRMCIO_INP0__COLOR) & 1))
  79. break;
  80. ) < 0)
  81. return -EBUSY;
  82. if (nvif_msec(&drm->device, 10,
  83. if (!(nvif_rd32(device, NV_PRMCIO_INP0__COLOR) & 1))
  84. break;
  85. ) < 0)
  86. return -EBUSY;
  87. udelay(100);
  88. /* when level triggers, sense is _LO_ */
  89. sense_a = nvif_rd08(device, NV_PRMCIO_INP0) & 0x10;
  90. /* take another reading until it agrees with sense_a... */
  91. do {
  92. udelay(100);
  93. sense_b = nvif_rd08(device, NV_PRMCIO_INP0) & 0x10;
  94. if (sense_a != sense_b) {
  95. sense_b_prime =
  96. nvif_rd08(device, NV_PRMCIO_INP0) & 0x10;
  97. if (sense_b == sense_b_prime) {
  98. /* ... unless two consecutive subsequent
  99. * samples agree; sense_a is replaced */
  100. sense_a = sense_b;
  101. /* force mis-match so we loop */
  102. sense_b = !sense_a;
  103. }
  104. }
  105. } while ((sense_a != sense_b) && ++j < MAX_HBLANK_OSC);
  106. if (j == MAX_HBLANK_OSC)
  107. /* with so much oscillation, default to sense:LO */
  108. sense[i] = false;
  109. else
  110. sense[i] = sense_a;
  111. }
  112. return 0;
  113. }
  114. static enum drm_connector_status nv04_dac_detect(struct drm_encoder *encoder,
  115. struct drm_connector *connector)
  116. {
  117. struct drm_device *dev = encoder->dev;
  118. struct nvif_object *device = &nouveau_drm(dev)->device.object;
  119. struct nouveau_drm *drm = nouveau_drm(dev);
  120. uint8_t saved_seq1, saved_pi, saved_rpc1, saved_cr_mode;
  121. uint8_t saved_palette0[3], saved_palette_mask;
  122. uint32_t saved_rtest_ctrl, saved_rgen_ctrl;
  123. int i;
  124. uint8_t blue;
  125. bool sense = true;
  126. /*
  127. * for this detection to work, there needs to be a mode set up on the
  128. * CRTC. this is presumed to be the case
  129. */
  130. if (nv_two_heads(dev))
  131. /* only implemented for head A for now */
  132. NVSetOwner(dev, 0);
  133. saved_cr_mode = NVReadVgaCrtc(dev, 0, NV_CIO_CR_MODE_INDEX);
  134. NVWriteVgaCrtc(dev, 0, NV_CIO_CR_MODE_INDEX, saved_cr_mode | 0x80);
  135. saved_seq1 = NVReadVgaSeq(dev, 0, NV_VIO_SR_CLOCK_INDEX);
  136. NVWriteVgaSeq(dev, 0, NV_VIO_SR_CLOCK_INDEX, saved_seq1 & ~0x20);
  137. saved_rtest_ctrl = NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL);
  138. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL,
  139. saved_rtest_ctrl & ~NV_PRAMDAC_TEST_CONTROL_PWRDWN_DAC_OFF);
  140. msleep(10);
  141. saved_pi = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_PIXEL_INDEX);
  142. NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_PIXEL_INDEX,
  143. saved_pi & ~(0x80 | MASK(NV_CIO_CRE_PIXEL_FORMAT)));
  144. saved_rpc1 = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_RPC1_INDEX);
  145. NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_RPC1_INDEX, saved_rpc1 & ~0xc0);
  146. nvif_wr08(device, NV_PRMDIO_READ_MODE_ADDRESS, 0x0);
  147. for (i = 0; i < 3; i++)
  148. saved_palette0[i] = nvif_rd08(device, NV_PRMDIO_PALETTE_DATA);
  149. saved_palette_mask = nvif_rd08(device, NV_PRMDIO_PIXEL_MASK);
  150. nvif_wr08(device, NV_PRMDIO_PIXEL_MASK, 0);
  151. saved_rgen_ctrl = NVReadRAMDAC(dev, 0, NV_PRAMDAC_GENERAL_CONTROL);
  152. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_GENERAL_CONTROL,
  153. (saved_rgen_ctrl & ~(NV_PRAMDAC_GENERAL_CONTROL_BPC_8BITS |
  154. NV_PRAMDAC_GENERAL_CONTROL_TERMINATION_75OHM)) |
  155. NV_PRAMDAC_GENERAL_CONTROL_PIXMIX_ON);
  156. blue = 8; /* start of test range */
  157. do {
  158. bool sense_pair[2];
  159. nvif_wr08(device, NV_PRMDIO_WRITE_MODE_ADDRESS, 0);
  160. nvif_wr08(device, NV_PRMDIO_PALETTE_DATA, 0);
  161. nvif_wr08(device, NV_PRMDIO_PALETTE_DATA, 0);
  162. /* testing blue won't find monochrome monitors. I don't care */
  163. nvif_wr08(device, NV_PRMDIO_PALETTE_DATA, blue);
  164. i = 0;
  165. /* take sample pairs until both samples in the pair agree */
  166. do {
  167. if (sample_load_twice(dev, sense_pair))
  168. goto out;
  169. } while ((sense_pair[0] != sense_pair[1]) &&
  170. ++i < MAX_SAMPLE_PAIRS);
  171. if (i == MAX_SAMPLE_PAIRS)
  172. /* too much oscillation defaults to LO */
  173. sense = false;
  174. else
  175. sense = sense_pair[0];
  176. /*
  177. * if sense goes LO before blue ramps to 0x18, monitor is not connected.
  178. * ergo, if blue gets to 0x18, monitor must be connected
  179. */
  180. } while (++blue < 0x18 && sense);
  181. out:
  182. nvif_wr08(device, NV_PRMDIO_PIXEL_MASK, saved_palette_mask);
  183. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_GENERAL_CONTROL, saved_rgen_ctrl);
  184. nvif_wr08(device, NV_PRMDIO_WRITE_MODE_ADDRESS, 0);
  185. for (i = 0; i < 3; i++)
  186. nvif_wr08(device, NV_PRMDIO_PALETTE_DATA, saved_palette0[i]);
  187. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL, saved_rtest_ctrl);
  188. NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_PIXEL_INDEX, saved_pi);
  189. NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_RPC1_INDEX, saved_rpc1);
  190. NVWriteVgaSeq(dev, 0, NV_VIO_SR_CLOCK_INDEX, saved_seq1);
  191. NVWriteVgaCrtc(dev, 0, NV_CIO_CR_MODE_INDEX, saved_cr_mode);
  192. if (blue == 0x18) {
  193. NV_DEBUG(drm, "Load detected on head A\n");
  194. return connector_status_connected;
  195. }
  196. return connector_status_disconnected;
  197. }
  198. uint32_t nv17_dac_sample_load(struct drm_encoder *encoder)
  199. {
  200. struct drm_device *dev = encoder->dev;
  201. struct nouveau_drm *drm = nouveau_drm(dev);
  202. struct nvif_object *device = &nouveau_drm(dev)->device.object;
  203. struct nvkm_gpio *gpio = nvxx_gpio(&drm->device);
  204. struct dcb_output *dcb = nouveau_encoder(encoder)->dcb;
  205. uint32_t sample, testval, regoffset = nv04_dac_output_offset(encoder);
  206. uint32_t saved_powerctrl_2 = 0, saved_powerctrl_4 = 0, saved_routput,
  207. saved_rtest_ctrl, saved_gpio0 = 0, saved_gpio1 = 0, temp, routput;
  208. int head;
  209. #define RGB_TEST_DATA(r, g, b) (r << 0 | g << 10 | b << 20)
  210. if (dcb->type == DCB_OUTPUT_TV) {
  211. testval = RGB_TEST_DATA(0xa0, 0xa0, 0xa0);
  212. if (drm->vbios.tvdactestval)
  213. testval = drm->vbios.tvdactestval;
  214. } else {
  215. testval = RGB_TEST_DATA(0x140, 0x140, 0x140); /* 0x94050140 */
  216. if (drm->vbios.dactestval)
  217. testval = drm->vbios.dactestval;
  218. }
  219. saved_rtest_ctrl = NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset);
  220. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset,
  221. saved_rtest_ctrl & ~NV_PRAMDAC_TEST_CONTROL_PWRDWN_DAC_OFF);
  222. saved_powerctrl_2 = nvif_rd32(device, NV_PBUS_POWERCTRL_2);
  223. nvif_wr32(device, NV_PBUS_POWERCTRL_2, saved_powerctrl_2 & 0xd7ffffff);
  224. if (regoffset == 0x68) {
  225. saved_powerctrl_4 = nvif_rd32(device, NV_PBUS_POWERCTRL_4);
  226. nvif_wr32(device, NV_PBUS_POWERCTRL_4, saved_powerctrl_4 & 0xffffffcf);
  227. }
  228. if (gpio) {
  229. saved_gpio1 = nvkm_gpio_get(gpio, 0, DCB_GPIO_TVDAC1, 0xff);
  230. saved_gpio0 = nvkm_gpio_get(gpio, 0, DCB_GPIO_TVDAC0, 0xff);
  231. nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, dcb->type == DCB_OUTPUT_TV);
  232. nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC0, 0xff, dcb->type == DCB_OUTPUT_TV);
  233. }
  234. msleep(4);
  235. saved_routput = NVReadRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset);
  236. head = (saved_routput & 0x100) >> 8;
  237. /* if there's a spare crtc, using it will minimise flicker */
  238. if (!(NVReadVgaCrtc(dev, head, NV_CIO_CRE_RPC1_INDEX) & 0xC0))
  239. head ^= 1;
  240. /* nv driver and nv31 use 0xfffffeee, nv34 and 6600 use 0xfffffece */
  241. routput = (saved_routput & 0xfffffece) | head << 8;
  242. if (drm->device.info.family >= NV_DEVICE_INFO_V0_CURIE) {
  243. if (dcb->type == DCB_OUTPUT_TV)
  244. routput |= 0x1a << 16;
  245. else
  246. routput &= ~(0x1a << 16);
  247. }
  248. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset, routput);
  249. msleep(1);
  250. temp = NVReadRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset);
  251. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset, temp | 1);
  252. NVWriteRAMDAC(dev, head, NV_PRAMDAC_TESTPOINT_DATA,
  253. NV_PRAMDAC_TESTPOINT_DATA_NOTBLANK | testval);
  254. temp = NVReadRAMDAC(dev, head, NV_PRAMDAC_TEST_CONTROL);
  255. NVWriteRAMDAC(dev, head, NV_PRAMDAC_TEST_CONTROL,
  256. temp | NV_PRAMDAC_TEST_CONTROL_TP_INS_EN_ASSERTED);
  257. msleep(5);
  258. sample = NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset);
  259. /* do it again just in case it's a residual current */
  260. sample &= NVReadRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset);
  261. temp = NVReadRAMDAC(dev, head, NV_PRAMDAC_TEST_CONTROL);
  262. NVWriteRAMDAC(dev, head, NV_PRAMDAC_TEST_CONTROL,
  263. temp & ~NV_PRAMDAC_TEST_CONTROL_TP_INS_EN_ASSERTED);
  264. NVWriteRAMDAC(dev, head, NV_PRAMDAC_TESTPOINT_DATA, 0);
  265. /* bios does something more complex for restoring, but I think this is good enough */
  266. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + regoffset, saved_routput);
  267. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + regoffset, saved_rtest_ctrl);
  268. if (regoffset == 0x68)
  269. nvif_wr32(device, NV_PBUS_POWERCTRL_4, saved_powerctrl_4);
  270. nvif_wr32(device, NV_PBUS_POWERCTRL_2, saved_powerctrl_2);
  271. if (gpio) {
  272. nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC1, 0xff, saved_gpio1);
  273. nvkm_gpio_set(gpio, 0, DCB_GPIO_TVDAC0, 0xff, saved_gpio0);
  274. }
  275. return sample;
  276. }
  277. static enum drm_connector_status
  278. nv17_dac_detect(struct drm_encoder *encoder, struct drm_connector *connector)
  279. {
  280. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  281. struct dcb_output *dcb = nouveau_encoder(encoder)->dcb;
  282. if (nv04_dac_in_use(encoder))
  283. return connector_status_disconnected;
  284. if (nv17_dac_sample_load(encoder) &
  285. NV_PRAMDAC_TEST_CONTROL_SENSEB_ALLHI) {
  286. NV_DEBUG(drm, "Load detected on output %c\n",
  287. '@' + ffs(dcb->or));
  288. return connector_status_connected;
  289. } else {
  290. return connector_status_disconnected;
  291. }
  292. }
  293. static bool nv04_dac_mode_fixup(struct drm_encoder *encoder,
  294. const struct drm_display_mode *mode,
  295. struct drm_display_mode *adjusted_mode)
  296. {
  297. if (nv04_dac_in_use(encoder))
  298. return false;
  299. return true;
  300. }
  301. static void nv04_dac_prepare(struct drm_encoder *encoder)
  302. {
  303. const struct drm_encoder_helper_funcs *helper = encoder->helper_private;
  304. struct drm_device *dev = encoder->dev;
  305. int head = nouveau_crtc(encoder->crtc)->index;
  306. helper->dpms(encoder, DRM_MODE_DPMS_OFF);
  307. nv04_dfp_disable(dev, head);
  308. }
  309. static void nv04_dac_mode_set(struct drm_encoder *encoder,
  310. struct drm_display_mode *mode,
  311. struct drm_display_mode *adjusted_mode)
  312. {
  313. struct drm_device *dev = encoder->dev;
  314. struct nouveau_drm *drm = nouveau_drm(dev);
  315. int head = nouveau_crtc(encoder->crtc)->index;
  316. if (nv_gf4_disp_arch(dev)) {
  317. struct drm_encoder *rebind;
  318. uint32_t dac_offset = nv04_dac_output_offset(encoder);
  319. uint32_t otherdac;
  320. /* bit 16-19 are bits that are set on some G70 cards,
  321. * but don't seem to have much effect */
  322. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + dac_offset,
  323. head << 8 | NV_PRAMDAC_DACCLK_SEL_DACCLK);
  324. /* force any other vga encoders to bind to the other crtc */
  325. list_for_each_entry(rebind, &dev->mode_config.encoder_list, head) {
  326. if (rebind == encoder
  327. || nouveau_encoder(rebind)->dcb->type != DCB_OUTPUT_ANALOG)
  328. continue;
  329. dac_offset = nv04_dac_output_offset(rebind);
  330. otherdac = NVReadRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + dac_offset);
  331. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + dac_offset,
  332. (otherdac & ~0x0100) | (head ^ 1) << 8);
  333. }
  334. }
  335. /* This could use refinement for flatpanels, but it should work this way */
  336. if (drm->device.info.chipset < 0x44)
  337. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + nv04_dac_output_offset(encoder), 0xf0000000);
  338. else
  339. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_TEST_CONTROL + nv04_dac_output_offset(encoder), 0x00100000);
  340. }
  341. static void nv04_dac_commit(struct drm_encoder *encoder)
  342. {
  343. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  344. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  345. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  346. const struct drm_encoder_helper_funcs *helper = encoder->helper_private;
  347. helper->dpms(encoder, DRM_MODE_DPMS_ON);
  348. NV_DEBUG(drm, "Output %s is running on CRTC %d using output %c\n",
  349. nouveau_encoder_connector_get(nv_encoder)->base.name,
  350. nv_crtc->index, '@' + ffs(nv_encoder->dcb->or));
  351. }
  352. void nv04_dac_update_dacclk(struct drm_encoder *encoder, bool enable)
  353. {
  354. struct drm_device *dev = encoder->dev;
  355. struct dcb_output *dcb = nouveau_encoder(encoder)->dcb;
  356. if (nv_gf4_disp_arch(dev)) {
  357. uint32_t *dac_users = &nv04_display(dev)->dac_users[ffs(dcb->or) - 1];
  358. int dacclk_off = NV_PRAMDAC_DACCLK + nv04_dac_output_offset(encoder);
  359. uint32_t dacclk = NVReadRAMDAC(dev, 0, dacclk_off);
  360. if (enable) {
  361. *dac_users |= 1 << dcb->index;
  362. NVWriteRAMDAC(dev, 0, dacclk_off, dacclk | NV_PRAMDAC_DACCLK_SEL_DACCLK);
  363. } else {
  364. *dac_users &= ~(1 << dcb->index);
  365. if (!*dac_users)
  366. NVWriteRAMDAC(dev, 0, dacclk_off,
  367. dacclk & ~NV_PRAMDAC_DACCLK_SEL_DACCLK);
  368. }
  369. }
  370. }
  371. /* Check if the DAC corresponding to 'encoder' is being used by
  372. * someone else. */
  373. bool nv04_dac_in_use(struct drm_encoder *encoder)
  374. {
  375. struct drm_device *dev = encoder->dev;
  376. struct dcb_output *dcb = nouveau_encoder(encoder)->dcb;
  377. return nv_gf4_disp_arch(encoder->dev) &&
  378. (nv04_display(dev)->dac_users[ffs(dcb->or) - 1] & ~(1 << dcb->index));
  379. }
  380. static void nv04_dac_dpms(struct drm_encoder *encoder, int mode)
  381. {
  382. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  383. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  384. if (nv_encoder->last_dpms == mode)
  385. return;
  386. nv_encoder->last_dpms = mode;
  387. NV_DEBUG(drm, "Setting dpms mode %d on vga encoder (output %d)\n",
  388. mode, nv_encoder->dcb->index);
  389. nv04_dac_update_dacclk(encoder, mode == DRM_MODE_DPMS_ON);
  390. }
  391. static void nv04_dac_save(struct drm_encoder *encoder)
  392. {
  393. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  394. struct drm_device *dev = encoder->dev;
  395. if (nv_gf4_disp_arch(dev))
  396. nv_encoder->restore.output = NVReadRAMDAC(dev, 0, NV_PRAMDAC_DACCLK +
  397. nv04_dac_output_offset(encoder));
  398. }
  399. static void nv04_dac_restore(struct drm_encoder *encoder)
  400. {
  401. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  402. struct drm_device *dev = encoder->dev;
  403. if (nv_gf4_disp_arch(dev))
  404. NVWriteRAMDAC(dev, 0, NV_PRAMDAC_DACCLK + nv04_dac_output_offset(encoder),
  405. nv_encoder->restore.output);
  406. nv_encoder->last_dpms = NV_DPMS_CLEARED;
  407. }
  408. static void nv04_dac_destroy(struct drm_encoder *encoder)
  409. {
  410. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  411. drm_encoder_cleanup(encoder);
  412. kfree(nv_encoder);
  413. }
  414. static const struct drm_encoder_helper_funcs nv04_dac_helper_funcs = {
  415. .dpms = nv04_dac_dpms,
  416. .save = nv04_dac_save,
  417. .restore = nv04_dac_restore,
  418. .mode_fixup = nv04_dac_mode_fixup,
  419. .prepare = nv04_dac_prepare,
  420. .commit = nv04_dac_commit,
  421. .mode_set = nv04_dac_mode_set,
  422. .detect = nv04_dac_detect
  423. };
  424. static const struct drm_encoder_helper_funcs nv17_dac_helper_funcs = {
  425. .dpms = nv04_dac_dpms,
  426. .save = nv04_dac_save,
  427. .restore = nv04_dac_restore,
  428. .mode_fixup = nv04_dac_mode_fixup,
  429. .prepare = nv04_dac_prepare,
  430. .commit = nv04_dac_commit,
  431. .mode_set = nv04_dac_mode_set,
  432. .detect = nv17_dac_detect
  433. };
  434. static const struct drm_encoder_funcs nv04_dac_funcs = {
  435. .destroy = nv04_dac_destroy,
  436. };
  437. int
  438. nv04_dac_create(struct drm_connector *connector, struct dcb_output *entry)
  439. {
  440. const struct drm_encoder_helper_funcs *helper;
  441. struct nouveau_encoder *nv_encoder = NULL;
  442. struct drm_device *dev = connector->dev;
  443. struct drm_encoder *encoder;
  444. nv_encoder = kzalloc(sizeof(*nv_encoder), GFP_KERNEL);
  445. if (!nv_encoder)
  446. return -ENOMEM;
  447. encoder = to_drm_encoder(nv_encoder);
  448. nv_encoder->dcb = entry;
  449. nv_encoder->or = ffs(entry->or) - 1;
  450. if (nv_gf4_disp_arch(dev))
  451. helper = &nv17_dac_helper_funcs;
  452. else
  453. helper = &nv04_dac_helper_funcs;
  454. drm_encoder_init(dev, encoder, &nv04_dac_funcs, DRM_MODE_ENCODER_DAC);
  455. drm_encoder_helper_add(encoder, helper);
  456. encoder->possible_crtcs = entry->heads;
  457. encoder->possible_clones = 0;
  458. drm_mode_connector_attach_encoder(connector, encoder);
  459. return 0;
  460. }