1 // SPDX-License-Identifier: GPL-2.0 2 // TI LM3697 LED chip family driver 3 // Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/ 4 5 #include <linux/gpio/consumer.h> 6 #include <linux/i2c.h> 7 #include <linux/of.h> 8 #include <linux/of_gpio.h> 9 #include <linux/regulator/consumer.h> 10 #include <linux/leds-ti-lmu-common.h> 11 12 #define LM3697_REV 0x0 13 #define LM3697_RESET 0x1 14 #define LM3697_OUTPUT_CONFIG 0x10 15 #define LM3697_CTRL_A_RAMP 0x11 16 #define LM3697_CTRL_B_RAMP 0x12 17 #define LM3697_CTRL_A_B_RT_RAMP 0x13 18 #define LM3697_CTRL_A_B_RAMP_CFG 0x14 19 #define LM3697_CTRL_A_B_BRT_CFG 0x16 20 #define LM3697_CTRL_A_FS_CURR_CFG 0x17 21 #define LM3697_CTRL_B_FS_CURR_CFG 0x18 22 #define LM3697_PWM_CFG 0x1c 23 #define LM3697_CTRL_A_BRT_LSB 0x20 24 #define LM3697_CTRL_A_BRT_MSB 0x21 25 #define LM3697_CTRL_B_BRT_LSB 0x22 26 #define LM3697_CTRL_B_BRT_MSB 0x23 27 #define LM3697_CTRL_ENABLE 0x24 28 29 #define LM3697_SW_RESET BIT(0) 30 31 #define LM3697_CTRL_A_EN BIT(0) 32 #define LM3697_CTRL_B_EN BIT(1) 33 #define LM3697_CTRL_A_B_EN (LM3697_CTRL_A_EN | LM3697_CTRL_B_EN) 34 35 #define LM3697_MAX_LED_STRINGS 3 36 37 #define LM3697_CONTROL_A 0 38 #define LM3697_CONTROL_B 1 39 #define LM3697_MAX_CONTROL_BANKS 2 40 41 /** 42 * struct lm3697_led - 43 * @hvled_strings: Array of LED strings associated with a control bank 44 * @label: LED label 45 * @led_dev: LED class device 46 * @priv: Pointer to the device struct 47 * @lmu_data: Register and setting values for common code 48 * @control_bank: Control bank the LED is associated to. 0 is control bank A 49 * 1 is control bank B 50 * @enabled: LED brightness level (or LED_OFF) 51 * @num_leds: Number of LEDs available 52 */ 53 struct lm3697_led { 54 u32 hvled_strings[LM3697_MAX_LED_STRINGS]; 55 char label[LED_MAX_NAME_SIZE]; 56 struct led_classdev led_dev; 57 struct lm3697 *priv; 58 struct ti_lmu_bank lmu_data; 59 int control_bank; 60 int enabled; 61 int num_leds; 62 }; 63 64 /** 65 * struct lm3697 - 66 * @enable_gpio: Hardware enable gpio 67 * @regulator: LED supply regulator pointer 68 * @client: Pointer to the I2C client 69 * @regmap: Devices register map 70 * @dev: Pointer to the devices device struct 71 * @lock: Lock for reading/writing the device 72 * @leds: Array of LED strings 73 * @bank_cfg: OUTPUT_CONFIG register values 74 * @num_banks: Number of control banks 75 */ 76 struct lm3697 { 77 struct gpio_desc *enable_gpio; 78 struct regulator *regulator; 79 struct i2c_client *client; 80 struct regmap *regmap; 81 struct device *dev; 82 struct mutex lock; 83 84 int bank_cfg; 85 int num_banks; 86 87 struct lm3697_led leds[]; 88 }; 89 90 static const struct reg_default lm3697_reg_defs[] = { 91 {LM3697_OUTPUT_CONFIG, 0x6}, 92 {LM3697_CTRL_A_RAMP, 0x0}, 93 {LM3697_CTRL_B_RAMP, 0x0}, 94 {LM3697_CTRL_A_B_RT_RAMP, 0x0}, 95 {LM3697_CTRL_A_B_RAMP_CFG, 0x0}, 96 {LM3697_CTRL_A_B_BRT_CFG, 0x0}, 97 {LM3697_CTRL_A_FS_CURR_CFG, 0x13}, 98 {LM3697_CTRL_B_FS_CURR_CFG, 0x13}, 99 {LM3697_PWM_CFG, 0xc}, 100 {LM3697_CTRL_A_BRT_LSB, 0x0}, 101 {LM3697_CTRL_A_BRT_MSB, 0x0}, 102 {LM3697_CTRL_B_BRT_LSB, 0x0}, 103 {LM3697_CTRL_B_BRT_MSB, 0x0}, 104 {LM3697_CTRL_ENABLE, 0x0}, 105 }; 106 107 static const struct regmap_config lm3697_regmap_config = { 108 .reg_bits = 8, 109 .val_bits = 8, 110 111 .max_register = LM3697_CTRL_ENABLE, 112 .reg_defaults = lm3697_reg_defs, 113 .num_reg_defaults = ARRAY_SIZE(lm3697_reg_defs), 114 .cache_type = REGCACHE_FLAT, 115 }; 116 117 static int lm3697_brightness_set(struct led_classdev *led_cdev, 118 enum led_brightness brt_val) 119 { 120 struct lm3697_led *led = container_of(led_cdev, struct lm3697_led, 121 led_dev); 122 int ctrl_en_val = (1 << led->control_bank); 123 struct device *dev = led->priv->dev; 124 int ret; 125 126 mutex_lock(&led->priv->lock); 127 128 if (brt_val == LED_OFF) { 129 ret = regmap_update_bits(led->priv->regmap, LM3697_CTRL_ENABLE, 130 ctrl_en_val, ~ctrl_en_val); 131 if (ret) { 132 dev_err(dev, "Cannot write ctrl register\n"); 133 goto brightness_out; 134 } 135 136 led->enabled = LED_OFF; 137 } else { 138 ret = ti_lmu_common_set_brightness(&led->lmu_data, brt_val); 139 if (ret) { 140 dev_err(dev, "Cannot write brightness\n"); 141 goto brightness_out; 142 } 143 144 if (!led->enabled) { 145 ret = regmap_update_bits(led->priv->regmap, 146 LM3697_CTRL_ENABLE, 147 ctrl_en_val, ctrl_en_val); 148 if (ret) { 149 dev_err(dev, "Cannot enable the device\n"); 150 goto brightness_out; 151 } 152 153 led->enabled = brt_val; 154 } 155 } 156 157 brightness_out: 158 mutex_unlock(&led->priv->lock); 159 return ret; 160 } 161 162 static int lm3697_init(struct lm3697 *priv) 163 { 164 struct device *dev = priv->dev; 165 struct lm3697_led *led; 166 int i, ret; 167 168 if (priv->enable_gpio) { 169 gpiod_direction_output(priv->enable_gpio, 1); 170 } else { 171 ret = regmap_write(priv->regmap, LM3697_RESET, LM3697_SW_RESET); 172 if (ret) { 173 dev_err(dev, "Cannot reset the device\n"); 174 goto out; 175 } 176 } 177 178 ret = regmap_write(priv->regmap, LM3697_CTRL_ENABLE, 0x0); 179 if (ret) { 180 dev_err(dev, "Cannot write ctrl enable\n"); 181 goto out; 182 } 183 184 ret = regmap_write(priv->regmap, LM3697_OUTPUT_CONFIG, priv->bank_cfg); 185 if (ret) 186 dev_err(dev, "Cannot write OUTPUT config\n"); 187 188 for (i = 0; i < priv->num_banks; i++) { 189 led = &priv->leds[i]; 190 ret = ti_lmu_common_set_ramp(&led->lmu_data); 191 if (ret) 192 dev_err(dev, "Setting the ramp rate failed\n"); 193 } 194 out: 195 return ret; 196 } 197 198 static int lm3697_probe_dt(struct lm3697 *priv) 199 { 200 struct fwnode_handle *child = NULL; 201 struct device *dev = priv->dev; 202 struct lm3697_led *led; 203 int ret = -EINVAL; 204 int control_bank; 205 size_t i = 0; 206 int j; 207 208 priv->enable_gpio = devm_gpiod_get_optional(dev, "enable", 209 GPIOD_OUT_LOW); 210 if (IS_ERR(priv->enable_gpio)) 211 return dev_err_probe(dev, PTR_ERR(priv->enable_gpio), 212 "Failed to get enable GPIO\n"); 213 214 priv->regulator = devm_regulator_get(dev, "vled"); 215 if (IS_ERR(priv->regulator)) 216 priv->regulator = NULL; 217 218 device_for_each_child_node(dev, child) { 219 struct led_init_data init_data = {}; 220 221 ret = fwnode_property_read_u32(child, "reg", &control_bank); 222 if (ret) { 223 dev_err(dev, "reg property missing\n"); 224 fwnode_handle_put(child); 225 goto child_out; 226 } 227 228 if (control_bank > LM3697_CONTROL_B) { 229 dev_err(dev, "reg property is invalid\n"); 230 ret = -EINVAL; 231 fwnode_handle_put(child); 232 goto child_out; 233 } 234 235 led = &priv->leds[i]; 236 237 ret = ti_lmu_common_get_brt_res(dev, child, &led->lmu_data); 238 if (ret) 239 dev_warn(dev, 240 "brightness resolution property missing\n"); 241 242 led->control_bank = control_bank; 243 led->lmu_data.regmap = priv->regmap; 244 led->lmu_data.runtime_ramp_reg = LM3697_CTRL_A_RAMP + 245 control_bank; 246 led->lmu_data.msb_brightness_reg = LM3697_CTRL_A_BRT_MSB + 247 led->control_bank * 2; 248 led->lmu_data.lsb_brightness_reg = LM3697_CTRL_A_BRT_LSB + 249 led->control_bank * 2; 250 251 led->num_leds = fwnode_property_count_u32(child, "led-sources"); 252 if (led->num_leds > LM3697_MAX_LED_STRINGS) { 253 dev_err(dev, "Too many LED strings defined\n"); 254 continue; 255 } 256 257 ret = fwnode_property_read_u32_array(child, "led-sources", 258 led->hvled_strings, 259 led->num_leds); 260 if (ret) { 261 dev_err(dev, "led-sources property missing\n"); 262 fwnode_handle_put(child); 263 goto child_out; 264 } 265 266 for (j = 0; j < led->num_leds; j++) 267 priv->bank_cfg |= 268 (led->control_bank << led->hvled_strings[j]); 269 270 ret = ti_lmu_common_get_ramp_params(dev, child, &led->lmu_data); 271 if (ret) 272 dev_warn(dev, "runtime-ramp properties missing\n"); 273 274 init_data.fwnode = child; 275 init_data.devicename = priv->client->name; 276 /* for backwards compatibility if `label` is not present */ 277 init_data.default_label = ":"; 278 279 led->priv = priv; 280 led->led_dev.max_brightness = led->lmu_data.max_brightness; 281 led->led_dev.brightness_set_blocking = lm3697_brightness_set; 282 283 ret = devm_led_classdev_register_ext(dev, &led->led_dev, 284 &init_data); 285 if (ret) { 286 dev_err(dev, "led register err: %d\n", ret); 287 fwnode_handle_put(child); 288 goto child_out; 289 } 290 291 i++; 292 } 293 294 child_out: 295 return ret; 296 } 297 298 static int lm3697_probe(struct i2c_client *client, 299 const struct i2c_device_id *id) 300 { 301 struct device *dev = &client->dev; 302 struct lm3697 *led; 303 int count; 304 int ret; 305 306 count = device_get_child_node_count(dev); 307 if (!count || count > LM3697_MAX_CONTROL_BANKS) { 308 dev_err(dev, "Strange device tree!"); 309 return -ENODEV; 310 } 311 312 led = devm_kzalloc(dev, struct_size(led, leds, count), GFP_KERNEL); 313 if (!led) 314 return -ENOMEM; 315 316 mutex_init(&led->lock); 317 i2c_set_clientdata(client, led); 318 319 led->client = client; 320 led->dev = dev; 321 led->num_banks = count; 322 led->regmap = devm_regmap_init_i2c(client, &lm3697_regmap_config); 323 if (IS_ERR(led->regmap)) { 324 ret = PTR_ERR(led->regmap); 325 dev_err(dev, "Failed to allocate register map: %d\n", ret); 326 return ret; 327 } 328 329 ret = lm3697_probe_dt(led); 330 if (ret) 331 return ret; 332 333 return lm3697_init(led); 334 } 335 336 static int lm3697_remove(struct i2c_client *client) 337 { 338 struct lm3697 *led = i2c_get_clientdata(client); 339 struct device *dev = &led->client->dev; 340 int ret; 341 342 ret = regmap_update_bits(led->regmap, LM3697_CTRL_ENABLE, 343 LM3697_CTRL_A_B_EN, 0); 344 if (ret) { 345 dev_err(dev, "Failed to disable the device\n"); 346 return ret; 347 } 348 349 if (led->enable_gpio) 350 gpiod_direction_output(led->enable_gpio, 0); 351 352 if (led->regulator) { 353 ret = regulator_disable(led->regulator); 354 if (ret) 355 dev_err(dev, "Failed to disable regulator\n"); 356 } 357 358 mutex_destroy(&led->lock); 359 360 return 0; 361 } 362 363 static const struct i2c_device_id lm3697_id[] = { 364 { "lm3697", 0 }, 365 { } 366 }; 367 MODULE_DEVICE_TABLE(i2c, lm3697_id); 368 369 static const struct of_device_id of_lm3697_leds_match[] = { 370 { .compatible = "ti,lm3697", }, 371 {}, 372 }; 373 MODULE_DEVICE_TABLE(of, of_lm3697_leds_match); 374 375 static struct i2c_driver lm3697_driver = { 376 .driver = { 377 .name = "lm3697", 378 .of_match_table = of_lm3697_leds_match, 379 }, 380 .probe = lm3697_probe, 381 .remove = lm3697_remove, 382 .id_table = lm3697_id, 383 }; 384 module_i2c_driver(lm3697_driver); 385 386 MODULE_DESCRIPTION("Texas Instruments LM3697 LED driver"); 387 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>"); 388 MODULE_LICENSE("GPL v2"); 389