Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *    Filename: ks0108.c
  4 *     Version: 0.1.0
  5 * Description: ks0108 LCD Controller driver
 
  6 *     Depends: parport
  7 *
  8 *      Author: Copyright (C) Miguel Ojeda <ojeda@kernel.org>
  9 *        Date: 2006-10-31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 10 */
 11
 12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 13
 14#include <linux/init.h>
 15#include <linux/module.h>
 16#include <linux/kernel.h>
 17#include <linux/delay.h>
 
 
 18#include <linux/parport.h>
 
 19#include <linux/ks0108.h>
 20
 21#define KS0108_NAME "ks0108"
 22
 23/*
 24 * Module Parameters
 25 */
 26
 27static unsigned int ks0108_port = CONFIG_KS0108_PORT;
 28module_param(ks0108_port, uint, 0444);
 29MODULE_PARM_DESC(ks0108_port, "Parallel port where the LCD is connected");
 30
 31static unsigned int ks0108_delay = CONFIG_KS0108_DELAY;
 32module_param(ks0108_delay, uint, 0444);
 33MODULE_PARM_DESC(ks0108_delay, "Delay between each control writing (microseconds)");
 34
 35/*
 36 * Device
 37 */
 38
 39static struct parport *ks0108_parport;
 40static struct pardevice *ks0108_pardevice;
 41
 42/*
 43 * ks0108 Exported Commands (don't lock)
 44 *
 45 *   You _should_ lock in the top driver: This functions _should not_
 46 *   get race conditions in any way. Locking for each byte here would be
 47 *   so slow and useless.
 48 *
 49 *   There are not bit definitions because they are not flags,
 50 *   just arbitrary combinations defined by the documentation for each
 51 *   function in the ks0108 LCD controller. If you want to know what means
 52 *   a specific combination, look at the function's name.
 53 *
 54 *   The ks0108_writecontrol bits need to be reverted ^(0,1,3) because
 55 *   the parallel port also revert them using a "not" logic gate.
 56 */
 57
 58#define bit(n) (((unsigned char)1)<<(n))
 59
 60void ks0108_writedata(unsigned char byte)
 61{
 62	parport_write_data(ks0108_parport, byte);
 63}
 64
 65void ks0108_writecontrol(unsigned char byte)
 66{
 67	udelay(ks0108_delay);
 68	parport_write_control(ks0108_parport, byte ^ (bit(0) | bit(1) | bit(3)));
 69}
 70
 71void ks0108_displaystate(unsigned char state)
 72{
 73	ks0108_writedata((state ? bit(0) : 0) | bit(1) | bit(2) | bit(3) | bit(4) | bit(5));
 74}
 75
 76void ks0108_startline(unsigned char startline)
 77{
 78	ks0108_writedata(min_t(unsigned char, startline, 63) | bit(6) |
 79			 bit(7));
 80}
 81
 82void ks0108_address(unsigned char address)
 83{
 84	ks0108_writedata(min_t(unsigned char, address, 63) | bit(6));
 85}
 86
 87void ks0108_page(unsigned char page)
 88{
 89	ks0108_writedata(min_t(unsigned char, page, 7) | bit(3) | bit(4) |
 90			 bit(5) | bit(7));
 91}
 92
 93EXPORT_SYMBOL_GPL(ks0108_writedata);
 94EXPORT_SYMBOL_GPL(ks0108_writecontrol);
 95EXPORT_SYMBOL_GPL(ks0108_displaystate);
 96EXPORT_SYMBOL_GPL(ks0108_startline);
 97EXPORT_SYMBOL_GPL(ks0108_address);
 98EXPORT_SYMBOL_GPL(ks0108_page);
 99
100/*
101 * Is the module inited?
102 */
103
104static unsigned char ks0108_inited;
105unsigned char ks0108_isinited(void)
106{
107	return ks0108_inited;
108}
109EXPORT_SYMBOL_GPL(ks0108_isinited);
110
111static void ks0108_parport_attach(struct parport *port)
 
 
 
 
112{
113	struct pardev_cb ks0108_cb;
 
114
115	if (port->base != ks0108_port)
116		return;
 
 
 
 
117
118	memset(&ks0108_cb, 0, sizeof(ks0108_cb));
119	ks0108_cb.flags = PARPORT_DEV_EXCL;
120	ks0108_pardevice = parport_register_dev_model(port, KS0108_NAME,
121						      &ks0108_cb, 0);
122	if (!ks0108_pardevice) {
123		pr_err("ERROR: parport didn't register new device\n");
124		return;
125	}
126	if (parport_claim(ks0108_pardevice)) {
127		pr_err("could not claim access to parport %i. Aborting.\n",
128		       ks0108_port);
129		goto err_unreg_device;
 
 
 
130	}
131
132	ks0108_parport = port;
133	ks0108_inited = 1;
134	return;
135
136err_unreg_device:
137	parport_unregister_device(ks0108_pardevice);
138	ks0108_pardevice = NULL;
 
 
139}
140
141static void ks0108_parport_detach(struct parport *port)
142{
143	if (port->base != ks0108_port)
144		return;
145
146	if (!ks0108_pardevice) {
147		pr_err("%s: already unregistered.\n", KS0108_NAME);
148		return;
149	}
150
151	parport_release(ks0108_pardevice);
152	parport_unregister_device(ks0108_pardevice);
153	ks0108_pardevice = NULL;
154	ks0108_parport = NULL;
155}
156
157/*
158 * Module Init & Exit
159 */
160
161static struct parport_driver ks0108_parport_driver = {
162	.name = "ks0108",
163	.match_port = ks0108_parport_attach,
164	.detach = ks0108_parport_detach,
165	.devmodel = true,
166};
167module_parport_driver(ks0108_parport_driver);
168
169MODULE_LICENSE("GPL v2");
170MODULE_AUTHOR("Miguel Ojeda <ojeda@kernel.org>");
171MODULE_DESCRIPTION("ks0108 LCD Controller driver");
172
v3.1
 
  1/*
  2 *    Filename: ks0108.c
  3 *     Version: 0.1.0
  4 * Description: ks0108 LCD Controller driver
  5 *     License: GPLv2
  6 *     Depends: parport
  7 *
  8 *      Author: Copyright (C) Miguel Ojeda Sandonis
  9 *        Date: 2006-10-31
 10 *
 11 *  This program is free software; you can redistribute it and/or modify
 12 *  it under the terms of the GNU General Public License version 2 as
 13 *  published by the Free Software Foundation.
 14 *
 15 *  This program is distributed in the hope that it will be useful,
 16 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 *  GNU General Public License for more details.
 19 *
 20 *  You should have received a copy of the GNU General Public License
 21 *  along with this program; if not, write to the Free Software
 22 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 *
 24 */
 25
 
 
 26#include <linux/init.h>
 27#include <linux/module.h>
 28#include <linux/kernel.h>
 29#include <linux/delay.h>
 30#include <linux/fs.h>
 31#include <linux/io.h>
 32#include <linux/parport.h>
 33#include <linux/uaccess.h>
 34#include <linux/ks0108.h>
 35
 36#define KS0108_NAME "ks0108"
 37
 38/*
 39 * Module Parameters
 40 */
 41
 42static unsigned int ks0108_port = CONFIG_KS0108_PORT;
 43module_param(ks0108_port, uint, S_IRUGO);
 44MODULE_PARM_DESC(ks0108_port, "Parallel port where the LCD is connected");
 45
 46static unsigned int ks0108_delay = CONFIG_KS0108_DELAY;
 47module_param(ks0108_delay, uint, S_IRUGO);
 48MODULE_PARM_DESC(ks0108_delay, "Delay between each control writing (microseconds)");
 49
 50/*
 51 * Device
 52 */
 53
 54static struct parport *ks0108_parport;
 55static struct pardevice *ks0108_pardevice;
 56
 57/*
 58 * ks0108 Exported Commands (don't lock)
 59 *
 60 *   You _should_ lock in the top driver: This functions _should not_
 61 *   get race conditions in any way. Locking for each byte here would be
 62 *   so slow and useless.
 63 *
 64 *   There are not bit definitions because they are not flags,
 65 *   just arbitrary combinations defined by the documentation for each
 66 *   function in the ks0108 LCD controller. If you want to know what means
 67 *   a specific combination, look at the function's name.
 68 *
 69 *   The ks0108_writecontrol bits need to be reverted ^(0,1,3) because
 70 *   the parallel port also revert them using a "not" logic gate.
 71 */
 72
 73#define bit(n) (((unsigned char)1)<<(n))
 74
 75void ks0108_writedata(unsigned char byte)
 76{
 77	parport_write_data(ks0108_parport, byte);
 78}
 79
 80void ks0108_writecontrol(unsigned char byte)
 81{
 82	udelay(ks0108_delay);
 83	parport_write_control(ks0108_parport, byte ^ (bit(0) | bit(1) | bit(3)));
 84}
 85
 86void ks0108_displaystate(unsigned char state)
 87{
 88	ks0108_writedata((state ? bit(0) : 0) | bit(1) | bit(2) | bit(3) | bit(4) | bit(5));
 89}
 90
 91void ks0108_startline(unsigned char startline)
 92{
 93	ks0108_writedata(min(startline,(unsigned char)63) | bit(6) | bit(7));
 
 94}
 95
 96void ks0108_address(unsigned char address)
 97{
 98	ks0108_writedata(min(address,(unsigned char)63) | bit(6));
 99}
100
101void ks0108_page(unsigned char page)
102{
103	ks0108_writedata(min(page,(unsigned char)7) | bit(3) | bit(4) | bit(5) | bit(7));
 
104}
105
106EXPORT_SYMBOL_GPL(ks0108_writedata);
107EXPORT_SYMBOL_GPL(ks0108_writecontrol);
108EXPORT_SYMBOL_GPL(ks0108_displaystate);
109EXPORT_SYMBOL_GPL(ks0108_startline);
110EXPORT_SYMBOL_GPL(ks0108_address);
111EXPORT_SYMBOL_GPL(ks0108_page);
112
113/*
114 * Is the module inited?
115 */
116
117static unsigned char ks0108_inited;
118unsigned char ks0108_isinited(void)
119{
120	return ks0108_inited;
121}
122EXPORT_SYMBOL_GPL(ks0108_isinited);
123
124/*
125 * Module Init & Exit
126 */
127
128static int __init ks0108_init(void)
129{
130	int result;
131	int ret = -EINVAL;
132
133	ks0108_parport = parport_find_base(ks0108_port);
134	if (ks0108_parport == NULL) {
135		printk(KERN_ERR KS0108_NAME ": ERROR: "
136			"parport didn't find %i port\n", ks0108_port);
137		goto none;
138	}
139
140	ks0108_pardevice = parport_register_device(ks0108_parport, KS0108_NAME,
141		NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
142	if (ks0108_pardevice == NULL) {
143		printk(KERN_ERR KS0108_NAME ": ERROR: "
144			"parport didn't register new device\n");
145		goto none;
 
146	}
147
148	result = parport_claim(ks0108_pardevice);
149	if (result != 0) {
150		printk(KERN_ERR KS0108_NAME ": ERROR: "
151			"can't claim %i parport, maybe in use\n", ks0108_port);
152		ret = result;
153		goto registered;
154	}
155
 
156	ks0108_inited = 1;
157	return 0;
158
159registered:
160	parport_unregister_device(ks0108_pardevice);
161
162none:
163	return ret;
164}
165
166static void __exit ks0108_exit(void)
167{
 
 
 
 
 
 
 
 
168	parport_release(ks0108_pardevice);
169	parport_unregister_device(ks0108_pardevice);
 
 
170}
171
172module_init(ks0108_init);
173module_exit(ks0108_exit);
 
 
 
 
 
 
 
 
 
174
175MODULE_LICENSE("GPL v2");
176MODULE_AUTHOR("Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>");
177MODULE_DESCRIPTION("ks0108 LCD Controller driver");
178