pinctrl-mtk-common.c 32 KB

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