pinctrl-spmi-mpp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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/platform_device.h>
  10. #include <linux/regmap.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/pinctrl/pinconf-generic.h>
  15. #include <linux/pinctrl/pinconf.h>
  16. #include <linux/pinctrl/pinmux.h>
  17. #include <dt-bindings/pinctrl/qcom,pmic-mpp.h>
  18. #include "../core.h"
  19. #include "../pinctrl-utils.h"
  20. #define PMIC_MPP_ADDRESS_RANGE 0x100
  21. /*
  22. * Pull Up Values - it indicates whether a pull-up should be
  23. * applied for bidirectional mode only. The hardware ignores the
  24. * configuration when operating in other modes.
  25. */
  26. #define PMIC_MPP_PULL_UP_0P6KOHM 0
  27. #define PMIC_MPP_PULL_UP_10KOHM 1
  28. #define PMIC_MPP_PULL_UP_30KOHM 2
  29. #define PMIC_MPP_PULL_UP_OPEN 3
  30. /* type registers base address bases */
  31. #define PMIC_MPP_REG_TYPE 0x4
  32. #define PMIC_MPP_REG_SUBTYPE 0x5
  33. /* mpp peripheral type and subtype values */
  34. #define PMIC_MPP_TYPE 0x11
  35. #define PMIC_MPP_SUBTYPE_4CH_NO_ANA_OUT 0x3
  36. #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_ANA_OUT 0x4
  37. #define PMIC_MPP_SUBTYPE_4CH_NO_SINK 0x5
  38. #define PMIC_MPP_SUBTYPE_ULT_4CH_NO_SINK 0x6
  39. #define PMIC_MPP_SUBTYPE_4CH_FULL_FUNC 0x7
  40. #define PMIC_MPP_SUBTYPE_8CH_FULL_FUNC 0xf
  41. #define PMIC_MPP_REG_RT_STS 0x10
  42. #define PMIC_MPP_REG_RT_STS_VAL_MASK 0x1
  43. /* control register base address bases */
  44. #define PMIC_MPP_REG_MODE_CTL 0x40
  45. #define PMIC_MPP_REG_DIG_VIN_CTL 0x41
  46. #define PMIC_MPP_REG_DIG_PULL_CTL 0x42
  47. #define PMIC_MPP_REG_DIG_IN_CTL 0x43
  48. #define PMIC_MPP_REG_EN_CTL 0x46
  49. #define PMIC_MPP_REG_AOUT_CTL 0x48
  50. #define PMIC_MPP_REG_AIN_CTL 0x4a
  51. #define PMIC_MPP_REG_SINK_CTL 0x4c
  52. /* PMIC_MPP_REG_MODE_CTL */
  53. #define PMIC_MPP_REG_MODE_VALUE_MASK 0x1
  54. #define PMIC_MPP_REG_MODE_FUNCTION_SHIFT 1
  55. #define PMIC_MPP_REG_MODE_FUNCTION_MASK 0x7
  56. #define PMIC_MPP_REG_MODE_DIR_SHIFT 4
  57. #define PMIC_MPP_REG_MODE_DIR_MASK 0x7
  58. /* PMIC_MPP_REG_DIG_VIN_CTL */
  59. #define PMIC_MPP_REG_VIN_SHIFT 0
  60. #define PMIC_MPP_REG_VIN_MASK 0x7
  61. /* PMIC_MPP_REG_DIG_PULL_CTL */
  62. #define PMIC_MPP_REG_PULL_SHIFT 0
  63. #define PMIC_MPP_REG_PULL_MASK 0x7
  64. /* PMIC_MPP_REG_EN_CTL */
  65. #define PMIC_MPP_REG_MASTER_EN_SHIFT 7
  66. /* PMIC_MPP_REG_AIN_CTL */
  67. #define PMIC_MPP_REG_AIN_ROUTE_SHIFT 0
  68. #define PMIC_MPP_REG_AIN_ROUTE_MASK 0x7
  69. #define PMIC_MPP_MODE_DIGITAL_INPUT 0
  70. #define PMIC_MPP_MODE_DIGITAL_OUTPUT 1
  71. #define PMIC_MPP_MODE_DIGITAL_BIDIR 2
  72. #define PMIC_MPP_MODE_ANALOG_BIDIR 3
  73. #define PMIC_MPP_MODE_ANALOG_INPUT 4
  74. #define PMIC_MPP_MODE_ANALOG_OUTPUT 5
  75. #define PMIC_MPP_MODE_CURRENT_SINK 6
  76. #define PMIC_MPP_SELECTOR_NORMAL 0
  77. #define PMIC_MPP_SELECTOR_PAIRED 1
  78. #define PMIC_MPP_SELECTOR_DTEST_FIRST 4
  79. #define PMIC_MPP_PHYSICAL_OFFSET 1
  80. /* Qualcomm specific pin configurations */
  81. #define PMIC_MPP_CONF_AMUX_ROUTE (PIN_CONFIG_END + 1)
  82. #define PMIC_MPP_CONF_ANALOG_LEVEL (PIN_CONFIG_END + 2)
  83. #define PMIC_MPP_CONF_DTEST_SELECTOR (PIN_CONFIG_END + 3)
  84. #define PMIC_MPP_CONF_PAIRED (PIN_CONFIG_END + 4)
  85. /**
  86. * struct pmic_mpp_pad - keep current MPP settings
  87. * @base: Address base in SPMI device.
  88. * @is_enabled: Set to false when MPP should be put in high Z state.
  89. * @out_value: Cached pin output value.
  90. * @output_enabled: Set to true if MPP output logic is enabled.
  91. * @input_enabled: Set to true if MPP input buffer logic is enabled.
  92. * @paired: Pin operates in paired mode
  93. * @has_pullup: Pin has support to configure pullup
  94. * @num_sources: Number of power-sources supported by this MPP.
  95. * @power_source: Current power-source used.
  96. * @amux_input: Set the source for analog input.
  97. * @aout_level: Analog output level
  98. * @pullup: Pullup resistor value. Valid in Bidirectional mode only.
  99. * @function: See pmic_mpp_functions[].
  100. * @drive_strength: Amount of current in sink mode
  101. * @dtest: DTEST route selector
  102. */
  103. struct pmic_mpp_pad {
  104. u16 base;
  105. bool is_enabled;
  106. bool out_value;
  107. bool output_enabled;
  108. bool input_enabled;
  109. bool paired;
  110. bool has_pullup;
  111. unsigned int num_sources;
  112. unsigned int power_source;
  113. unsigned int amux_input;
  114. unsigned int aout_level;
  115. unsigned int pullup;
  116. unsigned int function;
  117. unsigned int drive_strength;
  118. unsigned int dtest;
  119. };
  120. struct pmic_mpp_state {
  121. struct device *dev;
  122. struct regmap *map;
  123. struct pinctrl_dev *ctrl;
  124. struct gpio_chip chip;
  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 void pmic_mpp_irq_mask(struct irq_data *d)
  693. {
  694. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  695. irq_chip_mask_parent(d);
  696. gpiochip_disable_irq(gc, irqd_to_hwirq(d));
  697. }
  698. static void pmic_mpp_irq_unmask(struct irq_data *d)
  699. {
  700. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  701. gpiochip_enable_irq(gc, irqd_to_hwirq(d));
  702. irq_chip_unmask_parent(d);
  703. }
  704. static const struct irq_chip pmic_mpp_irq_chip = {
  705. .name = "spmi-mpp",
  706. .irq_ack = irq_chip_ack_parent,
  707. .irq_mask = pmic_mpp_irq_mask,
  708. .irq_unmask = pmic_mpp_irq_unmask,
  709. .irq_set_type = irq_chip_set_type_parent,
  710. .irq_set_wake = irq_chip_set_wake_parent,
  711. .flags = IRQCHIP_MASK_ON_SUSPEND | IRQCHIP_IMMUTABLE,
  712. GPIOCHIP_IRQ_RESOURCE_HELPERS,
  713. };
  714. static int pmic_mpp_probe(struct platform_device *pdev)
  715. {
  716. struct irq_domain *parent_domain;
  717. struct device_node *parent_node;
  718. struct device *dev = &pdev->dev;
  719. struct pinctrl_pin_desc *pindesc;
  720. struct pinctrl_desc *pctrldesc;
  721. struct pmic_mpp_pad *pad, *pads;
  722. struct pmic_mpp_state *state;
  723. struct gpio_irq_chip *girq;
  724. int ret, npins, i;
  725. u32 reg;
  726. ret = of_property_read_u32(dev->of_node, "reg", &reg);
  727. if (ret < 0) {
  728. dev_err(dev, "missing base address");
  729. return ret;
  730. }
  731. npins = (uintptr_t) device_get_match_data(&pdev->dev);
  732. BUG_ON(npins > ARRAY_SIZE(pmic_mpp_groups));
  733. state = devm_kzalloc(dev, sizeof(*state), GFP_KERNEL);
  734. if (!state)
  735. return -ENOMEM;
  736. platform_set_drvdata(pdev, state);
  737. state->dev = &pdev->dev;
  738. state->map = dev_get_regmap(dev->parent, NULL);
  739. pindesc = devm_kcalloc(dev, npins, sizeof(*pindesc), GFP_KERNEL);
  740. if (!pindesc)
  741. return -ENOMEM;
  742. pads = devm_kcalloc(dev, npins, sizeof(*pads), GFP_KERNEL);
  743. if (!pads)
  744. return -ENOMEM;
  745. pctrldesc = devm_kzalloc(dev, sizeof(*pctrldesc), GFP_KERNEL);
  746. if (!pctrldesc)
  747. return -ENOMEM;
  748. pctrldesc->pctlops = &pmic_mpp_pinctrl_ops;
  749. pctrldesc->pmxops = &pmic_mpp_pinmux_ops;
  750. pctrldesc->confops = &pmic_mpp_pinconf_ops;
  751. pctrldesc->owner = THIS_MODULE;
  752. pctrldesc->name = dev_name(dev);
  753. pctrldesc->pins = pindesc;
  754. pctrldesc->npins = npins;
  755. pctrldesc->num_custom_params = ARRAY_SIZE(pmic_mpp_bindings);
  756. pctrldesc->custom_params = pmic_mpp_bindings;
  757. #ifdef CONFIG_DEBUG_FS
  758. pctrldesc->custom_conf_items = pmic_conf_items;
  759. #endif
  760. for (i = 0; i < npins; i++, pindesc++) {
  761. pad = &pads[i];
  762. pindesc->drv_data = pad;
  763. pindesc->number = i;
  764. pindesc->name = pmic_mpp_groups[i];
  765. pad->base = reg + i * PMIC_MPP_ADDRESS_RANGE;
  766. ret = pmic_mpp_populate(state, pad);
  767. if (ret < 0)
  768. return ret;
  769. }
  770. state->chip = pmic_mpp_gpio_template;
  771. state->chip.parent = dev;
  772. state->chip.base = -1;
  773. state->chip.ngpio = npins;
  774. state->chip.label = dev_name(dev);
  775. state->chip.of_gpio_n_cells = 2;
  776. state->chip.can_sleep = false;
  777. state->ctrl = devm_pinctrl_register(dev, pctrldesc, state);
  778. if (IS_ERR(state->ctrl))
  779. return PTR_ERR(state->ctrl);
  780. parent_node = of_irq_find_parent(state->dev->of_node);
  781. if (!parent_node)
  782. return -ENXIO;
  783. parent_domain = irq_find_host(parent_node);
  784. of_node_put(parent_node);
  785. if (!parent_domain)
  786. return -ENXIO;
  787. girq = &state->chip.irq;
  788. gpio_irq_chip_set_chip(girq, &pmic_mpp_irq_chip);
  789. girq->default_type = IRQ_TYPE_NONE;
  790. girq->handler = handle_level_irq;
  791. girq->fwnode = dev_fwnode(state->dev);
  792. girq->parent_domain = parent_domain;
  793. girq->child_to_parent_hwirq = pmic_mpp_child_to_parent_hwirq;
  794. girq->populate_parent_alloc_arg = gpiochip_populate_parent_fwspec_fourcell;
  795. girq->child_offset_to_irq = pmic_mpp_child_offset_to_irq;
  796. girq->child_irq_domain_ops.translate = pmic_mpp_domain_translate;
  797. ret = gpiochip_add_data(&state->chip, state);
  798. if (ret) {
  799. dev_err(state->dev, "can't add gpio chip\n");
  800. return ret;
  801. }
  802. ret = gpiochip_add_pin_range(&state->chip, dev_name(dev), 0, 0, npins);
  803. if (ret) {
  804. dev_err(dev, "failed to add pin range\n");
  805. goto err_range;
  806. }
  807. return 0;
  808. err_range:
  809. gpiochip_remove(&state->chip);
  810. return ret;
  811. }
  812. static void pmic_mpp_remove(struct platform_device *pdev)
  813. {
  814. struct pmic_mpp_state *state = platform_get_drvdata(pdev);
  815. gpiochip_remove(&state->chip);
  816. }
  817. static const struct of_device_id pmic_mpp_of_match[] = {
  818. { .compatible = "qcom,pm8019-mpp", .data = (void *) 6 },
  819. { .compatible = "qcom,pm8226-mpp", .data = (void *) 8 },
  820. { .compatible = "qcom,pm8841-mpp", .data = (void *) 4 },
  821. { .compatible = "qcom,pm8916-mpp", .data = (void *) 4 },
  822. { .compatible = "qcom,pm8937-mpp", .data = (void *) 4 },
  823. { .compatible = "qcom,pm8941-mpp", .data = (void *) 8 },
  824. { .compatible = "qcom,pm8950-mpp", .data = (void *) 4 },
  825. { .compatible = "qcom,pmi8950-mpp", .data = (void *) 4 },
  826. { .compatible = "qcom,pm8994-mpp", .data = (void *) 8 },
  827. { .compatible = "qcom,pma8084-mpp", .data = (void *) 8 },
  828. { .compatible = "qcom,pmi8994-mpp", .data = (void *) 4 },
  829. { },
  830. };
  831. MODULE_DEVICE_TABLE(of, pmic_mpp_of_match);
  832. static struct platform_driver pmic_mpp_driver = {
  833. .driver = {
  834. .name = "qcom-spmi-mpp",
  835. .of_match_table = pmic_mpp_of_match,
  836. },
  837. .probe = pmic_mpp_probe,
  838. .remove_new = pmic_mpp_remove,
  839. };
  840. module_platform_driver(pmic_mpp_driver);
  841. MODULE_AUTHOR("Ivan T. Ivanov <iivanov@mm-sol.com>");
  842. MODULE_DESCRIPTION("Qualcomm SPMI PMIC MPP pin control driver");
  843. MODULE_ALIAS("platform:qcom-spmi-mpp");
  844. MODULE_LICENSE("GPL v2");