pinctrl-lpi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/gpio.h>
  7. #include <linux/io.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/pinctrl/pinconf-generic.h>
  11. #include <linux/pinctrl/pinconf.h>
  12. #include <linux/pinctrl/pinmux.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <linux/ratelimit.h>
  17. #include <linux/clk.h>
  18. #include <linux/bitops.h>
  19. #include <linux/delay.h>
  20. #include <soc/snd_event.h>
  21. #include <dsp/digital-cdc-rsc-mgr.h>
  22. #include <linux/pm_runtime.h>
  23. #include <dsp/audio_notifier.h>
  24. #include "core.h"
  25. #include "pinctrl-utils.h"
  26. #define LPI_AUTO_SUSPEND_DELAY 100 /* delay in msec */
  27. #define LPI_AUTO_SUSPEND_DELAY_ERROR 1 /* delay in msec */
  28. #define LPI_ADDRESS_SIZE 0x20000
  29. #define LPI_SLEW_ADDRESS_SIZE 0x1000
  30. #define LPI_GPIO_REG_VAL_CTL 0x00
  31. #define LPI_GPIO_REG_DIR_CTL 0x04
  32. #define LPI_SLEW_REG_VAL_CTL 0x00
  33. #define LPI_SLEW_RATE_MAX 0x03
  34. #define LPI_SLEW_BITS_SIZE 0x02
  35. #define LPI_SLEW_OFFSET_INVALID 0xFFFFFFFF
  36. #define LPI_GPIO_REG_PULL_SHIFT 0x0
  37. #define LPI_GPIO_REG_PULL_MASK 0x3
  38. #define LPI_GPIO_REG_FUNCTION_SHIFT 0x2
  39. #define LPI_GPIO_REG_FUNCTION_MASK 0x3C
  40. #define LPI_GPIO_REG_OUT_STRENGTH_SHIFT 0x6
  41. #define LPI_GPIO_REG_OUT_STRENGTH_MASK 0x1C0
  42. #define LPI_GPIO_REG_OE_SHIFT 0x9
  43. #define LPI_GPIO_REG_OE_MASK 0x200
  44. #define LPI_GPIO_REG_DIR_SHIFT 0x1
  45. #define LPI_GPIO_REG_DIR_MASK 0x2
  46. #define LPI_GPIO_BIAS_DISABLE 0x0
  47. #define LPI_GPIO_PULL_DOWN 0x1
  48. #define LPI_GPIO_KEEPER 0x2
  49. #define LPI_GPIO_PULL_UP 0x3
  50. #define LPI_GPIO_FUNC_GPIO "gpio"
  51. #define LPI_GPIO_FUNC_FUNC1 "func1"
  52. #define LPI_GPIO_FUNC_FUNC2 "func2"
  53. #define LPI_GPIO_FUNC_FUNC3 "func3"
  54. #define LPI_GPIO_FUNC_FUNC4 "func4"
  55. #define LPI_GPIO_FUNC_FUNC5 "func5"
  56. static bool lpi_dev_up;
  57. static struct device *lpi_dev;
  58. static bool initial_boot = false;
  59. /* The index of each function in lpi_gpio_functions[] array */
  60. enum lpi_gpio_func_index {
  61. LPI_GPIO_FUNC_INDEX_GPIO = 0x00,
  62. LPI_GPIO_FUNC_INDEX_FUNC1 = 0x01,
  63. LPI_GPIO_FUNC_INDEX_FUNC2 = 0x02,
  64. LPI_GPIO_FUNC_INDEX_FUNC3 = 0x03,
  65. LPI_GPIO_FUNC_INDEX_FUNC4 = 0x04,
  66. LPI_GPIO_FUNC_INDEX_FUNC5 = 0x05,
  67. };
  68. /**
  69. * struct lpi_gpio_pad - keep current GPIO settings
  70. * @offset: stores one of gpio_offset or slew_offset at a given time.
  71. * @gpio_offset: Nth GPIO in supported GPIOs.
  72. * @slew_offset: Nth GPIO's position in slew register in supported GPIOs.
  73. * @output_enabled: Set to true if GPIO output logic is enabled.
  74. * @value: value of a pin
  75. * @base: stores one of gpio_base or slew_base at a given time.
  76. * @gpio_base: Address base of LPI GPIO PAD.
  77. * @slew_base: Address base of LPI SLEW PAD.
  78. * @lpi_slew_reg: Address for lpi slew reg.
  79. * @pullup: Constant current which flow through GPIO output buffer.
  80. * @strength: No, Low, Medium, High
  81. * @function: See lpi_gpio_functions[]
  82. */
  83. struct lpi_gpio_pad {
  84. u32 offset;
  85. u32 gpio_offset;
  86. u32 slew_offset;
  87. bool output_enabled;
  88. bool value;
  89. char __iomem *base;
  90. char __iomem *gpio_base;
  91. char __iomem *slew_base;
  92. char __iomem *lpi_slew_reg;
  93. unsigned int pullup;
  94. unsigned int strength;
  95. unsigned int function;
  96. };
  97. struct lpi_gpio_state {
  98. struct device *dev;
  99. struct pinctrl_dev *ctrl;
  100. struct gpio_chip chip;
  101. char __iomem *base;
  102. struct clk *lpass_core_hw_vote;
  103. struct clk *lpass_audio_hw_vote;
  104. struct mutex slew_access_lock;
  105. bool core_hw_vote_status;
  106. struct mutex core_hw_vote_lock;
  107. };
  108. static const char *const lpi_gpio_groups[] = {
  109. "gpio0", "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7",
  110. "gpio8", "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14",
  111. "gpio15", "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21",
  112. "gpio22", "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28",
  113. "gpio29", "gpio30", "gpio31",
  114. };
  115. #define LPI_TLMM_MAX_PINS 100
  116. static u32 lpi_offset[LPI_TLMM_MAX_PINS];
  117. static u32 lpi_slew_offset[LPI_TLMM_MAX_PINS];
  118. static u32 lpi_slew_base[LPI_TLMM_MAX_PINS];
  119. static const char *const lpi_gpio_functions[] = {
  120. [LPI_GPIO_FUNC_INDEX_GPIO] = LPI_GPIO_FUNC_GPIO,
  121. [LPI_GPIO_FUNC_INDEX_FUNC1] = LPI_GPIO_FUNC_FUNC1,
  122. [LPI_GPIO_FUNC_INDEX_FUNC2] = LPI_GPIO_FUNC_FUNC2,
  123. [LPI_GPIO_FUNC_INDEX_FUNC3] = LPI_GPIO_FUNC_FUNC3,
  124. [LPI_GPIO_FUNC_INDEX_FUNC4] = LPI_GPIO_FUNC_FUNC4,
  125. [LPI_GPIO_FUNC_INDEX_FUNC5] = LPI_GPIO_FUNC_FUNC5,
  126. };
  127. int lpi_pinctrl_runtime_suspend(struct device *dev);
  128. static int lpi_gpio_read(struct lpi_gpio_pad *pad, unsigned int addr)
  129. {
  130. int ret = 0;
  131. struct lpi_gpio_state *state = NULL;
  132. static DEFINE_RATELIMIT_STATE(rtl, 1 * HZ, 1);
  133. if (!lpi_dev) {
  134. if (__ratelimit(&rtl))
  135. pr_err_ratelimited("%s: lpi_dev is NULL, return\n",
  136. __func__);
  137. return -EINVAL;
  138. }
  139. state = dev_get_drvdata(lpi_dev);
  140. if (!lpi_dev_up) {
  141. if (__ratelimit(&rtl))
  142. pr_err_ratelimited("%s: ADSP is down due to SSR, return\n",
  143. __func__);
  144. return 0;
  145. }
  146. pm_runtime_get_sync(lpi_dev);
  147. mutex_lock(&state->core_hw_vote_lock);
  148. if (!state->core_hw_vote_status) {
  149. if (__ratelimit(&rtl))
  150. pr_err_ratelimited("%s: core hw vote clk is not enabled\n",
  151. __func__);
  152. ret = -EINVAL;
  153. goto err;
  154. }
  155. ret = ioread32(pad->base + pad->offset + addr);
  156. if (ret < 0)
  157. pr_err_ratelimited("%s: read 0x%x failed\n", __func__, addr);
  158. err:
  159. mutex_unlock(&state->core_hw_vote_lock);
  160. pm_runtime_mark_last_busy(lpi_dev);
  161. pm_runtime_put_autosuspend(lpi_dev);
  162. return ret;
  163. }
  164. static int lpi_gpio_write(struct lpi_gpio_pad *pad, unsigned int addr,
  165. unsigned int val)
  166. {
  167. struct lpi_gpio_state *state = NULL;
  168. int ret = 0;
  169. static DEFINE_RATELIMIT_STATE(rtl, 1 * HZ, 1);
  170. if (!lpi_dev) {
  171. if (__ratelimit(&rtl))
  172. pr_err_ratelimited("%s: lpi_dev is NULL, return\n",
  173. __func__);
  174. return -EINVAL;
  175. }
  176. state = dev_get_drvdata(lpi_dev);
  177. if (!lpi_dev_up) {
  178. return 0;
  179. }
  180. pm_runtime_get_sync(lpi_dev);
  181. mutex_lock(&state->core_hw_vote_lock);
  182. if (!state->core_hw_vote_status) {
  183. if (__ratelimit(&rtl))
  184. pr_err_ratelimited("%s: core hw vote clk is not enabled\n",
  185. __func__);
  186. ret = -EINVAL;
  187. goto err;
  188. }
  189. iowrite32(val, pad->base + pad->offset + addr);
  190. err:
  191. mutex_unlock(&state->core_hw_vote_lock);
  192. pm_runtime_mark_last_busy(lpi_dev);
  193. pm_runtime_put_autosuspend(lpi_dev);
  194. return ret;
  195. }
  196. static int lpi_gpio_get_groups_count(struct pinctrl_dev *pctldev)
  197. {
  198. /* Every PIN is a group */
  199. return pctldev->desc->npins;
  200. }
  201. static const char *lpi_gpio_get_group_name(struct pinctrl_dev *pctldev,
  202. unsigned int pin)
  203. {
  204. return pctldev->desc->pins[pin].name;
  205. }
  206. static int lpi_gpio_get_group_pins(struct pinctrl_dev *pctldev,
  207. unsigned int pin,
  208. const unsigned int **pins,
  209. unsigned int *num_pins)
  210. {
  211. *pins = &pctldev->desc->pins[pin].number;
  212. *num_pins = 1;
  213. return 0;
  214. }
  215. static const struct pinctrl_ops lpi_gpio_pinctrl_ops = {
  216. .get_groups_count = lpi_gpio_get_groups_count,
  217. .get_group_name = lpi_gpio_get_group_name,
  218. .get_group_pins = lpi_gpio_get_group_pins,
  219. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  220. .dt_free_map = pinctrl_utils_free_map,
  221. };
  222. static int lpi_gpio_get_functions_count(struct pinctrl_dev *pctldev)
  223. {
  224. return ARRAY_SIZE(lpi_gpio_functions);
  225. }
  226. static const char *lpi_gpio_get_function_name(struct pinctrl_dev *pctldev,
  227. unsigned int function)
  228. {
  229. return lpi_gpio_functions[function];
  230. }
  231. static int lpi_gpio_get_function_groups(struct pinctrl_dev *pctldev,
  232. unsigned int function,
  233. const char *const **groups,
  234. unsigned *const num_qgroups)
  235. {
  236. *groups = lpi_gpio_groups;
  237. *num_qgroups = pctldev->desc->npins;
  238. return 0;
  239. }
  240. static int lpi_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
  241. unsigned int pin)
  242. {
  243. struct lpi_gpio_pad *pad;
  244. unsigned int val;
  245. pad = pctldev->desc->pins[pin].drv_data;
  246. if (pad != NULL) {
  247. pad->function = function;
  248. val = lpi_gpio_read(pad, LPI_GPIO_REG_VAL_CTL);
  249. val &= ~(LPI_GPIO_REG_FUNCTION_MASK);
  250. val |= pad->function << LPI_GPIO_REG_FUNCTION_SHIFT;
  251. lpi_gpio_write(pad, LPI_GPIO_REG_VAL_CTL, val);
  252. }
  253. return 0;
  254. }
  255. static const struct pinmux_ops lpi_gpio_pinmux_ops = {
  256. .get_functions_count = lpi_gpio_get_functions_count,
  257. .get_function_name = lpi_gpio_get_function_name,
  258. .get_function_groups = lpi_gpio_get_function_groups,
  259. .set_mux = lpi_gpio_set_mux,
  260. };
  261. static int lpi_config_get(struct pinctrl_dev *pctldev,
  262. unsigned int pin, unsigned long *config)
  263. {
  264. unsigned int param = pinconf_to_config_param(*config);
  265. struct lpi_gpio_pad *pad;
  266. unsigned int arg;
  267. pad = pctldev->desc->pins[pin].drv_data;
  268. switch (param) {
  269. case PIN_CONFIG_BIAS_DISABLE:
  270. arg = pad->pullup = LPI_GPIO_BIAS_DISABLE;
  271. break;
  272. case PIN_CONFIG_BIAS_PULL_DOWN:
  273. arg = pad->pullup == LPI_GPIO_PULL_DOWN;
  274. break;
  275. case PIN_CONFIG_BIAS_BUS_HOLD:
  276. arg = pad->pullup = LPI_GPIO_KEEPER;
  277. break;
  278. case PIN_CONFIG_BIAS_PULL_UP:
  279. arg = pad->pullup == LPI_GPIO_PULL_UP;
  280. break;
  281. case PIN_CONFIG_INPUT_ENABLE:
  282. case PIN_CONFIG_OUTPUT:
  283. arg = pad->output_enabled;
  284. break;
  285. default:
  286. return -EINVAL;
  287. }
  288. *config = pinconf_to_config_packed(param, arg);
  289. return 0;
  290. }
  291. static unsigned int lpi_drive_to_regval(u32 arg)
  292. {
  293. return (arg/2 - 1);
  294. }
  295. static int lpi_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  296. unsigned long *configs, unsigned int nconfs)
  297. {
  298. struct lpi_gpio_pad *pad;
  299. unsigned int param, arg;
  300. int i, ret = 0;
  301. volatile unsigned long val;
  302. struct lpi_gpio_state *state = dev_get_drvdata(pctldev->dev);
  303. pad = pctldev->desc->pins[pin].drv_data;
  304. for (i = 0; i < nconfs; i++) {
  305. param = pinconf_to_config_param(configs[i]);
  306. arg = pinconf_to_config_argument(configs[i]);
  307. dev_dbg(pctldev->dev, "%s: param: %d arg: %d pin: %d\n",
  308. __func__, param, arg, pin);
  309. switch (param) {
  310. case PIN_CONFIG_BIAS_DISABLE:
  311. pad->pullup = LPI_GPIO_BIAS_DISABLE;
  312. break;
  313. case PIN_CONFIG_BIAS_PULL_DOWN:
  314. pad->pullup = LPI_GPIO_PULL_DOWN;
  315. break;
  316. case PIN_CONFIG_BIAS_BUS_HOLD:
  317. pad->pullup = LPI_GPIO_KEEPER;
  318. break;
  319. case PIN_CONFIG_BIAS_PULL_UP:
  320. pad->pullup = LPI_GPIO_PULL_UP;
  321. break;
  322. case PIN_CONFIG_INPUT_ENABLE:
  323. pad->output_enabled = false;
  324. break;
  325. case PIN_CONFIG_OUTPUT:
  326. pad->output_enabled = true;
  327. pad->value = arg;
  328. break;
  329. case PIN_CONFIG_DRIVE_STRENGTH:
  330. pad->strength = arg;
  331. break;
  332. case PIN_CONFIG_SLEW_RATE:
  333. if (pad->slew_base == NULL ||
  334. pad->slew_offset == LPI_SLEW_OFFSET_INVALID) {
  335. dev_dbg(pctldev->dev, "%s: invalid slew settings for pin: %d\n",
  336. __func__, pin);
  337. goto set_gpio;
  338. }
  339. if (arg > LPI_SLEW_RATE_MAX) {
  340. dev_err_ratelimited(pctldev->dev, "%s: invalid slew rate %u for \
  341. pin: %d\n", __func__, arg, pin);
  342. goto set_gpio;
  343. }
  344. pad->base = pad->slew_base;
  345. pad->offset = 0;
  346. mutex_lock(&state->slew_access_lock);
  347. if (pad->lpi_slew_reg != NULL) {
  348. pad->base = pad->lpi_slew_reg;
  349. lpi_gpio_write(pad, LPI_SLEW_REG_VAL_CTL, arg);
  350. pad->base = pad->slew_base;
  351. goto slew_exit;
  352. }
  353. val = lpi_gpio_read(pad, LPI_SLEW_REG_VAL_CTL);
  354. pad->offset = pad->slew_offset;
  355. for (i = 0; i < LPI_SLEW_BITS_SIZE; i++) {
  356. if (arg & 0x01)
  357. set_bit(pad->offset, &val);
  358. else
  359. clear_bit(pad->offset, &val);
  360. pad->offset++;
  361. arg = arg >> 1;
  362. }
  363. pad->offset = 0;
  364. lpi_gpio_write(pad, LPI_SLEW_REG_VAL_CTL, val);
  365. slew_exit:
  366. mutex_unlock(&state->slew_access_lock);
  367. break;
  368. default:
  369. ret = -EINVAL;
  370. goto done;
  371. }
  372. }
  373. set_gpio:
  374. pad->base = pad->gpio_base;
  375. pad->offset = pad->gpio_offset;
  376. val = lpi_gpio_read(pad, LPI_GPIO_REG_VAL_CTL);
  377. val &= ~(LPI_GPIO_REG_PULL_MASK | LPI_GPIO_REG_OUT_STRENGTH_MASK |
  378. LPI_GPIO_REG_OE_MASK);
  379. val |= pad->pullup << LPI_GPIO_REG_PULL_SHIFT;
  380. val |= lpi_drive_to_regval(pad->strength) <<
  381. LPI_GPIO_REG_OUT_STRENGTH_SHIFT;
  382. if (pad->output_enabled)
  383. val |= pad->value << LPI_GPIO_REG_OE_SHIFT;
  384. lpi_gpio_write(pad, LPI_GPIO_REG_VAL_CTL, val);
  385. lpi_gpio_write(pad, LPI_GPIO_REG_DIR_CTL,
  386. pad->output_enabled << LPI_GPIO_REG_DIR_SHIFT);
  387. done:
  388. return ret;
  389. }
  390. static const struct pinconf_ops lpi_gpio_pinconf_ops = {
  391. .is_generic = true,
  392. .pin_config_group_get = lpi_config_get,
  393. .pin_config_group_set = lpi_config_set,
  394. };
  395. static int lpi_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
  396. {
  397. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  398. unsigned long config;
  399. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
  400. return lpi_config_set(state->ctrl, pin, &config, 1);
  401. }
  402. static int lpi_gpio_direction_output(struct gpio_chip *chip,
  403. unsigned int pin, int val)
  404. {
  405. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  406. unsigned long config;
  407. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
  408. return lpi_config_set(state->ctrl, pin, &config, 1);
  409. }
  410. static int lpi_gpio_get(struct gpio_chip *chip, unsigned int pin)
  411. {
  412. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  413. struct lpi_gpio_pad *pad;
  414. int value;
  415. pad = state->ctrl->desc->pins[pin].drv_data;
  416. value = lpi_gpio_read(pad, LPI_GPIO_REG_VAL_CTL);
  417. return value;
  418. }
  419. static void lpi_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
  420. {
  421. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  422. unsigned long config;
  423. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
  424. lpi_config_set(state->ctrl, pin, &config, 1);
  425. }
  426. static int lpi_notifier_service_cb(struct notifier_block *this,
  427. unsigned long opcode, void *ptr)
  428. {
  429. pr_debug("%s: Service opcode 0x%lx\n", __func__, opcode);
  430. switch (opcode) {
  431. case AUDIO_NOTIFIER_SERVICE_DOWN:
  432. if (initial_boot) {
  433. initial_boot = false;
  434. break;
  435. }
  436. lpi_dev_up = false;
  437. break;
  438. case AUDIO_NOTIFIER_SERVICE_UP:
  439. if (initial_boot) {
  440. initial_boot = false;
  441. lpi_dev_up = true;
  442. snd_event_notify(lpi_dev, SND_EVENT_UP);
  443. }
  444. break;
  445. default:
  446. break;
  447. }
  448. return NOTIFY_OK;
  449. }
  450. int lpi_pinctrl_suspend(struct device *dev)
  451. {
  452. int ret = 0;
  453. trace_printk("%s: system suspend\n", __func__);
  454. dev_dbg(dev, "%s: system suspend\n", __func__);
  455. if ((!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev))) {
  456. ret = lpi_pinctrl_runtime_suspend(dev);
  457. if (!ret) {
  458. /*
  459. * Synchronize runtime-pm and system-pm states:
  460. * At this point, we are already suspended. If
  461. * runtime-pm still thinks its active, then
  462. * make sure its status is in sync with HW
  463. * status. The three below calls let the
  464. * runtime-pm know that we are suspended
  465. * already without re-invoking the suspend
  466. * callback
  467. */
  468. pm_runtime_disable(dev);
  469. pm_runtime_set_suspended(dev);
  470. pm_runtime_enable(dev);
  471. }
  472. }
  473. return ret;
  474. }
  475. int lpi_pinctrl_resume(struct device *dev)
  476. {
  477. return 0;
  478. }
  479. static struct notifier_block service_nb = {
  480. .notifier_call = lpi_notifier_service_cb,
  481. .priority = -INT_MAX,
  482. };
  483. static void lpi_pinctrl_ssr_disable(struct device *dev, void *data)
  484. {
  485. trace_printk("%s: enter\n", __func__);
  486. lpi_dev_up = false;
  487. lpi_pinctrl_suspend(dev);
  488. }
  489. static int lpi_pinctrl_ssr_enable(struct device *dev, void *data)
  490. {
  491. struct lpi_gpio_state *state = NULL;
  492. dev_dbg(dev, "%s: enter\n", __func__);
  493. if (!lpi_dev) {
  494. dev_err(dev, "%s: lpi_dev is NULL, return\n", __func__);
  495. return -EINVAL;
  496. }
  497. state = dev_get_drvdata(lpi_dev);
  498. if (!initial_boot) {
  499. trace_printk("%s: enter\n", __func__);
  500. if (!lpi_dev_up) {
  501. msleep(100);
  502. if (state->lpass_core_hw_vote)
  503. digital_cdc_rsc_mgr_hw_vote_reset(
  504. state->lpass_core_hw_vote);
  505. if (state->lpass_audio_hw_vote)
  506. digital_cdc_rsc_mgr_hw_vote_reset(
  507. state->lpass_audio_hw_vote);
  508. }
  509. lpi_dev_up = true;
  510. }
  511. dev_dbg(dev, "%s: leave\n", __func__);
  512. return 0;
  513. }
  514. static const struct snd_event_ops lpi_pinctrl_ssr_ops = {
  515. .disable = lpi_pinctrl_ssr_disable,
  516. .enable = lpi_pinctrl_ssr_enable,
  517. };
  518. #ifdef CONFIG_DEBUG_FS
  519. #include <linux/seq_file.h>
  520. static unsigned int lpi_regval_to_drive(u32 val)
  521. {
  522. return (val + 1) * 2;
  523. }
  524. static void lpi_gpio_dbg_show_one(struct seq_file *s,
  525. struct pinctrl_dev *pctldev,
  526. struct gpio_chip *chip,
  527. unsigned int offset,
  528. unsigned int gpio)
  529. {
  530. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  531. struct pinctrl_pin_desc pindesc;
  532. struct lpi_gpio_pad *pad;
  533. unsigned int func;
  534. int is_out;
  535. int drive;
  536. int pull;
  537. u32 ctl_reg;
  538. static const char * const pulls[] = {
  539. "no pull",
  540. "pull down",
  541. "keeper",
  542. "pull up"
  543. };
  544. pctldev = pctldev ? : state->ctrl;
  545. pindesc = pctldev->desc->pins[offset];
  546. pad = pctldev->desc->pins[offset].drv_data;
  547. ctl_reg = lpi_gpio_read(pad, LPI_GPIO_REG_DIR_CTL);
  548. is_out = (ctl_reg & LPI_GPIO_REG_DIR_MASK) >> LPI_GPIO_REG_DIR_SHIFT;
  549. ctl_reg = lpi_gpio_read(pad, LPI_GPIO_REG_VAL_CTL);
  550. func = (ctl_reg & LPI_GPIO_REG_FUNCTION_MASK) >>
  551. LPI_GPIO_REG_FUNCTION_SHIFT;
  552. drive = (ctl_reg & LPI_GPIO_REG_OUT_STRENGTH_MASK) >>
  553. LPI_GPIO_REG_OUT_STRENGTH_SHIFT;
  554. pull = (ctl_reg & LPI_GPIO_REG_PULL_MASK) >> LPI_GPIO_REG_PULL_SHIFT;
  555. seq_printf(s, " %-8s: %-3s %d",
  556. pindesc.name, is_out ? "out" : "in", func);
  557. seq_printf(s, " %dmA", lpi_regval_to_drive(drive));
  558. seq_printf(s, " %s", pulls[pull]);
  559. }
  560. static void lpi_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  561. {
  562. unsigned int gpio = chip->base;
  563. unsigned int i;
  564. for (i = 0; i < chip->ngpio; i++, gpio++) {
  565. lpi_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  566. seq_puts(s, "\n");
  567. }
  568. }
  569. #else
  570. #define lpi_gpio_dbg_show NULL
  571. #endif
  572. static const struct gpio_chip lpi_gpio_template = {
  573. .direction_input = lpi_gpio_direction_input,
  574. .direction_output = lpi_gpio_direction_output,
  575. .get = lpi_gpio_get,
  576. .set = lpi_gpio_set,
  577. .request = gpiochip_generic_request,
  578. .free = gpiochip_generic_free,
  579. .dbg_show = lpi_gpio_dbg_show,
  580. };
  581. static int lpi_pinctrl_probe(struct platform_device *pdev)
  582. {
  583. struct device *dev = &pdev->dev;
  584. struct pinctrl_pin_desc *pindesc;
  585. struct pinctrl_desc *pctrldesc;
  586. struct lpi_gpio_pad *pad, *pads;
  587. struct lpi_gpio_state *state;
  588. int ret, npins, i;
  589. char __iomem *lpi_base;
  590. char __iomem *slew_base;
  591. u32 reg, slew_reg;
  592. struct clk *lpass_core_hw_vote = NULL;
  593. struct clk *lpass_audio_hw_vote = NULL;
  594. if (!audio_notifier_probe_status()) {
  595. pr_err("%s: Audio notify probe not completed, defer lpi pinctrl probe\n",
  596. __func__);
  597. return -EPROBE_DEFER;
  598. }
  599. ret = of_property_read_u32(dev->of_node, "reg", &reg);
  600. if (ret < 0) {
  601. dev_err(dev, "missing base address\n");
  602. return ret;
  603. }
  604. ret = of_property_read_u32(dev->of_node, "qcom,gpios-count", &npins);
  605. if (ret < 0)
  606. return ret;
  607. WARN_ON(npins > ARRAY_SIZE(lpi_gpio_groups));
  608. ret = of_property_read_u32_array(dev->of_node, "qcom,lpi-offset-tbl",
  609. lpi_offset, npins);
  610. if (ret < 0) {
  611. dev_err(dev, "error in reading lpi offset table: %d\n", ret);
  612. return ret;
  613. }
  614. ret = of_property_read_u32_array(dev->of_node,
  615. "qcom,lpi-slew-offset-tbl",
  616. lpi_slew_offset, npins);
  617. if (ret < 0) {
  618. for (i = 0; i < npins; i++)
  619. lpi_slew_offset[i] = LPI_SLEW_OFFSET_INVALID;
  620. dev_dbg(dev, "%s: error in reading lpi slew offset table: %d\n",
  621. __func__, ret);
  622. }
  623. ret = of_property_read_u32_array(dev->of_node,
  624. "qcom,lpi-slew-base-tbl",
  625. lpi_slew_base, npins);
  626. if (ret < 0) {
  627. for (i = 0; i < npins; i++)
  628. lpi_slew_base[i] = LPI_SLEW_OFFSET_INVALID;
  629. dev_dbg(dev, "%s: error in reading lpi slew table: %d\n",
  630. __func__, ret);
  631. }
  632. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  633. if (!state)
  634. return -ENOMEM;
  635. platform_set_drvdata(pdev, state);
  636. state->dev = &pdev->dev;
  637. slew_reg = 0;
  638. ret = of_property_read_u32(dev->of_node, "qcom,slew-reg", &slew_reg);
  639. if (!ret) {
  640. slew_base = devm_ioremap(dev, slew_reg, LPI_SLEW_ADDRESS_SIZE);
  641. if (slew_base == NULL) {
  642. dev_err(dev,
  643. "%s devm_ioremap failed for slew rate reg\n",
  644. __func__);
  645. ret = -ENOMEM;
  646. goto err_io;
  647. }
  648. } else {
  649. slew_base = NULL;
  650. dev_dbg(dev, "error in reading lpi slew register: %d\n",
  651. ret);
  652. }
  653. pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
  654. if (!pindesc)
  655. return -ENOMEM;
  656. pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
  657. if (!pads)
  658. return -ENOMEM;
  659. pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
  660. if (!pctrldesc)
  661. return -ENOMEM;
  662. pctrldesc->pctlops = &lpi_gpio_pinctrl_ops;
  663. pctrldesc->pmxops = &lpi_gpio_pinmux_ops;
  664. pctrldesc->confops = &lpi_gpio_pinconf_ops;
  665. pctrldesc->owner = THIS_MODULE;
  666. pctrldesc->name = dev_name(dev);
  667. pctrldesc->pins = pindesc;
  668. pctrldesc->npins = npins;
  669. lpi_base = devm_ioremap(dev, reg, LPI_ADDRESS_SIZE);
  670. if (lpi_base == NULL) {
  671. dev_err(dev, "%s devm_ioremap failed\n", __func__);
  672. return -ENOMEM;
  673. }
  674. state->base = lpi_base;
  675. for (i = 0; i < npins; i++, pindesc++) {
  676. pad = &pads[i];
  677. pindesc->drv_data = pad;
  678. pindesc->number = i;
  679. pindesc->name = lpi_gpio_groups[i];
  680. pad->gpio_base = lpi_base;
  681. pad->slew_base = slew_base;
  682. pad->base = pad->gpio_base;
  683. pad->gpio_offset = lpi_offset[i];
  684. pad->slew_offset = lpi_slew_offset[i];
  685. pad->offset = pad->gpio_offset;
  686. pad->lpi_slew_reg = NULL;
  687. if ((lpi_slew_base[i] != LPI_SLEW_OFFSET_INVALID) &&
  688. lpi_slew_base[i])
  689. pad->lpi_slew_reg = devm_ioremap(dev,
  690. lpi_slew_base[i], 0x4);
  691. }
  692. state->chip = lpi_gpio_template;
  693. state->chip.parent = dev;
  694. state->chip.base = -1;
  695. state->chip.ngpio = npins;
  696. state->chip.label = dev_name(dev);
  697. state->chip.of_gpio_n_cells = 2;
  698. state->chip.can_sleep = false;
  699. mutex_init(&state->slew_access_lock);
  700. mutex_init(&state->core_hw_vote_lock);
  701. state->ctrl = devm_pinctrl_register(dev, pctrldesc, state);
  702. if (IS_ERR(state->ctrl))
  703. return PTR_ERR(state->ctrl);
  704. ret = gpiochip_add_data(&state->chip, state);
  705. if (ret) {
  706. dev_err(state->dev, "can't add gpio chip\n");
  707. goto err_chip;
  708. }
  709. ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0, npins);
  710. if (ret) {
  711. dev_err(dev, "failed to add pin range\n");
  712. goto err_range;
  713. }
  714. lpi_dev = &pdev->dev;
  715. lpi_dev_up = true;
  716. initial_boot = true;
  717. ret = snd_event_client_register(dev, &lpi_pinctrl_ssr_ops, NULL);
  718. if (!ret) {
  719. snd_event_notify(dev, SND_EVENT_UP);
  720. } else {
  721. dev_err(dev, "%s: snd_event registration failed, ret [%d]\n",
  722. __func__, ret);
  723. goto err_range;
  724. }
  725. ret = audio_notifier_register("lpi_tlmm", AUDIO_NOTIFIER_ADSP_DOMAIN,
  726. &service_nb);
  727. if (ret < 0) {
  728. pr_err("%s: Audio notifier register failed ret = %d\n",
  729. __func__, ret);
  730. goto err_range;
  731. }
  732. /* Register LPASS core hw vote */
  733. lpass_core_hw_vote = devm_clk_get(&pdev->dev, "lpass_core_hw_vote");
  734. if (IS_ERR(lpass_core_hw_vote)) {
  735. ret = PTR_ERR(lpass_core_hw_vote);
  736. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  737. __func__, "lpass_core_hw_vote", ret);
  738. lpass_core_hw_vote = NULL;
  739. ret = 0;
  740. }
  741. state->lpass_core_hw_vote = lpass_core_hw_vote;
  742. /* Register LPASS audio hw vote */
  743. lpass_audio_hw_vote = devm_clk_get(&pdev->dev, "lpass_audio_hw_vote");
  744. if (IS_ERR(lpass_audio_hw_vote)) {
  745. ret = PTR_ERR(lpass_audio_hw_vote);
  746. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  747. __func__, "lpass_audio_hw_vote", ret);
  748. lpass_audio_hw_vote = NULL;
  749. ret = 0;
  750. }
  751. state->lpass_audio_hw_vote = lpass_audio_hw_vote;
  752. state->core_hw_vote_status = false;
  753. pm_runtime_set_autosuspend_delay(&pdev->dev, LPI_AUTO_SUSPEND_DELAY);
  754. pm_runtime_use_autosuspend(&pdev->dev);
  755. pm_runtime_set_suspended(&pdev->dev);
  756. pm_runtime_enable(&pdev->dev);
  757. return 0;
  758. //err_snd_evt:
  759. // audio_notifier_deregister("lpi_tlmm");
  760. err_range:
  761. gpiochip_remove(&state->chip);
  762. err_chip:
  763. mutex_destroy(&state->core_hw_vote_lock);
  764. mutex_destroy(&state->slew_access_lock);
  765. err_io:
  766. return ret;
  767. }
  768. static int lpi_pinctrl_remove(struct platform_device *pdev)
  769. {
  770. struct lpi_gpio_state *state = platform_get_drvdata(pdev);
  771. pm_runtime_disable(&pdev->dev);
  772. pm_runtime_set_suspended(&pdev->dev);
  773. snd_event_client_deregister(&pdev->dev);
  774. audio_notifier_deregister("lpi_tlmm");
  775. gpiochip_remove(&state->chip);
  776. mutex_destroy(&state->core_hw_vote_lock);
  777. mutex_destroy(&state->slew_access_lock);
  778. return 0;
  779. }
  780. static const struct of_device_id lpi_pinctrl_of_match[] = {
  781. { .compatible = "qcom,lpi-pinctrl" }, /* Generic */
  782. { },
  783. };
  784. MODULE_DEVICE_TABLE(of, lpi_pinctrl_of_match);
  785. int lpi_pinctrl_runtime_resume(struct device *dev)
  786. {
  787. struct lpi_gpio_state *state = dev_get_drvdata(dev);
  788. int ret = 0;
  789. struct clk *hw_vote = state->lpass_core_hw_vote;
  790. trace_printk("%s: enter\n", __func__);
  791. if (state->lpass_core_hw_vote == NULL) {
  792. dev_dbg(dev, "%s: Invalid core hw node\n", __func__);
  793. if (state->lpass_audio_hw_vote == NULL) {
  794. dev_dbg(dev, "%s: Invalid audio hw node\n", __func__);
  795. return 0;
  796. }
  797. hw_vote = state->lpass_audio_hw_vote;
  798. }
  799. mutex_lock(&state->core_hw_vote_lock);
  800. ret = digital_cdc_rsc_mgr_hw_vote_enable(hw_vote, dev);
  801. if (ret < 0) {
  802. pm_runtime_set_autosuspend_delay(dev,
  803. LPI_AUTO_SUSPEND_DELAY_ERROR);
  804. dev_err_ratelimited(dev, "%s:lpass core hw island enable failed\n",
  805. __func__);
  806. goto exit;
  807. } else {
  808. state->core_hw_vote_status = true;
  809. }
  810. pm_runtime_set_autosuspend_delay(dev, LPI_AUTO_SUSPEND_DELAY);
  811. exit:
  812. mutex_unlock(&state->core_hw_vote_lock);
  813. trace_printk("%s: exit\n", __func__);
  814. return 0;
  815. }
  816. int lpi_pinctrl_runtime_suspend(struct device *dev)
  817. {
  818. struct lpi_gpio_state *state = dev_get_drvdata(dev);
  819. struct clk *hw_vote = state->lpass_core_hw_vote;
  820. trace_printk("%s: enter\n", __func__);
  821. if (state->lpass_core_hw_vote == NULL) {
  822. dev_dbg(dev, "%s: Invalid core hw node\n", __func__);
  823. if (state->lpass_audio_hw_vote == NULL) {
  824. dev_dbg(dev, "%s: Invalid audio hw node\n", __func__);
  825. return 0;
  826. }
  827. hw_vote = state->lpass_audio_hw_vote;
  828. }
  829. mutex_lock(&state->core_hw_vote_lock);
  830. if (state->core_hw_vote_status) {
  831. digital_cdc_rsc_mgr_hw_vote_disable(hw_vote, dev);
  832. state->core_hw_vote_status = false;
  833. }
  834. mutex_unlock(&state->core_hw_vote_lock);
  835. trace_printk("%s: exit\n", __func__);
  836. return 0;
  837. }
  838. static const struct dev_pm_ops lpi_pinctrl_dev_pm_ops = {
  839. SET_SYSTEM_SLEEP_PM_OPS(
  840. lpi_pinctrl_suspend,
  841. lpi_pinctrl_resume
  842. )
  843. SET_RUNTIME_PM_OPS(
  844. lpi_pinctrl_runtime_suspend,
  845. lpi_pinctrl_runtime_resume,
  846. NULL
  847. )
  848. };
  849. static struct platform_driver lpi_pinctrl_driver = {
  850. .driver = {
  851. .name = "qcom-lpi-pinctrl",
  852. .pm = &lpi_pinctrl_dev_pm_ops,
  853. .of_match_table = lpi_pinctrl_of_match,
  854. .suppress_bind_attrs = true,
  855. },
  856. .probe = lpi_pinctrl_probe,
  857. .remove = lpi_pinctrl_remove,
  858. };
  859. module_platform_driver(lpi_pinctrl_driver);
  860. MODULE_DESCRIPTION("QTI LPI GPIO pin control driver");
  861. MODULE_LICENSE("GPL v2");