pinctrl-ssbi-gpio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * Copyright (c) 2015, Sony Mobile Communications AB.
  3. * Copyright (c) 2013, The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pinctrl/pinctrl.h>
  17. #include <linux/pinctrl/pinmux.h>
  18. #include <linux/pinctrl/pinconf.h>
  19. #include <linux/pinctrl/pinconf-generic.h>
  20. #include <linux/slab.h>
  21. #include <linux/regmap.h>
  22. #include <linux/gpio.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_irq.h>
  26. #include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
  27. #include "../core.h"
  28. #include "../pinctrl-utils.h"
  29. /* mode */
  30. #define PM8XXX_GPIO_MODE_ENABLED BIT(0)
  31. #define PM8XXX_GPIO_MODE_INPUT 0
  32. #define PM8XXX_GPIO_MODE_OUTPUT 2
  33. /* output buffer */
  34. #define PM8XXX_GPIO_PUSH_PULL 0
  35. #define PM8XXX_GPIO_OPEN_DRAIN 1
  36. /* bias */
  37. #define PM8XXX_GPIO_BIAS_PU_30 0
  38. #define PM8XXX_GPIO_BIAS_PU_1P5 1
  39. #define PM8XXX_GPIO_BIAS_PU_31P5 2
  40. #define PM8XXX_GPIO_BIAS_PU_1P5_30 3
  41. #define PM8XXX_GPIO_BIAS_PD 4
  42. #define PM8XXX_GPIO_BIAS_NP 5
  43. /* GPIO registers */
  44. #define SSBI_REG_ADDR_GPIO_BASE 0x150
  45. #define SSBI_REG_ADDR_GPIO(n) (SSBI_REG_ADDR_GPIO_BASE + n)
  46. #define PM8XXX_BANK_WRITE BIT(7)
  47. #define PM8XXX_MAX_GPIOS 44
  48. /* custom pinconf parameters */
  49. #define PM8XXX_QCOM_DRIVE_STRENGH (PIN_CONFIG_END + 1)
  50. #define PM8XXX_QCOM_PULL_UP_STRENGTH (PIN_CONFIG_END + 2)
  51. /**
  52. * struct pm8xxx_pin_data - dynamic configuration for a pin
  53. * @reg: address of the control register
  54. * @irq: IRQ from the PMIC interrupt controller
  55. * @power_source: logical selected voltage source, mapping in static data
  56. * is used translate to register values
  57. * @mode: operating mode for the pin (input/output)
  58. * @open_drain: output buffer configured as open-drain (vs push-pull)
  59. * @output_value: configured output value
  60. * @bias: register view of configured bias
  61. * @pull_up_strength: placeholder for selected pull up strength
  62. * only used to configure bias when pull up is selected
  63. * @output_strength: selector of output-strength
  64. * @disable: pin disabled / configured as tristate
  65. * @function: pinmux selector
  66. * @inverted: pin logic is inverted
  67. */
  68. struct pm8xxx_pin_data {
  69. unsigned reg;
  70. int irq;
  71. u8 power_source;
  72. u8 mode;
  73. bool open_drain;
  74. bool output_value;
  75. u8 bias;
  76. u8 pull_up_strength;
  77. u8 output_strength;
  78. bool disable;
  79. u8 function;
  80. bool inverted;
  81. };
  82. struct pm8xxx_gpio {
  83. struct device *dev;
  84. struct regmap *regmap;
  85. struct pinctrl_dev *pctrl;
  86. struct gpio_chip chip;
  87. struct pinctrl_desc desc;
  88. unsigned npins;
  89. };
  90. static const struct pinconf_generic_params pm8xxx_gpio_bindings[] = {
  91. {"qcom,drive-strength", PM8XXX_QCOM_DRIVE_STRENGH, 0},
  92. {"qcom,pull-up-strength", PM8XXX_QCOM_PULL_UP_STRENGTH, 0},
  93. };
  94. #ifdef CONFIG_DEBUG_FS
  95. static const struct pin_config_item pm8xxx_conf_items[ARRAY_SIZE(pm8xxx_gpio_bindings)] = {
  96. PCONFDUMP(PM8XXX_QCOM_DRIVE_STRENGH, "drive-strength", NULL, true),
  97. PCONFDUMP(PM8XXX_QCOM_PULL_UP_STRENGTH, "pull up strength", NULL, true),
  98. };
  99. #endif
  100. static const char * const pm8xxx_groups[PM8XXX_MAX_GPIOS] = {
  101. "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7", "gpio8",
  102. "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14", "gpio15",
  103. "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21", "gpio22",
  104. "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28", "gpio29",
  105. "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35", "gpio36",
  106. "gpio37", "gpio38", "gpio39", "gpio40", "gpio41", "gpio42", "gpio43",
  107. "gpio44",
  108. };
  109. static const char * const pm8xxx_gpio_functions[] = {
  110. PMIC_GPIO_FUNC_NORMAL, PMIC_GPIO_FUNC_PAIRED,
  111. PMIC_GPIO_FUNC_FUNC1, PMIC_GPIO_FUNC_FUNC2,
  112. PMIC_GPIO_FUNC_DTEST1, PMIC_GPIO_FUNC_DTEST2,
  113. PMIC_GPIO_FUNC_DTEST3, PMIC_GPIO_FUNC_DTEST4,
  114. };
  115. static int pm8xxx_read_bank(struct pm8xxx_gpio *pctrl,
  116. struct pm8xxx_pin_data *pin, int bank)
  117. {
  118. unsigned int val = bank << 4;
  119. int ret;
  120. ret = regmap_write(pctrl->regmap, pin->reg, val);
  121. if (ret) {
  122. dev_err(pctrl->dev, "failed to select bank %d\n", bank);
  123. return ret;
  124. }
  125. ret = regmap_read(pctrl->regmap, pin->reg, &val);
  126. if (ret) {
  127. dev_err(pctrl->dev, "failed to read register %d\n", bank);
  128. return ret;
  129. }
  130. return val;
  131. }
  132. static int pm8xxx_write_bank(struct pm8xxx_gpio *pctrl,
  133. struct pm8xxx_pin_data *pin,
  134. int bank,
  135. u8 val)
  136. {
  137. int ret;
  138. val |= PM8XXX_BANK_WRITE;
  139. val |= bank << 4;
  140. ret = regmap_write(pctrl->regmap, pin->reg, val);
  141. if (ret)
  142. dev_err(pctrl->dev, "failed to write register\n");
  143. return ret;
  144. }
  145. static int pm8xxx_get_groups_count(struct pinctrl_dev *pctldev)
  146. {
  147. struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
  148. return pctrl->npins;
  149. }
  150. static const char *pm8xxx_get_group_name(struct pinctrl_dev *pctldev,
  151. unsigned group)
  152. {
  153. return pm8xxx_groups[group];
  154. }
  155. static int pm8xxx_get_group_pins(struct pinctrl_dev *pctldev,
  156. unsigned group,
  157. const unsigned **pins,
  158. unsigned *num_pins)
  159. {
  160. struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
  161. *pins = &pctrl->desc.pins[group].number;
  162. *num_pins = 1;
  163. return 0;
  164. }
  165. static const struct pinctrl_ops pm8xxx_pinctrl_ops = {
  166. .get_groups_count = pm8xxx_get_groups_count,
  167. .get_group_name = pm8xxx_get_group_name,
  168. .get_group_pins = pm8xxx_get_group_pins,
  169. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  170. .dt_free_map = pinctrl_utils_free_map,
  171. };
  172. static int pm8xxx_get_functions_count(struct pinctrl_dev *pctldev)
  173. {
  174. return ARRAY_SIZE(pm8xxx_gpio_functions);
  175. }
  176. static const char *pm8xxx_get_function_name(struct pinctrl_dev *pctldev,
  177. unsigned function)
  178. {
  179. return pm8xxx_gpio_functions[function];
  180. }
  181. static int pm8xxx_get_function_groups(struct pinctrl_dev *pctldev,
  182. unsigned function,
  183. const char * const **groups,
  184. unsigned * const num_groups)
  185. {
  186. struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
  187. *groups = pm8xxx_groups;
  188. *num_groups = pctrl->npins;
  189. return 0;
  190. }
  191. static int pm8xxx_pinmux_set_mux(struct pinctrl_dev *pctldev,
  192. unsigned function,
  193. unsigned group)
  194. {
  195. struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
  196. struct pm8xxx_pin_data *pin = pctrl->desc.pins[group].drv_data;
  197. u8 val;
  198. pin->function = function;
  199. val = pin->function << 1;
  200. pm8xxx_write_bank(pctrl, pin, 4, val);
  201. return 0;
  202. }
  203. static const struct pinmux_ops pm8xxx_pinmux_ops = {
  204. .get_functions_count = pm8xxx_get_functions_count,
  205. .get_function_name = pm8xxx_get_function_name,
  206. .get_function_groups = pm8xxx_get_function_groups,
  207. .set_mux = pm8xxx_pinmux_set_mux,
  208. };
  209. static int pm8xxx_pin_config_get(struct pinctrl_dev *pctldev,
  210. unsigned int offset,
  211. unsigned long *config)
  212. {
  213. struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
  214. struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
  215. unsigned param = pinconf_to_config_param(*config);
  216. unsigned arg;
  217. switch (param) {
  218. case PIN_CONFIG_BIAS_DISABLE:
  219. arg = pin->bias == PM8XXX_GPIO_BIAS_NP;
  220. break;
  221. case PIN_CONFIG_BIAS_PULL_DOWN:
  222. arg = pin->bias == PM8XXX_GPIO_BIAS_PD;
  223. break;
  224. case PIN_CONFIG_BIAS_PULL_UP:
  225. arg = pin->bias <= PM8XXX_GPIO_BIAS_PU_1P5_30;
  226. break;
  227. case PM8XXX_QCOM_PULL_UP_STRENGTH:
  228. arg = pin->pull_up_strength;
  229. break;
  230. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  231. arg = pin->disable;
  232. break;
  233. case PIN_CONFIG_INPUT_ENABLE:
  234. arg = pin->mode == PM8XXX_GPIO_MODE_INPUT;
  235. break;
  236. case PIN_CONFIG_OUTPUT:
  237. if (pin->mode & PM8XXX_GPIO_MODE_OUTPUT)
  238. arg = pin->output_value;
  239. else
  240. arg = 0;
  241. break;
  242. case PIN_CONFIG_POWER_SOURCE:
  243. arg = pin->power_source;
  244. break;
  245. case PM8XXX_QCOM_DRIVE_STRENGH:
  246. arg = pin->output_strength;
  247. break;
  248. case PIN_CONFIG_DRIVE_PUSH_PULL:
  249. arg = !pin->open_drain;
  250. break;
  251. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  252. arg = pin->open_drain;
  253. break;
  254. default:
  255. return -EINVAL;
  256. }
  257. *config = pinconf_to_config_packed(param, arg);
  258. return 0;
  259. }
  260. static int pm8xxx_pin_config_set(struct pinctrl_dev *pctldev,
  261. unsigned int offset,
  262. unsigned long *configs,
  263. unsigned num_configs)
  264. {
  265. struct pm8xxx_gpio *pctrl = pinctrl_dev_get_drvdata(pctldev);
  266. struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
  267. unsigned param;
  268. unsigned arg;
  269. unsigned i;
  270. u8 banks = 0;
  271. u8 val;
  272. for (i = 0; i < num_configs; i++) {
  273. param = pinconf_to_config_param(configs[i]);
  274. arg = pinconf_to_config_argument(configs[i]);
  275. switch (param) {
  276. case PIN_CONFIG_BIAS_DISABLE:
  277. pin->bias = PM8XXX_GPIO_BIAS_NP;
  278. banks |= BIT(2);
  279. pin->disable = 0;
  280. banks |= BIT(3);
  281. break;
  282. case PIN_CONFIG_BIAS_PULL_DOWN:
  283. pin->bias = PM8XXX_GPIO_BIAS_PD;
  284. banks |= BIT(2);
  285. pin->disable = 0;
  286. banks |= BIT(3);
  287. break;
  288. case PM8XXX_QCOM_PULL_UP_STRENGTH:
  289. if (arg > PM8XXX_GPIO_BIAS_PU_1P5_30) {
  290. dev_err(pctrl->dev, "invalid pull-up strength\n");
  291. return -EINVAL;
  292. }
  293. pin->pull_up_strength = arg;
  294. /* FALLTHROUGH */
  295. case PIN_CONFIG_BIAS_PULL_UP:
  296. pin->bias = pin->pull_up_strength;
  297. banks |= BIT(2);
  298. pin->disable = 0;
  299. banks |= BIT(3);
  300. break;
  301. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  302. pin->disable = 1;
  303. banks |= BIT(3);
  304. break;
  305. case PIN_CONFIG_INPUT_ENABLE:
  306. pin->mode = PM8XXX_GPIO_MODE_INPUT;
  307. banks |= BIT(0) | BIT(1);
  308. break;
  309. case PIN_CONFIG_OUTPUT:
  310. pin->mode = PM8XXX_GPIO_MODE_OUTPUT;
  311. pin->output_value = !!arg;
  312. banks |= BIT(0) | BIT(1);
  313. break;
  314. case PIN_CONFIG_POWER_SOURCE:
  315. pin->power_source = arg;
  316. banks |= BIT(0);
  317. break;
  318. case PM8XXX_QCOM_DRIVE_STRENGH:
  319. if (arg > PMIC_GPIO_STRENGTH_LOW) {
  320. dev_err(pctrl->dev, "invalid drive strength\n");
  321. return -EINVAL;
  322. }
  323. pin->output_strength = arg;
  324. banks |= BIT(3);
  325. break;
  326. case PIN_CONFIG_DRIVE_PUSH_PULL:
  327. pin->open_drain = 0;
  328. banks |= BIT(1);
  329. break;
  330. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  331. pin->open_drain = 1;
  332. banks |= BIT(1);
  333. break;
  334. default:
  335. dev_err(pctrl->dev,
  336. "unsupported config parameter: %x\n",
  337. param);
  338. return -EINVAL;
  339. }
  340. }
  341. if (banks & BIT(0)) {
  342. val = pin->power_source << 1;
  343. val |= PM8XXX_GPIO_MODE_ENABLED;
  344. pm8xxx_write_bank(pctrl, pin, 0, val);
  345. }
  346. if (banks & BIT(1)) {
  347. val = pin->mode << 2;
  348. val |= pin->open_drain << 1;
  349. val |= pin->output_value;
  350. pm8xxx_write_bank(pctrl, pin, 1, val);
  351. }
  352. if (banks & BIT(2)) {
  353. val = pin->bias << 1;
  354. pm8xxx_write_bank(pctrl, pin, 2, val);
  355. }
  356. if (banks & BIT(3)) {
  357. val = pin->output_strength << 2;
  358. val |= pin->disable;
  359. pm8xxx_write_bank(pctrl, pin, 3, val);
  360. }
  361. if (banks & BIT(4)) {
  362. val = pin->function << 1;
  363. pm8xxx_write_bank(pctrl, pin, 4, val);
  364. }
  365. if (banks & BIT(5)) {
  366. val = 0;
  367. if (!pin->inverted)
  368. val |= BIT(3);
  369. pm8xxx_write_bank(pctrl, pin, 5, val);
  370. }
  371. return 0;
  372. }
  373. static const struct pinconf_ops pm8xxx_pinconf_ops = {
  374. .is_generic = true,
  375. .pin_config_group_get = pm8xxx_pin_config_get,
  376. .pin_config_group_set = pm8xxx_pin_config_set,
  377. };
  378. static struct pinctrl_desc pm8xxx_pinctrl_desc = {
  379. .name = "pm8xxx_gpio",
  380. .pctlops = &pm8xxx_pinctrl_ops,
  381. .pmxops = &pm8xxx_pinmux_ops,
  382. .confops = &pm8xxx_pinconf_ops,
  383. .owner = THIS_MODULE,
  384. };
  385. static int pm8xxx_gpio_direction_input(struct gpio_chip *chip,
  386. unsigned offset)
  387. {
  388. struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
  389. struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
  390. u8 val;
  391. pin->mode = PM8XXX_GPIO_MODE_INPUT;
  392. val = pin->mode << 2;
  393. pm8xxx_write_bank(pctrl, pin, 1, val);
  394. return 0;
  395. }
  396. static int pm8xxx_gpio_direction_output(struct gpio_chip *chip,
  397. unsigned offset,
  398. int value)
  399. {
  400. struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
  401. struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
  402. u8 val;
  403. pin->mode = PM8XXX_GPIO_MODE_OUTPUT;
  404. pin->output_value = !!value;
  405. val = pin->mode << 2;
  406. val |= pin->open_drain << 1;
  407. val |= pin->output_value;
  408. pm8xxx_write_bank(pctrl, pin, 1, val);
  409. return 0;
  410. }
  411. static int pm8xxx_gpio_get(struct gpio_chip *chip, unsigned offset)
  412. {
  413. struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
  414. struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
  415. bool state;
  416. int ret;
  417. if (pin->mode == PM8XXX_GPIO_MODE_OUTPUT) {
  418. ret = pin->output_value;
  419. } else {
  420. ret = irq_get_irqchip_state(pin->irq, IRQCHIP_STATE_LINE_LEVEL, &state);
  421. if (!ret)
  422. ret = !!state;
  423. }
  424. return ret;
  425. }
  426. static void pm8xxx_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  427. {
  428. struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
  429. struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
  430. u8 val;
  431. pin->output_value = !!value;
  432. val = pin->mode << 2;
  433. val |= pin->open_drain << 1;
  434. val |= pin->output_value;
  435. pm8xxx_write_bank(pctrl, pin, 1, val);
  436. }
  437. static int pm8xxx_gpio_of_xlate(struct gpio_chip *chip,
  438. const struct of_phandle_args *gpio_desc,
  439. u32 *flags)
  440. {
  441. if (chip->of_gpio_n_cells < 2)
  442. return -EINVAL;
  443. if (flags)
  444. *flags = gpio_desc->args[1];
  445. return gpio_desc->args[0] - 1;
  446. }
  447. static int pm8xxx_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  448. {
  449. struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
  450. struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
  451. return pin->irq;
  452. }
  453. #ifdef CONFIG_DEBUG_FS
  454. #include <linux/seq_file.h>
  455. static void pm8xxx_gpio_dbg_show_one(struct seq_file *s,
  456. struct pinctrl_dev *pctldev,
  457. struct gpio_chip *chip,
  458. unsigned offset,
  459. unsigned gpio)
  460. {
  461. struct pm8xxx_gpio *pctrl = gpiochip_get_data(chip);
  462. struct pm8xxx_pin_data *pin = pctrl->desc.pins[offset].drv_data;
  463. static const char * const modes[] = {
  464. "in", "both", "out", "off"
  465. };
  466. static const char * const biases[] = {
  467. "pull-up 30uA", "pull-up 1.5uA", "pull-up 31.5uA",
  468. "pull-up 1.5uA + 30uA boost", "pull-down 10uA", "no pull"
  469. };
  470. static const char * const buffer_types[] = {
  471. "push-pull", "open-drain"
  472. };
  473. static const char * const strengths[] = {
  474. "no", "high", "medium", "low"
  475. };
  476. seq_printf(s, " gpio%-2d:", offset + 1);
  477. if (pin->disable) {
  478. seq_puts(s, " ---");
  479. } else {
  480. seq_printf(s, " %-4s", modes[pin->mode]);
  481. seq_printf(s, " %-7s", pm8xxx_gpio_functions[pin->function]);
  482. seq_printf(s, " VIN%d", pin->power_source);
  483. seq_printf(s, " %-27s", biases[pin->bias]);
  484. seq_printf(s, " %-10s", buffer_types[pin->open_drain]);
  485. seq_printf(s, " %-4s", pin->output_value ? "high" : "low");
  486. seq_printf(s, " %-7s", strengths[pin->output_strength]);
  487. if (pin->inverted)
  488. seq_puts(s, " inverted");
  489. }
  490. }
  491. static void pm8xxx_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  492. {
  493. unsigned gpio = chip->base;
  494. unsigned i;
  495. for (i = 0; i < chip->ngpio; i++, gpio++) {
  496. pm8xxx_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  497. seq_puts(s, "\n");
  498. }
  499. }
  500. #else
  501. #define pm8xxx_gpio_dbg_show NULL
  502. #endif
  503. static struct gpio_chip pm8xxx_gpio_template = {
  504. .direction_input = pm8xxx_gpio_direction_input,
  505. .direction_output = pm8xxx_gpio_direction_output,
  506. .get = pm8xxx_gpio_get,
  507. .set = pm8xxx_gpio_set,
  508. .of_xlate = pm8xxx_gpio_of_xlate,
  509. .to_irq = pm8xxx_gpio_to_irq,
  510. .dbg_show = pm8xxx_gpio_dbg_show,
  511. .owner = THIS_MODULE,
  512. };
  513. static int pm8xxx_pin_populate(struct pm8xxx_gpio *pctrl,
  514. struct pm8xxx_pin_data *pin)
  515. {
  516. int val;
  517. val = pm8xxx_read_bank(pctrl, pin, 0);
  518. if (val < 0)
  519. return val;
  520. pin->power_source = (val >> 1) & 0x7;
  521. val = pm8xxx_read_bank(pctrl, pin, 1);
  522. if (val < 0)
  523. return val;
  524. pin->mode = (val >> 2) & 0x3;
  525. pin->open_drain = !!(val & BIT(1));
  526. pin->output_value = val & BIT(0);
  527. val = pm8xxx_read_bank(pctrl, pin, 2);
  528. if (val < 0)
  529. return val;
  530. pin->bias = (val >> 1) & 0x7;
  531. if (pin->bias <= PM8XXX_GPIO_BIAS_PU_1P5_30)
  532. pin->pull_up_strength = pin->bias;
  533. else
  534. pin->pull_up_strength = PM8XXX_GPIO_BIAS_PU_30;
  535. val = pm8xxx_read_bank(pctrl, pin, 3);
  536. if (val < 0)
  537. return val;
  538. pin->output_strength = (val >> 2) & 0x3;
  539. pin->disable = val & BIT(0);
  540. val = pm8xxx_read_bank(pctrl, pin, 4);
  541. if (val < 0)
  542. return val;
  543. pin->function = (val >> 1) & 0x7;
  544. val = pm8xxx_read_bank(pctrl, pin, 5);
  545. if (val < 0)
  546. return val;
  547. pin->inverted = !(val & BIT(3));
  548. return 0;
  549. }
  550. static const struct of_device_id pm8xxx_gpio_of_match[] = {
  551. { .compatible = "qcom,pm8018-gpio" },
  552. { .compatible = "qcom,pm8038-gpio" },
  553. { .compatible = "qcom,pm8058-gpio" },
  554. { .compatible = "qcom,pm8917-gpio" },
  555. { .compatible = "qcom,pm8921-gpio" },
  556. { .compatible = "qcom,ssbi-gpio" },
  557. { },
  558. };
  559. MODULE_DEVICE_TABLE(of, pm8xxx_gpio_of_match);
  560. static int pm8xxx_gpio_probe(struct platform_device *pdev)
  561. {
  562. struct pm8xxx_pin_data *pin_data;
  563. struct pinctrl_pin_desc *pins;
  564. struct pm8xxx_gpio *pctrl;
  565. int ret;
  566. int i, npins;
  567. pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
  568. if (!pctrl)
  569. return -ENOMEM;
  570. pctrl->dev = &pdev->dev;
  571. npins = platform_irq_count(pdev);
  572. if (!npins)
  573. return -EINVAL;
  574. if (npins < 0)
  575. return npins;
  576. pctrl->npins = npins;
  577. pctrl->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  578. if (!pctrl->regmap) {
  579. dev_err(&pdev->dev, "parent regmap unavailable\n");
  580. return -ENXIO;
  581. }
  582. pctrl->desc = pm8xxx_pinctrl_desc;
  583. pctrl->desc.npins = pctrl->npins;
  584. pins = devm_kcalloc(&pdev->dev,
  585. pctrl->desc.npins,
  586. sizeof(struct pinctrl_pin_desc),
  587. GFP_KERNEL);
  588. if (!pins)
  589. return -ENOMEM;
  590. pin_data = devm_kcalloc(&pdev->dev,
  591. pctrl->desc.npins,
  592. sizeof(struct pm8xxx_pin_data),
  593. GFP_KERNEL);
  594. if (!pin_data)
  595. return -ENOMEM;
  596. for (i = 0; i < pctrl->desc.npins; i++) {
  597. pin_data[i].reg = SSBI_REG_ADDR_GPIO(i);
  598. pin_data[i].irq = platform_get_irq(pdev, i);
  599. if (pin_data[i].irq < 0) {
  600. dev_err(&pdev->dev,
  601. "missing interrupts for pin %d\n", i);
  602. return pin_data[i].irq;
  603. }
  604. ret = pm8xxx_pin_populate(pctrl, &pin_data[i]);
  605. if (ret)
  606. return ret;
  607. pins[i].number = i;
  608. pins[i].name = pm8xxx_groups[i];
  609. pins[i].drv_data = &pin_data[i];
  610. }
  611. pctrl->desc.pins = pins;
  612. pctrl->desc.num_custom_params = ARRAY_SIZE(pm8xxx_gpio_bindings);
  613. pctrl->desc.custom_params = pm8xxx_gpio_bindings;
  614. #ifdef CONFIG_DEBUG_FS
  615. pctrl->desc.custom_conf_items = pm8xxx_conf_items;
  616. #endif
  617. pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
  618. if (IS_ERR(pctrl->pctrl)) {
  619. dev_err(&pdev->dev, "couldn't register pm8xxx gpio driver\n");
  620. return PTR_ERR(pctrl->pctrl);
  621. }
  622. pctrl->chip = pm8xxx_gpio_template;
  623. pctrl->chip.base = -1;
  624. pctrl->chip.parent = &pdev->dev;
  625. pctrl->chip.of_node = pdev->dev.of_node;
  626. pctrl->chip.of_gpio_n_cells = 2;
  627. pctrl->chip.label = dev_name(pctrl->dev);
  628. pctrl->chip.ngpio = pctrl->npins;
  629. ret = gpiochip_add_data(&pctrl->chip, pctrl);
  630. if (ret) {
  631. dev_err(&pdev->dev, "failed register gpiochip\n");
  632. return ret;
  633. }
  634. ret = gpiochip_add_pin_range(&pctrl->chip,
  635. dev_name(pctrl->dev),
  636. 0, 0, pctrl->chip.ngpio);
  637. if (ret) {
  638. dev_err(pctrl->dev, "failed to add pin range\n");
  639. goto unregister_gpiochip;
  640. }
  641. platform_set_drvdata(pdev, pctrl);
  642. dev_dbg(&pdev->dev, "Qualcomm pm8xxx gpio driver probed\n");
  643. return 0;
  644. unregister_gpiochip:
  645. gpiochip_remove(&pctrl->chip);
  646. return ret;
  647. }
  648. static int pm8xxx_gpio_remove(struct platform_device *pdev)
  649. {
  650. struct pm8xxx_gpio *pctrl = platform_get_drvdata(pdev);
  651. gpiochip_remove(&pctrl->chip);
  652. return 0;
  653. }
  654. static struct platform_driver pm8xxx_gpio_driver = {
  655. .driver = {
  656. .name = "qcom-ssbi-gpio",
  657. .of_match_table = pm8xxx_gpio_of_match,
  658. },
  659. .probe = pm8xxx_gpio_probe,
  660. .remove = pm8xxx_gpio_remove,
  661. };
  662. module_platform_driver(pm8xxx_gpio_driver);
  663. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  664. MODULE_DESCRIPTION("Qualcomm PM8xxx GPIO driver");
  665. MODULE_LICENSE("GPL v2");