pinctrl-sunxi.c 41 KB

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