pinctrl-abx500.c 26 KB

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