blob: 98b644680c83e4f1b884674b07c4b702b5c53d5b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass39f76112014-02-26 15:59:23 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass39f76112014-02-26 15:59:23 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <fdtdec.h>
10#include <malloc.h>
11#include <dm-demo.h>
12#include <asm/io.h>
Simon Glassa02af4a2015-01-05 20:05:31 -070013#include <asm/gpio.h>
Simon Glass39f76112014-02-26 15:59:23 -070014
15DECLARE_GLOBAL_DATA_PTR;
16
17/* Shape size */
18#define WIDTH 8
19#define HEIGHT 6
20
21struct shape_data {
22 int num_chars; /* Number of non-space characters output so far */
Simon Glassa02af4a2015-01-05 20:05:31 -070023 struct gpio_desc gpio_desc[8];
24 int gpio_count;
Simon Glass39f76112014-02-26 15:59:23 -070025};
26
27/* Crazy little function to draw shapes on the console */
Heiko Schocher54c5d082014-05-22 12:43:05 +020028static int shape_hello(struct udevice *dev, int ch)
Simon Glass39f76112014-02-26 15:59:23 -070029{
30 const struct dm_demo_pdata *pdata = dev_get_platdata(dev);
31 struct shape_data *data = dev_get_priv(dev);
32 static const struct shape {
33 int start;
34 int end;
35 int dstart;
36 int dend;
37 } shapes[3] = {
38 { 0, 1, 0, 1 },
39 { 0, WIDTH, 0, 0 },
40 { HEIGHT / 2 - 1, WIDTH - HEIGHT / 2 + 1, -1, 1},
41 };
42 struct shape shape;
43 unsigned int index;
44 int line, pos, inside;
45 const char *colour = pdata->colour;
46 int first = 0;
47
48 if (!ch)
49 ch = pdata->default_char;
50 if (!ch)
51 ch = '@';
52
53 index = (pdata->sides / 2) - 1;
54 if (index >= ARRAY_SIZE(shapes))
55 return -EIO;
56 shape = shapes[index];
57
58 for (line = 0; line < HEIGHT; line++) {
59 first = 1;
60 for (pos = 0; pos < WIDTH; pos++) {
61 inside = pos >= shape.start && pos < shape.end;
62 if (inside) {
63 putc(first ? *colour++ : ch);
64 data->num_chars++;
65 first = 0;
66 if (!*colour)
67 colour = pdata->colour;
68 } else {
69 putc(' ');
70 }
71 }
72 putc('\n');
73 shape.start += shape.dstart;
74 shape.end += shape.dend;
75 if (shape.start < 0) {
76 shape.dstart = -shape.dstart;
77 shape.dend = -shape.dend;
78 shape.start += shape.dstart;
79 shape.end += shape.dend;
80 }
81 }
82
83 return 0;
84}
85
Heiko Schocher54c5d082014-05-22 12:43:05 +020086static int shape_status(struct udevice *dev, int *status)
Simon Glass39f76112014-02-26 15:59:23 -070087{
88 struct shape_data *data = dev_get_priv(dev);
89
90 *status = data->num_chars;
91 return 0;
92}
93
Simon Glassa02af4a2015-01-05 20:05:31 -070094static int set_light(struct udevice *dev, int light)
95{
96 struct shape_data *priv = dev_get_priv(dev);
97 struct gpio_desc *desc;
98 int ret;
99 int i;
100
101 desc = priv->gpio_desc;
102 for (i = 0; i < priv->gpio_count; i++, desc++) {
103 uint mask = 1 << i;
104
105 ret = dm_gpio_set_value(desc, light & mask);
106 if (ret < 0)
107 return ret;
108 }
109
110 return 0;
111}
112
113static int get_light(struct udevice *dev)
114{
115 struct shape_data *priv = dev_get_priv(dev);
116 struct gpio_desc *desc;
117 uint value = 0;
118 int ret;
119 int i;
120
121 desc = priv->gpio_desc;
122 for (i = 0; i < priv->gpio_count; i++, desc++) {
123 uint mask = 1 << i;
124
125 ret = dm_gpio_get_value(desc);
126 if (ret < 0)
127 return ret;
128 if (ret)
129 value |= mask;
130 }
131
132 return value;
133}
134
Simon Glass39f76112014-02-26 15:59:23 -0700135static const struct demo_ops shape_ops = {
136 .hello = shape_hello,
137 .status = shape_status,
Simon Glassa02af4a2015-01-05 20:05:31 -0700138 .get_light = get_light,
139 .set_light = set_light,
Simon Glass39f76112014-02-26 15:59:23 -0700140};
141
Heiko Schocher54c5d082014-05-22 12:43:05 +0200142static int shape_ofdata_to_platdata(struct udevice *dev)
Simon Glass39f76112014-02-26 15:59:23 -0700143{
144 struct dm_demo_pdata *pdata = dev_get_platdata(dev);
145 int ret;
146
147 /* Parse the data that is common with all demo devices */
148 ret = demo_parse_dt(dev);
149 if (ret)
150 return ret;
151
152 /* Parse the data that only we need */
Simon Glasse160f7d2017-01-17 16:52:55 -0700153 pdata->default_char = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glass39f76112014-02-26 15:59:23 -0700154 "character", '@');
155
156 return 0;
157}
158
Simon Glassa02af4a2015-01-05 20:05:31 -0700159static int dm_shape_probe(struct udevice *dev)
160{
161 struct shape_data *priv = dev_get_priv(dev);
162 int ret;
163
164 ret = gpio_request_list_by_name(dev, "light-gpios", priv->gpio_desc,
165 ARRAY_SIZE(priv->gpio_desc),
166 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
167 if (ret < 0)
168 return ret;
169 priv->gpio_count = ret;
170 debug("%s: %d GPIOs\n", __func__, priv->gpio_count);
171
172 return 0;
173}
174
175static int dm_shape_remove(struct udevice *dev)
176{
177 struct shape_data *priv = dev_get_priv(dev);
178
179 return gpio_free_list(dev, priv->gpio_desc, priv->gpio_count);
180}
181
Simon Glassae7f4512014-06-11 23:29:45 -0600182static const struct udevice_id demo_shape_id[] = {
Simon Glass39f76112014-02-26 15:59:23 -0700183 { "demo-shape", 0 },
184 { },
185};
186
187U_BOOT_DRIVER(demo_shape_drv) = {
188 .name = "demo_shape_drv",
189 .of_match = demo_shape_id,
190 .id = UCLASS_DEMO,
191 .ofdata_to_platdata = shape_ofdata_to_platdata,
192 .ops = &shape_ops,
Simon Glassa02af4a2015-01-05 20:05:31 -0700193 .probe = dm_shape_probe,
194 .remove = dm_shape_remove,
Simon Glass39f76112014-02-26 15:59:23 -0700195 .priv_auto_alloc_size = sizeof(struct shape_data),
196 .platdata_auto_alloc_size = sizeof(struct dm_demo_pdata),
197};