pinctrl-spmi-gpio.c 22 KB

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