drm_fb_helper.c 35 KB

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