cdv_device.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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_APM_CMD 0x0
  171. #define PSB_APM_STS 0x04
  172. #define PSB_PM_SSC 0x20
  173. #define PSB_PM_SSS 0x30
  174. #define PSB_PWRGT_GFX_MASK 0x3
  175. #define CDV_PWRGT_DISPLAY_CNTR 0x000fc00c
  176. #define CDV_PWRGT_DISPLAY_STS 0x000fc00c
  177. static void cdv_init_pm(struct drm_device *dev)
  178. {
  179. struct drm_psb_private *dev_priv = dev->dev_private;
  180. u32 pwr_cnt;
  181. int i;
  182. dev_priv->apm_base = CDV_MSG_READ32(PSB_PUNIT_PORT,
  183. PSB_APMBA) & 0xFFFF;
  184. dev_priv->ospm_base = CDV_MSG_READ32(PSB_PUNIT_PORT,
  185. PSB_OSPMBA) & 0xFFFF;
  186. /* Force power on for now */
  187. pwr_cnt = inl(dev_priv->apm_base + PSB_APM_CMD);
  188. pwr_cnt &= ~PSB_PWRGT_GFX_MASK;
  189. outl(pwr_cnt, dev_priv->apm_base + PSB_APM_CMD);
  190. for (i = 0; i < 5; i++) {
  191. u32 pwr_sts = inl(dev_priv->apm_base + PSB_APM_STS);
  192. if ((pwr_sts & PSB_PWRGT_GFX_MASK) == 0)
  193. break;
  194. udelay(10);
  195. }
  196. pwr_cnt = inl(dev_priv->ospm_base + PSB_PM_SSC);
  197. pwr_cnt &= ~CDV_PWRGT_DISPLAY_CNTR;
  198. outl(pwr_cnt, dev_priv->ospm_base + PSB_PM_SSC);
  199. for (i = 0; i < 5; i++) {
  200. u32 pwr_sts = inl(dev_priv->ospm_base + PSB_PM_SSS);
  201. if ((pwr_sts & CDV_PWRGT_DISPLAY_STS) == 0)
  202. break;
  203. udelay(10);
  204. }
  205. }
  206. /**
  207. * cdv_save_display_registers - save registers lost on suspend
  208. * @dev: our DRM device
  209. *
  210. * Save the state we need in order to be able to restore the interface
  211. * upon resume from suspend
  212. *
  213. * FIXME: review
  214. */
  215. static int cdv_save_display_registers(struct drm_device *dev)
  216. {
  217. return 0;
  218. }
  219. /**
  220. * cdv_restore_display_registers - restore lost register state
  221. * @dev: our DRM device
  222. *
  223. * Restore register state that was lost during suspend and resume.
  224. *
  225. * FIXME: review
  226. */
  227. static int cdv_restore_display_registers(struct drm_device *dev)
  228. {
  229. return 0;
  230. }
  231. static int cdv_power_down(struct drm_device *dev)
  232. {
  233. return 0;
  234. }
  235. static int cdv_power_up(struct drm_device *dev)
  236. {
  237. return 0;
  238. }
  239. /* FIXME ? - shared with Poulsbo */
  240. static void cdv_get_core_freq(struct drm_device *dev)
  241. {
  242. uint32_t clock;
  243. struct pci_dev *pci_root = pci_get_bus_and_slot(0, 0);
  244. struct drm_psb_private *dev_priv = dev->dev_private;
  245. pci_write_config_dword(pci_root, 0xD0, 0xD0050300);
  246. pci_read_config_dword(pci_root, 0xD4, &clock);
  247. pci_dev_put(pci_root);
  248. switch (clock & 0x07) {
  249. case 0:
  250. dev_priv->core_freq = 100;
  251. break;
  252. case 1:
  253. dev_priv->core_freq = 133;
  254. break;
  255. case 2:
  256. dev_priv->core_freq = 150;
  257. break;
  258. case 3:
  259. dev_priv->core_freq = 178;
  260. break;
  261. case 4:
  262. dev_priv->core_freq = 200;
  263. break;
  264. case 5:
  265. case 6:
  266. case 7:
  267. dev_priv->core_freq = 266;
  268. default:
  269. dev_priv->core_freq = 0;
  270. }
  271. }
  272. static int cdv_chip_setup(struct drm_device *dev)
  273. {
  274. cdv_get_core_freq(dev);
  275. gma_intel_opregion_init(dev);
  276. psb_intel_init_bios(dev);
  277. return 0;
  278. }
  279. /* CDV is much like Poulsbo but has MID like SGX offsets and PM */
  280. const struct psb_ops cdv_chip_ops = {
  281. .name = "Cedartrail",
  282. .accel_2d = 0,
  283. .pipes = 2,
  284. .sgx_offset = MRST_SGX_OFFSET,
  285. .chip_setup = cdv_chip_setup,
  286. .crtc_helper = &cdv_intel_helper_funcs,
  287. .crtc_funcs = &cdv_intel_crtc_funcs,
  288. .output_init = cdv_output_init,
  289. #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
  290. .backlight_init = cdv_backlight_init,
  291. #endif
  292. .init_pm = cdv_init_pm,
  293. .save_regs = cdv_save_display_registers,
  294. .restore_regs = cdv_restore_display_registers,
  295. .power_down = cdv_power_down,
  296. .power_up = cdv_power_up,
  297. };