pinctrl-sunxi.c 34 KB

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