xref: /linux/drivers/auxdisplay/seg-led-gpio.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for a 7-segment LED display
4  *
5  * The decimal point LED present on some devices is currently not
6  * supported.
7  *
8  * Copyright (C) Allied Telesis Labs
9  */
10 
11 #include <linux/bitmap.h>
12 #include <linux/container_of.h>
13 #include <linux/errno.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/map_to_7segment.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/types.h>
19 #include <linux/workqueue.h>
20 
21 #include "line-display.h"
22 
23 struct seg_led_priv {
24 	struct linedisp linedisp;
25 	struct delayed_work work;
26 	struct gpio_descs *segment_gpios;
27 };
28 
29 static void seg_led_update(struct work_struct *work)
30 {
31 	struct seg_led_priv *priv = container_of(work, struct seg_led_priv, work.work);
32 	struct linedisp *linedisp = &priv->linedisp;
33 	struct linedisp_map *map = linedisp->map;
34 	DECLARE_BITMAP(values, 8) = { };
35 
36 	bitmap_set_value8(values, map_to_seg7(&map->map.seg7, linedisp->buf[0]), 0);
37 
38 	gpiod_multi_set_value_cansleep(priv->segment_gpios, values);
39 }
40 
41 static int seg_led_linedisp_get_map_type(struct linedisp *linedisp)
42 {
43 	struct seg_led_priv *priv = container_of(linedisp, struct seg_led_priv, linedisp);
44 
45 	INIT_DELAYED_WORK(&priv->work, seg_led_update);
46 	return LINEDISP_MAP_SEG7;
47 }
48 
49 static void seg_led_linedisp_update(struct linedisp *linedisp)
50 {
51 	struct seg_led_priv *priv = container_of(linedisp, struct seg_led_priv, linedisp);
52 
53 	schedule_delayed_work(&priv->work, 0);
54 }
55 
56 static const struct linedisp_ops seg_led_linedisp_ops = {
57 	.get_map_type = seg_led_linedisp_get_map_type,
58 	.update = seg_led_linedisp_update,
59 };
60 
61 static int seg_led_probe(struct platform_device *pdev)
62 {
63 	struct seg_led_priv *priv;
64 	struct device *dev = &pdev->dev;
65 
66 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
67 	if (!priv)
68 		return -ENOMEM;
69 
70 	platform_set_drvdata(pdev, priv);
71 
72 	priv->segment_gpios = devm_gpiod_get_array(dev, "segment", GPIOD_OUT_LOW);
73 	if (IS_ERR(priv->segment_gpios))
74 		return PTR_ERR(priv->segment_gpios);
75 
76 	if (priv->segment_gpios->ndescs < 7 || priv->segment_gpios->ndescs > 8)
77 		return -EINVAL;
78 
79 	return linedisp_register(&priv->linedisp, dev, 1, &seg_led_linedisp_ops);
80 }
81 
82 static void seg_led_remove(struct platform_device *pdev)
83 {
84 	struct seg_led_priv *priv = platform_get_drvdata(pdev);
85 
86 	cancel_delayed_work_sync(&priv->work);
87 	linedisp_unregister(&priv->linedisp);
88 }
89 
90 static const struct of_device_id seg_led_of_match[] = {
91 	{ .compatible = "gpio-7-segment"},
92 	{}
93 };
94 MODULE_DEVICE_TABLE(of, seg_led_of_match);
95 
96 static struct platform_driver seg_led_driver = {
97 	.probe = seg_led_probe,
98 	.remove = seg_led_remove,
99 	.driver = {
100 		.name = "seg-led-gpio",
101 		.of_match_table = seg_led_of_match,
102 	},
103 };
104 module_platform_driver(seg_led_driver);
105 
106 MODULE_AUTHOR("Chris Packham <chris.packham@alliedtelesis.co.nz>");
107 MODULE_DESCRIPTION("7 segment LED driver");
108 MODULE_LICENSE("GPL");
109 MODULE_IMPORT_NS("LINEDISP");
110