James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 1 | /* |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 2 | * da9211-regulator.c - Regulator device driver for DA9211/DA9213 |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 3 | * Copyright (C) 2014 Dialog Semiconductor Ltd. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Library General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Library General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/regulator/driver.h> |
| 23 | #include <linux/regulator/machine.h> |
| 24 | #include <linux/regmap.h> |
| 25 | #include <linux/irq.h> |
| 26 | #include <linux/interrupt.h> |
James Ban | 8c7dd8b | 2015-01-28 09:28:08 +0900 | [diff] [blame] | 27 | #include <linux/of_gpio.h> |
James Ban | bf3baca | 2014-08-27 11:47:07 +0900 | [diff] [blame] | 28 | #include <linux/regulator/of_regulator.h> |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 29 | #include <linux/regulator/da9211.h> |
| 30 | #include "da9211-regulator.h" |
| 31 | |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 32 | /* DEVICE IDs */ |
| 33 | #define DA9211_DEVICE_ID 0x22 |
| 34 | #define DA9213_DEVICE_ID 0x23 |
| 35 | |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 36 | #define DA9211_BUCK_MODE_SLEEP 1 |
| 37 | #define DA9211_BUCK_MODE_SYNC 2 |
| 38 | #define DA9211_BUCK_MODE_AUTO 3 |
| 39 | |
| 40 | /* DA9211 REGULATOR IDs */ |
| 41 | #define DA9211_ID_BUCKA 0 |
| 42 | #define DA9211_ID_BUCKB 1 |
| 43 | |
| 44 | struct da9211 { |
| 45 | struct device *dev; |
| 46 | struct regmap *regmap; |
| 47 | struct da9211_pdata *pdata; |
| 48 | struct regulator_dev *rdev[DA9211_MAX_REGULATORS]; |
| 49 | int num_regulator; |
| 50 | int chip_irq; |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 51 | int chip_id; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static const struct regmap_range_cfg da9211_regmap_range[] = { |
| 55 | { |
| 56 | .selector_reg = DA9211_REG_PAGE_CON, |
| 57 | .selector_mask = DA9211_REG_PAGE_MASK, |
| 58 | .selector_shift = DA9211_REG_PAGE_SHIFT, |
| 59 | .window_start = 0, |
| 60 | .window_len = 256, |
| 61 | .range_min = 0, |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 62 | .range_max = 5*128, |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 63 | }, |
| 64 | }; |
| 65 | |
| 66 | static const struct regmap_config da9211_regmap_config = { |
| 67 | .reg_bits = 8, |
| 68 | .val_bits = 8, |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 69 | .max_register = 5 * 128, |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 70 | .ranges = da9211_regmap_range, |
| 71 | .num_ranges = ARRAY_SIZE(da9211_regmap_range), |
| 72 | }; |
| 73 | |
| 74 | /* Default limits measured in millivolts and milliamps */ |
| 75 | #define DA9211_MIN_MV 300 |
| 76 | #define DA9211_MAX_MV 1570 |
| 77 | #define DA9211_STEP_MV 10 |
| 78 | |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 79 | /* Current limits for DA9211 buck (uA) indices |
| 80 | * corresponds with register values |
| 81 | */ |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 82 | static const int da9211_current_limits[] = { |
| 83 | 2000000, 2200000, 2400000, 2600000, 2800000, 3000000, 3200000, 3400000, |
| 84 | 3600000, 3800000, 4000000, 4200000, 4400000, 4600000, 4800000, 5000000 |
| 85 | }; |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 86 | /* Current limits for DA9213 buck (uA) indices |
| 87 | * corresponds with register values |
| 88 | */ |
| 89 | static const int da9213_current_limits[] = { |
| 90 | 3000000, 3200000, 3400000, 3600000, 3800000, 4000000, 4200000, 4400000, |
| 91 | 4600000, 4800000, 5000000, 5200000, 5400000, 5600000, 5800000, 6000000 |
| 92 | }; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 93 | |
| 94 | static unsigned int da9211_buck_get_mode(struct regulator_dev *rdev) |
| 95 | { |
| 96 | int id = rdev_get_id(rdev); |
| 97 | struct da9211 *chip = rdev_get_drvdata(rdev); |
| 98 | unsigned int data; |
| 99 | int ret, mode = 0; |
| 100 | |
| 101 | ret = regmap_read(chip->regmap, DA9211_REG_BUCKA_CONF+id, &data); |
| 102 | if (ret < 0) |
| 103 | return ret; |
| 104 | |
| 105 | switch (data & 0x03) { |
| 106 | case DA9211_BUCK_MODE_SYNC: |
| 107 | mode = REGULATOR_MODE_FAST; |
| 108 | break; |
| 109 | case DA9211_BUCK_MODE_AUTO: |
| 110 | mode = REGULATOR_MODE_NORMAL; |
| 111 | break; |
| 112 | case DA9211_BUCK_MODE_SLEEP: |
| 113 | mode = REGULATOR_MODE_STANDBY; |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | return mode; |
| 118 | } |
| 119 | |
| 120 | static int da9211_buck_set_mode(struct regulator_dev *rdev, |
| 121 | unsigned int mode) |
| 122 | { |
| 123 | int id = rdev_get_id(rdev); |
| 124 | struct da9211 *chip = rdev_get_drvdata(rdev); |
| 125 | int val = 0; |
| 126 | |
| 127 | switch (mode) { |
| 128 | case REGULATOR_MODE_FAST: |
| 129 | val = DA9211_BUCK_MODE_SYNC; |
| 130 | break; |
| 131 | case REGULATOR_MODE_NORMAL: |
| 132 | val = DA9211_BUCK_MODE_AUTO; |
| 133 | break; |
| 134 | case REGULATOR_MODE_STANDBY: |
| 135 | val = DA9211_BUCK_MODE_SLEEP; |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | return regmap_update_bits(chip->regmap, DA9211_REG_BUCKA_CONF+id, |
| 140 | 0x03, val); |
| 141 | } |
| 142 | |
| 143 | static int da9211_set_current_limit(struct regulator_dev *rdev, int min, |
| 144 | int max) |
| 145 | { |
| 146 | int id = rdev_get_id(rdev); |
| 147 | struct da9211 *chip = rdev_get_drvdata(rdev); |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 148 | int i, max_size; |
| 149 | const int *current_limits; |
| 150 | |
| 151 | switch (chip->chip_id) { |
| 152 | case DA9211: |
| 153 | current_limits = da9211_current_limits; |
| 154 | max_size = ARRAY_SIZE(da9211_current_limits)-1; |
| 155 | break; |
| 156 | case DA9213: |
| 157 | current_limits = da9213_current_limits; |
| 158 | max_size = ARRAY_SIZE(da9213_current_limits)-1; |
| 159 | break; |
| 160 | default: |
| 161 | return -EINVAL; |
| 162 | } |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 163 | |
| 164 | /* search for closest to maximum */ |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 165 | for (i = max_size; i >= 0; i--) { |
| 166 | if (min <= current_limits[i] && |
| 167 | max >= current_limits[i]) { |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 168 | return regmap_update_bits(chip->regmap, |
| 169 | DA9211_REG_BUCK_ILIM, |
| 170 | (0x0F << id*4), (i << id*4)); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | static int da9211_get_current_limit(struct regulator_dev *rdev) |
| 178 | { |
| 179 | int id = rdev_get_id(rdev); |
| 180 | struct da9211 *chip = rdev_get_drvdata(rdev); |
| 181 | unsigned int data; |
| 182 | int ret; |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 183 | const int *current_limits; |
| 184 | |
| 185 | switch (chip->chip_id) { |
| 186 | case DA9211: |
| 187 | current_limits = da9211_current_limits; |
| 188 | break; |
| 189 | case DA9213: |
| 190 | current_limits = da9213_current_limits; |
| 191 | break; |
| 192 | default: |
| 193 | return -EINVAL; |
| 194 | } |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 195 | |
| 196 | ret = regmap_read(chip->regmap, DA9211_REG_BUCK_ILIM, &data); |
| 197 | if (ret < 0) |
| 198 | return ret; |
| 199 | |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 200 | /* select one of 16 values: 0000 (2000mA or 3000mA) |
| 201 | * to 1111 (5000mA or 6000mA). |
| 202 | */ |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 203 | data = (data >> id*4) & 0x0F; |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 204 | return current_limits[data]; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static struct regulator_ops da9211_buck_ops = { |
| 208 | .get_mode = da9211_buck_get_mode, |
| 209 | .set_mode = da9211_buck_set_mode, |
| 210 | .enable = regulator_enable_regmap, |
| 211 | .disable = regulator_disable_regmap, |
| 212 | .is_enabled = regulator_is_enabled_regmap, |
| 213 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 214 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 215 | .list_voltage = regulator_list_voltage_linear, |
| 216 | .set_current_limit = da9211_set_current_limit, |
| 217 | .get_current_limit = da9211_get_current_limit, |
| 218 | }; |
| 219 | |
| 220 | #define DA9211_BUCK(_id) \ |
| 221 | {\ |
| 222 | .name = #_id,\ |
| 223 | .ops = &da9211_buck_ops,\ |
| 224 | .type = REGULATOR_VOLTAGE,\ |
| 225 | .id = DA9211_ID_##_id,\ |
| 226 | .n_voltages = (DA9211_MAX_MV - DA9211_MIN_MV) / DA9211_STEP_MV + 1,\ |
| 227 | .min_uV = (DA9211_MIN_MV * 1000),\ |
| 228 | .uV_step = (DA9211_STEP_MV * 1000),\ |
| 229 | .enable_reg = DA9211_REG_BUCKA_CONT + DA9211_ID_##_id,\ |
| 230 | .enable_mask = DA9211_BUCKA_EN,\ |
| 231 | .vsel_reg = DA9211_REG_VBUCKA_A + DA9211_ID_##_id * 2,\ |
| 232 | .vsel_mask = DA9211_VBUCK_MASK,\ |
| 233 | .owner = THIS_MODULE,\ |
| 234 | } |
| 235 | |
| 236 | static struct regulator_desc da9211_regulators[] = { |
| 237 | DA9211_BUCK(BUCKA), |
| 238 | DA9211_BUCK(BUCKB), |
| 239 | }; |
| 240 | |
James Ban | bf3baca | 2014-08-27 11:47:07 +0900 | [diff] [blame] | 241 | #ifdef CONFIG_OF |
| 242 | static struct of_regulator_match da9211_matches[] = { |
| 243 | [DA9211_ID_BUCKA] = { .name = "BUCKA" }, |
| 244 | [DA9211_ID_BUCKB] = { .name = "BUCKB" }, |
| 245 | }; |
| 246 | |
| 247 | static struct da9211_pdata *da9211_parse_regulators_dt( |
| 248 | struct device *dev) |
| 249 | { |
| 250 | struct da9211_pdata *pdata; |
| 251 | struct device_node *node; |
| 252 | int i, num, n; |
| 253 | |
| 254 | node = of_get_child_by_name(dev->of_node, "regulators"); |
| 255 | if (!node) { |
| 256 | dev_err(dev, "regulators node not found\n"); |
| 257 | return ERR_PTR(-ENODEV); |
| 258 | } |
| 259 | |
| 260 | num = of_regulator_match(dev, node, da9211_matches, |
| 261 | ARRAY_SIZE(da9211_matches)); |
| 262 | of_node_put(node); |
| 263 | if (num < 0) { |
| 264 | dev_err(dev, "Failed to match regulators\n"); |
| 265 | return ERR_PTR(-EINVAL); |
| 266 | } |
| 267 | |
| 268 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 269 | if (!pdata) |
| 270 | return ERR_PTR(-ENOMEM); |
| 271 | |
| 272 | pdata->num_buck = num; |
| 273 | |
| 274 | n = 0; |
| 275 | for (i = 0; i < ARRAY_SIZE(da9211_matches); i++) { |
| 276 | if (!da9211_matches[i].init_data) |
| 277 | continue; |
| 278 | |
| 279 | pdata->init_data[n] = da9211_matches[i].init_data; |
James Ban | 076c3b8 | 2015-01-16 12:13:27 +0900 | [diff] [blame] | 280 | pdata->reg_node[n] = da9211_matches[i].of_node; |
James Ban | 8c7dd8b | 2015-01-28 09:28:08 +0900 | [diff] [blame] | 281 | pdata->gpio_ren[n] = |
| 282 | of_get_named_gpio(da9211_matches[i].of_node, |
| 283 | "enable-gpios", 0); |
James Ban | bf3baca | 2014-08-27 11:47:07 +0900 | [diff] [blame] | 284 | n++; |
Fengguang Wu | c2a946e | 2014-08-29 12:41:59 +0100 | [diff] [blame] | 285 | } |
James Ban | bf3baca | 2014-08-27 11:47:07 +0900 | [diff] [blame] | 286 | |
| 287 | return pdata; |
| 288 | } |
| 289 | #else |
| 290 | static struct da9211_pdata *da9211_parse_regulators_dt( |
| 291 | struct device *dev) |
| 292 | { |
| 293 | return ERR_PTR(-ENODEV); |
| 294 | } |
| 295 | #endif |
| 296 | |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 297 | static irqreturn_t da9211_irq_handler(int irq, void *data) |
| 298 | { |
| 299 | struct da9211 *chip = data; |
| 300 | int reg_val, err, ret = IRQ_NONE; |
| 301 | |
| 302 | err = regmap_read(chip->regmap, DA9211_REG_EVENT_B, ®_val); |
| 303 | if (err < 0) |
| 304 | goto error_i2c; |
| 305 | |
| 306 | if (reg_val & DA9211_E_OV_CURR_A) { |
| 307 | regulator_notifier_call_chain(chip->rdev[0], |
Geert Uytterhoeven | a46a073 | 2015-02-23 17:12:16 +0100 | [diff] [blame] | 308 | REGULATOR_EVENT_OVER_CURRENT, NULL); |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 309 | |
| 310 | err = regmap_write(chip->regmap, DA9211_REG_EVENT_B, |
| 311 | DA9211_E_OV_CURR_A); |
| 312 | if (err < 0) |
| 313 | goto error_i2c; |
| 314 | |
| 315 | ret = IRQ_HANDLED; |
| 316 | } |
| 317 | |
| 318 | if (reg_val & DA9211_E_OV_CURR_B) { |
| 319 | regulator_notifier_call_chain(chip->rdev[1], |
Geert Uytterhoeven | a46a073 | 2015-02-23 17:12:16 +0100 | [diff] [blame] | 320 | REGULATOR_EVENT_OVER_CURRENT, NULL); |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 321 | |
| 322 | err = regmap_write(chip->regmap, DA9211_REG_EVENT_B, |
| 323 | DA9211_E_OV_CURR_B); |
| 324 | if (err < 0) |
| 325 | goto error_i2c; |
| 326 | |
| 327 | ret = IRQ_HANDLED; |
| 328 | } |
| 329 | |
| 330 | return ret; |
| 331 | |
| 332 | error_i2c: |
| 333 | dev_err(chip->dev, "I2C error : %d\n", err); |
| 334 | return IRQ_NONE; |
| 335 | } |
| 336 | |
| 337 | static int da9211_regulator_init(struct da9211 *chip) |
| 338 | { |
| 339 | struct regulator_config config = { }; |
| 340 | int i, ret; |
| 341 | unsigned int data; |
| 342 | |
| 343 | ret = regmap_read(chip->regmap, DA9211_REG_CONFIG_E, &data); |
| 344 | if (ret < 0) { |
Geert Uytterhoeven | 767e8aa | 2015-02-23 17:11:07 +0100 | [diff] [blame] | 345 | dev_err(chip->dev, "Failed to read CONFIG_E reg: %d\n", ret); |
Axel Lin | 516c151 | 2014-07-20 19:19:35 +0800 | [diff] [blame] | 346 | return ret; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | data &= DA9211_SLAVE_SEL; |
| 350 | /* If configuration for 1/2 bucks is different between platform data |
| 351 | * and the register, driver should exit. |
| 352 | */ |
| 353 | if ((chip->pdata->num_buck == 2 && data == 0x40) |
| 354 | || (chip->pdata->num_buck == 1 && data == 0x00)) { |
| 355 | if (data == 0) |
| 356 | chip->num_regulator = 1; |
| 357 | else |
| 358 | chip->num_regulator = 2; |
| 359 | } else { |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 360 | dev_err(chip->dev, "Configuration is mismatched\n"); |
Axel Lin | 516c151 | 2014-07-20 19:19:35 +0800 | [diff] [blame] | 361 | return -EINVAL; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | for (i = 0; i < chip->num_regulator; i++) { |
James Ban | bf3baca | 2014-08-27 11:47:07 +0900 | [diff] [blame] | 365 | config.init_data = chip->pdata->init_data[i]; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 366 | config.dev = chip->dev; |
| 367 | config.driver_data = chip; |
| 368 | config.regmap = chip->regmap; |
James Ban | 076c3b8 | 2015-01-16 12:13:27 +0900 | [diff] [blame] | 369 | config.of_node = chip->pdata->reg_node[i]; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 370 | |
James Ban | 8c7dd8b | 2015-01-28 09:28:08 +0900 | [diff] [blame] | 371 | if (gpio_is_valid(chip->pdata->gpio_ren[i])) { |
| 372 | config.ena_gpio = chip->pdata->gpio_ren[i]; |
| 373 | config.ena_gpio_initialized = true; |
| 374 | } else { |
| 375 | config.ena_gpio = -EINVAL; |
| 376 | config.ena_gpio_initialized = false; |
| 377 | } |
| 378 | |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 379 | chip->rdev[i] = devm_regulator_register(chip->dev, |
| 380 | &da9211_regulators[i], &config); |
| 381 | if (IS_ERR(chip->rdev[i])) { |
| 382 | dev_err(chip->dev, |
| 383 | "Failed to register DA9211 regulator\n"); |
Axel Lin | 516c151 | 2014-07-20 19:19:35 +0800 | [diff] [blame] | 384 | return PTR_ERR(chip->rdev[i]); |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | if (chip->chip_irq != 0) { |
| 388 | ret = regmap_update_bits(chip->regmap, |
James Ban | 4e7089f | 2014-09-29 16:59:20 +0900 | [diff] [blame] | 389 | DA9211_REG_MASK_B, DA9211_M_OV_CURR_A << i, 0); |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 390 | if (ret < 0) { |
| 391 | dev_err(chip->dev, |
| 392 | "Failed to update mask reg: %d\n", ret); |
Axel Lin | 516c151 | 2014-07-20 19:19:35 +0800 | [diff] [blame] | 393 | return ret; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return 0; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 399 | } |
James Ban | bf3baca | 2014-08-27 11:47:07 +0900 | [diff] [blame] | 400 | |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 401 | /* |
| 402 | * I2C driver interface functions |
| 403 | */ |
| 404 | static int da9211_i2c_probe(struct i2c_client *i2c, |
| 405 | const struct i2c_device_id *id) |
| 406 | { |
| 407 | struct da9211 *chip; |
| 408 | int error, ret; |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 409 | unsigned int data; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 410 | |
| 411 | chip = devm_kzalloc(&i2c->dev, sizeof(struct da9211), GFP_KERNEL); |
Axel Lin | 1d3e6a6 | 2014-08-17 18:34:48 +0800 | [diff] [blame] | 412 | if (!chip) |
| 413 | return -ENOMEM; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 414 | |
| 415 | chip->dev = &i2c->dev; |
| 416 | chip->regmap = devm_regmap_init_i2c(i2c, &da9211_regmap_config); |
| 417 | if (IS_ERR(chip->regmap)) { |
| 418 | error = PTR_ERR(chip->regmap); |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 419 | dev_err(chip->dev, "Failed to allocate register map: %d\n", |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 420 | error); |
| 421 | return error; |
| 422 | } |
| 423 | |
| 424 | i2c_set_clientdata(i2c, chip); |
| 425 | |
| 426 | chip->pdata = i2c->dev.platform_data; |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 427 | |
| 428 | ret = regmap_read(chip->regmap, DA9211_REG_DEVICE_ID, &data); |
| 429 | if (ret < 0) { |
| 430 | dev_err(chip->dev, "Failed to read DEVICE_ID reg: %d\n", ret); |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | switch (data) { |
| 435 | case DA9211_DEVICE_ID: |
| 436 | chip->chip_id = DA9211; |
| 437 | break; |
| 438 | case DA9213_DEVICE_ID: |
| 439 | chip->chip_id = DA9213; |
| 440 | break; |
| 441 | default: |
| 442 | dev_err(chip->dev, "Unsupported device id = 0x%x.\n", data); |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 443 | return -ENODEV; |
| 444 | } |
| 445 | |
James Ban | bf3baca | 2014-08-27 11:47:07 +0900 | [diff] [blame] | 446 | if (!chip->pdata) |
| 447 | chip->pdata = da9211_parse_regulators_dt(chip->dev); |
| 448 | |
| 449 | if (IS_ERR(chip->pdata)) { |
| 450 | dev_err(chip->dev, "No regulators defined for the platform\n"); |
| 451 | return PTR_ERR(chip->pdata); |
| 452 | } |
| 453 | |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 454 | chip->chip_irq = i2c->irq; |
| 455 | |
| 456 | if (chip->chip_irq != 0) { |
| 457 | ret = devm_request_threaded_irq(chip->dev, chip->chip_irq, NULL, |
| 458 | da9211_irq_handler, |
| 459 | IRQF_TRIGGER_LOW|IRQF_ONESHOT, |
| 460 | "da9211", chip); |
| 461 | if (ret != 0) { |
| 462 | dev_err(chip->dev, "Failed to request IRQ: %d\n", |
| 463 | chip->chip_irq); |
| 464 | return ret; |
| 465 | } |
| 466 | } else { |
| 467 | dev_warn(chip->dev, "No IRQ configured\n"); |
| 468 | } |
| 469 | |
| 470 | ret = da9211_regulator_init(chip); |
| 471 | |
| 472 | if (ret < 0) |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 473 | dev_err(chip->dev, "Failed to initialize regulator: %d\n", ret); |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 478 | static const struct i2c_device_id da9211_i2c_id[] = { |
Axel Lin | 6a52f56 | 2014-09-05 09:12:55 +0800 | [diff] [blame] | 479 | {"da9211", DA9211}, |
| 480 | {"da9213", DA9213}, |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 481 | {}, |
| 482 | }; |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 483 | MODULE_DEVICE_TABLE(i2c, da9211_i2c_id); |
| 484 | |
Axel Lin | 6a52f56 | 2014-09-05 09:12:55 +0800 | [diff] [blame] | 485 | #ifdef CONFIG_OF |
| 486 | static const struct of_device_id da9211_dt_ids[] = { |
| 487 | { .compatible = "dlg,da9211", .data = &da9211_i2c_id[0] }, |
| 488 | { .compatible = "dlg,da9213", .data = &da9211_i2c_id[1] }, |
| 489 | {}, |
| 490 | }; |
| 491 | MODULE_DEVICE_TABLE(of, da9211_dt_ids); |
| 492 | #endif |
| 493 | |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 494 | static struct i2c_driver da9211_regulator_driver = { |
| 495 | .driver = { |
| 496 | .name = "da9211", |
| 497 | .owner = THIS_MODULE, |
Axel Lin | 6a52f56 | 2014-09-05 09:12:55 +0800 | [diff] [blame] | 498 | .of_match_table = of_match_ptr(da9211_dt_ids), |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 499 | }, |
| 500 | .probe = da9211_i2c_probe, |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 501 | .id_table = da9211_i2c_id, |
| 502 | }; |
| 503 | |
| 504 | module_i2c_driver(da9211_regulator_driver); |
| 505 | |
| 506 | MODULE_AUTHOR("James Ban <James.Ban.opensource@diasemi.com>"); |
James Ban | 005547e | 2014-08-08 14:27:04 +0900 | [diff] [blame] | 507 | MODULE_DESCRIPTION("Regulator device driver for Dialog DA9211/DA9213"); |
James Ban | 1028a37 | 2014-07-14 13:48:45 +0900 | [diff] [blame] | 508 | MODULE_LICENSE("GPL v2"); |