Skip to content

Commit ea45d21

Browse files
asdfugillag-linaro
authored andcommitted
backlight: apple_dwi_bl: Add Apple DWI backlight driver
Add driver for backlight controllers attached via Apple DWI 2-wire interface, which is found on some Apple iPhones, iPads and iPod touches with a LCD display. Although there is an existing apple_bl driver, it is for backlight controllers on Intel Macs attached via PCI, which is completely different from the Samsung-derived DWI block. Reviewed-by: "Daniel Thompson (RISCstar)" <danielt@kernel.org> Signed-off-by: Nick Chan <towinchenmi@gmail.com> Reviewed-by: Neal Gompa <neal@gompa.dev> Link: https://lore.kernel.org/r/20250214040306.16312-3-towinchenmi@gmail.com Signed-off-by: Lee Jones <lee@kernel.org>
1 parent 0508d17 commit ea45d21

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

drivers/video/backlight/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,17 @@ config BACKLIGHT_APPLE
290290
If you have an Intel-based Apple say Y to enable a driver for its
291291
backlight.
292292

293+
config BACKLIGHT_APPLE_DWI
294+
tristate "Apple DWI 2-Wire Interface Backlight Driver"
295+
depends on ARCH_APPLE || COMPILE_TEST
296+
help
297+
Say Y to enable the backlight driver for backlight controllers
298+
attached via the Apple DWI 2-wire interface which is found in some
299+
Apple iPhones, iPads and iPod touches.
300+
301+
To compile this driver as a module, choose M here: the module will
302+
be called apple_dwi_bl.
303+
293304
config BACKLIGHT_QCOM_WLED
294305
tristate "Qualcomm PMIC WLED Driver"
295306
select REGMAP

drivers/video/backlight/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ obj-$(CONFIG_BACKLIGHT_ADP5520) += adp5520_bl.o
2323
obj-$(CONFIG_BACKLIGHT_ADP8860) += adp8860_bl.o
2424
obj-$(CONFIG_BACKLIGHT_ADP8870) += adp8870_bl.o
2525
obj-$(CONFIG_BACKLIGHT_APPLE) += apple_bl.o
26+
obj-$(CONFIG_BACKLIGHT_APPLE_DWI) += apple_dwi_bl.o
2627
obj-$(CONFIG_BACKLIGHT_AS3711) += as3711_bl.o
2728
obj-$(CONFIG_BACKLIGHT_BD6107) += bd6107.o
2829
obj-$(CONFIG_BACKLIGHT_CLASS_DEVICE) += backlight.o
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// SPDX-License-Identifier: GPL-2.0 OR MIT
2+
/*
3+
* Driver for backlight controllers attached via Apple DWI 2-wire interface
4+
*
5+
* Copyright (c) 2024 Nick Chan <towinchenmi@gmail.com>
6+
*/
7+
8+
#include <linux/backlight.h>
9+
#include <linux/bitfield.h>
10+
#include <linux/device.h>
11+
#include <linux/io.h>
12+
#include <linux/module.h>
13+
#include <linux/platform_device.h>
14+
15+
#define DWI_BL_CTL 0x0
16+
#define DWI_BL_CTL_SEND1 BIT(0)
17+
#define DWI_BL_CTL_SEND2 BIT(4)
18+
#define DWI_BL_CTL_SEND3 BIT(5)
19+
#define DWI_BL_CTL_LE_DATA BIT(6)
20+
/* Only used on Apple A9 and later */
21+
#define DWI_BL_CTL_SEND4 BIT(12)
22+
23+
#define DWI_BL_CMD 0x4
24+
#define DWI_BL_CMD_TYPE GENMASK(31, 28)
25+
#define DWI_BL_CMD_TYPE_SET_BRIGHTNESS 0xa
26+
#define DWI_BL_CMD_DATA GENMASK(10, 0)
27+
28+
#define DWI_BL_CTL_SEND (DWI_BL_CTL_SEND1 | \
29+
DWI_BL_CTL_SEND2 | \
30+
DWI_BL_CTL_SEND3 | \
31+
DWI_BL_CTL_LE_DATA | \
32+
DWI_BL_CTL_SEND4)
33+
34+
#define DWI_BL_MAX_BRIGHTNESS 2047
35+
36+
struct apple_dwi_bl {
37+
void __iomem *base;
38+
};
39+
40+
static int dwi_bl_update_status(struct backlight_device *bl)
41+
{
42+
struct apple_dwi_bl *dwi_bl = bl_get_data(bl);
43+
44+
int brightness = backlight_get_brightness(bl);
45+
46+
u32 cmd = 0;
47+
48+
cmd |= FIELD_PREP(DWI_BL_CMD_DATA, brightness);
49+
cmd |= FIELD_PREP(DWI_BL_CMD_TYPE, DWI_BL_CMD_TYPE_SET_BRIGHTNESS);
50+
51+
writel(cmd, dwi_bl->base + DWI_BL_CMD);
52+
writel(DWI_BL_CTL_SEND, dwi_bl->base + DWI_BL_CTL);
53+
54+
return 0;
55+
}
56+
57+
static int dwi_bl_get_brightness(struct backlight_device *bl)
58+
{
59+
struct apple_dwi_bl *dwi_bl = bl_get_data(bl);
60+
61+
u32 cmd = readl(dwi_bl->base + DWI_BL_CMD);
62+
63+
return FIELD_GET(DWI_BL_CMD_DATA, cmd);
64+
}
65+
66+
static const struct backlight_ops dwi_bl_ops = {
67+
.options = BL_CORE_SUSPENDRESUME,
68+
.get_brightness = dwi_bl_get_brightness,
69+
.update_status = dwi_bl_update_status
70+
};
71+
72+
static int dwi_bl_probe(struct platform_device *dev)
73+
{
74+
struct apple_dwi_bl *dwi_bl;
75+
struct backlight_device *bl;
76+
struct backlight_properties props;
77+
struct resource *res;
78+
79+
dwi_bl = devm_kzalloc(&dev->dev, sizeof(*dwi_bl), GFP_KERNEL);
80+
if (!dwi_bl)
81+
return -ENOMEM;
82+
83+
dwi_bl->base = devm_platform_get_and_ioremap_resource(dev, 0, &res);
84+
if (IS_ERR(dwi_bl->base))
85+
return PTR_ERR(dwi_bl->base);
86+
87+
memset(&props, 0, sizeof(struct backlight_properties));
88+
props.type = BACKLIGHT_PLATFORM;
89+
props.max_brightness = DWI_BL_MAX_BRIGHTNESS;
90+
props.scale = BACKLIGHT_SCALE_LINEAR;
91+
92+
bl = devm_backlight_device_register(&dev->dev, dev->name, &dev->dev,
93+
dwi_bl, &dwi_bl_ops, &props);
94+
if (IS_ERR(bl))
95+
return PTR_ERR(bl);
96+
97+
platform_set_drvdata(dev, dwi_bl);
98+
99+
bl->props.brightness = dwi_bl_get_brightness(bl);
100+
101+
return 0;
102+
}
103+
104+
static const struct of_device_id dwi_bl_of_match[] = {
105+
{ .compatible = "apple,dwi-bl" },
106+
{},
107+
};
108+
109+
MODULE_DEVICE_TABLE(of, dwi_bl_of_match);
110+
111+
static struct platform_driver dwi_bl_driver = {
112+
.driver = {
113+
.name = "apple-dwi-bl",
114+
.of_match_table = dwi_bl_of_match
115+
},
116+
.probe = dwi_bl_probe,
117+
};
118+
119+
module_platform_driver(dwi_bl_driver);
120+
121+
MODULE_DESCRIPTION("Apple DWI Backlight Driver");
122+
MODULE_AUTHOR("Nick Chan <towinchenmi@gmail.com>");
123+
MODULE_LICENSE("Dual MIT/GPL");

0 commit comments

Comments
 (0)