blob: 7b87b6bd40b95cb8912f2a581dd184d3af82c920 [file] [log] [blame]
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2022 Svyatoslav Ryhel <clamor95@gmail.com>
4 */
5
6#define LOG_CATEGORY UCLASS_PANEL_BACKLIGHT
7
8#include <backlight.h>
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +03009#include <dm.h>
Svyatoslav Ryhel98602962025-02-21 17:05:37 +020010#include <dm/ofnode.h>
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030011#include <i2c.h>
12#include <log.h>
13#include <linux/delay.h>
14#include <linux/err.h>
15#include <asm/gpio.h>
16
17#define LM3533_BL_MIN_BRIGHTNESS 0x02
18#define LM3533_BL_MAX_BRIGHTNESS 0xFF
19
20#define LM3533_SINK_OUTPUT_CONFIG_1 0x10
Svyatoslav Ryhel4e9f3232025-02-21 17:47:36 +020021#define LM3533_CONTROL_PWM_BASE 0x14
22#define PWM_MAX GENMASK(5, 0)
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030023#define LM3533_CONTROL_BANK_AB_BRIGHTNESS 0x1A
Svyatoslav Ryhel4e9f3232025-02-21 17:47:36 +020024#define LM3533_CONTROL_FULLSCALE_CURRENT_BASE 0x1F
25#define MAX_CURRENT_MIN 5000
26#define MAX_CURRENT_MAX 29800
27#define MAX_CURRENT_STEP 800
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030028#define LM3533_CONTROL_BANK_ENABLE 0x27
29#define LM3533_OVP_FREQUENCY_PWM_POLARITY 0x2C
Svyatoslav Ryhel2906b002025-02-21 17:22:47 +020030#define BOOST_OVP_MASK GENMASK(2, 1)
31#define BOOST_OVP_SHIFT 1
32#define BOOST_FREQ_MASK BIT(0)
33#define BOOST_FREQ_SHIFT 0
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030034#define LM3533_BRIGHTNESS_REGISTER_A 0x40
35
Svyatoslav Ryhel98602962025-02-21 17:05:37 +020036#define LM3533_BOOST_OVP_16V 16000000UL
37#define LM3533_BOOST_FREQ_500KHZ 500000UL
38
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030039struct lm3533_backlight_priv {
40 struct gpio_desc enable_gpio;
41 u32 def_bl_lvl;
Svyatoslav Ryhel98602962025-02-21 17:05:37 +020042
43 /* Core */
44 u32 boost_ovp;
45 u32 boost_freq;
46
47 /* Backlight */
48 u32 reg;
49 u16 max_current; /* 5000 - 29800 uA (800 uA step) */
50 u8 pwm; /* 0 - 0x3f */
51 bool linear;
52 bool hvled;
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030053};
54
55static int lm3533_backlight_enable(struct udevice *dev)
56{
57 struct lm3533_backlight_priv *priv = dev_get_priv(dev);
Svyatoslav Ryhel4e9f3232025-02-21 17:47:36 +020058 u8 val, id = priv->reg;
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030059 int ret;
60
Svyatoslav Ryhel4e9f3232025-02-21 17:47:36 +020061 if (priv->linear) {
62 ret = dm_i2c_reg_clrset(dev, LM3533_CONTROL_BANK_AB_BRIGHTNESS,
63 BIT(2 * id + 1), BIT(2 * id + 1));
64 if (ret)
65 return ret;
66 }
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030067
Svyatoslav Ryhel4e9f3232025-02-21 17:47:36 +020068 if (priv->hvled) {
69 ret = dm_i2c_reg_clrset(dev, LM3533_SINK_OUTPUT_CONFIG_1,
70 BIT(0) | BIT(1), id | id << 1);
71 if (ret)
72 return ret;
73 }
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030074
Svyatoslav Ryhel4e9f3232025-02-21 17:47:36 +020075 /* Set current */
76 if (priv->max_current < MAX_CURRENT_MIN || priv->max_current > MAX_CURRENT_MAX)
77 return -EINVAL;
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030078
Svyatoslav Ryhel4e9f3232025-02-21 17:47:36 +020079 val = (priv->max_current - MAX_CURRENT_MIN) / MAX_CURRENT_STEP;
80 ret = dm_i2c_reg_write(dev, LM3533_CONTROL_FULLSCALE_CURRENT_BASE + id, val);
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030081 if (ret)
82 return ret;
83
Svyatoslav Ryhel4e9f3232025-02-21 17:47:36 +020084 /* Set PWM mask */
85 if (priv->pwm > PWM_MAX)
86 return -EINVAL;
87
88 ret = dm_i2c_reg_write(dev, LM3533_CONTROL_PWM_BASE + id, priv->pwm);
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030089 if (ret)
90 return ret;
91
Svyatoslav Ryhel4e9f3232025-02-21 17:47:36 +020092 /* Enable Control Bank */
93 return dm_i2c_reg_clrset(dev, LM3533_CONTROL_BANK_ENABLE, BIT(id), BIT(id));
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +030094}
95
96static int lm3533_backlight_set_brightness(struct udevice *dev, int percent)
97{
98 struct lm3533_backlight_priv *priv = dev_get_priv(dev);
99 int ret;
100
101 if (percent == BACKLIGHT_DEFAULT)
102 percent = priv->def_bl_lvl;
103
104 if (percent < LM3533_BL_MIN_BRIGHTNESS)
105 percent = LM3533_BL_MIN_BRIGHTNESS;
106
107 if (percent > LM3533_BL_MAX_BRIGHTNESS)
108 percent = LM3533_BL_MAX_BRIGHTNESS;
109
110 /* Set brightness level */
111 ret = dm_i2c_reg_write(dev, LM3533_BRIGHTNESS_REGISTER_A,
112 percent);
113 if (ret)
114 return ret;
115
116 return 0;
117}
118
Svyatoslav Ryhel98602962025-02-21 17:05:37 +0200119static int lm3533_backlight_of_to_plat(struct udevice *dev)
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +0300120{
121 struct lm3533_backlight_priv *priv = dev_get_priv(dev);
Svyatoslav Ryhel98602962025-02-21 17:05:37 +0200122 ofnode child;
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +0300123 int ret;
124
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +0300125 ret = gpio_request_by_name(dev, "enable-gpios", 0,
126 &priv->enable_gpio, GPIOD_IS_OUT);
127 if (ret) {
128 log_err("Could not decode enable-gpios (%d)\n", ret);
129 return ret;
130 }
131
Svyatoslav Ryhel98602962025-02-21 17:05:37 +0200132 priv->boost_ovp = dev_read_u32_default(dev, "ti,boost-ovp-microvolt",
133 LM3533_BOOST_OVP_16V);
134
135 /* boost_ovp is defined in microvolts, convert to enum value */
136 priv->boost_ovp = priv->boost_ovp / (8 * 1000 * 1000) - 2;
137
138 priv->boost_freq = dev_read_u32_default(dev, "ti,boost-freq-hz",
139 LM3533_BOOST_FREQ_500KHZ);
140
141 /* boost_freq is defined in Hz, convert to enum value */
142 priv->boost_freq = priv->boost_freq / (500 * 1000) - 1;
143
144 /* Backlight is one of children but has no dedicated driver */
145 ofnode_for_each_subnode(child, dev_ofnode(dev)) {
146 if (ofnode_device_is_compatible(child, "ti,lm3533-backlight")) {
147 const char *node_name = ofnode_get_name(child);
148
149 if (!strcmp(&node_name[10], "1"))
150 priv->reg = 1;
151 else
152 priv->reg = 0;
153
154 priv->max_current = ofnode_read_u32_default(child, "ti,max-current-microamp",
155 5000);
156 priv->pwm = ofnode_read_u32_default(child, "ti,pwm-config-mask", 0);
157
158 priv->def_bl_lvl = ofnode_read_u32_default(child, "default-brightness",
159 LM3533_BL_MAX_BRIGHTNESS);
160
161 priv->linear = ofnode_read_bool(child, "ti,linear-mapping-mode");
162 priv->hvled = ofnode_read_bool(child, "ti,hardware-controlled");
163 }
164 }
165
166 return 0;
167}
168
169static int lm3533_backlight_probe(struct udevice *dev)
170{
Svyatoslav Ryhel2906b002025-02-21 17:22:47 +0200171 struct lm3533_backlight_priv *priv = dev_get_priv(dev);
172 int ret;
173
Svyatoslav Ryhel98602962025-02-21 17:05:37 +0200174 if (device_get_uclass_id(dev->parent) != UCLASS_I2C)
175 return -EPROTONOSUPPORT;
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +0300176
Svyatoslav Ryhel2906b002025-02-21 17:22:47 +0200177 dm_gpio_set_value(&priv->enable_gpio, 1);
178 mdelay(5);
179
180 ret = dm_i2c_reg_clrset(dev, LM3533_OVP_FREQUENCY_PWM_POLARITY,
181 BOOST_FREQ_MASK, priv->boost_freq << BOOST_FREQ_SHIFT);
182 if (ret) {
183 log_debug("%s: freq config failed %d\n", __func__, ret);
184 return ret;
185 }
186
187 ret = dm_i2c_reg_clrset(dev, LM3533_OVP_FREQUENCY_PWM_POLARITY,
188 BOOST_OVP_MASK, priv->boost_ovp << BOOST_OVP_SHIFT);
189 if (ret) {
190 log_debug("%s: ovp config failed %d\n", __func__, ret);
191 return ret;
192 }
193
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +0300194 return 0;
195}
196
197static const struct backlight_ops lm3533_backlight_ops = {
198 .enable = lm3533_backlight_enable,
199 .set_brightness = lm3533_backlight_set_brightness,
200};
201
202static const struct udevice_id lm3533_backlight_ids[] = {
203 { .compatible = "ti,lm3533" },
204 { }
205};
206
207U_BOOT_DRIVER(lm3533_backlight) = {
208 .name = "lm3533_backlight",
209 .id = UCLASS_PANEL_BACKLIGHT,
210 .of_match = lm3533_backlight_ids,
Svyatoslav Ryhel98602962025-02-21 17:05:37 +0200211 .of_to_plat = lm3533_backlight_of_to_plat,
Svyatoslav Ryhel3e9488a2023-04-25 10:51:42 +0300212 .probe = lm3533_backlight_probe,
213 .ops = &lm3533_backlight_ops,
214 .priv_auto = sizeof(struct lm3533_backlight_priv),
215};