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> |
| 19 | #include <linux/regulator/driver.h> |
| 20 | #include <linux/regulator/lp3971.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 22 | |
| 23 | struct lp3971 { |
| 24 | struct device *dev; |
| 25 | struct mutex io_lock; |
| 26 | struct i2c_client *i2c; |
| 27 | int num_regulators; |
| 28 | struct regulator_dev **rdev; |
| 29 | }; |
| 30 | |
| 31 | static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg); |
| 32 | static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val); |
| 33 | |
| 34 | #define LP3971_SYS_CONTROL1_REG 0x07 |
| 35 | |
| 36 | /* System control register 1 initial value, |
| 37 | bits 4 and 5 are EPROM programmable */ |
| 38 | #define SYS_CONTROL1_INIT_VAL 0x40 |
| 39 | #define SYS_CONTROL1_INIT_MASK 0xCF |
| 40 | |
| 41 | #define LP3971_BUCK_VOL_ENABLE_REG 0x10 |
| 42 | #define LP3971_BUCK_VOL_CHANGE_REG 0x20 |
| 43 | |
| 44 | /* Voltage control registers shift: |
| 45 | LP3971_BUCK1 -> 0 |
| 46 | LP3971_BUCK2 -> 4 |
| 47 | LP3971_BUCK3 -> 6 |
| 48 | */ |
Axel Lin | 451a73c | 2010-03-11 09:50:07 +0800 | [diff] [blame] | 49 | #define BUCK_VOL_CHANGE_SHIFT(x) (((!!x) << 2) | (x & ~0x01)) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 50 | #define BUCK_VOL_CHANGE_FLAG_GO 0x01 |
| 51 | #define BUCK_VOL_CHANGE_FLAG_TARGET 0x02 |
| 52 | #define BUCK_VOL_CHANGE_FLAG_MASK 0x03 |
| 53 | |
| 54 | #define LP3971_BUCK1_BASE 0x23 |
| 55 | #define LP3971_BUCK2_BASE 0x29 |
| 56 | #define LP3971_BUCK3_BASE 0x32 |
| 57 | |
Tobias Klauser | 6faa7e0 | 2009-12-23 14:13:17 +0100 | [diff] [blame] | 58 | static const int buck_base_addr[] = { |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 59 | LP3971_BUCK1_BASE, |
| 60 | LP3971_BUCK2_BASE, |
| 61 | LP3971_BUCK3_BASE, |
| 62 | }; |
| 63 | |
| 64 | #define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x]) |
| 65 | #define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1) |
| 66 | |
Tobias Klauser | 6faa7e0 | 2009-12-23 14:13:17 +0100 | [diff] [blame] | 67 | static const int buck_voltage_map[] = { |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 68 | 0, 800, 850, 900, 950, 1000, 1050, 1100, |
| 69 | 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500, |
| 70 | 1550, 1600, 1650, 1700, 1800, 1900, 2500, 2800, |
| 71 | 3000, 3300, |
| 72 | }; |
| 73 | |
| 74 | #define BUCK_TARGET_VOL_MASK 0x3f |
| 75 | #define BUCK_TARGET_VOL_MIN_IDX 0x01 |
| 76 | #define BUCK_TARGET_VOL_MAX_IDX 0x19 |
| 77 | |
| 78 | #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2) |
| 79 | |
| 80 | #define LP3971_LDO_ENABLE_REG 0x12 |
| 81 | #define LP3971_LDO_VOL_CONTR_BASE 0x39 |
| 82 | |
| 83 | /* Voltage control registers: |
| 84 | LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0 |
| 85 | LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0 |
| 86 | LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1 |
| 87 | LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1 |
| 88 | LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2 |
| 89 | */ |
| 90 | #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1)) |
| 91 | |
| 92 | /* Voltage control registers shift: |
| 93 | LP3971_LDO1 -> 0, LP3971_LDO2 -> 4 |
| 94 | LP3971_LDO3 -> 0, LP3971_LDO4 -> 4 |
| 95 | LP3971_LDO5 -> 0 |
| 96 | */ |
| 97 | #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2) |
| 98 | #define LDO_VOL_CONTR_MASK 0x0f |
| 99 | |
Tobias Klauser | 6faa7e0 | 2009-12-23 14:13:17 +0100 | [diff] [blame] | 100 | static const int ldo45_voltage_map[] = { |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 101 | 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, |
| 102 | 1400, 1500, 1800, 1900, 2500, 2800, 3000, 3300, |
| 103 | }; |
| 104 | |
Tobias Klauser | 6faa7e0 | 2009-12-23 14:13:17 +0100 | [diff] [blame] | 105 | static const int ldo123_voltage_map[] = { |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 106 | 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, |
| 107 | 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300, |
| 108 | }; |
| 109 | |
Tobias Klauser | 6faa7e0 | 2009-12-23 14:13:17 +0100 | [diff] [blame] | 110 | static const int *ldo_voltage_map[] = { |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 111 | ldo123_voltage_map, /* LDO1 */ |
| 112 | ldo123_voltage_map, /* LDO2 */ |
| 113 | ldo123_voltage_map, /* LDO3 */ |
| 114 | ldo45_voltage_map, /* LDO4 */ |
| 115 | ldo45_voltage_map, /* LDO5 */ |
| 116 | }; |
| 117 | |
| 118 | #define LDO_VOL_VALUE_MAP(x) (ldo_voltage_map[(x - LP3971_LDO1)]) |
| 119 | |
| 120 | #define LDO_VOL_MIN_IDX 0x00 |
| 121 | #define LDO_VOL_MAX_IDX 0x0f |
| 122 | |
| 123 | static int lp3971_ldo_list_voltage(struct regulator_dev *dev, unsigned index) |
| 124 | { |
| 125 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 126 | return 1000 * LDO_VOL_VALUE_MAP(ldo)[index]; |
| 127 | } |
| 128 | |
| 129 | static int lp3971_ldo_is_enabled(struct regulator_dev *dev) |
| 130 | { |
| 131 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 132 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 133 | u16 mask = 1 << (1 + ldo); |
| 134 | u16 val; |
| 135 | |
| 136 | val = lp3971_reg_read(lp3971, LP3971_LDO_ENABLE_REG); |
| 137 | return (val & mask) != 0; |
| 138 | } |
| 139 | |
| 140 | static int lp3971_ldo_enable(struct regulator_dev *dev) |
| 141 | { |
| 142 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 143 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 144 | u16 mask = 1 << (1 + ldo); |
| 145 | |
| 146 | return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, mask); |
| 147 | } |
| 148 | |
| 149 | static int lp3971_ldo_disable(struct regulator_dev *dev) |
| 150 | { |
| 151 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 152 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 153 | u16 mask = 1 << (1 + ldo); |
| 154 | |
| 155 | return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, 0); |
| 156 | } |
| 157 | |
| 158 | static int lp3971_ldo_get_voltage(struct regulator_dev *dev) |
| 159 | { |
| 160 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 161 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 162 | u16 val, reg; |
| 163 | |
| 164 | reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo)); |
| 165 | val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK; |
| 166 | |
| 167 | return 1000 * LDO_VOL_VALUE_MAP(ldo)[val]; |
| 168 | } |
| 169 | |
| 170 | static int lp3971_ldo_set_voltage(struct regulator_dev *dev, |
| 171 | int min_uV, int max_uV) |
| 172 | { |
| 173 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 174 | int ldo = rdev_get_id(dev) - LP3971_LDO1; |
| 175 | int min_vol = min_uV / 1000, max_vol = max_uV / 1000; |
| 176 | const int *vol_map = LDO_VOL_VALUE_MAP(ldo); |
| 177 | u16 val; |
| 178 | |
| 179 | if (min_vol < vol_map[LDO_VOL_MIN_IDX] || |
| 180 | min_vol > vol_map[LDO_VOL_MAX_IDX]) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | for (val = LDO_VOL_MIN_IDX; val <= LDO_VOL_MAX_IDX; val++) |
| 184 | if (vol_map[val] >= min_vol) |
| 185 | break; |
| 186 | |
Roel Kluin | 62737d4 | 2010-02-12 12:30:21 +0100 | [diff] [blame] | 187 | if (val > LDO_VOL_MAX_IDX || vol_map[val] > max_vol) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 188 | return -EINVAL; |
| 189 | |
| 190 | return lp3971_set_bits(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo), |
Axel Lin | cdb868f | 2010-03-09 16:53:59 +0800 | [diff] [blame] | 191 | LDO_VOL_CONTR_MASK << LDO_VOL_CONTR_SHIFT(ldo), |
| 192 | val << LDO_VOL_CONTR_SHIFT(ldo)); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static struct regulator_ops lp3971_ldo_ops = { |
| 196 | .list_voltage = lp3971_ldo_list_voltage, |
| 197 | .is_enabled = lp3971_ldo_is_enabled, |
| 198 | .enable = lp3971_ldo_enable, |
| 199 | .disable = lp3971_ldo_disable, |
| 200 | .get_voltage = lp3971_ldo_get_voltage, |
| 201 | .set_voltage = lp3971_ldo_set_voltage, |
| 202 | }; |
| 203 | |
| 204 | static int lp3971_dcdc_list_voltage(struct regulator_dev *dev, unsigned index) |
| 205 | { |
| 206 | return 1000 * buck_voltage_map[index]; |
| 207 | } |
| 208 | |
| 209 | static int lp3971_dcdc_is_enabled(struct regulator_dev *dev) |
| 210 | { |
| 211 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 212 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
| 213 | u16 mask = 1 << (buck * 2); |
| 214 | u16 val; |
| 215 | |
| 216 | val = lp3971_reg_read(lp3971, LP3971_BUCK_VOL_ENABLE_REG); |
| 217 | return (val & mask) != 0; |
| 218 | } |
| 219 | |
| 220 | static int lp3971_dcdc_enable(struct regulator_dev *dev) |
| 221 | { |
| 222 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 223 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
| 224 | u16 mask = 1 << (buck * 2); |
| 225 | |
| 226 | return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, mask); |
| 227 | } |
| 228 | |
| 229 | static int lp3971_dcdc_disable(struct regulator_dev *dev) |
| 230 | { |
| 231 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 232 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
| 233 | u16 mask = 1 << (buck * 2); |
| 234 | |
| 235 | return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, 0); |
| 236 | } |
| 237 | |
| 238 | static int lp3971_dcdc_get_voltage(struct regulator_dev *dev) |
| 239 | { |
| 240 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 241 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
| 242 | u16 reg; |
| 243 | int val; |
| 244 | |
| 245 | reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck)); |
| 246 | reg &= BUCK_TARGET_VOL_MASK; |
| 247 | |
| 248 | if (reg <= BUCK_TARGET_VOL_MAX_IDX) |
| 249 | val = 1000 * buck_voltage_map[reg]; |
| 250 | else { |
| 251 | val = 0; |
| 252 | dev_warn(&dev->dev, "chip reported incorrect voltage value.\n"); |
| 253 | } |
| 254 | |
| 255 | return val; |
| 256 | } |
| 257 | |
| 258 | static int lp3971_dcdc_set_voltage(struct regulator_dev *dev, |
| 259 | int min_uV, int max_uV) |
| 260 | { |
| 261 | struct lp3971 *lp3971 = rdev_get_drvdata(dev); |
| 262 | int buck = rdev_get_id(dev) - LP3971_DCDC1; |
| 263 | int min_vol = min_uV / 1000, max_vol = max_uV / 1000; |
| 264 | const int *vol_map = buck_voltage_map; |
| 265 | u16 val; |
| 266 | int ret; |
| 267 | |
| 268 | if (min_vol < vol_map[BUCK_TARGET_VOL_MIN_IDX] || |
| 269 | min_vol > vol_map[BUCK_TARGET_VOL_MAX_IDX]) |
| 270 | return -EINVAL; |
| 271 | |
| 272 | for (val = BUCK_TARGET_VOL_MIN_IDX; val <= BUCK_TARGET_VOL_MAX_IDX; |
| 273 | val++) |
| 274 | if (vol_map[val] >= min_vol) |
| 275 | break; |
| 276 | |
Roel Kluin | 62737d4 | 2010-02-12 12:30:21 +0100 | [diff] [blame] | 277 | if (val > BUCK_TARGET_VOL_MAX_IDX || vol_map[val] > max_vol) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 278 | return -EINVAL; |
| 279 | |
| 280 | ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck), |
| 281 | BUCK_TARGET_VOL_MASK, val); |
| 282 | if (ret) |
| 283 | return ret; |
| 284 | |
| 285 | ret = lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG, |
| 286 | BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck), |
| 287 | BUCK_VOL_CHANGE_FLAG_GO << BUCK_VOL_CHANGE_SHIFT(buck)); |
| 288 | if (ret) |
| 289 | return ret; |
| 290 | |
| 291 | return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG, |
| 292 | BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck), |
| 293 | 0 << BUCK_VOL_CHANGE_SHIFT(buck)); |
| 294 | } |
| 295 | |
| 296 | static struct regulator_ops lp3971_dcdc_ops = { |
| 297 | .list_voltage = lp3971_dcdc_list_voltage, |
| 298 | .is_enabled = lp3971_dcdc_is_enabled, |
| 299 | .enable = lp3971_dcdc_enable, |
| 300 | .disable = lp3971_dcdc_disable, |
| 301 | .get_voltage = lp3971_dcdc_get_voltage, |
| 302 | .set_voltage = lp3971_dcdc_set_voltage, |
| 303 | }; |
| 304 | |
| 305 | static struct regulator_desc regulators[] = { |
| 306 | { |
| 307 | .name = "LDO1", |
| 308 | .id = LP3971_LDO1, |
| 309 | .ops = &lp3971_ldo_ops, |
| 310 | .n_voltages = ARRAY_SIZE(ldo123_voltage_map), |
| 311 | .type = REGULATOR_VOLTAGE, |
| 312 | .owner = THIS_MODULE, |
| 313 | }, |
| 314 | { |
| 315 | .name = "LDO2", |
| 316 | .id = LP3971_LDO2, |
| 317 | .ops = &lp3971_ldo_ops, |
| 318 | .n_voltages = ARRAY_SIZE(ldo123_voltage_map), |
| 319 | .type = REGULATOR_VOLTAGE, |
| 320 | .owner = THIS_MODULE, |
| 321 | }, |
| 322 | { |
| 323 | .name = "LDO3", |
| 324 | .id = LP3971_LDO3, |
| 325 | .ops = &lp3971_ldo_ops, |
| 326 | .n_voltages = ARRAY_SIZE(ldo123_voltage_map), |
| 327 | .type = REGULATOR_VOLTAGE, |
| 328 | .owner = THIS_MODULE, |
| 329 | }, |
| 330 | { |
| 331 | .name = "LDO4", |
| 332 | .id = LP3971_LDO4, |
| 333 | .ops = &lp3971_ldo_ops, |
| 334 | .n_voltages = ARRAY_SIZE(ldo45_voltage_map), |
| 335 | .type = REGULATOR_VOLTAGE, |
| 336 | .owner = THIS_MODULE, |
| 337 | }, |
| 338 | { |
| 339 | .name = "LDO5", |
| 340 | .id = LP3971_LDO5, |
| 341 | .ops = &lp3971_ldo_ops, |
| 342 | .n_voltages = ARRAY_SIZE(ldo45_voltage_map), |
| 343 | .type = REGULATOR_VOLTAGE, |
| 344 | .owner = THIS_MODULE, |
| 345 | }, |
| 346 | { |
| 347 | .name = "DCDC1", |
| 348 | .id = LP3971_DCDC1, |
| 349 | .ops = &lp3971_dcdc_ops, |
| 350 | .n_voltages = ARRAY_SIZE(buck_voltage_map), |
| 351 | .type = REGULATOR_VOLTAGE, |
| 352 | .owner = THIS_MODULE, |
| 353 | }, |
| 354 | { |
| 355 | .name = "DCDC2", |
| 356 | .id = LP3971_DCDC2, |
| 357 | .ops = &lp3971_dcdc_ops, |
| 358 | .n_voltages = ARRAY_SIZE(buck_voltage_map), |
| 359 | .type = REGULATOR_VOLTAGE, |
| 360 | .owner = THIS_MODULE, |
| 361 | }, |
| 362 | { |
| 363 | .name = "DCDC3", |
| 364 | .id = LP3971_DCDC3, |
| 365 | .ops = &lp3971_dcdc_ops, |
| 366 | .n_voltages = ARRAY_SIZE(buck_voltage_map), |
| 367 | .type = REGULATOR_VOLTAGE, |
| 368 | .owner = THIS_MODULE, |
| 369 | }, |
| 370 | }; |
| 371 | |
| 372 | static int lp3971_i2c_read(struct i2c_client *i2c, char reg, int count, |
| 373 | u16 *dest) |
| 374 | { |
| 375 | int ret; |
| 376 | |
| 377 | if (count != 1) |
| 378 | return -EIO; |
| 379 | ret = i2c_smbus_read_byte_data(i2c, reg); |
Axel Lin | 27ef7f0 | 2010-08-06 08:09:14 +0800 | [diff] [blame^] | 380 | if (ret < 0) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 381 | return -EIO; |
| 382 | |
| 383 | *dest = ret; |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count, |
| 388 | const u16 *src) |
| 389 | { |
| 390 | int ret; |
| 391 | |
| 392 | if (count != 1) |
| 393 | return -EIO; |
| 394 | ret = i2c_smbus_write_byte_data(i2c, reg, *src); |
| 395 | if (ret >= 0) |
| 396 | return 0; |
| 397 | |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg) |
| 402 | { |
| 403 | u16 val = 0; |
| 404 | |
| 405 | mutex_lock(&lp3971->io_lock); |
| 406 | |
| 407 | lp3971_i2c_read(lp3971->i2c, reg, 1, &val); |
| 408 | |
| 409 | dev_dbg(lp3971->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg, |
| 410 | (unsigned)val&0xff); |
| 411 | |
| 412 | mutex_unlock(&lp3971->io_lock); |
| 413 | |
| 414 | return val & 0xff; |
| 415 | } |
| 416 | |
| 417 | static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val) |
| 418 | { |
| 419 | u16 tmp; |
| 420 | int ret; |
| 421 | |
| 422 | mutex_lock(&lp3971->io_lock); |
| 423 | |
| 424 | ret = lp3971_i2c_read(lp3971->i2c, reg, 1, &tmp); |
| 425 | tmp = (tmp & ~mask) | val; |
| 426 | if (ret == 0) { |
| 427 | ret = lp3971_i2c_write(lp3971->i2c, reg, 1, &tmp); |
| 428 | dev_dbg(lp3971->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg, |
| 429 | (unsigned)val&0xff); |
| 430 | } |
| 431 | mutex_unlock(&lp3971->io_lock); |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 436 | static int __devinit setup_regulators(struct lp3971 *lp3971, |
| 437 | struct lp3971_platform_data *pdata) |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 438 | { |
| 439 | int i, err; |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 440 | |
| 441 | lp3971->num_regulators = pdata->num_regulators; |
| 442 | lp3971->rdev = kcalloc(pdata->num_regulators, |
| 443 | sizeof(struct regulator_dev *), GFP_KERNEL); |
Dan Carpenter | 67e46f3 | 2010-03-07 15:36:45 +0300 | [diff] [blame] | 444 | if (!lp3971->rdev) { |
| 445 | err = -ENOMEM; |
| 446 | goto err_nomem; |
| 447 | } |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 448 | |
| 449 | /* Instantiate the regulators */ |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 450 | for (i = 0; i < pdata->num_regulators; i++) { |
| 451 | struct lp3971_regulator_subdev *reg = &pdata->regulators[i]; |
| 452 | lp3971->rdev[i] = regulator_register(®ulators[reg->id], |
| 453 | lp3971->dev, reg->initdata, lp3971); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 454 | |
Julia Lawall | d662fc8 | 2009-11-21 22:18:44 +0100 | [diff] [blame] | 455 | if (IS_ERR(lp3971->rdev[i])) { |
| 456 | err = PTR_ERR(lp3971->rdev[i]); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 457 | dev_err(lp3971->dev, "regulator init failed: %d\n", |
| 458 | err); |
| 459 | goto error; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return 0; |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 464 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 465 | error: |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 466 | while (--i >= 0) |
| 467 | regulator_unregister(lp3971->rdev[i]); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 468 | kfree(lp3971->rdev); |
| 469 | lp3971->rdev = NULL; |
Dan Carpenter | 67e46f3 | 2010-03-07 15:36:45 +0300 | [diff] [blame] | 470 | err_nomem: |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 471 | return err; |
| 472 | } |
| 473 | |
| 474 | static int __devinit lp3971_i2c_probe(struct i2c_client *i2c, |
| 475 | const struct i2c_device_id *id) |
| 476 | { |
| 477 | struct lp3971 *lp3971; |
| 478 | struct lp3971_platform_data *pdata = i2c->dev.platform_data; |
| 479 | int ret; |
| 480 | u16 val; |
| 481 | |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 482 | if (!pdata) { |
| 483 | dev_dbg(&i2c->dev, "No platform init data supplied\n"); |
| 484 | return -ENODEV; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 487 | lp3971 = kzalloc(sizeof(struct lp3971), GFP_KERNEL); |
| 488 | if (lp3971 == NULL) |
| 489 | return -ENOMEM; |
| 490 | |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 491 | lp3971->i2c = i2c; |
| 492 | lp3971->dev = &i2c->dev; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 493 | |
| 494 | mutex_init(&lp3971->io_lock); |
| 495 | |
| 496 | /* Detect LP3971 */ |
| 497 | ret = lp3971_i2c_read(i2c, LP3971_SYS_CONTROL1_REG, 1, &val); |
| 498 | if (ret == 0 && (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL) |
| 499 | ret = -ENODEV; |
| 500 | if (ret < 0) { |
| 501 | dev_err(&i2c->dev, "failed to detect device\n"); |
| 502 | goto err_detect; |
| 503 | } |
| 504 | |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 505 | ret = setup_regulators(lp3971, pdata); |
| 506 | if (ret < 0) |
| 507 | goto err_detect; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 508 | |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 509 | i2c_set_clientdata(i2c, lp3971); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 510 | return 0; |
| 511 | |
| 512 | err_detect: |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 513 | kfree(lp3971); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | static int __devexit lp3971_i2c_remove(struct i2c_client *i2c) |
| 518 | { |
| 519 | struct lp3971 *lp3971 = i2c_get_clientdata(i2c); |
| 520 | int i; |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 521 | |
Dmitry Torokhov | ebbed04 | 2010-02-23 23:38:17 -0800 | [diff] [blame] | 522 | for (i = 0; i < lp3971->num_regulators; i++) |
| 523 | regulator_unregister(lp3971->rdev[i]); |
| 524 | |
| 525 | kfree(lp3971->rdev); |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 526 | kfree(lp3971); |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static const struct i2c_device_id lp3971_i2c_id[] = { |
| 532 | { "lp3971", 0 }, |
| 533 | { } |
| 534 | }; |
| 535 | MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id); |
| 536 | |
| 537 | static struct i2c_driver lp3971_i2c_driver = { |
| 538 | .driver = { |
| 539 | .name = "LP3971", |
| 540 | .owner = THIS_MODULE, |
| 541 | }, |
| 542 | .probe = lp3971_i2c_probe, |
Liam Girdwood | 6113c3a | 2009-05-19 11:44:37 +0100 | [diff] [blame] | 543 | .remove = __devexit_p(lp3971_i2c_remove), |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 544 | .id_table = lp3971_i2c_id, |
| 545 | }; |
| 546 | |
| 547 | static int __init lp3971_module_init(void) |
| 548 | { |
Wolfram Sang | 12a1d93 | 2009-09-18 22:44:45 +0200 | [diff] [blame] | 549 | int ret; |
Marek Szyprowski | 0cbdf7b | 2009-05-19 07:33:55 +0200 | [diff] [blame] | 550 | |
| 551 | ret = i2c_add_driver(&lp3971_i2c_driver); |
| 552 | if (ret != 0) |
| 553 | pr_err("Failed to register I2C driver: %d\n", ret); |
| 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | module_init(lp3971_module_init); |
| 558 | |
| 559 | static void __exit lp3971_module_exit(void) |
| 560 | { |
| 561 | i2c_del_driver(&lp3971_i2c_driver); |
| 562 | } |
| 563 | module_exit(lp3971_module_exit); |
| 564 | |
| 565 | MODULE_LICENSE("GPL"); |
| 566 | MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>"); |
| 567 | MODULE_DESCRIPTION("LP3971 PMIC driver"); |