pinctrl-abx500.c 26 KB

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