1 /* 2 * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com> 3 * Keerthy <j-keerthy@ti.com> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <fdtdec.h> 10 #include <errno.h> 11 #include <dm.h> 12 #include <i2c.h> 13 #include <asm/gpio.h> 14 #include <power/pmic.h> 15 #include <power/regulator.h> 16 17 #define GPIO_REGULATOR_MAX_STATES 2 18 19 DECLARE_GLOBAL_DATA_PTR; 20 21 struct gpio_regulator_platdata { 22 struct gpio_desc gpio; /* GPIO for regulator voltage control */ 23 int states[GPIO_REGULATOR_MAX_STATES]; 24 int voltages[GPIO_REGULATOR_MAX_STATES]; 25 }; 26 27 static int gpio_regulator_ofdata_to_platdata(struct udevice *dev) 28 { 29 struct dm_regulator_uclass_platdata *uc_pdata; 30 struct gpio_regulator_platdata *dev_pdata; 31 struct gpio_desc *gpio; 32 const void *blob = gd->fdt_blob; 33 int ret, count, i, j; 34 u32 states_array[8]; 35 36 dev_pdata = dev_get_platdata(dev); 37 uc_pdata = dev_get_uclass_platdata(dev); 38 if (!uc_pdata) 39 return -ENXIO; 40 41 /* Set type to gpio */ 42 uc_pdata->type = REGULATOR_TYPE_GPIO; 43 44 /* 45 * Get gpio regulator gpio desc 46 * Assuming one GPIO per regulator. 47 * Can be extended later to multiple GPIOs 48 * per gpio-regulator. As of now no instance with multiple 49 * gpios is presnt 50 */ 51 gpio = &dev_pdata->gpio; 52 ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT); 53 if (ret) 54 debug("regulator gpio - not found! Error: %d", ret); 55 56 blob = dev_read_prop(dev, "states", &count); 57 if (!blob) 58 return -EINVAL; 59 60 ret = dev_read_u32_array(dev, "states", states_array, count / 4); 61 if (ret) 62 return -EINVAL; 63 64 for (i = 0, j = 0; i < count / 4; i += 2) { 65 dev_pdata->voltages[j] = states_array[i]; 66 dev_pdata->states[j] = states_array[i + 1]; 67 j++; 68 } 69 70 return 0; 71 } 72 73 static int gpio_regulator_get_value(struct udevice *dev) 74 { 75 struct dm_regulator_uclass_platdata *uc_pdata; 76 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev); 77 int enable; 78 79 if (!dev_pdata->gpio.dev) 80 return -ENOSYS; 81 82 uc_pdata = dev_get_uclass_platdata(dev); 83 if (uc_pdata->min_uV > uc_pdata->max_uV) { 84 debug("Invalid constraints for: %s\n", uc_pdata->name); 85 return -EINVAL; 86 } 87 88 enable = dm_gpio_get_value(&dev_pdata->gpio); 89 if (enable == dev_pdata->states[0]) 90 return dev_pdata->voltages[0]; 91 else 92 return dev_pdata->voltages[1]; 93 } 94 95 static int gpio_regulator_set_value(struct udevice *dev, int uV) 96 { 97 struct gpio_regulator_platdata *dev_pdata = dev_get_platdata(dev); 98 int ret; 99 bool enable; 100 101 if (!dev_pdata->gpio.dev) 102 return -ENOSYS; 103 104 if (uV == dev_pdata->voltages[0]) 105 enable = dev_pdata->states[0]; 106 else if (uV == dev_pdata->voltages[1]) 107 enable = dev_pdata->states[1]; 108 else 109 return -EINVAL; 110 111 ret = dm_gpio_set_value(&dev_pdata->gpio, enable); 112 if (ret) { 113 pr_err("Can't set regulator : %s gpio to: %d\n", dev->name, 114 enable); 115 return ret; 116 } 117 118 return 0; 119 } 120 121 static const struct dm_regulator_ops gpio_regulator_ops = { 122 .get_value = gpio_regulator_get_value, 123 .set_value = gpio_regulator_set_value, 124 }; 125 126 static const struct udevice_id gpio_regulator_ids[] = { 127 { .compatible = "regulator-gpio" }, 128 { }, 129 }; 130 131 U_BOOT_DRIVER(gpio_regulator) = { 132 .name = "gpio regulator", 133 .id = UCLASS_REGULATOR, 134 .ops = &gpio_regulator_ops, 135 .of_match = gpio_regulator_ids, 136 .ofdata_to_platdata = gpio_regulator_ofdata_to_platdata, 137 .platdata_auto_alloc_size = sizeof(struct gpio_regulator_platdata), 138 }; 139