1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the Cirrus EP93xx lcd backlight
4  *
5  * Copyright (c) 2010 H Hartley Sweeten <hsweeten@visionengravers.com>
6  *
7  * This driver controls the pulse width modulated brightness control output,
8  * BRIGHT, on the Cirrus EP9307, EP9312, and EP9315 processors.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/io.h>
14 #include <linux/fb.h>
15 #include <linux/backlight.h>
16 
17 #define EP93XX_MAX_COUNT		255
18 #define EP93XX_MAX_BRIGHT		255
19 #define EP93XX_DEF_BRIGHT		128
20 
21 struct ep93xxbl {
22 	void __iomem *mmio;
23 	int brightness;
24 };
25 
26 static int ep93xxbl_set(struct backlight_device *bl, int brightness)
27 {
28 	struct ep93xxbl *ep93xxbl = bl_get_data(bl);
29 
30 	writel((brightness << 8) | EP93XX_MAX_COUNT, ep93xxbl->mmio);
31 
32 	ep93xxbl->brightness = brightness;
33 
34 	return 0;
35 }
36 
37 static int ep93xxbl_update_status(struct backlight_device *bl)
38 {
39 	int brightness = bl->props.brightness;
40 
41 	if (bl->props.power != FB_BLANK_UNBLANK ||
42 	    bl->props.fb_blank != FB_BLANK_UNBLANK)
43 		brightness = 0;
44 
45 	return ep93xxbl_set(bl, brightness);
46 }
47 
48 static int ep93xxbl_get_brightness(struct backlight_device *bl)
49 {
50 	struct ep93xxbl *ep93xxbl = bl_get_data(bl);
51 
52 	return ep93xxbl->brightness;
53 }
54 
55 static const struct backlight_ops ep93xxbl_ops = {
56 	.update_status	= ep93xxbl_update_status,
57 	.get_brightness	= ep93xxbl_get_brightness,
58 };
59 
60 static int ep93xxbl_probe(struct platform_device *dev)
61 {
62 	struct ep93xxbl *ep93xxbl;
63 	struct backlight_device *bl;
64 	struct backlight_properties props;
65 	struct resource *res;
66 
67 	ep93xxbl = devm_kzalloc(&dev->dev, sizeof(*ep93xxbl), GFP_KERNEL);
68 	if (!ep93xxbl)
69 		return -ENOMEM;
70 
71 	res = platform_get_resource(dev, IORESOURCE_MEM, 0);
72 	if (!res)
73 		return -ENXIO;
74 
75 	/*
76 	 * FIXME - We don't do a request_mem_region here because we are
77 	 * sharing the register space with the framebuffer driver (see
78 	 * drivers/video/ep93xx-fb.c) and doing so will cause the second
79 	 * loaded driver to return -EBUSY.
80 	 *
81 	 * NOTE: No locking is required; the framebuffer does not touch
82 	 * this register.
83 	 */
84 	ep93xxbl->mmio = devm_ioremap(&dev->dev, res->start,
85 				      resource_size(res));
86 	if (!ep93xxbl->mmio)
87 		return -ENXIO;
88 
89 	memset(&props, 0, sizeof(struct backlight_properties));
90 	props.type = BACKLIGHT_RAW;
91 	props.max_brightness = EP93XX_MAX_BRIGHT;
92 	bl = devm_backlight_device_register(&dev->dev, dev->name, &dev->dev,
93 					ep93xxbl, &ep93xxbl_ops, &props);
94 	if (IS_ERR(bl))
95 		return PTR_ERR(bl);
96 
97 	bl->props.brightness = EP93XX_DEF_BRIGHT;
98 
99 	platform_set_drvdata(dev, bl);
100 
101 	ep93xxbl_update_status(bl);
102 
103 	return 0;
104 }
105 
106 #ifdef CONFIG_PM_SLEEP
107 static int ep93xxbl_suspend(struct device *dev)
108 {
109 	struct backlight_device *bl = dev_get_drvdata(dev);
110 
111 	return ep93xxbl_set(bl, 0);
112 }
113 
114 static int ep93xxbl_resume(struct device *dev)
115 {
116 	struct backlight_device *bl = dev_get_drvdata(dev);
117 
118 	backlight_update_status(bl);
119 	return 0;
120 }
121 #endif
122 
123 static SIMPLE_DEV_PM_OPS(ep93xxbl_pm_ops, ep93xxbl_suspend, ep93xxbl_resume);
124 
125 static struct platform_driver ep93xxbl_driver = {
126 	.driver		= {
127 		.name	= "ep93xx-bl",
128 		.pm	= &ep93xxbl_pm_ops,
129 	},
130 	.probe		= ep93xxbl_probe,
131 };
132 
133 module_platform_driver(ep93xxbl_driver);
134 
135 MODULE_DESCRIPTION("EP93xx Backlight Driver");
136 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
137 MODULE_LICENSE("GPL");
138 MODULE_ALIAS("platform:ep93xx-bl");
139