Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

power: supply: smb347-charger: Implement USB VBUS regulator

SMB347 can supply power to USB VBUS, implement the USB VBUS regulator.
USB VBUS needs to be powered for switching OTG-cable USB port into host
mode.

Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>

authored by

Dmitry Osipenko and committed by
Sebastian Reichel
565efae9 efe21754

+220
+1
drivers/power/supply/Kconfig
··· 680 680 config CHARGER_SMB347 681 681 tristate "Summit Microelectronics SMB3XX Battery Charger" 682 682 depends on I2C 683 + depends on REGULATOR 683 684 select REGMAP_I2C 684 685 help 685 686 Say Y to include support for Summit Microelectronics SMB345,
+219
drivers/power/supply/smb347-charger.c
··· 18 18 #include <linux/power_supply.h> 19 19 #include <linux/property.h> 20 20 #include <linux/regmap.h> 21 + #include <linux/regulator/driver.h> 21 22 22 23 #include <dt-bindings/power/summit,smb347-charger.h> 23 24 ··· 64 63 #define CFG_THERM_SOFT_COLD_COMPENSATION_SHIFT 2 65 64 #define CFG_THERM_MONITOR_DISABLED BIT(4) 66 65 #define CFG_SYSOK 0x08 66 + #define CFG_SYSOK_INOK_ACTIVE_HIGH BIT(0) 67 67 #define CFG_SYSOK_SUSPEND_HARD_LIMIT_DISABLED BIT(2) 68 68 #define CFG_OTHER 0x09 69 69 #define CFG_OTHER_RID_MASK 0xc0 70 70 #define CFG_OTHER_RID_ENABLED_AUTO_OTG 0xc0 71 71 #define CFG_OTG 0x0a 72 72 #define CFG_OTG_TEMP_THRESHOLD_MASK 0x30 73 + #define CFG_OTG_CURRENT_LIMIT_250mA BIT(2) 74 + #define CFG_OTG_CURRENT_LIMIT_750mA BIT(3) 73 75 #define CFG_OTG_TEMP_THRESHOLD_SHIFT 4 74 76 #define CFG_OTG_CC_COMPENSATION_MASK 0xc0 75 77 #define CFG_OTG_CC_COMPENSATION_SHIFT 6 ··· 96 92 #define CMD_A 0x30 97 93 #define CMD_A_CHG_ENABLED BIT(1) 98 94 #define CMD_A_SUSPEND_ENABLED BIT(2) 95 + #define CMD_A_OTG_ENABLED BIT(4) 99 96 #define CMD_A_ALLOW_WRITE BIT(7) 100 97 #define CMD_B 0x31 101 98 #define CMD_C 0x33 ··· 138 133 * @regmap: pointer to driver regmap 139 134 * @mains: power_supply instance for AC/DC power 140 135 * @usb: power_supply instance for USB power 136 + * @usb_rdev: USB VBUS regulator device 141 137 * @id: SMB charger ID 142 138 * @mains_online: is AC/DC input connected 143 139 * @usb_online: is USB input connected 144 140 * @irq_unsupported: is interrupt unsupported by SMB hardware 141 + * @usb_vbus_enabled: is USB VBUS powered by SMB charger 145 142 * @max_charge_current: maximum current (in uA) the battery can be charged 146 143 * @max_charge_voltage: maximum voltage (in uV) the battery can be charged 147 144 * @pre_charge_current: current (in uA) to use in pre-charging phase ··· 174 167 * @use_usb_otg: USB OTG output can be used (not implemented yet) 175 168 * @enable_control: how charging enable/disable is controlled 176 169 * (driver/pin controls) 170 + * @inok_polarity: polarity of INOK signal which denotes presence of external 171 + * power supply 177 172 * 178 173 * @use_main, @use_usb, and @use_usb_otg are means to enable/disable 179 174 * hardware support for these. This is useful when we want to have for ··· 198 189 struct regmap *regmap; 199 190 struct power_supply *mains; 200 191 struct power_supply *usb; 192 + struct regulator_dev *usb_rdev; 201 193 unsigned int id; 202 194 bool mains_online; 203 195 bool usb_online; 204 196 bool irq_unsupported; 197 + bool usb_vbus_enabled; 205 198 206 199 unsigned int max_charge_current; 207 200 unsigned int max_charge_voltage; ··· 224 213 bool use_usb; 225 214 bool use_usb_otg; 226 215 unsigned int enable_control; 216 + unsigned int inok_polarity; 227 217 }; 228 218 229 219 enum smb_charger_chipid { ··· 371 359 { 372 360 if (smb->enable_control != SMB3XX_CHG_ENABLE_SW) { 373 361 dev_dbg(smb->dev, "charging enable/disable in SW disabled\n"); 362 + return 0; 363 + } 364 + 365 + if (enable && smb->usb_vbus_enabled) { 366 + dev_dbg(smb->dev, "charging not enabled because USB is in host mode\n"); 374 367 return 0; 375 368 } 376 369 ··· 1270 1253 /* Select charging control */ 1271 1254 device_property_read_u32(dev, "summit,enable-charge-control", 1272 1255 &smb->enable_control); 1256 + 1257 + /* 1258 + * Polarity of INOK signal indicating presence of external power 1259 + * supply connected to the charger. 1260 + */ 1261 + device_property_read_u32(dev, "summit,inok-polarity", 1262 + &smb->inok_polarity); 1273 1263 } 1274 1264 1275 1265 static int smb347_get_battery_info(struct smb347_charger *smb) ··· 1328 1304 return 0; 1329 1305 } 1330 1306 1307 + static int smb347_usb_vbus_get_current_limit(struct regulator_dev *rdev) 1308 + { 1309 + struct smb347_charger *smb = rdev_get_drvdata(rdev); 1310 + unsigned int val; 1311 + int ret; 1312 + 1313 + ret = regmap_read(smb->regmap, CFG_OTG, &val); 1314 + if (ret < 0) 1315 + return ret; 1316 + 1317 + /* 1318 + * It's unknown what happens if this bit is unset due to lack of 1319 + * access to the datasheet, assume it's limit-enable. 1320 + */ 1321 + if (!(val & CFG_OTG_CURRENT_LIMIT_250mA)) 1322 + return 0; 1323 + 1324 + return val & CFG_OTG_CURRENT_LIMIT_750mA ? 750000 : 250000; 1325 + } 1326 + 1327 + static int smb347_usb_vbus_set_new_current_limit(struct smb347_charger *smb, 1328 + int max_uA) 1329 + { 1330 + const unsigned int mask = CFG_OTG_CURRENT_LIMIT_750mA | 1331 + CFG_OTG_CURRENT_LIMIT_250mA; 1332 + unsigned int val = CFG_OTG_CURRENT_LIMIT_250mA; 1333 + int ret; 1334 + 1335 + if (max_uA >= 750000) 1336 + val |= CFG_OTG_CURRENT_LIMIT_750mA; 1337 + 1338 + ret = regmap_update_bits(smb->regmap, CFG_OTG, mask, val); 1339 + if (ret < 0) 1340 + dev_err(smb->dev, "failed to change USB current limit\n"); 1341 + 1342 + return ret; 1343 + } 1344 + 1345 + static int smb347_usb_vbus_set_current_limit(struct regulator_dev *rdev, 1346 + int min_uA, int max_uA) 1347 + { 1348 + struct smb347_charger *smb = rdev_get_drvdata(rdev); 1349 + int ret; 1350 + 1351 + ret = smb347_set_writable(smb, true, true); 1352 + if (ret < 0) 1353 + return ret; 1354 + 1355 + ret = smb347_usb_vbus_set_new_current_limit(smb, max_uA); 1356 + smb347_set_writable(smb, false, true); 1357 + 1358 + return ret; 1359 + } 1360 + 1361 + static int smb347_usb_vbus_regulator_enable(struct regulator_dev *rdev) 1362 + { 1363 + struct smb347_charger *smb = rdev_get_drvdata(rdev); 1364 + int ret, max_uA; 1365 + 1366 + ret = smb347_set_writable(smb, true, true); 1367 + if (ret < 0) 1368 + return ret; 1369 + 1370 + smb347_charging_disable(smb); 1371 + 1372 + if (device_property_read_bool(&rdev->dev, "summit,needs-inok-toggle")) { 1373 + unsigned int sysok = 0; 1374 + 1375 + if (smb->inok_polarity == SMB3XX_SYSOK_INOK_ACTIVE_LOW) 1376 + sysok = CFG_SYSOK_INOK_ACTIVE_HIGH; 1377 + 1378 + /* 1379 + * VBUS won't be powered if INOK is active, so we need to 1380 + * manually disable INOK on some platforms. 1381 + */ 1382 + ret = regmap_update_bits(smb->regmap, CFG_SYSOK, 1383 + CFG_SYSOK_INOK_ACTIVE_HIGH, sysok); 1384 + if (ret < 0) { 1385 + dev_err(smb->dev, "failed to disable INOK\n"); 1386 + goto done; 1387 + } 1388 + } 1389 + 1390 + ret = smb347_usb_vbus_get_current_limit(rdev); 1391 + if (ret < 0) { 1392 + dev_err(smb->dev, "failed to get USB VBUS current limit\n"); 1393 + goto done; 1394 + } 1395 + 1396 + max_uA = ret; 1397 + 1398 + ret = smb347_usb_vbus_set_new_current_limit(smb, 250000); 1399 + if (ret < 0) { 1400 + dev_err(smb->dev, "failed to preset USB VBUS current limit\n"); 1401 + goto done; 1402 + } 1403 + 1404 + ret = regmap_set_bits(smb->regmap, CMD_A, CMD_A_OTG_ENABLED); 1405 + if (ret < 0) { 1406 + dev_err(smb->dev, "failed to enable USB VBUS\n"); 1407 + goto done; 1408 + } 1409 + 1410 + smb->usb_vbus_enabled = true; 1411 + 1412 + ret = smb347_usb_vbus_set_new_current_limit(smb, max_uA); 1413 + if (ret < 0) { 1414 + dev_err(smb->dev, "failed to restore USB VBUS current limit\n"); 1415 + goto done; 1416 + } 1417 + done: 1418 + smb347_set_writable(smb, false, true); 1419 + 1420 + return ret; 1421 + } 1422 + 1423 + static int smb347_usb_vbus_regulator_disable(struct regulator_dev *rdev) 1424 + { 1425 + struct smb347_charger *smb = rdev_get_drvdata(rdev); 1426 + int ret; 1427 + 1428 + ret = smb347_set_writable(smb, true, true); 1429 + if (ret < 0) 1430 + return ret; 1431 + 1432 + ret = regmap_clear_bits(smb->regmap, CMD_A, CMD_A_OTG_ENABLED); 1433 + if (ret < 0) { 1434 + dev_err(smb->dev, "failed to disable USB VBUS\n"); 1435 + goto done; 1436 + } 1437 + 1438 + smb->usb_vbus_enabled = false; 1439 + 1440 + if (device_property_read_bool(&rdev->dev, "summit,needs-inok-toggle")) { 1441 + unsigned int sysok = 0; 1442 + 1443 + if (smb->inok_polarity == SMB3XX_SYSOK_INOK_ACTIVE_HIGH) 1444 + sysok = CFG_SYSOK_INOK_ACTIVE_HIGH; 1445 + 1446 + ret = regmap_update_bits(smb->regmap, CFG_SYSOK, 1447 + CFG_SYSOK_INOK_ACTIVE_HIGH, sysok); 1448 + if (ret < 0) { 1449 + dev_err(smb->dev, "failed to enable INOK\n"); 1450 + goto done; 1451 + } 1452 + } 1453 + 1454 + smb347_start_stop_charging(smb); 1455 + done: 1456 + smb347_set_writable(smb, false, true); 1457 + 1458 + return ret; 1459 + } 1460 + 1331 1461 static const struct regmap_config smb347_regmap = { 1332 1462 .reg_bits = 8, 1333 1463 .val_bits = 8, ··· 1490 1312 .readable_reg = smb347_readable_reg, 1491 1313 .cache_type = REGCACHE_FLAT, 1492 1314 .num_reg_defaults_raw = SMB347_MAX_REGISTER, 1315 + }; 1316 + 1317 + static const struct regulator_ops smb347_usb_vbus_regulator_ops = { 1318 + .is_enabled = regulator_is_enabled_regmap, 1319 + .enable = smb347_usb_vbus_regulator_enable, 1320 + .disable = smb347_usb_vbus_regulator_disable, 1321 + .get_current_limit = smb347_usb_vbus_get_current_limit, 1322 + .set_current_limit = smb347_usb_vbus_set_current_limit, 1493 1323 }; 1494 1324 1495 1325 static const struct power_supply_desc smb347_mains_desc = { ··· 1516 1330 .num_properties = ARRAY_SIZE(smb347_properties), 1517 1331 }; 1518 1332 1333 + static const struct regulator_desc smb347_usb_vbus_regulator_desc = { 1334 + .name = "smb347-usb-vbus", 1335 + .of_match = of_match_ptr("usb-vbus"), 1336 + .ops = &smb347_usb_vbus_regulator_ops, 1337 + .type = REGULATOR_VOLTAGE, 1338 + .owner = THIS_MODULE, 1339 + .enable_reg = CMD_A, 1340 + .enable_mask = CMD_A_OTG_ENABLED, 1341 + .enable_val = CMD_A_OTG_ENABLED, 1342 + .fixed_uV = 5000000, 1343 + .n_voltages = 1, 1344 + }; 1345 + 1519 1346 static int smb347_probe(struct i2c_client *client, 1520 1347 const struct i2c_device_id *id) 1521 1348 { 1522 1349 struct power_supply_config mains_usb_cfg = {}; 1350 + struct regulator_config usb_rdev_cfg = {}; 1523 1351 struct device *dev = &client->dev; 1524 1352 struct smb347_charger *smb; 1525 1353 int ret; ··· 1581 1381 if (ret) 1582 1382 return ret; 1583 1383 1384 + usb_rdev_cfg.dev = dev; 1385 + usb_rdev_cfg.driver_data = smb; 1386 + usb_rdev_cfg.regmap = smb->regmap; 1387 + 1388 + smb->usb_rdev = devm_regulator_register(dev, 1389 + &smb347_usb_vbus_regulator_desc, 1390 + &usb_rdev_cfg); 1391 + if (IS_ERR(smb->usb_rdev)) { 1392 + smb347_irq_disable(smb); 1393 + return PTR_ERR(smb->usb_rdev); 1394 + } 1395 + 1584 1396 return 0; 1585 1397 } 1586 1398 ··· 1600 1388 { 1601 1389 struct smb347_charger *smb = i2c_get_clientdata(client); 1602 1390 1391 + smb347_usb_vbus_regulator_disable(smb->usb_rdev); 1603 1392 smb347_irq_disable(smb); 1604 1393 1605 1394 return 0; 1395 + } 1396 + 1397 + static void smb347_shutdown(struct i2c_client *client) 1398 + { 1399 + smb347_remove(client); 1606 1400 } 1607 1401 1608 1402 static const struct i2c_device_id smb347_id[] = { ··· 1634 1416 }, 1635 1417 .probe = smb347_probe, 1636 1418 .remove = smb347_remove, 1419 + .shutdown = smb347_shutdown, 1637 1420 .id_table = smb347_id, 1638 1421 }; 1639 1422 module_i2c_driver(smb347_driver);