blob: 987fbdf9bc08ab5e5b33155a121e3f5972fbb56b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc30ddcc2017-07-25 08:30:11 -06002/*
3 * Copyright (C) 2014 NVIDIA Corporation
Simon Glassc30ddcc2017-07-25 08:30:11 -06004 */
5
6#include <common.h>
7#include <dm.h>
8#include <asm/gpio.h>
Simon Glassbdd5f812023-09-14 18:21:46 -06009#include <linux/printk.h>
Simon Glassc30ddcc2017-07-25 08:30:11 -060010#include <power/as3722.h>
11#include <power/pmic.h>
12
13#define NUM_GPIOS 8
14
15int as3722_gpio_configure(struct udevice *pmic, unsigned int gpio,
16 unsigned long flags)
17{
18 u8 value = 0;
19 int err;
20
21 if (flags & AS3722_GPIO_OUTPUT_VDDH)
22 value |= AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH;
23
24 if (flags & AS3722_GPIO_INVERT)
25 value |= AS3722_GPIO_CONTROL_INVERT;
26
27 err = pmic_reg_write(pmic, AS3722_GPIO_CONTROL(gpio), value);
28 if (err) {
Simon Glass73126ac2018-11-18 08:14:28 -070029 pr_err("failed to configure GPIO#%u: %d\n", gpio, err);
Simon Glassc30ddcc2017-07-25 08:30:11 -060030 return err;
31 }
32
33 return 0;
34}
35
36static int as3722_gpio_set_value(struct udevice *dev, unsigned int gpio,
37 int level)
38{
39 struct udevice *pmic = dev_get_parent(dev);
40 const char *l;
41 u8 value;
42 int err;
43
44 if (gpio >= NUM_GPIOS)
45 return -EINVAL;
46
47 err = pmic_reg_read(pmic, AS3722_GPIO_SIGNAL_OUT);
48 if (err < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070049 pr_err("failed to read GPIO signal out register: %d\n", err);
Simon Glassc30ddcc2017-07-25 08:30:11 -060050 return err;
51 }
52 value = err;
53
54 if (level == 0) {
55 value &= ~(1 << gpio);
56 l = "low";
57 } else {
58 value |= 1 << gpio;
59 l = "high";
60 }
61
62 err = pmic_reg_write(pmic, AS3722_GPIO_SIGNAL_OUT, value);
63 if (err) {
Simon Glass73126ac2018-11-18 08:14:28 -070064 pr_err("failed to set GPIO#%u %s: %d\n", gpio, l, err);
Simon Glassc30ddcc2017-07-25 08:30:11 -060065 return err;
66 }
67
68 return 0;
69}
70
71int as3722_gpio_direction_output(struct udevice *dev, unsigned int gpio,
72 int value)
73{
74 struct udevice *pmic = dev_get_parent(dev);
75 int err;
76
77 if (gpio > 7)
78 return -EINVAL;
79
80 if (value == 0)
81 value = AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDL;
82 else
83 value = AS3722_GPIO_CONTROL_MODE_OUTPUT_VDDH;
84
85 err = pmic_reg_write(pmic, AS3722_GPIO_CONTROL(gpio), value);
86 if (err) {
Simon Glass73126ac2018-11-18 08:14:28 -070087 pr_err("failed to configure GPIO#%u as output: %d\n", gpio,
88 err);
Simon Glassc30ddcc2017-07-25 08:30:11 -060089 return err;
90 }
91
92 err = as3722_gpio_set_value(pmic, gpio, value);
93 if (err < 0) {
Simon Glass73126ac2018-11-18 08:14:28 -070094 pr_err("failed to set GPIO#%u high: %d\n", gpio, err);
Simon Glassc30ddcc2017-07-25 08:30:11 -060095 return err;
96 }
97
98 return 0;
99}
100
101static int as3722_gpio_probe(struct udevice *dev)
102{
103 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
104
105 uc_priv->gpio_count = NUM_GPIOS;
106 uc_priv->bank_name = "as3722_";
107
108 return 0;
109}
110
111static const struct dm_gpio_ops gpio_as3722_ops = {
112 .direction_output = as3722_gpio_direction_output,
113 .set_value = as3722_gpio_set_value,
114};
115
116U_BOOT_DRIVER(gpio_as3722) = {
117 .name = "gpio_as3722",
118 .id = UCLASS_GPIO,
119 .ops = &gpio_as3722_ops,
120 .probe = as3722_gpio_probe,
121};