pinctrl-sunxi.c 43 KB

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