intel_cht_int33fe_typec.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel Cherry Trail ACPI INT33FE pseudo device driver
  4. *
  5. * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
  6. *
  7. * Some Intel Cherry Trail based device which ship with Windows 10, have
  8. * this weird INT33FE ACPI device with a CRS table with 4 I2cSerialBusV2
  9. * resources, for 4 different chips attached to various I²C buses:
  10. * 1. The Whiskey Cove PMIC, which is also described by the INT34D3 ACPI device
  11. * 2. Maxim MAX17047 Fuel Gauge Controller
  12. * 3. FUSB302 USB Type-C Controller
  13. * 4. PI3USB30532 USB switch
  14. *
  15. * So this driver is a stub / pseudo driver whose only purpose is to
  16. * instantiate I²C clients for chips 2 - 4, so that standard I²C drivers
  17. * for these chips can bind to the them.
  18. */
  19. #include <linux/i2c.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/pci.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/property.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/slab.h>
  26. #include <linux/usb/pd.h>
  27. #include "intel_cht_int33fe_common.h"
  28. /*
  29. * Grrr, I severely dislike buggy BIOS-es. At least one BIOS enumerates
  30. * the max17047 both through the INT33FE ACPI device (it is right there
  31. * in the resources table) as well as through a separate MAX17047 device.
  32. *
  33. * These helpers are used to work around this by checking if an I²C client
  34. * for the max17047 has already been registered.
  35. */
  36. static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
  37. {
  38. struct i2c_client **max17047 = data;
  39. struct acpi_device *adev;
  40. adev = ACPI_COMPANION(dev);
  41. if (!adev)
  42. return 0;
  43. /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
  44. if (!acpi_dev_hid_uid_match(adev, "MAX17047", NULL))
  45. return 0;
  46. *max17047 = to_i2c_client(dev);
  47. return 1;
  48. }
  49. static const char * const max17047_suppliers[] = { "bq24190-charger" };
  50. static const struct property_entry max17047_properties[] = {
  51. PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
  52. { }
  53. };
  54. static const struct software_node max17047_node = {
  55. .name = "max17047",
  56. .properties = max17047_properties,
  57. };
  58. /*
  59. * We are not using inline property here because those are constant,
  60. * and we need to adjust this one at runtime to point to real
  61. * software node.
  62. */
  63. static struct software_node_ref_args fusb302_mux_refs[] = {
  64. { .node = NULL },
  65. };
  66. static const struct property_entry fusb302_properties[] = {
  67. PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
  68. PROPERTY_ENTRY_REF_ARRAY("usb-role-switch", fusb302_mux_refs),
  69. { }
  70. };
  71. static const struct software_node fusb302_node = {
  72. .name = "fusb302",
  73. .properties = fusb302_properties,
  74. };
  75. #define PDO_FIXED_FLAGS \
  76. (PDO_FIXED_DUAL_ROLE | PDO_FIXED_DATA_SWAP | PDO_FIXED_USB_COMM)
  77. static const u32 src_pdo[] = {
  78. PDO_FIXED(5000, 1500, PDO_FIXED_FLAGS),
  79. };
  80. static const u32 snk_pdo[] = {
  81. PDO_FIXED(5000, 400, PDO_FIXED_FLAGS),
  82. PDO_VAR(5000, 12000, 3000),
  83. };
  84. static const struct software_node pi3usb30532_node = {
  85. .name = "pi3usb30532",
  86. };
  87. static const struct software_node displayport_node = {
  88. .name = "displayport",
  89. };
  90. static const struct property_entry usb_connector_properties[] = {
  91. PROPERTY_ENTRY_STRING("data-role", "dual"),
  92. PROPERTY_ENTRY_STRING("power-role", "dual"),
  93. PROPERTY_ENTRY_STRING("try-power-role", "sink"),
  94. PROPERTY_ENTRY_U32_ARRAY("source-pdos", src_pdo),
  95. PROPERTY_ENTRY_U32_ARRAY("sink-pdos", snk_pdo),
  96. PROPERTY_ENTRY_U32("op-sink-microwatt", 2500000),
  97. PROPERTY_ENTRY_REF("orientation-switch", &pi3usb30532_node),
  98. PROPERTY_ENTRY_REF("mode-switch", &pi3usb30532_node),
  99. PROPERTY_ENTRY_REF("displayport", &displayport_node),
  100. { }
  101. };
  102. static const struct software_node usb_connector_node = {
  103. .name = "connector",
  104. .parent = &fusb302_node,
  105. .properties = usb_connector_properties,
  106. };
  107. static const struct software_node *node_group[] = {
  108. &fusb302_node,
  109. &max17047_node,
  110. &pi3usb30532_node,
  111. &displayport_node,
  112. &usb_connector_node,
  113. NULL
  114. };
  115. static int cht_int33fe_setup_dp(struct cht_int33fe_data *data)
  116. {
  117. struct fwnode_handle *fwnode;
  118. struct pci_dev *pdev;
  119. fwnode = software_node_fwnode(&displayport_node);
  120. if (!fwnode)
  121. return -ENODEV;
  122. /* First let's find the GPU PCI device */
  123. pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, NULL);
  124. if (!pdev || pdev->vendor != PCI_VENDOR_ID_INTEL) {
  125. pci_dev_put(pdev);
  126. return -ENODEV;
  127. }
  128. /* Then the DP child device node */
  129. data->dp = device_get_named_child_node(&pdev->dev, "DD02");
  130. pci_dev_put(pdev);
  131. if (!data->dp)
  132. return -ENODEV;
  133. fwnode->secondary = ERR_PTR(-ENODEV);
  134. data->dp->secondary = fwnode;
  135. return 0;
  136. }
  137. static void cht_int33fe_remove_nodes(struct cht_int33fe_data *data)
  138. {
  139. software_node_unregister_node_group(node_group);
  140. if (fusb302_mux_refs[0].node) {
  141. fwnode_handle_put(software_node_fwnode(fusb302_mux_refs[0].node));
  142. fusb302_mux_refs[0].node = NULL;
  143. }
  144. if (data->dp) {
  145. data->dp->secondary = NULL;
  146. fwnode_handle_put(data->dp);
  147. data->dp = NULL;
  148. }
  149. }
  150. static int cht_int33fe_add_nodes(struct cht_int33fe_data *data)
  151. {
  152. const struct software_node *mux_ref_node;
  153. int ret;
  154. /*
  155. * There is no ACPI device node for the USB role mux, so we need to wait
  156. * until the mux driver has created software node for the mux device.
  157. * It means we depend on the mux driver. This function will return
  158. * -EPROBE_DEFER until the mux device is registered.
  159. */
  160. mux_ref_node = software_node_find_by_name(NULL, "intel-xhci-usb-sw");
  161. if (!mux_ref_node)
  162. return -EPROBE_DEFER;
  163. /*
  164. * Update node used in "usb-role-switch" property. Note that we
  165. * rely on software_node_register_nodes() to use the original
  166. * instance of properties instead of copying them.
  167. */
  168. fusb302_mux_refs[0].node = mux_ref_node;
  169. ret = software_node_register_node_group(node_group);
  170. if (ret)
  171. return ret;
  172. /* The devices that are not created in this driver need extra steps. */
  173. /*
  174. * The DP connector does have ACPI device node. In this case we can just
  175. * find that ACPI node and assign our node as the secondary node to it.
  176. */
  177. ret = cht_int33fe_setup_dp(data);
  178. if (ret)
  179. goto err_remove_nodes;
  180. return 0;
  181. err_remove_nodes:
  182. cht_int33fe_remove_nodes(data);
  183. return ret;
  184. }
  185. static int
  186. cht_int33fe_register_max17047(struct device *dev, struct cht_int33fe_data *data)
  187. {
  188. struct i2c_client *max17047 = NULL;
  189. struct i2c_board_info board_info;
  190. struct fwnode_handle *fwnode;
  191. int ret;
  192. fwnode = software_node_fwnode(&max17047_node);
  193. if (!fwnode)
  194. return -ENODEV;
  195. i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
  196. if (max17047) {
  197. /* Pre-existing I²C client for the max17047, add device properties */
  198. set_secondary_fwnode(&max17047->dev, fwnode);
  199. /* And re-probe to get the new device properties applied */
  200. ret = device_reprobe(&max17047->dev);
  201. if (ret)
  202. dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
  203. return 0;
  204. }
  205. memset(&board_info, 0, sizeof(board_info));
  206. strlcpy(board_info.type, "max17047", I2C_NAME_SIZE);
  207. board_info.dev_name = "max17047";
  208. board_info.fwnode = fwnode;
  209. data->battery_fg = i2c_acpi_new_device(dev, 1, &board_info);
  210. return PTR_ERR_OR_ZERO(data->battery_fg);
  211. }
  212. int cht_int33fe_typec_probe(struct cht_int33fe_data *data)
  213. {
  214. struct device *dev = data->dev;
  215. struct i2c_board_info board_info;
  216. struct fwnode_handle *fwnode;
  217. struct regulator *regulator;
  218. int fusb302_irq;
  219. int ret;
  220. /*
  221. * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
  222. * We check for the bq24292i vbus regulator here, this has 2 purposes:
  223. * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
  224. * max-snk voltage to 12V with another charger-IC is not good.
  225. * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
  226. * regulator-map, which is part of the bq24292i regulator_init_data,
  227. * must be registered before the fusb302 is instantiated, otherwise
  228. * it will end up with a dummy-regulator.
  229. * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
  230. * which is defined in i2c-cht-wc.c from where the bq24292i I²C client
  231. * gets instantiated. We use regulator_get_optional here so that we
  232. * don't end up getting a dummy-regulator ourselves.
  233. */
  234. regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
  235. if (IS_ERR(regulator)) {
  236. ret = PTR_ERR(regulator);
  237. return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
  238. }
  239. regulator_put(regulator);
  240. /* The FUSB302 uses the IRQ at index 1 and is the only IRQ user */
  241. fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
  242. if (fusb302_irq < 0) {
  243. if (fusb302_irq != -EPROBE_DEFER)
  244. dev_err(dev, "Error getting FUSB302 irq\n");
  245. return fusb302_irq;
  246. }
  247. ret = cht_int33fe_add_nodes(data);
  248. if (ret)
  249. return ret;
  250. /* Work around BIOS bug, see comment on cht_int33fe_check_for_max17047() */
  251. ret = cht_int33fe_register_max17047(dev, data);
  252. if (ret)
  253. goto out_remove_nodes;
  254. fwnode = software_node_fwnode(&fusb302_node);
  255. if (!fwnode) {
  256. ret = -ENODEV;
  257. goto out_unregister_max17047;
  258. }
  259. memset(&board_info, 0, sizeof(board_info));
  260. strlcpy(board_info.type, "typec_fusb302", I2C_NAME_SIZE);
  261. board_info.dev_name = "fusb302";
  262. board_info.fwnode = fwnode;
  263. board_info.irq = fusb302_irq;
  264. data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
  265. if (IS_ERR(data->fusb302)) {
  266. ret = PTR_ERR(data->fusb302);
  267. goto out_unregister_max17047;
  268. }
  269. fwnode = software_node_fwnode(&pi3usb30532_node);
  270. if (!fwnode) {
  271. ret = -ENODEV;
  272. goto out_unregister_fusb302;
  273. }
  274. memset(&board_info, 0, sizeof(board_info));
  275. board_info.dev_name = "pi3usb30532";
  276. board_info.fwnode = fwnode;
  277. strlcpy(board_info.type, "pi3usb30532", I2C_NAME_SIZE);
  278. data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
  279. if (IS_ERR(data->pi3usb30532)) {
  280. ret = PTR_ERR(data->pi3usb30532);
  281. goto out_unregister_fusb302;
  282. }
  283. return 0;
  284. out_unregister_fusb302:
  285. i2c_unregister_device(data->fusb302);
  286. out_unregister_max17047:
  287. i2c_unregister_device(data->battery_fg);
  288. out_remove_nodes:
  289. cht_int33fe_remove_nodes(data);
  290. return ret;
  291. }
  292. int cht_int33fe_typec_remove(struct cht_int33fe_data *data)
  293. {
  294. i2c_unregister_device(data->pi3usb30532);
  295. i2c_unregister_device(data->fusb302);
  296. i2c_unregister_device(data->battery_fg);
  297. cht_int33fe_remove_nodes(data);
  298. return 0;
  299. }