1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/*
* TI lm3692x LED Driver
*
* Copyright (C) 2017 Texas Instruments
*
* Author: Dan Murphy <dmurphy@ti.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* Data sheet is located
* http://www.ti.com/lit/ds/snvsa29/snvsa29.pdf
*/
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <uapi/linux/uleds.h>
#define LM3692X_REV 0x0
#define LM3692X_RESET 0x1
#define LM3692X_EN 0x10
#define LM3692X_BRT_CTRL 0x11
#define LM3692X_PWM_CTRL 0x12
#define LM3692X_BOOST_CTRL 0x13
#define LM3692X_AUTO_FREQ_HI 0x15
#define LM3692X_AUTO_FREQ_LO 0x16
#define LM3692X_BL_ADJ_THRESH 0x17
#define LM3692X_BRT_LSB 0x18
#define LM3692X_BRT_MSB 0x19
#define LM3692X_FAULT_CTRL 0x1e
#define LM3692X_FAULT_FLAGS 0x1f
#define LM3692X_SW_RESET BIT(0)
#define LM3692X_DEVICE_EN BIT(0)
#define LM3692X_LED1_EN BIT(1)
#define LM3692X_LED2_EN BIT(2)
/* Brightness Control Bits */
#define LM3692X_BL_ADJ_POL BIT(0)
#define LM3692X_RAMP_RATE_125us 0x00
#define LM3692X_RAMP_RATE_250us BIT(1)
#define LM3692X_RAMP_RATE_500us BIT(2)
#define LM3692X_RAMP_RATE_1ms (BIT(1) | BIT(2))
#define LM3692X_RAMP_RATE_2ms BIT(3)
#define LM3692X_RAMP_RATE_4ms (BIT(3) | BIT(1))
#define LM3692X_RAMP_RATE_8ms (BIT(2) | BIT(3))
#define LM3692X_RAMP_RATE_16ms (BIT(1) | BIT(2) | BIT(3))
#define LM3692X_RAMP_EN BIT(4)
#define LM3692X_BRHT_MODE_REG 0x00
#define LM3692X_BRHT_MODE_PWM BIT(5)
#define LM3692X_BRHT_MODE_MULTI_RAMP BIT(6)
#define LM3692X_BRHT_MODE_RAMP_MULTI (BIT(5) | BIT(6))
#define LM3692X_MAP_MODE_EXP BIT(7)
/* PWM Register Bits */
#define LM3692X_PWM_FILTER_100 BIT(0)
#define LM3692X_PWM_FILTER_150 BIT(1)
#define LM3692X_PWM_FILTER_200 (BIT(0) | BIT(1))
#define LM3692X_PWM_HYSTER_1LSB BIT(2)
#define LM3692X_PWM_HYSTER_2LSB BIT(3)
#define LM3692X_PWM_HYSTER_3LSB (BIT(3) | BIT(2))
#define LM3692X_PWM_HYSTER_4LSB BIT(4)
#define LM3692X_PWM_HYSTER_5LSB (BIT(4) | BIT(2))
#define LM3692X_PWM_HYSTER_6LSB (BIT(4) | BIT(3))
#define LM3692X_PWM_POLARITY BIT(5)
#define LM3692X_PWM_SAMP_4MHZ BIT(6)
#define LM3692X_PWM_SAMP_24MHZ BIT(7)
/* Boost Control Bits */
#define LM3692X_OCP_PROT_1A BIT(0)
#define LM3692X_OCP_PROT_1_25A BIT(1)
#define LM3692X_OCP_PROT_1_5A (BIT(0) | BIT(1))
#define LM3692X_OVP_21V BIT(2)
#define LM3692X_OVP_25V BIT(3)
#define LM3692X_OVP_29V (BIT(2) | BIT(3))
#define LM3692X_MIN_IND_22UH BIT(4)
#define LM3692X_BOOST_SW_1MHZ BIT(5)
#define LM3692X_BOOST_SW_NO_SHIFT BIT(6)
/* Fault Control Bits */
#define LM3692X_FAULT_CTRL_OVP BIT(0)
#define LM3692X_FAULT_CTRL_OCP BIT(1)
#define LM3692X_FAULT_CTRL_TSD BIT(2)
#define LM3692X_FAULT_CTRL_OPEN BIT(3)
/* Fault Flag Bits */
#define LM3692X_FAULT_FLAG_OVP BIT(0)
#define LM3692X_FAULT_FLAG_OCP BIT(1)
#define LM3692X_FAULT_FLAG_TSD BIT(2)
#define LM3692X_FAULT_FLAG_SHRT BIT(3)
#define LM3692X_FAULT_FLAG_OPEN BIT(4)
/**
* struct lm3692x_led -
* @lock - Lock for reading/writing the device
* @client - Pointer to the I2C client
* @led_dev - LED class device pointer
* @regmap - Devices register map
* @enable_gpio - VDDIO/EN gpio to enable communication interface
* @regulator - LED supply regulator pointer
* @label - LED label
*/
struct lm3692x_led {
struct mutex lock;
struct i2c_client *client;
struct led_classdev led_dev;
struct regmap *regmap;
struct gpio_desc *enable_gpio;
struct regulator *regulator;
char label[LED_MAX_NAME_SIZE];
};
static const struct reg_default lm3692x_reg_defs[] = {
{LM3692X_EN, 0xf},
{LM3692X_BRT_CTRL, 0x61},
{LM3692X_PWM_CTRL, 0x73},
{LM3692X_BOOST_CTRL, 0x6f},
{LM3692X_AUTO_FREQ_HI, 0x0},
{LM3692X_AUTO_FREQ_LO, 0x0},
{LM3692X_BL_ADJ_THRESH, 0x0},
{LM3692X_BRT_LSB, 0x7},
{LM3692X_BRT_MSB, 0xff},
{LM3692X_FAULT_CTRL, 0x7},
};
static const struct regmap_config lm3692x_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.max_register = LM3692X_FAULT_FLAGS,
.reg_defaults = lm3692x_reg_defs,
.num_reg_defaults = ARRAY_SIZE(lm3692x_reg_defs),
.cache_type = REGCACHE_RBTREE,
};
static int lm3692x_fault_check(struct lm3692x_led *led)
{
int ret;
unsigned int read_buf;
ret = regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf);
if (ret)
return ret;
if (read_buf)
dev_err(&led->client->dev, "Detected a fault 0x%X\n", read_buf);
/* The first read may clear the fault. Check again to see if the fault
* still exits and return that value.
*/
regmap_read(led->regmap, LM3692X_FAULT_FLAGS, &read_buf);
if (read_buf)
dev_err(&led->client->dev, "Second read of fault flags 0x%X\n",
read_buf);
return read_buf;
}
static int lm3692x_brightness_set(struct led_classdev *led_cdev,
enum led_brightness brt_val)
{
struct lm3692x_led *led =
container_of(led_cdev, struct lm3692x_led, led_dev);
int ret;
int led_brightness_lsb = (brt_val >> 5);
mutex_lock(&led->lock);
ret = lm3692x_fault_check(led);
if (ret) {
dev_err(&led->client->dev, "Cannot read/clear faults\n");
goto out;
}
ret = regmap_write(led->regmap, LM3692X_BRT_MSB, brt_val);
if (ret) {
dev_err(&led->client->dev, "Cannot write MSB\n");
goto out;
}
ret = regmap_write(led->regmap, LM3692X_BRT_LSB, led_brightness_lsb);
if (ret) {
dev_err(&led->client->dev, "Cannot write LSB\n");
goto out;
}
out:
mutex_unlock(&led->lock);
return ret;
}
static int lm3692x_init(struct lm3692x_led *led)
{
int ret;
if (led->regulator) {
ret = regulator_enable(led->regulator);
if (ret) {
dev_err(&led->client->dev,
"Failed to enable regulator\n");
return ret;
}
}
if (led->enable_gpio)
gpiod_direction_output(led->enable_gpio, 1);
ret = lm3692x_fault_check(led);
if (ret) {
dev_err(&led->client->dev, "Cannot read/clear faults\n");
goto out;
}
ret = regmap_write(led->regmap, LM3692X_BRT_CTRL, 0x00);
if (ret)
goto out;
/*
* For glitch free operation, the following data should
* only be written while device enable bit is 0
* per Section 7.5.14 of the data sheet
*/
ret = regmap_write(led->regmap, LM3692X_PWM_CTRL,
LM3692X_PWM_FILTER_100 | LM3692X_PWM_SAMP_24MHZ);
if (ret)
goto out;
ret = regmap_write(led->regmap, LM3692X_BOOST_CTRL,
LM3692X_BRHT_MODE_RAMP_MULTI |
LM3692X_BL_ADJ_POL |
LM3692X_RAMP_RATE_250us);
if (ret)
goto out;
ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_HI, 0x00);
if (ret)
goto out;
ret = regmap_write(led->regmap, LM3692X_AUTO_FREQ_LO, 0x00);
if (ret)
goto out;
ret = regmap_write(led->regmap, LM3692X_BL_ADJ_THRESH, 0x00);
if (ret)
goto out;
ret = regmap_write(led->regmap, LM3692X_BRT_CTRL,
LM3692X_BL_ADJ_POL | LM3692X_PWM_HYSTER_4LSB);
if (ret)
goto out;
return ret;
out:
dev_err(&led->client->dev, "Fail writing initialization values\n");
if (led->enable_gpio)
gpiod_direction_output(led->enable_gpio, 0);
if (led->regulator) {
ret = regulator_disable(led->regulator);
if (ret)
dev_err(&led->client->dev,
"Failed to disable regulator\n");
}
return ret;
}
static int lm3692x_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int ret;
struct lm3692x_led *led;
struct device_node *np = client->dev.of_node;
struct device_node *child_node;
const char *name;
led = devm_kzalloc(&client->dev, sizeof(*led), GFP_KERNEL);
if (!led)
return -ENOMEM;
for_each_available_child_of_node(np, child_node) {
led->led_dev.default_trigger = of_get_property(child_node,
"linux,default-trigger",
NULL);
ret = of_property_read_string(child_node, "label", &name);
if (!ret)
snprintf(led->label, sizeof(led->label),
"%s:%s", id->name, name);
else
snprintf(led->label, sizeof(led->label),
"%s::backlight_cluster", id->name);
};
led->enable_gpio = devm_gpiod_get_optional(&client->dev,
"enable", GPIOD_OUT_LOW);
if (IS_ERR(led->enable_gpio)) {
ret = PTR_ERR(led->enable_gpio);
dev_err(&client->dev, "Failed to get enable gpio: %d\n", ret);
return ret;
}
led->regulator = devm_regulator_get(&client->dev, "vled");
if (IS_ERR(led->regulator))
led->regulator = NULL;
led->client = client;
led->led_dev.name = led->label;
led->led_dev.brightness_set_blocking = lm3692x_brightness_set;
mutex_init(&led->lock);
i2c_set_clientdata(client, led);
led->regmap = devm_regmap_init_i2c(client, &lm3692x_regmap_config);
if (IS_ERR(led->regmap)) {
ret = PTR_ERR(led->regmap);
dev_err(&client->dev, "Failed to allocate register map: %d\n",
ret);
return ret;
}
ret = lm3692x_init(led);
if (ret)
return ret;
ret = devm_led_classdev_register(&client->dev, &led->led_dev);
if (ret) {
dev_err(&client->dev, "led register err: %d\n", ret);
return ret;
}
return 0;
}
static int lm3692x_remove(struct i2c_client *client)
{
struct lm3692x_led *led = i2c_get_clientdata(client);
int ret;
if (led->enable_gpio)
gpiod_direction_output(led->enable_gpio, 0);
if (led->regulator) {
ret = regulator_disable(led->regulator);
if (ret)
dev_err(&led->client->dev,
"Failed to disable regulator\n");
}
mutex_destroy(&led->lock);
return 0;
}
static const struct i2c_device_id lm3692x_id[] = {
{ "lm36922", 0 },
{ "lm36923", 1 },
{ }
};
MODULE_DEVICE_TABLE(i2c, lm3692x_id);
static const struct of_device_id of_lm3692x_leds_match[] = {
{ .compatible = "ti,lm36922", },
{ .compatible = "ti,lm36923", },
{},
};
MODULE_DEVICE_TABLE(of, of_lm3692x_leds_match);
static struct i2c_driver lm3692x_driver = {
.driver = {
.name = "lm3692x",
.of_match_table = of_lm3692x_leds_match,
},
.probe = lm3692x_probe,
.remove = lm3692x_remove,
.id_table = lm3692x_id,
};
module_i2c_driver(lm3692x_driver);
MODULE_DESCRIPTION("Texas Instruments LM3692X LED driver");
MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
MODULE_LICENSE("GPL v2");
|