tps6598x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for TI TPS6598x USB Power Delivery controller family
  4. *
  5. * Copyright (C) 2017, Intel Corporation
  6. * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  7. */
  8. #include <linux/i2c.h>
  9. #include <linux/acpi.h>
  10. #include <linux/module.h>
  11. #include <linux/regmap.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/usb/typec.h>
  14. /* Register offsets */
  15. #define TPS_REG_CMD1 0x08
  16. #define TPS_REG_DATA1 0x09
  17. #define TPS_REG_INT_EVENT1 0x14
  18. #define TPS_REG_INT_EVENT2 0x15
  19. #define TPS_REG_INT_MASK1 0x16
  20. #define TPS_REG_INT_MASK2 0x17
  21. #define TPS_REG_INT_CLEAR1 0x18
  22. #define TPS_REG_INT_CLEAR2 0x19
  23. #define TPS_REG_STATUS 0x1a
  24. #define TPS_REG_SYSTEM_CONF 0x28
  25. #define TPS_REG_CTRL_CONF 0x29
  26. #define TPS_REG_POWER_STATUS 0x3f
  27. #define TPS_REG_RX_IDENTITY_SOP 0x48
  28. /* TPS_REG_INT_* bits */
  29. #define TPS_REG_INT_PLUG_EVENT BIT(3)
  30. /* TPS_REG_STATUS bits */
  31. #define TPS_STATUS_PLUG_PRESENT BIT(0)
  32. #define TPS_STATUS_ORIENTATION BIT(4)
  33. #define TPS_STATUS_PORTROLE(s) (!!((s) & BIT(5)))
  34. #define TPS_STATUS_DATAROLE(s) (!!((s) & BIT(6)))
  35. #define TPS_STATUS_VCONN(s) (!!((s) & BIT(7)))
  36. /* TPS_REG_SYSTEM_CONF bits */
  37. #define TPS_SYSCONF_PORTINFO(c) ((c) & 7)
  38. enum {
  39. TPS_PORTINFO_SINK,
  40. TPS_PORTINFO_SINK_ACCESSORY,
  41. TPS_PORTINFO_DRP_UFP,
  42. TPS_PORTINFO_DRP_UFP_DRD,
  43. TPS_PORTINFO_DRP_DFP,
  44. TPS_PORTINFO_DRP_DFP_DRD,
  45. TPS_PORTINFO_SOURCE,
  46. };
  47. /* TPS_REG_POWER_STATUS bits */
  48. #define TPS_POWER_STATUS_SOURCESINK BIT(1)
  49. #define TPS_POWER_STATUS_PWROPMODE(p) (((p) & GENMASK(3, 2)) >> 2)
  50. /* TPS_REG_RX_IDENTITY_SOP */
  51. struct tps6598x_rx_identity_reg {
  52. u8 status;
  53. struct usb_pd_identity identity;
  54. u32 vdo[3];
  55. } __packed;
  56. /* Standard Task return codes */
  57. #define TPS_TASK_TIMEOUT 1
  58. #define TPS_TASK_REJECTED 3
  59. /* Unrecognized commands will be replaced with "!CMD" */
  60. #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321)
  61. struct tps6598x {
  62. struct device *dev;
  63. struct regmap *regmap;
  64. struct mutex lock; /* device lock */
  65. u8 i2c_protocol:1;
  66. struct typec_port *port;
  67. struct typec_partner *partner;
  68. struct usb_pd_identity partner_identity;
  69. struct typec_capability typec_cap;
  70. };
  71. /*
  72. * Max data bytes for Data1, Data2, and other registers. See ch 1.3.2:
  73. * http://www.ti.com/lit/ug/slvuan1a/slvuan1a.pdf
  74. */
  75. #define TPS_MAX_LEN 64
  76. static int
  77. tps6598x_block_read(struct tps6598x *tps, u8 reg, void *val, size_t len)
  78. {
  79. u8 data[TPS_MAX_LEN + 1];
  80. int ret;
  81. if (WARN_ON(len + 1 > sizeof(data)))
  82. return -EINVAL;
  83. if (!tps->i2c_protocol)
  84. return regmap_raw_read(tps->regmap, reg, val, len);
  85. ret = regmap_raw_read(tps->regmap, reg, data, sizeof(data));
  86. if (ret)
  87. return ret;
  88. if (data[0] < len)
  89. return -EIO;
  90. memcpy(val, &data[1], len);
  91. return 0;
  92. }
  93. static int tps6598x_block_write(struct tps6598x *tps, u8 reg,
  94. const void *val, size_t len)
  95. {
  96. u8 data[TPS_MAX_LEN + 1];
  97. if (!tps->i2c_protocol)
  98. return regmap_raw_write(tps->regmap, reg, val, len);
  99. data[0] = len;
  100. memcpy(&data[1], val, len);
  101. return regmap_raw_write(tps->regmap, reg, data, sizeof(data));
  102. }
  103. static inline int tps6598x_read16(struct tps6598x *tps, u8 reg, u16 *val)
  104. {
  105. return tps6598x_block_read(tps, reg, val, sizeof(u16));
  106. }
  107. static inline int tps6598x_read32(struct tps6598x *tps, u8 reg, u32 *val)
  108. {
  109. return tps6598x_block_read(tps, reg, val, sizeof(u32));
  110. }
  111. static inline int tps6598x_read64(struct tps6598x *tps, u8 reg, u64 *val)
  112. {
  113. return tps6598x_block_read(tps, reg, val, sizeof(u64));
  114. }
  115. static inline int tps6598x_write16(struct tps6598x *tps, u8 reg, u16 val)
  116. {
  117. return tps6598x_block_write(tps, reg, &val, sizeof(u16));
  118. }
  119. static inline int tps6598x_write32(struct tps6598x *tps, u8 reg, u32 val)
  120. {
  121. return tps6598x_block_write(tps, reg, &val, sizeof(u32));
  122. }
  123. static inline int tps6598x_write64(struct tps6598x *tps, u8 reg, u64 val)
  124. {
  125. return tps6598x_block_write(tps, reg, &val, sizeof(u64));
  126. }
  127. static inline int
  128. tps6598x_write_4cc(struct tps6598x *tps, u8 reg, const char *val)
  129. {
  130. return tps6598x_block_write(tps, reg, val, 4);
  131. }
  132. static int tps6598x_read_partner_identity(struct tps6598x *tps)
  133. {
  134. struct tps6598x_rx_identity_reg id;
  135. int ret;
  136. ret = tps6598x_block_read(tps, TPS_REG_RX_IDENTITY_SOP,
  137. &id, sizeof(id));
  138. if (ret)
  139. return ret;
  140. tps->partner_identity = id.identity;
  141. return 0;
  142. }
  143. static int tps6598x_connect(struct tps6598x *tps, u32 status)
  144. {
  145. struct typec_partner_desc desc;
  146. enum typec_pwr_opmode mode;
  147. u16 pwr_status;
  148. int ret;
  149. if (tps->partner)
  150. return 0;
  151. ret = tps6598x_read16(tps, TPS_REG_POWER_STATUS, &pwr_status);
  152. if (ret < 0)
  153. return ret;
  154. mode = TPS_POWER_STATUS_PWROPMODE(pwr_status);
  155. desc.usb_pd = mode == TYPEC_PWR_MODE_PD;
  156. desc.accessory = TYPEC_ACCESSORY_NONE; /* XXX: handle accessories */
  157. desc.identity = NULL;
  158. if (desc.usb_pd) {
  159. ret = tps6598x_read_partner_identity(tps);
  160. if (ret)
  161. return ret;
  162. desc.identity = &tps->partner_identity;
  163. }
  164. typec_set_pwr_opmode(tps->port, mode);
  165. typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
  166. typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
  167. typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
  168. tps->partner = typec_register_partner(tps->port, &desc);
  169. if (IS_ERR(tps->partner))
  170. return PTR_ERR(tps->partner);
  171. if (desc.identity)
  172. typec_partner_set_identity(tps->partner);
  173. return 0;
  174. }
  175. static void tps6598x_disconnect(struct tps6598x *tps, u32 status)
  176. {
  177. if (!IS_ERR(tps->partner))
  178. typec_unregister_partner(tps->partner);
  179. tps->partner = NULL;
  180. typec_set_pwr_opmode(tps->port, TYPEC_PWR_MODE_USB);
  181. typec_set_pwr_role(tps->port, TPS_STATUS_PORTROLE(status));
  182. typec_set_vconn_role(tps->port, TPS_STATUS_VCONN(status));
  183. typec_set_data_role(tps->port, TPS_STATUS_DATAROLE(status));
  184. }
  185. static int tps6598x_exec_cmd(struct tps6598x *tps, const char *cmd,
  186. size_t in_len, u8 *in_data,
  187. size_t out_len, u8 *out_data)
  188. {
  189. unsigned long timeout;
  190. u32 val;
  191. int ret;
  192. ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
  193. if (ret)
  194. return ret;
  195. if (val && !INVALID_CMD(val))
  196. return -EBUSY;
  197. if (in_len) {
  198. ret = tps6598x_block_write(tps, TPS_REG_DATA1,
  199. in_data, in_len);
  200. if (ret)
  201. return ret;
  202. }
  203. ret = tps6598x_write_4cc(tps, TPS_REG_CMD1, cmd);
  204. if (ret < 0)
  205. return ret;
  206. /* XXX: Using 1s for now, but it may not be enough for every command. */
  207. timeout = jiffies + msecs_to_jiffies(1000);
  208. do {
  209. ret = tps6598x_read32(tps, TPS_REG_CMD1, &val);
  210. if (ret)
  211. return ret;
  212. if (INVALID_CMD(val))
  213. return -EINVAL;
  214. if (time_is_before_jiffies(timeout))
  215. return -ETIMEDOUT;
  216. } while (val);
  217. if (out_len) {
  218. ret = tps6598x_block_read(tps, TPS_REG_DATA1,
  219. out_data, out_len);
  220. if (ret)
  221. return ret;
  222. val = out_data[0];
  223. } else {
  224. ret = tps6598x_block_read(tps, TPS_REG_DATA1, &val, sizeof(u8));
  225. if (ret)
  226. return ret;
  227. }
  228. switch (val) {
  229. case TPS_TASK_TIMEOUT:
  230. return -ETIMEDOUT;
  231. case TPS_TASK_REJECTED:
  232. return -EPERM;
  233. default:
  234. break;
  235. }
  236. return 0;
  237. }
  238. static int
  239. tps6598x_dr_set(const struct typec_capability *cap, enum typec_data_role role)
  240. {
  241. struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
  242. const char *cmd = (role == TYPEC_DEVICE) ? "SWUF" : "SWDF";
  243. u32 status;
  244. int ret;
  245. mutex_lock(&tps->lock);
  246. ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
  247. if (ret)
  248. goto out_unlock;
  249. ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
  250. if (ret)
  251. goto out_unlock;
  252. if (role != TPS_STATUS_DATAROLE(status)) {
  253. ret = -EPROTO;
  254. goto out_unlock;
  255. }
  256. typec_set_data_role(tps->port, role);
  257. out_unlock:
  258. mutex_unlock(&tps->lock);
  259. return ret;
  260. }
  261. static int
  262. tps6598x_pr_set(const struct typec_capability *cap, enum typec_role role)
  263. {
  264. struct tps6598x *tps = container_of(cap, struct tps6598x, typec_cap);
  265. const char *cmd = (role == TYPEC_SINK) ? "SWSk" : "SWSr";
  266. u32 status;
  267. int ret;
  268. mutex_lock(&tps->lock);
  269. ret = tps6598x_exec_cmd(tps, cmd, 0, NULL, 0, NULL);
  270. if (ret)
  271. goto out_unlock;
  272. ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
  273. if (ret)
  274. goto out_unlock;
  275. if (role != TPS_STATUS_PORTROLE(status)) {
  276. ret = -EPROTO;
  277. goto out_unlock;
  278. }
  279. typec_set_pwr_role(tps->port, role);
  280. out_unlock:
  281. mutex_unlock(&tps->lock);
  282. return ret;
  283. }
  284. static irqreturn_t tps6598x_interrupt(int irq, void *data)
  285. {
  286. struct tps6598x *tps = data;
  287. u64 event1;
  288. u64 event2;
  289. u32 status;
  290. int ret;
  291. mutex_lock(&tps->lock);
  292. ret = tps6598x_read64(tps, TPS_REG_INT_EVENT1, &event1);
  293. ret |= tps6598x_read64(tps, TPS_REG_INT_EVENT2, &event2);
  294. if (ret) {
  295. dev_err(tps->dev, "%s: failed to read events\n", __func__);
  296. goto err_unlock;
  297. }
  298. ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
  299. if (ret) {
  300. dev_err(tps->dev, "%s: failed to read status\n", __func__);
  301. goto err_clear_ints;
  302. }
  303. /* Handle plug insert or removal */
  304. if ((event1 | event2) & TPS_REG_INT_PLUG_EVENT) {
  305. if (status & TPS_STATUS_PLUG_PRESENT) {
  306. ret = tps6598x_connect(tps, status);
  307. if (ret)
  308. dev_err(tps->dev,
  309. "failed to register partner\n");
  310. } else {
  311. tps6598x_disconnect(tps, status);
  312. }
  313. }
  314. err_clear_ints:
  315. tps6598x_write64(tps, TPS_REG_INT_CLEAR1, event1);
  316. tps6598x_write64(tps, TPS_REG_INT_CLEAR2, event2);
  317. err_unlock:
  318. mutex_unlock(&tps->lock);
  319. return IRQ_HANDLED;
  320. }
  321. static const struct regmap_config tps6598x_regmap_config = {
  322. .reg_bits = 8,
  323. .val_bits = 8,
  324. .max_register = 0x7F,
  325. };
  326. static int tps6598x_probe(struct i2c_client *client)
  327. {
  328. struct tps6598x *tps;
  329. u32 status;
  330. u32 conf;
  331. u32 vid;
  332. int ret;
  333. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  334. if (!tps)
  335. return -ENOMEM;
  336. mutex_init(&tps->lock);
  337. tps->dev = &client->dev;
  338. tps->regmap = devm_regmap_init_i2c(client, &tps6598x_regmap_config);
  339. if (IS_ERR(tps->regmap))
  340. return PTR_ERR(tps->regmap);
  341. ret = tps6598x_read32(tps, 0, &vid);
  342. if (ret < 0)
  343. return ret;
  344. if (!vid)
  345. return -ENODEV;
  346. /*
  347. * Checking can the adapter handle SMBus protocol. If it can not, the
  348. * driver needs to take care of block reads separately.
  349. *
  350. * FIXME: Testing with I2C_FUNC_I2C. regmap-i2c uses I2C protocol
  351. * unconditionally if the adapter has I2C_FUNC_I2C set.
  352. */
  353. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  354. tps->i2c_protocol = true;
  355. ret = tps6598x_read32(tps, TPS_REG_STATUS, &status);
  356. if (ret < 0)
  357. return ret;
  358. ret = tps6598x_read32(tps, TPS_REG_SYSTEM_CONF, &conf);
  359. if (ret < 0)
  360. return ret;
  361. tps->typec_cap.revision = USB_TYPEC_REV_1_2;
  362. tps->typec_cap.pd_revision = 0x200;
  363. tps->typec_cap.prefer_role = TYPEC_NO_PREFERRED_ROLE;
  364. tps->typec_cap.pr_set = tps6598x_pr_set;
  365. tps->typec_cap.dr_set = tps6598x_dr_set;
  366. switch (TPS_SYSCONF_PORTINFO(conf)) {
  367. case TPS_PORTINFO_SINK_ACCESSORY:
  368. case TPS_PORTINFO_SINK:
  369. tps->typec_cap.type = TYPEC_PORT_SNK;
  370. tps->typec_cap.data = TYPEC_PORT_UFP;
  371. break;
  372. case TPS_PORTINFO_DRP_UFP_DRD:
  373. case TPS_PORTINFO_DRP_DFP_DRD:
  374. tps->typec_cap.type = TYPEC_PORT_DRP;
  375. tps->typec_cap.data = TYPEC_PORT_DRD;
  376. break;
  377. case TPS_PORTINFO_DRP_UFP:
  378. tps->typec_cap.type = TYPEC_PORT_DRP;
  379. tps->typec_cap.data = TYPEC_PORT_UFP;
  380. break;
  381. case TPS_PORTINFO_DRP_DFP:
  382. tps->typec_cap.type = TYPEC_PORT_DRP;
  383. tps->typec_cap.data = TYPEC_PORT_DFP;
  384. break;
  385. case TPS_PORTINFO_SOURCE:
  386. tps->typec_cap.type = TYPEC_PORT_SRC;
  387. tps->typec_cap.data = TYPEC_PORT_DFP;
  388. break;
  389. default:
  390. return -ENODEV;
  391. }
  392. tps->port = typec_register_port(&client->dev, &tps->typec_cap);
  393. if (IS_ERR(tps->port))
  394. return PTR_ERR(tps->port);
  395. if (status & TPS_STATUS_PLUG_PRESENT) {
  396. ret = tps6598x_connect(tps, status);
  397. if (ret)
  398. dev_err(&client->dev, "failed to register partner\n");
  399. }
  400. ret = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  401. tps6598x_interrupt,
  402. IRQF_SHARED | IRQF_ONESHOT,
  403. dev_name(&client->dev), tps);
  404. if (ret) {
  405. tps6598x_disconnect(tps, 0);
  406. typec_unregister_port(tps->port);
  407. return ret;
  408. }
  409. i2c_set_clientdata(client, tps);
  410. return 0;
  411. }
  412. static int tps6598x_remove(struct i2c_client *client)
  413. {
  414. struct tps6598x *tps = i2c_get_clientdata(client);
  415. tps6598x_disconnect(tps, 0);
  416. typec_unregister_port(tps->port);
  417. return 0;
  418. }
  419. static const struct acpi_device_id tps6598x_acpi_match[] = {
  420. { "INT3515", 0 },
  421. { }
  422. };
  423. MODULE_DEVICE_TABLE(acpi, tps6598x_acpi_match);
  424. static struct i2c_driver tps6598x_i2c_driver = {
  425. .driver = {
  426. .name = "tps6598x",
  427. .acpi_match_table = tps6598x_acpi_match,
  428. },
  429. .probe_new = tps6598x_probe,
  430. .remove = tps6598x_remove,
  431. };
  432. module_i2c_driver(tps6598x_i2c_driver);
  433. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  434. MODULE_LICENSE("GPL v2");
  435. MODULE_DESCRIPTION("TI TPS6598x USB Power Delivery Controller Driver");