1 // SPDX-License-Identifier: GPL-2.0
5 * Description: ks0108 LCD Controller driver
8 * Author: Copyright (C) Miguel Ojeda <ojeda@kernel.org>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
20 #include <linux/parport.h>
21 #include <linux/uaccess.h>
22 #include <linux/ks0108.h>
24 #define KS0108_NAME "ks0108"
30 static unsigned int ks0108_port = CONFIG_KS0108_PORT;
31 module_param(ks0108_port, uint, 0444);
32 MODULE_PARM_DESC(ks0108_port, "Parallel port where the LCD is connected");
34 static unsigned int ks0108_delay = CONFIG_KS0108_DELAY;
35 module_param(ks0108_delay, uint, 0444);
36 MODULE_PARM_DESC(ks0108_delay, "Delay between each control writing (microseconds)");
42 static struct parport *ks0108_parport;
43 static struct pardevice *ks0108_pardevice;
46 * ks0108 Exported Commands (don't lock)
48 * You _should_ lock in the top driver: This functions _should not_
49 * get race conditions in any way. Locking for each byte here would be
50 * so slow and useless.
52 * There are not bit definitions because they are not flags,
53 * just arbitrary combinations defined by the documentation for each
54 * function in the ks0108 LCD controller. If you want to know what means
55 * a specific combination, look at the function's name.
57 * The ks0108_writecontrol bits need to be reverted ^(0,1,3) because
58 * the parallel port also revert them using a "not" logic gate.
61 #define bit(n) (((unsigned char)1)<<(n))
63 void ks0108_writedata(unsigned char byte)
65 parport_write_data(ks0108_parport, byte);
68 void ks0108_writecontrol(unsigned char byte)
71 parport_write_control(ks0108_parport, byte ^ (bit(0) | bit(1) | bit(3)));
74 void ks0108_displaystate(unsigned char state)
76 ks0108_writedata((state ? bit(0) : 0) | bit(1) | bit(2) | bit(3) | bit(4) | bit(5));
79 void ks0108_startline(unsigned char startline)
81 ks0108_writedata(min_t(unsigned char, startline, 63) | bit(6) |
85 void ks0108_address(unsigned char address)
87 ks0108_writedata(min_t(unsigned char, address, 63) | bit(6));
90 void ks0108_page(unsigned char page)
92 ks0108_writedata(min_t(unsigned char, page, 7) | bit(3) | bit(4) |
96 EXPORT_SYMBOL_GPL(ks0108_writedata);
97 EXPORT_SYMBOL_GPL(ks0108_writecontrol);
98 EXPORT_SYMBOL_GPL(ks0108_displaystate);
99 EXPORT_SYMBOL_GPL(ks0108_startline);
100 EXPORT_SYMBOL_GPL(ks0108_address);
101 EXPORT_SYMBOL_GPL(ks0108_page);
104 * Is the module inited?
107 static unsigned char ks0108_inited;
108 unsigned char ks0108_isinited(void)
110 return ks0108_inited;
112 EXPORT_SYMBOL_GPL(ks0108_isinited);
114 static void ks0108_parport_attach(struct parport *port)
116 struct pardev_cb ks0108_cb;
118 if (port->base != ks0108_port)
121 memset(&ks0108_cb, 0, sizeof(ks0108_cb));
122 ks0108_cb.flags = PARPORT_DEV_EXCL;
123 ks0108_pardevice = parport_register_dev_model(port, KS0108_NAME,
125 if (!ks0108_pardevice) {
126 pr_err("ERROR: parport didn't register new device\n");
129 if (parport_claim(ks0108_pardevice)) {
130 pr_err("could not claim access to parport %i. Aborting.\n",
132 goto err_unreg_device;
135 ks0108_parport = port;
140 parport_unregister_device(ks0108_pardevice);
141 ks0108_pardevice = NULL;
144 static void ks0108_parport_detach(struct parport *port)
146 if (port->base != ks0108_port)
149 if (!ks0108_pardevice) {
150 pr_err("%s: already unregistered.\n", KS0108_NAME);
154 parport_release(ks0108_pardevice);
155 parport_unregister_device(ks0108_pardevice);
156 ks0108_pardevice = NULL;
157 ks0108_parport = NULL;
164 static struct parport_driver ks0108_parport_driver = {
166 .match_port = ks0108_parport_attach,
167 .detach = ks0108_parport_detach,
170 module_parport_driver(ks0108_parport_driver);
172 MODULE_LICENSE("GPL v2");
173 MODULE_AUTHOR("Miguel Ojeda <ojeda@kernel.org>");
174 MODULE_DESCRIPTION("ks0108 LCD Controller driver");