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"); 124