pinctrl-lpi.c 26 KB

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