pinctrl-abx500.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2013
  3. *
  4. * Author: Patrice Chotard <patrice.chotard@st.com>
  5. * License terms: GNU General Public License (GPL) version 2
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/gpio.h>
  21. #include <linux/irq.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/bitops.h>
  25. #include <linux/mfd/abx500.h>
  26. #include <linux/mfd/abx500/ab8500.h>
  27. #include <linux/mfd/abx500/ab8500-gpio.h>
  28. #include <linux/pinctrl/pinctrl.h>
  29. #include <linux/pinctrl/consumer.h>
  30. #include <linux/pinctrl/pinmux.h>
  31. #include <linux/pinctrl/pinconf.h>
  32. #include <linux/pinctrl/pinconf-generic.h>
  33. #include <linux/pinctrl/machine.h>
  34. #include "pinctrl-abx500.h"
  35. #include "core.h"
  36. #include "pinconf.h"
  37. /*
  38. * The AB9540 and AB8540 GPIO support are extended versions
  39. * of the AB8500 GPIO support.
  40. * The AB9540 supports an additional (7th) register so that
  41. * more GPIO may be configured and used.
  42. * The AB8540 supports 4 new gpios (GPIOx_VBAT) that have
  43. * internal pull-up and pull-down capabilities.
  44. */
  45. /*
  46. * GPIO registers offset
  47. * Bank: 0x10
  48. */
  49. #define AB8500_GPIO_SEL1_REG 0x00
  50. #define AB8500_GPIO_SEL2_REG 0x01
  51. #define AB8500_GPIO_SEL3_REG 0x02
  52. #define AB8500_GPIO_SEL4_REG 0x03
  53. #define AB8500_GPIO_SEL5_REG 0x04
  54. #define AB8500_GPIO_SEL6_REG 0x05
  55. #define AB9540_GPIO_SEL7_REG 0x06
  56. #define AB8500_GPIO_DIR1_REG 0x10
  57. #define AB8500_GPIO_DIR2_REG 0x11
  58. #define AB8500_GPIO_DIR3_REG 0x12
  59. #define AB8500_GPIO_DIR4_REG 0x13
  60. #define AB8500_GPIO_DIR5_REG 0x14
  61. #define AB8500_GPIO_DIR6_REG 0x15
  62. #define AB9540_GPIO_DIR7_REG 0x16
  63. #define AB8500_GPIO_OUT1_REG 0x20
  64. #define AB8500_GPIO_OUT2_REG 0x21
  65. #define AB8500_GPIO_OUT3_REG 0x22
  66. #define AB8500_GPIO_OUT4_REG 0x23
  67. #define AB8500_GPIO_OUT5_REG 0x24
  68. #define AB8500_GPIO_OUT6_REG 0x25
  69. #define AB9540_GPIO_OUT7_REG 0x26
  70. #define AB8500_GPIO_PUD1_REG 0x30
  71. #define AB8500_GPIO_PUD2_REG 0x31
  72. #define AB8500_GPIO_PUD3_REG 0x32
  73. #define AB8500_GPIO_PUD4_REG 0x33
  74. #define AB8500_GPIO_PUD5_REG 0x34
  75. #define AB8500_GPIO_PUD6_REG 0x35
  76. #define AB9540_GPIO_PUD7_REG 0x36
  77. #define AB8500_GPIO_IN1_REG 0x40
  78. #define AB8500_GPIO_IN2_REG 0x41
  79. #define AB8500_GPIO_IN3_REG 0x42
  80. #define AB8500_GPIO_IN4_REG 0x43
  81. #define AB8500_GPIO_IN5_REG 0x44
  82. #define AB8500_GPIO_IN6_REG 0x45
  83. #define AB9540_GPIO_IN7_REG 0x46
  84. #define AB8540_GPIO_VINSEL_REG 0x47
  85. #define AB8540_GPIO_PULL_UPDOWN_REG 0x48
  86. #define AB8500_GPIO_ALTFUN_REG 0x50
  87. #define AB8540_GPIO_PULL_UPDOWN_MASK 0x03
  88. #define AB8540_GPIO_VINSEL_MASK 0x03
  89. #define AB8540_GPIOX_VBAT_START 51
  90. #define AB8540_GPIOX_VBAT_END 54
  91. #define ABX500_GPIO_INPUT 0
  92. #define ABX500_GPIO_OUTPUT 1
  93. struct abx500_pinctrl {
  94. struct device *dev;
  95. struct pinctrl_dev *pctldev;
  96. struct abx500_pinctrl_soc_data *soc;
  97. struct gpio_chip chip;
  98. struct ab8500 *parent;
  99. struct abx500_gpio_irq_cluster *irq_cluster;
  100. int irq_cluster_size;
  101. };
  102. /**
  103. * to_abx500_pinctrl() - get the pointer to abx500_pinctrl
  104. * @chip: Member of the structure abx500_pinctrl
  105. */
  106. static inline struct abx500_pinctrl *to_abx500_pinctrl(struct gpio_chip *chip)
  107. {
  108. return container_of(chip, struct abx500_pinctrl, chip);
  109. }
  110. static int abx500_gpio_get_bit(struct gpio_chip *chip, u8 reg,
  111. unsigned offset, bool *bit)
  112. {
  113. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  114. u8 pos = offset % 8;
  115. u8 val;
  116. int ret;
  117. reg += offset / 8;
  118. ret = abx500_get_register_interruptible(pct->dev,
  119. AB8500_MISC, reg, &val);
  120. *bit = !!(val & BIT(pos));
  121. if (ret < 0)
  122. dev_err(pct->dev,
  123. "%s read reg =%x, offset=%x failed (%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, "unknow 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. if (pctldev)
  534. mode = abx500_get_mode(pctldev, chip, offset);
  535. seq_printf(s, " %s", (mode < 0) ? "unknown" : modes[mode]);
  536. out:
  537. if (ret < 0)
  538. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  539. }
  540. static void abx500_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  541. {
  542. unsigned i;
  543. unsigned gpio = chip->base;
  544. struct abx500_pinctrl *pct = to_abx500_pinctrl(chip);
  545. struct pinctrl_dev *pctldev = pct->pctldev;
  546. for (i = 0; i < chip->ngpio; i++, gpio++) {
  547. /* On AB8500, there is no GPIO0, the first is the GPIO 1 */
  548. abx500_gpio_dbg_show_one(s, pctldev, chip, i + 1, gpio);
  549. seq_printf(s, "\n");
  550. }
  551. }
  552. #else
  553. static inline void abx500_gpio_dbg_show_one(struct seq_file *s,
  554. struct pinctrl_dev *pctldev,
  555. struct gpio_chip *chip,
  556. unsigned offset, unsigned gpio)
  557. {
  558. }
  559. #define abx500_gpio_dbg_show NULL
  560. #endif
  561. static int abx500_gpio_request(struct gpio_chip *chip, unsigned offset)
  562. {
  563. int gpio = chip->base + offset;
  564. return pinctrl_request_gpio(gpio);
  565. }
  566. static void abx500_gpio_free(struct gpio_chip *chip, unsigned offset)
  567. {
  568. int gpio = chip->base + offset;
  569. pinctrl_free_gpio(gpio);
  570. }
  571. static struct gpio_chip abx500gpio_chip = {
  572. .label = "abx500-gpio",
  573. .owner = THIS_MODULE,
  574. .request = abx500_gpio_request,
  575. .free = abx500_gpio_free,
  576. .direction_input = abx500_gpio_direction_input,
  577. .get = abx500_gpio_get,
  578. .direction_output = abx500_gpio_direction_output,
  579. .set = abx500_gpio_set,
  580. .to_irq = abx500_gpio_to_irq,
  581. .dbg_show = abx500_gpio_dbg_show,
  582. };
  583. static int abx500_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
  584. {
  585. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  586. return pct->soc->nfunctions;
  587. }
  588. static const char *abx500_pmx_get_func_name(struct pinctrl_dev *pctldev,
  589. unsigned function)
  590. {
  591. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  592. return pct->soc->functions[function].name;
  593. }
  594. static int abx500_pmx_get_func_groups(struct pinctrl_dev *pctldev,
  595. unsigned function,
  596. const char * const **groups,
  597. unsigned * const num_groups)
  598. {
  599. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  600. *groups = pct->soc->functions[function].groups;
  601. *num_groups = pct->soc->functions[function].ngroups;
  602. return 0;
  603. }
  604. static int abx500_pmx_enable(struct pinctrl_dev *pctldev, unsigned function,
  605. unsigned group)
  606. {
  607. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  608. struct gpio_chip *chip = &pct->chip;
  609. const struct abx500_pingroup *g;
  610. int i;
  611. int ret = 0;
  612. g = &pct->soc->groups[group];
  613. if (g->altsetting < 0)
  614. return -EINVAL;
  615. dev_dbg(pct->dev, "enable group %s, %u pins\n", g->name, g->npins);
  616. for (i = 0; i < g->npins; i++) {
  617. dev_dbg(pct->dev, "setting pin %d to altsetting %d\n",
  618. g->pins[i], g->altsetting);
  619. ret = abx500_set_mode(pctldev, chip, g->pins[i], g->altsetting);
  620. }
  621. if (ret < 0)
  622. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  623. return ret;
  624. }
  625. static void abx500_pmx_disable(struct pinctrl_dev *pctldev,
  626. unsigned function, unsigned group)
  627. {
  628. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  629. const struct abx500_pingroup *g;
  630. g = &pct->soc->groups[group];
  631. if (g->altsetting < 0)
  632. return;
  633. /* FIXME: poke out the mux, set the pin to some default state? */
  634. dev_dbg(pct->dev, "disable group %s, %u pins\n", g->name, g->npins);
  635. }
  636. static int abx500_gpio_request_enable(struct pinctrl_dev *pctldev,
  637. struct pinctrl_gpio_range *range,
  638. unsigned offset)
  639. {
  640. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  641. const struct abx500_pinrange *p;
  642. int ret;
  643. int i;
  644. /*
  645. * Different ranges have different ways to enable GPIO function on a
  646. * pin, so refer back to our local range type, where we handily define
  647. * what altfunc enables GPIO for a certain pin.
  648. */
  649. for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
  650. p = &pct->soc->gpio_ranges[i];
  651. if ((offset >= p->offset) &&
  652. (offset < (p->offset + p->npins)))
  653. break;
  654. }
  655. if (i == pct->soc->gpio_num_ranges) {
  656. dev_err(pct->dev, "%s failed to locate range\n", __func__);
  657. return -ENODEV;
  658. }
  659. dev_dbg(pct->dev, "enable GPIO by altfunc %d at gpio %d\n",
  660. p->altfunc, offset);
  661. ret = abx500_set_mode(pct->pctldev, &pct->chip,
  662. offset, p->altfunc);
  663. if (ret < 0)
  664. dev_err(pct->dev, "%s setting altfunc failed\n", __func__);
  665. return ret;
  666. }
  667. static void abx500_gpio_disable_free(struct pinctrl_dev *pctldev,
  668. struct pinctrl_gpio_range *range,
  669. unsigned offset)
  670. {
  671. }
  672. static const struct pinmux_ops abx500_pinmux_ops = {
  673. .get_functions_count = abx500_pmx_get_funcs_cnt,
  674. .get_function_name = abx500_pmx_get_func_name,
  675. .get_function_groups = abx500_pmx_get_func_groups,
  676. .enable = abx500_pmx_enable,
  677. .disable = abx500_pmx_disable,
  678. .gpio_request_enable = abx500_gpio_request_enable,
  679. .gpio_disable_free = abx500_gpio_disable_free,
  680. };
  681. static int abx500_get_groups_cnt(struct pinctrl_dev *pctldev)
  682. {
  683. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  684. return pct->soc->ngroups;
  685. }
  686. static const char *abx500_get_group_name(struct pinctrl_dev *pctldev,
  687. unsigned selector)
  688. {
  689. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  690. return pct->soc->groups[selector].name;
  691. }
  692. static int abx500_get_group_pins(struct pinctrl_dev *pctldev,
  693. unsigned selector,
  694. const unsigned **pins,
  695. unsigned *num_pins)
  696. {
  697. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  698. *pins = pct->soc->groups[selector].pins;
  699. *num_pins = pct->soc->groups[selector].npins;
  700. return 0;
  701. }
  702. static void abx500_pin_dbg_show(struct pinctrl_dev *pctldev,
  703. struct seq_file *s, unsigned offset)
  704. {
  705. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  706. struct gpio_chip *chip = &pct->chip;
  707. abx500_gpio_dbg_show_one(s, pctldev, chip, offset,
  708. chip->base + offset - 1);
  709. }
  710. static void abx500_dt_free_map(struct pinctrl_dev *pctldev,
  711. struct pinctrl_map *map, unsigned num_maps)
  712. {
  713. int i;
  714. for (i = 0; i < num_maps; i++)
  715. if (map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  716. kfree(map[i].data.configs.configs);
  717. kfree(map);
  718. }
  719. static int abx500_dt_reserve_map(struct pinctrl_map **map,
  720. unsigned *reserved_maps,
  721. unsigned *num_maps,
  722. unsigned reserve)
  723. {
  724. unsigned old_num = *reserved_maps;
  725. unsigned new_num = *num_maps + reserve;
  726. struct pinctrl_map *new_map;
  727. if (old_num >= new_num)
  728. return 0;
  729. new_map = krealloc(*map, sizeof(*new_map) * new_num, GFP_KERNEL);
  730. if (!new_map)
  731. return -ENOMEM;
  732. memset(new_map + old_num, 0, (new_num - old_num) * sizeof(*new_map));
  733. *map = new_map;
  734. *reserved_maps = new_num;
  735. return 0;
  736. }
  737. static int abx500_dt_add_map_mux(struct pinctrl_map **map,
  738. unsigned *reserved_maps,
  739. unsigned *num_maps, const char *group,
  740. const char *function)
  741. {
  742. if (*num_maps == *reserved_maps)
  743. return -ENOSPC;
  744. (*map)[*num_maps].type = PIN_MAP_TYPE_MUX_GROUP;
  745. (*map)[*num_maps].data.mux.group = group;
  746. (*map)[*num_maps].data.mux.function = function;
  747. (*num_maps)++;
  748. return 0;
  749. }
  750. static int abx500_dt_add_map_configs(struct pinctrl_map **map,
  751. unsigned *reserved_maps,
  752. unsigned *num_maps, const char *group,
  753. unsigned long *configs, unsigned num_configs)
  754. {
  755. unsigned long *dup_configs;
  756. if (*num_maps == *reserved_maps)
  757. return -ENOSPC;
  758. dup_configs = kmemdup(configs, num_configs * sizeof(*dup_configs),
  759. GFP_KERNEL);
  760. if (!dup_configs)
  761. return -ENOMEM;
  762. (*map)[*num_maps].type = PIN_MAP_TYPE_CONFIGS_PIN;
  763. (*map)[*num_maps].data.configs.group_or_pin = group;
  764. (*map)[*num_maps].data.configs.configs = dup_configs;
  765. (*map)[*num_maps].data.configs.num_configs = num_configs;
  766. (*num_maps)++;
  767. return 0;
  768. }
  769. static const char *abx500_find_pin_name(struct pinctrl_dev *pctldev,
  770. const char *pin_name)
  771. {
  772. int i, pin_number;
  773. struct abx500_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
  774. if (sscanf((char *)pin_name, "GPIO%d", &pin_number) == 1)
  775. for (i = 0; i < npct->soc->npins; i++)
  776. if (npct->soc->pins[i].number == pin_number)
  777. return npct->soc->pins[i].name;
  778. return NULL;
  779. }
  780. static int abx500_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  781. struct device_node *np,
  782. struct pinctrl_map **map,
  783. unsigned *reserved_maps,
  784. unsigned *num_maps)
  785. {
  786. int ret;
  787. const char *function = NULL;
  788. unsigned long *configs;
  789. unsigned int nconfigs = 0;
  790. bool has_config = 0;
  791. unsigned reserve = 0;
  792. struct property *prop;
  793. const char *group, *gpio_name;
  794. struct device_node *np_config;
  795. ret = of_property_read_string(np, "ste,function", &function);
  796. if (ret >= 0)
  797. reserve = 1;
  798. ret = pinconf_generic_parse_dt_config(np, &configs, &nconfigs);
  799. if (nconfigs)
  800. has_config = 1;
  801. np_config = of_parse_phandle(np, "ste,config", 0);
  802. if (np_config) {
  803. ret = pinconf_generic_parse_dt_config(np_config, &configs,
  804. &nconfigs);
  805. if (ret)
  806. goto exit;
  807. has_config |= nconfigs;
  808. }
  809. ret = of_property_count_strings(np, "ste,pins");
  810. if (ret < 0)
  811. goto exit;
  812. if (has_config)
  813. reserve++;
  814. reserve *= ret;
  815. ret = abx500_dt_reserve_map(map, reserved_maps, num_maps, reserve);
  816. if (ret < 0)
  817. goto exit;
  818. of_property_for_each_string(np, "ste,pins", prop, group) {
  819. if (function) {
  820. ret = abx500_dt_add_map_mux(map, reserved_maps,
  821. num_maps, group, function);
  822. if (ret < 0)
  823. goto exit;
  824. }
  825. if (has_config) {
  826. gpio_name = abx500_find_pin_name(pctldev, group);
  827. ret = abx500_dt_add_map_configs(map, reserved_maps,
  828. num_maps, gpio_name, configs, 1);
  829. if (ret < 0)
  830. goto exit;
  831. }
  832. }
  833. exit:
  834. return ret;
  835. }
  836. static int abx500_dt_node_to_map(struct pinctrl_dev *pctldev,
  837. struct device_node *np_config,
  838. struct pinctrl_map **map, unsigned *num_maps)
  839. {
  840. unsigned reserved_maps;
  841. struct device_node *np;
  842. int ret;
  843. reserved_maps = 0;
  844. *map = NULL;
  845. *num_maps = 0;
  846. for_each_child_of_node(np_config, np) {
  847. ret = abx500_dt_subnode_to_map(pctldev, np, map,
  848. &reserved_maps, num_maps);
  849. if (ret < 0) {
  850. abx500_dt_free_map(pctldev, *map, *num_maps);
  851. return ret;
  852. }
  853. }
  854. return 0;
  855. }
  856. static const struct pinctrl_ops abx500_pinctrl_ops = {
  857. .get_groups_count = abx500_get_groups_cnt,
  858. .get_group_name = abx500_get_group_name,
  859. .get_group_pins = abx500_get_group_pins,
  860. .pin_dbg_show = abx500_pin_dbg_show,
  861. .dt_node_to_map = abx500_dt_node_to_map,
  862. .dt_free_map = abx500_dt_free_map,
  863. };
  864. static int abx500_pin_config_get(struct pinctrl_dev *pctldev,
  865. unsigned pin,
  866. unsigned long *config)
  867. {
  868. return -ENOSYS;
  869. }
  870. static int abx500_pin_config_set(struct pinctrl_dev *pctldev,
  871. unsigned pin,
  872. unsigned long *configs,
  873. unsigned num_configs)
  874. {
  875. struct abx500_pinctrl *pct = pinctrl_dev_get_drvdata(pctldev);
  876. struct gpio_chip *chip = &pct->chip;
  877. unsigned offset;
  878. int ret = -EINVAL;
  879. int i;
  880. enum pin_config_param param;
  881. enum pin_config_param argument;
  882. for (i = 0; i < num_configs; i++) {
  883. param = pinconf_to_config_param(configs[i]);
  884. argument = pinconf_to_config_argument(configs[i]);
  885. dev_dbg(chip->dev, "pin %d [%#lx]: %s %s\n",
  886. pin, configs[i],
  887. (param == PIN_CONFIG_OUTPUT) ? "output " : "input",
  888. (param == PIN_CONFIG_OUTPUT) ?
  889. (argument ? "high" : "low") :
  890. (argument ? "pull up" : "pull down"));
  891. /* on ABx500, there is no GPIO0, so adjust the offset */
  892. offset = pin - 1;
  893. switch (param) {
  894. case PIN_CONFIG_BIAS_DISABLE:
  895. ret = abx500_gpio_direction_input(chip, offset);
  896. if (ret < 0)
  897. goto out;
  898. /*
  899. * Some chips only support pull down, while some
  900. * actually support both pull up and pull down. Such
  901. * chips have a "pullud" range specified for the pins
  902. * that support both features. If the pin is not
  903. * within that range, we fall back to the old bit set
  904. * that only support pull down.
  905. */
  906. if (abx500_pullud_supported(chip, pin))
  907. ret = abx500_set_pull_updown(pct,
  908. pin,
  909. ABX500_GPIO_PULL_NONE);
  910. else
  911. /* Chip only supports pull down */
  912. ret = abx500_gpio_set_bits(chip,
  913. AB8500_GPIO_PUD1_REG, offset,
  914. ABX500_GPIO_PULL_NONE);
  915. break;
  916. case PIN_CONFIG_BIAS_PULL_DOWN:
  917. ret = abx500_gpio_direction_input(chip, offset);
  918. if (ret < 0)
  919. goto out;
  920. /*
  921. * if argument = 1 set the pull down
  922. * else clear the pull down
  923. * Some chips only support pull down, while some
  924. * actually support both pull up and pull down. Such
  925. * chips have a "pullud" range specified for the pins
  926. * that support both features. If the pin is not
  927. * within that range, we fall back to the old bit set
  928. * that only support pull down.
  929. */
  930. if (abx500_pullud_supported(chip, pin))
  931. ret = abx500_set_pull_updown(pct,
  932. pin,
  933. argument ? ABX500_GPIO_PULL_DOWN :
  934. ABX500_GPIO_PULL_NONE);
  935. else
  936. /* Chip only supports pull down */
  937. ret = abx500_gpio_set_bits(chip,
  938. AB8500_GPIO_PUD1_REG,
  939. offset,
  940. argument ? ABX500_GPIO_PULL_DOWN :
  941. ABX500_GPIO_PULL_NONE);
  942. break;
  943. case PIN_CONFIG_BIAS_PULL_UP:
  944. ret = abx500_gpio_direction_input(chip, offset);
  945. if (ret < 0)
  946. goto out;
  947. /*
  948. * if argument = 1 set the pull up
  949. * else clear the pull up
  950. */
  951. ret = abx500_gpio_direction_input(chip, offset);
  952. /*
  953. * Some chips only support pull down, while some
  954. * actually support both pull up and pull down. Such
  955. * chips have a "pullud" range specified for the pins
  956. * that support both features. If the pin is not
  957. * within that range, do nothing
  958. */
  959. if (abx500_pullud_supported(chip, pin))
  960. ret = abx500_set_pull_updown(pct,
  961. pin,
  962. argument ? ABX500_GPIO_PULL_UP :
  963. ABX500_GPIO_PULL_NONE);
  964. break;
  965. case PIN_CONFIG_OUTPUT:
  966. ret = abx500_gpio_direction_output(chip, offset,
  967. argument);
  968. break;
  969. default:
  970. dev_err(chip->dev, "illegal configuration requested\n");
  971. }
  972. } /* for each config */
  973. out:
  974. if (ret < 0)
  975. dev_err(pct->dev, "%s failed (%d)\n", __func__, ret);
  976. return ret;
  977. }
  978. static const struct pinconf_ops abx500_pinconf_ops = {
  979. .pin_config_get = abx500_pin_config_get,
  980. .pin_config_set = abx500_pin_config_set,
  981. };
  982. static struct pinctrl_desc abx500_pinctrl_desc = {
  983. .name = "pinctrl-abx500",
  984. .pctlops = &abx500_pinctrl_ops,
  985. .pmxops = &abx500_pinmux_ops,
  986. .confops = &abx500_pinconf_ops,
  987. .owner = THIS_MODULE,
  988. };
  989. static int abx500_get_gpio_num(struct abx500_pinctrl_soc_data *soc)
  990. {
  991. unsigned int lowest = 0;
  992. unsigned int highest = 0;
  993. unsigned int npins = 0;
  994. int i;
  995. /*
  996. * Compute number of GPIOs from the last SoC gpio range descriptors
  997. * These ranges may include "holes" but the GPIO number space shall
  998. * still be homogeneous, so we need to detect and account for any
  999. * such holes so that these are included in the number of GPIO pins.
  1000. */
  1001. for (i = 0; i < soc->gpio_num_ranges; i++) {
  1002. unsigned gstart;
  1003. unsigned gend;
  1004. const struct abx500_pinrange *p;
  1005. p = &soc->gpio_ranges[i];
  1006. gstart = p->offset;
  1007. gend = p->offset + p->npins - 1;
  1008. if (i == 0) {
  1009. /* First iteration, set start values */
  1010. lowest = gstart;
  1011. highest = gend;
  1012. } else {
  1013. if (gstart < lowest)
  1014. lowest = gstart;
  1015. if (gend > highest)
  1016. highest = gend;
  1017. }
  1018. }
  1019. /* this gives the absolute number of pins */
  1020. npins = highest - lowest + 1;
  1021. return npins;
  1022. }
  1023. static const struct of_device_id abx500_gpio_match[] = {
  1024. { .compatible = "stericsson,ab8500-gpio", .data = (void *)PINCTRL_AB8500, },
  1025. { .compatible = "stericsson,ab8505-gpio", .data = (void *)PINCTRL_AB8505, },
  1026. { .compatible = "stericsson,ab8540-gpio", .data = (void *)PINCTRL_AB8540, },
  1027. { .compatible = "stericsson,ab9540-gpio", .data = (void *)PINCTRL_AB9540, },
  1028. { }
  1029. };
  1030. static int abx500_gpio_probe(struct platform_device *pdev)
  1031. {
  1032. struct ab8500_platform_data *abx500_pdata =
  1033. dev_get_platdata(pdev->dev.parent);
  1034. struct abx500_gpio_platform_data *pdata = NULL;
  1035. struct device_node *np = pdev->dev.of_node;
  1036. struct abx500_pinctrl *pct;
  1037. const struct platform_device_id *platid = platform_get_device_id(pdev);
  1038. unsigned int id = -1;
  1039. int ret, err;
  1040. int i;
  1041. if (abx500_pdata)
  1042. pdata = abx500_pdata->gpio;
  1043. if (!(pdata || np)) {
  1044. dev_err(&pdev->dev, "gpio dt and platform data missing\n");
  1045. return -ENODEV;
  1046. }
  1047. pct = devm_kzalloc(&pdev->dev, sizeof(struct abx500_pinctrl),
  1048. GFP_KERNEL);
  1049. if (pct == NULL) {
  1050. dev_err(&pdev->dev,
  1051. "failed to allocate memory for pct\n");
  1052. return -ENOMEM;
  1053. }
  1054. pct->dev = &pdev->dev;
  1055. pct->parent = dev_get_drvdata(pdev->dev.parent);
  1056. pct->chip = abx500gpio_chip;
  1057. pct->chip.dev = &pdev->dev;
  1058. pct->chip.base = (np) ? -1 : pdata->gpio_base;
  1059. if (platid)
  1060. id = platid->driver_data;
  1061. else if (np) {
  1062. const struct of_device_id *match;
  1063. match = of_match_device(abx500_gpio_match, &pdev->dev);
  1064. if (match)
  1065. id = (unsigned long)match->data;
  1066. }
  1067. /* Poke in other ASIC variants here */
  1068. switch (id) {
  1069. case PINCTRL_AB8500:
  1070. abx500_pinctrl_ab8500_init(&pct->soc);
  1071. break;
  1072. case PINCTRL_AB8540:
  1073. abx500_pinctrl_ab8540_init(&pct->soc);
  1074. break;
  1075. case PINCTRL_AB9540:
  1076. abx500_pinctrl_ab9540_init(&pct->soc);
  1077. break;
  1078. case PINCTRL_AB8505:
  1079. abx500_pinctrl_ab8505_init(&pct->soc);
  1080. break;
  1081. default:
  1082. dev_err(&pdev->dev, "Unsupported pinctrl sub driver (%d)\n", id);
  1083. return -EINVAL;
  1084. }
  1085. if (!pct->soc) {
  1086. dev_err(&pdev->dev, "Invalid SOC data\n");
  1087. return -EINVAL;
  1088. }
  1089. pct->chip.ngpio = abx500_get_gpio_num(pct->soc);
  1090. pct->irq_cluster = pct->soc->gpio_irq_cluster;
  1091. pct->irq_cluster_size = pct->soc->ngpio_irq_cluster;
  1092. ret = gpiochip_add(&pct->chip);
  1093. if (ret) {
  1094. dev_err(&pdev->dev, "unable to add gpiochip: %d\n", ret);
  1095. return ret;
  1096. }
  1097. dev_info(&pdev->dev, "added gpiochip\n");
  1098. abx500_pinctrl_desc.pins = pct->soc->pins;
  1099. abx500_pinctrl_desc.npins = pct->soc->npins;
  1100. pct->pctldev = pinctrl_register(&abx500_pinctrl_desc, &pdev->dev, pct);
  1101. if (!pct->pctldev) {
  1102. dev_err(&pdev->dev,
  1103. "could not register abx500 pinctrl driver\n");
  1104. ret = -EINVAL;
  1105. goto out_rem_chip;
  1106. }
  1107. dev_info(&pdev->dev, "registered pin controller\n");
  1108. /* We will handle a range of GPIO pins */
  1109. for (i = 0; i < pct->soc->gpio_num_ranges; i++) {
  1110. const struct abx500_pinrange *p = &pct->soc->gpio_ranges[i];
  1111. ret = gpiochip_add_pin_range(&pct->chip,
  1112. dev_name(&pdev->dev),
  1113. p->offset - 1, p->offset, p->npins);
  1114. if (ret < 0)
  1115. goto out_rem_chip;
  1116. }
  1117. platform_set_drvdata(pdev, pct);
  1118. dev_info(&pdev->dev, "initialized abx500 pinctrl driver\n");
  1119. return 0;
  1120. out_rem_chip:
  1121. err = gpiochip_remove(&pct->chip);
  1122. if (err)
  1123. dev_info(&pdev->dev, "failed to remove gpiochip\n");
  1124. return ret;
  1125. }
  1126. /**
  1127. * abx500_gpio_remove() - remove Ab8500-gpio driver
  1128. * @pdev: Platform device registered
  1129. */
  1130. static int abx500_gpio_remove(struct platform_device *pdev)
  1131. {
  1132. struct abx500_pinctrl *pct = platform_get_drvdata(pdev);
  1133. int ret;
  1134. ret = gpiochip_remove(&pct->chip);
  1135. if (ret < 0) {
  1136. dev_err(pct->dev, "unable to remove gpiochip: %d\n",
  1137. ret);
  1138. return ret;
  1139. }
  1140. return 0;
  1141. }
  1142. static const struct platform_device_id abx500_pinctrl_id[] = {
  1143. { "pinctrl-ab8500", PINCTRL_AB8500 },
  1144. { "pinctrl-ab8540", PINCTRL_AB8540 },
  1145. { "pinctrl-ab9540", PINCTRL_AB9540 },
  1146. { "pinctrl-ab8505", PINCTRL_AB8505 },
  1147. { },
  1148. };
  1149. static struct platform_driver abx500_gpio_driver = {
  1150. .driver = {
  1151. .name = "abx500-gpio",
  1152. .owner = THIS_MODULE,
  1153. .of_match_table = abx500_gpio_match,
  1154. },
  1155. .probe = abx500_gpio_probe,
  1156. .remove = abx500_gpio_remove,
  1157. .id_table = abx500_pinctrl_id,
  1158. };
  1159. static int __init abx500_gpio_init(void)
  1160. {
  1161. return platform_driver_register(&abx500_gpio_driver);
  1162. }
  1163. core_initcall(abx500_gpio_init);
  1164. MODULE_AUTHOR("Patrice Chotard <patrice.chotard@st.com>");
  1165. MODULE_DESCRIPTION("Driver allows to use AxB5xx unused pins to be used as GPIO");
  1166. MODULE_ALIAS("platform:abx500-gpio");
  1167. MODULE_LICENSE("GPL v2");