Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c30ddcc | 2017-07-25 08:30:11 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 NVIDIA Corporation |
Simon Glass | c30ddcc | 2017-07-25 08:30:11 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <asm/gpio.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 9 | #include <linux/printk.h> |
Simon Glass | c30ddcc | 2017-07-25 08:30:11 -0600 | [diff] [blame] | 10 | #include <power/as3722.h> |
| 11 | #include <power/pmic.h> |
| 12 | |
| 13 | #define NUM_GPIOS 8 |
| 14 | |
| 15 | int 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 Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 29 | pr_err("failed to configure GPIO#%u: %d\n", gpio, err); |
Simon Glass | c30ddcc | 2017-07-25 08:30:11 -0600 | [diff] [blame] | 30 | return err; |
| 31 | } |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static 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 Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 49 | pr_err("failed to read GPIO signal out register: %d\n", err); |
Simon Glass | c30ddcc | 2017-07-25 08:30:11 -0600 | [diff] [blame] | 50 | 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 Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 64 | pr_err("failed to set GPIO#%u %s: %d\n", gpio, l, err); |
Simon Glass | c30ddcc | 2017-07-25 08:30:11 -0600 | [diff] [blame] | 65 | return err; |
| 66 | } |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int 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 Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 87 | pr_err("failed to configure GPIO#%u as output: %d\n", gpio, |
| 88 | err); |
Simon Glass | c30ddcc | 2017-07-25 08:30:11 -0600 | [diff] [blame] | 89 | return err; |
| 90 | } |
| 91 | |
| 92 | err = as3722_gpio_set_value(pmic, gpio, value); |
| 93 | if (err < 0) { |
Simon Glass | 73126ac | 2018-11-18 08:14:28 -0700 | [diff] [blame] | 94 | pr_err("failed to set GPIO#%u high: %d\n", gpio, err); |
Simon Glass | c30ddcc | 2017-07-25 08:30:11 -0600 | [diff] [blame] | 95 | return err; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static 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 | |
| 111 | static const struct dm_gpio_ops gpio_as3722_ops = { |
| 112 | .direction_output = as3722_gpio_direction_output, |
| 113 | .set_value = as3722_gpio_set_value, |
| 114 | }; |
| 115 | |
| 116 | U_BOOT_DRIVER(gpio_as3722) = { |
| 117 | .name = "gpio_as3722", |
| 118 | .id = UCLASS_GPIO, |
| 119 | .ops = &gpio_as3722_ops, |
| 120 | .probe = as3722_gpio_probe, |
| 121 | }; |