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