]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/leds/leds-is31fl319x.c
leds: is31fl319x: Add shutdown pin and generate a 5ms low pulse when startup
[mirror_ubuntu-jammy-kernel.git] / drivers / leds / leds-is31fl319x.c
CommitLineData
36edc939 1// SPDX-License-Identifier: GPL-2.0-only
8c40b7d0
NS
2/*
3 * Copyright 2015-16 Golden Delicious Computers
4 *
5 * Author: Nikolaus Schaller <hns@goldelico.com>
6 *
8c40b7d0
NS
7 * LED driver for the IS31FL319{0,1,3,6,9} to drive 1, 3, 6 or 9 light
8 * effect LEDs.
8c40b7d0
NS
9 */
10
11#include <linux/err.h>
12#include <linux/i2c.h>
13#include <linux/leds.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/regmap.h>
18#include <linux/slab.h>
dddb4e38
GF
19#include <linux/delay.h>
20#include <linux/gpio/consumer.h>
8c40b7d0
NS
21
22/* register numbers */
23#define IS31FL319X_SHUTDOWN 0x00
24#define IS31FL319X_CTRL1 0x01
25#define IS31FL319X_CTRL2 0x02
26#define IS31FL319X_CONFIG1 0x03
27#define IS31FL319X_CONFIG2 0x04
28#define IS31FL319X_RAMP_MODE 0x05
29#define IS31FL319X_BREATH_MASK 0x06
30#define IS31FL319X_PWM(channel) (0x07 + channel)
31#define IS31FL319X_DATA_UPDATE 0x10
32#define IS31FL319X_T0(channel) (0x11 + channel)
33#define IS31FL319X_T123_1 0x1a
34#define IS31FL319X_T123_2 0x1b
35#define IS31FL319X_T123_3 0x1c
36#define IS31FL319X_T4(channel) (0x1d + channel)
37#define IS31FL319X_TIME_UPDATE 0x26
38#define IS31FL319X_RESET 0xff
39
40#define IS31FL319X_REG_CNT (IS31FL319X_RESET + 1)
41
42#define IS31FL319X_MAX_LEDS 9
43
44/* CS (Current Setting) in CONFIG2 register */
45#define IS31FL319X_CONFIG2_CS_SHIFT 4
46#define IS31FL319X_CONFIG2_CS_MASK 0x7
47#define IS31FL319X_CONFIG2_CS_STEP_REF 12
48
49#define IS31FL319X_CURRENT_MIN ((u32)5000)
50#define IS31FL319X_CURRENT_MAX ((u32)40000)
51#define IS31FL319X_CURRENT_STEP ((u32)5000)
52#define IS31FL319X_CURRENT_DEFAULT ((u32)20000)
53
54/* Audio gain in CONFIG2 register */
55#define IS31FL319X_AUDIO_GAIN_DB_MAX ((u32)21)
56#define IS31FL319X_AUDIO_GAIN_DB_STEP ((u32)3)
57
58/*
59 * regmap is used as a cache of chip's register space,
60 * to avoid reading back brightness values from chip,
61 * which is known to hang.
62 */
63struct is31fl319x_chip {
64 const struct is31fl319x_chipdef *cdef;
65 struct i2c_client *client;
dddb4e38 66 struct gpio_desc *shutdown_gpio;
8c40b7d0
NS
67 struct regmap *regmap;
68 struct mutex lock;
69 u32 audio_gain_db;
70
71 struct is31fl319x_led {
72 struct is31fl319x_chip *chip;
73 struct led_classdev cdev;
74 u32 max_microamp;
75 bool configured;
76 } leds[IS31FL319X_MAX_LEDS];
77};
78
79struct is31fl319x_chipdef {
80 int num_leds;
81};
82
83static const struct is31fl319x_chipdef is31fl3190_cdef = {
84 .num_leds = 1,
85};
86
87static const struct is31fl319x_chipdef is31fl3193_cdef = {
88 .num_leds = 3,
89};
90
91static const struct is31fl319x_chipdef is31fl3196_cdef = {
92 .num_leds = 6,
93};
94
95static const struct is31fl319x_chipdef is31fl3199_cdef = {
96 .num_leds = 9,
97};
98
99static const struct of_device_id of_is31fl319x_match[] = {
100 { .compatible = "issi,is31fl3190", .data = &is31fl3190_cdef, },
101 { .compatible = "issi,is31fl3191", .data = &is31fl3190_cdef, },
102 { .compatible = "issi,is31fl3193", .data = &is31fl3193_cdef, },
103 { .compatible = "issi,is31fl3196", .data = &is31fl3196_cdef, },
104 { .compatible = "issi,is31fl3199", .data = &is31fl3199_cdef, },
105 { .compatible = "si-en,sn3199", .data = &is31fl3199_cdef, },
106 { }
107};
108MODULE_DEVICE_TABLE(of, of_is31fl319x_match);
109
110static int is31fl319x_brightness_set(struct led_classdev *cdev,
111 enum led_brightness brightness)
112{
113 struct is31fl319x_led *led = container_of(cdev, struct is31fl319x_led,
114 cdev);
115 struct is31fl319x_chip *is31 = led->chip;
116 int chan = led - is31->leds;
117 int ret;
118 int i;
119 u8 ctrl1 = 0, ctrl2 = 0;
120
121 dev_dbg(&is31->client->dev, "%s %d: %d\n", __func__, chan, brightness);
122
123 mutex_lock(&is31->lock);
124
125 /* update PWM register */
126 ret = regmap_write(is31->regmap, IS31FL319X_PWM(chan), brightness);
127 if (ret < 0)
128 goto out;
129
130 /* read current brightness of all PWM channels */
131 for (i = 0; i < is31->cdef->num_leds; i++) {
132 unsigned int pwm_value;
133 bool on;
134
135 /*
136 * since neither cdev nor the chip can provide
137 * the current setting, we read from the regmap cache
138 */
139
140 ret = regmap_read(is31->regmap, IS31FL319X_PWM(i), &pwm_value);
141 dev_dbg(&is31->client->dev, "%s read %d: ret=%d: %d\n",
142 __func__, i, ret, pwm_value);
143 on = ret >= 0 && pwm_value > LED_OFF;
144
145 if (i < 3)
146 ctrl1 |= on << i; /* 0..2 => bit 0..2 */
147 else if (i < 6)
148 ctrl1 |= on << (i + 1); /* 3..5 => bit 4..6 */
149 else
150 ctrl2 |= on << (i - 6); /* 6..8 => bit 0..2 */
151 }
152
153 if (ctrl1 > 0 || ctrl2 > 0) {
154 dev_dbg(&is31->client->dev, "power up %02x %02x\n",
155 ctrl1, ctrl2);
156 regmap_write(is31->regmap, IS31FL319X_CTRL1, ctrl1);
157 regmap_write(is31->regmap, IS31FL319X_CTRL2, ctrl2);
158 /* update PWMs */
159 regmap_write(is31->regmap, IS31FL319X_DATA_UPDATE, 0x00);
160 /* enable chip from shut down */
161 ret = regmap_write(is31->regmap, IS31FL319X_SHUTDOWN, 0x01);
162 } else {
163 dev_dbg(&is31->client->dev, "power down\n");
164 /* shut down (no need to clear CTRL1/2) */
165 ret = regmap_write(is31->regmap, IS31FL319X_SHUTDOWN, 0x00);
166 }
167
168out:
169 mutex_unlock(&is31->lock);
170
171 return ret;
172}
173
174static int is31fl319x_parse_child_dt(const struct device *dev,
175 const struct device_node *child,
176 struct is31fl319x_led *led)
177{
178 struct led_classdev *cdev = &led->cdev;
179 int ret;
180
181 if (of_property_read_string(child, "label", &cdev->name))
182 cdev->name = child->name;
183
184 ret = of_property_read_string(child, "linux,default-trigger",
185 &cdev->default_trigger);
186 if (ret < 0 && ret != -EINVAL) /* is optional */
187 return ret;
188
189 led->max_microamp = IS31FL319X_CURRENT_DEFAULT;
190 ret = of_property_read_u32(child, "led-max-microamp",
191 &led->max_microamp);
192 if (!ret) {
193 if (led->max_microamp < IS31FL319X_CURRENT_MIN)
194 return -EINVAL; /* not supported */
195 led->max_microamp = min(led->max_microamp,
196 IS31FL319X_CURRENT_MAX);
197 }
198
199 return 0;
200}
201
202static int is31fl319x_parse_dt(struct device *dev,
203 struct is31fl319x_chip *is31)
204{
205 struct device_node *np = dev->of_node, *child;
206 const struct of_device_id *of_dev_id;
207 int count;
208 int ret;
209
210 if (!np)
211 return -ENODEV;
212
dddb4e38
GF
213 is31->shutdown_gpio = devm_gpiod_get_optional(dev,
214 "shutdown",
215 GPIOD_OUT_HIGH);
216 if (IS_ERR(is31->shutdown_gpio)) {
217 ret = PTR_ERR(is31->shutdown_gpio);
218 dev_err(dev, "Failed to get shutdown gpio: %d\n", ret);
219 return ret;
220 }
221
8c40b7d0
NS
222 of_dev_id = of_match_device(of_is31fl319x_match, dev);
223 if (!of_dev_id) {
224 dev_err(dev, "Failed to match device with supported chips\n");
225 return -EINVAL;
226 }
227
228 is31->cdef = of_dev_id->data;
229
230 count = of_get_child_count(np);
231
232 dev_dbg(dev, "probe %s with %d leds defined in DT\n",
233 of_dev_id->compatible, count);
234
235 if (!count || count > is31->cdef->num_leds) {
236 dev_err(dev, "Number of leds defined must be between 1 and %u\n",
237 is31->cdef->num_leds);
238 return -ENODEV;
239 }
240
241 for_each_child_of_node(np, child) {
242 struct is31fl319x_led *led;
243 u32 reg;
244
245 ret = of_property_read_u32(child, "reg", &reg);
246 if (ret) {
247 dev_err(dev, "Failed to read led 'reg' property\n");
248 goto put_child_node;
249 }
250
251 if (reg < 1 || reg > is31->cdef->num_leds) {
252 dev_err(dev, "invalid led reg %u\n", reg);
253 ret = -EINVAL;
254 goto put_child_node;
255 }
256
257 led = &is31->leds[reg - 1];
258
259 if (led->configured) {
260 dev_err(dev, "led %u is already configured\n", reg);
261 ret = -EINVAL;
262 goto put_child_node;
263 }
264
265 ret = is31fl319x_parse_child_dt(dev, child, led);
266 if (ret) {
267 dev_err(dev, "led %u DT parsing failed\n", reg);
268 goto put_child_node;
269 }
270
271 led->configured = true;
272 }
273
274 is31->audio_gain_db = 0;
275 ret = of_property_read_u32(np, "audio-gain-db", &is31->audio_gain_db);
276 if (!ret)
277 is31->audio_gain_db = min(is31->audio_gain_db,
278 IS31FL319X_AUDIO_GAIN_DB_MAX);
279
280 return 0;
281
282put_child_node:
283 of_node_put(child);
284 return ret;
285}
286
287static bool is31fl319x_readable_reg(struct device *dev, unsigned int reg)
288{ /* we have no readable registers */
289 return false;
290}
291
292static bool is31fl319x_volatile_reg(struct device *dev, unsigned int reg)
293{ /* volatile registers are not cached */
294 switch (reg) {
295 case IS31FL319X_DATA_UPDATE:
296 case IS31FL319X_TIME_UPDATE:
297 case IS31FL319X_RESET:
298 return true; /* always write-through */
299 default:
300 return false;
301 }
302}
303
304static const struct reg_default is31fl319x_reg_defaults[] = {
305 { IS31FL319X_CONFIG1, 0x00},
306 { IS31FL319X_CONFIG2, 0x00},
307 { IS31FL319X_PWM(0), 0x00},
308 { IS31FL319X_PWM(1), 0x00},
309 { IS31FL319X_PWM(2), 0x00},
310 { IS31FL319X_PWM(3), 0x00},
311 { IS31FL319X_PWM(4), 0x00},
312 { IS31FL319X_PWM(5), 0x00},
313 { IS31FL319X_PWM(6), 0x00},
314 { IS31FL319X_PWM(7), 0x00},
315 { IS31FL319X_PWM(8), 0x00},
316};
317
318static struct regmap_config regmap_config = {
319 .reg_bits = 8,
320 .val_bits = 8,
321 .max_register = IS31FL319X_REG_CNT,
322 .cache_type = REGCACHE_FLAT,
323 .readable_reg = is31fl319x_readable_reg,
324 .volatile_reg = is31fl319x_volatile_reg,
325 .reg_defaults = is31fl319x_reg_defaults,
326 .num_reg_defaults = ARRAY_SIZE(is31fl319x_reg_defaults),
327};
328
329static inline int is31fl319x_microamp_to_cs(struct device *dev, u32 microamp)
330{ /* round down to nearest supported value (range check done by caller) */
331 u32 step = microamp / IS31FL319X_CURRENT_STEP;
332
333 return ((IS31FL319X_CONFIG2_CS_STEP_REF - step) &
334 IS31FL319X_CONFIG2_CS_MASK) <<
335 IS31FL319X_CONFIG2_CS_SHIFT; /* CS encoding */
336}
337
338static inline int is31fl319x_db_to_gain(u32 dezibel)
339{ /* round down to nearest supported value (range check done by caller) */
340 return dezibel / IS31FL319X_AUDIO_GAIN_DB_STEP;
341}
342
343static int is31fl319x_probe(struct i2c_client *client,
344 const struct i2c_device_id *id)
345{
346 struct is31fl319x_chip *is31;
347 struct device *dev = &client->dev;
8c40b7d0
NS
348 int err;
349 int i = 0;
350 u32 aggregated_led_microamp = IS31FL319X_CURRENT_MAX;
351
7d9d60bd 352 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
8c40b7d0
NS
353 return -EIO;
354
355 is31 = devm_kzalloc(&client->dev, sizeof(*is31), GFP_KERNEL);
356 if (!is31)
357 return -ENOMEM;
358
359 mutex_init(&is31->lock);
360
361 err = is31fl319x_parse_dt(&client->dev, is31);
362 if (err)
363 goto free_mutex;
364
dddb4e38
GF
365 if (is31->shutdown_gpio) {
366 gpiod_direction_output(is31->shutdown_gpio, 0);
367 mdelay(5);
368 gpiod_direction_output(is31->shutdown_gpio, 1);
369 }
370
8c40b7d0
NS
371 is31->client = client;
372 is31->regmap = devm_regmap_init_i2c(client, &regmap_config);
373 if (IS_ERR(is31->regmap)) {
374 dev_err(&client->dev, "failed to allocate register map\n");
375 err = PTR_ERR(is31->regmap);
376 goto free_mutex;
377 }
378
379 i2c_set_clientdata(client, is31);
380
381 /* check for write-reply from chip (we can't read any registers) */
382 err = regmap_write(is31->regmap, IS31FL319X_RESET, 0x00);
383 if (err < 0) {
384 dev_err(&client->dev, "no response from chip write: err = %d\n",
385 err);
386 err = -EIO; /* does not answer */
387 goto free_mutex;
388 }
389
390 /*
391 * Kernel conventions require per-LED led-max-microamp property.
392 * But the chip does not allow to limit individual LEDs.
393 * So we take minimum from all subnodes for safety of hardware.
394 */
395 for (i = 0; i < is31->cdef->num_leds; i++)
396 if (is31->leds[i].configured &&
397 is31->leds[i].max_microamp < aggregated_led_microamp)
398 aggregated_led_microamp = is31->leds[i].max_microamp;
399
400 regmap_write(is31->regmap, IS31FL319X_CONFIG2,
401 is31fl319x_microamp_to_cs(dev, aggregated_led_microamp) |
402 is31fl319x_db_to_gain(is31->audio_gain_db));
403
404 for (i = 0; i < is31->cdef->num_leds; i++) {
405 struct is31fl319x_led *led = &is31->leds[i];
406
407 if (!led->configured)
408 continue;
409
410 led->chip = is31;
411 led->cdev.brightness_set_blocking = is31fl319x_brightness_set;
412
413 err = devm_led_classdev_register(&client->dev, &led->cdev);
414 if (err < 0)
415 goto free_mutex;
416 }
417
418 return 0;
419
420free_mutex:
421 mutex_destroy(&is31->lock);
422 return err;
423}
424
425static int is31fl319x_remove(struct i2c_client *client)
426{
427 struct is31fl319x_chip *is31 = i2c_get_clientdata(client);
428
429 mutex_destroy(&is31->lock);
430 return 0;
431}
432
433/*
434 * i2c-core (and modalias) requires that id_table be properly filled,
435 * even though it is not used for DeviceTree based instantiation.
436 */
437static const struct i2c_device_id is31fl319x_id[] = {
438 { "is31fl3190" },
439 { "is31fl3191" },
440 { "is31fl3193" },
441 { "is31fl3196" },
442 { "is31fl3199" },
443 { "sn3199" },
444 {},
445};
446MODULE_DEVICE_TABLE(i2c, is31fl319x_id);
447
448static struct i2c_driver is31fl319x_driver = {
449 .driver = {
450 .name = "leds-is31fl319x",
451 .of_match_table = of_match_ptr(of_is31fl319x_match),
452 },
453 .probe = is31fl319x_probe,
454 .remove = is31fl319x_remove,
455 .id_table = is31fl319x_id,
456};
457
458module_i2c_driver(is31fl319x_driver);
459
460MODULE_AUTHOR("H. Nikolaus Schaller <hns@goldelico.com>");
461MODULE_AUTHOR("Andrey Utkin <andrey_utkin@fastmail.com>");
462MODULE_DESCRIPTION("IS31FL319X LED driver");
463MODULE_LICENSE("GPL v2");