drm_fb_helper.c 38 KB

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