pinctrl-lpi.c 25 KB

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