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