leds-lp8501.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI LP8501 9 channel LED Driver
  4. *
  5. * Copyright (C) 2013 Texas Instruments
  6. *
  7. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/firmware.h>
  11. #include <linux/i2c.h>
  12. #include <linux/init.h>
  13. #include <linux/leds.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_data/leds-lp55xx.h>
  18. #include <linux/slab.h>
  19. #include "leds-lp55xx-common.h"
  20. #define LP8501_PAGES_PER_ENGINE 1
  21. #define LP8501_MAX_LEDS 9
  22. /* Registers */
  23. #define LP8501_REG_ENABLE 0x00
  24. #define LP8501_ENABLE BIT(6)
  25. #define LP8501_REG_OP_MODE 0x01
  26. #define LP8501_REG_PWR_CONFIG 0x05
  27. #define LP8501_PWR_CONFIG_M 0x03
  28. #define LP8501_REG_LED_PWM_BASE 0x16
  29. #define LP8501_REG_LED_CURRENT_BASE 0x26
  30. #define LP8501_REG_CONFIG 0x36
  31. #define LP8501_PWM_PSAVE BIT(7)
  32. #define LP8501_AUTO_INC BIT(6)
  33. #define LP8501_PWR_SAVE BIT(5)
  34. #define LP8501_CP_MODE_MASK 0x18
  35. #define LP8501_CP_MODE_SHIFT 3
  36. #define LP8501_INT_CLK BIT(0)
  37. #define LP8501_DEFAULT_CFG (LP8501_PWM_PSAVE | LP8501_AUTO_INC | LP8501_PWR_SAVE)
  38. #define LP8501_REG_STATUS 0x3A
  39. #define LP8501_ENGINE_BUSY BIT(4)
  40. #define LP8501_REG_RESET 0x3D
  41. #define LP8501_RESET 0xFF
  42. #define LP8501_REG_PROG_MEM 0x50
  43. static int lp8501_post_init_device(struct lp55xx_chip *chip)
  44. {
  45. int ret;
  46. u8 val = LP8501_DEFAULT_CFG;
  47. ret = lp55xx_write(chip, LP8501_REG_ENABLE, LP8501_ENABLE);
  48. if (ret)
  49. return ret;
  50. /* Chip startup time is 500 us, 1 - 2 ms gives some margin */
  51. usleep_range(1000, 2000);
  52. if (chip->pdata->clock_mode != LP55XX_CLOCK_EXT)
  53. val |= LP8501_INT_CLK;
  54. val |= (chip->pdata->charge_pump_mode << LP8501_CP_MODE_SHIFT) & LP8501_CP_MODE_MASK;
  55. ret = lp55xx_write(chip, LP8501_REG_CONFIG, val);
  56. if (ret)
  57. return ret;
  58. /* Power selection for each output */
  59. return lp55xx_update_bits(chip, LP8501_REG_PWR_CONFIG,
  60. LP8501_PWR_CONFIG_M, chip->pdata->pwr_sel);
  61. }
  62. static void lp8501_run_engine(struct lp55xx_chip *chip, bool start)
  63. {
  64. /* stop engine */
  65. if (!start) {
  66. lp55xx_stop_all_engine(chip);
  67. lp55xx_turn_off_channels(chip);
  68. return;
  69. }
  70. lp55xx_run_engine_common(chip);
  71. }
  72. /* Chip specific configurations */
  73. static struct lp55xx_device_config lp8501_cfg = {
  74. .reg_op_mode = {
  75. .addr = LP8501_REG_OP_MODE,
  76. },
  77. .reg_exec = {
  78. .addr = LP8501_REG_ENABLE,
  79. },
  80. .engine_busy = {
  81. .addr = LP8501_REG_STATUS,
  82. .mask = LP8501_ENGINE_BUSY,
  83. },
  84. .reset = {
  85. .addr = LP8501_REG_RESET,
  86. .val = LP8501_RESET,
  87. },
  88. .enable = {
  89. .addr = LP8501_REG_ENABLE,
  90. .val = LP8501_ENABLE,
  91. },
  92. .prog_mem_base = {
  93. .addr = LP8501_REG_PROG_MEM,
  94. },
  95. .reg_led_pwm_base = {
  96. .addr = LP8501_REG_LED_PWM_BASE,
  97. },
  98. .reg_led_current_base = {
  99. .addr = LP8501_REG_LED_CURRENT_BASE,
  100. },
  101. .pages_per_engine = LP8501_PAGES_PER_ENGINE,
  102. .max_channel = LP8501_MAX_LEDS,
  103. .post_init_device = lp8501_post_init_device,
  104. .brightness_fn = lp55xx_led_brightness,
  105. .set_led_current = lp55xx_set_led_current,
  106. .firmware_cb = lp55xx_firmware_loaded_cb,
  107. .run_engine = lp8501_run_engine,
  108. };
  109. static const struct i2c_device_id lp8501_id[] = {
  110. { "lp8501", .driver_data = (kernel_ulong_t)&lp8501_cfg, },
  111. { }
  112. };
  113. MODULE_DEVICE_TABLE(i2c, lp8501_id);
  114. static const struct of_device_id of_lp8501_leds_match[] = {
  115. { .compatible = "ti,lp8501", .data = &lp8501_cfg, },
  116. {},
  117. };
  118. MODULE_DEVICE_TABLE(of, of_lp8501_leds_match);
  119. static struct i2c_driver lp8501_driver = {
  120. .driver = {
  121. .name = "lp8501",
  122. .of_match_table = of_lp8501_leds_match,
  123. },
  124. .probe = lp55xx_probe,
  125. .remove = lp55xx_remove,
  126. .id_table = lp8501_id,
  127. };
  128. module_i2c_driver(lp8501_driver);
  129. MODULE_DESCRIPTION("Texas Instruments LP8501 LED driver");
  130. MODULE_AUTHOR("Milo Kim");
  131. MODULE_LICENSE("GPL");