ks0108.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/delay.h>
  16. #include <linux/parport.h>
  17. #include <linux/ks0108.h>
  18. #define KS0108_NAME "ks0108"
  19. /*
  20. * Module Parameters
  21. */
  22. static unsigned int ks0108_port = CONFIG_KS0108_PORT;
  23. module_param(ks0108_port, uint, 0444);
  24. MODULE_PARM_DESC(ks0108_port, "Parallel port where the LCD is connected");
  25. static unsigned int ks0108_delay = CONFIG_KS0108_DELAY;
  26. module_param(ks0108_delay, uint, 0444);
  27. MODULE_PARM_DESC(ks0108_delay, "Delay between each control writing (microseconds)");
  28. /*
  29. * Device
  30. */
  31. static struct parport *ks0108_parport;
  32. static struct pardevice *ks0108_pardevice;
  33. /*
  34. * ks0108 Exported Commands (don't lock)
  35. *
  36. * You _should_ lock in the top driver: This functions _should not_
  37. * get race conditions in any way. Locking for each byte here would be
  38. * so slow and useless.
  39. *
  40. * There are not bit definitions because they are not flags,
  41. * just arbitrary combinations defined by the documentation for each
  42. * function in the ks0108 LCD controller. If you want to know what means
  43. * a specific combination, look at the function's name.
  44. *
  45. * The ks0108_writecontrol bits need to be reverted ^(0,1,3) because
  46. * the parallel port also revert them using a "not" logic gate.
  47. */
  48. #define bit(n) (((unsigned char)1)<<(n))
  49. void ks0108_writedata(unsigned char byte)
  50. {
  51. parport_write_data(ks0108_parport, byte);
  52. }
  53. void ks0108_writecontrol(unsigned char byte)
  54. {
  55. udelay(ks0108_delay);
  56. parport_write_control(ks0108_parport, byte ^ (bit(0) | bit(1) | bit(3)));
  57. }
  58. void ks0108_displaystate(unsigned char state)
  59. {
  60. ks0108_writedata((state ? bit(0) : 0) | bit(1) | bit(2) | bit(3) | bit(4) | bit(5));
  61. }
  62. void ks0108_startline(unsigned char startline)
  63. {
  64. ks0108_writedata(min_t(unsigned char, startline, 63) | bit(6) |
  65. bit(7));
  66. }
  67. void ks0108_address(unsigned char address)
  68. {
  69. ks0108_writedata(min_t(unsigned char, address, 63) | bit(6));
  70. }
  71. void ks0108_page(unsigned char page)
  72. {
  73. ks0108_writedata(min_t(unsigned char, page, 7) | bit(3) | bit(4) |
  74. bit(5) | bit(7));
  75. }
  76. EXPORT_SYMBOL_GPL(ks0108_writedata);
  77. EXPORT_SYMBOL_GPL(ks0108_writecontrol);
  78. EXPORT_SYMBOL_GPL(ks0108_displaystate);
  79. EXPORT_SYMBOL_GPL(ks0108_startline);
  80. EXPORT_SYMBOL_GPL(ks0108_address);
  81. EXPORT_SYMBOL_GPL(ks0108_page);
  82. /*
  83. * Is the module inited?
  84. */
  85. static unsigned char ks0108_inited;
  86. unsigned char ks0108_isinited(void)
  87. {
  88. return ks0108_inited;
  89. }
  90. EXPORT_SYMBOL_GPL(ks0108_isinited);
  91. static void ks0108_parport_attach(struct parport *port)
  92. {
  93. struct pardev_cb ks0108_cb;
  94. if (port->base != ks0108_port)
  95. return;
  96. memset(&ks0108_cb, 0, sizeof(ks0108_cb));
  97. ks0108_cb.flags = PARPORT_DEV_EXCL;
  98. ks0108_pardevice = parport_register_dev_model(port, KS0108_NAME,
  99. &ks0108_cb, 0);
  100. if (!ks0108_pardevice) {
  101. pr_err("ERROR: parport didn't register new device\n");
  102. return;
  103. }
  104. if (parport_claim(ks0108_pardevice)) {
  105. pr_err("could not claim access to parport %i. Aborting.\n",
  106. ks0108_port);
  107. goto err_unreg_device;
  108. }
  109. ks0108_parport = port;
  110. ks0108_inited = 1;
  111. return;
  112. err_unreg_device:
  113. parport_unregister_device(ks0108_pardevice);
  114. ks0108_pardevice = NULL;
  115. }
  116. static void ks0108_parport_detach(struct parport *port)
  117. {
  118. if (port->base != ks0108_port)
  119. return;
  120. if (!ks0108_pardevice) {
  121. pr_err("%s: already unregistered.\n", KS0108_NAME);
  122. return;
  123. }
  124. parport_release(ks0108_pardevice);
  125. parport_unregister_device(ks0108_pardevice);
  126. ks0108_pardevice = NULL;
  127. ks0108_parport = NULL;
  128. }
  129. /*
  130. * Module Init & Exit
  131. */
  132. static struct parport_driver ks0108_parport_driver = {
  133. .name = "ks0108",
  134. .match_port = ks0108_parport_attach,
  135. .detach = ks0108_parport_detach,
  136. .devmodel = true,
  137. };
  138. module_parport_driver(ks0108_parport_driver);
  139. MODULE_LICENSE("GPL v2");
  140. MODULE_AUTHOR("Miguel Ojeda <ojeda@kernel.org>");
  141. MODULE_DESCRIPTION("ks0108 LCD Controller driver");