Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2014 Belkin Inc. |
| 4 | * Copyright 2015 Andrew Lunn <andrew@lunn.ch> |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/i2c.h> |
| 8 | #include <linux/leds.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/of.h> |
| 11 | #include <linux/of_device.h> |
| 12 | #include <linux/regmap.h> |
| 13 | #include <linux/slab.h> |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 14 | |
| 15 | #define TLC591XX_MAX_LEDS 16 |
Jean-Jacques Hiblot | a2cafdf | 2019-09-23 12:02:50 +0200 | [diff] [blame] | 16 | #define TLC591XX_MAX_BRIGHTNESS 256 |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 17 | |
| 18 | #define TLC591XX_REG_MODE1 0x00 |
| 19 | #define MODE1_RESPON_ADDR_MASK 0xF0 |
| 20 | #define MODE1_NORMAL_MODE (0 << 4) |
| 21 | #define MODE1_SPEED_MODE (1 << 4) |
| 22 | |
| 23 | #define TLC591XX_REG_MODE2 0x01 |
| 24 | #define MODE2_DIM (0 << 5) |
| 25 | #define MODE2_BLINK (1 << 5) |
| 26 | #define MODE2_OCH_STOP (0 << 3) |
| 27 | #define MODE2_OCH_ACK (1 << 3) |
| 28 | |
| 29 | #define TLC591XX_REG_PWM(x) (0x02 + (x)) |
| 30 | |
| 31 | #define TLC591XX_REG_GRPPWM 0x12 |
| 32 | #define TLC591XX_REG_GRPFREQ 0x13 |
| 33 | |
| 34 | /* LED Driver Output State, determine the source that drives LED outputs */ |
| 35 | #define LEDOUT_OFF 0x0 /* Output LOW */ |
| 36 | #define LEDOUT_ON 0x1 /* Output HI-Z */ |
| 37 | #define LEDOUT_DIM 0x2 /* Dimming */ |
| 38 | #define LEDOUT_BLINK 0x3 /* Blinking */ |
| 39 | #define LEDOUT_MASK 0x3 |
| 40 | |
| 41 | #define ldev_to_led(c) container_of(c, struct tlc591xx_led, ldev) |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 42 | |
| 43 | struct tlc591xx_led { |
| 44 | bool active; |
| 45 | unsigned int led_no; |
| 46 | struct led_classdev ldev; |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 47 | struct tlc591xx_priv *priv; |
| 48 | }; |
| 49 | |
| 50 | struct tlc591xx_priv { |
| 51 | struct tlc591xx_led leds[TLC591XX_MAX_LEDS]; |
| 52 | struct regmap *regmap; |
| 53 | unsigned int reg_ledout_offset; |
| 54 | }; |
| 55 | |
| 56 | struct tlc591xx { |
| 57 | unsigned int max_leds; |
| 58 | unsigned int reg_ledout_offset; |
| 59 | }; |
| 60 | |
| 61 | static const struct tlc591xx tlc59116 = { |
| 62 | .max_leds = 16, |
| 63 | .reg_ledout_offset = 0x14, |
| 64 | }; |
| 65 | |
| 66 | static const struct tlc591xx tlc59108 = { |
| 67 | .max_leds = 8, |
| 68 | .reg_ledout_offset = 0x0c, |
| 69 | }; |
| 70 | |
| 71 | static int |
| 72 | tlc591xx_set_mode(struct regmap *regmap, u8 mode) |
| 73 | { |
| 74 | int err; |
| 75 | u8 val; |
| 76 | |
| 77 | err = regmap_write(regmap, TLC591XX_REG_MODE1, MODE1_NORMAL_MODE); |
| 78 | if (err) |
| 79 | return err; |
| 80 | |
| 81 | val = MODE2_OCH_STOP | mode; |
| 82 | |
| 83 | return regmap_write(regmap, TLC591XX_REG_MODE2, val); |
| 84 | } |
| 85 | |
| 86 | static int |
| 87 | tlc591xx_set_ledout(struct tlc591xx_priv *priv, struct tlc591xx_led *led, |
| 88 | u8 val) |
| 89 | { |
| 90 | unsigned int i = (led->led_no % 4) * 2; |
| 91 | unsigned int mask = LEDOUT_MASK << i; |
| 92 | unsigned int addr = priv->reg_ledout_offset + (led->led_no >> 2); |
| 93 | |
| 94 | val = val << i; |
| 95 | |
| 96 | return regmap_update_bits(priv->regmap, addr, mask, val); |
| 97 | } |
| 98 | |
| 99 | static int |
| 100 | tlc591xx_set_pwm(struct tlc591xx_priv *priv, struct tlc591xx_led *led, |
| 101 | u8 brightness) |
| 102 | { |
| 103 | u8 pwm = TLC591XX_REG_PWM(led->led_no); |
| 104 | |
| 105 | return regmap_write(priv->regmap, pwm, brightness); |
| 106 | } |
| 107 | |
Andrew Lunn | d890389 | 2015-08-20 11:41:01 +0200 | [diff] [blame] | 108 | static int |
| 109 | tlc591xx_brightness_set(struct led_classdev *led_cdev, |
| 110 | enum led_brightness brightness) |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 111 | { |
Andrew Lunn | d890389 | 2015-08-20 11:41:01 +0200 | [diff] [blame] | 112 | struct tlc591xx_led *led = ldev_to_led(led_cdev); |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 113 | struct tlc591xx_priv *priv = led->priv; |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 114 | int err; |
| 115 | |
Jean-Jacques Hiblot | a2cafdf | 2019-09-23 12:02:50 +0200 | [diff] [blame] | 116 | switch ((int)brightness) { |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 117 | case 0: |
| 118 | err = tlc591xx_set_ledout(priv, led, LEDOUT_OFF); |
| 119 | break; |
Jean-Jacques Hiblot | a2cafdf | 2019-09-23 12:02:50 +0200 | [diff] [blame] | 120 | case TLC591XX_MAX_BRIGHTNESS: |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 121 | err = tlc591xx_set_ledout(priv, led, LEDOUT_ON); |
| 122 | break; |
| 123 | default: |
| 124 | err = tlc591xx_set_ledout(priv, led, LEDOUT_DIM); |
| 125 | if (!err) |
| 126 | err = tlc591xx_set_pwm(priv, led, brightness); |
| 127 | } |
| 128 | |
Andrew Lunn | d890389 | 2015-08-20 11:41:01 +0200 | [diff] [blame] | 129 | return err; |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 132 | static const struct regmap_config tlc591xx_regmap = { |
| 133 | .reg_bits = 8, |
| 134 | .val_bits = 8, |
| 135 | .max_register = 0x1e, |
| 136 | }; |
| 137 | |
| 138 | static const struct of_device_id of_tlc591xx_leds_match[] = { |
| 139 | { .compatible = "ti,tlc59116", |
| 140 | .data = &tlc59116 }, |
| 141 | { .compatible = "ti,tlc59108", |
| 142 | .data = &tlc59108 }, |
| 143 | {}, |
| 144 | }; |
| 145 | MODULE_DEVICE_TABLE(of, of_tlc591xx_leds_match); |
| 146 | |
| 147 | static int |
| 148 | tlc591xx_probe(struct i2c_client *client, |
| 149 | const struct i2c_device_id *id) |
| 150 | { |
Yang Yingliang | ee522bc | 2021-04-06 21:11:59 +0800 | [diff] [blame] | 151 | struct device_node *np, *child; |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 152 | struct device *dev = &client->dev; |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 153 | const struct tlc591xx *tlc591xx; |
| 154 | struct tlc591xx_priv *priv; |
| 155 | int err, count, reg; |
| 156 | |
Yang Yingliang | ee522bc | 2021-04-06 21:11:59 +0800 | [diff] [blame] | 157 | np = dev_of_node(dev); |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 158 | if (!np) |
| 159 | return -ENODEV; |
| 160 | |
Yang Yingliang | ee522bc | 2021-04-06 21:11:59 +0800 | [diff] [blame] | 161 | tlc591xx = device_get_match_data(dev); |
| 162 | if (!tlc591xx) |
| 163 | return -ENODEV; |
| 164 | |
Marek BehĂșn | 99a013c | 2020-09-18 00:32:56 +0200 | [diff] [blame] | 165 | count = of_get_available_child_count(np); |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 166 | if (!count || count > tlc591xx->max_leds) |
| 167 | return -EINVAL; |
| 168 | |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 169 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 170 | if (!priv) |
| 171 | return -ENOMEM; |
| 172 | |
| 173 | priv->regmap = devm_regmap_init_i2c(client, &tlc591xx_regmap); |
| 174 | if (IS_ERR(priv->regmap)) { |
| 175 | err = PTR_ERR(priv->regmap); |
| 176 | dev_err(dev, "Failed to allocate register map: %d\n", err); |
| 177 | return err; |
| 178 | } |
| 179 | priv->reg_ledout_offset = tlc591xx->reg_ledout_offset; |
| 180 | |
| 181 | i2c_set_clientdata(client, priv); |
| 182 | |
Jean-Jacques Hiblot | 1ab4531 | 2019-09-20 13:58:05 +0200 | [diff] [blame] | 183 | err = tlc591xx_set_mode(priv->regmap, MODE2_DIM); |
| 184 | if (err < 0) |
| 185 | return err; |
| 186 | |
Marek BehĂșn | 99a013c | 2020-09-18 00:32:56 +0200 | [diff] [blame] | 187 | for_each_available_child_of_node(np, child) { |
Jean-Jacques Hiblot | 1ab4531 | 2019-09-20 13:58:05 +0200 | [diff] [blame] | 188 | struct tlc591xx_led *led; |
Jean-Jacques Hiblot | 5b4b723 | 2019-09-20 13:58:06 +0200 | [diff] [blame] | 189 | struct led_init_data init_data = {}; |
| 190 | |
| 191 | init_data.fwnode = of_fwnode_handle(child); |
Jean-Jacques Hiblot | 1ab4531 | 2019-09-20 13:58:05 +0200 | [diff] [blame] | 192 | |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 193 | err = of_property_read_u32(child, "reg", ®); |
Julia Lawall | c687291 | 2017-07-15 11:58:19 +0200 | [diff] [blame] | 194 | if (err) { |
| 195 | of_node_put(child); |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 196 | return err; |
Julia Lawall | c687291 | 2017-07-15 11:58:19 +0200 | [diff] [blame] | 197 | } |
Julia Lawall | 1055790 | 2017-07-15 11:58:18 +0200 | [diff] [blame] | 198 | if (reg < 0 || reg >= tlc591xx->max_leds || |
Julia Lawall | c687291 | 2017-07-15 11:58:19 +0200 | [diff] [blame] | 199 | priv->leds[reg].active) { |
| 200 | of_node_put(child); |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 201 | return -EINVAL; |
Julia Lawall | c687291 | 2017-07-15 11:58:19 +0200 | [diff] [blame] | 202 | } |
Jean-Jacques Hiblot | 1ab4531 | 2019-09-20 13:58:05 +0200 | [diff] [blame] | 203 | led = &priv->leds[reg]; |
| 204 | |
| 205 | led->active = true; |
Jean-Jacques Hiblot | 1ab4531 | 2019-09-20 13:58:05 +0200 | [diff] [blame] | 206 | led->priv = priv; |
| 207 | led->led_no = reg; |
| 208 | led->ldev.brightness_set_blocking = tlc591xx_brightness_set; |
Jean-Jacques Hiblot | a2cafdf | 2019-09-23 12:02:50 +0200 | [diff] [blame] | 209 | led->ldev.max_brightness = TLC591XX_MAX_BRIGHTNESS; |
Jean-Jacques Hiblot | 5b4b723 | 2019-09-20 13:58:06 +0200 | [diff] [blame] | 210 | err = devm_led_classdev_register_ext(dev, &led->ldev, |
| 211 | &init_data); |
Tobias Jordan | 108f466 | 2020-09-26 02:51:17 +0200 | [diff] [blame] | 212 | if (err < 0) { |
| 213 | of_node_put(child); |
Krzysztof Kozlowski | 03eb2ca | 2020-08-26 16:50:13 +0200 | [diff] [blame] | 214 | return dev_err_probe(dev, err, |
| 215 | "couldn't register LED %s\n", |
| 216 | led->ldev.name); |
Tobias Jordan | 108f466 | 2020-09-26 02:51:17 +0200 | [diff] [blame] | 217 | } |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 218 | } |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static const struct i2c_device_id tlc591xx_id[] = { |
| 223 | { "tlc59116" }, |
| 224 | { "tlc59108" }, |
| 225 | {}, |
| 226 | }; |
| 227 | MODULE_DEVICE_TABLE(i2c, tlc591xx_id); |
| 228 | |
| 229 | static struct i2c_driver tlc591xx_driver = { |
| 230 | .driver = { |
| 231 | .name = "tlc591xx", |
| 232 | .of_match_table = of_match_ptr(of_tlc591xx_leds_match), |
| 233 | }, |
| 234 | .probe = tlc591xx_probe, |
Andrew Lunn | e370d01 | 2015-03-17 15:08:27 -0700 | [diff] [blame] | 235 | .id_table = tlc591xx_id, |
| 236 | }; |
| 237 | |
| 238 | module_i2c_driver(tlc591xx_driver); |
| 239 | |
| 240 | MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>"); |
| 241 | MODULE_LICENSE("GPL"); |
| 242 | MODULE_DESCRIPTION("TLC591XX LED driver"); |