Dan Murphy | 242b811 | 2020-08-12 14:50:16 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // TI LP50XX LED chip family driver |
| 3 | // Copyright (C) 2018-20 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/module.h> |
| 10 | #include <linux/mutex.h> |
| 11 | #include <linux/of.h> |
| 12 | #include <linux/of_gpio.h> |
| 13 | #include <linux/regmap.h> |
| 14 | #include <linux/regulator/consumer.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <uapi/linux/uleds.h> |
| 17 | |
| 18 | #include <linux/led-class-multicolor.h> |
| 19 | |
| 20 | #include "leds.h" |
| 21 | |
| 22 | #define LP50XX_DEV_CFG0 0x00 |
| 23 | #define LP50XX_DEV_CFG1 0x01 |
| 24 | #define LP50XX_LED_CFG0 0x02 |
| 25 | |
| 26 | /* LP5009 and LP5012 registers */ |
| 27 | #define LP5012_BNK_BRT 0x03 |
| 28 | #define LP5012_BNKA_CLR 0x04 |
| 29 | #define LP5012_BNKB_CLR 0x05 |
| 30 | #define LP5012_BNKC_CLR 0x06 |
| 31 | #define LP5012_LED0_BRT 0x07 |
| 32 | #define LP5012_OUT0_CLR 0x0b |
| 33 | #define LP5012_RESET 0x17 |
| 34 | |
| 35 | /* LP5018 and LP5024 registers */ |
| 36 | #define LP5024_BNK_BRT 0x03 |
| 37 | #define LP5024_BNKA_CLR 0x04 |
| 38 | #define LP5024_BNKB_CLR 0x05 |
| 39 | #define LP5024_BNKC_CLR 0x06 |
| 40 | #define LP5024_LED0_BRT 0x07 |
| 41 | #define LP5024_OUT0_CLR 0x0f |
| 42 | #define LP5024_RESET 0x27 |
| 43 | |
| 44 | /* LP5030 and LP5036 registers */ |
| 45 | #define LP5036_LED_CFG1 0x03 |
| 46 | #define LP5036_BNK_BRT 0x04 |
| 47 | #define LP5036_BNKA_CLR 0x05 |
| 48 | #define LP5036_BNKB_CLR 0x06 |
| 49 | #define LP5036_BNKC_CLR 0x07 |
| 50 | #define LP5036_LED0_BRT 0x08 |
| 51 | #define LP5036_OUT0_CLR 0x14 |
| 52 | #define LP5036_RESET 0x38 |
| 53 | |
| 54 | #define LP50XX_SW_RESET 0xff |
| 55 | #define LP50XX_CHIP_EN BIT(6) |
| 56 | |
| 57 | /* There are 3 LED outputs per bank */ |
| 58 | #define LP50XX_LEDS_PER_MODULE 3 |
| 59 | |
| 60 | #define LP5009_MAX_LED_MODULES 2 |
| 61 | #define LP5012_MAX_LED_MODULES 4 |
| 62 | #define LP5018_MAX_LED_MODULES 6 |
| 63 | #define LP5024_MAX_LED_MODULES 8 |
| 64 | #define LP5030_MAX_LED_MODULES 10 |
| 65 | #define LP5036_MAX_LED_MODULES 12 |
| 66 | |
| 67 | static const struct reg_default lp5012_reg_defs[] = { |
| 68 | {LP50XX_DEV_CFG0, 0x0}, |
| 69 | {LP50XX_DEV_CFG1, 0x3c}, |
| 70 | {LP50XX_LED_CFG0, 0x0}, |
| 71 | {LP5012_BNK_BRT, 0xff}, |
| 72 | {LP5012_BNKA_CLR, 0x0f}, |
| 73 | {LP5012_BNKB_CLR, 0x0f}, |
| 74 | {LP5012_BNKC_CLR, 0x0f}, |
| 75 | {LP5012_LED0_BRT, 0x0f}, |
| 76 | /* LEDX_BRT registers are all 0xff for defaults */ |
| 77 | {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff}, |
| 78 | {LP5012_OUT0_CLR, 0x0f}, |
| 79 | /* OUTX_CLR registers are all 0x0 for defaults */ |
| 80 | {0x0c, 0x00}, {0x0d, 0x00}, {0x0e, 0x00}, {0x0f, 0x00}, {0x10, 0x00}, |
| 81 | {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00}, {0x14, 0x00}, {0x15, 0x00}, |
| 82 | {0x16, 0x00}, |
| 83 | {LP5012_RESET, 0x00} |
| 84 | }; |
| 85 | |
| 86 | static const struct reg_default lp5024_reg_defs[] = { |
| 87 | {LP50XX_DEV_CFG0, 0x0}, |
| 88 | {LP50XX_DEV_CFG1, 0x3c}, |
| 89 | {LP50XX_LED_CFG0, 0x0}, |
| 90 | {LP5024_BNK_BRT, 0xff}, |
| 91 | {LP5024_BNKA_CLR, 0x0f}, |
| 92 | {LP5024_BNKB_CLR, 0x0f}, |
| 93 | {LP5024_BNKC_CLR, 0x0f}, |
| 94 | {LP5024_LED0_BRT, 0x0f}, |
| 95 | /* LEDX_BRT registers are all 0xff for defaults */ |
| 96 | {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff}, {0x0b, 0xff}, {0x0c, 0xff}, |
| 97 | {0x0d, 0xff}, {0x0e, 0xff}, |
| 98 | {LP5024_OUT0_CLR, 0x0f}, |
| 99 | /* OUTX_CLR registers are all 0x0 for defaults */ |
| 100 | {0x10, 0x00}, {0x11, 0x00}, {0x12, 0x00}, {0x13, 0x00}, {0x14, 0x00}, |
| 101 | {0x15, 0x00}, {0x16, 0x00}, {0x17, 0x00}, {0x18, 0x00}, {0x19, 0x00}, |
| 102 | {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00}, |
| 103 | {0x1f, 0x00}, {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00}, |
| 104 | {0x24, 0x00}, {0x25, 0x00}, {0x26, 0x00}, |
| 105 | {LP5024_RESET, 0x00} |
| 106 | }; |
| 107 | |
| 108 | static const struct reg_default lp5036_reg_defs[] = { |
| 109 | {LP50XX_DEV_CFG0, 0x0}, |
| 110 | {LP50XX_DEV_CFG1, 0x3c}, |
| 111 | {LP50XX_LED_CFG0, 0x0}, |
| 112 | {LP5036_LED_CFG1, 0x0}, |
| 113 | {LP5036_BNK_BRT, 0xff}, |
| 114 | {LP5036_BNKA_CLR, 0x0f}, |
| 115 | {LP5036_BNKB_CLR, 0x0f}, |
| 116 | {LP5036_BNKC_CLR, 0x0f}, |
| 117 | {LP5036_LED0_BRT, 0x0f}, |
| 118 | /* LEDX_BRT registers are all 0xff for defaults */ |
| 119 | {0x08, 0xff}, {0x09, 0xff}, {0x0a, 0xff}, {0x0b, 0xff}, {0x0c, 0xff}, |
| 120 | {0x0d, 0xff}, {0x0e, 0xff}, {0x0f, 0xff}, {0x10, 0xff}, {0x11, 0xff}, |
| 121 | {0x12, 0xff}, {0x13, 0xff}, |
| 122 | {LP5036_OUT0_CLR, 0x0f}, |
| 123 | /* OUTX_CLR registers are all 0x0 for defaults */ |
| 124 | {0x15, 0x00}, {0x16, 0x00}, {0x17, 0x00}, {0x18, 0x00}, {0x19, 0x00}, |
| 125 | {0x1a, 0x00}, {0x1b, 0x00}, {0x1c, 0x00}, {0x1d, 0x00}, {0x1e, 0x00}, |
| 126 | {0x1f, 0x00}, {0x20, 0x00}, {0x21, 0x00}, {0x22, 0x00}, {0x23, 0x00}, |
| 127 | {0x24, 0x00}, {0x25, 0x00}, {0x26, 0x00}, {0x27, 0x00}, {0x28, 0x00}, |
| 128 | {0x29, 0x00}, {0x2a, 0x00}, {0x2b, 0x00}, {0x2c, 0x00}, {0x2d, 0x00}, |
| 129 | {0x2e, 0x00}, {0x2f, 0x00}, {0x30, 0x00}, {0x31, 0x00}, {0x32, 0x00}, |
| 130 | {0x33, 0x00}, {0x34, 0x00}, {0x35, 0x00}, {0x36, 0x00}, {0x37, 0x00}, |
| 131 | {LP5036_RESET, 0x00} |
| 132 | }; |
| 133 | |
| 134 | static const struct regmap_config lp5012_regmap_config = { |
| 135 | .reg_bits = 8, |
| 136 | .val_bits = 8, |
| 137 | |
| 138 | .max_register = LP5012_RESET, |
| 139 | .reg_defaults = lp5012_reg_defs, |
| 140 | .num_reg_defaults = ARRAY_SIZE(lp5012_reg_defs), |
| 141 | .cache_type = REGCACHE_FLAT, |
| 142 | }; |
| 143 | |
| 144 | static const struct regmap_config lp5024_regmap_config = { |
| 145 | .reg_bits = 8, |
| 146 | .val_bits = 8, |
| 147 | |
| 148 | .max_register = LP5024_RESET, |
| 149 | .reg_defaults = lp5024_reg_defs, |
| 150 | .num_reg_defaults = ARRAY_SIZE(lp5024_reg_defs), |
| 151 | .cache_type = REGCACHE_FLAT, |
| 152 | }; |
| 153 | |
| 154 | static const struct regmap_config lp5036_regmap_config = { |
| 155 | .reg_bits = 8, |
| 156 | .val_bits = 8, |
| 157 | |
| 158 | .max_register = LP5036_RESET, |
| 159 | .reg_defaults = lp5036_reg_defs, |
| 160 | .num_reg_defaults = ARRAY_SIZE(lp5036_reg_defs), |
| 161 | .cache_type = REGCACHE_FLAT, |
| 162 | }; |
| 163 | |
| 164 | enum lp50xx_model { |
| 165 | LP5009, |
| 166 | LP5012, |
| 167 | LP5018, |
| 168 | LP5024, |
| 169 | LP5030, |
| 170 | LP5036, |
| 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * struct lp50xx_chip_info - |
| 175 | * @lp50xx_regmap_config: regmap register configuration |
| 176 | * @model_id: LED device model |
| 177 | * @max_modules: total number of supported LED modules |
| 178 | * @num_leds: number of LED outputs available on the device |
| 179 | * @led_brightness0_reg: first brightness register of the device |
| 180 | * @mix_out0_reg: first color mix register of the device |
| 181 | * @bank_brt_reg: bank brightness register |
| 182 | * @bank_mix_reg: color mix register |
| 183 | * @reset_reg: device reset register |
| 184 | */ |
| 185 | struct lp50xx_chip_info { |
| 186 | const struct regmap_config *lp50xx_regmap_config; |
| 187 | int model_id; |
| 188 | u8 max_modules; |
| 189 | u8 num_leds; |
| 190 | u8 led_brightness0_reg; |
| 191 | u8 mix_out0_reg; |
| 192 | u8 bank_brt_reg; |
| 193 | u8 bank_mix_reg; |
| 194 | u8 reset_reg; |
| 195 | }; |
| 196 | |
| 197 | static const struct lp50xx_chip_info lp50xx_chip_info_tbl[] = { |
| 198 | [LP5009] = { |
| 199 | .model_id = LP5009, |
| 200 | .max_modules = LP5009_MAX_LED_MODULES, |
| 201 | .num_leds = LP5009_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE, |
| 202 | .led_brightness0_reg = LP5012_LED0_BRT, |
| 203 | .mix_out0_reg = LP5012_OUT0_CLR, |
| 204 | .bank_brt_reg = LP5012_BNK_BRT, |
| 205 | .bank_mix_reg = LP5012_BNKA_CLR, |
| 206 | .reset_reg = LP5012_RESET, |
| 207 | .lp50xx_regmap_config = &lp5012_regmap_config, |
| 208 | }, |
| 209 | [LP5012] = { |
| 210 | .model_id = LP5012, |
| 211 | .max_modules = LP5012_MAX_LED_MODULES, |
| 212 | .num_leds = LP5012_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE, |
| 213 | .led_brightness0_reg = LP5012_LED0_BRT, |
| 214 | .mix_out0_reg = LP5012_OUT0_CLR, |
| 215 | .bank_brt_reg = LP5012_BNK_BRT, |
| 216 | .bank_mix_reg = LP5012_BNKA_CLR, |
| 217 | .reset_reg = LP5012_RESET, |
| 218 | .lp50xx_regmap_config = &lp5012_regmap_config, |
| 219 | }, |
| 220 | [LP5018] = { |
| 221 | .model_id = LP5018, |
| 222 | .max_modules = LP5018_MAX_LED_MODULES, |
| 223 | .num_leds = LP5018_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE, |
| 224 | .led_brightness0_reg = LP5024_LED0_BRT, |
| 225 | .mix_out0_reg = LP5024_OUT0_CLR, |
| 226 | .bank_brt_reg = LP5024_BNK_BRT, |
| 227 | .bank_mix_reg = LP5024_BNKA_CLR, |
| 228 | .reset_reg = LP5024_RESET, |
| 229 | .lp50xx_regmap_config = &lp5024_regmap_config, |
| 230 | }, |
| 231 | [LP5024] = { |
| 232 | .model_id = LP5024, |
| 233 | .max_modules = LP5024_MAX_LED_MODULES, |
| 234 | .num_leds = LP5024_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE, |
| 235 | .led_brightness0_reg = LP5024_LED0_BRT, |
| 236 | .mix_out0_reg = LP5024_OUT0_CLR, |
| 237 | .bank_brt_reg = LP5024_BNK_BRT, |
| 238 | .bank_mix_reg = LP5024_BNKA_CLR, |
| 239 | .reset_reg = LP5024_RESET, |
| 240 | .lp50xx_regmap_config = &lp5024_regmap_config, |
| 241 | }, |
| 242 | [LP5030] = { |
| 243 | .model_id = LP5030, |
| 244 | .max_modules = LP5030_MAX_LED_MODULES, |
| 245 | .num_leds = LP5030_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE, |
| 246 | .led_brightness0_reg = LP5036_LED0_BRT, |
| 247 | .mix_out0_reg = LP5036_OUT0_CLR, |
| 248 | .bank_brt_reg = LP5036_BNK_BRT, |
| 249 | .bank_mix_reg = LP5036_BNKA_CLR, |
| 250 | .reset_reg = LP5036_RESET, |
| 251 | .lp50xx_regmap_config = &lp5036_regmap_config, |
| 252 | }, |
| 253 | [LP5036] = { |
| 254 | .model_id = LP5036, |
| 255 | .max_modules = LP5036_MAX_LED_MODULES, |
| 256 | .num_leds = LP5036_MAX_LED_MODULES * LP50XX_LEDS_PER_MODULE, |
| 257 | .led_brightness0_reg = LP5036_LED0_BRT, |
| 258 | .mix_out0_reg = LP5036_OUT0_CLR, |
| 259 | .bank_brt_reg = LP5036_BNK_BRT, |
| 260 | .bank_mix_reg = LP5036_BNKA_CLR, |
| 261 | .reset_reg = LP5036_RESET, |
| 262 | .lp50xx_regmap_config = &lp5036_regmap_config, |
| 263 | }, |
| 264 | }; |
| 265 | |
| 266 | struct lp50xx_led { |
| 267 | struct led_classdev_mc mc_cdev; |
| 268 | struct lp50xx *priv; |
| 269 | unsigned long bank_modules; |
| 270 | int led_intensity[LP50XX_LEDS_PER_MODULE]; |
| 271 | u8 ctrl_bank_enabled; |
| 272 | int led_number; |
| 273 | }; |
| 274 | |
| 275 | /** |
| 276 | * struct lp50xx - |
| 277 | * @enable_gpio: hardware enable gpio |
| 278 | * @regulator: LED supply regulator pointer |
| 279 | * @client: pointer to the I2C client |
| 280 | * @regmap: device register map |
| 281 | * @dev: pointer to the devices device struct |
| 282 | * @lock: lock for reading/writing the device |
| 283 | * @chip_info: chip specific information (ie num_leds) |
| 284 | * @num_of_banked_leds: holds the number of banked LEDs |
| 285 | * @leds: array of LED strings |
| 286 | */ |
| 287 | struct lp50xx { |
| 288 | struct gpio_desc *enable_gpio; |
| 289 | struct regulator *regulator; |
| 290 | struct i2c_client *client; |
| 291 | struct regmap *regmap; |
| 292 | struct device *dev; |
| 293 | struct mutex lock; |
| 294 | const struct lp50xx_chip_info *chip_info; |
| 295 | int num_of_banked_leds; |
| 296 | |
| 297 | /* This needs to be at the end of the struct */ |
| 298 | struct lp50xx_led leds[]; |
| 299 | }; |
| 300 | |
| 301 | static struct lp50xx_led *mcled_cdev_to_led(struct led_classdev_mc *mc_cdev) |
| 302 | { |
| 303 | return container_of(mc_cdev, struct lp50xx_led, mc_cdev); |
| 304 | } |
| 305 | |
| 306 | static int lp50xx_brightness_set(struct led_classdev *cdev, |
| 307 | enum led_brightness brightness) |
| 308 | { |
| 309 | struct led_classdev_mc *mc_dev = lcdev_to_mccdev(cdev); |
| 310 | struct lp50xx_led *led = mcled_cdev_to_led(mc_dev); |
| 311 | const struct lp50xx_chip_info *led_chip = led->priv->chip_info; |
| 312 | u8 led_offset, reg_val; |
| 313 | int ret = 0; |
| 314 | int i; |
| 315 | |
| 316 | mutex_lock(&led->priv->lock); |
| 317 | if (led->ctrl_bank_enabled) |
| 318 | reg_val = led_chip->bank_brt_reg; |
| 319 | else |
| 320 | reg_val = led_chip->led_brightness0_reg + |
| 321 | led->led_number; |
| 322 | |
| 323 | ret = regmap_write(led->priv->regmap, reg_val, brightness); |
| 324 | if (ret) { |
| 325 | dev_err(&led->priv->client->dev, |
| 326 | "Cannot write brightness value %d\n", ret); |
| 327 | goto out; |
| 328 | } |
| 329 | |
| 330 | for (i = 0; i < led->mc_cdev.num_colors; i++) { |
| 331 | if (led->ctrl_bank_enabled) { |
| 332 | reg_val = led_chip->bank_mix_reg + i; |
| 333 | } else { |
| 334 | led_offset = (led->led_number * 3) + i; |
| 335 | reg_val = led_chip->mix_out0_reg + led_offset; |
| 336 | } |
| 337 | |
| 338 | ret = regmap_write(led->priv->regmap, reg_val, |
| 339 | mc_dev->subled_info[i].intensity); |
| 340 | if (ret) { |
| 341 | dev_err(&led->priv->client->dev, |
| 342 | "Cannot write intensity value %d\n", ret); |
| 343 | goto out; |
| 344 | } |
| 345 | } |
| 346 | out: |
| 347 | mutex_unlock(&led->priv->lock); |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | static int lp50xx_set_banks(struct lp50xx *priv, u32 led_banks[]) |
| 352 | { |
| 353 | u8 led_config_lo, led_config_hi; |
| 354 | u32 bank_enable_mask = 0; |
| 355 | int ret; |
| 356 | int i; |
| 357 | |
| 358 | for (i = 0; i < priv->chip_info->max_modules; i++) { |
| 359 | if (led_banks[i]) |
| 360 | bank_enable_mask |= (1 << led_banks[i]); |
| 361 | } |
| 362 | |
| 363 | led_config_lo = (u8)(bank_enable_mask & 0xff); |
| 364 | led_config_hi = (u8)(bank_enable_mask >> 8) & 0xff; |
| 365 | |
| 366 | ret = regmap_write(priv->regmap, LP50XX_LED_CFG0, led_config_lo); |
| 367 | if (ret) |
| 368 | return ret; |
| 369 | |
| 370 | if (priv->chip_info->model_id >= LP5030) |
| 371 | ret = regmap_write(priv->regmap, LP5036_LED_CFG1, led_config_hi); |
| 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | static int lp50xx_reset(struct lp50xx *priv) |
| 377 | { |
| 378 | return regmap_write(priv->regmap, priv->chip_info->reset_reg, LP50XX_SW_RESET); |
| 379 | } |
| 380 | |
| 381 | static int lp50xx_enable_disable(struct lp50xx *priv, int enable_disable) |
| 382 | { |
| 383 | int ret; |
| 384 | |
| 385 | if (priv->enable_gpio) { |
| 386 | ret = gpiod_direction_output(priv->enable_gpio, enable_disable); |
| 387 | if (ret) |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | if (enable_disable) |
| 392 | return regmap_write(priv->regmap, LP50XX_DEV_CFG0, LP50XX_CHIP_EN); |
| 393 | else |
| 394 | return regmap_write(priv->regmap, LP50XX_DEV_CFG0, 0); |
| 395 | |
| 396 | } |
| 397 | |
| 398 | static int lp50xx_probe_leds(struct fwnode_handle *child, struct lp50xx *priv, |
| 399 | struct lp50xx_led *led, int num_leds) |
| 400 | { |
| 401 | u32 led_banks[LP5036_MAX_LED_MODULES] = {0}; |
| 402 | int led_number; |
| 403 | int ret; |
| 404 | |
| 405 | if (num_leds > 1) { |
| 406 | if (num_leds > priv->chip_info->max_modules) { |
| 407 | dev_err(&priv->client->dev, "reg property is invalid\n"); |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | |
| 411 | priv->num_of_banked_leds = num_leds; |
| 412 | |
| 413 | ret = fwnode_property_read_u32_array(child, "reg", led_banks, num_leds); |
| 414 | if (ret) { |
| 415 | dev_err(&priv->client->dev, "reg property is missing\n"); |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | ret = lp50xx_set_banks(priv, led_banks); |
| 420 | if (ret) { |
| 421 | dev_err(&priv->client->dev, "Cannot setup banked LEDs\n"); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | led->ctrl_bank_enabled = 1; |
| 426 | } else { |
| 427 | ret = fwnode_property_read_u32(child, "reg", &led_number); |
| 428 | if (ret) { |
| 429 | dev_err(&priv->client->dev, "led reg property missing\n"); |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | if (led_number > priv->chip_info->num_leds) { |
| 434 | dev_err(&priv->client->dev, "led-sources property is invalid\n"); |
| 435 | return -EINVAL; |
| 436 | } |
| 437 | |
| 438 | led->led_number = led_number; |
| 439 | } |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int lp50xx_probe_dt(struct lp50xx *priv) |
| 445 | { |
| 446 | struct fwnode_handle *child = NULL; |
| 447 | struct fwnode_handle *led_node = NULL; |
| 448 | struct led_init_data init_data = {}; |
| 449 | struct led_classdev *led_cdev; |
| 450 | struct mc_subled *mc_led_info; |
| 451 | struct lp50xx_led *led; |
| 452 | int ret = -EINVAL; |
| 453 | int num_colors; |
| 454 | u32 color_id; |
| 455 | int i = 0; |
| 456 | |
| 457 | priv->enable_gpio = devm_gpiod_get_optional(priv->dev, "enable", GPIOD_OUT_LOW); |
Andy Shevchenko | 9a10def | 2021-02-16 17:50:44 +0200 | [diff] [blame] | 458 | if (IS_ERR(priv->enable_gpio)) |
| 459 | return dev_err_probe(priv->dev, PTR_ERR(priv->enable_gpio), |
| 460 | "Failed to get enable GPIO\n"); |
Dan Murphy | 242b811 | 2020-08-12 14:50:16 -0500 | [diff] [blame] | 461 | |
| 462 | priv->regulator = devm_regulator_get(priv->dev, "vled"); |
| 463 | if (IS_ERR(priv->regulator)) |
| 464 | priv->regulator = NULL; |
| 465 | |
| 466 | device_for_each_child_node(priv->dev, child) { |
| 467 | led = &priv->leds[i]; |
| 468 | ret = fwnode_property_count_u32(child, "reg"); |
| 469 | if (ret < 0) { |
| 470 | dev_err(&priv->client->dev, "reg property is invalid\n"); |
| 471 | goto child_out; |
| 472 | } |
| 473 | |
| 474 | ret = lp50xx_probe_leds(child, priv, led, ret); |
| 475 | if (ret) |
| 476 | goto child_out; |
| 477 | |
| 478 | init_data.fwnode = child; |
| 479 | num_colors = 0; |
| 480 | |
| 481 | /* |
| 482 | * There are only 3 LEDs per module otherwise they should be |
| 483 | * banked which also is presented as 3 LEDs. |
| 484 | */ |
| 485 | mc_led_info = devm_kcalloc(priv->dev, LP50XX_LEDS_PER_MODULE, |
| 486 | sizeof(*mc_led_info), GFP_KERNEL); |
Christophe JAILLET | 6d8d014 | 2020-09-22 23:05:15 +0200 | [diff] [blame] | 487 | if (!mc_led_info) { |
| 488 | ret = -ENOMEM; |
| 489 | goto child_out; |
| 490 | } |
Dan Murphy | 242b811 | 2020-08-12 14:50:16 -0500 | [diff] [blame] | 491 | |
| 492 | fwnode_for_each_child_node(child, led_node) { |
| 493 | ret = fwnode_property_read_u32(led_node, "color", |
| 494 | &color_id); |
| 495 | if (ret) { |
| 496 | dev_err(priv->dev, "Cannot read color\n"); |
| 497 | goto child_out; |
| 498 | } |
| 499 | |
| 500 | mc_led_info[num_colors].color_index = color_id; |
| 501 | num_colors++; |
| 502 | } |
| 503 | |
| 504 | led->priv = priv; |
| 505 | led->mc_cdev.num_colors = num_colors; |
| 506 | led->mc_cdev.subled_info = mc_led_info; |
| 507 | led_cdev = &led->mc_cdev.led_cdev; |
| 508 | led_cdev->brightness_set_blocking = lp50xx_brightness_set; |
| 509 | |
Dan Murphy | 242b811 | 2020-08-12 14:50:16 -0500 | [diff] [blame] | 510 | ret = devm_led_classdev_multicolor_register_ext(&priv->client->dev, |
| 511 | &led->mc_cdev, |
| 512 | &init_data); |
| 513 | if (ret) { |
| 514 | dev_err(&priv->client->dev, "led register err: %d\n", |
| 515 | ret); |
| 516 | goto child_out; |
| 517 | } |
| 518 | i++; |
| 519 | fwnode_handle_put(child); |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | |
| 524 | child_out: |
| 525 | fwnode_handle_put(child); |
| 526 | return ret; |
| 527 | } |
| 528 | |
Andy Shevchenko | ea1ff99 | 2021-02-16 17:50:45 +0200 | [diff] [blame^] | 529 | static int lp50xx_probe(struct i2c_client *client) |
Dan Murphy | 242b811 | 2020-08-12 14:50:16 -0500 | [diff] [blame] | 530 | { |
| 531 | struct lp50xx *led; |
| 532 | int count; |
| 533 | int ret; |
| 534 | |
| 535 | count = device_get_child_node_count(&client->dev); |
| 536 | if (!count) { |
| 537 | dev_err(&client->dev, "LEDs are not defined in device tree!"); |
| 538 | return -ENODEV; |
| 539 | } |
| 540 | |
| 541 | led = devm_kzalloc(&client->dev, struct_size(led, leds, count), |
| 542 | GFP_KERNEL); |
| 543 | if (!led) |
| 544 | return -ENOMEM; |
| 545 | |
| 546 | mutex_init(&led->lock); |
| 547 | led->client = client; |
| 548 | led->dev = &client->dev; |
Andy Shevchenko | ea1ff99 | 2021-02-16 17:50:45 +0200 | [diff] [blame^] | 549 | led->chip_info = device_get_match_data(&client->dev); |
Dan Murphy | 242b811 | 2020-08-12 14:50:16 -0500 | [diff] [blame] | 550 | i2c_set_clientdata(client, led); |
| 551 | led->regmap = devm_regmap_init_i2c(client, |
| 552 | led->chip_info->lp50xx_regmap_config); |
| 553 | if (IS_ERR(led->regmap)) { |
| 554 | ret = PTR_ERR(led->regmap); |
| 555 | dev_err(&client->dev, "Failed to allocate register map: %d\n", |
| 556 | ret); |
| 557 | return ret; |
| 558 | } |
| 559 | |
| 560 | ret = lp50xx_reset(led); |
| 561 | if (ret) |
| 562 | return ret; |
| 563 | |
| 564 | ret = lp50xx_enable_disable(led, 1); |
| 565 | if (ret) |
| 566 | return ret; |
| 567 | |
| 568 | return lp50xx_probe_dt(led); |
| 569 | } |
| 570 | |
| 571 | static int lp50xx_remove(struct i2c_client *client) |
| 572 | { |
| 573 | struct lp50xx *led = i2c_get_clientdata(client); |
| 574 | int ret; |
| 575 | |
| 576 | ret = lp50xx_enable_disable(led, 0); |
| 577 | if (ret) { |
| 578 | dev_err(&led->client->dev, "Failed to disable chip\n"); |
| 579 | return ret; |
| 580 | } |
| 581 | |
| 582 | if (led->regulator) { |
| 583 | ret = regulator_disable(led->regulator); |
| 584 | if (ret) |
| 585 | dev_err(&led->client->dev, |
| 586 | "Failed to disable regulator\n"); |
| 587 | } |
| 588 | |
| 589 | mutex_destroy(&led->lock); |
| 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static const struct i2c_device_id lp50xx_id[] = { |
Andy Shevchenko | ea1ff99 | 2021-02-16 17:50:45 +0200 | [diff] [blame^] | 595 | { "lp5009", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5009] }, |
| 596 | { "lp5012", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5012] }, |
| 597 | { "lp5018", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5018] }, |
| 598 | { "lp5024", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5024] }, |
| 599 | { "lp5030", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5030] }, |
| 600 | { "lp5036", (kernel_ulong_t)&lp50xx_chip_info_tbl[LP5036] }, |
Dan Murphy | 242b811 | 2020-08-12 14:50:16 -0500 | [diff] [blame] | 601 | { } |
| 602 | }; |
| 603 | MODULE_DEVICE_TABLE(i2c, lp50xx_id); |
| 604 | |
| 605 | static const struct of_device_id of_lp50xx_leds_match[] = { |
Andy Shevchenko | ea1ff99 | 2021-02-16 17:50:45 +0200 | [diff] [blame^] | 606 | { .compatible = "ti,lp5009", .data = &lp50xx_chip_info_tbl[LP5009] }, |
| 607 | { .compatible = "ti,lp5012", .data = &lp50xx_chip_info_tbl[LP5012] }, |
| 608 | { .compatible = "ti,lp5018", .data = &lp50xx_chip_info_tbl[LP5018] }, |
| 609 | { .compatible = "ti,lp5024", .data = &lp50xx_chip_info_tbl[LP5024] }, |
| 610 | { .compatible = "ti,lp5030", .data = &lp50xx_chip_info_tbl[LP5030] }, |
| 611 | { .compatible = "ti,lp5036", .data = &lp50xx_chip_info_tbl[LP5036] }, |
| 612 | {} |
Dan Murphy | 242b811 | 2020-08-12 14:50:16 -0500 | [diff] [blame] | 613 | }; |
| 614 | MODULE_DEVICE_TABLE(of, of_lp50xx_leds_match); |
| 615 | |
| 616 | static struct i2c_driver lp50xx_driver = { |
| 617 | .driver = { |
| 618 | .name = "lp50xx", |
| 619 | .of_match_table = of_lp50xx_leds_match, |
| 620 | }, |
Andy Shevchenko | ea1ff99 | 2021-02-16 17:50:45 +0200 | [diff] [blame^] | 621 | .probe_new = lp50xx_probe, |
Dan Murphy | 242b811 | 2020-08-12 14:50:16 -0500 | [diff] [blame] | 622 | .remove = lp50xx_remove, |
| 623 | .id_table = lp50xx_id, |
| 624 | }; |
| 625 | module_i2c_driver(lp50xx_driver); |
| 626 | |
| 627 | MODULE_DESCRIPTION("Texas Instruments LP50XX LED driver"); |
| 628 | MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>"); |
| 629 | MODULE_LICENSE("GPL v2"); |