ast_mode.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. * Parts based on xf86-video-ast
  4. * Copyright (c) 2005 ASPEED Technology Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  18. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * The above copyright notice and this permission notice (including the
  23. * next paragraph) shall be included in all copies or substantial portions
  24. * of the Software.
  25. *
  26. */
  27. /*
  28. * Authors: Dave Airlie <airlied@redhat.com>
  29. */
  30. #include <linux/export.h>
  31. #include <drm/drmP.h>
  32. #include <drm/drm_crtc.h>
  33. #include <drm/drm_crtc_helper.h>
  34. #include <drm/drm_plane_helper.h>
  35. #include "ast_drv.h"
  36. #include "ast_tables.h"
  37. static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev);
  38. static void ast_i2c_destroy(struct ast_i2c_chan *i2c);
  39. static int ast_cursor_set(struct drm_crtc *crtc,
  40. struct drm_file *file_priv,
  41. uint32_t handle,
  42. uint32_t width,
  43. uint32_t height);
  44. static int ast_cursor_move(struct drm_crtc *crtc,
  45. int x, int y);
  46. static inline void ast_load_palette_index(struct ast_private *ast,
  47. u8 index, u8 red, u8 green,
  48. u8 blue)
  49. {
  50. ast_io_write8(ast, AST_IO_DAC_INDEX_WRITE, index);
  51. ast_io_read8(ast, AST_IO_SEQ_PORT);
  52. ast_io_write8(ast, AST_IO_DAC_DATA, red);
  53. ast_io_read8(ast, AST_IO_SEQ_PORT);
  54. ast_io_write8(ast, AST_IO_DAC_DATA, green);
  55. ast_io_read8(ast, AST_IO_SEQ_PORT);
  56. ast_io_write8(ast, AST_IO_DAC_DATA, blue);
  57. ast_io_read8(ast, AST_IO_SEQ_PORT);
  58. }
  59. static void ast_crtc_load_lut(struct drm_crtc *crtc)
  60. {
  61. struct ast_private *ast = crtc->dev->dev_private;
  62. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  63. int i;
  64. if (!crtc->enabled)
  65. return;
  66. for (i = 0; i < 256; i++)
  67. ast_load_palette_index(ast, i, ast_crtc->lut_r[i],
  68. ast_crtc->lut_g[i], ast_crtc->lut_b[i]);
  69. }
  70. static bool ast_get_vbios_mode_info(struct drm_crtc *crtc, struct drm_display_mode *mode,
  71. struct drm_display_mode *adjusted_mode,
  72. struct ast_vbios_mode_info *vbios_mode)
  73. {
  74. struct ast_private *ast = crtc->dev->dev_private;
  75. u32 refresh_rate_index = 0, mode_id, color_index, refresh_rate;
  76. u32 hborder, vborder;
  77. bool check_sync;
  78. struct ast_vbios_enhtable *best = NULL;
  79. switch (crtc->primary->fb->bits_per_pixel) {
  80. case 8:
  81. vbios_mode->std_table = &vbios_stdtable[VGAModeIndex];
  82. color_index = VGAModeIndex - 1;
  83. break;
  84. case 16:
  85. vbios_mode->std_table = &vbios_stdtable[HiCModeIndex];
  86. color_index = HiCModeIndex;
  87. break;
  88. case 24:
  89. case 32:
  90. vbios_mode->std_table = &vbios_stdtable[TrueCModeIndex];
  91. color_index = TrueCModeIndex;
  92. break;
  93. default:
  94. return false;
  95. }
  96. switch (crtc->mode.crtc_hdisplay) {
  97. case 640:
  98. vbios_mode->enh_table = &res_640x480[refresh_rate_index];
  99. break;
  100. case 800:
  101. vbios_mode->enh_table = &res_800x600[refresh_rate_index];
  102. break;
  103. case 1024:
  104. vbios_mode->enh_table = &res_1024x768[refresh_rate_index];
  105. break;
  106. case 1280:
  107. if (crtc->mode.crtc_vdisplay == 800)
  108. vbios_mode->enh_table = &res_1280x800[refresh_rate_index];
  109. else
  110. vbios_mode->enh_table = &res_1280x1024[refresh_rate_index];
  111. break;
  112. case 1360:
  113. vbios_mode->enh_table = &res_1360x768[refresh_rate_index];
  114. break;
  115. case 1440:
  116. vbios_mode->enh_table = &res_1440x900[refresh_rate_index];
  117. break;
  118. case 1600:
  119. if (crtc->mode.crtc_vdisplay == 900)
  120. vbios_mode->enh_table = &res_1600x900[refresh_rate_index];
  121. else
  122. vbios_mode->enh_table = &res_1600x1200[refresh_rate_index];
  123. break;
  124. case 1680:
  125. vbios_mode->enh_table = &res_1680x1050[refresh_rate_index];
  126. break;
  127. case 1920:
  128. if (crtc->mode.crtc_vdisplay == 1080)
  129. vbios_mode->enh_table = &res_1920x1080[refresh_rate_index];
  130. else
  131. vbios_mode->enh_table = &res_1920x1200[refresh_rate_index];
  132. break;
  133. default:
  134. return false;
  135. }
  136. refresh_rate = drm_mode_vrefresh(mode);
  137. check_sync = vbios_mode->enh_table->flags & WideScreenMode;
  138. do {
  139. struct ast_vbios_enhtable *loop = vbios_mode->enh_table;
  140. while (loop->refresh_rate != 0xff) {
  141. if ((check_sync) &&
  142. (((mode->flags & DRM_MODE_FLAG_NVSYNC) &&
  143. (loop->flags & PVSync)) ||
  144. ((mode->flags & DRM_MODE_FLAG_PVSYNC) &&
  145. (loop->flags & NVSync)) ||
  146. ((mode->flags & DRM_MODE_FLAG_NHSYNC) &&
  147. (loop->flags & PHSync)) ||
  148. ((mode->flags & DRM_MODE_FLAG_PHSYNC) &&
  149. (loop->flags & NHSync)))) {
  150. loop++;
  151. continue;
  152. }
  153. if (loop->refresh_rate <= refresh_rate
  154. && (!best || loop->refresh_rate > best->refresh_rate))
  155. best = loop;
  156. loop++;
  157. }
  158. if (best || !check_sync)
  159. break;
  160. check_sync = 0;
  161. } while (1);
  162. if (best)
  163. vbios_mode->enh_table = best;
  164. hborder = (vbios_mode->enh_table->flags & HBorder) ? 8 : 0;
  165. vborder = (vbios_mode->enh_table->flags & VBorder) ? 8 : 0;
  166. adjusted_mode->crtc_htotal = vbios_mode->enh_table->ht;
  167. adjusted_mode->crtc_hblank_start = vbios_mode->enh_table->hde + hborder;
  168. adjusted_mode->crtc_hblank_end = vbios_mode->enh_table->ht - hborder;
  169. adjusted_mode->crtc_hsync_start = vbios_mode->enh_table->hde + hborder +
  170. vbios_mode->enh_table->hfp;
  171. adjusted_mode->crtc_hsync_end = (vbios_mode->enh_table->hde + hborder +
  172. vbios_mode->enh_table->hfp +
  173. vbios_mode->enh_table->hsync);
  174. adjusted_mode->crtc_vtotal = vbios_mode->enh_table->vt;
  175. adjusted_mode->crtc_vblank_start = vbios_mode->enh_table->vde + vborder;
  176. adjusted_mode->crtc_vblank_end = vbios_mode->enh_table->vt - vborder;
  177. adjusted_mode->crtc_vsync_start = vbios_mode->enh_table->vde + vborder +
  178. vbios_mode->enh_table->vfp;
  179. adjusted_mode->crtc_vsync_end = (vbios_mode->enh_table->vde + vborder +
  180. vbios_mode->enh_table->vfp +
  181. vbios_mode->enh_table->vsync);
  182. refresh_rate_index = vbios_mode->enh_table->refresh_rate_index;
  183. mode_id = vbios_mode->enh_table->mode_id;
  184. if (ast->chip == AST1180) {
  185. /* TODO 1180 */
  186. } else {
  187. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8c, (u8)((color_index & 0xf) << 4));
  188. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8d, refresh_rate_index & 0xff);
  189. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8e, mode_id & 0xff);
  190. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0x00);
  191. if (vbios_mode->enh_table->flags & NewModeInfo) {
  192. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0xa8);
  193. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x92, crtc->primary->fb->bits_per_pixel);
  194. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x93, adjusted_mode->clock / 1000);
  195. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x94, adjusted_mode->crtc_hdisplay);
  196. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x95, adjusted_mode->crtc_hdisplay >> 8);
  197. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x96, adjusted_mode->crtc_vdisplay);
  198. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x97, adjusted_mode->crtc_vdisplay >> 8);
  199. }
  200. }
  201. return true;
  202. }
  203. static void ast_set_std_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
  204. struct ast_vbios_mode_info *vbios_mode)
  205. {
  206. struct ast_private *ast = crtc->dev->dev_private;
  207. struct ast_vbios_stdtable *stdtable;
  208. u32 i;
  209. u8 jreg;
  210. stdtable = vbios_mode->std_table;
  211. jreg = stdtable->misc;
  212. ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
  213. /* Set SEQ */
  214. ast_set_index_reg(ast, AST_IO_SEQ_PORT, 0x00, 0x03);
  215. for (i = 0; i < 4; i++) {
  216. jreg = stdtable->seq[i];
  217. if (!i)
  218. jreg |= 0x20;
  219. ast_set_index_reg(ast, AST_IO_SEQ_PORT, (i + 1) , jreg);
  220. }
  221. /* Set CRTC */
  222. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
  223. for (i = 0; i < 25; i++)
  224. ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
  225. /* set AR */
  226. jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
  227. for (i = 0; i < 20; i++) {
  228. jreg = stdtable->ar[i];
  229. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, (u8)i);
  230. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, jreg);
  231. }
  232. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x14);
  233. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x00);
  234. jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
  235. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x20);
  236. /* Set GR */
  237. for (i = 0; i < 9; i++)
  238. ast_set_index_reg(ast, AST_IO_GR_PORT, i, stdtable->gr[i]);
  239. }
  240. static void ast_set_crtc_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
  241. struct ast_vbios_mode_info *vbios_mode)
  242. {
  243. struct ast_private *ast = crtc->dev->dev_private;
  244. u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0;
  245. u16 temp;
  246. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
  247. temp = (mode->crtc_htotal >> 3) - 5;
  248. if (temp & 0x100)
  249. jregAC |= 0x01; /* HT D[8] */
  250. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x00, 0x00, temp);
  251. temp = (mode->crtc_hdisplay >> 3) - 1;
  252. if (temp & 0x100)
  253. jregAC |= 0x04; /* HDE D[8] */
  254. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x01, 0x00, temp);
  255. temp = (mode->crtc_hblank_start >> 3) - 1;
  256. if (temp & 0x100)
  257. jregAC |= 0x10; /* HBS D[8] */
  258. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x02, 0x00, temp);
  259. temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f;
  260. if (temp & 0x20)
  261. jreg05 |= 0x80; /* HBE D[5] */
  262. if (temp & 0x40)
  263. jregAD |= 0x01; /* HBE D[5] */
  264. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x03, 0xE0, (temp & 0x1f));
  265. temp = (mode->crtc_hsync_start >> 3) - 1;
  266. if (temp & 0x100)
  267. jregAC |= 0x40; /* HRS D[5] */
  268. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x04, 0x00, temp);
  269. temp = ((mode->crtc_hsync_end >> 3) - 1) & 0x3f;
  270. if (temp & 0x20)
  271. jregAD |= 0x04; /* HRE D[5] */
  272. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05));
  273. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAC, 0x00, jregAC);
  274. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAD, 0x00, jregAD);
  275. /* vert timings */
  276. temp = (mode->crtc_vtotal) - 2;
  277. if (temp & 0x100)
  278. jreg07 |= 0x01;
  279. if (temp & 0x200)
  280. jreg07 |= 0x20;
  281. if (temp & 0x400)
  282. jregAE |= 0x01;
  283. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x06, 0x00, temp);
  284. temp = (mode->crtc_vsync_start) - 1;
  285. if (temp & 0x100)
  286. jreg07 |= 0x04;
  287. if (temp & 0x200)
  288. jreg07 |= 0x80;
  289. if (temp & 0x400)
  290. jregAE |= 0x08;
  291. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x10, 0x00, temp);
  292. temp = (mode->crtc_vsync_end - 1) & 0x3f;
  293. if (temp & 0x10)
  294. jregAE |= 0x20;
  295. if (temp & 0x20)
  296. jregAE |= 0x40;
  297. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x70, temp & 0xf);
  298. temp = mode->crtc_vdisplay - 1;
  299. if (temp & 0x100)
  300. jreg07 |= 0x02;
  301. if (temp & 0x200)
  302. jreg07 |= 0x40;
  303. if (temp & 0x400)
  304. jregAE |= 0x02;
  305. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x12, 0x00, temp);
  306. temp = mode->crtc_vblank_start - 1;
  307. if (temp & 0x100)
  308. jreg07 |= 0x08;
  309. if (temp & 0x200)
  310. jreg09 |= 0x20;
  311. if (temp & 0x400)
  312. jregAE |= 0x04;
  313. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x15, 0x00, temp);
  314. temp = mode->crtc_vblank_end - 1;
  315. if (temp & 0x100)
  316. jregAE |= 0x10;
  317. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x16, 0x00, temp);
  318. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x07, 0x00, jreg07);
  319. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x09, 0xdf, jreg09);
  320. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAE, 0x00, (jregAE | 0x80));
  321. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x80);
  322. }
  323. static void ast_set_offset_reg(struct drm_crtc *crtc)
  324. {
  325. struct ast_private *ast = crtc->dev->dev_private;
  326. u16 offset;
  327. offset = crtc->primary->fb->pitches[0] >> 3;
  328. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x13, (offset & 0xff));
  329. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xb0, (offset >> 8) & 0x3f);
  330. }
  331. static void ast_set_dclk_reg(struct drm_device *dev, struct drm_display_mode *mode,
  332. struct ast_vbios_mode_info *vbios_mode)
  333. {
  334. struct ast_private *ast = dev->dev_private;
  335. struct ast_vbios_dclk_info *clk_info;
  336. clk_info = &dclk_table[vbios_mode->enh_table->dclk_index];
  337. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc0, 0x00, clk_info->param1);
  338. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc1, 0x00, clk_info->param2);
  339. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xbb, 0x0f,
  340. (clk_info->param3 & 0x80) | ((clk_info->param3 & 0x3) << 4));
  341. }
  342. static void ast_set_ext_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
  343. struct ast_vbios_mode_info *vbios_mode)
  344. {
  345. struct ast_private *ast = crtc->dev->dev_private;
  346. u8 jregA0 = 0, jregA3 = 0, jregA8 = 0;
  347. switch (crtc->primary->fb->bits_per_pixel) {
  348. case 8:
  349. jregA0 = 0x70;
  350. jregA3 = 0x01;
  351. jregA8 = 0x00;
  352. break;
  353. case 15:
  354. case 16:
  355. jregA0 = 0x70;
  356. jregA3 = 0x04;
  357. jregA8 = 0x02;
  358. break;
  359. case 32:
  360. jregA0 = 0x70;
  361. jregA3 = 0x08;
  362. jregA8 = 0x02;
  363. break;
  364. }
  365. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa0, 0x8f, jregA0);
  366. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xf0, jregA3);
  367. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa8, 0xfd, jregA8);
  368. /* Set Threshold */
  369. if (ast->chip == AST2300 || ast->chip == AST2400) {
  370. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x78);
  371. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x60);
  372. } else if (ast->chip == AST2100 ||
  373. ast->chip == AST1100 ||
  374. ast->chip == AST2200 ||
  375. ast->chip == AST2150) {
  376. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x3f);
  377. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x2f);
  378. } else {
  379. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x2f);
  380. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x1f);
  381. }
  382. }
  383. static void ast_set_sync_reg(struct drm_device *dev, struct drm_display_mode *mode,
  384. struct ast_vbios_mode_info *vbios_mode)
  385. {
  386. struct ast_private *ast = dev->dev_private;
  387. u8 jreg;
  388. jreg = ast_io_read8(ast, AST_IO_MISC_PORT_READ);
  389. jreg &= ~0xC0;
  390. if (vbios_mode->enh_table->flags & NVSync) jreg |= 0x80;
  391. if (vbios_mode->enh_table->flags & NHSync) jreg |= 0x40;
  392. ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
  393. }
  394. static bool ast_set_dac_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
  395. struct ast_vbios_mode_info *vbios_mode)
  396. {
  397. switch (crtc->primary->fb->bits_per_pixel) {
  398. case 8:
  399. break;
  400. default:
  401. return false;
  402. }
  403. return true;
  404. }
  405. static void ast_set_start_address_crt1(struct drm_crtc *crtc, unsigned offset)
  406. {
  407. struct ast_private *ast = crtc->dev->dev_private;
  408. u32 addr;
  409. addr = offset >> 2;
  410. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0d, (u8)(addr & 0xff));
  411. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0c, (u8)((addr >> 8) & 0xff));
  412. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xaf, (u8)((addr >> 16) & 0xff));
  413. }
  414. static void ast_crtc_dpms(struct drm_crtc *crtc, int mode)
  415. {
  416. struct ast_private *ast = crtc->dev->dev_private;
  417. if (ast->chip == AST1180)
  418. return;
  419. switch (mode) {
  420. case DRM_MODE_DPMS_ON:
  421. case DRM_MODE_DPMS_STANDBY:
  422. case DRM_MODE_DPMS_SUSPEND:
  423. ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0);
  424. if (ast->tx_chip_type == AST_TX_DP501)
  425. ast_set_dp501_video_output(crtc->dev, 1);
  426. ast_crtc_load_lut(crtc);
  427. break;
  428. case DRM_MODE_DPMS_OFF:
  429. if (ast->tx_chip_type == AST_TX_DP501)
  430. ast_set_dp501_video_output(crtc->dev, 0);
  431. ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x20);
  432. break;
  433. }
  434. }
  435. static bool ast_crtc_mode_fixup(struct drm_crtc *crtc,
  436. const struct drm_display_mode *mode,
  437. struct drm_display_mode *adjusted_mode)
  438. {
  439. return true;
  440. }
  441. /* ast is different - we will force move buffers out of VRAM */
  442. static int ast_crtc_do_set_base(struct drm_crtc *crtc,
  443. struct drm_framebuffer *fb,
  444. int x, int y, int atomic)
  445. {
  446. struct ast_private *ast = crtc->dev->dev_private;
  447. struct drm_gem_object *obj;
  448. struct ast_framebuffer *ast_fb;
  449. struct ast_bo *bo;
  450. int ret;
  451. u64 gpu_addr;
  452. /* push the previous fb to system ram */
  453. if (!atomic && fb) {
  454. ast_fb = to_ast_framebuffer(fb);
  455. obj = ast_fb->obj;
  456. bo = gem_to_ast_bo(obj);
  457. ret = ast_bo_reserve(bo, false);
  458. if (ret)
  459. return ret;
  460. ast_bo_push_sysram(bo);
  461. ast_bo_unreserve(bo);
  462. }
  463. ast_fb = to_ast_framebuffer(crtc->primary->fb);
  464. obj = ast_fb->obj;
  465. bo = gem_to_ast_bo(obj);
  466. ret = ast_bo_reserve(bo, false);
  467. if (ret)
  468. return ret;
  469. ret = ast_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  470. if (ret) {
  471. ast_bo_unreserve(bo);
  472. return ret;
  473. }
  474. if (&ast->fbdev->afb == ast_fb) {
  475. /* if pushing console in kmap it */
  476. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  477. if (ret)
  478. DRM_ERROR("failed to kmap fbcon\n");
  479. else
  480. ast_fbdev_set_base(ast, gpu_addr);
  481. }
  482. ast_bo_unreserve(bo);
  483. ast_set_offset_reg(crtc);
  484. ast_set_start_address_crt1(crtc, (u32)gpu_addr);
  485. return 0;
  486. }
  487. static int ast_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  488. struct drm_framebuffer *old_fb)
  489. {
  490. return ast_crtc_do_set_base(crtc, old_fb, x, y, 0);
  491. }
  492. static int ast_crtc_mode_set(struct drm_crtc *crtc,
  493. struct drm_display_mode *mode,
  494. struct drm_display_mode *adjusted_mode,
  495. int x, int y,
  496. struct drm_framebuffer *old_fb)
  497. {
  498. struct drm_device *dev = crtc->dev;
  499. struct ast_private *ast = crtc->dev->dev_private;
  500. struct ast_vbios_mode_info vbios_mode;
  501. bool ret;
  502. if (ast->chip == AST1180) {
  503. DRM_ERROR("AST 1180 modesetting not supported\n");
  504. return -EINVAL;
  505. }
  506. ret = ast_get_vbios_mode_info(crtc, mode, adjusted_mode, &vbios_mode);
  507. if (ret == false)
  508. return -EINVAL;
  509. ast_open_key(ast);
  510. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa1, 0xff, 0x04);
  511. ast_set_std_reg(crtc, adjusted_mode, &vbios_mode);
  512. ast_set_crtc_reg(crtc, adjusted_mode, &vbios_mode);
  513. ast_set_offset_reg(crtc);
  514. ast_set_dclk_reg(dev, adjusted_mode, &vbios_mode);
  515. ast_set_ext_reg(crtc, adjusted_mode, &vbios_mode);
  516. ast_set_sync_reg(dev, adjusted_mode, &vbios_mode);
  517. ast_set_dac_reg(crtc, adjusted_mode, &vbios_mode);
  518. ast_crtc_mode_set_base(crtc, x, y, old_fb);
  519. return 0;
  520. }
  521. static void ast_crtc_disable(struct drm_crtc *crtc)
  522. {
  523. }
  524. static void ast_crtc_prepare(struct drm_crtc *crtc)
  525. {
  526. }
  527. static void ast_crtc_commit(struct drm_crtc *crtc)
  528. {
  529. struct ast_private *ast = crtc->dev->dev_private;
  530. ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0);
  531. }
  532. static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
  533. .dpms = ast_crtc_dpms,
  534. .mode_fixup = ast_crtc_mode_fixup,
  535. .mode_set = ast_crtc_mode_set,
  536. .mode_set_base = ast_crtc_mode_set_base,
  537. .disable = ast_crtc_disable,
  538. .load_lut = ast_crtc_load_lut,
  539. .prepare = ast_crtc_prepare,
  540. .commit = ast_crtc_commit,
  541. };
  542. static void ast_crtc_reset(struct drm_crtc *crtc)
  543. {
  544. }
  545. static void ast_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  546. u16 *blue, uint32_t start, uint32_t size)
  547. {
  548. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  549. int end = (start + size > 256) ? 256 : start + size, i;
  550. /* userspace palettes are always correct as is */
  551. for (i = start; i < end; i++) {
  552. ast_crtc->lut_r[i] = red[i] >> 8;
  553. ast_crtc->lut_g[i] = green[i] >> 8;
  554. ast_crtc->lut_b[i] = blue[i] >> 8;
  555. }
  556. ast_crtc_load_lut(crtc);
  557. }
  558. static void ast_crtc_destroy(struct drm_crtc *crtc)
  559. {
  560. drm_crtc_cleanup(crtc);
  561. kfree(crtc);
  562. }
  563. static const struct drm_crtc_funcs ast_crtc_funcs = {
  564. .cursor_set = ast_cursor_set,
  565. .cursor_move = ast_cursor_move,
  566. .reset = ast_crtc_reset,
  567. .set_config = drm_crtc_helper_set_config,
  568. .gamma_set = ast_crtc_gamma_set,
  569. .destroy = ast_crtc_destroy,
  570. };
  571. static int ast_crtc_init(struct drm_device *dev)
  572. {
  573. struct ast_crtc *crtc;
  574. int i;
  575. crtc = kzalloc(sizeof(struct ast_crtc), GFP_KERNEL);
  576. if (!crtc)
  577. return -ENOMEM;
  578. drm_crtc_init(dev, &crtc->base, &ast_crtc_funcs);
  579. drm_mode_crtc_set_gamma_size(&crtc->base, 256);
  580. drm_crtc_helper_add(&crtc->base, &ast_crtc_helper_funcs);
  581. for (i = 0; i < 256; i++) {
  582. crtc->lut_r[i] = i;
  583. crtc->lut_g[i] = i;
  584. crtc->lut_b[i] = i;
  585. }
  586. return 0;
  587. }
  588. static void ast_encoder_destroy(struct drm_encoder *encoder)
  589. {
  590. drm_encoder_cleanup(encoder);
  591. kfree(encoder);
  592. }
  593. static struct drm_encoder *ast_best_single_encoder(struct drm_connector *connector)
  594. {
  595. int enc_id = connector->encoder_ids[0];
  596. /* pick the encoder ids */
  597. if (enc_id)
  598. return drm_encoder_find(connector->dev, enc_id);
  599. return NULL;
  600. }
  601. static const struct drm_encoder_funcs ast_enc_funcs = {
  602. .destroy = ast_encoder_destroy,
  603. };
  604. static void ast_encoder_dpms(struct drm_encoder *encoder, int mode)
  605. {
  606. }
  607. static bool ast_mode_fixup(struct drm_encoder *encoder,
  608. const struct drm_display_mode *mode,
  609. struct drm_display_mode *adjusted_mode)
  610. {
  611. return true;
  612. }
  613. static void ast_encoder_mode_set(struct drm_encoder *encoder,
  614. struct drm_display_mode *mode,
  615. struct drm_display_mode *adjusted_mode)
  616. {
  617. }
  618. static void ast_encoder_prepare(struct drm_encoder *encoder)
  619. {
  620. }
  621. static void ast_encoder_commit(struct drm_encoder *encoder)
  622. {
  623. }
  624. static const struct drm_encoder_helper_funcs ast_enc_helper_funcs = {
  625. .dpms = ast_encoder_dpms,
  626. .mode_fixup = ast_mode_fixup,
  627. .prepare = ast_encoder_prepare,
  628. .commit = ast_encoder_commit,
  629. .mode_set = ast_encoder_mode_set,
  630. };
  631. static int ast_encoder_init(struct drm_device *dev)
  632. {
  633. struct ast_encoder *ast_encoder;
  634. ast_encoder = kzalloc(sizeof(struct ast_encoder), GFP_KERNEL);
  635. if (!ast_encoder)
  636. return -ENOMEM;
  637. drm_encoder_init(dev, &ast_encoder->base, &ast_enc_funcs,
  638. DRM_MODE_ENCODER_DAC);
  639. drm_encoder_helper_add(&ast_encoder->base, &ast_enc_helper_funcs);
  640. ast_encoder->base.possible_crtcs = 1;
  641. return 0;
  642. }
  643. static int ast_get_modes(struct drm_connector *connector)
  644. {
  645. struct ast_connector *ast_connector = to_ast_connector(connector);
  646. struct ast_private *ast = connector->dev->dev_private;
  647. struct edid *edid;
  648. int ret;
  649. bool flags = false;
  650. if (ast->tx_chip_type == AST_TX_DP501) {
  651. ast->dp501_maxclk = 0xff;
  652. edid = kmalloc(128, GFP_KERNEL);
  653. if (!edid)
  654. return -ENOMEM;
  655. flags = ast_dp501_read_edid(connector->dev, (u8 *)edid);
  656. if (flags)
  657. ast->dp501_maxclk = ast_get_dp501_max_clk(connector->dev);
  658. else
  659. kfree(edid);
  660. }
  661. if (!flags)
  662. edid = drm_get_edid(connector, &ast_connector->i2c->adapter);
  663. if (edid) {
  664. drm_mode_connector_update_edid_property(&ast_connector->base, edid);
  665. ret = drm_add_edid_modes(connector, edid);
  666. kfree(edid);
  667. return ret;
  668. } else
  669. drm_mode_connector_update_edid_property(&ast_connector->base, NULL);
  670. return 0;
  671. }
  672. static int ast_mode_valid(struct drm_connector *connector,
  673. struct drm_display_mode *mode)
  674. {
  675. struct ast_private *ast = connector->dev->dev_private;
  676. int flags = MODE_NOMODE;
  677. uint32_t jtemp;
  678. if (ast->support_wide_screen) {
  679. if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
  680. return MODE_OK;
  681. if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
  682. return MODE_OK;
  683. if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
  684. return MODE_OK;
  685. if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
  686. return MODE_OK;
  687. if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
  688. return MODE_OK;
  689. if ((ast->chip == AST2100) || (ast->chip == AST2200) || (ast->chip == AST2300) || (ast->chip == AST2400) || (ast->chip == AST1180)) {
  690. if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
  691. return MODE_OK;
  692. if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
  693. jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  694. if (jtemp & 0x01)
  695. return MODE_NOMODE;
  696. else
  697. return MODE_OK;
  698. }
  699. }
  700. }
  701. switch (mode->hdisplay) {
  702. case 640:
  703. if (mode->vdisplay == 480) flags = MODE_OK;
  704. break;
  705. case 800:
  706. if (mode->vdisplay == 600) flags = MODE_OK;
  707. break;
  708. case 1024:
  709. if (mode->vdisplay == 768) flags = MODE_OK;
  710. break;
  711. case 1280:
  712. if (mode->vdisplay == 1024) flags = MODE_OK;
  713. break;
  714. case 1600:
  715. if (mode->vdisplay == 1200) flags = MODE_OK;
  716. break;
  717. default:
  718. return flags;
  719. }
  720. return flags;
  721. }
  722. static void ast_connector_destroy(struct drm_connector *connector)
  723. {
  724. struct ast_connector *ast_connector = to_ast_connector(connector);
  725. ast_i2c_destroy(ast_connector->i2c);
  726. drm_connector_unregister(connector);
  727. drm_connector_cleanup(connector);
  728. kfree(connector);
  729. }
  730. static enum drm_connector_status
  731. ast_connector_detect(struct drm_connector *connector, bool force)
  732. {
  733. return connector_status_connected;
  734. }
  735. static const struct drm_connector_helper_funcs ast_connector_helper_funcs = {
  736. .mode_valid = ast_mode_valid,
  737. .get_modes = ast_get_modes,
  738. .best_encoder = ast_best_single_encoder,
  739. };
  740. static const struct drm_connector_funcs ast_connector_funcs = {
  741. .dpms = drm_helper_connector_dpms,
  742. .detect = ast_connector_detect,
  743. .fill_modes = drm_helper_probe_single_connector_modes,
  744. .destroy = ast_connector_destroy,
  745. };
  746. static int ast_connector_init(struct drm_device *dev)
  747. {
  748. struct ast_connector *ast_connector;
  749. struct drm_connector *connector;
  750. struct drm_encoder *encoder;
  751. ast_connector = kzalloc(sizeof(struct ast_connector), GFP_KERNEL);
  752. if (!ast_connector)
  753. return -ENOMEM;
  754. connector = &ast_connector->base;
  755. drm_connector_init(dev, connector, &ast_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  756. drm_connector_helper_add(connector, &ast_connector_helper_funcs);
  757. connector->interlace_allowed = 0;
  758. connector->doublescan_allowed = 0;
  759. drm_connector_register(connector);
  760. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  761. encoder = list_first_entry(&dev->mode_config.encoder_list, struct drm_encoder, head);
  762. drm_mode_connector_attach_encoder(connector, encoder);
  763. ast_connector->i2c = ast_i2c_create(dev);
  764. if (!ast_connector->i2c)
  765. DRM_ERROR("failed to add ddc bus for connector\n");
  766. return 0;
  767. }
  768. /* allocate cursor cache and pin at start of VRAM */
  769. static int ast_cursor_init(struct drm_device *dev)
  770. {
  771. struct ast_private *ast = dev->dev_private;
  772. int size;
  773. int ret;
  774. struct drm_gem_object *obj;
  775. struct ast_bo *bo;
  776. uint64_t gpu_addr;
  777. size = (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE) * AST_DEFAULT_HWC_NUM;
  778. ret = ast_gem_create(dev, size, true, &obj);
  779. if (ret)
  780. return ret;
  781. bo = gem_to_ast_bo(obj);
  782. ret = ast_bo_reserve(bo, false);
  783. if (unlikely(ret != 0))
  784. goto fail;
  785. ret = ast_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  786. ast_bo_unreserve(bo);
  787. if (ret)
  788. goto fail;
  789. /* kmap the object */
  790. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &ast->cache_kmap);
  791. if (ret)
  792. goto fail;
  793. ast->cursor_cache = obj;
  794. ast->cursor_cache_gpu_addr = gpu_addr;
  795. DRM_DEBUG_KMS("pinned cursor cache at %llx\n", ast->cursor_cache_gpu_addr);
  796. return 0;
  797. fail:
  798. return ret;
  799. }
  800. static void ast_cursor_fini(struct drm_device *dev)
  801. {
  802. struct ast_private *ast = dev->dev_private;
  803. ttm_bo_kunmap(&ast->cache_kmap);
  804. drm_gem_object_unreference_unlocked(ast->cursor_cache);
  805. }
  806. int ast_mode_init(struct drm_device *dev)
  807. {
  808. ast_cursor_init(dev);
  809. ast_crtc_init(dev);
  810. ast_encoder_init(dev);
  811. ast_connector_init(dev);
  812. return 0;
  813. }
  814. void ast_mode_fini(struct drm_device *dev)
  815. {
  816. ast_cursor_fini(dev);
  817. }
  818. static int get_clock(void *i2c_priv)
  819. {
  820. struct ast_i2c_chan *i2c = i2c_priv;
  821. struct ast_private *ast = i2c->dev->dev_private;
  822. uint32_t val, val2, count, pass;
  823. count = 0;
  824. pass = 0;
  825. val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
  826. do {
  827. val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
  828. if (val == val2) {
  829. pass++;
  830. } else {
  831. pass = 0;
  832. val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4) & 0x01;
  833. }
  834. } while ((pass < 5) && (count++ < 0x10000));
  835. return val & 1 ? 1 : 0;
  836. }
  837. static int get_data(void *i2c_priv)
  838. {
  839. struct ast_i2c_chan *i2c = i2c_priv;
  840. struct ast_private *ast = i2c->dev->dev_private;
  841. uint32_t val, val2, count, pass;
  842. count = 0;
  843. pass = 0;
  844. val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
  845. do {
  846. val2 = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
  847. if (val == val2) {
  848. pass++;
  849. } else {
  850. pass = 0;
  851. val = (ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5) & 0x01;
  852. }
  853. } while ((pass < 5) && (count++ < 0x10000));
  854. return val & 1 ? 1 : 0;
  855. }
  856. static void set_clock(void *i2c_priv, int clock)
  857. {
  858. struct ast_i2c_chan *i2c = i2c_priv;
  859. struct ast_private *ast = i2c->dev->dev_private;
  860. int i;
  861. u8 ujcrb7, jtemp;
  862. for (i = 0; i < 0x10000; i++) {
  863. ujcrb7 = ((clock & 0x01) ? 0 : 1);
  864. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xf4, ujcrb7);
  865. jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x01);
  866. if (ujcrb7 == jtemp)
  867. break;
  868. }
  869. }
  870. static void set_data(void *i2c_priv, int data)
  871. {
  872. struct ast_i2c_chan *i2c = i2c_priv;
  873. struct ast_private *ast = i2c->dev->dev_private;
  874. int i;
  875. u8 ujcrb7, jtemp;
  876. for (i = 0; i < 0x10000; i++) {
  877. ujcrb7 = ((data & 0x01) ? 0 : 1) << 2;
  878. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xf1, ujcrb7);
  879. jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x04);
  880. if (ujcrb7 == jtemp)
  881. break;
  882. }
  883. }
  884. static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev)
  885. {
  886. struct ast_i2c_chan *i2c;
  887. int ret;
  888. i2c = kzalloc(sizeof(struct ast_i2c_chan), GFP_KERNEL);
  889. if (!i2c)
  890. return NULL;
  891. i2c->adapter.owner = THIS_MODULE;
  892. i2c->adapter.class = I2C_CLASS_DDC;
  893. i2c->adapter.dev.parent = &dev->pdev->dev;
  894. i2c->dev = dev;
  895. i2c_set_adapdata(&i2c->adapter, i2c);
  896. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  897. "AST i2c bit bus");
  898. i2c->adapter.algo_data = &i2c->bit;
  899. i2c->bit.udelay = 20;
  900. i2c->bit.timeout = 2;
  901. i2c->bit.data = i2c;
  902. i2c->bit.setsda = set_data;
  903. i2c->bit.setscl = set_clock;
  904. i2c->bit.getsda = get_data;
  905. i2c->bit.getscl = get_clock;
  906. ret = i2c_bit_add_bus(&i2c->adapter);
  907. if (ret) {
  908. DRM_ERROR("Failed to register bit i2c\n");
  909. goto out_free;
  910. }
  911. return i2c;
  912. out_free:
  913. kfree(i2c);
  914. return NULL;
  915. }
  916. static void ast_i2c_destroy(struct ast_i2c_chan *i2c)
  917. {
  918. if (!i2c)
  919. return;
  920. i2c_del_adapter(&i2c->adapter);
  921. kfree(i2c);
  922. }
  923. static void ast_show_cursor(struct drm_crtc *crtc)
  924. {
  925. struct ast_private *ast = crtc->dev->dev_private;
  926. u8 jreg;
  927. jreg = 0x2;
  928. /* enable ARGB cursor */
  929. jreg |= 1;
  930. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, jreg);
  931. }
  932. static void ast_hide_cursor(struct drm_crtc *crtc)
  933. {
  934. struct ast_private *ast = crtc->dev->dev_private;
  935. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, 0x00);
  936. }
  937. static u32 copy_cursor_image(u8 *src, u8 *dst, int width, int height)
  938. {
  939. union {
  940. u32 ul;
  941. u8 b[4];
  942. } srcdata32[2], data32;
  943. union {
  944. u16 us;
  945. u8 b[2];
  946. } data16;
  947. u32 csum = 0;
  948. s32 alpha_dst_delta, last_alpha_dst_delta;
  949. u8 *srcxor, *dstxor;
  950. int i, j;
  951. u32 per_pixel_copy, two_pixel_copy;
  952. alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
  953. last_alpha_dst_delta = alpha_dst_delta - (width << 1);
  954. srcxor = src;
  955. dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
  956. per_pixel_copy = width & 1;
  957. two_pixel_copy = width >> 1;
  958. for (j = 0; j < height; j++) {
  959. for (i = 0; i < two_pixel_copy; i++) {
  960. srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
  961. srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
  962. data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
  963. data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
  964. data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
  965. data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);
  966. writel(data32.ul, dstxor);
  967. csum += data32.ul;
  968. dstxor += 4;
  969. srcxor += 8;
  970. }
  971. for (i = 0; i < per_pixel_copy; i++) {
  972. srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
  973. data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
  974. data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
  975. writew(data16.us, dstxor);
  976. csum += (u32)data16.us;
  977. dstxor += 2;
  978. srcxor += 4;
  979. }
  980. dstxor += last_alpha_dst_delta;
  981. }
  982. return csum;
  983. }
  984. static int ast_cursor_set(struct drm_crtc *crtc,
  985. struct drm_file *file_priv,
  986. uint32_t handle,
  987. uint32_t width,
  988. uint32_t height)
  989. {
  990. struct ast_private *ast = crtc->dev->dev_private;
  991. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  992. struct drm_gem_object *obj;
  993. struct ast_bo *bo;
  994. uint64_t gpu_addr;
  995. u32 csum;
  996. int ret;
  997. struct ttm_bo_kmap_obj uobj_map;
  998. u8 *src, *dst;
  999. bool src_isiomem, dst_isiomem;
  1000. if (!handle) {
  1001. ast_hide_cursor(crtc);
  1002. return 0;
  1003. }
  1004. if (width > AST_MAX_HWC_WIDTH || height > AST_MAX_HWC_HEIGHT)
  1005. return -EINVAL;
  1006. obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
  1007. if (!obj) {
  1008. DRM_ERROR("Cannot find cursor object %x for crtc\n", handle);
  1009. return -ENOENT;
  1010. }
  1011. bo = gem_to_ast_bo(obj);
  1012. ret = ast_bo_reserve(bo, false);
  1013. if (ret)
  1014. goto fail;
  1015. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &uobj_map);
  1016. src = ttm_kmap_obj_virtual(&uobj_map, &src_isiomem);
  1017. dst = ttm_kmap_obj_virtual(&ast->cache_kmap, &dst_isiomem);
  1018. if (src_isiomem == true)
  1019. DRM_ERROR("src cursor bo should be in main memory\n");
  1020. if (dst_isiomem == false)
  1021. DRM_ERROR("dst bo should be in VRAM\n");
  1022. dst += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
  1023. /* do data transfer to cursor cache */
  1024. csum = copy_cursor_image(src, dst, width, height);
  1025. /* write checksum + signature */
  1026. ttm_bo_kunmap(&uobj_map);
  1027. ast_bo_unreserve(bo);
  1028. {
  1029. u8 *dst = (u8 *)ast->cache_kmap.virtual + (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor + AST_HWC_SIZE;
  1030. writel(csum, dst);
  1031. writel(width, dst + AST_HWC_SIGNATURE_SizeX);
  1032. writel(height, dst + AST_HWC_SIGNATURE_SizeY);
  1033. writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
  1034. writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
  1035. /* set pattern offset */
  1036. gpu_addr = ast->cursor_cache_gpu_addr;
  1037. gpu_addr += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
  1038. gpu_addr >>= 3;
  1039. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, gpu_addr & 0xff);
  1040. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, (gpu_addr >> 8) & 0xff);
  1041. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, (gpu_addr >> 16) & 0xff);
  1042. }
  1043. ast_crtc->cursor_width = width;
  1044. ast_crtc->cursor_height = height;
  1045. ast_crtc->offset_x = AST_MAX_HWC_WIDTH - width;
  1046. ast_crtc->offset_y = AST_MAX_HWC_WIDTH - height;
  1047. ast->next_cursor = (ast->next_cursor + 1) % AST_DEFAULT_HWC_NUM;
  1048. ast_show_cursor(crtc);
  1049. drm_gem_object_unreference_unlocked(obj);
  1050. return 0;
  1051. fail:
  1052. drm_gem_object_unreference_unlocked(obj);
  1053. return ret;
  1054. }
  1055. static int ast_cursor_move(struct drm_crtc *crtc,
  1056. int x, int y)
  1057. {
  1058. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  1059. struct ast_private *ast = crtc->dev->dev_private;
  1060. int x_offset, y_offset;
  1061. u8 *sig;
  1062. sig = (u8 *)ast->cache_kmap.virtual + (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor + AST_HWC_SIZE;
  1063. writel(x, sig + AST_HWC_SIGNATURE_X);
  1064. writel(y, sig + AST_HWC_SIGNATURE_Y);
  1065. x_offset = ast_crtc->offset_x;
  1066. y_offset = ast_crtc->offset_y;
  1067. if (x < 0) {
  1068. x_offset = (-x) + ast_crtc->offset_x;
  1069. x = 0;
  1070. }
  1071. if (y < 0) {
  1072. y_offset = (-y) + ast_crtc->offset_y;
  1073. y = 0;
  1074. }
  1075. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc2, x_offset);
  1076. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc3, y_offset);
  1077. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc4, (x & 0xff));
  1078. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc5, ((x >> 8) & 0x0f));
  1079. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc6, (y & 0xff));
  1080. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc7, ((y >> 8) & 0x07));
  1081. /* dummy write to fire HWC */
  1082. ast_show_cursor(crtc);
  1083. return 0;
  1084. }