pinctrl-zx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (C) 2017 Sanechips Technology Co., Ltd.
  3. * Copyright 2017 Linaro Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/io.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/of_device.h>
  13. #include <linux/pinctrl/pinctrl.h>
  14. #include <linux/pinctrl/pinconf-generic.h>
  15. #include <linux/pinctrl/pinmux.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include "../core.h"
  19. #include "../pinctrl-utils.h"
  20. #include "../pinmux.h"
  21. #include "pinctrl-zx.h"
  22. #define ZX_PULL_DOWN BIT(0)
  23. #define ZX_PULL_UP BIT(1)
  24. #define ZX_INPUT_ENABLE BIT(3)
  25. #define ZX_DS_SHIFT 4
  26. #define ZX_DS_MASK (0x7 << ZX_DS_SHIFT)
  27. #define ZX_DS_VALUE(x) (((x) << ZX_DS_SHIFT) & ZX_DS_MASK)
  28. #define ZX_SLEW BIT(8)
  29. struct zx_pinctrl {
  30. struct pinctrl_dev *pctldev;
  31. struct device *dev;
  32. void __iomem *base;
  33. void __iomem *aux_base;
  34. spinlock_t lock;
  35. struct zx_pinctrl_soc_info *info;
  36. };
  37. static int zx_dt_node_to_map(struct pinctrl_dev *pctldev,
  38. struct device_node *np_config,
  39. struct pinctrl_map **map, u32 *num_maps)
  40. {
  41. return pinconf_generic_dt_node_to_map(pctldev, np_config, map,
  42. num_maps, PIN_MAP_TYPE_INVALID);
  43. }
  44. static const struct pinctrl_ops zx_pinctrl_ops = {
  45. .dt_node_to_map = zx_dt_node_to_map,
  46. .dt_free_map = pinctrl_utils_free_map,
  47. .get_groups_count = pinctrl_generic_get_group_count,
  48. .get_group_name = pinctrl_generic_get_group_name,
  49. .get_group_pins = pinctrl_generic_get_group_pins,
  50. };
  51. #define NONAON_MVAL 2
  52. static int zx_set_mux(struct pinctrl_dev *pctldev, unsigned int func_selector,
  53. unsigned int group_selector)
  54. {
  55. struct zx_pinctrl *zpctl = pinctrl_dev_get_drvdata(pctldev);
  56. struct zx_pinctrl_soc_info *info = zpctl->info;
  57. const struct pinctrl_pin_desc *pindesc = info->pins + group_selector;
  58. struct zx_pin_data *data = pindesc->drv_data;
  59. struct zx_mux_desc *mux;
  60. u32 mask, offset, bitpos;
  61. struct function_desc *func;
  62. unsigned long flags;
  63. u32 val, mval;
  64. /* Skip reserved pin */
  65. if (!data)
  66. return -EINVAL;
  67. mux = data->muxes;
  68. mask = (1 << data->width) - 1;
  69. offset = data->offset;
  70. bitpos = data->bitpos;
  71. func = pinmux_generic_get_function(pctldev, func_selector);
  72. if (!func)
  73. return -EINVAL;
  74. while (mux->name) {
  75. if (strcmp(mux->name, func->name) == 0)
  76. break;
  77. mux++;
  78. }
  79. /* Found mux value to be written */
  80. mval = mux->muxval;
  81. spin_lock_irqsave(&zpctl->lock, flags);
  82. if (data->aon_pin) {
  83. /*
  84. * It's an AON pin, whose mux register offset and bit position
  85. * can be caluculated from pin number. Each register covers 16
  86. * pins, and each pin occupies 2 bits.
  87. */
  88. u16 aoffset = pindesc->number / 16 * 4;
  89. u16 abitpos = (pindesc->number % 16) * 2;
  90. if (mval & AON_MUX_FLAG) {
  91. /*
  92. * This is a mux value that needs to be written into
  93. * AON pinmux register. Write it and then we're done.
  94. */
  95. val = readl(zpctl->aux_base + aoffset);
  96. val &= ~(0x3 << abitpos);
  97. val |= (mval & 0x3) << abitpos;
  98. writel(val, zpctl->aux_base + aoffset);
  99. } else {
  100. /*
  101. * It's a mux value that needs to be written into TOP
  102. * pinmux register.
  103. */
  104. val = readl(zpctl->base + offset);
  105. val &= ~(mask << bitpos);
  106. val |= (mval & mask) << bitpos;
  107. writel(val, zpctl->base + offset);
  108. /*
  109. * In this case, the AON pinmux register needs to be
  110. * set up to select non-AON function.
  111. */
  112. val = readl(zpctl->aux_base + aoffset);
  113. val &= ~(0x3 << abitpos);
  114. val |= NONAON_MVAL << abitpos;
  115. writel(val, zpctl->aux_base + aoffset);
  116. }
  117. } else {
  118. /*
  119. * This is a TOP pin, and we only need to set up TOP pinmux
  120. * register and then we're done with it.
  121. */
  122. val = readl(zpctl->base + offset);
  123. val &= ~(mask << bitpos);
  124. val |= (mval & mask) << bitpos;
  125. writel(val, zpctl->base + offset);
  126. }
  127. spin_unlock_irqrestore(&zpctl->lock, flags);
  128. return 0;
  129. }
  130. static const struct pinmux_ops zx_pinmux_ops = {
  131. .get_functions_count = pinmux_generic_get_function_count,
  132. .get_function_name = pinmux_generic_get_function_name,
  133. .get_function_groups = pinmux_generic_get_function_groups,
  134. .set_mux = zx_set_mux,
  135. };
  136. static int zx_pin_config_get(struct pinctrl_dev *pctldev, unsigned int pin,
  137. unsigned long *config)
  138. {
  139. struct zx_pinctrl *zpctl = pinctrl_dev_get_drvdata(pctldev);
  140. struct zx_pinctrl_soc_info *info = zpctl->info;
  141. const struct pinctrl_pin_desc *pindesc = info->pins + pin;
  142. struct zx_pin_data *data = pindesc->drv_data;
  143. enum pin_config_param param = pinconf_to_config_param(*config);
  144. u32 val;
  145. /* Skip reserved pin */
  146. if (!data)
  147. return -EINVAL;
  148. val = readl(zpctl->aux_base + data->coffset);
  149. val = val >> data->cbitpos;
  150. switch (param) {
  151. case PIN_CONFIG_BIAS_PULL_DOWN:
  152. val &= ZX_PULL_DOWN;
  153. val = !!val;
  154. if (val == 0)
  155. return -EINVAL;
  156. break;
  157. case PIN_CONFIG_BIAS_PULL_UP:
  158. val &= ZX_PULL_UP;
  159. val = !!val;
  160. if (val == 0)
  161. return -EINVAL;
  162. break;
  163. case PIN_CONFIG_INPUT_ENABLE:
  164. val &= ZX_INPUT_ENABLE;
  165. val = !!val;
  166. if (val == 0)
  167. return -EINVAL;
  168. break;
  169. case PIN_CONFIG_DRIVE_STRENGTH:
  170. val &= ZX_DS_MASK;
  171. val = val >> ZX_DS_SHIFT;
  172. break;
  173. case PIN_CONFIG_SLEW_RATE:
  174. val &= ZX_SLEW;
  175. val = !!val;
  176. break;
  177. default:
  178. return -ENOTSUPP;
  179. }
  180. *config = pinconf_to_config_packed(param, val);
  181. return 0;
  182. }
  183. static int zx_pin_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  184. unsigned long *configs, unsigned int num_configs)
  185. {
  186. struct zx_pinctrl *zpctl = pinctrl_dev_get_drvdata(pctldev);
  187. struct zx_pinctrl_soc_info *info = zpctl->info;
  188. const struct pinctrl_pin_desc *pindesc = info->pins + pin;
  189. struct zx_pin_data *data = pindesc->drv_data;
  190. enum pin_config_param param;
  191. u32 val, arg;
  192. int i;
  193. /* Skip reserved pin */
  194. if (!data)
  195. return -EINVAL;
  196. val = readl(zpctl->aux_base + data->coffset);
  197. for (i = 0; i < num_configs; i++) {
  198. param = pinconf_to_config_param(configs[i]);
  199. arg = pinconf_to_config_argument(configs[i]);
  200. switch (param) {
  201. case PIN_CONFIG_BIAS_PULL_DOWN:
  202. val |= ZX_PULL_DOWN << data->cbitpos;
  203. break;
  204. case PIN_CONFIG_BIAS_PULL_UP:
  205. val |= ZX_PULL_UP << data->cbitpos;
  206. break;
  207. case PIN_CONFIG_INPUT_ENABLE:
  208. val |= ZX_INPUT_ENABLE << data->cbitpos;
  209. break;
  210. case PIN_CONFIG_DRIVE_STRENGTH:
  211. val &= ~(ZX_DS_MASK << data->cbitpos);
  212. val |= ZX_DS_VALUE(arg) << data->cbitpos;
  213. break;
  214. case PIN_CONFIG_SLEW_RATE:
  215. if (arg)
  216. val |= ZX_SLEW << data->cbitpos;
  217. else
  218. val &= ~ZX_SLEW << data->cbitpos;
  219. break;
  220. default:
  221. return -ENOTSUPP;
  222. }
  223. }
  224. writel(val, zpctl->aux_base + data->coffset);
  225. return 0;
  226. }
  227. static const struct pinconf_ops zx_pinconf_ops = {
  228. .pin_config_set = zx_pin_config_set,
  229. .pin_config_get = zx_pin_config_get,
  230. .is_generic = true,
  231. };
  232. static int zx_pinctrl_build_state(struct platform_device *pdev)
  233. {
  234. struct zx_pinctrl *zpctl = platform_get_drvdata(pdev);
  235. struct zx_pinctrl_soc_info *info = zpctl->info;
  236. struct pinctrl_dev *pctldev = zpctl->pctldev;
  237. struct function_desc *functions;
  238. int nfunctions;
  239. struct group_desc *groups;
  240. int ngroups;
  241. int i;
  242. /* Every single pin composes a group */
  243. ngroups = info->npins;
  244. groups = devm_kcalloc(&pdev->dev, ngroups, sizeof(*groups),
  245. GFP_KERNEL);
  246. if (!groups)
  247. return -ENOMEM;
  248. for (i = 0; i < ngroups; i++) {
  249. const struct pinctrl_pin_desc *pindesc = info->pins + i;
  250. struct group_desc *group = groups + i;
  251. group->name = pindesc->name;
  252. group->pins = (int *) &pindesc->number;
  253. group->num_pins = 1;
  254. radix_tree_insert(&pctldev->pin_group_tree, i, group);
  255. }
  256. pctldev->num_groups = ngroups;
  257. /* Build function list from pin mux functions */
  258. functions = kcalloc(info->npins, sizeof(*functions), GFP_KERNEL);
  259. if (!functions)
  260. return -ENOMEM;
  261. nfunctions = 0;
  262. for (i = 0; i < info->npins; i++) {
  263. const struct pinctrl_pin_desc *pindesc = info->pins + i;
  264. struct zx_pin_data *data = pindesc->drv_data;
  265. struct zx_mux_desc *mux;
  266. /* Reserved pins do not have a drv_data at all */
  267. if (!data)
  268. continue;
  269. /* Loop over all muxes for the pin */
  270. mux = data->muxes;
  271. while (mux->name) {
  272. struct function_desc *func = functions;
  273. /* Search function list for given mux */
  274. while (func->name) {
  275. if (strcmp(mux->name, func->name) == 0) {
  276. /* Function exists */
  277. func->num_group_names++;
  278. break;
  279. }
  280. func++;
  281. }
  282. if (!func->name) {
  283. /* New function */
  284. func->name = mux->name;
  285. func->num_group_names = 1;
  286. radix_tree_insert(&pctldev->pin_function_tree,
  287. nfunctions++, func);
  288. }
  289. mux++;
  290. }
  291. }
  292. pctldev->num_functions = nfunctions;
  293. functions = krealloc(functions, nfunctions * sizeof(*functions),
  294. GFP_KERNEL);
  295. /* Find pin groups for every single function */
  296. for (i = 0; i < info->npins; i++) {
  297. const struct pinctrl_pin_desc *pindesc = info->pins + i;
  298. struct zx_pin_data *data = pindesc->drv_data;
  299. struct zx_mux_desc *mux;
  300. if (!data)
  301. continue;
  302. mux = data->muxes;
  303. while (mux->name) {
  304. struct function_desc *func;
  305. const char **group;
  306. int j;
  307. /* Find function for given mux */
  308. for (j = 0; j < nfunctions; j++)
  309. if (strcmp(functions[j].name, mux->name) == 0)
  310. break;
  311. func = functions + j;
  312. if (!func->group_names) {
  313. func->group_names = devm_kcalloc(&pdev->dev,
  314. func->num_group_names,
  315. sizeof(*func->group_names),
  316. GFP_KERNEL);
  317. if (!func->group_names) {
  318. kfree(functions);
  319. return -ENOMEM;
  320. }
  321. }
  322. group = func->group_names;
  323. while (*group)
  324. group++;
  325. *group = pindesc->name;
  326. mux++;
  327. }
  328. }
  329. return 0;
  330. }
  331. int zx_pinctrl_init(struct platform_device *pdev,
  332. struct zx_pinctrl_soc_info *info)
  333. {
  334. struct pinctrl_desc *pctldesc;
  335. struct zx_pinctrl *zpctl;
  336. struct device_node *np;
  337. struct resource *res;
  338. int ret;
  339. zpctl = devm_kzalloc(&pdev->dev, sizeof(*zpctl), GFP_KERNEL);
  340. if (!zpctl)
  341. return -ENOMEM;
  342. spin_lock_init(&zpctl->lock);
  343. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  344. zpctl->base = devm_ioremap_resource(&pdev->dev, res);
  345. if (IS_ERR(zpctl->base))
  346. return PTR_ERR(zpctl->base);
  347. np = of_parse_phandle(pdev->dev.of_node, "zte,auxiliary-controller", 0);
  348. if (!np) {
  349. dev_err(&pdev->dev, "failed to find auxiliary controller\n");
  350. return -ENODEV;
  351. }
  352. zpctl->aux_base = of_iomap(np, 0);
  353. of_node_put(np);
  354. if (!zpctl->aux_base)
  355. return -ENOMEM;
  356. zpctl->dev = &pdev->dev;
  357. zpctl->info = info;
  358. pctldesc = devm_kzalloc(&pdev->dev, sizeof(*pctldesc), GFP_KERNEL);
  359. if (!pctldesc)
  360. return -ENOMEM;
  361. pctldesc->name = dev_name(&pdev->dev);
  362. pctldesc->owner = THIS_MODULE;
  363. pctldesc->pins = info->pins;
  364. pctldesc->npins = info->npins;
  365. pctldesc->pctlops = &zx_pinctrl_ops;
  366. pctldesc->pmxops = &zx_pinmux_ops;
  367. pctldesc->confops = &zx_pinconf_ops;
  368. zpctl->pctldev = devm_pinctrl_register(&pdev->dev, pctldesc, zpctl);
  369. if (IS_ERR(zpctl->pctldev)) {
  370. ret = PTR_ERR(zpctl->pctldev);
  371. dev_err(&pdev->dev, "failed to register pinctrl: %d\n", ret);
  372. return ret;
  373. }
  374. platform_set_drvdata(pdev, zpctl);
  375. ret = zx_pinctrl_build_state(pdev);
  376. if (ret) {
  377. dev_err(&pdev->dev, "failed to build state: %d\n", ret);
  378. return ret;
  379. }
  380. dev_info(&pdev->dev, "initialized pinctrl driver\n");
  381. return 0;
  382. }