pinctrl-abx500.c 32 KB

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