pinctrl-samsung.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * pin-controller/pin-mux/pin-config/gpio-driver for Samsung's SoC's.
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. * Copyright (c) 2012 Linaro Ltd
  7. * http://www.linaro.org
  8. *
  9. * Author: Thomas Abraham <thomas.ab@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This driver implements the Samsung pinctrl driver. It supports setting up of
  17. * pinmux and pinconf configurations. The gpiolib interface is also included.
  18. * External interrupt (gpio and wakeup) support are not included in this driver
  19. * but provides extensions to which platform specific implementation of the gpio
  20. * and wakeup interrupts can be hooked to.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/slab.h>
  26. #include <linux/err.h>
  27. #include <linux/gpio.h>
  28. #include <linux/irqdomain.h>
  29. #include <linux/spinlock.h>
  30. #include "core.h"
  31. #include "pinctrl-samsung.h"
  32. #define GROUP_SUFFIX "-grp"
  33. #define GSUFFIX_LEN sizeof(GROUP_SUFFIX)
  34. #define FUNCTION_SUFFIX "-mux"
  35. #define FSUFFIX_LEN sizeof(FUNCTION_SUFFIX)
  36. /* list of all possible config options supported */
  37. static struct pin_config {
  38. char *prop_cfg;
  39. unsigned int cfg_type;
  40. } pcfgs[] = {
  41. { "samsung,pin-pud", PINCFG_TYPE_PUD },
  42. { "samsung,pin-drv", PINCFG_TYPE_DRV },
  43. { "samsung,pin-con-pdn", PINCFG_TYPE_CON_PDN },
  44. { "samsung,pin-pud-pdn", PINCFG_TYPE_PUD_PDN },
  45. };
  46. static unsigned int pin_base;
  47. static inline struct samsung_pin_bank *gc_to_pin_bank(struct gpio_chip *gc)
  48. {
  49. return container_of(gc, struct samsung_pin_bank, gpio_chip);
  50. }
  51. /* check if the selector is a valid pin group selector */
  52. static int samsung_get_group_count(struct pinctrl_dev *pctldev)
  53. {
  54. struct samsung_pinctrl_drv_data *drvdata;
  55. drvdata = pinctrl_dev_get_drvdata(pctldev);
  56. return drvdata->nr_groups;
  57. }
  58. /* return the name of the group selected by the group selector */
  59. static const char *samsung_get_group_name(struct pinctrl_dev *pctldev,
  60. unsigned selector)
  61. {
  62. struct samsung_pinctrl_drv_data *drvdata;
  63. drvdata = pinctrl_dev_get_drvdata(pctldev);
  64. return drvdata->pin_groups[selector].name;
  65. }
  66. /* return the pin numbers associated with the specified group */
  67. static int samsung_get_group_pins(struct pinctrl_dev *pctldev,
  68. unsigned selector, const unsigned **pins, unsigned *num_pins)
  69. {
  70. struct samsung_pinctrl_drv_data *drvdata;
  71. drvdata = pinctrl_dev_get_drvdata(pctldev);
  72. *pins = drvdata->pin_groups[selector].pins;
  73. *num_pins = drvdata->pin_groups[selector].num_pins;
  74. return 0;
  75. }
  76. /* create pinctrl_map entries by parsing device tree nodes */
  77. static int samsung_dt_node_to_map(struct pinctrl_dev *pctldev,
  78. struct device_node *np, struct pinctrl_map **maps,
  79. unsigned *nmaps)
  80. {
  81. struct device *dev = pctldev->dev;
  82. struct pinctrl_map *map;
  83. unsigned long *cfg = NULL;
  84. char *gname, *fname;
  85. int cfg_cnt = 0, map_cnt = 0, idx = 0;
  86. /* count the number of config options specfied in the node */
  87. for (idx = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
  88. if (of_find_property(np, pcfgs[idx].prop_cfg, NULL))
  89. cfg_cnt++;
  90. }
  91. /*
  92. * Find out the number of map entries to create. All the config options
  93. * can be accomadated into a single config map entry.
  94. */
  95. if (cfg_cnt)
  96. map_cnt = 1;
  97. if (of_find_property(np, "samsung,pin-function", NULL))
  98. map_cnt++;
  99. if (!map_cnt) {
  100. dev_err(dev, "node %s does not have either config or function "
  101. "configurations\n", np->name);
  102. return -EINVAL;
  103. }
  104. /* Allocate memory for pin-map entries */
  105. map = kzalloc(sizeof(*map) * map_cnt, GFP_KERNEL);
  106. if (!map) {
  107. dev_err(dev, "could not alloc memory for pin-maps\n");
  108. return -ENOMEM;
  109. }
  110. *nmaps = 0;
  111. /*
  112. * Allocate memory for pin group name. The pin group name is derived
  113. * from the node name from which these map entries are be created.
  114. */
  115. gname = kzalloc(strlen(np->name) + GSUFFIX_LEN, GFP_KERNEL);
  116. if (!gname) {
  117. dev_err(dev, "failed to alloc memory for group name\n");
  118. goto free_map;
  119. }
  120. sprintf(gname, "%s%s", np->name, GROUP_SUFFIX);
  121. /*
  122. * don't have config options? then skip over to creating function
  123. * map entries.
  124. */
  125. if (!cfg_cnt)
  126. goto skip_cfgs;
  127. /* Allocate memory for config entries */
  128. cfg = kzalloc(sizeof(*cfg) * cfg_cnt, GFP_KERNEL);
  129. if (!cfg) {
  130. dev_err(dev, "failed to alloc memory for configs\n");
  131. goto free_gname;
  132. }
  133. /* Prepare a list of config settings */
  134. for (idx = 0, cfg_cnt = 0; idx < ARRAY_SIZE(pcfgs); idx++) {
  135. u32 value;
  136. if (!of_property_read_u32(np, pcfgs[idx].prop_cfg, &value))
  137. cfg[cfg_cnt++] =
  138. PINCFG_PACK(pcfgs[idx].cfg_type, value);
  139. }
  140. /* create the config map entry */
  141. map[*nmaps].data.configs.group_or_pin = gname;
  142. map[*nmaps].data.configs.configs = cfg;
  143. map[*nmaps].data.configs.num_configs = cfg_cnt;
  144. map[*nmaps].type = PIN_MAP_TYPE_CONFIGS_GROUP;
  145. *nmaps += 1;
  146. skip_cfgs:
  147. /* create the function map entry */
  148. if (of_find_property(np, "samsung,pin-function", NULL)) {
  149. fname = kzalloc(strlen(np->name) + FSUFFIX_LEN, GFP_KERNEL);
  150. if (!fname) {
  151. dev_err(dev, "failed to alloc memory for func name\n");
  152. goto free_cfg;
  153. }
  154. sprintf(fname, "%s%s", np->name, FUNCTION_SUFFIX);
  155. map[*nmaps].data.mux.group = gname;
  156. map[*nmaps].data.mux.function = fname;
  157. map[*nmaps].type = PIN_MAP_TYPE_MUX_GROUP;
  158. *nmaps += 1;
  159. }
  160. *maps = map;
  161. return 0;
  162. free_cfg:
  163. kfree(cfg);
  164. free_gname:
  165. kfree(gname);
  166. free_map:
  167. kfree(map);
  168. return -ENOMEM;
  169. }
  170. /* free the memory allocated to hold the pin-map table */
  171. static void samsung_dt_free_map(struct pinctrl_dev *pctldev,
  172. struct pinctrl_map *map, unsigned num_maps)
  173. {
  174. int idx;
  175. for (idx = 0; idx < num_maps; idx++) {
  176. if (map[idx].type == PIN_MAP_TYPE_MUX_GROUP) {
  177. kfree(map[idx].data.mux.function);
  178. if (!idx)
  179. kfree(map[idx].data.mux.group);
  180. } else if (map->type == PIN_MAP_TYPE_CONFIGS_GROUP) {
  181. kfree(map[idx].data.configs.configs);
  182. if (!idx)
  183. kfree(map[idx].data.configs.group_or_pin);
  184. }
  185. };
  186. kfree(map);
  187. }
  188. /* list of pinctrl callbacks for the pinctrl core */
  189. static const struct pinctrl_ops samsung_pctrl_ops = {
  190. .get_groups_count = samsung_get_group_count,
  191. .get_group_name = samsung_get_group_name,
  192. .get_group_pins = samsung_get_group_pins,
  193. .dt_node_to_map = samsung_dt_node_to_map,
  194. .dt_free_map = samsung_dt_free_map,
  195. };
  196. /* check if the selector is a valid pin function selector */
  197. static int samsung_get_functions_count(struct pinctrl_dev *pctldev)
  198. {
  199. struct samsung_pinctrl_drv_data *drvdata;
  200. drvdata = pinctrl_dev_get_drvdata(pctldev);
  201. return drvdata->nr_functions;
  202. }
  203. /* return the name of the pin function specified */
  204. static const char *samsung_pinmux_get_fname(struct pinctrl_dev *pctldev,
  205. unsigned selector)
  206. {
  207. struct samsung_pinctrl_drv_data *drvdata;
  208. drvdata = pinctrl_dev_get_drvdata(pctldev);
  209. return drvdata->pmx_functions[selector].name;
  210. }
  211. /* return the groups associated for the specified function selector */
  212. static int samsung_pinmux_get_groups(struct pinctrl_dev *pctldev,
  213. unsigned selector, const char * const **groups,
  214. unsigned * const num_groups)
  215. {
  216. struct samsung_pinctrl_drv_data *drvdata;
  217. drvdata = pinctrl_dev_get_drvdata(pctldev);
  218. *groups = drvdata->pmx_functions[selector].groups;
  219. *num_groups = drvdata->pmx_functions[selector].num_groups;
  220. return 0;
  221. }
  222. /*
  223. * given a pin number that is local to a pin controller, find out the pin bank
  224. * and the register base of the pin bank.
  225. */
  226. static void pin_to_reg_bank(struct samsung_pinctrl_drv_data *drvdata,
  227. unsigned pin, void __iomem **reg, u32 *offset,
  228. struct samsung_pin_bank **bank)
  229. {
  230. struct samsung_pin_bank *b;
  231. b = drvdata->ctrl->pin_banks;
  232. while ((pin >= b->pin_base) &&
  233. ((b->pin_base + b->nr_pins - 1) < pin))
  234. b++;
  235. *reg = drvdata->virt_base + b->pctl_offset;
  236. *offset = pin - b->pin_base;
  237. if (bank)
  238. *bank = b;
  239. /* some banks have two config registers in a single bank */
  240. if (*offset * b->func_width > BITS_PER_LONG)
  241. *reg += 4;
  242. }
  243. /* enable or disable a pinmux function */
  244. static void samsung_pinmux_setup(struct pinctrl_dev *pctldev, unsigned selector,
  245. unsigned group, bool enable)
  246. {
  247. struct samsung_pinctrl_drv_data *drvdata;
  248. const unsigned int *pins;
  249. struct samsung_pin_bank *bank;
  250. void __iomem *reg;
  251. u32 mask, shift, data, pin_offset, cnt;
  252. unsigned long flags;
  253. drvdata = pinctrl_dev_get_drvdata(pctldev);
  254. pins = drvdata->pin_groups[group].pins;
  255. /*
  256. * for each pin in the pin group selected, program the correspoding pin
  257. * pin function number in the config register.
  258. */
  259. for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++) {
  260. pin_to_reg_bank(drvdata, pins[cnt] - drvdata->ctrl->base,
  261. &reg, &pin_offset, &bank);
  262. mask = (1 << bank->func_width) - 1;
  263. shift = pin_offset * bank->func_width;
  264. spin_lock_irqsave(&bank->slock, flags);
  265. data = readl(reg);
  266. data &= ~(mask << shift);
  267. if (enable)
  268. data |= drvdata->pin_groups[group].func << shift;
  269. writel(data, reg);
  270. spin_unlock_irqrestore(&bank->slock, flags);
  271. }
  272. }
  273. /* enable a specified pinmux by writing to registers */
  274. static int samsung_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector,
  275. unsigned group)
  276. {
  277. samsung_pinmux_setup(pctldev, selector, group, true);
  278. return 0;
  279. }
  280. /* disable a specified pinmux by writing to registers */
  281. static void samsung_pinmux_disable(struct pinctrl_dev *pctldev,
  282. unsigned selector, unsigned group)
  283. {
  284. samsung_pinmux_setup(pctldev, selector, group, false);
  285. }
  286. /*
  287. * The calls to gpio_direction_output() and gpio_direction_input()
  288. * leads to this function call (via the pinctrl_gpio_direction_{input|output}()
  289. * function called from the gpiolib interface).
  290. */
  291. static int samsung_pinmux_gpio_set_direction(struct pinctrl_dev *pctldev,
  292. struct pinctrl_gpio_range *range, unsigned offset, bool input)
  293. {
  294. struct samsung_pin_bank *bank;
  295. struct samsung_pinctrl_drv_data *drvdata;
  296. void __iomem *reg;
  297. u32 data, pin_offset, mask, shift;
  298. unsigned long flags;
  299. bank = gc_to_pin_bank(range->gc);
  300. drvdata = pinctrl_dev_get_drvdata(pctldev);
  301. pin_offset = offset - bank->pin_base;
  302. reg = drvdata->virt_base + bank->pctl_offset;
  303. mask = (1 << bank->func_width) - 1;
  304. shift = pin_offset * bank->func_width;
  305. spin_lock_irqsave(&bank->slock, flags);
  306. data = readl(reg);
  307. data &= ~(mask << shift);
  308. if (!input)
  309. data |= FUNC_OUTPUT << shift;
  310. writel(data, reg);
  311. spin_unlock_irqrestore(&bank->slock, flags);
  312. return 0;
  313. }
  314. /* list of pinmux callbacks for the pinmux vertical in pinctrl core */
  315. static const struct pinmux_ops samsung_pinmux_ops = {
  316. .get_functions_count = samsung_get_functions_count,
  317. .get_function_name = samsung_pinmux_get_fname,
  318. .get_function_groups = samsung_pinmux_get_groups,
  319. .enable = samsung_pinmux_enable,
  320. .disable = samsung_pinmux_disable,
  321. .gpio_set_direction = samsung_pinmux_gpio_set_direction,
  322. };
  323. /* set or get the pin config settings for a specified pin */
  324. static int samsung_pinconf_rw(struct pinctrl_dev *pctldev, unsigned int pin,
  325. unsigned long *config, bool set)
  326. {
  327. struct samsung_pinctrl_drv_data *drvdata;
  328. struct samsung_pin_bank *bank;
  329. void __iomem *reg_base;
  330. enum pincfg_type cfg_type = PINCFG_UNPACK_TYPE(*config);
  331. u32 data, width, pin_offset, mask, shift;
  332. u32 cfg_value, cfg_reg;
  333. unsigned long flags;
  334. drvdata = pinctrl_dev_get_drvdata(pctldev);
  335. pin_to_reg_bank(drvdata, pin - drvdata->ctrl->base, &reg_base,
  336. &pin_offset, &bank);
  337. switch (cfg_type) {
  338. case PINCFG_TYPE_PUD:
  339. width = bank->pud_width;
  340. cfg_reg = PUD_REG;
  341. break;
  342. case PINCFG_TYPE_DRV:
  343. width = bank->drv_width;
  344. cfg_reg = DRV_REG;
  345. break;
  346. case PINCFG_TYPE_CON_PDN:
  347. width = bank->conpdn_width;
  348. cfg_reg = CONPDN_REG;
  349. break;
  350. case PINCFG_TYPE_PUD_PDN:
  351. width = bank->pudpdn_width;
  352. cfg_reg = PUDPDN_REG;
  353. break;
  354. default:
  355. WARN_ON(1);
  356. return -EINVAL;
  357. }
  358. if (!width)
  359. return -EINVAL;
  360. spin_lock_irqsave(&bank->slock, flags);
  361. mask = (1 << width) - 1;
  362. shift = pin_offset * width;
  363. data = readl(reg_base + cfg_reg);
  364. if (set) {
  365. cfg_value = PINCFG_UNPACK_VALUE(*config);
  366. data &= ~(mask << shift);
  367. data |= (cfg_value << shift);
  368. writel(data, reg_base + cfg_reg);
  369. } else {
  370. data >>= shift;
  371. data &= mask;
  372. *config = PINCFG_PACK(cfg_type, data);
  373. }
  374. spin_unlock_irqrestore(&bank->slock, flags);
  375. return 0;
  376. }
  377. /* set the pin config settings for a specified pin */
  378. static int samsung_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
  379. unsigned long config)
  380. {
  381. return samsung_pinconf_rw(pctldev, pin, &config, true);
  382. }
  383. /* get the pin config settings for a specified pin */
  384. static int samsung_pinconf_get(struct pinctrl_dev *pctldev, unsigned int pin,
  385. unsigned long *config)
  386. {
  387. return samsung_pinconf_rw(pctldev, pin, config, false);
  388. }
  389. /* set the pin config settings for a specified pin group */
  390. static int samsung_pinconf_group_set(struct pinctrl_dev *pctldev,
  391. unsigned group, unsigned long config)
  392. {
  393. struct samsung_pinctrl_drv_data *drvdata;
  394. const unsigned int *pins;
  395. unsigned int cnt;
  396. drvdata = pinctrl_dev_get_drvdata(pctldev);
  397. pins = drvdata->pin_groups[group].pins;
  398. for (cnt = 0; cnt < drvdata->pin_groups[group].num_pins; cnt++)
  399. samsung_pinconf_set(pctldev, pins[cnt], config);
  400. return 0;
  401. }
  402. /* get the pin config settings for a specified pin group */
  403. static int samsung_pinconf_group_get(struct pinctrl_dev *pctldev,
  404. unsigned int group, unsigned long *config)
  405. {
  406. struct samsung_pinctrl_drv_data *drvdata;
  407. const unsigned int *pins;
  408. drvdata = pinctrl_dev_get_drvdata(pctldev);
  409. pins = drvdata->pin_groups[group].pins;
  410. samsung_pinconf_get(pctldev, pins[0], config);
  411. return 0;
  412. }
  413. /* list of pinconfig callbacks for pinconfig vertical in the pinctrl code */
  414. static const struct pinconf_ops samsung_pinconf_ops = {
  415. .pin_config_get = samsung_pinconf_get,
  416. .pin_config_set = samsung_pinconf_set,
  417. .pin_config_group_get = samsung_pinconf_group_get,
  418. .pin_config_group_set = samsung_pinconf_group_set,
  419. };
  420. /* gpiolib gpio_set callback function */
  421. static void samsung_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  422. {
  423. struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
  424. unsigned long flags;
  425. void __iomem *reg;
  426. u32 data;
  427. reg = bank->drvdata->virt_base + bank->pctl_offset;
  428. spin_lock_irqsave(&bank->slock, flags);
  429. data = readl(reg + DAT_REG);
  430. data &= ~(1 << offset);
  431. if (value)
  432. data |= 1 << offset;
  433. writel(data, reg + DAT_REG);
  434. spin_unlock_irqrestore(&bank->slock, flags);
  435. }
  436. /* gpiolib gpio_get callback function */
  437. static int samsung_gpio_get(struct gpio_chip *gc, unsigned offset)
  438. {
  439. void __iomem *reg;
  440. u32 data;
  441. struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
  442. reg = bank->drvdata->virt_base + bank->pctl_offset;
  443. data = readl(reg + DAT_REG);
  444. data >>= offset;
  445. data &= 1;
  446. return data;
  447. }
  448. /*
  449. * gpiolib gpio_direction_input callback function. The setting of the pin
  450. * mux function as 'gpio input' will be handled by the pinctrl susbsystem
  451. * interface.
  452. */
  453. static int samsung_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
  454. {
  455. return pinctrl_gpio_direction_input(gc->base + offset);
  456. }
  457. /*
  458. * gpiolib gpio_direction_output callback function. The setting of the pin
  459. * mux function as 'gpio output' will be handled by the pinctrl susbsystem
  460. * interface.
  461. */
  462. static int samsung_gpio_direction_output(struct gpio_chip *gc, unsigned offset,
  463. int value)
  464. {
  465. samsung_gpio_set(gc, offset, value);
  466. return pinctrl_gpio_direction_output(gc->base + offset);
  467. }
  468. /*
  469. * gpiolib gpio_to_irq callback function. Creates a mapping between a GPIO pin
  470. * and a virtual IRQ, if not already present.
  471. */
  472. static int samsung_gpio_to_irq(struct gpio_chip *gc, unsigned offset)
  473. {
  474. struct samsung_pin_bank *bank = gc_to_pin_bank(gc);
  475. unsigned int virq;
  476. if (!bank->irq_domain)
  477. return -ENXIO;
  478. virq = irq_create_mapping(bank->irq_domain, offset);
  479. return (virq) ? : -ENXIO;
  480. }
  481. /*
  482. * Parse the pin names listed in the 'samsung,pins' property and convert it
  483. * into a list of gpio numbers are create a pin group from it.
  484. */
  485. static int samsung_pinctrl_parse_dt_pins(struct platform_device *pdev,
  486. struct device_node *cfg_np,
  487. struct pinctrl_desc *pctl,
  488. unsigned int **pin_list,
  489. unsigned int *npins)
  490. {
  491. struct device *dev = &pdev->dev;
  492. struct property *prop;
  493. struct pinctrl_pin_desc const *pdesc = pctl->pins;
  494. unsigned int idx = 0, cnt;
  495. const char *pin_name;
  496. *npins = of_property_count_strings(cfg_np, "samsung,pins");
  497. if (IS_ERR_VALUE(*npins)) {
  498. dev_err(dev, "invalid pin list in %s node", cfg_np->name);
  499. return -EINVAL;
  500. }
  501. *pin_list = devm_kzalloc(dev, *npins * sizeof(**pin_list), GFP_KERNEL);
  502. if (!*pin_list) {
  503. dev_err(dev, "failed to allocate memory for pin list\n");
  504. return -ENOMEM;
  505. }
  506. of_property_for_each_string(cfg_np, "samsung,pins", prop, pin_name) {
  507. for (cnt = 0; cnt < pctl->npins; cnt++) {
  508. if (pdesc[cnt].name) {
  509. if (!strcmp(pin_name, pdesc[cnt].name)) {
  510. (*pin_list)[idx++] = pdesc[cnt].number;
  511. break;
  512. }
  513. }
  514. }
  515. if (cnt == pctl->npins) {
  516. dev_err(dev, "pin %s not valid in %s node\n",
  517. pin_name, cfg_np->name);
  518. devm_kfree(dev, *pin_list);
  519. return -EINVAL;
  520. }
  521. }
  522. return 0;
  523. }
  524. /*
  525. * Parse the information about all the available pin groups and pin functions
  526. * from device node of the pin-controller. A pin group is formed with all
  527. * the pins listed in the "samsung,pins" property.
  528. */
  529. static int samsung_pinctrl_parse_dt(struct platform_device *pdev,
  530. struct samsung_pinctrl_drv_data *drvdata)
  531. {
  532. struct device *dev = &pdev->dev;
  533. struct device_node *dev_np = dev->of_node;
  534. struct device_node *cfg_np;
  535. struct samsung_pin_group *groups, *grp;
  536. struct samsung_pmx_func *functions, *func;
  537. unsigned *pin_list;
  538. unsigned int npins, grp_cnt, func_idx = 0;
  539. char *gname, *fname;
  540. int ret;
  541. grp_cnt = of_get_child_count(dev_np);
  542. if (!grp_cnt)
  543. return -EINVAL;
  544. groups = devm_kzalloc(dev, grp_cnt * sizeof(*groups), GFP_KERNEL);
  545. if (!groups) {
  546. dev_err(dev, "failed allocate memory for ping group list\n");
  547. return -EINVAL;
  548. }
  549. grp = groups;
  550. functions = devm_kzalloc(dev, grp_cnt * sizeof(*functions), GFP_KERNEL);
  551. if (!functions) {
  552. dev_err(dev, "failed to allocate memory for function list\n");
  553. return -EINVAL;
  554. }
  555. func = functions;
  556. /*
  557. * Iterate over all the child nodes of the pin controller node
  558. * and create pin groups and pin function lists.
  559. */
  560. for_each_child_of_node(dev_np, cfg_np) {
  561. u32 function;
  562. if (!of_find_property(cfg_np, "samsung,pins", NULL))
  563. continue;
  564. ret = samsung_pinctrl_parse_dt_pins(pdev, cfg_np,
  565. &drvdata->pctl, &pin_list, &npins);
  566. if (ret)
  567. return ret;
  568. /* derive pin group name from the node name */
  569. gname = devm_kzalloc(dev, strlen(cfg_np->name) + GSUFFIX_LEN,
  570. GFP_KERNEL);
  571. if (!gname) {
  572. dev_err(dev, "failed to alloc memory for group name\n");
  573. return -ENOMEM;
  574. }
  575. sprintf(gname, "%s%s", cfg_np->name, GROUP_SUFFIX);
  576. grp->name = gname;
  577. grp->pins = pin_list;
  578. grp->num_pins = npins;
  579. of_property_read_u32(cfg_np, "samsung,pin-function", &function);
  580. grp->func = function;
  581. grp++;
  582. if (!of_find_property(cfg_np, "samsung,pin-function", NULL))
  583. continue;
  584. /* derive function name from the node name */
  585. fname = devm_kzalloc(dev, strlen(cfg_np->name) + FSUFFIX_LEN,
  586. GFP_KERNEL);
  587. if (!fname) {
  588. dev_err(dev, "failed to alloc memory for func name\n");
  589. return -ENOMEM;
  590. }
  591. sprintf(fname, "%s%s", cfg_np->name, FUNCTION_SUFFIX);
  592. func->name = fname;
  593. func->groups = devm_kzalloc(dev, sizeof(char *), GFP_KERNEL);
  594. if (!func->groups) {
  595. dev_err(dev, "failed to alloc memory for group list "
  596. "in pin function");
  597. return -ENOMEM;
  598. }
  599. func->groups[0] = gname;
  600. func->num_groups = 1;
  601. func++;
  602. func_idx++;
  603. }
  604. drvdata->pin_groups = groups;
  605. drvdata->nr_groups = grp_cnt;
  606. drvdata->pmx_functions = functions;
  607. drvdata->nr_functions = func_idx;
  608. return 0;
  609. }
  610. /* register the pinctrl interface with the pinctrl subsystem */
  611. static int samsung_pinctrl_register(struct platform_device *pdev,
  612. struct samsung_pinctrl_drv_data *drvdata)
  613. {
  614. struct pinctrl_desc *ctrldesc = &drvdata->pctl;
  615. struct pinctrl_pin_desc *pindesc, *pdesc;
  616. struct samsung_pin_bank *pin_bank;
  617. char *pin_names;
  618. int pin, bank, ret;
  619. ctrldesc->name = "samsung-pinctrl";
  620. ctrldesc->owner = THIS_MODULE;
  621. ctrldesc->pctlops = &samsung_pctrl_ops;
  622. ctrldesc->pmxops = &samsung_pinmux_ops;
  623. ctrldesc->confops = &samsung_pinconf_ops;
  624. pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
  625. drvdata->ctrl->nr_pins, GFP_KERNEL);
  626. if (!pindesc) {
  627. dev_err(&pdev->dev, "mem alloc for pin descriptors failed\n");
  628. return -ENOMEM;
  629. }
  630. ctrldesc->pins = pindesc;
  631. ctrldesc->npins = drvdata->ctrl->nr_pins;
  632. /* dynamically populate the pin number and pin name for pindesc */
  633. for (pin = 0, pdesc = pindesc; pin < ctrldesc->npins; pin++, pdesc++)
  634. pdesc->number = pin + drvdata->ctrl->base;
  635. /*
  636. * allocate space for storing the dynamically generated names for all
  637. * the pins which belong to this pin-controller.
  638. */
  639. pin_names = devm_kzalloc(&pdev->dev, sizeof(char) * PIN_NAME_LENGTH *
  640. drvdata->ctrl->nr_pins, GFP_KERNEL);
  641. if (!pin_names) {
  642. dev_err(&pdev->dev, "mem alloc for pin names failed\n");
  643. return -ENOMEM;
  644. }
  645. /* for each pin, the name of the pin is pin-bank name + pin number */
  646. for (bank = 0; bank < drvdata->ctrl->nr_banks; bank++) {
  647. pin_bank = &drvdata->ctrl->pin_banks[bank];
  648. for (pin = 0; pin < pin_bank->nr_pins; pin++) {
  649. sprintf(pin_names, "%s-%d", pin_bank->name, pin);
  650. pdesc = pindesc + pin_bank->pin_base + pin;
  651. pdesc->name = pin_names;
  652. pin_names += PIN_NAME_LENGTH;
  653. }
  654. }
  655. drvdata->pctl_dev = pinctrl_register(ctrldesc, &pdev->dev, drvdata);
  656. if (!drvdata->pctl_dev) {
  657. dev_err(&pdev->dev, "could not register pinctrl driver\n");
  658. return -EINVAL;
  659. }
  660. for (bank = 0; bank < drvdata->ctrl->nr_banks; ++bank) {
  661. pin_bank = &drvdata->ctrl->pin_banks[bank];
  662. pin_bank->grange.name = pin_bank->name;
  663. pin_bank->grange.id = bank;
  664. pin_bank->grange.pin_base = pin_bank->pin_base;
  665. pin_bank->grange.base = pin_bank->gpio_chip.base;
  666. pin_bank->grange.npins = pin_bank->gpio_chip.ngpio;
  667. pin_bank->grange.gc = &pin_bank->gpio_chip;
  668. pinctrl_add_gpio_range(drvdata->pctl_dev, &pin_bank->grange);
  669. }
  670. ret = samsung_pinctrl_parse_dt(pdev, drvdata);
  671. if (ret) {
  672. pinctrl_unregister(drvdata->pctl_dev);
  673. return ret;
  674. }
  675. return 0;
  676. }
  677. static const struct gpio_chip samsung_gpiolib_chip = {
  678. .set = samsung_gpio_set,
  679. .get = samsung_gpio_get,
  680. .direction_input = samsung_gpio_direction_input,
  681. .direction_output = samsung_gpio_direction_output,
  682. .to_irq = samsung_gpio_to_irq,
  683. .owner = THIS_MODULE,
  684. };
  685. /* register the gpiolib interface with the gpiolib subsystem */
  686. static int samsung_gpiolib_register(struct platform_device *pdev,
  687. struct samsung_pinctrl_drv_data *drvdata)
  688. {
  689. struct samsung_pin_ctrl *ctrl = drvdata->ctrl;
  690. struct samsung_pin_bank *bank = ctrl->pin_banks;
  691. struct gpio_chip *gc;
  692. int ret;
  693. int i;
  694. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  695. bank->gpio_chip = samsung_gpiolib_chip;
  696. gc = &bank->gpio_chip;
  697. gc->base = ctrl->base + bank->pin_base;
  698. gc->ngpio = bank->nr_pins;
  699. gc->dev = &pdev->dev;
  700. gc->of_node = bank->of_node;
  701. gc->label = bank->name;
  702. ret = gpiochip_add(gc);
  703. if (ret) {
  704. dev_err(&pdev->dev, "failed to register gpio_chip %s, error code: %d\n",
  705. gc->label, ret);
  706. goto fail;
  707. }
  708. }
  709. return 0;
  710. fail:
  711. for (--i, --bank; i >= 0; --i, --bank)
  712. if (gpiochip_remove(&bank->gpio_chip))
  713. dev_err(&pdev->dev, "gpio chip %s remove failed\n",
  714. bank->gpio_chip.label);
  715. return ret;
  716. }
  717. /* unregister the gpiolib interface with the gpiolib subsystem */
  718. static int samsung_gpiolib_unregister(struct platform_device *pdev,
  719. struct samsung_pinctrl_drv_data *drvdata)
  720. {
  721. struct samsung_pin_ctrl *ctrl = drvdata->ctrl;
  722. struct samsung_pin_bank *bank = ctrl->pin_banks;
  723. int ret = 0;
  724. int i;
  725. for (i = 0; !ret && i < ctrl->nr_banks; ++i, ++bank)
  726. ret = gpiochip_remove(&bank->gpio_chip);
  727. if (ret)
  728. dev_err(&pdev->dev, "gpio chip remove failed\n");
  729. return ret;
  730. }
  731. static const struct of_device_id samsung_pinctrl_dt_match[];
  732. /* retrieve the soc specific data */
  733. static struct samsung_pin_ctrl *samsung_pinctrl_get_soc_data(
  734. struct samsung_pinctrl_drv_data *d,
  735. struct platform_device *pdev)
  736. {
  737. int id;
  738. const struct of_device_id *match;
  739. struct device_node *node = pdev->dev.of_node;
  740. struct device_node *np;
  741. struct samsung_pin_ctrl *ctrl;
  742. struct samsung_pin_bank *bank;
  743. int i;
  744. id = of_alias_get_id(node, "pinctrl");
  745. if (id < 0) {
  746. dev_err(&pdev->dev, "failed to get alias id\n");
  747. return NULL;
  748. }
  749. match = of_match_node(samsung_pinctrl_dt_match, node);
  750. ctrl = (struct samsung_pin_ctrl *)match->data + id;
  751. bank = ctrl->pin_banks;
  752. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  753. spin_lock_init(&bank->slock);
  754. bank->drvdata = d;
  755. bank->pin_base = ctrl->nr_pins;
  756. ctrl->nr_pins += bank->nr_pins;
  757. }
  758. for_each_child_of_node(node, np) {
  759. if (!of_find_property(np, "gpio-controller", NULL))
  760. continue;
  761. bank = ctrl->pin_banks;
  762. for (i = 0; i < ctrl->nr_banks; ++i, ++bank) {
  763. if (!strcmp(bank->name, np->name)) {
  764. bank->of_node = np;
  765. break;
  766. }
  767. }
  768. }
  769. ctrl->base = pin_base;
  770. pin_base += ctrl->nr_pins;
  771. return ctrl;
  772. }
  773. static int samsung_pinctrl_probe(struct platform_device *pdev)
  774. {
  775. struct samsung_pinctrl_drv_data *drvdata;
  776. struct device *dev = &pdev->dev;
  777. struct samsung_pin_ctrl *ctrl;
  778. struct resource *res;
  779. int ret;
  780. if (!dev->of_node) {
  781. dev_err(dev, "device tree node not found\n");
  782. return -ENODEV;
  783. }
  784. drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
  785. if (!drvdata) {
  786. dev_err(dev, "failed to allocate memory for driver's "
  787. "private data\n");
  788. return -ENOMEM;
  789. }
  790. ctrl = samsung_pinctrl_get_soc_data(drvdata, pdev);
  791. if (!ctrl) {
  792. dev_err(&pdev->dev, "driver data not available\n");
  793. return -EINVAL;
  794. }
  795. drvdata->ctrl = ctrl;
  796. drvdata->dev = dev;
  797. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  798. if (!res) {
  799. dev_err(dev, "cannot find IO resource\n");
  800. return -ENOENT;
  801. }
  802. drvdata->virt_base = devm_ioremap_resource(&pdev->dev, res);
  803. if (IS_ERR(drvdata->virt_base))
  804. return PTR_ERR(drvdata->virt_base);
  805. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  806. if (res)
  807. drvdata->irq = res->start;
  808. ret = samsung_gpiolib_register(pdev, drvdata);
  809. if (ret)
  810. return ret;
  811. ret = samsung_pinctrl_register(pdev, drvdata);
  812. if (ret) {
  813. samsung_gpiolib_unregister(pdev, drvdata);
  814. return ret;
  815. }
  816. if (ctrl->eint_gpio_init)
  817. ctrl->eint_gpio_init(drvdata);
  818. if (ctrl->eint_wkup_init)
  819. ctrl->eint_wkup_init(drvdata);
  820. platform_set_drvdata(pdev, drvdata);
  821. return 0;
  822. }
  823. static const struct of_device_id samsung_pinctrl_dt_match[] = {
  824. #ifdef CONFIG_PINCTRL_EXYNOS
  825. { .compatible = "samsung,exynos4210-pinctrl",
  826. .data = (void *)exynos4210_pin_ctrl },
  827. { .compatible = "samsung,exynos4x12-pinctrl",
  828. .data = (void *)exynos4x12_pin_ctrl },
  829. #endif
  830. {},
  831. };
  832. MODULE_DEVICE_TABLE(of, samsung_pinctrl_dt_match);
  833. static struct platform_driver samsung_pinctrl_driver = {
  834. .probe = samsung_pinctrl_probe,
  835. .driver = {
  836. .name = "samsung-pinctrl",
  837. .owner = THIS_MODULE,
  838. .of_match_table = of_match_ptr(samsung_pinctrl_dt_match),
  839. },
  840. };
  841. static int __init samsung_pinctrl_drv_register(void)
  842. {
  843. return platform_driver_register(&samsung_pinctrl_driver);
  844. }
  845. postcore_initcall(samsung_pinctrl_drv_register);
  846. static void __exit samsung_pinctrl_drv_unregister(void)
  847. {
  848. platform_driver_unregister(&samsung_pinctrl_driver);
  849. }
  850. module_exit(samsung_pinctrl_drv_unregister);
  851. MODULE_AUTHOR("Thomas Abraham <thomas.ab@samsung.com>");
  852. MODULE_DESCRIPTION("Samsung pinctrl driver");
  853. MODULE_LICENSE("GPL v2");