nouveau_backlight.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "nouveau_drv.h"
  35. #include "nouveau_reg.h"
  36. #include "nouveau_encoder.h"
  37. #include "nouveau_connector.h"
  38. static struct ida bl_ida;
  39. #define BL_NAME_SIZE 15 // 12 for name + 2 for digits + 1 for '\0'
  40. struct nouveau_backlight {
  41. struct backlight_device *dev;
  42. int id;
  43. };
  44. static bool
  45. nouveau_get_backlight_name(char backlight_name[BL_NAME_SIZE],
  46. struct nouveau_backlight *bl)
  47. {
  48. const int nb = ida_simple_get(&bl_ida, 0, 0, GFP_KERNEL);
  49. if (nb < 0 || nb >= 100)
  50. return false;
  51. if (nb > 0)
  52. snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
  53. else
  54. snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight");
  55. bl->id = nb;
  56. return true;
  57. }
  58. static int
  59. nv40_get_intensity(struct backlight_device *bd)
  60. {
  61. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  62. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  63. struct nvif_object *device = &drm->client.device.object;
  64. int val = (nvif_rd32(device, NV40_PMC_BACKLIGHT) &
  65. NV40_PMC_BACKLIGHT_MASK) >> 16;
  66. return val;
  67. }
  68. static int
  69. nv40_set_intensity(struct backlight_device *bd)
  70. {
  71. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  72. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  73. struct nvif_object *device = &drm->client.device.object;
  74. int val = bd->props.brightness;
  75. int reg = nvif_rd32(device, NV40_PMC_BACKLIGHT);
  76. nvif_wr32(device, NV40_PMC_BACKLIGHT,
  77. (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
  78. return 0;
  79. }
  80. static const struct backlight_ops nv40_bl_ops = {
  81. .options = BL_CORE_SUSPENDRESUME,
  82. .get_brightness = nv40_get_intensity,
  83. .update_status = nv40_set_intensity,
  84. };
  85. static int
  86. nv40_backlight_init(struct nouveau_encoder *encoder,
  87. struct backlight_properties *props,
  88. const struct backlight_ops **ops)
  89. {
  90. struct nouveau_drm *drm = nouveau_drm(encoder->base.base.dev);
  91. struct nvif_object *device = &drm->client.device.object;
  92. if (!(nvif_rd32(device, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
  93. return -ENODEV;
  94. props->type = BACKLIGHT_RAW;
  95. props->max_brightness = 31;
  96. *ops = &nv40_bl_ops;
  97. return 0;
  98. }
  99. static int
  100. nv50_get_intensity(struct backlight_device *bd)
  101. {
  102. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  103. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  104. struct nvif_object *device = &drm->client.device.object;
  105. int or = ffs(nv_encoder->dcb->or) - 1;
  106. u32 div = 1025;
  107. u32 val;
  108. val = nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
  109. val &= NV50_PDISP_SOR_PWM_CTL_VAL;
  110. return ((val * 100) + (div / 2)) / div;
  111. }
  112. static int
  113. nv50_set_intensity(struct backlight_device *bd)
  114. {
  115. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  116. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  117. struct nvif_object *device = &drm->client.device.object;
  118. int or = ffs(nv_encoder->dcb->or) - 1;
  119. u32 div = 1025;
  120. u32 val = (bd->props.brightness * div) / 100;
  121. nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or),
  122. NV50_PDISP_SOR_PWM_CTL_NEW | val);
  123. return 0;
  124. }
  125. static const struct backlight_ops nv50_bl_ops = {
  126. .options = BL_CORE_SUSPENDRESUME,
  127. .get_brightness = nv50_get_intensity,
  128. .update_status = nv50_set_intensity,
  129. };
  130. static int
  131. nva3_get_intensity(struct backlight_device *bd)
  132. {
  133. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  134. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  135. struct nvif_object *device = &drm->client.device.object;
  136. int or = ffs(nv_encoder->dcb->or) - 1;
  137. u32 div, val;
  138. div = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
  139. val = nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
  140. val &= NVA3_PDISP_SOR_PWM_CTL_VAL;
  141. if (div && div >= val)
  142. return ((val * 100) + (div / 2)) / div;
  143. return 100;
  144. }
  145. static int
  146. nva3_set_intensity(struct backlight_device *bd)
  147. {
  148. struct nouveau_encoder *nv_encoder = bl_get_data(bd);
  149. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  150. struct nvif_object *device = &drm->client.device.object;
  151. int or = ffs(nv_encoder->dcb->or) - 1;
  152. u32 div, val;
  153. div = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
  154. val = (bd->props.brightness * div) / 100;
  155. if (div) {
  156. nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or),
  157. val |
  158. NV50_PDISP_SOR_PWM_CTL_NEW |
  159. NVA3_PDISP_SOR_PWM_CTL_UNK);
  160. return 0;
  161. }
  162. return -EINVAL;
  163. }
  164. static const struct backlight_ops nva3_bl_ops = {
  165. .options = BL_CORE_SUSPENDRESUME,
  166. .get_brightness = nva3_get_intensity,
  167. .update_status = nva3_set_intensity,
  168. };
  169. static int
  170. nv50_backlight_init(struct nouveau_encoder *nv_encoder,
  171. struct backlight_properties *props,
  172. const struct backlight_ops **ops)
  173. {
  174. struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
  175. struct nvif_object *device = &drm->client.device.object;
  176. if (!nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(ffs(nv_encoder->dcb->or) - 1)))
  177. return -ENODEV;
  178. if (drm->client.device.info.chipset <= 0xa0 ||
  179. drm->client.device.info.chipset == 0xaa ||
  180. drm->client.device.info.chipset == 0xac)
  181. *ops = &nv50_bl_ops;
  182. else
  183. *ops = &nva3_bl_ops;
  184. props->type = BACKLIGHT_RAW;
  185. props->max_brightness = 100;
  186. return 0;
  187. }
  188. int
  189. nouveau_backlight_init(struct drm_connector *connector)
  190. {
  191. struct nouveau_drm *drm = nouveau_drm(connector->dev);
  192. struct nouveau_backlight *bl;
  193. struct nouveau_encoder *nv_encoder = NULL;
  194. struct nvif_device *device = &drm->client.device;
  195. char backlight_name[BL_NAME_SIZE];
  196. struct backlight_properties props = {0};
  197. const struct backlight_ops *ops;
  198. int ret;
  199. if (apple_gmux_present()) {
  200. NV_INFO_ONCE(drm, "Apple GMUX detected: not registering Nouveau backlight interface\n");
  201. return 0;
  202. }
  203. if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
  204. nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
  205. else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
  206. nv_encoder = find_encoder(connector, DCB_OUTPUT_DP);
  207. else
  208. return 0;
  209. if (!nv_encoder)
  210. return 0;
  211. switch (device->info.family) {
  212. case NV_DEVICE_INFO_V0_CURIE:
  213. ret = nv40_backlight_init(nv_encoder, &props, &ops);
  214. break;
  215. case NV_DEVICE_INFO_V0_TESLA:
  216. case NV_DEVICE_INFO_V0_FERMI:
  217. case NV_DEVICE_INFO_V0_KEPLER:
  218. case NV_DEVICE_INFO_V0_MAXWELL:
  219. case NV_DEVICE_INFO_V0_PASCAL:
  220. case NV_DEVICE_INFO_V0_VOLTA:
  221. case NV_DEVICE_INFO_V0_TURING:
  222. ret = nv50_backlight_init(nv_encoder, &props, &ops);
  223. break;
  224. default:
  225. return 0;
  226. }
  227. if (ret == -ENODEV)
  228. return 0;
  229. else if (ret)
  230. return ret;
  231. bl = kzalloc(sizeof(*bl), GFP_KERNEL);
  232. if (!bl)
  233. return -ENOMEM;
  234. if (!nouveau_get_backlight_name(backlight_name, bl)) {
  235. NV_ERROR(drm, "Failed to retrieve a unique name for the backlight interface\n");
  236. goto fail_alloc;
  237. }
  238. bl->dev = backlight_device_register(backlight_name, connector->kdev,
  239. nv_encoder, ops, &props);
  240. if (IS_ERR(bl->dev)) {
  241. if (bl->id >= 0)
  242. ida_simple_remove(&bl_ida, bl->id);
  243. ret = PTR_ERR(bl->dev);
  244. goto fail_alloc;
  245. }
  246. nouveau_connector(connector)->backlight = bl;
  247. bl->dev->props.brightness = bl->dev->ops->get_brightness(bl->dev);
  248. backlight_update_status(bl->dev);
  249. return 0;
  250. fail_alloc:
  251. kfree(bl);
  252. return ret;
  253. }
  254. void
  255. nouveau_backlight_fini(struct drm_connector *connector)
  256. {
  257. struct nouveau_connector *nv_conn = nouveau_connector(connector);
  258. struct nouveau_backlight *bl = nv_conn->backlight;
  259. if (!bl)
  260. return;
  261. if (bl->id >= 0)
  262. ida_simple_remove(&bl_ida, bl->id);
  263. backlight_device_unregister(bl->dev);
  264. nv_conn->backlight = NULL;
  265. kfree(bl);
  266. }
  267. void
  268. nouveau_backlight_ctor(void)
  269. {
  270. ida_init(&bl_ida);
  271. }
  272. void
  273. nouveau_backlight_dtor(void)
  274. {
  275. ida_destroy(&bl_ida);
  276. }