Skip to content

Commit

Permalink
auxdisplay: Extract character line display core support
Browse files Browse the repository at this point in the history
Extract the character line display core support from the simple ASCII
LCD driver for the MIPS Boston, Malta & SEAD3 development boards into
its own subdriver, so it can be reused for other displays.

As this moves the "message" device attribute in sysfs in a "linedisp.N"
subdirectory, a symlink is added to preserve backwards compatibility.

Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
  • Loading branch information
geertu authored and intel-lab-lkp committed Jun 25, 2021
1 parent 2c4ee5f commit 5505aed
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 167 deletions.
7 changes: 7 additions & 0 deletions drivers/auxdisplay/Kconfig
Expand Up @@ -25,6 +25,12 @@ config CHARLCD
This is some character LCD core interface that multiple drivers can
use.

config LINEDISP
tristate "Character line display core support" if COMPILE_TEST
help
This is the core support for single-line character displays, to be
selected by drivers that use it.

config HD44780_COMMON
tristate "Common functions for HD44780 (and compatibles) LCD displays" if COMPILE_TEST
select CHARLCD
Expand Down Expand Up @@ -155,6 +161,7 @@ config IMG_ASCII_LCD
depends on HAS_IOMEM
default y if MIPS_MALTA
select MFD_SYSCON
select LINEDISP
help
Enable this to support the simple ASCII LCD displays found on
development boards such as the MIPS Boston, MIPS Malta & MIPS SEAD3
Expand Down
1 change: 1 addition & 0 deletions drivers/auxdisplay/Makefile
Expand Up @@ -13,3 +13,4 @@ obj-$(CONFIG_HD44780) += hd44780.o
obj-$(CONFIG_HT16K33) += ht16k33.o
obj-$(CONFIG_PARPORT_PANEL) += panel.o
obj-$(CONFIG_LCD2S) += lcd2s.o
obj-$(CONFIG_LINEDISP) += line-display.o
205 changes: 38 additions & 167 deletions drivers/auxdisplay/img-ascii-lcd.c
Expand Up @@ -4,7 +4,6 @@
* Author: Paul Burton <paul.burton@mips.com>
*/

#include <generated/utsrelease.h>
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/mfd/syscon.h>
Expand All @@ -14,7 +13,8 @@
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/sysfs.h>

#include "line-display.h"

struct img_ascii_lcd_ctx;

Expand All @@ -27,45 +27,37 @@ struct img_ascii_lcd_ctx;
struct img_ascii_lcd_config {
unsigned int num_chars;
bool external_regmap;
void (*update)(struct img_ascii_lcd_ctx *ctx);
void (*update)(struct linedisp *linedisp);
};

/**
* struct img_ascii_lcd_ctx - Private data structure
* @pdev: the ASCII LCD platform device
* @base: the base address of the LCD registers
* @regmap: the regmap through which LCD registers are accessed
* @offset: the offset within regmap to the start of the LCD registers
* @cfg: pointer to the LCD model configuration
* @message: the full message to display or scroll on the LCD
* @message_len: the length of the @message string
* @scroll_pos: index of the first character of @message currently displayed
* @scroll_rate: scroll interval in jiffies
* @timer: timer used to implement scrolling
* @linedisp: line display structure
* @curr: the string currently displayed on the LCD
*/
struct img_ascii_lcd_ctx {
struct platform_device *pdev;
union {
void __iomem *base;
struct regmap *regmap;
};
u32 offset;
const struct img_ascii_lcd_config *cfg;
char *message;
unsigned int message_len;
unsigned int scroll_pos;
unsigned int scroll_rate;
struct timer_list timer;
struct linedisp linedisp;
char curr[] __aligned(8);
};

/*
* MIPS Boston development board
*/

static void boston_update(struct img_ascii_lcd_ctx *ctx)
static void boston_update(struct linedisp *linedisp)
{
struct img_ascii_lcd_ctx *ctx =
container_of(linedisp, struct img_ascii_lcd_ctx, linedisp);
ulong val;

#if BITS_PER_LONG == 64
Expand All @@ -90,12 +82,14 @@ static struct img_ascii_lcd_config boston_config = {
* MIPS Malta development board
*/

static void malta_update(struct img_ascii_lcd_ctx *ctx)
static void malta_update(struct linedisp *linedisp)
{
struct img_ascii_lcd_ctx *ctx =
container_of(linedisp, struct img_ascii_lcd_ctx, linedisp);
unsigned int i;
int err = 0;

for (i = 0; i < ctx->cfg->num_chars; i++) {
for (i = 0; i < linedisp->num_chars; i++) {
err = regmap_write(ctx->regmap,
ctx->offset + (i * 8), ctx->curr[i]);
if (err)
Expand Down Expand Up @@ -173,12 +167,14 @@ static int sead3_wait_lcd_idle(struct img_ascii_lcd_ctx *ctx)
return 0;
}

static void sead3_update(struct img_ascii_lcd_ctx *ctx)
static void sead3_update(struct linedisp *linedisp)
{
struct img_ascii_lcd_ctx *ctx =
container_of(linedisp, struct img_ascii_lcd_ctx, linedisp);
unsigned int i;
int err = 0;

for (i = 0; i < ctx->cfg->num_chars; i++) {
for (i = 0; i < linedisp->num_chars; i++) {
err = sead3_wait_lcd_idle(ctx);
if (err)
break;
Expand Down Expand Up @@ -218,140 +214,22 @@ static const struct of_device_id img_ascii_lcd_matches[] = {
};
MODULE_DEVICE_TABLE(of, img_ascii_lcd_matches);

/**
* img_ascii_lcd_scroll() - scroll the display by a character
* @t: really a pointer to the private data structure
*
* Scroll the current message along the LCD by one character, rearming the
* timer if required.
*/
static void img_ascii_lcd_scroll(struct timer_list *t)
static int sysfs_create_file_link(struct kobject *parent,
struct kobject *target, const char *name)
{
struct img_ascii_lcd_ctx *ctx = from_timer(ctx, t, timer);
unsigned int i, ch = ctx->scroll_pos;
unsigned int num_chars = ctx->cfg->num_chars;

/* update the current message string */
for (i = 0; i < num_chars;) {
/* copy as many characters from the string as possible */
for (; i < num_chars && ch < ctx->message_len; i++, ch++)
ctx->curr[i] = ctx->message[ch];

/* wrap around to the start of the string */
ch = 0;
}

/* update the LCD */
ctx->cfg->update(ctx);
struct kernfs_node *kn;

/* move on to the next character */
ctx->scroll_pos++;
ctx->scroll_pos %= ctx->message_len;
kn = kernfs_find_and_get(target->sd, "message");
if (!kn)
return -ENOENT;

/* rearm the timer */
if (ctx->message_len > ctx->cfg->num_chars)
mod_timer(&ctx->timer, jiffies + ctx->scroll_rate);
}

/**
* img_ascii_lcd_display() - set the message to be displayed
* @ctx: pointer to the private data structure
* @msg: the message to display
* @count: length of msg, or -1
*
* Display a new message @msg on the LCD. @msg can be longer than the number of
* characters the LCD can display, in which case it will begin scrolling across
* the LCD display.
*
* Return: 0 on success, -ENOMEM on memory allocation failure
*/
static int img_ascii_lcd_display(struct img_ascii_lcd_ctx *ctx,
const char *msg, ssize_t count)
{
char *new_msg;

/* stop the scroll timer */
del_timer_sync(&ctx->timer);

if (count == -1)
count = strlen(msg);

/* if the string ends with a newline, trim it */
if (msg[count - 1] == '\n')
count--;

if (!count) {
/* clear the LCD */
devm_kfree(&ctx->pdev->dev, ctx->message);
ctx->message = NULL;
ctx->message_len = 0;
memset(ctx->curr, ' ', ctx->cfg->num_chars);
ctx->cfg->update(ctx);
return 0;
}

new_msg = devm_kmalloc(&ctx->pdev->dev, count + 1, GFP_KERNEL);
if (!new_msg)
return -ENOMEM;

memcpy(new_msg, msg, count);
new_msg[count] = 0;

if (ctx->message)
devm_kfree(&ctx->pdev->dev, ctx->message);

ctx->message = new_msg;
ctx->message_len = count;
ctx->scroll_pos = 0;

/* update the LCD */
img_ascii_lcd_scroll(&ctx->timer);
kn = kernfs_create_link(parent->sd, "message", kn);
if (!kn)
return -ENOENT;

return 0;
}

/**
* message_show() - read message via sysfs
* @dev: the LCD device
* @attr: the LCD message attribute
* @buf: the buffer to read the message into
*
* Read the current message being displayed or scrolled across the LCD display
* into @buf, for reads from sysfs.
*
* Return: the number of characters written to @buf
*/
static ssize_t message_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);

return sysfs_emit(buf, "%s\n", ctx->message);
}

/**
* message_store() - write a new message via sysfs
* @dev: the LCD device
* @attr: the LCD message attribute
* @buf: the buffer containing the new message
* @count: the size of the message in @buf
*
* Write a new message to display or scroll across the LCD display from sysfs.
*
* Return: the size of the message on success, else -ERRNO
*/
static ssize_t message_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);
int err;

err = img_ascii_lcd_display(ctx, buf, count);
return err ?: count;
}

static DEVICE_ATTR_RW(message);

/**
* img_ascii_lcd_probe() - probe an LCD display device
* @pdev: the LCD platform device
Expand Down Expand Up @@ -391,29 +269,22 @@ static int img_ascii_lcd_probe(struct platform_device *pdev)
return PTR_ERR(ctx->base);
}

ctx->pdev = pdev;
ctx->cfg = cfg;
ctx->message = NULL;
ctx->scroll_pos = 0;
ctx->scroll_rate = HZ / 2;

/* initialise a timer for scrolling the message */
timer_setup(&ctx->timer, img_ascii_lcd_scroll, 0);

platform_set_drvdata(pdev, ctx);

/* display a default message */
err = img_ascii_lcd_display(ctx, "Linux " UTS_RELEASE " ", -1);
err = linedisp_register(&ctx->linedisp, dev, cfg->num_chars, ctx->curr,
cfg->update);
if (err)
goto out_del_timer;
return err;

err = device_create_file(dev, &dev_attr_message);
/* for backwards compatibility */
err = sysfs_create_file_link(&dev->kobj, &ctx->linedisp.dev.kobj,
"message");
if (err)
goto out_del_timer;
goto err_unregister;

platform_set_drvdata(pdev, ctx);
return 0;
out_del_timer:
del_timer_sync(&ctx->timer);

err_unregister:
linedisp_unregister(&ctx->linedisp);
return err;
}

Expand All @@ -430,8 +301,8 @@ static int img_ascii_lcd_remove(struct platform_device *pdev)
{
struct img_ascii_lcd_ctx *ctx = platform_get_drvdata(pdev);

device_remove_file(&pdev->dev, &dev_attr_message);
del_timer_sync(&ctx->timer);
sysfs_remove_link(&pdev->dev.kobj, "message");
linedisp_unregister(&ctx->linedisp);
return 0;
}

Expand Down

0 comments on commit 5505aed

Please sign in to comment.