pinctrl-spmi-gpio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. struct pmic_gpio_bindings {
  115. const char *property;
  116. unsigned param;
  117. };
  118. static struct pmic_gpio_bindings pmic_gpio_bindings[] = {
  119. {"qcom,pull-up-strength", PMIC_GPIO_CONF_PULL_UP},
  120. {"qcom,drive-strength", PMIC_GPIO_CONF_STRENGTH},
  121. };
  122. static const char *const pmic_gpio_groups[] = {
  123. "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7", "gpio8",
  124. "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14", "gpio15",
  125. "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21", "gpio22",
  126. "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28", "gpio29",
  127. "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35", "gpio36",
  128. };
  129. static const char *const pmic_gpio_functions[] = {
  130. PMIC_GPIO_FUNC_NORMAL, PMIC_GPIO_FUNC_PAIRED,
  131. PMIC_GPIO_FUNC_FUNC1, PMIC_GPIO_FUNC_FUNC2,
  132. PMIC_GPIO_FUNC_DTEST1, PMIC_GPIO_FUNC_DTEST2,
  133. PMIC_GPIO_FUNC_DTEST3, PMIC_GPIO_FUNC_DTEST4,
  134. };
  135. static inline struct pmic_gpio_state *to_gpio_state(struct gpio_chip *chip)
  136. {
  137. return container_of(chip, struct pmic_gpio_state, chip);
  138. };
  139. static int pmic_gpio_read(struct pmic_gpio_state *state,
  140. struct pmic_gpio_pad *pad, unsigned int addr)
  141. {
  142. unsigned int val;
  143. int ret;
  144. ret = regmap_read(state->map, pad->base + addr, &val);
  145. if (ret < 0)
  146. dev_err(state->dev, "read 0x%x failed\n", addr);
  147. else
  148. ret = val;
  149. return ret;
  150. }
  151. static int pmic_gpio_write(struct pmic_gpio_state *state,
  152. struct pmic_gpio_pad *pad, unsigned int addr,
  153. unsigned int val)
  154. {
  155. int ret;
  156. ret = regmap_write(state->map, pad->base + addr, val);
  157. if (ret < 0)
  158. dev_err(state->dev, "write 0x%x failed\n", addr);
  159. return ret;
  160. }
  161. static int pmic_gpio_get_groups_count(struct pinctrl_dev *pctldev)
  162. {
  163. /* Every PIN is a group */
  164. return pctldev->desc->npins;
  165. }
  166. static const char *pmic_gpio_get_group_name(struct pinctrl_dev *pctldev,
  167. unsigned pin)
  168. {
  169. return pctldev->desc->pins[pin].name;
  170. }
  171. static int pmic_gpio_get_group_pins(struct pinctrl_dev *pctldev, unsigned pin,
  172. const unsigned **pins, unsigned *num_pins)
  173. {
  174. *pins = &pctldev->desc->pins[pin].number;
  175. *num_pins = 1;
  176. return 0;
  177. }
  178. static int pmic_gpio_parse_dt_config(struct device_node *np,
  179. struct pinctrl_dev *pctldev,
  180. unsigned long **configs,
  181. unsigned int *nconfs)
  182. {
  183. struct pmic_gpio_bindings *par;
  184. unsigned long cfg;
  185. int ret, i;
  186. u32 val;
  187. for (i = 0; i < ARRAY_SIZE(pmic_gpio_bindings); i++) {
  188. par = &pmic_gpio_bindings[i];
  189. ret = of_property_read_u32(np, par->property, &val);
  190. /* property not found */
  191. if (ret == -EINVAL)
  192. continue;
  193. /* use zero as default value */
  194. if (ret)
  195. val = 0;
  196. dev_dbg(pctldev->dev, "found %s with value %u\n",
  197. par->property, val);
  198. cfg = pinconf_to_config_packed(par->param, val);
  199. ret = pinctrl_utils_add_config(pctldev, configs, nconfs, cfg);
  200. if (ret)
  201. return ret;
  202. }
  203. return 0;
  204. }
  205. static int pmic_gpio_dt_subnode_to_map(struct pinctrl_dev *pctldev,
  206. struct device_node *np,
  207. struct pinctrl_map **map,
  208. unsigned *reserv, unsigned *nmaps,
  209. enum pinctrl_map_type type)
  210. {
  211. unsigned long *configs = NULL;
  212. unsigned nconfs = 0;
  213. struct property *prop;
  214. const char *group;
  215. int ret;
  216. ret = pmic_gpio_parse_dt_config(np, pctldev, &configs, &nconfs);
  217. if (ret < 0)
  218. return ret;
  219. if (!nconfs)
  220. return 0;
  221. ret = of_property_count_strings(np, "pins");
  222. if (ret < 0)
  223. goto exit;
  224. ret = pinctrl_utils_reserve_map(pctldev, map, reserv, nmaps, ret);
  225. if (ret < 0)
  226. goto exit;
  227. of_property_for_each_string(np, "pins", prop, group) {
  228. ret = pinctrl_utils_add_map_configs(pctldev, map,
  229. reserv, nmaps, group,
  230. configs, nconfs, type);
  231. if (ret < 0)
  232. break;
  233. }
  234. exit:
  235. kfree(configs);
  236. return ret;
  237. }
  238. static int pmic_gpio_dt_node_to_map(struct pinctrl_dev *pctldev,
  239. struct device_node *np_config,
  240. struct pinctrl_map **map, unsigned *nmaps)
  241. {
  242. enum pinctrl_map_type type;
  243. struct device_node *np;
  244. unsigned reserv;
  245. int ret;
  246. ret = 0;
  247. *map = NULL;
  248. *nmaps = 0;
  249. reserv = 0;
  250. type = PIN_MAP_TYPE_CONFIGS_GROUP;
  251. for_each_child_of_node(np_config, np) {
  252. ret = pinconf_generic_dt_subnode_to_map(pctldev, np, map,
  253. &reserv, nmaps, type);
  254. if (ret)
  255. break;
  256. ret = pmic_gpio_dt_subnode_to_map(pctldev, np, map, &reserv,
  257. nmaps, type);
  258. if (ret)
  259. break;
  260. }
  261. if (ret < 0)
  262. pinctrl_utils_dt_free_map(pctldev, *map, *nmaps);
  263. return ret;
  264. }
  265. static const struct pinctrl_ops pmic_gpio_pinctrl_ops = {
  266. .get_groups_count = pmic_gpio_get_groups_count,
  267. .get_group_name = pmic_gpio_get_group_name,
  268. .get_group_pins = pmic_gpio_get_group_pins,
  269. .dt_node_to_map = pmic_gpio_dt_node_to_map,
  270. .dt_free_map = pinctrl_utils_dt_free_map,
  271. };
  272. static int pmic_gpio_get_functions_count(struct pinctrl_dev *pctldev)
  273. {
  274. return ARRAY_SIZE(pmic_gpio_functions);
  275. }
  276. static const char *pmic_gpio_get_function_name(struct pinctrl_dev *pctldev,
  277. unsigned function)
  278. {
  279. return pmic_gpio_functions[function];
  280. }
  281. static int pmic_gpio_get_function_groups(struct pinctrl_dev *pctldev,
  282. unsigned function,
  283. const char *const **groups,
  284. unsigned *const num_qgroups)
  285. {
  286. *groups = pmic_gpio_groups;
  287. *num_qgroups = pctldev->desc->npins;
  288. return 0;
  289. }
  290. static int pmic_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned function,
  291. unsigned pin)
  292. {
  293. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  294. struct pmic_gpio_pad *pad;
  295. unsigned int val;
  296. int ret;
  297. pad = pctldev->desc->pins[pin].drv_data;
  298. pad->function = function;
  299. val = 0;
  300. if (pad->output_enabled) {
  301. if (pad->input_enabled)
  302. val = 2;
  303. else
  304. val = 1;
  305. }
  306. val |= pad->function << PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  307. val |= pad->out_value & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  308. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
  309. if (ret < 0)
  310. return ret;
  311. val = pad->is_enabled << PMIC_GPIO_REG_MASTER_EN_SHIFT;
  312. return pmic_gpio_write(state, pad, PMIC_GPIO_REG_EN_CTL, val);
  313. }
  314. static const struct pinmux_ops pmic_gpio_pinmux_ops = {
  315. .get_functions_count = pmic_gpio_get_functions_count,
  316. .get_function_name = pmic_gpio_get_function_name,
  317. .get_function_groups = pmic_gpio_get_function_groups,
  318. .set_mux = pmic_gpio_set_mux,
  319. };
  320. static int pmic_gpio_config_get(struct pinctrl_dev *pctldev,
  321. unsigned int pin, unsigned long *config)
  322. {
  323. unsigned param = pinconf_to_config_param(*config);
  324. struct pmic_gpio_pad *pad;
  325. unsigned arg;
  326. pad = pctldev->desc->pins[pin].drv_data;
  327. switch (param) {
  328. case PIN_CONFIG_DRIVE_PUSH_PULL:
  329. arg = pad->buffer_type == PMIC_GPIO_OUT_BUF_CMOS;
  330. break;
  331. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  332. arg = pad->buffer_type == PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS;
  333. break;
  334. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  335. arg = pad->buffer_type == PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS;
  336. break;
  337. case PIN_CONFIG_BIAS_PULL_DOWN:
  338. arg = pad->pullup == PMIC_GPIO_PULL_DOWN;
  339. break;
  340. case PIN_CONFIG_BIAS_DISABLE:
  341. arg = pad->pullup = PMIC_GPIO_PULL_DISABLE;
  342. break;
  343. case PIN_CONFIG_BIAS_PULL_UP:
  344. arg = pad->pullup == PMIC_GPIO_PULL_UP_30;
  345. break;
  346. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  347. arg = !pad->is_enabled;
  348. break;
  349. case PIN_CONFIG_POWER_SOURCE:
  350. arg = pad->power_source;
  351. break;
  352. case PIN_CONFIG_INPUT_ENABLE:
  353. arg = pad->input_enabled;
  354. break;
  355. case PIN_CONFIG_OUTPUT:
  356. arg = pad->out_value;
  357. break;
  358. case PMIC_GPIO_CONF_PULL_UP:
  359. arg = pad->pullup;
  360. break;
  361. case PMIC_GPIO_CONF_STRENGTH:
  362. arg = pad->strength;
  363. break;
  364. default:
  365. return -EINVAL;
  366. }
  367. *config = pinconf_to_config_packed(param, arg);
  368. return 0;
  369. }
  370. static int pmic_gpio_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  371. unsigned long *configs, unsigned nconfs)
  372. {
  373. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  374. struct pmic_gpio_pad *pad;
  375. unsigned param, arg;
  376. unsigned int val;
  377. int i, ret;
  378. pad = pctldev->desc->pins[pin].drv_data;
  379. for (i = 0; i < nconfs; i++) {
  380. param = pinconf_to_config_param(configs[i]);
  381. arg = pinconf_to_config_argument(configs[i]);
  382. switch (param) {
  383. case PIN_CONFIG_DRIVE_PUSH_PULL:
  384. pad->buffer_type = PMIC_GPIO_OUT_BUF_CMOS;
  385. break;
  386. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  387. if (!pad->have_buffer)
  388. return -EINVAL;
  389. pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS;
  390. break;
  391. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  392. if (!pad->have_buffer)
  393. return -EINVAL;
  394. pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS;
  395. break;
  396. case PIN_CONFIG_BIAS_DISABLE:
  397. pad->pullup = PMIC_GPIO_PULL_DISABLE;
  398. break;
  399. case PIN_CONFIG_BIAS_PULL_UP:
  400. pad->pullup = PMIC_GPIO_PULL_UP_30;
  401. break;
  402. case PIN_CONFIG_BIAS_PULL_DOWN:
  403. if (arg)
  404. pad->pullup = PMIC_GPIO_PULL_DOWN;
  405. else
  406. pad->pullup = PMIC_GPIO_PULL_DISABLE;
  407. break;
  408. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  409. pad->is_enabled = false;
  410. break;
  411. case PIN_CONFIG_POWER_SOURCE:
  412. if (arg > pad->num_sources)
  413. return -EINVAL;
  414. pad->power_source = arg;
  415. break;
  416. case PIN_CONFIG_INPUT_ENABLE:
  417. pad->input_enabled = arg ? true : false;
  418. break;
  419. case PIN_CONFIG_OUTPUT:
  420. pad->output_enabled = true;
  421. pad->out_value = arg;
  422. break;
  423. case PMIC_GPIO_CONF_PULL_UP:
  424. if (arg > PMIC_GPIO_PULL_UP_1P5_30)
  425. return -EINVAL;
  426. pad->pullup = arg;
  427. break;
  428. case PMIC_GPIO_CONF_STRENGTH:
  429. if (arg > PMIC_GPIO_STRENGTH_LOW)
  430. return -EINVAL;
  431. pad->strength = arg;
  432. break;
  433. default:
  434. return -EINVAL;
  435. }
  436. }
  437. val = pad->power_source << PMIC_GPIO_REG_VIN_SHIFT;
  438. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL, val);
  439. if (ret < 0)
  440. return ret;
  441. val = pad->pullup << PMIC_GPIO_REG_PULL_SHIFT;
  442. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL, val);
  443. if (ret < 0)
  444. return ret;
  445. val = pad->buffer_type << PMIC_GPIO_REG_OUT_TYPE_SHIFT;
  446. val = pad->strength << PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
  447. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL, val);
  448. if (ret < 0)
  449. return ret;
  450. val = 0;
  451. if (pad->output_enabled) {
  452. if (pad->input_enabled)
  453. val = 2;
  454. else
  455. val = 1;
  456. }
  457. val = val << PMIC_GPIO_REG_MODE_DIR_SHIFT;
  458. val |= pad->function << PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  459. val |= pad->out_value & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  460. return pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
  461. }
  462. static void pmic_gpio_config_dbg_show(struct pinctrl_dev *pctldev,
  463. struct seq_file *s, unsigned pin)
  464. {
  465. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  466. struct pmic_gpio_pad *pad;
  467. int ret, val;
  468. static const char *const biases[] = {
  469. "pull-up 30uA", "pull-up 1.5uA", "pull-up 31.5uA",
  470. "pull-up 1.5uA + 30uA boost", "pull-down 10uA", "no pull"
  471. };
  472. static const char *const buffer_types[] = {
  473. "push-pull", "open-drain", "open-source"
  474. };
  475. static const char *const strengths[] = {
  476. "no", "high", "medium", "low"
  477. };
  478. pad = pctldev->desc->pins[pin].drv_data;
  479. seq_printf(s, " gpio%-2d:", pin + PMIC_GPIO_PHYSICAL_OFFSET);
  480. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_EN_CTL);
  481. if (val < 0 || !(val >> PMIC_GPIO_REG_MASTER_EN_SHIFT)) {
  482. seq_puts(s, " ---");
  483. } else {
  484. if (!pad->input_enabled) {
  485. ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
  486. if (!ret) {
  487. ret &= PMIC_MPP_REG_RT_STS_VAL_MASK;
  488. pad->out_value = ret;
  489. }
  490. }
  491. seq_printf(s, " %-4s", pad->output_enabled ? "out" : "in");
  492. seq_printf(s, " %-7s", pmic_gpio_functions[pad->function]);
  493. seq_printf(s, " vin-%d", pad->power_source);
  494. seq_printf(s, " %-27s", biases[pad->pullup]);
  495. seq_printf(s, " %-10s", buffer_types[pad->buffer_type]);
  496. seq_printf(s, " %-4s", pad->out_value ? "high" : "low");
  497. seq_printf(s, " %-7s", strengths[pad->strength]);
  498. }
  499. }
  500. static const struct pinconf_ops pmic_gpio_pinconf_ops = {
  501. .pin_config_group_get = pmic_gpio_config_get,
  502. .pin_config_group_set = pmic_gpio_config_set,
  503. .pin_config_group_dbg_show = pmic_gpio_config_dbg_show,
  504. };
  505. static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  506. {
  507. struct pmic_gpio_state *state = to_gpio_state(chip);
  508. unsigned long config;
  509. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
  510. return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  511. }
  512. static int pmic_gpio_direction_output(struct gpio_chip *chip,
  513. unsigned pin, int val)
  514. {
  515. struct pmic_gpio_state *state = to_gpio_state(chip);
  516. unsigned long config;
  517. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
  518. return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  519. }
  520. static int pmic_gpio_get(struct gpio_chip *chip, unsigned pin)
  521. {
  522. struct pmic_gpio_state *state = to_gpio_state(chip);
  523. struct pmic_gpio_pad *pad;
  524. int ret;
  525. pad = state->ctrl->desc->pins[pin].drv_data;
  526. if (!pad->is_enabled)
  527. return -EINVAL;
  528. if (pad->input_enabled) {
  529. ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
  530. if (ret < 0)
  531. return ret;
  532. pad->out_value = ret & PMIC_MPP_REG_RT_STS_VAL_MASK;
  533. }
  534. return pad->out_value;
  535. }
  536. static void pmic_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  537. {
  538. struct pmic_gpio_state *state = to_gpio_state(chip);
  539. unsigned long config;
  540. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
  541. pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  542. }
  543. static int pmic_gpio_request(struct gpio_chip *chip, unsigned base)
  544. {
  545. return pinctrl_request_gpio(chip->base + base);
  546. }
  547. static void pmic_gpio_free(struct gpio_chip *chip, unsigned base)
  548. {
  549. pinctrl_free_gpio(chip->base + base);
  550. }
  551. static int pmic_gpio_of_xlate(struct gpio_chip *chip,
  552. const struct of_phandle_args *gpio_desc,
  553. u32 *flags)
  554. {
  555. if (chip->of_gpio_n_cells < 2)
  556. return -EINVAL;
  557. if (flags)
  558. *flags = gpio_desc->args[1];
  559. return gpio_desc->args[0] - PMIC_GPIO_PHYSICAL_OFFSET;
  560. }
  561. static int pmic_gpio_to_irq(struct gpio_chip *chip, unsigned pin)
  562. {
  563. struct pmic_gpio_state *state = to_gpio_state(chip);
  564. struct pmic_gpio_pad *pad;
  565. pad = state->ctrl->desc->pins[pin].drv_data;
  566. return pad->irq;
  567. }
  568. static void pmic_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  569. {
  570. struct pmic_gpio_state *state = to_gpio_state(chip);
  571. unsigned i;
  572. for (i = 0; i < chip->ngpio; i++) {
  573. pmic_gpio_config_dbg_show(state->ctrl, s, i);
  574. seq_puts(s, "\n");
  575. }
  576. }
  577. static const struct gpio_chip pmic_gpio_gpio_template = {
  578. .direction_input = pmic_gpio_direction_input,
  579. .direction_output = pmic_gpio_direction_output,
  580. .get = pmic_gpio_get,
  581. .set = pmic_gpio_set,
  582. .request = pmic_gpio_request,
  583. .free = pmic_gpio_free,
  584. .of_xlate = pmic_gpio_of_xlate,
  585. .to_irq = pmic_gpio_to_irq,
  586. .dbg_show = pmic_gpio_dbg_show,
  587. };
  588. static int pmic_gpio_populate(struct pmic_gpio_state *state,
  589. struct pmic_gpio_pad *pad)
  590. {
  591. int type, subtype, val, dir;
  592. type = pmic_gpio_read(state, pad, PMIC_GPIO_REG_TYPE);
  593. if (type < 0)
  594. return type;
  595. if (type != PMIC_GPIO_TYPE) {
  596. dev_err(state->dev, "incorrect block type 0x%x at 0x%x\n",
  597. type, pad->base);
  598. return -ENODEV;
  599. }
  600. subtype = pmic_gpio_read(state, pad, PMIC_GPIO_REG_SUBTYPE);
  601. if (subtype < 0)
  602. return subtype;
  603. switch (subtype) {
  604. case PMIC_GPIO_SUBTYPE_GPIO_4CH:
  605. pad->have_buffer = true;
  606. case PMIC_GPIO_SUBTYPE_GPIOC_4CH:
  607. pad->num_sources = 4;
  608. break;
  609. case PMIC_GPIO_SUBTYPE_GPIO_8CH:
  610. pad->have_buffer = true;
  611. case PMIC_GPIO_SUBTYPE_GPIOC_8CH:
  612. pad->num_sources = 8;
  613. break;
  614. default:
  615. dev_err(state->dev, "unknown GPIO type 0x%x\n", subtype);
  616. return -ENODEV;
  617. }
  618. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_MODE_CTL);
  619. if (val < 0)
  620. return val;
  621. pad->out_value = val & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  622. dir = val >> PMIC_GPIO_REG_MODE_DIR_SHIFT;
  623. dir &= PMIC_GPIO_REG_MODE_DIR_MASK;
  624. switch (dir) {
  625. case 0:
  626. pad->input_enabled = true;
  627. pad->output_enabled = false;
  628. break;
  629. case 1:
  630. pad->input_enabled = false;
  631. pad->output_enabled = true;
  632. break;
  633. case 2:
  634. pad->input_enabled = true;
  635. pad->output_enabled = true;
  636. break;
  637. default:
  638. dev_err(state->dev, "unknown GPIO direction\n");
  639. return -ENODEV;
  640. }
  641. pad->function = val >> PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  642. pad->function &= PMIC_GPIO_REG_MODE_FUNCTION_MASK;
  643. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL);
  644. if (val < 0)
  645. return val;
  646. pad->power_source = val >> PMIC_GPIO_REG_VIN_SHIFT;
  647. pad->power_source &= PMIC_GPIO_REG_VIN_MASK;
  648. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL);
  649. if (val < 0)
  650. return val;
  651. pad->pullup = val >> PMIC_GPIO_REG_PULL_SHIFT;
  652. pad->pullup &= PMIC_GPIO_REG_PULL_MASK;
  653. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL);
  654. if (val < 0)
  655. return val;
  656. pad->strength = val >> PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
  657. pad->strength &= PMIC_GPIO_REG_OUT_STRENGTH_MASK;
  658. pad->buffer_type = val >> PMIC_GPIO_REG_OUT_TYPE_SHIFT;
  659. pad->buffer_type &= PMIC_GPIO_REG_OUT_TYPE_MASK;
  660. /* Pin could be disabled with PIN_CONFIG_BIAS_HIGH_IMPEDANCE */
  661. pad->is_enabled = true;
  662. return 0;
  663. }
  664. static int pmic_gpio_probe(struct platform_device *pdev)
  665. {
  666. struct device *dev = &pdev->dev;
  667. struct pinctrl_pin_desc *pindesc;
  668. struct pinctrl_desc *pctrldesc;
  669. struct pmic_gpio_pad *pad, *pads;
  670. struct pmic_gpio_state *state;
  671. int ret, npins, i;
  672. u32 res[2];
  673. ret = of_property_read_u32_array(dev->of_node, "reg", res, 2);
  674. if (ret < 0) {
  675. dev_err(dev, "missing base address and/or range");
  676. return ret;
  677. }
  678. npins = res[1] / PMIC_GPIO_ADDRESS_RANGE;
  679. if (!npins)
  680. return -EINVAL;
  681. BUG_ON(npins > ARRAY_SIZE(pmic_gpio_groups));
  682. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  683. if (!state)
  684. return -ENOMEM;
  685. platform_set_drvdata(pdev, state);
  686. state->dev = &pdev->dev;
  687. state->map = dev_get_regmap(dev->parent, NULL);
  688. pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
  689. if (!pindesc)
  690. return -ENOMEM;
  691. pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
  692. if (!pads)
  693. return -ENOMEM;
  694. pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
  695. if (!pctrldesc)
  696. return -ENOMEM;
  697. pctrldesc->pctlops = &pmic_gpio_pinctrl_ops;
  698. pctrldesc->pmxops = &pmic_gpio_pinmux_ops;
  699. pctrldesc->confops = &pmic_gpio_pinconf_ops;
  700. pctrldesc->owner = THIS_MODULE;
  701. pctrldesc->name = dev_name(dev);
  702. pctrldesc->pins = pindesc;
  703. pctrldesc->npins = npins;
  704. for (i = 0; i < npins; i++, pindesc++) {
  705. pad = &pads[i];
  706. pindesc->drv_data = pad;
  707. pindesc->number = i;
  708. pindesc->name = pmic_gpio_groups[i];
  709. pad->irq = platform_get_irq(pdev, i);
  710. if (pad->irq < 0)
  711. return pad->irq;
  712. pad->base = res[0] + i * PMIC_GPIO_ADDRESS_RANGE;
  713. ret = pmic_gpio_populate(state, pad);
  714. if (ret < 0)
  715. return ret;
  716. }
  717. state->chip = pmic_gpio_gpio_template;
  718. state->chip.dev = dev;
  719. state->chip.base = -1;
  720. state->chip.ngpio = npins;
  721. state->chip.label = dev_name(dev);
  722. state->chip.of_gpio_n_cells = 2;
  723. state->chip.can_sleep = false;
  724. state->ctrl = pinctrl_register(pctrldesc, dev, state);
  725. if (!state->ctrl)
  726. return -ENODEV;
  727. ret = gpiochip_add(&state->chip);
  728. if (ret) {
  729. dev_err(state->dev, "can't add gpio chip\n");
  730. goto err_chip;
  731. }
  732. ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0, npins);
  733. if (ret) {
  734. dev_err(dev, "failed to add pin range\n");
  735. goto err_range;
  736. }
  737. return 0;
  738. err_range:
  739. gpiochip_remove(&state->chip);
  740. err_chip:
  741. pinctrl_unregister(state->ctrl);
  742. return ret;
  743. }
  744. static int pmic_gpio_remove(struct platform_device *pdev)
  745. {
  746. struct pmic_gpio_state *state = platform_get_drvdata(pdev);
  747. gpiochip_remove(&state->chip);
  748. pinctrl_unregister(state->ctrl);
  749. return 0;
  750. }
  751. static const struct of_device_id pmic_gpio_of_match[] = {
  752. { .compatible = "qcom,pm8941-gpio" }, /* 36 GPIO's */
  753. { .compatible = "qcom,pma8084-gpio" }, /* 22 GPIO's */
  754. { },
  755. };
  756. MODULE_DEVICE_TABLE(of, pmic_gpio_of_match);
  757. static struct platform_driver pmic_gpio_driver = {
  758. .driver = {
  759. .name = "qcom-spmi-gpio",
  760. .of_match_table = pmic_gpio_of_match,
  761. },
  762. .probe = pmic_gpio_probe,
  763. .remove = pmic_gpio_remove,
  764. };
  765. module_platform_driver(pmic_gpio_driver);
  766. MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
  767. MODULE_DESCRIPTION("Qualcomm SPMI PMIC GPIO pin control driver");
  768. MODULE_ALIAS("platform:qcom-spmi-gpio");
  769. MODULE_LICENSE("GPL v2");