1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2016 Gateworks Corporation, Inc. All Rights Reserved. 4 */ 5 #include <linux/i2c.h> 6 #include <linux/init.h> 7 #include <linux/interrupt.h> 8 #include <linux/module.h> 9 #include <linux/kernel.h> 10 #include <linux/of.h> 11 #include <linux/regmap.h> 12 #include <linux/regulator/driver.h> 13 #include <linux/regulator/machine.h> 14 #include <linux/regulator/of_regulator.h> 15 16 #define DRIVER_NAME "ltc3676" 17 18 /* LTC3676 Registers */ 19 #define LTC3676_BUCK1 0x01 20 #define LTC3676_BUCK2 0x02 21 #define LTC3676_BUCK3 0x03 22 #define LTC3676_BUCK4 0x04 23 #define LTC3676_LDOA 0x05 24 #define LTC3676_LDOB 0x06 25 #define LTC3676_SQD1 0x07 26 #define LTC3676_SQD2 0x08 27 #define LTC3676_CNTRL 0x09 28 #define LTC3676_DVB1A 0x0A 29 #define LTC3676_DVB1B 0x0B 30 #define LTC3676_DVB2A 0x0C 31 #define LTC3676_DVB2B 0x0D 32 #define LTC3676_DVB3A 0x0E 33 #define LTC3676_DVB3B 0x0F 34 #define LTC3676_DVB4A 0x10 35 #define LTC3676_DVB4B 0x11 36 #define LTC3676_MSKIRQ 0x12 37 #define LTC3676_MSKPG 0x13 38 #define LTC3676_USER 0x14 39 #define LTC3676_IRQSTAT 0x15 40 #define LTC3676_PGSTATL 0x16 41 #define LTC3676_PGSTATRT 0x17 42 #define LTC3676_HRST 0x1E 43 #define LTC3676_CLIRQ 0x1F 44 45 #define LTC3676_DVBxA_REF_SELECT BIT(5) 46 #define LTC3676_DVBxB_PGOOD_MASK BIT(5) 47 48 #define LTC3676_IRQSTAT_PGOOD_TIMEOUT BIT(3) 49 #define LTC3676_IRQSTAT_UNDERVOLT_WARN BIT(4) 50 #define LTC3676_IRQSTAT_UNDERVOLT_FAULT BIT(5) 51 #define LTC3676_IRQSTAT_THERMAL_WARN BIT(6) 52 #define LTC3676_IRQSTAT_THERMAL_FAULT BIT(7) 53 54 enum ltc3676_reg { 55 LTC3676_SW1, 56 LTC3676_SW2, 57 LTC3676_SW3, 58 LTC3676_SW4, 59 LTC3676_LDO1, 60 LTC3676_LDO2, 61 LTC3676_LDO3, 62 LTC3676_LDO4, 63 LTC3676_NUM_REGULATORS, 64 }; 65 66 struct ltc3676 { 67 struct regmap *regmap; 68 struct device *dev; 69 struct regulator_desc regulator_descs[LTC3676_NUM_REGULATORS]; 70 struct regulator_dev *regulators[LTC3676_NUM_REGULATORS]; 71 }; 72 73 static int ltc3676_set_suspend_voltage(struct regulator_dev *rdev, int uV) 74 { 75 struct ltc3676 *ltc3676 = rdev_get_drvdata(rdev); 76 struct device *dev = ltc3676->dev; 77 int dcdc = rdev_get_id(rdev); 78 int sel; 79 80 dev_dbg(dev, "%s id=%d uV=%d\n", __func__, dcdc, uV); 81 sel = regulator_map_voltage_linear(rdev, uV, uV); 82 if (sel < 0) 83 return sel; 84 85 /* DVBB register follows right after the corresponding DVBA register */ 86 return regmap_update_bits(ltc3676->regmap, rdev->desc->vsel_reg + 1, 87 rdev->desc->vsel_mask, sel); 88 } 89 90 static int ltc3676_set_suspend_mode(struct regulator_dev *rdev, 91 unsigned int mode) 92 { 93 struct ltc3676 *ltc3676= rdev_get_drvdata(rdev); 94 struct device *dev = ltc3676->dev; 95 int mask, val; 96 int dcdc = rdev_get_id(rdev); 97 98 dev_dbg(dev, "%s id=%d mode=%d\n", __func__, dcdc, mode); 99 100 mask = LTC3676_DVBxA_REF_SELECT; 101 switch (mode) { 102 case REGULATOR_MODE_STANDBY: 103 val = 0; /* select DVBxA */ 104 break; 105 case REGULATOR_MODE_NORMAL: 106 val = LTC3676_DVBxA_REF_SELECT; /* select DVBxB */ 107 break; 108 default: 109 dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n", 110 rdev->desc->name, mode); 111 return -EINVAL; 112 } 113 114 return regmap_update_bits(ltc3676->regmap, rdev->desc->vsel_reg, 115 mask, val); 116 } 117 118 static int ltc3676_set_voltage_sel(struct regulator_dev *rdev, unsigned selector) 119 { 120 struct ltc3676 *ltc3676 = rdev_get_drvdata(rdev); 121 struct device *dev = ltc3676->dev; 122 int ret, dcdc = rdev_get_id(rdev); 123 124 dev_dbg(dev, "%s id=%d selector=%d\n", __func__, dcdc, selector); 125 126 ret = regmap_update_bits(ltc3676->regmap, rdev->desc->vsel_reg + 1, 127 LTC3676_DVBxB_PGOOD_MASK, 128 LTC3676_DVBxB_PGOOD_MASK); 129 if (ret) 130 return ret; 131 132 return regulator_set_voltage_sel_regmap(rdev, selector); 133 } 134 135 static inline unsigned int ltc3676_scale(unsigned int uV, u32 r1, u32 r2) 136 { 137 uint64_t tmp; 138 if (uV == 0) 139 return 0; 140 tmp = (uint64_t)uV * r1; 141 do_div(tmp, r2); 142 return uV + (unsigned int)tmp; 143 } 144 145 static int ltc3676_of_parse_cb(struct device_node *np, 146 const struct regulator_desc *desc, 147 struct regulator_config *config) 148 { 149 struct ltc3676 *ltc3676 = config->driver_data; 150 struct regulator_desc *rdesc = <c3676->regulator_descs[desc->id]; 151 u32 r[2]; 152 int ret; 153 154 /* LDO3 has a fixed output */ 155 if (desc->id == LTC3676_LDO3) 156 return 0; 157 158 ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider", r, 2); 159 if (ret) { 160 dev_err(ltc3676->dev, "Failed to parse voltage divider: %d\n", 161 ret); 162 return ret; 163 } 164 165 rdesc->min_uV = ltc3676_scale(desc->min_uV, r[0], r[1]); 166 rdesc->uV_step = ltc3676_scale(desc->uV_step, r[0], r[1]); 167 rdesc->fixed_uV = ltc3676_scale(desc->fixed_uV, r[0], r[1]); 168 169 return 0; 170 } 171 172 /* SW1, SW2, SW3, SW4 linear 0.8V-3.3V with scalar via R1/R2 feeback res */ 173 static const struct regulator_ops ltc3676_linear_regulator_ops = { 174 .enable = regulator_enable_regmap, 175 .disable = regulator_disable_regmap, 176 .is_enabled = regulator_is_enabled_regmap, 177 .list_voltage = regulator_list_voltage_linear, 178 .set_voltage_sel = ltc3676_set_voltage_sel, 179 .get_voltage_sel = regulator_get_voltage_sel_regmap, 180 .set_suspend_voltage = ltc3676_set_suspend_voltage, 181 .set_suspend_mode = ltc3676_set_suspend_mode, 182 }; 183 184 /* LDO1 always on fixed 0.8V-3.3V via scalar via R1/R2 feeback res */ 185 static const struct regulator_ops ltc3676_fixed_standby_regulator_ops = { 186 }; 187 188 /* LDO2, LDO3 fixed (LDO2 has external scalar via R1/R2 feedback res) */ 189 static const struct regulator_ops ltc3676_fixed_regulator_ops = { 190 .enable = regulator_enable_regmap, 191 .disable = regulator_disable_regmap, 192 .is_enabled = regulator_is_enabled_regmap, 193 }; 194 195 #define LTC3676_REG(_id, _name, _ops, en_reg, en_bit, dvba_reg, dvb_mask) \ 196 [LTC3676_ ## _id] = { \ 197 .name = #_name, \ 198 .of_match = of_match_ptr(#_name), \ 199 .regulators_node = of_match_ptr("regulators"), \ 200 .of_parse_cb = ltc3676_of_parse_cb, \ 201 .n_voltages = (dvb_mask) + 1, \ 202 .min_uV = (dvba_reg) ? 412500 : 0, \ 203 .uV_step = (dvba_reg) ? 12500 : 0, \ 204 .ramp_delay = (dvba_reg) ? 800 : 0, \ 205 .fixed_uV = (dvb_mask) ? 0 : 725000, \ 206 .ops = <c3676_ ## _ops ## _regulator_ops, \ 207 .type = REGULATOR_VOLTAGE, \ 208 .id = LTC3676_ ## _id, \ 209 .owner = THIS_MODULE, \ 210 .vsel_reg = (dvba_reg), \ 211 .vsel_mask = (dvb_mask), \ 212 .enable_reg = (en_reg), \ 213 .enable_mask = (1 << en_bit), \ 214 } 215 216 #define LTC3676_LINEAR_REG(_id, _name, _en, _dvba) \ 217 LTC3676_REG(_id, _name, linear, \ 218 LTC3676_ ## _en, 7, \ 219 LTC3676_ ## _dvba, 0x1f) 220 221 #define LTC3676_FIXED_REG(_id, _name, _en_reg, _en_bit) \ 222 LTC3676_REG(_id, _name, fixed, LTC3676_ ## _en_reg, _en_bit, 0, 0) 223 224 static const struct regulator_desc ltc3676_regulators[LTC3676_NUM_REGULATORS] = { 225 LTC3676_LINEAR_REG(SW1, sw1, BUCK1, DVB1A), 226 LTC3676_LINEAR_REG(SW2, sw2, BUCK2, DVB2A), 227 LTC3676_LINEAR_REG(SW3, sw3, BUCK3, DVB3A), 228 LTC3676_LINEAR_REG(SW4, sw4, BUCK4, DVB4A), 229 LTC3676_REG(LDO1, ldo1, fixed_standby, 0, 0, 0, 0), 230 LTC3676_FIXED_REG(LDO2, ldo2, LDOA, 2), 231 LTC3676_FIXED_REG(LDO3, ldo3, LDOA, 5), 232 LTC3676_FIXED_REG(LDO4, ldo4, LDOB, 2), 233 }; 234 235 static bool ltc3676_readable_writeable_reg(struct device *dev, unsigned int reg) 236 { 237 switch (reg) { 238 case LTC3676_BUCK1 ... LTC3676_IRQSTAT: 239 case LTC3676_HRST: 240 case LTC3676_CLIRQ: 241 return true; 242 } 243 return false; 244 } 245 246 static bool ltc3676_volatile_reg(struct device *dev, unsigned int reg) 247 { 248 switch (reg) { 249 case LTC3676_IRQSTAT ... LTC3676_PGSTATRT: 250 return true; 251 } 252 return false; 253 } 254 255 static const struct regmap_config ltc3676_regmap_config = { 256 .reg_bits = 8, 257 .val_bits = 8, 258 .writeable_reg = ltc3676_readable_writeable_reg, 259 .readable_reg = ltc3676_readable_writeable_reg, 260 .volatile_reg = ltc3676_volatile_reg, 261 .max_register = LTC3676_CLIRQ, 262 .use_single_read = true, 263 .use_single_write = true, 264 .cache_type = REGCACHE_RBTREE, 265 }; 266 267 static irqreturn_t ltc3676_isr(int irq, void *dev_id) 268 { 269 struct ltc3676 *ltc3676 = dev_id; 270 struct device *dev = ltc3676->dev; 271 unsigned int i, irqstat, event; 272 273 regmap_read(ltc3676->regmap, LTC3676_IRQSTAT, &irqstat); 274 275 dev_dbg(dev, "irq%d irqstat=0x%02x\n", irq, irqstat); 276 if (irqstat & LTC3676_IRQSTAT_THERMAL_WARN) { 277 dev_warn(dev, "Over-temperature Warning\n"); 278 event = REGULATOR_EVENT_OVER_TEMP; 279 for (i = 0; i < LTC3676_NUM_REGULATORS; i++) 280 regulator_notifier_call_chain(ltc3676->regulators[i], 281 event, NULL); 282 } 283 284 if (irqstat & LTC3676_IRQSTAT_UNDERVOLT_WARN) { 285 dev_info(dev, "Undervoltage Warning\n"); 286 event = REGULATOR_EVENT_UNDER_VOLTAGE; 287 for (i = 0; i < LTC3676_NUM_REGULATORS; i++) 288 regulator_notifier_call_chain(ltc3676->regulators[i], 289 event, NULL); 290 } 291 292 /* Clear warning condition */ 293 regmap_write(ltc3676->regmap, LTC3676_CLIRQ, 0); 294 295 return IRQ_HANDLED; 296 } 297 298 static int ltc3676_regulator_probe(struct i2c_client *client) 299 { 300 struct device *dev = &client->dev; 301 struct regulator_init_data *init_data = dev_get_platdata(dev); 302 struct regulator_desc *descs; 303 struct ltc3676 *ltc3676; 304 int i, ret; 305 306 ltc3676 = devm_kzalloc(dev, sizeof(*ltc3676), GFP_KERNEL); 307 if (!ltc3676) 308 return -ENOMEM; 309 310 i2c_set_clientdata(client, ltc3676); 311 ltc3676->dev = dev; 312 313 descs = ltc3676->regulator_descs; 314 memcpy(descs, ltc3676_regulators, sizeof(ltc3676_regulators)); 315 descs[LTC3676_LDO3].fixed_uV = 1800000; /* LDO3 is fixed 1.8V */ 316 317 ltc3676->regmap = devm_regmap_init_i2c(client, <c3676_regmap_config); 318 if (IS_ERR(ltc3676->regmap)) { 319 ret = PTR_ERR(ltc3676->regmap); 320 dev_err(dev, "failed to initialize regmap: %d\n", ret); 321 return ret; 322 } 323 324 for (i = 0; i < LTC3676_NUM_REGULATORS; i++) { 325 struct regulator_desc *desc = <c3676->regulator_descs[i]; 326 struct regulator_config config = { }; 327 328 if (init_data) 329 config.init_data = &init_data[i]; 330 331 config.dev = dev; 332 config.driver_data = ltc3676; 333 334 ltc3676->regulators[i] = devm_regulator_register(dev, desc, 335 &config); 336 if (IS_ERR(ltc3676->regulators[i])) { 337 ret = PTR_ERR(ltc3676->regulators[i]); 338 dev_err(dev, "failed to register regulator %s: %d\n", 339 desc->name, ret); 340 return ret; 341 } 342 } 343 344 regmap_write(ltc3676->regmap, LTC3676_CLIRQ, 0); 345 if (client->irq) { 346 ret = devm_request_threaded_irq(dev, client->irq, NULL, 347 ltc3676_isr, 348 IRQF_TRIGGER_LOW | IRQF_ONESHOT, 349 client->name, ltc3676); 350 if (ret) { 351 dev_err(dev, "Failed to request IRQ: %d\n", ret); 352 return ret; 353 } 354 } 355 356 return 0; 357 } 358 359 static const struct i2c_device_id ltc3676_i2c_id[] = { 360 { "ltc3676" }, 361 { } 362 }; 363 MODULE_DEVICE_TABLE(i2c, ltc3676_i2c_id); 364 365 static const struct of_device_id __maybe_unused ltc3676_of_match[] = { 366 { .compatible = "lltc,ltc3676" }, 367 { }, 368 }; 369 MODULE_DEVICE_TABLE(of, ltc3676_of_match); 370 371 static struct i2c_driver ltc3676_driver = { 372 .driver = { 373 .name = DRIVER_NAME, 374 .of_match_table = of_match_ptr(ltc3676_of_match), 375 }, 376 .probe_new = ltc3676_regulator_probe, 377 .id_table = ltc3676_i2c_id, 378 }; 379 module_i2c_driver(ltc3676_driver); 380 381 MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>"); 382 MODULE_DESCRIPTION("Regulator driver for Linear Technology LTC3676"); 383 MODULE_LICENSE("GPL v2"); 384