Tero Kristo | daec341 | 2019-10-24 15:00:48 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 3 | * Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/ |
Tero Kristo | daec341 | 2019-10-24 15:00:48 +0530 | [diff] [blame] | 4 | * Tero Kristo <t-kristo@ti.com> |
| 5 | */ |
| 6 | |
Tero Kristo | daec341 | 2019-10-24 15:00:48 +0530 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <i2c.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <dm/device_compat.h> |
Tero Kristo | daec341 | 2019-10-24 15:00:48 +0530 | [diff] [blame] | 10 | #include <power/regulator.h> |
| 11 | |
| 12 | #define TPS62360_REG_SET0 0 |
| 13 | |
| 14 | #define TPS62360_I2C_CHIP 0x60 |
| 15 | |
| 16 | #define TPS62360_VSEL_STEPSIZE 10000 /* In uV */ |
| 17 | |
| 18 | struct tps62360_regulator_config { |
| 19 | u32 vmin; |
| 20 | u32 vmax; |
| 21 | }; |
| 22 | |
| 23 | struct tps62360_regulator_pdata { |
| 24 | u8 vsel_offset; |
| 25 | struct udevice *i2c; |
| 26 | struct tps62360_regulator_config *config; |
| 27 | }; |
| 28 | |
| 29 | /* |
| 30 | * TPS62362/TPS62363 are just re-using these values for now, their preset |
| 31 | * voltage values are just different compared to TPS62360/TPS62361. |
| 32 | */ |
| 33 | static struct tps62360_regulator_config tps62360_data = { |
| 34 | .vmin = 770000, |
| 35 | .vmax = 1400000, |
| 36 | }; |
| 37 | |
| 38 | static struct tps62360_regulator_config tps62361_data = { |
| 39 | .vmin = 500000, |
| 40 | .vmax = 1770000, |
| 41 | }; |
| 42 | |
| 43 | static int tps62360_regulator_set_value(struct udevice *dev, int uV) |
| 44 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 45 | struct tps62360_regulator_pdata *pdata = dev_get_plat(dev); |
Tero Kristo | daec341 | 2019-10-24 15:00:48 +0530 | [diff] [blame] | 46 | u8 regval; |
| 47 | |
| 48 | if (uV < pdata->config->vmin || uV > pdata->config->vmax) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | uV -= pdata->config->vmin; |
| 52 | |
| 53 | uV = DIV_ROUND_UP(uV, TPS62360_VSEL_STEPSIZE); |
| 54 | |
| 55 | if (uV > U8_MAX) |
| 56 | return -EINVAL; |
| 57 | |
| 58 | regval = (u8)uV; |
| 59 | |
| 60 | return dm_i2c_write(pdata->i2c, TPS62360_REG_SET0 + pdata->vsel_offset, |
| 61 | ®val, 1); |
| 62 | } |
| 63 | |
| 64 | static int tps62360_regulator_get_value(struct udevice *dev) |
| 65 | { |
| 66 | u8 regval; |
| 67 | int ret; |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 68 | struct tps62360_regulator_pdata *pdata = dev_get_plat(dev); |
Tero Kristo | daec341 | 2019-10-24 15:00:48 +0530 | [diff] [blame] | 69 | |
| 70 | ret = dm_i2c_read(pdata->i2c, TPS62360_REG_SET0 + pdata->vsel_offset, |
| 71 | ®val, 1); |
| 72 | if (ret) { |
| 73 | dev_err(dev, "i2c read failed: %d\n", ret); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | return (u32)regval * TPS62360_VSEL_STEPSIZE + pdata->config->vmin; |
| 78 | } |
| 79 | |
Keerthy | cdd05ef | 2020-01-10 14:52:05 +0530 | [diff] [blame] | 80 | static int tps62360_regulator_probe(struct udevice *dev) |
Tero Kristo | daec341 | 2019-10-24 15:00:48 +0530 | [diff] [blame] | 81 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 82 | struct tps62360_regulator_pdata *pdata = dev_get_plat(dev); |
Tero Kristo | daec341 | 2019-10-24 15:00:48 +0530 | [diff] [blame] | 83 | u8 vsel0; |
| 84 | u8 vsel1; |
| 85 | int ret; |
| 86 | |
| 87 | pdata->config = (void *)dev_get_driver_data(dev); |
| 88 | |
| 89 | vsel0 = dev_read_bool(dev, "ti,vsel0-state-high"); |
| 90 | vsel1 = dev_read_bool(dev, "ti,vsel1-state-high"); |
| 91 | |
| 92 | pdata->vsel_offset = vsel0 + vsel1 * 2; |
| 93 | |
| 94 | ret = i2c_get_chip(dev->parent, TPS62360_I2C_CHIP, 1, &pdata->i2c); |
| 95 | if (ret) { |
| 96 | dev_err(dev, "i2c dev get failed.\n"); |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static const struct dm_regulator_ops tps62360_regulator_ops = { |
| 104 | .get_value = tps62360_regulator_get_value, |
| 105 | .set_value = tps62360_regulator_set_value, |
| 106 | }; |
| 107 | |
| 108 | static const struct udevice_id tps62360_regulator_ids[] = { |
| 109 | { .compatible = "ti,tps62360", .data = (ulong)&tps62360_data }, |
| 110 | { .compatible = "ti,tps62361", .data = (ulong)&tps62361_data }, |
| 111 | { .compatible = "ti,tps62362", .data = (ulong)&tps62360_data }, |
| 112 | { .compatible = "ti,tps62363", .data = (ulong)&tps62361_data }, |
| 113 | { }, |
| 114 | }; |
| 115 | |
| 116 | U_BOOT_DRIVER(tps62360_regulator) = { |
| 117 | .name = "tps62360_regulator", |
| 118 | .id = UCLASS_REGULATOR, |
| 119 | .ops = &tps62360_regulator_ops, |
| 120 | .of_match = tps62360_regulator_ids, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 121 | .plat_auto = sizeof(struct tps62360_regulator_pdata), |
Keerthy | cdd05ef | 2020-01-10 14:52:05 +0530 | [diff] [blame] | 122 | .probe = tps62360_regulator_probe, |
Tero Kristo | daec341 | 2019-10-24 15:00:48 +0530 | [diff] [blame] | 123 | }; |