pinctrl-wmt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * Pinctrl driver for the Wondermedia SoC's
  3. *
  4. * Copyright (c) 2013 Tony Prisk <linux@prisktech.co.nz>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/err.h>
  16. #include <linux/gpio.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/pinctrl/consumer.h>
  24. #include <linux/pinctrl/machine.h>
  25. #include <linux/pinctrl/pinconf.h>
  26. #include <linux/pinctrl/pinconf-generic.h>
  27. #include <linux/pinctrl/pinctrl.h>
  28. #include <linux/pinctrl/pinmux.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. #include "pinctrl-wmt.h"
  32. static inline void wmt_setbits(struct wmt_pinctrl_data *data, u32 reg,
  33. u32 mask)
  34. {
  35. u32 val;
  36. val = readl_relaxed(data->base + reg);
  37. val |= mask;
  38. writel_relaxed(val, data->base + reg);
  39. }
  40. static inline void wmt_clearbits(struct wmt_pinctrl_data *data, u32 reg,
  41. u32 mask)
  42. {
  43. u32 val;
  44. val = readl_relaxed(data->base + reg);
  45. val &= ~mask;
  46. writel_relaxed(val, data->base + reg);
  47. }
  48. enum wmt_func_sel {
  49. WMT_FSEL_GPIO_IN = 0,
  50. WMT_FSEL_GPIO_OUT = 1,
  51. WMT_FSEL_ALT = 2,
  52. WMT_FSEL_COUNT = 3,
  53. };
  54. static const char * const wmt_functions[WMT_FSEL_COUNT] = {
  55. [WMT_FSEL_GPIO_IN] = "gpio_in",
  56. [WMT_FSEL_GPIO_OUT] = "gpio_out",
  57. [WMT_FSEL_ALT] = "alt",
  58. };
  59. static int wmt_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  60. {
  61. return WMT_FSEL_COUNT;
  62. }
  63. static const char *wmt_pmx_get_function_name(struct pinctrl_dev *pctldev,
  64. unsigned selector)
  65. {
  66. return wmt_functions[selector];
  67. }
  68. static int wmt_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  69. unsigned selector,
  70. const char * const **groups,
  71. unsigned * const num_groups)
  72. {
  73. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  74. /* every pin does every function */
  75. *groups = data->groups;
  76. *num_groups = data->ngroups;
  77. return 0;
  78. }
  79. static int wmt_set_pinmux(struct wmt_pinctrl_data *data, unsigned func,
  80. unsigned pin)
  81. {
  82. u32 bank = WMT_BANK_FROM_PIN(pin);
  83. u32 bit = WMT_BIT_FROM_PIN(pin);
  84. u32 reg_en = data->banks[bank].reg_en;
  85. u32 reg_dir = data->banks[bank].reg_dir;
  86. if (reg_dir == NO_REG) {
  87. dev_err(data->dev, "pin:%d no direction register defined\n",
  88. pin);
  89. return -EINVAL;
  90. }
  91. /*
  92. * If reg_en == NO_REG, we assume it is a dedicated GPIO and cannot be
  93. * disabled (as on VT8500) and that no alternate function is available.
  94. */
  95. switch (func) {
  96. case WMT_FSEL_GPIO_IN:
  97. if (reg_en != NO_REG)
  98. wmt_setbits(data, reg_en, BIT(bit));
  99. wmt_clearbits(data, reg_dir, BIT(bit));
  100. break;
  101. case WMT_FSEL_GPIO_OUT:
  102. if (reg_en != NO_REG)
  103. wmt_setbits(data, reg_en, BIT(bit));
  104. wmt_setbits(data, reg_dir, BIT(bit));
  105. break;
  106. case WMT_FSEL_ALT:
  107. if (reg_en == NO_REG) {
  108. dev_err(data->dev, "pin:%d no alt function available\n",
  109. pin);
  110. return -EINVAL;
  111. }
  112. wmt_clearbits(data, reg_en, BIT(bit));
  113. }
  114. return 0;
  115. }
  116. static int wmt_pmx_enable(struct pinctrl_dev *pctldev,
  117. unsigned func_selector,
  118. unsigned group_selector)
  119. {
  120. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  121. u32 pinnum = data->pins[group_selector].number;
  122. return wmt_set_pinmux(data, func_selector, pinnum);
  123. }
  124. static void wmt_pmx_disable(struct pinctrl_dev *pctldev,
  125. unsigned func_selector,
  126. unsigned group_selector)
  127. {
  128. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  129. u32 pinnum = data->pins[group_selector].number;
  130. /* disable by setting GPIO_IN */
  131. wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, pinnum);
  132. }
  133. static void wmt_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
  134. struct pinctrl_gpio_range *range,
  135. unsigned offset)
  136. {
  137. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  138. /* disable by setting GPIO_IN */
  139. wmt_set_pinmux(data, WMT_FSEL_GPIO_IN, offset);
  140. }
  141. static int wmt_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  142. struct pinctrl_gpio_range *range,
  143. unsigned offset,
  144. bool input)
  145. {
  146. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  147. wmt_set_pinmux(data, (input ? WMT_FSEL_GPIO_IN : WMT_FSEL_GPIO_OUT),
  148. offset);
  149. return 0;
  150. }
  151. static struct pinmux_ops wmt_pinmux_ops = {
  152. .get_functions_count = wmt_pmx_get_functions_count,
  153. .get_function_name = wmt_pmx_get_function_name,
  154. .get_function_groups = wmt_pmx_get_function_groups,
  155. .enable = wmt_pmx_enable,
  156. .disable = wmt_pmx_disable,
  157. .gpio_disable_free = wmt_pmx_gpio_disable_free,
  158. .gpio_set_direction = wmt_pmx_gpio_set_direction,
  159. };
  160. static int wmt_get_groups_count(struct pinctrl_dev *pctldev)
  161. {
  162. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  163. return data->ngroups;
  164. }
  165. static const char *wmt_get_group_name(struct pinctrl_dev *pctldev,
  166. unsigned selector)
  167. {
  168. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  169. return data->groups[selector];
  170. }
  171. static int wmt_get_group_pins(struct pinctrl_dev *pctldev,
  172. unsigned selector,
  173. const unsigned **pins,
  174. unsigned *num_pins)
  175. {
  176. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  177. *pins = &data->pins[selector].number;
  178. *num_pins = 1;
  179. return 0;
  180. }
  181. static int wmt_pctl_find_group_by_pin(struct wmt_pinctrl_data *data, u32 pin)
  182. {
  183. int i;
  184. for (i = 0; i < data->npins; i++) {
  185. if (data->pins[i].number == pin)
  186. return i;
  187. }
  188. return -EINVAL;
  189. }
  190. static int wmt_pctl_dt_node_to_map_func(struct wmt_pinctrl_data *data,
  191. struct device_node *np,
  192. u32 pin, u32 fnum,
  193. struct pinctrl_map **maps)
  194. {
  195. int group;
  196. struct pinctrl_map *map = *maps;
  197. if (fnum >= ARRAY_SIZE(wmt_functions)) {
  198. dev_err(data->dev, "invalid wm,function %d\n", fnum);
  199. return -EINVAL;
  200. }
  201. group = wmt_pctl_find_group_by_pin(data, pin);
  202. if (group < 0) {
  203. dev_err(data->dev, "unable to match pin %d to group\n", pin);
  204. return group;
  205. }
  206. map->type = PIN_MAP_TYPE_MUX_GROUP;
  207. map->data.mux.group = data->groups[group];
  208. map->data.mux.function = wmt_functions[fnum];
  209. (*maps)++;
  210. return 0;
  211. }
  212. static int wmt_pctl_dt_node_to_map_pull(struct wmt_pinctrl_data *data,
  213. struct device_node *np,
  214. u32 pin, u32 pull,
  215. struct pinctrl_map **maps)
  216. {
  217. int group;
  218. unsigned long *configs;
  219. struct pinctrl_map *map = *maps;
  220. if (pull > 2) {
  221. dev_err(data->dev, "invalid wm,pull %d\n", pull);
  222. return -EINVAL;
  223. }
  224. group = wmt_pctl_find_group_by_pin(data, pin);
  225. if (group < 0) {
  226. dev_err(data->dev, "unable to match pin %d to group\n", pin);
  227. return group;
  228. }
  229. configs = kzalloc(sizeof(*configs), GFP_KERNEL);
  230. if (!configs)
  231. return -ENOMEM;
  232. configs[0] = pull;
  233. map->type = PIN_MAP_TYPE_CONFIGS_PIN;
  234. map->data.configs.group_or_pin = data->groups[group];
  235. map->data.configs.configs = configs;
  236. map->data.configs.num_configs = 1;
  237. (*maps)++;
  238. return 0;
  239. }
  240. static void wmt_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  241. struct pinctrl_map *maps,
  242. unsigned num_maps)
  243. {
  244. int i;
  245. for (i = 0; i < num_maps; i++)
  246. if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  247. kfree(maps[i].data.configs.configs);
  248. kfree(maps);
  249. }
  250. static int wmt_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  251. struct device_node *np,
  252. struct pinctrl_map **map,
  253. unsigned *num_maps)
  254. {
  255. struct pinctrl_map *maps, *cur_map;
  256. struct property *pins, *funcs, *pulls;
  257. u32 pin, func, pull;
  258. int num_pins, num_funcs, num_pulls, maps_per_pin;
  259. int i, err;
  260. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  261. pins = of_find_property(np, "wm,pins", NULL);
  262. if (!pins) {
  263. dev_err(data->dev, "missing wmt,pins property\n");
  264. return -EINVAL;
  265. }
  266. funcs = of_find_property(np, "wm,function", NULL);
  267. pulls = of_find_property(np, "wm,pull", NULL);
  268. if (!funcs && !pulls) {
  269. dev_err(data->dev, "neither wm,function nor wm,pull specified\n");
  270. return -EINVAL;
  271. }
  272. /*
  273. * The following lines calculate how many values are defined for each
  274. * of the properties.
  275. */
  276. num_pins = pins->length / sizeof(u32);
  277. num_funcs = funcs ? (funcs->length / sizeof(u32)) : 0;
  278. num_pulls = pulls ? (pulls->length / sizeof(u32)) : 0;
  279. if (num_funcs > 1 && num_funcs != num_pins) {
  280. dev_err(data->dev, "wm,function must have 1 or %d entries\n",
  281. num_pins);
  282. return -EINVAL;
  283. }
  284. if (num_pulls > 1 && num_pulls != num_pins) {
  285. dev_err(data->dev, "wm,pull must have 1 or %d entries\n",
  286. num_pins);
  287. return -EINVAL;
  288. }
  289. maps_per_pin = 0;
  290. if (num_funcs)
  291. maps_per_pin++;
  292. if (num_pulls)
  293. maps_per_pin++;
  294. cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
  295. GFP_KERNEL);
  296. if (!maps)
  297. return -ENOMEM;
  298. for (i = 0; i < num_pins; i++) {
  299. err = of_property_read_u32_index(np, "wm,pins", i, &pin);
  300. if (err)
  301. goto fail;
  302. if (pin >= (data->nbanks * 32)) {
  303. dev_err(data->dev, "invalid wm,pins value\n");
  304. err = -EINVAL;
  305. goto fail;
  306. }
  307. if (num_funcs) {
  308. err = of_property_read_u32_index(np, "wm,function",
  309. (num_funcs > 1 ? i : 0), &func);
  310. if (err)
  311. goto fail;
  312. err = wmt_pctl_dt_node_to_map_func(data, np, pin, func,
  313. &cur_map);
  314. if (err)
  315. goto fail;
  316. }
  317. if (num_pulls) {
  318. err = of_property_read_u32_index(np, "wm,pull",
  319. (num_pulls > 1 ? i : 0), &pull);
  320. if (err)
  321. goto fail;
  322. err = wmt_pctl_dt_node_to_map_pull(data, np, pin, pull,
  323. &cur_map);
  324. if (err)
  325. goto fail;
  326. }
  327. }
  328. *map = maps;
  329. *num_maps = num_pins * maps_per_pin;
  330. return 0;
  331. /*
  332. * The fail path removes any maps that have been allocated. The fail path is
  333. * only called from code after maps has been kzalloc'd. It is also safe to
  334. * pass 'num_pins * maps_per_pin' as the map count even though we probably
  335. * failed before all the mappings were read as all maps are allocated at once,
  336. * and configs are only allocated for .type = PIN_MAP_TYPE_CONFIGS_PIN - there
  337. * is no failpath where a config can be allocated without .type being set.
  338. */
  339. fail:
  340. wmt_pctl_dt_free_map(pctldev, maps, num_pins * maps_per_pin);
  341. return err;
  342. }
  343. static struct pinctrl_ops wmt_pctl_ops = {
  344. .get_groups_count = wmt_get_groups_count,
  345. .get_group_name = wmt_get_group_name,
  346. .get_group_pins = wmt_get_group_pins,
  347. .dt_node_to_map = wmt_pctl_dt_node_to_map,
  348. .dt_free_map = wmt_pctl_dt_free_map,
  349. };
  350. static int wmt_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
  351. unsigned long *config)
  352. {
  353. return -ENOTSUPP;
  354. }
  355. static int wmt_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
  356. unsigned long config)
  357. {
  358. struct wmt_pinctrl_data *data = pinctrl_dev_get_drvdata(pctldev);
  359. enum pin_config_param param = pinconf_to_config_param(config);
  360. u16 arg = pinconf_to_config_argument(config);
  361. u32 bank = WMT_BANK_FROM_PIN(pin);
  362. u32 bit = WMT_BIT_FROM_PIN(pin);
  363. u32 reg_pull_en = data->banks[bank].reg_pull_en;
  364. u32 reg_pull_cfg = data->banks[bank].reg_pull_cfg;
  365. if ((reg_pull_en == NO_REG) || (reg_pull_cfg == NO_REG)) {
  366. dev_err(data->dev, "bias functions not supported on pin %d\n",
  367. pin);
  368. return -EINVAL;
  369. }
  370. if ((param == PIN_CONFIG_BIAS_PULL_DOWN) ||
  371. (param == PIN_CONFIG_BIAS_PULL_UP)) {
  372. if (arg == 0)
  373. param = PIN_CONFIG_BIAS_DISABLE;
  374. }
  375. switch (param) {
  376. case PIN_CONFIG_BIAS_DISABLE:
  377. wmt_clearbits(data, reg_pull_en, BIT(bit));
  378. break;
  379. case PIN_CONFIG_BIAS_PULL_DOWN:
  380. wmt_clearbits(data, reg_pull_cfg, BIT(bit));
  381. wmt_setbits(data, reg_pull_en, BIT(bit));
  382. break;
  383. case PIN_CONFIG_BIAS_PULL_UP:
  384. wmt_setbits(data, reg_pull_cfg, BIT(bit));
  385. wmt_setbits(data, reg_pull_en, BIT(bit));
  386. break;
  387. default:
  388. dev_err(data->dev, "unknown pinconf param\n");
  389. return -EINVAL;
  390. }
  391. return 0;
  392. }
  393. static struct pinconf_ops wmt_pinconf_ops = {
  394. .pin_config_get = wmt_pinconf_get,
  395. .pin_config_set = wmt_pinconf_set,
  396. };
  397. static struct pinctrl_desc wmt_desc = {
  398. .owner = THIS_MODULE,
  399. .name = "pinctrl-wmt",
  400. .pctlops = &wmt_pctl_ops,
  401. .pmxops = &wmt_pinmux_ops,
  402. .confops = &wmt_pinconf_ops,
  403. };
  404. static int wmt_gpio_request(struct gpio_chip *chip, unsigned offset)
  405. {
  406. return pinctrl_request_gpio(chip->base + offset);
  407. }
  408. static void wmt_gpio_free(struct gpio_chip *chip, unsigned offset)
  409. {
  410. pinctrl_free_gpio(chip->base + offset);
  411. }
  412. static int wmt_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
  413. {
  414. struct wmt_pinctrl_data *data = dev_get_drvdata(chip->dev);
  415. u32 bank = WMT_BANK_FROM_PIN(offset);
  416. u32 bit = WMT_BIT_FROM_PIN(offset);
  417. u32 reg_dir = data->banks[bank].reg_dir;
  418. u32 val;
  419. val = readl_relaxed(data->base + reg_dir);
  420. if (val & BIT(bit))
  421. return GPIOF_DIR_OUT;
  422. else
  423. return GPIOF_DIR_IN;
  424. }
  425. static int wmt_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  426. {
  427. return pinctrl_gpio_direction_input(chip->base + offset);
  428. }
  429. static int wmt_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  430. int value)
  431. {
  432. return pinctrl_gpio_direction_output(chip->base + offset);
  433. }
  434. static int wmt_gpio_get_value(struct gpio_chip *chip, unsigned offset)
  435. {
  436. struct wmt_pinctrl_data *data = dev_get_drvdata(chip->dev);
  437. u32 bank = WMT_BANK_FROM_PIN(offset);
  438. u32 bit = WMT_BIT_FROM_PIN(offset);
  439. u32 reg_data_in = data->banks[bank].reg_data_in;
  440. if (reg_data_in == NO_REG) {
  441. dev_err(data->dev, "no data in register defined\n");
  442. return -EINVAL;
  443. }
  444. return !!(readl_relaxed(data->base + reg_data_in) & BIT(bit));
  445. }
  446. static void wmt_gpio_set_value(struct gpio_chip *chip, unsigned offset,
  447. int val)
  448. {
  449. struct wmt_pinctrl_data *data = dev_get_drvdata(chip->dev);
  450. u32 bank = WMT_BANK_FROM_PIN(offset);
  451. u32 bit = WMT_BIT_FROM_PIN(offset);
  452. u32 reg_data_out = data->banks[bank].reg_data_out;
  453. if (reg_data_out == NO_REG) {
  454. dev_err(data->dev, "no data out register defined\n");
  455. return;
  456. }
  457. if (val)
  458. wmt_setbits(data, reg_data_out, BIT(bit));
  459. else
  460. wmt_clearbits(data, reg_data_out, BIT(bit));
  461. }
  462. static struct gpio_chip wmt_gpio_chip = {
  463. .label = "gpio-wmt",
  464. .owner = THIS_MODULE,
  465. .request = wmt_gpio_request,
  466. .free = wmt_gpio_free,
  467. .get_direction = wmt_gpio_get_direction,
  468. .direction_input = wmt_gpio_direction_input,
  469. .direction_output = wmt_gpio_direction_output,
  470. .get = wmt_gpio_get_value,
  471. .set = wmt_gpio_set_value,
  472. .can_sleep = 0,
  473. };
  474. int wmt_pinctrl_probe(struct platform_device *pdev,
  475. struct wmt_pinctrl_data *data)
  476. {
  477. int err;
  478. struct resource *res;
  479. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  480. data->base = devm_request_and_ioremap(&pdev->dev, res);
  481. if (!data->base) {
  482. dev_err(&pdev->dev, "failed to map memory resource\n");
  483. return -EBUSY;
  484. }
  485. wmt_desc.pins = data->pins;
  486. wmt_desc.npins = data->npins;
  487. data->gpio_chip = wmt_gpio_chip;
  488. data->gpio_chip.dev = &pdev->dev;
  489. data->gpio_chip.of_node = pdev->dev.of_node;
  490. data->gpio_chip.ngpio = data->nbanks * 32;
  491. platform_set_drvdata(pdev, data);
  492. data->dev = &pdev->dev;
  493. data->pctl_dev = pinctrl_register(&wmt_desc, &pdev->dev, data);
  494. if (!data->pctl_dev) {
  495. dev_err(&pdev->dev, "Failed to register pinctrl\n");
  496. return -EINVAL;
  497. }
  498. err = gpiochip_add(&data->gpio_chip);
  499. if (err) {
  500. dev_err(&pdev->dev, "could not add GPIO chip\n");
  501. goto fail_gpio;
  502. }
  503. err = gpiochip_add_pin_range(&data->gpio_chip, dev_name(data->dev),
  504. 0, 0, data->nbanks * 32);
  505. if (err)
  506. goto fail_range;
  507. dev_info(&pdev->dev, "Pin controller initialized\n");
  508. return 0;
  509. fail_range:
  510. if (gpiochip_remove(&data->gpio_chip))
  511. dev_err(&pdev->dev, "failed to remove gpio chip\n");
  512. fail_gpio:
  513. pinctrl_unregister(data->pctl_dev);
  514. return err;
  515. }
  516. int wmt_pinctrl_remove(struct platform_device *pdev)
  517. {
  518. struct wmt_pinctrl_data *data = platform_get_drvdata(pdev);
  519. int err;
  520. err = gpiochip_remove(&data->gpio_chip);
  521. if (err)
  522. dev_err(&pdev->dev, "failed to remove gpio chip\n");
  523. pinctrl_unregister(data->pctl_dev);
  524. return 0;
  525. }