pinctrl-meson.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Pin controller and GPIO driver for Amlogic Meson SoCs
  4. *
  5. * Copyright (C) 2014 Beniamino Galvani <[email protected]>
  6. */
  7. /*
  8. * The available pins are organized in banks (A,B,C,D,E,X,Y,Z,AO,
  9. * BOOT,CARD for meson6, X,Y,DV,H,Z,AO,BOOT,CARD for meson8 and
  10. * X,Y,DV,H,AO,BOOT,CARD,DIF for meson8b) and each bank has a
  11. * variable number of pins.
  12. *
  13. * The AO bank is special because it belongs to the Always-On power
  14. * domain which can't be powered off; the bank also uses a set of
  15. * registers different from the other banks.
  16. *
  17. * For each pin controller there are 4 different register ranges that
  18. * control the following properties of the pins:
  19. * 1) pin muxing
  20. * 2) pull enable/disable
  21. * 3) pull up/down
  22. * 4) GPIO direction, output value, input value
  23. *
  24. * In some cases the register ranges for pull enable and pull
  25. * direction are the same and thus there are only 3 register ranges.
  26. *
  27. * Since Meson G12A SoC, the ao register ranges for gpio, pull enable
  28. * and pull direction are the same, so there are only 2 register ranges.
  29. *
  30. * For the pull and GPIO configuration every bank uses a contiguous
  31. * set of bits in the register sets described above; the same register
  32. * can be shared by more banks with different offsets.
  33. *
  34. * In addition to this there are some registers shared between all
  35. * banks that control the IRQ functionality. This feature is not
  36. * supported at the moment by the driver.
  37. */
  38. #include <linux/device.h>
  39. #include <linux/gpio/driver.h>
  40. #include <linux/init.h>
  41. #include <linux/io.h>
  42. #include <linux/of.h>
  43. #include <linux/of_address.h>
  44. #include <linux/of_device.h>
  45. #include <linux/pinctrl/pinconf-generic.h>
  46. #include <linux/pinctrl/pinconf.h>
  47. #include <linux/pinctrl/pinctrl.h>
  48. #include <linux/pinctrl/pinmux.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/property.h>
  51. #include <linux/regmap.h>
  52. #include <linux/seq_file.h>
  53. #include "../core.h"
  54. #include "../pinctrl-utils.h"
  55. #include "pinctrl-meson.h"
  56. static const unsigned int meson_bit_strides[] = {
  57. 1, 1, 1, 1, 1, 2, 1
  58. };
  59. /**
  60. * meson_get_bank() - find the bank containing a given pin
  61. *
  62. * @pc: the pinctrl instance
  63. * @pin: the pin number
  64. * @bank: the found bank
  65. *
  66. * Return: 0 on success, a negative value on error
  67. */
  68. static int meson_get_bank(struct meson_pinctrl *pc, unsigned int pin,
  69. struct meson_bank **bank)
  70. {
  71. int i;
  72. for (i = 0; i < pc->data->num_banks; i++) {
  73. if (pin >= pc->data->banks[i].first &&
  74. pin <= pc->data->banks[i].last) {
  75. *bank = &pc->data->banks[i];
  76. return 0;
  77. }
  78. }
  79. return -EINVAL;
  80. }
  81. /**
  82. * meson_calc_reg_and_bit() - calculate register and bit for a pin
  83. *
  84. * @bank: the bank containing the pin
  85. * @pin: the pin number
  86. * @reg_type: the type of register needed (pull-enable, pull, etc...)
  87. * @reg: the computed register offset
  88. * @bit: the computed bit
  89. */
  90. static void meson_calc_reg_and_bit(struct meson_bank *bank, unsigned int pin,
  91. enum meson_reg_type reg_type,
  92. unsigned int *reg, unsigned int *bit)
  93. {
  94. struct meson_reg_desc *desc = &bank->regs[reg_type];
  95. *bit = (desc->bit + pin - bank->first) * meson_bit_strides[reg_type];
  96. *reg = (desc->reg + (*bit / 32)) * 4;
  97. *bit &= 0x1f;
  98. }
  99. static int meson_get_groups_count(struct pinctrl_dev *pcdev)
  100. {
  101. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  102. return pc->data->num_groups;
  103. }
  104. static const char *meson_get_group_name(struct pinctrl_dev *pcdev,
  105. unsigned selector)
  106. {
  107. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  108. return pc->data->groups[selector].name;
  109. }
  110. static int meson_get_group_pins(struct pinctrl_dev *pcdev, unsigned selector,
  111. const unsigned **pins, unsigned *num_pins)
  112. {
  113. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  114. *pins = pc->data->groups[selector].pins;
  115. *num_pins = pc->data->groups[selector].num_pins;
  116. return 0;
  117. }
  118. static void meson_pin_dbg_show(struct pinctrl_dev *pcdev, struct seq_file *s,
  119. unsigned offset)
  120. {
  121. seq_printf(s, " %s", dev_name(pcdev->dev));
  122. }
  123. static const struct pinctrl_ops meson_pctrl_ops = {
  124. .get_groups_count = meson_get_groups_count,
  125. .get_group_name = meson_get_group_name,
  126. .get_group_pins = meson_get_group_pins,
  127. .dt_node_to_map = pinconf_generic_dt_node_to_map_all,
  128. .dt_free_map = pinctrl_utils_free_map,
  129. .pin_dbg_show = meson_pin_dbg_show,
  130. };
  131. int meson_pmx_get_funcs_count(struct pinctrl_dev *pcdev)
  132. {
  133. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  134. return pc->data->num_funcs;
  135. }
  136. EXPORT_SYMBOL_GPL(meson_pmx_get_funcs_count);
  137. const char *meson_pmx_get_func_name(struct pinctrl_dev *pcdev,
  138. unsigned selector)
  139. {
  140. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  141. return pc->data->funcs[selector].name;
  142. }
  143. EXPORT_SYMBOL_GPL(meson_pmx_get_func_name);
  144. int meson_pmx_get_groups(struct pinctrl_dev *pcdev, unsigned selector,
  145. const char * const **groups,
  146. unsigned * const num_groups)
  147. {
  148. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  149. *groups = pc->data->funcs[selector].groups;
  150. *num_groups = pc->data->funcs[selector].num_groups;
  151. return 0;
  152. }
  153. EXPORT_SYMBOL_GPL(meson_pmx_get_groups);
  154. static int meson_pinconf_set_gpio_bit(struct meson_pinctrl *pc,
  155. unsigned int pin,
  156. unsigned int reg_type,
  157. bool arg)
  158. {
  159. struct meson_bank *bank;
  160. unsigned int reg, bit;
  161. int ret;
  162. ret = meson_get_bank(pc, pin, &bank);
  163. if (ret)
  164. return ret;
  165. meson_calc_reg_and_bit(bank, pin, reg_type, &reg, &bit);
  166. return regmap_update_bits(pc->reg_gpio, reg, BIT(bit),
  167. arg ? BIT(bit) : 0);
  168. }
  169. static int meson_pinconf_get_gpio_bit(struct meson_pinctrl *pc,
  170. unsigned int pin,
  171. unsigned int reg_type)
  172. {
  173. struct meson_bank *bank;
  174. unsigned int reg, bit, val;
  175. int ret;
  176. ret = meson_get_bank(pc, pin, &bank);
  177. if (ret)
  178. return ret;
  179. meson_calc_reg_and_bit(bank, pin, reg_type, &reg, &bit);
  180. ret = regmap_read(pc->reg_gpio, reg, &val);
  181. if (ret)
  182. return ret;
  183. return BIT(bit) & val ? 1 : 0;
  184. }
  185. static int meson_pinconf_set_output(struct meson_pinctrl *pc,
  186. unsigned int pin,
  187. bool out)
  188. {
  189. return meson_pinconf_set_gpio_bit(pc, pin, MESON_REG_DIR, !out);
  190. }
  191. static int meson_pinconf_get_output(struct meson_pinctrl *pc,
  192. unsigned int pin)
  193. {
  194. int ret = meson_pinconf_get_gpio_bit(pc, pin, MESON_REG_DIR);
  195. if (ret < 0)
  196. return ret;
  197. return !ret;
  198. }
  199. static int meson_pinconf_set_drive(struct meson_pinctrl *pc,
  200. unsigned int pin,
  201. bool high)
  202. {
  203. return meson_pinconf_set_gpio_bit(pc, pin, MESON_REG_OUT, high);
  204. }
  205. static int meson_pinconf_get_drive(struct meson_pinctrl *pc,
  206. unsigned int pin)
  207. {
  208. return meson_pinconf_get_gpio_bit(pc, pin, MESON_REG_OUT);
  209. }
  210. static int meson_pinconf_set_output_drive(struct meson_pinctrl *pc,
  211. unsigned int pin,
  212. bool high)
  213. {
  214. int ret;
  215. ret = meson_pinconf_set_output(pc, pin, true);
  216. if (ret)
  217. return ret;
  218. return meson_pinconf_set_drive(pc, pin, high);
  219. }
  220. static int meson_pinconf_disable_bias(struct meson_pinctrl *pc,
  221. unsigned int pin)
  222. {
  223. struct meson_bank *bank;
  224. unsigned int reg, bit = 0;
  225. int ret;
  226. ret = meson_get_bank(pc, pin, &bank);
  227. if (ret)
  228. return ret;
  229. meson_calc_reg_and_bit(bank, pin, MESON_REG_PULLEN, &reg, &bit);
  230. ret = regmap_update_bits(pc->reg_pullen, reg, BIT(bit), 0);
  231. if (ret)
  232. return ret;
  233. return 0;
  234. }
  235. static int meson_pinconf_enable_bias(struct meson_pinctrl *pc, unsigned int pin,
  236. bool pull_up)
  237. {
  238. struct meson_bank *bank;
  239. unsigned int reg, bit, val = 0;
  240. int ret;
  241. ret = meson_get_bank(pc, pin, &bank);
  242. if (ret)
  243. return ret;
  244. meson_calc_reg_and_bit(bank, pin, MESON_REG_PULL, &reg, &bit);
  245. if (pull_up)
  246. val = BIT(bit);
  247. ret = regmap_update_bits(pc->reg_pull, reg, BIT(bit), val);
  248. if (ret)
  249. return ret;
  250. meson_calc_reg_and_bit(bank, pin, MESON_REG_PULLEN, &reg, &bit);
  251. ret = regmap_update_bits(pc->reg_pullen, reg, BIT(bit), BIT(bit));
  252. if (ret)
  253. return ret;
  254. return 0;
  255. }
  256. static int meson_pinconf_set_drive_strength(struct meson_pinctrl *pc,
  257. unsigned int pin,
  258. u16 drive_strength_ua)
  259. {
  260. struct meson_bank *bank;
  261. unsigned int reg, bit, ds_val;
  262. int ret;
  263. if (!pc->reg_ds) {
  264. dev_err(pc->dev, "drive-strength not supported\n");
  265. return -ENOTSUPP;
  266. }
  267. ret = meson_get_bank(pc, pin, &bank);
  268. if (ret)
  269. return ret;
  270. meson_calc_reg_and_bit(bank, pin, MESON_REG_DS, &reg, &bit);
  271. if (drive_strength_ua <= 500) {
  272. ds_val = MESON_PINCONF_DRV_500UA;
  273. } else if (drive_strength_ua <= 2500) {
  274. ds_val = MESON_PINCONF_DRV_2500UA;
  275. } else if (drive_strength_ua <= 3000) {
  276. ds_val = MESON_PINCONF_DRV_3000UA;
  277. } else if (drive_strength_ua <= 4000) {
  278. ds_val = MESON_PINCONF_DRV_4000UA;
  279. } else {
  280. dev_warn_once(pc->dev,
  281. "pin %u: invalid drive-strength : %d , default to 4mA\n",
  282. pin, drive_strength_ua);
  283. ds_val = MESON_PINCONF_DRV_4000UA;
  284. }
  285. ret = regmap_update_bits(pc->reg_ds, reg, 0x3 << bit, ds_val << bit);
  286. if (ret)
  287. return ret;
  288. return 0;
  289. }
  290. static int meson_pinconf_set(struct pinctrl_dev *pcdev, unsigned int pin,
  291. unsigned long *configs, unsigned num_configs)
  292. {
  293. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  294. enum pin_config_param param;
  295. unsigned int arg = 0;
  296. int i, ret;
  297. for (i = 0; i < num_configs; i++) {
  298. param = pinconf_to_config_param(configs[i]);
  299. switch (param) {
  300. case PIN_CONFIG_DRIVE_STRENGTH_UA:
  301. case PIN_CONFIG_OUTPUT_ENABLE:
  302. case PIN_CONFIG_OUTPUT:
  303. arg = pinconf_to_config_argument(configs[i]);
  304. break;
  305. default:
  306. break;
  307. }
  308. switch (param) {
  309. case PIN_CONFIG_BIAS_DISABLE:
  310. ret = meson_pinconf_disable_bias(pc, pin);
  311. break;
  312. case PIN_CONFIG_BIAS_PULL_UP:
  313. ret = meson_pinconf_enable_bias(pc, pin, true);
  314. break;
  315. case PIN_CONFIG_BIAS_PULL_DOWN:
  316. ret = meson_pinconf_enable_bias(pc, pin, false);
  317. break;
  318. case PIN_CONFIG_DRIVE_STRENGTH_UA:
  319. ret = meson_pinconf_set_drive_strength(pc, pin, arg);
  320. break;
  321. case PIN_CONFIG_OUTPUT_ENABLE:
  322. ret = meson_pinconf_set_output(pc, pin, arg);
  323. break;
  324. case PIN_CONFIG_OUTPUT:
  325. ret = meson_pinconf_set_output_drive(pc, pin, arg);
  326. break;
  327. default:
  328. ret = -ENOTSUPP;
  329. }
  330. if (ret)
  331. return ret;
  332. }
  333. return 0;
  334. }
  335. static int meson_pinconf_get_pull(struct meson_pinctrl *pc, unsigned int pin)
  336. {
  337. struct meson_bank *bank;
  338. unsigned int reg, bit, val;
  339. int ret, conf;
  340. ret = meson_get_bank(pc, pin, &bank);
  341. if (ret)
  342. return ret;
  343. meson_calc_reg_and_bit(bank, pin, MESON_REG_PULLEN, &reg, &bit);
  344. ret = regmap_read(pc->reg_pullen, reg, &val);
  345. if (ret)
  346. return ret;
  347. if (!(val & BIT(bit))) {
  348. conf = PIN_CONFIG_BIAS_DISABLE;
  349. } else {
  350. meson_calc_reg_and_bit(bank, pin, MESON_REG_PULL, &reg, &bit);
  351. ret = regmap_read(pc->reg_pull, reg, &val);
  352. if (ret)
  353. return ret;
  354. if (val & BIT(bit))
  355. conf = PIN_CONFIG_BIAS_PULL_UP;
  356. else
  357. conf = PIN_CONFIG_BIAS_PULL_DOWN;
  358. }
  359. return conf;
  360. }
  361. static int meson_pinconf_get_drive_strength(struct meson_pinctrl *pc,
  362. unsigned int pin,
  363. u16 *drive_strength_ua)
  364. {
  365. struct meson_bank *bank;
  366. unsigned int reg, bit;
  367. unsigned int val;
  368. int ret;
  369. if (!pc->reg_ds)
  370. return -ENOTSUPP;
  371. ret = meson_get_bank(pc, pin, &bank);
  372. if (ret)
  373. return ret;
  374. meson_calc_reg_and_bit(bank, pin, MESON_REG_DS, &reg, &bit);
  375. ret = regmap_read(pc->reg_ds, reg, &val);
  376. if (ret)
  377. return ret;
  378. switch ((val >> bit) & 0x3) {
  379. case MESON_PINCONF_DRV_500UA:
  380. *drive_strength_ua = 500;
  381. break;
  382. case MESON_PINCONF_DRV_2500UA:
  383. *drive_strength_ua = 2500;
  384. break;
  385. case MESON_PINCONF_DRV_3000UA:
  386. *drive_strength_ua = 3000;
  387. break;
  388. case MESON_PINCONF_DRV_4000UA:
  389. *drive_strength_ua = 4000;
  390. break;
  391. default:
  392. return -EINVAL;
  393. }
  394. return 0;
  395. }
  396. static int meson_pinconf_get(struct pinctrl_dev *pcdev, unsigned int pin,
  397. unsigned long *config)
  398. {
  399. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  400. enum pin_config_param param = pinconf_to_config_param(*config);
  401. u16 arg;
  402. int ret;
  403. switch (param) {
  404. case PIN_CONFIG_BIAS_DISABLE:
  405. case PIN_CONFIG_BIAS_PULL_DOWN:
  406. case PIN_CONFIG_BIAS_PULL_UP:
  407. if (meson_pinconf_get_pull(pc, pin) == param)
  408. arg = 1;
  409. else
  410. return -EINVAL;
  411. break;
  412. case PIN_CONFIG_DRIVE_STRENGTH_UA:
  413. ret = meson_pinconf_get_drive_strength(pc, pin, &arg);
  414. if (ret)
  415. return ret;
  416. break;
  417. case PIN_CONFIG_OUTPUT_ENABLE:
  418. ret = meson_pinconf_get_output(pc, pin);
  419. if (ret <= 0)
  420. return -EINVAL;
  421. arg = 1;
  422. break;
  423. case PIN_CONFIG_OUTPUT:
  424. ret = meson_pinconf_get_output(pc, pin);
  425. if (ret <= 0)
  426. return -EINVAL;
  427. ret = meson_pinconf_get_drive(pc, pin);
  428. if (ret < 0)
  429. return -EINVAL;
  430. arg = ret;
  431. break;
  432. default:
  433. return -ENOTSUPP;
  434. }
  435. *config = pinconf_to_config_packed(param, arg);
  436. dev_dbg(pc->dev, "pinconf for pin %u is %lu\n", pin, *config);
  437. return 0;
  438. }
  439. static int meson_pinconf_group_set(struct pinctrl_dev *pcdev,
  440. unsigned int num_group,
  441. unsigned long *configs, unsigned num_configs)
  442. {
  443. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  444. struct meson_pmx_group *group = &pc->data->groups[num_group];
  445. int i;
  446. dev_dbg(pc->dev, "set pinconf for group %s\n", group->name);
  447. for (i = 0; i < group->num_pins; i++) {
  448. meson_pinconf_set(pcdev, group->pins[i], configs,
  449. num_configs);
  450. }
  451. return 0;
  452. }
  453. static int meson_pinconf_group_get(struct pinctrl_dev *pcdev,
  454. unsigned int group, unsigned long *config)
  455. {
  456. return -ENOTSUPP;
  457. }
  458. static const struct pinconf_ops meson_pinconf_ops = {
  459. .pin_config_get = meson_pinconf_get,
  460. .pin_config_set = meson_pinconf_set,
  461. .pin_config_group_get = meson_pinconf_group_get,
  462. .pin_config_group_set = meson_pinconf_group_set,
  463. .is_generic = true,
  464. };
  465. static int meson_gpio_get_direction(struct gpio_chip *chip, unsigned gpio)
  466. {
  467. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  468. int ret;
  469. ret = meson_pinconf_get_output(pc, gpio);
  470. if (ret < 0)
  471. return ret;
  472. return ret ? GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;
  473. }
  474. static int meson_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
  475. {
  476. return meson_pinconf_set_output(gpiochip_get_data(chip), gpio, false);
  477. }
  478. static int meson_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
  479. int value)
  480. {
  481. return meson_pinconf_set_output_drive(gpiochip_get_data(chip),
  482. gpio, value);
  483. }
  484. static void meson_gpio_set(struct gpio_chip *chip, unsigned gpio, int value)
  485. {
  486. meson_pinconf_set_drive(gpiochip_get_data(chip), gpio, value);
  487. }
  488. static int meson_gpio_get(struct gpio_chip *chip, unsigned gpio)
  489. {
  490. struct meson_pinctrl *pc = gpiochip_get_data(chip);
  491. unsigned int reg, bit, val;
  492. struct meson_bank *bank;
  493. int ret;
  494. ret = meson_get_bank(pc, gpio, &bank);
  495. if (ret)
  496. return ret;
  497. meson_calc_reg_and_bit(bank, gpio, MESON_REG_IN, &reg, &bit);
  498. regmap_read(pc->reg_gpio, reg, &val);
  499. return !!(val & BIT(bit));
  500. }
  501. static int meson_gpiolib_register(struct meson_pinctrl *pc)
  502. {
  503. int ret;
  504. pc->chip.label = pc->data->name;
  505. pc->chip.parent = pc->dev;
  506. pc->chip.fwnode = pc->fwnode;
  507. pc->chip.request = gpiochip_generic_request;
  508. pc->chip.free = gpiochip_generic_free;
  509. pc->chip.set_config = gpiochip_generic_config;
  510. pc->chip.get_direction = meson_gpio_get_direction;
  511. pc->chip.direction_input = meson_gpio_direction_input;
  512. pc->chip.direction_output = meson_gpio_direction_output;
  513. pc->chip.get = meson_gpio_get;
  514. pc->chip.set = meson_gpio_set;
  515. pc->chip.base = -1;
  516. pc->chip.ngpio = pc->data->num_pins;
  517. pc->chip.can_sleep = false;
  518. ret = gpiochip_add_data(&pc->chip, pc);
  519. if (ret) {
  520. dev_err(pc->dev, "can't add gpio chip %s\n",
  521. pc->data->name);
  522. return ret;
  523. }
  524. return 0;
  525. }
  526. static struct regmap_config meson_regmap_config = {
  527. .reg_bits = 32,
  528. .val_bits = 32,
  529. .reg_stride = 4,
  530. };
  531. static struct regmap *meson_map_resource(struct meson_pinctrl *pc,
  532. struct device_node *node, char *name)
  533. {
  534. struct resource res;
  535. void __iomem *base;
  536. int i;
  537. i = of_property_match_string(node, "reg-names", name);
  538. if (of_address_to_resource(node, i, &res))
  539. return NULL;
  540. base = devm_ioremap_resource(pc->dev, &res);
  541. if (IS_ERR(base))
  542. return ERR_CAST(base);
  543. meson_regmap_config.max_register = resource_size(&res) - 4;
  544. meson_regmap_config.name = devm_kasprintf(pc->dev, GFP_KERNEL,
  545. "%pOFn-%s", node,
  546. name);
  547. if (!meson_regmap_config.name)
  548. return ERR_PTR(-ENOMEM);
  549. return devm_regmap_init_mmio(pc->dev, base, &meson_regmap_config);
  550. }
  551. static int meson_pinctrl_parse_dt(struct meson_pinctrl *pc)
  552. {
  553. struct device_node *gpio_np;
  554. unsigned int chips;
  555. chips = gpiochip_node_count(pc->dev);
  556. if (!chips) {
  557. dev_err(pc->dev, "no gpio node found\n");
  558. return -EINVAL;
  559. }
  560. if (chips > 1) {
  561. dev_err(pc->dev, "multiple gpio nodes\n");
  562. return -EINVAL;
  563. }
  564. pc->fwnode = gpiochip_node_get_first(pc->dev);
  565. gpio_np = to_of_node(pc->fwnode);
  566. pc->reg_mux = meson_map_resource(pc, gpio_np, "mux");
  567. if (IS_ERR_OR_NULL(pc->reg_mux)) {
  568. dev_err(pc->dev, "mux registers not found\n");
  569. return pc->reg_mux ? PTR_ERR(pc->reg_mux) : -ENOENT;
  570. }
  571. pc->reg_gpio = meson_map_resource(pc, gpio_np, "gpio");
  572. if (IS_ERR_OR_NULL(pc->reg_gpio)) {
  573. dev_err(pc->dev, "gpio registers not found\n");
  574. return pc->reg_gpio ? PTR_ERR(pc->reg_gpio) : -ENOENT;
  575. }
  576. pc->reg_pull = meson_map_resource(pc, gpio_np, "pull");
  577. if (IS_ERR(pc->reg_pull))
  578. pc->reg_pull = NULL;
  579. pc->reg_pullen = meson_map_resource(pc, gpio_np, "pull-enable");
  580. if (IS_ERR(pc->reg_pullen))
  581. pc->reg_pullen = NULL;
  582. pc->reg_ds = meson_map_resource(pc, gpio_np, "ds");
  583. if (IS_ERR(pc->reg_ds)) {
  584. dev_dbg(pc->dev, "ds registers not found - skipping\n");
  585. pc->reg_ds = NULL;
  586. }
  587. if (pc->data->parse_dt)
  588. return pc->data->parse_dt(pc);
  589. return 0;
  590. }
  591. int meson8_aobus_parse_dt_extra(struct meson_pinctrl *pc)
  592. {
  593. if (!pc->reg_pull)
  594. return -EINVAL;
  595. pc->reg_pullen = pc->reg_pull;
  596. return 0;
  597. }
  598. EXPORT_SYMBOL_GPL(meson8_aobus_parse_dt_extra);
  599. int meson_a1_parse_dt_extra(struct meson_pinctrl *pc)
  600. {
  601. pc->reg_pull = pc->reg_gpio;
  602. pc->reg_pullen = pc->reg_gpio;
  603. pc->reg_ds = pc->reg_gpio;
  604. return 0;
  605. }
  606. EXPORT_SYMBOL_GPL(meson_a1_parse_dt_extra);
  607. int meson_pinctrl_probe(struct platform_device *pdev)
  608. {
  609. struct device *dev = &pdev->dev;
  610. struct meson_pinctrl *pc;
  611. int ret;
  612. pc = devm_kzalloc(dev, sizeof(struct meson_pinctrl), GFP_KERNEL);
  613. if (!pc)
  614. return -ENOMEM;
  615. pc->dev = dev;
  616. pc->data = (struct meson_pinctrl_data *) of_device_get_match_data(dev);
  617. ret = meson_pinctrl_parse_dt(pc);
  618. if (ret)
  619. return ret;
  620. pc->desc.name = "pinctrl-meson";
  621. pc->desc.owner = THIS_MODULE;
  622. pc->desc.pctlops = &meson_pctrl_ops;
  623. pc->desc.pmxops = pc->data->pmx_ops;
  624. pc->desc.confops = &meson_pinconf_ops;
  625. pc->desc.pins = pc->data->pins;
  626. pc->desc.npins = pc->data->num_pins;
  627. pc->pcdev = devm_pinctrl_register(pc->dev, &pc->desc, pc);
  628. if (IS_ERR(pc->pcdev)) {
  629. dev_err(pc->dev, "can't register pinctrl device");
  630. return PTR_ERR(pc->pcdev);
  631. }
  632. return meson_gpiolib_register(pc);
  633. }
  634. EXPORT_SYMBOL_GPL(meson_pinctrl_probe);
  635. MODULE_LICENSE("GPL v2");