nouveau_backlight.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (C) 2009 Red Hat <mjg@redhat.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  20. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. /*
  26. * Authors:
  27. * Matthew Garrett <mjg@redhat.com>
  28. *
  29. * Register locations derived from NVClock by Roderick Colenbrander
  30. */
  31. #include <linux/apple-gmux.h>
  32. #include <linux/backlight.h>
  33. #include <linux/idr.h>
  34. #include <drm/drm_probe_helper.h>
  35. #include "nouveau_drv.h"
  36. #include "nouveau_reg.h"
  37. #include "nouveau_encoder.h"
  38. #include "nouveau_connector.h"
  39. #include "nouveau_acpi.h"
  40. static struct ida bl_ida;
  41. #define BL_NAME_SIZE 24 // 12 for name + 11 for digits + 1 for '\0'
  42. static bool
  43. nouveau_get_backlight_name(char backlight_name[BL_NAME_SIZE],
  44. struct nouveau_backlight *bl)
  45. {
  46. const int nb = ida_alloc_max(&bl_ida, 99, GFP_KERNEL);
  47. if (nb < 0)
  48. return false;
  49. if (nb > 0)
  50. snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
  51. else
  52. snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight");
  53. bl->id = nb;
  54. return true;
  55. }
  56. static int
  57. nv40_get_intensity(struct backlight_device *bd)
  58. {
  59. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  60. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  61. struct nvif_object *device = &drm->client.device.object;
  62. int val = (nvif_rd32(device, NV40_PMC_BACKLIGHT) &
  63. NV40_PMC_BACKLIGHT_MASK) >> 16;
  64. return val;
  65. }
  66. static int
  67. nv40_set_intensity(struct backlight_device *bd)
  68. {
  69. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  70. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  71. struct nvif_object *device = &drm->client.device.object;
  72. int val = bd->props.brightness;
  73. int reg = nvif_rd32(device, NV40_PMC_BACKLIGHT);
  74. nvif_wr32(device, NV40_PMC_BACKLIGHT,
  75. (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
  76. return 0;
  77. }
  78. static const struct backlight_ops nv40_bl_ops = {
  79. .options = BL_CORE_SUSPENDRESUME,
  80. .get_brightness = nv40_get_intensity,
  81. .update_status = nv40_set_intensity,
  82. };
  83. static int
  84. nv40_backlight_init(struct nouveau_encoder *encoder,
  85. struct backlight_properties *props,
  86. const struct backlight_ops **ops)
  87. {
  88. struct nouveau_drm *drm = nouveau_drm(encoder->base.base.dev);
  89. struct nvif_object *device = &drm->client.device.object;
  90. if (!(nvif_rd32(device, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
  91. return -ENODEV;
  92. props->max_brightness = 31;
  93. *ops = &nv40_bl_ops;
  94. return 0;
  95. }
  96. /*
  97. * eDP brightness callbacks need to happen under lock, since we need to
  98. * enable/disable the backlight ourselves for modesets
  99. */
  100. static int
  101. nv50_edp_get_brightness(struct backlight_device *bd)
  102. {
  103. struct drm_connector *connector = dev_get_drvdata(bd->dev.parent);
  104. struct drm_device *dev = connector->dev;
  105. struct drm_crtc *crtc;
  106. struct drm_modeset_acquire_ctx ctx;
  107. int ret = 0;
  108. drm_modeset_acquire_init(&ctx, 0);
  109. retry:
  110. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
  111. if (ret == -EDEADLK)
  112. goto deadlock;
  113. else if (ret < 0)
  114. goto out;
  115. crtc = connector->state->crtc;
  116. if (!crtc)
  117. goto out;
  118. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  119. if (ret == -EDEADLK)
  120. goto deadlock;
  121. else if (ret < 0)
  122. goto out;
  123. if (!crtc->state->active)
  124. goto out;
  125. ret = bd->props.brightness;
  126. out:
  127. drm_modeset_drop_locks(&ctx);
  128. drm_modeset_acquire_fini(&ctx);
  129. return ret;
  130. deadlock:
  131. drm_modeset_backoff(&ctx);
  132. goto retry;
  133. }
  134. static int
  135. nv50_edp_set_brightness(struct backlight_device *bd)
  136. {
  137. struct drm_connector *connector = dev_get_drvdata(bd->dev.parent);
  138. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  139. struct drm_device *dev = connector->dev;
  140. struct drm_crtc *crtc;
  141. struct drm_dp_aux *aux = &nv_connector->aux;
  142. struct nouveau_backlight *nv_bl = nv_connector->backlight;
  143. struct drm_modeset_acquire_ctx ctx;
  144. int ret = 0;
  145. drm_modeset_acquire_init(&ctx, 0);
  146. retry:
  147. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
  148. if (ret == -EDEADLK)
  149. goto deadlock;
  150. else if (ret < 0)
  151. goto out;
  152. crtc = connector->state->crtc;
  153. if (!crtc)
  154. goto out;
  155. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  156. if (ret == -EDEADLK)
  157. goto deadlock;
  158. else if (ret < 0)
  159. goto out;
  160. if (crtc->state->active)
  161. ret = drm_edp_backlight_set_level(aux, &nv_bl->edp_info, bd->props.brightness);
  162. out:
  163. drm_modeset_drop_locks(&ctx);
  164. drm_modeset_acquire_fini(&ctx);
  165. return ret;
  166. deadlock:
  167. drm_modeset_backoff(&ctx);
  168. goto retry;
  169. }
  170. static const struct backlight_ops nv50_edp_bl_ops = {
  171. .get_brightness = nv50_edp_get_brightness,
  172. .update_status = nv50_edp_set_brightness,
  173. };
  174. static int
  175. nv50_get_intensity(struct backlight_device *bd)
  176. {
  177. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  178. return nvif_outp_bl_get(&nv_encoder->outp);
  179. }
  180. static int
  181. nv50_set_intensity(struct backlight_device *bd)
  182. {
  183. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  184. return nvif_outp_bl_set(&nv_encoder->outp, backlight_get_brightness(bd));
  185. }
  186. static const struct backlight_ops nv50_bl_ops = {
  187. .options = BL_CORE_SUSPENDRESUME,
  188. .get_brightness = nv50_get_intensity,
  189. .update_status = nv50_set_intensity,
  190. };
  191. /* FIXME: perform backlight probing for eDP _before_ this, this only gets called after connector
  192. * registration which happens after the initial modeset
  193. */
  194. static int
  195. nv50_backlight_init(struct nouveau_backlight *bl,
  196. struct nouveau_connector *nv_conn,
  197. struct nouveau_encoder *nv_encoder,
  198. struct backlight_properties *props,
  199. const struct backlight_ops **ops)
  200. {
  201. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  202. /*
  203. * Note when this runs the connectors have not been probed yet,
  204. * so nv_conn->base.status is not set yet.
  205. */
  206. if (nvif_outp_bl_get(&nv_encoder->outp) < 0 ||
  207. drm_helper_probe_detect(&nv_conn->base, NULL, false) != connector_status_connected)
  208. return -ENODEV;
  209. if (nv_conn->type == DCB_CONNECTOR_eDP) {
  210. int ret;
  211. u32 current_level;
  212. u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];
  213. u8 current_mode;
  214. ret = drm_dp_dpcd_read(&nv_conn->aux, DP_EDP_DPCD_REV, edp_dpcd,
  215. EDP_DISPLAY_CTL_CAP_SIZE);
  216. if (ret < 0)
  217. return ret;
  218. /* TODO: Add support for hybrid PWM/DPCD panels */
  219. if (drm_edp_backlight_supported(edp_dpcd) &&
  220. (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP) &&
  221. (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP)) {
  222. NV_DEBUG(drm, "DPCD backlight controls supported on %s\n",
  223. nv_conn->base.name);
  224. ret = drm_edp_backlight_init(&nv_conn->aux, &bl->edp_info,
  225. 0, 0, edp_dpcd,
  226. &current_level, &current_mode, false);
  227. if (ret < 0)
  228. return ret;
  229. ret = drm_edp_backlight_enable(&nv_conn->aux, &bl->edp_info, current_level);
  230. if (ret < 0) {
  231. NV_ERROR(drm, "Failed to enable backlight on %s: %d\n",
  232. nv_conn->base.name, ret);
  233. return ret;
  234. }
  235. *ops = &nv50_edp_bl_ops;
  236. props->brightness = current_level;
  237. props->max_brightness = bl->edp_info.max;
  238. bl->uses_dpcd = true;
  239. return 0;
  240. }
  241. }
  242. *ops = &nv50_bl_ops;
  243. props->max_brightness = 100;
  244. return 0;
  245. }
  246. int
  247. nouveau_backlight_init(struct drm_connector *connector)
  248. {
  249. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  250. struct nouveau_backlight *bl;
  251. struct nouveau_encoder *nv_encoder = NULL;
  252. struct nvif_device *device = &drm->client.device;
  253. char backlight_name[BL_NAME_SIZE];
  254. struct backlight_properties props = {0};
  255. const struct backlight_ops *ops;
  256. int ret;
  257. if (apple_gmux_present()) {
  258. NV_INFO_ONCE(drm, "Apple GMUX detected: not registering Nouveau backlight interface\n");
  259. return 0;
  260. }
  261. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
  262. nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
  263. else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  264. nv_encoder = find_encoder(connector, DCB_OUTPUT_DP);
  265. else
  266. return 0;
  267. if (!nv_encoder)
  268. return 0;
  269. bl = kzalloc_obj(*bl);
  270. if (!bl)
  271. return -ENOMEM;
  272. switch (device->info.family) {
  273. case NV_DEVICE_INFO_V0_CURIE:
  274. ret = nv40_backlight_init(nv_encoder, &props, &ops);
  275. break;
  276. case NV_DEVICE_INFO_V0_TESLA:
  277. case NV_DEVICE_INFO_V0_FERMI:
  278. case NV_DEVICE_INFO_V0_KEPLER:
  279. case NV_DEVICE_INFO_V0_MAXWELL:
  280. case NV_DEVICE_INFO_V0_PASCAL:
  281. case NV_DEVICE_INFO_V0_VOLTA:
  282. case NV_DEVICE_INFO_V0_TURING:
  283. case NV_DEVICE_INFO_V0_AMPERE: //XXX: not confirmed
  284. ret = nv50_backlight_init(bl, nouveau_connector(connector),
  285. nv_encoder, &props, &ops);
  286. break;
  287. default:
  288. ret = 0;
  289. goto fail_alloc;
  290. }
  291. if (ret) {
  292. if (ret == -ENODEV)
  293. ret = 0;
  294. goto fail_alloc;
  295. }
  296. if (!nouveau_acpi_video_backlight_use_native()) {
  297. NV_INFO(drm, "Skipping nv_backlight registration\n");
  298. goto fail_alloc;
  299. }
  300. if (!nouveau_get_backlight_name(backlight_name, bl)) {
  301. NV_ERROR(drm, "Failed to retrieve a unique name for the backlight interface\n");
  302. goto fail_alloc;
  303. }
  304. props.type = BACKLIGHT_RAW;
  305. bl->dev = backlight_device_register(backlight_name, connector->kdev,
  306. nv_encoder, ops, &props);
  307. if (IS_ERR(bl->dev)) {
  308. if (bl->id >= 0)
  309. ida_free(&bl_ida, bl->id);
  310. ret = PTR_ERR(bl->dev);
  311. goto fail_alloc;
  312. }
  313. nouveau_connector(connector)->backlight = bl;
  314. if (!bl->dev->props.brightness)
  315. bl->dev->props.brightness =
  316. bl->dev->ops->get_brightness(bl->dev);
  317. backlight_update_status(bl->dev);
  318. return 0;
  319. fail_alloc:
  320. kfree(bl);
  321. /*
  322. * If we get here we have an internal panel, but no nv_backlight,
  323. * try registering an ACPI video backlight device instead.
  324. */
  325. if (ret == 0)
  326. nouveau_acpi_video_register_backlight();
  327. return ret;
  328. }
  329. void
  330. nouveau_backlight_fini(struct drm_connector *connector)
  331. {
  332. struct nouveau_connector *nv_conn = nouveau_connector(connector);
  333. struct nouveau_backlight *bl = nv_conn->backlight;
  334. if (!bl)
  335. return;
  336. if (bl->id >= 0)
  337. ida_free(&bl_ida, bl->id);
  338. backlight_device_unregister(bl->dev);
  339. nv_conn->backlight = NULL;
  340. kfree(bl);
  341. }
  342. void
  343. nouveau_backlight_ctor(void)
  344. {
  345. ida_init(&bl_ida);
  346. }
  347. void
  348. nouveau_backlight_dtor(void)
  349. {
  350. ida_destroy(&bl_ida);
  351. }