pwm-meson.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * PWM controller driver for Amlogic Meson SoCs.
  4. *
  5. * This PWM is only a set of Gates, Dividers and Counters:
  6. * PWM output is achieved by calculating a clock that permits calculating
  7. * two periods (low and high). The counter then has to be set to switch after
  8. * N cycles for the first half period.
  9. * The hardware has no "polarity" setting. This driver reverses the period
  10. * cycles (the low length is inverted with the high length) for
  11. * PWM_POLARITY_INVERSED. This means that .get_state cannot read the polarity
  12. * from the hardware.
  13. * Setting the duty cycle will disable and re-enable the PWM output.
  14. * Disabling the PWM stops the output immediately (without waiting for the
  15. * current period to complete first).
  16. *
  17. * The public S912 (GXM) datasheet contains some documentation for this PWM
  18. * controller starting on page 543:
  19. * https://dl.khadas.com/Hardware/VIM2/Datasheet/S912_Datasheet_V0.220170314publicversion-Wesion.pdf
  20. * An updated version of this IP block is found in S922X (G12B) SoCs. The
  21. * datasheet contains the description for this IP block revision starting at
  22. * page 1084:
  23. * https://dn.odroid.com/S922X/ODROID-N2/Datasheet/S922X_Public_Datasheet_V0.2.pdf
  24. *
  25. * Copyright (c) 2016 BayLibre, SAS.
  26. * Author: Neil Armstrong <[email protected]>
  27. * Copyright (C) 2014 Amlogic, Inc.
  28. */
  29. #include <linux/bitfield.h>
  30. #include <linux/bits.h>
  31. #include <linux/clk.h>
  32. #include <linux/clk-provider.h>
  33. #include <linux/err.h>
  34. #include <linux/io.h>
  35. #include <linux/kernel.h>
  36. #include <linux/math64.h>
  37. #include <linux/module.h>
  38. #include <linux/of.h>
  39. #include <linux/of_device.h>
  40. #include <linux/platform_device.h>
  41. #include <linux/pwm.h>
  42. #include <linux/slab.h>
  43. #include <linux/spinlock.h>
  44. #define REG_PWM_A 0x0
  45. #define REG_PWM_B 0x4
  46. #define PWM_LOW_MASK GENMASK(15, 0)
  47. #define PWM_HIGH_MASK GENMASK(31, 16)
  48. #define REG_MISC_AB 0x8
  49. #define MISC_B_CLK_EN BIT(23)
  50. #define MISC_A_CLK_EN BIT(15)
  51. #define MISC_CLK_DIV_MASK 0x7f
  52. #define MISC_B_CLK_DIV_SHIFT 16
  53. #define MISC_A_CLK_DIV_SHIFT 8
  54. #define MISC_B_CLK_SEL_SHIFT 6
  55. #define MISC_A_CLK_SEL_SHIFT 4
  56. #define MISC_CLK_SEL_MASK 0x3
  57. #define MISC_B_EN BIT(1)
  58. #define MISC_A_EN BIT(0)
  59. #define MESON_NUM_PWMS 2
  60. static struct meson_pwm_channel_data {
  61. u8 reg_offset;
  62. u8 clk_sel_shift;
  63. u8 clk_div_shift;
  64. u32 clk_en_mask;
  65. u32 pwm_en_mask;
  66. } meson_pwm_per_channel_data[MESON_NUM_PWMS] = {
  67. {
  68. .reg_offset = REG_PWM_A,
  69. .clk_sel_shift = MISC_A_CLK_SEL_SHIFT,
  70. .clk_div_shift = MISC_A_CLK_DIV_SHIFT,
  71. .clk_en_mask = MISC_A_CLK_EN,
  72. .pwm_en_mask = MISC_A_EN,
  73. },
  74. {
  75. .reg_offset = REG_PWM_B,
  76. .clk_sel_shift = MISC_B_CLK_SEL_SHIFT,
  77. .clk_div_shift = MISC_B_CLK_DIV_SHIFT,
  78. .clk_en_mask = MISC_B_CLK_EN,
  79. .pwm_en_mask = MISC_B_EN,
  80. }
  81. };
  82. struct meson_pwm_channel {
  83. unsigned int hi;
  84. unsigned int lo;
  85. u8 pre_div;
  86. struct clk *clk_parent;
  87. struct clk_mux mux;
  88. struct clk *clk;
  89. };
  90. struct meson_pwm_data {
  91. const char * const *parent_names;
  92. unsigned int num_parents;
  93. };
  94. struct meson_pwm {
  95. struct pwm_chip chip;
  96. const struct meson_pwm_data *data;
  97. struct meson_pwm_channel channels[MESON_NUM_PWMS];
  98. void __iomem *base;
  99. /*
  100. * Protects register (write) access to the REG_MISC_AB register
  101. * that is shared between the two PWMs.
  102. */
  103. spinlock_t lock;
  104. };
  105. static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
  106. {
  107. return container_of(chip, struct meson_pwm, chip);
  108. }
  109. static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  110. {
  111. struct meson_pwm *meson = to_meson_pwm(chip);
  112. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  113. struct device *dev = chip->dev;
  114. int err;
  115. if (channel->clk_parent) {
  116. err = clk_set_parent(channel->clk, channel->clk_parent);
  117. if (err < 0) {
  118. dev_err(dev, "failed to set parent %s for %s: %d\n",
  119. __clk_get_name(channel->clk_parent),
  120. __clk_get_name(channel->clk), err);
  121. return err;
  122. }
  123. }
  124. err = clk_prepare_enable(channel->clk);
  125. if (err < 0) {
  126. dev_err(dev, "failed to enable clock %s: %d\n",
  127. __clk_get_name(channel->clk), err);
  128. return err;
  129. }
  130. return 0;
  131. }
  132. static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  133. {
  134. struct meson_pwm *meson = to_meson_pwm(chip);
  135. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  136. clk_disable_unprepare(channel->clk);
  137. }
  138. static int meson_pwm_calc(struct meson_pwm *meson, struct pwm_device *pwm,
  139. const struct pwm_state *state)
  140. {
  141. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  142. unsigned int pre_div, cnt, duty_cnt;
  143. unsigned long fin_freq;
  144. u64 duty, period;
  145. duty = state->duty_cycle;
  146. period = state->period;
  147. /*
  148. * Note this is wrong. The result is an output wave that isn't really
  149. * inverted and so is wrongly identified by .get_state as normal.
  150. * Fixing this needs some care however as some machines might rely on
  151. * this.
  152. */
  153. if (state->polarity == PWM_POLARITY_INVERSED)
  154. duty = period - duty;
  155. fin_freq = clk_get_rate(channel->clk);
  156. if (fin_freq == 0) {
  157. dev_err(meson->chip.dev, "invalid source clock frequency\n");
  158. return -EINVAL;
  159. }
  160. dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq);
  161. pre_div = div64_u64(fin_freq * period, NSEC_PER_SEC * 0xffffLL);
  162. if (pre_div > MISC_CLK_DIV_MASK) {
  163. dev_err(meson->chip.dev, "unable to get period pre_div\n");
  164. return -EINVAL;
  165. }
  166. cnt = div64_u64(fin_freq * period, NSEC_PER_SEC * (pre_div + 1));
  167. if (cnt > 0xffff) {
  168. dev_err(meson->chip.dev, "unable to get period cnt\n");
  169. return -EINVAL;
  170. }
  171. dev_dbg(meson->chip.dev, "period=%llu pre_div=%u cnt=%u\n", period,
  172. pre_div, cnt);
  173. if (duty == period) {
  174. channel->pre_div = pre_div;
  175. channel->hi = cnt;
  176. channel->lo = 0;
  177. } else if (duty == 0) {
  178. channel->pre_div = pre_div;
  179. channel->hi = 0;
  180. channel->lo = cnt;
  181. } else {
  182. /* Then check is we can have the duty with the same pre_div */
  183. duty_cnt = div64_u64(fin_freq * duty, NSEC_PER_SEC * (pre_div + 1));
  184. if (duty_cnt > 0xffff) {
  185. dev_err(meson->chip.dev, "unable to get duty cycle\n");
  186. return -EINVAL;
  187. }
  188. dev_dbg(meson->chip.dev, "duty=%llu pre_div=%u duty_cnt=%u\n",
  189. duty, pre_div, duty_cnt);
  190. channel->pre_div = pre_div;
  191. channel->hi = duty_cnt;
  192. channel->lo = cnt - duty_cnt;
  193. }
  194. return 0;
  195. }
  196. static void meson_pwm_enable(struct meson_pwm *meson, struct pwm_device *pwm)
  197. {
  198. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  199. struct meson_pwm_channel_data *channel_data;
  200. unsigned long flags;
  201. u32 value;
  202. channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
  203. spin_lock_irqsave(&meson->lock, flags);
  204. value = readl(meson->base + REG_MISC_AB);
  205. value &= ~(MISC_CLK_DIV_MASK << channel_data->clk_div_shift);
  206. value |= channel->pre_div << channel_data->clk_div_shift;
  207. value |= channel_data->clk_en_mask;
  208. writel(value, meson->base + REG_MISC_AB);
  209. value = FIELD_PREP(PWM_HIGH_MASK, channel->hi) |
  210. FIELD_PREP(PWM_LOW_MASK, channel->lo);
  211. writel(value, meson->base + channel_data->reg_offset);
  212. value = readl(meson->base + REG_MISC_AB);
  213. value |= channel_data->pwm_en_mask;
  214. writel(value, meson->base + REG_MISC_AB);
  215. spin_unlock_irqrestore(&meson->lock, flags);
  216. }
  217. static void meson_pwm_disable(struct meson_pwm *meson, struct pwm_device *pwm)
  218. {
  219. unsigned long flags;
  220. u32 value;
  221. spin_lock_irqsave(&meson->lock, flags);
  222. value = readl(meson->base + REG_MISC_AB);
  223. value &= ~meson_pwm_per_channel_data[pwm->hwpwm].pwm_en_mask;
  224. writel(value, meson->base + REG_MISC_AB);
  225. spin_unlock_irqrestore(&meson->lock, flags);
  226. }
  227. static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  228. const struct pwm_state *state)
  229. {
  230. struct meson_pwm *meson = to_meson_pwm(chip);
  231. struct meson_pwm_channel *channel = &meson->channels[pwm->hwpwm];
  232. int err = 0;
  233. if (!state->enabled) {
  234. if (state->polarity == PWM_POLARITY_INVERSED) {
  235. /*
  236. * This IP block revision doesn't have an "always high"
  237. * setting which we can use for "inverted disabled".
  238. * Instead we achieve this using the same settings
  239. * that we use a pre_div of 0 (to get the shortest
  240. * possible duration for one "count") and
  241. * "period == duty_cycle". This results in a signal
  242. * which is LOW for one "count", while being HIGH for
  243. * the rest of the (so the signal is HIGH for slightly
  244. * less than 100% of the period, but this is the best
  245. * we can achieve).
  246. */
  247. channel->pre_div = 0;
  248. channel->hi = ~0;
  249. channel->lo = 0;
  250. meson_pwm_enable(meson, pwm);
  251. } else {
  252. meson_pwm_disable(meson, pwm);
  253. }
  254. } else {
  255. err = meson_pwm_calc(meson, pwm, state);
  256. if (err < 0)
  257. return err;
  258. meson_pwm_enable(meson, pwm);
  259. }
  260. return 0;
  261. }
  262. static unsigned int meson_pwm_cnt_to_ns(struct pwm_chip *chip,
  263. struct pwm_device *pwm, u32 cnt)
  264. {
  265. struct meson_pwm *meson = to_meson_pwm(chip);
  266. struct meson_pwm_channel *channel;
  267. unsigned long fin_freq;
  268. u32 fin_ns;
  269. /* to_meson_pwm() can only be used after .get_state() is called */
  270. channel = &meson->channels[pwm->hwpwm];
  271. fin_freq = clk_get_rate(channel->clk);
  272. if (fin_freq == 0)
  273. return 0;
  274. fin_ns = div_u64(NSEC_PER_SEC, fin_freq);
  275. return cnt * fin_ns * (channel->pre_div + 1);
  276. }
  277. static int meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  278. struct pwm_state *state)
  279. {
  280. struct meson_pwm *meson = to_meson_pwm(chip);
  281. struct meson_pwm_channel_data *channel_data;
  282. struct meson_pwm_channel *channel;
  283. u32 value, tmp;
  284. if (!state)
  285. return 0;
  286. channel = &meson->channels[pwm->hwpwm];
  287. channel_data = &meson_pwm_per_channel_data[pwm->hwpwm];
  288. value = readl(meson->base + REG_MISC_AB);
  289. tmp = channel_data->pwm_en_mask | channel_data->clk_en_mask;
  290. state->enabled = (value & tmp) == tmp;
  291. tmp = value >> channel_data->clk_div_shift;
  292. channel->pre_div = FIELD_GET(MISC_CLK_DIV_MASK, tmp);
  293. value = readl(meson->base + channel_data->reg_offset);
  294. channel->lo = FIELD_GET(PWM_LOW_MASK, value);
  295. channel->hi = FIELD_GET(PWM_HIGH_MASK, value);
  296. state->period = meson_pwm_cnt_to_ns(chip, pwm, channel->lo + channel->hi);
  297. state->duty_cycle = meson_pwm_cnt_to_ns(chip, pwm, channel->hi);
  298. state->polarity = PWM_POLARITY_NORMAL;
  299. return 0;
  300. }
  301. static const struct pwm_ops meson_pwm_ops = {
  302. .request = meson_pwm_request,
  303. .free = meson_pwm_free,
  304. .apply = meson_pwm_apply,
  305. .get_state = meson_pwm_get_state,
  306. .owner = THIS_MODULE,
  307. };
  308. static const char * const pwm_meson8b_parent_names[] = {
  309. "xtal", "vid_pll", "fclk_div4", "fclk_div3"
  310. };
  311. static const struct meson_pwm_data pwm_meson8b_data = {
  312. .parent_names = pwm_meson8b_parent_names,
  313. .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names),
  314. };
  315. static const char * const pwm_gxbb_parent_names[] = {
  316. "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
  317. };
  318. static const struct meson_pwm_data pwm_gxbb_data = {
  319. .parent_names = pwm_gxbb_parent_names,
  320. .num_parents = ARRAY_SIZE(pwm_gxbb_parent_names),
  321. };
  322. /*
  323. * Only the 2 first inputs of the GXBB AO PWMs are valid
  324. * The last 2 are grounded
  325. */
  326. static const char * const pwm_gxbb_ao_parent_names[] = {
  327. "xtal", "clk81"
  328. };
  329. static const struct meson_pwm_data pwm_gxbb_ao_data = {
  330. .parent_names = pwm_gxbb_ao_parent_names,
  331. .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_names),
  332. };
  333. static const char * const pwm_axg_ee_parent_names[] = {
  334. "xtal", "fclk_div5", "fclk_div4", "fclk_div3"
  335. };
  336. static const struct meson_pwm_data pwm_axg_ee_data = {
  337. .parent_names = pwm_axg_ee_parent_names,
  338. .num_parents = ARRAY_SIZE(pwm_axg_ee_parent_names),
  339. };
  340. static const char * const pwm_axg_ao_parent_names[] = {
  341. "xtal", "axg_ao_clk81", "fclk_div4", "fclk_div5"
  342. };
  343. static const struct meson_pwm_data pwm_axg_ao_data = {
  344. .parent_names = pwm_axg_ao_parent_names,
  345. .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_names),
  346. };
  347. static const char * const pwm_g12a_ao_ab_parent_names[] = {
  348. "xtal", "g12a_ao_clk81", "fclk_div4", "fclk_div5"
  349. };
  350. static const struct meson_pwm_data pwm_g12a_ao_ab_data = {
  351. .parent_names = pwm_g12a_ao_ab_parent_names,
  352. .num_parents = ARRAY_SIZE(pwm_g12a_ao_ab_parent_names),
  353. };
  354. static const char * const pwm_g12a_ao_cd_parent_names[] = {
  355. "xtal", "g12a_ao_clk81",
  356. };
  357. static const struct meson_pwm_data pwm_g12a_ao_cd_data = {
  358. .parent_names = pwm_g12a_ao_cd_parent_names,
  359. .num_parents = ARRAY_SIZE(pwm_g12a_ao_cd_parent_names),
  360. };
  361. static const char * const pwm_g12a_ee_parent_names[] = {
  362. "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
  363. };
  364. static const struct meson_pwm_data pwm_g12a_ee_data = {
  365. .parent_names = pwm_g12a_ee_parent_names,
  366. .num_parents = ARRAY_SIZE(pwm_g12a_ee_parent_names),
  367. };
  368. static const struct of_device_id meson_pwm_matches[] = {
  369. {
  370. .compatible = "amlogic,meson8b-pwm",
  371. .data = &pwm_meson8b_data
  372. },
  373. {
  374. .compatible = "amlogic,meson-gxbb-pwm",
  375. .data = &pwm_gxbb_data
  376. },
  377. {
  378. .compatible = "amlogic,meson-gxbb-ao-pwm",
  379. .data = &pwm_gxbb_ao_data
  380. },
  381. {
  382. .compatible = "amlogic,meson-axg-ee-pwm",
  383. .data = &pwm_axg_ee_data
  384. },
  385. {
  386. .compatible = "amlogic,meson-axg-ao-pwm",
  387. .data = &pwm_axg_ao_data
  388. },
  389. {
  390. .compatible = "amlogic,meson-g12a-ee-pwm",
  391. .data = &pwm_g12a_ee_data
  392. },
  393. {
  394. .compatible = "amlogic,meson-g12a-ao-pwm-ab",
  395. .data = &pwm_g12a_ao_ab_data
  396. },
  397. {
  398. .compatible = "amlogic,meson-g12a-ao-pwm-cd",
  399. .data = &pwm_g12a_ao_cd_data
  400. },
  401. {},
  402. };
  403. MODULE_DEVICE_TABLE(of, meson_pwm_matches);
  404. static int meson_pwm_init_channels(struct meson_pwm *meson)
  405. {
  406. struct device *dev = meson->chip.dev;
  407. struct clk_init_data init;
  408. unsigned int i;
  409. char name[255];
  410. int err;
  411. for (i = 0; i < meson->chip.npwm; i++) {
  412. struct meson_pwm_channel *channel = &meson->channels[i];
  413. snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i);
  414. init.name = name;
  415. init.ops = &clk_mux_ops;
  416. init.flags = 0;
  417. init.parent_names = meson->data->parent_names;
  418. init.num_parents = meson->data->num_parents;
  419. channel->mux.reg = meson->base + REG_MISC_AB;
  420. channel->mux.shift =
  421. meson_pwm_per_channel_data[i].clk_sel_shift;
  422. channel->mux.mask = MISC_CLK_SEL_MASK;
  423. channel->mux.flags = 0;
  424. channel->mux.lock = &meson->lock;
  425. channel->mux.table = NULL;
  426. channel->mux.hw.init = &init;
  427. channel->clk = devm_clk_register(dev, &channel->mux.hw);
  428. if (IS_ERR(channel->clk)) {
  429. err = PTR_ERR(channel->clk);
  430. dev_err(dev, "failed to register %s: %d\n", name, err);
  431. return err;
  432. }
  433. snprintf(name, sizeof(name), "clkin%u", i);
  434. channel->clk_parent = devm_clk_get_optional(dev, name);
  435. if (IS_ERR(channel->clk_parent))
  436. return PTR_ERR(channel->clk_parent);
  437. }
  438. return 0;
  439. }
  440. static int meson_pwm_probe(struct platform_device *pdev)
  441. {
  442. struct meson_pwm *meson;
  443. int err;
  444. meson = devm_kzalloc(&pdev->dev, sizeof(*meson), GFP_KERNEL);
  445. if (!meson)
  446. return -ENOMEM;
  447. meson->base = devm_platform_ioremap_resource(pdev, 0);
  448. if (IS_ERR(meson->base))
  449. return PTR_ERR(meson->base);
  450. spin_lock_init(&meson->lock);
  451. meson->chip.dev = &pdev->dev;
  452. meson->chip.ops = &meson_pwm_ops;
  453. meson->chip.npwm = MESON_NUM_PWMS;
  454. meson->data = of_device_get_match_data(&pdev->dev);
  455. err = meson_pwm_init_channels(meson);
  456. if (err < 0)
  457. return err;
  458. err = devm_pwmchip_add(&pdev->dev, &meson->chip);
  459. if (err < 0) {
  460. dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err);
  461. return err;
  462. }
  463. return 0;
  464. }
  465. static struct platform_driver meson_pwm_driver = {
  466. .driver = {
  467. .name = "meson-pwm",
  468. .of_match_table = meson_pwm_matches,
  469. },
  470. .probe = meson_pwm_probe,
  471. };
  472. module_platform_driver(meson_pwm_driver);
  473. MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
  474. MODULE_AUTHOR("Neil Armstrong <[email protected]>");
  475. MODULE_LICENSE("Dual BSD/GPL");