cdv_device.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "psb_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. /* FIXME: should check if we are the active VGA device ?? */
  31. static void cdv_disable_vga(struct drm_device *dev)
  32. {
  33. u8 sr1;
  34. u32 vga_reg;
  35. vga_reg = VGACNTRL;
  36. outb(1, VGA_SR_INDEX);
  37. sr1 = inb(VGA_SR_DATA);
  38. outb(sr1 | 1<<5, VGA_SR_DATA);
  39. udelay(300);
  40. REG_WRITE(vga_reg, VGA_DISP_DISABLE);
  41. REG_READ(vga_reg);
  42. }
  43. static int cdv_output_init(struct drm_device *dev)
  44. {
  45. struct drm_psb_private *dev_priv = dev->dev_private;
  46. cdv_disable_vga(dev);
  47. cdv_intel_crt_init(dev, &dev_priv->mode_dev);
  48. cdv_intel_lvds_init(dev, &dev_priv->mode_dev);
  49. /* These bits indicate HDMI not SDVO on CDV, but we don't yet support
  50. the HDMI interface */
  51. if (REG_READ(SDVOB) & SDVO_DETECTED)
  52. cdv_hdmi_init(dev, &dev_priv->mode_dev, SDVOB);
  53. if (REG_READ(SDVOC) & SDVO_DETECTED)
  54. cdv_hdmi_init(dev, &dev_priv->mode_dev, SDVOC);
  55. return 0;
  56. }
  57. #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
  58. /*
  59. * Poulsbo Backlight Interfaces
  60. */
  61. #define BLC_PWM_PRECISION_FACTOR 100 /* 10000000 */
  62. #define BLC_PWM_FREQ_CALC_CONSTANT 32
  63. #define MHz 1000000
  64. #define PSB_BLC_PWM_PRECISION_FACTOR 10
  65. #define PSB_BLC_MAX_PWM_REG_FREQ 0xFFFE
  66. #define PSB_BLC_MIN_PWM_REG_FREQ 0x2
  67. #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
  68. #define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
  69. static int cdv_brightness;
  70. static struct backlight_device *cdv_backlight_device;
  71. static int cdv_get_brightness(struct backlight_device *bd)
  72. {
  73. /* return locally cached var instead of HW read (due to DPST etc.) */
  74. /* FIXME: ideally return actual value in case firmware fiddled with
  75. it */
  76. return cdv_brightness;
  77. }
  78. static int cdv_backlight_setup(struct drm_device *dev)
  79. {
  80. struct drm_psb_private *dev_priv = dev->dev_private;
  81. unsigned long core_clock;
  82. /* u32 bl_max_freq; */
  83. /* unsigned long value; */
  84. u16 bl_max_freq;
  85. uint32_t value;
  86. uint32_t blc_pwm_precision_factor;
  87. /* get bl_max_freq and pol from dev_priv*/
  88. if (!dev_priv->lvds_bl) {
  89. dev_err(dev->dev, "Has no valid LVDS backlight info\n");
  90. return -ENOENT;
  91. }
  92. bl_max_freq = dev_priv->lvds_bl->freq;
  93. blc_pwm_precision_factor = PSB_BLC_PWM_PRECISION_FACTOR;
  94. core_clock = dev_priv->core_freq;
  95. value = (core_clock * MHz) / BLC_PWM_FREQ_CALC_CONSTANT;
  96. value *= blc_pwm_precision_factor;
  97. value /= bl_max_freq;
  98. value /= blc_pwm_precision_factor;
  99. if (value > (unsigned long long)PSB_BLC_MAX_PWM_REG_FREQ ||
  100. value < (unsigned long long)PSB_BLC_MIN_PWM_REG_FREQ)
  101. return -ERANGE;
  102. else {
  103. /* FIXME */
  104. }
  105. return 0;
  106. }
  107. static int cdv_set_brightness(struct backlight_device *bd)
  108. {
  109. int level = bd->props.brightness;
  110. /* Percentage 1-100% being valid */
  111. if (level < 1)
  112. level = 1;
  113. /*cdv_intel_lvds_set_brightness(dev, level); FIXME */
  114. cdv_brightness = level;
  115. return 0;
  116. }
  117. static const struct backlight_ops cdv_ops = {
  118. .get_brightness = cdv_get_brightness,
  119. .update_status = cdv_set_brightness,
  120. };
  121. static int cdv_backlight_init(struct drm_device *dev)
  122. {
  123. struct drm_psb_private *dev_priv = dev->dev_private;
  124. int ret;
  125. struct backlight_properties props;
  126. memset(&props, 0, sizeof(struct backlight_properties));
  127. props.max_brightness = 100;
  128. props.type = BACKLIGHT_PLATFORM;
  129. cdv_backlight_device = backlight_device_register("psb-bl",
  130. NULL, (void *)dev, &cdv_ops, &props);
  131. if (IS_ERR(cdv_backlight_device))
  132. return PTR_ERR(cdv_backlight_device);
  133. ret = cdv_backlight_setup(dev);
  134. if (ret < 0) {
  135. backlight_device_unregister(cdv_backlight_device);
  136. cdv_backlight_device = NULL;
  137. return ret;
  138. }
  139. cdv_backlight_device->props.brightness = 100;
  140. cdv_backlight_device->props.max_brightness = 100;
  141. backlight_update_status(cdv_backlight_device);
  142. dev_priv->backlight_device = cdv_backlight_device;
  143. return 0;
  144. }
  145. #endif
  146. /*
  147. * Provide the Cedarview specific chip logic and low level methods
  148. * for power management
  149. *
  150. * FIXME: we need to implement the apm/ospm base management bits
  151. * for this and the MID devices.
  152. */
  153. static inline u32 CDV_MSG_READ32(uint port, uint offset)
  154. {
  155. int mcr = (0x10<<24) | (port << 16) | (offset << 8);
  156. uint32_t ret_val = 0;
  157. struct pci_dev *pci_root = pci_get_bus_and_slot(0, 0);
  158. pci_write_config_dword(pci_root, 0xD0, mcr);
  159. pci_read_config_dword(pci_root, 0xD4, &ret_val);
  160. pci_dev_put(pci_root);
  161. return ret_val;
  162. }
  163. static inline void CDV_MSG_WRITE32(uint port, uint offset, u32 value)
  164. {
  165. int mcr = (0x11<<24) | (port << 16) | (offset << 8) | 0xF0;
  166. struct pci_dev *pci_root = pci_get_bus_and_slot(0, 0);
  167. pci_write_config_dword(pci_root, 0xD4, value);
  168. pci_write_config_dword(pci_root, 0xD0, mcr);
  169. pci_dev_put(pci_root);
  170. }
  171. #define PSB_APM_CMD 0x0
  172. #define PSB_APM_STS 0x04
  173. #define PSB_PM_SSC 0x20
  174. #define PSB_PM_SSS 0x30
  175. #define PSB_PWRGT_GFX_MASK 0x3
  176. #define CDV_PWRGT_DISPLAY_CNTR 0x000fc00c
  177. #define CDV_PWRGT_DISPLAY_STS 0x000fc00c
  178. static void cdv_init_pm(struct drm_device *dev)
  179. {
  180. struct drm_psb_private *dev_priv = dev->dev_private;
  181. u32 pwr_cnt;
  182. int i;
  183. dev_priv->apm_base = CDV_MSG_READ32(PSB_PUNIT_PORT,
  184. PSB_APMBA) & 0xFFFF;
  185. dev_priv->ospm_base = CDV_MSG_READ32(PSB_PUNIT_PORT,
  186. PSB_OSPMBA) & 0xFFFF;
  187. /* Force power on for now */
  188. pwr_cnt = inl(dev_priv->apm_base + PSB_APM_CMD);
  189. pwr_cnt &= ~PSB_PWRGT_GFX_MASK;
  190. outl(pwr_cnt, dev_priv->apm_base + PSB_APM_CMD);
  191. for (i = 0; i < 5; i++) {
  192. u32 pwr_sts = inl(dev_priv->apm_base + PSB_APM_STS);
  193. if ((pwr_sts & PSB_PWRGT_GFX_MASK) == 0)
  194. break;
  195. udelay(10);
  196. }
  197. pwr_cnt = inl(dev_priv->ospm_base + PSB_PM_SSC);
  198. pwr_cnt &= ~CDV_PWRGT_DISPLAY_CNTR;
  199. outl(pwr_cnt, dev_priv->ospm_base + PSB_PM_SSC);
  200. for (i = 0; i < 5; i++) {
  201. u32 pwr_sts = inl(dev_priv->ospm_base + PSB_PM_SSS);
  202. if ((pwr_sts & CDV_PWRGT_DISPLAY_STS) == 0)
  203. break;
  204. udelay(10);
  205. }
  206. }
  207. /**
  208. * cdv_save_display_registers - save registers lost on suspend
  209. * @dev: our DRM device
  210. *
  211. * Save the state we need in order to be able to restore the interface
  212. * upon resume from suspend
  213. *
  214. * FIXME: review
  215. */
  216. static int cdv_save_display_registers(struct drm_device *dev)
  217. {
  218. return 0;
  219. }
  220. /**
  221. * cdv_restore_display_registers - restore lost register state
  222. * @dev: our DRM device
  223. *
  224. * Restore register state that was lost during suspend and resume.
  225. *
  226. * FIXME: review
  227. */
  228. static int cdv_restore_display_registers(struct drm_device *dev)
  229. {
  230. return 0;
  231. }
  232. static int cdv_power_down(struct drm_device *dev)
  233. {
  234. return 0;
  235. }
  236. static int cdv_power_up(struct drm_device *dev)
  237. {
  238. return 0;
  239. }
  240. /* FIXME ? - shared with Poulsbo */
  241. static void cdv_get_core_freq(struct drm_device *dev)
  242. {
  243. uint32_t clock;
  244. struct pci_dev *pci_root = pci_get_bus_and_slot(0, 0);
  245. struct drm_psb_private *dev_priv = dev->dev_private;
  246. pci_write_config_dword(pci_root, 0xD0, 0xD0050300);
  247. pci_read_config_dword(pci_root, 0xD4, &clock);
  248. pci_dev_put(pci_root);
  249. switch (clock & 0x07) {
  250. case 0:
  251. dev_priv->core_freq = 100;
  252. break;
  253. case 1:
  254. dev_priv->core_freq = 133;
  255. break;
  256. case 2:
  257. dev_priv->core_freq = 150;
  258. break;
  259. case 3:
  260. dev_priv->core_freq = 178;
  261. break;
  262. case 4:
  263. dev_priv->core_freq = 200;
  264. break;
  265. case 5:
  266. case 6:
  267. case 7:
  268. dev_priv->core_freq = 266;
  269. default:
  270. dev_priv->core_freq = 0;
  271. }
  272. }
  273. static int cdv_chip_setup(struct drm_device *dev)
  274. {
  275. cdv_get_core_freq(dev);
  276. gma_intel_opregion_init(dev);
  277. psb_intel_init_bios(dev);
  278. return 0;
  279. }
  280. /* CDV is much like Poulsbo but has MID like SGX offsets and PM */
  281. const struct psb_ops cdv_chip_ops = {
  282. .name = "Cedartrail",
  283. .accel_2d = 0,
  284. .pipes = 2,
  285. .sgx_offset = MRST_SGX_OFFSET,
  286. .chip_setup = cdv_chip_setup,
  287. .crtc_helper = &cdv_intel_helper_funcs,
  288. .crtc_funcs = &cdv_intel_crtc_funcs,
  289. .output_init = cdv_output_init,
  290. #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
  291. .backlight_init = cdv_backlight_init,
  292. #endif
  293. .init_pm = cdv_init_pm,
  294. .save_regs = cdv_save_display_registers,
  295. .restore_regs = cdv_restore_display_registers,
  296. .power_down = cdv_power_down,
  297. .power_up = cdv_power_up,
  298. };