Home | History | Annotate | Line # | Download | only in nouveau
      1  1.2  riastrad /*	$NetBSD: nouveau_backlight.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright (C) 2009 Red Hat <mjg (at) redhat.com>
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining
      7  1.1  riastrad  * a copy of this software and associated documentation files (the
      8  1.1  riastrad  * "Software"), to deal in the Software without restriction, including
      9  1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     10  1.1  riastrad  * distribute, sublicense, and/or sell copies of the Software, and to
     11  1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     12  1.1  riastrad  * the following conditions:
     13  1.1  riastrad  *
     14  1.1  riastrad  * The above copyright notice and this permission notice (including the
     15  1.1  riastrad  * next paragraph) shall be included in all copies or substantial
     16  1.1  riastrad  * portions of the Software.
     17  1.1  riastrad  *
     18  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     19  1.1  riastrad  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     20  1.1  riastrad  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     21  1.1  riastrad  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
     22  1.1  riastrad  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     23  1.1  riastrad  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     24  1.1  riastrad  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     25  1.1  riastrad  *
     26  1.1  riastrad  */
     27  1.1  riastrad 
     28  1.1  riastrad /*
     29  1.1  riastrad  * Authors:
     30  1.1  riastrad  *  Matthew Garrett <mjg (at) redhat.com>
     31  1.1  riastrad  *
     32  1.1  riastrad  * Register locations derived from NVClock by Roderick Colenbrander
     33  1.1  riastrad  */
     34  1.1  riastrad 
     35  1.2  riastrad #include <sys/cdefs.h>
     36  1.2  riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_backlight.c,v 1.3 2021/12/18 23:45:32 riastradh Exp $");
     37  1.2  riastrad 
     38  1.3  riastrad #include <linux/apple-gmux.h>
     39  1.1  riastrad #include <linux/backlight.h>
     40  1.3  riastrad #include <linux/idr.h>
     41  1.1  riastrad 
     42  1.3  riastrad #include "nouveau_drv.h"
     43  1.1  riastrad #include "nouveau_reg.h"
     44  1.1  riastrad #include "nouveau_encoder.h"
     45  1.3  riastrad #include "nouveau_connector.h"
     46  1.3  riastrad 
     47  1.3  riastrad static struct ida bl_ida;
     48  1.3  riastrad #define BL_NAME_SIZE 15 // 12 for name + 2 for digits + 1 for '\0'
     49  1.3  riastrad 
     50  1.3  riastrad struct nouveau_backlight {
     51  1.3  riastrad 	struct backlight_device *dev;
     52  1.3  riastrad 	int id;
     53  1.3  riastrad };
     54  1.3  riastrad 
     55  1.3  riastrad static bool
     56  1.3  riastrad nouveau_get_backlight_name(char backlight_name[BL_NAME_SIZE],
     57  1.3  riastrad 			   struct nouveau_backlight *bl)
     58  1.3  riastrad {
     59  1.3  riastrad 	const int nb = ida_simple_get(&bl_ida, 0, 0, GFP_KERNEL);
     60  1.3  riastrad 	if (nb < 0 || nb >= 100)
     61  1.3  riastrad 		return false;
     62  1.3  riastrad 	if (nb > 0)
     63  1.3  riastrad 		snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
     64  1.3  riastrad 	else
     65  1.3  riastrad 		snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight");
     66  1.3  riastrad 	bl->id = nb;
     67  1.3  riastrad 	return true;
     68  1.3  riastrad }
     69  1.1  riastrad 
     70  1.1  riastrad static int
     71  1.1  riastrad nv40_get_intensity(struct backlight_device *bd)
     72  1.1  riastrad {
     73  1.3  riastrad 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
     74  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
     75  1.3  riastrad 	struct nvif_object *device = &drm->client.device.object;
     76  1.2  riastrad 	int val = (nvif_rd32(device, NV40_PMC_BACKLIGHT) &
     77  1.3  riastrad 		   NV40_PMC_BACKLIGHT_MASK) >> 16;
     78  1.1  riastrad 
     79  1.1  riastrad 	return val;
     80  1.1  riastrad }
     81  1.1  riastrad 
     82  1.1  riastrad static int
     83  1.1  riastrad nv40_set_intensity(struct backlight_device *bd)
     84  1.1  riastrad {
     85  1.3  riastrad 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
     86  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
     87  1.3  riastrad 	struct nvif_object *device = &drm->client.device.object;
     88  1.1  riastrad 	int val = bd->props.brightness;
     89  1.2  riastrad 	int reg = nvif_rd32(device, NV40_PMC_BACKLIGHT);
     90  1.1  riastrad 
     91  1.2  riastrad 	nvif_wr32(device, NV40_PMC_BACKLIGHT,
     92  1.3  riastrad 		  (val << 16) | (reg & ~NV40_PMC_BACKLIGHT_MASK));
     93  1.1  riastrad 
     94  1.1  riastrad 	return 0;
     95  1.1  riastrad }
     96  1.1  riastrad 
     97  1.1  riastrad static const struct backlight_ops nv40_bl_ops = {
     98  1.1  riastrad 	.options = BL_CORE_SUSPENDRESUME,
     99  1.1  riastrad 	.get_brightness = nv40_get_intensity,
    100  1.1  riastrad 	.update_status = nv40_set_intensity,
    101  1.1  riastrad };
    102  1.1  riastrad 
    103  1.1  riastrad static int
    104  1.3  riastrad nv40_backlight_init(struct nouveau_encoder *encoder,
    105  1.3  riastrad 		    struct backlight_properties *props,
    106  1.3  riastrad 		    const struct backlight_ops **ops)
    107  1.1  riastrad {
    108  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(encoder->base.base.dev);
    109  1.3  riastrad 	struct nvif_object *device = &drm->client.device.object;
    110  1.1  riastrad 
    111  1.2  riastrad 	if (!(nvif_rd32(device, NV40_PMC_BACKLIGHT) & NV40_PMC_BACKLIGHT_MASK))
    112  1.3  riastrad 		return -ENODEV;
    113  1.1  riastrad 
    114  1.3  riastrad 	props->type = BACKLIGHT_RAW;
    115  1.3  riastrad 	props->max_brightness = 31;
    116  1.3  riastrad 	*ops = &nv40_bl_ops;
    117  1.1  riastrad 	return 0;
    118  1.1  riastrad }
    119  1.1  riastrad 
    120  1.1  riastrad static int
    121  1.1  riastrad nv50_get_intensity(struct backlight_device *bd)
    122  1.1  riastrad {
    123  1.1  riastrad 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
    124  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
    125  1.3  riastrad 	struct nvif_object *device = &drm->client.device.object;
    126  1.3  riastrad 	int or = ffs(nv_encoder->dcb->or) - 1;
    127  1.1  riastrad 	u32 div = 1025;
    128  1.1  riastrad 	u32 val;
    129  1.1  riastrad 
    130  1.2  riastrad 	val  = nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
    131  1.1  riastrad 	val &= NV50_PDISP_SOR_PWM_CTL_VAL;
    132  1.1  riastrad 	return ((val * 100) + (div / 2)) / div;
    133  1.1  riastrad }
    134  1.1  riastrad 
    135  1.1  riastrad static int
    136  1.1  riastrad nv50_set_intensity(struct backlight_device *bd)
    137  1.1  riastrad {
    138  1.1  riastrad 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
    139  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
    140  1.3  riastrad 	struct nvif_object *device = &drm->client.device.object;
    141  1.3  riastrad 	int or = ffs(nv_encoder->dcb->or) - 1;
    142  1.1  riastrad 	u32 div = 1025;
    143  1.1  riastrad 	u32 val = (bd->props.brightness * div) / 100;
    144  1.1  riastrad 
    145  1.2  riastrad 	nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or),
    146  1.3  riastrad 		  NV50_PDISP_SOR_PWM_CTL_NEW | val);
    147  1.1  riastrad 	return 0;
    148  1.1  riastrad }
    149  1.1  riastrad 
    150  1.1  riastrad static const struct backlight_ops nv50_bl_ops = {
    151  1.1  riastrad 	.options = BL_CORE_SUSPENDRESUME,
    152  1.1  riastrad 	.get_brightness = nv50_get_intensity,
    153  1.1  riastrad 	.update_status = nv50_set_intensity,
    154  1.1  riastrad };
    155  1.1  riastrad 
    156  1.1  riastrad static int
    157  1.1  riastrad nva3_get_intensity(struct backlight_device *bd)
    158  1.1  riastrad {
    159  1.1  riastrad 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
    160  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
    161  1.3  riastrad 	struct nvif_object *device = &drm->client.device.object;
    162  1.3  riastrad 	int or = ffs(nv_encoder->dcb->or) - 1;
    163  1.1  riastrad 	u32 div, val;
    164  1.1  riastrad 
    165  1.2  riastrad 	div  = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
    166  1.2  riastrad 	val  = nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(or));
    167  1.1  riastrad 	val &= NVA3_PDISP_SOR_PWM_CTL_VAL;
    168  1.1  riastrad 	if (div && div >= val)
    169  1.1  riastrad 		return ((val * 100) + (div / 2)) / div;
    170  1.1  riastrad 
    171  1.1  riastrad 	return 100;
    172  1.1  riastrad }
    173  1.1  riastrad 
    174  1.1  riastrad static int
    175  1.1  riastrad nva3_set_intensity(struct backlight_device *bd)
    176  1.1  riastrad {
    177  1.1  riastrad 	struct nouveau_encoder *nv_encoder = bl_get_data(bd);
    178  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
    179  1.3  riastrad 	struct nvif_object *device = &drm->client.device.object;
    180  1.3  riastrad 	int or = ffs(nv_encoder->dcb->or) - 1;
    181  1.1  riastrad 	u32 div, val;
    182  1.1  riastrad 
    183  1.2  riastrad 	div = nvif_rd32(device, NV50_PDISP_SOR_PWM_DIV(or));
    184  1.1  riastrad 	val = (bd->props.brightness * div) / 100;
    185  1.1  riastrad 	if (div) {
    186  1.3  riastrad 		nvif_wr32(device, NV50_PDISP_SOR_PWM_CTL(or),
    187  1.3  riastrad 			  val |
    188  1.3  riastrad 			  NV50_PDISP_SOR_PWM_CTL_NEW |
    189  1.3  riastrad 			  NVA3_PDISP_SOR_PWM_CTL_UNK);
    190  1.1  riastrad 		return 0;
    191  1.1  riastrad 	}
    192  1.1  riastrad 
    193  1.1  riastrad 	return -EINVAL;
    194  1.1  riastrad }
    195  1.1  riastrad 
    196  1.1  riastrad static const struct backlight_ops nva3_bl_ops = {
    197  1.1  riastrad 	.options = BL_CORE_SUSPENDRESUME,
    198  1.1  riastrad 	.get_brightness = nva3_get_intensity,
    199  1.1  riastrad 	.update_status = nva3_set_intensity,
    200  1.1  riastrad };
    201  1.1  riastrad 
    202  1.1  riastrad static int
    203  1.3  riastrad nv50_backlight_init(struct nouveau_encoder *nv_encoder,
    204  1.3  riastrad 		    struct backlight_properties *props,
    205  1.3  riastrad 		    const struct backlight_ops **ops)
    206  1.3  riastrad {
    207  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(nv_encoder->base.base.dev);
    208  1.3  riastrad 	struct nvif_object *device = &drm->client.device.object;
    209  1.3  riastrad 
    210  1.3  riastrad 	if (!nvif_rd32(device, NV50_PDISP_SOR_PWM_CTL(ffs(nv_encoder->dcb->or) - 1)))
    211  1.3  riastrad 		return -ENODEV;
    212  1.3  riastrad 
    213  1.3  riastrad 	if (drm->client.device.info.chipset <= 0xa0 ||
    214  1.3  riastrad 	    drm->client.device.info.chipset == 0xaa ||
    215  1.3  riastrad 	    drm->client.device.info.chipset == 0xac)
    216  1.3  riastrad 		*ops = &nv50_bl_ops;
    217  1.3  riastrad 	else
    218  1.3  riastrad 		*ops = &nva3_bl_ops;
    219  1.3  riastrad 
    220  1.3  riastrad 	props->type = BACKLIGHT_RAW;
    221  1.3  riastrad 	props->max_brightness = 100;
    222  1.3  riastrad 
    223  1.3  riastrad 	return 0;
    224  1.3  riastrad }
    225  1.3  riastrad 
    226  1.3  riastrad int
    227  1.3  riastrad nouveau_backlight_init(struct drm_connector *connector)
    228  1.1  riastrad {
    229  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(connector->dev);
    230  1.3  riastrad 	struct nouveau_backlight *bl;
    231  1.3  riastrad 	struct nouveau_encoder *nv_encoder = NULL;
    232  1.3  riastrad 	struct nvif_device *device = &drm->client.device;
    233  1.3  riastrad 	char backlight_name[BL_NAME_SIZE];
    234  1.3  riastrad 	struct backlight_properties props = {0};
    235  1.1  riastrad 	const struct backlight_ops *ops;
    236  1.3  riastrad 	int ret;
    237  1.1  riastrad 
    238  1.3  riastrad 	if (apple_gmux_present()) {
    239  1.3  riastrad 		NV_INFO_ONCE(drm, "Apple GMUX detected: not registering Nouveau backlight interface\n");
    240  1.3  riastrad 		return 0;
    241  1.3  riastrad 	}
    242  1.3  riastrad 
    243  1.3  riastrad 	if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
    244  1.3  riastrad 		nv_encoder = find_encoder(connector, DCB_OUTPUT_LVDS);
    245  1.3  riastrad 	else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP)
    246  1.1  riastrad 		nv_encoder = find_encoder(connector, DCB_OUTPUT_DP);
    247  1.3  riastrad 	else
    248  1.3  riastrad 		return 0;
    249  1.3  riastrad 
    250  1.3  riastrad 	if (!nv_encoder)
    251  1.3  riastrad 		return 0;
    252  1.3  riastrad 
    253  1.3  riastrad 	switch (device->info.family) {
    254  1.3  riastrad 	case NV_DEVICE_INFO_V0_CURIE:
    255  1.3  riastrad 		ret = nv40_backlight_init(nv_encoder, &props, &ops);
    256  1.3  riastrad 		break;
    257  1.3  riastrad 	case NV_DEVICE_INFO_V0_TESLA:
    258  1.3  riastrad 	case NV_DEVICE_INFO_V0_FERMI:
    259  1.3  riastrad 	case NV_DEVICE_INFO_V0_KEPLER:
    260  1.3  riastrad 	case NV_DEVICE_INFO_V0_MAXWELL:
    261  1.3  riastrad 	case NV_DEVICE_INFO_V0_PASCAL:
    262  1.3  riastrad 	case NV_DEVICE_INFO_V0_VOLTA:
    263  1.3  riastrad 	case NV_DEVICE_INFO_V0_TURING:
    264  1.3  riastrad 		ret = nv50_backlight_init(nv_encoder, &props, &ops);
    265  1.3  riastrad 		break;
    266  1.3  riastrad 	default:
    267  1.3  riastrad 		return 0;
    268  1.1  riastrad 	}
    269  1.1  riastrad 
    270  1.3  riastrad 	if (ret == -ENODEV)
    271  1.1  riastrad 		return 0;
    272  1.3  riastrad 	else if (ret)
    273  1.3  riastrad 		return ret;
    274  1.3  riastrad 
    275  1.3  riastrad 	bl = kzalloc(sizeof(*bl), GFP_KERNEL);
    276  1.3  riastrad 	if (!bl)
    277  1.3  riastrad 		return -ENOMEM;
    278  1.3  riastrad 
    279  1.3  riastrad 	if (!nouveau_get_backlight_name(backlight_name, bl)) {
    280  1.3  riastrad 		NV_ERROR(drm, "Failed to retrieve a unique name for the backlight interface\n");
    281  1.3  riastrad 		goto fail_alloc;
    282  1.3  riastrad 	}
    283  1.3  riastrad 
    284  1.3  riastrad 	bl->dev = backlight_device_register(backlight_name, connector->kdev,
    285  1.3  riastrad 					    nv_encoder, ops, &props);
    286  1.3  riastrad 	if (IS_ERR(bl->dev)) {
    287  1.3  riastrad 		if (bl->id >= 0)
    288  1.3  riastrad 			ida_simple_remove(&bl_ida, bl->id);
    289  1.3  riastrad 		ret = PTR_ERR(bl->dev);
    290  1.3  riastrad 		goto fail_alloc;
    291  1.3  riastrad 	}
    292  1.1  riastrad 
    293  1.3  riastrad 	nouveau_connector(connector)->backlight = bl;
    294  1.3  riastrad 	bl->dev->props.brightness = bl->dev->ops->get_brightness(bl->dev);
    295  1.3  riastrad 	backlight_update_status(bl->dev);
    296  1.1  riastrad 
    297  1.1  riastrad 	return 0;
    298  1.3  riastrad 
    299  1.3  riastrad fail_alloc:
    300  1.3  riastrad 	kfree(bl);
    301  1.3  riastrad 	return ret;
    302  1.1  riastrad }
    303  1.1  riastrad 
    304  1.3  riastrad void
    305  1.3  riastrad nouveau_backlight_fini(struct drm_connector *connector)
    306  1.1  riastrad {
    307  1.3  riastrad 	struct nouveau_connector *nv_conn = nouveau_connector(connector);
    308  1.3  riastrad 	struct nouveau_backlight *bl = nv_conn->backlight;
    309  1.3  riastrad 
    310  1.3  riastrad 	if (!bl)
    311  1.3  riastrad 		return;
    312  1.1  riastrad 
    313  1.3  riastrad 	if (bl->id >= 0)
    314  1.3  riastrad 		ida_simple_remove(&bl_ida, bl->id);
    315  1.1  riastrad 
    316  1.3  riastrad 	backlight_device_unregister(bl->dev);
    317  1.3  riastrad 	nv_conn->backlight = NULL;
    318  1.3  riastrad 	kfree(bl);
    319  1.1  riastrad }
    320  1.1  riastrad 
    321  1.1  riastrad void
    322  1.3  riastrad nouveau_backlight_ctor(void)
    323  1.1  riastrad {
    324  1.3  riastrad 	ida_init(&bl_ida);
    325  1.3  riastrad }
    326  1.1  riastrad 
    327  1.3  riastrad void
    328  1.3  riastrad nouveau_backlight_dtor(void)
    329  1.3  riastrad {
    330  1.3  riastrad 	ida_destroy(&bl_ida);
    331  1.1  riastrad }
    332