psb_device.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. static int psb_output_init(struct drm_device *dev)
  28. {
  29. struct drm_psb_private *dev_priv = dev->dev_private;
  30. psb_intel_lvds_init(dev, &dev_priv->mode_dev);
  31. psb_intel_sdvo_init(dev, SDVOB);
  32. return 0;
  33. }
  34. #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
  35. /*
  36. * Poulsbo Backlight Interfaces
  37. */
  38. #define BLC_PWM_PRECISION_FACTOR 100 /* 10000000 */
  39. #define BLC_PWM_FREQ_CALC_CONSTANT 32
  40. #define MHz 1000000
  41. #define PSB_BLC_PWM_PRECISION_FACTOR 10
  42. #define PSB_BLC_MAX_PWM_REG_FREQ 0xFFFE
  43. #define PSB_BLC_MIN_PWM_REG_FREQ 0x2
  44. #define PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR (0xFFFE)
  45. #define PSB_BACKLIGHT_PWM_CTL_SHIFT (16)
  46. static int psb_brightness;
  47. static struct backlight_device *psb_backlight_device;
  48. static int psb_get_brightness(struct backlight_device *bd)
  49. {
  50. /* return locally cached var instead of HW read (due to DPST etc.) */
  51. /* FIXME: ideally return actual value in case firmware fiddled with
  52. it */
  53. return psb_brightness;
  54. }
  55. static int psb_backlight_setup(struct drm_device *dev)
  56. {
  57. struct drm_psb_private *dev_priv = dev->dev_private;
  58. unsigned long core_clock;
  59. /* u32 bl_max_freq; */
  60. /* unsigned long value; */
  61. u16 bl_max_freq;
  62. uint32_t value;
  63. uint32_t blc_pwm_precision_factor;
  64. /* get bl_max_freq and pol from dev_priv*/
  65. if (!dev_priv->lvds_bl) {
  66. dev_err(dev->dev, "Has no valid LVDS backlight info\n");
  67. return -ENOENT;
  68. }
  69. bl_max_freq = dev_priv->lvds_bl->freq;
  70. blc_pwm_precision_factor = PSB_BLC_PWM_PRECISION_FACTOR;
  71. core_clock = dev_priv->core_freq;
  72. value = (core_clock * MHz) / BLC_PWM_FREQ_CALC_CONSTANT;
  73. value *= blc_pwm_precision_factor;
  74. value /= bl_max_freq;
  75. value /= blc_pwm_precision_factor;
  76. if (value > (unsigned long long)PSB_BLC_MAX_PWM_REG_FREQ ||
  77. value < (unsigned long long)PSB_BLC_MIN_PWM_REG_FREQ)
  78. return -ERANGE;
  79. else {
  80. value &= PSB_BACKLIGHT_PWM_POLARITY_BIT_CLEAR;
  81. REG_WRITE(BLC_PWM_CTL,
  82. (value << PSB_BACKLIGHT_PWM_CTL_SHIFT) | (value));
  83. }
  84. return 0;
  85. }
  86. static int psb_set_brightness(struct backlight_device *bd)
  87. {
  88. struct drm_device *dev = bl_get_data(psb_backlight_device);
  89. int level = bd->props.brightness;
  90. /* Percentage 1-100% being valid */
  91. if (level < 1)
  92. level = 1;
  93. psb_intel_lvds_set_brightness(dev, level);
  94. psb_brightness = level;
  95. return 0;
  96. }
  97. static const struct backlight_ops psb_ops = {
  98. .get_brightness = psb_get_brightness,
  99. .update_status = psb_set_brightness,
  100. };
  101. static int psb_backlight_init(struct drm_device *dev)
  102. {
  103. struct drm_psb_private *dev_priv = dev->dev_private;
  104. int ret;
  105. struct backlight_properties props;
  106. memset(&props, 0, sizeof(struct backlight_properties));
  107. props.max_brightness = 100;
  108. props.type = BACKLIGHT_PLATFORM;
  109. psb_backlight_device = backlight_device_register("psb-bl",
  110. NULL, (void *)dev, &psb_ops, &props);
  111. if (IS_ERR(psb_backlight_device))
  112. return PTR_ERR(psb_backlight_device);
  113. ret = psb_backlight_setup(dev);
  114. if (ret < 0) {
  115. backlight_device_unregister(psb_backlight_device);
  116. psb_backlight_device = NULL;
  117. return ret;
  118. }
  119. psb_backlight_device->props.brightness = 100;
  120. psb_backlight_device->props.max_brightness = 100;
  121. backlight_update_status(psb_backlight_device);
  122. dev_priv->backlight_device = psb_backlight_device;
  123. return 0;
  124. }
  125. #endif
  126. /*
  127. * Provide the Poulsbo specific chip logic and low level methods
  128. * for power management
  129. */
  130. static void psb_init_pm(struct drm_device *dev)
  131. {
  132. struct drm_psb_private *dev_priv = dev->dev_private;
  133. u32 gating = PSB_RSGX32(PSB_CR_CLKGATECTL);
  134. gating &= ~3; /* Disable 2D clock gating */
  135. gating |= 1;
  136. PSB_WSGX32(gating, PSB_CR_CLKGATECTL);
  137. PSB_RSGX32(PSB_CR_CLKGATECTL);
  138. }
  139. /**
  140. * psb_save_display_registers - save registers lost on suspend
  141. * @dev: our DRM device
  142. *
  143. * Save the state we need in order to be able to restore the interface
  144. * upon resume from suspend
  145. */
  146. static int psb_save_display_registers(struct drm_device *dev)
  147. {
  148. struct drm_psb_private *dev_priv = dev->dev_private;
  149. struct drm_crtc *crtc;
  150. struct drm_connector *connector;
  151. /* Display arbitration control + watermarks */
  152. dev_priv->saveDSPARB = PSB_RVDC32(DSPARB);
  153. dev_priv->saveDSPFW1 = PSB_RVDC32(DSPFW1);
  154. dev_priv->saveDSPFW2 = PSB_RVDC32(DSPFW2);
  155. dev_priv->saveDSPFW3 = PSB_RVDC32(DSPFW3);
  156. dev_priv->saveDSPFW4 = PSB_RVDC32(DSPFW4);
  157. dev_priv->saveDSPFW5 = PSB_RVDC32(DSPFW5);
  158. dev_priv->saveDSPFW6 = PSB_RVDC32(DSPFW6);
  159. dev_priv->saveCHICKENBIT = PSB_RVDC32(DSPCHICKENBIT);
  160. /* Save crtc and output state */
  161. mutex_lock(&dev->mode_config.mutex);
  162. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  163. if (drm_helper_crtc_in_use(crtc))
  164. crtc->funcs->save(crtc);
  165. }
  166. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  167. connector->funcs->save(connector);
  168. mutex_unlock(&dev->mode_config.mutex);
  169. return 0;
  170. }
  171. /**
  172. * psb_restore_display_registers - restore lost register state
  173. * @dev: our DRM device
  174. *
  175. * Restore register state that was lost during suspend and resume.
  176. */
  177. static int psb_restore_display_registers(struct drm_device *dev)
  178. {
  179. struct drm_psb_private *dev_priv = dev->dev_private;
  180. struct drm_crtc *crtc;
  181. struct drm_connector *connector;
  182. int pp_stat;
  183. /* Display arbitration + watermarks */
  184. PSB_WVDC32(dev_priv->saveDSPARB, DSPARB);
  185. PSB_WVDC32(dev_priv->saveDSPFW1, DSPFW1);
  186. PSB_WVDC32(dev_priv->saveDSPFW2, DSPFW2);
  187. PSB_WVDC32(dev_priv->saveDSPFW3, DSPFW3);
  188. PSB_WVDC32(dev_priv->saveDSPFW4, DSPFW4);
  189. PSB_WVDC32(dev_priv->saveDSPFW5, DSPFW5);
  190. PSB_WVDC32(dev_priv->saveDSPFW6, DSPFW6);
  191. PSB_WVDC32(dev_priv->saveCHICKENBIT, DSPCHICKENBIT);
  192. /*make sure VGA plane is off. it initializes to on after reset!*/
  193. PSB_WVDC32(0x80000000, VGACNTRL);
  194. mutex_lock(&dev->mode_config.mutex);
  195. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  196. if (drm_helper_crtc_in_use(crtc))
  197. crtc->funcs->restore(crtc);
  198. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  199. connector->funcs->restore(connector);
  200. mutex_unlock(&dev->mode_config.mutex);
  201. if (dev_priv->iLVDS_enable) {
  202. /*shutdown the panel*/
  203. PSB_WVDC32(0, PP_CONTROL);
  204. do {
  205. pp_stat = PSB_RVDC32(PP_STATUS);
  206. } while (pp_stat & 0x80000000);
  207. /* Turn off the plane */
  208. PSB_WVDC32(0x58000000, DSPACNTR);
  209. PSB_WVDC32(0, DSPASURF);/*trigger the plane disable*/
  210. /* Wait ~4 ticks */
  211. msleep(4);
  212. /* Turn off pipe */
  213. PSB_WVDC32(0x0, PIPEACONF);
  214. /* Wait ~8 ticks */
  215. msleep(8);
  216. /* Turn off PLLs */
  217. PSB_WVDC32(0, MRST_DPLL_A);
  218. } else {
  219. PSB_WVDC32(DPI_SHUT_DOWN, DPI_CONTROL_REG);
  220. PSB_WVDC32(0x0, PIPEACONF);
  221. PSB_WVDC32(0x2faf0000, BLC_PWM_CTL);
  222. while (REG_READ(0x70008) & 0x40000000)
  223. cpu_relax();
  224. while ((PSB_RVDC32(GEN_FIFO_STAT_REG) & DPI_FIFO_EMPTY)
  225. != DPI_FIFO_EMPTY)
  226. cpu_relax();
  227. PSB_WVDC32(0, DEVICE_READY_REG);
  228. }
  229. return 0;
  230. }
  231. static int psb_power_down(struct drm_device *dev)
  232. {
  233. return 0;
  234. }
  235. static int psb_power_up(struct drm_device *dev)
  236. {
  237. return 0;
  238. }
  239. static void psb_get_core_freq(struct drm_device *dev)
  240. {
  241. uint32_t clock;
  242. struct pci_dev *pci_root = pci_get_bus_and_slot(0, 0);
  243. struct drm_psb_private *dev_priv = dev->dev_private;
  244. /*pci_write_config_dword(pci_root, 0xD4, 0x00C32004);*/
  245. /*pci_write_config_dword(pci_root, 0xD0, 0xE0033000);*/
  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 psb_chip_setup(struct drm_device *dev)
  274. {
  275. psb_get_core_freq(dev);
  276. gma_intel_opregion_init(dev);
  277. psb_intel_init_bios(dev);
  278. return 0;
  279. }
  280. const struct psb_ops psb_chip_ops = {
  281. .name = "Poulsbo",
  282. .accel_2d = 1,
  283. .pipes = 2,
  284. .crtcs = 2,
  285. .sgx_offset = PSB_SGX_OFFSET,
  286. .chip_setup = psb_chip_setup,
  287. .crtc_helper = &psb_intel_helper_funcs,
  288. .crtc_funcs = &psb_intel_crtc_funcs,
  289. .output_init = psb_output_init,
  290. #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
  291. .backlight_init = psb_backlight_init,
  292. #endif
  293. .init_pm = psb_init_pm,
  294. .save_regs = psb_save_display_registers,
  295. .restore_regs = psb_restore_display_registers,
  296. .power_down = psb_power_down,
  297. .power_up = psb_power_up,
  298. };