pwm-meson.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. };
  99. struct meson_pwm {
  100. struct pwm_chip chip;
  101. const struct meson_pwm_data *data;
  102. void __iomem *base;
  103. u8 inverter_mask;
  104. spinlock_t lock;
  105. };
  106. static inline struct meson_pwm *to_meson_pwm(struct pwm_chip *chip)
  107. {
  108. return container_of(chip, struct meson_pwm, chip);
  109. }
  110. static int meson_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  111. {
  112. struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
  113. struct device *dev = chip->dev;
  114. int err;
  115. if (!channel)
  116. return -ENODEV;
  117. if (channel->clk_parent) {
  118. err = clk_set_parent(channel->clk, channel->clk_parent);
  119. if (err < 0) {
  120. dev_err(dev, "failed to set parent %s for %s: %d\n",
  121. __clk_get_name(channel->clk_parent),
  122. __clk_get_name(channel->clk), err);
  123. return err;
  124. }
  125. }
  126. err = clk_prepare_enable(channel->clk);
  127. if (err < 0) {
  128. dev_err(dev, "failed to enable clock %s: %d\n",
  129. __clk_get_name(channel->clk), err);
  130. return err;
  131. }
  132. chip->ops->get_state(chip, pwm, &channel->state);
  133. return 0;
  134. }
  135. static void meson_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  136. {
  137. struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
  138. if (channel)
  139. clk_disable_unprepare(channel->clk);
  140. }
  141. static int meson_pwm_calc(struct meson_pwm *meson,
  142. struct meson_pwm_channel *channel, unsigned int id,
  143. unsigned int duty, unsigned int period)
  144. {
  145. unsigned int pre_div, cnt, duty_cnt;
  146. unsigned long fin_freq = -1, fin_ns;
  147. if (~(meson->inverter_mask >> id) & 0x1)
  148. duty = period - duty;
  149. if (period == channel->state.period &&
  150. duty == channel->state.duty_cycle)
  151. return 0;
  152. fin_freq = clk_get_rate(channel->clk);
  153. if (fin_freq == 0) {
  154. dev_err(meson->chip.dev, "invalid source clock frequency\n");
  155. return -EINVAL;
  156. }
  157. dev_dbg(meson->chip.dev, "fin_freq: %lu Hz\n", fin_freq);
  158. fin_ns = NSEC_PER_SEC / fin_freq;
  159. /* Calc pre_div with the period */
  160. for (pre_div = 0; pre_div < MISC_CLK_DIV_MASK; pre_div++) {
  161. cnt = DIV_ROUND_CLOSEST(period, fin_ns * (pre_div + 1));
  162. dev_dbg(meson->chip.dev, "fin_ns=%lu pre_div=%u cnt=%u\n",
  163. fin_ns, pre_div, cnt);
  164. if (cnt <= 0xffff)
  165. break;
  166. }
  167. if (pre_div == MISC_CLK_DIV_MASK) {
  168. dev_err(meson->chip.dev, "unable to get period pre_div\n");
  169. return -EINVAL;
  170. }
  171. dev_dbg(meson->chip.dev, "period=%u 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 = DIV_ROUND_CLOSEST(duty, fin_ns * (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=%u 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,
  197. struct meson_pwm_channel *channel,
  198. unsigned int id)
  199. {
  200. u32 value, clk_shift, clk_enable, enable;
  201. unsigned int offset;
  202. switch (id) {
  203. case 0:
  204. clk_shift = MISC_A_CLK_DIV_SHIFT;
  205. clk_enable = MISC_A_CLK_EN;
  206. enable = MISC_A_EN;
  207. offset = REG_PWM_A;
  208. break;
  209. case 1:
  210. clk_shift = MISC_B_CLK_DIV_SHIFT;
  211. clk_enable = MISC_B_CLK_EN;
  212. enable = MISC_B_EN;
  213. offset = REG_PWM_B;
  214. break;
  215. default:
  216. return;
  217. }
  218. value = readl(meson->base + REG_MISC_AB);
  219. value &= ~(MISC_CLK_DIV_MASK << clk_shift);
  220. value |= channel->pre_div << clk_shift;
  221. value |= clk_enable;
  222. writel(value, meson->base + REG_MISC_AB);
  223. value = (channel->hi << PWM_HIGH_SHIFT) | channel->lo;
  224. writel(value, meson->base + offset);
  225. value = readl(meson->base + REG_MISC_AB);
  226. value |= enable;
  227. writel(value, meson->base + REG_MISC_AB);
  228. }
  229. static void meson_pwm_disable(struct meson_pwm *meson, unsigned int id)
  230. {
  231. u32 value, enable;
  232. switch (id) {
  233. case 0:
  234. enable = MISC_A_EN;
  235. break;
  236. case 1:
  237. enable = MISC_B_EN;
  238. break;
  239. default:
  240. return;
  241. }
  242. value = readl(meson->base + REG_MISC_AB);
  243. value &= ~enable;
  244. writel(value, meson->base + REG_MISC_AB);
  245. }
  246. static int meson_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  247. struct pwm_state *state)
  248. {
  249. struct meson_pwm_channel *channel = pwm_get_chip_data(pwm);
  250. struct meson_pwm *meson = to_meson_pwm(chip);
  251. unsigned long flags;
  252. int err = 0;
  253. if (!state)
  254. return -EINVAL;
  255. spin_lock_irqsave(&meson->lock, flags);
  256. if (!state->enabled) {
  257. meson_pwm_disable(meson, pwm->hwpwm);
  258. channel->state.enabled = false;
  259. goto unlock;
  260. }
  261. if (state->period != channel->state.period ||
  262. state->duty_cycle != channel->state.duty_cycle ||
  263. state->polarity != channel->state.polarity) {
  264. if (channel->state.enabled) {
  265. meson_pwm_disable(meson, pwm->hwpwm);
  266. channel->state.enabled = false;
  267. }
  268. if (state->polarity != channel->state.polarity) {
  269. if (state->polarity == PWM_POLARITY_NORMAL)
  270. meson->inverter_mask |= BIT(pwm->hwpwm);
  271. else
  272. meson->inverter_mask &= ~BIT(pwm->hwpwm);
  273. }
  274. err = meson_pwm_calc(meson, channel, pwm->hwpwm,
  275. state->duty_cycle, state->period);
  276. if (err < 0)
  277. goto unlock;
  278. channel->state.polarity = state->polarity;
  279. channel->state.period = state->period;
  280. channel->state.duty_cycle = state->duty_cycle;
  281. }
  282. if (state->enabled && !channel->state.enabled) {
  283. meson_pwm_enable(meson, channel, pwm->hwpwm);
  284. channel->state.enabled = true;
  285. }
  286. unlock:
  287. spin_unlock_irqrestore(&meson->lock, flags);
  288. return err;
  289. }
  290. static void meson_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  291. struct pwm_state *state)
  292. {
  293. struct meson_pwm *meson = to_meson_pwm(chip);
  294. u32 value, mask;
  295. if (!state)
  296. return;
  297. switch (pwm->hwpwm) {
  298. case 0:
  299. mask = MISC_A_EN;
  300. break;
  301. case 1:
  302. mask = MISC_B_EN;
  303. break;
  304. default:
  305. return;
  306. }
  307. value = readl(meson->base + REG_MISC_AB);
  308. state->enabled = (value & mask) != 0;
  309. }
  310. static const struct pwm_ops meson_pwm_ops = {
  311. .request = meson_pwm_request,
  312. .free = meson_pwm_free,
  313. .apply = meson_pwm_apply,
  314. .get_state = meson_pwm_get_state,
  315. .owner = THIS_MODULE,
  316. };
  317. static const char * const pwm_meson8b_parent_names[] = {
  318. "xtal", "vid_pll", "fclk_div4", "fclk_div3"
  319. };
  320. static const struct meson_pwm_data pwm_meson8b_data = {
  321. .parent_names = pwm_meson8b_parent_names,
  322. };
  323. static const char * const pwm_gxbb_parent_names[] = {
  324. "xtal", "hdmi_pll", "fclk_div4", "fclk_div3"
  325. };
  326. static const struct meson_pwm_data pwm_gxbb_data = {
  327. .parent_names = pwm_gxbb_parent_names,
  328. };
  329. static const struct of_device_id meson_pwm_matches[] = {
  330. { .compatible = "amlogic,meson8b-pwm", .data = &pwm_meson8b_data },
  331. { .compatible = "amlogic,meson-gxbb-pwm", .data = &pwm_gxbb_data },
  332. {},
  333. };
  334. MODULE_DEVICE_TABLE(of, meson_pwm_matches);
  335. static int meson_pwm_init_channels(struct meson_pwm *meson,
  336. struct meson_pwm_channel *channels)
  337. {
  338. struct device *dev = meson->chip.dev;
  339. struct device_node *np = dev->of_node;
  340. struct clk_init_data init;
  341. unsigned int i;
  342. char name[255];
  343. int err;
  344. for (i = 0; i < meson->chip.npwm; i++) {
  345. struct meson_pwm_channel *channel = &channels[i];
  346. snprintf(name, sizeof(name), "%s#mux%u", np->full_name, i);
  347. init.name = name;
  348. init.ops = &clk_mux_ops;
  349. init.flags = CLK_IS_BASIC;
  350. init.parent_names = meson->data->parent_names;
  351. init.num_parents = 1 << MISC_CLK_SEL_WIDTH;
  352. channel->mux.reg = meson->base + REG_MISC_AB;
  353. channel->mux.shift = mux_reg_shifts[i];
  354. channel->mux.mask = BIT(MISC_CLK_SEL_WIDTH) - 1;
  355. channel->mux.flags = 0;
  356. channel->mux.lock = &meson->lock;
  357. channel->mux.table = NULL;
  358. channel->mux.hw.init = &init;
  359. channel->clk = devm_clk_register(dev, &channel->mux.hw);
  360. if (IS_ERR(channel->clk)) {
  361. err = PTR_ERR(channel->clk);
  362. dev_err(dev, "failed to register %s: %d\n", name, err);
  363. return err;
  364. }
  365. snprintf(name, sizeof(name), "clkin%u", i);
  366. channel->clk_parent = devm_clk_get(dev, name);
  367. if (IS_ERR(channel->clk_parent)) {
  368. err = PTR_ERR(channel->clk_parent);
  369. if (err == -EPROBE_DEFER)
  370. return err;
  371. channel->clk_parent = NULL;
  372. }
  373. }
  374. return 0;
  375. }
  376. static void meson_pwm_add_channels(struct meson_pwm *meson,
  377. struct meson_pwm_channel *channels)
  378. {
  379. unsigned int i;
  380. for (i = 0; i < meson->chip.npwm; i++)
  381. pwm_set_chip_data(&meson->chip.pwms[i], &channels[i]);
  382. }
  383. static int meson_pwm_probe(struct platform_device *pdev)
  384. {
  385. struct meson_pwm_channel *channels;
  386. struct meson_pwm *meson;
  387. struct resource *regs;
  388. int err;
  389. meson = devm_kzalloc(&pdev->dev, sizeof(*meson), GFP_KERNEL);
  390. if (!meson)
  391. return -ENOMEM;
  392. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  393. meson->base = devm_ioremap_resource(&pdev->dev, regs);
  394. if (IS_ERR(meson->base))
  395. return PTR_ERR(meson->base);
  396. spin_lock_init(&meson->lock);
  397. meson->chip.dev = &pdev->dev;
  398. meson->chip.ops = &meson_pwm_ops;
  399. meson->chip.base = -1;
  400. meson->chip.npwm = 2;
  401. meson->chip.of_xlate = of_pwm_xlate_with_flags;
  402. meson->chip.of_pwm_n_cells = 3;
  403. meson->data = of_device_get_match_data(&pdev->dev);
  404. meson->inverter_mask = BIT(meson->chip.npwm) - 1;
  405. channels = devm_kcalloc(&pdev->dev, meson->chip.npwm, sizeof(*meson),
  406. GFP_KERNEL);
  407. if (!channels)
  408. return -ENOMEM;
  409. err = meson_pwm_init_channels(meson, channels);
  410. if (err < 0)
  411. return err;
  412. err = pwmchip_add(&meson->chip);
  413. if (err < 0) {
  414. dev_err(&pdev->dev, "failed to register PWM chip: %d\n", err);
  415. return err;
  416. }
  417. meson_pwm_add_channels(meson, channels);
  418. platform_set_drvdata(pdev, meson);
  419. return 0;
  420. }
  421. static int meson_pwm_remove(struct platform_device *pdev)
  422. {
  423. struct meson_pwm *meson = platform_get_drvdata(pdev);
  424. return pwmchip_remove(&meson->chip);
  425. }
  426. static struct platform_driver meson_pwm_driver = {
  427. .driver = {
  428. .name = "meson-pwm",
  429. .of_match_table = meson_pwm_matches,
  430. },
  431. .probe = meson_pwm_probe,
  432. .remove = meson_pwm_remove,
  433. };
  434. module_platform_driver(meson_pwm_driver);
  435. MODULE_ALIAS("platform:meson-pwm");
  436. MODULE_DESCRIPTION("Amlogic Meson PWM Generator driver");
  437. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
  438. MODULE_LICENSE("Dual BSD/GPL");