Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Regulator driver for National Semiconductors LP3971 PMIC chip |
| 4 | * |
| 5 | * Copyright (C) 2009 Samsung Electronics |
| 6 | * Author: Marek Szyprowski <m.szyprowski@samsung.com> |
| 7 | * |
| 8 | * Based on wm8350.c |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/bug.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/kernel.h> |
Paul Gortmaker | 65602c3 | 2011-07-17 16:28:23 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 16 | #include <linux/regulator/driver.h> |
| 17 | #include <linux/regulator/lp3971.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 19 | |
| 20 | struct lp3971 { |
| 21 | struct device *dev; |
| 22 | struct mutex io_lock; |
| 23 | struct i2c_client *i2c; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg); |
| 27 | static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val); |
| 28 | |
| 29 | #define LP3971_SYS_CONTROL1_REG 0x07 |
| 30 | |
| 31 | /* System control register 1 initial value, |
| 32 | bits 4 and 5 are EPROM programmable */ |
| 33 | #define SYS_CONTROL1_INIT_VAL 0x40 |
| 34 | #define SYS_CONTROL1_INIT_MASK 0xCF |
| 35 | |
| 36 | #define LP3971_BUCK_VOL_ENABLE_REG 0x10 |
| 37 | #define LP3971_BUCK_VOL_CHANGE_REG 0x20 |
| 38 | |
| 39 | /* Voltage control registers shift: |
| 40 | LP3971_BUCK1 -> 0 |
| 41 | LP3971_BUCK2 -> 4 |
| 42 | LP3971_BUCK3 -> 6 |
| 43 | */ |
Axel Lin | 451a73cd | 2010-03-11 09:50:07 +0800 | [diff] [blame] | 44 | #define BUCK_VOL_CHANGE_SHIFT(x) (((!!x) << 2) | (x & ~0x01)) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 45 | #define BUCK_VOL_CHANGE_FLAG_GO 0x01 |
| 46 | #define BUCK_VOL_CHANGE_FLAG_TARGET 0x02 |
| 47 | #define BUCK_VOL_CHANGE_FLAG_MASK 0x03 |
| 48 | |
| 49 | #define LP3971_BUCK1_BASE 0x23 |
| 50 | #define LP3971_BUCK2_BASE 0x29 |
| 51 | #define LP3971_BUCK3_BASE 0x32 |
| 52 | |
Tobias Klauser | 6faa7e0 | 2009-12-23 14:13:17 +0100 | [diff] [blame] | 53 | static const int buck_base_addr[] = { |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 54 | LP3971_BUCK1_BASE, |
| 55 | LP3971_BUCK2_BASE, |
| 56 | LP3971_BUCK3_BASE, |
| 57 | }; |
| 58 | |
| 59 | #define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x]) |
| 60 | #define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1) |
| 61 | |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 62 | static const unsigned int buck_voltage_map[] = { |
| 63 | 0, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000, |
| 64 | 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000, |
| 65 | 1550000, 1600000, 1650000, 1700000, 1800000, 1900000, 2500000, 2800000, |
| 66 | 3000000, 3300000, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | #define BUCK_TARGET_VOL_MASK 0x3f |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 70 | |
| 71 | #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2) |
| 72 | |
| 73 | #define LP3971_LDO_ENABLE_REG 0x12 |
| 74 | #define LP3971_LDO_VOL_CONTR_BASE 0x39 |
| 75 | |
| 76 | /* Voltage control registers: |
| 77 | LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0 |
| 78 | LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0 |
| 79 | LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1 |
| 80 | LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1 |
| 81 | LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2 |
| 82 | */ |
| 83 | #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1)) |
| 84 | |
| 85 | /* Voltage control registers shift: |
| 86 | LP3971_LDO1 -> 0, LP3971_LDO2 -> 4 |
| 87 | LP3971_LDO3 -> 0, LP3971_LDO4 -> 4 |
| 88 | LP3971_LDO5 -> 0 |
| 89 | */ |
| 90 | #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2) |
| 91 | #define LDO_VOL_CONTR_MASK 0x0f |
| 92 | |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 93 | static const unsigned int ldo45_voltage_map[] = { |
| 94 | 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000, |
| 95 | 1400000, 1500000, 1800000, 1900000, 2500000, 2800000, 3000000, 3300000, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 98 | static const unsigned int ldo123_voltage_map[] = { |
| 99 | 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000, |
| 100 | 2600000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 103 | #define LDO_VOL_MIN_IDX 0x00 |
| 104 | #define LDO_VOL_MAX_IDX 0x0f |
| 105 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 106 | static int lp3971_ldo_is_enabled(struct regulator_dev *dev) |
| 107 | { |
| 108 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 109 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 110 | u16 mask = 1 << (1 + ldo); |
| 111 | u16 val; |
| 112 | |
| 113 | val = lp3971_reg_read(lp3971, LP3971_LDO_ENABLE_REG); |
| 114 | return (val & mask) != 0; |
| 115 | } |
| 116 | |
| 117 | static int lp3971_ldo_enable(struct regulator_dev *dev) |
| 118 | { |
| 119 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 120 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 121 | u16 mask = 1 << (1 + ldo); |
| 122 | |
| 123 | return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, mask); |
| 124 | } |
| 125 | |
| 126 | static int lp3971_ldo_disable(struct regulator_dev *dev) |
| 127 | { |
| 128 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 129 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 130 | u16 mask = 1 << (1 + ldo); |
| 131 | |
| 132 | return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, 0); |
| 133 | } |
| 134 | |
Axel Lin | f38482f | 2012-12-10 19:46:52 +0800 | [diff] [blame] | 135 | static int lp3971_ldo_get_voltage_sel(struct regulator_dev *dev) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 136 | { |
| 137 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 138 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 139 | u16 val, reg; |
| 140 | |
| 141 | reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo)); |
| 142 | val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK; |
| 143 | |
Axel Lin | f38482f | 2012-12-10 19:46:52 +0800 | [diff] [blame] | 144 | return val; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 145 | } |
| 146 | |
Axel Lin | dd8e231 | 2012-03-23 10:49:54 +0800 | [diff] [blame] | 147 | static int lp3971_ldo_set_voltage_sel(struct regulator_dev *dev, |
| 148 | unsigned int selector) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 149 | { |
| 150 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 151 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 152 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 153 | return lp3971_set_bits(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo), |
Axel Lin | cdb868f | 2010-03-09 16:53:59 +0800 | [diff] [blame] | 154 | LDO_VOL_CONTR_MASK << LDO_VOL_CONTR_SHIFT(ldo), |
Axel Lin | dd8e231 | 2012-03-23 10:49:54 +0800 | [diff] [blame] | 155 | selector << LDO_VOL_CONTR_SHIFT(ldo)); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Axel Lin | 93b84ea | 2019-01-26 11:39:00 +0800 | [diff] [blame] | 158 | static const struct regulator_ops lp3971_ldo_ops = { |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 159 | .list_voltage = regulator_list_voltage_table, |
Axel Lin | 3e65561 | 2013-04-21 00:35:31 +0800 | [diff] [blame] | 160 | .map_voltage = regulator_map_voltage_ascend, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 161 | .is_enabled = lp3971_ldo_is_enabled, |
| 162 | .enable = lp3971_ldo_enable, |
| 163 | .disable = lp3971_ldo_disable, |
Axel Lin | f38482f | 2012-12-10 19:46:52 +0800 | [diff] [blame] | 164 | .get_voltage_sel = lp3971_ldo_get_voltage_sel, |
Axel Lin | dd8e231 | 2012-03-23 10:49:54 +0800 | [diff] [blame] | 165 | .set_voltage_sel = lp3971_ldo_set_voltage_sel, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 166 | }; |
| 167 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 168 | static int lp3971_dcdc_is_enabled(struct regulator_dev *dev) |
| 169 | { |
| 170 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 171 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
| 172 | u16 mask = 1 << (buck * 2); |
| 173 | u16 val; |
| 174 | |
| 175 | val = lp3971_reg_read(lp3971, LP3971_BUCK_VOL_ENABLE_REG); |
| 176 | return (val & mask) != 0; |
| 177 | } |
| 178 | |
| 179 | static int lp3971_dcdc_enable(struct regulator_dev *dev) |
| 180 | { |
| 181 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 182 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
| 183 | u16 mask = 1 << (buck * 2); |
| 184 | |
| 185 | return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, mask); |
| 186 | } |
| 187 | |
| 188 | static int lp3971_dcdc_disable(struct regulator_dev *dev) |
| 189 | { |
| 190 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 191 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
| 192 | u16 mask = 1 << (buck * 2); |
| 193 | |
| 194 | return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, 0); |
| 195 | } |
| 196 | |
Axel Lin | f38482f | 2012-12-10 19:46:52 +0800 | [diff] [blame] | 197 | static int lp3971_dcdc_get_voltage_sel(struct regulator_dev *dev) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 198 | { |
| 199 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 200 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
| 201 | u16 reg; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 202 | |
| 203 | reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck)); |
| 204 | reg &= BUCK_TARGET_VOL_MASK; |
| 205 | |
Axel Lin | f38482f | 2012-12-10 19:46:52 +0800 | [diff] [blame] | 206 | return reg; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Axel Lin | dd8e231 | 2012-03-23 10:49:54 +0800 | [diff] [blame] | 209 | static int lp3971_dcdc_set_voltage_sel(struct regulator_dev *dev, |
| 210 | unsigned int selector) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 211 | { |
| 212 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 213 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 214 | int ret; |
| 215 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 216 | ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck), |
Axel Lin | dd8e231 | 2012-03-23 10:49:54 +0800 | [diff] [blame] | 217 | BUCK_TARGET_VOL_MASK, selector); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 218 | if (ret) |
| 219 | return ret; |
| 220 | |
| 221 | ret = lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG, |
| 222 | BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck), |
| 223 | BUCK_VOL_CHANGE_FLAG_GO << BUCK_VOL_CHANGE_SHIFT(buck)); |
| 224 | if (ret) |
| 225 | return ret; |
| 226 | |
| 227 | return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG, |
| 228 | BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck), |
| 229 | 0 << BUCK_VOL_CHANGE_SHIFT(buck)); |
| 230 | } |
| 231 | |
Axel Lin | 93b84ea | 2019-01-26 11:39:00 +0800 | [diff] [blame] | 232 | static const struct regulator_ops lp3971_dcdc_ops = { |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 233 | .list_voltage = regulator_list_voltage_table, |
Axel Lin | 3e65561 | 2013-04-21 00:35:31 +0800 | [diff] [blame] | 234 | .map_voltage = regulator_map_voltage_ascend, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 235 | .is_enabled = lp3971_dcdc_is_enabled, |
| 236 | .enable = lp3971_dcdc_enable, |
| 237 | .disable = lp3971_dcdc_disable, |
Axel Lin | f38482f | 2012-12-10 19:46:52 +0800 | [diff] [blame] | 238 | .get_voltage_sel = lp3971_dcdc_get_voltage_sel, |
Axel Lin | dd8e231 | 2012-03-23 10:49:54 +0800 | [diff] [blame] | 239 | .set_voltage_sel = lp3971_dcdc_set_voltage_sel, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 240 | }; |
| 241 | |
Axel Lin | 14add4f | 2012-04-05 11:59:20 +0800 | [diff] [blame] | 242 | static const struct regulator_desc regulators[] = { |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 243 | { |
| 244 | .name = "LDO1", |
| 245 | .id = LP3971_LDO1, |
| 246 | .ops = &lp3971_ldo_ops, |
| 247 | .n_voltages = ARRAY_SIZE(ldo123_voltage_map), |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 248 | .volt_table = ldo123_voltage_map, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 249 | .type = REGULATOR_VOLTAGE, |
| 250 | .owner = THIS_MODULE, |
| 251 | }, |
| 252 | { |
| 253 | .name = "LDO2", |
| 254 | .id = LP3971_LDO2, |
| 255 | .ops = &lp3971_ldo_ops, |
| 256 | .n_voltages = ARRAY_SIZE(ldo123_voltage_map), |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 257 | .volt_table = ldo123_voltage_map, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 258 | .type = REGULATOR_VOLTAGE, |
| 259 | .owner = THIS_MODULE, |
| 260 | }, |
| 261 | { |
| 262 | .name = "LDO3", |
| 263 | .id = LP3971_LDO3, |
| 264 | .ops = &lp3971_ldo_ops, |
| 265 | .n_voltages = ARRAY_SIZE(ldo123_voltage_map), |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 266 | .volt_table = ldo123_voltage_map, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 267 | .type = REGULATOR_VOLTAGE, |
| 268 | .owner = THIS_MODULE, |
| 269 | }, |
| 270 | { |
| 271 | .name = "LDO4", |
| 272 | .id = LP3971_LDO4, |
| 273 | .ops = &lp3971_ldo_ops, |
| 274 | .n_voltages = ARRAY_SIZE(ldo45_voltage_map), |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 275 | .volt_table = ldo45_voltage_map, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 276 | .type = REGULATOR_VOLTAGE, |
| 277 | .owner = THIS_MODULE, |
| 278 | }, |
| 279 | { |
| 280 | .name = "LDO5", |
| 281 | .id = LP3971_LDO5, |
| 282 | .ops = &lp3971_ldo_ops, |
| 283 | .n_voltages = ARRAY_SIZE(ldo45_voltage_map), |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 284 | .volt_table = ldo45_voltage_map, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 285 | .type = REGULATOR_VOLTAGE, |
| 286 | .owner = THIS_MODULE, |
| 287 | }, |
| 288 | { |
| 289 | .name = "DCDC1", |
| 290 | .id = LP3971_DCDC1, |
| 291 | .ops = &lp3971_dcdc_ops, |
| 292 | .n_voltages = ARRAY_SIZE(buck_voltage_map), |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 293 | .volt_table = buck_voltage_map, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 294 | .type = REGULATOR_VOLTAGE, |
| 295 | .owner = THIS_MODULE, |
| 296 | }, |
| 297 | { |
| 298 | .name = "DCDC2", |
| 299 | .id = LP3971_DCDC2, |
| 300 | .ops = &lp3971_dcdc_ops, |
| 301 | .n_voltages = ARRAY_SIZE(buck_voltage_map), |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 302 | .volt_table = buck_voltage_map, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 303 | .type = REGULATOR_VOLTAGE, |
| 304 | .owner = THIS_MODULE, |
| 305 | }, |
| 306 | { |
| 307 | .name = "DCDC3", |
| 308 | .id = LP3971_DCDC3, |
| 309 | .ops = &lp3971_dcdc_ops, |
| 310 | .n_voltages = ARRAY_SIZE(buck_voltage_map), |
Axel Lin | cad8d76 | 2012-05-20 13:30:28 +0800 | [diff] [blame] | 311 | .volt_table = buck_voltage_map, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 312 | .type = REGULATOR_VOLTAGE, |
| 313 | .owner = THIS_MODULE, |
| 314 | }, |
| 315 | }; |
| 316 | |
| 317 | static int lp3971_i2c_read(struct i2c_client *i2c, char reg, int count, |
| 318 | u16 *dest) |
| 319 | { |
| 320 | int ret; |
| 321 | |
| 322 | if (count != 1) |
| 323 | return -EIO; |
| 324 | ret = i2c_smbus_read_byte_data(i2c, reg); |
Axel Lin | 27ef7f0 | 2010-08-06 08:09:14 +0800 | [diff] [blame] | 325 | if (ret < 0) |
Sachin Kamat | a1985d4 | 2014-02-18 16:11:03 +0530 | [diff] [blame] | 326 | return ret; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 327 | |
| 328 | *dest = ret; |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count, |
| 333 | const u16 *src) |
| 334 | { |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 335 | if (count != 1) |
| 336 | return -EIO; |
Axel Lin | 1bddc2f | 2010-08-09 17:01:21 +0800 | [diff] [blame] | 337 | return i2c_smbus_write_byte_data(i2c, reg, *src); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg) |
| 341 | { |
| 342 | u16 val = 0; |
| 343 | |
| 344 | mutex_lock(&lp3971->io_lock); |
| 345 | |
| 346 | lp3971_i2c_read(lp3971->i2c, reg, 1, &val); |
| 347 | |
| 348 | dev_dbg(lp3971->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg, |
| 349 | (unsigned)val&0xff); |
| 350 | |
| 351 | mutex_unlock(&lp3971->io_lock); |
| 352 | |
| 353 | return val & 0xff; |
| 354 | } |
| 355 | |
| 356 | static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val) |
| 357 | { |
| 358 | u16 tmp; |
| 359 | int ret; |
| 360 | |
| 361 | mutex_lock(&lp3971->io_lock); |
| 362 | |
| 363 | ret = lp3971_i2c_read(lp3971->i2c, reg, 1, &tmp); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 364 | if (ret == 0) { |
Dan Carpenter | 40e1d79 | 2016-04-14 12:34:41 +0300 | [diff] [blame] | 365 | tmp = (tmp & ~mask) | val; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 366 | ret = lp3971_i2c_write(lp3971->i2c, reg, 1, &tmp); |
| 367 | dev_dbg(lp3971->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg, |
| 368 | (unsigned)val&0xff); |
| 369 | } |
| 370 | mutex_unlock(&lp3971->io_lock); |
| 371 | |
| 372 | return ret; |
| 373 | } |
| 374 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 375 | static int setup_regulators(struct lp3971 *lp3971, |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 376 | struct lp3971_platform_data *pdata) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 377 | { |
| 378 | int i, err; |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 379 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 380 | /* Instantiate the regulators */ |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 381 | for (i = 0; i < pdata->num_regulators; i++) { |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 382 | struct regulator_config config = { }; |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 383 | struct lp3971_regulator_subdev *reg = &pdata->regulators[i]; |
Axel Lin | 56dde80 | 2013-11-13 14:52:26 +0800 | [diff] [blame] | 384 | struct regulator_dev *rdev; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 385 | |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 386 | config.dev = lp3971->dev; |
| 387 | config.init_data = reg->initdata; |
| 388 | config.driver_data = lp3971; |
| 389 | |
Axel Lin | 56dde80 | 2013-11-13 14:52:26 +0800 | [diff] [blame] | 390 | rdev = devm_regulator_register(lp3971->dev, |
| 391 | ®ulators[reg->id], &config); |
| 392 | if (IS_ERR(rdev)) { |
| 393 | err = PTR_ERR(rdev); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 394 | dev_err(lp3971->dev, "regulator init failed: %d\n", |
| 395 | err); |
Axel Lin | 56dde80 | 2013-11-13 14:52:26 +0800 | [diff] [blame] | 396 | return err; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | return 0; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Axel Lin | 77e2959 | 2020-01-09 23:58:08 +0800 | [diff] [blame] | 403 | static int lp3971_i2c_probe(struct i2c_client *i2c) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 404 | { |
| 405 | struct lp3971 *lp3971; |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 406 | struct lp3971_platform_data *pdata = dev_get_platdata(&i2c->dev); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 407 | int ret; |
| 408 | u16 val; |
| 409 | |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 410 | if (!pdata) { |
| 411 | dev_dbg(&i2c->dev, "No platform init data supplied\n"); |
| 412 | return -ENODEV; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Nikolay Balandin | 2af0af6 | 2013-05-25 14:50:40 -0700 | [diff] [blame] | 415 | lp3971 = devm_kzalloc(&i2c->dev, sizeof(struct lp3971), GFP_KERNEL); |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 416 | if (lp3971 == NULL) |
| 417 | return -ENOMEM; |
| 418 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 419 | lp3971->i2c = i2c; |
| 420 | lp3971->dev = &i2c->dev; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 421 | |
| 422 | mutex_init(&lp3971->io_lock); |
| 423 | |
| 424 | /* Detect LP3971 */ |
| 425 | ret = lp3971_i2c_read(i2c, LP3971_SYS_CONTROL1_REG, 1, &val); |
| 426 | if (ret == 0 && (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL) |
| 427 | ret = -ENODEV; |
| 428 | if (ret < 0) { |
| 429 | dev_err(&i2c->dev, "failed to detect device\n"); |
Nikolay Balandin | 2af0af6 | 2013-05-25 14:50:40 -0700 | [diff] [blame] | 430 | return ret; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 433 | ret = setup_regulators(lp3971, pdata); |
| 434 | if (ret < 0) |
Nikolay Balandin | 2af0af6 | 2013-05-25 14:50:40 -0700 | [diff] [blame] | 435 | return ret; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 436 | |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 437 | i2c_set_clientdata(i2c, lp3971); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 438 | return 0; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 441 | static const struct i2c_device_id lp3971_i2c_id[] = { |
Jingoo Han | 0a3ee93 | 2013-10-14 17:47:31 +0900 | [diff] [blame] | 442 | { "lp3971", 0 }, |
| 443 | { } |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 444 | }; |
| 445 | MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id); |
| 446 | |
| 447 | static struct i2c_driver lp3971_i2c_driver = { |
| 448 | .driver = { |
| 449 | .name = "LP3971", |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 450 | }, |
Axel Lin | 77e2959 | 2020-01-09 23:58:08 +0800 | [diff] [blame] | 451 | .probe_new = lp3971_i2c_probe, |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 452 | .id_table = lp3971_i2c_id, |
| 453 | }; |
| 454 | |
Mark Brown | 5af34e6 | 2012-04-03 23:33:10 +0100 | [diff] [blame] | 455 | module_i2c_driver(lp3971_i2c_driver); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 456 | |
| 457 | MODULE_LICENSE("GPL"); |
| 458 | MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>"); |
| 459 | MODULE_DESCRIPTION("LP3971 PMIC driver"); |