nouveau_backlight.c 8.8 KB

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