pinctrl-spmi-gpio.c 21 KB

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