drm_fb_helper.c 37 KB

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