pinctrl-abx500.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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 "pinctrl-abx500.h"
  34. /*
  35. * The AB9540 and AB8540 GPIO support are extended versions
  36. * of the AB8500 GPIO support.
  37. * The AB9540 supports an additional (7th) register so that
  38. * more GPIO may be configured and used.
  39. * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have
  40. * internal pull-up and pull-down capabilities.
  41. */
  42. /*
  43. * GPIO registers offset
  44. * Bank: 0x10
  45. */
  46. #define AB8500_GPIO_SEL1_REG 0x00
  47. #define AB8500_GPIO_SEL2_REG 0x01
  48. #define AB8500_GPIO_SEL3_REG 0x02
  49. #define AB8500_GPIO_SEL4_REG 0x03
  50. #define AB8500_GPIO_SEL5_REG 0x04
  51. #define AB8500_GPIO_SEL6_REG 0x05
  52. #define AB9540_GPIO_SEL7_REG 0x06
  53. #define AB8500_GPIO_DIR1_REG 0x10
  54. #define AB8500_GPIO_DIR2_REG 0x11
  55. #define AB8500_GPIO_DIR3_REG 0x12
  56. #define AB8500_GPIO_DIR4_REG 0x13
  57. #define AB8500_GPIO_DIR5_REG 0x14
  58. #define AB8500_GPIO_DIR6_REG 0x15
  59. #define AB9540_GPIO_DIR7_REG 0x16
  60. #define AB8500_GPIO_OUT1_REG 0x20
  61. #define AB8500_GPIO_OUT2_REG 0x21
  62. #define AB8500_GPIO_OUT3_REG 0x22
  63. #define AB8500_GPIO_OUT4_REG 0x23
  64. #define AB8500_GPIO_OUT5_REG 0x24
  65. #define AB8500_GPIO_OUT6_REG 0x25
  66. #define AB9540_GPIO_OUT7_REG 0x26
  67. #define AB8500_GPIO_PUD1_REG 0x30
  68. #define AB8500_GPIO_PUD2_REG 0x31
  69. #define AB8500_GPIO_PUD3_REG 0x32
  70. #define AB8500_GPIO_PUD4_REG 0x33
  71. #define AB8500_GPIO_PUD5_REG 0x34
  72. #define AB8500_GPIO_PUD6_REG 0x35
  73. #define AB9540_GPIO_PUD7_REG 0x36
  74. #define AB8500_GPIO_IN1_REG 0x40
  75. #define AB8500_GPIO_IN2_REG 0x41
  76. #define AB8500_GPIO_IN3_REG 0x42
  77. #define AB8500_GPIO_IN4_REG 0x43
  78. #define AB8500_GPIO_IN5_REG 0x44
  79. #define AB8500_GPIO_IN6_REG 0x45
  80. #define AB9540_GPIO_IN7_REG 0x46
  81. #define AB8540_GPIO_VINSEL_REG 0x47
  82. #define AB8540_GPIO_PULL_UPDOWN_REG 0x48
  83. #define AB8500_GPIO_ALTFUN_REG 0x50
  84. #define AB8540_GPIO_PULL_UPDOWN_MASK 0x03
  85. #define AB8540_GPIO_VINSEL_MASK 0x03
  86. #define AB8540_GPIOX_VBAT_START 51
  87. #define AB8540_GPIOX_VBAT_END 54
  88. struct abx500_pinctrl {
  89. struct device *dev;
  90. struct pinctrl_dev *pctldev;
  91. struct abx500_pinctrl_soc_data *soc;
  92. struct gpio_chip chip;
  93. struct ab8500 *parent;
  94. struct mutex lock;
  95. struct abx500_gpio_irq_cluster *irq_cluster;
  96. int irq_cluster_size;
  97. };
  98. /**
  99. * to_abx500_pinctrl() - get the pointer to abx500_pinctrl
  100. * @chip: Member of the structure abx500_pinctrl
  101. */
  102. static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip)
  103. {
  104. return container_of(chip, struct abx500_pinctrl, chip);
  105. }
  106. static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
  107. unsigned offset, bool *bit)
  108. {
  109. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  110. u8 pos = offset % 8;
  111. u8 val;
  112. int ret;
  113. reg += offset / 8;
  114. ret = abx500_get_register_interruptible(pct->dev,
  115. AB8500_MISC, reg, &val);
  116. *bit = !!(val & BIT(pos));
  117. if (ret < 0)
  118. dev_err(pct->dev,
  119. "%s read reg =%x, offset=%x failed\n",
  120. __func__, reg, offset);
  121. return ret;
  122. }
  123. static int abx500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
  124. unsigned offset, int val)
  125. {
  126. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  127. u8 pos = offset % 8;
  128. int ret;
  129. reg += offset / 8;
  130. ret = abx500_mask_and_set_register_interruptible(pct->dev,
  131. AB8500_MISC, reg, BIT(pos), val << pos);
  132. if (ret < 0)
  133. dev_err(pct->dev, "%s write failed\n", __func__);
  134. return ret;
  135. }
  136. /**
  137. * abx500_gpio_get() - Get the particular GPIO value
  138. * @chip: Gpio device
  139. * @offset: GPIO number to read
  140. */
  141. static int abx500_gpio_get(struct gpio_chip *chip, unsigned offset)
  142. {
  143. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  144. bool bit;
  145. int ret;
  146. ret = abx500_gpio_get_bit(chip, AB8500_GPIO_IN1_REG,
  147. offset, &bit);
  148. if (ret < 0) {
  149. dev_err(pct->dev, "%s failed\n", __func__);
  150. return ret;
  151. }
  152. return bit;
  153. }
  154. static void abx500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  155. {
  156. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  157. int ret;
  158. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
  159. if (ret < 0)
  160. dev_err(pct->dev, "%s write failed\n", __func__);
  161. }
  162. static int abx500_config_pull_updown(struct abx500_pinctrl *pct,
  163. int offset, enum abx500_gpio_pull_updown val)
  164. {
  165. u8 pos;
  166. int ret;
  167. struct pullud *pullud;
  168. if (!pct->soc->pullud) {
  169. dev_err(pct->dev, "%s AB chip doesn't support pull up/down feature",
  170. __func__);
  171. ret = -EPERM;
  172. goto out;
  173. }
  174. pullud = pct->soc->pullud;
  175. if ((offset < pullud->first_pin)
  176. || (offset > pullud->last_pin)) {
  177. ret = -EINVAL;
  178. goto out;
  179. }
  180. pos = offset << 1;
  181. ret = abx500_mask_and_set_register_interruptible(pct->dev,
  182. AB8500_MISC, AB8540_GPIO_PULL_UPDOWN_REG,
  183. AB8540_GPIO_PULL_UPDOWN_MASK << pos, val << pos);
  184. out:
  185. if (ret < 0)
  186. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  187. return ret;
  188. }
  189. static int abx500_gpio_direction_output(struct gpio_chip *chip,
  190. unsigned offset,
  191. int val)
  192. {
  193. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  194. struct pullud *pullud = pct->soc->pullud;
  195. unsigned gpio;
  196. int ret;
  197. /* set direction as output */
  198. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
  199. if (ret < 0)
  200. return ret;
  201. /* disable pull down */
  202. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
  203. if (ret < 0)
  204. return ret;
  205. /* if supported, disable both pull down and pull up */
  206. gpio = offset + 1;
  207. if (pullud && gpio >= pullud->first_pin && gpio <= pullud->last_pin) {
  208. ret = abx500_config_pull_updown(pct,
  209. gpio,
  210. ABX500_GPIO_PULL_NONE);
  211. if (ret < 0)
  212. return ret;
  213. }
  214. /* set the output as 1 or 0 */
  215. return abx500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
  216. }
  217. static int abx500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  218. {
  219. /* set the register as input */
  220. return abx500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
  221. }
  222. static int abx500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  223. {
  224. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  225. /* The AB8500 GPIO numbers are off by one */
  226. int gpio = offset + 1;
  227. int hwirq;
  228. int i;
  229. for (i = 0; i < pct->irq_cluster_size; i++) {
  230. struct abx500_gpio_irq_cluster *cluster =
  231. &pct->irq_cluster[i];
  232. if (gpio >= cluster->start && gpio <= cluster->end) {
  233. /*
  234. * The ABx500 GPIO's associated IRQs are clustered together
  235. * throughout the interrupt numbers at irregular intervals.
  236. * To solve this quandry, we have placed the read-in values
  237. * into the cluster information table.
  238. */
  239. hwirq = gpio - cluster->start + cluster->to_irq;
  240. return irq_create_mapping(pct->parent->domain, hwirq);
  241. }
  242. }
  243. return -EINVAL;
  244. }
  245. static int abx500_set_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
  246. unsigned gpio, int alt_setting)
  247. {
  248. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  249. struct alternate_functions af = pct->soc->alternate_functions[gpio];
  250. int ret;
  251. int val;
  252. unsigned offset;
  253. const char *modes[] = {
  254. [ABX500_DEFAULT] = "default",
  255. [ABX500_ALT_A] = "altA",
  256. [ABX500_ALT_B] = "altB",
  257. [ABX500_ALT_C] = "altC",
  258. };
  259. /* sanity check */
  260. if (((alt_setting == ABX500_ALT_A) && (af.gpiosel_bit == UNUSED)) ||
  261. ((alt_setting == ABX500_ALT_B) && (af.alt_bit1 == UNUSED)) ||
  262. ((alt_setting == ABX500_ALT_C) && (af.alt_bit2 == UNUSED))) {
  263. dev_dbg(pct->dev, "pin %d doesn't support %s mode\n", gpio,
  264. modes[alt_setting]);
  265. return -EINVAL;
  266. }
  267. /* on ABx5xx, there is no GPIO0, so adjust the offset */
  268. offset = gpio - 1;
  269. switch (alt_setting) {
  270. case ABX500_DEFAULT:
  271. /*
  272. * for ABx5xx family, default mode is always selected by
  273. * writing 0 to GPIOSELx register, except for pins which
  274. * support at least ALT_B mode, default mode is selected
  275. * by writing 1 to GPIOSELx register
  276. */
  277. val = 0;
  278. if (af.alt_bit1 != UNUSED)
  279. val++;
  280. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  281. offset, val);
  282. break;
  283. case ABX500_ALT_A:
  284. /*
  285. * for ABx5xx family, alt_a mode is always selected by
  286. * writing 1 to GPIOSELx register, except for pins which
  287. * support at least ALT_B mode, alt_a mode is selected
  288. * by writing 0 to GPIOSELx register and 0 in ALTFUNC
  289. * register
  290. */
  291. if (af.alt_bit1 != UNUSED) {
  292. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  293. offset, 0);
  294. ret = abx500_gpio_set_bits(chip,
  295. AB8500_GPIO_ALTFUN_REG,
  296. af.alt_bit1,
  297. !!(af.alta_val && BIT(0)));
  298. if (af.alt_bit2 != UNUSED)
  299. ret = abx500_gpio_set_bits(chip,
  300. AB8500_GPIO_ALTFUN_REG,
  301. af.alt_bit2,
  302. !!(af.alta_val && BIT(1)));
  303. } else
  304. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  305. offset, 1);
  306. break;
  307. case ABX500_ALT_B:
  308. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  309. offset, 0);
  310. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
  311. af.alt_bit1, !!(af.altb_val && BIT(0)));
  312. if (af.alt_bit2 != UNUSED)
  313. ret = abx500_gpio_set_bits(chip,
  314. AB8500_GPIO_ALTFUN_REG,
  315. af.alt_bit2,
  316. !!(af.altb_val && BIT(1)));
  317. break;
  318. case ABX500_ALT_C:
  319. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_SEL1_REG,
  320. offset, 0);
  321. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
  322. af.alt_bit2, !!(af.altc_val && BIT(0)));
  323. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_ALTFUN_REG,
  324. af.alt_bit2, !!(af.altc_val && BIT(1)));
  325. break;
  326. default:
  327. dev_dbg(pct->dev, "unknow alt_setting %d\n", alt_setting);
  328. return -EINVAL;
  329. }
  330. return ret;
  331. }
  332. static u8 abx500_get_mode(struct pinctrl_dev *pctldev, struct gpio_chip *chip,
  333. unsigned gpio)
  334. {
  335. u8 mode;
  336. bool bit_mode;
  337. bool alt_bit1;
  338. bool alt_bit2;
  339. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  340. struct alternate_functions af = pct->soc->alternate_functions[gpio];
  341. /* on ABx5xx, there is no GPIO0, so adjust the offset */
  342. unsigned offset = gpio - 1;
  343. /*
  344. * if gpiosel_bit is set to unused,
  345. * it means no GPIO or special case
  346. */
  347. if (af.gpiosel_bit == UNUSED)
  348. return ABX500_DEFAULT;
  349. /* read GpioSelx register */
  350. abx500_gpio_get_bit(chip, AB8500_GPIO_SEL1_REG + (offset / 8),
  351. af.gpiosel_bit, &bit_mode);
  352. mode = bit_mode;
  353. /* sanity check */
  354. if ((af.alt_bit1 < UNUSED) || (af.alt_bit1 > 7) ||
  355. (af.alt_bit2 < UNUSED) || (af.alt_bit2 > 7)) {
  356. dev_err(pct->dev,
  357. "alt_bitX value not in correct range (-1 to 7)\n");
  358. return -EINVAL;
  359. }
  360. /* if alt_bit2 is used, alt_bit1 must be used too */
  361. if ((af.alt_bit2 != UNUSED) && (af.alt_bit1 == UNUSED)) {
  362. dev_err(pct->dev,
  363. "if alt_bit2 is used, alt_bit1 can't be unused\n");
  364. return -EINVAL;
  365. }
  366. /* check if pin use AlternateFunction register */
  367. if ((af.alt_bit1 == UNUSED) && (af.alt_bit1 == UNUSED))
  368. return mode;
  369. /*
  370. * if pin GPIOSEL bit is set and pin supports alternate function,
  371. * it means DEFAULT mode
  372. */
  373. if (mode)
  374. return ABX500_DEFAULT;
  375. /*
  376. * pin use the AlternatFunction register
  377. * read alt_bit1 value
  378. */
  379. abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG,
  380. af.alt_bit1, &alt_bit1);
  381. if (af.alt_bit2 != UNUSED)
  382. /* read alt_bit2 value */
  383. abx500_gpio_get_bit(chip, AB8500_GPIO_ALTFUN_REG, af.alt_bit2,
  384. &alt_bit2);
  385. else
  386. alt_bit2 = 0;
  387. mode = (alt_bit2 << 1) + alt_bit1;
  388. if (mode == af.alta_val)
  389. return ABX500_ALT_A;
  390. else if (mode == af.altb_val)
  391. return ABX500_ALT_B;
  392. else
  393. return ABX500_ALT_C;
  394. }
  395. #ifdef CONFIG_DEBUG_FS
  396. #include <linux/seq_file.h>
  397. static void abx500_gpio_dbg_show_one(struct seq_file *s,
  398. struct pinctrl_dev *pctldev,
  399. struct gpio_chip *chip,
  400. unsigned offset, unsigned gpio)
  401. {
  402. const char *label = gpiochip_is_requested(chip, offset - 1);
  403. u8 gpio_offset = offset - 1;
  404. int mode = -1;
  405. bool is_out;
  406. bool pull;
  407. const char *modes[] = {
  408. [ABX500_DEFAULT] = "default",
  409. [ABX500_ALT_A] = "altA",
  410. [ABX500_ALT_B] = "altB",
  411. [ABX500_ALT_C] = "altC",
  412. };
  413. abx500_gpio_get_bit(chip, AB8500_GPIO_DIR1_REG, gpio_offset, &is_out);
  414. abx500_gpio_get_bit(chip, AB8500_GPIO_PUD1_REG, gpio_offset, &pull);
  415. if (pctldev)
  416. mode = abx500_get_mode(pctldev, chip, offset);
  417. seq_printf(s, " gpio-%-3d (%-20.20s) %-3s %-9s %s",
  418. gpio, label ?: "(none)",
  419. is_out ? "out" : "in ",
  420. is_out ?
  421. (chip->get
  422. ? (chip->get(chip, offset) ? "hi" : "lo")
  423. : "? ")
  424. : (pull ? "pull up" : "pull down"),
  425. (mode < 0) ? "unknown" : modes[mode]);
  426. }
  427. static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  428. {
  429. unsigned i;
  430. unsigned gpio = chip->base;
  431. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  432. struct pinctrl_dev *pctldev = pct->pctldev;
  433. for (i = 0; i < chip->ngpio; i++, gpio++) {
  434. /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
  435. abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
  436. seq_printf(s, "\n");
  437. }
  438. }
  439. #else
  440. static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
  441. struct pinctrl_dev *pctldev,
  442. struct gpio_chip *chip,
  443. unsigned offset, unsigned gpio)
  444. {
  445. }
  446. #define abx500_gpio_dbg_show NULL
  447. #endif
  448. int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
  449. {
  450. int gpio = chip->base + offset;
  451. return pinctrl_request_gpio(gpio);
  452. }
  453. void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
  454. {
  455. int gpio = chip->base + offset;
  456. pinctrl_free_gpio(gpio);
  457. }
  458. static struct gpio_chip abx500gpio_chip = {
  459. .label = "abx500-gpio",
  460. .owner = THIS_MODULE,
  461. .request = abx500_gpio_request,
  462. .free = abx500_gpio_free,
  463. .direction_input = abx500_gpio_direction_input,
  464. .get = abx500_gpio_get,
  465. .direction_output = abx500_gpio_direction_output,
  466. .set = abx500_gpio_set,
  467. .to_irq = abx500_gpio_to_irq,
  468. .dbg_show = abx500_gpio_dbg_show,
  469. };
  470. static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  471. {
  472. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  473. return pct->soc->nfunctions;
  474. }
  475. static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
  476. unsigned function)
  477. {
  478. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  479. return pct->soc->functions[function].name;
  480. }
  481. static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  482. unsigned function,
  483. const char * const **groups,
  484. unsigned * const num_groups)
  485. {
  486. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  487. *groups = pct->soc->functions[function].groups;
  488. *num_groups = pct->soc->functions[function].ngroups;
  489. return 0;
  490. }
  491. static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
  492. unsigned group)
  493. {
  494. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  495. struct gpio_chip *chip = &pct->chip;
  496. const struct abx500_pingroup *g;
  497. int i;
  498. int ret = 0;
  499. g = &pct->soc->groups[group];
  500. if (g->altsetting < 0)
  501. return -EINVAL;
  502. dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
  503. for (i = 0; i < g->npins; i++) {
  504. dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
  505. g->pins[i], g->altsetting);
  506. ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
  507. }
  508. return ret;
  509. }
  510. static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
  511. unsigned function, unsigned group)
  512. {
  513. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  514. const struct abx500_pingroup *g;
  515. g = &pct->soc->groups[group];
  516. if (g->altsetting < 0)
  517. return;
  518. /* FIXME: poke out the mux, set the pin to some default state? */
  519. dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
  520. }
  521. int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
  522. struct pinctrl_gpio_range *range,
  523. unsigned offset)
  524. {
  525. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  526. const struct abx500_pinrange *p;
  527. int ret;
  528. int i;
  529. /*
  530. * Different ranges have different ways to enable GPIO function on a
  531. * pin, so refer back to our local range type, where we handily define
  532. * what altfunc enables GPIO for a certain pin.
  533. */
  534. for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
  535. p = &pct->soc->gpio_ranges[i];
  536. if ((offset >= p->offset) &&
  537. (offset < (p->offset + p->npins)))
  538. break;
  539. }
  540. if (i == pct->soc->gpio_num_ranges) {
  541. dev_err(pct->dev, "%s failed to locate range\n", __func__);
  542. return -ENODEV;
  543. }
  544. dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
  545. p->altfunc, offset);
  546. ret = abx500_set_mode(pct->pctldev, &pct->chip,
  547. offset, p->altfunc);
  548. if (ret < 0) {
  549. dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
  550. return ret;
  551. }
  552. return ret;
  553. }
  554. static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
  555. struct pinctrl_gpio_range *range,
  556. unsigned offset)
  557. {
  558. }
  559. static struct pinmux_ops abx500_pinmux_ops = {
  560. .get_functions_count = abx500_pmx_get_funcs_cnt,
  561. .get_function_name = abx500_pmx_get_func_name,
  562. .get_function_groups = abx500_pmx_get_func_groups,
  563. .enable = abx500_pmx_enable,
  564. .disable = abx500_pmx_disable,
  565. .gpio_request_enable = abx500_gpio_request_enable,
  566. .gpio_disable_free = abx500_gpio_disable_free,
  567. };
  568. static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
  569. {
  570. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  571. return pct->soc->ngroups;
  572. }
  573. static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
  574. unsigned selector)
  575. {
  576. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  577. return pct->soc->groups[selector].name;
  578. }
  579. static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
  580. unsigned selector,
  581. const unsigned **pins,
  582. unsigned *num_pins)
  583. {
  584. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  585. *pins = pct->soc->groups[selector].pins;
  586. *num_pins = pct->soc->groups[selector].npins;
  587. return 0;
  588. }
  589. static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
  590. struct seq_file *s, unsigned offset)
  591. {
  592. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  593. struct gpio_chip *chip = &pct->chip;
  594. abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
  595. chip->base + offset - 1);
  596. }
  597. static struct pinctrl_ops abx500_pinctrl_ops = {
  598. .get_groups_count = abx500_get_groups_cnt,
  599. .get_group_name = abx500_get_group_name,
  600. .get_group_pins = abx500_get_group_pins,
  601. .pin_dbg_show = abx500_pin_dbg_show,
  602. };
  603. int abx500_pin_config_get(struct pinctrl_dev *pctldev,
  604. unsigned pin,
  605. unsigned long *config)
  606. {
  607. return -ENOSYS;
  608. }
  609. int abx500_pin_config_set(struct pinctrl_dev *pctldev,
  610. unsigned pin,
  611. unsigned long config)
  612. {
  613. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  614. struct pullud *pullud = pct->soc->pullud;
  615. struct gpio_chip *chip = &pct->chip;
  616. unsigned offset;
  617. int ret;
  618. enum pin_config_param param = pinconf_to_config_param(config);
  619. enum pin_config_param argument = pinconf_to_config_argument(config);
  620. dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
  621. pin, config, (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
  622. (param == PIN_CONFIG_OUTPUT) ? (argument ? "high" : "low") :
  623. (argument ? "pull up" : "pull down"));
  624. /* on ABx500, there is no GPIO0, so adjust the offset */
  625. offset = pin - 1;
  626. switch (param) {
  627. case PIN_CONFIG_BIAS_PULL_DOWN:
  628. /*
  629. * if argument = 1 set the pull down
  630. * else clear the pull down
  631. */
  632. ret = abx500_gpio_direction_input(chip, offset);
  633. /*
  634. * Some chips only support pull down, while some actually
  635. * support both pull up and pull down. Such chips have
  636. * a "pullud" range specified for the pins that support
  637. * both features. If the pin is not within that range, we
  638. * fall back to the old bit set that only support pull down.
  639. */
  640. if (pullud &&
  641. pin >= pullud->first_pin &&
  642. pin <= pullud->last_pin)
  643. ret = abx500_config_pull_updown(pct,
  644. pin,
  645. argument ? ABX500_GPIO_PULL_DOWN : ABX500_GPIO_PULL_NONE);
  646. else
  647. /* Chip only supports pull down */
  648. ret = abx500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG,
  649. offset, argument ? 0 : 1);
  650. break;
  651. case PIN_CONFIG_OUTPUT:
  652. ret = abx500_gpio_direction_output(chip, offset, argument);
  653. break;
  654. default:
  655. dev_err(chip->dev, "illegal configuration requested\n");
  656. return -EINVAL;
  657. }
  658. return ret;
  659. }
  660. static struct pinconf_ops abx500_pinconf_ops = {
  661. .pin_config_get = abx500_pin_config_get,
  662. .pin_config_set = abx500_pin_config_set,
  663. };
  664. static struct pinctrl_desc abx500_pinctrl_desc = {
  665. .name = "pinctrl-abx500",
  666. .pctlops = &abx500_pinctrl_ops,
  667. .pmxops = &abx500_pinmux_ops,
  668. .confops = &abx500_pinconf_ops,
  669. .owner = THIS_MODULE,
  670. };
  671. static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
  672. {
  673. unsigned int lowest = 0;
  674. unsigned int highest = 0;
  675. unsigned int npins = 0;
  676. int i;
  677. /*
  678. * Compute number of GPIOs from the last SoC gpio range descriptors
  679. * These ranges may include "holes" but the GPIO number space shall
  680. * still be homogeneous, so we need to detect and account for any
  681. * such holes so that these are included in the number of GPIO pins.
  682. */
  683. for (i = 0; i < soc->gpio_num_ranges; i++) {
  684. unsigned gstart;
  685. unsigned gend;
  686. const struct abx500_pinrange *p;
  687. p = &soc->gpio_ranges[i];
  688. gstart = p->offset;
  689. gend = p->offset + p->npins - 1;
  690. if (i == 0) {
  691. /* First iteration, set start values */
  692. lowest = gstart;
  693. highest = gend;
  694. } else {
  695. if (gstart < lowest)
  696. lowest = gstart;
  697. if (gend > highest)
  698. highest = gend;
  699. }
  700. }
  701. /* this gives the absolute number of pins */
  702. npins = highest - lowest + 1;
  703. return npins;
  704. }
  705. static const struct of_device_id abx500_gpio_match[] = {
  706. { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
  707. { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
  708. { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
  709. { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
  710. };
  711. static int abx500_gpio_probe(struct platform_device *pdev)
  712. {
  713. struct ab8500_platform_data *abx500_pdata =
  714. dev_get_platdata(pdev->dev.parent);
  715. struct abx500_gpio_platform_data *pdata = NULL;
  716. struct device_node *np = pdev->dev.of_node;
  717. struct abx500_pinctrl *pct;
  718. const struct platform_device_id *platid = platform_get_device_id(pdev);
  719. unsigned int id = -1;
  720. int ret, err;
  721. int i;
  722. if (abx500_pdata)
  723. pdata = abx500_pdata->gpio;
  724. if (!pdata) {
  725. if (np) {
  726. const struct of_device_id *match;
  727. match = of_match_device(abx500_gpio_match, &pdev->dev);
  728. if (!match)
  729. return -ENODEV;
  730. id = (unsigned long)match->data;
  731. } else {
  732. dev_err(&pdev->dev, "gpio dt and platform data missing\n");
  733. return -ENODEV;
  734. }
  735. }
  736. if (platid)
  737. id = platid->driver_data;
  738. pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
  739. GFP_KERNEL);
  740. if (pct == NULL) {
  741. dev_err(&pdev->dev,
  742. "failed to allocate memory for pct\n");
  743. return -ENOMEM;
  744. }
  745. pct->dev = &pdev->dev;
  746. pct->parent = dev_get_drvdata(pdev->dev.parent);
  747. pct->chip = abx500gpio_chip;
  748. pct->chip.dev = &pdev->dev;
  749. pct->chip.base = pdata->gpio_base;
  750. pct->chip.base = (np) ? -1 : pdata->gpio_base;
  751. /* initialize the lock */
  752. mutex_init(&pct->lock);
  753. /* Poke in other ASIC variants here */
  754. switch (id) {
  755. case PINCTRL_AB8500:
  756. abx500_pinctrl_ab8500_init(&pct->soc);
  757. break;
  758. case PINCTRL_AB8540:
  759. abx500_pinctrl_ab8540_init(&pct->soc);
  760. break;
  761. case PINCTRL_AB9540:
  762. abx500_pinctrl_ab9540_init(&pct->soc);
  763. break;
  764. case PINCTRL_AB8505:
  765. abx500_pinctrl_ab8505_init(&pct->soc);
  766. break;
  767. default:
  768. dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n",
  769. (int) platid->driver_data);
  770. mutex_destroy(&pct->lock);
  771. return -EINVAL;
  772. }
  773. if (!pct->soc) {
  774. dev_err(&pdev->dev, "Invalid SOC data\n");
  775. mutex_destroy(&pct->lock);
  776. return -EINVAL;
  777. }
  778. pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
  779. pct->irq_cluster = pct->soc->gpio_irq_cluster;
  780. pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
  781. ret = gpiochip_add(&pct->chip);
  782. if (ret) {
  783. dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
  784. mutex_destroy(&pct->lock);
  785. return ret;
  786. }
  787. dev_info(&pdev->dev, "added gpiochip\n");
  788. abx500_pinctrl_desc.pins = pct->soc->pins;
  789. abx500_pinctrl_desc.npins = pct->soc->npins;
  790. pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
  791. if (!pct->pctldev) {
  792. dev_err(&pdev->dev,
  793. "could not register abx500 pinctrl driver\n");
  794. ret = -EINVAL;
  795. goto out_rem_chip;
  796. }
  797. dev_info(&pdev->dev, "registered pin controller\n");
  798. /* We will handle a range of GPIO pins */
  799. for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
  800. const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
  801. ret = gpiochip_add_pin_range(&pct->chip,
  802. dev_name(&pdev->dev),
  803. p->offset - 1, p->offset, p->npins);
  804. if (ret < 0)
  805. goto out_rem_chip;
  806. }
  807. platform_set_drvdata(pdev, pct);
  808. dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
  809. return 0;
  810. out_rem_chip:
  811. err = gpiochip_remove(&pct->chip);
  812. if (err)
  813. dev_info(&pdev->dev, "failed to remove gpiochip\n");
  814. mutex_destroy(&pct->lock);
  815. return ret;
  816. }
  817. /**
  818. * abx500_gpio_remove() - remove Ab8500-gpio driver
  819. * @pdev: Platform device registered
  820. */
  821. static int abx500_gpio_remove(struct platform_device *pdev)
  822. {
  823. struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
  824. int ret;
  825. ret = gpiochip_remove(&pct->chip);
  826. if (ret < 0) {
  827. dev_err(pct->dev, "unable to remove gpiochip: %d\n",
  828. ret);
  829. return ret;
  830. }
  831. mutex_destroy(&pct->lock);
  832. return 0;
  833. }
  834. static const struct platform_device_id abx500_pinctrl_id[] = {
  835. { "pinctrl-ab8500", PINCTRL_AB8500 },
  836. { "pinctrl-ab8540", PINCTRL_AB8540 },
  837. { "pinctrl-ab9540", PINCTRL_AB9540 },
  838. { "pinctrl-ab8505", PINCTRL_AB8505 },
  839. { },
  840. };
  841. static struct platform_driver abx500_gpio_driver = {
  842. .driver = {
  843. .name = "abx500-gpio",
  844. .owner = THIS_MODULE,
  845. .of_match_table = abx500_gpio_match,
  846. },
  847. .probe = abx500_gpio_probe,
  848. .remove = abx500_gpio_remove,
  849. .id_table = abx500_pinctrl_id,
  850. };
  851. static int __init abx500_gpio_init(void)
  852. {
  853. return platform_driver_register(&abx500_gpio_driver);
  854. }
  855. core_initcall(abx500_gpio_init);
  856. MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
  857. MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
  858. MODULE_ALIAS("platform:abx500-gpio");
  859. MODULE_LICENSE("GPL v2");