pinctrl-spmi-gpio.c 39 KB

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