pinctrl-as3722.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. switch (val) {
  223. case AS3722_GPIO_IOSF_SD0_OUT:
  224. case AS3722_GPIO_IOSF_PWR_GOOD_OUT:
  225. case AS3722_GPIO_IOSF_Q32K_OUT:
  226. case AS3722_GPIO_IOSF_PWM_OUT:
  227. case AS3722_GPIO_IOSF_SD6_LOW_VOLT_LOW:
  228. ret = as3722_update_bits(as_pci->as3722, gpio_cntr_reg,
  229. AS3722_GPIO_MODE_MASK, AS3722_GPIO_MODE_OUTPUT_VDDH);
  230. if (ret < 0) {
  231. dev_err(as_pci->dev, "GPIO%d_CTRL update failed %d\n",
  232. group, ret);
  233. return ret;
  234. }
  235. as_pci->gpio_control[group].mode_prop =
  236. AS3722_GPIO_MODE_OUTPUT_VDDH;
  237. break;
  238. default:
  239. break;
  240. }
  241. return ret;
  242. }
  243. static int as3722_pinctrl_gpio_get_mode(unsigned gpio_mode_prop, bool input)
  244. {
  245. if (gpio_mode_prop & AS3722_GPIO_MODE_HIGH_IMPED)
  246. return -EINVAL;
  247. if (gpio_mode_prop & AS3722_GPIO_MODE_OPEN_DRAIN) {
  248. if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_UP)
  249. return AS3722_GPIO_MODE_IO_OPEN_DRAIN_PULL_UP;
  250. return AS3722_GPIO_MODE_IO_OPEN_DRAIN;
  251. }
  252. if (input) {
  253. if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_UP)
  254. return AS3722_GPIO_MODE_INPUT_PULL_UP;
  255. else if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_DOWN)
  256. return AS3722_GPIO_MODE_INPUT_PULL_DOWN;
  257. return AS3722_GPIO_MODE_INPUT;
  258. }
  259. if (gpio_mode_prop & AS3722_GPIO_MODE_PULL_DOWN)
  260. return AS3722_GPIO_MODE_OUTPUT_VDDL;
  261. return AS3722_GPIO_MODE_OUTPUT_VDDH;
  262. }
  263. static int as3722_pinctrl_gpio_request_enable(struct pinctrl_dev *pctldev,
  264. struct pinctrl_gpio_range *range, unsigned offset)
  265. {
  266. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  267. if (as_pci->gpio_control[offset].io_function)
  268. return -EBUSY;
  269. return 0;
  270. }
  271. static int as3722_pinctrl_gpio_set_direction(struct pinctrl_dev *pctldev,
  272. struct pinctrl_gpio_range *range, unsigned offset, bool input)
  273. {
  274. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  275. struct as3722 *as3722 = as_pci->as3722;
  276. int mode;
  277. mode = as3722_pinctrl_gpio_get_mode(
  278. as_pci->gpio_control[offset].mode_prop, input);
  279. if (mode < 0) {
  280. dev_err(as_pci->dev, "%s direction for GPIO %d not supported\n",
  281. (input) ? "Input" : "Output", offset);
  282. return mode;
  283. }
  284. if (as_pci->gpio_control[offset].enable_gpio_invert)
  285. mode |= AS3722_GPIO_INV;
  286. return as3722_write(as3722, AS3722_GPIOn_CONTROL_REG(offset), mode);
  287. }
  288. static const struct pinmux_ops as3722_pinmux_ops = {
  289. .get_functions_count = as3722_pinctrl_get_funcs_count,
  290. .get_function_name = as3722_pinctrl_get_func_name,
  291. .get_function_groups = as3722_pinctrl_get_func_groups,
  292. .enable = as3722_pinctrl_enable,
  293. .gpio_request_enable = as3722_pinctrl_gpio_request_enable,
  294. .gpio_set_direction = as3722_pinctrl_gpio_set_direction,
  295. };
  296. static int as3722_pinconf_get(struct pinctrl_dev *pctldev,
  297. unsigned pin, unsigned long *config)
  298. {
  299. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  300. enum pin_config_param param = pinconf_to_config_param(*config);
  301. int arg = 0;
  302. u16 prop;
  303. switch (param) {
  304. case PIN_CONFIG_BIAS_DISABLE:
  305. prop = AS3722_GPIO_MODE_PULL_UP |
  306. AS3722_GPIO_MODE_PULL_DOWN;
  307. if (!(as_pci->gpio_control[pin].mode_prop & prop))
  308. arg = 1;
  309. prop = 0;
  310. break;
  311. case PIN_CONFIG_BIAS_PULL_UP:
  312. prop = AS3722_GPIO_MODE_PULL_UP;
  313. break;
  314. case PIN_CONFIG_BIAS_PULL_DOWN:
  315. prop = AS3722_GPIO_MODE_PULL_DOWN;
  316. break;
  317. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  318. prop = AS3722_GPIO_MODE_OPEN_DRAIN;
  319. break;
  320. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  321. prop = AS3722_GPIO_MODE_HIGH_IMPED;
  322. break;
  323. default:
  324. dev_err(as_pci->dev, "Properties not supported\n");
  325. return -ENOTSUPP;
  326. }
  327. if (as_pci->gpio_control[pin].mode_prop & prop)
  328. arg = 1;
  329. *config = pinconf_to_config_packed(param, (u16)arg);
  330. return 0;
  331. }
  332. static int as3722_pinconf_set(struct pinctrl_dev *pctldev,
  333. unsigned pin, unsigned long *configs,
  334. unsigned num_configs)
  335. {
  336. struct as3722_pctrl_info *as_pci = pinctrl_dev_get_drvdata(pctldev);
  337. enum pin_config_param param;
  338. int mode_prop;
  339. int i;
  340. for (i = 0; i < num_configs; i++) {
  341. param = pinconf_to_config_param(configs[i]);
  342. mode_prop = as_pci->gpio_control[pin].mode_prop;
  343. switch (param) {
  344. case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT:
  345. break;
  346. case PIN_CONFIG_BIAS_DISABLE:
  347. mode_prop &= ~(AS3722_GPIO_MODE_PULL_UP |
  348. AS3722_GPIO_MODE_PULL_DOWN);
  349. break;
  350. case PIN_CONFIG_BIAS_PULL_UP:
  351. mode_prop |= AS3722_GPIO_MODE_PULL_UP;
  352. break;
  353. case PIN_CONFIG_BIAS_PULL_DOWN:
  354. mode_prop |= AS3722_GPIO_MODE_PULL_DOWN;
  355. break;
  356. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  357. mode_prop |= AS3722_GPIO_MODE_HIGH_IMPED;
  358. break;
  359. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  360. mode_prop |= AS3722_GPIO_MODE_OPEN_DRAIN;
  361. break;
  362. default:
  363. dev_err(as_pci->dev, "Properties not supported\n");
  364. return -ENOTSUPP;
  365. }
  366. as_pci->gpio_control[pin].mode_prop = mode_prop;
  367. }
  368. return 0;
  369. }
  370. static const struct pinconf_ops as3722_pinconf_ops = {
  371. .pin_config_get = as3722_pinconf_get,
  372. .pin_config_set = as3722_pinconf_set,
  373. };
  374. static struct pinctrl_desc as3722_pinctrl_desc = {
  375. .pctlops = &as3722_pinctrl_ops,
  376. .pmxops = &as3722_pinmux_ops,
  377. .confops = &as3722_pinconf_ops,
  378. .owner = THIS_MODULE,
  379. };
  380. static inline struct as3722_pctrl_info *to_as_pci(struct gpio_chip *chip)
  381. {
  382. return container_of(chip, struct as3722_pctrl_info, gpio_chip);
  383. }
  384. static int as3722_gpio_get(struct gpio_chip *chip, unsigned offset)
  385. {
  386. struct as3722_pctrl_info *as_pci = to_as_pci(chip);
  387. struct as3722 *as3722 = as_pci->as3722;
  388. int ret;
  389. u32 reg;
  390. u32 control;
  391. u32 val;
  392. int mode;
  393. int invert_enable;
  394. ret = as3722_read(as3722, AS3722_GPIOn_CONTROL_REG(offset), &control);
  395. if (ret < 0) {
  396. dev_err(as_pci->dev,
  397. "GPIO_CONTROL%d_REG read failed: %d\n", offset, ret);
  398. return ret;
  399. }
  400. invert_enable = !!(control & AS3722_GPIO_INV);
  401. mode = control & AS3722_GPIO_MODE_MASK;
  402. switch (mode) {
  403. case AS3722_GPIO_MODE_INPUT:
  404. case AS3722_GPIO_MODE_INPUT_PULL_UP:
  405. case AS3722_GPIO_MODE_INPUT_PULL_DOWN:
  406. case AS3722_GPIO_MODE_IO_OPEN_DRAIN:
  407. case AS3722_GPIO_MODE_IO_OPEN_DRAIN_PULL_UP:
  408. reg = AS3722_GPIO_SIGNAL_IN_REG;
  409. break;
  410. case AS3722_GPIO_MODE_OUTPUT_VDDH:
  411. case AS3722_GPIO_MODE_OUTPUT_VDDL:
  412. reg = AS3722_GPIO_SIGNAL_OUT_REG;
  413. break;
  414. default:
  415. return -EINVAL;
  416. }
  417. ret = as3722_read(as3722, reg, &val);
  418. if (ret < 0) {
  419. dev_err(as_pci->dev,
  420. "GPIO_SIGNAL_IN_REG read failed: %d\n", ret);
  421. return ret;
  422. }
  423. val = !!(val & AS3722_GPIOn_SIGNAL(offset));
  424. return (invert_enable) ? !val : val;
  425. }
  426. static void as3722_gpio_set(struct gpio_chip *chip, unsigned offset,
  427. int value)
  428. {
  429. struct as3722_pctrl_info *as_pci = to_as_pci(chip);
  430. struct as3722 *as3722 = as_pci->as3722;
  431. int en_invert = as_pci->gpio_control[offset].enable_gpio_invert;
  432. u32 val;
  433. int ret;
  434. if (value)
  435. val = (en_invert) ? 0 : AS3722_GPIOn_SIGNAL(offset);
  436. else
  437. val = (en_invert) ? AS3722_GPIOn_SIGNAL(offset) : 0;
  438. ret = as3722_update_bits(as3722, AS3722_GPIO_SIGNAL_OUT_REG,
  439. AS3722_GPIOn_SIGNAL(offset), val);
  440. if (ret < 0)
  441. dev_err(as_pci->dev,
  442. "GPIO_SIGNAL_OUT_REG update failed: %d\n", ret);
  443. }
  444. static int as3722_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  445. {
  446. return pinctrl_gpio_direction_input(chip->base + offset);
  447. }
  448. static int as3722_gpio_direction_output(struct gpio_chip *chip,
  449. unsigned offset, int value)
  450. {
  451. as3722_gpio_set(chip, offset, value);
  452. return pinctrl_gpio_direction_output(chip->base + offset);
  453. }
  454. static int as3722_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  455. {
  456. struct as3722_pctrl_info *as_pci = to_as_pci(chip);
  457. return as3722_irq_get_virq(as_pci->as3722, offset);
  458. }
  459. static int as3722_gpio_request(struct gpio_chip *chip, unsigned offset)
  460. {
  461. return pinctrl_request_gpio(chip->base + offset);
  462. }
  463. static void as3722_gpio_free(struct gpio_chip *chip, unsigned offset)
  464. {
  465. pinctrl_free_gpio(chip->base + offset);
  466. }
  467. static const struct gpio_chip as3722_gpio_chip = {
  468. .label = "as3722-gpio",
  469. .owner = THIS_MODULE,
  470. .request = as3722_gpio_request,
  471. .free = as3722_gpio_free,
  472. .get = as3722_gpio_get,
  473. .set = as3722_gpio_set,
  474. .direction_input = as3722_gpio_direction_input,
  475. .direction_output = as3722_gpio_direction_output,
  476. .to_irq = as3722_gpio_to_irq,
  477. .can_sleep = true,
  478. .ngpio = AS3722_PIN_NUM,
  479. .base = -1,
  480. };
  481. static int as3722_pinctrl_probe(struct platform_device *pdev)
  482. {
  483. struct as3722_pctrl_info *as_pci;
  484. int ret;
  485. int tret;
  486. as_pci = devm_kzalloc(&pdev->dev, sizeof(*as_pci), GFP_KERNEL);
  487. if (!as_pci)
  488. return -ENOMEM;
  489. as_pci->dev = &pdev->dev;
  490. as_pci->dev->of_node = pdev->dev.parent->of_node;
  491. as_pci->as3722 = dev_get_drvdata(pdev->dev.parent);
  492. platform_set_drvdata(pdev, as_pci);
  493. as_pci->pins = as3722_pins_desc;
  494. as_pci->num_pins = ARRAY_SIZE(as3722_pins_desc);
  495. as_pci->functions = as3722_pin_function;
  496. as_pci->num_functions = ARRAY_SIZE(as3722_pin_function);
  497. as_pci->pin_groups = as3722_pingroups;
  498. as_pci->num_pin_groups = ARRAY_SIZE(as3722_pingroups);
  499. as3722_pinctrl_desc.name = dev_name(&pdev->dev);
  500. as3722_pinctrl_desc.pins = as3722_pins_desc;
  501. as3722_pinctrl_desc.npins = ARRAY_SIZE(as3722_pins_desc);
  502. as_pci->pctl = pinctrl_register(&as3722_pinctrl_desc,
  503. &pdev->dev, as_pci);
  504. if (!as_pci->pctl) {
  505. dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
  506. return -EINVAL;
  507. }
  508. as_pci->gpio_chip = as3722_gpio_chip;
  509. as_pci->gpio_chip.dev = &pdev->dev;
  510. as_pci->gpio_chip.of_node = pdev->dev.parent->of_node;
  511. ret = gpiochip_add(&as_pci->gpio_chip);
  512. if (ret < 0) {
  513. dev_err(&pdev->dev, "Couldn't register gpiochip, %d\n", ret);
  514. goto fail_chip_add;
  515. }
  516. ret = gpiochip_add_pin_range(&as_pci->gpio_chip, dev_name(&pdev->dev),
  517. 0, 0, AS3722_PIN_NUM);
  518. if (ret < 0) {
  519. dev_err(&pdev->dev, "Couldn't add pin range, %d\n", ret);
  520. goto fail_range_add;
  521. }
  522. return 0;
  523. fail_range_add:
  524. tret = gpiochip_remove(&as_pci->gpio_chip);
  525. if (tret < 0)
  526. dev_warn(&pdev->dev, "Couldn't remove gpio chip, %d\n", tret);
  527. fail_chip_add:
  528. pinctrl_unregister(as_pci->pctl);
  529. return ret;
  530. }
  531. static int as3722_pinctrl_remove(struct platform_device *pdev)
  532. {
  533. struct as3722_pctrl_info *as_pci = platform_get_drvdata(pdev);
  534. int ret;
  535. ret = gpiochip_remove(&as_pci->gpio_chip);
  536. if (ret < 0)
  537. return ret;
  538. pinctrl_unregister(as_pci->pctl);
  539. return 0;
  540. }
  541. static struct of_device_id as3722_pinctrl_of_match[] = {
  542. { .compatible = "ams,as3722-pinctrl", },
  543. { },
  544. };
  545. MODULE_DEVICE_TABLE(of, as3722_pinctrl_of_match);
  546. static struct platform_driver as3722_pinctrl_driver = {
  547. .driver = {
  548. .name = "as3722-pinctrl",
  549. .owner = THIS_MODULE,
  550. .of_match_table = as3722_pinctrl_of_match,
  551. },
  552. .probe = as3722_pinctrl_probe,
  553. .remove = as3722_pinctrl_remove,
  554. };
  555. module_platform_driver(as3722_pinctrl_driver);
  556. MODULE_ALIAS("platform:as3722-pinctrl");
  557. MODULE_DESCRIPTION("AS3722 pin control and GPIO driver");
  558. MODULE_AUTHOR("Laxman Dewangan<ldewangan@nvidia.com>");
  559. MODULE_LICENSE("GPL v2");