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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
From: Profpatsch <mail@profpatsch.de>
Subject: [PATCH] backlight: Netronix LM3630A frontlight with ntxfw calibration

The tolino vision 6's frontlight is an LM3630A with a warm (bank A) and
a cold (bank B) LED string. Its vendor kernel does not expose the chip's
banks to userspace, but a brightness of 0..100 percent
(/sys/class/backlight/mxc_msp430.0) and a colour temperature
(/sys/class/backlight/lm3630a_led/color), mapped onto both banks'
brightness and current through a calibration table that the bootloader
keeps per device in its "ntxfw" area. KOReader's Kobo backend drives
exactly that interface.

ntx_lm3630a_bl.c does the same: it copies the table out of the ntxfw
area (a reserved-memory region, "memory-region"), registers both
backlight devices under the vendor's names, and programs the chip as the
vendor's lm3630a_bl.c does on this board (chip init on power up, the
enable GPIO off when both banks are off). Mainline's lm3630a_bl, which
exposes the banks with fixed full-scale current, stays as it is.
---
--- a/drivers/video/backlight/Kconfig
+++ b/drivers/video/backlight/Kconfig
@@ -384,6 +384,16 @@
 	help
 	  This supports TI LM3509 Backlight Driver
 
+config BACKLIGHT_NTX_LM3630A
+	tristate "Netronix frontlight on an LM3630A (ntxfw calibration)"
+	depends on I2C && OF
+	select REGMAP_I2C
+	help
+	  The frontlight of Netronix e-readers (tolino vision 6) as their
+	  vendor kernel drives it: a percentage and a colour temperature,
+	  mapped onto the LM3630A's banks through the calibration table in
+	  the bootloader's ntxfw area.
+
 config BACKLIGHT_LM3630A
 	tristate "Backlight Driver for LM3630A"
 	depends on I2C && PWM
--- a/drivers/video/backlight/Makefile
+++ b/drivers/video/backlight/Makefile
@@ -40,6 +40,7 @@
 obj-$(CONFIG_BACKLIGHT_LM3509)		+= lm3509_bl.o
 obj-$(CONFIG_BACKLIGHT_LM3533)		+= lm3533_bl.o
 obj-$(CONFIG_BACKLIGHT_LM3630A)		+= lm3630a_bl.o
+obj-$(CONFIG_BACKLIGHT_NTX_LM3630A)	+= ntx_lm3630a_bl.o
 obj-$(CONFIG_BACKLIGHT_LM3639)		+= lm3639_bl.o
 obj-$(CONFIG_BACKLIGHT_LOCOMO)		+= locomolcd.o
 obj-$(CONFIG_BACKLIGHT_LP855X)		+= lp855x_bl.o
--- /dev/null
+++ b/drivers/video/backlight/ntx_lm3630a_bl.c
@@ -0,0 +1,359 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Netronix frontlight on a TI LM3630A, as the vendor kernel of the tolino
+ * vision 6 drives it (drivers/video/backlight/ntx_bl.c and lm3630a_bl.c
+ * there): one brightness from 0 to 100 percent and a colour temperature,
+ * mapped onto the chip's two banks through the calibration table the
+ * bootloader keeps in its "ntxfw" area (ntxfw_p= on the vendor's command
+ * line), per device.
+ *
+ * Bank A is the warm LED string, bank B the cold one. For each colour
+ * temperature and percentage the table gives both banks' brightness and
+ * current; a current of 0 means the colour's default, and that 0 means the
+ * table header's (on the tolino: A 3, B 2).
+ *
+ * The sysfs interface is the vendor's, which KOReader's Kobo backend uses:
+ *   /sys/class/backlight/mxc_msp430.0/brightness   0..100 percent
+ *   /sys/class/backlight/lm3630a_led/color         0..max_color
+ *   /sys/class/backlight/lm3630a_led/brightness    0..100, the same
+ */
+
+#include <linux/backlight.h>
+#include <linux/delay.h>
+#include <linux/gpio/consumer.h>
+#include <linux/i2c.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/regmap.h>
+#include <linux/unaligned.h>
+
+#define REG_CTRL		0x00
+#define REG_CONFIG		0x01
+#define REG_BOOST		0x02
+#define REG_BRT_A		0x03
+#define REG_BRT_B		0x04
+#define REG_I_A			0x05
+#define REG_I_B			0x06
+#define REG_ONOFF_RAMP		0x07
+#define REG_RUN_RAMP		0x08
+#define REG_REV			0x1f
+#define REG_FILTER_STRENGTH	0x50
+
+#define CTRL_SLEEP		BIT(7)
+#define CTRL_LEDA_EN		BIT(2)
+#define CTRL_LEDB_EN		BIT(1)
+
+/* The bootloader's ntxfw blob (the vendor's ntx_firmware.h). */
+#define NTXFW_SIG_S		"{NTXFW-->"
+#define NTXFW_HDR_SIZE		52	/* signature, name, item count, version */
+#define NTXFW_ITEM_HDR_SIZE	56	/* magic, 0x12345678, type, size, name */
+#define NTXFW_TYPE_DUALFL_HDR	6
+#define NTXFW_TYPE_DUALFL_TAB	7
+
+struct ntx_fl_colour {			/* NTX_FW_LM3630FL_dualcolor_percent_tab */
+	char name[32];
+	u8 def_cur_a, def_cur_b, def_freq, def_flags;
+	u8 brt_a[100], cur_a[100], brt_b[100], cur_b[100];
+	__le32 total_current[100];
+	u8 reserved[8];
+} __packed;
+
+struct ntx_fl {
+	struct i2c_client *client;
+	struct regmap *regmap;
+	struct gpio_desc *enable;
+	struct mutex lock;
+	struct backlight_device *bl, *bl_led;
+	const struct ntx_fl_colour *colours;	/* copied out of ntxfw */
+	unsigned int ncolours;
+	u8 hdr_cur_a, hdr_cur_b;
+	unsigned int percent, colour;
+	bool powered;
+};
+
+static int ntx_fl_chip_init(struct ntx_fl *fl)
+{
+	/* As the vendor's lm3630a_chip_init() on this board. */
+	usleep_range(1000, 2000);
+	return regmap_write(fl->regmap, REG_FILTER_STRENGTH, 0x03) ?:
+	       regmap_update_bits(fl->regmap, REG_CONFIG, 0x07, 0x00) ?:
+	       regmap_write(fl->regmap, REG_ONOFF_RAMP, 0x00) ?:
+	       regmap_write(fl->regmap, REG_RUN_RAMP, 0x00) ?:
+	       regmap_write(fl->regmap, REG_BOOST, 0x63) ?:
+	       regmap_update_bits(fl->regmap, REG_I_A, 0x1f, 0) ?:
+	       regmap_write(fl->regmap, REG_I_B, 0) ?:
+	       regmap_update_bits(fl->regmap, REG_CTRL, 0x14, CTRL_LEDA_EN) ?:
+	       regmap_update_bits(fl->regmap, REG_CTRL, 0x0b, CTRL_LEDB_EN) ?:
+	       regmap_write(fl->regmap, REG_BRT_A, 0) ?:
+	       regmap_write(fl->regmap, REG_BRT_B, 0);
+}
+
+/* The vendor's _lm3630a_set_FL(). */
+static int ntx_fl_set(struct ntx_fl *fl, u8 cur_a, u8 brt_a, u8 cur_b, u8 brt_b)
+{
+	unsigned int en = 0;
+	int ret;
+
+	if (!cur_a && !cur_b) {
+		if (fl->powered) {
+			gpiod_set_value_cansleep(fl->enable, 0);
+			fl->powered = false;
+		}
+		return 0;
+	}
+	if (!fl->powered) {
+		gpiod_set_value_cansleep(fl->enable, 1);
+		fl->powered = true;
+		msleep(50);
+		ret = ntx_fl_chip_init(fl);
+		if (ret)
+			return ret;
+	}
+
+	ret = regmap_update_bits(fl->regmap, REG_CTRL, CTRL_SLEEP, 0);
+	if (ret)
+		return ret;
+	usleep_range(1000, 2000);
+
+	ret = regmap_write(fl->regmap, REG_BRT_A, brt_a) ?:
+	      regmap_update_bits(fl->regmap, REG_I_A, 0x1f, cur_a & 0x1f) ?:
+	      regmap_write(fl->regmap, REG_BRT_B, brt_b) ?:
+	      regmap_write(fl->regmap, REG_I_B, cur_b & 0x1f);
+	if (ret)
+		return ret;
+
+	if (cur_a)
+		en |= CTRL_LEDA_EN;
+	if (cur_b)
+		en |= CTRL_LEDB_EN;
+	return regmap_update_bits(fl->regmap, REG_CTRL,
+				  CTRL_LEDA_EN | CTRL_LEDB_EN, en);
+}
+
+/* The vendor's _fl_lm3630a_percentage(). */
+static int ntx_fl_apply(struct ntx_fl *fl)
+{
+	const struct ntx_fl_colour *c;
+	unsigned int i;
+	u8 cur_a, cur_b;
+
+	if (!fl->percent)
+		return ntx_fl_set(fl, 0, 0, 0, 0);
+
+	c = &fl->colours[fl->colour];
+	i = fl->percent - 1;
+	cur_a = c->cur_a[i] ?: c->def_cur_a ?: fl->hdr_cur_a ?: 4;
+	cur_b = c->cur_b[i] ?: c->def_cur_b ?: fl->hdr_cur_b ?: 7;
+	return ntx_fl_set(fl, cur_a, c->brt_a[i], cur_b, c->brt_b[i]);
+}
+
+static int ntx_fl_update_status(struct backlight_device *bl)
+{
+	struct ntx_fl *fl = bl_get_data(bl);
+	int ret;
+
+	mutex_lock(&fl->lock);
+	fl->percent = backlight_get_brightness(bl);
+	/* Both devices show the same percentage, as the vendor's do. */
+	if (fl->bl)
+		fl->bl->props.brightness = fl->percent;
+	if (fl->bl_led)
+		fl->bl_led->props.brightness = fl->percent;
+	ret = ntx_fl_apply(fl);
+	mutex_unlock(&fl->lock);
+	return ret;
+}
+
+static const struct backlight_ops ntx_fl_ops = {
+	.options = BL_CORE_SUSPENDRESUME,
+	.update_status = ntx_fl_update_status,
+};
+
+static ssize_t color_show(struct device *dev, struct device_attribute *attr,
+			  char *buf)
+{
+	struct ntx_fl *fl = bl_get_data(to_backlight_device(dev));
+
+	return sysfs_emit(buf, "%u", fl->colour);
+}
+
+static ssize_t color_store(struct device *dev, struct device_attribute *attr,
+			   const char *buf, size_t count)
+{
+	struct ntx_fl *fl = bl_get_data(to_backlight_device(dev));
+	unsigned int v;
+	int ret;
+
+	ret = kstrtouint(buf, 10, &v);
+	if (ret)
+		return ret;
+	if (v >= fl->ncolours)
+		return -EINVAL;
+	mutex_lock(&fl->lock);
+	fl->colour = v;
+	ret = ntx_fl_apply(fl);
+	mutex_unlock(&fl->lock);
+	return ret ?: count;
+}
+static DEVICE_ATTR_RW(color);
+
+static ssize_t max_color_show(struct device *dev, struct device_attribute *attr,
+			      char *buf)
+{
+	struct ntx_fl *fl = bl_get_data(to_backlight_device(dev));
+
+	return sysfs_emit(buf, "%u", fl->ncolours - 1);
+}
+static DEVICE_ATTR_RO(max_color);
+
+static struct attribute *ntx_fl_attrs[] = {
+	&dev_attr_color.attr,
+	&dev_attr_max_color.attr,
+	NULL
+};
+static const struct attribute_group ntx_fl_group = { .attrs = ntx_fl_attrs };
+
+static void ntx_fl_remove_group(void *data)
+{
+	struct backlight_device *bl = data;
+
+	sysfs_remove_group(&bl->dev.kobj, &ntx_fl_group);
+}
+
+/* Find the dual-colour tables in the bootloader's ntxfw area. */
+static int ntx_fl_parse_ntxfw(struct ntx_fl *fl)
+{
+	struct device *dev = &fl->client->dev;
+	struct device_node *np;
+	struct resource res;
+	const u8 *fw, *p, *end;
+	unsigned int items, i;
+	void *map;
+	int ret = -ENOENT;
+
+	np = of_parse_phandle(dev->of_node, "memory-region", 0);
+	if (!np)
+		return dev_err_probe(dev, -EINVAL, "no memory-region (ntxfw)\n");
+	ret = of_address_to_resource(np, 0, &res);
+	of_node_put(np);
+	if (ret)
+		return ret;
+	map = memremap(res.start, resource_size(&res), MEMREMAP_WB);
+	if (!map)
+		return -ENOMEM;
+	fw = map;
+	end = fw + resource_size(&res);
+
+	if (memcmp(fw, NTXFW_SIG_S, sizeof(NTXFW_SIG_S) - 1)) {
+		ret = dev_err_probe(dev, -EINVAL, "no ntxfw at %pR\n", &res);
+		goto out;
+	}
+	items = get_unaligned_le16(fw + 46);
+	p = fw + NTXFW_HDR_SIZE;
+	ret = -ENOENT;
+	for (i = 0; i < items && p + NTXFW_ITEM_HDR_SIZE <= end; i++) {
+		u16 type = get_unaligned_le16(p + 12);
+		u32 size = get_unaligned_le32(p + 16);
+		const u8 *body = p + NTXFW_ITEM_HDR_SIZE;
+
+		if (get_unaligned_le32(p + 8) != 0x12345678 || body + size > end)
+			break;
+		if (type == NTXFW_TYPE_DUALFL_HDR && size >= 6) {
+			fl->ncolours = get_unaligned_le32(body);
+			fl->hdr_cur_a = body[4];
+			fl->hdr_cur_b = body[5];
+		} else if (type == NTXFW_TYPE_DUALFL_TAB && fl->ncolours &&
+			   size >= fl->ncolours * sizeof(struct ntx_fl_colour)) {
+			fl->colours = devm_kmemdup(dev, body,
+				fl->ncolours * sizeof(struct ntx_fl_colour),
+				GFP_KERNEL);
+			ret = fl->colours ? 0 : -ENOMEM;
+		}
+		p = body + size;
+	}
+	if (ret == -ENOENT)
+		dev_err(dev, "no LM3630A dual-colour table in ntxfw\n");
+	else if (!ret)
+		dev_info(dev, "%u colour temperatures (default currents %u/%u)\n",
+			 fl->ncolours, fl->hdr_cur_a, fl->hdr_cur_b);
+out:
+	memunmap(map);
+	return ret;
+}
+
+static const struct regmap_config ntx_fl_regmap = {
+	.reg_bits = 8,
+	.val_bits = 8,
+	.max_register = REG_FILTER_STRENGTH,
+};
+
+static int ntx_fl_probe(struct i2c_client *client)
+{
+	struct backlight_properties props = {
+		.type = BACKLIGHT_RAW,
+		.max_brightness = 100,
+	};
+	struct device *dev = &client->dev;
+	struct ntx_fl *fl;
+	unsigned int rev;
+	int ret;
+
+	fl = devm_kzalloc(dev, sizeof(*fl), GFP_KERNEL);
+	if (!fl)
+		return -ENOMEM;
+	fl->client = client;
+	mutex_init(&fl->lock);
+
+	ret = ntx_fl_parse_ntxfw(fl);
+	if (ret)
+		return ret;
+
+	fl->regmap = devm_regmap_init_i2c(client, &ntx_fl_regmap);
+	if (IS_ERR(fl->regmap))
+		return PTR_ERR(fl->regmap);
+
+	/* Off at probe; the chip must be enabled to answer, so look. */
+	fl->enable = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
+	if (IS_ERR(fl->enable))
+		return PTR_ERR(fl->enable);
+	msleep(50);
+	ret = regmap_read(fl->regmap, REG_REV, &rev);
+	if (ret)
+		return dev_err_probe(dev, ret, "no LM3630A\n");
+	gpiod_set_value_cansleep(fl->enable, 0);
+	dev_info(dev, "LM3630A rev 0x%02x\n", rev);
+
+	fl->bl = devm_backlight_device_register(dev, "mxc_msp430.0", dev, fl,
+						&ntx_fl_ops, &props);
+	if (IS_ERR(fl->bl))
+		return PTR_ERR(fl->bl);
+	fl->bl_led = devm_backlight_device_register(dev, "lm3630a_led", dev, fl,
+						    &ntx_fl_ops, &props);
+	if (IS_ERR(fl->bl_led))
+		return PTR_ERR(fl->bl_led);
+	ret = sysfs_create_group(&fl->bl_led->dev.kobj, &ntx_fl_group);
+	if (ret)
+		return ret;
+	return devm_add_action_or_reset(dev, ntx_fl_remove_group, fl->bl_led);
+}
+
+static const struct of_device_id ntx_fl_of_match[] = {
+	{ .compatible = "netronix,lm3630a-frontlight" },
+	{ }
+};
+MODULE_DEVICE_TABLE(of, ntx_fl_of_match);
+
+static struct i2c_driver ntx_fl_driver = {
+	.driver = {
+		.name = "ntx-lm3630a",
+		.of_match_table = ntx_fl_of_match,
+	},
+	.probe = ntx_fl_probe,
+};
+module_i2c_driver(ntx_fl_driver);
+
+MODULE_DESCRIPTION("Netronix LM3630A frontlight with ntxfw calibration");
+MODULE_LICENSE("GPL");