1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
15 DECLARE_GLOBAL_DATA_PTR;
22 int num_chars; /* Number of non-space characters output so far */
23 struct gpio_desc gpio_desc[8];
27 /* Crazy little function to draw shapes on the console */
28 static int shape_hello(struct udevice *dev, int ch)
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 {
40 { HEIGHT / 2 - 1, WIDTH - HEIGHT / 2 + 1, -1, 1},
44 int line, pos, inside;
45 const char *colour = pdata->colour;
49 ch = pdata->default_char;
53 index = (pdata->sides / 2) - 1;
54 if (index >= ARRAY_SIZE(shapes))
56 shape = shapes[index];
58 for (line = 0; line < HEIGHT; line++) {
60 for (pos = 0; pos < WIDTH; pos++) {
61 inside = pos >= shape.start && pos < shape.end;
63 putc(first ? *colour++ : ch);
67 colour = pdata->colour;
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;
86 static int shape_status(struct udevice *dev, int *status)
88 struct shape_data *data = dev_get_priv(dev);
90 *status = data->num_chars;
94 static int set_light(struct udevice *dev, int light)
96 struct shape_data *priv = dev_get_priv(dev);
97 struct gpio_desc *desc;
101 desc = priv->gpio_desc;
102 for (i = 0; i < priv->gpio_count; i++, desc++) {
105 ret = dm_gpio_set_value(desc, light & mask);
113 static int get_light(struct udevice *dev)
115 struct shape_data *priv = dev_get_priv(dev);
116 struct gpio_desc *desc;
121 desc = priv->gpio_desc;
122 for (i = 0; i < priv->gpio_count; i++, desc++) {
125 ret = dm_gpio_get_value(desc);
135 static const struct demo_ops shape_ops = {
136 .hello = shape_hello,
137 .status = shape_status,
138 .get_light = get_light,
139 .set_light = set_light,
142 static int shape_ofdata_to_platdata(struct udevice *dev)
144 struct dm_demo_pdata *pdata = dev_get_platdata(dev);
147 /* Parse the data that is common with all demo devices */
148 ret = demo_parse_dt(dev);
152 /* Parse the data that only we need */
153 pdata->default_char = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
159 static int dm_shape_probe(struct udevice *dev)
161 struct shape_data *priv = dev_get_priv(dev);
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);
169 priv->gpio_count = ret;
170 debug("%s: %d GPIOs\n", __func__, priv->gpio_count);
175 static int dm_shape_remove(struct udevice *dev)
177 struct shape_data *priv = dev_get_priv(dev);
179 return gpio_free_list(dev, priv->gpio_desc, priv->gpio_count);
182 static const struct udevice_id demo_shape_id[] = {
187 U_BOOT_DRIVER(demo_shape_drv) = {
188 .name = "demo_shape_drv",
189 .of_match = demo_shape_id,
191 .ofdata_to_platdata = shape_ofdata_to_platdata,
193 .probe = dm_shape_probe,
194 .remove = dm_shape_remove,
195 .priv_auto_alloc_size = sizeof(struct shape_data),
196 .platdata_auto_alloc_size = sizeof(struct dm_demo_pdata),