dvo_ch7017.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright © 2006 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include "dvo.h"
  28. #define CH7017_TV_DISPLAY_MODE 0x00
  29. #define CH7017_FLICKER_FILTER 0x01
  30. #define CH7017_VIDEO_BANDWIDTH 0x02
  31. #define CH7017_TEXT_ENHANCEMENT 0x03
  32. #define CH7017_START_ACTIVE_VIDEO 0x04
  33. #define CH7017_HORIZONTAL_POSITION 0x05
  34. #define CH7017_VERTICAL_POSITION 0x06
  35. #define CH7017_BLACK_LEVEL 0x07
  36. #define CH7017_CONTRAST_ENHANCEMENT 0x08
  37. #define CH7017_TV_PLL 0x09
  38. #define CH7017_TV_PLL_M 0x0a
  39. #define CH7017_TV_PLL_N 0x0b
  40. #define CH7017_SUB_CARRIER_0 0x0c
  41. #define CH7017_CIV_CONTROL 0x10
  42. #define CH7017_CIV_0 0x11
  43. #define CH7017_CHROMA_BOOST 0x14
  44. #define CH7017_CLOCK_MODE 0x1c
  45. #define CH7017_INPUT_CLOCK 0x1d
  46. #define CH7017_GPIO_CONTROL 0x1e
  47. #define CH7017_INPUT_DATA_FORMAT 0x1f
  48. #define CH7017_CONNECTION_DETECT 0x20
  49. #define CH7017_DAC_CONTROL 0x21
  50. #define CH7017_BUFFERED_CLOCK_OUTPUT 0x22
  51. #define CH7017_DEFEAT_VSYNC 0x47
  52. #define CH7017_TEST_PATTERN 0x48
  53. #define CH7017_POWER_MANAGEMENT 0x49
  54. /** Enables the TV output path. */
  55. #define CH7017_TV_EN (1 << 0)
  56. #define CH7017_DAC0_POWER_DOWN (1 << 1)
  57. #define CH7017_DAC1_POWER_DOWN (1 << 2)
  58. #define CH7017_DAC2_POWER_DOWN (1 << 3)
  59. #define CH7017_DAC3_POWER_DOWN (1 << 4)
  60. /** Powers down the TV out block, and DAC0-3 */
  61. #define CH7017_TV_POWER_DOWN_EN (1 << 5)
  62. #define CH7017_VERSION_ID 0x4a
  63. #define CH7017_DEVICE_ID 0x4b
  64. #define CH7017_DEVICE_ID_VALUE 0x1b
  65. #define CH7018_DEVICE_ID_VALUE 0x1a
  66. #define CH7019_DEVICE_ID_VALUE 0x19
  67. #define CH7017_XCLK_D2_ADJUST 0x53
  68. #define CH7017_UP_SCALER_COEFF_0 0x55
  69. #define CH7017_UP_SCALER_COEFF_1 0x56
  70. #define CH7017_UP_SCALER_COEFF_2 0x57
  71. #define CH7017_UP_SCALER_COEFF_3 0x58
  72. #define CH7017_UP_SCALER_COEFF_4 0x59
  73. #define CH7017_UP_SCALER_VERTICAL_INC_0 0x5a
  74. #define CH7017_UP_SCALER_VERTICAL_INC_1 0x5b
  75. #define CH7017_GPIO_INVERT 0x5c
  76. #define CH7017_UP_SCALER_HORIZONTAL_INC_0 0x5d
  77. #define CH7017_UP_SCALER_HORIZONTAL_INC_1 0x5e
  78. #define CH7017_HORIZONTAL_ACTIVE_PIXEL_INPUT 0x5f
  79. /**< Low bits of horizontal active pixel input */
  80. #define CH7017_ACTIVE_INPUT_LINE_OUTPUT 0x60
  81. /** High bits of horizontal active pixel input */
  82. #define CH7017_LVDS_HAP_INPUT_MASK (0x7 << 0)
  83. /** High bits of vertical active line output */
  84. #define CH7017_LVDS_VAL_HIGH_MASK (0x7 << 3)
  85. #define CH7017_VERTICAL_ACTIVE_LINE_OUTPUT 0x61
  86. /**< Low bits of vertical active line output */
  87. #define CH7017_HORIZONTAL_ACTIVE_PIXEL_OUTPUT 0x62
  88. /**< Low bits of horizontal active pixel output */
  89. #define CH7017_LVDS_POWER_DOWN 0x63
  90. /** High bits of horizontal active pixel output */
  91. #define CH7017_LVDS_HAP_HIGH_MASK (0x7 << 0)
  92. /** Enables the LVDS power down state transition */
  93. #define CH7017_LVDS_POWER_DOWN_EN (1 << 6)
  94. /** Enables the LVDS upscaler */
  95. #define CH7017_LVDS_UPSCALER_EN (1 << 7)
  96. #define CH7017_LVDS_POWER_DOWN_DEFAULT_RESERVED 0x08
  97. #define CH7017_LVDS_ENCODING 0x64
  98. #define CH7017_LVDS_DITHER_2D (1 << 2)
  99. #define CH7017_LVDS_DITHER_DIS (1 << 3)
  100. #define CH7017_LVDS_DUAL_CHANNEL_EN (1 << 4)
  101. #define CH7017_LVDS_24_BIT (1 << 5)
  102. #define CH7017_LVDS_ENCODING_2 0x65
  103. #define CH7017_LVDS_PLL_CONTROL 0x66
  104. /** Enables the LVDS panel output path */
  105. #define CH7017_LVDS_PANEN (1 << 0)
  106. /** Enables the LVDS panel backlight */
  107. #define CH7017_LVDS_BKLEN (1 << 3)
  108. #define CH7017_POWER_SEQUENCING_T1 0x67
  109. #define CH7017_POWER_SEQUENCING_T2 0x68
  110. #define CH7017_POWER_SEQUENCING_T3 0x69
  111. #define CH7017_POWER_SEQUENCING_T4 0x6a
  112. #define CH7017_POWER_SEQUENCING_T5 0x6b
  113. #define CH7017_GPIO_DRIVER_TYPE 0x6c
  114. #define CH7017_GPIO_DATA 0x6d
  115. #define CH7017_GPIO_DIRECTION_CONTROL 0x6e
  116. #define CH7017_LVDS_PLL_FEEDBACK_DIV 0x71
  117. # define CH7017_LVDS_PLL_FEED_BACK_DIVIDER_SHIFT 4
  118. # define CH7017_LVDS_PLL_FEED_FORWARD_DIVIDER_SHIFT 0
  119. # define CH7017_LVDS_PLL_FEEDBACK_DEFAULT_RESERVED 0x80
  120. #define CH7017_LVDS_PLL_VCO_CONTROL 0x72
  121. # define CH7017_LVDS_PLL_VCO_DEFAULT_RESERVED 0x80
  122. # define CH7017_LVDS_PLL_VCO_SHIFT 4
  123. # define CH7017_LVDS_PLL_POST_SCALE_DIV_SHIFT 0
  124. #define CH7017_OUTPUTS_ENABLE 0x73
  125. # define CH7017_CHARGE_PUMP_LOW 0x0
  126. # define CH7017_CHARGE_PUMP_HIGH 0x3
  127. # define CH7017_LVDS_CHANNEL_A (1 << 3)
  128. # define CH7017_LVDS_CHANNEL_B (1 << 4)
  129. # define CH7017_TV_DAC_A (1 << 5)
  130. # define CH7017_TV_DAC_B (1 << 6)
  131. # define CH7017_DDC_SELECT_DC2 (1 << 7)
  132. #define CH7017_LVDS_OUTPUT_AMPLITUDE 0x74
  133. #define CH7017_LVDS_PLL_EMI_REDUCTION 0x75
  134. #define CH7017_LVDS_POWER_DOWN_FLICKER 0x76
  135. #define CH7017_LVDS_CONTROL_2 0x78
  136. # define CH7017_LOOP_FILTER_SHIFT 5
  137. # define CH7017_PHASE_DETECTOR_SHIFT 0
  138. #define CH7017_BANG_LIMIT_CONTROL 0x7f
  139. struct ch7017_priv {
  140. uint8_t dummy;
  141. };
  142. static void ch7017_dump_regs(struct intel_dvo_device *dvo);
  143. static void ch7017_dpms(struct intel_dvo_device *dvo, bool enable);
  144. static bool ch7017_read(struct intel_dvo_device *dvo, u8 addr, u8 *val)
  145. {
  146. struct i2c_msg msgs[] = {
  147. {
  148. .addr = dvo->slave_addr,
  149. .flags = 0,
  150. .len = 1,
  151. .buf = &addr,
  152. },
  153. {
  154. .addr = dvo->slave_addr,
  155. .flags = I2C_M_RD,
  156. .len = 1,
  157. .buf = val,
  158. }
  159. };
  160. return i2c_transfer(dvo->i2c_bus, msgs, 2) == 2;
  161. }
  162. static bool ch7017_write(struct intel_dvo_device *dvo, u8 addr, u8 val)
  163. {
  164. uint8_t buf[2] = { addr, val };
  165. struct i2c_msg msg = {
  166. .addr = dvo->slave_addr,
  167. .flags = 0,
  168. .len = 2,
  169. .buf = buf,
  170. };
  171. return i2c_transfer(dvo->i2c_bus, &msg, 1) == 1;
  172. }
  173. /** Probes for a CH7017 on the given bus and slave address. */
  174. static bool ch7017_init(struct intel_dvo_device *dvo,
  175. struct i2c_adapter *adapter)
  176. {
  177. struct ch7017_priv *priv;
  178. const char *str;
  179. u8 val;
  180. priv = kzalloc(sizeof(struct ch7017_priv), GFP_KERNEL);
  181. if (priv == NULL)
  182. return false;
  183. dvo->i2c_bus = adapter;
  184. dvo->dev_priv = priv;
  185. if (!ch7017_read(dvo, CH7017_DEVICE_ID, &val))
  186. goto fail;
  187. switch (val) {
  188. case CH7017_DEVICE_ID_VALUE:
  189. str = "ch7017";
  190. break;
  191. case CH7018_DEVICE_ID_VALUE:
  192. str = "ch7018";
  193. break;
  194. case CH7019_DEVICE_ID_VALUE:
  195. str = "ch7019";
  196. break;
  197. default:
  198. DRM_DEBUG_KMS("ch701x not detected, got %d: from %s "
  199. "slave %d.\n",
  200. val, adapter->name, dvo->slave_addr);
  201. goto fail;
  202. }
  203. DRM_DEBUG_KMS("%s detected on %s, addr %d\n",
  204. str, adapter->name, dvo->slave_addr);
  205. return true;
  206. fail:
  207. kfree(priv);
  208. return false;
  209. }
  210. static enum drm_connector_status ch7017_detect(struct intel_dvo_device *dvo)
  211. {
  212. return connector_status_connected;
  213. }
  214. static enum drm_mode_status ch7017_mode_valid(struct intel_dvo_device *dvo,
  215. struct drm_display_mode *mode)
  216. {
  217. if (mode->clock > 160000)
  218. return MODE_CLOCK_HIGH;
  219. return MODE_OK;
  220. }
  221. static void ch7017_mode_set(struct intel_dvo_device *dvo,
  222. const struct drm_display_mode *mode,
  223. const struct drm_display_mode *adjusted_mode)
  224. {
  225. uint8_t lvds_pll_feedback_div, lvds_pll_vco_control;
  226. uint8_t outputs_enable, lvds_control_2, lvds_power_down;
  227. uint8_t horizontal_active_pixel_input;
  228. uint8_t horizontal_active_pixel_output, vertical_active_line_output;
  229. uint8_t active_input_line_output;
  230. DRM_DEBUG_KMS("Registers before mode setting\n");
  231. ch7017_dump_regs(dvo);
  232. /* LVDS PLL settings from page 75 of 7017-7017ds.pdf*/
  233. if (mode->clock < 100000) {
  234. outputs_enable = CH7017_LVDS_CHANNEL_A | CH7017_CHARGE_PUMP_LOW;
  235. lvds_pll_feedback_div = CH7017_LVDS_PLL_FEEDBACK_DEFAULT_RESERVED |
  236. (2 << CH7017_LVDS_PLL_FEED_BACK_DIVIDER_SHIFT) |
  237. (13 << CH7017_LVDS_PLL_FEED_FORWARD_DIVIDER_SHIFT);
  238. lvds_pll_vco_control = CH7017_LVDS_PLL_VCO_DEFAULT_RESERVED |
  239. (2 << CH7017_LVDS_PLL_VCO_SHIFT) |
  240. (3 << CH7017_LVDS_PLL_POST_SCALE_DIV_SHIFT);
  241. lvds_control_2 = (1 << CH7017_LOOP_FILTER_SHIFT) |
  242. (0 << CH7017_PHASE_DETECTOR_SHIFT);
  243. } else {
  244. outputs_enable = CH7017_LVDS_CHANNEL_A | CH7017_CHARGE_PUMP_HIGH;
  245. lvds_pll_feedback_div = CH7017_LVDS_PLL_FEEDBACK_DEFAULT_RESERVED |
  246. (2 << CH7017_LVDS_PLL_FEED_BACK_DIVIDER_SHIFT) |
  247. (3 << CH7017_LVDS_PLL_FEED_FORWARD_DIVIDER_SHIFT);
  248. lvds_pll_feedback_div = 35;
  249. lvds_control_2 = (3 << CH7017_LOOP_FILTER_SHIFT) |
  250. (0 << CH7017_PHASE_DETECTOR_SHIFT);
  251. if (1) { /* XXX: dual channel panel detection. Assume yes for now. */
  252. outputs_enable |= CH7017_LVDS_CHANNEL_B;
  253. lvds_pll_vco_control = CH7017_LVDS_PLL_VCO_DEFAULT_RESERVED |
  254. (2 << CH7017_LVDS_PLL_VCO_SHIFT) |
  255. (13 << CH7017_LVDS_PLL_POST_SCALE_DIV_SHIFT);
  256. } else {
  257. lvds_pll_vco_control = CH7017_LVDS_PLL_VCO_DEFAULT_RESERVED |
  258. (1 << CH7017_LVDS_PLL_VCO_SHIFT) |
  259. (13 << CH7017_LVDS_PLL_POST_SCALE_DIV_SHIFT);
  260. }
  261. }
  262. horizontal_active_pixel_input = mode->hdisplay & 0x00ff;
  263. vertical_active_line_output = mode->vdisplay & 0x00ff;
  264. horizontal_active_pixel_output = mode->hdisplay & 0x00ff;
  265. active_input_line_output = ((mode->hdisplay & 0x0700) >> 8) |
  266. (((mode->vdisplay & 0x0700) >> 8) << 3);
  267. lvds_power_down = CH7017_LVDS_POWER_DOWN_DEFAULT_RESERVED |
  268. (mode->hdisplay & 0x0700) >> 8;
  269. ch7017_dpms(dvo, false);
  270. ch7017_write(dvo, CH7017_HORIZONTAL_ACTIVE_PIXEL_INPUT,
  271. horizontal_active_pixel_input);
  272. ch7017_write(dvo, CH7017_HORIZONTAL_ACTIVE_PIXEL_OUTPUT,
  273. horizontal_active_pixel_output);
  274. ch7017_write(dvo, CH7017_VERTICAL_ACTIVE_LINE_OUTPUT,
  275. vertical_active_line_output);
  276. ch7017_write(dvo, CH7017_ACTIVE_INPUT_LINE_OUTPUT,
  277. active_input_line_output);
  278. ch7017_write(dvo, CH7017_LVDS_PLL_VCO_CONTROL, lvds_pll_vco_control);
  279. ch7017_write(dvo, CH7017_LVDS_PLL_FEEDBACK_DIV, lvds_pll_feedback_div);
  280. ch7017_write(dvo, CH7017_LVDS_CONTROL_2, lvds_control_2);
  281. ch7017_write(dvo, CH7017_OUTPUTS_ENABLE, outputs_enable);
  282. /* Turn the LVDS back on with new settings. */
  283. ch7017_write(dvo, CH7017_LVDS_POWER_DOWN, lvds_power_down);
  284. DRM_DEBUG_KMS("Registers after mode setting\n");
  285. ch7017_dump_regs(dvo);
  286. }
  287. /* set the CH7017 power state */
  288. static void ch7017_dpms(struct intel_dvo_device *dvo, bool enable)
  289. {
  290. uint8_t val;
  291. ch7017_read(dvo, CH7017_LVDS_POWER_DOWN, &val);
  292. /* Turn off TV/VGA, and never turn it on since we don't support it. */
  293. ch7017_write(dvo, CH7017_POWER_MANAGEMENT,
  294. CH7017_DAC0_POWER_DOWN |
  295. CH7017_DAC1_POWER_DOWN |
  296. CH7017_DAC2_POWER_DOWN |
  297. CH7017_DAC3_POWER_DOWN |
  298. CH7017_TV_POWER_DOWN_EN);
  299. if (enable) {
  300. /* Turn on the LVDS */
  301. ch7017_write(dvo, CH7017_LVDS_POWER_DOWN,
  302. val & ~CH7017_LVDS_POWER_DOWN_EN);
  303. } else {
  304. /* Turn off the LVDS */
  305. ch7017_write(dvo, CH7017_LVDS_POWER_DOWN,
  306. val | CH7017_LVDS_POWER_DOWN_EN);
  307. }
  308. /* XXX: Should actually wait for update power status somehow */
  309. msleep(20);
  310. }
  311. static bool ch7017_get_hw_state(struct intel_dvo_device *dvo)
  312. {
  313. uint8_t val;
  314. ch7017_read(dvo, CH7017_LVDS_POWER_DOWN, &val);
  315. if (val & CH7017_LVDS_POWER_DOWN_EN)
  316. return false;
  317. else
  318. return true;
  319. }
  320. static void ch7017_dump_regs(struct intel_dvo_device *dvo)
  321. {
  322. uint8_t val;
  323. #define DUMP(reg) \
  324. do { \
  325. ch7017_read(dvo, reg, &val); \
  326. DRM_DEBUG_KMS(#reg ": %02x\n", val); \
  327. } while (0)
  328. DUMP(CH7017_HORIZONTAL_ACTIVE_PIXEL_INPUT);
  329. DUMP(CH7017_HORIZONTAL_ACTIVE_PIXEL_OUTPUT);
  330. DUMP(CH7017_VERTICAL_ACTIVE_LINE_OUTPUT);
  331. DUMP(CH7017_ACTIVE_INPUT_LINE_OUTPUT);
  332. DUMP(CH7017_LVDS_PLL_VCO_CONTROL);
  333. DUMP(CH7017_LVDS_PLL_FEEDBACK_DIV);
  334. DUMP(CH7017_LVDS_CONTROL_2);
  335. DUMP(CH7017_OUTPUTS_ENABLE);
  336. DUMP(CH7017_LVDS_POWER_DOWN);
  337. }
  338. static void ch7017_destroy(struct intel_dvo_device *dvo)
  339. {
  340. struct ch7017_priv *priv = dvo->dev_priv;
  341. if (priv) {
  342. kfree(priv);
  343. dvo->dev_priv = NULL;
  344. }
  345. }
  346. struct intel_dvo_dev_ops ch7017_ops = {
  347. .init = ch7017_init,
  348. .detect = ch7017_detect,
  349. .mode_valid = ch7017_mode_valid,
  350. .mode_set = ch7017_mode_set,
  351. .dpms = ch7017_dpms,
  352. .get_hw_state = ch7017_get_hw_state,
  353. .dump_regs = ch7017_dump_regs,
  354. .destroy = ch7017_destroy,
  355. };