Miguel Ojeda | 351f683b | 2018-02-17 20:33:13 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 2 | /* |
| 3 | * HD44780 Character LCD driver for Linux |
| 4 | * |
| 5 | * Copyright (C) 2000-2008, Willy Tarreau <w@1wt.eu> |
| 6 | * Copyright (C) 2016-2017 Glider bvba |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/gpio/consumer.h> |
| 11 | #include <linux/module.h> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame] | 12 | #include <linux/mod_devicetable.h> |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/property.h> |
| 15 | #include <linux/slab.h> |
| 16 | |
Masahiro Yamada | 7535428 | 2019-08-06 16:14:44 +0900 | [diff] [blame] | 17 | #include "charlcd.h" |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 18 | #include "hd44780_common.h" |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 19 | |
| 20 | enum hd44780_pin { |
| 21 | /* Order does matter due to writing to GPIO array subsets! */ |
| 22 | PIN_DATA0, /* Optional */ |
| 23 | PIN_DATA1, /* Optional */ |
| 24 | PIN_DATA2, /* Optional */ |
| 25 | PIN_DATA3, /* Optional */ |
| 26 | PIN_DATA4, |
| 27 | PIN_DATA5, |
| 28 | PIN_DATA6, |
| 29 | PIN_DATA7, |
| 30 | PIN_CTRL_RS, |
| 31 | PIN_CTRL_RW, /* Optional */ |
| 32 | PIN_CTRL_E, |
| 33 | PIN_CTRL_BL, /* Optional */ |
| 34 | PIN_NUM |
| 35 | }; |
| 36 | |
| 37 | struct hd44780 { |
| 38 | struct gpio_desc *pins[PIN_NUM]; |
| 39 | }; |
| 40 | |
Lars Poeschel | 66ce7d5 | 2020-11-03 10:58:04 +0100 | [diff] [blame] | 41 | static void hd44780_backlight(struct charlcd *lcd, enum charlcd_onoff on) |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 42 | { |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 43 | struct hd44780_common *hdc = lcd->drvdata; |
| 44 | struct hd44780 *hd = hdc->hd44780; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 45 | |
| 46 | if (hd->pins[PIN_CTRL_BL]) |
| 47 | gpiod_set_value_cansleep(hd->pins[PIN_CTRL_BL], on); |
| 48 | } |
| 49 | |
| 50 | static void hd44780_strobe_gpio(struct hd44780 *hd) |
| 51 | { |
| 52 | /* Maintain the data during 20 us before the strobe */ |
| 53 | udelay(20); |
| 54 | |
| 55 | gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 1); |
| 56 | |
| 57 | /* Maintain the strobe during 40 us */ |
| 58 | udelay(40); |
| 59 | |
| 60 | gpiod_set_value_cansleep(hd->pins[PIN_CTRL_E], 0); |
| 61 | } |
| 62 | |
| 63 | /* write to an LCD panel register in 8 bit GPIO mode */ |
| 64 | static void hd44780_write_gpio8(struct hd44780 *hd, u8 val, unsigned int rs) |
| 65 | { |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 66 | DECLARE_BITMAP(values, 10); /* for DATA[0-7], RS, RW */ |
| 67 | unsigned int n; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 68 | |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 69 | values[0] = val; |
| 70 | __assign_bit(8, values, rs); |
| 71 | n = hd->pins[PIN_CTRL_RW] ? 10 : 9; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 72 | |
| 73 | /* Present the data to the port */ |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 74 | gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA0], NULL, values); |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 75 | |
| 76 | hd44780_strobe_gpio(hd); |
| 77 | } |
| 78 | |
| 79 | /* write to an LCD panel register in 4 bit GPIO mode */ |
| 80 | static void hd44780_write_gpio4(struct hd44780 *hd, u8 val, unsigned int rs) |
| 81 | { |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 82 | DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */ |
| 83 | unsigned int n; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 84 | |
| 85 | /* High nibble + RS, RW */ |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 86 | values[0] = val >> 4; |
| 87 | __assign_bit(4, values, rs); |
| 88 | n = hd->pins[PIN_CTRL_RW] ? 6 : 5; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 89 | |
| 90 | /* Present the data to the port */ |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 91 | gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values); |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 92 | |
| 93 | hd44780_strobe_gpio(hd); |
| 94 | |
| 95 | /* Low nibble */ |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 96 | values[0] &= ~0x0fUL; |
| 97 | values[0] |= val & 0x0f; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 98 | |
| 99 | /* Present the data to the port */ |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 100 | gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values); |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 101 | |
| 102 | hd44780_strobe_gpio(hd); |
| 103 | } |
| 104 | |
| 105 | /* Send a command to the LCD panel in 8 bit GPIO mode */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame] | 106 | static void hd44780_write_cmd_gpio8(struct hd44780_common *hdc, int cmd) |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 107 | { |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 108 | struct hd44780 *hd = hdc->hd44780; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 109 | |
| 110 | hd44780_write_gpio8(hd, cmd, 0); |
| 111 | |
| 112 | /* The shortest command takes at least 120 us */ |
| 113 | udelay(120); |
| 114 | } |
| 115 | |
| 116 | /* Send data to the LCD panel in 8 bit GPIO mode */ |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 117 | static void hd44780_write_data_gpio8(struct hd44780_common *hdc, int data) |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 118 | { |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 119 | struct hd44780 *hd = hdc->hd44780; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 120 | |
| 121 | hd44780_write_gpio8(hd, data, 1); |
| 122 | |
| 123 | /* The shortest data takes at least 45 us */ |
| 124 | udelay(45); |
| 125 | } |
| 126 | |
| 127 | static const struct charlcd_ops hd44780_ops_gpio8 = { |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 128 | .backlight = hd44780_backlight, |
Lars Poeschel | b26deab | 2020-11-03 10:58:11 +0100 | [diff] [blame] | 129 | .print = hd44780_common_print, |
Lars Poeschel | d3a2fb8 | 2020-11-03 10:58:12 +0100 | [diff] [blame] | 130 | .gotoxy = hd44780_common_gotoxy, |
Lars Poeschel | 88645a8 | 2020-11-03 10:58:13 +0100 | [diff] [blame] | 131 | .home = hd44780_common_home, |
Lars Poeschel | 45421ff | 2020-11-03 10:58:14 +0100 | [diff] [blame] | 132 | .clear_display = hd44780_common_clear_display, |
Lars Poeschel | 01ec46d | 2020-11-03 10:58:17 +0100 | [diff] [blame] | 133 | .init_display = hd44780_common_init_display, |
Lars Poeschel | d2f2187 | 2020-11-03 10:58:18 +0100 | [diff] [blame] | 134 | .shift_cursor = hd44780_common_shift_cursor, |
| 135 | .shift_display = hd44780_common_shift_display, |
| 136 | .display = hd44780_common_display, |
| 137 | .cursor = hd44780_common_cursor, |
| 138 | .blink = hd44780_common_blink, |
| 139 | .fontsize = hd44780_common_fontsize, |
| 140 | .lines = hd44780_common_lines, |
Lars Poeschel | 339acb0 | 2020-11-03 10:58:20 +0100 | [diff] [blame] | 141 | .redefine_char = hd44780_common_redefine_char, |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | /* Send a command to the LCD panel in 4 bit GPIO mode */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame] | 145 | static void hd44780_write_cmd_gpio4(struct hd44780_common *hdc, int cmd) |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 146 | { |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 147 | struct hd44780 *hd = hdc->hd44780; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 148 | |
| 149 | hd44780_write_gpio4(hd, cmd, 0); |
| 150 | |
| 151 | /* The shortest command takes at least 120 us */ |
| 152 | udelay(120); |
| 153 | } |
| 154 | |
| 155 | /* Send 4-bits of a command to the LCD panel in raw 4 bit GPIO mode */ |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame] | 156 | static void hd44780_write_cmd_raw_gpio4(struct hd44780_common *hdc, int cmd) |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 157 | { |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 158 | DECLARE_BITMAP(values, 6); /* for DATA[4-7], RS, RW */ |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 159 | struct hd44780 *hd = hdc->hd44780; |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 160 | unsigned int n; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 161 | |
| 162 | /* Command nibble + RS, RW */ |
Janusz Krzysztofik | b9762be | 2018-09-05 23:50:05 +0200 | [diff] [blame] | 163 | values[0] = cmd & 0x0f; |
| 164 | n = hd->pins[PIN_CTRL_RW] ? 6 : 5; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 165 | |
| 166 | /* Present the data to the port */ |
Janusz Krzysztofik | 77588c1 | 2018-09-05 23:50:07 +0200 | [diff] [blame] | 167 | gpiod_set_array_value_cansleep(n, &hd->pins[PIN_DATA4], NULL, values); |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 168 | |
| 169 | hd44780_strobe_gpio(hd); |
| 170 | } |
| 171 | |
| 172 | /* Send data to the LCD panel in 4 bit GPIO mode */ |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 173 | static void hd44780_write_data_gpio4(struct hd44780_common *hdc, int data) |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 174 | { |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 175 | struct hd44780 *hd = hdc->hd44780; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 176 | |
| 177 | hd44780_write_gpio4(hd, data, 1); |
| 178 | |
| 179 | /* The shortest data takes at least 45 us */ |
| 180 | udelay(45); |
| 181 | } |
| 182 | |
| 183 | static const struct charlcd_ops hd44780_ops_gpio4 = { |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 184 | .backlight = hd44780_backlight, |
Lars Poeschel | b26deab | 2020-11-03 10:58:11 +0100 | [diff] [blame] | 185 | .print = hd44780_common_print, |
Lars Poeschel | d3a2fb8 | 2020-11-03 10:58:12 +0100 | [diff] [blame] | 186 | .gotoxy = hd44780_common_gotoxy, |
Lars Poeschel | 88645a8 | 2020-11-03 10:58:13 +0100 | [diff] [blame] | 187 | .home = hd44780_common_home, |
Lars Poeschel | 45421ff | 2020-11-03 10:58:14 +0100 | [diff] [blame] | 188 | .clear_display = hd44780_common_clear_display, |
Lars Poeschel | 01ec46d | 2020-11-03 10:58:17 +0100 | [diff] [blame] | 189 | .init_display = hd44780_common_init_display, |
Lars Poeschel | d2f2187 | 2020-11-03 10:58:18 +0100 | [diff] [blame] | 190 | .shift_cursor = hd44780_common_shift_cursor, |
| 191 | .shift_display = hd44780_common_shift_display, |
| 192 | .display = hd44780_common_display, |
| 193 | .cursor = hd44780_common_cursor, |
| 194 | .blink = hd44780_common_blink, |
| 195 | .fontsize = hd44780_common_fontsize, |
| 196 | .lines = hd44780_common_lines, |
Lars Poeschel | 339acb0 | 2020-11-03 10:58:20 +0100 | [diff] [blame] | 197 | .redefine_char = hd44780_common_redefine_char, |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | static int hd44780_probe(struct platform_device *pdev) |
| 201 | { |
| 202 | struct device *dev = &pdev->dev; |
| 203 | unsigned int i, base; |
| 204 | struct charlcd *lcd; |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 205 | struct hd44780_common *hdc; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 206 | struct hd44780 *hd; |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 207 | int ifwidth, ret = -ENOMEM; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 208 | |
| 209 | /* Required pins */ |
| 210 | ifwidth = gpiod_count(dev, "data"); |
| 211 | if (ifwidth < 0) |
| 212 | return ifwidth; |
| 213 | |
| 214 | switch (ifwidth) { |
| 215 | case 4: |
| 216 | base = PIN_DATA4; |
| 217 | break; |
| 218 | case 8: |
| 219 | base = PIN_DATA0; |
| 220 | break; |
| 221 | default: |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 225 | hdc = hd44780_common_alloc(); |
| 226 | if (!hdc) |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 227 | return -ENOMEM; |
| 228 | |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 229 | lcd = charlcd_alloc(); |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 230 | if (!lcd) |
| 231 | goto fail1; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 232 | |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 233 | hd = kzalloc(sizeof(struct hd44780), GFP_KERNEL); |
| 234 | if (!hd) |
| 235 | goto fail2; |
| 236 | |
| 237 | hdc->hd44780 = hd; |
| 238 | lcd->drvdata = hdc; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 239 | for (i = 0; i < ifwidth; i++) { |
| 240 | hd->pins[base + i] = devm_gpiod_get_index(dev, "data", i, |
| 241 | GPIOD_OUT_LOW); |
| 242 | if (IS_ERR(hd->pins[base + i])) { |
| 243 | ret = PTR_ERR(hd->pins[base + i]); |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 244 | goto fail3; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | hd->pins[PIN_CTRL_E] = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW); |
| 249 | if (IS_ERR(hd->pins[PIN_CTRL_E])) { |
| 250 | ret = PTR_ERR(hd->pins[PIN_CTRL_E]); |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 251 | goto fail3; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | hd->pins[PIN_CTRL_RS] = devm_gpiod_get(dev, "rs", GPIOD_OUT_HIGH); |
| 255 | if (IS_ERR(hd->pins[PIN_CTRL_RS])) { |
| 256 | ret = PTR_ERR(hd->pins[PIN_CTRL_RS]); |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 257 | goto fail3; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /* Optional pins */ |
| 261 | hd->pins[PIN_CTRL_RW] = devm_gpiod_get_optional(dev, "rw", |
| 262 | GPIOD_OUT_LOW); |
| 263 | if (IS_ERR(hd->pins[PIN_CTRL_RW])) { |
| 264 | ret = PTR_ERR(hd->pins[PIN_CTRL_RW]); |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 265 | goto fail3; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | hd->pins[PIN_CTRL_BL] = devm_gpiod_get_optional(dev, "backlight", |
| 269 | GPIOD_OUT_LOW); |
| 270 | if (IS_ERR(hd->pins[PIN_CTRL_BL])) { |
| 271 | ret = PTR_ERR(hd->pins[PIN_CTRL_BL]); |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 272 | goto fail3; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* Required properties */ |
Geert Uytterhoeven | c7c3f09 | 2017-03-23 14:25:37 +0100 | [diff] [blame] | 276 | ret = device_property_read_u32(dev, "display-height-chars", |
| 277 | &lcd->height); |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 278 | if (ret) |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 279 | goto fail3; |
Geert Uytterhoeven | c7c3f09 | 2017-03-23 14:25:37 +0100 | [diff] [blame] | 280 | ret = device_property_read_u32(dev, "display-width-chars", &lcd->width); |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 281 | if (ret) |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 282 | goto fail3; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * On displays with more than two rows, the internal buffer width is |
| 286 | * usually equal to the display width |
| 287 | */ |
| 288 | if (lcd->height > 2) |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 289 | hdc->bwidth = lcd->width; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 290 | |
| 291 | /* Optional properties */ |
Lars Poeschel | 2545c1c | 2020-11-03 10:58:06 +0100 | [diff] [blame] | 292 | device_property_read_u32(dev, "internal-buffer-width", &hdc->bwidth); |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 293 | |
Lars Poeschel | 3fc04dd | 2020-11-03 10:58:07 +0100 | [diff] [blame] | 294 | hdc->ifwidth = ifwidth; |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 295 | if (ifwidth == 8) { |
| 296 | lcd->ops = &hd44780_ops_gpio8; |
| 297 | hdc->write_data = hd44780_write_data_gpio8; |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame] | 298 | hdc->write_cmd = hd44780_write_cmd_gpio8; |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 299 | } else { |
| 300 | lcd->ops = &hd44780_ops_gpio4; |
| 301 | hdc->write_data = hd44780_write_data_gpio4; |
Lars Poeschel | 2c6a82f | 2020-11-03 10:58:09 +0100 | [diff] [blame] | 302 | hdc->write_cmd = hd44780_write_cmd_gpio4; |
| 303 | hdc->write_cmd_raw4 = hd44780_write_cmd_raw_gpio4; |
Lars Poeschel | 71ff701 | 2020-11-03 10:58:08 +0100 | [diff] [blame] | 304 | } |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 305 | |
| 306 | ret = charlcd_register(lcd); |
| 307 | if (ret) |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 308 | goto fail3; |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 309 | |
| 310 | platform_set_drvdata(pdev, lcd); |
| 311 | return 0; |
| 312 | |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 313 | fail3: |
| 314 | kfree(hd); |
| 315 | fail2: |
| 316 | kfree(lcd); |
| 317 | fail1: |
| 318 | kfree(hdc); |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | static int hd44780_remove(struct platform_device *pdev) |
| 323 | { |
| 324 | struct charlcd *lcd = platform_get_drvdata(pdev); |
| 325 | |
| 326 | charlcd_unregister(lcd); |
Lars Poeschel | 333ff32 | 2021-07-14 13:02:28 +0200 | [diff] [blame] | 327 | kfree(lcd->drvdata); |
Andy Shevchenko | 41c8d0a | 2019-03-12 16:44:28 +0200 | [diff] [blame] | 328 | |
Lars Poeschel | 718e05e | 2020-11-03 10:58:05 +0100 | [diff] [blame] | 329 | kfree(lcd); |
Geert Uytterhoeven | d47d883 | 2017-03-10 15:15:21 +0100 | [diff] [blame] | 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | static const struct of_device_id hd44780_of_match[] = { |
| 334 | { .compatible = "hit,hd44780" }, |
| 335 | { /* sentinel */ } |
| 336 | }; |
| 337 | MODULE_DEVICE_TABLE(of, hd44780_of_match); |
| 338 | |
| 339 | static struct platform_driver hd44780_driver = { |
| 340 | .probe = hd44780_probe, |
| 341 | .remove = hd44780_remove, |
| 342 | .driver = { |
| 343 | .name = "hd44780", |
| 344 | .of_match_table = hd44780_of_match, |
| 345 | }, |
| 346 | }; |
| 347 | |
| 348 | module_platform_driver(hd44780_driver); |
| 349 | MODULE_DESCRIPTION("HD44780 Character LCD driver"); |
| 350 | MODULE_AUTHOR("Geert Uytterhoeven <geert@linux-m68k.org>"); |
| 351 | MODULE_LICENSE("GPL"); |