Svyatoslav Ryhel | 4c3a081 | 2024-11-18 08:42:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2011 Texas Instruments |
| 4 | * Copyright (c) 2024 Svyatoslav Ryhel <clamor95@gmail.com> |
| 5 | */ |
| 6 | |
| 7 | #define LOG_CATEGORY UCLASS_PANEL_BACKLIGHT |
| 8 | |
| 9 | #include <malloc.h> |
| 10 | #include <backlight.h> |
| 11 | #include <dm.h> |
| 12 | #include <dm/devres.h> |
| 13 | #include <i2c.h> |
| 14 | #include <log.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <asm/gpio.h> |
| 18 | #include <power/regulator.h> |
| 19 | |
| 20 | #define LP855x_MIN_BRIGHTNESS 0x00 |
| 21 | #define LP855x_MAX_BRIGHTNESS 0xff |
| 22 | |
| 23 | /* LP8550/1/2/3/6 Registers */ |
| 24 | #define LP855X_BRIGHTNESS_CTRL 0x00 |
| 25 | #define LP855X_DEVICE_CTRL 0x01 |
| 26 | #define LP855X_EEPROM_START 0xa0 |
| 27 | #define LP855X_EEPROM_END 0xa7 |
| 28 | #define LP8556_EPROM_START 0x98 |
| 29 | #define LP8556_EPROM_END 0xaf |
| 30 | |
| 31 | /* LP8555/7 Registers */ |
| 32 | #define LP8557_BL_CMD 0x00 |
| 33 | #define LP8557_BL_MASK 0x01 |
| 34 | #define LP8557_BL_ON 0x01 |
| 35 | #define LP8557_BL_OFF 0x00 |
| 36 | #define LP8557_BRIGHTNESS_CTRL 0x04 |
| 37 | #define LP8557_CONFIG 0x10 |
| 38 | #define LP8555_EPROM_START 0x10 |
| 39 | #define LP8555_EPROM_END 0x7a |
| 40 | #define LP8557_EPROM_START 0x10 |
| 41 | #define LP8557_EPROM_END 0x1e |
| 42 | |
| 43 | struct lp855x_rom_data { |
| 44 | u8 addr; |
| 45 | u8 val; |
| 46 | }; |
| 47 | |
| 48 | struct lp855x_backlight_priv; |
| 49 | |
| 50 | /* |
| 51 | * struct lp855x_device_config |
| 52 | * @pre_init_device: init device function call before updating the brightness |
| 53 | * @reg_brightness: register address for brigthenss control |
| 54 | * @reg_devicectrl: register address for device control |
| 55 | * @post_init_device: late init device function call |
| 56 | */ |
| 57 | struct lp855x_device_config { |
| 58 | int (*pre_init_device)(struct udevice *dev); |
| 59 | u8 reg_brightness; |
| 60 | u8 reg_devicectrl; |
| 61 | u8 reg_eepromstart; |
| 62 | u8 reg_eepromend; |
| 63 | int (*post_init_device)(struct udevice *dev); |
| 64 | }; |
| 65 | |
| 66 | struct lp855x_backlight_priv { |
| 67 | struct udevice *supply; /* regulator for VDD input */ |
| 68 | struct udevice *enable; /* regulator for EN/VDDIO input */ |
| 69 | |
| 70 | u8 device_control; |
| 71 | u8 initial_brightness; |
| 72 | |
| 73 | int size_program; |
| 74 | struct lp855x_rom_data *rom_data; |
| 75 | struct lp855x_device_config *cfg; |
| 76 | }; |
| 77 | |
| 78 | static int lp855x_backlight_enable(struct udevice *dev) |
| 79 | { |
| 80 | struct lp855x_backlight_priv *priv = dev_get_priv(dev); |
| 81 | int ret; |
| 82 | |
| 83 | ret = regulator_set_enable_if_allowed(priv->supply, 1); |
| 84 | if (ret) { |
| 85 | log_debug("%s: enabling power-supply failed (%d)\n", |
| 86 | __func__, ret); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | ret = regulator_set_enable_if_allowed(priv->enable, 1); |
| 91 | if (ret) { |
| 92 | log_debug("%s: enabling enable-supply failed (%d)\n", |
| 93 | __func__, ret); |
| 94 | return ret; |
| 95 | } |
| 96 | mdelay(2); |
| 97 | |
| 98 | if (priv->cfg->pre_init_device) { |
| 99 | ret = priv->cfg->pre_init_device(dev); |
| 100 | if (ret) { |
| 101 | log_debug("%s: pre init device err: %d\n", |
| 102 | __func__, ret); |
| 103 | return ret; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | ret = dm_i2c_reg_write(dev, priv->cfg->reg_brightness, |
| 108 | priv->initial_brightness); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | |
| 112 | ret = dm_i2c_reg_write(dev, priv->cfg->reg_devicectrl, |
| 113 | priv->device_control); |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | |
| 117 | if (priv->size_program > 0) { |
| 118 | int i; |
| 119 | u8 val, addr; |
| 120 | |
| 121 | for (i = 0; i < priv->size_program; i++) { |
| 122 | addr = priv->rom_data[i].addr; |
| 123 | val = priv->rom_data[i].val; |
| 124 | |
| 125 | if (addr < priv->cfg->reg_eepromstart && |
| 126 | addr > priv->cfg->reg_eepromend) |
| 127 | continue; |
| 128 | |
| 129 | ret = dm_i2c_reg_write(dev, addr, val); |
| 130 | if (ret) |
| 131 | return ret; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (priv->cfg->post_init_device) { |
| 136 | ret = priv->cfg->post_init_device(dev); |
| 137 | if (ret) { |
| 138 | log_debug("%s: post init device err: %d\n", |
| 139 | __func__, ret); |
| 140 | return ret; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | static int lp855x_backlight_set_brightness(struct udevice *dev, int percent) |
| 148 | { |
| 149 | struct lp855x_backlight_priv *priv = dev_get_priv(dev); |
| 150 | |
| 151 | if (percent == BACKLIGHT_DEFAULT) |
| 152 | percent = priv->initial_brightness; |
| 153 | |
| 154 | if (percent < LP855x_MIN_BRIGHTNESS) |
| 155 | percent = LP855x_MIN_BRIGHTNESS; |
| 156 | |
| 157 | if (percent > LP855x_MAX_BRIGHTNESS) |
| 158 | percent = LP855x_MAX_BRIGHTNESS; |
| 159 | |
| 160 | /* Set brightness level */ |
| 161 | return dm_i2c_reg_write(dev, priv->cfg->reg_brightness, |
| 162 | percent); |
| 163 | } |
| 164 | |
| 165 | static int lp855x_backlight_probe(struct udevice *dev) |
| 166 | { |
| 167 | struct lp855x_backlight_priv *priv = dev_get_priv(dev); |
| 168 | int rom_length, ret; |
| 169 | |
| 170 | if (device_get_uclass_id(dev->parent) != UCLASS_I2C) |
| 171 | return -EPROTONOSUPPORT; |
| 172 | |
| 173 | priv->cfg = (struct lp855x_device_config *)dev_get_driver_data(dev); |
| 174 | |
| 175 | dev_read_u8(dev, "dev-ctrl", &priv->device_control); |
| 176 | dev_read_u8(dev, "init-brt", &priv->initial_brightness); |
| 177 | |
| 178 | /* Fill ROM platform data if defined */ |
| 179 | rom_length = dev_get_child_count(dev); |
| 180 | if (rom_length > 0) { |
| 181 | struct lp855x_rom_data *rom; |
| 182 | ofnode child; |
| 183 | int i = 0; |
| 184 | |
| 185 | rom = devm_kcalloc(dev, rom_length, sizeof(*rom), GFP_KERNEL); |
| 186 | if (!rom) |
| 187 | return -ENOMEM; |
| 188 | |
| 189 | dev_for_each_subnode(child, dev) { |
| 190 | ofnode_read_u8(child, "rom-addr", &rom[i].addr); |
| 191 | ofnode_read_u8(child, "rom-val", &rom[i].val); |
| 192 | i++; |
| 193 | } |
| 194 | |
| 195 | priv->size_program = rom_length; |
| 196 | priv->rom_data = &rom[0]; |
| 197 | } |
| 198 | |
| 199 | ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev, |
| 200 | "power-supply", &priv->supply); |
| 201 | if (ret) { |
| 202 | log_err("%s: cannot get power-supply: ret = %d\n", __func__, ret); |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev, |
| 207 | "enable-supply", &priv->enable); |
| 208 | if (ret) { |
| 209 | log_err("%s: cannot get enable-supply: ret = %d\n", __func__, ret); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static const struct backlight_ops lp855x_backlight_ops = { |
| 217 | .enable = lp855x_backlight_enable, |
| 218 | .set_brightness = lp855x_backlight_set_brightness, |
| 219 | }; |
| 220 | |
| 221 | static int lp8556_bl_rst(struct udevice *dev) |
| 222 | { |
| 223 | int ret; |
| 224 | |
| 225 | /* Reset backlight after updating EPROM settings */ |
| 226 | ret = dm_i2c_reg_clrset(dev, LP855X_DEVICE_CTRL, LP8557_BL_MASK, |
| 227 | LP8557_BL_OFF); |
| 228 | if (ret) |
| 229 | return ret; |
| 230 | |
| 231 | mdelay(10); |
| 232 | |
| 233 | return dm_i2c_reg_clrset(dev, LP855X_DEVICE_CTRL, LP8557_BL_MASK, |
| 234 | LP8557_BL_ON); |
| 235 | } |
| 236 | |
| 237 | static int lp8557_bl_off(struct udevice *dev) |
| 238 | { |
| 239 | /* BL_ON = 0 before updating EPROM settings */ |
| 240 | return dm_i2c_reg_clrset(dev, LP8557_BL_CMD, LP8557_BL_MASK, |
| 241 | LP8557_BL_OFF); |
| 242 | } |
| 243 | |
| 244 | static int lp8557_bl_on(struct udevice *dev) |
| 245 | { |
| 246 | /* BL_ON = 1 after updating EPROM settings */ |
| 247 | return dm_i2c_reg_clrset(dev, LP8557_BL_CMD, LP8557_BL_MASK, |
| 248 | LP8557_BL_ON); |
| 249 | } |
| 250 | |
| 251 | static struct lp855x_device_config lp855x_dev_cfg = { |
| 252 | .reg_brightness = LP855X_BRIGHTNESS_CTRL, |
| 253 | .reg_devicectrl = LP855X_DEVICE_CTRL, |
| 254 | .reg_eepromstart = LP855X_EEPROM_START, |
| 255 | .reg_eepromend = LP855X_EEPROM_END, |
| 256 | }; |
| 257 | |
| 258 | static struct lp855x_device_config lp8555_dev_cfg = { |
| 259 | .reg_brightness = LP8557_BRIGHTNESS_CTRL, |
| 260 | .reg_devicectrl = LP8557_CONFIG, |
| 261 | .reg_eepromstart = LP8555_EPROM_START, |
| 262 | .reg_eepromend = LP8555_EPROM_END, |
| 263 | .pre_init_device = lp8557_bl_off, |
| 264 | .post_init_device = lp8557_bl_on, |
| 265 | }; |
| 266 | |
| 267 | static struct lp855x_device_config lp8556_dev_cfg = { |
| 268 | .reg_brightness = LP855X_BRIGHTNESS_CTRL, |
| 269 | .reg_devicectrl = LP855X_DEVICE_CTRL, |
| 270 | .reg_eepromstart = LP8556_EPROM_START, |
| 271 | .reg_eepromend = LP8556_EPROM_END, |
| 272 | .post_init_device = lp8556_bl_rst, |
| 273 | }; |
| 274 | |
| 275 | static struct lp855x_device_config lp8557_dev_cfg = { |
| 276 | .reg_brightness = LP8557_BRIGHTNESS_CTRL, |
| 277 | .reg_devicectrl = LP8557_CONFIG, |
| 278 | .reg_eepromstart = LP8557_EPROM_START, |
| 279 | .reg_eepromend = LP8557_EPROM_END, |
| 280 | .pre_init_device = lp8557_bl_off, |
| 281 | .post_init_device = lp8557_bl_on, |
| 282 | }; |
| 283 | |
| 284 | static const struct udevice_id lp855x_backlight_ids[] = { |
| 285 | { .compatible = "ti,lp8550", .data = (ulong)&lp855x_dev_cfg }, |
| 286 | { .compatible = "ti,lp8551", .data = (ulong)&lp855x_dev_cfg }, |
| 287 | { .compatible = "ti,lp8552", .data = (ulong)&lp855x_dev_cfg }, |
| 288 | { .compatible = "ti,lp8553", .data = (ulong)&lp855x_dev_cfg }, |
| 289 | { .compatible = "ti,lp8555", .data = (ulong)&lp8555_dev_cfg }, |
| 290 | { .compatible = "ti,lp8556", .data = (ulong)&lp8556_dev_cfg }, |
| 291 | { .compatible = "ti,lp8557", .data = (ulong)&lp8557_dev_cfg }, |
| 292 | { } |
| 293 | }; |
| 294 | |
| 295 | U_BOOT_DRIVER(lp855x_backlight) = { |
| 296 | .name = "lp855x_backlight", |
| 297 | .id = UCLASS_PANEL_BACKLIGHT, |
| 298 | .of_match = lp855x_backlight_ids, |
| 299 | .probe = lp855x_backlight_probe, |
| 300 | .ops = &lp855x_backlight_ops, |
| 301 | .priv_auto = sizeof(struct lp855x_backlight_priv), |
| 302 | }; |