1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Simple driver for Texas Instruments LM3630A Backlight driver chip 4 * Copyright (C) 2012 Texas Instruments 5 */ 6 #include <linux/module.h> 7 #include <linux/slab.h> 8 #include <linux/i2c.h> 9 #include <linux/backlight.h> 10 #include <linux/err.h> 11 #include <linux/delay.h> 12 #include <linux/uaccess.h> 13 #include <linux/interrupt.h> 14 #include <linux/regmap.h> 15 #include <linux/gpio/consumer.h> 16 #include <linux/pwm.h> 17 #include <linux/platform_data/lm3630a_bl.h> 18 19 #define REG_CTRL 0x00 20 #define REG_BOOST 0x02 21 #define REG_CONFIG 0x01 22 #define REG_BRT_A 0x03 23 #define REG_BRT_B 0x04 24 #define REG_I_A 0x05 25 #define REG_I_B 0x06 26 #define REG_INT_STATUS 0x09 27 #define REG_INT_EN 0x0A 28 #define REG_FAULT 0x0B 29 #define REG_PWM_OUTLOW 0x12 30 #define REG_PWM_OUTHIGH 0x13 31 #define REG_FILTER_STRENGTH 0x50 32 #define REG_MAX 0x50 33 34 #define INT_DEBOUNCE_MSEC 10 35 36 #define LM3630A_BANK_0 0 37 #define LM3630A_BANK_1 1 38 39 #define LM3630A_NUM_SINKS 2 40 #define LM3630A_SINK_0 0 41 #define LM3630A_SINK_1 1 42 43 struct lm3630a_chip { 44 struct device *dev; 45 struct delayed_work work; 46 47 int irq; 48 struct workqueue_struct *irqthread; 49 struct lm3630a_platform_data *pdata; 50 struct backlight_device *bleda; 51 struct backlight_device *bledb; 52 struct gpio_desc *enable_gpio; 53 struct regmap *regmap; 54 struct pwm_device *pwmd; 55 struct pwm_state pwmd_state; 56 }; 57 58 /* i2c access */ 59 static int lm3630a_read(struct lm3630a_chip *pchip, unsigned int reg) 60 { 61 int rval; 62 unsigned int reg_val; 63 64 rval = regmap_read(pchip->regmap, reg, ®_val); 65 if (rval < 0) 66 return rval; 67 return reg_val & 0xFF; 68 } 69 70 static int lm3630a_write(struct lm3630a_chip *pchip, 71 unsigned int reg, unsigned int data) 72 { 73 return regmap_write(pchip->regmap, reg, data); 74 } 75 76 static int lm3630a_update(struct lm3630a_chip *pchip, 77 unsigned int reg, unsigned int mask, 78 unsigned int data) 79 { 80 return regmap_update_bits(pchip->regmap, reg, mask, data); 81 } 82 83 /* initialize chip */ 84 static int lm3630a_chip_init(struct lm3630a_chip *pchip) 85 { 86 int rval; 87 struct lm3630a_platform_data *pdata = pchip->pdata; 88 89 usleep_range(1000, 2000); 90 /* set Filter Strength Register */ 91 rval = lm3630a_write(pchip, REG_FILTER_STRENGTH, 0x03); 92 /* set Cofig. register */ 93 rval |= lm3630a_update(pchip, REG_CONFIG, 0x07, pdata->pwm_ctrl); 94 /* set boost control */ 95 rval |= lm3630a_write(pchip, REG_BOOST, 0x38); 96 /* set current A */ 97 rval |= lm3630a_update(pchip, REG_I_A, 0x1F, 0x1F); 98 /* set current B */ 99 rval |= lm3630a_write(pchip, REG_I_B, 0x1F); 100 /* set control */ 101 rval |= lm3630a_update(pchip, REG_CTRL, 0x14, pdata->leda_ctrl); 102 rval |= lm3630a_update(pchip, REG_CTRL, 0x0B, pdata->ledb_ctrl); 103 usleep_range(1000, 2000); 104 /* set brightness A and B */ 105 rval |= lm3630a_write(pchip, REG_BRT_A, pdata->leda_init_brt); 106 rval |= lm3630a_write(pchip, REG_BRT_B, pdata->ledb_init_brt); 107 108 if (rval < 0) 109 dev_err(pchip->dev, "i2c failed to access register\n"); 110 return rval; 111 } 112 113 /* interrupt handling */ 114 static void lm3630a_delayed_func(struct work_struct *work) 115 { 116 int rval; 117 struct lm3630a_chip *pchip; 118 119 pchip = container_of(work, struct lm3630a_chip, work.work); 120 121 rval = lm3630a_read(pchip, REG_INT_STATUS); 122 if (rval < 0) { 123 dev_err(pchip->dev, 124 "i2c failed to access REG_INT_STATUS Register\n"); 125 return; 126 } 127 128 dev_info(pchip->dev, "REG_INT_STATUS Register is 0x%x\n", rval); 129 } 130 131 static irqreturn_t lm3630a_isr_func(int irq, void *chip) 132 { 133 int rval; 134 struct lm3630a_chip *pchip = chip; 135 unsigned long delay = msecs_to_jiffies(INT_DEBOUNCE_MSEC); 136 137 queue_delayed_work(pchip->irqthread, &pchip->work, delay); 138 139 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00); 140 if (rval < 0) { 141 dev_err(pchip->dev, "i2c failed to access register\n"); 142 return IRQ_NONE; 143 } 144 return IRQ_HANDLED; 145 } 146 147 static int lm3630a_intr_config(struct lm3630a_chip *pchip) 148 { 149 int rval; 150 151 rval = lm3630a_write(pchip, REG_INT_EN, 0x87); 152 if (rval < 0) 153 return rval; 154 155 INIT_DELAYED_WORK(&pchip->work, lm3630a_delayed_func); 156 pchip->irqthread = create_singlethread_workqueue("lm3630a-irqthd"); 157 if (!pchip->irqthread) { 158 dev_err(pchip->dev, "create irq thread fail\n"); 159 return -ENOMEM; 160 } 161 if (request_threaded_irq 162 (pchip->irq, NULL, lm3630a_isr_func, 163 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "lm3630a_irq", pchip)) { 164 dev_err(pchip->dev, "request threaded irq fail\n"); 165 destroy_workqueue(pchip->irqthread); 166 return -ENOMEM; 167 } 168 return rval; 169 } 170 171 static int lm3630a_pwm_ctrl(struct lm3630a_chip *pchip, int br, int br_max) 172 { 173 int err; 174 175 pchip->pwmd_state.period = pchip->pdata->pwm_period; 176 177 err = pwm_set_relative_duty_cycle(&pchip->pwmd_state, br, br_max); 178 if (err) 179 return err; 180 181 pchip->pwmd_state.enabled = pchip->pwmd_state.duty_cycle ? true : false; 182 183 return pwm_apply_state(pchip->pwmd, &pchip->pwmd_state); 184 } 185 186 /* update and get brightness */ 187 static int lm3630a_bank_a_update_status(struct backlight_device *bl) 188 { 189 int ret; 190 struct lm3630a_chip *pchip = bl_get_data(bl); 191 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl; 192 193 /* pwm control */ 194 if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) 195 return lm3630a_pwm_ctrl(pchip, bl->props.brightness, 196 bl->props.max_brightness); 197 198 /* disable sleep */ 199 ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00); 200 if (ret < 0) 201 goto out_i2c_err; 202 usleep_range(1000, 2000); 203 /* minimum brightness is 0x04 */ 204 ret = lm3630a_write(pchip, REG_BRT_A, bl->props.brightness); 205 206 if (backlight_is_blank(bl) || (backlight_get_brightness(bl) < 0x4)) 207 /* turn the string off */ 208 ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDA_ENABLE, 0); 209 else 210 ret |= lm3630a_update(pchip, REG_CTRL, 211 LM3630A_LEDA_ENABLE, LM3630A_LEDA_ENABLE); 212 if (ret < 0) 213 goto out_i2c_err; 214 return 0; 215 216 out_i2c_err: 217 dev_err(pchip->dev, "i2c failed to access (%pe)\n", ERR_PTR(ret)); 218 return ret; 219 } 220 221 static int lm3630a_bank_a_get_brightness(struct backlight_device *bl) 222 { 223 int brightness, rval; 224 struct lm3630a_chip *pchip = bl_get_data(bl); 225 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl; 226 227 if ((pwm_ctrl & LM3630A_PWM_BANK_A) != 0) { 228 rval = lm3630a_read(pchip, REG_PWM_OUTHIGH); 229 if (rval < 0) 230 goto out_i2c_err; 231 brightness = (rval & 0x01) << 8; 232 rval = lm3630a_read(pchip, REG_PWM_OUTLOW); 233 if (rval < 0) 234 goto out_i2c_err; 235 brightness |= rval; 236 goto out; 237 } 238 239 /* disable sleep */ 240 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00); 241 if (rval < 0) 242 goto out_i2c_err; 243 usleep_range(1000, 2000); 244 rval = lm3630a_read(pchip, REG_BRT_A); 245 if (rval < 0) 246 goto out_i2c_err; 247 brightness = rval; 248 249 out: 250 bl->props.brightness = brightness; 251 return bl->props.brightness; 252 out_i2c_err: 253 dev_err(pchip->dev, "i2c failed to access register\n"); 254 return 0; 255 } 256 257 static const struct backlight_ops lm3630a_bank_a_ops = { 258 .options = BL_CORE_SUSPENDRESUME, 259 .update_status = lm3630a_bank_a_update_status, 260 .get_brightness = lm3630a_bank_a_get_brightness, 261 }; 262 263 /* update and get brightness */ 264 static int lm3630a_bank_b_update_status(struct backlight_device *bl) 265 { 266 int ret; 267 struct lm3630a_chip *pchip = bl_get_data(bl); 268 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl; 269 270 /* pwm control */ 271 if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) 272 return lm3630a_pwm_ctrl(pchip, bl->props.brightness, 273 bl->props.max_brightness); 274 275 /* disable sleep */ 276 ret = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00); 277 if (ret < 0) 278 goto out_i2c_err; 279 usleep_range(1000, 2000); 280 /* minimum brightness is 0x04 */ 281 ret = lm3630a_write(pchip, REG_BRT_B, bl->props.brightness); 282 283 if (backlight_is_blank(bl) || (backlight_get_brightness(bl) < 0x4)) 284 /* turn the string off */ 285 ret |= lm3630a_update(pchip, REG_CTRL, LM3630A_LEDB_ENABLE, 0); 286 else 287 ret |= lm3630a_update(pchip, REG_CTRL, 288 LM3630A_LEDB_ENABLE, LM3630A_LEDB_ENABLE); 289 if (ret < 0) 290 goto out_i2c_err; 291 return 0; 292 293 out_i2c_err: 294 dev_err(pchip->dev, "i2c failed to access (%pe)\n", ERR_PTR(ret)); 295 return ret; 296 } 297 298 static int lm3630a_bank_b_get_brightness(struct backlight_device *bl) 299 { 300 int brightness, rval; 301 struct lm3630a_chip *pchip = bl_get_data(bl); 302 enum lm3630a_pwm_ctrl pwm_ctrl = pchip->pdata->pwm_ctrl; 303 304 if ((pwm_ctrl & LM3630A_PWM_BANK_B) != 0) { 305 rval = lm3630a_read(pchip, REG_PWM_OUTHIGH); 306 if (rval < 0) 307 goto out_i2c_err; 308 brightness = (rval & 0x01) << 8; 309 rval = lm3630a_read(pchip, REG_PWM_OUTLOW); 310 if (rval < 0) 311 goto out_i2c_err; 312 brightness |= rval; 313 goto out; 314 } 315 316 /* disable sleep */ 317 rval = lm3630a_update(pchip, REG_CTRL, 0x80, 0x00); 318 if (rval < 0) 319 goto out_i2c_err; 320 usleep_range(1000, 2000); 321 rval = lm3630a_read(pchip, REG_BRT_B); 322 if (rval < 0) 323 goto out_i2c_err; 324 brightness = rval; 325 326 out: 327 bl->props.brightness = brightness; 328 return bl->props.brightness; 329 out_i2c_err: 330 dev_err(pchip->dev, "i2c failed to access register\n"); 331 return 0; 332 } 333 334 static const struct backlight_ops lm3630a_bank_b_ops = { 335 .options = BL_CORE_SUSPENDRESUME, 336 .update_status = lm3630a_bank_b_update_status, 337 .get_brightness = lm3630a_bank_b_get_brightness, 338 }; 339 340 static int lm3630a_backlight_register(struct lm3630a_chip *pchip) 341 { 342 struct lm3630a_platform_data *pdata = pchip->pdata; 343 struct backlight_properties props; 344 const char *label; 345 346 props.type = BACKLIGHT_RAW; 347 if (pdata->leda_ctrl != LM3630A_LEDA_DISABLE) { 348 props.brightness = pdata->leda_init_brt; 349 props.max_brightness = pdata->leda_max_brt; 350 label = pdata->leda_label ? pdata->leda_label : "lm3630a_leda"; 351 pchip->bleda = 352 devm_backlight_device_register(pchip->dev, label, 353 pchip->dev, pchip, 354 &lm3630a_bank_a_ops, &props); 355 if (IS_ERR(pchip->bleda)) 356 return PTR_ERR(pchip->bleda); 357 } 358 359 if ((pdata->ledb_ctrl != LM3630A_LEDB_DISABLE) && 360 (pdata->ledb_ctrl != LM3630A_LEDB_ON_A)) { 361 props.brightness = pdata->ledb_init_brt; 362 props.max_brightness = pdata->ledb_max_brt; 363 label = pdata->ledb_label ? pdata->ledb_label : "lm3630a_ledb"; 364 pchip->bledb = 365 devm_backlight_device_register(pchip->dev, label, 366 pchip->dev, pchip, 367 &lm3630a_bank_b_ops, &props); 368 if (IS_ERR(pchip->bledb)) 369 return PTR_ERR(pchip->bledb); 370 } 371 return 0; 372 } 373 374 static const struct regmap_config lm3630a_regmap = { 375 .reg_bits = 8, 376 .val_bits = 8, 377 .max_register = REG_MAX, 378 }; 379 380 static int lm3630a_parse_led_sources(struct fwnode_handle *node, 381 int default_led_sources) 382 { 383 u32 sources[LM3630A_NUM_SINKS]; 384 int ret, num_sources, i; 385 386 num_sources = fwnode_property_count_u32(node, "led-sources"); 387 if (num_sources < 0) 388 return default_led_sources; 389 else if (num_sources > ARRAY_SIZE(sources)) 390 return -EINVAL; 391 392 ret = fwnode_property_read_u32_array(node, "led-sources", sources, 393 num_sources); 394 if (ret) 395 return ret; 396 397 for (i = 0; i < num_sources; i++) { 398 if (sources[i] != LM3630A_SINK_0 && sources[i] != LM3630A_SINK_1) 399 return -EINVAL; 400 401 ret |= BIT(sources[i]); 402 } 403 404 return ret; 405 } 406 407 static int lm3630a_parse_bank(struct lm3630a_platform_data *pdata, 408 struct fwnode_handle *node, int *seen_led_sources) 409 { 410 int led_sources, ret; 411 const char *label; 412 u32 bank, val; 413 bool linear; 414 415 ret = fwnode_property_read_u32(node, "reg", &bank); 416 if (ret) 417 return ret; 418 419 if (bank != LM3630A_BANK_0 && bank != LM3630A_BANK_1) 420 return -EINVAL; 421 422 led_sources = lm3630a_parse_led_sources(node, BIT(bank)); 423 if (led_sources < 0) 424 return led_sources; 425 426 if (*seen_led_sources & led_sources) 427 return -EINVAL; 428 429 *seen_led_sources |= led_sources; 430 431 linear = fwnode_property_read_bool(node, 432 "ti,linear-mapping-mode"); 433 if (bank) { 434 if (led_sources & BIT(LM3630A_SINK_0) || 435 !(led_sources & BIT(LM3630A_SINK_1))) 436 return -EINVAL; 437 438 pdata->ledb_ctrl = linear ? 439 LM3630A_LEDB_ENABLE_LINEAR : 440 LM3630A_LEDB_ENABLE; 441 } else { 442 if (!(led_sources & BIT(LM3630A_SINK_0))) 443 return -EINVAL; 444 445 pdata->leda_ctrl = linear ? 446 LM3630A_LEDA_ENABLE_LINEAR : 447 LM3630A_LEDA_ENABLE; 448 449 if (led_sources & BIT(LM3630A_SINK_1)) 450 pdata->ledb_ctrl = LM3630A_LEDB_ON_A; 451 } 452 453 ret = fwnode_property_read_string(node, "label", &label); 454 if (!ret) { 455 if (bank) 456 pdata->ledb_label = label; 457 else 458 pdata->leda_label = label; 459 } 460 461 ret = fwnode_property_read_u32(node, "default-brightness", 462 &val); 463 if (!ret) { 464 if (bank) 465 pdata->ledb_init_brt = val; 466 else 467 pdata->leda_init_brt = val; 468 } 469 470 ret = fwnode_property_read_u32(node, "max-brightness", &val); 471 if (!ret) { 472 if (bank) 473 pdata->ledb_max_brt = val; 474 else 475 pdata->leda_max_brt = val; 476 } 477 478 return 0; 479 } 480 481 static int lm3630a_parse_node(struct lm3630a_chip *pchip, 482 struct lm3630a_platform_data *pdata) 483 { 484 int ret = -ENODEV, seen_led_sources = 0; 485 struct fwnode_handle *node; 486 487 device_for_each_child_node(pchip->dev, node) { 488 ret = lm3630a_parse_bank(pdata, node, &seen_led_sources); 489 if (ret) { 490 fwnode_handle_put(node); 491 return ret; 492 } 493 } 494 495 return ret; 496 } 497 498 static int lm3630a_probe(struct i2c_client *client) 499 { 500 struct lm3630a_platform_data *pdata = dev_get_platdata(&client->dev); 501 struct lm3630a_chip *pchip; 502 int rval; 503 504 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { 505 dev_err(&client->dev, "fail : i2c functionality check\n"); 506 return -EOPNOTSUPP; 507 } 508 509 pchip = devm_kzalloc(&client->dev, sizeof(struct lm3630a_chip), 510 GFP_KERNEL); 511 if (!pchip) 512 return -ENOMEM; 513 pchip->dev = &client->dev; 514 515 pchip->regmap = devm_regmap_init_i2c(client, &lm3630a_regmap); 516 if (IS_ERR(pchip->regmap)) { 517 rval = PTR_ERR(pchip->regmap); 518 dev_err(&client->dev, "fail : allocate reg. map: %d\n", rval); 519 return rval; 520 } 521 522 i2c_set_clientdata(client, pchip); 523 if (pdata == NULL) { 524 pdata = devm_kzalloc(pchip->dev, 525 sizeof(struct lm3630a_platform_data), 526 GFP_KERNEL); 527 if (pdata == NULL) 528 return -ENOMEM; 529 530 /* default values */ 531 pdata->leda_max_brt = LM3630A_MAX_BRIGHTNESS; 532 pdata->ledb_max_brt = LM3630A_MAX_BRIGHTNESS; 533 pdata->leda_init_brt = LM3630A_MAX_BRIGHTNESS; 534 pdata->ledb_init_brt = LM3630A_MAX_BRIGHTNESS; 535 536 rval = lm3630a_parse_node(pchip, pdata); 537 if (rval) { 538 dev_err(&client->dev, "fail : parse node\n"); 539 return rval; 540 } 541 } 542 pchip->pdata = pdata; 543 544 pchip->enable_gpio = devm_gpiod_get_optional(&client->dev, "enable", 545 GPIOD_OUT_HIGH); 546 if (IS_ERR(pchip->enable_gpio)) { 547 rval = PTR_ERR(pchip->enable_gpio); 548 return rval; 549 } 550 551 /* chip initialize */ 552 rval = lm3630a_chip_init(pchip); 553 if (rval < 0) { 554 dev_err(&client->dev, "fail : init chip\n"); 555 return rval; 556 } 557 /* backlight register */ 558 rval = lm3630a_backlight_register(pchip); 559 if (rval < 0) { 560 dev_err(&client->dev, "fail : backlight register.\n"); 561 return rval; 562 } 563 /* pwm */ 564 if (pdata->pwm_ctrl != LM3630A_PWM_DISABLE) { 565 pchip->pwmd = devm_pwm_get(pchip->dev, "lm3630a-pwm"); 566 if (IS_ERR(pchip->pwmd)) { 567 dev_err(&client->dev, "fail : get pwm device\n"); 568 return PTR_ERR(pchip->pwmd); 569 } 570 571 pwm_init_state(pchip->pwmd, &pchip->pwmd_state); 572 } 573 574 /* interrupt enable : irq 0 is not allowed */ 575 pchip->irq = client->irq; 576 if (pchip->irq) { 577 rval = lm3630a_intr_config(pchip); 578 if (rval < 0) 579 return rval; 580 } 581 dev_info(&client->dev, "LM3630A backlight register OK.\n"); 582 return 0; 583 } 584 585 static void lm3630a_remove(struct i2c_client *client) 586 { 587 int rval; 588 struct lm3630a_chip *pchip = i2c_get_clientdata(client); 589 590 rval = lm3630a_write(pchip, REG_BRT_A, 0); 591 if (rval < 0) 592 dev_err(pchip->dev, "i2c failed to access register\n"); 593 594 rval = lm3630a_write(pchip, REG_BRT_B, 0); 595 if (rval < 0) 596 dev_err(pchip->dev, "i2c failed to access register\n"); 597 598 if (pchip->irq) { 599 free_irq(pchip->irq, pchip); 600 destroy_workqueue(pchip->irqthread); 601 } 602 } 603 604 static const struct i2c_device_id lm3630a_id[] = { 605 {LM3630A_NAME, 0}, 606 {} 607 }; 608 609 MODULE_DEVICE_TABLE(i2c, lm3630a_id); 610 611 static const struct of_device_id lm3630a_match_table[] = { 612 { .compatible = "ti,lm3630a", }, 613 { }, 614 }; 615 616 MODULE_DEVICE_TABLE(of, lm3630a_match_table); 617 618 static struct i2c_driver lm3630a_i2c_driver = { 619 .driver = { 620 .name = LM3630A_NAME, 621 .of_match_table = lm3630a_match_table, 622 }, 623 .probe = lm3630a_probe, 624 .remove = lm3630a_remove, 625 .id_table = lm3630a_id, 626 }; 627 628 module_i2c_driver(lm3630a_i2c_driver); 629 630 MODULE_DESCRIPTION("Texas Instruments Backlight driver for LM3630A"); 631 MODULE_AUTHOR("Daniel Jeong <gshark.jeong@gmail.com>"); 632 MODULE_AUTHOR("LDD MLP <ldd-mlp@list.ti.com>"); 633 MODULE_LICENSE("GPL v2"); 634