Message ID | 20230929143055.31360-1-alkuor@gmail.com |
---|---|
Headers | show |
Series | Add TPS25750 USB type-C PD controller support | expand |
On Fri, Sep 29, 2023 at 10:30:49AM -0400, Abdel Alkuor wrote: > From: Abdel Alkuor <abdelalkuor@geotab.com> > > tps25750 event registers structure is different than tps6598x's, > tps25750 has 11 bytes of events which are read at once where > tps6598x has two event registers of 8 bytes each which are read > separately. Likewise MASK event registers. Also, not all events > are supported in both devices. > > - Create a new handler to accommodate tps25750 interrupt > - Add device data to of_device_id > > Signed-off-by: Abdel Alkuor <abdelalkuor@geotab.com> > --- > Changes in v8: > - Populate of_device_id with device data > - Change tps->cb to tps->data > - Assign matched data to tps data > Changes in v7: > - Add driver name to commit subject > - Create tps25750 interrupt handler > Changes in v6: > - Create tipd callbacks factory > Changes in v5: > - Incorporating tps25750 into tps6598x driver > > drivers/usb/typec/tipd/core.c | 109 +++++++++++++++++++++++++++++----- > 1 file changed, 95 insertions(+), 14 deletions(-) > > diff --git a/drivers/usb/typec/tipd/core.c b/drivers/usb/typec/tipd/core.c > index 32e42798688f..9dc4c3ae5c60 100644 > --- a/drivers/usb/typec/tipd/core.c > +++ b/drivers/usb/typec/tipd/core.c > @@ -18,6 +18,7 @@ > #include <linux/usb/role.h> > #include <linux/workqueue.h> > #include <linux/firmware.h> > +#include <linux/of_device.h> > > #include "tps6598x.h" > #include "trace.h" > @@ -101,6 +102,10 @@ static const char *const modes[] = { > /* Unrecognized commands will be replaced with "!CMD" */ > #define INVALID_CMD(_cmd_) (_cmd_ == 0x444d4321) > > +struct tipd_data { > + irq_handler_t irq_handler; > +}; > + > struct tps6598x { > struct device *dev; > struct regmap *regmap; > @@ -118,9 +123,11 @@ struct tps6598x { > enum power_supply_usb_type usb_type; > > int wakeup; > + u32 status; /* status reg */ > u16 pwr_status; > struct delayed_work wq_poll; > - irq_handler_t irq_handler; > + > + const struct tipd_data *data; > }; > > static enum power_supply_property tps6598x_psy_props[] = { > @@ -545,6 +552,64 @@ static irqreturn_t cd321x_interrupt(int irq, void *data) > return IRQ_NONE; > } > > +static bool tps6598x_has_role_changed(struct tps6598x *tps, u32 status) > +{ > + status ^= tps->status; > + > + return status & (TPS_STATUS_PORTROLE | TPS_STATUS_DATAROLE); > +} > + > +static irqreturn_t tps25750_interrupt(int irq, void *data) > +{ > + struct tps6598x *tps = data; > + u64 event[2] = { }; > + u32 status; > + int ret; > + > + mutex_lock(&tps->lock); > + > + ret = tps6598x_block_read(tps, TPS_REG_INT_EVENT1, event, 11); > + if (ret) { > + dev_err(tps->dev, "%s: failed to read events\n", __func__); > + goto err_unlock; > + } > + > + if (!(event[0] | event[1])) > + goto err_unlock; > + > + if (!tps6598x_read_status(tps, &status)) > + goto err_clear_ints; > + > + if ((event[0] | event[1]) & TPS_REG_INT_POWER_STATUS_UPDATE) > + if (!tps6598x_read_power_status(tps)) > + goto err_clear_ints; > + > + if ((event[0] | event[1]) & TPS_REG_INT_DATA_STATUS_UPDATE) > + if (!tps6598x_read_data_status(tps)) > + goto err_clear_ints; > + > + /* > + * data/port roles could be updated independently after > + * a plug event. Therefore, we need to check > + * for pr/dr status change to set TypeC dr/pr accordingly. > + */ > + if ((event[0] | event[1]) & TPS_REG_INT_PLUG_EVENT || > + tps6598x_has_role_changed(tps, status)) > + tps6598x_handle_plug_event(tps, status); > + > + tps->status = status; > + > +err_clear_ints: > + tps6598x_block_write(tps, TPS_REG_INT_CLEAR1, event, 11); > + > +err_unlock: > + mutex_unlock(&tps->lock); > + > + if (event[0] | event[1]) > + return IRQ_HANDLED; > + return IRQ_NONE; > +} > + > static irqreturn_t tps6598x_interrupt(int irq, void *data) > { > struct tps6598x *tps = data; > @@ -600,7 +665,7 @@ static void tps6598x_poll_work(struct work_struct *work) > struct tps6598x *tps = container_of(to_delayed_work(work), > struct tps6598x, wq_poll); > > - tps->irq_handler(0, tps); > + tps->data->irq_handler(0, tps); > queue_delayed_work(system_power_efficient_wq, > &tps->wq_poll, msecs_to_jiffies(POLL_INTERVAL)); > } > @@ -967,13 +1032,33 @@ static int tps25750_apply_patch(struct tps6598x *tps) > return 0; > }; > > +static const struct tipd_data cd321x_data = { > + .irq_handler = cd321x_interrupt, > +}; > + > +static const struct tipd_data tps6598x_data = { > + .irq_handler = tps6598x_interrupt, > +}; > + > +static const struct tipd_data tps25750_data = { > + .irq_handler = tps25750_interrupt, > +}; > + > +static const struct of_device_id tps6598x_of_match[] = { > + { .compatible = "ti,tps6598x", &tps6598x_data}, > + { .compatible = "apple,cd321x", &cd321x_data}, > + { .compatible = "ti,tps25750", &tps25750_data}, > + {} > +}; > +MODULE_DEVICE_TABLE(of, tps6598x_of_match); There is no reason to move that here. Keep all of those below where tps6598x_of_match was originally. > static int tps6598x_probe(struct i2c_client *client) > { > - irq_handler_t irq_handler = tps6598x_interrupt; > struct device_node *np = client->dev.of_node; > struct typec_capability typec_cap = { }; > struct tps6598x *tps; > struct fwnode_handle *fwnode; > + const struct of_device_id *match; > u32 status; > u32 conf; > u32 vid; > @@ -1017,7 +1102,6 @@ static int tps6598x_probe(struct i2c_client *client) > APPLE_CD_REG_INT_DATA_STATUS_UPDATE | > APPLE_CD_REG_INT_PLUG_EVENT; > > - irq_handler = cd321x_interrupt; > } else { > /* Enable power status, data status and plug event interrupts */ > mask1 = TPS_REG_INT_POWER_STATUS_UPDATE | > @@ -1025,7 +1109,12 @@ static int tps6598x_probe(struct i2c_client *client) > TPS_REG_INT_PLUG_EVENT; > } > > - tps->irq_handler = irq_handler; > + match = of_match_device(tps6598x_of_match, tps->dev); > + if (!match) > + return -EINVAL; > + > + tps->data = match->data; tps->data = device_get_match_data(tps->dev); > /* Make sure the controller has application firmware running */ > ret = tps6598x_check_mode(tps); > if (ret < 0) > @@ -1125,7 +1214,7 @@ static int tps6598x_probe(struct i2c_client *client) > > if (client->irq) { > ret = devm_request_threaded_irq(&client->dev, client->irq, NULL, > - irq_handler, > + tps->data->irq_handler, > IRQF_SHARED | IRQF_ONESHOT, > dev_name(&client->dev), tps); > } else { > @@ -1231,14 +1320,6 @@ static const struct dev_pm_ops tps6598x_pm_ops = { > SET_SYSTEM_SLEEP_PM_OPS(tps6598x_suspend, tps6598x_resume) > }; > > -static const struct of_device_id tps6598x_of_match[] = { > - { .compatible = "ti,tps6598x", }, > - { .compatible = "apple,cd321x", }, > - { .compatible = "ti,tps25750", }, > - {} > -}; > -MODULE_DEVICE_TABLE(of, tps6598x_of_match); static const struct tipd_data cd321x_data = { .irq_handler = cd321x_interrupt, }; static const struct tipd_data tps6598x_data = { .irq_handler = tps6598x_interrupt, }; static const struct tipd_data tps25750_data = { .irq_handler = tps25750_interrupt, }; static const struct of_device_id tps6598x_of_match[] = { { .compatible = "apple,cd321x", &cd321x_data}, { .compatible = "ti,tps25750", &tps25750_data}, { .compatible = "ti,tps6598x", &tps6598x_data}, {} }; MODULE_DEVICE_TABLE(of, tps6598x_of_match); > static const struct i2c_device_id tps6598x_id[] = { > { "tps6598x" }, > { } thanks,
From: Abdel Alkuor <abdelalkuor@geotab.com> TPS25750 USB type-C PD controller has the same register offsets as tps6598x. The following is a summary of incorporating TPS25750 into TPS6598x driver: - Only Check VID register (0x00) for TPS6598x and cd321x, as TPS25750 doesn't have VID register. - TypeC port registration will be registered differently for each PD controller. TPS6598x uses system configuration register (0x28) to get pr/dr capabilities. On the other hand, TPS25750 will use data role property and PD status register (0x40) to get pr/dr capabilities as TPS25750 doesn't have register 0x28 supported. - TPS25750 requires writing a binary configuration to switch PD controller from PTCH mode to APP mode which needs the following changes: - Add PTCH mode to the modes list. - Add an argument to tps6598x_check_mode to return the current mode. - Currently, tps6598x_exec_cmd has cmd timeout hardcoded to 1 second, and doesn't wait before checking DATA_OUT response. In TPS25750, patch 4CCs take longer than 1 second to execute and some requires a delay before checking DATA_OUT. To accommodate that, cmd_timeout and response_delay will be added as arguments to tps6598x_exec_cmd. - Implement applying patch sequence for TPS25750. - In pm suspend callback, patch mode needs to be checked and the binary configuration should be applied if needed. - For interrupt, TPS25750 has only one event register (0x14) and one mask register (0x16) of 11 bytes each, where TPS6598x has two event and two mask registers of 8 bytes each. Both TPS25750 and TPS65986x shares the same bit field offsets for events/masks/clear but many of there fields are reserved in TPS25750, the following needs to be done in tps6598x_interrupt: - Read EVENT1 register as a block of 11 bytes when tps25750 is present - Write CLEAR1 register as a block of 11 bytes when tps25750 is present - Add trace_tps25750_irq - During testing, I noticed that when a cable is plugged into the PD controller and before PD controller switches to APP mode, there is a lag between dr/pr updates and PlugInsertOrRemoval Event, so a check for dr/pr change needs to be added along TPS_REG_INT_PLUG_EVENT check - Add TPS25750 traces for status and power status registers. Trace for data register won't be added as it doesn't exist in the device. - Configure sleep mode for TPS25750. v8: - PATCH 1: - Define reg-names at top-level - PATCH 2: - Add Reviewed-by - PATCH 3: - Revert mode check return - PATCH 4: - Return mode when mode is checked - Use device_is_compatible instead of of_device_is_compatible - PATCH 5,6: - No changes - PATCH 7: - Use device_is_compatible instead of of_device_is_compatible - PATCH 8,9: - No changes - PATCH 10: - Change tps->cb to tps->data - PATCH 11,12: - No changes - PATCH 13,14: - Change tps->cb to tps->data v7: - PATCH 1: - Define reg at top-level - Remove description from reg-names - PATCH 2..7: Add tps6598x to the subject - PATCH 8: - Add tps6598x to the subject - Create tps25750 interrupt handler - PATCH 9..11: Add tps6598x to the subject - PATCH 12: - Add driver name to commit subject - Call trace_tps25750_irq directly from tps25750 interrupt handler - PATCH 13-14: Add tps6598x to the subject v6: - PATCH 1: Use reg property for patch address - PATCH 2: Use tps6598x_exec_cmd as a wrapper - PATCH 3: Return current mode and check it directly - PATCH 4: - Don't check VID for tps25750 as the VID register doesn't exist - Remove is_tps25750 flag from tps6598x struct - Get patch address from reg property - PATCH 5: Update eeprom macro to use TPS instead - PATCH 6: No changes - PATCH 7: Check tps25750 using is_compatiable device node - PATCH 8: Create tipd callbacks factory - PATCH 9: No changes - PATCH 10: Add port registration to tipd data factory - PATCH 11: Use tps25750_init instead of tps25750_apply_patch in resume as it initializes sleep mode - PATCH 12: Add trace irq to tipd callbacks factory - PATCH 13: Add trace power status to tipd data factory - PATCH 14: Add trace status to tipd data factory v5: - PATCH 1: Add tps25750 bindings to tps6598x - PATCH 2: Remove tps25750 driver and incorperate tps25750 into tps6598x driver - PATCH [3..15]: Incorporating tps25750 into tps6598x driver v4: - PATCH 1: No change - PATCH 2: Fix comments style and drop of_match_ptr v3: - PATCH 1: Fix node name - PATCH 2: Upload tps25750 driver patch v2: - PATCH 1: General properties clean up Abdel Alkuor (14): dt-bindings: usb: tps6598x: Add tps25750 USB: typec: tsp6598x: Add cmd timeout and response delay USB: typec: tps6598x: Add patch mode to tps6598x USB: typec: tps6598x: Load TPS25750 patch bundle USB: typec: tps6598x: Check for EEPROM present USB: typec: tps6598x: Clear dead battery flag USB: typec: tps6598x: Apply patch again after power resume USB: typec: tps6598x: Add interrupt support for TPS25750 USB: typec: tps6598x: Refactor tps6598x port registration USB: typec: tps6598x: Add port registration for tps25750 USB: typec: tps6598x: Enable sleep mode for tps25750 USB: typec: tps6598x: Add trace for tps25750 irq USB: typec: tps6598x: Add power status trace for tps25750 USB: typec: tps6598x: Add status trace for tps25750 .../devicetree/bindings/usb/ti,tps6598x.yaml | 81 ++- drivers/usb/typec/tipd/core.c | 637 +++++++++++++++--- drivers/usb/typec/tipd/tps6598x.h | 36 + drivers/usb/typec/tipd/trace.h | 92 +++ 4 files changed, 770 insertions(+), 76 deletions(-)