pinctrl-mtk-common.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /*
  2. * mt65xx pinctrl driver based on Allwinner A1X pinctrl driver.
  3. * Copyright (c) 2014 MediaTek Inc.
  4. * Author: Hongzhou.Yang <hongzhou.yang@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/io.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/pinctrl/consumer.h>
  22. #include <linux/pinctrl/machine.h>
  23. #include <linux/pinctrl/pinconf.h>
  24. #include <linux/pinctrl/pinconf-generic.h>
  25. #include <linux/pinctrl/pinctrl.h>
  26. #include <linux/pinctrl/pinmux.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/bitops.h>
  30. #include <linux/regmap.h>
  31. #include <linux/mfd/syscon.h>
  32. #include <linux/delay.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/pm.h>
  35. #include <dt-bindings/pinctrl/mt65xx.h>
  36. #include "../core.h"
  37. #include "../pinconf.h"
  38. #include "../pinctrl-utils.h"
  39. #include "pinctrl-mtk-common.h"
  40. #define MAX_GPIO_MODE_PER_REG 5
  41. #define GPIO_MODE_BITS 3
  42. #define GPIO_MODE_PREFIX "GPIO"
  43. static const char * const mtk_gpio_functions[] = {
  44. "func0", "func1", "func2", "func3",
  45. "func4", "func5", "func6", "func7",
  46. "func8", "func9", "func10", "func11",
  47. "func12", "func13", "func14", "func15",
  48. };
  49. /*
  50. * There are two base address for pull related configuration
  51. * in mt8135, and different GPIO pins use different base address.
  52. * When pin number greater than type1_start and less than type1_end,
  53. * should use the second base address.
  54. */
  55. static struct regmap *mtk_get_regmap(struct mtk_pinctrl *pctl,
  56. unsigned long pin)
  57. {
  58. if (pin >= pctl->devdata->type1_start && pin < pctl->devdata->type1_end)
  59. return pctl->regmap2;
  60. return pctl->regmap1;
  61. }
  62. static unsigned int mtk_get_port(struct mtk_pinctrl *pctl, unsigned long pin)
  63. {
  64. /* Different SoC has different mask and port shift. */
  65. return ((pin >> 4) & pctl->devdata->port_mask)
  66. << pctl->devdata->port_shf;
  67. }
  68. static int mtk_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  69. struct pinctrl_gpio_range *range, unsigned offset,
  70. bool input)
  71. {
  72. unsigned int reg_addr;
  73. unsigned int bit;
  74. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  75. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
  76. bit = BIT(offset & 0xf);
  77. if (pctl->devdata->spec_dir_set)
  78. pctl->devdata->spec_dir_set(&reg_addr, offset);
  79. if (input)
  80. /* Different SoC has different alignment offset. */
  81. reg_addr = CLR_ADDR(reg_addr, pctl);
  82. else
  83. reg_addr = SET_ADDR(reg_addr, pctl);
  84. regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
  85. return 0;
  86. }
  87. static void mtk_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  88. {
  89. unsigned int reg_addr;
  90. unsigned int bit;
  91. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  92. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dout_offset;
  93. bit = BIT(offset & 0xf);
  94. if (value)
  95. reg_addr = SET_ADDR(reg_addr, pctl);
  96. else
  97. reg_addr = CLR_ADDR(reg_addr, pctl);
  98. regmap_write(mtk_get_regmap(pctl, offset), reg_addr, bit);
  99. }
  100. static int mtk_pconf_set_ies_smt(struct mtk_pinctrl *pctl, unsigned pin,
  101. int value, enum pin_config_param arg)
  102. {
  103. unsigned int reg_addr, offset;
  104. unsigned int bit;
  105. /**
  106. * Due to some soc are not support ies/smt config, add this special
  107. * control to handle it.
  108. */
  109. if (!pctl->devdata->spec_ies_smt_set &&
  110. pctl->devdata->ies_offset == MTK_PINCTRL_NOT_SUPPORT &&
  111. arg == PIN_CONFIG_INPUT_ENABLE)
  112. return -EINVAL;
  113. if (!pctl->devdata->spec_ies_smt_set &&
  114. pctl->devdata->smt_offset == MTK_PINCTRL_NOT_SUPPORT &&
  115. arg == PIN_CONFIG_INPUT_SCHMITT_ENABLE)
  116. return -EINVAL;
  117. /*
  118. * Due to some pins are irregular, their input enable and smt
  119. * control register are discontinuous, so we need this special handle.
  120. */
  121. if (pctl->devdata->spec_ies_smt_set) {
  122. return pctl->devdata->spec_ies_smt_set(mtk_get_regmap(pctl, pin),
  123. pin, pctl->devdata->port_align, value, arg);
  124. }
  125. bit = BIT(pin & 0xf);
  126. if (arg == PIN_CONFIG_INPUT_ENABLE)
  127. offset = pctl->devdata->ies_offset;
  128. else
  129. offset = pctl->devdata->smt_offset;
  130. if (value)
  131. reg_addr = SET_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
  132. else
  133. reg_addr = CLR_ADDR(mtk_get_port(pctl, pin) + offset, pctl);
  134. regmap_write(mtk_get_regmap(pctl, pin), reg_addr, bit);
  135. return 0;
  136. }
  137. int mtk_pconf_spec_set_ies_smt_range(struct regmap *regmap,
  138. const struct mtk_pin_ies_smt_set *ies_smt_infos, unsigned int info_num,
  139. unsigned int pin, unsigned char align, int value)
  140. {
  141. unsigned int i, reg_addr, bit;
  142. for (i = 0; i < info_num; i++) {
  143. if (pin >= ies_smt_infos[i].start &&
  144. pin <= ies_smt_infos[i].end) {
  145. break;
  146. }
  147. }
  148. if (i == info_num)
  149. return -EINVAL;
  150. if (value)
  151. reg_addr = ies_smt_infos[i].offset + align;
  152. else
  153. reg_addr = ies_smt_infos[i].offset + (align << 1);
  154. bit = BIT(ies_smt_infos[i].bit);
  155. regmap_write(regmap, reg_addr, bit);
  156. return 0;
  157. }
  158. static const struct mtk_pin_drv_grp *mtk_find_pin_drv_grp_by_pin(
  159. struct mtk_pinctrl *pctl, unsigned long pin) {
  160. int i;
  161. for (i = 0; i < pctl->devdata->n_pin_drv_grps; i++) {
  162. const struct mtk_pin_drv_grp *pin_drv =
  163. pctl->devdata->pin_drv_grp + i;
  164. if (pin == pin_drv->pin)
  165. return pin_drv;
  166. }
  167. return NULL;
  168. }
  169. static int mtk_pconf_set_driving(struct mtk_pinctrl *pctl,
  170. unsigned int pin, unsigned char driving)
  171. {
  172. const struct mtk_pin_drv_grp *pin_drv;
  173. unsigned int val;
  174. unsigned int bits, mask, shift;
  175. const struct mtk_drv_group_desc *drv_grp;
  176. if (pin >= pctl->devdata->npins)
  177. return -EINVAL;
  178. pin_drv = mtk_find_pin_drv_grp_by_pin(pctl, pin);
  179. if (!pin_drv || pin_drv->grp > pctl->devdata->n_grp_cls)
  180. return -EINVAL;
  181. drv_grp = pctl->devdata->grp_desc + pin_drv->grp;
  182. if (driving >= drv_grp->min_drv && driving <= drv_grp->max_drv
  183. && !(driving % drv_grp->step)) {
  184. val = driving / drv_grp->step - 1;
  185. bits = drv_grp->high_bit - drv_grp->low_bit + 1;
  186. mask = BIT(bits) - 1;
  187. shift = pin_drv->bit + drv_grp->low_bit;
  188. mask <<= shift;
  189. val <<= shift;
  190. return regmap_update_bits(mtk_get_regmap(pctl, pin),
  191. pin_drv->offset, mask, val);
  192. }
  193. return -EINVAL;
  194. }
  195. int mtk_pctrl_spec_pull_set_samereg(struct regmap *regmap,
  196. const struct mtk_pin_spec_pupd_set_samereg *pupd_infos,
  197. unsigned int info_num, unsigned int pin,
  198. unsigned char align, bool isup, unsigned int r1r0)
  199. {
  200. unsigned int i;
  201. unsigned int reg_pupd, reg_set, reg_rst;
  202. unsigned int bit_pupd, bit_r0, bit_r1;
  203. const struct mtk_pin_spec_pupd_set_samereg *spec_pupd_pin;
  204. bool find = false;
  205. for (i = 0; i < info_num; i++) {
  206. if (pin == pupd_infos[i].pin) {
  207. find = true;
  208. break;
  209. }
  210. }
  211. if (!find)
  212. return -EINVAL;
  213. spec_pupd_pin = pupd_infos + i;
  214. reg_set = spec_pupd_pin->offset + align;
  215. reg_rst = spec_pupd_pin->offset + (align << 1);
  216. if (isup)
  217. reg_pupd = reg_rst;
  218. else
  219. reg_pupd = reg_set;
  220. bit_pupd = BIT(spec_pupd_pin->pupd_bit);
  221. regmap_write(regmap, reg_pupd, bit_pupd);
  222. bit_r0 = BIT(spec_pupd_pin->r0_bit);
  223. bit_r1 = BIT(spec_pupd_pin->r1_bit);
  224. switch (r1r0) {
  225. case MTK_PUPD_SET_R1R0_00:
  226. regmap_write(regmap, reg_rst, bit_r0);
  227. regmap_write(regmap, reg_rst, bit_r1);
  228. break;
  229. case MTK_PUPD_SET_R1R0_01:
  230. regmap_write(regmap, reg_set, bit_r0);
  231. regmap_write(regmap, reg_rst, bit_r1);
  232. break;
  233. case MTK_PUPD_SET_R1R0_10:
  234. regmap_write(regmap, reg_rst, bit_r0);
  235. regmap_write(regmap, reg_set, bit_r1);
  236. break;
  237. case MTK_PUPD_SET_R1R0_11:
  238. regmap_write(regmap, reg_set, bit_r0);
  239. regmap_write(regmap, reg_set, bit_r1);
  240. break;
  241. default:
  242. return -EINVAL;
  243. }
  244. return 0;
  245. }
  246. static int mtk_pconf_set_pull_select(struct mtk_pinctrl *pctl,
  247. unsigned int pin, bool enable, bool isup, unsigned int arg)
  248. {
  249. unsigned int bit;
  250. unsigned int reg_pullen, reg_pullsel, r1r0;
  251. int ret;
  252. /* Some pins' pull setting are very different,
  253. * they have separate pull up/down bit, R0 and R1
  254. * resistor bit, so we need this special handle.
  255. */
  256. if (pctl->devdata->spec_pull_set) {
  257. /* For special pins, bias-disable is set by R1R0,
  258. * the parameter should be "MTK_PUPD_SET_R1R0_00".
  259. */
  260. r1r0 = enable ? arg : MTK_PUPD_SET_R1R0_00;
  261. ret = pctl->devdata->spec_pull_set(mtk_get_regmap(pctl, pin),
  262. pin, pctl->devdata->port_align, isup, r1r0);
  263. if (!ret)
  264. return 0;
  265. }
  266. /* For generic pull config, default arg value should be 0 or 1. */
  267. if (arg != 0 && arg != 1) {
  268. dev_err(pctl->dev, "invalid pull-up argument %d on pin %d .\n",
  269. arg, pin);
  270. return -EINVAL;
  271. }
  272. bit = BIT(pin & 0xf);
  273. if (enable)
  274. reg_pullen = SET_ADDR(mtk_get_port(pctl, pin) +
  275. pctl->devdata->pullen_offset, pctl);
  276. else
  277. reg_pullen = CLR_ADDR(mtk_get_port(pctl, pin) +
  278. pctl->devdata->pullen_offset, pctl);
  279. if (isup)
  280. reg_pullsel = SET_ADDR(mtk_get_port(pctl, pin) +
  281. pctl->devdata->pullsel_offset, pctl);
  282. else
  283. reg_pullsel = CLR_ADDR(mtk_get_port(pctl, pin) +
  284. pctl->devdata->pullsel_offset, pctl);
  285. regmap_write(mtk_get_regmap(pctl, pin), reg_pullen, bit);
  286. regmap_write(mtk_get_regmap(pctl, pin), reg_pullsel, bit);
  287. return 0;
  288. }
  289. static int mtk_pconf_parse_conf(struct pinctrl_dev *pctldev,
  290. unsigned int pin, enum pin_config_param param,
  291. enum pin_config_param arg)
  292. {
  293. int ret = 0;
  294. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  295. switch (param) {
  296. case PIN_CONFIG_BIAS_DISABLE:
  297. ret = mtk_pconf_set_pull_select(pctl, pin, false, false, arg);
  298. break;
  299. case PIN_CONFIG_BIAS_PULL_UP:
  300. ret = mtk_pconf_set_pull_select(pctl, pin, true, true, arg);
  301. break;
  302. case PIN_CONFIG_BIAS_PULL_DOWN:
  303. ret = mtk_pconf_set_pull_select(pctl, pin, true, false, arg);
  304. break;
  305. case PIN_CONFIG_INPUT_ENABLE:
  306. mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
  307. ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
  308. break;
  309. case PIN_CONFIG_OUTPUT:
  310. mtk_gpio_set(pctl->chip, pin, arg);
  311. ret = mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
  312. break;
  313. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  314. mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
  315. ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
  316. break;
  317. case PIN_CONFIG_DRIVE_STRENGTH:
  318. ret = mtk_pconf_set_driving(pctl, pin, arg);
  319. break;
  320. default:
  321. ret = -EINVAL;
  322. }
  323. return ret;
  324. }
  325. static int mtk_pconf_group_get(struct pinctrl_dev *pctldev,
  326. unsigned group,
  327. unsigned long *config)
  328. {
  329. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  330. *config = pctl->groups[group].config;
  331. return 0;
  332. }
  333. static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  334. unsigned long *configs, unsigned num_configs)
  335. {
  336. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  337. struct mtk_pinctrl_group *g = &pctl->groups[group];
  338. int i, ret;
  339. for (i = 0; i < num_configs; i++) {
  340. ret = mtk_pconf_parse_conf(pctldev, g->pin,
  341. pinconf_to_config_param(configs[i]),
  342. pinconf_to_config_argument(configs[i]));
  343. if (ret < 0)
  344. return ret;
  345. g->config = configs[i];
  346. }
  347. return 0;
  348. }
  349. static const struct pinconf_ops mtk_pconf_ops = {
  350. .pin_config_group_get = mtk_pconf_group_get,
  351. .pin_config_group_set = mtk_pconf_group_set,
  352. };
  353. static struct mtk_pinctrl_group *
  354. mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *pctl, u32 pin)
  355. {
  356. int i;
  357. for (i = 0; i < pctl->ngroups; i++) {
  358. struct mtk_pinctrl_group *grp = pctl->groups + i;
  359. if (grp->pin == pin)
  360. return grp;
  361. }
  362. return NULL;
  363. }
  364. static const struct mtk_desc_function *mtk_pctrl_find_function_by_pin(
  365. struct mtk_pinctrl *pctl, u32 pin_num, u32 fnum)
  366. {
  367. const struct mtk_desc_pin *pin = pctl->devdata->pins + pin_num;
  368. const struct mtk_desc_function *func = pin->functions;
  369. while (func && func->name) {
  370. if (func->muxval == fnum)
  371. return func;
  372. func++;
  373. }
  374. return NULL;
  375. }
  376. static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *pctl,
  377. u32 pin_num, u32 fnum)
  378. {
  379. int i;
  380. for (i = 0; i < pctl->devdata->npins; i++) {
  381. const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
  382. if (pin->pin.number == pin_num) {
  383. const struct mtk_desc_function *func =
  384. pin->functions;
  385. while (func && func->name) {
  386. if (func->muxval == fnum)
  387. return true;
  388. func++;
  389. }
  390. break;
  391. }
  392. }
  393. return false;
  394. }
  395. static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
  396. u32 pin, u32 fnum, struct mtk_pinctrl_group *grp,
  397. struct pinctrl_map **map, unsigned *reserved_maps,
  398. unsigned *num_maps)
  399. {
  400. bool ret;
  401. if (*num_maps == *reserved_maps)
  402. return -ENOSPC;
  403. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  404. (*map)[*num_maps].data.mux.group = grp->name;
  405. ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
  406. if (!ret) {
  407. dev_err(pctl->dev, "invalid function %d on pin %d .\n",
  408. fnum, pin);
  409. return -EINVAL;
  410. }
  411. (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
  412. (*num_maps)++;
  413. return 0;
  414. }
  415. static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  416. struct device_node *node,
  417. struct pinctrl_map **map,
  418. unsigned *reserved_maps,
  419. unsigned *num_maps)
  420. {
  421. struct property *pins;
  422. u32 pinfunc, pin, func;
  423. int num_pins, num_funcs, maps_per_pin;
  424. unsigned long *configs;
  425. unsigned int num_configs;
  426. bool has_config = false;
  427. int i, err;
  428. unsigned reserve = 0;
  429. struct mtk_pinctrl_group *grp;
  430. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  431. pins = of_find_property(node, "pinmux", NULL);
  432. if (!pins) {
  433. dev_err(pctl->dev, "missing pins property in node %s .\n",
  434. node->name);
  435. return -EINVAL;
  436. }
  437. err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
  438. &num_configs);
  439. if (err)
  440. return err;
  441. if (num_configs)
  442. has_config = true;
  443. num_pins = pins->length / sizeof(u32);
  444. num_funcs = num_pins;
  445. maps_per_pin = 0;
  446. if (num_funcs)
  447. maps_per_pin++;
  448. if (has_config && num_pins >= 1)
  449. maps_per_pin++;
  450. if (!num_pins || !maps_per_pin) {
  451. err = -EINVAL;
  452. goto exit;
  453. }
  454. reserve = num_pins * maps_per_pin;
  455. err = pinctrl_utils_reserve_map(pctldev, map,
  456. reserved_maps, num_maps, reserve);
  457. if (err < 0)
  458. goto exit;
  459. for (i = 0; i < num_pins; i++) {
  460. err = of_property_read_u32_index(node, "pinmux",
  461. i, &pinfunc);
  462. if (err)
  463. goto exit;
  464. pin = MTK_GET_PIN_NO(pinfunc);
  465. func = MTK_GET_PIN_FUNC(pinfunc);
  466. if (pin >= pctl->devdata->npins ||
  467. func >= ARRAY_SIZE(mtk_gpio_functions)) {
  468. dev_err(pctl->dev, "invalid pins value.\n");
  469. err = -EINVAL;
  470. goto exit;
  471. }
  472. grp = mtk_pctrl_find_group_by_pin(pctl, pin);
  473. if (!grp) {
  474. dev_err(pctl->dev, "unable to match pin %d to group\n",
  475. pin);
  476. err = -EINVAL;
  477. goto exit;
  478. }
  479. err = mtk_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
  480. reserved_maps, num_maps);
  481. if (err < 0)
  482. goto exit;
  483. if (has_config) {
  484. err = pinctrl_utils_add_map_configs(pctldev, map,
  485. reserved_maps, num_maps, grp->name,
  486. configs, num_configs,
  487. PIN_MAP_TYPE_CONFIGS_GROUP);
  488. if (err < 0)
  489. goto exit;
  490. }
  491. }
  492. err = 0;
  493. exit:
  494. kfree(configs);
  495. return err;
  496. }
  497. static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  498. struct device_node *np_config,
  499. struct pinctrl_map **map, unsigned *num_maps)
  500. {
  501. struct device_node *np;
  502. unsigned reserved_maps;
  503. int ret;
  504. *map = NULL;
  505. *num_maps = 0;
  506. reserved_maps = 0;
  507. for_each_child_of_node(np_config, np) {
  508. ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
  509. &reserved_maps, num_maps);
  510. if (ret < 0) {
  511. pinctrl_utils_free_map(pctldev, *map, *num_maps);
  512. of_node_put(np);
  513. return ret;
  514. }
  515. }
  516. return 0;
  517. }
  518. static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  519. {
  520. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  521. return pctl->ngroups;
  522. }
  523. static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  524. unsigned group)
  525. {
  526. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  527. return pctl->groups[group].name;
  528. }
  529. static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  530. unsigned group,
  531. const unsigned **pins,
  532. unsigned *num_pins)
  533. {
  534. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  535. *pins = (unsigned *)&pctl->groups[group].pin;
  536. *num_pins = 1;
  537. return 0;
  538. }
  539. static const struct pinctrl_ops mtk_pctrl_ops = {
  540. .dt_node_to_map = mtk_pctrl_dt_node_to_map,
  541. .dt_free_map = pinctrl_utils_free_map,
  542. .get_groups_count = mtk_pctrl_get_groups_count,
  543. .get_group_name = mtk_pctrl_get_group_name,
  544. .get_group_pins = mtk_pctrl_get_group_pins,
  545. };
  546. static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  547. {
  548. return ARRAY_SIZE(mtk_gpio_functions);
  549. }
  550. static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  551. unsigned selector)
  552. {
  553. return mtk_gpio_functions[selector];
  554. }
  555. static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  556. unsigned function,
  557. const char * const **groups,
  558. unsigned * const num_groups)
  559. {
  560. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  561. *groups = pctl->grp_names;
  562. *num_groups = pctl->ngroups;
  563. return 0;
  564. }
  565. static int mtk_pmx_set_mode(struct pinctrl_dev *pctldev,
  566. unsigned long pin, unsigned long mode)
  567. {
  568. unsigned int reg_addr;
  569. unsigned char bit;
  570. unsigned int val;
  571. unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
  572. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  573. if (pctl->devdata->spec_pinmux_set)
  574. pctl->devdata->spec_pinmux_set(mtk_get_regmap(pctl, pin),
  575. pin, mode);
  576. reg_addr = ((pin / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
  577. + pctl->devdata->pinmux_offset;
  578. mode &= mask;
  579. bit = pin % MAX_GPIO_MODE_PER_REG;
  580. mask <<= (GPIO_MODE_BITS * bit);
  581. val = (mode << (GPIO_MODE_BITS * bit));
  582. return regmap_update_bits(mtk_get_regmap(pctl, pin),
  583. reg_addr, mask, val);
  584. }
  585. static const struct mtk_desc_pin *
  586. mtk_find_pin_by_eint_num(struct mtk_pinctrl *pctl, unsigned int eint_num)
  587. {
  588. int i;
  589. const struct mtk_desc_pin *pin;
  590. for (i = 0; i < pctl->devdata->npins; i++) {
  591. pin = pctl->devdata->pins + i;
  592. if (pin->eint.eintnum == eint_num)
  593. return pin;
  594. }
  595. return NULL;
  596. }
  597. static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
  598. unsigned function,
  599. unsigned group)
  600. {
  601. bool ret;
  602. const struct mtk_desc_function *desc;
  603. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  604. struct mtk_pinctrl_group *g = pctl->groups + group;
  605. ret = mtk_pctrl_is_function_valid(pctl, g->pin, function);
  606. if (!ret) {
  607. dev_err(pctl->dev, "invalid function %d on group %d .\n",
  608. function, group);
  609. return -EINVAL;
  610. }
  611. desc = mtk_pctrl_find_function_by_pin(pctl, g->pin, function);
  612. if (!desc)
  613. return -EINVAL;
  614. mtk_pmx_set_mode(pctldev, g->pin, desc->muxval);
  615. return 0;
  616. }
  617. static int mtk_pmx_find_gpio_mode(struct mtk_pinctrl *pctl,
  618. unsigned offset)
  619. {
  620. const struct mtk_desc_pin *pin = pctl->devdata->pins + offset;
  621. const struct mtk_desc_function *func = pin->functions;
  622. while (func && func->name) {
  623. if (!strncmp(func->name, GPIO_MODE_PREFIX,
  624. sizeof(GPIO_MODE_PREFIX)-1))
  625. return func->muxval;
  626. func++;
  627. }
  628. return -EINVAL;
  629. }
  630. static int mtk_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
  631. struct pinctrl_gpio_range *range,
  632. unsigned offset)
  633. {
  634. int muxval;
  635. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  636. muxval = mtk_pmx_find_gpio_mode(pctl, offset);
  637. if (muxval < 0) {
  638. dev_err(pctl->dev, "invalid gpio pin %d.\n", offset);
  639. return -EINVAL;
  640. }
  641. mtk_pmx_set_mode(pctldev, offset, muxval);
  642. mtk_pconf_set_ies_smt(pctl, offset, 1, PIN_CONFIG_INPUT_ENABLE);
  643. return 0;
  644. }
  645. static const struct pinmux_ops mtk_pmx_ops = {
  646. .get_functions_count = mtk_pmx_get_funcs_cnt,
  647. .get_function_name = mtk_pmx_get_func_name,
  648. .get_function_groups = mtk_pmx_get_func_groups,
  649. .set_mux = mtk_pmx_set_mux,
  650. .gpio_set_direction = mtk_pmx_gpio_set_direction,
  651. .gpio_request_enable = mtk_pmx_gpio_request_enable,
  652. };
  653. static int mtk_gpio_direction_input(struct gpio_chip *chip,
  654. unsigned offset)
  655. {
  656. return pinctrl_gpio_direction_input(chip->base + offset);
  657. }
  658. static int mtk_gpio_direction_output(struct gpio_chip *chip,
  659. unsigned offset, int value)
  660. {
  661. mtk_gpio_set(chip, offset, value);
  662. return pinctrl_gpio_direction_output(chip->base + offset);
  663. }
  664. static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  665. {
  666. unsigned int reg_addr;
  667. unsigned int bit;
  668. unsigned int read_val = 0;
  669. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  670. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
  671. bit = BIT(offset & 0xf);
  672. if (pctl->devdata->spec_dir_set)
  673. pctl->devdata->spec_dir_set(&reg_addr, offset);
  674. regmap_read(pctl->regmap1, reg_addr, &read_val);
  675. return !(read_val & bit);
  676. }
  677. static int mtk_gpio_get(struct gpio_chip *chip, unsigned offset)
  678. {
  679. unsigned int reg_addr;
  680. unsigned int bit;
  681. unsigned int read_val = 0;
  682. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  683. reg_addr = mtk_get_port(pctl, offset) +
  684. pctl->devdata->din_offset;
  685. bit = BIT(offset & 0xf);
  686. regmap_read(pctl->regmap1, reg_addr, &read_val);
  687. return !!(read_val & bit);
  688. }
  689. static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  690. {
  691. const struct mtk_desc_pin *pin;
  692. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  693. int irq;
  694. pin = pctl->devdata->pins + offset;
  695. if (pin->eint.eintnum == NO_EINT_SUPPORT)
  696. return -EINVAL;
  697. irq = irq_find_mapping(pctl->domain, pin->eint.eintnum);
  698. if (!irq)
  699. return -EINVAL;
  700. return irq;
  701. }
  702. static int mtk_pinctrl_irq_request_resources(struct irq_data *d)
  703. {
  704. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  705. const struct mtk_desc_pin *pin;
  706. int ret;
  707. pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
  708. if (!pin) {
  709. dev_err(pctl->dev, "Can not find pin\n");
  710. return -EINVAL;
  711. }
  712. ret = gpiochip_lock_as_irq(pctl->chip, pin->pin.number);
  713. if (ret) {
  714. dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
  715. irqd_to_hwirq(d));
  716. return ret;
  717. }
  718. /* set mux to INT mode */
  719. mtk_pmx_set_mode(pctl->pctl_dev, pin->pin.number, pin->eint.eintmux);
  720. /* set gpio direction to input */
  721. mtk_pmx_gpio_set_direction(pctl->pctl_dev, NULL, pin->pin.number, true);
  722. /* set input-enable */
  723. mtk_pconf_set_ies_smt(pctl, pin->pin.number, 1, PIN_CONFIG_INPUT_ENABLE);
  724. return 0;
  725. }
  726. static void mtk_pinctrl_irq_release_resources(struct irq_data *d)
  727. {
  728. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  729. const struct mtk_desc_pin *pin;
  730. pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
  731. if (!pin) {
  732. dev_err(pctl->dev, "Can not find pin\n");
  733. return;
  734. }
  735. gpiochip_unlock_as_irq(pctl->chip, pin->pin.number);
  736. }
  737. static void __iomem *mtk_eint_get_offset(struct mtk_pinctrl *pctl,
  738. unsigned int eint_num, unsigned int offset)
  739. {
  740. unsigned int eint_base = 0;
  741. void __iomem *reg;
  742. if (eint_num >= pctl->devdata->ap_num)
  743. eint_base = pctl->devdata->ap_num;
  744. reg = pctl->eint_reg_base + offset + ((eint_num - eint_base) / 32) * 4;
  745. return reg;
  746. }
  747. /*
  748. * mtk_can_en_debounce: Check the EINT number is able to enable debounce or not
  749. * @eint_num: the EINT number to setmtk_pinctrl
  750. */
  751. static unsigned int mtk_eint_can_en_debounce(struct mtk_pinctrl *pctl,
  752. unsigned int eint_num)
  753. {
  754. unsigned int sens;
  755. unsigned int bit = BIT(eint_num % 32);
  756. const struct mtk_eint_offsets *eint_offsets =
  757. &pctl->devdata->eint_offsets;
  758. void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
  759. eint_offsets->sens);
  760. if (readl(reg) & bit)
  761. sens = MT_LEVEL_SENSITIVE;
  762. else
  763. sens = MT_EDGE_SENSITIVE;
  764. if ((eint_num < pctl->devdata->db_cnt) && (sens != MT_EDGE_SENSITIVE))
  765. return 1;
  766. else
  767. return 0;
  768. }
  769. /*
  770. * mtk_eint_get_mask: To get the eint mask
  771. * @eint_num: the EINT number to get
  772. */
  773. static unsigned int mtk_eint_get_mask(struct mtk_pinctrl *pctl,
  774. unsigned int eint_num)
  775. {
  776. unsigned int bit = BIT(eint_num % 32);
  777. const struct mtk_eint_offsets *eint_offsets =
  778. &pctl->devdata->eint_offsets;
  779. void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
  780. eint_offsets->mask);
  781. return !!(readl(reg) & bit);
  782. }
  783. static int mtk_eint_flip_edge(struct mtk_pinctrl *pctl, int hwirq)
  784. {
  785. int start_level, curr_level;
  786. unsigned int reg_offset;
  787. const struct mtk_eint_offsets *eint_offsets = &(pctl->devdata->eint_offsets);
  788. u32 mask = BIT(hwirq & 0x1f);
  789. u32 port = (hwirq >> 5) & eint_offsets->port_mask;
  790. void __iomem *reg = pctl->eint_reg_base + (port << 2);
  791. const struct mtk_desc_pin *pin;
  792. pin = mtk_find_pin_by_eint_num(pctl, hwirq);
  793. curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
  794. do {
  795. start_level = curr_level;
  796. if (start_level)
  797. reg_offset = eint_offsets->pol_clr;
  798. else
  799. reg_offset = eint_offsets->pol_set;
  800. writel(mask, reg + reg_offset);
  801. curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
  802. } while (start_level != curr_level);
  803. return start_level;
  804. }
  805. static void mtk_eint_mask(struct irq_data *d)
  806. {
  807. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  808. const struct mtk_eint_offsets *eint_offsets =
  809. &pctl->devdata->eint_offsets;
  810. u32 mask = BIT(d->hwirq & 0x1f);
  811. void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
  812. eint_offsets->mask_set);
  813. writel(mask, reg);
  814. }
  815. static void mtk_eint_unmask(struct irq_data *d)
  816. {
  817. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  818. const struct mtk_eint_offsets *eint_offsets =
  819. &pctl->devdata->eint_offsets;
  820. u32 mask = BIT(d->hwirq & 0x1f);
  821. void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
  822. eint_offsets->mask_clr);
  823. writel(mask, reg);
  824. if (pctl->eint_dual_edges[d->hwirq])
  825. mtk_eint_flip_edge(pctl, d->hwirq);
  826. }
  827. static int mtk_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
  828. unsigned debounce)
  829. {
  830. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->parent);
  831. int eint_num, virq, eint_offset;
  832. unsigned int set_offset, bit, clr_bit, clr_offset, rst, i, unmask, dbnc;
  833. static const unsigned int debounce_time[] = {500, 1000, 16000, 32000, 64000,
  834. 128000, 256000};
  835. const struct mtk_desc_pin *pin;
  836. struct irq_data *d;
  837. pin = pctl->devdata->pins + offset;
  838. if (pin->eint.eintnum == NO_EINT_SUPPORT)
  839. return -EINVAL;
  840. eint_num = pin->eint.eintnum;
  841. virq = irq_find_mapping(pctl->domain, eint_num);
  842. eint_offset = (eint_num % 4) * 8;
  843. d = irq_get_irq_data(virq);
  844. set_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_set;
  845. clr_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_clr;
  846. if (!mtk_eint_can_en_debounce(pctl, eint_num))
  847. return -ENOSYS;
  848. dbnc = ARRAY_SIZE(debounce_time);
  849. for (i = 0; i < ARRAY_SIZE(debounce_time); i++) {
  850. if (debounce <= debounce_time[i]) {
  851. dbnc = i;
  852. break;
  853. }
  854. }
  855. if (!mtk_eint_get_mask(pctl, eint_num)) {
  856. mtk_eint_mask(d);
  857. unmask = 1;
  858. } else {
  859. unmask = 0;
  860. }
  861. clr_bit = 0xff << eint_offset;
  862. writel(clr_bit, pctl->eint_reg_base + clr_offset);
  863. bit = ((dbnc << EINT_DBNC_SET_DBNC_BITS) | EINT_DBNC_SET_EN) <<
  864. eint_offset;
  865. rst = EINT_DBNC_RST_BIT << eint_offset;
  866. writel(rst | bit, pctl->eint_reg_base + set_offset);
  867. /* Delay a while (more than 2T) to wait for hw debounce counter reset
  868. work correctly */
  869. udelay(1);
  870. if (unmask == 1)
  871. mtk_eint_unmask(d);
  872. return 0;
  873. }
  874. static int mtk_gpio_set_config(struct gpio_chip *chip, unsigned offset,
  875. unsigned long config)
  876. {
  877. u32 debounce;
  878. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  879. return -ENOTSUPP;
  880. debounce = pinconf_to_config_argument(config);
  881. return mtk_gpio_set_debounce(chip, offset, debounce);
  882. }
  883. static const struct gpio_chip mtk_gpio_chip = {
  884. .owner = THIS_MODULE,
  885. .request = gpiochip_generic_request,
  886. .free = gpiochip_generic_free,
  887. .get_direction = mtk_gpio_get_direction,
  888. .direction_input = mtk_gpio_direction_input,
  889. .direction_output = mtk_gpio_direction_output,
  890. .get = mtk_gpio_get,
  891. .set = mtk_gpio_set,
  892. .to_irq = mtk_gpio_to_irq,
  893. .set_config = mtk_gpio_set_config,
  894. .of_gpio_n_cells = 2,
  895. };
  896. static int mtk_eint_set_type(struct irq_data *d,
  897. unsigned int type)
  898. {
  899. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  900. const struct mtk_eint_offsets *eint_offsets =
  901. &pctl->devdata->eint_offsets;
  902. u32 mask = BIT(d->hwirq & 0x1f);
  903. void __iomem *reg;
  904. if (((type & IRQ_TYPE_EDGE_BOTH) && (type & IRQ_TYPE_LEVEL_MASK)) ||
  905. ((type & IRQ_TYPE_LEVEL_MASK) == IRQ_TYPE_LEVEL_MASK)) {
  906. dev_err(pctl->dev, "Can't configure IRQ%d (EINT%lu) for type 0x%X\n",
  907. d->irq, d->hwirq, type);
  908. return -EINVAL;
  909. }
  910. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  911. pctl->eint_dual_edges[d->hwirq] = 1;
  912. else
  913. pctl->eint_dual_edges[d->hwirq] = 0;
  914. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) {
  915. reg = mtk_eint_get_offset(pctl, d->hwirq,
  916. eint_offsets->pol_clr);
  917. writel(mask, reg);
  918. } else {
  919. reg = mtk_eint_get_offset(pctl, d->hwirq,
  920. eint_offsets->pol_set);
  921. writel(mask, reg);
  922. }
  923. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
  924. reg = mtk_eint_get_offset(pctl, d->hwirq,
  925. eint_offsets->sens_clr);
  926. writel(mask, reg);
  927. } else {
  928. reg = mtk_eint_get_offset(pctl, d->hwirq,
  929. eint_offsets->sens_set);
  930. writel(mask, reg);
  931. }
  932. if (pctl->eint_dual_edges[d->hwirq])
  933. mtk_eint_flip_edge(pctl, d->hwirq);
  934. return 0;
  935. }
  936. static int mtk_eint_irq_set_wake(struct irq_data *d, unsigned int on)
  937. {
  938. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  939. int shift = d->hwirq & 0x1f;
  940. int reg = d->hwirq >> 5;
  941. if (on)
  942. pctl->wake_mask[reg] |= BIT(shift);
  943. else
  944. pctl->wake_mask[reg] &= ~BIT(shift);
  945. return 0;
  946. }
  947. static void mtk_eint_chip_write_mask(const struct mtk_eint_offsets *chip,
  948. void __iomem *eint_reg_base, u32 *buf)
  949. {
  950. int port;
  951. void __iomem *reg;
  952. for (port = 0; port < chip->ports; port++) {
  953. reg = eint_reg_base + (port << 2);
  954. writel_relaxed(~buf[port], reg + chip->mask_set);
  955. writel_relaxed(buf[port], reg + chip->mask_clr);
  956. }
  957. }
  958. static void mtk_eint_chip_read_mask(const struct mtk_eint_offsets *chip,
  959. void __iomem *eint_reg_base, u32 *buf)
  960. {
  961. int port;
  962. void __iomem *reg;
  963. for (port = 0; port < chip->ports; port++) {
  964. reg = eint_reg_base + chip->mask + (port << 2);
  965. buf[port] = ~readl_relaxed(reg);
  966. /* Mask is 0 when irq is enabled, and 1 when disabled. */
  967. }
  968. }
  969. static int mtk_eint_suspend(struct device *device)
  970. {
  971. void __iomem *reg;
  972. struct mtk_pinctrl *pctl = dev_get_drvdata(device);
  973. const struct mtk_eint_offsets *eint_offsets =
  974. &pctl->devdata->eint_offsets;
  975. reg = pctl->eint_reg_base;
  976. mtk_eint_chip_read_mask(eint_offsets, reg, pctl->cur_mask);
  977. mtk_eint_chip_write_mask(eint_offsets, reg, pctl->wake_mask);
  978. return 0;
  979. }
  980. static int mtk_eint_resume(struct device *device)
  981. {
  982. struct mtk_pinctrl *pctl = dev_get_drvdata(device);
  983. const struct mtk_eint_offsets *eint_offsets =
  984. &pctl->devdata->eint_offsets;
  985. mtk_eint_chip_write_mask(eint_offsets,
  986. pctl->eint_reg_base, pctl->cur_mask);
  987. return 0;
  988. }
  989. const struct dev_pm_ops mtk_eint_pm_ops = {
  990. .suspend_noirq = mtk_eint_suspend,
  991. .resume_noirq = mtk_eint_resume,
  992. };
  993. static void mtk_eint_ack(struct irq_data *d)
  994. {
  995. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  996. const struct mtk_eint_offsets *eint_offsets =
  997. &pctl->devdata->eint_offsets;
  998. u32 mask = BIT(d->hwirq & 0x1f);
  999. void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
  1000. eint_offsets->ack);
  1001. writel(mask, reg);
  1002. }
  1003. static struct irq_chip mtk_pinctrl_irq_chip = {
  1004. .name = "mt-eint",
  1005. .irq_disable = mtk_eint_mask,
  1006. .irq_mask = mtk_eint_mask,
  1007. .irq_unmask = mtk_eint_unmask,
  1008. .irq_ack = mtk_eint_ack,
  1009. .irq_set_type = mtk_eint_set_type,
  1010. .irq_set_wake = mtk_eint_irq_set_wake,
  1011. .irq_request_resources = mtk_pinctrl_irq_request_resources,
  1012. .irq_release_resources = mtk_pinctrl_irq_release_resources,
  1013. };
  1014. static unsigned int mtk_eint_init(struct mtk_pinctrl *pctl)
  1015. {
  1016. const struct mtk_eint_offsets *eint_offsets =
  1017. &pctl->devdata->eint_offsets;
  1018. void __iomem *reg = pctl->eint_reg_base + eint_offsets->dom_en;
  1019. unsigned int i;
  1020. for (i = 0; i < pctl->devdata->ap_num; i += 32) {
  1021. writel(0xffffffff, reg);
  1022. reg += 4;
  1023. }
  1024. return 0;
  1025. }
  1026. static inline void
  1027. mtk_eint_debounce_process(struct mtk_pinctrl *pctl, int index)
  1028. {
  1029. unsigned int rst, ctrl_offset;
  1030. unsigned int bit, dbnc;
  1031. const struct mtk_eint_offsets *eint_offsets =
  1032. &pctl->devdata->eint_offsets;
  1033. ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_ctrl;
  1034. dbnc = readl(pctl->eint_reg_base + ctrl_offset);
  1035. bit = EINT_DBNC_SET_EN << ((index % 4) * 8);
  1036. if ((bit & dbnc) > 0) {
  1037. ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_set;
  1038. rst = EINT_DBNC_RST_BIT << ((index % 4) * 8);
  1039. writel(rst, pctl->eint_reg_base + ctrl_offset);
  1040. }
  1041. }
  1042. static void mtk_eint_irq_handler(struct irq_desc *desc)
  1043. {
  1044. struct irq_chip *chip = irq_desc_get_chip(desc);
  1045. struct mtk_pinctrl *pctl = irq_desc_get_handler_data(desc);
  1046. unsigned int status, eint_num;
  1047. int offset, index, virq;
  1048. const struct mtk_eint_offsets *eint_offsets =
  1049. &pctl->devdata->eint_offsets;
  1050. void __iomem *reg = mtk_eint_get_offset(pctl, 0, eint_offsets->stat);
  1051. int dual_edges, start_level, curr_level;
  1052. const struct mtk_desc_pin *pin;
  1053. chained_irq_enter(chip, desc);
  1054. for (eint_num = 0;
  1055. eint_num < pctl->devdata->ap_num;
  1056. eint_num += 32, reg += 4) {
  1057. status = readl(reg);
  1058. while (status) {
  1059. offset = __ffs(status);
  1060. index = eint_num + offset;
  1061. virq = irq_find_mapping(pctl->domain, index);
  1062. status &= ~BIT(offset);
  1063. dual_edges = pctl->eint_dual_edges[index];
  1064. if (dual_edges) {
  1065. /* Clear soft-irq in case we raised it
  1066. last time */
  1067. writel(BIT(offset), reg - eint_offsets->stat +
  1068. eint_offsets->soft_clr);
  1069. pin = mtk_find_pin_by_eint_num(pctl, index);
  1070. start_level = mtk_gpio_get(pctl->chip,
  1071. pin->pin.number);
  1072. }
  1073. generic_handle_irq(virq);
  1074. if (dual_edges) {
  1075. curr_level = mtk_eint_flip_edge(pctl, index);
  1076. /* If level changed, we might lost one edge
  1077. interrupt, raised it through soft-irq */
  1078. if (start_level != curr_level)
  1079. writel(BIT(offset), reg -
  1080. eint_offsets->stat +
  1081. eint_offsets->soft_set);
  1082. }
  1083. if (index < pctl->devdata->db_cnt)
  1084. mtk_eint_debounce_process(pctl , index);
  1085. }
  1086. }
  1087. chained_irq_exit(chip, desc);
  1088. }
  1089. static int mtk_pctrl_build_state(struct platform_device *pdev)
  1090. {
  1091. struct mtk_pinctrl *pctl = platform_get_drvdata(pdev);
  1092. int i;
  1093. pctl->ngroups = pctl->devdata->npins;
  1094. /* Allocate groups */
  1095. pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
  1096. sizeof(*pctl->groups), GFP_KERNEL);
  1097. if (!pctl->groups)
  1098. return -ENOMEM;
  1099. /* We assume that one pin is one group, use pin name as group name. */
  1100. pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
  1101. sizeof(*pctl->grp_names), GFP_KERNEL);
  1102. if (!pctl->grp_names)
  1103. return -ENOMEM;
  1104. for (i = 0; i < pctl->devdata->npins; i++) {
  1105. const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
  1106. struct mtk_pinctrl_group *group = pctl->groups + i;
  1107. group->name = pin->pin.name;
  1108. group->pin = pin->pin.number;
  1109. pctl->grp_names[i] = pin->pin.name;
  1110. }
  1111. return 0;
  1112. }
  1113. int mtk_pctrl_init(struct platform_device *pdev,
  1114. const struct mtk_pinctrl_devdata *data,
  1115. struct regmap *regmap)
  1116. {
  1117. struct pinctrl_pin_desc *pins;
  1118. struct mtk_pinctrl *pctl;
  1119. struct device_node *np = pdev->dev.of_node, *node;
  1120. struct property *prop;
  1121. struct resource *res;
  1122. int i, ret, irq, ports_buf;
  1123. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  1124. if (!pctl)
  1125. return -ENOMEM;
  1126. platform_set_drvdata(pdev, pctl);
  1127. prop = of_find_property(np, "pins-are-numbered", NULL);
  1128. if (!prop) {
  1129. dev_err(&pdev->dev, "only support pins-are-numbered format\n");
  1130. return -EINVAL;
  1131. }
  1132. node = of_parse_phandle(np, "mediatek,pctl-regmap", 0);
  1133. if (node) {
  1134. pctl->regmap1 = syscon_node_to_regmap(node);
  1135. if (IS_ERR(pctl->regmap1))
  1136. return PTR_ERR(pctl->regmap1);
  1137. } else if (regmap) {
  1138. pctl->regmap1 = regmap;
  1139. } else {
  1140. dev_err(&pdev->dev, "Pinctrl node has not register regmap.\n");
  1141. return -EINVAL;
  1142. }
  1143. /* Only 8135 has two base addr, other SoCs have only one. */
  1144. node = of_parse_phandle(np, "mediatek,pctl-regmap", 1);
  1145. if (node) {
  1146. pctl->regmap2 = syscon_node_to_regmap(node);
  1147. if (IS_ERR(pctl->regmap2))
  1148. return PTR_ERR(pctl->regmap2);
  1149. }
  1150. pctl->devdata = data;
  1151. ret = mtk_pctrl_build_state(pdev);
  1152. if (ret) {
  1153. dev_err(&pdev->dev, "build state failed: %d\n", ret);
  1154. return -EINVAL;
  1155. }
  1156. pins = devm_kcalloc(&pdev->dev, pctl->devdata->npins, sizeof(*pins),
  1157. GFP_KERNEL);
  1158. if (!pins)
  1159. return -ENOMEM;
  1160. for (i = 0; i < pctl->devdata->npins; i++)
  1161. pins[i] = pctl->devdata->pins[i].pin;
  1162. pctl->pctl_desc.name = dev_name(&pdev->dev);
  1163. pctl->pctl_desc.owner = THIS_MODULE;
  1164. pctl->pctl_desc.pins = pins;
  1165. pctl->pctl_desc.npins = pctl->devdata->npins;
  1166. pctl->pctl_desc.confops = &mtk_pconf_ops;
  1167. pctl->pctl_desc.pctlops = &mtk_pctrl_ops;
  1168. pctl->pctl_desc.pmxops = &mtk_pmx_ops;
  1169. pctl->dev = &pdev->dev;
  1170. pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->pctl_desc,
  1171. pctl);
  1172. if (IS_ERR(pctl->pctl_dev)) {
  1173. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  1174. return PTR_ERR(pctl->pctl_dev);
  1175. }
  1176. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  1177. if (!pctl->chip)
  1178. return -ENOMEM;
  1179. *pctl->chip = mtk_gpio_chip;
  1180. pctl->chip->ngpio = pctl->devdata->npins;
  1181. pctl->chip->label = dev_name(&pdev->dev);
  1182. pctl->chip->parent = &pdev->dev;
  1183. pctl->chip->base = -1;
  1184. ret = gpiochip_add_data(pctl->chip, pctl);
  1185. if (ret)
  1186. return -EINVAL;
  1187. /* Register the GPIO to pin mappings. */
  1188. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  1189. 0, 0, pctl->devdata->npins);
  1190. if (ret) {
  1191. ret = -EINVAL;
  1192. goto chip_error;
  1193. }
  1194. if (!of_property_read_bool(np, "interrupt-controller"))
  1195. return 0;
  1196. /* Get EINT register base from dts. */
  1197. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1198. if (!res) {
  1199. dev_err(&pdev->dev, "Unable to get Pinctrl resource\n");
  1200. ret = -EINVAL;
  1201. goto chip_error;
  1202. }
  1203. pctl->eint_reg_base = devm_ioremap_resource(&pdev->dev, res);
  1204. if (IS_ERR(pctl->eint_reg_base)) {
  1205. ret = -EINVAL;
  1206. goto chip_error;
  1207. }
  1208. ports_buf = pctl->devdata->eint_offsets.ports;
  1209. pctl->wake_mask = devm_kcalloc(&pdev->dev, ports_buf,
  1210. sizeof(*pctl->wake_mask), GFP_KERNEL);
  1211. if (!pctl->wake_mask) {
  1212. ret = -ENOMEM;
  1213. goto chip_error;
  1214. }
  1215. pctl->cur_mask = devm_kcalloc(&pdev->dev, ports_buf,
  1216. sizeof(*pctl->cur_mask), GFP_KERNEL);
  1217. if (!pctl->cur_mask) {
  1218. ret = -ENOMEM;
  1219. goto chip_error;
  1220. }
  1221. pctl->eint_dual_edges = devm_kcalloc(&pdev->dev, pctl->devdata->ap_num,
  1222. sizeof(int), GFP_KERNEL);
  1223. if (!pctl->eint_dual_edges) {
  1224. ret = -ENOMEM;
  1225. goto chip_error;
  1226. }
  1227. irq = irq_of_parse_and_map(np, 0);
  1228. if (!irq) {
  1229. dev_err(&pdev->dev, "couldn't parse and map irq\n");
  1230. ret = -EINVAL;
  1231. goto chip_error;
  1232. }
  1233. pctl->domain = irq_domain_add_linear(np,
  1234. pctl->devdata->ap_num, &irq_domain_simple_ops, NULL);
  1235. if (!pctl->domain) {
  1236. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  1237. ret = -ENOMEM;
  1238. goto chip_error;
  1239. }
  1240. mtk_eint_init(pctl);
  1241. for (i = 0; i < pctl->devdata->ap_num; i++) {
  1242. int virq = irq_create_mapping(pctl->domain, i);
  1243. irq_set_chip_and_handler(virq, &mtk_pinctrl_irq_chip,
  1244. handle_level_irq);
  1245. irq_set_chip_data(virq, pctl);
  1246. }
  1247. irq_set_chained_handler_and_data(irq, mtk_eint_irq_handler, pctl);
  1248. return 0;
  1249. chip_error:
  1250. gpiochip_remove(pctl->chip);
  1251. return ret;
  1252. }