drm_fb_helper.c 38 KB

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