drm_fb_helper.c 37 KB

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