pinctrl-abx500.c 27 KB

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