extcon-lc824206xa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ON Semiconductor LC824206XA Micro USB Switch driver
  4. *
  5. * Copyright (c) 2024 Hans de Goede <hansg@kernel.org>
  6. *
  7. * ON Semiconductor has an "Advance Information" datasheet available
  8. * (ENA2222-D.PDF), but no full datasheet. So there is no documentation
  9. * available for the registers.
  10. *
  11. * This driver is based on the register info from the extcon-fsa9285.c driver,
  12. * from the Lollipop Android sources for the Lenovo Yoga Tablet 2 (Pro)
  13. * 830 / 1050 / 1380 models. Note despite the name this is actually a driver
  14. * for the LC824206XA not the FSA9285. The Android sources can be downloaded
  15. * from Lenovo's support page for these tablets, filename:
  16. * yoga_tab_2_osc_android_to_lollipop_201505.rar.
  17. */
  18. #include <linux/bits.h>
  19. #include <linux/delay.h>
  20. #include <linux/device.h>
  21. #include <linux/extcon-provider.h>
  22. #include <linux/i2c.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.h>
  25. #include <linux/power_supply.h>
  26. #include <linux/property.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/workqueue.h>
  29. /*
  30. * Register defines as mentioned above there is no datasheet with register
  31. * info, so this may not be 100% accurate.
  32. */
  33. #define REG00 0x00
  34. #define REG00_INIT_VALUE 0x01
  35. #define REG_STATUS 0x01
  36. #define STATUS_OVP BIT(0)
  37. #define STATUS_DATA_SHORT BIT(1)
  38. #define STATUS_VBUS_PRESENT BIT(2)
  39. #define STATUS_USB_ID GENMASK(7, 3)
  40. #define STATUS_USB_ID_GND 0x80
  41. #define STATUS_USB_ID_ACA 0xf0
  42. #define STATUS_USB_ID_FLOAT 0xf8
  43. /*
  44. * This controls the DP/DM muxes + other switches,
  45. * meaning of individual bits is unknown.
  46. */
  47. #define REG_SWITCH_CONTROL 0x02
  48. #define SWITCH_STEREO_MIC 0xc8
  49. #define SWITCH_USB_HOST 0xec
  50. #define SWITCH_DISCONNECTED 0xf8
  51. #define SWITCH_USB_DEVICE 0xfc
  52. /* 5 bits? ADC 0x10 GND, 0x1a-0x1f ACA, 0x1f float */
  53. #define REG_ID_PIN_ADC_VALUE 0x03
  54. /* Masks for all 3 interrupt registers */
  55. #define INTR_ID_PIN_CHANGE BIT(0)
  56. #define INTR_VBUS_CHANGE BIT(1)
  57. /* Both of these get set after a continuous mode ADC conversion */
  58. #define INTR_ID_PIN_ADC_INT1 BIT(2)
  59. #define INTR_ID_PIN_ADC_INT2 BIT(3)
  60. /* Charger type available in reg 0x09 */
  61. #define INTR_CHARGER_DET_DONE BIT(4)
  62. #define INTR_OVP BIT(5)
  63. /* There are 7 interrupt sources, bit 6 use is unknown (OCP?) */
  64. #define INTR_ALL GENMASK(6, 0)
  65. /* Unmask interrupts this driver cares about */
  66. #define INTR_MASK \
  67. (INTR_ALL & ~(INTR_ID_PIN_CHANGE | INTR_VBUS_CHANGE | INTR_CHARGER_DET_DONE))
  68. /* Active (event happened and not cleared yet) interrupts */
  69. #define REG_INTR_STATUS 0x04
  70. /*
  71. * Writing a 1 to a bit here clears it in INTR_STATUS. These bits do NOT
  72. * auto-reset to 0, so these must be set to 0 manually after clearing.
  73. */
  74. #define REG_INTR_CLEAR 0x05
  75. /* Interrupts which bit is set to 1 here will not raise the HW IRQ */
  76. #define REG_INTR_MASK 0x06
  77. /* ID pin ADC control, meaning of individual bits is unknown */
  78. #define REG_ID_PIN_ADC_CTRL 0x07
  79. #define ID_PIN_ADC_AUTO 0x40
  80. #define ID_PIN_ADC_CONTINUOUS 0x44
  81. #define REG_CHARGER_DET 0x08
  82. #define CHARGER_DET_ON BIT(0)
  83. #define CHARGER_DET_CDP_ON BIT(1)
  84. #define CHARGER_DET_CDP_VAL BIT(2)
  85. #define REG_CHARGER_TYPE 0x09
  86. #define CHARGER_TYPE_UNKNOWN 0x00
  87. #define CHARGER_TYPE_DCP 0x01
  88. #define CHARGER_TYPE_SDP_OR_CDP 0x04
  89. #define CHARGER_TYPE_QC 0x06
  90. #define REG10 0x10
  91. #define REG10_INIT_VALUE 0x00
  92. struct lc824206xa_data {
  93. struct work_struct work;
  94. struct i2c_client *client;
  95. struct extcon_dev *edev;
  96. struct power_supply *psy;
  97. struct regulator *vbus_boost;
  98. unsigned int usb_type;
  99. unsigned int cable;
  100. unsigned int previous_cable;
  101. u8 switch_control;
  102. u8 previous_switch_control;
  103. bool vbus_ok;
  104. bool vbus_boost_enabled;
  105. bool fastcharge_over_miclr;
  106. };
  107. static const unsigned int lc824206xa_cables[] = {
  108. EXTCON_USB_HOST,
  109. EXTCON_CHG_USB_SDP,
  110. EXTCON_CHG_USB_CDP,
  111. EXTCON_CHG_USB_DCP,
  112. EXTCON_CHG_USB_ACA,
  113. EXTCON_CHG_USB_FAST,
  114. EXTCON_NONE,
  115. };
  116. /* read/write reg helpers to add error logging to smbus byte functions */
  117. static int lc824206xa_read_reg(struct lc824206xa_data *data, u8 reg)
  118. {
  119. int ret;
  120. ret = i2c_smbus_read_byte_data(data->client, reg);
  121. if (ret < 0)
  122. dev_err(&data->client->dev, "Error %d reading reg 0x%02x\n", ret, reg);
  123. return ret;
  124. }
  125. static int lc824206xa_write_reg(struct lc824206xa_data *data, u8 reg, u8 val)
  126. {
  127. int ret;
  128. ret = i2c_smbus_write_byte_data(data->client, reg, val);
  129. if (ret < 0)
  130. dev_err(&data->client->dev, "Error %d writing reg 0x%02x\n", ret, reg);
  131. return ret;
  132. }
  133. static int lc824206xa_get_id(struct lc824206xa_data *data)
  134. {
  135. int ret;
  136. ret = lc824206xa_write_reg(data, REG_ID_PIN_ADC_CTRL, ID_PIN_ADC_CONTINUOUS);
  137. if (ret)
  138. return ret;
  139. ret = lc824206xa_read_reg(data, REG_ID_PIN_ADC_VALUE);
  140. lc824206xa_write_reg(data, REG_ID_PIN_ADC_CTRL, ID_PIN_ADC_AUTO);
  141. return ret;
  142. }
  143. static void lc824206xa_set_vbus_boost(struct lc824206xa_data *data, bool enable)
  144. {
  145. int ret;
  146. if (data->vbus_boost_enabled == enable)
  147. return;
  148. if (enable)
  149. ret = regulator_enable(data->vbus_boost);
  150. else
  151. ret = regulator_disable(data->vbus_boost);
  152. if (ret == 0)
  153. data->vbus_boost_enabled = enable;
  154. else
  155. dev_err(&data->client->dev, "Error updating Vbus boost regulator: %d\n", ret);
  156. }
  157. static void lc824206xa_charger_detect(struct lc824206xa_data *data)
  158. {
  159. int charger_type, ret;
  160. charger_type = lc824206xa_read_reg(data, REG_CHARGER_TYPE);
  161. if (charger_type < 0)
  162. return;
  163. dev_dbg(&data->client->dev, "charger type 0x%02x\n", charger_type);
  164. switch (charger_type) {
  165. case CHARGER_TYPE_UNKNOWN:
  166. data->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
  167. /* Treat as SDP */
  168. data->cable = EXTCON_CHG_USB_SDP;
  169. data->switch_control = SWITCH_USB_DEVICE;
  170. break;
  171. case CHARGER_TYPE_SDP_OR_CDP:
  172. data->usb_type = POWER_SUPPLY_USB_TYPE_SDP;
  173. data->cable = EXTCON_CHG_USB_SDP;
  174. data->switch_control = SWITCH_USB_DEVICE;
  175. ret = lc824206xa_write_reg(data, REG_CHARGER_DET,
  176. CHARGER_DET_CDP_ON | CHARGER_DET_ON);
  177. if (ret < 0)
  178. break;
  179. msleep(100);
  180. ret = lc824206xa_read_reg(data, REG_CHARGER_DET);
  181. if (ret >= 0 && (ret & CHARGER_DET_CDP_VAL)) {
  182. data->usb_type = POWER_SUPPLY_USB_TYPE_CDP;
  183. data->cable = EXTCON_CHG_USB_CDP;
  184. }
  185. lc824206xa_write_reg(data, REG_CHARGER_DET, CHARGER_DET_ON);
  186. break;
  187. case CHARGER_TYPE_DCP:
  188. data->usb_type = POWER_SUPPLY_USB_TYPE_DCP;
  189. data->cable = EXTCON_CHG_USB_DCP;
  190. if (data->fastcharge_over_miclr)
  191. data->switch_control = SWITCH_STEREO_MIC;
  192. else
  193. data->switch_control = SWITCH_DISCONNECTED;
  194. break;
  195. case CHARGER_TYPE_QC:
  196. data->usb_type = POWER_SUPPLY_USB_TYPE_DCP;
  197. data->cable = EXTCON_CHG_USB_DCP;
  198. data->switch_control = SWITCH_DISCONNECTED;
  199. break;
  200. default:
  201. dev_warn(&data->client->dev, "Unknown charger type: 0x%02x\n", charger_type);
  202. break;
  203. }
  204. }
  205. static void lc824206xa_work(struct work_struct *work)
  206. {
  207. struct lc824206xa_data *data = container_of(work, struct lc824206xa_data, work);
  208. bool vbus_boost_enable = false;
  209. int status, id;
  210. status = lc824206xa_read_reg(data, REG_STATUS);
  211. if (status < 0)
  212. return;
  213. dev_dbg(&data->client->dev, "status 0x%02x\n", status);
  214. data->vbus_ok = (status & (STATUS_VBUS_PRESENT | STATUS_OVP)) == STATUS_VBUS_PRESENT;
  215. /* Read id pin ADC if necessary */
  216. switch (status & STATUS_USB_ID) {
  217. case STATUS_USB_ID_GND:
  218. case STATUS_USB_ID_FLOAT:
  219. break;
  220. default:
  221. /* Happens when the connector is inserted slowly, log at dbg level */
  222. dev_dbg(&data->client->dev, "Unknown status 0x%02x\n", status);
  223. fallthrough;
  224. case STATUS_USB_ID_ACA:
  225. id = lc824206xa_get_id(data);
  226. dev_dbg(&data->client->dev, "RID 0x%02x\n", id);
  227. switch (id) {
  228. case 0x10:
  229. status = STATUS_USB_ID_GND;
  230. break;
  231. case 0x18 ... 0x1e:
  232. status = STATUS_USB_ID_ACA;
  233. break;
  234. case 0x1f:
  235. status = STATUS_USB_ID_FLOAT;
  236. break;
  237. default:
  238. dev_warn(&data->client->dev, "Unknown RID 0x%02x\n", id);
  239. return;
  240. }
  241. }
  242. /* Check for out of spec OTG charging hubs, treat as ACA */
  243. if ((status & STATUS_USB_ID) == STATUS_USB_ID_GND &&
  244. data->vbus_ok && !data->vbus_boost_enabled) {
  245. dev_info(&data->client->dev, "Out of spec USB host adapter with Vbus present, not enabling 5V output\n");
  246. status = STATUS_USB_ID_ACA;
  247. }
  248. switch (status & STATUS_USB_ID) {
  249. case STATUS_USB_ID_ACA:
  250. data->usb_type = POWER_SUPPLY_USB_TYPE_ACA;
  251. data->cable = EXTCON_CHG_USB_ACA;
  252. data->switch_control = SWITCH_USB_HOST;
  253. break;
  254. case STATUS_USB_ID_GND:
  255. data->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
  256. data->cable = EXTCON_USB_HOST;
  257. data->switch_control = SWITCH_USB_HOST;
  258. vbus_boost_enable = true;
  259. break;
  260. case STATUS_USB_ID_FLOAT:
  261. /* When fast charging with Vbus > 5V, OVP will be set */
  262. if (data->fastcharge_over_miclr &&
  263. data->switch_control == SWITCH_STEREO_MIC &&
  264. (status & STATUS_OVP)) {
  265. data->cable = EXTCON_CHG_USB_FAST;
  266. break;
  267. }
  268. if (data->vbus_ok) {
  269. lc824206xa_charger_detect(data);
  270. } else {
  271. data->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
  272. data->cable = EXTCON_NONE;
  273. data->switch_control = SWITCH_DISCONNECTED;
  274. }
  275. break;
  276. }
  277. lc824206xa_set_vbus_boost(data, vbus_boost_enable);
  278. if (data->switch_control != data->previous_switch_control) {
  279. lc824206xa_write_reg(data, REG_SWITCH_CONTROL, data->switch_control);
  280. data->previous_switch_control = data->switch_control;
  281. }
  282. if (data->cable != data->previous_cable) {
  283. extcon_set_state_sync(data->edev, data->previous_cable, false);
  284. extcon_set_state_sync(data->edev, data->cable, true);
  285. data->previous_cable = data->cable;
  286. }
  287. power_supply_changed(data->psy);
  288. }
  289. static irqreturn_t lc824206xa_irq(int irq, void *_data)
  290. {
  291. struct lc824206xa_data *data = _data;
  292. int intr_status;
  293. intr_status = lc824206xa_read_reg(data, REG_INTR_STATUS);
  294. if (intr_status < 0)
  295. intr_status = INTR_ALL; /* Should never happen, clear all */
  296. dev_dbg(&data->client->dev, "interrupt 0x%02x\n", intr_status);
  297. lc824206xa_write_reg(data, REG_INTR_CLEAR, intr_status);
  298. lc824206xa_write_reg(data, REG_INTR_CLEAR, 0);
  299. schedule_work(&data->work);
  300. return IRQ_HANDLED;
  301. }
  302. /*
  303. * Newer charger (power_supply) drivers expect the max input current to be
  304. * provided by a parent power_supply device for the charger chip.
  305. */
  306. static int lc824206xa_psy_get_prop(struct power_supply *psy,
  307. enum power_supply_property psp,
  308. union power_supply_propval *val)
  309. {
  310. struct lc824206xa_data *data = power_supply_get_drvdata(psy);
  311. switch (psp) {
  312. case POWER_SUPPLY_PROP_ONLINE:
  313. val->intval = data->vbus_ok && !data->vbus_boost_enabled;
  314. break;
  315. case POWER_SUPPLY_PROP_USB_TYPE:
  316. val->intval = data->usb_type;
  317. break;
  318. case POWER_SUPPLY_PROP_CURRENT_MAX:
  319. switch (data->usb_type) {
  320. case POWER_SUPPLY_USB_TYPE_DCP:
  321. case POWER_SUPPLY_USB_TYPE_ACA:
  322. val->intval = 2000000;
  323. break;
  324. case POWER_SUPPLY_USB_TYPE_CDP:
  325. val->intval = 1500000;
  326. break;
  327. default:
  328. val->intval = 500000;
  329. }
  330. break;
  331. default:
  332. return -EINVAL;
  333. }
  334. return 0;
  335. }
  336. static const enum power_supply_property lc824206xa_psy_props[] = {
  337. POWER_SUPPLY_PROP_ONLINE,
  338. POWER_SUPPLY_PROP_USB_TYPE,
  339. POWER_SUPPLY_PROP_CURRENT_MAX,
  340. };
  341. static const struct power_supply_desc lc824206xa_psy_desc = {
  342. .name = "lc824206xa-charger-detect",
  343. .type = POWER_SUPPLY_TYPE_USB,
  344. .usb_types = BIT(POWER_SUPPLY_USB_TYPE_SDP) |
  345. BIT(POWER_SUPPLY_USB_TYPE_CDP) |
  346. BIT(POWER_SUPPLY_USB_TYPE_DCP) |
  347. BIT(POWER_SUPPLY_USB_TYPE_ACA) |
  348. BIT(POWER_SUPPLY_USB_TYPE_UNKNOWN),
  349. .properties = lc824206xa_psy_props,
  350. .num_properties = ARRAY_SIZE(lc824206xa_psy_props),
  351. .get_property = lc824206xa_psy_get_prop,
  352. };
  353. static int lc824206xa_probe(struct i2c_client *client)
  354. {
  355. struct power_supply_config psy_cfg = { };
  356. struct device *dev = &client->dev;
  357. struct lc824206xa_data *data;
  358. int ret;
  359. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  360. if (!data)
  361. return -ENOMEM;
  362. data->client = client;
  363. INIT_WORK(&data->work, lc824206xa_work);
  364. data->cable = EXTCON_NONE;
  365. data->previous_cable = EXTCON_NONE;
  366. data->usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN;
  367. /* Some designs use a custom fast-charge protocol over the mic L/R inputs */
  368. data->fastcharge_over_miclr =
  369. device_property_read_bool(dev, "onnn,enable-miclr-for-dcp");
  370. data->vbus_boost = devm_regulator_get(dev, "vbus");
  371. if (IS_ERR(data->vbus_boost))
  372. return dev_err_probe(dev, PTR_ERR(data->vbus_boost),
  373. "getting regulator\n");
  374. /* Init */
  375. ret = lc824206xa_write_reg(data, REG00, REG00_INIT_VALUE);
  376. ret |= lc824206xa_write_reg(data, REG10, REG10_INIT_VALUE);
  377. msleep(100);
  378. ret |= lc824206xa_write_reg(data, REG_INTR_CLEAR, INTR_ALL);
  379. ret |= lc824206xa_write_reg(data, REG_INTR_CLEAR, 0);
  380. ret |= lc824206xa_write_reg(data, REG_INTR_MASK, INTR_MASK);
  381. ret |= lc824206xa_write_reg(data, REG_ID_PIN_ADC_CTRL, ID_PIN_ADC_AUTO);
  382. ret |= lc824206xa_write_reg(data, REG_CHARGER_DET, CHARGER_DET_ON);
  383. if (ret)
  384. return -EIO;
  385. /* Initialize extcon device */
  386. data->edev = devm_extcon_dev_allocate(dev, lc824206xa_cables);
  387. if (IS_ERR(data->edev))
  388. return PTR_ERR(data->edev);
  389. ret = devm_extcon_dev_register(dev, data->edev);
  390. if (ret)
  391. return dev_err_probe(dev, ret, "registering extcon device\n");
  392. psy_cfg.drv_data = data;
  393. data->psy = devm_power_supply_register(dev, &lc824206xa_psy_desc, &psy_cfg);
  394. if (IS_ERR(data->psy))
  395. return dev_err_probe(dev, PTR_ERR(data->psy), "registering power supply\n");
  396. ret = devm_request_threaded_irq(dev, client->irq, NULL, lc824206xa_irq,
  397. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  398. KBUILD_MODNAME, data);
  399. if (ret)
  400. return dev_err_probe(dev, ret, "requesting IRQ\n");
  401. /* Sync initial state */
  402. schedule_work(&data->work);
  403. return 0;
  404. }
  405. static const struct i2c_device_id lc824206xa_i2c_ids[] = {
  406. { "lc824206xa" },
  407. { }
  408. };
  409. MODULE_DEVICE_TABLE(i2c, lc824206xa_i2c_ids);
  410. static struct i2c_driver lc824206xa_driver = {
  411. .driver = {
  412. .name = KBUILD_MODNAME,
  413. },
  414. .probe = lc824206xa_probe,
  415. .id_table = lc824206xa_i2c_ids,
  416. };
  417. module_i2c_driver(lc824206xa_driver);
  418. MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>");
  419. MODULE_DESCRIPTION("LC824206XA Micro USB Switch driver");
  420. MODULE_LICENSE("GPL");