2 * Copyright (c) 2012 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/arch/gpio.h>
17 #include <dt-bindings/gpio/x86-gpio.h>
19 DECLARE_GLOBAL_DATA_PTR
;
22 * struct broadwell_bank_priv - Private driver data
24 * @regs: Pointer to GPIO registers
25 * @bank: Bank number for this bank (0, 1 or 2)
26 * @offset: GPIO offset for this bank (0, 32 or 64)
28 struct broadwell_bank_priv
{
29 struct pch_lp_gpio_regs
*regs
;
34 static int broadwell_gpio_request(struct udevice
*dev
, unsigned offset
,
37 struct broadwell_bank_priv
*priv
= dev_get_priv(dev
);
38 struct pch_lp_gpio_regs
*regs
= priv
->regs
;
42 * Make sure that the GPIO pin we want isn't already in use for some
43 * built-in hardware function. We have to check this for every
46 debug("%s: request bank %d offset %d: ", __func__
, priv
->bank
, offset
);
47 val
= inl(®s
->own
[priv
->bank
]);
48 if (!(val
& (1UL << offset
))) {
49 debug("gpio is reserved for internal use\n");
57 static int broadwell_gpio_direction_input(struct udevice
*dev
, unsigned offset
)
59 struct broadwell_bank_priv
*priv
= dev_get_priv(dev
);
60 struct pch_lp_gpio_regs
*regs
= priv
->regs
;
62 setio_32(®s
->config
[priv
->offset
+ offset
], CONFA_DIR_INPUT
);
67 static int broadwell_gpio_get_value(struct udevice
*dev
, unsigned offset
)
69 struct broadwell_bank_priv
*priv
= dev_get_priv(dev
);
70 struct pch_lp_gpio_regs
*regs
= priv
->regs
;
72 return inl(®s
->config
[priv
->offset
+ offset
]) & CONFA_LEVEL_HIGH
?
76 static int broadwell_gpio_set_value(struct udevice
*dev
, unsigned offset
,
79 struct broadwell_bank_priv
*priv
= dev_get_priv(dev
);
80 struct pch_lp_gpio_regs
*regs
= priv
->regs
;
82 debug("%s: dev=%s, offset=%d, value=%d\n", __func__
, dev
->name
, offset
,
84 clrsetio_32(®s
->config
[priv
->offset
+ offset
], CONFA_OUTPUT_HIGH
,
85 value
? CONFA_OUTPUT_HIGH
: 0);
90 static int broadwell_gpio_direction_output(struct udevice
*dev
, unsigned offset
,
93 struct broadwell_bank_priv
*priv
= dev_get_priv(dev
);
94 struct pch_lp_gpio_regs
*regs
= priv
->regs
;
96 broadwell_gpio_set_value(dev
, offset
, value
);
97 clrio_32(®s
->config
[priv
->offset
+ offset
], CONFA_DIR_INPUT
);
102 static int broadwell_gpio_get_function(struct udevice
*dev
, unsigned offset
)
104 struct broadwell_bank_priv
*priv
= dev_get_priv(dev
);
105 struct pch_lp_gpio_regs
*regs
= priv
->regs
;
106 u32 mask
= 1UL << offset
;
108 if (!(inl(®s
->own
[priv
->bank
]) & mask
))
110 if (inl(®s
->config
[priv
->offset
+ offset
]) & CONFA_DIR_INPUT
)
116 static int broadwell_gpio_probe(struct udevice
*dev
)
118 struct broadwell_bank_platdata
*plat
= dev_get_platdata(dev
);
119 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
120 struct broadwell_bank_priv
*priv
= dev_get_priv(dev
);
122 uc_priv
->gpio_count
= GPIO_PER_BANK
;
123 uc_priv
->bank_name
= plat
->bank_name
;
125 priv
->regs
= (struct pch_lp_gpio_regs
*)(uintptr_t)plat
->base_addr
;
126 priv
->bank
= plat
->bank
;
127 priv
->offset
= priv
->bank
* 32;
128 debug("%s: probe done, regs %p, bank %d\n", __func__
, priv
->regs
,
134 static int broadwell_gpio_ofdata_to_platdata(struct udevice
*dev
)
136 struct broadwell_bank_platdata
*plat
= dev_get_platdata(dev
);
141 ret
= pch_get_gpio_base(dev
->parent
, &gpiobase
);
145 bank
= fdtdec_get_int(gd
->fdt_blob
, dev
->of_offset
, "reg", -1);
147 debug("%s: Invalid bank number %d\n", __func__
, bank
);
151 plat
->base_addr
= gpiobase
;
152 plat
->bank_name
= fdt_getprop(gd
->fdt_blob
, dev
->of_offset
,
158 static const struct dm_gpio_ops gpio_broadwell_ops
= {
159 .request
= broadwell_gpio_request
,
160 .direction_input
= broadwell_gpio_direction_input
,
161 .direction_output
= broadwell_gpio_direction_output
,
162 .get_value
= broadwell_gpio_get_value
,
163 .set_value
= broadwell_gpio_set_value
,
164 .get_function
= broadwell_gpio_get_function
,
167 static const struct udevice_id intel_broadwell_gpio_ids
[] = {
168 { .compatible
= "intel,broadwell-gpio" },
172 U_BOOT_DRIVER(gpio_broadwell
) = {
173 .name
= "gpio_broadwell",
175 .of_match
= intel_broadwell_gpio_ids
,
176 .ops
= &gpio_broadwell_ops
,
177 .ofdata_to_platdata
= broadwell_gpio_ofdata_to_platdata
,
178 .probe
= broadwell_gpio_probe
,
179 .priv_auto_alloc_size
= sizeof(struct broadwell_bank_priv
),
180 .platdata_auto_alloc_size
= sizeof(struct broadwell_bank_platdata
),