Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Filename: ks0108.c |
| 3 | * Version: 0.1.0 |
| 4 | * Description: ks0108 LCD Controller driver |
| 5 | * License: GPLv2 |
| 6 | * Depends: parport |
| 7 | * |
Miguel Ojeda | 450c622 | 2008-07-04 09:59:33 -0700 | [diff] [blame] | 8 | * Author: Copyright (C) Miguel Ojeda Sandonis |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 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 | |
Sudip Mukherjee | 7faad1d | 2015-07-20 17:27:22 +0530 | [diff] [blame] | 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 27 | |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 28 | #include <linux/init.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/io.h> |
| 34 | #include <linux/parport.h> |
| 35 | #include <linux/uaccess.h> |
| 36 | #include <linux/ks0108.h> |
| 37 | |
| 38 | #define KS0108_NAME "ks0108" |
| 39 | |
| 40 | /* |
| 41 | * Module Parameters |
| 42 | */ |
| 43 | |
| 44 | static unsigned int ks0108_port = CONFIG_KS0108_PORT; |
| 45 | module_param(ks0108_port, uint, S_IRUGO); |
| 46 | MODULE_PARM_DESC(ks0108_port, "Parallel port where the LCD is connected"); |
| 47 | |
| 48 | static unsigned int ks0108_delay = CONFIG_KS0108_DELAY; |
| 49 | module_param(ks0108_delay, uint, S_IRUGO); |
| 50 | MODULE_PARM_DESC(ks0108_delay, "Delay between each control writing (microseconds)"); |
| 51 | |
| 52 | /* |
| 53 | * Device |
| 54 | */ |
| 55 | |
| 56 | static struct parport *ks0108_parport; |
| 57 | static struct pardevice *ks0108_pardevice; |
| 58 | |
| 59 | /* |
| 60 | * ks0108 Exported Commands (don't lock) |
| 61 | * |
| 62 | * You _should_ lock in the top driver: This functions _should not_ |
| 63 | * get race conditions in any way. Locking for each byte here would be |
| 64 | * so slow and useless. |
| 65 | * |
| 66 | * There are not bit definitions because they are not flags, |
| 67 | * just arbitrary combinations defined by the documentation for each |
| 68 | * function in the ks0108 LCD controller. If you want to know what means |
| 69 | * a specific combination, look at the function's name. |
| 70 | * |
| 71 | * The ks0108_writecontrol bits need to be reverted ^(0,1,3) because |
| 72 | * the parallel port also revert them using a "not" logic gate. |
| 73 | */ |
| 74 | |
| 75 | #define bit(n) (((unsigned char)1)<<(n)) |
| 76 | |
| 77 | void ks0108_writedata(unsigned char byte) |
| 78 | { |
| 79 | parport_write_data(ks0108_parport, byte); |
| 80 | } |
| 81 | |
| 82 | void ks0108_writecontrol(unsigned char byte) |
| 83 | { |
| 84 | udelay(ks0108_delay); |
| 85 | parport_write_control(ks0108_parport, byte ^ (bit(0) | bit(1) | bit(3))); |
| 86 | } |
| 87 | |
| 88 | void ks0108_displaystate(unsigned char state) |
| 89 | { |
| 90 | ks0108_writedata((state ? bit(0) : 0) | bit(1) | bit(2) | bit(3) | bit(4) | bit(5)); |
| 91 | } |
| 92 | |
| 93 | void ks0108_startline(unsigned char startline) |
| 94 | { |
Sudip Mukherjee | c9efdbe | 2015-07-20 17:27:23 +0530 | [diff] [blame^] | 95 | ks0108_writedata(min_t(unsigned char, startline, 63) | bit(6) | |
| 96 | bit(7)); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void ks0108_address(unsigned char address) |
| 100 | { |
Sudip Mukherjee | c9efdbe | 2015-07-20 17:27:23 +0530 | [diff] [blame^] | 101 | ks0108_writedata(min_t(unsigned char, address, 63) | bit(6)); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void ks0108_page(unsigned char page) |
| 105 | { |
Sudip Mukherjee | c9efdbe | 2015-07-20 17:27:23 +0530 | [diff] [blame^] | 106 | ks0108_writedata(min_t(unsigned char, page, 7) | bit(3) | bit(4) | |
| 107 | bit(5) | bit(7)); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | EXPORT_SYMBOL_GPL(ks0108_writedata); |
| 111 | EXPORT_SYMBOL_GPL(ks0108_writecontrol); |
| 112 | EXPORT_SYMBOL_GPL(ks0108_displaystate); |
| 113 | EXPORT_SYMBOL_GPL(ks0108_startline); |
| 114 | EXPORT_SYMBOL_GPL(ks0108_address); |
| 115 | EXPORT_SYMBOL_GPL(ks0108_page); |
| 116 | |
| 117 | /* |
Miguel Ojeda | 34173a4 | 2007-02-20 13:58:00 -0800 | [diff] [blame] | 118 | * Is the module inited? |
| 119 | */ |
| 120 | |
| 121 | static unsigned char ks0108_inited; |
| 122 | unsigned char ks0108_isinited(void) |
| 123 | { |
| 124 | return ks0108_inited; |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(ks0108_isinited); |
| 127 | |
| 128 | /* |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 129 | * Module Init & Exit |
| 130 | */ |
| 131 | |
| 132 | static int __init ks0108_init(void) |
| 133 | { |
| 134 | int result; |
| 135 | int ret = -EINVAL; |
| 136 | |
| 137 | ks0108_parport = parport_find_base(ks0108_port); |
| 138 | if (ks0108_parport == NULL) { |
Sudip Mukherjee | 7faad1d | 2015-07-20 17:27:22 +0530 | [diff] [blame] | 139 | pr_err("ERROR: parport didn't find %i port\n", ks0108_port); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 140 | goto none; |
| 141 | } |
| 142 | |
| 143 | ks0108_pardevice = parport_register_device(ks0108_parport, KS0108_NAME, |
| 144 | NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL); |
Sudip Mukherjee | bab383d | 2015-07-20 17:27:21 +0530 | [diff] [blame] | 145 | parport_put_port(ks0108_parport); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 146 | if (ks0108_pardevice == NULL) { |
Sudip Mukherjee | 7faad1d | 2015-07-20 17:27:22 +0530 | [diff] [blame] | 147 | pr_err("ERROR: parport didn't register new device\n"); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 148 | goto none; |
| 149 | } |
| 150 | |
| 151 | result = parport_claim(ks0108_pardevice); |
| 152 | if (result != 0) { |
Sudip Mukherjee | 7faad1d | 2015-07-20 17:27:22 +0530 | [diff] [blame] | 153 | pr_err("ERROR: can't claim %i parport, maybe in use\n", |
| 154 | ks0108_port); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 155 | ret = result; |
| 156 | goto registered; |
| 157 | } |
| 158 | |
Miguel Ojeda | 34173a4 | 2007-02-20 13:58:00 -0800 | [diff] [blame] | 159 | ks0108_inited = 1; |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 160 | return 0; |
| 161 | |
| 162 | registered: |
| 163 | parport_unregister_device(ks0108_pardevice); |
| 164 | |
| 165 | none: |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static void __exit ks0108_exit(void) |
| 170 | { |
| 171 | parport_release(ks0108_pardevice); |
| 172 | parport_unregister_device(ks0108_pardevice); |
| 173 | } |
| 174 | |
| 175 | module_init(ks0108_init); |
| 176 | module_exit(ks0108_exit); |
| 177 | |
| 178 | MODULE_LICENSE("GPL v2"); |
Miguel Ojeda | 450c622 | 2008-07-04 09:59:33 -0700 | [diff] [blame] | 179 | MODULE_AUTHOR("Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>"); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 180 | MODULE_DESCRIPTION("ks0108 LCD Controller driver"); |
| 181 | |