Linux Framebuffer Layer development
 help / color / mirror / Atom feed
From: Luka Panio <lukapanio@gmail.com>
To: lee@kernel.org, danielt@kernel.org, jingoohan1@gmail.com
Cc: pavel@kernel.org, robh@kernel.org, krzk+dt@kernel.org,
	conor+dt@kernel.org, deller@gmx.de, griffin@kroah.com,
	junjie.cao@linux.dev, mitltlatltl@gmail.com,
	andreas@kemnade.info, dri-devel@lists.freedesktop.org,
	linux-leds@vger.kernel.org, devicetree@vger.kernel.org,
	linux-fbdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Luka Panio <lukapanio@gmail.com>
Subject: [PATCH v2 4/6] backlight: aw99706: Introduce per-chip data
Date: Sat,  5 Dec 2026 01:51:16 +0100	[thread overview]
Message-ID: <20261205005118.884885-5-lukapanio@gmail.com> (raw)
In-Reply-To: <20261205005118.884885-1-lukapanio@gmail.com>

Move the chip specific details (register map, DT properties, chip ID,
brightness and enable register layout, maximum brightness) into a chip
data structure selected by the match tables. This prepares the driver
to support other Awinic backlight controllers with a different register
layout.

Signed-off-by: Luka Panio <lukapanio@gmail.com>
---
 drivers/video/backlight/aw99706.c | 205 ++++++++++++++++++++----------
 1 file changed, 140 insertions(+), 65 deletions(-)

diff --git a/drivers/video/backlight/aw99706.c b/drivers/video/backlight/aw99706.c
index c6b0d0122ad6..db12ee1c979d 100644
--- a/drivers/video/backlight/aw99706.c
+++ b/drivers/video/backlight/aw99706.c
@@ -10,7 +10,6 @@
  */
 
 #include <linux/backlight.h>
-#include <linux/bitfield.h>
 #include <linux/delay.h>
 #include <linux/gpio/consumer.h>
 #include <linux/i2c.h>
@@ -36,10 +35,8 @@
 
 #define AW99706_CFG3_REG			0x03
 #define AW99706_CFG4_REG			0x04
-#define AW99706_BRT_MSB_MASK			GENMASK(3, 0)
 
 #define AW99706_CFG5_REG			0x05
-#define AW99706_BRT_LSB_MASK			GENMASK(7, 0)
 
 #define AW99706_CFG6_REG			0x06
 #define AW99706_RAMP_CTL_MASK			GENMASK(7, 6)
@@ -83,6 +80,9 @@ struct aw99706_dt_prop {
 	int (*lookup)(const struct aw99706_dt_prop *prop, u32 dt_val, u8 *val);
 	const u32 * const lookup_tbl;
 	u8 tbl_size;
+	u32 min;
+	u32 max;
+	u32 step;
 	u8 reg;
 	u8 mask;
 	u32 def_val;
@@ -109,56 +109,63 @@ static int aw99706_dt_property_lookup(const struct aw99706_dt_prop *prop,
 	return i == prop->tbl_size ? -EINVAL : 0;
 }
 
-#define MIN_ILED_MAX	5000
-#define MAX_ILED_MAX	50000
-#define STEP_ILED_MAX	500
-
-static int
-aw99706_dt_property_iled_max_convert(const struct aw99706_dt_prop *prop,
+static int aw99706_dt_property_range(const struct aw99706_dt_prop *prop,
 				     u32 dt_val, u8 *val)
 {
-	if (dt_val > MAX_ILED_MAX || dt_val < MIN_ILED_MAX)
+	if (dt_val > prop->max || dt_val < prop->min)
 		return -EINVAL;
 
-	if ((dt_val - MIN_ILED_MAX) % STEP_ILED_MAX)
+	if ((dt_val - prop->min) % prop->step)
 		return -EINVAL;
 
-	*val = (dt_val - MIN_ILED_MAX) / STEP_ILED_MAX;
+	*val = (dt_val - prop->min) / prop->step;
 
 	return 0;
 }
 
 static const struct aw99706_dt_prop aw99706_dt_props[] = {
 	{
-		"awinic,dim-mode", aw99706_dt_property_lookup,
-		NULL, 0,
-		AW99706_CFG0_REG, AW99706_DIM_MODE_MASK, 1,
+		.name = "awinic,dim-mode",
+		.lookup = aw99706_dt_property_lookup,
+		.reg = AW99706_CFG0_REG, .mask = AW99706_DIM_MODE_MASK,
+		.def_val = 1,
 	},
 	{
-		"awinic,sw-freq-hz", aw99706_dt_property_lookup,
-		aw99706_sw_freq_tbl, ARRAY_SIZE(aw99706_sw_freq_tbl),
-		AW99706_CFG1_REG, AW99706_SW_FREQ_MASK, 750000,
+		.name = "awinic,sw-freq-hz",
+		.lookup = aw99706_dt_property_lookup,
+		.lookup_tbl = aw99706_sw_freq_tbl,
+		.tbl_size = ARRAY_SIZE(aw99706_sw_freq_tbl),
+		.reg = AW99706_CFG1_REG, .mask = AW99706_SW_FREQ_MASK,
+		.def_val = 750000,
 	},
 	{
-		"awinic,sw-ilmt-microamp", aw99706_dt_property_lookup,
-		aw99706_sw_ilmt_tbl, ARRAY_SIZE(aw99706_sw_ilmt_tbl),
-		AW99706_CFG1_REG, AW99706_SW_ILMT_MASK, 3000000,
+		.name = "awinic,sw-ilmt-microamp",
+		.lookup = aw99706_dt_property_lookup,
+		.lookup_tbl = aw99706_sw_ilmt_tbl,
+		.tbl_size = ARRAY_SIZE(aw99706_sw_ilmt_tbl),
+		.reg = AW99706_CFG1_REG, .mask = AW99706_SW_ILMT_MASK,
+		.def_val = 3000000,
 	},
 	{
-		"awinic,iled-max-microamp", aw99706_dt_property_iled_max_convert,
-		NULL, 0,
-		AW99706_CFG2_REG, AW99706_ILED_MAX_MASK, 20000,
-
+		.name = "awinic,iled-max-microamp",
+		.lookup = aw99706_dt_property_range,
+		.min = 5000, .max = 50000, .step = 500,
+		.reg = AW99706_CFG2_REG, .mask = AW99706_ILED_MAX_MASK,
+		.def_val = 20000,
 	},
 	{
-		"awinic,uvlo-thres-microvolt", aw99706_dt_property_lookup,
-		aw99706_ulvo_thres_tbl, ARRAY_SIZE(aw99706_ulvo_thres_tbl),
-		AW99706_CFG2_REG, AW99706_UVLOSEL_MASK, 2200000,
+		.name = "awinic,uvlo-thres-microvolt",
+		.lookup = aw99706_dt_property_lookup,
+		.lookup_tbl = aw99706_ulvo_thres_tbl,
+		.tbl_size = ARRAY_SIZE(aw99706_ulvo_thres_tbl),
+		.reg = AW99706_CFG2_REG, .mask = AW99706_UVLOSEL_MASK,
+		.def_val = 2200000,
 	},
 	{
-		"awinic,ramp-ctl", aw99706_dt_property_lookup,
-		NULL, 0,
-		AW99706_CFG6_REG, AW99706_RAMP_CTL_MASK, 2,
+		.name = "awinic,ramp-ctl",
+		.lookup = aw99706_dt_property_lookup,
+		.reg = AW99706_CFG6_REG, .mask = AW99706_RAMP_CTL_MASK,
+		.def_val = 2,
 	}
 };
 
@@ -168,13 +175,34 @@ struct reg_init_data {
 	u8 val;
 };
 
+#define AW99706_MAX_INIT_REGS	8
+
+struct aw99706_chip_data {
+	const char *bl_name;
+	const struct regmap_config *regmap_config;
+	const struct aw99706_dt_prop *dt_props;
+	u8 num_dt_props;
+	u8 id_reg;
+	u8 id;
+	u32 max_brightness;
+	u8 brt_msb_reg;
+	u8 brt_lsb_reg;
+	u8 brt_lsb_bits;
+	bool brt_lsb_first;
+	u8 en_reg;
+	u8 en_mask;
+	u8 en_val;
+};
+
 struct aw99706_device {
 	struct i2c_client *client;
 	struct device *dev;
+	const struct aw99706_chip_data *chip;
 	struct regmap *regmap;
 	struct backlight_device *bl_dev;
 	struct gpio_desc *hwen_gpio;
-	struct reg_init_data init_tbl[ARRAY_SIZE(aw99706_dt_props)];
+	struct reg_init_data init_tbl[AW99706_MAX_INIT_REGS];
+	unsigned int num_init;
 	bool bl_enable;
 };
 
@@ -239,16 +267,29 @@ static inline int aw99706_i2c_update_bits(struct aw99706_device *aw, u8 reg,
 	return regmap_update_bits(aw->regmap, reg, mask, val);
 }
 
+static void aw99706_add_init(struct aw99706_device *aw, u8 reg, u8 mask,
+			     u8 val)
+{
+	if (WARN_ON(aw->num_init >= ARRAY_SIZE(aw->init_tbl)))
+		return;
+
+	aw->init_tbl[aw->num_init].reg = reg;
+	aw->init_tbl[aw->num_init].mask = mask;
+	aw->init_tbl[aw->num_init].val = val;
+	aw->num_init++;
+}
+
 static void aw99706_dt_parse(struct aw99706_device *aw,
 			     struct backlight_properties *bl_props)
 {
+	const struct aw99706_chip_data *chip = aw->chip;
 	const struct aw99706_dt_prop *prop;
-	u32 dt_val;
+	u32 dt_val, max_brt, brt;
 	int ret, i;
 	u8 val;
 
-	for (i = 0; i < ARRAY_SIZE(aw99706_dt_props); i++) {
-		prop = &aw99706_dt_props[i];
+	for (i = 0; i < chip->num_dt_props; i++) {
+		prop = &chip->dt_props[i];
 		ret = device_property_read_u32(aw->dev, prop->name, &dt_val);
 		if (ret < 0)
 			dt_val = prop->def_val;
@@ -260,23 +301,23 @@ static void aw99706_dt_parse(struct aw99706_device *aw,
 			prop->lookup(prop, prop->def_val, &val);
 		}
 
-		aw->init_tbl[i].reg = prop->reg;
-		aw->init_tbl[i].mask = prop->mask;
-		aw->init_tbl[i].val = val << __ffs(prop->mask);
+		aw99706_add_init(aw, prop->reg, prop->mask,
+				 val << __ffs(prop->mask));
 	}
 
-	bl_props->brightness = AW99706_MAX_BRT_LVL >> 1;
-	bl_props->max_brightness = AW99706_MAX_BRT_LVL;
-	device_property_read_u32(aw->dev, "default-brightness",
-				 &bl_props->brightness);
-	device_property_read_u32(aw->dev, "max-brightness",
-				 &bl_props->max_brightness);
+	brt = chip->max_brightness >> 1;
+	max_brt = chip->max_brightness;
+	device_property_read_u32(aw->dev, "default-brightness", &brt);
+	device_property_read_u32(aw->dev, "max-brightness", &max_brt);
+
+	if (max_brt > chip->max_brightness)
+		max_brt = chip->max_brightness;
 
-	if (bl_props->max_brightness > AW99706_MAX_BRT_LVL)
-		bl_props->max_brightness = AW99706_MAX_BRT_LVL;
+	if (brt > max_brt)
+		brt = max_brt;
 
-	if (bl_props->brightness > bl_props->max_brightness)
-		bl_props->brightness = bl_props->max_brightness;
+	bl_props->brightness = brt;
+	bl_props->max_brightness = max_brt;
 }
 
 static int aw99706_hw_init(struct aw99706_device *aw)
@@ -285,7 +326,7 @@ static int aw99706_hw_init(struct aw99706_device *aw)
 
 	gpiod_set_value_cansleep(aw->hwen_gpio, 1);
 
-	for (i = 0; i < ARRAY_SIZE(aw->init_tbl); i++) {
+	for (i = 0; i < aw->num_init; i++) {
 		ret = aw99706_i2c_update_bits(aw, aw->init_tbl[i].reg,
 					      aw->init_tbl[i].mask,
 					      aw->init_tbl[i].val);
@@ -300,30 +341,45 @@ static int aw99706_hw_init(struct aw99706_device *aw)
 
 static int aw99706_bl_enable(struct aw99706_device *aw, bool en)
 {
+	const struct aw99706_chip_data *chip = aw->chip;
 	int ret;
-	u8 val;
 
-	val = FIELD_PREP(AW99706_BACKLIGHT_EN_MASK, en);
-	ret = aw99706_i2c_update_bits(aw, AW99706_CFGD_REG,
-				      AW99706_BACKLIGHT_EN_MASK, val);
+	ret = aw99706_i2c_update_bits(aw, chip->en_reg, chip->en_mask,
+				      en ? chip->en_val : 0);
 	if (ret)
 		dev_err(aw->dev, "Failed to enable backlight!\n");
 
 	return ret;
 }
 
-static int aw99706_update_brightness(struct aw99706_device *aw, u32 brt_lvl)
+static int aw99706_write_brightness(struct aw99706_device *aw, u32 brt_lvl)
 {
-	bool bl_enable_now = !!brt_lvl;
+	const struct aw99706_chip_data *chip = aw->chip;
+	u8 lsb = brt_lvl & GENMASK(chip->brt_lsb_bits - 1, 0);
+	u8 msb = brt_lvl >> chip->brt_lsb_bits;
 	int ret;
 
-	ret = aw99706_i2c_write(aw, AW99706_CFG4_REG,
-				(brt_lvl >> 8) & AW99706_BRT_MSB_MASK);
+	if (chip->brt_lsb_first) {
+		ret = aw99706_i2c_write(aw, chip->brt_lsb_reg, lsb);
+		if (ret < 0)
+			return ret;
+
+		return aw99706_i2c_write(aw, chip->brt_msb_reg, msb);
+	}
+
+	ret = aw99706_i2c_write(aw, chip->brt_msb_reg, msb);
 	if (ret < 0)
 		return ret;
 
-	ret = aw99706_i2c_write(aw, AW99706_CFG5_REG,
-				brt_lvl & AW99706_BRT_LSB_MASK);
+	return aw99706_i2c_write(aw, chip->brt_lsb_reg, lsb);
+}
+
+static int aw99706_update_brightness(struct aw99706_device *aw, u32 brt_lvl)
+{
+	bool bl_enable_now = !!brt_lvl;
+	int ret;
+
+	ret = aw99706_write_brightness(aw, brt_lvl);
 	if (ret < 0)
 		return ret;
 
@@ -361,7 +417,7 @@ static int aw99706_chip_id_read(struct aw99706_device *aw)
 	int ret;
 	unsigned int val;
 
-	ret = aw99706_i2c_read(aw, AW99706_CHIPID_REG, &val);
+	ret = aw99706_i2c_read(aw, aw->chip->id_reg, &val);
 	if (ret < 0)
 		return ret;
 
@@ -382,9 +438,12 @@ static int aw99706_probe(struct i2c_client *client)
 
 	aw->client = client;
 	aw->dev = dev;
+	aw->chip = i2c_get_match_data(client);
+	if (!aw->chip)
+		return -ENODEV;
 	i2c_set_clientdata(client, aw);
 
-	aw->regmap = devm_regmap_init_i2c(client, &aw99706_regmap_config);
+	aw->regmap = devm_regmap_init_i2c(client, aw->chip->regmap_config);
 	if (IS_ERR(aw->regmap))
 		return dev_err_probe(dev, PTR_ERR(aw->regmap),
 				     "Failed to init regmap\n");
@@ -399,7 +458,7 @@ static int aw99706_probe(struct i2c_client *client)
 	gpiod_set_value_cansleep(aw->hwen_gpio, 1);
 
 	ret = aw99706_chip_id_read(aw);
-	if (ret != AW99706_ID) {
+	if (ret != aw->chip->id) {
 		ret = dev_err_probe(dev, -ENODEV, "Unknown chip id 0x%02x\n",
 				    ret);
 		goto err_power_off;
@@ -414,7 +473,7 @@ static int aw99706_probe(struct i2c_client *client)
 	props.type = BACKLIGHT_RAW;
 	props.scale = BACKLIGHT_SCALE_LINEAR;
 
-	bl_dev = devm_backlight_device_register(dev, "aw99706-backlight", dev,
+	bl_dev = devm_backlight_device_register(dev, aw->chip->bl_name, dev,
 						aw, &aw99706_bl_ops, &props);
 	if (IS_ERR(bl_dev)) {
 		ret = dev_err_probe(dev, PTR_ERR(bl_dev),
@@ -458,14 +517,30 @@ static int aw99706_resume(struct device *dev)
 
 static DEFINE_SIMPLE_DEV_PM_OPS(aw99706_pm_ops, aw99706_suspend, aw99706_resume);
 
+static const struct aw99706_chip_data aw99706_chip = {
+	.bl_name = "aw99706-backlight",
+	.regmap_config = &aw99706_regmap_config,
+	.dt_props = aw99706_dt_props,
+	.num_dt_props = ARRAY_SIZE(aw99706_dt_props),
+	.id_reg = AW99706_CHIPID_REG,
+	.id = AW99706_ID,
+	.max_brightness = AW99706_MAX_BRT_LVL,
+	.brt_msb_reg = AW99706_CFG4_REG,
+	.brt_lsb_reg = AW99706_CFG5_REG,
+	.brt_lsb_bits = 8,
+	.en_reg = AW99706_CFGD_REG,
+	.en_mask = AW99706_BACKLIGHT_EN_MASK,
+	.en_val = AW99706_BACKLIGHT_EN_MASK,
+};
+
 static const struct i2c_device_id aw99706_ids[] = {
-	{ .name = "aw99706" },
+	{ .name = "aw99706", .driver_data = (kernel_ulong_t)&aw99706_chip },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, aw99706_ids);
 
 static const struct of_device_id aw99706_match_table[] = {
-	{ .compatible = "awinic,aw99706", },
+	{ .compatible = "awinic,aw99706", .data = &aw99706_chip },
 	{ }
 };
 MODULE_DEVICE_TABLE(of, aw99706_match_table);
-- 
2.55.0


  parent reply	other threads:[~2026-12-05  0:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  0:51 [PATCH v2 0/6] backlight: aw99706: Add Awinic AW99703 support Luka Panio
2026-12-05  0:51 ` [PATCH v2 1/6] dt-bindings: leds: backlight: aw99706: Use standard led-max-microamp Luka Panio
2026-12-05  0:51 ` [PATCH v2 2/6] dt-bindings: leds: backlight: Add Awinic AW99703 backlight Luka Panio
2026-12-05  0:51 ` [PATCH v2 3/6] backlight: aw99706: Enable the chip before reading its ID Luka Panio
2026-12-05  0:51 ` Luka Panio [this message]
2026-12-05  0:51 ` [PATCH v2 5/6] backlight: aw99706: Support the led-max-microamp property Luka Panio
2026-12-05  0:51 ` [PATCH v2 6/6] backlight: aw99706: Add AW99703 support Luka Panio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205005118.884885-5-lukapanio@gmail.com \
    --to=lukapanio@gmail.com \
    --cc=andreas@kemnade.info \
    --cc=conor+dt@kernel.org \
    --cc=danielt@kernel.org \
    --cc=deller@gmx.de \
    --cc=devicetree@vger.kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=griffin@kroah.com \
    --cc=jingoohan1@gmail.com \
    --cc=junjie.cao@linux.dev \
    --cc=krzk+dt@kernel.org \
    --cc=lee@kernel.org \
    --cc=linux-fbdev@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=mitltlatltl@gmail.com \
    --cc=pavel@kernel.org \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox