ucs1002_power.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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 enum power_supply_usb_type ucs1002_usb_types[] = {
  256. POWER_SUPPLY_USB_TYPE_PD,
  257. POWER_SUPPLY_USB_TYPE_SDP,
  258. POWER_SUPPLY_USB_TYPE_DCP,
  259. POWER_SUPPLY_USB_TYPE_CDP,
  260. POWER_SUPPLY_USB_TYPE_UNKNOWN,
  261. };
  262. static int ucs1002_set_usb_type(struct ucs1002_info *info, int val)
  263. {
  264. unsigned int mode;
  265. if (val < 0 || val >= ARRAY_SIZE(ucs1002_usb_types))
  266. return -EINVAL;
  267. switch (ucs1002_usb_types[val]) {
  268. case POWER_SUPPLY_USB_TYPE_PD:
  269. mode = V_SET_ACTIVE_MODE_DEDICATED;
  270. break;
  271. case POWER_SUPPLY_USB_TYPE_SDP:
  272. mode = V_SET_ACTIVE_MODE_BC12_SDP;
  273. break;
  274. case POWER_SUPPLY_USB_TYPE_DCP:
  275. mode = V_SET_ACTIVE_MODE_BC12_DCP;
  276. break;
  277. case POWER_SUPPLY_USB_TYPE_CDP:
  278. mode = V_SET_ACTIVE_MODE_BC12_CDP;
  279. break;
  280. default:
  281. return -EINVAL;
  282. }
  283. return regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG,
  284. V_SET_ACTIVE_MODE_MASK, mode);
  285. }
  286. static int ucs1002_get_usb_type(struct ucs1002_info *info,
  287. union power_supply_propval *val)
  288. {
  289. enum power_supply_usb_type type;
  290. unsigned int reg;
  291. int ret;
  292. ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, &reg);
  293. if (ret)
  294. return ret;
  295. switch (reg & F_ACTIVE_MODE_MASK) {
  296. default:
  297. type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
  298. break;
  299. case F_ACTIVE_MODE_DEDICATED:
  300. type = POWER_SUPPLY_USB_TYPE_PD;
  301. break;
  302. case F_ACTIVE_MODE_BC12_SDP:
  303. type = POWER_SUPPLY_USB_TYPE_SDP;
  304. break;
  305. case F_ACTIVE_MODE_BC12_DCP:
  306. type = POWER_SUPPLY_USB_TYPE_DCP;
  307. break;
  308. case F_ACTIVE_MODE_BC12_CDP:
  309. type = POWER_SUPPLY_USB_TYPE_CDP;
  310. break;
  311. }
  312. val->intval = type;
  313. return 0;
  314. }
  315. static int ucs1002_get_property(struct power_supply *psy,
  316. enum power_supply_property psp,
  317. union power_supply_propval *val)
  318. {
  319. struct ucs1002_info *info = power_supply_get_drvdata(psy);
  320. switch (psp) {
  321. case POWER_SUPPLY_PROP_ONLINE:
  322. return ucs1002_get_online(info, val);
  323. case POWER_SUPPLY_PROP_CHARGE_NOW:
  324. return ucs1002_get_charge(info, val);
  325. case POWER_SUPPLY_PROP_CURRENT_NOW:
  326. return ucs1002_get_current(info, val);
  327. case POWER_SUPPLY_PROP_CURRENT_MAX:
  328. return ucs1002_get_max_current(info, val);
  329. case POWER_SUPPLY_PROP_USB_TYPE:
  330. return ucs1002_get_usb_type(info, val);
  331. case POWER_SUPPLY_PROP_HEALTH:
  332. return val->intval = info->health;
  333. case POWER_SUPPLY_PROP_PRESENT:
  334. val->intval = info->present;
  335. return 0;
  336. case POWER_SUPPLY_PROP_MANUFACTURER:
  337. val->strval = UCS1002_MANUFACTURER;
  338. return 0;
  339. default:
  340. return -EINVAL;
  341. }
  342. }
  343. static int ucs1002_set_property(struct power_supply *psy,
  344. enum power_supply_property psp,
  345. const union power_supply_propval *val)
  346. {
  347. struct ucs1002_info *info = power_supply_get_drvdata(psy);
  348. switch (psp) {
  349. case POWER_SUPPLY_PROP_CURRENT_MAX:
  350. return ucs1002_set_max_current(info, val->intval);
  351. case POWER_SUPPLY_PROP_USB_TYPE:
  352. return ucs1002_set_usb_type(info, val->intval);
  353. default:
  354. return -EINVAL;
  355. }
  356. }
  357. static int ucs1002_property_is_writeable(struct power_supply *psy,
  358. enum power_supply_property psp)
  359. {
  360. switch (psp) {
  361. case POWER_SUPPLY_PROP_CURRENT_MAX:
  362. case POWER_SUPPLY_PROP_USB_TYPE:
  363. return true;
  364. default:
  365. return false;
  366. }
  367. }
  368. static const struct power_supply_desc ucs1002_charger_desc = {
  369. .name = "ucs1002",
  370. .type = POWER_SUPPLY_TYPE_USB,
  371. .usb_types = ucs1002_usb_types,
  372. .num_usb_types = ARRAY_SIZE(ucs1002_usb_types),
  373. .get_property = ucs1002_get_property,
  374. .set_property = ucs1002_set_property,
  375. .property_is_writeable = ucs1002_property_is_writeable,
  376. .properties = ucs1002_props,
  377. .num_properties = ARRAY_SIZE(ucs1002_props),
  378. };
  379. static void ucs1002_health_poll(struct work_struct *work)
  380. {
  381. struct ucs1002_info *info = container_of(work, struct ucs1002_info,
  382. health_poll.work);
  383. int ret;
  384. u32 reg;
  385. ret = regmap_read(info->regmap, UCS1002_REG_INTERRUPT_STATUS, &reg);
  386. if (ret)
  387. return;
  388. /* bad health and no status change, just schedule us again in a while */
  389. if ((reg & F_ERR) && info->health != POWER_SUPPLY_HEALTH_GOOD) {
  390. schedule_delayed_work(&info->health_poll,
  391. msecs_to_jiffies(2000));
  392. return;
  393. }
  394. if (reg & F_TSD)
  395. info->health = POWER_SUPPLY_HEALTH_OVERHEAT;
  396. else if (reg & (F_OVER_VOLT | F_BACK_VOLT))
  397. info->health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  398. else if (reg & F_OVER_ILIM)
  399. info->health = POWER_SUPPLY_HEALTH_OVERCURRENT;
  400. else if (reg & (F_DISCHARGE_ERR | F_MIN_KEEP_OUT))
  401. info->health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  402. else
  403. info->health = POWER_SUPPLY_HEALTH_GOOD;
  404. sysfs_notify(&info->charger->dev.kobj, NULL, "health");
  405. }
  406. static irqreturn_t ucs1002_charger_irq(int irq, void *data)
  407. {
  408. int ret, regval;
  409. bool present;
  410. struct ucs1002_info *info = data;
  411. present = info->present;
  412. ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, &regval);
  413. if (ret)
  414. return IRQ_HANDLED;
  415. /* update attached status */
  416. info->present = regval & F_ADET_PIN;
  417. /* notify the change */
  418. if (present != info->present)
  419. power_supply_changed(info->charger);
  420. return IRQ_HANDLED;
  421. }
  422. static irqreturn_t ucs1002_alert_irq(int irq, void *data)
  423. {
  424. struct ucs1002_info *info = data;
  425. mod_delayed_work(system_wq, &info->health_poll, 0);
  426. return IRQ_HANDLED;
  427. }
  428. static int ucs1002_regulator_enable(struct regulator_dev *rdev)
  429. {
  430. struct ucs1002_info *info = rdev_get_drvdata(rdev);
  431. /*
  432. * If the output is disabled due to 0 maximum current, just pretend the
  433. * enable did work. The regulator will be enabled as soon as we get a
  434. * a non-zero maximum current budget.
  435. */
  436. if (info->output_disable)
  437. return 0;
  438. return regulator_enable_regmap(rdev);
  439. }
  440. static const struct regulator_ops ucs1002_regulator_ops = {
  441. .is_enabled = regulator_is_enabled_regmap,
  442. .enable = ucs1002_regulator_enable,
  443. .disable = regulator_disable_regmap,
  444. };
  445. static const struct regulator_desc ucs1002_regulator_descriptor = {
  446. .name = "ucs1002-vbus",
  447. .ops = &ucs1002_regulator_ops,
  448. .type = REGULATOR_VOLTAGE,
  449. .owner = THIS_MODULE,
  450. .enable_reg = UCS1002_REG_SWITCH_CFG,
  451. .enable_mask = F_PWR_EN_SET,
  452. .enable_val = F_PWR_EN_SET,
  453. .fixed_uV = 5000000,
  454. .n_voltages = 1,
  455. };
  456. static int ucs1002_probe(struct i2c_client *client,
  457. const struct i2c_device_id *dev_id)
  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");