pinctrl-abx500.c 34 KB

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