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 return max8925_backlight_set(bl
, backlight_get_brightness(bl
));
70 static int max8925_backlight_get_brightness(struct backlight_device
*bl
)
72 struct max8925_backlight_data
*data
= bl_get_data(bl
);
73 struct max8925_chip
*chip
= data
->chip
;
76 ret
= max8925_reg_read(chip
->i2c
, data
->reg_cntl
);
79 data
->current_brightness
= ret
;
80 dev_dbg(chip
->dev
, "get brightness %d\n", data
->current_brightness
);
84 static const struct backlight_ops max8925_backlight_ops
= {
85 .options
= BL_CORE_SUSPENDRESUME
,
86 .update_status
= max8925_backlight_update_status
,
87 .get_brightness
= max8925_backlight_get_brightness
,
90 static void max8925_backlight_dt_init(struct platform_device
*pdev
)
92 struct device_node
*nproot
= pdev
->dev
.parent
->of_node
, *np
;
93 struct max8925_backlight_pdata
*pdata
;
96 if (!nproot
|| !IS_ENABLED(CONFIG_OF
))
99 pdata
= devm_kzalloc(&pdev
->dev
,
100 sizeof(struct max8925_backlight_pdata
),
105 np
= of_get_child_by_name(nproot
, "backlight");
107 dev_err(&pdev
->dev
, "failed to find backlight node\n");
111 if (!of_property_read_u32(np
, "maxim,max8925-dual-string", &val
))
112 pdata
->dual_string
= val
;
116 pdev
->dev
.platform_data
= pdata
;
119 static int max8925_backlight_probe(struct platform_device
*pdev
)
121 struct max8925_chip
*chip
= dev_get_drvdata(pdev
->dev
.parent
);
122 struct max8925_backlight_pdata
*pdata
;
123 struct max8925_backlight_data
*data
;
124 struct backlight_device
*bl
;
125 struct backlight_properties props
;
126 struct resource
*res
;
130 data
= devm_kzalloc(&pdev
->dev
, sizeof(struct max8925_backlight_data
),
135 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
137 dev_err(&pdev
->dev
, "No REG resource for mode control!\n");
140 data
->reg_mode_cntl
= res
->start
;
141 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 1);
143 dev_err(&pdev
->dev
, "No REG resource for control!\n");
146 data
->reg_cntl
= res
->start
;
149 data
->current_brightness
= 0;
151 memset(&props
, 0, sizeof(struct backlight_properties
));
152 props
.type
= BACKLIGHT_RAW
;
153 props
.max_brightness
= MAX_BRIGHTNESS
;
154 bl
= devm_backlight_device_register(&pdev
->dev
, "max8925-backlight",
156 &max8925_backlight_ops
, &props
);
158 dev_err(&pdev
->dev
, "failed to register backlight\n");
161 bl
->props
.brightness
= MAX_BRIGHTNESS
;
163 platform_set_drvdata(pdev
, bl
);
166 if (!pdev
->dev
.platform_data
)
167 max8925_backlight_dt_init(pdev
);
169 pdata
= pdev
->dev
.platform_data
;
174 value
|= (LWX_FREQ(pdata
->lxw_freq
) << 4);
175 if (pdata
->dual_string
)
178 ret
= max8925_set_bits(chip
->i2c
, data
->reg_mode_cntl
, 0xfe, value
);
181 backlight_update_status(bl
);
185 static struct platform_driver max8925_backlight_driver
= {
187 .name
= "max8925-backlight",
189 .probe
= max8925_backlight_probe
,
192 module_platform_driver(max8925_backlight_driver
);
194 MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
195 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
196 MODULE_LICENSE("GPL");
197 MODULE_ALIAS("platform:max8925-backlight");