pinctrl-mtk-common.c 38 KB

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