nouveau_backlight.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (C) 2009 Red Hat <[email protected]>
  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 <[email protected]>
  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 "nouveau_drv.h"
  35. #include "nouveau_reg.h"
  36. #include "nouveau_encoder.h"
  37. #include "nouveau_connector.h"
  38. #include "nouveau_acpi.h"
  39. static struct ida bl_ida;
  40. #define BL_NAME_SIZE 15 // 12 for name + 2 for digits + 1 for '\0'
  41. static bool
  42. nouveau_get_backlight_name(char backlight_name[BL_NAME_SIZE],
  43. struct nouveau_backlight *bl)
  44. {
  45. const int nb = ida_alloc_max(&bl_ida, 99, GFP_KERNEL);
  46. if (nb < 0)
  47. return false;
  48. if (nb > 0)
  49. snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
  50. else
  51. snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight");
  52. bl->id = nb;
  53. return true;
  54. }
  55. static int
  56. nv40_get_intensity(struct backlight_device *bd)
  57. {
  58. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  59. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  60. struct nvif_object *device = &drm->client.device.object;
  61. int val = (nvif_rd32(device, NV40_PMC_BACKLIGHT) &
  62. NV40_PMC_BACKLIGHT_MASK) >> 16;
  63. return val;
  64. }
  65. static int
  66. nv40_set_intensity(struct backlight_device *bd)
  67. {
  68. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  69. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  70. struct nvif_object *device = &drm->client.device.object;
  71. int val = bd->props.brightness;
  72. int reg = nvif_rd32(device, NV40_PMC_BACKLIGHT);
  73. nvif_wr32(device, NV40_PMC_BACKLIGHT,
  74. (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
  75. return 0;
  76. }
  77. static const struct backlight_ops nv40_bl_ops = {
  78. .options = BL_CORE_SUSPENDRESUME,
  79. .get_brightness = nv40_get_intensity,
  80. .update_status = nv40_set_intensity,
  81. };
  82. static int
  83. nv40_backlight_init(struct nouveau_encoder *encoder,
  84. struct backlight_properties *props,
  85. const struct backlight_ops **ops)
  86. {
  87. struct nouveau_drm *drm = nouveau_drm(encoder->base.base.dev);
  88. struct nvif_object *device = &drm->client.device.object;
  89. if (!(nvif_rd32(device, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
  90. return -ENODEV;
  91. props->max_brightness = 31;
  92. *ops = &nv40_bl_ops;
  93. return 0;
  94. }
  95. static int
  96. nv50_get_intensity(struct backlight_device *bd)
  97. {
  98. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  99. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  100. struct nvif_object *device = &drm->client.device.object;
  101. int or = ffs(nv_encoder->dcb->or) - 1;
  102. u32 div = 1025;
  103. u32 val;
  104. val = nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
  105. val &= NV50_PDISP_SOR_PWM_CTL_VAL;
  106. return ((val * 100) + (div / 2)) / div;
  107. }
  108. static int
  109. nv50_set_intensity(struct backlight_device *bd)
  110. {
  111. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  112. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  113. struct nvif_object *device = &drm->client.device.object;
  114. int or = ffs(nv_encoder->dcb->or) - 1;
  115. u32 div = 1025;
  116. u32 val = (bd->props.brightness * div) / 100;
  117. nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or),
  118. NV50_PDISP_SOR_PWM_CTL_NEW | val);
  119. return 0;
  120. }
  121. static const struct backlight_ops nv50_bl_ops = {
  122. .options = BL_CORE_SUSPENDRESUME,
  123. .get_brightness = nv50_get_intensity,
  124. .update_status = nv50_set_intensity,
  125. };
  126. /*
  127. * eDP brightness callbacks need to happen under lock, since we need to
  128. * enable/disable the backlight ourselves for modesets
  129. */
  130. static int
  131. nv50_edp_get_brightness(struct backlight_device *bd)
  132. {
  133. struct drm_connector *connector = dev_get_drvdata(bd->dev.parent);
  134. struct drm_device *dev = connector->dev;
  135. struct drm_crtc *crtc;
  136. struct drm_modeset_acquire_ctx ctx;
  137. int ret = 0;
  138. drm_modeset_acquire_init(&ctx, 0);
  139. retry:
  140. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
  141. if (ret == -EDEADLK)
  142. goto deadlock;
  143. else if (ret < 0)
  144. goto out;
  145. crtc = connector->state->crtc;
  146. if (!crtc)
  147. goto out;
  148. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  149. if (ret == -EDEADLK)
  150. goto deadlock;
  151. else if (ret < 0)
  152. goto out;
  153. if (!crtc->state->active)
  154. goto out;
  155. ret = bd->props.brightness;
  156. out:
  157. drm_modeset_drop_locks(&ctx);
  158. drm_modeset_acquire_fini(&ctx);
  159. return ret;
  160. deadlock:
  161. drm_modeset_backoff(&ctx);
  162. goto retry;
  163. }
  164. static int
  165. nv50_edp_set_brightness(struct backlight_device *bd)
  166. {
  167. struct drm_connector *connector = dev_get_drvdata(bd->dev.parent);
  168. struct nouveau_connector *nv_connector = nouveau_connector(connector);
  169. struct drm_device *dev = connector->dev;
  170. struct drm_crtc *crtc;
  171. struct drm_dp_aux *aux = &nv_connector->aux;
  172. struct nouveau_backlight *nv_bl = nv_connector->backlight;
  173. struct drm_modeset_acquire_ctx ctx;
  174. int ret = 0;
  175. drm_modeset_acquire_init(&ctx, 0);
  176. retry:
  177. ret = drm_modeset_lock(&dev->mode_config.connection_mutex, &ctx);
  178. if (ret == -EDEADLK)
  179. goto deadlock;
  180. else if (ret < 0)
  181. goto out;
  182. crtc = connector->state->crtc;
  183. if (!crtc)
  184. goto out;
  185. ret = drm_modeset_lock(&crtc->mutex, &ctx);
  186. if (ret == -EDEADLK)
  187. goto deadlock;
  188. else if (ret < 0)
  189. goto out;
  190. if (crtc->state->active)
  191. ret = drm_edp_backlight_set_level(aux, &nv_bl->edp_info, bd->props.brightness);
  192. out:
  193. drm_modeset_drop_locks(&ctx);
  194. drm_modeset_acquire_fini(&ctx);
  195. return ret;
  196. deadlock:
  197. drm_modeset_backoff(&ctx);
  198. goto retry;
  199. }
  200. static const struct backlight_ops nv50_edp_bl_ops = {
  201. .get_brightness = nv50_edp_get_brightness,
  202. .update_status = nv50_edp_set_brightness,
  203. };
  204. static int
  205. nva3_get_intensity(struct backlight_device *bd)
  206. {
  207. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  208. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  209. struct nvif_object *device = &drm->client.device.object;
  210. int or = ffs(nv_encoder->dcb->or) - 1;
  211. u32 div, val;
  212. div = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
  213. val = nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
  214. val &= NVA3_PDISP_SOR_PWM_CTL_VAL;
  215. if (div && div >= val)
  216. return ((val * 100) + (div / 2)) / div;
  217. return 100;
  218. }
  219. static int
  220. nva3_set_intensity(struct backlight_device *bd)
  221. {
  222. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  223. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  224. struct nvif_object *device = &drm->client.device.object;
  225. int or = ffs(nv_encoder->dcb->or) - 1;
  226. u32 div, val;
  227. div = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
  228. val = (bd->props.brightness * div) / 100;
  229. if (div) {
  230. nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or),
  231. val |
  232. NV50_PDISP_SOR_PWM_CTL_NEW |
  233. NVA3_PDISP_SOR_PWM_CTL_UNK);
  234. return 0;
  235. }
  236. return -EINVAL;
  237. }
  238. static const struct backlight_ops nva3_bl_ops = {
  239. .options = BL_CORE_SUSPENDRESUME,
  240. .get_brightness = nva3_get_intensity,
  241. .update_status = nva3_set_intensity,
  242. };
  243. /* FIXME: perform backlight probing for eDP _before_ this, this only gets called after connector
  244. * registration which happens after the initial modeset
  245. */
  246. static int
  247. nv50_backlight_init(struct nouveau_backlight *bl,
  248. struct nouveau_connector *nv_conn,
  249. struct nouveau_encoder *nv_encoder,
  250. struct backlight_properties *props,
  251. const struct backlight_ops **ops)
  252. {
  253. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  254. struct nvif_object *device = &drm->client.device.object;
  255. if (!nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(ffs(nv_encoder->dcb->or) - 1)) ||
  256. nv_conn->base.status != connector_status_connected)
  257. return -ENODEV;
  258. if (nv_conn->type == DCB_CONNECTOR_eDP) {
  259. int ret;
  260. u16 current_level;
  261. u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];
  262. u8 current_mode;
  263. ret = drm_dp_dpcd_read(&nv_conn->aux, DP_EDP_DPCD_REV, edp_dpcd,
  264. EDP_DISPLAY_CTL_CAP_SIZE);
  265. if (ret < 0)
  266. return ret;
  267. /* TODO: Add support for hybrid PWM/DPCD panels */
  268. if (drm_edp_backlight_supported(edp_dpcd) &&
  269. (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP) &&
  270. (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP)) {
  271. NV_DEBUG(drm, "DPCD backlight controls supported on %s\n",
  272. nv_conn->base.name);
  273. ret = drm_edp_backlight_init(&nv_conn->aux, &bl->edp_info, 0, edp_dpcd,
  274. &current_level, &current_mode);
  275. if (ret < 0)
  276. return ret;
  277. ret = drm_edp_backlight_enable(&nv_conn->aux, &bl->edp_info, current_level);
  278. if (ret < 0) {
  279. NV_ERROR(drm, "Failed to enable backlight on %s: %d\n",
  280. nv_conn->base.name, ret);
  281. return ret;
  282. }
  283. *ops = &nv50_edp_bl_ops;
  284. props->brightness = current_level;
  285. props->max_brightness = bl->edp_info.max;
  286. bl->uses_dpcd = true;
  287. return 0;
  288. }
  289. }
  290. if (drm->client.device.info.chipset <= 0xa0 ||
  291. drm->client.device.info.chipset == 0xaa ||
  292. drm->client.device.info.chipset == 0xac)
  293. *ops = &nv50_bl_ops;
  294. else
  295. *ops = &nva3_bl_ops;
  296. props->max_brightness = 100;
  297. return 0;
  298. }
  299. int
  300. nouveau_backlight_init(struct drm_connector *connector)
  301. {
  302. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  303. struct nouveau_backlight *bl;
  304. struct nouveau_encoder *nv_encoder = NULL;
  305. struct nvif_device *device = &drm->client.device;
  306. char backlight_name[BL_NAME_SIZE];
  307. struct backlight_properties props = {0};
  308. const struct backlight_ops *ops;
  309. int ret;
  310. if (apple_gmux_present()) {
  311. NV_INFO_ONCE(drm, "Apple GMUX detected: not registering Nouveau backlight interface\n");
  312. return 0;
  313. }
  314. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
  315. nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
  316. else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  317. nv_encoder = find_encoder(connector, DCB_OUTPUT_DP);
  318. else
  319. return 0;
  320. if (!nv_encoder)
  321. return 0;
  322. bl = kzalloc(sizeof(*bl), GFP_KERNEL);
  323. if (!bl)
  324. return -ENOMEM;
  325. switch (device->info.family) {
  326. case NV_DEVICE_INFO_V0_CURIE:
  327. ret = nv40_backlight_init(nv_encoder, &props, &ops);
  328. break;
  329. case NV_DEVICE_INFO_V0_TESLA:
  330. case NV_DEVICE_INFO_V0_FERMI:
  331. case NV_DEVICE_INFO_V0_KEPLER:
  332. case NV_DEVICE_INFO_V0_MAXWELL:
  333. case NV_DEVICE_INFO_V0_PASCAL:
  334. case NV_DEVICE_INFO_V0_VOLTA:
  335. case NV_DEVICE_INFO_V0_TURING:
  336. case NV_DEVICE_INFO_V0_AMPERE: //XXX: not confirmed
  337. ret = nv50_backlight_init(bl, nouveau_connector(connector),
  338. nv_encoder, &props, &ops);
  339. break;
  340. default:
  341. ret = 0;
  342. goto fail_alloc;
  343. }
  344. if (ret) {
  345. if (ret == -ENODEV)
  346. ret = 0;
  347. goto fail_alloc;
  348. }
  349. if (!nouveau_acpi_video_backlight_use_native()) {
  350. NV_INFO(drm, "Skipping nv_backlight registration\n");
  351. goto fail_alloc;
  352. }
  353. if (!nouveau_get_backlight_name(backlight_name, bl)) {
  354. NV_ERROR(drm, "Failed to retrieve a unique name for the backlight interface\n");
  355. goto fail_alloc;
  356. }
  357. props.type = BACKLIGHT_RAW;
  358. bl->dev = backlight_device_register(backlight_name, connector->kdev,
  359. nv_encoder, ops, &props);
  360. if (IS_ERR(bl->dev)) {
  361. if (bl->id >= 0)
  362. ida_free(&bl_ida, bl->id);
  363. ret = PTR_ERR(bl->dev);
  364. goto fail_alloc;
  365. }
  366. nouveau_connector(connector)->backlight = bl;
  367. if (!bl->dev->props.brightness)
  368. bl->dev->props.brightness =
  369. bl->dev->ops->get_brightness(bl->dev);
  370. backlight_update_status(bl->dev);
  371. return 0;
  372. fail_alloc:
  373. kfree(bl);
  374. /*
  375. * If we get here we have an internal panel, but no nv_backlight,
  376. * try registering an ACPI video backlight device instead.
  377. */
  378. if (ret == 0)
  379. nouveau_acpi_video_register_backlight();
  380. return ret;
  381. }
  382. void
  383. nouveau_backlight_fini(struct drm_connector *connector)
  384. {
  385. struct nouveau_connector *nv_conn = nouveau_connector(connector);
  386. struct nouveau_backlight *bl = nv_conn->backlight;
  387. if (!bl)
  388. return;
  389. if (bl->id >= 0)
  390. ida_free(&bl_ida, bl->id);
  391. backlight_device_unregister(bl->dev);
  392. nv_conn->backlight = NULL;
  393. kfree(bl);
  394. }
  395. void
  396. nouveau_backlight_ctor(void)
  397. {
  398. ida_init(&bl_ida);
  399. }
  400. void
  401. nouveau_backlight_dtor(void)
  402. {
  403. ida_destroy(&bl_ida);
  404. }