pinctrl-abx500.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2013
  3. *
  4. * Author: Patrice Chotard <patrice.chotard@st.com>
  5. * License terms: GNU General Public License (GPL) version 2
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/gpio.h>
  21. #include <linux/irq.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/bitops.h>
  25. #include <linux/mfd/abx500.h>
  26. #include <linux/mfd/abx500/ab8500.h>
  27. #include <linux/pinctrl/pinctrl.h>
  28. #include <linux/pinctrl/consumer.h>
  29. #include <linux/pinctrl/pinmux.h>
  30. #include <linux/pinctrl/pinconf.h>
  31. #include <linux/pinctrl/pinconf-generic.h>
  32. #include <linux/pinctrl/machine.h>
  33. #include "pinctrl-abx500.h"
  34. #include "../core.h"
  35. #include "../pinconf.h"
  36. #include "../pinctrl-utils.h"
  37. /*
  38. * The AB9540 and AB8540 GPIO support are extended versions
  39. * of the AB8500 GPIO support.
  40. * The AB9540 supports an additional (7th) register so that
  41. * more GPIO may be configured and used.
  42. * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have
  43. * internal pull-up and pull-down capabilities.
  44. */
  45. /*
  46. * GPIO registers offset
  47. * Bank: 0x10
  48. */
  49. #define AB8500_GPIO_SEL1_REG 0x00
  50. #define AB8500_GPIO_SEL2_REG 0x01
  51. #define AB8500_GPIO_SEL3_REG 0x02
  52. #define AB8500_GPIO_SEL4_REG 0x03
  53. #define AB8500_GPIO_SEL5_REG 0x04
  54. #define AB8500_GPIO_SEL6_REG 0x05
  55. #define AB9540_GPIO_SEL7_REG 0x06
  56. #define AB8500_GPIO_DIR1_REG 0x10
  57. #define AB8500_GPIO_DIR2_REG 0x11
  58. #define AB8500_GPIO_DIR3_REG 0x12
  59. #define AB8500_GPIO_DIR4_REG 0x13
  60. #define AB8500_GPIO_DIR5_REG 0x14
  61. #define AB8500_GPIO_DIR6_REG 0x15
  62. #define AB9540_GPIO_DIR7_REG 0x16
  63. #define AB8500_GPIO_OUT1_REG 0x20
  64. #define AB8500_GPIO_OUT2_REG 0x21
  65. #define AB8500_GPIO_OUT3_REG 0x22
  66. #define AB8500_GPIO_OUT4_REG 0x23
  67. #define AB8500_GPIO_OUT5_REG 0x24
  68. #define AB8500_GPIO_OUT6_REG 0x25
  69. #define AB9540_GPIO_OUT7_REG 0x26
  70. #define AB8500_GPIO_PUD1_REG 0x30
  71. #define AB8500_GPIO_PUD2_REG 0x31
  72. #define AB8500_GPIO_PUD3_REG 0x32
  73. #define AB8500_GPIO_PUD4_REG 0x33
  74. #define AB8500_GPIO_PUD5_REG 0x34
  75. #define AB8500_GPIO_PUD6_REG 0x35
  76. #define AB9540_GPIO_PUD7_REG 0x36
  77. #define AB8500_GPIO_IN1_REG 0x40
  78. #define AB8500_GPIO_IN2_REG 0x41
  79. #define AB8500_GPIO_IN3_REG 0x42
  80. #define AB8500_GPIO_IN4_REG 0x43
  81. #define AB8500_GPIO_IN5_REG 0x44
  82. #define AB8500_GPIO_IN6_REG 0x45
  83. #define AB9540_GPIO_IN7_REG 0x46
  84. #define AB8540_GPIO_VINSEL_REG 0x47
  85. #define AB8540_GPIO_PULL_UPDOWN_REG 0x48
  86. #define AB8500_GPIO_ALTFUN_REG 0x50
  87. #define AB8540_GPIO_PULL_UPDOWN_MASK 0x03
  88. #define AB8540_GPIO_VINSEL_MASK 0x03
  89. #define AB8540_GPIOX_VBAT_START 51
  90. #define AB8540_GPIOX_VBAT_END 54
  91. #define ABX500_GPIO_INPUT 0
  92. #define ABX500_GPIO_OUTPUT 1
  93. struct abx500_pinctrl {
  94. struct device *dev;
  95. struct pinctrl_dev *pctldev;
  96. struct abx500_pinctrl_soc_data *soc;
  97. struct gpio_chip chip;
  98. struct ab8500 *parent;
  99. struct abx500_gpio_irq_cluster *irq_cluster;
  100. int irq_cluster_size;
  101. };
  102. /**
  103. * to_abx500_pinctrl() - get the pointer to abx500_pinctrl
  104. * @chip: Member of the structure abx500_pinctrl
  105. */
  106. static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip)
  107. {
  108. return container_of(chip, struct abx500_pinctrl, chip);
  109. }
  110. static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
  111. unsigned offset, bool *bit)
  112. {
  113. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  114. u8 pos = offset % 8;
  115. u8 val;
  116. int ret;
  117. reg += offset / 8;
  118. ret = abx500_get_register_interruptible(pct->dev,
  119. AB8500_MISC, reg, &val);
  120. *bit = !!(val & BIT(pos));
  121. if (ret < 0)
  122. dev_err(pct->dev,
  123. "%s read reg =%x, offset=%x failed (%d)\n",
  124. __func__, reg, offset, ret);
  125. return ret;
  126. }
  127. static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
  128. unsigned offset, int val)
  129. {
  130. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  131. u8 pos = offset % 8;
  132. int ret;
  133. reg += offset / 8;
  134. ret = abx500_mask_and_set_register_interruptible(pct->dev,
  135. AB8500_MISC, reg, BIT(pos), val << pos);
  136. if (ret < 0)
  137. dev_err(pct->dev, "%s write reg, %x offset %x failed (%d)\n",
  138. __func__, reg, offset, ret);
  139. return ret;
  140. }
  141. /**
  142. * abx500_gpio_get() - Get the particular GPIO value
  143. * @chip: Gpio device
  144. * @offset: GPIO number to read
  145. */
  146. static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
  147. {
  148. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  149. bool bit;
  150. bool is_out;
  151. u8 gpio_offset = offset - 1;
  152. int ret;
  153. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
  154. gpio_offset, &is_out);
  155. if (ret < 0)
  156. goto out;
  157. if (is_out)
  158. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_OUT1_REG,
  159. gpio_offset, &bit);
  160. else
  161. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
  162. gpio_offset, &bit);
  163. out:
  164. if (ret < 0) {
  165. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  166. return ret;
  167. }
  168. return bit;
  169. }
  170. static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  171. {
  172. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  173. int ret;
  174. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
  175. if (ret < 0)
  176. dev_err(pct->dev, "%s write failed (%d)\n", __func__, ret);
  177. }
  178. static int abx500_get_pull_updown(struct abx500_pinctrl *pct, int offset,
  179. enum abx500_gpio_pull_updown *pull_updown)
  180. {
  181. u8 pos;
  182. u8 val;
  183. int ret;
  184. struct pullud *pullud;
  185. if (!pct->soc->pullud) {
  186. dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
  187. __func__);
  188. ret = -EPERM;
  189. goto out;
  190. }
  191. pullud = pct->soc->pullud;
  192. if ((offset < pullud->first_pin)
  193. || (offset > pullud->last_pin)) {
  194. ret = -EINVAL;
  195. goto out;
  196. }
  197. ret = abx500_get_register_interruptible(pct->dev,
  198. AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG, &val);
  199. pos = (offset - pullud->first_pin) << 1;
  200. *pull_updown = (val >> pos) & AB8540_GPIO_PULL_UPDOWN_MASK;
  201. out:
  202. if (ret < 0)
  203. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  204. return ret;
  205. }
  206. static int abx500_set_pull_updown(struct abx500_pinctrl *pct,
  207. int offset, enum abx500_gpio_pull_updown val)
  208. {
  209. u8 pos;
  210. int ret;
  211. struct pullud *pullud;
  212. if (!pct->soc->pullud) {
  213. dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
  214. __func__);
  215. ret = -EPERM;
  216. goto out;
  217. }
  218. pullud = pct->soc->pullud;
  219. if ((offset < pullud->first_pin)
  220. || (offset > pullud->last_pin)) {
  221. ret = -EINVAL;
  222. goto out;
  223. }
  224. pos = (offset - pullud->first_pin) << 1;
  225. ret = abx500_mask_and_set_register_interruptible(pct->dev,
  226. AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
  227. AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
  228. out:
  229. if (ret < 0)
  230. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  231. return ret;
  232. }
  233. static bool abx500_pullud_supported(struct gpio_chip *chip, unsigned gpio)
  234. {
  235. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  236. struct pullud *pullud = pct->soc->pullud;
  237. return (pullud &&
  238. gpio >= pullud->first_pin &&
  239. gpio <= pullud->last_pin);
  240. }
  241. static int abx500_gpio_direction_output(struct gpio_chip *chip,
  242. unsigned offset,
  243. int val)
  244. {
  245. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  246. unsigned gpio;
  247. int ret;
  248. /* set direction as output */
  249. ret = abx500_gpio_set_bits(chip,
  250. AB8500_GPIO_DIR1_REG,
  251. offset,
  252. ABX500_GPIO_OUTPUT);
  253. if (ret < 0)
  254. goto out;
  255. /* disable pull down */
  256. ret = abx500_gpio_set_bits(chip,
  257. AB8500_GPIO_PUD1_REG,
  258. offset,
  259. ABX500_GPIO_PULL_NONE);
  260. if (ret < 0)
  261. goto out;
  262. /* if supported, disable both pull down and pull up */
  263. gpio = offset + 1;
  264. if (abx500_pullud_supported(chip, gpio)) {
  265. ret = abx500_set_pull_updown(pct,
  266. gpio,
  267. ABX500_GPIO_PULL_NONE);
  268. }
  269. out:
  270. if (ret < 0) {
  271. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  272. return ret;
  273. }
  274. /* set the output as 1 or 0 */
  275. return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
  276. }
  277. static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  278. {
  279. /* set the register as input */
  280. return abx500_gpio_set_bits(chip,
  281. AB8500_GPIO_DIR1_REG,
  282. offset,
  283. ABX500_GPIO_INPUT);
  284. }
  285. static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  286. {
  287. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  288. /* The AB8500 GPIO numbers are off by one */
  289. int gpio = offset + 1;
  290. int hwirq;
  291. int i;
  292. for (i = 0; i < pct->irq_cluster_size; i++) {
  293. struct abx500_gpio_irq_cluster *cluster =
  294. &pct->irq_cluster[i];
  295. if (gpio >= cluster->start && gpio <= cluster->end) {
  296. /*
  297. * The ABx500 GPIO's associated IRQs are clustered together
  298. * throughout the interrupt numbers at irregular intervals.
  299. * To solve this quandry, we have placed the read-in values
  300. * into the cluster information table.
  301. */
  302. hwirq = gpio - cluster->start + cluster->to_irq;
  303. return irq_create_mapping(pct->parent->domain, hwirq);
  304. }
  305. }
  306. return -EINVAL;
  307. }
  308. static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
  309. unsigned gpio, int alt_setting)
  310. {
  311. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  312. struct alternate_functions af = pct->soc->alternate_functions[gpio];
  313. int ret;
  314. int val;
  315. unsigned offset;
  316. const char *modes[] = {
  317. [ABX500_DEFAULT] = "default",
  318. [ABX500_ALT_A] = "altA",
  319. [ABX500_ALT_B] = "altB",
  320. [ABX500_ALT_C] = "altC",
  321. };
  322. /* sanity check */
  323. if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
  324. ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
  325. ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
  326. dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
  327. modes[alt_setting]);
  328. return -EINVAL;
  329. }
  330. /* on ABx5xx, there is no GPIO0, so adjust the offset */
  331. offset = gpio - 1;
  332. switch (alt_setting) {
  333. case ABX500_DEFAULT:
  334. /*
  335. * for ABx5xx family, default mode is always selected by
  336. * writing 0 to GPIOSELx register, except for pins which
  337. * support at least ALT_B mode, default mode is selected
  338. * by writing 1 to GPIOSELx register
  339. */
  340. val = 0;
  341. if (af.alt_bit1 != UNUSED)
  342. val++;
  343. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  344. offset, val);
  345. break;
  346. case ABX500_ALT_A:
  347. /*
  348. * for ABx5xx family, alt_a mode is always selected by
  349. * writing 1 to GPIOSELx register, except for pins which
  350. * support at least ALT_B mode, alt_a mode is selected
  351. * by writing 0 to GPIOSELx register and 0 in ALTFUNC
  352. * register
  353. */
  354. if (af.alt_bit1 != UNUSED) {
  355. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  356. offset, 0);
  357. if (ret < 0)
  358. goto out;
  359. ret = abx500_gpio_set_bits(chip,
  360. AB8500_GPIO_ALTFUN_REG,
  361. af.alt_bit1,
  362. !!(af.alta_val & BIT(0)));
  363. if (ret < 0)
  364. goto out;
  365. if (af.alt_bit2 != UNUSED)
  366. ret = abx500_gpio_set_bits(chip,
  367. AB8500_GPIO_ALTFUN_REG,
  368. af.alt_bit2,
  369. !!(af.alta_val & BIT(1)));
  370. } else
  371. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  372. offset, 1);
  373. break;
  374. case ABX500_ALT_B:
  375. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  376. offset, 0);
  377. if (ret < 0)
  378. goto out;
  379. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
  380. af.alt_bit1, !!(af.altb_val & BIT(0)));
  381. if (ret < 0)
  382. goto out;
  383. if (af.alt_bit2 != UNUSED)
  384. ret = abx500_gpio_set_bits(chip,
  385. AB8500_GPIO_ALTFUN_REG,
  386. af.alt_bit2,
  387. !!(af.altb_val & BIT(1)));
  388. break;
  389. case ABX500_ALT_C:
  390. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  391. offset, 0);
  392. if (ret < 0)
  393. goto out;
  394. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
  395. af.alt_bit2, !!(af.altc_val & BIT(0)));
  396. if (ret < 0)
  397. goto out;
  398. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
  399. af.alt_bit2, !!(af.altc_val & BIT(1)));
  400. break;
  401. default:
  402. dev_dbg(pct->dev, "unknown alt_setting %d\n", alt_setting);
  403. return -EINVAL;
  404. }
  405. out:
  406. if (ret < 0)
  407. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  408. return ret;
  409. }
  410. static int abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
  411. unsigned gpio)
  412. {
  413. u8 mode;
  414. bool bit_mode;
  415. bool alt_bit1;
  416. bool alt_bit2;
  417. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  418. struct alternate_functions af = pct->soc->alternate_functions[gpio];
  419. /* on ABx5xx, there is no GPIO0, so adjust the offset */
  420. unsigned offset = gpio - 1;
  421. int ret;
  422. /*
  423. * if gpiosel_bit is set to unused,
  424. * it means no GPIO or special case
  425. */
  426. if (af.gpiosel_bit == UNUSED)
  427. return ABX500_DEFAULT;
  428. /* read GpioSelx register */
  429. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
  430. af.gpiosel_bit, &bit_mode);
  431. if (ret < 0)
  432. goto out;
  433. mode = bit_mode;
  434. /* sanity check */
  435. if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
  436. (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
  437. dev_err(pct->dev,
  438. "alt_bitX value not in correct range (-1 to 7)\n");
  439. return -EINVAL;
  440. }
  441. /* if alt_bit2 is used, alt_bit1 must be used too */
  442. if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
  443. dev_err(pct->dev,
  444. "if alt_bit2 is used, alt_bit1 can't be unused\n");
  445. return -EINVAL;
  446. }
  447. /* check if pin use AlternateFunction register */
  448. if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
  449. return mode;
  450. /*
  451. * if pin GPIOSEL bit is set and pin supports alternate function,
  452. * it means DEFAULT mode
  453. */
  454. if (mode)
  455. return ABX500_DEFAULT;
  456. /*
  457. * pin use the AlternatFunction register
  458. * read alt_bit1 value
  459. */
  460. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
  461. af.alt_bit1, &alt_bit1);
  462. if (ret < 0)
  463. goto out;
  464. if (af.alt_bit2 != UNUSED) {
  465. /* read alt_bit2 value */
  466. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
  467. af.alt_bit2,
  468. &alt_bit2);
  469. if (ret < 0)
  470. goto out;
  471. } else
  472. alt_bit2 = 0;
  473. mode = (alt_bit2 << 1) + alt_bit1;
  474. if (mode == af.alta_val)
  475. return ABX500_ALT_A;
  476. else if (mode == af.altb_val)
  477. return ABX500_ALT_B;
  478. else
  479. return ABX500_ALT_C;
  480. out:
  481. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  482. return ret;
  483. }
  484. #ifdef CONFIG_DEBUG_FS
  485. #include <linux/seq_file.h>
  486. static void abx500_gpio_dbg_show_one(struct seq_file *s,
  487. struct pinctrl_dev *pctldev,
  488. struct gpio_chip *chip,
  489. unsigned offset, unsigned gpio)
  490. {
  491. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  492. const char *label = gpiochip_is_requested(chip, offset - 1);
  493. u8 gpio_offset = offset - 1;
  494. int mode = -1;
  495. bool is_out;
  496. bool pd;
  497. enum abx500_gpio_pull_updown pud = 0;
  498. int ret;
  499. const char *modes[] = {
  500. [ABX500_DEFAULT] = "default",
  501. [ABX500_ALT_A] = "altA",
  502. [ABX500_ALT_B] = "altB",
  503. [ABX500_ALT_C] = "altC",
  504. };
  505. const char *pull_up_down[] = {
  506. [ABX500_GPIO_PULL_DOWN] = "pull down",
  507. [ABX500_GPIO_PULL_NONE] = "pull none",
  508. [ABX500_GPIO_PULL_NONE + 1] = "pull none",
  509. [ABX500_GPIO_PULL_UP] = "pull up",
  510. };
  511. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
  512. gpio_offset, &is_out);
  513. if (ret < 0)
  514. goto out;
  515. seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
  516. gpio, label ?: "(none)",
  517. is_out ? "out" : "in ");
  518. if (!is_out) {
  519. if (abx500_pullud_supported(chip, offset)) {
  520. ret = abx500_get_pull_updown(pct, offset, &pud);
  521. if (ret < 0)
  522. goto out;
  523. seq_printf(s, " %-9s", pull_up_down[pud]);
  524. } else {
  525. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
  526. gpio_offset, &pd);
  527. if (ret < 0)
  528. goto out;
  529. seq_printf(s, " %-9s", pull_up_down[pd]);
  530. }
  531. } else
  532. seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
  533. mode = abx500_get_mode(pctldev, chip, offset);
  534. seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
  535. out:
  536. if (ret < 0)
  537. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  538. }
  539. static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  540. {
  541. unsigned i;
  542. unsigned gpio = chip->base;
  543. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  544. struct pinctrl_dev *pctldev = pct->pctldev;
  545. for (i = 0; i < chip->ngpio; i++, gpio++) {
  546. /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
  547. abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
  548. seq_printf(s, "\n");
  549. }
  550. }
  551. #else
  552. static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
  553. struct pinctrl_dev *pctldev,
  554. struct gpio_chip *chip,
  555. unsigned offset, unsigned gpio)
  556. {
  557. }
  558. #define abx500_gpio_dbg_show NULL
  559. #endif
  560. static struct gpio_chip abx500gpio_chip = {
  561. .label = "abx500-gpio",
  562. .owner = THIS_MODULE,
  563. .request = gpiochip_generic_request,
  564. .free = gpiochip_generic_free,
  565. .direction_input = abx500_gpio_direction_input,
  566. .get = abx500_gpio_get,
  567. .direction_output = abx500_gpio_direction_output,
  568. .set = abx500_gpio_set,
  569. .to_irq = abx500_gpio_to_irq,
  570. .dbg_show = abx500_gpio_dbg_show,
  571. };
  572. static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  573. {
  574. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  575. return pct->soc->nfunctions;
  576. }
  577. static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
  578. unsigned function)
  579. {
  580. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  581. return pct->soc->functions[function].name;
  582. }
  583. static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  584. unsigned function,
  585. const char * const **groups,
  586. unsigned * const num_groups)
  587. {
  588. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  589. *groups = pct->soc->functions[function].groups;
  590. *num_groups = pct->soc->functions[function].ngroups;
  591. return 0;
  592. }
  593. static int abx500_pmx_set(struct pinctrl_dev *pctldev, unsigned function,
  594. unsigned group)
  595. {
  596. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  597. struct gpio_chip *chip = &pct->chip;
  598. const struct abx500_pingroup *g;
  599. int i;
  600. int ret = 0;
  601. g = &pct->soc->groups[group];
  602. if (g->altsetting < 0)
  603. return -EINVAL;
  604. dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
  605. for (i = 0; i < g->npins; i++) {
  606. dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
  607. g->pins[i], g->altsetting);
  608. ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
  609. }
  610. if (ret < 0)
  611. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  612. return ret;
  613. }
  614. static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
  615. struct pinctrl_gpio_range *range,
  616. unsigned offset)
  617. {
  618. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  619. const struct abx500_pinrange *p;
  620. int ret;
  621. int i;
  622. /*
  623. * Different ranges have different ways to enable GPIO function on a
  624. * pin, so refer back to our local range type, where we handily define
  625. * what altfunc enables GPIO for a certain pin.
  626. */
  627. for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
  628. p = &pct->soc->gpio_ranges[i];
  629. if ((offset >= p->offset) &&
  630. (offset < (p->offset + p->npins)))
  631. break;
  632. }
  633. if (i == pct->soc->gpio_num_ranges) {
  634. dev_err(pct->dev, "%s failed to locate range\n", __func__);
  635. return -ENODEV;
  636. }
  637. dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
  638. p->altfunc, offset);
  639. ret = abx500_set_mode(pct->pctldev, &pct->chip,
  640. offset, p->altfunc);
  641. if (ret < 0)
  642. dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
  643. return ret;
  644. }
  645. static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
  646. struct pinctrl_gpio_range *range,
  647. unsigned offset)
  648. {
  649. }
  650. static const struct pinmux_ops abx500_pinmux_ops = {
  651. .get_functions_count = abx500_pmx_get_funcs_cnt,
  652. .get_function_name = abx500_pmx_get_func_name,
  653. .get_function_groups = abx500_pmx_get_func_groups,
  654. .set_mux = abx500_pmx_set,
  655. .gpio_request_enable = abx500_gpio_request_enable,
  656. .gpio_disable_free = abx500_gpio_disable_free,
  657. };
  658. static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
  659. {
  660. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  661. return pct->soc->ngroups;
  662. }
  663. static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
  664. unsigned selector)
  665. {
  666. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  667. return pct->soc->groups[selector].name;
  668. }
  669. static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
  670. unsigned selector,
  671. const unsigned **pins,
  672. unsigned *num_pins)
  673. {
  674. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  675. *pins = pct->soc->groups[selector].pins;
  676. *num_pins = pct->soc->groups[selector].npins;
  677. return 0;
  678. }
  679. static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
  680. struct seq_file *s, unsigned offset)
  681. {
  682. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  683. struct gpio_chip *chip = &pct->chip;
  684. abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
  685. chip->base + offset - 1);
  686. }
  687. static int abx500_dt_add_map_mux(struct pinctrl_map **map,
  688. unsigned *reserved_maps,
  689. unsigned *num_maps, const char *group,
  690. const char *function)
  691. {
  692. if (*num_maps == *reserved_maps)
  693. return -ENOSPC;
  694. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  695. (*map)[*num_maps].data.mux.group = group;
  696. (*map)[*num_maps].data.mux.function = function;
  697. (*num_maps)++;
  698. return 0;
  699. }
  700. static int abx500_dt_add_map_configs(struct pinctrl_map **map,
  701. unsigned *reserved_maps,
  702. unsigned *num_maps, const char *group,
  703. unsigned long *configs, unsigned num_configs)
  704. {
  705. unsigned long *dup_configs;
  706. if (*num_maps == *reserved_maps)
  707. return -ENOSPC;
  708. dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
  709. GFP_KERNEL);
  710. if (!dup_configs)
  711. return -ENOMEM;
  712. (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
  713. (*map)[*num_maps].data.configs.group_or_pin = group;
  714. (*map)[*num_maps].data.configs.configs = dup_configs;
  715. (*map)[*num_maps].data.configs.num_configs = num_configs;
  716. (*num_maps)++;
  717. return 0;
  718. }
  719. static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
  720. const char *pin_name)
  721. {
  722. int i, pin_number;
  723. struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  724. if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
  725. for (i = 0; i < npct->soc->npins; i++)
  726. if (npct->soc->pins[i].number == pin_number)
  727. return npct->soc->pins[i].name;
  728. return NULL;
  729. }
  730. static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  731. struct device_node *np,
  732. struct pinctrl_map **map,
  733. unsigned *reserved_maps,
  734. unsigned *num_maps)
  735. {
  736. int ret;
  737. const char *function = NULL;
  738. unsigned long *configs;
  739. unsigned int nconfigs = 0;
  740. struct property *prop;
  741. ret = of_property_read_string(np, "function", &function);
  742. if (ret >= 0) {
  743. const char *group;
  744. ret = of_property_count_strings(np, "groups");
  745. if (ret < 0)
  746. goto exit;
  747. ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps,
  748. num_maps, ret);
  749. if (ret < 0)
  750. goto exit;
  751. of_property_for_each_string(np, "groups", prop, group) {
  752. ret = abx500_dt_add_map_mux(map, reserved_maps,
  753. num_maps, group, function);
  754. if (ret < 0)
  755. goto exit;
  756. }
  757. }
  758. ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &nconfigs);
  759. if (nconfigs) {
  760. const char *gpio_name;
  761. const char *pin;
  762. ret = of_property_count_strings(np, "pins");
  763. if (ret < 0)
  764. goto exit;
  765. ret = pinctrl_utils_reserve_map(pctldev, map,
  766. reserved_maps,
  767. num_maps, ret);
  768. if (ret < 0)
  769. goto exit;
  770. of_property_for_each_string(np, "pins", prop, pin) {
  771. gpio_name = abx500_find_pin_name(pctldev, pin);
  772. ret = abx500_dt_add_map_configs(map, reserved_maps,
  773. num_maps, gpio_name, configs, 1);
  774. if (ret < 0)
  775. goto exit;
  776. }
  777. }
  778. exit:
  779. return ret;
  780. }
  781. static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
  782. struct device_node *np_config,
  783. struct pinctrl_map **map, unsigned *num_maps)
  784. {
  785. unsigned reserved_maps;
  786. struct device_node *np;
  787. int ret;
  788. reserved_maps = 0;
  789. *map = NULL;
  790. *num_maps = 0;
  791. for_each_child_of_node(np_config, np) {
  792. ret = abx500_dt_subnode_to_map(pctldev, np, map,
  793. &reserved_maps, num_maps);
  794. if (ret < 0) {
  795. pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
  796. return ret;
  797. }
  798. }
  799. return 0;
  800. }
  801. static const struct pinctrl_ops abx500_pinctrl_ops = {
  802. .get_groups_count = abx500_get_groups_cnt,
  803. .get_group_name = abx500_get_group_name,
  804. .get_group_pins = abx500_get_group_pins,
  805. .pin_dbg_show = abx500_pin_dbg_show,
  806. .dt_node_to_map = abx500_dt_node_to_map,
  807. .dt_free_map = pinctrl_utils_dt_free_map,
  808. };
  809. static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
  810. unsigned pin,
  811. unsigned long *config)
  812. {
  813. return -ENOSYS;
  814. }
  815. static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
  816. unsigned pin,
  817. unsigned long *configs,
  818. unsigned num_configs)
  819. {
  820. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  821. struct gpio_chip *chip = &pct->chip;
  822. unsigned offset;
  823. int ret = -EINVAL;
  824. int i;
  825. enum pin_config_param param;
  826. enum pin_config_param argument;
  827. for (i = 0; i < num_configs; i++) {
  828. param = pinconf_to_config_param(configs[i]);
  829. argument = pinconf_to_config_argument(configs[i]);
  830. dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
  831. pin, configs[i],
  832. (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
  833. (param == PIN_CONFIG_OUTPUT) ?
  834. (argument ? "high" : "low") :
  835. (argument ? "pull up" : "pull down"));
  836. /* on ABx500, there is no GPIO0, so adjust the offset */
  837. offset = pin - 1;
  838. switch (param) {
  839. case PIN_CONFIG_BIAS_DISABLE:
  840. ret = abx500_gpio_direction_input(chip, offset);
  841. if (ret < 0)
  842. goto out;
  843. /*
  844. * Some chips only support pull down, while some
  845. * actually support both pull up and pull down. Such
  846. * chips have a "pullud" range specified for the pins
  847. * that support both features. If the pin is not
  848. * within that range, we fall back to the old bit set
  849. * that only support pull down.
  850. */
  851. if (abx500_pullud_supported(chip, pin))
  852. ret = abx500_set_pull_updown(pct,
  853. pin,
  854. ABX500_GPIO_PULL_NONE);
  855. else
  856. /* Chip only supports pull down */
  857. ret = abx500_gpio_set_bits(chip,
  858. AB8500_GPIO_PUD1_REG, offset,
  859. ABX500_GPIO_PULL_NONE);
  860. break;
  861. case PIN_CONFIG_BIAS_PULL_DOWN:
  862. ret = abx500_gpio_direction_input(chip, offset);
  863. if (ret < 0)
  864. goto out;
  865. /*
  866. * if argument = 1 set the pull down
  867. * else clear the pull down
  868. * Some chips only support pull down, while some
  869. * actually support both pull up and pull down. Such
  870. * chips have a "pullud" range specified for the pins
  871. * that support both features. If the pin is not
  872. * within that range, we fall back to the old bit set
  873. * that only support pull down.
  874. */
  875. if (abx500_pullud_supported(chip, pin))
  876. ret = abx500_set_pull_updown(pct,
  877. pin,
  878. argument ? ABX500_GPIO_PULL_DOWN :
  879. ABX500_GPIO_PULL_NONE);
  880. else
  881. /* Chip only supports pull down */
  882. ret = abx500_gpio_set_bits(chip,
  883. AB8500_GPIO_PUD1_REG,
  884. offset,
  885. argument ? ABX500_GPIO_PULL_DOWN :
  886. ABX500_GPIO_PULL_NONE);
  887. break;
  888. case PIN_CONFIG_BIAS_PULL_UP:
  889. ret = abx500_gpio_direction_input(chip, offset);
  890. if (ret < 0)
  891. goto out;
  892. /*
  893. * if argument = 1 set the pull up
  894. * else clear the pull up
  895. */
  896. ret = abx500_gpio_direction_input(chip, offset);
  897. /*
  898. * Some chips only support pull down, while some
  899. * actually support both pull up and pull down. Such
  900. * chips have a "pullud" range specified for the pins
  901. * that support both features. If the pin is not
  902. * within that range, do nothing
  903. */
  904. if (abx500_pullud_supported(chip, pin))
  905. ret = abx500_set_pull_updown(pct,
  906. pin,
  907. argument ? ABX500_GPIO_PULL_UP :
  908. ABX500_GPIO_PULL_NONE);
  909. break;
  910. case PIN_CONFIG_OUTPUT:
  911. ret = abx500_gpio_direction_output(chip, offset,
  912. argument);
  913. break;
  914. default:
  915. dev_err(chip->dev, "illegal configuration requested\n");
  916. }
  917. } /* for each config */
  918. out:
  919. if (ret < 0)
  920. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  921. return ret;
  922. }
  923. static const struct pinconf_ops abx500_pinconf_ops = {
  924. .pin_config_get = abx500_pin_config_get,
  925. .pin_config_set = abx500_pin_config_set,
  926. .is_generic = true,
  927. };
  928. static struct pinctrl_desc abx500_pinctrl_desc = {
  929. .name = "pinctrl-abx500",
  930. .pctlops = &abx500_pinctrl_ops,
  931. .pmxops = &abx500_pinmux_ops,
  932. .confops = &abx500_pinconf_ops,
  933. .owner = THIS_MODULE,
  934. };
  935. static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
  936. {
  937. unsigned int lowest = 0;
  938. unsigned int highest = 0;
  939. unsigned int npins = 0;
  940. int i;
  941. /*
  942. * Compute number of GPIOs from the last SoC gpio range descriptors
  943. * These ranges may include "holes" but the GPIO number space shall
  944. * still be homogeneous, so we need to detect and account for any
  945. * such holes so that these are included in the number of GPIO pins.
  946. */
  947. for (i = 0; i < soc->gpio_num_ranges; i++) {
  948. unsigned gstart;
  949. unsigned gend;
  950. const struct abx500_pinrange *p;
  951. p = &soc->gpio_ranges[i];
  952. gstart = p->offset;
  953. gend = p->offset + p->npins - 1;
  954. if (i == 0) {
  955. /* First iteration, set start values */
  956. lowest = gstart;
  957. highest = gend;
  958. } else {
  959. if (gstart < lowest)
  960. lowest = gstart;
  961. if (gend > highest)
  962. highest = gend;
  963. }
  964. }
  965. /* this gives the absolute number of pins */
  966. npins = highest - lowest + 1;
  967. return npins;
  968. }
  969. static const struct of_device_id abx500_gpio_match[] = {
  970. { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
  971. { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
  972. { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
  973. { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
  974. { }
  975. };
  976. static int abx500_gpio_probe(struct platform_device *pdev)
  977. {
  978. struct device_node *np = pdev->dev.of_node;
  979. const struct of_device_id *match;
  980. struct abx500_pinctrl *pct;
  981. unsigned int id = -1;
  982. int ret;
  983. int i;
  984. if (!np) {
  985. dev_err(&pdev->dev, "gpio dt node missing\n");
  986. return -ENODEV;
  987. }
  988. pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
  989. GFP_KERNEL);
  990. if (pct == NULL) {
  991. dev_err(&pdev->dev,
  992. "failed to allocate memory for pct\n");
  993. return -ENOMEM;
  994. }
  995. pct->dev = &pdev->dev;
  996. pct->parent = dev_get_drvdata(pdev->dev.parent);
  997. pct->chip = abx500gpio_chip;
  998. pct->chip.dev = &pdev->dev;
  999. pct->chip.base = -1; /* Dynamic allocation */
  1000. match = of_match_device(abx500_gpio_match, &pdev->dev);
  1001. if (!match) {
  1002. dev_err(&pdev->dev, "gpio dt not matching\n");
  1003. return -ENODEV;
  1004. }
  1005. id = (unsigned long)match->data;
  1006. /* Poke in other ASIC variants here */
  1007. switch (id) {
  1008. case PINCTRL_AB8500:
  1009. abx500_pinctrl_ab8500_init(&pct->soc);
  1010. break;
  1011. case PINCTRL_AB8540:
  1012. abx500_pinctrl_ab8540_init(&pct->soc);
  1013. break;
  1014. case PINCTRL_AB9540:
  1015. abx500_pinctrl_ab9540_init(&pct->soc);
  1016. break;
  1017. case PINCTRL_AB8505:
  1018. abx500_pinctrl_ab8505_init(&pct->soc);
  1019. break;
  1020. default:
  1021. dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
  1022. return -EINVAL;
  1023. }
  1024. if (!pct->soc) {
  1025. dev_err(&pdev->dev, "Invalid SOC data\n");
  1026. return -EINVAL;
  1027. }
  1028. pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
  1029. pct->irq_cluster = pct->soc->gpio_irq_cluster;
  1030. pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
  1031. ret = gpiochip_add(&pct->chip);
  1032. if (ret) {
  1033. dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
  1034. return ret;
  1035. }
  1036. dev_info(&pdev->dev, "added gpiochip\n");
  1037. abx500_pinctrl_desc.pins = pct->soc->pins;
  1038. abx500_pinctrl_desc.npins = pct->soc->npins;
  1039. pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
  1040. if (IS_ERR(pct->pctldev)) {
  1041. dev_err(&pdev->dev,
  1042. "could not register abx500 pinctrl driver\n");
  1043. ret = PTR_ERR(pct->pctldev);
  1044. goto out_rem_chip;
  1045. }
  1046. dev_info(&pdev->dev, "registered pin controller\n");
  1047. /* We will handle a range of GPIO pins */
  1048. for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
  1049. const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
  1050. ret = gpiochip_add_pin_range(&pct->chip,
  1051. dev_name(&pdev->dev),
  1052. p->offset - 1, p->offset, p->npins);
  1053. if (ret < 0)
  1054. goto out_rem_chip;
  1055. }
  1056. platform_set_drvdata(pdev, pct);
  1057. dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
  1058. return 0;
  1059. out_rem_chip:
  1060. gpiochip_remove(&pct->chip);
  1061. return ret;
  1062. }
  1063. /**
  1064. * abx500_gpio_remove() - remove Ab8500-gpio driver
  1065. * @pdev: Platform device registered
  1066. */
  1067. static int abx500_gpio_remove(struct platform_device *pdev)
  1068. {
  1069. struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
  1070. gpiochip_remove(&pct->chip);
  1071. return 0;
  1072. }
  1073. static struct platform_driver abx500_gpio_driver = {
  1074. .driver = {
  1075. .name = "abx500-gpio",
  1076. .of_match_table = abx500_gpio_match,
  1077. },
  1078. .probe = abx500_gpio_probe,
  1079. .remove = abx500_gpio_remove,
  1080. };
  1081. static int __init abx500_gpio_init(void)
  1082. {
  1083. return platform_driver_register(&abx500_gpio_driver);
  1084. }
  1085. core_initcall(abx500_gpio_init);
  1086. MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
  1087. MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
  1088. MODULE_ALIAS("platform:abx500-gpio");
  1089. MODULE_LICENSE("GPL v2");