1/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
2/*
3 * Copyright (C) 2021 Glider bv
4 *
5 * Based on include/uapi/linux/map_to_7segment.h:
6
7 * Copyright (c) 2005 Henk Vergonet <Henk.Vergonet@gmail.com>
8 */
9
10#ifndef MAP_TO_14SEGMENT_H
11#define MAP_TO_14SEGMENT_H
12
13/* This file provides translation primitives and tables for the conversion
14 * of (ASCII) characters to a 14-segments notation.
15 *
16 * The 14 segment's wikipedia notation below is used as standard.
17 * See: https://en.wikipedia.org/wiki/Fourteen-segment_display
18 *
19 * Notation:	+---a---+
20 *		|\  |  /|
21 *		f h i j b
22 *		|  \|/  |
23 *		+-g1+-g2+
24 *		|  /|\  |
25 *		e k l m c
26 *		|/  |  \|
27 *		+---d---+
28 *
29 * Usage:
30 *
31 *   Register a map variable, and fill it with a character set:
32 *	static SEG14_DEFAULT_MAP(map_seg14);
33 *
34 *
35 *   Then use for conversion:
36 *	seg14 = map_to_seg14(&map_seg14, some_char);
37 *	...
38 *
39 * In device drivers it is recommended, if required, to make the char map
40 * accessible via the sysfs interface using the following scheme:
41 *
42 * static ssize_t map_seg14_show(struct device *dev,
43 *				 struct device_attribute *attr, char *buf)
44 * {
45 *	memcpy(buf, &map_seg14, sizeof(map_seg14));
46 *	return sizeof(map_seg14);
47 * }
48 * static ssize_t map_seg14_store(struct device *dev,
49 *				  struct device_attribute *attr,
50 *				  const char *buf, size_t cnt)
51 * {
52 *	if (cnt != sizeof(map_seg14))
53 *		return -EINVAL;
54 *	memcpy(&map_seg14, buf, cnt);
55 *	return cnt;
56 * }
57 * static DEVICE_ATTR_RW(map_seg14);
58 */
59#include <linux/errno.h>
60#include <linux/types.h>
61
62#include <asm/byteorder.h>
63
64#define BIT_SEG14_A		0
65#define BIT_SEG14_B		1
66#define BIT_SEG14_C		2
67#define BIT_SEG14_D		3
68#define BIT_SEG14_E		4
69#define BIT_SEG14_F		5
70#define BIT_SEG14_G1		6
71#define BIT_SEG14_G2		7
72#define BIT_SEG14_H		8
73#define BIT_SEG14_I		9
74#define BIT_SEG14_J		10
75#define BIT_SEG14_K		11
76#define BIT_SEG14_L		12
77#define BIT_SEG14_M		13
78#define BIT_SEG14_RESERVED1	14
79#define BIT_SEG14_RESERVED2	15
80
81struct seg14_conversion_map {
82	__be16 table[128];
83};
84
85static __inline__ int map_to_seg14(struct seg14_conversion_map *map, int c)
86{
87	if (c < 0 || c >= sizeof(map->table) / sizeof(map->table[0]))
88		return -EINVAL;
89
90	return __be16_to_cpu(map->table[c]);
91}
92
93#define SEG14_CONVERSION_MAP(_name, _map)	\
94	struct seg14_conversion_map _name = { .table = { _map } }
95
96/*
97 * It is recommended to use a facility that allows user space to redefine
98 * custom character sets for LCD devices. Please use a sysfs interface
99 * as described above.
100 */
101#define MAP_TO_SEG14_SYSFS_FILE	"map_seg14"
102
103/*******************************************************************************
104 * ASCII conversion table
105 ******************************************************************************/
106
107#define _SEG14(sym, a, b, c, d, e, f, g1, g2, h, j, k, l, m, n)	\
108	__cpu_to_be16( a << BIT_SEG14_A  |  b << BIT_SEG14_B  |	\
109		       c << BIT_SEG14_C  |  d << BIT_SEG14_D  |	\
110		       e << BIT_SEG14_E  |  f << BIT_SEG14_F  |	\
111		      g1 << BIT_SEG14_G1 | g2 << BIT_SEG14_G2 |	\
112		       h << BIT_SEG14_H  |  j << BIT_SEG14_I  |	\
113		       k << BIT_SEG14_J  |  l << BIT_SEG14_K  |	\
114		       m << BIT_SEG14_L  |  n << BIT_SEG14_M )
115
116#define _MAP_0_32_ASCII_SEG14_NON_PRINTABLE				\
117	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
118
119#define _MAP_33_47_ASCII_SEG14_SYMBOL				\
120	_SEG14('!', 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),	\
121	_SEG14('"', 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0),	\
122	_SEG14('#', 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0),	\
123	_SEG14('$', 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0),	\
124	_SEG14('%', 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0),	\
125	_SEG14('&', 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1),	\
126	_SEG14('\'',0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0),	\
127	_SEG14('(', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1),	\
128	_SEG14(')', 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0),	\
129	_SEG14('*', 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1),	\
130	_SEG14('+', 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0),	\
131	_SEG14(',', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0),	\
132	_SEG14('-', 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0),	\
133	_SEG14('.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),	\
134	_SEG14('/', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0),
135
136#define _MAP_48_57_ASCII_SEG14_NUMERIC				\
137	_SEG14('0', 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0),	\
138	_SEG14('1', 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0),	\
139	_SEG14('2', 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0),	\
140	_SEG14('3', 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0),	\
141	_SEG14('4', 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0),	\
142	_SEG14('5', 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1),	\
143	_SEG14('6', 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0),	\
144	_SEG14('7', 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0),	\
145	_SEG14('8', 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0),	\
146	_SEG14('9', 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0),
147
148#define _MAP_58_64_ASCII_SEG14_SYMBOL				\
149	_SEG14(':', 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0),	\
150	_SEG14(';', 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0),	\
151	_SEG14('<', 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1),	\
152	_SEG14('=', 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0),	\
153	_SEG14('>', 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0),	\
154	_SEG14('?', 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0),	\
155	_SEG14('@', 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0),
156
157#define _MAP_65_90_ASCII_SEG14_ALPHA_UPPER			\
158	_SEG14('A', 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0),	\
159	_SEG14('B', 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0),	\
160	_SEG14('C', 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),	\
161	_SEG14('D', 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0),	\
162	_SEG14('E', 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0),	\
163	_SEG14('F', 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0),	\
164	_SEG14('G', 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0),	\
165	_SEG14('H', 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0),	\
166	_SEG14('I', 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0),	\
167	_SEG14('J', 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),	\
168	_SEG14('K', 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1),	\
169	_SEG14('L', 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),	\
170	_SEG14('M', 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0),	\
171	_SEG14('N', 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1),	\
172	_SEG14('O', 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),	\
173	_SEG14('P', 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0),	\
174	_SEG14('Q', 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1),	\
175	_SEG14('R', 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1),	\
176	_SEG14('S', 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0),	\
177	_SEG14('T', 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0),	\
178	_SEG14('U', 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),	\
179	_SEG14('V', 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0),	\
180	_SEG14('W', 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1),	\
181	_SEG14('X', 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1),	\
182	_SEG14('Y', 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0),	\
183	_SEG14('Z', 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0),
184
185#define _MAP_91_96_ASCII_SEG14_SYMBOL				\
186	_SEG14('[', 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),	\
187	_SEG14('\\',0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1),	\
188	_SEG14(']', 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),	\
189	_SEG14('^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1),	\
190	_SEG14('_', 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),	\
191	_SEG14('`', 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0),
192
193#define _MAP_97_122_ASCII_SEG14_ALPHA_LOWER			\
194	_SEG14('a', 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0),	\
195	_SEG14('b', 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1),	\
196	_SEG14('c', 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0),	\
197	_SEG14('d', 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0),	\
198	_SEG14('e', 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0),	\
199	_SEG14('f', 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0),	\
200	_SEG14('g', 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0),	\
201	_SEG14('h', 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0),	\
202	_SEG14('i', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0),	\
203	_SEG14('j', 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0),	\
204	_SEG14('k', 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1),	\
205	_SEG14('l', 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0),	\
206	_SEG14('m', 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0),	\
207	_SEG14('n', 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0),	\
208	_SEG14('o', 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0),	\
209	_SEG14('p', 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0),	\
210	_SEG14('q', 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0),	\
211	_SEG14('r', 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0),	\
212	_SEG14('s', 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1),	\
213	_SEG14('t', 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0),	\
214	_SEG14('u', 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0),	\
215	_SEG14('v', 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0),	\
216	_SEG14('w', 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1),	\
217	_SEG14('x', 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1),	\
218	_SEG14('y', 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0),	\
219	_SEG14('z', 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0),
220
221#define _MAP_123_126_ASCII_SEG14_SYMBOL				\
222	_SEG14('{', 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0),	\
223	_SEG14('|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0),	\
224	_SEG14('}', 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1),	\
225	_SEG14('~', 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0),
226
227/* Maps */
228#define MAP_ASCII14SEG_ALPHANUM			\
229	_MAP_0_32_ASCII_SEG14_NON_PRINTABLE	\
230	_MAP_33_47_ASCII_SEG14_SYMBOL		\
231	_MAP_48_57_ASCII_SEG14_NUMERIC		\
232	_MAP_58_64_ASCII_SEG14_SYMBOL		\
233	_MAP_65_90_ASCII_SEG14_ALPHA_UPPER	\
234	_MAP_91_96_ASCII_SEG14_SYMBOL		\
235	_MAP_97_122_ASCII_SEG14_ALPHA_LOWER	\
236	_MAP_123_126_ASCII_SEG14_SYMBOL
237
238#define SEG14_DEFAULT_MAP(_name)		\
239	SEG14_CONVERSION_MAP(_name, MAP_ASCII14SEG_ALPHANUM)
240
241#endif	/* MAP_TO_14SEGMENT_H */
242