pinctrl-abx500.c 32 KB

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