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