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