pinctrl-spmi-gpio.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/gpio.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/pinctrl/pinconf-generic.h>
  17. #include <linux/pinctrl/pinconf.h>
  18. #include <linux/pinctrl/pinmux.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <linux/types.h>
  23. #include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
  24. #include "../core.h"
  25. #include "../pinctrl-utils.h"
  26. #define PMIC_GPIO_ADDRESS_RANGE 0x100
  27. /* type and subtype registers base address offsets */
  28. #define PMIC_GPIO_REG_TYPE 0x4
  29. #define PMIC_GPIO_REG_SUBTYPE 0x5
  30. /* GPIO peripheral type and subtype out_values */
  31. #define PMIC_GPIO_TYPE 0x10
  32. #define PMIC_GPIO_SUBTYPE_GPIO_4CH 0x1
  33. #define PMIC_GPIO_SUBTYPE_GPIOC_4CH 0x5
  34. #define PMIC_GPIO_SUBTYPE_GPIO_8CH 0x9
  35. #define PMIC_GPIO_SUBTYPE_GPIOC_8CH 0xd
  36. #define PMIC_MPP_REG_RT_STS 0x10
  37. #define PMIC_MPP_REG_RT_STS_VAL_MASK 0x1
  38. /* control register base address offsets */
  39. #define PMIC_GPIO_REG_MODE_CTL 0x40
  40. #define PMIC_GPIO_REG_DIG_VIN_CTL 0x41
  41. #define PMIC_GPIO_REG_DIG_PULL_CTL 0x42
  42. #define PMIC_GPIO_REG_DIG_OUT_CTL 0x45
  43. #define PMIC_GPIO_REG_EN_CTL 0x46
  44. /* PMIC_GPIO_REG_MODE_CTL */
  45. #define PMIC_GPIO_REG_MODE_VALUE_SHIFT 0x1
  46. #define PMIC_GPIO_REG_MODE_FUNCTION_SHIFT 1
  47. #define PMIC_GPIO_REG_MODE_FUNCTION_MASK 0x7
  48. #define PMIC_GPIO_REG_MODE_DIR_SHIFT 4
  49. #define PMIC_GPIO_REG_MODE_DIR_MASK 0x7
  50. /* PMIC_GPIO_REG_DIG_VIN_CTL */
  51. #define PMIC_GPIO_REG_VIN_SHIFT 0
  52. #define PMIC_GPIO_REG_VIN_MASK 0x7
  53. /* PMIC_GPIO_REG_DIG_PULL_CTL */
  54. #define PMIC_GPIO_REG_PULL_SHIFT 0
  55. #define PMIC_GPIO_REG_PULL_MASK 0x7
  56. #define PMIC_GPIO_PULL_DOWN 4
  57. #define PMIC_GPIO_PULL_DISABLE 5
  58. /* PMIC_GPIO_REG_DIG_OUT_CTL */
  59. #define PMIC_GPIO_REG_OUT_STRENGTH_SHIFT 0
  60. #define PMIC_GPIO_REG_OUT_STRENGTH_MASK 0x3
  61. #define PMIC_GPIO_REG_OUT_TYPE_SHIFT 4
  62. #define PMIC_GPIO_REG_OUT_TYPE_MASK 0x3
  63. /*
  64. * Output type - indicates pin should be configured as push-pull,
  65. * open drain or open source.
  66. */
  67. #define PMIC_GPIO_OUT_BUF_CMOS 0
  68. #define PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS 1
  69. #define PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS 2
  70. /* PMIC_GPIO_REG_EN_CTL */
  71. #define PMIC_GPIO_REG_MASTER_EN_SHIFT 7
  72. #define PMIC_GPIO_PHYSICAL_OFFSET 1
  73. /* Qualcomm specific pin configurations */
  74. #define PMIC_GPIO_CONF_PULL_UP (PIN_CONFIG_END + 1)
  75. #define PMIC_GPIO_CONF_STRENGTH (PIN_CONFIG_END + 2)
  76. /**
  77. * struct pmic_gpio_pad - keep current GPIO settings
  78. * @base: Address base in SPMI device.
  79. * @irq: IRQ number which this GPIO generate.
  80. * @is_enabled: Set to false when GPIO should be put in high Z state.
  81. * @out_value: Cached pin output value
  82. * @have_buffer: Set to true if GPIO output could be configured in push-pull,
  83. * open-drain or open-source mode.
  84. * @output_enabled: Set to true if GPIO output logic is enabled.
  85. * @input_enabled: Set to true if GPIO input buffer logic is enabled.
  86. * @num_sources: Number of power-sources supported by this GPIO.
  87. * @power_source: Current power-source used.
  88. * @buffer_type: Push-pull, open-drain or open-source.
  89. * @pullup: Constant current which flow trough GPIO output buffer.
  90. * @strength: No, Low, Medium, High
  91. * @function: See pmic_gpio_functions[]
  92. */
  93. struct pmic_gpio_pad {
  94. u16 base;
  95. int irq;
  96. bool is_enabled;
  97. bool out_value;
  98. bool have_buffer;
  99. bool output_enabled;
  100. bool input_enabled;
  101. unsigned int num_sources;
  102. unsigned int power_source;
  103. unsigned int buffer_type;
  104. unsigned int pullup;
  105. unsigned int strength;
  106. unsigned int function;
  107. };
  108. struct pmic_gpio_state {
  109. struct device *dev;
  110. struct regmap *map;
  111. struct pinctrl_dev *ctrl;
  112. struct gpio_chip chip;
  113. };
  114. static const struct pinconf_generic_params pmic_gpio_bindings[] = {
  115. {"qcom,pull-up-strength", PMIC_GPIO_CONF_PULL_UP, 0},
  116. {"qcom,drive-strength", PMIC_GPIO_CONF_STRENGTH, 0},
  117. };
  118. #ifdef CONFIG_DEBUG_FS
  119. static const struct pin_config_item pmic_conf_items[ARRAY_SIZE(pmic_gpio_bindings)] = {
  120. PCONFDUMP(PMIC_GPIO_CONF_PULL_UP, "pull up strength", NULL, true),
  121. PCONFDUMP(PMIC_GPIO_CONF_STRENGTH, "drive-strength", NULL, true),
  122. };
  123. #endif
  124. static const char *const pmic_gpio_groups[] = {
  125. "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7", "gpio8",
  126. "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14", "gpio15",
  127. "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21", "gpio22",
  128. "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28", "gpio29",
  129. "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35", "gpio36",
  130. };
  131. static const char *const pmic_gpio_functions[] = {
  132. PMIC_GPIO_FUNC_NORMAL, PMIC_GPIO_FUNC_PAIRED,
  133. PMIC_GPIO_FUNC_FUNC1, PMIC_GPIO_FUNC_FUNC2,
  134. PMIC_GPIO_FUNC_DTEST1, PMIC_GPIO_FUNC_DTEST2,
  135. PMIC_GPIO_FUNC_DTEST3, PMIC_GPIO_FUNC_DTEST4,
  136. };
  137. static inline struct pmic_gpio_state *to_gpio_state(struct gpio_chip *chip)
  138. {
  139. return container_of(chip, struct pmic_gpio_state, chip);
  140. };
  141. static int pmic_gpio_read(struct pmic_gpio_state *state,
  142. struct pmic_gpio_pad *pad, unsigned int addr)
  143. {
  144. unsigned int val;
  145. int ret;
  146. ret = regmap_read(state->map, pad->base + addr, &val);
  147. if (ret < 0)
  148. dev_err(state->dev, "read 0x%x failed\n", addr);
  149. else
  150. ret = val;
  151. return ret;
  152. }
  153. static int pmic_gpio_write(struct pmic_gpio_state *state,
  154. struct pmic_gpio_pad *pad, unsigned int addr,
  155. unsigned int val)
  156. {
  157. int ret;
  158. ret = regmap_write(state->map, pad->base + addr, val);
  159. if (ret < 0)
  160. dev_err(state->dev, "write 0x%x failed\n", addr);
  161. return ret;
  162. }
  163. static int pmic_gpio_get_groups_count(struct pinctrl_dev *pctldev)
  164. {
  165. /* Every PIN is a group */
  166. return pctldev->desc->npins;
  167. }
  168. static const char *pmic_gpio_get_group_name(struct pinctrl_dev *pctldev,
  169. unsigned pin)
  170. {
  171. return pctldev->desc->pins[pin].name;
  172. }
  173. static int pmic_gpio_get_group_pins(struct pinctrl_dev *pctldev, unsigned pin,
  174. const unsigned **pins, unsigned *num_pins)
  175. {
  176. *pins = &pctldev->desc->pins[pin].number;
  177. *num_pins = 1;
  178. return 0;
  179. }
  180. static const struct pinctrl_ops pmic_gpio_pinctrl_ops = {
  181. .get_groups_count = pmic_gpio_get_groups_count,
  182. .get_group_name = pmic_gpio_get_group_name,
  183. .get_group_pins = pmic_gpio_get_group_pins,
  184. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  185. .dt_free_map = pinctrl_utils_dt_free_map,
  186. };
  187. static int pmic_gpio_get_functions_count(struct pinctrl_dev *pctldev)
  188. {
  189. return ARRAY_SIZE(pmic_gpio_functions);
  190. }
  191. static const char *pmic_gpio_get_function_name(struct pinctrl_dev *pctldev,
  192. unsigned function)
  193. {
  194. return pmic_gpio_functions[function];
  195. }
  196. static int pmic_gpio_get_function_groups(struct pinctrl_dev *pctldev,
  197. unsigned function,
  198. const char *const **groups,
  199. unsigned *const num_qgroups)
  200. {
  201. *groups = pmic_gpio_groups;
  202. *num_qgroups = pctldev->desc->npins;
  203. return 0;
  204. }
  205. static int pmic_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned function,
  206. unsigned pin)
  207. {
  208. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  209. struct pmic_gpio_pad *pad;
  210. unsigned int val;
  211. int ret;
  212. pad = pctldev->desc->pins[pin].drv_data;
  213. pad->function = function;
  214. val = 0;
  215. if (pad->output_enabled) {
  216. if (pad->input_enabled)
  217. val = 2;
  218. else
  219. val = 1;
  220. }
  221. val = val << PMIC_GPIO_REG_MODE_DIR_SHIFT;
  222. val |= pad->function << PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  223. val |= pad->out_value & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  224. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
  225. if (ret < 0)
  226. return ret;
  227. val = pad->is_enabled << PMIC_GPIO_REG_MASTER_EN_SHIFT;
  228. return pmic_gpio_write(state, pad, PMIC_GPIO_REG_EN_CTL, val);
  229. }
  230. static const struct pinmux_ops pmic_gpio_pinmux_ops = {
  231. .get_functions_count = pmic_gpio_get_functions_count,
  232. .get_function_name = pmic_gpio_get_function_name,
  233. .get_function_groups = pmic_gpio_get_function_groups,
  234. .set_mux = pmic_gpio_set_mux,
  235. };
  236. static int pmic_gpio_config_get(struct pinctrl_dev *pctldev,
  237. unsigned int pin, unsigned long *config)
  238. {
  239. unsigned param = pinconf_to_config_param(*config);
  240. struct pmic_gpio_pad *pad;
  241. unsigned arg;
  242. pad = pctldev->desc->pins[pin].drv_data;
  243. switch (param) {
  244. case PIN_CONFIG_DRIVE_PUSH_PULL:
  245. arg = pad->buffer_type == PMIC_GPIO_OUT_BUF_CMOS;
  246. break;
  247. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  248. arg = pad->buffer_type == PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS;
  249. break;
  250. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  251. arg = pad->buffer_type == PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS;
  252. break;
  253. case PIN_CONFIG_BIAS_PULL_DOWN:
  254. arg = pad->pullup == PMIC_GPIO_PULL_DOWN;
  255. break;
  256. case PIN_CONFIG_BIAS_DISABLE:
  257. arg = pad->pullup = PMIC_GPIO_PULL_DISABLE;
  258. break;
  259. case PIN_CONFIG_BIAS_PULL_UP:
  260. arg = pad->pullup == PMIC_GPIO_PULL_UP_30;
  261. break;
  262. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  263. arg = !pad->is_enabled;
  264. break;
  265. case PIN_CONFIG_POWER_SOURCE:
  266. arg = pad->power_source;
  267. break;
  268. case PIN_CONFIG_INPUT_ENABLE:
  269. arg = pad->input_enabled;
  270. break;
  271. case PIN_CONFIG_OUTPUT:
  272. arg = pad->out_value;
  273. break;
  274. case PMIC_GPIO_CONF_PULL_UP:
  275. arg = pad->pullup;
  276. break;
  277. case PMIC_GPIO_CONF_STRENGTH:
  278. arg = pad->strength;
  279. break;
  280. default:
  281. return -EINVAL;
  282. }
  283. *config = pinconf_to_config_packed(param, arg);
  284. return 0;
  285. }
  286. static int pmic_gpio_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  287. unsigned long *configs, unsigned nconfs)
  288. {
  289. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  290. struct pmic_gpio_pad *pad;
  291. unsigned param, arg;
  292. unsigned int val;
  293. int i, ret;
  294. pad = pctldev->desc->pins[pin].drv_data;
  295. for (i = 0; i < nconfs; i++) {
  296. param = pinconf_to_config_param(configs[i]);
  297. arg = pinconf_to_config_argument(configs[i]);
  298. switch (param) {
  299. case PIN_CONFIG_DRIVE_PUSH_PULL:
  300. pad->buffer_type = PMIC_GPIO_OUT_BUF_CMOS;
  301. break;
  302. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  303. if (!pad->have_buffer)
  304. return -EINVAL;
  305. pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS;
  306. break;
  307. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  308. if (!pad->have_buffer)
  309. return -EINVAL;
  310. pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS;
  311. break;
  312. case PIN_CONFIG_BIAS_DISABLE:
  313. pad->pullup = PMIC_GPIO_PULL_DISABLE;
  314. break;
  315. case PIN_CONFIG_BIAS_PULL_UP:
  316. pad->pullup = PMIC_GPIO_PULL_UP_30;
  317. break;
  318. case PIN_CONFIG_BIAS_PULL_DOWN:
  319. if (arg)
  320. pad->pullup = PMIC_GPIO_PULL_DOWN;
  321. else
  322. pad->pullup = PMIC_GPIO_PULL_DISABLE;
  323. break;
  324. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  325. pad->is_enabled = false;
  326. break;
  327. case PIN_CONFIG_POWER_SOURCE:
  328. if (arg > pad->num_sources)
  329. return -EINVAL;
  330. pad->power_source = arg;
  331. break;
  332. case PIN_CONFIG_INPUT_ENABLE:
  333. pad->input_enabled = arg ? true : false;
  334. break;
  335. case PIN_CONFIG_OUTPUT:
  336. pad->output_enabled = true;
  337. pad->out_value = arg;
  338. break;
  339. case PMIC_GPIO_CONF_PULL_UP:
  340. if (arg > PMIC_GPIO_PULL_UP_1P5_30)
  341. return -EINVAL;
  342. pad->pullup = arg;
  343. break;
  344. case PMIC_GPIO_CONF_STRENGTH:
  345. if (arg > PMIC_GPIO_STRENGTH_LOW)
  346. return -EINVAL;
  347. pad->strength = arg;
  348. break;
  349. default:
  350. return -EINVAL;
  351. }
  352. }
  353. val = pad->power_source << PMIC_GPIO_REG_VIN_SHIFT;
  354. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL, val);
  355. if (ret < 0)
  356. return ret;
  357. val = pad->pullup << PMIC_GPIO_REG_PULL_SHIFT;
  358. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL, val);
  359. if (ret < 0)
  360. return ret;
  361. val = pad->buffer_type << PMIC_GPIO_REG_OUT_TYPE_SHIFT;
  362. val |= pad->strength << PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
  363. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL, val);
  364. if (ret < 0)
  365. return ret;
  366. val = 0;
  367. if (pad->output_enabled) {
  368. if (pad->input_enabled)
  369. val = 2;
  370. else
  371. val = 1;
  372. }
  373. val = val << PMIC_GPIO_REG_MODE_DIR_SHIFT;
  374. val |= pad->function << PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  375. val |= pad->out_value & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  376. return pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
  377. }
  378. static void pmic_gpio_config_dbg_show(struct pinctrl_dev *pctldev,
  379. struct seq_file *s, unsigned pin)
  380. {
  381. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  382. struct pmic_gpio_pad *pad;
  383. int ret, val;
  384. static const char *const biases[] = {
  385. "pull-up 30uA", "pull-up 1.5uA", "pull-up 31.5uA",
  386. "pull-up 1.5uA + 30uA boost", "pull-down 10uA", "no pull"
  387. };
  388. static const char *const buffer_types[] = {
  389. "push-pull", "open-drain", "open-source"
  390. };
  391. static const char *const strengths[] = {
  392. "no", "high", "medium", "low"
  393. };
  394. pad = pctldev->desc->pins[pin].drv_data;
  395. seq_printf(s, " gpio%-2d:", pin + PMIC_GPIO_PHYSICAL_OFFSET);
  396. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_EN_CTL);
  397. if (val < 0 || !(val >> PMIC_GPIO_REG_MASTER_EN_SHIFT)) {
  398. seq_puts(s, " ---");
  399. } else {
  400. if (pad->input_enabled) {
  401. ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
  402. if (ret < 0)
  403. return;
  404. ret &= PMIC_MPP_REG_RT_STS_VAL_MASK;
  405. pad->out_value = ret;
  406. }
  407. seq_printf(s, " %-4s", pad->output_enabled ? "out" : "in");
  408. seq_printf(s, " %-7s", pmic_gpio_functions[pad->function]);
  409. seq_printf(s, " vin-%d", pad->power_source);
  410. seq_printf(s, " %-27s", biases[pad->pullup]);
  411. seq_printf(s, " %-10s", buffer_types[pad->buffer_type]);
  412. seq_printf(s, " %-4s", pad->out_value ? "high" : "low");
  413. seq_printf(s, " %-7s", strengths[pad->strength]);
  414. }
  415. }
  416. static const struct pinconf_ops pmic_gpio_pinconf_ops = {
  417. .is_generic = true,
  418. .pin_config_group_get = pmic_gpio_config_get,
  419. .pin_config_group_set = pmic_gpio_config_set,
  420. .pin_config_group_dbg_show = pmic_gpio_config_dbg_show,
  421. };
  422. static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  423. {
  424. struct pmic_gpio_state *state = to_gpio_state(chip);
  425. unsigned long config;
  426. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
  427. return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  428. }
  429. static int pmic_gpio_direction_output(struct gpio_chip *chip,
  430. unsigned pin, int val)
  431. {
  432. struct pmic_gpio_state *state = to_gpio_state(chip);
  433. unsigned long config;
  434. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
  435. return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  436. }
  437. static int pmic_gpio_get(struct gpio_chip *chip, unsigned pin)
  438. {
  439. struct pmic_gpio_state *state = to_gpio_state(chip);
  440. struct pmic_gpio_pad *pad;
  441. int ret;
  442. pad = state->ctrl->desc->pins[pin].drv_data;
  443. if (!pad->is_enabled)
  444. return -EINVAL;
  445. if (pad->input_enabled) {
  446. ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
  447. if (ret < 0)
  448. return ret;
  449. pad->out_value = ret & PMIC_MPP_REG_RT_STS_VAL_MASK;
  450. }
  451. return pad->out_value;
  452. }
  453. static void pmic_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  454. {
  455. struct pmic_gpio_state *state = to_gpio_state(chip);
  456. unsigned long config;
  457. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
  458. pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  459. }
  460. static int pmic_gpio_request(struct gpio_chip *chip, unsigned base)
  461. {
  462. return pinctrl_request_gpio(chip->base + base);
  463. }
  464. static void pmic_gpio_free(struct gpio_chip *chip, unsigned base)
  465. {
  466. pinctrl_free_gpio(chip->base + base);
  467. }
  468. static int pmic_gpio_of_xlate(struct gpio_chip *chip,
  469. const struct of_phandle_args *gpio_desc,
  470. u32 *flags)
  471. {
  472. if (chip->of_gpio_n_cells < 2)
  473. return -EINVAL;
  474. if (flags)
  475. *flags = gpio_desc->args[1];
  476. return gpio_desc->args[0] - PMIC_GPIO_PHYSICAL_OFFSET;
  477. }
  478. static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  479. {
  480. struct pmic_gpio_state *state = to_gpio_state(chip);
  481. struct pmic_gpio_pad *pad;
  482. pad = state->ctrl->desc->pins[pin].drv_data;
  483. return pad->irq;
  484. }
  485. static void pmic_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  486. {
  487. struct pmic_gpio_state *state = to_gpio_state(chip);
  488. unsigned i;
  489. for (i = 0; i < chip->ngpio; i++) {
  490. pmic_gpio_config_dbg_show(state->ctrl, s, i);
  491. seq_puts(s, "\n");
  492. }
  493. }
  494. static const struct gpio_chip pmic_gpio_gpio_template = {
  495. .direction_input = pmic_gpio_direction_input,
  496. .direction_output = pmic_gpio_direction_output,
  497. .get = pmic_gpio_get,
  498. .set = pmic_gpio_set,
  499. .request = pmic_gpio_request,
  500. .free = pmic_gpio_free,
  501. .of_xlate = pmic_gpio_of_xlate,
  502. .to_irq = pmic_gpio_to_irq,
  503. .dbg_show = pmic_gpio_dbg_show,
  504. };
  505. static int pmic_gpio_populate(struct pmic_gpio_state *state,
  506. struct pmic_gpio_pad *pad)
  507. {
  508. int type, subtype, val, dir;
  509. type = pmic_gpio_read(state, pad, PMIC_GPIO_REG_TYPE);
  510. if (type < 0)
  511. return type;
  512. if (type != PMIC_GPIO_TYPE) {
  513. dev_err(state->dev, "incorrect block type 0x%x at 0x%x\n",
  514. type, pad->base);
  515. return -ENODEV;
  516. }
  517. subtype = pmic_gpio_read(state, pad, PMIC_GPIO_REG_SUBTYPE);
  518. if (subtype < 0)
  519. return subtype;
  520. switch (subtype) {
  521. case PMIC_GPIO_SUBTYPE_GPIO_4CH:
  522. pad->have_buffer = true;
  523. case PMIC_GPIO_SUBTYPE_GPIOC_4CH:
  524. pad->num_sources = 4;
  525. break;
  526. case PMIC_GPIO_SUBTYPE_GPIO_8CH:
  527. pad->have_buffer = true;
  528. case PMIC_GPIO_SUBTYPE_GPIOC_8CH:
  529. pad->num_sources = 8;
  530. break;
  531. default:
  532. dev_err(state->dev, "unknown GPIO type 0x%x\n", subtype);
  533. return -ENODEV;
  534. }
  535. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_MODE_CTL);
  536. if (val < 0)
  537. return val;
  538. pad->out_value = val & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  539. dir = val >> PMIC_GPIO_REG_MODE_DIR_SHIFT;
  540. dir &= PMIC_GPIO_REG_MODE_DIR_MASK;
  541. switch (dir) {
  542. case 0:
  543. pad->input_enabled = true;
  544. pad->output_enabled = false;
  545. break;
  546. case 1:
  547. pad->input_enabled = false;
  548. pad->output_enabled = true;
  549. break;
  550. case 2:
  551. pad->input_enabled = true;
  552. pad->output_enabled = true;
  553. break;
  554. default:
  555. dev_err(state->dev, "unknown GPIO direction\n");
  556. return -ENODEV;
  557. }
  558. pad->function = val >> PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  559. pad->function &= PMIC_GPIO_REG_MODE_FUNCTION_MASK;
  560. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL);
  561. if (val < 0)
  562. return val;
  563. pad->power_source = val >> PMIC_GPIO_REG_VIN_SHIFT;
  564. pad->power_source &= PMIC_GPIO_REG_VIN_MASK;
  565. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL);
  566. if (val < 0)
  567. return val;
  568. pad->pullup = val >> PMIC_GPIO_REG_PULL_SHIFT;
  569. pad->pullup &= PMIC_GPIO_REG_PULL_MASK;
  570. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL);
  571. if (val < 0)
  572. return val;
  573. pad->strength = val >> PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
  574. pad->strength &= PMIC_GPIO_REG_OUT_STRENGTH_MASK;
  575. pad->buffer_type = val >> PMIC_GPIO_REG_OUT_TYPE_SHIFT;
  576. pad->buffer_type &= PMIC_GPIO_REG_OUT_TYPE_MASK;
  577. /* Pin could be disabled with PIN_CONFIG_BIAS_HIGH_IMPEDANCE */
  578. pad->is_enabled = true;
  579. return 0;
  580. }
  581. static int pmic_gpio_probe(struct platform_device *pdev)
  582. {
  583. struct device *dev = &pdev->dev;
  584. struct pinctrl_pin_desc *pindesc;
  585. struct pinctrl_desc *pctrldesc;
  586. struct pmic_gpio_pad *pad, *pads;
  587. struct pmic_gpio_state *state;
  588. int ret, npins, i;
  589. u32 res[2];
  590. ret = of_property_read_u32_array(dev->of_node, "reg", res, 2);
  591. if (ret < 0) {
  592. dev_err(dev, "missing base address and/or range");
  593. return ret;
  594. }
  595. npins = res[1] / PMIC_GPIO_ADDRESS_RANGE;
  596. if (!npins)
  597. return -EINVAL;
  598. BUG_ON(npins > ARRAY_SIZE(pmic_gpio_groups));
  599. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  600. if (!state)
  601. return -ENOMEM;
  602. platform_set_drvdata(pdev, state);
  603. state->dev = &pdev->dev;
  604. state->map = dev_get_regmap(dev->parent, NULL);
  605. pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
  606. if (!pindesc)
  607. return -ENOMEM;
  608. pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
  609. if (!pads)
  610. return -ENOMEM;
  611. pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
  612. if (!pctrldesc)
  613. return -ENOMEM;
  614. pctrldesc->pctlops = &pmic_gpio_pinctrl_ops;
  615. pctrldesc->pmxops = &pmic_gpio_pinmux_ops;
  616. pctrldesc->confops = &pmic_gpio_pinconf_ops;
  617. pctrldesc->owner = THIS_MODULE;
  618. pctrldesc->name = dev_name(dev);
  619. pctrldesc->pins = pindesc;
  620. pctrldesc->npins = npins;
  621. pctrldesc->num_custom_params = ARRAY_SIZE(pmic_gpio_bindings);
  622. pctrldesc->custom_params = pmic_gpio_bindings;
  623. #ifdef CONFIG_DEBUG_FS
  624. pctrldesc->custom_conf_items = pmic_conf_items;
  625. #endif
  626. for (i = 0; i < npins; i++, pindesc++) {
  627. pad = &pads[i];
  628. pindesc->drv_data = pad;
  629. pindesc->number = i;
  630. pindesc->name = pmic_gpio_groups[i];
  631. pad->irq = platform_get_irq(pdev, i);
  632. if (pad->irq < 0)
  633. return pad->irq;
  634. pad->base = res[0] + i * PMIC_GPIO_ADDRESS_RANGE;
  635. ret = pmic_gpio_populate(state, pad);
  636. if (ret < 0)
  637. return ret;
  638. }
  639. state->chip = pmic_gpio_gpio_template;
  640. state->chip.dev = dev;
  641. state->chip.base = -1;
  642. state->chip.ngpio = npins;
  643. state->chip.label = dev_name(dev);
  644. state->chip.of_gpio_n_cells = 2;
  645. state->chip.can_sleep = false;
  646. state->ctrl = pinctrl_register(pctrldesc, dev, state);
  647. if (!state->ctrl)
  648. return -ENODEV;
  649. ret = gpiochip_add(&state->chip);
  650. if (ret) {
  651. dev_err(state->dev, "can't add gpio chip\n");
  652. goto err_chip;
  653. }
  654. ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0, npins);
  655. if (ret) {
  656. dev_err(dev, "failed to add pin range\n");
  657. goto err_range;
  658. }
  659. return 0;
  660. err_range:
  661. gpiochip_remove(&state->chip);
  662. err_chip:
  663. pinctrl_unregister(state->ctrl);
  664. return ret;
  665. }
  666. static int pmic_gpio_remove(struct platform_device *pdev)
  667. {
  668. struct pmic_gpio_state *state = platform_get_drvdata(pdev);
  669. gpiochip_remove(&state->chip);
  670. pinctrl_unregister(state->ctrl);
  671. return 0;
  672. }
  673. static const struct of_device_id pmic_gpio_of_match[] = {
  674. { .compatible = "qcom,pm8916-gpio" }, /* 4 GPIO's */
  675. { .compatible = "qcom,pm8941-gpio" }, /* 36 GPIO's */
  676. { .compatible = "qcom,pma8084-gpio" }, /* 22 GPIO's */
  677. { },
  678. };
  679. MODULE_DEVICE_TABLE(of, pmic_gpio_of_match);
  680. static struct platform_driver pmic_gpio_driver = {
  681. .driver = {
  682. .name = "qcom-spmi-gpio",
  683. .of_match_table = pmic_gpio_of_match,
  684. },
  685. .probe = pmic_gpio_probe,
  686. .remove = pmic_gpio_remove,
  687. };
  688. module_platform_driver(pmic_gpio_driver);
  689. MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
  690. MODULE_DESCRIPTION("Qualcomm SPMI PMIC GPIO pin control driver");
  691. MODULE_ALIAS("platform:qcom-spmi-gpio");
  692. MODULE_LICENSE("GPL v2");