qcom_pmic_gpio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Qualcomm generic pmic gpio driver
  4. *
  5. * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <power/pmic.h>
  11. #include <spmi/spmi.h>
  12. #include <asm/io.h>
  13. #include <asm/gpio.h>
  14. #include <linux/bitops.h>
  15. /* Register offset for each gpio */
  16. #define REG_OFFSET(x) ((x) * 0x100)
  17. /* Register maps */
  18. /* Type and subtype are shared for all PMIC peripherals */
  19. #define REG_TYPE 0x4
  20. #define REG_SUBTYPE 0x5
  21. /* GPIO peripheral type and subtype out_values */
  22. #define REG_TYPE_VAL 0x10
  23. #define REG_SUBTYPE_GPIO_4CH 0x1
  24. #define REG_SUBTYPE_GPIOC_4CH 0x5
  25. #define REG_SUBTYPE_GPIO_8CH 0x9
  26. #define REG_SUBTYPE_GPIOC_8CH 0xd
  27. #define REG_SUBTYPE_GPIO_LV 0x10
  28. #define REG_SUBTYPE_GPIO_MV 0x11
  29. #define REG_STATUS 0x08
  30. #define REG_STATUS_VAL_MASK 0x1
  31. /* MODE_CTL */
  32. #define REG_CTL 0x40
  33. #define REG_CTL_MODE_MASK 0x70
  34. #define REG_CTL_MODE_INPUT 0x00
  35. #define REG_CTL_MODE_INOUT 0x20
  36. #define REG_CTL_MODE_OUTPUT 0x10
  37. #define REG_CTL_OUTPUT_MASK 0x0F
  38. #define REG_CTL_LV_MV_MODE_MASK 0x3
  39. #define REG_CTL_LV_MV_MODE_INPUT 0x0
  40. #define REG_CTL_LV_MV_MODE_INOUT 0x2
  41. #define REG_CTL_LV_MV_MODE_OUTPUT 0x1
  42. #define REG_DIG_VIN_CTL 0x41
  43. #define REG_DIG_VIN_VIN0 0
  44. #define REG_DIG_PULL_CTL 0x42
  45. #define REG_DIG_PULL_NO_PU 0x5
  46. #define REG_LV_MV_OUTPUT_CTL 0x44
  47. #define REG_LV_MV_OUTPUT_CTL_MASK 0x80
  48. #define REG_LV_MV_OUTPUT_CTL_SHIFT 7
  49. #define REG_DIG_OUT_CTL 0x45
  50. #define REG_DIG_OUT_CTL_CMOS (0x0 << 4)
  51. #define REG_DIG_OUT_CTL_DRIVE_L 0x1
  52. #define REG_EN_CTL 0x46
  53. #define REG_EN_CTL_ENABLE (1 << 7)
  54. struct qcom_gpio_bank {
  55. uint32_t pid; /* Peripheral ID on SPMI bus */
  56. bool lv_mv_type; /* If subtype is GPIO_LV(0x10) or GPIO_MV(0x11) */
  57. };
  58. static int qcom_gpio_set_direction(struct udevice *dev, unsigned offset,
  59. bool input, int value)
  60. {
  61. struct qcom_gpio_bank *priv = dev_get_priv(dev);
  62. uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
  63. uint32_t reg_ctl_val;
  64. int ret;
  65. /* Disable the GPIO */
  66. ret = pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL,
  67. REG_EN_CTL_ENABLE, 0);
  68. if (ret < 0)
  69. return ret;
  70. /* Select the mode and output */
  71. if (priv->lv_mv_type) {
  72. if (input)
  73. reg_ctl_val = REG_CTL_LV_MV_MODE_INPUT;
  74. else
  75. reg_ctl_val = REG_CTL_LV_MV_MODE_INOUT;
  76. } else {
  77. if (input)
  78. reg_ctl_val = REG_CTL_MODE_INPUT;
  79. else
  80. reg_ctl_val = REG_CTL_MODE_INOUT | !!value;
  81. }
  82. ret = pmic_reg_write(dev->parent, gpio_base + REG_CTL, reg_ctl_val);
  83. if (ret < 0)
  84. return ret;
  85. if (priv->lv_mv_type && !input) {
  86. ret = pmic_reg_write(dev->parent,
  87. gpio_base + REG_LV_MV_OUTPUT_CTL,
  88. !!value << REG_LV_MV_OUTPUT_CTL_SHIFT);
  89. if (ret < 0)
  90. return ret;
  91. }
  92. /* Set the right pull (no pull) */
  93. ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_PULL_CTL,
  94. REG_DIG_PULL_NO_PU);
  95. if (ret < 0)
  96. return ret;
  97. /* Configure output pin drivers if needed */
  98. if (!input) {
  99. /* Select the VIN - VIN0, pin is input so it doesn't matter */
  100. ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_VIN_CTL,
  101. REG_DIG_VIN_VIN0);
  102. if (ret < 0)
  103. return ret;
  104. /* Set the right dig out control */
  105. ret = pmic_reg_write(dev->parent, gpio_base + REG_DIG_OUT_CTL,
  106. REG_DIG_OUT_CTL_CMOS |
  107. REG_DIG_OUT_CTL_DRIVE_L);
  108. if (ret < 0)
  109. return ret;
  110. }
  111. /* Enable the GPIO */
  112. return pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL, 0,
  113. REG_EN_CTL_ENABLE);
  114. }
  115. static int qcom_gpio_direction_input(struct udevice *dev, unsigned offset)
  116. {
  117. return qcom_gpio_set_direction(dev, offset, true, 0);
  118. }
  119. static int qcom_gpio_direction_output(struct udevice *dev, unsigned offset,
  120. int value)
  121. {
  122. return qcom_gpio_set_direction(dev, offset, false, value);
  123. }
  124. static int qcom_gpio_get_function(struct udevice *dev, unsigned offset)
  125. {
  126. struct qcom_gpio_bank *priv = dev_get_priv(dev);
  127. uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
  128. int reg;
  129. reg = pmic_reg_read(dev->parent, gpio_base + REG_CTL);
  130. if (reg < 0)
  131. return reg;
  132. if (priv->lv_mv_type) {
  133. switch (reg & REG_CTL_LV_MV_MODE_MASK) {
  134. case REG_CTL_LV_MV_MODE_INPUT:
  135. return GPIOF_INPUT;
  136. case REG_CTL_LV_MV_MODE_INOUT: /* Fallthrough */
  137. case REG_CTL_LV_MV_MODE_OUTPUT:
  138. return GPIOF_OUTPUT;
  139. default:
  140. return GPIOF_UNKNOWN;
  141. }
  142. } else {
  143. switch (reg & REG_CTL_MODE_MASK) {
  144. case REG_CTL_MODE_INPUT:
  145. return GPIOF_INPUT;
  146. case REG_CTL_MODE_INOUT: /* Fallthrough */
  147. case REG_CTL_MODE_OUTPUT:
  148. return GPIOF_OUTPUT;
  149. default:
  150. return GPIOF_UNKNOWN;
  151. }
  152. }
  153. }
  154. static int qcom_gpio_get_value(struct udevice *dev, unsigned offset)
  155. {
  156. struct qcom_gpio_bank *priv = dev_get_priv(dev);
  157. uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
  158. int reg;
  159. reg = pmic_reg_read(dev->parent, gpio_base + REG_STATUS);
  160. if (reg < 0)
  161. return reg;
  162. return !!(reg & REG_STATUS_VAL_MASK);
  163. }
  164. static int qcom_gpio_set_value(struct udevice *dev, unsigned offset,
  165. int value)
  166. {
  167. struct qcom_gpio_bank *priv = dev_get_priv(dev);
  168. uint32_t gpio_base = priv->pid + REG_OFFSET(offset);
  169. /* Set the output value of the gpio */
  170. if (priv->lv_mv_type)
  171. return pmic_clrsetbits(dev->parent,
  172. gpio_base + REG_LV_MV_OUTPUT_CTL,
  173. REG_LV_MV_OUTPUT_CTL_MASK,
  174. !!value << REG_LV_MV_OUTPUT_CTL_SHIFT);
  175. else
  176. return pmic_clrsetbits(dev->parent, gpio_base + REG_CTL,
  177. REG_CTL_OUTPUT_MASK, !!value);
  178. }
  179. static const struct dm_gpio_ops qcom_gpio_ops = {
  180. .direction_input = qcom_gpio_direction_input,
  181. .direction_output = qcom_gpio_direction_output,
  182. .get_value = qcom_gpio_get_value,
  183. .set_value = qcom_gpio_set_value,
  184. .get_function = qcom_gpio_get_function,
  185. };
  186. static int qcom_gpio_probe(struct udevice *dev)
  187. {
  188. struct qcom_gpio_bank *priv = dev_get_priv(dev);
  189. int reg;
  190. priv->pid = dev_read_addr(dev);
  191. if (priv->pid == FDT_ADDR_T_NONE)
  192. return log_msg_ret("bad address", -EINVAL);
  193. /* Do a sanity check */
  194. reg = pmic_reg_read(dev->parent, priv->pid + REG_TYPE);
  195. if (reg != REG_TYPE_VAL)
  196. return log_msg_ret("bad type", -ENXIO);
  197. reg = pmic_reg_read(dev->parent, priv->pid + REG_SUBTYPE);
  198. if (reg != REG_SUBTYPE_GPIO_4CH && reg != REG_SUBTYPE_GPIOC_4CH &&
  199. reg != REG_SUBTYPE_GPIO_LV && reg != REG_SUBTYPE_GPIO_MV)
  200. return log_msg_ret("bad subtype", -ENXIO);
  201. priv->lv_mv_type = reg == REG_SUBTYPE_GPIO_LV ||
  202. reg == REG_SUBTYPE_GPIO_MV;
  203. return 0;
  204. }
  205. static int qcom_gpio_of_to_plat(struct udevice *dev)
  206. {
  207. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  208. uc_priv->gpio_count = dev_read_u32_default(dev, "gpio-count", 0);
  209. uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
  210. if (uc_priv->bank_name == NULL)
  211. uc_priv->bank_name = "qcom_pmic";
  212. return 0;
  213. }
  214. static const struct udevice_id qcom_gpio_ids[] = {
  215. { .compatible = "qcom,pm8916-gpio" },
  216. { .compatible = "qcom,pm8994-gpio" }, /* 22 GPIO's */
  217. { .compatible = "qcom,pm8998-gpio" },
  218. { .compatible = "qcom,pms405-gpio" },
  219. { }
  220. };
  221. U_BOOT_DRIVER(qcom_pmic_gpio) = {
  222. .name = "qcom_pmic_gpio",
  223. .id = UCLASS_GPIO,
  224. .of_match = qcom_gpio_ids,
  225. .of_to_plat = qcom_gpio_of_to_plat,
  226. .probe = qcom_gpio_probe,
  227. .ops = &qcom_gpio_ops,
  228. .priv_auto = sizeof(struct qcom_gpio_bank),
  229. };
  230. /* Add pmic buttons as GPIO as well - there is no generic way for now */
  231. #define PON_INT_RT_STS 0x10
  232. #define KPDPWR_ON_INT_BIT 0
  233. #define RESIN_ON_INT_BIT 1
  234. static int qcom_pwrkey_get_function(struct udevice *dev, unsigned offset)
  235. {
  236. return GPIOF_INPUT;
  237. }
  238. static int qcom_pwrkey_get_value(struct udevice *dev, unsigned offset)
  239. {
  240. struct qcom_gpio_bank *priv = dev_get_priv(dev);
  241. int reg = pmic_reg_read(dev->parent, priv->pid + PON_INT_RT_STS);
  242. if (reg < 0)
  243. return 0;
  244. switch (offset) {
  245. case 0: /* Power button */
  246. return (reg & BIT(KPDPWR_ON_INT_BIT)) != 0;
  247. break;
  248. case 1: /* Reset button */
  249. default:
  250. return (reg & BIT(RESIN_ON_INT_BIT)) != 0;
  251. break;
  252. }
  253. }
  254. /*
  255. * Since pmic buttons modelled as GPIO, we need empty direction functions
  256. * to trick u-boot button driver
  257. */
  258. static int qcom_pwrkey_direction_input(struct udevice *dev, unsigned int offset)
  259. {
  260. return 0;
  261. }
  262. static int qcom_pwrkey_direction_output(struct udevice *dev, unsigned int offset, int value)
  263. {
  264. return -EOPNOTSUPP;
  265. }
  266. static const struct dm_gpio_ops qcom_pwrkey_ops = {
  267. .get_value = qcom_pwrkey_get_value,
  268. .get_function = qcom_pwrkey_get_function,
  269. .direction_input = qcom_pwrkey_direction_input,
  270. .direction_output = qcom_pwrkey_direction_output,
  271. };
  272. static int qcom_pwrkey_probe(struct udevice *dev)
  273. {
  274. struct qcom_gpio_bank *priv = dev_get_priv(dev);
  275. int reg;
  276. priv->pid = dev_read_addr(dev);
  277. if (priv->pid == FDT_ADDR_T_NONE)
  278. return log_msg_ret("bad address", -EINVAL);
  279. /* Do a sanity check */
  280. reg = pmic_reg_read(dev->parent, priv->pid + REG_TYPE);
  281. if (reg != 0x1)
  282. return log_msg_ret("bad type", -ENXIO);
  283. reg = pmic_reg_read(dev->parent, priv->pid + REG_SUBTYPE);
  284. if ((reg & 0x5) == 0)
  285. return log_msg_ret("bad subtype", -ENXIO);
  286. return 0;
  287. }
  288. static int qcom_pwrkey_of_to_plat(struct udevice *dev)
  289. {
  290. struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
  291. uc_priv->gpio_count = 2;
  292. uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
  293. if (uc_priv->bank_name == NULL)
  294. uc_priv->bank_name = "pwkey_qcom";
  295. return 0;
  296. }
  297. static const struct udevice_id qcom_pwrkey_ids[] = {
  298. { .compatible = "qcom,pm8916-pwrkey" },
  299. { .compatible = "qcom,pm8994-pwrkey" },
  300. { .compatible = "qcom,pm8998-pwrkey" },
  301. { }
  302. };
  303. U_BOOT_DRIVER(pwrkey_qcom) = {
  304. .name = "pwrkey_qcom",
  305. .id = UCLASS_GPIO,
  306. .of_match = qcom_pwrkey_ids,
  307. .of_to_plat = qcom_pwrkey_of_to_plat,
  308. .probe = qcom_pwrkey_probe,
  309. .ops = &qcom_pwrkey_ops,
  310. .priv_auto = sizeof(struct qcom_gpio_bank),
  311. };