drm_connector.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*
  2. * Copyright (c) 2016 Intel Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <drm/drmP.h>
  23. #include <drm/drm_connector.h>
  24. #include <drm/drm_edid.h>
  25. #include <drm/drm_encoder.h>
  26. #include "drm_crtc_internal.h"
  27. #include "drm_internal.h"
  28. /**
  29. * DOC: overview
  30. *
  31. * In DRM connectors are the general abstraction for display sinks, and include
  32. * als fixed panels or anything else that can display pixels in some form. As
  33. * opposed to all other KMS objects representing hardware (like CRTC, encoder or
  34. * plane abstractions) connectors can be hotplugged and unplugged at runtime.
  35. * Hence they are reference-counted using drm_connector_reference() and
  36. * drm_connector_unreference().
  37. *
  38. * KMS driver must create, initialize, register and attach at a struct
  39. * &drm_connector for each such sink. The instance is created as other KMS
  40. * objects and initialized by setting the following fields.
  41. *
  42. * The connector is then registered with a call to drm_connector_init() with a
  43. * pointer to the connector functions and a connector type, and exposed through
  44. * sysfs with a call to drm_connector_register().
  45. *
  46. * Connectors must be attached to an encoder to be used. For devices that map
  47. * connectors to encoders 1:1, the connector should be attached at
  48. * initialization time with a call to drm_mode_connector_attach_encoder(). The
  49. * driver must also set the struct &drm_connector encoder field to point to the
  50. * attached encoder.
  51. *
  52. * For connectors which are not fixed (like built-in panels) the driver needs to
  53. * support hotplug notifications. The simplest way to do that is by using the
  54. * probe helpers, see drm_kms_helper_poll_init() for connectors which don't have
  55. * hardware support for hotplug interrupts. Connectors with hardware hotplug
  56. * support can instead use e.g. drm_helper_hpd_irq_event().
  57. */
  58. struct drm_conn_prop_enum_list {
  59. int type;
  60. const char *name;
  61. struct ida ida;
  62. };
  63. /*
  64. * Connector and encoder types.
  65. */
  66. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  67. { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  68. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  69. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  70. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  71. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  72. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  73. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  74. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  75. { DRM_MODE_CONNECTOR_Component, "Component" },
  76. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  77. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  78. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  79. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  80. { DRM_MODE_CONNECTOR_TV, "TV" },
  81. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  82. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  83. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  84. { DRM_MODE_CONNECTOR_DPI, "DPI" },
  85. };
  86. void drm_connector_ida_init(void)
  87. {
  88. int i;
  89. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  90. ida_init(&drm_connector_enum_list[i].ida);
  91. }
  92. void drm_connector_ida_destroy(void)
  93. {
  94. int i;
  95. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  96. ida_destroy(&drm_connector_enum_list[i].ida);
  97. }
  98. /**
  99. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  100. * @connector: connector to quwery
  101. *
  102. * The kernel supports per-connector configuration of its consoles through
  103. * use of the video= parameter. This function parses that option and
  104. * extracts the user's specified mode (or enable/disable status) for a
  105. * particular connector. This is typically only used during the early fbdev
  106. * setup.
  107. */
  108. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  109. {
  110. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  111. char *option = NULL;
  112. if (fb_get_options(connector->name, &option))
  113. return;
  114. if (!drm_mode_parse_command_line_for_connector(option,
  115. connector,
  116. mode))
  117. return;
  118. if (mode->force) {
  119. const char *s;
  120. switch (mode->force) {
  121. case DRM_FORCE_OFF:
  122. s = "OFF";
  123. break;
  124. case DRM_FORCE_ON_DIGITAL:
  125. s = "ON - dig";
  126. break;
  127. default:
  128. case DRM_FORCE_ON:
  129. s = "ON";
  130. break;
  131. }
  132. DRM_INFO("forcing %s connector %s\n", connector->name, s);
  133. connector->force = mode->force;
  134. }
  135. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  136. connector->name,
  137. mode->xres, mode->yres,
  138. mode->refresh_specified ? mode->refresh : 60,
  139. mode->rb ? " reduced blanking" : "",
  140. mode->margins ? " with margins" : "",
  141. mode->interlace ? " interlaced" : "");
  142. }
  143. static void drm_connector_free(struct kref *kref)
  144. {
  145. struct drm_connector *connector =
  146. container_of(kref, struct drm_connector, base.refcount);
  147. struct drm_device *dev = connector->dev;
  148. drm_mode_object_unregister(dev, &connector->base);
  149. connector->funcs->destroy(connector);
  150. }
  151. /**
  152. * drm_connector_init - Init a preallocated connector
  153. * @dev: DRM device
  154. * @connector: the connector to init
  155. * @funcs: callbacks for this connector
  156. * @connector_type: user visible type of the connector
  157. *
  158. * Initialises a preallocated connector. Connectors should be
  159. * subclassed as part of driver connector objects.
  160. *
  161. * Returns:
  162. * Zero on success, error code on failure.
  163. */
  164. int drm_connector_init(struct drm_device *dev,
  165. struct drm_connector *connector,
  166. const struct drm_connector_funcs *funcs,
  167. int connector_type)
  168. {
  169. struct drm_mode_config *config = &dev->mode_config;
  170. int ret;
  171. struct ida *connector_ida =
  172. &drm_connector_enum_list[connector_type].ida;
  173. drm_modeset_lock_all(dev);
  174. ret = drm_mode_object_get_reg(dev, &connector->base,
  175. DRM_MODE_OBJECT_CONNECTOR,
  176. false, drm_connector_free);
  177. if (ret)
  178. goto out_unlock;
  179. connector->base.properties = &connector->properties;
  180. connector->dev = dev;
  181. connector->funcs = funcs;
  182. ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
  183. if (ret < 0)
  184. goto out_put;
  185. connector->index = ret;
  186. ret = 0;
  187. connector->connector_type = connector_type;
  188. connector->connector_type_id =
  189. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  190. if (connector->connector_type_id < 0) {
  191. ret = connector->connector_type_id;
  192. goto out_put_id;
  193. }
  194. connector->name =
  195. kasprintf(GFP_KERNEL, "%s-%d",
  196. drm_connector_enum_list[connector_type].name,
  197. connector->connector_type_id);
  198. if (!connector->name) {
  199. ret = -ENOMEM;
  200. goto out_put_type_id;
  201. }
  202. INIT_LIST_HEAD(&connector->probed_modes);
  203. INIT_LIST_HEAD(&connector->modes);
  204. connector->edid_blob_ptr = NULL;
  205. connector->status = connector_status_unknown;
  206. drm_connector_get_cmdline_mode(connector);
  207. /* We should add connectors at the end to avoid upsetting the connector
  208. * index too much. */
  209. list_add_tail(&connector->head, &config->connector_list);
  210. config->num_connector++;
  211. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  212. drm_object_attach_property(&connector->base,
  213. config->edid_property,
  214. 0);
  215. drm_object_attach_property(&connector->base,
  216. config->dpms_property, 0);
  217. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  218. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  219. }
  220. connector->debugfs_entry = NULL;
  221. out_put_type_id:
  222. if (ret)
  223. ida_simple_remove(connector_ida, connector->connector_type_id);
  224. out_put_id:
  225. if (ret)
  226. ida_simple_remove(&config->connector_ida, connector->index);
  227. out_put:
  228. if (ret)
  229. drm_mode_object_unregister(dev, &connector->base);
  230. out_unlock:
  231. drm_modeset_unlock_all(dev);
  232. return ret;
  233. }
  234. EXPORT_SYMBOL(drm_connector_init);
  235. /**
  236. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  237. * @connector: connector to attach
  238. * @encoder: encoder to attach @connector to
  239. *
  240. * This function links up a connector to an encoder. Note that the routing
  241. * restrictions between encoders and crtcs are exposed to userspace through the
  242. * possible_clones and possible_crtcs bitmasks.
  243. *
  244. * Returns:
  245. * Zero on success, negative errno on failure.
  246. */
  247. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  248. struct drm_encoder *encoder)
  249. {
  250. int i;
  251. /*
  252. * In the past, drivers have attempted to model the static association
  253. * of connector to encoder in simple connector/encoder devices using a
  254. * direct assignment of connector->encoder = encoder. This connection
  255. * is a logical one and the responsibility of the core, so drivers are
  256. * expected not to mess with this.
  257. *
  258. * Note that the error return should've been enough here, but a large
  259. * majority of drivers ignores the return value, so add in a big WARN
  260. * to get people's attention.
  261. */
  262. if (WARN_ON(connector->encoder))
  263. return -EINVAL;
  264. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  265. if (connector->encoder_ids[i] == 0) {
  266. connector->encoder_ids[i] = encoder->base.id;
  267. return 0;
  268. }
  269. }
  270. return -ENOMEM;
  271. }
  272. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  273. static void drm_mode_remove(struct drm_connector *connector,
  274. struct drm_display_mode *mode)
  275. {
  276. list_del(&mode->head);
  277. drm_mode_destroy(connector->dev, mode);
  278. }
  279. /**
  280. * drm_connector_cleanup - cleans up an initialised connector
  281. * @connector: connector to cleanup
  282. *
  283. * Cleans up the connector but doesn't free the object.
  284. */
  285. void drm_connector_cleanup(struct drm_connector *connector)
  286. {
  287. struct drm_device *dev = connector->dev;
  288. struct drm_display_mode *mode, *t;
  289. /* The connector should have been removed from userspace long before
  290. * it is finally destroyed.
  291. */
  292. if (WARN_ON(connector->registered))
  293. drm_connector_unregister(connector);
  294. if (connector->tile_group) {
  295. drm_mode_put_tile_group(dev, connector->tile_group);
  296. connector->tile_group = NULL;
  297. }
  298. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  299. drm_mode_remove(connector, mode);
  300. list_for_each_entry_safe(mode, t, &connector->modes, head)
  301. drm_mode_remove(connector, mode);
  302. ida_simple_remove(&drm_connector_enum_list[connector->connector_type].ida,
  303. connector->connector_type_id);
  304. ida_simple_remove(&dev->mode_config.connector_ida,
  305. connector->index);
  306. kfree(connector->display_info.bus_formats);
  307. drm_mode_object_unregister(dev, &connector->base);
  308. kfree(connector->name);
  309. connector->name = NULL;
  310. list_del(&connector->head);
  311. dev->mode_config.num_connector--;
  312. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  313. if (connector->state && connector->funcs->atomic_destroy_state)
  314. connector->funcs->atomic_destroy_state(connector,
  315. connector->state);
  316. memset(connector, 0, sizeof(*connector));
  317. }
  318. EXPORT_SYMBOL(drm_connector_cleanup);
  319. /**
  320. * drm_connector_register - register a connector
  321. * @connector: the connector to register
  322. *
  323. * Register userspace interfaces for a connector
  324. *
  325. * Returns:
  326. * Zero on success, error code on failure.
  327. */
  328. int drm_connector_register(struct drm_connector *connector)
  329. {
  330. int ret;
  331. if (connector->registered)
  332. return 0;
  333. ret = drm_sysfs_connector_add(connector);
  334. if (ret)
  335. return ret;
  336. ret = drm_debugfs_connector_add(connector);
  337. if (ret) {
  338. goto err_sysfs;
  339. }
  340. if (connector->funcs->late_register) {
  341. ret = connector->funcs->late_register(connector);
  342. if (ret)
  343. goto err_debugfs;
  344. }
  345. drm_mode_object_register(connector->dev, &connector->base);
  346. connector->registered = true;
  347. return 0;
  348. err_debugfs:
  349. drm_debugfs_connector_remove(connector);
  350. err_sysfs:
  351. drm_sysfs_connector_remove(connector);
  352. return ret;
  353. }
  354. EXPORT_SYMBOL(drm_connector_register);
  355. /**
  356. * drm_connector_unregister - unregister a connector
  357. * @connector: the connector to unregister
  358. *
  359. * Unregister userspace interfaces for a connector
  360. */
  361. void drm_connector_unregister(struct drm_connector *connector)
  362. {
  363. if (!connector->registered)
  364. return;
  365. if (connector->funcs->early_unregister)
  366. connector->funcs->early_unregister(connector);
  367. drm_sysfs_connector_remove(connector);
  368. drm_debugfs_connector_remove(connector);
  369. connector->registered = false;
  370. }
  371. EXPORT_SYMBOL(drm_connector_unregister);
  372. void drm_connector_unregister_all(struct drm_device *dev)
  373. {
  374. struct drm_connector *connector;
  375. /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
  376. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  377. drm_connector_unregister(connector);
  378. }
  379. int drm_connector_register_all(struct drm_device *dev)
  380. {
  381. struct drm_connector *connector;
  382. int ret;
  383. /* FIXME: taking the mode config mutex ends up in a clash with
  384. * fbcon/backlight registration */
  385. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  386. ret = drm_connector_register(connector);
  387. if (ret)
  388. goto err;
  389. }
  390. return 0;
  391. err:
  392. mutex_unlock(&dev->mode_config.mutex);
  393. drm_connector_unregister_all(dev);
  394. return ret;
  395. }
  396. /**
  397. * drm_get_connector_status_name - return a string for connector status
  398. * @status: connector status to compute name of
  399. *
  400. * In contrast to the other drm_get_*_name functions this one here returns a
  401. * const pointer and hence is threadsafe.
  402. */
  403. const char *drm_get_connector_status_name(enum drm_connector_status status)
  404. {
  405. if (status == connector_status_connected)
  406. return "connected";
  407. else if (status == connector_status_disconnected)
  408. return "disconnected";
  409. else
  410. return "unknown";
  411. }
  412. EXPORT_SYMBOL(drm_get_connector_status_name);
  413. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  414. { SubPixelUnknown, "Unknown" },
  415. { SubPixelHorizontalRGB, "Horizontal RGB" },
  416. { SubPixelHorizontalBGR, "Horizontal BGR" },
  417. { SubPixelVerticalRGB, "Vertical RGB" },
  418. { SubPixelVerticalBGR, "Vertical BGR" },
  419. { SubPixelNone, "None" },
  420. };
  421. /**
  422. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  423. * @order: enum of subpixel_order
  424. *
  425. * Note you could abuse this and return something out of bounds, but that
  426. * would be a caller error. No unscrubbed user data should make it here.
  427. */
  428. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  429. {
  430. return drm_subpixel_enum_list[order].name;
  431. }
  432. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  433. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  434. { DRM_MODE_DPMS_ON, "On" },
  435. { DRM_MODE_DPMS_STANDBY, "Standby" },
  436. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  437. { DRM_MODE_DPMS_OFF, "Off" }
  438. };
  439. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  440. /**
  441. * drm_display_info_set_bus_formats - set the supported bus formats
  442. * @info: display info to store bus formats in
  443. * @formats: array containing the supported bus formats
  444. * @num_formats: the number of entries in the fmts array
  445. *
  446. * Store the supported bus formats in display info structure.
  447. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  448. * a full list of available formats.
  449. */
  450. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  451. const u32 *formats,
  452. unsigned int num_formats)
  453. {
  454. u32 *fmts = NULL;
  455. if (!formats && num_formats)
  456. return -EINVAL;
  457. if (formats && num_formats) {
  458. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  459. GFP_KERNEL);
  460. if (!fmts)
  461. return -ENOMEM;
  462. }
  463. kfree(info->bus_formats);
  464. info->bus_formats = fmts;
  465. info->num_bus_formats = num_formats;
  466. return 0;
  467. }
  468. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  469. /* Optional connector properties. */
  470. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  471. { DRM_MODE_SCALE_NONE, "None" },
  472. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  473. { DRM_MODE_SCALE_CENTER, "Center" },
  474. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  475. };
  476. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  477. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  478. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  479. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  480. };
  481. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  482. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  483. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  484. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  485. };
  486. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  487. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  488. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  489. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  490. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  491. };
  492. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  493. drm_dvi_i_subconnector_enum_list)
  494. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  495. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  496. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  497. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  498. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  499. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  500. };
  501. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  502. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  503. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  504. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  505. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  506. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  507. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  508. };
  509. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  510. drm_tv_subconnector_enum_list)
  511. /**
  512. * DOC: standard connector properties
  513. *
  514. * DRM connectors have a few standardized properties:
  515. *
  516. * EDID:
  517. * Blob property which contains the current EDID read from the sink. This
  518. * is useful to parse sink identification information like vendor, model
  519. * and serial. Drivers should update this property by calling
  520. * drm_mode_connector_update_edid_property(), usually after having parsed
  521. * the EDID using drm_add_edid_modes(). Userspace cannot change this
  522. * property.
  523. * DPMS:
  524. * Legacy property for setting the power state of the connector. For atomic
  525. * drivers this is only provided for backwards compatibility with existing
  526. * drivers, it remaps to controlling the "ACTIVE" property on the CRTC the
  527. * connector is linked to. Drivers should never set this property directly,
  528. * it is handled by the DRM core by calling the ->dpms() callback in
  529. * &drm_connector_funcs. Atomic drivers should implement this hook using
  530. * drm_atomic_helper_connector_dpms(). This is the only property standard
  531. * connector property that userspace can change.
  532. * PATH:
  533. * Connector path property to identify how this sink is physically
  534. * connected. Used by DP MST. This should be set by calling
  535. * drm_mode_connector_set_path_property(), in the case of DP MST with the
  536. * path property the MST manager created. Userspace cannot change this
  537. * property.
  538. * TILE:
  539. * Connector tile group property to indicate how a set of DRM connector
  540. * compose together into one logical screen. This is used by both high-res
  541. * external screens (often only using a single cable, but exposing multiple
  542. * DP MST sinks), or high-res integrated panels (like dual-link DSI) which
  543. * are not gen-locked. Note that for tiled panels which are genlocked, like
  544. * dual-link LVDS or dual-link DSI, the driver should try to not expose the
  545. * tiling and virtualize both &drm_crtc and &drm_plane if needed. Drivers
  546. * should update this value using drm_mode_connector_set_tile_property().
  547. * Userspace cannot change this property.
  548. *
  549. * Connectors also have one standardized atomic property:
  550. *
  551. * CRTC_ID:
  552. * Mode object ID of the &drm_crtc this connector should be connected to.
  553. */
  554. int drm_connector_create_standard_properties(struct drm_device *dev)
  555. {
  556. struct drm_property *prop;
  557. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  558. DRM_MODE_PROP_IMMUTABLE,
  559. "EDID", 0);
  560. if (!prop)
  561. return -ENOMEM;
  562. dev->mode_config.edid_property = prop;
  563. prop = drm_property_create_enum(dev, 0,
  564. "DPMS", drm_dpms_enum_list,
  565. ARRAY_SIZE(drm_dpms_enum_list));
  566. if (!prop)
  567. return -ENOMEM;
  568. dev->mode_config.dpms_property = prop;
  569. prop = drm_property_create(dev,
  570. DRM_MODE_PROP_BLOB |
  571. DRM_MODE_PROP_IMMUTABLE,
  572. "PATH", 0);
  573. if (!prop)
  574. return -ENOMEM;
  575. dev->mode_config.path_property = prop;
  576. prop = drm_property_create(dev,
  577. DRM_MODE_PROP_BLOB |
  578. DRM_MODE_PROP_IMMUTABLE,
  579. "TILE", 0);
  580. if (!prop)
  581. return -ENOMEM;
  582. dev->mode_config.tile_property = prop;
  583. return 0;
  584. }
  585. /**
  586. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  587. * @dev: DRM device
  588. *
  589. * Called by a driver the first time a DVI-I connector is made.
  590. */
  591. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  592. {
  593. struct drm_property *dvi_i_selector;
  594. struct drm_property *dvi_i_subconnector;
  595. if (dev->mode_config.dvi_i_select_subconnector_property)
  596. return 0;
  597. dvi_i_selector =
  598. drm_property_create_enum(dev, 0,
  599. "select subconnector",
  600. drm_dvi_i_select_enum_list,
  601. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  602. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  603. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  604. "subconnector",
  605. drm_dvi_i_subconnector_enum_list,
  606. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  607. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  608. return 0;
  609. }
  610. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  611. /**
  612. * drm_create_tv_properties - create TV specific connector properties
  613. * @dev: DRM device
  614. * @num_modes: number of different TV formats (modes) supported
  615. * @modes: array of pointers to strings containing name of each format
  616. *
  617. * Called by a driver's TV initialization routine, this function creates
  618. * the TV specific connector properties for a given device. Caller is
  619. * responsible for allocating a list of format names and passing them to
  620. * this routine.
  621. */
  622. int drm_mode_create_tv_properties(struct drm_device *dev,
  623. unsigned int num_modes,
  624. const char * const modes[])
  625. {
  626. struct drm_property *tv_selector;
  627. struct drm_property *tv_subconnector;
  628. unsigned int i;
  629. if (dev->mode_config.tv_select_subconnector_property)
  630. return 0;
  631. /*
  632. * Basic connector properties
  633. */
  634. tv_selector = drm_property_create_enum(dev, 0,
  635. "select subconnector",
  636. drm_tv_select_enum_list,
  637. ARRAY_SIZE(drm_tv_select_enum_list));
  638. if (!tv_selector)
  639. goto nomem;
  640. dev->mode_config.tv_select_subconnector_property = tv_selector;
  641. tv_subconnector =
  642. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  643. "subconnector",
  644. drm_tv_subconnector_enum_list,
  645. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  646. if (!tv_subconnector)
  647. goto nomem;
  648. dev->mode_config.tv_subconnector_property = tv_subconnector;
  649. /*
  650. * Other, TV specific properties: margins & TV modes.
  651. */
  652. dev->mode_config.tv_left_margin_property =
  653. drm_property_create_range(dev, 0, "left margin", 0, 100);
  654. if (!dev->mode_config.tv_left_margin_property)
  655. goto nomem;
  656. dev->mode_config.tv_right_margin_property =
  657. drm_property_create_range(dev, 0, "right margin", 0, 100);
  658. if (!dev->mode_config.tv_right_margin_property)
  659. goto nomem;
  660. dev->mode_config.tv_top_margin_property =
  661. drm_property_create_range(dev, 0, "top margin", 0, 100);
  662. if (!dev->mode_config.tv_top_margin_property)
  663. goto nomem;
  664. dev->mode_config.tv_bottom_margin_property =
  665. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  666. if (!dev->mode_config.tv_bottom_margin_property)
  667. goto nomem;
  668. dev->mode_config.tv_mode_property =
  669. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  670. "mode", num_modes);
  671. if (!dev->mode_config.tv_mode_property)
  672. goto nomem;
  673. for (i = 0; i < num_modes; i++)
  674. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  675. i, modes[i]);
  676. dev->mode_config.tv_brightness_property =
  677. drm_property_create_range(dev, 0, "brightness", 0, 100);
  678. if (!dev->mode_config.tv_brightness_property)
  679. goto nomem;
  680. dev->mode_config.tv_contrast_property =
  681. drm_property_create_range(dev, 0, "contrast", 0, 100);
  682. if (!dev->mode_config.tv_contrast_property)
  683. goto nomem;
  684. dev->mode_config.tv_flicker_reduction_property =
  685. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  686. if (!dev->mode_config.tv_flicker_reduction_property)
  687. goto nomem;
  688. dev->mode_config.tv_overscan_property =
  689. drm_property_create_range(dev, 0, "overscan", 0, 100);
  690. if (!dev->mode_config.tv_overscan_property)
  691. goto nomem;
  692. dev->mode_config.tv_saturation_property =
  693. drm_property_create_range(dev, 0, "saturation", 0, 100);
  694. if (!dev->mode_config.tv_saturation_property)
  695. goto nomem;
  696. dev->mode_config.tv_hue_property =
  697. drm_property_create_range(dev, 0, "hue", 0, 100);
  698. if (!dev->mode_config.tv_hue_property)
  699. goto nomem;
  700. return 0;
  701. nomem:
  702. return -ENOMEM;
  703. }
  704. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  705. /**
  706. * drm_mode_create_scaling_mode_property - create scaling mode property
  707. * @dev: DRM device
  708. *
  709. * Called by a driver the first time it's needed, must be attached to desired
  710. * connectors.
  711. */
  712. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  713. {
  714. struct drm_property *scaling_mode;
  715. if (dev->mode_config.scaling_mode_property)
  716. return 0;
  717. scaling_mode =
  718. drm_property_create_enum(dev, 0, "scaling mode",
  719. drm_scaling_mode_enum_list,
  720. ARRAY_SIZE(drm_scaling_mode_enum_list));
  721. dev->mode_config.scaling_mode_property = scaling_mode;
  722. return 0;
  723. }
  724. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  725. /**
  726. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  727. * @dev: DRM device
  728. *
  729. * Called by a driver the first time it's needed, must be attached to desired
  730. * connectors.
  731. *
  732. * Returns:
  733. * Zero on success, negative errno on failure.
  734. */
  735. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  736. {
  737. if (dev->mode_config.aspect_ratio_property)
  738. return 0;
  739. dev->mode_config.aspect_ratio_property =
  740. drm_property_create_enum(dev, 0, "aspect ratio",
  741. drm_aspect_ratio_enum_list,
  742. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  743. if (dev->mode_config.aspect_ratio_property == NULL)
  744. return -ENOMEM;
  745. return 0;
  746. }
  747. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  748. /**
  749. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  750. * @dev: DRM device
  751. *
  752. * Create the the suggested x/y offset property for connectors.
  753. */
  754. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  755. {
  756. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  757. return 0;
  758. dev->mode_config.suggested_x_property =
  759. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  760. dev->mode_config.suggested_y_property =
  761. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  762. if (dev->mode_config.suggested_x_property == NULL ||
  763. dev->mode_config.suggested_y_property == NULL)
  764. return -ENOMEM;
  765. return 0;
  766. }
  767. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  768. /**
  769. * drm_mode_connector_set_path_property - set tile property on connector
  770. * @connector: connector to set property on.
  771. * @path: path to use for property; must not be NULL.
  772. *
  773. * This creates a property to expose to userspace to specify a
  774. * connector path. This is mainly used for DisplayPort MST where
  775. * connectors have a topology and we want to allow userspace to give
  776. * them more meaningful names.
  777. *
  778. * Returns:
  779. * Zero on success, negative errno on failure.
  780. */
  781. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  782. const char *path)
  783. {
  784. struct drm_device *dev = connector->dev;
  785. int ret;
  786. ret = drm_property_replace_global_blob(dev,
  787. &connector->path_blob_ptr,
  788. strlen(path) + 1,
  789. path,
  790. &connector->base,
  791. dev->mode_config.path_property);
  792. return ret;
  793. }
  794. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  795. /**
  796. * drm_mode_connector_set_tile_property - set tile property on connector
  797. * @connector: connector to set property on.
  798. *
  799. * This looks up the tile information for a connector, and creates a
  800. * property for userspace to parse if it exists. The property is of
  801. * the form of 8 integers using ':' as a separator.
  802. *
  803. * Returns:
  804. * Zero on success, errno on failure.
  805. */
  806. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  807. {
  808. struct drm_device *dev = connector->dev;
  809. char tile[256];
  810. int ret;
  811. if (!connector->has_tile) {
  812. ret = drm_property_replace_global_blob(dev,
  813. &connector->tile_blob_ptr,
  814. 0,
  815. NULL,
  816. &connector->base,
  817. dev->mode_config.tile_property);
  818. return ret;
  819. }
  820. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  821. connector->tile_group->id, connector->tile_is_single_monitor,
  822. connector->num_h_tile, connector->num_v_tile,
  823. connector->tile_h_loc, connector->tile_v_loc,
  824. connector->tile_h_size, connector->tile_v_size);
  825. ret = drm_property_replace_global_blob(dev,
  826. &connector->tile_blob_ptr,
  827. strlen(tile) + 1,
  828. tile,
  829. &connector->base,
  830. dev->mode_config.tile_property);
  831. return ret;
  832. }
  833. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  834. /**
  835. * drm_mode_connector_update_edid_property - update the edid property of a connector
  836. * @connector: drm connector
  837. * @edid: new value of the edid property
  838. *
  839. * This function creates a new blob modeset object and assigns its id to the
  840. * connector's edid property.
  841. *
  842. * Returns:
  843. * Zero on success, negative errno on failure.
  844. */
  845. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  846. const struct edid *edid)
  847. {
  848. struct drm_device *dev = connector->dev;
  849. size_t size = 0;
  850. int ret;
  851. /* ignore requests to set edid when overridden */
  852. if (connector->override_edid)
  853. return 0;
  854. if (edid)
  855. size = EDID_LENGTH * (1 + edid->extensions);
  856. ret = drm_property_replace_global_blob(dev,
  857. &connector->edid_blob_ptr,
  858. size,
  859. edid,
  860. &connector->base,
  861. dev->mode_config.edid_property);
  862. return ret;
  863. }
  864. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  865. int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  866. struct drm_property *property,
  867. uint64_t value)
  868. {
  869. int ret = -EINVAL;
  870. struct drm_connector *connector = obj_to_connector(obj);
  871. /* Do DPMS ourselves */
  872. if (property == connector->dev->mode_config.dpms_property) {
  873. ret = (*connector->funcs->dpms)(connector, (int)value);
  874. } else if (connector->funcs->set_property)
  875. ret = connector->funcs->set_property(connector, property, value);
  876. /* store the property value if successful */
  877. if (!ret)
  878. drm_object_property_set_value(&connector->base, property, value);
  879. return ret;
  880. }
  881. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  882. void *data, struct drm_file *file_priv)
  883. {
  884. struct drm_mode_connector_set_property *conn_set_prop = data;
  885. struct drm_mode_obj_set_property obj_set_prop = {
  886. .value = conn_set_prop->value,
  887. .prop_id = conn_set_prop->prop_id,
  888. .obj_id = conn_set_prop->connector_id,
  889. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  890. };
  891. /* It does all the locking and checking we need */
  892. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  893. }
  894. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  895. {
  896. /* For atomic drivers only state objects are synchronously updated and
  897. * protected by modeset locks, so check those first. */
  898. if (connector->state)
  899. return connector->state->best_encoder;
  900. return connector->encoder;
  901. }
  902. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  903. const struct drm_file *file_priv)
  904. {
  905. /*
  906. * If user-space hasn't configured the driver to expose the stereo 3D
  907. * modes, don't expose them.
  908. */
  909. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  910. return false;
  911. return true;
  912. }
  913. int drm_mode_getconnector(struct drm_device *dev, void *data,
  914. struct drm_file *file_priv)
  915. {
  916. struct drm_mode_get_connector *out_resp = data;
  917. struct drm_connector *connector;
  918. struct drm_encoder *encoder;
  919. struct drm_display_mode *mode;
  920. int mode_count = 0;
  921. int encoders_count = 0;
  922. int ret = 0;
  923. int copied = 0;
  924. int i;
  925. struct drm_mode_modeinfo u_mode;
  926. struct drm_mode_modeinfo __user *mode_ptr;
  927. uint32_t __user *encoder_ptr;
  928. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  929. return -EINVAL;
  930. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  931. mutex_lock(&dev->mode_config.mutex);
  932. connector = drm_connector_lookup(dev, out_resp->connector_id);
  933. if (!connector) {
  934. ret = -ENOENT;
  935. goto out_unlock;
  936. }
  937. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  938. if (connector->encoder_ids[i] != 0)
  939. encoders_count++;
  940. if (out_resp->count_modes == 0) {
  941. connector->funcs->fill_modes(connector,
  942. dev->mode_config.max_width,
  943. dev->mode_config.max_height);
  944. }
  945. /* delayed so we get modes regardless of pre-fill_modes state */
  946. list_for_each_entry(mode, &connector->modes, head)
  947. if (drm_mode_expose_to_userspace(mode, file_priv))
  948. mode_count++;
  949. out_resp->connector_id = connector->base.id;
  950. out_resp->connector_type = connector->connector_type;
  951. out_resp->connector_type_id = connector->connector_type_id;
  952. out_resp->mm_width = connector->display_info.width_mm;
  953. out_resp->mm_height = connector->display_info.height_mm;
  954. out_resp->subpixel = connector->display_info.subpixel_order;
  955. out_resp->connection = connector->status;
  956. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  957. encoder = drm_connector_get_encoder(connector);
  958. if (encoder)
  959. out_resp->encoder_id = encoder->base.id;
  960. else
  961. out_resp->encoder_id = 0;
  962. /*
  963. * This ioctl is called twice, once to determine how much space is
  964. * needed, and the 2nd time to fill it.
  965. */
  966. if ((out_resp->count_modes >= mode_count) && mode_count) {
  967. copied = 0;
  968. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  969. list_for_each_entry(mode, &connector->modes, head) {
  970. if (!drm_mode_expose_to_userspace(mode, file_priv))
  971. continue;
  972. drm_mode_convert_to_umode(&u_mode, mode);
  973. if (copy_to_user(mode_ptr + copied,
  974. &u_mode, sizeof(u_mode))) {
  975. ret = -EFAULT;
  976. goto out;
  977. }
  978. copied++;
  979. }
  980. }
  981. out_resp->count_modes = mode_count;
  982. ret = drm_mode_object_get_properties(&connector->base, file_priv->atomic,
  983. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  984. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  985. &out_resp->count_props);
  986. if (ret)
  987. goto out;
  988. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  989. copied = 0;
  990. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  991. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  992. if (connector->encoder_ids[i] != 0) {
  993. if (put_user(connector->encoder_ids[i],
  994. encoder_ptr + copied)) {
  995. ret = -EFAULT;
  996. goto out;
  997. }
  998. copied++;
  999. }
  1000. }
  1001. }
  1002. out_resp->count_encoders = encoders_count;
  1003. out:
  1004. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1005. drm_connector_unreference(connector);
  1006. out_unlock:
  1007. mutex_unlock(&dev->mode_config.mutex);
  1008. return ret;
  1009. }
  1010. /**
  1011. * DOC: Tile group
  1012. *
  1013. * Tile groups are used to represent tiled monitors with a unique integer
  1014. * identifier. Tiled monitors using DisplayID v1.3 have a unique 8-byte handle,
  1015. * we store this in a tile group, so we have a common identifier for all tiles
  1016. * in a monitor group. The property is called "TILE". Drivers can manage tile
  1017. * groups using drm_mode_create_tile_group(), drm_mode_put_tile_group() and
  1018. * drm_mode_get_tile_group(). But this is only needed for internal panels where
  1019. * the tile group information is exposed through a non-standard way.
  1020. */
  1021. static void drm_tile_group_free(struct kref *kref)
  1022. {
  1023. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  1024. struct drm_device *dev = tg->dev;
  1025. mutex_lock(&dev->mode_config.idr_mutex);
  1026. idr_remove(&dev->mode_config.tile_idr, tg->id);
  1027. mutex_unlock(&dev->mode_config.idr_mutex);
  1028. kfree(tg);
  1029. }
  1030. /**
  1031. * drm_mode_put_tile_group - drop a reference to a tile group.
  1032. * @dev: DRM device
  1033. * @tg: tile group to drop reference to.
  1034. *
  1035. * drop reference to tile group and free if 0.
  1036. */
  1037. void drm_mode_put_tile_group(struct drm_device *dev,
  1038. struct drm_tile_group *tg)
  1039. {
  1040. kref_put(&tg->refcount, drm_tile_group_free);
  1041. }
  1042. EXPORT_SYMBOL(drm_mode_put_tile_group);
  1043. /**
  1044. * drm_mode_get_tile_group - get a reference to an existing tile group
  1045. * @dev: DRM device
  1046. * @topology: 8-bytes unique per monitor.
  1047. *
  1048. * Use the unique bytes to get a reference to an existing tile group.
  1049. *
  1050. * RETURNS:
  1051. * tile group or NULL if not found.
  1052. */
  1053. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  1054. char topology[8])
  1055. {
  1056. struct drm_tile_group *tg;
  1057. int id;
  1058. mutex_lock(&dev->mode_config.idr_mutex);
  1059. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  1060. if (!memcmp(tg->group_data, topology, 8)) {
  1061. if (!kref_get_unless_zero(&tg->refcount))
  1062. tg = NULL;
  1063. mutex_unlock(&dev->mode_config.idr_mutex);
  1064. return tg;
  1065. }
  1066. }
  1067. mutex_unlock(&dev->mode_config.idr_mutex);
  1068. return NULL;
  1069. }
  1070. EXPORT_SYMBOL(drm_mode_get_tile_group);
  1071. /**
  1072. * drm_mode_create_tile_group - create a tile group from a displayid description
  1073. * @dev: DRM device
  1074. * @topology: 8-bytes unique per monitor.
  1075. *
  1076. * Create a tile group for the unique monitor, and get a unique
  1077. * identifier for the tile group.
  1078. *
  1079. * RETURNS:
  1080. * new tile group or error.
  1081. */
  1082. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  1083. char topology[8])
  1084. {
  1085. struct drm_tile_group *tg;
  1086. int ret;
  1087. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  1088. if (!tg)
  1089. return ERR_PTR(-ENOMEM);
  1090. kref_init(&tg->refcount);
  1091. memcpy(tg->group_data, topology, 8);
  1092. tg->dev = dev;
  1093. mutex_lock(&dev->mode_config.idr_mutex);
  1094. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  1095. if (ret >= 0) {
  1096. tg->id = ret;
  1097. } else {
  1098. kfree(tg);
  1099. tg = ERR_PTR(ret);
  1100. }
  1101. mutex_unlock(&dev->mode_config.idr_mutex);
  1102. return tg;
  1103. }
  1104. EXPORT_SYMBOL(drm_mode_create_tile_group);