pinctrl-mtk-common.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. /*
  2. * mt65xx pinctrl driver based on Allwinner A1X pinctrl driver.
  3. * Copyright (c) 2014 MediaTek Inc.
  4. * Author: Hongzhou.Yang <hongzhou.yang@mediatek.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/io.h>
  16. #include <linux/gpio/driver.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/pinctrl/consumer.h>
  22. #include <linux/pinctrl/machine.h>
  23. #include <linux/pinctrl/pinconf.h>
  24. #include <linux/pinctrl/pinconf-generic.h>
  25. #include <linux/pinctrl/pinctrl.h>
  26. #include <linux/pinctrl/pinmux.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/bitops.h>
  30. #include <linux/regmap.h>
  31. #include <linux/mfd/syscon.h>
  32. #include <linux/delay.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/pm.h>
  35. #include <dt-bindings/pinctrl/mt65xx.h>
  36. #include "../core.h"
  37. #include "../pinconf.h"
  38. #include "../pinctrl-utils.h"
  39. #include "mtk-eint.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, r1r0;
  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. /* For special pins, bias-disable is set by R1R0,
  259. * the parameter should be "MTK_PUPD_SET_R1R0_00".
  260. */
  261. r1r0 = enable ? arg : MTK_PUPD_SET_R1R0_00;
  262. ret = pctl->devdata->spec_pull_set(mtk_get_regmap(pctl, pin),
  263. pin, pctl->devdata->port_align, isup, r1r0);
  264. if (!ret)
  265. return 0;
  266. }
  267. /* For generic pull config, default arg value should be 0 or 1. */
  268. if (arg != 0 && arg != 1) {
  269. dev_err(pctl->dev, "invalid pull-up argument %d on pin %d .\n",
  270. arg, pin);
  271. return -EINVAL;
  272. }
  273. bit = BIT(pin & 0xf);
  274. if (enable)
  275. reg_pullen = SET_ADDR(mtk_get_port(pctl, pin) +
  276. pctl->devdata->pullen_offset, pctl);
  277. else
  278. reg_pullen = CLR_ADDR(mtk_get_port(pctl, pin) +
  279. pctl->devdata->pullen_offset, pctl);
  280. if (isup)
  281. reg_pullsel = SET_ADDR(mtk_get_port(pctl, pin) +
  282. pctl->devdata->pullsel_offset, pctl);
  283. else
  284. reg_pullsel = CLR_ADDR(mtk_get_port(pctl, pin) +
  285. pctl->devdata->pullsel_offset, pctl);
  286. regmap_write(mtk_get_regmap(pctl, pin), reg_pullen, bit);
  287. regmap_write(mtk_get_regmap(pctl, pin), reg_pullsel, bit);
  288. return 0;
  289. }
  290. static int mtk_pconf_parse_conf(struct pinctrl_dev *pctldev,
  291. unsigned int pin, enum pin_config_param param,
  292. enum pin_config_param arg)
  293. {
  294. int ret = 0;
  295. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  296. switch (param) {
  297. case PIN_CONFIG_BIAS_DISABLE:
  298. ret = mtk_pconf_set_pull_select(pctl, pin, false, false, arg);
  299. break;
  300. case PIN_CONFIG_BIAS_PULL_UP:
  301. ret = mtk_pconf_set_pull_select(pctl, pin, true, true, arg);
  302. break;
  303. case PIN_CONFIG_BIAS_PULL_DOWN:
  304. ret = mtk_pconf_set_pull_select(pctl, pin, true, false, arg);
  305. break;
  306. case PIN_CONFIG_INPUT_ENABLE:
  307. mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
  308. ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
  309. break;
  310. case PIN_CONFIG_OUTPUT:
  311. mtk_gpio_set(pctl->chip, pin, arg);
  312. ret = mtk_pmx_gpio_set_direction(pctldev, NULL, pin, false);
  313. break;
  314. case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
  315. mtk_pmx_gpio_set_direction(pctldev, NULL, pin, true);
  316. ret = mtk_pconf_set_ies_smt(pctl, pin, arg, param);
  317. break;
  318. case PIN_CONFIG_DRIVE_STRENGTH:
  319. ret = mtk_pconf_set_driving(pctl, pin, arg);
  320. break;
  321. default:
  322. ret = -EINVAL;
  323. }
  324. return ret;
  325. }
  326. static int mtk_pconf_group_get(struct pinctrl_dev *pctldev,
  327. unsigned group,
  328. unsigned long *config)
  329. {
  330. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  331. *config = pctl->groups[group].config;
  332. return 0;
  333. }
  334. static int mtk_pconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
  335. unsigned long *configs, unsigned num_configs)
  336. {
  337. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  338. struct mtk_pinctrl_group *g = &pctl->groups[group];
  339. int i, ret;
  340. for (i = 0; i < num_configs; i++) {
  341. ret = mtk_pconf_parse_conf(pctldev, g->pin,
  342. pinconf_to_config_param(configs[i]),
  343. pinconf_to_config_argument(configs[i]));
  344. if (ret < 0)
  345. return ret;
  346. g->config = configs[i];
  347. }
  348. return 0;
  349. }
  350. static const struct pinconf_ops mtk_pconf_ops = {
  351. .pin_config_group_get = mtk_pconf_group_get,
  352. .pin_config_group_set = mtk_pconf_group_set,
  353. };
  354. static struct mtk_pinctrl_group *
  355. mtk_pctrl_find_group_by_pin(struct mtk_pinctrl *pctl, u32 pin)
  356. {
  357. int i;
  358. for (i = 0; i < pctl->ngroups; i++) {
  359. struct mtk_pinctrl_group *grp = pctl->groups + i;
  360. if (grp->pin == pin)
  361. return grp;
  362. }
  363. return NULL;
  364. }
  365. static const struct mtk_desc_function *mtk_pctrl_find_function_by_pin(
  366. struct mtk_pinctrl *pctl, u32 pin_num, u32 fnum)
  367. {
  368. const struct mtk_desc_pin *pin = pctl->devdata->pins + pin_num;
  369. const struct mtk_desc_function *func = pin->functions;
  370. while (func && func->name) {
  371. if (func->muxval == fnum)
  372. return func;
  373. func++;
  374. }
  375. return NULL;
  376. }
  377. static bool mtk_pctrl_is_function_valid(struct mtk_pinctrl *pctl,
  378. u32 pin_num, u32 fnum)
  379. {
  380. int i;
  381. for (i = 0; i < pctl->devdata->npins; i++) {
  382. const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
  383. if (pin->pin.number == pin_num) {
  384. const struct mtk_desc_function *func =
  385. pin->functions;
  386. while (func && func->name) {
  387. if (func->muxval == fnum)
  388. return true;
  389. func++;
  390. }
  391. break;
  392. }
  393. }
  394. return false;
  395. }
  396. static int mtk_pctrl_dt_node_to_map_func(struct mtk_pinctrl *pctl,
  397. u32 pin, u32 fnum, struct mtk_pinctrl_group *grp,
  398. struct pinctrl_map **map, unsigned *reserved_maps,
  399. unsigned *num_maps)
  400. {
  401. bool ret;
  402. if (*num_maps == *reserved_maps)
  403. return -ENOSPC;
  404. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  405. (*map)[*num_maps].data.mux.group = grp->name;
  406. ret = mtk_pctrl_is_function_valid(pctl, pin, fnum);
  407. if (!ret) {
  408. dev_err(pctl->dev, "invalid function %d on pin %d .\n",
  409. fnum, pin);
  410. return -EINVAL;
  411. }
  412. (*map)[*num_maps].data.mux.function = mtk_gpio_functions[fnum];
  413. (*num_maps)++;
  414. return 0;
  415. }
  416. static int mtk_pctrl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  417. struct device_node *node,
  418. struct pinctrl_map **map,
  419. unsigned *reserved_maps,
  420. unsigned *num_maps)
  421. {
  422. struct property *pins;
  423. u32 pinfunc, pin, func;
  424. int num_pins, num_funcs, maps_per_pin;
  425. unsigned long *configs;
  426. unsigned int num_configs;
  427. bool has_config = false;
  428. int i, err;
  429. unsigned reserve = 0;
  430. struct mtk_pinctrl_group *grp;
  431. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  432. pins = of_find_property(node, "pinmux", NULL);
  433. if (!pins) {
  434. dev_err(pctl->dev, "missing pins property in node %s .\n",
  435. node->name);
  436. return -EINVAL;
  437. }
  438. err = pinconf_generic_parse_dt_config(node, pctldev, &configs,
  439. &num_configs);
  440. if (err)
  441. return err;
  442. if (num_configs)
  443. has_config = true;
  444. num_pins = pins->length / sizeof(u32);
  445. num_funcs = num_pins;
  446. maps_per_pin = 0;
  447. if (num_funcs)
  448. maps_per_pin++;
  449. if (has_config && num_pins >= 1)
  450. maps_per_pin++;
  451. if (!num_pins || !maps_per_pin) {
  452. err = -EINVAL;
  453. goto exit;
  454. }
  455. reserve = num_pins * maps_per_pin;
  456. err = pinctrl_utils_reserve_map(pctldev, map,
  457. reserved_maps, num_maps, reserve);
  458. if (err < 0)
  459. goto exit;
  460. for (i = 0; i < num_pins; i++) {
  461. err = of_property_read_u32_index(node, "pinmux",
  462. i, &pinfunc);
  463. if (err)
  464. goto exit;
  465. pin = MTK_GET_PIN_NO(pinfunc);
  466. func = MTK_GET_PIN_FUNC(pinfunc);
  467. if (pin >= pctl->devdata->npins ||
  468. func >= ARRAY_SIZE(mtk_gpio_functions)) {
  469. dev_err(pctl->dev, "invalid pins value.\n");
  470. err = -EINVAL;
  471. goto exit;
  472. }
  473. grp = mtk_pctrl_find_group_by_pin(pctl, pin);
  474. if (!grp) {
  475. dev_err(pctl->dev, "unable to match pin %d to group\n",
  476. pin);
  477. err = -EINVAL;
  478. goto exit;
  479. }
  480. err = mtk_pctrl_dt_node_to_map_func(pctl, pin, func, grp, map,
  481. reserved_maps, num_maps);
  482. if (err < 0)
  483. goto exit;
  484. if (has_config) {
  485. err = pinctrl_utils_add_map_configs(pctldev, map,
  486. reserved_maps, num_maps, grp->name,
  487. configs, num_configs,
  488. PIN_MAP_TYPE_CONFIGS_GROUP);
  489. if (err < 0)
  490. goto exit;
  491. }
  492. }
  493. err = 0;
  494. exit:
  495. kfree(configs);
  496. return err;
  497. }
  498. static int mtk_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  499. struct device_node *np_config,
  500. struct pinctrl_map **map, unsigned *num_maps)
  501. {
  502. struct device_node *np;
  503. unsigned reserved_maps;
  504. int ret;
  505. *map = NULL;
  506. *num_maps = 0;
  507. reserved_maps = 0;
  508. for_each_child_of_node(np_config, np) {
  509. ret = mtk_pctrl_dt_subnode_to_map(pctldev, np, map,
  510. &reserved_maps, num_maps);
  511. if (ret < 0) {
  512. pinctrl_utils_free_map(pctldev, *map, *num_maps);
  513. of_node_put(np);
  514. return ret;
  515. }
  516. }
  517. return 0;
  518. }
  519. static int mtk_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  520. {
  521. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  522. return pctl->ngroups;
  523. }
  524. static const char *mtk_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  525. unsigned group)
  526. {
  527. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  528. return pctl->groups[group].name;
  529. }
  530. static int mtk_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  531. unsigned group,
  532. const unsigned **pins,
  533. unsigned *num_pins)
  534. {
  535. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  536. *pins = (unsigned *)&pctl->groups[group].pin;
  537. *num_pins = 1;
  538. return 0;
  539. }
  540. static const struct pinctrl_ops mtk_pctrl_ops = {
  541. .dt_node_to_map = mtk_pctrl_dt_node_to_map,
  542. .dt_free_map = pinctrl_utils_free_map,
  543. .get_groups_count = mtk_pctrl_get_groups_count,
  544. .get_group_name = mtk_pctrl_get_group_name,
  545. .get_group_pins = mtk_pctrl_get_group_pins,
  546. };
  547. static int mtk_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  548. {
  549. return ARRAY_SIZE(mtk_gpio_functions);
  550. }
  551. static const char *mtk_pmx_get_func_name(struct pinctrl_dev *pctldev,
  552. unsigned selector)
  553. {
  554. return mtk_gpio_functions[selector];
  555. }
  556. static int mtk_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  557. unsigned function,
  558. const char * const **groups,
  559. unsigned * const num_groups)
  560. {
  561. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  562. *groups = pctl->grp_names;
  563. *num_groups = pctl->ngroups;
  564. return 0;
  565. }
  566. static int mtk_pmx_set_mode(struct pinctrl_dev *pctldev,
  567. unsigned long pin, unsigned long mode)
  568. {
  569. unsigned int reg_addr;
  570. unsigned char bit;
  571. unsigned int val;
  572. unsigned int mask = (1L << GPIO_MODE_BITS) - 1;
  573. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  574. if (pctl->devdata->spec_pinmux_set)
  575. pctl->devdata->spec_pinmux_set(mtk_get_regmap(pctl, pin),
  576. pin, mode);
  577. reg_addr = ((pin / MAX_GPIO_MODE_PER_REG) << pctl->devdata->port_shf)
  578. + pctl->devdata->pinmux_offset;
  579. mode &= mask;
  580. bit = pin % MAX_GPIO_MODE_PER_REG;
  581. mask <<= (GPIO_MODE_BITS * bit);
  582. val = (mode << (GPIO_MODE_BITS * bit));
  583. return regmap_update_bits(mtk_get_regmap(pctl, pin),
  584. reg_addr, mask, val);
  585. }
  586. static const struct mtk_desc_pin *
  587. mtk_find_pin_by_eint_num(struct mtk_pinctrl *pctl, unsigned int eint_num)
  588. {
  589. int i;
  590. const struct mtk_desc_pin *pin;
  591. for (i = 0; i < pctl->devdata->npins; i++) {
  592. pin = pctl->devdata->pins + i;
  593. if (pin->eint.eintnum == eint_num)
  594. return pin;
  595. }
  596. return NULL;
  597. }
  598. static int mtk_pmx_set_mux(struct pinctrl_dev *pctldev,
  599. unsigned function,
  600. unsigned group)
  601. {
  602. bool ret;
  603. const struct mtk_desc_function *desc;
  604. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  605. struct mtk_pinctrl_group *g = pctl->groups + group;
  606. ret = mtk_pctrl_is_function_valid(pctl, g->pin, function);
  607. if (!ret) {
  608. dev_err(pctl->dev, "invalid function %d on group %d .\n",
  609. function, group);
  610. return -EINVAL;
  611. }
  612. desc = mtk_pctrl_find_function_by_pin(pctl, g->pin, function);
  613. if (!desc)
  614. return -EINVAL;
  615. mtk_pmx_set_mode(pctldev, g->pin, desc->muxval);
  616. return 0;
  617. }
  618. static int mtk_pmx_find_gpio_mode(struct mtk_pinctrl *pctl,
  619. unsigned offset)
  620. {
  621. const struct mtk_desc_pin *pin = pctl->devdata->pins + offset;
  622. const struct mtk_desc_function *func = pin->functions;
  623. while (func && func->name) {
  624. if (!strncmp(func->name, GPIO_MODE_PREFIX,
  625. sizeof(GPIO_MODE_PREFIX)-1))
  626. return func->muxval;
  627. func++;
  628. }
  629. return -EINVAL;
  630. }
  631. static int mtk_pmx_gpio_request_enable(struct pinctrl_dev *pctldev,
  632. struct pinctrl_gpio_range *range,
  633. unsigned offset)
  634. {
  635. int muxval;
  636. struct mtk_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  637. muxval = mtk_pmx_find_gpio_mode(pctl, offset);
  638. if (muxval < 0) {
  639. dev_err(pctl->dev, "invalid gpio pin %d.\n", offset);
  640. return -EINVAL;
  641. }
  642. mtk_pmx_set_mode(pctldev, offset, muxval);
  643. mtk_pconf_set_ies_smt(pctl, offset, 1, PIN_CONFIG_INPUT_ENABLE);
  644. return 0;
  645. }
  646. static const struct pinmux_ops mtk_pmx_ops = {
  647. .get_functions_count = mtk_pmx_get_funcs_cnt,
  648. .get_function_name = mtk_pmx_get_func_name,
  649. .get_function_groups = mtk_pmx_get_func_groups,
  650. .set_mux = mtk_pmx_set_mux,
  651. .gpio_set_direction = mtk_pmx_gpio_set_direction,
  652. .gpio_request_enable = mtk_pmx_gpio_request_enable,
  653. };
  654. static int mtk_gpio_direction_input(struct gpio_chip *chip,
  655. unsigned offset)
  656. {
  657. return pinctrl_gpio_direction_input(chip->base + offset);
  658. }
  659. static int mtk_gpio_direction_output(struct gpio_chip *chip,
  660. unsigned offset, int value)
  661. {
  662. mtk_gpio_set(chip, offset, value);
  663. return pinctrl_gpio_direction_output(chip->base + offset);
  664. }
  665. static int mtk_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  666. {
  667. unsigned int reg_addr;
  668. unsigned int bit;
  669. unsigned int read_val = 0;
  670. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  671. reg_addr = mtk_get_port(pctl, offset) + pctl->devdata->dir_offset;
  672. bit = BIT(offset & 0xf);
  673. if (pctl->devdata->spec_dir_set)
  674. pctl->devdata->spec_dir_set(&reg_addr, offset);
  675. regmap_read(pctl->regmap1, reg_addr, &read_val);
  676. return !(read_val & bit);
  677. }
  678. static int mtk_gpio_get(struct gpio_chip *chip, unsigned offset)
  679. {
  680. unsigned int reg_addr;
  681. unsigned int bit;
  682. unsigned int read_val = 0;
  683. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  684. reg_addr = mtk_get_port(pctl, offset) +
  685. pctl->devdata->din_offset;
  686. bit = BIT(offset & 0xf);
  687. regmap_read(pctl->regmap1, reg_addr, &read_val);
  688. return !!(read_val & bit);
  689. }
  690. static int mtk_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  691. {
  692. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  693. const struct mtk_desc_pin *pin;
  694. unsigned long eint_n;
  695. pin = pctl->devdata->pins + offset;
  696. if (pin->eint.eintnum == NO_EINT_SUPPORT)
  697. return -EINVAL;
  698. eint_n = pin->eint.eintnum;
  699. return mtk_eint_find_irq(pctl->eint, eint_n);
  700. }
  701. static int mtk_gpio_set_config(struct gpio_chip *chip, unsigned offset,
  702. unsigned long config)
  703. {
  704. struct mtk_pinctrl *pctl = gpiochip_get_data(chip);
  705. const struct mtk_desc_pin *pin;
  706. unsigned long eint_n;
  707. u32 debounce;
  708. if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
  709. return -ENOTSUPP;
  710. pin = pctl->devdata->pins + offset;
  711. if (pin->eint.eintnum == NO_EINT_SUPPORT)
  712. return -EINVAL;
  713. debounce = pinconf_to_config_argument(config);
  714. eint_n = pin->eint.eintnum;
  715. return mtk_eint_set_debounce(pctl->eint, eint_n, debounce);
  716. }
  717. static const struct gpio_chip mtk_gpio_chip = {
  718. .owner = THIS_MODULE,
  719. .request = gpiochip_generic_request,
  720. .free = gpiochip_generic_free,
  721. .get_direction = mtk_gpio_get_direction,
  722. .direction_input = mtk_gpio_direction_input,
  723. .direction_output = mtk_gpio_direction_output,
  724. .get = mtk_gpio_get,
  725. .set = mtk_gpio_set,
  726. .to_irq = mtk_gpio_to_irq,
  727. .set_config = mtk_gpio_set_config,
  728. .of_gpio_n_cells = 2,
  729. };
  730. static int mtk_eint_suspend(struct device *device)
  731. {
  732. struct mtk_pinctrl *pctl = dev_get_drvdata(device);
  733. return mtk_eint_do_suspend(pctl->eint);
  734. }
  735. static int mtk_eint_resume(struct device *device)
  736. {
  737. struct mtk_pinctrl *pctl = dev_get_drvdata(device);
  738. return mtk_eint_do_resume(pctl->eint);
  739. }
  740. const struct dev_pm_ops mtk_eint_pm_ops = {
  741. .suspend_noirq = mtk_eint_suspend,
  742. .resume_noirq = mtk_eint_resume,
  743. };
  744. static int mtk_pctrl_build_state(struct platform_device *pdev)
  745. {
  746. struct mtk_pinctrl *pctl = platform_get_drvdata(pdev);
  747. int i;
  748. pctl->ngroups = pctl->devdata->npins;
  749. /* Allocate groups */
  750. pctl->groups = devm_kcalloc(&pdev->dev, pctl->ngroups,
  751. sizeof(*pctl->groups), GFP_KERNEL);
  752. if (!pctl->groups)
  753. return -ENOMEM;
  754. /* We assume that one pin is one group, use pin name as group name. */
  755. pctl->grp_names = devm_kcalloc(&pdev->dev, pctl->ngroups,
  756. sizeof(*pctl->grp_names), GFP_KERNEL);
  757. if (!pctl->grp_names)
  758. return -ENOMEM;
  759. for (i = 0; i < pctl->devdata->npins; i++) {
  760. const struct mtk_desc_pin *pin = pctl->devdata->pins + i;
  761. struct mtk_pinctrl_group *group = pctl->groups + i;
  762. group->name = pin->pin.name;
  763. group->pin = pin->pin.number;
  764. pctl->grp_names[i] = pin->pin.name;
  765. }
  766. return 0;
  767. }
  768. static int
  769. mtk_xt_get_gpio_n(void *data, unsigned long eint_n, unsigned int *gpio_n,
  770. struct gpio_chip **gpio_chip)
  771. {
  772. struct mtk_pinctrl *pctl = (struct mtk_pinctrl *)data;
  773. const struct mtk_desc_pin *pin;
  774. pin = mtk_find_pin_by_eint_num(pctl, eint_n);
  775. if (!pin)
  776. return -EINVAL;
  777. *gpio_chip = pctl->chip;
  778. *gpio_n = pin->pin.number;
  779. return 0;
  780. }
  781. static int mtk_xt_get_gpio_state(void *data, unsigned long eint_n)
  782. {
  783. struct mtk_pinctrl *pctl = (struct mtk_pinctrl *)data;
  784. const struct mtk_desc_pin *pin;
  785. pin = mtk_find_pin_by_eint_num(pctl, eint_n);
  786. if (!pin)
  787. return -EINVAL;
  788. return mtk_gpio_get(pctl->chip, pin->pin.number);
  789. }
  790. static int mtk_xt_set_gpio_as_eint(void *data, unsigned long eint_n)
  791. {
  792. struct mtk_pinctrl *pctl = (struct mtk_pinctrl *)data;
  793. const struct mtk_desc_pin *pin;
  794. pin = mtk_find_pin_by_eint_num(pctl, eint_n);
  795. if (!pin)
  796. return -EINVAL;
  797. /* set mux to INT mode */
  798. mtk_pmx_set_mode(pctl->pctl_dev, pin->pin.number, pin->eint.eintmux);
  799. /* set gpio direction to input */
  800. mtk_pmx_gpio_set_direction(pctl->pctl_dev, NULL, pin->pin.number,
  801. true);
  802. /* set input-enable */
  803. mtk_pconf_set_ies_smt(pctl, pin->pin.number, 1,
  804. PIN_CONFIG_INPUT_ENABLE);
  805. return 0;
  806. }
  807. static const struct mtk_eint_xt mtk_eint_xt = {
  808. .get_gpio_n = mtk_xt_get_gpio_n,
  809. .get_gpio_state = mtk_xt_get_gpio_state,
  810. .set_gpio_as_eint = mtk_xt_set_gpio_as_eint,
  811. };
  812. static int mtk_eint_init(struct mtk_pinctrl *pctl, struct platform_device *pdev)
  813. {
  814. struct device_node *np = pdev->dev.of_node;
  815. struct resource *res;
  816. if (!of_property_read_bool(np, "interrupt-controller"))
  817. return -ENODEV;
  818. pctl->eint = devm_kzalloc(pctl->dev, sizeof(*pctl->eint), GFP_KERNEL);
  819. if (!pctl->eint)
  820. return -ENOMEM;
  821. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  822. pctl->eint->base = devm_ioremap_resource(&pdev->dev, res);
  823. if (IS_ERR(pctl->eint->base))
  824. return PTR_ERR(pctl->eint->base);
  825. pctl->eint->irq = irq_of_parse_and_map(np, 0);
  826. if (!pctl->eint->irq)
  827. return -EINVAL;
  828. pctl->eint->dev = &pdev->dev;
  829. /*
  830. * If pctl->eint->regs == NULL, it would fall back into using a generic
  831. * register map in mtk_eint_do_init calls.
  832. */
  833. pctl->eint->regs = pctl->devdata->eint_regs;
  834. pctl->eint->hw = &pctl->devdata->eint_hw;
  835. pctl->eint->pctl = pctl;
  836. pctl->eint->gpio_xlate = &mtk_eint_xt;
  837. return mtk_eint_do_init(pctl->eint);
  838. }
  839. int mtk_pctrl_init(struct platform_device *pdev,
  840. const struct mtk_pinctrl_devdata *data,
  841. struct regmap *regmap)
  842. {
  843. struct pinctrl_pin_desc *pins;
  844. struct mtk_pinctrl *pctl;
  845. struct device_node *np = pdev->dev.of_node, *node;
  846. struct property *prop;
  847. int ret, i;
  848. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  849. if (!pctl)
  850. return -ENOMEM;
  851. platform_set_drvdata(pdev, pctl);
  852. prop = of_find_property(np, "pins-are-numbered", NULL);
  853. if (!prop) {
  854. dev_err(&pdev->dev, "only support pins-are-numbered format\n");
  855. return -EINVAL;
  856. }
  857. node = of_parse_phandle(np, "mediatek,pctl-regmap", 0);
  858. if (node) {
  859. pctl->regmap1 = syscon_node_to_regmap(node);
  860. if (IS_ERR(pctl->regmap1))
  861. return PTR_ERR(pctl->regmap1);
  862. } else if (regmap) {
  863. pctl->regmap1 = regmap;
  864. } else {
  865. dev_err(&pdev->dev, "Pinctrl node has not register regmap.\n");
  866. return -EINVAL;
  867. }
  868. /* Only 8135 has two base addr, other SoCs have only one. */
  869. node = of_parse_phandle(np, "mediatek,pctl-regmap", 1);
  870. if (node) {
  871. pctl->regmap2 = syscon_node_to_regmap(node);
  872. if (IS_ERR(pctl->regmap2))
  873. return PTR_ERR(pctl->regmap2);
  874. }
  875. pctl->devdata = data;
  876. ret = mtk_pctrl_build_state(pdev);
  877. if (ret) {
  878. dev_err(&pdev->dev, "build state failed: %d\n", ret);
  879. return -EINVAL;
  880. }
  881. pins = devm_kcalloc(&pdev->dev, pctl->devdata->npins, sizeof(*pins),
  882. GFP_KERNEL);
  883. if (!pins)
  884. return -ENOMEM;
  885. for (i = 0; i < pctl->devdata->npins; i++)
  886. pins[i] = pctl->devdata->pins[i].pin;
  887. pctl->pctl_desc.name = dev_name(&pdev->dev);
  888. pctl->pctl_desc.owner = THIS_MODULE;
  889. pctl->pctl_desc.pins = pins;
  890. pctl->pctl_desc.npins = pctl->devdata->npins;
  891. pctl->pctl_desc.confops = &mtk_pconf_ops;
  892. pctl->pctl_desc.pctlops = &mtk_pctrl_ops;
  893. pctl->pctl_desc.pmxops = &mtk_pmx_ops;
  894. pctl->dev = &pdev->dev;
  895. pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, &pctl->pctl_desc,
  896. pctl);
  897. if (IS_ERR(pctl->pctl_dev)) {
  898. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  899. return PTR_ERR(pctl->pctl_dev);
  900. }
  901. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  902. if (!pctl->chip)
  903. return -ENOMEM;
  904. *pctl->chip = mtk_gpio_chip;
  905. pctl->chip->ngpio = pctl->devdata->npins;
  906. pctl->chip->label = dev_name(&pdev->dev);
  907. pctl->chip->parent = &pdev->dev;
  908. pctl->chip->base = -1;
  909. ret = gpiochip_add_data(pctl->chip, pctl);
  910. if (ret)
  911. return -EINVAL;
  912. /* Register the GPIO to pin mappings. */
  913. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  914. 0, 0, pctl->devdata->npins);
  915. if (ret) {
  916. ret = -EINVAL;
  917. goto chip_error;
  918. }
  919. ret = mtk_eint_init(pctl, pdev);
  920. if (ret)
  921. goto chip_error;
  922. return 0;
  923. chip_error:
  924. gpiochip_remove(pctl->chip);
  925. return ret;
  926. }