pinmux.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /*
  2. * linux/arch/arm/mach-tegra/pinmux.c
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/io.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/of_device.h>
  24. #include <mach/iomap.h>
  25. #include <mach/pinmux.h>
  26. #define HSM_EN(reg) (((reg) >> 2) & 0x1)
  27. #define SCHMT_EN(reg) (((reg) >> 3) & 0x1)
  28. #define LPMD(reg) (((reg) >> 4) & 0x3)
  29. #define DRVDN(reg) (((reg) >> 12) & 0x1f)
  30. #define DRVUP(reg) (((reg) >> 20) & 0x1f)
  31. #define SLWR(reg) (((reg) >> 28) & 0x3)
  32. #define SLWF(reg) (((reg) >> 30) & 0x3)
  33. static const struct tegra_pingroup_desc *pingroups;
  34. static const struct tegra_drive_pingroup_desc *drive_pingroups;
  35. static int pingroup_max;
  36. static int drive_max;
  37. static char *tegra_mux_names[TEGRA_MAX_MUX] = {
  38. [TEGRA_MUX_AHB_CLK] = "AHB_CLK",
  39. [TEGRA_MUX_APB_CLK] = "APB_CLK",
  40. [TEGRA_MUX_AUDIO_SYNC] = "AUDIO_SYNC",
  41. [TEGRA_MUX_CRT] = "CRT",
  42. [TEGRA_MUX_DAP1] = "DAP1",
  43. [TEGRA_MUX_DAP2] = "DAP2",
  44. [TEGRA_MUX_DAP3] = "DAP3",
  45. [TEGRA_MUX_DAP4] = "DAP4",
  46. [TEGRA_MUX_DAP5] = "DAP5",
  47. [TEGRA_MUX_DISPLAYA] = "DISPLAYA",
  48. [TEGRA_MUX_DISPLAYB] = "DISPLAYB",
  49. [TEGRA_MUX_EMC_TEST0_DLL] = "EMC_TEST0_DLL",
  50. [TEGRA_MUX_EMC_TEST1_DLL] = "EMC_TEST1_DLL",
  51. [TEGRA_MUX_GMI] = "GMI",
  52. [TEGRA_MUX_GMI_INT] = "GMI_INT",
  53. [TEGRA_MUX_HDMI] = "HDMI",
  54. [TEGRA_MUX_I2C] = "I2C",
  55. [TEGRA_MUX_I2C2] = "I2C2",
  56. [TEGRA_MUX_I2C3] = "I2C3",
  57. [TEGRA_MUX_IDE] = "IDE",
  58. [TEGRA_MUX_IRDA] = "IRDA",
  59. [TEGRA_MUX_KBC] = "KBC",
  60. [TEGRA_MUX_MIO] = "MIO",
  61. [TEGRA_MUX_MIPI_HS] = "MIPI_HS",
  62. [TEGRA_MUX_NAND] = "NAND",
  63. [TEGRA_MUX_OSC] = "OSC",
  64. [TEGRA_MUX_OWR] = "OWR",
  65. [TEGRA_MUX_PCIE] = "PCIE",
  66. [TEGRA_MUX_PLLA_OUT] = "PLLA_OUT",
  67. [TEGRA_MUX_PLLC_OUT1] = "PLLC_OUT1",
  68. [TEGRA_MUX_PLLM_OUT1] = "PLLM_OUT1",
  69. [TEGRA_MUX_PLLP_OUT2] = "PLLP_OUT2",
  70. [TEGRA_MUX_PLLP_OUT3] = "PLLP_OUT3",
  71. [TEGRA_MUX_PLLP_OUT4] = "PLLP_OUT4",
  72. [TEGRA_MUX_PWM] = "PWM",
  73. [TEGRA_MUX_PWR_INTR] = "PWR_INTR",
  74. [TEGRA_MUX_PWR_ON] = "PWR_ON",
  75. [TEGRA_MUX_RTCK] = "RTCK",
  76. [TEGRA_MUX_SDIO1] = "SDIO1",
  77. [TEGRA_MUX_SDIO2] = "SDIO2",
  78. [TEGRA_MUX_SDIO3] = "SDIO3",
  79. [TEGRA_MUX_SDIO4] = "SDIO4",
  80. [TEGRA_MUX_SFLASH] = "SFLASH",
  81. [TEGRA_MUX_SPDIF] = "SPDIF",
  82. [TEGRA_MUX_SPI1] = "SPI1",
  83. [TEGRA_MUX_SPI2] = "SPI2",
  84. [TEGRA_MUX_SPI2_ALT] = "SPI2_ALT",
  85. [TEGRA_MUX_SPI3] = "SPI3",
  86. [TEGRA_MUX_SPI4] = "SPI4",
  87. [TEGRA_MUX_TRACE] = "TRACE",
  88. [TEGRA_MUX_TWC] = "TWC",
  89. [TEGRA_MUX_UARTA] = "UARTA",
  90. [TEGRA_MUX_UARTB] = "UARTB",
  91. [TEGRA_MUX_UARTC] = "UARTC",
  92. [TEGRA_MUX_UARTD] = "UARTD",
  93. [TEGRA_MUX_UARTE] = "UARTE",
  94. [TEGRA_MUX_ULPI] = "ULPI",
  95. [TEGRA_MUX_VI] = "VI",
  96. [TEGRA_MUX_VI_SENSOR_CLK] = "VI_SENSOR_CLK",
  97. [TEGRA_MUX_XIO] = "XIO",
  98. [TEGRA_MUX_BLINK] = "BLINK",
  99. [TEGRA_MUX_CEC] = "CEC",
  100. [TEGRA_MUX_CLK12] = "CLK12",
  101. [TEGRA_MUX_DAP] = "DAP",
  102. [TEGRA_MUX_DAPSDMMC2] = "DAPSDMMC2",
  103. [TEGRA_MUX_DDR] = "DDR",
  104. [TEGRA_MUX_DEV3] = "DEV3",
  105. [TEGRA_MUX_DTV] = "DTV",
  106. [TEGRA_MUX_VI_ALT1] = "VI_ALT1",
  107. [TEGRA_MUX_VI_ALT2] = "VI_ALT2",
  108. [TEGRA_MUX_VI_ALT3] = "VI_ALT3",
  109. [TEGRA_MUX_EMC_DLL] = "EMC_DLL",
  110. [TEGRA_MUX_EXTPERIPH1] = "EXTPERIPH1",
  111. [TEGRA_MUX_EXTPERIPH2] = "EXTPERIPH2",
  112. [TEGRA_MUX_EXTPERIPH3] = "EXTPERIPH3",
  113. [TEGRA_MUX_GMI_ALT] = "GMI_ALT",
  114. [TEGRA_MUX_HDA] = "HDA",
  115. [TEGRA_MUX_HSI] = "HSI",
  116. [TEGRA_MUX_I2C4] = "I2C4",
  117. [TEGRA_MUX_I2C5] = "I2C5",
  118. [TEGRA_MUX_I2CPWR] = "I2CPWR",
  119. [TEGRA_MUX_I2S0] = "I2S0",
  120. [TEGRA_MUX_I2S1] = "I2S1",
  121. [TEGRA_MUX_I2S2] = "I2S2",
  122. [TEGRA_MUX_I2S3] = "I2S3",
  123. [TEGRA_MUX_I2S4] = "I2S4",
  124. [TEGRA_MUX_NAND_ALT] = "NAND_ALT",
  125. [TEGRA_MUX_POPSDIO4] = "POPSDIO4",
  126. [TEGRA_MUX_POPSDMMC4] = "POPSDMMC4",
  127. [TEGRA_MUX_PWM0] = "PWM0",
  128. [TEGRA_MUX_PWM1] = "PWM2",
  129. [TEGRA_MUX_PWM2] = "PWM2",
  130. [TEGRA_MUX_PWM3] = "PWM3",
  131. [TEGRA_MUX_SATA] = "SATA",
  132. [TEGRA_MUX_SPI5] = "SPI5",
  133. [TEGRA_MUX_SPI6] = "SPI6",
  134. [TEGRA_MUX_SYSCLK] = "SYSCLK",
  135. [TEGRA_MUX_VGP1] = "VGP1",
  136. [TEGRA_MUX_VGP2] = "VGP2",
  137. [TEGRA_MUX_VGP3] = "VGP3",
  138. [TEGRA_MUX_VGP4] = "VGP4",
  139. [TEGRA_MUX_VGP5] = "VGP5",
  140. [TEGRA_MUX_VGP6] = "VGP6",
  141. [TEGRA_MUX_SAFE] = "<safe>",
  142. };
  143. static const char *tegra_drive_names[TEGRA_MAX_DRIVE] = {
  144. [TEGRA_DRIVE_DIV_8] = "DIV_8",
  145. [TEGRA_DRIVE_DIV_4] = "DIV_4",
  146. [TEGRA_DRIVE_DIV_2] = "DIV_2",
  147. [TEGRA_DRIVE_DIV_1] = "DIV_1",
  148. };
  149. static const char *tegra_slew_names[TEGRA_MAX_SLEW] = {
  150. [TEGRA_SLEW_FASTEST] = "FASTEST",
  151. [TEGRA_SLEW_FAST] = "FAST",
  152. [TEGRA_SLEW_SLOW] = "SLOW",
  153. [TEGRA_SLEW_SLOWEST] = "SLOWEST",
  154. };
  155. static DEFINE_SPINLOCK(mux_lock);
  156. static const char *pingroup_name(int pg)
  157. {
  158. if (pg < 0 || pg >= pingroup_max)
  159. return "<UNKNOWN>";
  160. return pingroups[pg].name;
  161. }
  162. static const char *func_name(enum tegra_mux_func func)
  163. {
  164. if (func == TEGRA_MUX_RSVD1)
  165. return "RSVD1";
  166. if (func == TEGRA_MUX_RSVD2)
  167. return "RSVD2";
  168. if (func == TEGRA_MUX_RSVD3)
  169. return "RSVD3";
  170. if (func == TEGRA_MUX_RSVD4)
  171. return "RSVD4";
  172. if (func == TEGRA_MUX_NONE)
  173. return "NONE";
  174. if (func < 0 || func >= TEGRA_MAX_MUX)
  175. return "<UNKNOWN>";
  176. return tegra_mux_names[func];
  177. }
  178. static const char *tri_name(unsigned long val)
  179. {
  180. return val ? "TRISTATE" : "NORMAL";
  181. }
  182. static const char *pupd_name(unsigned long val)
  183. {
  184. switch (val) {
  185. case 0:
  186. return "NORMAL";
  187. case 1:
  188. return "PULL_DOWN";
  189. case 2:
  190. return "PULL_UP";
  191. default:
  192. return "RSVD";
  193. }
  194. }
  195. static int nbanks;
  196. static void __iomem **regs;
  197. static inline u32 pg_readl(u32 bank, u32 reg)
  198. {
  199. return readl(regs[bank] + reg);
  200. }
  201. static inline void pg_writel(u32 val, u32 bank, u32 reg)
  202. {
  203. writel(val, regs[bank] + reg);
  204. }
  205. static int tegra_pinmux_set_func(const struct tegra_pingroup_config *config)
  206. {
  207. int mux = -1;
  208. int i;
  209. unsigned long reg;
  210. unsigned long flags;
  211. int pg = config->pingroup;
  212. enum tegra_mux_func func = config->func;
  213. if (pg < 0 || pg >= pingroup_max)
  214. return -ERANGE;
  215. if (pingroups[pg].mux_reg < 0)
  216. return -EINVAL;
  217. if (func < 0)
  218. return -ERANGE;
  219. if (func == TEGRA_MUX_SAFE)
  220. func = pingroups[pg].func_safe;
  221. if (func & TEGRA_MUX_RSVD) {
  222. mux = func & 0x3;
  223. } else {
  224. for (i = 0; i < 4; i++) {
  225. if (pingroups[pg].funcs[i] == func) {
  226. mux = i;
  227. break;
  228. }
  229. }
  230. }
  231. if (mux < 0)
  232. return -EINVAL;
  233. spin_lock_irqsave(&mux_lock, flags);
  234. reg = pg_readl(pingroups[pg].mux_bank, pingroups[pg].mux_reg);
  235. reg &= ~(0x3 << pingroups[pg].mux_bit);
  236. reg |= mux << pingroups[pg].mux_bit;
  237. pg_writel(reg, pingroups[pg].mux_bank, pingroups[pg].mux_reg);
  238. spin_unlock_irqrestore(&mux_lock, flags);
  239. return 0;
  240. }
  241. int tegra_pinmux_set_tristate(int pg, enum tegra_tristate tristate)
  242. {
  243. unsigned long reg;
  244. unsigned long flags;
  245. if (pg < 0 || pg >= pingroup_max)
  246. return -ERANGE;
  247. if (pingroups[pg].tri_reg < 0)
  248. return -EINVAL;
  249. spin_lock_irqsave(&mux_lock, flags);
  250. reg = pg_readl(pingroups[pg].tri_bank, pingroups[pg].tri_reg);
  251. reg &= ~(0x1 << pingroups[pg].tri_bit);
  252. if (tristate)
  253. reg |= 1 << pingroups[pg].tri_bit;
  254. pg_writel(reg, pingroups[pg].tri_bank, pingroups[pg].tri_reg);
  255. spin_unlock_irqrestore(&mux_lock, flags);
  256. return 0;
  257. }
  258. int tegra_pinmux_set_pullupdown(int pg, enum tegra_pullupdown pupd)
  259. {
  260. unsigned long reg;
  261. unsigned long flags;
  262. if (pg < 0 || pg >= pingroup_max)
  263. return -ERANGE;
  264. if (pingroups[pg].pupd_reg < 0)
  265. return -EINVAL;
  266. if (pupd != TEGRA_PUPD_NORMAL &&
  267. pupd != TEGRA_PUPD_PULL_DOWN &&
  268. pupd != TEGRA_PUPD_PULL_UP)
  269. return -EINVAL;
  270. spin_lock_irqsave(&mux_lock, flags);
  271. reg = pg_readl(pingroups[pg].pupd_bank, pingroups[pg].pupd_reg);
  272. reg &= ~(0x3 << pingroups[pg].pupd_bit);
  273. reg |= pupd << pingroups[pg].pupd_bit;
  274. pg_writel(reg, pingroups[pg].pupd_bank, pingroups[pg].pupd_reg);
  275. spin_unlock_irqrestore(&mux_lock, flags);
  276. return 0;
  277. }
  278. static void tegra_pinmux_config_pingroup(const struct tegra_pingroup_config *config)
  279. {
  280. int pingroup = config->pingroup;
  281. enum tegra_mux_func func = config->func;
  282. enum tegra_pullupdown pupd = config->pupd;
  283. enum tegra_tristate tristate = config->tristate;
  284. int err;
  285. if (pingroups[pingroup].mux_reg >= 0) {
  286. err = tegra_pinmux_set_func(config);
  287. if (err < 0)
  288. pr_err("pinmux: can't set pingroup %s func to %s: %d\n",
  289. pingroup_name(pingroup), func_name(func), err);
  290. }
  291. if (pingroups[pingroup].pupd_reg >= 0) {
  292. err = tegra_pinmux_set_pullupdown(pingroup, pupd);
  293. if (err < 0)
  294. pr_err("pinmux: can't set pingroup %s pullupdown to %s: %d\n",
  295. pingroup_name(pingroup), pupd_name(pupd), err);
  296. }
  297. if (pingroups[pingroup].tri_reg >= 0) {
  298. err = tegra_pinmux_set_tristate(pingroup, tristate);
  299. if (err < 0)
  300. pr_err("pinmux: can't set pingroup %s tristate to %s: %d\n",
  301. pingroup_name(pingroup), tri_name(func), err);
  302. }
  303. }
  304. void tegra_pinmux_config_table(const struct tegra_pingroup_config *config, int len)
  305. {
  306. int i;
  307. for (i = 0; i < len; i++)
  308. tegra_pinmux_config_pingroup(&config[i]);
  309. }
  310. static const char *drive_pinmux_name(int pg)
  311. {
  312. if (pg < 0 || pg >= drive_max)
  313. return "<UNKNOWN>";
  314. return drive_pingroups[pg].name;
  315. }
  316. static const char *enable_name(unsigned long val)
  317. {
  318. return val ? "ENABLE" : "DISABLE";
  319. }
  320. static const char *drive_name(unsigned long val)
  321. {
  322. if (val >= TEGRA_MAX_DRIVE)
  323. return "<UNKNOWN>";
  324. return tegra_drive_names[val];
  325. }
  326. static const char *slew_name(unsigned long val)
  327. {
  328. if (val >= TEGRA_MAX_SLEW)
  329. return "<UNKNOWN>";
  330. return tegra_slew_names[val];
  331. }
  332. static int tegra_drive_pinmux_set_hsm(int pg, enum tegra_hsm hsm)
  333. {
  334. unsigned long flags;
  335. u32 reg;
  336. if (pg < 0 || pg >= drive_max)
  337. return -ERANGE;
  338. if (hsm != TEGRA_HSM_ENABLE && hsm != TEGRA_HSM_DISABLE)
  339. return -EINVAL;
  340. spin_lock_irqsave(&mux_lock, flags);
  341. reg = pg_readl(drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  342. if (hsm == TEGRA_HSM_ENABLE)
  343. reg |= (1 << 2);
  344. else
  345. reg &= ~(1 << 2);
  346. pg_writel(reg, drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  347. spin_unlock_irqrestore(&mux_lock, flags);
  348. return 0;
  349. }
  350. static int tegra_drive_pinmux_set_schmitt(int pg, enum tegra_schmitt schmitt)
  351. {
  352. unsigned long flags;
  353. u32 reg;
  354. if (pg < 0 || pg >= drive_max)
  355. return -ERANGE;
  356. if (schmitt != TEGRA_SCHMITT_ENABLE && schmitt != TEGRA_SCHMITT_DISABLE)
  357. return -EINVAL;
  358. spin_lock_irqsave(&mux_lock, flags);
  359. reg = pg_readl(drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  360. if (schmitt == TEGRA_SCHMITT_ENABLE)
  361. reg |= (1 << 3);
  362. else
  363. reg &= ~(1 << 3);
  364. pg_writel(reg, drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  365. spin_unlock_irqrestore(&mux_lock, flags);
  366. return 0;
  367. }
  368. static int tegra_drive_pinmux_set_drive(int pg, enum tegra_drive drive)
  369. {
  370. unsigned long flags;
  371. u32 reg;
  372. if (pg < 0 || pg >= drive_max)
  373. return -ERANGE;
  374. if (drive < 0 || drive >= TEGRA_MAX_DRIVE)
  375. return -EINVAL;
  376. spin_lock_irqsave(&mux_lock, flags);
  377. reg = pg_readl(drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  378. reg &= ~(0x3 << 4);
  379. reg |= drive << 4;
  380. pg_writel(reg, drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  381. spin_unlock_irqrestore(&mux_lock, flags);
  382. return 0;
  383. }
  384. static int tegra_drive_pinmux_set_pull_down(int pg,
  385. enum tegra_pull_strength pull_down)
  386. {
  387. unsigned long flags;
  388. u32 reg;
  389. if (pg < 0 || pg >= drive_max)
  390. return -ERANGE;
  391. if (pull_down < 0 || pull_down >= TEGRA_MAX_PULL)
  392. return -EINVAL;
  393. spin_lock_irqsave(&mux_lock, flags);
  394. reg = pg_readl(drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  395. reg &= ~(0x1f << 12);
  396. reg |= pull_down << 12;
  397. pg_writel(reg, drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  398. spin_unlock_irqrestore(&mux_lock, flags);
  399. return 0;
  400. }
  401. static int tegra_drive_pinmux_set_pull_up(int pg,
  402. enum tegra_pull_strength pull_up)
  403. {
  404. unsigned long flags;
  405. u32 reg;
  406. if (pg < 0 || pg >= drive_max)
  407. return -ERANGE;
  408. if (pull_up < 0 || pull_up >= TEGRA_MAX_PULL)
  409. return -EINVAL;
  410. spin_lock_irqsave(&mux_lock, flags);
  411. reg = pg_readl(drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  412. reg &= ~(0x1f << 12);
  413. reg |= pull_up << 12;
  414. pg_writel(reg, drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  415. spin_unlock_irqrestore(&mux_lock, flags);
  416. return 0;
  417. }
  418. static int tegra_drive_pinmux_set_slew_rising(int pg,
  419. enum tegra_slew slew_rising)
  420. {
  421. unsigned long flags;
  422. u32 reg;
  423. if (pg < 0 || pg >= drive_max)
  424. return -ERANGE;
  425. if (slew_rising < 0 || slew_rising >= TEGRA_MAX_SLEW)
  426. return -EINVAL;
  427. spin_lock_irqsave(&mux_lock, flags);
  428. reg = pg_readl(drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  429. reg &= ~(0x3 << 28);
  430. reg |= slew_rising << 28;
  431. pg_writel(reg, drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  432. spin_unlock_irqrestore(&mux_lock, flags);
  433. return 0;
  434. }
  435. static int tegra_drive_pinmux_set_slew_falling(int pg,
  436. enum tegra_slew slew_falling)
  437. {
  438. unsigned long flags;
  439. u32 reg;
  440. if (pg < 0 || pg >= drive_max)
  441. return -ERANGE;
  442. if (slew_falling < 0 || slew_falling >= TEGRA_MAX_SLEW)
  443. return -EINVAL;
  444. spin_lock_irqsave(&mux_lock, flags);
  445. reg = pg_readl(drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  446. reg &= ~(0x3 << 30);
  447. reg |= slew_falling << 30;
  448. pg_writel(reg, drive_pingroups[pg].reg_bank, drive_pingroups[pg].reg);
  449. spin_unlock_irqrestore(&mux_lock, flags);
  450. return 0;
  451. }
  452. static void tegra_drive_pinmux_config_pingroup(int pingroup,
  453. enum tegra_hsm hsm,
  454. enum tegra_schmitt schmitt,
  455. enum tegra_drive drive,
  456. enum tegra_pull_strength pull_down,
  457. enum tegra_pull_strength pull_up,
  458. enum tegra_slew slew_rising,
  459. enum tegra_slew slew_falling)
  460. {
  461. int err;
  462. err = tegra_drive_pinmux_set_hsm(pingroup, hsm);
  463. if (err < 0)
  464. pr_err("pinmux: can't set pingroup %s hsm to %s: %d\n",
  465. drive_pinmux_name(pingroup),
  466. enable_name(hsm), err);
  467. err = tegra_drive_pinmux_set_schmitt(pingroup, schmitt);
  468. if (err < 0)
  469. pr_err("pinmux: can't set pingroup %s schmitt to %s: %d\n",
  470. drive_pinmux_name(pingroup),
  471. enable_name(schmitt), err);
  472. err = tegra_drive_pinmux_set_drive(pingroup, drive);
  473. if (err < 0)
  474. pr_err("pinmux: can't set pingroup %s drive to %s: %d\n",
  475. drive_pinmux_name(pingroup),
  476. drive_name(drive), err);
  477. err = tegra_drive_pinmux_set_pull_down(pingroup, pull_down);
  478. if (err < 0)
  479. pr_err("pinmux: can't set pingroup %s pull down to %d: %d\n",
  480. drive_pinmux_name(pingroup),
  481. pull_down, err);
  482. err = tegra_drive_pinmux_set_pull_up(pingroup, pull_up);
  483. if (err < 0)
  484. pr_err("pinmux: can't set pingroup %s pull up to %d: %d\n",
  485. drive_pinmux_name(pingroup),
  486. pull_up, err);
  487. err = tegra_drive_pinmux_set_slew_rising(pingroup, slew_rising);
  488. if (err < 0)
  489. pr_err("pinmux: can't set pingroup %s rising slew to %s: %d\n",
  490. drive_pinmux_name(pingroup),
  491. slew_name(slew_rising), err);
  492. err = tegra_drive_pinmux_set_slew_falling(pingroup, slew_falling);
  493. if (err < 0)
  494. pr_err("pinmux: can't set pingroup %s falling slew to %s: %d\n",
  495. drive_pinmux_name(pingroup),
  496. slew_name(slew_falling), err);
  497. }
  498. void tegra_drive_pinmux_config_table(struct tegra_drive_pingroup_config *config,
  499. int len)
  500. {
  501. int i;
  502. for (i = 0; i < len; i++)
  503. tegra_drive_pinmux_config_pingroup(config[i].pingroup,
  504. config[i].hsm,
  505. config[i].schmitt,
  506. config[i].drive,
  507. config[i].pull_down,
  508. config[i].pull_up,
  509. config[i].slew_rising,
  510. config[i].slew_falling);
  511. }
  512. void tegra_pinmux_set_safe_pinmux_table(const struct tegra_pingroup_config *config,
  513. int len)
  514. {
  515. int i;
  516. struct tegra_pingroup_config c;
  517. for (i = 0; i < len; i++) {
  518. int err;
  519. c = config[i];
  520. if (c.pingroup < 0 || c.pingroup >= pingroup_max) {
  521. WARN_ON(1);
  522. continue;
  523. }
  524. c.func = pingroups[c.pingroup].func_safe;
  525. err = tegra_pinmux_set_func(&c);
  526. if (err < 0)
  527. pr_err("%s: tegra_pinmux_set_func returned %d setting "
  528. "%s to %s\n", __func__, err,
  529. pingroup_name(c.pingroup), func_name(c.func));
  530. }
  531. }
  532. void tegra_pinmux_config_pinmux_table(const struct tegra_pingroup_config *config,
  533. int len)
  534. {
  535. int i;
  536. for (i = 0; i < len; i++) {
  537. int err;
  538. if (config[i].pingroup < 0 ||
  539. config[i].pingroup >= pingroup_max) {
  540. WARN_ON(1);
  541. continue;
  542. }
  543. err = tegra_pinmux_set_func(&config[i]);
  544. if (err < 0)
  545. pr_err("%s: tegra_pinmux_set_func returned %d setting "
  546. "%s to %s\n", __func__, err,
  547. pingroup_name(config[i].pingroup),
  548. func_name(config[i].func));
  549. }
  550. }
  551. void tegra_pinmux_config_tristate_table(const struct tegra_pingroup_config *config,
  552. int len, enum tegra_tristate tristate)
  553. {
  554. int i;
  555. int err;
  556. int pingroup;
  557. for (i = 0; i < len; i++) {
  558. pingroup = config[i].pingroup;
  559. if (pingroups[pingroup].tri_reg >= 0) {
  560. err = tegra_pinmux_set_tristate(pingroup, tristate);
  561. if (err < 0)
  562. pr_err("pinmux: can't set pingroup %s tristate"
  563. " to %s: %d\n", pingroup_name(pingroup),
  564. tri_name(tristate), err);
  565. }
  566. }
  567. }
  568. void tegra_pinmux_config_pullupdown_table(const struct tegra_pingroup_config *config,
  569. int len, enum tegra_pullupdown pupd)
  570. {
  571. int i;
  572. int err;
  573. int pingroup;
  574. for (i = 0; i < len; i++) {
  575. pingroup = config[i].pingroup;
  576. if (pingroups[pingroup].pupd_reg >= 0) {
  577. err = tegra_pinmux_set_pullupdown(pingroup, pupd);
  578. if (err < 0)
  579. pr_err("pinmux: can't set pingroup %s pullupdown"
  580. " to %s: %d\n", pingroup_name(pingroup),
  581. pupd_name(pupd), err);
  582. }
  583. }
  584. }
  585. static struct of_device_id tegra_pinmux_of_match[] __devinitdata = {
  586. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  587. { .compatible = "nvidia,tegra20-pinmux", tegra20_pinmux_init },
  588. #endif
  589. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  590. { .compatible = "nvidia,tegra30-pinmux", tegra30_pinmux_init },
  591. #endif
  592. { },
  593. };
  594. static int __devinit tegra_pinmux_probe(struct platform_device *pdev)
  595. {
  596. struct resource *res;
  597. int i;
  598. int config_bad = 0;
  599. const struct of_device_id *match;
  600. match = of_match_device(tegra_pinmux_of_match, &pdev->dev);
  601. if (match)
  602. ((pinmux_init)(match->data))(&pingroups, &pingroup_max,
  603. &drive_pingroups, &drive_max);
  604. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  605. else
  606. /* no device tree available, so we must be on tegra20 */
  607. tegra20_pinmux_init(&pingroups, &pingroup_max,
  608. &drive_pingroups, &drive_max);
  609. #else
  610. pr_warn("non Tegra20 platform requires pinmux devicetree node\n");
  611. #endif
  612. for (i = 0; ; i++) {
  613. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  614. if (!res)
  615. break;
  616. }
  617. nbanks = i;
  618. for (i = 0; i < pingroup_max; i++) {
  619. if (pingroups[i].tri_bank >= nbanks) {
  620. dev_err(&pdev->dev, "pingroup %d: bad tri_bank\n", i);
  621. config_bad = 1;
  622. }
  623. if (pingroups[i].mux_bank >= nbanks) {
  624. dev_err(&pdev->dev, "pingroup %d: bad mux_bank\n", i);
  625. config_bad = 1;
  626. }
  627. if (pingroups[i].pupd_bank >= nbanks) {
  628. dev_err(&pdev->dev, "pingroup %d: bad pupd_bank\n", i);
  629. config_bad = 1;
  630. }
  631. }
  632. for (i = 0; i < drive_max; i++) {
  633. if (drive_pingroups[i].reg_bank >= nbanks) {
  634. dev_err(&pdev->dev,
  635. "drive pingroup %d: bad reg_bank\n", i);
  636. config_bad = 1;
  637. }
  638. }
  639. if (config_bad)
  640. return -ENODEV;
  641. regs = devm_kzalloc(&pdev->dev, nbanks * sizeof(*regs), GFP_KERNEL);
  642. if (!regs) {
  643. dev_err(&pdev->dev, "Can't alloc regs pointer\n");
  644. return -ENODEV;
  645. }
  646. for (i = 0; i < nbanks; i++) {
  647. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  648. if (!res) {
  649. dev_err(&pdev->dev, "Missing MEM resource\n");
  650. return -ENODEV;
  651. }
  652. if (!devm_request_mem_region(&pdev->dev, res->start,
  653. resource_size(res),
  654. dev_name(&pdev->dev))) {
  655. dev_err(&pdev->dev,
  656. "Couldn't request MEM resource %d\n", i);
  657. return -ENODEV;
  658. }
  659. regs[i] = devm_ioremap(&pdev->dev, res->start,
  660. resource_size(res));
  661. if (!regs) {
  662. dev_err(&pdev->dev, "Couldn't ioremap regs %d\n", i);
  663. return -ENODEV;
  664. }
  665. }
  666. return 0;
  667. }
  668. static struct platform_driver tegra_pinmux_driver = {
  669. .driver = {
  670. .name = "tegra-pinmux",
  671. .owner = THIS_MODULE,
  672. .of_match_table = tegra_pinmux_of_match,
  673. },
  674. .probe = tegra_pinmux_probe,
  675. };
  676. static int __init tegra_pinmux_init(void)
  677. {
  678. return platform_driver_register(&tegra_pinmux_driver);
  679. }
  680. postcore_initcall(tegra_pinmux_init);
  681. #ifdef CONFIG_DEBUG_FS
  682. #include <linux/debugfs.h>
  683. #include <linux/seq_file.h>
  684. static void dbg_pad_field(struct seq_file *s, int len)
  685. {
  686. seq_putc(s, ',');
  687. while (len-- > -1)
  688. seq_putc(s, ' ');
  689. }
  690. static int dbg_pinmux_show(struct seq_file *s, void *unused)
  691. {
  692. int i;
  693. int len;
  694. for (i = 0; i < pingroup_max; i++) {
  695. unsigned long reg;
  696. unsigned long tri;
  697. unsigned long mux;
  698. unsigned long pupd;
  699. seq_printf(s, "\t{TEGRA_PINGROUP_%s", pingroups[i].name);
  700. len = strlen(pingroups[i].name);
  701. dbg_pad_field(s, 5 - len);
  702. if (pingroups[i].mux_reg < 0) {
  703. seq_printf(s, "TEGRA_MUX_NONE");
  704. len = strlen("NONE");
  705. } else {
  706. reg = pg_readl(pingroups[i].mux_bank,
  707. pingroups[i].mux_reg);
  708. mux = (reg >> pingroups[i].mux_bit) & 0x3;
  709. if (pingroups[i].funcs[mux] == TEGRA_MUX_RSVD) {
  710. seq_printf(s, "TEGRA_MUX_RSVD%1lu", mux+1);
  711. len = 5;
  712. } else {
  713. seq_printf(s, "TEGRA_MUX_%s",
  714. tegra_mux_names[pingroups[i].funcs[mux]]);
  715. len = strlen(tegra_mux_names[pingroups[i].funcs[mux]]);
  716. }
  717. }
  718. dbg_pad_field(s, 13-len);
  719. if (pingroups[i].pupd_reg < 0) {
  720. seq_printf(s, "TEGRA_PUPD_NORMAL");
  721. len = strlen("NORMAL");
  722. } else {
  723. reg = pg_readl(pingroups[i].pupd_bank,
  724. pingroups[i].pupd_reg);
  725. pupd = (reg >> pingroups[i].pupd_bit) & 0x3;
  726. seq_printf(s, "TEGRA_PUPD_%s", pupd_name(pupd));
  727. len = strlen(pupd_name(pupd));
  728. }
  729. dbg_pad_field(s, 9 - len);
  730. if (pingroups[i].tri_reg < 0) {
  731. seq_printf(s, "TEGRA_TRI_NORMAL");
  732. } else {
  733. reg = pg_readl(pingroups[i].tri_bank,
  734. pingroups[i].tri_reg);
  735. tri = (reg >> pingroups[i].tri_bit) & 0x1;
  736. seq_printf(s, "TEGRA_TRI_%s", tri_name(tri));
  737. }
  738. seq_printf(s, "},\n");
  739. }
  740. return 0;
  741. }
  742. static int dbg_pinmux_open(struct inode *inode, struct file *file)
  743. {
  744. return single_open(file, dbg_pinmux_show, &inode->i_private);
  745. }
  746. static const struct file_operations debug_fops = {
  747. .open = dbg_pinmux_open,
  748. .read = seq_read,
  749. .llseek = seq_lseek,
  750. .release = single_release,
  751. };
  752. static int dbg_drive_pinmux_show(struct seq_file *s, void *unused)
  753. {
  754. int i;
  755. int len;
  756. for (i = 0; i < drive_max; i++) {
  757. u32 reg;
  758. seq_printf(s, "\t{TEGRA_DRIVE_PINGROUP_%s",
  759. drive_pingroups[i].name);
  760. len = strlen(drive_pingroups[i].name);
  761. dbg_pad_field(s, 7 - len);
  762. reg = pg_readl(drive_pingroups[i].reg_bank,
  763. drive_pingroups[i].reg);
  764. if (HSM_EN(reg)) {
  765. seq_printf(s, "TEGRA_HSM_ENABLE");
  766. len = 16;
  767. } else {
  768. seq_printf(s, "TEGRA_HSM_DISABLE");
  769. len = 17;
  770. }
  771. dbg_pad_field(s, 17 - len);
  772. if (SCHMT_EN(reg)) {
  773. seq_printf(s, "TEGRA_SCHMITT_ENABLE");
  774. len = 21;
  775. } else {
  776. seq_printf(s, "TEGRA_SCHMITT_DISABLE");
  777. len = 22;
  778. }
  779. dbg_pad_field(s, 22 - len);
  780. seq_printf(s, "TEGRA_DRIVE_%s", drive_name(LPMD(reg)));
  781. len = strlen(drive_name(LPMD(reg)));
  782. dbg_pad_field(s, 5 - len);
  783. seq_printf(s, "TEGRA_PULL_%d", DRVDN(reg));
  784. len = DRVDN(reg) < 10 ? 1 : 2;
  785. dbg_pad_field(s, 2 - len);
  786. seq_printf(s, "TEGRA_PULL_%d", DRVUP(reg));
  787. len = DRVUP(reg) < 10 ? 1 : 2;
  788. dbg_pad_field(s, 2 - len);
  789. seq_printf(s, "TEGRA_SLEW_%s", slew_name(SLWR(reg)));
  790. len = strlen(slew_name(SLWR(reg)));
  791. dbg_pad_field(s, 7 - len);
  792. seq_printf(s, "TEGRA_SLEW_%s", slew_name(SLWF(reg)));
  793. seq_printf(s, "},\n");
  794. }
  795. return 0;
  796. }
  797. static int dbg_drive_pinmux_open(struct inode *inode, struct file *file)
  798. {
  799. return single_open(file, dbg_drive_pinmux_show, &inode->i_private);
  800. }
  801. static const struct file_operations debug_drive_fops = {
  802. .open = dbg_drive_pinmux_open,
  803. .read = seq_read,
  804. .llseek = seq_lseek,
  805. .release = single_release,
  806. };
  807. static int __init tegra_pinmux_debuginit(void)
  808. {
  809. (void) debugfs_create_file("tegra_pinmux", S_IRUGO,
  810. NULL, NULL, &debug_fops);
  811. (void) debugfs_create_file("tegra_pinmux_drive", S_IRUGO,
  812. NULL, NULL, &debug_drive_fops);
  813. return 0;
  814. }
  815. late_initcall(tegra_pinmux_debuginit);
  816. #endif