pinctrl-abx500.c 27 KB

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