ucs1002_power.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for UCS1002 Programmable USB Port Power Controller
  4. *
  5. * Copyright (C) 2019 Zodiac Inflight Innovations
  6. */
  7. #include <linux/bits.h>
  8. #include <linux/freezer.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/i2c.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/kthread.h>
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/power_supply.h>
  19. #include <linux/regmap.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/of_regulator.h>
  22. /* UCS1002 Registers */
  23. #define UCS1002_REG_CURRENT_MEASUREMENT 0x00
  24. /*
  25. * The Total Accumulated Charge registers store the total accumulated
  26. * charge delivered from the VS source to a portable device. The total
  27. * value is calculated using four registers, from 01h to 04h. The bit
  28. * weighting of the registers is given in mA/hrs.
  29. */
  30. #define UCS1002_REG_TOTAL_ACC_CHARGE 0x01
  31. /* Other Status Register */
  32. #define UCS1002_REG_OTHER_STATUS 0x0f
  33. # define F_ADET_PIN BIT(4)
  34. # define F_CHG_ACT BIT(3)
  35. /* Interrupt Status */
  36. #define UCS1002_REG_INTERRUPT_STATUS 0x10
  37. # define F_ERR BIT(7)
  38. # define F_DISCHARGE_ERR BIT(6)
  39. # define F_RESET BIT(5)
  40. # define F_MIN_KEEP_OUT BIT(4)
  41. # define F_TSD BIT(3)
  42. # define F_OVER_VOLT BIT(2)
  43. # define F_BACK_VOLT BIT(1)
  44. # define F_OVER_ILIM BIT(0)
  45. /* Pin Status Register */
  46. #define UCS1002_REG_PIN_STATUS 0x14
  47. # define UCS1002_PWR_STATE_MASK 0x03
  48. # define F_PWR_EN_PIN BIT(6)
  49. # define F_M2_PIN BIT(5)
  50. # define F_M1_PIN BIT(4)
  51. # define F_EM_EN_PIN BIT(3)
  52. # define F_SEL_PIN BIT(2)
  53. # define F_ACTIVE_MODE_MASK GENMASK(5, 3)
  54. # define F_ACTIVE_MODE_PASSTHROUGH F_M2_PIN
  55. # define F_ACTIVE_MODE_DEDICATED F_EM_EN_PIN
  56. # define F_ACTIVE_MODE_BC12_DCP (F_M2_PIN | F_EM_EN_PIN)
  57. # define F_ACTIVE_MODE_BC12_SDP F_M1_PIN
  58. # define F_ACTIVE_MODE_BC12_CDP (F_M1_PIN | F_M2_PIN | F_EM_EN_PIN)
  59. /* General Configuration Register */
  60. #define UCS1002_REG_GENERAL_CFG 0x15
  61. # define F_RATION_EN BIT(3)
  62. /* Emulation Configuration Register */
  63. #define UCS1002_REG_EMU_CFG 0x16
  64. /* Switch Configuration Register */
  65. #define UCS1002_REG_SWITCH_CFG 0x17
  66. # define F_PIN_IGNORE BIT(7)
  67. # define F_EM_EN_SET BIT(5)
  68. # define F_M2_SET BIT(4)
  69. # define F_M1_SET BIT(3)
  70. # define F_S0_SET BIT(2)
  71. # define F_PWR_EN_SET BIT(1)
  72. # define F_LATCH_SET BIT(0)
  73. # define V_SET_ACTIVE_MODE_MASK GENMASK(5, 3)
  74. # define V_SET_ACTIVE_MODE_PASSTHROUGH F_M2_SET
  75. # define V_SET_ACTIVE_MODE_DEDICATED F_EM_EN_SET
  76. # define V_SET_ACTIVE_MODE_BC12_DCP (F_M2_SET | F_EM_EN_SET)
  77. # define V_SET_ACTIVE_MODE_BC12_SDP F_M1_SET
  78. # define V_SET_ACTIVE_MODE_BC12_CDP (F_M1_SET | F_M2_SET | F_EM_EN_SET)
  79. /* Current Limit Register */
  80. #define UCS1002_REG_ILIMIT 0x19
  81. # define UCS1002_ILIM_SW_MASK GENMASK(3, 0)
  82. /* Product ID */
  83. #define UCS1002_REG_PRODUCT_ID 0xfd
  84. # define UCS1002_PRODUCT_ID 0x4e
  85. /* Manufacture name */
  86. #define UCS1002_MANUFACTURER "SMSC"
  87. struct ucs1002_info {
  88. struct power_supply *charger;
  89. struct i2c_client *client;
  90. struct regmap *regmap;
  91. struct regulator_desc *regulator_descriptor;
  92. struct regulator_dev *rdev;
  93. bool present;
  94. bool output_disable;
  95. struct delayed_work health_poll;
  96. int health;
  97. };
  98. static enum power_supply_property ucs1002_props[] = {
  99. POWER_SUPPLY_PROP_ONLINE,
  100. POWER_SUPPLY_PROP_CHARGE_NOW,
  101. POWER_SUPPLY_PROP_CURRENT_NOW,
  102. POWER_SUPPLY_PROP_CURRENT_MAX,
  103. POWER_SUPPLY_PROP_PRESENT, /* the presence of PED */
  104. POWER_SUPPLY_PROP_MANUFACTURER,
  105. POWER_SUPPLY_PROP_USB_TYPE,
  106. POWER_SUPPLY_PROP_HEALTH,
  107. };
  108. static int ucs1002_get_online(struct ucs1002_info *info,
  109. union power_supply_propval *val)
  110. {
  111. unsigned int reg;
  112. int ret;
  113. ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, &reg);
  114. if (ret)
  115. return ret;
  116. val->intval = !!(reg & F_CHG_ACT);
  117. return 0;
  118. }
  119. static int ucs1002_get_charge(struct ucs1002_info *info,
  120. union power_supply_propval *val)
  121. {
  122. /*
  123. * To fit within 32 bits some values are rounded (uA/h)
  124. *
  125. * For Total Accumulated Charge Middle Low Byte register, addr
  126. * 03h, byte 2
  127. *
  128. * B0: 0.01084 mA/h rounded to 11 uA/h
  129. * B1: 0.02169 mA/h rounded to 22 uA/h
  130. * B2: 0.04340 mA/h rounded to 43 uA/h
  131. * B3: 0.08676 mA/h rounded to 87 uA/h
  132. * B4: 0.17350 mA/h rounded to 173 uÁ/h
  133. *
  134. * For Total Accumulated Charge Low Byte register, addr 04h,
  135. * byte 3
  136. *
  137. * B6: 0.00271 mA/h rounded to 3 uA/h
  138. * B7: 0.005422 mA/h rounded to 5 uA/h
  139. */
  140. static const int bit_weights_uAh[BITS_PER_TYPE(u32)] = {
  141. /*
  142. * Bit corresponding to low byte (offset 0x04)
  143. * B0 B1 B2 B3 B4 B5 B6 B7
  144. */
  145. 0, 0, 0, 0, 0, 0, 3, 5,
  146. /*
  147. * Bit corresponding to middle low byte (offset 0x03)
  148. * B0 B1 B2 B3 B4 B5 B6 B7
  149. */
  150. 11, 22, 43, 87, 173, 347, 694, 1388,
  151. /*
  152. * Bit corresponding to middle high byte (offset 0x02)
  153. * B0 B1 B2 B3 B4 B5 B6 B7
  154. */
  155. 2776, 5552, 11105, 22210, 44420, 88840, 177700, 355400,
  156. /*
  157. * Bit corresponding to high byte (offset 0x01)
  158. * B0 B1 B2 B3 B4 B5 B6 B7
  159. */
  160. 710700, 1421000, 2843000, 5685000, 11371000, 22742000,
  161. 45484000, 90968000,
  162. };
  163. unsigned long total_acc_charger;
  164. unsigned int reg;
  165. int i, ret;
  166. ret = regmap_bulk_read(info->regmap, UCS1002_REG_TOTAL_ACC_CHARGE,
  167. &reg, sizeof(u32));
  168. if (ret)
  169. return ret;
  170. total_acc_charger = be32_to_cpu(reg); /* BE as per offsets above */
  171. val->intval = 0;
  172. for_each_set_bit(i, &total_acc_charger, ARRAY_SIZE(bit_weights_uAh))
  173. val->intval += bit_weights_uAh[i];
  174. return 0;
  175. }
  176. static int ucs1002_get_current(struct ucs1002_info *info,
  177. union power_supply_propval *val)
  178. {
  179. /*
  180. * The Current Measurement register stores the measured
  181. * current value delivered to the portable device. The range
  182. * is from 9.76 mA to 2.5 A.
  183. */
  184. static const int bit_weights_uA[BITS_PER_TYPE(u8)] = {
  185. 9760, 19500, 39000, 78100, 156200, 312300, 624600, 1249300,
  186. };
  187. unsigned long current_measurement;
  188. unsigned int reg;
  189. int i, ret;
  190. ret = regmap_read(info->regmap, UCS1002_REG_CURRENT_MEASUREMENT, &reg);
  191. if (ret)
  192. return ret;
  193. current_measurement = reg;
  194. val->intval = 0;
  195. for_each_set_bit(i, &current_measurement, ARRAY_SIZE(bit_weights_uA))
  196. val->intval += bit_weights_uA[i];
  197. return 0;
  198. }
  199. /*
  200. * The Current Limit register stores the maximum current used by the
  201. * port switch. The range is from 500mA to 2.5 A.
  202. */
  203. static const u32 ucs1002_current_limit_uA[] = {
  204. 500000, 900000, 1000000, 1200000, 1500000, 1800000, 2000000, 2500000,
  205. };
  206. static int ucs1002_get_max_current(struct ucs1002_info *info,
  207. union power_supply_propval *val)
  208. {
  209. unsigned int reg;
  210. int ret;
  211. if (info->output_disable) {
  212. val->intval = 0;
  213. return 0;
  214. }
  215. ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, &reg);
  216. if (ret)
  217. return ret;
  218. val->intval = ucs1002_current_limit_uA[reg & UCS1002_ILIM_SW_MASK];
  219. return 0;
  220. }
  221. static int ucs1002_set_max_current(struct ucs1002_info *info, u32 val)
  222. {
  223. unsigned int reg;
  224. int ret, idx;
  225. if (val == 0) {
  226. info->output_disable = true;
  227. regulator_disable_regmap(info->rdev);
  228. return 0;
  229. }
  230. for (idx = 0; idx < ARRAY_SIZE(ucs1002_current_limit_uA); idx++) {
  231. if (val == ucs1002_current_limit_uA[idx])
  232. break;
  233. }
  234. if (idx == ARRAY_SIZE(ucs1002_current_limit_uA))
  235. return -EINVAL;
  236. ret = regmap_write(info->regmap, UCS1002_REG_ILIMIT, idx);
  237. if (ret)
  238. return ret;
  239. /*
  240. * Any current limit setting exceeding the one set via ILIM
  241. * pin will be rejected, so we read out freshly changed limit
  242. * to make sure that it took effect.
  243. */
  244. ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, &reg);
  245. if (ret)
  246. return ret;
  247. if (reg != idx)
  248. return -EINVAL;
  249. info->output_disable = false;
  250. if (info->rdev && info->rdev->use_count &&
  251. !regulator_is_enabled_regmap(info->rdev))
  252. regulator_enable_regmap(info->rdev);
  253. return 0;
  254. }
  255. static int ucs1002_set_usb_type(struct ucs1002_info *info, int val)
  256. {
  257. unsigned int mode;
  258. switch (val) {
  259. /*
  260. * POWER_SUPPLY_USB_TYPE_UNKNOWN == 0, map this to dedicated for
  261. * userspace API compatibility with older versions of this driver
  262. * which mapped 0 to dedicated.
  263. */
  264. case POWER_SUPPLY_USB_TYPE_UNKNOWN:
  265. case POWER_SUPPLY_USB_TYPE_PD:
  266. mode = V_SET_ACTIVE_MODE_DEDICATED;
  267. break;
  268. case POWER_SUPPLY_USB_TYPE_SDP:
  269. mode = V_SET_ACTIVE_MODE_BC12_SDP;
  270. break;
  271. case POWER_SUPPLY_USB_TYPE_DCP:
  272. mode = V_SET_ACTIVE_MODE_BC12_DCP;
  273. break;
  274. case POWER_SUPPLY_USB_TYPE_CDP:
  275. mode = V_SET_ACTIVE_MODE_BC12_CDP;
  276. break;
  277. default:
  278. return -EINVAL;
  279. }
  280. return regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG,
  281. V_SET_ACTIVE_MODE_MASK, mode);
  282. }
  283. static int ucs1002_get_usb_type(struct ucs1002_info *info,
  284. union power_supply_propval *val)
  285. {
  286. enum power_supply_usb_type type;
  287. unsigned int reg;
  288. int ret;
  289. ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, &reg);
  290. if (ret)
  291. return ret;
  292. switch (reg & F_ACTIVE_MODE_MASK) {
  293. default:
  294. type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
  295. break;
  296. case F_ACTIVE_MODE_DEDICATED:
  297. type = POWER_SUPPLY_USB_TYPE_PD;
  298. break;
  299. case F_ACTIVE_MODE_BC12_SDP:
  300. type = POWER_SUPPLY_USB_TYPE_SDP;
  301. break;
  302. case F_ACTIVE_MODE_BC12_DCP:
  303. type = POWER_SUPPLY_USB_TYPE_DCP;
  304. break;
  305. case F_ACTIVE_MODE_BC12_CDP:
  306. type = POWER_SUPPLY_USB_TYPE_CDP;
  307. break;
  308. }
  309. val->intval = type;
  310. return 0;
  311. }
  312. static int ucs1002_get_property(struct power_supply *psy,
  313. enum power_supply_property psp,
  314. union power_supply_propval *val)
  315. {
  316. struct ucs1002_info *info = power_supply_get_drvdata(psy);
  317. switch (psp) {
  318. case POWER_SUPPLY_PROP_ONLINE:
  319. return ucs1002_get_online(info, val);
  320. case POWER_SUPPLY_PROP_CHARGE_NOW:
  321. return ucs1002_get_charge(info, val);
  322. case POWER_SUPPLY_PROP_CURRENT_NOW:
  323. return ucs1002_get_current(info, val);
  324. case POWER_SUPPLY_PROP_CURRENT_MAX:
  325. return ucs1002_get_max_current(info, val);
  326. case POWER_SUPPLY_PROP_USB_TYPE:
  327. return ucs1002_get_usb_type(info, val);
  328. case POWER_SUPPLY_PROP_HEALTH:
  329. val->intval = info->health;
  330. return 0;
  331. case POWER_SUPPLY_PROP_PRESENT:
  332. val->intval = info->present;
  333. return 0;
  334. case POWER_SUPPLY_PROP_MANUFACTURER:
  335. val->strval = UCS1002_MANUFACTURER;
  336. return 0;
  337. default:
  338. return -EINVAL;
  339. }
  340. }
  341. static int ucs1002_set_property(struct power_supply *psy,
  342. enum power_supply_property psp,
  343. const union power_supply_propval *val)
  344. {
  345. struct ucs1002_info *info = power_supply_get_drvdata(psy);
  346. switch (psp) {
  347. case POWER_SUPPLY_PROP_CURRENT_MAX:
  348. return ucs1002_set_max_current(info, val->intval);
  349. case POWER_SUPPLY_PROP_USB_TYPE:
  350. return ucs1002_set_usb_type(info, val->intval);
  351. default:
  352. return -EINVAL;
  353. }
  354. }
  355. static int ucs1002_property_is_writeable(struct power_supply *psy,
  356. enum power_supply_property psp)
  357. {
  358. switch (psp) {
  359. case POWER_SUPPLY_PROP_CURRENT_MAX:
  360. case POWER_SUPPLY_PROP_USB_TYPE:
  361. return true;
  362. default:
  363. return false;
  364. }
  365. }
  366. static const struct power_supply_desc ucs1002_charger_desc = {
  367. .name = "ucs1002",
  368. .type = POWER_SUPPLY_TYPE_USB,
  369. .usb_types = BIT(POWER_SUPPLY_USB_TYPE_SDP) |
  370. BIT(POWER_SUPPLY_USB_TYPE_CDP) |
  371. BIT(POWER_SUPPLY_USB_TYPE_DCP) |
  372. BIT(POWER_SUPPLY_USB_TYPE_PD) |
  373. BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN),
  374. .get_property = ucs1002_get_property,
  375. .set_property = ucs1002_set_property,
  376. .property_is_writeable = ucs1002_property_is_writeable,
  377. .properties = ucs1002_props,
  378. .num_properties = ARRAY_SIZE(ucs1002_props),
  379. };
  380. static void ucs1002_health_poll(struct work_struct *work)
  381. {
  382. struct ucs1002_info *info = container_of(work, struct ucs1002_info,
  383. health_poll.work);
  384. int ret;
  385. u32 reg;
  386. ret = regmap_read(info->regmap, UCS1002_REG_INTERRUPT_STATUS, &reg);
  387. if (ret)
  388. return;
  389. /* bad health and no status change, just schedule us again in a while */
  390. if ((reg & F_ERR) && info->health != POWER_SUPPLY_HEALTH_GOOD) {
  391. schedule_delayed_work(&info->health_poll,
  392. msecs_to_jiffies(2000));
  393. return;
  394. }
  395. if (reg & F_TSD)
  396. info->health = POWER_SUPPLY_HEALTH_OVERHEAT;
  397. else if (reg & (F_OVER_VOLT | F_BACK_VOLT))
  398. info->health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  399. else if (reg & F_OVER_ILIM)
  400. info->health = POWER_SUPPLY_HEALTH_OVERCURRENT;
  401. else if (reg & (F_DISCHARGE_ERR | F_MIN_KEEP_OUT))
  402. info->health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  403. else
  404. info->health = POWER_SUPPLY_HEALTH_GOOD;
  405. sysfs_notify(&info->charger->dev.kobj, NULL, "health");
  406. }
  407. static irqreturn_t ucs1002_charger_irq(int irq, void *data)
  408. {
  409. int ret, regval;
  410. bool present;
  411. struct ucs1002_info *info = data;
  412. present = info->present;
  413. ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, &regval);
  414. if (ret)
  415. return IRQ_HANDLED;
  416. /* update attached status */
  417. info->present = regval & F_ADET_PIN;
  418. /* notify the change */
  419. if (present != info->present)
  420. power_supply_changed(info->charger);
  421. return IRQ_HANDLED;
  422. }
  423. static irqreturn_t ucs1002_alert_irq(int irq, void *data)
  424. {
  425. struct ucs1002_info *info = data;
  426. mod_delayed_work(system_wq, &info->health_poll, 0);
  427. return IRQ_HANDLED;
  428. }
  429. static int ucs1002_regulator_enable(struct regulator_dev *rdev)
  430. {
  431. struct ucs1002_info *info = rdev_get_drvdata(rdev);
  432. /*
  433. * If the output is disabled due to 0 maximum current, just pretend the
  434. * enable did work. The regulator will be enabled as soon as we get a
  435. * a non-zero maximum current budget.
  436. */
  437. if (info->output_disable)
  438. return 0;
  439. return regulator_enable_regmap(rdev);
  440. }
  441. static const struct regulator_ops ucs1002_regulator_ops = {
  442. .is_enabled = regulator_is_enabled_regmap,
  443. .enable = ucs1002_regulator_enable,
  444. .disable = regulator_disable_regmap,
  445. };
  446. static const struct regulator_desc ucs1002_regulator_descriptor = {
  447. .name = "ucs1002-vbus",
  448. .ops = &ucs1002_regulator_ops,
  449. .type = REGULATOR_VOLTAGE,
  450. .owner = THIS_MODULE,
  451. .enable_reg = UCS1002_REG_SWITCH_CFG,
  452. .enable_mask = F_PWR_EN_SET,
  453. .enable_val = F_PWR_EN_SET,
  454. .fixed_uV = 5000000,
  455. .n_voltages = 1,
  456. };
  457. static int ucs1002_probe(struct i2c_client *client)
  458. {
  459. struct device *dev = &client->dev;
  460. struct power_supply_config charger_config = {};
  461. const struct regmap_config regmap_config = {
  462. .reg_bits = 8,
  463. .val_bits = 8,
  464. };
  465. struct regulator_config regulator_config = {};
  466. int irq_a_det, irq_alert, ret;
  467. struct ucs1002_info *info;
  468. unsigned int regval;
  469. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  470. if (!info)
  471. return -ENOMEM;
  472. info->regmap = devm_regmap_init_i2c(client, &regmap_config);
  473. ret = PTR_ERR_OR_ZERO(info->regmap);
  474. if (ret) {
  475. dev_err(dev, "Regmap initialization failed: %d\n", ret);
  476. return ret;
  477. }
  478. info->client = client;
  479. irq_a_det = of_irq_get_byname(dev->of_node, "a_det");
  480. irq_alert = of_irq_get_byname(dev->of_node, "alert");
  481. charger_config.of_node = dev->of_node;
  482. charger_config.drv_data = info;
  483. ret = regmap_read(info->regmap, UCS1002_REG_PRODUCT_ID, &regval);
  484. if (ret) {
  485. dev_err(dev, "Failed to read product ID: %d\n", ret);
  486. return ret;
  487. }
  488. if (regval != UCS1002_PRODUCT_ID) {
  489. dev_err(dev,
  490. "Product ID does not match (0x%02x != 0x%02x)\n",
  491. regval, UCS1002_PRODUCT_ID);
  492. return -ENODEV;
  493. }
  494. /* Enable charge rationing by default */
  495. ret = regmap_update_bits(info->regmap, UCS1002_REG_GENERAL_CFG,
  496. F_RATION_EN, F_RATION_EN);
  497. if (ret) {
  498. dev_err(dev, "Failed to read general config: %d\n", ret);
  499. return ret;
  500. }
  501. /*
  502. * Ignore the M1, M2, PWR_EN, and EM_EN pin states. Set active
  503. * mode selection to BC1.2 CDP.
  504. */
  505. ret = regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG,
  506. V_SET_ACTIVE_MODE_MASK | F_PIN_IGNORE,
  507. V_SET_ACTIVE_MODE_BC12_CDP | F_PIN_IGNORE);
  508. if (ret) {
  509. dev_err(dev, "Failed to configure default mode: %d\n", ret);
  510. return ret;
  511. }
  512. /*
  513. * Be safe and set initial current limit to 500mA
  514. */
  515. ret = ucs1002_set_max_current(info, 500000);
  516. if (ret) {
  517. dev_err(dev, "Failed to set max current default: %d\n", ret);
  518. return ret;
  519. }
  520. info->charger = devm_power_supply_register(dev, &ucs1002_charger_desc,
  521. &charger_config);
  522. ret = PTR_ERR_OR_ZERO(info->charger);
  523. if (ret) {
  524. dev_err(dev, "Failed to register power supply: %d\n", ret);
  525. return ret;
  526. }
  527. ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, &regval);
  528. if (ret) {
  529. dev_err(dev, "Failed to read pin status: %d\n", ret);
  530. return ret;
  531. }
  532. info->regulator_descriptor =
  533. devm_kmemdup(dev, &ucs1002_regulator_descriptor,
  534. sizeof(ucs1002_regulator_descriptor),
  535. GFP_KERNEL);
  536. if (!info->regulator_descriptor)
  537. return -ENOMEM;
  538. info->regulator_descriptor->enable_is_inverted = !(regval & F_SEL_PIN);
  539. regulator_config.dev = dev;
  540. regulator_config.of_node = dev->of_node;
  541. regulator_config.regmap = info->regmap;
  542. regulator_config.driver_data = info;
  543. info->rdev = devm_regulator_register(dev, info->regulator_descriptor,
  544. &regulator_config);
  545. ret = PTR_ERR_OR_ZERO(info->rdev);
  546. if (ret) {
  547. dev_err(dev, "Failed to register VBUS regulator: %d\n", ret);
  548. return ret;
  549. }
  550. info->health = POWER_SUPPLY_HEALTH_GOOD;
  551. INIT_DELAYED_WORK(&info->health_poll, ucs1002_health_poll);
  552. if (irq_a_det > 0) {
  553. ret = devm_request_threaded_irq(dev, irq_a_det, NULL,
  554. ucs1002_charger_irq,
  555. IRQF_ONESHOT,
  556. "ucs1002-a_det", info);
  557. if (ret) {
  558. dev_err(dev, "Failed to request A_DET threaded irq: %d\n",
  559. ret);
  560. return ret;
  561. }
  562. }
  563. if (irq_alert > 0) {
  564. ret = devm_request_irq(dev, irq_alert, ucs1002_alert_irq,
  565. 0,"ucs1002-alert", info);
  566. if (ret) {
  567. dev_err(dev, "Failed to request ALERT threaded irq: %d\n",
  568. ret);
  569. return ret;
  570. }
  571. }
  572. return 0;
  573. }
  574. static const struct of_device_id ucs1002_of_match[] = {
  575. { .compatible = "microchip,ucs1002", },
  576. { /* sentinel */ },
  577. };
  578. MODULE_DEVICE_TABLE(of, ucs1002_of_match);
  579. static struct i2c_driver ucs1002_driver = {
  580. .driver = {
  581. .name = "ucs1002",
  582. .of_match_table = ucs1002_of_match,
  583. },
  584. .probe = ucs1002_probe,
  585. };
  586. module_i2c_driver(ucs1002_driver);
  587. MODULE_DESCRIPTION("Microchip UCS1002 Programmable USB Port Power Controller");
  588. MODULE_AUTHOR("Enric Balletbo Serra <enric.balletbo@collabora.com>");
  589. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  590. MODULE_LICENSE("GPL");