1 // SPDX-License-Identifier: GPL-2.0 2 // TI LM3692x LED chip family driver 3 // Copyright (C) 2017-18 Texas Instruments Incorporated - https://www.ti.com/ 4 5 #include <linux/gpio/consumer.h> 6 #include <linux/i2c.h> 7 #include <linux/init.h> 8 #include <linux/leds.h> 9 #include <linux/log2.h> 10 #include <linux/module.h> 11 #include <linux/mutex.h> 12 #include <linux/of.h> 13 #include <linux/of_gpio.h> 14 #include <linux/regmap.h> 15 #include <linux/regulator/consumer.h> 16 #include <linux/slab.h> 17 18 #define LM36922_MODEL 0 19 #define LM36923_MODEL 1 20 21 #define LM3692X_REV 0x0 22 #define LM3692X_RESET 0x1 23 #define LM3692X_EN 0x10 24 #define LM3692X_BRT_CTRL 0x11 25 #define LM3692X_PWM_CTRL 0x12 26 #define LM3692X_BOOST_CTRL 0x13 27 #define LM3692X_AUTO_FREQ_HI 0x15 28 #define LM3692X_AUTO_FREQ_LO 0x16 29 #define LM3692X_BL_ADJ_THRESH 0x17 30 #define LM3692X_BRT_LSB 0x18 31 #define LM3692X_BRT_MSB 0x19 32 #define LM3692X_FAULT_CTRL 0x1e 33 #define LM3692X_FAULT_FLAGS 0x1f 34 35 #define LM3692X_SW_RESET BIT(0) 36 #define LM3692X_DEVICE_EN BIT(0) 37 #define LM3692X_LED1_EN BIT(1) 38 #define LM3692X_LED2_EN BIT(2) 39 #define LM36923_LED3_EN BIT(3) 40 #define LM3692X_ENABLE_MASK (LM3692X_DEVICE_EN | LM3692X_LED1_EN | \ 41 LM3692X_LED2_EN | LM36923_LED3_EN) 42 43 /* Brightness Control Bits */ 44 #define LM3692X_BL_ADJ_POL BIT(0) 45 #define LM3692X_RAMP_RATE_125us 0x00 46 #define LM3692X_RAMP_RATE_250us BIT(1) 47 #define LM3692X_RAMP_RATE_500us BIT(2) 48 #define LM3692X_RAMP_RATE_1ms (BIT(1) | BIT(2)) 49 #define LM3692X_RAMP_RATE_2ms BIT(3) 50 #define LM3692X_RAMP_RATE_4ms (BIT(3) | BIT(1)) 51 #define LM3692X_RAMP_RATE_8ms (BIT(2) | BIT(3)) 52 #define LM3692X_RAMP_RATE_16ms (BIT(1) | BIT(2) | BIT(3)) 53 #define LM3692X_RAMP_EN BIT(4) 54 #define LM3692X_BRHT_MODE_REG 0x00 55 #define LM3692X_BRHT_MODE_PWM BIT(5) 56 #define LM3692X_BRHT_MODE_MULTI_RAMP BIT(6) 57 #define LM3692X_BRHT_MODE_RAMP_MULTI (BIT(5) | BIT(6)) 58 #define LM3692X_MAP_MODE_EXP BIT(7) 59 60 /* PWM Register Bits */ 61 #define LM3692X_PWM_FILTER_100 BIT(0) 62 #define LM3692X_PWM_FILTER_150 BIT(1) 63 #define LM3692X_PWM_FILTER_200 (BIT(0) | BIT(1)) 64 #define LM3692X_PWM_HYSTER_1LSB BIT(2) 65 #define LM3692X_PWM_HYSTER_2LSB BIT(3) 66 #define LM3692X_PWM_HYSTER_3LSB (BIT(3) | BIT(2)) 67 #define LM3692X_PWM_HYSTER_4LSB BIT(4) 68 #define LM3692X_PWM_HYSTER_5LSB (BIT(4) | BIT(2)) 69 #define LM3692X_PWM_HYSTER_6LSB (BIT(4) | BIT(3)) 70 #define LM3692X_PWM_POLARITY BIT(5) 71 #define LM3692X_PWM_SAMP_4MHZ BIT(6) 72 #define LM3692X_PWM_SAMP_24MHZ BIT(7) 73 74 /* Boost Control Bits */ 75 #define LM3692X_OCP_PROT_1A BIT(0) 76 #define LM3692X_OCP_PROT_1_25A BIT(1) 77 #define LM3692X_OCP_PROT_1_5A (BIT(0) | BIT(1)) 78 #define LM3692X_OVP_21V BIT(2) 79 #define LM3692X_OVP_25V BIT(3) 80 #define LM3692X_OVP_29V (BIT(2) | BIT(3)) 81 #define LM3692X_MIN_IND_22UH BIT(4) 82 #define LM3692X_BOOST_SW_1MHZ BIT(5) 83 #define LM3692X_BOOST_SW_NO_SHIFT BIT(6) 84 85 /* Fault Control Bits */ 86 #define LM3692X_FAULT_CTRL_OVP BIT(0) 87 #define LM3692X_FAULT_CTRL_OCP BIT(1) 88 #define LM3692X_FAULT_CTRL_TSD BIT(2) 89 #define LM3692X_FAULT_CTRL_OPEN BIT(3) 90 91 /* Fault Flag Bits */ 92 #define LM3692X_FAULT_FLAG_OVP BIT(0) 93 #define LM3692X_FAULT_FLAG_OCP BIT(1) 94 #define LM3692X_FAULT_FLAG_TSD BIT(2) 95 #define LM3692X_FAULT_FLAG_SHRT BIT(3) 96 #define LM3692X_FAULT_FLAG_OPEN BIT(4) 97 98 /** 99 * struct lm3692x_led - 100 * @lock - Lock for reading/writing the device 101 * @client - Pointer to the I2C client 102 * @led_dev - LED class device pointer 103 * @regmap - Devices register map 104 * @enable_gpio - VDDIO/EN gpio to enable communication interface 105 * @regulator - LED supply regulator pointer 106 * @led_enable - LED sync to be enabled 107 * @model_id - Current device model ID enumerated 108 */ 109 struct lm3692x_led { 110 struct mutex lock; 111 struct i2c_client *client; 112 struct led_classdev led_dev; 113 struct regmap *regmap; 114 struct gpio_desc *enable_gpio; 115 struct regulator *regulator; 116 int led_enable; 117 int model_id; 118 119 u8 boost_ctrl, brightness_ctrl; 120 bool enabled; 121 }; 122 123 static const struct reg_default lm3692x_reg_defs[] = { 124 {LM3692X_EN, 0xf}, 125 {LM3692X_BRT_CTRL, 0x61}, 126 {LM3692X_PWM_CTRL, 0x73}, 127 {LM3692X_BOOST_CTRL, 0x6f}, 128 {LM3692X_AUTO_FREQ_HI, 0x0}, 129 {LM3692X_AUTO_FREQ_LO, 0x0}, 130 {LM3692X_BL_ADJ_THRESH, 0x0}, 131 {LM3692X_BRT_LSB, 0x7}, 132 {LM3692X_BRT_MSB, 0xff}, 133 {LM3692X_FAULT_CTRL, 0x7}, 134 }; 135 136 static const struct regmap_config lm3692x_regmap_config = { 137 .reg_bits = 8, 138 .val_bits = 8, 139 140 .max_register = LM3692X_FAULT_FLAGS, 141 .reg_defaults = lm3692x_reg_defs, 142 .num_reg_defaults = ARRAY_SIZE(lm3692x_reg_defs), 143 .cache_type = REGCACHE_RBTREE, 144 }; 145 146 static int lm3692x_fault_check(struct lm3692x_led *led) 147 { 148 int ret; 149 unsigned int read_buf; 150 151 ret = regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf); 152 if (ret) 153 return ret; 154 155 if (read_buf) 156 dev_err(&led->client->dev, "Detected a fault 0x%X\n", read_buf); 157 158 /* The first read may clear the fault. Check again to see if the fault 159 * still exits and return that value. 160 */ 161 regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf); 162 if (read_buf) 163 dev_err(&led->client->dev, "Second read of fault flags 0x%X\n", 164 read_buf); 165 166 return read_buf; 167 } 168 169 static int lm3692x_leds_enable(struct lm3692x_led *led) 170 { 171 int enable_state; 172 int ret, reg_ret; 173 174 if (led->enabled) 175 return 0; 176 177 if (led->regulator) { 178 ret = regulator_enable(led->regulator); 179 if (ret) { 180 dev_err(&led->client->dev, 181 "Failed to enable regulator: %d\n", ret); 182 return ret; 183 } 184 } 185 186 if (led->enable_gpio) 187 gpiod_direction_output(led->enable_gpio, 1); 188 189 ret = lm3692x_fault_check(led); 190 if (ret) { 191 dev_err(&led->client->dev, "Cannot read/clear faults: %d\n", 192 ret); 193 goto out; 194 } 195 196 ret = regmap_write(led->regmap, LM3692X_BRT_CTRL, 0x00); 197 if (ret) 198 goto out; 199 200 /* 201 * For glitch free operation, the following data should 202 * only be written while LEDx enable bits are 0 and the device enable 203 * bit is set to 1. 204 * per Section 7.5.14 of the data sheet 205 */ 206 ret = regmap_write(led->regmap, LM3692X_EN, LM3692X_DEVICE_EN); 207 if (ret) 208 goto out; 209 210 /* Set the brightness to 0 so when enabled the LEDs do not come 211 * on with full brightness. 212 */ 213 ret = regmap_write(led->regmap, LM3692X_BRT_MSB, 0); 214 if (ret) 215 goto out; 216 217 ret = regmap_write(led->regmap, LM3692X_BRT_LSB, 0); 218 if (ret) 219 goto out; 220 221 ret = regmap_write(led->regmap, LM3692X_PWM_CTRL, 222 LM3692X_PWM_FILTER_100 | LM3692X_PWM_SAMP_24MHZ); 223 if (ret) 224 goto out; 225 226 ret = regmap_write(led->regmap, LM3692X_BOOST_CTRL, led->boost_ctrl); 227 if (ret) 228 goto out; 229 230 ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_HI, 0x00); 231 if (ret) 232 goto out; 233 234 ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_LO, 0x00); 235 if (ret) 236 goto out; 237 238 ret = regmap_write(led->regmap, LM3692X_BL_ADJ_THRESH, 0x00); 239 if (ret) 240 goto out; 241 242 ret = regmap_write(led->regmap, LM3692X_BRT_CTRL, 243 LM3692X_BL_ADJ_POL | LM3692X_RAMP_EN); 244 if (ret) 245 goto out; 246 247 switch (led->led_enable) { 248 case 0: 249 default: 250 if (led->model_id == LM36923_MODEL) 251 enable_state = LM3692X_LED1_EN | LM3692X_LED2_EN | 252 LM36923_LED3_EN; 253 else 254 enable_state = LM3692X_LED1_EN | LM3692X_LED2_EN; 255 256 break; 257 case 1: 258 enable_state = LM3692X_LED1_EN; 259 break; 260 case 2: 261 enable_state = LM3692X_LED2_EN; 262 break; 263 264 case 3: 265 if (led->model_id == LM36923_MODEL) { 266 enable_state = LM36923_LED3_EN; 267 break; 268 } 269 270 ret = -EINVAL; 271 dev_err(&led->client->dev, 272 "LED3 sync not available on this device\n"); 273 goto out; 274 } 275 276 ret = regmap_update_bits(led->regmap, LM3692X_EN, LM3692X_ENABLE_MASK, 277 enable_state | LM3692X_DEVICE_EN); 278 279 led->enabled = true; 280 return ret; 281 out: 282 dev_err(&led->client->dev, "Fail writing initialization values\n"); 283 284 if (led->enable_gpio) 285 gpiod_direction_output(led->enable_gpio, 0); 286 287 if (led->regulator) { 288 reg_ret = regulator_disable(led->regulator); 289 if (reg_ret) 290 dev_err(&led->client->dev, 291 "Failed to disable regulator: %d\n", reg_ret); 292 } 293 294 return ret; 295 } 296 297 static int lm3692x_leds_disable(struct lm3692x_led *led) 298 { 299 int ret; 300 301 if (!led->enabled) 302 return 0; 303 304 ret = regmap_update_bits(led->regmap, LM3692X_EN, LM3692X_DEVICE_EN, 0); 305 if (ret) { 306 dev_err(&led->client->dev, "Failed to disable regulator: %d\n", 307 ret); 308 return ret; 309 } 310 311 if (led->enable_gpio) 312 gpiod_direction_output(led->enable_gpio, 0); 313 314 if (led->regulator) { 315 ret = regulator_disable(led->regulator); 316 if (ret) 317 dev_err(&led->client->dev, 318 "Failed to disable regulator: %d\n", ret); 319 } 320 321 led->enabled = false; 322 return ret; 323 } 324 325 static int lm3692x_brightness_set(struct led_classdev *led_cdev, 326 enum led_brightness brt_val) 327 { 328 struct lm3692x_led *led = 329 container_of(led_cdev, struct lm3692x_led, led_dev); 330 int ret; 331 int led_brightness_lsb = (brt_val >> 5); 332 333 mutex_lock(&led->lock); 334 335 if (brt_val == 0) { 336 ret = lm3692x_leds_disable(led); 337 goto out; 338 } else { 339 lm3692x_leds_enable(led); 340 } 341 342 ret = lm3692x_fault_check(led); 343 if (ret) { 344 dev_err(&led->client->dev, "Cannot read/clear faults: %d\n", 345 ret); 346 goto out; 347 } 348 349 ret = regmap_write(led->regmap, LM3692X_BRT_MSB, brt_val); 350 if (ret) { 351 dev_err(&led->client->dev, "Cannot write MSB: %d\n", ret); 352 goto out; 353 } 354 355 ret = regmap_write(led->regmap, LM3692X_BRT_LSB, led_brightness_lsb); 356 if (ret) { 357 dev_err(&led->client->dev, "Cannot write LSB: %d\n", ret); 358 goto out; 359 } 360 out: 361 mutex_unlock(&led->lock); 362 return ret; 363 } 364 365 static enum led_brightness lm3692x_max_brightness(struct lm3692x_led *led, 366 u32 max_cur) 367 { 368 u32 max_code; 369 370 /* see p.12 of LM36922 data sheet for brightness formula */ 371 max_code = ((max_cur * 1000) - 37806) / 12195; 372 if (max_code > 0x7FF) 373 max_code = 0x7FF; 374 375 return max_code >> 3; 376 } 377 378 static int lm3692x_probe_dt(struct lm3692x_led *led) 379 { 380 struct fwnode_handle *child = NULL; 381 struct led_init_data init_data = {}; 382 u32 ovp, max_cur; 383 int ret; 384 385 led->enable_gpio = devm_gpiod_get_optional(&led->client->dev, 386 "enable", GPIOD_OUT_LOW); 387 if (IS_ERR(led->enable_gpio)) { 388 ret = PTR_ERR(led->enable_gpio); 389 dev_err(&led->client->dev, "Failed to get enable gpio: %d\n", 390 ret); 391 return ret; 392 } 393 394 led->regulator = devm_regulator_get_optional(&led->client->dev, "vled"); 395 if (IS_ERR(led->regulator)) { 396 ret = PTR_ERR(led->regulator); 397 if (ret != -ENODEV) 398 return dev_err_probe(&led->client->dev, ret, 399 "Failed to get vled regulator\n"); 400 401 led->regulator = NULL; 402 } 403 404 led->boost_ctrl = LM3692X_BOOST_SW_1MHZ | 405 LM3692X_BOOST_SW_NO_SHIFT | 406 LM3692X_OCP_PROT_1_5A; 407 ret = device_property_read_u32(&led->client->dev, 408 "ti,ovp-microvolt", &ovp); 409 if (ret) { 410 led->boost_ctrl |= LM3692X_OVP_29V; 411 } else { 412 switch (ovp) { 413 case 17000000: 414 break; 415 case 21000000: 416 led->boost_ctrl |= LM3692X_OVP_21V; 417 break; 418 case 25000000: 419 led->boost_ctrl |= LM3692X_OVP_25V; 420 break; 421 case 29000000: 422 led->boost_ctrl |= LM3692X_OVP_29V; 423 break; 424 default: 425 dev_err(&led->client->dev, "Invalid OVP %d\n", ovp); 426 return -EINVAL; 427 } 428 } 429 430 child = device_get_next_child_node(&led->client->dev, child); 431 if (!child) { 432 dev_err(&led->client->dev, "No LED Child node\n"); 433 return -ENODEV; 434 } 435 436 ret = fwnode_property_read_u32(child, "reg", &led->led_enable); 437 if (ret) { 438 dev_err(&led->client->dev, "reg DT property missing\n"); 439 return ret; 440 } 441 442 ret = fwnode_property_read_u32(child, "led-max-microamp", &max_cur); 443 led->led_dev.max_brightness = ret ? LED_FULL : 444 lm3692x_max_brightness(led, max_cur); 445 446 init_data.fwnode = child; 447 init_data.devicename = led->client->name; 448 init_data.default_label = ":"; 449 450 ret = devm_led_classdev_register_ext(&led->client->dev, &led->led_dev, 451 &init_data); 452 if (ret) { 453 dev_err(&led->client->dev, "led register err: %d\n", ret); 454 return ret; 455 } 456 457 return 0; 458 } 459 460 static int lm3692x_probe(struct i2c_client *client, 461 const struct i2c_device_id *id) 462 { 463 struct lm3692x_led *led; 464 int ret; 465 466 led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL); 467 if (!led) 468 return -ENOMEM; 469 470 mutex_init(&led->lock); 471 led->client = client; 472 led->led_dev.brightness_set_blocking = lm3692x_brightness_set; 473 led->model_id = id->driver_data; 474 i2c_set_clientdata(client, led); 475 476 led->regmap = devm_regmap_init_i2c(client, &lm3692x_regmap_config); 477 if (IS_ERR(led->regmap)) { 478 ret = PTR_ERR(led->regmap); 479 dev_err(&client->dev, "Failed to allocate register map: %d\n", 480 ret); 481 return ret; 482 } 483 484 ret = lm3692x_probe_dt(led); 485 if (ret) 486 return ret; 487 488 ret = lm3692x_leds_enable(led); 489 if (ret) 490 return ret; 491 492 return 0; 493 } 494 495 static int lm3692x_remove(struct i2c_client *client) 496 { 497 struct lm3692x_led *led = i2c_get_clientdata(client); 498 int ret; 499 500 ret = lm3692x_leds_disable(led); 501 if (ret) 502 return ret; 503 mutex_destroy(&led->lock); 504 505 return 0; 506 } 507 508 static const struct i2c_device_id lm3692x_id[] = { 509 { "lm36922", LM36922_MODEL }, 510 { "lm36923", LM36923_MODEL }, 511 { } 512 }; 513 MODULE_DEVICE_TABLE(i2c, lm3692x_id); 514 515 static const struct of_device_id of_lm3692x_leds_match[] = { 516 { .compatible = "ti,lm36922", }, 517 { .compatible = "ti,lm36923", }, 518 {}, 519 }; 520 MODULE_DEVICE_TABLE(of, of_lm3692x_leds_match); 521 522 static struct i2c_driver lm3692x_driver = { 523 .driver = { 524 .name = "lm3692x", 525 .of_match_table = of_lm3692x_leds_match, 526 }, 527 .probe = lm3692x_probe, 528 .remove = lm3692x_remove, 529 .id_table = lm3692x_id, 530 }; 531 module_i2c_driver(lm3692x_driver); 532 533 MODULE_DESCRIPTION("Texas Instruments LM3692X LED driver"); 534 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>"); 535 MODULE_LICENSE("GPL v2"); 536