Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3501218pxf; Mon, 22 Mar 2021 07:53:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/oVQHN+joGr4GSU6LcYVZHczhIivvNNfkijOXzWFnQtcqmG2/Sq7BXlotLU9qIEIAv/bf X-Received: by 2002:a17:906:1d4e:: with SMTP id o14mr79911ejh.549.1616424833650; Mon, 22 Mar 2021 07:53:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616424833; cv=none; d=google.com; s=arc-20160816; b=VPthC8hV6gylzgW0M7lq1C1Tj5jMX8q/iMxUZt+xE6FFfLHXIG/yWYR2G9PrV6FwCl Tf/gpXlryljWa5nzqiNuR7jHzP7grjWicUZZSmc4hmVFgRzeR3G+2QfzYSiR0tLGdAe6 XhLV3ngj8ZqhgfEnoJqxSD0BBRs71SFsuiKVW4N2enpYMaCCrMTIjf3TDNzqhNA2obmq Ll/JTkxa+ri6pvaywY3N2mWG8GkvY+hQrTF8Mq/B7SKYHVq0C6LJTVeb6VTMjoC5r00h hJVMr1u1gjb3H0xufNoavbSF5thWkDMxWXBF2/v08QSbD9jkNKdM2ewJ1CmHotwvN7Qu AnDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=YstV5W7FipJg4hYtP55WI/iUVl7ZufJJVpmR9aubFUY=; b=Fvc6H/kjvVtHBIPw75nlO/0FVBL1zDX9/XRv9fas/97+sYn2MKrqlKJqZmZKZrwltU N6RrFzgUJMyqla2LSUJTVmQZgrqDVT960/PwJRMrFngQVB8444pMY0CygyKigkF13Mcb JjNaODsUaJNW2Xhg9ADN9peQOQYL31Lbz99bnCzPD9uqHAahy6frAHGXO33naKU1x+Cb T0p6VBtJIxcOyDiNR/9oxE9gHUSYpeEOTu3ZE/NoIJXgOi65GazJOa3zmtq3hN32PpAs x42P5Z18UbnB0xY4itOC5ekKXdK8kdUSZIjEk+g/6JGWF3Q+f3mh2EMMXYWDKUQS/fD6 zPWQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e26si11553055ejy.207.2021.03.22.07.53.31; Mon, 22 Mar 2021 07:53:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231352AbhCVOuh (ORCPT + 99 others); Mon, 22 Mar 2021 10:50:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230385AbhCVOtB (ORCPT ); Mon, 22 Mar 2021 10:49:01 -0400 Received: from michel.telenet-ops.be (michel.telenet-ops.be [IPv6:2a02:1800:110:4::f00:18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9648C0617AB for ; Mon, 22 Mar 2021 07:48:55 -0700 (PDT) Received: from ramsan.of.borg ([IPv6:2a02:1810:ac12:ed20:250b:f16c:c5e2:505d]) by michel.telenet-ops.be with bizsmtp id jSot2400d2HDxaV06SotqR; Mon, 22 Mar 2021 15:48:53 +0100 Received: from rox.of.borg ([192.168.97.57]) by ramsan.of.borg with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1lOLrQ-009ZWU-Tb; Mon, 22 Mar 2021 15:48:52 +0100 Received: from geert by rox.of.borg with local (Exim 4.93) (envelope-from ) id 1lOLrP-004T6p-Vv; Mon, 22 Mar 2021 15:48:52 +0100 From: Geert Uytterhoeven To: Robin van der Gracht , Rob Herring , Miguel Ojeda , Paul Burton , Greg Kroah-Hartman Cc: devicetree@vger.kernel.org, linux-mips@vger.kernel.org, linux-kernel@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH 16/17] auxdisplay: ht16k33: Add support for segment displays Date: Mon, 22 Mar 2021 15:48:47 +0100 Message-Id: <20210322144848.1065067-17-geert@linux-m68k.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210322144848.1065067-1-geert@linux-m68k.org> References: <20210322144848.1065067-1-geert@linux-m68k.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The Holtek HT16K33 LED controller is not only used for driving dot-matrix displays, but also for driving segment displays. Add support for 4-digit 7-segment and quad 14-segment alphanumeric displays, like the Adafruit 7-segment and 14-segment display backpack and FeatherWing expansion boards. Use the character line display core support to display a message, which will be scrolled if it doesn't fit. Signed-off-by: Geert Uytterhoeven --- The 7-segment support is based on schematics, and untested on actual hardware. --- drivers/auxdisplay/ht16k33.c | 198 +++++++++++++++++++++++++++++++++-- 1 file changed, 191 insertions(+), 7 deletions(-) diff --git a/drivers/auxdisplay/ht16k33.c b/drivers/auxdisplay/ht16k33.c index 11d86960fbc5d8c1..0b502a6039f89c6b 100644 --- a/drivers/auxdisplay/ht16k33.c +++ b/drivers/auxdisplay/ht16k33.c @@ -5,6 +5,7 @@ * Author: Robin van der Gracht * * Copyright: (C) 2016 Protonic Holland. + * Copyright (C) 2021 Glider bv */ #include @@ -17,9 +18,18 @@ #include #include #include +#include +#include #include #include +#include +#include + +#include + +#include "line-display.h" + /* Registers */ #define REG_SYSTEM_SETUP 0x20 #define REG_SYSTEM_SETUP_OSC_ON BIT(0) @@ -47,6 +57,12 @@ #define BYTES_PER_ROW (HT16K33_MATRIX_LED_MAX_ROWS / 8) #define HT16K33_FB_SIZE (HT16K33_MATRIX_LED_MAX_COLS * BYTES_PER_ROW) +enum display_type { + DISP_MATRIX = 0, + DISP_QUAD_7SEG, + DISP_QUAD_14SEG, +}; + struct ht16k33_keypad { struct i2c_client *client; struct input_dev *dev; @@ -67,11 +83,25 @@ struct ht16k33_fbdev { uint8_t *cache; }; +struct ht16k33_seg { + struct linedisp linedisp; + union { + struct seg7_conversion_map seg7; + struct seg14_conversion_map seg14; + } map; + unsigned int map_size; + char curr[4]; +}; + struct ht16k33_priv { struct i2c_client *client; struct delayed_work work; struct ht16k33_keypad keypad; - struct ht16k33_fbdev fbdev; + union { + struct ht16k33_fbdev fbdev; + struct ht16k33_seg seg; + }; + enum display_type type; }; static const struct fb_fix_screeninfo ht16k33_fb_fix = { @@ -101,6 +131,33 @@ static const struct fb_var_screeninfo ht16k33_fb_var = { .vmode = FB_VMODE_NONINTERLACED, }; +static const SEG7_DEFAULT_MAP(initial_map_seg7); +static const SEG14_DEFAULT_MAP(initial_map_seg14); + +static ssize_t map_seg_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct ht16k33_priv *priv = dev_get_drvdata(dev); + + memcpy(buf, &priv->seg.map, priv->seg.map_size); + return priv->seg.map_size; +} + +static ssize_t map_seg_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t cnt) +{ + struct ht16k33_priv *priv = dev_get_drvdata(dev); + + if (cnt != priv->seg.map_size) + return -EINVAL; + + memcpy(&priv->seg.map, buf, cnt); + return cnt; +} + +static DEVICE_ATTR(map_seg7, 0644, map_seg_show, map_seg_store); +static DEVICE_ATTR(map_seg14, 0644, map_seg_show, map_seg_store); + static int ht16k33_display_on(struct ht16k33_priv *priv) { uint8_t data = REG_DISPLAY_SETUP | REG_DISPLAY_SETUP_ON; @@ -325,6 +382,51 @@ static void ht16k33_keypad_stop(struct input_dev *dev) disable_irq(keypad->client->irq); } +static void ht16k33_linedisp_update(struct linedisp *linedisp) +{ + struct ht16k33_priv *priv = container_of(linedisp, struct ht16k33_priv, + seg.linedisp); + + schedule_delayed_work(&priv->work, 0); +} + +static void ht16k33_seg7_update(struct work_struct *work) +{ + struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv, + work.work); + struct ht16k33_seg *seg = &priv->seg; + char *s = seg->curr; + uint8_t buf[9]; + + buf[0] = map_to_seg7(&seg->map.seg7, *s++); + buf[1] = 0; + buf[2] = map_to_seg7(&seg->map.seg7, *s++); + buf[3] = 0; + buf[4] = 0; + buf[5] = 0; + buf[6] = map_to_seg7(&seg->map.seg7, *s++); + buf[7] = 0; + buf[8] = map_to_seg7(&seg->map.seg7, *s++); + + i2c_smbus_write_i2c_block_data(priv->client, 0, ARRAY_SIZE(buf), buf); +} + +static void ht16k33_seg14_update(struct work_struct *work) +{ + struct ht16k33_priv *priv = container_of(work, struct ht16k33_priv, + work.work); + struct ht16k33_seg *seg = &priv->seg; + char *s = seg->curr; + uint8_t buf[8]; + + put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf); + put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf + 2); + put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf + 4); + put_unaligned_le16(map_to_seg14(&seg->map.seg14, *s++), buf + 6); + + i2c_smbus_write_i2c_block_data(priv->client, 0, ARRAY_SIZE(buf), buf); +} + static int ht16k33_keypad_probe(struct i2c_client *client, struct ht16k33_keypad *keypad) { @@ -473,10 +575,57 @@ static int ht16k33_fbdev_probe(struct i2c_client *client, return err; } +static int ht16k33_seg_probe(struct i2c_client *client, + struct ht16k33_priv *priv, uint32_t brightness) +{ + struct ht16k33_seg *seg = &priv->seg; + struct device *dev = &client->dev; + int err; + + err = ht16k33_brightness_set(priv, MAX_BRIGHTNESS); + if (err) + return err; + + switch (priv->type) { + case DISP_MATRIX: + /* not handled here */ + break; + + case DISP_QUAD_7SEG: + INIT_DELAYED_WORK(&priv->work, ht16k33_seg7_update); + seg->map.seg7 = initial_map_seg7; + seg->map_size = sizeof(seg->map.seg7); + err = device_create_file(dev, &dev_attr_map_seg7); + break; + + case DISP_QUAD_14SEG: + INIT_DELAYED_WORK(&priv->work, ht16k33_seg14_update); + seg->map.seg14 = initial_map_seg14; + seg->map_size = sizeof(seg->map.seg14); + err = device_create_file(dev, &dev_attr_map_seg14); + break; + } + if (err) + return err; + + err = linedisp_register(&seg->linedisp, dev, 4, seg->curr, + ht16k33_linedisp_update); + if (err) + goto err_remove_map_file; + + return 0; + +err_remove_map_file: + device_remove_file(dev, &dev_attr_map_seg7); + device_remove_file(dev, &dev_attr_map_seg14); + return err; +} + static int ht16k33_probe(struct i2c_client *client) { struct device *dev = &client->dev; struct device_node *node = dev->of_node; + const struct of_device_id *id; struct ht16k33_priv *priv; uint32_t dft_brightness; int err; @@ -491,6 +640,9 @@ static int ht16k33_probe(struct i2c_client *client) return -ENOMEM; priv->client = client; + id = i2c_of_match_device(dev->driver->of_match_table, client); + if (id) + priv->type = (uintptr_t)id->data; i2c_set_clientdata(client, priv); err = ht16k33_initialize(priv); @@ -515,8 +667,19 @@ static int ht16k33_probe(struct i2c_client *client) return err; } - /* Frame Buffer Display */ - return ht16k33_fbdev_probe(client, priv, dft_brightness); + switch (priv->type) { + case DISP_MATRIX: + /* Frame Buffer Display */ + err = ht16k33_fbdev_probe(client, priv, dft_brightness); + break; + + case DISP_QUAD_7SEG: + case DISP_QUAD_14SEG: + /* Segment Display */ + err = ht16k33_seg_probe(client, priv, dft_brightness); + break; + } + return err; } static int ht16k33_remove(struct i2c_client *client) @@ -525,9 +688,21 @@ static int ht16k33_remove(struct i2c_client *client) struct ht16k33_fbdev *fbdev = &priv->fbdev; cancel_delayed_work_sync(&priv->work); - unregister_framebuffer(fbdev->info); - framebuffer_release(fbdev->info); - free_page((unsigned long) fbdev->buffer); + + switch (priv->type) { + case DISP_MATRIX: + unregister_framebuffer(fbdev->info); + framebuffer_release(fbdev->info); + free_page((unsigned long)fbdev->buffer); + break; + + case DISP_QUAD_7SEG: + case DISP_QUAD_14SEG: + linedisp_unregister(&priv->seg.linedisp); + device_remove_file(&client->dev, &dev_attr_map_seg7); + device_remove_file(&client->dev, &dev_attr_map_seg14); + break; + } return 0; } @@ -539,7 +714,16 @@ static const struct i2c_device_id ht16k33_i2c_match[] = { MODULE_DEVICE_TABLE(i2c, ht16k33_i2c_match); static const struct of_device_id ht16k33_of_match[] = { - { .compatible = "holtek,ht16k33", }, + { + /* 0.56" 4-Digit 7-Segment FeatherWing Display (Red) */ + .compatible = "adafruit,3108", .data = (void *)DISP_QUAD_7SEG, + }, { + /* 0.54" Quad Alphanumeric FeatherWing Display (Red) */ + .compatible = "adafruit,3130", .data = (void *)DISP_QUAD_14SEG, + }, { + /* Generic, assumed Dot-Matrix Display */ + .compatible = "holtek,ht16k33", .data = (void *)DISP_MATRIX, + }, { } }; MODULE_DEVICE_TABLE(of, ht16k33_of_match); -- 2.25.1