pwm-meson.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright (c) 2016 BayLibre, SAS.
  8. * Author: Neil Armstrong <narmstrong@baylibre.com>
  9. * Copyright (C) 2014 Amlogic, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of version 2 of the GNU General Public License as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  22. * The full GNU General Public License is included in this distribution
  23. * in the file called COPYING.
  24. *
  25. * BSD LICENSE
  26. *
  27. * Copyright (c) 2016 BayLibre, SAS.
  28. * Author: Neil Armstrong <narmstrong@baylibre.com>
  29. * Copyright (C) 2014 Amlogic, Inc.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions
  33. * are met:
  34. *
  35. * * Redistributions of source code must retain the above copyright
  36. * notice, this list of conditions and the following disclaimer.
  37. * * Redistributions in binary form must reproduce the above copyright
  38. * notice, this list of conditions and the following disclaimer in
  39. * the documentation and/or other materials provided with the
  40. * distribution.
  41. * * Neither the name of Intel Corporation nor the names of its
  42. * contributors may be used to endorse or promote products derived
  43. * from this software without specific prior written permission.
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  46. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  47. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  48. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  49. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  51. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  52. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  53. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  54. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  55. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. */
  57. #include <linux/clk.h>
  58. #include <linux/clk-provider.h>
  59. #include <linux/err.h>
  60. #include <linux/io.h>
  61. #include <linux/kernel.h>
  62. #include <linux/module.h>
  63. #include <linux/of.h>
  64. #include <linux/of_device.h>
  65. #include <linux/platform_device.h>
  66. #include <linux/pwm.h>
  67. #include <linux/slab.h>
  68. #include <linux/spinlock.h>
  69. #define REG_PWM_A 0x0
  70. #define REG_PWM_B 0x4
  71. #define PWM_HIGH_SHIFT 16
  72. #define REG_MISC_AB 0x8
  73. #define MISC_B_CLK_EN BIT(23)
  74. #define MISC_A_CLK_EN BIT(15)
  75. #define MISC_CLK_DIV_MASK 0x7f
  76. #define MISC_B_CLK_DIV_SHIFT 16
  77. #define MISC_A_CLK_DIV_SHIFT 8
  78. #define MISC_B_CLK_SEL_SHIFT 6
  79. #define MISC_A_CLK_SEL_SHIFT 4
  80. #define MISC_CLK_SEL_WIDTH 2
  81. #define MISC_B_EN BIT(1)
  82. #define MISC_A_EN BIT(0)
  83. static const unsigned int mux_reg_shifts[] = {
  84. MISC_A_CLK_SEL_SHIFT,
  85. MISC_B_CLK_SEL_SHIFT
  86. };
  87. struct meson_pwm_channel {
  88. unsigned int hi;
  89. unsigned int lo;
  90. u8 pre_div;
  91. struct pwm_state state;
  92. struct clk *clk_parent;
  93. struct clk_mux mux;
  94. struct clk *clk;
  95. };
  96. struct meson_pwm_data {
  97. const char * const *parent_names;
  98. unsigned int num_parents;
  99. };
  100. struct meson_pwm {
  101. struct pwm_chip chip;
  102. const struct meson_pwm_data *data;
  103. void __iomem *base;
  104. u8 inverter_mask;
  105. spinlock_t lock;
  106. };
  107. static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
  108. {
  109. return container_of(chip, struct meson_pwm, chip);
  110. }
  111. static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  112. {
  113. struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
  114. struct device *dev = chip->dev;
  115. int err;
  116. if (!channel)
  117. return -ENODEV;
  118. if (channel->clk_parent) {
  119. err = clk_set_parent(channel->clk, channel->clk_parent);
  120. if (err < 0) {
  121. dev_err(dev, "failed to set parent %s for %s: %d\n",
  122. __clk_get_name(channel->clk_parent),
  123. __clk_get_name(channel->clk), err);
  124. return err;
  125. }
  126. }
  127. err = clk_prepare_enable(channel->clk);
  128. if (err < 0) {
  129. dev_err(dev, "failed to enable clock %s: %d\n",
  130. __clk_get_name(channel->clk), err);
  131. return err;
  132. }
  133. chip->ops->get_state(chip, pwm, &channel->state);
  134. return 0;
  135. }
  136. static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  137. {
  138. struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
  139. if (channel)
  140. clk_disable_unprepare(channel->clk);
  141. }
  142. static int meson_pwm_calc(struct meson_pwm *meson,
  143. struct meson_pwm_channel *channel, unsigned int id,
  144. unsigned int duty, unsigned int period)
  145. {
  146. unsigned int pre_div, cnt, duty_cnt;
  147. unsigned long fin_freq = -1;
  148. u64 fin_ps;
  149. if (~(meson->inverter_mask >> id) & 0x1)
  150. duty = period - duty;
  151. if (period == channel->state.period &&
  152. duty == channel->state.duty_cycle)
  153. return 0;
  154. fin_freq = clk_get_rate(channel->clk);
  155. if (fin_freq == 0) {
  156. dev_err(meson->chip.dev, "invalid source clock frequency\n");
  157. return -EINVAL;
  158. }
  159. dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq);
  160. fin_ps = (u64)NSEC_PER_SEC * 1000;
  161. do_div(fin_ps, fin_freq);
  162. /* Calc pre_div with the period */
  163. for (pre_div = 0; pre_div < MISC_CLK_DIV_MASK; pre_div++) {
  164. cnt = DIV_ROUND_CLOSEST_ULL((u64)period * 1000,
  165. fin_ps * (pre_div + 1));
  166. dev_dbg(meson->chip.dev, "fin_ps=%llu pre_div=%u cnt=%u\n",
  167. fin_ps, pre_div, cnt);
  168. if (cnt <= 0xffff)
  169. break;
  170. }
  171. if (pre_div == MISC_CLK_DIV_MASK) {
  172. dev_err(meson->chip.dev, "unable to get period pre_div\n");
  173. return -EINVAL;
  174. }
  175. dev_dbg(meson->chip.dev, "period=%u pre_div=%u cnt=%u\n", period,
  176. pre_div, cnt);
  177. if (duty == period) {
  178. channel->pre_div = pre_div;
  179. channel->hi = cnt;
  180. channel->lo = 0;
  181. } else if (duty == 0) {
  182. channel->pre_div = pre_div;
  183. channel->hi = 0;
  184. channel->lo = cnt;
  185. } else {
  186. /* Then check is we can have the duty with the same pre_div */
  187. duty_cnt = DIV_ROUND_CLOSEST_ULL((u64)duty * 1000,
  188. fin_ps * (pre_div + 1));
  189. if (duty_cnt > 0xffff) {
  190. dev_err(meson->chip.dev, "unable to get duty cycle\n");
  191. return -EINVAL;
  192. }
  193. dev_dbg(meson->chip.dev, "duty=%u pre_div=%u duty_cnt=%u\n",
  194. duty, pre_div, duty_cnt);
  195. channel->pre_div = pre_div;
  196. channel->hi = duty_cnt;
  197. channel->lo = cnt - duty_cnt;
  198. }
  199. return 0;
  200. }
  201. static void meson_pwm_enable(struct meson_pwm *meson,
  202. struct meson_pwm_channel *channel,
  203. unsigned int id)
  204. {
  205. u32 value, clk_shift, clk_enable, enable;
  206. unsigned int offset;
  207. switch (id) {
  208. case 0:
  209. clk_shift = MISC_A_CLK_DIV_SHIFT;
  210. clk_enable = MISC_A_CLK_EN;
  211. enable = MISC_A_EN;
  212. offset = REG_PWM_A;
  213. break;
  214. case 1:
  215. clk_shift = MISC_B_CLK_DIV_SHIFT;
  216. clk_enable = MISC_B_CLK_EN;
  217. enable = MISC_B_EN;
  218. offset = REG_PWM_B;
  219. break;
  220. default:
  221. return;
  222. }
  223. value = readl(meson->base + REG_MISC_AB);
  224. value &= ~(MISC_CLK_DIV_MASK << clk_shift);
  225. value |= channel->pre_div << clk_shift;
  226. value |= clk_enable;
  227. writel(value, meson->base + REG_MISC_AB);
  228. value = (channel->hi << PWM_HIGH_SHIFT) | channel->lo;
  229. writel(value, meson->base + offset);
  230. value = readl(meson->base + REG_MISC_AB);
  231. value |= enable;
  232. writel(value, meson->base + REG_MISC_AB);
  233. }
  234. static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
  235. {
  236. u32 value, enable;
  237. switch (id) {
  238. case 0:
  239. enable = MISC_A_EN;
  240. break;
  241. case 1:
  242. enable = MISC_B_EN;
  243. break;
  244. default:
  245. return;
  246. }
  247. value = readl(meson->base + REG_MISC_AB);
  248. value &= ~enable;
  249. writel(value, meson->base + REG_MISC_AB);
  250. }
  251. static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  252. struct pwm_state *state)
  253. {
  254. struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
  255. struct meson_pwm *meson = to_meson_pwm(chip);
  256. unsigned long flags;
  257. int err = 0;
  258. if (!state)
  259. return -EINVAL;
  260. spin_lock_irqsave(&meson->lock, flags);
  261. if (!state->enabled) {
  262. meson_pwm_disable(meson, pwm->hwpwm);
  263. channel->state.enabled = false;
  264. goto unlock;
  265. }
  266. if (state->period != channel->state.period ||
  267. state->duty_cycle != channel->state.duty_cycle ||
  268. state->polarity != channel->state.polarity) {
  269. if (channel->state.enabled) {
  270. meson_pwm_disable(meson, pwm->hwpwm);
  271. channel->state.enabled = false;
  272. }
  273. if (state->polarity != channel->state.polarity) {
  274. if (state->polarity == PWM_POLARITY_NORMAL)
  275. meson->inverter_mask |= BIT(pwm->hwpwm);
  276. else
  277. meson->inverter_mask &= ~BIT(pwm->hwpwm);
  278. }
  279. err = meson_pwm_calc(meson, channel, pwm->hwpwm,
  280. state->duty_cycle, state->period);
  281. if (err < 0)
  282. goto unlock;
  283. channel->state.polarity = state->polarity;
  284. channel->state.period = state->period;
  285. channel->state.duty_cycle = state->duty_cycle;
  286. }
  287. if (state->enabled && !channel->state.enabled) {
  288. meson_pwm_enable(meson, channel, pwm->hwpwm);
  289. channel->state.enabled = true;
  290. }
  291. unlock:
  292. spin_unlock_irqrestore(&meson->lock, flags);
  293. return err;
  294. }
  295. static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  296. struct pwm_state *state)
  297. {
  298. struct meson_pwm *meson = to_meson_pwm(chip);
  299. u32 value, mask;
  300. if (!state)
  301. return;
  302. switch (pwm->hwpwm) {
  303. case 0:
  304. mask = MISC_A_EN;
  305. break;
  306. case 1:
  307. mask = MISC_B_EN;
  308. break;
  309. default:
  310. return;
  311. }
  312. value = readl(meson->base + REG_MISC_AB);
  313. state->enabled = (value & mask) != 0;
  314. }
  315. static const struct pwm_ops meson_pwm_ops = {
  316. .request = meson_pwm_request,
  317. .free = meson_pwm_free,
  318. .apply = meson_pwm_apply,
  319. .get_state = meson_pwm_get_state,
  320. .owner = THIS_MODULE,
  321. };
  322. static const char * const pwm_meson8b_parent_names[] = {
  323. "xtal", "vid_pll", "fclk_div4", "fclk_div3"
  324. };
  325. static const struct meson_pwm_data pwm_meson8b_data = {
  326. .parent_names = pwm_meson8b_parent_names,
  327. .num_parents = ARRAY_SIZE(pwm_meson8b_parent_names),
  328. };
  329. static const char * const pwm_gxbb_parent_names[] = {
  330. "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
  331. };
  332. static const struct meson_pwm_data pwm_gxbb_data = {
  333. .parent_names = pwm_gxbb_parent_names,
  334. .num_parents = ARRAY_SIZE(pwm_gxbb_parent_names),
  335. };
  336. /*
  337. * Only the 2 first inputs of the GXBB AO PWMs are valid
  338. * The last 2 are grounded
  339. */
  340. static const char * const pwm_gxbb_ao_parent_names[] = {
  341. "xtal", "clk81"
  342. };
  343. static const struct meson_pwm_data pwm_gxbb_ao_data = {
  344. .parent_names = pwm_gxbb_ao_parent_names,
  345. .num_parents = ARRAY_SIZE(pwm_gxbb_ao_parent_names),
  346. };
  347. static const char * const pwm_axg_ee_parent_names[] = {
  348. "xtal", "fclk_div5", "fclk_div4", "fclk_div3"
  349. };
  350. static const struct meson_pwm_data pwm_axg_ee_data = {
  351. .parent_names = pwm_axg_ee_parent_names,
  352. .num_parents = ARRAY_SIZE(pwm_axg_ee_parent_names),
  353. };
  354. static const char * const pwm_axg_ao_parent_names[] = {
  355. "aoclk81", "xtal", "fclk_div4", "fclk_div5"
  356. };
  357. static const struct meson_pwm_data pwm_axg_ao_data = {
  358. .parent_names = pwm_axg_ao_parent_names,
  359. .num_parents = ARRAY_SIZE(pwm_axg_ao_parent_names),
  360. };
  361. static const struct of_device_id meson_pwm_matches[] = {
  362. {
  363. .compatible = "amlogic,meson8b-pwm",
  364. .data = &pwm_meson8b_data
  365. },
  366. {
  367. .compatible = "amlogic,meson-gxbb-pwm",
  368. .data = &pwm_gxbb_data
  369. },
  370. {
  371. .compatible = "amlogic,meson-gxbb-ao-pwm",
  372. .data = &pwm_gxbb_ao_data
  373. },
  374. {
  375. .compatible = "amlogic,meson-axg-ee-pwm",
  376. .data = &pwm_axg_ee_data
  377. },
  378. {
  379. .compatible = "amlogic,meson-axg-ao-pwm",
  380. .data = &pwm_axg_ao_data
  381. },
  382. {},
  383. };
  384. MODULE_DEVICE_TABLE(of, meson_pwm_matches);
  385. static int meson_pwm_init_channels(struct meson_pwm *meson,
  386. struct meson_pwm_channel *channels)
  387. {
  388. struct device *dev = meson->chip.dev;
  389. struct clk_init_data init;
  390. unsigned int i;
  391. char name[255];
  392. int err;
  393. for (i = 0; i < meson->chip.npwm; i++) {
  394. struct meson_pwm_channel *channel = &channels[i];
  395. snprintf(name, sizeof(name), "%s#mux%u", dev_name(dev), i);
  396. init.name = name;
  397. init.ops = &clk_mux_ops;
  398. init.flags = CLK_IS_BASIC;
  399. init.parent_names = meson->data->parent_names;
  400. init.num_parents = meson->data->num_parents;
  401. channel->mux.reg = meson->base + REG_MISC_AB;
  402. channel->mux.shift = mux_reg_shifts[i];
  403. channel->mux.mask = BIT(MISC_CLK_SEL_WIDTH) - 1;
  404. channel->mux.flags = 0;
  405. channel->mux.lock = &meson->lock;
  406. channel->mux.table = NULL;
  407. channel->mux.hw.init = &init;
  408. channel->clk = devm_clk_register(dev, &channel->mux.hw);
  409. if (IS_ERR(channel->clk)) {
  410. err = PTR_ERR(channel->clk);
  411. dev_err(dev, "failed to register %s: %d\n", name, err);
  412. return err;
  413. }
  414. snprintf(name, sizeof(name), "clkin%u", i);
  415. channel->clk_parent = devm_clk_get(dev, name);
  416. if (IS_ERR(channel->clk_parent)) {
  417. err = PTR_ERR(channel->clk_parent);
  418. if (err == -EPROBE_DEFER)
  419. return err;
  420. channel->clk_parent = NULL;
  421. }
  422. }
  423. return 0;
  424. }
  425. static void meson_pwm_add_channels(struct meson_pwm *meson,
  426. struct meson_pwm_channel *channels)
  427. {
  428. unsigned int i;
  429. for (i = 0; i < meson->chip.npwm; i++)
  430. pwm_set_chip_data(&meson->chip.pwms[i], &channels[i]);
  431. }
  432. static int meson_pwm_probe(struct platform_device *pdev)
  433. {
  434. struct meson_pwm_channel *channels;
  435. struct meson_pwm *meson;
  436. struct resource *regs;
  437. int err;
  438. meson = devm_kzalloc(&pdev->dev, sizeof(*meson), GFP_KERNEL);
  439. if (!meson)
  440. return -ENOMEM;
  441. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  442. meson->base = devm_ioremap_resource(&pdev->dev, regs);
  443. if (IS_ERR(meson->base))
  444. return PTR_ERR(meson->base);
  445. spin_lock_init(&meson->lock);
  446. meson->chip.dev = &pdev->dev;
  447. meson->chip.ops = &meson_pwm_ops;
  448. meson->chip.base = -1;
  449. meson->chip.npwm = 2;
  450. meson->chip.of_xlate = of_pwm_xlate_with_flags;
  451. meson->chip.of_pwm_n_cells = 3;
  452. meson->data = of_device_get_match_data(&pdev->dev);
  453. meson->inverter_mask = BIT(meson->chip.npwm) - 1;
  454. channels = devm_kcalloc(&pdev->dev, meson->chip.npwm,
  455. sizeof(*channels), GFP_KERNEL);
  456. if (!channels)
  457. return -ENOMEM;
  458. err = meson_pwm_init_channels(meson, channels);
  459. if (err < 0)
  460. return err;
  461. err = pwmchip_add(&meson->chip);
  462. if (err < 0) {
  463. dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err);
  464. return err;
  465. }
  466. meson_pwm_add_channels(meson, channels);
  467. platform_set_drvdata(pdev, meson);
  468. return 0;
  469. }
  470. static int meson_pwm_remove(struct platform_device *pdev)
  471. {
  472. struct meson_pwm *meson = platform_get_drvdata(pdev);
  473. return pwmchip_remove(&meson->chip);
  474. }
  475. static struct platform_driver meson_pwm_driver = {
  476. .driver = {
  477. .name = "meson-pwm",
  478. .of_match_table = meson_pwm_matches,
  479. },
  480. .probe = meson_pwm_probe,
  481. .remove = meson_pwm_remove,
  482. };
  483. module_platform_driver(meson_pwm_driver);
  484. MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
  485. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  486. MODULE_LICENSE("Dual BSD/GPL");