pinctrl-sunxi.c 41 KB

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