dss.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /*
  2. * linux/drivers/video/omap2/dss/dss.c
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  6. *
  7. * Some code and ideas taken from drivers/video/omap/ driver
  8. * by Imre Deak.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #define DSS_SUBSYS_NAME "DSS"
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/io.h>
  26. #include <linux/export.h>
  27. #include <linux/err.h>
  28. #include <linux/delay.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/clk.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/gfp.h>
  34. #include <linux/sizes.h>
  35. #include <linux/mfd/syscon.h>
  36. #include <linux/regmap.h>
  37. #include <linux/of.h>
  38. #include <linux/regulator/consumer.h>
  39. #include <linux/suspend.h>
  40. #include <linux/component.h>
  41. #include <video/omapdss.h>
  42. #include "dss.h"
  43. #include "dss_features.h"
  44. #define DSS_SZ_REGS SZ_512
  45. struct dss_reg {
  46. u16 idx;
  47. };
  48. #define DSS_REG(idx) ((const struct dss_reg) { idx })
  49. #define DSS_REVISION DSS_REG(0x0000)
  50. #define DSS_SYSCONFIG DSS_REG(0x0010)
  51. #define DSS_SYSSTATUS DSS_REG(0x0014)
  52. #define DSS_CONTROL DSS_REG(0x0040)
  53. #define DSS_SDI_CONTROL DSS_REG(0x0044)
  54. #define DSS_PLL_CONTROL DSS_REG(0x0048)
  55. #define DSS_SDI_STATUS DSS_REG(0x005C)
  56. #define REG_GET(idx, start, end) \
  57. FLD_GET(dss_read_reg(idx), start, end)
  58. #define REG_FLD_MOD(idx, val, start, end) \
  59. dss_write_reg(idx, FLD_MOD(dss_read_reg(idx), val, start, end))
  60. struct dss_features {
  61. u8 fck_div_max;
  62. u8 dss_fck_multiplier;
  63. const char *parent_clk_name;
  64. const enum omap_display_type *ports;
  65. int num_ports;
  66. int (*dpi_select_source)(int port, enum omap_channel channel);
  67. };
  68. static struct {
  69. struct platform_device *pdev;
  70. void __iomem *base;
  71. struct regmap *syscon_pll_ctrl;
  72. u32 syscon_pll_ctrl_offset;
  73. struct clk *parent_clk;
  74. struct clk *dss_clk;
  75. unsigned long dss_clk_rate;
  76. unsigned long cache_req_pck;
  77. unsigned long cache_prate;
  78. struct dispc_clock_info cache_dispc_cinfo;
  79. enum omap_dss_clk_source dsi_clk_source[MAX_NUM_DSI];
  80. enum omap_dss_clk_source dispc_clk_source;
  81. enum omap_dss_clk_source lcd_clk_source[MAX_DSS_LCD_MANAGERS];
  82. bool ctx_valid;
  83. u32 ctx[DSS_SZ_REGS / sizeof(u32)];
  84. const struct dss_features *feat;
  85. struct dss_pll *video1_pll;
  86. struct dss_pll *video2_pll;
  87. } dss;
  88. static const char * const dss_generic_clk_source_names[] = {
  89. [OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC] = "DSI_PLL_HSDIV_DISPC",
  90. [OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI] = "DSI_PLL_HSDIV_DSI",
  91. [OMAP_DSS_CLK_SRC_FCK] = "DSS_FCK",
  92. [OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC] = "DSI_PLL2_HSDIV_DISPC",
  93. [OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DSI] = "DSI_PLL2_HSDIV_DSI",
  94. };
  95. static bool dss_initialized;
  96. bool omapdss_is_initialized(void)
  97. {
  98. return dss_initialized;
  99. }
  100. EXPORT_SYMBOL(omapdss_is_initialized);
  101. static inline void dss_write_reg(const struct dss_reg idx, u32 val)
  102. {
  103. __raw_writel(val, dss.base + idx.idx);
  104. }
  105. static inline u32 dss_read_reg(const struct dss_reg idx)
  106. {
  107. return __raw_readl(dss.base + idx.idx);
  108. }
  109. #define SR(reg) \
  110. dss.ctx[(DSS_##reg).idx / sizeof(u32)] = dss_read_reg(DSS_##reg)
  111. #define RR(reg) \
  112. dss_write_reg(DSS_##reg, dss.ctx[(DSS_##reg).idx / sizeof(u32)])
  113. static void dss_save_context(void)
  114. {
  115. DSSDBG("dss_save_context\n");
  116. SR(CONTROL);
  117. if (dss_feat_get_supported_displays(OMAP_DSS_CHANNEL_LCD) &
  118. OMAP_DISPLAY_TYPE_SDI) {
  119. SR(SDI_CONTROL);
  120. SR(PLL_CONTROL);
  121. }
  122. dss.ctx_valid = true;
  123. DSSDBG("context saved\n");
  124. }
  125. static void dss_restore_context(void)
  126. {
  127. DSSDBG("dss_restore_context\n");
  128. if (!dss.ctx_valid)
  129. return;
  130. RR(CONTROL);
  131. if (dss_feat_get_supported_displays(OMAP_DSS_CHANNEL_LCD) &
  132. OMAP_DISPLAY_TYPE_SDI) {
  133. RR(SDI_CONTROL);
  134. RR(PLL_CONTROL);
  135. }
  136. DSSDBG("context restored\n");
  137. }
  138. #undef SR
  139. #undef RR
  140. void dss_ctrl_pll_enable(enum dss_pll_id pll_id, bool enable)
  141. {
  142. unsigned shift;
  143. unsigned val;
  144. if (!dss.syscon_pll_ctrl)
  145. return;
  146. val = !enable;
  147. switch (pll_id) {
  148. case DSS_PLL_VIDEO1:
  149. shift = 0;
  150. break;
  151. case DSS_PLL_VIDEO2:
  152. shift = 1;
  153. break;
  154. case DSS_PLL_HDMI:
  155. shift = 2;
  156. break;
  157. default:
  158. DSSERR("illegal DSS PLL ID %d\n", pll_id);
  159. return;
  160. }
  161. regmap_update_bits(dss.syscon_pll_ctrl, dss.syscon_pll_ctrl_offset,
  162. 1 << shift, val << shift);
  163. }
  164. void dss_ctrl_pll_set_control_mux(enum dss_pll_id pll_id,
  165. enum omap_channel channel)
  166. {
  167. unsigned shift, val;
  168. if (!dss.syscon_pll_ctrl)
  169. return;
  170. switch (channel) {
  171. case OMAP_DSS_CHANNEL_LCD:
  172. shift = 3;
  173. switch (pll_id) {
  174. case DSS_PLL_VIDEO1:
  175. val = 0; break;
  176. case DSS_PLL_HDMI:
  177. val = 1; break;
  178. default:
  179. DSSERR("error in PLL mux config for LCD\n");
  180. return;
  181. }
  182. break;
  183. case OMAP_DSS_CHANNEL_LCD2:
  184. shift = 5;
  185. switch (pll_id) {
  186. case DSS_PLL_VIDEO1:
  187. val = 0; break;
  188. case DSS_PLL_VIDEO2:
  189. val = 1; break;
  190. case DSS_PLL_HDMI:
  191. val = 2; break;
  192. default:
  193. DSSERR("error in PLL mux config for LCD2\n");
  194. return;
  195. }
  196. break;
  197. case OMAP_DSS_CHANNEL_LCD3:
  198. shift = 7;
  199. switch (pll_id) {
  200. case DSS_PLL_VIDEO1:
  201. val = 1; break;
  202. case DSS_PLL_VIDEO2:
  203. val = 0; break;
  204. case DSS_PLL_HDMI:
  205. val = 2; break;
  206. default:
  207. DSSERR("error in PLL mux config for LCD3\n");
  208. return;
  209. }
  210. break;
  211. default:
  212. DSSERR("error in PLL mux config\n");
  213. return;
  214. }
  215. regmap_update_bits(dss.syscon_pll_ctrl, dss.syscon_pll_ctrl_offset,
  216. 0x3 << shift, val << shift);
  217. }
  218. void dss_sdi_init(int datapairs)
  219. {
  220. u32 l;
  221. BUG_ON(datapairs > 3 || datapairs < 1);
  222. l = dss_read_reg(DSS_SDI_CONTROL);
  223. l = FLD_MOD(l, 0xf, 19, 15); /* SDI_PDIV */
  224. l = FLD_MOD(l, datapairs-1, 3, 2); /* SDI_PRSEL */
  225. l = FLD_MOD(l, 2, 1, 0); /* SDI_BWSEL */
  226. dss_write_reg(DSS_SDI_CONTROL, l);
  227. l = dss_read_reg(DSS_PLL_CONTROL);
  228. l = FLD_MOD(l, 0x7, 25, 22); /* SDI_PLL_FREQSEL */
  229. l = FLD_MOD(l, 0xb, 16, 11); /* SDI_PLL_REGN */
  230. l = FLD_MOD(l, 0xb4, 10, 1); /* SDI_PLL_REGM */
  231. dss_write_reg(DSS_PLL_CONTROL, l);
  232. }
  233. int dss_sdi_enable(void)
  234. {
  235. unsigned long timeout;
  236. dispc_pck_free_enable(1);
  237. /* Reset SDI PLL */
  238. REG_FLD_MOD(DSS_PLL_CONTROL, 1, 18, 18); /* SDI_PLL_SYSRESET */
  239. udelay(1); /* wait 2x PCLK */
  240. /* Lock SDI PLL */
  241. REG_FLD_MOD(DSS_PLL_CONTROL, 1, 28, 28); /* SDI_PLL_GOBIT */
  242. /* Waiting for PLL lock request to complete */
  243. timeout = jiffies + msecs_to_jiffies(500);
  244. while (dss_read_reg(DSS_SDI_STATUS) & (1 << 6)) {
  245. if (time_after_eq(jiffies, timeout)) {
  246. DSSERR("PLL lock request timed out\n");
  247. goto err1;
  248. }
  249. }
  250. /* Clearing PLL_GO bit */
  251. REG_FLD_MOD(DSS_PLL_CONTROL, 0, 28, 28);
  252. /* Waiting for PLL to lock */
  253. timeout = jiffies + msecs_to_jiffies(500);
  254. while (!(dss_read_reg(DSS_SDI_STATUS) & (1 << 5))) {
  255. if (time_after_eq(jiffies, timeout)) {
  256. DSSERR("PLL lock timed out\n");
  257. goto err1;
  258. }
  259. }
  260. dispc_lcd_enable_signal(1);
  261. /* Waiting for SDI reset to complete */
  262. timeout = jiffies + msecs_to_jiffies(500);
  263. while (!(dss_read_reg(DSS_SDI_STATUS) & (1 << 2))) {
  264. if (time_after_eq(jiffies, timeout)) {
  265. DSSERR("SDI reset timed out\n");
  266. goto err2;
  267. }
  268. }
  269. return 0;
  270. err2:
  271. dispc_lcd_enable_signal(0);
  272. err1:
  273. /* Reset SDI PLL */
  274. REG_FLD_MOD(DSS_PLL_CONTROL, 0, 18, 18); /* SDI_PLL_SYSRESET */
  275. dispc_pck_free_enable(0);
  276. return -ETIMEDOUT;
  277. }
  278. void dss_sdi_disable(void)
  279. {
  280. dispc_lcd_enable_signal(0);
  281. dispc_pck_free_enable(0);
  282. /* Reset SDI PLL */
  283. REG_FLD_MOD(DSS_PLL_CONTROL, 0, 18, 18); /* SDI_PLL_SYSRESET */
  284. }
  285. const char *dss_get_generic_clk_source_name(enum omap_dss_clk_source clk_src)
  286. {
  287. return dss_generic_clk_source_names[clk_src];
  288. }
  289. void dss_dump_clocks(struct seq_file *s)
  290. {
  291. const char *fclk_name, *fclk_real_name;
  292. unsigned long fclk_rate;
  293. if (dss_runtime_get())
  294. return;
  295. seq_printf(s, "- DSS -\n");
  296. fclk_name = dss_get_generic_clk_source_name(OMAP_DSS_CLK_SRC_FCK);
  297. fclk_real_name = dss_feat_get_clk_source_name(OMAP_DSS_CLK_SRC_FCK);
  298. fclk_rate = clk_get_rate(dss.dss_clk);
  299. seq_printf(s, "%s (%s) = %lu\n",
  300. fclk_name, fclk_real_name,
  301. fclk_rate);
  302. dss_runtime_put();
  303. }
  304. static void dss_dump_regs(struct seq_file *s)
  305. {
  306. #define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, dss_read_reg(r))
  307. if (dss_runtime_get())
  308. return;
  309. DUMPREG(DSS_REVISION);
  310. DUMPREG(DSS_SYSCONFIG);
  311. DUMPREG(DSS_SYSSTATUS);
  312. DUMPREG(DSS_CONTROL);
  313. if (dss_feat_get_supported_displays(OMAP_DSS_CHANNEL_LCD) &
  314. OMAP_DISPLAY_TYPE_SDI) {
  315. DUMPREG(DSS_SDI_CONTROL);
  316. DUMPREG(DSS_PLL_CONTROL);
  317. DUMPREG(DSS_SDI_STATUS);
  318. }
  319. dss_runtime_put();
  320. #undef DUMPREG
  321. }
  322. static void dss_select_dispc_clk_source(enum omap_dss_clk_source clk_src)
  323. {
  324. int b;
  325. u8 start, end;
  326. switch (clk_src) {
  327. case OMAP_DSS_CLK_SRC_FCK:
  328. b = 0;
  329. break;
  330. case OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC:
  331. b = 1;
  332. break;
  333. case OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC:
  334. b = 2;
  335. break;
  336. default:
  337. BUG();
  338. return;
  339. }
  340. dss_feat_get_reg_field(FEAT_REG_DISPC_CLK_SWITCH, &start, &end);
  341. REG_FLD_MOD(DSS_CONTROL, b, start, end); /* DISPC_CLK_SWITCH */
  342. dss.dispc_clk_source = clk_src;
  343. }
  344. void dss_select_dsi_clk_source(int dsi_module,
  345. enum omap_dss_clk_source clk_src)
  346. {
  347. int b, pos;
  348. switch (clk_src) {
  349. case OMAP_DSS_CLK_SRC_FCK:
  350. b = 0;
  351. break;
  352. case OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DSI:
  353. BUG_ON(dsi_module != 0);
  354. b = 1;
  355. break;
  356. case OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DSI:
  357. BUG_ON(dsi_module != 1);
  358. b = 1;
  359. break;
  360. default:
  361. BUG();
  362. return;
  363. }
  364. pos = dsi_module == 0 ? 1 : 10;
  365. REG_FLD_MOD(DSS_CONTROL, b, pos, pos); /* DSIx_CLK_SWITCH */
  366. dss.dsi_clk_source[dsi_module] = clk_src;
  367. }
  368. void dss_select_lcd_clk_source(enum omap_channel channel,
  369. enum omap_dss_clk_source clk_src)
  370. {
  371. int b, ix, pos;
  372. if (!dss_has_feature(FEAT_LCD_CLK_SRC)) {
  373. dss_select_dispc_clk_source(clk_src);
  374. return;
  375. }
  376. switch (clk_src) {
  377. case OMAP_DSS_CLK_SRC_FCK:
  378. b = 0;
  379. break;
  380. case OMAP_DSS_CLK_SRC_DSI_PLL_HSDIV_DISPC:
  381. BUG_ON(channel != OMAP_DSS_CHANNEL_LCD);
  382. b = 1;
  383. break;
  384. case OMAP_DSS_CLK_SRC_DSI2_PLL_HSDIV_DISPC:
  385. BUG_ON(channel != OMAP_DSS_CHANNEL_LCD2 &&
  386. channel != OMAP_DSS_CHANNEL_LCD3);
  387. b = 1;
  388. break;
  389. default:
  390. BUG();
  391. return;
  392. }
  393. pos = channel == OMAP_DSS_CHANNEL_LCD ? 0 :
  394. (channel == OMAP_DSS_CHANNEL_LCD2 ? 12 : 19);
  395. REG_FLD_MOD(DSS_CONTROL, b, pos, pos); /* LCDx_CLK_SWITCH */
  396. ix = channel == OMAP_DSS_CHANNEL_LCD ? 0 :
  397. (channel == OMAP_DSS_CHANNEL_LCD2 ? 1 : 2);
  398. dss.lcd_clk_source[ix] = clk_src;
  399. }
  400. enum omap_dss_clk_source dss_get_dispc_clk_source(void)
  401. {
  402. return dss.dispc_clk_source;
  403. }
  404. enum omap_dss_clk_source dss_get_dsi_clk_source(int dsi_module)
  405. {
  406. return dss.dsi_clk_source[dsi_module];
  407. }
  408. enum omap_dss_clk_source dss_get_lcd_clk_source(enum omap_channel channel)
  409. {
  410. if (dss_has_feature(FEAT_LCD_CLK_SRC)) {
  411. int ix = channel == OMAP_DSS_CHANNEL_LCD ? 0 :
  412. (channel == OMAP_DSS_CHANNEL_LCD2 ? 1 : 2);
  413. return dss.lcd_clk_source[ix];
  414. } else {
  415. /* LCD_CLK source is the same as DISPC_FCLK source for
  416. * OMAP2 and OMAP3 */
  417. return dss.dispc_clk_source;
  418. }
  419. }
  420. bool dss_div_calc(unsigned long pck, unsigned long fck_min,
  421. dss_div_calc_func func, void *data)
  422. {
  423. int fckd, fckd_start, fckd_stop;
  424. unsigned long fck;
  425. unsigned long fck_hw_max;
  426. unsigned long fckd_hw_max;
  427. unsigned long prate;
  428. unsigned m;
  429. fck_hw_max = dss_feat_get_param_max(FEAT_PARAM_DSS_FCK);
  430. if (dss.parent_clk == NULL) {
  431. unsigned pckd;
  432. pckd = fck_hw_max / pck;
  433. fck = pck * pckd;
  434. fck = clk_round_rate(dss.dss_clk, fck);
  435. return func(fck, data);
  436. }
  437. fckd_hw_max = dss.feat->fck_div_max;
  438. m = dss.feat->dss_fck_multiplier;
  439. prate = clk_get_rate(dss.parent_clk);
  440. fck_min = fck_min ? fck_min : 1;
  441. fckd_start = min(prate * m / fck_min, fckd_hw_max);
  442. fckd_stop = max(DIV_ROUND_UP(prate * m, fck_hw_max), 1ul);
  443. for (fckd = fckd_start; fckd >= fckd_stop; --fckd) {
  444. fck = DIV_ROUND_UP(prate, fckd) * m;
  445. if (func(fck, data))
  446. return true;
  447. }
  448. return false;
  449. }
  450. int dss_set_fck_rate(unsigned long rate)
  451. {
  452. int r;
  453. DSSDBG("set fck to %lu\n", rate);
  454. r = clk_set_rate(dss.dss_clk, rate);
  455. if (r)
  456. return r;
  457. dss.dss_clk_rate = clk_get_rate(dss.dss_clk);
  458. WARN_ONCE(dss.dss_clk_rate != rate,
  459. "clk rate mismatch: %lu != %lu", dss.dss_clk_rate,
  460. rate);
  461. return 0;
  462. }
  463. unsigned long dss_get_dispc_clk_rate(void)
  464. {
  465. return dss.dss_clk_rate;
  466. }
  467. static int dss_setup_default_clock(void)
  468. {
  469. unsigned long max_dss_fck, prate;
  470. unsigned long fck;
  471. unsigned fck_div;
  472. int r;
  473. max_dss_fck = dss_feat_get_param_max(FEAT_PARAM_DSS_FCK);
  474. if (dss.parent_clk == NULL) {
  475. fck = clk_round_rate(dss.dss_clk, max_dss_fck);
  476. } else {
  477. prate = clk_get_rate(dss.parent_clk);
  478. fck_div = DIV_ROUND_UP(prate * dss.feat->dss_fck_multiplier,
  479. max_dss_fck);
  480. fck = DIV_ROUND_UP(prate, fck_div) * dss.feat->dss_fck_multiplier;
  481. }
  482. r = dss_set_fck_rate(fck);
  483. if (r)
  484. return r;
  485. return 0;
  486. }
  487. void dss_set_venc_output(enum omap_dss_venc_type type)
  488. {
  489. int l = 0;
  490. if (type == OMAP_DSS_VENC_TYPE_COMPOSITE)
  491. l = 0;
  492. else if (type == OMAP_DSS_VENC_TYPE_SVIDEO)
  493. l = 1;
  494. else
  495. BUG();
  496. /* venc out selection. 0 = comp, 1 = svideo */
  497. REG_FLD_MOD(DSS_CONTROL, l, 6, 6);
  498. }
  499. void dss_set_dac_pwrdn_bgz(bool enable)
  500. {
  501. REG_FLD_MOD(DSS_CONTROL, enable, 5, 5); /* DAC Power-Down Control */
  502. }
  503. void dss_select_hdmi_venc_clk_source(enum dss_hdmi_venc_clk_source_select src)
  504. {
  505. enum omap_display_type dp;
  506. dp = dss_feat_get_supported_displays(OMAP_DSS_CHANNEL_DIGIT);
  507. /* Complain about invalid selections */
  508. WARN_ON((src == DSS_VENC_TV_CLK) && !(dp & OMAP_DISPLAY_TYPE_VENC));
  509. WARN_ON((src == DSS_HDMI_M_PCLK) && !(dp & OMAP_DISPLAY_TYPE_HDMI));
  510. /* Select only if we have options */
  511. if ((dp & OMAP_DISPLAY_TYPE_VENC) && (dp & OMAP_DISPLAY_TYPE_HDMI))
  512. REG_FLD_MOD(DSS_CONTROL, src, 15, 15); /* VENC_HDMI_SWITCH */
  513. }
  514. enum dss_hdmi_venc_clk_source_select dss_get_hdmi_venc_clk_source(void)
  515. {
  516. enum omap_display_type displays;
  517. displays = dss_feat_get_supported_displays(OMAP_DSS_CHANNEL_DIGIT);
  518. if ((displays & OMAP_DISPLAY_TYPE_HDMI) == 0)
  519. return DSS_VENC_TV_CLK;
  520. if ((displays & OMAP_DISPLAY_TYPE_VENC) == 0)
  521. return DSS_HDMI_M_PCLK;
  522. return REG_GET(DSS_CONTROL, 15, 15);
  523. }
  524. static int dss_dpi_select_source_omap2_omap3(int port, enum omap_channel channel)
  525. {
  526. if (channel != OMAP_DSS_CHANNEL_LCD)
  527. return -EINVAL;
  528. return 0;
  529. }
  530. static int dss_dpi_select_source_omap4(int port, enum omap_channel channel)
  531. {
  532. int val;
  533. switch (channel) {
  534. case OMAP_DSS_CHANNEL_LCD2:
  535. val = 0;
  536. break;
  537. case OMAP_DSS_CHANNEL_DIGIT:
  538. val = 1;
  539. break;
  540. default:
  541. return -EINVAL;
  542. }
  543. REG_FLD_MOD(DSS_CONTROL, val, 17, 17);
  544. return 0;
  545. }
  546. static int dss_dpi_select_source_omap5(int port, enum omap_channel channel)
  547. {
  548. int val;
  549. switch (channel) {
  550. case OMAP_DSS_CHANNEL_LCD:
  551. val = 1;
  552. break;
  553. case OMAP_DSS_CHANNEL_LCD2:
  554. val = 2;
  555. break;
  556. case OMAP_DSS_CHANNEL_LCD3:
  557. val = 3;
  558. break;
  559. case OMAP_DSS_CHANNEL_DIGIT:
  560. val = 0;
  561. break;
  562. default:
  563. return -EINVAL;
  564. }
  565. REG_FLD_MOD(DSS_CONTROL, val, 17, 16);
  566. return 0;
  567. }
  568. static int dss_dpi_select_source_dra7xx(int port, enum omap_channel channel)
  569. {
  570. switch (port) {
  571. case 0:
  572. return dss_dpi_select_source_omap5(port, channel);
  573. case 1:
  574. if (channel != OMAP_DSS_CHANNEL_LCD2)
  575. return -EINVAL;
  576. break;
  577. case 2:
  578. if (channel != OMAP_DSS_CHANNEL_LCD3)
  579. return -EINVAL;
  580. break;
  581. default:
  582. return -EINVAL;
  583. }
  584. return 0;
  585. }
  586. int dss_dpi_select_source(int port, enum omap_channel channel)
  587. {
  588. return dss.feat->dpi_select_source(port, channel);
  589. }
  590. static int dss_get_clocks(void)
  591. {
  592. struct clk *clk;
  593. clk = devm_clk_get(&dss.pdev->dev, "fck");
  594. if (IS_ERR(clk)) {
  595. DSSERR("can't get clock fck\n");
  596. return PTR_ERR(clk);
  597. }
  598. dss.dss_clk = clk;
  599. if (dss.feat->parent_clk_name) {
  600. clk = clk_get(NULL, dss.feat->parent_clk_name);
  601. if (IS_ERR(clk)) {
  602. DSSERR("Failed to get %s\n", dss.feat->parent_clk_name);
  603. return PTR_ERR(clk);
  604. }
  605. } else {
  606. clk = NULL;
  607. }
  608. dss.parent_clk = clk;
  609. return 0;
  610. }
  611. static void dss_put_clocks(void)
  612. {
  613. if (dss.parent_clk)
  614. clk_put(dss.parent_clk);
  615. }
  616. int dss_runtime_get(void)
  617. {
  618. int r;
  619. DSSDBG("dss_runtime_get\n");
  620. r = pm_runtime_get_sync(&dss.pdev->dev);
  621. WARN_ON(r < 0);
  622. return r < 0 ? r : 0;
  623. }
  624. void dss_runtime_put(void)
  625. {
  626. int r;
  627. DSSDBG("dss_runtime_put\n");
  628. r = pm_runtime_put_sync(&dss.pdev->dev);
  629. WARN_ON(r < 0 && r != -ENOSYS && r != -EBUSY);
  630. }
  631. /* DEBUGFS */
  632. #if defined(CONFIG_OMAP2_DSS_DEBUGFS)
  633. void dss_debug_dump_clocks(struct seq_file *s)
  634. {
  635. dss_dump_clocks(s);
  636. dispc_dump_clocks(s);
  637. #ifdef CONFIG_OMAP2_DSS_DSI
  638. dsi_dump_clocks(s);
  639. #endif
  640. }
  641. #endif
  642. static const enum omap_display_type omap2plus_ports[] = {
  643. OMAP_DISPLAY_TYPE_DPI,
  644. };
  645. static const enum omap_display_type omap34xx_ports[] = {
  646. OMAP_DISPLAY_TYPE_DPI,
  647. OMAP_DISPLAY_TYPE_SDI,
  648. };
  649. static const enum omap_display_type dra7xx_ports[] = {
  650. OMAP_DISPLAY_TYPE_DPI,
  651. OMAP_DISPLAY_TYPE_DPI,
  652. OMAP_DISPLAY_TYPE_DPI,
  653. };
  654. static const struct dss_features omap24xx_dss_feats = {
  655. /*
  656. * fck div max is really 16, but the divider range has gaps. The range
  657. * from 1 to 6 has no gaps, so let's use that as a max.
  658. */
  659. .fck_div_max = 6,
  660. .dss_fck_multiplier = 2,
  661. .parent_clk_name = "core_ck",
  662. .dpi_select_source = &dss_dpi_select_source_omap2_omap3,
  663. .ports = omap2plus_ports,
  664. .num_ports = ARRAY_SIZE(omap2plus_ports),
  665. };
  666. static const struct dss_features omap34xx_dss_feats = {
  667. .fck_div_max = 16,
  668. .dss_fck_multiplier = 2,
  669. .parent_clk_name = "dpll4_ck",
  670. .dpi_select_source = &dss_dpi_select_source_omap2_omap3,
  671. .ports = omap34xx_ports,
  672. .num_ports = ARRAY_SIZE(omap34xx_ports),
  673. };
  674. static const struct dss_features omap3630_dss_feats = {
  675. .fck_div_max = 32,
  676. .dss_fck_multiplier = 1,
  677. .parent_clk_name = "dpll4_ck",
  678. .dpi_select_source = &dss_dpi_select_source_omap2_omap3,
  679. .ports = omap2plus_ports,
  680. .num_ports = ARRAY_SIZE(omap2plus_ports),
  681. };
  682. static const struct dss_features omap44xx_dss_feats = {
  683. .fck_div_max = 32,
  684. .dss_fck_multiplier = 1,
  685. .parent_clk_name = "dpll_per_x2_ck",
  686. .dpi_select_source = &dss_dpi_select_source_omap4,
  687. .ports = omap2plus_ports,
  688. .num_ports = ARRAY_SIZE(omap2plus_ports),
  689. };
  690. static const struct dss_features omap54xx_dss_feats = {
  691. .fck_div_max = 64,
  692. .dss_fck_multiplier = 1,
  693. .parent_clk_name = "dpll_per_x2_ck",
  694. .dpi_select_source = &dss_dpi_select_source_omap5,
  695. .ports = omap2plus_ports,
  696. .num_ports = ARRAY_SIZE(omap2plus_ports),
  697. };
  698. static const struct dss_features am43xx_dss_feats = {
  699. .fck_div_max = 0,
  700. .dss_fck_multiplier = 0,
  701. .parent_clk_name = NULL,
  702. .dpi_select_source = &dss_dpi_select_source_omap2_omap3,
  703. .ports = omap2plus_ports,
  704. .num_ports = ARRAY_SIZE(omap2plus_ports),
  705. };
  706. static const struct dss_features dra7xx_dss_feats = {
  707. .fck_div_max = 64,
  708. .dss_fck_multiplier = 1,
  709. .parent_clk_name = "dpll_per_x2_ck",
  710. .dpi_select_source = &dss_dpi_select_source_dra7xx,
  711. .ports = dra7xx_ports,
  712. .num_ports = ARRAY_SIZE(dra7xx_ports),
  713. };
  714. static int dss_init_features(struct platform_device *pdev)
  715. {
  716. const struct dss_features *src;
  717. struct dss_features *dst;
  718. dst = devm_kzalloc(&pdev->dev, sizeof(*dst), GFP_KERNEL);
  719. if (!dst) {
  720. dev_err(&pdev->dev, "Failed to allocate local DSS Features\n");
  721. return -ENOMEM;
  722. }
  723. switch (omapdss_get_version()) {
  724. case OMAPDSS_VER_OMAP24xx:
  725. src = &omap24xx_dss_feats;
  726. break;
  727. case OMAPDSS_VER_OMAP34xx_ES1:
  728. case OMAPDSS_VER_OMAP34xx_ES3:
  729. case OMAPDSS_VER_AM35xx:
  730. src = &omap34xx_dss_feats;
  731. break;
  732. case OMAPDSS_VER_OMAP3630:
  733. src = &omap3630_dss_feats;
  734. break;
  735. case OMAPDSS_VER_OMAP4430_ES1:
  736. case OMAPDSS_VER_OMAP4430_ES2:
  737. case OMAPDSS_VER_OMAP4:
  738. src = &omap44xx_dss_feats;
  739. break;
  740. case OMAPDSS_VER_OMAP5:
  741. src = &omap54xx_dss_feats;
  742. break;
  743. case OMAPDSS_VER_AM43xx:
  744. src = &am43xx_dss_feats;
  745. break;
  746. case OMAPDSS_VER_DRA7xx:
  747. src = &dra7xx_dss_feats;
  748. break;
  749. default:
  750. return -ENODEV;
  751. }
  752. memcpy(dst, src, sizeof(*dst));
  753. dss.feat = dst;
  754. return 0;
  755. }
  756. static int dss_init_ports(struct platform_device *pdev)
  757. {
  758. struct device_node *parent = pdev->dev.of_node;
  759. struct device_node *port;
  760. int r;
  761. if (parent == NULL)
  762. return 0;
  763. port = omapdss_of_get_next_port(parent, NULL);
  764. if (!port)
  765. return 0;
  766. if (dss.feat->num_ports == 0)
  767. return 0;
  768. do {
  769. enum omap_display_type port_type;
  770. u32 reg;
  771. r = of_property_read_u32(port, "reg", &reg);
  772. if (r)
  773. reg = 0;
  774. if (reg >= dss.feat->num_ports)
  775. continue;
  776. port_type = dss.feat->ports[reg];
  777. switch (port_type) {
  778. case OMAP_DISPLAY_TYPE_DPI:
  779. dpi_init_port(pdev, port);
  780. break;
  781. case OMAP_DISPLAY_TYPE_SDI:
  782. sdi_init_port(pdev, port);
  783. break;
  784. default:
  785. break;
  786. }
  787. } while ((port = omapdss_of_get_next_port(parent, port)) != NULL);
  788. return 0;
  789. }
  790. static void dss_uninit_ports(struct platform_device *pdev)
  791. {
  792. struct device_node *parent = pdev->dev.of_node;
  793. struct device_node *port;
  794. if (parent == NULL)
  795. return;
  796. port = omapdss_of_get_next_port(parent, NULL);
  797. if (!port)
  798. return;
  799. if (dss.feat->num_ports == 0)
  800. return;
  801. do {
  802. enum omap_display_type port_type;
  803. u32 reg;
  804. int r;
  805. r = of_property_read_u32(port, "reg", &reg);
  806. if (r)
  807. reg = 0;
  808. if (reg >= dss.feat->num_ports)
  809. continue;
  810. port_type = dss.feat->ports[reg];
  811. switch (port_type) {
  812. case OMAP_DISPLAY_TYPE_DPI:
  813. dpi_uninit_port(port);
  814. break;
  815. case OMAP_DISPLAY_TYPE_SDI:
  816. sdi_uninit_port(port);
  817. break;
  818. default:
  819. break;
  820. }
  821. } while ((port = omapdss_of_get_next_port(parent, port)) != NULL);
  822. }
  823. static int dss_video_pll_probe(struct platform_device *pdev)
  824. {
  825. struct device_node *np = pdev->dev.of_node;
  826. struct regulator *pll_regulator;
  827. int r;
  828. if (!np)
  829. return 0;
  830. if (of_property_read_bool(np, "syscon-pll-ctrl")) {
  831. dss.syscon_pll_ctrl = syscon_regmap_lookup_by_phandle(np,
  832. "syscon-pll-ctrl");
  833. if (IS_ERR(dss.syscon_pll_ctrl)) {
  834. dev_err(&pdev->dev,
  835. "failed to get syscon-pll-ctrl regmap\n");
  836. return PTR_ERR(dss.syscon_pll_ctrl);
  837. }
  838. if (of_property_read_u32_index(np, "syscon-pll-ctrl", 1,
  839. &dss.syscon_pll_ctrl_offset)) {
  840. dev_err(&pdev->dev,
  841. "failed to get syscon-pll-ctrl offset\n");
  842. return -EINVAL;
  843. }
  844. }
  845. pll_regulator = devm_regulator_get(&pdev->dev, "vdda_video");
  846. if (IS_ERR(pll_regulator)) {
  847. r = PTR_ERR(pll_regulator);
  848. switch (r) {
  849. case -ENOENT:
  850. pll_regulator = NULL;
  851. break;
  852. case -EPROBE_DEFER:
  853. return -EPROBE_DEFER;
  854. default:
  855. DSSERR("can't get DPLL VDDA regulator\n");
  856. return r;
  857. }
  858. }
  859. if (of_property_match_string(np, "reg-names", "pll1") >= 0) {
  860. dss.video1_pll = dss_video_pll_init(pdev, 0, pll_regulator);
  861. if (IS_ERR(dss.video1_pll))
  862. return PTR_ERR(dss.video1_pll);
  863. }
  864. if (of_property_match_string(np, "reg-names", "pll2") >= 0) {
  865. dss.video2_pll = dss_video_pll_init(pdev, 1, pll_regulator);
  866. if (IS_ERR(dss.video2_pll)) {
  867. dss_video_pll_uninit(dss.video1_pll);
  868. return PTR_ERR(dss.video2_pll);
  869. }
  870. }
  871. return 0;
  872. }
  873. /* DSS HW IP initialisation */
  874. static int dss_bind(struct device *dev)
  875. {
  876. struct platform_device *pdev = to_platform_device(dev);
  877. struct resource *dss_mem;
  878. u32 rev;
  879. int r;
  880. dss.pdev = pdev;
  881. r = dss_init_features(dss.pdev);
  882. if (r)
  883. return r;
  884. dss_mem = platform_get_resource(dss.pdev, IORESOURCE_MEM, 0);
  885. if (!dss_mem) {
  886. DSSERR("can't get IORESOURCE_MEM DSS\n");
  887. return -EINVAL;
  888. }
  889. dss.base = devm_ioremap(&pdev->dev, dss_mem->start,
  890. resource_size(dss_mem));
  891. if (!dss.base) {
  892. DSSERR("can't ioremap DSS\n");
  893. return -ENOMEM;
  894. }
  895. r = dss_get_clocks();
  896. if (r)
  897. return r;
  898. r = dss_setup_default_clock();
  899. if (r)
  900. goto err_setup_clocks;
  901. r = dss_video_pll_probe(pdev);
  902. if (r)
  903. goto err_pll_init;
  904. r = dss_init_ports(pdev);
  905. if (r)
  906. goto err_init_ports;
  907. pm_runtime_enable(&pdev->dev);
  908. r = dss_runtime_get();
  909. if (r)
  910. goto err_runtime_get;
  911. dss.dss_clk_rate = clk_get_rate(dss.dss_clk);
  912. /* Select DPLL */
  913. REG_FLD_MOD(DSS_CONTROL, 0, 0, 0);
  914. dss_select_dispc_clk_source(OMAP_DSS_CLK_SRC_FCK);
  915. #ifdef CONFIG_OMAP2_DSS_VENC
  916. REG_FLD_MOD(DSS_CONTROL, 1, 4, 4); /* venc dac demen */
  917. REG_FLD_MOD(DSS_CONTROL, 1, 3, 3); /* venc clock 4x enable */
  918. REG_FLD_MOD(DSS_CONTROL, 0, 2, 2); /* venc clock mode = normal */
  919. #endif
  920. dss.dsi_clk_source[0] = OMAP_DSS_CLK_SRC_FCK;
  921. dss.dsi_clk_source[1] = OMAP_DSS_CLK_SRC_FCK;
  922. dss.dispc_clk_source = OMAP_DSS_CLK_SRC_FCK;
  923. dss.lcd_clk_source[0] = OMAP_DSS_CLK_SRC_FCK;
  924. dss.lcd_clk_source[1] = OMAP_DSS_CLK_SRC_FCK;
  925. rev = dss_read_reg(DSS_REVISION);
  926. printk(KERN_INFO "OMAP DSS rev %d.%d\n",
  927. FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
  928. dss_runtime_put();
  929. r = component_bind_all(&pdev->dev, NULL);
  930. if (r)
  931. goto err_component;
  932. dss_debugfs_create_file("dss", dss_dump_regs);
  933. pm_set_vt_switch(0);
  934. dss_initialized = true;
  935. return 0;
  936. err_component:
  937. err_runtime_get:
  938. pm_runtime_disable(&pdev->dev);
  939. dss_uninit_ports(pdev);
  940. err_init_ports:
  941. if (dss.video1_pll)
  942. dss_video_pll_uninit(dss.video1_pll);
  943. if (dss.video2_pll)
  944. dss_video_pll_uninit(dss.video2_pll);
  945. err_pll_init:
  946. err_setup_clocks:
  947. dss_put_clocks();
  948. return r;
  949. }
  950. static void dss_unbind(struct device *dev)
  951. {
  952. struct platform_device *pdev = to_platform_device(dev);
  953. dss_initialized = false;
  954. component_unbind_all(&pdev->dev, NULL);
  955. if (dss.video1_pll)
  956. dss_video_pll_uninit(dss.video1_pll);
  957. if (dss.video2_pll)
  958. dss_video_pll_uninit(dss.video2_pll);
  959. dss_uninit_ports(pdev);
  960. pm_runtime_disable(&pdev->dev);
  961. dss_put_clocks();
  962. }
  963. static const struct component_master_ops dss_component_ops = {
  964. .bind = dss_bind,
  965. .unbind = dss_unbind,
  966. };
  967. static int dss_component_compare(struct device *dev, void *data)
  968. {
  969. struct device *child = data;
  970. return dev == child;
  971. }
  972. static int dss_add_child_component(struct device *dev, void *data)
  973. {
  974. struct component_match **match = data;
  975. /*
  976. * HACK
  977. * We don't have a working driver for rfbi, so skip it here always.
  978. * Otherwise dss will never get probed successfully, as it will wait
  979. * for rfbi to get probed.
  980. */
  981. if (strstr(dev_name(dev), "rfbi"))
  982. return 0;
  983. component_match_add(dev->parent, match, dss_component_compare, dev);
  984. return 0;
  985. }
  986. static int dss_probe(struct platform_device *pdev)
  987. {
  988. struct component_match *match = NULL;
  989. int r;
  990. /* add all the child devices as components */
  991. device_for_each_child(&pdev->dev, &match, dss_add_child_component);
  992. r = component_master_add_with_match(&pdev->dev, &dss_component_ops, match);
  993. if (r)
  994. return r;
  995. return 0;
  996. }
  997. static int dss_remove(struct platform_device *pdev)
  998. {
  999. component_master_del(&pdev->dev, &dss_component_ops);
  1000. return 0;
  1001. }
  1002. static int dss_runtime_suspend(struct device *dev)
  1003. {
  1004. dss_save_context();
  1005. dss_set_min_bus_tput(dev, 0);
  1006. return 0;
  1007. }
  1008. static int dss_runtime_resume(struct device *dev)
  1009. {
  1010. int r;
  1011. /*
  1012. * Set an arbitrarily high tput request to ensure OPP100.
  1013. * What we should really do is to make a request to stay in OPP100,
  1014. * without any tput requirements, but that is not currently possible
  1015. * via the PM layer.
  1016. */
  1017. r = dss_set_min_bus_tput(dev, 1000000000);
  1018. if (r)
  1019. return r;
  1020. dss_restore_context();
  1021. return 0;
  1022. }
  1023. static const struct dev_pm_ops dss_pm_ops = {
  1024. .runtime_suspend = dss_runtime_suspend,
  1025. .runtime_resume = dss_runtime_resume,
  1026. };
  1027. static const struct of_device_id dss_of_match[] = {
  1028. { .compatible = "ti,omap2-dss", },
  1029. { .compatible = "ti,omap3-dss", },
  1030. { .compatible = "ti,omap4-dss", },
  1031. { .compatible = "ti,omap5-dss", },
  1032. { .compatible = "ti,dra7-dss", },
  1033. {},
  1034. };
  1035. MODULE_DEVICE_TABLE(of, dss_of_match);
  1036. static struct platform_driver omap_dsshw_driver = {
  1037. .probe = dss_probe,
  1038. .remove = dss_remove,
  1039. .driver = {
  1040. .name = "omapdss_dss",
  1041. .pm = &dss_pm_ops,
  1042. .of_match_table = dss_of_match,
  1043. .suppress_bind_attrs = true,
  1044. },
  1045. };
  1046. int __init dss_init_platform_driver(void)
  1047. {
  1048. return platform_driver_register(&omap_dsshw_driver);
  1049. }
  1050. void dss_uninit_platform_driver(void)
  1051. {
  1052. platform_driver_unregister(&omap_dsshw_driver);
  1053. }