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