apple_dwi_bl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <linux/backlight.h>
  8. #include <linux/bitfield.h>
  9. #include <linux/device.h>
  10. #include <linux/io.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #define DWI_BL_CTL 0x0
  15. #define DWI_BL_CTL_SEND1 BIT(0)
  16. #define DWI_BL_CTL_SEND2 BIT(4)
  17. #define DWI_BL_CTL_SEND3 BIT(5)
  18. #define DWI_BL_CTL_LE_DATA BIT(6)
  19. /* Only used on Apple A9 and later */
  20. #define DWI_BL_CTL_SEND4 BIT(12)
  21. #define DWI_BL_CMD 0x4
  22. #define DWI_BL_CMD_TYPE GENMASK(31, 28)
  23. #define DWI_BL_CMD_TYPE_SET_BRIGHTNESS 0xa
  24. #define DWI_BL_CMD_DATA GENMASK(10, 0)
  25. #define DWI_BL_CTL_SEND (DWI_BL_CTL_SEND1 | \
  26. DWI_BL_CTL_SEND2 | \
  27. DWI_BL_CTL_SEND3 | \
  28. DWI_BL_CTL_LE_DATA | \
  29. DWI_BL_CTL_SEND4)
  30. #define DWI_BL_MAX_BRIGHTNESS 2047
  31. struct apple_dwi_bl {
  32. void __iomem *base;
  33. };
  34. static int dwi_bl_update_status(struct backlight_device *bl)
  35. {
  36. struct apple_dwi_bl *dwi_bl = bl_get_data(bl);
  37. int brightness = backlight_get_brightness(bl);
  38. u32 cmd = 0;
  39. cmd |= FIELD_PREP(DWI_BL_CMD_DATA, brightness);
  40. cmd |= FIELD_PREP(DWI_BL_CMD_TYPE, DWI_BL_CMD_TYPE_SET_BRIGHTNESS);
  41. writel(cmd, dwi_bl->base + DWI_BL_CMD);
  42. writel(DWI_BL_CTL_SEND, dwi_bl->base + DWI_BL_CTL);
  43. return 0;
  44. }
  45. static int dwi_bl_get_brightness(struct backlight_device *bl)
  46. {
  47. struct apple_dwi_bl *dwi_bl = bl_get_data(bl);
  48. u32 cmd = readl(dwi_bl->base + DWI_BL_CMD);
  49. return FIELD_GET(DWI_BL_CMD_DATA, cmd);
  50. }
  51. static const struct backlight_ops dwi_bl_ops = {
  52. .options = BL_CORE_SUSPENDRESUME,
  53. .get_brightness = dwi_bl_get_brightness,
  54. .update_status = dwi_bl_update_status
  55. };
  56. static int dwi_bl_probe(struct platform_device *dev)
  57. {
  58. struct apple_dwi_bl *dwi_bl;
  59. struct backlight_device *bl;
  60. struct backlight_properties props;
  61. struct resource *res;
  62. dwi_bl = devm_kzalloc(&dev->dev, sizeof(*dwi_bl), GFP_KERNEL);
  63. if (!dwi_bl)
  64. return -ENOMEM;
  65. dwi_bl->base = devm_platform_get_and_ioremap_resource(dev, 0, &res);
  66. if (IS_ERR(dwi_bl->base))
  67. return PTR_ERR(dwi_bl->base);
  68. memset(&props, 0, sizeof(struct backlight_properties));
  69. props.type = BACKLIGHT_PLATFORM;
  70. props.max_brightness = DWI_BL_MAX_BRIGHTNESS;
  71. props.scale = BACKLIGHT_SCALE_LINEAR;
  72. bl = devm_backlight_device_register(&dev->dev, dev->name, &dev->dev,
  73. dwi_bl, &dwi_bl_ops, &props);
  74. if (IS_ERR(bl))
  75. return PTR_ERR(bl);
  76. platform_set_drvdata(dev, dwi_bl);
  77. bl->props.brightness = dwi_bl_get_brightness(bl);
  78. return 0;
  79. }
  80. static const struct of_device_id dwi_bl_of_match[] = {
  81. { .compatible = "apple,dwi-bl" },
  82. {},
  83. };
  84. MODULE_DEVICE_TABLE(of, dwi_bl_of_match);
  85. static struct platform_driver dwi_bl_driver = {
  86. .driver = {
  87. .name = "apple-dwi-bl",
  88. .of_match_table = dwi_bl_of_match
  89. },
  90. .probe = dwi_bl_probe,
  91. };
  92. module_platform_driver(dwi_bl_driver);
  93. MODULE_DESCRIPTION("Apple DWI Backlight Driver");
  94. MODULE_AUTHOR("Nick Chan <towinchenmi@gmail.com>");
  95. MODULE_LICENSE("Dual MIT/GPL");