pinctrl-slpi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. *
  6. */
  7. #include <linux/gpio.h>
  8. #include <linux/io.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/pinctrl/pinconf-generic.h>
  12. #include <linux/pinctrl/pinconf.h>
  13. #include <linux/pinctrl/pinmux.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/types.h>
  17. #include <linux/clk.h>
  18. #include "../core.h"
  19. #include "../pinctrl-utils.h"
  20. static struct clk *clk_ssc;
  21. /**
  22. * struct slpi_pin - SLPI pin definition
  23. * @name: Name of the pin.
  24. * @ctl_reg: Offset of the register holding control bits for this group.
  25. * @io_reg: Offset of the register holding input/output bits for this group.
  26. * @pull_bit: Offset in @ctl_reg for the bias configuration.
  27. * @mux_bit: Offset in @ctl_reg for the pinmux function selection.
  28. * @drv_bit: Offset in @ctl_reg for the drive strength configuration.
  29. * @oe_bit: Offset in @ctl_reg for controlling output enable.
  30. * @in_bit: Offset in @io_reg for the input bit value.
  31. * @out_bit: Offset in @io_reg for the output bit value.
  32. */
  33. struct slpi_pin {
  34. void __iomem *base;
  35. unsigned int offset;
  36. unsigned int ctl_reg;
  37. unsigned int io_reg;
  38. unsigned int pull_bit:5;
  39. unsigned int mux_bit:5;
  40. unsigned int drv_bit:5;
  41. unsigned int oe_bit:5;
  42. unsigned int in_bit:5;
  43. unsigned int out_bit:5;
  44. };
  45. /* The index of each function in slpi_pin_functions[] array */
  46. enum slpi_pin_func_index {
  47. SLPI_PIN_FUNC_INDEX_GPIO = 0x00,
  48. SLPI_PIN_FUNC_INDEX_FUNC1 = 0x01,
  49. SLPI_PIN_FUNC_INDEX_FUNC2 = 0x02,
  50. SLPI_PIN_FUNC_INDEX_FUNC3 = 0x03,
  51. SLPI_PIN_FUNC_INDEX_FUNC4 = 0x04,
  52. SLPI_PIN_FUNC_INDEX_FUNC5 = 0x05,
  53. };
  54. #define SLPI_PIN_FUNC_GPIO "gpio"
  55. #define SLPI_PIN_FUNC_FUNC1 "func1"
  56. #define SLPI_PIN_FUNC_FUNC2 "func2"
  57. #define SLPI_PIN_FUNC_FUNC3 "func3"
  58. #define SLPI_PIN_FUNC_FUNC4 "func4"
  59. #define SLPI_PIN_FUNC_FUNC5 "func5"
  60. static const char *const slpi_gpio_groups[] = {
  61. "gpio0", "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7",
  62. "gpio8", "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14",
  63. "gpio15", "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21",
  64. "gpio22", "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28",
  65. "gpio29", "gpio30", "gpio31",
  66. };
  67. static const char *const slpi_pin_functions[] = {
  68. [SLPI_PIN_FUNC_INDEX_GPIO] = SLPI_PIN_FUNC_GPIO,
  69. [SLPI_PIN_FUNC_INDEX_FUNC1] = SLPI_PIN_FUNC_FUNC1,
  70. [SLPI_PIN_FUNC_INDEX_FUNC2] = SLPI_PIN_FUNC_FUNC2,
  71. [SLPI_PIN_FUNC_INDEX_FUNC3] = SLPI_PIN_FUNC_FUNC3,
  72. [SLPI_PIN_FUNC_INDEX_FUNC4] = SLPI_PIN_FUNC_FUNC4,
  73. [SLPI_PIN_FUNC_INDEX_FUNC5] = SLPI_PIN_FUNC_FUNC5,
  74. };
  75. static unsigned int slpi_read(struct slpi_pin *pin, u32 reg)
  76. {
  77. return readl_relaxed(pin->base + pin->offset + reg);
  78. }
  79. static void slpi_write(u32 val, struct slpi_pin *pin, u32 reg)
  80. {
  81. return writel_relaxed(val, pin->base + pin->offset + reg);
  82. }
  83. static int slpi_get_groups_count(struct pinctrl_dev *pctldev)
  84. {
  85. /* Every PIN is a group */
  86. return pctldev->desc->npins;
  87. }
  88. static const char *slpi_get_group_name(struct pinctrl_dev *pctldev,
  89. unsigned int pin)
  90. {
  91. return pctldev->desc->pins[pin].name;
  92. }
  93. static int slpi_get_group_pins(struct pinctrl_dev *pctldev,
  94. unsigned int pin,
  95. const unsigned int **pins,
  96. unsigned int *num_pins)
  97. {
  98. *pins = &pctldev->desc->pins[pin].number;
  99. *num_pins = 1;
  100. return 0;
  101. }
  102. static const struct pinctrl_ops slpi_pinctrl_ops = {
  103. .get_groups_count = slpi_get_groups_count,
  104. .get_group_name = slpi_get_group_name,
  105. .get_group_pins = slpi_get_group_pins,
  106. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  107. .dt_free_map = pinctrl_utils_free_map,
  108. };
  109. static int slpi_get_functions_count(struct pinctrl_dev *pctldev)
  110. {
  111. return ARRAY_SIZE(slpi_pin_functions);
  112. }
  113. static const char *slpi_get_function_name(struct pinctrl_dev *pctldev,
  114. unsigned int function)
  115. {
  116. return slpi_pin_functions[function];
  117. }
  118. static int slpi_get_function_groups(struct pinctrl_dev *pctldev,
  119. unsigned int function,
  120. const char * const **groups,
  121. unsigned int * const num_groups)
  122. {
  123. *groups = slpi_gpio_groups;
  124. *num_groups = pctldev->desc->npins;
  125. return 0;
  126. }
  127. static int slpi_pinmux_set_mux(struct pinctrl_dev *pctldev,
  128. unsigned int function,
  129. unsigned int pin_index)
  130. {
  131. struct slpi_pin *pin;
  132. u32 val;
  133. int ret;
  134. ret = clk_prepare_enable(clk_ssc);
  135. if (ret)
  136. return ret;
  137. pin = pctldev->desc->pins[pin_index].drv_data;
  138. if (WARN_ON(function >= ARRAY_SIZE(slpi_pin_functions)))
  139. return -EINVAL;
  140. val = slpi_read(pin, pin->ctl_reg);
  141. val &= ~(0x7 << pin->mux_bit);
  142. val |= function << pin->mux_bit;
  143. slpi_write(val, pin, pin->ctl_reg);
  144. clk_disable_unprepare(clk_ssc);
  145. return 0;
  146. }
  147. static const struct pinmux_ops slpi_pinmux_ops = {
  148. .get_functions_count = slpi_get_functions_count,
  149. .get_function_name = slpi_get_function_name,
  150. .get_function_groups = slpi_get_function_groups,
  151. .set_mux = slpi_pinmux_set_mux,
  152. };
  153. static int slpi_config_reg(const struct slpi_pin *pin,
  154. unsigned int param,
  155. unsigned int *mask,
  156. unsigned int *bit)
  157. {
  158. switch (param) {
  159. case PIN_CONFIG_BIAS_DISABLE:
  160. case PIN_CONFIG_BIAS_PULL_DOWN:
  161. case PIN_CONFIG_BIAS_BUS_HOLD:
  162. case PIN_CONFIG_BIAS_PULL_UP:
  163. *bit = pin->pull_bit;
  164. *mask = 3;
  165. break;
  166. case PIN_CONFIG_DRIVE_STRENGTH:
  167. *bit = pin->drv_bit;
  168. *mask = 7;
  169. break;
  170. case PIN_CONFIG_OUTPUT:
  171. case PIN_CONFIG_INPUT_ENABLE:
  172. *bit = pin->oe_bit;
  173. *mask = 1;
  174. break;
  175. default:
  176. return -EOPNOTSUPP;
  177. }
  178. return 0;
  179. }
  180. #define MSM_NO_PULL 0
  181. #define MSM_PULL_DOWN 1
  182. #define MSM_KEEPER 2
  183. #define MSM_PULL_UP 3
  184. static unsigned int slpi_regval_to_drive(u32 val)
  185. {
  186. return (val + 1) * 2;
  187. }
  188. static int slpi_config_group_get(struct pinctrl_dev *pctldev,
  189. unsigned int pin_index,
  190. unsigned long *config)
  191. {
  192. unsigned int param = pinconf_to_config_param(*config);
  193. struct slpi_pin *pin;
  194. unsigned int mask;
  195. unsigned int arg;
  196. unsigned int bit;
  197. int ret;
  198. u32 val;
  199. ret = clk_prepare_enable(clk_ssc);
  200. if (ret)
  201. return ret;
  202. pin = pctldev->desc->pins[pin_index].drv_data;
  203. ret = slpi_config_reg(pin, param, &mask, &bit);
  204. if (ret < 0)
  205. return ret;
  206. val = slpi_read(pin, pin->ctl_reg);
  207. arg = (val >> bit) & mask;
  208. /* Convert register value to pinconf value */
  209. switch (param) {
  210. case PIN_CONFIG_BIAS_DISABLE:
  211. arg = arg == MSM_NO_PULL;
  212. break;
  213. case PIN_CONFIG_BIAS_PULL_DOWN:
  214. arg = arg == MSM_PULL_DOWN;
  215. break;
  216. case PIN_CONFIG_BIAS_BUS_HOLD:
  217. arg = arg == MSM_KEEPER;
  218. break;
  219. case PIN_CONFIG_BIAS_PULL_UP:
  220. arg = arg == MSM_PULL_UP;
  221. break;
  222. case PIN_CONFIG_DRIVE_STRENGTH:
  223. arg = slpi_regval_to_drive(arg);
  224. break;
  225. case PIN_CONFIG_OUTPUT:
  226. /* Pin is not output */
  227. if (!arg)
  228. return -EINVAL;
  229. val = slpi_read(pin, pin->io_reg);
  230. arg = !!(val & BIT(pin->in_bit));
  231. break;
  232. case PIN_CONFIG_INPUT_ENABLE:
  233. /* Pin is output */
  234. if (arg)
  235. return -EINVAL;
  236. arg = 1;
  237. break;
  238. default:
  239. return -EOPNOTSUPP;
  240. }
  241. *config = pinconf_to_config_packed(param, arg);
  242. clk_disable_unprepare(clk_ssc);
  243. return 0;
  244. }
  245. static int slpi_config_group_set(struct pinctrl_dev *pctldev,
  246. unsigned int pin_index,
  247. unsigned long *configs,
  248. unsigned int num_configs)
  249. {
  250. struct slpi_pin *pin;
  251. unsigned int param;
  252. unsigned int mask;
  253. unsigned int arg;
  254. unsigned int bit;
  255. int ret;
  256. u32 val;
  257. int i;
  258. ret = clk_prepare_enable(clk_ssc);
  259. if (ret)
  260. return ret;
  261. pin = pctldev->desc->pins[pin_index].drv_data;
  262. for (i = 0; i < num_configs; i++) {
  263. param = pinconf_to_config_param(configs[i]);
  264. arg = pinconf_to_config_argument(configs[i]);
  265. ret = slpi_config_reg(pin, param, &mask, &bit);
  266. if (ret < 0)
  267. return ret;
  268. /* Convert pinconf values to register values */
  269. switch (param) {
  270. case PIN_CONFIG_BIAS_DISABLE:
  271. arg = MSM_NO_PULL;
  272. break;
  273. case PIN_CONFIG_BIAS_PULL_DOWN:
  274. arg = MSM_PULL_DOWN;
  275. break;
  276. case PIN_CONFIG_BIAS_BUS_HOLD:
  277. arg = MSM_KEEPER;
  278. break;
  279. case PIN_CONFIG_BIAS_PULL_UP:
  280. arg = MSM_PULL_UP;
  281. break;
  282. case PIN_CONFIG_DRIVE_STRENGTH:
  283. /* Check for invalid values */
  284. if (arg > 16 || arg < 2 || (arg % 2) != 0)
  285. arg = -1;
  286. else
  287. arg = (arg / 2) - 1;
  288. break;
  289. case PIN_CONFIG_OUTPUT:
  290. /* set output value */
  291. val = slpi_read(pin, pin->io_reg);
  292. if (arg)
  293. val |= BIT(pin->out_bit);
  294. else
  295. val &= ~BIT(pin->out_bit);
  296. slpi_write(val, pin, pin->io_reg);
  297. /* enable output */
  298. arg = 1;
  299. break;
  300. case PIN_CONFIG_INPUT_ENABLE:
  301. /* disable output */
  302. arg = 0;
  303. break;
  304. default:
  305. dev_err(pctldev->dev, "Unsupported config parameter: %x\n",
  306. param);
  307. return -EINVAL;
  308. }
  309. /* Range-check user-supplied value */
  310. if (arg & ~mask) {
  311. dev_err(pctldev->dev, "config %x: %x is invalid\n",
  312. param, arg);
  313. return -EINVAL;
  314. }
  315. val = slpi_read(pin, pin->ctl_reg);
  316. val &= ~(mask << bit);
  317. val |= arg << bit;
  318. slpi_write(val, pin, pin->ctl_reg);
  319. }
  320. clk_disable_unprepare(clk_ssc);
  321. return 0;
  322. }
  323. static const struct pinconf_ops slpi_pinconf_ops = {
  324. .is_generic = true,
  325. .pin_config_group_get = slpi_config_group_get,
  326. .pin_config_group_set = slpi_config_group_set,
  327. };
  328. static int slpi_pinctrl_probe(struct platform_device *pdev)
  329. {
  330. struct device *dev = &pdev->dev;
  331. struct pinctrl_dev *pctldev;
  332. struct pinctrl_pin_desc *pindesc;
  333. struct pinctrl_desc *pctrldesc;
  334. struct slpi_pin *pin, *pins;
  335. struct resource *res;
  336. int ret, npins, i;
  337. void __iomem *base;
  338. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  339. base = devm_ioremap_resource(&pdev->dev, res);
  340. if (IS_ERR(base))
  341. return PTR_ERR(base);
  342. clk_ssc = devm_clk_get(dev, "ssc-clk");
  343. if (IS_ERR_OR_NULL(clk_ssc)) {
  344. dev_err(dev, "Error in getting ssc-clk\n");
  345. return -EPROBE_DEFER;
  346. }
  347. ret = of_property_read_u32(dev->of_node, "qcom,num-pins", &npins);
  348. if (ret < 0)
  349. return ret;
  350. pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
  351. if (!pindesc)
  352. return -ENOMEM;
  353. WARN_ON(npins > ARRAY_SIZE(slpi_gpio_groups));
  354. pins = devm_kcalloc(dev, npins, sizeof(*pins), GFP_KERNEL);
  355. if (!pins)
  356. return -ENOMEM;
  357. pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
  358. if (!pctrldesc)
  359. return -ENOMEM;
  360. pctrldesc->pctlops = &slpi_pinctrl_ops;
  361. pctrldesc->pmxops = &slpi_pinmux_ops;
  362. pctrldesc->confops = &slpi_pinconf_ops;
  363. pctrldesc->owner = THIS_MODULE;
  364. pctrldesc->name = dev_name(&pdev->dev);
  365. pctrldesc->pins = pindesc;
  366. pctrldesc->npins = npins;
  367. for (i = 0; i < npins; i++, pindesc++) {
  368. pin = &pins[i];
  369. pindesc->drv_data = pin;
  370. pindesc->number = i;
  371. pindesc->name = slpi_gpio_groups[i];
  372. pin->base = base;
  373. pin->offset = i * 0x1000;
  374. pin->ctl_reg = 0x0;
  375. pin->io_reg = 0x4;
  376. pin->pull_bit = 0;
  377. pin->out_bit = 1;
  378. pin->mux_bit = 2;
  379. pin->oe_bit = 9;
  380. pin->drv_bit = 6;
  381. pin->in_bit = 0;
  382. }
  383. pctldev = devm_pinctrl_register(&pdev->dev, pctrldesc, NULL);
  384. if (IS_ERR(pctldev)) {
  385. dev_err(dev, "Failed to register pinctrl device\n");
  386. return PTR_ERR(pctldev);
  387. }
  388. return 0;
  389. }
  390. static const struct of_device_id slpi_pinctrl_of_match[] = {
  391. { .compatible = "qcom,slpi-pinctrl" }, /* Generic */
  392. { },
  393. };
  394. MODULE_DEVICE_TABLE(of, slpi_pinctrl_of_match);
  395. static struct platform_driver slpi_pinctrl_driver = {
  396. .driver = {
  397. .name = "qcom-slpi-pinctrl",
  398. .of_match_table = slpi_pinctrl_of_match,
  399. },
  400. .probe = slpi_pinctrl_probe,
  401. };
  402. module_platform_driver(slpi_pinctrl_driver);
  403. MODULE_DESCRIPTION("QTI SLPI GPIO pin control driver");
  404. MODULE_LICENSE("GPL");