1
2
3
4
5
6
7
8
9
10
11#include <generated/utsrelease.h>
12#include <linux/kernel.h>
13#include <linux/io.h>
14#include <linux/mfd/syscon.h>
15#include <linux/module.h>
16#include <linux/of_address.h>
17#include <linux/of_platform.h>
18#include <linux/platform_device.h>
19#include <linux/regmap.h>
20#include <linux/slab.h>
21#include <linux/sysfs.h>
22
23struct img_ascii_lcd_ctx;
24
25
26
27
28
29
30
31struct img_ascii_lcd_config {
32 unsigned int num_chars;
33 bool external_regmap;
34 void (*update)(struct img_ascii_lcd_ctx *ctx);
35};
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51struct img_ascii_lcd_ctx {
52 struct platform_device *pdev;
53 union {
54 void __iomem *base;
55 struct regmap *regmap;
56 };
57 u32 offset;
58 const struct img_ascii_lcd_config *cfg;
59 char *message;
60 unsigned int message_len;
61 unsigned int scroll_pos;
62 unsigned int scroll_rate;
63 struct timer_list timer;
64 char curr[] __aligned(8);
65};
66
67
68
69
70
71static void boston_update(struct img_ascii_lcd_ctx *ctx)
72{
73 ulong val;
74
75#if BITS_PER_LONG == 64
76 val = *((u64 *)&ctx->curr[0]);
77 __raw_writeq(val, ctx->base);
78#elif BITS_PER_LONG == 32
79 val = *((u32 *)&ctx->curr[0]);
80 __raw_writel(val, ctx->base);
81 val = *((u32 *)&ctx->curr[4]);
82 __raw_writel(val, ctx->base + 4);
83#else
84# error Not 32 or 64 bit
85#endif
86}
87
88static struct img_ascii_lcd_config boston_config = {
89 .num_chars = 8,
90 .update = boston_update,
91};
92
93
94
95
96
97static void malta_update(struct img_ascii_lcd_ctx *ctx)
98{
99 unsigned int i;
100 int err = 0;
101
102 for (i = 0; i < ctx->cfg->num_chars; i++) {
103 err = regmap_write(ctx->regmap,
104 ctx->offset + (i * 8), ctx->curr[i]);
105 if (err)
106 break;
107 }
108
109 if (unlikely(err))
110 pr_err_ratelimited("Failed to update LCD display: %d\n", err);
111}
112
113static struct img_ascii_lcd_config malta_config = {
114 .num_chars = 8,
115 .external_regmap = true,
116 .update = malta_update,
117};
118
119
120
121
122
123enum {
124 SEAD3_REG_LCD_CTRL = 0x00,
125#define SEAD3_REG_LCD_CTRL_SETDRAM BIT(7)
126 SEAD3_REG_LCD_DATA = 0x08,
127 SEAD3_REG_CPLD_STATUS = 0x10,
128#define SEAD3_REG_CPLD_STATUS_BUSY BIT(0)
129 SEAD3_REG_CPLD_DATA = 0x18,
130#define SEAD3_REG_CPLD_DATA_BUSY BIT(7)
131};
132
133static int sead3_wait_sm_idle(struct img_ascii_lcd_ctx *ctx)
134{
135 unsigned int status;
136 int err;
137
138 do {
139 err = regmap_read(ctx->regmap,
140 ctx->offset + SEAD3_REG_CPLD_STATUS,
141 &status);
142 if (err)
143 return err;
144 } while (status & SEAD3_REG_CPLD_STATUS_BUSY);
145
146 return 0;
147
148}
149
150static int sead3_wait_lcd_idle(struct img_ascii_lcd_ctx *ctx)
151{
152 unsigned int cpld_data;
153 int err;
154
155 err = sead3_wait_sm_idle(ctx);
156 if (err)
157 return err;
158
159 do {
160 err = regmap_read(ctx->regmap,
161 ctx->offset + SEAD3_REG_LCD_CTRL,
162 &cpld_data);
163 if (err)
164 return err;
165
166 err = sead3_wait_sm_idle(ctx);
167 if (err)
168 return err;
169
170 err = regmap_read(ctx->regmap,
171 ctx->offset + SEAD3_REG_CPLD_DATA,
172 &cpld_data);
173 if (err)
174 return err;
175 } while (cpld_data & SEAD3_REG_CPLD_DATA_BUSY);
176
177 return 0;
178}
179
180static void sead3_update(struct img_ascii_lcd_ctx *ctx)
181{
182 unsigned int i;
183 int err = 0;
184
185 for (i = 0; i < ctx->cfg->num_chars; i++) {
186 err = sead3_wait_lcd_idle(ctx);
187 if (err)
188 break;
189
190 err = regmap_write(ctx->regmap,
191 ctx->offset + SEAD3_REG_LCD_CTRL,
192 SEAD3_REG_LCD_CTRL_SETDRAM | i);
193 if (err)
194 break;
195
196 err = sead3_wait_lcd_idle(ctx);
197 if (err)
198 break;
199
200 err = regmap_write(ctx->regmap,
201 ctx->offset + SEAD3_REG_LCD_DATA,
202 ctx->curr[i]);
203 if (err)
204 break;
205 }
206
207 if (unlikely(err))
208 pr_err_ratelimited("Failed to update LCD display: %d\n", err);
209}
210
211static struct img_ascii_lcd_config sead3_config = {
212 .num_chars = 16,
213 .external_regmap = true,
214 .update = sead3_update,
215};
216
217static const struct of_device_id img_ascii_lcd_matches[] = {
218 { .compatible = "img,boston-lcd", .data = &boston_config },
219 { .compatible = "mti,malta-lcd", .data = &malta_config },
220 { .compatible = "mti,sead3-lcd", .data = &sead3_config },
221 { }
222};
223MODULE_DEVICE_TABLE(of, img_ascii_lcd_matches);
224
225
226
227
228
229
230
231
232static void img_ascii_lcd_scroll(struct timer_list *t)
233{
234 struct img_ascii_lcd_ctx *ctx = from_timer(ctx, t, timer);
235 unsigned int i, ch = ctx->scroll_pos;
236 unsigned int num_chars = ctx->cfg->num_chars;
237
238
239 for (i = 0; i < num_chars;) {
240
241 for (; i < num_chars && ch < ctx->message_len; i++, ch++)
242 ctx->curr[i] = ctx->message[ch];
243
244
245 ch = 0;
246 }
247
248
249 ctx->cfg->update(ctx);
250
251
252 ctx->scroll_pos++;
253 ctx->scroll_pos %= ctx->message_len;
254
255
256 if (ctx->message_len > ctx->cfg->num_chars)
257 mod_timer(&ctx->timer, jiffies + ctx->scroll_rate);
258}
259
260
261
262
263
264
265
266
267
268
269
270
271
272static int img_ascii_lcd_display(struct img_ascii_lcd_ctx *ctx,
273 const char *msg, ssize_t count)
274{
275 char *new_msg;
276
277
278 del_timer_sync(&ctx->timer);
279
280 if (count == -1)
281 count = strlen(msg);
282
283
284 if (msg[count - 1] == '\n')
285 count--;
286
287 new_msg = devm_kmalloc(&ctx->pdev->dev, count + 1, GFP_KERNEL);
288 if (!new_msg)
289 return -ENOMEM;
290
291 memcpy(new_msg, msg, count);
292 new_msg[count] = 0;
293
294 if (ctx->message)
295 devm_kfree(&ctx->pdev->dev, ctx->message);
296
297 ctx->message = new_msg;
298 ctx->message_len = count;
299 ctx->scroll_pos = 0;
300
301
302 img_ascii_lcd_scroll(&ctx->timer);
303
304 return 0;
305}
306
307
308
309
310
311
312
313
314
315
316
317
318static ssize_t message_show(struct device *dev, struct device_attribute *attr,
319 char *buf)
320{
321 struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);
322
323 return sprintf(buf, "%s\n", ctx->message);
324}
325
326
327
328
329
330
331
332
333
334
335
336
337static ssize_t message_store(struct device *dev, struct device_attribute *attr,
338 const char *buf, size_t count)
339{
340 struct img_ascii_lcd_ctx *ctx = dev_get_drvdata(dev);
341 int err;
342
343 err = img_ascii_lcd_display(ctx, buf, count);
344 return err ?: count;
345}
346
347static DEVICE_ATTR_RW(message);
348
349
350
351
352
353
354
355
356
357
358static int img_ascii_lcd_probe(struct platform_device *pdev)
359{
360 const struct of_device_id *match;
361 const struct img_ascii_lcd_config *cfg;
362 struct img_ascii_lcd_ctx *ctx;
363 struct resource *res;
364 int err;
365
366 match = of_match_device(img_ascii_lcd_matches, &pdev->dev);
367 if (!match)
368 return -ENODEV;
369
370 cfg = match->data;
371 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx) + cfg->num_chars,
372 GFP_KERNEL);
373 if (!ctx)
374 return -ENOMEM;
375
376 if (cfg->external_regmap) {
377 ctx->regmap = syscon_node_to_regmap(pdev->dev.parent->of_node);
378 if (IS_ERR(ctx->regmap))
379 return PTR_ERR(ctx->regmap);
380
381 if (of_property_read_u32(pdev->dev.of_node, "offset",
382 &ctx->offset))
383 return -EINVAL;
384 } else {
385 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
386 ctx->base = devm_ioremap_resource(&pdev->dev, res);
387 if (IS_ERR(ctx->base))
388 return PTR_ERR(ctx->base);
389 }
390
391 ctx->pdev = pdev;
392 ctx->cfg = cfg;
393 ctx->message = NULL;
394 ctx->scroll_pos = 0;
395 ctx->scroll_rate = HZ / 2;
396
397
398 timer_setup(&ctx->timer, img_ascii_lcd_scroll, 0);
399
400 platform_set_drvdata(pdev, ctx);
401
402
403 err = img_ascii_lcd_display(ctx, "Linux " UTS_RELEASE " ", -1);
404 if (err)
405 goto out_del_timer;
406
407 err = device_create_file(&pdev->dev, &dev_attr_message);
408 if (err)
409 goto out_del_timer;
410
411 return 0;
412out_del_timer:
413 del_timer_sync(&ctx->timer);
414 return err;
415}
416
417
418
419
420
421
422
423
424
425
426static int img_ascii_lcd_remove(struct platform_device *pdev)
427{
428 struct img_ascii_lcd_ctx *ctx = platform_get_drvdata(pdev);
429
430 device_remove_file(&pdev->dev, &dev_attr_message);
431 del_timer_sync(&ctx->timer);
432 return 0;
433}
434
435static struct platform_driver img_ascii_lcd_driver = {
436 .driver = {
437 .name = "img-ascii-lcd",
438 .of_match_table = img_ascii_lcd_matches,
439 },
440 .probe = img_ascii_lcd_probe,
441 .remove = img_ascii_lcd_remove,
442};
443module_platform_driver(img_ascii_lcd_driver);
444
445MODULE_DESCRIPTION("Imagination Technologies ASCII LCD Display");
446MODULE_AUTHOR("Paul Burton <paul.burton@mips.com>");
447MODULE_LICENSE("GPL");
448