drm_fb_helper.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /*
  2. * Copyright (c) 2006-2009 Red Hat Inc.
  3. * Copyright (c) 2006-2008 Intel Corporation
  4. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  5. *
  6. * DRM framebuffer helper functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Dave Airlie <airlied@linux.ie>
  28. * Jesse Barnes <jesse.barnes@intel.com>
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/sysrq.h>
  32. #include <linux/fb.h>
  33. #include "drmP.h"
  34. #include "drm_crtc.h"
  35. #include "drm_fb_helper.h"
  36. #include "drm_crtc_helper.h"
  37. MODULE_AUTHOR("David Airlie, Jesse Barnes");
  38. MODULE_DESCRIPTION("DRM KMS helper");
  39. MODULE_LICENSE("GPL and additional rights");
  40. static LIST_HEAD(kernel_fb_helper_list);
  41. /* simple single crtc case helper function */
  42. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  43. {
  44. struct drm_device *dev = fb_helper->dev;
  45. struct drm_connector *connector;
  46. int i;
  47. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  48. struct drm_fb_helper_connector *fb_helper_connector;
  49. fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  50. if (!fb_helper_connector)
  51. goto fail;
  52. fb_helper_connector->connector = connector;
  53. fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  54. }
  55. return 0;
  56. fail:
  57. for (i = 0; i < fb_helper->connector_count; i++) {
  58. kfree(fb_helper->connector_info[i]);
  59. fb_helper->connector_info[i] = NULL;
  60. }
  61. fb_helper->connector_count = 0;
  62. return -ENOMEM;
  63. }
  64. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  65. /**
  66. * drm_fb_helper_connector_parse_command_line - parse command line for connector
  67. * @connector - connector to parse line for
  68. * @mode_option - per connector mode option
  69. *
  70. * This parses the connector specific then generic command lines for
  71. * modes and options to configure the connector.
  72. *
  73. * This uses the same parameters as the fb modedb.c, except for extra
  74. * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
  75. *
  76. * enable/enable Digital/disable bit at the end
  77. */
  78. static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
  79. const char *mode_option)
  80. {
  81. const char *name;
  82. unsigned int namelen;
  83. int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
  84. unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
  85. int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
  86. int i;
  87. enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
  88. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  89. struct drm_connector *connector = fb_helper_conn->connector;
  90. if (!fb_helper_conn)
  91. return false;
  92. cmdline_mode = &fb_helper_conn->cmdline_mode;
  93. if (!mode_option)
  94. mode_option = fb_mode_option;
  95. if (!mode_option) {
  96. cmdline_mode->specified = false;
  97. return false;
  98. }
  99. name = mode_option;
  100. namelen = strlen(name);
  101. for (i = namelen-1; i >= 0; i--) {
  102. switch (name[i]) {
  103. case '@':
  104. namelen = i;
  105. if (!refresh_specified && !bpp_specified &&
  106. !yres_specified) {
  107. refresh = simple_strtol(&name[i+1], NULL, 10);
  108. refresh_specified = 1;
  109. if (cvt || rb)
  110. cvt = 0;
  111. } else
  112. goto done;
  113. break;
  114. case '-':
  115. namelen = i;
  116. if (!bpp_specified && !yres_specified) {
  117. bpp = simple_strtol(&name[i+1], NULL, 10);
  118. bpp_specified = 1;
  119. if (cvt || rb)
  120. cvt = 0;
  121. } else
  122. goto done;
  123. break;
  124. case 'x':
  125. if (!yres_specified) {
  126. yres = simple_strtol(&name[i+1], NULL, 10);
  127. yres_specified = 1;
  128. } else
  129. goto done;
  130. case '0' ... '9':
  131. break;
  132. case 'M':
  133. if (!yres_specified)
  134. cvt = 1;
  135. break;
  136. case 'R':
  137. if (!cvt)
  138. rb = 1;
  139. break;
  140. case 'm':
  141. if (!cvt)
  142. margins = 1;
  143. break;
  144. case 'i':
  145. if (!cvt)
  146. interlace = 1;
  147. break;
  148. case 'e':
  149. force = DRM_FORCE_ON;
  150. break;
  151. case 'D':
  152. if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
  153. (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
  154. force = DRM_FORCE_ON;
  155. else
  156. force = DRM_FORCE_ON_DIGITAL;
  157. break;
  158. case 'd':
  159. force = DRM_FORCE_OFF;
  160. break;
  161. default:
  162. goto done;
  163. }
  164. }
  165. if (i < 0 && yres_specified) {
  166. xres = simple_strtol(name, NULL, 10);
  167. res_specified = 1;
  168. }
  169. done:
  170. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  171. drm_get_connector_name(connector), xres, yres,
  172. (refresh) ? refresh : 60, (rb) ? " reduced blanking" :
  173. "", (margins) ? " with margins" : "", (interlace) ?
  174. " interlaced" : "");
  175. if (force) {
  176. const char *s;
  177. switch (force) {
  178. case DRM_FORCE_OFF: s = "OFF"; break;
  179. case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
  180. default:
  181. case DRM_FORCE_ON: s = "ON"; break;
  182. }
  183. DRM_INFO("forcing %s connector %s\n",
  184. drm_get_connector_name(connector), s);
  185. connector->force = force;
  186. }
  187. if (res_specified) {
  188. cmdline_mode->specified = true;
  189. cmdline_mode->xres = xres;
  190. cmdline_mode->yres = yres;
  191. }
  192. if (refresh_specified) {
  193. cmdline_mode->refresh_specified = true;
  194. cmdline_mode->refresh = refresh;
  195. }
  196. if (bpp_specified) {
  197. cmdline_mode->bpp_specified = true;
  198. cmdline_mode->bpp = bpp;
  199. }
  200. cmdline_mode->rb = rb ? true : false;
  201. cmdline_mode->cvt = cvt ? true : false;
  202. cmdline_mode->interlace = interlace ? true : false;
  203. return true;
  204. }
  205. static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
  206. {
  207. struct drm_fb_helper_connector *fb_helper_conn;
  208. int i;
  209. for (i = 0; i < fb_helper->connector_count; i++) {
  210. char *option = NULL;
  211. fb_helper_conn = fb_helper->connector_info[i];
  212. /* do something on return - turn off connector maybe */
  213. if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
  214. continue;
  215. drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
  216. }
  217. return 0;
  218. }
  219. bool drm_fb_helper_force_kernel_mode(void)
  220. {
  221. int i = 0;
  222. bool ret, error = false;
  223. struct drm_fb_helper *helper;
  224. if (list_empty(&kernel_fb_helper_list))
  225. return false;
  226. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  227. for (i = 0; i < helper->crtc_count; i++) {
  228. struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
  229. ret = drm_crtc_helper_set_config(mode_set);
  230. if (ret)
  231. error = true;
  232. }
  233. }
  234. return error;
  235. }
  236. int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
  237. void *panic_str)
  238. {
  239. DRM_ERROR("panic occurred, switching back to text console\n");
  240. return drm_fb_helper_force_kernel_mode();
  241. return 0;
  242. }
  243. EXPORT_SYMBOL(drm_fb_helper_panic);
  244. static struct notifier_block paniced = {
  245. .notifier_call = drm_fb_helper_panic,
  246. };
  247. /**
  248. * drm_fb_helper_restore - restore the framebuffer console (kernel) config
  249. *
  250. * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
  251. */
  252. void drm_fb_helper_restore(void)
  253. {
  254. bool ret;
  255. ret = drm_fb_helper_force_kernel_mode();
  256. if (ret == true)
  257. DRM_ERROR("Failed to restore crtc configuration\n");
  258. }
  259. EXPORT_SYMBOL(drm_fb_helper_restore);
  260. #ifdef CONFIG_MAGIC_SYSRQ
  261. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  262. {
  263. drm_fb_helper_restore();
  264. }
  265. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  266. static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3)
  267. {
  268. schedule_work(&drm_fb_helper_restore_work);
  269. }
  270. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  271. .handler = drm_fb_helper_sysrq,
  272. .help_msg = "force-fb(V)",
  273. .action_msg = "Restore framebuffer console",
  274. };
  275. #else
  276. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  277. #endif
  278. static void drm_fb_helper_on(struct fb_info *info)
  279. {
  280. struct drm_fb_helper *fb_helper = info->par;
  281. struct drm_device *dev = fb_helper->dev;
  282. struct drm_crtc *crtc;
  283. struct drm_crtc_helper_funcs *crtc_funcs;
  284. struct drm_encoder *encoder;
  285. int i;
  286. /*
  287. * For each CRTC in this fb, turn the crtc on then,
  288. * find all associated encoders and turn them on.
  289. */
  290. mutex_lock(&dev->mode_config.mutex);
  291. for (i = 0; i < fb_helper->crtc_count; i++) {
  292. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  293. crtc_funcs = crtc->helper_private;
  294. if (!crtc->enabled)
  295. continue;
  296. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  297. /* Found a CRTC on this fb, now find encoders */
  298. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  299. if (encoder->crtc == crtc) {
  300. struct drm_encoder_helper_funcs *encoder_funcs;
  301. encoder_funcs = encoder->helper_private;
  302. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  303. }
  304. }
  305. }
  306. mutex_unlock(&dev->mode_config.mutex);
  307. }
  308. static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
  309. {
  310. struct drm_fb_helper *fb_helper = info->par;
  311. struct drm_device *dev = fb_helper->dev;
  312. struct drm_crtc *crtc;
  313. struct drm_crtc_helper_funcs *crtc_funcs;
  314. struct drm_encoder *encoder;
  315. int i;
  316. /*
  317. * For each CRTC in this fb, find all associated encoders
  318. * and turn them off, then turn off the CRTC.
  319. */
  320. mutex_lock(&dev->mode_config.mutex);
  321. for (i = 0; i < fb_helper->crtc_count; i++) {
  322. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  323. crtc_funcs = crtc->helper_private;
  324. if (!crtc->enabled)
  325. continue;
  326. /* Found a CRTC on this fb, now find encoders */
  327. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  328. if (encoder->crtc == crtc) {
  329. struct drm_encoder_helper_funcs *encoder_funcs;
  330. encoder_funcs = encoder->helper_private;
  331. encoder_funcs->dpms(encoder, dpms_mode);
  332. }
  333. }
  334. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  335. }
  336. mutex_unlock(&dev->mode_config.mutex);
  337. }
  338. int drm_fb_helper_blank(int blank, struct fb_info *info)
  339. {
  340. switch (blank) {
  341. /* Display: On; HSync: On, VSync: On */
  342. case FB_BLANK_UNBLANK:
  343. drm_fb_helper_on(info);
  344. break;
  345. /* Display: Off; HSync: On, VSync: On */
  346. case FB_BLANK_NORMAL:
  347. drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
  348. break;
  349. /* Display: Off; HSync: Off, VSync: On */
  350. case FB_BLANK_HSYNC_SUSPEND:
  351. drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
  352. break;
  353. /* Display: Off; HSync: On, VSync: Off */
  354. case FB_BLANK_VSYNC_SUSPEND:
  355. drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
  356. break;
  357. /* Display: Off; HSync: Off, VSync: Off */
  358. case FB_BLANK_POWERDOWN:
  359. drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
  360. break;
  361. }
  362. return 0;
  363. }
  364. EXPORT_SYMBOL(drm_fb_helper_blank);
  365. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  366. {
  367. int i;
  368. for (i = 0; i < helper->connector_count; i++)
  369. kfree(helper->connector_info[i]);
  370. kfree(helper->connector_info);
  371. for (i = 0; i < helper->crtc_count; i++)
  372. kfree(helper->crtc_info[i].mode_set.connectors);
  373. kfree(helper->crtc_info);
  374. }
  375. int drm_fb_helper_init_crtc_count(struct drm_device *dev,
  376. struct drm_fb_helper *helper,
  377. int crtc_count, int max_conn_count)
  378. {
  379. struct drm_crtc *crtc;
  380. int ret = 0;
  381. int i;
  382. INIT_LIST_HEAD(&helper->kernel_fb_list);
  383. helper->dev = dev;
  384. helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  385. if (!helper->crtc_info)
  386. return -ENOMEM;
  387. helper->crtc_count = crtc_count;
  388. helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  389. if (!helper->connector_info) {
  390. kfree(helper->crtc_info);
  391. return -ENOMEM;
  392. }
  393. helper->connector_count = 0;
  394. for (i = 0; i < crtc_count; i++) {
  395. helper->crtc_info[i].mode_set.connectors =
  396. kcalloc(max_conn_count,
  397. sizeof(struct drm_connector *),
  398. GFP_KERNEL);
  399. if (!helper->crtc_info[i].mode_set.connectors) {
  400. ret = -ENOMEM;
  401. goto out_free;
  402. }
  403. helper->crtc_info[i].mode_set.num_connectors = 0;
  404. }
  405. i = 0;
  406. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  407. helper->crtc_info[i].crtc_id = crtc->base.id;
  408. helper->crtc_info[i].mode_set.crtc = crtc;
  409. i++;
  410. }
  411. helper->conn_limit = max_conn_count;
  412. return 0;
  413. out_free:
  414. drm_fb_helper_crtc_free(helper);
  415. return -ENOMEM;
  416. }
  417. EXPORT_SYMBOL(drm_fb_helper_init_crtc_count);
  418. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  419. u16 blue, u16 regno, struct fb_info *info)
  420. {
  421. struct drm_fb_helper *fb_helper = info->par;
  422. struct drm_framebuffer *fb = fb_helper->fb;
  423. int pindex;
  424. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  425. u32 *palette;
  426. u32 value;
  427. /* place color in psuedopalette */
  428. if (regno > 16)
  429. return -EINVAL;
  430. palette = (u32 *)info->pseudo_palette;
  431. red >>= (16 - info->var.red.length);
  432. green >>= (16 - info->var.green.length);
  433. blue >>= (16 - info->var.blue.length);
  434. value = (red << info->var.red.offset) |
  435. (green << info->var.green.offset) |
  436. (blue << info->var.blue.offset);
  437. palette[regno] = value;
  438. return 0;
  439. }
  440. pindex = regno;
  441. if (fb->bits_per_pixel == 16) {
  442. pindex = regno << 3;
  443. if (fb->depth == 16 && regno > 63)
  444. return -EINVAL;
  445. if (fb->depth == 15 && regno > 31)
  446. return -EINVAL;
  447. if (fb->depth == 16) {
  448. u16 r, g, b;
  449. int i;
  450. if (regno < 32) {
  451. for (i = 0; i < 8; i++)
  452. fb_helper->funcs->gamma_set(crtc, red,
  453. green, blue, pindex + i);
  454. }
  455. fb_helper->funcs->gamma_get(crtc, &r,
  456. &g, &b,
  457. pindex >> 1);
  458. for (i = 0; i < 4; i++)
  459. fb_helper->funcs->gamma_set(crtc, r,
  460. green, b,
  461. (pindex >> 1) + i);
  462. }
  463. }
  464. if (fb->depth != 16)
  465. fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
  466. return 0;
  467. }
  468. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  469. {
  470. struct drm_fb_helper *fb_helper = info->par;
  471. struct drm_crtc_helper_funcs *crtc_funcs;
  472. u16 *red, *green, *blue, *transp;
  473. struct drm_crtc *crtc;
  474. int i, rc = 0;
  475. int start;
  476. for (i = 0; i < fb_helper->crtc_count; i++) {
  477. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  478. crtc_funcs = crtc->helper_private;
  479. red = cmap->red;
  480. green = cmap->green;
  481. blue = cmap->blue;
  482. transp = cmap->transp;
  483. start = cmap->start;
  484. for (i = 0; i < cmap->len; i++) {
  485. u16 hred, hgreen, hblue, htransp = 0xffff;
  486. hred = *red++;
  487. hgreen = *green++;
  488. hblue = *blue++;
  489. if (transp)
  490. htransp = *transp++;
  491. rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  492. if (rc)
  493. return rc;
  494. }
  495. crtc_funcs->load_lut(crtc);
  496. }
  497. return rc;
  498. }
  499. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  500. int drm_fb_helper_setcolreg(unsigned regno,
  501. unsigned red,
  502. unsigned green,
  503. unsigned blue,
  504. unsigned transp,
  505. struct fb_info *info)
  506. {
  507. struct drm_fb_helper *fb_helper = info->par;
  508. struct drm_crtc *crtc;
  509. struct drm_crtc_helper_funcs *crtc_funcs;
  510. int i;
  511. int ret;
  512. if (regno > 255)
  513. return 1;
  514. for (i = 0; i < fb_helper->crtc_count; i++) {
  515. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  516. crtc_funcs = crtc->helper_private;
  517. ret = setcolreg(crtc, red, green, blue, regno, info);
  518. if (ret)
  519. return ret;
  520. crtc_funcs->load_lut(crtc);
  521. }
  522. return 0;
  523. }
  524. EXPORT_SYMBOL(drm_fb_helper_setcolreg);
  525. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  526. struct fb_info *info)
  527. {
  528. struct drm_fb_helper *fb_helper = info->par;
  529. struct drm_framebuffer *fb = fb_helper->fb;
  530. int depth;
  531. if (var->pixclock != 0)
  532. return -EINVAL;
  533. /* Need to resize the fb object !!! */
  534. if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) {
  535. DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
  536. "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel,
  537. fb->width, fb->height, fb->bits_per_pixel);
  538. return -EINVAL;
  539. }
  540. switch (var->bits_per_pixel) {
  541. case 16:
  542. depth = (var->green.length == 6) ? 16 : 15;
  543. break;
  544. case 32:
  545. depth = (var->transp.length > 0) ? 32 : 24;
  546. break;
  547. default:
  548. depth = var->bits_per_pixel;
  549. break;
  550. }
  551. switch (depth) {
  552. case 8:
  553. var->red.offset = 0;
  554. var->green.offset = 0;
  555. var->blue.offset = 0;
  556. var->red.length = 8;
  557. var->green.length = 8;
  558. var->blue.length = 8;
  559. var->transp.length = 0;
  560. var->transp.offset = 0;
  561. break;
  562. case 15:
  563. var->red.offset = 10;
  564. var->green.offset = 5;
  565. var->blue.offset = 0;
  566. var->red.length = 5;
  567. var->green.length = 5;
  568. var->blue.length = 5;
  569. var->transp.length = 1;
  570. var->transp.offset = 15;
  571. break;
  572. case 16:
  573. var->red.offset = 11;
  574. var->green.offset = 5;
  575. var->blue.offset = 0;
  576. var->red.length = 5;
  577. var->green.length = 6;
  578. var->blue.length = 5;
  579. var->transp.length = 0;
  580. var->transp.offset = 0;
  581. break;
  582. case 24:
  583. var->red.offset = 16;
  584. var->green.offset = 8;
  585. var->blue.offset = 0;
  586. var->red.length = 8;
  587. var->green.length = 8;
  588. var->blue.length = 8;
  589. var->transp.length = 0;
  590. var->transp.offset = 0;
  591. break;
  592. case 32:
  593. var->red.offset = 16;
  594. var->green.offset = 8;
  595. var->blue.offset = 0;
  596. var->red.length = 8;
  597. var->green.length = 8;
  598. var->blue.length = 8;
  599. var->transp.length = 8;
  600. var->transp.offset = 24;
  601. break;
  602. default:
  603. return -EINVAL;
  604. }
  605. return 0;
  606. }
  607. EXPORT_SYMBOL(drm_fb_helper_check_var);
  608. /* this will let fbcon do the mode init */
  609. int drm_fb_helper_set_par(struct fb_info *info)
  610. {
  611. struct drm_fb_helper *fb_helper = info->par;
  612. struct drm_device *dev = fb_helper->dev;
  613. struct fb_var_screeninfo *var = &info->var;
  614. struct drm_crtc *crtc;
  615. int ret;
  616. int i;
  617. if (var->pixclock != 0) {
  618. DRM_ERROR("PIXEL CLOCK SET\n");
  619. return -EINVAL;
  620. }
  621. mutex_lock(&dev->mode_config.mutex);
  622. for (i = 0; i < fb_helper->crtc_count; i++) {
  623. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  624. ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
  625. if (ret) {
  626. mutex_unlock(&dev->mode_config.mutex);
  627. return ret;
  628. }
  629. }
  630. mutex_unlock(&dev->mode_config.mutex);
  631. return 0;
  632. }
  633. EXPORT_SYMBOL(drm_fb_helper_set_par);
  634. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  635. struct fb_info *info)
  636. {
  637. struct drm_fb_helper *fb_helper = info->par;
  638. struct drm_device *dev = fb_helper->dev;
  639. struct drm_mode_set *modeset;
  640. struct drm_crtc *crtc;
  641. int ret = 0;
  642. int i;
  643. mutex_lock(&dev->mode_config.mutex);
  644. for (i = 0; i < fb_helper->crtc_count; i++) {
  645. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  646. modeset = &fb_helper->crtc_info[i].mode_set;
  647. modeset->x = var->xoffset;
  648. modeset->y = var->yoffset;
  649. if (modeset->num_connectors) {
  650. ret = crtc->funcs->set_config(modeset);
  651. if (!ret) {
  652. info->var.xoffset = var->xoffset;
  653. info->var.yoffset = var->yoffset;
  654. }
  655. }
  656. }
  657. mutex_unlock(&dev->mode_config.mutex);
  658. return ret;
  659. }
  660. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  661. int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  662. int preferred_bpp)
  663. {
  664. int new_fb = 0;
  665. int crtc_count = 0;
  666. int ret, i;
  667. struct fb_info *info;
  668. struct drm_fb_helper_surface_size sizes;
  669. int gamma_size = 0;
  670. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  671. sizes.surface_depth = 24;
  672. sizes.surface_bpp = 32;
  673. sizes.fb_width = (unsigned)-1;
  674. sizes.fb_height = (unsigned)-1;
  675. /* if driver picks 8 or 16 by default use that
  676. for both depth/bpp */
  677. if (preferred_bpp != sizes.surface_bpp) {
  678. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  679. }
  680. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  681. for (i = 0; i < fb_helper->connector_count; i++) {
  682. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  683. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  684. cmdline_mode = &fb_helper_conn->cmdline_mode;
  685. if (cmdline_mode->bpp_specified) {
  686. switch (cmdline_mode->bpp) {
  687. case 8:
  688. sizes.surface_depth = sizes.surface_bpp = 8;
  689. break;
  690. case 15:
  691. sizes.surface_depth = 15;
  692. sizes.surface_bpp = 16;
  693. break;
  694. case 16:
  695. sizes.surface_depth = sizes.surface_bpp = 16;
  696. break;
  697. case 24:
  698. sizes.surface_depth = sizes.surface_bpp = 24;
  699. break;
  700. case 32:
  701. sizes.surface_depth = 24;
  702. sizes.surface_bpp = 32;
  703. break;
  704. }
  705. break;
  706. }
  707. }
  708. crtc_count = 0;
  709. for (i = 0; i < fb_helper->crtc_count; i++) {
  710. struct drm_display_mode *desired_mode;
  711. desired_mode = fb_helper->crtc_info[i].desired_mode;
  712. if (desired_mode) {
  713. if (gamma_size == 0)
  714. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  715. if (desired_mode->hdisplay < sizes.fb_width)
  716. sizes.fb_width = desired_mode->hdisplay;
  717. if (desired_mode->vdisplay < sizes.fb_height)
  718. sizes.fb_height = desired_mode->vdisplay;
  719. if (desired_mode->hdisplay > sizes.surface_width)
  720. sizes.surface_width = desired_mode->hdisplay;
  721. if (desired_mode->vdisplay > sizes.surface_height)
  722. sizes.surface_height = desired_mode->vdisplay;
  723. crtc_count++;
  724. }
  725. }
  726. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  727. /* hmm everyone went away - assume VGA cable just fell out
  728. and will come back later. */
  729. return 0;
  730. }
  731. /* push down into drivers */
  732. new_fb = (*fb_helper->fb_probe)(fb_helper, &sizes);
  733. if (new_fb < 0)
  734. return new_fb;
  735. info = fb_helper->fbdev;
  736. /* set the fb pointer */
  737. for (i = 0; i < fb_helper->crtc_count; i++) {
  738. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  739. }
  740. if (new_fb) {
  741. info->var.pixclock = 0;
  742. ret = fb_alloc_cmap(&info->cmap, gamma_size, 0);
  743. if (ret)
  744. return ret;
  745. if (register_framebuffer(info) < 0) {
  746. fb_dealloc_cmap(&info->cmap);
  747. return -EINVAL;
  748. }
  749. printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
  750. info->fix.id);
  751. } else {
  752. drm_fb_helper_set_par(info);
  753. }
  754. /* Switch back to kernel console on panic */
  755. /* multi card linked list maybe */
  756. if (list_empty(&kernel_fb_helper_list)) {
  757. printk(KERN_INFO "registered panic notifier\n");
  758. atomic_notifier_chain_register(&panic_notifier_list,
  759. &paniced);
  760. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  761. }
  762. if (new_fb)
  763. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  764. return 0;
  765. }
  766. EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
  767. void drm_fb_helper_free(struct drm_fb_helper *helper)
  768. {
  769. if (!list_empty(&helper->kernel_fb_list)) {
  770. list_del(&helper->kernel_fb_list);
  771. if (list_empty(&kernel_fb_helper_list)) {
  772. printk(KERN_INFO "unregistered panic notifier\n");
  773. atomic_notifier_chain_unregister(&panic_notifier_list,
  774. &paniced);
  775. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  776. }
  777. }
  778. drm_fb_helper_crtc_free(helper);
  779. if (helper->fbdev->cmap.len)
  780. fb_dealloc_cmap(&helper->fbdev->cmap);
  781. }
  782. EXPORT_SYMBOL(drm_fb_helper_free);
  783. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  784. uint32_t depth)
  785. {
  786. info->fix.type = FB_TYPE_PACKED_PIXELS;
  787. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  788. FB_VISUAL_TRUECOLOR;
  789. info->fix.type_aux = 0;
  790. info->fix.xpanstep = 1; /* doing it in hw */
  791. info->fix.ypanstep = 1; /* doing it in hw */
  792. info->fix.ywrapstep = 0;
  793. info->fix.accel = FB_ACCEL_NONE;
  794. info->fix.type_aux = 0;
  795. info->fix.line_length = pitch;
  796. return;
  797. }
  798. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  799. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  800. uint32_t fb_width, uint32_t fb_height)
  801. {
  802. struct drm_framebuffer *fb = fb_helper->fb;
  803. info->pseudo_palette = fb_helper->pseudo_palette;
  804. info->var.xres_virtual = fb->width;
  805. info->var.yres_virtual = fb->height;
  806. info->var.bits_per_pixel = fb->bits_per_pixel;
  807. info->var.xoffset = 0;
  808. info->var.yoffset = 0;
  809. info->var.activate = FB_ACTIVATE_NOW;
  810. info->var.height = -1;
  811. info->var.width = -1;
  812. switch (fb->depth) {
  813. case 8:
  814. info->var.red.offset = 0;
  815. info->var.green.offset = 0;
  816. info->var.blue.offset = 0;
  817. info->var.red.length = 8; /* 8bit DAC */
  818. info->var.green.length = 8;
  819. info->var.blue.length = 8;
  820. info->var.transp.offset = 0;
  821. info->var.transp.length = 0;
  822. break;
  823. case 15:
  824. info->var.red.offset = 10;
  825. info->var.green.offset = 5;
  826. info->var.blue.offset = 0;
  827. info->var.red.length = 5;
  828. info->var.green.length = 5;
  829. info->var.blue.length = 5;
  830. info->var.transp.offset = 15;
  831. info->var.transp.length = 1;
  832. break;
  833. case 16:
  834. info->var.red.offset = 11;
  835. info->var.green.offset = 5;
  836. info->var.blue.offset = 0;
  837. info->var.red.length = 5;
  838. info->var.green.length = 6;
  839. info->var.blue.length = 5;
  840. info->var.transp.offset = 0;
  841. break;
  842. case 24:
  843. info->var.red.offset = 16;
  844. info->var.green.offset = 8;
  845. info->var.blue.offset = 0;
  846. info->var.red.length = 8;
  847. info->var.green.length = 8;
  848. info->var.blue.length = 8;
  849. info->var.transp.offset = 0;
  850. info->var.transp.length = 0;
  851. break;
  852. case 32:
  853. info->var.red.offset = 16;
  854. info->var.green.offset = 8;
  855. info->var.blue.offset = 0;
  856. info->var.red.length = 8;
  857. info->var.green.length = 8;
  858. info->var.blue.length = 8;
  859. info->var.transp.offset = 24;
  860. info->var.transp.length = 8;
  861. break;
  862. default:
  863. break;
  864. }
  865. info->var.xres = fb_width;
  866. info->var.yres = fb_height;
  867. }
  868. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  869. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  870. uint32_t maxX,
  871. uint32_t maxY)
  872. {
  873. struct drm_connector *connector;
  874. int count = 0;
  875. int i;
  876. for (i = 0; i < fb_helper->connector_count; i++) {
  877. connector = fb_helper->connector_info[i]->connector;
  878. count += connector->funcs->fill_modes(connector, maxX, maxY);
  879. }
  880. return count;
  881. }
  882. static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  883. {
  884. struct drm_display_mode *mode;
  885. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  886. if (drm_mode_width(mode) > width ||
  887. drm_mode_height(mode) > height)
  888. continue;
  889. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  890. return mode;
  891. }
  892. return NULL;
  893. }
  894. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  895. {
  896. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  897. cmdline_mode = &fb_connector->cmdline_mode;
  898. return cmdline_mode->specified;
  899. }
  900. static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
  901. int width, int height)
  902. {
  903. struct drm_fb_helper_cmdline_mode *cmdline_mode;
  904. struct drm_display_mode *mode = NULL;
  905. cmdline_mode = &fb_helper_conn->cmdline_mode;
  906. if (cmdline_mode->specified == false)
  907. return mode;
  908. /* attempt to find a matching mode in the list of modes
  909. * we have gotten so far, if not add a CVT mode that conforms
  910. */
  911. if (cmdline_mode->rb || cmdline_mode->margins)
  912. goto create_mode;
  913. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  914. /* check width/height */
  915. if (mode->hdisplay != cmdline_mode->xres ||
  916. mode->vdisplay != cmdline_mode->yres)
  917. continue;
  918. if (cmdline_mode->refresh_specified) {
  919. if (mode->vrefresh != cmdline_mode->refresh)
  920. continue;
  921. }
  922. if (cmdline_mode->interlace) {
  923. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  924. continue;
  925. }
  926. return mode;
  927. }
  928. create_mode:
  929. mode = drm_cvt_mode(fb_helper_conn->connector->dev, cmdline_mode->xres,
  930. cmdline_mode->yres,
  931. cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
  932. cmdline_mode->rb, cmdline_mode->interlace,
  933. cmdline_mode->margins);
  934. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  935. list_add(&mode->head, &fb_helper_conn->connector->modes);
  936. return mode;
  937. }
  938. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  939. {
  940. bool enable;
  941. if (strict) {
  942. enable = connector->status == connector_status_connected;
  943. } else {
  944. enable = connector->status != connector_status_disconnected;
  945. }
  946. return enable;
  947. }
  948. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  949. bool *enabled)
  950. {
  951. bool any_enabled = false;
  952. struct drm_connector *connector;
  953. int i = 0;
  954. for (i = 0; i < fb_helper->connector_count; i++) {
  955. connector = fb_helper->connector_info[i]->connector;
  956. enabled[i] = drm_connector_enabled(connector, true);
  957. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  958. enabled[i] ? "yes" : "no");
  959. any_enabled |= enabled[i];
  960. }
  961. if (any_enabled)
  962. return;
  963. for (i = 0; i < fb_helper->connector_count; i++) {
  964. connector = fb_helper->connector_info[i]->connector;
  965. enabled[i] = drm_connector_enabled(connector, false);
  966. }
  967. }
  968. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  969. struct drm_display_mode **modes,
  970. bool *enabled, int width, int height)
  971. {
  972. struct drm_fb_helper_connector *fb_helper_conn;
  973. int i;
  974. for (i = 0; i < fb_helper->connector_count; i++) {
  975. fb_helper_conn = fb_helper->connector_info[i];
  976. if (enabled[i] == false)
  977. continue;
  978. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  979. fb_helper_conn->connector->base.id);
  980. /* got for command line mode first */
  981. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  982. if (!modes[i]) {
  983. DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  984. fb_helper_conn->connector->base.id);
  985. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  986. }
  987. /* No preferred modes, pick one off the list */
  988. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  989. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  990. break;
  991. }
  992. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  993. "none");
  994. }
  995. return true;
  996. }
  997. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  998. struct drm_fb_helper_crtc **best_crtcs,
  999. struct drm_display_mode **modes,
  1000. int n, int width, int height)
  1001. {
  1002. int c, o;
  1003. struct drm_device *dev = fb_helper->dev;
  1004. struct drm_connector *connector;
  1005. struct drm_connector_helper_funcs *connector_funcs;
  1006. struct drm_encoder *encoder;
  1007. struct drm_fb_helper_crtc *best_crtc;
  1008. int my_score, best_score, score;
  1009. struct drm_fb_helper_crtc **crtcs, *crtc;
  1010. struct drm_fb_helper_connector *fb_helper_conn;
  1011. if (n == fb_helper->connector_count)
  1012. return 0;
  1013. fb_helper_conn = fb_helper->connector_info[n];
  1014. connector = fb_helper_conn->connector;
  1015. best_crtcs[n] = NULL;
  1016. best_crtc = NULL;
  1017. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1018. if (modes[n] == NULL)
  1019. return best_score;
  1020. crtcs = kzalloc(dev->mode_config.num_connector *
  1021. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1022. if (!crtcs)
  1023. return best_score;
  1024. my_score = 1;
  1025. if (connector->status == connector_status_connected)
  1026. my_score++;
  1027. if (drm_has_cmdline_mode(fb_helper_conn))
  1028. my_score++;
  1029. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1030. my_score++;
  1031. connector_funcs = connector->helper_private;
  1032. encoder = connector_funcs->best_encoder(connector);
  1033. if (!encoder)
  1034. goto out;
  1035. /* select a crtc for this connector and then attempt to configure
  1036. remaining connectors */
  1037. for (c = 0; c < fb_helper->crtc_count; c++) {
  1038. crtc = &fb_helper->crtc_info[c];
  1039. if ((encoder->possible_crtcs & (1 << c)) == 0) {
  1040. continue;
  1041. }
  1042. for (o = 0; o < n; o++)
  1043. if (best_crtcs[o] == crtc)
  1044. break;
  1045. if (o < n) {
  1046. /* ignore cloning for now */
  1047. continue;
  1048. }
  1049. crtcs[n] = crtc;
  1050. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1051. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1052. width, height);
  1053. if (score > best_score) {
  1054. best_crtc = crtc;
  1055. best_score = score;
  1056. memcpy(best_crtcs, crtcs,
  1057. dev->mode_config.num_connector *
  1058. sizeof(struct drm_fb_helper_crtc *));
  1059. }
  1060. }
  1061. out:
  1062. kfree(crtcs);
  1063. return best_score;
  1064. }
  1065. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  1066. {
  1067. struct drm_device *dev = fb_helper->dev;
  1068. struct drm_fb_helper_crtc **crtcs;
  1069. struct drm_display_mode **modes;
  1070. struct drm_encoder *encoder;
  1071. struct drm_mode_set *modeset;
  1072. bool *enabled;
  1073. int width, height;
  1074. int i, ret;
  1075. DRM_DEBUG_KMS("\n");
  1076. width = dev->mode_config.max_width;
  1077. height = dev->mode_config.max_height;
  1078. /* clean out all the encoder/crtc combos */
  1079. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1080. encoder->crtc = NULL;
  1081. }
  1082. crtcs = kcalloc(dev->mode_config.num_connector,
  1083. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1084. modes = kcalloc(dev->mode_config.num_connector,
  1085. sizeof(struct drm_display_mode *), GFP_KERNEL);
  1086. enabled = kcalloc(dev->mode_config.num_connector,
  1087. sizeof(bool), GFP_KERNEL);
  1088. drm_enable_connectors(fb_helper, enabled);
  1089. ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
  1090. if (!ret)
  1091. DRM_ERROR("Unable to find initial modes\n");
  1092. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
  1093. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1094. /* need to set the modesets up here for use later */
  1095. /* fill out the connector<->crtc mappings into the modesets */
  1096. for (i = 0; i < fb_helper->crtc_count; i++) {
  1097. modeset = &fb_helper->crtc_info[i].mode_set;
  1098. modeset->num_connectors = 0;
  1099. }
  1100. for (i = 0; i < fb_helper->connector_count; i++) {
  1101. struct drm_display_mode *mode = modes[i];
  1102. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1103. modeset = &fb_crtc->mode_set;
  1104. if (mode && fb_crtc) {
  1105. DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  1106. mode->name, fb_crtc->mode_set.crtc->base.id);
  1107. fb_crtc->desired_mode = mode;
  1108. if (modeset->mode)
  1109. drm_mode_destroy(dev, modeset->mode);
  1110. modeset->mode = drm_mode_duplicate(dev,
  1111. fb_crtc->desired_mode);
  1112. modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  1113. }
  1114. }
  1115. kfree(crtcs);
  1116. kfree(modes);
  1117. kfree(enabled);
  1118. }
  1119. /**
  1120. * drm_helper_initial_config - setup a sane initial connector configuration
  1121. * @dev: DRM device
  1122. *
  1123. * LOCKING:
  1124. * Called at init time, must take mode config lock.
  1125. *
  1126. * Scan the CRTCs and connectors and try to put together an initial setup.
  1127. * At the moment, this is a cloned configuration across all heads with
  1128. * a new framebuffer object as the backing store.
  1129. *
  1130. * RETURNS:
  1131. * Zero if everything went ok, nonzero otherwise.
  1132. */
  1133. bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper)
  1134. {
  1135. struct drm_device *dev = fb_helper->dev;
  1136. int count = 0;
  1137. /* disable all the possible outputs/crtcs before entering KMS mode */
  1138. drm_helper_disable_unused_functions(fb_helper->dev);
  1139. drm_fb_helper_parse_command_line(fb_helper);
  1140. count = drm_fb_helper_probe_connector_modes(fb_helper,
  1141. dev->mode_config.max_width,
  1142. dev->mode_config.max_height);
  1143. /*
  1144. * we shouldn't end up with no modes here.
  1145. */
  1146. if (count == 0)
  1147. printk(KERN_INFO "No connectors reported connected with modes\n");
  1148. drm_setup_crtcs(fb_helper);
  1149. return 0;
  1150. }
  1151. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1152. bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper,
  1153. u32 max_width, u32 max_height)
  1154. {
  1155. DRM_DEBUG_KMS("\n");
  1156. drm_fb_helper_probe_connector_modes(fb_helper, max_width,
  1157. max_height);
  1158. drm_setup_crtcs(fb_helper);
  1159. return true;
  1160. }
  1161. EXPORT_SYMBOL(drm_helper_fb_hotplug_event);