1 | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | /* |
3 | * Copyright (C) 2016 Imagination Technologies |
4 | * Author: Paul Burton <paul.burton@mips.com> |
5 | */ |
6 | |
7 | #include <linux/kernel.h> |
8 | #include <linux/io.h> |
9 | #include <linux/mfd/syscon.h> |
10 | #include <linux/module.h> |
11 | #include <linux/of.h> |
12 | #include <linux/platform_device.h> |
13 | #include <linux/property.h> |
14 | #include <linux/regmap.h> |
15 | #include <linux/slab.h> |
16 | |
17 | #include "line-display.h" |
18 | |
19 | struct img_ascii_lcd_ctx; |
20 | |
21 | /** |
22 | * struct img_ascii_lcd_config - Configuration information about an LCD model |
23 | * @num_chars: the number of characters the LCD can display |
24 | * @external_regmap: true if registers are in a system controller, else false |
25 | * @ops: character line display operations |
26 | */ |
27 | struct img_ascii_lcd_config { |
28 | unsigned int num_chars; |
29 | bool external_regmap; |
30 | const struct linedisp_ops ops; |
31 | }; |
32 | |
33 | /** |
34 | * struct img_ascii_lcd_ctx - Private data structure |
35 | * @linedisp: line display structure |
36 | * @base: the base address of the LCD registers |
37 | * @regmap: the regmap through which LCD registers are accessed |
38 | * @offset: the offset within regmap to the start of the LCD registers |
39 | * @cfg: pointer to the LCD model configuration |
40 | */ |
41 | struct img_ascii_lcd_ctx { |
42 | struct linedisp linedisp; |
43 | union { |
44 | void __iomem *base; |
45 | struct regmap *regmap; |
46 | }; |
47 | u32 offset; |
48 | const struct img_ascii_lcd_config *cfg; |
49 | }; |
50 | |
51 | /* |
52 | * MIPS Boston development board |
53 | */ |
54 | |
55 | static void boston_update(struct linedisp *linedisp) |
56 | { |
57 | struct img_ascii_lcd_ctx *ctx = |
58 | container_of(linedisp, struct img_ascii_lcd_ctx, linedisp); |
59 | ulong val; |
60 | |
61 | #if BITS_PER_LONG == 64 |
62 | val = *((u64 *)&linedisp->buf[0]); |
63 | __raw_writeq(val, addr: ctx->base); |
64 | #elif BITS_PER_LONG == 32 |
65 | val = *((u32 *)&linedisp->buf[0]); |
66 | __raw_writel(val, ctx->base); |
67 | val = *((u32 *)&linedisp->buf[4]); |
68 | __raw_writel(val, ctx->base + 4); |
69 | #else |
70 | # error Not 32 or 64 bit |
71 | #endif |
72 | } |
73 | |
74 | static struct img_ascii_lcd_config boston_config = { |
75 | .num_chars = 8, |
76 | .ops = { |
77 | .update = boston_update, |
78 | }, |
79 | }; |
80 | |
81 | /* |
82 | * MIPS Malta development board |
83 | */ |
84 | |
85 | static void malta_update(struct linedisp *linedisp) |
86 | { |
87 | struct img_ascii_lcd_ctx *ctx = |
88 | container_of(linedisp, struct img_ascii_lcd_ctx, linedisp); |
89 | unsigned int i; |
90 | int err = 0; |
91 | |
92 | for (i = 0; i < linedisp->num_chars; i++) { |
93 | err = regmap_write(map: ctx->regmap, |
94 | reg: ctx->offset + (i * 8), val: linedisp->buf[i]); |
95 | if (err) |
96 | break; |
97 | } |
98 | |
99 | if (unlikely(err)) |
100 | pr_err_ratelimited("Failed to update LCD display: %d\n" , err); |
101 | } |
102 | |
103 | static struct img_ascii_lcd_config malta_config = { |
104 | .num_chars = 8, |
105 | .external_regmap = true, |
106 | .ops = { |
107 | .update = malta_update, |
108 | }, |
109 | }; |
110 | |
111 | /* |
112 | * MIPS SEAD3 development board |
113 | */ |
114 | |
115 | enum { |
116 | SEAD3_REG_LCD_CTRL = 0x00, |
117 | #define SEAD3_REG_LCD_CTRL_SETDRAM BIT(7) |
118 | SEAD3_REG_LCD_DATA = 0x08, |
119 | SEAD3_REG_CPLD_STATUS = 0x10, |
120 | #define SEAD3_REG_CPLD_STATUS_BUSY BIT(0) |
121 | SEAD3_REG_CPLD_DATA = 0x18, |
122 | #define SEAD3_REG_CPLD_DATA_BUSY BIT(7) |
123 | }; |
124 | |
125 | static int sead3_wait_sm_idle(struct img_ascii_lcd_ctx *ctx) |
126 | { |
127 | unsigned int status; |
128 | int err; |
129 | |
130 | do { |
131 | err = regmap_read(map: ctx->regmap, |
132 | reg: ctx->offset + SEAD3_REG_CPLD_STATUS, |
133 | val: &status); |
134 | if (err) |
135 | return err; |
136 | } while (status & SEAD3_REG_CPLD_STATUS_BUSY); |
137 | |
138 | return 0; |
139 | |
140 | } |
141 | |
142 | static int sead3_wait_lcd_idle(struct img_ascii_lcd_ctx *ctx) |
143 | { |
144 | unsigned int cpld_data; |
145 | int err; |
146 | |
147 | err = sead3_wait_sm_idle(ctx); |
148 | if (err) |
149 | return err; |
150 | |
151 | do { |
152 | err = regmap_read(map: ctx->regmap, |
153 | reg: ctx->offset + SEAD3_REG_LCD_CTRL, |
154 | val: &cpld_data); |
155 | if (err) |
156 | return err; |
157 | |
158 | err = sead3_wait_sm_idle(ctx); |
159 | if (err) |
160 | return err; |
161 | |
162 | err = regmap_read(map: ctx->regmap, |
163 | reg: ctx->offset + SEAD3_REG_CPLD_DATA, |
164 | val: &cpld_data); |
165 | if (err) |
166 | return err; |
167 | } while (cpld_data & SEAD3_REG_CPLD_DATA_BUSY); |
168 | |
169 | return 0; |
170 | } |
171 | |
172 | static void sead3_update(struct linedisp *linedisp) |
173 | { |
174 | struct img_ascii_lcd_ctx *ctx = |
175 | container_of(linedisp, struct img_ascii_lcd_ctx, linedisp); |
176 | unsigned int i; |
177 | int err = 0; |
178 | |
179 | for (i = 0; i < linedisp->num_chars; i++) { |
180 | err = sead3_wait_lcd_idle(ctx); |
181 | if (err) |
182 | break; |
183 | |
184 | err = regmap_write(map: ctx->regmap, |
185 | reg: ctx->offset + SEAD3_REG_LCD_CTRL, |
186 | SEAD3_REG_LCD_CTRL_SETDRAM | i); |
187 | if (err) |
188 | break; |
189 | |
190 | err = sead3_wait_lcd_idle(ctx); |
191 | if (err) |
192 | break; |
193 | |
194 | err = regmap_write(map: ctx->regmap, |
195 | reg: ctx->offset + SEAD3_REG_LCD_DATA, |
196 | val: linedisp->buf[i]); |
197 | if (err) |
198 | break; |
199 | } |
200 | |
201 | if (unlikely(err)) |
202 | pr_err_ratelimited("Failed to update LCD display: %d\n" , err); |
203 | } |
204 | |
205 | static struct img_ascii_lcd_config sead3_config = { |
206 | .num_chars = 16, |
207 | .external_regmap = true, |
208 | .ops = { |
209 | .update = sead3_update, |
210 | }, |
211 | }; |
212 | |
213 | static const struct of_device_id img_ascii_lcd_matches[] = { |
214 | { .compatible = "img,boston-lcd" , .data = &boston_config }, |
215 | { .compatible = "mti,malta-lcd" , .data = &malta_config }, |
216 | { .compatible = "mti,sead3-lcd" , .data = &sead3_config }, |
217 | { /* sentinel */ } |
218 | }; |
219 | MODULE_DEVICE_TABLE(of, img_ascii_lcd_matches); |
220 | |
221 | /** |
222 | * img_ascii_lcd_probe() - probe an LCD display device |
223 | * @pdev: the LCD platform device |
224 | * |
225 | * Probe an LCD display device, ensuring that we have the required resources in |
226 | * order to access the LCD & setting up private data as well as sysfs files. |
227 | * |
228 | * Return: 0 on success, else -ERRNO |
229 | */ |
230 | static int img_ascii_lcd_probe(struct platform_device *pdev) |
231 | { |
232 | struct device *dev = &pdev->dev; |
233 | const struct img_ascii_lcd_config *cfg = device_get_match_data(dev); |
234 | struct img_ascii_lcd_ctx *ctx; |
235 | int err; |
236 | |
237 | ctx = devm_kzalloc(dev, size: sizeof(*ctx), GFP_KERNEL); |
238 | if (!ctx) |
239 | return -ENOMEM; |
240 | |
241 | if (cfg->external_regmap) { |
242 | ctx->regmap = syscon_node_to_regmap(np: dev->parent->of_node); |
243 | if (IS_ERR(ptr: ctx->regmap)) |
244 | return PTR_ERR(ptr: ctx->regmap); |
245 | |
246 | if (of_property_read_u32(np: dev->of_node, propname: "offset" , out_value: &ctx->offset)) |
247 | return -EINVAL; |
248 | } else { |
249 | ctx->base = devm_platform_ioremap_resource(pdev, index: 0); |
250 | if (IS_ERR(ptr: ctx->base)) |
251 | return PTR_ERR(ptr: ctx->base); |
252 | } |
253 | |
254 | err = linedisp_register(linedisp: &ctx->linedisp, parent: dev, num_chars: cfg->num_chars, ops: &cfg->ops); |
255 | if (err) |
256 | return err; |
257 | |
258 | /* for backwards compatibility */ |
259 | err = compat_only_sysfs_link_entry_to_kobj(kobj: &dev->kobj, |
260 | target_kobj: &ctx->linedisp.dev.kobj, |
261 | target_name: "message" , NULL); |
262 | if (err) |
263 | goto err_unregister; |
264 | |
265 | platform_set_drvdata(pdev, data: ctx); |
266 | return 0; |
267 | |
268 | err_unregister: |
269 | linedisp_unregister(linedisp: &ctx->linedisp); |
270 | return err; |
271 | } |
272 | |
273 | /** |
274 | * img_ascii_lcd_remove() - remove an LCD display device |
275 | * @pdev: the LCD platform device |
276 | * |
277 | * Remove an LCD display device, freeing private resources & ensuring that the |
278 | * driver stops using the LCD display registers. |
279 | */ |
280 | static void img_ascii_lcd_remove(struct platform_device *pdev) |
281 | { |
282 | struct img_ascii_lcd_ctx *ctx = platform_get_drvdata(pdev); |
283 | |
284 | sysfs_remove_link(kobj: &pdev->dev.kobj, name: "message" ); |
285 | linedisp_unregister(linedisp: &ctx->linedisp); |
286 | } |
287 | |
288 | static struct platform_driver img_ascii_lcd_driver = { |
289 | .driver = { |
290 | .name = "img-ascii-lcd" , |
291 | .of_match_table = img_ascii_lcd_matches, |
292 | }, |
293 | .probe = img_ascii_lcd_probe, |
294 | .remove_new = img_ascii_lcd_remove, |
295 | }; |
296 | module_platform_driver(img_ascii_lcd_driver); |
297 | |
298 | MODULE_DESCRIPTION("Imagination Technologies ASCII LCD Display" ); |
299 | MODULE_AUTHOR("Paul Burton <paul.burton@mips.com>" ); |
300 | MODULE_LICENSE("GPL" ); |
301 | MODULE_IMPORT_NS(LINEDISP); |
302 | |