cdv_device.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**************************************************************************
  2. * Copyright (c) 2011, Intel Corporation.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. **************************************************************************/
  19. #include <linux/backlight.h>
  20. #include <drm/drmP.h>
  21. #include <drm/drm.h>
  22. #include "gma_drm.h"
  23. #include "psb_drv.h"
  24. #include "psb_reg.h"
  25. #include "psb_intel_reg.h"
  26. #include "intel_bios.h"
  27. #include "cdv_device.h"
  28. #define VGA_SR_INDEX 0x3c4
  29. #define VGA_SR_DATA 0x3c5
  30. static void cdv_disable_vga(struct drm_device *dev)
  31. {
  32. u8 sr1;
  33. u32 vga_reg;
  34. vga_reg = VGACNTRL;
  35. outb(1, VGA_SR_INDEX);
  36. sr1 = inb(VGA_SR_DATA);
  37. outb(sr1 | 1<<5, VGA_SR_DATA);
  38. udelay(300);
  39. REG_WRITE(vga_reg, VGA_DISP_DISABLE);
  40. REG_READ(vga_reg);
  41. }
  42. static int cdv_output_init(struct drm_device *dev)
  43. {
  44. struct drm_psb_private *dev_priv = dev->dev_private;
  45. cdv_disable_vga(dev);
  46. cdv_intel_crt_init(dev, &dev_priv->mode_dev);
  47. cdv_intel_lvds_init(dev, &dev_priv->mode_dev);
  48. /* These bits indicate HDMI not SDVO on CDV, but we don't yet support
  49. the HDMI interface */
  50. if (REG_READ(SDVOB) & SDVO_DETECTED)
  51. cdv_hdmi_init(dev, &dev_priv->mode_dev, SDVOB);
  52. if (REG_READ(SDVOC) & SDVO_DETECTED)
  53. cdv_hdmi_init(dev, &dev_priv->mode_dev, SDVOC);
  54. return 0;
  55. }
  56. #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
  57. /*
  58. * Poulsbo Backlight Interfaces
  59. */
  60. #define BLC_PWM_PRECISION_FACTOR 100 /* 10000000 */
  61. #define BLC_PWM_FREQ_CALC_CONSTANT 32
  62. #define MHz 1000000
  63. #define PSB_BLC_PWM_PRECISION_FACTOR 10
  64. #define PSB_BLC_MAX_PWM_REG_FREQ 0xFFFE
  65. #define PSB_BLC_MIN_PWM_REG_FREQ 0x2
  66. #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
  67. #define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
  68. static int cdv_brightness;
  69. static struct backlight_device *cdv_backlight_device;
  70. static int cdv_get_brightness(struct backlight_device *bd)
  71. {
  72. /* return locally cached var instead of HW read (due to DPST etc.) */
  73. /* FIXME: ideally return actual value in case firmware fiddled with
  74. it */
  75. return cdv_brightness;
  76. }
  77. static int cdv_backlight_setup(struct drm_device *dev)
  78. {
  79. struct drm_psb_private *dev_priv = dev->dev_private;
  80. unsigned long core_clock;
  81. /* u32 bl_max_freq; */
  82. /* unsigned long value; */
  83. u16 bl_max_freq;
  84. uint32_t value;
  85. uint32_t blc_pwm_precision_factor;
  86. /* get bl_max_freq and pol from dev_priv*/
  87. if (!dev_priv->lvds_bl) {
  88. dev_err(dev->dev, "Has no valid LVDS backlight info\n");
  89. return -ENOENT;
  90. }
  91. bl_max_freq = dev_priv->lvds_bl->freq;
  92. blc_pwm_precision_factor = PSB_BLC_PWM_PRECISION_FACTOR;
  93. core_clock = dev_priv->core_freq;
  94. value = (core_clock * MHz) / BLC_PWM_FREQ_CALC_CONSTANT;
  95. value *= blc_pwm_precision_factor;
  96. value /= bl_max_freq;
  97. value /= blc_pwm_precision_factor;
  98. if (value > (unsigned long long)PSB_BLC_MAX_PWM_REG_FREQ ||
  99. value < (unsigned long long)PSB_BLC_MIN_PWM_REG_FREQ)
  100. return -ERANGE;
  101. else {
  102. /* FIXME */
  103. }
  104. return 0;
  105. }
  106. static int cdv_set_brightness(struct backlight_device *bd)
  107. {
  108. int level = bd->props.brightness;
  109. /* Percentage 1-100% being valid */
  110. if (level < 1)
  111. level = 1;
  112. /*cdv_intel_lvds_set_brightness(dev, level); FIXME */
  113. cdv_brightness = level;
  114. return 0;
  115. }
  116. static const struct backlight_ops cdv_ops = {
  117. .get_brightness = cdv_get_brightness,
  118. .update_status = cdv_set_brightness,
  119. };
  120. static int cdv_backlight_init(struct drm_device *dev)
  121. {
  122. struct drm_psb_private *dev_priv = dev->dev_private;
  123. int ret;
  124. struct backlight_properties props;
  125. memset(&props, 0, sizeof(struct backlight_properties));
  126. props.max_brightness = 100;
  127. props.type = BACKLIGHT_PLATFORM;
  128. cdv_backlight_device = backlight_device_register("psb-bl",
  129. NULL, (void *)dev, &cdv_ops, &props);
  130. if (IS_ERR(cdv_backlight_device))
  131. return PTR_ERR(cdv_backlight_device);
  132. ret = cdv_backlight_setup(dev);
  133. if (ret < 0) {
  134. backlight_device_unregister(cdv_backlight_device);
  135. cdv_backlight_device = NULL;
  136. return ret;
  137. }
  138. cdv_backlight_device->props.brightness = 100;
  139. cdv_backlight_device->props.max_brightness = 100;
  140. backlight_update_status(cdv_backlight_device);
  141. dev_priv->backlight_device = cdv_backlight_device;
  142. return 0;
  143. }
  144. #endif
  145. /*
  146. * Provide the Cedarview specific chip logic and low level methods
  147. * for power management
  148. *
  149. * FIXME: we need to implement the apm/ospm base management bits
  150. * for this and the MID devices.
  151. */
  152. static inline u32 CDV_MSG_READ32(uint port, uint offset)
  153. {
  154. int mcr = (0x10<<24) | (port << 16) | (offset << 8);
  155. uint32_t ret_val = 0;
  156. struct pci_dev *pci_root = pci_get_bus_and_slot(0, 0);
  157. pci_write_config_dword(pci_root, 0xD0, mcr);
  158. pci_read_config_dword(pci_root, 0xD4, &ret_val);
  159. pci_dev_put(pci_root);
  160. return ret_val;
  161. }
  162. static inline void CDV_MSG_WRITE32(uint port, uint offset, u32 value)
  163. {
  164. int mcr = (0x11<<24) | (port << 16) | (offset << 8) | 0xF0;
  165. struct pci_dev *pci_root = pci_get_bus_and_slot(0, 0);
  166. pci_write_config_dword(pci_root, 0xD4, value);
  167. pci_write_config_dword(pci_root, 0xD0, mcr);
  168. pci_dev_put(pci_root);
  169. }
  170. #define PSB_PM_SSC 0x20
  171. #define PSB_PM_SSS 0x30
  172. #define PSB_PWRGT_GFX_ON 0x02
  173. #define PSB_PWRGT_GFX_OFF 0x01
  174. #define PSB_PWRGT_GFX_D0 0x00
  175. #define PSB_PWRGT_GFX_D3 0x03
  176. static void cdv_init_pm(struct drm_device *dev)
  177. {
  178. struct drm_psb_private *dev_priv = dev->dev_private;
  179. u32 pwr_cnt;
  180. int i;
  181. dev_priv->apm_base = CDV_MSG_READ32(PSB_PUNIT_PORT,
  182. PSB_APMBA) & 0xFFFF;
  183. dev_priv->ospm_base = CDV_MSG_READ32(PSB_PUNIT_PORT,
  184. PSB_OSPMBA) & 0xFFFF;
  185. /* Power status */
  186. pwr_cnt = inl(dev_priv->apm_base + PSB_APM_CMD);
  187. /* Enable the GPU */
  188. pwr_cnt &= ~PSB_PWRGT_GFX_MASK;
  189. pwr_cnt |= PSB_PWRGT_GFX_ON;
  190. outl(pwr_cnt, dev_priv->apm_base + PSB_APM_CMD);
  191. /* Wait for the GPU power */
  192. for (i = 0; i < 5; i++) {
  193. u32 pwr_sts = inl(dev_priv->apm_base + PSB_APM_STS);
  194. if ((pwr_sts & PSB_PWRGT_GFX_MASK) == 0)
  195. return;
  196. udelay(10);
  197. }
  198. dev_err(dev->dev, "GPU: power management timed out.\n");
  199. }
  200. /**
  201. * cdv_save_display_registers - save registers lost on suspend
  202. * @dev: our DRM device
  203. *
  204. * Save the state we need in order to be able to restore the interface
  205. * upon resume from suspend
  206. */
  207. static int cdv_save_display_registers(struct drm_device *dev)
  208. {
  209. struct drm_psb_private *dev_priv = dev->dev_private;
  210. struct psb_save_area *regs = &dev_priv->regs;
  211. struct drm_connector *connector;
  212. dev_info(dev->dev, "Saving GPU registers.\n");
  213. pci_read_config_byte(dev->pdev, 0xF4, &regs->cdv.saveLBB);
  214. regs->cdv.saveDSPCLK_GATE_D = REG_READ(DSPCLK_GATE_D);
  215. regs->cdv.saveRAMCLK_GATE_D = REG_READ(RAMCLK_GATE_D);
  216. regs->cdv.saveDSPARB = REG_READ(DSPARB);
  217. regs->cdv.saveDSPFW[0] = REG_READ(DSPFW1);
  218. regs->cdv.saveDSPFW[1] = REG_READ(DSPFW2);
  219. regs->cdv.saveDSPFW[2] = REG_READ(DSPFW3);
  220. regs->cdv.saveDSPFW[3] = REG_READ(DSPFW4);
  221. regs->cdv.saveDSPFW[4] = REG_READ(DSPFW5);
  222. regs->cdv.saveDSPFW[5] = REG_READ(DSPFW6);
  223. regs->cdv.saveADPA = REG_READ(ADPA);
  224. regs->cdv.savePP_CONTROL = REG_READ(PP_CONTROL);
  225. regs->cdv.savePFIT_PGM_RATIOS = REG_READ(PFIT_PGM_RATIOS);
  226. regs->saveBLC_PWM_CTL = REG_READ(BLC_PWM_CTL);
  227. regs->saveBLC_PWM_CTL2 = REG_READ(BLC_PWM_CTL2);
  228. regs->cdv.saveLVDS = REG_READ(LVDS);
  229. regs->cdv.savePFIT_CONTROL = REG_READ(PFIT_CONTROL);
  230. regs->cdv.savePP_ON_DELAYS = REG_READ(PP_ON_DELAYS);
  231. regs->cdv.savePP_OFF_DELAYS = REG_READ(PP_OFF_DELAYS);
  232. regs->cdv.savePP_CYCLE = REG_READ(PP_CYCLE);
  233. regs->cdv.saveVGACNTRL = REG_READ(VGACNTRL);
  234. regs->cdv.saveIER = REG_READ(PSB_INT_ENABLE_R);
  235. regs->cdv.saveIMR = REG_READ(PSB_INT_MASK_R);
  236. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  237. connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
  238. return 0;
  239. }
  240. /**
  241. * cdv_restore_display_registers - restore lost register state
  242. * @dev: our DRM device
  243. *
  244. * Restore register state that was lost during suspend and resume.
  245. *
  246. * FIXME: review
  247. */
  248. static int cdv_restore_display_registers(struct drm_device *dev)
  249. {
  250. struct drm_psb_private *dev_priv = dev->dev_private;
  251. struct psb_save_area *regs = &dev_priv->regs;
  252. struct drm_connector *connector;
  253. u32 temp;
  254. pci_write_config_byte(dev->pdev, 0xF4, regs->cdv.saveLBB);
  255. REG_WRITE(DSPCLK_GATE_D, regs->cdv.saveDSPCLK_GATE_D);
  256. REG_WRITE(RAMCLK_GATE_D, regs->cdv.saveRAMCLK_GATE_D);
  257. /* BIOS does below anyway */
  258. REG_WRITE(DPIO_CFG, 0);
  259. REG_WRITE(DPIO_CFG, DPIO_MODE_SELECT_0 | DPIO_CMN_RESET_N);
  260. temp = REG_READ(DPLL_A);
  261. if ((temp & DPLL_SYNCLOCK_ENABLE) == 0) {
  262. REG_WRITE(DPLL_A, temp | DPLL_SYNCLOCK_ENABLE);
  263. REG_READ(DPLL_A);
  264. }
  265. temp = REG_READ(DPLL_B);
  266. if ((temp & DPLL_SYNCLOCK_ENABLE) == 0) {
  267. REG_WRITE(DPLL_B, temp | DPLL_SYNCLOCK_ENABLE);
  268. REG_READ(DPLL_B);
  269. }
  270. udelay(500);
  271. REG_WRITE(DSPFW1, regs->cdv.saveDSPFW[0]);
  272. REG_WRITE(DSPFW2, regs->cdv.saveDSPFW[1]);
  273. REG_WRITE(DSPFW3, regs->cdv.saveDSPFW[2]);
  274. REG_WRITE(DSPFW4, regs->cdv.saveDSPFW[3]);
  275. REG_WRITE(DSPFW5, regs->cdv.saveDSPFW[4]);
  276. REG_WRITE(DSPFW6, regs->cdv.saveDSPFW[5]);
  277. REG_WRITE(DSPARB, regs->cdv.saveDSPARB);
  278. REG_WRITE(ADPA, regs->cdv.saveADPA);
  279. REG_WRITE(BLC_PWM_CTL2, regs->saveBLC_PWM_CTL2);
  280. REG_WRITE(LVDS, regs->cdv.saveLVDS);
  281. REG_WRITE(PFIT_CONTROL, regs->cdv.savePFIT_CONTROL);
  282. REG_WRITE(PFIT_PGM_RATIOS, regs->cdv.savePFIT_PGM_RATIOS);
  283. REG_WRITE(BLC_PWM_CTL, regs->saveBLC_PWM_CTL);
  284. REG_WRITE(PP_ON_DELAYS, regs->cdv.savePP_ON_DELAYS);
  285. REG_WRITE(PP_OFF_DELAYS, regs->cdv.savePP_OFF_DELAYS);
  286. REG_WRITE(PP_CYCLE, regs->cdv.savePP_CYCLE);
  287. REG_WRITE(PP_CONTROL, regs->cdv.savePP_CONTROL);
  288. REG_WRITE(VGACNTRL, regs->cdv.saveVGACNTRL);
  289. REG_WRITE(PSB_INT_ENABLE_R, regs->cdv.saveIER);
  290. REG_WRITE(PSB_INT_MASK_R, regs->cdv.saveIMR);
  291. /* Fix arbitration bug */
  292. CDV_MSG_WRITE32(3, 0x30, 0x08027108);
  293. drm_mode_config_reset(dev);
  294. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  295. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  296. /* Resume the modeset for every activated CRTC */
  297. drm_helper_resume_force_mode(dev);
  298. return 0;
  299. }
  300. static int cdv_power_down(struct drm_device *dev)
  301. {
  302. struct drm_psb_private *dev_priv = dev->dev_private;
  303. u32 pwr_cnt, pwr_mask, pwr_sts;
  304. int tries = 5;
  305. pwr_cnt = inl(dev_priv->apm_base + PSB_APM_CMD);
  306. pwr_cnt &= ~PSB_PWRGT_GFX_MASK;
  307. pwr_cnt |= PSB_PWRGT_GFX_OFF;
  308. pwr_mask = PSB_PWRGT_GFX_MASK;
  309. outl(pwr_cnt, dev_priv->apm_base + PSB_APM_CMD);
  310. while (tries--) {
  311. pwr_sts = inl(dev_priv->apm_base + PSB_APM_STS);
  312. if ((pwr_sts & pwr_mask) == PSB_PWRGT_GFX_D3)
  313. return 0;
  314. udelay(10);
  315. }
  316. return 0;
  317. }
  318. static int cdv_power_up(struct drm_device *dev)
  319. {
  320. struct drm_psb_private *dev_priv = dev->dev_private;
  321. u32 pwr_cnt, pwr_mask, pwr_sts;
  322. int tries = 5;
  323. pwr_cnt = inl(dev_priv->apm_base + PSB_APM_CMD);
  324. pwr_cnt &= ~PSB_PWRGT_GFX_MASK;
  325. pwr_cnt |= PSB_PWRGT_GFX_ON;
  326. pwr_mask = PSB_PWRGT_GFX_MASK;
  327. outl(pwr_cnt, dev_priv->apm_base + PSB_APM_CMD);
  328. while (tries--) {
  329. pwr_sts = inl(dev_priv->apm_base + PSB_APM_STS);
  330. if ((pwr_sts & pwr_mask) == PSB_PWRGT_GFX_D0)
  331. return 0;
  332. udelay(10);
  333. }
  334. return 0;
  335. }
  336. /* FIXME ? - shared with Poulsbo */
  337. static void cdv_get_core_freq(struct drm_device *dev)
  338. {
  339. uint32_t clock;
  340. struct pci_dev *pci_root = pci_get_bus_and_slot(0, 0);
  341. struct drm_psb_private *dev_priv = dev->dev_private;
  342. pci_write_config_dword(pci_root, 0xD0, 0xD0050300);
  343. pci_read_config_dword(pci_root, 0xD4, &clock);
  344. pci_dev_put(pci_root);
  345. switch (clock & 0x07) {
  346. case 0:
  347. dev_priv->core_freq = 100;
  348. break;
  349. case 1:
  350. dev_priv->core_freq = 133;
  351. break;
  352. case 2:
  353. dev_priv->core_freq = 150;
  354. break;
  355. case 3:
  356. dev_priv->core_freq = 178;
  357. break;
  358. case 4:
  359. dev_priv->core_freq = 200;
  360. break;
  361. case 5:
  362. case 6:
  363. case 7:
  364. dev_priv->core_freq = 266;
  365. default:
  366. dev_priv->core_freq = 0;
  367. }
  368. }
  369. static int cdv_chip_setup(struct drm_device *dev)
  370. {
  371. cdv_get_core_freq(dev);
  372. gma_intel_opregion_init(dev);
  373. psb_intel_init_bios(dev);
  374. REG_WRITE(PORT_HOTPLUG_EN, 0);
  375. REG_WRITE(PORT_HOTPLUG_STAT, REG_READ(PORT_HOTPLUG_STAT));
  376. return 0;
  377. }
  378. /* CDV is much like Poulsbo but has MID like SGX offsets and PM */
  379. const struct psb_ops cdv_chip_ops = {
  380. .name = "GMA3600/3650",
  381. .accel_2d = 0,
  382. .pipes = 2,
  383. .crtcs = 2,
  384. .sgx_offset = MRST_SGX_OFFSET,
  385. .chip_setup = cdv_chip_setup,
  386. .crtc_helper = &cdv_intel_helper_funcs,
  387. .crtc_funcs = &cdv_intel_crtc_funcs,
  388. .output_init = cdv_output_init,
  389. #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
  390. .backlight_init = cdv_backlight_init,
  391. #endif
  392. .init_pm = cdv_init_pm,
  393. .save_regs = cdv_save_display_registers,
  394. .restore_regs = cdv_restore_display_registers,
  395. .power_down = cdv_power_down,
  396. .power_up = cdv_power_up,
  397. };