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