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