pinctrl-sunxi.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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. val = 0;
  459. break;
  460. case PIN_CONFIG_BIAS_PULL_UP:
  461. if (arg == 0)
  462. return -EINVAL;
  463. val = 1;
  464. break;
  465. case PIN_CONFIG_BIAS_PULL_DOWN:
  466. if (arg == 0)
  467. return -EINVAL;
  468. val = 2;
  469. break;
  470. default:
  471. /* sunxi_pconf_reg should catch anything unsupported */
  472. WARN_ON(1);
  473. return -ENOTSUPP;
  474. }
  475. raw_spin_lock_irqsave(&pctl->lock, flags);
  476. reg = readl(pctl->membase + offset);
  477. reg &= ~(mask << shift);
  478. writel(reg | val << shift, pctl->membase + offset);
  479. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  480. } /* for each config */
  481. return 0;
  482. }
  483. static const struct pinconf_ops sunxi_pconf_ops = {
  484. .is_generic = true,
  485. .pin_config_get = sunxi_pconf_get,
  486. .pin_config_group_get = sunxi_pconf_group_get,
  487. .pin_config_group_set = sunxi_pconf_group_set,
  488. };
  489. static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  490. {
  491. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  492. return pctl->nfunctions;
  493. }
  494. static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
  495. unsigned function)
  496. {
  497. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  498. return pctl->functions[function].name;
  499. }
  500. static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  501. unsigned function,
  502. const char * const **groups,
  503. unsigned * const num_groups)
  504. {
  505. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  506. *groups = pctl->functions[function].groups;
  507. *num_groups = pctl->functions[function].ngroups;
  508. return 0;
  509. }
  510. static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
  511. unsigned pin,
  512. u8 config)
  513. {
  514. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  515. unsigned long flags;
  516. u32 val, mask;
  517. raw_spin_lock_irqsave(&pctl->lock, flags);
  518. pin -= pctl->desc->pin_base;
  519. val = readl(pctl->membase + sunxi_mux_reg(pin));
  520. mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
  521. writel((val & ~mask) | config << sunxi_mux_offset(pin),
  522. pctl->membase + sunxi_mux_reg(pin));
  523. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  524. }
  525. static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev,
  526. unsigned function,
  527. unsigned group)
  528. {
  529. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  530. struct sunxi_pinctrl_group *g = pctl->groups + group;
  531. struct sunxi_pinctrl_function *func = pctl->functions + function;
  532. struct sunxi_desc_function *desc =
  533. sunxi_pinctrl_desc_find_function_by_name(pctl,
  534. g->name,
  535. func->name);
  536. if (!desc)
  537. return -EINVAL;
  538. sunxi_pmx_set(pctldev, g->pin, desc->muxval);
  539. return 0;
  540. }
  541. static int
  542. sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  543. struct pinctrl_gpio_range *range,
  544. unsigned offset,
  545. bool input)
  546. {
  547. struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
  548. struct sunxi_desc_function *desc;
  549. const char *func;
  550. if (input)
  551. func = "gpio_in";
  552. else
  553. func = "gpio_out";
  554. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
  555. if (!desc)
  556. return -EINVAL;
  557. sunxi_pmx_set(pctldev, offset, desc->muxval);
  558. return 0;
  559. }
  560. static const struct pinmux_ops sunxi_pmx_ops = {
  561. .get_functions_count = sunxi_pmx_get_funcs_cnt,
  562. .get_function_name = sunxi_pmx_get_func_name,
  563. .get_function_groups = sunxi_pmx_get_func_groups,
  564. .set_mux = sunxi_pmx_set_mux,
  565. .gpio_set_direction = sunxi_pmx_gpio_set_direction,
  566. };
  567. static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
  568. unsigned offset)
  569. {
  570. return pinctrl_gpio_direction_input(chip->base + offset);
  571. }
  572. static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
  573. {
  574. struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
  575. u32 reg = sunxi_data_reg(offset);
  576. u8 index = sunxi_data_offset(offset);
  577. bool set_mux = pctl->desc->irq_read_needs_mux &&
  578. gpiochip_line_is_irq(chip, offset);
  579. u32 pin = offset + chip->base;
  580. u32 val;
  581. if (set_mux)
  582. sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_INPUT);
  583. val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
  584. if (set_mux)
  585. sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_IRQ);
  586. return !!val;
  587. }
  588. static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
  589. unsigned offset, int value)
  590. {
  591. struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
  592. u32 reg = sunxi_data_reg(offset);
  593. u8 index = sunxi_data_offset(offset);
  594. unsigned long flags;
  595. u32 regval;
  596. raw_spin_lock_irqsave(&pctl->lock, flags);
  597. regval = readl(pctl->membase + reg);
  598. if (value)
  599. regval |= BIT(index);
  600. else
  601. regval &= ~(BIT(index));
  602. writel(regval, pctl->membase + reg);
  603. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  604. }
  605. static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
  606. unsigned offset, int value)
  607. {
  608. sunxi_pinctrl_gpio_set(chip, offset, value);
  609. return pinctrl_gpio_direction_output(chip->base + offset);
  610. }
  611. static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
  612. const struct of_phandle_args *gpiospec,
  613. u32 *flags)
  614. {
  615. int pin, base;
  616. base = PINS_PER_BANK * gpiospec->args[0];
  617. pin = base + gpiospec->args[1];
  618. if (pin > gc->ngpio)
  619. return -EINVAL;
  620. if (flags)
  621. *flags = gpiospec->args[2];
  622. return pin;
  623. }
  624. static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  625. {
  626. struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
  627. struct sunxi_desc_function *desc;
  628. unsigned pinnum = pctl->desc->pin_base + offset;
  629. unsigned irqnum;
  630. if (offset >= chip->ngpio)
  631. return -ENXIO;
  632. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
  633. if (!desc)
  634. return -EINVAL;
  635. irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
  636. dev_dbg(chip->parent, "%s: request IRQ for GPIO %d, return %d\n",
  637. chip->label, offset + chip->base, irqnum);
  638. return irq_find_mapping(pctl->domain, irqnum);
  639. }
  640. static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
  641. {
  642. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  643. struct sunxi_desc_function *func;
  644. int ret;
  645. func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
  646. pctl->irq_array[d->hwirq], "irq");
  647. if (!func)
  648. return -EINVAL;
  649. ret = gpiochip_lock_as_irq(pctl->chip,
  650. pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
  651. if (ret) {
  652. dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
  653. irqd_to_hwirq(d));
  654. return ret;
  655. }
  656. /* Change muxing to INT mode */
  657. sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
  658. return 0;
  659. }
  660. static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
  661. {
  662. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  663. gpiochip_unlock_as_irq(pctl->chip,
  664. pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
  665. }
  666. static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
  667. {
  668. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  669. u32 reg = sunxi_irq_cfg_reg(d->hwirq, pctl->desc->irq_bank_base);
  670. u8 index = sunxi_irq_cfg_offset(d->hwirq);
  671. unsigned long flags;
  672. u32 regval;
  673. u8 mode;
  674. switch (type) {
  675. case IRQ_TYPE_EDGE_RISING:
  676. mode = IRQ_EDGE_RISING;
  677. break;
  678. case IRQ_TYPE_EDGE_FALLING:
  679. mode = IRQ_EDGE_FALLING;
  680. break;
  681. case IRQ_TYPE_EDGE_BOTH:
  682. mode = IRQ_EDGE_BOTH;
  683. break;
  684. case IRQ_TYPE_LEVEL_HIGH:
  685. mode = IRQ_LEVEL_HIGH;
  686. break;
  687. case IRQ_TYPE_LEVEL_LOW:
  688. mode = IRQ_LEVEL_LOW;
  689. break;
  690. default:
  691. return -EINVAL;
  692. }
  693. raw_spin_lock_irqsave(&pctl->lock, flags);
  694. if (type & IRQ_TYPE_LEVEL_MASK)
  695. irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
  696. handle_fasteoi_irq, NULL);
  697. else
  698. irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
  699. handle_edge_irq, NULL);
  700. regval = readl(pctl->membase + reg);
  701. regval &= ~(IRQ_CFG_IRQ_MASK << index);
  702. writel(regval | (mode << index), pctl->membase + reg);
  703. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  704. return 0;
  705. }
  706. static void sunxi_pinctrl_irq_ack(struct irq_data *d)
  707. {
  708. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  709. u32 status_reg = sunxi_irq_status_reg(d->hwirq,
  710. pctl->desc->irq_bank_base);
  711. u8 status_idx = sunxi_irq_status_offset(d->hwirq);
  712. /* Clear the IRQ */
  713. writel(1 << status_idx, pctl->membase + status_reg);
  714. }
  715. static void sunxi_pinctrl_irq_mask(struct irq_data *d)
  716. {
  717. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  718. u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
  719. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  720. unsigned long flags;
  721. u32 val;
  722. raw_spin_lock_irqsave(&pctl->lock, flags);
  723. /* Mask the IRQ */
  724. val = readl(pctl->membase + reg);
  725. writel(val & ~(1 << idx), pctl->membase + reg);
  726. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  727. }
  728. static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
  729. {
  730. struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
  731. u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
  732. u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
  733. unsigned long flags;
  734. u32 val;
  735. raw_spin_lock_irqsave(&pctl->lock, flags);
  736. /* Unmask the IRQ */
  737. val = readl(pctl->membase + reg);
  738. writel(val | (1 << idx), pctl->membase + reg);
  739. raw_spin_unlock_irqrestore(&pctl->lock, flags);
  740. }
  741. static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
  742. {
  743. sunxi_pinctrl_irq_ack(d);
  744. sunxi_pinctrl_irq_unmask(d);
  745. }
  746. static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
  747. .name = "sunxi_pio_edge",
  748. .irq_ack = sunxi_pinctrl_irq_ack,
  749. .irq_mask = sunxi_pinctrl_irq_mask,
  750. .irq_unmask = sunxi_pinctrl_irq_unmask,
  751. .irq_request_resources = sunxi_pinctrl_irq_request_resources,
  752. .irq_release_resources = sunxi_pinctrl_irq_release_resources,
  753. .irq_set_type = sunxi_pinctrl_irq_set_type,
  754. .flags = IRQCHIP_SKIP_SET_WAKE,
  755. };
  756. static struct irq_chip sunxi_pinctrl_level_irq_chip = {
  757. .name = "sunxi_pio_level",
  758. .irq_eoi = sunxi_pinctrl_irq_ack,
  759. .irq_mask = sunxi_pinctrl_irq_mask,
  760. .irq_unmask = sunxi_pinctrl_irq_unmask,
  761. /* Define irq_enable / disable to avoid spurious irqs for drivers
  762. * using these to suppress irqs while they clear the irq source */
  763. .irq_enable = sunxi_pinctrl_irq_ack_unmask,
  764. .irq_disable = sunxi_pinctrl_irq_mask,
  765. .irq_request_resources = sunxi_pinctrl_irq_request_resources,
  766. .irq_release_resources = sunxi_pinctrl_irq_release_resources,
  767. .irq_set_type = sunxi_pinctrl_irq_set_type,
  768. .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_EOI_THREADED |
  769. IRQCHIP_EOI_IF_HANDLED,
  770. };
  771. static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
  772. struct device_node *node,
  773. const u32 *intspec,
  774. unsigned int intsize,
  775. unsigned long *out_hwirq,
  776. unsigned int *out_type)
  777. {
  778. struct sunxi_pinctrl *pctl = d->host_data;
  779. struct sunxi_desc_function *desc;
  780. int pin, base;
  781. if (intsize < 3)
  782. return -EINVAL;
  783. base = PINS_PER_BANK * intspec[0];
  784. pin = pctl->desc->pin_base + base + intspec[1];
  785. desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
  786. if (!desc)
  787. return -EINVAL;
  788. *out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
  789. *out_type = intspec[2];
  790. return 0;
  791. }
  792. static const struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
  793. .xlate = sunxi_pinctrl_irq_of_xlate,
  794. };
  795. static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
  796. {
  797. unsigned int irq = irq_desc_get_irq(desc);
  798. struct irq_chip *chip = irq_desc_get_chip(desc);
  799. struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
  800. unsigned long bank, reg, val;
  801. for (bank = 0; bank < pctl->desc->irq_banks; bank++)
  802. if (irq == pctl->irq[bank])
  803. break;
  804. if (bank == pctl->desc->irq_banks)
  805. return;
  806. reg = sunxi_irq_status_reg_from_bank(bank, pctl->desc->irq_bank_base);
  807. val = readl(pctl->membase + reg);
  808. if (val) {
  809. int irqoffset;
  810. chained_irq_enter(chip, desc);
  811. for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
  812. int pin_irq = irq_find_mapping(pctl->domain,
  813. bank * IRQ_PER_BANK + irqoffset);
  814. generic_handle_irq(pin_irq);
  815. }
  816. chained_irq_exit(chip, desc);
  817. }
  818. }
  819. static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
  820. const char *name)
  821. {
  822. struct sunxi_pinctrl_function *func = pctl->functions;
  823. while (func->name) {
  824. /* function already there */
  825. if (strcmp(func->name, name) == 0) {
  826. func->ngroups++;
  827. return -EEXIST;
  828. }
  829. func++;
  830. }
  831. func->name = name;
  832. func->ngroups = 1;
  833. pctl->nfunctions++;
  834. return 0;
  835. }
  836. static int sunxi_pinctrl_build_state(struct platform_device *pdev)
  837. {
  838. struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
  839. void *ptr;
  840. int i;
  841. /*
  842. * Allocate groups
  843. *
  844. * We assume that the number of groups is the number of pins
  845. * given in the data array.
  846. * This will not always be true, since some pins might not be
  847. * available in the current variant, but fortunately for us,
  848. * this means that the number of pins is the maximum group
  849. * number we will ever see.
  850. */
  851. pctl->groups = devm_kzalloc(&pdev->dev,
  852. pctl->desc->npins * sizeof(*pctl->groups),
  853. GFP_KERNEL);
  854. if (!pctl->groups)
  855. return -ENOMEM;
  856. for (i = 0; i < pctl->desc->npins; i++) {
  857. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  858. struct sunxi_pinctrl_group *group = pctl->groups + pctl->ngroups;
  859. if (pin->variant && !(pctl->variant & pin->variant))
  860. continue;
  861. group->name = pin->pin.name;
  862. group->pin = pin->pin.number;
  863. /* And now we count the actual number of pins / groups */
  864. pctl->ngroups++;
  865. }
  866. /*
  867. * We suppose that we won't have any more functions than pins,
  868. * we'll reallocate that later anyway
  869. */
  870. pctl->functions = devm_kzalloc(&pdev->dev,
  871. pctl->ngroups * sizeof(*pctl->functions),
  872. GFP_KERNEL);
  873. if (!pctl->functions)
  874. return -ENOMEM;
  875. /* Count functions and their associated groups */
  876. for (i = 0; i < pctl->desc->npins; i++) {
  877. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  878. struct sunxi_desc_function *func;
  879. if (pin->variant && !(pctl->variant & pin->variant))
  880. continue;
  881. for (func = pin->functions; func->name; func++) {
  882. if (func->variant && !(pctl->variant & func->variant))
  883. continue;
  884. /* Create interrupt mapping while we're at it */
  885. if (!strcmp(func->name, "irq")) {
  886. int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
  887. pctl->irq_array[irqnum] = pin->pin.number;
  888. }
  889. sunxi_pinctrl_add_function(pctl, func->name);
  890. }
  891. }
  892. /* And now allocated and fill the array for real */
  893. ptr = krealloc(pctl->functions,
  894. pctl->nfunctions * sizeof(*pctl->functions),
  895. GFP_KERNEL);
  896. if (!ptr) {
  897. kfree(pctl->functions);
  898. pctl->functions = NULL;
  899. return -ENOMEM;
  900. }
  901. pctl->functions = ptr;
  902. for (i = 0; i < pctl->desc->npins; i++) {
  903. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  904. struct sunxi_desc_function *func;
  905. if (pin->variant && !(pctl->variant & pin->variant))
  906. continue;
  907. for (func = pin->functions; func->name; func++) {
  908. struct sunxi_pinctrl_function *func_item;
  909. const char **func_grp;
  910. if (func->variant && !(pctl->variant & func->variant))
  911. continue;
  912. func_item = sunxi_pinctrl_find_function_by_name(pctl,
  913. func->name);
  914. if (!func_item)
  915. return -EINVAL;
  916. if (!func_item->groups) {
  917. func_item->groups =
  918. devm_kzalloc(&pdev->dev,
  919. func_item->ngroups * sizeof(*func_item->groups),
  920. GFP_KERNEL);
  921. if (!func_item->groups)
  922. return -ENOMEM;
  923. }
  924. func_grp = func_item->groups;
  925. while (*func_grp)
  926. func_grp++;
  927. *func_grp = pin->pin.name;
  928. }
  929. }
  930. return 0;
  931. }
  932. static int sunxi_pinctrl_get_debounce_div(struct clk *clk, int freq, int *diff)
  933. {
  934. unsigned long clock = clk_get_rate(clk);
  935. unsigned int best_diff, best_div;
  936. int i;
  937. best_diff = abs(freq - clock);
  938. best_div = 0;
  939. for (i = 1; i < 8; i++) {
  940. int cur_diff = abs(freq - (clock >> i));
  941. if (cur_diff < best_diff) {
  942. best_diff = cur_diff;
  943. best_div = i;
  944. }
  945. }
  946. *diff = best_diff;
  947. return best_div;
  948. }
  949. static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
  950. struct device_node *node)
  951. {
  952. unsigned int hosc_diff, losc_diff;
  953. unsigned int hosc_div, losc_div;
  954. struct clk *hosc, *losc;
  955. u8 div, src;
  956. int i, ret;
  957. /* Deal with old DTs that didn't have the oscillators */
  958. if (of_count_phandle_with_args(node, "clocks", "#clock-cells") != 3)
  959. return 0;
  960. /* If we don't have any setup, bail out */
  961. if (!of_find_property(node, "input-debounce", NULL))
  962. return 0;
  963. losc = devm_clk_get(pctl->dev, "losc");
  964. if (IS_ERR(losc))
  965. return PTR_ERR(losc);
  966. hosc = devm_clk_get(pctl->dev, "hosc");
  967. if (IS_ERR(hosc))
  968. return PTR_ERR(hosc);
  969. for (i = 0; i < pctl->desc->irq_banks; i++) {
  970. unsigned long debounce_freq;
  971. u32 debounce;
  972. ret = of_property_read_u32_index(node, "input-debounce",
  973. i, &debounce);
  974. if (ret)
  975. return ret;
  976. if (!debounce)
  977. continue;
  978. debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC, debounce);
  979. losc_div = sunxi_pinctrl_get_debounce_div(losc,
  980. debounce_freq,
  981. &losc_diff);
  982. hosc_div = sunxi_pinctrl_get_debounce_div(hosc,
  983. debounce_freq,
  984. &hosc_diff);
  985. if (hosc_diff < losc_diff) {
  986. div = hosc_div;
  987. src = 1;
  988. } else {
  989. div = losc_div;
  990. src = 0;
  991. }
  992. writel(src | div << 4,
  993. pctl->membase +
  994. sunxi_irq_debounce_reg_from_bank(i,
  995. pctl->desc->irq_bank_base));
  996. }
  997. return 0;
  998. }
  999. int sunxi_pinctrl_init_with_variant(struct platform_device *pdev,
  1000. const struct sunxi_pinctrl_desc *desc,
  1001. unsigned long variant)
  1002. {
  1003. struct device_node *node = pdev->dev.of_node;
  1004. struct pinctrl_desc *pctrl_desc;
  1005. struct pinctrl_pin_desc *pins;
  1006. struct sunxi_pinctrl *pctl;
  1007. struct resource *res;
  1008. int i, ret, last_pin, pin_idx;
  1009. struct clk *clk;
  1010. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  1011. if (!pctl)
  1012. return -ENOMEM;
  1013. platform_set_drvdata(pdev, pctl);
  1014. raw_spin_lock_init(&pctl->lock);
  1015. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1016. pctl->membase = devm_ioremap_resource(&pdev->dev, res);
  1017. if (IS_ERR(pctl->membase))
  1018. return PTR_ERR(pctl->membase);
  1019. pctl->dev = &pdev->dev;
  1020. pctl->desc = desc;
  1021. pctl->variant = variant;
  1022. pctl->irq_array = devm_kcalloc(&pdev->dev,
  1023. IRQ_PER_BANK * pctl->desc->irq_banks,
  1024. sizeof(*pctl->irq_array),
  1025. GFP_KERNEL);
  1026. if (!pctl->irq_array)
  1027. return -ENOMEM;
  1028. ret = sunxi_pinctrl_build_state(pdev);
  1029. if (ret) {
  1030. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  1031. return ret;
  1032. }
  1033. pins = devm_kzalloc(&pdev->dev,
  1034. pctl->desc->npins * sizeof(*pins),
  1035. GFP_KERNEL);
  1036. if (!pins)
  1037. return -ENOMEM;
  1038. for (i = 0, pin_idx = 0; i < pctl->desc->npins; i++) {
  1039. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  1040. if (pin->variant && !(pctl->variant & pin->variant))
  1041. continue;
  1042. pins[pin_idx++] = pin->pin;
  1043. }
  1044. pctrl_desc = devm_kzalloc(&pdev->dev,
  1045. sizeof(*pctrl_desc),
  1046. GFP_KERNEL);
  1047. if (!pctrl_desc)
  1048. return -ENOMEM;
  1049. pctrl_desc->name = dev_name(&pdev->dev);
  1050. pctrl_desc->owner = THIS_MODULE;
  1051. pctrl_desc->pins = pins;
  1052. pctrl_desc->npins = pctl->ngroups;
  1053. pctrl_desc->confops = &sunxi_pconf_ops;
  1054. pctrl_desc->pctlops = &sunxi_pctrl_ops;
  1055. pctrl_desc->pmxops = &sunxi_pmx_ops;
  1056. pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
  1057. if (IS_ERR(pctl->pctl_dev)) {
  1058. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  1059. return PTR_ERR(pctl->pctl_dev);
  1060. }
  1061. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  1062. if (!pctl->chip)
  1063. return -ENOMEM;
  1064. last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
  1065. pctl->chip->owner = THIS_MODULE;
  1066. pctl->chip->request = gpiochip_generic_request,
  1067. pctl->chip->free = gpiochip_generic_free,
  1068. pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
  1069. pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
  1070. pctl->chip->get = sunxi_pinctrl_gpio_get,
  1071. pctl->chip->set = sunxi_pinctrl_gpio_set,
  1072. pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
  1073. pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
  1074. pctl->chip->of_gpio_n_cells = 3,
  1075. pctl->chip->can_sleep = false,
  1076. pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
  1077. pctl->desc->pin_base;
  1078. pctl->chip->label = dev_name(&pdev->dev);
  1079. pctl->chip->parent = &pdev->dev;
  1080. pctl->chip->base = pctl->desc->pin_base;
  1081. ret = gpiochip_add_data(pctl->chip, pctl);
  1082. if (ret)
  1083. return ret;
  1084. for (i = 0; i < pctl->desc->npins; i++) {
  1085. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  1086. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  1087. pin->pin.number - pctl->desc->pin_base,
  1088. pin->pin.number, 1);
  1089. if (ret)
  1090. goto gpiochip_error;
  1091. }
  1092. clk = devm_clk_get(&pdev->dev, NULL);
  1093. if (IS_ERR(clk)) {
  1094. ret = PTR_ERR(clk);
  1095. goto gpiochip_error;
  1096. }
  1097. ret = clk_prepare_enable(clk);
  1098. if (ret)
  1099. goto gpiochip_error;
  1100. pctl->irq = devm_kcalloc(&pdev->dev,
  1101. pctl->desc->irq_banks,
  1102. sizeof(*pctl->irq),
  1103. GFP_KERNEL);
  1104. if (!pctl->irq) {
  1105. ret = -ENOMEM;
  1106. goto clk_error;
  1107. }
  1108. for (i = 0; i < pctl->desc->irq_banks; i++) {
  1109. pctl->irq[i] = platform_get_irq(pdev, i);
  1110. if (pctl->irq[i] < 0) {
  1111. ret = pctl->irq[i];
  1112. goto clk_error;
  1113. }
  1114. }
  1115. pctl->domain = irq_domain_add_linear(node,
  1116. pctl->desc->irq_banks * IRQ_PER_BANK,
  1117. &sunxi_pinctrl_irq_domain_ops,
  1118. pctl);
  1119. if (!pctl->domain) {
  1120. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  1121. ret = -ENOMEM;
  1122. goto clk_error;
  1123. }
  1124. for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
  1125. int irqno = irq_create_mapping(pctl->domain, i);
  1126. irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
  1127. handle_edge_irq);
  1128. irq_set_chip_data(irqno, pctl);
  1129. }
  1130. for (i = 0; i < pctl->desc->irq_banks; i++) {
  1131. /* Mask and clear all IRQs before registering a handler */
  1132. writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i,
  1133. pctl->desc->irq_bank_base));
  1134. writel(0xffffffff,
  1135. pctl->membase + sunxi_irq_status_reg_from_bank(i,
  1136. pctl->desc->irq_bank_base));
  1137. irq_set_chained_handler_and_data(pctl->irq[i],
  1138. sunxi_pinctrl_irq_handler,
  1139. pctl);
  1140. }
  1141. sunxi_pinctrl_setup_debounce(pctl, node);
  1142. dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
  1143. return 0;
  1144. clk_error:
  1145. clk_disable_unprepare(clk);
  1146. gpiochip_error:
  1147. gpiochip_remove(pctl->chip);
  1148. return ret;
  1149. }