blob: 8ae624a514c32ae3593d3de7a6311a45befa647f [file] [log] [blame]
George Hungd9f9d7e2020-07-16 22:03:39 +08001From 2d46e81d9bbb624695bac8400a4ac41b7f116ec4 Mon Sep 17 00:00:00 2001
2From: Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>
3Date: Fri, 27 Jul 2018 12:34:54 -0700
4Subject: [PATCH 2/3] drivers: misc: Character device driver for seven segment
5 display
6
7Character device driver which implements the user-space
8API for letting a user write to two 7-segment displays including
9any conversion methods necessary to map the user input
10to two 7-segment displays.
11
12Signed-off-by: Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>
13Signed-off-by: Kun Yi <kunyi@google.com>
14Signed-off-by: Benjamin Fair <benjaminfair@google.com>
15---
16 drivers/misc/Kconfig | 8 ++
17 drivers/misc/Makefile | 1 +
18 drivers/misc/seven_seg_disp.c | 201 ++++++++++++++++++++++++++++++++++
19 drivers/misc/seven_seg_disp.h | 34 ++++++
20 4 files changed, 244 insertions(+)
21 create mode 100644 drivers/misc/seven_seg_disp.c
22 create mode 100644 drivers/misc/seven_seg_disp.h
23
24diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
25index 9d96469fb41c..d443de886346 100644
26--- a/drivers/misc/Kconfig
27+++ b/drivers/misc/Kconfig
28@@ -473,6 +473,14 @@ config VEXPRESS_SYSCFG
29 bus. System Configuration interface is one of the possible means
30 of generating transactions on this bus.
31
32+config SEVEN_SEGMENT_DISPLAY
33+ tristate "Character driver for seven segment display support"
34+ help
35+ Character device driver which implements the user-space
36+ API for letting a user write to two 7-segment displays including
37+ any conversion methods necessary to map the user input
38+ to two 7-segment displays.
39+
40 config PCI_ENDPOINT_TEST
41 depends on PCI
42 select CRC32
43
44diff --git a/drivers/misc/seven_seg_disp.c b/drivers/misc/seven_seg_disp.c
45new file mode 100644
46index 000000000000..db1c571f68e5
47--- /dev/null
48+++ b/drivers/misc/seven_seg_disp.c
49@@ -0,0 +1,201 @@
50+/*
51+ * Copyright (c) 2016 Google, Inc
52+ *
53+ * This program is free software; you can redistribute it and/or modify
54+ * it under the terms of the GNU General Public License version 2 or later as
55+ * published by the Free Software Foundation.
56+ */
57+
58+#include <linux/module.h>
59+#include <linux/version.h>
60+#include <linux/kernel.h>
61+#include <linux/types.h>
62+#include <linux/kdev_t.h>
63+#include <linux/fs.h>
64+#include <linux/uaccess.h>
65+#include <linux/ctype.h>
66+#include <linux/of.h>
67+#include <linux/io.h>
68+#include <linux/slab.h>
69+#include <linux/platform_device.h>
70+
71+#include "seven_seg_disp.h"
72+
73+#define LED_DOT 0x01
74+
75+/*
76+ * 0 1 2 3 4 5 6 7 8 9 A B C D E F
77+ * _ _ _ _ _ _ _ _ _ _ _ _
78+ * | | | _| _| |_| |_ |_ | |_| |_| |_| |_ | _| |_ |_
79+ * |_| | |_ _| | _| |_| | |_| | | | |_| |_ |_| |_ |
80+ *
81+ * data[7:1] = led[a:g]
82+ */
83+const u8 seven_seg_bits[] = {
84+ 0xFC, 0x60, 0xDA, 0xF2, 0x66, 0xB6, 0xBE, 0xE0,
85+ 0xFE, 0xF6, 0xEE, 0x3E, 0x9C, 0x7A, 0x9E, 0x8E
86+ };
87+
88+/*
89+ * 0 1 2 3 4 5 6 7 8 9 A B C D E F
90+ * _ _ _ _ _
91+ * | |_ |_| |_ _ _ _ _ _ _ _ |_ _| _| | |
92+ * |_ |_ | | _| |_| |_| | |
93+ *
94+ * data[7:1] = led[a:g]
95+ */
96+const u8 special_seven_seg_bits[] = {
97+ 0x00, 0x9C, 0x1E, 0xCE, 0x8E, 0x02, 0x02, 0x02,
98+ 0x02, 0x02, 0x02, 0x02, 0xB6, 0x7A, 0x7A, 0xEC
99+ };
100+
101+static dev_t seven_seg_devno;
102+static struct class *seven_seg_disp_class;
103+
104+static int seven_seg_disp_open(struct inode *inode, struct file *filp)
105+{
106+ struct seven_seg_disp_dev *disp_dev;
107+
108+ disp_dev = container_of(inode->i_cdev,
109+ struct seven_seg_disp_dev, cdev);
110+ filp->private_data = disp_dev;
111+ return 0;
112+}
113+
114+static int seven_seg_disp_close(struct inode *inode, struct file *filp)
115+{
116+ filp->private_data = NULL;
117+ return 0;
118+}
119+
120+static ssize_t seven_seg_disp_read(struct file *filp, char __user *buf, size_t
121+ len, loff_t *off)
122+{
123+ struct seven_seg_disp_dev *disp_dev = filp->private_data;
124+
125+ if (disp_dev->disp_data_valid)
126+ return -EINVAL;
127+
128+ if (copy_to_user(buf, disp_dev->seven_seg_disp_data_array,
129+ MAX_DISP_CHAR_SIZE) != 0) {
130+ return -EFAULT;
131+ }
132+
133+ return 0;
134+}
135+
136+static u16 convert_to_disp_data(char *buf)
137+{
138+ u8 low_display;
139+ u8 high_display;
140+ u16 led_value;
141+
142+ low_display = seven_seg_bits[hex_to_bin(buf[2])];
143+
144+ high_display = (buf[0] == '1') ?
145+ special_seven_seg_bits[hex_to_bin(buf[1])] :
146+ seven_seg_bits[hex_to_bin(buf[1])];
147+
148+ led_value = low_display | (high_display << 8);
149+ if (buf[0] == '1')
150+ led_value |= LED_DOT | (LED_DOT << 8);
151+
152+ return led_value;
153+}
154+
155+static ssize_t seven_seg_disp_write(struct file *filp, const char __user *buf,
156+ size_t len, loff_t *off)
157+{
158+ int length = len - 1;
159+ int i;
160+
161+ struct seven_seg_disp_dev *disp_dev = filp->private_data;
162+
163+ if (length != MAX_DISP_CHAR_SIZE)
164+ return -EINVAL;
165+
166+ if (copy_from_user(disp_dev->seven_seg_disp_data_array,
167+ buf, length) != 0) {
168+ return -EFAULT;
169+ }
170+
171+ for (i = 0; i < MAX_DISP_CHAR_SIZE; i++) {
172+ if (!isxdigit(disp_dev->seven_seg_disp_data_array[i]))
173+ return -EINVAL;
174+ }
175+
176+ disp_dev->current_seven_seg_disp_data = convert_to_disp_data(
177+ disp_dev->seven_seg_disp_data_array);
178+ disp_dev->disp_data_valid = true;
179+ disp_dev->update_seven_seg_data(&disp_dev->parent,
180+ disp_dev->current_seven_seg_disp_data);
181+
182+ return len;
183+}
184+
185+static const struct file_operations seven_seg_disp_fops = {
186+
187+ .owner = THIS_MODULE,
188+ .open = seven_seg_disp_open,
189+ .release = seven_seg_disp_close,
190+ .read = seven_seg_disp_read,
191+ .write = seven_seg_disp_write
192+};
193+
194+void seven_seg_rem_cdev(struct seven_seg_disp_dev *disp_dev)
195+{
196+ cdev_del(&disp_dev->cdev);
197+ device_destroy(seven_seg_disp_class, seven_seg_devno);
198+}
199+
200+int seven_seg_setup_cdev(struct seven_seg_disp_dev *disp_dev,
201+ void (*update_disp_data)(struct device *, u16 data))
202+{
203+ struct device *dev;
204+ int err;
205+
206+ dev = device_create(seven_seg_disp_class, NULL,
207+ seven_seg_devno,
208+ NULL, "seven_seg_disp_val");
209+ if (dev == NULL)
210+ return -EIO;
211+ disp_dev->dev = dev;
212+ disp_dev->update_seven_seg_data = update_disp_data;
213+ disp_dev->disp_data_valid = false;
214+
215+ cdev_init(&disp_dev->cdev, &seven_seg_disp_fops);
216+ err = cdev_add(&disp_dev->cdev, seven_seg_devno, 1);
217+ if (err)
218+ device_destroy(seven_seg_disp_class, seven_seg_devno);
219+ return err;
220+}
221+
222+static int __init seven_seg_disp_init(void)
223+{
224+ int err = alloc_chrdev_region(&seven_seg_devno, 0, 1, "disp_state");
225+
226+ if (err < 0)
227+ return err;
228+
229+ seven_seg_disp_class = class_create(THIS_MODULE, "disp_state");
230+ if (seven_seg_disp_class == NULL)
231+ goto unreg_chrdev;
232+
233+ return 0;
234+
235+unreg_chrdev:
236+ unregister_chrdev_region(seven_seg_devno, 1);
237+ return -EIO;
238+}
239+
240+static void __exit seven_seg_disp_exit(void)
241+{
242+ class_destroy(seven_seg_disp_class);
243+ unregister_chrdev_region(seven_seg_devno, 1);
244+}
245+
246+module_init(seven_seg_disp_init);
247+module_exit(seven_seg_disp_exit);
248+MODULE_LICENSE("GPL");
249+MODULE_AUTHOR("Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>");
250+MODULE_DESCRIPTION("Seven segment display character driver");
251diff --git a/drivers/misc/seven_seg_disp.h b/drivers/misc/seven_seg_disp.h
252new file mode 100644
253index 000000000000..0ebed0802747
254--- /dev/null
255+++ b/drivers/misc/seven_seg_disp.h
256@@ -0,0 +1,34 @@
257+/*
258+ * Copyright (c) 2016 Google, Inc
259+ *
260+ * This program is free software; you can redistribute it and/or modify
261+ * it under the terms of the GNU General Public License version 2 or later as
262+ * published by the Free Software Foundation.
263+ */
264+
265+#ifndef SEVEN_SEG_DISP_H
266+#define SEVEN_SEG_DISP_H
267+
268+#include <linux/device.h>
269+#include <linux/cdev.h>
270+
271+#define MAX_DISP_CHAR_SIZE 3
272+
273+#define DEFAULT_REFRESH_INTERVAL_MS 600
274+
275+struct seven_seg_disp_dev {
276+ bool disp_data_valid;
277+ u16 current_seven_seg_disp_data;
278+ char seven_seg_disp_data_array[MAX_DISP_CHAR_SIZE];
279+ struct device parent;
280+ struct device *dev;
281+ struct cdev cdev;
282+ void (*update_seven_seg_data)(struct device *, u16 data);
283+};
284+
285+int seven_seg_setup_cdev(struct seven_seg_disp_dev *disp_dev,
286+ void (*update_disp_data)(struct device *, u16 data));
287+
288+void seven_seg_rem_cdev(struct seven_seg_disp_dev *disp_dev);
289+
290+#endif
291--
2922.22.0.770.g0f2c4a37fd-goog
293