pinctrl-lpi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2018, 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 <soc/snd_event.h>
  17. #include <linux/pm_runtime.h>
  18. #include <dsp/audio_notifier.h>
  19. #include "core.h"
  20. #include "pinctrl-utils.h"
  21. #define LPI_AUTO_SUSPEND_DELAY 100 /* delay in msec */
  22. #define LPI_ADDRESS_SIZE 0x20000
  23. #define LPI_GPIO_REG_VAL_CTL 0x00
  24. #define LPI_GPIO_REG_DIR_CTL 0x04
  25. #define LPI_GPIO_REG_PULL_SHIFT 0x0
  26. #define LPI_GPIO_REG_PULL_MASK 0x3
  27. #define LPI_GPIO_REG_FUNCTION_SHIFT 0x2
  28. #define LPI_GPIO_REG_FUNCTION_MASK 0x3C
  29. #define LPI_GPIO_REG_OUT_STRENGTH_SHIFT 0x6
  30. #define LPI_GPIO_REG_OUT_STRENGTH_MASK 0x1C0
  31. #define LPI_GPIO_REG_OE_SHIFT 0x9
  32. #define LPI_GPIO_REG_OE_MASK 0x200
  33. #define LPI_GPIO_REG_DIR_SHIFT 0x1
  34. #define LPI_GPIO_REG_DIR_MASK 0x2
  35. #define LPI_GPIO_BIAS_DISABLE 0x0
  36. #define LPI_GPIO_PULL_DOWN 0x1
  37. #define LPI_GPIO_KEEPER 0x2
  38. #define LPI_GPIO_PULL_UP 0x3
  39. #define LPI_GPIO_FUNC_GPIO "gpio"
  40. #define LPI_GPIO_FUNC_FUNC1 "func1"
  41. #define LPI_GPIO_FUNC_FUNC2 "func2"
  42. #define LPI_GPIO_FUNC_FUNC3 "func3"
  43. #define LPI_GPIO_FUNC_FUNC4 "func4"
  44. #define LPI_GPIO_FUNC_FUNC5 "func5"
  45. static bool lpi_dev_up;
  46. static struct device *lpi_dev;
  47. /* The index of each function in lpi_gpio_functions[] array */
  48. enum lpi_gpio_func_index {
  49. LPI_GPIO_FUNC_INDEX_GPIO = 0x00,
  50. LPI_GPIO_FUNC_INDEX_FUNC1 = 0x01,
  51. LPI_GPIO_FUNC_INDEX_FUNC2 = 0x02,
  52. LPI_GPIO_FUNC_INDEX_FUNC3 = 0x03,
  53. LPI_GPIO_FUNC_INDEX_FUNC4 = 0x04,
  54. LPI_GPIO_FUNC_INDEX_FUNC5 = 0x05,
  55. };
  56. /**
  57. * struct lpi_gpio_pad - keep current GPIO settings
  58. * @offset: Nth GPIO in supported GPIOs.
  59. * @output_enabled: Set to true if GPIO output logic is enabled.
  60. * @value: value of a pin
  61. * @base: Address base of LPI GPIO PAD.
  62. * @pullup: Constant current which flow through GPIO output buffer.
  63. * @strength: No, Low, Medium, High
  64. * @function: See lpi_gpio_functions[]
  65. */
  66. struct lpi_gpio_pad {
  67. u32 offset;
  68. bool output_enabled;
  69. bool value;
  70. char __iomem *base;
  71. unsigned int pullup;
  72. unsigned int strength;
  73. unsigned int function;
  74. };
  75. struct lpi_gpio_state {
  76. struct device *dev;
  77. struct pinctrl_dev *ctrl;
  78. struct gpio_chip chip;
  79. char __iomem *base;
  80. struct clk *lpass_npa_rsc_island;
  81. };
  82. static const char *const lpi_gpio_groups[] = {
  83. "gpio0", "gpio1", "gpio2", "gpio3", "gpio4", "gpio5", "gpio6", "gpio7",
  84. "gpio8", "gpio9", "gpio10", "gpio11", "gpio12", "gpio13", "gpio14",
  85. "gpio15", "gpio16", "gpio17", "gpio18", "gpio19", "gpio20", "gpio21",
  86. "gpio22", "gpio23", "gpio24", "gpio25", "gpio26", "gpio27", "gpio28",
  87. "gpio29", "gpio30", "gpio31",
  88. };
  89. #define LPI_TLMM_MAX_PINS 100
  90. static u32 lpi_offset[LPI_TLMM_MAX_PINS];
  91. static const char *const lpi_gpio_functions[] = {
  92. [LPI_GPIO_FUNC_INDEX_GPIO] = LPI_GPIO_FUNC_GPIO,
  93. [LPI_GPIO_FUNC_INDEX_FUNC1] = LPI_GPIO_FUNC_FUNC1,
  94. [LPI_GPIO_FUNC_INDEX_FUNC2] = LPI_GPIO_FUNC_FUNC2,
  95. [LPI_GPIO_FUNC_INDEX_FUNC3] = LPI_GPIO_FUNC_FUNC3,
  96. [LPI_GPIO_FUNC_INDEX_FUNC4] = LPI_GPIO_FUNC_FUNC4,
  97. [LPI_GPIO_FUNC_INDEX_FUNC5] = LPI_GPIO_FUNC_FUNC5,
  98. };
  99. static int lpi_gpio_read(struct lpi_gpio_pad *pad, unsigned int addr)
  100. {
  101. int ret;
  102. if (!lpi_dev_up) {
  103. pr_err_ratelimited("%s: ADSP is down due to SSR, return\n",
  104. __func__);
  105. return 0;
  106. }
  107. pm_runtime_get_sync(lpi_dev);
  108. ret = ioread32(pad->base + pad->offset + addr);
  109. if (ret < 0)
  110. pr_err("%s: read 0x%x failed\n", __func__, addr);
  111. pm_runtime_mark_last_busy(lpi_dev);
  112. pm_runtime_put_autosuspend(lpi_dev);
  113. return ret;
  114. }
  115. static int lpi_gpio_write(struct lpi_gpio_pad *pad, unsigned int addr,
  116. unsigned int val)
  117. {
  118. if (!lpi_dev_up) {
  119. pr_err_ratelimited("%s: ADSP is down due to SSR, return\n",
  120. __func__);
  121. return 0;
  122. }
  123. pm_runtime_get_sync(lpi_dev);
  124. iowrite32(val, pad->base + pad->offset + addr);
  125. pm_runtime_mark_last_busy(lpi_dev);
  126. pm_runtime_put_autosuspend(lpi_dev);
  127. return 0;
  128. }
  129. static int lpi_gpio_get_groups_count(struct pinctrl_dev *pctldev)
  130. {
  131. /* Every PIN is a group */
  132. return pctldev->desc->npins;
  133. }
  134. static const char *lpi_gpio_get_group_name(struct pinctrl_dev *pctldev,
  135. unsigned int pin)
  136. {
  137. return pctldev->desc->pins[pin].name;
  138. }
  139. static int lpi_gpio_get_group_pins(struct pinctrl_dev *pctldev,
  140. unsigned int pin,
  141. const unsigned int **pins,
  142. unsigned int *num_pins)
  143. {
  144. *pins = &pctldev->desc->pins[pin].number;
  145. *num_pins = 1;
  146. return 0;
  147. }
  148. static const struct pinctrl_ops lpi_gpio_pinctrl_ops = {
  149. .get_groups_count = lpi_gpio_get_groups_count,
  150. .get_group_name = lpi_gpio_get_group_name,
  151. .get_group_pins = lpi_gpio_get_group_pins,
  152. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  153. .dt_free_map = pinctrl_utils_free_map,
  154. };
  155. static int lpi_gpio_get_functions_count(struct pinctrl_dev *pctldev)
  156. {
  157. return ARRAY_SIZE(lpi_gpio_functions);
  158. }
  159. static const char *lpi_gpio_get_function_name(struct pinctrl_dev *pctldev,
  160. unsigned int function)
  161. {
  162. return lpi_gpio_functions[function];
  163. }
  164. static int lpi_gpio_get_function_groups(struct pinctrl_dev *pctldev,
  165. unsigned int function,
  166. const char *const **groups,
  167. unsigned *const num_qgroups)
  168. {
  169. *groups = lpi_gpio_groups;
  170. *num_qgroups = pctldev->desc->npins;
  171. return 0;
  172. }
  173. static int lpi_gpio_set_mux(struct pinctrl_dev *pctldev, unsigned int function,
  174. unsigned int pin)
  175. {
  176. struct lpi_gpio_pad *pad;
  177. unsigned int val;
  178. pad = pctldev->desc->pins[pin].drv_data;
  179. pad->function = function;
  180. val = lpi_gpio_read(pad, LPI_GPIO_REG_VAL_CTL);
  181. val &= ~(LPI_GPIO_REG_FUNCTION_MASK);
  182. val |= pad->function << LPI_GPIO_REG_FUNCTION_SHIFT;
  183. lpi_gpio_write(pad, LPI_GPIO_REG_VAL_CTL, val);
  184. return 0;
  185. }
  186. static const struct pinmux_ops lpi_gpio_pinmux_ops = {
  187. .get_functions_count = lpi_gpio_get_functions_count,
  188. .get_function_name = lpi_gpio_get_function_name,
  189. .get_function_groups = lpi_gpio_get_function_groups,
  190. .set_mux = lpi_gpio_set_mux,
  191. };
  192. static int lpi_config_get(struct pinctrl_dev *pctldev,
  193. unsigned int pin, unsigned long *config)
  194. {
  195. unsigned int param = pinconf_to_config_param(*config);
  196. struct lpi_gpio_pad *pad;
  197. unsigned int arg;
  198. pad = pctldev->desc->pins[pin].drv_data;
  199. switch (param) {
  200. case PIN_CONFIG_BIAS_DISABLE:
  201. arg = pad->pullup = LPI_GPIO_BIAS_DISABLE;
  202. break;
  203. case PIN_CONFIG_BIAS_PULL_DOWN:
  204. arg = pad->pullup == LPI_GPIO_PULL_DOWN;
  205. break;
  206. case PIN_CONFIG_BIAS_BUS_HOLD:
  207. arg = pad->pullup = LPI_GPIO_KEEPER;
  208. break;
  209. case PIN_CONFIG_BIAS_PULL_UP:
  210. arg = pad->pullup == LPI_GPIO_PULL_UP;
  211. break;
  212. case PIN_CONFIG_INPUT_ENABLE:
  213. case PIN_CONFIG_OUTPUT:
  214. arg = pad->output_enabled;
  215. break;
  216. default:
  217. return -EINVAL;
  218. }
  219. *config = pinconf_to_config_packed(param, arg);
  220. return 0;
  221. }
  222. static unsigned int lpi_drive_to_regval(u32 arg)
  223. {
  224. return (arg/2 - 1);
  225. }
  226. static int lpi_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  227. unsigned long *configs, unsigned int nconfs)
  228. {
  229. struct lpi_gpio_pad *pad;
  230. unsigned int param, arg;
  231. int i, ret = 0, val;
  232. pad = pctldev->desc->pins[pin].drv_data;
  233. for (i = 0; i < nconfs; i++) {
  234. param = pinconf_to_config_param(configs[i]);
  235. arg = pinconf_to_config_argument(configs[i]);
  236. dev_dbg(pctldev->dev, "%s: param: %d arg: %d pin: %d\n",
  237. __func__, param, arg, pin);
  238. switch (param) {
  239. case PIN_CONFIG_BIAS_DISABLE:
  240. pad->pullup = LPI_GPIO_BIAS_DISABLE;
  241. break;
  242. case PIN_CONFIG_BIAS_PULL_DOWN:
  243. pad->pullup = LPI_GPIO_PULL_DOWN;
  244. break;
  245. case PIN_CONFIG_BIAS_BUS_HOLD:
  246. pad->pullup = LPI_GPIO_KEEPER;
  247. break;
  248. case PIN_CONFIG_BIAS_PULL_UP:
  249. pad->pullup = LPI_GPIO_PULL_UP;
  250. break;
  251. case PIN_CONFIG_INPUT_ENABLE:
  252. pad->output_enabled = false;
  253. break;
  254. case PIN_CONFIG_OUTPUT:
  255. pad->output_enabled = true;
  256. pad->value = arg;
  257. break;
  258. case PIN_CONFIG_DRIVE_STRENGTH:
  259. pad->strength = arg;
  260. break;
  261. default:
  262. ret = -EINVAL;
  263. goto done;
  264. }
  265. }
  266. val = lpi_gpio_read(pad, LPI_GPIO_REG_VAL_CTL);
  267. val &= ~(LPI_GPIO_REG_PULL_MASK | LPI_GPIO_REG_OUT_STRENGTH_MASK |
  268. LPI_GPIO_REG_OE_MASK);
  269. val |= pad->pullup << LPI_GPIO_REG_PULL_SHIFT;
  270. val |= lpi_drive_to_regval(pad->strength) <<
  271. LPI_GPIO_REG_OUT_STRENGTH_SHIFT;
  272. if (pad->output_enabled)
  273. val |= pad->value << LPI_GPIO_REG_OE_SHIFT;
  274. lpi_gpio_write(pad, LPI_GPIO_REG_VAL_CTL, val);
  275. lpi_gpio_write(pad, LPI_GPIO_REG_DIR_CTL,
  276. pad->output_enabled << LPI_GPIO_REG_DIR_SHIFT);
  277. done:
  278. return ret;
  279. }
  280. static const struct pinconf_ops lpi_gpio_pinconf_ops = {
  281. .is_generic = true,
  282. .pin_config_group_get = lpi_config_get,
  283. .pin_config_group_set = lpi_config_set,
  284. };
  285. static int lpi_gpio_direction_input(struct gpio_chip *chip, unsigned int pin)
  286. {
  287. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  288. unsigned long config;
  289. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
  290. return lpi_config_set(state->ctrl, pin, &config, 1);
  291. }
  292. static int lpi_gpio_direction_output(struct gpio_chip *chip,
  293. unsigned int pin, int val)
  294. {
  295. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  296. unsigned long config;
  297. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
  298. return lpi_config_set(state->ctrl, pin, &config, 1);
  299. }
  300. static int lpi_gpio_get(struct gpio_chip *chip, unsigned int pin)
  301. {
  302. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  303. struct lpi_gpio_pad *pad;
  304. int value;
  305. pad = state->ctrl->desc->pins[pin].drv_data;
  306. value = lpi_gpio_read(pad, LPI_GPIO_REG_VAL_CTL);
  307. return value;
  308. }
  309. static void lpi_gpio_set(struct gpio_chip *chip, unsigned int pin, int value)
  310. {
  311. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  312. unsigned long config;
  313. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
  314. lpi_config_set(state->ctrl, pin, &config, 1);
  315. }
  316. static int lpi_notifier_service_cb(struct notifier_block *this,
  317. unsigned long opcode, void *ptr)
  318. {
  319. static bool initial_boot = true;
  320. pr_debug("%s: Service opcode 0x%lx\n", __func__, opcode);
  321. switch (opcode) {
  322. case AUDIO_NOTIFIER_SERVICE_DOWN:
  323. if (initial_boot) {
  324. initial_boot = false;
  325. break;
  326. }
  327. snd_event_notify(lpi_dev, SND_EVENT_DOWN);
  328. lpi_dev_up = false;
  329. break;
  330. case AUDIO_NOTIFIER_SERVICE_UP:
  331. if (initial_boot)
  332. initial_boot = false;
  333. lpi_dev_up = true;
  334. snd_event_notify(lpi_dev, SND_EVENT_UP);
  335. break;
  336. default:
  337. break;
  338. }
  339. return NOTIFY_OK;
  340. }
  341. static struct notifier_block service_nb = {
  342. .notifier_call = lpi_notifier_service_cb,
  343. .priority = -INT_MAX,
  344. };
  345. static void lpi_pinctrl_ssr_disable(struct device *dev, void *data)
  346. {
  347. lpi_dev_up = false;
  348. }
  349. static const struct snd_event_ops lpi_pinctrl_ssr_ops = {
  350. .disable = lpi_pinctrl_ssr_disable,
  351. };
  352. #ifdef CONFIG_DEBUG_FS
  353. #include <linux/seq_file.h>
  354. static unsigned int lpi_regval_to_drive(u32 val)
  355. {
  356. return (val + 1) * 2;
  357. }
  358. static void lpi_gpio_dbg_show_one(struct seq_file *s,
  359. struct pinctrl_dev *pctldev,
  360. struct gpio_chip *chip,
  361. unsigned int offset,
  362. unsigned int gpio)
  363. {
  364. struct lpi_gpio_state *state = gpiochip_get_data(chip);
  365. struct pinctrl_pin_desc pindesc;
  366. struct lpi_gpio_pad *pad;
  367. unsigned int func;
  368. int is_out;
  369. int drive;
  370. int pull;
  371. u32 ctl_reg;
  372. static const char * const pulls[] = {
  373. "no pull",
  374. "pull down",
  375. "keeper",
  376. "pull up"
  377. };
  378. pctldev = pctldev ? : state->ctrl;
  379. pindesc = pctldev->desc->pins[offset];
  380. pad = pctldev->desc->pins[offset].drv_data;
  381. ctl_reg = lpi_gpio_read(pad, LPI_GPIO_REG_DIR_CTL);
  382. is_out = (ctl_reg & LPI_GPIO_REG_DIR_MASK) >> LPI_GPIO_REG_DIR_SHIFT;
  383. ctl_reg = lpi_gpio_read(pad, LPI_GPIO_REG_VAL_CTL);
  384. func = (ctl_reg & LPI_GPIO_REG_FUNCTION_MASK) >>
  385. LPI_GPIO_REG_FUNCTION_SHIFT;
  386. drive = (ctl_reg & LPI_GPIO_REG_OUT_STRENGTH_MASK) >>
  387. LPI_GPIO_REG_OUT_STRENGTH_SHIFT;
  388. pull = (ctl_reg & LPI_GPIO_REG_PULL_MASK) >> LPI_GPIO_REG_PULL_SHIFT;
  389. seq_printf(s, " %-8s: %-3s %d",
  390. pindesc.name, is_out ? "out" : "in", func);
  391. seq_printf(s, " %dmA", lpi_regval_to_drive(drive));
  392. seq_printf(s, " %s", pulls[pull]);
  393. }
  394. static void lpi_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  395. {
  396. unsigned int gpio = chip->base;
  397. unsigned int i;
  398. for (i = 0; i < chip->ngpio; i++, gpio++) {
  399. lpi_gpio_dbg_show_one(s, NULL, chip, i, gpio);
  400. seq_puts(s, "\n");
  401. }
  402. }
  403. #else
  404. #define lpi_gpio_dbg_show NULL
  405. #endif
  406. static const struct gpio_chip lpi_gpio_template = {
  407. .direction_input = lpi_gpio_direction_input,
  408. .direction_output = lpi_gpio_direction_output,
  409. .get = lpi_gpio_get,
  410. .set = lpi_gpio_set,
  411. .request = gpiochip_generic_request,
  412. .free = gpiochip_generic_free,
  413. .dbg_show = lpi_gpio_dbg_show,
  414. };
  415. static int lpi_pinctrl_probe(struct platform_device *pdev)
  416. {
  417. struct device *dev = &pdev->dev;
  418. struct pinctrl_pin_desc *pindesc;
  419. struct pinctrl_desc *pctrldesc;
  420. struct lpi_gpio_pad *pad, *pads;
  421. struct lpi_gpio_state *state;
  422. int ret, npins, i;
  423. char __iomem *lpi_base;
  424. u32 reg;
  425. struct clk *lpass_npa_rsc_island = NULL;
  426. ret = of_property_read_u32(dev->of_node, "reg", &reg);
  427. if (ret < 0) {
  428. dev_err(dev, "missing base address\n");
  429. return ret;
  430. }
  431. ret = of_property_read_u32(dev->of_node, "qcom,num-gpios", &npins);
  432. if (ret < 0)
  433. return ret;
  434. WARN_ON(npins > ARRAY_SIZE(lpi_gpio_groups));
  435. ret = of_property_read_u32_array(dev->of_node, "qcom,lpi-offset-tbl",
  436. lpi_offset, npins);
  437. if (ret < 0) {
  438. dev_err(dev, "error in reading lpi offset table: %d\n", ret);
  439. return ret;
  440. }
  441. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  442. if (!state)
  443. return -ENOMEM;
  444. platform_set_drvdata(pdev, state);
  445. state->dev = &pdev->dev;
  446. pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
  447. if (!pindesc)
  448. return -ENOMEM;
  449. pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
  450. if (!pads)
  451. return -ENOMEM;
  452. pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
  453. if (!pctrldesc)
  454. return -ENOMEM;
  455. pctrldesc->pctlops = &lpi_gpio_pinctrl_ops;
  456. pctrldesc->pmxops = &lpi_gpio_pinmux_ops;
  457. pctrldesc->confops = &lpi_gpio_pinconf_ops;
  458. pctrldesc->owner = THIS_MODULE;
  459. pctrldesc->name = dev_name(dev);
  460. pctrldesc->pins = pindesc;
  461. pctrldesc->npins = npins;
  462. lpi_base = devm_ioremap(dev, reg, LPI_ADDRESS_SIZE);
  463. if (lpi_base == NULL) {
  464. dev_err(dev, "%s devm_ioremap failed\n", __func__);
  465. return -ENOMEM;
  466. }
  467. state->base = lpi_base;
  468. for (i = 0; i < npins; i++, pindesc++) {
  469. pad = &pads[i];
  470. pindesc->drv_data = pad;
  471. pindesc->number = i;
  472. pindesc->name = lpi_gpio_groups[i];
  473. pad->base = lpi_base;
  474. pad->offset = lpi_offset[i];
  475. }
  476. state->chip = lpi_gpio_template;
  477. state->chip.parent = dev;
  478. state->chip.base = -1;
  479. state->chip.ngpio = npins;
  480. state->chip.label = dev_name(dev);
  481. state->chip.of_gpio_n_cells = 2;
  482. state->chip.can_sleep = false;
  483. state->ctrl = devm_pinctrl_register(dev, pctrldesc, state);
  484. if (IS_ERR(state->ctrl))
  485. return PTR_ERR(state->ctrl);
  486. ret = gpiochip_add_data(&state->chip, state);
  487. if (ret) {
  488. dev_err(state->dev, "can't add gpio chip\n");
  489. goto err_chip;
  490. }
  491. ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0, npins);
  492. if (ret) {
  493. dev_err(dev, "failed to add pin range\n");
  494. goto err_range;
  495. }
  496. lpi_dev = &pdev->dev;
  497. lpi_dev_up = true;
  498. ret = audio_notifier_register("lpi_tlmm", AUDIO_NOTIFIER_ADSP_DOMAIN,
  499. &service_nb);
  500. if (ret < 0) {
  501. pr_err("%s: Audio notifier register failed ret = %d\n",
  502. __func__, ret);
  503. goto err_range;
  504. }
  505. ret = snd_event_client_register(dev, &lpi_pinctrl_ssr_ops, NULL);
  506. if (!ret) {
  507. snd_event_notify(dev, SND_EVENT_UP);
  508. } else {
  509. dev_err(dev, "%s: snd_event registration failed, ret [%d]\n",
  510. __func__, ret);
  511. goto err_snd_evt;
  512. }
  513. /* Register LPASS NPA resource */
  514. lpass_npa_rsc_island = devm_clk_get(&pdev->dev, "island_lpass_npa_rsc");
  515. if (IS_ERR(lpass_npa_rsc_island)) {
  516. ret = PTR_ERR(lpass_npa_rsc_island);
  517. dev_dbg(&pdev->dev, "%s: clk get %s failed %d\n",
  518. __func__, "island_lpass_npa_rsc", ret);
  519. lpass_npa_rsc_island = NULL;
  520. ret = 0;
  521. }
  522. state->lpass_npa_rsc_island = lpass_npa_rsc_island;
  523. pm_runtime_set_autosuspend_delay(&pdev->dev, LPI_AUTO_SUSPEND_DELAY);
  524. pm_runtime_use_autosuspend(&pdev->dev);
  525. pm_runtime_set_suspended(&pdev->dev);
  526. pm_runtime_enable(&pdev->dev);
  527. return 0;
  528. err_snd_evt:
  529. audio_notifier_deregister("lpi_tlmm");
  530. err_range:
  531. gpiochip_remove(&state->chip);
  532. err_chip:
  533. return ret;
  534. }
  535. static int lpi_pinctrl_remove(struct platform_device *pdev)
  536. {
  537. struct lpi_gpio_state *state = platform_get_drvdata(pdev);
  538. pm_runtime_disable(&pdev->dev);
  539. pm_runtime_set_suspended(&pdev->dev);
  540. snd_event_client_deregister(&pdev->dev);
  541. audio_notifier_deregister("lpi_tlmm");
  542. gpiochip_remove(&state->chip);
  543. return 0;
  544. }
  545. static const struct of_device_id lpi_pinctrl_of_match[] = {
  546. { .compatible = "qcom,lpi-pinctrl" }, /* Generic */
  547. { },
  548. };
  549. MODULE_DEVICE_TABLE(of, lpi_pinctrl_of_match);
  550. int lpi_pinctrl_runtime_resume(struct device *dev)
  551. {
  552. struct lpi_gpio_state *state = dev_get_drvdata(dev);
  553. int ret = 0;
  554. if (state->lpass_npa_rsc_island == NULL) {
  555. dev_dbg(dev, "%s: Invalid lpass npa rsc node\n", __func__);
  556. return 0;
  557. }
  558. ret = clk_prepare_enable(state->lpass_npa_rsc_island);
  559. if (ret < 0) {
  560. dev_err(dev, "%s:lpass npa rsc island enable failed\n",
  561. __func__);
  562. }
  563. pm_runtime_set_autosuspend_delay(dev, LPI_AUTO_SUSPEND_DELAY);
  564. return 0;
  565. }
  566. int lpi_pinctrl_runtime_suspend(struct device *dev)
  567. {
  568. struct lpi_gpio_state *state = dev_get_drvdata(dev);
  569. if (state->lpass_npa_rsc_island == NULL) {
  570. dev_dbg(dev, "%s: Invalid lpass npa rsc node\n", __func__);
  571. return 0;
  572. }
  573. clk_disable_unprepare(state->lpass_npa_rsc_island);
  574. return 0;
  575. }
  576. static const struct dev_pm_ops lpi_pinctrl_dev_pm_ops = {
  577. SET_RUNTIME_PM_OPS(
  578. lpi_pinctrl_runtime_suspend,
  579. lpi_pinctrl_runtime_resume,
  580. NULL
  581. )
  582. };
  583. static struct platform_driver lpi_pinctrl_driver = {
  584. .driver = {
  585. .name = "qcom-lpi-pinctrl",
  586. .pm = &lpi_pinctrl_dev_pm_ops,
  587. .of_match_table = lpi_pinctrl_of_match,
  588. },
  589. .probe = lpi_pinctrl_probe,
  590. .remove = lpi_pinctrl_remove,
  591. };
  592. module_platform_driver(lpi_pinctrl_driver);
  593. MODULE_DESCRIPTION("QTI LPI GPIO pin control driver");
  594. MODULE_LICENSE("GPL v2");