pinctrl-sunxi.c 34 KB

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