| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Texas Instruments TPS61310/TPS61311 flash LED driver with I2C interface |
| 4 |
* |
| 5 |
* Copyright 2025 Matthias Fend <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/gpio/consumer.h> |
| 9 |
#include <linux/i2c.h> |
| 10 |
#include <linux/led-class-flash.h> |
| 11 |
#include <linux/leds.h> |
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/regmap.h> |
| 14 |
#include <media/v4l2-flash-led-class.h> |
| 15 |
|
| 16 |
#define TPS6131X_REG_0 0x00 |
| 17 |
#define TPS6131X_REG_0_RESET BIT(7) |
| 18 |
#define TPS6131X_REG_0_DCLC13 GENMASK(5, 3) |
| 19 |
#define TPS6131X_REG_0_DCLC13_SHIFT 3 |
| 20 |
#define TPS6131X_REG_0_DCLC2 GENMASK(2, 0) |
| 21 |
#define TPS6131X_REG_0_DCLC2_SHIFT 0 |
| 22 |
|
| 23 |
#define TPS6131X_REG_1 0x01 |
| 24 |
#define TPS6131X_REG_1_MODE GENMASK(7, 6) |
| 25 |
#define TPS6131X_REG_1_MODE_SHIFT 6 |
| 26 |
#define TPS6131X_REG_1_FC2 GENMASK(5, 0) |
| 27 |
#define TPS6131X_REG_1_FC2_SHIFT 0 |
| 28 |
|
| 29 |
#define TPS6131X_REG_2 0x02 |
| 30 |
#define TPS6131X_REG_2_MODE GENMASK(7, 6) |
| 31 |
#define TPS6131X_REG_2_MODE_SHIFT 6 |
| 32 |
#define TPS6131X_REG_2_ENVM BIT(5) |
| 33 |
#define TPS6131X_REG_2_FC13 GENMASK(4, 0) |
| 34 |
#define TPS6131X_REG_2_FC13_SHIFT 0 |
| 35 |
|
| 36 |
#define TPS6131X_REG_3 0x03 |
| 37 |
#define TPS6131X_REG_3_STIM GENMASK(7, 5) |
| 38 |
#define TPS6131X_REG_3_STIM_SHIFT 5 |
| 39 |
#define TPS6131X_REG_3_HPFL BIT(4) |
| 40 |
#define TPS6131X_REG_3_SELSTIM_TO BIT(3) |
| 41 |
#define TPS6131X_REG_3_STT BIT(2) |
| 42 |
#define TPS6131X_REG_3_SFT BIT(1) |
| 43 |
#define TPS6131X_REG_3_TXMASK BIT(0) |
| 44 |
|
| 45 |
#define TPS6131X_REG_4 0x04 |
| 46 |
#define TPS6131X_REG_4_PG BIT(7) |
| 47 |
#define TPS6131X_REG_4_HOTDIE_HI BIT(6) |
| 48 |
#define TPS6131X_REG_4_HOTDIE_LO BIT(5) |
| 49 |
#define TPS6131X_REG_4_ILIM BIT(4) |
| 50 |
#define TPS6131X_REG_4_INDC GENMASK(3, 0) |
| 51 |
#define TPS6131X_REG_4_INDC_SHIFT 0 |
| 52 |
|
| 53 |
#define TPS6131X_REG_5 0x05 |
| 54 |
#define TPS6131X_REG_5_SELFCAL BIT(7) |
| 55 |
#define TPS6131X_REG_5_ENPSM BIT(6) |
| 56 |
#define TPS6131X_REG_5_STSTRB1_DIR BIT(5) |
| 57 |
#define TPS6131X_REG_5_GPIO BIT(4) |
| 58 |
#define TPS6131X_REG_5_GPIOTYPE BIT(3) |
| 59 |
#define TPS6131X_REG_5_ENLED3 BIT(2) |
| 60 |
#define TPS6131X_REG_5_ENLED2 BIT(1) |
| 61 |
#define TPS6131X_REG_5_ENLED1 BIT(0) |
| 62 |
|
| 63 |
#define TPS6131X_REG_6 0x06 |
| 64 |
#define TPS6131X_REG_6_ENTS BIT(7) |
| 65 |
#define TPS6131X_REG_6_LEDHOT BIT(6) |
| 66 |
#define TPS6131X_REG_6_LEDWARN BIT(5) |
| 67 |
#define TPS6131X_REG_6_LEDHDR BIT(4) |
| 68 |
#define TPS6131X_REG_6_OV GENMASK(3, 0) |
| 69 |
#define TPS6131X_REG_6_OV_SHIFT 0 |
| 70 |
|
| 71 |
#define TPS6131X_REG_7 0x07 |
| 72 |
#define TPS6131X_REG_7_ENBATMON BIT(7) |
| 73 |
#define TPS6131X_REG_7_BATDROOP GENMASK(6, 4) |
| 74 |
#define TPS6131X_REG_7_BATDROOP_SHIFT 4 |
| 75 |
#define TPS6131X_REG_7_REVID GENMASK(2, 0) |
| 76 |
#define TPS6131X_REG_7_REVID_SHIFT 0 |
| 77 |
|
| 78 |
#define TPS6131X_MAX_CHANNELS 3 |
| 79 |
|
| 80 |
#define TPS6131X_FLASH_MAX_I_CHAN13_MA 400 |
| 81 |
#define TPS6131X_FLASH_MAX_I_CHAN2_MA 800 |
| 82 |
#define TPS6131X_FLASH_STEP_I_MA 25 |
| 83 |
|
| 84 |
#define TPS6131X_TORCH_MAX_I_CHAN13_MA 175 |
| 85 |
#define TPS6131X_TORCH_MAX_I_CHAN2_MA 175 |
| 86 |
#define TPS6131X_TORCH_STEP_I_MA 25 |
| 87 |
|
| 88 |
/* The torch watchdog timer must be refreshed within an interval of 13 seconds. */ |
| 89 |
#define TPS6131X_TORCH_REFRESH_INTERVAL_JIFFIES msecs_to_jiffies(10000) |
| 90 |
|
| 91 |
#define UA_TO_MA(UA) ((UA) / 1000) |
| 92 |
|
| 93 |
enum tps6131x_mode { |
| 94 |
TPS6131X_MODE_SHUTDOWN = 0x0, |
| 95 |
TPS6131X_MODE_TORCH = 0x1, |
| 96 |
TPS6131X_MODE_FLASH = 0x2, |
| 97 |
}; |
| 98 |
|
| 99 |
struct tps6131x { |
| 100 |
struct device *dev; |
| 101 |
struct regmap *regmap; |
| 102 |
struct gpio_desc *reset_gpio; |
| 103 |
/* |
| 104 |
* Registers 0, 1, 2, and 3 control parts of the controller that are not completely |
| 105 |
* independent of each other. Since some operations require the registers to be written in |
| 106 |
* a specific order to avoid unwanted side effects, they are synchronized with a lock. |
| 107 |
*/ |
| 108 |
struct mutex lock; /* Hardware access lock for register 0, 1, 2 and 3 */ |
| 109 |
struct delayed_work torch_refresh_work; |
| 110 |
bool valley_current_limit; |
| 111 |
bool chan1_en; |
| 112 |
bool chan2_en; |
| 113 |
bool chan3_en; |
| 114 |
struct fwnode_handle *led_node; |
| 115 |
u32 max_flash_current_ma; |
| 116 |
u32 step_flash_current_ma; |
| 117 |
u32 max_torch_current_ma; |
| 118 |
u32 step_torch_current_ma; |
| 119 |
u32 max_timeout_us; |
| 120 |
struct led_classdev_flash fled_cdev; |
| 121 |
struct v4l2_flash *v4l2_flash; |
| 122 |
}; |
| 123 |
|
| 124 |
static struct tps6131x *fled_cdev_to_tps6131x(struct led_classdev_flash *fled_cdev) |
| 125 |
{ |
| 126 |
return container_of(fled_cdev, struct tps6131x, fled_cdev); |
| 127 |
} |
| 128 |
|
| 129 |
/* |
| 130 |
* Register contents after a power on/reset. These values cannot be changed. |
| 131 |
*/ |
| 132 |
|
| 133 |
#define TPS6131X_DCLC2_50MA 2 |
| 134 |
#define TPS6131X_DCLC13_25MA 1 |
| 135 |
#define TPS6131X_FC2_400MA 16 |
| 136 |
#define TPS6131X_FC13_200MA 8 |
| 137 |
#define TPS6131X_STIM_0_579MS_1_37MS 6 |
| 138 |
#define TPS6131X_SELSTIM_RANGE0 0 |
| 139 |
#define TPS6131X_INDC_OFF 0 |
| 140 |
#define TPS6131X_OV_4950MV 9 |
| 141 |
#define TPS6131X_BATDROOP_150MV 4 |
| 142 |
|
| 143 |
static const struct reg_default tps6131x_regmap_defaults[] = { |
| 144 |
{ TPS6131X_REG_0, (TPS6131X_DCLC13_25MA << TPS6131X_REG_0_DCLC13_SHIFT) | |
| 145 |
(TPS6131X_DCLC2_50MA << TPS6131X_REG_0_DCLC2_SHIFT) }, |
| 146 |
{ TPS6131X_REG_1, (TPS6131X_MODE_SHUTDOWN << TPS6131X_REG_1_MODE_SHIFT) | |
| 147 |
(TPS6131X_FC2_400MA << TPS6131X_REG_1_FC2_SHIFT) }, |
| 148 |
{ TPS6131X_REG_2, (TPS6131X_MODE_SHUTDOWN << TPS6131X_REG_2_MODE_SHIFT) | |
| 149 |
(TPS6131X_FC13_200MA << TPS6131X_REG_2_FC13_SHIFT) }, |
| 150 |
{ TPS6131X_REG_3, (TPS6131X_STIM_0_579MS_1_37MS << TPS6131X_REG_3_STIM_SHIFT) | |
| 151 |
(TPS6131X_SELSTIM_RANGE0 << TPS6131X_REG_3_SELSTIM_TO) | |
| 152 |
TPS6131X_REG_3_TXMASK }, |
| 153 |
{ TPS6131X_REG_4, (TPS6131X_INDC_OFF << TPS6131X_REG_4_INDC_SHIFT) }, |
| 154 |
{ TPS6131X_REG_5, TPS6131X_REG_5_ENPSM | TPS6131X_REG_5_STSTRB1_DIR | |
| 155 |
TPS6131X_REG_5_GPIOTYPE | TPS6131X_REG_5_ENLED2 }, |
| 156 |
{ TPS6131X_REG_6, (TPS6131X_OV_4950MV << TPS6131X_REG_6_OV_SHIFT) }, |
| 157 |
{ TPS6131X_REG_7, (TPS6131X_BATDROOP_150MV << TPS6131X_REG_7_BATDROOP_SHIFT) }, |
| 158 |
}; |
| 159 |
|
| 160 |
/* |
| 161 |
* These registers contain flags that are reset when read. |
| 162 |
*/ |
| 163 |
static bool tps6131x_regmap_precious(struct device *dev, unsigned int reg) |
| 164 |
{ |
| 165 |
switch (reg) { |
| 166 |
case TPS6131X_REG_3: |
| 167 |
case TPS6131X_REG_4: |
| 168 |
case TPS6131X_REG_6: |
| 169 |
return true; |
| 170 |
default: |
| 171 |
return false; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
static const struct regmap_config tps6131x_regmap = { |
| 176 |
.reg_bits = 8, |
| 177 |
.val_bits = 8, |
| 178 |
.max_register = TPS6131X_REG_7, |
| 179 |
.reg_defaults = tps6131x_regmap_defaults, |
| 180 |
.num_reg_defaults = ARRAY_SIZE(tps6131x_regmap_defaults), |
| 181 |
.cache_type = REGCACHE_FLAT, |
| 182 |
.precious_reg = &tps6131x_regmap_precious, |
| 183 |
}; |
| 184 |
|
| 185 |
struct tps6131x_timer_config { |
| 186 |
u8 val; |
| 187 |
u8 range; |
| 188 |
u32 time_us; |
| 189 |
}; |
| 190 |
|
| 191 |
static const struct tps6131x_timer_config tps6131x_timer_configs[] = { |
| 192 |
{ .val = 0, .range = 1, .time_us = 5300 }, |
| 193 |
{ .val = 1, .range = 1, .time_us = 10700 }, |
| 194 |
{ .val = 2, .range = 1, .time_us = 16000 }, |
| 195 |
{ .val = 3, .range = 1, .time_us = 21300 }, |
| 196 |
{ .val = 4, .range = 1, .time_us = 26600 }, |
| 197 |
{ .val = 5, .range = 1, .time_us = 32000 }, |
| 198 |
{ .val = 6, .range = 1, .time_us = 37300 }, |
| 199 |
{ .val = 0, .range = 0, .time_us = 68200 }, |
| 200 |
{ .val = 7, .range = 1, .time_us = 71500 }, |
| 201 |
{ .val = 1, .range = 0, .time_us = 102200 }, |
| 202 |
{ .val = 2, .range = 0, .time_us = 136300 }, |
| 203 |
{ .val = 3, .range = 0, .time_us = 170400 }, |
| 204 |
{ .val = 4, .range = 0, .time_us = 204500 }, |
| 205 |
{ .val = 5, .range = 0, .time_us = 340800 }, |
| 206 |
{ .val = 6, .range = 0, .time_us = 579300 }, |
| 207 |
{ .val = 7, .range = 0, .time_us = 852000 }, |
| 208 |
}; |
| 209 |
|
| 210 |
static const struct tps6131x_timer_config *tps6131x_find_closest_timer_config(u32 timeout_us) |
| 211 |
{ |
| 212 |
const struct tps6131x_timer_config *timer_config = &tps6131x_timer_configs[0]; |
| 213 |
u32 diff, min_diff = U32_MAX; |
| 214 |
int i; |
| 215 |
|
| 216 |
for (i = 0; i < ARRAY_SIZE(tps6131x_timer_configs); i++) { |
| 217 |
diff = abs(tps6131x_timer_configs[i].time_us - timeout_us); |
| 218 |
if (diff < min_diff) { |
| 219 |
timer_config = &tps6131x_timer_configs[i]; |
| 220 |
min_diff = diff; |
| 221 |
if (!min_diff) |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
return timer_config; |
| 227 |
} |
| 228 |
|
| 229 |
static int tps6131x_reset_chip(struct tps6131x *tps6131x) |
| 230 |
{ |
| 231 |
int ret; |
| 232 |
|
| 233 |
if (tps6131x->reset_gpio) { |
| 234 |
gpiod_set_value_cansleep(tps6131x->reset_gpio, 1); |
| 235 |
fsleep(10); |
| 236 |
gpiod_set_value_cansleep(tps6131x->reset_gpio, 0); |
| 237 |
fsleep(100); |
| 238 |
} else { |
| 239 |
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_0, TPS6131X_REG_0_RESET, |
| 240 |
TPS6131X_REG_0_RESET); |
| 241 |
if (ret) |
| 242 |
return ret; |
| 243 |
|
| 244 |
fsleep(100); |
| 245 |
|
| 246 |
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_0, TPS6131X_REG_0_RESET, 0); |
| 247 |
if (ret) |
| 248 |
return ret; |
| 249 |
} |
| 250 |
|
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
static int tps6131x_init_chip(struct tps6131x *tps6131x) |
| 255 |
{ |
| 256 |
u32 val; |
| 257 |
int ret; |
| 258 |
|
| 259 |
val = tps6131x->valley_current_limit ? TPS6131X_REG_4_ILIM : 0; |
| 260 |
|
| 261 |
ret = regmap_write(tps6131x->regmap, TPS6131X_REG_4, val); |
| 262 |
if (ret) |
| 263 |
return ret; |
| 264 |
|
| 265 |
val = TPS6131X_REG_5_ENPSM | TPS6131X_REG_5_STSTRB1_DIR | TPS6131X_REG_5_GPIOTYPE; |
| 266 |
|
| 267 |
if (tps6131x->chan1_en) |
| 268 |
val |= TPS6131X_REG_5_ENLED1; |
| 269 |
|
| 270 |
if (tps6131x->chan2_en) |
| 271 |
val |= TPS6131X_REG_5_ENLED2; |
| 272 |
|
| 273 |
if (tps6131x->chan3_en) |
| 274 |
val |= TPS6131X_REG_5_ENLED3; |
| 275 |
|
| 276 |
ret = regmap_write(tps6131x->regmap, TPS6131X_REG_5, val); |
| 277 |
if (ret) |
| 278 |
return ret; |
| 279 |
|
| 280 |
val = TPS6131X_REG_6_ENTS; |
| 281 |
|
| 282 |
ret = regmap_write(tps6131x->regmap, TPS6131X_REG_6, val); |
| 283 |
if (ret) |
| 284 |
return ret; |
| 285 |
|
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
static int tps6131x_set_mode(struct tps6131x *tps6131x, enum tps6131x_mode mode, bool force) |
| 290 |
{ |
| 291 |
u8 val = mode << TPS6131X_REG_1_MODE_SHIFT; |
| 292 |
|
| 293 |
return regmap_update_bits_base(tps6131x->regmap, TPS6131X_REG_1, TPS6131X_REG_1_MODE, val, |
| 294 |
NULL, false, force); |
| 295 |
} |
| 296 |
|
| 297 |
static void tps6131x_torch_refresh_handler(struct work_struct *work) |
| 298 |
{ |
| 299 |
struct tps6131x *tps6131x = container_of(work, struct tps6131x, torch_refresh_work.work); |
| 300 |
int ret; |
| 301 |
|
| 302 |
guard(mutex)(&tps6131x->lock); |
| 303 |
|
| 304 |
ret = tps6131x_set_mode(tps6131x, TPS6131X_MODE_TORCH, true); |
| 305 |
if (ret < 0) { |
| 306 |
dev_err(tps6131x->dev, "Failed to refresh torch watchdog timer\n"); |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
schedule_delayed_work(&tps6131x->torch_refresh_work, |
| 311 |
TPS6131X_TORCH_REFRESH_INTERVAL_JIFFIES); |
| 312 |
} |
| 313 |
|
| 314 |
static int tps6131x_brightness_set(struct led_classdev *cdev, enum led_brightness brightness) |
| 315 |
{ |
| 316 |
struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev); |
| 317 |
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev); |
| 318 |
u32 num_chans, steps_chan13, steps_chan2, steps_remaining; |
| 319 |
u8 reg0; |
| 320 |
int ret; |
| 321 |
|
| 322 |
cancel_delayed_work_sync(&tps6131x->torch_refresh_work); |
| 323 |
|
| 324 |
/* |
| 325 |
* The brightness parameter uses the number of current steps as the unit (not the current |
| 326 |
* value itself). Since the reported step size can vary depending on the configuration, |
| 327 |
* this value must be converted into actual register steps. |
| 328 |
*/ |
| 329 |
steps_remaining = (brightness * tps6131x->step_torch_current_ma) / TPS6131X_TORCH_STEP_I_MA; |
| 330 |
|
| 331 |
num_chans = tps6131x->chan1_en + tps6131x->chan2_en + tps6131x->chan3_en; |
| 332 |
|
| 333 |
/* |
| 334 |
* The currents are distributed as evenly as possible across the activated channels. |
| 335 |
* Since channels 1 and 3 share the same register setting, they always use the same current |
| 336 |
* value. Channel 2 supports higher currents and thus takes over the remaining additional |
| 337 |
* portion that cannot be covered by the other channels. |
| 338 |
*/ |
| 339 |
steps_chan13 = min_t(u32, steps_remaining / num_chans, |
| 340 |
TPS6131X_TORCH_MAX_I_CHAN13_MA / TPS6131X_TORCH_STEP_I_MA); |
| 341 |
if (tps6131x->chan1_en) |
| 342 |
steps_remaining -= steps_chan13; |
| 343 |
if (tps6131x->chan3_en) |
| 344 |
steps_remaining -= steps_chan13; |
| 345 |
|
| 346 |
steps_chan2 = min_t(u32, steps_remaining, |
| 347 |
TPS6131X_TORCH_MAX_I_CHAN2_MA / TPS6131X_TORCH_STEP_I_MA); |
| 348 |
|
| 349 |
guard(mutex)(&tps6131x->lock); |
| 350 |
|
| 351 |
reg0 = (steps_chan13 << TPS6131X_REG_0_DCLC13_SHIFT) | |
| 352 |
(steps_chan2 << TPS6131X_REG_0_DCLC2_SHIFT); |
| 353 |
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_0, |
| 354 |
TPS6131X_REG_0_DCLC13 | TPS6131X_REG_0_DCLC2, reg0); |
| 355 |
if (ret < 0) |
| 356 |
return ret; |
| 357 |
|
| 358 |
ret = tps6131x_set_mode(tps6131x, brightness ? TPS6131X_MODE_TORCH : TPS6131X_MODE_SHUTDOWN, |
| 359 |
true); |
| 360 |
if (ret < 0) |
| 361 |
return ret; |
| 362 |
|
| 363 |
/* |
| 364 |
* In order to use both the flash and the video light functions purely via the I2C |
| 365 |
* interface, STRB1 must be low. If STRB1 is low, then the video light watchdog timer |
| 366 |
* is also active, which puts the device into the shutdown state after around 13 seconds. |
| 367 |
* To prevent this, the mode must be refreshed within the watchdog timeout. |
| 368 |
*/ |
| 369 |
if (brightness) |
| 370 |
schedule_delayed_work(&tps6131x->torch_refresh_work, |
| 371 |
TPS6131X_TORCH_REFRESH_INTERVAL_JIFFIES); |
| 372 |
|
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
static int tps6131x_strobe_set(struct led_classdev_flash *fled_cdev, bool state) |
| 377 |
{ |
| 378 |
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev); |
| 379 |
int ret; |
| 380 |
|
| 381 |
guard(mutex)(&tps6131x->lock); |
| 382 |
|
| 383 |
ret = tps6131x_set_mode(tps6131x, state ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN, |
| 384 |
true); |
| 385 |
if (ret < 0) |
| 386 |
return ret; |
| 387 |
|
| 388 |
if (state) { |
| 389 |
ret = regmap_update_bits_base(tps6131x->regmap, TPS6131X_REG_3, TPS6131X_REG_3_SFT, |
| 390 |
TPS6131X_REG_3_SFT, NULL, false, true); |
| 391 |
if (ret) |
| 392 |
return ret; |
| 393 |
} |
| 394 |
|
| 395 |
ret = regmap_update_bits_base(tps6131x->regmap, TPS6131X_REG_3, TPS6131X_REG_3_SFT, 0, NULL, |
| 396 |
false, true); |
| 397 |
if (ret) |
| 398 |
return ret; |
| 399 |
|
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
static int tps6131x_flash_brightness_set(struct led_classdev_flash *fled_cdev, u32 brightness) |
| 404 |
{ |
| 405 |
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev); |
| 406 |
u32 num_chans; |
| 407 |
u32 steps_chan13, steps_chan2; |
| 408 |
u32 steps_remaining; |
| 409 |
int ret; |
| 410 |
|
| 411 |
steps_remaining = brightness / TPS6131X_FLASH_STEP_I_MA; |
| 412 |
num_chans = tps6131x->chan1_en + tps6131x->chan2_en + tps6131x->chan3_en; |
| 413 |
steps_chan13 = min_t(u32, steps_remaining / num_chans, |
| 414 |
TPS6131X_FLASH_MAX_I_CHAN13_MA / TPS6131X_FLASH_STEP_I_MA); |
| 415 |
if (tps6131x->chan1_en) |
| 416 |
steps_remaining -= steps_chan13; |
| 417 |
if (tps6131x->chan3_en) |
| 418 |
steps_remaining -= steps_chan13; |
| 419 |
steps_chan2 = min_t(u32, steps_remaining, |
| 420 |
TPS6131X_FLASH_MAX_I_CHAN2_MA / TPS6131X_FLASH_STEP_I_MA); |
| 421 |
|
| 422 |
guard(mutex)(&tps6131x->lock); |
| 423 |
|
| 424 |
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_2, TPS6131X_REG_2_FC13, |
| 425 |
steps_chan13 << TPS6131X_REG_2_FC13_SHIFT); |
| 426 |
if (ret < 0) |
| 427 |
return ret; |
| 428 |
|
| 429 |
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_1, TPS6131X_REG_1_FC2, |
| 430 |
steps_chan2 << TPS6131X_REG_1_FC2_SHIFT); |
| 431 |
if (ret < 0) |
| 432 |
return ret; |
| 433 |
|
| 434 |
fled_cdev->brightness.val = brightness; |
| 435 |
|
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
static int tps6131x_flash_timeout_set(struct led_classdev_flash *fled_cdev, u32 timeout_us) |
| 440 |
{ |
| 441 |
const struct tps6131x_timer_config *timer_config; |
| 442 |
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev); |
| 443 |
u8 reg3; |
| 444 |
int ret; |
| 445 |
|
| 446 |
guard(mutex)(&tps6131x->lock); |
| 447 |
|
| 448 |
timer_config = tps6131x_find_closest_timer_config(timeout_us); |
| 449 |
|
| 450 |
reg3 = timer_config->val << TPS6131X_REG_3_STIM_SHIFT; |
| 451 |
if (timer_config->range) |
| 452 |
reg3 |= TPS6131X_REG_3_SELSTIM_TO; |
| 453 |
|
| 454 |
ret = regmap_update_bits(tps6131x->regmap, TPS6131X_REG_3, |
| 455 |
TPS6131X_REG_3_STIM | TPS6131X_REG_3_SELSTIM_TO, reg3); |
| 456 |
if (ret < 0) |
| 457 |
return ret; |
| 458 |
|
| 459 |
fled_cdev->timeout.val = timer_config->time_us; |
| 460 |
|
| 461 |
return 0; |
| 462 |
} |
| 463 |
|
| 464 |
static int tps6131x_strobe_get(struct led_classdev_flash *fled_cdev, bool *state) |
| 465 |
{ |
| 466 |
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev); |
| 467 |
unsigned int reg3; |
| 468 |
int ret; |
| 469 |
|
| 470 |
ret = regmap_read_bypassed(tps6131x->regmap, TPS6131X_REG_3, ®3); |
| 471 |
if (ret) |
| 472 |
return ret; |
| 473 |
|
| 474 |
*state = !!(reg3 & TPS6131X_REG_3_SFT); |
| 475 |
|
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
static int tps6131x_flash_fault_get(struct led_classdev_flash *fled_cdev, u32 *fault) |
| 480 |
{ |
| 481 |
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev); |
| 482 |
unsigned int reg3, reg4, reg6; |
| 483 |
int ret; |
| 484 |
|
| 485 |
*fault = 0; |
| 486 |
|
| 487 |
ret = regmap_read_bypassed(tps6131x->regmap, TPS6131X_REG_3, ®3); |
| 488 |
if (ret < 0) |
| 489 |
return ret; |
| 490 |
|
| 491 |
ret = regmap_read_bypassed(tps6131x->regmap, TPS6131X_REG_4, ®4); |
| 492 |
if (ret < 0) |
| 493 |
return ret; |
| 494 |
|
| 495 |
ret = regmap_read_bypassed(tps6131x->regmap, TPS6131X_REG_6, ®6); |
| 496 |
if (ret < 0) |
| 497 |
return ret; |
| 498 |
|
| 499 |
if (reg3 & TPS6131X_REG_3_HPFL) |
| 500 |
*fault |= LED_FAULT_SHORT_CIRCUIT; |
| 501 |
|
| 502 |
if (reg3 & TPS6131X_REG_3_SELSTIM_TO) |
| 503 |
*fault |= LED_FAULT_TIMEOUT; |
| 504 |
|
| 505 |
if (reg4 & TPS6131X_REG_4_HOTDIE_HI) |
| 506 |
*fault |= LED_FAULT_OVER_TEMPERATURE; |
| 507 |
|
| 508 |
if (reg6 & (TPS6131X_REG_6_LEDHOT | TPS6131X_REG_6_LEDWARN)) |
| 509 |
*fault |= LED_FAULT_LED_OVER_TEMPERATURE; |
| 510 |
|
| 511 |
if (!(reg6 & TPS6131X_REG_6_LEDHDR)) |
| 512 |
*fault |= LED_FAULT_UNDER_VOLTAGE; |
| 513 |
|
| 514 |
if (reg6 & TPS6131X_REG_6_LEDHOT) { |
| 515 |
ret = regmap_update_bits_base(tps6131x->regmap, TPS6131X_REG_6, |
| 516 |
TPS6131X_REG_6_LEDHOT, 0, NULL, false, true); |
| 517 |
if (ret < 0) |
| 518 |
return ret; |
| 519 |
} |
| 520 |
|
| 521 |
return 0; |
| 522 |
} |
| 523 |
|
| 524 |
static const struct led_flash_ops flash_ops = { |
| 525 |
.flash_brightness_set = tps6131x_flash_brightness_set, |
| 526 |
.strobe_set = tps6131x_strobe_set, |
| 527 |
.strobe_get = tps6131x_strobe_get, |
| 528 |
.timeout_set = tps6131x_flash_timeout_set, |
| 529 |
.fault_get = tps6131x_flash_fault_get, |
| 530 |
}; |
| 531 |
|
| 532 |
static int tps6131x_parse_node(struct tps6131x *tps6131x) |
| 533 |
{ |
| 534 |
const struct tps6131x_timer_config *timer_config; |
| 535 |
struct device *dev = tps6131x->dev; |
| 536 |
u32 channels[TPS6131X_MAX_CHANNELS]; |
| 537 |
u32 current_step_multiplier; |
| 538 |
u32 current_ua; |
| 539 |
u32 max_current_flash_ma, max_current_torch_ma; |
| 540 |
u32 timeout_us; |
| 541 |
int num_channels; |
| 542 |
int i; |
| 543 |
int ret; |
| 544 |
|
| 545 |
tps6131x->valley_current_limit = device_property_read_bool(dev, "ti,valley-current-limit"); |
| 546 |
|
| 547 |
tps6131x->led_node = device_get_next_child_node(dev, NULL); |
| 548 |
if (!tps6131x->led_node) { |
| 549 |
dev_err(dev, "Missing LED node\n"); |
| 550 |
return -EINVAL; |
| 551 |
} |
| 552 |
|
| 553 |
num_channels = fwnode_property_count_u32(tps6131x->led_node, "led-sources"); |
| 554 |
if (num_channels <= 0) { |
| 555 |
dev_err(dev, "Failed to read led-sources property\n"); |
| 556 |
return -EINVAL; |
| 557 |
} |
| 558 |
|
| 559 |
if (num_channels > TPS6131X_MAX_CHANNELS) { |
| 560 |
dev_err(dev, "led-sources count %u exceeds maximum channel count %u\n", |
| 561 |
num_channels, TPS6131X_MAX_CHANNELS); |
| 562 |
return -EINVAL; |
| 563 |
} |
| 564 |
|
| 565 |
ret = fwnode_property_read_u32_array(tps6131x->led_node, "led-sources", channels, |
| 566 |
num_channels); |
| 567 |
if (ret < 0) { |
| 568 |
dev_err(dev, "Failed to read led-sources property\n"); |
| 569 |
return ret; |
| 570 |
} |
| 571 |
|
| 572 |
max_current_flash_ma = 0; |
| 573 |
max_current_torch_ma = 0; |
| 574 |
for (i = 0; i < num_channels; i++) { |
| 575 |
switch (channels[i]) { |
| 576 |
case 1: |
| 577 |
tps6131x->chan1_en = true; |
| 578 |
max_current_flash_ma += TPS6131X_FLASH_MAX_I_CHAN13_MA; |
| 579 |
max_current_torch_ma += TPS6131X_TORCH_MAX_I_CHAN13_MA; |
| 580 |
break; |
| 581 |
case 2: |
| 582 |
tps6131x->chan2_en = true; |
| 583 |
max_current_flash_ma += TPS6131X_FLASH_MAX_I_CHAN2_MA; |
| 584 |
max_current_torch_ma += TPS6131X_TORCH_MAX_I_CHAN2_MA; |
| 585 |
break; |
| 586 |
case 3: |
| 587 |
tps6131x->chan3_en = true; |
| 588 |
max_current_flash_ma += TPS6131X_FLASH_MAX_I_CHAN13_MA; |
| 589 |
max_current_torch_ma += TPS6131X_TORCH_MAX_I_CHAN13_MA; |
| 590 |
break; |
| 591 |
default: |
| 592 |
dev_err(dev, "led-source out of range [1-3]\n"); |
| 593 |
return -EINVAL; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
/* |
| 598 |
* If only channels 1 and 3 are used, the step size is doubled because the two channels |
| 599 |
* share the same current control register. |
| 600 |
*/ |
| 601 |
current_step_multiplier = |
| 602 |
(tps6131x->chan1_en && tps6131x->chan3_en && !tps6131x->chan2_en) ? 2 : 1; |
| 603 |
tps6131x->step_flash_current_ma = current_step_multiplier * TPS6131X_FLASH_STEP_I_MA; |
| 604 |
tps6131x->step_torch_current_ma = current_step_multiplier * TPS6131X_TORCH_STEP_I_MA; |
| 605 |
|
| 606 |
ret = fwnode_property_read_u32(tps6131x->led_node, "led-max-microamp", ¤t_ua); |
| 607 |
if (ret < 0) { |
| 608 |
dev_err(dev, "Failed to read led-max-microamp property\n"); |
| 609 |
return ret; |
| 610 |
} |
| 611 |
|
| 612 |
tps6131x->max_torch_current_ma = UA_TO_MA(current_ua); |
| 613 |
|
| 614 |
if (!tps6131x->max_torch_current_ma || |
| 615 |
tps6131x->max_torch_current_ma > max_current_torch_ma || |
| 616 |
(tps6131x->max_torch_current_ma % tps6131x->step_torch_current_ma)) { |
| 617 |
dev_err(dev, "led-max-microamp out of range or not a multiple of %u\n", |
| 618 |
tps6131x->step_torch_current_ma); |
| 619 |
return -EINVAL; |
| 620 |
} |
| 621 |
|
| 622 |
ret = fwnode_property_read_u32(tps6131x->led_node, "flash-max-microamp", ¤t_ua); |
| 623 |
if (ret < 0) { |
| 624 |
dev_err(dev, "Failed to read flash-max-microamp property\n"); |
| 625 |
return ret; |
| 626 |
} |
| 627 |
|
| 628 |
tps6131x->max_flash_current_ma = UA_TO_MA(current_ua); |
| 629 |
|
| 630 |
if (!tps6131x->max_flash_current_ma || |
| 631 |
tps6131x->max_flash_current_ma > max_current_flash_ma || |
| 632 |
(tps6131x->max_flash_current_ma % tps6131x->step_flash_current_ma)) { |
| 633 |
dev_err(dev, "flash-max-microamp out of range or not a multiple of %u\n", |
| 634 |
tps6131x->step_flash_current_ma); |
| 635 |
return -EINVAL; |
| 636 |
} |
| 637 |
|
| 638 |
ret = fwnode_property_read_u32(tps6131x->led_node, "flash-max-timeout-us", &timeout_us); |
| 639 |
if (ret < 0) { |
| 640 |
dev_err(dev, "Failed to read flash-max-timeout-us property\n"); |
| 641 |
return ret; |
| 642 |
} |
| 643 |
|
| 644 |
timer_config = tps6131x_find_closest_timer_config(timeout_us); |
| 645 |
tps6131x->max_timeout_us = timer_config->time_us; |
| 646 |
|
| 647 |
if (tps6131x->max_timeout_us != timeout_us) |
| 648 |
dev_warn(dev, "flash-max-timeout-us %u not supported (using %u)\n", timeout_us, |
| 649 |
tps6131x->max_timeout_us); |
| 650 |
|
| 651 |
return 0; |
| 652 |
} |
| 653 |
|
| 654 |
static int tps6131x_led_class_setup(struct tps6131x *tps6131x) |
| 655 |
{ |
| 656 |
const struct tps6131x_timer_config *timer_config; |
| 657 |
struct led_classdev *led_cdev; |
| 658 |
struct led_flash_setting *setting; |
| 659 |
struct led_init_data init_data = {}; |
| 660 |
int ret; |
| 661 |
|
| 662 |
tps6131x->fled_cdev.ops = &flash_ops; |
| 663 |
|
| 664 |
setting = &tps6131x->fled_cdev.timeout; |
| 665 |
timer_config = tps6131x_find_closest_timer_config(0); |
| 666 |
setting->min = timer_config->time_us; |
| 667 |
setting->max = tps6131x->max_timeout_us; |
| 668 |
setting->step = 1; /* Only some specific time periods are supported. No fixed step size. */ |
| 669 |
setting->val = setting->min; |
| 670 |
|
| 671 |
setting = &tps6131x->fled_cdev.brightness; |
| 672 |
setting->min = tps6131x->step_flash_current_ma; |
| 673 |
setting->max = tps6131x->max_flash_current_ma; |
| 674 |
setting->step = tps6131x->step_flash_current_ma; |
| 675 |
setting->val = setting->min; |
| 676 |
|
| 677 |
led_cdev = &tps6131x->fled_cdev.led_cdev; |
| 678 |
led_cdev->brightness_set_blocking = tps6131x_brightness_set; |
| 679 |
led_cdev->max_brightness = tps6131x->max_torch_current_ma; |
| 680 |
led_cdev->flags |= LED_DEV_CAP_FLASH; |
| 681 |
|
| 682 |
init_data.fwnode = tps6131x->led_node; |
| 683 |
init_data.devicename = NULL; |
| 684 |
init_data.default_label = NULL; |
| 685 |
init_data.devname_mandatory = false; |
| 686 |
|
| 687 |
ret = devm_led_classdev_flash_register_ext(tps6131x->dev, &tps6131x->fled_cdev, |
| 688 |
&init_data); |
| 689 |
if (ret) |
| 690 |
return ret; |
| 691 |
|
| 692 |
return 0; |
| 693 |
} |
| 694 |
|
| 695 |
static int tps6131x_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, bool enable) |
| 696 |
{ |
| 697 |
struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev; |
| 698 |
struct tps6131x *tps6131x = fled_cdev_to_tps6131x(fled_cdev); |
| 699 |
|
| 700 |
guard(mutex)(&tps6131x->lock); |
| 701 |
|
| 702 |
return tps6131x_set_mode(tps6131x, enable ? TPS6131X_MODE_FLASH : TPS6131X_MODE_SHUTDOWN, |
| 703 |
false); |
| 704 |
} |
| 705 |
|
| 706 |
static const struct v4l2_flash_ops tps6131x_v4l2_flash_ops = { |
| 707 |
.external_strobe_set = tps6131x_flash_external_strobe_set, |
| 708 |
}; |
| 709 |
|
| 710 |
static int tps6131x_v4l2_setup(struct tps6131x *tps6131x) |
| 711 |
{ |
| 712 |
struct v4l2_flash_config v4l2_cfg = { 0 }; |
| 713 |
struct led_flash_setting *intensity = &v4l2_cfg.intensity; |
| 714 |
|
| 715 |
intensity->min = tps6131x->step_torch_current_ma; |
| 716 |
intensity->max = tps6131x->max_torch_current_ma; |
| 717 |
intensity->step = tps6131x->step_torch_current_ma; |
| 718 |
intensity->val = intensity->min; |
| 719 |
|
| 720 |
strscpy(v4l2_cfg.dev_name, tps6131x->fled_cdev.led_cdev.dev->kobj.name, |
| 721 |
sizeof(v4l2_cfg.dev_name)); |
| 722 |
|
| 723 |
v4l2_cfg.has_external_strobe = true; |
| 724 |
v4l2_cfg.flash_faults = LED_FAULT_TIMEOUT | LED_FAULT_OVER_TEMPERATURE | |
| 725 |
LED_FAULT_SHORT_CIRCUIT | LED_FAULT_UNDER_VOLTAGE | |
| 726 |
LED_FAULT_LED_OVER_TEMPERATURE; |
| 727 |
|
| 728 |
tps6131x->v4l2_flash = v4l2_flash_init(tps6131x->dev, tps6131x->led_node, |
| 729 |
&tps6131x->fled_cdev, &tps6131x_v4l2_flash_ops, |
| 730 |
&v4l2_cfg); |
| 731 |
if (IS_ERR(tps6131x->v4l2_flash)) { |
| 732 |
dev_err(tps6131x->dev, "Failed to initialize v4l2 flash LED\n"); |
| 733 |
return PTR_ERR(tps6131x->v4l2_flash); |
| 734 |
} |
| 735 |
|
| 736 |
return 0; |
| 737 |
} |
| 738 |
|
| 739 |
static int tps6131x_probe(struct i2c_client *client) |
| 740 |
{ |
| 741 |
struct tps6131x *tps6131x; |
| 742 |
int ret; |
| 743 |
|
| 744 |
tps6131x = devm_kzalloc(&client->dev, sizeof(*tps6131x), GFP_KERNEL); |
| 745 |
if (!tps6131x) |
| 746 |
return -ENOMEM; |
| 747 |
|
| 748 |
tps6131x->dev = &client->dev; |
| 749 |
i2c_set_clientdata(client, tps6131x); |
| 750 |
mutex_init(&tps6131x->lock); |
| 751 |
INIT_DELAYED_WORK(&tps6131x->torch_refresh_work, tps6131x_torch_refresh_handler); |
| 752 |
|
| 753 |
ret = tps6131x_parse_node(tps6131x); |
| 754 |
if (ret) |
| 755 |
return ret; |
| 756 |
|
| 757 |
tps6131x->regmap = devm_regmap_init_i2c(client, &tps6131x_regmap); |
| 758 |
if (IS_ERR(tps6131x->regmap)) { |
| 759 |
ret = PTR_ERR(tps6131x->regmap); |
| 760 |
return dev_err_probe(&client->dev, ret, "Failed to allocate register map\n"); |
| 761 |
} |
| 762 |
|
| 763 |
tps6131x->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", GPIOD_OUT_HIGH); |
| 764 |
if (IS_ERR(tps6131x->reset_gpio)) { |
| 765 |
ret = PTR_ERR(tps6131x->reset_gpio); |
| 766 |
return dev_err_probe(&client->dev, ret, "Failed to get reset GPIO\n"); |
| 767 |
} |
| 768 |
|
| 769 |
ret = tps6131x_reset_chip(tps6131x); |
| 770 |
if (ret) |
| 771 |
return dev_err_probe(&client->dev, ret, "Failed to reset LED controller\n"); |
| 772 |
|
| 773 |
ret = tps6131x_init_chip(tps6131x); |
| 774 |
if (ret) |
| 775 |
return dev_err_probe(&client->dev, ret, "Failed to initialize LED controller\n"); |
| 776 |
|
| 777 |
ret = tps6131x_led_class_setup(tps6131x); |
| 778 |
if (ret) |
| 779 |
return dev_err_probe(&client->dev, ret, "Failed to setup LED class\n"); |
| 780 |
|
| 781 |
ret = tps6131x_v4l2_setup(tps6131x); |
| 782 |
if (ret) |
| 783 |
return dev_err_probe(&client->dev, ret, "Failed to setup v4l2 flash\n"); |
| 784 |
|
| 785 |
return 0; |
| 786 |
} |
| 787 |
|
| 788 |
static void tps6131x_remove(struct i2c_client *client) |
| 789 |
{ |
| 790 |
struct tps6131x *tps6131x = i2c_get_clientdata(client); |
| 791 |
|
| 792 |
v4l2_flash_release(tps6131x->v4l2_flash); |
| 793 |
|
| 794 |
cancel_delayed_work_sync(&tps6131x->torch_refresh_work); |
| 795 |
} |
| 796 |
|
| 797 |
static const struct of_device_id of_tps6131x_leds_match[] = { |
| 798 |
{ .compatible = "ti,tps61310" }, |
| 799 |
{} |
| 800 |
}; |
| 801 |
MODULE_DEVICE_TABLE(of, of_tps6131x_leds_match); |
| 802 |
|
| 803 |
static struct i2c_driver tps6131x_i2c_driver = { |
| 804 |
.driver = { |
| 805 |
.name = "tps6131x", |
| 806 |
.of_match_table = of_tps6131x_leds_match, |
| 807 |
}, |
| 808 |
.probe = tps6131x_probe, |
| 809 |
.remove = tps6131x_remove, |
| 810 |
}; |
| 811 |
module_i2c_driver(tps6131x_i2c_driver); |
| 812 |
|
| 813 |
MODULE_DESCRIPTION("Texas Instruments TPS6131X flash LED driver"); |
| 814 |
MODULE_AUTHOR("Matthias Fend <[email protected]>"); |
| 815 |
MODULE_LICENSE("GPL"); |