1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * GMA500 Backlight Interface
4  *
5  * Copyright (c) 2009-2011, Intel Corporation.
6  *
7  * Authors: Eric Knopp
8  */
9 
10 #include <acpi/video.h>
11 
12 #include "psb_drv.h"
13 #include "psb_intel_reg.h"
14 #include "psb_intel_drv.h"
15 #include "intel_bios.h"
16 #include "power.h"
17 
gma_backlight_enable(struct drm_device * dev)18 void gma_backlight_enable(struct drm_device *dev)
19 {
20 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
21 
22 	dev_priv->backlight_enabled = true;
23 	dev_priv->ops->backlight_set(dev, dev_priv->backlight_level);
24 }
25 
gma_backlight_disable(struct drm_device * dev)26 void gma_backlight_disable(struct drm_device *dev)
27 {
28 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
29 
30 	dev_priv->backlight_enabled = false;
31 	dev_priv->ops->backlight_set(dev, 0);
32 }
33 
gma_backlight_set(struct drm_device * dev,int v)34 void gma_backlight_set(struct drm_device *dev, int v)
35 {
36 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
37 
38 	dev_priv->backlight_level = v;
39 	if (dev_priv->backlight_enabled)
40 		dev_priv->ops->backlight_set(dev, v);
41 }
42 
gma_backlight_get_brightness(struct backlight_device * bd)43 static int gma_backlight_get_brightness(struct backlight_device *bd)
44 {
45 	struct drm_device *dev = bl_get_data(bd);
46 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
47 
48 	if (dev_priv->ops->backlight_get)
49 		return dev_priv->ops->backlight_get(dev);
50 
51 	return dev_priv->backlight_level;
52 }
53 
gma_backlight_update_status(struct backlight_device * bd)54 static int gma_backlight_update_status(struct backlight_device *bd)
55 {
56 	struct drm_device *dev = bl_get_data(bd);
57 	int level = backlight_get_brightness(bd);
58 
59 	/* Percentage 1-100% being valid */
60 	if (level < 1)
61 		level = 1;
62 
63 	gma_backlight_set(dev, level);
64 	return 0;
65 }
66 
67 static const struct backlight_ops gma_backlight_ops __maybe_unused = {
68 	.get_brightness = gma_backlight_get_brightness,
69 	.update_status  = gma_backlight_update_status,
70 };
71 
gma_backlight_init(struct drm_device * dev)72 int gma_backlight_init(struct drm_device *dev)
73 {
74 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
75 	struct backlight_properties props __maybe_unused = {};
76 	int ret;
77 
78 	dev_priv->backlight_enabled = true;
79 	dev_priv->backlight_level = 100;
80 
81 	ret = dev_priv->ops->backlight_init(dev);
82 	if (ret)
83 		return ret;
84 
85 	if (!acpi_video_backlight_use_native()) {
86 		drm_info(dev, "Skipping %s backlight registration\n",
87 			 dev_priv->ops->backlight_name);
88 		return 0;
89 	}
90 
91 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
92 	props.brightness = dev_priv->backlight_level;
93 	props.max_brightness = PSB_MAX_BRIGHTNESS;
94 	props.type = BACKLIGHT_RAW;
95 
96 	dev_priv->backlight_device =
97 		backlight_device_register(dev_priv->ops->backlight_name,
98 					  dev->dev, dev,
99 					  &gma_backlight_ops, &props);
100 	if (IS_ERR(dev_priv->backlight_device))
101 		return PTR_ERR(dev_priv->backlight_device);
102 #endif
103 
104 	return 0;
105 }
106 
gma_backlight_exit(struct drm_device * dev)107 void gma_backlight_exit(struct drm_device *dev)
108 {
109 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
110 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
111 
112 	if (dev_priv->backlight_device)
113 		backlight_device_unregister(dev_priv->backlight_device);
114 #endif
115 }
116