pinctrl-abx500.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  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/mfd/abx500/ab8500-gpio.h>
  28. #include <linux/pinctrl/pinctrl.h>
  29. #include <linux/pinctrl/consumer.h>
  30. #include <linux/pinctrl/pinmux.h>
  31. #include <linux/pinctrl/pinconf.h>
  32. #include <linux/pinctrl/pinconf-generic.h>
  33. #include <linux/pinctrl/machine.h>
  34. #include "pinctrl-abx500.h"
  35. #include "core.h"
  36. #include "pinconf.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\n",
  124. __func__, reg, offset);
  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 failed\n", __func__);
  138. return ret;
  139. }
  140. /**
  141. * abx500_gpio_get() - Get the particular GPIO value
  142. * @chip: Gpio device
  143. * @offset: GPIO number to read
  144. */
  145. static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
  146. {
  147. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  148. bool bit;
  149. bool is_out;
  150. u8 gpio_offset = offset - 1;
  151. int ret;
  152. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG,
  153. gpio_offset, &is_out);
  154. if (ret < 0) {
  155. dev_err(pct->dev, "%s failed\n", __func__);
  156. return ret;
  157. }
  158. if (is_out)
  159. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_OUT1_REG,
  160. gpio_offset, &bit);
  161. else
  162. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
  163. gpio_offset, &bit);
  164. if (ret < 0) {
  165. dev_err(pct->dev, "%s failed\n", __func__);
  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\n", __func__);
  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. return ret;
  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. return ret;
  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. if (ret < 0)
  269. return ret;
  270. }
  271. /* set the output as 1 or 0 */
  272. return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
  273. }
  274. static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  275. {
  276. /* set the register as input */
  277. return abx500_gpio_set_bits(chip,
  278. AB8500_GPIO_DIR1_REG,
  279. offset,
  280. ABX500_GPIO_INPUT);
  281. }
  282. static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  283. {
  284. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  285. /* The AB8500 GPIO numbers are off by one */
  286. int gpio = offset + 1;
  287. int hwirq;
  288. int i;
  289. for (i = 0; i < pct->irq_cluster_size; i++) {
  290. struct abx500_gpio_irq_cluster *cluster =
  291. &pct->irq_cluster[i];
  292. if (gpio >= cluster->start && gpio <= cluster->end) {
  293. /*
  294. * The ABx500 GPIO's associated IRQs are clustered together
  295. * throughout the interrupt numbers at irregular intervals.
  296. * To solve this quandry, we have placed the read-in values
  297. * into the cluster information table.
  298. */
  299. hwirq = gpio - cluster->start + cluster->to_irq;
  300. return irq_create_mapping(pct->parent->domain, hwirq);
  301. }
  302. }
  303. return -EINVAL;
  304. }
  305. static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
  306. unsigned gpio, int alt_setting)
  307. {
  308. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  309. struct alternate_functions af = pct->soc->alternate_functions[gpio];
  310. int ret;
  311. int val;
  312. unsigned offset;
  313. const char *modes[] = {
  314. [ABX500_DEFAULT] = "default",
  315. [ABX500_ALT_A] = "altA",
  316. [ABX500_ALT_B] = "altB",
  317. [ABX500_ALT_C] = "altC",
  318. };
  319. /* sanity check */
  320. if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
  321. ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
  322. ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
  323. dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
  324. modes[alt_setting]);
  325. return -EINVAL;
  326. }
  327. /* on ABx5xx, there is no GPIO0, so adjust the offset */
  328. offset = gpio - 1;
  329. switch (alt_setting) {
  330. case ABX500_DEFAULT:
  331. /*
  332. * for ABx5xx family, default mode is always selected by
  333. * writing 0 to GPIOSELx register, except for pins which
  334. * support at least ALT_B mode, default mode is selected
  335. * by writing 1 to GPIOSELx register
  336. */
  337. val = 0;
  338. if (af.alt_bit1 != UNUSED)
  339. val++;
  340. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  341. offset, val);
  342. break;
  343. case ABX500_ALT_A:
  344. /*
  345. * for ABx5xx family, alt_a mode is always selected by
  346. * writing 1 to GPIOSELx register, except for pins which
  347. * support at least ALT_B mode, alt_a mode is selected
  348. * by writing 0 to GPIOSELx register and 0 in ALTFUNC
  349. * register
  350. */
  351. if (af.alt_bit1 != UNUSED) {
  352. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  353. offset, 0);
  354. ret = abx500_gpio_set_bits(chip,
  355. AB8500_GPIO_ALTFUN_REG,
  356. af.alt_bit1,
  357. !!(af.alta_val && BIT(0)));
  358. if (af.alt_bit2 != UNUSED)
  359. ret = abx500_gpio_set_bits(chip,
  360. AB8500_GPIO_ALTFUN_REG,
  361. af.alt_bit2,
  362. !!(af.alta_val && BIT(1)));
  363. } else
  364. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  365. offset, 1);
  366. break;
  367. case ABX500_ALT_B:
  368. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  369. offset, 0);
  370. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
  371. af.alt_bit1, !!(af.altb_val && BIT(0)));
  372. if (af.alt_bit2 != UNUSED)
  373. ret = abx500_gpio_set_bits(chip,
  374. AB8500_GPIO_ALTFUN_REG,
  375. af.alt_bit2,
  376. !!(af.altb_val && BIT(1)));
  377. break;
  378. case ABX500_ALT_C:
  379. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  380. offset, 0);
  381. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
  382. af.alt_bit2, !!(af.altc_val && BIT(0)));
  383. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
  384. af.alt_bit2, !!(af.altc_val && BIT(1)));
  385. break;
  386. default:
  387. dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
  388. return -EINVAL;
  389. }
  390. return ret;
  391. }
  392. static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
  393. unsigned gpio)
  394. {
  395. u8 mode;
  396. bool bit_mode;
  397. bool alt_bit1;
  398. bool alt_bit2;
  399. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  400. struct alternate_functions af = pct->soc->alternate_functions[gpio];
  401. /* on ABx5xx, there is no GPIO0, so adjust the offset */
  402. unsigned offset = gpio - 1;
  403. /*
  404. * if gpiosel_bit is set to unused,
  405. * it means no GPIO or special case
  406. */
  407. if (af.gpiosel_bit == UNUSED)
  408. return ABX500_DEFAULT;
  409. /* read GpioSelx register */
  410. abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
  411. af.gpiosel_bit, &bit_mode);
  412. mode = bit_mode;
  413. /* sanity check */
  414. if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
  415. (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
  416. dev_err(pct->dev,
  417. "alt_bitX value not in correct range (-1 to 7)\n");
  418. return -EINVAL;
  419. }
  420. /* if alt_bit2 is used, alt_bit1 must be used too */
  421. if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
  422. dev_err(pct->dev,
  423. "if alt_bit2 is used, alt_bit1 can't be unused\n");
  424. return -EINVAL;
  425. }
  426. /* check if pin use AlternateFunction register */
  427. if ((af.alt_bit1 == UNUSED) && (af.alt_bit2 == UNUSED))
  428. return mode;
  429. /*
  430. * if pin GPIOSEL bit is set and pin supports alternate function,
  431. * it means DEFAULT mode
  432. */
  433. if (mode)
  434. return ABX500_DEFAULT;
  435. /*
  436. * pin use the AlternatFunction register
  437. * read alt_bit1 value
  438. */
  439. abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
  440. af.alt_bit1, &alt_bit1);
  441. if (af.alt_bit2 != UNUSED)
  442. /* read alt_bit2 value */
  443. abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
  444. &alt_bit2);
  445. else
  446. alt_bit2 = 0;
  447. mode = (alt_bit2 << 1) + alt_bit1;
  448. if (mode == af.alta_val)
  449. return ABX500_ALT_A;
  450. else if (mode == af.altb_val)
  451. return ABX500_ALT_B;
  452. else
  453. return ABX500_ALT_C;
  454. }
  455. #ifdef CONFIG_DEBUG_FS
  456. #include <linux/seq_file.h>
  457. static void abx500_gpio_dbg_show_one(struct seq_file *s,
  458. struct pinctrl_dev *pctldev,
  459. struct gpio_chip *chip,
  460. unsigned offset, unsigned gpio)
  461. {
  462. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  463. const char *label = gpiochip_is_requested(chip, offset - 1);
  464. u8 gpio_offset = offset - 1;
  465. int mode = -1;
  466. bool is_out;
  467. bool pd;
  468. enum abx500_gpio_pull_updown pud = 0;
  469. const char *modes[] = {
  470. [ABX500_DEFAULT] = "default",
  471. [ABX500_ALT_A] = "altA",
  472. [ABX500_ALT_B] = "altB",
  473. [ABX500_ALT_C] = "altC",
  474. };
  475. const char *pull_up_down[] = {
  476. [ABX500_GPIO_PULL_DOWN] = "pull down",
  477. [ABX500_GPIO_PULL_NONE] = "pull none",
  478. [ABX500_GPIO_PULL_NONE + 1] = "pull none",
  479. [ABX500_GPIO_PULL_UP] = "pull up",
  480. };
  481. abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
  482. seq_printf(s, " gpio-%-3d (%-20.20s) %-3s",
  483. gpio, label ?: "(none)",
  484. is_out ? "out" : "in ");
  485. if (!is_out) {
  486. if (abx500_pullud_supported(chip, offset)) {
  487. abx500_get_pull_updown(pct, offset, &pud);
  488. seq_printf(s, " %-9s", pull_up_down[pud]);
  489. } else {
  490. abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG,
  491. gpio_offset, &pd);
  492. seq_printf(s, " %-9s", pull_up_down[pd]);
  493. }
  494. } else
  495. seq_printf(s, " %-9s", chip->get(chip, offset) ? "hi" : "lo");
  496. if (pctldev)
  497. mode = abx500_get_mode(pctldev, chip, offset);
  498. seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
  499. }
  500. static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  501. {
  502. unsigned i;
  503. unsigned gpio = chip->base;
  504. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  505. struct pinctrl_dev *pctldev = pct->pctldev;
  506. for (i = 0; i < chip->ngpio; i++, gpio++) {
  507. /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
  508. abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
  509. seq_printf(s, "\n");
  510. }
  511. }
  512. #else
  513. static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
  514. struct pinctrl_dev *pctldev,
  515. struct gpio_chip *chip,
  516. unsigned offset, unsigned gpio)
  517. {
  518. }
  519. #define abx500_gpio_dbg_show NULL
  520. #endif
  521. static int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
  522. {
  523. int gpio = chip->base + offset;
  524. return pinctrl_request_gpio(gpio);
  525. }
  526. static void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
  527. {
  528. int gpio = chip->base + offset;
  529. pinctrl_free_gpio(gpio);
  530. }
  531. static struct gpio_chip abx500gpio_chip = {
  532. .label = "abx500-gpio",
  533. .owner = THIS_MODULE,
  534. .request = abx500_gpio_request,
  535. .free = abx500_gpio_free,
  536. .direction_input = abx500_gpio_direction_input,
  537. .get = abx500_gpio_get,
  538. .direction_output = abx500_gpio_direction_output,
  539. .set = abx500_gpio_set,
  540. .to_irq = abx500_gpio_to_irq,
  541. .dbg_show = abx500_gpio_dbg_show,
  542. };
  543. static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  544. {
  545. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  546. return pct->soc->nfunctions;
  547. }
  548. static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
  549. unsigned function)
  550. {
  551. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  552. return pct->soc->functions[function].name;
  553. }
  554. static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  555. unsigned function,
  556. const char * const **groups,
  557. unsigned * const num_groups)
  558. {
  559. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  560. *groups = pct->soc->functions[function].groups;
  561. *num_groups = pct->soc->functions[function].ngroups;
  562. return 0;
  563. }
  564. static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
  565. unsigned group)
  566. {
  567. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  568. struct gpio_chip *chip = &pct->chip;
  569. const struct abx500_pingroup *g;
  570. int i;
  571. int ret = 0;
  572. g = &pct->soc->groups[group];
  573. if (g->altsetting < 0)
  574. return -EINVAL;
  575. dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
  576. for (i = 0; i < g->npins; i++) {
  577. dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
  578. g->pins[i], g->altsetting);
  579. ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
  580. }
  581. return ret;
  582. }
  583. static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
  584. unsigned function, unsigned group)
  585. {
  586. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  587. const struct abx500_pingroup *g;
  588. g = &pct->soc->groups[group];
  589. if (g->altsetting < 0)
  590. return;
  591. /* FIXME: poke out the mux, set the pin to some default state? */
  592. dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
  593. }
  594. static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
  595. struct pinctrl_gpio_range *range,
  596. unsigned offset)
  597. {
  598. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  599. const struct abx500_pinrange *p;
  600. int ret;
  601. int i;
  602. /*
  603. * Different ranges have different ways to enable GPIO function on a
  604. * pin, so refer back to our local range type, where we handily define
  605. * what altfunc enables GPIO for a certain pin.
  606. */
  607. for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
  608. p = &pct->soc->gpio_ranges[i];
  609. if ((offset >= p->offset) &&
  610. (offset < (p->offset + p->npins)))
  611. break;
  612. }
  613. if (i == pct->soc->gpio_num_ranges) {
  614. dev_err(pct->dev, "%s failed to locate range\n", __func__);
  615. return -ENODEV;
  616. }
  617. dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
  618. p->altfunc, offset);
  619. ret = abx500_set_mode(pct->pctldev, &pct->chip,
  620. offset, p->altfunc);
  621. if (ret < 0) {
  622. dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
  623. return ret;
  624. }
  625. return ret;
  626. }
  627. static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
  628. struct pinctrl_gpio_range *range,
  629. unsigned offset)
  630. {
  631. }
  632. static const struct pinmux_ops abx500_pinmux_ops = {
  633. .get_functions_count = abx500_pmx_get_funcs_cnt,
  634. .get_function_name = abx500_pmx_get_func_name,
  635. .get_function_groups = abx500_pmx_get_func_groups,
  636. .enable = abx500_pmx_enable,
  637. .disable = abx500_pmx_disable,
  638. .gpio_request_enable = abx500_gpio_request_enable,
  639. .gpio_disable_free = abx500_gpio_disable_free,
  640. };
  641. static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
  642. {
  643. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  644. return pct->soc->ngroups;
  645. }
  646. static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
  647. unsigned selector)
  648. {
  649. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  650. return pct->soc->groups[selector].name;
  651. }
  652. static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
  653. unsigned selector,
  654. const unsigned **pins,
  655. unsigned *num_pins)
  656. {
  657. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  658. *pins = pct->soc->groups[selector].pins;
  659. *num_pins = pct->soc->groups[selector].npins;
  660. return 0;
  661. }
  662. static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
  663. struct seq_file *s, unsigned offset)
  664. {
  665. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  666. struct gpio_chip *chip = &pct->chip;
  667. abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
  668. chip->base + offset - 1);
  669. }
  670. static void abx500_dt_free_map(struct pinctrl_dev *pctldev,
  671. struct pinctrl_map *map, unsigned num_maps)
  672. {
  673. int i;
  674. for (i = 0; i < num_maps; i++)
  675. if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  676. kfree(map[i].data.configs.configs);
  677. kfree(map);
  678. }
  679. static int abx500_dt_reserve_map(struct pinctrl_map **map,
  680. unsigned *reserved_maps,
  681. unsigned *num_maps,
  682. unsigned reserve)
  683. {
  684. unsigned old_num = *reserved_maps;
  685. unsigned new_num = *num_maps + reserve;
  686. struct pinctrl_map *new_map;
  687. if (old_num >= new_num)
  688. return 0;
  689. new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
  690. if (!new_map)
  691. return -ENOMEM;
  692. memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
  693. *map = new_map;
  694. *reserved_maps = new_num;
  695. return 0;
  696. }
  697. static int abx500_dt_add_map_mux(struct pinctrl_map **map,
  698. unsigned *reserved_maps,
  699. unsigned *num_maps, const char *group,
  700. const char *function)
  701. {
  702. if (*num_maps == *reserved_maps)
  703. return -ENOSPC;
  704. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  705. (*map)[*num_maps].data.mux.group = group;
  706. (*map)[*num_maps].data.mux.function = function;
  707. (*num_maps)++;
  708. return 0;
  709. }
  710. static int abx500_dt_add_map_configs(struct pinctrl_map **map,
  711. unsigned *reserved_maps,
  712. unsigned *num_maps, const char *group,
  713. unsigned long *configs, unsigned num_configs)
  714. {
  715. unsigned long *dup_configs;
  716. if (*num_maps == *reserved_maps)
  717. return -ENOSPC;
  718. dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
  719. GFP_KERNEL);
  720. if (!dup_configs)
  721. return -ENOMEM;
  722. (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
  723. (*map)[*num_maps].data.configs.group_or_pin = group;
  724. (*map)[*num_maps].data.configs.configs = dup_configs;
  725. (*map)[*num_maps].data.configs.num_configs = num_configs;
  726. (*num_maps)++;
  727. return 0;
  728. }
  729. static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
  730. const char *pin_name)
  731. {
  732. int i, pin_number;
  733. struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  734. if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
  735. for (i = 0; i < npct->soc->npins; i++)
  736. if (npct->soc->pins[i].number == pin_number)
  737. return npct->soc->pins[i].name;
  738. return NULL;
  739. }
  740. static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  741. struct device_node *np,
  742. struct pinctrl_map **map,
  743. unsigned *reserved_maps,
  744. unsigned *num_maps)
  745. {
  746. int ret;
  747. const char *function = NULL;
  748. unsigned long *configs;
  749. unsigned int nconfigs = 0;
  750. bool has_config = 0;
  751. unsigned reserve = 0;
  752. struct property *prop;
  753. const char *group, *gpio_name;
  754. struct device_node *np_config;
  755. ret = of_property_read_string(np, "ste,function", &function);
  756. if (ret >= 0)
  757. reserve = 1;
  758. ret = pinconf_generic_parse_dt_config(np, &configs, &nconfigs);
  759. if (nconfigs)
  760. has_config = 1;
  761. np_config = of_parse_phandle(np, "ste,config", 0);
  762. if (np_config) {
  763. ret = pinconf_generic_parse_dt_config(np_config, &configs,
  764. &nconfigs);
  765. if (ret)
  766. goto exit;
  767. has_config |= nconfigs;
  768. }
  769. ret = of_property_count_strings(np, "ste,pins");
  770. if (ret < 0)
  771. goto exit;
  772. if (has_config)
  773. reserve++;
  774. reserve *= ret;
  775. ret = abx500_dt_reserve_map(map, reserved_maps, num_maps, reserve);
  776. if (ret < 0)
  777. goto exit;
  778. of_property_for_each_string(np, "ste,pins", prop, group) {
  779. if (function) {
  780. ret = abx500_dt_add_map_mux(map, reserved_maps,
  781. num_maps, group, function);
  782. if (ret < 0)
  783. goto exit;
  784. }
  785. if (has_config) {
  786. gpio_name = abx500_find_pin_name(pctldev, group);
  787. ret = abx500_dt_add_map_configs(map, reserved_maps,
  788. num_maps, gpio_name, configs, 1);
  789. if (ret < 0)
  790. goto exit;
  791. }
  792. }
  793. exit:
  794. return ret;
  795. }
  796. static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
  797. struct device_node *np_config,
  798. struct pinctrl_map **map, unsigned *num_maps)
  799. {
  800. unsigned reserved_maps;
  801. struct device_node *np;
  802. int ret;
  803. reserved_maps = 0;
  804. *map = NULL;
  805. *num_maps = 0;
  806. for_each_child_of_node(np_config, np) {
  807. ret = abx500_dt_subnode_to_map(pctldev, np, map,
  808. &reserved_maps, num_maps);
  809. if (ret < 0) {
  810. abx500_dt_free_map(pctldev, *map, *num_maps);
  811. return ret;
  812. }
  813. }
  814. return 0;
  815. }
  816. static const struct pinctrl_ops abx500_pinctrl_ops = {
  817. .get_groups_count = abx500_get_groups_cnt,
  818. .get_group_name = abx500_get_group_name,
  819. .get_group_pins = abx500_get_group_pins,
  820. .pin_dbg_show = abx500_pin_dbg_show,
  821. .dt_node_to_map = abx500_dt_node_to_map,
  822. .dt_free_map = abx500_dt_free_map,
  823. };
  824. static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
  825. unsigned pin,
  826. unsigned long *config)
  827. {
  828. return -ENOSYS;
  829. }
  830. static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
  831. unsigned pin,
  832. unsigned long config)
  833. {
  834. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  835. struct gpio_chip *chip = &pct->chip;
  836. unsigned offset;
  837. int ret = -EINVAL;
  838. enum pin_config_param param = pinconf_to_config_param(config);
  839. enum pin_config_param argument = pinconf_to_config_argument(config);
  840. dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
  841. pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
  842. (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
  843. (argument ? "pull up" : "pull down"));
  844. /* on ABx500, there is no GPIO0, so adjust the offset */
  845. offset = pin - 1;
  846. switch (param) {
  847. case PIN_CONFIG_BIAS_DISABLE:
  848. ret = abx500_gpio_direction_input(chip, offset);
  849. /*
  850. * Some chips only support pull down, while some actually
  851. * support both pull up and pull down. Such chips have
  852. * a "pullud" range specified for the pins that support
  853. * both features. If the pin is not within that range, we
  854. * fall back to the old bit set that only support pull down.
  855. */
  856. if (abx500_pullud_supported(chip, pin))
  857. ret = abx500_set_pull_updown(pct,
  858. pin,
  859. ABX500_GPIO_PULL_NONE);
  860. else
  861. /* Chip only supports pull down */
  862. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
  863. offset, ABX500_GPIO_PULL_NONE);
  864. break;
  865. case PIN_CONFIG_BIAS_PULL_DOWN:
  866. ret = abx500_gpio_direction_input(chip, offset);
  867. /*
  868. * if argument = 1 set the pull down
  869. * else clear the pull down
  870. * Some chips only support pull down, while some actually
  871. * support both pull up and pull down. Such chips have
  872. * a "pullud" range specified for the pins that support
  873. * both features. If the pin is not within that range, we
  874. * fall back to the old bit set that only support pull down.
  875. */
  876. if (abx500_pullud_supported(chip, pin))
  877. ret = abx500_set_pull_updown(pct,
  878. pin,
  879. argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
  880. else
  881. /* Chip only supports pull down */
  882. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
  883. offset,
  884. argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
  885. break;
  886. case PIN_CONFIG_BIAS_PULL_UP:
  887. ret = abx500_gpio_direction_input(chip, offset);
  888. /*
  889. * if argument = 1 set the pull up
  890. * else clear the pull up
  891. */
  892. ret = abx500_gpio_direction_input(chip, offset);
  893. /*
  894. * Some chips only support pull down, while some actually
  895. * support both pull up and pull down. Such chips have
  896. * a "pullud" range specified for the pins that support
  897. * both features. If the pin is not within that range, do
  898. * nothing
  899. */
  900. if (abx500_pullud_supported(chip, pin))
  901. ret = abx500_set_pull_updown(pct,
  902. pin,
  903. argument ? ABX500_GPIO_PULL_UP : ABX500_GPIO_PULL_NONE);
  904. break;
  905. case PIN_CONFIG_OUTPUT:
  906. ret = abx500_gpio_direction_output(chip, offset, argument);
  907. break;
  908. default:
  909. dev_err(chip->dev, "illegal configuration requested\n");
  910. }
  911. return ret;
  912. }
  913. static const struct pinconf_ops abx500_pinconf_ops = {
  914. .pin_config_get = abx500_pin_config_get,
  915. .pin_config_set = abx500_pin_config_set,
  916. };
  917. static struct pinctrl_desc abx500_pinctrl_desc = {
  918. .name = "pinctrl-abx500",
  919. .pctlops = &abx500_pinctrl_ops,
  920. .pmxops = &abx500_pinmux_ops,
  921. .confops = &abx500_pinconf_ops,
  922. .owner = THIS_MODULE,
  923. };
  924. static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
  925. {
  926. unsigned int lowest = 0;
  927. unsigned int highest = 0;
  928. unsigned int npins = 0;
  929. int i;
  930. /*
  931. * Compute number of GPIOs from the last SoC gpio range descriptors
  932. * These ranges may include "holes" but the GPIO number space shall
  933. * still be homogeneous, so we need to detect and account for any
  934. * such holes so that these are included in the number of GPIO pins.
  935. */
  936. for (i = 0; i < soc->gpio_num_ranges; i++) {
  937. unsigned gstart;
  938. unsigned gend;
  939. const struct abx500_pinrange *p;
  940. p = &soc->gpio_ranges[i];
  941. gstart = p->offset;
  942. gend = p->offset + p->npins - 1;
  943. if (i == 0) {
  944. /* First iteration, set start values */
  945. lowest = gstart;
  946. highest = gend;
  947. } else {
  948. if (gstart < lowest)
  949. lowest = gstart;
  950. if (gend > highest)
  951. highest = gend;
  952. }
  953. }
  954. /* this gives the absolute number of pins */
  955. npins = highest - lowest + 1;
  956. return npins;
  957. }
  958. static const struct of_device_id abx500_gpio_match[] = {
  959. { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
  960. { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
  961. { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
  962. { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
  963. { }
  964. };
  965. static int abx500_gpio_probe(struct platform_device *pdev)
  966. {
  967. struct ab8500_platform_data *abx500_pdata =
  968. dev_get_platdata(pdev->dev.parent);
  969. struct abx500_gpio_platform_data *pdata = NULL;
  970. struct device_node *np = pdev->dev.of_node;
  971. struct abx500_pinctrl *pct;
  972. const struct platform_device_id *platid = platform_get_device_id(pdev);
  973. unsigned int id = -1;
  974. int ret, err;
  975. int i;
  976. if (abx500_pdata)
  977. pdata = abx500_pdata->gpio;
  978. if (!(pdata || np)) {
  979. dev_err(&pdev->dev, "gpio dt and platform data missing\n");
  980. return -ENODEV;
  981. }
  982. pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
  983. GFP_KERNEL);
  984. if (pct == NULL) {
  985. dev_err(&pdev->dev,
  986. "failed to allocate memory for pct\n");
  987. return -ENOMEM;
  988. }
  989. pct->dev = &pdev->dev;
  990. pct->parent = dev_get_drvdata(pdev->dev.parent);
  991. pct->chip = abx500gpio_chip;
  992. pct->chip.dev = &pdev->dev;
  993. pct->chip.base = (np) ? -1 : pdata->gpio_base;
  994. if (platid)
  995. id = platid->driver_data;
  996. else if (np) {
  997. const struct of_device_id *match;
  998. match = of_match_device(abx500_gpio_match, &pdev->dev);
  999. if (match)
  1000. id = (unsigned long)match->data;
  1001. }
  1002. /* Poke in other ASIC variants here */
  1003. switch (id) {
  1004. case PINCTRL_AB8500:
  1005. abx500_pinctrl_ab8500_init(&pct->soc);
  1006. break;
  1007. case PINCTRL_AB8540:
  1008. abx500_pinctrl_ab8540_init(&pct->soc);
  1009. break;
  1010. case PINCTRL_AB9540:
  1011. abx500_pinctrl_ab9540_init(&pct->soc);
  1012. break;
  1013. case PINCTRL_AB8505:
  1014. abx500_pinctrl_ab8505_init(&pct->soc);
  1015. break;
  1016. default:
  1017. dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
  1018. return -EINVAL;
  1019. }
  1020. if (!pct->soc) {
  1021. dev_err(&pdev->dev, "Invalid SOC data\n");
  1022. return -EINVAL;
  1023. }
  1024. pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
  1025. pct->irq_cluster = pct->soc->gpio_irq_cluster;
  1026. pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
  1027. ret = gpiochip_add(&pct->chip);
  1028. if (ret) {
  1029. dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
  1030. return ret;
  1031. }
  1032. dev_info(&pdev->dev, "added gpiochip\n");
  1033. abx500_pinctrl_desc.pins = pct->soc->pins;
  1034. abx500_pinctrl_desc.npins = pct->soc->npins;
  1035. pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
  1036. if (!pct->pctldev) {
  1037. dev_err(&pdev->dev,
  1038. "could not register abx500 pinctrl driver\n");
  1039. ret = -EINVAL;
  1040. goto out_rem_chip;
  1041. }
  1042. dev_info(&pdev->dev, "registered pin controller\n");
  1043. /* We will handle a range of GPIO pins */
  1044. for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
  1045. const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
  1046. ret = gpiochip_add_pin_range(&pct->chip,
  1047. dev_name(&pdev->dev),
  1048. p->offset - 1, p->offset, p->npins);
  1049. if (ret < 0)
  1050. goto out_rem_chip;
  1051. }
  1052. platform_set_drvdata(pdev, pct);
  1053. dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
  1054. return 0;
  1055. out_rem_chip:
  1056. err = gpiochip_remove(&pct->chip);
  1057. if (err)
  1058. dev_info(&pdev->dev, "failed to remove gpiochip\n");
  1059. return ret;
  1060. }
  1061. /**
  1062. * abx500_gpio_remove() - remove Ab8500-gpio driver
  1063. * @pdev: Platform device registered
  1064. */
  1065. static int abx500_gpio_remove(struct platform_device *pdev)
  1066. {
  1067. struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
  1068. int ret;
  1069. ret = gpiochip_remove(&pct->chip);
  1070. if (ret < 0) {
  1071. dev_err(pct->dev, "unable to remove gpiochip: %d\n",
  1072. ret);
  1073. return ret;
  1074. }
  1075. return 0;
  1076. }
  1077. static const struct platform_device_id abx500_pinctrl_id[] = {
  1078. { "pinctrl-ab8500", PINCTRL_AB8500 },
  1079. { "pinctrl-ab8540", PINCTRL_AB8540 },
  1080. { "pinctrl-ab9540", PINCTRL_AB9540 },
  1081. { "pinctrl-ab8505", PINCTRL_AB8505 },
  1082. { },
  1083. };
  1084. static struct platform_driver abx500_gpio_driver = {
  1085. .driver = {
  1086. .name = "abx500-gpio",
  1087. .owner = THIS_MODULE,
  1088. .of_match_table = abx500_gpio_match,
  1089. },
  1090. .probe = abx500_gpio_probe,
  1091. .remove = abx500_gpio_remove,
  1092. .id_table = abx500_pinctrl_id,
  1093. };
  1094. static int __init abx500_gpio_init(void)
  1095. {
  1096. return platform_driver_register(&abx500_gpio_driver);
  1097. }
  1098. core_initcall(abx500_gpio_init);
  1099. MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
  1100. MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
  1101. MODULE_ALIAS("platform:abx500-gpio");
  1102. MODULE_LICENSE("GPL v2");