Miguel Ojeda | 351f683b | 2018-02-17 20:33:13 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Filename: ks0108.c |
| 4 | * Version: 0.1.0 |
| 5 | * Description: ks0108 LCD Controller driver |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 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 |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
Sudip Mukherjee | 7faad1d | 2015-07-20 17:27:22 +0530 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/parport.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/ks0108.h> |
| 23 | |
| 24 | #define KS0108_NAME "ks0108" |
| 25 | |
| 26 | /* |
| 27 | * Module Parameters |
| 28 | */ |
| 29 | |
| 30 | static unsigned int ks0108_port = CONFIG_KS0108_PORT; |
| 31 | module_param(ks0108_port, uint, S_IRUGO); |
| 32 | MODULE_PARM_DESC(ks0108_port, "Parallel port where the LCD is connected"); |
| 33 | |
| 34 | static unsigned int ks0108_delay = CONFIG_KS0108_DELAY; |
| 35 | module_param(ks0108_delay, uint, S_IRUGO); |
| 36 | MODULE_PARM_DESC(ks0108_delay, "Delay between each control writing (microseconds)"); |
| 37 | |
| 38 | /* |
| 39 | * Device |
| 40 | */ |
| 41 | |
| 42 | static struct parport *ks0108_parport; |
| 43 | static struct pardevice *ks0108_pardevice; |
| 44 | |
| 45 | /* |
| 46 | * ks0108 Exported Commands (don't lock) |
| 47 | * |
| 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. |
| 51 | * |
| 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. |
| 56 | * |
| 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. |
| 59 | */ |
| 60 | |
| 61 | #define bit(n) (((unsigned char)1)<<(n)) |
| 62 | |
| 63 | void ks0108_writedata(unsigned char byte) |
| 64 | { |
| 65 | parport_write_data(ks0108_parport, byte); |
| 66 | } |
| 67 | |
| 68 | void ks0108_writecontrol(unsigned char byte) |
| 69 | { |
| 70 | udelay(ks0108_delay); |
| 71 | parport_write_control(ks0108_parport, byte ^ (bit(0) | bit(1) | bit(3))); |
| 72 | } |
| 73 | |
| 74 | void ks0108_displaystate(unsigned char state) |
| 75 | { |
| 76 | ks0108_writedata((state ? bit(0) : 0) | bit(1) | bit(2) | bit(3) | bit(4) | bit(5)); |
| 77 | } |
| 78 | |
| 79 | void ks0108_startline(unsigned char startline) |
| 80 | { |
Sudip Mukherjee | c9efdbe | 2015-07-20 17:27:23 +0530 | [diff] [blame] | 81 | ks0108_writedata(min_t(unsigned char, startline, 63) | bit(6) | |
| 82 | bit(7)); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void ks0108_address(unsigned char address) |
| 86 | { |
Sudip Mukherjee | c9efdbe | 2015-07-20 17:27:23 +0530 | [diff] [blame] | 87 | ks0108_writedata(min_t(unsigned char, address, 63) | bit(6)); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void ks0108_page(unsigned char page) |
| 91 | { |
Sudip Mukherjee | c9efdbe | 2015-07-20 17:27:23 +0530 | [diff] [blame] | 92 | ks0108_writedata(min_t(unsigned char, page, 7) | bit(3) | bit(4) | |
| 93 | bit(5) | bit(7)); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 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); |
| 102 | |
| 103 | /* |
Miguel Ojeda | 34173a4 | 2007-02-20 13:58:00 -0800 | [diff] [blame] | 104 | * Is the module inited? |
| 105 | */ |
| 106 | |
| 107 | static unsigned char ks0108_inited; |
| 108 | unsigned char ks0108_isinited(void) |
| 109 | { |
| 110 | return ks0108_inited; |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(ks0108_isinited); |
| 113 | |
Sudip Mukherjee | 4edd70c | 2015-07-20 17:27:24 +0530 | [diff] [blame] | 114 | static void ks0108_parport_attach(struct parport *port) |
| 115 | { |
| 116 | struct pardev_cb ks0108_cb; |
| 117 | |
| 118 | if (port->base != ks0108_port) |
| 119 | return; |
| 120 | |
| 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, |
| 124 | &ks0108_cb, 0); |
| 125 | if (!ks0108_pardevice) { |
| 126 | pr_err("ERROR: parport didn't register new device\n"); |
| 127 | return; |
| 128 | } |
| 129 | if (parport_claim(ks0108_pardevice)) { |
| 130 | pr_err("could not claim access to parport %i. Aborting.\n", |
| 131 | ks0108_port); |
| 132 | goto err_unreg_device; |
| 133 | } |
| 134 | |
Sudip Mukherjee | 92f2618 | 2015-08-13 19:06:05 +0530 | [diff] [blame] | 135 | ks0108_parport = port; |
Sudip Mukherjee | 4edd70c | 2015-07-20 17:27:24 +0530 | [diff] [blame] | 136 | ks0108_inited = 1; |
| 137 | return; |
| 138 | |
| 139 | err_unreg_device: |
| 140 | parport_unregister_device(ks0108_pardevice); |
| 141 | ks0108_pardevice = NULL; |
| 142 | } |
| 143 | |
| 144 | static void ks0108_parport_detach(struct parport *port) |
| 145 | { |
| 146 | if (port->base != ks0108_port) |
| 147 | return; |
| 148 | |
| 149 | if (!ks0108_pardevice) { |
| 150 | pr_err("%s: already unregistered.\n", KS0108_NAME); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | parport_release(ks0108_pardevice); |
| 155 | parport_unregister_device(ks0108_pardevice); |
| 156 | ks0108_pardevice = NULL; |
Sudip Mukherjee | 92f2618 | 2015-08-13 19:06:05 +0530 | [diff] [blame] | 157 | ks0108_parport = NULL; |
Sudip Mukherjee | 4edd70c | 2015-07-20 17:27:24 +0530 | [diff] [blame] | 158 | } |
| 159 | |
Miguel Ojeda | 34173a4 | 2007-02-20 13:58:00 -0800 | [diff] [blame] | 160 | /* |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 161 | * Module Init & Exit |
| 162 | */ |
| 163 | |
Sudip Mukherjee | 4edd70c | 2015-07-20 17:27:24 +0530 | [diff] [blame] | 164 | static struct parport_driver ks0108_parport_driver = { |
| 165 | .name = "ks0108", |
| 166 | .match_port = ks0108_parport_attach, |
| 167 | .detach = ks0108_parport_detach, |
| 168 | .devmodel = true, |
| 169 | }; |
| 170 | |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 171 | static int __init ks0108_init(void) |
| 172 | { |
Sudip Mukherjee | 4edd70c | 2015-07-20 17:27:24 +0530 | [diff] [blame] | 173 | return parport_register_driver(&ks0108_parport_driver); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void __exit ks0108_exit(void) |
| 177 | { |
Sudip Mukherjee | 4edd70c | 2015-07-20 17:27:24 +0530 | [diff] [blame] | 178 | parport_unregister_driver(&ks0108_parport_driver); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | module_init(ks0108_init); |
| 182 | module_exit(ks0108_exit); |
| 183 | |
| 184 | MODULE_LICENSE("GPL v2"); |
Miguel Ojeda | 450c622 | 2008-07-04 09:59:33 -0700 | [diff] [blame] | 185 | MODULE_AUTHOR("Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>"); |
Miguel Ojeda Sandonis | 70e8404 | 2007-02-10 01:44:32 -0800 | [diff] [blame] | 186 | MODULE_DESCRIPTION("ks0108 LCD Controller driver"); |
| 187 | |