pinctrl-abx500.c 28 KB

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