|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Backlight driver for the Kinetic KTZ8866 |
| 4 | + * |
| 5 | + * Copyright (C) 2022, 2023 Jianhua Lu <[email protected]> |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/backlight.h> |
| 9 | +#include <linux/err.h> |
| 10 | +#include <linux/gpio/consumer.h> |
| 11 | +#include <linux/i2c.h> |
| 12 | +#include <linux/module.h> |
| 13 | +#include <linux/of.h> |
| 14 | +#include <linux/regmap.h> |
| 15 | + |
| 16 | +#define DEFAULT_BRIGHTNESS 1500 |
| 17 | +#define MAX_BRIGHTNESS 2047 |
| 18 | +#define REG_MAX 0x15 |
| 19 | + |
| 20 | +/* reg */ |
| 21 | +#define DEVICE_ID 0x01 |
| 22 | +#define BL_CFG1 0x02 |
| 23 | +#define BL_CFG2 0x03 |
| 24 | +#define BL_BRT_LSB 0x04 |
| 25 | +#define BL_BRT_MSB 0x05 |
| 26 | +#define BL_EN 0x08 |
| 27 | +#define LCD_BIAS_CFG1 0x09 |
| 28 | +#define LCD_BIAS_CFG2 0x0A |
| 29 | +#define LCD_BIAS_CFG3 0x0B |
| 30 | +#define LCD_BOOST_CFG 0x0C |
| 31 | +#define OUTP_CFG 0x0D |
| 32 | +#define OUTN_CFG 0x0E |
| 33 | +#define FLAG 0x0F |
| 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 |
| 40 | + |
| 41 | +/* definition */ |
| 42 | +#define BL_EN_BIT BIT(6) |
| 43 | +#define LCD_BIAS_EN 0x9F |
| 44 | +#define PWM_HYST 0x5 |
| 45 | + |
| 46 | +struct ktz8866 { |
| 47 | + struct i2c_client *client; |
| 48 | + struct regmap *regmap; |
| 49 | + bool led_on; |
| 50 | + struct gpio_desc *enable_gpio; |
| 51 | +}; |
| 52 | + |
| 53 | +static const struct regmap_config ktz8866_regmap_config = { |
| 54 | + .reg_bits = 8, |
| 55 | + .val_bits = 8, |
| 56 | + .max_register = REG_MAX, |
| 57 | +}; |
| 58 | + |
| 59 | +static int ktz8866_write(struct ktz8866 *ktz, unsigned int reg, |
| 60 | + unsigned int val) |
| 61 | +{ |
| 62 | + return regmap_write(ktz->regmap, reg, val); |
| 63 | +} |
| 64 | + |
| 65 | +static int ktz8866_update_bits(struct ktz8866 *ktz, unsigned int reg, |
| 66 | + unsigned int mask, unsigned int val) |
| 67 | +{ |
| 68 | + return regmap_update_bits(ktz->regmap, reg, mask, val); |
| 69 | +} |
| 70 | + |
| 71 | +static int ktz8866_backlight_update_status(struct backlight_device *backlight_dev) |
| 72 | +{ |
| 73 | + struct ktz8866 *ktz = bl_get_data(backlight_dev); |
| 74 | + unsigned int brightness = backlight_get_brightness(backlight_dev); |
| 75 | + |
| 76 | + if (!ktz->led_on && brightness > 0) { |
| 77 | + ktz8866_update_bits(ktz, BL_EN, BL_EN_BIT, BL_EN_BIT); |
| 78 | + ktz->led_on = true; |
| 79 | + } else if (brightness == 0) { |
| 80 | + ktz8866_update_bits(ktz, BL_EN, BL_EN_BIT, 0); |
| 81 | + ktz->led_on = false; |
| 82 | + } |
| 83 | + |
| 84 | + /* Set brightness */ |
| 85 | + ktz8866_write(ktz, BL_BRT_LSB, brightness & 0x7); |
| 86 | + ktz8866_write(ktz, BL_BRT_MSB, (brightness >> 3) & 0xFF); |
| 87 | + |
| 88 | + return 0; |
| 89 | +} |
| 90 | + |
| 91 | +static const struct backlight_ops ktz8866_backlight_ops = { |
| 92 | + .options = BL_CORE_SUSPENDRESUME, |
| 93 | + .update_status = ktz8866_backlight_update_status, |
| 94 | +}; |
| 95 | + |
| 96 | +static void ktz8866_init(struct ktz8866 *ktz) |
| 97 | +{ |
| 98 | + unsigned int val = 0; |
| 99 | + |
| 100 | + if (of_property_read_u32(ktz->client->dev.of_node, "current-num-sinks", &val)) |
| 101 | + ktz8866_write(ktz, BL_EN, BIT(val) - 1); |
| 102 | + else |
| 103 | + /* Enable all 6 current sinks if the number of current sinks isn't specified. */ |
| 104 | + ktz8866_write(ktz, BL_EN, BIT(6) - 1); |
| 105 | + |
| 106 | + if (of_property_read_u32(ktz->client->dev.of_node, "kinetic,current-ramp-delay-ms", &val)) { |
| 107 | + if (val <= 128) |
| 108 | + ktz8866_write(ktz, BL_CFG2, BIT(7) | (ilog2(val) << 3) | PWM_HYST); |
| 109 | + else |
| 110 | + ktz8866_write(ktz, BL_CFG2, BIT(7) | ((5 + val / 64) << 3) | PWM_HYST); |
| 111 | + } |
| 112 | + |
| 113 | + if (of_property_read_u32(ktz->client->dev.of_node, "kinetic,led-enable-ramp-delay-ms", &val)) { |
| 114 | + if (val == 0) |
| 115 | + ktz8866_write(ktz, BL_DIMMING, 0); |
| 116 | + else { |
| 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); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 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); |
| 125 | +} |
| 126 | + |
| 127 | +static int ktz8866_probe(struct i2c_client *client, |
| 128 | + const struct i2c_device_id *id) |
| 129 | +{ |
| 130 | + struct backlight_device *backlight_dev; |
| 131 | + struct backlight_properties props; |
| 132 | + struct ktz8866 *ktz; |
| 133 | + int ret = 0; |
| 134 | + |
| 135 | + ktz = devm_kzalloc(&client->dev, sizeof(*ktz), GFP_KERNEL); |
| 136 | + if (!ktz) |
| 137 | + return -ENOMEM; |
| 138 | + |
| 139 | + ktz->client = client; |
| 140 | + ktz->regmap = devm_regmap_init_i2c(client, &ktz8866_regmap_config); |
| 141 | + if (IS_ERR(ktz->regmap)) |
| 142 | + return dev_err_probe(&client->dev, PTR_ERR(ktz->regmap), "failed to init regmap\n"); |
| 143 | + |
| 144 | + ret = devm_regulator_get_enable(&client->dev, "vddpos"); |
| 145 | + if (ret) |
| 146 | + return dev_err_probe(&client->dev, ret, "get regulator vddpos failed\n"); |
| 147 | + ret = devm_regulator_get_enable(&client->dev, "vddneg"); |
| 148 | + if (ret) |
| 149 | + return dev_err_probe(&client->dev, ret, "get regulator vddneg failed\n"); |
| 150 | + |
| 151 | + ktz->enable_gpio = devm_gpiod_get_optional(&client->dev, "enable", GPIOD_OUT_HIGH); |
| 152 | + if (IS_ERR(ktz->enable_gpio)) |
| 153 | + return PTR_ERR(ktz->enable_gpio); |
| 154 | + |
| 155 | + memset(&props, 0, sizeof(props)); |
| 156 | + props.type = BACKLIGHT_RAW; |
| 157 | + props.max_brightness = MAX_BRIGHTNESS; |
| 158 | + props.brightness = DEFAULT_BRIGHTNESS; |
| 159 | + props.scale = BACKLIGHT_SCALE_LINEAR; |
| 160 | + |
| 161 | + backlight_dev = devm_backlight_device_register(&client->dev, "ktz8866-backlight", |
| 162 | + &client->dev, ktz, &ktz8866_backlight_ops, &props); |
| 163 | + if (IS_ERR(backlight_dev)) |
| 164 | + return dev_err_probe(&client->dev, PTR_ERR(backlight_dev), |
| 165 | + "failed to register backlight device\n"); |
| 166 | + |
| 167 | + ktz8866_init(ktz); |
| 168 | + |
| 169 | + i2c_set_clientdata(client, backlight_dev); |
| 170 | + backlight_update_status(backlight_dev); |
| 171 | + |
| 172 | + return 0; |
| 173 | +} |
| 174 | + |
| 175 | +static void ktz8866_remove(struct i2c_client *client) |
| 176 | +{ |
| 177 | + struct backlight_device *backlight_dev = i2c_get_clientdata(client); |
| 178 | + backlight_dev->props.brightness = 0; |
| 179 | + backlight_update_status(backlight_dev); |
| 180 | +} |
| 181 | + |
| 182 | +static const struct i2c_device_id ktz8866_ids[] = { |
| 183 | + { "ktz8866", 0 }, |
| 184 | + {}, |
| 185 | +}; |
| 186 | +MODULE_DEVICE_TABLE(i2c, ktz8866_ids); |
| 187 | + |
| 188 | +static const struct of_device_id ktz8866_match_table[] = { |
| 189 | + { |
| 190 | + .compatible = "kinetic,ktz8866", |
| 191 | + }, |
| 192 | + {}, |
| 193 | +}; |
| 194 | + |
| 195 | +static struct i2c_driver ktz8866_driver = { |
| 196 | + .driver = { |
| 197 | + .name = "ktz8866", |
| 198 | + .of_match_table = ktz8866_match_table, |
| 199 | + }, |
| 200 | + .probe = ktz8866_probe, |
| 201 | + .remove = ktz8866_remove, |
| 202 | + .id_table = ktz8866_ids, |
| 203 | +}; |
| 204 | + |
| 205 | +module_i2c_driver(ktz8866_driver); |
| 206 | + |
| 207 | +MODULE_DESCRIPTION("Kinetic KTZ8866 Backlight Driver"); |
| 208 | +MODULE_AUTHOR( "Jianhua Lu <[email protected]>"); |
| 209 | +MODULE_LICENSE("GPL"); |
0 commit comments