pinctrl-abx500.c 33 KB

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