123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
- * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/power_supply.h>
- #include <linux/regmap.h>
- #include <linux/i2c.h>
- #include <linux/mutex.h>
- #include <linux/usb/typec.h>
- #include <linux/usb/ucsi_glink.h>
- #include <linux/soc/qcom/fsa4480-i2c.h>
- #include <linux/iio/consumer.h>
- #include <linux/qti-regmap-debugfs.h>
- #define FSA4480_I2C_NAME "fsa4480-driver"
- #define FSA4480_SWITCH_SETTINGS 0x04
- #define FSA4480_SWITCH_CONTROL 0x05
- #define FSA4480_SWITCH_STATUS1 0x07
- #define FSA4480_SLOW_L 0x08
- #define FSA4480_SLOW_R 0x09
- #define FSA4480_SLOW_MIC 0x0A
- #define FSA4480_SLOW_SENSE 0x0B
- #define FSA4480_SLOW_GND 0x0C
- #define FSA4480_DELAY_L_R 0x0D
- #define FSA4480_DELAY_L_MIC 0x0E
- #define FSA4480_DELAY_L_SENSE 0x0F
- #define FSA4480_DELAY_L_AGND 0x10
- #define FSA4480_RESET 0x1E
- struct fsa4480_priv {
- struct regmap *regmap;
- struct device *dev;
- struct power_supply *usb_psy;
- struct notifier_block nb;
- struct iio_channel *iio_ch;
- atomic_t usbc_mode;
- struct work_struct usbc_analog_work;
- struct blocking_notifier_head fsa4480_notifier;
- struct mutex notification_lock;
- u32 use_powersupply;
- };
- struct fsa4480_reg_val {
- u16 reg;
- u8 val;
- };
- static const struct regmap_config fsa4480_regmap_config = {
- .reg_bits = 8,
- .val_bits = 8,
- .max_register = FSA4480_RESET,
- };
- static const struct fsa4480_reg_val fsa_reg_i2c_defaults[] = {
- {FSA4480_SLOW_L, 0x00},
- {FSA4480_SLOW_R, 0x00},
- {FSA4480_SLOW_MIC, 0x00},
- {FSA4480_SLOW_SENSE, 0x00},
- {FSA4480_SLOW_GND, 0x00},
- {FSA4480_DELAY_L_R, 0x00},
- {FSA4480_DELAY_L_MIC, 0x00},
- {FSA4480_DELAY_L_SENSE, 0x00},
- {FSA4480_DELAY_L_AGND, 0x09},
- {FSA4480_SWITCH_SETTINGS, 0x98},
- };
- static void fsa4480_usbc_update_settings(struct fsa4480_priv *fsa_priv,
- u32 switch_control, u32 switch_enable)
- {
- u32 prev_control, prev_enable;
- if (!fsa_priv->regmap) {
- dev_err(fsa_priv->dev, "%s: regmap invalid\n", __func__);
- return;
- }
- regmap_read(fsa_priv->regmap, FSA4480_SWITCH_CONTROL, &prev_control);
- regmap_read(fsa_priv->regmap, FSA4480_SWITCH_SETTINGS, &prev_enable);
- if (prev_control == switch_control && prev_enable == switch_enable) {
- dev_dbg(fsa_priv->dev, "%s: settings unchanged\n", __func__);
- return;
- }
- regmap_write(fsa_priv->regmap, FSA4480_SWITCH_SETTINGS, 0x80);
- regmap_write(fsa_priv->regmap, FSA4480_SWITCH_CONTROL, switch_control);
- /* FSA4480 chip hardware requirement */
- usleep_range(50, 55);
- regmap_write(fsa_priv->regmap, FSA4480_SWITCH_SETTINGS, switch_enable);
- }
- static int fsa4480_usbc_event_changed_psupply(struct fsa4480_priv *fsa_priv,
- unsigned long evt, void *ptr)
- {
- struct device *dev = NULL;
- if (!fsa_priv)
- return -EINVAL;
- dev = fsa_priv->dev;
- if (!dev)
- return -EINVAL;
- dev_dbg(dev, "%s: queueing usbc_analog_work\n",
- __func__);
- pm_stay_awake(fsa_priv->dev);
- queue_work(system_freezable_wq, &fsa_priv->usbc_analog_work);
- return 0;
- }
- static int fsa4480_usbc_event_changed_ucsi(struct fsa4480_priv *fsa_priv,
- unsigned long evt, void *ptr)
- {
- struct device *dev;
- enum typec_accessory acc = ((struct ucsi_glink_constat_info *)ptr)->acc;
- if (!fsa_priv)
- return -EINVAL;
- dev = fsa_priv->dev;
- if (!dev)
- return -EINVAL;
- dev_dbg(dev, "%s: USB change event received, supply mode %d, usbc mode %ld, expected %d\n",
- __func__, acc, fsa_priv->usbc_mode.counter,
- TYPEC_ACCESSORY_AUDIO);
- switch (acc) {
- case TYPEC_ACCESSORY_AUDIO:
- case TYPEC_ACCESSORY_NONE:
- if (atomic_read(&(fsa_priv->usbc_mode)) == acc)
- break; /* filter notifications received before */
- atomic_set(&(fsa_priv->usbc_mode), acc);
- dev_dbg(dev, "%s: queueing usbc_analog_work\n",
- __func__);
- pm_stay_awake(fsa_priv->dev);
- queue_work(system_freezable_wq, &fsa_priv->usbc_analog_work);
- break;
- default:
- break;
- }
- return 0;
- }
- static int fsa4480_usbc_event_changed(struct notifier_block *nb_ptr,
- unsigned long evt, void *ptr)
- {
- struct fsa4480_priv *fsa_priv =
- container_of(nb_ptr, struct fsa4480_priv, nb);
- struct device *dev;
- if (!fsa_priv)
- return -EINVAL;
- dev = fsa_priv->dev;
- if (!dev)
- return -EINVAL;
- if (fsa_priv->use_powersupply)
- return fsa4480_usbc_event_changed_psupply(fsa_priv, evt, ptr);
- else
- return fsa4480_usbc_event_changed_ucsi(fsa_priv, evt, ptr);
- }
- static int fsa4480_usbc_analog_setup_switches_psupply(
- struct fsa4480_priv *fsa_priv)
- {
- int rc = 0;
- union power_supply_propval mode;
- struct device *dev;
- if (!fsa_priv)
- return -EINVAL;
- dev = fsa_priv->dev;
- if (!dev)
- return -EINVAL;
- rc = iio_read_channel_processed(fsa_priv->iio_ch, &mode.intval);
- mutex_lock(&fsa_priv->notification_lock);
- /* get latest mode again within locked context */
- if (rc < 0) {
- dev_err(dev, "%s: Unable to read USB TYPEC_MODE: %d\n",
- __func__, rc);
- goto done;
- }
- dev_dbg(dev, "%s: setting GPIOs active = %d rcvd intval 0x%X\n",
- __func__, mode.intval != TYPEC_ACCESSORY_NONE, mode.intval);
- atomic_set(&(fsa_priv->usbc_mode), mode.intval);
- switch (mode.intval) {
- /* add all modes FSA should notify for in here */
- case TYPEC_ACCESSORY_AUDIO:
- /* activate switches */
- fsa4480_usbc_update_settings(fsa_priv, 0x00, 0x9F);
- /* notify call chain on event */
- blocking_notifier_call_chain(&fsa_priv->fsa4480_notifier,
- mode.intval, NULL);
- break;
- case TYPEC_ACCESSORY_NONE:
- /* notify call chain on event */
- blocking_notifier_call_chain(&fsa_priv->fsa4480_notifier,
- TYPEC_ACCESSORY_NONE, NULL);
- /* deactivate switches */
- fsa4480_usbc_update_settings(fsa_priv, 0x18, 0x98);
- break;
- default:
- /* ignore other usb connection modes */
- break;
- }
- done:
- mutex_unlock(&fsa_priv->notification_lock);
- return rc;
- }
- static int fsa4480_usbc_analog_setup_switches_ucsi(
- struct fsa4480_priv *fsa_priv)
- {
- int rc = 0;
- int mode;
- struct device *dev;
- if (!fsa_priv)
- return -EINVAL;
- dev = fsa_priv->dev;
- if (!dev)
- return -EINVAL;
- mutex_lock(&fsa_priv->notification_lock);
- /* get latest mode again within locked context */
- mode = atomic_read(&(fsa_priv->usbc_mode));
- dev_dbg(dev, "%s: setting GPIOs active = %d\n",
- __func__, mode != TYPEC_ACCESSORY_NONE);
- switch (mode) {
- /* add all modes FSA should notify for in here */
- case TYPEC_ACCESSORY_AUDIO:
- /* activate switches */
- fsa4480_usbc_update_settings(fsa_priv, 0x00, 0x9F);
- /* notify call chain on event */
- blocking_notifier_call_chain(&fsa_priv->fsa4480_notifier,
- mode, NULL);
- break;
- case TYPEC_ACCESSORY_NONE:
- /* notify call chain on event */
- blocking_notifier_call_chain(&fsa_priv->fsa4480_notifier,
- TYPEC_ACCESSORY_NONE, NULL);
- /* deactivate switches */
- fsa4480_usbc_update_settings(fsa_priv, 0x18, 0x98);
- break;
- default:
- /* ignore other usb connection modes */
- break;
- }
- mutex_unlock(&fsa_priv->notification_lock);
- return rc;
- }
- static int fsa4480_usbc_analog_setup_switches(struct fsa4480_priv *fsa_priv)
- {
- if (fsa_priv->use_powersupply)
- return fsa4480_usbc_analog_setup_switches_psupply(fsa_priv);
- else
- return fsa4480_usbc_analog_setup_switches_ucsi(fsa_priv);
- }
- /*
- * fsa4480_reg_notifier - register notifier block with fsa driver
- *
- * @nb - notifier block of fsa4480
- * @node - phandle node to fsa4480 device
- *
- * Returns 0 on success, or error code
- */
- int fsa4480_reg_notifier(struct notifier_block *nb,
- struct device_node *node)
- {
- int rc = 0;
- struct i2c_client *client = of_find_i2c_device_by_node(node);
- struct fsa4480_priv *fsa_priv;
- if (!client)
- return -EINVAL;
- fsa_priv = (struct fsa4480_priv *)i2c_get_clientdata(client);
- if (!fsa_priv)
- return -EINVAL;
- rc = blocking_notifier_chain_register
- (&fsa_priv->fsa4480_notifier, nb);
- dev_dbg(fsa_priv->dev, "%s: registered notifier for %s\n",
- __func__, node->name);
- if (rc)
- return rc;
- /*
- * as part of the init sequence check if there is a connected
- * USB C analog adapter
- */
- if (atomic_read(&(fsa_priv->usbc_mode)) == TYPEC_ACCESSORY_AUDIO) {
- dev_dbg(fsa_priv->dev, "%s: analog adapter already inserted\n",
- __func__);
- rc = fsa4480_usbc_analog_setup_switches(fsa_priv);
- }
- return rc;
- }
- EXPORT_SYMBOL_GPL(fsa4480_reg_notifier);
- /*
- * fsa4480_unreg_notifier - unregister notifier block with fsa driver
- *
- * @nb - notifier block of fsa4480
- * @node - phandle node to fsa4480 device
- *
- * Returns 0 on pass, or error code
- */
- int fsa4480_unreg_notifier(struct notifier_block *nb,
- struct device_node *node)
- {
- int rc = 0;
- struct i2c_client *client = of_find_i2c_device_by_node(node);
- struct fsa4480_priv *fsa_priv;
- struct device *dev;
- union power_supply_propval mode;
- if (!client)
- return -EINVAL;
- fsa_priv = (struct fsa4480_priv *)i2c_get_clientdata(client);
- if (!fsa_priv)
- return -EINVAL;
- if (fsa_priv->use_powersupply) {
- dev = fsa_priv->dev;
- if (!dev)
- return -EINVAL;
- mutex_lock(&fsa_priv->notification_lock);
- /* get latest mode within locked context */
- rc = iio_read_channel_processed(fsa_priv->iio_ch, &mode.intval);
- if (rc < 0) {
- dev_dbg(dev, "%s: Unable to read USB TYPEC_MODE: %d\n",
- __func__, rc);
- mutex_unlock(&fsa_priv->notification_lock);
- return rc;
- }
- /* Do not reset switch settings for usb digital hs */
- if (mode.intval == TYPEC_ACCESSORY_AUDIO)
- fsa4480_usbc_update_settings(fsa_priv, 0x18, 0x98);
- rc = blocking_notifier_chain_unregister
- (&fsa_priv->fsa4480_notifier, nb);
- mutex_unlock(&fsa_priv->notification_lock);
- } else {
- fsa4480_usbc_update_settings(fsa_priv, 0x18, 0x98);
- rc = blocking_notifier_chain_unregister
- (&fsa_priv->fsa4480_notifier, nb);
- }
- return rc;
- }
- EXPORT_SYMBOL_GPL(fsa4480_unreg_notifier);
- static int fsa4480_validate_display_port_settings(struct fsa4480_priv *fsa_priv)
- {
- u32 switch_status = 0;
- regmap_read(fsa_priv->regmap, FSA4480_SWITCH_STATUS1, &switch_status);
- if ((switch_status != 0x23) && (switch_status != 0x1C)) {
- pr_err("AUX SBU1/2 switch status is invalid = %u\n",
- switch_status);
- return -EIO;
- }
- return 0;
- }
- /*
- * fsa4480_switch_event - configure FSA switch position based on event
- *
- * @node - phandle node to fsa4480 device
- * @event - fsa_function enum
- *
- * Returns int on whether the switch happened or not
- */
- int fsa4480_switch_event(struct device_node *node,
- enum fsa_function event)
- {
- int switch_control = 0;
- struct i2c_client *client = of_find_i2c_device_by_node(node);
- struct fsa4480_priv *fsa_priv;
- if (!client)
- return -EINVAL;
- fsa_priv = (struct fsa4480_priv *)i2c_get_clientdata(client);
- if (!fsa_priv)
- return -EINVAL;
- if (!fsa_priv->regmap)
- return -EINVAL;
- switch (event) {
- case FSA_MIC_GND_SWAP:
- regmap_read(fsa_priv->regmap, FSA4480_SWITCH_CONTROL,
- &switch_control);
- if ((switch_control & 0x07) == 0x07)
- switch_control = 0x0;
- else
- switch_control = 0x7;
- fsa4480_usbc_update_settings(fsa_priv, switch_control, 0x9F);
- break;
- case FSA_USBC_ORIENTATION_CC1:
- fsa4480_usbc_update_settings(fsa_priv, 0x18, 0xF8);
- return fsa4480_validate_display_port_settings(fsa_priv);
- case FSA_USBC_ORIENTATION_CC2:
- fsa4480_usbc_update_settings(fsa_priv, 0x78, 0xF8);
- return fsa4480_validate_display_port_settings(fsa_priv);
- case FSA_USBC_DISPLAYPORT_DISCONNECTED:
- fsa4480_usbc_update_settings(fsa_priv, 0x18, 0x98);
- break;
- default:
- break;
- }
- return 0;
- }
- EXPORT_SYMBOL_GPL(fsa4480_switch_event);
- static void fsa4480_usbc_analog_work_fn(struct work_struct *work)
- {
- struct fsa4480_priv *fsa_priv =
- container_of(work, struct fsa4480_priv, usbc_analog_work);
- if (!fsa_priv) {
- pr_err("%s: fsa container invalid\n", __func__);
- return;
- }
- fsa4480_usbc_analog_setup_switches(fsa_priv);
- pm_relax(fsa_priv->dev);
- }
- static void fsa4480_update_reg_defaults(struct regmap *regmap)
- {
- u8 i;
- for (i = 0; i < ARRAY_SIZE(fsa_reg_i2c_defaults); i++)
- regmap_write(regmap, fsa_reg_i2c_defaults[i].reg,
- fsa_reg_i2c_defaults[i].val);
- }
- static int fsa4480_probe(struct i2c_client *i2c,
- const struct i2c_device_id *id)
- {
- struct fsa4480_priv *fsa_priv;
- u32 use_powersupply = 0;
- int rc = 0;
- fsa_priv = devm_kzalloc(&i2c->dev, sizeof(*fsa_priv),
- GFP_KERNEL);
- if (!fsa_priv)
- return -ENOMEM;
- memset(fsa_priv, 0, sizeof(struct fsa4480_priv));
- fsa_priv->dev = &i2c->dev;
- fsa_priv->regmap = devm_regmap_init_i2c(i2c, &fsa4480_regmap_config);
- if (IS_ERR_OR_NULL(fsa_priv->regmap)) {
- dev_err(fsa_priv->dev, "%s: Failed to initialize regmap: %d\n",
- __func__, rc);
- if (!fsa_priv->regmap) {
- rc = -EINVAL;
- goto err_data;
- }
- rc = PTR_ERR(fsa_priv->regmap);
- goto err_data;
- }
- fsa4480_update_reg_defaults(fsa_priv->regmap);
- devm_regmap_qti_debugfs_register(fsa_priv->dev, fsa_priv->regmap);
- fsa_priv->nb.notifier_call = fsa4480_usbc_event_changed;
- fsa_priv->nb.priority = 0;
- rc = of_property_read_u32(fsa_priv->dev->of_node,
- "qcom,use-power-supply", &use_powersupply);
- if (rc || use_powersupply == 0) {
- dev_dbg(fsa_priv->dev,
- "%s: Looking up %s property failed or disabled\n",
- __func__, "qcom,use-power-supply");
- fsa_priv->use_powersupply = 0;
- rc = register_ucsi_glink_notifier(&fsa_priv->nb);
- if (rc) {
- dev_err(fsa_priv->dev,
- "%s: ucsi glink notifier registration failed: %d\n",
- __func__, rc);
- goto err_data;
- }
- } else {
- fsa_priv->use_powersupply = 1;
- fsa_priv->usb_psy = power_supply_get_by_name("usb");
- if (!fsa_priv->usb_psy) {
- rc = -EPROBE_DEFER;
- dev_dbg(fsa_priv->dev,
- "%s: could not get USB psy info: %d\n",
- __func__, rc);
- goto err_data;
- }
- fsa_priv->iio_ch = iio_channel_get(fsa_priv->dev, "typec_mode");
- if (!fsa_priv->iio_ch) {
- dev_err(fsa_priv->dev,
- "%s: iio_channel_get failed for typec_mode\n",
- __func__);
- goto err_supply;
- }
- rc = power_supply_reg_notifier(&fsa_priv->nb);
- if (rc) {
- dev_err(fsa_priv->dev,
- "%s: power supply reg failed: %d\n",
- __func__, rc);
- goto err_supply;
- }
- }
- mutex_init(&fsa_priv->notification_lock);
- i2c_set_clientdata(i2c, fsa_priv);
- INIT_WORK(&fsa_priv->usbc_analog_work,
- fsa4480_usbc_analog_work_fn);
- BLOCKING_INIT_NOTIFIER_HEAD(&fsa_priv->fsa4480_notifier);
- return 0;
- err_supply:
- power_supply_put(fsa_priv->usb_psy);
- err_data:
- return rc;
- }
- static void fsa4480_remove(struct i2c_client *i2c)
- {
- struct fsa4480_priv *fsa_priv =
- (struct fsa4480_priv *)i2c_get_clientdata(i2c);
- if (!fsa_priv)
- return;
- if (fsa_priv->use_powersupply) {
- /* deregister from PMI */
- power_supply_unreg_notifier(&fsa_priv->nb);
- power_supply_put(fsa_priv->usb_psy);
- } else {
- unregister_ucsi_glink_notifier(&fsa_priv->nb);
- }
- fsa4480_usbc_update_settings(fsa_priv, 0x18, 0x98);
- cancel_work_sync(&fsa_priv->usbc_analog_work);
- pm_relax(fsa_priv->dev);
- mutex_destroy(&fsa_priv->notification_lock);
- dev_set_drvdata(&i2c->dev, NULL);
- }
- static const struct of_device_id fsa4480_i2c_dt_match[] = {
- {
- .compatible = "qcom,fsa4480-i2c",
- },
- {}
- };
- static struct i2c_driver fsa4480_i2c_driver = {
- .driver = {
- .name = FSA4480_I2C_NAME,
- .of_match_table = fsa4480_i2c_dt_match,
- .probe_type = PROBE_PREFER_ASYNCHRONOUS,
- },
- .probe = fsa4480_probe,
- .remove = fsa4480_remove,
- };
- static int __init fsa4480_init(void)
- {
- int rc;
- rc = i2c_add_driver(&fsa4480_i2c_driver);
- if (rc)
- pr_err("fsa4480: Failed to register I2C driver: %d\n", rc);
- return rc;
- }
- module_init(fsa4480_init);
- static void __exit fsa4480_exit(void)
- {
- i2c_del_driver(&fsa4480_i2c_driver);
- }
- module_exit(fsa4480_exit);
- MODULE_DESCRIPTION("FSA4480 I2C driver");
- MODULE_LICENSE("GPL");
|