1 // SPDX-License-Identifier: GPL-2.0-only
3 * Backlight driver for Maxim MAX8925
5 * Copyright (C) 2009 Marvell International Ltd.
6 * Haojian Zhuang <haojian.zhuang@marvell.com>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/platform_device.h>
13 #include <linux/i2c.h>
14 #include <linux/backlight.h>
15 #include <linux/mfd/max8925.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
19 #define MAX_BRIGHTNESS (0xff)
20 #define MIN_BRIGHTNESS (0)
22 #define LWX_FREQ(x) (((x - 601) / 100) & 0x7)
24 struct max8925_backlight_data
{
25 struct max8925_chip
*chip
;
27 int current_brightness
;
32 static int max8925_backlight_set(struct backlight_device
*bl
, int brightness
)
34 struct max8925_backlight_data
*data
= bl_get_data(bl
);
35 struct max8925_chip
*chip
= data
->chip
;
39 if (brightness
> MAX_BRIGHTNESS
)
40 value
= MAX_BRIGHTNESS
;
44 ret
= max8925_reg_write(chip
->i2c
, data
->reg_cntl
, value
);
48 if (!data
->current_brightness
&& brightness
)
49 /* enable WLED output */
50 ret
= max8925_set_bits(chip
->i2c
, data
->reg_mode_cntl
, 1, 1);
52 /* disable WLED output */
53 ret
= max8925_set_bits(chip
->i2c
, data
->reg_mode_cntl
, 1, 0);
56 dev_dbg(chip
->dev
, "set brightness %d\n", value
);
57 data
->current_brightness
= value
;
60 dev_dbg(chip
->dev
, "set brightness %d failure with return value:%d\n",
65 static int max8925_backlight_update_status(struct backlight_device
*bl
)
67 int brightness
= bl
->props
.brightness
;
69 if (bl
->props
.power
!= FB_BLANK_UNBLANK
)
72 if (bl
->props
.fb_blank
!= FB_BLANK_UNBLANK
)
75 if (bl
->props
.state
& BL_CORE_SUSPENDED
)
78 return max8925_backlight_set(bl
, brightness
);
81 static int max8925_backlight_get_brightness(struct backlight_device
*bl
)
83 struct max8925_backlight_data
*data
= bl_get_data(bl
);
84 struct max8925_chip
*chip
= data
->chip
;
87 ret
= max8925_reg_read(chip
->i2c
, data
->reg_cntl
);
90 data
->current_brightness
= ret
;
91 dev_dbg(chip
->dev
, "get brightness %d\n", data
->current_brightness
);
95 static const struct backlight_ops max8925_backlight_ops
= {
96 .options
= BL_CORE_SUSPENDRESUME
,
97 .update_status
= max8925_backlight_update_status
,
98 .get_brightness
= max8925_backlight_get_brightness
,
101 static void max8925_backlight_dt_init(struct platform_device
*pdev
)
103 struct device_node
*nproot
= pdev
->dev
.parent
->of_node
, *np
;
104 struct max8925_backlight_pdata
*pdata
;
107 if (!nproot
|| !IS_ENABLED(CONFIG_OF
))
110 pdata
= devm_kzalloc(&pdev
->dev
,
111 sizeof(struct max8925_backlight_pdata
),
116 np
= of_get_child_by_name(nproot
, "backlight");
118 dev_err(&pdev
->dev
, "failed to find backlight node\n");
122 if (!of_property_read_u32(np
, "maxim,max8925-dual-string", &val
))
123 pdata
->dual_string
= val
;
127 pdev
->dev
.platform_data
= pdata
;
130 static int max8925_backlight_probe(struct platform_device
*pdev
)
132 struct max8925_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
133 struct max8925_backlight_pdata
*pdata
;
134 struct max8925_backlight_data
*data
;
135 struct backlight_device
*bl
;
136 struct backlight_properties props
;
137 struct resource
*res
;
141 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8925_backlight_data
),
146 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
148 dev_err(&pdev
->dev
, "No REG resource for mode control!\n");
151 data
->reg_mode_cntl
= res
->start
;
152 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 1);
154 dev_err(&pdev
->dev
, "No REG resource for control!\n");
157 data
->reg_cntl
= res
->start
;
160 data
->current_brightness
= 0;
162 memset(&props
, 0, sizeof(struct backlight_properties
));
163 props
.type
= BACKLIGHT_RAW
;
164 props
.max_brightness
= MAX_BRIGHTNESS
;
165 bl
= devm_backlight_device_register(&pdev
->dev
, "max8925-backlight",
167 &max8925_backlight_ops
, &props
);
169 dev_err(&pdev
->dev
, "failed to register backlight\n");
172 bl
->props
.brightness
= MAX_BRIGHTNESS
;
174 platform_set_drvdata(pdev
, bl
);
177 if (!pdev
->dev
.platform_data
)
178 max8925_backlight_dt_init(pdev
);
180 pdata
= pdev
->dev
.platform_data
;
185 value
|= (LWX_FREQ(pdata
->lxw_freq
) << 4);
186 if (pdata
->dual_string
)
189 ret
= max8925_set_bits(chip
->i2c
, data
->reg_mode_cntl
, 0xfe, value
);
192 backlight_update_status(bl
);
196 static struct platform_driver max8925_backlight_driver
= {
198 .name
= "max8925-backlight",
200 .probe
= max8925_backlight_probe
,
203 module_platform_driver(max8925_backlight_driver
);
205 MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
206 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
207 MODULE_LICENSE("GPL");
208 MODULE_ALIAS("platform:max8925-backlight");