pinctrl-spmi-gpio.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2014, 2016-2021 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/gpio/driver.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #include <linux/spmi.h>
  16. #include <linux/types.h>
  17. #include <linux/pinctrl/pinconf-generic.h>
  18. #include <linux/pinctrl/pinconf.h>
  19. #include <linux/pinctrl/pinmux.h>
  20. #include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
  21. #include "../core.h"
  22. #include "../pinctrl-utils.h"
  23. #define PMIC_GPIO_ADDRESS_RANGE 0x100
  24. /* type and subtype registers base address offsets */
  25. #define PMIC_GPIO_REG_TYPE 0x4
  26. #define PMIC_GPIO_REG_SUBTYPE 0x5
  27. /* GPIO peripheral type and subtype out_values */
  28. #define PMIC_GPIO_TYPE 0x10
  29. #define PMIC_GPIO_SUBTYPE_GPIO_4CH 0x1
  30. #define PMIC_GPIO_SUBTYPE_GPIOC_4CH 0x5
  31. #define PMIC_GPIO_SUBTYPE_GPIO_8CH 0x9
  32. #define PMIC_GPIO_SUBTYPE_GPIOC_8CH 0xd
  33. #define PMIC_GPIO_SUBTYPE_GPIO_LV 0x10
  34. #define PMIC_GPIO_SUBTYPE_GPIO_MV 0x11
  35. #define PMIC_GPIO_SUBTYPE_GPIO_LV_VIN2 0x12
  36. #define PMIC_GPIO_SUBTYPE_GPIO_MV_VIN3 0x13
  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_LV_MV_DIG_OUT_SOURCE_CTL 0x44
  44. #define PMIC_GPIO_REG_DIG_IN_CTL 0x43
  45. #define PMIC_GPIO_REG_DIG_OUT_CTL 0x45
  46. #define PMIC_GPIO_REG_EN_CTL 0x46
  47. #define PMIC_GPIO_REG_LV_MV_ANA_PASS_THRU_SEL 0x4A
  48. /* PMIC_GPIO_REG_MODE_CTL */
  49. #define PMIC_GPIO_REG_MODE_VALUE_SHIFT 0x1
  50. #define PMIC_GPIO_REG_MODE_FUNCTION_SHIFT 1
  51. #define PMIC_GPIO_REG_MODE_FUNCTION_MASK 0x7
  52. #define PMIC_GPIO_REG_MODE_DIR_SHIFT 4
  53. #define PMIC_GPIO_REG_MODE_DIR_MASK 0x7
  54. #define PMIC_GPIO_MODE_DIGITAL_INPUT 0
  55. #define PMIC_GPIO_MODE_DIGITAL_OUTPUT 1
  56. #define PMIC_GPIO_MODE_DIGITAL_INPUT_OUTPUT 2
  57. #define PMIC_GPIO_MODE_ANALOG_PASS_THRU 3
  58. #define PMIC_GPIO_REG_LV_MV_MODE_DIR_MASK 0x3
  59. /* PMIC_GPIO_REG_DIG_VIN_CTL */
  60. #define PMIC_GPIO_REG_VIN_SHIFT 0
  61. #define PMIC_GPIO_REG_VIN_MASK 0x7
  62. /* PMIC_GPIO_REG_DIG_PULL_CTL */
  63. #define PMIC_GPIO_REG_PULL_SHIFT 0
  64. #define PMIC_GPIO_REG_PULL_MASK 0x7
  65. #define PMIC_GPIO_PULL_DOWN 4
  66. #define PMIC_GPIO_PULL_DISABLE 5
  67. /* PMIC_GPIO_REG_LV_MV_DIG_OUT_SOURCE_CTL for LV/MV */
  68. #define PMIC_GPIO_LV_MV_OUTPUT_INVERT 0x80
  69. #define PMIC_GPIO_LV_MV_OUTPUT_INVERT_SHIFT 7
  70. #define PMIC_GPIO_LV_MV_OUTPUT_SOURCE_SEL_MASK 0xF
  71. /* PMIC_GPIO_REG_DIG_IN_CTL */
  72. #define PMIC_GPIO_LV_MV_DIG_IN_DTEST_EN 0x80
  73. #define PMIC_GPIO_LV_MV_DIG_IN_DTEST_SEL_MASK 0x7
  74. #define PMIC_GPIO_DIG_IN_DTEST_SEL_MASK 0xf
  75. /* PMIC_GPIO_REG_DIG_OUT_CTL */
  76. #define PMIC_GPIO_REG_OUT_STRENGTH_SHIFT 0
  77. #define PMIC_GPIO_REG_OUT_STRENGTH_MASK 0x3
  78. #define PMIC_GPIO_REG_OUT_TYPE_SHIFT 4
  79. #define PMIC_GPIO_REG_OUT_TYPE_MASK 0x3
  80. /*
  81. * Output type - indicates pin should be configured as push-pull,
  82. * open drain or open source.
  83. */
  84. #define PMIC_GPIO_OUT_BUF_CMOS 0
  85. #define PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS 1
  86. #define PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS 2
  87. #define PMIC_GPIO_OUT_STRENGTH_LOW 1
  88. #define PMIC_GPIO_OUT_STRENGTH_HIGH 3
  89. /* PMIC_GPIO_REG_EN_CTL */
  90. #define PMIC_GPIO_REG_MASTER_EN_SHIFT 7
  91. #define PMIC_GPIO_PHYSICAL_OFFSET 1
  92. /* PMIC_GPIO_REG_LV_MV_ANA_PASS_THRU_SEL */
  93. #define PMIC_GPIO_LV_MV_ANA_MUX_SEL_MASK 0x3
  94. /* Qualcomm specific pin configurations */
  95. #define PMIC_GPIO_CONF_PULL_UP (PIN_CONFIG_END + 1)
  96. #define PMIC_GPIO_CONF_STRENGTH (PIN_CONFIG_END + 2)
  97. #define PMIC_GPIO_CONF_ATEST (PIN_CONFIG_END + 3)
  98. #define PMIC_GPIO_CONF_ANALOG_PASS (PIN_CONFIG_END + 4)
  99. #define PMIC_GPIO_CONF_DTEST_BUFFER (PIN_CONFIG_END + 5)
  100. /* The index of each function in pmic_gpio_functions[] array */
  101. enum pmic_gpio_func_index {
  102. PMIC_GPIO_FUNC_INDEX_NORMAL,
  103. PMIC_GPIO_FUNC_INDEX_PAIRED,
  104. PMIC_GPIO_FUNC_INDEX_FUNC1,
  105. PMIC_GPIO_FUNC_INDEX_FUNC2,
  106. PMIC_GPIO_FUNC_INDEX_FUNC3,
  107. PMIC_GPIO_FUNC_INDEX_FUNC4,
  108. PMIC_GPIO_FUNC_INDEX_DTEST1,
  109. PMIC_GPIO_FUNC_INDEX_DTEST2,
  110. PMIC_GPIO_FUNC_INDEX_DTEST3,
  111. PMIC_GPIO_FUNC_INDEX_DTEST4,
  112. };
  113. /**
  114. * struct pmic_gpio_pad - keep current GPIO settings
  115. * @base: Address base in SPMI device.
  116. * @is_enabled: Set to false when GPIO should be put in high Z state.
  117. * @out_value: Cached pin output value
  118. * @have_buffer: Set to true if GPIO output could be configured in push-pull,
  119. * open-drain or open-source mode.
  120. * @output_enabled: Set to true if GPIO output logic is enabled.
  121. * @input_enabled: Set to true if GPIO input buffer logic is enabled.
  122. * @analog_pass: Set to true if GPIO is in analog-pass-through mode.
  123. * @lv_mv_type: Set to true if GPIO subtype is GPIO_LV(0x10) or GPIO_MV(0x11).
  124. * @num_sources: Number of power-sources supported by this GPIO.
  125. * @power_source: Current power-source used.
  126. * @buffer_type: Push-pull, open-drain or open-source.
  127. * @pullup: Constant current which flow trough GPIO output buffer.
  128. * @strength: No, Low, Medium, High
  129. * @function: See pmic_gpio_functions[]
  130. * @atest: the ATEST selection for GPIO analog-pass-through mode
  131. * @dtest_buffer: the DTEST buffer selection for digital input mode.
  132. */
  133. struct pmic_gpio_pad {
  134. u16 base;
  135. bool is_enabled;
  136. bool out_value;
  137. bool have_buffer;
  138. bool output_enabled;
  139. bool input_enabled;
  140. bool analog_pass;
  141. bool lv_mv_type;
  142. unsigned int num_sources;
  143. unsigned int power_source;
  144. unsigned int buffer_type;
  145. unsigned int pullup;
  146. unsigned int strength;
  147. unsigned int function;
  148. unsigned int atest;
  149. unsigned int dtest_buffer;
  150. };
  151. struct pmic_gpio_state {
  152. struct device *dev;
  153. struct regmap *map;
  154. struct pinctrl_dev *ctrl;
  155. struct gpio_chip chip;
  156. u8 usid;
  157. u8 pid_base;
  158. };
  159. static const struct pinconf_generic_params pmic_gpio_bindings[] = {
  160. {"qcom,pull-up-strength", PMIC_GPIO_CONF_PULL_UP, 0},
  161. {"qcom,drive-strength", PMIC_GPIO_CONF_STRENGTH, 0},
  162. {"qcom,atest", PMIC_GPIO_CONF_ATEST, 0},
  163. {"qcom,analog-pass", PMIC_GPIO_CONF_ANALOG_PASS, 0},
  164. {"qcom,dtest-buffer", PMIC_GPIO_CONF_DTEST_BUFFER, 0},
  165. };
  166. #ifdef CONFIG_DEBUG_FS
  167. static const struct pin_config_item pmic_conf_items[ARRAY_SIZE(pmic_gpio_bindings)] = {
  168. PCONFDUMP(PMIC_GPIO_CONF_PULL_UP, "pull up strength", NULL, true),
  169. PCONFDUMP(PMIC_GPIO_CONF_STRENGTH, "drive-strength", NULL, true),
  170. PCONFDUMP(PMIC_GPIO_CONF_ATEST, "atest", NULL, true),
  171. PCONFDUMP(PMIC_GPIO_CONF_ANALOG_PASS, "analog-pass", NULL, true),
  172. PCONFDUMP(PMIC_GPIO_CONF_DTEST_BUFFER, "dtest-buffer", NULL, true),
  173. };
  174. #endif
  175. static const char *const pmic_gpio_groups[] = {
  176. "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7", "gpio8",
  177. "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14", "gpio15",
  178. "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21", "gpio22",
  179. "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28", "gpio29",
  180. "gpio30", "gpio31", "gpio32", "gpio33", "gpio34", "gpio35", "gpio36",
  181. };
  182. static const char *const pmic_gpio_functions[] = {
  183. [PMIC_GPIO_FUNC_INDEX_NORMAL] = PMIC_GPIO_FUNC_NORMAL,
  184. [PMIC_GPIO_FUNC_INDEX_PAIRED] = PMIC_GPIO_FUNC_PAIRED,
  185. [PMIC_GPIO_FUNC_INDEX_FUNC1] = PMIC_GPIO_FUNC_FUNC1,
  186. [PMIC_GPIO_FUNC_INDEX_FUNC2] = PMIC_GPIO_FUNC_FUNC2,
  187. [PMIC_GPIO_FUNC_INDEX_FUNC3] = PMIC_GPIO_FUNC_FUNC3,
  188. [PMIC_GPIO_FUNC_INDEX_FUNC4] = PMIC_GPIO_FUNC_FUNC4,
  189. [PMIC_GPIO_FUNC_INDEX_DTEST1] = PMIC_GPIO_FUNC_DTEST1,
  190. [PMIC_GPIO_FUNC_INDEX_DTEST2] = PMIC_GPIO_FUNC_DTEST2,
  191. [PMIC_GPIO_FUNC_INDEX_DTEST3] = PMIC_GPIO_FUNC_DTEST3,
  192. [PMIC_GPIO_FUNC_INDEX_DTEST4] = PMIC_GPIO_FUNC_DTEST4,
  193. };
  194. static int pmic_gpio_read(struct pmic_gpio_state *state,
  195. struct pmic_gpio_pad *pad, unsigned int addr)
  196. {
  197. unsigned int val;
  198. int ret;
  199. ret = regmap_read(state->map, pad->base + addr, &val);
  200. if (ret < 0)
  201. dev_err(state->dev, "read 0x%x failed\n", addr);
  202. else
  203. ret = val;
  204. return ret;
  205. }
  206. static int pmic_gpio_write(struct pmic_gpio_state *state,
  207. struct pmic_gpio_pad *pad, unsigned int addr,
  208. unsigned int val)
  209. {
  210. int ret;
  211. ret = regmap_write(state->map, pad->base + addr, val);
  212. if (ret < 0)
  213. dev_err(state->dev, "write 0x%x failed\n", addr);
  214. return ret;
  215. }
  216. static int pmic_gpio_get_groups_count(struct pinctrl_dev *pctldev)
  217. {
  218. /* Every PIN is a group */
  219. return pctldev->desc->npins;
  220. }
  221. static const char *pmic_gpio_get_group_name(struct pinctrl_dev *pctldev,
  222. unsigned pin)
  223. {
  224. return pctldev->desc->pins[pin].name;
  225. }
  226. static int pmic_gpio_get_group_pins(struct pinctrl_dev *pctldev, unsigned pin,
  227. const unsigned **pins, unsigned *num_pins)
  228. {
  229. *pins = &pctldev->desc->pins[pin].number;
  230. *num_pins = 1;
  231. return 0;
  232. }
  233. static const struct pinctrl_ops pmic_gpio_pinctrl_ops = {
  234. .get_groups_count = pmic_gpio_get_groups_count,
  235. .get_group_name = pmic_gpio_get_group_name,
  236. .get_group_pins = pmic_gpio_get_group_pins,
  237. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  238. .dt_free_map = pinctrl_utils_free_map,
  239. };
  240. static int pmic_gpio_get_functions_count(struct pinctrl_dev *pctldev)
  241. {
  242. return ARRAY_SIZE(pmic_gpio_functions);
  243. }
  244. static const char *pmic_gpio_get_function_name(struct pinctrl_dev *pctldev,
  245. unsigned function)
  246. {
  247. return pmic_gpio_functions[function];
  248. }
  249. static int pmic_gpio_get_function_groups(struct pinctrl_dev *pctldev,
  250. unsigned function,
  251. const char *const **groups,
  252. unsigned *const num_qgroups)
  253. {
  254. *groups = pmic_gpio_groups;
  255. *num_qgroups = pctldev->desc->npins;
  256. return 0;
  257. }
  258. static int pmic_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned function,
  259. unsigned pin)
  260. {
  261. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  262. struct pmic_gpio_pad *pad;
  263. unsigned int val;
  264. int ret;
  265. if (function > PMIC_GPIO_FUNC_INDEX_DTEST4) {
  266. pr_err("function: %d is not defined\n", function);
  267. return -EINVAL;
  268. }
  269. pad = pctldev->desc->pins[pin].drv_data;
  270. /*
  271. * Non-LV/MV subtypes only support 2 special functions,
  272. * offsetting the dtestx function values by 2
  273. */
  274. if (!pad->lv_mv_type) {
  275. if (function == PMIC_GPIO_FUNC_INDEX_FUNC3 ||
  276. function == PMIC_GPIO_FUNC_INDEX_FUNC4) {
  277. pr_err("LV/MV subtype doesn't have func3/func4\n");
  278. return -EINVAL;
  279. }
  280. if (function >= PMIC_GPIO_FUNC_INDEX_DTEST1)
  281. function -= (PMIC_GPIO_FUNC_INDEX_DTEST1 -
  282. PMIC_GPIO_FUNC_INDEX_FUNC3);
  283. }
  284. pad->function = function;
  285. if (pad->analog_pass)
  286. val = PMIC_GPIO_MODE_ANALOG_PASS_THRU;
  287. else if (pad->output_enabled && pad->input_enabled)
  288. val = PMIC_GPIO_MODE_DIGITAL_INPUT_OUTPUT;
  289. else if (pad->output_enabled)
  290. val = PMIC_GPIO_MODE_DIGITAL_OUTPUT;
  291. else
  292. val = PMIC_GPIO_MODE_DIGITAL_INPUT;
  293. if (pad->lv_mv_type) {
  294. ret = pmic_gpio_write(state, pad,
  295. PMIC_GPIO_REG_MODE_CTL, val);
  296. if (ret < 0)
  297. return ret;
  298. val = pad->atest - 1;
  299. ret = pmic_gpio_write(state, pad,
  300. PMIC_GPIO_REG_LV_MV_ANA_PASS_THRU_SEL, val);
  301. if (ret < 0)
  302. return ret;
  303. val = pad->out_value
  304. << PMIC_GPIO_LV_MV_OUTPUT_INVERT_SHIFT;
  305. val |= pad->function
  306. & PMIC_GPIO_LV_MV_OUTPUT_SOURCE_SEL_MASK;
  307. ret = pmic_gpio_write(state, pad,
  308. PMIC_GPIO_REG_LV_MV_DIG_OUT_SOURCE_CTL, val);
  309. if (ret < 0)
  310. return ret;
  311. } else {
  312. val = val << PMIC_GPIO_REG_MODE_DIR_SHIFT;
  313. val |= pad->function << PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  314. val |= pad->out_value & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  315. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
  316. if (ret < 0)
  317. return ret;
  318. }
  319. val = pad->is_enabled << PMIC_GPIO_REG_MASTER_EN_SHIFT;
  320. return pmic_gpio_write(state, pad, PMIC_GPIO_REG_EN_CTL, val);
  321. }
  322. static const struct pinmux_ops pmic_gpio_pinmux_ops = {
  323. .get_functions_count = pmic_gpio_get_functions_count,
  324. .get_function_name = pmic_gpio_get_function_name,
  325. .get_function_groups = pmic_gpio_get_function_groups,
  326. .set_mux = pmic_gpio_set_mux,
  327. };
  328. static int pmic_gpio_config_get(struct pinctrl_dev *pctldev,
  329. unsigned int pin, unsigned long *config)
  330. {
  331. unsigned param = pinconf_to_config_param(*config);
  332. struct pmic_gpio_pad *pad;
  333. unsigned arg;
  334. pad = pctldev->desc->pins[pin].drv_data;
  335. switch (param) {
  336. case PIN_CONFIG_DRIVE_PUSH_PULL:
  337. if (pad->buffer_type != PMIC_GPIO_OUT_BUF_CMOS)
  338. return -EINVAL;
  339. arg = 1;
  340. break;
  341. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  342. if (pad->buffer_type != PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS)
  343. return -EINVAL;
  344. arg = 1;
  345. break;
  346. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  347. if (pad->buffer_type != PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS)
  348. return -EINVAL;
  349. arg = 1;
  350. break;
  351. case PIN_CONFIG_BIAS_PULL_DOWN:
  352. if (pad->pullup != PMIC_GPIO_PULL_DOWN)
  353. return -EINVAL;
  354. arg = 1;
  355. break;
  356. case PIN_CONFIG_BIAS_DISABLE:
  357. if (pad->pullup != PMIC_GPIO_PULL_DISABLE)
  358. return -EINVAL;
  359. arg = 1;
  360. break;
  361. case PIN_CONFIG_BIAS_PULL_UP:
  362. if (pad->pullup != PMIC_GPIO_PULL_UP_30)
  363. return -EINVAL;
  364. arg = 1;
  365. break;
  366. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  367. if (pad->is_enabled)
  368. return -EINVAL;
  369. arg = 1;
  370. break;
  371. case PIN_CONFIG_POWER_SOURCE:
  372. arg = pad->power_source;
  373. break;
  374. case PIN_CONFIG_INPUT_ENABLE:
  375. if (!pad->input_enabled)
  376. return -EINVAL;
  377. arg = 1;
  378. break;
  379. case PIN_CONFIG_OUTPUT_ENABLE:
  380. arg = pad->output_enabled;
  381. break;
  382. case PIN_CONFIG_OUTPUT:
  383. arg = pad->out_value;
  384. break;
  385. case PMIC_GPIO_CONF_PULL_UP:
  386. arg = pad->pullup;
  387. break;
  388. case PMIC_GPIO_CONF_STRENGTH:
  389. switch (pad->strength) {
  390. case PMIC_GPIO_OUT_STRENGTH_HIGH:
  391. arg = PMIC_GPIO_STRENGTH_HIGH;
  392. break;
  393. case PMIC_GPIO_OUT_STRENGTH_LOW:
  394. arg = PMIC_GPIO_STRENGTH_LOW;
  395. break;
  396. default:
  397. arg = pad->strength;
  398. break;
  399. }
  400. break;
  401. case PMIC_GPIO_CONF_ATEST:
  402. arg = pad->atest;
  403. break;
  404. case PMIC_GPIO_CONF_ANALOG_PASS:
  405. arg = pad->analog_pass;
  406. break;
  407. case PMIC_GPIO_CONF_DTEST_BUFFER:
  408. arg = pad->dtest_buffer;
  409. break;
  410. default:
  411. return -EINVAL;
  412. }
  413. *config = pinconf_to_config_packed(param, arg);
  414. return 0;
  415. }
  416. static int pmic_gpio_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  417. unsigned long *configs, unsigned nconfs)
  418. {
  419. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  420. struct pmic_gpio_pad *pad;
  421. unsigned param, arg;
  422. unsigned int val;
  423. int i, ret;
  424. pad = pctldev->desc->pins[pin].drv_data;
  425. pad->is_enabled = true;
  426. for (i = 0; i < nconfs; i++) {
  427. param = pinconf_to_config_param(configs[i]);
  428. arg = pinconf_to_config_argument(configs[i]);
  429. switch (param) {
  430. case PIN_CONFIG_DRIVE_PUSH_PULL:
  431. pad->buffer_type = PMIC_GPIO_OUT_BUF_CMOS;
  432. break;
  433. case PIN_CONFIG_DRIVE_OPEN_DRAIN:
  434. if (!pad->have_buffer)
  435. return -EINVAL;
  436. pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_NMOS;
  437. break;
  438. case PIN_CONFIG_DRIVE_OPEN_SOURCE:
  439. if (!pad->have_buffer)
  440. return -EINVAL;
  441. pad->buffer_type = PMIC_GPIO_OUT_BUF_OPEN_DRAIN_PMOS;
  442. break;
  443. case PIN_CONFIG_BIAS_DISABLE:
  444. pad->pullup = PMIC_GPIO_PULL_DISABLE;
  445. break;
  446. case PIN_CONFIG_BIAS_PULL_UP:
  447. pad->pullup = PMIC_GPIO_PULL_UP_30;
  448. break;
  449. case PIN_CONFIG_BIAS_PULL_DOWN:
  450. if (arg)
  451. pad->pullup = PMIC_GPIO_PULL_DOWN;
  452. else
  453. pad->pullup = PMIC_GPIO_PULL_DISABLE;
  454. break;
  455. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  456. pad->is_enabled = false;
  457. break;
  458. case PIN_CONFIG_POWER_SOURCE:
  459. if (arg >= pad->num_sources)
  460. return -EINVAL;
  461. pad->power_source = arg;
  462. break;
  463. case PIN_CONFIG_INPUT_ENABLE:
  464. pad->input_enabled = arg ? true : false;
  465. break;
  466. case PIN_CONFIG_OUTPUT_ENABLE:
  467. pad->output_enabled = arg ? true : false;
  468. break;
  469. case PIN_CONFIG_OUTPUT:
  470. pad->output_enabled = true;
  471. pad->out_value = arg;
  472. break;
  473. case PMIC_GPIO_CONF_PULL_UP:
  474. if (arg > PMIC_GPIO_PULL_UP_1P5_30)
  475. return -EINVAL;
  476. pad->pullup = arg;
  477. break;
  478. case PMIC_GPIO_CONF_STRENGTH:
  479. if (arg > PMIC_GPIO_STRENGTH_LOW)
  480. return -EINVAL;
  481. switch (arg) {
  482. case PMIC_GPIO_STRENGTH_HIGH:
  483. pad->strength = PMIC_GPIO_OUT_STRENGTH_HIGH;
  484. break;
  485. case PMIC_GPIO_STRENGTH_LOW:
  486. pad->strength = PMIC_GPIO_OUT_STRENGTH_LOW;
  487. break;
  488. default:
  489. pad->strength = arg;
  490. break;
  491. }
  492. break;
  493. case PMIC_GPIO_CONF_ATEST:
  494. if (!pad->lv_mv_type || arg > 4)
  495. return -EINVAL;
  496. pad->atest = arg;
  497. break;
  498. case PMIC_GPIO_CONF_ANALOG_PASS:
  499. if (!pad->lv_mv_type)
  500. return -EINVAL;
  501. pad->analog_pass = true;
  502. break;
  503. case PMIC_GPIO_CONF_DTEST_BUFFER:
  504. if (arg > 4)
  505. return -EINVAL;
  506. pad->dtest_buffer = arg;
  507. break;
  508. default:
  509. return -EINVAL;
  510. }
  511. }
  512. val = pad->power_source << PMIC_GPIO_REG_VIN_SHIFT;
  513. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL, val);
  514. if (ret < 0)
  515. return ret;
  516. val = pad->pullup << PMIC_GPIO_REG_PULL_SHIFT;
  517. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL, val);
  518. if (ret < 0)
  519. return ret;
  520. val = pad->buffer_type << PMIC_GPIO_REG_OUT_TYPE_SHIFT;
  521. val |= pad->strength << PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
  522. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL, val);
  523. if (ret < 0)
  524. return ret;
  525. if (pad->dtest_buffer == 0) {
  526. val = 0;
  527. } else {
  528. if (pad->lv_mv_type) {
  529. val = pad->dtest_buffer - 1;
  530. val |= PMIC_GPIO_LV_MV_DIG_IN_DTEST_EN;
  531. } else {
  532. val = BIT(pad->dtest_buffer - 1);
  533. }
  534. }
  535. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_DIG_IN_CTL, val);
  536. if (ret < 0)
  537. return ret;
  538. if (pad->analog_pass)
  539. val = PMIC_GPIO_MODE_ANALOG_PASS_THRU;
  540. else if (pad->output_enabled && pad->input_enabled)
  541. val = PMIC_GPIO_MODE_DIGITAL_INPUT_OUTPUT;
  542. else if (pad->output_enabled)
  543. val = PMIC_GPIO_MODE_DIGITAL_OUTPUT;
  544. else
  545. val = PMIC_GPIO_MODE_DIGITAL_INPUT;
  546. if (pad->lv_mv_type) {
  547. ret = pmic_gpio_write(state, pad,
  548. PMIC_GPIO_REG_MODE_CTL, val);
  549. if (ret < 0)
  550. return ret;
  551. val = pad->atest - 1;
  552. ret = pmic_gpio_write(state, pad,
  553. PMIC_GPIO_REG_LV_MV_ANA_PASS_THRU_SEL, val);
  554. if (ret < 0)
  555. return ret;
  556. val = pad->out_value
  557. << PMIC_GPIO_LV_MV_OUTPUT_INVERT_SHIFT;
  558. val |= pad->function
  559. & PMIC_GPIO_LV_MV_OUTPUT_SOURCE_SEL_MASK;
  560. ret = pmic_gpio_write(state, pad,
  561. PMIC_GPIO_REG_LV_MV_DIG_OUT_SOURCE_CTL, val);
  562. if (ret < 0)
  563. return ret;
  564. } else {
  565. val = val << PMIC_GPIO_REG_MODE_DIR_SHIFT;
  566. val |= pad->function << PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  567. val |= pad->out_value & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  568. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_MODE_CTL, val);
  569. if (ret < 0)
  570. return ret;
  571. }
  572. val = pad->is_enabled << PMIC_GPIO_REG_MASTER_EN_SHIFT;
  573. ret = pmic_gpio_write(state, pad, PMIC_GPIO_REG_EN_CTL, val);
  574. return ret;
  575. }
  576. static void pmic_gpio_config_dbg_show(struct pinctrl_dev *pctldev,
  577. struct seq_file *s, unsigned pin)
  578. {
  579. struct pmic_gpio_state *state = pinctrl_dev_get_drvdata(pctldev);
  580. struct pmic_gpio_pad *pad;
  581. int ret, val, function;
  582. static const char *const biases[] = {
  583. "pull-up 30uA", "pull-up 1.5uA", "pull-up 31.5uA",
  584. "pull-up 1.5uA + 30uA boost", "pull-down 10uA", "no pull"
  585. };
  586. static const char *const buffer_types[] = {
  587. "push-pull", "open-drain", "open-source"
  588. };
  589. static const char *const strengths[] = {
  590. "no", "low", "medium", "high"
  591. };
  592. pad = pctldev->desc->pins[pin].drv_data;
  593. seq_printf(s, " gpio%-2d:", pin + PMIC_GPIO_PHYSICAL_OFFSET);
  594. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_EN_CTL);
  595. if (val < 0 || !(val >> PMIC_GPIO_REG_MASTER_EN_SHIFT)) {
  596. seq_puts(s, " ---");
  597. } else {
  598. if (pad->input_enabled) {
  599. ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
  600. if (ret < 0)
  601. return;
  602. ret &= PMIC_MPP_REG_RT_STS_VAL_MASK;
  603. pad->out_value = ret;
  604. }
  605. /*
  606. * For the non-LV/MV subtypes only 2 special functions are
  607. * available, offsetting the dtest function values by 2.
  608. */
  609. function = pad->function;
  610. if (!pad->lv_mv_type &&
  611. pad->function >= PMIC_GPIO_FUNC_INDEX_FUNC3)
  612. function += PMIC_GPIO_FUNC_INDEX_DTEST1 -
  613. PMIC_GPIO_FUNC_INDEX_FUNC3;
  614. if (pad->analog_pass)
  615. seq_puts(s, " analog-pass");
  616. else
  617. seq_printf(s, " %-4s",
  618. pad->output_enabled ? "out" : "in");
  619. seq_printf(s, " %-4s", pad->out_value ? "high" : "low");
  620. seq_printf(s, " %-7s", pmic_gpio_functions[function]);
  621. seq_printf(s, " vin-%d", pad->power_source);
  622. seq_printf(s, " %-27s", biases[pad->pullup]);
  623. seq_printf(s, " %-10s", buffer_types[pad->buffer_type]);
  624. seq_printf(s, " %-7s", strengths[pad->strength]);
  625. seq_printf(s, " atest-%d", pad->atest);
  626. seq_printf(s, " dtest-%d", pad->dtest_buffer);
  627. }
  628. }
  629. static const struct pinconf_ops pmic_gpio_pinconf_ops = {
  630. .is_generic = true,
  631. .pin_config_group_get = pmic_gpio_config_get,
  632. .pin_config_group_set = pmic_gpio_config_set,
  633. .pin_config_group_dbg_show = pmic_gpio_config_dbg_show,
  634. };
  635. static int pmic_gpio_direction_input(struct gpio_chip *chip, unsigned pin)
  636. {
  637. struct pmic_gpio_state *state = gpiochip_get_data(chip);
  638. unsigned long config;
  639. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
  640. return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  641. }
  642. static int pmic_gpio_direction_output(struct gpio_chip *chip,
  643. unsigned pin, int val)
  644. {
  645. struct pmic_gpio_state *state = gpiochip_get_data(chip);
  646. unsigned long config;
  647. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
  648. return pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  649. }
  650. static int pmic_gpio_get(struct gpio_chip *chip, unsigned pin)
  651. {
  652. struct pmic_gpio_state *state = gpiochip_get_data(chip);
  653. struct pmic_gpio_pad *pad;
  654. int ret;
  655. pad = state->ctrl->desc->pins[pin].drv_data;
  656. if (!pad->is_enabled)
  657. return -EINVAL;
  658. if (pad->input_enabled) {
  659. ret = pmic_gpio_read(state, pad, PMIC_MPP_REG_RT_STS);
  660. if (ret < 0)
  661. return ret;
  662. pad->out_value = ret & PMIC_MPP_REG_RT_STS_VAL_MASK;
  663. }
  664. return !!pad->out_value;
  665. }
  666. static void pmic_gpio_set(struct gpio_chip *chip, unsigned pin, int value)
  667. {
  668. struct pmic_gpio_state *state = gpiochip_get_data(chip);
  669. unsigned long config;
  670. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
  671. pmic_gpio_config_set(state->ctrl, pin, &config, 1);
  672. }
  673. static int pmic_gpio_of_xlate(struct gpio_chip *chip,
  674. const struct of_phandle_args *gpio_desc,
  675. u32 *flags)
  676. {
  677. if (chip->of_gpio_n_cells < 2)
  678. return -EINVAL;
  679. if (flags)
  680. *flags = gpio_desc->args[1];
  681. return gpio_desc->args[0] - PMIC_GPIO_PHYSICAL_OFFSET;
  682. }
  683. static void pmic_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  684. {
  685. struct pmic_gpio_state *state = gpiochip_get_data(chip);
  686. unsigned i;
  687. for (i = 0; i < chip->ngpio; i++) {
  688. pmic_gpio_config_dbg_show(state->ctrl, s, i);
  689. seq_puts(s, "\n");
  690. }
  691. }
  692. static const struct gpio_chip pmic_gpio_gpio_template = {
  693. .direction_input = pmic_gpio_direction_input,
  694. .direction_output = pmic_gpio_direction_output,
  695. .get = pmic_gpio_get,
  696. .set = pmic_gpio_set,
  697. .request = gpiochip_generic_request,
  698. .free = gpiochip_generic_free,
  699. .of_xlate = pmic_gpio_of_xlate,
  700. .dbg_show = pmic_gpio_dbg_show,
  701. };
  702. static int pmic_gpio_populate(struct pmic_gpio_state *state,
  703. struct pmic_gpio_pad *pad)
  704. {
  705. int type, subtype, val, dir;
  706. type = pmic_gpio_read(state, pad, PMIC_GPIO_REG_TYPE);
  707. if (type < 0)
  708. return type;
  709. if (type != PMIC_GPIO_TYPE) {
  710. dev_err(state->dev, "incorrect block type 0x%x at 0x%x\n",
  711. type, pad->base);
  712. return -ENODEV;
  713. }
  714. subtype = pmic_gpio_read(state, pad, PMIC_GPIO_REG_SUBTYPE);
  715. if (subtype < 0)
  716. return subtype;
  717. switch (subtype) {
  718. case PMIC_GPIO_SUBTYPE_GPIO_4CH:
  719. pad->have_buffer = true;
  720. fallthrough;
  721. case PMIC_GPIO_SUBTYPE_GPIOC_4CH:
  722. pad->num_sources = 4;
  723. break;
  724. case PMIC_GPIO_SUBTYPE_GPIO_8CH:
  725. pad->have_buffer = true;
  726. fallthrough;
  727. case PMIC_GPIO_SUBTYPE_GPIOC_8CH:
  728. pad->num_sources = 8;
  729. break;
  730. case PMIC_GPIO_SUBTYPE_GPIO_LV:
  731. pad->num_sources = 1;
  732. pad->have_buffer = true;
  733. pad->lv_mv_type = true;
  734. break;
  735. case PMIC_GPIO_SUBTYPE_GPIO_MV:
  736. pad->num_sources = 2;
  737. pad->have_buffer = true;
  738. pad->lv_mv_type = true;
  739. break;
  740. case PMIC_GPIO_SUBTYPE_GPIO_LV_VIN2:
  741. pad->num_sources = 2;
  742. pad->have_buffer = true;
  743. pad->lv_mv_type = true;
  744. break;
  745. case PMIC_GPIO_SUBTYPE_GPIO_MV_VIN3:
  746. pad->num_sources = 3;
  747. pad->have_buffer = true;
  748. pad->lv_mv_type = true;
  749. break;
  750. default:
  751. dev_err(state->dev, "unknown GPIO type 0x%x\n", subtype);
  752. return -ENODEV;
  753. }
  754. if (pad->lv_mv_type) {
  755. val = pmic_gpio_read(state, pad,
  756. PMIC_GPIO_REG_LV_MV_DIG_OUT_SOURCE_CTL);
  757. if (val < 0)
  758. return val;
  759. pad->out_value = !!(val & PMIC_GPIO_LV_MV_OUTPUT_INVERT);
  760. pad->function = val & PMIC_GPIO_LV_MV_OUTPUT_SOURCE_SEL_MASK;
  761. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_MODE_CTL);
  762. if (val < 0)
  763. return val;
  764. dir = val & PMIC_GPIO_REG_LV_MV_MODE_DIR_MASK;
  765. } else {
  766. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_MODE_CTL);
  767. if (val < 0)
  768. return val;
  769. pad->out_value = val & PMIC_GPIO_REG_MODE_VALUE_SHIFT;
  770. dir = val >> PMIC_GPIO_REG_MODE_DIR_SHIFT;
  771. dir &= PMIC_GPIO_REG_MODE_DIR_MASK;
  772. pad->function = val >> PMIC_GPIO_REG_MODE_FUNCTION_SHIFT;
  773. pad->function &= PMIC_GPIO_REG_MODE_FUNCTION_MASK;
  774. }
  775. switch (dir) {
  776. case PMIC_GPIO_MODE_DIGITAL_INPUT:
  777. pad->input_enabled = true;
  778. pad->output_enabled = false;
  779. break;
  780. case PMIC_GPIO_MODE_DIGITAL_OUTPUT:
  781. pad->input_enabled = false;
  782. pad->output_enabled = true;
  783. break;
  784. case PMIC_GPIO_MODE_DIGITAL_INPUT_OUTPUT:
  785. pad->input_enabled = true;
  786. pad->output_enabled = true;
  787. break;
  788. case PMIC_GPIO_MODE_ANALOG_PASS_THRU:
  789. if (!pad->lv_mv_type)
  790. return -ENODEV;
  791. pad->analog_pass = true;
  792. break;
  793. default:
  794. dev_err(state->dev, "unknown GPIO direction\n");
  795. return -ENODEV;
  796. }
  797. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_VIN_CTL);
  798. if (val < 0)
  799. return val;
  800. pad->power_source = val >> PMIC_GPIO_REG_VIN_SHIFT;
  801. pad->power_source &= PMIC_GPIO_REG_VIN_MASK;
  802. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_PULL_CTL);
  803. if (val < 0)
  804. return val;
  805. pad->pullup = val >> PMIC_GPIO_REG_PULL_SHIFT;
  806. pad->pullup &= PMIC_GPIO_REG_PULL_MASK;
  807. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_IN_CTL);
  808. if (val < 0)
  809. return val;
  810. if (pad->lv_mv_type && (val & PMIC_GPIO_LV_MV_DIG_IN_DTEST_EN))
  811. pad->dtest_buffer =
  812. (val & PMIC_GPIO_LV_MV_DIG_IN_DTEST_SEL_MASK) + 1;
  813. else if (!pad->lv_mv_type)
  814. pad->dtest_buffer = ffs(val);
  815. else
  816. pad->dtest_buffer = 0;
  817. val = pmic_gpio_read(state, pad, PMIC_GPIO_REG_DIG_OUT_CTL);
  818. if (val < 0)
  819. return val;
  820. pad->strength = val >> PMIC_GPIO_REG_OUT_STRENGTH_SHIFT;
  821. pad->strength &= PMIC_GPIO_REG_OUT_STRENGTH_MASK;
  822. pad->buffer_type = val >> PMIC_GPIO_REG_OUT_TYPE_SHIFT;
  823. pad->buffer_type &= PMIC_GPIO_REG_OUT_TYPE_MASK;
  824. if (pad->lv_mv_type) {
  825. val = pmic_gpio_read(state, pad,
  826. PMIC_GPIO_REG_LV_MV_ANA_PASS_THRU_SEL);
  827. if (val < 0)
  828. return val;
  829. pad->atest = (val & PMIC_GPIO_LV_MV_ANA_MUX_SEL_MASK) + 1;
  830. }
  831. /* Pin could be disabled with PIN_CONFIG_BIAS_HIGH_IMPEDANCE */
  832. pad->is_enabled = true;
  833. return 0;
  834. }
  835. static int pmic_gpio_domain_translate(struct irq_domain *domain,
  836. struct irq_fwspec *fwspec,
  837. unsigned long *hwirq,
  838. unsigned int *type)
  839. {
  840. struct pmic_gpio_state *state = container_of(domain->host_data,
  841. struct pmic_gpio_state,
  842. chip);
  843. if (fwspec->param_count != 2 ||
  844. fwspec->param[0] < 1 || fwspec->param[0] > state->chip.ngpio)
  845. return -EINVAL;
  846. *hwirq = fwspec->param[0] - PMIC_GPIO_PHYSICAL_OFFSET;
  847. *type = fwspec->param[1];
  848. return 0;
  849. }
  850. static unsigned int pmic_gpio_child_offset_to_irq(struct gpio_chip *chip,
  851. unsigned int offset)
  852. {
  853. return offset + PMIC_GPIO_PHYSICAL_OFFSET;
  854. }
  855. static int pmic_gpio_child_to_parent_hwirq(struct gpio_chip *chip,
  856. unsigned int child_hwirq,
  857. unsigned int child_type,
  858. unsigned int *parent_hwirq,
  859. unsigned int *parent_type)
  860. {
  861. struct pmic_gpio_state *state = gpiochip_get_data(chip);
  862. *parent_hwirq = child_hwirq + state->pid_base;
  863. *parent_type = child_type;
  864. return 0;
  865. }
  866. static int pmic_gpio_populate_parent_fwspec(struct gpio_chip *chip,
  867. union gpio_irq_fwspec *gfwspec,
  868. unsigned int parent_hwirq,
  869. unsigned int parent_type)
  870. {
  871. struct pmic_gpio_state *state = gpiochip_get_data(chip);
  872. struct irq_fwspec *fwspec = &gfwspec->fwspec;
  873. fwspec->fwnode = chip->irq.parent_domain->fwnode;
  874. fwspec->param_count = 4;
  875. fwspec->param[0] = state->usid;
  876. fwspec->param[1] = parent_hwirq;
  877. /* param[2] must be left as 0 */
  878. fwspec->param[3] = parent_type;
  879. return 0;
  880. }
  881. static void pmic_gpio_irq_mask(struct irq_data *data)
  882. {
  883. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  884. irq_chip_mask_parent(data);
  885. gpiochip_disable_irq(gc, data->hwirq);
  886. }
  887. static void pmic_gpio_irq_unmask(struct irq_data *data)
  888. {
  889. struct gpio_chip *gc = irq_data_get_irq_chip_data(data);
  890. gpiochip_enable_irq(gc, data->hwirq);
  891. irq_chip_unmask_parent(data);
  892. }
  893. static const struct irq_chip spmi_gpio_irq_chip = {
  894. .name = "spmi-gpio",
  895. .irq_ack = irq_chip_ack_parent,
  896. .irq_mask = pmic_gpio_irq_mask,
  897. .irq_unmask = pmic_gpio_irq_unmask,
  898. .irq_set_type = irq_chip_set_type_parent,
  899. .irq_set_wake = irq_chip_set_wake_parent,
  900. .flags = IRQCHIP_IMMUTABLE | IRQCHIP_MASK_ON_SUSPEND,
  901. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  902. };
  903. static int pmic_gpio_probe(struct platform_device *pdev)
  904. {
  905. struct irq_domain *parent_domain;
  906. struct device_node *parent_node;
  907. struct device *dev = &pdev->dev;
  908. struct pinctrl_pin_desc *pindesc;
  909. struct pinctrl_desc *pctrldesc;
  910. struct pmic_gpio_pad *pad, *pads;
  911. struct pmic_gpio_state *state;
  912. struct gpio_irq_chip *girq;
  913. const struct spmi_device *parent_spmi_dev;
  914. int ret, npins, i;
  915. u32 reg;
  916. ret = of_property_read_u32(dev->of_node, "reg", &reg);
  917. if (ret < 0) {
  918. dev_err(dev, "missing base address");
  919. return ret;
  920. }
  921. npins = (uintptr_t) device_get_match_data(&pdev->dev);
  922. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  923. if (!state)
  924. return -ENOMEM;
  925. platform_set_drvdata(pdev, state);
  926. state->dev = &pdev->dev;
  927. state->map = dev_get_regmap(dev->parent, NULL);
  928. parent_spmi_dev = to_spmi_device(dev->parent);
  929. state->usid = parent_spmi_dev->usid;
  930. state->pid_base = reg >> 8;
  931. pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
  932. if (!pindesc)
  933. return -ENOMEM;
  934. pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
  935. if (!pads)
  936. return -ENOMEM;
  937. pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
  938. if (!pctrldesc)
  939. return -ENOMEM;
  940. pctrldesc->pctlops = &pmic_gpio_pinctrl_ops;
  941. pctrldesc->pmxops = &pmic_gpio_pinmux_ops;
  942. pctrldesc->confops = &pmic_gpio_pinconf_ops;
  943. pctrldesc->owner = THIS_MODULE;
  944. pctrldesc->name = dev_name(dev);
  945. pctrldesc->pins = pindesc;
  946. pctrldesc->npins = npins;
  947. pctrldesc->num_custom_params = ARRAY_SIZE(pmic_gpio_bindings);
  948. pctrldesc->custom_params = pmic_gpio_bindings;
  949. #ifdef CONFIG_DEBUG_FS
  950. pctrldesc->custom_conf_items = pmic_conf_items;
  951. #endif
  952. for (i = 0; i < npins; i++, pindesc++) {
  953. pad = &pads[i];
  954. pindesc->drv_data = pad;
  955. pindesc->number = i;
  956. pindesc->name = pmic_gpio_groups[i];
  957. pad->base = reg + i * PMIC_GPIO_ADDRESS_RANGE;
  958. ret = pmic_gpio_populate(state, pad);
  959. if (ret < 0)
  960. return ret;
  961. }
  962. state->chip = pmic_gpio_gpio_template;
  963. state->chip.parent = dev;
  964. state->chip.base = -1;
  965. state->chip.ngpio = npins;
  966. state->chip.label = dev_name(dev);
  967. state->chip.of_gpio_n_cells = 2;
  968. state->chip.can_sleep = false;
  969. state->ctrl = devm_pinctrl_register(dev, pctrldesc, state);
  970. if (IS_ERR(state->ctrl))
  971. return PTR_ERR(state->ctrl);
  972. parent_node = of_irq_find_parent(state->dev->of_node);
  973. if (!parent_node)
  974. return -ENXIO;
  975. parent_domain = irq_find_host(parent_node);
  976. of_node_put(parent_node);
  977. if (!parent_domain)
  978. return -ENXIO;
  979. girq = &state->chip.irq;
  980. gpio_irq_chip_set_chip(girq, &spmi_gpio_irq_chip);
  981. girq->default_type = IRQ_TYPE_NONE;
  982. girq->handler = handle_level_irq;
  983. girq->fwnode = dev_fwnode(state->dev);
  984. girq->parent_domain = parent_domain;
  985. girq->child_to_parent_hwirq = pmic_gpio_child_to_parent_hwirq;
  986. girq->populate_parent_alloc_arg = pmic_gpio_populate_parent_fwspec;
  987. girq->child_offset_to_irq = pmic_gpio_child_offset_to_irq;
  988. girq->child_irq_domain_ops.translate = pmic_gpio_domain_translate;
  989. ret = gpiochip_add_data(&state->chip, state);
  990. if (ret) {
  991. dev_err(state->dev, "can't add gpio chip\n");
  992. return ret;
  993. }
  994. /*
  995. * For DeviceTree-supported systems, the gpio core checks the
  996. * pinctrl's device node for the "gpio-ranges" property.
  997. * If it is present, it takes care of adding the pin ranges
  998. * for the driver. In this case the driver can skip ahead.
  999. *
  1000. * In order to remain compatible with older, existing DeviceTree
  1001. * files which don't set the "gpio-ranges" property or systems that
  1002. * utilize ACPI the driver has to call gpiochip_add_pin_range().
  1003. */
  1004. if (!of_property_read_bool(dev->of_node, "gpio-ranges")) {
  1005. ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0,
  1006. npins);
  1007. if (ret) {
  1008. dev_err(dev, "failed to add pin range\n");
  1009. goto err_range;
  1010. }
  1011. }
  1012. return 0;
  1013. err_range:
  1014. gpiochip_remove(&state->chip);
  1015. return ret;
  1016. }
  1017. static void pmic_gpio_remove(struct platform_device *pdev)
  1018. {
  1019. struct pmic_gpio_state *state = platform_get_drvdata(pdev);
  1020. gpiochip_remove(&state->chip);
  1021. }
  1022. static const struct of_device_id pmic_gpio_of_match[] = {
  1023. { .compatible = "qcom,pm2250-gpio", .data = (void *) 10 },
  1024. /* pm660 has 13 GPIOs with holes on 1, 5, 6, 7, 8 and 10 */
  1025. { .compatible = "qcom,pm660-gpio", .data = (void *) 13 },
  1026. /* pm660l has 12 GPIOs with holes on 1, 2, 10, 11 and 12 */
  1027. { .compatible = "qcom,pm660l-gpio", .data = (void *) 12 },
  1028. { .compatible = "qcom,pm6125-gpio", .data = (void *) 9 },
  1029. { .compatible = "qcom,pm6150-gpio", .data = (void *) 10 },
  1030. { .compatible = "qcom,pm6150l-gpio", .data = (void *) 12 },
  1031. { .compatible = "qcom,pm6350-gpio", .data = (void *) 9 },
  1032. { .compatible = "qcom,pm6450-gpio", .data = (void *) 9 },
  1033. { .compatible = "qcom,pm7250b-gpio", .data = (void *) 12 },
  1034. { .compatible = "qcom,pm7325-gpio", .data = (void *) 10 },
  1035. { .compatible = "qcom,pm7550ba-gpio", .data = (void *) 8},
  1036. { .compatible = "qcom,pm8005-gpio", .data = (void *) 4 },
  1037. { .compatible = "qcom,pm8019-gpio", .data = (void *) 6 },
  1038. /* pm8150 has 10 GPIOs with holes on 2, 5, 7 and 8 */
  1039. { .compatible = "qcom,pm8150-gpio", .data = (void *) 10 },
  1040. { .compatible = "qcom,pmc8180-gpio", .data = (void *) 10 },
  1041. /* pm8150b has 12 GPIOs with holes on 3, r and 7 */
  1042. { .compatible = "qcom,pm8150b-gpio", .data = (void *) 12 },
  1043. /* pm8150l has 12 GPIOs with holes on 7 */
  1044. { .compatible = "qcom,pm8150l-gpio", .data = (void *) 12 },
  1045. { .compatible = "qcom,pmc8180c-gpio", .data = (void *) 12 },
  1046. { .compatible = "qcom,pm8226-gpio", .data = (void *) 8 },
  1047. { .compatible = "qcom,pm8350-gpio", .data = (void *) 10 },
  1048. { .compatible = "qcom,pm8350b-gpio", .data = (void *) 8 },
  1049. { .compatible = "qcom,pm8350c-gpio", .data = (void *) 9 },
  1050. { .compatible = "qcom,pm8450-gpio", .data = (void *) 4 },
  1051. { .compatible = "qcom,pm8550-gpio", .data = (void *) 12 },
  1052. { .compatible = "qcom,pm8550b-gpio", .data = (void *) 12 },
  1053. { .compatible = "qcom,pm8550ve-gpio", .data = (void *) 8 },
  1054. { .compatible = "qcom,pm8550vs-gpio", .data = (void *) 6 },
  1055. { .compatible = "qcom,pm8916-gpio", .data = (void *) 4 },
  1056. /* pm8937 has 8 GPIOs with holes on 3, 4 and 6 */
  1057. { .compatible = "qcom,pm8937-gpio", .data = (void *) 8 },
  1058. { .compatible = "qcom,pm8941-gpio", .data = (void *) 36 },
  1059. /* pm8950 has 8 GPIOs with holes on 3 */
  1060. { .compatible = "qcom,pm8950-gpio", .data = (void *) 8 },
  1061. /* pm8953 has 8 GPIOs with holes on 3 and 6 */
  1062. { .compatible = "qcom,pm8953-gpio", .data = (void *) 8 },
  1063. { .compatible = "qcom,pm8994-gpio", .data = (void *) 22 },
  1064. { .compatible = "qcom,pm8998-gpio", .data = (void *) 26 },
  1065. { .compatible = "qcom,pma8084-gpio", .data = (void *) 22 },
  1066. { .compatible = "qcom,pmc8380-gpio", .data = (void *) 10 },
  1067. { .compatible = "qcom,pmd8028-gpio", .data = (void *) 4 },
  1068. { .compatible = "qcom,pmi632-gpio", .data = (void *) 8 },
  1069. { .compatible = "qcom,pmi8950-gpio", .data = (void *) 2 },
  1070. { .compatible = "qcom,pmi8994-gpio", .data = (void *) 10 },
  1071. { .compatible = "qcom,pmi8998-gpio", .data = (void *) 14 },
  1072. { .compatible = "qcom,pmih0108-gpio", .data = (void *) 18 },
  1073. { .compatible = "qcom,pmk8350-gpio", .data = (void *) 4 },
  1074. { .compatible = "qcom,pmk8550-gpio", .data = (void *) 6 },
  1075. { .compatible = "qcom,pmm8155au-gpio", .data = (void *) 10 },
  1076. { .compatible = "qcom,pmm8654au-gpio", .data = (void *) 12 },
  1077. /* pmp8074 has 12 GPIOs with holes on 1 and 12 */
  1078. { .compatible = "qcom,pmp8074-gpio", .data = (void *) 12 },
  1079. { .compatible = "qcom,pmr735a-gpio", .data = (void *) 4 },
  1080. { .compatible = "qcom,pmr735b-gpio", .data = (void *) 4 },
  1081. { .compatible = "qcom,pmr735d-gpio", .data = (void *) 2 },
  1082. /* pms405 has 12 GPIOs with holes on 1, 9, and 10 */
  1083. { .compatible = "qcom,pms405-gpio", .data = (void *) 12 },
  1084. /* pmx55 has 11 GPIOs with holes on 3, 7, 10, 11 */
  1085. { .compatible = "qcom,pmx55-gpio", .data = (void *) 11 },
  1086. { .compatible = "qcom,pmx65-gpio", .data = (void *) 16 },
  1087. { .compatible = "qcom,pmx75-gpio", .data = (void *) 16 },
  1088. { .compatible = "qcom,pmxr2230-gpio", .data = (void *) 12 },
  1089. { },
  1090. };
  1091. MODULE_DEVICE_TABLE(of, pmic_gpio_of_match);
  1092. static struct platform_driver pmic_gpio_driver = {
  1093. .driver = {
  1094. .name = "qcom-spmi-gpio",
  1095. .of_match_table = pmic_gpio_of_match,
  1096. },
  1097. .probe = pmic_gpio_probe,
  1098. .remove_new = pmic_gpio_remove,
  1099. };
  1100. module_platform_driver(pmic_gpio_driver);
  1101. MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
  1102. MODULE_DESCRIPTION("Qualcomm SPMI PMIC GPIO pin control driver");
  1103. MODULE_ALIAS("platform:qcom-spmi-gpio");
  1104. MODULE_LICENSE("GPL v2");