intel_cht_int33fe.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Intel Cherry Trail ACPI INT33FE pseudo device driver
  3. *
  4. * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Some Intel Cherry Trail based device which ship with Windows 10, have
  11. * this weird INT33FE ACPI device with a CRS table with 4 I2cSerialBusV2
  12. * resources, for 4 different chips attached to various i2c busses:
  13. * 1. The Whiskey Cove pmic, which is also described by the INT34D3 ACPI device
  14. * 2. Maxim MAX17047 Fuel Gauge Controller
  15. * 3. FUSB302 USB Type-C Controller
  16. * 4. PI3USB30532 USB switch
  17. *
  18. * So this driver is a stub / pseudo driver whose only purpose is to
  19. * instantiate i2c-clients for chips 2 - 4, so that standard i2c drivers
  20. * for these chips can bind to the them.
  21. */
  22. #include <linux/acpi.h>
  23. #include <linux/i2c.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/regulator/consumer.h>
  27. #include <linux/slab.h>
  28. #define EXPECTED_PTYPE 4
  29. struct cht_int33fe_data {
  30. struct i2c_client *max17047;
  31. struct i2c_client *fusb302;
  32. struct i2c_client *pi3usb30532;
  33. /* Contain a list-head must be per device */
  34. struct device_connection connections[3];
  35. };
  36. /*
  37. * Grrr I severly dislike buggy BIOS-es. At least one BIOS enumerates
  38. * the max17047 both through the INT33FE ACPI device (it is right there
  39. * in the resources table) as well as through a separate MAX17047 device.
  40. *
  41. * These helpers are used to work around this by checking if an i2c-client
  42. * for the max17047 has already been registered.
  43. */
  44. static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
  45. {
  46. struct i2c_client **max17047 = data;
  47. struct acpi_device *adev;
  48. const char *hid;
  49. adev = ACPI_COMPANION(dev);
  50. if (!adev)
  51. return 0;
  52. hid = acpi_device_hid(adev);
  53. /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
  54. if (strcmp(hid, "MAX17047"))
  55. return 0;
  56. *max17047 = to_i2c_client(dev);
  57. return 1;
  58. }
  59. static struct i2c_client *cht_int33fe_find_max17047(void)
  60. {
  61. struct i2c_client *max17047 = NULL;
  62. i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
  63. return max17047;
  64. }
  65. static const char * const max17047_suppliers[] = { "bq24190-charger" };
  66. static const struct property_entry max17047_props[] = {
  67. PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
  68. { }
  69. };
  70. static const struct property_entry fusb302_props[] = {
  71. PROPERTY_ENTRY_STRING("fcs,extcon-name", "cht_wcove_pwrsrc"),
  72. PROPERTY_ENTRY_U32("fcs,max-sink-microvolt", 12000000),
  73. PROPERTY_ENTRY_U32("fcs,max-sink-microamp", 3000000),
  74. PROPERTY_ENTRY_U32("fcs,max-sink-microwatt", 36000000),
  75. { }
  76. };
  77. static int cht_int33fe_probe(struct i2c_client *client)
  78. {
  79. struct device *dev = &client->dev;
  80. struct i2c_board_info board_info;
  81. struct cht_int33fe_data *data;
  82. struct i2c_client *max17047;
  83. struct regulator *regulator;
  84. unsigned long long ptyp;
  85. acpi_status status;
  86. int fusb302_irq;
  87. int ret;
  88. status = acpi_evaluate_integer(ACPI_HANDLE(dev), "PTYP", NULL, &ptyp);
  89. if (ACPI_FAILURE(status)) {
  90. dev_err(dev, "Error getting PTYPE\n");
  91. return -ENODEV;
  92. }
  93. /*
  94. * The same ACPI HID is used for different configurations check PTYP
  95. * to ensure that we are dealing with the expected config.
  96. */
  97. if (ptyp != EXPECTED_PTYPE)
  98. return -ENODEV;
  99. /* Check presence of INT34D3 (hardware-rev 3) expected for ptype == 4 */
  100. if (!acpi_dev_present("INT34D3", "1", 3)) {
  101. dev_err(dev, "Error PTYPE == %d, but no INT34D3 device\n",
  102. EXPECTED_PTYPE);
  103. return -ENODEV;
  104. }
  105. /*
  106. * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
  107. * We check for the bq24292i vbus regulator here, this has 2 purposes:
  108. * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
  109. * max-snk voltage to 12V with another charger-IC is not good.
  110. * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
  111. * regulator-map, which is part of the bq24292i regulator_init_data,
  112. * must be registered before the fusb302 is instantiated, otherwise
  113. * it will end up with a dummy-regulator.
  114. * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
  115. * which is defined in i2c-cht-wc.c from where the bq24292i i2c-client
  116. * gets instantiated. We use regulator_get_optional here so that we
  117. * don't end up getting a dummy-regulator ourselves.
  118. */
  119. regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
  120. if (IS_ERR(regulator)) {
  121. ret = PTR_ERR(regulator);
  122. return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
  123. }
  124. regulator_put(regulator);
  125. /* The FUSB302 uses the irq at index 1 and is the only irq user */
  126. fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
  127. if (fusb302_irq < 0) {
  128. if (fusb302_irq != -EPROBE_DEFER)
  129. dev_err(dev, "Error getting FUSB302 irq\n");
  130. return fusb302_irq;
  131. }
  132. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  133. if (!data)
  134. return -ENOMEM;
  135. /* Work around BIOS bug, see comment on cht_int33fe_find_max17047 */
  136. max17047 = cht_int33fe_find_max17047();
  137. if (max17047) {
  138. /* Pre-existing i2c-client for the max17047, add device-props */
  139. ret = device_add_properties(&max17047->dev, max17047_props);
  140. if (ret)
  141. return ret;
  142. /* And re-probe to get the new device-props applied. */
  143. ret = device_reprobe(&max17047->dev);
  144. if (ret)
  145. dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
  146. } else {
  147. memset(&board_info, 0, sizeof(board_info));
  148. strlcpy(board_info.type, "max17047", I2C_NAME_SIZE);
  149. board_info.dev_name = "max17047";
  150. board_info.properties = max17047_props;
  151. data->max17047 = i2c_acpi_new_device(dev, 1, &board_info);
  152. if (!data->max17047)
  153. return -EPROBE_DEFER; /* Wait for i2c-adapter to load */
  154. }
  155. data->connections[0].endpoint[0] = "i2c-fusb302";
  156. data->connections[0].endpoint[1] = "i2c-pi3usb30532";
  157. data->connections[0].id = "typec-switch";
  158. data->connections[1].endpoint[0] = "i2c-fusb302";
  159. data->connections[1].endpoint[1] = "i2c-pi3usb30532";
  160. data->connections[1].id = "typec-mux";
  161. data->connections[2].endpoint[0] = "i2c-fusb302";
  162. data->connections[2].endpoint[1] = "intel_xhci_usb_sw-role-switch";
  163. data->connections[2].id = "usb-role-switch";
  164. device_connection_add(&data->connections[0]);
  165. device_connection_add(&data->connections[1]);
  166. device_connection_add(&data->connections[2]);
  167. memset(&board_info, 0, sizeof(board_info));
  168. strlcpy(board_info.type, "typec_fusb302", I2C_NAME_SIZE);
  169. board_info.dev_name = "fusb302";
  170. board_info.properties = fusb302_props;
  171. board_info.irq = fusb302_irq;
  172. data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
  173. if (!data->fusb302)
  174. goto out_unregister_max17047;
  175. memset(&board_info, 0, sizeof(board_info));
  176. board_info.dev_name = "pi3usb30532";
  177. strlcpy(board_info.type, "pi3usb30532", I2C_NAME_SIZE);
  178. data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
  179. if (!data->pi3usb30532)
  180. goto out_unregister_fusb302;
  181. i2c_set_clientdata(client, data);
  182. return 0;
  183. out_unregister_fusb302:
  184. i2c_unregister_device(data->fusb302);
  185. out_unregister_max17047:
  186. if (data->max17047)
  187. i2c_unregister_device(data->max17047);
  188. device_connection_remove(&data->connections[2]);
  189. device_connection_remove(&data->connections[1]);
  190. device_connection_remove(&data->connections[0]);
  191. return -EPROBE_DEFER; /* Wait for the i2c-adapter to load */
  192. }
  193. static int cht_int33fe_remove(struct i2c_client *i2c)
  194. {
  195. struct cht_int33fe_data *data = i2c_get_clientdata(i2c);
  196. i2c_unregister_device(data->pi3usb30532);
  197. i2c_unregister_device(data->fusb302);
  198. if (data->max17047)
  199. i2c_unregister_device(data->max17047);
  200. device_connection_remove(&data->connections[2]);
  201. device_connection_remove(&data->connections[1]);
  202. device_connection_remove(&data->connections[0]);
  203. return 0;
  204. }
  205. static const struct i2c_device_id cht_int33fe_i2c_id[] = {
  206. { }
  207. };
  208. MODULE_DEVICE_TABLE(i2c, cht_int33fe_i2c_id);
  209. static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
  210. { "INT33FE", },
  211. { }
  212. };
  213. MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
  214. static struct i2c_driver cht_int33fe_driver = {
  215. .driver = {
  216. .name = "Intel Cherry Trail ACPI INT33FE driver",
  217. .acpi_match_table = ACPI_PTR(cht_int33fe_acpi_ids),
  218. },
  219. .probe_new = cht_int33fe_probe,
  220. .remove = cht_int33fe_remove,
  221. .id_table = cht_int33fe_i2c_id,
  222. .disable_i2c_core_irq_mapping = true,
  223. };
  224. module_i2c_driver(cht_int33fe_driver);
  225. MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
  226. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  227. MODULE_LICENSE("GPL");