pinctrl-sunxi.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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. int i;
  840. /*
  841. * Allocate groups
  842. *
  843. * We assume that the number of groups is the number of pins
  844. * given in the data array.
  845. * This will not always be true, since some pins might not be
  846. * available in the current variant, but fortunately for us,
  847. * this means that the number of pins is the maximum group
  848. * number we will ever see.
  849. */
  850. pctl->groups = devm_kzalloc(&pdev->dev,
  851. pctl->desc->npins * sizeof(*pctl->groups),
  852. GFP_KERNEL);
  853. if (!pctl->groups)
  854. return -ENOMEM;
  855. for (i = 0; i < pctl->desc->npins; i++) {
  856. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  857. struct sunxi_pinctrl_group *group = pctl->groups + pctl->ngroups;
  858. if (pin->variant && !(pctl->variant & pin->variant))
  859. continue;
  860. group->name = pin->pin.name;
  861. group->pin = pin->pin.number;
  862. /* And now we count the actual number of pins / groups */
  863. pctl->ngroups++;
  864. }
  865. /*
  866. * We suppose that we won't have any more functions than pins,
  867. * we'll reallocate that later anyway
  868. */
  869. pctl->functions = devm_kzalloc(&pdev->dev,
  870. pctl->ngroups * sizeof(*pctl->functions),
  871. GFP_KERNEL);
  872. if (!pctl->functions)
  873. return -ENOMEM;
  874. /* Count functions and their associated groups */
  875. for (i = 0; i < pctl->desc->npins; i++) {
  876. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  877. struct sunxi_desc_function *func;
  878. if (pin->variant && !(pctl->variant & pin->variant))
  879. continue;
  880. for (func = pin->functions; func->name; func++) {
  881. if (func->variant && !(pctl->variant & func->variant))
  882. continue;
  883. /* Create interrupt mapping while we're at it */
  884. if (!strcmp(func->name, "irq")) {
  885. int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
  886. pctl->irq_array[irqnum] = pin->pin.number;
  887. }
  888. sunxi_pinctrl_add_function(pctl, func->name);
  889. }
  890. }
  891. /* And now allocated and fill the array for real */
  892. pctl->functions = krealloc(pctl->functions,
  893. pctl->nfunctions * sizeof(*pctl->functions),
  894. GFP_KERNEL);
  895. if (!pctl->functions) {
  896. kfree(pctl->functions);
  897. return -ENOMEM;
  898. }
  899. for (i = 0; i < pctl->desc->npins; i++) {
  900. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  901. struct sunxi_desc_function *func;
  902. if (pin->variant && !(pctl->variant & pin->variant))
  903. continue;
  904. for (func = pin->functions; func->name; func++) {
  905. struct sunxi_pinctrl_function *func_item;
  906. const char **func_grp;
  907. if (func->variant && !(pctl->variant & func->variant))
  908. continue;
  909. func_item = sunxi_pinctrl_find_function_by_name(pctl,
  910. func->name);
  911. if (!func_item)
  912. return -EINVAL;
  913. if (!func_item->groups) {
  914. func_item->groups =
  915. devm_kzalloc(&pdev->dev,
  916. func_item->ngroups * sizeof(*func_item->groups),
  917. GFP_KERNEL);
  918. if (!func_item->groups)
  919. return -ENOMEM;
  920. }
  921. func_grp = func_item->groups;
  922. while (*func_grp)
  923. func_grp++;
  924. *func_grp = pin->pin.name;
  925. }
  926. }
  927. return 0;
  928. }
  929. static int sunxi_pinctrl_get_debounce_div(struct clk *clk, int freq, int *diff)
  930. {
  931. unsigned long clock = clk_get_rate(clk);
  932. unsigned int best_diff, best_div;
  933. int i;
  934. best_diff = abs(freq - clock);
  935. best_div = 0;
  936. for (i = 1; i < 8; i++) {
  937. int cur_diff = abs(freq - (clock >> i));
  938. if (cur_diff < best_diff) {
  939. best_diff = cur_diff;
  940. best_div = i;
  941. }
  942. }
  943. *diff = best_diff;
  944. return best_div;
  945. }
  946. static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
  947. struct device_node *node)
  948. {
  949. unsigned int hosc_diff, losc_diff;
  950. unsigned int hosc_div, losc_div;
  951. struct clk *hosc, *losc;
  952. u8 div, src;
  953. int i, ret;
  954. /* Deal with old DTs that didn't have the oscillators */
  955. if (of_count_phandle_with_args(node, "clocks", "#clock-cells") != 3)
  956. return 0;
  957. /* If we don't have any setup, bail out */
  958. if (!of_find_property(node, "input-debounce", NULL))
  959. return 0;
  960. losc = devm_clk_get(pctl->dev, "losc");
  961. if (IS_ERR(losc))
  962. return PTR_ERR(losc);
  963. hosc = devm_clk_get(pctl->dev, "hosc");
  964. if (IS_ERR(hosc))
  965. return PTR_ERR(hosc);
  966. for (i = 0; i < pctl->desc->irq_banks; i++) {
  967. unsigned long debounce_freq;
  968. u32 debounce;
  969. ret = of_property_read_u32_index(node, "input-debounce",
  970. i, &debounce);
  971. if (ret)
  972. return ret;
  973. if (!debounce)
  974. continue;
  975. debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC, debounce);
  976. losc_div = sunxi_pinctrl_get_debounce_div(losc,
  977. debounce_freq,
  978. &losc_diff);
  979. hosc_div = sunxi_pinctrl_get_debounce_div(hosc,
  980. debounce_freq,
  981. &hosc_diff);
  982. if (hosc_diff < losc_diff) {
  983. div = hosc_div;
  984. src = 1;
  985. } else {
  986. div = losc_div;
  987. src = 0;
  988. }
  989. writel(src | div << 4,
  990. pctl->membase +
  991. sunxi_irq_debounce_reg_from_bank(i,
  992. pctl->desc->irq_bank_base));
  993. }
  994. return 0;
  995. }
  996. int sunxi_pinctrl_init_with_variant(struct platform_device *pdev,
  997. const struct sunxi_pinctrl_desc *desc,
  998. unsigned long variant)
  999. {
  1000. struct device_node *node = pdev->dev.of_node;
  1001. struct pinctrl_desc *pctrl_desc;
  1002. struct pinctrl_pin_desc *pins;
  1003. struct sunxi_pinctrl *pctl;
  1004. struct resource *res;
  1005. int i, ret, last_pin, pin_idx;
  1006. struct clk *clk;
  1007. pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
  1008. if (!pctl)
  1009. return -ENOMEM;
  1010. platform_set_drvdata(pdev, pctl);
  1011. raw_spin_lock_init(&pctl->lock);
  1012. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1013. pctl->membase = devm_ioremap_resource(&pdev->dev, res);
  1014. if (IS_ERR(pctl->membase))
  1015. return PTR_ERR(pctl->membase);
  1016. pctl->dev = &pdev->dev;
  1017. pctl->desc = desc;
  1018. pctl->variant = variant;
  1019. pctl->irq_array = devm_kcalloc(&pdev->dev,
  1020. IRQ_PER_BANK * pctl->desc->irq_banks,
  1021. sizeof(*pctl->irq_array),
  1022. GFP_KERNEL);
  1023. if (!pctl->irq_array)
  1024. return -ENOMEM;
  1025. ret = sunxi_pinctrl_build_state(pdev);
  1026. if (ret) {
  1027. dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
  1028. return ret;
  1029. }
  1030. pins = devm_kzalloc(&pdev->dev,
  1031. pctl->desc->npins * sizeof(*pins),
  1032. GFP_KERNEL);
  1033. if (!pins)
  1034. return -ENOMEM;
  1035. for (i = 0, pin_idx = 0; i < pctl->desc->npins; i++) {
  1036. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  1037. if (pin->variant && !(pctl->variant & pin->variant))
  1038. continue;
  1039. pins[pin_idx++] = pin->pin;
  1040. }
  1041. pctrl_desc = devm_kzalloc(&pdev->dev,
  1042. sizeof(*pctrl_desc),
  1043. GFP_KERNEL);
  1044. if (!pctrl_desc)
  1045. return -ENOMEM;
  1046. pctrl_desc->name = dev_name(&pdev->dev);
  1047. pctrl_desc->owner = THIS_MODULE;
  1048. pctrl_desc->pins = pins;
  1049. pctrl_desc->npins = pctl->ngroups;
  1050. pctrl_desc->confops = &sunxi_pconf_ops;
  1051. pctrl_desc->pctlops = &sunxi_pctrl_ops;
  1052. pctrl_desc->pmxops = &sunxi_pmx_ops;
  1053. pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
  1054. if (IS_ERR(pctl->pctl_dev)) {
  1055. dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
  1056. return PTR_ERR(pctl->pctl_dev);
  1057. }
  1058. pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
  1059. if (!pctl->chip)
  1060. return -ENOMEM;
  1061. last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
  1062. pctl->chip->owner = THIS_MODULE;
  1063. pctl->chip->request = gpiochip_generic_request,
  1064. pctl->chip->free = gpiochip_generic_free,
  1065. pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
  1066. pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
  1067. pctl->chip->get = sunxi_pinctrl_gpio_get,
  1068. pctl->chip->set = sunxi_pinctrl_gpio_set,
  1069. pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
  1070. pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
  1071. pctl->chip->of_gpio_n_cells = 3,
  1072. pctl->chip->can_sleep = false,
  1073. pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
  1074. pctl->desc->pin_base;
  1075. pctl->chip->label = dev_name(&pdev->dev);
  1076. pctl->chip->parent = &pdev->dev;
  1077. pctl->chip->base = pctl->desc->pin_base;
  1078. ret = gpiochip_add_data(pctl->chip, pctl);
  1079. if (ret)
  1080. return ret;
  1081. for (i = 0; i < pctl->desc->npins; i++) {
  1082. const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
  1083. ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
  1084. pin->pin.number - pctl->desc->pin_base,
  1085. pin->pin.number, 1);
  1086. if (ret)
  1087. goto gpiochip_error;
  1088. }
  1089. clk = devm_clk_get(&pdev->dev, NULL);
  1090. if (IS_ERR(clk)) {
  1091. ret = PTR_ERR(clk);
  1092. goto gpiochip_error;
  1093. }
  1094. ret = clk_prepare_enable(clk);
  1095. if (ret)
  1096. goto gpiochip_error;
  1097. pctl->irq = devm_kcalloc(&pdev->dev,
  1098. pctl->desc->irq_banks,
  1099. sizeof(*pctl->irq),
  1100. GFP_KERNEL);
  1101. if (!pctl->irq) {
  1102. ret = -ENOMEM;
  1103. goto clk_error;
  1104. }
  1105. for (i = 0; i < pctl->desc->irq_banks; i++) {
  1106. pctl->irq[i] = platform_get_irq(pdev, i);
  1107. if (pctl->irq[i] < 0) {
  1108. ret = pctl->irq[i];
  1109. goto clk_error;
  1110. }
  1111. }
  1112. pctl->domain = irq_domain_add_linear(node,
  1113. pctl->desc->irq_banks * IRQ_PER_BANK,
  1114. &sunxi_pinctrl_irq_domain_ops,
  1115. pctl);
  1116. if (!pctl->domain) {
  1117. dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
  1118. ret = -ENOMEM;
  1119. goto clk_error;
  1120. }
  1121. for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
  1122. int irqno = irq_create_mapping(pctl->domain, i);
  1123. irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
  1124. handle_edge_irq);
  1125. irq_set_chip_data(irqno, pctl);
  1126. }
  1127. for (i = 0; i < pctl->desc->irq_banks; i++) {
  1128. /* Mask and clear all IRQs before registering a handler */
  1129. writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i,
  1130. pctl->desc->irq_bank_base));
  1131. writel(0xffffffff,
  1132. pctl->membase + sunxi_irq_status_reg_from_bank(i,
  1133. pctl->desc->irq_bank_base));
  1134. irq_set_chained_handler_and_data(pctl->irq[i],
  1135. sunxi_pinctrl_irq_handler,
  1136. pctl);
  1137. }
  1138. sunxi_pinctrl_setup_debounce(pctl, node);
  1139. dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
  1140. return 0;
  1141. clk_error:
  1142. clk_disable_unprepare(clk);
  1143. gpiochip_error:
  1144. gpiochip_remove(pctl->chip);
  1145. return ret;
  1146. }