pinctrl-lpi.c 25 KB

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