Blame view
drivers/auxdisplay/hd44780.c
6.97 KB
351f683b9 auxdisplay: Repla... |
1 |
// SPDX-License-Identifier: GPL-2.0+ |
d47d88361 auxdisplay: Add H... |
2 3 4 5 6 |
/* * HD44780 Character LCD driver for Linux * * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu> * Copyright (C) 2016-2017 Glider bvba |
d47d88361 auxdisplay: Add H... |
7 8 9 10 11 |
*/ #include <linux/delay.h> #include <linux/gpio/consumer.h> #include <linux/module.h> |
ac3167257 headers: separate... |
12 |
#include <linux/mod_devicetable.h> |
d47d88361 auxdisplay: Add H... |
13 14 15 |
#include <linux/platform_device.h> #include <linux/property.h> #include <linux/slab.h> |
75354284c auxdisplay: charl... |
16 |
#include "charlcd.h" |
d47d88361 auxdisplay: Add H... |
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
enum hd44780_pin { /* Order does matter due to writing to GPIO array subsets! */ PIN_DATA0, /* Optional */ PIN_DATA1, /* Optional */ PIN_DATA2, /* Optional */ PIN_DATA3, /* Optional */ PIN_DATA4, PIN_DATA5, PIN_DATA6, PIN_DATA7, PIN_CTRL_RS, PIN_CTRL_RW, /* Optional */ PIN_CTRL_E, PIN_CTRL_BL, /* Optional */ PIN_NUM }; struct hd44780 { struct gpio_desc *pins[PIN_NUM]; }; static void hd44780_backlight(struct charlcd *lcd, int on) { struct hd44780 *hd = lcd->drvdata; if (hd->pins[PIN_CTRL_BL]) gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on); } static void hd44780_strobe_gpio(struct hd44780 *hd) { /* Maintain the data during 20 us before the strobe */ udelay(20); gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 1); /* Maintain the strobe during 40 us */ udelay(40); gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 0); } /* write to an LCD panel register in 8 bit GPIO mode */ static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs) { |
b9762bebc gpiolib: Pass bit... |
63 64 65 66 67 68 |
DECLARE_BITMAP(values, 10); /* for DATA[0-7], RS, RW */ unsigned int n; values[0] = val; __assign_bit(8, values, rs); n = hd->pins[PIN_CTRL_RW] ? 10 : 9; |
d47d88361 auxdisplay: Add H... |
69 70 |
/* Present the data to the port */ |
77588c14a gpiolib: Pass arr... |
71 |
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], NULL, values); |
d47d88361 auxdisplay: Add H... |
72 73 74 75 76 77 78 |
hd44780_strobe_gpio(hd); } /* write to an LCD panel register in 4 bit GPIO mode */ static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs) { |
b9762bebc gpiolib: Pass bit... |
79 80 |
DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */ unsigned int n; |
d47d88361 auxdisplay: Add H... |
81 82 |
/* High nibble + RS, RW */ |
b9762bebc gpiolib: Pass bit... |
83 84 85 |
values[0] = val >> 4; __assign_bit(4, values, rs); n = hd->pins[PIN_CTRL_RW] ? 6 : 5; |
d47d88361 auxdisplay: Add H... |
86 87 |
/* Present the data to the port */ |
77588c14a gpiolib: Pass arr... |
88 |
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values); |
d47d88361 auxdisplay: Add H... |
89 90 91 92 |
hd44780_strobe_gpio(hd); /* Low nibble */ |
b9762bebc gpiolib: Pass bit... |
93 94 |
values[0] &= ~0x0fUL; values[0] |= val & 0x0f; |
d47d88361 auxdisplay: Add H... |
95 96 |
/* Present the data to the port */ |
77588c14a gpiolib: Pass arr... |
97 |
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values); |
d47d88361 auxdisplay: Add H... |
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
hd44780_strobe_gpio(hd); } /* Send a command to the LCD panel in 8 bit GPIO mode */ static void hd44780_write_cmd_gpio8(struct charlcd *lcd, int cmd) { struct hd44780 *hd = lcd->drvdata; hd44780_write_gpio8(hd, cmd, 0); /* The shortest command takes at least 120 us */ udelay(120); } /* Send data to the LCD panel in 8 bit GPIO mode */ static void hd44780_write_data_gpio8(struct charlcd *lcd, int data) { struct hd44780 *hd = lcd->drvdata; hd44780_write_gpio8(hd, data, 1); /* The shortest data takes at least 45 us */ udelay(45); } static const struct charlcd_ops hd44780_ops_gpio8 = { .write_cmd = hd44780_write_cmd_gpio8, .write_data = hd44780_write_data_gpio8, .backlight = hd44780_backlight, }; /* Send a command to the LCD panel in 4 bit GPIO mode */ static void hd44780_write_cmd_gpio4(struct charlcd *lcd, int cmd) { struct hd44780 *hd = lcd->drvdata; hd44780_write_gpio4(hd, cmd, 0); /* The shortest command takes at least 120 us */ udelay(120); } /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */ static void hd44780_write_cmd_raw_gpio4(struct charlcd *lcd, int cmd) { |
b9762bebc gpiolib: Pass bit... |
144 |
DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */ |
d47d88361 auxdisplay: Add H... |
145 |
struct hd44780 *hd = lcd->drvdata; |
b9762bebc gpiolib: Pass bit... |
146 |
unsigned int n; |
d47d88361 auxdisplay: Add H... |
147 148 |
/* Command nibble + RS, RW */ |
b9762bebc gpiolib: Pass bit... |
149 150 |
values[0] = cmd & 0x0f; n = hd->pins[PIN_CTRL_RW] ? 6 : 5; |
d47d88361 auxdisplay: Add H... |
151 152 |
/* Present the data to the port */ |
77588c14a gpiolib: Pass arr... |
153 |
gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values); |
d47d88361 auxdisplay: Add H... |
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
hd44780_strobe_gpio(hd); } /* Send data to the LCD panel in 4 bit GPIO mode */ static void hd44780_write_data_gpio4(struct charlcd *lcd, int data) { struct hd44780 *hd = lcd->drvdata; hd44780_write_gpio4(hd, data, 1); /* The shortest data takes at least 45 us */ udelay(45); } static const struct charlcd_ops hd44780_ops_gpio4 = { .write_cmd = hd44780_write_cmd_gpio4, .write_cmd_raw4 = hd44780_write_cmd_raw_gpio4, .write_data = hd44780_write_data_gpio4, .backlight = hd44780_backlight, }; static int hd44780_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; unsigned int i, base; struct charlcd *lcd; struct hd44780 *hd; int ifwidth, ret; /* Required pins */ ifwidth = gpiod_count(dev, "data"); if (ifwidth < 0) return ifwidth; switch (ifwidth) { case 4: base = PIN_DATA4; break; case 8: base = PIN_DATA0; break; default: return -EINVAL; } lcd = charlcd_alloc(sizeof(struct hd44780)); if (!lcd) return -ENOMEM; hd = lcd->drvdata; for (i = 0; i < ifwidth; i++) { hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i, GPIOD_OUT_LOW); if (IS_ERR(hd->pins[base + i])) { ret = PTR_ERR(hd->pins[base + i]); goto fail; } } hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); if (IS_ERR(hd->pins[PIN_CTRL_E])) { ret = PTR_ERR(hd->pins[PIN_CTRL_E]); goto fail; } hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH); if (IS_ERR(hd->pins[PIN_CTRL_RS])) { ret = PTR_ERR(hd->pins[PIN_CTRL_RS]); goto fail; } /* Optional pins */ hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw", GPIOD_OUT_LOW); if (IS_ERR(hd->pins[PIN_CTRL_RW])) { ret = PTR_ERR(hd->pins[PIN_CTRL_RW]); goto fail; } hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight", GPIOD_OUT_LOW); if (IS_ERR(hd->pins[PIN_CTRL_BL])) { ret = PTR_ERR(hd->pins[PIN_CTRL_BL]); goto fail; } /* Required properties */ |
c7c3f096e auxdisplay: hd447... |
243 244 |
ret = device_property_read_u32(dev, "display-height-chars", &lcd->height); |
d47d88361 auxdisplay: Add H... |
245 246 |
if (ret) goto fail; |
c7c3f096e auxdisplay: hd447... |
247 |
ret = device_property_read_u32(dev, "display-width-chars", &lcd->width); |
d47d88361 auxdisplay: Add H... |
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
if (ret) goto fail; /* * On displays with more than two rows, the internal buffer width is * usually equal to the display width */ if (lcd->height > 2) lcd->bwidth = lcd->width; /* Optional properties */ device_property_read_u32(dev, "internal-buffer-width", &lcd->bwidth); lcd->ifwidth = ifwidth; lcd->ops = ifwidth == 8 ? &hd44780_ops_gpio8 : &hd44780_ops_gpio4; ret = charlcd_register(lcd); if (ret) goto fail; platform_set_drvdata(pdev, lcd); return 0; fail: |
cb79eb95c auxdisplay: hd447... |
272 |
charlcd_free(lcd); |
d47d88361 auxdisplay: Add H... |
273 274 275 276 277 278 279 280 |
return ret; } static int hd44780_remove(struct platform_device *pdev) { struct charlcd *lcd = platform_get_drvdata(pdev); charlcd_unregister(lcd); |
41c8d0adf auxdisplay: hd447... |
281 |
|
cb79eb95c auxdisplay: hd447... |
282 |
charlcd_free(lcd); |
d47d88361 auxdisplay: Add H... |
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
return 0; } static const struct of_device_id hd44780_of_match[] = { { .compatible = "hit,hd44780" }, { /* sentinel */ } }; MODULE_DEVICE_TABLE(of, hd44780_of_match); static struct platform_driver hd44780_driver = { .probe = hd44780_probe, .remove = hd44780_remove, .driver = { .name = "hd44780", .of_match_table = hd44780_of_match, }, }; module_platform_driver(hd44780_driver); MODULE_DESCRIPTION("HD44780 Character LCD driver"); MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>"); MODULE_LICENSE("GPL"); |