// SPDX-License-Identifier: GPL-2.0-or-later /* * Driver for MPS MP3309C White LED driver with I2C interface * * This driver support both analog (by I2C commands) and PWM dimming control * modes. * * Copyright (C) 2023 ASEM Srl * Author: Flavio Suligoi <f.suligoi@asem.it> * * Based on pwm_bl.c
*/
staticint mp3309c_enable_device(struct mp3309c_chip *chip)
{
u8 reg_val; int ret;
/* I2C register #0 - Device enable */
ret = regmap_update_bits(chip->regmap, REG_I2C_0, REG_I2C_0_EN,
REG_I2C_0_EN); if (ret) return ret;
/* * I2C register #1 - Set working mode: * - enable/disable synchronous mode * - set overvoltage protection (OVP)
*/
reg_val = 0x00; if (chip->pdata->sync_mode)
reg_val |= REG_I2C_1_SYNC;
reg_val |= chip->pdata->over_voltage_protection;
ret = regmap_write(chip->regmap, REG_I2C_1, reg_val); if (ret) return ret;
return 0;
}
staticint mp3309c_bl_update_status(struct backlight_device *bl)
{ struct mp3309c_chip *chip = bl_get_data(bl); int brightness = backlight_get_brightness(bl); struct pwm_state pwmstate; unsignedint analog_val, bits_val; int i, ret;
if (chip->pdata->dimming_mode == DIMMING_PWM) { /* * PWM control mode
*/
pwm_get_state(chip->pwmd, &pwmstate);
pwm_set_relative_duty_cycle(&pwmstate,
chip->pdata->levels[brightness],
chip->pdata->levels[chip->pdata->max_brightness]);
pwmstate.enabled = true;
ret = pwm_apply_might_sleep(chip->pwmd, &pwmstate); if (ret) return ret;
switch (chip->pdata->status) { case FIRST_POWER_ON: case BACKLIGHT_OFF: /* * After 20ms of low pwm signal level, the chip turns * off automatically. In this case, before enabling the * chip again, we must wait about 10ms for pwm signal to * stabilize.
*/ if (brightness > 0) {
msleep(10);
mp3309c_enable_device(chip);
chip->pdata->status = BACKLIGHT_ON;
} else {
chip->pdata->status = BACKLIGHT_OFF;
} break; case BACKLIGHT_ON: if (brightness == 0)
chip->pdata->status = BACKLIGHT_OFF; break;
}
} else { /* * Analog (by I2C command) control mode * * The first time, before setting brightness, we must enable the * device
*/ if (chip->pdata->status == FIRST_POWER_ON)
mp3309c_enable_device(chip);
/* * Dimming mode I2C command (fixed dimming range 0..31) * * The 5 bits of the dimming analog value D4..D0 is allocated * in the I2C register #0, in the following way: * * +--+--+--+--+--+--+--+--+ * |EN|D0|D1|D2|D3|D4|XX|XX| * +--+--+--+--+--+--+--+--+
*/
analog_val = brightness;
bits_val = 0; for (i = 0; i <= 5; i++)
bits_val += ((analog_val >> i) & 0x01) << (6 - i);
ret = regmap_update_bits(chip->regmap, REG_I2C_0,
ANALOG_I2C_REG_MASK, bits_val); if (ret) return ret;
staticint mp3309c_parse_fwnode(struct mp3309c_chip *chip, struct mp3309c_platform_data *pdata)
{ int ret, i; unsignedint tmp_value; struct device *dev = chip->dev; int num_levels;
if (!dev_fwnode(dev)) return dev_err_probe(dev, -ENODEV, "failed to get firmware node\n");
/* * Dimming mode: the MP3309C provides two dimming control mode: * * - PWM mode * - Analog by I2C control mode (default) * * I2C control mode is assumed as default but, if the pwms property is * found in the backlight node, the mode switches to PWM mode.
*/
pdata->dimming_mode = DIMMING_ANALOG_I2C; if (device_property_present(dev, "pwms")) {
chip->pwmd = devm_pwm_get(dev, NULL); if (IS_ERR(chip->pwmd)) return dev_err_probe(dev, PTR_ERR(chip->pwmd), "error getting pwm data\n");
pdata->dimming_mode = DIMMING_PWM;
pwm_apply_args(chip->pwmd);
}
/* * In I2C control mode the dimming levels (0..31) are fixed by the * hardware, while in PWM control mode they can be chosen by the user, * to allow nonlinear mappings.
*/ if (pdata->dimming_mode == DIMMING_ANALOG_I2C) { /* * Analog (by I2C commands) control mode: fixed 0..31 brightness * levels
*/
num_levels = ANALOG_I2C_NUM_LEVELS;
/* Enable GPIO used in I2C dimming mode only */
chip->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH); if (IS_ERR(chip->enable_gpio)) return dev_err_probe(dev, PTR_ERR(chip->enable_gpio), "error getting enable gpio\n");
} else { /* * PWM control mode: check for brightness level in DT
*/ if (device_property_present(dev, "brightness-levels")) { /* Read brightness levels from DT */
num_levels = device_property_count_u32(dev, "brightness-levels"); if (num_levels < 2) return -EINVAL;
} else { /* Use default brightness levels */
num_levels = MP3309C_PWM_DEFAULT_NUM_LEVELS;
}
}
/* Fill brightness levels array */
pdata->levels = devm_kcalloc(dev, num_levels, sizeof(*pdata->levels), GFP_KERNEL); if (!pdata->levels) return -ENOMEM; if (device_property_present(dev, "brightness-levels")) {
ret = device_property_read_u32_array(dev, "brightness-levels",
pdata->levels, num_levels); if (ret < 0) return ret;
} else { for (i = 0; i < num_levels; i++)
pdata->levels[i] = i;
}
pdata->max_brightness = num_levels - 1;
ret = device_property_read_u32(dev, "default-brightness", &pdata->default_brightness); if (ret)
pdata->default_brightness = pdata->max_brightness; if (pdata->default_brightness > pdata->max_brightness) {
dev_err_probe(dev, -ERANGE, "default brightness exceeds max brightness\n");
pdata->default_brightness = pdata->max_brightness;
}
/* * Over-voltage protection (OVP) * * This (optional) property values are: * * - 13.5V * - 24V * - 35.5V (hardware default setting) * * If missing, the default value for OVP is 35.5V
*/
pdata->over_voltage_protection = REG_I2C_1_OVP1;
ret = device_property_read_u32(dev, "mps,overvoltage-protection-microvolt", &tmp_value); if (!ret) { switch (tmp_value) { case 13500000:
pdata->over_voltage_protection = 0x00; break; case 24000000:
pdata->over_voltage_protection = REG_I2C_1_OVP0; break; case 35500000:
pdata->over_voltage_protection = REG_I2C_1_OVP1; break; default: return -EINVAL;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.