blob: 2cbf7488ad62d62d966453f54e4164d9635ad512 [file] [log] [blame]
T Karthik Reddy15efe0f2022-02-23 16:21:30 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * slg7xl45106_i2c_gpo driver
4 *
5 * Copyright (C) 2021 Xilinx, Inc.
6 */
7
8#include <common.h>
9#include <errno.h>
10#include <asm/io.h>
11#include <asm/gpio.h>
12#include <dm.h>
13#include <i2c.h>
14#include <asm/arch/hardware.h>
15
16#define SLG7XL45106_REG 0xdb
17
18static int slg7xl45106_i2c_gpo_direction_input(struct udevice *dev,
19 unsigned int offset)
20{
21 return 0;
22}
23
24static int slg7xl45106_i2c_gpo_xlate(struct udevice *dev,
25 struct gpio_desc *desc,
26 struct ofnode_phandle_args *args)
27{
28 desc->offset = (unsigned int)args->args[0];
29
30 return 0;
31}
32
33static int slg7xl45106_i2c_gpo_set_value(struct udevice *dev,
34 unsigned int offset, int value)
35{
36 int ret;
37 u8 val;
38
39 ret = dm_i2c_read(dev, SLG7XL45106_REG, &val, 1);
40 if (ret)
41 return ret;
42
43 if (value)
44 val |= BIT(offset);
45 else
46 val &= ~BIT(offset);
47
48 return dm_i2c_write(dev, SLG7XL45106_REG, &val, 1);
49}
50
51static int slg7xl45106_i2c_gpo_direction_output(struct udevice *dev,
52 unsigned int offset, int value)
53{
54 return slg7xl45106_i2c_gpo_set_value(dev, offset, value);
55}
56
57static int slg7xl45106_i2c_gpo_get_value(struct udevice *dev,
58 unsigned int offset)
59{
60 int ret;
61 u8 val;
62
63 ret = dm_i2c_read(dev, SLG7XL45106_REG, &val, 1);
64 if (ret)
65 return ret;
66
67 return !!(val & BIT(offset));
68}
69
70static int slg7xl45106_i2c_gpo_get_function(struct udevice *dev,
71 unsigned int offset)
72{
73 return GPIOF_OUTPUT;
74}
75
76static const struct dm_gpio_ops slg7xl45106_i2c_gpo_ops = {
77 .direction_input = slg7xl45106_i2c_gpo_direction_input,
78 .direction_output = slg7xl45106_i2c_gpo_direction_output,
79 .get_value = slg7xl45106_i2c_gpo_get_value,
80 .set_value = slg7xl45106_i2c_gpo_set_value,
81 .get_function = slg7xl45106_i2c_gpo_get_function,
82 .xlate = slg7xl45106_i2c_gpo_xlate,
83};
84
85static int slg7xl45106_i2c_gpo_probe(struct udevice *dev)
86{
87 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
88 const void *label_ptr;
89
90 label_ptr = dev_read_prop(dev, "label", NULL);
91 if (label_ptr) {
92 uc_priv->bank_name = strdup(label_ptr);
93 if (!uc_priv->bank_name)
94 return -ENOMEM;
95 } else {
96 uc_priv->bank_name = dev->name;
97 }
98
99 uc_priv->gpio_count = 8;
100
101 return 0;
102}
103
104static const struct udevice_id slg7xl45106_i2c_gpo_ids[] = {
105 { .compatible = "dlg,slg7xl45106",},
106 { }
107};
108
109U_BOOT_DRIVER(slg7xl45106_i2c_gpo) = {
110 .name = "slg7xl45106_i2c_gpo",
111 .id = UCLASS_GPIO,
112 .ops = &slg7xl45106_i2c_gpo_ops,
113 .of_match = slg7xl45106_i2c_gpo_ids,
114 .probe = slg7xl45106_i2c_gpo_probe,
115};