pinctrl-spmi-gpio.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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 |= pad->function << PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  222. val |= pad->out_value & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  223. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
  224. if (ret < 0)
  225. return ret;
  226. val = pad->is_enabled << PMIC_GPIO_REG_MASTER_EN_SHIFT;
  227. return pmic_gpio_write(state, pad, PMIC_GPIO_REG_EN_CTL, val);
  228. }
  229. static const struct pinmux_ops pmic_gpio_pinmux_ops = {
  230. .get_functions_count = pmic_gpio_get_functions_count,
  231. .get_function_name = pmic_gpio_get_function_name,
  232. .get_function_groups = pmic_gpio_get_function_groups,
  233. .set_mux = pmic_gpio_set_mux,
  234. };
  235. static int pmic_gpio_config_get(struct pinctrl_dev *pctldev,
  236. unsigned int pin, unsigned long *config)
  237. {
  238. unsigned param = pinconf_to_config_param(*config);
  239. struct pmic_gpio_pad *pad;
  240. unsigned arg;
  241. pad = pctldev->desc->pins[pin].drv_data;
  242. switch (param) {
  243. case PIN_CONFIG_DRIVE_PUSH_PULL:
  244. arg = pad->buffer_type == PMIC_GPIO_OUT_BUF_CMOS;
  245. break;
  246. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  247. arg = pad->buffer_type == PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS;
  248. break;
  249. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  250. arg = pad->buffer_type == PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS;
  251. break;
  252. case PIN_CONFIG_BIAS_PULL_DOWN:
  253. arg = pad->pullup == PMIC_GPIO_PULL_DOWN;
  254. break;
  255. case PIN_CONFIG_BIAS_DISABLE:
  256. arg = pad->pullup = PMIC_GPIO_PULL_DISABLE;
  257. break;
  258. case PIN_CONFIG_BIAS_PULL_UP:
  259. arg = pad->pullup == PMIC_GPIO_PULL_UP_30;
  260. break;
  261. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  262. arg = !pad->is_enabled;
  263. break;
  264. case PIN_CONFIG_POWER_SOURCE:
  265. arg = pad->power_source;
  266. break;
  267. case PIN_CONFIG_INPUT_ENABLE:
  268. arg = pad->input_enabled;
  269. break;
  270. case PIN_CONFIG_OUTPUT:
  271. arg = pad->out_value;
  272. break;
  273. case PMIC_GPIO_CONF_PULL_UP:
  274. arg = pad->pullup;
  275. break;
  276. case PMIC_GPIO_CONF_STRENGTH:
  277. arg = pad->strength;
  278. break;
  279. default:
  280. return -EINVAL;
  281. }
  282. *config = pinconf_to_config_packed(param, arg);
  283. return 0;
  284. }
  285. static int pmic_gpio_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  286. unsigned long *configs, unsigned nconfs)
  287. {
  288. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  289. struct pmic_gpio_pad *pad;
  290. unsigned param, arg;
  291. unsigned int val;
  292. int i, ret;
  293. pad = pctldev->desc->pins[pin].drv_data;
  294. for (i = 0; i < nconfs; i++) {
  295. param = pinconf_to_config_param(configs[i]);
  296. arg = pinconf_to_config_argument(configs[i]);
  297. switch (param) {
  298. case PIN_CONFIG_DRIVE_PUSH_PULL:
  299. pad->buffer_type = PMIC_GPIO_OUT_BUF_CMOS;
  300. break;
  301. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  302. if (!pad->have_buffer)
  303. return -EINVAL;
  304. pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS;
  305. break;
  306. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  307. if (!pad->have_buffer)
  308. return -EINVAL;
  309. pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS;
  310. break;
  311. case PIN_CONFIG_BIAS_DISABLE:
  312. pad->pullup = PMIC_GPIO_PULL_DISABLE;
  313. break;
  314. case PIN_CONFIG_BIAS_PULL_UP:
  315. pad->pullup = PMIC_GPIO_PULL_UP_30;
  316. break;
  317. case PIN_CONFIG_BIAS_PULL_DOWN:
  318. if (arg)
  319. pad->pullup = PMIC_GPIO_PULL_DOWN;
  320. else
  321. pad->pullup = PMIC_GPIO_PULL_DISABLE;
  322. break;
  323. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  324. pad->is_enabled = false;
  325. break;
  326. case PIN_CONFIG_POWER_SOURCE:
  327. if (arg > pad->num_sources)
  328. return -EINVAL;
  329. pad->power_source = arg;
  330. break;
  331. case PIN_CONFIG_INPUT_ENABLE:
  332. pad->input_enabled = arg ? true : false;
  333. break;
  334. case PIN_CONFIG_OUTPUT:
  335. pad->output_enabled = true;
  336. pad->out_value = arg;
  337. break;
  338. case PMIC_GPIO_CONF_PULL_UP:
  339. if (arg > PMIC_GPIO_PULL_UP_1P5_30)
  340. return -EINVAL;
  341. pad->pullup = arg;
  342. break;
  343. case PMIC_GPIO_CONF_STRENGTH:
  344. if (arg > PMIC_GPIO_STRENGTH_LOW)
  345. return -EINVAL;
  346. pad->strength = arg;
  347. break;
  348. default:
  349. return -EINVAL;
  350. }
  351. }
  352. val = pad->power_source << PMIC_GPIO_REG_VIN_SHIFT;
  353. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL, val);
  354. if (ret < 0)
  355. return ret;
  356. val = pad->pullup << PMIC_GPIO_REG_PULL_SHIFT;
  357. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL, val);
  358. if (ret < 0)
  359. return ret;
  360. val = pad->buffer_type << PMIC_GPIO_REG_OUT_TYPE_SHIFT;
  361. val = pad->strength << PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
  362. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL, val);
  363. if (ret < 0)
  364. return ret;
  365. val = 0;
  366. if (pad->output_enabled) {
  367. if (pad->input_enabled)
  368. val = 2;
  369. else
  370. val = 1;
  371. }
  372. val = val << PMIC_GPIO_REG_MODE_DIR_SHIFT;
  373. val |= pad->function << PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  374. val |= pad->out_value & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  375. return pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
  376. }
  377. static void pmic_gpio_config_dbg_show(struct pinctrl_dev *pctldev,
  378. struct seq_file *s, unsigned pin)
  379. {
  380. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  381. struct pmic_gpio_pad *pad;
  382. int ret, val;
  383. static const char *const biases[] = {
  384. "pull-up 30uA", "pull-up 1.5uA", "pull-up 31.5uA",
  385. "pull-up 1.5uA + 30uA boost", "pull-down 10uA", "no pull"
  386. };
  387. static const char *const buffer_types[] = {
  388. "push-pull", "open-drain", "open-source"
  389. };
  390. static const char *const strengths[] = {
  391. "no", "high", "medium", "low"
  392. };
  393. pad = pctldev->desc->pins[pin].drv_data;
  394. seq_printf(s, " gpio%-2d:", pin + PMIC_GPIO_PHYSICAL_OFFSET);
  395. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_EN_CTL);
  396. if (val < 0 || !(val >> PMIC_GPIO_REG_MASTER_EN_SHIFT)) {
  397. seq_puts(s, " ---");
  398. } else {
  399. if (!pad->input_enabled) {
  400. ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
  401. if (!ret) {
  402. ret &= PMIC_MPP_REG_RT_STS_VAL_MASK;
  403. pad->out_value = ret;
  404. }
  405. }
  406. seq_printf(s, " %-4s", pad->output_enabled ? "out" : "in");
  407. seq_printf(s, " %-7s", pmic_gpio_functions[pad->function]);
  408. seq_printf(s, " vin-%d", pad->power_source);
  409. seq_printf(s, " %-27s", biases[pad->pullup]);
  410. seq_printf(s, " %-10s", buffer_types[pad->buffer_type]);
  411. seq_printf(s, " %-4s", pad->out_value ? "high" : "low");
  412. seq_printf(s, " %-7s", strengths[pad->strength]);
  413. }
  414. }
  415. static const struct pinconf_ops pmic_gpio_pinconf_ops = {
  416. .is_generic = true,
  417. .pin_config_group_get = pmic_gpio_config_get,
  418. .pin_config_group_set = pmic_gpio_config_set,
  419. .pin_config_group_dbg_show = pmic_gpio_config_dbg_show,
  420. };
  421. static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  422. {
  423. struct pmic_gpio_state *state = to_gpio_state(chip);
  424. unsigned long config;
  425. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
  426. return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  427. }
  428. static int pmic_gpio_direction_output(struct gpio_chip *chip,
  429. unsigned pin, int val)
  430. {
  431. struct pmic_gpio_state *state = to_gpio_state(chip);
  432. unsigned long config;
  433. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
  434. return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  435. }
  436. static int pmic_gpio_get(struct gpio_chip *chip, unsigned pin)
  437. {
  438. struct pmic_gpio_state *state = to_gpio_state(chip);
  439. struct pmic_gpio_pad *pad;
  440. int ret;
  441. pad = state->ctrl->desc->pins[pin].drv_data;
  442. if (!pad->is_enabled)
  443. return -EINVAL;
  444. if (pad->input_enabled) {
  445. ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
  446. if (ret < 0)
  447. return ret;
  448. pad->out_value = ret & PMIC_MPP_REG_RT_STS_VAL_MASK;
  449. }
  450. return pad->out_value;
  451. }
  452. static void pmic_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  453. {
  454. struct pmic_gpio_state *state = to_gpio_state(chip);
  455. unsigned long config;
  456. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
  457. pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  458. }
  459. static int pmic_gpio_request(struct gpio_chip *chip, unsigned base)
  460. {
  461. return pinctrl_request_gpio(chip->base + base);
  462. }
  463. static void pmic_gpio_free(struct gpio_chip *chip, unsigned base)
  464. {
  465. pinctrl_free_gpio(chip->base + base);
  466. }
  467. static int pmic_gpio_of_xlate(struct gpio_chip *chip,
  468. const struct of_phandle_args *gpio_desc,
  469. u32 *flags)
  470. {
  471. if (chip->of_gpio_n_cells < 2)
  472. return -EINVAL;
  473. if (flags)
  474. *flags = gpio_desc->args[1];
  475. return gpio_desc->args[0] - PMIC_GPIO_PHYSICAL_OFFSET;
  476. }
  477. static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  478. {
  479. struct pmic_gpio_state *state = to_gpio_state(chip);
  480. struct pmic_gpio_pad *pad;
  481. pad = state->ctrl->desc->pins[pin].drv_data;
  482. return pad->irq;
  483. }
  484. static void pmic_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  485. {
  486. struct pmic_gpio_state *state = to_gpio_state(chip);
  487. unsigned i;
  488. for (i = 0; i < chip->ngpio; i++) {
  489. pmic_gpio_config_dbg_show(state->ctrl, s, i);
  490. seq_puts(s, "\n");
  491. }
  492. }
  493. static const struct gpio_chip pmic_gpio_gpio_template = {
  494. .direction_input = pmic_gpio_direction_input,
  495. .direction_output = pmic_gpio_direction_output,
  496. .get = pmic_gpio_get,
  497. .set = pmic_gpio_set,
  498. .request = pmic_gpio_request,
  499. .free = pmic_gpio_free,
  500. .of_xlate = pmic_gpio_of_xlate,
  501. .to_irq = pmic_gpio_to_irq,
  502. .dbg_show = pmic_gpio_dbg_show,
  503. };
  504. static int pmic_gpio_populate(struct pmic_gpio_state *state,
  505. struct pmic_gpio_pad *pad)
  506. {
  507. int type, subtype, val, dir;
  508. type = pmic_gpio_read(state, pad, PMIC_GPIO_REG_TYPE);
  509. if (type < 0)
  510. return type;
  511. if (type != PMIC_GPIO_TYPE) {
  512. dev_err(state->dev, "incorrect block type 0x%x at 0x%x\n",
  513. type, pad->base);
  514. return -ENODEV;
  515. }
  516. subtype = pmic_gpio_read(state, pad, PMIC_GPIO_REG_SUBTYPE);
  517. if (subtype < 0)
  518. return subtype;
  519. switch (subtype) {
  520. case PMIC_GPIO_SUBTYPE_GPIO_4CH:
  521. pad->have_buffer = true;
  522. case PMIC_GPIO_SUBTYPE_GPIOC_4CH:
  523. pad->num_sources = 4;
  524. break;
  525. case PMIC_GPIO_SUBTYPE_GPIO_8CH:
  526. pad->have_buffer = true;
  527. case PMIC_GPIO_SUBTYPE_GPIOC_8CH:
  528. pad->num_sources = 8;
  529. break;
  530. default:
  531. dev_err(state->dev, "unknown GPIO type 0x%x\n", subtype);
  532. return -ENODEV;
  533. }
  534. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_MODE_CTL);
  535. if (val < 0)
  536. return val;
  537. pad->out_value = val & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  538. dir = val >> PMIC_GPIO_REG_MODE_DIR_SHIFT;
  539. dir &= PMIC_GPIO_REG_MODE_DIR_MASK;
  540. switch (dir) {
  541. case 0:
  542. pad->input_enabled = true;
  543. pad->output_enabled = false;
  544. break;
  545. case 1:
  546. pad->input_enabled = false;
  547. pad->output_enabled = true;
  548. break;
  549. case 2:
  550. pad->input_enabled = true;
  551. pad->output_enabled = true;
  552. break;
  553. default:
  554. dev_err(state->dev, "unknown GPIO direction\n");
  555. return -ENODEV;
  556. }
  557. pad->function = val >> PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  558. pad->function &= PMIC_GPIO_REG_MODE_FUNCTION_MASK;
  559. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL);
  560. if (val < 0)
  561. return val;
  562. pad->power_source = val >> PMIC_GPIO_REG_VIN_SHIFT;
  563. pad->power_source &= PMIC_GPIO_REG_VIN_MASK;
  564. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL);
  565. if (val < 0)
  566. return val;
  567. pad->pullup = val >> PMIC_GPIO_REG_PULL_SHIFT;
  568. pad->pullup &= PMIC_GPIO_REG_PULL_MASK;
  569. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL);
  570. if (val < 0)
  571. return val;
  572. pad->strength = val >> PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
  573. pad->strength &= PMIC_GPIO_REG_OUT_STRENGTH_MASK;
  574. pad->buffer_type = val >> PMIC_GPIO_REG_OUT_TYPE_SHIFT;
  575. pad->buffer_type &= PMIC_GPIO_REG_OUT_TYPE_MASK;
  576. /* Pin could be disabled with PIN_CONFIG_BIAS_HIGH_IMPEDANCE */
  577. pad->is_enabled = true;
  578. return 0;
  579. }
  580. static int pmic_gpio_probe(struct platform_device *pdev)
  581. {
  582. struct device *dev = &pdev->dev;
  583. struct pinctrl_pin_desc *pindesc;
  584. struct pinctrl_desc *pctrldesc;
  585. struct pmic_gpio_pad *pad, *pads;
  586. struct pmic_gpio_state *state;
  587. int ret, npins, i;
  588. u32 res[2];
  589. ret = of_property_read_u32_array(dev->of_node, "reg", res, 2);
  590. if (ret < 0) {
  591. dev_err(dev, "missing base address and/or range");
  592. return ret;
  593. }
  594. npins = res[1] / PMIC_GPIO_ADDRESS_RANGE;
  595. if (!npins)
  596. return -EINVAL;
  597. BUG_ON(npins > ARRAY_SIZE(pmic_gpio_groups));
  598. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  599. if (!state)
  600. return -ENOMEM;
  601. platform_set_drvdata(pdev, state);
  602. state->dev = &pdev->dev;
  603. state->map = dev_get_regmap(dev->parent, NULL);
  604. pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
  605. if (!pindesc)
  606. return -ENOMEM;
  607. pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
  608. if (!pads)
  609. return -ENOMEM;
  610. pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
  611. if (!pctrldesc)
  612. return -ENOMEM;
  613. pctrldesc->pctlops = &pmic_gpio_pinctrl_ops;
  614. pctrldesc->pmxops = &pmic_gpio_pinmux_ops;
  615. pctrldesc->confops = &pmic_gpio_pinconf_ops;
  616. pctrldesc->owner = THIS_MODULE;
  617. pctrldesc->name = dev_name(dev);
  618. pctrldesc->pins = pindesc;
  619. pctrldesc->npins = npins;
  620. pctrldesc->num_custom_params = ARRAY_SIZE(pmic_gpio_bindings);
  621. pctrldesc->custom_params = pmic_gpio_bindings;
  622. #ifdef CONFIG_DEBUG_FS
  623. pctrldesc->custom_conf_items = pmic_conf_items;
  624. #endif
  625. for (i = 0; i < npins; i++, pindesc++) {
  626. pad = &pads[i];
  627. pindesc->drv_data = pad;
  628. pindesc->number = i;
  629. pindesc->name = pmic_gpio_groups[i];
  630. pad->irq = platform_get_irq(pdev, i);
  631. if (pad->irq < 0)
  632. return pad->irq;
  633. pad->base = res[0] + i * PMIC_GPIO_ADDRESS_RANGE;
  634. ret = pmic_gpio_populate(state, pad);
  635. if (ret < 0)
  636. return ret;
  637. }
  638. state->chip = pmic_gpio_gpio_template;
  639. state->chip.dev = dev;
  640. state->chip.base = -1;
  641. state->chip.ngpio = npins;
  642. state->chip.label = dev_name(dev);
  643. state->chip.of_gpio_n_cells = 2;
  644. state->chip.can_sleep = false;
  645. state->ctrl = pinctrl_register(pctrldesc, dev, state);
  646. if (!state->ctrl)
  647. return -ENODEV;
  648. ret = gpiochip_add(&state->chip);
  649. if (ret) {
  650. dev_err(state->dev, "can't add gpio chip\n");
  651. goto err_chip;
  652. }
  653. ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0, npins);
  654. if (ret) {
  655. dev_err(dev, "failed to add pin range\n");
  656. goto err_range;
  657. }
  658. return 0;
  659. err_range:
  660. gpiochip_remove(&state->chip);
  661. err_chip:
  662. pinctrl_unregister(state->ctrl);
  663. return ret;
  664. }
  665. static int pmic_gpio_remove(struct platform_device *pdev)
  666. {
  667. struct pmic_gpio_state *state = platform_get_drvdata(pdev);
  668. gpiochip_remove(&state->chip);
  669. pinctrl_unregister(state->ctrl);
  670. return 0;
  671. }
  672. static const struct of_device_id pmic_gpio_of_match[] = {
  673. { .compatible = "qcom,pm8941-gpio" }, /* 36 GPIO's */
  674. { .compatible = "qcom,pma8084-gpio" }, /* 22 GPIO's */
  675. { },
  676. };
  677. MODULE_DEVICE_TABLE(of, pmic_gpio_of_match);
  678. static struct platform_driver pmic_gpio_driver = {
  679. .driver = {
  680. .name = "qcom-spmi-gpio",
  681. .of_match_table = pmic_gpio_of_match,
  682. },
  683. .probe = pmic_gpio_probe,
  684. .remove = pmic_gpio_remove,
  685. };
  686. module_platform_driver(pmic_gpio_driver);
  687. MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
  688. MODULE_DESCRIPTION("Qualcomm SPMI PMIC GPIO pin control driver");
  689. MODULE_ALIAS("platform:qcom-spmi-gpio");
  690. MODULE_LICENSE("GPL v2");