tps6131x
static struct tps6131x *fled_cdev_to_tps6131x(struct led_classdev_flash *fled_cdev)
return container_of(fled_cdev, struct tps6131x, fled_cdev);
static int tps6131x_reset_chip(struct tps6131x *tps6131x)
if (tps6131x->reset_gpio) {
gpiod_set_value_cansleep(tps6131x->reset_gpio, 1);
gpiod_set_value_cansleep(tps6131x->reset_gpio, 0);
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_0, TPS6131X_REG_0_RESET,
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_0, TPS6131X_REG_0_RESET, 0);
static int tps6131x_init_chip(struct tps6131x *tps6131x)
val = tps6131x->valley_current_limit ? TPS6131X_REG_4_ILIM : 0;
ret = regmap_write(tps6131x->regmap, TPS6131X_REG_4, val);
if (tps6131x->chan1_en)
if (tps6131x->chan2_en)
if (tps6131x->chan3_en)
ret = regmap_write(tps6131x->regmap, TPS6131X_REG_5, val);
ret = regmap_write(tps6131x->regmap, TPS6131X_REG_6, val);
static int tps6131x_set_mode(struct tps6131x *tps6131x, enum tps6131x_mode mode, bool force)
return regmap_update_bits_base(tps6131x->regmap, TPS6131X_REG_1, TPS6131X_REG_1_MODE, val,
struct tps6131x *tps6131x = container_of(work, struct tps6131x, torch_refresh_work.work);
guard(mutex)(&tps6131x->lock);
ret = tps6131x_set_mode(tps6131x, TPS6131X_MODE_TORCH, true);
dev_err(tps6131x->dev, "Failed to refresh torch watchdog timer\n");
schedule_delayed_work(&tps6131x->torch_refresh_work,
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
cancel_delayed_work_sync(&tps6131x->torch_refresh_work);
steps_remaining = (brightness * tps6131x->step_torch_current_ma) / TPS6131X_TORCH_STEP_I_MA;
num_chans = tps6131x->chan1_en + tps6131x->chan2_en + tps6131x->chan3_en;
if (tps6131x->chan1_en)
if (tps6131x->chan3_en)
guard(mutex)(&tps6131x->lock);
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_0,
ret = tps6131x_set_mode(tps6131x, brightness ? TPS6131X_MODE_TORCH : TPS6131X_MODE_SHUTDOWN,
schedule_delayed_work(&tps6131x->torch_refresh_work,
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
guard(mutex)(&tps6131x->lock);
ret = tps6131x_set_mode(tps6131x, state ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
ret = regmap_update_bits_base(tps6131x->regmap, TPS6131X_REG_3, TPS6131X_REG_3_SFT,
ret = regmap_update_bits_base(tps6131x->regmap, TPS6131X_REG_3, TPS6131X_REG_3_SFT, 0, NULL,
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
num_chans = tps6131x->chan1_en + tps6131x->chan2_en + tps6131x->chan3_en;
if (tps6131x->chan1_en)
if (tps6131x->chan3_en)
guard(mutex)(&tps6131x->lock);
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_2, TPS6131X_REG_2_FC13,
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_1, TPS6131X_REG_1_FC2,
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
guard(mutex)(&tps6131x->lock);
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_3,
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
ret = regmap_read_bypassed(tps6131x->regmap, TPS6131X_REG_3, ®3);
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
ret = regmap_read_bypassed(tps6131x->regmap, TPS6131X_REG_3, ®3);
ret = regmap_read_bypassed(tps6131x->regmap, TPS6131X_REG_4, ®4);
ret = regmap_read_bypassed(tps6131x->regmap, TPS6131X_REG_6, ®6);
ret = regmap_update_bits_base(tps6131x->regmap, TPS6131X_REG_6,
static int tps6131x_parse_node(struct tps6131x *tps6131x)
struct device *dev = tps6131x->dev;
tps6131x->valley_current_limit = device_property_read_bool(dev, "ti,valley-current-limit");
tps6131x->led_node = device_get_next_child_node(dev, NULL);
if (!tps6131x->led_node) {
num_channels = fwnode_property_count_u32(tps6131x->led_node, "led-sources");
ret = fwnode_property_read_u32_array(tps6131x->led_node, "led-sources", channels,
tps6131x->chan1_en = true;
tps6131x->chan2_en = true;
tps6131x->chan3_en = true;
(tps6131x->chan1_en && tps6131x->chan3_en && !tps6131x->chan2_en) ? 2 : 1;
tps6131x->step_flash_current_ma = current_step_multiplier * TPS6131X_FLASH_STEP_I_MA;
tps6131x->step_torch_current_ma = current_step_multiplier * TPS6131X_TORCH_STEP_I_MA;
ret = fwnode_property_read_u32(tps6131x->led_node, "led-max-microamp", ¤t_ua);
tps6131x->max_torch_current_ma = UA_TO_MA(current_ua);
if (!tps6131x->max_torch_current_ma ||
tps6131x->max_torch_current_ma > max_current_torch_ma ||
(tps6131x->max_torch_current_ma % tps6131x->step_torch_current_ma)) {
tps6131x->step_torch_current_ma);
ret = fwnode_property_read_u32(tps6131x->led_node, "flash-max-microamp", ¤t_ua);
tps6131x->max_flash_current_ma = UA_TO_MA(current_ua);
if (!tps6131x->max_flash_current_ma ||
tps6131x->max_flash_current_ma > max_current_flash_ma ||
(tps6131x->max_flash_current_ma % tps6131x->step_flash_current_ma)) {
tps6131x->step_flash_current_ma);
ret = fwnode_property_read_u32(tps6131x->led_node, "flash-max-timeout-us", &timeout_us);
tps6131x->max_timeout_us = timer_config->time_us;
if (tps6131x->max_timeout_us != timeout_us)
tps6131x->max_timeout_us);
static int tps6131x_led_class_setup(struct tps6131x *tps6131x)
tps6131x->fled_cdev.ops = &flash_ops;
setting = &tps6131x->fled_cdev.timeout;
setting->max = tps6131x->max_timeout_us;
setting = &tps6131x->fled_cdev.brightness;
setting->min = tps6131x->step_flash_current_ma;
setting->max = tps6131x->max_flash_current_ma;
setting->step = tps6131x->step_flash_current_ma;
led_cdev = &tps6131x->fled_cdev.led_cdev;
led_cdev->max_brightness = tps6131x->max_torch_current_ma;
init_data.fwnode = tps6131x->led_node;
ret = devm_led_classdev_flash_register_ext(tps6131x->dev, &tps6131x->fled_cdev,
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev);
guard(mutex)(&tps6131x->lock);
return tps6131x_set_mode(tps6131x, enable ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN,
static int tps6131x_v4l2_setup(struct tps6131x *tps6131x)
intensity->min = tps6131x->step_torch_current_ma;
intensity->max = tps6131x->max_torch_current_ma;
intensity->step = tps6131x->step_torch_current_ma;
strscpy(v4l2_cfg.dev_name, tps6131x->fled_cdev.led_cdev.dev->kobj.name,
tps6131x->v4l2_flash = v4l2_flash_init(tps6131x->dev, tps6131x->led_node,
&tps6131x->fled_cdev, &tps6131x_v4l2_flash_ops,
if (IS_ERR(tps6131x->v4l2_flash)) {
dev_err(tps6131x->dev, "Failed to initialize v4l2 flash LED\n");
return PTR_ERR(tps6131x->v4l2_flash);
struct tps6131x *tps6131x;
tps6131x = devm_kzalloc(&client->dev, sizeof(*tps6131x), GFP_KERNEL);
if (!tps6131x)
tps6131x->dev = &client->dev;
i2c_set_clientdata(client, tps6131x);
mutex_init(&tps6131x->lock);
INIT_DELAYED_WORK(&tps6131x->torch_refresh_work, tps6131x_torch_refresh_handler);
ret = tps6131x_parse_node(tps6131x);
tps6131x->regmap = devm_regmap_init_i2c(client, &tps6131x_regmap);
if (IS_ERR(tps6131x->regmap)) {
ret = PTR_ERR(tps6131x->regmap);
tps6131x->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH);
if (IS_ERR(tps6131x->reset_gpio)) {
ret = PTR_ERR(tps6131x->reset_gpio);
ret = tps6131x_reset_chip(tps6131x);
ret = tps6131x_init_chip(tps6131x);
ret = tps6131x_led_class_setup(tps6131x);
ret = tps6131x_v4l2_setup(tps6131x);
struct tps6131x *tps6131x = i2c_get_clientdata(client);
v4l2_flash_release(tps6131x->v4l2_flash);
cancel_delayed_work_sync(&tps6131x->torch_refresh_work);