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