pinctrl-lpass-lpi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2020 Linaro Ltd.
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/clk.h>
  8. #include <linux/gpio/driver.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/pinctrl/pinconf-generic.h>
  14. #include <linux/pinctrl/pinconf.h>
  15. #include <linux/pinctrl/pinmux.h>
  16. #include "../pinctrl-utils.h"
  17. #include "pinctrl-lpass-lpi.h"
  18. #define MAX_NR_GPIO 32
  19. #define GPIO_FUNC 0
  20. #define MAX_LPI_NUM_CLKS 2
  21. struct lpi_pinctrl {
  22. struct device *dev;
  23. struct pinctrl_dev *ctrl;
  24. struct gpio_chip chip;
  25. struct pinctrl_desc desc;
  26. char __iomem *tlmm_base;
  27. char __iomem *slew_base;
  28. struct clk_bulk_data clks[MAX_LPI_NUM_CLKS];
  29. /* Protects from concurrent register updates */
  30. struct mutex lock;
  31. DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
  32. const struct lpi_pinctrl_variant_data *data;
  33. };
  34. static int lpi_gpio_read(struct lpi_pinctrl *state, unsigned int pin,
  35. unsigned int addr)
  36. {
  37. return ioread32(state->tlmm_base + LPI_TLMM_REG_OFFSET * pin + addr);
  38. }
  39. static int lpi_gpio_write(struct lpi_pinctrl *state, unsigned int pin,
  40. unsigned int addr, unsigned int val)
  41. {
  42. iowrite32(val, state->tlmm_base + LPI_TLMM_REG_OFFSET * pin + addr);
  43. return 0;
  44. }
  45. static const struct pinctrl_ops lpi_gpio_pinctrl_ops = {
  46. .get_groups_count = pinctrl_generic_get_group_count,
  47. .get_group_name = pinctrl_generic_get_group_name,
  48. .get_group_pins = pinctrl_generic_get_group_pins,
  49. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  50. .dt_free_map = pinctrl_utils_free_map,
  51. };
  52. static int lpi_gpio_get_functions_count(struct pinctrl_dev *pctldev)
  53. {
  54. struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  55. return pctrl->data->nfunctions;
  56. }
  57. static const char *lpi_gpio_get_function_name(struct pinctrl_dev *pctldev,
  58. unsigned int function)
  59. {
  60. struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  61. return pctrl->data->functions[function].name;
  62. }
  63. static int lpi_gpio_get_function_groups(struct pinctrl_dev *pctldev,
  64. unsigned int function,
  65. const char *const **groups,
  66. unsigned *const num_qgroups)
  67. {
  68. struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  69. *groups = pctrl->data->functions[function].groups;
  70. *num_qgroups = pctrl->data->functions[function].ngroups;
  71. return 0;
  72. }
  73. static int lpi_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
  74. unsigned int group)
  75. {
  76. struct lpi_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
  77. const struct lpi_pingroup *g = &pctrl->data->groups[group];
  78. u32 val;
  79. int i, pin = g->pin;
  80. for (i = 0; i < g->nfuncs; i++) {
  81. if (g->funcs[i] == function)
  82. break;
  83. }
  84. if (WARN_ON(i == g->nfuncs))
  85. return -EINVAL;
  86. mutex_lock(&pctrl->lock);
  87. val = lpi_gpio_read(pctrl, pin, LPI_GPIO_CFG_REG);
  88. /*
  89. * If this is the first time muxing to GPIO and the direction is
  90. * output, make sure that we're not going to be glitching the pin
  91. * by reading the current state of the pin and setting it as the
  92. * output.
  93. */
  94. if (i == GPIO_FUNC && (val & LPI_GPIO_OE_MASK) &&
  95. !test_and_set_bit(group, pctrl->ever_gpio)) {
  96. u32 io_val = lpi_gpio_read(pctrl, group, LPI_GPIO_VALUE_REG);
  97. if (io_val & LPI_GPIO_VALUE_IN_MASK) {
  98. if (!(io_val & LPI_GPIO_VALUE_OUT_MASK))
  99. lpi_gpio_write(pctrl, group, LPI_GPIO_VALUE_REG,
  100. io_val | LPI_GPIO_VALUE_OUT_MASK);
  101. } else {
  102. if (io_val & LPI_GPIO_VALUE_OUT_MASK)
  103. lpi_gpio_write(pctrl, group, LPI_GPIO_VALUE_REG,
  104. io_val & ~LPI_GPIO_VALUE_OUT_MASK);
  105. }
  106. }
  107. u32p_replace_bits(&val, i, LPI_GPIO_FUNCTION_MASK);
  108. lpi_gpio_write(pctrl, pin, LPI_GPIO_CFG_REG, val);
  109. mutex_unlock(&pctrl->lock);
  110. return 0;
  111. }
  112. static const struct pinmux_ops lpi_gpio_pinmux_ops = {
  113. .get_functions_count = lpi_gpio_get_functions_count,
  114. .get_function_name = lpi_gpio_get_function_name,
  115. .get_function_groups = lpi_gpio_get_function_groups,
  116. .set_mux = lpi_gpio_set_mux,
  117. };
  118. static int lpi_config_get(struct pinctrl_dev *pctldev,
  119. unsigned int pin, unsigned long *config)
  120. {
  121. unsigned int param = pinconf_to_config_param(*config);
  122. struct lpi_pinctrl *state = dev_get_drvdata(pctldev->dev);
  123. unsigned int arg = 0;
  124. int is_out;
  125. int pull;
  126. u32 ctl_reg;
  127. ctl_reg = lpi_gpio_read(state, pin, LPI_GPIO_CFG_REG);
  128. is_out = ctl_reg & LPI_GPIO_OE_MASK;
  129. pull = FIELD_GET(LPI_GPIO_PULL_MASK, ctl_reg);
  130. switch (param) {
  131. case PIN_CONFIG_BIAS_DISABLE:
  132. if (pull == LPI_GPIO_BIAS_DISABLE)
  133. arg = 1;
  134. break;
  135. case PIN_CONFIG_BIAS_PULL_DOWN:
  136. if (pull == LPI_GPIO_PULL_DOWN)
  137. arg = 1;
  138. break;
  139. case PIN_CONFIG_BIAS_BUS_HOLD:
  140. if (pull == LPI_GPIO_KEEPER)
  141. arg = 1;
  142. break;
  143. case PIN_CONFIG_BIAS_PULL_UP:
  144. if (pull == LPI_GPIO_PULL_UP)
  145. arg = 1;
  146. break;
  147. case PIN_CONFIG_INPUT_ENABLE:
  148. case PIN_CONFIG_OUTPUT:
  149. if (is_out)
  150. arg = 1;
  151. break;
  152. default:
  153. return -EINVAL;
  154. }
  155. *config = pinconf_to_config_packed(param, arg);
  156. return 0;
  157. }
  158. static int lpi_config_set_slew_rate(struct lpi_pinctrl *pctrl,
  159. const struct lpi_pingroup *g,
  160. unsigned int group, unsigned int slew)
  161. {
  162. unsigned long sval;
  163. void __iomem *reg;
  164. int slew_offset;
  165. if (slew > LPI_SLEW_RATE_MAX) {
  166. dev_err(pctrl->dev, "invalid slew rate %u for pin: %d\n",
  167. slew, group);
  168. return -EINVAL;
  169. }
  170. slew_offset = g->slew_offset;
  171. if (slew_offset == LPI_NO_SLEW)
  172. return 0;
  173. if (pctrl->data->flags & LPI_FLAG_SLEW_RATE_SAME_REG)
  174. reg = pctrl->tlmm_base + LPI_TLMM_REG_OFFSET * group + LPI_GPIO_CFG_REG;
  175. else
  176. reg = pctrl->slew_base + LPI_SLEW_RATE_CTL_REG;
  177. mutex_lock(&pctrl->lock);
  178. sval = ioread32(reg);
  179. sval &= ~(LPI_SLEW_RATE_MASK << slew_offset);
  180. sval |= slew << slew_offset;
  181. iowrite32(sval, reg);
  182. mutex_unlock(&pctrl->lock);
  183. return 0;
  184. }
  185. static int lpi_config_set(struct pinctrl_dev *pctldev, unsigned int group,
  186. unsigned long *configs, unsigned int nconfs)
  187. {
  188. struct lpi_pinctrl *pctrl = dev_get_drvdata(pctldev->dev);
  189. unsigned int param, arg, pullup = LPI_GPIO_BIAS_DISABLE, strength = 2;
  190. bool value, output_enabled = false;
  191. const struct lpi_pingroup *g;
  192. int i, ret;
  193. u32 val;
  194. g = &pctrl->data->groups[group];
  195. for (i = 0; i < nconfs; i++) {
  196. param = pinconf_to_config_param(configs[i]);
  197. arg = pinconf_to_config_argument(configs[i]);
  198. switch (param) {
  199. case PIN_CONFIG_BIAS_DISABLE:
  200. pullup = LPI_GPIO_BIAS_DISABLE;
  201. break;
  202. case PIN_CONFIG_BIAS_PULL_DOWN:
  203. pullup = LPI_GPIO_PULL_DOWN;
  204. break;
  205. case PIN_CONFIG_BIAS_BUS_HOLD:
  206. pullup = LPI_GPIO_KEEPER;
  207. break;
  208. case PIN_CONFIG_BIAS_PULL_UP:
  209. pullup = LPI_GPIO_PULL_UP;
  210. break;
  211. case PIN_CONFIG_INPUT_ENABLE:
  212. output_enabled = false;
  213. break;
  214. case PIN_CONFIG_OUTPUT:
  215. output_enabled = true;
  216. value = arg;
  217. break;
  218. case PIN_CONFIG_DRIVE_STRENGTH:
  219. strength = arg;
  220. break;
  221. case PIN_CONFIG_SLEW_RATE:
  222. ret = lpi_config_set_slew_rate(pctrl, g, group, arg);
  223. if (ret)
  224. return ret;
  225. break;
  226. default:
  227. return -EINVAL;
  228. }
  229. }
  230. /*
  231. * As per Hardware Programming Guide, when configuring pin as output,
  232. * set the pin value before setting output-enable (OE).
  233. */
  234. if (output_enabled) {
  235. val = u32_encode_bits(value ? 1 : 0, LPI_GPIO_VALUE_OUT_MASK);
  236. lpi_gpio_write(pctrl, group, LPI_GPIO_VALUE_REG, val);
  237. }
  238. mutex_lock(&pctrl->lock);
  239. val = lpi_gpio_read(pctrl, group, LPI_GPIO_CFG_REG);
  240. u32p_replace_bits(&val, pullup, LPI_GPIO_PULL_MASK);
  241. u32p_replace_bits(&val, LPI_GPIO_DS_TO_VAL(strength),
  242. LPI_GPIO_OUT_STRENGTH_MASK);
  243. u32p_replace_bits(&val, output_enabled, LPI_GPIO_OE_MASK);
  244. lpi_gpio_write(pctrl, group, LPI_GPIO_CFG_REG, val);
  245. mutex_unlock(&pctrl->lock);
  246. return 0;
  247. }
  248. static const struct pinconf_ops lpi_gpio_pinconf_ops = {
  249. .is_generic = true,
  250. .pin_config_group_get = lpi_config_get,
  251. .pin_config_group_set = lpi_config_set,
  252. };
  253. static int lpi_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
  254. {
  255. struct lpi_pinctrl *state = gpiochip_get_data(chip);
  256. unsigned long config;
  257. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
  258. return lpi_config_set(state->ctrl, pin, &config, 1);
  259. }
  260. static int lpi_gpio_direction_output(struct gpio_chip *chip,
  261. unsigned int pin, int val)
  262. {
  263. struct lpi_pinctrl *state = gpiochip_get_data(chip);
  264. unsigned long config;
  265. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
  266. return lpi_config_set(state->ctrl, pin, &config, 1);
  267. }
  268. static int lpi_gpio_get(struct gpio_chip *chip, unsigned int pin)
  269. {
  270. struct lpi_pinctrl *state = gpiochip_get_data(chip);
  271. return lpi_gpio_read(state, pin, LPI_GPIO_VALUE_REG) &
  272. LPI_GPIO_VALUE_IN_MASK;
  273. }
  274. static void lpi_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
  275. {
  276. struct lpi_pinctrl *state = gpiochip_get_data(chip);
  277. unsigned long config;
  278. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
  279. lpi_config_set(state->ctrl, pin, &config, 1);
  280. }
  281. #ifdef CONFIG_DEBUG_FS
  282. static unsigned int lpi_regval_to_drive(u32 val)
  283. {
  284. return (val + 1) * 2;
  285. }
  286. static void lpi_gpio_dbg_show_one(struct seq_file *s,
  287. struct pinctrl_dev *pctldev,
  288. struct gpio_chip *chip,
  289. unsigned int offset,
  290. unsigned int gpio)
  291. {
  292. struct lpi_pinctrl *state = gpiochip_get_data(chip);
  293. struct pinctrl_pin_desc pindesc;
  294. unsigned int func;
  295. int is_out;
  296. int drive;
  297. int pull;
  298. u32 ctl_reg;
  299. static const char * const pulls[] = {
  300. "no pull",
  301. "pull down",
  302. "keeper",
  303. "pull up"
  304. };
  305. pctldev = pctldev ? : state->ctrl;
  306. pindesc = pctldev->desc->pins[offset];
  307. ctl_reg = lpi_gpio_read(state, offset, LPI_GPIO_CFG_REG);
  308. is_out = ctl_reg & LPI_GPIO_OE_MASK;
  309. func = FIELD_GET(LPI_GPIO_FUNCTION_MASK, ctl_reg);
  310. drive = FIELD_GET(LPI_GPIO_OUT_STRENGTH_MASK, ctl_reg);
  311. pull = FIELD_GET(LPI_GPIO_PULL_MASK, ctl_reg);
  312. seq_printf(s, " %-8s: %-3s %d", pindesc.name, is_out ? "out" : "in", func);
  313. seq_printf(s, " %dmA", lpi_regval_to_drive(drive));
  314. seq_printf(s, " %s", pulls[pull]);
  315. }
  316. static void lpi_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  317. {
  318. unsigned int gpio = chip->base;
  319. unsigned int i;
  320. for (i = 0; i < chip->ngpio; i++, gpio++) {
  321. lpi_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  322. seq_puts(s, "\n");
  323. }
  324. }
  325. #else
  326. #define lpi_gpio_dbg_show NULL
  327. #endif
  328. static const struct gpio_chip lpi_gpio_template = {
  329. .direction_input = lpi_gpio_direction_input,
  330. .direction_output = lpi_gpio_direction_output,
  331. .get = lpi_gpio_get,
  332. .set = lpi_gpio_set,
  333. .request = gpiochip_generic_request,
  334. .free = gpiochip_generic_free,
  335. .dbg_show = lpi_gpio_dbg_show,
  336. };
  337. static int lpi_build_pin_desc_groups(struct lpi_pinctrl *pctrl)
  338. {
  339. int i, ret;
  340. for (i = 0; i < pctrl->data->npins; i++) {
  341. const struct pinctrl_pin_desc *pin_info = pctrl->desc.pins + i;
  342. ret = pinctrl_generic_add_group(pctrl->ctrl, pin_info->name,
  343. (int *)&pin_info->number, 1, NULL);
  344. if (ret < 0)
  345. goto err_pinctrl;
  346. }
  347. return 0;
  348. err_pinctrl:
  349. for (; i > 0; i--)
  350. pinctrl_generic_remove_group(pctrl->ctrl, i - 1);
  351. return ret;
  352. }
  353. int lpi_pinctrl_probe(struct platform_device *pdev)
  354. {
  355. const struct lpi_pinctrl_variant_data *data;
  356. struct device *dev = &pdev->dev;
  357. struct lpi_pinctrl *pctrl;
  358. int ret;
  359. pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
  360. if (!pctrl)
  361. return -ENOMEM;
  362. platform_set_drvdata(pdev, pctrl);
  363. data = of_device_get_match_data(dev);
  364. if (!data)
  365. return -EINVAL;
  366. if (WARN_ON(data->npins > MAX_NR_GPIO))
  367. return -EINVAL;
  368. pctrl->data = data;
  369. pctrl->dev = &pdev->dev;
  370. pctrl->clks[0].id = "core";
  371. pctrl->clks[1].id = "audio";
  372. pctrl->tlmm_base = devm_platform_ioremap_resource(pdev, 0);
  373. if (IS_ERR(pctrl->tlmm_base))
  374. return dev_err_probe(dev, PTR_ERR(pctrl->tlmm_base),
  375. "TLMM resource not provided\n");
  376. if (!(data->flags & LPI_FLAG_SLEW_RATE_SAME_REG)) {
  377. pctrl->slew_base = devm_platform_ioremap_resource(pdev, 1);
  378. if (IS_ERR(pctrl->slew_base))
  379. return dev_err_probe(dev, PTR_ERR(pctrl->slew_base),
  380. "Slew resource not provided\n");
  381. }
  382. ret = devm_clk_bulk_get_optional(dev, MAX_LPI_NUM_CLKS, pctrl->clks);
  383. if (ret)
  384. return ret;
  385. ret = clk_bulk_prepare_enable(MAX_LPI_NUM_CLKS, pctrl->clks);
  386. if (ret)
  387. return dev_err_probe(dev, ret, "Can't enable clocks\n");
  388. pctrl->desc.pctlops = &lpi_gpio_pinctrl_ops;
  389. pctrl->desc.pmxops = &lpi_gpio_pinmux_ops;
  390. pctrl->desc.confops = &lpi_gpio_pinconf_ops;
  391. pctrl->desc.owner = THIS_MODULE;
  392. pctrl->desc.name = dev_name(dev);
  393. pctrl->desc.pins = data->pins;
  394. pctrl->desc.npins = data->npins;
  395. pctrl->chip = lpi_gpio_template;
  396. pctrl->chip.parent = dev;
  397. pctrl->chip.base = -1;
  398. pctrl->chip.ngpio = data->npins;
  399. pctrl->chip.label = dev_name(dev);
  400. pctrl->chip.can_sleep = false;
  401. mutex_init(&pctrl->lock);
  402. pctrl->ctrl = devm_pinctrl_register(dev, &pctrl->desc, pctrl);
  403. if (IS_ERR(pctrl->ctrl)) {
  404. ret = PTR_ERR(pctrl->ctrl);
  405. dev_err(dev, "failed to add pin controller\n");
  406. goto err_pinctrl;
  407. }
  408. ret = lpi_build_pin_desc_groups(pctrl);
  409. if (ret)
  410. goto err_pinctrl;
  411. ret = devm_gpiochip_add_data(dev, &pctrl->chip, pctrl);
  412. if (ret) {
  413. dev_err(pctrl->dev, "can't add gpio chip\n");
  414. goto err_pinctrl;
  415. }
  416. return 0;
  417. err_pinctrl:
  418. mutex_destroy(&pctrl->lock);
  419. clk_bulk_disable_unprepare(MAX_LPI_NUM_CLKS, pctrl->clks);
  420. return ret;
  421. }
  422. EXPORT_SYMBOL_GPL(lpi_pinctrl_probe);
  423. void lpi_pinctrl_remove(struct platform_device *pdev)
  424. {
  425. struct lpi_pinctrl *pctrl = platform_get_drvdata(pdev);
  426. int i;
  427. mutex_destroy(&pctrl->lock);
  428. clk_bulk_disable_unprepare(MAX_LPI_NUM_CLKS, pctrl->clks);
  429. for (i = 0; i < pctrl->data->npins; i++)
  430. pinctrl_generic_remove_group(pctrl->ctrl, i);
  431. }
  432. EXPORT_SYMBOL_GPL(lpi_pinctrl_remove);
  433. MODULE_DESCRIPTION("QTI LPI GPIO pin control driver");
  434. MODULE_LICENSE("GPL");