123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2013, Sony Mobile Communications AB.
- * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
- * Copyright (c) 2023-2024, Qualcomm Innovation Center, Inc. All rights reserved.
- */
- #include <linux/delay.h>
- #include <linux/err.h>
- #include <linux/io.h>
- #include <linux/module.h>
- #include <linux/of.h>
- #include <linux/platform_device.h>
- #include <linux/pinctrl/machine.h>
- #include <linux/pinctrl/pinctrl.h>
- #include <linux/pinctrl/pinmux.h>
- #include <linux/pinctrl/pinconf.h>
- #include <linux/pinctrl/pinconf-generic.h>
- #include <linux/slab.h>
- #include <linux/gpio/driver.h>
- #include <linux/interrupt.h>
- #include <linux/spinlock.h>
- #include <linux/reboot.h>
- #include <linux/pm.h>
- #include <linux/log2.h>
- #include <linux/qcom_scm.h>
- #include <linux/soc/qcom/irq.h>
- #include <linux/bitmap.h>
- #include <linux/sizes.h>
- #include <linux/suspend.h>
- #include <linux/notifier.h>
- #include <linux/syscore_ops.h>
- #include "../core.h"
- #include "../pinconf.h"
- #include "pinctrl-msm.h"
- #include "../pinctrl-utils.h"
- #if IS_ENABLED(CONFIG_SEC_GPIO_DVS)
- #include "secgpio_dvs.h"
- #endif
- #define MAX_NR_GPIO 300
- #define MAX_NR_TILES 4
- #define DEFAULT_REG_SIZE_4K 1
- #define PS_HOLD_OFFSET 0x820
- #define QUP_MASK GENMASK(5, 0)
- #define SPARE_MASK GENMASK(15, 8)
- /**
- * struct msm_pinctrl - state for a pinctrl-msm device
- * @dev: device handle.
- * @pctrl: pinctrl handle.
- * @chip: gpiochip handle.
- * @desc: pin controller descriptor
- * @restart_nb: restart notifier block.
- * @irq: parent irq for the TLMM irq_chip.
- * @n_dir_conns: The number of pins directly connected to GIC.
- * @intr_target_use_scm: route irq to application cpu using scm calls
- * @lock: Spinlock to protect register resources as well
- * as msm_pinctrl data structures.
- * @enabled_irqs: Bitmap of currently enabled irqs.
- * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
- * detection.
- * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
- * @disabled_for_mux: These IRQs were disabled because we muxed away.
- * @ever_gpio: This bit is set the first time we mux a pin to gpio_func.
- * @soc: Reference to soc_data of platform specific data.
- * @regs: Base addresses for the TLMM tiles.
- * @phys_base: Physical base address
- */
- struct msm_pinctrl {
- struct device *dev;
- struct pinctrl_dev *pctrl;
- struct gpio_chip chip;
- struct pinctrl_desc desc;
- struct notifier_block restart_nb;
- int irq;
- int n_dir_conns;
- bool intr_target_use_scm;
- raw_spinlock_t lock;
- DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
- DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
- DECLARE_BITMAP(skip_wake_irqs, MAX_NR_GPIO);
- DECLARE_BITMAP(disabled_for_mux, MAX_NR_GPIO);
- DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
- const struct msm_pinctrl_soc_data *soc;
- void __iomem *regs[MAX_NR_TILES];
- u32 phys_base[MAX_NR_TILES];
- struct msm_gpio_regs *gpio_regs;
- struct msm_tile *msm_tile_regs;
- bool hibernation;
- };
- static struct msm_pinctrl *msm_pinctrl_data;
- static bool pinctrl_msm_log_mask;
- #define EGPIO_PRESENT 11
- #define EGPIO_ENABLE 12
- #define I2C_PULL 13
- #define MSM_APPS_OWNER 1
- #define MSM_REMOTE_OWNER 0
- /* custom pinconf parameters for msm pinictrl*/
- #define MSM_PIN_CONFIG_APPS (PIN_CONFIG_END + 1)
- #define MSM_PIN_CONFIG_REMOTE (PIN_CONFIG_END + 2)
- #define MSM_PIN_CONFIG_I2C_PULL (PIN_CONFIG_END + 3)
- static const struct pinconf_generic_params msm_gpio_bindings[] = {
- {"qcom,apps", MSM_PIN_CONFIG_APPS, 0},
- {"qcom,remote", MSM_PIN_CONFIG_REMOTE, 0},
- {"qcom,i2c_pull", MSM_PIN_CONFIG_I2C_PULL, 0},
- };
- static const char * const pulls_keeper[] = {
- "no pull",
- "pull down",
- "keeper",
- "pull up"
- };
- static const char * const pulls_no_keeper[] = {
- "no pull",
- "pull down",
- "pull up",
- };
- #define MSM_ACCESSOR(name) \
- static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
- const struct msm_pingroup *g) \
- { \
- return readl(pctrl->regs[g->tile] + g->name##_reg); \
- } \
- static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
- const struct msm_pingroup *g) \
- { \
- writel(val, pctrl->regs[g->tile] + g->name##_reg); \
- }
- MSM_ACCESSOR(ctl)
- MSM_ACCESSOR(io)
- MSM_ACCESSOR(intr_cfg)
- MSM_ACCESSOR(intr_status)
- MSM_ACCESSOR(intr_target)
- struct msm_gpio_regs {
- u32 ctl_reg;
- u32 io_reg;
- u32 intr_cfg_reg;
- u32 intr_status_reg;
- };
- struct msm_tile {
- u32 dir_con_regs[8];
- };
- static void msm_ack_intr_status(struct msm_pinctrl *pctrl,
- const struct msm_pingroup *g)
- {
- u32 val = g->intr_ack_high ? BIT(g->intr_status_bit) : 0;
- msm_writel_intr_status(val, pctrl, g);
- }
- static int msm_get_groups_count(struct pinctrl_dev *pctldev)
- {
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- return pctrl->soc->ngroups;
- }
- static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
- unsigned group)
- {
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- return pctrl->soc->groups[group].name;
- }
- static int msm_get_group_pins(struct pinctrl_dev *pctldev,
- unsigned group,
- const unsigned **pins,
- unsigned *num_pins)
- {
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- *pins = pctrl->soc->groups[group].pins;
- *num_pins = pctrl->soc->groups[group].npins;
- return 0;
- }
- static const struct pinctrl_ops msm_pinctrl_ops = {
- .get_groups_count = msm_get_groups_count,
- .get_group_name = msm_get_group_name,
- .get_group_pins = msm_get_group_pins,
- .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
- .dt_free_map = pinctrl_utils_free_map,
- };
- static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
- {
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- struct gpio_chip *chip = &pctrl->chip;
- return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
- }
- static int msm_get_functions_count(struct pinctrl_dev *pctldev)
- {
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- return pctrl->soc->nfunctions;
- }
- static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
- unsigned function)
- {
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- return pctrl->soc->functions[function].name;
- }
- static int msm_get_function_groups(struct pinctrl_dev *pctldev,
- unsigned function,
- const char * const **groups,
- unsigned * const num_groups)
- {
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- *groups = pctrl->soc->functions[function].groups;
- *num_groups = pctrl->soc->functions[function].ngroups;
- return 0;
- }
- static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
- unsigned function,
- unsigned group)
- {
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- struct gpio_chip *gc = &pctrl->chip;
- const struct msm_pinctrl_soc_data *ps = pctrl->soc;
- unsigned int irq = irq_find_mapping(gc->irq.domain, group);
- struct irq_data *d = irq_get_irq_data(irq);
- unsigned int gpio_func = pctrl->soc->gpio_func;
- unsigned int egpio_func = pctrl->soc->egpio_func;
- const struct msm_pingroup *g;
- unsigned long flags;
- u32 val, mask;
- int i;
- g = &pctrl->soc->groups[group];
- mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
- for (i = 0; i < g->nfuncs; i++) {
- if (g->funcs[i] == function)
- break;
- }
- if (WARN_ON(i == g->nfuncs))
- return -EINVAL;
- /*
- * If an GPIO interrupt is setup on this pin then we need special
- * handling. Specifically interrupt detection logic will still see
- * the pin twiddle even when we're muxed away.
- *
- * When we see a pin with an interrupt setup on it then we'll disable
- * (mask) interrupts on it when we mux away until we mux back. Note
- * that disable_irq() refcounts and interrupts are disabled as long as
- * at least one disable_irq() has been called.
- */
- if (d && i != gpio_func &&
- !test_and_set_bit(d->hwirq, pctrl->disabled_for_mux))
- disable_irq(irq);
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- val = msm_readl_ctl(pctrl, g);
- /*
- * If this is the first time muxing to GPIO and the direction is
- * output, make sure that we're not going to be glitching the pin
- * by reading the current state of the pin and setting it as the
- * output.
- */
- if (i == gpio_func && (val & BIT(g->oe_bit)) &&
- !test_and_set_bit(group, pctrl->ever_gpio)) {
- u32 io_val = msm_readl_io(pctrl, g);
- if (io_val & BIT(g->in_bit)) {
- if (!(io_val & BIT(g->out_bit)))
- msm_writel_io(io_val | BIT(g->out_bit), pctrl, g);
- } else {
- if (io_val & BIT(g->out_bit))
- msm_writel_io(io_val & ~BIT(g->out_bit), pctrl, g);
- }
- }
- if (egpio_func && i == egpio_func) {
- if (val & BIT(g->egpio_present))
- val &= ~BIT(g->egpio_enable);
- } else {
- val &= ~mask;
- val |= i << g->mux_bit;
- /* Claim ownership of pin if egpio capable and
- * also need check if setting NA in funcs.
- */
- if (egpio_func && val & BIT(g->egpio_present)) {
- if (g->funcs[egpio_func] != ps->nfunctions)
- val |= BIT(g->egpio_enable);
- }
- }
- msm_writel_ctl(val, pctrl, g);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- if (d && i == gpio_func &&
- test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) {
- /*
- * Clear interrupts detected while not GPIO since we only
- * masked things.
- */
- if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
- irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
- else
- msm_ack_intr_status(pctrl, g);
- enable_irq(irq);
- }
- return 0;
- }
- static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
- struct pinctrl_gpio_range *range,
- unsigned offset)
- {
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- const struct msm_pingroup *g = &pctrl->soc->groups[offset];
- /* No funcs? Probably ACPI so can't do anything here */
- if (!g->nfuncs)
- return 0;
- return msm_pinmux_set_mux(pctldev, g->funcs[pctrl->soc->gpio_func], offset);
- }
- static const struct pinmux_ops msm_pinmux_ops = {
- .request = msm_pinmux_request,
- .get_functions_count = msm_get_functions_count,
- .get_function_name = msm_get_function_name,
- .get_function_groups = msm_get_function_groups,
- .gpio_request_enable = msm_pinmux_request_gpio,
- .set_mux = msm_pinmux_set_mux,
- };
- static int msm_config_reg(struct msm_pinctrl *pctrl,
- const struct msm_pingroup *g,
- unsigned param,
- unsigned *mask,
- unsigned *bit)
- {
- switch (param) {
- case PIN_CONFIG_BIAS_DISABLE:
- case PIN_CONFIG_BIAS_PULL_DOWN:
- case PIN_CONFIG_BIAS_BUS_HOLD:
- case PIN_CONFIG_BIAS_PULL_UP:
- *bit = g->pull_bit;
- *mask = 3;
- break;
- case PIN_CONFIG_DRIVE_OPEN_DRAIN:
- *bit = g->od_bit;
- *mask = 1;
- break;
- case PIN_CONFIG_DRIVE_STRENGTH:
- *bit = g->drv_bit;
- *mask = 7;
- break;
- case PIN_CONFIG_OUTPUT:
- case PIN_CONFIG_INPUT_ENABLE:
- *bit = g->oe_bit;
- *mask = 1;
- break;
- case MSM_PIN_CONFIG_APPS:
- case MSM_PIN_CONFIG_REMOTE:
- *bit = EGPIO_ENABLE;
- *mask = 1;
- break;
- case MSM_PIN_CONFIG_I2C_PULL:
- *bit = I2C_PULL;
- *mask = 1;
- break;
- default:
- return -ENOTSUPP;
- }
- return 0;
- }
- #define MSM_NO_PULL 0
- #define MSM_PULL_DOWN 1
- #define MSM_KEEPER 2
- #define MSM_PULL_UP_NO_KEEPER 2
- #define MSM_PULL_UP 3
- static unsigned msm_regval_to_drive(u32 val)
- {
- return (val + 1) * 2;
- }
- static int msm_config_group_get(struct pinctrl_dev *pctldev,
- unsigned int group,
- unsigned long *config)
- {
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- unsigned param = pinconf_to_config_param(*config);
- unsigned mask;
- unsigned arg;
- unsigned bit;
- int ret;
- u32 val;
- if (!gpiochip_line_is_valid(&msm_pinctrl_data->chip, group))
- return -EINVAL;
- g = &pctrl->soc->groups[group];
- ret = msm_config_reg(pctrl, g, param, &mask, &bit);
- if (ret < 0)
- return ret;
- val = msm_readl_ctl(pctrl, g);
- arg = (val >> bit) & mask;
- /* Convert register value to pinconf value */
- switch (param) {
- case PIN_CONFIG_BIAS_DISABLE:
- if (arg != MSM_NO_PULL)
- return -EINVAL;
- arg = 1;
- break;
- case PIN_CONFIG_BIAS_PULL_DOWN:
- if (arg != MSM_PULL_DOWN)
- return -EINVAL;
- arg = 1;
- break;
- case PIN_CONFIG_BIAS_BUS_HOLD:
- if (pctrl->soc->pull_no_keeper)
- return -ENOTSUPP;
- if (arg != MSM_KEEPER)
- return -EINVAL;
- arg = 1;
- break;
- case PIN_CONFIG_BIAS_PULL_UP:
- if (pctrl->soc->pull_no_keeper)
- arg = arg == MSM_PULL_UP_NO_KEEPER;
- else
- arg = arg == MSM_PULL_UP;
- if (!arg)
- return -EINVAL;
- break;
- case PIN_CONFIG_DRIVE_OPEN_DRAIN:
- /* Pin is not open-drain */
- if (!arg)
- return -EINVAL;
- arg = 1;
- break;
- case PIN_CONFIG_DRIVE_STRENGTH:
- arg = msm_regval_to_drive(arg);
- break;
- case PIN_CONFIG_OUTPUT:
- /* Pin is not output */
- if (!arg)
- return -EINVAL;
- val = msm_readl_io(pctrl, g);
- arg = !!(val & BIT(g->in_bit));
- break;
- case PIN_CONFIG_INPUT_ENABLE:
- /* Pin is output */
- if (arg)
- return -EINVAL;
- arg = 1;
- break;
- case MSM_PIN_CONFIG_APPS:
- /* Pin do not support egpio or remote owner */
- if (!(val & BIT(EGPIO_PRESENT)) || !arg)
- return -EINVAL;
- arg = 1;
- break;
- case MSM_PIN_CONFIG_REMOTE:
- /* Pin do not support epgio or APPS owner */
- if (!(val & BIT(EGPIO_PRESENT)) || arg)
- return -EINVAL;
- arg = 1;
- break;
- case MSM_PIN_CONFIG_I2C_PULL:
- arg = 1;
- break;
- default:
- return -ENOTSUPP;
- }
- *config = pinconf_to_config_packed(param, arg);
- return 0;
- }
- static int msm_config_group_set(struct pinctrl_dev *pctldev,
- unsigned group,
- unsigned long *configs,
- unsigned num_configs)
- {
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
- unsigned long flags;
- unsigned param;
- unsigned mask;
- unsigned arg;
- unsigned bit;
- u32 owner_bit, owner_update = 0;
- int ret;
- u32 val;
- int i;
- g = &pctrl->soc->groups[group];
- for (i = 0; i < num_configs; i++) {
- param = pinconf_to_config_param(configs[i]);
- arg = pinconf_to_config_argument(configs[i]);
- ret = msm_config_reg(pctrl, g, param, &mask, &bit);
- if (ret < 0)
- return ret;
- /* Convert pinconf values to register values */
- switch (param) {
- case PIN_CONFIG_BIAS_DISABLE:
- arg = MSM_NO_PULL;
- break;
- case PIN_CONFIG_BIAS_PULL_DOWN:
- arg = MSM_PULL_DOWN;
- break;
- case PIN_CONFIG_BIAS_BUS_HOLD:
- if (pctrl->soc->pull_no_keeper)
- return -ENOTSUPP;
- arg = MSM_KEEPER;
- break;
- case PIN_CONFIG_BIAS_PULL_UP:
- if (pctrl->soc->pull_no_keeper)
- arg = MSM_PULL_UP_NO_KEEPER;
- else
- arg = MSM_PULL_UP;
- break;
- case PIN_CONFIG_DRIVE_OPEN_DRAIN:
- arg = 1;
- break;
- case PIN_CONFIG_DRIVE_STRENGTH:
- /* Check for invalid values */
- if (arg > 16 || arg < 2 || (arg % 2) != 0)
- arg = -1;
- else
- arg = (arg / 2) - 1;
- break;
- case PIN_CONFIG_OUTPUT:
- /* set output value */
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- val = msm_readl_io(pctrl, g);
- if (arg)
- val |= BIT(g->out_bit);
- else
- val &= ~BIT(g->out_bit);
- msm_writel_io(val, pctrl, g);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- /* enable output */
- arg = 1;
- break;
- case PIN_CONFIG_INPUT_ENABLE:
- /* disable output */
- arg = 0;
- break;
- case MSM_PIN_CONFIG_APPS:
- owner_update = 1;
- owner_bit = MSM_APPS_OWNER;
- break;
- case MSM_PIN_CONFIG_REMOTE:
- owner_update = 1;
- owner_bit = MSM_REMOTE_OWNER;
- break;
- case MSM_PIN_CONFIG_I2C_PULL:
- arg = 1;
- break;
- default:
- dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
- param);
- return -EINVAL;
- }
- /* Range-check user-supplied value */
- if (arg & ~mask) {
- dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
- return -EINVAL;
- }
- /* Update the owner bits as final config update */
- if (param == MSM_PIN_CONFIG_APPS || param == MSM_PIN_CONFIG_REMOTE)
- continue;
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- val = msm_readl_ctl(pctrl, g);
- val &= ~(mask << bit);
- val |= arg << bit;
- msm_writel_ctl(val, pctrl, g);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- }
- if (owner_update) {
- ret = msm_config_reg(pctrl, g,
- MSM_PIN_CONFIG_APPS, &mask, &bit);
- if (ret < 0)
- return ret;
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- val = msm_readl_ctl(pctrl, g);
- if (val & BIT(EGPIO_PRESENT)) {
- val &= ~(mask << bit);
- val |= owner_bit << bit;
- msm_writel_ctl(val, pctrl, g);
- }
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- }
- return 0;
- }
- static const struct pinconf_ops msm_pinconf_ops = {
- .is_generic = true,
- .pin_config_group_get = msm_config_group_get,
- .pin_config_group_set = msm_config_group_set,
- };
- static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
- {
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
- unsigned long flags;
- u32 val;
- g = &pctrl->soc->groups[offset];
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- val = msm_readl_ctl(pctrl, g);
- val &= ~BIT(g->oe_bit);
- msm_writel_ctl(val, pctrl, g);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- return 0;
- }
- static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
- {
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
- unsigned long flags;
- u32 val;
- g = &pctrl->soc->groups[offset];
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- val = msm_readl_io(pctrl, g);
- if (value)
- val |= BIT(g->out_bit);
- else
- val &= ~BIT(g->out_bit);
- msm_writel_io(val, pctrl, g);
- val = msm_readl_ctl(pctrl, g);
- val |= BIT(g->oe_bit);
- msm_writel_ctl(val, pctrl, g);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- return 0;
- }
- static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
- {
- struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
- const struct msm_pingroup *g;
- u32 val;
- g = &pctrl->soc->groups[offset];
- val = msm_readl_ctl(pctrl, g);
- return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT :
- GPIO_LINE_DIRECTION_IN;
- }
- static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
- {
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
- u32 val;
- g = &pctrl->soc->groups[offset];
- val = msm_readl_io(pctrl, g);
- return !!(val & BIT(g->in_bit));
- }
- static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
- {
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
- unsigned long flags;
- u32 val;
- g = &pctrl->soc->groups[offset];
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- val = msm_readl_io(pctrl, g);
- if (value)
- val |= BIT(g->out_bit);
- else
- val &= ~BIT(g->out_bit);
- msm_writel_io(val, pctrl, g);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- }
- static void msm_gpio_pin_status_get(struct msm_pinctrl *pctrl, const struct msm_pingroup *g,
- unsigned int offset, int *is_out, unsigned int *func,
- int *drive, int *pull, int *egpio_enable, int *val)
- {
- u32 ctl_reg, io_reg;
- ctl_reg = msm_readl_ctl(pctrl, g);
- io_reg = msm_readl_io(pctrl, g);
- *is_out = !!(ctl_reg & BIT(g->oe_bit));
- *func = (ctl_reg >> g->mux_bit) & 7;
- *drive = (ctl_reg >> g->drv_bit) & 7;
- *pull = (ctl_reg >> g->pull_bit) & 3;
- *egpio_enable = 0;
- if (pctrl->soc->egpio_func && ctl_reg & BIT(g->egpio_present))
- *egpio_enable = !(ctl_reg & BIT(g->egpio_enable));
- if (*is_out)
- *val = !!(io_reg & BIT(g->out_bit));
- else
- *val = !!(io_reg & BIT(g->in_bit));
- }
- #if IS_ENABLED(CONFIG_SEC_GPIO_DVS) || IS_ENABLED(CONFIG_SEC_GPIO_DUMP)
- #define AP_GPIO_MAX 210
- #define GET_RESULT_GPIO(a, b, c) \
- ((a<<4 & 0xF0) | (b<<1 & 0xE) | (c & 0x1))
- struct gpiomux_setting {
- int func;
- int drv;
- int pull;
- int is_out;
- int val;
- };
- static void msm_gp_get_all(struct gpio_chip *chip, u32 pin_no, struct gpiomux_setting *set)
- {
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
- u32 ctl_reg, io_reg;
- int drive;
- g = &pctrl->soc->groups[pin_no];
- ctl_reg = msm_readl_ctl(pctrl, g);
- io_reg = msm_readl_io(pctrl, g);
- set->is_out = !!(ctl_reg & BIT(g->oe_bit));
- set->func = (ctl_reg >> g->mux_bit) & 7;
- drive = (ctl_reg >> g->drv_bit) & 7;
- set->drv = msm_regval_to_drive(drive);
- set->pull = (ctl_reg >> g->pull_bit) & 3;
- if (set->is_out)
- set->val = !!(io_reg & BIT(g->out_bit));
- else
- set->val = !!(io_reg & BIT(g->in_bit));
- }
- #endif
- #if IS_ENABLED(CONFIG_SEC_GPIO_DVS)
- /****************************************************************/
- /* Pre-defined variables. (DO NOT CHANGE THIS!!) */
- static unsigned char checkgpiomap_result[GDVS_PHONE_STATUS_MAX][AP_GPIO_MAX];
- static struct gpiomap_result gpiomap_result = {
- .init = checkgpiomap_result[PHONE_INIT],
- .sleep = checkgpiomap_result[PHONE_SLEEP]
- };
- static void msm_check_gpio_status(unsigned char phonestate)
- {
- struct gpio_chip *gp;
- struct gpiomux_setting set;
- int i;
- u8 temp_io = 0, temp_pdpu = 0, temp_lh = 0;
- if (!msm_pinctrl_data) {
- pr_err("pinctrl data is not available\n");
- return;
- }
- gp = &msm_pinctrl_data->chip;
- pr_info("[dvs_%s] state : %s\n", __func__,
- (phonestate == PHONE_INIT) ? "init" : "sleep");
- for (i = 0; i < AP_GPIO_MAX; i++) {
- /* If it is not valid gpio or secure, Shows IN/PD/L */
- if (!gpiochip_line_is_valid(gp, i)) {
- checkgpiomap_result[phonestate][i] =
- GET_RESULT_GPIO(0x1, 0x1, 0x0);
- continue;
- }
- msm_gp_get_all(gp, i, &set);
- if (set.func == 0) {
- if (set.is_out)
- temp_io = 0x02; /* GPIO_OUT */
- else
- temp_io = 0x01; /* GPIO_IN */
- } else
- temp_io = 0x0; /* FUNC */
- switch (set.pull) {
- case MSM_NO_PULL:
- temp_pdpu = 0x00;
- break;
- case MSM_PULL_DOWN:
- temp_pdpu = 0x01;
- break;
- /*
- * gpiodvs is only for samsung,
- * so pull definition could be different from original value.
- * gpiodvs definition: PU(0x2), KEEPER(0x3)
- */
- case MSM_PULL_UP:
- temp_pdpu = 0x02;
- break;
- case MSM_KEEPER:
- temp_pdpu = 0x03;
- break;
- default:
- temp_pdpu = 0x07;
- break;
- }
- temp_lh = set.val;
- checkgpiomap_result[phonestate][i] =
- GET_RESULT_GPIO(temp_io, temp_pdpu, temp_lh);
- }
- pr_info("[dvs_%s]-\n", __func__);
- }
- static struct gpio_dvs_t msm_gpio_dvs = {
- .result = &gpiomap_result,
- .check_gpio_status = msm_check_gpio_status,
- .count = AP_GPIO_MAX,
- .check_sleep = false,
- };
- const struct secgpio_dvs_data msm_gpio_dvs_data = {
- .gpio_dvs = &msm_gpio_dvs,
- };
- EXPORT_SYMBOL_GPL(msm_gpio_dvs_data);
- #endif /* CONFIG_SEC_GPIO_DVS */
- #if IS_ENABLED(CONFIG_SEC_GPIO_DUMP)
- static const char * const gpiomux_func_str[] = {
- "GPIO",
- "Func_1",
- "Func_2",
- "Func_3",
- "Func_4",
- "Func_5",
- "Func_6",
- "Func_7",
- "Func_8",
- "Func_9",
- "Func_a",
- "Func_b",
- "Func_c",
- "Func_d",
- "Func_e",
- "Func_f",
- };
- static const char * const gpiomux_pull_str[] = {
- "PULL_NONE",
- "PULL_DOWN",
- "PULL_KEEPER",
- "PULL_UP",
- };
- static const char * const gpiomux_dir_str[] = {
- "IN",
- "OUT",
- };
- static const char * const gpiomux_val_str[] = {
- "LOW",
- "HIGH",
- };
- void sec_ap_gpio_debug_print(void)
- {
- struct gpio_chip *gp;
- struct gpiomux_setting set = {0,};
- int i;
- if (!msm_pinctrl_data) {
- pr_err("pinctrl data is not available\n");
- return;
- }
- gp = &msm_pinctrl_data->chip;
- for (i = 0; i < AP_GPIO_MAX; i++) {
- if (!gpiochip_line_is_valid(gp, i))
- continue;
- msm_gp_get_all(gp, i, &set);
- pr_info("GPIO[%u] %10s %10s %13s DRV_%dmA %10s\n",
- i,
- gpiomux_func_str[set.func],
- gpiomux_dir_str[set.is_out],
- gpiomux_pull_str[set.pull],
- set.drv,
- gpiomux_val_str[set.val]);
- }
- }
- EXPORT_SYMBOL(sec_ap_gpio_debug_print);
- #endif /* CONFIG_SEC_GPIO_DUMP */
- #ifdef CONFIG_DEBUG_FS
- #include <linux/seq_file.h>
- static void msm_gpio_dbg_show_one(struct seq_file *s,
- struct pinctrl_dev *pctldev,
- struct gpio_chip *chip,
- unsigned offset,
- unsigned gpio)
- {
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
- unsigned func;
- int is_out;
- int drive;
- int pull;
- int val;
- int egpio_enable;
- if (!gpiochip_line_is_valid(chip, offset))
- return;
- g = &pctrl->soc->groups[offset];
- msm_gpio_pin_status_get(pctrl, g, offset, &is_out, &func,
- &drive, &pull, &egpio_enable, &val);
- if (egpio_enable) {
- seq_printf(s, " %-8s: egpio\n", g->name);
- return;
- }
- seq_printf(s, " %-8s: %-3s", g->name, is_out ? "out" : "in");
- seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
- seq_printf(s, " %dmA", msm_regval_to_drive(drive));
- if (pctrl->soc->pull_no_keeper)
- seq_printf(s, " %s", pulls_no_keeper[pull]);
- else
- seq_printf(s, " %s", pulls_keeper[pull]);
- seq_puts(s, "\n");
- }
- static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
- {
- unsigned gpio = chip->base;
- unsigned i;
- for (i = 0; i < chip->ngpio; i++, gpio++)
- msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
- }
- #else
- #define msm_gpio_dbg_show NULL
- #endif
- static void msm_gpio_log_pin_status(struct gpio_chip *chip, unsigned int offset)
- {
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
- unsigned int func;
- int is_out;
- int drive;
- int pull;
- int val;
- int egpio_enable;
- if (!gpiochip_line_is_valid(chip, offset))
- return;
- g = &pctrl->soc->groups[offset];
- msm_gpio_pin_status_get(pctrl, g, offset, &is_out, &func,
- &drive, &pull, &egpio_enable, &val);
- printk_deferred("%s: %s, %s, func%d, %dmA, %s\n",
- g->name, is_out ? "out" : "in",
- val ? "high" : "low", func,
- msm_regval_to_drive(drive),
- pctrl->soc->pull_no_keeper ? pulls_no_keeper[pull] : pulls_keeper[pull]);
- }
- static void msm_gpios_status(struct gpio_chip *chip)
- {
- unsigned int i;
- for (i = 0; i < chip->ngpio; i++)
- msm_gpio_log_pin_status(chip, i);
- }
- static int msm_gpio_init_valid_mask(struct gpio_chip *gc,
- unsigned long *valid_mask,
- unsigned int ngpios)
- {
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- int ret;
- unsigned int len, i;
- const int *reserved = pctrl->soc->reserved_gpios;
- struct property *prop;
- const __be32 *p;
- u16 *tmp;
- /* Driver provided reserved list overrides DT and ACPI */
- if (reserved) {
- bitmap_fill(valid_mask, ngpios);
- for (i = 0; reserved[i] >= 0; i++) {
- if (i >= ngpios || reserved[i] >= ngpios) {
- dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
- return -EINVAL;
- }
- clear_bit(reserved[i], valid_mask);
- }
- return 0;
- }
- if (of_property_count_u32_elems(pctrl->dev->of_node, "qcom,gpios-reserved") > 0) {
- bitmap_fill(valid_mask, ngpios);
- of_property_for_each_u32(pctrl->dev->of_node, "qcom,gpios-reserved", prop, p, i) {
- if (i >= ngpios) {
- dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
- return -EINVAL;
- }
- clear_bit(i, valid_mask);
- }
- }
- /* The number of GPIOs in the ACPI tables */
- len = ret = device_property_count_u16(pctrl->dev, "gpios");
- if (ret < 0)
- return 0;
- if (ret > ngpios)
- return -EINVAL;
- tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
- if (!tmp)
- return -ENOMEM;
- ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
- if (ret < 0) {
- dev_err(pctrl->dev, "could not read list of GPIOs\n");
- goto out;
- }
- bitmap_zero(valid_mask, ngpios);
- for (i = 0; i < len; i++)
- set_bit(tmp[i], valid_mask);
- out:
- kfree(tmp);
- return ret;
- }
- static const struct gpio_chip msm_gpio_template = {
- .direction_input = msm_gpio_direction_input,
- .direction_output = msm_gpio_direction_output,
- .get_direction = msm_gpio_get_direction,
- .get = msm_gpio_get,
- .set = msm_gpio_set,
- .request = gpiochip_generic_request,
- .free = gpiochip_generic_free,
- .dbg_show = msm_gpio_dbg_show,
- };
- /* For dual-edge interrupts in software, since some hardware has no
- * such support:
- *
- * At appropriate moments, this function may be called to flip the polarity
- * settings of both-edge irq lines to try and catch the next edge.
- *
- * The attempt is considered successful if:
- * - the status bit goes high, indicating that an edge was caught, or
- * - the input value of the gpio doesn't change during the attempt.
- * If the value changes twice during the process, that would cause the first
- * test to fail but would force the second, as two opposite
- * transitions would cause a detection no matter the polarity setting.
- *
- * The do-loop tries to sledge-hammer closed the timing hole between
- * the initial value-read and the polarity-write - if the line value changes
- * during that window, an interrupt is lost, the new polarity setting is
- * incorrect, and the first success test will fail, causing a retry.
- *
- * Algorithm comes from Google's msmgpio driver.
- */
- static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
- const struct msm_pingroup *g,
- struct irq_data *d)
- {
- int loop_limit = 100;
- unsigned val, val2, intstat;
- unsigned pol;
- do {
- val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
- pol = msm_readl_intr_cfg(pctrl, g);
- pol ^= BIT(g->intr_polarity_bit);
- msm_writel_intr_cfg(pol, pctrl, g);
- val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
- intstat = msm_readl_intr_status(pctrl, g);
- if (intstat || (val == val2))
- return;
- } while (loop_limit-- > 0);
- dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
- val, val2);
- }
- static void msm_gpio_irq_mask(struct irq_data *d)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- const struct msm_pingroup *g;
- unsigned long flags;
- u32 val;
- if (d->parent_data)
- irq_chip_mask_parent(d);
- if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
- return;
- g = &pctrl->soc->groups[d->hwirq];
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- val = msm_readl_intr_cfg(pctrl, g);
- /*
- * There are two bits that control interrupt forwarding to the CPU. The
- * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
- * latched into the interrupt status register when the hardware detects
- * an irq that it's configured for (either edge for edge type or level
- * for level type irq). The 'non-raw' status enable bit causes the
- * hardware to assert the summary interrupt to the CPU if the latched
- * status bit is set. There's a bug though, the edge detection logic
- * seems to have a problem where toggling the RAW_STATUS_EN bit may
- * cause the status bit to latch spuriously when there isn't any edge
- * so we can't touch that bit for edge type irqs and we have to keep
- * the bit set anyway so that edges are latched while the line is masked.
- *
- * To make matters more complicated, leaving the RAW_STATUS_EN bit
- * enabled all the time causes level interrupts to re-latch into the
- * status register because the level is still present on the line after
- * we ack it. We clear the raw status enable bit during mask here and
- * set the bit on unmask so the interrupt can't latch into the hardware
- * while it's masked.
- */
- if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
- val &= ~BIT(g->intr_raw_status_bit);
- val &= ~BIT(g->intr_enable_bit);
- msm_writel_intr_cfg(val, pctrl, g);
- clear_bit(d->hwirq, pctrl->enabled_irqs);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- }
- static void msm_gpio_irq_unmask(struct irq_data *d)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- const struct msm_pingroup *g;
- unsigned long flags;
- u32 val;
- if (d->parent_data)
- irq_chip_unmask_parent(d);
- if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
- return;
- g = &pctrl->soc->groups[d->hwirq];
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- val = msm_readl_intr_cfg(pctrl, g);
- val |= BIT(g->intr_raw_status_bit);
- val |= BIT(g->intr_enable_bit);
- msm_writel_intr_cfg(val, pctrl, g);
- set_bit(d->hwirq, pctrl->enabled_irqs);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- }
- static void msm_gpio_dirconn_handler(struct irq_desc *desc)
- {
- struct irq_data *irqd = irq_desc_get_handler_data(desc);
- struct irq_chip *chip = irq_desc_get_chip(desc);
- if (!irqd)
- return;
- chained_irq_enter(chip, desc);
- generic_handle_irq(irqd->irq);
- chained_irq_exit(chip, desc);
- irq_set_irqchip_state(irq_desc_get_irq_data(desc)->irq,
- IRQCHIP_STATE_ACTIVE, 0);
- }
- static void configure_tlmm_dc_polarity(struct irq_data *d,
- unsigned int type, u32 offset)
- {
- const struct msm_pingroup *g;
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- unsigned int polarity = 1, val;
- unsigned long flags;
- if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH)) {
- /*
- * Since the default polarity is set to 0, change it to 1 for
- * Rising edge and active high interrupt type such that the line
- * is not inverted.
- */
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- g = &pctrl->soc->groups[d->hwirq];
- val = readl_relaxed(msm_pinctrl_data->regs[g->tile] +
- g->dir_conn_reg + (offset * 4));
- val |= polarity << 8;
- writel_relaxed(val, msm_pinctrl_data->regs[g->tile] +
- g->dir_conn_reg + (offset * 4));
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- }
- }
- static void enable_tlmm_dc(irq_hw_number_t irq)
- {
- struct irq_data *dir_conn_data = irq_get_irq_data(irq);
- if (!dir_conn_data || !(dir_conn_data->chip))
- return;
- dir_conn_data->chip->irq_unmask(dir_conn_data);
- }
- static bool is_gpio_tlmm_dc(struct irq_data *d, u32 *offset,
- irq_hw_number_t *irq)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- struct msm_dir_conn *dc;
- int i;
- for (i = pctrl->n_dir_conns; i > 0; i--) {
- dc = &pctrl->soc->dir_conn[i];
- *offset = pctrl->n_dir_conns - i;
- *irq = dc->irq;
- if (dc->gpio == d->hwirq)
- return true;
- }
- return false;
- }
- static void msm_gpio_irq_enable(struct irq_data *d)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- gpiochip_enable_irq(gc, d->hwirq);
- if (d->parent_data)
- irq_chip_enable_parent(d);
- if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
- msm_gpio_irq_unmask(d);
- }
- static void msm_gpio_irq_disable(struct irq_data *d)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- if (d->parent_data)
- irq_chip_disable_parent(d);
- if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
- msm_gpio_irq_mask(d);
- gpiochip_disable_irq(gc, d->hwirq);
- }
- /**
- * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent.
- * @d: The irq dta.
- *
- * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are
- * normally handled by the parent irqchip. The logic here is slightly
- * different due to what's easy to do with our parent, but in principle it's
- * the same.
- */
- static void msm_gpio_update_dual_edge_parent(struct irq_data *d)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
- int loop_limit = 100;
- unsigned int val;
- unsigned int type;
- /* Read the value and make a guess about what edge we need to catch */
- val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
- type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
- do {
- /* Set the parent to catch the next edge */
- irq_chip_set_type_parent(d, type);
- /*
- * Possibly the line changed between when we last read "val"
- * (and decided what edge we needed) and when set the edge.
- * If the value didn't change (or changed and then changed
- * back) then we're done.
- */
- val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
- if (type == IRQ_TYPE_EDGE_RISING) {
- if (!val)
- return;
- type = IRQ_TYPE_EDGE_FALLING;
- } else if (type == IRQ_TYPE_EDGE_FALLING) {
- if (val)
- return;
- type = IRQ_TYPE_EDGE_RISING;
- }
- } while (loop_limit-- > 0);
- dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
- }
- static void msm_gpio_irq_ack(struct irq_data *d)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- const struct msm_pingroup *g;
- unsigned long flags;
- if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
- if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
- msm_gpio_update_dual_edge_parent(d);
- return;
- }
- g = &pctrl->soc->groups[d->hwirq];
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- msm_ack_intr_status(pctrl, g);
- if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
- msm_gpio_update_dual_edge_pos(pctrl, g, d);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- }
- static void msm_gpio_irq_eoi(struct irq_data *d)
- {
- d = d->parent_data;
- if (d)
- d->chip->irq_eoi(d);
- }
- static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d,
- unsigned int type)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- return type == IRQ_TYPE_EDGE_BOTH &&
- pctrl->soc->wakeirq_dual_edge_errata && d->parent_data &&
- test_bit(d->hwirq, pctrl->skip_wake_irqs);
- }
- static void msm_dirconn_cfg_reg(struct irq_data *d, u32 offset)
- {
- u32 val;
- const struct msm_pingroup *g;
- unsigned long flags;
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- g = &pctrl->soc->groups[d->hwirq];
- val = (d->hwirq) & 0xFF;
- writel_relaxed(val, pctrl->regs[g->tile] + g->dir_conn_reg
- + (offset * 4));
- val = msm_readl_intr_cfg(pctrl, g);
- val |= BIT(g->dir_conn_en_bit);
- msm_writel_intr_cfg(val, pctrl, g);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- }
- static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- const struct msm_pingroup *g;
- irq_hw_number_t irq = 0;
- u32 intr_target_mask = GENMASK(2, 0);
- unsigned long flags;
- u32 offset = 0;
- bool was_enabled;
- u32 val;
- if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
- set_bit(d->hwirq, pctrl->dual_edge_irqs);
- irq_set_handler_locked(d, handle_fasteoi_ack_irq);
- msm_gpio_update_dual_edge_parent(d);
- return 0;
- }
- if (d->parent_data)
- irq_chip_set_type_parent(d, type);
- if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
- clear_bit(d->hwirq, pctrl->dual_edge_irqs);
- irq_set_handler_locked(d, handle_fasteoi_irq);
- return 0;
- }
- if (is_gpio_tlmm_dc(d, &offset, &irq) && type != IRQ_TYPE_EDGE_BOTH) {
- configure_tlmm_dc_polarity(d, type, offset);
- enable_tlmm_dc(irq);
- if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
- irq_set_handler_locked(d, handle_level_irq);
- else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
- irq_set_handler_locked(d, handle_edge_irq);
- return 0;
- }
- g = &pctrl->soc->groups[d->hwirq];
- raw_spin_lock_irqsave(&pctrl->lock, flags);
- /*
- * For hw without possibility of detecting both edges
- */
- if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
- set_bit(d->hwirq, pctrl->dual_edge_irqs);
- else
- clear_bit(d->hwirq, pctrl->dual_edge_irqs);
- /* Route interrupts to application cpu.
- * With intr_target_use_scm interrupts are routed to
- * application cpu using scm calls.
- */
- if (g->intr_target_width)
- intr_target_mask = GENMASK(g->intr_target_width - 1, 0);
- if (pctrl->intr_target_use_scm) {
- u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
- int ret;
- qcom_scm_io_readl(addr, &val);
- val &= ~(intr_target_mask << g->intr_target_bit);
- val |= g->intr_target_kpss_val << g->intr_target_bit;
- ret = qcom_scm_io_writel(addr, val);
- if (ret)
- dev_err(pctrl->dev,
- "Failed routing %lu interrupt to Apps proc",
- d->hwirq);
- } else {
- val = msm_readl_intr_target(pctrl, g);
- val &= ~(intr_target_mask << g->intr_target_bit);
- val |= g->intr_target_kpss_val << g->intr_target_bit;
- msm_writel_intr_target(val, pctrl, g);
- }
- /* Update configuration for gpio.
- * RAW_STATUS_EN is left on for all gpio irqs. Due to the
- * internal circuitry of TLMM, toggling the RAW_STATUS
- * could cause the INTR_STATUS to be set for EDGE interrupts.
- */
- val = msm_readl_intr_cfg(pctrl, g);
- was_enabled = val & BIT(g->intr_raw_status_bit);
- val |= BIT(g->intr_raw_status_bit);
- if (g->intr_detection_width == 2) {
- val &= ~(3 << g->intr_detection_bit);
- val &= ~(1 << g->intr_polarity_bit);
- switch (type) {
- case IRQ_TYPE_EDGE_RISING:
- val |= 1 << g->intr_detection_bit;
- val |= BIT(g->intr_polarity_bit);
- break;
- case IRQ_TYPE_EDGE_FALLING:
- val |= 2 << g->intr_detection_bit;
- val |= BIT(g->intr_polarity_bit);
- break;
- case IRQ_TYPE_EDGE_BOTH:
- val |= 3 << g->intr_detection_bit;
- val |= BIT(g->intr_polarity_bit);
- break;
- case IRQ_TYPE_LEVEL_LOW:
- break;
- case IRQ_TYPE_LEVEL_HIGH:
- val |= BIT(g->intr_polarity_bit);
- break;
- }
- } else if (g->intr_detection_width == 1) {
- val &= ~(1 << g->intr_detection_bit);
- val &= ~(1 << g->intr_polarity_bit);
- switch (type) {
- case IRQ_TYPE_EDGE_RISING:
- val |= BIT(g->intr_detection_bit);
- val |= BIT(g->intr_polarity_bit);
- break;
- case IRQ_TYPE_EDGE_FALLING:
- val |= BIT(g->intr_detection_bit);
- break;
- case IRQ_TYPE_EDGE_BOTH:
- val |= BIT(g->intr_detection_bit);
- val |= BIT(g->intr_polarity_bit);
- break;
- case IRQ_TYPE_LEVEL_LOW:
- break;
- case IRQ_TYPE_LEVEL_HIGH:
- val |= BIT(g->intr_polarity_bit);
- break;
- }
- } else {
- BUG();
- }
- msm_writel_intr_cfg(val, pctrl, g);
- /*
- * The first time we set RAW_STATUS_EN it could trigger an interrupt.
- * Clear the interrupt. This is safe because we have
- * IRQCHIP_SET_TYPE_MASKED.
- */
- if (!was_enabled)
- msm_ack_intr_status(pctrl, g);
- if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
- msm_gpio_update_dual_edge_pos(pctrl, g, d);
- raw_spin_unlock_irqrestore(&pctrl->lock, flags);
- if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
- irq_set_handler_locked(d, handle_level_irq);
- else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
- irq_set_handler_locked(d, handle_edge_irq);
- return 0;
- }
- static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- /*
- * While they may not wake up when the TLMM is powered off,
- * some GPIOs would like to wakeup the system from suspend
- * when TLMM is powered on. To allow that, enable the GPIO
- * summary line to be wakeup capable at GIC.
- */
- if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
- return irq_chip_set_wake_parent(d, on);
- return irq_set_irq_wake(pctrl->irq, on);
- }
- static int msm_gpio_irq_reqres(struct irq_data *d)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- int ret;
- if (!try_module_get(gc->owner))
- return -ENODEV;
- ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
- if (ret)
- goto out;
- msm_gpio_direction_input(gc, d->hwirq);
- if (gpiochip_lock_as_irq(gc, d->hwirq)) {
- dev_err(gc->parent,
- "unable to lock HW IRQ %lu for IRQ\n",
- d->hwirq);
- ret = -EINVAL;
- goto out;
- }
- /*
- * The disable / clear-enable workaround we do in msm_pinmux_set_mux()
- * only works if disable is not lazy since we only clear any bogus
- * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
- */
- irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
- return 0;
- out:
- module_put(gc->owner);
- return ret;
- }
- static void msm_gpio_irq_relres(struct irq_data *d)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- gpiochip_unlock_as_irq(gc, d->hwirq);
- module_put(gc->owner);
- }
- static int msm_gpio_irq_set_affinity(struct irq_data *d,
- const struct cpumask *dest, bool force)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- unsigned int i, n_dir_conns = pctrl->n_dir_conns;
- struct irq_data *gpio_irq_data;
- struct msm_dir_conn *dc = NULL;
- for (i = n_dir_conns; i > 0; i--) {
- dc = &pctrl->soc->dir_conn[i];
- gpio_irq_data = irq_get_irq_data(dc->irq);
- if (!gpio_irq_data || !(gpio_irq_data->chip) ||
- !(gpio_irq_data->chip->irq_set_affinity))
- continue;
- if (d->hwirq == dc->gpio)
- return gpio_irq_data->chip->irq_set_affinity(gpio_irq_data, dest, force);
- }
- if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
- return irq_chip_set_affinity_parent(d, dest, force);
- return -EINVAL;
- }
- static int msm_gpio_irq_set_irqchip_state(struct irq_data *d, enum irqchip_irq_state which,
- bool val)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
- if (which != IRQCHIP_STATE_PENDING)
- return -EINVAL;
- if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
- return -EINVAL;
- msm_writel_intr_status(val, pctrl, g);
- return 0;
- }
- static int msm_gpio_irq_get_irqchip_state(struct irq_data *d, enum irqchip_irq_state which,
- bool *val)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
- if (which != IRQCHIP_STATE_PENDING)
- return -EINVAL;
- if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
- return -EINVAL;
- g = &pctrl->soc->groups[d->hwirq];
- *val = msm_readl_intr_status(pctrl, g);
- return 0;
- }
- static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
- {
- struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
- return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
- return -EINVAL;
- }
- static void msm_gpio_irq_handler(struct irq_desc *desc)
- {
- struct gpio_chip *gc = irq_desc_get_handler_data(desc);
- const struct msm_pingroup *g;
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- struct irq_chip *chip = irq_desc_get_chip(desc);
- int handled = 0;
- u32 val;
- int i;
- chained_irq_enter(chip, desc);
- /*
- * Each pin has it's own IRQ status register, so use
- * enabled_irq bitmap to limit the number of reads.
- */
- for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
- g = &pctrl->soc->groups[i];
- val = msm_readl_intr_status(pctrl, g);
- if (val & BIT(g->intr_status_bit)) {
- generic_handle_domain_irq(gc->irq.domain, i);
- handled++;
- }
- }
- /* No interrupts were flagged */
- if (handled == 0)
- handle_bad_irq(desc);
- chained_irq_exit(chip, desc);
- }
- static int msm_gpio_wakeirq(struct gpio_chip *gc,
- unsigned int child,
- unsigned int child_type,
- unsigned int *parent,
- unsigned int *parent_type)
- {
- struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
- const struct msm_gpio_wakeirq_map *map;
- const struct msm_pingroup *g;
- u32 intr_cfg;
- int i;
- *parent = GPIO_NO_WAKE_IRQ;
- *parent_type = IRQ_TYPE_EDGE_RISING;
- for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
- map = &pctrl->soc->wakeirq_map[i];
- if (map->gpio == child) {
- *parent = map->wakeirq;
- break;
- }
- }
- /*
- * Additionally set intr_wakeup_enable_bit for the GPIOs Routed
- * to parent irqchip depending on intr_wakeup_present_bit.
- */
- if (*parent != GPIO_NO_WAKE_IRQ) {
- g = &pctrl->soc->groups[child];
- if (!g->intr_wakeup_present_bit)
- return 0;
- intr_cfg = msm_readl_intr_cfg(pctrl, g);
- if (intr_cfg & BIT(g->intr_wakeup_present_bit)) {
- intr_cfg |= BIT(g->intr_wakeup_enable_bit);
- msm_writel_intr_cfg(intr_cfg, pctrl, g);
- }
- }
- return 0;
- }
- static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
- {
- bool have_reserved, have_gpios;
- if (pctrl->soc->reserved_gpios)
- return true;
- have_reserved = of_property_count_u32_elems(pctrl->dev->of_node, "qcom,gpios-reserved") > 0;
- have_gpios = device_property_count_u16(pctrl->dev, "gpios") > 0;
- if (have_reserved && have_gpios)
- dev_warn(pctrl->dev, "qcom,gpios-reserved and gpios are both defined. Only one should be used.\n");
- return have_reserved || have_gpios;
- }
- static const struct irq_chip msm_gpio_irq_chip = {
- .name = "msmgpio",
- .irq_enable = msm_gpio_irq_enable,
- .irq_disable = msm_gpio_irq_disable,
- .irq_mask = msm_gpio_irq_mask,
- .irq_unmask = msm_gpio_irq_unmask,
- .irq_ack = msm_gpio_irq_ack,
- .irq_eoi = msm_gpio_irq_eoi,
- .irq_set_type = msm_gpio_irq_set_type,
- .irq_set_wake = msm_gpio_irq_set_wake,
- .irq_request_resources = msm_gpio_irq_reqres,
- .irq_release_resources = msm_gpio_irq_relres,
- .irq_set_affinity = msm_gpio_irq_set_affinity,
- .irq_set_irqchip_state = msm_gpio_irq_set_irqchip_state,
- .irq_get_irqchip_state = msm_gpio_irq_get_irqchip_state,
- .irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity,
- .flags = (IRQCHIP_MASK_ON_SUSPEND |
- IRQCHIP_SET_TYPE_MASKED |
- IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND |
- IRQCHIP_IMMUTABLE),
- };
- static int msm_gpio_init(struct msm_pinctrl *pctrl)
- {
- struct gpio_chip *chip;
- struct gpio_irq_chip *girq;
- int i, ret;
- unsigned gpio, ngpio = pctrl->soc->ngpios;
- struct device_node *np;
- bool skip;
- if (WARN_ON(ngpio > MAX_NR_GPIO))
- return -EINVAL;
- chip = &pctrl->chip;
- chip->base = -1;
- chip->ngpio = ngpio;
- chip->label = dev_name(pctrl->dev);
- chip->parent = pctrl->dev;
- chip->owner = THIS_MODULE;
- if (msm_gpio_needs_valid_mask(pctrl))
- chip->init_valid_mask = msm_gpio_init_valid_mask;
- np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
- if (np) {
- chip->irq.parent_domain = irq_find_matching_host(np,
- DOMAIN_BUS_WAKEUP);
- of_node_put(np);
- if (!chip->irq.parent_domain)
- return -EPROBE_DEFER;
- chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
- /*
- * Let's skip handling the GPIOs, if the parent irqchip
- * is handling the direct connect IRQ of the GPIO.
- */
- skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
- for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
- gpio = pctrl->soc->wakeirq_map[i].gpio;
- set_bit(gpio, pctrl->skip_wake_irqs);
- }
- }
- girq = &chip->irq;
- gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip);
- girq->parent_handler = msm_gpio_irq_handler;
- girq->fwnode = pctrl->dev->fwnode;
- girq->num_parents = 1;
- girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
- GFP_KERNEL);
- if (!girq->parents)
- return -ENOMEM;
- girq->default_type = IRQ_TYPE_NONE;
- girq->handler = handle_bad_irq;
- girq->parents[0] = pctrl->irq;
- ret = gpiochip_add_data(&pctrl->chip, pctrl);
- if (ret) {
- dev_err(pctrl->dev, "Failed register gpiochip\n");
- return ret;
- }
- /*
- * For DeviceTree-supported systems, the gpio core checks the
- * pinctrl's device node for the "gpio-ranges" property.
- * If it is present, it takes care of adding the pin ranges
- * for the driver. In this case the driver can skip ahead.
- *
- * In order to remain compatible with older, existing DeviceTree
- * files which don't set the "gpio-ranges" property or systems that
- * utilize ACPI the driver has to call gpiochip_add_pin_range().
- */
- if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
- ret = gpiochip_add_pin_range(&pctrl->chip,
- dev_name(pctrl->dev), 0, 0, chip->ngpio);
- if (ret) {
- dev_err(pctrl->dev, "Failed to add pin range\n");
- gpiochip_remove(&pctrl->chip);
- return ret;
- }
- }
- return 0;
- }
- static void msm_gpio_setup_dir_connects(struct msm_pinctrl *pctrl)
- {
- struct msm_dir_conn *dc = NULL;
- unsigned int i, n_dir_conns = pctrl->n_dir_conns, offset = 0;
- irq_hw_number_t dirconn_irq = 0, gpio_irq = 0;
- struct irq_data *gpio_irq_data;
- struct irq_domain *child_domain = pctrl->chip.irq.domain;
- for (i = n_dir_conns; i > 0; i--) {
- dc = &pctrl->soc->dir_conn[i];
- dirconn_irq = dc->irq;
- offset = n_dir_conns - i;
- if (dc->gpio == -1)
- continue;
- gpio_irq = irq_create_mapping(child_domain, dc->gpio);
- irq_set_parent(gpio_irq, dirconn_irq);
- irq_set_chip_data(gpio_irq, &(pctrl->chip));
- irq_set_chip_and_handler_name(gpio_irq, &msm_gpio_irq_chip, NULL, NULL);
- gpio_irq_data = irq_get_irq_data(gpio_irq);
- if (!gpio_irq_data)
- continue;
- irq_set_handler_data(dirconn_irq, gpio_irq_data);
- msm_dirconn_cfg_reg(gpio_irq_data, offset);
- }
- }
- static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
- void *data)
- {
- struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
- writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
- mdelay(1000);
- return NOTIFY_DONE;
- }
- static struct msm_pinctrl *poweroff_pctrl;
- static void msm_ps_hold_poweroff(void)
- {
- msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
- }
- static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
- {
- int i;
- const struct msm_function *func = pctrl->soc->functions;
- for (i = 0; i < pctrl->soc->nfunctions; i++)
- if (!strcmp(func[i].name, "ps_hold")) {
- pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
- pctrl->restart_nb.priority = 128;
- if (register_restart_handler(&pctrl->restart_nb))
- dev_err(pctrl->dev,
- "failed to setup restart handler.\n");
- poweroff_pctrl = pctrl;
- pm_power_off = msm_ps_hold_poweroff;
- break;
- }
- }
- #ifdef CONFIG_HIBERNATION
- static int pinctrl_hibernation_notifier(struct notifier_block *nb,
- unsigned long event, void *dummy)
- {
- struct msm_pinctrl *pctrl = msm_pinctrl_data;
- const struct msm_pinctrl_soc_data *soc = pctrl->soc;
- if (event == PM_HIBERNATION_PREPARE) {
- pctrl->gpio_regs = kcalloc(soc->ngroups,
- sizeof(*pctrl->gpio_regs), GFP_KERNEL);
- if (pctrl->gpio_regs == NULL)
- return -ENOMEM;
- if (soc->ntiles) {
- pctrl->msm_tile_regs = kcalloc(soc->ntiles,
- sizeof(*pctrl->msm_tile_regs), GFP_KERNEL);
- if (pctrl->msm_tile_regs == NULL) {
- kfree(pctrl->gpio_regs);
- return -ENOMEM;
- }
- }
- pctrl->hibernation = true;
- } else if (event == PM_POST_HIBERNATION) {
- kfree(pctrl->gpio_regs);
- kfree(pctrl->msm_tile_regs);
- pctrl->gpio_regs = NULL;
- pctrl->msm_tile_regs = NULL;
- pctrl->hibernation = false;
- }
- return NOTIFY_OK;
- }
- static struct notifier_block pinctrl_notif_block = {
- .notifier_call = pinctrl_hibernation_notifier,
- };
- static int msm_pinctrl_hibernation_suspend(void)
- {
- const struct msm_pingroup *pgroup;
- struct msm_pinctrl *pctrl = msm_pinctrl_data;
- const struct msm_pinctrl_soc_data *soc = pctrl->soc;
- void __iomem *tile_addr = NULL;
- u32 i, j;
- if (likely(!pctrl->hibernation))
- return 0;
- /* Save direction conn registers for hmss */
- for (i = 0; i < soc->ntiles; i++) {
- if (soc->tiles)
- tile_addr = pctrl->regs[i] + soc->dir_conn_addr[i];
- else
- tile_addr = pctrl->regs[0] + soc->dir_conn_addr[i];
- pr_err("The tile addr generated is 0x%lx\n", (u64)tile_addr);
- for (j = 0; j < 8; j++)
- pctrl->msm_tile_regs[i].dir_con_regs[j] =
- readl_relaxed(tile_addr + j*4);
- }
- /* All normal gpios will have common registers, first save them */
- for (i = 0; i < soc->ngpios; i++) {
- pgroup = &soc->groups[i];
- pctrl->gpio_regs[i].ctl_reg =
- msm_readl_ctl(pctrl, pgroup);
- pctrl->gpio_regs[i].io_reg =
- msm_readl_io(pctrl, pgroup);
- if (pgroup->intr_cfg_reg)
- pctrl->gpio_regs[i].intr_cfg_reg =
- msm_readl_intr_cfg(pctrl, pgroup);
- if (pgroup->intr_status_reg)
- pctrl->gpio_regs[i].intr_status_reg =
- msm_readl_intr_status(pctrl, pgroup);
- }
- for ( ; i < soc->ngroups; i++) {
- pgroup = &soc->groups[i];
- if (pgroup->ctl_reg)
- pctrl->gpio_regs[i].ctl_reg =
- msm_readl_ctl(pctrl, pgroup);
- if (pgroup->io_reg)
- pctrl->gpio_regs[i].io_reg =
- msm_readl_io(pctrl, pgroup);
- }
- return 0;
- }
- static void msm_pinctrl_hibernation_resume(void)
- {
- u32 i, j;
- const struct msm_pingroup *pgroup;
- struct msm_pinctrl *pctrl = msm_pinctrl_data;
- const struct msm_pinctrl_soc_data *soc = pctrl->soc;
- void __iomem *tile_addr = NULL;
- if (likely(!pctrl->hibernation) || !pctrl->gpio_regs || !pctrl->msm_tile_regs)
- return;
- for (i = 0; i < soc->ntiles; i++) {
- if (soc->tiles)
- tile_addr = pctrl->regs[i] + soc->dir_conn_addr[i];
- else
- tile_addr = pctrl->regs[0] + soc->dir_conn_addr[i];
- pr_err("The tile addr generated is 0x%lx\n", (u64)tile_addr);
- for (j = 0; j < 8; j++)
- writel_relaxed(pctrl->msm_tile_regs[i].dir_con_regs[j],
- tile_addr + j*4);
- }
- /* Restore normal gpios */
- for (i = 0; i < soc->ngpios; i++) {
- pgroup = &soc->groups[i];
- msm_writel_ctl(pctrl->gpio_regs[i].ctl_reg, pctrl, pgroup);
- msm_writel_io(pctrl->gpio_regs[i].io_reg, pctrl, pgroup);
- if (pgroup->intr_cfg_reg)
- msm_writel_intr_cfg(pctrl->gpio_regs[i].intr_cfg_reg,
- pctrl, pgroup);
- if (pgroup->intr_status_reg)
- msm_writel_intr_status(pctrl->gpio_regs[i].intr_status_reg,
- pctrl, pgroup);
- }
- for ( ; i < soc->ngroups; i++) {
- pgroup = &soc->groups[i];
- if (pgroup->ctl_reg)
- msm_writel_ctl(pctrl->gpio_regs[i].ctl_reg,
- pctrl, pgroup);
- if (pgroup->io_reg)
- msm_writel_io(pctrl->gpio_regs[i].io_reg,
- pctrl, pgroup);
- }
- }
- static struct syscore_ops msm_pinctrl_pm_ops = {
- .suspend = msm_pinctrl_hibernation_suspend,
- .resume = msm_pinctrl_hibernation_resume,
- };
- #endif
- static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
- {
- struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
- return pinctrl_force_sleep(pctrl->pctrl);
- }
- static __maybe_unused int msm_pinctrl_resume(struct device *dev)
- {
- struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
- return pinctrl_force_default(pctrl->pctrl);
- }
- SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
- msm_pinctrl_resume);
- EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
- void debug_pintctrl_msm_enable(void)
- {
- pinctrl_msm_log_mask = true;
- }
- EXPORT_SYMBOL(debug_pintctrl_msm_enable);
- void debug_pintctrl_msm_disable(void)
- {
- pinctrl_msm_log_mask = false;
- }
- EXPORT_SYMBOL(debug_pintctrl_msm_disable);
- static __maybe_unused int noirq_msm_pinctrl_suspend(struct device *dev)
- {
- struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
- if (pinctrl_msm_log_mask) {
- printk_deferred("%s\n", pctrl->chip.label);
- msm_gpios_status(&pctrl->chip);
- }
- return 0;
- }
- const struct dev_pm_ops noirq_msm_pinctrl_dev_pm_ops = {
- .suspend_noirq = noirq_msm_pinctrl_suspend,
- };
- EXPORT_SYMBOL(noirq_msm_pinctrl_dev_pm_ops);
- /*
- * msm_gpio_mpm_wake_set - API to make interrupt wakeup capable
- * @dev: Device corrsponding to pinctrl
- * @gpio: Gpio number to make interrupt wakeup capable
- * @enable: Enable/Disable wakeup capability
- */
- int msm_gpio_mpm_wake_set(unsigned int gpio, bool enable)
- {
- const struct msm_pingroup *g;
- unsigned long flags;
- u32 val, intr_cfg;
- g = &msm_pinctrl_data->soc->groups[gpio];
- if (g->wake_bit == -1 && !g->intr_wakeup_present_bit)
- return -ENOENT;
- raw_spin_lock_irqsave(&msm_pinctrl_data->lock, flags);
- intr_cfg = msm_readl_intr_cfg(msm_pinctrl_data, g);
- if ((intr_cfg & BIT(g->intr_wakeup_present_bit)) &&
- test_bit(gpio, msm_pinctrl_data->skip_wake_irqs)) {
- if (enable)
- intr_cfg |= BIT(g->intr_wakeup_enable_bit);
- else
- intr_cfg &= ~BIT(g->intr_wakeup_enable_bit);
- msm_writel_intr_cfg(intr_cfg, msm_pinctrl_data, g);
- goto exit;
- }
- val = readl_relaxed(msm_pinctrl_data->regs[g->tile] + g->wake_reg);
- if (enable)
- val |= BIT(g->wake_bit);
- else
- val &= ~BIT(g->wake_bit);
- writel_relaxed(val, msm_pinctrl_data->regs[g->tile] + g->wake_reg);
- exit:
- raw_spin_unlock_irqrestore(&msm_pinctrl_data->lock, flags);
- return 0;
- }
- EXPORT_SYMBOL(msm_gpio_mpm_wake_set);
- /*
- * msm_gpio_get_pin_address - API to get GPIO physical address range
- * @gpio_num: global GPIO num, as returned from gpio apis - desc_to_gpio().
- * @res: Out param. Resource struct with start/end addr populated.
- * @ret: true if the pin is valid, false otherwise.
- */
- bool msm_gpio_get_pin_address(unsigned int gpio_num, struct resource *res)
- {
- struct gpio_chip *chip;
- const struct msm_pingroup *g;
- struct resource *tile_res;
- struct platform_device *pdev;
- unsigned int gpio;
- if (!msm_pinctrl_data) {
- pr_err("pinctrl data is not available\n");
- return false;
- }
- chip = &msm_pinctrl_data->chip;
- pdev = to_platform_device(msm_pinctrl_data->dev);
- gpio = gpio_num - chip->base;
- if (!gpiochip_line_is_valid(chip, gpio))
- return false;
- if (!res)
- return false;
- g = &msm_pinctrl_data->soc->groups[gpio];
- if (msm_pinctrl_data->soc->tiles)
- tile_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
- msm_pinctrl_data->soc->tiles[g->tile]);
- else
- tile_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (!tile_res) {
- dev_err(&pdev->dev, "failed to get resource\n");
- return false;
- }
- res->start = tile_res->start + g->ctl_reg;
- res->end = res->start + (g->reg_size_4k ? : DEFAULT_REG_SIZE_4K) * SZ_4K - 1;
- return true;
- }
- EXPORT_SYMBOL(msm_gpio_get_pin_address);
- int msm_qup_write(u32 mode, u32 val)
- {
- int i;
- struct pinctrl_qup *regs = msm_pinctrl_data->soc->qup_regs;
- int num_regs = msm_pinctrl_data->soc->nqup_regs;
- /*Iterate over modes*/
- for (i = 0; i < num_regs; i++) {
- if (regs[i].mode == mode) {
- writel_relaxed(val & QUP_MASK,
- msm_pinctrl_data->regs[0] + regs[i].offset);
- return 0;
- }
- }
- return -ENOENT;
- }
- EXPORT_SYMBOL(msm_qup_write);
- int msm_qup_read(unsigned int mode)
- {
- int i, val;
- struct pinctrl_qup *regs = msm_pinctrl_data->soc->qup_regs;
- int num_regs = msm_pinctrl_data->soc->nqup_regs;
- /*Iterate over modes*/
- for (i = 0; i < num_regs; i++) {
- if (regs[i].mode == mode) {
- val = readl_relaxed(msm_pinctrl_data->regs[0] +
- regs[i].offset);
- return val & QUP_MASK;
- }
- }
- return -ENOENT;
- }
- EXPORT_SYMBOL(msm_qup_read);
- int msm_spare_write(int spare_reg, u32 val)
- {
- u32 offset;
- const struct msm_spare_tlmm *regs = msm_pinctrl_data->soc->spare_regs;
- int num_regs = msm_pinctrl_data->soc->nspare_regs;
- if (!regs || spare_reg >= num_regs)
- return -ENOENT;
- offset = regs[spare_reg].offset;
- if (offset != 0) {
- writel_relaxed(val & SPARE_MASK,
- msm_pinctrl_data->regs[0] + offset);
- return 0;
- }
- return -ENOENT;
- }
- EXPORT_SYMBOL(msm_spare_write);
- int msm_spare_read(int spare_reg)
- {
- u32 offset, val;
- const struct msm_spare_tlmm *regs = msm_pinctrl_data->soc->spare_regs;
- int num_regs = msm_pinctrl_data->soc->nspare_regs;
- if (!regs || spare_reg >= num_regs)
- return -ENOENT;
- offset = regs[spare_reg].offset;
- if (offset != 0) {
- val = readl_relaxed(msm_pinctrl_data->regs[0] + offset);
- return val & SPARE_MASK;
- }
- return -ENOENT;
- }
- EXPORT_SYMBOL(msm_spare_read);
- int msm_pinctrl_probe(struct platform_device *pdev,
- const struct msm_pinctrl_soc_data *soc_data)
- {
- struct msm_pinctrl *pctrl;
- struct resource *res;
- int ret, i, num_irq, irq;
- msm_pinctrl_data = pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
- if (!pctrl)
- return -ENOMEM;
- pctrl->hibernation = false;
- pctrl->dev = &pdev->dev;
- pctrl->soc = soc_data;
- pctrl->chip = msm_gpio_template;
- pctrl->intr_target_use_scm = of_device_is_compatible(
- pctrl->dev->of_node,
- "qcom,ipq8064-pinctrl");
- raw_spin_lock_init(&pctrl->lock);
- if (soc_data->tiles) {
- for (i = 0; i < soc_data->ntiles; i++) {
- res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
- soc_data->tiles[i]);
- pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
- if (IS_ERR(pctrl->regs[i]))
- return PTR_ERR(pctrl->regs[i]);
- }
- } else {
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (!res)
- return -ENOENT;
- pctrl->regs[0] = devm_ioremap_resource(&pdev->dev, res);
- if (IS_ERR(pctrl->regs[0]))
- return PTR_ERR(pctrl->regs[0]);
- pctrl->phys_base[0] = res->start;
- }
- msm_pinctrl_setup_pm_reset(pctrl);
- pctrl->irq = platform_get_irq(pdev, 0);
- if (pctrl->irq < 0)
- return pctrl->irq;
- pctrl->desc.owner = THIS_MODULE;
- pctrl->desc.pctlops = &msm_pinctrl_ops;
- pctrl->desc.pmxops = &msm_pinmux_ops;
- pctrl->desc.confops = &msm_pinconf_ops;
- pctrl->desc.name = dev_name(&pdev->dev);
- pctrl->desc.pins = pctrl->soc->pins;
- pctrl->desc.npins = pctrl->soc->npins;
- pctrl->desc.num_custom_params = ARRAY_SIZE(msm_gpio_bindings);
- pctrl->desc.custom_params = msm_gpio_bindings;
- pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
- if (IS_ERR(pctrl->pctrl)) {
- dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
- return PTR_ERR(pctrl->pctrl);
- }
- ret = msm_gpio_init(pctrl);
- if (ret)
- return ret;
- num_irq = platform_irq_count(pdev);
- for (i = 1; i < num_irq; i++) {
- struct msm_dir_conn *dc = &soc_data->dir_conn[i];
- irq = platform_get_irq(pdev, i);
- dc->irq = irq;
- __irq_set_handler(irq, msm_gpio_dirconn_handler, false, NULL);
- irq_set_irq_type(irq, IRQ_TYPE_EDGE_RISING);
- pctrl->n_dir_conns++;
- }
- msm_gpio_setup_dir_connects(pctrl);
- pinctrl_msm_log_mask = false;
- platform_set_drvdata(pdev, pctrl);
- #ifdef CONFIG_HIBERNATION
- register_syscore_ops(&msm_pinctrl_pm_ops);
- ret = register_pm_notifier(&pinctrl_notif_block);
- if (ret)
- return ret;
- #endif
- dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
- return 0;
- }
- EXPORT_SYMBOL(msm_pinctrl_probe);
- int msm_pinctrl_remove(struct platform_device *pdev)
- {
- struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
- gpiochip_remove(&pctrl->chip);
- unregister_restart_handler(&pctrl->restart_nb);
- return 0;
- }
- EXPORT_SYMBOL(msm_pinctrl_remove);
- MODULE_SOFTDEP("pre: qcom-pdc");
- MODULE_DESCRIPTION("Qualcomm Technologies, Inc. pinctrl-msm driver");
- MODULE_LICENSE("GPL v2");
|