pinctrl-mtk-common.c 36 KB

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