|
| 1 | +/* |
| 2 | + * rpi_bl.c - Backlight controller through VPU |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License version 2 as |
| 6 | + * published by the Free Software Foundation. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <linux/backlight.h> |
| 10 | +#include <linux/err.h> |
| 11 | +#include <linux/fb.h> |
| 12 | +#include <linux/gpio.h> |
| 13 | +#include <linux/init.h> |
| 14 | +#include <linux/kernel.h> |
| 15 | +#include <linux/module.h> |
| 16 | +#include <linux/of.h> |
| 17 | +#include <linux/of_gpio.h> |
| 18 | +#include <linux/platform_device.h> |
| 19 | +#include <linux/slab.h> |
| 20 | +#include <soc/bcm2835/raspberrypi-firmware.h> |
| 21 | + |
| 22 | +struct rpi_backlight { |
| 23 | + struct device *dev; |
| 24 | + struct device *fbdev; |
| 25 | + struct rpi_firmware *fw; |
| 26 | +}; |
| 27 | + |
| 28 | +static int rpi_backlight_update_status(struct backlight_device *bl) |
| 29 | +{ |
| 30 | + struct rpi_backlight *gbl = bl_get_data(bl); |
| 31 | + int brightness = bl->props.brightness; |
| 32 | + int ret; |
| 33 | + |
| 34 | + if (bl->props.power != FB_BLANK_UNBLANK || |
| 35 | + bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK)) |
| 36 | + brightness = 0; |
| 37 | + |
| 38 | + ret = rpi_firmware_property(gbl->fw, |
| 39 | + RPI_FIRMWARE_FRAMEBUFFER_SET_BACKLIGHT, |
| 40 | + &brightness, sizeof(brightness)); |
| 41 | + if (ret) { |
| 42 | + dev_err(gbl->dev, "Failed to set brightness\n"); |
| 43 | + return ret; |
| 44 | + } |
| 45 | + |
| 46 | + if (brightness < 0) { |
| 47 | + dev_err(gbl->dev, "Backlight change failed\n"); |
| 48 | + return -EAGAIN; |
| 49 | + } |
| 50 | + |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | +static const struct backlight_ops rpi_backlight_ops = { |
| 55 | + .options = BL_CORE_SUSPENDRESUME, |
| 56 | + .update_status = rpi_backlight_update_status, |
| 57 | +}; |
| 58 | + |
| 59 | +static int rpi_backlight_probe(struct platform_device *pdev) |
| 60 | +{ |
| 61 | + struct backlight_properties props; |
| 62 | + struct backlight_device *bl; |
| 63 | + struct rpi_backlight *gbl; |
| 64 | + struct device_node *fw_node; |
| 65 | + |
| 66 | + gbl = devm_kzalloc(&pdev->dev, sizeof(*gbl), GFP_KERNEL); |
| 67 | + if (gbl == NULL) |
| 68 | + return -ENOMEM; |
| 69 | + |
| 70 | + gbl->dev = &pdev->dev; |
| 71 | + |
| 72 | + fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0); |
| 73 | + if (!fw_node) { |
| 74 | + dev_err(&pdev->dev, "Missing firmware node\n"); |
| 75 | + return -ENOENT; |
| 76 | + } |
| 77 | + |
| 78 | + gbl->fw = rpi_firmware_get(fw_node); |
| 79 | + if (!gbl->fw) |
| 80 | + return -EPROBE_DEFER; |
| 81 | + |
| 82 | + memset(&props, 0, sizeof(props)); |
| 83 | + props.type = BACKLIGHT_RAW; |
| 84 | + props.max_brightness = 255; |
| 85 | + bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev), |
| 86 | + &pdev->dev, gbl, &rpi_backlight_ops, |
| 87 | + &props); |
| 88 | + if (IS_ERR(bl)) { |
| 89 | + dev_err(&pdev->dev, "failed to register backlight\n"); |
| 90 | + return PTR_ERR(bl); |
| 91 | + } |
| 92 | + |
| 93 | + bl->props.brightness = 255; |
| 94 | + backlight_update_status(bl); |
| 95 | + |
| 96 | + platform_set_drvdata(pdev, bl); |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +static const struct of_device_id rpi_backlight_of_match[] = { |
| 101 | + { .compatible = "raspberrypi,rpi-backlight" }, |
| 102 | + { /* sentinel */ } |
| 103 | +}; |
| 104 | +MODULE_DEVICE_TABLE(of, rpi_backlight_of_match); |
| 105 | + |
| 106 | +static struct platform_driver rpi_backlight_driver = { |
| 107 | + .driver = { |
| 108 | + .name = "rpi-backlight", |
| 109 | + .of_match_table = of_match_ptr(rpi_backlight_of_match), |
| 110 | + }, |
| 111 | + .probe = rpi_backlight_probe, |
| 112 | +}; |
| 113 | + |
| 114 | +module_platform_driver(rpi_backlight_driver); |
| 115 | + |
| 116 | +MODULE_AUTHOR( "Gordon Hollingworth <[email protected]>"); |
| 117 | +MODULE_DESCRIPTION("Raspberry Pi mailbox based Backlight Driver"); |
| 118 | +MODULE_LICENSE("GPL"); |
0 commit comments