pinctrl-as3722.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * ams AS3722 pin control and GPIO driver.
  3. *
  4. * Copyright (c) 2013, NVIDIA Corporation.
  5. *
  6. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  13. * whether express or implied; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  20. * 02111-1307, USA
  21. */
  22. #include <linux/delay.h>
  23. #include <linux/gpio.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/mfd/as3722.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/pinctrl/consumer.h>
  31. #include <linux/pinctrl/machine.h>
  32. #include <linux/pinctrl/pinctrl.h>
  33. #include <linux/pinctrl/pinconf-generic.h>
  34. #include <linux/pinctrl/pinconf.h>
  35. #include <linux/pinctrl/pinmux.h>
  36. #include <linux/pm.h>
  37. #include <linux/slab.h>
  38. #include "core.h"
  39. #include "pinconf.h"
  40. #include "pinctrl-utils.h"
  41. #define AS3722_PIN_GPIO0 0
  42. #define AS3722_PIN_GPIO1 1
  43. #define AS3722_PIN_GPIO2 2
  44. #define AS3722_PIN_GPIO3 3
  45. #define AS3722_PIN_GPIO4 4
  46. #define AS3722_PIN_GPIO5 5
  47. #define AS3722_PIN_GPIO6 6
  48. #define AS3722_PIN_GPIO7 7
  49. #define AS3722_PIN_NUM (AS3722_PIN_GPIO7 + 1)
  50. #define AS3722_GPIO_MODE_PULL_UP BIT(PIN_CONFIG_BIAS_PULL_UP)
  51. #define AS3722_GPIO_MODE_PULL_DOWN BIT(PIN_CONFIG_BIAS_PULL_DOWN)
  52. #define AS3722_GPIO_MODE_HIGH_IMPED BIT(PIN_CONFIG_BIAS_HIGH_IMPEDANCE)
  53. #define AS3722_GPIO_MODE_OPEN_DRAIN BIT(PIN_CONFIG_DRIVE_OPEN_DRAIN)
  54. struct as3722_pin_function {
  55. const char *name;
  56. const char * const *groups;
  57. unsigned ngroups;
  58. int mux_option;
  59. };
  60. struct as3722_gpio_pin_control {
  61. bool enable_gpio_invert;
  62. unsigned mode_prop;
  63. int io_function;
  64. };
  65. struct as3722_pingroup {
  66. const char *name;
  67. const unsigned pins[1];
  68. unsigned npins;
  69. };
  70. struct as3722_pctrl_info {
  71. struct device *dev;
  72. struct pinctrl_dev *pctl;
  73. struct as3722 *as3722;
  74. struct gpio_chip gpio_chip;
  75. int pins_current_opt[AS3722_PIN_NUM];
  76. const struct as3722_pin_function *functions;
  77. unsigned num_functions;
  78. const struct as3722_pingroup *pin_groups;
  79. int num_pin_groups;
  80. const struct pinctrl_pin_desc *pins;
  81. unsigned num_pins;
  82. struct as3722_gpio_pin_control gpio_control[AS3722_PIN_NUM];
  83. };
  84. static const struct pinctrl_pin_desc as3722_pins_desc[] = {
  85. PINCTRL_PIN(AS3722_PIN_GPIO0, "gpio0"),
  86. PINCTRL_PIN(AS3722_PIN_GPIO1, "gpio1"),
  87. PINCTRL_PIN(AS3722_PIN_GPIO2, "gpio2"),
  88. PINCTRL_PIN(AS3722_PIN_GPIO3, "gpio3"),
  89. PINCTRL_PIN(AS3722_PIN_GPIO4, "gpio4"),
  90. PINCTRL_PIN(AS3722_PIN_GPIO5, "gpio5"),
  91. PINCTRL_PIN(AS3722_PIN_GPIO6, "gpio6"),
  92. PINCTRL_PIN(AS3722_PIN_GPIO7, "gpio7"),
  93. };
  94. static const char * const gpio_groups[] = {
  95. "gpio0",
  96. "gpio1",
  97. "gpio2",
  98. "gpio3",
  99. "gpio4",
  100. "gpio5",
  101. "gpio6",
  102. "gpio7",
  103. };
  104. enum as3722_pinmux_option {
  105. AS3722_PINMUX_GPIO = 0,
  106. AS3722_PINMUX_INTERRUPT_OUT = 1,
  107. AS3722_PINMUX_VSUB_VBAT_UNDEB_LOW_OUT = 2,
  108. AS3722_PINMUX_GPIO_INTERRUPT = 3,
  109. AS3722_PINMUX_PWM_INPUT = 4,
  110. AS3722_PINMUX_VOLTAGE_IN_STBY = 5,
  111. AS3722_PINMUX_OC_PG_SD0 = 6,
  112. AS3722_PINMUX_PG_OUT = 7,
  113. AS3722_PINMUX_CLK32K_OUT = 8,
  114. AS3722_PINMUX_WATCHDOG_INPUT = 9,
  115. AS3722_PINMUX_SOFT_RESET_IN = 11,
  116. AS3722_PINMUX_PWM_OUTPUT = 12,
  117. AS3722_PINMUX_VSUB_VBAT_LOW_DEB_OUT = 13,
  118. AS3722_PINMUX_OC_PG_SD6 = 14,
  119. };
  120. #define FUNCTION_GROUP(fname, mux) \
  121. { \
  122. .name = #fname, \
  123. .groups = gpio_groups, \
  124. .ngroups = ARRAY_SIZE(gpio_groups), \
  125. .mux_option = AS3722_PINMUX_##mux, \
  126. }
  127. static const struct as3722_pin_function as3722_pin_function[] = {
  128. FUNCTION_GROUP(gpio, GPIO),
  129. FUNCTION_GROUP(interrupt-out, INTERRUPT_OUT),
  130. FUNCTION_GROUP(gpio-in-interrupt, GPIO_INTERRUPT),
  131. FUNCTION_GROUP(vsup-vbat-low-undebounce-out, VSUB_VBAT_UNDEB_LOW_OUT),
  132. FUNCTION_GROUP(vsup-vbat-low-debounce-out, VSUB_VBAT_LOW_DEB_OUT),
  133. FUNCTION_GROUP(voltage-in-standby, VOLTAGE_IN_STBY),
  134. FUNCTION_GROUP(oc-pg-sd0, OC_PG_SD0),
  135. FUNCTION_GROUP(oc-pg-sd6, OC_PG_SD6),
  136. FUNCTION_GROUP(powergood-out, PG_OUT),
  137. FUNCTION_GROUP(pwm-in, PWM_INPUT),
  138. FUNCTION_GROUP(pwm-out, PWM_OUTPUT),
  139. FUNCTION_GROUP(clk32k-out, CLK32K_OUT),
  140. FUNCTION_GROUP(watchdog-in, WATCHDOG_INPUT),
  141. FUNCTION_GROUP(soft-reset-in, SOFT_RESET_IN),
  142. };
  143. #define AS3722_PINGROUP(pg_name, pin_id) \
  144. { \
  145. .name = #pg_name, \
  146. .pins = {AS3722_PIN_##pin_id}, \
  147. .npins = 1, \
  148. }
  149. static const struct as3722_pingroup as3722_pingroups[] = {
  150. AS3722_PINGROUP(gpio0, GPIO0),
  151. AS3722_PINGROUP(gpio1, GPIO1),
  152. AS3722_PINGROUP(gpio2, GPIO2),
  153. AS3722_PINGROUP(gpio3, GPIO3),
  154. AS3722_PINGROUP(gpio4, GPIO4),
  155. AS3722_PINGROUP(gpio5, GPIO5),
  156. AS3722_PINGROUP(gpio6, GPIO6),
  157. AS3722_PINGROUP(gpio7, GPIO7),
  158. };
  159. static int as3722_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  160. {
  161. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  162. return as_pci->num_pin_groups;
  163. }
  164. static const char *as3722_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  165. unsigned group)
  166. {
  167. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  168. return as_pci->pin_groups[group].name;
  169. }
  170. static int as3722_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  171. unsigned group, const unsigned **pins, unsigned *num_pins)
  172. {
  173. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  174. *pins = as_pci->pin_groups[group].pins;
  175. *num_pins = as_pci->pin_groups[group].npins;
  176. return 0;
  177. }
  178. static const struct pinctrl_ops as3722_pinctrl_ops = {
  179. .get_groups_count = as3722_pinctrl_get_groups_count,
  180. .get_group_name = as3722_pinctrl_get_group_name,
  181. .get_group_pins = as3722_pinctrl_get_group_pins,
  182. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  183. .dt_free_map = pinctrl_utils_dt_free_map,
  184. };
  185. static int as3722_pinctrl_get_funcs_count(struct pinctrl_dev *pctldev)
  186. {
  187. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  188. return as_pci->num_functions;
  189. }
  190. static const char *as3722_pinctrl_get_func_name(struct pinctrl_dev *pctldev,
  191. unsigned function)
  192. {
  193. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  194. return as_pci->functions[function].name;
  195. }
  196. static int as3722_pinctrl_get_func_groups(struct pinctrl_dev *pctldev,
  197. unsigned function, const char * const **groups,
  198. unsigned * const num_groups)
  199. {
  200. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  201. *groups = as_pci->functions[function].groups;
  202. *num_groups = as_pci->functions[function].ngroups;
  203. return 0;
  204. }
  205. static int as3722_pinctrl_enable(struct pinctrl_dev *pctldev, unsigned function,
  206. unsigned group)
  207. {
  208. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  209. int gpio_cntr_reg = AS3722_GPIOn_CONTROL_REG(group);
  210. u8 val = AS3722_GPIO_IOSF_VAL(as_pci->functions[function].mux_option);
  211. int ret;
  212. dev_dbg(as_pci->dev, "%s(): GPIO %u pin to function %u and val %u\n",
  213. __func__, group, function, val);
  214. ret = as3722_update_bits(as_pci->as3722, gpio_cntr_reg,
  215. AS3722_GPIO_IOSF_MASK, val);
  216. if (ret < 0) {
  217. dev_err(as_pci->dev, "GPIO%d_CTRL_REG update failed %d\n",
  218. group, ret);
  219. return ret;
  220. }
  221. as_pci->gpio_control[group].io_function = function;
  222. return ret;
  223. }
  224. static int as3722_pinctrl_gpio_get_mode(unsigned gpio_mode_prop, bool input)
  225. {
  226. if (gpio_mode_prop & AS3722_GPIO_MODE_HIGH_IMPED)
  227. return -EINVAL;
  228. if (gpio_mode_prop & AS3722_GPIO_MODE_OPEN_DRAIN) {
  229. if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_UP)
  230. return AS3722_GPIO_MODE_IO_OPEN_DRAIN_PULL_UP;
  231. return AS3722_GPIO_MODE_IO_OPEN_DRAIN;
  232. }
  233. if (input) {
  234. if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_UP)
  235. return AS3722_GPIO_MODE_INPUT_PULL_UP;
  236. else if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_DOWN)
  237. return AS3722_GPIO_MODE_INPUT_PULL_DOWN;
  238. return AS3722_GPIO_MODE_INPUT;
  239. }
  240. if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_DOWN)
  241. return AS3722_GPIO_MODE_OUTPUT_VDDL;
  242. return AS3722_GPIO_MODE_OUTPUT_VDDH;
  243. }
  244. static int as3722_pinctrl_gpio_request_enable(struct pinctrl_dev *pctldev,
  245. struct pinctrl_gpio_range *range, unsigned offset)
  246. {
  247. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  248. if (as_pci->gpio_control[offset].io_function)
  249. return -EBUSY;
  250. return 0;
  251. }
  252. static int as3722_pinctrl_gpio_set_direction(struct pinctrl_dev *pctldev,
  253. struct pinctrl_gpio_range *range, unsigned offset, bool input)
  254. {
  255. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  256. struct as3722 *as3722 = as_pci->as3722;
  257. int mode;
  258. mode = as3722_pinctrl_gpio_get_mode(
  259. as_pci->gpio_control[offset].mode_prop, input);
  260. if (mode < 0) {
  261. dev_err(as_pci->dev, "%s direction for GPIO %d not supported\n",
  262. (input) ? "Input" : "Output", offset);
  263. return mode;
  264. }
  265. if (as_pci->gpio_control[offset].enable_gpio_invert)
  266. mode |= AS3722_GPIO_INV;
  267. return as3722_write(as3722, AS3722_GPIOn_CONTROL_REG(offset), mode);
  268. }
  269. static const struct pinmux_ops as3722_pinmux_ops = {
  270. .get_functions_count = as3722_pinctrl_get_funcs_count,
  271. .get_function_name = as3722_pinctrl_get_func_name,
  272. .get_function_groups = as3722_pinctrl_get_func_groups,
  273. .enable = as3722_pinctrl_enable,
  274. .gpio_request_enable = as3722_pinctrl_gpio_request_enable,
  275. .gpio_set_direction = as3722_pinctrl_gpio_set_direction,
  276. };
  277. static int as3722_pinconf_get(struct pinctrl_dev *pctldev,
  278. unsigned pin, unsigned long *config)
  279. {
  280. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  281. enum pin_config_param param = pinconf_to_config_param(*config);
  282. int arg = 0;
  283. u16 prop;
  284. switch (param) {
  285. case PIN_CONFIG_BIAS_DISABLE:
  286. prop = AS3722_GPIO_MODE_PULL_UP |
  287. AS3722_GPIO_MODE_PULL_DOWN;
  288. if (!(as_pci->gpio_control[pin].mode_prop & prop))
  289. arg = 1;
  290. prop = 0;
  291. break;
  292. case PIN_CONFIG_BIAS_PULL_UP:
  293. prop = AS3722_GPIO_MODE_PULL_UP;
  294. break;
  295. case PIN_CONFIG_BIAS_PULL_DOWN:
  296. prop = AS3722_GPIO_MODE_PULL_DOWN;
  297. break;
  298. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  299. prop = AS3722_GPIO_MODE_OPEN_DRAIN;
  300. break;
  301. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  302. prop = AS3722_GPIO_MODE_HIGH_IMPED;
  303. break;
  304. default:
  305. dev_err(as_pci->dev, "Properties not supported\n");
  306. return -ENOTSUPP;
  307. }
  308. if (as_pci->gpio_control[pin].mode_prop & prop)
  309. arg = 1;
  310. *config = pinconf_to_config_packed(param, (u16)arg);
  311. return 0;
  312. }
  313. static int as3722_pinconf_set(struct pinctrl_dev *pctldev,
  314. unsigned pin, unsigned long *configs,
  315. unsigned num_configs)
  316. {
  317. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  318. enum pin_config_param param;
  319. int mode_prop;
  320. int i;
  321. for (i = 0; i < num_configs; i++) {
  322. param = pinconf_to_config_param(configs[i]);
  323. mode_prop = as_pci->gpio_control[pin].mode_prop;
  324. switch (param) {
  325. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  326. break;
  327. case PIN_CONFIG_BIAS_DISABLE:
  328. mode_prop &= ~(AS3722_GPIO_MODE_PULL_UP |
  329. AS3722_GPIO_MODE_PULL_DOWN);
  330. break;
  331. case PIN_CONFIG_BIAS_PULL_UP:
  332. mode_prop |= AS3722_GPIO_MODE_PULL_UP;
  333. break;
  334. case PIN_CONFIG_BIAS_PULL_DOWN:
  335. mode_prop |= AS3722_GPIO_MODE_PULL_DOWN;
  336. break;
  337. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  338. mode_prop |= AS3722_GPIO_MODE_HIGH_IMPED;
  339. break;
  340. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  341. mode_prop |= AS3722_GPIO_MODE_OPEN_DRAIN;
  342. break;
  343. default:
  344. dev_err(as_pci->dev, "Properties not supported\n");
  345. return -ENOTSUPP;
  346. }
  347. as_pci->gpio_control[pin].mode_prop = mode_prop;
  348. }
  349. return 0;
  350. }
  351. static const struct pinconf_ops as3722_pinconf_ops = {
  352. .pin_config_get = as3722_pinconf_get,
  353. .pin_config_set = as3722_pinconf_set,
  354. };
  355. static struct pinctrl_desc as3722_pinctrl_desc = {
  356. .pctlops = &as3722_pinctrl_ops,
  357. .pmxops = &as3722_pinmux_ops,
  358. .confops = &as3722_pinconf_ops,
  359. .owner = THIS_MODULE,
  360. };
  361. static inline struct as3722_pctrl_info *to_as_pci(struct gpio_chip *chip)
  362. {
  363. return container_of(chip, struct as3722_pctrl_info, gpio_chip);
  364. }
  365. static int as3722_gpio_get(struct gpio_chip *chip, unsigned offset)
  366. {
  367. struct as3722_pctrl_info *as_pci = to_as_pci(chip);
  368. struct as3722 *as3722 = as_pci->as3722;
  369. int ret;
  370. u32 reg;
  371. u32 control;
  372. u32 val;
  373. int mode;
  374. int invert_enable;
  375. ret = as3722_read(as3722, AS3722_GPIOn_CONTROL_REG(offset), &control);
  376. if (ret < 0) {
  377. dev_err(as_pci->dev,
  378. "GPIO_CONTROL%d_REG read failed: %d\n", offset, ret);
  379. return ret;
  380. }
  381. invert_enable = !!(control & AS3722_GPIO_INV);
  382. mode = control & AS3722_GPIO_MODE_MASK;
  383. switch (mode) {
  384. case AS3722_GPIO_MODE_INPUT:
  385. case AS3722_GPIO_MODE_INPUT_PULL_UP:
  386. case AS3722_GPIO_MODE_INPUT_PULL_DOWN:
  387. case AS3722_GPIO_MODE_IO_OPEN_DRAIN:
  388. case AS3722_GPIO_MODE_IO_OPEN_DRAIN_PULL_UP:
  389. reg = AS3722_GPIO_SIGNAL_IN_REG;
  390. break;
  391. case AS3722_GPIO_MODE_OUTPUT_VDDH:
  392. case AS3722_GPIO_MODE_OUTPUT_VDDL:
  393. reg = AS3722_GPIO_SIGNAL_OUT_REG;
  394. break;
  395. default:
  396. return -EINVAL;
  397. }
  398. ret = as3722_read(as3722, reg, &val);
  399. if (ret < 0) {
  400. dev_err(as_pci->dev,
  401. "GPIO_SIGNAL_IN_REG read failed: %d\n", ret);
  402. return ret;
  403. }
  404. val = !!(val & AS3722_GPIOn_SIGNAL(offset));
  405. return (invert_enable) ? !val : val;
  406. }
  407. static void as3722_gpio_set(struct gpio_chip *chip, unsigned offset,
  408. int value)
  409. {
  410. struct as3722_pctrl_info *as_pci = to_as_pci(chip);
  411. struct as3722 *as3722 = as_pci->as3722;
  412. int en_invert = as_pci->gpio_control[offset].enable_gpio_invert;
  413. u32 val;
  414. int ret;
  415. if (value)
  416. val = (en_invert) ? 0 : AS3722_GPIOn_SIGNAL(offset);
  417. else
  418. val = (en_invert) ? AS3722_GPIOn_SIGNAL(offset) : 0;
  419. ret = as3722_update_bits(as3722, AS3722_GPIO_SIGNAL_OUT_REG,
  420. AS3722_GPIOn_SIGNAL(offset), val);
  421. if (ret < 0)
  422. dev_err(as_pci->dev,
  423. "GPIO_SIGNAL_OUT_REG update failed: %d\n", ret);
  424. }
  425. static int as3722_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  426. {
  427. return pinctrl_gpio_direction_input(chip->base + offset);
  428. }
  429. static int as3722_gpio_direction_output(struct gpio_chip *chip,
  430. unsigned offset, int value)
  431. {
  432. as3722_gpio_set(chip, offset, value);
  433. return pinctrl_gpio_direction_output(chip->base + offset);
  434. }
  435. static int as3722_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  436. {
  437. struct as3722_pctrl_info *as_pci = to_as_pci(chip);
  438. return as3722_irq_get_virq(as_pci->as3722, offset);
  439. }
  440. static int as3722_gpio_request(struct gpio_chip *chip, unsigned offset)
  441. {
  442. return pinctrl_request_gpio(chip->base + offset);
  443. }
  444. static void as3722_gpio_free(struct gpio_chip *chip, unsigned offset)
  445. {
  446. pinctrl_free_gpio(chip->base + offset);
  447. }
  448. static const struct gpio_chip as3722_gpio_chip = {
  449. .label = "as3722-gpio",
  450. .owner = THIS_MODULE,
  451. .request = as3722_gpio_request,
  452. .free = as3722_gpio_free,
  453. .get = as3722_gpio_get,
  454. .set = as3722_gpio_set,
  455. .direction_input = as3722_gpio_direction_input,
  456. .direction_output = as3722_gpio_direction_output,
  457. .to_irq = as3722_gpio_to_irq,
  458. .can_sleep = 1,
  459. .ngpio = AS3722_PIN_NUM,
  460. .base = -1,
  461. };
  462. static int as3722_pinctrl_probe(struct platform_device *pdev)
  463. {
  464. struct as3722_pctrl_info *as_pci;
  465. int ret;
  466. int tret;
  467. as_pci = devm_kzalloc(&pdev->dev, sizeof(*as_pci), GFP_KERNEL);
  468. if (!as_pci)
  469. return -ENOMEM;
  470. as_pci->dev = &pdev->dev;
  471. as_pci->dev->of_node = pdev->dev.parent->of_node;
  472. as_pci->as3722 = dev_get_drvdata(pdev->dev.parent);
  473. platform_set_drvdata(pdev, as_pci);
  474. as_pci->pins = as3722_pins_desc;
  475. as_pci->num_pins = ARRAY_SIZE(as3722_pins_desc);
  476. as_pci->functions = as3722_pin_function;
  477. as_pci->num_functions = ARRAY_SIZE(as3722_pin_function);
  478. as_pci->pin_groups = as3722_pingroups;
  479. as_pci->num_pin_groups = ARRAY_SIZE(as3722_pingroups);
  480. as3722_pinctrl_desc.name = dev_name(&pdev->dev);
  481. as3722_pinctrl_desc.pins = as3722_pins_desc;
  482. as3722_pinctrl_desc.npins = ARRAY_SIZE(as3722_pins_desc);
  483. as_pci->pctl = pinctrl_register(&as3722_pinctrl_desc,
  484. &pdev->dev, as_pci);
  485. if (!as_pci->pctl) {
  486. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  487. return -EINVAL;
  488. }
  489. as_pci->gpio_chip = as3722_gpio_chip;
  490. as_pci->gpio_chip.dev = &pdev->dev;
  491. as_pci->gpio_chip.of_node = pdev->dev.parent->of_node;
  492. ret = gpiochip_add(&as_pci->gpio_chip);
  493. if (ret < 0) {
  494. dev_err(&pdev->dev, "Couldn't register gpiochip, %d\n", ret);
  495. goto fail_chip_add;
  496. }
  497. ret = gpiochip_add_pin_range(&as_pci->gpio_chip, dev_name(&pdev->dev),
  498. 0, 0, AS3722_PIN_NUM);
  499. if (ret < 0) {
  500. dev_err(&pdev->dev, "Couldn't add pin range, %d\n", ret);
  501. goto fail_range_add;
  502. }
  503. return 0;
  504. fail_range_add:
  505. tret = gpiochip_remove(&as_pci->gpio_chip);
  506. if (tret < 0)
  507. dev_warn(&pdev->dev, "Couldn't remove gpio chip, %d\n", tret);
  508. fail_chip_add:
  509. pinctrl_unregister(as_pci->pctl);
  510. return ret;
  511. }
  512. static int as3722_pinctrl_remove(struct platform_device *pdev)
  513. {
  514. struct as3722_pctrl_info *as_pci = platform_get_drvdata(pdev);
  515. int ret;
  516. ret = gpiochip_remove(&as_pci->gpio_chip);
  517. if (ret < 0)
  518. return ret;
  519. pinctrl_unregister(as_pci->pctl);
  520. return 0;
  521. }
  522. static struct of_device_id as3722_pinctrl_of_match[] = {
  523. { .compatible = "ams,as3722-pinctrl", },
  524. { },
  525. };
  526. MODULE_DEVICE_TABLE(of, as3722_pinctrl_of_match);
  527. static struct platform_driver as3722_pinctrl_driver = {
  528. .driver = {
  529. .name = "as3722-pinctrl",
  530. .owner = THIS_MODULE,
  531. .of_match_table = as3722_pinctrl_of_match,
  532. },
  533. .probe = as3722_pinctrl_probe,
  534. .remove = as3722_pinctrl_remove,
  535. };
  536. module_platform_driver(as3722_pinctrl_driver);
  537. MODULE_ALIAS("platform:as3722-pinctrl");
  538. MODULE_DESCRIPTION("AS3722 pin control and GPIO driver");
  539. MODULE_AUTHOR("Laxman Dewangan<ldewangan@nvidia.com>");
  540. MODULE_LICENSE("GPL v2");