Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 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 | /* |
| 27 | * Authors: |
| 28 | * Matthew Garrett <mjg@redhat.com> |
| 29 | * |
| 30 | * Register locations derived from NVClock by Roderick Colenbrander |
| 31 | */ |
| 32 | |
| 33 | #include <linux/backlight.h> |
Ben Skeggs | 5bead79 | 2010-11-04 09:56:12 +1000 | [diff] [blame] | 34 | #include <linux/acpi.h> |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 35 | |
| 36 | #include "drmP.h" |
| 37 | #include "nouveau_drv.h" |
| 38 | #include "nouveau_drm.h" |
| 39 | #include "nouveau_reg.h" |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 40 | #include "nouveau_encoder.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 41 | |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 42 | static int |
| 43 | nv40_get_intensity(struct backlight_device *bd) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 44 | { |
| 45 | struct drm_device *dev = bl_get_data(bd); |
| 46 | int val = (nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK) |
| 47 | >> 16; |
| 48 | |
| 49 | return val; |
| 50 | } |
| 51 | |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 52 | static int |
| 53 | nv40_set_intensity(struct backlight_device *bd) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 54 | { |
| 55 | struct drm_device *dev = bl_get_data(bd); |
| 56 | int val = bd->props.brightness; |
| 57 | int reg = nv_rd32(dev, NV40_PMC_BACKLIGHT); |
| 58 | |
| 59 | nv_wr32(dev, NV40_PMC_BACKLIGHT, |
| 60 | (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK)); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 65 | static const struct backlight_ops nv40_bl_ops = { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 66 | .options = BL_CORE_SUSPENDRESUME, |
| 67 | .get_brightness = nv40_get_intensity, |
| 68 | .update_status = nv40_set_intensity, |
| 69 | }; |
| 70 | |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 71 | static int |
| 72 | nv40_backlight_init(struct drm_connector *connector) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 73 | { |
Matthew Garrett | 7eae3ef | 2011-03-22 16:30:24 -0700 | [diff] [blame] | 74 | struct drm_device *dev = connector->dev; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 75 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
Matthew Garrett | 7eae3ef | 2011-03-22 16:30:24 -0700 | [diff] [blame] | 76 | struct backlight_properties props; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 77 | struct backlight_device *bd; |
| 78 | |
| 79 | if (!(nv_rd32(dev, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK)) |
| 80 | return 0; |
| 81 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 82 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 83 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 84 | props.max_brightness = 31; |
Matthew Garrett | 7eae3ef | 2011-03-22 16:30:24 -0700 | [diff] [blame] | 85 | bd = backlight_device_register("nv_backlight", &connector->kdev, dev, |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 86 | &nv40_bl_ops, &props); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 87 | if (IS_ERR(bd)) |
| 88 | return PTR_ERR(bd); |
| 89 | |
| 90 | dev_priv->backlight = bd; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 91 | bd->props.brightness = nv40_get_intensity(bd); |
| 92 | backlight_update_status(bd); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 97 | static int |
| 98 | nv50_get_intensity(struct backlight_device *bd) |
| 99 | { |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 100 | struct nouveau_encoder *nv_encoder = bl_get_data(bd); |
| 101 | struct drm_device *dev = nv_encoder->base.base.dev; |
| 102 | int or = nv_encoder->or; |
Ben Skeggs | 0946145 | 2011-08-02 20:45:35 +1000 | [diff] [blame^] | 103 | u32 div = 1025; |
| 104 | u32 val; |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 105 | |
Ben Skeggs | 0946145 | 2011-08-02 20:45:35 +1000 | [diff] [blame^] | 106 | val = nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT + (or * 0x800)); |
| 107 | return ((val * 100) + (div / 2)) / div; |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static int |
| 111 | nv50_set_intensity(struct backlight_device *bd) |
| 112 | { |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 113 | struct nouveau_encoder *nv_encoder = bl_get_data(bd); |
| 114 | struct drm_device *dev = nv_encoder->base.base.dev; |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 115 | int or = nv_encoder->or; |
Ben Skeggs | 0946145 | 2011-08-02 20:45:35 +1000 | [diff] [blame^] | 116 | u32 div = 1025; |
| 117 | u32 val = (bd->props.brightness * div) / 100; |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 118 | |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 119 | nv_wr32(dev, NV50_PDISPLAY_SOR_BACKLIGHT + (or * 0x800), |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 120 | val | NV50_PDISPLAY_SOR_BACKLIGHT_ENABLE); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static const struct backlight_ops nv50_bl_ops = { |
| 125 | .options = BL_CORE_SUSPENDRESUME, |
| 126 | .get_brightness = nv50_get_intensity, |
| 127 | .update_status = nv50_set_intensity, |
| 128 | }; |
| 129 | |
| 130 | static int |
| 131 | nv50_backlight_init(struct drm_connector *connector) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 132 | { |
Matthew Garrett | 7eae3ef | 2011-03-22 16:30:24 -0700 | [diff] [blame] | 133 | struct drm_device *dev = connector->dev; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 134 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 135 | struct nouveau_encoder *nv_encoder; |
Matthew Garrett | 7eae3ef | 2011-03-22 16:30:24 -0700 | [diff] [blame] | 136 | struct backlight_properties props; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 137 | struct backlight_device *bd; |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 138 | int or; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 139 | |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 140 | nv_encoder = find_encoder(connector, OUTPUT_LVDS); |
| 141 | if (!nv_encoder) { |
| 142 | nv_encoder = find_encoder(connector, OUTPUT_DP); |
| 143 | if (!nv_encoder) |
| 144 | return -ENODEV; |
| 145 | } |
| 146 | |
| 147 | or = nv_encoder->or; |
| 148 | |
| 149 | if (!nv_rd32(dev, NV50_PDISPLAY_SOR_BACKLIGHT + (or * 0x800))) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 150 | return 0; |
| 151 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 152 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 153 | props.type = BACKLIGHT_RAW; |
Ben Skeggs | 0946145 | 2011-08-02 20:45:35 +1000 | [diff] [blame^] | 154 | props.max_brightness = 100; |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 155 | bd = backlight_device_register("nv_backlight", &connector->kdev, |
| 156 | nv_encoder, &nv50_bl_ops, &props); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 157 | if (IS_ERR(bd)) |
| 158 | return PTR_ERR(bd); |
| 159 | |
| 160 | dev_priv->backlight = bd; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 161 | bd->props.brightness = nv50_get_intensity(bd); |
| 162 | backlight_update_status(bd); |
| 163 | return 0; |
| 164 | } |
| 165 | |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 166 | int |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 167 | nouveau_backlight_init(struct drm_device *dev) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 168 | { |
| 169 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 170 | struct drm_connector *connector; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 171 | |
Ben Skeggs | 5bead79 | 2010-11-04 09:56:12 +1000 | [diff] [blame] | 172 | #ifdef CONFIG_ACPI |
| 173 | if (acpi_video_backlight_support()) { |
| 174 | NV_INFO(dev, "ACPI backlight interface available, " |
| 175 | "not registering our own\n"); |
| 176 | return 0; |
| 177 | } |
| 178 | #endif |
| 179 | |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 180 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 181 | if (connector->connector_type != DRM_MODE_CONNECTOR_LVDS && |
| 182 | connector->connector_type != DRM_MODE_CONNECTOR_eDP) |
| 183 | continue; |
| 184 | |
| 185 | switch (dev_priv->card_type) { |
| 186 | case NV_40: |
| 187 | return nv40_backlight_init(connector); |
| 188 | case NV_50: |
| 189 | return nv50_backlight_init(connector); |
| 190 | default: |
| 191 | break; |
| 192 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 193 | } |
| 194 | |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 195 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Ben Skeggs | 7307648 | 2011-08-02 18:54:43 +1000 | [diff] [blame] | 199 | void |
Ben Skeggs | 10b461e | 2011-08-02 19:29:37 +1000 | [diff] [blame] | 200 | nouveau_backlight_exit(struct drm_device *dev) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 201 | { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 202 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 203 | |
| 204 | if (dev_priv->backlight) { |
| 205 | backlight_device_unregister(dev_priv->backlight); |
| 206 | dev_priv->backlight = NULL; |
| 207 | } |
| 208 | } |