Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver. |
| 3 | * |
| 4 | * Supported Part Numbers: |
| 5 | * FAN53555UC00X/01X/03X/04X/05X |
| 6 | * |
| 7 | * Copyright (c) 2012 Marvell Technology Ltd. |
| 8 | * Yunfan Zhang <yfzhang@marvell.com> |
| 9 | * |
| 10 | * This package is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | */ |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/param.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/regulator/driver.h> |
| 20 | #include <linux/regulator/machine.h> |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 21 | #include <linux/regulator/of_regulator.h> |
| 22 | #include <linux/of_device.h> |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 23 | #include <linux/i2c.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/regmap.h> |
| 26 | #include <linux/regulator/fan53555.h> |
| 27 | |
| 28 | /* Voltage setting */ |
| 29 | #define FAN53555_VSEL0 0x00 |
| 30 | #define FAN53555_VSEL1 0x01 |
| 31 | /* Control register */ |
| 32 | #define FAN53555_CONTROL 0x02 |
| 33 | /* IC Type */ |
| 34 | #define FAN53555_ID1 0x03 |
| 35 | /* IC mask version */ |
| 36 | #define FAN53555_ID2 0x04 |
| 37 | /* Monitor register */ |
| 38 | #define FAN53555_MONITOR 0x05 |
| 39 | |
| 40 | /* VSEL bit definitions */ |
| 41 | #define VSEL_BUCK_EN (1 << 7) |
| 42 | #define VSEL_MODE (1 << 6) |
| 43 | #define VSEL_NSEL_MASK 0x3F |
| 44 | /* Chip ID and Verison */ |
| 45 | #define DIE_ID 0x0F /* ID1 */ |
| 46 | #define DIE_REV 0x0F /* ID2 */ |
| 47 | /* Control bit definitions */ |
| 48 | #define CTL_OUTPUT_DISCHG (1 << 7) |
| 49 | #define CTL_SLEW_MASK (0x7 << 4) |
| 50 | #define CTL_SLEW_SHIFT 4 |
| 51 | #define CTL_RESET (1 << 2) |
| 52 | |
| 53 | #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */ |
| 54 | |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 55 | enum fan53555_vendor { |
| 56 | FAN53555_VENDOR_FAIRCHILD = 0, |
| 57 | FAN53555_VENDOR_SILERGY, |
| 58 | }; |
| 59 | |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 60 | /* IC Type */ |
| 61 | enum { |
| 62 | FAN53555_CHIP_ID_00 = 0, |
| 63 | FAN53555_CHIP_ID_01, |
| 64 | FAN53555_CHIP_ID_02, |
| 65 | FAN53555_CHIP_ID_03, |
| 66 | FAN53555_CHIP_ID_04, |
| 67 | FAN53555_CHIP_ID_05, |
Wadim Egorov | 5e39cf4 | 2016-04-20 10:01:07 +0200 | [diff] [blame] | 68 | FAN53555_CHIP_ID_08 = 8, |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Wadim Egorov | e57cbb7 | 2016-04-20 10:01:08 +0200 | [diff] [blame] | 71 | /* IC mask revision */ |
| 72 | enum { |
| 73 | FAN53555_CHIP_REV_00 = 0x3, |
| 74 | FAN53555_CHIP_REV_13 = 0xf, |
| 75 | }; |
| 76 | |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 77 | enum { |
| 78 | SILERGY_SYR82X = 8, |
| 79 | }; |
| 80 | |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 81 | struct fan53555_device_info { |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 82 | enum fan53555_vendor vendor; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 83 | struct regmap *regmap; |
| 84 | struct device *dev; |
| 85 | struct regulator_desc desc; |
| 86 | struct regulator_dev *rdev; |
| 87 | struct regulator_init_data *regulator; |
| 88 | /* IC Type and Rev */ |
| 89 | int chip_id; |
| 90 | int chip_rev; |
| 91 | /* Voltage setting register */ |
| 92 | unsigned int vol_reg; |
| 93 | unsigned int sleep_reg; |
| 94 | /* Voltage range and step(linear) */ |
| 95 | unsigned int vsel_min; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 96 | unsigned int vsel_step; |
| 97 | /* Voltage slew rate limiting */ |
| 98 | unsigned int slew_rate; |
| 99 | /* Sleep voltage cache */ |
| 100 | unsigned int sleep_vol_cache; |
| 101 | }; |
| 102 | |
| 103 | static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV) |
| 104 | { |
| 105 | struct fan53555_device_info *di = rdev_get_drvdata(rdev); |
| 106 | int ret; |
| 107 | |
| 108 | if (di->sleep_vol_cache == uV) |
| 109 | return 0; |
| 110 | ret = regulator_map_voltage_linear(rdev, uV, uV); |
| 111 | if (ret < 0) |
Sachin Kamat | 145fe1e | 2014-02-18 16:11:04 +0530 | [diff] [blame] | 112 | return ret; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 113 | ret = regmap_update_bits(di->regmap, di->sleep_reg, |
| 114 | VSEL_NSEL_MASK, ret); |
| 115 | if (ret < 0) |
Sachin Kamat | 145fe1e | 2014-02-18 16:11:04 +0530 | [diff] [blame] | 116 | return ret; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 117 | /* Cache the sleep voltage setting. |
| 118 | * Might not be the real voltage which is rounded */ |
| 119 | di->sleep_vol_cache = uV; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
zhangqing | ab7cad3 | 2016-01-12 03:05:56 -0800 | [diff] [blame] | 124 | static int fan53555_set_suspend_enable(struct regulator_dev *rdev) |
| 125 | { |
| 126 | struct fan53555_device_info *di = rdev_get_drvdata(rdev); |
| 127 | |
| 128 | return regmap_update_bits(di->regmap, di->sleep_reg, |
| 129 | VSEL_BUCK_EN, VSEL_BUCK_EN); |
| 130 | } |
| 131 | |
| 132 | static int fan53555_set_suspend_disable(struct regulator_dev *rdev) |
| 133 | { |
| 134 | struct fan53555_device_info *di = rdev_get_drvdata(rdev); |
| 135 | |
| 136 | return regmap_update_bits(di->regmap, di->sleep_reg, |
| 137 | VSEL_BUCK_EN, 0); |
| 138 | } |
| 139 | |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 140 | static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode) |
| 141 | { |
| 142 | struct fan53555_device_info *di = rdev_get_drvdata(rdev); |
| 143 | |
| 144 | switch (mode) { |
| 145 | case REGULATOR_MODE_FAST: |
| 146 | regmap_update_bits(di->regmap, di->vol_reg, |
| 147 | VSEL_MODE, VSEL_MODE); |
| 148 | break; |
| 149 | case REGULATOR_MODE_NORMAL: |
| 150 | regmap_update_bits(di->regmap, di->vol_reg, VSEL_MODE, 0); |
| 151 | break; |
| 152 | default: |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static unsigned int fan53555_get_mode(struct regulator_dev *rdev) |
| 159 | { |
| 160 | struct fan53555_device_info *di = rdev_get_drvdata(rdev); |
| 161 | unsigned int val; |
| 162 | int ret = 0; |
| 163 | |
| 164 | ret = regmap_read(di->regmap, di->vol_reg, &val); |
| 165 | if (ret < 0) |
| 166 | return ret; |
| 167 | if (val & VSEL_MODE) |
| 168 | return REGULATOR_MODE_FAST; |
| 169 | else |
| 170 | return REGULATOR_MODE_NORMAL; |
| 171 | } |
| 172 | |
Krzysztof Kozlowski | 121b567 | 2015-01-05 10:04:19 +0100 | [diff] [blame] | 173 | static const int slew_rates[] = { |
Heiko Stuebner | dd7e71f | 2014-09-16 17:54:01 +0200 | [diff] [blame] | 174 | 64000, |
| 175 | 32000, |
| 176 | 16000, |
| 177 | 8000, |
| 178 | 4000, |
| 179 | 2000, |
| 180 | 1000, |
| 181 | 500, |
| 182 | }; |
| 183 | |
| 184 | static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp) |
| 185 | { |
| 186 | struct fan53555_device_info *di = rdev_get_drvdata(rdev); |
| 187 | int regval = -1, i; |
| 188 | |
| 189 | for (i = 0; i < ARRAY_SIZE(slew_rates); i++) { |
| 190 | if (ramp <= slew_rates[i]) |
| 191 | regval = i; |
| 192 | else |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | if (regval < 0) { |
| 197 | dev_err(di->dev, "unsupported ramp value %d\n", ramp); |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | |
| 201 | return regmap_update_bits(di->regmap, FAN53555_CONTROL, |
| 202 | CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT); |
| 203 | } |
| 204 | |
Bhumika Goyal | 71880ab | 2017-01-28 19:32:05 +0530 | [diff] [blame^] | 205 | static const struct regulator_ops fan53555_regulator_ops = { |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 206 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 207 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
Heiko Stuebner | fda87a4 | 2015-05-26 18:19:45 +0200 | [diff] [blame] | 208 | .set_voltage_time_sel = regulator_set_voltage_time_sel, |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 209 | .map_voltage = regulator_map_voltage_linear, |
| 210 | .list_voltage = regulator_list_voltage_linear, |
| 211 | .set_suspend_voltage = fan53555_set_suspend_voltage, |
| 212 | .enable = regulator_enable_regmap, |
| 213 | .disable = regulator_disable_regmap, |
| 214 | .is_enabled = regulator_is_enabled_regmap, |
| 215 | .set_mode = fan53555_set_mode, |
| 216 | .get_mode = fan53555_get_mode, |
Heiko Stuebner | dd7e71f | 2014-09-16 17:54:01 +0200 | [diff] [blame] | 217 | .set_ramp_delay = fan53555_set_ramp, |
zhangqing | ab7cad3 | 2016-01-12 03:05:56 -0800 | [diff] [blame] | 218 | .set_suspend_enable = fan53555_set_suspend_enable, |
| 219 | .set_suspend_disable = fan53555_set_suspend_disable, |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 222 | static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di) |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 223 | { |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 224 | /* Init voltage range and step */ |
| 225 | switch (di->chip_id) { |
| 226 | case FAN53555_CHIP_ID_00: |
Wadim Egorov | e57cbb7 | 2016-04-20 10:01:08 +0200 | [diff] [blame] | 227 | switch (di->chip_rev) { |
| 228 | case FAN53555_CHIP_REV_00: |
| 229 | di->vsel_min = 600000; |
| 230 | di->vsel_step = 10000; |
| 231 | break; |
| 232 | case FAN53555_CHIP_REV_13: |
| 233 | di->vsel_min = 800000; |
| 234 | di->vsel_step = 10000; |
| 235 | break; |
| 236 | default: |
| 237 | dev_err(di->dev, |
| 238 | "Chip ID %d with rev %d not supported!\n", |
| 239 | di->chip_id, di->chip_rev); |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | break; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 243 | case FAN53555_CHIP_ID_01: |
| 244 | case FAN53555_CHIP_ID_03: |
| 245 | case FAN53555_CHIP_ID_05: |
Wadim Egorov | 5e39cf4 | 2016-04-20 10:01:07 +0200 | [diff] [blame] | 246 | case FAN53555_CHIP_ID_08: |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 247 | di->vsel_min = 600000; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 248 | di->vsel_step = 10000; |
| 249 | break; |
| 250 | case FAN53555_CHIP_ID_04: |
| 251 | di->vsel_min = 603000; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 252 | di->vsel_step = 12826; |
| 253 | break; |
| 254 | default: |
| 255 | dev_err(di->dev, |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 256 | "Chip ID %d not supported!\n", di->chip_id); |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 257 | return -EINVAL; |
| 258 | } |
Heiko Stuebner | dd7e71f | 2014-09-16 17:54:01 +0200 | [diff] [blame] | 259 | |
| 260 | return 0; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 263 | static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di) |
| 264 | { |
| 265 | /* Init voltage range and step */ |
| 266 | switch (di->chip_id) { |
| 267 | case SILERGY_SYR82X: |
| 268 | di->vsel_min = 712500; |
| 269 | di->vsel_step = 12500; |
| 270 | break; |
| 271 | default: |
| 272 | dev_err(di->dev, |
| 273 | "Chip ID %d not supported!\n", di->chip_id); |
| 274 | return -EINVAL; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /* For 00,01,03,05 options: |
| 281 | * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V. |
| 282 | * For 04 option: |
| 283 | * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V. |
| 284 | * */ |
| 285 | static int fan53555_device_setup(struct fan53555_device_info *di, |
| 286 | struct fan53555_platform_data *pdata) |
| 287 | { |
| 288 | int ret = 0; |
| 289 | |
| 290 | /* Setup voltage control register */ |
| 291 | switch (pdata->sleep_vsel_id) { |
| 292 | case FAN53555_VSEL_ID_0: |
| 293 | di->sleep_reg = FAN53555_VSEL0; |
| 294 | di->vol_reg = FAN53555_VSEL1; |
| 295 | break; |
| 296 | case FAN53555_VSEL_ID_1: |
| 297 | di->sleep_reg = FAN53555_VSEL1; |
| 298 | di->vol_reg = FAN53555_VSEL0; |
| 299 | break; |
| 300 | default: |
| 301 | dev_err(di->dev, "Invalid VSEL ID!\n"); |
| 302 | return -EINVAL; |
| 303 | } |
| 304 | |
| 305 | switch (di->vendor) { |
| 306 | case FAN53555_VENDOR_FAIRCHILD: |
| 307 | ret = fan53555_voltages_setup_fairchild(di); |
| 308 | break; |
| 309 | case FAN53555_VENDOR_SILERGY: |
| 310 | ret = fan53555_voltages_setup_silergy(di); |
| 311 | break; |
| 312 | default: |
Axel Lin | fe23053 | 2014-09-18 21:48:48 +0800 | [diff] [blame] | 313 | dev_err(di->dev, "vendor %d not supported!\n", di->vendor); |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 314 | return -EINVAL; |
| 315 | } |
| 316 | |
| 317 | return ret; |
| 318 | } |
| 319 | |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 320 | static int fan53555_regulator_register(struct fan53555_device_info *di, |
| 321 | struct regulator_config *config) |
| 322 | { |
| 323 | struct regulator_desc *rdesc = &di->desc; |
| 324 | |
| 325 | rdesc->name = "fan53555-reg"; |
Heiko Stuebner | 3415d60 | 2014-09-14 21:23:01 +0200 | [diff] [blame] | 326 | rdesc->supply_name = "vin"; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 327 | rdesc->ops = &fan53555_regulator_ops; |
| 328 | rdesc->type = REGULATOR_VOLTAGE; |
| 329 | rdesc->n_voltages = FAN53555_NVOLTAGES; |
| 330 | rdesc->enable_reg = di->vol_reg; |
| 331 | rdesc->enable_mask = VSEL_BUCK_EN; |
| 332 | rdesc->min_uV = di->vsel_min; |
| 333 | rdesc->uV_step = di->vsel_step; |
| 334 | rdesc->vsel_reg = di->vol_reg; |
| 335 | rdesc->vsel_mask = VSEL_NSEL_MASK; |
| 336 | rdesc->owner = THIS_MODULE; |
| 337 | |
Axel Lin | b15f5f7 | 2013-09-03 14:27:56 +0800 | [diff] [blame] | 338 | di->rdev = devm_regulator_register(di->dev, &di->desc, config); |
Rusty Russell | 8c6ffba | 2013-07-15 11:20:32 +0930 | [diff] [blame] | 339 | return PTR_ERR_OR_ZERO(di->rdev); |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Krzysztof Kozlowski | 121b567 | 2015-01-05 10:04:19 +0100 | [diff] [blame] | 342 | static const struct regmap_config fan53555_regmap_config = { |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 343 | .reg_bits = 8, |
| 344 | .val_bits = 8, |
| 345 | }; |
| 346 | |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 347 | static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev, |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 348 | struct device_node *np, |
| 349 | const struct regulator_desc *desc) |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 350 | { |
| 351 | struct fan53555_platform_data *pdata; |
| 352 | int ret; |
| 353 | u32 tmp; |
| 354 | |
| 355 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 356 | if (!pdata) |
| 357 | return NULL; |
| 358 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 359 | pdata->regulator = of_get_regulator_init_data(dev, np, desc); |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 360 | |
| 361 | ret = of_property_read_u32(np, "fcs,suspend-voltage-selector", |
| 362 | &tmp); |
| 363 | if (!ret) |
| 364 | pdata->sleep_vsel_id = tmp; |
| 365 | |
| 366 | return pdata; |
| 367 | } |
| 368 | |
| 369 | static const struct of_device_id fan53555_dt_ids[] = { |
| 370 | { |
| 371 | .compatible = "fcs,fan53555", |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 372 | .data = (void *)FAN53555_VENDOR_FAIRCHILD |
| 373 | }, { |
| 374 | .compatible = "silergy,syr827", |
| 375 | .data = (void *)FAN53555_VENDOR_SILERGY, |
| 376 | }, { |
| 377 | .compatible = "silergy,syr828", |
| 378 | .data = (void *)FAN53555_VENDOR_SILERGY, |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 379 | }, |
| 380 | { } |
| 381 | }; |
| 382 | MODULE_DEVICE_TABLE(of, fan53555_dt_ids); |
| 383 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 384 | static int fan53555_regulator_probe(struct i2c_client *client, |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 385 | const struct i2c_device_id *id) |
| 386 | { |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 387 | struct device_node *np = client->dev.of_node; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 388 | struct fan53555_device_info *di; |
| 389 | struct fan53555_platform_data *pdata; |
| 390 | struct regulator_config config = { }; |
| 391 | unsigned int val; |
| 392 | int ret; |
| 393 | |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 394 | di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info), |
| 395 | GFP_KERNEL); |
| 396 | if (!di) |
| 397 | return -ENOMEM; |
| 398 | |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 399 | pdata = dev_get_platdata(&client->dev); |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 400 | if (!pdata) |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 401 | pdata = fan53555_parse_dt(&client->dev, np, &di->desc); |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 402 | |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 403 | if (!pdata || !pdata->regulator) { |
| 404 | dev_err(&client->dev, "Platform data not found!\n"); |
| 405 | return -ENODEV; |
| 406 | } |
| 407 | |
Axel Lin | e13426b | 2014-09-18 21:49:38 +0800 | [diff] [blame] | 408 | di->regulator = pdata->regulator; |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 409 | if (client->dev.of_node) { |
| 410 | const struct of_device_id *match; |
| 411 | |
| 412 | match = of_match_device(of_match_ptr(fan53555_dt_ids), |
| 413 | &client->dev); |
| 414 | if (!match) |
| 415 | return -ENODEV; |
| 416 | |
Heiko Stübner | 6f34163 | 2014-09-17 13:21:48 +0200 | [diff] [blame] | 417 | di->vendor = (unsigned long) match->data; |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 418 | } else { |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 419 | /* if no ramp constraint set, get the pdata ramp_delay */ |
| 420 | if (!di->regulator->constraints.ramp_delay) { |
| 421 | int slew_idx = (pdata->slew_rate & 0x7) |
| 422 | ? pdata->slew_rate : 0; |
Heiko Stuebner | dd7e71f | 2014-09-16 17:54:01 +0200 | [diff] [blame] | 423 | |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 424 | di->regulator->constraints.ramp_delay |
| 425 | = slew_rates[slew_idx]; |
| 426 | } |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 427 | |
| 428 | di->vendor = id->driver_data; |
Heiko Stuebner | dd7e71f | 2014-09-16 17:54:01 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 431 | di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config); |
| 432 | if (IS_ERR(di->regmap)) { |
| 433 | dev_err(&client->dev, "Failed to allocate regmap!\n"); |
| 434 | return PTR_ERR(di->regmap); |
| 435 | } |
| 436 | di->dev = &client->dev; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 437 | i2c_set_clientdata(client, di); |
| 438 | /* Get chip ID */ |
| 439 | ret = regmap_read(di->regmap, FAN53555_ID1, &val); |
| 440 | if (ret < 0) { |
| 441 | dev_err(&client->dev, "Failed to get chip ID!\n"); |
Sachin Kamat | 145fe1e | 2014-02-18 16:11:04 +0530 | [diff] [blame] | 442 | return ret; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 443 | } |
| 444 | di->chip_id = val & DIE_ID; |
| 445 | /* Get chip revision */ |
| 446 | ret = regmap_read(di->regmap, FAN53555_ID2, &val); |
| 447 | if (ret < 0) { |
| 448 | dev_err(&client->dev, "Failed to get chip Rev!\n"); |
Sachin Kamat | 145fe1e | 2014-02-18 16:11:04 +0530 | [diff] [blame] | 449 | return ret; |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 450 | } |
| 451 | di->chip_rev = val & DIE_REV; |
| 452 | dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n", |
| 453 | di->chip_id, di->chip_rev); |
| 454 | /* Device init */ |
| 455 | ret = fan53555_device_setup(di, pdata); |
| 456 | if (ret < 0) { |
| 457 | dev_err(&client->dev, "Failed to setup device!\n"); |
| 458 | return ret; |
| 459 | } |
| 460 | /* Register regulator */ |
| 461 | config.dev = di->dev; |
| 462 | config.init_data = di->regulator; |
| 463 | config.regmap = di->regmap; |
| 464 | config.driver_data = di; |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 465 | config.of_node = np; |
| 466 | |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 467 | ret = fan53555_regulator_register(di, &config); |
| 468 | if (ret < 0) |
| 469 | dev_err(&client->dev, "Failed to register regulator!\n"); |
| 470 | return ret; |
| 471 | |
| 472 | } |
| 473 | |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 474 | static const struct i2c_device_id fan53555_id[] = { |
Heiko Stuebner | ee30928 | 2014-09-16 21:22:54 +0200 | [diff] [blame] | 475 | { |
| 476 | .name = "fan53555", |
| 477 | .driver_data = FAN53555_VENDOR_FAIRCHILD |
| 478 | }, { |
| 479 | .name = "syr82x", |
| 480 | .driver_data = FAN53555_VENDOR_SILERGY |
| 481 | }, |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 482 | { }, |
| 483 | }; |
Javier Martinez Canillas | e80c47b | 2015-07-30 18:18:40 +0200 | [diff] [blame] | 484 | MODULE_DEVICE_TABLE(i2c, fan53555_id); |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 485 | |
| 486 | static struct i2c_driver fan53555_regulator_driver = { |
| 487 | .driver = { |
| 488 | .name = "fan53555-regulator", |
Heiko Stuebner | 91f23d8 | 2014-09-16 21:22:53 +0200 | [diff] [blame] | 489 | .of_match_table = of_match_ptr(fan53555_dt_ids), |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 490 | }, |
| 491 | .probe = fan53555_regulator_probe, |
Yunfan Zhang | 49d8c59 | 2012-09-08 03:52:18 -0700 | [diff] [blame] | 492 | .id_table = fan53555_id, |
| 493 | }; |
| 494 | |
| 495 | module_i2c_driver(fan53555_regulator_driver); |
| 496 | |
| 497 | MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>"); |
| 498 | MODULE_DESCRIPTION("FAN53555 regulator driver"); |
| 499 | MODULE_LICENSE("GPL v2"); |