pinctrl-msm.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013, Sony Mobile Communications AB.
  4. * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  5. * Copyright (c) 2023-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pinctrl/machine.h>
  14. #include <linux/pinctrl/pinctrl.h>
  15. #include <linux/pinctrl/pinmux.h>
  16. #include <linux/pinctrl/pinconf.h>
  17. #include <linux/pinctrl/pinconf-generic.h>
  18. #include <linux/slab.h>
  19. #include <linux/gpio/driver.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/reboot.h>
  23. #include <linux/pm.h>
  24. #include <linux/log2.h>
  25. #include <linux/qcom_scm.h>
  26. #include <linux/soc/qcom/irq.h>
  27. #include <linux/bitmap.h>
  28. #include <linux/sizes.h>
  29. #include <linux/suspend.h>
  30. #include <linux/notifier.h>
  31. #include <linux/syscore_ops.h>
  32. #include "../core.h"
  33. #include "../pinconf.h"
  34. #include "pinctrl-msm.h"
  35. #include "../pinctrl-utils.h"
  36. #if IS_ENABLED(CONFIG_SEC_GPIO_DVS)
  37. #include "secgpio_dvs.h"
  38. #endif
  39. #define MAX_NR_GPIO 300
  40. #define MAX_NR_TILES 4
  41. #define DEFAULT_REG_SIZE_4K 1
  42. #define PS_HOLD_OFFSET 0x820
  43. #define QUP_MASK GENMASK(5, 0)
  44. #define SPARE_MASK GENMASK(15, 8)
  45. /**
  46. * struct msm_pinctrl - state for a pinctrl-msm device
  47. * @dev: device handle.
  48. * @pctrl: pinctrl handle.
  49. * @chip: gpiochip handle.
  50. * @desc: pin controller descriptor
  51. * @restart_nb: restart notifier block.
  52. * @irq: parent irq for the TLMM irq_chip.
  53. * @n_dir_conns: The number of pins directly connected to GIC.
  54. * @intr_target_use_scm: route irq to application cpu using scm calls
  55. * @lock: Spinlock to protect register resources as well
  56. * as msm_pinctrl data structures.
  57. * @enabled_irqs: Bitmap of currently enabled irqs.
  58. * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
  59. * detection.
  60. * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
  61. * @disabled_for_mux: These IRQs were disabled because we muxed away.
  62. * @ever_gpio: This bit is set the first time we mux a pin to gpio_func.
  63. * @soc: Reference to soc_data of platform specific data.
  64. * @regs: Base addresses for the TLMM tiles.
  65. * @phys_base: Physical base address
  66. */
  67. struct msm_pinctrl {
  68. struct device *dev;
  69. struct pinctrl_dev *pctrl;
  70. struct gpio_chip chip;
  71. struct pinctrl_desc desc;
  72. struct notifier_block restart_nb;
  73. int irq;
  74. int n_dir_conns;
  75. bool intr_target_use_scm;
  76. raw_spinlock_t lock;
  77. DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
  78. DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
  79. DECLARE_BITMAP(skip_wake_irqs, MAX_NR_GPIO);
  80. DECLARE_BITMAP(disabled_for_mux, MAX_NR_GPIO);
  81. DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
  82. const struct msm_pinctrl_soc_data *soc;
  83. void __iomem *regs[MAX_NR_TILES];
  84. u32 phys_base[MAX_NR_TILES];
  85. struct msm_gpio_regs *gpio_regs;
  86. struct msm_tile *msm_tile_regs;
  87. bool hibernation;
  88. };
  89. static struct msm_pinctrl *msm_pinctrl_data;
  90. static bool pinctrl_msm_log_mask;
  91. #define EGPIO_PRESENT 11
  92. #define EGPIO_ENABLE 12
  93. #define I2C_PULL 13
  94. #define MSM_APPS_OWNER 1
  95. #define MSM_REMOTE_OWNER 0
  96. /* custom pinconf parameters for msm pinictrl*/
  97. #define MSM_PIN_CONFIG_APPS (PIN_CONFIG_END + 1)
  98. #define MSM_PIN_CONFIG_REMOTE (PIN_CONFIG_END + 2)
  99. #define MSM_PIN_CONFIG_I2C_PULL (PIN_CONFIG_END + 3)
  100. static const struct pinconf_generic_params msm_gpio_bindings[] = {
  101. {"qcom,apps", MSM_PIN_CONFIG_APPS, 0},
  102. {"qcom,remote", MSM_PIN_CONFIG_REMOTE, 0},
  103. {"qcom,i2c_pull", MSM_PIN_CONFIG_I2C_PULL, 0},
  104. };
  105. static const char * const pulls_keeper[] = {
  106. "no pull",
  107. "pull down",
  108. "keeper",
  109. "pull up"
  110. };
  111. static const char * const pulls_no_keeper[] = {
  112. "no pull",
  113. "pull down",
  114. "pull up",
  115. };
  116. #define MSM_ACCESSOR(name) \
  117. static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
  118. const struct msm_pingroup *g) \
  119. { \
  120. return readl(pctrl->regs[g->tile] + g->name##_reg); \
  121. } \
  122. static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
  123. const struct msm_pingroup *g) \
  124. { \
  125. writel(val, pctrl->regs[g->tile] + g->name##_reg); \
  126. }
  127. MSM_ACCESSOR(ctl)
  128. MSM_ACCESSOR(io)
  129. MSM_ACCESSOR(intr_cfg)
  130. MSM_ACCESSOR(intr_status)
  131. MSM_ACCESSOR(intr_target)
  132. struct msm_gpio_regs {
  133. u32 ctl_reg;
  134. u32 io_reg;
  135. u32 intr_cfg_reg;
  136. u32 intr_status_reg;
  137. };
  138. struct msm_tile {
  139. u32 dir_con_regs[8];
  140. };
  141. static void msm_ack_intr_status(struct msm_pinctrl *pctrl,
  142. const struct msm_pingroup *g)
  143. {
  144. u32 val = g->intr_ack_high ? BIT(g->intr_status_bit) : 0;
  145. msm_writel_intr_status(val, pctrl, g);
  146. }
  147. static int msm_get_groups_count(struct pinctrl_dev *pctldev)
  148. {
  149. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  150. return pctrl->soc->ngroups;
  151. }
  152. static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
  153. unsigned group)
  154. {
  155. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  156. return pctrl->soc->groups[group].name;
  157. }
  158. static int msm_get_group_pins(struct pinctrl_dev *pctldev,
  159. unsigned group,
  160. const unsigned **pins,
  161. unsigned *num_pins)
  162. {
  163. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  164. *pins = pctrl->soc->groups[group].pins;
  165. *num_pins = pctrl->soc->groups[group].npins;
  166. return 0;
  167. }
  168. static const struct pinctrl_ops msm_pinctrl_ops = {
  169. .get_groups_count = msm_get_groups_count,
  170. .get_group_name = msm_get_group_name,
  171. .get_group_pins = msm_get_group_pins,
  172. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  173. .dt_free_map = pinctrl_utils_free_map,
  174. };
  175. static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
  176. {
  177. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  178. struct gpio_chip *chip = &pctrl->chip;
  179. return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
  180. }
  181. static int msm_get_functions_count(struct pinctrl_dev *pctldev)
  182. {
  183. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  184. return pctrl->soc->nfunctions;
  185. }
  186. static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
  187. unsigned function)
  188. {
  189. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  190. return pctrl->soc->functions[function].name;
  191. }
  192. static int msm_get_function_groups(struct pinctrl_dev *pctldev,
  193. unsigned function,
  194. const char * const **groups,
  195. unsigned * const num_groups)
  196. {
  197. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  198. *groups = pctrl->soc->functions[function].groups;
  199. *num_groups = pctrl->soc->functions[function].ngroups;
  200. return 0;
  201. }
  202. static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
  203. unsigned function,
  204. unsigned group)
  205. {
  206. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  207. struct gpio_chip *gc = &pctrl->chip;
  208. const struct msm_pinctrl_soc_data *ps = pctrl->soc;
  209. unsigned int irq = irq_find_mapping(gc->irq.domain, group);
  210. struct irq_data *d = irq_get_irq_data(irq);
  211. unsigned int gpio_func = pctrl->soc->gpio_func;
  212. unsigned int egpio_func = pctrl->soc->egpio_func;
  213. const struct msm_pingroup *g;
  214. unsigned long flags;
  215. u32 val, mask;
  216. int i;
  217. g = &pctrl->soc->groups[group];
  218. mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
  219. for (i = 0; i < g->nfuncs; i++) {
  220. if (g->funcs[i] == function)
  221. break;
  222. }
  223. if (WARN_ON(i == g->nfuncs))
  224. return -EINVAL;
  225. /*
  226. * If an GPIO interrupt is setup on this pin then we need special
  227. * handling. Specifically interrupt detection logic will still see
  228. * the pin twiddle even when we're muxed away.
  229. *
  230. * When we see a pin with an interrupt setup on it then we'll disable
  231. * (mask) interrupts on it when we mux away until we mux back. Note
  232. * that disable_irq() refcounts and interrupts are disabled as long as
  233. * at least one disable_irq() has been called.
  234. */
  235. if (d && i != gpio_func &&
  236. !test_and_set_bit(d->hwirq, pctrl->disabled_for_mux))
  237. disable_irq(irq);
  238. raw_spin_lock_irqsave(&pctrl->lock, flags);
  239. val = msm_readl_ctl(pctrl, g);
  240. /*
  241. * If this is the first time muxing to GPIO and the direction is
  242. * output, make sure that we're not going to be glitching the pin
  243. * by reading the current state of the pin and setting it as the
  244. * output.
  245. */
  246. if (i == gpio_func && (val & BIT(g->oe_bit)) &&
  247. !test_and_set_bit(group, pctrl->ever_gpio)) {
  248. u32 io_val = msm_readl_io(pctrl, g);
  249. if (io_val & BIT(g->in_bit)) {
  250. if (!(io_val & BIT(g->out_bit)))
  251. msm_writel_io(io_val | BIT(g->out_bit), pctrl, g);
  252. } else {
  253. if (io_val & BIT(g->out_bit))
  254. msm_writel_io(io_val & ~BIT(g->out_bit), pctrl, g);
  255. }
  256. }
  257. if (egpio_func && i == egpio_func) {
  258. if (val & BIT(g->egpio_present))
  259. val &= ~BIT(g->egpio_enable);
  260. } else {
  261. val &= ~mask;
  262. val |= i << g->mux_bit;
  263. /* Claim ownership of pin if egpio capable and
  264. * also need check if setting NA in funcs.
  265. */
  266. if (egpio_func && val & BIT(g->egpio_present)) {
  267. if (g->funcs[egpio_func] != ps->nfunctions)
  268. val |= BIT(g->egpio_enable);
  269. }
  270. }
  271. msm_writel_ctl(val, pctrl, g);
  272. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  273. if (d && i == gpio_func &&
  274. test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) {
  275. /*
  276. * Clear interrupts detected while not GPIO since we only
  277. * masked things.
  278. */
  279. if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
  280. irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
  281. else
  282. msm_ack_intr_status(pctrl, g);
  283. enable_irq(irq);
  284. }
  285. return 0;
  286. }
  287. static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
  288. struct pinctrl_gpio_range *range,
  289. unsigned offset)
  290. {
  291. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  292. const struct msm_pingroup *g = &pctrl->soc->groups[offset];
  293. /* No funcs? Probably ACPI so can't do anything here */
  294. if (!g->nfuncs)
  295. return 0;
  296. return msm_pinmux_set_mux(pctldev, g->funcs[pctrl->soc->gpio_func], offset);
  297. }
  298. static const struct pinmux_ops msm_pinmux_ops = {
  299. .request = msm_pinmux_request,
  300. .get_functions_count = msm_get_functions_count,
  301. .get_function_name = msm_get_function_name,
  302. .get_function_groups = msm_get_function_groups,
  303. .gpio_request_enable = msm_pinmux_request_gpio,
  304. .set_mux = msm_pinmux_set_mux,
  305. };
  306. static int msm_config_reg(struct msm_pinctrl *pctrl,
  307. const struct msm_pingroup *g,
  308. unsigned param,
  309. unsigned *mask,
  310. unsigned *bit)
  311. {
  312. switch (param) {
  313. case PIN_CONFIG_BIAS_DISABLE:
  314. case PIN_CONFIG_BIAS_PULL_DOWN:
  315. case PIN_CONFIG_BIAS_BUS_HOLD:
  316. case PIN_CONFIG_BIAS_PULL_UP:
  317. *bit = g->pull_bit;
  318. *mask = 3;
  319. break;
  320. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  321. *bit = g->od_bit;
  322. *mask = 1;
  323. break;
  324. case PIN_CONFIG_DRIVE_STRENGTH:
  325. *bit = g->drv_bit;
  326. *mask = 7;
  327. break;
  328. case PIN_CONFIG_OUTPUT:
  329. case PIN_CONFIG_INPUT_ENABLE:
  330. *bit = g->oe_bit;
  331. *mask = 1;
  332. break;
  333. case MSM_PIN_CONFIG_APPS:
  334. case MSM_PIN_CONFIG_REMOTE:
  335. *bit = EGPIO_ENABLE;
  336. *mask = 1;
  337. break;
  338. case MSM_PIN_CONFIG_I2C_PULL:
  339. *bit = I2C_PULL;
  340. *mask = 1;
  341. break;
  342. default:
  343. return -ENOTSUPP;
  344. }
  345. return 0;
  346. }
  347. #define MSM_NO_PULL 0
  348. #define MSM_PULL_DOWN 1
  349. #define MSM_KEEPER 2
  350. #define MSM_PULL_UP_NO_KEEPER 2
  351. #define MSM_PULL_UP 3
  352. static unsigned msm_regval_to_drive(u32 val)
  353. {
  354. return (val + 1) * 2;
  355. }
  356. static int msm_config_group_get(struct pinctrl_dev *pctldev,
  357. unsigned int group,
  358. unsigned long *config)
  359. {
  360. const struct msm_pingroup *g;
  361. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  362. unsigned param = pinconf_to_config_param(*config);
  363. unsigned mask;
  364. unsigned arg;
  365. unsigned bit;
  366. int ret;
  367. u32 val;
  368. if (!gpiochip_line_is_valid(&msm_pinctrl_data->chip, group))
  369. return -EINVAL;
  370. g = &pctrl->soc->groups[group];
  371. ret = msm_config_reg(pctrl, g, param, &mask, &bit);
  372. if (ret < 0)
  373. return ret;
  374. val = msm_readl_ctl(pctrl, g);
  375. arg = (val >> bit) & mask;
  376. /* Convert register value to pinconf value */
  377. switch (param) {
  378. case PIN_CONFIG_BIAS_DISABLE:
  379. if (arg != MSM_NO_PULL)
  380. return -EINVAL;
  381. arg = 1;
  382. break;
  383. case PIN_CONFIG_BIAS_PULL_DOWN:
  384. if (arg != MSM_PULL_DOWN)
  385. return -EINVAL;
  386. arg = 1;
  387. break;
  388. case PIN_CONFIG_BIAS_BUS_HOLD:
  389. if (pctrl->soc->pull_no_keeper)
  390. return -ENOTSUPP;
  391. if (arg != MSM_KEEPER)
  392. return -EINVAL;
  393. arg = 1;
  394. break;
  395. case PIN_CONFIG_BIAS_PULL_UP:
  396. if (pctrl->soc->pull_no_keeper)
  397. arg = arg == MSM_PULL_UP_NO_KEEPER;
  398. else
  399. arg = arg == MSM_PULL_UP;
  400. if (!arg)
  401. return -EINVAL;
  402. break;
  403. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  404. /* Pin is not open-drain */
  405. if (!arg)
  406. return -EINVAL;
  407. arg = 1;
  408. break;
  409. case PIN_CONFIG_DRIVE_STRENGTH:
  410. arg = msm_regval_to_drive(arg);
  411. break;
  412. case PIN_CONFIG_OUTPUT:
  413. /* Pin is not output */
  414. if (!arg)
  415. return -EINVAL;
  416. val = msm_readl_io(pctrl, g);
  417. arg = !!(val & BIT(g->in_bit));
  418. break;
  419. case PIN_CONFIG_INPUT_ENABLE:
  420. /* Pin is output */
  421. if (arg)
  422. return -EINVAL;
  423. arg = 1;
  424. break;
  425. case MSM_PIN_CONFIG_APPS:
  426. /* Pin do not support egpio or remote owner */
  427. if (!(val & BIT(EGPIO_PRESENT)) || !arg)
  428. return -EINVAL;
  429. arg = 1;
  430. break;
  431. case MSM_PIN_CONFIG_REMOTE:
  432. /* Pin do not support epgio or APPS owner */
  433. if (!(val & BIT(EGPIO_PRESENT)) || arg)
  434. return -EINVAL;
  435. arg = 1;
  436. break;
  437. case MSM_PIN_CONFIG_I2C_PULL:
  438. arg = 1;
  439. break;
  440. default:
  441. return -ENOTSUPP;
  442. }
  443. *config = pinconf_to_config_packed(param, arg);
  444. return 0;
  445. }
  446. static int msm_config_group_set(struct pinctrl_dev *pctldev,
  447. unsigned group,
  448. unsigned long *configs,
  449. unsigned num_configs)
  450. {
  451. const struct msm_pingroup *g;
  452. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  453. unsigned long flags;
  454. unsigned param;
  455. unsigned mask;
  456. unsigned arg;
  457. unsigned bit;
  458. u32 owner_bit, owner_update = 0;
  459. int ret;
  460. u32 val;
  461. int i;
  462. g = &pctrl->soc->groups[group];
  463. for (i = 0; i < num_configs; i++) {
  464. param = pinconf_to_config_param(configs[i]);
  465. arg = pinconf_to_config_argument(configs[i]);
  466. ret = msm_config_reg(pctrl, g, param, &mask, &bit);
  467. if (ret < 0)
  468. return ret;
  469. /* Convert pinconf values to register values */
  470. switch (param) {
  471. case PIN_CONFIG_BIAS_DISABLE:
  472. arg = MSM_NO_PULL;
  473. break;
  474. case PIN_CONFIG_BIAS_PULL_DOWN:
  475. arg = MSM_PULL_DOWN;
  476. break;
  477. case PIN_CONFIG_BIAS_BUS_HOLD:
  478. if (pctrl->soc->pull_no_keeper)
  479. return -ENOTSUPP;
  480. arg = MSM_KEEPER;
  481. break;
  482. case PIN_CONFIG_BIAS_PULL_UP:
  483. if (pctrl->soc->pull_no_keeper)
  484. arg = MSM_PULL_UP_NO_KEEPER;
  485. else
  486. arg = MSM_PULL_UP;
  487. break;
  488. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  489. arg = 1;
  490. break;
  491. case PIN_CONFIG_DRIVE_STRENGTH:
  492. /* Check for invalid values */
  493. if (arg > 16 || arg < 2 || (arg % 2) != 0)
  494. arg = -1;
  495. else
  496. arg = (arg / 2) - 1;
  497. break;
  498. case PIN_CONFIG_OUTPUT:
  499. /* set output value */
  500. raw_spin_lock_irqsave(&pctrl->lock, flags);
  501. val = msm_readl_io(pctrl, g);
  502. if (arg)
  503. val |= BIT(g->out_bit);
  504. else
  505. val &= ~BIT(g->out_bit);
  506. msm_writel_io(val, pctrl, g);
  507. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  508. /* enable output */
  509. arg = 1;
  510. break;
  511. case PIN_CONFIG_INPUT_ENABLE:
  512. /* disable output */
  513. arg = 0;
  514. break;
  515. case MSM_PIN_CONFIG_APPS:
  516. owner_update = 1;
  517. owner_bit = MSM_APPS_OWNER;
  518. break;
  519. case MSM_PIN_CONFIG_REMOTE:
  520. owner_update = 1;
  521. owner_bit = MSM_REMOTE_OWNER;
  522. break;
  523. case MSM_PIN_CONFIG_I2C_PULL:
  524. arg = 1;
  525. break;
  526. default:
  527. dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
  528. param);
  529. return -EINVAL;
  530. }
  531. /* Range-check user-supplied value */
  532. if (arg & ~mask) {
  533. dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
  534. return -EINVAL;
  535. }
  536. /* Update the owner bits as final config update */
  537. if (param == MSM_PIN_CONFIG_APPS || param == MSM_PIN_CONFIG_REMOTE)
  538. continue;
  539. raw_spin_lock_irqsave(&pctrl->lock, flags);
  540. val = msm_readl_ctl(pctrl, g);
  541. val &= ~(mask << bit);
  542. val |= arg << bit;
  543. msm_writel_ctl(val, pctrl, g);
  544. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  545. }
  546. if (owner_update) {
  547. ret = msm_config_reg(pctrl, g,
  548. MSM_PIN_CONFIG_APPS, &mask, &bit);
  549. if (ret < 0)
  550. return ret;
  551. raw_spin_lock_irqsave(&pctrl->lock, flags);
  552. val = msm_readl_ctl(pctrl, g);
  553. if (val & BIT(EGPIO_PRESENT)) {
  554. val &= ~(mask << bit);
  555. val |= owner_bit << bit;
  556. msm_writel_ctl(val, pctrl, g);
  557. }
  558. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  559. }
  560. return 0;
  561. }
  562. static const struct pinconf_ops msm_pinconf_ops = {
  563. .is_generic = true,
  564. .pin_config_group_get = msm_config_group_get,
  565. .pin_config_group_set = msm_config_group_set,
  566. };
  567. static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  568. {
  569. const struct msm_pingroup *g;
  570. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  571. unsigned long flags;
  572. u32 val;
  573. g = &pctrl->soc->groups[offset];
  574. raw_spin_lock_irqsave(&pctrl->lock, flags);
  575. val = msm_readl_ctl(pctrl, g);
  576. val &= ~BIT(g->oe_bit);
  577. msm_writel_ctl(val, pctrl, g);
  578. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  579. return 0;
  580. }
  581. static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  582. {
  583. const struct msm_pingroup *g;
  584. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  585. unsigned long flags;
  586. u32 val;
  587. g = &pctrl->soc->groups[offset];
  588. raw_spin_lock_irqsave(&pctrl->lock, flags);
  589. val = msm_readl_io(pctrl, g);
  590. if (value)
  591. val |= BIT(g->out_bit);
  592. else
  593. val &= ~BIT(g->out_bit);
  594. msm_writel_io(val, pctrl, g);
  595. val = msm_readl_ctl(pctrl, g);
  596. val |= BIT(g->oe_bit);
  597. msm_writel_ctl(val, pctrl, g);
  598. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  599. return 0;
  600. }
  601. static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  602. {
  603. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  604. const struct msm_pingroup *g;
  605. u32 val;
  606. g = &pctrl->soc->groups[offset];
  607. val = msm_readl_ctl(pctrl, g);
  608. return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT :
  609. GPIO_LINE_DIRECTION_IN;
  610. }
  611. static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
  612. {
  613. const struct msm_pingroup *g;
  614. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  615. u32 val;
  616. g = &pctrl->soc->groups[offset];
  617. val = msm_readl_io(pctrl, g);
  618. return !!(val & BIT(g->in_bit));
  619. }
  620. static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  621. {
  622. const struct msm_pingroup *g;
  623. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  624. unsigned long flags;
  625. u32 val;
  626. g = &pctrl->soc->groups[offset];
  627. raw_spin_lock_irqsave(&pctrl->lock, flags);
  628. val = msm_readl_io(pctrl, g);
  629. if (value)
  630. val |= BIT(g->out_bit);
  631. else
  632. val &= ~BIT(g->out_bit);
  633. msm_writel_io(val, pctrl, g);
  634. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  635. }
  636. static void msm_gpio_pin_status_get(struct msm_pinctrl *pctrl, const struct msm_pingroup *g,
  637. unsigned int offset, int *is_out, unsigned int *func,
  638. int *drive, int *pull, int *egpio_enable, int *val)
  639. {
  640. u32 ctl_reg, io_reg;
  641. ctl_reg = msm_readl_ctl(pctrl, g);
  642. io_reg = msm_readl_io(pctrl, g);
  643. *is_out = !!(ctl_reg & BIT(g->oe_bit));
  644. *func = (ctl_reg >> g->mux_bit) & 7;
  645. *drive = (ctl_reg >> g->drv_bit) & 7;
  646. *pull = (ctl_reg >> g->pull_bit) & 3;
  647. *egpio_enable = 0;
  648. if (pctrl->soc->egpio_func && ctl_reg & BIT(g->egpio_present))
  649. *egpio_enable = !(ctl_reg & BIT(g->egpio_enable));
  650. if (*is_out)
  651. *val = !!(io_reg & BIT(g->out_bit));
  652. else
  653. *val = !!(io_reg & BIT(g->in_bit));
  654. }
  655. #if IS_ENABLED(CONFIG_SEC_GPIO_DVS) || IS_ENABLED(CONFIG_SEC_GPIO_DUMP)
  656. #define AP_GPIO_MAX 210
  657. #define GET_RESULT_GPIO(a, b, c) \
  658. ((a<<4 & 0xF0) | (b<<1 & 0xE) | (c & 0x1))
  659. struct gpiomux_setting {
  660. int func;
  661. int drv;
  662. int pull;
  663. int is_out;
  664. int val;
  665. };
  666. static void msm_gp_get_all(struct gpio_chip *chip, u32 pin_no, struct gpiomux_setting *set)
  667. {
  668. const struct msm_pingroup *g;
  669. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  670. u32 ctl_reg, io_reg;
  671. int drive;
  672. g = &pctrl->soc->groups[pin_no];
  673. ctl_reg = msm_readl_ctl(pctrl, g);
  674. io_reg = msm_readl_io(pctrl, g);
  675. set->is_out = !!(ctl_reg & BIT(g->oe_bit));
  676. set->func = (ctl_reg >> g->mux_bit) & 7;
  677. drive = (ctl_reg >> g->drv_bit) & 7;
  678. set->drv = msm_regval_to_drive(drive);
  679. set->pull = (ctl_reg >> g->pull_bit) & 3;
  680. if (set->is_out)
  681. set->val = !!(io_reg & BIT(g->out_bit));
  682. else
  683. set->val = !!(io_reg & BIT(g->in_bit));
  684. }
  685. #endif
  686. #if IS_ENABLED(CONFIG_SEC_GPIO_DVS)
  687. /****************************************************************/
  688. /* Pre-defined variables. (DO NOT CHANGE THIS!!) */
  689. static unsigned char checkgpiomap_result[GDVS_PHONE_STATUS_MAX][AP_GPIO_MAX];
  690. static struct gpiomap_result gpiomap_result = {
  691. .init = checkgpiomap_result[PHONE_INIT],
  692. .sleep = checkgpiomap_result[PHONE_SLEEP]
  693. };
  694. static void msm_check_gpio_status(unsigned char phonestate)
  695. {
  696. struct gpio_chip *gp;
  697. struct gpiomux_setting set;
  698. int i;
  699. u8 temp_io = 0, temp_pdpu = 0, temp_lh = 0;
  700. if (!msm_pinctrl_data) {
  701. pr_err("pinctrl data is not available\n");
  702. return;
  703. }
  704. gp = &msm_pinctrl_data->chip;
  705. pr_info("[dvs_%s] state : %s\n", __func__,
  706. (phonestate == PHONE_INIT) ? "init" : "sleep");
  707. for (i = 0; i < AP_GPIO_MAX; i++) {
  708. /* If it is not valid gpio or secure, Shows IN/PD/L */
  709. if (!gpiochip_line_is_valid(gp, i)) {
  710. checkgpiomap_result[phonestate][i] =
  711. GET_RESULT_GPIO(0x1, 0x1, 0x0);
  712. continue;
  713. }
  714. msm_gp_get_all(gp, i, &set);
  715. if (set.func == 0) {
  716. if (set.is_out)
  717. temp_io = 0x02; /* GPIO_OUT */
  718. else
  719. temp_io = 0x01; /* GPIO_IN */
  720. } else
  721. temp_io = 0x0; /* FUNC */
  722. switch (set.pull) {
  723. case MSM_NO_PULL:
  724. temp_pdpu = 0x00;
  725. break;
  726. case MSM_PULL_DOWN:
  727. temp_pdpu = 0x01;
  728. break;
  729. /*
  730. * gpiodvs is only for samsung,
  731. * so pull definition could be different from original value.
  732. * gpiodvs definition: PU(0x2), KEEPER(0x3)
  733. */
  734. case MSM_PULL_UP:
  735. temp_pdpu = 0x02;
  736. break;
  737. case MSM_KEEPER:
  738. temp_pdpu = 0x03;
  739. break;
  740. default:
  741. temp_pdpu = 0x07;
  742. break;
  743. }
  744. temp_lh = set.val;
  745. checkgpiomap_result[phonestate][i] =
  746. GET_RESULT_GPIO(temp_io, temp_pdpu, temp_lh);
  747. }
  748. pr_info("[dvs_%s]-\n", __func__);
  749. }
  750. static struct gpio_dvs_t msm_gpio_dvs = {
  751. .result = &gpiomap_result,
  752. .check_gpio_status = msm_check_gpio_status,
  753. .count = AP_GPIO_MAX,
  754. .check_sleep = false,
  755. };
  756. const struct secgpio_dvs_data msm_gpio_dvs_data = {
  757. .gpio_dvs = &msm_gpio_dvs,
  758. };
  759. EXPORT_SYMBOL_GPL(msm_gpio_dvs_data);
  760. #endif /* CONFIG_SEC_GPIO_DVS */
  761. #if IS_ENABLED(CONFIG_SEC_GPIO_DUMP)
  762. static const char * const gpiomux_func_str[] = {
  763. "GPIO",
  764. "Func_1",
  765. "Func_2",
  766. "Func_3",
  767. "Func_4",
  768. "Func_5",
  769. "Func_6",
  770. "Func_7",
  771. "Func_8",
  772. "Func_9",
  773. "Func_a",
  774. "Func_b",
  775. "Func_c",
  776. "Func_d",
  777. "Func_e",
  778. "Func_f",
  779. };
  780. static const char * const gpiomux_pull_str[] = {
  781. "PULL_NONE",
  782. "PULL_DOWN",
  783. "PULL_KEEPER",
  784. "PULL_UP",
  785. };
  786. static const char * const gpiomux_dir_str[] = {
  787. "IN",
  788. "OUT",
  789. };
  790. static const char * const gpiomux_val_str[] = {
  791. "LOW",
  792. "HIGH",
  793. };
  794. void sec_ap_gpio_debug_print(void)
  795. {
  796. struct gpio_chip *gp;
  797. struct gpiomux_setting set = {0,};
  798. int i;
  799. if (!msm_pinctrl_data) {
  800. pr_err("pinctrl data is not available\n");
  801. return;
  802. }
  803. gp = &msm_pinctrl_data->chip;
  804. for (i = 0; i < AP_GPIO_MAX; i++) {
  805. if (!gpiochip_line_is_valid(gp, i))
  806. continue;
  807. msm_gp_get_all(gp, i, &set);
  808. pr_info("GPIO[%u] %10s %10s %13s DRV_%dmA %10s\n",
  809. i,
  810. gpiomux_func_str[set.func],
  811. gpiomux_dir_str[set.is_out],
  812. gpiomux_pull_str[set.pull],
  813. set.drv,
  814. gpiomux_val_str[set.val]);
  815. }
  816. }
  817. EXPORT_SYMBOL(sec_ap_gpio_debug_print);
  818. #endif /* CONFIG_SEC_GPIO_DUMP */
  819. #ifdef CONFIG_DEBUG_FS
  820. #include <linux/seq_file.h>
  821. static void msm_gpio_dbg_show_one(struct seq_file *s,
  822. struct pinctrl_dev *pctldev,
  823. struct gpio_chip *chip,
  824. unsigned offset,
  825. unsigned gpio)
  826. {
  827. const struct msm_pingroup *g;
  828. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  829. unsigned func;
  830. int is_out;
  831. int drive;
  832. int pull;
  833. int val;
  834. int egpio_enable;
  835. if (!gpiochip_line_is_valid(chip, offset))
  836. return;
  837. g = &pctrl->soc->groups[offset];
  838. msm_gpio_pin_status_get(pctrl, g, offset, &is_out, &func,
  839. &drive, &pull, &egpio_enable, &val);
  840. if (egpio_enable) {
  841. seq_printf(s, " %-8s: egpio\n", g->name);
  842. return;
  843. }
  844. seq_printf(s, " %-8s: %-3s", g->name, is_out ? "out" : "in");
  845. seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
  846. seq_printf(s, " %dmA", msm_regval_to_drive(drive));
  847. if (pctrl->soc->pull_no_keeper)
  848. seq_printf(s, " %s", pulls_no_keeper[pull]);
  849. else
  850. seq_printf(s, " %s", pulls_keeper[pull]);
  851. seq_puts(s, "\n");
  852. }
  853. static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  854. {
  855. unsigned gpio = chip->base;
  856. unsigned i;
  857. for (i = 0; i < chip->ngpio; i++, gpio++)
  858. msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  859. }
  860. #else
  861. #define msm_gpio_dbg_show NULL
  862. #endif
  863. static void msm_gpio_log_pin_status(struct gpio_chip *chip, unsigned int offset)
  864. {
  865. const struct msm_pingroup *g;
  866. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  867. unsigned int func;
  868. int is_out;
  869. int drive;
  870. int pull;
  871. int val;
  872. int egpio_enable;
  873. if (!gpiochip_line_is_valid(chip, offset))
  874. return;
  875. g = &pctrl->soc->groups[offset];
  876. msm_gpio_pin_status_get(pctrl, g, offset, &is_out, &func,
  877. &drive, &pull, &egpio_enable, &val);
  878. printk_deferred("%s: %s, %s, func%d, %dmA, %s\n",
  879. g->name, is_out ? "out" : "in",
  880. val ? "high" : "low", func,
  881. msm_regval_to_drive(drive),
  882. pctrl->soc->pull_no_keeper ? pulls_no_keeper[pull] : pulls_keeper[pull]);
  883. }
  884. static void msm_gpios_status(struct gpio_chip *chip)
  885. {
  886. unsigned int i;
  887. for (i = 0; i < chip->ngpio; i++)
  888. msm_gpio_log_pin_status(chip, i);
  889. }
  890. static int msm_gpio_init_valid_mask(struct gpio_chip *gc,
  891. unsigned long *valid_mask,
  892. unsigned int ngpios)
  893. {
  894. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  895. int ret;
  896. unsigned int len, i;
  897. const int *reserved = pctrl->soc->reserved_gpios;
  898. struct property *prop;
  899. const __be32 *p;
  900. u16 *tmp;
  901. /* Driver provided reserved list overrides DT and ACPI */
  902. if (reserved) {
  903. bitmap_fill(valid_mask, ngpios);
  904. for (i = 0; reserved[i] >= 0; i++) {
  905. if (i >= ngpios || reserved[i] >= ngpios) {
  906. dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
  907. return -EINVAL;
  908. }
  909. clear_bit(reserved[i], valid_mask);
  910. }
  911. return 0;
  912. }
  913. if (of_property_count_u32_elems(pctrl->dev->of_node, "qcom,gpios-reserved") > 0) {
  914. bitmap_fill(valid_mask, ngpios);
  915. of_property_for_each_u32(pctrl->dev->of_node, "qcom,gpios-reserved", prop, p, i) {
  916. if (i >= ngpios) {
  917. dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
  918. return -EINVAL;
  919. }
  920. clear_bit(i, valid_mask);
  921. }
  922. }
  923. /* The number of GPIOs in the ACPI tables */
  924. len = ret = device_property_count_u16(pctrl->dev, "gpios");
  925. if (ret < 0)
  926. return 0;
  927. if (ret > ngpios)
  928. return -EINVAL;
  929. tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
  930. if (!tmp)
  931. return -ENOMEM;
  932. ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
  933. if (ret < 0) {
  934. dev_err(pctrl->dev, "could not read list of GPIOs\n");
  935. goto out;
  936. }
  937. bitmap_zero(valid_mask, ngpios);
  938. for (i = 0; i < len; i++)
  939. set_bit(tmp[i], valid_mask);
  940. out:
  941. kfree(tmp);
  942. return ret;
  943. }
  944. static const struct gpio_chip msm_gpio_template = {
  945. .direction_input = msm_gpio_direction_input,
  946. .direction_output = msm_gpio_direction_output,
  947. .get_direction = msm_gpio_get_direction,
  948. .get = msm_gpio_get,
  949. .set = msm_gpio_set,
  950. .request = gpiochip_generic_request,
  951. .free = gpiochip_generic_free,
  952. .dbg_show = msm_gpio_dbg_show,
  953. };
  954. /* For dual-edge interrupts in software, since some hardware has no
  955. * such support:
  956. *
  957. * At appropriate moments, this function may be called to flip the polarity
  958. * settings of both-edge irq lines to try and catch the next edge.
  959. *
  960. * The attempt is considered successful if:
  961. * - the status bit goes high, indicating that an edge was caught, or
  962. * - the input value of the gpio doesn't change during the attempt.
  963. * If the value changes twice during the process, that would cause the first
  964. * test to fail but would force the second, as two opposite
  965. * transitions would cause a detection no matter the polarity setting.
  966. *
  967. * The do-loop tries to sledge-hammer closed the timing hole between
  968. * the initial value-read and the polarity-write - if the line value changes
  969. * during that window, an interrupt is lost, the new polarity setting is
  970. * incorrect, and the first success test will fail, causing a retry.
  971. *
  972. * Algorithm comes from Google's msmgpio driver.
  973. */
  974. static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
  975. const struct msm_pingroup *g,
  976. struct irq_data *d)
  977. {
  978. int loop_limit = 100;
  979. unsigned val, val2, intstat;
  980. unsigned pol;
  981. do {
  982. val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  983. pol = msm_readl_intr_cfg(pctrl, g);
  984. pol ^= BIT(g->intr_polarity_bit);
  985. msm_writel_intr_cfg(pol, pctrl, g);
  986. val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  987. intstat = msm_readl_intr_status(pctrl, g);
  988. if (intstat || (val == val2))
  989. return;
  990. } while (loop_limit-- > 0);
  991. dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
  992. val, val2);
  993. }
  994. static void msm_gpio_irq_mask(struct irq_data *d)
  995. {
  996. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  997. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  998. const struct msm_pingroup *g;
  999. unsigned long flags;
  1000. u32 val;
  1001. if (d->parent_data)
  1002. irq_chip_mask_parent(d);
  1003. if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1004. return;
  1005. g = &pctrl->soc->groups[d->hwirq];
  1006. raw_spin_lock_irqsave(&pctrl->lock, flags);
  1007. val = msm_readl_intr_cfg(pctrl, g);
  1008. /*
  1009. * There are two bits that control interrupt forwarding to the CPU. The
  1010. * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
  1011. * latched into the interrupt status register when the hardware detects
  1012. * an irq that it's configured for (either edge for edge type or level
  1013. * for level type irq). The 'non-raw' status enable bit causes the
  1014. * hardware to assert the summary interrupt to the CPU if the latched
  1015. * status bit is set. There's a bug though, the edge detection logic
  1016. * seems to have a problem where toggling the RAW_STATUS_EN bit may
  1017. * cause the status bit to latch spuriously when there isn't any edge
  1018. * so we can't touch that bit for edge type irqs and we have to keep
  1019. * the bit set anyway so that edges are latched while the line is masked.
  1020. *
  1021. * To make matters more complicated, leaving the RAW_STATUS_EN bit
  1022. * enabled all the time causes level interrupts to re-latch into the
  1023. * status register because the level is still present on the line after
  1024. * we ack it. We clear the raw status enable bit during mask here and
  1025. * set the bit on unmask so the interrupt can't latch into the hardware
  1026. * while it's masked.
  1027. */
  1028. if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
  1029. val &= ~BIT(g->intr_raw_status_bit);
  1030. val &= ~BIT(g->intr_enable_bit);
  1031. msm_writel_intr_cfg(val, pctrl, g);
  1032. clear_bit(d->hwirq, pctrl->enabled_irqs);
  1033. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  1034. }
  1035. static void msm_gpio_irq_unmask(struct irq_data *d)
  1036. {
  1037. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1038. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1039. const struct msm_pingroup *g;
  1040. unsigned long flags;
  1041. u32 val;
  1042. if (d->parent_data)
  1043. irq_chip_unmask_parent(d);
  1044. if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1045. return;
  1046. g = &pctrl->soc->groups[d->hwirq];
  1047. raw_spin_lock_irqsave(&pctrl->lock, flags);
  1048. val = msm_readl_intr_cfg(pctrl, g);
  1049. val |= BIT(g->intr_raw_status_bit);
  1050. val |= BIT(g->intr_enable_bit);
  1051. msm_writel_intr_cfg(val, pctrl, g);
  1052. set_bit(d->hwirq, pctrl->enabled_irqs);
  1053. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  1054. }
  1055. static void msm_gpio_dirconn_handler(struct irq_desc *desc)
  1056. {
  1057. struct irq_data *irqd = irq_desc_get_handler_data(desc);
  1058. struct irq_chip *chip = irq_desc_get_chip(desc);
  1059. if (!irqd)
  1060. return;
  1061. chained_irq_enter(chip, desc);
  1062. generic_handle_irq(irqd->irq);
  1063. chained_irq_exit(chip, desc);
  1064. irq_set_irqchip_state(irq_desc_get_irq_data(desc)->irq,
  1065. IRQCHIP_STATE_ACTIVE, 0);
  1066. }
  1067. static void configure_tlmm_dc_polarity(struct irq_data *d,
  1068. unsigned int type, u32 offset)
  1069. {
  1070. const struct msm_pingroup *g;
  1071. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1072. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1073. unsigned int polarity = 1, val;
  1074. unsigned long flags;
  1075. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH)) {
  1076. /*
  1077. * Since the default polarity is set to 0, change it to 1 for
  1078. * Rising edge and active high interrupt type such that the line
  1079. * is not inverted.
  1080. */
  1081. raw_spin_lock_irqsave(&pctrl->lock, flags);
  1082. g = &pctrl->soc->groups[d->hwirq];
  1083. val = readl_relaxed(msm_pinctrl_data->regs[g->tile] +
  1084. g->dir_conn_reg + (offset * 4));
  1085. val |= polarity << 8;
  1086. writel_relaxed(val, msm_pinctrl_data->regs[g->tile] +
  1087. g->dir_conn_reg + (offset * 4));
  1088. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  1089. }
  1090. }
  1091. static void enable_tlmm_dc(irq_hw_number_t irq)
  1092. {
  1093. struct irq_data *dir_conn_data = irq_get_irq_data(irq);
  1094. if (!dir_conn_data || !(dir_conn_data->chip))
  1095. return;
  1096. dir_conn_data->chip->irq_unmask(dir_conn_data);
  1097. }
  1098. static bool is_gpio_tlmm_dc(struct irq_data *d, u32 *offset,
  1099. irq_hw_number_t *irq)
  1100. {
  1101. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1102. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1103. struct msm_dir_conn *dc;
  1104. int i;
  1105. for (i = pctrl->n_dir_conns; i > 0; i--) {
  1106. dc = &pctrl->soc->dir_conn[i];
  1107. *offset = pctrl->n_dir_conns - i;
  1108. *irq = dc->irq;
  1109. if (dc->gpio == d->hwirq)
  1110. return true;
  1111. }
  1112. return false;
  1113. }
  1114. static void msm_gpio_irq_enable(struct irq_data *d)
  1115. {
  1116. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1117. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1118. gpiochip_enable_irq(gc, d->hwirq);
  1119. if (d->parent_data)
  1120. irq_chip_enable_parent(d);
  1121. if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1122. msm_gpio_irq_unmask(d);
  1123. }
  1124. static void msm_gpio_irq_disable(struct irq_data *d)
  1125. {
  1126. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1127. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1128. if (d->parent_data)
  1129. irq_chip_disable_parent(d);
  1130. if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1131. msm_gpio_irq_mask(d);
  1132. gpiochip_disable_irq(gc, d->hwirq);
  1133. }
  1134. /**
  1135. * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent.
  1136. * @d: The irq dta.
  1137. *
  1138. * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are
  1139. * normally handled by the parent irqchip. The logic here is slightly
  1140. * different due to what's easy to do with our parent, but in principle it's
  1141. * the same.
  1142. */
  1143. static void msm_gpio_update_dual_edge_parent(struct irq_data *d)
  1144. {
  1145. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1146. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1147. const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
  1148. int loop_limit = 100;
  1149. unsigned int val;
  1150. unsigned int type;
  1151. /* Read the value and make a guess about what edge we need to catch */
  1152. val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  1153. type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
  1154. do {
  1155. /* Set the parent to catch the next edge */
  1156. irq_chip_set_type_parent(d, type);
  1157. /*
  1158. * Possibly the line changed between when we last read "val"
  1159. * (and decided what edge we needed) and when set the edge.
  1160. * If the value didn't change (or changed and then changed
  1161. * back) then we're done.
  1162. */
  1163. val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  1164. if (type == IRQ_TYPE_EDGE_RISING) {
  1165. if (!val)
  1166. return;
  1167. type = IRQ_TYPE_EDGE_FALLING;
  1168. } else if (type == IRQ_TYPE_EDGE_FALLING) {
  1169. if (val)
  1170. return;
  1171. type = IRQ_TYPE_EDGE_RISING;
  1172. }
  1173. } while (loop_limit-- > 0);
  1174. dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
  1175. }
  1176. static void msm_gpio_irq_ack(struct irq_data *d)
  1177. {
  1178. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1179. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1180. const struct msm_pingroup *g;
  1181. unsigned long flags;
  1182. if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
  1183. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  1184. msm_gpio_update_dual_edge_parent(d);
  1185. return;
  1186. }
  1187. g = &pctrl->soc->groups[d->hwirq];
  1188. raw_spin_lock_irqsave(&pctrl->lock, flags);
  1189. msm_ack_intr_status(pctrl, g);
  1190. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  1191. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  1192. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  1193. }
  1194. static void msm_gpio_irq_eoi(struct irq_data *d)
  1195. {
  1196. d = d->parent_data;
  1197. if (d)
  1198. d->chip->irq_eoi(d);
  1199. }
  1200. static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d,
  1201. unsigned int type)
  1202. {
  1203. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1204. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1205. return type == IRQ_TYPE_EDGE_BOTH &&
  1206. pctrl->soc->wakeirq_dual_edge_errata && d->parent_data &&
  1207. test_bit(d->hwirq, pctrl->skip_wake_irqs);
  1208. }
  1209. static void msm_dirconn_cfg_reg(struct irq_data *d, u32 offset)
  1210. {
  1211. u32 val;
  1212. const struct msm_pingroup *g;
  1213. unsigned long flags;
  1214. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1215. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1216. raw_spin_lock_irqsave(&pctrl->lock, flags);
  1217. g = &pctrl->soc->groups[d->hwirq];
  1218. val = (d->hwirq) & 0xFF;
  1219. writel_relaxed(val, pctrl->regs[g->tile] + g->dir_conn_reg
  1220. + (offset * 4));
  1221. val = msm_readl_intr_cfg(pctrl, g);
  1222. val |= BIT(g->dir_conn_en_bit);
  1223. msm_writel_intr_cfg(val, pctrl, g);
  1224. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  1225. }
  1226. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  1227. {
  1228. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1229. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1230. const struct msm_pingroup *g;
  1231. irq_hw_number_t irq = 0;
  1232. u32 intr_target_mask = GENMASK(2, 0);
  1233. unsigned long flags;
  1234. u32 offset = 0;
  1235. bool was_enabled;
  1236. u32 val;
  1237. if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
  1238. set_bit(d->hwirq, pctrl->dual_edge_irqs);
  1239. irq_set_handler_locked(d, handle_fasteoi_ack_irq);
  1240. msm_gpio_update_dual_edge_parent(d);
  1241. return 0;
  1242. }
  1243. if (d->parent_data)
  1244. irq_chip_set_type_parent(d, type);
  1245. if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
  1246. clear_bit(d->hwirq, pctrl->dual_edge_irqs);
  1247. irq_set_handler_locked(d, handle_fasteoi_irq);
  1248. return 0;
  1249. }
  1250. if (is_gpio_tlmm_dc(d, &offset, &irq) && type != IRQ_TYPE_EDGE_BOTH) {
  1251. configure_tlmm_dc_polarity(d, type, offset);
  1252. enable_tlmm_dc(irq);
  1253. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  1254. irq_set_handler_locked(d, handle_level_irq);
  1255. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  1256. irq_set_handler_locked(d, handle_edge_irq);
  1257. return 0;
  1258. }
  1259. g = &pctrl->soc->groups[d->hwirq];
  1260. raw_spin_lock_irqsave(&pctrl->lock, flags);
  1261. /*
  1262. * For hw without possibility of detecting both edges
  1263. */
  1264. if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
  1265. set_bit(d->hwirq, pctrl->dual_edge_irqs);
  1266. else
  1267. clear_bit(d->hwirq, pctrl->dual_edge_irqs);
  1268. /* Route interrupts to application cpu.
  1269. * With intr_target_use_scm interrupts are routed to
  1270. * application cpu using scm calls.
  1271. */
  1272. if (g->intr_target_width)
  1273. intr_target_mask = GENMASK(g->intr_target_width - 1, 0);
  1274. if (pctrl->intr_target_use_scm) {
  1275. u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
  1276. int ret;
  1277. qcom_scm_io_readl(addr, &val);
  1278. val &= ~(intr_target_mask << g->intr_target_bit);
  1279. val |= g->intr_target_kpss_val << g->intr_target_bit;
  1280. ret = qcom_scm_io_writel(addr, val);
  1281. if (ret)
  1282. dev_err(pctrl->dev,
  1283. "Failed routing %lu interrupt to Apps proc",
  1284. d->hwirq);
  1285. } else {
  1286. val = msm_readl_intr_target(pctrl, g);
  1287. val &= ~(intr_target_mask << g->intr_target_bit);
  1288. val |= g->intr_target_kpss_val << g->intr_target_bit;
  1289. msm_writel_intr_target(val, pctrl, g);
  1290. }
  1291. /* Update configuration for gpio.
  1292. * RAW_STATUS_EN is left on for all gpio irqs. Due to the
  1293. * internal circuitry of TLMM, toggling the RAW_STATUS
  1294. * could cause the INTR_STATUS to be set for EDGE interrupts.
  1295. */
  1296. val = msm_readl_intr_cfg(pctrl, g);
  1297. was_enabled = val & BIT(g->intr_raw_status_bit);
  1298. val |= BIT(g->intr_raw_status_bit);
  1299. if (g->intr_detection_width == 2) {
  1300. val &= ~(3 << g->intr_detection_bit);
  1301. val &= ~(1 << g->intr_polarity_bit);
  1302. switch (type) {
  1303. case IRQ_TYPE_EDGE_RISING:
  1304. val |= 1 << g->intr_detection_bit;
  1305. val |= BIT(g->intr_polarity_bit);
  1306. break;
  1307. case IRQ_TYPE_EDGE_FALLING:
  1308. val |= 2 << g->intr_detection_bit;
  1309. val |= BIT(g->intr_polarity_bit);
  1310. break;
  1311. case IRQ_TYPE_EDGE_BOTH:
  1312. val |= 3 << g->intr_detection_bit;
  1313. val |= BIT(g->intr_polarity_bit);
  1314. break;
  1315. case IRQ_TYPE_LEVEL_LOW:
  1316. break;
  1317. case IRQ_TYPE_LEVEL_HIGH:
  1318. val |= BIT(g->intr_polarity_bit);
  1319. break;
  1320. }
  1321. } else if (g->intr_detection_width == 1) {
  1322. val &= ~(1 << g->intr_detection_bit);
  1323. val &= ~(1 << g->intr_polarity_bit);
  1324. switch (type) {
  1325. case IRQ_TYPE_EDGE_RISING:
  1326. val |= BIT(g->intr_detection_bit);
  1327. val |= BIT(g->intr_polarity_bit);
  1328. break;
  1329. case IRQ_TYPE_EDGE_FALLING:
  1330. val |= BIT(g->intr_detection_bit);
  1331. break;
  1332. case IRQ_TYPE_EDGE_BOTH:
  1333. val |= BIT(g->intr_detection_bit);
  1334. val |= BIT(g->intr_polarity_bit);
  1335. break;
  1336. case IRQ_TYPE_LEVEL_LOW:
  1337. break;
  1338. case IRQ_TYPE_LEVEL_HIGH:
  1339. val |= BIT(g->intr_polarity_bit);
  1340. break;
  1341. }
  1342. } else {
  1343. BUG();
  1344. }
  1345. msm_writel_intr_cfg(val, pctrl, g);
  1346. /*
  1347. * The first time we set RAW_STATUS_EN it could trigger an interrupt.
  1348. * Clear the interrupt. This is safe because we have
  1349. * IRQCHIP_SET_TYPE_MASKED.
  1350. */
  1351. if (!was_enabled)
  1352. msm_ack_intr_status(pctrl, g);
  1353. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  1354. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  1355. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  1356. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  1357. irq_set_handler_locked(d, handle_level_irq);
  1358. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  1359. irq_set_handler_locked(d, handle_edge_irq);
  1360. return 0;
  1361. }
  1362. static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  1363. {
  1364. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1365. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1366. /*
  1367. * While they may not wake up when the TLMM is powered off,
  1368. * some GPIOs would like to wakeup the system from suspend
  1369. * when TLMM is powered on. To allow that, enable the GPIO
  1370. * summary line to be wakeup capable at GIC.
  1371. */
  1372. if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1373. return irq_chip_set_wake_parent(d, on);
  1374. return irq_set_irq_wake(pctrl->irq, on);
  1375. }
  1376. static int msm_gpio_irq_reqres(struct irq_data *d)
  1377. {
  1378. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1379. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1380. int ret;
  1381. if (!try_module_get(gc->owner))
  1382. return -ENODEV;
  1383. ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
  1384. if (ret)
  1385. goto out;
  1386. msm_gpio_direction_input(gc, d->hwirq);
  1387. if (gpiochip_lock_as_irq(gc, d->hwirq)) {
  1388. dev_err(gc->parent,
  1389. "unable to lock HW IRQ %lu for IRQ\n",
  1390. d->hwirq);
  1391. ret = -EINVAL;
  1392. goto out;
  1393. }
  1394. /*
  1395. * The disable / clear-enable workaround we do in msm_pinmux_set_mux()
  1396. * only works if disable is not lazy since we only clear any bogus
  1397. * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
  1398. */
  1399. irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
  1400. return 0;
  1401. out:
  1402. module_put(gc->owner);
  1403. return ret;
  1404. }
  1405. static void msm_gpio_irq_relres(struct irq_data *d)
  1406. {
  1407. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1408. gpiochip_unlock_as_irq(gc, d->hwirq);
  1409. module_put(gc->owner);
  1410. }
  1411. static int msm_gpio_irq_set_affinity(struct irq_data *d,
  1412. const struct cpumask *dest, bool force)
  1413. {
  1414. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1415. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1416. unsigned int i, n_dir_conns = pctrl->n_dir_conns;
  1417. struct irq_data *gpio_irq_data;
  1418. struct msm_dir_conn *dc = NULL;
  1419. for (i = n_dir_conns; i > 0; i--) {
  1420. dc = &pctrl->soc->dir_conn[i];
  1421. gpio_irq_data = irq_get_irq_data(dc->irq);
  1422. if (!gpio_irq_data || !(gpio_irq_data->chip) ||
  1423. !(gpio_irq_data->chip->irq_set_affinity))
  1424. continue;
  1425. if (d->hwirq == dc->gpio)
  1426. return gpio_irq_data->chip->irq_set_affinity(gpio_irq_data, dest, force);
  1427. }
  1428. if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1429. return irq_chip_set_affinity_parent(d, dest, force);
  1430. return -EINVAL;
  1431. }
  1432. static int msm_gpio_irq_set_irqchip_state(struct irq_data *d, enum irqchip_irq_state which,
  1433. bool val)
  1434. {
  1435. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1436. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1437. const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
  1438. if (which != IRQCHIP_STATE_PENDING)
  1439. return -EINVAL;
  1440. if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1441. return -EINVAL;
  1442. msm_writel_intr_status(val, pctrl, g);
  1443. return 0;
  1444. }
  1445. static int msm_gpio_irq_get_irqchip_state(struct irq_data *d, enum irqchip_irq_state which,
  1446. bool *val)
  1447. {
  1448. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1449. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1450. const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
  1451. if (which != IRQCHIP_STATE_PENDING)
  1452. return -EINVAL;
  1453. if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1454. return -EINVAL;
  1455. g = &pctrl->soc->groups[d->hwirq];
  1456. *val = msm_readl_intr_status(pctrl, g);
  1457. return 0;
  1458. }
  1459. static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  1460. {
  1461. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1462. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1463. if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1464. return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
  1465. return -EINVAL;
  1466. }
  1467. static void msm_gpio_irq_handler(struct irq_desc *desc)
  1468. {
  1469. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  1470. const struct msm_pingroup *g;
  1471. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1472. struct irq_chip *chip = irq_desc_get_chip(desc);
  1473. int handled = 0;
  1474. u32 val;
  1475. int i;
  1476. chained_irq_enter(chip, desc);
  1477. /*
  1478. * Each pin has it's own IRQ status register, so use
  1479. * enabled_irq bitmap to limit the number of reads.
  1480. */
  1481. for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
  1482. g = &pctrl->soc->groups[i];
  1483. val = msm_readl_intr_status(pctrl, g);
  1484. if (val & BIT(g->intr_status_bit)) {
  1485. generic_handle_domain_irq(gc->irq.domain, i);
  1486. handled++;
  1487. }
  1488. }
  1489. /* No interrupts were flagged */
  1490. if (handled == 0)
  1491. handle_bad_irq(desc);
  1492. chained_irq_exit(chip, desc);
  1493. }
  1494. static int msm_gpio_wakeirq(struct gpio_chip *gc,
  1495. unsigned int child,
  1496. unsigned int child_type,
  1497. unsigned int *parent,
  1498. unsigned int *parent_type)
  1499. {
  1500. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1501. const struct msm_gpio_wakeirq_map *map;
  1502. const struct msm_pingroup *g;
  1503. u32 intr_cfg;
  1504. int i;
  1505. *parent = GPIO_NO_WAKE_IRQ;
  1506. *parent_type = IRQ_TYPE_EDGE_RISING;
  1507. for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
  1508. map = &pctrl->soc->wakeirq_map[i];
  1509. if (map->gpio == child) {
  1510. *parent = map->wakeirq;
  1511. break;
  1512. }
  1513. }
  1514. /*
  1515. * Additionally set intr_wakeup_enable_bit for the GPIOs Routed
  1516. * to parent irqchip depending on intr_wakeup_present_bit.
  1517. */
  1518. if (*parent != GPIO_NO_WAKE_IRQ) {
  1519. g = &pctrl->soc->groups[child];
  1520. if (!g->intr_wakeup_present_bit)
  1521. return 0;
  1522. intr_cfg = msm_readl_intr_cfg(pctrl, g);
  1523. if (intr_cfg & BIT(g->intr_wakeup_present_bit)) {
  1524. intr_cfg |= BIT(g->intr_wakeup_enable_bit);
  1525. msm_writel_intr_cfg(intr_cfg, pctrl, g);
  1526. }
  1527. }
  1528. return 0;
  1529. }
  1530. static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
  1531. {
  1532. bool have_reserved, have_gpios;
  1533. if (pctrl->soc->reserved_gpios)
  1534. return true;
  1535. have_reserved = of_property_count_u32_elems(pctrl->dev->of_node, "qcom,gpios-reserved") > 0;
  1536. have_gpios = device_property_count_u16(pctrl->dev, "gpios") > 0;
  1537. if (have_reserved && have_gpios)
  1538. dev_warn(pctrl->dev, "qcom,gpios-reserved and gpios are both defined. Only one should be used.\n");
  1539. return have_reserved || have_gpios;
  1540. }
  1541. static const struct irq_chip msm_gpio_irq_chip = {
  1542. .name = "msmgpio",
  1543. .irq_enable = msm_gpio_irq_enable,
  1544. .irq_disable = msm_gpio_irq_disable,
  1545. .irq_mask = msm_gpio_irq_mask,
  1546. .irq_unmask = msm_gpio_irq_unmask,
  1547. .irq_ack = msm_gpio_irq_ack,
  1548. .irq_eoi = msm_gpio_irq_eoi,
  1549. .irq_set_type = msm_gpio_irq_set_type,
  1550. .irq_set_wake = msm_gpio_irq_set_wake,
  1551. .irq_request_resources = msm_gpio_irq_reqres,
  1552. .irq_release_resources = msm_gpio_irq_relres,
  1553. .irq_set_affinity = msm_gpio_irq_set_affinity,
  1554. .irq_set_irqchip_state = msm_gpio_irq_set_irqchip_state,
  1555. .irq_get_irqchip_state = msm_gpio_irq_get_irqchip_state,
  1556. .irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity,
  1557. .flags = (IRQCHIP_MASK_ON_SUSPEND |
  1558. IRQCHIP_SET_TYPE_MASKED |
  1559. IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND |
  1560. IRQCHIP_IMMUTABLE),
  1561. };
  1562. static int msm_gpio_init(struct msm_pinctrl *pctrl)
  1563. {
  1564. struct gpio_chip *chip;
  1565. struct gpio_irq_chip *girq;
  1566. int i, ret;
  1567. unsigned gpio, ngpio = pctrl->soc->ngpios;
  1568. struct device_node *np;
  1569. bool skip;
  1570. if (WARN_ON(ngpio > MAX_NR_GPIO))
  1571. return -EINVAL;
  1572. chip = &pctrl->chip;
  1573. chip->base = -1;
  1574. chip->ngpio = ngpio;
  1575. chip->label = dev_name(pctrl->dev);
  1576. chip->parent = pctrl->dev;
  1577. chip->owner = THIS_MODULE;
  1578. if (msm_gpio_needs_valid_mask(pctrl))
  1579. chip->init_valid_mask = msm_gpio_init_valid_mask;
  1580. np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
  1581. if (np) {
  1582. chip->irq.parent_domain = irq_find_matching_host(np,
  1583. DOMAIN_BUS_WAKEUP);
  1584. of_node_put(np);
  1585. if (!chip->irq.parent_domain)
  1586. return -EPROBE_DEFER;
  1587. chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
  1588. /*
  1589. * Let's skip handling the GPIOs, if the parent irqchip
  1590. * is handling the direct connect IRQ of the GPIO.
  1591. */
  1592. skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
  1593. for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
  1594. gpio = pctrl->soc->wakeirq_map[i].gpio;
  1595. set_bit(gpio, pctrl->skip_wake_irqs);
  1596. }
  1597. }
  1598. girq = &chip->irq;
  1599. gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip);
  1600. girq->parent_handler = msm_gpio_irq_handler;
  1601. girq->fwnode = pctrl->dev->fwnode;
  1602. girq->num_parents = 1;
  1603. girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
  1604. GFP_KERNEL);
  1605. if (!girq->parents)
  1606. return -ENOMEM;
  1607. girq->default_type = IRQ_TYPE_NONE;
  1608. girq->handler = handle_bad_irq;
  1609. girq->parents[0] = pctrl->irq;
  1610. ret = gpiochip_add_data(&pctrl->chip, pctrl);
  1611. if (ret) {
  1612. dev_err(pctrl->dev, "Failed register gpiochip\n");
  1613. return ret;
  1614. }
  1615. /*
  1616. * For DeviceTree-supported systems, the gpio core checks the
  1617. * pinctrl's device node for the "gpio-ranges" property.
  1618. * If it is present, it takes care of adding the pin ranges
  1619. * for the driver. In this case the driver can skip ahead.
  1620. *
  1621. * In order to remain compatible with older, existing DeviceTree
  1622. * files which don't set the "gpio-ranges" property or systems that
  1623. * utilize ACPI the driver has to call gpiochip_add_pin_range().
  1624. */
  1625. if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
  1626. ret = gpiochip_add_pin_range(&pctrl->chip,
  1627. dev_name(pctrl->dev), 0, 0, chip->ngpio);
  1628. if (ret) {
  1629. dev_err(pctrl->dev, "Failed to add pin range\n");
  1630. gpiochip_remove(&pctrl->chip);
  1631. return ret;
  1632. }
  1633. }
  1634. return 0;
  1635. }
  1636. static void msm_gpio_setup_dir_connects(struct msm_pinctrl *pctrl)
  1637. {
  1638. struct msm_dir_conn *dc = NULL;
  1639. unsigned int i, n_dir_conns = pctrl->n_dir_conns, offset = 0;
  1640. irq_hw_number_t dirconn_irq = 0, gpio_irq = 0;
  1641. struct irq_data *gpio_irq_data;
  1642. struct irq_domain *child_domain = pctrl->chip.irq.domain;
  1643. for (i = n_dir_conns; i > 0; i--) {
  1644. dc = &pctrl->soc->dir_conn[i];
  1645. dirconn_irq = dc->irq;
  1646. offset = n_dir_conns - i;
  1647. if (dc->gpio == -1)
  1648. continue;
  1649. gpio_irq = irq_create_mapping(child_domain, dc->gpio);
  1650. irq_set_parent(gpio_irq, dirconn_irq);
  1651. irq_set_chip_data(gpio_irq, &(pctrl->chip));
  1652. irq_set_chip_and_handler_name(gpio_irq, &msm_gpio_irq_chip, NULL, NULL);
  1653. gpio_irq_data = irq_get_irq_data(gpio_irq);
  1654. if (!gpio_irq_data)
  1655. continue;
  1656. irq_set_handler_data(dirconn_irq, gpio_irq_data);
  1657. msm_dirconn_cfg_reg(gpio_irq_data, offset);
  1658. }
  1659. }
  1660. static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
  1661. void *data)
  1662. {
  1663. struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
  1664. writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
  1665. mdelay(1000);
  1666. return NOTIFY_DONE;
  1667. }
  1668. static struct msm_pinctrl *poweroff_pctrl;
  1669. static void msm_ps_hold_poweroff(void)
  1670. {
  1671. msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
  1672. }
  1673. static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
  1674. {
  1675. int i;
  1676. const struct msm_function *func = pctrl->soc->functions;
  1677. for (i = 0; i < pctrl->soc->nfunctions; i++)
  1678. if (!strcmp(func[i].name, "ps_hold")) {
  1679. pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
  1680. pctrl->restart_nb.priority = 128;
  1681. if (register_restart_handler(&pctrl->restart_nb))
  1682. dev_err(pctrl->dev,
  1683. "failed to setup restart handler.\n");
  1684. poweroff_pctrl = pctrl;
  1685. pm_power_off = msm_ps_hold_poweroff;
  1686. break;
  1687. }
  1688. }
  1689. #ifdef CONFIG_HIBERNATION
  1690. static int pinctrl_hibernation_notifier(struct notifier_block *nb,
  1691. unsigned long event, void *dummy)
  1692. {
  1693. struct msm_pinctrl *pctrl = msm_pinctrl_data;
  1694. const struct msm_pinctrl_soc_data *soc = pctrl->soc;
  1695. if (event == PM_HIBERNATION_PREPARE) {
  1696. pctrl->gpio_regs = kcalloc(soc->ngroups,
  1697. sizeof(*pctrl->gpio_regs), GFP_KERNEL);
  1698. if (pctrl->gpio_regs == NULL)
  1699. return -ENOMEM;
  1700. if (soc->ntiles) {
  1701. pctrl->msm_tile_regs = kcalloc(soc->ntiles,
  1702. sizeof(*pctrl->msm_tile_regs), GFP_KERNEL);
  1703. if (pctrl->msm_tile_regs == NULL) {
  1704. kfree(pctrl->gpio_regs);
  1705. return -ENOMEM;
  1706. }
  1707. }
  1708. pctrl->hibernation = true;
  1709. } else if (event == PM_POST_HIBERNATION) {
  1710. kfree(pctrl->gpio_regs);
  1711. kfree(pctrl->msm_tile_regs);
  1712. pctrl->gpio_regs = NULL;
  1713. pctrl->msm_tile_regs = NULL;
  1714. pctrl->hibernation = false;
  1715. }
  1716. return NOTIFY_OK;
  1717. }
  1718. static struct notifier_block pinctrl_notif_block = {
  1719. .notifier_call = pinctrl_hibernation_notifier,
  1720. };
  1721. static int msm_pinctrl_hibernation_suspend(void)
  1722. {
  1723. const struct msm_pingroup *pgroup;
  1724. struct msm_pinctrl *pctrl = msm_pinctrl_data;
  1725. const struct msm_pinctrl_soc_data *soc = pctrl->soc;
  1726. void __iomem *tile_addr = NULL;
  1727. u32 i, j;
  1728. if (likely(!pctrl->hibernation))
  1729. return 0;
  1730. /* Save direction conn registers for hmss */
  1731. for (i = 0; i < soc->ntiles; i++) {
  1732. if (soc->tiles)
  1733. tile_addr = pctrl->regs[i] + soc->dir_conn_addr[i];
  1734. else
  1735. tile_addr = pctrl->regs[0] + soc->dir_conn_addr[i];
  1736. pr_err("The tile addr generated is 0x%lx\n", (u64)tile_addr);
  1737. for (j = 0; j < 8; j++)
  1738. pctrl->msm_tile_regs[i].dir_con_regs[j] =
  1739. readl_relaxed(tile_addr + j*4);
  1740. }
  1741. /* All normal gpios will have common registers, first save them */
  1742. for (i = 0; i < soc->ngpios; i++) {
  1743. pgroup = &soc->groups[i];
  1744. pctrl->gpio_regs[i].ctl_reg =
  1745. msm_readl_ctl(pctrl, pgroup);
  1746. pctrl->gpio_regs[i].io_reg =
  1747. msm_readl_io(pctrl, pgroup);
  1748. if (pgroup->intr_cfg_reg)
  1749. pctrl->gpio_regs[i].intr_cfg_reg =
  1750. msm_readl_intr_cfg(pctrl, pgroup);
  1751. if (pgroup->intr_status_reg)
  1752. pctrl->gpio_regs[i].intr_status_reg =
  1753. msm_readl_intr_status(pctrl, pgroup);
  1754. }
  1755. for ( ; i < soc->ngroups; i++) {
  1756. pgroup = &soc->groups[i];
  1757. if (pgroup->ctl_reg)
  1758. pctrl->gpio_regs[i].ctl_reg =
  1759. msm_readl_ctl(pctrl, pgroup);
  1760. if (pgroup->io_reg)
  1761. pctrl->gpio_regs[i].io_reg =
  1762. msm_readl_io(pctrl, pgroup);
  1763. }
  1764. return 0;
  1765. }
  1766. static void msm_pinctrl_hibernation_resume(void)
  1767. {
  1768. u32 i, j;
  1769. const struct msm_pingroup *pgroup;
  1770. struct msm_pinctrl *pctrl = msm_pinctrl_data;
  1771. const struct msm_pinctrl_soc_data *soc = pctrl->soc;
  1772. void __iomem *tile_addr = NULL;
  1773. if (likely(!pctrl->hibernation) || !pctrl->gpio_regs || !pctrl->msm_tile_regs)
  1774. return;
  1775. for (i = 0; i < soc->ntiles; i++) {
  1776. if (soc->tiles)
  1777. tile_addr = pctrl->regs[i] + soc->dir_conn_addr[i];
  1778. else
  1779. tile_addr = pctrl->regs[0] + soc->dir_conn_addr[i];
  1780. pr_err("The tile addr generated is 0x%lx\n", (u64)tile_addr);
  1781. for (j = 0; j < 8; j++)
  1782. writel_relaxed(pctrl->msm_tile_regs[i].dir_con_regs[j],
  1783. tile_addr + j*4);
  1784. }
  1785. /* Restore normal gpios */
  1786. for (i = 0; i < soc->ngpios; i++) {
  1787. pgroup = &soc->groups[i];
  1788. msm_writel_ctl(pctrl->gpio_regs[i].ctl_reg, pctrl, pgroup);
  1789. msm_writel_io(pctrl->gpio_regs[i].io_reg, pctrl, pgroup);
  1790. if (pgroup->intr_cfg_reg)
  1791. msm_writel_intr_cfg(pctrl->gpio_regs[i].intr_cfg_reg,
  1792. pctrl, pgroup);
  1793. if (pgroup->intr_status_reg)
  1794. msm_writel_intr_status(pctrl->gpio_regs[i].intr_status_reg,
  1795. pctrl, pgroup);
  1796. }
  1797. for ( ; i < soc->ngroups; i++) {
  1798. pgroup = &soc->groups[i];
  1799. if (pgroup->ctl_reg)
  1800. msm_writel_ctl(pctrl->gpio_regs[i].ctl_reg,
  1801. pctrl, pgroup);
  1802. if (pgroup->io_reg)
  1803. msm_writel_io(pctrl->gpio_regs[i].io_reg,
  1804. pctrl, pgroup);
  1805. }
  1806. }
  1807. static struct syscore_ops msm_pinctrl_pm_ops = {
  1808. .suspend = msm_pinctrl_hibernation_suspend,
  1809. .resume = msm_pinctrl_hibernation_resume,
  1810. };
  1811. #endif
  1812. static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
  1813. {
  1814. struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
  1815. return pinctrl_force_sleep(pctrl->pctrl);
  1816. }
  1817. static __maybe_unused int msm_pinctrl_resume(struct device *dev)
  1818. {
  1819. struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
  1820. return pinctrl_force_default(pctrl->pctrl);
  1821. }
  1822. SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
  1823. msm_pinctrl_resume);
  1824. EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
  1825. void debug_pintctrl_msm_enable(void)
  1826. {
  1827. pinctrl_msm_log_mask = true;
  1828. }
  1829. EXPORT_SYMBOL(debug_pintctrl_msm_enable);
  1830. void debug_pintctrl_msm_disable(void)
  1831. {
  1832. pinctrl_msm_log_mask = false;
  1833. }
  1834. EXPORT_SYMBOL(debug_pintctrl_msm_disable);
  1835. static __maybe_unused int noirq_msm_pinctrl_suspend(struct device *dev)
  1836. {
  1837. struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
  1838. if (pinctrl_msm_log_mask) {
  1839. printk_deferred("%s\n", pctrl->chip.label);
  1840. msm_gpios_status(&pctrl->chip);
  1841. }
  1842. return 0;
  1843. }
  1844. const struct dev_pm_ops noirq_msm_pinctrl_dev_pm_ops = {
  1845. .suspend_noirq = noirq_msm_pinctrl_suspend,
  1846. };
  1847. EXPORT_SYMBOL(noirq_msm_pinctrl_dev_pm_ops);
  1848. /*
  1849. * msm_gpio_mpm_wake_set - API to make interrupt wakeup capable
  1850. * @dev: Device corrsponding to pinctrl
  1851. * @gpio: Gpio number to make interrupt wakeup capable
  1852. * @enable: Enable/Disable wakeup capability
  1853. */
  1854. int msm_gpio_mpm_wake_set(unsigned int gpio, bool enable)
  1855. {
  1856. const struct msm_pingroup *g;
  1857. unsigned long flags;
  1858. u32 val, intr_cfg;
  1859. g = &msm_pinctrl_data->soc->groups[gpio];
  1860. if (g->wake_bit == -1 && !g->intr_wakeup_present_bit)
  1861. return -ENOENT;
  1862. raw_spin_lock_irqsave(&msm_pinctrl_data->lock, flags);
  1863. intr_cfg = msm_readl_intr_cfg(msm_pinctrl_data, g);
  1864. if ((intr_cfg & BIT(g->intr_wakeup_present_bit)) &&
  1865. test_bit(gpio, msm_pinctrl_data->skip_wake_irqs)) {
  1866. if (enable)
  1867. intr_cfg |= BIT(g->intr_wakeup_enable_bit);
  1868. else
  1869. intr_cfg &= ~BIT(g->intr_wakeup_enable_bit);
  1870. msm_writel_intr_cfg(intr_cfg, msm_pinctrl_data, g);
  1871. goto exit;
  1872. }
  1873. val = readl_relaxed(msm_pinctrl_data->regs[g->tile] + g->wake_reg);
  1874. if (enable)
  1875. val |= BIT(g->wake_bit);
  1876. else
  1877. val &= ~BIT(g->wake_bit);
  1878. writel_relaxed(val, msm_pinctrl_data->regs[g->tile] + g->wake_reg);
  1879. exit:
  1880. raw_spin_unlock_irqrestore(&msm_pinctrl_data->lock, flags);
  1881. return 0;
  1882. }
  1883. EXPORT_SYMBOL(msm_gpio_mpm_wake_set);
  1884. /*
  1885. * msm_gpio_get_pin_address - API to get GPIO physical address range
  1886. * @gpio_num: global GPIO num, as returned from gpio apis - desc_to_gpio().
  1887. * @res: Out param. Resource struct with start/end addr populated.
  1888. * @ret: true if the pin is valid, false otherwise.
  1889. */
  1890. bool msm_gpio_get_pin_address(unsigned int gpio_num, struct resource *res)
  1891. {
  1892. struct gpio_chip *chip;
  1893. const struct msm_pingroup *g;
  1894. struct resource *tile_res;
  1895. struct platform_device *pdev;
  1896. unsigned int gpio;
  1897. if (!msm_pinctrl_data) {
  1898. pr_err("pinctrl data is not available\n");
  1899. return false;
  1900. }
  1901. chip = &msm_pinctrl_data->chip;
  1902. pdev = to_platform_device(msm_pinctrl_data->dev);
  1903. gpio = gpio_num - chip->base;
  1904. if (!gpiochip_line_is_valid(chip, gpio))
  1905. return false;
  1906. if (!res)
  1907. return false;
  1908. g = &msm_pinctrl_data->soc->groups[gpio];
  1909. if (msm_pinctrl_data->soc->tiles)
  1910. tile_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1911. msm_pinctrl_data->soc->tiles[g->tile]);
  1912. else
  1913. tile_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1914. if (!tile_res) {
  1915. dev_err(&pdev->dev, "failed to get resource\n");
  1916. return false;
  1917. }
  1918. res->start = tile_res->start + g->ctl_reg;
  1919. res->end = res->start + (g->reg_size_4k ? : DEFAULT_REG_SIZE_4K) * SZ_4K - 1;
  1920. return true;
  1921. }
  1922. EXPORT_SYMBOL(msm_gpio_get_pin_address);
  1923. int msm_qup_write(u32 mode, u32 val)
  1924. {
  1925. int i;
  1926. struct pinctrl_qup *regs = msm_pinctrl_data->soc->qup_regs;
  1927. int num_regs = msm_pinctrl_data->soc->nqup_regs;
  1928. /*Iterate over modes*/
  1929. for (i = 0; i < num_regs; i++) {
  1930. if (regs[i].mode == mode) {
  1931. writel_relaxed(val & QUP_MASK,
  1932. msm_pinctrl_data->regs[0] + regs[i].offset);
  1933. return 0;
  1934. }
  1935. }
  1936. return -ENOENT;
  1937. }
  1938. EXPORT_SYMBOL(msm_qup_write);
  1939. int msm_qup_read(unsigned int mode)
  1940. {
  1941. int i, val;
  1942. struct pinctrl_qup *regs = msm_pinctrl_data->soc->qup_regs;
  1943. int num_regs = msm_pinctrl_data->soc->nqup_regs;
  1944. /*Iterate over modes*/
  1945. for (i = 0; i < num_regs; i++) {
  1946. if (regs[i].mode == mode) {
  1947. val = readl_relaxed(msm_pinctrl_data->regs[0] +
  1948. regs[i].offset);
  1949. return val & QUP_MASK;
  1950. }
  1951. }
  1952. return -ENOENT;
  1953. }
  1954. EXPORT_SYMBOL(msm_qup_read);
  1955. int msm_spare_write(int spare_reg, u32 val)
  1956. {
  1957. u32 offset;
  1958. const struct msm_spare_tlmm *regs = msm_pinctrl_data->soc->spare_regs;
  1959. int num_regs = msm_pinctrl_data->soc->nspare_regs;
  1960. if (!regs || spare_reg >= num_regs)
  1961. return -ENOENT;
  1962. offset = regs[spare_reg].offset;
  1963. if (offset != 0) {
  1964. writel_relaxed(val & SPARE_MASK,
  1965. msm_pinctrl_data->regs[0] + offset);
  1966. return 0;
  1967. }
  1968. return -ENOENT;
  1969. }
  1970. EXPORT_SYMBOL(msm_spare_write);
  1971. int msm_spare_read(int spare_reg)
  1972. {
  1973. u32 offset, val;
  1974. const struct msm_spare_tlmm *regs = msm_pinctrl_data->soc->spare_regs;
  1975. int num_regs = msm_pinctrl_data->soc->nspare_regs;
  1976. if (!regs || spare_reg >= num_regs)
  1977. return -ENOENT;
  1978. offset = regs[spare_reg].offset;
  1979. if (offset != 0) {
  1980. val = readl_relaxed(msm_pinctrl_data->regs[0] + offset);
  1981. return val & SPARE_MASK;
  1982. }
  1983. return -ENOENT;
  1984. }
  1985. EXPORT_SYMBOL(msm_spare_read);
  1986. int msm_pinctrl_probe(struct platform_device *pdev,
  1987. const struct msm_pinctrl_soc_data *soc_data)
  1988. {
  1989. struct msm_pinctrl *pctrl;
  1990. struct resource *res;
  1991. int ret, i, num_irq, irq;
  1992. msm_pinctrl_data = pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  1993. if (!pctrl)
  1994. return -ENOMEM;
  1995. pctrl->hibernation = false;
  1996. pctrl->dev = &pdev->dev;
  1997. pctrl->soc = soc_data;
  1998. pctrl->chip = msm_gpio_template;
  1999. pctrl->intr_target_use_scm = of_device_is_compatible(
  2000. pctrl->dev->of_node,
  2001. "qcom,ipq8064-pinctrl");
  2002. raw_spin_lock_init(&pctrl->lock);
  2003. if (soc_data->tiles) {
  2004. for (i = 0; i < soc_data->ntiles; i++) {
  2005. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  2006. soc_data->tiles[i]);
  2007. pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
  2008. if (IS_ERR(pctrl->regs[i]))
  2009. return PTR_ERR(pctrl->regs[i]);
  2010. }
  2011. } else {
  2012. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2013. if (!res)
  2014. return -ENOENT;
  2015. pctrl->regs[0] = devm_ioremap_resource(&pdev->dev, res);
  2016. if (IS_ERR(pctrl->regs[0]))
  2017. return PTR_ERR(pctrl->regs[0]);
  2018. pctrl->phys_base[0] = res->start;
  2019. }
  2020. msm_pinctrl_setup_pm_reset(pctrl);
  2021. pctrl->irq = platform_get_irq(pdev, 0);
  2022. if (pctrl->irq < 0)
  2023. return pctrl->irq;
  2024. pctrl->desc.owner = THIS_MODULE;
  2025. pctrl->desc.pctlops = &msm_pinctrl_ops;
  2026. pctrl->desc.pmxops = &msm_pinmux_ops;
  2027. pctrl->desc.confops = &msm_pinconf_ops;
  2028. pctrl->desc.name = dev_name(&pdev->dev);
  2029. pctrl->desc.pins = pctrl->soc->pins;
  2030. pctrl->desc.npins = pctrl->soc->npins;
  2031. pctrl->desc.num_custom_params = ARRAY_SIZE(msm_gpio_bindings);
  2032. pctrl->desc.custom_params = msm_gpio_bindings;
  2033. pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
  2034. if (IS_ERR(pctrl->pctrl)) {
  2035. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  2036. return PTR_ERR(pctrl->pctrl);
  2037. }
  2038. ret = msm_gpio_init(pctrl);
  2039. if (ret)
  2040. return ret;
  2041. num_irq = platform_irq_count(pdev);
  2042. for (i = 1; i < num_irq; i++) {
  2043. struct msm_dir_conn *dc = &soc_data->dir_conn[i];
  2044. irq = platform_get_irq(pdev, i);
  2045. dc->irq = irq;
  2046. __irq_set_handler(irq, msm_gpio_dirconn_handler, false, NULL);
  2047. irq_set_irq_type(irq, IRQ_TYPE_EDGE_RISING);
  2048. pctrl->n_dir_conns++;
  2049. }
  2050. msm_gpio_setup_dir_connects(pctrl);
  2051. pinctrl_msm_log_mask = false;
  2052. platform_set_drvdata(pdev, pctrl);
  2053. #ifdef CONFIG_HIBERNATION
  2054. register_syscore_ops(&msm_pinctrl_pm_ops);
  2055. ret = register_pm_notifier(&pinctrl_notif_block);
  2056. if (ret)
  2057. return ret;
  2058. #endif
  2059. dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
  2060. return 0;
  2061. }
  2062. EXPORT_SYMBOL(msm_pinctrl_probe);
  2063. int msm_pinctrl_remove(struct platform_device *pdev)
  2064. {
  2065. struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
  2066. gpiochip_remove(&pctrl->chip);
  2067. unregister_restart_handler(&pctrl->restart_nb);
  2068. return 0;
  2069. }
  2070. EXPORT_SYMBOL(msm_pinctrl_remove);
  2071. MODULE_SOFTDEP("pre: qcom-pdc");
  2072. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. pinctrl-msm driver");
  2073. MODULE_LICENSE("GPL v2");