pinctrl-mtk-common.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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;
  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. ret = pctl->devdata->spec_pull_set(mtk_get_regmap(pctl, pin),
  258. pin, pctl->devdata->port_align, isup, arg);
  259. if (!ret)
  260. return 0;
  261. }
  262. /* For generic pull config, default arg value should be 0 or 1. */
  263. if (arg != 0 && arg != 1) {
  264. dev_err(pctl->dev, "invalid pull-up argument %d on pin %d .\n",
  265. arg, pin);
  266. return -EINVAL;
  267. }
  268. bit = BIT(pin & 0xf);
  269. if (enable)
  270. reg_pullen = SET_ADDR(mtk_get_port(pctl, pin) +
  271. pctl->devdata->pullen_offset, pctl);
  272. else
  273. reg_pullen = CLR_ADDR(mtk_get_port(pctl, pin) +
  274. pctl->devdata->pullen_offset, pctl);
  275. if (isup)
  276. reg_pullsel = SET_ADDR(mtk_get_port(pctl, pin) +
  277. pctl->devdata->pullsel_offset, pctl);
  278. else
  279. reg_pullsel = CLR_ADDR(mtk_get_port(pctl, pin) +
  280. pctl->devdata->pullsel_offset, pctl);
  281. regmap_write(mtk_get_regmap(pctl, pin), reg_pullen, bit);
  282. regmap_write(mtk_get_regmap(pctl, pin), reg_pullsel, bit);
  283. return 0;
  284. }
  285. static int mtk_pconf_parse_conf(struct pinctrl_dev *pctldev,
  286. unsigned int pin, enum pin_config_param param,
  287. enum pin_config_param arg)
  288. {
  289. int ret = 0;
  290. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  291. switch (param) {
  292. case PIN_CONFIG_BIAS_DISABLE:
  293. ret = mtk_pconf_set_pull_select(pctl, pin, false, false, arg);
  294. break;
  295. case PIN_CONFIG_BIAS_PULL_UP:
  296. ret = mtk_pconf_set_pull_select(pctl, pin, true, true, arg);
  297. break;
  298. case PIN_CONFIG_BIAS_PULL_DOWN:
  299. ret = mtk_pconf_set_pull_select(pctl, pin, true, false, arg);
  300. break;
  301. case PIN_CONFIG_INPUT_ENABLE:
  302. mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
  303. ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
  304. break;
  305. case PIN_CONFIG_OUTPUT:
  306. mtk_gpio_set(pctl->chip, pin, arg);
  307. ret = mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
  308. break;
  309. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  310. mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
  311. ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
  312. break;
  313. case PIN_CONFIG_DRIVE_STRENGTH:
  314. ret = mtk_pconf_set_driving(pctl, pin, arg);
  315. break;
  316. default:
  317. ret = -EINVAL;
  318. }
  319. return ret;
  320. }
  321. static int mtk_pconf_group_get(struct pinctrl_dev *pctldev,
  322. unsigned group,
  323. unsigned long *config)
  324. {
  325. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  326. *config = pctl->groups[group].config;
  327. return 0;
  328. }
  329. static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  330. unsigned long *configs, unsigned num_configs)
  331. {
  332. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  333. struct mtk_pinctrl_group *g = &pctl->groups[group];
  334. int i, ret;
  335. for (i = 0; i < num_configs; i++) {
  336. ret = mtk_pconf_parse_conf(pctldev, g->pin,
  337. pinconf_to_config_param(configs[i]),
  338. pinconf_to_config_argument(configs[i]));
  339. if (ret < 0)
  340. return ret;
  341. g->config = configs[i];
  342. }
  343. return 0;
  344. }
  345. static const struct pinconf_ops mtk_pconf_ops = {
  346. .pin_config_group_get = mtk_pconf_group_get,
  347. .pin_config_group_set = mtk_pconf_group_set,
  348. };
  349. static struct mtk_pinctrl_group *
  350. mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *pctl, u32 pin)
  351. {
  352. int i;
  353. for (i = 0; i < pctl->ngroups; i++) {
  354. struct mtk_pinctrl_group *grp = pctl->groups + i;
  355. if (grp->pin == pin)
  356. return grp;
  357. }
  358. return NULL;
  359. }
  360. static const struct mtk_desc_function *mtk_pctrl_find_function_by_pin(
  361. struct mtk_pinctrl *pctl, u32 pin_num, u32 fnum)
  362. {
  363. const struct mtk_desc_pin *pin = pctl->devdata->pins + pin_num;
  364. const struct mtk_desc_function *func = pin->functions;
  365. while (func && func->name) {
  366. if (func->muxval == fnum)
  367. return func;
  368. func++;
  369. }
  370. return NULL;
  371. }
  372. static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *pctl,
  373. u32 pin_num, u32 fnum)
  374. {
  375. int i;
  376. for (i = 0; i < pctl->devdata->npins; i++) {
  377. const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
  378. if (pin->pin.number == pin_num) {
  379. const struct mtk_desc_function *func =
  380. pin->functions;
  381. while (func && func->name) {
  382. if (func->muxval == fnum)
  383. return true;
  384. func++;
  385. }
  386. break;
  387. }
  388. }
  389. return false;
  390. }
  391. static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
  392. u32 pin, u32 fnum, struct mtk_pinctrl_group *grp,
  393. struct pinctrl_map **map, unsigned *reserved_maps,
  394. unsigned *num_maps)
  395. {
  396. bool ret;
  397. if (*num_maps == *reserved_maps)
  398. return -ENOSPC;
  399. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  400. (*map)[*num_maps].data.mux.group = grp->name;
  401. ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
  402. if (!ret) {
  403. dev_err(pctl->dev, "invalid function %d on pin %d .\n",
  404. fnum, pin);
  405. return -EINVAL;
  406. }
  407. (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
  408. (*num_maps)++;
  409. return 0;
  410. }
  411. static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  412. struct device_node *node,
  413. struct pinctrl_map **map,
  414. unsigned *reserved_maps,
  415. unsigned *num_maps)
  416. {
  417. struct property *pins;
  418. u32 pinfunc, pin, func;
  419. int num_pins, num_funcs, maps_per_pin;
  420. unsigned long *configs;
  421. unsigned int num_configs;
  422. bool has_config = 0;
  423. int i, err;
  424. unsigned reserve = 0;
  425. struct mtk_pinctrl_group *grp;
  426. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  427. pins = of_find_property(node, "pinmux", NULL);
  428. if (!pins) {
  429. dev_err(pctl->dev, "missing pins property in node %s .\n",
  430. node->name);
  431. return -EINVAL;
  432. }
  433. err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
  434. &num_configs);
  435. if (err)
  436. return err;
  437. if (num_configs)
  438. has_config = 1;
  439. num_pins = pins->length / sizeof(u32);
  440. num_funcs = num_pins;
  441. maps_per_pin = 0;
  442. if (num_funcs)
  443. maps_per_pin++;
  444. if (has_config && num_pins >= 1)
  445. maps_per_pin++;
  446. if (!num_pins || !maps_per_pin) {
  447. err = -EINVAL;
  448. goto exit;
  449. }
  450. reserve = num_pins * maps_per_pin;
  451. err = pinctrl_utils_reserve_map(pctldev, map,
  452. reserved_maps, num_maps, reserve);
  453. if (err < 0)
  454. goto exit;
  455. for (i = 0; i < num_pins; i++) {
  456. err = of_property_read_u32_index(node, "pinmux",
  457. i, &pinfunc);
  458. if (err)
  459. goto exit;
  460. pin = MTK_GET_PIN_NO(pinfunc);
  461. func = MTK_GET_PIN_FUNC(pinfunc);
  462. if (pin >= pctl->devdata->npins ||
  463. func >= ARRAY_SIZE(mtk_gpio_functions)) {
  464. dev_err(pctl->dev, "invalid pins value.\n");
  465. err = -EINVAL;
  466. goto exit;
  467. }
  468. grp = mtk_pctrl_find_group_by_pin(pctl, pin);
  469. if (!grp) {
  470. dev_err(pctl->dev, "unable to match pin %d to group\n",
  471. pin);
  472. err = -EINVAL;
  473. goto exit;
  474. }
  475. err = mtk_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
  476. reserved_maps, num_maps);
  477. if (err < 0)
  478. goto exit;
  479. if (has_config) {
  480. err = pinctrl_utils_add_map_configs(pctldev, map,
  481. reserved_maps, num_maps, grp->name,
  482. configs, num_configs,
  483. PIN_MAP_TYPE_CONFIGS_GROUP);
  484. if (err < 0)
  485. goto exit;
  486. }
  487. }
  488. err = 0;
  489. exit:
  490. kfree(configs);
  491. return err;
  492. }
  493. static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  494. struct device_node *np_config,
  495. struct pinctrl_map **map, unsigned *num_maps)
  496. {
  497. struct device_node *np;
  498. unsigned reserved_maps;
  499. int ret;
  500. *map = NULL;
  501. *num_maps = 0;
  502. reserved_maps = 0;
  503. for_each_child_of_node(np_config, np) {
  504. ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
  505. &reserved_maps, num_maps);
  506. if (ret < 0) {
  507. pinctrl_utils_free_map(pctldev, *map, *num_maps);
  508. of_node_put(np);
  509. return ret;
  510. }
  511. }
  512. return 0;
  513. }
  514. static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  515. {
  516. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  517. return pctl->ngroups;
  518. }
  519. static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  520. unsigned group)
  521. {
  522. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  523. return pctl->groups[group].name;
  524. }
  525. static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  526. unsigned group,
  527. const unsigned **pins,
  528. unsigned *num_pins)
  529. {
  530. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  531. *pins = (unsigned *)&pctl->groups[group].pin;
  532. *num_pins = 1;
  533. return 0;
  534. }
  535. static const struct pinctrl_ops mtk_pctrl_ops = {
  536. .dt_node_to_map = mtk_pctrl_dt_node_to_map,
  537. .dt_free_map = pinctrl_utils_free_map,
  538. .get_groups_count = mtk_pctrl_get_groups_count,
  539. .get_group_name = mtk_pctrl_get_group_name,
  540. .get_group_pins = mtk_pctrl_get_group_pins,
  541. };
  542. static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  543. {
  544. return ARRAY_SIZE(mtk_gpio_functions);
  545. }
  546. static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  547. unsigned selector)
  548. {
  549. return mtk_gpio_functions[selector];
  550. }
  551. static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  552. unsigned function,
  553. const char * const **groups,
  554. unsigned * const num_groups)
  555. {
  556. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  557. *groups = pctl->grp_names;
  558. *num_groups = pctl->ngroups;
  559. return 0;
  560. }
  561. static int mtk_pmx_set_mode(struct pinctrl_dev *pctldev,
  562. unsigned long pin, unsigned long mode)
  563. {
  564. unsigned int reg_addr;
  565. unsigned char bit;
  566. unsigned int val;
  567. unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
  568. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  569. if (pctl->devdata->spec_pinmux_set)
  570. pctl->devdata->spec_pinmux_set(mtk_get_regmap(pctl, pin),
  571. pin, mode);
  572. reg_addr = ((pin / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
  573. + pctl->devdata->pinmux_offset;
  574. mode &= mask;
  575. bit = pin % MAX_GPIO_MODE_PER_REG;
  576. mask <<= (GPIO_MODE_BITS * bit);
  577. val = (mode << (GPIO_MODE_BITS * bit));
  578. return regmap_update_bits(mtk_get_regmap(pctl, pin),
  579. reg_addr, mask, val);
  580. }
  581. static const struct mtk_desc_pin *
  582. mtk_find_pin_by_eint_num(struct mtk_pinctrl *pctl, unsigned int eint_num)
  583. {
  584. int i;
  585. const struct mtk_desc_pin *pin;
  586. for (i = 0; i < pctl->devdata->npins; i++) {
  587. pin = pctl->devdata->pins + i;
  588. if (pin->eint.eintnum == eint_num)
  589. return pin;
  590. }
  591. return NULL;
  592. }
  593. static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
  594. unsigned function,
  595. unsigned group)
  596. {
  597. bool ret;
  598. const struct mtk_desc_function *desc;
  599. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  600. struct mtk_pinctrl_group *g = pctl->groups + group;
  601. ret = mtk_pctrl_is_function_valid(pctl, g->pin, function);
  602. if (!ret) {
  603. dev_err(pctl->dev, "invalid function %d on group %d .\n",
  604. function, group);
  605. return -EINVAL;
  606. }
  607. desc = mtk_pctrl_find_function_by_pin(pctl, g->pin, function);
  608. if (!desc)
  609. return -EINVAL;
  610. mtk_pmx_set_mode(pctldev, g->pin, desc->muxval);
  611. return 0;
  612. }
  613. static int mtk_pmx_find_gpio_mode(struct mtk_pinctrl *pctl,
  614. unsigned offset)
  615. {
  616. const struct mtk_desc_pin *pin = pctl->devdata->pins + offset;
  617. const struct mtk_desc_function *func = pin->functions;
  618. while (func && func->name) {
  619. if (!strncmp(func->name, GPIO_MODE_PREFIX,
  620. sizeof(GPIO_MODE_PREFIX)-1))
  621. return func->muxval;
  622. func++;
  623. }
  624. return -EINVAL;
  625. }
  626. static int mtk_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
  627. struct pinctrl_gpio_range *range,
  628. unsigned offset)
  629. {
  630. int muxval;
  631. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  632. muxval = mtk_pmx_find_gpio_mode(pctl, offset);
  633. if (muxval < 0) {
  634. dev_err(pctl->dev, "invalid gpio pin %d.\n", offset);
  635. return -EINVAL;
  636. }
  637. mtk_pmx_set_mode(pctldev, offset, muxval);
  638. mtk_pconf_set_ies_smt(pctl, offset, 1, PIN_CONFIG_INPUT_ENABLE);
  639. return 0;
  640. }
  641. static const struct pinmux_ops mtk_pmx_ops = {
  642. .get_functions_count = mtk_pmx_get_funcs_cnt,
  643. .get_function_name = mtk_pmx_get_func_name,
  644. .get_function_groups = mtk_pmx_get_func_groups,
  645. .set_mux = mtk_pmx_set_mux,
  646. .gpio_set_direction = mtk_pmx_gpio_set_direction,
  647. .gpio_request_enable = mtk_pmx_gpio_request_enable,
  648. };
  649. static int mtk_gpio_direction_input(struct gpio_chip *chip,
  650. unsigned offset)
  651. {
  652. return pinctrl_gpio_direction_input(chip->base + offset);
  653. }
  654. static int mtk_gpio_direction_output(struct gpio_chip *chip,
  655. unsigned offset, int value)
  656. {
  657. mtk_gpio_set(chip, offset, value);
  658. return pinctrl_gpio_direction_output(chip->base + offset);
  659. }
  660. static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  661. {
  662. unsigned int reg_addr;
  663. unsigned int bit;
  664. unsigned int read_val = 0;
  665. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  666. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
  667. bit = BIT(offset & 0xf);
  668. if (pctl->devdata->spec_dir_set)
  669. pctl->devdata->spec_dir_set(&reg_addr, offset);
  670. regmap_read(pctl->regmap1, reg_addr, &read_val);
  671. return !(read_val & bit);
  672. }
  673. static int mtk_gpio_get(struct gpio_chip *chip, unsigned offset)
  674. {
  675. unsigned int reg_addr;
  676. unsigned int bit;
  677. unsigned int read_val = 0;
  678. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  679. reg_addr = mtk_get_port(pctl, offset) +
  680. pctl->devdata->din_offset;
  681. bit = BIT(offset & 0xf);
  682. regmap_read(pctl->regmap1, reg_addr, &read_val);
  683. return !!(read_val & bit);
  684. }
  685. static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  686. {
  687. const struct mtk_desc_pin *pin;
  688. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  689. int irq;
  690. pin = pctl->devdata->pins + offset;
  691. if (pin->eint.eintnum == NO_EINT_SUPPORT)
  692. return -EINVAL;
  693. irq = irq_find_mapping(pctl->domain, pin->eint.eintnum);
  694. if (!irq)
  695. return -EINVAL;
  696. return irq;
  697. }
  698. static int mtk_pinctrl_irq_request_resources(struct irq_data *d)
  699. {
  700. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  701. const struct mtk_desc_pin *pin;
  702. int ret;
  703. pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
  704. if (!pin) {
  705. dev_err(pctl->dev, "Can not find pin\n");
  706. return -EINVAL;
  707. }
  708. ret = gpiochip_lock_as_irq(pctl->chip, pin->pin.number);
  709. if (ret) {
  710. dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
  711. irqd_to_hwirq(d));
  712. return ret;
  713. }
  714. /* set mux to INT mode */
  715. mtk_pmx_set_mode(pctl->pctl_dev, pin->pin.number, pin->eint.eintmux);
  716. /* set gpio direction to input */
  717. mtk_pmx_gpio_set_direction(pctl->pctl_dev, NULL, pin->pin.number, true);
  718. /* set input-enable */
  719. mtk_pconf_set_ies_smt(pctl, pin->pin.number, 1, PIN_CONFIG_INPUT_ENABLE);
  720. return 0;
  721. }
  722. static void mtk_pinctrl_irq_release_resources(struct irq_data *d)
  723. {
  724. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  725. const struct mtk_desc_pin *pin;
  726. pin = mtk_find_pin_by_eint_num(pctl, d->hwirq);
  727. if (!pin) {
  728. dev_err(pctl->dev, "Can not find pin\n");
  729. return;
  730. }
  731. gpiochip_unlock_as_irq(pctl->chip, pin->pin.number);
  732. }
  733. static void __iomem *mtk_eint_get_offset(struct mtk_pinctrl *pctl,
  734. unsigned int eint_num, unsigned int offset)
  735. {
  736. unsigned int eint_base = 0;
  737. void __iomem *reg;
  738. if (eint_num >= pctl->devdata->ap_num)
  739. eint_base = pctl->devdata->ap_num;
  740. reg = pctl->eint_reg_base + offset + ((eint_num - eint_base) / 32) * 4;
  741. return reg;
  742. }
  743. /*
  744. * mtk_can_en_debounce: Check the EINT number is able to enable debounce or not
  745. * @eint_num: the EINT number to setmtk_pinctrl
  746. */
  747. static unsigned int mtk_eint_can_en_debounce(struct mtk_pinctrl *pctl,
  748. unsigned int eint_num)
  749. {
  750. unsigned int sens;
  751. unsigned int bit = BIT(eint_num % 32);
  752. const struct mtk_eint_offsets *eint_offsets =
  753. &pctl->devdata->eint_offsets;
  754. void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
  755. eint_offsets->sens);
  756. if (readl(reg) & bit)
  757. sens = MT_LEVEL_SENSITIVE;
  758. else
  759. sens = MT_EDGE_SENSITIVE;
  760. if ((eint_num < pctl->devdata->db_cnt) && (sens != MT_EDGE_SENSITIVE))
  761. return 1;
  762. else
  763. return 0;
  764. }
  765. /*
  766. * mtk_eint_get_mask: To get the eint mask
  767. * @eint_num: the EINT number to get
  768. */
  769. static unsigned int mtk_eint_get_mask(struct mtk_pinctrl *pctl,
  770. unsigned int eint_num)
  771. {
  772. unsigned int bit = BIT(eint_num % 32);
  773. const struct mtk_eint_offsets *eint_offsets =
  774. &pctl->devdata->eint_offsets;
  775. void __iomem *reg = mtk_eint_get_offset(pctl, eint_num,
  776. eint_offsets->mask);
  777. return !!(readl(reg) & bit);
  778. }
  779. static int mtk_eint_flip_edge(struct mtk_pinctrl *pctl, int hwirq)
  780. {
  781. int start_level, curr_level;
  782. unsigned int reg_offset;
  783. const struct mtk_eint_offsets *eint_offsets = &(pctl->devdata->eint_offsets);
  784. u32 mask = BIT(hwirq & 0x1f);
  785. u32 port = (hwirq >> 5) & eint_offsets->port_mask;
  786. void __iomem *reg = pctl->eint_reg_base + (port << 2);
  787. const struct mtk_desc_pin *pin;
  788. pin = mtk_find_pin_by_eint_num(pctl, hwirq);
  789. curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
  790. do {
  791. start_level = curr_level;
  792. if (start_level)
  793. reg_offset = eint_offsets->pol_clr;
  794. else
  795. reg_offset = eint_offsets->pol_set;
  796. writel(mask, reg + reg_offset);
  797. curr_level = mtk_gpio_get(pctl->chip, pin->pin.number);
  798. } while (start_level != curr_level);
  799. return start_level;
  800. }
  801. static void mtk_eint_mask(struct irq_data *d)
  802. {
  803. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  804. const struct mtk_eint_offsets *eint_offsets =
  805. &pctl->devdata->eint_offsets;
  806. u32 mask = BIT(d->hwirq & 0x1f);
  807. void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
  808. eint_offsets->mask_set);
  809. writel(mask, reg);
  810. }
  811. static void mtk_eint_unmask(struct irq_data *d)
  812. {
  813. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  814. const struct mtk_eint_offsets *eint_offsets =
  815. &pctl->devdata->eint_offsets;
  816. u32 mask = BIT(d->hwirq & 0x1f);
  817. void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
  818. eint_offsets->mask_clr);
  819. writel(mask, reg);
  820. if (pctl->eint_dual_edges[d->hwirq])
  821. mtk_eint_flip_edge(pctl, d->hwirq);
  822. }
  823. static int mtk_gpio_set_debounce(struct gpio_chip *chip, unsigned offset,
  824. unsigned debounce)
  825. {
  826. struct mtk_pinctrl *pctl = dev_get_drvdata(chip->parent);
  827. int eint_num, virq, eint_offset;
  828. unsigned int set_offset, bit, clr_bit, clr_offset, rst, i, unmask, dbnc;
  829. static const unsigned int debounce_time[] = {500, 1000, 16000, 32000, 64000,
  830. 128000, 256000};
  831. const struct mtk_desc_pin *pin;
  832. struct irq_data *d;
  833. pin = pctl->devdata->pins + offset;
  834. if (pin->eint.eintnum == NO_EINT_SUPPORT)
  835. return -EINVAL;
  836. eint_num = pin->eint.eintnum;
  837. virq = irq_find_mapping(pctl->domain, eint_num);
  838. eint_offset = (eint_num % 4) * 8;
  839. d = irq_get_irq_data(virq);
  840. set_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_set;
  841. clr_offset = (eint_num / 4) * 4 + pctl->devdata->eint_offsets.dbnc_clr;
  842. if (!mtk_eint_can_en_debounce(pctl, eint_num))
  843. return -ENOSYS;
  844. dbnc = ARRAY_SIZE(debounce_time);
  845. for (i = 0; i < ARRAY_SIZE(debounce_time); i++) {
  846. if (debounce <= debounce_time[i]) {
  847. dbnc = i;
  848. break;
  849. }
  850. }
  851. if (!mtk_eint_get_mask(pctl, eint_num)) {
  852. mtk_eint_mask(d);
  853. unmask = 1;
  854. } else {
  855. unmask = 0;
  856. }
  857. clr_bit = 0xff << eint_offset;
  858. writel(clr_bit, pctl->eint_reg_base + clr_offset);
  859. bit = ((dbnc << EINT_DBNC_SET_DBNC_BITS) | EINT_DBNC_SET_EN) <<
  860. eint_offset;
  861. rst = EINT_DBNC_RST_BIT << eint_offset;
  862. writel(rst | bit, pctl->eint_reg_base + set_offset);
  863. /* Delay a while (more than 2T) to wait for hw debounce counter reset
  864. work correctly */
  865. udelay(1);
  866. if (unmask == 1)
  867. mtk_eint_unmask(d);
  868. return 0;
  869. }
  870. static const struct gpio_chip mtk_gpio_chip = {
  871. .owner = THIS_MODULE,
  872. .request = gpiochip_generic_request,
  873. .free = gpiochip_generic_free,
  874. .get_direction = mtk_gpio_get_direction,
  875. .direction_input = mtk_gpio_direction_input,
  876. .direction_output = mtk_gpio_direction_output,
  877. .get = mtk_gpio_get,
  878. .set = mtk_gpio_set,
  879. .to_irq = mtk_gpio_to_irq,
  880. .set_debounce = mtk_gpio_set_debounce,
  881. .of_gpio_n_cells = 2,
  882. };
  883. static int mtk_eint_set_type(struct irq_data *d,
  884. unsigned int type)
  885. {
  886. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  887. const struct mtk_eint_offsets *eint_offsets =
  888. &pctl->devdata->eint_offsets;
  889. u32 mask = BIT(d->hwirq & 0x1f);
  890. void __iomem *reg;
  891. if (((type & IRQ_TYPE_EDGE_BOTH) && (type & IRQ_TYPE_LEVEL_MASK)) ||
  892. ((type & IRQ_TYPE_LEVEL_MASK) == IRQ_TYPE_LEVEL_MASK)) {
  893. dev_err(pctl->dev, "Can't configure IRQ%d (EINT%lu) for type 0x%X\n",
  894. d->irq, d->hwirq, type);
  895. return -EINVAL;
  896. }
  897. if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH)
  898. pctl->eint_dual_edges[d->hwirq] = 1;
  899. else
  900. pctl->eint_dual_edges[d->hwirq] = 0;
  901. if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_EDGE_FALLING)) {
  902. reg = mtk_eint_get_offset(pctl, d->hwirq,
  903. eint_offsets->pol_clr);
  904. writel(mask, reg);
  905. } else {
  906. reg = mtk_eint_get_offset(pctl, d->hwirq,
  907. eint_offsets->pol_set);
  908. writel(mask, reg);
  909. }
  910. if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
  911. reg = mtk_eint_get_offset(pctl, d->hwirq,
  912. eint_offsets->sens_clr);
  913. writel(mask, reg);
  914. } else {
  915. reg = mtk_eint_get_offset(pctl, d->hwirq,
  916. eint_offsets->sens_set);
  917. writel(mask, reg);
  918. }
  919. if (pctl->eint_dual_edges[d->hwirq])
  920. mtk_eint_flip_edge(pctl, d->hwirq);
  921. return 0;
  922. }
  923. static int mtk_eint_irq_set_wake(struct irq_data *d, unsigned int on)
  924. {
  925. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  926. int shift = d->hwirq & 0x1f;
  927. int reg = d->hwirq >> 5;
  928. if (on)
  929. pctl->wake_mask[reg] |= BIT(shift);
  930. else
  931. pctl->wake_mask[reg] &= ~BIT(shift);
  932. return 0;
  933. }
  934. static void mtk_eint_chip_write_mask(const struct mtk_eint_offsets *chip,
  935. void __iomem *eint_reg_base, u32 *buf)
  936. {
  937. int port;
  938. void __iomem *reg;
  939. for (port = 0; port < chip->ports; port++) {
  940. reg = eint_reg_base + (port << 2);
  941. writel_relaxed(~buf[port], reg + chip->mask_set);
  942. writel_relaxed(buf[port], reg + chip->mask_clr);
  943. }
  944. }
  945. static void mtk_eint_chip_read_mask(const struct mtk_eint_offsets *chip,
  946. void __iomem *eint_reg_base, u32 *buf)
  947. {
  948. int port;
  949. void __iomem *reg;
  950. for (port = 0; port < chip->ports; port++) {
  951. reg = eint_reg_base + chip->mask + (port << 2);
  952. buf[port] = ~readl_relaxed(reg);
  953. /* Mask is 0 when irq is enabled, and 1 when disabled. */
  954. }
  955. }
  956. static int mtk_eint_suspend(struct device *device)
  957. {
  958. void __iomem *reg;
  959. struct mtk_pinctrl *pctl = dev_get_drvdata(device);
  960. const struct mtk_eint_offsets *eint_offsets =
  961. &pctl->devdata->eint_offsets;
  962. reg = pctl->eint_reg_base;
  963. mtk_eint_chip_read_mask(eint_offsets, reg, pctl->cur_mask);
  964. mtk_eint_chip_write_mask(eint_offsets, reg, pctl->wake_mask);
  965. return 0;
  966. }
  967. static int mtk_eint_resume(struct device *device)
  968. {
  969. struct mtk_pinctrl *pctl = dev_get_drvdata(device);
  970. const struct mtk_eint_offsets *eint_offsets =
  971. &pctl->devdata->eint_offsets;
  972. mtk_eint_chip_write_mask(eint_offsets,
  973. pctl->eint_reg_base, pctl->cur_mask);
  974. return 0;
  975. }
  976. const struct dev_pm_ops mtk_eint_pm_ops = {
  977. .suspend_noirq = mtk_eint_suspend,
  978. .resume_noirq = mtk_eint_resume,
  979. };
  980. static void mtk_eint_ack(struct irq_data *d)
  981. {
  982. struct mtk_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  983. const struct mtk_eint_offsets *eint_offsets =
  984. &pctl->devdata->eint_offsets;
  985. u32 mask = BIT(d->hwirq & 0x1f);
  986. void __iomem *reg = mtk_eint_get_offset(pctl, d->hwirq,
  987. eint_offsets->ack);
  988. writel(mask, reg);
  989. }
  990. static struct irq_chip mtk_pinctrl_irq_chip = {
  991. .name = "mt-eint",
  992. .irq_disable = mtk_eint_mask,
  993. .irq_mask = mtk_eint_mask,
  994. .irq_unmask = mtk_eint_unmask,
  995. .irq_ack = mtk_eint_ack,
  996. .irq_set_type = mtk_eint_set_type,
  997. .irq_set_wake = mtk_eint_irq_set_wake,
  998. .irq_request_resources = mtk_pinctrl_irq_request_resources,
  999. .irq_release_resources = mtk_pinctrl_irq_release_resources,
  1000. };
  1001. static unsigned int mtk_eint_init(struct mtk_pinctrl *pctl)
  1002. {
  1003. const struct mtk_eint_offsets *eint_offsets =
  1004. &pctl->devdata->eint_offsets;
  1005. void __iomem *reg = pctl->eint_reg_base + eint_offsets->dom_en;
  1006. unsigned int i;
  1007. for (i = 0; i < pctl->devdata->ap_num; i += 32) {
  1008. writel(0xffffffff, reg);
  1009. reg += 4;
  1010. }
  1011. return 0;
  1012. }
  1013. static inline void
  1014. mtk_eint_debounce_process(struct mtk_pinctrl *pctl, int index)
  1015. {
  1016. unsigned int rst, ctrl_offset;
  1017. unsigned int bit, dbnc;
  1018. const struct mtk_eint_offsets *eint_offsets =
  1019. &pctl->devdata->eint_offsets;
  1020. ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_ctrl;
  1021. dbnc = readl(pctl->eint_reg_base + ctrl_offset);
  1022. bit = EINT_DBNC_SET_EN << ((index % 4) * 8);
  1023. if ((bit & dbnc) > 0) {
  1024. ctrl_offset = (index / 4) * 4 + eint_offsets->dbnc_set;
  1025. rst = EINT_DBNC_RST_BIT << ((index % 4) * 8);
  1026. writel(rst, pctl->eint_reg_base + ctrl_offset);
  1027. }
  1028. }
  1029. static void mtk_eint_irq_handler(struct irq_desc *desc)
  1030. {
  1031. struct irq_chip *chip = irq_desc_get_chip(desc);
  1032. struct mtk_pinctrl *pctl = irq_desc_get_handler_data(desc);
  1033. unsigned int status, eint_num;
  1034. int offset, index, virq;
  1035. const struct mtk_eint_offsets *eint_offsets =
  1036. &pctl->devdata->eint_offsets;
  1037. void __iomem *reg = mtk_eint_get_offset(pctl, 0, eint_offsets->stat);
  1038. int dual_edges, start_level, curr_level;
  1039. const struct mtk_desc_pin *pin;
  1040. chained_irq_enter(chip, desc);
  1041. for (eint_num = 0;
  1042. eint_num < pctl->devdata->ap_num;
  1043. eint_num += 32, reg += 4) {
  1044. status = readl(reg);
  1045. while (status) {
  1046. offset = __ffs(status);
  1047. index = eint_num + offset;
  1048. virq = irq_find_mapping(pctl->domain, index);
  1049. status &= ~BIT(offset);
  1050. dual_edges = pctl->eint_dual_edges[index];
  1051. if (dual_edges) {
  1052. /* Clear soft-irq in case we raised it
  1053. last time */
  1054. writel(BIT(offset), reg - eint_offsets->stat +
  1055. eint_offsets->soft_clr);
  1056. pin = mtk_find_pin_by_eint_num(pctl, index);
  1057. start_level = mtk_gpio_get(pctl->chip,
  1058. pin->pin.number);
  1059. }
  1060. generic_handle_irq(virq);
  1061. if (dual_edges) {
  1062. curr_level = mtk_eint_flip_edge(pctl, index);
  1063. /* If level changed, we might lost one edge
  1064. interrupt, raised it through soft-irq */
  1065. if (start_level != curr_level)
  1066. writel(BIT(offset), reg -
  1067. eint_offsets->stat +
  1068. eint_offsets->soft_set);
  1069. }
  1070. if (index < pctl->devdata->db_cnt)
  1071. mtk_eint_debounce_process(pctl , index);
  1072. }
  1073. }
  1074. chained_irq_exit(chip, desc);
  1075. }
  1076. static int mtk_pctrl_build_state(struct platform_device *pdev)
  1077. {
  1078. struct mtk_pinctrl *pctl = platform_get_drvdata(pdev);
  1079. int i;
  1080. pctl->ngroups = pctl->devdata->npins;
  1081. /* Allocate groups */
  1082. pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
  1083. sizeof(*pctl->groups), GFP_KERNEL);
  1084. if (!pctl->groups)
  1085. return -ENOMEM;
  1086. /* We assume that one pin is one group, use pin name as group name. */
  1087. pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
  1088. sizeof(*pctl->grp_names), GFP_KERNEL);
  1089. if (!pctl->grp_names)
  1090. return -ENOMEM;
  1091. for (i = 0; i < pctl->devdata->npins; i++) {
  1092. const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
  1093. struct mtk_pinctrl_group *group = pctl->groups + i;
  1094. group->name = pin->pin.name;
  1095. group->pin = pin->pin.number;
  1096. pctl->grp_names[i] = pin->pin.name;
  1097. }
  1098. return 0;
  1099. }
  1100. int mtk_pctrl_init(struct platform_device *pdev,
  1101. const struct mtk_pinctrl_devdata *data,
  1102. struct regmap *regmap)
  1103. {
  1104. struct pinctrl_pin_desc *pins;
  1105. struct mtk_pinctrl *pctl;
  1106. struct device_node *np = pdev->dev.of_node, *node;
  1107. struct property *prop;
  1108. struct resource *res;
  1109. int i, ret, irq, ports_buf;
  1110. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  1111. if (!pctl)
  1112. return -ENOMEM;
  1113. platform_set_drvdata(pdev, pctl);
  1114. prop = of_find_property(np, "pins-are-numbered", NULL);
  1115. if (!prop) {
  1116. dev_err(&pdev->dev, "only support pins-are-numbered format\n");
  1117. return -EINVAL;
  1118. }
  1119. node = of_parse_phandle(np, "mediatek,pctl-regmap", 0);
  1120. if (node) {
  1121. pctl->regmap1 = syscon_node_to_regmap(node);
  1122. if (IS_ERR(pctl->regmap1))
  1123. return PTR_ERR(pctl->regmap1);
  1124. } else if (regmap) {
  1125. pctl->regmap1 = regmap;
  1126. } else {
  1127. dev_err(&pdev->dev, "Pinctrl node has not register regmap.\n");
  1128. return -EINVAL;
  1129. }
  1130. /* Only 8135 has two base addr, other SoCs have only one. */
  1131. node = of_parse_phandle(np, "mediatek,pctl-regmap", 1);
  1132. if (node) {
  1133. pctl->regmap2 = syscon_node_to_regmap(node);
  1134. if (IS_ERR(pctl->regmap2))
  1135. return PTR_ERR(pctl->regmap2);
  1136. }
  1137. pctl->devdata = data;
  1138. ret = mtk_pctrl_build_state(pdev);
  1139. if (ret) {
  1140. dev_err(&pdev->dev, "build state failed: %d\n", ret);
  1141. return -EINVAL;
  1142. }
  1143. pins = devm_kcalloc(&pdev->dev, pctl->devdata->npins, sizeof(*pins),
  1144. GFP_KERNEL);
  1145. if (!pins)
  1146. return -ENOMEM;
  1147. for (i = 0; i < pctl->devdata->npins; i++)
  1148. pins[i] = pctl->devdata->pins[i].pin;
  1149. pctl->pctl_desc.name = dev_name(&pdev->dev);
  1150. pctl->pctl_desc.owner = THIS_MODULE;
  1151. pctl->pctl_desc.pins = pins;
  1152. pctl->pctl_desc.npins = pctl->devdata->npins;
  1153. pctl->pctl_desc.confops = &mtk_pconf_ops;
  1154. pctl->pctl_desc.pctlops = &mtk_pctrl_ops;
  1155. pctl->pctl_desc.pmxops = &mtk_pmx_ops;
  1156. pctl->dev = &pdev->dev;
  1157. pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->pctl_desc,
  1158. pctl);
  1159. if (IS_ERR(pctl->pctl_dev)) {
  1160. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  1161. return PTR_ERR(pctl->pctl_dev);
  1162. }
  1163. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  1164. if (!pctl->chip)
  1165. return -ENOMEM;
  1166. *pctl->chip = mtk_gpio_chip;
  1167. pctl->chip->ngpio = pctl->devdata->npins;
  1168. pctl->chip->label = dev_name(&pdev->dev);
  1169. pctl->chip->parent = &pdev->dev;
  1170. pctl->chip->base = -1;
  1171. ret = gpiochip_add_data(pctl->chip, pctl);
  1172. if (ret)
  1173. return -EINVAL;
  1174. /* Register the GPIO to pin mappings. */
  1175. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  1176. 0, 0, pctl->devdata->npins);
  1177. if (ret) {
  1178. ret = -EINVAL;
  1179. goto chip_error;
  1180. }
  1181. if (!of_property_read_bool(np, "interrupt-controller"))
  1182. return 0;
  1183. /* Get EINT register base from dts. */
  1184. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1185. if (!res) {
  1186. dev_err(&pdev->dev, "Unable to get Pinctrl resource\n");
  1187. ret = -EINVAL;
  1188. goto chip_error;
  1189. }
  1190. pctl->eint_reg_base = devm_ioremap_resource(&pdev->dev, res);
  1191. if (IS_ERR(pctl->eint_reg_base)) {
  1192. ret = -EINVAL;
  1193. goto chip_error;
  1194. }
  1195. ports_buf = pctl->devdata->eint_offsets.ports;
  1196. pctl->wake_mask = devm_kcalloc(&pdev->dev, ports_buf,
  1197. sizeof(*pctl->wake_mask), GFP_KERNEL);
  1198. if (!pctl->wake_mask) {
  1199. ret = -ENOMEM;
  1200. goto chip_error;
  1201. }
  1202. pctl->cur_mask = devm_kcalloc(&pdev->dev, ports_buf,
  1203. sizeof(*pctl->cur_mask), GFP_KERNEL);
  1204. if (!pctl->cur_mask) {
  1205. ret = -ENOMEM;
  1206. goto chip_error;
  1207. }
  1208. pctl->eint_dual_edges = devm_kcalloc(&pdev->dev, pctl->devdata->ap_num,
  1209. sizeof(int), GFP_KERNEL);
  1210. if (!pctl->eint_dual_edges) {
  1211. ret = -ENOMEM;
  1212. goto chip_error;
  1213. }
  1214. irq = irq_of_parse_and_map(np, 0);
  1215. if (!irq) {
  1216. dev_err(&pdev->dev, "couldn't parse and map irq\n");
  1217. ret = -EINVAL;
  1218. goto chip_error;
  1219. }
  1220. pctl->domain = irq_domain_add_linear(np,
  1221. pctl->devdata->ap_num, &irq_domain_simple_ops, NULL);
  1222. if (!pctl->domain) {
  1223. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  1224. ret = -ENOMEM;
  1225. goto chip_error;
  1226. }
  1227. mtk_eint_init(pctl);
  1228. for (i = 0; i < pctl->devdata->ap_num; i++) {
  1229. int virq = irq_create_mapping(pctl->domain, i);
  1230. irq_set_chip_and_handler(virq, &mtk_pinctrl_irq_chip,
  1231. handle_level_irq);
  1232. irq_set_chip_data(virq, pctl);
  1233. }
  1234. irq_set_chained_handler_and_data(irq, mtk_eint_irq_handler, pctl);
  1235. return 0;
  1236. chip_error:
  1237. gpiochip_remove(pctl->chip);
  1238. return ret;
  1239. }