pinctrl-exynos5440.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. /*
  2. * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's EXYNOS5440 SoC.
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  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 as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/io.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/gpio.h>
  18. #include <linux/device.h>
  19. #include <linux/pinctrl/pinctrl.h>
  20. #include <linux/pinctrl/pinmux.h>
  21. #include <linux/pinctrl/pinconf.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/of_irq.h>
  25. #include "../core.h"
  26. /* EXYNOS5440 GPIO and Pinctrl register offsets */
  27. #define GPIO_MUX 0x00
  28. #define GPIO_IE 0x04
  29. #define GPIO_INT 0x08
  30. #define GPIO_TYPE 0x0C
  31. #define GPIO_VAL 0x10
  32. #define GPIO_OE 0x14
  33. #define GPIO_IN 0x18
  34. #define GPIO_PE 0x1C
  35. #define GPIO_PS 0x20
  36. #define GPIO_SR 0x24
  37. #define GPIO_DS0 0x28
  38. #define GPIO_DS1 0x2C
  39. #define EXYNOS5440_MAX_PINS 23
  40. #define EXYNOS5440_MAX_GPIO_INT 8
  41. #define PIN_NAME_LENGTH 10
  42. #define GROUP_SUFFIX "-grp"
  43. #define GSUFFIX_LEN sizeof(GROUP_SUFFIX)
  44. #define FUNCTION_SUFFIX "-mux"
  45. #define FSUFFIX_LEN sizeof(FUNCTION_SUFFIX)
  46. /*
  47. * pin configuration type and its value are packed together into a 16-bits.
  48. * The upper 8-bits represent the configuration type and the lower 8-bits
  49. * hold the value of the configuration type.
  50. */
  51. #define PINCFG_TYPE_MASK 0xFF
  52. #define PINCFG_VALUE_SHIFT 8
  53. #define PINCFG_VALUE_MASK (0xFF << PINCFG_VALUE_SHIFT)
  54. #define PINCFG_PACK(type, value) (((value) << PINCFG_VALUE_SHIFT) | type)
  55. #define PINCFG_UNPACK_TYPE(cfg) ((cfg) & PINCFG_TYPE_MASK)
  56. #define PINCFG_UNPACK_VALUE(cfg) (((cfg) & PINCFG_VALUE_MASK) >> \
  57. PINCFG_VALUE_SHIFT)
  58. /**
  59. * enum pincfg_type - possible pin configuration types supported.
  60. * @PINCFG_TYPE_PUD: Pull up/down configuration.
  61. * @PINCFG_TYPE_DRV: Drive strength configuration.
  62. * @PINCFG_TYPE_SKEW_RATE: Skew rate configuration.
  63. * @PINCFG_TYPE_INPUT_TYPE: Pin input type configuration.
  64. */
  65. enum pincfg_type {
  66. PINCFG_TYPE_PUD,
  67. PINCFG_TYPE_DRV,
  68. PINCFG_TYPE_SKEW_RATE,
  69. PINCFG_TYPE_INPUT_TYPE
  70. };
  71. /**
  72. * struct exynos5440_pin_group: represent group of pins for pincfg setting.
  73. * @name: name of the pin group, used to lookup the group.
  74. * @pins: the pins included in this group.
  75. * @num_pins: number of pins included in this group.
  76. */
  77. struct exynos5440_pin_group {
  78. const char *name;
  79. const unsigned int *pins;
  80. u8 num_pins;
  81. };
  82. /**
  83. * struct exynos5440_pmx_func: represent a pin function.
  84. * @name: name of the pin function, used to lookup the function.
  85. * @groups: one or more names of pin groups that provide this function.
  86. * @num_groups: number of groups included in @groups.
  87. * @function: the function number to be programmed when selected.
  88. */
  89. struct exynos5440_pmx_func {
  90. const char *name;
  91. const char **groups;
  92. u8 num_groups;
  93. unsigned long function;
  94. };
  95. /**
  96. * struct exynos5440_pinctrl_priv_data: driver's private runtime data.
  97. * @reg_base: ioremapped based address of the register space.
  98. * @gc: gpio chip registered with gpiolib.
  99. * @pin_groups: list of pin groups parsed from device tree.
  100. * @nr_groups: number of pin groups available.
  101. * @pmx_functions: list of pin functions parsed from device tree.
  102. * @nr_functions: number of pin functions available.
  103. */
  104. struct exynos5440_pinctrl_priv_data {
  105. void __iomem *reg_base;
  106. struct gpio_chip *gc;
  107. struct irq_domain *irq_domain;
  108. const struct exynos5440_pin_group *pin_groups;
  109. unsigned int nr_groups;
  110. const struct exynos5440_pmx_func *pmx_functions;
  111. unsigned int nr_functions;
  112. };
  113. /**
  114. * struct exynos5440_gpio_intr_data: private data for gpio interrupts.
  115. * @priv: driver's private runtime data.
  116. * @gpio_int: gpio interrupt number.
  117. */
  118. struct exynos5440_gpio_intr_data {
  119. struct exynos5440_pinctrl_priv_data *priv;
  120. unsigned int gpio_int;
  121. };
  122. /* list of all possible config options supported */
  123. static struct pin_config {
  124. char *prop_cfg;
  125. unsigned int cfg_type;
  126. } pcfgs[] = {
  127. { "samsung,exynos5440-pin-pud", PINCFG_TYPE_PUD },
  128. { "samsung,exynos5440-pin-drv", PINCFG_TYPE_DRV },
  129. { "samsung,exynos5440-pin-skew-rate", PINCFG_TYPE_SKEW_RATE },
  130. { "samsung,exynos5440-pin-input-type", PINCFG_TYPE_INPUT_TYPE },
  131. };
  132. /* check if the selector is a valid pin group selector */
  133. static int exynos5440_get_group_count(struct pinctrl_dev *pctldev)
  134. {
  135. struct exynos5440_pinctrl_priv_data *priv;
  136. priv = pinctrl_dev_get_drvdata(pctldev);
  137. return priv->nr_groups;
  138. }
  139. /* return the name of the group selected by the group selector */
  140. static const char *exynos5440_get_group_name(struct pinctrl_dev *pctldev,
  141. unsigned selector)
  142. {
  143. struct exynos5440_pinctrl_priv_data *priv;
  144. priv = pinctrl_dev_get_drvdata(pctldev);
  145. return priv->pin_groups[selector].name;
  146. }
  147. /* return the pin numbers associated with the specified group */
  148. static int exynos5440_get_group_pins(struct pinctrl_dev *pctldev,
  149. unsigned selector, const unsigned **pins, unsigned *num_pins)
  150. {
  151. struct exynos5440_pinctrl_priv_data *priv;
  152. priv = pinctrl_dev_get_drvdata(pctldev);
  153. *pins = priv->pin_groups[selector].pins;
  154. *num_pins = priv->pin_groups[selector].num_pins;
  155. return 0;
  156. }
  157. /* create pinctrl_map entries by parsing device tree nodes */
  158. static int exynos5440_dt_node_to_map(struct pinctrl_dev *pctldev,
  159. struct device_node *np, struct pinctrl_map **maps,
  160. unsigned *nmaps)
  161. {
  162. struct device *dev = pctldev->dev;
  163. struct pinctrl_map *map;
  164. unsigned long *cfg = NULL;
  165. char *gname, *fname;
  166. int cfg_cnt = 0, map_cnt = 0, idx = 0;
  167. /* count the number of config options specfied in the node */
  168. for (idx = 0; idx < ARRAY_SIZE(pcfgs); idx++)
  169. if (of_find_property(np, pcfgs[idx].prop_cfg, NULL))
  170. cfg_cnt++;
  171. /*
  172. * Find out the number of map entries to create. All the config options
  173. * can be accomadated into a single config map entry.
  174. */
  175. if (cfg_cnt)
  176. map_cnt = 1;
  177. if (of_find_property(np, "samsung,exynos5440-pin-function", NULL))
  178. map_cnt++;
  179. if (!map_cnt) {
  180. dev_err(dev, "node %s does not have either config or function "
  181. "configurations\n", np->name);
  182. return -EINVAL;
  183. }
  184. /* Allocate memory for pin-map entries */
  185. map = kzalloc(sizeof(*map) * map_cnt, GFP_KERNEL);
  186. if (!map) {
  187. dev_err(dev, "could not alloc memory for pin-maps\n");
  188. return -ENOMEM;
  189. }
  190. *nmaps = 0;
  191. /*
  192. * Allocate memory for pin group name. The pin group name is derived
  193. * from the node name from which these map entries are be created.
  194. */
  195. gname = kzalloc(strlen(np->name) + GSUFFIX_LEN, GFP_KERNEL);
  196. if (!gname) {
  197. dev_err(dev, "failed to alloc memory for group name\n");
  198. goto free_map;
  199. }
  200. snprintf(gname, strlen(np->name) + 4, "%s%s", np->name, GROUP_SUFFIX);
  201. /*
  202. * don't have config options? then skip over to creating function
  203. * map entries.
  204. */
  205. if (!cfg_cnt)
  206. goto skip_cfgs;
  207. /* Allocate memory for config entries */
  208. cfg = kzalloc(sizeof(*cfg) * cfg_cnt, GFP_KERNEL);
  209. if (!cfg) {
  210. dev_err(dev, "failed to alloc memory for configs\n");
  211. goto free_gname;
  212. }
  213. /* Prepare a list of config settings */
  214. for (idx = 0, cfg_cnt = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
  215. u32 value;
  216. if (!of_property_read_u32(np, pcfgs[idx].prop_cfg, &value))
  217. cfg[cfg_cnt++] =
  218. PINCFG_PACK(pcfgs[idx].cfg_type, value);
  219. }
  220. /* create the config map entry */
  221. map[*nmaps].data.configs.group_or_pin = gname;
  222. map[*nmaps].data.configs.configs = cfg;
  223. map[*nmaps].data.configs.num_configs = cfg_cnt;
  224. map[*nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  225. *nmaps += 1;
  226. skip_cfgs:
  227. /* create the function map entry */
  228. if (of_find_property(np, "samsung,exynos5440-pin-function", NULL)) {
  229. fname = kzalloc(strlen(np->name) + FSUFFIX_LEN, GFP_KERNEL);
  230. if (!fname) {
  231. dev_err(dev, "failed to alloc memory for func name\n");
  232. goto free_cfg;
  233. }
  234. snprintf(fname, strlen(np->name) + 4, "%s%s", np->name,
  235. FUNCTION_SUFFIX);
  236. map[*nmaps].data.mux.group = gname;
  237. map[*nmaps].data.mux.function = fname;
  238. map[*nmaps].type = PIN_MAP_TYPE_MUX_GROUP;
  239. *nmaps += 1;
  240. }
  241. *maps = map;
  242. return 0;
  243. free_cfg:
  244. kfree(cfg);
  245. free_gname:
  246. kfree(gname);
  247. free_map:
  248. kfree(map);
  249. return -ENOMEM;
  250. }
  251. /* free the memory allocated to hold the pin-map table */
  252. static void exynos5440_dt_free_map(struct pinctrl_dev *pctldev,
  253. struct pinctrl_map *map, unsigned num_maps)
  254. {
  255. int idx;
  256. for (idx = 0; idx < num_maps; idx++) {
  257. if (map[idx].type == PIN_MAP_TYPE_MUX_GROUP) {
  258. kfree(map[idx].data.mux.function);
  259. if (!idx)
  260. kfree(map[idx].data.mux.group);
  261. } else if (map->type == PIN_MAP_TYPE_CONFIGS_GROUP) {
  262. kfree(map[idx].data.configs.configs);
  263. if (!idx)
  264. kfree(map[idx].data.configs.group_or_pin);
  265. }
  266. };
  267. kfree(map);
  268. }
  269. /* list of pinctrl callbacks for the pinctrl core */
  270. static const struct pinctrl_ops exynos5440_pctrl_ops = {
  271. .get_groups_count = exynos5440_get_group_count,
  272. .get_group_name = exynos5440_get_group_name,
  273. .get_group_pins = exynos5440_get_group_pins,
  274. .dt_node_to_map = exynos5440_dt_node_to_map,
  275. .dt_free_map = exynos5440_dt_free_map,
  276. };
  277. /* check if the selector is a valid pin function selector */
  278. static int exynos5440_get_functions_count(struct pinctrl_dev *pctldev)
  279. {
  280. struct exynos5440_pinctrl_priv_data *priv;
  281. priv = pinctrl_dev_get_drvdata(pctldev);
  282. return priv->nr_functions;
  283. }
  284. /* return the name of the pin function specified */
  285. static const char *exynos5440_pinmux_get_fname(struct pinctrl_dev *pctldev,
  286. unsigned selector)
  287. {
  288. struct exynos5440_pinctrl_priv_data *priv;
  289. priv = pinctrl_dev_get_drvdata(pctldev);
  290. return priv->pmx_functions[selector].name;
  291. }
  292. /* return the groups associated for the specified function selector */
  293. static int exynos5440_pinmux_get_groups(struct pinctrl_dev *pctldev,
  294. unsigned selector, const char * const **groups,
  295. unsigned * const num_groups)
  296. {
  297. struct exynos5440_pinctrl_priv_data *priv;
  298. priv = pinctrl_dev_get_drvdata(pctldev);
  299. *groups = priv->pmx_functions[selector].groups;
  300. *num_groups = priv->pmx_functions[selector].num_groups;
  301. return 0;
  302. }
  303. /* enable or disable a pinmux function */
  304. static void exynos5440_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
  305. unsigned group, bool enable)
  306. {
  307. struct exynos5440_pinctrl_priv_data *priv;
  308. void __iomem *base;
  309. u32 function;
  310. u32 data;
  311. priv = pinctrl_dev_get_drvdata(pctldev);
  312. base = priv->reg_base;
  313. function = priv->pmx_functions[selector].function;
  314. data = readl(base + GPIO_MUX);
  315. if (enable)
  316. data |= (1 << function);
  317. else
  318. data &= ~(1 << function);
  319. writel(data, base + GPIO_MUX);
  320. }
  321. /* enable a specified pinmux by writing to registers */
  322. static int exynos5440_pinmux_set_mux(struct pinctrl_dev *pctldev,
  323. unsigned selector,
  324. unsigned group)
  325. {
  326. exynos5440_pinmux_setup(pctldev, selector, group, true);
  327. return 0;
  328. }
  329. /*
  330. * The calls to gpio_direction_output() and gpio_direction_input()
  331. * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
  332. * function called from the gpiolib interface).
  333. */
  334. static int exynos5440_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
  335. struct pinctrl_gpio_range *range, unsigned offset, bool input)
  336. {
  337. return 0;
  338. }
  339. /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
  340. static const struct pinmux_ops exynos5440_pinmux_ops = {
  341. .get_functions_count = exynos5440_get_functions_count,
  342. .get_function_name = exynos5440_pinmux_get_fname,
  343. .get_function_groups = exynos5440_pinmux_get_groups,
  344. .set_mux = exynos5440_pinmux_set_mux,
  345. .gpio_set_direction = exynos5440_pinmux_gpio_set_direction,
  346. };
  347. /* set the pin config settings for a specified pin */
  348. static int exynos5440_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  349. unsigned long *configs,
  350. unsigned num_configs)
  351. {
  352. struct exynos5440_pinctrl_priv_data *priv;
  353. void __iomem *base;
  354. enum pincfg_type cfg_type;
  355. u32 cfg_value;
  356. u32 data;
  357. int i;
  358. priv = pinctrl_dev_get_drvdata(pctldev);
  359. base = priv->reg_base;
  360. for (i = 0; i < num_configs; i++) {
  361. cfg_type = PINCFG_UNPACK_TYPE(configs[i]);
  362. cfg_value = PINCFG_UNPACK_VALUE(configs[i]);
  363. switch (cfg_type) {
  364. case PINCFG_TYPE_PUD:
  365. /* first set pull enable/disable bit */
  366. data = readl(base + GPIO_PE);
  367. data &= ~(1 << pin);
  368. if (cfg_value)
  369. data |= (1 << pin);
  370. writel(data, base + GPIO_PE);
  371. /* then set pull up/down bit */
  372. data = readl(base + GPIO_PS);
  373. data &= ~(1 << pin);
  374. if (cfg_value == 2)
  375. data |= (1 << pin);
  376. writel(data, base + GPIO_PS);
  377. break;
  378. case PINCFG_TYPE_DRV:
  379. /* set the first bit of the drive strength */
  380. data = readl(base + GPIO_DS0);
  381. data &= ~(1 << pin);
  382. data |= ((cfg_value & 1) << pin);
  383. writel(data, base + GPIO_DS0);
  384. cfg_value >>= 1;
  385. /* set the second bit of the driver strength */
  386. data = readl(base + GPIO_DS1);
  387. data &= ~(1 << pin);
  388. data |= ((cfg_value & 1) << pin);
  389. writel(data, base + GPIO_DS1);
  390. break;
  391. case PINCFG_TYPE_SKEW_RATE:
  392. data = readl(base + GPIO_SR);
  393. data &= ~(1 << pin);
  394. data |= ((cfg_value & 1) << pin);
  395. writel(data, base + GPIO_SR);
  396. break;
  397. case PINCFG_TYPE_INPUT_TYPE:
  398. data = readl(base + GPIO_TYPE);
  399. data &= ~(1 << pin);
  400. data |= ((cfg_value & 1) << pin);
  401. writel(data, base + GPIO_TYPE);
  402. break;
  403. default:
  404. WARN_ON(1);
  405. return -EINVAL;
  406. }
  407. } /* for each config */
  408. return 0;
  409. }
  410. /* get the pin config settings for a specified pin */
  411. static int exynos5440_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  412. unsigned long *config)
  413. {
  414. struct exynos5440_pinctrl_priv_data *priv;
  415. void __iomem *base;
  416. enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
  417. u32 data;
  418. priv = pinctrl_dev_get_drvdata(pctldev);
  419. base = priv->reg_base;
  420. switch (cfg_type) {
  421. case PINCFG_TYPE_PUD:
  422. data = readl(base + GPIO_PE);
  423. data = (data >> pin) & 1;
  424. if (!data)
  425. *config = 0;
  426. else
  427. *config = ((readl(base + GPIO_PS) >> pin) & 1) + 1;
  428. break;
  429. case PINCFG_TYPE_DRV:
  430. data = readl(base + GPIO_DS0);
  431. data = (data >> pin) & 1;
  432. *config = data;
  433. data = readl(base + GPIO_DS1);
  434. data = (data >> pin) & 1;
  435. *config |= (data << 1);
  436. break;
  437. case PINCFG_TYPE_SKEW_RATE:
  438. data = readl(base + GPIO_SR);
  439. *config = (data >> pin) & 1;
  440. break;
  441. case PINCFG_TYPE_INPUT_TYPE:
  442. data = readl(base + GPIO_TYPE);
  443. *config = (data >> pin) & 1;
  444. break;
  445. default:
  446. WARN_ON(1);
  447. return -EINVAL;
  448. }
  449. return 0;
  450. }
  451. /* set the pin config settings for a specified pin group */
  452. static int exynos5440_pinconf_group_set(struct pinctrl_dev *pctldev,
  453. unsigned group, unsigned long *configs,
  454. unsigned num_configs)
  455. {
  456. struct exynos5440_pinctrl_priv_data *priv;
  457. const unsigned int *pins;
  458. unsigned int cnt;
  459. priv = pinctrl_dev_get_drvdata(pctldev);
  460. pins = priv->pin_groups[group].pins;
  461. for (cnt = 0; cnt < priv->pin_groups[group].num_pins; cnt++)
  462. exynos5440_pinconf_set(pctldev, pins[cnt], configs,
  463. num_configs);
  464. return 0;
  465. }
  466. /* get the pin config settings for a specified pin group */
  467. static int exynos5440_pinconf_group_get(struct pinctrl_dev *pctldev,
  468. unsigned int group, unsigned long *config)
  469. {
  470. struct exynos5440_pinctrl_priv_data *priv;
  471. const unsigned int *pins;
  472. priv = pinctrl_dev_get_drvdata(pctldev);
  473. pins = priv->pin_groups[group].pins;
  474. exynos5440_pinconf_get(pctldev, pins[0], config);
  475. return 0;
  476. }
  477. /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
  478. static const struct pinconf_ops exynos5440_pinconf_ops = {
  479. .pin_config_get = exynos5440_pinconf_get,
  480. .pin_config_set = exynos5440_pinconf_set,
  481. .pin_config_group_get = exynos5440_pinconf_group_get,
  482. .pin_config_group_set = exynos5440_pinconf_group_set,
  483. };
  484. /* gpiolib gpio_set callback function */
  485. static void exynos5440_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  486. {
  487. struct exynos5440_pinctrl_priv_data *priv = dev_get_drvdata(gc->dev);
  488. void __iomem *base = priv->reg_base;
  489. u32 data;
  490. data = readl(base + GPIO_VAL);
  491. data &= ~(1 << offset);
  492. if (value)
  493. data |= 1 << offset;
  494. writel(data, base + GPIO_VAL);
  495. }
  496. /* gpiolib gpio_get callback function */
  497. static int exynos5440_gpio_get(struct gpio_chip *gc, unsigned offset)
  498. {
  499. struct exynos5440_pinctrl_priv_data *priv = dev_get_drvdata(gc->dev);
  500. void __iomem *base = priv->reg_base;
  501. u32 data;
  502. data = readl(base + GPIO_IN);
  503. data >>= offset;
  504. data &= 1;
  505. return data;
  506. }
  507. /* gpiolib gpio_direction_input callback function */
  508. static int exynos5440_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  509. {
  510. struct exynos5440_pinctrl_priv_data *priv = dev_get_drvdata(gc->dev);
  511. void __iomem *base = priv->reg_base;
  512. u32 data;
  513. /* first disable the data output enable on this pin */
  514. data = readl(base + GPIO_OE);
  515. data &= ~(1 << offset);
  516. writel(data, base + GPIO_OE);
  517. /* now enable input on this pin */
  518. data = readl(base + GPIO_IE);
  519. data |= 1 << offset;
  520. writel(data, base + GPIO_IE);
  521. return 0;
  522. }
  523. /* gpiolib gpio_direction_output callback function */
  524. static int exynos5440_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  525. int value)
  526. {
  527. struct exynos5440_pinctrl_priv_data *priv = dev_get_drvdata(gc->dev);
  528. void __iomem *base = priv->reg_base;
  529. u32 data;
  530. exynos5440_gpio_set(gc, offset, value);
  531. /* first disable the data input enable on this pin */
  532. data = readl(base + GPIO_IE);
  533. data &= ~(1 << offset);
  534. writel(data, base + GPIO_IE);
  535. /* now enable output on this pin */
  536. data = readl(base + GPIO_OE);
  537. data |= 1 << offset;
  538. writel(data, base + GPIO_OE);
  539. return 0;
  540. }
  541. /* gpiolib gpio_to_irq callback function */
  542. static int exynos5440_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  543. {
  544. struct exynos5440_pinctrl_priv_data *priv = dev_get_drvdata(gc->dev);
  545. unsigned int virq;
  546. if (offset < 16 || offset > 23)
  547. return -ENXIO;
  548. if (!priv->irq_domain)
  549. return -ENXIO;
  550. virq = irq_create_mapping(priv->irq_domain, offset - 16);
  551. return virq ? : -ENXIO;
  552. }
  553. /* parse the pin numbers listed in the 'samsung,exynos5440-pins' property */
  554. static int exynos5440_pinctrl_parse_dt_pins(struct platform_device *pdev,
  555. struct device_node *cfg_np, unsigned int **pin_list,
  556. unsigned int *npins)
  557. {
  558. struct device *dev = &pdev->dev;
  559. struct property *prop;
  560. prop = of_find_property(cfg_np, "samsung,exynos5440-pins", NULL);
  561. if (!prop)
  562. return -ENOENT;
  563. *npins = prop->length / sizeof(unsigned long);
  564. if (!*npins) {
  565. dev_err(dev, "invalid pin list in %s node", cfg_np->name);
  566. return -EINVAL;
  567. }
  568. *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
  569. if (!*pin_list) {
  570. dev_err(dev, "failed to allocate memory for pin list\n");
  571. return -ENOMEM;
  572. }
  573. return of_property_read_u32_array(cfg_np, "samsung,exynos5440-pins",
  574. *pin_list, *npins);
  575. }
  576. /*
  577. * Parse the information about all the available pin groups and pin functions
  578. * from device node of the pin-controller.
  579. */
  580. static int exynos5440_pinctrl_parse_dt(struct platform_device *pdev,
  581. struct exynos5440_pinctrl_priv_data *priv)
  582. {
  583. struct device *dev = &pdev->dev;
  584. struct device_node *dev_np = dev->of_node;
  585. struct device_node *cfg_np;
  586. struct exynos5440_pin_group *groups, *grp;
  587. struct exynos5440_pmx_func *functions, *func;
  588. unsigned *pin_list;
  589. unsigned int npins, grp_cnt, func_idx = 0;
  590. char *gname, *fname;
  591. int ret;
  592. grp_cnt = of_get_child_count(dev_np);
  593. if (!grp_cnt)
  594. return -EINVAL;
  595. groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
  596. if (!groups) {
  597. dev_err(dev, "failed allocate memory for ping group list\n");
  598. return -EINVAL;
  599. }
  600. grp = groups;
  601. functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
  602. if (!functions) {
  603. dev_err(dev, "failed to allocate memory for function list\n");
  604. return -EINVAL;
  605. }
  606. func = functions;
  607. /*
  608. * Iterate over all the child nodes of the pin controller node
  609. * and create pin groups and pin function lists.
  610. */
  611. for_each_child_of_node(dev_np, cfg_np) {
  612. u32 function;
  613. ret = exynos5440_pinctrl_parse_dt_pins(pdev, cfg_np,
  614. &pin_list, &npins);
  615. if (ret) {
  616. gname = NULL;
  617. goto skip_to_pin_function;
  618. }
  619. /* derive pin group name from the node name */
  620. gname = devm_kzalloc(dev, strlen(cfg_np->name) + GSUFFIX_LEN,
  621. GFP_KERNEL);
  622. if (!gname) {
  623. dev_err(dev, "failed to alloc memory for group name\n");
  624. return -ENOMEM;
  625. }
  626. snprintf(gname, strlen(cfg_np->name) + 4, "%s%s", cfg_np->name,
  627. GROUP_SUFFIX);
  628. grp->name = gname;
  629. grp->pins = pin_list;
  630. grp->num_pins = npins;
  631. grp++;
  632. skip_to_pin_function:
  633. ret = of_property_read_u32(cfg_np, "samsung,exynos5440-pin-function",
  634. &function);
  635. if (ret)
  636. continue;
  637. /* derive function name from the node name */
  638. fname = devm_kzalloc(dev, strlen(cfg_np->name) + FSUFFIX_LEN,
  639. GFP_KERNEL);
  640. if (!fname) {
  641. dev_err(dev, "failed to alloc memory for func name\n");
  642. return -ENOMEM;
  643. }
  644. snprintf(fname, strlen(cfg_np->name) + 4, "%s%s", cfg_np->name,
  645. FUNCTION_SUFFIX);
  646. func->name = fname;
  647. func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
  648. if (!func->groups) {
  649. dev_err(dev, "failed to alloc memory for group list "
  650. "in pin function");
  651. return -ENOMEM;
  652. }
  653. func->groups[0] = gname;
  654. func->num_groups = gname ? 1 : 0;
  655. func->function = function;
  656. func++;
  657. func_idx++;
  658. }
  659. priv->pin_groups = groups;
  660. priv->nr_groups = grp_cnt;
  661. priv->pmx_functions = functions;
  662. priv->nr_functions = func_idx;
  663. return 0;
  664. }
  665. /* register the pinctrl interface with the pinctrl subsystem */
  666. static int exynos5440_pinctrl_register(struct platform_device *pdev,
  667. struct exynos5440_pinctrl_priv_data *priv)
  668. {
  669. struct device *dev = &pdev->dev;
  670. struct pinctrl_desc *ctrldesc;
  671. struct pinctrl_dev *pctl_dev;
  672. struct pinctrl_pin_desc *pindesc, *pdesc;
  673. struct pinctrl_gpio_range grange;
  674. char *pin_names;
  675. int pin, ret;
  676. ctrldesc = devm_kzalloc(dev, sizeof(*ctrldesc), GFP_KERNEL);
  677. if (!ctrldesc) {
  678. dev_err(dev, "could not allocate memory for pinctrl desc\n");
  679. return -ENOMEM;
  680. }
  681. ctrldesc->name = "exynos5440-pinctrl";
  682. ctrldesc->owner = THIS_MODULE;
  683. ctrldesc->pctlops = &exynos5440_pctrl_ops;
  684. ctrldesc->pmxops = &exynos5440_pinmux_ops;
  685. ctrldesc->confops = &exynos5440_pinconf_ops;
  686. pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
  687. EXYNOS5440_MAX_PINS, GFP_KERNEL);
  688. if (!pindesc) {
  689. dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
  690. return -ENOMEM;
  691. }
  692. ctrldesc->pins = pindesc;
  693. ctrldesc->npins = EXYNOS5440_MAX_PINS;
  694. /* dynamically populate the pin number and pin name for pindesc */
  695. for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
  696. pdesc->number = pin;
  697. /*
  698. * allocate space for storing the dynamically generated names for all
  699. * the pins which belong to this pin-controller.
  700. */
  701. pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
  702. ctrldesc->npins, GFP_KERNEL);
  703. if (!pin_names) {
  704. dev_err(&pdev->dev, "mem alloc for pin names failed\n");
  705. return -ENOMEM;
  706. }
  707. /* for each pin, set the name of the pin */
  708. for (pin = 0; pin < ctrldesc->npins; pin++) {
  709. snprintf(pin_names, 6, "gpio%02d", pin);
  710. pdesc = pindesc + pin;
  711. pdesc->name = pin_names;
  712. pin_names += PIN_NAME_LENGTH;
  713. }
  714. ret = exynos5440_pinctrl_parse_dt(pdev, priv);
  715. if (ret)
  716. return ret;
  717. pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, priv);
  718. if (!pctl_dev) {
  719. dev_err(&pdev->dev, "could not register pinctrl driver\n");
  720. return -EINVAL;
  721. }
  722. grange.name = "exynos5440-pctrl-gpio-range";
  723. grange.id = 0;
  724. grange.base = 0;
  725. grange.npins = EXYNOS5440_MAX_PINS;
  726. grange.gc = priv->gc;
  727. pinctrl_add_gpio_range(pctl_dev, &grange);
  728. return 0;
  729. }
  730. /* register the gpiolib interface with the gpiolib subsystem */
  731. static int exynos5440_gpiolib_register(struct platform_device *pdev,
  732. struct exynos5440_pinctrl_priv_data *priv)
  733. {
  734. struct gpio_chip *gc;
  735. int ret;
  736. gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
  737. if (!gc) {
  738. dev_err(&pdev->dev, "mem alloc for gpio_chip failed\n");
  739. return -ENOMEM;
  740. }
  741. priv->gc = gc;
  742. gc->base = 0;
  743. gc->ngpio = EXYNOS5440_MAX_PINS;
  744. gc->dev = &pdev->dev;
  745. gc->set = exynos5440_gpio_set;
  746. gc->get = exynos5440_gpio_get;
  747. gc->direction_input = exynos5440_gpio_direction_input;
  748. gc->direction_output = exynos5440_gpio_direction_output;
  749. gc->to_irq = exynos5440_gpio_to_irq;
  750. gc->label = "gpiolib-exynos5440";
  751. gc->owner = THIS_MODULE;
  752. ret = gpiochip_add(gc);
  753. if (ret) {
  754. dev_err(&pdev->dev, "failed to register gpio_chip %s, error "
  755. "code: %d\n", gc->label, ret);
  756. return ret;
  757. }
  758. return 0;
  759. }
  760. /* unregister the gpiolib interface with the gpiolib subsystem */
  761. static int exynos5440_gpiolib_unregister(struct platform_device *pdev,
  762. struct exynos5440_pinctrl_priv_data *priv)
  763. {
  764. gpiochip_remove(priv->gc);
  765. return 0;
  766. }
  767. static void exynos5440_gpio_irq_unmask(struct irq_data *irqd)
  768. {
  769. struct exynos5440_pinctrl_priv_data *d;
  770. unsigned long gpio_int;
  771. d = irq_data_get_irq_chip_data(irqd);
  772. gpio_int = readl(d->reg_base + GPIO_INT);
  773. gpio_int |= 1 << irqd->hwirq;
  774. writel(gpio_int, d->reg_base + GPIO_INT);
  775. }
  776. static void exynos5440_gpio_irq_mask(struct irq_data *irqd)
  777. {
  778. struct exynos5440_pinctrl_priv_data *d;
  779. unsigned long gpio_int;
  780. d = irq_data_get_irq_chip_data(irqd);
  781. gpio_int = readl(d->reg_base + GPIO_INT);
  782. gpio_int &= ~(1 << irqd->hwirq);
  783. writel(gpio_int, d->reg_base + GPIO_INT);
  784. }
  785. /* irq_chip for gpio interrupts */
  786. static struct irq_chip exynos5440_gpio_irq_chip = {
  787. .name = "exynos5440_gpio_irq_chip",
  788. .irq_unmask = exynos5440_gpio_irq_unmask,
  789. .irq_mask = exynos5440_gpio_irq_mask,
  790. };
  791. /* interrupt handler for GPIO interrupts 0..7 */
  792. static irqreturn_t exynos5440_gpio_irq(int irq, void *data)
  793. {
  794. struct exynos5440_gpio_intr_data *intd = data;
  795. struct exynos5440_pinctrl_priv_data *d = intd->priv;
  796. int virq;
  797. virq = irq_linear_revmap(d->irq_domain, intd->gpio_int);
  798. if (!virq)
  799. return IRQ_NONE;
  800. generic_handle_irq(virq);
  801. return IRQ_HANDLED;
  802. }
  803. static int exynos5440_gpio_irq_map(struct irq_domain *h, unsigned int virq,
  804. irq_hw_number_t hw)
  805. {
  806. struct exynos5440_pinctrl_priv_data *d = h->host_data;
  807. irq_set_chip_data(virq, d);
  808. irq_set_chip_and_handler(virq, &exynos5440_gpio_irq_chip,
  809. handle_level_irq);
  810. set_irq_flags(virq, IRQF_VALID);
  811. return 0;
  812. }
  813. /* irq domain callbacks for gpio interrupt controller */
  814. static const struct irq_domain_ops exynos5440_gpio_irqd_ops = {
  815. .map = exynos5440_gpio_irq_map,
  816. .xlate = irq_domain_xlate_twocell,
  817. };
  818. /* setup handling of gpio interrupts */
  819. static int exynos5440_gpio_irq_init(struct platform_device *pdev,
  820. struct exynos5440_pinctrl_priv_data *priv)
  821. {
  822. struct device *dev = &pdev->dev;
  823. struct exynos5440_gpio_intr_data *intd;
  824. int i, irq, ret;
  825. intd = devm_kzalloc(dev, sizeof(*intd) * EXYNOS5440_MAX_GPIO_INT,
  826. GFP_KERNEL);
  827. if (!intd) {
  828. dev_err(dev, "failed to allocate memory for gpio intr data\n");
  829. return -ENOMEM;
  830. }
  831. for (i = 0; i < EXYNOS5440_MAX_GPIO_INT; i++) {
  832. irq = irq_of_parse_and_map(dev->of_node, i);
  833. if (irq <= 0) {
  834. dev_err(dev, "irq parsing failed\n");
  835. return -EINVAL;
  836. }
  837. intd->gpio_int = i;
  838. intd->priv = priv;
  839. ret = devm_request_irq(dev, irq, exynos5440_gpio_irq,
  840. 0, dev_name(dev), intd++);
  841. if (ret) {
  842. dev_err(dev, "irq request failed\n");
  843. return -ENXIO;
  844. }
  845. }
  846. priv->irq_domain = irq_domain_add_linear(dev->of_node,
  847. EXYNOS5440_MAX_GPIO_INT,
  848. &exynos5440_gpio_irqd_ops, priv);
  849. if (!priv->irq_domain) {
  850. dev_err(dev, "failed to create irq domain\n");
  851. return -ENXIO;
  852. }
  853. return 0;
  854. }
  855. static int exynos5440_pinctrl_probe(struct platform_device *pdev)
  856. {
  857. struct device *dev = &pdev->dev;
  858. struct exynos5440_pinctrl_priv_data *priv;
  859. struct resource *res;
  860. int ret;
  861. if (!dev->of_node) {
  862. dev_err(dev, "device tree node not found\n");
  863. return -ENODEV;
  864. }
  865. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  866. if (!priv) {
  867. dev_err(dev, "could not allocate memory for private data\n");
  868. return -ENOMEM;
  869. }
  870. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  871. priv->reg_base = devm_ioremap_resource(&pdev->dev, res);
  872. if (IS_ERR(priv->reg_base))
  873. return PTR_ERR(priv->reg_base);
  874. ret = exynos5440_gpiolib_register(pdev, priv);
  875. if (ret)
  876. return ret;
  877. ret = exynos5440_pinctrl_register(pdev, priv);
  878. if (ret) {
  879. exynos5440_gpiolib_unregister(pdev, priv);
  880. return ret;
  881. }
  882. ret = exynos5440_gpio_irq_init(pdev, priv);
  883. if (ret) {
  884. dev_err(dev, "failed to setup gpio interrupts\n");
  885. return ret;
  886. }
  887. platform_set_drvdata(pdev, priv);
  888. dev_info(dev, "EXYNOS5440 pinctrl driver registered\n");
  889. return 0;
  890. }
  891. static const struct of_device_id exynos5440_pinctrl_dt_match[] = {
  892. { .compatible = "samsung,exynos5440-pinctrl" },
  893. {},
  894. };
  895. MODULE_DEVICE_TABLE(of, exynos5440_pinctrl_dt_match);
  896. static struct platform_driver exynos5440_pinctrl_driver = {
  897. .probe = exynos5440_pinctrl_probe,
  898. .driver = {
  899. .name = "exynos5440-pinctrl",
  900. .owner = THIS_MODULE,
  901. .of_match_table = exynos5440_pinctrl_dt_match,
  902. },
  903. };
  904. static int __init exynos5440_pinctrl_drv_register(void)
  905. {
  906. return platform_driver_register(&exynos5440_pinctrl_driver);
  907. }
  908. postcore_initcall(exynos5440_pinctrl_drv_register);
  909. static void __exit exynos5440_pinctrl_drv_unregister(void)
  910. {
  911. platform_driver_unregister(&exynos5440_pinctrl_driver);
  912. }
  913. module_exit(exynos5440_pinctrl_drv_unregister);
  914. MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
  915. MODULE_DESCRIPTION("Samsung EXYNOS5440 SoC pinctrl driver");
  916. MODULE_LICENSE("GPL v2");