pinctrl-meson.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Pin controller and GPIO driver for Amlogic Meson SoCs
  3. *
  4. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * You should have received a copy of the GNU General Public License
  11. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. * The available pins are organized in banks (A,B,C,D,E,X,Y,Z,AO,
  15. * BOOT,CARD for meson6, X,Y,DV,H,Z,AO,BOOT,CARD for meson8 and
  16. * X,Y,DV,H,AO,BOOT,CARD,DIF for meson8b) and each bank has a
  17. * variable number of pins.
  18. *
  19. * The AO bank is special because it belongs to the Always-On power
  20. * domain which can't be powered off; the bank also uses a set of
  21. * registers different from the other banks.
  22. *
  23. * For each pin controller there are 4 different register ranges that
  24. * control the following properties of the pins:
  25. * 1) pin muxing
  26. * 2) pull enable/disable
  27. * 3) pull up/down
  28. * 4) GPIO direction, output value, input value
  29. *
  30. * In some cases the register ranges for pull enable and pull
  31. * direction are the same and thus there are only 3 register ranges.
  32. *
  33. * For the pull and GPIO configuration every bank uses a contiguous
  34. * set of bits in the register sets described above; the same register
  35. * can be shared by more banks with different offsets.
  36. *
  37. * In addition to this there are some registers shared between all
  38. * banks that control the IRQ functionality. This feature is not
  39. * supported at the moment by the driver.
  40. */
  41. #include <linux/device.h>
  42. #include <linux/gpio.h>
  43. #include <linux/init.h>
  44. #include <linux/io.h>
  45. #include <linux/of.h>
  46. #include <linux/of_address.h>
  47. #include <linux/of_device.h>
  48. #include <linux/pinctrl/pinconf-generic.h>
  49. #include <linux/pinctrl/pinconf.h>
  50. #include <linux/pinctrl/pinctrl.h>
  51. #include <linux/pinctrl/pinmux.h>
  52. #include <linux/platform_device.h>
  53. #include <linux/regmap.h>
  54. #include <linux/seq_file.h>
  55. #include "../core.h"
  56. #include "../pinctrl-utils.h"
  57. #include "pinctrl-meson.h"
  58. /**
  59. * meson_get_bank() - find the bank containing a given pin
  60. *
  61. * @pc: the pinctrl instance
  62. * @pin: the pin number
  63. * @bank: the found bank
  64. *
  65. * Return: 0 on success, a negative value on error
  66. */
  67. static int meson_get_bank(struct meson_pinctrl *pc, unsigned int pin,
  68. struct meson_bank **bank)
  69. {
  70. int i;
  71. for (i = 0; i < pc->data->num_banks; i++) {
  72. if (pin >= pc->data->banks[i].first &&
  73. pin <= pc->data->banks[i].last) {
  74. *bank = &pc->data->banks[i];
  75. return 0;
  76. }
  77. }
  78. return -EINVAL;
  79. }
  80. /**
  81. * meson_calc_reg_and_bit() - calculate register and bit for a pin
  82. *
  83. * @bank: the bank containing the pin
  84. * @pin: the pin number
  85. * @reg_type: the type of register needed (pull-enable, pull, etc...)
  86. * @reg: the computed register offset
  87. * @bit: the computed bit
  88. */
  89. static void meson_calc_reg_and_bit(struct meson_bank *bank, unsigned int pin,
  90. enum meson_reg_type reg_type,
  91. unsigned int *reg, unsigned int *bit)
  92. {
  93. struct meson_reg_desc *desc = &bank->regs[reg_type];
  94. *reg = desc->reg * 4;
  95. *bit = desc->bit + pin - bank->first;
  96. }
  97. static int meson_get_groups_count(struct pinctrl_dev *pcdev)
  98. {
  99. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  100. return pc->data->num_groups;
  101. }
  102. static const char *meson_get_group_name(struct pinctrl_dev *pcdev,
  103. unsigned selector)
  104. {
  105. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  106. return pc->data->groups[selector].name;
  107. }
  108. static int meson_get_group_pins(struct pinctrl_dev *pcdev, unsigned selector,
  109. const unsigned **pins, unsigned *num_pins)
  110. {
  111. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  112. *pins = pc->data->groups[selector].pins;
  113. *num_pins = pc->data->groups[selector].num_pins;
  114. return 0;
  115. }
  116. static void meson_pin_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s,
  117. unsigned offset)
  118. {
  119. seq_printf(s, " %s", dev_name(pcdev->dev));
  120. }
  121. static const struct pinctrl_ops meson_pctrl_ops = {
  122. .get_groups_count = meson_get_groups_count,
  123. .get_group_name = meson_get_group_name,
  124. .get_group_pins = meson_get_group_pins,
  125. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  126. .dt_free_map = pinctrl_utils_free_map,
  127. .pin_dbg_show = meson_pin_dbg_show,
  128. };
  129. int meson_pmx_get_funcs_count(struct pinctrl_dev *pcdev)
  130. {
  131. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  132. return pc->data->num_funcs;
  133. }
  134. const char *meson_pmx_get_func_name(struct pinctrl_dev *pcdev,
  135. unsigned selector)
  136. {
  137. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  138. return pc->data->funcs[selector].name;
  139. }
  140. int meson_pmx_get_groups(struct pinctrl_dev *pcdev, unsigned selector,
  141. const char * const **groups,
  142. unsigned * const num_groups)
  143. {
  144. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  145. *groups = pc->data->funcs[selector].groups;
  146. *num_groups = pc->data->funcs[selector].num_groups;
  147. return 0;
  148. }
  149. static int meson_pinconf_set(struct pinctrl_dev *pcdev, unsigned int pin,
  150. unsigned long *configs, unsigned num_configs)
  151. {
  152. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  153. struct meson_bank *bank;
  154. enum pin_config_param param;
  155. unsigned int reg, bit;
  156. int i, ret;
  157. ret = meson_get_bank(pc, pin, &bank);
  158. if (ret)
  159. return ret;
  160. for (i = 0; i < num_configs; i++) {
  161. param = pinconf_to_config_param(configs[i]);
  162. switch (param) {
  163. case PIN_CONFIG_BIAS_DISABLE:
  164. dev_dbg(pc->dev, "pin %u: disable bias\n", pin);
  165. meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
  166. ret = regmap_update_bits(pc->reg_pull, reg,
  167. BIT(bit), 0);
  168. if (ret)
  169. return ret;
  170. break;
  171. case PIN_CONFIG_BIAS_PULL_UP:
  172. dev_dbg(pc->dev, "pin %u: enable pull-up\n", pin);
  173. meson_calc_reg_and_bit(bank, pin, REG_PULLEN,
  174. &reg, &bit);
  175. ret = regmap_update_bits(pc->reg_pullen, reg,
  176. BIT(bit), BIT(bit));
  177. if (ret)
  178. return ret;
  179. meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
  180. ret = regmap_update_bits(pc->reg_pull, reg,
  181. BIT(bit), BIT(bit));
  182. if (ret)
  183. return ret;
  184. break;
  185. case PIN_CONFIG_BIAS_PULL_DOWN:
  186. dev_dbg(pc->dev, "pin %u: enable pull-down\n", pin);
  187. meson_calc_reg_and_bit(bank, pin, REG_PULLEN,
  188. &reg, &bit);
  189. ret = regmap_update_bits(pc->reg_pullen, reg,
  190. BIT(bit), BIT(bit));
  191. if (ret)
  192. return ret;
  193. meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
  194. ret = regmap_update_bits(pc->reg_pull, reg,
  195. BIT(bit), 0);
  196. if (ret)
  197. return ret;
  198. break;
  199. default:
  200. return -ENOTSUPP;
  201. }
  202. }
  203. return 0;
  204. }
  205. static int meson_pinconf_get_pull(struct meson_pinctrl *pc, unsigned int pin)
  206. {
  207. struct meson_bank *bank;
  208. unsigned int reg, bit, val;
  209. int ret, conf;
  210. ret = meson_get_bank(pc, pin, &bank);
  211. if (ret)
  212. return ret;
  213. meson_calc_reg_and_bit(bank, pin, REG_PULLEN, &reg, &bit);
  214. ret = regmap_read(pc->reg_pullen, reg, &val);
  215. if (ret)
  216. return ret;
  217. if (!(val & BIT(bit))) {
  218. conf = PIN_CONFIG_BIAS_DISABLE;
  219. } else {
  220. meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
  221. ret = regmap_read(pc->reg_pull, reg, &val);
  222. if (ret)
  223. return ret;
  224. if (val & BIT(bit))
  225. conf = PIN_CONFIG_BIAS_PULL_UP;
  226. else
  227. conf = PIN_CONFIG_BIAS_PULL_DOWN;
  228. }
  229. return conf;
  230. }
  231. static int meson_pinconf_get(struct pinctrl_dev *pcdev, unsigned int pin,
  232. unsigned long *config)
  233. {
  234. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  235. enum pin_config_param param = pinconf_to_config_param(*config);
  236. u16 arg;
  237. switch (param) {
  238. case PIN_CONFIG_BIAS_DISABLE:
  239. case PIN_CONFIG_BIAS_PULL_DOWN:
  240. case PIN_CONFIG_BIAS_PULL_UP:
  241. if (meson_pinconf_get_pull(pc, pin) == param)
  242. arg = 1;
  243. else
  244. return -EINVAL;
  245. break;
  246. default:
  247. return -ENOTSUPP;
  248. }
  249. *config = pinconf_to_config_packed(param, arg);
  250. dev_dbg(pc->dev, "pinconf for pin %u is %lu\n", pin, *config);
  251. return 0;
  252. }
  253. static int meson_pinconf_group_set(struct pinctrl_dev *pcdev,
  254. unsigned int num_group,
  255. unsigned long *configs, unsigned num_configs)
  256. {
  257. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  258. struct meson_pmx_group *group = &pc->data->groups[num_group];
  259. int i;
  260. dev_dbg(pc->dev, "set pinconf for group %s\n", group->name);
  261. for (i = 0; i < group->num_pins; i++) {
  262. meson_pinconf_set(pcdev, group->pins[i], configs,
  263. num_configs);
  264. }
  265. return 0;
  266. }
  267. static int meson_pinconf_group_get(struct pinctrl_dev *pcdev,
  268. unsigned int group, unsigned long *config)
  269. {
  270. return -ENOTSUPP;
  271. }
  272. static const struct pinconf_ops meson_pinconf_ops = {
  273. .pin_config_get = meson_pinconf_get,
  274. .pin_config_set = meson_pinconf_set,
  275. .pin_config_group_get = meson_pinconf_group_get,
  276. .pin_config_group_set = meson_pinconf_group_set,
  277. .is_generic = true,
  278. };
  279. static int meson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  280. {
  281. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  282. unsigned int reg, bit;
  283. struct meson_bank *bank;
  284. int ret;
  285. ret = meson_get_bank(pc, gpio, &bank);
  286. if (ret)
  287. return ret;
  288. meson_calc_reg_and_bit(bank, gpio, REG_DIR, &reg, &bit);
  289. return regmap_update_bits(pc->reg_gpio, reg, BIT(bit), BIT(bit));
  290. }
  291. static int meson_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  292. int value)
  293. {
  294. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  295. unsigned int reg, bit;
  296. struct meson_bank *bank;
  297. int ret;
  298. ret = meson_get_bank(pc, gpio, &bank);
  299. if (ret)
  300. return ret;
  301. meson_calc_reg_and_bit(bank, gpio, REG_DIR, &reg, &bit);
  302. ret = regmap_update_bits(pc->reg_gpio, reg, BIT(bit), 0);
  303. if (ret)
  304. return ret;
  305. meson_calc_reg_and_bit(bank, gpio, REG_OUT, &reg, &bit);
  306. return regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
  307. value ? BIT(bit) : 0);
  308. }
  309. static void meson_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
  310. {
  311. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  312. unsigned int reg, bit;
  313. struct meson_bank *bank;
  314. int ret;
  315. ret = meson_get_bank(pc, gpio, &bank);
  316. if (ret)
  317. return;
  318. meson_calc_reg_and_bit(bank, gpio, REG_OUT, &reg, &bit);
  319. regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
  320. value ? BIT(bit) : 0);
  321. }
  322. static int meson_gpio_get(struct gpio_chip *chip, unsigned gpio)
  323. {
  324. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  325. unsigned int reg, bit, val;
  326. struct meson_bank *bank;
  327. int ret;
  328. ret = meson_get_bank(pc, gpio, &bank);
  329. if (ret)
  330. return ret;
  331. meson_calc_reg_and_bit(bank, gpio, REG_IN, &reg, &bit);
  332. regmap_read(pc->reg_gpio, reg, &val);
  333. return !!(val & BIT(bit));
  334. }
  335. static int meson_gpiolib_register(struct meson_pinctrl *pc)
  336. {
  337. int ret;
  338. pc->chip.label = pc->data->name;
  339. pc->chip.parent = pc->dev;
  340. pc->chip.request = gpiochip_generic_request;
  341. pc->chip.free = gpiochip_generic_free;
  342. pc->chip.direction_input = meson_gpio_direction_input;
  343. pc->chip.direction_output = meson_gpio_direction_output;
  344. pc->chip.get = meson_gpio_get;
  345. pc->chip.set = meson_gpio_set;
  346. pc->chip.base = -1;
  347. pc->chip.ngpio = pc->data->num_pins;
  348. pc->chip.can_sleep = false;
  349. pc->chip.of_node = pc->of_node;
  350. pc->chip.of_gpio_n_cells = 2;
  351. ret = gpiochip_add_data(&pc->chip, pc);
  352. if (ret) {
  353. dev_err(pc->dev, "can't add gpio chip %s\n",
  354. pc->data->name);
  355. return ret;
  356. }
  357. return 0;
  358. }
  359. static struct regmap_config meson_regmap_config = {
  360. .reg_bits = 32,
  361. .val_bits = 32,
  362. .reg_stride = 4,
  363. };
  364. static struct regmap *meson_map_resource(struct meson_pinctrl *pc,
  365. struct device_node *node, char *name)
  366. {
  367. struct resource res;
  368. void __iomem *base;
  369. int i;
  370. i = of_property_match_string(node, "reg-names", name);
  371. if (of_address_to_resource(node, i, &res))
  372. return ERR_PTR(-ENOENT);
  373. base = devm_ioremap_resource(pc->dev, &res);
  374. if (IS_ERR(base))
  375. return ERR_CAST(base);
  376. meson_regmap_config.max_register = resource_size(&res) - 4;
  377. meson_regmap_config.name = devm_kasprintf(pc->dev, GFP_KERNEL,
  378. "%s-%s", node->name,
  379. name);
  380. if (!meson_regmap_config.name)
  381. return ERR_PTR(-ENOMEM);
  382. return devm_regmap_init_mmio(pc->dev, base, &meson_regmap_config);
  383. }
  384. static int meson_pinctrl_parse_dt(struct meson_pinctrl *pc,
  385. struct device_node *node)
  386. {
  387. struct device_node *np, *gpio_np = NULL;
  388. for_each_child_of_node(node, np) {
  389. if (!of_find_property(np, "gpio-controller", NULL))
  390. continue;
  391. if (gpio_np) {
  392. dev_err(pc->dev, "multiple gpio nodes\n");
  393. return -EINVAL;
  394. }
  395. gpio_np = np;
  396. }
  397. if (!gpio_np) {
  398. dev_err(pc->dev, "no gpio node found\n");
  399. return -EINVAL;
  400. }
  401. pc->of_node = gpio_np;
  402. pc->reg_mux = meson_map_resource(pc, gpio_np, "mux");
  403. if (IS_ERR(pc->reg_mux)) {
  404. dev_err(pc->dev, "mux registers not found\n");
  405. return PTR_ERR(pc->reg_mux);
  406. }
  407. pc->reg_pull = meson_map_resource(pc, gpio_np, "pull");
  408. if (IS_ERR(pc->reg_pull)) {
  409. dev_err(pc->dev, "pull registers not found\n");
  410. return PTR_ERR(pc->reg_pull);
  411. }
  412. pc->reg_pullen = meson_map_resource(pc, gpio_np, "pull-enable");
  413. /* Use pull region if pull-enable one is not present */
  414. if (IS_ERR(pc->reg_pullen))
  415. pc->reg_pullen = pc->reg_pull;
  416. pc->reg_gpio = meson_map_resource(pc, gpio_np, "gpio");
  417. if (IS_ERR(pc->reg_gpio)) {
  418. dev_err(pc->dev, "gpio registers not found\n");
  419. return PTR_ERR(pc->reg_gpio);
  420. }
  421. return 0;
  422. }
  423. int meson_pinctrl_probe(struct platform_device *pdev)
  424. {
  425. struct device *dev = &pdev->dev;
  426. struct meson_pinctrl *pc;
  427. int ret;
  428. pc = devm_kzalloc(dev, sizeof(struct meson_pinctrl), GFP_KERNEL);
  429. if (!pc)
  430. return -ENOMEM;
  431. pc->dev = dev;
  432. pc->data = (struct meson_pinctrl_data *) of_device_get_match_data(dev);
  433. ret = meson_pinctrl_parse_dt(pc, dev->of_node);
  434. if (ret)
  435. return ret;
  436. pc->desc.name = "pinctrl-meson";
  437. pc->desc.owner = THIS_MODULE;
  438. pc->desc.pctlops = &meson_pctrl_ops;
  439. pc->desc.pmxops = pc->data->pmx_ops;
  440. pc->desc.confops = &meson_pinconf_ops;
  441. pc->desc.pins = pc->data->pins;
  442. pc->desc.npins = pc->data->num_pins;
  443. pc->pcdev = devm_pinctrl_register(pc->dev, &pc->desc, pc);
  444. if (IS_ERR(pc->pcdev)) {
  445. dev_err(pc->dev, "can't register pinctrl device");
  446. return PTR_ERR(pc->pcdev);
  447. }
  448. return meson_gpiolib_register(pc);
  449. }