^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Backlight driver for Maxim MAX8925
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2009 Marvell International Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Haojian Zhuang <haojian.zhuang@marvell.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mfd/max8925.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define MAX_BRIGHTNESS (0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define MIN_BRIGHTNESS (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define LWX_FREQ(x) (((x - 601) / 100) & 0x7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct max8925_backlight_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct max8925_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int current_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int reg_mode_cntl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int reg_cntl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int max8925_backlight_set(struct backlight_device *bl, int brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct max8925_backlight_data *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct max8925_chip *chip = data->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned char value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (brightness > MAX_BRIGHTNESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) value = MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) value = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ret = max8925_reg_write(chip->i2c, data->reg_cntl, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!data->current_brightness && brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* enable WLED output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) else if (!brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* disable WLED output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) dev_dbg(chip->dev, "set brightness %d\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) data->current_brightness = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) dev_dbg(chip->dev, "set brightness %d failure with return value:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) value, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int max8925_backlight_update_status(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return max8925_backlight_set(bl, backlight_get_brightness(bl));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static int max8925_backlight_get_brightness(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct max8925_backlight_data *data = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct max8925_chip *chip = data->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) ret = max8925_reg_read(chip->i2c, data->reg_cntl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) data->current_brightness = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static const struct backlight_ops max8925_backlight_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .options = BL_CORE_SUSPENDRESUME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .update_status = max8925_backlight_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .get_brightness = max8925_backlight_get_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static void max8925_backlight_dt_init(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct device_node *nproot = pdev->dev.parent->of_node, *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct max8925_backlight_pdata *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!nproot || !IS_ENABLED(CONFIG_OF))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) pdata = devm_kzalloc(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) sizeof(struct max8925_backlight_pdata),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) np = of_get_child_by_name(nproot, "backlight");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (!np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) dev_err(&pdev->dev, "failed to find backlight node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!of_property_read_u32(np, "maxim,max8925-dual-string", &val))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) pdata->dual_string = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) pdev->dev.platform_data = pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int max8925_backlight_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct max8925_backlight_pdata *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct max8925_backlight_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct backlight_device *bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned char value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) data = devm_kzalloc(&pdev->dev, sizeof(struct max8925_backlight_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (data == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) res = platform_get_resource(pdev, IORESOURCE_REG, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dev_err(&pdev->dev, "No REG resource for mode control!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) data->reg_mode_cntl = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) res = platform_get_resource(pdev, IORESOURCE_REG, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) dev_err(&pdev->dev, "No REG resource for control!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) data->reg_cntl = res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) data->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) data->current_brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) memset(&props, 0, sizeof(struct backlight_properties));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) props.max_brightness = MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) bl = devm_backlight_device_register(&pdev->dev, "max8925-backlight",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) &pdev->dev, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) &max8925_backlight_ops, &props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (IS_ERR(bl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) dev_err(&pdev->dev, "failed to register backlight\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return PTR_ERR(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) bl->props.brightness = MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) platform_set_drvdata(pdev, bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!pdev->dev.platform_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) max8925_backlight_dt_init(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) pdata = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (pdata->lxw_scl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) value |= (1 << 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (pdata->lxw_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) value |= (LWX_FREQ(pdata->lxw_freq) << 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (pdata->dual_string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) value |= (1 << 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 0xfe, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) backlight_update_status(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static struct platform_driver max8925_backlight_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .name = "max8925-backlight",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .probe = max8925_backlight_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) module_platform_driver(max8925_backlight_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) MODULE_ALIAS("platform:max8925-backlight");