pinctrl-spmi-mpp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/gpio/driver.h>
  6. #include <linux/module.h>
  7. #include <linux/of.h>
  8. #include <linux/of_irq.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/regmap.h>
  14. #include <linux/slab.h>
  15. #include <linux/types.h>
  16. #include <dt-bindings/pinctrl/qcom,pmic-mpp.h>
  17. #include "../core.h"
  18. #include "../pinctrl-utils.h"
  19. #define PMIC_MPP_ADDRESS_RANGE 0x100
  20. /*
  21. * Pull Up Values - it indicates whether a pull-up should be
  22. * applied for bidirectional mode only. The hardware ignores the
  23. * configuration when operating in other modes.
  24. */
  25. #define PMIC_MPP_PULL_UP_0P6KOHM 0
  26. #define PMIC_MPP_PULL_UP_10KOHM 1
  27. #define PMIC_MPP_PULL_UP_30KOHM 2
  28. #define PMIC_MPP_PULL_UP_OPEN 3
  29. /* type registers base address bases */
  30. #define PMIC_MPP_REG_TYPE 0x4
  31. #define PMIC_MPP_REG_SUBTYPE 0x5
  32. /* mpp peripheral type and subtype values */
  33. #define PMIC_MPP_TYPE 0x11
  34. #define PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT 0x3
  35. #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT 0x4
  36. #define PMIC_MPP_SUBTYPE_4CH_NO_SINK 0x5
  37. #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK 0x6
  38. #define PMIC_MPP_SUBTYPE_4CH_FULL_FUNC 0x7
  39. #define PMIC_MPP_SUBTYPE_8CH_FULL_FUNC 0xf
  40. #define PMIC_MPP_REG_RT_STS 0x10
  41. #define PMIC_MPP_REG_RT_STS_VAL_MASK 0x1
  42. /* control register base address bases */
  43. #define PMIC_MPP_REG_MODE_CTL 0x40
  44. #define PMIC_MPP_REG_DIG_VIN_CTL 0x41
  45. #define PMIC_MPP_REG_DIG_PULL_CTL 0x42
  46. #define PMIC_MPP_REG_DIG_IN_CTL 0x43
  47. #define PMIC_MPP_REG_EN_CTL 0x46
  48. #define PMIC_MPP_REG_AOUT_CTL 0x48
  49. #define PMIC_MPP_REG_AIN_CTL 0x4a
  50. #define PMIC_MPP_REG_SINK_CTL 0x4c
  51. /* PMIC_MPP_REG_MODE_CTL */
  52. #define PMIC_MPP_REG_MODE_VALUE_MASK 0x1
  53. #define PMIC_MPP_REG_MODE_FUNCTION_SHIFT 1
  54. #define PMIC_MPP_REG_MODE_FUNCTION_MASK 0x7
  55. #define PMIC_MPP_REG_MODE_DIR_SHIFT 4
  56. #define PMIC_MPP_REG_MODE_DIR_MASK 0x7
  57. /* PMIC_MPP_REG_DIG_VIN_CTL */
  58. #define PMIC_MPP_REG_VIN_SHIFT 0
  59. #define PMIC_MPP_REG_VIN_MASK 0x7
  60. /* PMIC_MPP_REG_DIG_PULL_CTL */
  61. #define PMIC_MPP_REG_PULL_SHIFT 0
  62. #define PMIC_MPP_REG_PULL_MASK 0x7
  63. /* PMIC_MPP_REG_EN_CTL */
  64. #define PMIC_MPP_REG_MASTER_EN_SHIFT 7
  65. /* PMIC_MPP_REG_AIN_CTL */
  66. #define PMIC_MPP_REG_AIN_ROUTE_SHIFT 0
  67. #define PMIC_MPP_REG_AIN_ROUTE_MASK 0x7
  68. #define PMIC_MPP_MODE_DIGITAL_INPUT 0
  69. #define PMIC_MPP_MODE_DIGITAL_OUTPUT 1
  70. #define PMIC_MPP_MODE_DIGITAL_BIDIR 2
  71. #define PMIC_MPP_MODE_ANALOG_BIDIR 3
  72. #define PMIC_MPP_MODE_ANALOG_INPUT 4
  73. #define PMIC_MPP_MODE_ANALOG_OUTPUT 5
  74. #define PMIC_MPP_MODE_CURRENT_SINK 6
  75. #define PMIC_MPP_SELECTOR_NORMAL 0
  76. #define PMIC_MPP_SELECTOR_PAIRED 1
  77. #define PMIC_MPP_SELECTOR_DTEST_FIRST 4
  78. #define PMIC_MPP_PHYSICAL_OFFSET 1
  79. /* Qualcomm specific pin configurations */
  80. #define PMIC_MPP_CONF_AMUX_ROUTE (PIN_CONFIG_END + 1)
  81. #define PMIC_MPP_CONF_ANALOG_LEVEL (PIN_CONFIG_END + 2)
  82. #define PMIC_MPP_CONF_DTEST_SELECTOR (PIN_CONFIG_END + 3)
  83. #define PMIC_MPP_CONF_PAIRED (PIN_CONFIG_END + 4)
  84. /**
  85. * struct pmic_mpp_pad - keep current MPP settings
  86. * @base: Address base in SPMI device.
  87. * @is_enabled: Set to false when MPP should be put in high Z state.
  88. * @out_value: Cached pin output value.
  89. * @output_enabled: Set to true if MPP output logic is enabled.
  90. * @input_enabled: Set to true if MPP input buffer logic is enabled.
  91. * @paired: Pin operates in paired mode
  92. * @has_pullup: Pin has support to configure pullup
  93. * @num_sources: Number of power-sources supported by this MPP.
  94. * @power_source: Current power-source used.
  95. * @amux_input: Set the source for analog input.
  96. * @aout_level: Analog output level
  97. * @pullup: Pullup resistor value. Valid in Bidirectional mode only.
  98. * @function: See pmic_mpp_functions[].
  99. * @drive_strength: Amount of current in sink mode
  100. * @dtest: DTEST route selector
  101. */
  102. struct pmic_mpp_pad {
  103. u16 base;
  104. bool is_enabled;
  105. bool out_value;
  106. bool output_enabled;
  107. bool input_enabled;
  108. bool paired;
  109. bool has_pullup;
  110. unsigned int num_sources;
  111. unsigned int power_source;
  112. unsigned int amux_input;
  113. unsigned int aout_level;
  114. unsigned int pullup;
  115. unsigned int function;
  116. unsigned int drive_strength;
  117. unsigned int dtest;
  118. };
  119. struct pmic_mpp_state {
  120. struct device *dev;
  121. struct regmap *map;
  122. struct pinctrl_dev *ctrl;
  123. struct gpio_chip chip;
  124. struct irq_chip irq;
  125. };
  126. static const struct pinconf_generic_params pmic_mpp_bindings[] = {
  127. {"qcom,amux-route", PMIC_MPP_CONF_AMUX_ROUTE, 0},
  128. {"qcom,analog-level", PMIC_MPP_CONF_ANALOG_LEVEL, 0},
  129. {"qcom,dtest", PMIC_MPP_CONF_DTEST_SELECTOR, 0},
  130. {"qcom,paired", PMIC_MPP_CONF_PAIRED, 0},
  131. };
  132. #ifdef CONFIG_DEBUG_FS
  133. static const struct pin_config_item pmic_conf_items[] = {
  134. PCONFDUMP(PMIC_MPP_CONF_AMUX_ROUTE, "analog mux", NULL, true),
  135. PCONFDUMP(PMIC_MPP_CONF_ANALOG_LEVEL, "analog level", NULL, true),
  136. PCONFDUMP(PMIC_MPP_CONF_DTEST_SELECTOR, "dtest", NULL, true),
  137. PCONFDUMP(PMIC_MPP_CONF_PAIRED, "paired", NULL, false),
  138. };
  139. #endif
  140. static const char *const pmic_mpp_groups[] = {
  141. "mpp1", "mpp2", "mpp3", "mpp4", "mpp5", "mpp6", "mpp7", "mpp8",
  142. };
  143. #define PMIC_MPP_DIGITAL 0
  144. #define PMIC_MPP_ANALOG 1
  145. #define PMIC_MPP_SINK 2
  146. static const char *const pmic_mpp_functions[] = {
  147. "digital", "analog", "sink"
  148. };
  149. static int pmic_mpp_read(struct pmic_mpp_state *state,
  150. struct pmic_mpp_pad *pad, unsigned int addr)
  151. {
  152. unsigned int val;
  153. int ret;
  154. ret = regmap_read(state->map, pad->base + addr, &val);
  155. if (ret < 0)
  156. dev_err(state->dev, "read 0x%x failed\n", addr);
  157. else
  158. ret = val;
  159. return ret;
  160. }
  161. static int pmic_mpp_write(struct pmic_mpp_state *state,
  162. struct pmic_mpp_pad *pad, unsigned int addr,
  163. unsigned int val)
  164. {
  165. int ret;
  166. ret = regmap_write(state->map, pad->base + addr, val);
  167. if (ret < 0)
  168. dev_err(state->dev, "write 0x%x failed\n", addr);
  169. return ret;
  170. }
  171. static int pmic_mpp_get_groups_count(struct pinctrl_dev *pctldev)
  172. {
  173. /* Every PIN is a group */
  174. return pctldev->desc->npins;
  175. }
  176. static const char *pmic_mpp_get_group_name(struct pinctrl_dev *pctldev,
  177. unsigned pin)
  178. {
  179. return pctldev->desc->pins[pin].name;
  180. }
  181. static int pmic_mpp_get_group_pins(struct pinctrl_dev *pctldev,
  182. unsigned pin,
  183. const unsigned **pins, unsigned *num_pins)
  184. {
  185. *pins = &pctldev->desc->pins[pin].number;
  186. *num_pins = 1;
  187. return 0;
  188. }
  189. static const struct pinctrl_ops pmic_mpp_pinctrl_ops = {
  190. .get_groups_count = pmic_mpp_get_groups_count,
  191. .get_group_name = pmic_mpp_get_group_name,
  192. .get_group_pins = pmic_mpp_get_group_pins,
  193. .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
  194. .dt_free_map = pinctrl_utils_free_map,
  195. };
  196. static int pmic_mpp_get_functions_count(struct pinctrl_dev *pctldev)
  197. {
  198. return ARRAY_SIZE(pmic_mpp_functions);
  199. }
  200. static const char *pmic_mpp_get_function_name(struct pinctrl_dev *pctldev,
  201. unsigned function)
  202. {
  203. return pmic_mpp_functions[function];
  204. }
  205. static int pmic_mpp_get_function_groups(struct pinctrl_dev *pctldev,
  206. unsigned function,
  207. const char *const **groups,
  208. unsigned *const num_qgroups)
  209. {
  210. *groups = pmic_mpp_groups;
  211. *num_qgroups = pctldev->desc->npins;
  212. return 0;
  213. }
  214. static int pmic_mpp_write_mode_ctl(struct pmic_mpp_state *state,
  215. struct pmic_mpp_pad *pad)
  216. {
  217. unsigned int mode;
  218. unsigned int sel;
  219. unsigned int val;
  220. unsigned int en;
  221. switch (pad->function) {
  222. case PMIC_MPP_ANALOG:
  223. if (pad->input_enabled && pad->output_enabled)
  224. mode = PMIC_MPP_MODE_ANALOG_BIDIR;
  225. else if (pad->input_enabled)
  226. mode = PMIC_MPP_MODE_ANALOG_INPUT;
  227. else
  228. mode = PMIC_MPP_MODE_ANALOG_OUTPUT;
  229. break;
  230. case PMIC_MPP_DIGITAL:
  231. if (pad->input_enabled && pad->output_enabled)
  232. mode = PMIC_MPP_MODE_DIGITAL_BIDIR;
  233. else if (pad->input_enabled)
  234. mode = PMIC_MPP_MODE_DIGITAL_INPUT;
  235. else
  236. mode = PMIC_MPP_MODE_DIGITAL_OUTPUT;
  237. break;
  238. case PMIC_MPP_SINK:
  239. default:
  240. mode = PMIC_MPP_MODE_CURRENT_SINK;
  241. break;
  242. }
  243. if (pad->dtest)
  244. sel = PMIC_MPP_SELECTOR_DTEST_FIRST + pad->dtest - 1;
  245. else if (pad->paired)
  246. sel = PMIC_MPP_SELECTOR_PAIRED;
  247. else
  248. sel = PMIC_MPP_SELECTOR_NORMAL;
  249. en = !!pad->out_value;
  250. val = mode << PMIC_MPP_REG_MODE_DIR_SHIFT |
  251. sel << PMIC_MPP_REG_MODE_FUNCTION_SHIFT |
  252. en;
  253. return pmic_mpp_write(state, pad, PMIC_MPP_REG_MODE_CTL, val);
  254. }
  255. static int pmic_mpp_set_mux(struct pinctrl_dev *pctldev, unsigned function,
  256. unsigned pin)
  257. {
  258. struct pmic_mpp_state *state = pinctrl_dev_get_drvdata(pctldev);
  259. struct pmic_mpp_pad *pad;
  260. unsigned int val;
  261. int ret;
  262. pad = pctldev->desc->pins[pin].drv_data;
  263. pad->function = function;
  264. ret = pmic_mpp_write_mode_ctl(state, pad);
  265. if (ret < 0)
  266. return ret;
  267. val = pad->is_enabled << PMIC_MPP_REG_MASTER_EN_SHIFT;
  268. return pmic_mpp_write(state, pad, PMIC_MPP_REG_EN_CTL, val);
  269. }
  270. static const struct pinmux_ops pmic_mpp_pinmux_ops = {
  271. .get_functions_count = pmic_mpp_get_functions_count,
  272. .get_function_name = pmic_mpp_get_function_name,
  273. .get_function_groups = pmic_mpp_get_function_groups,
  274. .set_mux = pmic_mpp_set_mux,
  275. };
  276. static int pmic_mpp_config_get(struct pinctrl_dev *pctldev,
  277. unsigned int pin, unsigned long *config)
  278. {
  279. unsigned param = pinconf_to_config_param(*config);
  280. struct pmic_mpp_pad *pad;
  281. unsigned arg = 0;
  282. pad = pctldev->desc->pins[pin].drv_data;
  283. switch (param) {
  284. case PIN_CONFIG_BIAS_DISABLE:
  285. if (pad->pullup != PMIC_MPP_PULL_UP_OPEN)
  286. return -EINVAL;
  287. arg = 1;
  288. break;
  289. case PIN_CONFIG_BIAS_PULL_UP:
  290. switch (pad->pullup) {
  291. case PMIC_MPP_PULL_UP_0P6KOHM:
  292. arg = 600;
  293. break;
  294. case PMIC_MPP_PULL_UP_10KOHM:
  295. arg = 10000;
  296. break;
  297. case PMIC_MPP_PULL_UP_30KOHM:
  298. arg = 30000;
  299. break;
  300. default:
  301. return -EINVAL;
  302. }
  303. break;
  304. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  305. if (pad->is_enabled)
  306. return -EINVAL;
  307. arg = 1;
  308. break;
  309. case PIN_CONFIG_POWER_SOURCE:
  310. arg = pad->power_source;
  311. break;
  312. case PIN_CONFIG_INPUT_ENABLE:
  313. if (!pad->input_enabled)
  314. return -EINVAL;
  315. arg = 1;
  316. break;
  317. case PIN_CONFIG_OUTPUT:
  318. arg = pad->out_value;
  319. break;
  320. case PMIC_MPP_CONF_DTEST_SELECTOR:
  321. arg = pad->dtest;
  322. break;
  323. case PMIC_MPP_CONF_AMUX_ROUTE:
  324. arg = pad->amux_input;
  325. break;
  326. case PMIC_MPP_CONF_PAIRED:
  327. if (!pad->paired)
  328. return -EINVAL;
  329. arg = 1;
  330. break;
  331. case PIN_CONFIG_DRIVE_STRENGTH:
  332. arg = pad->drive_strength;
  333. break;
  334. case PMIC_MPP_CONF_ANALOG_LEVEL:
  335. arg = pad->aout_level;
  336. break;
  337. default:
  338. return -EINVAL;
  339. }
  340. /* Convert register value to pinconf value */
  341. *config = pinconf_to_config_packed(param, arg);
  342. return 0;
  343. }
  344. static int pmic_mpp_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
  345. unsigned long *configs, unsigned nconfs)
  346. {
  347. struct pmic_mpp_state *state = pinctrl_dev_get_drvdata(pctldev);
  348. struct pmic_mpp_pad *pad;
  349. unsigned param, arg;
  350. unsigned int val;
  351. int i, ret;
  352. pad = pctldev->desc->pins[pin].drv_data;
  353. /* Make it possible to enable the pin, by not setting high impedance */
  354. pad->is_enabled = true;
  355. for (i = 0; i < nconfs; i++) {
  356. param = pinconf_to_config_param(configs[i]);
  357. arg = pinconf_to_config_argument(configs[i]);
  358. switch (param) {
  359. case PIN_CONFIG_BIAS_DISABLE:
  360. pad->pullup = PMIC_MPP_PULL_UP_OPEN;
  361. break;
  362. case PIN_CONFIG_BIAS_PULL_UP:
  363. switch (arg) {
  364. case 600:
  365. pad->pullup = PMIC_MPP_PULL_UP_0P6KOHM;
  366. break;
  367. case 10000:
  368. pad->pullup = PMIC_MPP_PULL_UP_10KOHM;
  369. break;
  370. case 30000:
  371. pad->pullup = PMIC_MPP_PULL_UP_30KOHM;
  372. break;
  373. default:
  374. return -EINVAL;
  375. }
  376. break;
  377. case PIN_CONFIG_BIAS_HIGH_IMPEDANCE:
  378. pad->is_enabled = false;
  379. break;
  380. case PIN_CONFIG_POWER_SOURCE:
  381. if (arg >= pad->num_sources)
  382. return -EINVAL;
  383. pad->power_source = arg;
  384. break;
  385. case PIN_CONFIG_INPUT_ENABLE:
  386. pad->input_enabled = arg ? true : false;
  387. break;
  388. case PIN_CONFIG_OUTPUT:
  389. pad->output_enabled = true;
  390. pad->out_value = arg;
  391. break;
  392. case PMIC_MPP_CONF_DTEST_SELECTOR:
  393. pad->dtest = arg;
  394. break;
  395. case PIN_CONFIG_DRIVE_STRENGTH:
  396. pad->drive_strength = arg;
  397. break;
  398. case PMIC_MPP_CONF_AMUX_ROUTE:
  399. if (arg >= PMIC_MPP_AMUX_ROUTE_ABUS4)
  400. return -EINVAL;
  401. pad->amux_input = arg;
  402. break;
  403. case PMIC_MPP_CONF_ANALOG_LEVEL:
  404. pad->aout_level = arg;
  405. break;
  406. case PMIC_MPP_CONF_PAIRED:
  407. pad->paired = !!arg;
  408. break;
  409. default:
  410. return -EINVAL;
  411. }
  412. }
  413. val = pad->power_source << PMIC_MPP_REG_VIN_SHIFT;
  414. ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_DIG_VIN_CTL, val);
  415. if (ret < 0)
  416. return ret;
  417. if (pad->has_pullup) {
  418. val = pad->pullup << PMIC_MPP_REG_PULL_SHIFT;
  419. ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_DIG_PULL_CTL,
  420. val);
  421. if (ret < 0)
  422. return ret;
  423. }
  424. val = pad->amux_input & PMIC_MPP_REG_AIN_ROUTE_MASK;
  425. ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_AIN_CTL, val);
  426. if (ret < 0)
  427. return ret;
  428. ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_AOUT_CTL, pad->aout_level);
  429. if (ret < 0)
  430. return ret;
  431. ret = pmic_mpp_write_mode_ctl(state, pad);
  432. if (ret < 0)
  433. return ret;
  434. ret = pmic_mpp_write(state, pad, PMIC_MPP_REG_SINK_CTL, pad->drive_strength);
  435. if (ret < 0)
  436. return ret;
  437. val = pad->is_enabled << PMIC_MPP_REG_MASTER_EN_SHIFT;
  438. return pmic_mpp_write(state, pad, PMIC_MPP_REG_EN_CTL, val);
  439. }
  440. static void pmic_mpp_config_dbg_show(struct pinctrl_dev *pctldev,
  441. struct seq_file *s, unsigned pin)
  442. {
  443. struct pmic_mpp_state *state = pinctrl_dev_get_drvdata(pctldev);
  444. struct pmic_mpp_pad *pad;
  445. int ret;
  446. static const char *const biases[] = {
  447. "0.6kOhm", "10kOhm", "30kOhm", "Disabled"
  448. };
  449. pad = pctldev->desc->pins[pin].drv_data;
  450. seq_printf(s, " mpp%-2d:", pin + PMIC_MPP_PHYSICAL_OFFSET);
  451. if (!pad->is_enabled) {
  452. seq_puts(s, " ---");
  453. } else {
  454. if (pad->input_enabled) {
  455. ret = pmic_mpp_read(state, pad, PMIC_MPP_REG_RT_STS);
  456. if (ret < 0)
  457. return;
  458. ret &= PMIC_MPP_REG_RT_STS_VAL_MASK;
  459. pad->out_value = ret;
  460. }
  461. seq_printf(s, " %-4s", pad->output_enabled ? "out" : "in");
  462. seq_printf(s, " %-7s", pmic_mpp_functions[pad->function]);
  463. seq_printf(s, " vin-%d", pad->power_source);
  464. seq_printf(s, " %d", pad->aout_level);
  465. if (pad->has_pullup)
  466. seq_printf(s, " %-8s", biases[pad->pullup]);
  467. seq_printf(s, " %-4s", pad->out_value ? "high" : "low");
  468. if (pad->dtest)
  469. seq_printf(s, " dtest%d", pad->dtest);
  470. if (pad->paired)
  471. seq_puts(s, " paired");
  472. }
  473. }
  474. static const struct pinconf_ops pmic_mpp_pinconf_ops = {
  475. .is_generic = true,
  476. .pin_config_group_get = pmic_mpp_config_get,
  477. .pin_config_group_set = pmic_mpp_config_set,
  478. .pin_config_group_dbg_show = pmic_mpp_config_dbg_show,
  479. };
  480. static int pmic_mpp_direction_input(struct gpio_chip *chip, unsigned pin)
  481. {
  482. struct pmic_mpp_state *state = gpiochip_get_data(chip);
  483. unsigned long config;
  484. config = pinconf_to_config_packed(PIN_CONFIG_INPUT_ENABLE, 1);
  485. return pmic_mpp_config_set(state->ctrl, pin, &config, 1);
  486. }
  487. static int pmic_mpp_direction_output(struct gpio_chip *chip,
  488. unsigned pin, int val)
  489. {
  490. struct pmic_mpp_state *state = gpiochip_get_data(chip);
  491. unsigned long config;
  492. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, val);
  493. return pmic_mpp_config_set(state->ctrl, pin, &config, 1);
  494. }
  495. static int pmic_mpp_get(struct gpio_chip *chip, unsigned pin)
  496. {
  497. struct pmic_mpp_state *state = gpiochip_get_data(chip);
  498. struct pmic_mpp_pad *pad;
  499. int ret;
  500. pad = state->ctrl->desc->pins[pin].drv_data;
  501. if (pad->input_enabled) {
  502. ret = pmic_mpp_read(state, pad, PMIC_MPP_REG_RT_STS);
  503. if (ret < 0)
  504. return ret;
  505. pad->out_value = ret & PMIC_MPP_REG_RT_STS_VAL_MASK;
  506. }
  507. return !!pad->out_value;
  508. }
  509. static void pmic_mpp_set(struct gpio_chip *chip, unsigned pin, int value)
  510. {
  511. struct pmic_mpp_state *state = gpiochip_get_data(chip);
  512. unsigned long config;
  513. config = pinconf_to_config_packed(PIN_CONFIG_OUTPUT, value);
  514. pmic_mpp_config_set(state->ctrl, pin, &config, 1);
  515. }
  516. static int pmic_mpp_of_xlate(struct gpio_chip *chip,
  517. const struct of_phandle_args *gpio_desc,
  518. u32 *flags)
  519. {
  520. if (chip->of_gpio_n_cells < 2)
  521. return -EINVAL;
  522. if (flags)
  523. *flags = gpio_desc->args[1];
  524. return gpio_desc->args[0] - PMIC_MPP_PHYSICAL_OFFSET;
  525. }
  526. static void pmic_mpp_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  527. {
  528. struct pmic_mpp_state *state = gpiochip_get_data(chip);
  529. unsigned i;
  530. for (i = 0; i < chip->ngpio; i++) {
  531. pmic_mpp_config_dbg_show(state->ctrl, s, i);
  532. seq_puts(s, "\n");
  533. }
  534. }
  535. static const struct gpio_chip pmic_mpp_gpio_template = {
  536. .direction_input = pmic_mpp_direction_input,
  537. .direction_output = pmic_mpp_direction_output,
  538. .get = pmic_mpp_get,
  539. .set = pmic_mpp_set,
  540. .request = gpiochip_generic_request,
  541. .free = gpiochip_generic_free,
  542. .of_xlate = pmic_mpp_of_xlate,
  543. .dbg_show = pmic_mpp_dbg_show,
  544. };
  545. static int pmic_mpp_populate(struct pmic_mpp_state *state,
  546. struct pmic_mpp_pad *pad)
  547. {
  548. int type, subtype, val, dir;
  549. unsigned int sel;
  550. type = pmic_mpp_read(state, pad, PMIC_MPP_REG_TYPE);
  551. if (type < 0)
  552. return type;
  553. if (type != PMIC_MPP_TYPE) {
  554. dev_err(state->dev, "incorrect block type 0x%x at 0x%x\n",
  555. type, pad->base);
  556. return -ENODEV;
  557. }
  558. subtype = pmic_mpp_read(state, pad, PMIC_MPP_REG_SUBTYPE);
  559. if (subtype < 0)
  560. return subtype;
  561. switch (subtype) {
  562. case PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT:
  563. case PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT:
  564. case PMIC_MPP_SUBTYPE_4CH_NO_SINK:
  565. case PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK:
  566. case PMIC_MPP_SUBTYPE_4CH_FULL_FUNC:
  567. pad->num_sources = 4;
  568. break;
  569. case PMIC_MPP_SUBTYPE_8CH_FULL_FUNC:
  570. pad->num_sources = 8;
  571. break;
  572. default:
  573. dev_err(state->dev, "unknown MPP type 0x%x at 0x%x\n",
  574. subtype, pad->base);
  575. return -ENODEV;
  576. }
  577. val = pmic_mpp_read(state, pad, PMIC_MPP_REG_MODE_CTL);
  578. if (val < 0)
  579. return val;
  580. pad->out_value = val & PMIC_MPP_REG_MODE_VALUE_MASK;
  581. dir = val >> PMIC_MPP_REG_MODE_DIR_SHIFT;
  582. dir &= PMIC_MPP_REG_MODE_DIR_MASK;
  583. switch (dir) {
  584. case PMIC_MPP_MODE_DIGITAL_INPUT:
  585. pad->input_enabled = true;
  586. pad->output_enabled = false;
  587. pad->function = PMIC_MPP_DIGITAL;
  588. break;
  589. case PMIC_MPP_MODE_DIGITAL_OUTPUT:
  590. pad->input_enabled = false;
  591. pad->output_enabled = true;
  592. pad->function = PMIC_MPP_DIGITAL;
  593. break;
  594. case PMIC_MPP_MODE_DIGITAL_BIDIR:
  595. pad->input_enabled = true;
  596. pad->output_enabled = true;
  597. pad->function = PMIC_MPP_DIGITAL;
  598. break;
  599. case PMIC_MPP_MODE_ANALOG_BIDIR:
  600. pad->input_enabled = true;
  601. pad->output_enabled = true;
  602. pad->function = PMIC_MPP_ANALOG;
  603. break;
  604. case PMIC_MPP_MODE_ANALOG_INPUT:
  605. pad->input_enabled = true;
  606. pad->output_enabled = false;
  607. pad->function = PMIC_MPP_ANALOG;
  608. break;
  609. case PMIC_MPP_MODE_ANALOG_OUTPUT:
  610. pad->input_enabled = false;
  611. pad->output_enabled = true;
  612. pad->function = PMIC_MPP_ANALOG;
  613. break;
  614. case PMIC_MPP_MODE_CURRENT_SINK:
  615. pad->input_enabled = false;
  616. pad->output_enabled = true;
  617. pad->function = PMIC_MPP_SINK;
  618. break;
  619. default:
  620. dev_err(state->dev, "unknown MPP direction\n");
  621. return -ENODEV;
  622. }
  623. sel = val >> PMIC_MPP_REG_MODE_FUNCTION_SHIFT;
  624. sel &= PMIC_MPP_REG_MODE_FUNCTION_MASK;
  625. if (sel >= PMIC_MPP_SELECTOR_DTEST_FIRST)
  626. pad->dtest = sel + 1;
  627. else if (sel == PMIC_MPP_SELECTOR_PAIRED)
  628. pad->paired = true;
  629. val = pmic_mpp_read(state, pad, PMIC_MPP_REG_DIG_VIN_CTL);
  630. if (val < 0)
  631. return val;
  632. pad->power_source = val >> PMIC_MPP_REG_VIN_SHIFT;
  633. pad->power_source &= PMIC_MPP_REG_VIN_MASK;
  634. if (subtype != PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT &&
  635. subtype != PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK) {
  636. val = pmic_mpp_read(state, pad, PMIC_MPP_REG_DIG_PULL_CTL);
  637. if (val < 0)
  638. return val;
  639. pad->pullup = val >> PMIC_MPP_REG_PULL_SHIFT;
  640. pad->pullup &= PMIC_MPP_REG_PULL_MASK;
  641. pad->has_pullup = true;
  642. }
  643. val = pmic_mpp_read(state, pad, PMIC_MPP_REG_AIN_CTL);
  644. if (val < 0)
  645. return val;
  646. pad->amux_input = val >> PMIC_MPP_REG_AIN_ROUTE_SHIFT;
  647. pad->amux_input &= PMIC_MPP_REG_AIN_ROUTE_MASK;
  648. val = pmic_mpp_read(state, pad, PMIC_MPP_REG_SINK_CTL);
  649. if (val < 0)
  650. return val;
  651. pad->drive_strength = val;
  652. val = pmic_mpp_read(state, pad, PMIC_MPP_REG_AOUT_CTL);
  653. if (val < 0)
  654. return val;
  655. pad->aout_level = val;
  656. val = pmic_mpp_read(state, pad, PMIC_MPP_REG_EN_CTL);
  657. if (val < 0)
  658. return val;
  659. pad->is_enabled = !!val;
  660. return 0;
  661. }
  662. static int pmic_mpp_domain_translate(struct irq_domain *domain,
  663. struct irq_fwspec *fwspec,
  664. unsigned long *hwirq,
  665. unsigned int *type)
  666. {
  667. struct pmic_mpp_state *state = container_of(domain->host_data,
  668. struct pmic_mpp_state,
  669. chip);
  670. if (fwspec->param_count != 2 ||
  671. fwspec->param[0] < 1 || fwspec->param[0] > state->chip.ngpio)
  672. return -EINVAL;
  673. *hwirq = fwspec->param[0] - PMIC_MPP_PHYSICAL_OFFSET;
  674. *type = fwspec->param[1];
  675. return 0;
  676. }
  677. static unsigned int pmic_mpp_child_offset_to_irq(struct gpio_chip *chip,
  678. unsigned int offset)
  679. {
  680. return offset + PMIC_MPP_PHYSICAL_OFFSET;
  681. }
  682. static int pmic_mpp_child_to_parent_hwirq(struct gpio_chip *chip,
  683. unsigned int child_hwirq,
  684. unsigned int child_type,
  685. unsigned int *parent_hwirq,
  686. unsigned int *parent_type)
  687. {
  688. *parent_hwirq = child_hwirq + 0xc0;
  689. *parent_type = child_type;
  690. return 0;
  691. }
  692. static int pmic_mpp_probe(struct platform_device *pdev)
  693. {
  694. struct irq_domain *parent_domain;
  695. struct device_node *parent_node;
  696. struct device *dev = &pdev->dev;
  697. struct pinctrl_pin_desc *pindesc;
  698. struct pinctrl_desc *pctrldesc;
  699. struct pmic_mpp_pad *pad, *pads;
  700. struct pmic_mpp_state *state;
  701. struct gpio_irq_chip *girq;
  702. int ret, npins, i;
  703. u32 reg;
  704. ret = of_property_read_u32(dev->of_node, "reg", &reg);
  705. if (ret < 0) {
  706. dev_err(dev, "missing base address");
  707. return ret;
  708. }
  709. npins = (uintptr_t) device_get_match_data(&pdev->dev);
  710. BUG_ON(npins > ARRAY_SIZE(pmic_mpp_groups));
  711. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  712. if (!state)
  713. return -ENOMEM;
  714. platform_set_drvdata(pdev, state);
  715. state->dev = &pdev->dev;
  716. state->map = dev_get_regmap(dev->parent, NULL);
  717. pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
  718. if (!pindesc)
  719. return -ENOMEM;
  720. pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
  721. if (!pads)
  722. return -ENOMEM;
  723. pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
  724. if (!pctrldesc)
  725. return -ENOMEM;
  726. pctrldesc->pctlops = &pmic_mpp_pinctrl_ops;
  727. pctrldesc->pmxops = &pmic_mpp_pinmux_ops;
  728. pctrldesc->confops = &pmic_mpp_pinconf_ops;
  729. pctrldesc->owner = THIS_MODULE;
  730. pctrldesc->name = dev_name(dev);
  731. pctrldesc->pins = pindesc;
  732. pctrldesc->npins = npins;
  733. pctrldesc->num_custom_params = ARRAY_SIZE(pmic_mpp_bindings);
  734. pctrldesc->custom_params = pmic_mpp_bindings;
  735. #ifdef CONFIG_DEBUG_FS
  736. pctrldesc->custom_conf_items = pmic_conf_items;
  737. #endif
  738. for (i = 0; i < npins; i++, pindesc++) {
  739. pad = &pads[i];
  740. pindesc->drv_data = pad;
  741. pindesc->number = i;
  742. pindesc->name = pmic_mpp_groups[i];
  743. pad->base = reg + i * PMIC_MPP_ADDRESS_RANGE;
  744. ret = pmic_mpp_populate(state, pad);
  745. if (ret < 0)
  746. return ret;
  747. }
  748. state->chip = pmic_mpp_gpio_template;
  749. state->chip.parent = dev;
  750. state->chip.base = -1;
  751. state->chip.ngpio = npins;
  752. state->chip.label = dev_name(dev);
  753. state->chip.of_gpio_n_cells = 2;
  754. state->chip.can_sleep = false;
  755. state->ctrl = devm_pinctrl_register(dev, pctrldesc, state);
  756. if (IS_ERR(state->ctrl))
  757. return PTR_ERR(state->ctrl);
  758. parent_node = of_irq_find_parent(state->dev->of_node);
  759. if (!parent_node)
  760. return -ENXIO;
  761. parent_domain = irq_find_host(parent_node);
  762. of_node_put(parent_node);
  763. if (!parent_domain)
  764. return -ENXIO;
  765. state->irq.name = "spmi-mpp",
  766. state->irq.irq_ack = irq_chip_ack_parent,
  767. state->irq.irq_mask = irq_chip_mask_parent,
  768. state->irq.irq_unmask = irq_chip_unmask_parent,
  769. state->irq.irq_set_type = irq_chip_set_type_parent,
  770. state->irq.irq_set_wake = irq_chip_set_wake_parent,
  771. state->irq.flags = IRQCHIP_MASK_ON_SUSPEND,
  772. girq = &state->chip.irq;
  773. girq->chip = &state->irq;
  774. girq->default_type = IRQ_TYPE_NONE;
  775. girq->handler = handle_level_irq;
  776. girq->fwnode = of_node_to_fwnode(state->dev->of_node);
  777. girq->parent_domain = parent_domain;
  778. girq->child_to_parent_hwirq = pmic_mpp_child_to_parent_hwirq;
  779. girq->populate_parent_alloc_arg = gpiochip_populate_parent_fwspec_fourcell;
  780. girq->child_offset_to_irq = pmic_mpp_child_offset_to_irq;
  781. girq->child_irq_domain_ops.translate = pmic_mpp_domain_translate;
  782. ret = gpiochip_add_data(&state->chip, state);
  783. if (ret) {
  784. dev_err(state->dev, "can't add gpio chip\n");
  785. return ret;
  786. }
  787. ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0, npins);
  788. if (ret) {
  789. dev_err(dev, "failed to add pin range\n");
  790. goto err_range;
  791. }
  792. return 0;
  793. err_range:
  794. gpiochip_remove(&state->chip);
  795. return ret;
  796. }
  797. static int pmic_mpp_remove(struct platform_device *pdev)
  798. {
  799. struct pmic_mpp_state *state = platform_get_drvdata(pdev);
  800. gpiochip_remove(&state->chip);
  801. return 0;
  802. }
  803. static const struct of_device_id pmic_mpp_of_match[] = {
  804. { .compatible = "qcom,pm8019-mpp", .data = (void *) 6 },
  805. { .compatible = "qcom,pm8226-mpp", .data = (void *) 8 },
  806. { .compatible = "qcom,pm8841-mpp", .data = (void *) 4 },
  807. { .compatible = "qcom,pm8916-mpp", .data = (void *) 4 },
  808. { .compatible = "qcom,pm8941-mpp", .data = (void *) 8 },
  809. { .compatible = "qcom,pm8950-mpp", .data = (void *) 4 },
  810. { .compatible = "qcom,pmi8950-mpp", .data = (void *) 4 },
  811. { .compatible = "qcom,pm8994-mpp", .data = (void *) 8 },
  812. { .compatible = "qcom,pma8084-mpp", .data = (void *) 8 },
  813. { .compatible = "qcom,pmi8994-mpp", .data = (void *) 4 },
  814. { },
  815. };
  816. MODULE_DEVICE_TABLE(of, pmic_mpp_of_match);
  817. static struct platform_driver pmic_mpp_driver = {
  818. .driver = {
  819. .name = "qcom-spmi-mpp",
  820. .of_match_table = pmic_mpp_of_match,
  821. },
  822. .probe = pmic_mpp_probe,
  823. .remove = pmic_mpp_remove,
  824. };
  825. module_platform_driver(pmic_mpp_driver);
  826. MODULE_AUTHOR("Ivan T. Ivanov <[email protected]>");
  827. MODULE_DESCRIPTION("Qualcomm SPMI PMIC MPP pin control driver");
  828. MODULE_ALIAS("platform:qcom-spmi-mpp");
  829. MODULE_LICENSE("GPL v2");