pinctrl-sunxi.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * Allwinner A1X SoCs pinctrl driver.
  3. *
  4. * Copyright (C) 2012 Maxime Ripard
  5. *
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/io.h>
  13. #include <linux/clk.h>
  14. #include <linux/gpio/driver.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/irqchip/chained_irq.h>
  17. #include <linux/export.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/pinctrl.h>
  25. #include <linux/pinctrl/pinconf-generic.h>
  26. #include <linux/pinctrl/pinmux.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <dt-bindings/pinctrl/sun4i-a10.h>
  30. #include "../core.h"
  31. #include "pinctrl-sunxi.h"
  32. static struct irq_chip sunxi_pinctrl_edge_irq_chip;
  33. static struct irq_chip sunxi_pinctrl_level_irq_chip;
  34. static struct sunxi_pinctrl_group *
  35. sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
  36. {
  37. int i;
  38. for (i = 0; i < pctl->ngroups; i++) {
  39. struct sunxi_pinctrl_group *grp = pctl->groups + i;
  40. if (!strcmp(grp->name, group))
  41. return grp;
  42. }
  43. return NULL;
  44. }
  45. static struct sunxi_pinctrl_function *
  46. sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
  47. const char *name)
  48. {
  49. struct sunxi_pinctrl_function *func = pctl->functions;
  50. int i;
  51. for (i = 0; i < pctl->nfunctions; i++) {
  52. if (!func[i].name)
  53. break;
  54. if (!strcmp(func[i].name, name))
  55. return func + i;
  56. }
  57. return NULL;
  58. }
  59. static struct sunxi_desc_function *
  60. sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
  61. const char *pin_name,
  62. const char *func_name)
  63. {
  64. int i;
  65. for (i = 0; i < pctl->desc->npins; i++) {
  66. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  67. if (!strcmp(pin->pin.name, pin_name)) {
  68. struct sunxi_desc_function *func = pin->functions;
  69. while (func->name) {
  70. if (!strcmp(func->name, func_name))
  71. return func;
  72. func++;
  73. }
  74. }
  75. }
  76. return NULL;
  77. }
  78. static struct sunxi_desc_function *
  79. sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
  80. const u16 pin_num,
  81. const char *func_name)
  82. {
  83. int i;
  84. for (i = 0; i < pctl->desc->npins; i++) {
  85. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  86. if (pin->pin.number == pin_num) {
  87. struct sunxi_desc_function *func = pin->functions;
  88. while (func->name) {
  89. if (!strcmp(func->name, func_name))
  90. return func;
  91. func++;
  92. }
  93. }
  94. }
  95. return NULL;
  96. }
  97. static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
  98. {
  99. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  100. return pctl->ngroups;
  101. }
  102. static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
  103. unsigned group)
  104. {
  105. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  106. return pctl->groups[group].name;
  107. }
  108. static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
  109. unsigned group,
  110. const unsigned **pins,
  111. unsigned *num_pins)
  112. {
  113. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  114. *pins = (unsigned *)&pctl->groups[group].pin;
  115. *num_pins = 1;
  116. return 0;
  117. }
  118. static bool sunxi_pctrl_has_bias_prop(struct device_node *node)
  119. {
  120. return of_find_property(node, "bias-pull-up", NULL) ||
  121. of_find_property(node, "bias-pull-down", NULL) ||
  122. of_find_property(node, "bias-disable", NULL) ||
  123. of_find_property(node, "allwinner,pull", NULL);
  124. }
  125. static bool sunxi_pctrl_has_drive_prop(struct device_node *node)
  126. {
  127. return of_find_property(node, "drive-strength", NULL) ||
  128. of_find_property(node, "allwinner,drive", NULL);
  129. }
  130. static int sunxi_pctrl_parse_bias_prop(struct device_node *node)
  131. {
  132. u32 val;
  133. /* Try the new style binding */
  134. if (of_find_property(node, "bias-pull-up", NULL))
  135. return PIN_CONFIG_BIAS_PULL_UP;
  136. if (of_find_property(node, "bias-pull-down", NULL))
  137. return PIN_CONFIG_BIAS_PULL_DOWN;
  138. if (of_find_property(node, "bias-disable", NULL))
  139. return PIN_CONFIG_BIAS_DISABLE;
  140. /* And fall back to the old binding */
  141. if (of_property_read_u32(node, "allwinner,pull", &val))
  142. return -EINVAL;
  143. switch (val) {
  144. case SUN4I_PINCTRL_NO_PULL:
  145. return PIN_CONFIG_BIAS_DISABLE;
  146. case SUN4I_PINCTRL_PULL_UP:
  147. return PIN_CONFIG_BIAS_PULL_UP;
  148. case SUN4I_PINCTRL_PULL_DOWN:
  149. return PIN_CONFIG_BIAS_PULL_DOWN;
  150. }
  151. return -EINVAL;
  152. }
  153. static int sunxi_pctrl_parse_drive_prop(struct device_node *node)
  154. {
  155. u32 val;
  156. /* Try the new style binding */
  157. if (!of_property_read_u32(node, "drive-strength", &val)) {
  158. /* We can't go below 10mA ... */
  159. if (val < 10)
  160. return -EINVAL;
  161. /* ... and only up to 40 mA ... */
  162. if (val > 40)
  163. val = 40;
  164. /* by steps of 10 mA */
  165. return rounddown(val, 10);
  166. }
  167. /* And then fall back to the old binding */
  168. if (of_property_read_u32(node, "allwinner,drive", &val))
  169. return -EINVAL;
  170. return (val + 1) * 10;
  171. }
  172. static const char *sunxi_pctrl_parse_function_prop(struct device_node *node)
  173. {
  174. const char *function;
  175. int ret;
  176. /* Try the generic binding */
  177. ret = of_property_read_string(node, "function", &function);
  178. if (!ret)
  179. return function;
  180. /* And fall back to our legacy one */
  181. ret = of_property_read_string(node, "allwinner,function", &function);
  182. if (!ret)
  183. return function;
  184. return NULL;
  185. }
  186. static const char *sunxi_pctrl_find_pins_prop(struct device_node *node,
  187. int *npins)
  188. {
  189. int count;
  190. /* Try the generic binding */
  191. count = of_property_count_strings(node, "pins");
  192. if (count > 0) {
  193. *npins = count;
  194. return "pins";
  195. }
  196. /* And fall back to our legacy one */
  197. count = of_property_count_strings(node, "allwinner,pins");
  198. if (count > 0) {
  199. *npins = count;
  200. return "allwinner,pins";
  201. }
  202. return NULL;
  203. }
  204. static unsigned long *sunxi_pctrl_build_pin_config(struct device_node *node,
  205. unsigned int *len)
  206. {
  207. unsigned long *pinconfig;
  208. unsigned int configlen = 0, idx = 0;
  209. int ret;
  210. if (sunxi_pctrl_has_drive_prop(node))
  211. configlen++;
  212. if (sunxi_pctrl_has_bias_prop(node))
  213. configlen++;
  214. /*
  215. * If we don't have any configuration, bail out
  216. */
  217. if (!configlen)
  218. return NULL;
  219. pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
  220. if (!pinconfig)
  221. return ERR_PTR(-ENOMEM);
  222. if (sunxi_pctrl_has_drive_prop(node)) {
  223. int drive = sunxi_pctrl_parse_drive_prop(node);
  224. if (drive < 0) {
  225. ret = drive;
  226. goto err_free;
  227. }
  228. pinconfig[idx++] = pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
  229. drive);
  230. }
  231. if (sunxi_pctrl_has_bias_prop(node)) {
  232. int pull = sunxi_pctrl_parse_bias_prop(node);
  233. int arg = 0;
  234. if (pull < 0) {
  235. ret = pull;
  236. goto err_free;
  237. }
  238. if (pull != PIN_CONFIG_BIAS_DISABLE)
  239. arg = 1; /* hardware uses weak pull resistors */
  240. pinconfig[idx++] = pinconf_to_config_packed(pull, arg);
  241. }
  242. *len = configlen;
  243. return pinconfig;
  244. err_free:
  245. kfree(pinconfig);
  246. return ERR_PTR(ret);
  247. }
  248. static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
  249. struct device_node *node,
  250. struct pinctrl_map **map,
  251. unsigned *num_maps)
  252. {
  253. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  254. unsigned long *pinconfig;
  255. struct property *prop;
  256. const char *function, *pin_prop;
  257. const char *group;
  258. int ret, npins, nmaps, configlen = 0, i = 0;
  259. *map = NULL;
  260. *num_maps = 0;
  261. function = sunxi_pctrl_parse_function_prop(node);
  262. if (!function) {
  263. dev_err(pctl->dev, "missing function property in node %s\n",
  264. node->name);
  265. return -EINVAL;
  266. }
  267. pin_prop = sunxi_pctrl_find_pins_prop(node, &npins);
  268. if (!pin_prop) {
  269. dev_err(pctl->dev, "missing pins property in node %s\n",
  270. node->name);
  271. return -EINVAL;
  272. }
  273. /*
  274. * We have two maps for each pin: one for the function, one
  275. * for the configuration (bias, strength, etc).
  276. *
  277. * We might be slightly overshooting, since we might not have
  278. * any configuration.
  279. */
  280. nmaps = npins * 2;
  281. *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
  282. if (!*map)
  283. return -ENOMEM;
  284. pinconfig = sunxi_pctrl_build_pin_config(node, &configlen);
  285. if (IS_ERR(pinconfig)) {
  286. ret = PTR_ERR(pinconfig);
  287. goto err_free_map;
  288. }
  289. of_property_for_each_string(node, pin_prop, prop, group) {
  290. struct sunxi_pinctrl_group *grp =
  291. sunxi_pinctrl_find_group_by_name(pctl, group);
  292. if (!grp) {
  293. dev_err(pctl->dev, "unknown pin %s", group);
  294. continue;
  295. }
  296. if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
  297. grp->name,
  298. function)) {
  299. dev_err(pctl->dev, "unsupported function %s on pin %s",
  300. function, group);
  301. continue;
  302. }
  303. (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
  304. (*map)[i].data.mux.group = group;
  305. (*map)[i].data.mux.function = function;
  306. i++;
  307. if (pinconfig) {
  308. (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  309. (*map)[i].data.configs.group_or_pin = group;
  310. (*map)[i].data.configs.configs = pinconfig;
  311. (*map)[i].data.configs.num_configs = configlen;
  312. i++;
  313. }
  314. }
  315. *num_maps = i;
  316. /*
  317. * We know have the number of maps we need, we can resize our
  318. * map array
  319. */
  320. *map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL);
  321. if (!*map)
  322. return -ENOMEM;
  323. return 0;
  324. err_free_map:
  325. kfree(*map);
  326. *map = NULL;
  327. return ret;
  328. }
  329. static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
  330. struct pinctrl_map *map,
  331. unsigned num_maps)
  332. {
  333. int i;
  334. /* pin config is never in the first map */
  335. for (i = 1; i < num_maps; i++) {
  336. if (map[i].type != PIN_MAP_TYPE_CONFIGS_GROUP)
  337. continue;
  338. /*
  339. * All the maps share the same pin config,
  340. * free only the first one we find.
  341. */
  342. kfree(map[i].data.configs.configs);
  343. break;
  344. }
  345. kfree(map);
  346. }
  347. static const struct pinctrl_ops sunxi_pctrl_ops = {
  348. .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
  349. .dt_free_map = sunxi_pctrl_dt_free_map,
  350. .get_groups_count = sunxi_pctrl_get_groups_count,
  351. .get_group_name = sunxi_pctrl_get_group_name,
  352. .get_group_pins = sunxi_pctrl_get_group_pins,
  353. };
  354. static int sunxi_pconf_reg(unsigned pin, enum pin_config_param param,
  355. u32 *offset, u32 *shift, u32 *mask)
  356. {
  357. switch (param) {
  358. case PIN_CONFIG_DRIVE_STRENGTH:
  359. *offset = sunxi_dlevel_reg(pin);
  360. *shift = sunxi_dlevel_offset(pin);
  361. *mask = DLEVEL_PINS_MASK;
  362. break;
  363. case PIN_CONFIG_BIAS_PULL_UP:
  364. case PIN_CONFIG_BIAS_PULL_DOWN:
  365. case PIN_CONFIG_BIAS_DISABLE:
  366. *offset = sunxi_pull_reg(pin);
  367. *shift = sunxi_pull_offset(pin);
  368. *mask = PULL_PINS_MASK;
  369. break;
  370. default:
  371. return -ENOTSUPP;
  372. }
  373. return 0;
  374. }
  375. static int sunxi_pconf_get(struct pinctrl_dev *pctldev, unsigned pin,
  376. unsigned long *config)
  377. {
  378. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  379. enum pin_config_param param = pinconf_to_config_param(*config);
  380. u32 offset, shift, mask, val;
  381. u16 arg;
  382. int ret;
  383. pin -= pctl->desc->pin_base;
  384. ret = sunxi_pconf_reg(pin, param, &offset, &shift, &mask);
  385. if (ret < 0)
  386. return ret;
  387. val = (readl(pctl->membase + offset) >> shift) & mask;
  388. switch (pinconf_to_config_param(*config)) {
  389. case PIN_CONFIG_DRIVE_STRENGTH:
  390. arg = (val + 1) * 10;
  391. break;
  392. case PIN_CONFIG_BIAS_PULL_UP:
  393. if (val != SUN4I_PINCTRL_PULL_UP)
  394. return -EINVAL;
  395. arg = 1; /* hardware is weak pull-up */
  396. break;
  397. case PIN_CONFIG_BIAS_PULL_DOWN:
  398. if (val != SUN4I_PINCTRL_PULL_DOWN)
  399. return -EINVAL;
  400. arg = 1; /* hardware is weak pull-down */
  401. break;
  402. case PIN_CONFIG_BIAS_DISABLE:
  403. if (val != SUN4I_PINCTRL_NO_PULL)
  404. return -EINVAL;
  405. arg = 0;
  406. break;
  407. default:
  408. /* sunxi_pconf_reg should catch anything unsupported */
  409. WARN_ON(1);
  410. return -ENOTSUPP;
  411. }
  412. *config = pinconf_to_config_packed(param, arg);
  413. return 0;
  414. }
  415. static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
  416. unsigned group,
  417. unsigned long *config)
  418. {
  419. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  420. struct sunxi_pinctrl_group *g = &pctl->groups[group];
  421. /* We only support 1 pin per group. Chain it to the pin callback */
  422. return sunxi_pconf_get(pctldev, g->pin, config);
  423. }
  424. static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
  425. unsigned group,
  426. unsigned long *configs,
  427. unsigned num_configs)
  428. {
  429. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  430. struct sunxi_pinctrl_group *g = &pctl->groups[group];
  431. unsigned pin = g->pin - pctl->desc->pin_base;
  432. int i;
  433. for (i = 0; i < num_configs; i++) {
  434. enum pin_config_param param;
  435. unsigned long flags;
  436. u32 offset, shift, mask, reg;
  437. u32 arg, val;
  438. int ret;
  439. param = pinconf_to_config_param(configs[i]);
  440. arg = pinconf_to_config_argument(configs[i]);
  441. ret = sunxi_pconf_reg(pin, param, &offset, &shift, &mask);
  442. if (ret < 0)
  443. return ret;
  444. switch (param) {
  445. case PIN_CONFIG_DRIVE_STRENGTH:
  446. if (arg < 10 || arg > 40)
  447. return -EINVAL;
  448. /*
  449. * We convert from mA to what the register expects:
  450. * 0: 10mA
  451. * 1: 20mA
  452. * 2: 30mA
  453. * 3: 40mA
  454. */
  455. val = arg / 10 - 1;
  456. break;
  457. case PIN_CONFIG_BIAS_DISABLE:
  458. continue;
  459. case PIN_CONFIG_BIAS_PULL_UP:
  460. if (arg == 0)
  461. return -EINVAL;
  462. val = 1;
  463. break;
  464. case PIN_CONFIG_BIAS_PULL_DOWN:
  465. if (arg == 0)
  466. return -EINVAL;
  467. val = 2;
  468. break;
  469. default:
  470. /* sunxi_pconf_reg should catch anything unsupported */
  471. WARN_ON(1);
  472. return -ENOTSUPP;
  473. }
  474. raw_spin_lock_irqsave(&pctl->lock, flags);
  475. reg = readl(pctl->membase + offset);
  476. reg &= ~(mask << shift);
  477. writel(reg | val << shift, pctl->membase + offset);
  478. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  479. } /* for each config */
  480. return 0;
  481. }
  482. static const struct pinconf_ops sunxi_pconf_ops = {
  483. .is_generic = true,
  484. .pin_config_get = sunxi_pconf_get,
  485. .pin_config_group_get = sunxi_pconf_group_get,
  486. .pin_config_group_set = sunxi_pconf_group_set,
  487. };
  488. static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  489. {
  490. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  491. return pctl->nfunctions;
  492. }
  493. static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
  494. unsigned function)
  495. {
  496. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  497. return pctl->functions[function].name;
  498. }
  499. static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  500. unsigned function,
  501. const char * const **groups,
  502. unsigned * const num_groups)
  503. {
  504. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  505. *groups = pctl->functions[function].groups;
  506. *num_groups = pctl->functions[function].ngroups;
  507. return 0;
  508. }
  509. static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
  510. unsigned pin,
  511. u8 config)
  512. {
  513. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  514. unsigned long flags;
  515. u32 val, mask;
  516. raw_spin_lock_irqsave(&pctl->lock, flags);
  517. pin -= pctl->desc->pin_base;
  518. val = readl(pctl->membase + sunxi_mux_reg(pin));
  519. mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
  520. writel((val & ~mask) | config << sunxi_mux_offset(pin),
  521. pctl->membase + sunxi_mux_reg(pin));
  522. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  523. }
  524. static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev,
  525. unsigned function,
  526. unsigned group)
  527. {
  528. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  529. struct sunxi_pinctrl_group *g = pctl->groups + group;
  530. struct sunxi_pinctrl_function *func = pctl->functions + function;
  531. struct sunxi_desc_function *desc =
  532. sunxi_pinctrl_desc_find_function_by_name(pctl,
  533. g->name,
  534. func->name);
  535. if (!desc)
  536. return -EINVAL;
  537. sunxi_pmx_set(pctldev, g->pin, desc->muxval);
  538. return 0;
  539. }
  540. static int
  541. sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  542. struct pinctrl_gpio_range *range,
  543. unsigned offset,
  544. bool input)
  545. {
  546. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  547. struct sunxi_desc_function *desc;
  548. const char *func;
  549. if (input)
  550. func = "gpio_in";
  551. else
  552. func = "gpio_out";
  553. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
  554. if (!desc)
  555. return -EINVAL;
  556. sunxi_pmx_set(pctldev, offset, desc->muxval);
  557. return 0;
  558. }
  559. static const struct pinmux_ops sunxi_pmx_ops = {
  560. .get_functions_count = sunxi_pmx_get_funcs_cnt,
  561. .get_function_name = sunxi_pmx_get_func_name,
  562. .get_function_groups = sunxi_pmx_get_func_groups,
  563. .set_mux = sunxi_pmx_set_mux,
  564. .gpio_set_direction = sunxi_pmx_gpio_set_direction,
  565. };
  566. static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
  567. unsigned offset)
  568. {
  569. return pinctrl_gpio_direction_input(chip->base + offset);
  570. }
  571. static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
  572. {
  573. struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
  574. u32 reg = sunxi_data_reg(offset);
  575. u8 index = sunxi_data_offset(offset);
  576. bool set_mux = pctl->desc->irq_read_needs_mux &&
  577. gpiochip_line_is_irq(chip, offset);
  578. u32 pin = offset + chip->base;
  579. u32 val;
  580. if (set_mux)
  581. sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_INPUT);
  582. val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
  583. if (set_mux)
  584. sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_IRQ);
  585. return !!val;
  586. }
  587. static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
  588. unsigned offset, int value)
  589. {
  590. struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
  591. u32 reg = sunxi_data_reg(offset);
  592. u8 index = sunxi_data_offset(offset);
  593. unsigned long flags;
  594. u32 regval;
  595. raw_spin_lock_irqsave(&pctl->lock, flags);
  596. regval = readl(pctl->membase + reg);
  597. if (value)
  598. regval |= BIT(index);
  599. else
  600. regval &= ~(BIT(index));
  601. writel(regval, pctl->membase + reg);
  602. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  603. }
  604. static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
  605. unsigned offset, int value)
  606. {
  607. sunxi_pinctrl_gpio_set(chip, offset, value);
  608. return pinctrl_gpio_direction_output(chip->base + offset);
  609. }
  610. static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
  611. const struct of_phandle_args *gpiospec,
  612. u32 *flags)
  613. {
  614. int pin, base;
  615. base = PINS_PER_BANK * gpiospec->args[0];
  616. pin = base + gpiospec->args[1];
  617. if (pin > gc->ngpio)
  618. return -EINVAL;
  619. if (flags)
  620. *flags = gpiospec->args[2];
  621. return pin;
  622. }
  623. static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  624. {
  625. struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
  626. struct sunxi_desc_function *desc;
  627. unsigned pinnum = pctl->desc->pin_base + offset;
  628. unsigned irqnum;
  629. if (offset >= chip->ngpio)
  630. return -ENXIO;
  631. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
  632. if (!desc)
  633. return -EINVAL;
  634. irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
  635. dev_dbg(chip->parent, "%s: request IRQ for GPIO %d, return %d\n",
  636. chip->label, offset + chip->base, irqnum);
  637. return irq_find_mapping(pctl->domain, irqnum);
  638. }
  639. static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
  640. {
  641. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  642. struct sunxi_desc_function *func;
  643. int ret;
  644. func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
  645. pctl->irq_array[d->hwirq], "irq");
  646. if (!func)
  647. return -EINVAL;
  648. ret = gpiochip_lock_as_irq(pctl->chip,
  649. pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
  650. if (ret) {
  651. dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
  652. irqd_to_hwirq(d));
  653. return ret;
  654. }
  655. /* Change muxing to INT mode */
  656. sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
  657. return 0;
  658. }
  659. static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
  660. {
  661. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  662. gpiochip_unlock_as_irq(pctl->chip,
  663. pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
  664. }
  665. static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
  666. {
  667. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  668. u32 reg = sunxi_irq_cfg_reg(d->hwirq, pctl->desc->irq_bank_base);
  669. u8 index = sunxi_irq_cfg_offset(d->hwirq);
  670. unsigned long flags;
  671. u32 regval;
  672. u8 mode;
  673. switch (type) {
  674. case IRQ_TYPE_EDGE_RISING:
  675. mode = IRQ_EDGE_RISING;
  676. break;
  677. case IRQ_TYPE_EDGE_FALLING:
  678. mode = IRQ_EDGE_FALLING;
  679. break;
  680. case IRQ_TYPE_EDGE_BOTH:
  681. mode = IRQ_EDGE_BOTH;
  682. break;
  683. case IRQ_TYPE_LEVEL_HIGH:
  684. mode = IRQ_LEVEL_HIGH;
  685. break;
  686. case IRQ_TYPE_LEVEL_LOW:
  687. mode = IRQ_LEVEL_LOW;
  688. break;
  689. default:
  690. return -EINVAL;
  691. }
  692. raw_spin_lock_irqsave(&pctl->lock, flags);
  693. if (type & IRQ_TYPE_LEVEL_MASK)
  694. irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
  695. handle_fasteoi_irq, NULL);
  696. else
  697. irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
  698. handle_edge_irq, NULL);
  699. regval = readl(pctl->membase + reg);
  700. regval &= ~(IRQ_CFG_IRQ_MASK << index);
  701. writel(regval | (mode << index), pctl->membase + reg);
  702. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  703. return 0;
  704. }
  705. static void sunxi_pinctrl_irq_ack(struct irq_data *d)
  706. {
  707. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  708. u32 status_reg = sunxi_irq_status_reg(d->hwirq,
  709. pctl->desc->irq_bank_base);
  710. u8 status_idx = sunxi_irq_status_offset(d->hwirq);
  711. /* Clear the IRQ */
  712. writel(1 << status_idx, pctl->membase + status_reg);
  713. }
  714. static void sunxi_pinctrl_irq_mask(struct irq_data *d)
  715. {
  716. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  717. u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
  718. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  719. unsigned long flags;
  720. u32 val;
  721. raw_spin_lock_irqsave(&pctl->lock, flags);
  722. /* Mask the IRQ */
  723. val = readl(pctl->membase + reg);
  724. writel(val & ~(1 << idx), pctl->membase + reg);
  725. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  726. }
  727. static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
  728. {
  729. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  730. u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
  731. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  732. unsigned long flags;
  733. u32 val;
  734. raw_spin_lock_irqsave(&pctl->lock, flags);
  735. /* Unmask the IRQ */
  736. val = readl(pctl->membase + reg);
  737. writel(val | (1 << idx), pctl->membase + reg);
  738. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  739. }
  740. static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
  741. {
  742. sunxi_pinctrl_irq_ack(d);
  743. sunxi_pinctrl_irq_unmask(d);
  744. }
  745. static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
  746. .name = "sunxi_pio_edge",
  747. .irq_ack = sunxi_pinctrl_irq_ack,
  748. .irq_mask = sunxi_pinctrl_irq_mask,
  749. .irq_unmask = sunxi_pinctrl_irq_unmask,
  750. .irq_request_resources = sunxi_pinctrl_irq_request_resources,
  751. .irq_release_resources = sunxi_pinctrl_irq_release_resources,
  752. .irq_set_type = sunxi_pinctrl_irq_set_type,
  753. .flags = IRQCHIP_SKIP_SET_WAKE,
  754. };
  755. static struct irq_chip sunxi_pinctrl_level_irq_chip = {
  756. .name = "sunxi_pio_level",
  757. .irq_eoi = sunxi_pinctrl_irq_ack,
  758. .irq_mask = sunxi_pinctrl_irq_mask,
  759. .irq_unmask = sunxi_pinctrl_irq_unmask,
  760. /* Define irq_enable / disable to avoid spurious irqs for drivers
  761. * using these to suppress irqs while they clear the irq source */
  762. .irq_enable = sunxi_pinctrl_irq_ack_unmask,
  763. .irq_disable = sunxi_pinctrl_irq_mask,
  764. .irq_request_resources = sunxi_pinctrl_irq_request_resources,
  765. .irq_release_resources = sunxi_pinctrl_irq_release_resources,
  766. .irq_set_type = sunxi_pinctrl_irq_set_type,
  767. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_EOI_THREADED |
  768. IRQCHIP_EOI_IF_HANDLED,
  769. };
  770. static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
  771. struct device_node *node,
  772. const u32 *intspec,
  773. unsigned int intsize,
  774. unsigned long *out_hwirq,
  775. unsigned int *out_type)
  776. {
  777. struct sunxi_pinctrl *pctl = d->host_data;
  778. struct sunxi_desc_function *desc;
  779. int pin, base;
  780. if (intsize < 3)
  781. return -EINVAL;
  782. base = PINS_PER_BANK * intspec[0];
  783. pin = pctl->desc->pin_base + base + intspec[1];
  784. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
  785. if (!desc)
  786. return -EINVAL;
  787. *out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
  788. *out_type = intspec[2];
  789. return 0;
  790. }
  791. static struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
  792. .xlate = sunxi_pinctrl_irq_of_xlate,
  793. };
  794. static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
  795. {
  796. unsigned int irq = irq_desc_get_irq(desc);
  797. struct irq_chip *chip = irq_desc_get_chip(desc);
  798. struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
  799. unsigned long bank, reg, val;
  800. for (bank = 0; bank < pctl->desc->irq_banks; bank++)
  801. if (irq == pctl->irq[bank])
  802. break;
  803. if (bank == pctl->desc->irq_banks)
  804. return;
  805. reg = sunxi_irq_status_reg_from_bank(bank, pctl->desc->irq_bank_base);
  806. val = readl(pctl->membase + reg);
  807. if (val) {
  808. int irqoffset;
  809. chained_irq_enter(chip, desc);
  810. for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
  811. int pin_irq = irq_find_mapping(pctl->domain,
  812. bank * IRQ_PER_BANK + irqoffset);
  813. generic_handle_irq(pin_irq);
  814. }
  815. chained_irq_exit(chip, desc);
  816. }
  817. }
  818. static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
  819. const char *name)
  820. {
  821. struct sunxi_pinctrl_function *func = pctl->functions;
  822. while (func->name) {
  823. /* function already there */
  824. if (strcmp(func->name, name) == 0) {
  825. func->ngroups++;
  826. return -EEXIST;
  827. }
  828. func++;
  829. }
  830. func->name = name;
  831. func->ngroups = 1;
  832. pctl->nfunctions++;
  833. return 0;
  834. }
  835. static int sunxi_pinctrl_build_state(struct platform_device *pdev)
  836. {
  837. struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
  838. int i;
  839. /*
  840. * Allocate groups
  841. *
  842. * We assume that the number of groups is the number of pins
  843. * given in the data array.
  844. * This will not always be true, since some pins might not be
  845. * available in the current variant, but fortunately for us,
  846. * this means that the number of pins is the maximum group
  847. * number we will ever see.
  848. */
  849. pctl->groups = devm_kzalloc(&pdev->dev,
  850. pctl->desc->npins * sizeof(*pctl->groups),
  851. GFP_KERNEL);
  852. if (!pctl->groups)
  853. return -ENOMEM;
  854. for (i = 0; i < pctl->desc->npins; i++) {
  855. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  856. struct sunxi_pinctrl_group *group = pctl->groups + pctl->ngroups;
  857. if (pin->variant && !(pctl->variant & pin->variant))
  858. continue;
  859. group->name = pin->pin.name;
  860. group->pin = pin->pin.number;
  861. /* And now we count the actual number of pins / groups */
  862. pctl->ngroups++;
  863. }
  864. /*
  865. * We suppose that we won't have any more functions than pins,
  866. * we'll reallocate that later anyway
  867. */
  868. pctl->functions = devm_kzalloc(&pdev->dev,
  869. pctl->ngroups * sizeof(*pctl->functions),
  870. GFP_KERNEL);
  871. if (!pctl->functions)
  872. return -ENOMEM;
  873. /* Count functions and their associated groups */
  874. for (i = 0; i < pctl->desc->npins; i++) {
  875. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  876. struct sunxi_desc_function *func;
  877. if (pin->variant && !(pctl->variant & pin->variant))
  878. continue;
  879. for (func = pin->functions; func->name; func++) {
  880. if (func->variant && !(pctl->variant & func->variant))
  881. continue;
  882. /* Create interrupt mapping while we're at it */
  883. if (!strcmp(func->name, "irq")) {
  884. int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
  885. pctl->irq_array[irqnum] = pin->pin.number;
  886. }
  887. sunxi_pinctrl_add_function(pctl, func->name);
  888. }
  889. }
  890. /* And now allocated and fill the array for real */
  891. pctl->functions = krealloc(pctl->functions,
  892. pctl->nfunctions * sizeof(*pctl->functions),
  893. GFP_KERNEL);
  894. if (!pctl->functions) {
  895. kfree(pctl->functions);
  896. return -ENOMEM;
  897. }
  898. for (i = 0; i < pctl->desc->npins; i++) {
  899. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  900. struct sunxi_desc_function *func;
  901. if (pin->variant && !(pctl->variant & pin->variant))
  902. continue;
  903. for (func = pin->functions; func->name; func++) {
  904. struct sunxi_pinctrl_function *func_item;
  905. const char **func_grp;
  906. if (func->variant && !(pctl->variant & func->variant))
  907. continue;
  908. func_item = sunxi_pinctrl_find_function_by_name(pctl,
  909. func->name);
  910. if (!func_item)
  911. return -EINVAL;
  912. if (!func_item->groups) {
  913. func_item->groups =
  914. devm_kzalloc(&pdev->dev,
  915. func_item->ngroups * sizeof(*func_item->groups),
  916. GFP_KERNEL);
  917. if (!func_item->groups)
  918. return -ENOMEM;
  919. }
  920. func_grp = func_item->groups;
  921. while (*func_grp)
  922. func_grp++;
  923. *func_grp = pin->pin.name;
  924. }
  925. }
  926. return 0;
  927. }
  928. static int sunxi_pinctrl_get_debounce_div(struct clk *clk, int freq, int *diff)
  929. {
  930. unsigned long clock = clk_get_rate(clk);
  931. unsigned int best_diff, best_div;
  932. int i;
  933. best_diff = abs(freq - clock);
  934. best_div = 0;
  935. for (i = 1; i < 8; i++) {
  936. int cur_diff = abs(freq - (clock >> i));
  937. if (cur_diff < best_diff) {
  938. best_diff = cur_diff;
  939. best_div = i;
  940. }
  941. }
  942. *diff = best_diff;
  943. return best_div;
  944. }
  945. static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
  946. struct device_node *node)
  947. {
  948. unsigned int hosc_diff, losc_diff;
  949. unsigned int hosc_div, losc_div;
  950. struct clk *hosc, *losc;
  951. u8 div, src;
  952. int i, ret;
  953. /* Deal with old DTs that didn't have the oscillators */
  954. if (of_count_phandle_with_args(node, "clocks", "#clock-cells") != 3)
  955. return 0;
  956. /* If we don't have any setup, bail out */
  957. if (!of_find_property(node, "input-debounce", NULL))
  958. return 0;
  959. losc = devm_clk_get(pctl->dev, "losc");
  960. if (IS_ERR(losc))
  961. return PTR_ERR(losc);
  962. hosc = devm_clk_get(pctl->dev, "hosc");
  963. if (IS_ERR(hosc))
  964. return PTR_ERR(hosc);
  965. for (i = 0; i < pctl->desc->irq_banks; i++) {
  966. unsigned long debounce_freq;
  967. u32 debounce;
  968. ret = of_property_read_u32_index(node, "input-debounce",
  969. i, &debounce);
  970. if (ret)
  971. return ret;
  972. if (!debounce)
  973. continue;
  974. debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC, debounce);
  975. losc_div = sunxi_pinctrl_get_debounce_div(losc,
  976. debounce_freq,
  977. &losc_diff);
  978. hosc_div = sunxi_pinctrl_get_debounce_div(hosc,
  979. debounce_freq,
  980. &hosc_diff);
  981. if (hosc_diff < losc_diff) {
  982. div = hosc_div;
  983. src = 1;
  984. } else {
  985. div = losc_div;
  986. src = 0;
  987. }
  988. writel(src | div << 4,
  989. pctl->membase +
  990. sunxi_irq_debounce_reg_from_bank(i,
  991. pctl->desc->irq_bank_base));
  992. }
  993. return 0;
  994. }
  995. int sunxi_pinctrl_init_with_variant(struct platform_device *pdev,
  996. const struct sunxi_pinctrl_desc *desc,
  997. unsigned long variant)
  998. {
  999. struct device_node *node = pdev->dev.of_node;
  1000. struct pinctrl_desc *pctrl_desc;
  1001. struct pinctrl_pin_desc *pins;
  1002. struct sunxi_pinctrl *pctl;
  1003. struct resource *res;
  1004. int i, ret, last_pin, pin_idx;
  1005. struct clk *clk;
  1006. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  1007. if (!pctl)
  1008. return -ENOMEM;
  1009. platform_set_drvdata(pdev, pctl);
  1010. raw_spin_lock_init(&pctl->lock);
  1011. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1012. pctl->membase = devm_ioremap_resource(&pdev->dev, res);
  1013. if (IS_ERR(pctl->membase))
  1014. return PTR_ERR(pctl->membase);
  1015. pctl->dev = &pdev->dev;
  1016. pctl->desc = desc;
  1017. pctl->variant = variant;
  1018. pctl->irq_array = devm_kcalloc(&pdev->dev,
  1019. IRQ_PER_BANK * pctl->desc->irq_banks,
  1020. sizeof(*pctl->irq_array),
  1021. GFP_KERNEL);
  1022. if (!pctl->irq_array)
  1023. return -ENOMEM;
  1024. ret = sunxi_pinctrl_build_state(pdev);
  1025. if (ret) {
  1026. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  1027. return ret;
  1028. }
  1029. pins = devm_kzalloc(&pdev->dev,
  1030. pctl->desc->npins * sizeof(*pins),
  1031. GFP_KERNEL);
  1032. if (!pins)
  1033. return -ENOMEM;
  1034. for (i = 0, pin_idx = 0; i < pctl->desc->npins; i++) {
  1035. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  1036. if (pin->variant && !(pctl->variant & pin->variant))
  1037. continue;
  1038. pins[pin_idx++] = pin->pin;
  1039. }
  1040. pctrl_desc = devm_kzalloc(&pdev->dev,
  1041. sizeof(*pctrl_desc),
  1042. GFP_KERNEL);
  1043. if (!pctrl_desc)
  1044. return -ENOMEM;
  1045. pctrl_desc->name = dev_name(&pdev->dev);
  1046. pctrl_desc->owner = THIS_MODULE;
  1047. pctrl_desc->pins = pins;
  1048. pctrl_desc->npins = pctl->ngroups;
  1049. pctrl_desc->confops = &sunxi_pconf_ops;
  1050. pctrl_desc->pctlops = &sunxi_pctrl_ops;
  1051. pctrl_desc->pmxops = &sunxi_pmx_ops;
  1052. pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
  1053. if (IS_ERR(pctl->pctl_dev)) {
  1054. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  1055. return PTR_ERR(pctl->pctl_dev);
  1056. }
  1057. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  1058. if (!pctl->chip)
  1059. return -ENOMEM;
  1060. last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
  1061. pctl->chip->owner = THIS_MODULE;
  1062. pctl->chip->request = gpiochip_generic_request,
  1063. pctl->chip->free = gpiochip_generic_free,
  1064. pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
  1065. pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
  1066. pctl->chip->get = sunxi_pinctrl_gpio_get,
  1067. pctl->chip->set = sunxi_pinctrl_gpio_set,
  1068. pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
  1069. pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
  1070. pctl->chip->of_gpio_n_cells = 3,
  1071. pctl->chip->can_sleep = false,
  1072. pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
  1073. pctl->desc->pin_base;
  1074. pctl->chip->label = dev_name(&pdev->dev);
  1075. pctl->chip->parent = &pdev->dev;
  1076. pctl->chip->base = pctl->desc->pin_base;
  1077. ret = gpiochip_add_data(pctl->chip, pctl);
  1078. if (ret)
  1079. return ret;
  1080. for (i = 0; i < pctl->desc->npins; i++) {
  1081. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  1082. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  1083. pin->pin.number - pctl->desc->pin_base,
  1084. pin->pin.number, 1);
  1085. if (ret)
  1086. goto gpiochip_error;
  1087. }
  1088. clk = devm_clk_get(&pdev->dev, NULL);
  1089. if (IS_ERR(clk)) {
  1090. ret = PTR_ERR(clk);
  1091. goto gpiochip_error;
  1092. }
  1093. ret = clk_prepare_enable(clk);
  1094. if (ret)
  1095. goto gpiochip_error;
  1096. pctl->irq = devm_kcalloc(&pdev->dev,
  1097. pctl->desc->irq_banks,
  1098. sizeof(*pctl->irq),
  1099. GFP_KERNEL);
  1100. if (!pctl->irq) {
  1101. ret = -ENOMEM;
  1102. goto clk_error;
  1103. }
  1104. for (i = 0; i < pctl->desc->irq_banks; i++) {
  1105. pctl->irq[i] = platform_get_irq(pdev, i);
  1106. if (pctl->irq[i] < 0) {
  1107. ret = pctl->irq[i];
  1108. goto clk_error;
  1109. }
  1110. }
  1111. pctl->domain = irq_domain_add_linear(node,
  1112. pctl->desc->irq_banks * IRQ_PER_BANK,
  1113. &sunxi_pinctrl_irq_domain_ops,
  1114. pctl);
  1115. if (!pctl->domain) {
  1116. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  1117. ret = -ENOMEM;
  1118. goto clk_error;
  1119. }
  1120. for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
  1121. int irqno = irq_create_mapping(pctl->domain, i);
  1122. irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
  1123. handle_edge_irq);
  1124. irq_set_chip_data(irqno, pctl);
  1125. }
  1126. for (i = 0; i < pctl->desc->irq_banks; i++) {
  1127. /* Mask and clear all IRQs before registering a handler */
  1128. writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i,
  1129. pctl->desc->irq_bank_base));
  1130. writel(0xffffffff,
  1131. pctl->membase + sunxi_irq_status_reg_from_bank(i,
  1132. pctl->desc->irq_bank_base));
  1133. irq_set_chained_handler_and_data(pctl->irq[i],
  1134. sunxi_pinctrl_irq_handler,
  1135. pctl);
  1136. }
  1137. sunxi_pinctrl_setup_debounce(pctl, node);
  1138. dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
  1139. return 0;
  1140. clk_error:
  1141. clk_disable_unprepare(clk);
  1142. gpiochip_error:
  1143. gpiochip_remove(pctl->chip);
  1144. return ret;
  1145. }