1 // SPDX-License-Identifier: GPL-2.0-only
3 * Backlight driver for the Kinetic KTZ8866
5 * Copyright (C) 2022, 2023 Jianhua Lu <lujianhua000@gmail.com>
8 #include <linux/backlight.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
14 #include <linux/regmap.h>
16 #define DEFAULT_BRIGHTNESS 1500
17 #define MAX_BRIGHTNESS 2047
21 #define DEVICE_ID 0x01
24 #define BL_BRT_LSB 0x04
25 #define BL_BRT_MSB 0x05
27 #define LCD_BIAS_CFG1 0x09
28 #define LCD_BIAS_CFG2 0x0A
29 #define LCD_BIAS_CFG3 0x0B
30 #define LCD_BOOST_CFG 0x0C
34 #define BL_OPTION1 0x10
35 #define BL_OPTION2 0x11
36 #define PWM2DIG_LSBs 0x12
37 #define PWM2DIG_MSBs 0x13
38 #define BL_DIMMING 0x14
39 #define PWM_RAMP_TIME 0x15
42 #define BL_EN_BIT BIT(6)
43 #define LCD_BIAS_EN 0x9F
47 struct i2c_client
*client
;
48 struct regmap
*regmap
;
50 struct gpio_desc
*enable_gpio
;
53 static const struct regmap_config ktz8866_regmap_config
= {
56 .max_register
= REG_MAX
,
59 static int ktz8866_write(struct ktz8866
*ktz
, unsigned int reg
,
62 return regmap_write(ktz
->regmap
, reg
, val
);
65 static int ktz8866_update_bits(struct ktz8866
*ktz
, unsigned int reg
,
66 unsigned int mask
, unsigned int val
)
68 return regmap_update_bits(ktz
->regmap
, reg
, mask
, val
);
71 static int ktz8866_backlight_update_status(struct backlight_device
*backlight_dev
)
73 struct ktz8866
*ktz
= bl_get_data(backlight_dev
);
74 unsigned int brightness
= backlight_get_brightness(backlight_dev
);
76 if (!ktz
->led_on
&& brightness
> 0) {
77 ktz8866_update_bits(ktz
, BL_EN
, BL_EN_BIT
, BL_EN_BIT
);
79 } else if (brightness
== 0) {
80 ktz8866_update_bits(ktz
, BL_EN
, BL_EN_BIT
, 0);
85 ktz8866_write(ktz
, BL_BRT_LSB
, brightness
& 0x7);
86 ktz8866_write(ktz
, BL_BRT_MSB
, (brightness
>> 3) & 0xFF);
91 static const struct backlight_ops ktz8866_backlight_ops
= {
92 .options
= BL_CORE_SUSPENDRESUME
,
93 .update_status
= ktz8866_backlight_update_status
,
96 static void ktz8866_init(struct ktz8866
*ktz
)
100 if (!of_property_read_u32(ktz
->client
->dev
.of_node
, "current-num-sinks", &val
))
101 ktz8866_write(ktz
, BL_EN
, BIT(val
) - 1);
103 /* Enable all 6 current sinks if the number of current sinks isn't specified. */
104 ktz8866_write(ktz
, BL_EN
, BIT(6) - 1);
106 if (!of_property_read_u32(ktz
->client
->dev
.of_node
, "kinetic,current-ramp-delay-ms", &val
)) {
108 ktz8866_write(ktz
, BL_CFG2
, BIT(7) | (ilog2(val
) << 3) | PWM_HYST
);
110 ktz8866_write(ktz
, BL_CFG2
, BIT(7) | ((5 + val
/ 64) << 3) | PWM_HYST
);
113 if (!of_property_read_u32(ktz
->client
->dev
.of_node
, "kinetic,led-enable-ramp-delay-ms", &val
)) {
115 ktz8866_write(ktz
, BL_DIMMING
, 0);
117 unsigned int ramp_off_time
= ilog2(val
) + 1;
118 unsigned int ramp_on_time
= ramp_off_time
<< 4;
119 ktz8866_write(ktz
, BL_DIMMING
, ramp_on_time
| ramp_off_time
);
123 if (of_property_read_bool(ktz
->client
->dev
.of_node
, "kinetic,enable-lcd-bias"))
124 ktz8866_write(ktz
, LCD_BIAS_CFG1
, LCD_BIAS_EN
);
127 static int ktz8866_probe(struct i2c_client
*client
)
129 struct backlight_device
*backlight_dev
;
130 struct backlight_properties props
;
134 ktz
= devm_kzalloc(&client
->dev
, sizeof(*ktz
), GFP_KERNEL
);
138 ktz
->client
= client
;
139 ktz
->regmap
= devm_regmap_init_i2c(client
, &ktz8866_regmap_config
);
140 if (IS_ERR(ktz
->regmap
))
141 return dev_err_probe(&client
->dev
, PTR_ERR(ktz
->regmap
), "failed to init regmap\n");
143 ret
= devm_regulator_get_enable(&client
->dev
, "vddpos");
145 return dev_err_probe(&client
->dev
, ret
, "get regulator vddpos failed\n");
146 ret
= devm_regulator_get_enable(&client
->dev
, "vddneg");
148 return dev_err_probe(&client
->dev
, ret
, "get regulator vddneg failed\n");
150 ktz
->enable_gpio
= devm_gpiod_get_optional(&client
->dev
, "enable", GPIOD_OUT_HIGH
);
151 if (IS_ERR(ktz
->enable_gpio
))
152 return PTR_ERR(ktz
->enable_gpio
);
154 memset(&props
, 0, sizeof(props
));
155 props
.type
= BACKLIGHT_RAW
;
156 props
.max_brightness
= MAX_BRIGHTNESS
;
157 props
.brightness
= DEFAULT_BRIGHTNESS
;
158 props
.scale
= BACKLIGHT_SCALE_LINEAR
;
160 backlight_dev
= devm_backlight_device_register(&client
->dev
, "ktz8866-backlight",
161 &client
->dev
, ktz
, &ktz8866_backlight_ops
, &props
);
162 if (IS_ERR(backlight_dev
))
163 return dev_err_probe(&client
->dev
, PTR_ERR(backlight_dev
),
164 "failed to register backlight device\n");
168 i2c_set_clientdata(client
, backlight_dev
);
169 backlight_update_status(backlight_dev
);
174 static void ktz8866_remove(struct i2c_client
*client
)
176 struct backlight_device
*backlight_dev
= i2c_get_clientdata(client
);
177 backlight_dev
->props
.brightness
= 0;
178 backlight_update_status(backlight_dev
);
181 static const struct i2c_device_id ktz8866_ids
[] = {
185 MODULE_DEVICE_TABLE(i2c
, ktz8866_ids
);
187 static const struct of_device_id ktz8866_match_table
[] = {
189 .compatible
= "kinetic,ktz8866",
193 MODULE_DEVICE_TABLE(of
, ktz8866_match_table
);
195 static struct i2c_driver ktz8866_driver
= {
198 .of_match_table
= ktz8866_match_table
,
200 .probe
= ktz8866_probe
,
201 .remove
= ktz8866_remove
,
202 .id_table
= ktz8866_ids
,
205 module_i2c_driver(ktz8866_driver
);
207 MODULE_DESCRIPTION("Kinetic KTZ8866 Backlight Driver");
208 MODULE_AUTHOR("Jianhua Lu <lujianhua000@gmail.com>");
209 MODULE_LICENSE("GPL");