pwm-mtk-disp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * MediaTek display pulse-width-modulation controller driver.
  3. * Copyright (c) 2015 MediaTek Inc.
  4. * Copyright (C) 2021 XiaoMi, Inc.
  5. * Author: YH Huang <yh.huang@mediatek.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pwm.h>
  24. #include <linux/slab.h>
  25. #include <linux/of_address.h>
  26. #include <linux/delay.h>
  27. extern unsigned int mt_get_ckgen_freq(int ID);
  28. #define DISP_PWM_EN 0x00
  29. #define PWM_CLKDIV_SHIFT 16
  30. #define PWM_CLKDIV_MAX 0x3ff
  31. #define PWM_CLKDIV_MASK (PWM_CLKDIV_MAX << PWM_CLKDIV_SHIFT)
  32. #define PWM_PERIOD_BIT_WIDTH 12
  33. #define PWM_PERIOD_MASK ((1 << PWM_PERIOD_BIT_WIDTH) - 1)
  34. #define PWM_HIGH_WIDTH_SHIFT 16
  35. #define PWM_HIGH_WIDTH_MASK (0x1fff << PWM_HIGH_WIDTH_SHIFT)
  36. #undef pr_fmt
  37. #define pr_fmt(fmt) "[mtk_disp_pwm]" fmt
  38. struct mtk_pwm_data {
  39. u32 enable_mask;
  40. unsigned int con0;
  41. u32 con0_sel;
  42. unsigned int con1;
  43. bool has_commit;
  44. unsigned int commit;
  45. unsigned int commit_mask;
  46. unsigned int bls_debug;
  47. u32 bls_debug_mask;
  48. };
  49. struct mtk_disp_pwm {
  50. struct pwm_chip chip;
  51. const struct mtk_pwm_data *data;
  52. struct clk *clk_main;
  53. struct clk *clk_mm;
  54. struct clk *clk_source;
  55. void __iomem *base;
  56. void __iomem *pmw_src_addr;
  57. bool pwm_src_enabled;
  58. u32 source_rate;
  59. bool pwm_src_set;
  60. };
  61. static inline struct mtk_disp_pwm *to_mtk_disp_pwm(struct pwm_chip *chip)
  62. {
  63. return container_of(chip, struct mtk_disp_pwm, chip);
  64. }
  65. static void mtk_disp_pwm_update_bits(struct mtk_disp_pwm *mdp, u32 offset,
  66. u32 mask, u32 data)
  67. {
  68. void __iomem *address = mdp->base + offset;
  69. u32 value;
  70. value = readl(address);
  71. value &= ~mask;
  72. value |= data;
  73. writel(value, address);
  74. }
  75. static int get_pwm_src_base(struct device *dev, struct mtk_disp_pwm *mdp)
  76. {
  77. int ret = 0;
  78. struct device_node *node;
  79. void __iomem *pmw_src_base;
  80. u32 addr_offset = 0;
  81. node = of_parse_phandle(dev->of_node, "pwm_src_base", 0);
  82. if (!node) {
  83. dev_info(dev, "no pwm_src node\n");
  84. return -1;
  85. }
  86. pmw_src_base = of_iomap(node, 0);
  87. if (!pmw_src_base) {
  88. dev_info(dev, "no pwm_src address\n");
  89. of_node_put(node);
  90. return -1;
  91. }
  92. ret = of_property_read_u32(dev->of_node, "pwm_src_addr", &addr_offset);
  93. if (ret >= 0)
  94. mdp->pmw_src_addr = pmw_src_base + addr_offset;
  95. dev_info(dev, "get pwm_src_addr=%x\n", addr_offset);
  96. of_node_put(node);
  97. return ret;
  98. }
  99. static int pwm_src_power_on(struct mtk_disp_pwm *mdp)
  100. {
  101. u32 regosc;
  102. if (!mdp->pmw_src_addr || mdp->pwm_src_enabled)
  103. return 0;
  104. mdp->pwm_src_enabled = true;
  105. regosc = readl(mdp->pmw_src_addr);
  106. regosc = regosc | 0x1;
  107. writel(regosc, mdp->pmw_src_addr);
  108. udelay(150);
  109. regosc = readl(mdp->pmw_src_addr);
  110. regosc = regosc | 0x4;
  111. writel(regosc, mdp->pmw_src_addr);
  112. regosc = readl(mdp->pmw_src_addr);
  113. return 0;
  114. }
  115. static int pwm_src_power_off(struct mtk_disp_pwm *mdp)
  116. {
  117. u32 regosc;
  118. if (!mdp->pmw_src_addr || !mdp->pwm_src_enabled)
  119. return 0;
  120. mdp->pwm_src_enabled = false;
  121. regosc = readl(mdp->pmw_src_addr);
  122. regosc = regosc & (~0x4);
  123. writel(regosc, mdp->pmw_src_addr);
  124. udelay(150);
  125. regosc = readl(mdp->pmw_src_addr);
  126. regosc = regosc & (~0x1);
  127. writel(regosc, mdp->pmw_src_addr);
  128. regosc = readl(mdp->pmw_src_addr);
  129. return 0;
  130. }
  131. static int mtk_disp_pwm_config_impl(struct mtk_disp_pwm *mdp,
  132. int duty_ns, int period_ns)
  133. {
  134. u32 clk_div, period, high_width, value;
  135. u64 div, rate;
  136. int err;
  137. /*
  138. * Find period, high_width and clk_div to suit duty_ns and period_ns.
  139. * Calculate proper div value to keep period value in the bound.
  140. *
  141. * period_ns = 10^9 * (clk_div + 1) * (period + 1) / PWM_CLK_RATE
  142. * duty_ns = 10^9 * (clk_div + 1) * high_width / PWM_CLK_RATE
  143. *
  144. * period = (PWM_CLK_RATE * period_ns) / (10^9 * (clk_div + 1)) - 1
  145. * high_width = (PWM_CLK_RATE * duty_ns) / (10^9 * (clk_div + 1))
  146. */
  147. dev_notice(mdp->chip.dev, "duty=%d period=%d\n", duty_ns, period_ns);
  148. rate = clk_get_rate(mdp->clk_main);
  149. clk_div = div_u64(rate * period_ns, NSEC_PER_SEC) >>
  150. PWM_PERIOD_BIT_WIDTH;
  151. if (clk_div > PWM_CLKDIV_MAX)
  152. return -EINVAL;
  153. dev_notice(mdp->chip.dev, "rate=%lld clk_div=%d\n", rate, clk_div);
  154. div = NSEC_PER_SEC * (clk_div + 1);
  155. period = div64_u64(rate * period_ns, div);
  156. if (period > 0)
  157. period--;
  158. high_width = div64_u64(rate * duty_ns, div);
  159. value = period | (high_width << PWM_HIGH_WIDTH_SHIFT);
  160. dev_dbg(mdp->chip.dev, "high_width=%d period=%d\n",
  161. high_width, period);
  162. pwm_src_power_on(mdp);
  163. err = clk_enable(mdp->clk_main);
  164. if (err < 0)
  165. return err;
  166. err = clk_enable(mdp->clk_mm);
  167. if (err < 0) {
  168. clk_disable(mdp->clk_main);
  169. return err;
  170. }
  171. mtk_disp_pwm_update_bits(mdp, mdp->data->con0,
  172. PWM_CLKDIV_MASK,
  173. clk_div << PWM_CLKDIV_SHIFT);
  174. mtk_disp_pwm_update_bits(mdp, mdp->data->con1,
  175. PWM_PERIOD_MASK | PWM_HIGH_WIDTH_MASK,
  176. value);
  177. if (mdp->data->has_commit) {
  178. mtk_disp_pwm_update_bits(mdp, mdp->data->commit,
  179. mdp->data->commit_mask,
  180. mdp->data->commit_mask);
  181. mtk_disp_pwm_update_bits(mdp, mdp->data->commit,
  182. mdp->data->commit_mask,
  183. 0x0);
  184. }
  185. clk_disable(mdp->clk_mm);
  186. clk_disable(mdp->clk_main);
  187. return 0;
  188. }
  189. static int mtk_disp_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  190. int duty_ns, int period_ns)
  191. {
  192. struct mtk_disp_pwm *mdp = to_mtk_disp_pwm(chip);
  193. return mtk_disp_pwm_config_impl(mdp, duty_ns, period_ns);
  194. }
  195. static int mtk_disp_pwm_enable_impl(struct mtk_disp_pwm *mdp)
  196. {
  197. int err;
  198. dev_dbg(mdp->chip.dev, "%s\n", __func__);
  199. err = clk_enable(mdp->clk_main);
  200. if (err < 0)
  201. return err;
  202. err = clk_enable(mdp->clk_mm);
  203. if (err < 0) {
  204. clk_disable(mdp->clk_main);
  205. return err;
  206. }
  207. pwm_src_power_on(mdp);
  208. if (mdp->pwm_src_set != true) {
  209. if (!IS_ERR(mdp->clk_source)) {
  210. err = clk_set_parent(mdp->clk_mm, mdp->clk_source);
  211. if (err < 0)
  212. dev_info(mdp->chip.dev, "no pwm_src\n");
  213. dev_info(mdp->chip.dev, "select clk_mm with pwm_src\n");
  214. }
  215. mdp->pwm_src_set = true;
  216. }
  217. mtk_disp_pwm_update_bits(mdp, DISP_PWM_EN, mdp->data->enable_mask,
  218. mdp->data->enable_mask);
  219. return 0;
  220. }
  221. static int mtk_disp_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  222. {
  223. struct mtk_disp_pwm *mdp = to_mtk_disp_pwm(chip);
  224. return mtk_disp_pwm_enable_impl(mdp);
  225. }
  226. static void mtk_disp_pwm_disable_impl(struct mtk_disp_pwm *mdp)
  227. {
  228. dev_dbg(mdp->chip.dev, "%s\n", __func__);
  229. mtk_disp_pwm_update_bits(mdp, DISP_PWM_EN, mdp->data->enable_mask,
  230. 0x0);
  231. clk_disable(mdp->clk_mm);
  232. clk_disable(mdp->clk_main);
  233. pwm_src_power_off(mdp);
  234. }
  235. static void mtk_disp_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  236. {
  237. struct mtk_disp_pwm *mdp = to_mtk_disp_pwm(chip);
  238. mtk_disp_pwm_disable_impl(mdp);
  239. }
  240. static const struct pwm_ops mtk_disp_pwm_ops = {
  241. .config = mtk_disp_pwm_config,
  242. .enable = mtk_disp_pwm_enable,
  243. .disable = mtk_disp_pwm_disable,
  244. .owner = THIS_MODULE,
  245. };
  246. static struct mtk_disp_pwm *g_mdp;
  247. static int mtk_disp_pwm_probe(struct platform_device *pdev)
  248. {
  249. struct resource *r;
  250. struct clk *pwm_src;
  251. int ret;
  252. pr_notice("%s start\n", __func__);
  253. g_mdp = devm_kzalloc(&pdev->dev, sizeof(*g_mdp), GFP_KERNEL);
  254. if (!g_mdp)
  255. return -ENOMEM;
  256. g_mdp->data = of_device_get_match_data(&pdev->dev);
  257. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  258. g_mdp->base = devm_ioremap_resource(&pdev->dev, r);
  259. if (IS_ERR(g_mdp->base))
  260. return PTR_ERR(g_mdp->base);
  261. g_mdp->clk_main = devm_clk_get(&pdev->dev, "main");
  262. if (IS_ERR(g_mdp->clk_main))
  263. return PTR_ERR(g_mdp->clk_main);
  264. g_mdp->clk_mm = devm_clk_get(&pdev->dev, "mm");
  265. if (IS_ERR(g_mdp->clk_mm))
  266. return PTR_ERR(g_mdp->clk_mm);
  267. ret = clk_prepare(g_mdp->clk_main);
  268. if (ret < 0)
  269. return ret;
  270. ret = clk_prepare(g_mdp->clk_mm);
  271. if (ret < 0)
  272. goto disable_clk_main;
  273. g_mdp->clk_source = devm_clk_get(&pdev->dev, "pwm_src");
  274. get_pwm_src_base(&pdev->dev, g_mdp);
  275. g_mdp->chip.dev = &pdev->dev;
  276. g_mdp->chip.ops = &mtk_disp_pwm_ops;
  277. g_mdp->chip.base = -1;
  278. g_mdp->chip.npwm = 1;
  279. ret = pwmchip_add(&g_mdp->chip);
  280. if (ret < 0) {
  281. dev_err(&pdev->dev, "pwmchip_add() failed: %d\n", ret);
  282. goto disable_clk_mm;
  283. }
  284. platform_set_drvdata(pdev, g_mdp);
  285. /*
  286. * For MT2701, disable double buffer before writing register
  287. * and select manual mode and use PWM_PERIOD/PWM_HIGH_WIDTH.
  288. */
  289. if (!g_mdp->data->has_commit) {
  290. mtk_disp_pwm_update_bits(g_mdp, g_mdp->data->bls_debug,
  291. g_mdp->data->bls_debug_mask,
  292. g_mdp->data->bls_debug_mask);
  293. mtk_disp_pwm_update_bits(g_mdp, g_mdp->data->con0,
  294. g_mdp->data->con0_sel,
  295. g_mdp->data->con0_sel);
  296. }
  297. pr_notice("%s end\n", __func__);
  298. return 0;
  299. disable_clk_mm:
  300. clk_unprepare(g_mdp->clk_mm);
  301. disable_clk_main:
  302. clk_unprepare(g_mdp->clk_main);
  303. return ret;
  304. }
  305. static int mtk_disp_pwm_remove(struct platform_device *pdev)
  306. {
  307. struct mtk_disp_pwm *mdp = platform_get_drvdata(pdev);
  308. int ret;
  309. ret = pwmchip_remove(&mdp->chip);
  310. clk_unprepare(mdp->clk_mm);
  311. clk_unprepare(mdp->clk_main);
  312. return ret;
  313. }
  314. static const struct mtk_pwm_data mt2701_pwm_data = {
  315. .enable_mask = BIT(16),
  316. .con0 = 0xa8,
  317. .con0_sel = 0x2,
  318. .con1 = 0xac,
  319. .has_commit = false,
  320. .bls_debug = 0xb0,
  321. .bls_debug_mask = 0x3,
  322. };
  323. static const struct mtk_pwm_data mt8173_pwm_data = {
  324. .enable_mask = BIT(0),
  325. .con0 = 0x10,
  326. .con0_sel = 0x0,
  327. .con1 = 0x14,
  328. .has_commit = true,
  329. .commit = 0x8,
  330. .commit_mask = 0x1,
  331. };
  332. static const struct mtk_pwm_data mt6799_pwm_data = {
  333. .enable_mask = BIT(0),
  334. .con0 = 0x18,
  335. .con0_sel = 0x0,
  336. .con1 = 0x1C,
  337. .has_commit = true,
  338. .commit = 0xC,
  339. .commit_mask = 0x1,
  340. };
  341. static const struct of_device_id mtk_disp_pwm_of_match[] = {
  342. {.compatible = "mediatek,mt2701-disp-pwm", .data = &mt2701_pwm_data},
  343. {.compatible = "mediatek,mt6595-disp-pwm", .data = &mt8173_pwm_data},
  344. {.compatible = "mediatek,mt6779-disp-pwm", .data = &mt6799_pwm_data},
  345. {.compatible = "mediatek,mt6873-disp-pwm", .data = &mt6799_pwm_data},
  346. {.compatible = "mediatek,mt6885-disp-pwm", .data = &mt6799_pwm_data},
  347. {.compatible = "mediatek,mt6853-disp-pwm", .data = &mt6799_pwm_data},
  348. {.compatible = "mediatek,mt8173-disp-pwm", .data = &mt8173_pwm_data},
  349. {.compatible = "mediatek,mt6833-disp-pwm", .data = &mt6799_pwm_data},
  350. {}
  351. };
  352. MODULE_DEVICE_TABLE(of, mtk_disp_pwm_of_match);
  353. static struct platform_driver mtk_disp_pwm_driver = {
  354. .driver = {
  355. .name = "mediatek-disp-pwm",
  356. .of_match_table = mtk_disp_pwm_of_match,
  357. },
  358. .probe = mtk_disp_pwm_probe,
  359. .remove = mtk_disp_pwm_remove,
  360. };
  361. module_platform_driver(mtk_disp_pwm_driver);
  362. static s32 disp_pwm_ut_case;
  363. int disp_pwm_ut_set(const char *val, const struct kernel_param *kp)
  364. {
  365. int result;
  366. int value1, value2;
  367. result = sscanf(val, "%d %d %d", &disp_pwm_ut_case,
  368. &value1, &value2);
  369. if (result != 3) {
  370. pr_notice("invalid input: %s, result(%d)\n", val, result);
  371. return -EINVAL;
  372. }
  373. pr_notice("%s: case_id=%d\n", __func__, disp_pwm_ut_case);
  374. switch (disp_pwm_ut_case) {
  375. case 0:
  376. pr_notice("use read to dump current pwm setting\n");
  377. break;
  378. case 1:
  379. pr_notice("use read to dump pwm watch\n");
  380. break;
  381. case 2:
  382. pr_notice("test: duty_ns=%d period_ns=%d\n", value1, value2);
  383. mtk_disp_pwm_config_impl(g_mdp, value1, value2);
  384. break;
  385. case 3:
  386. pr_notice("enable clock\n");
  387. mtk_disp_pwm_enable_impl(g_mdp);
  388. break;
  389. case 4:
  390. pr_notice("disable clock\n");
  391. mtk_disp_pwm_disable_impl(g_mdp);
  392. break;
  393. default:
  394. pr_notice("invalid case_id: %d\n", disp_pwm_ut_case);
  395. break;
  396. }
  397. pr_notice("%s END\n", __func__);
  398. return 0;
  399. }
  400. int disp_pwm_ut_get(char *buf, const struct kernel_param *kp)
  401. {
  402. int length = 0;
  403. switch (disp_pwm_ut_case) {
  404. case 0:
  405. case 2:
  406. length += snprintf(buf + length, PAGE_SIZE - length,
  407. "con0: 0x%08x\n",
  408. readl(g_mdp->base + g_mdp->data->con0));
  409. length += snprintf(buf + length, PAGE_SIZE - length,
  410. "con1: 0x%08x\n",
  411. readl(g_mdp->base + g_mdp->data->con1));
  412. break;
  413. case 1:
  414. length += snprintf(buf + length, PAGE_SIZE - length,
  415. "watch2: 0x%08x\n", readl(g_mdp->base + 0x30));
  416. break;
  417. default:
  418. pr_notice("not support read for case_id: %d\n",
  419. disp_pwm_ut_case);
  420. break;
  421. }
  422. buf[length] = '\0';
  423. return length;
  424. }
  425. static struct kernel_param_ops disp_pwm_ops = {
  426. .set = disp_pwm_ut_set,
  427. .get = disp_pwm_ut_get,
  428. };
  429. module_param_cb(disp_pwm_case, &disp_pwm_ops, NULL, 0644);
  430. MODULE_PARM_DESC(disp_pwm_case, "force mtk disp pwm UT test case");
  431. MODULE_AUTHOR("YH Huang <yh.huang@mediatek.com>");
  432. MODULE_DESCRIPTION("MediaTek SoC display PWM driver");
  433. MODULE_LICENSE("GPL v2");