1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2015
4 * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
5 *
6 * (C) Copyright 2017
7 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
8 */
9
10 #include <common.h>
11 #include <dm.h>
12 #include <regmap.h>
13 #include <misc.h>
14
15 struct gdsys_rxaui_ctrl_regs {
16 u16 gen_cnt;
17 u16 err_cnt;
18 u16 succ_cnt;
19 u16 status;
20 u16 ctrl_0;
21 u16 ctrl_1;
22 };
23
24 #define rxaui_ctrl_set(map, member, val) \
25 regmap_set(map, struct gdsys_rxaui_ctrl_regs, member, val)
26
27 #define rxaui_ctrl_get(map, member, valp) \
28 regmap_get(map, struct gdsys_rxaui_ctrl_regs, member, valp)
29
30 struct gdsys_rxaui_ctrl_priv {
31 struct regmap *map;
32 bool state;
33 };
34
gdsys_rxaui_set_polarity_inversion(struct udevice * dev,bool val)35 int gdsys_rxaui_set_polarity_inversion(struct udevice *dev, bool val)
36 {
37 struct gdsys_rxaui_ctrl_priv *priv = dev_get_priv(dev);
38 u16 state;
39
40 priv->state = !priv->state;
41
42 rxaui_ctrl_get(priv->map, ctrl_1, &state);
43
44 if (val)
45 state |= ~0x7800;
46 else
47 state &= ~0x7800;
48
49 rxaui_ctrl_set(priv->map, ctrl_1, state);
50
51 return !priv->state;
52 }
53
54 static const struct misc_ops gdsys_rxaui_ctrl_ops = {
55 .set_enabled = gdsys_rxaui_set_polarity_inversion,
56 };
57
gdsys_rxaui_ctrl_probe(struct udevice * dev)58 int gdsys_rxaui_ctrl_probe(struct udevice *dev)
59 {
60 struct gdsys_rxaui_ctrl_priv *priv = dev_get_priv(dev);
61
62 regmap_init_mem(dev_ofnode(dev), &priv->map);
63
64 priv->state = false;
65
66 return 0;
67 }
68
69 static const struct udevice_id gdsys_rxaui_ctrl_ids[] = {
70 { .compatible = "gdsys,rxaui_ctrl" },
71 { }
72 };
73
74 U_BOOT_DRIVER(gdsys_rxaui_ctrl) = {
75 .name = "gdsys_rxaui_ctrl",
76 .id = UCLASS_MISC,
77 .ops = &gdsys_rxaui_ctrl_ops,
78 .of_match = gdsys_rxaui_ctrl_ids,
79 .probe = gdsys_rxaui_ctrl_probe,
80 .priv_auto_alloc_size = sizeof(struct gdsys_rxaui_ctrl_priv),
81 };
82