pinctrl-msm.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013, Sony Mobile Communications AB.
  4. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/err.h>
  8. #include <linux/gpio/driver.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/log2.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm.h>
  16. #include <linux/firmware/qcom/qcom_scm.h>
  17. #include <linux/reboot.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/pinctrl/machine.h>
  22. #include <linux/pinctrl/pinconf-generic.h>
  23. #include <linux/pinctrl/pinconf.h>
  24. #include <linux/pinctrl/pinmux.h>
  25. #include <linux/soc/qcom/irq.h>
  26. #include "../core.h"
  27. #include "../pinconf.h"
  28. #include "../pinctrl-utils.h"
  29. #include "pinctrl-msm.h"
  30. #define MAX_NR_GPIO 300
  31. #define MAX_NR_TILES 4
  32. #define PS_HOLD_OFFSET 0x820
  33. /**
  34. * struct msm_pinctrl - state for a pinctrl-msm device
  35. * @dev: device handle.
  36. * @pctrl: pinctrl handle.
  37. * @chip: gpiochip handle.
  38. * @desc: pin controller descriptor
  39. * @irq: parent irq for the TLMM irq_chip.
  40. * @intr_target_use_scm: route irq to application cpu using scm calls
  41. * @lock: Spinlock to protect register resources as well
  42. * as msm_pinctrl data structures.
  43. * @enabled_irqs: Bitmap of currently enabled irqs.
  44. * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
  45. * detection.
  46. * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
  47. * @disabled_for_mux: These IRQs were disabled because we muxed away.
  48. * @ever_gpio: This bit is set the first time we mux a pin to gpio_func.
  49. * @soc: Reference to soc_data of platform specific data.
  50. * @regs: Base addresses for the TLMM tiles.
  51. * @phys_base: Physical base address
  52. */
  53. struct msm_pinctrl {
  54. struct device *dev;
  55. struct pinctrl_dev *pctrl;
  56. struct gpio_chip chip;
  57. struct pinctrl_desc desc;
  58. int irq;
  59. bool intr_target_use_scm;
  60. raw_spinlock_t lock;
  61. DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
  62. DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
  63. DECLARE_BITMAP(skip_wake_irqs, MAX_NR_GPIO);
  64. DECLARE_BITMAP(disabled_for_mux, MAX_NR_GPIO);
  65. DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
  66. const struct msm_pinctrl_soc_data *soc;
  67. void __iomem *regs[MAX_NR_TILES];
  68. u32 phys_base[MAX_NR_TILES];
  69. };
  70. #define MSM_ACCESSOR(name) \
  71. static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
  72. const struct msm_pingroup *g) \
  73. { \
  74. return readl(pctrl->regs[g->tile] + g->name##_reg); \
  75. } \
  76. static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
  77. const struct msm_pingroup *g) \
  78. { \
  79. writel(val, pctrl->regs[g->tile] + g->name##_reg); \
  80. }
  81. MSM_ACCESSOR(ctl)
  82. MSM_ACCESSOR(io)
  83. MSM_ACCESSOR(intr_cfg)
  84. MSM_ACCESSOR(intr_status)
  85. MSM_ACCESSOR(intr_target)
  86. static void msm_ack_intr_status(struct msm_pinctrl *pctrl,
  87. const struct msm_pingroup *g)
  88. {
  89. u32 val = g->intr_ack_high ? BIT(g->intr_status_bit) : 0;
  90. msm_writel_intr_status(val, pctrl, g);
  91. }
  92. static int msm_get_groups_count(struct pinctrl_dev *pctldev)
  93. {
  94. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  95. return pctrl->soc->ngroups;
  96. }
  97. static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
  98. unsigned group)
  99. {
  100. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  101. return pctrl->soc->groups[group].grp.name;
  102. }
  103. static int msm_get_group_pins(struct pinctrl_dev *pctldev,
  104. unsigned group,
  105. const unsigned **pins,
  106. unsigned *num_pins)
  107. {
  108. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  109. *pins = pctrl->soc->groups[group].grp.pins;
  110. *num_pins = pctrl->soc->groups[group].grp.npins;
  111. return 0;
  112. }
  113. static const struct pinctrl_ops msm_pinctrl_ops = {
  114. .get_groups_count = msm_get_groups_count,
  115. .get_group_name = msm_get_group_name,
  116. .get_group_pins = msm_get_group_pins,
  117. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  118. .dt_free_map = pinctrl_utils_free_map,
  119. };
  120. static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
  121. {
  122. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  123. struct gpio_chip *chip = &pctrl->chip;
  124. return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
  125. }
  126. static int msm_get_functions_count(struct pinctrl_dev *pctldev)
  127. {
  128. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  129. return pctrl->soc->nfunctions;
  130. }
  131. static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
  132. unsigned function)
  133. {
  134. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  135. return pctrl->soc->functions[function].name;
  136. }
  137. static int msm_get_function_groups(struct pinctrl_dev *pctldev,
  138. unsigned function,
  139. const char * const **groups,
  140. unsigned * const num_groups)
  141. {
  142. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  143. *groups = pctrl->soc->functions[function].groups;
  144. *num_groups = pctrl->soc->functions[function].ngroups;
  145. return 0;
  146. }
  147. static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
  148. unsigned function,
  149. unsigned group)
  150. {
  151. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  152. struct gpio_chip *gc = &pctrl->chip;
  153. unsigned int irq = irq_find_mapping(gc->irq.domain, group);
  154. struct irq_data *d = irq_get_irq_data(irq);
  155. unsigned int gpio_func = pctrl->soc->gpio_func;
  156. unsigned int egpio_func = pctrl->soc->egpio_func;
  157. const struct msm_pingroup *g;
  158. unsigned long flags;
  159. u32 val, mask;
  160. int i;
  161. g = &pctrl->soc->groups[group];
  162. mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
  163. for (i = 0; i < g->nfuncs; i++) {
  164. if (g->funcs[i] == function)
  165. break;
  166. }
  167. if (WARN_ON(i == g->nfuncs))
  168. return -EINVAL;
  169. /*
  170. * If an GPIO interrupt is setup on this pin then we need special
  171. * handling. Specifically interrupt detection logic will still see
  172. * the pin twiddle even when we're muxed away.
  173. *
  174. * When we see a pin with an interrupt setup on it then we'll disable
  175. * (mask) interrupts on it when we mux away until we mux back. Note
  176. * that disable_irq() refcounts and interrupts are disabled as long as
  177. * at least one disable_irq() has been called.
  178. */
  179. if (d && i != gpio_func &&
  180. !test_and_set_bit(d->hwirq, pctrl->disabled_for_mux))
  181. disable_irq(irq);
  182. raw_spin_lock_irqsave(&pctrl->lock, flags);
  183. val = msm_readl_ctl(pctrl, g);
  184. /*
  185. * If this is the first time muxing to GPIO and the direction is
  186. * output, make sure that we're not going to be glitching the pin
  187. * by reading the current state of the pin and setting it as the
  188. * output.
  189. */
  190. if (i == gpio_func && (val & BIT(g->oe_bit)) &&
  191. !test_and_set_bit(group, pctrl->ever_gpio)) {
  192. u32 io_val = msm_readl_io(pctrl, g);
  193. if (io_val & BIT(g->in_bit)) {
  194. if (!(io_val & BIT(g->out_bit)))
  195. msm_writel_io(io_val | BIT(g->out_bit), pctrl, g);
  196. } else {
  197. if (io_val & BIT(g->out_bit))
  198. msm_writel_io(io_val & ~BIT(g->out_bit), pctrl, g);
  199. }
  200. }
  201. if (egpio_func && i == egpio_func) {
  202. if (val & BIT(g->egpio_present))
  203. val &= ~BIT(g->egpio_enable);
  204. } else {
  205. val &= ~mask;
  206. val |= i << g->mux_bit;
  207. /* Claim ownership of pin if egpio capable */
  208. if (egpio_func && val & BIT(g->egpio_present))
  209. val |= BIT(g->egpio_enable);
  210. }
  211. msm_writel_ctl(val, pctrl, g);
  212. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  213. if (d && i == gpio_func &&
  214. test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) {
  215. /*
  216. * Clear interrupts detected while not GPIO since we only
  217. * masked things.
  218. */
  219. if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
  220. irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
  221. else
  222. msm_ack_intr_status(pctrl, g);
  223. enable_irq(irq);
  224. }
  225. return 0;
  226. }
  227. static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
  228. struct pinctrl_gpio_range *range,
  229. unsigned offset)
  230. {
  231. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  232. const struct msm_pingroup *g = &pctrl->soc->groups[offset];
  233. /* No funcs? Probably ACPI so can't do anything here */
  234. if (!g->nfuncs)
  235. return 0;
  236. return msm_pinmux_set_mux(pctldev, g->funcs[pctrl->soc->gpio_func], offset);
  237. }
  238. static const struct pinmux_ops msm_pinmux_ops = {
  239. .request = msm_pinmux_request,
  240. .get_functions_count = msm_get_functions_count,
  241. .get_function_name = msm_get_function_name,
  242. .get_function_groups = msm_get_function_groups,
  243. .gpio_request_enable = msm_pinmux_request_gpio,
  244. .set_mux = msm_pinmux_set_mux,
  245. };
  246. static int msm_config_reg(struct msm_pinctrl *pctrl,
  247. const struct msm_pingroup *g,
  248. unsigned param,
  249. unsigned *mask,
  250. unsigned *bit)
  251. {
  252. switch (param) {
  253. case PIN_CONFIG_BIAS_DISABLE:
  254. case PIN_CONFIG_BIAS_PULL_DOWN:
  255. case PIN_CONFIG_BIAS_BUS_HOLD:
  256. case PIN_CONFIG_BIAS_PULL_UP:
  257. *bit = g->pull_bit;
  258. *mask = 3;
  259. if (g->i2c_pull_bit)
  260. *mask |= BIT(g->i2c_pull_bit) >> *bit;
  261. break;
  262. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  263. *bit = g->od_bit;
  264. *mask = 1;
  265. break;
  266. case PIN_CONFIG_DRIVE_STRENGTH:
  267. *bit = g->drv_bit;
  268. *mask = 7;
  269. break;
  270. case PIN_CONFIG_OUTPUT:
  271. case PIN_CONFIG_INPUT_ENABLE:
  272. case PIN_CONFIG_OUTPUT_ENABLE:
  273. *bit = g->oe_bit;
  274. *mask = 1;
  275. break;
  276. default:
  277. return -ENOTSUPP;
  278. }
  279. return 0;
  280. }
  281. #define MSM_NO_PULL 0
  282. #define MSM_PULL_DOWN 1
  283. #define MSM_KEEPER 2
  284. #define MSM_PULL_UP_NO_KEEPER 2
  285. #define MSM_PULL_UP 3
  286. #define MSM_I2C_STRONG_PULL_UP 2200
  287. static unsigned msm_regval_to_drive(u32 val)
  288. {
  289. return (val + 1) * 2;
  290. }
  291. static int msm_config_group_get(struct pinctrl_dev *pctldev,
  292. unsigned int group,
  293. unsigned long *config)
  294. {
  295. const struct msm_pingroup *g;
  296. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  297. unsigned param = pinconf_to_config_param(*config);
  298. unsigned mask;
  299. unsigned arg;
  300. unsigned bit;
  301. int ret;
  302. u32 val;
  303. /* Pin information can only be requested from valid pin groups */
  304. if (!gpiochip_line_is_valid(&pctrl->chip, group))
  305. return -EINVAL;
  306. g = &pctrl->soc->groups[group];
  307. ret = msm_config_reg(pctrl, g, param, &mask, &bit);
  308. if (ret < 0)
  309. return ret;
  310. val = msm_readl_ctl(pctrl, g);
  311. arg = (val >> bit) & mask;
  312. /* Convert register value to pinconf value */
  313. switch (param) {
  314. case PIN_CONFIG_BIAS_DISABLE:
  315. if (arg != MSM_NO_PULL)
  316. return -EINVAL;
  317. arg = 1;
  318. break;
  319. case PIN_CONFIG_BIAS_PULL_DOWN:
  320. if (arg != MSM_PULL_DOWN)
  321. return -EINVAL;
  322. arg = 1;
  323. break;
  324. case PIN_CONFIG_BIAS_BUS_HOLD:
  325. if (pctrl->soc->pull_no_keeper)
  326. return -ENOTSUPP;
  327. if (arg != MSM_KEEPER)
  328. return -EINVAL;
  329. arg = 1;
  330. break;
  331. case PIN_CONFIG_BIAS_PULL_UP:
  332. if (pctrl->soc->pull_no_keeper)
  333. arg = arg == MSM_PULL_UP_NO_KEEPER;
  334. else if (arg & BIT(g->i2c_pull_bit))
  335. arg = MSM_I2C_STRONG_PULL_UP;
  336. else
  337. arg = arg == MSM_PULL_UP;
  338. if (!arg)
  339. return -EINVAL;
  340. break;
  341. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  342. /* Pin is not open-drain */
  343. if (!arg)
  344. return -EINVAL;
  345. arg = 1;
  346. break;
  347. case PIN_CONFIG_DRIVE_STRENGTH:
  348. arg = msm_regval_to_drive(arg);
  349. break;
  350. case PIN_CONFIG_OUTPUT:
  351. /* Pin is not output */
  352. if (!arg)
  353. return -EINVAL;
  354. val = msm_readl_io(pctrl, g);
  355. arg = !!(val & BIT(g->in_bit));
  356. break;
  357. case PIN_CONFIG_OUTPUT_ENABLE:
  358. if (!arg)
  359. return -EINVAL;
  360. break;
  361. default:
  362. return -ENOTSUPP;
  363. }
  364. *config = pinconf_to_config_packed(param, arg);
  365. return 0;
  366. }
  367. static int msm_config_group_set(struct pinctrl_dev *pctldev,
  368. unsigned group,
  369. unsigned long *configs,
  370. unsigned num_configs)
  371. {
  372. const struct msm_pingroup *g;
  373. struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  374. unsigned long flags;
  375. unsigned param;
  376. unsigned mask;
  377. unsigned arg;
  378. unsigned bit;
  379. int ret;
  380. u32 val;
  381. int i;
  382. g = &pctrl->soc->groups[group];
  383. for (i = 0; i < num_configs; i++) {
  384. param = pinconf_to_config_param(configs[i]);
  385. arg = pinconf_to_config_argument(configs[i]);
  386. ret = msm_config_reg(pctrl, g, param, &mask, &bit);
  387. if (ret < 0)
  388. return ret;
  389. /* Convert pinconf values to register values */
  390. switch (param) {
  391. case PIN_CONFIG_BIAS_DISABLE:
  392. arg = MSM_NO_PULL;
  393. break;
  394. case PIN_CONFIG_BIAS_PULL_DOWN:
  395. arg = MSM_PULL_DOWN;
  396. break;
  397. case PIN_CONFIG_BIAS_BUS_HOLD:
  398. if (pctrl->soc->pull_no_keeper)
  399. return -ENOTSUPP;
  400. arg = MSM_KEEPER;
  401. break;
  402. case PIN_CONFIG_BIAS_PULL_UP:
  403. if (pctrl->soc->pull_no_keeper)
  404. arg = MSM_PULL_UP_NO_KEEPER;
  405. else if (g->i2c_pull_bit && arg == MSM_I2C_STRONG_PULL_UP)
  406. arg = BIT(g->i2c_pull_bit) | MSM_PULL_UP;
  407. else
  408. arg = MSM_PULL_UP;
  409. break;
  410. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  411. arg = 1;
  412. break;
  413. case PIN_CONFIG_DRIVE_STRENGTH:
  414. /* Check for invalid values */
  415. if (arg > 16 || arg < 2 || (arg % 2) != 0)
  416. arg = -1;
  417. else
  418. arg = (arg / 2) - 1;
  419. break;
  420. case PIN_CONFIG_OUTPUT:
  421. /* set output value */
  422. raw_spin_lock_irqsave(&pctrl->lock, flags);
  423. val = msm_readl_io(pctrl, g);
  424. if (arg)
  425. val |= BIT(g->out_bit);
  426. else
  427. val &= ~BIT(g->out_bit);
  428. msm_writel_io(val, pctrl, g);
  429. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  430. /* enable output */
  431. arg = 1;
  432. break;
  433. case PIN_CONFIG_INPUT_ENABLE:
  434. /*
  435. * According to pinctrl documentation this should
  436. * actually be a no-op.
  437. *
  438. * The docs are explicit that "this does not affect
  439. * the pin's ability to drive output" but what we do
  440. * here is to modify the output enable bit. Thus, to
  441. * follow the docs we should remove that.
  442. *
  443. * The docs say that we should enable any relevant
  444. * input buffer, but TLMM there is no input buffer that
  445. * can be enabled/disabled. It's always on.
  446. *
  447. * The points above, explain why this _should_ be a
  448. * no-op. However, for historical reasons and to
  449. * support old device trees, we'll violate the docs
  450. * and still affect the output.
  451. *
  452. * It should further be noted that this old historical
  453. * behavior actually overrides arg to 0. That means
  454. * that "input-enable" and "input-disable" in a device
  455. * tree would _both_ disable the output. We'll
  456. * continue to preserve this behavior as well since
  457. * we have no other use for this attribute.
  458. */
  459. arg = 0;
  460. break;
  461. case PIN_CONFIG_OUTPUT_ENABLE:
  462. arg = !!arg;
  463. break;
  464. default:
  465. dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
  466. param);
  467. return -EINVAL;
  468. }
  469. /* Range-check user-supplied value */
  470. if (arg & ~mask) {
  471. dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
  472. return -EINVAL;
  473. }
  474. raw_spin_lock_irqsave(&pctrl->lock, flags);
  475. val = msm_readl_ctl(pctrl, g);
  476. val &= ~(mask << bit);
  477. val |= arg << bit;
  478. msm_writel_ctl(val, pctrl, g);
  479. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  480. }
  481. return 0;
  482. }
  483. static const struct pinconf_ops msm_pinconf_ops = {
  484. .is_generic = true,
  485. .pin_config_group_get = msm_config_group_get,
  486. .pin_config_group_set = msm_config_group_set,
  487. };
  488. static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  489. {
  490. const struct msm_pingroup *g;
  491. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  492. unsigned long flags;
  493. u32 val;
  494. g = &pctrl->soc->groups[offset];
  495. raw_spin_lock_irqsave(&pctrl->lock, flags);
  496. val = msm_readl_ctl(pctrl, g);
  497. val &= ~BIT(g->oe_bit);
  498. msm_writel_ctl(val, pctrl, g);
  499. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  500. return 0;
  501. }
  502. static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  503. {
  504. const struct msm_pingroup *g;
  505. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  506. unsigned long flags;
  507. u32 val;
  508. g = &pctrl->soc->groups[offset];
  509. raw_spin_lock_irqsave(&pctrl->lock, flags);
  510. val = msm_readl_io(pctrl, g);
  511. if (value)
  512. val |= BIT(g->out_bit);
  513. else
  514. val &= ~BIT(g->out_bit);
  515. msm_writel_io(val, pctrl, g);
  516. val = msm_readl_ctl(pctrl, g);
  517. val |= BIT(g->oe_bit);
  518. msm_writel_ctl(val, pctrl, g);
  519. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  520. return 0;
  521. }
  522. static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  523. {
  524. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  525. const struct msm_pingroup *g;
  526. u32 val;
  527. g = &pctrl->soc->groups[offset];
  528. val = msm_readl_ctl(pctrl, g);
  529. return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT :
  530. GPIO_LINE_DIRECTION_IN;
  531. }
  532. static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
  533. {
  534. const struct msm_pingroup *g;
  535. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  536. u32 val;
  537. g = &pctrl->soc->groups[offset];
  538. val = msm_readl_io(pctrl, g);
  539. return !!(val & BIT(g->in_bit));
  540. }
  541. static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  542. {
  543. const struct msm_pingroup *g;
  544. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  545. unsigned long flags;
  546. u32 val;
  547. g = &pctrl->soc->groups[offset];
  548. raw_spin_lock_irqsave(&pctrl->lock, flags);
  549. val = msm_readl_io(pctrl, g);
  550. if (value)
  551. val |= BIT(g->out_bit);
  552. else
  553. val &= ~BIT(g->out_bit);
  554. msm_writel_io(val, pctrl, g);
  555. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  556. }
  557. #ifdef CONFIG_DEBUG_FS
  558. static void msm_gpio_dbg_show_one(struct seq_file *s,
  559. struct pinctrl_dev *pctldev,
  560. struct gpio_chip *chip,
  561. unsigned offset,
  562. unsigned gpio)
  563. {
  564. const struct msm_pingroup *g;
  565. struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
  566. unsigned func;
  567. int is_out;
  568. int drive;
  569. int pull;
  570. int val;
  571. int egpio_enable;
  572. u32 ctl_reg, io_reg;
  573. static const char * const pulls_keeper[] = {
  574. "no pull",
  575. "pull down",
  576. "keeper",
  577. "pull up"
  578. };
  579. static const char * const pulls_no_keeper[] = {
  580. "no pull",
  581. "pull down",
  582. "pull up",
  583. };
  584. if (!gpiochip_line_is_valid(chip, offset))
  585. return;
  586. g = &pctrl->soc->groups[offset];
  587. ctl_reg = msm_readl_ctl(pctrl, g);
  588. io_reg = msm_readl_io(pctrl, g);
  589. is_out = !!(ctl_reg & BIT(g->oe_bit));
  590. func = (ctl_reg >> g->mux_bit) & 7;
  591. drive = (ctl_reg >> g->drv_bit) & 7;
  592. pull = (ctl_reg >> g->pull_bit) & 3;
  593. egpio_enable = 0;
  594. if (pctrl->soc->egpio_func && ctl_reg & BIT(g->egpio_present))
  595. egpio_enable = !(ctl_reg & BIT(g->egpio_enable));
  596. if (is_out)
  597. val = !!(io_reg & BIT(g->out_bit));
  598. else
  599. val = !!(io_reg & BIT(g->in_bit));
  600. if (egpio_enable) {
  601. seq_printf(s, " %-8s: egpio\n", g->grp.name);
  602. return;
  603. }
  604. seq_printf(s, " %-8s: %-3s", g->grp.name, is_out ? "out" : "in");
  605. seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
  606. seq_printf(s, " %dmA", msm_regval_to_drive(drive));
  607. if (pctrl->soc->pull_no_keeper)
  608. seq_printf(s, " %s", pulls_no_keeper[pull]);
  609. else
  610. seq_printf(s, " %s", pulls_keeper[pull]);
  611. seq_puts(s, "\n");
  612. }
  613. static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  614. {
  615. unsigned gpio = chip->base;
  616. unsigned i;
  617. for (i = 0; i < chip->ngpio; i++, gpio++)
  618. msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  619. }
  620. #else
  621. #define msm_gpio_dbg_show NULL
  622. #endif
  623. static int msm_gpio_init_valid_mask(struct gpio_chip *gc,
  624. unsigned long *valid_mask,
  625. unsigned int ngpios)
  626. {
  627. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  628. int ret;
  629. unsigned int len, i;
  630. const int *reserved = pctrl->soc->reserved_gpios;
  631. u16 *tmp;
  632. /* Remove driver-provided reserved GPIOs from valid_mask */
  633. if (reserved) {
  634. for (i = 0; reserved[i] >= 0; i++) {
  635. if (i >= ngpios || reserved[i] >= ngpios) {
  636. dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
  637. return -EINVAL;
  638. }
  639. clear_bit(reserved[i], valid_mask);
  640. }
  641. return 0;
  642. }
  643. /* The number of GPIOs in the ACPI tables */
  644. len = ret = device_property_count_u16(pctrl->dev, "gpios");
  645. if (ret < 0)
  646. return 0;
  647. if (ret > ngpios)
  648. return -EINVAL;
  649. tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
  650. if (!tmp)
  651. return -ENOMEM;
  652. ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
  653. if (ret < 0) {
  654. dev_err(pctrl->dev, "could not read list of GPIOs\n");
  655. goto out;
  656. }
  657. bitmap_zero(valid_mask, ngpios);
  658. for (i = 0; i < len; i++)
  659. set_bit(tmp[i], valid_mask);
  660. out:
  661. kfree(tmp);
  662. return ret;
  663. }
  664. static const struct gpio_chip msm_gpio_template = {
  665. .direction_input = msm_gpio_direction_input,
  666. .direction_output = msm_gpio_direction_output,
  667. .get_direction = msm_gpio_get_direction,
  668. .get = msm_gpio_get,
  669. .set = msm_gpio_set,
  670. .request = gpiochip_generic_request,
  671. .free = gpiochip_generic_free,
  672. .dbg_show = msm_gpio_dbg_show,
  673. };
  674. /* For dual-edge interrupts in software, since some hardware has no
  675. * such support:
  676. *
  677. * At appropriate moments, this function may be called to flip the polarity
  678. * settings of both-edge irq lines to try and catch the next edge.
  679. *
  680. * The attempt is considered successful if:
  681. * - the status bit goes high, indicating that an edge was caught, or
  682. * - the input value of the gpio doesn't change during the attempt.
  683. * If the value changes twice during the process, that would cause the first
  684. * test to fail but would force the second, as two opposite
  685. * transitions would cause a detection no matter the polarity setting.
  686. *
  687. * The do-loop tries to sledge-hammer closed the timing hole between
  688. * the initial value-read and the polarity-write - if the line value changes
  689. * during that window, an interrupt is lost, the new polarity setting is
  690. * incorrect, and the first success test will fail, causing a retry.
  691. *
  692. * Algorithm comes from Google's msmgpio driver.
  693. */
  694. static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
  695. const struct msm_pingroup *g,
  696. struct irq_data *d)
  697. {
  698. int loop_limit = 100;
  699. unsigned val, val2, intstat;
  700. unsigned pol;
  701. do {
  702. val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  703. pol = msm_readl_intr_cfg(pctrl, g);
  704. pol ^= BIT(g->intr_polarity_bit);
  705. msm_writel_intr_cfg(pol, pctrl, g);
  706. val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  707. intstat = msm_readl_intr_status(pctrl, g);
  708. if (intstat || (val == val2))
  709. return;
  710. } while (loop_limit-- > 0);
  711. dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
  712. val, val2);
  713. }
  714. static void msm_gpio_irq_mask(struct irq_data *d)
  715. {
  716. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  717. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  718. const struct msm_pingroup *g;
  719. unsigned long flags;
  720. u32 val;
  721. if (d->parent_data)
  722. irq_chip_mask_parent(d);
  723. if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
  724. return;
  725. g = &pctrl->soc->groups[d->hwirq];
  726. raw_spin_lock_irqsave(&pctrl->lock, flags);
  727. val = msm_readl_intr_cfg(pctrl, g);
  728. /*
  729. * There are two bits that control interrupt forwarding to the CPU. The
  730. * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
  731. * latched into the interrupt status register when the hardware detects
  732. * an irq that it's configured for (either edge for edge type or level
  733. * for level type irq). The 'non-raw' status enable bit causes the
  734. * hardware to assert the summary interrupt to the CPU if the latched
  735. * status bit is set. There's a bug though, the edge detection logic
  736. * seems to have a problem where toggling the RAW_STATUS_EN bit may
  737. * cause the status bit to latch spuriously when there isn't any edge
  738. * so we can't touch that bit for edge type irqs and we have to keep
  739. * the bit set anyway so that edges are latched while the line is masked.
  740. *
  741. * To make matters more complicated, leaving the RAW_STATUS_EN bit
  742. * enabled all the time causes level interrupts to re-latch into the
  743. * status register because the level is still present on the line after
  744. * we ack it. We clear the raw status enable bit during mask here and
  745. * set the bit on unmask so the interrupt can't latch into the hardware
  746. * while it's masked.
  747. */
  748. if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
  749. val &= ~BIT(g->intr_raw_status_bit);
  750. val &= ~BIT(g->intr_enable_bit);
  751. msm_writel_intr_cfg(val, pctrl, g);
  752. clear_bit(d->hwirq, pctrl->enabled_irqs);
  753. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  754. }
  755. static void msm_gpio_irq_unmask(struct irq_data *d)
  756. {
  757. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  758. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  759. const struct msm_pingroup *g;
  760. unsigned long flags;
  761. u32 val;
  762. if (d->parent_data)
  763. irq_chip_unmask_parent(d);
  764. if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
  765. return;
  766. g = &pctrl->soc->groups[d->hwirq];
  767. raw_spin_lock_irqsave(&pctrl->lock, flags);
  768. val = msm_readl_intr_cfg(pctrl, g);
  769. val |= BIT(g->intr_raw_status_bit);
  770. val |= BIT(g->intr_enable_bit);
  771. msm_writel_intr_cfg(val, pctrl, g);
  772. set_bit(d->hwirq, pctrl->enabled_irqs);
  773. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  774. }
  775. static void msm_gpio_irq_enable(struct irq_data *d)
  776. {
  777. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  778. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  779. gpiochip_enable_irq(gc, d->hwirq);
  780. if (d->parent_data)
  781. irq_chip_enable_parent(d);
  782. if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
  783. msm_gpio_irq_unmask(d);
  784. }
  785. static void msm_gpio_irq_disable(struct irq_data *d)
  786. {
  787. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  788. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  789. if (d->parent_data)
  790. irq_chip_disable_parent(d);
  791. if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
  792. msm_gpio_irq_mask(d);
  793. gpiochip_disable_irq(gc, d->hwirq);
  794. }
  795. /**
  796. * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent.
  797. * @d: The irq dta.
  798. *
  799. * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are
  800. * normally handled by the parent irqchip. The logic here is slightly
  801. * different due to what's easy to do with our parent, but in principle it's
  802. * the same.
  803. */
  804. static void msm_gpio_update_dual_edge_parent(struct irq_data *d)
  805. {
  806. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  807. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  808. const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
  809. int loop_limit = 100;
  810. unsigned int val;
  811. unsigned int type;
  812. /* Read the value and make a guess about what edge we need to catch */
  813. val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  814. type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
  815. do {
  816. /* Set the parent to catch the next edge */
  817. irq_chip_set_type_parent(d, type);
  818. /*
  819. * Possibly the line changed between when we last read "val"
  820. * (and decided what edge we needed) and when set the edge.
  821. * If the value didn't change (or changed and then changed
  822. * back) then we're done.
  823. */
  824. val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
  825. if (type == IRQ_TYPE_EDGE_RISING) {
  826. if (!val)
  827. return;
  828. type = IRQ_TYPE_EDGE_FALLING;
  829. } else if (type == IRQ_TYPE_EDGE_FALLING) {
  830. if (val)
  831. return;
  832. type = IRQ_TYPE_EDGE_RISING;
  833. }
  834. } while (loop_limit-- > 0);
  835. dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
  836. }
  837. static void msm_gpio_irq_ack(struct irq_data *d)
  838. {
  839. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  840. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  841. const struct msm_pingroup *g;
  842. unsigned long flags;
  843. if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
  844. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  845. msm_gpio_update_dual_edge_parent(d);
  846. return;
  847. }
  848. g = &pctrl->soc->groups[d->hwirq];
  849. raw_spin_lock_irqsave(&pctrl->lock, flags);
  850. msm_ack_intr_status(pctrl, g);
  851. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  852. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  853. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  854. }
  855. static void msm_gpio_irq_eoi(struct irq_data *d)
  856. {
  857. d = d->parent_data;
  858. if (d)
  859. d->chip->irq_eoi(d);
  860. }
  861. static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d,
  862. unsigned int type)
  863. {
  864. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  865. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  866. return type == IRQ_TYPE_EDGE_BOTH &&
  867. pctrl->soc->wakeirq_dual_edge_errata && d->parent_data &&
  868. test_bit(d->hwirq, pctrl->skip_wake_irqs);
  869. }
  870. static void msm_gpio_irq_init_valid_mask(struct gpio_chip *gc,
  871. unsigned long *valid_mask,
  872. unsigned int ngpios)
  873. {
  874. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  875. const struct msm_pingroup *g;
  876. int i;
  877. bitmap_fill(valid_mask, ngpios);
  878. for (i = 0; i < ngpios; i++) {
  879. g = &pctrl->soc->groups[i];
  880. if (g->intr_detection_width != 1 &&
  881. g->intr_detection_width != 2)
  882. clear_bit(i, valid_mask);
  883. }
  884. }
  885. static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  886. {
  887. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  888. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  889. const struct msm_pingroup *g;
  890. u32 intr_target_mask = GENMASK(2, 0);
  891. unsigned long flags;
  892. u32 val, oldval;
  893. if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
  894. set_bit(d->hwirq, pctrl->dual_edge_irqs);
  895. irq_set_handler_locked(d, handle_fasteoi_ack_irq);
  896. msm_gpio_update_dual_edge_parent(d);
  897. return 0;
  898. }
  899. if (d->parent_data)
  900. irq_chip_set_type_parent(d, type);
  901. if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
  902. clear_bit(d->hwirq, pctrl->dual_edge_irqs);
  903. irq_set_handler_locked(d, handle_fasteoi_irq);
  904. return 0;
  905. }
  906. g = &pctrl->soc->groups[d->hwirq];
  907. raw_spin_lock_irqsave(&pctrl->lock, flags);
  908. /*
  909. * For hw without possibility of detecting both edges
  910. */
  911. if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
  912. set_bit(d->hwirq, pctrl->dual_edge_irqs);
  913. else
  914. clear_bit(d->hwirq, pctrl->dual_edge_irqs);
  915. /* Route interrupts to application cpu.
  916. * With intr_target_use_scm interrupts are routed to
  917. * application cpu using scm calls.
  918. */
  919. if (g->intr_target_width)
  920. intr_target_mask = GENMASK(g->intr_target_width - 1, 0);
  921. if (pctrl->intr_target_use_scm) {
  922. u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
  923. int ret;
  924. qcom_scm_io_readl(addr, &val);
  925. val &= ~(intr_target_mask << g->intr_target_bit);
  926. val |= g->intr_target_kpss_val << g->intr_target_bit;
  927. ret = qcom_scm_io_writel(addr, val);
  928. if (ret)
  929. dev_err(pctrl->dev,
  930. "Failed routing %lu interrupt to Apps proc",
  931. d->hwirq);
  932. } else {
  933. val = msm_readl_intr_target(pctrl, g);
  934. val &= ~(intr_target_mask << g->intr_target_bit);
  935. val |= g->intr_target_kpss_val << g->intr_target_bit;
  936. msm_writel_intr_target(val, pctrl, g);
  937. }
  938. /* Update configuration for gpio.
  939. * RAW_STATUS_EN is left on for all gpio irqs. Due to the
  940. * internal circuitry of TLMM, toggling the RAW_STATUS
  941. * could cause the INTR_STATUS to be set for EDGE interrupts.
  942. */
  943. val = oldval = msm_readl_intr_cfg(pctrl, g);
  944. val |= BIT(g->intr_raw_status_bit);
  945. if (g->intr_detection_width == 2) {
  946. val &= ~(3 << g->intr_detection_bit);
  947. val &= ~(1 << g->intr_polarity_bit);
  948. switch (type) {
  949. case IRQ_TYPE_EDGE_RISING:
  950. val |= 1 << g->intr_detection_bit;
  951. val |= BIT(g->intr_polarity_bit);
  952. break;
  953. case IRQ_TYPE_EDGE_FALLING:
  954. val |= 2 << g->intr_detection_bit;
  955. val |= BIT(g->intr_polarity_bit);
  956. break;
  957. case IRQ_TYPE_EDGE_BOTH:
  958. val |= 3 << g->intr_detection_bit;
  959. val |= BIT(g->intr_polarity_bit);
  960. break;
  961. case IRQ_TYPE_LEVEL_LOW:
  962. break;
  963. case IRQ_TYPE_LEVEL_HIGH:
  964. val |= BIT(g->intr_polarity_bit);
  965. break;
  966. }
  967. } else if (g->intr_detection_width == 1) {
  968. val &= ~(1 << g->intr_detection_bit);
  969. val &= ~(1 << g->intr_polarity_bit);
  970. switch (type) {
  971. case IRQ_TYPE_EDGE_RISING:
  972. val |= BIT(g->intr_detection_bit);
  973. val |= BIT(g->intr_polarity_bit);
  974. break;
  975. case IRQ_TYPE_EDGE_FALLING:
  976. val |= BIT(g->intr_detection_bit);
  977. break;
  978. case IRQ_TYPE_EDGE_BOTH:
  979. val |= BIT(g->intr_detection_bit);
  980. val |= BIT(g->intr_polarity_bit);
  981. break;
  982. case IRQ_TYPE_LEVEL_LOW:
  983. break;
  984. case IRQ_TYPE_LEVEL_HIGH:
  985. val |= BIT(g->intr_polarity_bit);
  986. break;
  987. }
  988. } else {
  989. BUG();
  990. }
  991. msm_writel_intr_cfg(val, pctrl, g);
  992. /*
  993. * The first time we set RAW_STATUS_EN it could trigger an interrupt.
  994. * Clear the interrupt. This is safe because we have
  995. * IRQCHIP_SET_TYPE_MASKED. When changing the interrupt type, we could
  996. * also still have a non-matching interrupt latched, so clear whenever
  997. * making changes to the interrupt configuration.
  998. */
  999. if (val != oldval)
  1000. msm_ack_intr_status(pctrl, g);
  1001. if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
  1002. msm_gpio_update_dual_edge_pos(pctrl, g, d);
  1003. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  1004. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  1005. irq_set_handler_locked(d, handle_level_irq);
  1006. else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
  1007. irq_set_handler_locked(d, handle_edge_irq);
  1008. return 0;
  1009. }
  1010. static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
  1011. {
  1012. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1013. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1014. /*
  1015. * While they may not wake up when the TLMM is powered off,
  1016. * some GPIOs would like to wakeup the system from suspend
  1017. * when TLMM is powered on. To allow that, enable the GPIO
  1018. * summary line to be wakeup capable at GIC.
  1019. */
  1020. if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1021. return irq_chip_set_wake_parent(d, on);
  1022. return irq_set_irq_wake(pctrl->irq, on);
  1023. }
  1024. static int msm_gpio_irq_reqres(struct irq_data *d)
  1025. {
  1026. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1027. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1028. const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
  1029. unsigned long flags;
  1030. int ret;
  1031. if (!try_module_get(gc->owner))
  1032. return -ENODEV;
  1033. ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
  1034. if (ret)
  1035. goto out;
  1036. msm_gpio_direction_input(gc, d->hwirq);
  1037. if (gpiochip_lock_as_irq(gc, d->hwirq)) {
  1038. dev_err(gc->parent,
  1039. "unable to lock HW IRQ %lu for IRQ\n",
  1040. d->hwirq);
  1041. ret = -EINVAL;
  1042. goto out;
  1043. }
  1044. /*
  1045. * The disable / clear-enable workaround we do in msm_pinmux_set_mux()
  1046. * only works if disable is not lazy since we only clear any bogus
  1047. * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
  1048. */
  1049. irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
  1050. /*
  1051. * If the wakeup_enable bit is present and marked as available for the
  1052. * requested GPIO, it should be enabled when the GPIO is marked as
  1053. * wake irq in order to allow the interrupt event to be transfered to
  1054. * the PDC HW.
  1055. * While the name implies only the wakeup event, it's also required for
  1056. * the interrupt event.
  1057. */
  1058. if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) {
  1059. u32 intr_cfg;
  1060. raw_spin_lock_irqsave(&pctrl->lock, flags);
  1061. intr_cfg = msm_readl_intr_cfg(pctrl, g);
  1062. if (intr_cfg & BIT(g->intr_wakeup_present_bit)) {
  1063. intr_cfg |= BIT(g->intr_wakeup_enable_bit);
  1064. msm_writel_intr_cfg(intr_cfg, pctrl, g);
  1065. }
  1066. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  1067. }
  1068. return 0;
  1069. out:
  1070. module_put(gc->owner);
  1071. return ret;
  1072. }
  1073. static void msm_gpio_irq_relres(struct irq_data *d)
  1074. {
  1075. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1076. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1077. const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
  1078. unsigned long flags;
  1079. /* Disable the wakeup_enable bit if it has been set in msm_gpio_irq_reqres() */
  1080. if (test_bit(d->hwirq, pctrl->skip_wake_irqs) && g->intr_wakeup_present_bit) {
  1081. u32 intr_cfg;
  1082. raw_spin_lock_irqsave(&pctrl->lock, flags);
  1083. intr_cfg = msm_readl_intr_cfg(pctrl, g);
  1084. if (intr_cfg & BIT(g->intr_wakeup_present_bit)) {
  1085. intr_cfg &= ~BIT(g->intr_wakeup_enable_bit);
  1086. msm_writel_intr_cfg(intr_cfg, pctrl, g);
  1087. }
  1088. raw_spin_unlock_irqrestore(&pctrl->lock, flags);
  1089. }
  1090. gpiochip_unlock_as_irq(gc, d->hwirq);
  1091. module_put(gc->owner);
  1092. }
  1093. static int msm_gpio_irq_set_affinity(struct irq_data *d,
  1094. const struct cpumask *dest, bool force)
  1095. {
  1096. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1097. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1098. if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1099. return irq_chip_set_affinity_parent(d, dest, force);
  1100. return -EINVAL;
  1101. }
  1102. static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
  1103. {
  1104. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  1105. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1106. if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
  1107. return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
  1108. return -EINVAL;
  1109. }
  1110. static void msm_gpio_irq_handler(struct irq_desc *desc)
  1111. {
  1112. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  1113. const struct msm_pingroup *g;
  1114. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1115. struct irq_chip *chip = irq_desc_get_chip(desc);
  1116. int handled = 0;
  1117. u32 val;
  1118. int i;
  1119. chained_irq_enter(chip, desc);
  1120. /*
  1121. * Each pin has it's own IRQ status register, so use
  1122. * enabled_irq bitmap to limit the number of reads.
  1123. */
  1124. for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
  1125. g = &pctrl->soc->groups[i];
  1126. val = msm_readl_intr_status(pctrl, g);
  1127. if (val & BIT(g->intr_status_bit)) {
  1128. generic_handle_domain_irq(gc->irq.domain, i);
  1129. handled++;
  1130. }
  1131. }
  1132. /* No interrupts were flagged */
  1133. if (handled == 0)
  1134. handle_bad_irq(desc);
  1135. chained_irq_exit(chip, desc);
  1136. }
  1137. static int msm_gpio_wakeirq(struct gpio_chip *gc,
  1138. unsigned int child,
  1139. unsigned int child_type,
  1140. unsigned int *parent,
  1141. unsigned int *parent_type)
  1142. {
  1143. struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
  1144. const struct msm_gpio_wakeirq_map *map;
  1145. int i;
  1146. *parent = GPIO_NO_WAKE_IRQ;
  1147. *parent_type = IRQ_TYPE_EDGE_RISING;
  1148. for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
  1149. map = &pctrl->soc->wakeirq_map[i];
  1150. if (map->gpio == child) {
  1151. *parent = map->wakeirq;
  1152. break;
  1153. }
  1154. }
  1155. return 0;
  1156. }
  1157. static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
  1158. {
  1159. if (pctrl->soc->reserved_gpios)
  1160. return true;
  1161. return device_property_count_u16(pctrl->dev, "gpios") > 0;
  1162. }
  1163. static const struct irq_chip msm_gpio_irq_chip = {
  1164. .name = "msmgpio",
  1165. .irq_enable = msm_gpio_irq_enable,
  1166. .irq_disable = msm_gpio_irq_disable,
  1167. .irq_mask = msm_gpio_irq_mask,
  1168. .irq_unmask = msm_gpio_irq_unmask,
  1169. .irq_ack = msm_gpio_irq_ack,
  1170. .irq_eoi = msm_gpio_irq_eoi,
  1171. .irq_set_type = msm_gpio_irq_set_type,
  1172. .irq_set_wake = msm_gpio_irq_set_wake,
  1173. .irq_request_resources = msm_gpio_irq_reqres,
  1174. .irq_release_resources = msm_gpio_irq_relres,
  1175. .irq_set_affinity = msm_gpio_irq_set_affinity,
  1176. .irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity,
  1177. .flags = (IRQCHIP_MASK_ON_SUSPEND |
  1178. IRQCHIP_SET_TYPE_MASKED |
  1179. IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND |
  1180. IRQCHIP_IMMUTABLE),
  1181. };
  1182. static int msm_gpio_init(struct msm_pinctrl *pctrl)
  1183. {
  1184. struct gpio_chip *chip;
  1185. struct gpio_irq_chip *girq;
  1186. int i, ret;
  1187. unsigned gpio, ngpio = pctrl->soc->ngpios;
  1188. struct device_node *np;
  1189. bool skip;
  1190. if (WARN_ON(ngpio > MAX_NR_GPIO))
  1191. return -EINVAL;
  1192. chip = &pctrl->chip;
  1193. chip->base = -1;
  1194. chip->ngpio = ngpio;
  1195. chip->label = dev_name(pctrl->dev);
  1196. chip->parent = pctrl->dev;
  1197. chip->owner = THIS_MODULE;
  1198. if (msm_gpio_needs_valid_mask(pctrl))
  1199. chip->init_valid_mask = msm_gpio_init_valid_mask;
  1200. np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
  1201. if (np) {
  1202. chip->irq.parent_domain = irq_find_matching_host(np,
  1203. DOMAIN_BUS_WAKEUP);
  1204. of_node_put(np);
  1205. if (!chip->irq.parent_domain)
  1206. return -EPROBE_DEFER;
  1207. chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
  1208. /*
  1209. * Let's skip handling the GPIOs, if the parent irqchip
  1210. * is handling the direct connect IRQ of the GPIO.
  1211. */
  1212. skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
  1213. for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
  1214. gpio = pctrl->soc->wakeirq_map[i].gpio;
  1215. set_bit(gpio, pctrl->skip_wake_irqs);
  1216. }
  1217. }
  1218. girq = &chip->irq;
  1219. gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip);
  1220. girq->parent_handler = msm_gpio_irq_handler;
  1221. girq->fwnode = dev_fwnode(pctrl->dev);
  1222. girq->num_parents = 1;
  1223. girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
  1224. GFP_KERNEL);
  1225. if (!girq->parents)
  1226. return -ENOMEM;
  1227. girq->default_type = IRQ_TYPE_NONE;
  1228. girq->handler = handle_bad_irq;
  1229. girq->parents[0] = pctrl->irq;
  1230. girq->init_valid_mask = msm_gpio_irq_init_valid_mask;
  1231. ret = gpiochip_add_data(&pctrl->chip, pctrl);
  1232. if (ret) {
  1233. dev_err(pctrl->dev, "Failed register gpiochip\n");
  1234. return ret;
  1235. }
  1236. /*
  1237. * For DeviceTree-supported systems, the gpio core checks the
  1238. * pinctrl's device node for the "gpio-ranges" property.
  1239. * If it is present, it takes care of adding the pin ranges
  1240. * for the driver. In this case the driver can skip ahead.
  1241. *
  1242. * In order to remain compatible with older, existing DeviceTree
  1243. * files which don't set the "gpio-ranges" property or systems that
  1244. * utilize ACPI the driver has to call gpiochip_add_pin_range().
  1245. */
  1246. if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
  1247. ret = gpiochip_add_pin_range(&pctrl->chip,
  1248. dev_name(pctrl->dev), 0, 0, chip->ngpio);
  1249. if (ret) {
  1250. dev_err(pctrl->dev, "Failed to add pin range\n");
  1251. gpiochip_remove(&pctrl->chip);
  1252. return ret;
  1253. }
  1254. }
  1255. return 0;
  1256. }
  1257. static int msm_ps_hold_restart(struct sys_off_data *data)
  1258. {
  1259. struct msm_pinctrl *pctrl = data->cb_data;
  1260. writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
  1261. mdelay(1000);
  1262. return NOTIFY_DONE;
  1263. }
  1264. static struct msm_pinctrl *poweroff_pctrl;
  1265. static void msm_ps_hold_poweroff(void)
  1266. {
  1267. struct sys_off_data data = {
  1268. .cb_data = poweroff_pctrl,
  1269. };
  1270. msm_ps_hold_restart(&data);
  1271. }
  1272. static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
  1273. {
  1274. int i;
  1275. const struct pinfunction *func = pctrl->soc->functions;
  1276. for (i = 0; i < pctrl->soc->nfunctions; i++)
  1277. if (!strcmp(func[i].name, "ps_hold")) {
  1278. if (devm_register_sys_off_handler(pctrl->dev,
  1279. SYS_OFF_MODE_RESTART,
  1280. 128,
  1281. msm_ps_hold_restart,
  1282. pctrl))
  1283. dev_err(pctrl->dev,
  1284. "failed to setup restart handler.\n");
  1285. poweroff_pctrl = pctrl;
  1286. pm_power_off = msm_ps_hold_poweroff;
  1287. break;
  1288. }
  1289. }
  1290. static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
  1291. {
  1292. struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
  1293. return pinctrl_force_sleep(pctrl->pctrl);
  1294. }
  1295. static __maybe_unused int msm_pinctrl_resume(struct device *dev)
  1296. {
  1297. struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
  1298. return pinctrl_force_default(pctrl->pctrl);
  1299. }
  1300. SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
  1301. msm_pinctrl_resume);
  1302. EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
  1303. int msm_pinctrl_probe(struct platform_device *pdev,
  1304. const struct msm_pinctrl_soc_data *soc_data)
  1305. {
  1306. struct msm_pinctrl *pctrl;
  1307. struct resource *res;
  1308. int ret;
  1309. int i;
  1310. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  1311. if (!pctrl)
  1312. return -ENOMEM;
  1313. pctrl->dev = &pdev->dev;
  1314. pctrl->soc = soc_data;
  1315. pctrl->chip = msm_gpio_template;
  1316. pctrl->intr_target_use_scm = of_device_is_compatible(
  1317. pctrl->dev->of_node,
  1318. "qcom,ipq8064-pinctrl");
  1319. raw_spin_lock_init(&pctrl->lock);
  1320. if (soc_data->tiles) {
  1321. for (i = 0; i < soc_data->ntiles; i++) {
  1322. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  1323. soc_data->tiles[i]);
  1324. pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
  1325. if (IS_ERR(pctrl->regs[i]))
  1326. return PTR_ERR(pctrl->regs[i]);
  1327. }
  1328. } else {
  1329. pctrl->regs[0] = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1330. if (IS_ERR(pctrl->regs[0]))
  1331. return PTR_ERR(pctrl->regs[0]);
  1332. pctrl->phys_base[0] = res->start;
  1333. }
  1334. msm_pinctrl_setup_pm_reset(pctrl);
  1335. pctrl->irq = platform_get_irq(pdev, 0);
  1336. if (pctrl->irq < 0)
  1337. return pctrl->irq;
  1338. pctrl->desc.owner = THIS_MODULE;
  1339. pctrl->desc.pctlops = &msm_pinctrl_ops;
  1340. pctrl->desc.pmxops = &msm_pinmux_ops;
  1341. pctrl->desc.confops = &msm_pinconf_ops;
  1342. pctrl->desc.name = dev_name(&pdev->dev);
  1343. pctrl->desc.pins = pctrl->soc->pins;
  1344. pctrl->desc.npins = pctrl->soc->npins;
  1345. pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
  1346. if (IS_ERR(pctrl->pctrl)) {
  1347. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  1348. return PTR_ERR(pctrl->pctrl);
  1349. }
  1350. ret = msm_gpio_init(pctrl);
  1351. if (ret)
  1352. return ret;
  1353. platform_set_drvdata(pdev, pctrl);
  1354. dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
  1355. return 0;
  1356. }
  1357. EXPORT_SYMBOL(msm_pinctrl_probe);
  1358. void msm_pinctrl_remove(struct platform_device *pdev)
  1359. {
  1360. struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
  1361. gpiochip_remove(&pctrl->chip);
  1362. }
  1363. EXPORT_SYMBOL(msm_pinctrl_remove);
  1364. MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver");
  1365. MODULE_LICENSE("GPL v2");