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