pinctrl-lpi.c 27 KB

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