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