dvo_ch7xxx.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**************************************************************************
  2. Copyright © 2006 Dave Airlie
  3. All Rights Reserved.
  4. Permission is hereby granted, free of charge, to any person obtaining a
  5. copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sub license, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice (including the
  12. next paragraph) shall be included in all copies or substantial portions
  13. of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  18. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  19. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  20. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. **************************************************************************/
  22. #include "dvo.h"
  23. #define CH7xxx_REG_VID 0x4a
  24. #define CH7xxx_REG_DID 0x4b
  25. #define CH7011_VID 0x83 /* 7010 as well */
  26. #define CH7010B_VID 0x05
  27. #define CH7009A_VID 0x84
  28. #define CH7009B_VID 0x85
  29. #define CH7301_VID 0x95
  30. #define CH7xxx_VID 0x84
  31. #define CH7xxx_DID 0x17
  32. #define CH7010_DID 0x16
  33. #define CH7xxx_NUM_REGS 0x4c
  34. #define CH7xxx_CM 0x1c
  35. #define CH7xxx_CM_XCM (1<<0)
  36. #define CH7xxx_CM_MCP (1<<2)
  37. #define CH7xxx_INPUT_CLOCK 0x1d
  38. #define CH7xxx_GPIO 0x1e
  39. #define CH7xxx_GPIO_HPIR (1<<3)
  40. #define CH7xxx_IDF 0x1f
  41. #define CH7xxx_IDF_HSP (1<<3)
  42. #define CH7xxx_IDF_VSP (1<<4)
  43. #define CH7xxx_CONNECTION_DETECT 0x20
  44. #define CH7xxx_CDET_DVI (1<<5)
  45. #define CH7301_DAC_CNTL 0x21
  46. #define CH7301_HOTPLUG 0x23
  47. #define CH7xxx_TCTL 0x31
  48. #define CH7xxx_TVCO 0x32
  49. #define CH7xxx_TPCP 0x33
  50. #define CH7xxx_TPD 0x34
  51. #define CH7xxx_TPVT 0x35
  52. #define CH7xxx_TLPF 0x36
  53. #define CH7xxx_TCT 0x37
  54. #define CH7301_TEST_PATTERN 0x48
  55. #define CH7xxx_PM 0x49
  56. #define CH7xxx_PM_FPD (1<<0)
  57. #define CH7301_PM_DACPD0 (1<<1)
  58. #define CH7301_PM_DACPD1 (1<<2)
  59. #define CH7301_PM_DACPD2 (1<<3)
  60. #define CH7xxx_PM_DVIL (1<<6)
  61. #define CH7xxx_PM_DVIP (1<<7)
  62. #define CH7301_SYNC_POLARITY 0x56
  63. #define CH7301_SYNC_RGB_YUV (1<<0)
  64. #define CH7301_SYNC_POL_DVI (1<<5)
  65. /** @file
  66. * driver for the Chrontel 7xxx DVI chip over DVO.
  67. */
  68. static struct ch7xxx_id_struct {
  69. uint8_t vid;
  70. char *name;
  71. } ch7xxx_ids[] = {
  72. { CH7011_VID, "CH7011" },
  73. { CH7010B_VID, "CH7010B" },
  74. { CH7009A_VID, "CH7009A" },
  75. { CH7009B_VID, "CH7009B" },
  76. { CH7301_VID, "CH7301" },
  77. };
  78. static struct ch7xxx_did_struct {
  79. uint8_t did;
  80. char *name;
  81. } ch7xxx_dids[] = {
  82. { CH7xxx_DID, "CH7XXX" },
  83. { CH7010_DID, "CH7010B" },
  84. };
  85. struct ch7xxx_priv {
  86. bool quiet;
  87. };
  88. static char *ch7xxx_get_id(uint8_t vid)
  89. {
  90. int i;
  91. for (i = 0; i < ARRAY_SIZE(ch7xxx_ids); i++) {
  92. if (ch7xxx_ids[i].vid == vid)
  93. return ch7xxx_ids[i].name;
  94. }
  95. return NULL;
  96. }
  97. static char *ch7xxx_get_did(uint8_t did)
  98. {
  99. int i;
  100. for (i = 0; i < ARRAY_SIZE(ch7xxx_dids); i++) {
  101. if (ch7xxx_dids[i].did == did)
  102. return ch7xxx_dids[i].name;
  103. }
  104. return NULL;
  105. }
  106. /** Reads an 8 bit register */
  107. static bool ch7xxx_readb(struct intel_dvo_device *dvo, int addr, uint8_t *ch)
  108. {
  109. struct ch7xxx_priv *ch7xxx = dvo->dev_priv;
  110. struct i2c_adapter *adapter = dvo->i2c_bus;
  111. u8 out_buf[2];
  112. u8 in_buf[2];
  113. struct i2c_msg msgs[] = {
  114. {
  115. .addr = dvo->slave_addr,
  116. .flags = 0,
  117. .len = 1,
  118. .buf = out_buf,
  119. },
  120. {
  121. .addr = dvo->slave_addr,
  122. .flags = I2C_M_RD,
  123. .len = 1,
  124. .buf = in_buf,
  125. }
  126. };
  127. out_buf[0] = addr;
  128. out_buf[1] = 0;
  129. if (i2c_transfer(adapter, msgs, 2) == 2) {
  130. *ch = in_buf[0];
  131. return true;
  132. }
  133. if (!ch7xxx->quiet) {
  134. DRM_DEBUG_KMS("Unable to read register 0x%02x from %s:%02x.\n",
  135. addr, adapter->name, dvo->slave_addr);
  136. }
  137. return false;
  138. }
  139. /** Writes an 8 bit register */
  140. static bool ch7xxx_writeb(struct intel_dvo_device *dvo, int addr, uint8_t ch)
  141. {
  142. struct ch7xxx_priv *ch7xxx = dvo->dev_priv;
  143. struct i2c_adapter *adapter = dvo->i2c_bus;
  144. uint8_t out_buf[2];
  145. struct i2c_msg msg = {
  146. .addr = dvo->slave_addr,
  147. .flags = 0,
  148. .len = 2,
  149. .buf = out_buf,
  150. };
  151. out_buf[0] = addr;
  152. out_buf[1] = ch;
  153. if (i2c_transfer(adapter, &msg, 1) == 1)
  154. return true;
  155. if (!ch7xxx->quiet) {
  156. DRM_DEBUG_KMS("Unable to write register 0x%02x to %s:%d.\n",
  157. addr, adapter->name, dvo->slave_addr);
  158. }
  159. return false;
  160. }
  161. static bool ch7xxx_init(struct intel_dvo_device *dvo,
  162. struct i2c_adapter *adapter)
  163. {
  164. /* this will detect the CH7xxx chip on the specified i2c bus */
  165. struct ch7xxx_priv *ch7xxx;
  166. uint8_t vendor, device;
  167. char *name, *devid;
  168. ch7xxx = kzalloc(sizeof(struct ch7xxx_priv), GFP_KERNEL);
  169. if (ch7xxx == NULL)
  170. return false;
  171. dvo->i2c_bus = adapter;
  172. dvo->dev_priv = ch7xxx;
  173. ch7xxx->quiet = true;
  174. if (!ch7xxx_readb(dvo, CH7xxx_REG_VID, &vendor))
  175. goto out;
  176. name = ch7xxx_get_id(vendor);
  177. if (!name) {
  178. DRM_DEBUG_KMS("ch7xxx not detected; got 0x%02x from %s "
  179. "slave %d.\n",
  180. vendor, adapter->name, dvo->slave_addr);
  181. goto out;
  182. }
  183. if (!ch7xxx_readb(dvo, CH7xxx_REG_DID, &device))
  184. goto out;
  185. devid = ch7xxx_get_did(device);
  186. if (!devid) {
  187. DRM_DEBUG_KMS("ch7xxx not detected; got 0x%02x from %s "
  188. "slave %d.\n",
  189. vendor, adapter->name, dvo->slave_addr);
  190. goto out;
  191. }
  192. ch7xxx->quiet = false;
  193. DRM_DEBUG_KMS("Detected %s chipset, vendor/device ID 0x%02x/0x%02x\n",
  194. name, vendor, device);
  195. return true;
  196. out:
  197. kfree(ch7xxx);
  198. return false;
  199. }
  200. static enum drm_connector_status ch7xxx_detect(struct intel_dvo_device *dvo)
  201. {
  202. uint8_t cdet, orig_pm, pm;
  203. ch7xxx_readb(dvo, CH7xxx_PM, &orig_pm);
  204. pm = orig_pm;
  205. pm &= ~CH7xxx_PM_FPD;
  206. pm |= CH7xxx_PM_DVIL | CH7xxx_PM_DVIP;
  207. ch7xxx_writeb(dvo, CH7xxx_PM, pm);
  208. ch7xxx_readb(dvo, CH7xxx_CONNECTION_DETECT, &cdet);
  209. ch7xxx_writeb(dvo, CH7xxx_PM, orig_pm);
  210. if (cdet & CH7xxx_CDET_DVI)
  211. return connector_status_connected;
  212. return connector_status_disconnected;
  213. }
  214. static enum drm_mode_status ch7xxx_mode_valid(struct intel_dvo_device *dvo,
  215. struct drm_display_mode *mode)
  216. {
  217. if (mode->clock > 165000)
  218. return MODE_CLOCK_HIGH;
  219. return MODE_OK;
  220. }
  221. static void ch7xxx_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 tvco, tpcp, tpd, tlpf, idf;
  226. if (mode->clock <= 65000) {
  227. tvco = 0x23;
  228. tpcp = 0x08;
  229. tpd = 0x16;
  230. tlpf = 0x60;
  231. } else {
  232. tvco = 0x2d;
  233. tpcp = 0x06;
  234. tpd = 0x26;
  235. tlpf = 0xa0;
  236. }
  237. ch7xxx_writeb(dvo, CH7xxx_TCTL, 0x00);
  238. ch7xxx_writeb(dvo, CH7xxx_TVCO, tvco);
  239. ch7xxx_writeb(dvo, CH7xxx_TPCP, tpcp);
  240. ch7xxx_writeb(dvo, CH7xxx_TPD, tpd);
  241. ch7xxx_writeb(dvo, CH7xxx_TPVT, 0x30);
  242. ch7xxx_writeb(dvo, CH7xxx_TLPF, tlpf);
  243. ch7xxx_writeb(dvo, CH7xxx_TCT, 0x00);
  244. ch7xxx_readb(dvo, CH7xxx_IDF, &idf);
  245. idf &= ~(CH7xxx_IDF_HSP | CH7xxx_IDF_VSP);
  246. if (mode->flags & DRM_MODE_FLAG_PHSYNC)
  247. idf |= CH7xxx_IDF_HSP;
  248. if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  249. idf |= CH7xxx_IDF_VSP;
  250. ch7xxx_writeb(dvo, CH7xxx_IDF, idf);
  251. }
  252. /* set the CH7xxx power state */
  253. static void ch7xxx_dpms(struct intel_dvo_device *dvo, bool enable)
  254. {
  255. if (enable)
  256. ch7xxx_writeb(dvo, CH7xxx_PM, CH7xxx_PM_DVIL | CH7xxx_PM_DVIP);
  257. else
  258. ch7xxx_writeb(dvo, CH7xxx_PM, CH7xxx_PM_FPD);
  259. }
  260. static bool ch7xxx_get_hw_state(struct intel_dvo_device *dvo)
  261. {
  262. u8 val;
  263. ch7xxx_readb(dvo, CH7xxx_PM, &val);
  264. if (val & (CH7xxx_PM_DVIL | CH7xxx_PM_DVIP))
  265. return true;
  266. else
  267. return false;
  268. }
  269. static void ch7xxx_dump_regs(struct intel_dvo_device *dvo)
  270. {
  271. int i;
  272. for (i = 0; i < CH7xxx_NUM_REGS; i++) {
  273. uint8_t val;
  274. if ((i % 8) == 0)
  275. DRM_DEBUG_KMS("\n %02X: ", i);
  276. ch7xxx_readb(dvo, i, &val);
  277. DRM_DEBUG_KMS("%02X ", val);
  278. }
  279. }
  280. static void ch7xxx_destroy(struct intel_dvo_device *dvo)
  281. {
  282. struct ch7xxx_priv *ch7xxx = dvo->dev_priv;
  283. if (ch7xxx) {
  284. kfree(ch7xxx);
  285. dvo->dev_priv = NULL;
  286. }
  287. }
  288. struct intel_dvo_dev_ops ch7xxx_ops = {
  289. .init = ch7xxx_init,
  290. .detect = ch7xxx_detect,
  291. .mode_valid = ch7xxx_mode_valid,
  292. .mode_set = ch7xxx_mode_set,
  293. .dpms = ch7xxx_dpms,
  294. .get_hw_state = ch7xxx_get_hw_state,
  295. .dump_regs = ch7xxx_dump_regs,
  296. .destroy = ch7xxx_destroy,
  297. };