linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Andreas Färber" <afaerber@suse.de>
To: linux-realtek-soc@lists.infradead.org, linux-leds@vger.kernel.org
Cc: "Jacek Anaszewski" <jacek.anaszewski@gmail.com>,
	"Pavel Machek" <pavel@ucw.cz>, "Dan Murphy" <dmurphy@ti.com>,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, "Andreas Färber" <afaerber@suse.de>
Subject: [RFC 19/25] leds: tm1628: Add 7-segment display support
Date: Thu, 12 Dec 2019 04:39:46 +0100	[thread overview]
Message-ID: <20191212033952.5967-20-afaerber@suse.de> (raw)
In-Reply-To: <20191212033952.5967-1-afaerber@suse.de>

Detect DT child nodes covering more than one LED component and implement
a text to 7-segment display mapping.

Signed-off-by: Andreas Färber <afaerber@suse.de>
---
 drivers/leds/leds-tm1628.c | 230 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 229 insertions(+), 1 deletion(-)

diff --git a/drivers/leds/leds-tm1628.c b/drivers/leds/leds-tm1628.c
index 8a8fd1562853..e2c2a5d91596 100644
--- a/drivers/leds/leds-tm1628.c
+++ b/drivers/leds/leds-tm1628.c
@@ -47,6 +47,15 @@ struct tm1628_info {
 	int				default_pwm;
 };
 
+struct tm1628_segment {
+	u32 grid;
+	u32 seg;
+};
+
+struct tm1628_display {
+	struct tm1628_segment segments[8];
+};
+
 struct tm1628_led {
 	struct led_classdev	leddev;
 	struct tm1628		*ctrl;
@@ -62,6 +71,8 @@ struct tm1628 {
 	int				mode_index;
 	int				pwm_index;
 	u8				data[14];
+	unsigned int			num_displays;
+	struct tm1628_display		*displays;
 	unsigned int			num_leds;
 	struct tm1628_led		leds[];
 };
@@ -253,6 +264,215 @@ static int tm1628_register_led(struct tm1628 *s,
 	return devm_led_classdev_register_ext(dev, &led->leddev, &init_data);
 }
 
+#define SSD_TOP			BIT(0)
+#define SSD_TOP_RIGHT		BIT(1)
+#define SSD_BOTTOM_RIGHT	BIT(2)
+#define SSD_BOTTOM		BIT(3)
+#define SSD_BOTTOM_LEFT		BIT(4)
+#define SSD_TOP_LEFT		BIT(5)
+#define SSD_MIDDLE		BIT(6)
+#define SSD_DOT			BIT(7)
+
+struct tm1628_ssd_char {
+	char ch;
+	u8 segs;
+};
+
+static const struct tm1628_ssd_char tm1628_char_ssd_map[] = {
+	{ '0', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ '1', SSD_TOP_RIGHT | SSD_BOTTOM_RIGHT },
+	{ '2', SSD_TOP | SSD_TOP_RIGHT | SSD_MIDDLE | SSD_BOTTOM_LEFT | SSD_BOTTOM },
+	{ '3', SSD_TOP | SSD_TOP_RIGHT | SSD_MIDDLE | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ '4', SSD_TOP_LEFT | SSD_TOP_RIGHT | SSD_MIDDLE | SSD_BOTTOM_RIGHT },
+	{ '5', SSD_TOP | SSD_TOP_LEFT | SSD_MIDDLE | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ '6', SSD_TOP | SSD_TOP_LEFT | SSD_MIDDLE | SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ '7', SSD_TOP | SSD_TOP_RIGHT | SSD_BOTTOM_RIGHT },
+	{ '8', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT | SSD_MIDDLE |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ '9', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT | SSD_MIDDLE |
+	       SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ 'A', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT | SSD_MIDDLE |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT },
+	{ 'B', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT | SSD_MIDDLE |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ 'C', SSD_TOP | SSD_TOP_LEFT | SSD_BOTTOM_LEFT | SSD_BOTTOM },
+	{ 'D', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ 'E', SSD_TOP | SSD_TOP_LEFT | SSD_MIDDLE | SSD_BOTTOM_LEFT | SSD_BOTTOM },
+	{ 'F', SSD_TOP | SSD_TOP_LEFT | SSD_MIDDLE | SSD_BOTTOM_LEFT },
+	{ 'G', SSD_TOP | SSD_TOP_LEFT | SSD_BOTTOM_LEFT | SSD_BOTTOM | SSD_BOTTOM_RIGHT },
+	{ 'H', SSD_TOP_LEFT | SSD_TOP_RIGHT | SSD_MIDDLE |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT },
+	{ 'I', SSD_TOP_LEFT | SSD_BOTTOM_LEFT },
+	{ 'J', SSD_TOP | SSD_TOP_RIGHT | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ 'K', SSD_TOP_LEFT | SSD_TOP_RIGHT | SSD_MIDDLE |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT },
+	{ 'L', SSD_TOP_LEFT | SSD_BOTTOM_LEFT | SSD_BOTTOM },
+	{ 'O', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ 'P', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT | SSD_MIDDLE |
+	       SSD_BOTTOM_LEFT },
+	{ 'Q', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ 'R', SSD_TOP | SSD_TOP_LEFT | SSD_TOP_RIGHT | SSD_MIDDLE |
+	       SSD_BOTTOM_LEFT | SSD_BOTTOM_RIGHT },
+	{ 'S', SSD_TOP | SSD_TOP_LEFT | SSD_MIDDLE | SSD_BOTTOM_RIGHT | SSD_BOTTOM },
+	{ 'T', SSD_TOP | SSD_TOP_LEFT | SSD_BOTTOM_LEFT },
+	{ 'U', SSD_TOP_LEFT | SSD_BOTTOM_LEFT | SSD_BOTTOM | SSD_BOTTOM_RIGHT | SSD_TOP_RIGHT },
+	{ 'V', SSD_TOP_LEFT | SSD_BOTTOM_LEFT | SSD_BOTTOM | SSD_BOTTOM_RIGHT | SSD_TOP_RIGHT },
+	{ 'W', SSD_TOP_LEFT | SSD_BOTTOM_LEFT | SSD_BOTTOM | SSD_BOTTOM_RIGHT | SSD_TOP_RIGHT },
+	{ 'Z', SSD_TOP | SSD_TOP_RIGHT | SSD_MIDDLE | SSD_BOTTOM_LEFT | SSD_BOTTOM },
+	{ 'b', SSD_TOP_LEFT | SSD_MIDDLE | SSD_BOTTOM_LEFT | SSD_BOTTOM | SSD_BOTTOM_RIGHT },
+	{ 'l', SSD_TOP_LEFT | SSD_BOTTOM_LEFT },
+	{ 'o', SSD_MIDDLE | SSD_BOTTOM_LEFT | SSD_BOTTOM | SSD_BOTTOM_RIGHT },
+	{ 't', SSD_TOP_LEFT | SSD_MIDDLE | SSD_BOTTOM_LEFT | SSD_BOTTOM },
+	{ 'u', SSD_BOTTOM_LEFT | SSD_BOTTOM | SSD_BOTTOM_RIGHT },
+	{ 'v', SSD_BOTTOM_LEFT | SSD_BOTTOM | SSD_BOTTOM_RIGHT },
+	{ 'w', SSD_BOTTOM_LEFT | SSD_BOTTOM | SSD_BOTTOM_RIGHT },
+	{ '-', SSD_MIDDLE },
+	{ '_', SSD_BOTTOM },
+	{ '.', SSD_DOT },
+};
+
+static u8 tm1628_get_char_ssd_map(char ch)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(tm1628_char_ssd_map); i++) {
+		if (tm1628_char_ssd_map[i].ch == ch)
+			return tm1628_char_ssd_map[i].segs;
+	}
+
+	return 0x0;
+}
+
+static int tm1628_display_apply_map(struct tm1628 *s,
+	struct tm1628_display *display, u8 map)
+{
+	struct tm1628_segment *segment;
+	int i;
+
+	for (i = 0; i < 8; i++) {
+		segment = &display->segments[i];
+		tm1628_set_led(s, segment->grid, segment->seg, map & BIT(i));
+	}
+
+	return 0;
+}
+
+static ssize_t text_store(struct device *dev,
+	struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct tm1628 *s = dev_get_drvdata(dev);
+	size_t offset, len = count;
+	u8 map;
+	int i, ret;
+
+	if (len > 0 && buf[len - 1] == '\n')
+		len--;
+
+	for (i = 0, offset = 0; i < s->num_displays; i++) {
+		if (offset < len) {
+			map = tm1628_get_char_ssd_map(buf[offset]);
+			offset++;
+		} else
+			map = 0x0;
+
+		tm1628_display_apply_map(s, &s->displays[i], map);
+	}
+
+	ret = tm1628_set_address(s->spi, 0x0);
+	if (ret)
+		return ret;
+
+	ret = tm1628_write_data(s->spi, s->data, 14);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static struct device_attribute tm1628_attr =
+	__ATTR_WO(text);
+
+static int tm1628_register_display(struct tm1628 *s,
+	struct fwnode_handle *node)
+{
+	struct device *dev = &s->spi->dev;
+	struct tm1628_display *display;
+	u32 *reg;
+	u32 grid, seg;
+	int i, j, ret, reg_count;
+
+	reg_count = fwnode_property_count_u32(node, "reg");
+	if (reg_count < 0)
+		return reg_count;
+
+	if (reg_count % 2) {
+		dev_warn(dev, "Ignoring extra cell in %s reg property\n",
+			 fwnode_get_name(node));
+		reg_count--;
+	}
+
+	if (s->displays) {
+		dev_warn(dev, "Only one display supported\n");
+		return -EINVAL;
+	}
+
+	s->num_displays = reg_count >> 1;
+
+	reg = devm_kzalloc(dev, reg_count * sizeof(*reg), GFP_KERNEL);
+	if (!reg)
+		return -ENOMEM;
+
+	ret = fwnode_property_read_u32_array(node, "reg", reg, reg_count);
+	if (ret) {
+		dev_err(dev, "Reading %s reg property failed (%d)\n",
+			fwnode_get_name(node), ret);
+		return ret;
+	}
+
+	s->displays = devm_kzalloc(dev, s->num_displays * sizeof(*s->displays), GFP_KERNEL);
+	if (!s->displays)
+		return -ENOMEM;
+
+	for (i = 0; i < s->num_displays; i++) {
+		display = &s->displays[i];
+		grid = reg[i * 2];
+		seg  = reg[i * 2 + 1];
+		if (grid == 0 && seg != 0) {
+			if (!tm1628_is_valid_seg(s, seg)) {
+				dev_warn(dev, "%s reg out of range\n", fwnode_get_name(node));
+				return -EINVAL;
+			}
+			grid = s->info->modes[s->mode_index].grid_mask;
+			for (j = 0; grid && j < 7; j++) {
+				display->segments[j].seg = seg;
+				display->segments[j].grid = __ffs(grid);
+				grid &= ~BIT(display->segments[j].grid);
+			}
+		} else if (grid != 0 && seg == 0) {
+			if (!tm1628_is_valid_grid(s, grid)) {
+				dev_warn(dev, "%s reg out of range\n", fwnode_get_name(node));
+				return -EINVAL;
+			}
+			seg = s->info->modes[s->mode_index].seg_mask;
+			for (j = 0; seg && j < 8; j++) {
+				display->segments[j].grid = grid;
+				display->segments[j].seg = __ffs(seg);
+				seg &= ~BIT(display->segments[j].seg);
+			}
+		}
+	}
+
+	devm_kfree(dev, reg);
+
+	device_create_file(dev, &tm1628_attr);
+
+	return 0;
+}
+
 /* Work around __builtin_popcount() */
 static u32 tm1628_grid_popcount(u8 grid_mask)
 {
@@ -325,7 +545,7 @@ static int tm1628_spi_probe(struct spi_device *spi)
 			return ret;
 		}
 
-		if (fwnode_property_count_u32(child, "reg") == 2) {
+		if (reg[0] != 0 && reg[1] != 0 && fwnode_property_count_u32(child, "reg") == 2) {
 			ret = tm1628_register_led(s, child, reg[0], reg[1], &s->leds[i++]);
 			if (ret && ret != -EINVAL) {
 				dev_err(&spi->dev, "Failed to register LED %s (%d)\n",
@@ -334,6 +554,14 @@ static int tm1628_spi_probe(struct spi_device *spi)
 				return ret;
 			}
 			s->num_leds++;
+		} else {
+			ret = tm1628_register_display(s, child);
+			if (ret) {
+				dev_err(&spi->dev, "Failed to register display %s (%d)\n",
+					fwnode_get_name(child), ret);
+				fwnode_handle_put(child);
+				return ret;
+			}
 		}
 	}
 
-- 
2.16.4


  parent reply	other threads:[~2019-12-12  3:40 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-12  3:39 [RFC 00/25] arm64: realtek: Add Xnano X5 and implement TM1628/FD628/AiP1618 LED controllers Andreas Färber
2019-12-12  3:39 ` [RFC 01/25] dt-bindings: vendor-prefixes: Add Xnano Andreas Färber
2019-12-19 22:26   ` Rob Herring
2019-12-12  3:39 ` [RFC 02/25] dt-bindings: arm: realtek: Add Xnano X5 Andreas Färber
2019-12-19 22:27   ` Rob Herring
2019-12-12  3:39 ` [RFC 03/25] arm64: dts: realtek: rtd1295: " Andreas Färber
2019-12-12  3:39 ` [RFC 04/25] spi: gpio: Implement LSB First bitbang support Andreas Färber
2019-12-12  8:40   ` Geert Uytterhoeven
2019-12-12 15:14     ` Andreas Färber
2019-12-12 17:19       ` Mark Brown
2019-12-12 21:08         ` Andreas Färber
2019-12-13 11:42           ` Mark Brown
2019-12-12  3:39 ` [RFC 05/25] dt-bindings: vendor-prefixes: Add Titan Micro Electronics Andreas Färber
2019-12-19 22:31   ` Rob Herring
2019-12-12  3:39 ` [RFC 06/25] dt-bindings: leds: Add Titan Micro Electronics TM1628 Andreas Färber
2019-12-19 23:04   ` Rob Herring
2019-12-12  3:39 ` [RFC 07/25] " Andreas Färber
2019-12-14  9:48   ` Andreas Färber
2019-12-12  3:39 ` [RFC 08/25] arm64: dts: realtek: rtd129x-zidoo-x9s: Add TM1628 LED controller Andreas Färber
2019-12-12  3:39 ` [RFC 09/25] arm64: dts: realtek: rtd1295-zidoo-x9s: Add regular LEDs to TM1628 Andreas Färber
2019-12-12  3:39 ` [RFC 10/25] dt-bindings: vendor-prefixes: Add Fuda Hisi Microelectronics Andreas Färber
2019-12-19 23:04   ` Rob Herring
2019-12-12  3:39 ` [RFC 11/25] dt-bindings: leds: tm1628: Add Fuda Hisi Microelectronics FD628 Andreas Färber
2019-12-19 23:05   ` Rob Herring
2019-12-12  3:39 ` [RFC 12/25] " Andreas Färber
2019-12-12  3:39 ` [RFC 13/25] arm64: dts: realtek: rtd1295-xnano-x5: Add FD628 LED controller Andreas Färber
2019-12-12  3:39 ` [RFC 14/25] arm64: dts: realtek: rtd1295-xnano-x5: Add regular LEDs to FD628 Andreas Färber
2019-12-21 20:21   ` Pavel Machek
2019-12-12  3:39 ` [RFC 15/25] dt-bindings: vendor-prefixes: Add Fude Microelectronics Andreas Färber
2019-12-19 23:05   ` Rob Herring
2019-12-12  3:39 ` [RFC 16/25] dt-bindings: leds: tm1628: Add Fude Microelectronics AiP1618 Andreas Färber
2019-12-19 23:06   ` Rob Herring
2019-12-12  3:39 ` [RFC 17/25] leds: tm1628: Prepare " Andreas Färber
2019-12-21 19:55   ` Andreas Färber
2019-12-12  3:39 ` [RFC 18/25] dt-bindings: leds: tm1628: Define display child nodes Andreas Färber
2019-12-12  3:39 ` Andreas Färber [this message]
2019-12-12  8:33   ` [RFC 19/25] leds: tm1628: Add 7-segment display support Geert Uytterhoeven
2019-12-12 14:10     ` Andreas Färber
2019-12-21 20:23   ` Pavel Machek
2019-12-12  3:39 ` [RFC 20/25] arm64: dts: realtek: rtd1295-zidoo-x9s: Add display to TM1628 Andreas Färber
2019-12-12  3:39 ` [RFC 21/25] arm64: dts: realtek: rtd1295-xnano-x5: Add display to FD628 Andreas Färber
2019-12-12  3:39 ` [RFC 22/25] leds: tm1826: Add combined glyph support Andreas Färber
2019-12-21 20:27   ` Pavel Machek
2019-12-21 20:41     ` Andreas Färber
2019-12-21 21:04       ` Pavel Machek
2019-12-21 21:49         ` Andreas Färber
     [not found]           ` <CANiq72nA9OLa0SjY8W055J_2A32tcp7S98SruKSdWH2dm25VKw@mail.gmail.com>
2019-12-22  3:14             ` Andreas Färber
2020-01-15 14:31               ` Miguel Ojeda
2019-12-12  3:39 ` [RFC 23/25] WIP: leds: tm1628: Prepare TM1628 keys Andreas Färber
2019-12-12  3:39 ` [RFC 24/25] WIP: leds: tm1628: Prepare FD628 keys Andreas Färber
2019-12-12  3:39 ` [RFC 25/25] WIP: leds: tm1628: Prepare AiP1618 keys Andreas Färber
2019-12-12 13:14 ` [RFC 00/25] arm64: realtek: Add Xnano X5 and implement TM1628/FD628/AiP1618 LED controllers Robin Murphy
2019-12-12 20:55   ` Andreas Färber
2019-12-13 14:07     ` Robin Murphy
2019-12-13 14:36       ` Geert Uytterhoeven
2020-02-25 21:42       ` Ezra Buehler
2020-02-26 13:03         ` Pavel Machek
2019-12-21 18:20 ` Pavel Machek
2019-12-21 21:07   ` Andreas Färber
2020-01-15 13:34 ` Andreas Färber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191212033952.5967-20-afaerber@suse.de \
    --to=afaerber@suse.de \
    --cc=dmurphy@ti.com \
    --cc=jacek.anaszewski@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=linux-realtek-soc@lists.infradead.org \
    --cc=pavel@ucw.cz \
    --subject='Re: [RFC 19/25] leds: tm1628: Add 7-segment display support' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).