Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 2 | /* |
| 3 | * linux/drivers/regulator/aat2870-regulator.c |
| 4 | * |
| 5 | * Copyright (c) 2011, NVIDIA Corporation. |
| 6 | * Author: Jin Park <jinyoungp@nvidia.com> |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/err.h> |
Randy Dunlap | 3a7d021 | 2011-07-30 06:13:09 +0200 | [diff] [blame] | 12 | #include <linux/module.h> |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/regulator/driver.h> |
| 16 | #include <linux/regulator/machine.h> |
| 17 | #include <linux/mfd/aat2870.h> |
| 18 | |
| 19 | struct aat2870_regulator { |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 20 | struct aat2870_data *aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 21 | struct regulator_desc desc; |
| 22 | |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 23 | u8 enable_addr; |
| 24 | u8 enable_shift; |
| 25 | u8 enable_mask; |
| 26 | |
| 27 | u8 voltage_addr; |
| 28 | u8 voltage_shift; |
| 29 | u8 voltage_mask; |
| 30 | }; |
| 31 | |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 32 | static int aat2870_ldo_set_voltage_sel(struct regulator_dev *rdev, |
| 33 | unsigned selector) |
| 34 | { |
| 35 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 36 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 37 | |
| 38 | return aat2870->update(aat2870, ri->voltage_addr, ri->voltage_mask, |
Axel Lin | a5228d2 | 2011-11-28 14:07:48 +0800 | [diff] [blame] | 39 | selector << ri->voltage_shift); |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static int aat2870_ldo_get_voltage_sel(struct regulator_dev *rdev) |
| 43 | { |
| 44 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 45 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 46 | u8 val; |
| 47 | int ret; |
| 48 | |
| 49 | ret = aat2870->read(aat2870, ri->voltage_addr, &val); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | |
| 53 | return (val & ri->voltage_mask) >> ri->voltage_shift; |
| 54 | } |
| 55 | |
| 56 | static int aat2870_ldo_enable(struct regulator_dev *rdev) |
| 57 | { |
| 58 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 59 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 60 | |
| 61 | return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, |
| 62 | ri->enable_mask); |
| 63 | } |
| 64 | |
| 65 | static int aat2870_ldo_disable(struct regulator_dev *rdev) |
| 66 | { |
| 67 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 68 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 69 | |
| 70 | return aat2870->update(aat2870, ri->enable_addr, ri->enable_mask, 0); |
| 71 | } |
| 72 | |
| 73 | static int aat2870_ldo_is_enabled(struct regulator_dev *rdev) |
| 74 | { |
| 75 | struct aat2870_regulator *ri = rdev_get_drvdata(rdev); |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 76 | struct aat2870_data *aat2870 = ri->aat2870; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 77 | u8 val; |
| 78 | int ret; |
| 79 | |
| 80 | ret = aat2870->read(aat2870, ri->enable_addr, &val); |
| 81 | if (ret) |
| 82 | return ret; |
| 83 | |
| 84 | return val & ri->enable_mask ? 1 : 0; |
| 85 | } |
| 86 | |
Bhumika Goyal | 2abf29a | 2017-01-28 19:00:52 +0530 | [diff] [blame] | 87 | static const struct regulator_ops aat2870_ldo_ops = { |
Axel Lin | 4506c6d | 2012-07-06 19:14:32 +0800 | [diff] [blame] | 88 | .list_voltage = regulator_list_voltage_table, |
Axel Lin | 08d6da2 | 2014-03-24 20:45:38 +0800 | [diff] [blame] | 89 | .map_voltage = regulator_map_voltage_ascend, |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 90 | .set_voltage_sel = aat2870_ldo_set_voltage_sel, |
| 91 | .get_voltage_sel = aat2870_ldo_get_voltage_sel, |
| 92 | .enable = aat2870_ldo_enable, |
| 93 | .disable = aat2870_ldo_disable, |
| 94 | .is_enabled = aat2870_ldo_is_enabled, |
| 95 | }; |
| 96 | |
Axel Lin | 4506c6d | 2012-07-06 19:14:32 +0800 | [diff] [blame] | 97 | static const unsigned int aat2870_ldo_voltages[] = { |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 98 | 1200000, 1300000, 1500000, 1600000, |
| 99 | 1800000, 2000000, 2200000, 2500000, |
| 100 | 2600000, 2700000, 2800000, 2900000, |
| 101 | 3000000, 3100000, 3200000, 3300000, |
| 102 | }; |
| 103 | |
| 104 | #define AAT2870_LDO(ids) \ |
| 105 | { \ |
| 106 | .desc = { \ |
| 107 | .name = #ids, \ |
| 108 | .id = AAT2870_ID_##ids, \ |
| 109 | .n_voltages = ARRAY_SIZE(aat2870_ldo_voltages), \ |
Axel Lin | 4506c6d | 2012-07-06 19:14:32 +0800 | [diff] [blame] | 110 | .volt_table = aat2870_ldo_voltages, \ |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 111 | .ops = &aat2870_ldo_ops, \ |
| 112 | .type = REGULATOR_VOLTAGE, \ |
| 113 | .owner = THIS_MODULE, \ |
| 114 | }, \ |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static struct aat2870_regulator aat2870_regulators[] = { |
| 118 | AAT2870_LDO(LDOA), |
| 119 | AAT2870_LDO(LDOB), |
| 120 | AAT2870_LDO(LDOC), |
| 121 | AAT2870_LDO(LDOD), |
| 122 | }; |
| 123 | |
| 124 | static struct aat2870_regulator *aat2870_get_regulator(int id) |
| 125 | { |
| 126 | struct aat2870_regulator *ri = NULL; |
| 127 | int i; |
| 128 | |
| 129 | for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) { |
| 130 | ri = &aat2870_regulators[i]; |
| 131 | if (ri->desc.id == id) |
| 132 | break; |
| 133 | } |
| 134 | |
Axel Lin | d4d6373 | 2011-11-28 14:06:31 +0800 | [diff] [blame] | 135 | if (i == ARRAY_SIZE(aat2870_regulators)) |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 136 | return NULL; |
| 137 | |
| 138 | ri->enable_addr = AAT2870_LDO_EN; |
| 139 | ri->enable_shift = id - AAT2870_ID_LDOA; |
| 140 | ri->enable_mask = 0x1 << ri->enable_shift; |
| 141 | |
| 142 | ri->voltage_addr = (id - AAT2870_ID_LDOA) / 2 ? |
| 143 | AAT2870_LDO_CD : AAT2870_LDO_AB; |
| 144 | ri->voltage_shift = (id - AAT2870_ID_LDOA) % 2 ? 0 : 4; |
| 145 | ri->voltage_mask = 0xF << ri->voltage_shift; |
| 146 | |
| 147 | return ri; |
| 148 | } |
| 149 | |
| 150 | static int aat2870_regulator_probe(struct platform_device *pdev) |
| 151 | { |
| 152 | struct aat2870_regulator *ri; |
Mark Brown | e4c5288 | 2012-09-10 12:07:23 +0800 | [diff] [blame] | 153 | struct regulator_config config = { }; |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 154 | struct regulator_dev *rdev; |
| 155 | |
| 156 | ri = aat2870_get_regulator(pdev->id); |
| 157 | if (!ri) { |
| 158 | dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id); |
| 159 | return -EINVAL; |
| 160 | } |
Axel Lin | b21bcd1 | 2012-03-02 09:28:44 +0800 | [diff] [blame] | 161 | ri->aat2870 = dev_get_drvdata(pdev->dev.parent); |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 162 | |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 163 | config.dev = &pdev->dev; |
| 164 | config.driver_data = ri; |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 165 | config.init_data = dev_get_platdata(&pdev->dev); |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 166 | |
Axel Lin | b1a613d | 2013-09-03 14:19:00 +0800 | [diff] [blame] | 167 | rdev = devm_regulator_register(&pdev->dev, &ri->desc, &config); |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 168 | if (IS_ERR(rdev)) { |
| 169 | dev_err(&pdev->dev, "Failed to register regulator %s\n", |
| 170 | ri->desc.name); |
| 171 | return PTR_ERR(rdev); |
| 172 | } |
| 173 | platform_set_drvdata(pdev, rdev); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 178 | static struct platform_driver aat2870_regulator_driver = { |
| 179 | .driver = { |
| 180 | .name = "aat2870-regulator", |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 181 | }, |
| 182 | .probe = aat2870_regulator_probe, |
Jin Park | f7eb6c5 | 2011-07-04 17:43:42 +0900 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | static int __init aat2870_regulator_init(void) |
| 186 | { |
| 187 | return platform_driver_register(&aat2870_regulator_driver); |
| 188 | } |
| 189 | subsys_initcall(aat2870_regulator_init); |
| 190 | |
| 191 | static void __exit aat2870_regulator_exit(void) |
| 192 | { |
| 193 | platform_driver_unregister(&aat2870_regulator_driver); |
| 194 | } |
| 195 | module_exit(aat2870_regulator_exit); |
| 196 | |
| 197 | MODULE_DESCRIPTION("AnalogicTech AAT2870 Regulator"); |
| 198 | MODULE_LICENSE("GPL"); |
| 199 | MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>"); |
Mark Brown | ad9c5ff | 2012-03-14 17:44:56 +0000 | [diff] [blame] | 200 | MODULE_ALIAS("platform:aat2870-regulator"); |