Skip to contentSkip to conversation

backlight: Add TI LMU backlight driver

PATCH In review Milo Kim 3 participants 3 messages Mar 20, 2017

Subsystems (5)
lore
Share & tools
lore

Milo Kim

author
#0 lore raw

PATCH 1/1 backlight: Add TI LMU backlight driver

This is consolidated driver which supports backlight devices below.
LM3532, LM3631, LM3632, LM3633, LM3695 and LM3697.

Structure
---------
It consists of two parts - core and data.

Core part supports features below.
- Backlight subsystem control
- Channel configuration from DT properties
- Light dimming effect control: ramp up and down.
- LMU fault monitor notifier handling
- PWM brightness control

Data part describes device specific data.
- Register value configuration for each LMU device
: initialization, channel configuration, control mode, enable and
brightness.
- PWM action configuration
- Light dimming effect table
- Option for LMU fault monitor support

Macros for register data
------------------------
All LMU devices have 8-bit based registers. LMU_BL_REG() creates 24-bit register value in data part. It consists of address, mask and value. On the other hand, register value should be parsed when the driver reads/writes data from/to I2C registers. Driver uses LMU_BL_GET_ADDR(), LMU_BL_GET_MASK() and LMU_BL_GET_VAL() for this purpose.

Data structure
--------------
ti_lmu_bl: Backlight output channel data
ti_lmu_bl_chip: Backlight device data. One device can have multiple
backlight channel data.
ti_lmu_bl_reg: Backlight device register data
ti_lmu_bl_cfg: Backlight configuration data for each LMU device

Cc Rob Herring <[email protected]>
Cc Sebastian Reichel <[email protected]>
Cc Tony Lindgren <[email protected]>
---
 .../bindings/leds/backlight/ti-lmu-backlight.txt   |  65 ++
 drivers/video/backlight/Kconfig                    |   7 +
 drivers/video/backlight/Makefile                   |   3 +
 drivers/video/backlight/ti-lmu-backlight-core.c    | 655 +++++++++++++++++++++
 drivers/video/backlight/ti-lmu-backlight-data.c    | 287 +++++++++
 include/linux/mfd/ti-lmu-backlight.h               | 290 +++++++++
 6 files changed, 1307 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/leds/backlight/ti-lmu-backlight.txt
 create mode 100644 drivers/video/backlight/ti-lmu-backlight-core.c
 create mode 100644 drivers/video/backlight/ti-lmu-backlight-data.c
 create mode 100644 include/linux/mfd/ti-lmu-backlight.h
Documentation/devicetree/bindings/leds/backlight/ti-lmu-backlight.txt new file +65 -0
new file mode 100644
index 000000000000..c2c35b293716
@@ -0,0 +1,65 @@
+TI LMU backlight device tree bindings
+
+Required property:
+  - compatible: Should be one of:
+                "ti,lm3532-backlight"
+                "ti,lm3631-backlight"
+                "ti,lm3632-backlight"
+                "ti,lm3633-backlight"
+                "ti,lm3695-backlight"
+                "ti,lm3697-backlight"
+
+Optional properties:
+  There are two backlight control mode. One is I2C, the other is PWM mode.
+  Following properties are only specified in PWM mode.
+  Please note that LMU backlight device can have only one PWM channel.
+
+  - pwms: OF device-tree PWM specification.
+  - pwm-names: a list of names for the PWM devices specified in the "pwms"
+               property.
+
+  For the PWM user nodes, please refer to [1].
+
+Child nodes:
+  LMU backlight is represented as sub-nodes of the TI LMU device [2].
+  So, LMU backlight should have more than one backlight child node.
+  Each node exactly matches with backlight control bank configuration.
+  Maximum numbers of child nodes depend on the device.
+  1 = LM3631, LM3632, LM3695
+  2 = LM3633, LM3697
+  3 = LM3532
+
+  Required property of a child node:
+  - led-sources: List of enabled channels from 0 to 2.
+                 Please refer to LED binding [3].
+                 For output channels, please refer to the datasheets [4].
+
+  Optional properties of a child node:
+  - label: Backlight channel identification.
+           Please refer to LED binding [3].
+  - default-brightness-level: Backlight initial brightness value.
+                              Type is <u32>. It is set as soon as backlight
+                              device is created.
+                              0 ~ 2047 = LM3631, LM3632, LM3633, LM3695 and
+                                         LM3697
+                              0 ~ 255  = LM3532
+  - ramp-up-msec, ramp-down-msec: Light dimming effect properties.
+                                  Type is <u32>. Unit is millisecond.
+                                  0 ~ 65 msec    = LM3532
+                                  0 ~ 4000 msec  = LM3631
+                                  0 ~ 16000 msec = LM3633 and LM3697
+  - pwm-period: PWM period. Only valid in PWM brightness mode.
+                Type is <u32>. If this property is missing, then control
+                mode is set to I2C by default.
+
+Examples: Please refer to ti-lmu dt-bindings. [2].
+
+[1] ../pwm/pwm.txt
+[2] ../mfd/ti-lmu.txt
+[3] ../leds/common.txt
+[4] LM3532: http://www.ti.com/product/LM3532/datasheet
+    LM3631: http://www.ti.com/product/LM3631/datasheet
+    LM3632: http://www.ti.com/product/LM3632A/datasheet
+    LM3633: http://www.ti.com/product/LM3633/datasheet
+    LM3695: Datasheet is not opened yet, but only two strings are used.
+    LM3697: http://www.ti.com/product/LM3697/datasheet
drivers/video/backlight/Kconfig +7 -0
index 5ffa4b4e26c0..451d04315810 100644
@@ -427,6 +427,13 @@ config BACKLIGHT_SKY81452
 	  To compile this driver as a module, choose M here: the module will
 	  be called sky81452-backlight
 
+config BACKLIGHT_TI_LMU
+	tristate "Backlight driver for TI LMU"
+	depends on BACKLIGHT_CLASS_DEVICE && MFD_TI_LMU
+	help
+	  Say Y to enable the backlight driver for TI LMU devices.
+	  This supports LM3532, LM3631, LM3632, LM3633, LM3695 and LM3697.
+
 config BACKLIGHT_TPS65217
 	tristate "TPS65217 Backlight"
 	depends on BACKLIGHT_CLASS_DEVICE && MFD_TPS65217
drivers/video/backlight/Makefile +3 -0
index 16ec534cff30..0f74ce7d1e36 100644
@@ -52,6 +52,9 @@ obj-$(CONFIG_BACKLIGHT_PM8941_WLED)	+= pm8941-wled.o
 obj-$(CONFIG_BACKLIGHT_PWM)		+= pwm_bl.o
 obj-$(CONFIG_BACKLIGHT_SAHARA)		+= kb3886_bl.o
 obj-$(CONFIG_BACKLIGHT_SKY81452)	+= sky81452-backlight.o
+ti-lmu-backlight-objs			:= ti-lmu-backlight-core.o \
+					   ti-lmu-backlight-data.o
+obj-$(CONFIG_BACKLIGHT_TI_LMU)		+= ti-lmu-backlight.o
 obj-$(CONFIG_BACKLIGHT_TOSA)		+= tosa_bl.o
 obj-$(CONFIG_BACKLIGHT_TPS65217)	+= tps65217_bl.o
 obj-$(CONFIG_BACKLIGHT_WM831X)		+= wm831x_bl.o
drivers/video/backlight/ti-lmu-backlight-core.c new file +655 -0

655 lines not on this page · raw message

drivers/video/backlight/ti-lmu-backlight-data.c new file +287 -0

287 lines not on this page · raw message

include/linux/mfd/ti-lmu-backlight.h new file +290 -0

290 lines not on this page · raw message

Signature · 1 line
2.11.0
Tested-by on v1 1/1
#1 lore raw

Hi,

Quoted 38 lines · Milo Kim

This is consolidated driver which supports backlight devices below.
LM3532, LM3631, LM3632, LM3633, LM3695 and LM3697.

Structure
---------
It consists of two parts - core and data.

Core part supports features below.
- Backlight subsystem control
- Channel configuration from DT properties
- Light dimming effect control: ramp up and down.
- LMU fault monitor notifier handling
- PWM brightness control

Data part describes device specific data.
- Register value configuration for each LMU device
: initialization, channel configuration, control mode, enable and
brightness.
- PWM action configuration
- Light dimming effect table
- Option for LMU fault monitor support

Macros for register data
------------------------
All LMU devices have 8-bit based registers. LMU_BL_REG() creates 24-bit register value in data part. It consists of address, mask and value. On the other hand, register value should be parsed when the driver reads/writes data from/to I2C registers. Driver uses LMU_BL_GET_ADDR(), LMU_BL_GET_MASK() and LMU_BL_GET_VAL() for this purpose.

Data structure
--------------
ti_lmu_bl: Backlight output channel data
ti_lmu_bl_chip: Backlight device data. One device can have multiple
backlight channel data.
ti_lmu_bl_reg: Backlight device register data
ti_lmu_bl_cfg: Backlight configuration data for each LMU device

Quoted 4 lines
Cc Rob Herring <[email protected]>
Cc Sebastian Reichel <[email protected]>
Cc Tony Lindgren <[email protected]>

Works on Droid 4:

-- Sebastian

#2 lore raw
In reply to Milo Kim
Quoted 22 lines · Milo Kim

This is consolidated driver which supports backlight devices below.
LM3532, LM3631, LM3632, LM3633, LM3695 and LM3697.

Structure
---------
It consists of two parts - core and data.

Core part supports features below.
- Backlight subsystem control
- Channel configuration from DT properties
- Light dimming effect control: ramp up and down.
- LMU fault monitor notifier handling
- PWM brightness control

Data part describes device specific data.
- Register value configuration for each LMU device
: initialization, channel configuration, control mode, enable and
brightness.
- PWM action configuration
- Light dimming effect table
- Option for LMU fault monitor support

Quoted 7 lines

Macros for register data
------------------------
All LMU devices have 8-bit based registers. LMU_BL_REG() creates 24-bit register value in data part. It consists of address, mask and value. On the other hand, register value should be parsed when the driver reads/writes data from/to I2C registers. Driver uses LMU_BL_GET_ADDR(), LMU_BL_GET_MASK() and LMU_BL_GET_VAL() for this purpose.

This sounds suspiciously like you have hand rolled your own structure type and, bluntly, this strikes me as pretty crazy

What is wrong with struct { u8 addr; u8 mask; u8 val; }?

Quoted 8 lines

Data structure
--------------
ti_lmu_bl: Backlight output channel data
ti_lmu_bl_chip: Backlight device data. One device can have multiple
backlight channel data.
ti_lmu_bl_reg: Backlight device register data
ti_lmu_bl_cfg: Backlight configuration data for each LMU device

Quoted 4 lines
Cc Rob Herring <[email protected]>
Cc Sebastian Reichel <[email protected]>
Cc Tony Lindgren <[email protected]>

I've only done a very quick review of this patch since I'd prefer to see the above sorted out before I do a more detailed review.

However I did spot a couple of other small things that I might as well share now. Nevertheless please don't be suprised when further issues come out after you share v2!

Quoted 2 lines
---
 .../bindings/leds/backlight/ti-lmu-backlight.txt   |  65 ++

Device tree bindings should be sent in a seperate patch, see Documentation/devicetree/bindings/submitting-patches.txt .

Quoted 9 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +9 -0
+static int ti_lmu_backlight_enable(struct ti_lmu_bl *lmu_bl, int enable)
+{
+	struct ti_lmu_bl_chip *chip = lmu_bl->chip;
+	struct regmap *regmap = chip->lmu->regmap;
+	unsigned long enable_time = chip->cfg->reginfo->enable_usec;
+	u8 *reg = chip->cfg->reginfo->enable;
+	u8 mask = BIT(lmu_bl->bank_id);
+
+	if (!reg)

drivers/video/backlight/ti-lmu-backlight-core.c line 52

Quoted 10 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +10 -0
+		return -EINVAL;
+
+	if (enable)
+		return regmap_update_bits(regmap, *reg, mask, mask);
+	else
+		return regmap_update_bits(regmap, *reg, mask, 0);
+
+	if (enable_time > 0)
+		usleep_range(enable_time, enable_time + 100);
+}

That sleep is unreachable.

Quoted 13 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +13 -0
+
+static void ti_lmu_backlight_pwm_ctrl(struct ti_lmu_bl *lmu_bl, int brightness,
+				      int max_brightness)
+{
+	struct pwm_device *pwm;
+	unsigned int duty, period;
+
+	if (!lmu_bl->pwm) {
+		pwm = devm_pwm_get(lmu_bl->chip->dev, DEFAULT_PWM_NAME);
+		if (IS_ERR(pwm)) {
+			dev_err(lmu_bl->chip->dev,
+				"Can not get PWM device, err: %ld\n",
+				PTR_ERR(pwm));

drivers/video/backlight/ti-lmu-backlight-core.c line 75

Quoted 10 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +10 -0
+			return;
+		}
+
+		lmu_bl->pwm = pwm;
+
+		/*
+		 * FIXME: pwm_apply_args() should be removed when switching to
+		 * the atomic PWM API.
+		 */
+		pwm_apply_args(pwm);

What is a plan for this FIXME?

Quoted 4 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +4 -0
+	}
+
+	period = lmu_bl->pwm_period;
+	duty = brightness * period / max_brightness;

drivers/video/backlight/ti-lmu-backlight-core.c line 89

Quoted 10 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +10 -0
+
+	pwm_config(lmu_bl->pwm, duty, period);
+	if (duty)
+		pwm_enable(lmu_bl->pwm);
+	else
+		pwm_disable(lmu_bl->pwm);
+}
+
+static int ti_lmu_backlight_update_brightness_register(struct ti_lmu_bl *lmu_bl,
+						       int brightness)

This function appears to do a lot more than "update the brightness register". It seems like a lot of the logic at the top of this function belongs in the caller instead.

Quoted 28 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +28 -0
+{
+	const struct ti_lmu_bl_cfg *cfg = lmu_bl->chip->cfg;
+	const struct ti_lmu_bl_reg *reginfo = cfg->reginfo;
+	struct regmap *regmap = lmu_bl->chip->lmu->regmap;
+	u8 reg, val;
+	int ret;
+
+	if (lmu_bl->mode == BL_PWM_BASED) {
+		switch (cfg->pwm_action) {
+		case UPDATE_PWM_ONLY:
+			/* No register update is required */
+			return 0;
+		case UPDATE_MAX_BRT:
+			/*
+			 * PWM can start from any non-zero code and dim down
+			 * to zero. So, brightness register should be updated
+			 * even in PWM mode.
+			 */
+			if (brightness > 0)
+				brightness = MAX_BRIGHTNESS_11BIT;
+			else
+				brightness = 0;
+			break;
+		default:
+			break;
+		}
+	}
+

drivers/video/backlight/ti-lmu-backlight-core.c line 127

Quoted 10 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +10 -0
+	/*
+	 * Brightness register update
+	 *
+	 * 11 bit dimming: update LSB bits and write MSB byte.
+	 *		   MSB brightness should be shifted.
+	 *  8 bit dimming: write MSB byte.
+	 */
+
+	if (!reginfo->brightness_msb)
+		return -EINVAL;

Under what circumstances could brightness_msb be invalid?

If you're worried this might be unset this should have been checked (once) during registration.I could see would be inadequate error checking during registration...

Quoted 24 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +24 -0
+
+	if (cfg->max_brightness == MAX_BRIGHTNESS_11BIT) {
+		if (!reginfo->brightness_lsb)
+			return -EINVAL;
+
+		reg = reginfo->brightness_lsb[lmu_bl->bank_id];
+		ret = regmap_update_bits(regmap, reg,
+					 LMU_BACKLIGHT_11BIT_LSB_MASK,
+					 brightness);
+		if (ret)
+			return ret;
+
+		val = (brightness >> LMU_BACKLIGHT_11BIT_MSB_SHIFT) & 0xFF;
+	} else {
+		val = brightness & 0xFF;
+	}
+
+	reg = reginfo->brightness_msb[lmu_bl->bank_id];
+	return regmap_write(regmap, reg, val);
+}
+
+static int ti_lmu_backlight_update_status(struct backlight_device *bl_dev)
+{
+	struct ti_lmu_bl *lmu_bl = bl_get_data(bl_dev);

drivers/video/backlight/ti-lmu-backlight-core.c line 161

Quoted 10 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +10 -0
+	int brightness = bl_dev->props.brightness;
+	int ret;
+
+	if (bl_dev->props.state & BL_CORE_SUSPENDED)
+		brightness = 0;
+
+	if (brightness > 0)
+		ret = ti_lmu_backlight_enable(lmu_bl, 1);
+	else
+		ret = ti_lmu_backlight_enable(lmu_bl, 0);

ret = ti_lmu_backlight_enable(lmu_bl, brightness > 0);

Quoted 38 lines

[...]

drivers/video/backlight/ti-lmu-backlight-core.c new file +37 -0
+static struct ti_lmu_bl_chip *
+ti_lmu_backlight_register(struct device *dev, struct ti_lmu *lmu,
+			  const struct ti_lmu_bl_cfg *cfg)
+{
+	struct ti_lmu_bl_chip *chip;
+	struct ti_lmu_bl *each;
+	int i, ret;
+
+	if (!cfg) {
+		dev_err(dev, "Operation is not configured\n");
+		return ERR_PTR(-EINVAL);
+	}
+
+	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
+	if (!chip)
+		return ERR_PTR(-ENOMEM);
+
+	chip->dev = dev;
+	chip->lmu = lmu;
+	chip->cfg = cfg;
+
+	ret = ti_lmu_backlight_of_create(chip, dev->of_node);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ret = ti_lmu_backlight_init(chip);
+	if (ret) {
+		dev_err(dev, "Backlight init err: %d\n", ret);
+		return ERR_PTR(ret);
+	}
+
+	for (i = 0; i < chip->num_backlights; i++) {
+		each = chip->lmu_bl + i;
+
+		ret = ti_lmu_backlight_configure(each);
+		if (ret) {
+			dev_err(dev, "Backlight config err: %d\n", ret);

drivers/video/backlight/ti-lmu-backlight-core.c line 567

Quoted 10 lines
drivers/video/backlight/ti-lmu-backlight-core.c new file +10 -0
+			return ERR_PTR(ret);
+		}
+
+		ret = ti_lmu_backlight_add_device(dev, each);
+		if (ret) {
+			dev_err(dev, "Backlight device err: %d\n", ret);
+			return ERR_PTR(ret);
+		}
+
+		backlight_update_status(each->bl_dev);

Having asked why brightness_msb could ever by unset it is ironic to see the error code ignored here (so in the case it was unset we would spuriously have a successful probe anyway).

Quoted 1 line

[...]

Daniel.