pinctrl-meson.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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/driver.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_PULLEN, &reg,
  166. &bit);
  167. ret = regmap_update_bits(pc->reg_pullen, reg,
  168. BIT(bit), 0);
  169. if (ret)
  170. return ret;
  171. break;
  172. case PIN_CONFIG_BIAS_PULL_UP:
  173. dev_dbg(pc->dev, "pin %u: enable pull-up\n", pin);
  174. meson_calc_reg_and_bit(bank, pin, REG_PULLEN,
  175. &reg, &bit);
  176. ret = regmap_update_bits(pc->reg_pullen, reg,
  177. BIT(bit), BIT(bit));
  178. if (ret)
  179. return ret;
  180. meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
  181. ret = regmap_update_bits(pc->reg_pull, reg,
  182. BIT(bit), BIT(bit));
  183. if (ret)
  184. return ret;
  185. break;
  186. case PIN_CONFIG_BIAS_PULL_DOWN:
  187. dev_dbg(pc->dev, "pin %u: enable pull-down\n", pin);
  188. meson_calc_reg_and_bit(bank, pin, REG_PULLEN,
  189. &reg, &bit);
  190. ret = regmap_update_bits(pc->reg_pullen, reg,
  191. BIT(bit), BIT(bit));
  192. if (ret)
  193. return ret;
  194. meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
  195. ret = regmap_update_bits(pc->reg_pull, reg,
  196. BIT(bit), 0);
  197. if (ret)
  198. return ret;
  199. break;
  200. default:
  201. return -ENOTSUPP;
  202. }
  203. }
  204. return 0;
  205. }
  206. static int meson_pinconf_get_pull(struct meson_pinctrl *pc, unsigned int pin)
  207. {
  208. struct meson_bank *bank;
  209. unsigned int reg, bit, val;
  210. int ret, conf;
  211. ret = meson_get_bank(pc, pin, &bank);
  212. if (ret)
  213. return ret;
  214. meson_calc_reg_and_bit(bank, pin, REG_PULLEN, &reg, &bit);
  215. ret = regmap_read(pc->reg_pullen, reg, &val);
  216. if (ret)
  217. return ret;
  218. if (!(val & BIT(bit))) {
  219. conf = PIN_CONFIG_BIAS_DISABLE;
  220. } else {
  221. meson_calc_reg_and_bit(bank, pin, REG_PULL, &reg, &bit);
  222. ret = regmap_read(pc->reg_pull, reg, &val);
  223. if (ret)
  224. return ret;
  225. if (val & BIT(bit))
  226. conf = PIN_CONFIG_BIAS_PULL_UP;
  227. else
  228. conf = PIN_CONFIG_BIAS_PULL_DOWN;
  229. }
  230. return conf;
  231. }
  232. static int meson_pinconf_get(struct pinctrl_dev *pcdev, unsigned int pin,
  233. unsigned long *config)
  234. {
  235. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  236. enum pin_config_param param = pinconf_to_config_param(*config);
  237. u16 arg;
  238. switch (param) {
  239. case PIN_CONFIG_BIAS_DISABLE:
  240. case PIN_CONFIG_BIAS_PULL_DOWN:
  241. case PIN_CONFIG_BIAS_PULL_UP:
  242. if (meson_pinconf_get_pull(pc, pin) == param)
  243. arg = 1;
  244. else
  245. return -EINVAL;
  246. break;
  247. default:
  248. return -ENOTSUPP;
  249. }
  250. *config = pinconf_to_config_packed(param, arg);
  251. dev_dbg(pc->dev, "pinconf for pin %u is %lu\n", pin, *config);
  252. return 0;
  253. }
  254. static int meson_pinconf_group_set(struct pinctrl_dev *pcdev,
  255. unsigned int num_group,
  256. unsigned long *configs, unsigned num_configs)
  257. {
  258. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  259. struct meson_pmx_group *group = &pc->data->groups[num_group];
  260. int i;
  261. dev_dbg(pc->dev, "set pinconf for group %s\n", group->name);
  262. for (i = 0; i < group->num_pins; i++) {
  263. meson_pinconf_set(pcdev, group->pins[i], configs,
  264. num_configs);
  265. }
  266. return 0;
  267. }
  268. static int meson_pinconf_group_get(struct pinctrl_dev *pcdev,
  269. unsigned int group, unsigned long *config)
  270. {
  271. return -ENOTSUPP;
  272. }
  273. static const struct pinconf_ops meson_pinconf_ops = {
  274. .pin_config_get = meson_pinconf_get,
  275. .pin_config_set = meson_pinconf_set,
  276. .pin_config_group_get = meson_pinconf_group_get,
  277. .pin_config_group_set = meson_pinconf_group_set,
  278. .is_generic = true,
  279. };
  280. static int meson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  281. {
  282. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  283. unsigned int reg, bit;
  284. struct meson_bank *bank;
  285. int ret;
  286. ret = meson_get_bank(pc, gpio, &bank);
  287. if (ret)
  288. return ret;
  289. meson_calc_reg_and_bit(bank, gpio, REG_DIR, &reg, &bit);
  290. return regmap_update_bits(pc->reg_gpio, reg, BIT(bit), BIT(bit));
  291. }
  292. static int meson_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  293. int value)
  294. {
  295. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  296. unsigned int reg, bit;
  297. struct meson_bank *bank;
  298. int ret;
  299. ret = meson_get_bank(pc, gpio, &bank);
  300. if (ret)
  301. return ret;
  302. meson_calc_reg_and_bit(bank, gpio, REG_DIR, &reg, &bit);
  303. ret = regmap_update_bits(pc->reg_gpio, reg, BIT(bit), 0);
  304. if (ret)
  305. return ret;
  306. meson_calc_reg_and_bit(bank, gpio, REG_OUT, &reg, &bit);
  307. return regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
  308. value ? BIT(bit) : 0);
  309. }
  310. static void meson_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
  311. {
  312. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  313. unsigned int reg, bit;
  314. struct meson_bank *bank;
  315. int ret;
  316. ret = meson_get_bank(pc, gpio, &bank);
  317. if (ret)
  318. return;
  319. meson_calc_reg_and_bit(bank, gpio, REG_OUT, &reg, &bit);
  320. regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
  321. value ? BIT(bit) : 0);
  322. }
  323. static int meson_gpio_get(struct gpio_chip *chip, unsigned gpio)
  324. {
  325. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  326. unsigned int reg, bit, val;
  327. struct meson_bank *bank;
  328. int ret;
  329. ret = meson_get_bank(pc, gpio, &bank);
  330. if (ret)
  331. return ret;
  332. meson_calc_reg_and_bit(bank, gpio, REG_IN, &reg, &bit);
  333. regmap_read(pc->reg_gpio, reg, &val);
  334. return !!(val & BIT(bit));
  335. }
  336. static int meson_gpiolib_register(struct meson_pinctrl *pc)
  337. {
  338. int ret;
  339. pc->chip.label = pc->data->name;
  340. pc->chip.parent = pc->dev;
  341. pc->chip.request = gpiochip_generic_request;
  342. pc->chip.free = gpiochip_generic_free;
  343. pc->chip.direction_input = meson_gpio_direction_input;
  344. pc->chip.direction_output = meson_gpio_direction_output;
  345. pc->chip.get = meson_gpio_get;
  346. pc->chip.set = meson_gpio_set;
  347. pc->chip.base = -1;
  348. pc->chip.ngpio = pc->data->num_pins;
  349. pc->chip.can_sleep = false;
  350. pc->chip.of_node = pc->of_node;
  351. pc->chip.of_gpio_n_cells = 2;
  352. ret = gpiochip_add_data(&pc->chip, pc);
  353. if (ret) {
  354. dev_err(pc->dev, "can't add gpio chip %s\n",
  355. pc->data->name);
  356. return ret;
  357. }
  358. return 0;
  359. }
  360. static struct regmap_config meson_regmap_config = {
  361. .reg_bits = 32,
  362. .val_bits = 32,
  363. .reg_stride = 4,
  364. };
  365. static struct regmap *meson_map_resource(struct meson_pinctrl *pc,
  366. struct device_node *node, char *name)
  367. {
  368. struct resource res;
  369. void __iomem *base;
  370. int i;
  371. i = of_property_match_string(node, "reg-names", name);
  372. if (of_address_to_resource(node, i, &res))
  373. return ERR_PTR(-ENOENT);
  374. base = devm_ioremap_resource(pc->dev, &res);
  375. if (IS_ERR(base))
  376. return ERR_CAST(base);
  377. meson_regmap_config.max_register = resource_size(&res) - 4;
  378. meson_regmap_config.name = devm_kasprintf(pc->dev, GFP_KERNEL,
  379. "%pOFn-%s", node,
  380. name);
  381. if (!meson_regmap_config.name)
  382. return ERR_PTR(-ENOMEM);
  383. return devm_regmap_init_mmio(pc->dev, base, &meson_regmap_config);
  384. }
  385. static int meson_pinctrl_parse_dt(struct meson_pinctrl *pc,
  386. struct device_node *node)
  387. {
  388. struct device_node *np, *gpio_np = NULL;
  389. for_each_child_of_node(node, np) {
  390. if (!of_find_property(np, "gpio-controller", NULL))
  391. continue;
  392. if (gpio_np) {
  393. dev_err(pc->dev, "multiple gpio nodes\n");
  394. return -EINVAL;
  395. }
  396. gpio_np = np;
  397. }
  398. if (!gpio_np) {
  399. dev_err(pc->dev, "no gpio node found\n");
  400. return -EINVAL;
  401. }
  402. pc->of_node = gpio_np;
  403. pc->reg_mux = meson_map_resource(pc, gpio_np, "mux");
  404. if (IS_ERR(pc->reg_mux)) {
  405. dev_err(pc->dev, "mux registers not found\n");
  406. return PTR_ERR(pc->reg_mux);
  407. }
  408. pc->reg_pull = meson_map_resource(pc, gpio_np, "pull");
  409. if (IS_ERR(pc->reg_pull)) {
  410. dev_err(pc->dev, "pull registers not found\n");
  411. return PTR_ERR(pc->reg_pull);
  412. }
  413. pc->reg_pullen = meson_map_resource(pc, gpio_np, "pull-enable");
  414. /* Use pull region if pull-enable one is not present */
  415. if (IS_ERR(pc->reg_pullen))
  416. pc->reg_pullen = pc->reg_pull;
  417. pc->reg_gpio = meson_map_resource(pc, gpio_np, "gpio");
  418. if (IS_ERR(pc->reg_gpio)) {
  419. dev_err(pc->dev, "gpio registers not found\n");
  420. return PTR_ERR(pc->reg_gpio);
  421. }
  422. return 0;
  423. }
  424. int meson_pinctrl_probe(struct platform_device *pdev)
  425. {
  426. struct device *dev = &pdev->dev;
  427. struct meson_pinctrl *pc;
  428. int ret;
  429. pc = devm_kzalloc(dev, sizeof(struct meson_pinctrl), GFP_KERNEL);
  430. if (!pc)
  431. return -ENOMEM;
  432. pc->dev = dev;
  433. pc->data = (struct meson_pinctrl_data *) of_device_get_match_data(dev);
  434. ret = meson_pinctrl_parse_dt(pc, dev->of_node);
  435. if (ret)
  436. return ret;
  437. pc->desc.name = "pinctrl-meson";
  438. pc->desc.owner = THIS_MODULE;
  439. pc->desc.pctlops = &meson_pctrl_ops;
  440. pc->desc.pmxops = pc->data->pmx_ops;
  441. pc->desc.confops = &meson_pinconf_ops;
  442. pc->desc.pins = pc->data->pins;
  443. pc->desc.npins = pc->data->num_pins;
  444. pc->pcdev = devm_pinctrl_register(pc->dev, &pc->desc, pc);
  445. if (IS_ERR(pc->pcdev)) {
  446. dev_err(pc->dev, "can't register pinctrl device");
  447. return PTR_ERR(pc->pcdev);
  448. }
  449. return meson_gpiolib_register(pc);
  450. }