1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
13 #include <asm/global_data.h>
17 DECLARE_GLOBAL_DATA_PTR;
24 int num_chars; /* Number of non-space characters output so far */
25 struct gpio_desc gpio_desc[8];
29 /* Crazy little function to draw shapes on the console */
30 static int shape_hello(struct udevice *dev, int ch)
32 const struct dm_demo_pdata *pdata = dev_get_plat(dev);
33 struct shape_data *data = dev_get_priv(dev);
34 static const struct shape {
42 { HEIGHT / 2 - 1, WIDTH - HEIGHT / 2 + 1, -1, 1},
46 int line, pos, inside;
47 const char *colour = pdata->colour;
51 ch = pdata->default_char;
55 index = (pdata->sides / 2) - 1;
56 if (index >= ARRAY_SIZE(shapes))
58 shape = shapes[index];
60 for (line = 0; line < HEIGHT; line++) {
62 for (pos = 0; pos < WIDTH; pos++) {
63 inside = pos >= shape.start && pos < shape.end;
65 putc(first ? *colour++ : ch);
69 colour = pdata->colour;
75 shape.start += shape.dstart;
76 shape.end += shape.dend;
77 if (shape.start < 0) {
78 shape.dstart = -shape.dstart;
79 shape.dend = -shape.dend;
80 shape.start += shape.dstart;
81 shape.end += shape.dend;
88 static int shape_status(struct udevice *dev, int *status)
90 struct shape_data *data = dev_get_priv(dev);
92 *status = data->num_chars;
96 static int set_light(struct udevice *dev, int light)
98 struct shape_data *priv = dev_get_priv(dev);
99 struct gpio_desc *desc;
103 desc = priv->gpio_desc;
104 for (i = 0; i < priv->gpio_count; i++, desc++) {
107 ret = dm_gpio_set_value(desc, light & mask);
115 static int get_light(struct udevice *dev)
117 struct shape_data *priv = dev_get_priv(dev);
118 struct gpio_desc *desc;
123 desc = priv->gpio_desc;
124 for (i = 0; i < priv->gpio_count; i++, desc++) {
127 ret = dm_gpio_get_value(desc);
137 static const struct demo_ops shape_ops = {
138 .hello = shape_hello,
139 .status = shape_status,
140 .get_light = get_light,
141 .set_light = set_light,
144 static int shape_of_to_plat(struct udevice *dev)
146 struct dm_demo_pdata *pdata = dev_get_plat(dev);
149 /* Parse the data that is common with all demo devices */
150 ret = demo_parse_dt(dev);
154 /* Parse the data that only we need */
155 pdata->default_char = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
161 static int dm_shape_probe(struct udevice *dev)
163 struct shape_data *priv = dev_get_priv(dev);
166 ret = gpio_request_list_by_name(dev, "light-gpios", priv->gpio_desc,
167 ARRAY_SIZE(priv->gpio_desc),
168 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
171 priv->gpio_count = ret;
172 debug("%s: %d GPIOs\n", __func__, priv->gpio_count);
177 static int dm_shape_remove(struct udevice *dev)
179 struct shape_data *priv = dev_get_priv(dev);
181 return gpio_free_list(dev, priv->gpio_desc, priv->gpio_count);
184 static const struct udevice_id demo_shape_id[] = {
189 U_BOOT_DRIVER(demo_shape_drv) = {
190 .name = "demo_shape_drv",
191 .of_match = demo_shape_id,
193 .of_to_plat = shape_of_to_plat,
195 .probe = dm_shape_probe,
196 .remove = dm_shape_remove,
197 .priv_auto = sizeof(struct shape_data),
198 .plat_auto = sizeof(struct dm_demo_pdata),